diff --git a/OVMF/OVMF_VARS_4M.fd b/OVMF/OVMF_VARS_4M.fd index 28ba5cc..e276d0b 100644 Binary files a/OVMF/OVMF_VARS_4M.fd and b/OVMF/OVMF_VARS_4M.fd differ diff --git a/OVMF/OVMF_VARS_4M_d.fd b/OVMF/OVMF_VARS_4M_d.fd index f5cb681..14b4bbe 100644 Binary files a/OVMF/OVMF_VARS_4M_d.fd and b/OVMF/OVMF_VARS_4M_d.fd differ diff --git a/code/boot.ini b/code/boot.ini index c4b0d0a..4be974f 100644 --- a/code/boot.ini +++ b/code/boot.ini @@ -1,5 +1,7 @@ [boot] ; Structural system-volume selector consumed by the bootloader and kernel storage stack. +; mbr/<8-hex-disk-signature>/ +; gpt/ system_volume = mbr/4E544C31/2 [kernel] diff --git a/code/build/build.c b/code/build/build.c index 2f1089f..8c3f04f 100644 --- a/code/build/build.c +++ b/code/build/build.c @@ -10,6 +10,8 @@ #define KERNEL_CFLAGS_LITERAL COMMON_CFLAGS_LITERAL " /Icode\\kernel\\include" #define DRIVER_CFLAGS_LITERAL COMMON_CFLAGS_LITERAL #define USER_CFLAGS_LITERAL COMMON_CFLAGS_LITERAL +// POSIX targets own their standard C/POSIX headers; /X prevents the host UCRT from entering the target ABI. +#define POSIX_USER_CFLAGS_LITERAL "/X /Icode\\public\\posix /Icode\\public\\freestanding " USER_CFLAGS_LITERAL #define BOOT_CFLAGS_LITERAL KERNEL_CFLAGS_LITERAL #define USER_HEAP_OBJS_LITERAL "obj\\runtime\\user_heap.obj obj\\runtime\\user_heap_exports.obj" #define NATIVE_RUNTIME_OBJS_LITERAL "obj\\runtime\\native_exception.obj obj\\runtime\\native_start.obj" @@ -21,6 +23,8 @@ #define NATIVE_EXE_ENTRY_LITERAL "/ENTRY:NtlNativeStart" #define USER_IMAGE_FLAGS_LITERAL "/DYNAMICBASE /HIGHENTROPYVA" #define LWIP_CFLAGS_LITERAL DRIVER_CFLAGS_LITERAL " /Icode\\drivers\\net\\lwip\\include /Icode\\drivers\\net\\lwip\\third_party\\lwip\\src\\include /wd4005 /wd4100 /wd4103 /wd4127 /wd4244 /wd4267 /wd4706" +#define EXT4_CFLAGS_LITERAL DRIVER_CFLAGS_LITERAL " /Icode\\drivers\\fs\\ext4\\include /Icode\\drivers\\fs\\ext4\\third_party\\lwext4\\include /DCONFIG_USE_DEFAULT_CFG=1 /DCONFIG_HAVE_OWN_ERRNO=1 /DCONFIG_DEBUG_PRINTF=0 /DCONFIG_DEBUG_ASSERT=0 /DCONFIG_BLOCK_DEV_ENABLE_STATS=0 /DCONFIG_EXT4_BLOCKDEVS_COUNT=8 /DCONFIG_EXT4_MOUNTPOINTS_COUNT=8 /wd4005 /wd4100 /wd4101 /wd4103 /wd4127 /wd4244 /wd4267 /wd4706 /wd4996" +#define NTFS3G_CFLAGS_LITERAL DRIVER_CFLAGS_LITERAL " /Icode\\drivers\\fs\\ntfs\\ntfs3g\\include /Icode\\drivers\\fs\\ntfs\\ntfs3g\\third_party\\ntfs-3g\\include /Icode\\drivers\\fs\\ntfs\\ntfs3g\\third_party\\ntfs-3g\\include\\ntfs-3g /DHAVE_CONFIG_H=1 /DNO_NTFS_DEVICE_DEFAULT_IO_OPS=1 /wd4005 /wd4018 /wd4100 /wd4101 /wd4103 /wd4127 /wd4133 /wd4146 /wd4244 /wd4267 /wd4305 /wd4311 /wd4312 /wd4706 /wd4996" #define LFLAGS_LITERAL "/nologo /NODEFAULTLIB /DEBUG" // Partition offsets inside disk.vhd @@ -31,8 +35,11 @@ const char* CommonCflags = COMMON_CFLAGS_LITERAL; const char* KernelCflags = KERNEL_CFLAGS_LITERAL; const char* UserCflags = USER_CFLAGS_LITERAL; +const char* PosixUserCflags = POSIX_USER_CFLAGS_LITERAL; const char* BootCflags = BOOT_CFLAGS_LITERAL; const char* LwipCflags = LWIP_CFLAGS_LITERAL; +const char* Ext4Cflags = EXT4_CFLAGS_LITERAL; +const char* Ntfs3gCflags = NTFS3G_CFLAGS_LITERAL; const char* LFLAGS = LFLAGS_LITERAL; typedef struct Driver_Target { @@ -56,8 +63,7 @@ static const Driver_Target STANDALONE_DRIVERS[] = { DRIVER_TARGET_LIBS("hda", "obj\\drivers\\hda", "code\\drivers\\audio\\hda\\hda.c code\\drivers\\audio\\hda\\hda_transport.c code\\drivers\\audio\\hda\\hda_codec.c", "bin\\pci.lib"), DRIVER_TARGET_LIBS("ata", "obj\\drivers\\ata", "code\\drivers\\storage\\ata\\ata.c", "bin\\pci.lib"), DRIVER_TARGET_LIBS("ahci", "obj\\drivers\\ahci", "code\\drivers\\storage\\ahci\\ahci.c code\\drivers\\storage\\ahci\\ahci_port.c", "bin\\pci.lib"), - DRIVER_TARGET("fat", "obj\\drivers\\fat", "code\\drivers\\fs\\fat\\fat.c code\\drivers\\fs\\fat\\fat_mount.c"), - DRIVER_TARGET("ntfs", "obj\\drivers\\ntfs", "code\\drivers\\fs\\ntfs\\ntfs.c"), + DRIVER_TARGET("fat", "obj\\drivers\\fat", "code\\drivers\\fs\\fat\\fat.c code\\drivers\\fs\\fat\\fat_directory.c code\\drivers\\fs\\fat\\fat_file.c code\\drivers\\fs\\fat\\fat_async.c code\\drivers\\fs\\fat\\fat_driver.c code\\drivers\\fs\\fat\\fat_mount.c"), DRIVER_TARGET_LIBS("vmwaresvga", "obj\\drivers\\vmwaresvga", "code\\drivers\\display\\vmwaresvga\\vmwaresvga.c", "bin\\pci.lib"), DRIVER_TARGET_LIBS("usb", "obj\\drivers\\usb", "code\\drivers\\bus\\usb\\usb.c code\\drivers\\bus\\usb\\usb_bus.c code\\drivers\\bus\\usb\\usb_composite.c code\\drivers\\bus\\usb\\usb_core.c code\\drivers\\bus\\usb\\usb_xhci.c code\\drivers\\bus\\usb\\usb_hid.c code\\drivers\\bus\\usb\\usb_storage.c code\\drivers\\bus\\usb\\usb_hub.c", "bin\\pci.lib"), DRIVER_TARGET_LIBS("rtl816x", "obj\\drivers\\rtl816x", "code\\drivers\\net\\rtl816x\\rtl816x.c", "bin\\pci.lib"), @@ -67,14 +73,14 @@ static const Driver_Target STANDALONE_DRIVERS[] = { // Creates layout. void create_layout() { const char* dirs[] = { - "obj", "bin", "obj\\boot", "obj\\kernel", "obj\\lwip", "obj\\hal", - "obj\\syscall", "obj\\runtime", "obj\\win32", "obj\\posix", "obj\\drivers", "obj\\init", "obj\\rssrv", + "obj", "bin", "obj\\boot", "obj\\kernel", "obj\\lwip", "obj\\ext4", "obj\\ntfs3g", "obj\\hal", + "obj\\syscall", "obj\\runtime", "obj\\win32", "obj\\posix", "obj\\drivers", "obj\\init", "obj\\rssrv", "obj\\posixsrv", "obj\\dwm", "obj\\shell_win32", "obj\\shell_posix", "obj\\shell_native", "obj\\async_apc", "obj\\async_cancel", "obj\\apc_return_smoke", "obj\\edit", "obj\\httpget", "obj\\tcpserve", "obj\\udp", "obj\\resolve", "obj\\ping", "obj\\doom", "obj\\video", "obj\\memtop", - "obj\\file_stat_smoke", "obj\\handle_dup_smoke", "obj\\security_smoke", "obj\\port_ipc_smoke", "obj\\resource_service_smoke", "obj\\named_queue_lifetime_smoke", "obj\\input_api_smoke", "obj\\input_model_smoke", "obj\\timer_smoke", "obj\\stats_abi_smoke", "obj\\realpath_smoke", "obj\\file_lock_smoke", "obj\\process_protection_smoke", "obj\\process_exit_multithread_smoke", "obj\\process_builder_smoke", "obj\\socket_addr_smoke", "obj\\syscall_contract_smoke", "obj\\would_block_smoke", "obj\\audio_stream_smoke", "obj\\winmm_waveout_smoke", "obj\\win32_condition_variable_smoke", "obj\\win32_mutex_smoke", "obj\\win32_thread_suspend_smoke", "obj\\win32_module_lookup_smoke", "obj\\cwd_userspace_smoke", "obj\\mmap_smoke", "obj\\pcache_smoke", "obj\\pcache_pressure", "obj\\dirty_writeback", "obj\\shared_mmap", + "obj\\file_stat_smoke", "obj\\handle_dup_smoke", "obj\\security_smoke", "obj\\port_ipc_smoke", "obj\\resource_service_smoke", "obj\\named_queue_lifetime_smoke", "obj\\input_api_smoke", "obj\\input_model_smoke", "obj\\input_service_smoke", "obj\\console_service_smoke", "obj\\desktop_smoke", "obj\\timer_smoke", "obj\\stats_abi_smoke", "obj\\realpath_smoke", "obj\\file_lock_smoke", "obj\\process_protection_smoke", "obj\\process_exit_multithread_smoke", "obj\\thread_name_smoke", "obj\\process_builder_smoke", "obj\\socket_addr_smoke", "obj\\syscall_contract_smoke", "obj\\would_block_smoke", "obj\\audio_stream_smoke", "obj\\winmm_waveout_smoke", "obj\\win32_condition_variable_smoke", "obj\\win32_mutex_smoke", "obj\\win32_thread_suspend_smoke", "obj\\win32_module_lookup_smoke", "obj\\cwd_userspace_smoke", "obj\\mmap_smoke", "obj\\pcache_smoke", "obj\\pcache_pressure", "obj\\dirty_writeback", "obj\\shared_mmap", "obj\\pcache_invalidate", "obj\\pcache_mmap_read_reuse", "obj\\pcache_multithread", - "obj\\all_tests", "obj\\surface_smoke", "obj\\surface_invalid_rect", "obj\\surface_multithread_present", "obj\\net_irp_smoke", "obj\\module_load_smoke", "obj\\posix_module_smoke", "obj\\exception_dispatcher_smoke", "obj\\native_exception_runtime_smoke", "obj\\native_raise_exception_smoke", "obj\\exception_smoke", "obj\\win32_exception_smoke", "obj\\win32_seh_smoke" + "obj\\all_tests", "obj\\posix_service_smoke", "obj\\surface_smoke", "obj\\surface_invalid_rect", "obj\\surface_multithread_commit", "obj\\surface_cross_process_smoke", "obj\\net_irp_smoke", "obj\\module_load_smoke", "obj\\posix_module_smoke", "obj\\posix_runtime_smoke", "obj\\posix_fd_smoke", "obj\\posix_pipe_smoke", "obj\\exception_dispatcher_smoke", "obj\\native_exception_runtime_smoke", "obj\\native_raise_exception_smoke", "obj\\exception_smoke", "obj\\win32_exception_smoke", "obj\\win32_seh_smoke" }; for (size_t i = 0; i < NOB_ARRAY_LEN(dirs); i++) nob_mkdir_if_not_exists(dirs[i]); nob_mkdir_if_not_exists(PCI_BUS_DRIVER.obj_dir); @@ -108,14 +114,14 @@ bool clean_object_directory(const char* obj_dir) { // Cleans object directories that are linked through wildcards. bool clean_build_object_directories(void) { const char* dirs[] = { - "obj\\boot", "obj\\kernel", "obj\\lwip", "obj\\hal", + "obj\\boot", "obj\\kernel", "obj\\lwip", "obj\\ext4", "obj\\hal", "obj\\syscall", "obj\\runtime", "obj\\win32", "obj\\posix", - "obj\\init", "obj\\rssrv", "obj\\shell_win32", "obj\\shell_posix", "obj\\shell_native", + "obj\\init", "obj\\rssrv", "obj\\posixsrv", "obj\\dwm", "obj\\shell_win32", "obj\\shell_posix", "obj\\shell_native", "obj\\async_apc", "obj\\async_cancel", "obj\\apc_return_smoke", "obj\\edit", "obj\\httpget", "obj\\tcpserve", "obj\\udp", "obj\\resolve", "obj\\ping", - "obj\\doom", "obj\\video", "obj\\memtop", "obj\\file_stat_smoke", "obj\\handle_dup_smoke", "obj\\security_smoke", "obj\\port_ipc_smoke", "obj\\resource_service_smoke", "obj\\named_queue_lifetime_smoke", "obj\\input_api_smoke", "obj\\input_model_smoke", "obj\\timer_smoke", "obj\\stats_abi_smoke", "obj\\realpath_smoke", "obj\\file_lock_smoke", "obj\\process_protection_smoke", "obj\\process_exit_multithread_smoke", "obj\\process_builder_smoke", "obj\\socket_addr_smoke", "obj\\syscall_contract_smoke", "obj\\would_block_smoke", "obj\\audio_stream_smoke", "obj\\winmm_waveout_smoke", "obj\\win32_condition_variable_smoke", "obj\\win32_mutex_smoke", "obj\\win32_thread_suspend_smoke", "obj\\win32_module_lookup_smoke", "obj\\cwd_userspace_smoke", "obj\\mmap_smoke", "obj\\pcache_smoke", "obj\\pcache_pressure", "obj\\dirty_writeback", "obj\\shared_mmap", + "obj\\doom", "obj\\video", "obj\\memtop", "obj\\file_stat_smoke", "obj\\handle_dup_smoke", "obj\\security_smoke", "obj\\port_ipc_smoke", "obj\\resource_service_smoke", "obj\\named_queue_lifetime_smoke", "obj\\input_api_smoke", "obj\\input_model_smoke", "obj\\input_service_smoke", "obj\\console_service_smoke", "obj\\desktop_smoke", "obj\\timer_smoke", "obj\\stats_abi_smoke", "obj\\realpath_smoke", "obj\\file_lock_smoke", "obj\\process_protection_smoke", "obj\\process_exit_multithread_smoke", "obj\\thread_name_smoke", "obj\\process_builder_smoke", "obj\\socket_addr_smoke", "obj\\syscall_contract_smoke", "obj\\would_block_smoke", "obj\\audio_stream_smoke", "obj\\winmm_waveout_smoke", "obj\\win32_condition_variable_smoke", "obj\\win32_mutex_smoke", "obj\\win32_thread_suspend_smoke", "obj\\win32_module_lookup_smoke", "obj\\cwd_userspace_smoke", "obj\\mmap_smoke", "obj\\pcache_smoke", "obj\\pcache_pressure", "obj\\dirty_writeback", "obj\\shared_mmap", "obj\\pcache_invalidate", "obj\\pcache_mmap_read_reuse", "obj\\pcache_multithread", - "obj\\all_tests", "obj\\surface_smoke", "obj\\surface_invalid_rect", "obj\\surface_multithread_present", "obj\\net_irp_smoke", "obj\\module_load_smoke", "obj\\posix_module_smoke", "obj\\exception_dispatcher_smoke", "obj\\native_exception_runtime_smoke", "obj\\native_raise_exception_smoke", "obj\\exception_smoke", "obj\\win32_exception_smoke", "obj\\win32_seh_smoke" + "obj\\all_tests", "obj\\posix_service_smoke", "obj\\surface_smoke", "obj\\surface_invalid_rect", "obj\\surface_multithread_commit", "obj\\surface_cross_process_smoke", "obj\\net_irp_smoke", "obj\\module_load_smoke", "obj\\posix_module_smoke", "obj\\posix_runtime_smoke", "obj\\posix_fd_smoke", "obj\\posix_pipe_smoke", "obj\\exception_dispatcher_smoke", "obj\\native_exception_runtime_smoke", "obj\\native_raise_exception_smoke", "obj\\exception_smoke", "obj\\win32_exception_smoke", "obj\\win32_seh_smoke" }; for (size_t i = 0; i < NOB_ARRAY_LEN(dirs); i++) { @@ -321,6 +327,14 @@ int main(int argc, char** argv) { if (!create_disk_vhd_if_missing()) return 1; if (!create_usbfatroot_if_missing()) return 1; create_layout(); + if (!nob_file_exists("code\\drivers\\fs\\ext4\\third_party\\lwext4\\src\\ext4.c")) { + nob_log(NOB_ERROR, "lwext4 is not vendored. Run: powershell -ExecutionPolicy Bypass -File code\\drivers\\fs\\ext4\\vendor_lwext4.ps1"); + return 1; + } + if (!nob_file_exists("code\\drivers\\fs\\ntfs\\ntfs3g\\third_party\\ntfs-3g\\libntfs-3g\\volume.c")) { + nob_log(NOB_ERROR, "ntfs-3g is not vendored. Run: powershell -ExecutionPolicy Bypass -File code\\drivers\\fs\\ntfs\\ntfs3g\\vendor_ntfs3g.ps1"); + return 1; + } if (!clean_build_object_directories()) return 1; nob_log(NOB_INFO, "--- Building Core Subsystems ---"); @@ -339,6 +353,12 @@ int main(int argc, char** argv) { nob_temp_sprintf("cl %s /DNTL_SYSCALL_STATIC /c /Foobj\\syscall\\process_loader_s.obj code\\user\\syscall\\process_loader.c", UserCflags), nob_temp_sprintf("cl %s /DNTL_SYSCALL_BUILD /c /Foobj\\syscall\\resource.obj code\\user\\syscall\\resource.c", UserCflags), nob_temp_sprintf("cl %s /DNTL_SYSCALL_STATIC /c /Foobj\\syscall\\resource_s.obj code\\user\\syscall\\resource.c", UserCflags), + nob_temp_sprintf("cl %s /DNTL_SYSCALL_BUILD /c /Foobj\\syscall\\input_service.obj code\\user\\syscall\\input_service.c", UserCflags), + nob_temp_sprintf("cl %s /DNTL_SYSCALL_STATIC /c /Foobj\\syscall\\input_service_s.obj code\\user\\syscall\\input_service.c", UserCflags), + nob_temp_sprintf("cl %s /DNTL_SYSCALL_BUILD /c /Foobj\\syscall\\console_service.obj code\\user\\syscall\\console_service.c", UserCflags), + nob_temp_sprintf("cl %s /DNTL_SYSCALL_STATIC /c /Foobj\\syscall\\console_service_s.obj code\\user\\syscall\\console_service.c", UserCflags), + nob_temp_sprintf("cl %s /DNTL_SYSCALL_BUILD /c /Foobj\\syscall\\desktop.obj code\\user\\syscall\\desktop.c", UserCflags), + nob_temp_sprintf("cl %s /DNTL_SYSCALL_STATIC /c /Foobj\\syscall\\desktop_s.obj code\\user\\syscall\\desktop.c", UserCflags), nob_temp_sprintf("cl %s /DNTL_SYSCALL_BUILD /c /Foobj\\syscall\\path.obj code\\user\\syscall\\path.c", UserCflags), nob_temp_sprintf("cl %s /DNTL_SYSCALL_STATIC /c /Foobj\\syscall\\path_s.obj code\\user\\syscall\\path.c", UserCflags), "for %f in (code\\boot\\uefi\\*.asm) do ml64 /nologo /c /Fo\"obj\\boot\\%~nf.obj\" \"%f\"", @@ -349,7 +369,7 @@ int main(int argc, char** argv) { nob_temp_sprintf("cl %s /c /Foobj\\runtime\\user_heap_exports.obj code\\user\\runtime\\heap_exports.c", UserCflags), nob_temp_sprintf("cl %s /c /Foobj\\runtime\\native_exception.obj code\\user\\runtime\\native_exception.c", UserCflags), nob_temp_sprintf("cl %s /c /Foobj\\runtime\\native_start.obj code\\user\\runtime\\native_start.c", UserCflags), - nob_temp_sprintf("cl %s /c /Foobj\\runtime\\posix_start.obj code\\user\\runtime\\posix_start.c", UserCflags), + nob_temp_sprintf("cl %s /c /Foobj\\runtime\\posix_start.obj code\\user\\runtime\\posix_start.c", PosixUserCflags), // Bootloader. nob_temp_sprintf("cl %s /MP /c /Icode\\boot\\uefi\\include /Foobj\\boot\\ code\\boot\\uefi\\*.c code\\common\\boot_volume_selector.c && link %s /FORCE:MULTIPLE /SUBSYSTEM:EFI_APPLICATION /ENTRY:UefiMain /OUT:bin\\bootx64.efi obj\\libc.obj code\\boot\\uefi\\efilib.lib obj\\boot\\*.obj", BootCflags, LFLAGS), @@ -361,10 +381,10 @@ int main(int argc, char** argv) { nob_temp_sprintf("cl %s /DKE_DISPLAY_BUILD /MP /c /Foobj\\kernel\\ code\\kernel\\*.c code\\common\\boot_volume_selector.c && link %s /SUBSYSTEM:NATIVE /FIXED /DYNAMICBASE:NO /HIGHENTROPYVA:NO /RELEASE /BASE:0xFFFF800000000000 /ENTRY:KernelMain /OUT:bin\\kernel.exe bin\\hal.lib obj\\printf.obj obj\\libc.obj obj\\kernel\\*.obj", KernelCflags, LFLAGS), // Subsystem DLLs. - "link /nologo /SUBSYSTEM:NATIVE /NODEFAULTLIB /DLL " USER_IMAGE_FLAGS_LITERAL " /OUT:bin\\syscall.dll obj\\syscall\\stubs.obj obj\\syscall\\pe_loader.obj obj\\syscall\\module_loader.obj obj\\syscall\\process_loader.obj obj\\syscall\\resource.obj obj\\syscall\\path.obj obj\\libc.obj", - "lib /nologo /OUT:bin\\syscall_s.lib obj\\syscall\\stubs_s.obj obj\\syscall\\pe_loader_s.obj obj\\syscall\\module_loader_s.obj obj\\syscall\\process_loader_s.obj obj\\syscall\\resource_s.obj obj\\syscall\\path_s.obj", + "link /nologo /SUBSYSTEM:NATIVE /NODEFAULTLIB /DLL " USER_IMAGE_FLAGS_LITERAL " /OUT:bin\\syscall.dll obj\\syscall\\stubs.obj obj\\syscall\\pe_loader.obj obj\\syscall\\module_loader.obj obj\\syscall\\process_loader.obj obj\\syscall\\resource.obj obj\\syscall\\input_service.obj obj\\syscall\\console_service.obj obj\\syscall\\desktop.obj obj\\syscall\\path.obj obj\\libc.obj", + "lib /nologo /OUT:bin\\syscall_s.lib obj\\syscall\\stubs_s.obj obj\\syscall\\pe_loader_s.obj obj\\syscall\\module_loader_s.obj obj\\syscall\\process_loader_s.obj obj\\syscall\\resource_s.obj obj\\syscall\\input_service_s.obj obj\\syscall\\console_service_s.obj obj\\syscall\\desktop_s.obj obj\\syscall\\path_s.obj", nob_temp_sprintf("cl %s /DUSER_HEAP_EXPORTS /DNTL_EXCEPTION_EXPORTS /MP /c /Foobj\\win32\\ code\\user\\runtime\\heap.c code\\user\\runtime\\heap_exports.c code\\user\\runtime\\native_exception.c code\\user\\personalities\\win32\\*.c && link /nologo /SUBSYSTEM:NATIVE /NODEFAULTLIB /DLL " USER_IMAGE_FLAGS_LITERAL " /OUT:bin\\win32.dll bin\\syscall.lib obj\\libc.obj obj\\win32\\*.obj", UserCflags), - nob_temp_sprintf("cl %s /DUSER_HEAP_EXPORTS /MP /c /Foobj\\posix\\ code\\user\\runtime\\heap.c code\\user\\runtime\\heap_exports.c code\\user\\personalities\\posix\\*.c && link /nologo /SUBSYSTEM:NATIVE /NODEFAULTLIB /DLL " USER_IMAGE_FLAGS_LITERAL " /OUT:bin\\posix.dll bin\\syscall.lib obj\\libc.obj obj\\posix\\*.obj", UserCflags) + nob_temp_sprintf("cl %s /DNTL_POSIX_BUILD /DUSER_HEAP_EXPORTS /MP /c /Foobj\\posix\\ code\\user\\runtime\\heap.c code\\user\\runtime\\heap_exports.c code\\user\\personalities\\posix\\*.c && link /nologo /SUBSYSTEM:NATIVE /NODEFAULTLIB /DLL " USER_IMAGE_FLAGS_LITERAL " /OUT:bin\\posix.dll bin\\syscall.lib obj\\libc.obj obj\\posix\\*.obj", PosixUserCflags) }; for (size_t i = 0; i < NOB_ARRAY_LEN(core_tasks); i++) { @@ -407,10 +427,44 @@ int main(int argc, char** argv) { nob_da_append(&procs, run_async(nob_temp_sprintf("cl %s /MP /c /Foobj\\lwip\\ %s && link /nologo /SUBSYSTEM:NATIVE /NODEFAULTLIB /DLL /OUT:bin\\lwip.sys /ENTRY:DriverEntry bin\\kernel.lib obj\\libc.obj obj\\lwip\\*.obj", LwipCflags, lwip_files.items))); } + Nob_String_Builder ext4_sb = {0}, ext4_files = {0}; + if (nob_read_entire_file("code\\drivers\\fs\\ext4\\lwext4_sources.txt", &ext4_sb)) { + Nob_String_View sv = nob_sv_from_parts(ext4_sb.items, ext4_sb.count); + while (sv.count > 0) { + Nob_String_View line = nob_sv_trim(nob_sv_chop_by_delim(&sv, '\n')); + if (line.count > 0 && line.data[0] != '#') { + nob_sb_append_cstr(&ext4_files, "code\\"); + for (size_t i = 0; i < line.count; i++) nob_sb_append_buf(&ext4_files, &(char){line.data[i] == '/' ? '\\' : line.data[i]}, 1); + nob_sb_append_cstr(&ext4_files, " "); + } + } + nob_sb_append_null(&ext4_files); + nob_da_append(&procs, run_async(nob_temp_sprintf("cl %s /MP /c /Foobj\\ext4\\ %s && link /nologo /SUBSYSTEM:NATIVE /NODEFAULTLIB /DLL /OUT:bin\\ext4.sys /ENTRY:DriverEntry bin\\kernel.lib obj\\libc.obj obj\\ext4\\*.obj", Ext4Cflags, ext4_files.items))); + } + + Nob_String_Builder ntfs3g_sb = {0}, ntfs3g_files = {0}; + if (nob_read_entire_file("code\\drivers\\fs\\ntfs\\ntfs3g\\ntfs3g_sources.txt", &ntfs3g_sb)) { + Nob_String_View sv = nob_sv_from_parts(ntfs3g_sb.items, ntfs3g_sb.count); + while (sv.count > 0) { + Nob_String_View line = nob_sv_trim(nob_sv_chop_by_delim(&sv, '\n')); + if (line.count > 0 && line.data[0] != '#') { + nob_sb_append_cstr(&ntfs3g_files, "code\\"); + for (size_t i = 0; i < line.count; i++) nob_sb_append_buf(&ntfs3g_files, &(char){line.data[i] == '/' ? '\\' : line.data[i]}, 1); + nob_sb_append_cstr(&ntfs3g_files, " "); + } + } + nob_sb_append_null(&ntfs3g_files); + nob_da_append(&procs, run_async(nob_temp_sprintf("cl %s /MP /c /Foobj\\ntfs3g\\ %s && link /nologo /SUBSYSTEM:NATIVE /NODEFAULTLIB /DLL /OUT:bin\\ntfs.sys /ENTRY:DriverEntry bin\\kernel.lib obj\\printf.obj obj\\libc.obj obj\\ntfs3g\\*.obj", Ntfs3gCflags, ntfs3g_files.items))); + } + + const char* apps[][4] = { {"system\\init", NATIVE_INIT_LIBS_LITERAL, "init", "/ENTRY:_start"}, {"system\\rssrv", NATIVE_SERVICE_LIBS_LITERAL, "rssrv", "/ENTRY:_start"}, + {"system\\posixsrv", NATIVE_SERVICE_LIBS_LITERAL, "posixsrv", "/ENTRY:_start"}, + {"system\\dwm", NATIVE_SERVICE_LIBS_LITERAL, "dwm", "/ENTRY:_start"}, {"tests\\all_tests", NATIVE_EXE_LIBS_LITERAL, "all_tests", NATIVE_EXE_ENTRY_LITERAL}, + {"tests\\posix_service_smoke", NATIVE_EXE_LIBS_LITERAL, "posix_service_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"apps\\shell_win32", WIN32_EXE_LIBS_LITERAL, "shell_win32", NATIVE_EXE_ENTRY_LITERAL}, {"apps\\shell_posix", POSIX_EXE_LIBS_LITERAL, "shell_posix", "/ENTRY:PosixStart"}, {"apps\\shell_native", NATIVE_EXE_LIBS_LITERAL, "shell_native", NATIVE_EXE_ENTRY_LITERAL}, @@ -425,17 +479,24 @@ int main(int argc, char** argv) { {"tests\\named_queue_lifetime_smoke", NATIVE_EXE_LIBS_LITERAL, "named_queue_lifetime_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\input_api_smoke", NATIVE_EXE_LIBS_LITERAL, "input_api_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\input_model_smoke", NATIVE_EXE_LIBS_LITERAL, "input_model_smoke", NATIVE_EXE_ENTRY_LITERAL}, + {"tests\\input_service_smoke", NATIVE_EXE_LIBS_LITERAL, "input_service_smoke", NATIVE_EXE_ENTRY_LITERAL}, + {"tests\\console_service_smoke", NATIVE_EXE_LIBS_LITERAL, "console_service_smoke", NATIVE_EXE_ENTRY_LITERAL}, + {"tests\\desktop_smoke", NATIVE_EXE_LIBS_LITERAL, "desktop_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\timer_smoke", NATIVE_EXE_LIBS_LITERAL, "timer_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\stats_abi_smoke", NATIVE_EXE_LIBS_LITERAL, "stats_abi_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\realpath_smoke", NATIVE_EXE_LIBS_LITERAL, "realpath_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\file_lock_smoke", NATIVE_EXE_LIBS_LITERAL, "file_lock_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\process_protection_smoke", NATIVE_EXE_LIBS_LITERAL, "process_protection_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\process_exit_multithread_smoke", NATIVE_EXE_LIBS_LITERAL, "process_exit_multithread_smoke", NATIVE_EXE_ENTRY_LITERAL}, + {"tests\\thread_name_smoke", NATIVE_EXE_LIBS_LITERAL, "thread_name_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\process_builder_smoke", "bin\\syscall_s.lib obj\\libc.obj " NATIVE_RUNTIME_OBJS_LITERAL, "process_builder_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\socket_addr_smoke", NATIVE_EXE_LIBS_LITERAL, "socket_addr_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\net_irp_smoke", NATIVE_EXE_LIBS_LITERAL, "net_irp_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\module_load_smoke", NATIVE_EXE_LIBS_LITERAL, "module_load_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\posix_module_smoke", POSIX_EXE_LIBS_LITERAL, "posix_module_smoke", "/ENTRY:PosixStart"}, + {"tests\\posix_runtime_smoke", POSIX_EXE_LIBS_LITERAL, "posix_runtime_smoke", "/ENTRY:PosixStart"}, + {"tests\\posix_fd_smoke", POSIX_EXE_LIBS_LITERAL, "posix_fd_smoke", "/ENTRY:PosixStart"}, + {"tests\\posix_pipe_smoke", POSIX_EXE_LIBS_LITERAL, "posix_pipe_smoke", "/ENTRY:PosixStart"}, {"tests\\exception_dispatcher_smoke", NATIVE_EXE_LIBS_LITERAL, "exception_dispatcher_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\native_exception_runtime_smoke", NATIVE_EXE_LIBS_LITERAL, "native_exception_runtime_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\native_raise_exception_smoke", NATIVE_EXE_LIBS_LITERAL, "native_raise_exception_smoke", NATIVE_EXE_ENTRY_LITERAL}, @@ -461,7 +522,8 @@ int main(int argc, char** argv) { {"tests\\pcache_multithread", NATIVE_EXE_LIBS_LITERAL, "pcache_multithread", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\surface_smoke", NATIVE_EXE_LIBS_LITERAL, "surface_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"tests\\surface_invalid_rect", NATIVE_EXE_LIBS_LITERAL, "surface_invalid_rect", NATIVE_EXE_ENTRY_LITERAL}, - {"tests\\surface_multithread_present", NATIVE_EXE_LIBS_LITERAL, "surface_multithread_present", NATIVE_EXE_ENTRY_LITERAL}, + {"tests\\surface_multithread_commit", NATIVE_EXE_LIBS_LITERAL, "surface_multithread_commit", NATIVE_EXE_ENTRY_LITERAL}, + {"tests\\surface_cross_process_smoke", NATIVE_EXE_LIBS_LITERAL, "surface_cross_process_smoke", NATIVE_EXE_ENTRY_LITERAL}, {"apps\\edit", NATIVE_EXE_LIBS_LITERAL, "edit", NATIVE_EXE_ENTRY_LITERAL}, {"apps\\httpget", NATIVE_EXE_LIBS_LITERAL, "httpget", NATIVE_EXE_ENTRY_LITERAL}, {"apps\\tcpserve", NATIVE_EXE_LIBS_LITERAL, "tcpserve", NATIVE_EXE_ENTRY_LITERAL}, @@ -474,9 +536,12 @@ int main(int argc, char** argv) { }; for (size_t i = 0; i < NOB_ARRAY_LEN(apps); i++) { + const char* appCflags = strcmp(apps[i][3], "/ENTRY:PosixStart") == 0 + ? PosixUserCflags + : UserCflags; const char* cmd = nob_temp_sprintf( "cl %s /MP /c /Foobj\\%s\\ code\\user\\%s\\*.c && link %s /SUBSYSTEM:NATIVE " USER_IMAGE_FLAGS_LITERAL " %s /OUT:bin\\%s.exe /PDB:bin\\%s.pdb %s obj\\%s\\*.obj", - UserCflags, apps[i][2], apps[i][0], LFLAGS, apps[i][3], apps[i][2], apps[i][2], apps[i][1], apps[i][2] + appCflags, apps[i][2], apps[i][0], LFLAGS, apps[i][3], apps[i][2], apps[i][2], apps[i][1], apps[i][2] ); nob_da_append(&procs, run_async(cmd)); } @@ -514,20 +579,23 @@ int main(int argc, char** argv) { "bin\\win32.dll", "bin\\posix.dll", "code\\settings.ini", + "code\\services.ini", "bin\\init.exe", "bin\\rssrv.exe", + "bin\\posixsrv.exe", + "bin\\dwm.exe", }; if (!mcopy_files_to_dir(VHD_PART2, "system/", system_files, NOB_ARRAY_LEN(system_files))) return 1; const char* driver_files[] = { "bin\\pci.sys", "bin\\ps2.sys", - "bin\\serial.sys", "bin\\hda.sys", "bin\\ata.sys", "bin\\ahci.sys", "bin\\fat.sys", "bin\\ntfs.sys", + "bin\\ext4.sys", "bin\\vmwaresvga.sys", "bin\\lwip.sys", "bin\\usb.sys", @@ -536,6 +604,8 @@ int main(int argc, char** argv) { "code\\drivers\\drivers.ini", }; if (!mcopy_files_to_dir(VHD_PART2, "drivers/", driver_files, NOB_ARRAY_LEN(driver_files))) return 1; + // The build artifact keeps the descriptive target name; the installed serial driver gets the cereal joke. + if (!mcopy_file("bin\\serial.sys", VHD_PART2, "drivers/cereal.sys")) return 1; const char* app_files[] = { "bin\\shell_win32.exe", @@ -555,6 +625,7 @@ int main(int argc, char** argv) { const char* test_files[] = { "bin\\all_tests.exe", + "bin\\posix_service_smoke.exe", "bin\\async_apc.exe", "bin\\async_cancel.exe", "bin\\apc_return_smoke.exe", @@ -566,17 +637,24 @@ int main(int argc, char** argv) { "bin\\named_queue_lifetime_smoke.exe", "bin\\input_api_smoke.exe", "bin\\input_model_smoke.exe", + "bin\\input_service_smoke.exe", + "bin\\console_service_smoke.exe", + "bin\\desktop_smoke.exe", "bin\\timer_smoke.exe", "bin\\stats_abi_smoke.exe", "bin\\realpath_smoke.exe", "bin\\file_lock_smoke.exe", "bin\\process_protection_smoke.exe", "bin\\process_exit_multithread_smoke.exe", + "bin\\thread_name_smoke.exe", "bin\\process_builder_smoke.exe", "bin\\socket_addr_smoke.exe", "bin\\net_irp_smoke.exe", "bin\\module_load_smoke.exe", "bin\\posix_module_smoke.exe", + "bin\\posix_runtime_smoke.exe", + "bin\\posix_fd_smoke.exe", + "bin\\posix_pipe_smoke.exe", "bin\\exception_dispatcher_smoke.exe", "bin\\native_exception_runtime_smoke.exe", "bin\\native_raise_exception_smoke.exe", @@ -602,7 +680,8 @@ int main(int argc, char** argv) { "bin\\pcache_multithread.exe", "bin\\surface_smoke.exe", "bin\\surface_invalid_rect.exe", - "bin\\surface_multithread_present.exe", + "bin\\surface_multithread_commit.exe", + "bin\\surface_cross_process_smoke.exe", }; if (!mcopy_files_to_dir(VHD_PART2, "tests/", test_files, NOB_ARRAY_LEN(test_files))) return 1; diff --git a/code/common/include/ntlite/internal/session_control.h b/code/common/include/ntlite/internal/session_control.h new file mode 100644 index 0000000..2962d18 --- /dev/null +++ b/code/common/include/ntlite/internal/session_control.h @@ -0,0 +1,48 @@ +// Private RSSRV-to-DWM Session 0 routing protocol. Capability possession, not path publication, grants access. + +#pragma once + +#include +#include +#include + +#define NTL_SESSION_DWM_PROTOCOL_VERSION 1u +#define NTL_SESSION_DWM_FLAG_NONE 0x00000000u + +#define NTL_SESSION_DWM_OP_SET_KEYBOARD_FOCUS 1u +#define NTL_SESSION_DWM_OP_SET_POINTER_FOCUS 2u +#define NTL_SESSION_DWM_OP_SET_POINTER_CAPTURE 3u + +#define NTL_SESSION_DWM_EVENT_OBSERVE_KEY 1u +#define NTL_SESSION_DWM_EVENT_OBSERVE_MOUSE 2u +#define NTL_SESSION_DWM_EVENT_DELIVER_KEY 3u +#define NTL_SESSION_DWM_EVENT_DELIVER_MOUSE 4u + +typedef struct NTL_SESSION_DWM_REQUEST { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + uint32_t Enabled; + uint32_t Reserved[3]; +} NTL_SESSION_DWM_REQUEST; + +typedef struct NTL_SESSION_DWM_REPLY { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + KSTATUS Status; + uint32_t Reserved[3]; +} NTL_SESSION_DWM_REPLY; + +typedef struct NTL_SESSION_DWM_EVENT { + uint32_t Size; + uint32_t Version; + uint32_t Type; + uint32_t Flags; + int32_t PointerX; + int32_t PointerY; + INPUT_KEY_RECORD Key; + INPUT_MOUSE_EVENT Mouse; +} NTL_SESSION_DWM_EVENT; diff --git a/code/docs/services.md b/code/docs/services.md new file mode 100644 index 0000000..173d624 --- /dev/null +++ b/code/docs/services.md @@ -0,0 +1,222 @@ +# Native service architecture + +This document defines the intended user-mode service model for nt-lite. It is an architectural contract, not a description of every detail of the current implementation. Code should move toward this model without introducing temporary service boundaries that are not meant to survive. + +## Process roles + +`init.exe` is the kernel-selected bootstrap user process and user-mode service supervisor. Its process identifier is deliberately not an ABI: it happens to be allocated early today, but code, configuration, diagnostics, and personalities must not assume a particular PID for init. + +`init.exe` is not a UNIX init process: it does not imply a kernel process hierarchy, it does not adopt orphaned processes, and it does not provide reparenting semantics. Holding a process handle for supervision does not make the supervised process a child in a UNIX sense. + +The intended user-mode system plane has four roles: + +- `init.exe` owns service lifecycle and authority delegation. +- `rssrv.exe` is the critical native system server. +- Session-owned infrastructure such as `dwm.exe` is noncritical and owns bulky policy that should not make RSSRV monolithic. +- Subsystem-specific personality servers, such as `posixsrv.exe`, are optional and noncritical to the native system. + +Do not split native input, resource naming, display authority, or console ownership into one service process per subsystem merely to isolate components. nt-lite is a hybrid kernel. RSSRV is intentionally a substantial native server, but it is not the desktop compositor: the desktop window manager and composition engine belong in a separate `dwm.exe` process so window-tree, z-order, damage, decoration, and composition policy can evolve without expanding RSSRV into the whole graphical environment. + +## Kernel and RSSRV ownership + +The kernel owns mechanisms that require kernel authority or hardware access. The bootstrap service manager receives explicit manager capabilities for the native resource plane and delegates attenuated input, display, volume, and namespace authority to RSSRV; this authority must not depend on init's PID. This includes: + +- process and thread execution; +- virtual memory and sections; +- object handles and capabilities; +- ports and other IPC primitives; +- filesystem and storage drivers; +- physical input drivers and raw input-device objects; +- display miniports, low-level display devices, framebuffer or VRAM mappings, and hardware presentation primitives; +- kernel surfaces and other graphics primitives whose implementation belongs below user-mode policy. + +RSSRV owns shared native user-mode policy and system-visible logical resources. Its intended responsibilities include: + +- the global native object namespace; +- namespace directories, bindings, enumeration, and symbolic links; +- publication of mounted volumes and other native resources; +- logical keyboard and mouse ownership above raw device input; +- keyboard state and layout policy where it does not belong in the hardware driver; +- logical pointer state, input focus, capture, and routing; +- native console objects, console buffers, console input, and active-console policy; +- interactive-session state; +- the session's display metadata, modeset policy, and transitional console authority; +- authority and routing used by the session desktop/window manager, without owning the window tree or compositor. + +RSSRV owns the logical object and its policy; it does not replace the kernel driver or hardware object. For example, a keyboard driver remains a kernel component, while RSSRV consumes normalized key events and owns the logical keyboard state exposed to native user mode. + +RSSRV should remain internally modular even though its native resource facilities share one process and failure domain. Separate namespace, volume, input, console, session, and display components are preferred over separate user-mode services. Desktop/window/composition policy is the exception: it has enough independent bulk and failure behavior to live in `dwm.exe`. + +The implemented RSSRV foundation uses one root `RSSRV_STATE` and passes explicit component state to worker threads instead of relying on subsystem globals. The namespace tree lives in `namespace.c`, mounted-volume publication in `volume.c`, resource protocol/listener handling in `resource_service.c`, Session 0 policy in `session.c`, and `rssrv.c` is limited to bootstrap, service control, and component lifetime. RSSRV receives attenuated kernel input-manager and display-manager capabilities in dedicated input/display component state rather than relying on ambient process privilege. + +Logical input is now an RSSRV-owned facility. The kernel input manager exposes a capability-scoped stream of normalized physical key make/break and relative mouse events. RSSRV consumes that stream, owns key-down/modifier/lock state, performs the initial keyboard-layout character mapping, and owns logical pointer position and mouse-button/wheel state. Session 0 owns routing policy above that logical state: one active target, keyboard focus, ordinary pointer focus, and optional pointer capture. Keyboard and pointer dispatch snapshot the selected target before calling it, so target-specific queues do not share or race the raw logical-input queue. Session 0 owns logical pointer state and routing, while DWM mirrors that authoritative position to the visible hardware cursor through its separately delegated display-manager capability. RSSRV publishes the raw logical listener at `/Services/Input`; normal native applications and the current POSIX/Win32 personality bindings may consume that service through the `NtlInput*` client API, while the console receives a separately routed focused-input queue. The old global input syscalls remain a transitional native ABI for existing callers and contract tests, but they are no longer the canonical interactive input path and should be deleted after those callers migrate. + +Typematic is not fully migrated yet. RSSRV accepts repeated physical make events as repeated logical key records, while the legacy kernel-global input path retains its existing kernel repeat timer. A later input/console phase may move repeat timing completely into RSSRV once console routing and terminal policy provide the final destination model. + +## Desktop window manager + +`dwm.exe` is the Session 0 desktop window manager and compositor. It is not a personality server and it is not a replacement for RSSRV. RSSRV remains the authority broker for the interactive session: it owns Session 0, logical input routing, console ownership, and the namespace objects through which the session is named. init owns bootstrap delegation of the display-manager capability and splits its rights between RSSRV and DWM. + +DWM owns graphical policy that would otherwise make RSSRV monolithic: + +- the desktop and window tree; +- z-order and visibility; +- window bounds and decorations; +- dirty-region aggregation and composition scheduling; +- composition of client surfaces into the desktop surface; +- later animation and graphical effects. + +DWM must not receive ambient display privilege. init delegates an attenuated display-manager capability carrying query, physical-present, and cursor rights only; RSSRV receives the disjoint mode-setting and transitional-console authority it still needs. Mode-setting remains outside DWM unless a later design gives it a concrete reason to own that policy. A DWM crash must not take RSSRV down; native namespace, input, console, and session authority remain alive even if the graphical desktop disappears. DWM becomes eligible for `restart=on-failure` only after its session reconstruction protocol is defined, and then it is subject to the same three-restarts-in-any-sliding-five-minute-window rule as every other restartable managed process. + +DWM also owns visible pointer presentation policy independently from RSSRV input routing. The native desktop model must support a free cursor, a cursor confined to a desktop/window rectangle, and a relative-pointer lock suitable for games. Relative lock hides and pins the hardware cursor while preserving RSSRV's relative mouse deltas for the captured input target; it must not synthesize motion by repeatedly warping the logical pointer. Cursor visibility is an explicit DWM policy bit rather than a side effect of focus or capture. RSSRV pointer capture chooses the input recipient, while DWM confinement/relative-lock controls only presentation. The later desktop/window protocol will authorize focused clients to request these transitions without granting them DisplayManager cursor authority. + +## Personality servers + +A personality server exists only for state or policy that is specific to one application personality and must be shared between processes. + +Personality servers are not the owners of generic hardware or native session resources. They consume native facilities exposed by the kernel and RSSRV. + +`posixsrv.exe` may eventually own POSIX-specific shared state such as: + +- POSIX credentials and supplementary groups; +- POSIX process-group and session identifiers; +- cross-process POSIX signal state and routing; +- the synthetic POSIX filesystem metadata overlay; +- POSIX FIFO metadata; +- passwd and group database state. + +POSIX does not cause the kernel to acquire a UNIX process hierarchy. A POSIX launch relationship may be tracked by the POSIX subsystem when required by an exposed API, but there is no reparenting to a distinguished process and no orphan adoption. In particular, POSIX must not infer special semantics from whatever PID the kernel assigned to `init.exe`. + +A Win32 personality server should not be created merely for symmetry. If shared Win32-only state eventually requires a server, `winsrv.exe` is the preferred role name. Native console, input, session, and display ownership remains in RSSRV rather than moving into a Win32 server. + +## Service discovery + +The numeric kernel system-service registry is bootstrap infrastructure, not the general service namespace. + +`SYS_SERVICE_RESOURCE` exists so processes can find RSSRV before a higher-level namespace is available. New ordinary services should not receive additional numeric kernel service identifiers merely for discovery. + +Once RSSRV is available, named services and logical system resources should be published through the RSSRV namespace. A future namespace may contain entries such as: + +```text +/ +├── Device +│ ├── Keyboard0 +│ ├── Mouse0 +│ ├── Display0 +│ └── HarddiskVolume1 +├── Services +│ ├── Input +│ ├── Console +│ └── Posix +├── Sessions +│ └── 0 +│ ├── Console +│ └── Desktop +├── SystemRoot -> /Device/HarddiskVolume1 +└── Console -> /Sessions/0/Console +``` + +The current namespace foundation materializes `/Device`, `/Services`, and `/Sessions` as real RSSRV namespace directories. `/SystemRoot` is a real RSSRV symbolic link to the independently published `/Device/HarddiskVolumeN` object for the bootstrap system volume. `/Device/Display0` is a query-only display-manager child capability: init delegates mode-setting and transitional console authority to RSSRV while physical-present and cursor authority are delegated separately to DWM. `/Services/Input` and `/Services/Console` are bound RSSRV listener capabilities for the raw logical-input and native-console protocols. Session 0 is materialized as `/Sessions/0`, with `/Sessions/0/Desktop` reserved for the userspace desktop/window-manager object and `/Sessions/0/Console` bound to the current console listener. `/Console` is a symbolic link to `/Sessions/0/Console`. Namespace directories are enumerable through the resource protocol; native shell `dir` and tab completion use that enumeration rather than synthesizing names from filesystem paths. + +Names added later are not ABI until code deliberately makes them so. The important rule is that RSSRV is the canonical named namespace after bootstrap. + +## Service configuration and supervision + +`init.exe` owns service lifecycle. The mandatory `system/services.ini` file, packaged next to `settings.ini`, defines stable service policy. The kernel does not parse this file. `init.exe` reads it directly through the bootstrap system-volume capability before RSSRV starts, so locating RSSRV never depends on RSSRV itself. + +The configuration uses case-sensitive `[service.]` sections. Every service section must define all of these keys: + +```ini +[service.rssrv] +type=resource +image=system/rssrv.exe +start=auto +critical=true +restart=never +ready_timeout_ms=5000 +stop_timeout_ms=5000 +requires= +``` + +The parser is deliberately strict. Unknown or duplicate keys, duplicate service identifiers, malformed values, missing required keys, missing dependencies, dependency cycles, and an automatic service depending on a disabled service are fatal configuration errors. The file has no includes, quoting language, environment expansion, continuation lines, or implicit fallback descriptor. A missing or malformed `services.ini` is fatal rather than causing init to fall back to a compiled duplicate of service policy. + +`type` selects an audited service adapter whose bootstrap authority remains compiled into init. Configuration is not a capability language. In particular, the resource adapter owns the restricted security context and startup capabilities required by RSSRV; `services.ini` cannot grant arbitrary privileges or handles. The generic adapter is intentionally narrow: the service receives only its private service-control listener, runs under the shared unprivileged service context, exports exactly one public Port listener in READY, and init publishes that listener at `/Services/` through RSSRV. Automatic generic services must be noncritical, `restart=never`, and explicitly depend on RSSRV. `posixsrv.exe` is the first consumer of this adapter. + +Exactly one resource service must be configured. While resource-server reconstruction is unsupported it must be automatic, critical, dependency-free, and `restart=never`. A future implementation may relax only the restart restriction once recovery semantics exist. + +Managed services use the parsed descriptor and a runtime record. The runtime record owns the current process handle, control connection, and lifecycle state. Service dependencies are resolved to descriptor indices and dependency-first startup order is computed while validating the configuration. + +The private service-control READY protocol is generic. A READY notification reports a status and the number of transferred export handles; the meaning and expected count of those handles come from the compiled service adapter. RSSRV exports its init-only administrative resource listener plus the Session 0 control listener delegated onward to DWM. DWM exports one desktop listener for init to publish at `/Sessions/0/Desktop`. A generic service exports one public listener for `/Services/`. The protocol does not encode service-specific READY flags. + +The configured interactive session process is not a managed service. It is launched and observed separately from the service table. `dwm.exe`, by contrast, is an explicit noncritical `[service.dwm]`: it is launched by init in dependency order after RSSRV, receives no ambient security privilege, and exports no machine-global endpoint. Its initial bootstrap owns the visible hardware cursor; later compositor/window protocol work remains session policy inside that same service process. + +The service manager should remain small. Restart accounting, additional startup capabilities, and recovery policy should be added only where they have a concrete consumer. + +A service is expected to move through a small set of states such as: + +```text +stopped -> starting -> ready -> stopping -> stopped + \-> failed +``` + +A service becomes ready only after its authoritative state and exported capabilities are usable. Process creation alone is not readiness. + +A normal requested stop is not a crash and must not consume restart budget. + +## Automatic restart policy + +Automatic restart is opt-in for each service. A service must not be marked restartable until its authoritative state, exported capabilities, and dependent clients have a defined recovery model. + +When automatic restart is enabled for a failed or crashed service, `init.exe` applies a sliding five-minute restart window: + +1. Remove recorded automatic-restart timestamps that are at least five minutes old. +2. If three automatic restarts remain in the window, suppress another automatic restart and leave the service in the failed state. +3. Otherwise record the new restart attempt and restart the service. + +This permits at most three automatic restart attempts in any sliding five-minute interval. It is not a fixed five-minute bucket and it does not reset on wall-clock boundaries. + +A restart attempt is counted when `init.exe` commits to an automatic restart after a failure, not merely when the failed process is observed. Startup failure of the replacement still consumes that attempt. + +Dependency-driven recovery caused by a service failure counts against the failed service's restart budget. A deliberate stop, shutdown, or explicit administrative start does not count as an automatic restart. + +The default policy is no automatic restart until reconstruction semantics are defined. In particular, RSSRV is critical and must not be blindly restarted as though its namespace and session state survived. If RSSRV is ever made restartable, recovery must explicitly rebuild the native service plane and any user session state that cannot survive the loss of RSSRV. + +The three-restarts-in-five-minutes rule is an invariant of `restart=on-failure`, not a configurable knob. The current shipped service set contains no automatically restartable service, so init does not yet need runtime restart accounting. RSSRV is `restart=never`; an unexpected RSSRV exit is a terminal critical-service failure. The interactive session process is not a service and its separate restart behavior is outside this service restart budget. + +## Failure domains + +RSSRV is a critical native service because it is intended to own authoritative namespace and session state. Loss of RSSRV is therefore a native service-plane failure, not an ordinary personality failure. If a critical service reaches an unrecoverable failed state, init emits a fatal diagnostic and exits; the kernel already treats unexpected init termination as fatal. + +Personality servers are noncritical to the native system. Failure of `posixsrv.exe`, for example, may invalidate POSIX shared state and require affected POSIX processes to fail or be terminated, but it must not make native RSSRV facilities unavailable. + +Clients must not silently treat a newly started service instance as the same stateful server unless the protocol explicitly supports reconstruction. A restarted service instance has a new generation of authoritative state unless a future design specifies otherwise. + +## Design constraints + +When extending this model: + +- keep hardware mechanism in the kernel and shared native user-mode policy in RSSRV; +- do not create a service merely because a subsystem can be isolated behind IPC; +- do not duplicate a native resource owner inside multiple personality servers; +- keep the numeric kernel service registry bootstrap-only; +- keep service authority capability-based rather than identity checks against process names; +- define recovery semantics before enabling automatic restart; +- prefer one canonical path to a service or resource and remove transitional aliases once the real model works. + +## Native console ownership + +RSSRV owns the one logical native console used before multi-session support exists. The console is an RSSRV component, not a separate service process. It is published as `/Services/Console` so native clients and personality libraries use one canonical console protocol. + +The console component currently serializes output, cursor movement, fills, explicit glyph drawing, clear operations, and logical keyboard input. It owns a monotonically increasing content generation and a focused key queue populated by Session 0 routing. The input service remains available for applications that need the raw logical keyboard or pointer stream rather than console semantics. + +The kernel text console remains a transitional rendering backend. RSSRV reaches it only through the delegated display-manager capability and `SYS_DISPLAY_MANAGER_RIGHT_CONSOLE`; RSSRV does not receive `SYS_PRIV_CONSOLE_ACCESS`. The capability-scoped console bridge exists so normal user applications can migrate away from ambient console syscalls before the kernel text-buffer implementation is removed or reduced to early-boot/panic use. + +Normal native and personality console code should use `NtlConsole*`. The old `SysWriteConsole`, `SysGetConsoleInfo`, `SysSetConsoleCursor`, fill, clear, and direct glyph syscalls remain supported temporarily for bootstrapping, diagnostics, and regression tests. Their existence is not a commitment to keep two console APIs. Once DWM session/window composition can replace the kernel's interactive text-console path, remaining ordinary callers should move and the stale ambient syscalls should be deleted. + +Session 0 now exists as explicit RSSRV state. The console registers a stable in-process input target with that session and is the initial active target, keyboard focus, and pointer focus. Pointer capture is represented separately so the later userspace window manager can capture pointer delivery without changing the raw input service or console protocol. The console owns its own focused key queue; `/Services/Input` remains a raw logical observer queue rather than competing with console reads for the same records. + +`/Services/Console` names the process-independent console protocol, `/Sessions/0/Console` names the Session 0 instance, and `/Console` is the convenience symbolic link to the current session console. `/Sessions/0/Desktop` is currently only a namespace directory; the later DWM phase will replace that placeholder with the first real desktop policy object. + +Display authority is split explicitly at bootstrap. RSSRV retains mode-setting and transitional console rights, while init delegates physical-present and hardware-cursor rights to the noncritical DWM service. The published `/Device/Display0` capability remains intentionally query-only. Neither RSSRV nor ordinary applications need ambient `SYS_PRIV_DISPLAY_ACCESS` for the normal path. + +Kernel surfaces are now shareable capability-backed pixel storage rather than process-private presentation objects. A surface may have multiple independent VAD mappings in one or more processes; mapping rights distinguish read-only compositor access from client write access. `SysCommitSurface` records clipped damage, increments a content generation, and signals the waitable surface object without touching physical scanout. Surface wakeups are deliberately coalescing: consumers use the generation to detect skipped commits, and `LastDamageBounds` describes only the most recent commit rather than an acknowledged damage queue. The later DWM window protocol will carry the compositor's authoritative dirty-region submission. `SysPresentDisplayManagerSurface` is the capability-scoped physical-present path intended for DWM. The old ambient `SysPresentSurface` API and its handle right are deleted. Ordinary clients can create, map, share, wait on, and commit surfaces but cannot touch physical scanout. `SysPresentDisplayManagerSurface` is the only surface-to-scanout path and requires an explicitly delegated DisplayManager PRESENT capability. diff --git a/code/drivers/audio/hda/hda_codec.c b/code/drivers/audio/hda/hda_codec.c index ad341de..0f6953a 100644 --- a/code/drivers/audio/hda/hda_codec.c +++ b/code/drivers/audio/hda/hda_codec.c @@ -1672,10 +1672,11 @@ static KSTATUS HdaCodecStartWorkers(HDA_CODEC_STATE* state) { return status; state->WorkRunning = 1; - KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + const KE_THREAD_CREATE_PARAMETERS workParameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "hda.codec.work"); status = KeCreateKernelThread((uintptr_t)HdaCodecWorkThread, (uintptr_t)state, - ¶meters, + &workParameters, &state->WorkThread); if (!KSUCCESS(status)) { state->WorkRunning = 0; @@ -1685,9 +1686,11 @@ static KSTATUS HdaCodecStartWorkers(HDA_CODEC_STATE* state) { return NTLSTATUS_SUCCESS; state->PollRunning = 1; + const KE_THREAD_CREATE_PARAMETERS pollParameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "hda.codec.poll"); status = KeCreateKernelThread((uintptr_t)HdaCodecPollThread, (uintptr_t)state, - ¶meters, + &pollParameters, &state->PollThread); if (!KSUCCESS(status)) { state->PollRunning = 0; diff --git a/code/drivers/audio/hda/hda_transport.c b/code/drivers/audio/hda/hda_transport.c index d1af379..d0b6e98 100644 --- a/code/drivers/audio/hda/hda_transport.c +++ b/code/drivers/audio/hda/hda_transport.c @@ -1395,7 +1395,8 @@ KSTATUS HdaStartRecoveryWorker(HDA_CONTROLLER* controller) { if (!controller || !controller->RecoverySemaphore || !controller->RecoveryFinished) return NTLSTATUS_INVALID_PARAMETER; controller->RecoveryWorkerRunning = true; - KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NORMAL(PriorityHigh); + KE_THREAD_CREATE_PARAMETERS parameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityHigh, "hda.recovery"); KSTATUS status = KeCreateKernelThread((uintptr_t)HdaRecoveryWorker, (uintptr_t)controller, ¶meters, diff --git a/code/drivers/bus/usb/usb_hub.c b/code/drivers/bus/usb/usb_hub.c index f4d14dc..669a96a 100644 --- a/code/drivers/bus/usb/usb_hub.c +++ b/code/drivers/bus/usb/usb_hub.c @@ -646,7 +646,8 @@ static KSTATUS UsbHubStartWorker(USB_HUB_EXTENSION* hub) { } hub->WorkerRunning = true; - const KE_THREAD_CREATE_PARAMETERS workerParameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + const KE_THREAD_CREATE_PARAMETERS workerParameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "usb.hub"); status = KeCreateKernelThread((uintptr_t)UsbHubWorker, (uintptr_t)hub, &workerParameters, diff --git a/code/drivers/drivers.ini b/code/drivers/drivers.ini index 6d147b5..4439655 100644 --- a/code/drivers/drivers.ini +++ b/code/drivers/drivers.ini @@ -2,12 +2,13 @@ pci.sys hda.sys ps2.sys -serial.sys +cereal.sys ahci.sys ata.sys usb.sys fat.sys ntfs.sys +ext4.sys [runtime] lwip.sys diff --git a/code/drivers/fs/ext4/ext4_driver.c b/code/drivers/fs/ext4/ext4_driver.c new file mode 100644 index 0000000..7ca4255 --- /dev/null +++ b/code/drivers/fs/ext4/ext4_driver.c @@ -0,0 +1,1179 @@ +/* + * NTLite ext2/ext3/ext4 read/write filesystem driver backed by lwext4. + * + * Keep NTLite policy and I/O glue here; third_party/lwext4 stays upstream. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#define NTL_EXT4_SECTOR_SIZE 512u +#define NTL_EXT4_ZERO_CHUNK 4096u + +#ifndef INT64_MAX +#define INT64_MAX 9223372036854775807LL +#endif + +typedef struct NTL_EXT4_VOLUME { + uint8_t DiskIndex; + uint64_t PartitionLba; + uint64_t PartitionSectors; + + struct ext4_blockdev BlockDev; + struct ext4_blockdev_iface BlockIface; + + char DeviceName[24]; + char MountName[24]; + + IO_VOLUME* Volume; + LIST_ENTRY MountLink; + bool Mounted; + bool JournalStarted; + bool Dead; +} NTL_EXT4_VOLUME; + +typedef struct NTL_EXT4_FILE { + NTL_EXT4_VOLUME* Volume; + ext4_file File; + char* Path; +} NTL_EXT4_FILE; + +typedef struct NTL_EXT4_DIR { + NTL_EXT4_VOLUME* Volume; + ext4_dir Dir; + char* Path; +} NTL_EXT4_DIR; + +static LIST_ENTRY gExt4Mounts; +static uint32_t gExt4MountedCount; +static uint32_t gExt4Serial; + +static MUTEX gExt4MountMutex; +static MUTEX gExt4FsMutex; +static SPINLOCK gExt4MutexInitLock = SPINLOCK_INIT; +static int gExt4MutexesReady; + +/* lwext4 also defines LIST_INIT for its BSD queue heads. Avoid that macro + * here because NTLite LIST_ENTRY uses the Fwd/Bwd intrusive-list layout. */ +static void Ext4ListInit(LIST_ENTRY* head) { + head->Fwd = head; + head->Bwd = head; +} + +static void Ext4InitStaticMutex(MUTEX* mutex) { + memset(mutex, 0, sizeof(*mutex)); + ObInitializeObjectHeader(&mutex->Header, ObjectTypeMutex); + mutex->Lock = (SPINLOCK)SPINLOCK_INIT; + mutex->Owner = (THREAD*)0; + Ext4ListInit(&mutex->WaitList); + Ext4ListInit(&mutex->OwnerLink); + mutex->DonationClass = KeSchedulingNormal; + mutex->DonationPriority = PriorityLow; + mutex->HasDonation = 0; + mutex->Abandoned = 0; +} + +static void Ext4EnsureMutexes(void) { + if (gExt4MutexesReady) + return; + + uintptr_t flags = KeAcquireSpinlock(&gExt4MutexInitLock); + if (!gExt4MutexesReady) { + Ext4InitStaticMutex(&gExt4MountMutex); + Ext4InitStaticMutex(&gExt4FsMutex); + gExt4MutexesReady = 1; + } + KeReleaseSpinlock(&gExt4MutexInitLock, flags); +} + +static KSTATUS Ext4AcquireFs(void) { + Ext4EnsureMutexes(); + if (!KeCanWait()) + return NTLSTATUS_INVALID_IRQL; + KSTATUS status = KeAcquireMutex(&gExt4FsMutex, KE_WAIT_INFINITE); + return KE_MUTEX_ACQUIRE_SUCCEEDED(status) ? NTLSTATUS_SUCCESS : status; +} + +static void Ext4ReleaseFs(void) { + KeReleaseMutex(&gExt4FsMutex); +} + +static KSTATUS Ext4MapError(int error) { + switch (error) { + case EOK: return NTLSTATUS_SUCCESS; + case ENOENT: + case ENXIO: + case ENODEV: return NTLSTATUS_NOT_FOUND; + case EEXIST: return NTLSTATUS_ALREADY_EXISTS; + case ENOMEM: return NTLSTATUS_NO_MEMORY; + case ENOSPC: + case EFBIG: + case EMLINK: return NTLSTATUS_LIMIT_REACHED; + case EACCES: + case EPERM: + case EROFS: return NTLSTATUS_ACCESS_DENIED; + case ENOTSUP: return NTLSTATUS_NOT_SUPPORTED; + case EIO: return NTLSTATUS_DEVICE_ERROR; + case EINVAL: + case EFAULT: + case E2BIG: + case EISDIR: + case ENOTDIR: + case ENOTEMPTY: return NTLSTATUS_INVALID_PARAMETER; + default: return NTLSTATUS_FAILURE; + } +} + +static NTL_EXT4_VOLUME* Ext4Volume(IO_VOLUME_PROVIDER* provider) { + return provider ? (NTL_EXT4_VOLUME*)provider->Context : (NTL_EXT4_VOLUME*)0; +} + +static bool Ext4PathValid(SYS_STRING_VIEW path, bool allowRoot) { + if (!SysSvIsValid(path)) + return false; + if (!allowRoot && path.Length == 0) + return false; + return true; +} + +/* Converts the VFS-relative path into lwext4's private mount namespace. */ +static char* Ext4BuildPath(NTL_EXT4_VOLUME* volume, SYS_STRING_VIEW relative) { + if (!volume || !SysSvIsValid(relative)) + return (char*)0; + + size_t prefix = strlen(volume->MountName); + if (relative.Length > SIZE_MAX - prefix - 1) + return (char*)0; + + size_t bytes = prefix + relative.Length + 1; + char* path = (char*)MmAllocatePool(bytes); + if (!path) + return (char*)0; + + memcpy(path, volume->MountName, prefix); + if (relative.Length) + memcpy(path + prefix, relative.Data, relative.Length); + path[prefix + relative.Length] = '\0'; + return path; +} + +static uint32_t Ext4NowSeconds(void) { + uint64_t now = KeQueryUnixTimeSeconds(); + return now > UINT32_MAX ? UINT32_MAX : (uint32_t)now; +} + +#define EXT4_TOUCH_ATIME 0x1u +#define EXT4_TOUCH_MTIME 0x2u +#define EXT4_TOUCH_CTIME 0x4u + +static void Ext4TouchLocked(const char* path, uint32_t mask) { + if (!path) return; + uint32_t now = Ext4NowSeconds(); + if (mask & EXT4_TOUCH_ATIME) (void)ext4_atime_set(path, now); + if (mask & EXT4_TOUCH_MTIME) (void)ext4_mtime_set(path, now); + if (mask & EXT4_TOUCH_CTIME) (void)ext4_ctime_set(path, now); +} + +static void Ext4SetEpochTime(SYS_SYSTEM_TIME* out, uint32_t seconds) { + if (!out) + return; + memset(out, 0, sizeof(*out)); + out->Flags = SYS_TIME_FLAG_VALID; + out->TimeZoneMinutes = SYS_TIME_UNSPECIFIED_TZ; + out->EpochSeconds = seconds; + + uint64_t days = (uint64_t)seconds / 86400ull; + uint32_t rem = seconds % 86400u; + out->Hour = (uint8_t)(rem / 3600u); + out->Minute = (uint8_t)((rem % 3600u) / 60u); + out->Second = (uint8_t)(rem % 60u); + out->DayOfWeek = (uint8_t)((days + 4u) % 7u); + + int64_t z = (int64_t)days + 719468ll; + int64_t era = (z >= 0 ? z : z - 146096ll) / 146097ll; + uint32_t doe = (uint32_t)(z - era * 146097ll); + uint32_t yoe = (doe - doe / 1460u + doe / 36524u - doe / 146096u) / 365u; + int64_t year = (int64_t)yoe + era * 400ll; + uint32_t doy = doe - (365u * yoe + yoe / 4u - yoe / 100u); + uint32_t mp = (5u * doy + 2u) / 153u; + uint32_t day = doy - (153u * mp + 2u) / 5u + 1u; + uint32_t month = mp + (mp < 10u ? 3u : (uint32_t)-9); + year += (month <= 2u); + out->Year = (uint16_t)year; + out->Month = (uint8_t)month; + out->Day = (uint8_t)day; +} + +static bool Ext4IsDotEntry(const ext4_direntry* entry) { + if (!entry) + return false; + if (entry->name_length == 1 && entry->name[0] == '.') + return true; + return entry->name_length == 2 && entry->name[0] == '.' && entry->name[1] == '.'; +} + +static char* Ext4BuildDirEntryPath(const NTL_EXT4_DIR* dir, const ext4_direntry* entry) { + if (!dir || !dir->Path || !entry) + return (char*)0; + + size_t parentLength = strlen(dir->Path); + size_t nameLength = entry->name_length; + bool needSlash = parentLength != 0 && dir->Path[parentLength - 1] != '/'; + size_t separator = needSlash ? 1u : 0u; + + if (nameLength > SIZE_MAX - parentLength - separator - 1u) + return (char*)0; + + size_t bytes = parentLength + separator + nameLength + 1u; + char* path = (char*)MmAllocatePool(bytes); + if (!path) + return (char*)0; + + memcpy(path, dir->Path, parentLength); + size_t offset = parentLength; + if (needSlash) + path[offset++] = '/'; + if (nameLength) + memcpy(path + offset, entry->name, nameLength); + path[offset + nameLength] = '\0'; + return path; +} + +/* lwext4 v0.0.3 only shrinks in ext4_ftruncate and refuses seeks beyond EOF. */ +static int Ext4GrowFileLocked(ext4_file* file, uint64_t target) { + static const uint8_t zeros[NTL_EXT4_ZERO_CHUNK] = {0}; + uint64_t current = ext4_fsize(file); + if (target <= current) + return EOK; + if (current > (uint64_t)INT64_MAX || target > (uint64_t)INT64_MAX) + return EFBIG; + + int r = ext4_fseek(file, (int64_t)current, SEEK_SET); + if (r != EOK) + return r; + + while (current < target) { + uint64_t left = target - current; + size_t chunk = left < sizeof(zeros) ? (size_t)left : sizeof(zeros); + size_t written = 0; + r = ext4_fwrite(file, zeros, chunk, &written); + if (r != EOK) + return r; + if (written != chunk) + return EIO; + current += written; + } + return EOK; +} + +/* ---- lwext4 physical block device ---- */ + +static NTL_EXT4_VOLUME* Ext4BlockVolume(struct ext4_blockdev* bdev) { + if (!bdev || !bdev->bdif) + return (NTL_EXT4_VOLUME*)0; + return (NTL_EXT4_VOLUME*)bdev->bdif->p_user; +} + +static int Ext4BlockOpen(struct ext4_blockdev* bdev) { + return Ext4BlockVolume(bdev) ? EOK : ENODEV; +} + +static int Ext4BlockRead(struct ext4_blockdev* bdev, void* buffer, + uint64_t blockId, uint32_t blockCount) { + NTL_EXT4_VOLUME* volume = Ext4BlockVolume(bdev); + if (!volume || (!buffer && blockCount)) + return EINVAL; + if (blockId > volume->PartitionSectors || + (uint64_t)blockCount > volume->PartitionSectors - blockId) + return EIO; + if (blockCount == 0) + return EOK; + + KSTATUS status = IoReadDiskSectors(volume->DiskIndex, + volume->PartitionLba + blockId, + blockCount, + buffer); + return KSUCCESS(status) ? EOK : EIO; +} + +static int Ext4BlockWrite(struct ext4_blockdev* bdev, const void* buffer, + uint64_t blockId, uint32_t blockCount) { + NTL_EXT4_VOLUME* volume = Ext4BlockVolume(bdev); + if (!volume || (!buffer && blockCount)) + return EINVAL; + if (blockId > volume->PartitionSectors || + (uint64_t)blockCount > volume->PartitionSectors - blockId) + return EIO; + if (blockCount == 0) + return EOK; + + KSTATUS status = IoWriteDiskSectors(volume->DiskIndex, + volume->PartitionLba + blockId, + blockCount, + buffer); + return KSUCCESS(status) ? EOK : EIO; +} + +static int Ext4BlockClose(struct ext4_blockdev* bdev) { + NTL_EXT4_VOLUME* volume = Ext4BlockVolume(bdev); + if (!volume) + return ENODEV; + return KSUCCESS(IoFlushDisk(volume->DiskIndex)) ? EOK : EIO; +} + +static void Ext4InitBlockDevice(NTL_EXT4_VOLUME* volume) { + memset(&volume->BlockIface, 0, sizeof(volume->BlockIface)); + memset(&volume->BlockDev, 0, sizeof(volume->BlockDev)); + + volume->BlockIface.open = Ext4BlockOpen; + volume->BlockIface.bread = Ext4BlockRead; + volume->BlockIface.bwrite = Ext4BlockWrite; + volume->BlockIface.close = Ext4BlockClose; + volume->BlockIface.lock = (int (*)(struct ext4_blockdev*))0; + volume->BlockIface.unlock = (int (*)(struct ext4_blockdev*))0; + volume->BlockIface.ph_bsize = NTL_EXT4_SECTOR_SIZE; + volume->BlockIface.ph_bcnt = volume->PartitionSectors; + volume->BlockIface.p_user = volume; + + volume->BlockDev.bdif = &volume->BlockIface; + volume->BlockDev.part_offset = 0; + volume->BlockDev.part_size = volume->PartitionSectors * NTL_EXT4_SECTOR_SIZE; +} + +/* ---- IO_VOLUME_PROVIDER operations ---- */ + +static KSTATUS Ext4Open(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path, void** fileCtx) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume || !fileCtx || !Ext4PathValid(path, false)) + return NTLSTATUS_INVALID_PARAMETER; + + char* fullPath = Ext4BuildPath(volume, path); + if (!fullPath) + return NTLSTATUS_NO_MEMORY; + + NTL_EXT4_FILE* file = (NTL_EXT4_FILE*)MmAllocatePool(sizeof(*file)); + if (!file) { + MmFreePool(fullPath); + return NTLSTATUS_NO_MEMORY; + } + memset(file, 0, sizeof(*file)); + file->Volume = volume; + + KSTATUS status = Ext4AcquireFs(); + if (KSUCCESS(status)) { + int r = ext4_fopen2(&file->File, fullPath, O_RDWR); + status = Ext4MapError(r); + Ext4ReleaseFs(); + } + + if (!KSUCCESS(status)) { + MmFreePool(fullPath); + MmFreePool(file); + return status; + } + + file->Path = fullPath; + *fileCtx = file; + return NTLSTATUS_SUCCESS; +} + +static KSTATUS Ext4Read(IO_VOLUME_PROVIDER* provider, void* fileCtx, uint64_t offset, + void* buffer, size_t size, size_t* bytesRead) { + (void)provider; + NTL_EXT4_FILE* file = (NTL_EXT4_FILE*)fileCtx; + if (!file || (!buffer && size) || !bytesRead) + return NTLSTATUS_INVALID_PARAMETER; + *bytesRead = 0; + if (offset > (uint64_t)INT64_MAX) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = Ext4AcquireFs(); + if (!KSUCCESS(status)) + return status; + + uint64_t fileSize = ext4_fsize(&file->File); + if (offset >= fileSize || size == 0) { + Ext4ReleaseFs(); + return NTLSTATUS_SUCCESS; + } + + uint64_t available = fileSize - offset; + size_t toRead = available < (uint64_t)size ? (size_t)available : size; + int r = ext4_fseek(&file->File, (int64_t)offset, SEEK_SET); + if (r == EOK) + r = ext4_fread(&file->File, buffer, toRead, bytesRead); + if (r == EOK && *bytesRead) + Ext4TouchLocked(file->Path, EXT4_TOUCH_ATIME); + status = Ext4MapError(r); + Ext4ReleaseFs(); + return status; +} + +static KSTATUS Ext4Write(IO_VOLUME_PROVIDER* provider, void* fileCtx, uint64_t offset, + const void* buffer, size_t size, size_t* bytesWritten) { + (void)provider; + NTL_EXT4_FILE* file = (NTL_EXT4_FILE*)fileCtx; + if (!file || (!buffer && size) || !bytesWritten) + return NTLSTATUS_INVALID_PARAMETER; + *bytesWritten = 0; + if (offset > (uint64_t)INT64_MAX || (uint64_t)size > (uint64_t)INT64_MAX - offset) + return NTLSTATUS_LIMIT_REACHED; + if (size == 0) + return NTLSTATUS_SUCCESS; + + KSTATUS status = Ext4AcquireFs(); + if (!KSUCCESS(status)) + return status; + + int r = EOK; + uint64_t fileSize = ext4_fsize(&file->File); + if (offset > fileSize) + r = Ext4GrowFileLocked(&file->File, offset); + if (r == EOK) + r = ext4_fseek(&file->File, (int64_t)offset, SEEK_SET); + if (r == EOK) + r = ext4_fwrite(&file->File, buffer, size, bytesWritten); + if (r == EOK && *bytesWritten) + Ext4TouchLocked(file->Path, EXT4_TOUCH_MTIME | EXT4_TOUCH_CTIME); + + status = Ext4MapError(r); + Ext4ReleaseFs(); + return status; +} + +static KSTATUS Ext4Create(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path, void** fileCtx) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume || !fileCtx || !Ext4PathValid(path, false)) + return NTLSTATUS_INVALID_PARAMETER; + + char* fullPath = Ext4BuildPath(volume, path); + if (!fullPath) + return NTLSTATUS_NO_MEMORY; + + NTL_EXT4_FILE* file = (NTL_EXT4_FILE*)MmAllocatePool(sizeof(*file)); + if (!file) { + MmFreePool(fullPath); + return NTLSTATUS_NO_MEMORY; + } + memset(file, 0, sizeof(*file)); + file->Volume = volume; + + KSTATUS status = Ext4AcquireFs(); + if (KSUCCESS(status)) { + int r = ext4_fopen2(&file->File, fullPath, O_RDWR | O_CREAT | O_EXCL); + if (r == EOK) + Ext4TouchLocked(fullPath, EXT4_TOUCH_ATIME | EXT4_TOUCH_MTIME | EXT4_TOUCH_CTIME); + status = Ext4MapError(r); + Ext4ReleaseFs(); + } + + if (!KSUCCESS(status)) { + MmFreePool(fullPath); + MmFreePool(file); + return status; + } + file->Path = fullPath; + *fileCtx = file; + return NTLSTATUS_SUCCESS; +} + +static KSTATUS Ext4Delete(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume || !Ext4PathValid(path, false)) + return NTLSTATUS_INVALID_PARAMETER; + char* fullPath = Ext4BuildPath(volume, path); + if (!fullPath) + return NTLSTATUS_NO_MEMORY; + + KSTATUS status = Ext4AcquireFs(); + if (KSUCCESS(status)) { + status = Ext4MapError(ext4_fremove(fullPath)); + Ext4ReleaseFs(); + } + MmFreePool(fullPath); + return status; +} + +static KSTATUS Ext4Rename(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW oldPath, + SYS_STRING_VIEW newPath) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume || !Ext4PathValid(oldPath, false) || !Ext4PathValid(newPath, false)) + return NTLSTATUS_INVALID_PARAMETER; + + char* oldFull = Ext4BuildPath(volume, oldPath); + char* newFull = Ext4BuildPath(volume, newPath); + if (!oldFull || !newFull) { + if (oldFull) MmFreePool(oldFull); + if (newFull) MmFreePool(newFull); + return NTLSTATUS_NO_MEMORY; + } + + KSTATUS status = Ext4AcquireFs(); + if (KSUCCESS(status)) { + int r; + if (ext4_inode_exist(oldFull, EXT4_DE_DIR) == EOK) + r = ext4_dir_mv(oldFull, newFull); + else + r = ext4_frename(oldFull, newFull); + status = Ext4MapError(r); + Ext4ReleaseFs(); + } + + MmFreePool(oldFull); + MmFreePool(newFull); + return status; +} + +static KSTATUS Ext4CreateDir(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume || !Ext4PathValid(path, false)) + return NTLSTATUS_INVALID_PARAMETER; + char* fullPath = Ext4BuildPath(volume, path); + if (!fullPath) + return NTLSTATUS_NO_MEMORY; + + KSTATUS status = Ext4AcquireFs(); + if (KSUCCESS(status)) { + int r = ext4_dir_mk(fullPath); + if (r == EOK) + Ext4TouchLocked(fullPath, EXT4_TOUCH_ATIME | EXT4_TOUCH_MTIME | EXT4_TOUCH_CTIME); + status = Ext4MapError(r); + Ext4ReleaseFs(); + } + MmFreePool(fullPath); + return status; +} + +static KSTATUS Ext4RemoveDir(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume || !Ext4PathValid(path, false)) + return NTLSTATUS_INVALID_PARAMETER; + char* fullPath = Ext4BuildPath(volume, path); + if (!fullPath) + return NTLSTATUS_NO_MEMORY; + + KSTATUS status = Ext4AcquireFs(); + if (!KSUCCESS(status)) { + MmFreePool(fullPath); + return status; + } + + ext4_dir dir; + memset(&dir, 0, sizeof(dir)); + int r = ext4_dir_open(&dir, fullPath); + bool empty = true; + if (r == EOK) { + const ext4_direntry* entry; + while ((entry = ext4_dir_entry_next(&dir)) != (const ext4_direntry*)0) { + if (entry->inode != 0 && !Ext4IsDotEntry(entry)) { + empty = false; + break; + } + } + int closeResult = ext4_dir_close(&dir); + if (r == EOK) + r = closeResult; + } + if (r == EOK && !empty) + r = ENOTEMPTY; + if (r == EOK) + r = ext4_dir_rm(fullPath); + + status = Ext4MapError(r); + Ext4ReleaseFs(); + MmFreePool(fullPath); + return status; +} + +static KSTATUS Ext4Truncate(IO_VOLUME_PROVIDER* provider, void* fileCtx, + uint64_t newSize, uint32_t fileFlags) { + (void)provider; + (void)fileFlags; + NTL_EXT4_FILE* file = (NTL_EXT4_FILE*)fileCtx; + if (!file) + return NTLSTATUS_INVALID_PARAMETER; + if (newSize > (uint64_t)INT64_MAX) + return NTLSTATUS_LIMIT_REACHED; + + KSTATUS status = Ext4AcquireFs(); + if (!KSUCCESS(status)) + return status; + + uint64_t oldSize = ext4_fsize(&file->File); + int r = newSize <= oldSize + ? ext4_ftruncate(&file->File, newSize) + : Ext4GrowFileLocked(&file->File, newSize); + if (r == EOK && newSize != oldSize) + Ext4TouchLocked(file->Path, EXT4_TOUCH_MTIME | EXT4_TOUCH_CTIME); + status = Ext4MapError(r); + Ext4ReleaseFs(); + return status; +} + +static uint64_t Ext4GetSize(IO_VOLUME_PROVIDER* provider, void* fileCtx) { + (void)provider; + NTL_EXT4_FILE* file = (NTL_EXT4_FILE*)fileCtx; + if (!file) + return 0; + + if (!KSUCCESS(Ext4AcquireFs())) + return file->File.fsize; + uint64_t size = ext4_fsize(&file->File); + Ext4ReleaseFs(); + return size; +} + +static KSTATUS Ext4QueryInfo(IO_VOLUME_PROVIDER* provider, void* fileCtx, SYS_FILE_STAT* out) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + NTL_EXT4_FILE* file = (NTL_EXT4_FILE*)fileCtx; + if (!volume || !file || !out) + return NTLSTATUS_INVALID_PARAMETER; + + memset(out, 0, sizeof(*out)); + out->Size = sizeof(*out); + out->Type = SYS_FILE_TYPE_REGULAR; + out->Attributes = SYS_FILE_ATTR_ARCHIVE; + out->LinkCount = 1; + out->FileSize = Ext4GetSize(provider, fileCtx); + out->FileId = file->File.inode; + if (volume->Volume) + out->VolumeId = IoGetVolumeInstanceId(volume->Volume); + + KSTATUS status = Ext4AcquireFs(); + if (KSUCCESS(status)) { + uint32_t atime = 0, mtime = 0, ctime = 0; + if (file->Path && ext4_atime_get(file->Path, &atime) == EOK) + Ext4SetEpochTime(&out->LastAccessTime, atime); + if (file->Path && ext4_mtime_get(file->Path, &mtime) == EOK) + Ext4SetEpochTime(&out->LastWriteTime, mtime); + if (file->Path && ext4_ctime_get(file->Path, &ctime) == EOK) { + Ext4SetEpochTime(&out->ChangeTime, ctime); + Ext4SetEpochTime(&out->CreationTime, ctime); + } + Ext4ReleaseFs(); + } + return NTLSTATUS_SUCCESS; +} + +static KSTATUS Ext4Stat(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path, SYS_FILE_STAT* out) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume || !out || !Ext4PathValid(path, true)) + return NTLSTATUS_INVALID_PARAMETER; + char* fullPath = Ext4BuildPath(volume, path); + if (!fullPath) + return NTLSTATUS_NO_MEMORY; + + KSTATUS status = Ext4AcquireFs(); + if (!KSUCCESS(status)) { + MmFreePool(fullPath); + return status; + } + + memset(out, 0, sizeof(*out)); + out->Size = sizeof(*out); + out->LinkCount = 1; + if (volume->Volume) + out->VolumeId = IoGetVolumeInstanceId(volume->Volume); + + bool isDir = ext4_inode_exist(fullPath, EXT4_DE_DIR) == EOK; + int r = EOK; + if (isDir) { + ext4_dir dir; + memset(&dir, 0, sizeof(dir)); + r = ext4_dir_open(&dir, fullPath); + if (r == EOK) { + out->Type = SYS_FILE_TYPE_DIRECTORY; + out->Attributes = SYS_FILE_ATTR_DIRECTORY; + out->FileId = dir.f.inode; + r = ext4_dir_close(&dir); + } + } else { + ext4_file file; + memset(&file, 0, sizeof(file)); + r = ext4_fopen2(&file, fullPath, O_RDONLY); + if (r == EOK) { + out->Type = SYS_FILE_TYPE_REGULAR; + out->Attributes = SYS_FILE_ATTR_ARCHIVE; + out->FileId = file.inode; + out->FileSize = ext4_fsize(&file); + r = ext4_fclose(&file); + } + } + + if (r == EOK) { + uint32_t atime = 0, mtime = 0, ctime = 0; + if (ext4_atime_get(fullPath, &atime) == EOK) + Ext4SetEpochTime(&out->LastAccessTime, atime); + if (ext4_mtime_get(fullPath, &mtime) == EOK) + Ext4SetEpochTime(&out->LastWriteTime, mtime); + if (ext4_ctime_get(fullPath, &ctime) == EOK) { + Ext4SetEpochTime(&out->ChangeTime, ctime); + Ext4SetEpochTime(&out->CreationTime, ctime); + } + } + + status = Ext4MapError(r); + Ext4ReleaseFs(); + MmFreePool(fullPath); + return status; +} + +static KSTATUS Ext4QueryIdentity(IO_VOLUME_PROVIDER* provider, void* fileCtx, + IO_FILE_IDENTITY* out) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + NTL_EXT4_FILE* file = (NTL_EXT4_FILE*)fileCtx; + if (!volume || !file || !out) + return NTLSTATUS_INVALID_PARAMETER; + + memset(out, 0, sizeof(*out)); + if (volume->Volume) + out->VolumeId = IoGetVolumeInstanceId(volume->Volume); + out->FileId = file->File.inode; + out->Size = Ext4GetSize(provider, fileCtx); + return NTLSTATUS_SUCCESS; +} + +static KSTATUS Ext4Flush(IO_VOLUME_PROVIDER* provider, void* fileCtx) { + (void)fileCtx; + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = Ext4AcquireFs(); + if (!KSUCCESS(status)) + return status; + int r = ext4_cache_flush(volume->MountName); + Ext4ReleaseFs(); + if (r != EOK) + return Ext4MapError(r); + return IoFlushDisk(volume->DiskIndex); +} + +static void Ext4Close(IO_VOLUME_PROVIDER* provider, void* fileCtx) { + (void)provider; + NTL_EXT4_FILE* file = (NTL_EXT4_FILE*)fileCtx; + if (!file) + return; + if (KSUCCESS(Ext4AcquireFs())) { + (void)ext4_fclose(&file->File); + Ext4ReleaseFs(); + } + if (file->Path) MmFreePool(file->Path); + MmFreePool(file); +} + +static void Ext4QueryStats(IO_VOLUME_PROVIDER* provider, IO_FILESYSTEM_STATS* out) { + (void)provider; + if (out) + memset(out, 0, sizeof(*out)); +} + +static KSTATUS Ext4OpenDir(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path, void** dirCtx) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume || !dirCtx || !Ext4PathValid(path, true)) + return NTLSTATUS_INVALID_PARAMETER; + char* fullPath = Ext4BuildPath(volume, path); + if (!fullPath) + return NTLSTATUS_NO_MEMORY; + + NTL_EXT4_DIR* dir = (NTL_EXT4_DIR*)MmAllocatePool(sizeof(*dir)); + if (!dir) { + MmFreePool(fullPath); + return NTLSTATUS_NO_MEMORY; + } + memset(dir, 0, sizeof(*dir)); + dir->Volume = volume; + + KSTATUS status = Ext4AcquireFs(); + if (KSUCCESS(status)) { + status = Ext4MapError(ext4_dir_open(&dir->Dir, fullPath)); + Ext4ReleaseFs(); + } + + if (!KSUCCESS(status)) { + MmFreePool(fullPath); + MmFreePool(dir); + return status; + } + dir->Path = fullPath; + *dirCtx = dir; + return NTLSTATUS_SUCCESS; +} + +static KSTATUS Ext4ReadDir(IO_VOLUME_PROVIDER* provider, void* dirCtx, KDIR_ENTRY* out) { + (void)provider; + NTL_EXT4_DIR* dir = (NTL_EXT4_DIR*)dirCtx; + if (!dir || !out) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = Ext4AcquireFs(); + if (!KSUCCESS(status)) + return status; + + const ext4_direntry* entry; + do { + entry = ext4_dir_entry_next(&dir->Dir); + } while (entry && (entry->inode == 0 || Ext4IsDotEntry(entry))); + + if (!entry) { + Ext4ReleaseFs(); + return NTLSTATUS_NO_MORE_FILES; + } + + memset(out, 0, sizeof(*out)); + size_t nameLength = entry->name_length; + if (nameLength >= SYS_DIR_NAME_MAX) + nameLength = SYS_DIR_NAME_MAX - 1; + memcpy(out->Name, entry->name, nameLength); + out->Name[nameLength] = '\0'; + out->Attr = entry->inode_type == EXT4_DE_DIR + ? SYS_FILE_ATTR_DIRECTORY + : SYS_FILE_ATTR_ARCHIVE; + + /* SYS_DIR_ENTRY carries the size directly. NTFS/FAT populate it during + * enumeration, so do the same here instead of making `dir` report 0. */ + char* childPath = Ext4BuildDirEntryPath(dir, entry); + if (childPath) { + if (entry->inode_type != EXT4_DE_DIR) { + ext4_file file; + memset(&file, 0, sizeof(file)); + if (ext4_fopen2(&file, childPath, O_RDONLY) == EOK) { + uint64_t fileSize = ext4_fsize(&file); + out->FileSize = fileSize > UINT32_MAX ? UINT32_MAX : (uint32_t)fileSize; + (void)ext4_fclose(&file); + } + } + + uint32_t timestamp = 0; + if (ext4_ctime_get(childPath, ×tamp) == EOK) + Ext4SetEpochTime(&out->CreateTime, timestamp); + if (ext4_atime_get(childPath, ×tamp) == EOK) + Ext4SetEpochTime(&out->LastAccessTime, timestamp); + if (ext4_mtime_get(childPath, ×tamp) == EOK) + Ext4SetEpochTime(&out->LastWriteTime, timestamp); + MmFreePool(childPath); + } + + Ext4ReleaseFs(); + return NTLSTATUS_SUCCESS; +} + +static void Ext4CloseDir(IO_VOLUME_PROVIDER* provider, void* dirCtx) { + (void)provider; + NTL_EXT4_DIR* dir = (NTL_EXT4_DIR*)dirCtx; + if (!dir) + return; + if (KSUCCESS(Ext4AcquireFs())) { + (void)ext4_dir_close(&dir->Dir); + Ext4ReleaseFs(); + } + if (dir->Path) + MmFreePool(dir->Path); + MmFreePool(dir); +} + +static void Ext4VolumeDead(IO_VOLUME_PROVIDER* provider, IO_VOLUME_REMOVAL_REASON reason) { + (void)reason; + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (volume) + volume->Dead = true; +} + +static void Ext4DestroyProvider(IO_VOLUME_PROVIDER* provider) { + NTL_EXT4_VOLUME* volume = Ext4Volume(provider); + if (!volume) + return; + + bool canFree = true; + if (KSUCCESS(Ext4AcquireFs())) { + if (volume->Mounted) { + if (volume->JournalStarted) + (void)ext4_journal_stop(volume->MountName); + (void)ext4_cache_flush(volume->MountName); + int r = ext4_umount(volume->MountName); + if (r == EOK) { + volume->Mounted = false; + /* SharpExt4 v0.0.3 unregister clears the slot but returns ENOENT. */ + (void)ext4_device_unregister(volume->DeviceName); + } else { + /* Static lwext4 tables may still reference this blockdev. Leak instead of UAF. */ + canFree = false; + DbgPrint("EXT4: unmount failed for %s rc=%d; retaining mount context\n", + volume->MountName, r); + } + } + Ext4ReleaseFs(); + } else { + canFree = false; + } + + if (canFree) { + memset(volume, 0, sizeof(*volume)); + MmFreePool(volume); + } +} + +static void Ext4InitProvider(IO_VOLUME_PROVIDER* provider, NTL_EXT4_VOLUME* volume) { + memset(provider, 0, sizeof(*provider)); + provider->Open = Ext4Open; + provider->Read = Ext4Read; + provider->Write = Ext4Write; + provider->Create = Ext4Create; + provider->Delete = Ext4Delete; + provider->Rename = Ext4Rename; + provider->CreateDir = Ext4CreateDir; + provider->RemoveDir = Ext4RemoveDir; + provider->Truncate = Ext4Truncate; + provider->GetSize = Ext4GetSize; + provider->QueryInfo = Ext4QueryInfo; + provider->Stat = Ext4Stat; + provider->QueryIdentity = Ext4QueryIdentity; + provider->Flush = Ext4Flush; + provider->Close = Ext4Close; + provider->QueryStats = Ext4QueryStats; + provider->OpenDir = Ext4OpenDir; + provider->ReadDir = Ext4ReadDir; + provider->CloseDir = Ext4CloseDir; + provider->VolumeDead = Ext4VolumeDead; + provider->Destroy = Ext4DestroyProvider; + provider->Context = volume; +} + +/* ---- partition discovery / mount lifecycle ---- */ + +static NTL_EXT4_VOLUME* Ext4FindMountLocked(const IO_PARTITION_INFO* partition) { + if (!partition) + return (NTL_EXT4_VOLUME*)0; + for (LIST_ENTRY* entry = gExt4Mounts.Fwd; entry != &gExt4Mounts; entry = entry->Fwd) { + NTL_EXT4_VOLUME* volume = CONTAINING_RECORD(entry, NTL_EXT4_VOLUME, MountLink); + if (volume->DiskIndex == partition->DiskIndex && + volume->PartitionLba == partition->StartLba) + return volume; + } + return (NTL_EXT4_VOLUME*)0; +} + +static bool Ext4ProbePartition(const IO_PARTITION_INFO* partition) { + if (!partition || partition->SectorCount < 3) + return false; + uint8_t sector[NTL_EXT4_SECTOR_SIZE]; + KSTATUS status = IoReadDiskSectors(partition->DiskIndex, + partition->StartLba + 2, + 1, + sector); + if (!KSUCCESS(status)) + return false; + /* ext superblock starts at byte 1024; s_magic is +56. */ + return sector[56] == 0x53 && sector[57] == 0xEF; +} + +static void Ext4Hex8(char* out, uint32_t value) { + static const char digits[] = "0123456789abcdef"; + for (int i = 7; i >= 0; --i) { + out[i] = digits[value & 0xFu]; + value >>= 4; + } +} + +static void Ext4AssignNames(NTL_EXT4_VOLUME* volume) { + uint32_t serial = ++gExt4Serial; + memcpy(volume->DeviceName, "ntlext4", 7); + Ext4Hex8(volume->DeviceName + 7, serial); + volume->DeviceName[15] = '\0'; + + volume->MountName[0] = '/'; + volume->MountName[1] = 'e'; + Ext4Hex8(volume->MountName + 2, serial); + volume->MountName[10] = '/'; + volume->MountName[11] = '\0'; +} + +static bool Ext4TryMountPartition(const IO_PARTITION_INFO* partition) { + if (!partition || partition->SectorCount < 3 || + partition->SectorCount > UINT64_MAX / NTL_EXT4_SECTOR_SIZE || + !IoDiskDevicePresent(partition->DiskIndex)) + return false; + if (!Ext4ProbePartition(partition)) + return false; + + Ext4EnsureMutexes(); + if (!KE_MUTEX_ACQUIRE_SUCCEEDED(KeAcquireMutex(&gExt4MountMutex, KE_WAIT_INFINITE))) + return false; + + if (Ext4FindMountLocked(partition)) { + KeReleaseMutex(&gExt4MountMutex); + return false; + } + + NTL_EXT4_VOLUME* volume = (NTL_EXT4_VOLUME*)MmAllocatePool(sizeof(*volume)); + if (!volume) { + KeReleaseMutex(&gExt4MountMutex); + return false; + } + memset(volume, 0, sizeof(*volume)); + Ext4ListInit(&volume->MountLink); + volume->DiskIndex = partition->DiskIndex; + volume->PartitionLba = partition->StartLba; + volume->PartitionSectors = partition->SectorCount; + Ext4AssignNames(volume); + Ext4InitBlockDevice(volume); + + KSTATUS status = Ext4AcquireFs(); + int r = EIO; + if (KSUCCESS(status)) { + r = ext4_device_register(&volume->BlockDev, volume->DeviceName); + if (r == EOK) { + r = ext4_mount(volume->DeviceName, volume->MountName, false); + if (r == EOK) { + volume->Mounted = true; + int recover = ext4_recover(volume->MountName); + if (recover != EOK && recover != ENOTSUP) + DbgPrint("EXT4: journal recovery %s rc=%d (continuing)\n", + volume->MountName, recover); + int journal = ext4_journal_start(volume->MountName); + if (journal == EOK) + volume->JournalStarted = true; + else + DbgPrint("EXT4: journal start %s rc=%d (continuing)\n", + volume->MountName, journal); + } else { + (void)ext4_device_unregister(volume->DeviceName); + } + } + Ext4ReleaseFs(); + } + + if (r != EOK) { + DbgPrint("EXT4: disk=%u lba=%llu lwext4 mount failed rc=%d\n", + partition->DiskIndex, (unsigned long long)partition->StartLba, r); + MmFreePool(volume); + KeReleaseMutex(&gExt4MountMutex); + return false; + } + + IO_VOLUME_PROVIDER provider; + Ext4InitProvider(&provider, volume); + IO_VOLUME_SOURCE source; + source.Type = IoVolumeSourcePartition; + source.DiskIndex = volume->DiskIndex; + memset(source.Reserved, 0, sizeof(source.Reserved)); + source.StartLba = volume->PartitionLba; + source.SectorCount = volume->PartitionSectors; + + IO_VOLUME* ioVolume = (IO_VOLUME*)0; + status = IoCreateVolume(&provider, &source, &ioVolume); + if (!KSUCCESS(status)) { + if (ioVolume) { + (void)IoRemoveVolume(ioVolume, IoVolumeUnmounted); + IoReleaseVolume(ioVolume); + } else { + /* No IO_VOLUME owns the provider yet, so clean it here. */ + Ext4DestroyProvider(&provider); + } + DbgPrint("EXT4: disk=%u lba=%llu IoCreateVolume failed status=0x%x\n", + partition->DiskIndex, (unsigned long long)partition->StartLba, status); + KeReleaseMutex(&gExt4MountMutex); + return false; + } + + volume->Volume = ioVolume; + ListInsertTail(&gExt4Mounts, &volume->MountLink); + gExt4MountedCount++; + uint32_t instance = IoGetVolumeInstanceId(ioVolume); + uint32_t count = gExt4MountedCount; + + DbgPrint("EXT4: disk=%u lba=%llu mounted volume=%u at %s; total=%u\n", + volume->DiskIndex, (unsigned long long)volume->PartitionLba, + instance, volume->MountName, count); + KeReleaseMutex(&gExt4MountMutex); + return true; +} + +static void Ext4UnmountPartition(const IO_PARTITION_INFO* partition, IO_PARTITION_CHANGE change) { + if (!partition) + return; + + Ext4EnsureMutexes(); + if (!KE_MUTEX_ACQUIRE_SUCCEEDED(KeAcquireMutex(&gExt4MountMutex, KE_WAIT_INFINITE))) + return; + + NTL_EXT4_VOLUME* volume = Ext4FindMountLocked(partition); + if (!volume) { + KeReleaseMutex(&gExt4MountMutex); + return; + } + + IO_VOLUME* ioVolume = volume->Volume; + uint32_t instance = IoGetVolumeInstanceId(ioVolume); + ListRemove(&volume->MountLink); + volume->Volume = (IO_VOLUME*)0; + if (gExt4MountedCount) + gExt4MountedCount--; + uint32_t count = gExt4MountedCount; + KeReleaseMutex(&gExt4MountMutex); + + (void)IoRemoveVolume(ioVolume, IoVolumeBackingRemoved); + IoReleaseVolume(ioVolume); + DbgPrint("EXT4: disk=%u lba=%llu %s; unmounted volume=%u; total=%u\n", + partition->DiskIndex, (unsigned long long)partition->StartLba, + change == IoPartitionSurpriseRemoved ? "surprise removed" : "removed", + instance, count); +} + +static void Ext4PartitionChange(const IO_PARTITION_INFO* partition, + IO_PARTITION_CHANGE change, + void* context) { + (void)context; + switch (change) { + case IoPartitionArrived: + (void)Ext4TryMountPartition(partition); + break; + case IoPartitionSurpriseRemoved: + case IoPartitionRemoved: + Ext4UnmountPartition(partition, change); + break; + default: + break; + } +} + +KSTATUS DriverEntry(const NTL_DDK* ddk, NTL_DRIVER_INIT* init) { + DbgPrint("EXT4: Init\n"); + if (!ddk) + return NTLSTATUS_INVALID_PARAMETER; + if (init) + init->Name = "ext4"; + + Ext4ListInit(&gExt4Mounts); + gExt4MountedCount = 0; + gExt4Serial = 0; + Ext4EnsureMutexes(); + + KSTATUS status = IoRegisterPartitionChangeCallback(Ext4PartitionChange, (void*)0); + if (!KSUCCESS(status)) { + DbgPrint("EXT4: partition callback registration failed status=0x%x\n", status); + return status; + } + + if (!gExt4MountedCount) + DbgPrint("EXT4: no mountable ext partitions found; driver resident for partition changes.\n"); + DbgPrint("EXT4: Init complete; mounted %u volume(s).\n", gExt4MountedCount); + return NTLSTATUS_SUCCESS; +} diff --git a/code/drivers/fs/ext4/ext4_port.c b/code/drivers/fs/ext4/ext4_port.c new file mode 100644 index 0000000..a252661 --- /dev/null +++ b/code/drivers/fs/ext4/ext4_port.c @@ -0,0 +1,74 @@ +/* lwext4 freestanding allocation shim for NTLite drivers. */ + +#include +#include +#include +#include + +#include "ext4_port.h" + +void* ext4_ntl_malloc(size_t size) { + if (size == 0) + size = 1; + return MmAllocatePool(size); +} + +void* ext4_ntl_calloc(size_t count, size_t size) { + if (count != 0 && size > SIZE_MAX / count) + return (void*)0; + + size_t bytes = count * size; + if (bytes == 0) + bytes = 1; + + void* ptr = MmAllocatePool(bytes); + if (ptr) + memset(ptr, 0, bytes); + return ptr; +} + +void* ext4_ntl_realloc(void* ptr, size_t size) { + if (!ptr) + return ext4_ntl_malloc(size); + if (size == 0) { + MmFreePool(ptr); + return (void*)0; + } + return MmReallocatePool(ptr, size); +} + +void ext4_ntl_free(void* ptr) { + if (ptr) + MmFreePool(ptr); +} + +/* lwext4 uses the standard allocation names when CONFIG_USE_USER_MALLOC=0. */ +void* malloc(size_t size) { return ext4_ntl_malloc(size); } +void* calloc(size_t count, size_t size) { return ext4_ntl_calloc(count, size); } +void* realloc(void* ptr, size_t size) { return ext4_ntl_realloc(ptr, size); } +void free(void* ptr) { ext4_ntl_free(ptr); } +/* lwext4 uses qsort for directory-index construction. A small insertion + * sort is sufficient here and keeps the freestanding port dependency-free. */ +void qsort(void* base, size_t count, size_t size, + int (*compare)(const void*, const void*)) { + if (!base || !compare || size == 0 || count < 2) + return; + + unsigned char* bytes = (unsigned char*)base; + for (size_t i = 1; i < count; ++i) { + size_t j = i; + while (j != 0) { + unsigned char* left = bytes + (j - 1) * size; + unsigned char* right = bytes + j * size; + if (compare(left, right) <= 0) + break; + + for (size_t k = 0; k < size; ++k) { + unsigned char temp = left[k]; + left[k] = right[k]; + right[k] = temp; + } + --j; + } + } +} diff --git a/code/drivers/fs/ext4/ext4_port.h b/code/drivers/fs/ext4/ext4_port.h new file mode 100644 index 0000000..ffe1902 --- /dev/null +++ b/code/drivers/fs/ext4/ext4_port.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +void* ext4_ntl_malloc(size_t size); +void* ext4_ntl_calloc(size_t count, size_t size); +void* ext4_ntl_realloc(void* ptr, size_t size); +void ext4_ntl_free(void* ptr); diff --git a/code/drivers/fs/ext4/include/stdlib.h b/code/drivers/fs/ext4/include/stdlib.h new file mode 100644 index 0000000..0ce769e --- /dev/null +++ b/code/drivers/fs/ext4/include/stdlib.h @@ -0,0 +1,19 @@ +#pragma once + +/* Minimal freestanding stdlib surface used by lwext4. */ +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void* malloc(size_t size); +void* calloc(size_t count, size_t size); +void* realloc(void* ptr, size_t size); +void free(void* ptr); +void qsort(void* base, size_t count, size_t size, + int (*compare)(const void*, const void*)); + +#ifdef __cplusplus +} +#endif diff --git a/code/drivers/fs/ext4/lwext4_sources.txt b/code/drivers/fs/ext4/lwext4_sources.txt new file mode 100644 index 0000000..971d65b --- /dev/null +++ b/code/drivers/fs/ext4/lwext4_sources.txt @@ -0,0 +1,24 @@ +# NTLite glue and lwext4 sources used by ext4.sys. +# Run vendor_lwext4.ps1 once before building. +drivers/fs/ext4/ext4_port.c +drivers/fs/ext4/ext4_driver.c + +drivers/fs/ext4/third_party/lwext4/src/ext4.c +drivers/fs/ext4/third_party/lwext4/src/ext4_balloc.c +drivers/fs/ext4/third_party/lwext4/src/ext4_bcache.c +drivers/fs/ext4/third_party/lwext4/src/ext4_bitmap.c +drivers/fs/ext4/third_party/lwext4/src/ext4_block_group.c +drivers/fs/ext4/third_party/lwext4/src/ext4_blockdev.c +drivers/fs/ext4/third_party/lwext4/src/ext4_crc32.c +drivers/fs/ext4/third_party/lwext4/src/ext4_debug.c +drivers/fs/ext4/third_party/lwext4/src/ext4_dir.c +drivers/fs/ext4/third_party/lwext4/src/ext4_dir_idx.c +drivers/fs/ext4/third_party/lwext4/src/ext4_extent.c +drivers/fs/ext4/third_party/lwext4/src/ext4_fs.c +drivers/fs/ext4/third_party/lwext4/src/ext4_hash.c +drivers/fs/ext4/third_party/lwext4/src/ext4_ialloc.c +drivers/fs/ext4/third_party/lwext4/src/ext4_inode.c +drivers/fs/ext4/third_party/lwext4/src/ext4_journal.c +drivers/fs/ext4/third_party/lwext4/src/ext4_super.c +drivers/fs/ext4/third_party/lwext4/src/ext4_trans.c +drivers/fs/ext4/third_party/lwext4/src/ext4_xattr.c diff --git a/code/drivers/fs/ext4/third_party/lwext4/blockdev/blockdev.c b/code/drivers/fs/ext4/third_party/lwext4/blockdev/blockdev.c new file mode 100644 index 0000000..a44a2e1 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/blockdev/blockdev.c @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +#include +#include +#include + + +/**********************BLOCKDEV INTERFACE**************************************/ +static int blockdev_open(struct ext4_blockdev *bdev); +static int blockdev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id, + uint32_t blk_cnt); +static int blockdev_bwrite(struct ext4_blockdev *bdev, const void *buf, + uint64_t blk_id, uint32_t blk_cnt); +static int blockdev_close(struct ext4_blockdev *bdev); +static int blockdev_lock(struct ext4_blockdev *bdev); +static int blockdev_unlock(struct ext4_blockdev *bdev); + +/******************************************************************************/ +EXT4_BLOCKDEV_STATIC_INSTANCE(blockdev, 512, 0, blockdev_open, + blockdev_bread, blockdev_bwrite, blockdev_close, + blockdev_lock, blockdev_unlock); + +/******************************************************************************/ +static int blockdev_open(struct ext4_blockdev *bdev) +{ + /*blockdev_open: skeleton*/ + return EIO; +} + +/******************************************************************************/ + +static int blockdev_bread(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id, + uint32_t blk_cnt) +{ + /*blockdev_bread: skeleton*/ + return EIO; +} + + +/******************************************************************************/ +static int blockdev_bwrite(struct ext4_blockdev *bdev, const void *buf, + uint64_t blk_id, uint32_t blk_cnt) +{ + /*blockdev_bwrite: skeleton*/ + return EIO; +} +/******************************************************************************/ +static int blockdev_close(struct ext4_blockdev *bdev) +{ + /*blockdev_close: skeleton*/ + return EIO; +} + +static int blockdev_lock(struct ext4_blockdev *bdev) +{ + /*blockdev_lock: skeleton*/ + return EIO; +} + +static int blockdev_unlock(struct ext4_blockdev *bdev) +{ + /*blockdev_unlock: skeleton*/ + return EIO; +} + +/******************************************************************************/ +struct ext4_blockdev *ext4_blockdev_get(void) +{ + return &blockdev; +} +/******************************************************************************/ + diff --git a/code/drivers/fs/ext4/third_party/lwext4/blockdev/blockdev.h b/code/drivers/fs/ext4/third_party/lwext4/blockdev/blockdev.h new file mode 100644 index 0000000..17f947a --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/blockdev/blockdev.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef BLOCKDEV_H_ +#define BLOCKDEV_H_ + +#include +#include + +#include +#include + +/**@brief File blockdev get.*/ +struct ext4_blockdev *ext4_blockdev_get(void); + +#endif /* BLOCKDEV_H_ */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/framework.h b/code/drivers/fs/ext4/third_party/lwext4/framework.h new file mode 100644 index 0000000..54b83e9 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/framework.h @@ -0,0 +1,5 @@ +#pragma once + +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +// Windows Header Files +#include diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4.h new file mode 100644 index 0000000..e8db7aa --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4.h @@ -0,0 +1,628 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4.h + * @brief Ext4 high level operations (files, directories, mount points...). + * Client has to include only this file. + */ + +#ifndef EXT4_H_ +#define EXT4_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_errno.h" +#include "ext4_oflags.h" +#include "ext4_debug.h" + +#include "ext4_blockdev.h" + +/********************************OS LOCK INFERFACE***************************/ + +/**@brief OS dependent lock interface.*/ +struct ext4_lock { + + /**@brief Lock access to mount point.*/ + void (*lock)(void); + + /**@brief Unlock access to mount point.*/ + void (*unlock)(void); +}; + +/********************************FILE DESCRIPTOR*****************************/ + +/**@brief File descriptor. */ +typedef struct ext4_file { + + /**@brief Mount point handle.*/ + struct ext4_mountpoint *mp; + + /**@brief File inode id.*/ + uint32_t inode; + + /**@brief Open flags.*/ + uint32_t flags; + + /**@brief File size.*/ + uint64_t fsize; + + /**@brief Actual file position.*/ + uint64_t fpos; +} ext4_file; + +/*****************************DIRECTORY DESCRIPTOR***************************/ + +/**@brief Directory entry descriptor. */ +typedef struct ext4_direntry { + uint32_t inode; + uint16_t entry_length; + uint8_t name_length; + uint8_t inode_type; + uint8_t name[255]; +} ext4_direntry; + +/**@brief Directory descriptor. */ +typedef struct ext4_dir { + /**@brief File descriptor.*/ + ext4_file f; + /**@brief Current directory entry.*/ + ext4_direntry de; + /**@brief Next entry offset.*/ + uint64_t next_off; +} ext4_dir; + +/********************************MOUNT OPERATIONS****************************/ + +/**@brief Register block device. + * + * @param bd Block device. + * @param dev_name Block device name. + * + * @return Standard error code.*/ +int ext4_device_register(struct ext4_blockdev *bd, + const char *dev_name); + +/**@brief Un-register block device. + * + * @param dev_name Block device name. + * + * @return Standard error code.*/ +int ext4_device_unregister(const char *dev_name); + +/**@brief Un-register all block devices. + * + * @return Standard error code.*/ +int ext4_device_unregister_all(void); + +/**@brief Mount a block device with EXT4 partition to the mount point. + * + * @param dev_name Block device name (@ref ext4_device_register). + * @param mount_point Mount point, for example: + * - / + * - /my_partition/ + * - /my_second_partition/ + * @param read_only mount as read-only mode. + * + * @return Standard error code */ +int ext4_mount(const char *dev_name, + const char *mount_point, + bool read_only); + +/**@brief Umount operation. + * + * @param mount_pount Mount point. + * + * @return Standard error code */ +int ext4_umount(const char *mount_point); + +/**@brief Starts journaling. Journaling start/stop functions are transparent + * and might be used on filesystems without journaling support. + * @warning Usage: + * ext4_mount("sda1", "/"); + * ext4_journal_start("/"); + * + * //File operations here... + * + * ext4_journal_stop("/"); + * ext4_umount("/"); + * @param mount_pount Mount point. + * + * @return Standard error code. */ +int ext4_journal_start(const char *mount_point); + +/**@brief Stops journaling. Journaling start/stop functions are transparent + * and might be used on filesystems without journaling support. + * + * @param mount_pount Mount point name. + * + * @return Standard error code. */ +int ext4_journal_stop(const char *mount_point); + +/**@brief Journal recovery. + * @warning Must be called after @ref ext4_mount. + * + * @param mount_pount Mount point. + * + * @return Standard error code. */ +int ext4_recover(const char *mount_point); + +/**@brief Some of the filesystem stats. */ +struct ext4_mount_stats { + uint32_t inodes_count; + uint32_t free_inodes_count; + uint64_t blocks_count; + uint64_t free_blocks_count; + + uint32_t block_size; + uint32_t block_group_count; + uint32_t blocks_per_group; + uint32_t inodes_per_group; + + char volume_name[16]; +}; + +/**@brief Get file mount point stats. + * + * @param mount_pount Mount point. + * @param stats Filesystem stats. + * + * @return Standard error code. */ +int ext4_mount_point_stats(const char *mount_point, + struct ext4_mount_stats *stats); + +/**@brief Setup OS lock routines. + * + * @param mount_pount Mount point. + * @param locks Lock and unlock functions + * + * @return Standard error code. */ +int ext4_mount_setup_locks(const char *mount_point, + const struct ext4_lock *locks); + +/**@brief Acquire the filesystem superblock pointer of a mp. + * + * @param mount_pount Mount point. + * @param sb Superblock handle + * + * @return Standard error code. */ +int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb); + +/**@brief Enable/disable write back cache mode. + * @warning Default model of cache is write trough. It means that when You do: + * + * ext4_fopen(...); + * ext4_fwrite(...); + * < --- data is flushed to physical drive + * + * When you do: + * ext4_cache_write_back(..., 1); + * ext4_fopen(...); + * ext4_fwrite(...); + * < --- data is NOT flushed to physical drive + * ext4_cache_write_back(..., 0); + * < --- when write back mode is disabled all + * cache data will be flushed + * To enable write back mode permanently just call this function + * once after ext4_mount (and disable before ext4_umount). + * + * Some of the function use write back cache mode internally. + * If you enable write back mode twice you have to disable it twice + * to flush all data: + * + * ext4_cache_write_back(..., 1); + * ext4_cache_write_back(..., 1); + * + * ext4_cache_write_back(..., 0); + * ext4_cache_write_back(..., 0); + * + * Write back mode is useful when you want to create a lot of empty + * files/directories. + * + * @param mount_pount Mount point. + * @param on Enable/disable cache writeback mode. + * + * @return Standard error code. */ +int ext4_cache_write_back(const char *path, bool on); + + +/**@brief Force cache flush. + * + * @param mount_pount Mount point. + * + * @return Standard error code. */ +int ext4_cache_flush(const char *path); + +/********************************FILE OPERATIONS*****************************/ + +/**@brief Remove file by path. + * + * @param path Path to file. + * + * @return Standard error code. */ +int ext4_fremove(const char *path); + +/**@brief Create a hardlink for a file. + * + * @param path Path to file. + * @param hardlink_path Path of hardlink. + * + * @return Standard error code. */ +int ext4_flink(const char *path, const char *hardlink_path); + +/**@brief Rename file. + * @param path Source. + * @param new_path Destination. + * @return Standard error code. */ +int ext4_frename(const char *path, const char *new_path); + +/**@brief File open function. + * + * @param file File handle. + * @param path File path, has to start from mount point:/my_partition/file. + * @param flags File open flags. + * |---------------------------------------------------------------| + * | r or rb O_RDONLY | + * |---------------------------------------------------------------| + * | w or wb O_WRONLY|O_CREAT|O_TRUNC | + * |---------------------------------------------------------------| + * | a or ab O_WRONLY|O_CREAT|O_APPEND | + * |---------------------------------------------------------------| + * | r+ or rb+ or r+b O_RDWR | + * |---------------------------------------------------------------| + * | w+ or wb+ or w+b O_RDWR|O_CREAT|O_TRUNC | + * |---------------------------------------------------------------| + * | a+ or ab+ or a+b O_RDWR|O_CREAT|O_APPEND | + * |---------------------------------------------------------------| + * + * @return Standard error code.*/ +int ext4_fopen(ext4_file *file, const char *path, const char *flags); + +/**@brief Alternate file open function. + * + * @param file File handle. + * @param path File path, has to start from mount point:/my_partition/file. + * @param flags File open flags. + * + * @return Standard error code.*/ +int ext4_fopen2(ext4_file *file, const char *path, int flags); + +/**@brief File close function. + * + * @param file File handle. + * + * @return Standard error code.*/ +int ext4_fclose(ext4_file *file); + + +/**@brief File truncate function. + * + * @param file File handle. + * @param size New file size. + * + * @return Standard error code.*/ +int ext4_ftruncate(ext4_file *file, uint64_t size); + +/**@brief Read data from file. + * + * @param file File handle. + * @param buf Output buffer. + * @param size Bytes to read. + * @param rcnt Bytes read (NULL allowed). + * + * @return Standard error code.*/ +int ext4_fread(ext4_file *file, void *buf, size_t size, size_t *rcnt); + +/**@brief Write data to file. + * + * @param file File handle. + * @param buf Data to write + * @param size Write length.. + * @param wcnt Bytes written (NULL allowed). + * + * @return Standard error code.*/ +int ext4_fwrite(ext4_file *file, const void *buf, size_t size, size_t *wcnt); + +/**@brief File seek operation. + * + * @param file File handle. + * @param offset Offset to seek. + * @param origin Seek type: + * @ref SEEK_SET + * @ref SEEK_CUR + * @ref SEEK_END + * + * @return Standard error code.*/ +int ext4_fseek(ext4_file *file, int64_t offset, uint32_t origin); + +/**@brief Get file position. + * + * @param file File handle. + * + * @return Actual file position */ +uint64_t ext4_ftell(ext4_file *file); + +/**@brief Get file size. + * + * @param file File handle. + * + * @return File size. */ +uint64_t ext4_fsize(ext4_file *file); + + +/**@brief Get inode of file/directory/link. + * + * @param path Parh to file/dir/link. + * @param ret_ino Inode number. + * @param inode Inode internals. + * + * @return Standard error code.*/ +int ext4_raw_inode_fill(const char *path, uint32_t *ret_ino, + struct ext4_inode *inode); + +/**@brief Check if inode exists. + * + * @param path Parh to file/dir/link. + * @param type Inode type. + * @ref EXT4_DIRENTRY_UNKNOWN + * @ref EXT4_DE_REG_FILE + * @ref EXT4_DE_DIR + * @ref EXT4_DE_CHRDEV + * @ref EXT4_DE_BLKDEV + * @ref EXT4_DE_FIFO + * @ref EXT4_DE_SOCK + * @ref EXT4_DE_SYMLINK + * + * @return Standard error code.*/ +int ext4_inode_exist(const char *path, int type); + +/**@brief Change file/directory/link mode bits. + * + * @param path Path to file/dir/link. + * @param mode New mode bits (for example 0777). + * + * @return Standard error code.*/ +int ext4_mode_set(const char *path, uint32_t mode); + + +/**@brief Get file/directory/link mode bits. + * + * @param path Path to file/dir/link. + * @param mode New mode bits (for example 0777). + * + * @return Standard error code.*/ +int ext4_mode_get(const char *path, uint32_t *mode); + +/**@brief Change file owner and group. + * + * @param path Path to file/dir/link. + * @param uid User id. + * @param gid Group id. + * + * @return Standard error code.*/ +int ext4_owner_set(const char *path, uint32_t uid, uint32_t gid); + +/**@brief Get file/directory/link owner and group. + * + * @param path Path to file/dir/link. + * @param uid User id. + * @param gid Group id. + * + * @return Standard error code.*/ +int ext4_owner_get(const char *path, uint32_t *uid, uint32_t *gid); + +/**@brief Set file/directory/link access time. + * + * @param path Path to file/dir/link. + * @param atime Access timestamp. + * + * @return Standard error code.*/ +int ext4_atime_set(const char *path, uint32_t atime); + +/**@brief Set file/directory/link modify time. + * + * @param path Path to file/dir/link. + * @param mtime Modify timestamp. + * + * @return Standard error code.*/ +int ext4_mtime_set(const char *path, uint32_t mtime); + +/**@brief Set file/directory/link change time. + * + * @param path Path to file/dir/link. + * @param ctime Change timestamp. + * + * @return Standard error code.*/ +int ext4_ctime_set(const char *path, uint32_t ctime); + +/**@brief Get file/directory/link access time. + * + * @param path Path to file/dir/link. + * @param atime Access timestamp. + * + * @return Standard error code.*/ +int ext4_atime_get(const char *path, uint32_t *atime); + +/**@brief Get file/directory/link modify time. + * + * @param path Path to file/dir/link. + * @param mtime Modify timestamp. + * + * @return Standard error code.*/ +int ext4_mtime_get(const char *path, uint32_t *mtime); + +/**@brief Get file/directory/link change time. + * + * @param path Pathto file/dir/link. + * @param ctime Change timestamp. + * + * @return standard error code*/ +int ext4_ctime_get(const char *path, uint32_t *ctime); + +/**@brief Create symbolic link. + * + * @param target Destination entry path. + * @param path Source entry path. + * + * @return Standard error code.*/ +int ext4_fsymlink(const char *target, const char *path); + +/**@brief Create special file. + * @param path Path to new special file. + * @param filetype Filetype of the new special file. + * (that must not be regular file, directory, or unknown type) + * @param dev If filetype is char device or block device, + * the device number will become the payload in the inode. + * @return Standard error code.*/ +int ext4_mknod(const char *path, int filetype, uint32_t dev); + +/**@brief Read symbolic link payload. + * + * @param path Path to symlink. + * @param buf Output buffer. + * @param bufsize Output buffer max size. + * @param rcnt Bytes read. + * + * @return Standard error code.*/ +int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt); + +/**@brief Set extended attribute. + * + * @param path Path to file/directory + * @param name Name of the entry to add. + * @param name_len Length of @name in bytes. + * @param data Data of the entry to add. + * @param data_size Size of data to add. + * + * @return Standard error code.*/ +int ext4_setxattr(const char *path, const char *name, size_t name_len, + const void *data, size_t data_size); + +/**@brief Get extended attribute. + * + * @param path Path to file/directory. + * @param name Name of the entry to get. + * @param name_len Length of @name in bytes. + * @param data Data of the entry to get. + * @param data_size Size of data to get. + * + * @return Standard error code.*/ +int ext4_getxattr(const char *path, const char *name, size_t name_len, + void *buf, size_t buf_size, size_t *data_size); + +/**@brief List extended attributes. + * + * @param path Path to file/directory. + * @param list List to hold the name of entries. + * @param size Size of @list in bytes. + * @param ret_size Used bytes of @list. + * + * @return Standard error code.*/ +int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size); + +/**@brief Remove extended attribute. + * + * @param path Path to file/directory. + * @param name Name of the entry to remove. + * @param name_len Length of @name in bytes. + * + * @return Standard error code.*/ +int ext4_removexattr(const char *path, const char *name, size_t name_len); + + +/*********************************DIRECTORY OPERATION***********************/ + +/**@brief Recursive directory remove. + * + * @param path Directory path to remove + * + * @return Standard error code.*/ +int ext4_dir_rm(const char *path); + +/**@brief Rename/move directory. + * + * @param path Source path. + * @param new_path Destination path. + * + * @return Standard error code. */ +int ext4_dir_mv(const char *path, const char *new_path); + +/**@brief Create new directory. + * + * @param path Directory name. + * + * @return Standard error code.*/ +int ext4_dir_mk(const char *path); + +/**@brief Directory open. + * + * @param dir Directory handle. + * @param path Directory path. + * + * @return Standard error code.*/ +int ext4_dir_open(ext4_dir *dir, const char *path); + +/**@brief Directory close. + * + * @param dir directory handle. + * + * @return Standard error code.*/ +int ext4_dir_close(ext4_dir *dir); + +/**@brief Return next directory entry. + * + * @param dir Directory handle. + * + * @return Directory entry id (NULL if no entry)*/ +const ext4_direntry *ext4_dir_entry_next(ext4_dir *dir); + +/**@brief Rewine directory entry offset. + * + * @param dir Directory handle.*/ +void ext4_dir_entry_rewind(ext4_dir *dir); + + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_balloc.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_balloc.h new file mode 100644 index 0000000..4e2f224 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_balloc.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_balloc.h + * @brief Physical block allocator. + */ + +#ifndef EXT4_BALLOC_H_ +#define EXT4_BALLOC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" + +#include "ext4_fs.h" + +#include +#include + +/**@brief Compute number of block group from block address. + * @param sb superblock pointer. + * @param baddr Absolute address of block. + * @return Block group index + */ +uint32_t ext4_balloc_get_bgid_of_block(struct ext4_sblock *s, + ext4_fsblk_t baddr); + +/**@brief Compute the starting block address of a block group + * @param sb superblock pointer. + * @param bgid block group index + * @return Block address + */ +ext4_fsblk_t ext4_balloc_get_block_of_bgid(struct ext4_sblock *s, + uint32_t bgid); + +/**@brief Calculate and set checksum of block bitmap. + * @param sb superblock pointer. + * @param bg block group + * @param bitmap bitmap buffer + */ +void ext4_balloc_set_bitmap_csum(struct ext4_sblock *sb, + struct ext4_bgroup *bg, + void *bitmap); + +/**@brief Free block from inode. + * @param inode_ref inode reference + * @param baddr block address + * @return standard error code*/ +int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t baddr); + +/**@brief Free blocks from inode. + * @param inode_ref inode reference + * @param first block address + * @param count block count + * @return standard error code*/ +int ext4_balloc_free_blocks(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t first, uint32_t count); + +/**@brief Allocate block procedure. + * @param inode_ref inode reference + * @param goal + * @param baddr allocated block address + * @return standard error code*/ +int ext4_balloc_alloc_block(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t goal, + ext4_fsblk_t *baddr); + +/**@brief Try allocate selected block. + * @param inode_ref inode reference + * @param baddr block address to allocate + * @param free if baddr is not allocated + * @return standard error code*/ +int ext4_balloc_try_alloc_block(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t baddr, bool *free); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_BALLOC_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_bcache.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_bcache.h new file mode 100644 index 0000000..b7ae438 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_bcache.h @@ -0,0 +1,297 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_bcache.h + * @brief Block cache allocator. + */ + +#ifndef EXT4_BCACHE_H_ +#define EXT4_BCACHE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" + +#include +#include +#include "misc/tree.h" +#include "misc/queue.h" + +#define EXT4_BLOCK_ZERO() \ + {.lb_id = 0, .data = 0} + +/**@brief Single block descriptor*/ +struct ext4_block { + /**@brief Logical block ID*/ + uint64_t lb_id; + + /**@brief Buffer */ + struct ext4_buf *buf; + + /**@brief Data buffer.*/ + uint8_t *data; +}; + +struct ext4_bcache; + +/**@brief Single block descriptor*/ +struct ext4_buf { + /**@brief Flags*/ + int flags; + + /**@brief Logical block address*/ + uint64_t lba; + + /**@brief Data buffer.*/ + uint8_t *data; + + /**@brief LRU priority. (unused) */ + uint32_t lru_prio; + + /**@brief LRU id.*/ + uint32_t lru_id; + + /**@brief Reference count table*/ + uint32_t refctr; + + /**@brief The block cache this buffer belongs to. */ + struct ext4_bcache *bc; + + /**@brief Whether or not buffer is on dirty list.*/ + bool on_dirty_list; + + /**@brief LBA tree node*/ + RB_ENTRY(ext4_buf) lba_node; + + /**@brief LRU tree node*/ + RB_ENTRY(ext4_buf) lru_node; + + /**@brief Dirty list node*/ + SLIST_ENTRY(ext4_buf) dirty_node; + + /**@brief Callback routine after a disk-write operation. + * @param bc block cache descriptor + * @param buf buffer descriptor + * @param standard error code returned by bdev->bwrite() + * @param arg argument passed to this routine*/ + void (*end_write)(struct ext4_bcache *bc, + struct ext4_buf *buf, + int res, + void *arg); + + /**@brief argument passed to end_write() callback.*/ + void *end_write_arg; +}; + +/**@brief Block cache descriptor*/ +struct ext4_bcache { + + /**@brief Item count in block cache*/ + uint32_t cnt; + + /**@brief Item size in block cache*/ + uint32_t itemsize; + + /**@brief Last recently used counter*/ + uint32_t lru_ctr; + + /**@brief Currently referenced datablocks*/ + uint32_t ref_blocks; + + /**@brief Maximum referenced datablocks*/ + uint32_t max_ref_blocks; + + /**@brief The blockdev binded to this block cache*/ + struct ext4_blockdev *bdev; + + /**@brief The cache should not be shaked */ + bool dont_shake; + + /**@brief A tree holding all bufs*/ + RB_HEAD(ext4_buf_lba, ext4_buf) lba_root; + + /**@brief A tree holding unreferenced bufs*/ + RB_HEAD(ext4_buf_lru, ext4_buf) lru_root; + + /**@brief A singly-linked list holding dirty buffers*/ + SLIST_HEAD(ext4_buf_dirty, ext4_buf) dirty_list; +}; + +/**@brief buffer state bits + * + * - BC♡UPTODATE: Buffer contains valid data. + * - BC_DIRTY: Buffer is dirty. + * - BC_FLUSH: Buffer will be immediately flushed, + * when no one references it. + * - BC_TMP: Buffer will be dropped once its refctr + * reaches zero. + */ +enum bcache_state_bits { + BC_UPTODATE, + BC_DIRTY, + BC_FLUSH, + BC_TMP +}; + +#define ext4_bcache_set_flag(buf, b) \ + (buf)->flags |= 1 << (b) + +#define ext4_bcache_clear_flag(buf, b) \ + (buf)->flags &= ~(1 << (b)) + +#define ext4_bcache_test_flag(buf, b) \ + (((buf)->flags & (1 << (b))) >> (b)) + +static inline void ext4_bcache_set_dirty(struct ext4_buf *buf) { + ext4_bcache_set_flag(buf, BC_UPTODATE); + ext4_bcache_set_flag(buf, BC_DIRTY); +} + +static inline void ext4_bcache_clear_dirty(struct ext4_buf *buf) { + ext4_bcache_clear_flag(buf, BC_UPTODATE); + ext4_bcache_clear_flag(buf, BC_DIRTY); +} + +/**@brief Increment reference counter of buf by 1.*/ +#define ext4_bcache_inc_ref(buf) ((buf)->refctr++) + +/**@brief Decrement reference counter of buf by 1.*/ +#define ext4_bcache_dec_ref(buf) ((buf)->refctr--) + +/**@brief Insert buffer to dirty cache list + * @param bc block cache descriptor + * @param buf buffer descriptor */ +static inline void +ext4_bcache_insert_dirty_node(struct ext4_bcache *bc, struct ext4_buf *buf) { + if (!buf->on_dirty_list) { + SLIST_INSERT_HEAD(&bc->dirty_list, buf, dirty_node); + buf->on_dirty_list = true; + } +} + +/**@brief Remove buffer to dirty cache list + * @param bc block cache descriptor + * @param buf buffer descriptor */ +static inline void +ext4_bcache_remove_dirty_node(struct ext4_bcache *bc, struct ext4_buf *buf) { + if (buf->on_dirty_list) { + SLIST_REMOVE(&bc->dirty_list, buf, ext4_buf, dirty_node); + buf->on_dirty_list = false; + } +} + + +/**@brief Dynamic initialization of block cache. + * @param bc block cache descriptor + * @param cnt items count in block cache + * @param itemsize single item size (in bytes) + * @return standard error code*/ +int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt, + uint32_t itemsize); + +/**@brief Do cleanup works on block cache. + * @param bc block cache descriptor.*/ +void ext4_bcache_cleanup(struct ext4_bcache *bc); + +/**@brief Dynamic de-initialization of block cache. + * @param bc block cache descriptor + * @return standard error code*/ +int ext4_bcache_fini_dynamic(struct ext4_bcache *bc); + +/**@brief Get a buffer with the lowest LRU counter in bcache. + * @param bc block cache descriptor + * @return buffer with the lowest LRU counter*/ +struct ext4_buf *ext4_buf_lowest_lru(struct ext4_bcache *bc); + +/**@brief Drop unreferenced buffer from bcache. + * @param bc block cache descriptor + * @param buf buffer*/ +void ext4_bcache_drop_buf(struct ext4_bcache *bc, struct ext4_buf *buf); + +/**@brief Invalidate a buffer. + * @param bc block cache descriptor + * @param buf buffer*/ +void ext4_bcache_invalidate_buf(struct ext4_bcache *bc, + struct ext4_buf *buf); + +/**@brief Invalidate a range of buffers. + * @param bc block cache descriptor + * @param from starting lba + * @param cnt block counts + * @param buf buffer*/ +void ext4_bcache_invalidate_lba(struct ext4_bcache *bc, + uint64_t from, + uint32_t cnt); + +/**@brief Find existing buffer from block cache memory. + * Unreferenced block allocation is based on LRU + * (Last Recently Used) algorithm. + * @param bc block cache descriptor + * @param b block to alloc + * @param lba logical block address + * @return block cache buffer */ +struct ext4_buf * +ext4_bcache_find_get(struct ext4_bcache *bc, struct ext4_block *b, + uint64_t lba); + +/**@brief Allocate block from block cache memory. + * Unreferenced block allocation is based on LRU + * (Last Recently Used) algorithm. + * @param bc block cache descriptor + * @param b block to alloc + * @param is_new block is new (needs to be read) + * @return standard error code*/ +int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b, + bool *is_new); + +/**@brief Free block from cache memory (decrement reference counter). + * @param bc block cache descriptor + * @param b block to free + * @return standard error code*/ +int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b); + +/**@brief Return a full status of block cache. + * @param bc block cache descriptor + * @return full status*/ +bool ext4_bcache_is_full(struct ext4_bcache *bc); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_BCACHE_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_bitmap.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_bitmap.h new file mode 100644 index 0000000..cb73b76 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_bitmap.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_bitmap.h + * @brief Block/inode bitmap allocator. + */ + +#ifndef EXT4_BITMAP_H_ +#define EXT4_BITMAP_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" + +#include +#include + +/**@brief Set bitmap bit. + * @param bmap bitmap + * @param bit bit to set*/ +static inline void ext4_bmap_bit_set(uint8_t *bmap, uint32_t bit) +{ + *(bmap + (bit >> 3)) |= (1 << (bit & 7)); +} + +/**@brief Clear bitmap bit. + * @param bmap bitmap buffer + * @param bit bit to clear*/ +static inline void ext4_bmap_bit_clr(uint8_t *bmap, uint32_t bit) +{ + *(bmap + (bit >> 3)) &= ~(1 << (bit & 7)); +} + +/**@brief Check if the bitmap bit is set. + * @param bmap bitmap buffer + * @param bit bit to check*/ +static inline bool ext4_bmap_is_bit_set(uint8_t *bmap, uint32_t bit) +{ + return (*(bmap + (bit >> 3)) & (1 << (bit & 7))); +} + +/**@brief Check if the bitmap bit is clear. + * @param bmap bitmap buffer + * @param bit bit to check*/ +static inline bool ext4_bmap_is_bit_clr(uint8_t *bmap, uint32_t bit) +{ + return !ext4_bmap_is_bit_set(bmap, bit); +} + +/**@brief Free range of bits in bitmap. + * @param bmap bitmap buffer + * @param sbit start bit + * @param bcnt bit count*/ +void ext4_bmap_bits_free(uint8_t *bmap, uint32_t sbit, uint32_t bcnt); + +/**@brief Find first clear bit in bitmap. + * @param sbit start bit of search + * @param ebit end bit of search + * @param bit_id output parameter (first free bit) + * @return standard error code*/ +int ext4_bmap_bit_find_clr(uint8_t *bmap, uint32_t sbit, uint32_t ebit, + uint32_t *bit_id); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_BITMAP_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_block_group.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_block_group.h new file mode 100644 index 0000000..88e00a1 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_block_group.h @@ -0,0 +1,330 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_block_group.h + * @brief Block group function set. + */ + +#ifndef EXT4_BLOCK_GROUP_H_ +#define EXT4_BLOCK_GROUP_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_super.h" + +#include +#include + +/**@brief Get address of block with data block bitmap. + * @param bg pointer to block group + * @param s pointer to superblock + * @return Address of block with block bitmap + */ +static inline uint64_t ext4_bg_get_block_bitmap(struct ext4_bgroup *bg, + struct ext4_sblock *s) +{ + uint64_t v = to_le32(bg->block_bitmap_lo); + + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint64_t)to_le32(bg->block_bitmap_hi) << 32; + + return v; +} + +/**@brief Set address of block with data block bitmap. + * @param bg pointer to block group + * @param s pointer to superblock + * @param blk block to set + * @return Address of block with block bitmap + */ +static inline void ext4_bg_set_block_bitmap(struct ext4_bgroup *bg, + struct ext4_sblock *s, uint64_t blk) +{ + + bg->block_bitmap_lo = to_le32((uint32_t)blk); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->block_bitmap_hi = to_le32(blk >> 32); + +} + +/**@brief Get address of block with i-node bitmap. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @return Address of block with i-node bitmap + */ +static inline uint64_t ext4_bg_get_inode_bitmap(struct ext4_bgroup *bg, + struct ext4_sblock *s) +{ + + uint64_t v = to_le32(bg->inode_bitmap_lo); + + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint64_t)to_le32(bg->inode_bitmap_hi) << 32; + + return v; +} + +/**@brief Set address of block with i-node bitmap. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @param blk block to set + * @return Address of block with i-node bitmap + */ +static inline void ext4_bg_set_inode_bitmap(struct ext4_bgroup *bg, + struct ext4_sblock *s, uint64_t blk) +{ + bg->inode_bitmap_lo = to_le32((uint32_t)blk); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->inode_bitmap_hi = to_le32(blk >> 32); + +} + + +/**@brief Get address of the first block of the i-node table. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @return Address of first block of i-node table + */ +static inline uint64_t +ext4_bg_get_inode_table_first_block(struct ext4_bgroup *bg, + struct ext4_sblock *s) +{ + uint64_t v = to_le32(bg->inode_table_first_block_lo); + + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint64_t)to_le32(bg->inode_table_first_block_hi) << 32; + + return v; +} + +/**@brief Set address of the first block of the i-node table. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @param blk block to set + * @return Address of first block of i-node table + */ +static inline void +ext4_bg_set_inode_table_first_block(struct ext4_bgroup *bg, + struct ext4_sblock *s, uint64_t blk) +{ + bg->inode_table_first_block_lo = to_le32((uint32_t)blk); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->inode_table_first_block_hi = to_le32(blk >> 32); +} + +/**@brief Get number of free blocks in block group. + * @param bg Pointer to block group + * @param sb Pointer to superblock + * @return Number of free blocks in block group + */ +static inline uint32_t ext4_bg_get_free_blocks_count(struct ext4_bgroup *bg, + struct ext4_sblock *s) +{ + uint32_t v = to_le16(bg->free_blocks_count_lo); + + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint32_t)to_le16(bg->free_blocks_count_hi) << 16; + + return v; +} + +/**@brief Set number of free blocks in block group. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @param cnt Number of free blocks in block group + */ +static inline void ext4_bg_set_free_blocks_count(struct ext4_bgroup *bg, + struct ext4_sblock *s, + uint32_t cnt) +{ + bg->free_blocks_count_lo = to_le16((cnt << 16) >> 16); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->free_blocks_count_hi = to_le16(cnt >> 16); +} + +/**@brief Get number of free i-nodes in block group. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @return Number of free i-nodes in block group + */ +static inline uint32_t ext4_bg_get_free_inodes_count(struct ext4_bgroup *bg, + struct ext4_sblock *s) +{ + uint32_t v = to_le16(bg->free_inodes_count_lo); + + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint32_t)to_le16(bg->free_inodes_count_hi) << 16; + + return v; +} + +/**@brief Set number of free i-nodes in block group. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @param cnt Number of free i-nodes in block group + */ +static inline void ext4_bg_set_free_inodes_count(struct ext4_bgroup *bg, + struct ext4_sblock *s, + uint32_t cnt) +{ + bg->free_inodes_count_lo = to_le16((cnt << 16) >> 16); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->free_inodes_count_hi = to_le16(cnt >> 16); +} + +/**@brief Get number of used directories in block group. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @return Number of used directories in block group + */ +static inline uint32_t ext4_bg_get_used_dirs_count(struct ext4_bgroup *bg, + struct ext4_sblock *s) +{ + uint32_t v = to_le16(bg->used_dirs_count_lo); + + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint32_t)to_le16(bg->used_dirs_count_hi) << 16; + + return v; +} + +/**@brief Set number of used directories in block group. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @param cnt Number of used directories in block group + */ +static inline void ext4_bg_set_used_dirs_count(struct ext4_bgroup *bg, + struct ext4_sblock *s, + uint32_t cnt) +{ + bg->used_dirs_count_lo = to_le16((cnt << 16) >> 16); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->used_dirs_count_hi = to_le16(cnt >> 16); +} + +/**@brief Get number of unused i-nodes. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @return Number of unused i-nodes + */ +static inline uint32_t ext4_bg_get_itable_unused(struct ext4_bgroup *bg, + struct ext4_sblock *s) +{ + + uint32_t v = to_le16(bg->itable_unused_lo); + + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + v |= (uint32_t)to_le16(bg->itable_unused_hi) << 16; + + return v; +} + +/**@brief Set number of unused i-nodes. + * @param bg Pointer to block group + * @param s Pointer to superblock + * @param cnt Number of unused i-nodes + */ +static inline void ext4_bg_set_itable_unused(struct ext4_bgroup *bg, + struct ext4_sblock *s, + uint32_t cnt) +{ + bg->itable_unused_lo = to_le16((cnt << 16) >> 16); + if (ext4_sb_get_desc_size(s) > EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->itable_unused_hi = to_le16(cnt >> 16); +} + +/**@brief Set checksum of block group. + * @param bg Pointer to block group + * @param crc Cheksum of block group + */ +static inline void ext4_bg_set_checksum(struct ext4_bgroup *bg, uint16_t crc) +{ + bg->checksum = to_le16(crc); +} + +/**@brief Check if block group has a flag. + * @param bg Pointer to block group + * @param flag Flag to be checked + * @return True if flag is set to 1 + */ +static inline bool ext4_bg_has_flag(struct ext4_bgroup *bg, uint32_t f) +{ + return to_le16(bg->flags) & f; +} + +/**@brief Set flag of block group. + * @param bg Pointer to block group + * @param flag Flag to be set + */ +static inline void ext4_bg_set_flag(struct ext4_bgroup *bg, uint32_t f) +{ + uint16_t flags = to_le16(bg->flags); + flags |= f; + bg->flags = to_le16(flags); +} + +/**@brief Clear flag of block group. + * @param bg Pointer to block group + * @param flag Flag to be cleared + */ +static inline void ext4_bg_clear_flag(struct ext4_bgroup *bg, uint32_t f) +{ + uint16_t flags = to_le16(bg->flags); + flags &= ~f; + bg->flags = to_le16(flags); +} + +/**@brief Calculate CRC16 of the block group. + * @param crc Init value + * @param buffer Input buffer + * @param len Sizeof input buffer + * @return Computed CRC16*/ +uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_BLOCK_GROUP_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_blockdev.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_blockdev.h new file mode 100644 index 0000000..e509649 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_blockdev.h @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_blockdev.h + * @brief Block device module. + */ + +#ifndef EXT4_BLOCKDEV_H_ +#define EXT4_BLOCKDEV_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_bcache.h" + +#include +#include + +struct ext4_blockdev_iface { + /**@brief Open device function + * @param bdev block device.*/ + int (*open)(struct ext4_blockdev *bdev); + + /**@brief Block read function. + * @param bdev block device + * @param buf output buffer + * @param blk_id block id + * @param blk_cnt block count*/ + int (*bread)(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id, + uint32_t blk_cnt); + + /**@brief Block write function. + * @param buf input buffer + * @param blk_id block id + * @param blk_cnt block count*/ + int (*bwrite)(struct ext4_blockdev *bdev, const void *buf, + uint64_t blk_id, uint32_t blk_cnt); + + /**@brief Close device function. + * @param bdev block device.*/ + int (*close)(struct ext4_blockdev *bdev); + + /**@brief Lock block device. Required in multi partition mode + * operations. Not mandatory field. + * @param bdev block device.*/ + int (*lock)(struct ext4_blockdev *bdev); + + /**@brief Unlock block device. Required in multi partition mode + * operations. Not mandatory field. + * @param bdev block device.*/ + int (*unlock)(struct ext4_blockdev *bdev); + + /**@brief Block size (bytes): physical*/ + uint32_t ph_bsize; + + /**@brief Block count: physical*/ + uint64_t ph_bcnt; + + /**@brief Block size buffer: physical*/ + uint8_t ph_bbuf[512]; + + /**@brief Reference counter to block device interface*/ + uint32_t ph_refctr; + + /**@brief Physical read counter*/ + uint32_t bread_ctr; + + /**@brief Physical write counter*/ + uint32_t bwrite_ctr; + + /**@brief User data pointer*/ + void* p_user; + + /**@brief Track/Cylinder : physical*/ + uint32_t ph_tcnt; + + /**@brief Sector/Track : physical*/ + uint32_t ph_scnt; + + char fname[512]; + void* dev_file; +}; + +/**@brief Definition of the simple block device.*/ +struct ext4_blockdev { + /**@brief Block device interface*/ + struct ext4_blockdev_iface *bdif; + + /**@brief Offset in bdif. For multi partition mode.*/ + uint64_t part_offset; + + /**@brief Part size in bdif. For multi partition mode.*/ + uint64_t part_size; + + /**@brief Block cache.*/ + struct ext4_bcache *bc; + + /**@brief Block size (bytes) logical*/ + uint32_t lg_bsize; + + /**@brief Block count: logical*/ + uint64_t lg_bcnt; + + /**@brief Cache write back mode reference counter*/ + uint32_t cache_write_back; + + /**@brief The filesystem this block device belongs to. */ + struct ext4_fs *fs; + + void *journal; +}; + +/**@brief Static initialization of the block device.*/ +#define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, __bread,\ + __bwrite, __close, __lock, __unlock) \ + static uint8_t __name##_ph_bbuf[(__bsize)]; \ + static struct ext4_blockdev_iface __name##_iface = { \ + .open = __open, \ + .bread = __bread, \ + .bwrite = __bwrite, \ + .close = __close, \ + .lock = __lock, \ + .unlock = __unlock, \ + .ph_bsize = __bsize, \ + .ph_bcnt = __bcnt, \ + .ph_bbuf = __name##_ph_bbuf, \ + }; \ + static struct ext4_blockdev __name = { \ + .bdif = &__name##_iface, \ + .part_offset = 0, \ + .part_size = (__bcnt) * (__bsize), \ + } + +/**@brief Block device initialization. + * @param bdev block device descriptor + * @param bg_bsize logical block size + * @param bdev block device descriptor + * @return standard error code*/ +int ext4_block_init(struct ext4_blockdev *bdev); + +/**@brief Binds a bcache to block device. + * @param bdev block device descriptor + * @param bc block cache descriptor + * @return standard error code*/ +int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc); + +/**@brief Close block device + * @param bdev block device descriptor + * @return standard error code*/ +int ext4_block_fini(struct ext4_blockdev *bdev); + +/**@brief Flush data in given buffer to disk. + * @param bdev block device descriptor + * @param buf buffer + * @return standard error code*/ +int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf); + +/**@brief Flush data in buffer of given lba to disk, + * if that buffer exists in block cache. + * @param bdev block device descriptor + * @param lba logical block address + * @return standard error code*/ +int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba); + +/**@brief Set logical block size in block device. + * @param bdev block device descriptor + * @param lb_size logical block size (in bytes) + * @return standard error code*/ +void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint32_t lb_bsize); + +/**@brief Block get function (through cache, don't read). + * @param bdev block device descriptor + * @param b block descriptor + * @param lba logical block address + * @return standard error code*/ +int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b, + uint64_t lba); + +/**@brief Block get function (through cache). + * @param bdev block device descriptor + * @param b block descriptor + * @param lba logical block address + * @return standard error code*/ +int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b, + uint64_t lba); + +/**@brief Block set procedure (through cache). + * @param bdev block device descriptor + * @param b block descriptor + * @return standard error code*/ +int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b); + +/**@brief Block read procedure (without cache) + * @param bdev block device descriptor + * @param buf output buffer + * @param lba logical block address + * @return standard error code*/ +int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba, + uint32_t cnt); + +/**@brief Block write procedure (without cache) + * @param bdev block device descriptor + * @param buf output buffer + * @param lba logical block address + * @return standard error code*/ +int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf, + uint64_t lba, uint32_t cnt); + +/**@brief Write to block device (by direct address). + * @param bdev block device descriptor + * @param off byte offset in block device + * @param buf input buffer + * @param len length of the write buffer + * @return standard error code*/ +int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off, + const void *buf, uint32_t len); + +/**@brief Read freom block device (by direct address). + * @param bdev block device descriptor + * @param off byte offset in block device + * @param buf input buffer + * @param len length of the write buffer + * @return standard error code*/ +int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf, + uint32_t len); + +/**@brief Flush all dirty buffers to disk + * @param bdev block device descriptor + * @return standard error code*/ +int ext4_block_cache_flush(struct ext4_blockdev *bdev); + +/**@brief Enable/disable write back cache mode + * @param bdev block device descriptor + * @param on_off + * !0 - ENABLE + * 0 - DISABLE (all delayed cache buffers will be flushed) + * @return standard error code*/ +int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_BLOCKDEV_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_config.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_config.h new file mode 100644 index 0000000..72dcdc3 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_config.h @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_config.h + * @brief Configuration file. + */ + +#ifndef EXT4_CONFIG_H_ +#define EXT4_CONFIG_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#if !CONFIG_USE_DEFAULT_CFG +#include "generated/ext4_config.h" +#endif + +/*****************************************************************************/ + +#define F_SET_EXT2 2 +#define F_SET_EXT3 3 +#define F_SET_EXT4 4 + +#ifndef CONFIG_EXT_FEATURE_SET_LVL +#define CONFIG_EXT_FEATURE_SET_LVL F_SET_EXT4 +#endif + +/*****************************************************************************/ + +#if CONFIG_EXT_FEATURE_SET_LVL == F_SET_EXT2 +/*Superblock features flag EXT2*/ +#define CONFIG_SUPPORTED_FCOM EXT2_SUPPORTED_FCOM +#define CONFIG_SUPPORTED_FINCOM (EXT2_SUPPORTED_FINCOM | EXT_FINCOM_IGNORED) +#define CONFIG_SUPPORTED_FRO_COM EXT2_SUPPORTED_FRO_COM + +#elif CONFIG_EXT_FEATURE_SET_LVL == F_SET_EXT3 +/*Superblock features flag EXT3*/ +#define CONFIG_SUPPORTED_FCOM EXT3_SUPPORTED_FCOM +#define CONFIG_SUPPORTED_FINCOM (EXT3_SUPPORTED_FINCOM | EXT_FINCOM_IGNORED) +#define CONFIG_SUPPORTED_FRO_COM EXT3_SUPPORTED_FRO_COM +#elif CONFIG_EXT_FEATURE_SET_LVL == F_SET_EXT4 +/*Superblock features flag EXT4*/ +#define CONFIG_SUPPORTED_FCOM EXT4_SUPPORTED_FCOM +#define CONFIG_SUPPORTED_FINCOM (EXT4_SUPPORTED_FINCOM | EXT_FINCOM_IGNORED) +#define CONFIG_SUPPORTED_FRO_COM EXT4_SUPPORTED_FRO_COM +#else +#define "Unsupported CONFIG_EXT_FEATURE_SET_LVL" +#endif + +#define CONFIG_DIR_INDEX_ENABLE (CONFIG_SUPPORTED_FCOM & EXT4_FCOM_DIR_INDEX) +#define CONFIG_EXTENT_ENABLE (CONFIG_SUPPORTED_FINCOM & EXT4_FINCOM_EXTENTS) +#define CONFIG_META_CSUM_ENABLE (CONFIG_SUPPORTED_FRO_COM & EXT4_FRO_COM_METADATA_CSUM) + +/*****************************************************************************/ + +/**@brief Enable/disable journaling*/ +#ifndef CONFIG_JOURNALING_ENABLE +#define CONFIG_JOURNALING_ENABLE 1 +#endif + +/**@brief Enable/disable xattr*/ +#ifndef CONFIG_XATTR_ENABLE +#define CONFIG_XATTR_ENABLE 1 +#endif + +/**@brief Enable/disable extents*/ +#ifndef CONFIG_EXTENTS_ENABLE +#define CONFIG_EXTENTS_ENABLE 1 +#endif + +/**@brief Include error codes from ext4_errno or standard library.*/ +#ifndef CONFIG_HAVE_OWN_ERRNO +#define CONFIG_HAVE_OWN_ERRNO 0 +#endif + +/**@brief Debug printf enable (stdout)*/ +#ifndef CONFIG_DEBUG_PRINTF +#define CONFIG_DEBUG_PRINTF 1 +#endif + +/**@brief Assert printf enable (stdout)*/ +#ifndef CONFIG_DEBUG_ASSERT +#define CONFIG_DEBUG_ASSERT 1 +#endif + +/**@brief Include assert codes from ext4_debug or standard library.*/ +#ifndef CONFIG_HAVE_OWN_ASSERT +#define CONFIG_HAVE_OWN_ASSERT 1 +#endif + +/**@brief Statistics of block device*/ +#ifndef CONFIG_BLOCK_DEV_ENABLE_STATS +#define CONFIG_BLOCK_DEV_ENABLE_STATS 1 +#endif + +/**@brief Cache size of block device.*/ +#ifndef CONFIG_BLOCK_DEV_CACHE_SIZE +#define CONFIG_BLOCK_DEV_CACHE_SIZE 8 +#endif + + +/**@brief Maximum block device name*/ +#ifndef CONFIG_EXT4_MAX_BLOCKDEV_NAME +#define CONFIG_EXT4_MAX_BLOCKDEV_NAME 32 +#endif + + +/**@brief Maximum block device count*/ +#ifndef CONFIG_EXT4_BLOCKDEVS_COUNT +#define CONFIG_EXT4_BLOCKDEVS_COUNT 2 +#endif + +/**@brief Maximum mountpoint name*/ +#ifndef CONFIG_EXT4_MAX_MP_NAME +#define CONFIG_EXT4_MAX_MP_NAME 32 +#endif + +/**@brief Maximum mountpoint count*/ +#ifndef CONFIG_EXT4_MOUNTPOINTS_COUNT +#define CONFIG_EXT4_MOUNTPOINTS_COUNT 2 +#endif + +/**@brief Include open flags from ext4_errno or standard library.*/ +#ifndef CONFIG_HAVE_OWN_OFLAGS +#define CONFIG_HAVE_OWN_OFLAGS 1 +#endif + +/**@brief Maximum single truncate size. Transactions must be limited to reduce + * number of allocetions for single transaction*/ +#ifndef CONFIG_MAX_TRUNCATE_SIZE +#define CONFIG_MAX_TRUNCATE_SIZE (16ul * 1024ul * 1024ul) +#endif + + +/**@brief Unaligned access switch on/off*/ +#ifndef CONFIG_UNALIGNED_ACCESS +#define CONFIG_UNALIGNED_ACCESS 0 +#endif + +/**@brief Switches use of malloc/free functions family + * from standard library to user provided*/ +#ifndef CONFIG_USE_USER_MALLOC +#define CONFIG_USE_USER_MALLOC 0 +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_CONFIG_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_crc32.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_crc32.h new file mode 100644 index 0000000..3dad1d1 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_crc32.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Based on FreeBSD. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_crc32.h + * @brief Crc32c routine. Taken from FreeBSD kernel. + */ + +#ifndef LWEXT4_EXT4_CRC32C_H_ +#define LWEXT4_EXT4_CRC32C_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" + +#include + +/**@brief CRC32 algorithm. + * @param crc input feed + * @param buf input buffer + * @param size input buffer length (bytes) + * @return updated crc32 value*/ +uint32_t ext4_crc32(uint32_t crc, const void *buf, uint32_t size); + +/**@brief CRC32C algorithm. + * @param crc input feed + * @param buf input buffer + * @param length input buffer length (bytes) + * @return updated crc32c value*/ +uint32_t ext4_crc32c(uint32_t crc, const void *buf, uint32_t size); + +#ifdef __cplusplus +} +#endif + +#endif /* LWEXT4_EXT4_CRC32C_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_debug.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_debug.h new file mode 100644 index 0000000..6eb8aa1 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_debug.h @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_debug.c + * @brief Debug printf and assert macros. + */ + +#ifndef EXT4_DEBUG_H_ +#define EXT4_DEBUG_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_errno.h" + +#if !CONFIG_HAVE_OWN_ASSERT +#include +#endif + +#include +#include + +#ifndef PRIu64 +#define PRIu64 "llu" +#endif + +#ifndef PRId64 +#define PRId64 "lld" +#endif + + +#define DEBUG_BALLOC (1ul << 0) +#define DEBUG_BCACHE (1ul << 1) +#define DEBUG_BITMAP (1ul << 2) +#define DEBUG_BLOCK_GROUP (1ul << 3) +#define DEBUG_BLOCKDEV (1ul << 4) +#define DEBUG_DIR_IDX (1ul << 5) +#define DEBUG_DIR (1ul << 6) +#define DEBUG_EXTENT (1ul << 7) +#define DEBUG_FS (1ul << 8) +#define DEBUG_HASH (1ul << 9) +#define DEBUG_IALLOC (1ul << 10) +#define DEBUG_INODE (1ul << 11) +#define DEBUG_SUPER (1ul << 12) +#define DEBUG_XATTR (1ul << 13) +#define DEBUG_MKFS (1ul << 14) +#define DEBUG_EXT4 (1ul << 15) +#define DEBUG_JBD (1ul << 16) +#define DEBUG_MBR (1ul << 17) + +#define DEBUG_NOPREFIX (1ul << 31) +#define DEBUG_ALL (0xFFFFFFFF) + +static inline const char *ext4_dmask_id2str(uint32_t m) +{ + switch(m) { + case DEBUG_BALLOC: + return "ext4_balloc: "; + case DEBUG_BCACHE: + return "ext4_bcache: "; + case DEBUG_BITMAP: + return "ext4_bitmap: "; + case DEBUG_BLOCK_GROUP: + return "ext4_block_group: "; + case DEBUG_BLOCKDEV: + return "ext4_blockdev: "; + case DEBUG_DIR_IDX: + return "ext4_dir_idx: "; + case DEBUG_DIR: + return "ext4_dir: "; + case DEBUG_EXTENT: + return "ext4_extent: "; + case DEBUG_FS: + return "ext4_fs: "; + case DEBUG_HASH: + return "ext4_hash: "; + case DEBUG_IALLOC: + return "ext4_ialloc: "; + case DEBUG_INODE: + return "ext4_inode: "; + case DEBUG_SUPER: + return "ext4_super: "; + case DEBUG_XATTR: + return "ext4_xattr: "; + case DEBUG_MKFS: + return "ext4_mkfs: "; + case DEBUG_JBD: + return "ext4_jbd: "; + case DEBUG_MBR: + return "ext4_mbr: "; + case DEBUG_EXT4: + return "ext4: "; + } + return ""; +} +#define DBG_NONE "" +#define DBG_INFO "[info] " +#define DBG_WARN "[warn] " +#define DBG_ERROR "[error] " + +/**@brief Global mask debug set. + * @brief m new debug mask.*/ +void ext4_dmask_set(uint32_t m); + +/**@brief Global mask debug clear. + * @brief m new debug mask.*/ +void ext4_dmask_clr(uint32_t m); + +/**@brief Global debug mask get. + * @return debug mask*/ +uint32_t ext4_dmask_get(void); + +#if CONFIG_DEBUG_PRINTF +#include + +/**@brief Debug printf.*/ +#define ext4_dbg(m, ...) \ + do { \ + if ((m) & ext4_dmask_get()) { \ + if (!((m) & DEBUG_NOPREFIX)) { \ + printf("%s", ext4_dmask_id2str(m)); \ + printf("l: %d ", __LINE__); \ + } \ + printf(__VA_ARGS__); \ + fflush(stdout); \ + } \ + } while (0) +#else +#define ext4_dbg(m, ...) do { } while (0) +#endif + +#if CONFIG_DEBUG_ASSERT +/**@brief Debug assertion.*/ +#if CONFIG_HAVE_OWN_ASSERT +#include + +#define ext4_assert(_v) \ + do { \ + if (!(_v)) { \ + printf("assertion failed:\nfile: %s\nline: %d\n", \ + __FILE__, __LINE__); \ + while (1) \ + ; \ + } \ + } while (0) +#else +#define ext4_assert(_v) assert(_v) +#endif +#else +#define ext4_assert(_v) ((void)(_v)) +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_DEBUG_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_dir.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_dir.h new file mode 100644 index 0000000..d3b1bf5 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_dir.h @@ -0,0 +1,301 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_dir.h + * @brief Directory handle procedures. + */ + +#ifndef EXT4_DIR_H_ +#define EXT4_DIR_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_blockdev.h" +#include "ext4_super.h" + +#include + +struct ext4_dir_iter { + struct ext4_inode_ref *inode_ref; + struct ext4_block curr_blk; + uint64_t curr_off; + struct ext4_dir_en *curr; +}; + +struct ext4_dir_search_result { + struct ext4_block block; + struct ext4_dir_en *dentry; +}; + + +/**@brief Get i-node number from directory entry. + * @param de Directory entry + * @return I-node number + */ +static inline uint32_t +ext4_dir_en_get_inode(struct ext4_dir_en *de) +{ + return to_le32(de->inode); +} + +/**@brief Set i-node number to directory entry. + * @param de Directory entry + * @param inode I-node number + */ +static inline void +ext4_dir_en_set_inode(struct ext4_dir_en *de, uint32_t inode) +{ + de->inode = to_le32(inode); +} + +/**@brief Set i-node number to directory entry. (For HTree root) + * @param de Directory entry + * @param inode I-node number + */ +static inline void +ext4_dx_dot_en_set_inode(struct ext4_dir_idx_dot_en *de, uint32_t inode) +{ + de->inode = to_le32(inode); +} + +/**@brief Get directory entry length. + * @param de Directory entry + * @return Entry length + */ +static inline uint16_t ext4_dir_en_get_entry_len(struct ext4_dir_en *de) +{ + return to_le16(de->entry_len); +} + +/**@brief Set directory entry length. + * @param de Directory entry + * @param length Entry length + */ +static inline void ext4_dir_en_set_entry_len(struct ext4_dir_en *de, uint16_t l) +{ + de->entry_len = to_le16(l); +} + +/**@brief Get directory entry name length. + * @param sb Superblock + * @param de Directory entry + * @return Entry name length + */ +static inline uint16_t ext4_dir_en_get_name_len(struct ext4_sblock *sb, + struct ext4_dir_en *de) +{ + uint16_t v = de->name_len; + + if ((ext4_get32(sb, rev_level) == 0) && + (ext4_get32(sb, minor_rev_level) < 5)) + v |= ((uint16_t)de->in.name_length_high) << 8; + + return v; +} + +/**@brief Set directory entry name length. + * @param sb Superblock + * @param de Directory entry + * @param length Entry name length + */ +static inline void ext4_dir_en_set_name_len(struct ext4_sblock *sb, + struct ext4_dir_en *de, + uint16_t len) +{ + de->name_len = (len << 8) >> 8; + + if ((ext4_get32(sb, rev_level) == 0) && + (ext4_get32(sb, minor_rev_level) < 5)) + de->in.name_length_high = len >> 8; +} + +/**@brief Get i-node type of directory entry. + * @param sb Superblock + * @param de Directory entry + * @return I-node type (file, dir, etc.) + */ +static inline uint8_t ext4_dir_en_get_inode_type(struct ext4_sblock *sb, + struct ext4_dir_en *de) +{ + if ((ext4_get32(sb, rev_level) > 0) || + (ext4_get32(sb, minor_rev_level) >= 5)) + return de->in.inode_type; + + return EXT4_DE_UNKNOWN; +} +/**@brief Set i-node type of directory entry. + * @param sb Superblock + * @param de Directory entry + * @param type I-node type (file, dir, etc.) + */ + +static inline void ext4_dir_en_set_inode_type(struct ext4_sblock *sb, + struct ext4_dir_en *de, uint8_t t) +{ + if ((ext4_get32(sb, rev_level) > 0) || + (ext4_get32(sb, minor_rev_level) >= 5)) + de->in.inode_type = t; +} + +/**@brief Verify checksum of a linear directory leaf block + * @param inode_ref Directory i-node + * @param dirent Linear directory leaf block + * @return true means the block passed checksum verification + */ +bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref, + struct ext4_dir_en *dirent); + +/**@brief Initialize directory iterator. + * Set position to the first valid entry from the required position. + * @param it Pointer to iterator to be initialized + * @param inode_ref Directory i-node + * @param pos Position to start reading entries from + * @return Error code + */ +int ext4_dir_iterator_init(struct ext4_dir_iter *it, + struct ext4_inode_ref *inode_ref, uint64_t pos); + +/**@brief Jump to the next valid entry + * @param it Initialized iterator + * @return Error code + */ +int ext4_dir_iterator_next(struct ext4_dir_iter *it); + +/**@brief Uninitialize directory iterator. + * Release all allocated structures. + * @param it Iterator to be finished + * @return Error code + */ +int ext4_dir_iterator_fini(struct ext4_dir_iter *it); + +/**@brief Write directory entry to concrete data block. + * @param sb Superblock + * @param en Pointer to entry to be written + * @param entry_len Length of new entry + * @param child Child i-node to be written to new entry + * @param name Name of the new entry + * @param name_len Length of entry name + */ +void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en, + uint16_t entry_len, struct ext4_inode_ref *child, + const char *name, size_t name_len); + +/**@brief Add new entry to the directory. + * @param parent Directory i-node + * @param name Name of new entry + * @param child I-node to be referenced from new entry + * @return Error code + */ +int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name, + uint32_t name_len, struct ext4_inode_ref *child); + +/**@brief Find directory entry with passed name. + * @param result Result structure to be returned if entry found + * @param parent Directory i-node + * @param name Name of entry to be found + * @param name_len Name length + * @return Error code + */ +int ext4_dir_find_entry(struct ext4_dir_search_result *result, + struct ext4_inode_ref *parent, const char *name, + uint32_t name_len); + +/**@brief Remove directory entry. + * @param parent Directory i-node + * @param name Name of the entry to be removed + * @param name_len Name length + * @return Error code + */ +int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name, + uint32_t name_len); + +/**@brief Try to insert entry to concrete data block. + * @param sb Superblock + * @param inode_ref Directory i-node + * @param dst_blk Block to try to insert entry to + * @param child Child i-node to be inserted by new entry + * @param name Name of the new entry + * @param name_len Length of the new entry name + * @return Error code + */ +int ext4_dir_try_insert_entry(struct ext4_sblock *sb, + struct ext4_inode_ref *inode_ref, + struct ext4_block *dst_blk, + struct ext4_inode_ref *child, const char *name, + uint32_t name_len); + +/**@brief Try to find entry in block by name. + * @param block Block containing entries + * @param sb Superblock + * @param name_len Length of entry name + * @param name Name of entry to be found + * @param res_entry Output pointer to found entry, NULL if not found + * @return Error code + */ +int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb, + size_t name_len, const char *name, + struct ext4_dir_en **res_entry); + +/**@brief Simple function to release allocated data from result. + * @param parent Parent inode + * @param result Search result to destroy + * @return Error code + * + */ +int ext4_dir_destroy_result(struct ext4_inode_ref *parent, + struct ext4_dir_search_result *result); + +void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref, + struct ext4_dir_en *dirent); + + +void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_DIR_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_dir_idx.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_dir_idx.h new file mode 100644 index 0000000..f3e83d6 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_dir_idx.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_dir_idx.h + * @brief Directory indexing procedures. + */ + +#ifndef EXT4_DIR_IDX_H_ +#define EXT4_DIR_IDX_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" + +#include "ext4_fs.h" +#include "ext4_dir.h" + +#include +#include + +struct ext4_dir_idx_block { + struct ext4_block b; + struct ext4_dir_idx_entry *entries; + struct ext4_dir_idx_entry *position; +}; + +#define EXT4_DIR_DX_INIT_BCNT 2 + + +/**@brief Initialize index structure of new directory. + * @param dir Pointer to directory i-node + * @param dir Pointer to parent directory i-node + * @return Error code + */ +int ext4_dir_dx_init(struct ext4_inode_ref *dir, + struct ext4_inode_ref *parent); + +/**@brief Try to find directory entry using directory index. + * @param result Output value - if entry will be found, + * than will be passed through this parameter + * @param inode_ref Directory i-node + * @param name_len Length of name to be found + * @param name Name to be found + * @return Error code + */ +int ext4_dir_dx_find_entry(struct ext4_dir_search_result *result, + struct ext4_inode_ref *inode_ref, size_t name_len, + const char *name); + +/**@brief Add new entry to indexed directory + * @param parent Directory i-node + * @param child I-node to be referenced from directory entry + * @param name Name of new directory entry + * @return Error code + */ +int ext4_dir_dx_add_entry(struct ext4_inode_ref *parent, + struct ext4_inode_ref *child, const char *name, uint32_t name_len); + +/**@brief Add new entry to indexed directory + * @param dir Directory i-node + * @param parent_inode parent inode index + * @return Error code + */ +int ext4_dir_dx_reset_parent_inode(struct ext4_inode_ref *dir, + uint32_t parent_inode); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_DIR_IDX_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_errno.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_errno.h new file mode 100644 index 0000000..edf89a9 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_errno.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_errno.h + * @brief Error codes. + */ +#ifndef EXT4_ERRNO_H_ +#define EXT4_ERRNO_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" + +#if !CONFIG_HAVE_OWN_ERRNO +#include +#else +#define EPERM 1 /* Operation not permitted */ +#define ENOENT 2 /* No such file or directory */ +#define EIO 5 /* I/O error */ +#define ENXIO 6 /* No such device or address */ +#define E2BIG 7 /* Argument list too long */ +#define ENOMEM 12 /* Out of memory */ +#define EACCES 13 /* Permission denied */ +#define EFAULT 14 /* Bad address */ +#define EEXIST 17 /* File exists */ +#define ENODEV 19 /* No such device */ +#define ENOTDIR 20 /* Not a directory */ +#define EISDIR 21 /* Is a directory */ +#define EINVAL 22 /* Invalid argument */ +#define EFBIG 27 /* File too large */ +#define ENOSPC 28 /* No space left on device */ +#define EROFS 30 /* Read-only file system */ +#define EMLINK 31 /* Too many links */ +#define ERANGE 34 /* Math result not representable */ +#define ENOTEMPTY 39 /* Directory not empty */ +#define ENODATA 61 /* No data available */ +#define ENOTSUP 95 /* Not supported */ +#endif + +#ifndef ENODATA + #ifdef ENOATTR + #define ENODATA ENOATTR + #else + #define ENODATA 61 + #endif +#endif + +#ifndef ENOTSUP +#define ENOTSUP 95 +#endif + +#ifndef EOK +#define EOK 0 +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_ERRNO_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_extent.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_extent.h new file mode 100644 index 0000000..3032b03 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_extent.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_extent.h + * @brief More complex filesystem functions. + */ +#ifndef EXT4_EXTENT_H_ +#define EXT4_EXTENT_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_inode.h" + +void ext4_extent_tree_init(struct ext4_inode_ref *inode_ref); + + +int ext4_extent_get_blocks(struct ext4_inode_ref *inode_ref, ext4_lblk_t iblock, + uint32_t max_blocks, ext4_fsblk_t *result, bool create, + uint32_t *blocks_count); + + +/**@brief Release all data blocks starting from specified logical block. + * @param inode_ref I-node to release blocks from + * @param iblock_from First logical block to release + * @return Error code */ +int ext4_extent_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from, + ext4_lblk_t to); + + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_EXTENT_H_ */ +/** +* @} +*/ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_fs.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_fs.h new file mode 100644 index 0000000..9653f47 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_fs.h @@ -0,0 +1,280 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_fs.c + * @brief More complex filesystem functions. + */ + +#ifndef EXT4_FS_H_ +#define EXT4_FS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" + +#include +#include + +struct ext4_fs { + bool read_only; + + struct ext4_blockdev *bdev; + struct ext4_sblock sb; + + uint64_t inode_block_limits[4]; + uint64_t inode_blocks_per_level[4]; + + uint32_t last_inode_bg_id; + + struct jbd_fs *jbd_fs; + struct jbd_journal *jbd_journal; + struct jbd_trans *curr_trans; +}; + +struct ext4_block_group_ref { + struct ext4_block block; + struct ext4_bgroup *block_group; + struct ext4_fs *fs; + uint32_t index; + bool dirty; +}; + +struct ext4_inode_ref { + struct ext4_block block; + struct ext4_inode *inode; + struct ext4_fs *fs; + uint32_t index; + bool dirty; +}; + + +/**@brief Convert block address to relative index in block group. + * @param sb Superblock pointer + * @param baddr Block number to convert + * @return Relative number of block + */ +static inline uint32_t ext4_fs_addr_to_idx_bg(struct ext4_sblock *s, + ext4_fsblk_t baddr) +{ + if (ext4_get32(s, first_data_block) && baddr) + baddr--; + + return baddr % ext4_get32(s, blocks_per_group); +} + +/**@brief Convert relative block address in group to absolute address. + * @param s Superblock pointer + * @param index Relative block address + * @param bgid Block group + * @return Absolute block address + */ +static inline ext4_fsblk_t ext4_fs_bg_idx_to_addr(struct ext4_sblock *s, + uint32_t index, + uint32_t bgid) +{ + if (ext4_get32(s, first_data_block)) + index++; + + return ext4_get32(s, blocks_per_group) * bgid + index; +} + +/**@brief TODO: */ +static inline ext4_fsblk_t ext4_fs_first_bg_block_no(struct ext4_sblock *s, + uint32_t bgid) +{ + return (uint64_t)bgid * ext4_get32(s, blocks_per_group) + + ext4_get32(s, first_data_block); +} + +/**@brief Initialize filesystem and read all needed data. + * @param fs Filesystem instance to be initialized + * @param bdev Identifier if device with the filesystem + * @param read_only Mark the filesystem as read-only. + * @return Error code + */ +int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev, + bool read_only); + +/**@brief Destroy filesystem instance (used by unmount operation). + * @param fs Filesystem to be destroyed + * @return Error code + */ +int ext4_fs_fini(struct ext4_fs *fs); + +/**@brief Check filesystem's features, if supported by this driver + * Function can return EOK and set read_only flag. It mean's that + * there are some not-supported features, that can cause problems + * during some write operations. + * @param fs Filesystem to be checked + * @param read_only Flag if filesystem should be mounted only for reading + * @return Error code + */ +int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only); + +/**@brief Get reference to block group specified by index. + * @param fs Filesystem to find block group on + * @param bgid Index of block group to load + * @param ref Output pointer for reference + * @return Error code + */ +int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid, + struct ext4_block_group_ref *ref); + +/**@brief Put reference to block group. + * @param ref Pointer for reference to be put back + * @return Error code + */ +int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref); + +/**@brief Get reference to i-node specified by index. + * @param fs Filesystem to find i-node on + * @param index Index of i-node to load + * @param ref Output pointer for reference + * @return Error code + */ +int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index, + struct ext4_inode_ref *ref); + +/**@brief Reset blocks field of i-node. + * @param fs Filesystem to reset blocks field of i-inode on + * @param inode_ref ref Pointer for inode to be operated on + */ +void ext4_fs_inode_blocks_init(struct ext4_fs *fs, + struct ext4_inode_ref *inode_ref); + +/**@brief Put reference to i-node. + * @param ref Pointer for reference to be put back + * @return Error code + */ +int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref); + +/**@brief Convert filetype to inode mode. + * @param filetype + * @return inode mode + */ +uint32_t ext4_fs_correspond_inode_mode(int filetype); + +/**@brief Allocate new i-node in the filesystem. + * @param fs Filesystem to allocated i-node on + * @param inode_ref Output pointer to return reference to allocated i-node + * @param filetype File type of newly created i-node + * @return Error code + */ +int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref, + int filetype); + +/**@brief Release i-node and mark it as free. + * @param inode_ref I-node to be released + * @return Error code + */ +int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref); + +/**@brief Truncate i-node data blocks. + * @param inode_ref I-node to be truncated + * @param new_size New size of inode (must be < current size) + * @return Error code + */ +int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size); + +/**@brief Compute 'goal' for inode index + * @param inode_ref Reference to inode, to allocate block for + * @return goal + */ +ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref); + +/**@brief Compute 'goal' for allocation algorithm (For blockmap). + * @param inode_ref Reference to inode, to allocate block for + * @param goal + * @return error code + */ +int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t *goal); + +/**@brief Get physical block address by logical index of the block. + * @param inode_ref I-node to read block address from + * @param iblock Logical index of block + * @param fblock Output pointer for return physical + * block address + * @param support_unwritten Indicate whether unwritten block range + * is supported under the current context + * @return Error code + */ +int ext4_fs_get_inode_dblk_idx(struct ext4_inode_ref *inode_ref, + ext4_lblk_t iblock, ext4_fsblk_t *fblock, + bool support_unwritten); + +/**@brief Initialize a part of unwritten range of the inode. + * @param inode_ref I-node to proceed on. + * @param iblock Logical index of block + * @param fblock Output pointer for return physical block address + * @return Error code + */ +int ext4_fs_init_inode_dblk_idx(struct ext4_inode_ref *inode_ref, + ext4_lblk_t iblock, ext4_fsblk_t *fblock); + +/**@brief Append following logical block to the i-node. + * @param inode_ref I-node to append block to + * @param fblock Output physical block address of newly allocated block + * @param iblock Output logical number of newly allocated block + * @return Error code + */ +int ext4_fs_append_inode_dblk(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t *fblock, ext4_lblk_t *iblock); + +/**@brief Increment inode link count. + * @param inode none handle + */ +void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref); + +/**@brief Decrement inode link count. + * @param inode none handle + */ +void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_FS_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_hash.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_hash.h new file mode 100644 index 0000000..17989a1 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_hash.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_hash.h + * @brief Directory indexing hash functions. + */ + +#ifndef EXT4_HASH_H_ +#define EXT4_HASH_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" + +#include + +struct ext4_hash_info { + uint32_t hash; + uint32_t minor_hash; + uint32_t hash_version; + const uint32_t *seed; +}; + + +/**@brief Directory entry name hash function. + * @param name entry name + * @param len entry name length + * @param hash_seed (from superblock) + * @param hash version (from superblock) + * @param hash_minor output value + * @param hash_major output value + * @return standard error code*/ +int ext2_htree_hash(const char *name, int len, const uint32_t *hash_seed, + int hash_version, uint32_t *hash_major, + uint32_t *hash_minor); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_HASH_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_ialloc.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_ialloc.h new file mode 100644 index 0000000..cea3fe6 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_ialloc.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_ialloc.c + * @brief Inode allocation procedures. + */ + +#ifndef EXT4_IALLOC_H_ +#define EXT4_IALLOC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" + +/**@brief Calculate and set checksum of inode bitmap. + * @param sb superblock pointer. + * @param bg block group + * @param bitmap bitmap buffer + */ +void ext4_ialloc_set_bitmap_csum(struct ext4_sblock *sb, struct ext4_bgroup *bg, + void *bitmap); + +/**@brief Free i-node number and modify filesystem data structers. + * @param fs Filesystem, where the i-node is located + * @param index Index of i-node to be release + * @param is_dir Flag us for information whether i-node is directory or not + */ +int ext4_ialloc_free_inode(struct ext4_fs *fs, uint32_t index, bool is_dir); + +/**@brief I-node allocation algorithm. + * This is more simple algorithm, than Orlov allocator used + * in the Linux kernel. + * @param fs Filesystem to allocate i-node on + * @param index Output value - allocated i-node number + * @param is_dir Flag if allocated i-node will be file or directory + * @return Error code + */ +int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *index, bool is_dir); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_IALLOC_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_inode.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_inode.h new file mode 100644 index 0000000..e0ca6e3 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_inode.h @@ -0,0 +1,354 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_inode.h + * @brief Inode handle functions + */ + +#ifndef EXT4_INODE_H_ +#define EXT4_INODE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" + +#include + +/**@brief Get mode of the i-node. + * @param sb Superblock + * @param inode I-node to load mode from + * @return Mode of the i-node + */ +uint32_t ext4_inode_get_mode(struct ext4_sblock *sb, struct ext4_inode *inode); + +/**@brief Set mode of the i-node. + * @param sb Superblock + * @param inode I-node to set mode to + * @param mode Mode to set to i-node + */ +void ext4_inode_set_mode(struct ext4_sblock *sb, struct ext4_inode *inode, + uint32_t mode); + +/**@brief Get ID of the i-node owner (user id). + * @param inode I-node to load uid from + * @return User ID of the i-node owner + */ +uint32_t ext4_inode_get_uid(struct ext4_inode *inode); + +/**@brief Set ID of the i-node owner. + * @param inode I-node to set uid to + * @param uid ID of the i-node owner + */ +void ext4_inode_set_uid(struct ext4_inode *inode, uint32_t uid); + +/**@brief Get real i-node size. + * @param sb Superblock + * @param inode I-node to load size from + * @return Real size of i-node + */ +uint64_t ext4_inode_get_size(struct ext4_sblock *sb, struct ext4_inode *inode); + +/**@brief Set real i-node size. + * @param inode I-node to set size to + * @param size Size of the i-node + */ +void ext4_inode_set_size(struct ext4_inode *inode, uint64_t size); + +/**@brief Get time, when i-node was last accessed. + * @param inode I-node + * @return Time of the last access (POSIX) + */ +uint32_t ext4_inode_get_access_time(struct ext4_inode *inode); + +/**@brief Set time, when i-node was last accessed. + * @param inode I-node + * @param time Time of the last access (POSIX) + */ +void ext4_inode_set_access_time(struct ext4_inode *inode, uint32_t time); + +/**@brief Get time, when i-node was last changed. + * @param inode I-node + * @return Time of the last change (POSIX) + */ +uint32_t ext4_inode_get_change_inode_time(struct ext4_inode *inode); + +/**@brief Set time, when i-node was last changed. + * @param inode I-node + * @param time Time of the last change (POSIX) + */ +void ext4_inode_set_change_inode_time(struct ext4_inode *inode, uint32_t time); + +/**@brief Get time, when i-node content was last modified. + * @param inode I-node + * @return Time of the last content modification (POSIX) + */ +uint32_t ext4_inode_get_modif_time(struct ext4_inode *inode); + +/**@brief Set time, when i-node content was last modified. + * @param inode I-node + * @param time Time of the last content modification (POSIX) + */ +void ext4_inode_set_modif_time(struct ext4_inode *inode, uint32_t time); + +/**@brief Get time, when i-node was deleted. + * @param inode I-node + * @return Time of the delete action (POSIX) + */ +uint32_t ext4_inode_get_del_time(struct ext4_inode *inode); + +/**@brief Set time, when i-node was deleted. + * @param inode I-node + * @param time Time of the delete action (POSIX) + */ +void ext4_inode_set_del_time(struct ext4_inode *inode, uint32_t time); + +/**@brief Get ID of the i-node owner's group. + * @param inode I-node to load gid from + * @return Group ID of the i-node owner + */ +uint32_t ext4_inode_get_gid(struct ext4_inode *inode); + +/**@brief Set ID to the i-node owner's group. + * @param inode I-node to set gid to + * @param gid Group ID of the i-node owner + */ +void ext4_inode_set_gid(struct ext4_inode *inode, uint32_t gid); + +/**@brief Get number of links to i-node. + * @param inode I-node to load number of links from + * @return Number of links to i-node + */ +uint16_t ext4_inode_get_links_cnt(struct ext4_inode *inode); + +/**@brief Set number of links to i-node. + * @param inode I-node to set number of links to + * @param count Number of links to i-node + */ +void ext4_inode_set_links_cnt(struct ext4_inode *inode, uint16_t cnt); + +/**@brief Get number of 512-bytes blocks used for i-node. + * @param sb Superblock + * @param inode I-node + * @return Number of 512-bytes blocks + */ +uint64_t ext4_inode_get_blocks_count(struct ext4_sblock *sb, + struct ext4_inode *inode); + +/**@brief Set number of 512-bytes blocks used for i-node. + * @param sb Superblock + * @param inode I-node + * @param count Number of 512-bytes blocks + * @return Error code + */ +int ext4_inode_set_blocks_count(struct ext4_sblock *sb, + struct ext4_inode *inode, uint64_t cnt); + +/**@brief Get flags (features) of i-node. + * @param inode I-node to get flags from + * @return Flags (bitmap) + */ +uint32_t ext4_inode_get_flags(struct ext4_inode *inode); + +/**@brief Set flags (features) of i-node. + * @param inode I-node to set flags to + * @param flags Flags to set to i-node + */ +void ext4_inode_set_flags(struct ext4_inode *inode, uint32_t flags); + +/**@brief Get file generation (used by NFS). + * @param inode I-node + * @return File generation + */ +uint32_t ext4_inode_get_generation(struct ext4_inode *inode); + +/**@brief Set file generation (used by NFS). + * @param inode I-node + * @param generation File generation + */ +void ext4_inode_set_generation(struct ext4_inode *inode, uint32_t gen); + +/**@brief Get extra I-node size field. + * @param sb Superblock + * @param inode I-node + * @return extra I-node size + */ +uint16_t ext4_inode_get_extra_isize(struct ext4_sblock *sb, + struct ext4_inode *inode); + +/**@brief Set extra I-node size field. + * @param sb Superblock + * @param inode I-node + * @param size extra I-node size + */ +void ext4_inode_set_extra_isize(struct ext4_sblock *sb, + struct ext4_inode *inode, + uint16_t size); + +/**@brief Get address of block, where are extended attributes located. + * @param inode I-node + * @param sb Superblock + * @return Block address + */ +uint64_t ext4_inode_get_file_acl(struct ext4_inode *inode, + struct ext4_sblock *sb); + +/**@brief Set address of block, where are extended attributes located. + * @param inode I-node + * @param sb Superblock + * @param file_acl Block address + */ +void ext4_inode_set_file_acl(struct ext4_inode *inode, struct ext4_sblock *sb, + uint64_t acl); + +/**@brief Get block address of specified direct block. + * @param inode I-node to load block from + * @param idx Index of logical block + * @return Physical block address + */ +uint32_t ext4_inode_get_direct_block(struct ext4_inode *inode, uint32_t idx); + +/**@brief Set block address of specified direct block. + * @param inode I-node to set block address to + * @param idx Index of logical block + * @param fblock Physical block address + */ +void ext4_inode_set_direct_block(struct ext4_inode *inode, uint32_t idx, + uint32_t block); + +/**@brief Get block address of specified indirect block. + * @param inode I-node to get block address from + * @param idx Index of indirect block + * @return Physical block address + */ +uint32_t ext4_inode_get_indirect_block(struct ext4_inode *inode, uint32_t idx); + +/**@brief Set block address of specified indirect block. + * @param inode I-node to set block address to + * @param idx Index of indirect block + * @param fblock Physical block address + */ +void ext4_inode_set_indirect_block(struct ext4_inode *inode, uint32_t idx, + uint32_t block); + +/**@brief Get device number + * @param inode I-node to get device number from + * @return Device number + */ +uint32_t ext4_inode_get_dev(struct ext4_inode *inode); + +/**@brief Set device number + * @param inode I-node to set device number to + * @param dev Device number + */ +void ext4_inode_set_dev(struct ext4_inode *inode, uint32_t dev); + +/**@brief return the type of i-node + * @param sb Superblock + * @param inode I-node to return the type of + * @return Result of check operation + */ +uint32_t ext4_inode_type(struct ext4_sblock *sb, struct ext4_inode *inode); + +/**@brief Check if i-node has specified type. + * @param sb Superblock + * @param inode I-node to check type of + * @param type Type to check + * @return Result of check operation + */ +bool ext4_inode_is_type(struct ext4_sblock *sb, struct ext4_inode *inode, + uint32_t type); + +/**@brief Check if i-node has specified flag. + * @param inode I-node to check flags of + * @param flag Flag to check + * @return Result of check operation + */ +bool ext4_inode_has_flag(struct ext4_inode *inode, uint32_t f); + +/**@brief Remove specified flag from i-node. + * @param inode I-node to clear flag on + * @param clear_flag Flag to be cleared + */ +void ext4_inode_clear_flag(struct ext4_inode *inode, uint32_t f); + +/**@brief Set specified flag to i-node. + * @param inode I-node to set flag on + * @param set_flag Flag to be set + */ +void ext4_inode_set_flag(struct ext4_inode *inode, uint32_t f); + +/**@brief Get inode checksum(crc32) + * @param sb Superblock + * @param inode I-node to get checksum value from + */ +uint32_t +ext4_inode_get_csum(struct ext4_sblock *sb, struct ext4_inode *inode); + +/**@brief Get inode checksum(crc32) + * @param sb Superblock + * @param inode I-node to get checksum value from + */ +void +ext4_inode_set_csum(struct ext4_sblock *sb, struct ext4_inode *inode, + uint32_t checksum); + +/**@brief Check if i-node can be truncated. + * @param sb Superblock + * @param inode I-node to check + * @return Result of the check operation + */ +bool ext4_inode_can_truncate(struct ext4_sblock *sb, struct ext4_inode *inode); + +/**@brief Get extent header from the root of the extent tree. + * @param inode I-node to get extent header from + * @return Pointer to extent header of the root node + */ +struct ext4_extent_header * +ext4_inode_get_extent_header(struct ext4_inode *inode); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_INODE_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_journal.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_journal.h new file mode 100644 index 0000000..76a4a87 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_journal.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_journal.h + * @brief Journal handle functions + */ + +#ifndef EXT4_JOURNAL_H_ +#define EXT4_JOURNAL_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" +#include "misc/queue.h" +#include "misc/tree.h" + +struct jbd_fs { + struct ext4_blockdev *bdev; + struct ext4_inode_ref inode_ref; + struct jbd_sb sb; + + bool dirty; +}; + +struct jbd_buf { + uint32_t jbd_lba; + struct ext4_block block; + struct jbd_trans *trans; + struct jbd_block_rec *block_rec; + TAILQ_ENTRY(jbd_buf) buf_node; + TAILQ_ENTRY(jbd_buf) dirty_buf_node; +}; + +struct jbd_revoke_rec { + ext4_fsblk_t lba; + RB_ENTRY(jbd_revoke_rec) revoke_node; +}; + +struct jbd_block_rec { + ext4_fsblk_t lba; + struct jbd_trans *trans; + RB_ENTRY(jbd_block_rec) block_rec_node; + LIST_ENTRY(jbd_block_rec) tbrec_node; + TAILQ_HEAD(jbd_buf_dirty, jbd_buf) dirty_buf_queue; +}; + +struct jbd_trans { + uint32_t trans_id; + + uint32_t start_iblock; + int alloc_blocks; + int data_cnt; + uint32_t data_csum; + int written_cnt; + int error; + + struct jbd_journal *journal; + + TAILQ_HEAD(jbd_trans_buf, jbd_buf) buf_queue; + RB_HEAD(jbd_revoke_tree, jbd_revoke_rec) revoke_root; + LIST_HEAD(jbd_trans_block_rec, jbd_block_rec) tbrec_list; + TAILQ_ENTRY(jbd_trans) trans_node; +}; + +struct jbd_journal { + uint32_t first; + uint32_t start; + uint32_t last; + uint32_t trans_id; + uint32_t alloc_trans_id; + + uint32_t block_size; + + TAILQ_HEAD(jbd_cp_queue, jbd_trans) cp_queue; + RB_HEAD(jbd_block, jbd_block_rec) block_rec_root; + + struct jbd_fs *jbd_fs; +}; + +int jbd_get_fs(struct ext4_fs *fs, + struct jbd_fs *jbd_fs); +int jbd_put_fs(struct jbd_fs *jbd_fs); +int jbd_inode_bmap(struct jbd_fs *jbd_fs, + ext4_lblk_t iblock, + ext4_fsblk_t *fblock); +int jbd_recover(struct jbd_fs *jbd_fs); +int jbd_journal_start(struct jbd_fs *jbd_fs, + struct jbd_journal *journal); +int jbd_journal_stop(struct jbd_journal *journal); +struct jbd_trans * +jbd_journal_new_trans(struct jbd_journal *journal); +int jbd_trans_set_block_dirty(struct jbd_trans *trans, + struct ext4_block *block); +int jbd_trans_revoke_block(struct jbd_trans *trans, + ext4_fsblk_t lba); +int jbd_trans_try_revoke_block(struct jbd_trans *trans, + ext4_fsblk_t lba); +void jbd_journal_free_trans(struct jbd_journal *journal, + struct jbd_trans *trans, + bool abort); +int jbd_journal_commit_trans(struct jbd_journal *journal, + struct jbd_trans *trans); +void +jbd_journal_purge_cp_trans(struct jbd_journal *journal, + bool flush, + bool once); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_JOURNAL_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_mbr.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_mbr.h new file mode 100644 index 0000000..858ab02 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_mbr.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_mbr.h + * @brief Master boot record parser + */ + +#ifndef EXT4_MBR_H_ +#define EXT4_MBR_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_blockdev.h" + +/**@brief Master boot record block devices descriptor*/ +struct ext4_mbr_bdevs { + struct ext4_blockdev partitions[4]; +}; + +int ext4_mbr_scan(struct ext4_blockdev *parent, struct ext4_mbr_bdevs *bdevs); + +/**@brief Master boot record partitions*/ +struct ext4_mbr_parts { + + /**@brief Percentage division tab: + * - {50, 20, 10, 20} + * Sum of all 4 elements must be <= 100*/ + uint8_t division[4]; +}; + +int ext4_mbr_write(struct ext4_blockdev *parent, struct ext4_mbr_parts *parts, uint32_t disk_id); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_MBR_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_misc.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_misc.h new file mode 100644 index 0000000..3067d4d --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_misc.h @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_misc.h + * @brief Miscellaneous helpers. + */ + +#ifndef EXT4_MISC_H_ +#define EXT4_MISC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/**************************************************************/ + +#define EXT4_DIV_ROUND_UP(x, y) (((x) + (y) - 1)/(y)) +#define EXT4_ALIGN(x, y) ((y) * EXT4_DIV_ROUND_UP((x), (y))) + +/****************************Endian conversion*****************/ + +static inline uint64_t reorder64(uint64_t n) +{ + return ((n & 0xff) << 56) | + ((n & 0xff00) << 40) | + ((n & 0xff0000) << 24) | + ((n & 0xff000000LL) << 8) | + ((n & 0xff00000000LL) >> 8) | + ((n & 0xff0000000000LL) >> 24) | + ((n & 0xff000000000000LL) >> 40) | + ((n & 0xff00000000000000LL) >> 56); +} + +static inline uint32_t reorder32(uint32_t n) +{ + return ((n & 0xff) << 24) | + ((n & 0xff00) << 8) | + ((n & 0xff0000) >> 8) | + ((n & 0xff000000) >> 24); +} + +static inline uint16_t reorder16(uint16_t n) +{ + return ((n & 0xff) << 8) | + ((n & 0xff00) >> 8); +} + +#ifdef CONFIG_BIG_ENDIAN +#define to_le64(_n) reorder64(_n) +#define to_le32(_n) reorder32(_n) +#define to_le16(_n) reorder16(_n) + +#define to_be64(_n) _n +#define to_be32(_n) _n +#define to_be16(_n) _n + +#else +#define to_le64(_n) _n +#define to_le32(_n) _n +#define to_le16(_n) _n + +#define to_be64(_n) reorder64(_n) +#define to_be32(_n) reorder32(_n) +#define to_be16(_n) reorder16(_n) +#endif + +/****************************Access macros to ext4 structures*****************/ + +#define ext4_get32(s, f) to_le32((s)->f) +#define ext4_get16(s, f) to_le16((s)->f) +#define ext4_get8(s, f) (s)->f + +#define ext4_set32(s, f, v) \ + do { \ + (s)->f = to_le32(v); \ + } while (0) +#define ext4_set16(s, f, v) \ + do { \ + (s)->f = to_le16(v); \ + } while (0) +#define ext4_set8 \ + (s, f, v) do { (s)->f = (v); } \ + while (0) + +/****************************Access macros to jbd2 structures*****************/ + +#define jbd_get32(s, f) to_be32((s)->f) +#define jbd_get16(s, f) to_be16((s)->f) +#define jbd_get8(s, f) (s)->f + +#define jbd_set32(s, f, v) \ + do { \ + (s)->f = to_be32(v); \ + } while (0) +#define jbd_set16(s, f, v) \ + do { \ + (s)->f = to_be16(v); \ + } while (0) +#define jbd_set8 \ + (s, f, v) do { (s)->f = (v); } \ + while (0) + +#ifdef __GNUC__ + #ifndef __unused + #define __unused __attribute__ ((__unused__)) + #endif +#else + #define __unused +#endif + +#ifndef offsetof +#define offsetof(type, field) \ + ((size_t)(&(((type *)0)->field))) +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_MISC_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_mkfs.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_mkfs.h new file mode 100644 index 0000000..16a05f8 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_mkfs.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_mkfs.h + * @brief + */ + +#ifndef EXT4_MKFS_H_ +#define EXT4_MKFS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" + +#include "ext4_blockdev.h" +#include "ext4_fs.h" + +#include +#include + +struct ext4_mkfs_info { + uint64_t len; + uint32_t block_size; + uint32_t blocks_per_group; + uint32_t inodes_per_group; + uint32_t inode_size; + uint32_t inodes; + uint32_t journal_blocks; + uint32_t feat_ro_compat; + uint32_t feat_compat; + uint32_t feat_incompat; + uint32_t bg_desc_reserve_blocks; + uint16_t dsc_size; + uint8_t uuid[UUID_SIZE]; + bool journal; + const char *label; +}; + + +int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info); + +int ext4_mkfs(struct ext4_fs *fs, struct ext4_blockdev *bd, + struct ext4_mkfs_info *info, int fs_type); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_MKFS_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_oflags.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_oflags.h new file mode 100644 index 0000000..4233267 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_oflags.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_oflags.h + * @brief File opening & seeking flags. + */ +#ifndef EXT4_OFLAGS_H_ +#define EXT4_OFLAGS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +/********************************FILE OPEN FLAGS*****************************/ + +#if CONFIG_HAVE_OWN_OFLAGS + + #ifndef O_RDONLY + #define O_RDONLY 00 + #endif + + #ifndef O_WRONLY + #define O_WRONLY 01 + #endif + + #ifndef O_RDWR + #define O_RDWR 02 + #endif + + #ifndef O_CREAT + #define O_CREAT 0100 + #endif + + #ifndef O_EXCL + #define O_EXCL 0200 + #endif + + #ifndef O_TRUNC + #define O_TRUNC 01000 + #endif + + #ifndef O_APPEND + #define O_APPEND 02000 + #endif + +/********************************FILE SEEK FLAGS*****************************/ + + #ifndef SEEK_SET + #define SEEK_SET 0 + #endif + + #ifndef SEEK_CUR + #define SEEK_CUR 1 + #endif + + #ifndef SEEK_END + #define SEEK_END 2 + #endif + +#else + //#include + #include +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_OFLAGS_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_super.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_super.h new file mode 100644 index 0000000..b420225 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_super.h @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_super.c + * @brief Superblock operations. + */ + +#ifndef EXT4_SUPER_H_ +#define EXT4_SUPER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" + +/**@brief Blocks count get stored in superblock. + * @param s superblock descriptor + * @return count of blocks*/ +static inline uint64_t ext4_sb_get_blocks_cnt(struct ext4_sblock *s) +{ + return ((uint64_t)to_le32(s->blocks_count_hi) << 32) | + to_le32(s->blocks_count_lo); +} + +/**@brief Blocks count set in superblock. + * @param s superblock descriptor + * @return count of blocks*/ +static inline void ext4_sb_set_blocks_cnt(struct ext4_sblock *s, uint64_t cnt) +{ + s->blocks_count_lo = to_le32((cnt << 32) >> 32); + s->blocks_count_hi = to_le32(cnt >> 32); +} + +/**@brief Free blocks count get stored in superblock. + * @param s superblock descriptor + * @return free blocks*/ +static inline uint64_t ext4_sb_get_free_blocks_cnt(struct ext4_sblock *s) +{ + return ((uint64_t)to_le32(s->free_blocks_count_hi) << 32) | + to_le32(s->free_blocks_count_lo); +} + +/**@brief Free blocks count set. + * @param s superblock descriptor + * @param cnt new value of free blocks*/ +static inline void ext4_sb_set_free_blocks_cnt(struct ext4_sblock *s, + uint64_t cnt) +{ + s->free_blocks_count_lo = to_le32((cnt << 32) >> 32); + s->free_blocks_count_hi = to_le32(cnt >> 32); +} + +/**@brief Block size get from superblock. + * @param s superblock descriptor + * @return block size in bytes*/ +static inline uint32_t ext4_sb_get_block_size(struct ext4_sblock *s) +{ + return 1024 << to_le32(s->log_block_size); +} + +/**@brief Block group descriptor size. + * @param s superblock descriptor + * @return block group descriptor size in bytes*/ +static inline uint16_t ext4_sb_get_desc_size(struct ext4_sblock *s) +{ + uint16_t size = to_le16(s->desc_size); + + return size < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE + ? EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE + : size; +} + +/*************************Flags and features*********************************/ + +/**@brief Support check of flag. + * @param s superblock descriptor + * @param v flag to check + * @return true if flag is supported*/ +static inline bool ext4_sb_check_flag(struct ext4_sblock *s, uint32_t v) +{ + return to_le32(s->flags) & v; +} + +/**@brief Support check of feature compatible. + * @param s superblock descriptor + * @param v feature to check + * @return true if feature is supported*/ +static inline bool ext4_sb_feature_com(struct ext4_sblock *s, uint32_t v) +{ + return to_le32(s->features_compatible) & v; +} + +/**@brief Support check of feature incompatible. + * @param s superblock descriptor + * @param v feature to check + * @return true if feature is supported*/ +static inline bool ext4_sb_feature_incom(struct ext4_sblock *s, uint32_t v) +{ + return to_le32(s->features_incompatible) & v; +} + +/**@brief Support check of read only flag. + * @param s superblock descriptor + * @param v flag to check + * @return true if flag is supported*/ +static inline bool ext4_sb_feature_ro_com(struct ext4_sblock *s, uint32_t v) +{ + return to_le32(s->features_read_only) & v; +} + +/**@brief Block group to flex group. + * @param s superblock descriptor + * @param block_group block group + * @return flex group id*/ +static inline uint32_t ext4_sb_bg_to_flex(struct ext4_sblock *s, + uint32_t block_group) +{ + return block_group >> to_le32(s->log_groups_per_flex); +} + +/**@brief Flex block group size. + * @param s superblock descriptor + * @return flex bg size*/ +static inline uint32_t ext4_sb_flex_bg_size(struct ext4_sblock *s) +{ + return 1 << to_le32(s->log_groups_per_flex); +} + +/**@brief Return first meta block group id. + * @param s superblock descriptor + * @return first meta_bg id */ +static inline uint32_t ext4_sb_first_meta_bg(struct ext4_sblock *s) +{ + return to_le32(s->first_meta_bg); +} + +/**************************More complex functions****************************/ + +/**@brief Returns a block group count. + * @param s superblock descriptor + * @return count of block groups*/ +uint32_t ext4_block_group_cnt(struct ext4_sblock *s); + +/**@brief Returns block count in block group + * (last block group may have less blocks) + * @param s superblock descriptor + * @param bgid block group id + * @return blocks count*/ +uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid); + +/**@brief Returns inodes count in block group + * (last block group may have less inodes) + * @param s superblock descriptor + * @param bgid block group id + * @return inodes count*/ +uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid); + +/***************************Read/write/check superblock**********************/ + +/**@brief Superblock write. + * @param bdev block device descriptor. + * @param s superblock descriptor + * @return Standard error code */ +int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s); + +/**@brief Superblock read. + * @param bdev block device descriptor. + * @param s superblock descriptor + * @return Standard error code */ +int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s); + +/**@brief Superblock simple validation. + * @param s superblock descriptor + * @return true if OK*/ +bool ext4_sb_check(struct ext4_sblock *s); + +/**@brief Superblock presence in block group. + * @param s superblock descriptor + * @param block_group block group id + * @return true if block group has superblock*/ +bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t block_group); + +/**@brief TODO:*/ +bool ext4_sb_sparse(uint32_t group); + +/**@brief TODO:*/ +uint32_t ext4_bg_num_gdb(struct ext4_sblock *s, uint32_t group); + +/**@brief TODO:*/ +uint32_t ext4_num_base_meta_clusters(struct ext4_sblock *s, + uint32_t block_group); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_SUPER_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_trans.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_trans.h new file mode 100644 index 0000000..e3cb28a --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_trans.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_trans.h + * @brief Transaction handle functions + */ + +#ifndef EXT4_TRANS_H +#define EXT4_TRANS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" + + +/**@brief Mark a buffer dirty and add it to the current transaction. + * @param buf buffer + * @return standard error code*/ +int ext4_trans_set_block_dirty(struct ext4_buf *buf); + +/**@brief Block get function (through cache, don't read). + * jbd_trans_get_access would be called in order to + * get write access to the buffer. + * @param bdev block device descriptor + * @param b block descriptor + * @param lba logical block address + * @return standard error code*/ +int ext4_trans_block_get_noread(struct ext4_blockdev *bdev, + struct ext4_block *b, + uint64_t lba); + +/**@brief Block get function (through cache). + * jbd_trans_get_access would be called in order to + * get write access to the buffer. + * @param bdev block device descriptor + * @param b block descriptor + * @param lba logical block address + * @return standard error code*/ +int ext4_trans_block_get(struct ext4_blockdev *bdev, + struct ext4_block *b, + uint64_t lba); + +/**@brief Try to add block to be revoked to the current transaction. + * @param bdev block device descriptor + * @param lba logical block address + * @return standard error code*/ +int ext4_trans_try_revoke_block(struct ext4_blockdev *bdev, + uint64_t lba); + +#ifdef __cplusplus +} +#endif + +#endif /* EXT4_TRANS_H */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_types.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_types.h new file mode 100644 index 0000000..2fd051e --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_types.h @@ -0,0 +1,844 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_types.h + * @brief Ext4 data structure definitions. + */ + +#ifndef EXT4_TYPES_H_ +#define EXT4_TYPES_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_blockdev.h" +#include "misc/tree.h" + +#include +#include + +/* + * Types of blocks. + */ +typedef uint32_t ext4_lblk_t; +typedef uint64_t ext4_fsblk_t; + + +#define EXT4_CHECKSUM_CRC32C 1 + +#define UUID_SIZE 16 + +#pragma pack(push, 1) + +/* + * Structure of the super block + */ +struct ext4_sblock { + uint32_t inodes_count; /* I-nodes count */ + uint32_t blocks_count_lo; /* Blocks count */ + uint32_t reserved_blocks_count_lo; /* Reserved blocks count */ + uint32_t free_blocks_count_lo; /* Free blocks count */ + uint32_t free_inodes_count; /* Free inodes count */ + uint32_t first_data_block; /* First Data Block */ + uint32_t log_block_size; /* Block size */ + uint32_t log_cluster_size; /* Obsoleted fragment size */ + uint32_t blocks_per_group; /* Number of blocks per group */ + uint32_t frags_per_group; /* Obsoleted fragments per group */ + uint32_t inodes_per_group; /* Number of inodes per group */ + uint32_t mount_time; /* Mount time */ + uint32_t write_time; /* Write time */ + uint16_t mount_count; /* Mount count */ + uint16_t max_mount_count; /* Maximal mount count */ + uint16_t magic; /* Magic signature */ + uint16_t state; /* File system state */ + uint16_t errors; /* Behavior when detecting errors */ + uint16_t minor_rev_level; /* Minor revision level */ + uint32_t last_check_time; /* Time of last check */ + uint32_t check_interval; /* Maximum time between checks */ + uint32_t creator_os; /* Creator OS */ + uint32_t rev_level; /* Revision level */ + uint16_t def_resuid; /* Default uid for reserved blocks */ + uint16_t def_resgid; /* Default gid for reserved blocks */ + + /* Fields for EXT4_DYNAMIC_REV superblocks only. */ + uint32_t first_inode; /* First non-reserved inode */ + uint16_t inode_size; /* Size of inode structure */ + uint16_t block_group_index; /* Block group index of this superblock */ + uint32_t features_compatible; /* Compatible feature set */ + uint32_t features_incompatible; /* Incompatible feature set */ + uint32_t features_read_only; /* Readonly-compatible feature set */ + uint8_t uuid[UUID_SIZE]; /* 128-bit uuid for volume */ + char volume_name[16]; /* Volume name */ + char last_mounted[64]; /* Directory where last mounted */ + uint32_t algorithm_usage_bitmap; /* For compression */ + + /* + * Performance hints. Directory preallocation should only + * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on. + */ + uint8_t s_prealloc_blocks; /* Number of blocks to try to preallocate */ + uint8_t s_prealloc_dir_blocks; /* Number to preallocate for dirs */ + uint16_t s_reserved_gdt_blocks; /* Per group desc for online growth */ + + /* + * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set. + */ + uint8_t journal_uuid[UUID_SIZE]; /* UUID of journal superblock */ + uint32_t journal_inode_number; /* Inode number of journal file */ + uint32_t journal_dev; /* Device number of journal file */ + uint32_t last_orphan; /* Head of list of inodes to delete */ + uint32_t hash_seed[4]; /* HTREE hash seed */ + uint8_t default_hash_version; /* Default hash version to use */ + uint8_t journal_backup_type; + uint16_t desc_size; /* Size of group descriptor */ + uint32_t default_mount_opts; /* Default mount options */ + uint32_t first_meta_bg; /* First metablock block group */ + uint32_t mkfs_time; /* When the filesystem was created */ + uint32_t journal_blocks[17]; /* Backup of the journal inode */ + + /* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */ + uint32_t blocks_count_hi; /* Blocks count */ + uint32_t reserved_blocks_count_hi; /* Reserved blocks count */ + uint32_t free_blocks_count_hi; /* Free blocks count */ + uint16_t min_extra_isize; /* All inodes have at least # bytes */ + uint16_t want_extra_isize; /* New inodes should reserve # bytes */ + uint32_t flags; /* Miscellaneous flags */ + uint16_t raid_stride; /* RAID stride */ + uint16_t mmp_interval; /* # seconds to wait in MMP checking */ + uint64_t mmp_block; /* Block for multi-mount protection */ + uint32_t raid_stripe_width; /* Blocks on all data disks (N * stride) */ + uint8_t log_groups_per_flex; /* FLEX_BG group size */ + uint8_t checksum_type; + uint16_t reserved_pad; + uint64_t kbytes_written; /* Number of lifetime kilobytes written */ + uint32_t snapshot_inum; /* I-node number of active snapshot */ + uint32_t snapshot_id; /* Sequential ID of active snapshot */ + uint64_t + snapshot_r_blocks_count; /* Reserved blocks for active snapshot's + future use */ + uint32_t + snapshot_list; /* I-node number of the head of the on-disk snapshot + list */ + uint32_t error_count; /* Number of file system errors */ + uint32_t first_error_time; /* First time an error happened */ + uint32_t first_error_ino; /* I-node involved in first error */ + uint64_t first_error_block; /* Block involved of first error */ + uint8_t first_error_func[32]; /* Function where the error happened */ + uint32_t first_error_line; /* Line number where error happened */ + uint32_t last_error_time; /* Most recent time of an error */ + uint32_t last_error_ino; /* I-node involved in last error */ + uint32_t last_error_line; /* Line number where error happened */ + uint64_t last_error_block; /* Block involved of last error */ + uint8_t last_error_func[32]; /* Function where the error happened */ + uint8_t mount_opts[64]; + uint32_t usr_quota_inum; /* inode for tracking user quota */ + uint32_t grp_quota_inum; /* inode for tracking group quota */ + uint32_t overhead_clusters; /* overhead blocks/clusters in fs */ + uint32_t backup_bgs[2]; /* groups with sparse_super2 SBs */ + uint8_t encrypt_algos[4]; /* Encryption algorithms in use */ + uint8_t encrypt_pw_salt[16]; /* Salt used for string2key algorithm */ + uint32_t lpf_ino; /* Location of the lost+found inode */ + uint32_t padding[100]; /* Padding to the end of the block */ + uint32_t checksum; /* crc32c(superblock) */ +}; + +#pragma pack(pop) + +#define EXT4_SUPERBLOCK_MAGIC 0xEF53 +#define EXT4_SUPERBLOCK_SIZE 1024 +#define EXT4_SUPERBLOCK_OFFSET 1024 + +#define EXT4_SUPERBLOCK_OS_LINUX 0 +#define EXT4_SUPERBLOCK_OS_HURD 1 + +/* + * Misc. filesystem flags + */ +#define EXT4_SUPERBLOCK_FLAGS_SIGNED_HASH 0x0001 +#define EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH 0x0002 +#define EXT4_SUPERBLOCK_FLAGS_TEST_FILESYS 0x0004 +/* + * Filesystem states + */ +#define EXT4_SUPERBLOCK_STATE_VALID_FS 0x0001 /* Unmounted cleanly */ +#define EXT4_SUPERBLOCK_STATE_ERROR_FS 0x0002 /* Errors detected */ +#define EXT4_SUPERBLOCK_STATE_ORPHAN_FS 0x0004 /* Orphans being recovered */ + +/* + * Behaviour when errors detected + */ +#define EXT4_SUPERBLOCK_ERRORS_CONTINUE 1 /* Continue execution */ +#define EXT4_SUPERBLOCK_ERRORS_RO 2 /* Remount fs read-only */ +#define EXT4_SUPERBLOCK_ERRORS_PANIC 3 /* Panic */ +#define EXT4_SUPERBLOCK_ERRORS_DEFAULT EXT4_ERRORS_CONTINUE + +/* + * Compatible features + */ +#define EXT4_FCOM_DIR_PREALLOC 0x0001 +#define EXT4_FCOM_IMAGIC_INODES 0x0002 +#define EXT4_FCOM_HAS_JOURNAL 0x0004 +#define EXT4_FCOM_EXT_ATTR 0x0008 +#define EXT4_FCOM_RESIZE_INODE 0x0010 +#define EXT4_FCOM_DIR_INDEX 0x0020 + +/* + * Read-only compatible features + */ +#define EXT4_FRO_COM_SPARSE_SUPER 0x0001 +#define EXT4_FRO_COM_LARGE_FILE 0x0002 +#define EXT4_FRO_COM_BTREE_DIR 0x0004 +#define EXT4_FRO_COM_HUGE_FILE 0x0008 +#define EXT4_FRO_COM_GDT_CSUM 0x0010 +#define EXT4_FRO_COM_DIR_NLINK 0x0020 +#define EXT4_FRO_COM_EXTRA_ISIZE 0x0040 +#define EXT4_FRO_COM_QUOTA 0x0100 +#define EXT4_FRO_COM_BIGALLOC 0x0200 +#define EXT4_FRO_COM_METADATA_CSUM 0x0400 + +/* + * Incompatible features + */ +#define EXT4_FINCOM_COMPRESSION 0x0001 +#define EXT4_FINCOM_FILETYPE 0x0002 +#define EXT4_FINCOM_RECOVER 0x0004 /* Needs recovery */ +#define EXT4_FINCOM_JOURNAL_DEV 0x0008 /* Journal device */ +#define EXT4_FINCOM_META_BG 0x0010 +#define EXT4_FINCOM_EXTENTS 0x0040 /* extents support */ +#define EXT4_FINCOM_64BIT 0x0080 +#define EXT4_FINCOM_MMP 0x0100 +#define EXT4_FINCOM_FLEX_BG 0x0200 +#define EXT4_FINCOM_EA_INODE 0x0400 /* EA in inode */ +#define EXT4_FINCOM_DIRDATA 0x1000 /* data in dirent */ +#define EXT4_FINCOM_BG_USE_META_CSUM 0x2000 /* use crc32c for bg */ +#define EXT4_FINCOM_LARGEDIR 0x4000 /* >2GB or 3-lvl htree */ +#define EXT4_FINCOM_INLINE_DATA 0x8000 /* data in inode */ + +/* + * EXT2 supported feature set + */ +#define EXT2_SUPPORTED_FCOM 0x0000 + +#define EXT2_SUPPORTED_FINCOM \ + (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG) + +#define EXT2_SUPPORTED_FRO_COM \ + (EXT4_FRO_COM_SPARSE_SUPER | \ + EXT4_FRO_COM_LARGE_FILE) + +/* + * EXT3 supported feature set + */ +#define EXT3_SUPPORTED_FCOM (EXT4_FCOM_DIR_INDEX) + +#define EXT3_SUPPORTED_FINCOM \ + (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG) + +#define EXT3_SUPPORTED_FRO_COM \ + (EXT4_FRO_COM_SPARSE_SUPER | EXT4_FRO_COM_LARGE_FILE) + +/* + * EXT4 supported feature set + */ +#define EXT4_SUPPORTED_FCOM (EXT4_FCOM_DIR_INDEX) + +#define EXT4_SUPPORTED_FINCOM \ + (EXT4_FINCOM_FILETYPE | EXT4_FINCOM_META_BG | \ + EXT4_FINCOM_EXTENTS | EXT4_FINCOM_FLEX_BG | \ + EXT4_FINCOM_64BIT) + +#define EXT4_SUPPORTED_FRO_COM \ + (EXT4_FRO_COM_SPARSE_SUPER | \ + EXT4_FRO_COM_METADATA_CSUM | \ + EXT4_FRO_COM_LARGE_FILE | EXT4_FRO_COM_GDT_CSUM | \ + EXT4_FRO_COM_DIR_NLINK | \ + EXT4_FRO_COM_EXTRA_ISIZE | EXT4_FRO_COM_HUGE_FILE) + +/*Ignored features: + * RECOVER - journaling in lwext4 is not supported + * (probably won't be ever...) + * MMP - multi-mout protection (impossible scenario) + * */ +#define EXT_FINCOM_IGNORED \ + EXT4_FINCOM_RECOVER | EXT4_FINCOM_MMP + +#if 0 +/*TODO: Features incompatible to implement*/ +#define EXT4_SUPPORTED_FINCOM + (EXT4_FINCOM_INLINE_DATA) + +/*TODO: Features read only to implement*/ +#define EXT4_SUPPORTED_FRO_COM + EXT4_FRO_COM_BIGALLOC |\ + EXT4_FRO_COM_QUOTA) +#endif + + +/* Inode table/bitmap not in use */ +#define EXT4_BLOCK_GROUP_INODE_UNINIT 0x0001 +/* Block bitmap not in use */ +#define EXT4_BLOCK_GROUP_BLOCK_UNINIT 0x0002 +/* On-disk itable initialized to zero */ +#define EXT4_BLOCK_GROUP_ITABLE_ZEROED 0x0004 + +/* + * Structure of a blocks group descriptor + */ +struct ext4_bgroup { + uint32_t block_bitmap_lo; /* Blocks bitmap block */ + uint32_t inode_bitmap_lo; /* Inodes bitmap block */ + uint32_t inode_table_first_block_lo; /* Inodes table block */ + uint16_t free_blocks_count_lo; /* Free blocks count */ + uint16_t free_inodes_count_lo; /* Free inodes count */ + uint16_t used_dirs_count_lo; /* Directories count */ + uint16_t flags; /* EXT4_BG_flags (INODE_UNINIT, etc) */ + uint32_t exclude_bitmap_lo; /* Exclude bitmap for snapshots */ + uint16_t block_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+bbitmap) LE */ + uint16_t inode_bitmap_csum_lo; /* crc32c(s_uuid+grp_num+ibitmap) LE */ + uint16_t itable_unused_lo; /* Unused inodes count */ + uint16_t checksum; /* crc16(sb_uuid+group+desc) */ + + uint32_t block_bitmap_hi; /* Blocks bitmap block MSB */ + uint32_t inode_bitmap_hi; /* I-nodes bitmap block MSB */ + uint32_t inode_table_first_block_hi; /* I-nodes table block MSB */ + uint16_t free_blocks_count_hi; /* Free blocks count MSB */ + uint16_t free_inodes_count_hi; /* Free i-nodes count MSB */ + uint16_t used_dirs_count_hi; /* Directories count MSB */ + uint16_t itable_unused_hi; /* Unused inodes count MSB */ + uint32_t exclude_bitmap_hi; /* Exclude bitmap block MSB */ + uint16_t block_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+bbitmap) BE */ + uint16_t inode_bitmap_csum_hi; /* crc32c(s_uuid+grp_num+ibitmap) BE */ + uint32_t reserved; /* Padding */ +}; + + +#define EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE 32 +#define EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE 64 + +#define EXT4_MIN_BLOCK_SIZE 1024 /* 1 KiB */ +#define EXT4_MAX_BLOCK_SIZE 65536 /* 64 KiB */ +#define EXT4_REV0_INODE_SIZE 128 + +#define EXT4_INODE_BLOCK_SIZE 512 + +#define EXT4_INODE_DIRECT_BLOCK_COUNT 12 +#define EXT4_INODE_INDIRECT_BLOCK EXT4_INODE_DIRECT_BLOCK_COUNT +#define EXT4_INODE_DOUBLE_INDIRECT_BLOCK (EXT4_INODE_INDIRECT_BLOCK + 1) +#define EXT4_INODE_TRIPPLE_INDIRECT_BLOCK (EXT4_INODE_DOUBLE_INDIRECT_BLOCK + 1) +#define EXT4_INODE_BLOCKS (EXT4_INODE_TRIPPLE_INDIRECT_BLOCK + 1) +#define EXT4_INODE_INDIRECT_BLOCK_COUNT \ + (EXT4_INODE_BLOCKS - EXT4_INODE_DIRECT_BLOCK_COUNT) + +#pragma pack(push, 1) + +/* + * Structure of an inode on the disk + */ +struct ext4_inode { + uint16_t mode; /* File mode */ + uint16_t uid; /* Low 16 bits of owner uid */ + uint32_t size_lo; /* Size in bytes */ + uint32_t access_time; /* Access time */ + uint32_t change_inode_time; /* I-node change time */ + uint32_t modification_time; /* Modification time */ + uint32_t deletion_time; /* Deletion time */ + uint16_t gid; /* Low 16 bits of group id */ + uint16_t links_count; /* Links count */ + uint32_t blocks_count_lo; /* Blocks count */ + uint32_t flags; /* File flags */ + uint32_t unused_osd1; /* OS dependent - not used in HelenOS */ + uint32_t blocks[EXT4_INODE_BLOCKS]; /* Pointers to blocks */ + uint32_t generation; /* File version (for NFS) */ + uint32_t file_acl_lo; /* File ACL */ + uint32_t size_hi; + uint32_t obso_faddr; /* Obsoleted fragment address */ + + union { + struct { + uint16_t blocks_high; + uint16_t file_acl_high; + uint16_t uid_high; + uint16_t gid_high; + uint16_t checksum_lo; /* crc32c(uuid+inum+inode) LE */ + uint16_t reserved2; + } linux2; + struct { + uint16_t reserved1; + uint16_t mode_high; + uint16_t uid_high; + uint16_t gid_high; + uint32_t author; + } hurd2; + } osd2; + + uint16_t extra_isize; + uint16_t checksum_hi; /* crc32c(uuid+inum+inode) BE */ + uint32_t ctime_extra; /* Extra change time (nsec << 2 | epoch) */ + uint32_t mtime_extra; /* Extra Modification time (nsec << 2 | epoch) */ + uint32_t atime_extra; /* Extra Access time (nsec << 2 | epoch) */ + uint32_t crtime; /* File creation time */ + uint32_t + crtime_extra; /* Extra file creation time (nsec << 2 | epoch) */ + uint32_t version_hi; /* High 32 bits for 64-bit version */ +}; + +#pragma pack(pop) + +#define EXT4_INODE_MODE_FIFO 0x1000 +#define EXT4_INODE_MODE_CHARDEV 0x2000 +#define EXT4_INODE_MODE_DIRECTORY 0x4000 +#define EXT4_INODE_MODE_BLOCKDEV 0x6000 +#define EXT4_INODE_MODE_FILE 0x8000 +#define EXT4_INODE_MODE_SOFTLINK 0xA000 +#define EXT4_INODE_MODE_SOCKET 0xC000 +#define EXT4_INODE_MODE_TYPE_MASK 0xF000 + +/* + * Inode flags + */ +#define EXT4_INODE_FLAG_SECRM 0x00000001 /* Secure deletion */ +#define EXT4_INODE_FLAG_UNRM 0x00000002 /* Undelete */ +#define EXT4_INODE_FLAG_COMPR 0x00000004 /* Compress file */ +#define EXT4_INODE_FLAG_SYNC 0x00000008 /* Synchronous updates */ +#define EXT4_INODE_FLAG_IMMUTABLE 0x00000010 /* Immutable file */ +#define EXT4_INODE_FLAG_APPEND 0x00000020 /* writes to file may only append */ +#define EXT4_INODE_FLAG_NODUMP 0x00000040 /* do not dump file */ +#define EXT4_INODE_FLAG_NOATIME 0x00000080 /* do not update atime */ + +/* Compression flags */ +#define EXT4_INODE_FLAG_DIRTY 0x00000100 +#define EXT4_INODE_FLAG_COMPRBLK \ + 0x00000200 /* One or more compressed clusters */ +#define EXT4_INODE_FLAG_NOCOMPR 0x00000400 /* Don't compress */ +#define EXT4_INODE_FLAG_ECOMPR 0x00000800 /* Compression error */ + +#define EXT4_INODE_FLAG_INDEX 0x00001000 /* hash-indexed directory */ +#define EXT4_INODE_FLAG_IMAGIC 0x00002000 /* AFS directory */ +#define EXT4_INODE_FLAG_JOURNAL_DATA \ + 0x00004000 /* File data should be journaled */ +#define EXT4_INODE_FLAG_NOTAIL 0x00008000 /* File tail should not be merged */ +#define EXT4_INODE_FLAG_DIRSYNC \ + 0x00010000 /* Dirsync behaviour (directories only) */ +#define EXT4_INODE_FLAG_TOPDIR 0x00020000 /* Top of directory hierarchies */ +#define EXT4_INODE_FLAG_HUGE_FILE 0x00040000 /* Set to each huge file */ +#define EXT4_INODE_FLAG_EXTENTS 0x00080000 /* Inode uses extents */ +#define EXT4_INODE_FLAG_EA_INODE 0x00200000 /* Inode used for large EA */ +#define EXT4_INODE_FLAG_EOFBLOCKS 0x00400000 /* Blocks allocated beyond EOF */ +#define EXT4_INODE_FLAG_RESERVED 0x80000000 /* reserved for ext4 lib */ + +#define EXT4_INODE_ROOT_INDEX 2 + + +#define EXT4_DIRECTORY_FILENAME_LEN 255 + +/**@brief Directory entry types. */ +enum { EXT4_DE_UNKNOWN = 0, + EXT4_DE_REG_FILE, + EXT4_DE_DIR, + EXT4_DE_CHRDEV, + EXT4_DE_BLKDEV, + EXT4_DE_FIFO, + EXT4_DE_SOCK, + EXT4_DE_SYMLINK }; + +#define EXT4_DIRENTRY_DIR_CSUM 0xDE + +#pragma pack(push, 1) + +union ext4_dir_en_internal { + uint8_t name_length_high; /* Higher 8 bits of name length */ + uint8_t inode_type; /* Type of referenced inode (in rev >= 0.5) */ +}; + +/** + * Linked list directory entry structure + */ +struct ext4_dir_en { + uint32_t inode; /* I-node for the entry */ + uint16_t entry_len; /* Distance to the next directory entry */ + uint8_t name_len; /* Lower 8 bits of name length */ + + union ext4_dir_en_internal in; + uint8_t name[]; /* Entry name */ +}; + +/* Structures for indexed directory */ + +struct ext4_dir_idx_climit { + uint16_t limit; + uint16_t count; +}; + +struct ext4_dir_idx_dot_en { + uint32_t inode; + uint16_t entry_length; + uint8_t name_length; + uint8_t inode_type; + uint8_t name[4]; +}; + +struct ext4_dir_idx_rinfo { + uint32_t reserved_zero; + uint8_t hash_version; + uint8_t info_length; + uint8_t indirect_levels; + uint8_t unused_flags; +}; + +struct ext4_dir_idx_entry { + uint32_t hash; + uint32_t block; +}; + +struct ext4_dir_idx_root { + struct ext4_dir_idx_dot_en dots[2]; + struct ext4_dir_idx_rinfo info; + struct ext4_dir_idx_entry en[]; +}; + +struct ext4_fake_dir_entry { + uint32_t inode; + uint16_t entry_length; + uint8_t name_length; + uint8_t inode_type; +}; + +struct ext4_dir_idx_node { + struct ext4_fake_dir_entry fake; + struct ext4_dir_idx_entry entries[]; +}; + +/* + * This goes at the end of each htree block. + */ +struct ext4_dir_idx_tail { + uint32_t reserved; + uint32_t checksum; /* crc32c(uuid+inum+dirblock) */ +}; + +/* + * This is a bogus directory entry at the end of each leaf block that + * records checksums. + */ +struct ext4_dir_entry_tail { + uint32_t reserved_zero1; /* Pretend to be unused */ + uint16_t rec_len; /* 12 */ + uint8_t reserved_zero2; /* Zero name length */ + uint8_t reserved_ft; /* 0xDE, fake file type */ + uint32_t checksum; /* crc32c(uuid+inum+dirblock) */ +}; + +#pragma pack(pop) + +#define EXT4_DIRENT_TAIL(block, blocksize) \ + ((struct ext4_dir_entry_tail *)(((char *)(block)) + ((blocksize) - \ + sizeof(struct ext4_dir_entry_tail)))) + +#define EXT4_ERR_BAD_DX_DIR (-25000) + +#define EXT4_LINK_MAX 65000 + +#define EXT4_BAD_INO 1 +#define EXT4_ROOT_INO 2 +#define EXT4_BOOT_LOADER_INO 5 +#define EXT4_UNDEL_DIR_INO 6 +#define EXT4_RESIZE_INO 7 +#define EXT4_JOURNAL_INO 8 + +#define EXT4_GOOD_OLD_FIRST_INO 11 +#define EXT_MAX_BLOCKS (ext4_lblk_t) (-1) +#define IN_RANGE(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1) + + +/******************************************************************************/ + +/* EXT3 HTree directory indexing */ +#define EXT2_HTREE_LEGACY 0 +#define EXT2_HTREE_HALF_MD4 1 +#define EXT2_HTREE_TEA 2 +#define EXT2_HTREE_LEGACY_UNSIGNED 3 +#define EXT2_HTREE_HALF_MD4_UNSIGNED 4 +#define EXT2_HTREE_TEA_UNSIGNED 5 + +#define EXT2_HTREE_EOF 0x7FFFFFFFUL + +#define EXT4_GOOD_OLD_INODE_SIZE 128 + +/*****************************************************************************/ + +/* + * JBD stores integers in big endian. + */ + +#define JBD_MAGIC_NUMBER 0xc03b3998U /* The first 4 bytes of /dev/random! */ + +/* + * Descriptor block types: + */ + +#define JBD_DESCRIPTOR_BLOCK 1 +#define JBD_COMMIT_BLOCK 2 +#define JBD_SUPERBLOCK 3 +#define JBD_SUPERBLOCK_V2 4 +#define JBD_REVOKE_BLOCK 5 + +#pragma pack(push, 1) + +/* + * Standard header for all descriptor blocks: + */ +struct jbd_bhdr { + uint32_t magic; + uint32_t blocktype; + uint32_t sequence; +}; + +#pragma pack(pop) + +/* + * Checksum types. + */ +#define JBD_CRC32_CHKSUM 1 +#define JBD_MD5_CHKSUM 2 +#define JBD_SHA1_CHKSUM 3 +#define JBD_CRC32C_CHKSUM 4 + +#define JBD_CRC32_CHKSUM_SIZE 4 + +#define JBD_CHECKSUM_BYTES (32 / sizeof(uint32_t)) + +#pragma pack(push, 1) + +/* + * Commit block header for storing transactional checksums: + * + * NOTE: If FEATURE_COMPAT_CHECKSUM (checksum v1) is set, the h_chksum* + * fields are used to store a checksum of the descriptor and data blocks. + * + * If FEATURE_INCOMPAT_CSUM_V2 (checksum v2) is set, then the h_chksum + * field is used to store crc32c(uuid+commit_block). Each journal metadata + * block gets its own checksum, and data block checksums are stored in + * journal_block_tag (in the descriptor). The other h_chksum* fields are + * not used. + * + * If FEATURE_INCOMPAT_CSUM_V3 is set, the descriptor block uses + * journal_block_tag3_t to store a full 32-bit checksum. Everything else + * is the same as v2. + * + * Checksum v1, v2, and v3 are mutually exclusive features. + */ + +struct jbd_commit_header { + struct jbd_bhdr header; + uint8_t chksum_type; + uint8_t chksum_size; + uint8_t padding[2]; + uint32_t chksum[JBD_CHECKSUM_BYTES]; + uint64_t commit_sec; + uint32_t commit_nsec; +}; + +/* + * The block tag: used to describe a single buffer in the journal + */ +struct jbd_block_tag3 { + uint32_t blocknr; /* The on-disk block number */ + uint32_t flags; /* See below */ + uint32_t blocknr_high; /* most-significant high 32bits. */ + uint32_t checksum; /* crc32c(uuid+seq+block) */ +}; + +struct jbd_block_tag { + uint32_t blocknr; /* The on-disk block number */ + uint16_t checksum; /* truncated crc32c(uuid+seq+block) */ + uint16_t flags; /* See below */ + uint32_t blocknr_high; /* most-significant high 32bits. */ +}; + +#pragma pack(pop) + +/* Definitions for the journal tag flags word: */ +#define JBD_FLAG_ESCAPE 1 /* on-disk block is escaped */ +#define JBD_FLAG_SAME_UUID 2 /* block has same uuid as previous */ +#define JBD_FLAG_DELETED 4 /* block deleted by this transaction */ +#define JBD_FLAG_LAST_TAG 8 /* last tag in this descriptor block */ + +#pragma pack(push, 1) + +/* Tail of descriptor block, for checksumming */ +struct jbd_block_tail { + uint32_t checksum; +}; + +/* + * The revoke descriptor: used on disk to describe a series of blocks to + * be revoked from the log + */ +struct jbd_revoke_header { + struct jbd_bhdr header; + uint32_t count; /* Count of bytes used in the block */ +}; + +/* Tail of revoke block, for checksumming */ +struct jbd_revoke_tail { + uint32_t checksum; +}; + +#pragma pack(pop) + +#define JBD_USERS_MAX 48 +#define JBD_USERS_SIZE (UUID_SIZE * JBD_USERS_MAX) + +#pragma pack(push, 1) + +/* + * The journal superblock. All fields are in big-endian byte order. + */ +struct jbd_sb { +/* 0x0000 */ + struct jbd_bhdr header; + +/* 0x000C */ + /* Static information describing the journal */ + uint32_t blocksize; /* journal device blocksize */ + uint32_t maxlen; /* total blocks in journal file */ + uint32_t first; /* first block of log information */ + +/* 0x0018 */ + /* Dynamic information describing the current state of the log */ + uint32_t sequence; /* first commit ID expected in log */ + uint32_t start; /* blocknr of start of log */ + +/* 0x0020 */ + /* Error value, as set by journal_abort(). */ + int32_t error_val; + +/* 0x0024 */ + /* Remaining fields are only valid in a version-2 superblock */ + uint32_t feature_compat; /* compatible feature set */ + uint32_t feature_incompat; /* incompatible feature set */ + uint32_t feature_ro_compat; /* readonly-compatible feature set */ +/* 0x0030 */ + uint8_t uuid[UUID_SIZE]; /* 128-bit uuid for journal */ + +/* 0x0040 */ + uint32_t nr_users; /* Nr of filesystems sharing log */ + + uint32_t dynsuper; /* Blocknr of dynamic superblock copy*/ + +/* 0x0048 */ + uint32_t max_transaction; /* Limit of journal blocks per trans.*/ + uint32_t max_trandata; /* Limit of data blocks per trans. */ + +/* 0x0050 */ + uint8_t checksum_type; /* checksum type */ + uint8_t padding2[3]; + uint32_t padding[42]; + uint32_t checksum; /* crc32c(superblock) */ + +/* 0x0100 */ + uint8_t users[JBD_USERS_SIZE]; /* ids of all fs'es sharing the log */ + +/* 0x0400 */ +}; + +#pragma pack(pop) + +#define JBD_SUPERBLOCK_SIZE sizeof(struct jbd_sb) + +#define JBD_HAS_COMPAT_FEATURE(jsb,mask) \ + ((jsb)->header.blocktype >= to_be32(2) && \ + ((jsb)->feature_compat & to_be32((mask)))) +#define JBD_HAS_RO_COMPAT_FEATURE(jsb,mask) \ + ((jsb)->header.blocktype >= to_be32(2) && \ + ((jsb)->feature_ro_compat & to_be32((mask)))) +#define JBD_HAS_INCOMPAT_FEATURE(jsb,mask) \ + ((jsb)->header.blocktype >= to_be32(2) && \ + ((jsb)->feature_incompat & to_be32((mask)))) + +#define JBD_FEATURE_COMPAT_CHECKSUM 0x00000001 + +#define JBD_FEATURE_INCOMPAT_REVOKE 0x00000001 +#define JBD_FEATURE_INCOMPAT_64BIT 0x00000002 +#define JBD_FEATURE_INCOMPAT_ASYNC_COMMIT 0x00000004 +#define JBD_FEATURE_INCOMPAT_CSUM_V2 0x00000008 +#define JBD_FEATURE_INCOMPAT_CSUM_V3 0x00000010 + +/* Features known to this kernel version: */ +#define JBD_KNOWN_COMPAT_FEATURES 0 +#define JBD_KNOWN_ROCOMPAT_FEATURES 0 +#define JBD_KNOWN_INCOMPAT_FEATURES (JBD_FEATURE_INCOMPAT_REVOKE|\ + JBD_FEATURE_INCOMPAT_ASYNC_COMMIT|\ + JBD_FEATURE_INCOMPAT_64BIT|\ + JBD_FEATURE_INCOMPAT_CSUM_V2|\ + JBD_FEATURE_INCOMPAT_CSUM_V3) + +/*****************************************************************************/ + +#define EXT4_CRC32_INIT (0xFFFFFFFFUL) + +/*****************************************************************************/ + +#ifdef __cplusplus +} +#endif + + +#if CONFIG_USE_USER_MALLOC + +#define ext4_malloc ext4_user_malloc +#define ext4_calloc ext4_user_calloc +#define ext4_realloc ext4_user_realloc +#define ext4_free ext4_user_free + +#else + +#define ext4_malloc malloc +#define ext4_calloc calloc +#define ext4_realloc realloc +#define ext4_free free + +#endif + + +#endif /* EXT4_TYPES_H_ */ + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/ext4_xattr.h b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_xattr.h new file mode 100644 index 0000000..227b3a6 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/ext4_xattr.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com) + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_xattr.h + * @brief Extended Attribute manipulation. + */ + +#ifndef EXT4_XATTR_H_ +#define EXT4_XATTR_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_inode.h" + +struct ext4_xattr_info { + uint8_t name_index; + const char *name; + size_t name_len; + const void *value; + size_t value_len; +}; + +struct ext4_xattr_list_entry { + uint8_t name_index; + char *name; + size_t name_len; + struct ext4_xattr_list_entry *next; +}; + +struct ext4_xattr_search { + /* The first entry in the buffer */ + struct ext4_xattr_entry *first; + + /* The address of the buffer */ + void *base; + + /* The first inaccessible address */ + void *end; + + /* The current entry pointer */ + struct ext4_xattr_entry *here; + + /* Entry not found */ + bool not_found; +}; + +const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len, + uint8_t *name_index, size_t *name_len, + bool *found); + +const char *ext4_get_xattr_name_prefix(uint8_t name_index, + size_t *ret_prefix_len); + +int ext4_xattr_list(struct ext4_inode_ref *inode_ref, + struct ext4_xattr_list_entry *list, size_t *list_len); + +int ext4_xattr_get(struct ext4_inode_ref *inode_ref, uint8_t name_index, + const char *name, size_t name_len, void *buf, size_t buf_len, + size_t *data_len); + +int ext4_xattr_remove(struct ext4_inode_ref *inode_ref, uint8_t name_index, + const char *name, size_t name_len); + +int ext4_xattr_set(struct ext4_inode_ref *inode_ref, uint8_t name_index, + const char *name, size_t name_len, const void *value, + size_t value_len); + +#ifdef __cplusplus +} +#endif + +#endif +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/generated/ext4_config.h b/code/drivers/fs/ext4/third_party/lwext4/include/generated/ext4_config.h new file mode 100644 index 0000000..433ea0e --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/generated/ext4_config.h @@ -0,0 +1,5 @@ +#define CONFIG_USE_DEFAULT_CONFIG 0 +#define CONFIG_HAVE_OWN_OFLAGS 0 +#define CONFIG_HAVE_OWN_ERRNO 0 +#define CONFIG_HAVE_OWN_ASSERT 0 +#define CONFIG_BLOCK_DEV_CACHE_SIZE 16 diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/getopt.h b/code/drivers/fs/ext4/third_party/lwext4/include/getopt.h new file mode 100644 index 0000000..a07ffd2 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/getopt.h @@ -0,0 +1,653 @@ +#ifndef __GETOPT_H__ +/** + * DISCLAIMER + * This file is part of the mingw-w64 runtime package. + * + * The mingw-w64 runtime package and its code is distributed in the hope that it + * will be useful but WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESSED OR + * IMPLIED ARE HEREBY DISCLAIMED. This includes but is not limited to + * warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + /* + * Copyright (c) 2002 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dieter Baron and Thomas Klausner. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#pragma warning(disable:4996) + +#define __GETOPT_H__ + +/* All the headers include this file. */ +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ + +#ifdef REPLACE_GETOPT +int opterr = 1; /* if error message should be printed */ +int optind = 1; /* index into parent argv vector */ +int optopt = '?'; /* character checked for validity */ +#undef optreset /* see getopt.h */ +#define optreset __mingw_optreset +int optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ +#endif + +//extern int optind; /* index of first non-option in argv */ +//extern int optopt; /* single option character, as parsed */ +//extern int opterr; /* flag to enable built-in diagnostics... */ +// /* (user may set to zero, to suppress) */ +// +//extern char *optarg; /* pointer to argument of current option */ + +#define PRINT_ERROR ((opterr) && (*options != ':')) + +#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ +#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ +#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ + +/* return values */ +#define BADCH (int)'?' +#define BADARG ((*options == ':') ? (int)':' : (int)'?') +#define INORDER (int)1 + +#ifndef __CYGWIN__ +#define __progname __argv[0] +#else +extern char __declspec(dllimport) *__progname; +#endif + +#ifdef __CYGWIN__ +static char EMSG[] = ""; +#else +#define EMSG "" +#endif + +static int getopt_internal(int, char * const *, const char *, + const struct option *, int *, int); +static int parse_long_options(char * const *, const char *, + const struct option *, int *, int); +static int gcd(int, int); +static void permute_args(int, int, int, char * const *); + +static char *place = EMSG; /* option letter processing */ + +/* XXX: set optreset to 1 rather than these two */ +static int nonopt_start = -1; /* first non option argument (for permute) */ +static int nonopt_end = -1; /* first option after non options (for permute) */ + +/* Error messages */ +static const char recargchar[] = "option requires an argument -- %c"; +static const char recargstring[] = "option requires an argument -- %s"; +static const char ambig[] = "ambiguous option -- %.*s"; +static const char noarg[] = "option doesn't take an argument -- %.*s"; +static const char illoptchar[] = "unknown option -- %c"; +static const char illoptstring[] = "unknown option -- %s"; + +static void +_vwarnx(const char *fmt,va_list ap) +{ + (void)fprintf(stderr,"%s: ",__progname); + if (fmt != NULL) + (void)vfprintf(stderr,fmt,ap); + (void)fprintf(stderr,"\n"); +} + +static void +warnx(const char *fmt,...) +{ + va_list ap; + va_start(ap,fmt); + _vwarnx(fmt,ap); + va_end(ap); +} + +/* + * Compute the greatest common divisor of a and b. + */ +static int +gcd(int a, int b) +{ + int c; + + c = a % b; + while (c != 0) { + a = b; + b = c; + c = a % b; + } + + return (b); +} + +/* + * Exchange the block from nonopt_start to nonopt_end with the block + * from nonopt_end to opt_end (keeping the same order of arguments + * in each block). + */ +static void +permute_args(int panonopt_start, int panonopt_end, int opt_end, + char * const *nargv) +{ + int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; + char *swap; + + /* + * compute lengths of blocks and number and size of cycles + */ + nnonopts = panonopt_end - panonopt_start; + nopts = opt_end - panonopt_end; + ncycle = gcd(nnonopts, nopts); + cyclelen = (opt_end - panonopt_start) / ncycle; + + for (i = 0; i < ncycle; i++) { + cstart = panonopt_end+i; + pos = cstart; + for (j = 0; j < cyclelen; j++) { + if (pos >= panonopt_end) + pos -= nnonopts; + else + pos += nopts; + swap = nargv[pos]; + /* LINTED const cast */ + ((char **) nargv)[pos] = nargv[cstart]; + /* LINTED const cast */ + ((char **)nargv)[cstart] = swap; + } + } +} + +#ifdef REPLACE_GETOPT +/* + * getopt -- + * Parse argc/argv argument vector. + * + * [eventually this will replace the BSD getopt] + */ +int +getopt(int nargc, char * const *nargv, const char *options) +{ + + /* + * We don't pass FLAG_PERMUTE to getopt_internal() since + * the BSD getopt(3) (unlike GNU) has never done this. + * + * Furthermore, since many privileged programs call getopt() + * before dropping privileges it makes sense to keep things + * as simple (and bug-free) as possible. + */ + return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); +} +#endif /* REPLACE_GETOPT */ + +//extern int getopt(int nargc, char * const *nargv, const char *options); + +#ifdef _BSD_SOURCE +/* + * BSD adds the non-standard `optreset' feature, for reinitialisation + * of `getopt' parsing. We support this feature, for applications which + * proclaim their BSD heritage, before including this header; however, + * to maintain portability, developers are advised to avoid it. + */ +# define optreset __mingw_optreset +extern int optreset; +#endif +#ifdef __cplusplus +} +#endif +/* + * POSIX requires the `getopt' API to be specified in `unistd.h'; + * thus, `unistd.h' includes this header. However, we do not want + * to expose the `getopt_long' or `getopt_long_only' APIs, when + * included in this manner. Thus, close the standard __GETOPT_H__ + * declarations block, and open an additional __GETOPT_LONG_H__ + * specific block, only when *not* __UNISTD_H_SOURCED__, in which + * to declare the extended API. + */ +#endif /* !defined(__GETOPT_H__) */ + +#if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) +#define __GETOPT_LONG_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +struct option /* specification for a long form option... */ +{ + const char *name; /* option name, without leading hyphens */ + int has_arg; /* does it take an argument? */ + int *flag; /* where to save its status, or NULL */ + int val; /* its associated status value */ +}; + +enum /* permitted values for its `has_arg' field... */ +{ + no_argument = 0, /* option never takes an argument */ + required_argument, /* option always requires an argument */ + optional_argument /* option may take an argument */ +}; + +/* + * parse_long_options -- + * Parse long options in argc/argv argument vector. + * Returns -1 if short_too is set and the option does not match long_options. + */ +static int +parse_long_options(char * const *nargv, const char *options, + const struct option *long_options, int *idx, int short_too) +{ + char *current_argv, *has_equal; + size_t current_argv_len; + int i, ambiguous, match; + +#define IDENTICAL_INTERPRETATION(_x, _y) \ + (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \ + long_options[(_x)].flag == long_options[(_y)].flag && \ + long_options[(_x)].val == long_options[(_y)].val) + + current_argv = place; + match = -1; + ambiguous = 0; + + optind++; + + if ((has_equal = strchr(current_argv, '=')) != NULL) { + /* argument found (--option=arg) */ + current_argv_len = has_equal - current_argv; + has_equal++; + } else + current_argv_len = strlen(current_argv); + + for (i = 0; long_options[i].name; i++) { + /* find matching long option */ + if (strncmp(current_argv, long_options[i].name, + current_argv_len)) + continue; + + if (strlen(long_options[i].name) == current_argv_len) { + /* exact match */ + match = i; + ambiguous = 0; + break; + } + /* + * If this is a known short option, don't allow + * a partial match of a single character. + */ + if (short_too && current_argv_len == 1) + continue; + + if (match == -1) /* partial match */ + match = i; + else if (!IDENTICAL_INTERPRETATION(i, match)) + ambiguous = 1; + } + if (ambiguous) { + /* ambiguous abbreviation */ + if (PRINT_ERROR) + warnx(ambig, (int)current_argv_len, + current_argv); + optopt = 0; + return (BADCH); + } + if (match != -1) { /* option found */ + if (long_options[match].has_arg == no_argument + && has_equal) { + if (PRINT_ERROR) + warnx(noarg, (int)current_argv_len, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + return (BADARG); + } + if (long_options[match].has_arg == required_argument || + long_options[match].has_arg == optional_argument) { + if (has_equal) + optarg = has_equal; + else if (long_options[match].has_arg == + required_argument) { + /* + * optional argument doesn't use next nargv + */ + optarg = nargv[optind++]; + } + } + if ((long_options[match].has_arg == required_argument) + && (optarg == NULL)) { + /* + * Missing argument; leading ':' indicates no error + * should be generated. + */ + if (PRINT_ERROR) + warnx(recargstring, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + --optind; + return (BADARG); + } + } else { /* unknown option */ + if (short_too) { + --optind; + return (-1); + } + if (PRINT_ERROR) + warnx(illoptstring, current_argv); + optopt = 0; + return (BADCH); + } + if (idx) + *idx = match; + if (long_options[match].flag) { + *long_options[match].flag = long_options[match].val; + return (0); + } else + return (long_options[match].val); +#undef IDENTICAL_INTERPRETATION +} + +/* + * getopt_internal -- + * Parse argc/argv argument vector. Called by user level routines. + */ +static int +getopt_internal(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx, int flags) +{ + char *oli; /* option letter list index */ + int optchar, short_too; + static int posixly_correct = -1; + + if (options == NULL) + return (-1); + + /* + * XXX Some GNU programs (like cvs) set optind to 0 instead of + * XXX using optreset. Work around this braindamage. + */ + if (optind == 0) + optind = optreset = 1; + + /* + * Disable GNU extensions if POSIXLY_CORRECT is set or options + * string begins with a '+'. + * + * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or + * optreset != 0 for GNU compatibility. + */ + if (posixly_correct == -1 || optreset != 0) + posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); + if (*options == '-') + flags |= FLAG_ALLARGS; + else if (posixly_correct || *options == '+') + flags &= ~FLAG_PERMUTE; + if (*options == '+' || *options == '-') + options++; + + optarg = NULL; + if (optreset) + nonopt_start = nonopt_end = -1; +start: + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc) { /* end of argument vector */ + place = EMSG; + if (nonopt_end != -1) { + /* do permutation, if we have to */ + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + else if (nonopt_start != -1) { + /* + * If we skipped non-options, set optind + * to the first of them. + */ + optind = nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + if (*(place = nargv[optind]) != '-' || + (place[1] == '\0' && strchr(options, '-') == NULL)) { + place = EMSG; /* found non-option */ + if (flags & FLAG_ALLARGS) { + /* + * GNU extension: + * return non-option as argument to option 1 + */ + optarg = nargv[optind++]; + return (INORDER); + } + if (!(flags & FLAG_PERMUTE)) { + /* + * If no permutation wanted, stop parsing + * at first non-option. + */ + return (-1); + } + /* do permutation */ + if (nonopt_start == -1) + nonopt_start = optind; + else if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + nonopt_start = optind - + (nonopt_end - nonopt_start); + nonopt_end = -1; + } + optind++; + /* process next argument */ + goto start; + } + if (nonopt_start != -1 && nonopt_end == -1) + nonopt_end = optind; + + /* + * If we have "-" do nothing, if "--" we are done. + */ + if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { + optind++; + place = EMSG; + /* + * We found an option (--), so if we skipped + * non-options, we have to permute. + */ + if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + } + + /* + * Check long options if: + * 1) we were passed some + * 2) the arg is not just "-" + * 3) either the arg starts with -- we are getopt_long_only() + */ + if (long_options != NULL && place != nargv[optind] && + (*place == '-' || (flags & FLAG_LONGONLY))) { + short_too = 0; + if (*place == '-') + place++; /* --foo long option */ + else if (*place != ':' && strchr(options, *place) != NULL) + short_too = 1; /* could be short option too */ + + optchar = parse_long_options(nargv, options, long_options, + idx, short_too); + if (optchar != -1) { + place = EMSG; + return (optchar); + } + } + + if ((optchar = (int)*place++) == (int)':' || + (optchar == (int)'-' && *place != '\0') || + (oli = (char*)strchr(options, optchar)) == NULL) { + /* + * If the user specified "-" and '-' isn't listed in + * options, return -1 (non-option) as per POSIX. + * Otherwise, it is an unknown option character (or ':'). + */ + if (optchar == (int)'-' && *place == '\0') + return (-1); + if (!*place) + ++optind; + if (PRINT_ERROR) + warnx(illoptchar, optchar); + optopt = optchar; + return (BADCH); + } + if (long_options != NULL && optchar == 'W' && oli[1] == ';') { + /* -W long-option */ + if (*place) /* no space */ + /* NOTHING */; + else if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else /* white space */ + place = nargv[optind]; + optchar = parse_long_options(nargv, options, long_options, + idx, 0); + place = EMSG; + return (optchar); + } + if (*++oli != ':') { /* doesn't take argument */ + if (!*place) + ++optind; + } else { /* takes (optional) argument */ + optarg = NULL; + if (*place) /* no white space */ + optarg = place; + else if (oli[1] != ':') { /* arg not optional */ + if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else + optarg = nargv[optind]; + } + place = EMSG; + ++optind; + } + /* dump back option letter */ + return (optchar); +} + +/* + * getopt_long -- + * Parse argc/argv argument vector. + */ +int +getopt_long(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE)); +} + +/* + * getopt_long_only -- + * Parse argc/argv argument vector. + */ +int +getopt_long_only(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE|FLAG_LONGONLY)); +} + +//extern int getopt_long(int nargc, char * const *nargv, const char *options, +// const struct option *long_options, int *idx); +//extern int getopt_long_only(int nargc, char * const *nargv, const char *options, +// const struct option *long_options, int *idx); +/* + * Previous MinGW implementation had... + */ +#ifndef HAVE_DECL_GETOPT +/* + * ...for the long form API only; keep this for compatibility. + */ +# define HAVE_DECL_GETOPT 1 +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */ \ No newline at end of file diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/misc/queue.h b/code/drivers/fs/ext4/third_party/lwext4/include/misc/queue.h new file mode 100644 index 0000000..6efd6f3 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/misc/queue.h @@ -0,0 +1,702 @@ +/*- + * Copyright (c) 1991, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)queue.h 8.5 (Berkeley) 8/20/94 + * $FreeBSD$ + */ + +#ifndef _SYS_QUEUE_H_ +#define _SYS_QUEUE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "../ext4_config.h" + +/* + * This file defines four types of data structures: singly-linked lists, + * singly-linked tail queues, lists and tail queues. + * + * A singly-linked list is headed by a single forward pointer. The elements + * are singly linked for minimum space and pointer manipulation overhead at + * the expense of O(n) removal for arbitrary elements. New elements can be + * added to the list after an existing element or at the head of the list. + * Elements being removed from the head of the list should use the explicit + * macro for this purpose for optimum efficiency. A singly-linked list may + * only be traversed in the forward direction. Singly-linked lists are ideal + * for applications with large datasets and few or no removals or for + * implementing a LIFO queue. + * + * A singly-linked tail queue is headed by a pair of pointers, one to the + * head of the list and the other to the tail of the list. The elements are + * singly linked for minimum space and pointer manipulation overhead at the + * expense of O(n) removal for arbitrary elements. New elements can be added + * to the list after an existing element, at the head of the list, or at the + * end of the list. Elements being removed from the head of the tail queue + * should use the explicit macro for this purpose for optimum efficiency. + * A singly-linked tail queue may only be traversed in the forward direction. + * Singly-linked tail queues are ideal for applications with large datasets + * and few or no removals or for implementing a FIFO queue. + * + * A list is headed by a single forward pointer (or an array of forward + * pointers for a hash table header). The elements are doubly linked + * so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before + * or after an existing element or at the head of the list. A list + * may be traversed in either direction. + * + * A tail queue is headed by a pair of pointers, one to the head of the + * list and the other to the tail of the list. The elements are doubly + * linked so that an arbitrary element can be removed without a need to + * traverse the list. New elements can be added to the list before or + * after an existing element, at the head of the list, or at the end of + * the list. A tail queue may be traversed in either direction. + * + * For details on the use of these macros, see the queue(3) manual page. + * + * + * SLIST LIST STAILQ TAILQ + * _HEAD + + + + + * _HEAD_INITIALIZER + + + + + * _ENTRY + + + + + * _INIT + + + + + * _EMPTY + + + + + * _FIRST + + + + + * _NEXT + + + + + * _PREV - + - + + * _LAST - - + + + * _FOREACH + + + + + * _FOREACH_FROM + + + + + * _FOREACH_SAFE + + + + + * _FOREACH_FROM_SAFE + + + + + * _FOREACH_REVERSE - - - + + * _FOREACH_REVERSE_FROM - - - + + * _FOREACH_REVERSE_SAFE - - - + + * _FOREACH_REVERSE_FROM_SAFE - - - + + * _INSERT_HEAD + + + + + * _INSERT_BEFORE - + - + + * _INSERT_AFTER + + + + + * _INSERT_TAIL - - + + + * _CONCAT - - + + + * _REMOVE_AFTER + - + - + * _REMOVE_HEAD + - + - + * _REMOVE + + + + + * _SWAP + + + + + * + */ +#ifdef QUEUE_MACRO_DEBUG +/* Store the last 2 places the queue element or head was altered */ +struct qm_trace { + unsigned long lastline; + unsigned long prevline; + const char *lastfile; + const char *prevfile; +}; + +#define TRACEBUF struct qm_trace trace; +#define TRACEBUF_INITIALIZER { __LINE__, 0, __FILE__, NULL } , +#define TRASHIT(x) do {(x) = (void *)-1;} while (0) +#define QMD_SAVELINK(name, link) void **name = (void *)&(link) + +#define QMD_TRACE_HEAD(head) do { \ + (head)->trace.prevline = (head)->trace.lastline; \ + (head)->trace.prevfile = (head)->trace.lastfile; \ + (head)->trace.lastline = __LINE__; \ + (head)->trace.lastfile = __FILE__; \ +} while (0) + +#define QMD_TRACE_ELEM(elem) do { \ + (elem)->trace.prevline = (elem)->trace.lastline; \ + (elem)->trace.prevfile = (elem)->trace.lastfile; \ + (elem)->trace.lastline = __LINE__; \ + (elem)->trace.lastfile = __FILE__; \ +} while (0) + +#else +#define QMD_TRACE_ELEM(elem) +#define QMD_TRACE_HEAD(head) +#define QMD_SAVELINK(name, link) +#define TRACEBUF +#define TRACEBUF_INITIALIZER +#define TRASHIT(x) +#endif /* QUEUE_MACRO_DEBUG */ + +/* + * Singly-linked List declarations. + */ +#define SLIST_HEAD(name, type) \ +struct name { \ + struct type *slh_first; /* first element */ \ +} + +#define SLIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define SLIST_ENTRY(type) \ +struct { \ + struct type *sle_next; /* next element */ \ +} + +/* + * Singly-linked List functions. + */ +#define SLIST_EMPTY(head) ((head)->slh_first == NULL) + +#define SLIST_FIRST(head) ((head)->slh_first) + +#define SLIST_FOREACH(var, head, field) \ + for ((var) = SLIST_FIRST((head)); \ + (var); \ + (var) = SLIST_NEXT((var), field)) + +#define SLIST_FOREACH_FROM(var, head, field) \ + for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ + (var); \ + (var) = SLIST_NEXT((var), field)) + +#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SLIST_FIRST((head)); \ + (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define SLIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ + for ((var) = ((var) ? (var) : SLIST_FIRST((head))); \ + (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ + for ((varp) = &SLIST_FIRST((head)); \ + ((var) = *(varp)) != NULL; \ + (varp) = &SLIST_NEXT((var), field)) + +#define SLIST_INIT(head) do { \ + SLIST_FIRST((head)) = NULL; \ +} while (0) + +#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ + SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ + SLIST_NEXT((slistelm), field) = (elm); \ +} while (0) + +#define SLIST_INSERT_HEAD(head, elm, field) do { \ + SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ + SLIST_FIRST((head)) = (elm); \ +} while (0) + +#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) + +#define SLIST_REMOVE(head, elm, type, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.sle_next); \ + if (SLIST_FIRST((head)) == (elm)) { \ + SLIST_REMOVE_HEAD((head), field); \ + } \ + else { \ + struct type *curelm = SLIST_FIRST((head)); \ + while (SLIST_NEXT(curelm, field) != (elm)) \ + curelm = SLIST_NEXT(curelm, field); \ + SLIST_REMOVE_AFTER(curelm, field); \ + } \ + TRASHIT(*oldnext); \ +} while (0) + +#define SLIST_REMOVE_AFTER(elm, field) do { \ + SLIST_NEXT(elm, field) = \ + SLIST_NEXT(SLIST_NEXT(elm, field), field); \ +} while (0) + +#define SLIST_REMOVE_HEAD(head, field) do { \ + SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ +} while (0) + +#define SLIST_SWAP(head1, head2, type) do { \ + struct type *swap_first = SLIST_FIRST(head1); \ + SLIST_FIRST(head1) = SLIST_FIRST(head2); \ + SLIST_FIRST(head2) = swap_first; \ +} while (0) + +/* + * Singly-linked Tail queue declarations. + */ +#define STAILQ_HEAD(name, type) \ +struct name { \ + struct type *stqh_first;/* first element */ \ + struct type **stqh_last;/* addr of last next element */ \ +} + +#define STAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).stqh_first } + +#define STAILQ_ENTRY(type) \ +struct { \ + struct type *stqe_next; /* next element */ \ +} + +/* + * Singly-linked Tail queue functions. + */ +#define STAILQ_CONCAT(head1, head2) do { \ + if (!STAILQ_EMPTY((head2))) { \ + *(head1)->stqh_last = (head2)->stqh_first; \ + (head1)->stqh_last = (head2)->stqh_last; \ + STAILQ_INIT((head2)); \ + } \ +} while (0) + +#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) + +#define STAILQ_FIRST(head) ((head)->stqh_first) + +#define STAILQ_FOREACH(var, head, field) \ + for((var) = STAILQ_FIRST((head)); \ + (var); \ + (var) = STAILQ_NEXT((var), field)) + +#define STAILQ_FOREACH_FROM(var, head, field) \ + for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ + (var); \ + (var) = STAILQ_NEXT((var), field)) + +#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = STAILQ_FIRST((head)); \ + (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define STAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ + for ((var) = ((var) ? (var) : STAILQ_FIRST((head))); \ + (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define STAILQ_INIT(head) do { \ + STAILQ_FIRST((head)) = NULL; \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ +} while (0) + +#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ + if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ + STAILQ_NEXT((tqelm), field) = (elm); \ +} while (0) + +#define STAILQ_INSERT_HEAD(head, elm, field) do { \ + if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ + STAILQ_FIRST((head)) = (elm); \ +} while (0) + +#define STAILQ_INSERT_TAIL(head, elm, field) do { \ + STAILQ_NEXT((elm), field) = NULL; \ + *(head)->stqh_last = (elm); \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ +} while (0) + +#define STAILQ_LAST(head, type, field) \ + (STAILQ_EMPTY((head)) ? NULL : \ + __containerof((head)->stqh_last, struct type, field.stqe_next)) + +#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) + +#define STAILQ_REMOVE(head, elm, type, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \ + if (STAILQ_FIRST((head)) == (elm)) { \ + STAILQ_REMOVE_HEAD((head), field); \ + } \ + else { \ + struct type *curelm = STAILQ_FIRST((head)); \ + while (STAILQ_NEXT(curelm, field) != (elm)) \ + curelm = STAILQ_NEXT(curelm, field); \ + STAILQ_REMOVE_AFTER(head, curelm, field); \ + } \ + TRASHIT(*oldnext); \ +} while (0) + +#define STAILQ_REMOVE_AFTER(head, elm, field) do { \ + if ((STAILQ_NEXT(elm, field) = \ + STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ +} while (0) + +#define STAILQ_REMOVE_HEAD(head, field) do { \ + if ((STAILQ_FIRST((head)) = \ + STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ +} while (0) + +#define STAILQ_SWAP(head1, head2, type) do { \ + struct type *swap_first = STAILQ_FIRST(head1); \ + struct type **swap_last = (head1)->stqh_last; \ + STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \ + (head1)->stqh_last = (head2)->stqh_last; \ + STAILQ_FIRST(head2) = swap_first; \ + (head2)->stqh_last = swap_last; \ + if (STAILQ_EMPTY(head1)) \ + (head1)->stqh_last = &STAILQ_FIRST(head1); \ + if (STAILQ_EMPTY(head2)) \ + (head2)->stqh_last = &STAILQ_FIRST(head2); \ +} while (0) + + +/* + * List declarations. + */ +#define LIST_HEAD(name, type) \ +struct name { \ + struct type *lh_first; /* first element */ \ +} + +#define LIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define LIST_ENTRY(type) \ +struct { \ + struct type *le_next; /* next element */ \ + struct type **le_prev; /* address of previous next element */ \ +} + +/* + * List functions. + */ + +#if (defined(_KERNEL) && defined(INVARIANTS)) +#define QMD_LIST_CHECK_HEAD(head, field) do { \ + if (LIST_FIRST((head)) != NULL && \ + LIST_FIRST((head))->field.le_prev != \ + &LIST_FIRST((head))) \ + panic("Bad list head %p first->prev != head", (head)); \ +} while (0) + +#define QMD_LIST_CHECK_NEXT(elm, field) do { \ + if (LIST_NEXT((elm), field) != NULL && \ + LIST_NEXT((elm), field)->field.le_prev != \ + &((elm)->field.le_next)) \ + panic("Bad link elm %p next->prev != elm", (elm)); \ +} while (0) + +#define QMD_LIST_CHECK_PREV(elm, field) do { \ + if (*(elm)->field.le_prev != (elm)) \ + panic("Bad link elm %p prev->next != elm", (elm)); \ +} while (0) +#else +#define QMD_LIST_CHECK_HEAD(head, field) +#define QMD_LIST_CHECK_NEXT(elm, field) +#define QMD_LIST_CHECK_PREV(elm, field) +#endif /* (_KERNEL && INVARIANTS) */ + +#define LIST_EMPTY(head) ((head)->lh_first == NULL) + +#define LIST_FIRST(head) ((head)->lh_first) + +#define LIST_FOREACH(var, head, field) \ + for ((var) = LIST_FIRST((head)); \ + (var); \ + (var) = LIST_NEXT((var), field)) + +#define LIST_FOREACH_FROM(var, head, field) \ + for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ + (var); \ + (var) = LIST_NEXT((var), field)) + +#define LIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define LIST_FOREACH_FROM_SAFE(var, head, field, tvar) \ + for ((var) = ((var) ? (var) : LIST_FIRST((head))); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define LIST_INIT(head) do { \ + LIST_FIRST((head)) = NULL; \ +} while (0) + +#define LIST_INSERT_AFTER(listelm, elm, field) do { \ + QMD_LIST_CHECK_NEXT(listelm, field); \ + if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ + LIST_NEXT((listelm), field)->field.le_prev = \ + &LIST_NEXT((elm), field); \ + LIST_NEXT((listelm), field) = (elm); \ + (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ +} while (0) + +#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ + QMD_LIST_CHECK_PREV(listelm, field); \ + (elm)->field.le_prev = (listelm)->field.le_prev; \ + LIST_NEXT((elm), field) = (listelm); \ + *(listelm)->field.le_prev = (elm); \ + (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ +} while (0) + +#define LIST_INSERT_HEAD(head, elm, field) do { \ + QMD_LIST_CHECK_HEAD((head), field); \ + if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ + LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ + LIST_FIRST((head)) = (elm); \ + (elm)->field.le_prev = &LIST_FIRST((head)); \ +} while (0) + +#define LIST_NEXT(elm, field) ((elm)->field.le_next) + +#define LIST_PREV(elm, head, type, field) \ + ((elm)->field.le_prev == &LIST_FIRST((head)) ? NULL : \ + __containerof((elm)->field.le_prev, struct type, field.le_next)) + +#define LIST_REMOVE(elm, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.le_next); \ + QMD_SAVELINK(oldprev, (elm)->field.le_prev); \ + QMD_LIST_CHECK_NEXT(elm, field); \ + QMD_LIST_CHECK_PREV(elm, field); \ + if (LIST_NEXT((elm), field) != NULL) \ + LIST_NEXT((elm), field)->field.le_prev = \ + (elm)->field.le_prev; \ + *(elm)->field.le_prev = LIST_NEXT((elm), field); \ + TRASHIT(*oldnext); \ + TRASHIT(*oldprev); \ +} while (0) + +#define LIST_SWAP(head1, head2, type, field) do { \ + struct type *swap_tmp = LIST_FIRST((head1)); \ + LIST_FIRST((head1)) = LIST_FIRST((head2)); \ + LIST_FIRST((head2)) = swap_tmp; \ + if ((swap_tmp = LIST_FIRST((head1))) != NULL) \ + swap_tmp->field.le_prev = &LIST_FIRST((head1)); \ + if ((swap_tmp = LIST_FIRST((head2))) != NULL) \ + swap_tmp->field.le_prev = &LIST_FIRST((head2)); \ +} while (0) + +/* + * Tail queue declarations. + */ +#define TAILQ_HEAD(name, type) \ +struct name { \ + struct type *tqh_first; /* first element */ \ + struct type **tqh_last; /* addr of last next element */ \ + TRACEBUF \ +} + +#define TAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).tqh_first, TRACEBUF_INITIALIZER } + +#define TAILQ_ENTRY(type) \ +struct { \ + struct type *tqe_next; /* next element */ \ + struct type **tqe_prev; /* address of previous next element */ \ + TRACEBUF \ +} + +/* + * Tail queue functions. + */ +#if (defined(_KERNEL) && defined(INVARIANTS)) +#define QMD_TAILQ_CHECK_HEAD(head, field) do { \ + if (!TAILQ_EMPTY(head) && \ + TAILQ_FIRST((head))->field.tqe_prev != \ + &TAILQ_FIRST((head))) \ + panic("Bad tailq head %p first->prev != head", (head)); \ +} while (0) + +#define QMD_TAILQ_CHECK_TAIL(head, field) do { \ + if (*(head)->tqh_last != NULL) \ + panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \ +} while (0) + +#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \ + if (TAILQ_NEXT((elm), field) != NULL && \ + TAILQ_NEXT((elm), field)->field.tqe_prev != \ + &((elm)->field.tqe_next)) \ + panic("Bad link elm %p next->prev != elm", (elm)); \ +} while (0) + +#define QMD_TAILQ_CHECK_PREV(elm, field) do { \ + if (*(elm)->field.tqe_prev != (elm)) \ + panic("Bad link elm %p prev->next != elm", (elm)); \ +} while (0) +#else +#define QMD_TAILQ_CHECK_HEAD(head, field) +#define QMD_TAILQ_CHECK_TAIL(head, headname) +#define QMD_TAILQ_CHECK_NEXT(elm, field) +#define QMD_TAILQ_CHECK_PREV(elm, field) +#endif /* (_KERNEL && INVARIANTS) */ + +#define TAILQ_CONCAT(head1, head2, field) do { \ + if (!TAILQ_EMPTY(head2)) { \ + *(head1)->tqh_last = (head2)->tqh_first; \ + (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ + (head1)->tqh_last = (head2)->tqh_last; \ + TAILQ_INIT((head2)); \ + QMD_TRACE_HEAD(head1); \ + QMD_TRACE_HEAD(head2); \ + } \ +} while (0) + +#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) + +#define TAILQ_FIRST(head) ((head)->tqh_first) + +#define TAILQ_FOREACH(var, head, field) \ + for ((var) = TAILQ_FIRST((head)); \ + (var); \ + (var) = TAILQ_NEXT((var), field)) + +#define TAILQ_FOREACH_FROM(var, head, field) \ + for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ + (var); \ + (var) = TAILQ_NEXT((var), field)) + +#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define TAILQ_FOREACH_FROM_SAFE(var, head, field, tvar) \ + for ((var) = ((var) ? (var) : TAILQ_FIRST((head))); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) + +#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ + for ((var) = TAILQ_LAST((head), headname); \ + (var); \ + (var) = TAILQ_PREV((var), headname, field)) + +#define TAILQ_FOREACH_REVERSE_FROM(var, head, headname, field) \ + for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ + (var); \ + (var) = TAILQ_PREV((var), headname, field)) + +#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ + for ((var) = TAILQ_LAST((head), headname); \ + (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ + (var) = (tvar)) + +#define TAILQ_FOREACH_REVERSE_FROM_SAFE(var, head, headname, field, tvar) \ + for ((var) = ((var) ? (var) : TAILQ_LAST((head), headname)); \ + (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ + (var) = (tvar)) + +#define TAILQ_INIT(head) do { \ + TAILQ_FIRST((head)) = NULL; \ + (head)->tqh_last = &TAILQ_FIRST((head)); \ + QMD_TRACE_HEAD(head); \ +} while (0) + +#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ + QMD_TAILQ_CHECK_NEXT(listelm, field); \ + if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ + TAILQ_NEXT((elm), field)->field.tqe_prev = \ + &TAILQ_NEXT((elm), field); \ + else { \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ + QMD_TRACE_HEAD(head); \ + } \ + TAILQ_NEXT((listelm), field) = (elm); \ + (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ + QMD_TRACE_ELEM(&(elm)->field); \ + QMD_TRACE_ELEM(&(listelm)->field); \ +} while (0) + +#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ + QMD_TAILQ_CHECK_PREV(listelm, field); \ + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ + TAILQ_NEXT((elm), field) = (listelm); \ + *(listelm)->field.tqe_prev = (elm); \ + (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ + QMD_TRACE_ELEM(&(elm)->field); \ + QMD_TRACE_ELEM(&(listelm)->field); \ +} while (0) + +#define TAILQ_INSERT_HEAD(head, elm, field) do { \ + QMD_TAILQ_CHECK_HEAD(head, field); \ + if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ + TAILQ_FIRST((head))->field.tqe_prev = \ + &TAILQ_NEXT((elm), field); \ + else \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ + TAILQ_FIRST((head)) = (elm); \ + (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ + QMD_TRACE_HEAD(head); \ + QMD_TRACE_ELEM(&(elm)->field); \ +} while (0) + +#define TAILQ_INSERT_TAIL(head, elm, field) do { \ + QMD_TAILQ_CHECK_TAIL(head, field); \ + TAILQ_NEXT((elm), field) = NULL; \ + (elm)->field.tqe_prev = (head)->tqh_last; \ + *(head)->tqh_last = (elm); \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ + QMD_TRACE_HEAD(head); \ + QMD_TRACE_ELEM(&(elm)->field); \ +} while (0) + +#define TAILQ_LAST(head, headname) \ + (*(((struct headname *)((head)->tqh_last))->tqh_last)) + +#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) + +#define TAILQ_PREV(elm, headname, field) \ + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) + +#define TAILQ_REMOVE(head, elm, field) do { \ + QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \ + QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \ + QMD_TAILQ_CHECK_NEXT(elm, field); \ + QMD_TAILQ_CHECK_PREV(elm, field); \ + if ((TAILQ_NEXT((elm), field)) != NULL) \ + TAILQ_NEXT((elm), field)->field.tqe_prev = \ + (elm)->field.tqe_prev; \ + else { \ + (head)->tqh_last = (elm)->field.tqe_prev; \ + QMD_TRACE_HEAD(head); \ + } \ + *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ + TRASHIT(*oldnext); \ + TRASHIT(*oldprev); \ + QMD_TRACE_ELEM(&(elm)->field); \ +} while (0) + +#define TAILQ_SWAP(head1, head2, type, field) do { \ + struct type *swap_first = (head1)->tqh_first; \ + struct type **swap_last = (head1)->tqh_last; \ + (head1)->tqh_first = (head2)->tqh_first; \ + (head1)->tqh_last = (head2)->tqh_last; \ + (head2)->tqh_first = swap_first; \ + (head2)->tqh_last = swap_last; \ + if ((swap_first = (head1)->tqh_first) != NULL) \ + swap_first->field.tqe_prev = &(head1)->tqh_first; \ + else \ + (head1)->tqh_last = &(head1)->tqh_first; \ + if ((swap_first = (head2)->tqh_first) != NULL) \ + swap_first->field.tqe_prev = &(head2)->tqh_first; \ + else \ + (head2)->tqh_last = &(head2)->tqh_first; \ +} while (0) + +#ifdef __cplusplus +} +#endif + +#endif /* !_SYS_QUEUE_H_ */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/misc/tree.h b/code/drivers/fs/ext4/third_party/lwext4/include/misc/tree.h new file mode 100644 index 0000000..8ed5d41 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/misc/tree.h @@ -0,0 +1,809 @@ +/* $NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $ */ +/* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */ +/* $FreeBSD$ */ + +/*- + * Copyright 2002 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_TREE_H_ +#define _SYS_TREE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "../ext4_config.h" + +/* + * This file defines data structures for different types of trees: + * splay trees and red-black trees. + * + * A splay tree is a self-organizing data structure. Every operation + * on the tree causes a splay to happen. The splay moves the requested + * node to the root of the tree and partly rebalances it. + * + * This has the benefit that request locality causes faster lookups as + * the requested nodes move to the top of the tree. On the other hand, + * every lookup causes memory writes. + * + * The Balance Theorem bounds the total access time for m operations + * and n inserts on an initially empty tree as O((m + n)lg n). The + * amortized cost for a sequence of m accesses to a splay tree is O(lg n); + * + * A red-black tree is a binary search tree with the node color as an + * extra attribute. It fulfills a set of conditions: + * - every search path from the root to a leaf consists of the + * same number of black nodes, + * - each red node (except for the root) has a black parent, + * - each leaf node is black. + * + * Every operation on a red-black tree is bounded as O(lg n). + * The maximum height of a red-black tree is 2lg (n+1). + */ + +#define SPLAY_HEAD(name, type) \ +struct name { \ + struct type *sph_root; /* root of the tree */ \ +} + +#define SPLAY_INITIALIZER(root) \ + { NULL } + +#define SPLAY_INIT(root) do { \ + (root)->sph_root = NULL; \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_ENTRY(type) \ +struct { \ + struct type *spe_left; /* left element */ \ + struct type *spe_right; /* right element */ \ +} + +#define SPLAY_LEFT(elm, field) (elm)->field.spe_left +#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right +#define SPLAY_ROOT(head) (head)->sph_root +#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL) + +/* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */ +#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \ + SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \ + SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ + (head)->sph_root = tmp; \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \ + SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \ + SPLAY_LEFT(tmp, field) = (head)->sph_root; \ + (head)->sph_root = tmp; \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_LINKLEFT(head, tmp, field) do { \ + SPLAY_LEFT(tmp, field) = (head)->sph_root; \ + tmp = (head)->sph_root; \ + (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_LINKRIGHT(head, tmp, field) do { \ + SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ + tmp = (head)->sph_root; \ + (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \ +} while (/*CONSTCOND*/ 0) + +#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \ + SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \ + SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\ + SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \ + SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \ +} while (/*CONSTCOND*/ 0) + +/* Generates prototypes and inline functions */ + +#define SPLAY_PROTOTYPE(name, type, field, cmp) \ +void name##_SPLAY(struct name *, struct type *); \ +void name##_SPLAY_MINMAX(struct name *, int); \ +struct type *name##_SPLAY_INSERT(struct name *, struct type *); \ +struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \ + \ +/* Finds the node with the same key as elm */ \ +static __inline struct type * \ +name##_SPLAY_FIND(struct name *head, struct type *elm) \ +{ \ + if (SPLAY_EMPTY(head)) \ + return(NULL); \ + name##_SPLAY(head, elm); \ + if ((cmp)(elm, (head)->sph_root) == 0) \ + return (head->sph_root); \ + return (NULL); \ +} \ + \ +static __inline struct type * \ +name##_SPLAY_NEXT(struct name *head, struct type *elm) \ +{ \ + name##_SPLAY(head, elm); \ + if (SPLAY_RIGHT(elm, field) != NULL) { \ + elm = SPLAY_RIGHT(elm, field); \ + while (SPLAY_LEFT(elm, field) != NULL) { \ + elm = SPLAY_LEFT(elm, field); \ + } \ + } else \ + elm = NULL; \ + return (elm); \ +} \ + \ +static __inline struct type * \ +name##_SPLAY_MIN_MAX(struct name *head, int val) \ +{ \ + name##_SPLAY_MINMAX(head, val); \ + return (SPLAY_ROOT(head)); \ +} + +/* Main splay operation. + * Moves node close to the key of elm to top + */ +#define SPLAY_GENERATE(name, type, field, cmp) \ +struct type * \ +name##_SPLAY_INSERT(struct name *head, struct type *elm) \ +{ \ + if (SPLAY_EMPTY(head)) { \ + SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \ + } else { \ + int __comp; \ + name##_SPLAY(head, elm); \ + __comp = (cmp)(elm, (head)->sph_root); \ + if(__comp < 0) { \ + SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\ + SPLAY_RIGHT(elm, field) = (head)->sph_root; \ + SPLAY_LEFT((head)->sph_root, field) = NULL; \ + } else if (__comp > 0) { \ + SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\ + SPLAY_LEFT(elm, field) = (head)->sph_root; \ + SPLAY_RIGHT((head)->sph_root, field) = NULL; \ + } else \ + return ((head)->sph_root); \ + } \ + (head)->sph_root = (elm); \ + return (NULL); \ +} \ + \ +struct type * \ +name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ +{ \ + struct type *__tmp; \ + if (SPLAY_EMPTY(head)) \ + return (NULL); \ + name##_SPLAY(head, elm); \ + if ((cmp)(elm, (head)->sph_root) == 0) { \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \ + (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\ + } else { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\ + name##_SPLAY(head, elm); \ + SPLAY_RIGHT((head)->sph_root, field) = __tmp; \ + } \ + return (elm); \ + } \ + return (NULL); \ +} \ + \ +void \ +name##_SPLAY(struct name *head, struct type *elm) \ +{ \ + struct type __node, *__left, *__right, *__tmp; \ + int __comp; \ +\ + SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ + __left = __right = &__node; \ +\ + while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \ + if (__comp < 0) { \ + __tmp = SPLAY_LEFT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if ((cmp)(elm, __tmp) < 0){ \ + SPLAY_ROTATE_RIGHT(head, __tmp, field); \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKLEFT(head, __right, field); \ + } else if (__comp > 0) { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if ((cmp)(elm, __tmp) > 0){ \ + SPLAY_ROTATE_LEFT(head, __tmp, field); \ + if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKRIGHT(head, __left, field); \ + } \ + } \ + SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ +} \ + \ +/* Splay with either the minimum or the maximum element \ + * Used to find minimum or maximum element in tree. \ + */ \ +void name##_SPLAY_MINMAX(struct name *head, int __comp) \ +{ \ + struct type __node, *__left, *__right, *__tmp; \ +\ + SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ + __left = __right = &__node; \ +\ + while (1) { \ + if (__comp < 0) { \ + __tmp = SPLAY_LEFT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if (__comp < 0){ \ + SPLAY_ROTATE_RIGHT(head, __tmp, field); \ + if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKLEFT(head, __right, field); \ + } else if (__comp > 0) { \ + __tmp = SPLAY_RIGHT((head)->sph_root, field); \ + if (__tmp == NULL) \ + break; \ + if (__comp > 0) { \ + SPLAY_ROTATE_LEFT(head, __tmp, field); \ + if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ + break; \ + } \ + SPLAY_LINKRIGHT(head, __left, field); \ + } \ + } \ + SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ +} + +#define SPLAY_NEGINF -1 +#define SPLAY_INF 1 + +#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y) +#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y) +#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y) +#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y) +#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \ + : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF)) +#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \ + : name##_SPLAY_MIN_MAX(x, SPLAY_INF)) + +#define SPLAY_FOREACH(x, name, head) \ + for ((x) = SPLAY_MIN(name, head); \ + (x) != NULL; \ + (x) = SPLAY_NEXT(name, head, x)) + +/* Macros that define a red-black tree */ +#define RB_HEAD(name, type) \ +struct name { \ + struct type *rbh_root; /* root of the tree */ \ +} + +#define RB_INITIALIZER(root) \ + { NULL } + +#define RB_INIT(root) do { \ + (root)->rbh_root = NULL; \ +} while (/*CONSTCOND*/ 0) + +#define RB_BLACK 0 +#define RB_RED 1 +#define RB_ENTRY(type) \ +struct { \ + struct type *rbe_left; /* left element */ \ + struct type *rbe_right; /* right element */ \ + struct type *rbe_parent; /* parent element */ \ + int rbe_color; /* node color */ \ +} + +#define RB_LEFT(elm, field) (elm)->field.rbe_left +#define RB_RIGHT(elm, field) (elm)->field.rbe_right +#define RB_PARENT(elm, field) (elm)->field.rbe_parent +#define RB_COLOR(elm, field) (elm)->field.rbe_color +#define RB_ROOT(head) (head)->rbh_root +#define RB_EMPTY(head) (RB_ROOT(head) == NULL) + +#define RB_SET(elm, parent, field) do { \ + RB_PARENT(elm, field) = parent; \ + RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \ + RB_COLOR(elm, field) = RB_RED; \ +} while (/*CONSTCOND*/ 0) + +#define RB_SET_BLACKRED(black, red, field) do { \ + RB_COLOR(black, field) = RB_BLACK; \ + RB_COLOR(red, field) = RB_RED; \ +} while (/*CONSTCOND*/ 0) + +#ifndef RB_AUGMENT +#define RB_AUGMENT(x) do {} while (0) +#endif + +#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ + (tmp) = RB_RIGHT(elm, field); \ + if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \ + RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_LEFT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ +} while (/*CONSTCOND*/ 0) + +#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ + (tmp) = RB_LEFT(elm, field); \ + if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \ + RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \ + } \ + RB_AUGMENT(elm); \ + if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \ + if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ + RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ + else \ + RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ + } else \ + (head)->rbh_root = (tmp); \ + RB_RIGHT(tmp, field) = (elm); \ + RB_PARENT(elm, field) = (tmp); \ + RB_AUGMENT(tmp); \ + if ((RB_PARENT(tmp, field))) \ + RB_AUGMENT(RB_PARENT(tmp, field)); \ +} while (/*CONSTCOND*/ 0) + +/* Generates prototypes and inline functions */ +#define RB_PROTOTYPE(name, type, field, cmp) \ + RB_PROTOTYPE_INTERNAL(name, type, field, cmp,) +#define RB_PROTOTYPE_STATIC(name, type, field, cmp) \ + RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static) +#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \ + RB_PROTOTYPE_INSERT_COLOR(name, type, attr); \ + RB_PROTOTYPE_REMOVE_COLOR(name, type, attr); \ + RB_PROTOTYPE_INSERT(name, type, attr); \ + RB_PROTOTYPE_REMOVE(name, type, attr); \ + RB_PROTOTYPE_FIND(name, type, attr); \ + RB_PROTOTYPE_NFIND(name, type, attr); \ + RB_PROTOTYPE_NEXT(name, type, attr); \ + RB_PROTOTYPE_PREV(name, type, attr); \ + RB_PROTOTYPE_MINMAX(name, type, attr); +#define RB_PROTOTYPE_INSERT_COLOR(name, type, attr) \ + attr void name##_RB_INSERT_COLOR(struct name *, struct type *) +#define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr) \ + attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *) +#define RB_PROTOTYPE_REMOVE(name, type, attr) \ + attr struct type *name##_RB_REMOVE(struct name *, struct type *) +#define RB_PROTOTYPE_INSERT(name, type, attr) \ + attr struct type *name##_RB_INSERT(struct name *, struct type *) +#define RB_PROTOTYPE_FIND(name, type, attr) \ + attr struct type *name##_RB_FIND(struct name *, struct type *) +#define RB_PROTOTYPE_NFIND(name, type, attr) \ + attr struct type *name##_RB_NFIND(struct name *, struct type *) +#define RB_PROTOTYPE_NEXT(name, type, attr) \ + attr struct type *name##_RB_NEXT(struct type *) +#define RB_PROTOTYPE_PREV(name, type, attr) \ + attr struct type *name##_RB_PREV(struct type *) +#define RB_PROTOTYPE_MINMAX(name, type, attr) \ + attr struct type *name##_RB_MINMAX(struct name *, int) + +/* Main rb operation. + * Moves node close to the key of elm to top + */ +#define RB_GENERATE(name, type, field, cmp) \ + RB_GENERATE_INTERNAL(name, type, field, cmp,) +#define RB_GENERATE_STATIC(name, type, field, cmp) \ + RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static) +#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \ + RB_GENERATE_INSERT_COLOR(name, type, field, attr) \ + RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \ + RB_GENERATE_INSERT(name, type, field, cmp, attr) \ + RB_GENERATE_REMOVE(name, type, field, attr) \ + RB_GENERATE_FIND(name, type, field, cmp, attr) \ + RB_GENERATE_NFIND(name, type, field, cmp, attr) \ + RB_GENERATE_NEXT(name, type, field, attr) \ + RB_GENERATE_PREV(name, type, field, attr) \ + RB_GENERATE_MINMAX(name, type, field, attr) + +#define RB_GENERATE_INSERT_COLOR(name, type, field, attr) \ +attr void \ +name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ +{ \ + struct type *parent, *gparent, *tmp; \ + while ((parent = RB_PARENT(elm, field)) != NULL && \ + RB_COLOR(parent, field) == RB_RED) { \ + gparent = RB_PARENT(parent, field); \ + if (parent == RB_LEFT(gparent, field)) { \ + tmp = RB_RIGHT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field);\ + elm = gparent; \ + continue; \ + } \ + if (RB_RIGHT(parent, field) == elm) { \ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_RIGHT(head, gparent, tmp, field); \ + } else { \ + tmp = RB_LEFT(gparent, field); \ + if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ + RB_COLOR(tmp, field) = RB_BLACK; \ + RB_SET_BLACKRED(parent, gparent, field);\ + elm = gparent; \ + continue; \ + } \ + if (RB_LEFT(parent, field) == elm) { \ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + tmp = parent; \ + parent = elm; \ + elm = tmp; \ + } \ + RB_SET_BLACKRED(parent, gparent, field); \ + RB_ROTATE_LEFT(head, gparent, tmp, field); \ + } \ + } \ + RB_COLOR(head->rbh_root, field) = RB_BLACK; \ +} + +#define RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \ +attr void \ +name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \ +{ \ + struct type *tmp; \ + while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \ + elm != RB_ROOT(head)) { \ + if (RB_LEFT(parent, field) == elm) { \ + tmp = RB_RIGHT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + tmp = RB_RIGHT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\ + struct type *oleft; \ + if ((oleft = RB_LEFT(tmp, field)) \ + != NULL) \ + RB_COLOR(oleft, field) = RB_BLACK;\ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_RIGHT(head, tmp, oleft, field);\ + tmp = RB_RIGHT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_RIGHT(tmp, field)) \ + RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\ + RB_ROTATE_LEFT(head, parent, tmp, field);\ + elm = RB_ROOT(head); \ + break; \ + } \ + } else { \ + tmp = RB_LEFT(parent, field); \ + if (RB_COLOR(tmp, field) == RB_RED) { \ + RB_SET_BLACKRED(tmp, parent, field); \ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + tmp = RB_LEFT(parent, field); \ + } \ + if ((RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ + (RB_RIGHT(tmp, field) == NULL || \ + RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ + RB_COLOR(tmp, field) = RB_RED; \ + elm = parent; \ + parent = RB_PARENT(elm, field); \ + } else { \ + if (RB_LEFT(tmp, field) == NULL || \ + RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\ + struct type *oright; \ + if ((oright = RB_RIGHT(tmp, field)) \ + != NULL) \ + RB_COLOR(oright, field) = RB_BLACK;\ + RB_COLOR(tmp, field) = RB_RED; \ + RB_ROTATE_LEFT(head, tmp, oright, field);\ + tmp = RB_LEFT(parent, field); \ + } \ + RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ + RB_COLOR(parent, field) = RB_BLACK; \ + if (RB_LEFT(tmp, field)) \ + RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\ + RB_ROTATE_RIGHT(head, parent, tmp, field);\ + elm = RB_ROOT(head); \ + break; \ + } \ + } \ + } \ + if (elm) \ + RB_COLOR(elm, field) = RB_BLACK; \ +} + +#define RB_GENERATE_REMOVE(name, type, field, attr) \ +attr struct type * \ +name##_RB_REMOVE(struct name *head, struct type *elm) \ +{ \ + struct type *child, *parent, *old = elm; \ + int color; \ + if (RB_LEFT(elm, field) == NULL) \ + child = RB_RIGHT(elm, field); \ + else if (RB_RIGHT(elm, field) == NULL) \ + child = RB_LEFT(elm, field); \ + else { \ + struct type *left; \ + elm = RB_RIGHT(elm, field); \ + while ((left = RB_LEFT(elm, field)) != NULL) \ + elm = left; \ + child = RB_RIGHT(elm, field); \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ + if (RB_PARENT(elm, field) == old) \ + parent = elm; \ + (elm)->field = (old)->field; \ + if (RB_PARENT(old, field)) { \ + if (RB_LEFT(RB_PARENT(old, field), field) == old)\ + RB_LEFT(RB_PARENT(old, field), field) = elm;\ + else \ + RB_RIGHT(RB_PARENT(old, field), field) = elm;\ + RB_AUGMENT(RB_PARENT(old, field)); \ + } else \ + RB_ROOT(head) = elm; \ + RB_PARENT(RB_LEFT(old, field), field) = elm; \ + if (RB_RIGHT(old, field)) \ + RB_PARENT(RB_RIGHT(old, field), field) = elm; \ + if (parent) { \ + left = parent; \ + do { \ + RB_AUGMENT(left); \ + } while ((left = RB_PARENT(left, field)) != NULL); \ + } \ + goto color; \ + } \ + parent = RB_PARENT(elm, field); \ + color = RB_COLOR(elm, field); \ + if (child) \ + RB_PARENT(child, field) = parent; \ + if (parent) { \ + if (RB_LEFT(parent, field) == elm) \ + RB_LEFT(parent, field) = child; \ + else \ + RB_RIGHT(parent, field) = child; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = child; \ +color: \ + if (color == RB_BLACK) \ + name##_RB_REMOVE_COLOR(head, parent, child); \ + return (old); \ +} \ + +#define RB_GENERATE_INSERT(name, type, field, cmp, attr) \ +/* Inserts a node into the RB tree */ \ +attr struct type * \ +name##_RB_INSERT(struct name *head, struct type *elm) \ +{ \ + struct type *tmp; \ + struct type *parent = NULL; \ + int comp = 0; \ + tmp = RB_ROOT(head); \ + while (tmp) { \ + parent = tmp; \ + comp = (cmp)(elm, parent); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + RB_SET(elm, parent, field); \ + if (parent != NULL) { \ + if (comp < 0) \ + RB_LEFT(parent, field) = elm; \ + else \ + RB_RIGHT(parent, field) = elm; \ + RB_AUGMENT(parent); \ + } else \ + RB_ROOT(head) = elm; \ + name##_RB_INSERT_COLOR(head, elm); \ + return (NULL); \ +} + +#define RB_GENERATE_FIND(name, type, field, cmp, attr) \ +/* Finds the node with the same key as elm */ \ +attr struct type * \ +name##_RB_FIND(struct name *head, struct type *elm) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + int comp; \ + while (tmp) { \ + comp = cmp(elm, tmp); \ + if (comp < 0) \ + tmp = RB_LEFT(tmp, field); \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + return (NULL); \ +} + +#define RB_GENERATE_NFIND(name, type, field, cmp, attr) \ +/* Finds the first node greater than or equal to the search key */ \ +attr struct type * \ +name##_RB_NFIND(struct name *head, struct type *elm) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + struct type *res = NULL; \ + int comp; \ + while (tmp) { \ + comp = cmp(elm, tmp); \ + if (comp < 0) { \ + res = tmp; \ + tmp = RB_LEFT(tmp, field); \ + } \ + else if (comp > 0) \ + tmp = RB_RIGHT(tmp, field); \ + else \ + return (tmp); \ + } \ + return (res); \ +} + +#define RB_GENERATE_NEXT(name, type, field, attr) \ +/* ARGSUSED */ \ +attr struct type * \ +name##_RB_NEXT(struct type *elm) \ +{ \ + if (RB_RIGHT(elm, field)) { \ + elm = RB_RIGHT(elm, field); \ + while (RB_LEFT(elm, field)) \ + elm = RB_LEFT(elm, field); \ + } else { \ + if (RB_PARENT(elm, field) && \ + (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + else { \ + while (RB_PARENT(elm, field) && \ + (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\ + elm = RB_PARENT(elm, field); \ + elm = RB_PARENT(elm, field); \ + } \ + } \ + return (elm); \ +} + +#define RB_GENERATE_PREV(name, type, field, attr) \ +/* ARGSUSED */ \ +attr struct type * \ +name##_RB_PREV(struct type *elm) \ +{ \ + if (RB_LEFT(elm, field)) { \ + elm = RB_LEFT(elm, field); \ + while (RB_RIGHT(elm, field)) \ + elm = RB_RIGHT(elm, field); \ + } else { \ + if (RB_PARENT(elm, field) && \ + (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \ + elm = RB_PARENT(elm, field); \ + else { \ + while (RB_PARENT(elm, field) && \ + (elm == RB_LEFT(RB_PARENT(elm, field), field)))\ + elm = RB_PARENT(elm, field); \ + elm = RB_PARENT(elm, field); \ + } \ + } \ + return (elm); \ +} + +#define RB_GENERATE_MINMAX(name, type, field, attr) \ +attr struct type * \ +name##_RB_MINMAX(struct name *head, int val) \ +{ \ + struct type *tmp = RB_ROOT(head); \ + struct type *parent = NULL; \ + while (tmp) { \ + parent = tmp; \ + if (val < 0) \ + tmp = RB_LEFT(tmp, field); \ + else \ + tmp = RB_RIGHT(tmp, field); \ + } \ + return (parent); \ +} + +#define RB_NEGINF -1 +#define RB_INF 1 + +#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y) +#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y) +#define RB_FIND(name, x, y) name##_RB_FIND(x, y) +#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y) +#define RB_NEXT(name, x, y) name##_RB_NEXT(y) +#define RB_PREV(name, x, y) name##_RB_PREV(y) +#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF) +#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF) + +#define RB_FOREACH(x, name, head) \ + for ((x) = RB_MIN(name, head); \ + (x) != NULL; \ + (x) = name##_RB_NEXT(x)) + +#define RB_FOREACH_FROM(x, name, y) \ + for ((x) = (y); \ + ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \ + (x) = (y)) + +#define RB_FOREACH_SAFE(x, name, head, y) \ + for ((x) = RB_MIN(name, head); \ + ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \ + (x) = (y)) + +#define RB_FOREACH_REVERSE(x, name, head) \ + for ((x) = RB_MAX(name, head); \ + (x) != NULL; \ + (x) = name##_RB_PREV(x)) + +#define RB_FOREACH_REVERSE_FROM(x, name, y) \ + for ((x) = (y); \ + ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \ + (x) = (y)) + +#define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \ + for ((x) = RB_MAX(name, head); \ + ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \ + (x) = (y)) + +#ifdef __cplusplus +} +#endif + +#endif /* _SYS_TREE_H_ */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/include/unistd.h b/code/drivers/fs/ext4/third_party/lwext4/include/unistd.h new file mode 100644 index 0000000..5d24403 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/include/unistd.h @@ -0,0 +1,56 @@ +#ifndef _UNISTD_H +#define _UNISTD_H 1 + +/* This is intended as a drop-in replacement for unistd.h on Windows. + * Please add functionality as neeeded. + * https://stackoverflow.com/a/826027/1202830 + */ + +#include +#include +#include /* getopt at: https://gist.github.com/ashelly/7776712 */ +#include /* for getpid() and the exec..() family */ +#include /* for _getcwd() and _chdir() */ + +#define srandom srand +#define random rand + +/* Values for the second argument to access. + These may be OR'd together. */ +#define R_OK 4 /* Test for read permission. */ +#define W_OK 2 /* Test for write permission. */ +//#define X_OK 1 /* execute permission - unsupported in windows*/ +#define F_OK 0 /* Test for existence. */ + +#define access _access +#define dup2 _dup2 +#define execve _execve +#define ftruncate _chsize +#define unlink _unlink +#define fileno _fileno +#define getcwd _getcwd +#define chdir _chdir +#define isatty _isatty +#define lseek _lseek +/* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call e.g. closesocket(). */ + +#ifdef _WIN64 +#define ssize_t __int64 +#else +#define ssize_t long +#endif + +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 +/* should be in some equivalent to */ +typedef __int8 int8_t; +typedef __int16 int16_t; +typedef __int32 int32_t; +typedef __int64 int64_t; +typedef unsigned __int8 uint8_t; +typedef unsigned __int16 uint16_t; +typedef unsigned __int32 uint32_t; +typedef unsigned __int64 uint64_t; + +#endif /* unistd.h */ \ No newline at end of file diff --git a/code/drivers/fs/ext4/third_party/lwext4/lwext4.vcxproj b/code/drivers/fs/ext4/third_party/lwext4/lwext4.vcxproj new file mode 100644 index 0000000..f4aa3a1 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/lwext4.vcxproj @@ -0,0 +1,177 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {34d8a968-f941-43e7-aa4e-19c264b995b0} + SharpExt4 + 10.0 + + + + StaticLibrary + true + v143 + Unicode + + + StaticLibrary + false + v143 + Unicode + + + StaticLibrary + true + v143 + Unicode + + + StaticLibrary + false + v143 + Unicode + + + + + + + + + + + + + + + + + + + + + + + NotUsing + pch.h + Level3 + _CRT_SECURE_NO_WARNINGS;WIN32;_DEBUG;%(PreprocessorDefinitions) + ./include + + + + + + + + NotUsing + pch.h + Level3 + _CRT_SECURE_NO_WARNINGS;_DEBUG;%(PreprocessorDefinitions) + ./include + + + + + + + + NotUsing + pch.h + Level3 + _CRT_SECURE_NO_WARNINGS;WIN32;NDEBUG;%(PreprocessorDefinitions) + ./include + + + + + + + + NotUsing + pch.h + Level3 + _CRT_SECURE_NO_WARNINGS;NDEBUG;%(PreprocessorDefinitions) + ./include + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/code/drivers/fs/ext4/third_party/lwext4/lwext4.vcxproj.filters b/code/drivers/fs/ext4/third_party/lwext4/lwext4.vcxproj.filters new file mode 100644 index 0000000..c3050c7 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/lwext4.vcxproj.filters @@ -0,0 +1,171 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/code/drivers/fs/ext4/third_party/lwext4/lwext4.vcxproj.user b/code/drivers/fs/ext4/third_party/lwext4/lwext4.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/lwext4.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4.c new file mode 100644 index 0000000..a310c17 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4.c @@ -0,0 +1,3240 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4.h + * @brief Ext4 high level operations (file, directory, mountpoints...) + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_oflags.h" +#include "ext4_debug.h" + +#include "ext4.h" +#include "ext4_trans.h" +#include "ext4_blockdev.h" +#include "ext4_fs.h" +#include "ext4_dir.h" +#include "ext4_inode.h" +#include "ext4_super.h" +#include "ext4_block_group.h" +#include "ext4_dir_idx.h" +#include "ext4_xattr.h" +#include "ext4_journal.h" + + +#include +#include + +/**@brief Mount point OS dependent lock*/ +#define EXT4_MP_LOCK(_m) \ + do { \ + if ((_m)->os_locks) \ + (_m)->os_locks->lock(); \ + } while (0) + +/**@brief Mount point OS dependent unlock*/ +#define EXT4_MP_UNLOCK(_m) \ + do { \ + if ((_m)->os_locks) \ + (_m)->os_locks->unlock(); \ + } while (0) + +/**@brief Mount point descriptor.*/ +struct ext4_mountpoint { + + /**@brief Mount done flag.*/ + bool mounted; + + /**@brief Mount point name (@ref ext4_mount)*/ + char name[CONFIG_EXT4_MAX_MP_NAME + 1]; + + /**@brief OS dependent lock/unlock functions.*/ + const struct ext4_lock *os_locks; + + /**@brief Ext4 filesystem internals.*/ + struct ext4_fs fs; + + /**@brief JBD fs.*/ + struct jbd_fs jbd_fs; + + /**@brief Journal.*/ + struct jbd_journal jbd_journal; + + /**@brief Block cache.*/ + struct ext4_bcache bc; +}; + +/**@brief Block devices descriptor.*/ +struct ext4_block_devices { + + /**@brief Block device name.*/ + char name[CONFIG_EXT4_MAX_BLOCKDEV_NAME + 1]; + + /**@brief Block device handle.*/ + struct ext4_blockdev *bd; +}; + +/**@brief Block devices.*/ +static struct ext4_block_devices s_bdevices[CONFIG_EXT4_BLOCKDEVS_COUNT]; + +/**@brief Mountpoints.*/ +static struct ext4_mountpoint s_mp[CONFIG_EXT4_MOUNTPOINTS_COUNT]; + +int ext4_device_register(struct ext4_blockdev *bd, + const char *dev_name) +{ + ext4_assert(bd && dev_name); + + if (strlen(dev_name) > CONFIG_EXT4_MAX_BLOCKDEV_NAME) + return EINVAL; + + for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) { + if (!strcmp(s_bdevices[i].name, dev_name)) + return EEXIST; + } + + for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) { + if (!s_bdevices[i].bd) { + strcpy(s_bdevices[i].name, dev_name); + s_bdevices[i].bd = bd; + return EOK; + } + } + + return ENOSPC; +} + +int ext4_device_unregister(const char *dev_name) +{ + ext4_assert(dev_name); + + for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) { + if (strcmp(s_bdevices[i].name, dev_name)) + continue; + + memset(&s_bdevices[i], 0, sizeof(s_bdevices[i])); + } + + return ENOENT; +} + +int ext4_device_unregister_all(void) +{ + memset(s_bdevices, 0, sizeof(s_bdevices)); + + return EOK; +} + +/****************************************************************************/ + +static bool ext4_is_dots(const uint8_t *name, size_t name_size) +{ + if ((name_size == 1) && (name[0] == '.')) + return true; + + if ((name_size == 2) && (name[0] == '.') && (name[1] == '.')) + return true; + + return false; +} + +static int ext4_has_children(bool *has_children, struct ext4_inode_ref *enode) +{ + struct ext4_sblock *sb = &enode->fs->sb; + + /* Check if node is directory */ + if (!ext4_inode_is_type(sb, enode->inode, EXT4_INODE_MODE_DIRECTORY)) { + *has_children = false; + return EOK; + } + + struct ext4_dir_iter it; + int rc = ext4_dir_iterator_init(&it, enode, 0); + if (rc != EOK) + return rc; + + /* Find a non-empty directory entry */ + bool found = false; + while (it.curr != NULL) { + if (ext4_dir_en_get_inode(it.curr) != 0) { + uint16_t nsize; + nsize = ext4_dir_en_get_name_len(sb, it.curr); + if (!ext4_is_dots(it.curr->name, nsize)) { + found = true; + break; + } + } + + rc = ext4_dir_iterator_next(&it); + if (rc != EOK) { + ext4_dir_iterator_fini(&it); + return rc; + } + } + + rc = ext4_dir_iterator_fini(&it); + if (rc != EOK) + return rc; + + *has_children = found; + + return EOK; +} + +static int ext4_link(struct ext4_mountpoint *mp, struct ext4_inode_ref *parent, + struct ext4_inode_ref *ch, const char *n, + uint32_t len, bool rename) +{ + /* Check maximum name length */ + if (len > EXT4_DIRECTORY_FILENAME_LEN) + return EINVAL; + + /* Add entry to parent directory */ + int r = ext4_dir_add_entry(parent, n, len, ch); + if (r != EOK) + return r; + + /* Fill new dir -> add '.' and '..' entries. + * Also newly allocated inode should have 0 link count. + */ + + bool is_dir = ext4_inode_is_type(&mp->fs.sb, ch->inode, + EXT4_INODE_MODE_DIRECTORY); + if (is_dir && !rename) { + +#if CONFIG_DIR_INDEX_ENABLE + /* Initialize directory index if supported */ + if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) { + r = ext4_dir_dx_init(ch, parent); + if (r != EOK) + return r; + + ext4_inode_set_flag(ch->inode, EXT4_INODE_FLAG_INDEX); + ch->dirty = true; + } else +#endif + { + r = ext4_dir_add_entry(ch, ".", strlen("."), ch); + if (r != EOK) { + ext4_dir_remove_entry(parent, n, strlen(n)); + return r; + } + + r = ext4_dir_add_entry(ch, "..", strlen(".."), parent); + if (r != EOK) { + ext4_dir_remove_entry(parent, n, strlen(n)); + ext4_dir_remove_entry(ch, ".", strlen(".")); + return r; + } + } + + /*New empty directory. Two links (. and ..) */ + ext4_inode_set_links_cnt(ch->inode, 2); + ext4_fs_inode_links_count_inc(parent); + ch->dirty = true; + parent->dirty = true; + return r; + } + /* + * In case we want to rename a directory, + * we reset the original '..' pointer. + */ + if (is_dir) { + bool idx; + idx = ext4_inode_has_flag(ch->inode, EXT4_INODE_FLAG_INDEX); + struct ext4_dir_search_result res; + if (!idx) { + r = ext4_dir_find_entry(&res, ch, "..", strlen("..")); + if (r != EOK) + return EIO; + + ext4_dir_en_set_inode(res.dentry, parent->index); + ext4_trans_set_block_dirty(res.block.buf); + r = ext4_dir_destroy_result(ch, &res); + if (r != EOK) + return r; + + } else { +#if CONFIG_DIR_INDEX_ENABLE + r = ext4_dir_dx_reset_parent_inode(ch, parent->index); + if (r != EOK) + return r; + +#endif + } + + ext4_fs_inode_links_count_inc(parent); + parent->dirty = true; + } + if (!rename) { + ext4_fs_inode_links_count_inc(ch); + ch->dirty = true; + } + + return r; +} + +static int ext4_unlink(struct ext4_mountpoint *mp, + struct ext4_inode_ref *parent, + struct ext4_inode_ref *child, const char *name, + uint32_t name_len) +{ + bool has_children; + int rc = ext4_has_children(&has_children, child); + if (rc != EOK) + return rc; + + /* Cannot unlink non-empty node */ + if (has_children) + return ENOTEMPTY; + + /* Remove entry from parent directory */ + rc = ext4_dir_remove_entry(parent, name, name_len); + if (rc != EOK) + return rc; + + bool is_dir = ext4_inode_is_type(&mp->fs.sb, child->inode, + EXT4_INODE_MODE_DIRECTORY); + + /* If directory - handle links from parent */ + if (is_dir) { + ext4_fs_inode_links_count_dec(parent); + parent->dirty = true; + } + + /* + * TODO: Update timestamps of the parent + * (when we have wall-clock time). + * + * ext4_inode_set_change_inode_time(parent->inode, (uint32_t) now); + * ext4_inode_set_modification_time(parent->inode, (uint32_t) now); + * parent->dirty = true; + */ + + /* + * TODO: Update timestamp for inode. + * + * ext4_inode_set_change_inode_time(child->inode, + * (uint32_t) now); + */ + if (ext4_inode_get_links_cnt(child->inode)) { + ext4_fs_inode_links_count_dec(child); + child->dirty = true; + } + + return EOK; +} + +/****************************************************************************/ + +int ext4_mount(const char *dev_name, const char *mount_point, + bool read_only) +{ + int r; + uint32_t bsize; + struct ext4_bcache *bc; + struct ext4_blockdev *bd = 0; + struct ext4_mountpoint *mp = 0; + + ext4_assert(mount_point && dev_name); + + size_t mp_len = strlen(mount_point); + + if (mp_len > CONFIG_EXT4_MAX_MP_NAME) + return EINVAL; + + if (mount_point[mp_len - 1] != '/') + return ENOTSUP; + + for (size_t i = 0; i < CONFIG_EXT4_BLOCKDEVS_COUNT; ++i) { + if (!strcmp(dev_name, s_bdevices[i].name)) { + bd = s_bdevices[i].bd; + break; + } + } + + if (!bd) + return ENODEV; + + for (size_t i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) { + if (!s_mp[i].mounted) { + strcpy(s_mp[i].name, mount_point); + s_mp[i].mounted = 1; + mp = &s_mp[i]; + break; + } + + if (!strcmp(s_mp[i].name, mount_point)) + return EOK; + } + + if (!mp) + return ENOMEM; + + r = ext4_block_init(bd); + if (r != EOK) + return r; + + r = ext4_fs_init(&mp->fs, bd, read_only); + if (r != EOK) { + ext4_block_fini(bd); + return r; + } + + bsize = ext4_sb_get_block_size(&mp->fs.sb); + ext4_block_set_lb_size(bd, bsize); + bc = &mp->bc; + + r = ext4_bcache_init_dynamic(bc, CONFIG_BLOCK_DEV_CACHE_SIZE, bsize); + if (r != EOK) { + ext4_block_fini(bd); + return r; + } + + if (bsize != bc->itemsize) + return ENOTSUP; + + /*Bind block cache to block device*/ + r = ext4_block_bind_bcache(bd, bc); + if (r != EOK) { + ext4_bcache_cleanup(bc); + ext4_block_fini(bd); + ext4_bcache_fini_dynamic(bc); + return r; + } + + bd->fs = &mp->fs; + return r; +} + + +int ext4_umount(const char *mount_point) +{ + int i; + int r; + struct ext4_mountpoint *mp = 0; + + for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) { + if (!strcmp(s_mp[i].name, mount_point)) { + mp = &s_mp[i]; + break; + } + } + + if (!mp) + return ENODEV; + + r = ext4_fs_fini(&mp->fs); + if (r != EOK) + goto Finish; + + mp->mounted = 0; + + ext4_bcache_cleanup(mp->fs.bdev->bc); + ext4_bcache_fini_dynamic(mp->fs.bdev->bc); + + r = ext4_block_fini(mp->fs.bdev); +Finish: + mp->fs.bdev->fs = NULL; + return r; +} + +static struct ext4_mountpoint *ext4_get_mount(const char *path) +{ + for (size_t i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) { + + if (!s_mp[i].mounted) + continue; + + if (!strncmp(s_mp[i].name, path, strlen(s_mp[i].name))) + return &s_mp[i]; + } + + return NULL; +} + +__unused +static int __ext4_journal_start(const char *mount_point) +{ + int r = EOK; + struct ext4_mountpoint *mp = ext4_get_mount(mount_point); + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EOK; + + if (ext4_sb_feature_com(&mp->fs.sb, + EXT4_FCOM_HAS_JOURNAL)) { + r = jbd_get_fs(&mp->fs, &mp->jbd_fs); + if (r != EOK) + goto Finish; + + r = jbd_journal_start(&mp->jbd_fs, &mp->jbd_journal); + if (r != EOK) { + mp->jbd_fs.dirty = false; + jbd_put_fs(&mp->jbd_fs); + goto Finish; + } + mp->fs.jbd_fs = &mp->jbd_fs; + mp->fs.jbd_journal = &mp->jbd_journal; + } +Finish: + return r; +} + +__unused +static int __ext4_journal_stop(const char *mount_point) +{ + int r = EOK; + struct ext4_mountpoint *mp = ext4_get_mount(mount_point); + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EOK; + + if (ext4_sb_feature_com(&mp->fs.sb, + EXT4_FCOM_HAS_JOURNAL)) { + r = jbd_journal_stop(&mp->jbd_journal); + if (r != EOK) { + mp->jbd_fs.dirty = false; + jbd_put_fs(&mp->jbd_fs); + mp->fs.jbd_journal = NULL; + mp->fs.jbd_fs = NULL; + goto Finish; + } + + r = jbd_put_fs(&mp->jbd_fs); + if (r != EOK) { + mp->fs.jbd_journal = NULL; + mp->fs.jbd_fs = NULL; + goto Finish; + } + + mp->fs.jbd_journal = NULL; + mp->fs.jbd_fs = NULL; + } +Finish: + return r; +} + +__unused +static int __ext4_recover(const char *mount_point) +{ + struct ext4_mountpoint *mp = ext4_get_mount(mount_point); + int r = ENOTSUP; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_HAS_JOURNAL)) { + struct jbd_fs *jbd_fs = ext4_calloc(1, sizeof(struct jbd_fs)); + if (!jbd_fs) { + r = ENOMEM; + goto Finish; + } + + r = jbd_get_fs(&mp->fs, jbd_fs); + if (r != EOK) { + ext4_free(jbd_fs); + goto Finish; + } + + r = jbd_recover(jbd_fs); + jbd_put_fs(jbd_fs); + ext4_free(jbd_fs); + } + if (r == EOK && !mp->fs.read_only) { + uint32_t bgid; + uint64_t free_blocks_count = 0; + uint32_t free_inodes_count = 0; + struct ext4_block_group_ref bg_ref; + + /* Update superblock's stats */ + for (bgid = 0;bgid < ext4_block_group_cnt(&mp->fs.sb);bgid++) { + r = ext4_fs_get_block_group_ref(&mp->fs, bgid, &bg_ref); + if (r != EOK) + goto Finish; + + free_blocks_count += + ext4_bg_get_free_blocks_count(bg_ref.block_group, + &mp->fs.sb); + free_inodes_count += + ext4_bg_get_free_inodes_count(bg_ref.block_group, + &mp->fs.sb); + + ext4_fs_put_block_group_ref(&bg_ref); + } + ext4_sb_set_free_blocks_cnt(&mp->fs.sb, free_blocks_count); + ext4_set32(&mp->fs.sb, free_inodes_count, free_inodes_count); + /* We don't need to save the superblock stats immediately. */ + } + +Finish: + EXT4_MP_UNLOCK(mp); + return r; +} + +__unused +static int __ext4_trans_start(struct ext4_mountpoint *mp) +{ + int r = EOK; + + if (mp->fs.jbd_journal && !mp->fs.curr_trans) { + struct jbd_journal *journal = mp->fs.jbd_journal; + struct jbd_trans *trans; + trans = jbd_journal_new_trans(journal); + if (!trans) { + r = ENOMEM; + goto Finish; + } + mp->fs.curr_trans = trans; + } +Finish: + return r; +} + +__unused +static int __ext4_trans_stop(struct ext4_mountpoint *mp) +{ + int r = EOK; + + if (mp->fs.jbd_journal && mp->fs.curr_trans) { + struct jbd_journal *journal = mp->fs.jbd_journal; + struct jbd_trans *trans = mp->fs.curr_trans; + r = jbd_journal_commit_trans(journal, trans); + mp->fs.curr_trans = NULL; + } + return r; +} + +__unused +static void __ext4_trans_abort(struct ext4_mountpoint *mp) +{ + if (mp->fs.jbd_journal && mp->fs.curr_trans) { + struct jbd_journal *journal = mp->fs.jbd_journal; + struct jbd_trans *trans = mp->fs.curr_trans; + jbd_journal_free_trans(journal, trans, true); + mp->fs.curr_trans = NULL; + } +} + +int ext4_journal_start(const char *mount_point __unused) +{ + int r = EOK; +#if CONFIG_JOURNALING_ENABLE + r = __ext4_journal_start(mount_point); +#endif + return r; +} + +int ext4_journal_stop(const char *mount_point __unused) +{ + int r = EOK; +#if CONFIG_JOURNALING_ENABLE + r = __ext4_journal_stop(mount_point); +#endif + return r; +} + +int ext4_recover(const char *mount_point __unused) +{ + int r = EOK; +#if CONFIG_JOURNALING_ENABLE + r = __ext4_recover(mount_point); +#endif + return r; +} + +static int ext4_trans_start(struct ext4_mountpoint *mp __unused) +{ + int r = EOK; +#if CONFIG_JOURNALING_ENABLE + r = __ext4_trans_start(mp); +#endif + return r; +} + +static int ext4_trans_stop(struct ext4_mountpoint *mp __unused) +{ + int r = EOK; +#if CONFIG_JOURNALING_ENABLE + r = __ext4_trans_stop(mp); +#endif + return r; +} + +static void ext4_trans_abort(struct ext4_mountpoint *mp __unused) +{ +#if CONFIG_JOURNALING_ENABLE + __ext4_trans_abort(mp); +#endif +} + + +int ext4_mount_point_stats(const char *mount_point, + struct ext4_mount_stats *stats) +{ + struct ext4_mountpoint *mp = ext4_get_mount(mount_point); + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + stats->inodes_count = ext4_get32(&mp->fs.sb, inodes_count); + stats->free_inodes_count = ext4_get32(&mp->fs.sb, free_inodes_count); + stats->blocks_count = ext4_sb_get_blocks_cnt(&mp->fs.sb); + stats->free_blocks_count = ext4_sb_get_free_blocks_cnt(&mp->fs.sb); + stats->block_size = ext4_sb_get_block_size(&mp->fs.sb); + + stats->block_group_count = ext4_block_group_cnt(&mp->fs.sb); + stats->blocks_per_group = ext4_get32(&mp->fs.sb, blocks_per_group); + stats->inodes_per_group = ext4_get32(&mp->fs.sb, inodes_per_group); + + memcpy(stats->volume_name, mp->fs.sb.volume_name, 16); + EXT4_MP_UNLOCK(mp); + + return EOK; +} + +int ext4_mount_setup_locks(const char *mount_point, + const struct ext4_lock *locks) +{ + uint32_t i; + struct ext4_mountpoint *mp = 0; + + for (i = 0; i < CONFIG_EXT4_MOUNTPOINTS_COUNT; ++i) { + if (!strcmp(s_mp[i].name, mount_point)) { + mp = &s_mp[i]; + break; + } + } + if (!mp) + return ENOENT; + + mp->os_locks = locks; + return EOK; +} + +/********************************FILE OPERATIONS*****************************/ + +static int ext4_path_check(const char *path, bool *is_goal) +{ + int i; + + for (i = 0; i < EXT4_DIRECTORY_FILENAME_LEN; ++i) { + + if (path[i] == '/') { + *is_goal = false; + return i; + } + + if (path[i] == 0) { + *is_goal = true; + return i; + } + } + + return 0; +} + +static bool ext4_parse_flags(const char *flags, uint32_t *file_flags) +{ + if (!flags) + return false; + + if (!strcmp(flags, "r") || !strcmp(flags, "rb")) { + *file_flags = O_RDONLY; + return true; + } + + if (!strcmp(flags, "w") || !strcmp(flags, "wb")) { + *file_flags = O_WRONLY | O_CREAT | O_TRUNC; + return true; + } + + if (!strcmp(flags, "a") || !strcmp(flags, "ab")) { + *file_flags = O_WRONLY | O_CREAT | O_APPEND; + return true; + } + + if (!strcmp(flags, "r+") || !strcmp(flags, "rb+") || + !strcmp(flags, "r+b")) { + *file_flags = O_RDWR; + return true; + } + + if (!strcmp(flags, "w+") || !strcmp(flags, "wb+") || + !strcmp(flags, "w+b")) { + *file_flags = O_RDWR | O_CREAT | O_TRUNC; + return true; + } + + if (!strcmp(flags, "a+") || !strcmp(flags, "ab+") || + !strcmp(flags, "a+b")) { + *file_flags = O_RDWR | O_CREAT | O_APPEND; + return true; + } + + return false; +} + +static int ext4_trunc_inode(struct ext4_mountpoint *mp, + uint32_t index, uint64_t new_size) +{ + int r = EOK; + struct ext4_fs *const fs = &mp->fs; + struct ext4_inode_ref inode_ref; + uint64_t inode_size; + bool has_trans = mp->fs.jbd_journal && mp->fs.curr_trans; + r = ext4_fs_get_inode_ref(fs, index, &inode_ref); + if (r != EOK) + return r; + + inode_size = ext4_inode_get_size(&fs->sb, inode_ref.inode); + ext4_fs_put_inode_ref(&inode_ref); + if (has_trans) + ext4_trans_stop(mp); + + while (inode_size > new_size + CONFIG_MAX_TRUNCATE_SIZE) { + + inode_size -= CONFIG_MAX_TRUNCATE_SIZE; + + ext4_trans_start(mp); + r = ext4_fs_get_inode_ref(fs, index, &inode_ref); + if (r != EOK) { + ext4_trans_abort(mp); + break; + } + r = ext4_fs_truncate_inode(&inode_ref, inode_size); + if (r != EOK) + ext4_fs_put_inode_ref(&inode_ref); + else + r = ext4_fs_put_inode_ref(&inode_ref); + + if (r != EOK) { + ext4_trans_abort(mp); + goto Finish; + } else + ext4_trans_stop(mp); + } + + if (inode_size > new_size) { + + inode_size = new_size; + + ext4_trans_start(mp); + r = ext4_fs_get_inode_ref(fs, index, &inode_ref); + if (r != EOK) { + ext4_trans_abort(mp); + goto Finish; + } + r = ext4_fs_truncate_inode(&inode_ref, inode_size); + if (r != EOK) + ext4_fs_put_inode_ref(&inode_ref); + else + r = ext4_fs_put_inode_ref(&inode_ref); + + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + + } + +Finish: + + if (has_trans) + ext4_trans_start(mp); + + return r; +} + +static int ext4_trunc_dir(struct ext4_mountpoint *mp, + struct ext4_inode_ref *parent, + struct ext4_inode_ref *dir) +{ + int r = EOK; + bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode, + EXT4_INODE_MODE_DIRECTORY); + uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb); + if (!is_dir) + return EINVAL; + +#if CONFIG_DIR_INDEX_ENABLE + /* Initialize directory index if supported */ + if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) { + r = ext4_dir_dx_init(dir, parent); + if (r != EOK) + return r; + + r = ext4_trunc_inode(mp, dir->index, + EXT4_DIR_DX_INIT_BCNT * block_size); + if (r != EOK) + return r; + } else +#endif + { + r = ext4_trunc_inode(mp, dir->index, block_size); + if (r != EOK) + return r; + } + + return ext4_fs_truncate_inode(dir, 0); +} + +/* + * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN, + * any filetype of the target dir entry will be accepted. + */ +static int ext4_generic_open2(ext4_file *f, const char *path, int flags, + int ftype, uint32_t *parent_inode, + uint32_t *name_off) +{ + bool is_goal = false; + uint32_t imode = EXT4_INODE_MODE_DIRECTORY; + uint32_t next_inode; + + int r; + int len; + struct ext4_mountpoint *mp = ext4_get_mount(path); + struct ext4_dir_search_result result; + struct ext4_inode_ref ref; + + f->mp = 0; + + if (!mp) + return ENOENT; + + struct ext4_fs *const fs = &mp->fs; + struct ext4_sblock *const sb = &mp->fs.sb; + + if (fs->read_only && flags & O_CREAT) + return EROFS; + + f->flags = flags; + + /*Skip mount point*/ + path += strlen(mp->name); + + if (name_off) + *name_off = strlen(mp->name); + + /*Load root*/ + r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref); + if (r != EOK) + return r; + + if (parent_inode) + *parent_inode = ref.index; + + len = ext4_path_check(path, &is_goal); + while (1) { + + len = ext4_path_check(path, &is_goal); + if (!len) { + /*If root open was request.*/ + if (ftype == EXT4_DE_DIR || ftype == EXT4_DE_UNKNOWN) + if (is_goal) + break; + + r = ENOENT; + break; + } + + r = ext4_dir_find_entry(&result, &ref, path, len); + if (r != EOK) { + + /*Destroy last result*/ + ext4_dir_destroy_result(&ref, &result); + if (r != ENOENT) + break; + + if (!(f->flags & O_CREAT)) + break; + + /*O_CREAT allows create new entry*/ + struct ext4_inode_ref child_ref; + r = ext4_fs_alloc_inode(fs, &child_ref, + is_goal ? ftype : EXT4_DE_DIR); + + if (r != EOK) + break; + + ext4_fs_inode_blocks_init(fs, &child_ref); + + /*Link with root dir.*/ + r = ext4_link(mp, &ref, &child_ref, path, len, false); + if (r != EOK) { + /*Fail. Free new inode.*/ + ext4_fs_free_inode(&child_ref); + /*We do not want to write new inode. + But block has to be released.*/ + child_ref.dirty = false; + ext4_fs_put_inode_ref(&child_ref); + break; + } + + ext4_fs_put_inode_ref(&child_ref); + continue; + } + + if (parent_inode) + *parent_inode = ref.index; + + next_inode = ext4_dir_en_get_inode(result.dentry); + if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) { + uint8_t t; + t = ext4_dir_en_get_inode_type(sb, result.dentry); + imode = ext4_fs_correspond_inode_mode(t); + } else { + struct ext4_inode_ref child_ref; + r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref); + if (r != EOK) + break; + + imode = ext4_inode_type(sb, child_ref.inode); + ext4_fs_put_inode_ref(&child_ref); + } + + r = ext4_dir_destroy_result(&ref, &result); + if (r != EOK) + break; + + /*If expected file error*/ + if (imode != EXT4_INODE_MODE_DIRECTORY && !is_goal) { + r = ENOENT; + break; + } + if (ftype != EXT4_DE_UNKNOWN) { + bool df = imode != ext4_fs_correspond_inode_mode(ftype); + if (df && is_goal) { + r = ENOENT; + break; + } + } + + r = ext4_fs_put_inode_ref(&ref); + if (r != EOK) + break; + + r = ext4_fs_get_inode_ref(fs, next_inode, &ref); + if (r != EOK) + break; + + if (is_goal) + break; + + path += len + 1; + + if (name_off) + *name_off += len + 1; + } + + if (r != EOK) { + ext4_fs_put_inode_ref(&ref); + return r; + } + + if (is_goal) { + + if ((f->flags & O_TRUNC) && (imode == EXT4_INODE_MODE_FILE)) { + r = ext4_trunc_inode(mp, ref.index, 0); + if (r != EOK) { + ext4_fs_put_inode_ref(&ref); + return r; + } + } + + f->mp = mp; + f->fsize = ext4_inode_get_size(sb, ref.inode); + f->inode = ref.index; + f->fpos = 0; + + if (f->flags & O_APPEND) + f->fpos = f->fsize; + } + + return ext4_fs_put_inode_ref(&ref); +} + +/****************************************************************************/ + +static int ext4_generic_open(ext4_file *f, const char *path, const char *flags, + bool file_expect, uint32_t *parent_inode, + uint32_t *name_off) +{ + uint32_t iflags; + int filetype; + int r; + struct ext4_mountpoint *mp = ext4_get_mount(path); + + if (ext4_parse_flags(flags, &iflags) == false) + return EINVAL; + + if (file_expect == true) + filetype = EXT4_DE_REG_FILE; + else + filetype = EXT4_DE_DIR; + + if (iflags & O_CREAT) + ext4_trans_start(mp); + + r = ext4_generic_open2(f, path, iflags, filetype, parent_inode, + name_off); + + if (iflags & O_CREAT) { + if (r == EOK) + ext4_trans_stop(mp); + else + ext4_trans_abort(mp); + } + + return r; +} + +static int ext4_create_hardlink(const char *path, + struct ext4_inode_ref *child_ref, bool rename) +{ + bool is_goal = false; + uint32_t inode_mode = EXT4_INODE_MODE_DIRECTORY; + uint32_t next_inode; + + int r; + int len; + struct ext4_mountpoint *mp = ext4_get_mount(path); + struct ext4_dir_search_result result; + struct ext4_inode_ref ref; + + if (!mp) + return ENOENT; + + struct ext4_fs *const fs = &mp->fs; + struct ext4_sblock *const sb = &mp->fs.sb; + + /*Skip mount point*/ + path += strlen(mp->name); + + /*Load root*/ + r = ext4_fs_get_inode_ref(fs, EXT4_INODE_ROOT_INDEX, &ref); + if (r != EOK) + return r; + + len = ext4_path_check(path, &is_goal); + while (1) { + + len = ext4_path_check(path, &is_goal); + if (!len) { + /*If root open was request.*/ + r = is_goal ? EINVAL : ENOENT; + break; + } + + r = ext4_dir_find_entry(&result, &ref, path, len); + if (r != EOK) { + + /*Destroy last result*/ + ext4_dir_destroy_result(&ref, &result); + + if (r != ENOENT || !is_goal) + break; + + /*Link with root dir.*/ + r = ext4_link(mp, &ref, child_ref, path, len, rename); + break; + } else if (r == EOK && is_goal) { + /*Destroy last result*/ + ext4_dir_destroy_result(&ref, &result); + r = EEXIST; + break; + } + + next_inode = result.dentry->inode; + if (ext4_sb_feature_incom(sb, EXT4_FINCOM_FILETYPE)) { + uint8_t t; + t = ext4_dir_en_get_inode_type(sb, result.dentry); + inode_mode = ext4_fs_correspond_inode_mode(t); + } else { + struct ext4_inode_ref child_ref; + r = ext4_fs_get_inode_ref(fs, next_inode, &child_ref); + if (r != EOK) + break; + + inode_mode = ext4_inode_type(sb, child_ref.inode); + ext4_fs_put_inode_ref(&child_ref); + } + + r = ext4_dir_destroy_result(&ref, &result); + if (r != EOK) + break; + + if (inode_mode != EXT4_INODE_MODE_DIRECTORY) { + r = is_goal ? EEXIST : ENOENT; + break; + } + + r = ext4_fs_put_inode_ref(&ref); + if (r != EOK) + break; + + r = ext4_fs_get_inode_ref(fs, next_inode, &ref); + if (r != EOK) + break; + + if (is_goal) + break; + + path += len + 1; + }; + + if (r != EOK) { + ext4_fs_put_inode_ref(&ref); + return r; + } + + r = ext4_fs_put_inode_ref(&ref); + return r; +} + +static int ext4_remove_orig_reference(const char *path, uint32_t name_off, + struct ext4_inode_ref *parent_ref, + struct ext4_inode_ref *child_ref) +{ + bool is_goal; + int r; + int len; + struct ext4_mountpoint *mp = ext4_get_mount(path); + + if (!mp) + return ENOENT; + + /*Set path*/ + path += name_off; + + len = ext4_path_check(path, &is_goal); + + /* Remove entry from parent directory */ + r = ext4_dir_remove_entry(parent_ref, path, len); + if (r != EOK) + goto Finish; + + if (ext4_inode_is_type(&mp->fs.sb, child_ref->inode, + EXT4_INODE_MODE_DIRECTORY)) { + ext4_fs_inode_links_count_dec(parent_ref); + parent_ref->dirty = true; + } +Finish: + return r; +} + +int ext4_flink(const char *path, const char *hardlink_path) +{ + int r; + ext4_file f; + uint32_t name_off; + bool child_loaded = false; + uint32_t parent_inode, child_inode; + struct ext4_mountpoint *mp = ext4_get_mount(path); + struct ext4_mountpoint *target_mp = ext4_get_mount(hardlink_path); + struct ext4_inode_ref child_ref; + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + /* Will that happen? Anyway return EINVAL for such case. */ + if (mp != target_mp) + return EINVAL; + + EXT4_MP_LOCK(mp); + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, + &parent_inode, &name_off); + if (r != EOK) { + EXT4_MP_UNLOCK(mp); + return r; + } + + child_inode = f.inode; + ext4_fclose(&f); + ext4_trans_start(mp); + + /*We have file to unlink. Load it.*/ + r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref); + if (r != EOK) + goto Finish; + + child_loaded = true; + + /* Creating hardlink for directory is not allowed. */ + if (ext4_inode_is_type(&mp->fs.sb, child_ref.inode, + EXT4_INODE_MODE_DIRECTORY)) { + r = EINVAL; + goto Finish; + } + + r = ext4_create_hardlink(hardlink_path, &child_ref, false); + +Finish: + if (child_loaded) + ext4_fs_put_inode_ref(&child_ref); + + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + + EXT4_MP_UNLOCK(mp); + return r; + +} + +int ext4_frename(const char *path, const char *new_path) +{ + int r; + ext4_file f; + uint32_t name_off; + bool parent_loaded = false, child_loaded = false; + uint32_t parent_inode, child_inode; + struct ext4_mountpoint *mp = ext4_get_mount(path); + struct ext4_inode_ref child_ref, parent_ref; + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + EXT4_MP_LOCK(mp); + + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, + &parent_inode, &name_off); + if (r != EOK) { + EXT4_MP_UNLOCK(mp); + return r; + } + + child_inode = f.inode; + ext4_fclose(&f); + ext4_trans_start(mp); + + /*Load parent*/ + r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent_ref); + if (r != EOK) + goto Finish; + + parent_loaded = true; + + /*We have file to unlink. Load it.*/ + r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child_ref); + if (r != EOK) + goto Finish; + + child_loaded = true; + + r = ext4_create_hardlink(new_path, &child_ref, true); + if (r != EOK) + goto Finish; + + r = ext4_remove_orig_reference(path, name_off, &parent_ref, &child_ref); + if (r != EOK) + goto Finish; + +Finish: + if (parent_loaded) + ext4_fs_put_inode_ref(&parent_ref); + + if (child_loaded) + ext4_fs_put_inode_ref(&child_ref); + + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + + EXT4_MP_UNLOCK(mp); + return r; + +} + +/****************************************************************************/ + +int ext4_get_sblock(const char *mount_point, struct ext4_sblock **sb) +{ + struct ext4_mountpoint *mp = ext4_get_mount(mount_point); + + if (!mp) + return ENOENT; + + *sb = &mp->fs.sb; + return EOK; +} + +int ext4_cache_write_back(const char *path, bool on) +{ + struct ext4_mountpoint *mp = ext4_get_mount(path); + int ret; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + ret = ext4_block_cache_write_back(mp->fs.bdev, on); + EXT4_MP_UNLOCK(mp); + return ret; +} + +int ext4_cache_flush(const char *path) +{ + struct ext4_mountpoint *mp = ext4_get_mount(path); + int ret; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + ret = ext4_block_cache_flush(mp->fs.bdev); + EXT4_MP_UNLOCK(mp); + return ret; +} + +int ext4_fremove(const char *path) +{ + ext4_file f; + uint32_t parent_inode; + uint32_t child_inode; + uint32_t name_off; + bool is_goal; + int r; + int len; + struct ext4_inode_ref child; + struct ext4_inode_ref parent; + struct ext4_mountpoint *mp = ext4_get_mount(path); + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + EXT4_MP_LOCK(mp); + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, + &parent_inode, &name_off); + if (r != EOK) { + EXT4_MP_UNLOCK(mp); + return r; + } + + child_inode = f.inode; + ext4_fclose(&f); + ext4_trans_start(mp); + + /*Load parent*/ + r = ext4_fs_get_inode_ref(&mp->fs, parent_inode, &parent); + if (r != EOK) { + ext4_trans_abort(mp); + EXT4_MP_UNLOCK(mp); + return r; + } + + /*We have file to delete. Load it.*/ + r = ext4_fs_get_inode_ref(&mp->fs, child_inode, &child); + if (r != EOK) { + ext4_fs_put_inode_ref(&parent); + ext4_trans_abort(mp); + EXT4_MP_UNLOCK(mp); + return r; + } + /* We do not allow opening files here. */ + if (ext4_inode_type(&mp->fs.sb, child.inode) == + EXT4_INODE_MODE_DIRECTORY) { + ext4_fs_put_inode_ref(&parent); + ext4_fs_put_inode_ref(&child); + ext4_trans_abort(mp); + EXT4_MP_UNLOCK(mp); + return r; + } + + /*Link count will be zero, the inode should be freed. */ + if (ext4_inode_get_links_cnt(child.inode) == 1) { + ext4_block_cache_write_back(mp->fs.bdev, 1); + r = ext4_trunc_inode(mp, child.index, 0); + if (r != EOK) { + ext4_fs_put_inode_ref(&parent); + ext4_fs_put_inode_ref(&child); + ext4_trans_abort(mp); + EXT4_MP_UNLOCK(mp); + return r; + } + ext4_block_cache_write_back(mp->fs.bdev, 0); + } + + /*Set path*/ + path += name_off; + + len = ext4_path_check(path, &is_goal); + + /*Unlink from parent*/ + r = ext4_unlink(mp, &parent, &child, path, len); + if (r != EOK) + goto Finish; + + /*Link count is zero, the inode should be freed. */ + if (!ext4_inode_get_links_cnt(child.inode)) { + ext4_inode_set_del_time(child.inode, -1L); + + r = ext4_fs_free_inode(&child); + if (r != EOK) + goto Finish; + } + +Finish: + ext4_fs_put_inode_ref(&child); + ext4_fs_put_inode_ref(&parent); + + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + + EXT4_MP_UNLOCK(mp); + return r; +} + +int ext4_fopen(ext4_file *file, const char *path, const char *flags) +{ + struct ext4_mountpoint *mp = ext4_get_mount(path); + int r; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + + ext4_block_cache_write_back(mp->fs.bdev, 1); + r = ext4_generic_open(file, path, flags, true, 0, 0); + ext4_block_cache_write_back(mp->fs.bdev, 0); + + EXT4_MP_UNLOCK(mp); + return r; +} + +int ext4_fopen2(ext4_file *file, const char *path, int flags) +{ + struct ext4_mountpoint *mp = ext4_get_mount(path); + int r; + int filetype; + + if (!mp) + return ENOENT; + + filetype = EXT4_DE_REG_FILE; + + EXT4_MP_LOCK(mp); + ext4_block_cache_write_back(mp->fs.bdev, 1); + + if (flags & O_CREAT) + ext4_trans_start(mp); + + r = ext4_generic_open2(file, path, flags, filetype, NULL, NULL); + + if (flags & O_CREAT) { + if (r == EOK) + ext4_trans_stop(mp); + else + ext4_trans_abort(mp); + } + + ext4_block_cache_write_back(mp->fs.bdev, 0); + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_fclose(ext4_file *file) +{ + ext4_assert(file && file->mp); + + file->mp = 0; + file->flags = 0; + file->inode = 0; + file->fpos = file->fsize = 0; + + return EOK; +} + +static int ext4_ftruncate_no_lock(ext4_file *file, uint64_t size) +{ + struct ext4_inode_ref ref; + int r; + + + r = ext4_fs_get_inode_ref(&file->mp->fs, file->inode, &ref); + if (r != EOK) { + EXT4_MP_UNLOCK(file->mp); + return r; + } + + /*Sync file size*/ + file->fsize = ext4_inode_get_size(&file->mp->fs.sb, ref.inode); + if (file->fsize <= size) { + r = EOK; + goto Finish; + } + + /*Start write back cache mode.*/ + r = ext4_block_cache_write_back(file->mp->fs.bdev, 1); + if (r != EOK) + goto Finish; + + r = ext4_trunc_inode(file->mp, ref.index, size); + if (r != EOK) + goto Finish; + + file->fsize = size; + if (file->fpos > size) + file->fpos = size; + + /*Stop write back cache mode*/ + ext4_block_cache_write_back(file->mp->fs.bdev, 0); + + if (r != EOK) + goto Finish; + +Finish: + ext4_fs_put_inode_ref(&ref); + return r; + +} + +int ext4_ftruncate(ext4_file *f, uint64_t size) +{ + int r; + ext4_assert(f && f->mp); + + if (f->mp->fs.read_only) + return EROFS; + + if (f->flags & O_RDONLY) + return EPERM; + + EXT4_MP_LOCK(f->mp); + + ext4_trans_start(f->mp); + r = ext4_ftruncate_no_lock(f, size); + if (r != EOK) + ext4_trans_abort(f->mp); + else + ext4_trans_stop(f->mp); + + EXT4_MP_UNLOCK(f->mp); + return r; +} + +int ext4_fread(ext4_file *file, void *buf, size_t size, size_t *rcnt) +{ + uint32_t unalg; + uint32_t iblock_idx; + uint32_t iblock_last; + uint32_t block_size; + + ext4_fsblk_t fblock; + ext4_fsblk_t fblock_start; + uint32_t fblock_count; + + uint8_t *u8_buf = buf; + int r; + struct ext4_inode_ref ref; + + ext4_assert(file && file->mp); + + if (file->flags & O_WRONLY) + return EPERM; + + if (!size) + return EOK; + + EXT4_MP_LOCK(file->mp); + + struct ext4_fs *const fs = &file->mp->fs; + struct ext4_sblock *const sb = &file->mp->fs.sb; + + if (rcnt) + *rcnt = 0; + + r = ext4_fs_get_inode_ref(fs, file->inode, &ref); + if (r != EOK) { + EXT4_MP_UNLOCK(file->mp); + return r; + } + + /*Sync file size*/ + file->fsize = ext4_inode_get_size(sb, ref.inode); + + block_size = ext4_sb_get_block_size(sb); + size = ((uint64_t)size > (file->fsize - file->fpos)) + ? ((size_t)(file->fsize - file->fpos)) : size; + + iblock_idx = (uint32_t)((file->fpos) / block_size); + iblock_last = (uint32_t)((file->fpos + size) / block_size); + unalg = (file->fpos) % block_size; + + /*If the size of symlink is smaller than 60 bytes*/ + bool softlink; + softlink = ext4_inode_is_type(sb, ref.inode, EXT4_INODE_MODE_SOFTLINK); + if (softlink && file->fsize < sizeof(ref.inode->blocks) + && !ext4_inode_get_blocks_count(sb, ref.inode)) { + + char *content = (char *)ref.inode->blocks; + if (file->fpos < file->fsize) { + size_t len = size; + if (unalg + size > (uint32_t)file->fsize) + len = (uint32_t)file->fsize - unalg; + memcpy(buf, content + unalg, len); + if (rcnt) + *rcnt = len; + + } + + r = EOK; + goto Finish; + } + + if (unalg) { + size_t len = size; + if (size > (block_size - unalg)) + len = block_size - unalg; + + r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true); + if (r != EOK) + goto Finish; + + /* Do we get an unwritten range? */ + if (fblock != 0) { + uint64_t off = fblock * block_size + unalg; + r = ext4_block_readbytes(file->mp->fs.bdev, off, u8_buf, len); + if (r != EOK) + goto Finish; + + } else { + /* Yes, we do. */ + memset(u8_buf, 0, len); + } + + u8_buf += len; + size -= len; + file->fpos += len; + + if (rcnt) + *rcnt += len; + + iblock_idx++; + } + + fblock_start = 0; + fblock_count = 0; + while (size >= block_size) { + while (iblock_idx < iblock_last) { + r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, + &fblock, true); + if (r != EOK) + goto Finish; + + iblock_idx++; + + if (!fblock_start) + fblock_start = fblock; + + if ((fblock_start + fblock_count) != fblock) + break; + + fblock_count++; + } + + r = ext4_blocks_get_direct(file->mp->fs.bdev, u8_buf, fblock_start, + fblock_count); + if (r != EOK) + goto Finish; + + size -= block_size * fblock_count; + u8_buf += block_size * fblock_count; + file->fpos += block_size * fblock_count; + + if (rcnt) + *rcnt += block_size * fblock_count; + + fblock_start = fblock; + fblock_count = 1; + } + + if (size) { + uint64_t off; + r = ext4_fs_get_inode_dblk_idx(&ref, iblock_idx, &fblock, true); + if (r != EOK) + goto Finish; + + off = fblock * block_size; + r = ext4_block_readbytes(file->mp->fs.bdev, off, u8_buf, size); + if (r != EOK) + goto Finish; + + file->fpos += size; + + if (rcnt) + *rcnt += size; + } + +Finish: + ext4_fs_put_inode_ref(&ref); + EXT4_MP_UNLOCK(file->mp); + return r; +} + +int ext4_fwrite(ext4_file *file, const void *buf, size_t size, size_t *wcnt) +{ + uint32_t unalg; + uint32_t iblk_idx; + uint32_t iblock_last; + uint32_t ifile_blocks; + uint32_t block_size; + + uint32_t fblock_count; + ext4_fsblk_t fblk; + ext4_fsblk_t fblock_start; + + struct ext4_inode_ref ref; + const uint8_t *u8_buf = buf; + int r, rr = EOK; + + ext4_assert(file && file->mp); + + if (file->mp->fs.read_only) + return EROFS; + + if (file->flags & O_RDONLY) + return EPERM; + + if (!size) + return EOK; + + EXT4_MP_LOCK(file->mp); + ext4_trans_start(file->mp); + + struct ext4_fs *const fs = &file->mp->fs; + struct ext4_sblock *const sb = &file->mp->fs.sb; + + if (wcnt) + *wcnt = 0; + + r = ext4_fs_get_inode_ref(fs, file->inode, &ref); + if (r != EOK) { + ext4_trans_abort(file->mp); + EXT4_MP_UNLOCK(file->mp); + return r; + } + + /*Sync file size*/ + file->fsize = ext4_inode_get_size(sb, ref.inode); + block_size = ext4_sb_get_block_size(sb); + + iblock_last = (uint32_t)((file->fpos + size) / block_size); + iblk_idx = (uint32_t)(file->fpos / block_size); + ifile_blocks = (uint32_t)((file->fsize + block_size - 1) / block_size); + + unalg = (file->fpos) % block_size; + + if (unalg) { + size_t len = size; + uint64_t off; + if (size > (block_size - unalg)) + len = block_size - unalg; + + r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk); + if (r != EOK) + goto Finish; + + off = fblk * block_size + unalg; + r = ext4_block_writebytes(file->mp->fs.bdev, off, u8_buf, len); + if (r != EOK) + goto Finish; + + u8_buf += len; + size -= len; + file->fpos += len; + + if (wcnt) + *wcnt += len; + + iblk_idx++; + } + + /*Start write back cache mode.*/ + r = ext4_block_cache_write_back(file->mp->fs.bdev, 1); + if (r != EOK) + goto Finish; + + fblock_start = 0; + fblock_count = 0; + while (size >= block_size) { + + while (iblk_idx < iblock_last) { + if (iblk_idx < ifile_blocks) { + r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, + &fblk); + if (r != EOK) + goto Finish; + } else { + rr = ext4_fs_append_inode_dblk(&ref, &fblk, + &iblk_idx); + if (rr != EOK) { + /* Unable to append more blocks. But + * some block might be allocated already + * */ + break; + } + } + + iblk_idx++; + + if (!fblock_start) { + fblock_start = fblk; + } + + if ((fblock_start + fblock_count) != fblk) + break; + + fblock_count++; + } + + r = ext4_blocks_set_direct(file->mp->fs.bdev, u8_buf, fblock_start, + fblock_count); + if (r != EOK) + break; + + size -= block_size * fblock_count; + u8_buf += block_size * fblock_count; + file->fpos += block_size * fblock_count; + + if (wcnt) + *wcnt += block_size * fblock_count; + + fblock_start = fblk; + fblock_count = 1; + + if (rr != EOK) { + /*ext4_fs_append_inode_block has failed and no + * more blocks might be written. But node size + * should be updated.*/ + r = rr; + goto out_fsize; + } + } + + /*Stop write back cache mode*/ + ext4_block_cache_write_back(file->mp->fs.bdev, 0); + + if (r != EOK) + goto Finish; + + if (size) { + uint64_t off; + if (iblk_idx < ifile_blocks) { + r = ext4_fs_init_inode_dblk_idx(&ref, iblk_idx, &fblk); + if (r != EOK) + goto Finish; + } else { + r = ext4_fs_append_inode_dblk(&ref, &fblk, &iblk_idx); + if (r != EOK) + /*Node size sholud be updated.*/ + goto out_fsize; + } + + off = fblk * block_size; + r = ext4_block_writebytes(file->mp->fs.bdev, off, u8_buf, size); + if (r != EOK) + goto Finish; + + file->fpos += size; + + if (wcnt) + *wcnt += size; + } + +out_fsize: + if (file->fpos > file->fsize) { + file->fsize = file->fpos; + ext4_inode_set_size(ref.inode, file->fsize); + ref.dirty = true; + } + +Finish: + r = ext4_fs_put_inode_ref(&ref); + + if (r != EOK) + ext4_trans_abort(file->mp); + else + ext4_trans_stop(file->mp); + + EXT4_MP_UNLOCK(file->mp); + return r; +} + +int ext4_fseek(ext4_file *file, int64_t offset, uint32_t origin) +{ + switch (origin) { + case SEEK_SET: + if (offset < 0 || (uint64_t)offset > file->fsize) + return EINVAL; + + file->fpos = offset; + return EOK; + case SEEK_CUR: + if ((offset < 0 && (uint64_t)(-offset) > file->fpos) || + (offset > 0 && + (uint64_t)offset > (file->fsize - file->fpos))) + return EINVAL; + + file->fpos += offset; + return EOK; + case SEEK_END: + if (offset < 0 || (uint64_t)offset > file->fsize) + return EINVAL; + + file->fpos = file->fsize - offset; + return EOK; + } + return EINVAL; +} + +uint64_t ext4_ftell(ext4_file *file) +{ + return file->fpos; +} + +uint64_t ext4_fsize(ext4_file *file) +{ + return file->fsize; +} + + +static int ext4_trans_get_inode_ref(const char *path, + struct ext4_mountpoint *mp, + struct ext4_inode_ref *inode_ref) +{ + int r; + ext4_file f; + + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) + return r; + + ext4_trans_start(mp); + + r = ext4_fs_get_inode_ref(&mp->fs, f.inode, inode_ref); + if (r != EOK) { + ext4_trans_abort(mp); + return r; + } + + return r; +} + +static int ext4_trans_put_inode_ref(struct ext4_mountpoint *mp, + struct ext4_inode_ref *inode_ref) +{ + int r; + + r = ext4_fs_put_inode_ref(inode_ref); + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + + return r; +} + + +int ext4_raw_inode_fill(const char *path, uint32_t *ret_ino, + struct ext4_inode *inode) +{ + int r; + ext4_file f; + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) { + EXT4_MP_UNLOCK(mp); + return r; + } + + /*Load parent*/ + r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref); + if (r != EOK) { + EXT4_MP_UNLOCK(mp); + return r; + } + + if (ret_ino) + *ret_ino = f.inode; + + memcpy(inode, inode_ref.inode, sizeof(struct ext4_inode)); + ext4_fs_put_inode_ref(&inode_ref); + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_inode_exist(const char *path, int type) +{ + int r; + ext4_file f; + struct ext4_mountpoint *mp = ext4_get_mount(path); + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + r = ext4_generic_open2(&f, path, O_RDONLY, type, NULL, NULL); + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_mode_set(const char *path, uint32_t mode) +{ + int r; + uint32_t orig_mode; + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + EXT4_MP_LOCK(mp); + + r = ext4_trans_get_inode_ref(path, mp, &inode_ref); + if (r != EOK) + goto Finish; + + orig_mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode); + orig_mode &= ~0xFFF; + orig_mode |= mode & 0xFFF; + ext4_inode_set_mode(&mp->fs.sb, inode_ref.inode, orig_mode); + + inode_ref.dirty = true; + r = ext4_trans_put_inode_ref(mp, &inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_owner_set(const char *path, uint32_t uid, uint32_t gid) +{ + int r; + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + EXT4_MP_LOCK(mp); + + r = ext4_trans_get_inode_ref(path, mp, &inode_ref); + if (r != EOK) + goto Finish; + + ext4_inode_set_uid(inode_ref.inode, uid); + ext4_inode_set_gid(inode_ref.inode, gid); + + inode_ref.dirty = true; + r = ext4_trans_put_inode_ref(mp, &inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_mode_get(const char *path, uint32_t *mode) +{ + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + ext4_file f; + int r; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) + goto Finish; + + r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref); + if (r != EOK) + goto Finish; + + *mode = ext4_inode_get_mode(&mp->fs.sb, inode_ref.inode); + r = ext4_fs_put_inode_ref(&inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_owner_get(const char *path, uint32_t *uid, uint32_t *gid) +{ + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + ext4_file f; + int r; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) + goto Finish; + + r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref); + if (r != EOK) + goto Finish; + + *uid = ext4_inode_get_uid(inode_ref.inode); + *gid = ext4_inode_get_gid(inode_ref.inode); + r = ext4_fs_put_inode_ref(&inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_atime_set(const char *path, uint32_t atime) +{ + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + int r; + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + EXT4_MP_LOCK(mp); + + r = ext4_trans_get_inode_ref(path, mp, &inode_ref); + if (r != EOK) + goto Finish; + + ext4_inode_set_access_time(inode_ref.inode, atime); + inode_ref.dirty = true; + r = ext4_trans_put_inode_ref(mp, &inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_mtime_set(const char *path, uint32_t mtime) +{ + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + int r; + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + EXT4_MP_LOCK(mp); + + r = ext4_trans_get_inode_ref(path, mp, &inode_ref); + if (r != EOK) + goto Finish; + + ext4_inode_set_modif_time(inode_ref.inode, mtime); + inode_ref.dirty = true; + r = ext4_trans_put_inode_ref(mp, &inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_ctime_set(const char *path, uint32_t ctime) +{ + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + int r; + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + EXT4_MP_LOCK(mp); + + r = ext4_trans_get_inode_ref(path, mp, &inode_ref); + if (r != EOK) + goto Finish; + + ext4_inode_set_change_inode_time(inode_ref.inode, ctime); + inode_ref.dirty = true; + r = ext4_trans_put_inode_ref(mp, &inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_atime_get(const char *path, uint32_t *atime) +{ + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + ext4_file f; + int r; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) + goto Finish; + + r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref); + if (r != EOK) + goto Finish; + + *atime = ext4_inode_get_access_time(inode_ref.inode); + r = ext4_fs_put_inode_ref(&inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_mtime_get(const char *path, uint32_t *mtime) +{ + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + ext4_file f; + int r; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) + goto Finish; + + r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref); + if (r != EOK) + goto Finish; + + *mtime = ext4_inode_get_modif_time(inode_ref.inode); + r = ext4_fs_put_inode_ref(&inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_ctime_get(const char *path, uint32_t *ctime) +{ + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + ext4_file f; + int r; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) + goto Finish; + + r = ext4_fs_get_inode_ref(&mp->fs, f.inode, &inode_ref); + if (r != EOK) + goto Finish; + + *ctime = ext4_inode_get_change_inode_time(inode_ref.inode); + r = ext4_fs_put_inode_ref(&inode_ref); + + Finish: + EXT4_MP_UNLOCK(mp); + + return r; +} + +static int ext4_fsymlink_set(ext4_file *f, const void *buf, uint32_t size) +{ + struct ext4_inode_ref ref; + uint32_t sblock; + ext4_fsblk_t fblock; + uint32_t block_size; + int r; + + ext4_assert(f && f->mp); + + if (!size) + return EOK; + + r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref); + if (r != EOK) + return r; + + /*Sync file size*/ + block_size = ext4_sb_get_block_size(&f->mp->fs.sb); + if (size > block_size) { + r = EINVAL; + goto Finish; + } + r = ext4_ftruncate_no_lock(f, 0); + if (r != EOK) + goto Finish; + + /*Start write back cache mode.*/ + r = ext4_block_cache_write_back(f->mp->fs.bdev, 1); + if (r != EOK) + goto Finish; + + /*If the size of symlink is smaller than 60 bytes*/ + if (size < sizeof(ref.inode->blocks)) { + memset(ref.inode->blocks, 0, sizeof(ref.inode->blocks)); + memcpy(ref.inode->blocks, buf, size); + ext4_inode_clear_flag(ref.inode, EXT4_INODE_FLAG_EXTENTS); + } else { + uint64_t off; + ext4_fs_inode_blocks_init(&f->mp->fs, &ref); + r = ext4_fs_append_inode_dblk(&ref, &fblock, &sblock); + if (r != EOK) + goto Finish; + + off = fblock * block_size; + r = ext4_block_writebytes(f->mp->fs.bdev, off, buf, size); + if (r != EOK) + goto Finish; + } + + /*Stop write back cache mode*/ + ext4_block_cache_write_back(f->mp->fs.bdev, 0); + + if (r != EOK) + goto Finish; + + ext4_inode_set_size(ref.inode, size); + ref.dirty = true; + + f->fsize = size; + if (f->fpos > size) + f->fpos = size; + +Finish: + ext4_fs_put_inode_ref(&ref); + return r; +} + +int ext4_fsymlink(const char *target, const char *path) +{ + struct ext4_mountpoint *mp = ext4_get_mount(path); + int r; + ext4_file f; + int filetype; + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + filetype = EXT4_DE_SYMLINK; + + EXT4_MP_LOCK(mp); + ext4_block_cache_write_back(mp->fs.bdev, 1); + ext4_trans_start(mp); + + r = ext4_generic_open2(&f, path, O_RDWR | O_CREAT, filetype, NULL, NULL); + if (r == EOK) + r = ext4_fsymlink_set(&f, target, strlen(target)); + else + goto Finish; + + ext4_fclose(&f); + +Finish: + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + + ext4_block_cache_write_back(mp->fs.bdev, 0); + EXT4_MP_UNLOCK(mp); + return r; +} + +int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt) +{ + struct ext4_mountpoint *mp = ext4_get_mount(path); + int r; + ext4_file f; + int filetype; + + if (!mp) + return ENOENT; + + if (!buf) + return EINVAL; + + filetype = EXT4_DE_SYMLINK; + + EXT4_MP_LOCK(mp); + ext4_block_cache_write_back(mp->fs.bdev, 1); + r = ext4_generic_open2(&f, path, O_RDONLY, filetype, NULL, NULL); + if (r == EOK) + r = ext4_fread(&f, buf, bufsize, rcnt); + else + goto Finish; + + ext4_fclose(&f); + +Finish: + ext4_block_cache_write_back(mp->fs.bdev, 0); + EXT4_MP_UNLOCK(mp); + return r; +} + +static int ext4_mknod_set(ext4_file *f, uint32_t dev) +{ + struct ext4_inode_ref ref; + int r; + + ext4_assert(f && f->mp); + + r = ext4_fs_get_inode_ref(&f->mp->fs, f->inode, &ref); + if (r != EOK) + return r; + + ext4_inode_set_dev(ref.inode, dev); + + ext4_inode_set_size(ref.inode, 0); + ref.dirty = true; + + f->fsize = 0; + f->fpos = 0; + + r = ext4_fs_put_inode_ref(&ref); + return r; +} + +int ext4_mknod(const char *path, int filetype, uint32_t dev) +{ + struct ext4_mountpoint *mp = ext4_get_mount(path); + int r; + ext4_file f; + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + /* + * The filetype shouldn't be normal file, directory or + * unknown. + */ + if (filetype == EXT4_DE_UNKNOWN || + filetype == EXT4_DE_REG_FILE || + filetype == EXT4_DE_DIR || + filetype == EXT4_DE_SYMLINK) + return EINVAL; + + /* + * Nor should it be any bogus value. + */ + if (filetype != EXT4_DE_CHRDEV && + filetype != EXT4_DE_BLKDEV && + filetype != EXT4_DE_FIFO && + filetype != EXT4_DE_SOCK) + return EINVAL; + + EXT4_MP_LOCK(mp); + ext4_block_cache_write_back(mp->fs.bdev, 1); + ext4_trans_start(mp); + + r = ext4_generic_open2(&f, path, O_RDWR | O_CREAT, filetype, NULL, NULL); + if (r == EOK) { + if (filetype == EXT4_DE_CHRDEV || + filetype == EXT4_DE_BLKDEV) + r = ext4_mknod_set(&f, dev); + } else { + goto Finish; + } + + ext4_fclose(&f); + +Finish: + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + + ext4_block_cache_write_back(mp->fs.bdev, 0); + EXT4_MP_UNLOCK(mp); + return r; +} + +int ext4_setxattr(const char *path, const char *name, size_t name_len, + const void *data, size_t data_size) +{ + bool found; + int r = EOK; + ext4_file f; + uint32_t inode; + uint8_t name_index; + const char *dissected_name = NULL; + size_t dissected_len = 0; + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + dissected_name = ext4_extract_xattr_name(name, name_len, + &name_index, &dissected_len, + &found); + if (!found) + return EINVAL; + + EXT4_MP_LOCK(mp); + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) { + EXT4_MP_UNLOCK(mp); + return r; + } + + inode = f.inode; + ext4_fclose(&f); + ext4_trans_start(mp); + + r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref); + if (r != EOK) + goto Finish; + + r = ext4_xattr_set(&inode_ref, name_index, dissected_name, + dissected_len, data, data_size); + + ext4_fs_put_inode_ref(&inode_ref); +Finish: + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + + EXT4_MP_UNLOCK(mp); + return r; +} + +int ext4_getxattr(const char *path, const char *name, size_t name_len, + void *buf, size_t buf_size, size_t *data_size) +{ + bool found; + int r = EOK; + ext4_file f; + uint32_t inode; + uint8_t name_index; + const char *dissected_name = NULL; + size_t dissected_len = 0; + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + if (!mp) + return ENOENT; + + dissected_name = ext4_extract_xattr_name(name, name_len, + &name_index, &dissected_len, + &found); + if (!found) + return EINVAL; + + EXT4_MP_LOCK(mp); + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) + goto Finish; + + inode = f.inode; + ext4_fclose(&f); + + r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref); + if (r != EOK) + goto Finish; + + r = ext4_xattr_get(&inode_ref, name_index, dissected_name, + dissected_len, buf, buf_size, data_size); + + ext4_fs_put_inode_ref(&inode_ref); +Finish: + EXT4_MP_UNLOCK(mp); + return r; +} + +int ext4_listxattr(const char *path, char *list, size_t size, size_t *ret_size) +{ + int r = EOK; + ext4_file f; + uint32_t inode; + size_t list_len, list_size = 0; + struct ext4_inode_ref inode_ref; + struct ext4_xattr_list_entry *xattr_list = NULL, + *entry = NULL; + struct ext4_mountpoint *mp = ext4_get_mount(path); + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) + goto Finish; + inode = f.inode; + ext4_fclose(&f); + + r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref); + if (r != EOK) + goto Finish; + + r = ext4_xattr_list(&inode_ref, NULL, &list_len); + if (r == EOK && list_len) { + xattr_list = ext4_malloc(list_len); + if (!xattr_list) { + ext4_fs_put_inode_ref(&inode_ref); + r = ENOMEM; + goto Finish; + } + entry = xattr_list; + r = ext4_xattr_list(&inode_ref, entry, &list_len); + if (r != EOK) { + ext4_fs_put_inode_ref(&inode_ref); + goto Finish; + } + + for (;entry;entry = entry->next) { + size_t prefix_len; + const char *prefix = + ext4_get_xattr_name_prefix(entry->name_index, + &prefix_len); + if (size) { + if (prefix_len + entry->name_len + 1 > size) { + ext4_fs_put_inode_ref(&inode_ref); + r = ERANGE; + goto Finish; + } + } + + if (list && size) { + memcpy(list, prefix, prefix_len); + list += prefix_len; + memcpy(list, entry->name, + entry->name_len); + list[entry->name_len] = 0; + list += entry->name_len + 1; + + size -= prefix_len + entry->name_len + 1; + } + + list_size += prefix_len + entry->name_len + 1; + } + if (ret_size) + *ret_size = list_size; + + } + ext4_fs_put_inode_ref(&inode_ref); +Finish: + EXT4_MP_UNLOCK(mp); + if (xattr_list) + ext4_free(xattr_list); + + return r; + +} + +int ext4_removexattr(const char *path, const char *name, size_t name_len) +{ + bool found; + int r = EOK; + ext4_file f; + uint32_t inode; + uint8_t name_index; + const char *dissected_name = NULL; + size_t dissected_len = 0; + struct ext4_inode_ref inode_ref; + struct ext4_mountpoint *mp = ext4_get_mount(path); + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + dissected_name = ext4_extract_xattr_name(name, name_len, + &name_index, &dissected_len, + &found); + if (!found) + return EINVAL; + + EXT4_MP_LOCK(mp); + r = ext4_generic_open2(&f, path, O_RDONLY, EXT4_DE_UNKNOWN, NULL, NULL); + if (r != EOK) { + EXT4_MP_LOCK(mp); + return r; + } + + inode = f.inode; + ext4_fclose(&f); + ext4_trans_start(mp); + + r = ext4_fs_get_inode_ref(&mp->fs, inode, &inode_ref); + if (r != EOK) + goto Finish; + + r = ext4_xattr_remove(&inode_ref, name_index, dissected_name, + dissected_len); + + ext4_fs_put_inode_ref(&inode_ref); +Finish: + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + + EXT4_MP_UNLOCK(mp); + return r; + +} + +/*********************************DIRECTORY OPERATION************************/ + +int ext4_dir_rm(const char *path) +{ + int r; + int len; + ext4_file f; + + struct ext4_mountpoint *mp = ext4_get_mount(path); + struct ext4_inode_ref act; + struct ext4_inode_ref child; + struct ext4_dir_iter it; + + uint32_t name_off; + uint32_t inode_up; + uint32_t inode_current; + uint32_t depth = 1; + + bool has_children; + bool is_goal; + bool dir_end; + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + EXT4_MP_LOCK(mp); + + struct ext4_fs *const fs = &mp->fs; + + /*Check if exist.*/ + r = ext4_generic_open(&f, path, "r", false, &inode_up, &name_off); + if (r != EOK) { + EXT4_MP_UNLOCK(mp); + return r; + } + + path += name_off; + len = ext4_path_check(path, &is_goal); + inode_current = f.inode; + + ext4_block_cache_write_back(mp->fs.bdev, 1); + + do { + + uint64_t act_curr_pos = 0; + has_children = false; + dir_end = false; + + while (r == EOK && !has_children && !dir_end) { + + /*Load directory node.*/ + r = ext4_fs_get_inode_ref(fs, inode_current, &act); + if (r != EOK) { + break; + } + + /*Initialize iterator.*/ + r = ext4_dir_iterator_init(&it, &act, act_curr_pos); + if (r != EOK) { + ext4_fs_put_inode_ref(&act); + break; + } + + if (!it.curr) { + dir_end = true; + goto End; + } + + ext4_trans_start(mp); + + /*Get up directory inode when ".." entry*/ + if ((it.curr->name_len == 2) && + ext4_is_dots(it.curr->name, it.curr->name_len)) { + inode_up = ext4_dir_en_get_inode(it.curr); + } + + /*If directory or file entry, but not "." ".." entry*/ + if (!ext4_is_dots(it.curr->name, it.curr->name_len)) { + + /*Get child inode reference do unlink + * directory/file.*/ + uint32_t cinode; + uint32_t inode_type; + cinode = ext4_dir_en_get_inode(it.curr); + r = ext4_fs_get_inode_ref(fs, cinode, &child); + if (r != EOK) + goto End; + + /*If directory with no leaf children*/ + r = ext4_has_children(&has_children, &child); + if (r != EOK) { + ext4_fs_put_inode_ref(&child); + goto End; + } + + if (has_children) { + /*Has directory children. Go into this + * directory.*/ + inode_up = inode_current; + inode_current = cinode; + depth++; + ext4_fs_put_inode_ref(&child); + goto End; + } + inode_type = ext4_inode_type(&mp->fs.sb, + child.inode); + + /* Truncate */ + if (inode_type != EXT4_INODE_MODE_DIRECTORY) + r = ext4_trunc_inode(mp, child.index, 0); + else + r = ext4_trunc_dir(mp, &act, &child); + + if (r != EOK) { + ext4_fs_put_inode_ref(&child); + goto End; + } + + /*No children in child directory or file. Just + * unlink.*/ + r = ext4_unlink(f.mp, &act, &child, + (char *)it.curr->name, + it.curr->name_len); + if (r != EOK) { + ext4_fs_put_inode_ref(&child); + goto End; + } + + ext4_inode_set_del_time(child.inode, -1L); + ext4_inode_set_links_cnt(child.inode, 0); + child.dirty = true; + + r = ext4_fs_free_inode(&child); + if (r != EOK) { + ext4_fs_put_inode_ref(&child); + goto End; + } + + r = ext4_fs_put_inode_ref(&child); + if (r != EOK) + goto End; + + } + + r = ext4_dir_iterator_next(&it); + if (r != EOK) + goto End; + + act_curr_pos = it.curr_off; +End: + ext4_dir_iterator_fini(&it); + if (r == EOK) + r = ext4_fs_put_inode_ref(&act); + else + ext4_fs_put_inode_ref(&act); + + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + } + + if (dir_end) { + /*Directory iterator reached last entry*/ + depth--; + if (depth) + inode_current = inode_up; + + } + + if (r != EOK) + break; + + } while (depth); + + /*Last unlink*/ + if (r == EOK && !depth) { + /*Load parent.*/ + struct ext4_inode_ref parent; + r = ext4_fs_get_inode_ref(&f.mp->fs, inode_up, + &parent); + if (r != EOK) + goto Finish; + r = ext4_fs_get_inode_ref(&f.mp->fs, inode_current, + &act); + if (r != EOK) { + ext4_fs_put_inode_ref(&act); + goto Finish; + } + + ext4_trans_start(mp); + + /* In this place all directories should be + * unlinked. + * Last unlink from root of current directory*/ + r = ext4_unlink(f.mp, &parent, &act, + (char *)path, len); + if (r != EOK) { + ext4_fs_put_inode_ref(&parent); + ext4_fs_put_inode_ref(&act); + goto Finish; + } + + if (ext4_inode_get_links_cnt(act.inode) == 2) { + ext4_inode_set_del_time(act.inode, -1L); + ext4_inode_set_links_cnt(act.inode, 0); + act.dirty = true; + /*Truncate*/ + r = ext4_trunc_dir(mp, &parent, &act); + if (r != EOK) { + ext4_fs_put_inode_ref(&parent); + ext4_fs_put_inode_ref(&act); + goto Finish; + } + + r = ext4_fs_free_inode(&act); + if (r != EOK) { + ext4_fs_put_inode_ref(&parent); + ext4_fs_put_inode_ref(&act); + goto Finish; + } + } + + r = ext4_fs_put_inode_ref(&parent); + if (r != EOK) + goto Finish; + + r = ext4_fs_put_inode_ref(&act); + Finish: + if (r != EOK) + ext4_trans_abort(mp); + else + ext4_trans_stop(mp); + } + + ext4_block_cache_write_back(mp->fs.bdev, 0); + EXT4_MP_UNLOCK(mp); + + return r; +} + +int ext4_dir_mv(const char *path, const char *new_path) +{ + return ext4_frename(path, new_path); +} + +int ext4_dir_mk(const char *path) +{ + int r; + ext4_file f; + struct ext4_mountpoint *mp = ext4_get_mount(path); + + if (!mp) + return ENOENT; + + if (mp->fs.read_only) + return EROFS; + + EXT4_MP_LOCK(mp); + + /*Check if exist.*/ + r = ext4_generic_open(&f, path, "r", false, 0, 0); + if (r == EOK) + goto Finish; + + /*Create new directory.*/ + r = ext4_generic_open(&f, path, "w", false, 0, 0); + +Finish: + EXT4_MP_UNLOCK(mp); + return r; +} + +int ext4_dir_open(ext4_dir *dir, const char *path) +{ + struct ext4_mountpoint *mp = ext4_get_mount(path); + int r; + + if (!mp) + return ENOENT; + + EXT4_MP_LOCK(mp); + r = ext4_generic_open(&dir->f, path, "r", false, 0, 0); + dir->next_off = 0; + EXT4_MP_UNLOCK(mp); + return r; +} + +int ext4_dir_close(ext4_dir *dir) +{ + return ext4_fclose(&dir->f); +} + +const ext4_direntry *ext4_dir_entry_next(ext4_dir *dir) +{ +#define EXT4_DIR_ENTRY_OFFSET_TERM (uint64_t)(-1) + + int r; + uint16_t name_length; + ext4_direntry *de = 0; + struct ext4_inode_ref dir_inode; + struct ext4_dir_iter it; + + EXT4_MP_LOCK(dir->f.mp); + + if (dir->next_off == EXT4_DIR_ENTRY_OFFSET_TERM) { + EXT4_MP_UNLOCK(dir->f.mp); + return 0; + } + + r = ext4_fs_get_inode_ref(&dir->f.mp->fs, dir->f.inode, &dir_inode); + if (r != EOK) { + goto Finish; + } + + r = ext4_dir_iterator_init(&it, &dir_inode, dir->next_off); + if (r != EOK) { + ext4_fs_put_inode_ref(&dir_inode); + goto Finish; + } + + memset(&dir->de.name, 0, sizeof(dir->de.name)); + name_length = ext4_dir_en_get_name_len(&dir->f.mp->fs.sb, + it.curr); + memcpy(&dir->de.name, it.curr->name, name_length); + + /* Directly copying the content isn't safe for Big-endian targets*/ + dir->de.inode = ext4_dir_en_get_inode(it.curr); + dir->de.entry_length = ext4_dir_en_get_entry_len(it.curr); + dir->de.name_length = name_length; + dir->de.inode_type = ext4_dir_en_get_inode_type(&dir->f.mp->fs.sb, + it.curr); + + de = &dir->de; + + ext4_dir_iterator_next(&it); + + dir->next_off = it.curr ? it.curr_off : EXT4_DIR_ENTRY_OFFSET_TERM; + + ext4_dir_iterator_fini(&it); + ext4_fs_put_inode_ref(&dir_inode); + +Finish: + EXT4_MP_UNLOCK(dir->f.mp); + return de; +} + +void ext4_dir_entry_rewind(ext4_dir *dir) +{ + dir->next_off = 0; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_balloc.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_balloc.c new file mode 100644 index 0000000..1e70856 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_balloc.c @@ -0,0 +1,665 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_balloc.c + * @brief Physical block allocator. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_trans.h" +#include "ext4_balloc.h" +#include "ext4_super.h" +#include "ext4_crc32.h" +#include "ext4_block_group.h" +#include "ext4_fs.h" +#include "ext4_bitmap.h" +#include "ext4_inode.h" + +/**@brief Compute number of block group from block address. + * @param sb superblock pointer. + * @param baddr Absolute address of block. + * @return Block group index + */ +uint32_t ext4_balloc_get_bgid_of_block(struct ext4_sblock *s, + uint64_t baddr) +{ + if (ext4_get32(s, first_data_block) && baddr) + baddr--; + + return (uint32_t)(baddr / ext4_get32(s, blocks_per_group)); +} + +/**@brief Compute the starting block address of a block group + * @param sb superblock pointer. + * @param bgid block group index + * @return Block address + */ +uint64_t ext4_balloc_get_block_of_bgid(struct ext4_sblock *s, + uint32_t bgid) +{ + uint64_t baddr = 0; + if (ext4_get32(s, first_data_block)) + baddr++; + + baddr += bgid * ext4_get32(s, blocks_per_group); + return baddr; +} + +#if CONFIG_META_CSUM_ENABLE +static uint32_t ext4_balloc_bitmap_csum(struct ext4_sblock *sb, + void *bitmap) +{ + uint32_t checksum = 0; + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + uint32_t blocks_per_group = ext4_get32(sb, blocks_per_group); + + /* First calculate crc32 checksum against fs uuid */ + checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, + sizeof(sb->uuid)); + /* Then calculate crc32 checksum against block_group_desc */ + checksum = ext4_crc32c(checksum, bitmap, blocks_per_group / 8); + } + return checksum; +} +#else +#define ext4_balloc_bitmap_csum(...) 0 +#endif + +void ext4_balloc_set_bitmap_csum(struct ext4_sblock *sb, + struct ext4_bgroup *bg, + void *bitmap __unused) +{ + int desc_size = ext4_sb_get_desc_size(sb); + uint32_t checksum = ext4_balloc_bitmap_csum(sb, bitmap); + uint16_t lo_checksum = to_le16(checksum & 0xFFFF), + hi_checksum = to_le16(checksum >> 16); + + if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + return; + + /* See if we need to assign a 32bit checksum */ + bg->block_bitmap_csum_lo = lo_checksum; + if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->block_bitmap_csum_hi = hi_checksum; + +} + +#if CONFIG_META_CSUM_ENABLE +static bool +ext4_balloc_verify_bitmap_csum(struct ext4_sblock *sb, + struct ext4_bgroup *bg, + void *bitmap __unused) +{ + int desc_size = ext4_sb_get_desc_size(sb); + uint32_t checksum = ext4_balloc_bitmap_csum(sb, bitmap); + uint16_t lo_checksum = to_le16(checksum & 0xFFFF), + hi_checksum = to_le16(checksum >> 16); + + if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + return true; + + if (bg->block_bitmap_csum_lo != lo_checksum) + return false; + + if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE) + if (bg->block_bitmap_csum_hi != hi_checksum) + return false; + + return true; +} +#else +#define ext4_balloc_verify_bitmap_csum(...) true +#endif + +int ext4_balloc_free_block(struct ext4_inode_ref *inode_ref, ext4_fsblk_t baddr) +{ + struct ext4_fs *fs = inode_ref->fs; + struct ext4_sblock *sb = &fs->sb; + + uint32_t bg_id = ext4_balloc_get_bgid_of_block(sb, baddr); + uint32_t index_in_group = ext4_fs_addr_to_idx_bg(sb, baddr); + + /* Load block group reference */ + struct ext4_block_group_ref bg_ref; + int rc = ext4_fs_get_block_group_ref(fs, bg_id, &bg_ref); + if (rc != EOK) + return rc; + + struct ext4_bgroup *bg = bg_ref.block_group; + + /* Load block with bitmap */ + ext4_fsblk_t bitmap_block_addr = + ext4_bg_get_block_bitmap(bg, sb); + + struct ext4_block bitmap_block; + + rc = ext4_trans_block_get(fs->bdev, &bitmap_block, bitmap_block_addr); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + if (!ext4_balloc_verify_bitmap_csum(sb, bg, bitmap_block.data)) { + ext4_dbg(DEBUG_BALLOC, + DBG_WARN "Bitmap checksum failed." + "Group: %" PRIu32"\n", + bg_ref.index); + } + + /* Modify bitmap */ + ext4_bmap_bit_clr(bitmap_block.data, index_in_group); + ext4_balloc_set_bitmap_csum(sb, bg, bitmap_block.data); + ext4_trans_set_block_dirty(bitmap_block.buf); + + /* Release block with bitmap */ + rc = ext4_block_set(fs->bdev, &bitmap_block); + if (rc != EOK) { + /* Error in saving bitmap */ + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + uint32_t block_size = ext4_sb_get_block_size(sb); + + /* Update superblock free blocks count */ + uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb); + sb_free_blocks++; + ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); + + /* Update inode blocks count */ + uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode); + ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE; + ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); + inode_ref->dirty = true; + + /* Update block group free blocks count */ + uint32_t free_blocks = ext4_bg_get_free_blocks_count(bg, sb); + free_blocks++; + ext4_bg_set_free_blocks_count(bg, sb, free_blocks); + + bg_ref.dirty = true; + + rc = ext4_trans_try_revoke_block(fs->bdev, baddr); + if (rc != EOK) { + bg_ref.dirty = false; + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + ext4_bcache_invalidate_lba(fs->bdev->bc, baddr, 1); + /* Release block group reference */ + rc = ext4_fs_put_block_group_ref(&bg_ref); + + return rc; +} + +int ext4_balloc_free_blocks(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t first, uint32_t count) +{ + int rc = EOK; + uint32_t blk_cnt = count; + ext4_fsblk_t start_block = first; + struct ext4_fs *fs = inode_ref->fs; + struct ext4_sblock *sb = &fs->sb; + + /* Compute indexes */ + uint32_t bg_first = ext4_balloc_get_bgid_of_block(sb, first); + + /* Compute indexes */ + uint32_t bg_last = ext4_balloc_get_bgid_of_block(sb, first + count - 1); + + if (!ext4_sb_feature_incom(sb, EXT4_FINCOM_FLEX_BG)) { + /*It is not possible without flex_bg that blocks are continuous + * and and last block belongs to other bg.*/ + if (bg_last != bg_first) { + ext4_dbg(DEBUG_BALLOC, DBG_WARN "FLEX_BG: disabled & " + "bg_last: %"PRIu32" bg_first: %"PRIu32"\n", + bg_last, bg_first); + } + } + + /* Load block group reference */ + struct ext4_block_group_ref bg_ref; + while (bg_first <= bg_last) { + + rc = ext4_fs_get_block_group_ref(fs, bg_first, &bg_ref); + if (rc != EOK) + return rc; + + struct ext4_bgroup *bg = bg_ref.block_group; + + uint32_t idx_in_bg_first; + idx_in_bg_first = ext4_fs_addr_to_idx_bg(sb, first); + + /* Load block with bitmap */ + ext4_fsblk_t bitmap_blk = ext4_bg_get_block_bitmap(bg, sb); + + struct ext4_block blk; + rc = ext4_trans_block_get(fs->bdev, &blk, bitmap_blk); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + if (!ext4_balloc_verify_bitmap_csum(sb, bg, blk.data)) { + ext4_dbg(DEBUG_BALLOC, + DBG_WARN "Bitmap checksum failed." + "Group: %" PRIu32"\n", + bg_ref.index); + } + uint32_t free_cnt; + free_cnt = ext4_sb_get_block_size(sb) * 8 - idx_in_bg_first; + + /*If last block, free only count blocks*/ + free_cnt = count > free_cnt ? free_cnt : count; + + /* Modify bitmap */ + ext4_bmap_bits_free(blk.data, idx_in_bg_first, free_cnt); + ext4_balloc_set_bitmap_csum(sb, bg, blk.data); + ext4_trans_set_block_dirty(blk.buf); + + count -= free_cnt; + first += free_cnt; + + /* Release block with bitmap */ + rc = ext4_block_set(fs->bdev, &blk); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + uint32_t block_size = ext4_sb_get_block_size(sb); + + /* Update superblock free blocks count */ + uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb); + sb_free_blocks += free_cnt; + ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); + + /* Update inode blocks count */ + uint64_t ino_blocks; + ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode); + ino_blocks -= free_cnt * (block_size / EXT4_INODE_BLOCK_SIZE); + ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); + inode_ref->dirty = true; + + /* Update block group free blocks count */ + uint32_t free_blocks; + free_blocks = ext4_bg_get_free_blocks_count(bg, sb); + free_blocks += free_cnt; + ext4_bg_set_free_blocks_count(bg, sb, free_blocks); + bg_ref.dirty = true; + + /* Release block group reference */ + rc = ext4_fs_put_block_group_ref(&bg_ref); + if (rc != EOK) + break; + + bg_first++; + } + + uint32_t i; + for (i = 0;i < blk_cnt;i++) { + rc = ext4_trans_try_revoke_block(fs->bdev, start_block + i); + if (rc != EOK) + return rc; + + } + + ext4_bcache_invalidate_lba(fs->bdev->bc, start_block, blk_cnt); + /*All blocks should be released*/ + ext4_assert(count == 0); + + return rc; +} + +int ext4_balloc_alloc_block(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t goal, + ext4_fsblk_t *fblock) +{ + ext4_fsblk_t alloc = 0; + ext4_fsblk_t bmp_blk_adr; + uint32_t rel_blk_idx = 0; + uint64_t free_blocks; + int r; + struct ext4_sblock *sb = &inode_ref->fs->sb; + + /* Load block group number for goal and relative index */ + uint32_t bg_id = ext4_balloc_get_bgid_of_block(sb, goal); + uint32_t idx_in_bg = ext4_fs_addr_to_idx_bg(sb, goal); + + struct ext4_block b; + struct ext4_block_group_ref bg_ref; + + /* Load block group reference */ + r = ext4_fs_get_block_group_ref(inode_ref->fs, bg_id, &bg_ref); + if (r != EOK) + return r; + + struct ext4_bgroup *bg = bg_ref.block_group; + + free_blocks = ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); + if (free_blocks == 0) { + /* This group has no free blocks */ + goto goal_failed; + } + + /* Compute indexes */ + ext4_fsblk_t first_in_bg; + first_in_bg = ext4_balloc_get_block_of_bgid(sb, bg_ref.index); + + uint32_t first_in_bg_index; + first_in_bg_index = ext4_fs_addr_to_idx_bg(sb, first_in_bg); + + if (idx_in_bg < first_in_bg_index) + idx_in_bg = first_in_bg_index; + + /* Load block with bitmap */ + bmp_blk_adr = ext4_bg_get_block_bitmap(bg_ref.block_group, sb); + + r = ext4_trans_block_get(inode_ref->fs->bdev, &b, bmp_blk_adr); + if (r != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return r; + } + + if (!ext4_balloc_verify_bitmap_csum(sb, bg, b.data)) { + ext4_dbg(DEBUG_BALLOC, + DBG_WARN "Bitmap checksum failed." + "Group: %" PRIu32"\n", + bg_ref.index); + } + + /* Check if goal is free */ + if (ext4_bmap_is_bit_clr(b.data, idx_in_bg)) { + ext4_bmap_bit_set(b.data, idx_in_bg); + ext4_balloc_set_bitmap_csum(sb, bg_ref.block_group, + b.data); + ext4_trans_set_block_dirty(b.buf); + r = ext4_block_set(inode_ref->fs->bdev, &b); + if (r != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return r; + } + + alloc = ext4_fs_bg_idx_to_addr(sb, idx_in_bg, bg_id); + goto success; + } + + uint32_t blk_in_bg = ext4_blocks_in_group_cnt(sb, bg_id); + + uint32_t end_idx = (idx_in_bg + 63) & ~63; + if (end_idx > blk_in_bg) + end_idx = blk_in_bg; + + /* Try to find free block near to goal */ + uint32_t tmp_idx; + for (tmp_idx = idx_in_bg + 1; tmp_idx < end_idx; ++tmp_idx) { + if (ext4_bmap_is_bit_clr(b.data, tmp_idx)) { + ext4_bmap_bit_set(b.data, tmp_idx); + + ext4_balloc_set_bitmap_csum(sb, bg, b.data); + ext4_trans_set_block_dirty(b.buf); + r = ext4_block_set(inode_ref->fs->bdev, &b); + if (r != EOK) + return r; + + alloc = ext4_fs_bg_idx_to_addr(sb, tmp_idx, bg_id); + goto success; + } + } + + /* Find free bit in bitmap */ + r = ext4_bmap_bit_find_clr(b.data, idx_in_bg, blk_in_bg, &rel_blk_idx); + if (r == EOK) { + ext4_bmap_bit_set(b.data, rel_blk_idx); + ext4_balloc_set_bitmap_csum(sb, bg_ref.block_group, b.data); + ext4_trans_set_block_dirty(b.buf); + r = ext4_block_set(inode_ref->fs->bdev, &b); + if (r != EOK) + return r; + + alloc = ext4_fs_bg_idx_to_addr(sb, rel_blk_idx, bg_id); + goto success; + } + + /* No free block found yet */ + r = ext4_block_set(inode_ref->fs->bdev, &b); + if (r != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return r; + } + +goal_failed: + + r = ext4_fs_put_block_group_ref(&bg_ref); + if (r != EOK) + return r; + + /* Try other block groups */ + uint32_t block_group_count = ext4_block_group_cnt(sb); + uint32_t bgid = (bg_id + 1) % block_group_count; + uint32_t count = block_group_count; + + while (count > 0) { + r = ext4_fs_get_block_group_ref(inode_ref->fs, bgid, &bg_ref); + if (r != EOK) + return r; + + struct ext4_bgroup *bg = bg_ref.block_group; + free_blocks = ext4_bg_get_free_blocks_count(bg, sb); + if (free_blocks == 0) { + /* This group has no free blocks */ + goto next_group; + } + + /* Load block with bitmap */ + bmp_blk_adr = ext4_bg_get_block_bitmap(bg, sb); + r = ext4_trans_block_get(inode_ref->fs->bdev, &b, bmp_blk_adr); + if (r != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return r; + } + + if (!ext4_balloc_verify_bitmap_csum(sb, bg, b.data)) { + ext4_dbg(DEBUG_BALLOC, + DBG_WARN "Bitmap checksum failed." + "Group: %" PRIu32"\n", + bg_ref.index); + } + + /* Compute indexes */ + first_in_bg = ext4_balloc_get_block_of_bgid(sb, bgid); + idx_in_bg = ext4_fs_addr_to_idx_bg(sb, first_in_bg); + blk_in_bg = ext4_blocks_in_group_cnt(sb, bgid); + first_in_bg_index = ext4_fs_addr_to_idx_bg(sb, first_in_bg); + + if (idx_in_bg < first_in_bg_index) + idx_in_bg = first_in_bg_index; + + r = ext4_bmap_bit_find_clr(b.data, idx_in_bg, blk_in_bg, + &rel_blk_idx); + if (r == EOK) { + ext4_bmap_bit_set(b.data, rel_blk_idx); + ext4_balloc_set_bitmap_csum(sb, bg, b.data); + ext4_trans_set_block_dirty(b.buf); + r = ext4_block_set(inode_ref->fs->bdev, &b); + if (r != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return r; + } + + alloc = ext4_fs_bg_idx_to_addr(sb, rel_blk_idx, bgid); + goto success; + } + + r = ext4_block_set(inode_ref->fs->bdev, &b); + if (r != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return r; + } + + next_group: + r = ext4_fs_put_block_group_ref(&bg_ref); + if (r != EOK) { + return r; + } + + /* Goto next group */ + bgid = (bgid + 1) % block_group_count; + count--; + } + + return ENOSPC; + +success: + /* Empty command - because of syntax */ + ; + + uint32_t block_size = ext4_sb_get_block_size(sb); + + /* Update superblock free blocks count */ + uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb); + sb_free_blocks--; + ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); + + /* Update inode blocks (different block size!) count */ + uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode); + ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE; + ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); + inode_ref->dirty = true; + + /* Update block group free blocks count */ + + uint32_t fb_cnt = ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); + fb_cnt--; + ext4_bg_set_free_blocks_count(bg_ref.block_group, sb, fb_cnt); + + bg_ref.dirty = true; + r = ext4_fs_put_block_group_ref(&bg_ref); + + *fblock = alloc; + return r; +} + +int ext4_balloc_try_alloc_block(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t baddr, bool *free) +{ + int rc; + + struct ext4_fs *fs = inode_ref->fs; + struct ext4_sblock *sb = &fs->sb; + + /* Compute indexes */ + uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, baddr); + uint32_t index_in_group = ext4_fs_addr_to_idx_bg(sb, baddr); + + /* Load block group reference */ + struct ext4_block_group_ref bg_ref; + rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref); + if (rc != EOK) + return rc; + + /* Load block with bitmap */ + ext4_fsblk_t bmp_blk_addr; + bmp_blk_addr = ext4_bg_get_block_bitmap(bg_ref.block_group, sb); + + struct ext4_block b; + rc = ext4_trans_block_get(fs->bdev, &b, bmp_blk_addr); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + if (!ext4_balloc_verify_bitmap_csum(sb, bg_ref.block_group, b.data)) { + ext4_dbg(DEBUG_BALLOC, + DBG_WARN "Bitmap checksum failed." + "Group: %" PRIu32"\n", + bg_ref.index); + } + + /* Check if block is free */ + *free = ext4_bmap_is_bit_clr(b.data, index_in_group); + + /* Allocate block if possible */ + if (*free) { + ext4_bmap_bit_set(b.data, index_in_group); + ext4_balloc_set_bitmap_csum(sb, bg_ref.block_group, b.data); + ext4_trans_set_block_dirty(b.buf); + } + + /* Release block with bitmap */ + rc = ext4_block_set(fs->bdev, &b); + if (rc != EOK) { + /* Error in saving bitmap */ + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + /* If block is not free, return */ + if (!(*free)) + goto terminate; + + uint32_t block_size = ext4_sb_get_block_size(sb); + + /* Update superblock free blocks count */ + uint64_t sb_free_blocks = ext4_sb_get_free_blocks_cnt(sb); + sb_free_blocks--; + ext4_sb_set_free_blocks_cnt(sb, sb_free_blocks); + + /* Update inode blocks count */ + uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode); + ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE; + ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks); + inode_ref->dirty = true; + + /* Update block group free blocks count */ + uint32_t fb_cnt = ext4_bg_get_free_blocks_count(bg_ref.block_group, sb); + fb_cnt--; + ext4_bg_set_free_blocks_count(bg_ref.block_group, sb, fb_cnt); + + bg_ref.dirty = true; + +terminate: + return ext4_fs_put_block_group_ref(&bg_ref); +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_bcache.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_bcache.c new file mode 100644 index 0000000..8b96825 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_bcache.c @@ -0,0 +1,325 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_bcache.c + * @brief Block cache allocator. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_bcache.h" +#include "ext4_blockdev.h" +#include "ext4_debug.h" +#include "ext4_errno.h" + +#include +#include + +static int ext4_bcache_lba_compare(struct ext4_buf *a, struct ext4_buf *b) +{ + if (a->lba > b->lba) + return 1; + else if (a->lba < b->lba) + return -1; + return 0; +} + +static int ext4_bcache_lru_compare(struct ext4_buf *a, struct ext4_buf *b) +{ + if (a->lru_id > b->lru_id) + return 1; + else if (a->lru_id < b->lru_id) + return -1; + return 0; +} + +RB_GENERATE_INTERNAL(ext4_buf_lba, ext4_buf, lba_node, + ext4_bcache_lba_compare, static inline) +RB_GENERATE_INTERNAL(ext4_buf_lru, ext4_buf, lru_node, + ext4_bcache_lru_compare, static inline) + +int ext4_bcache_init_dynamic(struct ext4_bcache *bc, uint32_t cnt, + uint32_t itemsize) +{ + ext4_assert(bc && cnt && itemsize); + + memset(bc, 0, sizeof(struct ext4_bcache)); + + bc->cnt = cnt; + bc->itemsize = itemsize; + bc->ref_blocks = 0; + bc->max_ref_blocks = 0; + + return EOK; +} + +void ext4_bcache_cleanup(struct ext4_bcache *bc) +{ + struct ext4_buf *buf, *tmp; + RB_FOREACH_SAFE(buf, ext4_buf_lba, &bc->lba_root, tmp) { + ext4_block_flush_buf(bc->bdev, buf); + ext4_bcache_drop_buf(bc, buf); + } +} + +int ext4_bcache_fini_dynamic(struct ext4_bcache *bc) +{ + memset(bc, 0, sizeof(struct ext4_bcache)); + return EOK; +} + +/**@brief: + * + * This is ext4_bcache, the module handling basic buffer-cache stuff. + * + * Buffers in a bcache are sorted by their LBA and stored in a + * RB-Tree(lba_root). + * + * Bcache also maintains another RB-Tree(lru_root) right now, where + * buffers are sorted by their LRU id. + * + * A singly-linked list is used to track those dirty buffers which are + * ready to be flushed. (Those buffers which are dirty but also referenced + * are not considered ready to be flushed.) + * + * When a buffer is not referenced, it will be stored in both lba_root + * and lru_root, while it will only be stored in lba_root when it is + * referenced. + */ + +static struct ext4_buf * +ext4_buf_alloc(struct ext4_bcache *bc, uint64_t lba) +{ + void *data; + struct ext4_buf *buf; + data = ext4_malloc(bc->itemsize); + if (!data) + return NULL; + + buf = ext4_calloc(1, sizeof(struct ext4_buf)); + if (!buf) { + ext4_free(data); + return NULL; + } + + buf->lba = lba; + buf->data = data; + buf->bc = bc; + return buf; +} + +static void ext4_buf_free(struct ext4_buf *buf) +{ + ext4_free(buf->data); + ext4_free(buf); +} + +static struct ext4_buf * +ext4_buf_lookup(struct ext4_bcache *bc, uint64_t lba) +{ + struct ext4_buf tmp = { + .lba = lba + }; + + return RB_FIND(ext4_buf_lba, &bc->lba_root, &tmp); +} + +struct ext4_buf *ext4_buf_lowest_lru(struct ext4_bcache *bc) +{ + return RB_MIN(ext4_buf_lru, &bc->lru_root); +} + +void ext4_bcache_drop_buf(struct ext4_bcache *bc, struct ext4_buf *buf) +{ + /* Warn on dropping any referenced buffers.*/ + if (buf->refctr) { + ext4_dbg(DEBUG_BCACHE, DBG_WARN "Buffer is still referenced. " + "lba: %" PRIu64 ", refctr: %" PRIu32 "\n", + buf->lba, buf->refctr); + } else + RB_REMOVE(ext4_buf_lru, &bc->lru_root, buf); + + RB_REMOVE(ext4_buf_lba, &bc->lba_root, buf); + + /*Forcibly drop dirty buffer.*/ + if (ext4_bcache_test_flag(buf, BC_DIRTY)) + ext4_bcache_remove_dirty_node(bc, buf); + + ext4_buf_free(buf); + bc->ref_blocks--; +} + +void ext4_bcache_invalidate_buf(struct ext4_bcache *bc, + struct ext4_buf *buf) +{ + buf->end_write = NULL; + buf->end_write_arg = NULL; + + /* Clear both dirty and up-to-date flags. */ + if (ext4_bcache_test_flag(buf, BC_DIRTY)) + ext4_bcache_remove_dirty_node(bc, buf); + + ext4_bcache_clear_dirty(buf); +} + +void ext4_bcache_invalidate_lba(struct ext4_bcache *bc, + uint64_t from, + uint32_t cnt) +{ + uint64_t end = from + cnt - 1; + struct ext4_buf *tmp = ext4_buf_lookup(bc, from), *buf; + RB_FOREACH_FROM(buf, ext4_buf_lba, tmp) { + if (buf->lba > end) + break; + + ext4_bcache_invalidate_buf(bc, buf); + } +} + +struct ext4_buf * +ext4_bcache_find_get(struct ext4_bcache *bc, struct ext4_block *b, + uint64_t lba) +{ + struct ext4_buf *buf = ext4_buf_lookup(bc, lba); + if (buf) { + /* If buffer is not referenced. */ + if (!buf->refctr) { + /* Assign new value to LRU id and increment LRU counter + * by 1*/ + buf->lru_id = ++bc->lru_ctr; + RB_REMOVE(ext4_buf_lru, &bc->lru_root, buf); + if (ext4_bcache_test_flag(buf, BC_DIRTY)) + ext4_bcache_remove_dirty_node(bc, buf); + + } + + ext4_bcache_inc_ref(buf); + + b->lb_id = lba; + b->buf = buf; + b->data = buf->data; + } + return buf; +} + +int ext4_bcache_alloc(struct ext4_bcache *bc, struct ext4_block *b, + bool *is_new) +{ + /* Try to search the buffer with exaxt LBA. */ + struct ext4_buf *buf = ext4_bcache_find_get(bc, b, b->lb_id); + if (buf) { + *is_new = false; + return EOK; + } + + /* We need to allocate one buffer.*/ + buf = ext4_buf_alloc(bc, b->lb_id); + if (!buf) + return ENOMEM; + + RB_INSERT(ext4_buf_lba, &bc->lba_root, buf); + /* One more buffer in bcache now. :-) */ + bc->ref_blocks++; + + /*Calc ref blocks max depth*/ + if (bc->max_ref_blocks < bc->ref_blocks) + bc->max_ref_blocks = bc->ref_blocks; + + + ext4_bcache_inc_ref(buf); + /* Assign new value to LRU id and increment LRU counter + * by 1*/ + buf->lru_id = ++bc->lru_ctr; + + b->buf = buf; + b->data = buf->data; + + *is_new = true; + return EOK; +} + +int ext4_bcache_free(struct ext4_bcache *bc, struct ext4_block *b) +{ + struct ext4_buf *buf = b->buf; + + ext4_assert(bc && b); + + /*Check if valid.*/ + ext4_assert(b->lb_id); + + /*Block should have a valid pointer to ext4_buf.*/ + ext4_assert(buf); + + /*Check if someone don't try free unreferenced block cache.*/ + ext4_assert(buf->refctr); + + /*Just decrease reference counter*/ + ext4_bcache_dec_ref(buf); + + /* We are the last one touching this buffer, do the cleanups. */ + if (!buf->refctr) { + RB_INSERT(ext4_buf_lru, &bc->lru_root, buf); + /* This buffer is ready to be flushed. */ + if (ext4_bcache_test_flag(buf, BC_DIRTY) && + ext4_bcache_test_flag(buf, BC_UPTODATE)) { + if (bc->bdev->cache_write_back && + !ext4_bcache_test_flag(buf, BC_FLUSH) && + !ext4_bcache_test_flag(buf, BC_TMP)) + ext4_bcache_insert_dirty_node(bc, buf); + else { + ext4_block_flush_buf(bc->bdev, buf); + ext4_bcache_clear_flag(buf, BC_FLUSH); + } + } + + /* The buffer is invalidated...drop it. */ + if (!ext4_bcache_test_flag(buf, BC_UPTODATE) || + ext4_bcache_test_flag(buf, BC_TMP)) + ext4_bcache_drop_buf(bc, buf); + + } + + b->lb_id = 0; + b->data = 0; + + return EOK; +} + +bool ext4_bcache_is_full(struct ext4_bcache *bc) +{ + return (bc->cnt <= bc->ref_blocks); +} + + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_bitmap.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_bitmap.c new file mode 100644 index 0000000..be37a5a --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_bitmap.c @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_bitmap.c + * @brief Block/inode bitmap allocator. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_bitmap.h" + +void ext4_bmap_bits_free(uint8_t *bmap, uint32_t sbit, uint32_t bcnt) +{ + uint32_t i = sbit; + + while (i & 7) { + + if (!bcnt) + return; + + ext4_bmap_bit_clr(bmap, i); + + bcnt--; + i++; + } + sbit = i; + bmap += (sbit >> 3); + +#if CONFIG_UNALIGNED_ACCESS + while (bcnt >= 32) { + *(uint32_t *)bmap = 0; + bmap += 4; + bcnt -= 32; + sbit += 32; + } + + while (bcnt >= 16) { + *(uint16_t *)bmap = 0; + bmap += 2; + bcnt -= 16; + sbit += 16; + } +#endif + + while (bcnt >= 8) { + *bmap = 0; + bmap += 1; + bcnt -= 8; + sbit += 8; + } + + for (i = 0; i < bcnt; ++i) { + ext4_bmap_bit_clr(bmap, i); + } +} + +int ext4_bmap_bit_find_clr(uint8_t *bmap, uint32_t sbit, uint32_t ebit, + uint32_t *bit_id) +{ + uint32_t i; + uint32_t bcnt = ebit - sbit; + + i = sbit; + + while (i & 7) { + + if (!bcnt) + return ENOSPC; + + if (ext4_bmap_is_bit_clr(bmap, i)) { + *bit_id = sbit; + return EOK; + } + + i++; + bcnt--; + } + + sbit = i; + bmap += (sbit >> 3); + +#if CONFIG_UNALIGNED_ACCESS + while (bcnt >= 32) { + if (*(uint32_t *)bmap != 0xFFFFFFFF) + goto finish_it; + + bmap += 4; + bcnt -= 32; + sbit += 32; + } + + while (bcnt >= 16) { + if (*(uint16_t *)bmap != 0xFFFF) + goto finish_it; + + bmap += 2; + bcnt -= 16; + sbit += 16; + } +finish_it: +#endif + while (bcnt >= 8) { + if (*bmap != 0xFF) { + for (i = 0; i < 8; ++i) { + if (ext4_bmap_is_bit_clr(bmap, i)) { + *bit_id = sbit + i; + return EOK; + } + } + } + + bmap += 1; + bcnt -= 8; + sbit += 8; + } + + for (i = 0; i < bcnt; ++i) { + if (ext4_bmap_is_bit_clr(bmap, i)) { + *bit_id = sbit + i; + return EOK; + } + } + + return ENOSPC; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_block_group.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_block_group.c new file mode 100644 index 0000000..2dc4f76 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_block_group.c @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_block_group.c + * @brief Block group function set. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_block_group.h" + +/**@brief CRC-16 look up table*/ +static uint16_t const crc16_tab[256] = { + 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, + 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0, + 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, + 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941, + 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, + 0x1DC0, 0x1C80, 0xDC41, 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, + 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, + 0x1040, 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, + 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, 0x3C00, + 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, + 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, 0x2800, 0xE8C1, 0xE981, + 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41, + 0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, + 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, + 0x2080, 0xE041, 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, + 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, + 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, + 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, 0x7800, 0xB8C1, + 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, + 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, 0xB401, 0x74C0, 0x7580, 0xB541, + 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, + 0x71C0, 0x7080, 0xB041, 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, + 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, + 0x5440, 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, + 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, 0x8801, + 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, + 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, 0x4400, 0x84C1, 0x8581, + 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, + 0x4100, 0x81C1, 0x8081, 0x4040}; + +uint16_t ext4_bg_crc16(uint16_t crc, const uint8_t *buffer, size_t len) +{ + while (len--) + + crc = (((crc >> 8) & 0xffU) ^ + crc16_tab[(crc ^ *buffer++) & 0xffU]) & + 0x0000ffffU; + return crc; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_blockdev.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_blockdev.c new file mode 100644 index 0000000..3cfa61b --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_blockdev.c @@ -0,0 +1,477 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_blockdev.c + * @brief Block device module. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_blockdev.h" +#include "ext4_fs.h" +#include "ext4_journal.h" + +#include +#include + +static void ext4_bdif_lock(struct ext4_blockdev *bdev) +{ + if (!bdev->bdif->lock) + return; + + int r = bdev->bdif->lock(bdev); + ext4_assert(r == EOK); +} + +static void ext4_bdif_unlock(struct ext4_blockdev *bdev) +{ + if (!bdev->bdif->unlock) + return; + + int r = bdev->bdif->unlock(bdev); + ext4_assert(r == EOK); +} + +static int ext4_bdif_bread(struct ext4_blockdev *bdev, void *buf, + uint64_t blk_id, uint32_t blk_cnt) +{ + ext4_bdif_lock(bdev); + int r = bdev->bdif->bread(bdev, buf, blk_id, blk_cnt); + bdev->bdif->bread_ctr++; + ext4_bdif_unlock(bdev); + return r; +} + +static int ext4_bdif_bwrite(struct ext4_blockdev *bdev, const void *buf, + uint64_t blk_id, uint32_t blk_cnt) +{ + ext4_bdif_lock(bdev); + int r = bdev->bdif->bwrite(bdev, buf, blk_id, blk_cnt); + bdev->bdif->bwrite_ctr++; + ext4_bdif_unlock(bdev); + return r; +} + +int ext4_block_init(struct ext4_blockdev *bdev) +{ + int rc; + ext4_assert(bdev); + ext4_assert(bdev->bdif); + ext4_assert(bdev->bdif->open && + bdev->bdif->close && + bdev->bdif->bread && + bdev->bdif->bwrite); + + if (!bdev->bdif->ph_refctr) + { + /*Low level block init*/ + rc = bdev->bdif->open(bdev); + if (rc != EOK) + return rc; + + bdev->bdif->ph_refctr = 1; + } + else if (bdev->bdif->ph_refctr) { + bdev->bdif->ph_refctr++; + } + + return EOK; +} + +int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc) +{ + ext4_assert(bdev && bc); + bdev->bc = bc; + bc->bdev = bdev; + return EOK; +} + +void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint32_t lb_bsize) +{ + /*Logical block size has to be multiply of physical */ + ext4_assert(!(lb_bsize % bdev->bdif->ph_bsize)); + + bdev->lg_bsize = lb_bsize; + bdev->lg_bcnt = bdev->part_size / lb_bsize; +} + +int ext4_block_fini(struct ext4_blockdev *bdev) +{ + ext4_assert(bdev); + + if (!bdev->bdif->ph_refctr) + return EOK; + + bdev->bdif->ph_refctr--; + if (bdev->bdif->ph_refctr) + return EOK; + + /*Low level block fini*/ + return bdev->bdif->close(bdev); +} + +int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf) +{ + int r; + struct ext4_bcache *bc = bdev->bc; + + if (ext4_bcache_test_flag(buf, BC_DIRTY) && + ext4_bcache_test_flag(buf, BC_UPTODATE)) { + r = ext4_blocks_set_direct(bdev, buf->data, buf->lba, 1); + if (r) { + if (buf->end_write) { + bc->dont_shake = true; + buf->end_write(bc, buf, r, buf->end_write_arg); + bc->dont_shake = false; + } + + return r; + } + + ext4_bcache_remove_dirty_node(bc, buf); + ext4_bcache_clear_flag(buf, BC_DIRTY); + if (buf->end_write) { + bc->dont_shake = true; + buf->end_write(bc, buf, r, buf->end_write_arg); + bc->dont_shake = false; + } + } + return EOK; +} + +int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba) +{ + int r = EOK; + struct ext4_buf *buf; + struct ext4_block b; + buf = ext4_bcache_find_get(bdev->bc, &b, lba); + if (buf) { + r = ext4_block_flush_buf(bdev, buf); + ext4_bcache_free(bdev->bc, &b); + } + return r; +} + +int ext4_block_cache_shake(struct ext4_blockdev *bdev) +{ + int r = EOK; + struct ext4_buf *buf; + if (bdev->bc->dont_shake) + return EOK; + + bdev->bc->dont_shake = true; + + while (!RB_EMPTY(&bdev->bc->lru_root) && + ext4_bcache_is_full(bdev->bc)) { + + buf = ext4_buf_lowest_lru(bdev->bc); + ext4_assert(buf); + if (ext4_bcache_test_flag(buf, BC_DIRTY)) { + r = ext4_block_flush_buf(bdev, buf); + if (r != EOK) + break; + + } + + ext4_bcache_drop_buf(bdev->bc, buf); + } + bdev->bc->dont_shake = false; + return r; +} + +int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b, + uint64_t lba) +{ + bool is_new; + int r; + + ext4_assert(bdev && b); + + if (!bdev->bdif->ph_refctr) + return EIO; + + if (!(lba < bdev->lg_bcnt)) + return ENXIO; + + b->lb_id = lba; + + /*If cache is full we have to (flush and) drop it anyway :(*/ + r = ext4_block_cache_shake(bdev); + if (r != EOK) + return r; + + r = ext4_bcache_alloc(bdev->bc, b, &is_new); + if (r != EOK) + return r; + + if (!b->data) + return ENOMEM; + + return EOK; +} + +int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b, + uint64_t lba) +{ + int r = ext4_block_get_noread(bdev, b, lba); + if (r != EOK) + return r; + + if (ext4_bcache_test_flag(b->buf, BC_UPTODATE)) { + /* Data in the cache is up-to-date. + * Reading from physical device is not required */ + return EOK; + } + + r = ext4_blocks_get_direct(bdev, b->data, lba, 1); + if (r != EOK) { + ext4_bcache_free(bdev->bc, b); + b->lb_id = 0; + return r; + } + + /* Mark buffer up-to-date, since + * fresh data is read from physical device just now. */ + ext4_bcache_set_flag(b->buf, BC_UPTODATE); + return EOK; +} + +int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b) +{ + ext4_assert(bdev && b); + ext4_assert(b->buf); + + if (!bdev->bdif->ph_refctr) + return EIO; + + return ext4_bcache_free(bdev->bc, b); +} + +int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba, + uint32_t cnt) +{ + uint64_t pba; + uint32_t pb_cnt; + + ext4_assert(bdev && buf); + + pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize; + pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize; + + return ext4_bdif_bread(bdev, buf, pba, pb_cnt * cnt); +} + +int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf, + uint64_t lba, uint32_t cnt) +{ + uint64_t pba; + uint32_t pb_cnt; + + ext4_assert(bdev && buf); + + pba = (lba * bdev->lg_bsize + bdev->part_offset) / bdev->bdif->ph_bsize; + pb_cnt = bdev->lg_bsize / bdev->bdif->ph_bsize; + + return ext4_bdif_bwrite(bdev, buf, pba, pb_cnt * cnt); +} + +int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off, + const void *buf, uint32_t len) +{ + uint64_t block_idx; + uint32_t blen; + uint32_t unalg; + int r = EOK; + + const uint8_t *p = (void *)buf; + + ext4_assert(bdev && buf); + + if (!bdev->bdif->ph_refctr) + return EIO; + + if (off + len > bdev->part_size) + return EINVAL; /*Ups. Out of range operation*/ + + block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize); + + /*OK lets deal with the first possible unaligned block*/ + unalg = (off & (bdev->bdif->ph_bsize - 1)); + if (unalg) { + + uint32_t wlen = (bdev->bdif->ph_bsize - unalg) > len + ? len + : (bdev->bdif->ph_bsize - unalg); + + r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1); + if (r != EOK) + return r; + + memcpy(bdev->bdif->ph_bbuf + unalg, p, wlen); + r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1); + if (r != EOK) + return r; + + p += wlen; + len -= wlen; + block_idx++; + } + + /*Aligned data*/ + blen = len / bdev->bdif->ph_bsize; + if (blen != 0) { + r = ext4_bdif_bwrite(bdev, p, block_idx, blen); + if (r != EOK) + return r; + + p += bdev->bdif->ph_bsize * blen; + len -= bdev->bdif->ph_bsize * blen; + + block_idx += blen; + } + + /*Rest of the data*/ + if (len) { + r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1); + if (r != EOK) + return r; + + memcpy(bdev->bdif->ph_bbuf, p, len); + r = ext4_bdif_bwrite(bdev, bdev->bdif->ph_bbuf, block_idx, 1); + if (r != EOK) + return r; + } + + return r; +} + +int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf, + uint32_t len) +{ + uint64_t block_idx; + uint32_t blen; + uint32_t unalg; + int r = EOK; + + uint8_t *p = (void *)buf; + + ext4_assert(bdev && buf); + + if (!bdev->bdif->ph_refctr) + return EIO; + + if (off + len > bdev->part_size) + return EINVAL; /*Ups. Out of range operation*/ + + block_idx = ((off + bdev->part_offset) / bdev->bdif->ph_bsize); + + /*OK lets deal with the first possible unaligned block*/ + unalg = (off & (bdev->bdif->ph_bsize - 1)); + if (unalg) { + + uint32_t rlen = (bdev->bdif->ph_bsize - unalg) > len + ? len + : (bdev->bdif->ph_bsize - unalg); + + r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1); + if (r != EOK) + return r; + + memcpy(p, bdev->bdif->ph_bbuf + unalg, rlen); + + p += rlen; + len -= rlen; + block_idx++; + } + + /*Aligned data*/ + blen = len / bdev->bdif->ph_bsize; + + if (blen != 0) { + r = ext4_bdif_bread(bdev, p, block_idx, blen); + if (r != EOK) + return r; + + p += bdev->bdif->ph_bsize * blen; + len -= bdev->bdif->ph_bsize * blen; + + block_idx += blen; + } + + /*Rest of the data*/ + if (len) { + r = ext4_bdif_bread(bdev, bdev->bdif->ph_bbuf, block_idx, 1); + if (r != EOK) + return r; + + memcpy(p, bdev->bdif->ph_bbuf, len); + } + + return r; +} + +int ext4_block_cache_flush(struct ext4_blockdev *bdev) +{ + while (!SLIST_EMPTY(&bdev->bc->dirty_list)) { + int r; + struct ext4_buf *buf = SLIST_FIRST(&bdev->bc->dirty_list); + ext4_assert(buf); + r = ext4_block_flush_buf(bdev, buf); + if (r != EOK) + return r; + + } + return EOK; +} + +int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off) +{ + if (on_off) + bdev->cache_write_back++; + + if (!on_off && bdev->cache_write_back) + bdev->cache_write_back--; + + if (bdev->cache_write_back) + return EOK; + + /*Flush data in all delayed cache blocks*/ + return ext4_block_cache_flush(bdev); +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_crc32.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_crc32.c new file mode 100644 index 0000000..d15ab79 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_crc32.c @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2014 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Based on FreeBSD. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_crc32c.c + * @brief Crc32c routine. Taken from FreeBSD kernel. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_crc32.h" + +static const uint32_t crc32_tab[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +/* */ +/* CRC LOOKUP TABLE */ +/* ================ */ +/* The following CRC lookup table was generated automagically */ +/* by the Rocksoft^tm Model CRC Algorithm Table Generation */ +/* Program V1.0 using the following model parameters: */ +/* */ +/* Width : 4 bytes. */ +/* Poly : 0x1EDC6F41L */ +/* Reverse : TRUE. */ +/* */ +/* For more information on the Rocksoft^tm Model CRC Algorithm, */ +/* see the document titled "A Painless Guide to CRC Error */ +/* Detection Algorithms" by Ross Williams */ +/* (ross@guest.adelaide.edu.au.). This document is likely to be */ +/* in the FTP archive "ftp.adelaide.edu.au/pub/rocksoft". */ +/* */ +static const uint32_t crc32c_tab[256] = { + 0x00000000L, 0xF26B8303L, 0xE13B70F7L, 0x1350F3F4L, 0xC79A971FL, + 0x35F1141CL, 0x26A1E7E8L, 0xD4CA64EBL, 0x8AD958CFL, 0x78B2DBCCL, + 0x6BE22838L, 0x9989AB3BL, 0x4D43CFD0L, 0xBF284CD3L, 0xAC78BF27L, + 0x5E133C24L, 0x105EC76FL, 0xE235446CL, 0xF165B798L, 0x030E349BL, + 0xD7C45070L, 0x25AFD373L, 0x36FF2087L, 0xC494A384L, 0x9A879FA0L, + 0x68EC1CA3L, 0x7BBCEF57L, 0x89D76C54L, 0x5D1D08BFL, 0xAF768BBCL, + 0xBC267848L, 0x4E4DFB4BL, 0x20BD8EDEL, 0xD2D60DDDL, 0xC186FE29L, + 0x33ED7D2AL, 0xE72719C1L, 0x154C9AC2L, 0x061C6936L, 0xF477EA35L, + 0xAA64D611L, 0x580F5512L, 0x4B5FA6E6L, 0xB93425E5L, 0x6DFE410EL, + 0x9F95C20DL, 0x8CC531F9L, 0x7EAEB2FAL, 0x30E349B1L, 0xC288CAB2L, + 0xD1D83946L, 0x23B3BA45L, 0xF779DEAEL, 0x05125DADL, 0x1642AE59L, + 0xE4292D5AL, 0xBA3A117EL, 0x4851927DL, 0x5B016189L, 0xA96AE28AL, + 0x7DA08661L, 0x8FCB0562L, 0x9C9BF696L, 0x6EF07595L, 0x417B1DBCL, + 0xB3109EBFL, 0xA0406D4BL, 0x522BEE48L, 0x86E18AA3L, 0x748A09A0L, + 0x67DAFA54L, 0x95B17957L, 0xCBA24573L, 0x39C9C670L, 0x2A993584L, + 0xD8F2B687L, 0x0C38D26CL, 0xFE53516FL, 0xED03A29BL, 0x1F682198L, + 0x5125DAD3L, 0xA34E59D0L, 0xB01EAA24L, 0x42752927L, 0x96BF4DCCL, + 0x64D4CECFL, 0x77843D3BL, 0x85EFBE38L, 0xDBFC821CL, 0x2997011FL, + 0x3AC7F2EBL, 0xC8AC71E8L, 0x1C661503L, 0xEE0D9600L, 0xFD5D65F4L, + 0x0F36E6F7L, 0x61C69362L, 0x93AD1061L, 0x80FDE395L, 0x72966096L, + 0xA65C047DL, 0x5437877EL, 0x4767748AL, 0xB50CF789L, 0xEB1FCBADL, + 0x197448AEL, 0x0A24BB5AL, 0xF84F3859L, 0x2C855CB2L, 0xDEEEDFB1L, + 0xCDBE2C45L, 0x3FD5AF46L, 0x7198540DL, 0x83F3D70EL, 0x90A324FAL, + 0x62C8A7F9L, 0xB602C312L, 0x44694011L, 0x5739B3E5L, 0xA55230E6L, + 0xFB410CC2L, 0x092A8FC1L, 0x1A7A7C35L, 0xE811FF36L, 0x3CDB9BDDL, + 0xCEB018DEL, 0xDDE0EB2AL, 0x2F8B6829L, 0x82F63B78L, 0x709DB87BL, + 0x63CD4B8FL, 0x91A6C88CL, 0x456CAC67L, 0xB7072F64L, 0xA457DC90L, + 0x563C5F93L, 0x082F63B7L, 0xFA44E0B4L, 0xE9141340L, 0x1B7F9043L, + 0xCFB5F4A8L, 0x3DDE77ABL, 0x2E8E845FL, 0xDCE5075CL, 0x92A8FC17L, + 0x60C37F14L, 0x73938CE0L, 0x81F80FE3L, 0x55326B08L, 0xA759E80BL, + 0xB4091BFFL, 0x466298FCL, 0x1871A4D8L, 0xEA1A27DBL, 0xF94AD42FL, + 0x0B21572CL, 0xDFEB33C7L, 0x2D80B0C4L, 0x3ED04330L, 0xCCBBC033L, + 0xA24BB5A6L, 0x502036A5L, 0x4370C551L, 0xB11B4652L, 0x65D122B9L, + 0x97BAA1BAL, 0x84EA524EL, 0x7681D14DL, 0x2892ED69L, 0xDAF96E6AL, + 0xC9A99D9EL, 0x3BC21E9DL, 0xEF087A76L, 0x1D63F975L, 0x0E330A81L, + 0xFC588982L, 0xB21572C9L, 0x407EF1CAL, 0x532E023EL, 0xA145813DL, + 0x758FE5D6L, 0x87E466D5L, 0x94B49521L, 0x66DF1622L, 0x38CC2A06L, + 0xCAA7A905L, 0xD9F75AF1L, 0x2B9CD9F2L, 0xFF56BD19L, 0x0D3D3E1AL, + 0x1E6DCDEEL, 0xEC064EEDL, 0xC38D26C4L, 0x31E6A5C7L, 0x22B65633L, + 0xD0DDD530L, 0x0417B1DBL, 0xF67C32D8L, 0xE52CC12CL, 0x1747422FL, + 0x49547E0BL, 0xBB3FFD08L, 0xA86F0EFCL, 0x5A048DFFL, 0x8ECEE914L, + 0x7CA56A17L, 0x6FF599E3L, 0x9D9E1AE0L, 0xD3D3E1ABL, 0x21B862A8L, + 0x32E8915CL, 0xC083125FL, 0x144976B4L, 0xE622F5B7L, 0xF5720643L, + 0x07198540L, 0x590AB964L, 0xAB613A67L, 0xB831C993L, 0x4A5A4A90L, + 0x9E902E7BL, 0x6CFBAD78L, 0x7FAB5E8CL, 0x8DC0DD8FL, 0xE330A81AL, + 0x115B2B19L, 0x020BD8EDL, 0xF0605BEEL, 0x24AA3F05L, 0xD6C1BC06L, + 0xC5914FF2L, 0x37FACCF1L, 0x69E9F0D5L, 0x9B8273D6L, 0x88D28022L, + 0x7AB90321L, 0xAE7367CAL, 0x5C18E4C9L, 0x4F48173DL, 0xBD23943EL, + 0xF36E6F75L, 0x0105EC76L, 0x12551F82L, 0xE03E9C81L, 0x34F4F86AL, + 0xC69F7B69L, 0xD5CF889DL, 0x27A40B9EL, 0x79B737BAL, 0x8BDCB4B9L, + 0x988C474DL, 0x6AE7C44EL, 0xBE2DA0A5L, 0x4C4623A6L, 0x5F16D052L, + 0xAD7D5351L}; + +static inline uint32_t crc32(uint32_t crc, const void *buf, uint32_t size, + const uint32_t *tab) +{ + const uint8_t *p = (const uint8_t *)buf; + + while (size--) + crc = tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); + + return (crc); +} + +uint32_t ext4_crc32(uint32_t crc, const void *buf, uint32_t size) +{ + return crc32(crc, buf, size, crc32_tab); +} + +uint32_t ext4_crc32c(uint32_t crc, const void *buf, uint32_t size) +{ + return crc32(crc, buf, size, crc32c_tab); +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_debug.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_debug.c new file mode 100644 index 0000000..09ab2bd --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_debug.c @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_debug.c + * @brief Debug printf and assert macros. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include + +static uint32_t debug_mask; + +void ext4_dmask_set(uint32_t m) +{ + debug_mask |= m; +} + +void ext4_dmask_clr(uint32_t m) +{ + debug_mask &= ~m; +} + +uint32_t ext4_dmask_get(void) +{ + return debug_mask; +} + + + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_dir.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_dir.c new file mode 100644 index 0000000..54fd79d --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_dir.c @@ -0,0 +1,708 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_dir.h + * @brief Directory handle procedures. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_trans.h" +#include "ext4_dir.h" +#include "ext4_dir_idx.h" +#include "ext4_crc32.h" +#include "ext4_inode.h" +#include "ext4_fs.h" + +#include + +/****************************************************************************/ + +/* Walk through a dirent block to find a checksum "dirent" at the tail */ +static struct ext4_dir_entry_tail * +ext4_dir_get_tail(struct ext4_inode_ref *inode_ref, + struct ext4_dir_en *de) +{ + struct ext4_dir_entry_tail *t; + struct ext4_sblock *sb = &inode_ref->fs->sb; + + t = EXT4_DIRENT_TAIL(de, ext4_sb_get_block_size(sb)); + + if (t->reserved_zero1 || t->reserved_zero2) + return NULL; + if (to_le16(t->rec_len) != sizeof(struct ext4_dir_entry_tail)) + return NULL; + if (t->reserved_ft != EXT4_DIRENTRY_DIR_CSUM) + return NULL; + + return t; +} + +#if CONFIG_META_CSUM_ENABLE +static uint32_t ext4_dir_csum(struct ext4_inode_ref *inode_ref, + struct ext4_dir_en *dirent, int size) +{ + uint32_t csum; + struct ext4_sblock *sb = &inode_ref->fs->sb; + uint32_t ino_index = to_le32(inode_ref->index); + uint32_t ino_gen = to_le32(ext4_inode_get_generation(inode_ref->inode)); + + /* First calculate crc32 checksum against fs uuid */ + csum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid)); + /* Then calculate crc32 checksum against inode number + * and inode generation */ + csum = ext4_crc32c(csum, &ino_index, sizeof(ino_index)); + csum = ext4_crc32c(csum, &ino_gen, sizeof(ino_gen)); + /* Finally calculate crc32 checksum against directory entries */ + csum = ext4_crc32c(csum, dirent, size); + return csum; +} +#else +#define ext4_dir_csum(...) 0 +#endif + +bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref, + struct ext4_dir_en *dirent) +{ +#ifdef CONFIG_META_CSUM_ENABLE + struct ext4_dir_entry_tail *t; + struct ext4_sblock *sb = &inode_ref->fs->sb; + + /* Compute the checksum only if the filesystem supports it */ + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + t = ext4_dir_get_tail(inode_ref, dirent); + if (!t) { + /* There is no space to hold the checksum */ + return false; + } + + ptrdiff_t __unused diff = (char *)t - (char *)dirent; + uint32_t csum = ext4_dir_csum(inode_ref, dirent, diff); + if (t->checksum != to_le32(csum)) + return false; + + } +#endif + return true; +} + +void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t) +{ + memset(t, 0, sizeof(struct ext4_dir_entry_tail)); + t->rec_len = to_le16(sizeof(struct ext4_dir_entry_tail)); + t->reserved_ft = EXT4_DIRENTRY_DIR_CSUM; +} + +void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref, + struct ext4_dir_en *dirent) +{ + struct ext4_dir_entry_tail *t; + struct ext4_sblock *sb = &inode_ref->fs->sb; + + /* Compute the checksum only if the filesystem supports it */ + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + t = ext4_dir_get_tail(inode_ref, dirent); + if (!t) { + /* There is no space to hold the checksum */ + return; + } + + ptrdiff_t __unused diff = (char *)t - (char *)dirent; + uint32_t csum = ext4_dir_csum(inode_ref, dirent, diff); + t->checksum = to_le32(csum); + } +} + +/**@brief Do some checks before returning iterator. + * @param it Iterator to be checked + * @param block_size Size of data block + * @return Error code + */ +static int ext4_dir_iterator_set(struct ext4_dir_iter *it, + uint32_t block_size) +{ + uint32_t off_in_block = it->curr_off % block_size; + struct ext4_sblock *sb = &it->inode_ref->fs->sb; + + it->curr = NULL; + + /* Ensure proper alignment */ + if ((off_in_block % 4) != 0) + return EIO; + + /* Ensure that the core of the entry does not overflow the block */ + if (off_in_block > block_size - 8) + return EIO; + + struct ext4_dir_en *en; + en = (void *)(it->curr_blk.data + off_in_block); + + /* Ensure that the whole entry does not overflow the block */ + uint16_t length = ext4_dir_en_get_entry_len(en); + if (off_in_block + length > block_size) + return EIO; + + /* Ensure the name length is not too large */ + if (ext4_dir_en_get_name_len(sb, en) > length - 8) + return EIO; + + /* Everything OK - "publish" the entry */ + it->curr = en; + return EOK; +} + +/**@brief Seek to next valid directory entry. + * Here can be jumped to the next data block. + * @param it Initialized iterator + * @param pos Position of the next entry + * @return Error code + */ +static int ext4_dir_iterator_seek(struct ext4_dir_iter *it, uint64_t pos) +{ + struct ext4_sblock *sb = &it->inode_ref->fs->sb; + struct ext4_inode *inode = it->inode_ref->inode; + struct ext4_blockdev *bdev = it->inode_ref->fs->bdev; + uint64_t size = ext4_inode_get_size(sb, inode); + int r; + + /* The iterator is not valid until we seek to the desired position */ + it->curr = NULL; + + /* Are we at the end? */ + if (pos >= size) { + if (it->curr_blk.lb_id) { + + r = ext4_block_set(bdev, &it->curr_blk); + it->curr_blk.lb_id = 0; + if (r != EOK) + return r; + } + + it->curr_off = pos; + return EOK; + } + + /* Compute next block address */ + uint32_t block_size = ext4_sb_get_block_size(sb); + uint64_t current_blk_idx = it->curr_off / block_size; + uint32_t next_blk_idx = (uint32_t)(pos / block_size); + + /* + * If we don't have a block or are moving across block boundary, + * we need to get another block + */ + if ((it->curr_blk.lb_id == 0) || + (current_blk_idx != next_blk_idx)) { + if (it->curr_blk.lb_id) { + r = ext4_block_set(bdev, &it->curr_blk); + it->curr_blk.lb_id = 0; + + if (r != EOK) + return r; + } + + ext4_fsblk_t next_blk; + r = ext4_fs_get_inode_dblk_idx(it->inode_ref, next_blk_idx, + &next_blk, false); + if (r != EOK) + return r; + + r = ext4_trans_block_get(bdev, &it->curr_blk, next_blk); + if (r != EOK) { + it->curr_blk.lb_id = 0; + return r; + } + } + + it->curr_off = pos; + return ext4_dir_iterator_set(it, block_size); +} + +int ext4_dir_iterator_init(struct ext4_dir_iter *it, + struct ext4_inode_ref *inode_ref, uint64_t pos) +{ + it->inode_ref = inode_ref; + it->curr = 0; + it->curr_off = 0; + it->curr_blk.lb_id = 0; + + return ext4_dir_iterator_seek(it, pos); +} + +int ext4_dir_iterator_next(struct ext4_dir_iter *it) +{ + int r = EOK; + uint16_t skip; + + while (r == EOK) { + skip = ext4_dir_en_get_entry_len(it->curr); + r = ext4_dir_iterator_seek(it, it->curr_off + skip); + + if (!it->curr) + break; + /*Skip NULL referenced entry*/ + if (ext4_dir_en_get_inode(it->curr) != 0) + break; + } + + return r; +} + +int ext4_dir_iterator_fini(struct ext4_dir_iter *it) +{ + it->curr = 0; + + if (it->curr_blk.lb_id) + return ext4_block_set(it->inode_ref->fs->bdev, &it->curr_blk); + + return EOK; +} + +void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en, + uint16_t entry_len, struct ext4_inode_ref *child, + const char *name, size_t name_len) +{ + /* Check maximum entry length */ + ext4_assert(entry_len <= ext4_sb_get_block_size(sb)); + + /* Set type of entry */ + switch (ext4_inode_type(sb, child->inode)) { + case EXT4_INODE_MODE_DIRECTORY: + ext4_dir_en_set_inode_type(sb, en, EXT4_DE_DIR); + break; + case EXT4_INODE_MODE_FILE: + ext4_dir_en_set_inode_type(sb, en, EXT4_DE_REG_FILE); + break; + case EXT4_INODE_MODE_SOFTLINK: + ext4_dir_en_set_inode_type(sb, en, EXT4_DE_SYMLINK); + break; + case EXT4_INODE_MODE_CHARDEV: + ext4_dir_en_set_inode_type(sb, en, EXT4_DE_CHRDEV); + break; + case EXT4_INODE_MODE_BLOCKDEV: + ext4_dir_en_set_inode_type(sb, en, EXT4_DE_BLKDEV); + break; + case EXT4_INODE_MODE_FIFO: + ext4_dir_en_set_inode_type(sb, en, EXT4_DE_FIFO); + break; + case EXT4_INODE_MODE_SOCKET: + ext4_dir_en_set_inode_type(sb, en, EXT4_DE_SOCK); + break; + default: + /* FIXME: unsupported filetype */ + ext4_dir_en_set_inode_type(sb, en, EXT4_DE_UNKNOWN); + } + + /* Set basic attributes */ + ext4_dir_en_set_inode(en, child->index); + ext4_dir_en_set_entry_len(en, entry_len); + ext4_dir_en_set_name_len(sb, en, (uint16_t)name_len); + + /* Write name */ + memcpy(en->name, name, name_len); +} + +int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name, + uint32_t name_len, struct ext4_inode_ref *child) +{ + int r; + struct ext4_fs *fs = parent->fs; + struct ext4_sblock *sb = &parent->fs->sb; + +#if CONFIG_DIR_INDEX_ENABLE + /* Index adding (if allowed) */ + if ((ext4_sb_feature_com(sb, EXT4_FCOM_DIR_INDEX)) && + (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) { + r = ext4_dir_dx_add_entry(parent, child, name, name_len); + + /* Check if index is not corrupted */ + if (r != EXT4_ERR_BAD_DX_DIR) { + if (r != EOK) + return r; + + return EOK; + } + + /* Needed to clear dir index flag if corrupted */ + ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX); + parent->dirty = true; + } +#endif + + /* Linear algorithm */ + uint32_t iblock = 0; + ext4_fsblk_t fblock = 0; + uint32_t block_size = ext4_sb_get_block_size(sb); + uint64_t inode_size = ext4_inode_get_size(sb, parent->inode); + uint32_t total_blocks = (uint32_t)(inode_size / block_size); + + /* Find block, where is space for new entry and try to add */ + bool success = false; + for (iblock = 0; iblock < total_blocks; ++iblock) { + r = ext4_fs_get_inode_dblk_idx(parent, iblock, &fblock, false); + if (r != EOK) + return r; + + struct ext4_block block; + r = ext4_trans_block_get(fs->bdev, &block, fblock); + if (r != EOK) + return r; + + if (!ext4_dir_csum_verify(parent, (void *)block.data)) { + ext4_dbg(DEBUG_DIR, + DBG_WARN "Leaf block checksum failed." + "Inode: %" PRIu32", " + "Block: %" PRIu32"\n", + parent->index, + iblock); + } + + /* If adding is successful, function can finish */ + r = ext4_dir_try_insert_entry(sb, parent, &block, child, + name, name_len); + if (r == EOK) + success = true; + + r = ext4_block_set(fs->bdev, &block); + if (r != EOK) + return r; + + if (success) + return EOK; + } + + /* No free block found - needed to allocate next data block */ + + iblock = 0; + fblock = 0; + r = ext4_fs_append_inode_dblk(parent, &fblock, &iblock); + if (r != EOK) + return r; + + /* Load new block */ + struct ext4_block b; + + r = ext4_trans_block_get_noread(fs->bdev, &b, fblock); + if (r != EOK) + return r; + + /* Fill block with zeroes */ + memset(b.data, 0, block_size); + struct ext4_dir_en *blk_en = (void *)b.data; + + /* Save new block */ + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + uint16_t el = block_size - sizeof(struct ext4_dir_entry_tail); + ext4_dir_write_entry(sb, blk_en, el, child, name, name_len); + ext4_dir_init_entry_tail(EXT4_DIRENT_TAIL(b.data, block_size)); + } else { + ext4_dir_write_entry(sb, blk_en, block_size, child, name, + name_len); + } + + ext4_dir_set_csum(parent, (void *)b.data); + ext4_trans_set_block_dirty(b.buf); + r = ext4_block_set(fs->bdev, &b); + + return r; +} + +int ext4_dir_find_entry(struct ext4_dir_search_result *result, + struct ext4_inode_ref *parent, const char *name, + uint32_t name_len) +{ + int r; + struct ext4_sblock *sb = &parent->fs->sb; + + /* Entry clear */ + result->block.lb_id = 0; + result->dentry = NULL; + +#if CONFIG_DIR_INDEX_ENABLE + /* Index search */ + if ((ext4_sb_feature_com(sb, EXT4_FCOM_DIR_INDEX)) && + (ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX))) { + r = ext4_dir_dx_find_entry(result, parent, name_len, name); + /* Check if index is not corrupted */ + if (r != EXT4_ERR_BAD_DX_DIR) { + if (r != EOK) + return r; + + return EOK; + } + + /* Needed to clear dir index flag if corrupted */ + ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX); + parent->dirty = true; + } +#endif + + /* Linear algorithm */ + + uint32_t iblock; + ext4_fsblk_t fblock; + uint32_t block_size = ext4_sb_get_block_size(sb); + uint64_t inode_size = ext4_inode_get_size(sb, parent->inode); + uint32_t total_blocks = (uint32_t)(inode_size / block_size); + + /* Walk through all data blocks */ + for (iblock = 0; iblock < total_blocks; ++iblock) { + /* Load block address */ + r = ext4_fs_get_inode_dblk_idx(parent, iblock, &fblock, false); + if (r != EOK) + return r; + + /* Load data block */ + struct ext4_block b; + r = ext4_trans_block_get(parent->fs->bdev, &b, fblock); + if (r != EOK) + return r; + + if (!ext4_dir_csum_verify(parent, (void *)b.data)) { + ext4_dbg(DEBUG_DIR, + DBG_WARN "Leaf block checksum failed." + "Inode: %" PRIu32", " + "Block: %" PRIu32"\n", + parent->index, + iblock); + } + + /* Try to find entry in block */ + struct ext4_dir_en *res_entry; + r = ext4_dir_find_in_block(&b, sb, name_len, name, &res_entry); + if (r == EOK) { + result->block = b; + result->dentry = res_entry; + return EOK; + } + + /* Entry not found - put block and continue to the next block */ + + r = ext4_block_set(parent->fs->bdev, &b); + if (r != EOK) + return r; + } + + return ENOENT; +} + +int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name, + uint32_t name_len) +{ + struct ext4_sblock *sb = &parent->fs->sb; + /* Check if removing from directory */ + if (!ext4_inode_is_type(sb, parent->inode, EXT4_INODE_MODE_DIRECTORY)) + return ENOTDIR; + + /* Try to find entry */ + struct ext4_dir_search_result result; + int rc = ext4_dir_find_entry(&result, parent, name, name_len); + if (rc != EOK) + return rc; + + /* Invalidate entry */ + ext4_dir_en_set_inode(result.dentry, 0); + + /* Store entry position in block */ + uint32_t pos = (uint8_t *)result.dentry - result.block.data; + + /* + * If entry is not the first in block, it must be merged + * with previous entry + */ + if (pos != 0) { + uint32_t offset = 0; + + /* Start from the first entry in block */ + struct ext4_dir_en *tmp_de =(void *)result.block.data; + uint16_t de_len = ext4_dir_en_get_entry_len(tmp_de); + + /* Find direct predecessor of removed entry */ + while ((offset + de_len) < pos) { + offset += ext4_dir_en_get_entry_len(tmp_de); + tmp_de = (void *)(result.block.data + offset); + de_len = ext4_dir_en_get_entry_len(tmp_de); + } + + ext4_assert(de_len + offset == pos); + + /* Add to removed entry length to predecessor's length */ + uint16_t del_len; + del_len = ext4_dir_en_get_entry_len(result.dentry); + ext4_dir_en_set_entry_len(tmp_de, de_len + del_len); + } + + ext4_dir_set_csum(parent, + (struct ext4_dir_en *)result.block.data); + ext4_trans_set_block_dirty(result.block.buf); + + return ext4_dir_destroy_result(parent, &result); +} + +int ext4_dir_try_insert_entry(struct ext4_sblock *sb, + struct ext4_inode_ref *inode_ref, + struct ext4_block *dst_blk, + struct ext4_inode_ref *child, const char *name, + uint32_t name_len) +{ + /* Compute required length entry and align it to 4 bytes */ + uint32_t block_size = ext4_sb_get_block_size(sb); + uint16_t required_len = sizeof(struct ext4_fake_dir_entry) + name_len; + + if ((required_len % 4) != 0) + required_len += 4 - (required_len % 4); + + /* Initialize pointers, stop means to upper bound */ + struct ext4_dir_en *start = (void *)dst_blk->data; + struct ext4_dir_en *stop = (void *)(dst_blk->data + block_size); + + /* + * Walk through the block and check for invalid entries + * or entries with free space for new entry + */ + while (start < stop) { + uint32_t inode = ext4_dir_en_get_inode(start); + uint16_t rec_len = ext4_dir_en_get_entry_len(start); + uint8_t itype = ext4_dir_en_get_inode_type(sb, start); + + /* If invalid and large enough entry, use it */ + if ((inode == 0) && (itype != EXT4_DIRENTRY_DIR_CSUM) && + (rec_len >= required_len)) { + ext4_dir_write_entry(sb, start, rec_len, child, name, + name_len); + ext4_dir_set_csum(inode_ref, (void *)dst_blk->data); + ext4_trans_set_block_dirty(dst_blk->buf); + + return EOK; + } + + /* Valid entry, try to split it */ + if (inode != 0) { + uint16_t used_len; + used_len = ext4_dir_en_get_name_len(sb, start); + + uint16_t sz; + sz = sizeof(struct ext4_fake_dir_entry) + used_len; + + if ((used_len % 4) != 0) + sz += 4 - (used_len % 4); + + uint16_t free_space = rec_len - sz; + + /* There is free space for new entry */ + if (free_space >= required_len) { + /* Cut tail of current entry */ + struct ext4_dir_en * new_entry; + new_entry = (void *)((uint8_t *)start + sz); + ext4_dir_en_set_entry_len(start, sz); + ext4_dir_write_entry(sb, new_entry, free_space, + child, name, name_len); + + ext4_dir_set_csum(inode_ref, + (void *)dst_blk->data); + ext4_trans_set_block_dirty(dst_blk->buf); + return EOK; + } + } + + /* Jump to the next entry */ + start = (void *)((uint8_t *)start + rec_len); + } + + /* No free space found for new entry */ + return ENOSPC; +} + +int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb, + size_t name_len, const char *name, + struct ext4_dir_en **res_entry) +{ + /* Start from the first entry in block */ + struct ext4_dir_en *de = (struct ext4_dir_en *)block->data; + + /* Set upper bound for cycling */ + uint8_t *addr_limit = block->data + ext4_sb_get_block_size(sb); + + /* Walk through the block and check entries */ + while ((uint8_t *)de < addr_limit) { + /* Termination condition */ + if ((uint8_t *)de + name_len > addr_limit) + break; + + /* Valid entry - check it */ + if (ext4_dir_en_get_inode(de) != 0) { + /* For more efficient compare only lengths firstly*/ + uint16_t el = ext4_dir_en_get_name_len(sb, de); + if (el == name_len) { + /* Compare names */ + if (memcmp(name, de->name, name_len) == 0) { + *res_entry = de; + return EOK; + } + } + } + + uint16_t de_len = ext4_dir_en_get_entry_len(de); + + /* Corrupted entry */ + if (de_len == 0) + return EINVAL; + + /* Jump to next entry */ + de = (struct ext4_dir_en *)((uint8_t *)de + de_len); + } + + /* Entry not found */ + return ENOENT; +} + +int ext4_dir_destroy_result(struct ext4_inode_ref *parent, + struct ext4_dir_search_result *result) +{ + if (result->block.lb_id) + return ext4_block_set(parent->fs->bdev, &result->block); + + return EOK; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_dir_idx.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_dir_idx.c new file mode 100644 index 0000000..f095652 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_dir_idx.c @@ -0,0 +1,1403 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_dir_idx.c + * @brief Directory indexing procedures. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_trans.h" +#include "ext4_dir_idx.h" +#include "ext4_dir.h" +#include "ext4_blockdev.h" +#include "ext4_fs.h" +#include "ext4_super.h" +#include "ext4_inode.h" +#include "ext4_crc32.h" +#include "ext4_hash.h" + +#include +#include + +/**@brief Get hash version used in directory index. + * @param root_info Pointer to root info structure of index + * @return Hash algorithm version + */ +static inline uint8_t +ext4_dir_dx_rinfo_get_hash_version(struct ext4_dir_idx_rinfo *ri) +{ + return ri->hash_version; +} + +/**@brief Set hash version, that will be used in directory index. + * @param root_info Pointer to root info structure of index + * @param v Hash algorithm version + */ +static inline void +ext4_dir_dx_rinfo_set_hash_version(struct ext4_dir_idx_rinfo *ri, uint8_t v) +{ + ri->hash_version = v; +} + +/**@brief Get length of root_info structure in bytes. + * @param root_info Pointer to root info structure of index + * @return Length of the structure + */ +static inline uint8_t +ext4_dir_dx_rinfo_get_info_length(struct ext4_dir_idx_rinfo *ri) +{ + return ri->info_length; +} + +/**@brief Set length of root_info structure in bytes. + * @param root_info Pointer to root info structure of index + * @param info_length Length of the structure + */ +static inline void +ext4_dir_dx_root_info_set_info_length(struct ext4_dir_idx_rinfo *ri, + uint8_t len) +{ + ri->info_length = len; +} + +/**@brief Get number of indirect levels of HTree. + * @param root_info Pointer to root info structure of index + * @return Height of HTree (actually only 0 or 1) + */ +static inline uint8_t +ext4_dir_dx_rinfo_get_indirect_levels(struct ext4_dir_idx_rinfo *ri) +{ + return ri->indirect_levels; +} + +/**@brief Set number of indirect levels of HTree. + * @param root_info Pointer to root info structure of index + * @param lvl Height of HTree (actually only 0 or 1) + */ +static inline void +ext4_dir_dx_rinfo_set_indirect_levels(struct ext4_dir_idx_rinfo *ri, uint8_t l) +{ + ri->indirect_levels = l; +} + +/**@brief Get maximum number of index node entries. + * @param climit Pointer to counlimit structure + * @return Maximum of entries in node + */ +static inline uint16_t +ext4_dir_dx_climit_get_limit(struct ext4_dir_idx_climit *climit) +{ + return to_le16(climit->limit); +} + +/**@brief Set maximum number of index node entries. + * @param climit Pointer to counlimit structure + * @param limit Maximum of entries in node + */ +static inline void +ext4_dir_dx_climit_set_limit(struct ext4_dir_idx_climit *climit, uint16_t limit) +{ + climit->limit = to_le16(limit); +} + +/**@brief Get current number of index node entries. + * @param climit Pointer to counlimit structure + * @return Number of entries in node + */ +static inline uint16_t +ext4_dir_dx_climit_get_count(struct ext4_dir_idx_climit *climit) +{ + return to_le16(climit->count); +} + +/**@brief Set current number of index node entries. + * @param climit Pointer to counlimit structure + * @param count Number of entries in node + */ +static inline void +ext4_dir_dx_climit_set_count(struct ext4_dir_idx_climit *climit, uint16_t count) +{ + climit->count = to_le16(count); +} + +/**@brief Get hash value of index entry. + * @param entry Pointer to index entry + * @return Hash value + */ +static inline uint32_t +ext4_dir_dx_entry_get_hash(struct ext4_dir_idx_entry *entry) +{ + return to_le32(entry->hash); +} + +/**@brief Set hash value of index entry. + * @param entry Pointer to index entry + * @param hash Hash value + */ +static inline void +ext4_dir_dx_entry_set_hash(struct ext4_dir_idx_entry *entry, uint32_t hash) +{ + entry->hash = to_le32(hash); +} + +/**@brief Get block address where child node is located. + * @param entry Pointer to index entry + * @return Block address of child node + */ +static inline uint32_t +ext4_dir_dx_entry_get_block(struct ext4_dir_idx_entry *entry) +{ + return to_le32(entry->block); +} + +/**@brief Set block address where child node is located. + * @param entry Pointer to index entry + * @param block Block address of child node + */ +static inline void +ext4_dir_dx_entry_set_block(struct ext4_dir_idx_entry *entry, uint32_t block) +{ + entry->block = to_le32(block); +} + +/**@brief Sort entry item.*/ +struct ext4_dx_sort_entry { + uint32_t hash; + uint32_t rec_len; + void *dentry; +}; + +static int ext4_dir_dx_hash_string(struct ext4_hash_info *hinfo, int len, + const char *name) +{ + return ext2_htree_hash(name, len, hinfo->seed, hinfo->hash_version, + &hinfo->hash, &hinfo->minor_hash); +} + +#if CONFIG_META_CSUM_ENABLE +static uint32_t ext4_dir_dx_checksum(struct ext4_inode_ref *inode_ref, void *de, + int count_offset, int count, + struct ext4_dir_idx_tail *t) +{ + uint32_t orig_cum, csum = 0; + struct ext4_sblock *sb = &inode_ref->fs->sb; + int sz; + + /* Compute the checksum only if the filesystem supports it */ + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + uint32_t ino_index = to_le32(inode_ref->index); + uint32_t ino_gen; + ino_gen = to_le32(ext4_inode_get_generation(inode_ref->inode)); + + sz = count_offset + (count * sizeof(struct ext4_dir_idx_tail)); + orig_cum = t->checksum; + t->checksum = 0; + /* First calculate crc32 checksum against fs uuid */ + csum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid)); + /* Then calculate crc32 checksum against inode number + * and inode generation */ + csum = ext4_crc32c(csum, &ino_index, sizeof(ino_index)); + csum = ext4_crc32c(csum, &ino_gen, sizeof(ino_gen)); + /* After that calculate crc32 checksum against all the dx_entry */ + csum = ext4_crc32c(csum, de, sz); + /* Finally calculate crc32 checksum for dx_tail */ + csum = ext4_crc32c(csum, t, sizeof(struct ext4_dir_idx_tail)); + t->checksum = orig_cum; + } + return csum; +} + +static struct ext4_dir_idx_climit * +ext4_dir_dx_get_climit(struct ext4_inode_ref *inode_ref, + struct ext4_dir_en *dirent, int *offset) +{ + struct ext4_dir_en *dp; + struct ext4_dir_idx_root *root; + struct ext4_sblock *sb = &inode_ref->fs->sb; + uint32_t block_size = ext4_sb_get_block_size(sb); + uint16_t entry_len = ext4_dir_en_get_entry_len(dirent); + int count_offset; + + + if (entry_len == 12) { + root = (struct ext4_dir_idx_root *)dirent; + dp = (struct ext4_dir_en *)&root->dots[1]; + if (ext4_dir_en_get_entry_len(dp) != (block_size - 12)) + return NULL; + if (root->info.reserved_zero) + return NULL; + if (root->info.info_length != sizeof(struct ext4_dir_idx_rinfo)) + return NULL; + count_offset = 32; + } else if (entry_len == block_size) { + count_offset = 8; + } else { + return NULL; + } + + if (offset) + *offset = count_offset; + return (struct ext4_dir_idx_climit *)(((char *)dirent) + count_offset); +} + +/* + * BIG FAT NOTES: + * Currently we do not verify the checksum of HTree node. + */ +static bool ext4_dir_dx_csum_verify(struct ext4_inode_ref *inode_ref, + struct ext4_dir_en *de) +{ + struct ext4_sblock *sb = &inode_ref->fs->sb; + uint32_t block_size = ext4_sb_get_block_size(sb); + int coff, limit, cnt; + + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + struct ext4_dir_idx_climit *climit; + climit = ext4_dir_dx_get_climit(inode_ref, de, &coff); + if (!climit) { + /* Directory seems corrupted. */ + return true; + } + struct ext4_dir_idx_tail *t; + limit = ext4_dir_dx_climit_get_limit(climit); + cnt = ext4_dir_dx_climit_get_count(climit); + if (coff + (limit * sizeof(struct ext4_dir_idx_entry)) > + (block_size - sizeof(struct ext4_dir_idx_tail))) { + /* There is no space to hold the checksum */ + return true; + } + t = (void *)(((struct ext4_dir_idx_entry *)climit) + limit); + + uint32_t c; + c = to_le32(ext4_dir_dx_checksum(inode_ref, de, coff, cnt, t)); + if (t->checksum != c) + return false; + } + return true; +} + + +static void ext4_dir_set_dx_csum(struct ext4_inode_ref *inode_ref, + struct ext4_dir_en *dirent) +{ + int coff, limit, count; + struct ext4_sblock *sb = &inode_ref->fs->sb; + uint32_t block_size = ext4_sb_get_block_size(sb); + + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + struct ext4_dir_idx_climit *climit; + climit = ext4_dir_dx_get_climit(inode_ref, dirent, &coff); + if (!climit) { + /* Directory seems corrupted. */ + return; + } + struct ext4_dir_idx_tail *t; + limit = ext4_dir_dx_climit_get_limit(climit); + count = ext4_dir_dx_climit_get_count(climit); + if (coff + (limit * sizeof(struct ext4_dir_idx_entry)) > + (block_size - sizeof(struct ext4_dir_idx_tail))) { + /* There is no space to hold the checksum */ + return; + } + + t = (void *)(((struct ext4_dir_idx_entry *)climit) + limit); + t->checksum = to_le32(ext4_dir_dx_checksum(inode_ref, dirent, + coff, count, t)); + } +} +#else +#define ext4_dir_dx_csum_verify(...) true +#define ext4_dir_set_dx_csum(...) +#endif + +/****************************************************************************/ + +int ext4_dir_dx_init(struct ext4_inode_ref *dir, struct ext4_inode_ref *parent) +{ + /* Load block 0, where will be index root located */ + ext4_fsblk_t fblock; + uint32_t iblock = 0; + bool need_append = + (ext4_inode_get_size(&dir->fs->sb, dir->inode) + < EXT4_DIR_DX_INIT_BCNT) + ? true : false; + struct ext4_sblock *sb = &dir->fs->sb; + uint32_t block_size = ext4_sb_get_block_size(&dir->fs->sb); + struct ext4_block block; + + int rc; + + if (!need_append) + rc = ext4_fs_init_inode_dblk_idx(dir, iblock, &fblock); + else + rc = ext4_fs_append_inode_dblk(dir, &fblock, &iblock); + + if (rc != EOK) + return rc; + + rc = ext4_trans_block_get_noread(dir->fs->bdev, &block, fblock); + if (rc != EOK) + return rc; + + /* Initialize pointers to data structures */ + struct ext4_dir_idx_root *root = (void *)block.data; + struct ext4_dir_idx_rinfo *info = &(root->info); + + memset(root, 0, sizeof(struct ext4_dir_idx_root)); + struct ext4_dir_en *de; + + /* Initialize dot entries */ + de = (struct ext4_dir_en *)root->dots; + ext4_dir_write_entry(sb, de, 12, dir, ".", strlen(".")); + + de = (struct ext4_dir_en *)(root->dots + 1); + uint16_t elen = block_size - 12; + ext4_dir_write_entry(sb, de, elen, parent, "..", strlen("..")); + + /* Initialize root info structure */ + uint8_t hash_version = ext4_get8(&dir->fs->sb, default_hash_version); + + ext4_dir_dx_rinfo_set_hash_version(info, hash_version); + ext4_dir_dx_rinfo_set_indirect_levels(info, 0); + ext4_dir_dx_root_info_set_info_length(info, 8); + + /* Set limit and current number of entries */ + struct ext4_dir_idx_climit *climit; + climit = (struct ext4_dir_idx_climit *)&root->en; + + ext4_dir_dx_climit_set_count(climit, 1); + + uint32_t entry_space; + entry_space = block_size - 2 * sizeof(struct ext4_dir_idx_dot_en) - + sizeof(struct ext4_dir_idx_rinfo); + + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + entry_space -= sizeof(struct ext4_dir_idx_tail); + + uint16_t root_limit = entry_space / sizeof(struct ext4_dir_idx_entry); + ext4_dir_dx_climit_set_limit(climit, root_limit); + + /* Append new block, where will be new entries inserted in the future */ + iblock++; + if (!need_append) + rc = ext4_fs_init_inode_dblk_idx(dir, iblock, &fblock); + else + rc = ext4_fs_append_inode_dblk(dir, &fblock, &iblock); + + if (rc != EOK) { + ext4_block_set(dir->fs->bdev, &block); + return rc; + } + + struct ext4_block new_block; + rc = ext4_trans_block_get_noread(dir->fs->bdev, &new_block, fblock); + if (rc != EOK) { + ext4_block_set(dir->fs->bdev, &block); + return rc; + } + + /* Fill the whole block with empty entry */ + struct ext4_dir_en *be = (void *)new_block.data; + + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + uint16_t len = block_size - sizeof(struct ext4_dir_entry_tail); + ext4_dir_en_set_entry_len(be, len); + ext4_dir_en_set_name_len(sb, be, 0); + ext4_dir_en_set_inode_type(sb, be, EXT4_DE_UNKNOWN); + ext4_dir_init_entry_tail(EXT4_DIRENT_TAIL(be, block_size)); + ext4_dir_set_csum(dir, be); + } else { + ext4_dir_en_set_entry_len(be, block_size); + } + + ext4_dir_en_set_inode(be, 0); + + ext4_trans_set_block_dirty(new_block.buf); + rc = ext4_block_set(dir->fs->bdev, &new_block); + if (rc != EOK) { + ext4_block_set(dir->fs->bdev, &block); + return rc; + } + + /* Connect new block to the only entry in index */ + struct ext4_dir_idx_entry *entry = root->en; + ext4_dir_dx_entry_set_block(entry, iblock); + + ext4_dir_set_dx_csum(dir, (struct ext4_dir_en *)block.data); + ext4_trans_set_block_dirty(block.buf); + + return ext4_block_set(dir->fs->bdev, &block); +} + +/**@brief Initialize hash info structure necessary for index operations. + * @param hinfo Pointer to hinfo to be initialized + * @param root_block Root block (number 0) of index + * @param sb Pointer to superblock + * @param name_len Length of name to be computed hash value from + * @param name Name to be computed hash value from + * @return Standard error code + */ +static int ext4_dir_hinfo_init(struct ext4_hash_info *hinfo, + struct ext4_block *root_block, + struct ext4_sblock *sb, size_t name_len, + const char *name) +{ + struct ext4_dir_idx_root *root; + + root = (struct ext4_dir_idx_root *)root_block->data; + if ((root->info.hash_version != EXT2_HTREE_LEGACY) && + (root->info.hash_version != EXT2_HTREE_HALF_MD4) && + (root->info.hash_version != EXT2_HTREE_TEA)) + return EXT4_ERR_BAD_DX_DIR; + + /* Check unused flags */ + if (root->info.unused_flags != 0) + return EXT4_ERR_BAD_DX_DIR; + + /* Check indirect levels */ + if (root->info.indirect_levels > 1) + return EXT4_ERR_BAD_DX_DIR; + + /* Check if node limit is correct */ + uint32_t block_size = ext4_sb_get_block_size(sb); + uint32_t entry_space = block_size; + entry_space -= 2 * sizeof(struct ext4_dir_idx_dot_en); + entry_space -= sizeof(struct ext4_dir_idx_rinfo); + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + entry_space -= sizeof(struct ext4_dir_idx_tail); + entry_space = entry_space / sizeof(struct ext4_dir_idx_entry); + + struct ext4_dir_idx_climit *climit = (void *)&root->en; + uint16_t limit = ext4_dir_dx_climit_get_limit(climit); + if (limit != entry_space) + return EXT4_ERR_BAD_DX_DIR; + + /* Check hash version and modify if necessary */ + hinfo->hash_version = ext4_dir_dx_rinfo_get_hash_version(&root->info); + if ((hinfo->hash_version <= EXT2_HTREE_TEA) && + (ext4_sb_check_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) { + /* Use unsigned hash */ + hinfo->hash_version += 3; + } + + /* Load hash seed from superblock */ + hinfo->seed = ext4_get8(sb, hash_seed); + + /* Compute hash value of name */ + if (name) + return ext4_dir_dx_hash_string(hinfo, name_len, name); + + return EOK; +} + +/**@brief Walk through index tree and load leaf with corresponding hash value. + * @param hinfo Initialized hash info structure + * @param inode_ref Current i-node + * @param root_block Root block (iblock 0), where is root node located + * @param dx_block Pointer to leaf node in dx_blocks array + * @param dx_blocks Array with the whole path from root to leaf + * @return Standard error code + */ +static int ext4_dir_dx_get_leaf(struct ext4_hash_info *hinfo, + struct ext4_inode_ref *inode_ref, + struct ext4_block *root_block, + struct ext4_dir_idx_block **dx_block, + struct ext4_dir_idx_block *dx_blocks) +{ + struct ext4_dir_idx_root *root; + struct ext4_dir_idx_entry *entries; + struct ext4_dir_idx_entry *p; + struct ext4_dir_idx_entry *q; + struct ext4_dir_idx_entry *m; + struct ext4_dir_idx_entry *at; + ext4_fsblk_t fblk; + uint32_t block_size; + uint16_t limit; + uint16_t entry_space; + uint8_t ind_level; + int r; + + struct ext4_dir_idx_block *tmp_dx_blk = dx_blocks; + struct ext4_block *tmp_blk = root_block; + struct ext4_sblock *sb = &inode_ref->fs->sb; + + block_size = ext4_sb_get_block_size(sb); + root = (struct ext4_dir_idx_root *)root_block->data; + entries = (struct ext4_dir_idx_entry *)&root->en; + limit = ext4_dir_dx_climit_get_limit((void *)entries); + ind_level = ext4_dir_dx_rinfo_get_indirect_levels(&root->info); + + /* Walk through the index tree */ + while (true) { + uint16_t cnt = ext4_dir_dx_climit_get_count((void *)entries); + if ((cnt == 0) || (cnt > limit)) + return EXT4_ERR_BAD_DX_DIR; + + /* Do binary search in every node */ + p = entries + 1; + q = entries + cnt - 1; + + while (p <= q) { + m = p + (q - p) / 2; + if (ext4_dir_dx_entry_get_hash(m) > hinfo->hash) + q = m - 1; + else + p = m + 1; + } + + at = p - 1; + + /* Write results */ + memcpy(&tmp_dx_blk->b, tmp_blk, sizeof(struct ext4_block)); + tmp_dx_blk->entries = entries; + tmp_dx_blk->position = at; + + /* Is algorithm in the leaf? */ + if (ind_level == 0) { + *dx_block = tmp_dx_blk; + return EOK; + } + + /* Goto child node */ + uint32_t n_blk = ext4_dir_dx_entry_get_block(at); + + ind_level--; + + r = ext4_fs_get_inode_dblk_idx(inode_ref, n_blk, &fblk, false); + if (r != EOK) + return r; + + r = ext4_trans_block_get(inode_ref->fs->bdev, tmp_blk, fblk); + if (r != EOK) + return r; + + entries = ((struct ext4_dir_idx_node *)tmp_blk->data)->entries; + limit = ext4_dir_dx_climit_get_limit((void *)entries); + + entry_space = block_size - sizeof(struct ext4_fake_dir_entry); + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + entry_space -= sizeof(struct ext4_dir_idx_tail); + + entry_space = entry_space / sizeof(struct ext4_dir_idx_entry); + + if (limit != entry_space) { + ext4_block_set(inode_ref->fs->bdev, tmp_blk); + return EXT4_ERR_BAD_DX_DIR; + } + + if (!ext4_dir_dx_csum_verify(inode_ref, (void *)tmp_blk->data)) { + ext4_dbg(DEBUG_DIR_IDX, + DBG_WARN "HTree checksum failed." + "Inode: %" PRIu32", " + "Block: %" PRIu32"\n", + inode_ref->index, + n_blk); + } + + ++tmp_dx_blk; + } + + /* Unreachable */ + return EOK; +} + +/**@brief Check if the the next block would be checked during entry search. + * @param inode_ref Directory i-node + * @param hash Hash value to check + * @param dx_block Current block + * @param dx_blocks Array with path from root to leaf node + * @return Standard Error code + */ +static int ext4_dir_dx_next_block(struct ext4_inode_ref *inode_ref, + uint32_t hash, + struct ext4_dir_idx_block *dx_block, + struct ext4_dir_idx_block *dx_blocks) +{ + int r; + uint32_t num_handles = 0; + ext4_fsblk_t blk_adr; + struct ext4_dir_idx_block *p = dx_block; + + /* Try to find data block with next bunch of entries */ + while (true) { + uint16_t cnt = ext4_dir_dx_climit_get_count((void *)p->entries); + + p->position++; + if (p->position < p->entries + cnt) + break; + + if (p == dx_blocks) + return EOK; + + num_handles++; + p--; + } + + /* Check hash collision (if not occurred - no next block cannot be + * used)*/ + uint32_t current_hash = ext4_dir_dx_entry_get_hash(p->position); + if ((hash & 1) == 0) { + if ((current_hash & ~1) != hash) + return 0; + } + + /* Fill new path */ + while (num_handles--) { + uint32_t blk = ext4_dir_dx_entry_get_block(p->position); + r = ext4_fs_get_inode_dblk_idx(inode_ref, blk, &blk_adr, false); + if (r != EOK) + return r; + + struct ext4_block b; + r = ext4_trans_block_get(inode_ref->fs->bdev, &b, blk_adr); + if (r != EOK) + return r; + + if (!ext4_dir_dx_csum_verify(inode_ref, (void *)b.data)) { + ext4_dbg(DEBUG_DIR_IDX, + DBG_WARN "HTree checksum failed." + "Inode: %" PRIu32", " + "Block: %" PRIu32"\n", + inode_ref->index, + blk); + } + + p++; + + /* Don't forget to put old block (prevent memory leak) */ + r = ext4_block_set(inode_ref->fs->bdev, &p->b); + if (r != EOK) + return r; + + memcpy(&p->b, &b, sizeof(b)); + p->entries = ((struct ext4_dir_idx_node *)b.data)->entries; + p->position = p->entries; + } + + return ENOENT; +} + +int ext4_dir_dx_find_entry(struct ext4_dir_search_result *result, + struct ext4_inode_ref *inode_ref, size_t name_len, + const char *name) +{ + /* Load direct block 0 (index root) */ + ext4_fsblk_t root_block_addr; + int rc2; + int rc; + rc = ext4_fs_get_inode_dblk_idx(inode_ref, 0, &root_block_addr, false); + if (rc != EOK) + return rc; + + struct ext4_fs *fs = inode_ref->fs; + + struct ext4_block root_block; + rc = ext4_trans_block_get(fs->bdev, &root_block, root_block_addr); + if (rc != EOK) + return rc; + + if (!ext4_dir_dx_csum_verify(inode_ref, (void *)root_block.data)) { + ext4_dbg(DEBUG_DIR_IDX, + DBG_WARN "HTree root checksum failed." + "Inode: %" PRIu32", " + "Block: %" PRIu32"\n", + inode_ref->index, + (uint32_t)0); + } + + /* Initialize hash info (compute hash value) */ + struct ext4_hash_info hinfo; + rc = ext4_dir_hinfo_init(&hinfo, &root_block, &fs->sb, name_len, name); + if (rc != EOK) { + ext4_block_set(fs->bdev, &root_block); + return EXT4_ERR_BAD_DX_DIR; + } + + /* + * Hardcoded number 2 means maximum height of index tree, + * specified in the Linux driver. + */ + struct ext4_dir_idx_block dx_blocks[2]; + struct ext4_dir_idx_block *dx_block; + struct ext4_dir_idx_block *tmp; + + rc = ext4_dir_dx_get_leaf(&hinfo, inode_ref, &root_block, &dx_block, + dx_blocks); + if (rc != EOK) { + ext4_block_set(fs->bdev, &root_block); + return EXT4_ERR_BAD_DX_DIR; + } + + do { + /* Load leaf block */ + uint32_t leaf_blk_idx; + ext4_fsblk_t leaf_block_addr; + struct ext4_block b; + + leaf_blk_idx = ext4_dir_dx_entry_get_block(dx_block->position); + rc = ext4_fs_get_inode_dblk_idx(inode_ref, leaf_blk_idx, + &leaf_block_addr, false); + if (rc != EOK) + goto cleanup; + + rc = ext4_trans_block_get(fs->bdev, &b, leaf_block_addr); + if (rc != EOK) + goto cleanup; + + if (!ext4_dir_csum_verify(inode_ref, (void *)b.data)) { + ext4_dbg(DEBUG_DIR_IDX, + DBG_WARN "HTree leaf block checksum failed." + "Inode: %" PRIu32", " + "Block: %" PRIu32"\n", + inode_ref->index, + leaf_blk_idx); + } + + /* Linear search inside block */ + struct ext4_dir_en *de; + rc = ext4_dir_find_in_block(&b, &fs->sb, name_len, name, &de); + + /* Found => return it */ + if (rc == EOK) { + result->block = b; + result->dentry = de; + goto cleanup; + } + + /* Not found, leave untouched */ + rc2 = ext4_block_set(fs->bdev, &b); + if (rc2 != EOK) + goto cleanup; + + if (rc != ENOENT) + goto cleanup; + + /* check if the next block could be checked */ + rc = ext4_dir_dx_next_block(inode_ref, hinfo.hash, dx_block, + &dx_blocks[0]); + if (rc < 0) + goto cleanup; + } while (rc == ENOENT); + + /* Entry not found */ + rc = ENOENT; + +cleanup: + /* The whole path must be released (preventing memory leak) */ + tmp = dx_blocks; + + while (tmp <= dx_block) { + rc2 = ext4_block_set(fs->bdev, &tmp->b); + if (rc == EOK && rc2 != EOK) + rc = rc2; + ++tmp; + } + + return rc; +} + +/**@brief Compare function used to pass in quicksort implementation. + * It can compare two entries by hash value. + * @param arg1 First entry + * @param arg2 Second entry + * @param dummy Unused parameter, can be NULL + * + * @return Classic compare result + * (0: equal, -1: arg1 < arg2, 1: arg1 > arg2) + */ +static int ext4_dir_dx_entry_comparator(const void *arg1, const void *arg2) +{ + struct ext4_dx_sort_entry *entry1 = (void *)arg1; + struct ext4_dx_sort_entry *entry2 = (void *)arg2; + + if (entry1->hash == entry2->hash) + return 0; + + if (entry1->hash < entry2->hash) + return -1; + else + return 1; +} + +/**@brief Insert new index entry to block. + * Note that space for new entry must be checked by caller. + * @param inode_ref Directory i-node + * @param index_block Block where to insert new entry + * @param hash Hash value covered by child node + * @param iblock Logical number of child block + * + */ +static void +ext4_dir_dx_insert_entry(struct ext4_inode_ref *inode_ref __unused, + struct ext4_dir_idx_block *index_block, + uint32_t hash, uint32_t iblock) +{ + struct ext4_dir_idx_entry *old_index_entry = index_block->position; + struct ext4_dir_idx_entry *new_index_entry = old_index_entry + 1; + struct ext4_dir_idx_climit *climit = (void *)index_block->entries; + struct ext4_dir_idx_entry *start_index = index_block->entries; + uint32_t count = ext4_dir_dx_climit_get_count(climit); + + size_t bytes; + bytes = (uint8_t *)(start_index + count) - (uint8_t *)(new_index_entry); + + memmove(new_index_entry + 1, new_index_entry, bytes); + + ext4_dir_dx_entry_set_block(new_index_entry, iblock); + ext4_dir_dx_entry_set_hash(new_index_entry, hash); + ext4_dir_dx_climit_set_count(climit, count + 1); + ext4_dir_set_dx_csum(inode_ref, (void *)index_block->b.data); + ext4_trans_set_block_dirty(index_block->b.buf); +} + +/**@brief Split directory entries to two parts preventing node overflow. + * @param inode_ref Directory i-node + * @param hinfo Hash info + * @param old_data_block Block with data to be split + * @param index_block Block where index entries are located + * @param new_data_block Output value for newly allocated data block + */ +static int ext4_dir_dx_split_data(struct ext4_inode_ref *inode_ref, + struct ext4_hash_info *hinfo, + struct ext4_block *old_data_block, + struct ext4_dir_idx_block *index_block, + struct ext4_block *new_data_block) +{ + int rc = EOK; + struct ext4_sblock *sb = &inode_ref->fs->sb; + uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb); + + /* Allocate buffer for directory entries */ + uint8_t *entry_buffer = ext4_malloc(block_size); + if (entry_buffer == NULL) + return ENOMEM; + + /* dot entry has the smallest size available */ + uint32_t max_ecnt = block_size / sizeof(struct ext4_dir_idx_dot_en); + + /* Allocate sort entry */ + struct ext4_dx_sort_entry *sort; + + sort = ext4_malloc(max_ecnt * sizeof(struct ext4_dx_sort_entry)); + if (sort == NULL) { + ext4_free(entry_buffer); + return ENOMEM; + } + + uint32_t idx = 0; + uint32_t real_size = 0; + + /* Initialize hinfo */ + struct ext4_hash_info hinfo_tmp; + memcpy(&hinfo_tmp, hinfo, sizeof(struct ext4_hash_info)); + + /* Load all valid entries to the buffer */ + struct ext4_dir_en *de = (void *)old_data_block->data; + uint8_t *entry_buffer_ptr = entry_buffer; + while ((void *)de < (void *)(old_data_block->data + block_size)) { + /* Read only valid entries */ + if (ext4_dir_en_get_inode(de) && de->name_len) { + uint16_t len = ext4_dir_en_get_name_len(sb, de); + rc = ext4_dir_dx_hash_string(&hinfo_tmp, len, + (char *)de->name); + if (rc != EOK) { + ext4_free(sort); + ext4_free(entry_buffer); + return rc; + } + + uint32_t rec_len = 8 + len; + if ((rec_len % 4) != 0) + rec_len += 4 - (rec_len % 4); + + memcpy(entry_buffer_ptr, de, rec_len); + + sort[idx].dentry = entry_buffer_ptr; + sort[idx].rec_len = rec_len; + sort[idx].hash = hinfo_tmp.hash; + + entry_buffer_ptr += rec_len; + real_size += rec_len; + idx++; + } + + size_t elen = ext4_dir_en_get_entry_len(de); + de = (void *)((uint8_t *)de + elen); + } + + qsort(sort, idx, sizeof(struct ext4_dx_sort_entry), + ext4_dir_dx_entry_comparator); + + /* Allocate new block for store the second part of entries */ + ext4_fsblk_t new_fblock; + uint32_t new_iblock; + rc = ext4_fs_append_inode_dblk(inode_ref, &new_fblock, &new_iblock); + if (rc != EOK) { + ext4_free(sort); + ext4_free(entry_buffer); + return rc; + } + + /* Load new block */ + struct ext4_block new_data_block_tmp; + rc = ext4_trans_block_get_noread(inode_ref->fs->bdev, &new_data_block_tmp, + new_fblock); + if (rc != EOK) { + ext4_free(sort); + ext4_free(entry_buffer); + return rc; + } + + /* + * Distribute entries to two blocks (by size) + * - compute the half + */ + uint32_t new_hash = 0; + uint32_t current_size = 0; + uint32_t mid = 0; + uint32_t i; + for (i = 0; i < idx; ++i) { + if ((current_size + sort[i].rec_len) > (block_size / 2)) { + new_hash = sort[i].hash; + mid = i; + break; + } + + current_size += sort[i].rec_len; + } + + /* Check hash collision */ + uint32_t continued = 0; + if (new_hash == sort[mid - 1].hash) + continued = 1; + + uint32_t off = 0; + void *ptr; + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + block_size -= sizeof(struct ext4_dir_entry_tail); + + /* First part - to the old block */ + for (i = 0; i < mid; ++i) { + ptr = old_data_block->data + off; + memcpy(ptr, sort[i].dentry, sort[i].rec_len); + + struct ext4_dir_en *t = ptr; + if (i < (mid - 1)) + ext4_dir_en_set_entry_len(t, sort[i].rec_len); + else + ext4_dir_en_set_entry_len(t, block_size - off); + + off += sort[i].rec_len; + } + + /* Second part - to the new block */ + off = 0; + for (i = mid; i < idx; ++i) { + ptr = new_data_block_tmp.data + off; + memcpy(ptr, sort[i].dentry, sort[i].rec_len); + + struct ext4_dir_en *t = ptr; + if (i < (idx - 1)) + ext4_dir_en_set_entry_len(t, sort[i].rec_len); + else + ext4_dir_en_set_entry_len(t, block_size - off); + + off += sort[i].rec_len; + } + + block_size = ext4_sb_get_block_size(&inode_ref->fs->sb); + + /* Do some steps to finish operation */ + sb = &inode_ref->fs->sb; + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + struct ext4_dir_entry_tail *t; + + t = EXT4_DIRENT_TAIL(old_data_block->data, block_size); + ext4_dir_init_entry_tail(t); + t = EXT4_DIRENT_TAIL(new_data_block_tmp.data, block_size); + ext4_dir_init_entry_tail(t); + } + ext4_dir_set_csum(inode_ref, (void *)old_data_block->data); + ext4_dir_set_csum(inode_ref, (void *)new_data_block_tmp.data); + ext4_trans_set_block_dirty(old_data_block->buf); + ext4_trans_set_block_dirty(new_data_block_tmp.buf); + + ext4_free(sort); + ext4_free(entry_buffer); + + ext4_dir_dx_insert_entry(inode_ref, index_block, new_hash + continued, + new_iblock); + + *new_data_block = new_data_block_tmp; + return EOK; +} + +/**@brief Split index node and maybe some parent nodes in the tree hierarchy. + * @param inode_ref Directory i-node + * @param dx_blocks Array with path from root to leaf node + * @param dx_block Leaf block to be split if needed + * @return Error code + */ +static int +ext4_dir_dx_split_index(struct ext4_inode_ref *ino_ref, + struct ext4_dir_idx_block *dx_blks, + struct ext4_dir_idx_block *dxb, + struct ext4_dir_idx_block **new_dx_block) +{ + struct ext4_sblock *sb = &ino_ref->fs->sb; + struct ext4_dir_idx_entry *e; + int r; + + uint32_t block_size = ext4_sb_get_block_size(&ino_ref->fs->sb); + uint32_t entry_space = block_size - sizeof(struct ext4_fake_dir_entry); + uint32_t node_limit = entry_space / sizeof(struct ext4_dir_idx_entry); + + bool meta_csum = ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM); + + if (dxb == dx_blks) + e = ((struct ext4_dir_idx_root *)dxb->b.data)->en; + else + e = ((struct ext4_dir_idx_node *)dxb->b.data)->entries; + + struct ext4_dir_idx_climit *climit = (struct ext4_dir_idx_climit *)e; + + uint16_t leaf_limit = ext4_dir_dx_climit_get_limit(climit); + uint16_t leaf_count = ext4_dir_dx_climit_get_count(climit); + + /* Check if is necessary to split index block */ + if (leaf_limit == leaf_count) { + struct ext4_dir_idx_entry *ren; + ptrdiff_t levels = dxb - dx_blks; + + ren = ((struct ext4_dir_idx_root *)dx_blks[0].b.data)->en; + struct ext4_dir_idx_climit *rclimit = (void *)ren; + uint16_t root_limit = ext4_dir_dx_climit_get_limit(rclimit); + uint16_t root_count = ext4_dir_dx_climit_get_count(rclimit); + + + /* Linux limitation */ + if ((levels > 0) && (root_limit == root_count)) + return ENOSPC; + + /* Add new block to directory */ + ext4_fsblk_t new_fblk; + uint32_t new_iblk; + r = ext4_fs_append_inode_dblk(ino_ref, &new_fblk, &new_iblk); + if (r != EOK) + return r; + + /* load new block */ + struct ext4_block b; + r = ext4_trans_block_get_noread(ino_ref->fs->bdev, &b, new_fblk); + if (r != EOK) + return r; + + struct ext4_dir_idx_node *new_node = (void *)b.data; + struct ext4_dir_idx_entry *new_en = new_node->entries; + + memset(&new_node->fake, 0, sizeof(struct ext4_fake_dir_entry)); + new_node->fake.entry_length = block_size; + + /* Split leaf node */ + if (levels > 0) { + uint32_t count_left = leaf_count / 2; + uint32_t count_right = leaf_count - count_left; + uint32_t hash_right; + size_t sz; + + struct ext4_dir_idx_climit *left_climit; + struct ext4_dir_idx_climit *right_climit; + + hash_right = ext4_dir_dx_entry_get_hash(e + count_left); + /* Copy data to new node */ + sz = count_right * sizeof(struct ext4_dir_idx_entry); + memcpy(new_en, e + count_left, sz); + + /* Initialize new node */ + left_climit = (struct ext4_dir_idx_climit *)e; + right_climit = (struct ext4_dir_idx_climit *)new_en; + + ext4_dir_dx_climit_set_count(left_climit, count_left); + ext4_dir_dx_climit_set_count(right_climit, count_right); + + if (meta_csum) + entry_space -= sizeof(struct ext4_dir_idx_tail); + + ext4_dir_dx_climit_set_limit(right_climit, node_limit); + + /* Which index block is target for new entry */ + uint32_t position_index = + (dxb->position - dxb->entries); + if (position_index >= count_left) { + ext4_dir_set_dx_csum( + ino_ref, + (struct ext4_dir_en *) + dxb->b.data); + ext4_trans_set_block_dirty(dxb->b.buf); + + struct ext4_block block_tmp = dxb->b; + + dxb->b = b; + + dxb->position = + new_en + position_index - count_left; + dxb->entries = new_en; + + b = block_tmp; + } + + /* Finally insert new entry */ + ext4_dir_dx_insert_entry(ino_ref, dx_blks, hash_right, + new_iblk); + ext4_dir_set_dx_csum(ino_ref, (void*)dx_blks[0].b.data); + ext4_dir_set_dx_csum(ino_ref, (void*)dx_blks[1].b.data); + ext4_trans_set_block_dirty(dx_blks[0].b.buf); + ext4_trans_set_block_dirty(dx_blks[1].b.buf); + + ext4_dir_set_dx_csum(ino_ref, (void *)b.data); + ext4_trans_set_block_dirty(b.buf); + return ext4_block_set(ino_ref->fs->bdev, &b); + } else { + size_t sz; + /* Copy data from root to child block */ + sz = leaf_count * sizeof(struct ext4_dir_idx_entry); + memcpy(new_en, e, sz); + + struct ext4_dir_idx_climit *new_climit = (void*)new_en; + if (meta_csum) + entry_space -= sizeof(struct ext4_dir_idx_tail); + + ext4_dir_dx_climit_set_limit(new_climit, node_limit); + + /* Set values in root node */ + struct ext4_dir_idx_climit *new_root_climit = (void *)e; + + ext4_dir_dx_climit_set_count(new_root_climit, 1); + ext4_dir_dx_entry_set_block(e, new_iblk); + + struct ext4_dir_idx_root *r = (void *)dx_blks[0].b.data; + r->info.indirect_levels = 1; + + /* Add new entry to the path */ + dxb = dx_blks + 1; + dxb->position = dx_blks->position - e + new_en; + dxb->entries = new_en; + dxb->b = b; + *new_dx_block = dxb; + + ext4_dir_set_dx_csum(ino_ref, (void*)dx_blks[0].b.data); + ext4_dir_set_dx_csum(ino_ref, (void*)dx_blks[1].b.data); + ext4_trans_set_block_dirty(dx_blks[0].b.buf); + ext4_trans_set_block_dirty(dx_blks[1].b.buf); + } + } + + return EOK; +} + +int ext4_dir_dx_add_entry(struct ext4_inode_ref *parent, + struct ext4_inode_ref *child, const char *name, uint32_t name_len) +{ + int rc2 = EOK; + int r; + /* Get direct block 0 (index root) */ + ext4_fsblk_t rblock_addr; + r = ext4_fs_get_inode_dblk_idx(parent, 0, &rblock_addr, false); + if (r != EOK) + return r; + + struct ext4_fs *fs = parent->fs; + struct ext4_block root_blk; + + r = ext4_trans_block_get(fs->bdev, &root_blk, rblock_addr); + if (r != EOK) + return r; + + if (!ext4_dir_dx_csum_verify(parent, (void*)root_blk.data)) { + ext4_dbg(DEBUG_DIR_IDX, + DBG_WARN "HTree root checksum failed." + "Inode: %" PRIu32", " + "Block: %" PRIu32"\n", + parent->index, + (uint32_t)0); + } + + /* Initialize hinfo structure (mainly compute hash) */ + struct ext4_hash_info hinfo; + r = ext4_dir_hinfo_init(&hinfo, &root_blk, &fs->sb, name_len, name); + if (r != EOK) { + ext4_block_set(fs->bdev, &root_blk); + return EXT4_ERR_BAD_DX_DIR; + } + + /* + * Hardcoded number 2 means maximum height of index + * tree defined in Linux. + */ + struct ext4_dir_idx_block dx_blks[2]; + struct ext4_dir_idx_block *dx_blk; + struct ext4_dir_idx_block *dx_it; + + r = ext4_dir_dx_get_leaf(&hinfo, parent, &root_blk, &dx_blk, dx_blks); + if (r != EOK) { + r = EXT4_ERR_BAD_DX_DIR; + goto release_index; + } + + /* Try to insert to existing data block */ + uint32_t leaf_block_idx = ext4_dir_dx_entry_get_block(dx_blk->position); + ext4_fsblk_t leaf_block_addr; + r = ext4_fs_get_inode_dblk_idx(parent, leaf_block_idx, + &leaf_block_addr, false); + if (r != EOK) + goto release_index; + + /* + * Check if there is needed to split index node + * (and recursively also parent nodes) + */ + r = ext4_dir_dx_split_index(parent, dx_blks, dx_blk, &dx_blk); + if (r != EOK) + goto release_target_index; + + struct ext4_block target_block; + r = ext4_trans_block_get(fs->bdev, &target_block, leaf_block_addr); + if (r != EOK) + goto release_index; + + if (!ext4_dir_csum_verify(parent,(void *)target_block.data)) { + ext4_dbg(DEBUG_DIR_IDX, + DBG_WARN "HTree leaf block checksum failed." + "Inode: %" PRIu32", " + "Block: %" PRIu32"\n", + parent->index, + leaf_block_idx); + } + + /* Check if insert operation passed */ + r = ext4_dir_try_insert_entry(&fs->sb, parent, &target_block, child, + name, name_len); + if (r == EOK) + goto release_target_index; + + /* Split entries to two blocks (includes sorting by hash value) */ + struct ext4_block new_block; + r = ext4_dir_dx_split_data(parent, &hinfo, &target_block, dx_blk, + &new_block); + if (r != EOK) { + rc2 = r; + goto release_target_index; + } + + /* Where to save new entry */ + uint32_t blk_hash = ext4_dir_dx_entry_get_hash(dx_blk->position + 1); + if (hinfo.hash >= blk_hash) + r = ext4_dir_try_insert_entry(&fs->sb, parent, &new_block, + child, name, name_len); + else + r = ext4_dir_try_insert_entry(&fs->sb, parent, &target_block, + child, name, name_len); + + /* Cleanup */ + r = ext4_block_set(fs->bdev, &new_block); + if (r != EOK) + return r; + +/* Cleanup operations */ + +release_target_index: + rc2 = r; + + r = ext4_block_set(fs->bdev, &target_block); + if (r != EOK) + return r; + +release_index: + if (r != EOK) + rc2 = r; + + dx_it = dx_blks; + + while (dx_it <= dx_blk) { + r = ext4_block_set(fs->bdev, &dx_it->b); + if (r != EOK) + return r; + + dx_it++; + } + + return rc2; +} + +int ext4_dir_dx_reset_parent_inode(struct ext4_inode_ref *dir, + uint32_t parent_inode) +{ + /* Load block 0, where will be index root located */ + ext4_fsblk_t fblock; + int rc = ext4_fs_get_inode_dblk_idx(dir, 0, &fblock, false); + if (rc != EOK) + return rc; + + struct ext4_block block; + rc = ext4_trans_block_get(dir->fs->bdev, &block, fblock); + if (rc != EOK) + return rc; + + if (!ext4_dir_dx_csum_verify(dir, (void *)block.data)) { + ext4_dbg(DEBUG_DIR_IDX, + DBG_WARN "HTree root checksum failed." + "Inode: %" PRIu32", " + "Block: %" PRIu32"\n", + dir->index, + (uint32_t)0); + } + + /* Initialize pointers to data structures */ + struct ext4_dir_idx_root *root = (void *)block.data; + + /* Fill the inode field with a new parent ino. */ + ext4_dx_dot_en_set_inode(&root->dots[1], parent_inode); + + ext4_dir_set_dx_csum(dir, (void *)block.data); + ext4_trans_set_block_dirty(block.buf); + + return ext4_block_set(dir->fs->bdev, &block); +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_extent.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_extent.c new file mode 100644 index 0000000..ad64a27 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_extent.c @@ -0,0 +1,2140 @@ +/* + * Copyright (c) 2017 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * Copyright (c) 2017 Kaho Ng (ngkaho1234@gmail.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_blockdev.h" +#include "ext4_trans.h" +#include "ext4_fs.h" +#include "ext4_super.h" +#include "ext4_crc32.h" +#include "ext4_balloc.h" +#include "ext4_extent.h" + +#include +#include +#include +#include + +#if CONFIG_EXTENTS_ENABLE +/* + * used by extent splitting. + */ +#define EXT4_EXT_MARK_UNWRIT1 0x02 /* mark first half unwritten */ +#define EXT4_EXT_MARK_UNWRIT2 0x04 /* mark second half unwritten */ +#define EXT4_EXT_DATA_VALID1 0x08 /* first half contains valid data */ +#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */ +#define EXT4_EXT_NO_COMBINE 0x20 /* do not combine two extents */ + +#define EXT4_EXT_UNWRITTEN_MASK (1L << 15) + +#define EXT4_EXT_MAX_LEN_WRITTEN (1L << 15) +#define EXT4_EXT_MAX_LEN_UNWRITTEN \ + (EXT4_EXT_MAX_LEN_WRITTEN - 1) + +#define EXT4_EXT_GET_LEN(ex) to_le16((ex)->block_count) +#define EXT4_EXT_GET_LEN_UNWRITTEN(ex) \ + (EXT4_EXT_GET_LEN(ex) & ~(EXT4_EXT_UNWRITTEN_MASK)) +#define EXT4_EXT_SET_LEN(ex, count) \ + ((ex)->block_count = to_le16(count)) + +#define EXT4_EXT_IS_UNWRITTEN(ex) \ + (EXT4_EXT_GET_LEN(ex) > EXT4_EXT_MAX_LEN_WRITTEN) +#define EXT4_EXT_SET_UNWRITTEN(ex) \ + ((ex)->block_count |= to_le16(EXT4_EXT_UNWRITTEN_MASK)) +#define EXT4_EXT_SET_WRITTEN(ex) \ + ((ex)->block_count &= ~(to_le16(EXT4_EXT_UNWRITTEN_MASK))) + +/* + * Array of ext4_ext_path contains path to some extent. + * Creation/lookup routines use it for traversal/splitting/etc. + * Truncate uses it to simulate recursive walking. + */ +struct ext4_extent_path { + ext4_fsblk_t p_block; + struct ext4_block block; + int32_t depth; + int32_t maxdepth; + struct ext4_extent_header *header; + struct ext4_extent_index *index; + struct ext4_extent *extent; +}; + + +#pragma pack(push, 1) + +/* + * This is the extent tail on-disk structure. + * All other extent structures are 12 bytes long. It turns out that + * block_size % 12 >= 4 for at least all powers of 2 greater than 512, which + * covers all valid ext4 block sizes. Therefore, this tail structure can be + * crammed into the end of the block without having to rebalance the tree. + */ +struct ext4_extent_tail +{ + uint32_t et_checksum; /* crc32c(uuid+inum+extent_block) */ +}; + +/* + * This is the extent on-disk structure. + * It's used at the bottom of the tree. + */ +struct ext4_extent { + uint32_t first_block; /* First logical block extent covers */ + uint16_t block_count; /* Number of blocks covered by extent */ + uint16_t start_hi; /* High 16 bits of physical block */ + uint32_t start_lo; /* Low 32 bits of physical block */ +}; + +/* + * This is index on-disk structure. + * It's used at all the levels except the bottom. + */ +struct ext4_extent_index { + uint32_t first_block; /* Index covers logical blocks from 'block' */ + + /** + * Pointer to the physical block of the next + * level. leaf or next index could be there + * high 16 bits of physical block + */ + uint32_t leaf_lo; + uint16_t leaf_hi; + uint16_t padding; +}; + +/* + * Each block (leaves and indexes), even inode-stored has header. + */ +struct ext4_extent_header { + uint16_t magic; + uint16_t entries_count; /* Number of valid entries */ + uint16_t max_entries_count; /* Capacity of store in entries */ + uint16_t depth; /* Has tree real underlying blocks? */ + uint32_t generation; /* generation of the tree */ +}; + +#pragma pack(pop) + + +#define EXT4_EXTENT_MAGIC 0xF30A + +#define EXT4_EXTENT_FIRST(header) \ + ((struct ext4_extent *)(((char *)(header)) + \ + sizeof(struct ext4_extent_header))) + +#define EXT4_EXTENT_FIRST_INDEX(header) \ + ((struct ext4_extent_index *)(((char *)(header)) + \ + sizeof(struct ext4_extent_header))) + +/* + * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an + * initialized extent. This is 2^15 and not (2^16 - 1), since we use the + * MSB of ee_len field in the extent datastructure to signify if this + * particular extent is an initialized extent or an uninitialized (i.e. + * preallocated). + * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an + * uninitialized extent. + * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an + * uninitialized one. In other words, if MSB of ee_len is set, it is an + * uninitialized extent with only one special scenario when ee_len = 0x8000. + * In this case we can not have an uninitialized extent of zero length and + * thus we make it as a special case of initialized extent with 0x8000 length. + * This way we get better extent-to-group alignment for initialized extents. + * Hence, the maximum number of blocks we can have in an *initialized* + * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767). + */ +#define EXT_INIT_MAX_LEN (1L << 15) +#define EXT_UNWRITTEN_MAX_LEN (EXT_INIT_MAX_LEN - 1) + +#define EXT_EXTENT_SIZE sizeof(struct ext4_extent) +#define EXT_INDEX_SIZE sizeof(struct ext4_extent_idx) + +#define EXT_FIRST_EXTENT(__hdr__) \ + ((struct ext4_extent *)(((char *)(__hdr__)) + \ + sizeof(struct ext4_extent_header))) +#define EXT_FIRST_INDEX(__hdr__) \ + ((struct ext4_extent_index *)(((char *)(__hdr__)) + \ + sizeof(struct ext4_extent_header))) +#define EXT_HAS_FREE_INDEX(__path__) \ + (to_le16((__path__)->header->entries_count) < \ + to_le16((__path__)->header->max_entries_count)) +#define EXT_LAST_EXTENT(__hdr__) \ + (EXT_FIRST_EXTENT((__hdr__)) + to_le16((__hdr__)->entries_count) - 1) +#define EXT_LAST_INDEX(__hdr__) \ + (EXT_FIRST_INDEX((__hdr__)) + to_le16((__hdr__)->entries_count) - 1) +#define EXT_MAX_EXTENT(__hdr__) \ + (EXT_FIRST_EXTENT((__hdr__)) + to_le16((__hdr__)->max_entries_count) - 1) +#define EXT_MAX_INDEX(__hdr__) \ + (EXT_FIRST_INDEX((__hdr__)) + to_le16((__hdr__)->max_entries_count) - 1) + +#define EXT4_EXTENT_TAIL_OFFSET(hdr) \ + (sizeof(struct ext4_extent_header) + \ + (sizeof(struct ext4_extent) * to_le16((hdr)->max_entries_count))) + + +/**@brief Get logical number of the block covered by extent. + * @param extent Extent to load number from + * @return Logical number of the first block covered by extent */ +static inline uint32_t ext4_extent_get_first_block(struct ext4_extent *extent) +{ + return to_le32(extent->first_block); +} + +/**@brief Set logical number of the first block covered by extent. + * @param extent Extent to set number to + * @param iblock Logical number of the first block covered by extent */ +static inline void ext4_extent_set_first_block(struct ext4_extent *extent, + uint32_t iblock) +{ + extent->first_block = to_le32(iblock); +} + +/**@brief Get number of blocks covered by extent. + * @param extent Extent to load count from + * @return Number of blocks covered by extent */ +static inline uint16_t ext4_extent_get_block_count(struct ext4_extent *extent) +{ + if (EXT4_EXT_IS_UNWRITTEN(extent)) + return EXT4_EXT_GET_LEN_UNWRITTEN(extent); + else + return EXT4_EXT_GET_LEN(extent); +} +/**@brief Set number of blocks covered by extent. + * @param extent Extent to load count from + * @param count Number of blocks covered by extent + * @param unwritten Whether the extent is unwritten or not */ +static inline void ext4_extent_set_block_count(struct ext4_extent *extent, + uint16_t count, bool unwritten) +{ + EXT4_EXT_SET_LEN(extent, count); + if (unwritten) + EXT4_EXT_SET_UNWRITTEN(extent); +} + +/**@brief Get physical number of the first block covered by extent. + * @param extent Extent to load number + * @return Physical number of the first block covered by extent */ +static inline uint64_t ext4_extent_get_start(struct ext4_extent *extent) +{ + return ((uint64_t)to_le16(extent->start_hi)) << 32 | + ((uint64_t)to_le32(extent->start_lo)); +} + + +/**@brief Set physical number of the first block covered by extent. + * @param extent Extent to load number + * @param fblock Physical number of the first block covered by extent */ +static inline void ext4_extent_set_start(struct ext4_extent *extent, uint64_t fblock) +{ + extent->start_lo = to_le32((fblock << 32) >> 32); + extent->start_hi = to_le16((uint16_t)(fblock >> 32)); +} + + +/**@brief Get logical number of the block covered by extent index. + * @param index Extent index to load number from + * @return Logical number of the first block covered by extent index */ +static inline uint32_t +ext4_extent_index_get_first_block(struct ext4_extent_index *index) +{ + return to_le32(index->first_block); +} + +/**@brief Set logical number of the block covered by extent index. + * @param index Extent index to set number to + * @param iblock Logical number of the first block covered by extent index */ +static inline void +ext4_extent_index_set_first_block(struct ext4_extent_index *index, + uint32_t iblock) +{ + index->first_block = to_le32(iblock); +} + +/**@brief Get physical number of block where the child node is located. + * @param index Extent index to load number from + * @return Physical number of the block with child node */ +static inline uint64_t +ext4_extent_index_get_leaf(struct ext4_extent_index *index) +{ + return ((uint64_t)to_le16(index->leaf_hi)) << 32 | + ((uint64_t)to_le32(index->leaf_lo)); +} + +/**@brief Set physical number of block where the child node is located. + * @param index Extent index to set number to + * @param fblock Ohysical number of the block with child node */ +static inline void ext4_extent_index_set_leaf(struct ext4_extent_index *index, + uint64_t fblock) +{ + index->leaf_lo = to_le32((fblock << 32) >> 32); + index->leaf_hi = to_le16((uint16_t)(fblock >> 32)); +} + +/**@brief Get magic value from extent header. + * @param header Extent header to load value from + * @return Magic value of extent header */ +static inline uint16_t +ext4_extent_header_get_magic(struct ext4_extent_header *header) +{ + return to_le16(header->magic); +} + +/**@brief Set magic value to extent header. + * @param header Extent header to set value to + * @param magic Magic value of extent header */ +static inline void ext4_extent_header_set_magic(struct ext4_extent_header *header, + uint16_t magic) +{ + header->magic = to_le16(magic); +} + +/**@brief Get number of entries from extent header + * @param header Extent header to get value from + * @return Number of entries covered by extent header */ +static inline uint16_t +ext4_extent_header_get_entries_count(struct ext4_extent_header *header) +{ + return to_le16(header->entries_count); +} + +/**@brief Set number of entries to extent header + * @param header Extent header to set value to + * @param count Number of entries covered by extent header */ +static inline void +ext4_extent_header_set_entries_count(struct ext4_extent_header *header, + uint16_t count) +{ + header->entries_count = to_le16(count); +} + +/**@brief Get maximum number of entries from extent header + * @param header Extent header to get value from + * @return Maximum number of entries covered by extent header */ +static inline uint16_t +ext4_extent_header_get_max_entries_count(struct ext4_extent_header *header) +{ + return to_le16(header->max_entries_count); +} + +/**@brief Set maximum number of entries to extent header + * @param header Extent header to set value to + * @param max_count Maximum number of entries covered by extent header */ +static inline void +ext4_extent_header_set_max_entries_count(struct ext4_extent_header *header, + uint16_t max_count) +{ + header->max_entries_count = to_le16(max_count); +} + +/**@brief Get depth of extent subtree. + * @param header Extent header to get value from + * @return Depth of extent subtree */ +static inline uint16_t +ext4_extent_header_get_depth(struct ext4_extent_header *header) +{ + return to_le16(header->depth); +} + +/**@brief Set depth of extent subtree. + * @param header Extent header to set value to + * @param depth Depth of extent subtree */ +static inline void +ext4_extent_header_set_depth(struct ext4_extent_header *header, uint16_t depth) +{ + header->depth = to_le16(depth); +} + +/**@brief Get generation from extent header + * @param header Extent header to get value from + * @return Generation */ +static inline uint32_t +ext4_extent_header_get_generation(struct ext4_extent_header *header) +{ + return to_le32(header->generation); +} + +/**@brief Set generation to extent header + * @param header Extent header to set value to + * @param generation Generation */ +static inline void +ext4_extent_header_set_generation(struct ext4_extent_header *header, + uint32_t generation) +{ + header->generation = to_le32(generation); +} + +void ext4_extent_tree_init(struct ext4_inode_ref *inode_ref) +{ + /* Initialize extent root header */ + struct ext4_extent_header *header = + ext4_inode_get_extent_header(inode_ref->inode); + ext4_extent_header_set_depth(header, 0); + ext4_extent_header_set_entries_count(header, 0); + ext4_extent_header_set_generation(header, 0); + ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC); + + uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) - + sizeof(struct ext4_extent_header)) / + sizeof(struct ext4_extent); + + ext4_extent_header_set_max_entries_count(header, max_entries); + inode_ref->dirty = true; +} + + +static struct ext4_extent_tail * +find_ext4_extent_tail(struct ext4_extent_header *eh) +{ + return (struct ext4_extent_tail *)(((char *)eh) + + EXT4_EXTENT_TAIL_OFFSET(eh)); +} + +static struct ext4_extent_header *ext_inode_hdr(struct ext4_inode *inode) +{ + return (struct ext4_extent_header *)inode->blocks; +} + +static struct ext4_extent_header *ext_block_hdr(struct ext4_block *block) +{ + return (struct ext4_extent_header *)block->data; +} + +static uint16_t ext_depth(struct ext4_inode *inode) +{ + return to_le16(ext_inode_hdr(inode)->depth); +} + +static uint16_t ext4_ext_get_actual_len(struct ext4_extent *ext) +{ + return (to_le16(ext->block_count) <= EXT_INIT_MAX_LEN + ? to_le16(ext->block_count) + : (to_le16(ext->block_count) - EXT_INIT_MAX_LEN)); +} + +static void ext4_ext_mark_initialized(struct ext4_extent *ext) +{ + ext->block_count = to_le16(ext4_ext_get_actual_len(ext)); +} + +static void ext4_ext_mark_unwritten(struct ext4_extent *ext) +{ + ext->block_count |= to_le16(EXT_INIT_MAX_LEN); +} + +static int ext4_ext_is_unwritten(struct ext4_extent *ext) +{ + /* Extent with ee_len of 0x8000 is treated as an initialized extent */ + return (to_le16(ext->block_count) > EXT_INIT_MAX_LEN); +} + +/* + * ext4_ext_pblock: + * combine low and high parts of physical block number into ext4_fsblk_t + */ +static ext4_fsblk_t ext4_ext_pblock(struct ext4_extent *ex) +{ + ext4_fsblk_t block; + + block = to_le32(ex->start_lo); + block |= ((ext4_fsblk_t)to_le16(ex->start_hi) << 31) << 1; + return block; +} + +/* + * ext4_idx_pblock: + * combine low and high parts of a leaf physical block number into ext4_fsblk_t + */ +static ext4_fsblk_t ext4_idx_pblock(struct ext4_extent_index *ix) +{ + ext4_fsblk_t block; + + block = to_le32(ix->leaf_lo); + block |= ((ext4_fsblk_t)to_le16(ix->leaf_hi) << 31) << 1; + return block; +} + +/* + * ext4_ext_store_pblock: + * stores a large physical block number into an extent struct, + * breaking it into parts + */ +static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) +{ + ex->start_lo = to_le32((uint32_t)(pb & 0xffffffff)); + ex->start_hi = to_le16((uint16_t)((pb >> 32)) & 0xffff); +} + +/* + * ext4_idx_store_pblock: + * stores a large physical block number into an index struct, + * breaking it into parts + */ +static void ext4_idx_store_pblock(struct ext4_extent_index *ix, ext4_fsblk_t pb) +{ + ix->leaf_lo = to_le32((uint32_t)(pb & 0xffffffff)); + ix->leaf_hi = to_le16((uint16_t)((pb >> 32)) & 0xffff); +} + +static int ext4_allocate_single_block(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t goal, ext4_fsblk_t *blockp) +{ + return ext4_balloc_alloc_block(inode_ref, goal, blockp); +} + +static ext4_fsblk_t ext4_new_meta_blocks(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t goal, + uint32_t flags __unused, + uint32_t *count, int *errp) +{ + ext4_fsblk_t block = 0; + + *errp = ext4_allocate_single_block(inode_ref, goal, &block); + if (count) + *count = 1; + return block; +} + +static void ext4_ext_free_blocks(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t block, uint32_t count, + uint32_t flags __unused) +{ + ext4_balloc_free_blocks(inode_ref, block, count); +} + +static uint16_t ext4_ext_space_block(struct ext4_inode_ref *inode_ref) +{ + uint16_t size; + uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb); + + size = (block_size - sizeof(struct ext4_extent_header)) / + sizeof(struct ext4_extent); +#ifdef AGGRESSIVE_TEST + if (size > 6) + size = 6; +#endif + return size; +} + +static uint16_t ext4_ext_space_block_idx(struct ext4_inode_ref *inode_ref) +{ + uint16_t size; + uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb); + + size = (block_size - sizeof(struct ext4_extent_header)) / + sizeof(struct ext4_extent_index); +#ifdef AGGRESSIVE_TEST + if (size > 5) + size = 5; +#endif + return size; +} + +static uint16_t ext4_ext_space_root(struct ext4_inode_ref *inode_ref) +{ + uint16_t size; + + size = sizeof(inode_ref->inode->blocks); + size -= sizeof(struct ext4_extent_header); + size /= sizeof(struct ext4_extent); +#ifdef AGGRESSIVE_TEST + if (size > 3) + size = 3; +#endif + return size; +} + +static uint16_t ext4_ext_space_root_idx(struct ext4_inode_ref *inode_ref) +{ + uint16_t size; + + size = sizeof(inode_ref->inode->blocks); + size -= sizeof(struct ext4_extent_header); + size /= sizeof(struct ext4_extent_index); +#ifdef AGGRESSIVE_TEST + if (size > 4) + size = 4; +#endif + return size; +} + +static uint16_t ext4_ext_max_entries(struct ext4_inode_ref *inode_ref, + uint32_t depth) +{ + uint16_t max; + + if (depth == ext_depth(inode_ref->inode)) { + if (depth == 0) + max = ext4_ext_space_root(inode_ref); + else + max = ext4_ext_space_root_idx(inode_ref); + } else { + if (depth == 0) + max = ext4_ext_space_block(inode_ref); + else + max = ext4_ext_space_block_idx(inode_ref); + } + + return max; +} + +static ext4_fsblk_t ext4_ext_find_goal(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path, + ext4_lblk_t block) +{ + if (path) { + uint32_t depth = path->depth; + struct ext4_extent *ex; + + /* + * Try to predict block placement assuming that we are + * filling in a file which will eventually be + * non-sparse --- i.e., in the case of libbfd writing + * an ELF object sections out-of-order but in a way + * the eventually results in a contiguous object or + * executable file, or some database extending a table + * space file. However, this is actually somewhat + * non-ideal if we are writing a sparse file such as + * qemu or KVM writing a raw image file that is going + * to stay fairly sparse, since it will end up + * fragmenting the file system's free space. Maybe we + * should have some hueristics or some way to allow + * userspace to pass a hint to file system, + * especially if the latter case turns out to be + * common. + */ + ex = path[depth].extent; + if (ex) { + ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); + ext4_lblk_t ext_block = to_le32(ex->first_block); + + if (block > ext_block) + return ext_pblk + (block - ext_block); + else + return ext_pblk - (ext_block - block); + } + + /* it looks like index is empty; + * try to find starting block from index itself */ + if (path[depth].block.lb_id) + return path[depth].block.lb_id; + } + + /* OK. use inode's group */ + return ext4_fs_inode_to_goal_block(inode_ref); +} + +/* + * Allocation for a meta data block + */ +static ext4_fsblk_t ext4_ext_new_meta_block(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path, + struct ext4_extent *ex, int *err, + uint32_t flags) +{ + ext4_fsblk_t goal, newblock; + + goal = ext4_ext_find_goal(inode_ref, path, to_le32(ex->first_block)); + newblock = ext4_new_meta_blocks(inode_ref, goal, flags, NULL, err); + return newblock; +} + +#if CONFIG_META_CSUM_ENABLE +static uint32_t ext4_ext_block_csum(struct ext4_inode_ref *inode_ref, + struct ext4_extent_header *eh) +{ + uint32_t checksum = 0; + struct ext4_sblock *sb = &inode_ref->fs->sb; + + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + uint32_t ino_index = to_le32(inode_ref->index); + uint32_t ino_gen = + to_le32(ext4_inode_get_generation(inode_ref->inode)); + /* First calculate crc32 checksum against fs uuid */ + checksum = + ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid)); + /* Then calculate crc32 checksum against inode number + * and inode generation */ + checksum = ext4_crc32c(checksum, &ino_index, sizeof(ino_index)); + checksum = ext4_crc32c(checksum, &ino_gen, sizeof(ino_gen)); + /* Finally calculate crc32 checksum against + * the entire extent block up to the checksum field */ + checksum = + ext4_crc32c(checksum, eh, EXT4_EXTENT_TAIL_OFFSET(eh)); + } + return checksum; +} +#else +#define ext4_ext_block_csum(...) 0 +#endif + +static void +ext4_extent_block_csum_set(struct ext4_inode_ref *inode_ref __unused, + struct ext4_extent_header *eh) +{ + struct ext4_extent_tail *tail; + + tail = find_ext4_extent_tail(eh); + tail->et_checksum = to_le32(ext4_ext_block_csum(inode_ref, eh)); +} + +static int ext4_ext_dirty(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path) +{ + if (path->block.lb_id) + ext4_trans_set_block_dirty(path->block.buf); + else + inode_ref->dirty = true; + + return EOK; +} + +static void ext4_ext_drop_refs(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path, bool keep_other) +{ + int32_t depth, i; + + if (!path) + return; + if (keep_other) + depth = 0; + else + depth = path->depth; + + for (i = 0; i <= depth; i++, path++) { + if (path->block.lb_id) { + if (ext4_bcache_test_flag(path->block.buf, BC_DIRTY)) + ext4_extent_block_csum_set(inode_ref, + path->header); + + ext4_block_set(inode_ref->fs->bdev, &path->block); + } + } +} + +/* + * Check that whether the basic information inside the extent header + * is correct or not. + */ +static int ext4_ext_check(struct ext4_inode_ref *inode_ref, + struct ext4_extent_header *eh, uint16_t depth, + ext4_fsblk_t pblk __unused) +{ + struct ext4_extent_tail *tail; + struct ext4_sblock *sb = &inode_ref->fs->sb; + const char *error_msg; + (void)error_msg; + + if (to_le16(eh->magic) != EXT4_EXTENT_MAGIC) { + error_msg = "invalid magic"; + goto corrupted; + } + if (to_le16(eh->depth) != depth) { + error_msg = "unexpected eh_depth"; + goto corrupted; + } + if (eh->max_entries_count == 0) { + error_msg = "invalid eh_max"; + goto corrupted; + } + if (to_le16(eh->entries_count) > to_le16(eh->max_entries_count)) { + error_msg = "invalid eh_entries"; + goto corrupted; + } + + tail = find_ext4_extent_tail(eh); + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + if (tail->et_checksum != + to_le32(ext4_ext_block_csum(inode_ref, eh))) { + ext4_dbg(DEBUG_EXTENT, + DBG_WARN "Extent block checksum failed." + "Blocknr: %" PRIu64 "\n", + pblk); + } + } + + return EOK; + +corrupted: + ext4_dbg(DEBUG_EXTENT, "Bad extents B+ tree block: %s. " + "Blocknr: %" PRId64 "\n", + error_msg, pblk); + return EIO; +} + +static int read_extent_tree_block(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t pblk, int32_t depth, + struct ext4_block *bh, + uint32_t flags __unused) +{ + int err; + + err = ext4_trans_block_get(inode_ref->fs->bdev, bh, pblk); + if (err != EOK) + goto errout; + + err = ext4_ext_check(inode_ref, ext_block_hdr(bh), depth, pblk); + if (err != EOK) + goto errout; + + return EOK; +errout: + if (bh->lb_id) + ext4_block_set(inode_ref->fs->bdev, bh); + + return err; +} + +/* + * ext4_ext_binsearch_idx: + * binary search for the closest index of the given block + * the header must be checked before calling this + */ +static void ext4_ext_binsearch_idx(struct ext4_extent_path *path, + ext4_lblk_t block) +{ + struct ext4_extent_header *eh = path->header; + struct ext4_extent_index *r, *l, *m; + + l = EXT_FIRST_INDEX(eh) + 1; + r = EXT_LAST_INDEX(eh); + while (l <= r) { + m = l + (r - l) / 2; + if (block < to_le32(m->first_block)) + r = m - 1; + else + l = m + 1; + } + + path->index = l - 1; +} + +/* + * ext4_ext_binsearch: + * binary search for closest extent of the given block + * the header must be checked before calling this + */ +static void ext4_ext_binsearch(struct ext4_extent_path *path, ext4_lblk_t block) +{ + struct ext4_extent_header *eh = path->header; + struct ext4_extent *r, *l, *m; + + if (eh->entries_count == 0) { + /* + * this leaf is empty: + * we get such a leaf in split/add case + */ + return; + } + + l = EXT_FIRST_EXTENT(eh) + 1; + r = EXT_LAST_EXTENT(eh); + + while (l <= r) { + m = l + (r - l) / 2; + if (block < to_le32(m->first_block)) + r = m - 1; + else + l = m + 1; + } + + path->extent = l - 1; +} + +static int ext4_find_extent(struct ext4_inode_ref *inode_ref, ext4_lblk_t block, + struct ext4_extent_path **orig_path, uint32_t flags) +{ + struct ext4_extent_header *eh; + struct ext4_block bh = EXT4_BLOCK_ZERO(); + ext4_fsblk_t buf_block = 0; + struct ext4_extent_path *path = *orig_path; + int32_t depth, ppos = 0; + int32_t i; + int ret; + + eh = ext_inode_hdr(inode_ref->inode); + depth = ext_depth(inode_ref->inode); + + if (path) { + ext4_ext_drop_refs(inode_ref, path, 0); + if (depth > path[0].maxdepth) { + ext4_free(path); + *orig_path = path = NULL; + } + } + if (!path) { + int32_t path_depth = depth + 1; + /* account possible depth increase */ + path = ext4_calloc(1, sizeof(struct ext4_extent_path) * + (path_depth + 1)); + if (!path) + return ENOMEM; + path[0].maxdepth = path_depth; + } + path[0].header = eh; + path[0].block = bh; + + i = depth; + /* walk through the tree */ + while (i) { + ext4_ext_binsearch_idx(path + ppos, block); + path[ppos].p_block = ext4_idx_pblock(path[ppos].index); + path[ppos].depth = i; + path[ppos].extent = NULL; + buf_block = path[ppos].p_block; + + i--; + ppos++; + if (!path[ppos].block.lb_id || + path[ppos].block.lb_id != buf_block) { + ret = read_extent_tree_block(inode_ref, buf_block, i, + &bh, flags); + if (ret != EOK) { + goto err; + } + if (ppos > depth) { + ext4_block_set(inode_ref->fs->bdev, &bh); + ret = EIO; + goto err; + } + + eh = ext_block_hdr(&bh); + path[ppos].block = bh; + path[ppos].header = eh; + } + } + + path[ppos].depth = i; + path[ppos].extent = NULL; + path[ppos].index = NULL; + + /* find extent */ + ext4_ext_binsearch(path + ppos, block); + /* if not an empty leaf */ + if (path[ppos].extent) + path[ppos].p_block = ext4_ext_pblock(path[ppos].extent); + + *orig_path = path; + + ret = EOK; + return ret; + +err: + ext4_ext_drop_refs(inode_ref, path, 0); + ext4_free(path); + if (orig_path) + *orig_path = NULL; + return ret; +} + +static void ext4_ext_init_header(struct ext4_inode_ref *inode_ref, + struct ext4_extent_header *eh, int32_t depth) +{ + eh->entries_count = 0; + eh->max_entries_count = to_le16(ext4_ext_max_entries(inode_ref, depth)); + eh->magic = to_le16(EXT4_EXTENT_MAGIC); + eh->depth = depth; +} + +static int ext4_ext_insert_index(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path, int at, + ext4_lblk_t insert_index, + ext4_fsblk_t insert_block, bool set_to_ix) +{ + struct ext4_extent_index *ix; + struct ext4_extent_path *curp = path + at; + int len, err; + struct ext4_extent_header *eh; + + if (curp->index && insert_index == to_le32(curp->index->first_block)) + return EIO; + + if (to_le16(curp->header->entries_count) == + to_le16(curp->header->max_entries_count)) + return EIO; + + eh = curp->header; + if (curp->index == NULL) { + ix = EXT_FIRST_INDEX(eh); + curp->index = ix; + } else if (insert_index > to_le32(curp->index->first_block)) { + /* insert after */ + ix = curp->index + 1; + } else { + /* insert before */ + ix = curp->index; + } + + if (ix > EXT_MAX_INDEX(eh)) + return EIO; + + len = EXT_LAST_INDEX(eh) - ix + 1; + ext4_assert(len >= 0); + if (len > 0) + memmove(ix + 1, ix, len * sizeof(struct ext4_extent_index)); + + ix->first_block = to_le32(insert_index); + ext4_idx_store_pblock(ix, insert_block); + eh->entries_count = to_le16(to_le16(eh->entries_count) + 1); + + if (ix > EXT_LAST_INDEX(eh)) { + err = EIO; + goto out; + } + + err = ext4_ext_dirty(inode_ref, curp); + +out: + if (err == EOK && set_to_ix) { + curp->index = ix; + curp->p_block = ext4_idx_pblock(ix); + } + return err; +} + +static int ext4_ext_split_node(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path, int at, + struct ext4_extent *newext, + struct ext4_extent_path *npath, + bool *ins_right_leaf) +{ + int i, npath_at, ret; + ext4_lblk_t insert_index; + ext4_fsblk_t newblock = 0; + int depth = ext_depth(inode_ref->inode); + npath_at = depth - at; + + ext4_assert(at > 0); + + if (path[depth].extent != EXT_MAX_EXTENT(path[depth].header)) + insert_index = path[depth].extent[1].first_block; + else + insert_index = newext->first_block; + + for (i = depth; i >= at; i--, npath_at--) { + struct ext4_block bh = EXT4_BLOCK_ZERO(); + + /* FIXME: currently we split at the point after the current + * extent. */ + newblock = + ext4_ext_new_meta_block(inode_ref, path, newext, &ret, 0); + if (ret != EOK) + goto cleanup; + + /* For write access.*/ + ret = ext4_trans_block_get_noread(inode_ref->fs->bdev, &bh, + newblock); + if (ret != EOK) + goto cleanup; + + if (i == depth) { + /* start copy from next extent */ + int m = EXT_MAX_EXTENT(path[i].header) - path[i].extent; + struct ext4_extent_header *neh; + struct ext4_extent *ex; + neh = ext_block_hdr(&bh); + ex = EXT_FIRST_EXTENT(neh); + ext4_ext_init_header(inode_ref, neh, 0); + if (m) { + memmove(ex, path[i].extent + 1, + sizeof(struct ext4_extent) * m); + neh->entries_count = + to_le16(to_le16(neh->entries_count) + m); + path[i].header->entries_count = to_le16( + to_le16(path[i].header->entries_count) - m); + ret = ext4_ext_dirty(inode_ref, path + i); + if (ret != EOK) + goto cleanup; + + npath[npath_at].p_block = ext4_ext_pblock(ex); + npath[npath_at].extent = ex; + } else { + npath[npath_at].p_block = 0; + npath[npath_at].extent = NULL; + } + + npath[npath_at].depth = to_le16(neh->depth); + npath[npath_at].maxdepth = 0; + npath[npath_at].index = NULL; + npath[npath_at].header = neh; + npath[npath_at].block = bh; + + ext4_trans_set_block_dirty(bh.buf); + } else { + int m = EXT_MAX_INDEX(path[i].header) - path[i].index; + struct ext4_extent_header *neh; + struct ext4_extent_index *ix; + neh = ext_block_hdr(&bh); + ix = EXT_FIRST_INDEX(neh); + ext4_ext_init_header(inode_ref, neh, depth - i); + ix->first_block = to_le32(insert_index); + ext4_idx_store_pblock(ix, + npath[npath_at + 1].block.lb_id); + neh->entries_count = to_le16(1); + if (m) { + memmove(ix + 1, path[i].index + 1, + sizeof(struct ext4_extent) * m); + neh->entries_count = + to_le16(to_le16(neh->entries_count) + m); + path[i].header->entries_count = to_le16( + to_le16(path[i].header->entries_count) - m); + ret = ext4_ext_dirty(inode_ref, path + i); + if (ret != EOK) + goto cleanup; + } + + npath[npath_at].p_block = ext4_idx_pblock(ix); + npath[npath_at].depth = to_le16(neh->depth); + npath[npath_at].maxdepth = 0; + npath[npath_at].extent = NULL; + npath[npath_at].index = ix; + npath[npath_at].header = neh; + npath[npath_at].block = bh; + + ext4_trans_set_block_dirty(bh.buf); + } + } + newblock = 0; + + /* + * If newext->first_block can be included into the + * right sub-tree. + */ + if (to_le32(newext->first_block) < insert_index) + *ins_right_leaf = false; + else + *ins_right_leaf = true; + + ret = ext4_ext_insert_index(inode_ref, path, at - 1, insert_index, + npath[0].block.lb_id, *ins_right_leaf); + +cleanup: + if (ret != EOK) { + if (newblock) + ext4_ext_free_blocks(inode_ref, newblock, 1, 0); + + npath_at = depth - at; + while (npath_at >= 0) { + if (npath[npath_at].block.lb_id) { + newblock = npath[npath_at].block.lb_id; + ext4_block_set(inode_ref->fs->bdev, + &npath[npath_at].block); + ext4_ext_free_blocks(inode_ref, newblock, 1, 0); + memset(&npath[npath_at].block, 0, + sizeof(struct ext4_block)); + } + npath_at--; + } + } + return ret; +} + +/* + * ext4_ext_correct_indexes: + * if leaf gets modified and modified extent is first in the leaf, + * then we have to correct all indexes above. + */ +static int ext4_ext_correct_indexes(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path) +{ + struct ext4_extent_header *eh; + int32_t depth = ext_depth(inode_ref->inode); + struct ext4_extent *ex; + uint32_t border; + int32_t k; + int err = EOK; + + eh = path[depth].header; + ex = path[depth].extent; + + if (ex == NULL || eh == NULL) + return EIO; + + if (depth == 0) { + /* there is no tree at all */ + return EOK; + } + + if (ex != EXT_FIRST_EXTENT(eh)) { + /* we correct tree if first leaf got modified only */ + return EOK; + } + + k = depth - 1; + border = path[depth].extent->first_block; + path[k].index->first_block = border; + err = ext4_ext_dirty(inode_ref, path + k); + if (err != EOK) + return err; + + while (k--) { + /* change all left-side indexes */ + if (path[k + 1].index != EXT_FIRST_INDEX(path[k + 1].header)) + break; + path[k].index->first_block = border; + err = ext4_ext_dirty(inode_ref, path + k); + if (err != EOK) + break; + } + + return err; +} + +static inline bool ext4_ext_can_prepend(struct ext4_extent *ex1, + struct ext4_extent *ex2) +{ + if (ext4_ext_pblock(ex2) + ext4_ext_get_actual_len(ex2) != + ext4_ext_pblock(ex1)) + return 0; + +#ifdef AGGRESSIVE_TEST + if (ext4_ext_get_actual_len(ex1) + ext4_ext_get_actual_len(ex2) > 4) + return 0; +#else + if (ext4_ext_is_unwritten(ex1)) { + if (ext4_ext_get_actual_len(ex1) + + ext4_ext_get_actual_len(ex2) > + EXT_UNWRITTEN_MAX_LEN) + return 0; + } else if (ext4_ext_get_actual_len(ex1) + ext4_ext_get_actual_len(ex2) > + EXT_INIT_MAX_LEN) + return 0; +#endif + + if (to_le32(ex2->first_block) + ext4_ext_get_actual_len(ex2) != + to_le32(ex1->first_block)) + return 0; + + return 1; +} + +static inline bool ext4_ext_can_append(struct ext4_extent *ex1, + struct ext4_extent *ex2) +{ + if (ext4_ext_pblock(ex1) + ext4_ext_get_actual_len(ex1) != + ext4_ext_pblock(ex2)) + return 0; + +#ifdef AGGRESSIVE_TEST + if (ext4_ext_get_actual_len(ex1) + ext4_ext_get_actual_len(ex2) > 4) + return 0; +#else + if (ext4_ext_is_unwritten(ex1)) { + if (ext4_ext_get_actual_len(ex1) + + ext4_ext_get_actual_len(ex2) > + EXT_UNWRITTEN_MAX_LEN) + return 0; + } else if (ext4_ext_get_actual_len(ex1) + ext4_ext_get_actual_len(ex2) > + EXT_INIT_MAX_LEN) + return 0; +#endif + + if (to_le32(ex1->first_block) + ext4_ext_get_actual_len(ex1) != + to_le32(ex2->first_block)) + return 0; + + return 1; +} + +static int ext4_ext_insert_leaf(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path, int at, + struct ext4_extent *newext, int flags, + bool *need_split) +{ + struct ext4_extent_path *curp = path + at; + struct ext4_extent *ex = curp->extent; + int len, err, unwritten; + struct ext4_extent_header *eh; + + *need_split = false; + + if (curp->extent && + to_le32(newext->first_block) == to_le32(curp->extent->first_block)) + return EIO; + + if (!(flags & EXT4_EXT_NO_COMBINE)) { + if (curp->extent && ext4_ext_can_append(curp->extent, newext)) { + unwritten = ext4_ext_is_unwritten(curp->extent); + curp->extent->block_count = + to_le16(ext4_ext_get_actual_len(curp->extent) + + ext4_ext_get_actual_len(newext)); + if (unwritten) + ext4_ext_mark_unwritten(curp->extent); + + err = ext4_ext_dirty(inode_ref, curp); + goto out; + } + + if (curp->extent && + ext4_ext_can_prepend(curp->extent, newext)) { + unwritten = ext4_ext_is_unwritten(curp->extent); + curp->extent->first_block = newext->first_block; + curp->extent->block_count = + to_le16(ext4_ext_get_actual_len(curp->extent) + + ext4_ext_get_actual_len(newext)); + if (unwritten) + ext4_ext_mark_unwritten(curp->extent); + + err = ext4_ext_dirty(inode_ref, curp); + goto out; + } + } + + if (to_le16(curp->header->entries_count) == + to_le16(curp->header->max_entries_count)) { + err = EIO; + *need_split = true; + goto out; + } else { + eh = curp->header; + if (curp->extent == NULL) { + ex = EXT_FIRST_EXTENT(eh); + curp->extent = ex; + } else if (to_le32(newext->first_block) > + to_le32(curp->extent->first_block)) { + /* insert after */ + ex = curp->extent + 1; + } else { + /* insert before */ + ex = curp->extent; + } + } + + len = EXT_LAST_EXTENT(eh) - ex + 1; + ext4_assert(len >= 0); + if (len > 0) + memmove(ex + 1, ex, len * sizeof(struct ext4_extent)); + + if (ex > EXT_MAX_EXTENT(eh)) { + err = EIO; + goto out; + } + + ex->first_block = newext->first_block; + ex->block_count = newext->block_count; + ext4_ext_store_pblock(ex, ext4_ext_pblock(newext)); + eh->entries_count = to_le16(to_le16(eh->entries_count) + 1); + + if (ex > EXT_LAST_EXTENT(eh)) { + err = EIO; + goto out; + } + + err = ext4_ext_correct_indexes(inode_ref, path); + if (err != EOK) + goto out; + err = ext4_ext_dirty(inode_ref, curp); + +out: + if (err == EOK) { + curp->extent = ex; + curp->p_block = ext4_ext_pblock(ex); + } + + return err; +} + +/* + * ext4_ext_grow_indepth: + * implements tree growing procedure: + * - allocates new block + * - moves top-level data (index block or leaf) into the new block + * - initializes new top-level, creating index that points to the + * just created block + */ +static int ext4_ext_grow_indepth(struct ext4_inode_ref *inode_ref, + uint32_t flags) +{ + struct ext4_extent_header *neh; + struct ext4_block bh = EXT4_BLOCK_ZERO(); + ext4_fsblk_t newblock, goal = 0; + int err = EOK; + + /* Try to prepend new index to old one */ + if (ext_depth(inode_ref->inode)) + goal = ext4_idx_pblock( + EXT_FIRST_INDEX(ext_inode_hdr(inode_ref->inode))); + else + goal = ext4_fs_inode_to_goal_block(inode_ref); + + newblock = ext4_new_meta_blocks(inode_ref, goal, flags, NULL, &err); + if (newblock == 0) + return err; + + /* # */ + err = ext4_trans_block_get_noread(inode_ref->fs->bdev, &bh, newblock); + if (err != EOK) { + ext4_ext_free_blocks(inode_ref, newblock, 1, 0); + return err; + } + + /* move top-level index/leaf into new block */ + memmove(bh.data, inode_ref->inode->blocks, + sizeof(inode_ref->inode->blocks)); + + /* set size of new block */ + neh = ext_block_hdr(&bh); + /* old root could have indexes or leaves + * so calculate e_max right way */ + if (ext_depth(inode_ref->inode)) + neh->max_entries_count = + to_le16(ext4_ext_space_block_idx(inode_ref)); + else + neh->max_entries_count = + to_le16(ext4_ext_space_block(inode_ref)); + + neh->magic = to_le16(EXT4_EXTENT_MAGIC); + ext4_extent_block_csum_set(inode_ref, neh); + + /* Update top-level index: num,max,pointer */ + neh = ext_inode_hdr(inode_ref->inode); + neh->entries_count = to_le16(1); + ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); + if (neh->depth == 0) { + /* Root extent block becomes index block */ + neh->max_entries_count = + to_le16(ext4_ext_space_root_idx(inode_ref)); + EXT_FIRST_INDEX(neh) + ->first_block = EXT_FIRST_EXTENT(neh)->first_block; + } + neh->depth = to_le16(to_le16(neh->depth) + 1); + + ext4_trans_set_block_dirty(bh.buf); + inode_ref->dirty = true; + ext4_block_set(inode_ref->fs->bdev, &bh); + + return err; +} + +static inline void ext4_ext_replace_path(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path, + struct ext4_extent_path *newpath, + int at) +{ + ext4_ext_drop_refs(inode_ref, path + at, 1); + path[at] = *newpath; + memset(newpath, 0, sizeof(struct ext4_extent_path)); +} + +int ext4_ext_insert_extent(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path **ppath, + struct ext4_extent *newext, int flags) +{ + int depth, level = 0, ret = 0; + struct ext4_extent_path *path = *ppath; + struct ext4_extent_path *npath = NULL; + bool ins_right_leaf = false; + bool need_split; + +again: + depth = ext_depth(inode_ref->inode); + ret = ext4_ext_insert_leaf(inode_ref, path, depth, newext, flags, + &need_split); + if (ret == EIO && need_split == true) { + int i; + for (i = depth, level = 0; i >= 0; i--, level++) + if (EXT_HAS_FREE_INDEX(path + i)) + break; + + /* Do we need to grow the tree? */ + if (i < 0) { + ret = ext4_ext_grow_indepth(inode_ref, 0); + if (ret != EOK) + goto out; + + ret = ext4_find_extent( + inode_ref, to_le32(newext->first_block), ppath, 0); + if (ret != EOK) + goto out; + + path = *ppath; + /* + * After growing the tree, there should be free space in + * the only child node of the root. + */ + level--; + depth++; + } + + i = depth - (level - 1); + /* We split from leaf to the i-th node */ + if (level > 0) { + npath = ext4_calloc(1, sizeof(struct ext4_extent_path) * + (level)); + if (!npath) { + ret = ENOMEM; + goto out; + } + ret = ext4_ext_split_node(inode_ref, path, i, newext, + npath, &ins_right_leaf); + if (ret != EOK) + goto out; + + while (--level >= 0) { + if (ins_right_leaf) + ext4_ext_replace_path(inode_ref, path, + &npath[level], + i + level); + else if (npath[level].block.lb_id) + ext4_ext_drop_refs(inode_ref, + npath + level, 1); + } + } + goto again; + } + +out: + if (ret != EOK) { + if (path) + ext4_ext_drop_refs(inode_ref, path, 0); + + while (--level >= 0 && npath) { + if (npath[level].block.lb_id) { + ext4_fsblk_t block = npath[level].block.lb_id; + ext4_ext_free_blocks(inode_ref, block, 1, 0); + ext4_ext_drop_refs(inode_ref, npath + level, 1); + } + } + } + if (npath) + ext4_free(npath); + + return ret; +} + +static void ext4_ext_remove_blocks(struct ext4_inode_ref *inode_ref, + struct ext4_extent *ex, ext4_lblk_t from, + ext4_lblk_t to) +{ + ext4_lblk_t len = to - from + 1; + ext4_lblk_t num; + ext4_fsblk_t start; + num = from - to_le32(ex->first_block); + start = ext4_ext_pblock(ex) + num; + ext4_dbg(DEBUG_EXTENT, + "Freeing %" PRIu32 " at %" PRIu64 ", %" PRIu32 "\n", from, + start, len); + + ext4_ext_free_blocks(inode_ref, start, len, 0); +} + +static int ext4_ext_remove_idx(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path, int32_t depth) +{ + int err = EOK; + int32_t i = depth; + ext4_fsblk_t leaf; + + /* free index block */ + leaf = ext4_idx_pblock(path[i].index); + + if (path[i].index != EXT_LAST_INDEX(path[i].header)) { + ptrdiff_t len = EXT_LAST_INDEX(path[i].header) - path[i].index; + memmove(path[i].index, path[i].index + 1, + len * sizeof(struct ext4_extent_index)); + } + + path[i].header->entries_count = + to_le16(to_le16(path[i].header->entries_count) - 1); + err = ext4_ext_dirty(inode_ref, path + i); + if (err != EOK) + return err; + + ext4_dbg(DEBUG_EXTENT, "IDX: Freeing %" PRIu32 " at %" PRIu64 ", %d\n", + to_le32(path[i].index->first_block), leaf, 1); + ext4_ext_free_blocks(inode_ref, leaf, 1, 0); + + /* + * We may need to correct the paths after the first extents/indexes in + * a node being modified. + * + * We do not need to consider whether there's any extents presenting or + * not, as garbage will be cleared soon. + */ + while (i > 0) { + if (path[i].index != EXT_FIRST_INDEX(path[i].header)) + break; + + path[i - 1].index->first_block = path[i].index->first_block; + err = ext4_ext_dirty(inode_ref, path + i - 1); + if (err != EOK) + break; + + i--; + } + return err; +} + +static int ext4_ext_remove_leaf(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path *path, ext4_lblk_t from, + ext4_lblk_t to) +{ + + int32_t depth = ext_depth(inode_ref->inode); + struct ext4_extent *ex = path[depth].extent; + struct ext4_extent *start_ex, *ex2 = NULL; + struct ext4_extent_header *eh = path[depth].header; + int32_t len; + int err = EOK; + uint16_t new_entries; + + start_ex = ex; + new_entries = to_le16(eh->entries_count); + while (ex <= EXT_LAST_EXTENT(path[depth].header) && + to_le32(ex->first_block) <= to) { + int32_t new_len = 0; + int unwritten; + ext4_lblk_t start, new_start; + ext4_fsblk_t newblock; + new_start = start = to_le32(ex->first_block); + len = ext4_ext_get_actual_len(ex); + newblock = ext4_ext_pblock(ex); + /* + * The 1st case: + * The position that we start truncation is inside the range of an + * extent. Here we should calculate the new length of that extent and + * may start the removal from the next extent. + */ + if (start < from) { + len -= from - start; + new_len = from - start; + start = from; + start_ex++; + } else { + /* + * The second case: + * The last block to be truncated is inside the range of an + * extent. We need to calculate the new length and the new + * start of the extent. + */ + if (start + len - 1 > to) { + new_len = start + len - 1 - to; + len -= new_len; + new_start = to + 1; + newblock += to + 1 - start; + ex2 = ex; + } + } + + ext4_ext_remove_blocks(inode_ref, ex, start, start + len - 1); + /* + * Set the first block of the extent if it is presented. + */ + ex->first_block = to_le32(new_start); + + /* + * If the new length of the current extent we are working on is + * zero, remove it. + */ + if (!new_len) + new_entries--; + else { + unwritten = ext4_ext_is_unwritten(ex); + ex->block_count = to_le16(new_len); + ext4_ext_store_pblock(ex, newblock); + if (unwritten) + ext4_ext_mark_unwritten(ex); + } + + ex += 1; + } + + if (ex2 == NULL) + ex2 = ex; + + /* + * Move any remaining extents to the starting position of the node. + */ + if (ex2 <= EXT_LAST_EXTENT(eh)) + memmove(start_ex, ex2, (EXT_LAST_EXTENT(eh) - ex2 + 1) * + sizeof(struct ext4_extent)); + + eh->entries_count = to_le16(new_entries); + ext4_ext_dirty(inode_ref, path + depth); + + /* + * If the extent pointer is pointed to the first extent of the node, and + * there's still extents presenting, we may need to correct the indexes + * of the paths. + */ + if (path[depth].extent == EXT_FIRST_EXTENT(eh) && eh->entries_count) { + err = ext4_ext_correct_indexes(inode_ref, path); + if (err != EOK) + return err; + } + + /* if this leaf is free, then we should + * remove it from index block above */ + if (eh->entries_count == 0 && path[depth].block.lb_id) + err = ext4_ext_remove_idx(inode_ref, path, depth - 1); + else if (depth > 0) + path[depth - 1].index++; + + return err; +} + +/* + * Check if there's more to remove at a specific level. + */ +static bool ext4_ext_more_to_rm(struct ext4_extent_path *path, ext4_lblk_t to) +{ + if (!to_le16(path->header->entries_count)) + return false; + + if (path->index > EXT_LAST_INDEX(path->header)) + return false; + + if (to_le32(path->index->first_block) > to) + return false; + + return true; +} + +int ext4_extent_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from, + ext4_lblk_t to) +{ + struct ext4_extent_path *path = NULL; + int ret = EOK; + int32_t depth = ext_depth(inode_ref->inode); + int32_t i; + + ret = ext4_find_extent(inode_ref, from, &path, 0); + if (ret != EOK) + goto out; + + if (!path[depth].extent) { + ret = EOK; + goto out; + } + + bool in_range = IN_RANGE(from, to_le32(path[depth].extent->first_block), + ext4_ext_get_actual_len(path[depth].extent)); + + if (!in_range) { + ret = EOK; + goto out; + } + + /* If we do remove_space inside the range of an extent */ + if ((to_le32(path[depth].extent->first_block) < from) && + (to < to_le32(path[depth].extent->first_block) + + ext4_ext_get_actual_len(path[depth].extent) - 1)) { + + struct ext4_extent *ex = path[depth].extent, newex; + int unwritten = ext4_ext_is_unwritten(ex); + ext4_lblk_t ee_block = to_le32(ex->first_block); + int32_t len = ext4_ext_get_actual_len(ex); + ext4_fsblk_t newblock = to + 1 - ee_block + ext4_ext_pblock(ex); + + ex->block_count = to_le16(from - ee_block); + if (unwritten) + ext4_ext_mark_unwritten(ex); + + ext4_ext_dirty(inode_ref, path + depth); + + newex.first_block = to_le32(to + 1); + newex.block_count = to_le16(ee_block + len - 1 - to); + ext4_ext_store_pblock(&newex, newblock); + if (unwritten) + ext4_ext_mark_unwritten(&newex); + + ret = ext4_ext_insert_extent(inode_ref, &path, &newex, 0); + goto out; + } + + i = depth; + while (i >= 0) { + if (i == depth) { + struct ext4_extent_header *eh; + struct ext4_extent *first_ex, *last_ex; + ext4_lblk_t leaf_from, leaf_to; + eh = path[i].header; + ext4_assert(to_le16(eh->entries_count) > 0); + first_ex = EXT_FIRST_EXTENT(eh); + last_ex = EXT_LAST_EXTENT(eh); + leaf_from = to_le32(first_ex->first_block); + leaf_to = to_le32(last_ex->first_block) + + ext4_ext_get_actual_len(last_ex) - 1; + if (leaf_from < from) + leaf_from = from; + + if (leaf_to > to) + leaf_to = to; + + ext4_ext_remove_leaf(inode_ref, path, leaf_from, + leaf_to); + ext4_ext_drop_refs(inode_ref, path + i, 0); + i--; + continue; + } + + struct ext4_extent_header *eh; + eh = path[i].header; + if (ext4_ext_more_to_rm(path + i, to)) { + struct ext4_block bh = EXT4_BLOCK_ZERO(); + if (path[i + 1].block.lb_id) + ext4_ext_drop_refs(inode_ref, path + i + 1, 0); + + ret = read_extent_tree_block( + inode_ref, ext4_idx_pblock(path[i].index), + depth - i - 1, &bh, 0); + if (ret != EOK) + goto out; + + path[i].p_block = ext4_idx_pblock(path[i].index); + path[i + 1].block = bh; + path[i + 1].header = ext_block_hdr(&bh); + path[i + 1].depth = depth - i - 1; + if (i + 1 == depth) + path[i + 1].extent = + EXT_FIRST_EXTENT(path[i + 1].header); + else + path[i + 1].index = + EXT_FIRST_INDEX(path[i + 1].header); + + i++; + } else { + if (i > 0) { + /* + * Garbage entries will finally be cleared here. + */ + if (!eh->entries_count) + ret = ext4_ext_remove_idx(inode_ref, + path, i - 1); + else + path[i - 1].index++; + } + + if (i) + ext4_block_set(inode_ref->fs->bdev, + &path[i].block); + + i--; + } + } + + /* TODO: flexible tree reduction should be here */ + if (path->header->entries_count == 0) { + /* + * truncate to zero freed all the tree, + * so we need to correct eh_depth + */ + ext_inode_hdr(inode_ref->inode)->depth = 0; + ext_inode_hdr(inode_ref->inode)->max_entries_count = + to_le16(ext4_ext_space_root(inode_ref)); + ret = ext4_ext_dirty(inode_ref, path); + } + +out: + ext4_ext_drop_refs(inode_ref, path, 0); + ext4_free(path); + path = NULL; + return ret; +} + +static int ext4_ext_split_extent_at(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path **ppath, + ext4_lblk_t split, uint32_t split_flag) +{ + struct ext4_extent *ex, newex; + ext4_fsblk_t newblock; + ext4_lblk_t ee_block; + int32_t ee_len; + int32_t depth = ext_depth(inode_ref->inode); + int err = EOK; + + ex = (*ppath)[depth].extent; + ee_block = to_le32(ex->first_block); + ee_len = ext4_ext_get_actual_len(ex); + newblock = split - ee_block + ext4_ext_pblock(ex); + + if (split == ee_block) { + /* + * case b: block @split is the block that the extent begins with + * then we just change the state of the extent, and splitting + * is not needed. + */ + if (split_flag & EXT4_EXT_MARK_UNWRIT2) + ext4_ext_mark_unwritten(ex); + else + ext4_ext_mark_initialized(ex); + + err = ext4_ext_dirty(inode_ref, *ppath + depth); + goto out; + } + + ex->block_count = to_le16(split - ee_block); + if (split_flag & EXT4_EXT_MARK_UNWRIT1) + ext4_ext_mark_unwritten(ex); + + err = ext4_ext_dirty(inode_ref, *ppath + depth); + if (err != EOK) + goto out; + + newex.first_block = to_le32(split); + newex.block_count = to_le16(ee_len - (split - ee_block)); + ext4_ext_store_pblock(&newex, newblock); + if (split_flag & EXT4_EXT_MARK_UNWRIT2) + ext4_ext_mark_unwritten(&newex); + err = ext4_ext_insert_extent(inode_ref, ppath, &newex, + EXT4_EXT_NO_COMBINE); + if (err != EOK) + goto restore_extent_len; + +out: + return err; +restore_extent_len: + ex->block_count = to_le16(ee_len); + err = ext4_ext_dirty(inode_ref, *ppath + depth); + return err; +} + +static int ext4_ext_convert_to_initialized(struct ext4_inode_ref *inode_ref, + struct ext4_extent_path **ppath, + ext4_lblk_t split, uint32_t blocks) +{ + int32_t depth = ext_depth(inode_ref->inode), err = EOK; + struct ext4_extent *ex = (*ppath)[depth].extent; + + ext4_assert(to_le32(ex->first_block) <= split); + + if (split + blocks == + to_le32(ex->first_block) + ext4_ext_get_actual_len(ex)) { + /* split and initialize right part */ + err = ext4_ext_split_extent_at(inode_ref, ppath, split, + EXT4_EXT_MARK_UNWRIT1); + } else if (to_le32(ex->first_block) == split) { + /* split and initialize left part */ + err = ext4_ext_split_extent_at(inode_ref, ppath, split + blocks, + EXT4_EXT_MARK_UNWRIT2); + } else { + /* split 1 extent to 3 and initialize the 2nd */ + err = ext4_ext_split_extent_at(inode_ref, ppath, split + blocks, + EXT4_EXT_MARK_UNWRIT1 | + EXT4_EXT_MARK_UNWRIT2); + if (err == EOK) { + err = ext4_ext_split_extent_at(inode_ref, ppath, split, + EXT4_EXT_MARK_UNWRIT1); + } + } + + return err; +} + +static ext4_lblk_t ext4_ext_next_allocated_block(struct ext4_extent_path *path) +{ + int32_t depth; + + depth = path->depth; + + if (depth == 0 && path->extent == NULL) + return EXT_MAX_BLOCKS; + + while (depth >= 0) { + if (depth == path->depth) { + /* leaf */ + if (path[depth].extent && + path[depth].extent != + EXT_LAST_EXTENT(path[depth].header)) + return to_le32( + path[depth].extent[1].first_block); + } else { + /* index */ + if (path[depth].index != + EXT_LAST_INDEX(path[depth].header)) + return to_le32( + path[depth].index[1].first_block); + } + depth--; + } + + return EXT_MAX_BLOCKS; +} + +static int ext4_ext_zero_unwritten_range(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t block, + uint32_t blocks_count) +{ + int err = EOK; + uint32_t i; + uint32_t block_size = ext4_sb_get_block_size(&inode_ref->fs->sb); + for (i = 0; i < blocks_count; i++) { + struct ext4_block bh = EXT4_BLOCK_ZERO(); + err = ext4_trans_block_get_noread(inode_ref->fs->bdev, &bh, + block + i); + if (err != EOK) + break; + + memset(bh.data, 0, block_size); + ext4_trans_set_block_dirty(bh.buf); + err = ext4_block_set(inode_ref->fs->bdev, &bh); + if (err != EOK) + break; + } + return err; +} + +__unused static void print_path(struct ext4_extent_path *path) +{ + int32_t i = path->depth; + while (i >= 0) { + + ptrdiff_t a = + (path->extent) + ? (path->extent - EXT_FIRST_EXTENT(path->header)) + : 0; + ptrdiff_t b = + (path->index) + ? (path->index - EXT_FIRST_INDEX(path->header)) + : 0; + + (void)a; + (void)b; + ext4_dbg(DEBUG_EXTENT, + "depth %" PRId32 ", p_block: %" PRIu64 "," + "p_ext offset: %td, p_idx offset: %td\n", + i, path->p_block, a, b); + i--; + path++; + } +} + +int ext4_extent_get_blocks(struct ext4_inode_ref *inode_ref, ext4_lblk_t iblock, + uint32_t max_blocks, ext4_fsblk_t *result, + bool create, uint32_t *blocks_count) +{ + struct ext4_extent_path *path = NULL; + struct ext4_extent newex, *ex; + ext4_fsblk_t goal; + int err = EOK; + int32_t depth; + uint32_t allocated = 0; + ext4_lblk_t next; + ext4_fsblk_t newblock; + + if (result) + *result = 0; + + if (blocks_count) + *blocks_count = 0; + + /* find extent for this block */ + err = ext4_find_extent(inode_ref, iblock, &path, 0); + if (err != EOK) { + path = NULL; + goto out2; + } + + depth = ext_depth(inode_ref->inode); + + /* + * consistent leaf must not be empty + * this situations is possible, though, _during_ tree modification + * this is why assert can't be put in ext4_ext_find_extent() + */ + ex = path[depth].extent; + if (ex) { + ext4_lblk_t ee_block = to_le32(ex->first_block); + ext4_fsblk_t ee_start = ext4_ext_pblock(ex); + uint16_t ee_len = ext4_ext_get_actual_len(ex); + /* if found exent covers block, simple return it */ + if (IN_RANGE(iblock, ee_block, ee_len)) { + /* number of remain blocks in the extent */ + allocated = ee_len - (iblock - ee_block); + + if (!ext4_ext_is_unwritten(ex)) { + newblock = iblock - ee_block + ee_start; + goto out; + } + + if (!create) { + newblock = 0; + goto out; + } + + uint32_t zero_range; + zero_range = allocated; + if (zero_range > max_blocks) + zero_range = max_blocks; + + newblock = iblock - ee_block + ee_start; + err = ext4_ext_zero_unwritten_range(inode_ref, newblock, + zero_range); + if (err != EOK) + goto out2; + + err = ext4_ext_convert_to_initialized( + inode_ref, &path, iblock, zero_range); + if (err != EOK) + goto out2; + + goto out; + } + } + + /* + * requested block isn't allocated yet + * we couldn't try to create block if create flag is zero + */ + if (!create) { + goto out2; + } + + /* find next allocated block so that we know how many + * blocks we can allocate without ovelapping next extent */ + next = ext4_ext_next_allocated_block(path); + allocated = next - iblock; + if (allocated > max_blocks) + allocated = max_blocks; + + /* allocate new block */ + goal = ext4_ext_find_goal(inode_ref, path, iblock); + newblock = ext4_new_meta_blocks(inode_ref, goal, 0, &allocated, &err); + if (!newblock) + goto out2; + + /* try to insert new extent into found leaf and return */ + newex.first_block = to_le32(iblock); + ext4_ext_store_pblock(&newex, newblock); + newex.block_count = to_le16(allocated); + err = ext4_ext_insert_extent(inode_ref, &path, &newex, 0); + if (err != EOK) { + /* free data blocks we just allocated */ + ext4_ext_free_blocks(inode_ref, ext4_ext_pblock(&newex), + to_le16(newex.block_count), 0); + goto out2; + } + + /* previous routine could use block we allocated */ + newblock = ext4_ext_pblock(&newex); + +out: + if (allocated > max_blocks) + allocated = max_blocks; + + if (result) + *result = newblock; + + if (blocks_count) + *blocks_count = allocated; + +out2: + if (path) { + ext4_ext_drop_refs(inode_ref, path, 0); + ext4_free(path); + } + + return err; +} +#endif diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_fs.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_fs.c new file mode 100644 index 0000000..873462c --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_fs.c @@ -0,0 +1,1751 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_fs.c + * @brief More complex filesystem functions. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_trans.h" +#include "ext4_fs.h" +#include "ext4_blockdev.h" +#include "ext4_super.h" +#include "ext4_crc32.h" +#include "ext4_block_group.h" +#include "ext4_balloc.h" +#include "ext4_bitmap.h" +#include "ext4_inode.h" +#include "ext4_ialloc.h" +#include "ext4_extent.h" + +#include + +int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev, + bool read_only) +{ + int r, i; + uint16_t tmp; + uint32_t bsize; + + ext4_assert(fs && bdev); + + fs->bdev = bdev; + + fs->read_only = read_only; + + r = ext4_sb_read(fs->bdev, &fs->sb); + if (r != EOK) + return r; + + if (!ext4_sb_check(&fs->sb)) + return ENOTSUP; + + bsize = ext4_sb_get_block_size(&fs->sb); + if (bsize > EXT4_MAX_BLOCK_SIZE) + return ENXIO; + + r = ext4_fs_check_features(fs, &read_only); + if (r != EOK) + return r; + + if (read_only) + fs->read_only = read_only; + + /* Compute limits for indirect block levels */ + uint32_t blocks_id = bsize / sizeof(uint32_t); + + fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT; + fs->inode_blocks_per_level[0] = 1; + + for (i = 1; i < 4; i++) { + fs->inode_blocks_per_level[i] = + fs->inode_blocks_per_level[i - 1] * blocks_id; + fs->inode_block_limits[i] = fs->inode_block_limits[i - 1] + + fs->inode_blocks_per_level[i]; + } + + /*Validate FS*/ + tmp = ext4_get16(&fs->sb, state); + if (tmp & EXT4_SUPERBLOCK_STATE_ERROR_FS) + ext4_dbg(DEBUG_FS, DBG_WARN + "last umount error: superblock fs_error flag\n"); + + + if (!fs->read_only) { + /* Mark system as mounted */ + ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_ERROR_FS); + r = ext4_sb_write(fs->bdev, &fs->sb); + if (r != EOK) + return r; + + /*Update mount count*/ + ext4_set16(&fs->sb, mount_count, ext4_get16(&fs->sb, mount_count) + 1); + } + + return r; +} + +int ext4_fs_fini(struct ext4_fs *fs) +{ + ext4_assert(fs); + + /*Set superblock state*/ + ext4_set16(&fs->sb, state, EXT4_SUPERBLOCK_STATE_VALID_FS); + + if (!fs->read_only) + return ext4_sb_write(fs->bdev, &fs->sb); + + return EOK; +} + +static void ext4_fs_debug_features_inc(uint32_t features_incompatible) +{ + if (features_incompatible & EXT4_FINCOM_COMPRESSION) + ext4_dbg(DEBUG_FS, DBG_NONE "compression\n"); + if (features_incompatible & EXT4_FINCOM_FILETYPE) + ext4_dbg(DEBUG_FS, DBG_NONE "filetype\n"); + if (features_incompatible & EXT4_FINCOM_RECOVER) + ext4_dbg(DEBUG_FS, DBG_NONE "recover\n"); + if (features_incompatible & EXT4_FINCOM_JOURNAL_DEV) + ext4_dbg(DEBUG_FS, DBG_NONE "journal_dev\n"); + if (features_incompatible & EXT4_FINCOM_META_BG) + ext4_dbg(DEBUG_FS, DBG_NONE "meta_bg\n"); + if (features_incompatible & EXT4_FINCOM_EXTENTS) + ext4_dbg(DEBUG_FS, DBG_NONE "extents\n"); + if (features_incompatible & EXT4_FINCOM_64BIT) + ext4_dbg(DEBUG_FS, DBG_NONE "64bit\n"); + if (features_incompatible & EXT4_FINCOM_MMP) + ext4_dbg(DEBUG_FS, DBG_NONE "mnp\n"); + if (features_incompatible & EXT4_FINCOM_FLEX_BG) + ext4_dbg(DEBUG_FS, DBG_NONE "flex_bg\n"); + if (features_incompatible & EXT4_FINCOM_EA_INODE) + ext4_dbg(DEBUG_FS, DBG_NONE "ea_inode\n"); + if (features_incompatible & EXT4_FINCOM_DIRDATA) + ext4_dbg(DEBUG_FS, DBG_NONE "dirdata\n"); + if (features_incompatible & EXT4_FINCOM_BG_USE_META_CSUM) + ext4_dbg(DEBUG_FS, DBG_NONE "meta_csum\n"); + if (features_incompatible & EXT4_FINCOM_LARGEDIR) + ext4_dbg(DEBUG_FS, DBG_NONE "largedir\n"); + if (features_incompatible & EXT4_FINCOM_INLINE_DATA) + ext4_dbg(DEBUG_FS, DBG_NONE "inline_data\n"); +} +static void ext4_fs_debug_features_comp(uint32_t features_compatible) +{ + if (features_compatible & EXT4_FCOM_DIR_PREALLOC) + ext4_dbg(DEBUG_FS, DBG_NONE "dir_prealloc\n"); + if (features_compatible & EXT4_FCOM_IMAGIC_INODES) + ext4_dbg(DEBUG_FS, DBG_NONE "imagic_inodes\n"); + if (features_compatible & EXT4_FCOM_HAS_JOURNAL) + ext4_dbg(DEBUG_FS, DBG_NONE "has_journal\n"); + if (features_compatible & EXT4_FCOM_EXT_ATTR) + ext4_dbg(DEBUG_FS, DBG_NONE "ext_attr\n"); + if (features_compatible & EXT4_FCOM_RESIZE_INODE) + ext4_dbg(DEBUG_FS, DBG_NONE "resize_inode\n"); + if (features_compatible & EXT4_FCOM_DIR_INDEX) + ext4_dbg(DEBUG_FS, DBG_NONE "dir_index\n"); +} + +static void ext4_fs_debug_features_ro(uint32_t features_ro) +{ + if (features_ro & EXT4_FRO_COM_SPARSE_SUPER) + ext4_dbg(DEBUG_FS, DBG_NONE "sparse_super\n"); + if (features_ro & EXT4_FRO_COM_LARGE_FILE) + ext4_dbg(DEBUG_FS, DBG_NONE "large_file\n"); + if (features_ro & EXT4_FRO_COM_BTREE_DIR) + ext4_dbg(DEBUG_FS, DBG_NONE "btree_dir\n"); + if (features_ro & EXT4_FRO_COM_HUGE_FILE) + ext4_dbg(DEBUG_FS, DBG_NONE "huge_file\n"); + if (features_ro & EXT4_FRO_COM_GDT_CSUM) + ext4_dbg(DEBUG_FS, DBG_NONE "gtd_csum\n"); + if (features_ro & EXT4_FRO_COM_DIR_NLINK) + ext4_dbg(DEBUG_FS, DBG_NONE "dir_nlink\n"); + if (features_ro & EXT4_FRO_COM_EXTRA_ISIZE) + ext4_dbg(DEBUG_FS, DBG_NONE "extra_isize\n"); + if (features_ro & EXT4_FRO_COM_QUOTA) + ext4_dbg(DEBUG_FS, DBG_NONE "quota\n"); + if (features_ro & EXT4_FRO_COM_BIGALLOC) + ext4_dbg(DEBUG_FS, DBG_NONE "bigalloc\n"); + if (features_ro & EXT4_FRO_COM_METADATA_CSUM) + ext4_dbg(DEBUG_FS, DBG_NONE "metadata_csum\n"); +} + +int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only) +{ + ext4_assert(fs && read_only); + uint32_t v; + if (ext4_get32(&fs->sb, rev_level) == 0) { + *read_only = false; + return EOK; + } + + ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_incompatible:\n"); + ext4_fs_debug_features_inc(ext4_get32(&fs->sb, features_incompatible)); + + ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_compatible:\n"); + ext4_fs_debug_features_comp(ext4_get32(&fs->sb, features_compatible)); + + ext4_dbg(DEBUG_FS, DBG_INFO "sblock features_read_only:\n"); + ext4_fs_debug_features_ro(ext4_get32(&fs->sb, features_read_only)); + + /*Check features_incompatible*/ + v = (ext4_get32(&fs->sb, features_incompatible) & + (~CONFIG_SUPPORTED_FINCOM)); + if (v) { + ext4_dbg(DEBUG_FS, DBG_ERROR + "sblock has unsupported features incompatible:\n"); + ext4_fs_debug_features_inc(v); + return ENOTSUP; + } + + /*Check features_read_only*/ + v = ext4_get32(&fs->sb, features_read_only); + v &= ~CONFIG_SUPPORTED_FRO_COM; + if (v) { + ext4_dbg(DEBUG_FS, DBG_WARN + "sblock has unsupported features read only:\n"); + ext4_fs_debug_features_ro(v); + *read_only = true; + return EOK; + } + *read_only = false; + + return EOK; +} + +/**@brief Determine whether the block is inside the group. + * @param baddr block address + * @param bgid block group id + * @return Error code + */ +static bool ext4_block_in_group(struct ext4_sblock *s, ext4_fsblk_t baddr, + uint32_t bgid) +{ + uint32_t actual_bgid; + actual_bgid = ext4_balloc_get_bgid_of_block(s, baddr); + if (actual_bgid == bgid) + return true; + return false; +} + +/**@brief To avoid calling the atomic setbit hundreds or thousands of times, we only + * need to use it within a single byte (to ensure we get endianness right). + * We can use memset for the rest of the bitmap as there are no other users. + */ +static void ext4_fs_mark_bitmap_end(int start_bit, int end_bit, void *bitmap) +{ + int i; + + if (start_bit >= end_bit) + return; + + for (i = start_bit; (unsigned)i < ((start_bit + 7) & ~7UL); i++) + ext4_bmap_bit_set(bitmap, i); + + if (i < end_bit) + memset((char *)bitmap + (i >> 3), 0xff, (end_bit - i) >> 3); +} + +/**@brief Initialize block bitmap in block group. + * @param bg_ref Reference to block group + * @return Error code + */ +static int ext4_fs_init_block_bitmap(struct ext4_block_group_ref *bg_ref) +{ + struct ext4_sblock *sb = &bg_ref->fs->sb; + struct ext4_bgroup *bg = bg_ref->block_group; + int rc; + + uint32_t bit, bit_max; + uint32_t group_blocks; + uint16_t inode_size = ext4_get16(sb, inode_size); + uint32_t block_size = ext4_sb_get_block_size(sb); + uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group); + + ext4_fsblk_t i; + ext4_fsblk_t bmp_blk = ext4_bg_get_block_bitmap(bg, sb); + ext4_fsblk_t bmp_inode = ext4_bg_get_inode_bitmap(bg, sb); + ext4_fsblk_t inode_table = ext4_bg_get_inode_table_first_block(bg, sb); + ext4_fsblk_t first_bg = ext4_balloc_get_block_of_bgid(sb, bg_ref->index); + + uint32_t dsc_per_block = block_size / ext4_sb_get_desc_size(sb); + + bool flex_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_FLEX_BG); + bool meta_bg = ext4_sb_feature_incom(sb, EXT4_FINCOM_META_BG); + + uint32_t inode_table_bcnt = inodes_per_group * inode_size / block_size; + + struct ext4_block block_bitmap; + rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &block_bitmap, bmp_blk); + if (rc != EOK) + return rc; + + memset(block_bitmap.data, 0, block_size); + bit_max = ext4_sb_is_super_in_bg(sb, bg_ref->index); + + uint32_t count = ext4_sb_first_meta_bg(sb) * dsc_per_block; + if (!meta_bg || bg_ref->index < count) { + if (bit_max) { + bit_max += ext4_bg_num_gdb(sb, bg_ref->index); + bit_max += ext4_get16(sb, s_reserved_gdt_blocks); + } + } else { /* For META_BG_BLOCK_GROUPS */ + bit_max += ext4_bg_num_gdb(sb, bg_ref->index); + } + for (bit = 0; bit < bit_max; bit++) + ext4_bmap_bit_set(block_bitmap.data, bit); + + if (bg_ref->index == ext4_block_group_cnt(sb) - 1) { + /* + * Even though mke2fs always initialize first and last group + * if some other tool enabled the EXT4_BG_BLOCK_UNINIT we need + * to make sure we calculate the right free blocks + */ + + group_blocks = (uint32_t)(ext4_sb_get_blocks_cnt(sb) - + ext4_get32(sb, first_data_block) - + ext4_get32(sb, blocks_per_group) * + (ext4_block_group_cnt(sb) - 1)); + } else { + group_blocks = ext4_get32(sb, blocks_per_group); + } + + bool in_bg; + in_bg = ext4_block_in_group(sb, bmp_blk, bg_ref->index); + if (!flex_bg || in_bg) + ext4_bmap_bit_set(block_bitmap.data, + (uint32_t)(bmp_blk - first_bg)); + + in_bg = ext4_block_in_group(sb, bmp_inode, bg_ref->index); + if (!flex_bg || in_bg) + ext4_bmap_bit_set(block_bitmap.data, + (uint32_t)(bmp_inode - first_bg)); + + for (i = inode_table; i < inode_table + inode_table_bcnt; i++) { + in_bg = ext4_block_in_group(sb, i, bg_ref->index); + if (!flex_bg || in_bg) + ext4_bmap_bit_set(block_bitmap.data, + (uint32_t)(i - first_bg)); + } + /* + * Also if the number of blocks within the group is + * less than the blocksize * 8 ( which is the size + * of bitmap ), set rest of the block bitmap to 1 + */ + ext4_fs_mark_bitmap_end(group_blocks, block_size * 8, block_bitmap.data); + ext4_trans_set_block_dirty(block_bitmap.buf); + + ext4_balloc_set_bitmap_csum(sb, bg_ref->block_group, block_bitmap.data); + bg_ref->dirty = true; + + /* Save bitmap */ + return ext4_block_set(bg_ref->fs->bdev, &block_bitmap); +} + +/**@brief Initialize i-node bitmap in block group. + * @param bg_ref Reference to block group + * @return Error code + */ +static int ext4_fs_init_inode_bitmap(struct ext4_block_group_ref *bg_ref) +{ + int rc; + struct ext4_sblock *sb = &bg_ref->fs->sb; + struct ext4_bgroup *bg = bg_ref->block_group; + + /* Load bitmap */ + ext4_fsblk_t bitmap_block_addr = ext4_bg_get_inode_bitmap(bg, sb); + + struct ext4_block b; + rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, bitmap_block_addr); + if (rc != EOK) + return rc; + + /* Initialize all bitmap bits to zero */ + uint32_t block_size = ext4_sb_get_block_size(sb); + uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group); + + memset(b.data, 0, (inodes_per_group + 7) / 8); + + uint32_t start_bit = inodes_per_group; + uint32_t end_bit = block_size * 8; + + uint32_t i; + for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++) + ext4_bmap_bit_set(b.data, i); + + if (i < end_bit) + memset(b.data + (i >> 3), 0xff, (end_bit - i) >> 3); + + ext4_trans_set_block_dirty(b.buf); + + ext4_ialloc_set_bitmap_csum(sb, bg, b.data); + bg_ref->dirty = true; + + /* Save bitmap */ + return ext4_block_set(bg_ref->fs->bdev, &b); +} + +/**@brief Initialize i-node table in block group. + * @param bg_ref Reference to block group + * @return Error code + */ +static int ext4_fs_init_inode_table(struct ext4_block_group_ref *bg_ref) +{ + struct ext4_sblock *sb = &bg_ref->fs->sb; + struct ext4_bgroup *bg = bg_ref->block_group; + + uint32_t inode_size = ext4_get16(sb, inode_size); + uint32_t block_size = ext4_sb_get_block_size(sb); + uint32_t inodes_per_block = block_size / inode_size; + uint32_t inodes_in_group = ext4_inodes_in_group_cnt(sb, bg_ref->index); + uint32_t table_blocks = inodes_in_group / inodes_per_block; + ext4_fsblk_t fblock; + + if (inodes_in_group % inodes_per_block) + table_blocks++; + + /* Compute initialization bounds */ + ext4_fsblk_t first_block = ext4_bg_get_inode_table_first_block(bg, sb); + + ext4_fsblk_t last_block = first_block + table_blocks - 1; + + /* Initialization of all itable blocks */ + for (fblock = first_block; fblock <= last_block; ++fblock) { + struct ext4_block b; + int rc = ext4_trans_block_get_noread(bg_ref->fs->bdev, &b, fblock); + if (rc != EOK) + return rc; + + memset(b.data, 0, block_size); + ext4_trans_set_block_dirty(b.buf); + + rc = ext4_block_set(bg_ref->fs->bdev, &b); + if (rc != EOK) + return rc; + } + + return EOK; +} + +static ext4_fsblk_t ext4_fs_get_descriptor_block(struct ext4_sblock *s, + uint32_t bgid, + uint32_t dsc_per_block) +{ + uint32_t first_meta_bg, dsc_id; + int has_super = 0; + dsc_id = bgid / dsc_per_block; + first_meta_bg = ext4_sb_first_meta_bg(s); + + bool meta_bg = ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG); + + if (!meta_bg || dsc_id < first_meta_bg) + return ext4_get32(s, first_data_block) + dsc_id + 1; + + if (ext4_sb_is_super_in_bg(s, bgid)) + has_super = 1; + + return (has_super + ext4_fs_first_bg_block_no(s, bgid)); +} + +/**@brief Compute checksum of block group descriptor. + * @param sb Superblock + * @param bgid Index of block group in the filesystem + * @param bg Block group to compute checksum for + * @return Checksum value + */ +static uint16_t ext4_fs_bg_checksum(struct ext4_sblock *sb, uint32_t bgid, + struct ext4_bgroup *bg) +{ + /* If checksum not supported, 0 will be returned */ + uint16_t crc = 0; +#if CONFIG_META_CSUM_ENABLE + /* Compute the checksum only if the filesystem supports it */ + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + /* Use metadata_csum algorithm instead */ + uint32_t le32_bgid = to_le32(bgid); + uint32_t orig_checksum, checksum; + + /* Preparation: temporarily set bg checksum to 0 */ + orig_checksum = bg->checksum; + bg->checksum = 0; + + /* First calculate crc32 checksum against fs uuid */ + checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, + sizeof(sb->uuid)); + /* Then calculate crc32 checksum against bgid */ + checksum = ext4_crc32c(checksum, &le32_bgid, sizeof(bgid)); + /* Finally calculate crc32 checksum against block_group_desc */ + checksum = ext4_crc32c(checksum, bg, ext4_sb_get_desc_size(sb)); + bg->checksum = orig_checksum; + + crc = checksum & 0xFFFF; + return crc; + } +#endif + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_GDT_CSUM)) { + uint8_t *base = (uint8_t *)bg; + uint8_t *checksum = (uint8_t *)&bg->checksum; + + uint32_t offset = (uint32_t)(checksum - base); + + /* Convert block group index to little endian */ + uint32_t group = to_le32(bgid); + + /* Initialization */ + crc = ext4_bg_crc16(~0, sb->uuid, sizeof(sb->uuid)); + + /* Include index of block group */ + crc = ext4_bg_crc16(crc, (uint8_t *)&group, sizeof(group)); + + /* Compute crc from the first part (stop before checksum field) + */ + crc = ext4_bg_crc16(crc, (uint8_t *)bg, offset); + + /* Skip checksum */ + offset += sizeof(bg->checksum); + + /* Checksum of the rest of block group descriptor */ + if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_64BIT)) && + (offset < ext4_sb_get_desc_size(sb))) { + + const uint8_t *start = ((uint8_t *)bg) + offset; + size_t len = ext4_sb_get_desc_size(sb) - offset; + crc = ext4_bg_crc16(crc, start, len); + } + } + return crc; +} + +#if CONFIG_META_CSUM_ENABLE +static bool ext4_fs_verify_bg_csum(struct ext4_sblock *sb, + uint32_t bgid, + struct ext4_bgroup *bg) +{ + if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + return true; + + return ext4_fs_bg_checksum(sb, bgid, bg) == to_le16(bg->checksum); +} +#else +#define ext4_fs_verify_bg_csum(...) true +#endif + +int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid, + struct ext4_block_group_ref *ref) +{ + /* Compute number of descriptors, that fits in one data block */ + uint32_t block_size = ext4_sb_get_block_size(&fs->sb); + uint32_t dsc_cnt = block_size / ext4_sb_get_desc_size(&fs->sb); + + /* Block group descriptor table starts at the next block after + * superblock */ + uint64_t block_id = ext4_fs_get_descriptor_block(&fs->sb, bgid, dsc_cnt); + + uint32_t offset = (bgid % dsc_cnt) * ext4_sb_get_desc_size(&fs->sb); + + int rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id); + if (rc != EOK) + return rc; + + ref->block_group = (void *)(ref->block.data + offset); + ref->fs = fs; + ref->index = bgid; + ref->dirty = false; + struct ext4_bgroup *bg = ref->block_group; + + if (!ext4_fs_verify_bg_csum(&fs->sb, bgid, bg)) { + ext4_dbg(DEBUG_FS, + DBG_WARN "Block group descriptor checksum failed." + "Block group index: %" PRIu32"\n", + bgid); + } + + if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT)) { + rc = ext4_fs_init_block_bitmap(ref); + if (rc != EOK) { + ext4_block_set(fs->bdev, &ref->block); + return rc; + } + ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_BLOCK_UNINIT); + ref->dirty = true; + } + + if (ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT)) { + rc = ext4_fs_init_inode_bitmap(ref); + if (rc != EOK) { + ext4_block_set(ref->fs->bdev, &ref->block); + return rc; + } + + ext4_bg_clear_flag(bg, EXT4_BLOCK_GROUP_INODE_UNINIT); + + if (!ext4_bg_has_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED)) { + rc = ext4_fs_init_inode_table(ref); + if (rc != EOK) { + ext4_block_set(fs->bdev, &ref->block); + return rc; + } + + ext4_bg_set_flag(bg, EXT4_BLOCK_GROUP_ITABLE_ZEROED); + } + + ref->dirty = true; + } + + return EOK; +} + +int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref) +{ + /* Check if reference modified */ + if (ref->dirty) { + /* Compute new checksum of block group */ + uint16_t cs; + cs = ext4_fs_bg_checksum(&ref->fs->sb, ref->index, + ref->block_group); + ref->block_group->checksum = to_le16(cs); + + /* Mark block dirty for writing changes to physical device */ + ext4_trans_set_block_dirty(ref->block.buf); + } + + /* Put back block, that contains block group descriptor */ + return ext4_block_set(ref->fs->bdev, &ref->block); +} + +#if CONFIG_META_CSUM_ENABLE +static uint32_t ext4_fs_inode_checksum(struct ext4_inode_ref *inode_ref) +{ + uint32_t checksum = 0; + struct ext4_sblock *sb = &inode_ref->fs->sb; + uint16_t inode_size = ext4_get16(sb, inode_size); + + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + uint32_t orig_checksum; + + uint32_t ino_index = to_le32(inode_ref->index); + uint32_t ino_gen = + to_le32(ext4_inode_get_generation(inode_ref->inode)); + + /* Preparation: temporarily set bg checksum to 0 */ + orig_checksum = ext4_inode_get_csum(sb, inode_ref->inode); + ext4_inode_set_csum(sb, inode_ref->inode, 0); + + /* First calculate crc32 checksum against fs uuid */ + checksum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, + sizeof(sb->uuid)); + /* Then calculate crc32 checksum against inode number + * and inode generation */ + checksum = ext4_crc32c(checksum, &ino_index, sizeof(ino_index)); + checksum = ext4_crc32c(checksum, &ino_gen, sizeof(ino_gen)); + /* Finally calculate crc32 checksum against + * the entire inode */ + checksum = ext4_crc32c(checksum, inode_ref->inode, inode_size); + ext4_inode_set_csum(sb, inode_ref->inode, orig_checksum); + + /* If inode size is not large enough to hold the + * upper 16bit of the checksum */ + if (inode_size == EXT4_GOOD_OLD_INODE_SIZE) + checksum &= 0xFFFF; + + } + return checksum; +} +#else +#define ext4_fs_inode_checksum(...) 0 +#endif + +static void ext4_fs_set_inode_checksum(struct ext4_inode_ref *inode_ref) +{ + struct ext4_sblock *sb = &inode_ref->fs->sb; + if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + return; + + uint32_t csum = ext4_fs_inode_checksum(inode_ref); + ext4_inode_set_csum(sb, inode_ref->inode, csum); +} + +#if CONFIG_META_CSUM_ENABLE +static bool ext4_fs_verify_inode_csum(struct ext4_inode_ref *inode_ref) +{ + struct ext4_sblock *sb = &inode_ref->fs->sb; + if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + return true; + + return ext4_inode_get_csum(sb, inode_ref->inode) == + ext4_fs_inode_checksum(inode_ref); +} +#else +#define ext4_fs_verify_inode_csum(...) true +#endif + +static int +__ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index, + struct ext4_inode_ref *ref, + bool initialized) +{ + /* Compute number of i-nodes, that fits in one data block */ + uint32_t inodes_per_group = ext4_get32(&fs->sb, inodes_per_group); + + /* + * Inode numbers are 1-based, but it is simpler to work with 0-based + * when computing indices + */ + index -= 1; + uint32_t block_group = index / inodes_per_group; + uint32_t offset_in_group = index % inodes_per_group; + + /* Load block group, where i-node is located */ + struct ext4_block_group_ref bg_ref; + + int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref); + if (rc != EOK) { + return rc; + } + + /* Load block address, where i-node table is located */ + ext4_fsblk_t inode_table_start = + ext4_bg_get_inode_table_first_block(bg_ref.block_group, &fs->sb); + + /* Put back block group reference (not needed more) */ + rc = ext4_fs_put_block_group_ref(&bg_ref); + if (rc != EOK) { + return rc; + } + + /* Compute position of i-node in the block group */ + uint16_t inode_size = ext4_get16(&fs->sb, inode_size); + uint32_t block_size = ext4_sb_get_block_size(&fs->sb); + uint32_t byte_offset_in_group = offset_in_group * inode_size; + + /* Compute block address */ + ext4_fsblk_t block_id = + inode_table_start + (byte_offset_in_group / block_size); + + rc = ext4_trans_block_get(fs->bdev, &ref->block, block_id); + if (rc != EOK) { + return rc; + } + + /* Compute position of i-node in the data block */ + uint32_t offset_in_block = byte_offset_in_group % block_size; + ref->inode = (struct ext4_inode *)(ref->block.data + offset_in_block); + + /* We need to store the original value of index in the reference */ + ref->index = index + 1; + ref->fs = fs; + ref->dirty = false; + + if (initialized && !ext4_fs_verify_inode_csum(ref)) { + ext4_dbg(DEBUG_FS, + DBG_WARN "Inode checksum failed." + "Inode: %" PRIu32"\n", + ref->index); + } + + return EOK; +} + +int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index, + struct ext4_inode_ref *ref) +{ + return __ext4_fs_get_inode_ref(fs, index, ref, true); +} + +int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref) +{ + /* Check if reference modified */ + if (ref->dirty) { + /* Mark block dirty for writing changes to physical device */ + ext4_fs_set_inode_checksum(ref); + ext4_trans_set_block_dirty(ref->block.buf); + } + + /* Put back block, that contains i-node */ + return ext4_block_set(ref->fs->bdev, &ref->block); +} + +void ext4_fs_inode_blocks_init(struct ext4_fs *fs, + struct ext4_inode_ref *inode_ref) +{ + struct ext4_inode *inode = inode_ref->inode; + + /* Reset blocks array. For inode which is not directory or file, just + * fill in blocks with 0 */ + switch (ext4_inode_type(&fs->sb, inode_ref->inode)) { + case EXT4_INODE_MODE_FILE: + case EXT4_INODE_MODE_DIRECTORY: + break; + default: + return; + } + +#if CONFIG_EXTENT_ENABLE + /* Initialize extents if needed */ + if (ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) { + ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS); + + /* Initialize extent root header */ + ext4_extent_tree_init(inode_ref); + } + + inode_ref->dirty = true; +#endif +} + +uint32_t ext4_fs_correspond_inode_mode(int filetype) +{ + switch (filetype) { + case EXT4_DE_DIR: + return EXT4_INODE_MODE_DIRECTORY; + case EXT4_DE_REG_FILE: + return EXT4_INODE_MODE_FILE; + case EXT4_DE_SYMLINK: + return EXT4_INODE_MODE_SOFTLINK; + case EXT4_DE_CHRDEV: + return EXT4_INODE_MODE_CHARDEV; + case EXT4_DE_BLKDEV: + return EXT4_INODE_MODE_BLOCKDEV; + case EXT4_DE_FIFO: + return EXT4_INODE_MODE_FIFO; + case EXT4_DE_SOCK: + return EXT4_INODE_MODE_SOCKET; + } + /* FIXME: unsupported filetype */ + return EXT4_INODE_MODE_FILE; +} + +int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref, + int filetype) +{ + /* Check if newly allocated i-node will be a directory */ + bool is_dir; + uint16_t inode_size = ext4_get16(&fs->sb, inode_size); + + is_dir = (filetype == EXT4_DE_DIR); + + /* Allocate inode by allocation algorithm */ + uint32_t index; + int rc = ext4_ialloc_alloc_inode(fs, &index, is_dir); + if (rc != EOK) + return rc; + + /* Load i-node from on-disk i-node table */ + rc = __ext4_fs_get_inode_ref(fs, index, inode_ref, false); + if (rc != EOK) { + ext4_ialloc_free_inode(fs, index, is_dir); + return rc; + } + + /* Initialize i-node */ + struct ext4_inode *inode = inode_ref->inode; + + memset(inode, 0, inode_size); + + uint32_t mode; + if (is_dir) { + /* + * Default directory permissions to be compatible with other + * systems + * 0777 (octal) == rwxrwxrwx + */ + + mode = 0777; + mode |= EXT4_INODE_MODE_DIRECTORY; + } else if (filetype == EXT4_DE_SYMLINK) { + /* + * Default symbolic link permissions to be compatible with other systems + * 0777 (octal) == rwxrwxrwx + */ + + mode = 0777; + mode |= EXT4_INODE_MODE_SOFTLINK; + } else { + /* + * Default file permissions to be compatible with other systems + * 0666 (octal) == rw-rw-rw- + */ + + mode = 0666; + mode |= ext4_fs_correspond_inode_mode(filetype); + } + ext4_inode_set_mode(&fs->sb, inode, mode); + + ext4_inode_set_links_cnt(inode, 0); + ext4_inode_set_uid(inode, 0); + ext4_inode_set_gid(inode, 0); + ext4_inode_set_size(inode, 0); + ext4_inode_set_access_time(inode, 0); + ext4_inode_set_change_inode_time(inode, 0); + ext4_inode_set_modif_time(inode, 0); + ext4_inode_set_del_time(inode, 0); + ext4_inode_set_blocks_count(&fs->sb, inode, 0); + ext4_inode_set_flags(inode, 0); + ext4_inode_set_generation(inode, 0); + if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) { + uint16_t size = ext4_get16(&fs->sb, want_extra_isize); + ext4_inode_set_extra_isize(&fs->sb, inode, size); + } + + memset(inode->blocks, 0, sizeof(inode->blocks)); + inode_ref->dirty = true; + + return EOK; +} + +int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref) +{ + struct ext4_fs *fs = inode_ref->fs; + uint32_t offset; + uint32_t suboff; + int rc; +#if CONFIG_EXTENT_ENABLE + /* For extents must be data block destroyed by other way */ + if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { + /* Data structures are released during truncate operation... */ + goto finish; + } +#endif + /* Release all indirect (no data) blocks */ + + /* 1) Single indirect */ + ext4_fsblk_t fblock = ext4_inode_get_indirect_block(inode_ref->inode, 0); + if (fblock != 0) { + int rc = ext4_balloc_free_block(inode_ref, fblock); + if (rc != EOK) + return rc; + + ext4_inode_set_indirect_block(inode_ref->inode, 0, 0); + } + + uint32_t block_size = ext4_sb_get_block_size(&fs->sb); + uint32_t count = block_size / sizeof(uint32_t); + + struct ext4_block block; + + /* 2) Double indirect */ + fblock = ext4_inode_get_indirect_block(inode_ref->inode, 1); + if (fblock != 0) { + int rc = ext4_trans_block_get(fs->bdev, &block, fblock); + if (rc != EOK) + return rc; + + ext4_fsblk_t ind_block; + for (offset = 0; offset < count; ++offset) { + ind_block = to_le32(((uint32_t *)block.data)[offset]); + + if (ind_block == 0) + continue; + rc = ext4_balloc_free_block(inode_ref, ind_block); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + } + + ext4_block_set(fs->bdev, &block); + rc = ext4_balloc_free_block(inode_ref, fblock); + if (rc != EOK) + return rc; + + ext4_inode_set_indirect_block(inode_ref->inode, 1, 0); + } + + /* 3) Tripple indirect */ + struct ext4_block subblock; + fblock = ext4_inode_get_indirect_block(inode_ref->inode, 2); + if (fblock == 0) + goto finish; + rc = ext4_trans_block_get(fs->bdev, &block, fblock); + if (rc != EOK) + return rc; + + ext4_fsblk_t ind_block; + for (offset = 0; offset < count; ++offset) { + ind_block = to_le32(((uint32_t *)block.data)[offset]); + + if (ind_block == 0) + continue; + rc = ext4_trans_block_get(fs->bdev, &subblock, + ind_block); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + ext4_fsblk_t ind_subblk; + for (suboff = 0; suboff < count; ++suboff) { + ind_subblk = to_le32(((uint32_t *)subblock.data)[suboff]); + + if (ind_subblk == 0) + continue; + rc = ext4_balloc_free_block(inode_ref, ind_subblk); + if (rc != EOK) { + ext4_block_set(fs->bdev, &subblock); + ext4_block_set(fs->bdev, &block); + return rc; + } + + } + + ext4_block_set(fs->bdev, &subblock); + + rc = ext4_balloc_free_block(inode_ref, + ind_block); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + } + + ext4_block_set(fs->bdev, &block); + rc = ext4_balloc_free_block(inode_ref, fblock); + if (rc != EOK) + return rc; + + ext4_inode_set_indirect_block(inode_ref->inode, 2, 0); +finish: + /* Mark inode dirty for writing to the physical device */ + inode_ref->dirty = true; + + /* Free block with extended attributes if present */ + ext4_fsblk_t xattr_block = + ext4_inode_get_file_acl(inode_ref->inode, &fs->sb); + if (xattr_block) { + int rc = ext4_balloc_free_block(inode_ref, xattr_block); + if (rc != EOK) + return rc; + + ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, 0); + } + + /* Free inode by allocator */ + if (ext4_inode_is_type(&fs->sb, inode_ref->inode, + EXT4_INODE_MODE_DIRECTORY)) + rc = ext4_ialloc_free_inode(fs, inode_ref->index, true); + else + rc = ext4_ialloc_free_inode(fs, inode_ref->index, false); + + return rc; +} + + +/**@brief Release data block from i-node + * @param inode_ref I-node to release block from + * @param iblock Logical block to be released + * @return Error code + */ +static int ext4_fs_release_inode_block(struct ext4_inode_ref *inode_ref, + ext4_lblk_t iblock) +{ + ext4_fsblk_t fblock; + + struct ext4_fs *fs = inode_ref->fs; + + /* Extents are handled otherwise = there is not support in this function + */ + ext4_assert(!( + ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS)))); + + struct ext4_inode *inode = inode_ref->inode; + + /* Handle simple case when we are dealing with direct reference */ + if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) { + fblock = ext4_inode_get_direct_block(inode, iblock); + + /* Sparse file */ + if (fblock == 0) + return EOK; + + ext4_inode_set_direct_block(inode, iblock, 0); + return ext4_balloc_free_block(inode_ref, fblock); + } + + /* Determine the indirection level needed to get the desired block */ + unsigned int level = 0; + unsigned int i; + for (i = 1; i < 4; i++) { + if (iblock < fs->inode_block_limits[i]) { + level = i; + break; + } + } + + if (level == 0) + return EIO; + + /* Compute offsets for the topmost level */ + uint32_t block_offset_in_level = + (uint32_t)(iblock - fs->inode_block_limits[level - 1]); + ext4_fsblk_t current_block = + ext4_inode_get_indirect_block(inode, level - 1); + uint32_t offset_in_block = + (uint32_t)(block_offset_in_level / fs->inode_blocks_per_level[level - 1]); + + /* + * Navigate through other levels, until we find the block number + * or find null reference meaning we are dealing with sparse file + */ + struct ext4_block block; + + while (level > 0) { + + /* Sparse check */ + if (current_block == 0) + return EOK; + + int rc = ext4_trans_block_get(fs->bdev, &block, current_block); + if (rc != EOK) + return rc; + + current_block = + to_le32(((uint32_t *)block.data)[offset_in_block]); + + /* Set zero if physical data block address found */ + if (level == 1) { + ((uint32_t *)block.data)[offset_in_block] = to_le32(0); + ext4_trans_set_block_dirty(block.buf); + } + + rc = ext4_block_set(fs->bdev, &block); + if (rc != EOK) + return rc; + + level--; + + /* + * If we are on the last level, break here as + * there is no next level to visit + */ + if (level == 0) + break; + + /* Visit the next level */ + block_offset_in_level %= fs->inode_blocks_per_level[level]; + offset_in_block = (uint32_t)(block_offset_in_level / + fs->inode_blocks_per_level[level - 1]); + } + + fblock = current_block; + if (fblock == 0) + return EOK; + + /* Physical block is not referenced, it can be released */ + return ext4_balloc_free_block(inode_ref, fblock); +} + +int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size) +{ + struct ext4_sblock *sb = &inode_ref->fs->sb; + uint32_t i; + int r; + bool v; + + /* Check flags, if i-node can be truncated */ + if (!ext4_inode_can_truncate(sb, inode_ref->inode)) + return EINVAL; + + /* If sizes are equal, nothing has to be done. */ + uint64_t old_size = ext4_inode_get_size(sb, inode_ref->inode); + if (old_size == new_size) + return EOK; + + /* It's not supported to make the larger file by truncate operation */ + if (old_size < new_size) + return EINVAL; + + /* For symbolic link which is small enough */ + v = ext4_inode_is_type(sb, inode_ref->inode, EXT4_INODE_MODE_SOFTLINK); + if (v && old_size < sizeof(inode_ref->inode->blocks) && + !ext4_inode_get_blocks_count(sb, inode_ref->inode)) { + char *content = (char *)inode_ref->inode->blocks + new_size; + memset(content, 0, + sizeof(inode_ref->inode->blocks) - (uint32_t)new_size); + ext4_inode_set_size(inode_ref->inode, new_size); + inode_ref->dirty = true; + + return EOK; + } + + i = ext4_inode_type(sb, inode_ref->inode); + if (i == EXT4_INODE_MODE_CHARDEV || + i == EXT4_INODE_MODE_BLOCKDEV || + i == EXT4_INODE_MODE_SOCKET) { + inode_ref->inode->blocks[0] = 0; + inode_ref->inode->blocks[1] = 0; + + inode_ref->dirty = true; + return EOK; + } + + /* Compute how many blocks will be released */ + uint32_t block_size = ext4_sb_get_block_size(sb); + uint32_t new_blocks_cnt = (uint32_t)((new_size + block_size - 1) / block_size); + uint32_t old_blocks_cnt = (uint32_t)((old_size + block_size - 1) / block_size); + uint32_t diff_blocks_cnt = old_blocks_cnt - new_blocks_cnt; +#if CONFIG_EXTENT_ENABLE + if ((ext4_sb_feature_incom(sb, EXT4_FINCOM_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { + + /* Extents require special operation */ + if (diff_blocks_cnt) { + r = ext4_extent_remove_space(inode_ref, new_blocks_cnt, + EXT_MAX_BLOCKS); + if (r != EOK) + return r; + + } + } else +#endif + { + /* Release data blocks from the end of file */ + + /* Starting from 1 because of logical blocks are numbered from 0 + */ + for (i = 0; i < diff_blocks_cnt; ++i) { + r = ext4_fs_release_inode_block(inode_ref, + new_blocks_cnt + i); + if (r != EOK) + return r; + } + } + + /* Update i-node */ + ext4_inode_set_size(inode_ref->inode, new_size); + inode_ref->dirty = true; + + return EOK; +} + +/**@brief Compute 'goal' for inode index + * @param inode_ref Reference to inode, to allocate block for + * @return goal + */ +ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref) +{ + uint32_t grp_inodes = ext4_get32(&inode_ref->fs->sb, inodes_per_group); + return (inode_ref->index - 1) / grp_inodes; +} + +/**@brief Compute 'goal' for allocation algorithm (For blockmap). + * @param inode_ref Reference to inode, to allocate block for + * @param goal + * @return error code + */ +int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t *goal) +{ + int r; + struct ext4_sblock *sb = &inode_ref->fs->sb; + *goal = 0; + + uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode); + uint32_t block_size = ext4_sb_get_block_size(sb); + uint32_t iblock_cnt = (uint32_t)(inode_size / block_size); + + if (inode_size % block_size != 0) + iblock_cnt++; + + /* If inode has some blocks, get last block address + 1 */ + if (iblock_cnt > 0) { + r = ext4_fs_get_inode_dblk_idx(inode_ref, iblock_cnt - 1, + goal, false); + if (r != EOK) + return r; + + if (*goal != 0) { + (*goal)++; + return r; + } + + /* If goal == 0, sparse file -> continue */ + } + + /* Identify block group of inode */ + + uint32_t inodes_per_bg = ext4_get32(sb, inodes_per_group); + uint32_t block_group = (inode_ref->index - 1) / inodes_per_bg; + block_size = ext4_sb_get_block_size(sb); + + /* Load block group reference */ + struct ext4_block_group_ref bg_ref; + r = ext4_fs_get_block_group_ref(inode_ref->fs, block_group, &bg_ref); + if (r != EOK) + return r; + + struct ext4_bgroup *bg = bg_ref.block_group; + + /* Compute indexes */ + uint32_t bg_count = ext4_block_group_cnt(sb); + ext4_fsblk_t itab_first_block = ext4_bg_get_inode_table_first_block(bg, sb); + uint16_t itab_item_size = ext4_get16(sb, inode_size); + uint32_t itab_bytes; + + /* Check for last block group */ + if (block_group < bg_count - 1) { + itab_bytes = inodes_per_bg * itab_item_size; + } else { + /* Last block group could be smaller */ + uint32_t inodes_cnt = ext4_get32(sb, inodes_count); + + itab_bytes = (inodes_cnt - ((bg_count - 1) * inodes_per_bg)); + itab_bytes *= itab_item_size; + } + + ext4_fsblk_t inode_table_blocks = itab_bytes / block_size; + + if (itab_bytes % block_size) + inode_table_blocks++; + + *goal = itab_first_block + inode_table_blocks; + + return ext4_fs_put_block_group_ref(&bg_ref); +} + +static int ext4_fs_get_inode_dblk_idx_internal(struct ext4_inode_ref *inode_ref, + ext4_lblk_t iblock, ext4_fsblk_t *fblock, + bool extent_create, + bool support_unwritten __unused) +{ + struct ext4_fs *fs = inode_ref->fs; + + /* For empty file is situation simple */ + if (ext4_inode_get_size(&fs->sb, inode_ref->inode) == 0) { + *fblock = 0; + return EOK; + } + + ext4_fsblk_t current_block; + + (void)extent_create; +#if CONFIG_EXTENT_ENABLE + /* Handle i-node using extents */ + if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { + + ext4_fsblk_t current_fsblk; + int rc = ext4_extent_get_blocks(inode_ref, iblock, 1, + ¤t_fsblk, extent_create, NULL); + if (rc != EOK) + return rc; + + current_block = current_fsblk; + *fblock = current_block; + + ext4_assert(*fblock || support_unwritten); + return EOK; + } +#endif + + struct ext4_inode *inode = inode_ref->inode; + + /* Direct block are read directly from array in i-node structure */ + if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) { + current_block = + ext4_inode_get_direct_block(inode, (uint32_t)iblock); + *fblock = current_block; + return EOK; + } + + /* Determine indirection level of the target block */ + unsigned int l = 0; + unsigned int i; + for (i = 1; i < 4; i++) { + if (iblock < fs->inode_block_limits[i]) { + l = i; + break; + } + } + + if (l == 0) + return EIO; + + /* Compute offsets for the topmost level */ + uint32_t blk_off_in_lvl = (uint32_t)(iblock - fs->inode_block_limits[l - 1]); + current_block = ext4_inode_get_indirect_block(inode, l - 1); + uint32_t off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]); + + /* Sparse file */ + if (current_block == 0) { + *fblock = 0; + return EOK; + } + + struct ext4_block block; + + /* + * Navigate through other levels, until we find the block number + * or find null reference meaning we are dealing with sparse file + */ + while (l > 0) { + /* Load indirect block */ + int rc = ext4_trans_block_get(fs->bdev, &block, current_block); + if (rc != EOK) + return rc; + + /* Read block address from indirect block */ + current_block = + to_le32(((uint32_t *)block.data)[off_in_blk]); + + /* Put back indirect block untouched */ + rc = ext4_block_set(fs->bdev, &block); + if (rc != EOK) + return rc; + + /* Check for sparse file */ + if (current_block == 0) { + *fblock = 0; + return EOK; + } + + /* Jump to the next level */ + l--; + + /* Termination condition - we have address of data block loaded + */ + if (l == 0) + break; + + /* Visit the next level */ + blk_off_in_lvl %= fs->inode_blocks_per_level[l]; + off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]); + } + + *fblock = current_block; + + return EOK; +} + + +int ext4_fs_get_inode_dblk_idx(struct ext4_inode_ref *inode_ref, + ext4_lblk_t iblock, ext4_fsblk_t *fblock, + bool support_unwritten) +{ + return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock, + false, support_unwritten); +} + +int ext4_fs_init_inode_dblk_idx(struct ext4_inode_ref *inode_ref, + ext4_lblk_t iblock, ext4_fsblk_t *fblock) +{ + return ext4_fs_get_inode_dblk_idx_internal(inode_ref, iblock, fblock, + true, true); +} + +static int ext4_fs_set_inode_data_block_index(struct ext4_inode_ref *inode_ref, + ext4_lblk_t iblock, ext4_fsblk_t fblock) +{ + struct ext4_fs *fs = inode_ref->fs; + +#if CONFIG_EXTENT_ENABLE + /* Handle inode using extents */ + if ((ext4_sb_feature_incom(&fs->sb, EXT4_FINCOM_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { + /* Not reachable */ + return ENOTSUP; + } +#endif + + /* Handle simple case when we are dealing with direct reference */ + if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) { + ext4_inode_set_direct_block(inode_ref->inode, (uint32_t)iblock, + (uint32_t)fblock); + inode_ref->dirty = true; + + return EOK; + } + + /* Determine the indirection level needed to get the desired block */ + unsigned int l = 0; + unsigned int i; + for (i = 1; i < 4; i++) { + if (iblock < fs->inode_block_limits[i]) { + l = i; + break; + } + } + + if (l == 0) + return EIO; + + uint32_t block_size = ext4_sb_get_block_size(&fs->sb); + + /* Compute offsets for the topmost level */ + uint32_t blk_off_in_lvl = (uint32_t)(iblock - fs->inode_block_limits[l - 1]); + ext4_fsblk_t current_block = + ext4_inode_get_indirect_block(inode_ref->inode, l - 1); + uint32_t off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]); + + ext4_fsblk_t new_blk; + + struct ext4_block block; + struct ext4_block new_block; + + /* Is needed to allocate indirect block on the i-node level */ + if (current_block == 0) { + /* Allocate new indirect block */ + ext4_fsblk_t goal; + int rc = ext4_fs_indirect_find_goal(inode_ref, &goal); + if (rc != EOK) + return rc; + + rc = ext4_balloc_alloc_block(inode_ref, goal, &new_blk); + if (rc != EOK) + return rc; + + /* Update i-node */ + ext4_inode_set_indirect_block(inode_ref->inode, l - 1, + (uint32_t)new_blk); + inode_ref->dirty = true; + + /* Load newly allocated block */ + rc = ext4_trans_block_get_noread(fs->bdev, &new_block, new_blk); + if (rc != EOK) { + ext4_balloc_free_block(inode_ref, new_blk); + return rc; + } + + /* Initialize new block */ + memset(new_block.data, 0, block_size); + ext4_trans_set_block_dirty(new_block.buf); + + /* Put back the allocated block */ + rc = ext4_block_set(fs->bdev, &new_block); + if (rc != EOK) + return rc; + + current_block = new_blk; + } + + /* + * Navigate through other levels, until we find the block number + * or find null reference meaning we are dealing with sparse file + */ + while (l > 0) { + int rc = ext4_trans_block_get(fs->bdev, &block, current_block); + if (rc != EOK) + return rc; + + current_block = to_le32(((uint32_t *)block.data)[off_in_blk]); + if ((l > 1) && (current_block == 0)) { + ext4_fsblk_t goal; + rc = ext4_fs_indirect_find_goal(inode_ref, &goal); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + /* Allocate new block */ + rc = + ext4_balloc_alloc_block(inode_ref, goal, &new_blk); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + /* Load newly allocated block */ + rc = ext4_trans_block_get_noread(fs->bdev, &new_block, + new_blk); + + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + /* Initialize allocated block */ + memset(new_block.data, 0, block_size); + ext4_trans_set_block_dirty(new_block.buf); + + rc = ext4_block_set(fs->bdev, &new_block); + if (rc != EOK) { + ext4_block_set(fs->bdev, &block); + return rc; + } + + /* Write block address to the parent */ + uint32_t * p = (uint32_t * )block.data; + p[off_in_blk] = to_le32((uint32_t)new_blk); + ext4_trans_set_block_dirty(block.buf); + current_block = new_blk; + } + + /* Will be finished, write the fblock address */ + if (l == 1) { + uint32_t * p = (uint32_t * )block.data; + p[off_in_blk] = to_le32((uint32_t)fblock); + ext4_trans_set_block_dirty(block.buf); + } + + rc = ext4_block_set(fs->bdev, &block); + if (rc != EOK) + return rc; + + l--; + + /* + * If we are on the last level, break here as + * there is no next level to visit + */ + if (l == 0) + break; + + /* Visit the next level */ + blk_off_in_lvl %= fs->inode_blocks_per_level[l]; + off_in_blk = (uint32_t)(blk_off_in_lvl / fs->inode_blocks_per_level[l - 1]); + } + + return EOK; +} + + +int ext4_fs_append_inode_dblk(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t *fblock, ext4_lblk_t *iblock) +{ +#if CONFIG_EXTENT_ENABLE + /* Handle extents separately */ + if ((ext4_sb_feature_incom(&inode_ref->fs->sb, EXT4_FINCOM_EXTENTS)) && + (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) { + int rc; + ext4_fsblk_t current_fsblk; + struct ext4_sblock *sb = &inode_ref->fs->sb; + uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode); + uint32_t block_size = ext4_sb_get_block_size(sb); + *iblock = (uint32_t)((inode_size + block_size - 1) / block_size); + + rc = ext4_extent_get_blocks(inode_ref, *iblock, 1, + ¤t_fsblk, true, NULL); + if (rc != EOK) + return rc; + + *fblock = current_fsblk; + ext4_assert(*fblock); + + ext4_inode_set_size(inode_ref->inode, inode_size + block_size); + inode_ref->dirty = true; + + + return rc; + } +#endif + struct ext4_sblock *sb = &inode_ref->fs->sb; + + /* Compute next block index and allocate data block */ + uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode); + uint32_t block_size = ext4_sb_get_block_size(sb); + + /* Align size i-node size */ + if ((inode_size % block_size) != 0) + inode_size += block_size - (inode_size % block_size); + + /* Logical blocks are numbered from 0 */ + uint32_t new_block_idx = (uint32_t)(inode_size / block_size); + + /* Allocate new physical block */ + ext4_fsblk_t goal, phys_block; + int rc = ext4_fs_indirect_find_goal(inode_ref, &goal); + if (rc != EOK) + return rc; + + rc = ext4_balloc_alloc_block(inode_ref, goal, &phys_block); + if (rc != EOK) + return rc; + + /* Add physical block address to the i-node */ + rc = ext4_fs_set_inode_data_block_index(inode_ref, new_block_idx, + phys_block); + if (rc != EOK) { + ext4_balloc_free_block(inode_ref, phys_block); + return rc; + } + + /* Update i-node */ + ext4_inode_set_size(inode_ref->inode, inode_size + block_size); + inode_ref->dirty = true; + + *fblock = phys_block; + *iblock = new_block_idx; + + return EOK; +} + +void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref) +{ + uint16_t link; + bool is_dx; + link = ext4_inode_get_links_cnt(inode_ref->inode); + link++; + ext4_inode_set_links_cnt(inode_ref->inode, link); + + is_dx = ext4_sb_feature_com(&inode_ref->fs->sb, EXT4_FCOM_DIR_INDEX) && + ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_INDEX); + + if (is_dx && link > 1) { + if (link >= EXT4_LINK_MAX || link == 2) { + ext4_inode_set_links_cnt(inode_ref->inode, 1); + + uint32_t v; + v = ext4_get32(&inode_ref->fs->sb, features_read_only); + v |= EXT4_FRO_COM_DIR_NLINK; + ext4_set32(&inode_ref->fs->sb, features_read_only, v); + } + } +} + +void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref) +{ + uint16_t links = ext4_inode_get_links_cnt(inode_ref->inode); + if (!ext4_inode_is_type(&inode_ref->fs->sb, inode_ref->inode, + EXT4_INODE_MODE_DIRECTORY)) { + if (links > 0) + ext4_inode_set_links_cnt(inode_ref->inode, links - 1); + return; + } + + if (links > 2) + ext4_inode_set_links_cnt(inode_ref->inode, links - 1); +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_hash.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_hash.c new file mode 100644 index 0000000..7de4873 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_hash.c @@ -0,0 +1,327 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * FreeBSD: + * Copyright (c) 2010, 2013 Zheng Liu + * Copyright (c) 2012, Vyacheslav Matyushin + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +/* + * The following notice applies to the code in ext2_half_md4(): + * + * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved. + * + * License to copy and use this software is granted provided that it + * is identified as the "RSA Data Security, Inc. MD4 Message-Digest + * Algorithm" in all material mentioning or referencing this software + * or this function. + * + * License is also granted to make and use derivative works provided + * that such works are identified as "derived from the RSA Data + * Security, Inc. MD4 Message-Digest Algorithm" in all material + * mentioning or referencing the derived work. + * + * RSA Data Security, Inc. makes no representations concerning either + * the merchantability of this software or the suitability of this + * software for any particular purpose. It is provided "as is" + * without express or implied warranty of any kind. + * + * These notices must be retained in any copies of any part of this + * documentation and/or software. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_hash.c + * @brief Directory indexing hash functions. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include + +/* F, G, and H are MD4 functions */ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) + +/* ROTATE_LEFT rotates x left n bits */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) + +/* + * FF, GG, and HH are transformations for rounds 1, 2, and 3. + * Rotation is separated from addition to prevent recomputation. + */ +#define FF(a, b, c, d, x, s) \ + { \ + (a) += F((b), (c), (d)) + (x); \ + (a) = ROTATE_LEFT((a), (s)); \ + \ +} + +#define GG(a, b, c, d, x, s) \ + { \ + (a) += G((b), (c), (d)) + (x) + (uint32_t)0x5A827999; \ + (a) = ROTATE_LEFT((a), (s)); \ + \ +} + +#define HH(a, b, c, d, x, s) \ + { \ + (a) += H((b), (c), (d)) + (x) + (uint32_t)0x6ED9EBA1; \ + (a) = ROTATE_LEFT((a), (s)); \ + \ +} + +/* + * MD4 basic transformation. It transforms state based on block. + * + * This is a half md4 algorithm since Linux uses this algorithm for dir + * index. This function is derived from the RSA Data Security, Inc. MD4 + * Message-Digest Algorithm and was modified as necessary. + * + * The return value of this function is uint32_t in Linux, but actually we don't + * need to check this value, so in our version this function doesn't return any + * value. + */ +static void ext2_half_md4(uint32_t hash[4], uint32_t data[8]) +{ + uint32_t a = hash[0], b = hash[1], c = hash[2], d = hash[3]; + + /* Round 1 */ + FF(a, b, c, d, data[0], 3); + FF(d, a, b, c, data[1], 7); + FF(c, d, a, b, data[2], 11); + FF(b, c, d, a, data[3], 19); + FF(a, b, c, d, data[4], 3); + FF(d, a, b, c, data[5], 7); + FF(c, d, a, b, data[6], 11); + FF(b, c, d, a, data[7], 19); + + /* Round 2 */ + GG(a, b, c, d, data[1], 3); + GG(d, a, b, c, data[3], 5); + GG(c, d, a, b, data[5], 9); + GG(b, c, d, a, data[7], 13); + GG(a, b, c, d, data[0], 3); + GG(d, a, b, c, data[2], 5); + GG(c, d, a, b, data[4], 9); + GG(b, c, d, a, data[6], 13); + + /* Round 3 */ + HH(a, b, c, d, data[3], 3); + HH(d, a, b, c, data[7], 9); + HH(c, d, a, b, data[2], 11); + HH(b, c, d, a, data[6], 15); + HH(a, b, c, d, data[1], 3); + HH(d, a, b, c, data[5], 9); + HH(c, d, a, b, data[0], 11); + HH(b, c, d, a, data[4], 15); + + hash[0] += a; + hash[1] += b; + hash[2] += c; + hash[3] += d; +} + +/* + * Tiny Encryption Algorithm. + */ +static void ext2_tea(uint32_t hash[4], uint32_t data[8]) +{ + uint32_t tea_delta = 0x9E3779B9; + uint32_t sum; + uint32_t x = hash[0], y = hash[1]; + int n = 16; + int i = 1; + + while (n-- > 0) { + sum = i * tea_delta; + x += ((y << 4) + data[0]) ^ (y + sum) ^ ((y >> 5) + data[1]); + y += ((x << 4) + data[2]) ^ (x + sum) ^ ((x >> 5) + data[3]); + i++; + } + + hash[0] += x; + hash[1] += y; +} + +static uint32_t ext2_legacy_hash(const char *name, int len, int unsigned_char) +{ + uint32_t h0, h1 = 0x12A3FE2D, h2 = 0x37ABE8F9; + uint32_t multi = 0x6D22F5; + const unsigned char *uname = (const unsigned char *)name; + const signed char *sname = (const signed char *)name; + int val, i; + + for (i = 0; i < len; i++) { + if (unsigned_char) + val = (unsigned int)*uname++; + else + val = (int)*sname++; + + h0 = h2 + (h1 ^ (val * multi)); + if (h0 & 0x80000000) + h0 -= 0x7FFFFFFF; + h2 = h1; + h1 = h0; + } + + return (h1 << 1); +} + +static void ext2_prep_hashbuf(const char *src, uint32_t slen, uint32_t *dst, + int dlen, int unsigned_char) +{ + uint32_t padding = slen | (slen << 8) | (slen << 16) | (slen << 24); + uint32_t buf_val; + int len, i; + int buf_byte; + const unsigned char *ubuf = (const unsigned char *)src; + const signed char *sbuf = (const signed char *)src; + + if (slen > (uint32_t)dlen) + len = dlen; + else + len = slen; + + buf_val = padding; + + for (i = 0; i < len; i++) { + if (unsigned_char) + buf_byte = (unsigned int)ubuf[i]; + else + buf_byte = (int)sbuf[i]; + + if ((i % 4) == 0) + buf_val = padding; + + buf_val <<= 8; + buf_val += buf_byte; + + if ((i % 4) == 3) { + *dst++ = buf_val; + dlen -= sizeof(uint32_t); + buf_val = padding; + } + } + + dlen -= sizeof(uint32_t); + if (dlen >= 0) + *dst++ = buf_val; + + dlen -= sizeof(uint32_t); + while (dlen >= 0) { + *dst++ = padding; + dlen -= sizeof(uint32_t); + } +} + +int ext2_htree_hash(const char *name, int len, const uint32_t *hash_seed, + int hash_version, uint32_t *hash_major, + uint32_t *hash_minor) +{ + uint32_t hash[4]; + uint32_t data[8]; + uint32_t major = 0, minor = 0; + int unsigned_char = 0; + + if (!name || !hash_major) + return (-1); + + if (len < 1 || len > 255) + goto error; + + hash[0] = 0x67452301; + hash[1] = 0xEFCDAB89; + hash[2] = 0x98BADCFE; + hash[3] = 0x10325476; + + if (hash_seed) + memcpy(hash, hash_seed, sizeof(hash)); + + switch (hash_version) { + case EXT2_HTREE_TEA_UNSIGNED: + unsigned_char = 1; + /* FALLTHRU */ + case EXT2_HTREE_TEA: + while (len > 0) { + ext2_prep_hashbuf(name, len, data, 16, unsigned_char); + ext2_tea(hash, data); + len -= 16; + name += 16; + } + major = hash[0]; + minor = hash[1]; + break; + case EXT2_HTREE_LEGACY_UNSIGNED: + unsigned_char = 1; + /* FALLTHRU */ + case EXT2_HTREE_LEGACY: + major = ext2_legacy_hash(name, len, unsigned_char); + break; + case EXT2_HTREE_HALF_MD4_UNSIGNED: + unsigned_char = 1; + /* FALLTHRU */ + case EXT2_HTREE_HALF_MD4: + while (len > 0) { + ext2_prep_hashbuf(name, len, data, 32, unsigned_char); + ext2_half_md4(hash, data); + len -= 32; + name += 32; + } + major = hash[1]; + minor = hash[2]; + break; + default: + goto error; + } + + major &= ~1; + if (major == (EXT2_HTREE_EOF << 1)) + major = (EXT2_HTREE_EOF - 1) << 1; + *hash_major = major; + if (hash_minor) + *hash_minor = minor; + + return EOK; + +error: + *hash_major = 0; + if (hash_minor) + *hash_minor = 0; + return ENOTSUP; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_ialloc.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_ialloc.c new file mode 100644 index 0000000..c8621ea --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_ialloc.c @@ -0,0 +1,370 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_ialloc.c + * @brief Inode allocation procedures. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_trans.h" +#include "ext4_ialloc.h" +#include "ext4_super.h" +#include "ext4_crc32.h" +#include "ext4_fs.h" +#include "ext4_blockdev.h" +#include "ext4_block_group.h" +#include "ext4_bitmap.h" + +/**@brief Convert i-node number to relative index in block group. + * @param sb Superblock + * @param inode I-node number to be converted + * @return Index of the i-node in the block group + */ +static uint32_t ext4_ialloc_inode_to_bgidx(struct ext4_sblock *sb, + uint32_t inode) +{ + uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group); + return (inode - 1) % inodes_per_group; +} + +/**@brief Convert relative index of i-node to absolute i-node number. + * @param sb Superblock + * @param index Index to be converted + * @return Absolute number of the i-node + * + */ +static uint32_t ext4_ialloc_bgidx_to_inode(struct ext4_sblock *sb, + uint32_t index, uint32_t bgid) +{ + uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group); + return bgid * inodes_per_group + (index + 1); +} + +/**@brief Compute block group number from the i-node number. + * @param sb Superblock + * @param inode I-node number to be found the block group for + * @return Block group number computed from i-node number + */ +static uint32_t ext4_ialloc_get_bgid_of_inode(struct ext4_sblock *sb, + uint32_t inode) +{ + uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group); + return (inode - 1) / inodes_per_group; +} + +#if CONFIG_META_CSUM_ENABLE +static uint32_t ext4_ialloc_bitmap_csum(struct ext4_sblock *sb, void *bitmap) +{ + uint32_t csum = 0; + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + uint32_t inodes_per_group = + ext4_get32(sb, inodes_per_group); + + /* First calculate crc32 checksum against fs uuid */ + csum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid)); + /* Then calculate crc32 checksum against inode bitmap */ + csum = ext4_crc32c(csum, bitmap, (inodes_per_group + 7) / 8); + } + return csum; +} +#else +#define ext4_ialloc_bitmap_csum(...) 0 +#endif + +void ext4_ialloc_set_bitmap_csum(struct ext4_sblock *sb, struct ext4_bgroup *bg, + void *bitmap __unused) +{ + int desc_size = ext4_sb_get_desc_size(sb); + uint32_t csum = ext4_ialloc_bitmap_csum(sb, bitmap); + uint16_t lo_csum = to_le16(csum & 0xFFFF), + hi_csum = to_le16(csum >> 16); + + if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + return; + + /* See if we need to assign a 32bit checksum */ + bg->inode_bitmap_csum_lo = lo_csum; + if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE) + bg->inode_bitmap_csum_hi = hi_csum; + +} + +#if CONFIG_META_CSUM_ENABLE +static bool +ext4_ialloc_verify_bitmap_csum(struct ext4_sblock *sb, struct ext4_bgroup *bg, + void *bitmap __unused) +{ + + int desc_size = ext4_sb_get_desc_size(sb); + uint32_t csum = ext4_ialloc_bitmap_csum(sb, bitmap); + uint16_t lo_csum = to_le16(csum & 0xFFFF), + hi_csum = to_le16(csum >> 16); + + if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + return true; + + if (bg->inode_bitmap_csum_lo != lo_csum) + return false; + + if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE) + if (bg->inode_bitmap_csum_hi != hi_csum) + return false; + + return true; +} +#else +#define ext4_ialloc_verify_bitmap_csum(...) true +#endif + +int ext4_ialloc_free_inode(struct ext4_fs *fs, uint32_t index, bool is_dir) +{ + struct ext4_sblock *sb = &fs->sb; + + /* Compute index of block group and load it */ + uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index); + + struct ext4_block_group_ref bg_ref; + int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref); + if (rc != EOK) + return rc; + + struct ext4_bgroup *bg = bg_ref.block_group; + + /* Load i-node bitmap */ + ext4_fsblk_t bitmap_block_addr = + ext4_bg_get_inode_bitmap(bg, sb); + + struct ext4_block b; + rc = ext4_trans_block_get(fs->bdev, &b, bitmap_block_addr); + if (rc != EOK) + return rc; + + if (!ext4_ialloc_verify_bitmap_csum(sb, bg, b.data)) { + ext4_dbg(DEBUG_IALLOC, + DBG_WARN "Bitmap checksum failed." + "Group: %" PRIu32"\n", + bg_ref.index); + } + + /* Free i-node in the bitmap */ + uint32_t index_in_group = ext4_ialloc_inode_to_bgidx(sb, index); + ext4_bmap_bit_clr(b.data, index_in_group); + ext4_ialloc_set_bitmap_csum(sb, bg, b.data); + ext4_trans_set_block_dirty(b.buf); + + /* Put back the block with bitmap */ + rc = ext4_block_set(fs->bdev, &b); + if (rc != EOK) { + /* Error in saving bitmap */ + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + /* If released i-node is a directory, decrement used directories count + */ + if (is_dir) { + uint32_t bg_used_dirs = ext4_bg_get_used_dirs_count(bg, sb); + bg_used_dirs--; + ext4_bg_set_used_dirs_count(bg, sb, bg_used_dirs); + } + + /* Update block group free inodes count */ + uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb); + free_inodes++; + ext4_bg_set_free_inodes_count(bg, sb, free_inodes); + + bg_ref.dirty = true; + + /* Put back the modified block group */ + rc = ext4_fs_put_block_group_ref(&bg_ref); + if (rc != EOK) + return rc; + + /* Update superblock free inodes count */ + ext4_set32(sb, free_inodes_count, + ext4_get32(sb, free_inodes_count) + 1); + + return EOK; +} + +int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *idx, bool is_dir) +{ + struct ext4_sblock *sb = &fs->sb; + + uint32_t bgid = fs->last_inode_bg_id; + uint32_t bg_count = ext4_block_group_cnt(sb); + uint32_t sb_free_inodes = ext4_get32(sb, free_inodes_count); + bool rewind = false; + + /* Try to find free i-node in all block groups */ + while (bgid <= bg_count) { + + if (bgid == bg_count) { + if (rewind) + break; + bg_count = fs->last_inode_bg_id; + bgid = 0; + rewind = true; + continue; + } + + /* Load block group to check */ + struct ext4_block_group_ref bg_ref; + int rc = ext4_fs_get_block_group_ref(fs, bgid, &bg_ref); + if (rc != EOK) + return rc; + + struct ext4_bgroup *bg = bg_ref.block_group; + + /* Read necessary values for algorithm */ + uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb); + uint32_t used_dirs = ext4_bg_get_used_dirs_count(bg, sb); + + /* Check if this block group is good candidate for allocation */ + if (free_inodes > 0) { + /* Load block with bitmap */ + ext4_fsblk_t bmp_blk_add = ext4_bg_get_inode_bitmap(bg, sb); + + struct ext4_block b; + rc = ext4_trans_block_get(fs->bdev, &b, bmp_blk_add); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + if (!ext4_ialloc_verify_bitmap_csum(sb, bg, b.data)) { + ext4_dbg(DEBUG_IALLOC, + DBG_WARN "Bitmap checksum failed." + "Group: %" PRIu32"\n", + bg_ref.index); + } + + /* Try to allocate i-node in the bitmap */ + uint32_t inodes_in_bg; + uint32_t idx_in_bg; + + inodes_in_bg = ext4_inodes_in_group_cnt(sb, bgid); + rc = ext4_bmap_bit_find_clr(b.data, 0, inodes_in_bg, + &idx_in_bg); + /* Block group has not any free i-node */ + if (rc == ENOSPC) { + rc = ext4_block_set(fs->bdev, &b); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + rc = ext4_fs_put_block_group_ref(&bg_ref); + if (rc != EOK) + return rc; + + continue; + } + + ext4_bmap_bit_set(b.data, idx_in_bg); + + /* Free i-node found, save the bitmap */ + ext4_ialloc_set_bitmap_csum(sb,bg, + b.data); + ext4_trans_set_block_dirty(b.buf); + + ext4_block_set(fs->bdev, &b); + if (rc != EOK) { + ext4_fs_put_block_group_ref(&bg_ref); + return rc; + } + + /* Modify filesystem counters */ + free_inodes--; + ext4_bg_set_free_inodes_count(bg, sb, free_inodes); + + /* Increment used directories counter */ + if (is_dir) { + used_dirs++; + ext4_bg_set_used_dirs_count(bg, sb, used_dirs); + } + + /* Decrease unused inodes count */ + uint32_t unused = + ext4_bg_get_itable_unused(bg, sb); + + uint32_t free = inodes_in_bg - unused; + + if (idx_in_bg >= free) { + unused = inodes_in_bg - (idx_in_bg + 1); + ext4_bg_set_itable_unused(bg, sb, unused); + } + + /* Save modified block group */ + bg_ref.dirty = true; + + rc = ext4_fs_put_block_group_ref(&bg_ref); + if (rc != EOK) + return rc; + + /* Update superblock */ + sb_free_inodes--; + ext4_set32(sb, free_inodes_count, sb_free_inodes); + + /* Compute the absolute i-nodex number */ + *idx = ext4_ialloc_bgidx_to_inode(sb, idx_in_bg, bgid); + + fs->last_inode_bg_id = bgid; + + return EOK; + } + + /* Block group not modified, put it and jump to the next block + * group */ + ext4_fs_put_block_group_ref(&bg_ref); + if (rc != EOK) + return rc; + + ++bgid; + } + + return ENOSPC; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_inode.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_inode.c new file mode 100644 index 0000000..13473f7 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_inode.c @@ -0,0 +1,405 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_inode.c + * @brief Inode handle functions + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_inode.h" +#include "ext4_super.h" + +/**@brief Compute number of bits for block count. + * @param block_size Filesystem block_size + * @return Number of bits + */ +static uint32_t ext4_inode_block_bits_count(uint32_t block_size) +{ + uint32_t bits = 8; + uint32_t size = block_size; + + do { + bits++; + size = size >> 1; + } while (size > 256); + + return bits; +} + +uint32_t ext4_inode_get_mode(struct ext4_sblock *sb, struct ext4_inode *inode) +{ + uint32_t v = to_le16(inode->mode); + + if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD) { + v |= ((uint32_t)to_le16(inode->osd2.hurd2.mode_high)) << 16; + } + + return v; +} + +void ext4_inode_set_mode(struct ext4_sblock *sb, struct ext4_inode *inode, + uint32_t mode) +{ + inode->mode = to_le16((mode << 16) >> 16); + + if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_HURD) + inode->osd2.hurd2.mode_high = to_le16(mode >> 16); +} + +uint32_t ext4_inode_get_uid(struct ext4_inode *inode) +{ + return to_le32(inode->uid); +} + +void ext4_inode_set_uid(struct ext4_inode *inode, uint32_t uid) +{ + inode->uid = to_le32(uid); +} + +uint64_t ext4_inode_get_size(struct ext4_sblock *sb, struct ext4_inode *inode) +{ + uint64_t v = to_le32(inode->size_lo); + + if ((ext4_get32(sb, rev_level) > 0) && + (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE))) + v |= ((uint64_t)to_le32(inode->size_hi)) << 32; + + return v; +} + +void ext4_inode_set_size(struct ext4_inode *inode, uint64_t size) +{ + inode->size_lo = to_le32((size << 32) >> 32); + inode->size_hi = to_le32(size >> 32); +} + +uint32_t ext4_inode_get_csum(struct ext4_sblock *sb, struct ext4_inode *inode) +{ + uint16_t inode_size = ext4_get16(sb, inode_size); + uint32_t v = to_le16(inode->osd2.linux2.checksum_lo); + + if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) + v |= ((uint32_t)to_le16(inode->checksum_hi)) << 16; + + return v; +} + +void ext4_inode_set_csum(struct ext4_sblock *sb, struct ext4_inode *inode, + uint32_t checksum) +{ + uint16_t inode_size = ext4_get16(sb, inode_size); + inode->osd2.linux2.checksum_lo = + to_le16((checksum << 16) >> 16); + + if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) + inode->checksum_hi = to_le16(checksum >> 16); + +} + +uint32_t ext4_inode_get_access_time(struct ext4_inode *inode) +{ + return to_le32(inode->access_time); +} +void ext4_inode_set_access_time(struct ext4_inode *inode, uint32_t time) +{ + inode->access_time = to_le32(time); +} + +uint32_t ext4_inode_get_change_inode_time(struct ext4_inode *inode) +{ + return to_le32(inode->change_inode_time); +} +void ext4_inode_set_change_inode_time(struct ext4_inode *inode, uint32_t time) +{ + inode->change_inode_time = to_le32(time); +} + +uint32_t ext4_inode_get_modif_time(struct ext4_inode *inode) +{ + return to_le32(inode->modification_time); +} + +void ext4_inode_set_modif_time(struct ext4_inode *inode, uint32_t time) +{ + inode->modification_time = to_le32(time); +} + +uint32_t ext4_inode_get_del_time(struct ext4_inode *inode) +{ + return to_le32(inode->deletion_time); +} + +void ext4_inode_set_del_time(struct ext4_inode *inode, uint32_t time) +{ + inode->deletion_time = to_le32(time); +} + +uint32_t ext4_inode_get_gid(struct ext4_inode *inode) +{ + return to_le32(inode->gid); +} +void ext4_inode_set_gid(struct ext4_inode *inode, uint32_t gid) +{ + inode->gid = to_le32(gid); +} + +uint16_t ext4_inode_get_links_cnt(struct ext4_inode *inode) +{ + return to_le16(inode->links_count); +} +void ext4_inode_set_links_cnt(struct ext4_inode *inode, uint16_t cnt) +{ + inode->links_count = to_le16(cnt); +} + +uint64_t ext4_inode_get_blocks_count(struct ext4_sblock *sb, + struct ext4_inode *inode) +{ + uint64_t cnt = to_le32(inode->blocks_count_lo); + + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_HUGE_FILE)) { + + /* 48-bit field */ + cnt |= (uint64_t)to_le16(inode->osd2.linux2.blocks_high) << 32; + + if (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_HUGE_FILE)) { + + uint32_t block_count = ext4_sb_get_block_size(sb); + uint32_t b = ext4_inode_block_bits_count(block_count); + return cnt << (b - 9); + } + } + + return cnt; +} + +int ext4_inode_set_blocks_count(struct ext4_sblock *sb, + struct ext4_inode *inode, uint64_t count) +{ + /* 32-bit maximum */ + uint64_t max = 0; + max = ~max >> 32; + + if (count <= max) { + inode->blocks_count_lo = to_le32((uint32_t)count); + inode->osd2.linux2.blocks_high = 0; + ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE); + + return EOK; + } + + /* Check if there can be used huge files (many blocks) */ + if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_HUGE_FILE)) + return EINVAL; + + /* 48-bit maximum */ + max = 0; + max = ~max >> 16; + + if (count <= max) { + inode->blocks_count_lo = to_le32((uint32_t)count); + inode->osd2.linux2.blocks_high = to_le16((uint16_t)(count >> 32)); + ext4_inode_clear_flag(inode, EXT4_INODE_FLAG_HUGE_FILE); + } else { + uint32_t block_count = ext4_sb_get_block_size(sb); + uint32_t block_bits =ext4_inode_block_bits_count(block_count); + + ext4_inode_set_flag(inode, EXT4_INODE_FLAG_HUGE_FILE); + count = count >> (block_bits - 9); + inode->blocks_count_lo = to_le32((uint32_t)count); + inode->osd2.linux2.blocks_high = to_le16((uint16_t)(count >> 32)); + } + + return EOK; +} + +uint32_t ext4_inode_get_flags(struct ext4_inode *inode) +{ + return to_le32(inode->flags); +} +void ext4_inode_set_flags(struct ext4_inode *inode, uint32_t flags) +{ + inode->flags = to_le32(flags); +} + +uint32_t ext4_inode_get_generation(struct ext4_inode *inode) +{ + return to_le32(inode->generation); +} +void ext4_inode_set_generation(struct ext4_inode *inode, uint32_t gen) +{ + inode->generation = to_le32(gen); +} + +uint16_t ext4_inode_get_extra_isize(struct ext4_sblock *sb, + struct ext4_inode *inode) +{ + uint16_t inode_size = ext4_get16(sb, inode_size); + if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) + return to_le16(inode->extra_isize); + else + return 0; +} + +void ext4_inode_set_extra_isize(struct ext4_sblock *sb, + struct ext4_inode *inode, + uint16_t size) +{ + uint16_t inode_size = ext4_get16(sb, inode_size); + if (inode_size > EXT4_GOOD_OLD_INODE_SIZE) + inode->extra_isize = to_le16(size); +} + +uint64_t ext4_inode_get_file_acl(struct ext4_inode *inode, + struct ext4_sblock *sb) +{ + uint64_t v = to_le32(inode->file_acl_lo); + + if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX) + v |= (uint32_t)to_le16(inode->osd2.linux2.file_acl_high) << 16; + + return v; +} + +void ext4_inode_set_file_acl(struct ext4_inode *inode, struct ext4_sblock *sb, + uint64_t acl) +{ + inode->file_acl_lo = to_le32((acl << 32) >> 32); + + if (ext4_get32(sb, creator_os) == EXT4_SUPERBLOCK_OS_LINUX) + inode->osd2.linux2.file_acl_high = to_le16((uint16_t)(acl >> 32)); +} + +uint32_t ext4_inode_get_direct_block(struct ext4_inode *inode, uint32_t idx) +{ + return to_le32(inode->blocks[idx]); +} +void ext4_inode_set_direct_block(struct ext4_inode *inode, uint32_t idx, + uint32_t block) +{ + inode->blocks[idx] = to_le32(block); +} + +uint32_t ext4_inode_get_indirect_block(struct ext4_inode *inode, uint32_t idx) +{ + return to_le32(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]); +} + +void ext4_inode_set_indirect_block(struct ext4_inode *inode, uint32_t idx, + uint32_t block) +{ + inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK] = to_le32(block); +} + +uint32_t ext4_inode_get_dev(struct ext4_inode *inode) +{ + uint32_t dev_0, dev_1; + dev_0 = ext4_inode_get_direct_block(inode, 0); + dev_1 = ext4_inode_get_direct_block(inode, 1); + + if (dev_0) + return dev_0; + else + return dev_1; +} + +void ext4_inode_set_dev(struct ext4_inode *inode, uint32_t dev) +{ + if (dev & ~0xFFFF) + ext4_inode_set_direct_block(inode, 1, dev); + else + ext4_inode_set_direct_block(inode, 0, dev); +} + +uint32_t ext4_inode_type(struct ext4_sblock *sb, struct ext4_inode *inode) +{ + return (ext4_inode_get_mode(sb, inode) & EXT4_INODE_MODE_TYPE_MASK); +} + +bool ext4_inode_is_type(struct ext4_sblock *sb, struct ext4_inode *inode, + uint32_t type) +{ + return ext4_inode_type(sb, inode) == type; +} + +bool ext4_inode_has_flag(struct ext4_inode *inode, uint32_t f) +{ + return ext4_inode_get_flags(inode) & f; +} + +void ext4_inode_clear_flag(struct ext4_inode *inode, uint32_t f) +{ + uint32_t flags = ext4_inode_get_flags(inode); + flags = flags & (~f); + ext4_inode_set_flags(inode, flags); +} + +void ext4_inode_set_flag(struct ext4_inode *inode, uint32_t f) +{ + uint32_t flags = ext4_inode_get_flags(inode); + flags = flags | f; + ext4_inode_set_flags(inode, flags); +} + +bool ext4_inode_can_truncate(struct ext4_sblock *sb, struct ext4_inode *inode) +{ + if ((ext4_inode_has_flag(inode, EXT4_INODE_FLAG_APPEND)) || + (ext4_inode_has_flag(inode, EXT4_INODE_FLAG_IMMUTABLE))) + return false; + + if ((ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) || + (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_DIRECTORY)) || + (ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_SOFTLINK))) + return true; + + return false; +} + +struct ext4_extent_header * +ext4_inode_get_extent_header(struct ext4_inode *inode) +{ + return (struct ext4_extent_header *)inode->blocks; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_journal.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_journal.c new file mode 100644 index 0000000..80888d1 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_journal.c @@ -0,0 +1,2292 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_journal.c + * @brief Journal handle functions + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_fs.h" +#include "ext4_super.h" +#include "ext4_journal.h" +#include "ext4_blockdev.h" +#include "ext4_crc32.h" +#include "ext4_journal.h" + +#include +#include + +/**@brief Revoke entry during journal replay.*/ +struct revoke_entry { + /**@brief Block number not to be replayed.*/ + ext4_fsblk_t block; + + /**@brief For any transaction id smaller + * than trans_id, records of @block + * in those transactions should not + * be replayed.*/ + uint32_t trans_id; + + /**@brief Revoke tree node.*/ + RB_ENTRY(revoke_entry) revoke_node; +}; + +/**@brief Valid journal replay information.*/ +struct recover_info { + /**@brief Starting transaction id.*/ + uint32_t start_trans_id; + + /**@brief Ending transaction id.*/ + uint32_t last_trans_id; + + /**@brief Used as internal argument.*/ + uint32_t this_trans_id; + + /**@brief No of transactions went through.*/ + uint32_t trans_cnt; + + /**@brief RB-Tree storing revoke entries.*/ + RB_HEAD(jbd_revoke, revoke_entry) revoke_root; +}; + +/**@brief Journal replay internal arguments.*/ +struct replay_arg { + /**@brief Journal replay information.*/ + struct recover_info *info; + + /**@brief Current block we are on.*/ + uint32_t *this_block; + + /**@brief Current trans_id we are on.*/ + uint32_t this_trans_id; +}; + +/* Make sure we wrap around the log correctly! */ +#define wrap(sb, var) \ +do { \ + if (var >= jbd_get32((sb), maxlen)) \ + var -= (jbd_get32((sb), maxlen) - jbd_get32((sb), first)); \ +} while (0) + +static inline int32_t +trans_id_diff(uint32_t x, uint32_t y) +{ + int32_t diff = x - y; + return diff; +} + +static int +jbd_revoke_entry_cmp(struct revoke_entry *a, struct revoke_entry *b) +{ + if (a->block > b->block) + return 1; + else if (a->block < b->block) + return -1; + return 0; +} + +static int +jbd_block_rec_cmp(struct jbd_block_rec *a, struct jbd_block_rec *b) +{ + if (a->lba > b->lba) + return 1; + else if (a->lba < b->lba) + return -1; + return 0; +} + +static int +jbd_revoke_rec_cmp(struct jbd_revoke_rec *a, struct jbd_revoke_rec *b) +{ + if (a->lba > b->lba) + return 1; + else if (a->lba < b->lba) + return -1; + return 0; +} + +RB_GENERATE_INTERNAL(jbd_revoke, revoke_entry, revoke_node, + jbd_revoke_entry_cmp, static inline) +RB_GENERATE_INTERNAL(jbd_block, jbd_block_rec, block_rec_node, + jbd_block_rec_cmp, static inline) +RB_GENERATE_INTERNAL(jbd_revoke_tree, jbd_revoke_rec, revoke_node, + jbd_revoke_rec_cmp, static inline) + +#define jbd_alloc_revoke_entry() ext4_calloc(1, sizeof(struct revoke_entry)) +#define jbd_free_revoke_entry(addr) ext4_free(addr) + +static int jbd_has_csum(struct jbd_sb *jbd_sb) +{ + if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V2)) + return 2; + + if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V3)) + return 3; + + return 0; +} + +#if CONFIG_META_CSUM_ENABLE +static uint32_t jbd_sb_csum(struct jbd_sb *jbd_sb) +{ + uint32_t checksum = 0; + + if (jbd_has_csum(jbd_sb)) { + uint32_t orig_checksum = jbd_sb->checksum; + jbd_set32(jbd_sb, checksum, 0); + /* Calculate crc32c checksum against tho whole superblock */ + checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_sb, + JBD_SUPERBLOCK_SIZE); + jbd_sb->checksum = orig_checksum; + } + return checksum; +} +#else +#define jbd_sb_csum(...) 0 +#endif + +static void jbd_sb_csum_set(struct jbd_sb *jbd_sb) +{ + if (!jbd_has_csum(jbd_sb)) + return; + + jbd_set32(jbd_sb, checksum, jbd_sb_csum(jbd_sb)); +} + +#if CONFIG_META_CSUM_ENABLE +static bool +jbd_verify_sb_csum(struct jbd_sb *jbd_sb) +{ + if (!jbd_has_csum(jbd_sb)) + return true; + + return jbd_sb_csum(jbd_sb) == jbd_get32(jbd_sb, checksum); +} +#else +#define jbd_verify_sb_csum(...) true +#endif + +#if CONFIG_META_CSUM_ENABLE +static uint32_t jbd_meta_csum(struct jbd_fs *jbd_fs, + struct jbd_bhdr *bhdr) +{ + uint32_t checksum = 0; + + if (jbd_has_csum(&jbd_fs->sb)) { + uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize); + struct jbd_block_tail *tail = + (struct jbd_block_tail *)((char *)bhdr + block_size - + sizeof(struct jbd_block_tail)); + uint32_t orig_checksum = tail->checksum; + tail->checksum = 0; + + /* First calculate crc32c checksum against fs uuid */ + checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid, + sizeof(jbd_fs->sb.uuid)); + /* Calculate crc32c checksum against tho whole block */ + checksum = ext4_crc32c(checksum, bhdr, + block_size); + tail->checksum = orig_checksum; + } + return checksum; +} +#else +#define jbd_meta_csum(...) 0 +#endif + +static void jbd_meta_csum_set(struct jbd_fs *jbd_fs, + struct jbd_bhdr *bhdr) +{ + uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize); + struct jbd_block_tail *tail = (struct jbd_block_tail *) + ((char *)bhdr + block_size - + sizeof(struct jbd_block_tail)); + if (!jbd_has_csum(&jbd_fs->sb)) + return; + + tail->checksum = to_be32(jbd_meta_csum(jbd_fs, bhdr)); +} + +#if CONFIG_META_CSUM_ENABLE +static bool +jbd_verify_meta_csum(struct jbd_fs *jbd_fs, + struct jbd_bhdr *bhdr) +{ + uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize); + struct jbd_block_tail *tail = (struct jbd_block_tail *) + ((char *)bhdr + block_size - + sizeof(struct jbd_block_tail)); + if (!jbd_has_csum(&jbd_fs->sb)) + return true; + + return jbd_meta_csum(jbd_fs, bhdr) == to_be32(tail->checksum); +} +#else +#define jbd_verify_meta_csum(...) true +#endif + +#if CONFIG_META_CSUM_ENABLE +static uint32_t jbd_commit_csum(struct jbd_fs *jbd_fs, + struct jbd_commit_header *header) +{ + uint32_t checksum = 0; + + if (jbd_has_csum(&jbd_fs->sb)) { + uint8_t orig_checksum_type = header->chksum_type, + orig_checksum_size = header->chksum_size; + uint32_t orig_checksum = header->chksum[0]; + uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize); + header->chksum_type = 0; + header->chksum_size = 0; + header->chksum[0] = 0; + + /* First calculate crc32c checksum against fs uuid */ + checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid, + sizeof(jbd_fs->sb.uuid)); + /* Calculate crc32c checksum against tho whole block */ + checksum = ext4_crc32c(checksum, header, + block_size); + + header->chksum_type = orig_checksum_type; + header->chksum_size = orig_checksum_size; + header->chksum[0] = orig_checksum; + } + return checksum; +} +#else +#define jbd_commit_csum(...) 0 +#endif + +static void jbd_commit_csum_set(struct jbd_fs *jbd_fs, + struct jbd_commit_header *header) +{ + if (!jbd_has_csum(&jbd_fs->sb)) + return; + + header->chksum_type = 0; + header->chksum_size = 0; + header->chksum[0] = jbd_commit_csum(jbd_fs, header); +} + +#if CONFIG_META_CSUM_ENABLE +static bool jbd_verify_commit_csum(struct jbd_fs *jbd_fs, + struct jbd_commit_header *header) +{ + if (!jbd_has_csum(&jbd_fs->sb)) + return true; + + return header->chksum[0] == to_be32(jbd_commit_csum(jbd_fs, + header)); +} +#else +#define jbd_verify_commit_csum(...) true +#endif + +#if CONFIG_META_CSUM_ENABLE +/* + * NOTE: We only make use of @csum parameter when + * JBD_FEATURE_COMPAT_CHECKSUM is enabled. + */ +static uint32_t jbd_block_csum(struct jbd_fs *jbd_fs, const void *buf, + uint32_t csum, + uint32_t sequence) +{ + uint32_t checksum = 0; + + if (jbd_has_csum(&jbd_fs->sb)) { + uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize); + /* First calculate crc32c checksum against fs uuid */ + checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid, + sizeof(jbd_fs->sb.uuid)); + /* Then calculate crc32c checksum against sequence no. */ + checksum = ext4_crc32c(checksum, &sequence, + sizeof(uint32_t)); + /* Calculate crc32c checksum against tho whole block */ + checksum = ext4_crc32c(checksum, buf, + block_size); + } else if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_COMPAT_CHECKSUM)) { + uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize); + /* Calculate crc32c checksum against tho whole block */ + checksum = ext4_crc32(csum, buf, + block_size); + } + return checksum; +} +#else +#define jbd_block_csum(...) 0 +#endif + +static void jbd_block_tag_csum_set(struct jbd_fs *jbd_fs, void *__tag, + uint32_t checksum) +{ + int ver = jbd_has_csum(&jbd_fs->sb); + if (!ver) + return; + + if (ver == 2) { + struct jbd_block_tag *tag = __tag; + tag->checksum = (uint16_t)to_be32(checksum); + } else { + struct jbd_block_tag3 *tag = __tag; + tag->checksum = to_be32(checksum); + } +} + +/**@brief Write jbd superblock to disk. + * @param jbd_fs jbd filesystem + * @param s jbd superblock + * @return standard error code*/ +static int jbd_sb_write(struct jbd_fs *jbd_fs, struct jbd_sb *s) +{ + int rc; + struct ext4_fs *fs = jbd_fs->inode_ref.fs; + uint64_t offset; + ext4_fsblk_t fblock; + rc = jbd_inode_bmap(jbd_fs, 0, &fblock); + if (rc != EOK) + return rc; + + jbd_sb_csum_set(s); + offset = fblock * ext4_sb_get_block_size(&fs->sb); + return ext4_block_writebytes(fs->bdev, offset, s, + EXT4_SUPERBLOCK_SIZE); +} + +/**@brief Read jbd superblock from disk. + * @param jbd_fs jbd filesystem + * @param s jbd superblock + * @return standard error code*/ +static int jbd_sb_read(struct jbd_fs *jbd_fs, struct jbd_sb *s) +{ + int rc; + struct ext4_fs *fs = jbd_fs->inode_ref.fs; + uint64_t offset; + ext4_fsblk_t fblock; + rc = jbd_inode_bmap(jbd_fs, 0, &fblock); + if (rc != EOK) + return rc; + + offset = fblock * ext4_sb_get_block_size(&fs->sb); + return ext4_block_readbytes(fs->bdev, offset, s, + EXT4_SUPERBLOCK_SIZE); +} + +/**@brief Verify jbd superblock. + * @param sb jbd superblock + * @return true if jbd superblock is valid */ +static bool jbd_verify_sb(struct jbd_sb *sb) +{ + struct jbd_bhdr *header = &sb->header; + if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER) + return false; + + if (jbd_get32(header, blocktype) != JBD_SUPERBLOCK && + jbd_get32(header, blocktype) != JBD_SUPERBLOCK_V2) + return false; + + return jbd_verify_sb_csum(sb); +} + +/**@brief Write back dirty jbd superblock to disk. + * @param jbd_fs jbd filesystem + * @return standard error code*/ +static int jbd_write_sb(struct jbd_fs *jbd_fs) +{ + int rc = EOK; + if (jbd_fs->dirty) { + rc = jbd_sb_write(jbd_fs, &jbd_fs->sb); + if (rc != EOK) + return rc; + + jbd_fs->dirty = false; + } + return rc; +} + +/**@brief Get reference to jbd filesystem. + * @param fs Filesystem to load journal of + * @param jbd_fs jbd filesystem + * @return standard error code*/ +int jbd_get_fs(struct ext4_fs *fs, + struct jbd_fs *jbd_fs) +{ + int rc; + uint32_t journal_ino; + + memset(jbd_fs, 0, sizeof(struct jbd_fs)); + /* See if there is journal inode on this filesystem.*/ + /* FIXME: detection on existance ofbkejournal bdev is + * missing.*/ + journal_ino = ext4_get32(&fs->sb, journal_inode_number); + + rc = ext4_fs_get_inode_ref(fs, + journal_ino, + &jbd_fs->inode_ref); + if (rc != EOK) + return rc; + + rc = jbd_sb_read(jbd_fs, &jbd_fs->sb); + if (rc != EOK) + goto Error; + + if (!jbd_verify_sb(&jbd_fs->sb)) { + rc = EIO; + goto Error; + } + + if (rc == EOK) + jbd_fs->bdev = fs->bdev; + + return rc; +Error: + ext4_fs_put_inode_ref(&jbd_fs->inode_ref); + memset(jbd_fs, 0, sizeof(struct jbd_fs)); + + return rc; +} + +/**@brief Put reference of jbd filesystem. + * @param jbd_fs jbd filesystem + * @return standard error code*/ +int jbd_put_fs(struct jbd_fs *jbd_fs) +{ + int rc = EOK; + rc = jbd_write_sb(jbd_fs); + + ext4_fs_put_inode_ref(&jbd_fs->inode_ref); + return rc; +} + +/**@brief Data block lookup helper. + * @param jbd_fs jbd filesystem + * @param iblock block index + * @param fblock logical block address + * @return standard error code*/ +int jbd_inode_bmap(struct jbd_fs *jbd_fs, + ext4_lblk_t iblock, + ext4_fsblk_t *fblock) +{ + int rc = ext4_fs_get_inode_dblk_idx( + &jbd_fs->inode_ref, + iblock, + fblock, + false); + return rc; +} + +/**@brief jbd block get function (through cache). + * @param jbd_fs jbd filesystem + * @param block block descriptor + * @param fblock jbd logical block address + * @return standard error code*/ +static int jbd_block_get(struct jbd_fs *jbd_fs, + struct ext4_block *block, + ext4_fsblk_t fblock) +{ + /* TODO: journal device. */ + int rc; + struct ext4_blockdev *bdev = jbd_fs->bdev; + ext4_lblk_t iblock = (ext4_lblk_t)fblock; + + /* Lookup the logical block address of + * fblock.*/ + rc = jbd_inode_bmap(jbd_fs, iblock, + &fblock); + if (rc != EOK) + return rc; + + rc = ext4_block_get(bdev, block, fblock); + + /* If succeeded, mark buffer as BC_FLUSH to indicate + * that data should be written to disk immediately.*/ + if (rc == EOK) { + ext4_bcache_set_flag(block->buf, BC_FLUSH); + /* As we don't want to occupy too much space + * in block cache, we set this buffer BC_TMP.*/ + ext4_bcache_set_flag(block->buf, BC_TMP); + } + + return rc; +} + +/**@brief jbd block get function (through cache, don't read). + * @param jbd_fs jbd filesystem + * @param block block descriptor + * @param fblock jbd logical block address + * @return standard error code*/ +static int jbd_block_get_noread(struct jbd_fs *jbd_fs, + struct ext4_block *block, + ext4_fsblk_t fblock) +{ + /* TODO: journal device. */ + int rc; + struct ext4_blockdev *bdev = jbd_fs->bdev; + ext4_lblk_t iblock = (ext4_lblk_t)fblock; + rc = jbd_inode_bmap(jbd_fs, iblock, + &fblock); + if (rc != EOK) + return rc; + + rc = ext4_block_get_noread(bdev, block, fblock); + if (rc == EOK) + ext4_bcache_set_flag(block->buf, BC_FLUSH); + + return rc; +} + +/**@brief jbd block set procedure (through cache). + * @param jbd_fs jbd filesystem + * @param block block descriptor + * @return standard error code*/ +static int jbd_block_set(struct jbd_fs *jbd_fs, + struct ext4_block *block) +{ + struct ext4_blockdev *bdev = jbd_fs->bdev; + return ext4_block_set(bdev, block); +} + +/**@brief helper functions to calculate + * block tag size, not including UUID part. + * @param jbd_fs jbd filesystem + * @return tag size in bytes*/ +static int jbd_tag_bytes(struct jbd_fs *jbd_fs) +{ + int size; + + /* It is very easy to deal with the case which + * JBD_FEATURE_INCOMPAT_CSUM_V3 is enabled.*/ + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_CSUM_V3)) + return sizeof(struct jbd_block_tag3); + + size = sizeof(struct jbd_block_tag); + + /* If JBD_FEATURE_INCOMPAT_CSUM_V2 is enabled, + * add 2 bytes to size.*/ + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_CSUM_V2)) + size += sizeof(uint16_t); + + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_64BIT)) + return size; + + /* If block number is 4 bytes in size, + * minus 4 bytes from size */ + return size - sizeof(uint32_t); +} + +/**@brief Tag information. */ +struct tag_info { + /**@brief Tag size in bytes, including UUID part.*/ + int tag_bytes; + + /**@brief block number stored in this tag.*/ + ext4_fsblk_t block; + + /**@brief Is the first 4 bytes of block equals to + * JBD_MAGIC_NUMBER? */ + bool is_escape; + + /**@brief whether UUID part exists or not.*/ + bool uuid_exist; + + /**@brief UUID content if UUID part exists.*/ + uint8_t uuid[UUID_SIZE]; + + /**@brief Is this the last tag? */ + bool last_tag; + + /**@brief crc32c checksum. */ + uint32_t checksum; +}; + +/**@brief Extract information from a block tag. + * @param __tag pointer to the block tag + * @param tag_bytes block tag size of this jbd filesystem + * @param remaining size in buffer containing the block tag + * @param tag_info information of this tag. + * @return EOK when succeed, otherwise return EINVAL.*/ +static int +jbd_extract_block_tag(struct jbd_fs *jbd_fs, + void *__tag, + int tag_bytes, + int32_t remain_buf_size, + struct tag_info *tag_info) +{ + char *uuid_start; + tag_info->tag_bytes = tag_bytes; + tag_info->uuid_exist = false; + tag_info->last_tag = false; + tag_info->is_escape = false; + + /* See whether it is possible to hold a valid block tag.*/ + if (remain_buf_size - tag_bytes < 0) + return EINVAL; + + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_CSUM_V3)) { + struct jbd_block_tag3 *tag = __tag; + tag_info->block = jbd_get32(tag, blocknr); + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_64BIT)) + tag_info->block |= + (uint64_t)jbd_get32(tag, blocknr_high) << 32; + + if (jbd_get32(tag, flags) & JBD_FLAG_ESCAPE) + tag_info->is_escape = true; + + if (!(jbd_get32(tag, flags) & JBD_FLAG_SAME_UUID)) { + /* See whether it is possible to hold UUID part.*/ + if (remain_buf_size - tag_bytes < UUID_SIZE) + return EINVAL; + + uuid_start = (char *)tag + tag_bytes; + tag_info->uuid_exist = true; + tag_info->tag_bytes += UUID_SIZE; + memcpy(tag_info->uuid, uuid_start, UUID_SIZE); + } + + if (jbd_get32(tag, flags) & JBD_FLAG_LAST_TAG) + tag_info->last_tag = true; + + } else { + struct jbd_block_tag *tag = __tag; + tag_info->block = jbd_get32(tag, blocknr); + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_64BIT)) + tag_info->block |= + (uint64_t)jbd_get32(tag, blocknr_high) << 32; + + if (jbd_get16(tag, flags) & JBD_FLAG_ESCAPE) + tag_info->is_escape = true; + + if (!(jbd_get16(tag, flags) & JBD_FLAG_SAME_UUID)) { + /* See whether it is possible to hold UUID part.*/ + if (remain_buf_size - tag_bytes < UUID_SIZE) + return EINVAL; + + uuid_start = (char *)tag + tag_bytes; + tag_info->uuid_exist = true; + tag_info->tag_bytes += UUID_SIZE; + memcpy(tag_info->uuid, uuid_start, UUID_SIZE); + } + + if (jbd_get16(tag, flags) & JBD_FLAG_LAST_TAG) + tag_info->last_tag = true; + + } + return EOK; +} + +/**@brief Write information to a block tag. + * @param __tag pointer to the block tag + * @param remaining size in buffer containing the block tag + * @param tag_info information of this tag. + * @return EOK when succeed, otherwise return EINVAL.*/ +static int +jbd_write_block_tag(struct jbd_fs *jbd_fs, + void *__tag, + int32_t remain_buf_size, + struct tag_info *tag_info) +{ + char *uuid_start; + int tag_bytes = jbd_tag_bytes(jbd_fs); + + tag_info->tag_bytes = tag_bytes; + + /* See whether it is possible to hold a valid block tag.*/ + if (remain_buf_size - tag_bytes < 0) + return EINVAL; + + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_CSUM_V3)) { + struct jbd_block_tag3 *tag = __tag; + memset(tag, 0, sizeof(struct jbd_block_tag3)); + jbd_set32(tag, blocknr, (uint32_t)tag_info->block); + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_64BIT)) + jbd_set32(tag, blocknr_high, tag_info->block >> 32); + + if (tag_info->uuid_exist) { + /* See whether it is possible to hold UUID part.*/ + if (remain_buf_size - tag_bytes < UUID_SIZE) + return EINVAL; + + uuid_start = (char *)tag + tag_bytes; + tag_info->tag_bytes += UUID_SIZE; + memcpy(uuid_start, tag_info->uuid, UUID_SIZE); + } else + jbd_set32(tag, flags, + jbd_get32(tag, flags) | JBD_FLAG_SAME_UUID); + + jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum); + + if (tag_info->last_tag) + jbd_set32(tag, flags, + jbd_get32(tag, flags) | JBD_FLAG_LAST_TAG); + + if (tag_info->is_escape) + jbd_set32(tag, flags, + jbd_get32(tag, flags) | JBD_FLAG_ESCAPE); + + } else { + struct jbd_block_tag *tag = __tag; + memset(tag, 0, sizeof(struct jbd_block_tag)); + jbd_set32(tag, blocknr, (uint32_t)tag_info->block); + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_64BIT)) + jbd_set32(tag, blocknr_high, tag_info->block >> 32); + + if (tag_info->uuid_exist) { + /* See whether it is possible to hold UUID part.*/ + if (remain_buf_size - tag_bytes < UUID_SIZE) + return EINVAL; + + uuid_start = (char *)tag + tag_bytes; + tag_info->tag_bytes += UUID_SIZE; + memcpy(uuid_start, tag_info->uuid, UUID_SIZE); + } else + jbd_set16(tag, flags, + jbd_get16(tag, flags) | JBD_FLAG_SAME_UUID); + + jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum); + + if (tag_info->last_tag) + jbd_set16(tag, flags, + jbd_get16(tag, flags) | JBD_FLAG_LAST_TAG); + + + if (tag_info->is_escape) + jbd_set16(tag, flags, + jbd_get16(tag, flags) | JBD_FLAG_ESCAPE); + + } + return EOK; +} + +/**@brief Iterate all block tags in a block. + * @param jbd_fs jbd filesystem + * @param __tag_start pointer to the block + * @param tag_tbl_size size of the block + * @param func callback routine to indicate that + * a block tag is found + * @param arg additional argument to be passed to func */ +static void +jbd_iterate_block_table(struct jbd_fs *jbd_fs, + void *__tag_start, + int32_t tag_tbl_size, + void (*func)(struct jbd_fs * jbd_fs, + struct tag_info *tag_info, + void *arg), + void *arg) +{ + char *tag_start, *tag_ptr; + int tag_bytes = jbd_tag_bytes(jbd_fs); + tag_start = __tag_start; + tag_ptr = tag_start; + + /* Cut off the size of block tail storing checksum. */ + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_CSUM_V2) || + JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_CSUM_V3)) + tag_tbl_size -= sizeof(struct jbd_block_tail); + + while (tag_tbl_size) { + struct tag_info tag_info; + int rc = jbd_extract_block_tag(jbd_fs, + tag_ptr, + tag_bytes, + tag_tbl_size, + &tag_info); + if (rc != EOK) + break; + + if (func) + func(jbd_fs, &tag_info, arg); + + /* Stop the iteration when we reach the last tag. */ + if (tag_info.last_tag) + break; + + tag_ptr += tag_info.tag_bytes; + tag_tbl_size -= tag_info.tag_bytes; + } +} + +static void jbd_display_block_tags(struct jbd_fs *jbd_fs, + struct tag_info *tag_info, + void *arg) +{ + uint32_t *iblock = arg; + ext4_dbg(DEBUG_JBD, "Block in block_tag: %" PRIu64 "\n", tag_info->block); + (*iblock)++; + wrap(&jbd_fs->sb, *iblock); + (void)jbd_fs; + return; +} + +static struct revoke_entry * +jbd_revoke_entry_lookup(struct recover_info *info, ext4_fsblk_t block) +{ + struct revoke_entry tmp = { + .block = block + }; + + return RB_FIND(jbd_revoke, &info->revoke_root, &tmp); +} + +/**@brief Replay a block in a transaction. + * @param jbd_fs jbd filesystem + * @param tag_info tag_info of the logged block.*/ +static void jbd_replay_block_tags(struct jbd_fs *jbd_fs, + struct tag_info *tag_info, + void *__arg) +{ + int r; + struct replay_arg *arg = __arg; + struct recover_info *info = arg->info; + uint32_t *this_block = arg->this_block; + struct revoke_entry *revoke_entry; + struct ext4_block journal_block, ext4_block; + struct ext4_fs *fs = jbd_fs->inode_ref.fs; + + (*this_block)++; + wrap(&jbd_fs->sb, *this_block); + + /* We replay this block only if the current transaction id + * is equal or greater than that in revoke entry.*/ + revoke_entry = jbd_revoke_entry_lookup(info, tag_info->block); + if (revoke_entry && + trans_id_diff(arg->this_trans_id, revoke_entry->trans_id) <= 0) + return; + + ext4_dbg(DEBUG_JBD, + "Replaying block in block_tag: %" PRIu64 "\n", + tag_info->block); + + r = jbd_block_get(jbd_fs, &journal_block, *this_block); + if (r != EOK) + return; + + /* We need special treatment for ext4 superblock. */ + if (tag_info->block) { + r = ext4_block_get_noread(fs->bdev, &ext4_block, tag_info->block); + if (r != EOK) { + jbd_block_set(jbd_fs, &journal_block); + return; + } + + memcpy(ext4_block.data, + journal_block.data, + jbd_get32(&jbd_fs->sb, blocksize)); + + if (tag_info->is_escape) + ((struct jbd_bhdr *)ext4_block.data)->magic = + to_be32(JBD_MAGIC_NUMBER); + + ext4_bcache_set_dirty(ext4_block.buf); + ext4_block_set(fs->bdev, &ext4_block); + } else { + uint16_t mount_count, state; + mount_count = ext4_get16(&fs->sb, mount_count); + state = ext4_get16(&fs->sb, state); + + memcpy(&fs->sb, + journal_block.data + EXT4_SUPERBLOCK_OFFSET, + EXT4_SUPERBLOCK_SIZE); + + /* Mark system as mounted */ + ext4_set16(&fs->sb, state, state); + r = ext4_sb_write(fs->bdev, &fs->sb); + if (r != EOK) + return; + + /*Update mount count*/ + ext4_set16(&fs->sb, mount_count, mount_count); + } + + jbd_block_set(jbd_fs, &journal_block); + + return; +} + +/**@brief Add block address to revoke tree, along with + * its transaction id. + * @param info journal replay info + * @param block block address to be replayed.*/ +static void jbd_add_revoke_block_tags(struct recover_info *info, + ext4_fsblk_t block) +{ + struct revoke_entry *revoke_entry; + + ext4_dbg(DEBUG_JBD, "Add block %" PRIu64 " to revoke tree\n", block); + /* If the revoke entry with respect to the block address + * exists already, update its transaction id.*/ + revoke_entry = jbd_revoke_entry_lookup(info, block); + if (revoke_entry) { + revoke_entry->trans_id = info->this_trans_id; + return; + } + + revoke_entry = jbd_alloc_revoke_entry(); + ext4_assert(revoke_entry); + revoke_entry->block = block; + revoke_entry->trans_id = info->this_trans_id; + RB_INSERT(jbd_revoke, &info->revoke_root, revoke_entry); + + return; +} + +static void jbd_destroy_revoke_tree(struct recover_info *info) +{ + while (!RB_EMPTY(&info->revoke_root)) { + struct revoke_entry *revoke_entry = + RB_MIN(jbd_revoke, &info->revoke_root); + ext4_assert(revoke_entry); + RB_REMOVE(jbd_revoke, &info->revoke_root, revoke_entry); + jbd_free_revoke_entry(revoke_entry); + } +} + + +#define ACTION_SCAN 0 +#define ACTION_REVOKE 1 +#define ACTION_RECOVER 2 + +/**@brief Add entries in a revoke block to revoke tree. + * @param jbd_fs jbd filesystem + * @param header revoke block header + * @param recover_info journal replay info*/ +static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs, + struct jbd_bhdr *header, + struct recover_info *info) +{ + char *blocks_entry; + struct jbd_revoke_header *revoke_hdr = + (struct jbd_revoke_header *)header; + uint32_t i, nr_entries, record_len = 4; + + /* If we are working on a 64bit jbd filesystem, */ + if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb, + JBD_FEATURE_INCOMPAT_64BIT)) + record_len = 8; + + nr_entries = (jbd_get32(revoke_hdr, count) - + sizeof(struct jbd_revoke_header)) / + record_len; + + blocks_entry = (char *)(revoke_hdr + 1); + + for (i = 0;i < nr_entries;i++) { + if (record_len == 8) { + uint64_t *blocks = + (uint64_t *)blocks_entry; + jbd_add_revoke_block_tags(info, to_be64(*blocks)); + } else { + uint32_t *blocks = + (uint32_t *)blocks_entry; + jbd_add_revoke_block_tags(info, to_be32(*blocks)); + } + blocks_entry += record_len; + } +} + +static void jbd_debug_descriptor_block(struct jbd_fs *jbd_fs, + struct jbd_bhdr *header, + uint32_t *iblock) +{ + jbd_iterate_block_table(jbd_fs, + header + 1, + jbd_get32(&jbd_fs->sb, blocksize) - + sizeof(struct jbd_bhdr), + jbd_display_block_tags, + iblock); +} + +static void jbd_replay_descriptor_block(struct jbd_fs *jbd_fs, + struct jbd_bhdr *header, + struct replay_arg *arg) +{ + jbd_iterate_block_table(jbd_fs, + header + 1, + jbd_get32(&jbd_fs->sb, blocksize) - + sizeof(struct jbd_bhdr), + jbd_replay_block_tags, + arg); +} + +/**@brief The core routine of journal replay. + * @param jbd_fs jbd filesystem + * @param recover_info journal replay info + * @param action action needed to be taken + * @return standard error code*/ +static int jbd_iterate_log(struct jbd_fs *jbd_fs, + struct recover_info *info, + int action) +{ + int r = EOK; + bool log_end = false; + struct jbd_sb *sb = &jbd_fs->sb; + uint32_t start_trans_id, this_trans_id; + uint32_t start_block, this_block; + + /* We start iterating valid blocks in the whole journal.*/ + start_trans_id = this_trans_id = jbd_get32(sb, sequence); + start_block = this_block = jbd_get32(sb, start); + if (action == ACTION_SCAN) + info->trans_cnt = 0; + else if (!info->trans_cnt) + log_end = true; + + ext4_dbg(DEBUG_JBD, "Start of journal at trans id: %" PRIu32 "\n", + start_trans_id); + + while (!log_end) { + struct ext4_block block; + struct jbd_bhdr *header; + /* If we are not scanning for the last + * valid transaction in the journal, + * we will stop when we reach the end of + * the journal.*/ + if (action != ACTION_SCAN) + if (trans_id_diff(this_trans_id, info->last_trans_id) > 0) { + log_end = true; + continue; + } + + r = jbd_block_get(jbd_fs, &block, this_block); + if (r != EOK) + break; + + header = (struct jbd_bhdr *)block.data; + /* This block does not have a valid magic number, + * so we have reached the end of the journal.*/ + if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER) { + jbd_block_set(jbd_fs, &block); + log_end = true; + continue; + } + + /* If the transaction id we found is not expected, + * we may have reached the end of the journal. + * + * If we are not scanning the journal, something + * bad might have taken place. :-( */ + if (jbd_get32(header, sequence) != this_trans_id) { + if (action != ACTION_SCAN) + r = EIO; + + jbd_block_set(jbd_fs, &block); + log_end = true; + continue; + } + + switch (jbd_get32(header, blocktype)) { + case JBD_DESCRIPTOR_BLOCK: + if (!jbd_verify_meta_csum(jbd_fs, header)) { + ext4_dbg(DEBUG_JBD, + DBG_WARN "Descriptor block checksum failed." + "Journal block: %" PRIu32"\n", + this_block); + log_end = true; + break; + } + ext4_dbg(DEBUG_JBD, "Descriptor block: %" PRIu32", " + "trans_id: %" PRIu32"\n", + this_block, this_trans_id); + if (action == ACTION_RECOVER) { + struct replay_arg replay_arg; + replay_arg.info = info; + replay_arg.this_block = &this_block; + replay_arg.this_trans_id = this_trans_id; + + jbd_replay_descriptor_block(jbd_fs, + header, &replay_arg); + } else + jbd_debug_descriptor_block(jbd_fs, + header, &this_block); + + break; + case JBD_COMMIT_BLOCK: + if (!jbd_verify_commit_csum(jbd_fs, + (struct jbd_commit_header *)header)) { + ext4_dbg(DEBUG_JBD, + DBG_WARN "Commit block checksum failed." + "Journal block: %" PRIu32"\n", + this_block); + log_end = true; + break; + } + ext4_dbg(DEBUG_JBD, "Commit block: %" PRIu32", " + "trans_id: %" PRIu32"\n", + this_block, this_trans_id); + /* + * This is the end of a transaction, + * we may now proceed to the next transaction. + */ + this_trans_id++; + if (action == ACTION_SCAN) + info->trans_cnt++; + break; + case JBD_REVOKE_BLOCK: + if (!jbd_verify_meta_csum(jbd_fs, header)) { + ext4_dbg(DEBUG_JBD, + DBG_WARN "Revoke block checksum failed." + "Journal block: %" PRIu32"\n", + this_block); + log_end = true; + break; + } + ext4_dbg(DEBUG_JBD, "Revoke block: %" PRIu32", " + "trans_id: %" PRIu32"\n", + this_block, this_trans_id); + if (action == ACTION_REVOKE) { + info->this_trans_id = this_trans_id; + jbd_build_revoke_tree(jbd_fs, + header, info); + } + break; + default: + log_end = true; + break; + } + jbd_block_set(jbd_fs, &block); + this_block++; + wrap(sb, this_block); + if (this_block == start_block) + log_end = true; + + } + ext4_dbg(DEBUG_JBD, "End of journal.\n"); + if (r == EOK && action == ACTION_SCAN) { + /* We have finished scanning the journal. */ + info->start_trans_id = start_trans_id; + if (trans_id_diff(this_trans_id, start_trans_id) > 0) + info->last_trans_id = this_trans_id - 1; + else + info->last_trans_id = this_trans_id; + } + + return r; +} + +/**@brief Replay journal. + * @param jbd_fs jbd filesystem + * @return standard error code*/ +int jbd_recover(struct jbd_fs *jbd_fs) +{ + int r; + struct recover_info info; + struct jbd_sb *sb = &jbd_fs->sb; + if (!sb->start) + return EOK; + + RB_INIT(&info.revoke_root); + + r = jbd_iterate_log(jbd_fs, &info, ACTION_SCAN); + if (r != EOK) + return r; + + r = jbd_iterate_log(jbd_fs, &info, ACTION_REVOKE); + if (r != EOK) + return r; + + r = jbd_iterate_log(jbd_fs, &info, ACTION_RECOVER); + if (r == EOK) { + /* If we successfully replay the journal, + * clear EXT4_FINCOM_RECOVER flag on the + * ext4 superblock, and set the start of + * journal to 0.*/ + uint32_t features_incompatible = + ext4_get32(&jbd_fs->inode_ref.fs->sb, + features_incompatible); + jbd_set32(&jbd_fs->sb, start, 0); + jbd_set32(&jbd_fs->sb, sequence, info.last_trans_id); + features_incompatible &= ~EXT4_FINCOM_RECOVER; + ext4_set32(&jbd_fs->inode_ref.fs->sb, + features_incompatible, + features_incompatible); + jbd_fs->dirty = true; + r = ext4_sb_write(jbd_fs->bdev, + &jbd_fs->inode_ref.fs->sb); + } + jbd_destroy_revoke_tree(&info); + return r; +} + +static void jbd_journal_write_sb(struct jbd_journal *journal) +{ + struct jbd_fs *jbd_fs = journal->jbd_fs; + jbd_set32(&jbd_fs->sb, start, journal->start); + jbd_set32(&jbd_fs->sb, sequence, journal->trans_id); + jbd_fs->dirty = true; +} + +/**@brief Start accessing the journal. + * @param jbd_fs jbd filesystem + * @param journal current journal session + * @return standard error code*/ +int jbd_journal_start(struct jbd_fs *jbd_fs, + struct jbd_journal *journal) +{ + int r; + uint32_t features_incompatible = + ext4_get32(&jbd_fs->inode_ref.fs->sb, + features_incompatible); + features_incompatible |= EXT4_FINCOM_RECOVER; + ext4_set32(&jbd_fs->inode_ref.fs->sb, + features_incompatible, + features_incompatible); + r = ext4_sb_write(jbd_fs->bdev, + &jbd_fs->inode_ref.fs->sb); + if (r != EOK) + return r; + + journal->first = jbd_get32(&jbd_fs->sb, first); + journal->start = journal->first; + journal->last = journal->first; + /* + * To invalidate any stale records we need to start from + * the checkpoint transaction ID of the previous journalling session + * plus 1. + */ + journal->trans_id = jbd_get32(&jbd_fs->sb, sequence) + 1; + journal->alloc_trans_id = journal->trans_id; + + journal->block_size = jbd_get32(&jbd_fs->sb, blocksize); + + TAILQ_INIT(&journal->cp_queue); + RB_INIT(&journal->block_rec_root); + journal->jbd_fs = jbd_fs; + jbd_journal_write_sb(journal); + r = jbd_write_sb(jbd_fs); + if (r != EOK) + return r; + + jbd_fs->bdev->journal = journal; + return EOK; +} + +static void jbd_trans_end_write(struct ext4_bcache *bc __unused, + struct ext4_buf *buf __unused, + int res, + void *arg); + +/* + * This routine is only suitable to committed transactions. */ +static void jbd_journal_flush_trans(struct jbd_trans *trans) +{ + struct jbd_buf *jbd_buf, *tmp; + struct jbd_journal *journal = trans->journal; + struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs; + void *tmp_data = ext4_malloc(journal->block_size); + ext4_assert(tmp_data); + + TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, + tmp) { + struct ext4_buf *buf; + struct ext4_block block; + /* The buffer is not yet flushed. */ + buf = ext4_bcache_find_get(fs->bdev->bc, &block, + jbd_buf->block_rec->lba); + if (!(buf && ext4_bcache_test_flag(buf, BC_UPTODATE) && + jbd_buf->block_rec->trans == trans)) { + int r; + struct ext4_block jbd_block = EXT4_BLOCK_ZERO(); + r = jbd_block_get(journal->jbd_fs, + &jbd_block, + jbd_buf->jbd_lba); + ext4_assert(r == EOK); + memcpy(tmp_data, jbd_block.data, + journal->block_size); + ext4_block_set(fs->bdev, &jbd_block); + r = ext4_blocks_set_direct(fs->bdev, tmp_data, + jbd_buf->block_rec->lba, 1); + jbd_trans_end_write(fs->bdev->bc, buf, r, jbd_buf); + } else + ext4_block_flush_buf(fs->bdev, buf); + + if (buf) + ext4_block_set(fs->bdev, &block); + } + + ext4_free(tmp_data); +} + +static void +jbd_journal_skip_pure_revoke(struct jbd_journal *journal, + struct jbd_trans *trans) +{ + journal->start = trans->start_iblock + + trans->alloc_blocks; + wrap(&journal->jbd_fs->sb, journal->start); + journal->trans_id = trans->trans_id + 1; + jbd_journal_free_trans(journal, + trans, false); + jbd_journal_write_sb(journal); +} + +void +jbd_journal_purge_cp_trans(struct jbd_journal *journal, + bool flush, + bool once) +{ + struct jbd_trans *trans; + while ((trans = TAILQ_FIRST(&journal->cp_queue))) { + if (!trans->data_cnt) { + TAILQ_REMOVE(&journal->cp_queue, + trans, + trans_node); + jbd_journal_skip_pure_revoke(journal, trans); + } else { + if (trans->data_cnt == + trans->written_cnt) { + journal->start = + trans->start_iblock + + trans->alloc_blocks; + wrap(&journal->jbd_fs->sb, + journal->start); + journal->trans_id = + trans->trans_id + 1; + TAILQ_REMOVE(&journal->cp_queue, + trans, + trans_node); + jbd_journal_free_trans(journal, + trans, + false); + jbd_journal_write_sb(journal); + } else if (!flush) { + journal->start = + trans->start_iblock; + wrap(&journal->jbd_fs->sb, + journal->start); + journal->trans_id = + trans->trans_id; + jbd_journal_write_sb(journal); + break; + } else + jbd_journal_flush_trans(trans); + } + if (once) + break; + } +} + +/**@brief Stop accessing the journal. + * @param journal current journal session + * @return standard error code*/ +int jbd_journal_stop(struct jbd_journal *journal) +{ + int r; + struct jbd_fs *jbd_fs = journal->jbd_fs; + uint32_t features_incompatible; + + /* Make sure that journalled content have reached + * the disk.*/ + jbd_journal_purge_cp_trans(journal, true, false); + + /* There should be no block record in this journal + * session. */ + if (!RB_EMPTY(&journal->block_rec_root)) + ext4_dbg(DEBUG_JBD, + DBG_WARN "There are still block records " + "in this journal session!\n"); + + features_incompatible = + ext4_get32(&jbd_fs->inode_ref.fs->sb, + features_incompatible); + features_incompatible &= ~EXT4_FINCOM_RECOVER; + ext4_set32(&jbd_fs->inode_ref.fs->sb, + features_incompatible, + features_incompatible); + r = ext4_sb_write(jbd_fs->bdev, + &jbd_fs->inode_ref.fs->sb); + if (r != EOK) + return r; + + journal->start = 0; + journal->trans_id = 0; + jbd_journal_write_sb(journal); + return jbd_write_sb(journal->jbd_fs); +} + +/**@brief Allocate a block in the journal. + * @param journal current journal session + * @param trans transaction + * @return allocated block address*/ +static uint32_t jbd_journal_alloc_block(struct jbd_journal *journal, + struct jbd_trans *trans) +{ + uint32_t start_block; + + start_block = journal->last++; + trans->alloc_blocks++; + wrap(&journal->jbd_fs->sb, journal->last); + + /* If there is no space left, flush just one journalled + * transaction.*/ + if (journal->last == journal->start) { + jbd_journal_purge_cp_trans(journal, true, true); + ext4_assert(journal->last != journal->start); + } + + return start_block; +} + +static struct jbd_block_rec * +jbd_trans_block_rec_lookup(struct jbd_journal *journal, + ext4_fsblk_t lba) +{ + struct jbd_block_rec tmp = { + .lba = lba + }; + + return RB_FIND(jbd_block, + &journal->block_rec_root, + &tmp); +} + +static void +jbd_trans_change_ownership(struct jbd_block_rec *block_rec, + struct jbd_trans *new_trans) +{ + LIST_REMOVE(block_rec, tbrec_node); + if (new_trans) { + /* Now this block record belongs to this transaction. */ + LIST_INSERT_HEAD(&new_trans->tbrec_list, block_rec, tbrec_node); + } + block_rec->trans = new_trans; +} + +static inline struct jbd_block_rec * +jbd_trans_insert_block_rec(struct jbd_trans *trans, + ext4_fsblk_t lba) +{ + struct jbd_block_rec *block_rec; + block_rec = jbd_trans_block_rec_lookup(trans->journal, lba); + if (block_rec) { + jbd_trans_change_ownership(block_rec, trans); + return block_rec; + } + block_rec = ext4_calloc(1, sizeof(struct jbd_block_rec)); + if (!block_rec) + return NULL; + + block_rec->lba = lba; + block_rec->trans = trans; + TAILQ_INIT(&block_rec->dirty_buf_queue); + LIST_INSERT_HEAD(&trans->tbrec_list, block_rec, tbrec_node); + RB_INSERT(jbd_block, &trans->journal->block_rec_root, block_rec); + return block_rec; +} + +/* + * This routine will do the dirty works. + */ +static void +jbd_trans_finish_callback(struct jbd_journal *journal, + const struct jbd_trans *trans, + struct jbd_block_rec *block_rec, + bool abort, + bool revoke) +{ + struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs; + if (block_rec->trans != trans) + return; + + if (!abort) { + struct jbd_buf *jbd_buf, *tmp; + TAILQ_FOREACH_SAFE(jbd_buf, + &block_rec->dirty_buf_queue, + dirty_buf_node, + tmp) { + jbd_trans_end_write(fs->bdev->bc, + NULL, + EOK, + jbd_buf); + } + } else { + /* + * We have to roll back data if the block is going to be + * aborted. + */ + struct jbd_buf *jbd_buf; + struct ext4_block jbd_block = EXT4_BLOCK_ZERO(), + block = EXT4_BLOCK_ZERO(); + jbd_buf = TAILQ_LAST(&block_rec->dirty_buf_queue, + jbd_buf_dirty); + if (jbd_buf) { + if (!revoke) { + int r; + r = ext4_block_get_noread(fs->bdev, + &block, + block_rec->lba); + ext4_assert(r == EOK); + r = jbd_block_get(journal->jbd_fs, + &jbd_block, + jbd_buf->jbd_lba); + ext4_assert(r == EOK); + memcpy(block.data, jbd_block.data, + journal->block_size); + + jbd_trans_change_ownership(block_rec, + jbd_buf->trans); + + block.buf->end_write = jbd_trans_end_write; + block.buf->end_write_arg = jbd_buf; + + ext4_bcache_set_flag(jbd_block.buf, BC_TMP); + ext4_bcache_set_dirty(block.buf); + + ext4_block_set(fs->bdev, &jbd_block); + ext4_block_set(fs->bdev, &block); + return; + } else { + /* The revoked buffer is yet written. */ + jbd_trans_change_ownership(block_rec, + jbd_buf->trans); + } + } + } +} + +static inline void +jbd_trans_remove_block_rec(struct jbd_journal *journal, + struct jbd_block_rec *block_rec, + struct jbd_trans *trans) +{ + /* If this block record doesn't belong to this transaction, + * give up.*/ + if (block_rec->trans == trans) { + LIST_REMOVE(block_rec, tbrec_node); + RB_REMOVE(jbd_block, + &journal->block_rec_root, + block_rec); + ext4_free(block_rec); + } +} + +/**@brief Add block to a transaction and mark it dirty. + * @param trans transaction + * @param block block descriptor + * @return standard error code*/ +int jbd_trans_set_block_dirty(struct jbd_trans *trans, + struct ext4_block *block) +{ + struct jbd_buf *jbd_buf; + struct jbd_revoke_rec *rec, tmp_rec = { + .lba = block->lb_id + }; + struct jbd_block_rec *block_rec; + + if (block->buf->end_write == jbd_trans_end_write) { + jbd_buf = block->buf->end_write_arg; + if (jbd_buf && jbd_buf->trans == trans) + return EOK; + } + jbd_buf = ext4_calloc(1, sizeof(struct jbd_buf)); + if (!jbd_buf) + return ENOMEM; + + if ((block_rec = jbd_trans_insert_block_rec(trans, + block->lb_id)) == NULL) { + ext4_free(jbd_buf); + return ENOMEM; + } + + TAILQ_INSERT_TAIL(&block_rec->dirty_buf_queue, + jbd_buf, + dirty_buf_node); + + jbd_buf->block_rec = block_rec; + jbd_buf->trans = trans; + jbd_buf->block = *block; + ext4_bcache_inc_ref(block->buf); + + /* If the content reach the disk, notify us + * so that we may do a checkpoint. */ + block->buf->end_write = jbd_trans_end_write; + block->buf->end_write_arg = jbd_buf; + + trans->data_cnt++; + TAILQ_INSERT_HEAD(&trans->buf_queue, jbd_buf, buf_node); + + ext4_bcache_set_dirty(block->buf); + rec = RB_FIND(jbd_revoke_tree, + &trans->revoke_root, + &tmp_rec); + if (rec) { + RB_REMOVE(jbd_revoke_tree, &trans->revoke_root, + rec); + ext4_free(rec); + } + + return EOK; +} + +/**@brief Add block to be revoked to a transaction + * @param trans transaction + * @param lba logical block address + * @return standard error code*/ +int jbd_trans_revoke_block(struct jbd_trans *trans, + ext4_fsblk_t lba) +{ + struct jbd_revoke_rec tmp_rec = { + .lba = lba + }, *rec; + rec = RB_FIND(jbd_revoke_tree, + &trans->revoke_root, + &tmp_rec); + if (rec) + return EOK; + + rec = ext4_calloc(1, sizeof(struct jbd_revoke_rec)); + if (!rec) + return ENOMEM; + + rec->lba = lba; + RB_INSERT(jbd_revoke_tree, &trans->revoke_root, rec); + return EOK; +} + +/**@brief Try to add block to be revoked to a transaction. + * If @lba still remains in an transaction on checkpoint + * queue, add @lba as a revoked block to the transaction. + * @param trans transaction + * @param lba logical block address + * @return standard error code*/ +int jbd_trans_try_revoke_block(struct jbd_trans *trans, + ext4_fsblk_t lba) +{ + struct jbd_journal *journal = trans->journal; + struct jbd_block_rec *block_rec = + jbd_trans_block_rec_lookup(journal, lba); + + if (block_rec) { + if (block_rec->trans == trans) { + struct jbd_buf *jbd_buf = + TAILQ_LAST(&block_rec->dirty_buf_queue, + jbd_buf_dirty); + /* If there are still unwritten buffers. */ + if (TAILQ_FIRST(&block_rec->dirty_buf_queue) != + jbd_buf) + jbd_trans_revoke_block(trans, lba); + + } else + jbd_trans_revoke_block(trans, lba); + } + + return EOK; +} + +/**@brief Free a transaction + * @param journal current journal session + * @param trans transaction + * @param abort discard all the modifications on the block? + * @return standard error code*/ +void jbd_journal_free_trans(struct jbd_journal *journal, + struct jbd_trans *trans, + bool abort) +{ + struct jbd_buf *jbd_buf, *tmp; + struct jbd_revoke_rec *rec, *tmp2; + struct jbd_block_rec *block_rec, *tmp3; + struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs; + TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, + tmp) { + block_rec = jbd_buf->block_rec; + if (abort) { + jbd_buf->block.buf->end_write = NULL; + jbd_buf->block.buf->end_write_arg = NULL; + ext4_bcache_clear_dirty(jbd_buf->block.buf); + ext4_block_set(fs->bdev, &jbd_buf->block); + } + + TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue, + jbd_buf, + dirty_buf_node); + jbd_trans_finish_callback(journal, + trans, + block_rec, + abort, + false); + TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node); + ext4_free(jbd_buf); + } + RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root, + tmp2) { + RB_REMOVE(jbd_revoke_tree, &trans->revoke_root, rec); + ext4_free(rec); + } + LIST_FOREACH_SAFE(block_rec, &trans->tbrec_list, tbrec_node, + tmp3) { + jbd_trans_remove_block_rec(journal, block_rec, trans); + } + + ext4_free(trans); +} + +/**@brief Write commit block for a transaction + * @param trans transaction + * @return standard error code*/ +static int jbd_trans_write_commit_block(struct jbd_trans *trans) +{ + int rc; + struct ext4_block block; + struct jbd_commit_header *header; + uint32_t commit_iblock; + struct jbd_journal *journal = trans->journal; + + commit_iblock = jbd_journal_alloc_block(journal, trans); + + rc = jbd_block_get_noread(journal->jbd_fs, &block, commit_iblock); + if (rc != EOK) + return rc; + + header = (struct jbd_commit_header *)block.data; + jbd_set32(&header->header, magic, JBD_MAGIC_NUMBER); + jbd_set32(&header->header, blocktype, JBD_COMMIT_BLOCK); + jbd_set32(&header->header, sequence, trans->trans_id); + + if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb, + JBD_FEATURE_COMPAT_CHECKSUM)) { + header->chksum_type = JBD_CRC32_CHKSUM; + header->chksum_size = JBD_CRC32_CHKSUM_SIZE; + jbd_set32(header, chksum[0], trans->data_csum); + } + jbd_commit_csum_set(journal->jbd_fs, header); + ext4_bcache_set_dirty(block.buf); + ext4_bcache_set_flag(block.buf, BC_TMP); + rc = jbd_block_set(journal->jbd_fs, &block); + return rc; +} + +/**@brief Write descriptor block for a transaction + * @param journal current journal session + * @param trans transaction + * @return standard error code*/ +static int jbd_journal_prepare(struct jbd_journal *journal, + struct jbd_trans *trans) +{ + int rc = EOK, i = 0; + struct ext4_block desc_block = EXT4_BLOCK_ZERO(), + data_block = EXT4_BLOCK_ZERO(); + int32_t tag_tbl_size = 0; + uint32_t desc_iblock = 0; + uint32_t data_iblock = 0; + char *tag_start = NULL, *tag_ptr = NULL; + struct jbd_buf *jbd_buf, *tmp; + struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs; + uint32_t checksum = EXT4_CRC32_INIT; + struct jbd_bhdr *bhdr = NULL; + void *data; + + /* Try to remove any non-dirty buffers from the tail of + * buf_queue. */ + TAILQ_FOREACH_REVERSE_SAFE(jbd_buf, &trans->buf_queue, + jbd_trans_buf, buf_node, tmp) { + struct jbd_revoke_rec tmp_rec = { + .lba = jbd_buf->block_rec->lba + }; + /* We stop the iteration when we find a dirty buffer. */ + if (ext4_bcache_test_flag(jbd_buf->block.buf, + BC_DIRTY)) + break; + + TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue, + jbd_buf, + dirty_buf_node); + + jbd_buf->block.buf->end_write = NULL; + jbd_buf->block.buf->end_write_arg = NULL; + jbd_trans_finish_callback(journal, + trans, + jbd_buf->block_rec, + true, + RB_FIND(jbd_revoke_tree, + &trans->revoke_root, + &tmp_rec)); + jbd_trans_remove_block_rec(journal, + jbd_buf->block_rec, trans); + trans->data_cnt--; + + ext4_block_set(fs->bdev, &jbd_buf->block); + TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node); + ext4_free(jbd_buf); + } + + TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, tmp) { + struct tag_info tag_info; + bool uuid_exist = false; + bool is_escape = false; + struct jbd_revoke_rec tmp_rec = { + .lba = jbd_buf->block_rec->lba + }; + if (!ext4_bcache_test_flag(jbd_buf->block.buf, + BC_DIRTY)) { + TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue, + jbd_buf, + dirty_buf_node); + + jbd_buf->block.buf->end_write = NULL; + jbd_buf->block.buf->end_write_arg = NULL; + + /* The buffer has not been modified, just release + * that jbd_buf. */ + jbd_trans_finish_callback(journal, + trans, + jbd_buf->block_rec, + true, + RB_FIND(jbd_revoke_tree, + &trans->revoke_root, + &tmp_rec)); + jbd_trans_remove_block_rec(journal, + jbd_buf->block_rec, trans); + trans->data_cnt--; + + ext4_block_set(fs->bdev, &jbd_buf->block); + TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node); + ext4_free(jbd_buf); + continue; + } + checksum = jbd_block_csum(journal->jbd_fs, + jbd_buf->block.data, + checksum, + trans->trans_id); + if (((struct jbd_bhdr *)jbd_buf->block.data)->magic == + to_be32(JBD_MAGIC_NUMBER)) + is_escape = true; + +again: + if (!desc_iblock) { + desc_iblock = jbd_journal_alloc_block(journal, trans); + rc = jbd_block_get_noread(journal->jbd_fs, &desc_block, desc_iblock); + if (rc != EOK) + break; + + bhdr = (struct jbd_bhdr *)desc_block.data; + jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER); + jbd_set32(bhdr, blocktype, JBD_DESCRIPTOR_BLOCK); + jbd_set32(bhdr, sequence, trans->trans_id); + + tag_start = (char *)(bhdr + 1); + tag_ptr = tag_start; + uuid_exist = true; + tag_tbl_size = journal->block_size - + sizeof(struct jbd_bhdr); + + if (jbd_has_csum(&journal->jbd_fs->sb)) + tag_tbl_size -= sizeof(struct jbd_block_tail); + + if (!trans->start_iblock) + trans->start_iblock = desc_iblock; + + ext4_bcache_set_dirty(desc_block.buf); + ext4_bcache_set_flag(desc_block.buf, BC_TMP); + } + tag_info.block = jbd_buf->block.lb_id; + tag_info.uuid_exist = uuid_exist; + tag_info.is_escape = is_escape; + if (i == trans->data_cnt - 1) + tag_info.last_tag = true; + else + tag_info.last_tag = false; + + tag_info.checksum = checksum; + + if (uuid_exist) + memcpy(tag_info.uuid, journal->jbd_fs->sb.uuid, + UUID_SIZE); + + rc = jbd_write_block_tag(journal->jbd_fs, + tag_ptr, + tag_tbl_size, + &tag_info); + if (rc != EOK) { + jbd_meta_csum_set(journal->jbd_fs, bhdr); + desc_iblock = 0; + rc = jbd_block_set(journal->jbd_fs, &desc_block); + if (rc != EOK) + break; + + goto again; + } + + data_iblock = jbd_journal_alloc_block(journal, trans); + rc = jbd_block_get_noread(journal->jbd_fs, &data_block, data_iblock); + if (rc != EOK) { + desc_iblock = 0; + ext4_bcache_clear_dirty(desc_block.buf); + jbd_block_set(journal->jbd_fs, &desc_block); + break; + } + + data = data_block.data; + memcpy(data, jbd_buf->block.data, + journal->block_size); + if (is_escape) + ((struct jbd_bhdr *)data)->magic = 0; + + ext4_bcache_set_dirty(data_block.buf); + ext4_bcache_set_flag(data_block.buf, BC_TMP); + rc = jbd_block_set(journal->jbd_fs, &data_block); + if (rc != EOK) { + desc_iblock = 0; + ext4_bcache_clear_dirty(desc_block.buf); + jbd_block_set(journal->jbd_fs, &desc_block); + break; + } + jbd_buf->jbd_lba = data_iblock; + + tag_ptr += tag_info.tag_bytes; + tag_tbl_size -= tag_info.tag_bytes; + + i++; + } + if (rc == EOK && desc_iblock) { + jbd_meta_csum_set(journal->jbd_fs, + (struct jbd_bhdr *)bhdr); + trans->data_csum = checksum; + rc = jbd_block_set(journal->jbd_fs, &desc_block); + } + + return rc; +} + +/**@brief Write revoke block for a transaction + * @param journal current journal session + * @param trans transaction + * @return standard error code*/ +static int +jbd_journal_prepare_revoke(struct jbd_journal *journal, + struct jbd_trans *trans) +{ + int rc = EOK, i = 0; + struct ext4_block desc_block = EXT4_BLOCK_ZERO(); + int32_t tag_tbl_size = 0; + uint32_t desc_iblock = 0; + char *blocks_entry = NULL; + struct jbd_revoke_rec *rec, *tmp; + struct jbd_revoke_header *header = NULL; + int32_t record_len = 4; + struct jbd_bhdr *bhdr = NULL; + + if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb, + JBD_FEATURE_INCOMPAT_64BIT)) + record_len = 8; + + RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root, + tmp) { +again: + if (!desc_iblock) { + desc_iblock = jbd_journal_alloc_block(journal, trans); + rc = jbd_block_get_noread(journal->jbd_fs, &desc_block, + desc_iblock); + if (rc != EOK) + break; + + bhdr = (struct jbd_bhdr *)desc_block.data; + jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER); + jbd_set32(bhdr, blocktype, JBD_REVOKE_BLOCK); + jbd_set32(bhdr, sequence, trans->trans_id); + + header = (struct jbd_revoke_header *)bhdr; + blocks_entry = (char *)(header + 1); + tag_tbl_size = journal->block_size - + sizeof(struct jbd_revoke_header); + + if (jbd_has_csum(&journal->jbd_fs->sb)) + tag_tbl_size -= sizeof(struct jbd_block_tail); + + if (!trans->start_iblock) + trans->start_iblock = desc_iblock; + + ext4_bcache_set_dirty(desc_block.buf); + ext4_bcache_set_flag(desc_block.buf, BC_TMP); + } + + if (tag_tbl_size < record_len) { + jbd_set32(header, count, + journal->block_size - tag_tbl_size); + jbd_meta_csum_set(journal->jbd_fs, bhdr); + bhdr = NULL; + desc_iblock = 0; + header = NULL; + rc = jbd_block_set(journal->jbd_fs, &desc_block); + if (rc != EOK) + break; + + goto again; + } + if (record_len == 8) { + uint64_t *blocks = + (uint64_t *)blocks_entry; + *blocks = to_be64(rec->lba); + } else { + uint32_t *blocks = + (uint32_t *)blocks_entry; + *blocks = to_be32((uint32_t)rec->lba); + } + blocks_entry += record_len; + tag_tbl_size -= record_len; + + i++; + } + if (rc == EOK && desc_iblock) { + if (header != NULL) + jbd_set32(header, count, + journal->block_size - tag_tbl_size); + + jbd_meta_csum_set(journal->jbd_fs, bhdr); + rc = jbd_block_set(journal->jbd_fs, &desc_block); + } + + return rc; +} + +/**@brief Put references of block descriptors in a transaction. + * @param journal current journal session + * @param trans transaction*/ +void jbd_journal_cp_trans(struct jbd_journal *journal, struct jbd_trans *trans) +{ + struct jbd_buf *jbd_buf, *tmp; + struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs; + TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, + tmp) { + struct ext4_block block = jbd_buf->block; + ext4_block_set(fs->bdev, &block); + } +} + +/**@brief Update the start block of the journal when + * all the contents in a transaction reach the disk.*/ +static void jbd_trans_end_write(struct ext4_bcache *bc __unused, + struct ext4_buf *buf, + int res, + void *arg) +{ + struct jbd_buf *jbd_buf = arg; + struct jbd_trans *trans = jbd_buf->trans; + struct jbd_block_rec *block_rec = jbd_buf->block_rec; + struct jbd_journal *journal = trans->journal; + bool first_in_queue = + trans == TAILQ_FIRST(&journal->cp_queue); + if (res != EOK) + trans->error = res; + + TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node); + TAILQ_REMOVE(&block_rec->dirty_buf_queue, + jbd_buf, + dirty_buf_node); + + jbd_trans_finish_callback(journal, + trans, + jbd_buf->block_rec, + false, + false); + if (block_rec->trans == trans && buf) { + /* Clear the end_write and end_write_arg fields. */ + buf->end_write = NULL; + buf->end_write_arg = NULL; + } + + ext4_free(jbd_buf); + + trans->written_cnt++; + if (trans->written_cnt == trans->data_cnt) { + /* If it is the first transaction on checkpoint queue, + * we will shift the start of the journal to the next + * transaction, and remove subsequent written + * transactions from checkpoint queue until we find + * an unwritten one. */ + if (first_in_queue) { + journal->start = trans->start_iblock + + trans->alloc_blocks; + wrap(&journal->jbd_fs->sb, journal->start); + journal->trans_id = trans->trans_id + 1; + TAILQ_REMOVE(&journal->cp_queue, trans, trans_node); + jbd_journal_free_trans(journal, trans, false); + + jbd_journal_purge_cp_trans(journal, false, false); + jbd_journal_write_sb(journal); + jbd_write_sb(journal->jbd_fs); + } + } +} + +/**@brief Commit a transaction to the journal immediately. + * @param journal current journal session + * @param trans transaction + * @return standard error code*/ +static int __jbd_journal_commit_trans(struct jbd_journal *journal, + struct jbd_trans *trans) +{ + int rc = EOK; + uint32_t last = journal->last; + struct jbd_revoke_rec *rec, *tmp; + + trans->trans_id = journal->alloc_trans_id; + rc = jbd_journal_prepare(journal, trans); + if (rc != EOK) + goto Finish; + + rc = jbd_journal_prepare_revoke(journal, trans); + if (rc != EOK) + goto Finish; + + if (TAILQ_EMPTY(&trans->buf_queue) && + RB_EMPTY(&trans->revoke_root)) { + /* Since there are no entries in both buffer list + * and revoke entry list, we do not consider trans as + * complete transaction and just return EOK.*/ + jbd_journal_free_trans(journal, trans, false); + goto Finish; + } + + rc = jbd_trans_write_commit_block(trans); + if (rc != EOK) + goto Finish; + + journal->alloc_trans_id++; + + /* Complete the checkpoint of buffers which are revoked. */ + RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root, + tmp) { + struct jbd_block_rec *block_rec = + jbd_trans_block_rec_lookup(journal, rec->lba); + struct jbd_buf *jbd_buf = NULL; + if (block_rec) + jbd_buf = TAILQ_LAST(&block_rec->dirty_buf_queue, + jbd_buf_dirty); + if (jbd_buf) { + struct ext4_buf *buf; + struct ext4_block block = EXT4_BLOCK_ZERO(); + /* + * We do this to reset the ext4_buf::end_write and + * ext4_buf::end_write_arg fields so that the checkpoint + * callback won't be triggered again. + */ + buf = ext4_bcache_find_get(journal->jbd_fs->bdev->bc, + &block, + jbd_buf->block_rec->lba); + jbd_trans_end_write(journal->jbd_fs->bdev->bc, + buf, + EOK, + jbd_buf); + if (buf) + ext4_block_set(journal->jbd_fs->bdev, &block); + } + } + + if (TAILQ_EMPTY(&journal->cp_queue)) { + /* + * This transaction is going to be the first object in the + * checkpoint queue. + * When the first transaction in checkpoint queue is completely + * written to disk, we shift the tail of the log to right. + */ + if (trans->data_cnt) { + journal->start = trans->start_iblock; + wrap(&journal->jbd_fs->sb, journal->start); + journal->trans_id = trans->trans_id; + jbd_journal_write_sb(journal); + jbd_write_sb(journal->jbd_fs); + TAILQ_INSERT_TAIL(&journal->cp_queue, trans, + trans_node); + jbd_journal_cp_trans(journal, trans); + } else { + journal->start = trans->start_iblock + + trans->alloc_blocks; + wrap(&journal->jbd_fs->sb, journal->start); + journal->trans_id = trans->trans_id + 1; + jbd_journal_write_sb(journal); + jbd_journal_free_trans(journal, trans, false); + } + } else { + /* No need to do anything to the JBD superblock. */ + TAILQ_INSERT_TAIL(&journal->cp_queue, trans, + trans_node); + if (trans->data_cnt) + jbd_journal_cp_trans(journal, trans); + } +Finish: + if (rc != EOK && rc != ENOSPC) { + journal->last = last; + jbd_journal_free_trans(journal, trans, true); + } + return rc; +} + +/**@brief Allocate a new transaction + * @param journal current journal session + * @return transaction allocated*/ +struct jbd_trans * +jbd_journal_new_trans(struct jbd_journal *journal) +{ + struct jbd_trans *trans = NULL; + trans = ext4_calloc(1, sizeof(struct jbd_trans)); + if (!trans) + return NULL; + + /* We will assign a trans_id to this transaction, + * once it has been committed.*/ + trans->journal = journal; + trans->data_csum = EXT4_CRC32_INIT; + trans->error = EOK; + TAILQ_INIT(&trans->buf_queue); + return trans; +} + +/**@brief Commit a transaction to the journal immediately. + * @param journal current journal session + * @param trans transaction + * @return standard error code*/ +int jbd_journal_commit_trans(struct jbd_journal *journal, + struct jbd_trans *trans) +{ + int r = EOK; + r = __jbd_journal_commit_trans(journal, trans); + return r; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_mbr.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_mbr.c new file mode 100644 index 0000000..fb61dca --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_mbr.c @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_mbr.c + * @brief Master boot record parser + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_mbr.h" + +#include +#include + +#define MBR_SIGNATURE 0xAA55 + +#pragma pack(push, 1) + +struct ext4_part_entry { + uint8_t status; + uint8_t chs1[3]; + uint8_t type; + uint8_t chs2[3]; + uint32_t first_lba; + uint32_t sectors; +}; + +struct ext4_mbr { + uint8_t bootstrap[442]; + uint32_t disk_id; + struct ext4_part_entry part_entry[4]; + uint16_t signature; +}; + +#pragma pack(pop) + +int ext4_mbr_scan(struct ext4_blockdev *parent, struct ext4_mbr_bdevs *bdevs) +{ + int r; + size_t i; + + ext4_dbg(DEBUG_MBR, DBG_INFO "ext4_mbr_scan\n"); + memset(bdevs, 0, sizeof(struct ext4_mbr_bdevs)); + r = ext4_block_init(parent); + if (r != EOK) + return r; + + r = ext4_block_readbytes(parent, 0, parent->bdif->ph_bbuf, 512); + if (r != EOK) { + goto blockdev_fini; + } + + const struct ext4_mbr *mbr = (void *)parent->bdif->ph_bbuf; + + if (to_le16(mbr->signature) != MBR_SIGNATURE) { + ext4_dbg(DEBUG_MBR, DBG_ERROR "ext4_mbr_scan: unknown " + "signature: 0x%x\n", to_le16(mbr->signature)); + r = ENOENT; + goto blockdev_fini; + } + + /*Show bootstrap code*/ + ext4_dbg(DEBUG_MBR, "mbr_part: bootstrap:"); + for (i = 0; i < sizeof(mbr->bootstrap); ++i) { + if (!(i & 0xF)) + ext4_dbg(DEBUG_MBR | DEBUG_NOPREFIX, "\n"); + ext4_dbg(DEBUG_MBR | DEBUG_NOPREFIX, "%02x, ", mbr->bootstrap[i]); + } + + ext4_dbg(DEBUG_MBR | DEBUG_NOPREFIX, "\n\n"); + for (i = 0; i < 4; ++i) { + const struct ext4_part_entry *pe = &mbr->part_entry[i]; + ext4_dbg(DEBUG_MBR, "mbr_part: %d\n", (int)i); + ext4_dbg(DEBUG_MBR, "\tstatus: 0x%x\n", pe->status); + ext4_dbg(DEBUG_MBR, "\ttype 0x%x:\n", pe->type); + ext4_dbg(DEBUG_MBR, "\tfirst_lba: 0x%"PRIx32"\n", pe->first_lba); + ext4_dbg(DEBUG_MBR, "\tsectors: 0x%"PRIx32"\n", pe->sectors); + + if (!pe->sectors) + continue; /*Empty entry*/ + + if (pe->type != 0x83) + continue; /*Unsupported entry. 0x83 - linux native*/ + + bdevs->partitions[i].bdif = parent->bdif; + bdevs->partitions[i].part_offset = + (uint64_t)pe->first_lba * parent->bdif->ph_bsize; + bdevs->partitions[i].part_size = + (uint64_t)pe->sectors * parent->bdif->ph_bsize; + } + + blockdev_fini: + ext4_block_fini(parent); + return r; +} + +int ext4_mbr_write(struct ext4_blockdev *parent, struct ext4_mbr_parts *parts, uint32_t disk_id) +{ + int r; + uint64_t disk_size; + uint32_t division_sum = parts->division[0] + parts->division[1] + + parts->division[2] + parts->division[3]; + + if (division_sum > 100) + return EINVAL; + + ext4_dbg(DEBUG_MBR, DBG_INFO "ext4_mbr_write\n"); + r = ext4_block_init(parent); + if (r != EOK) + return r; + + disk_size = parent->part_size; + + /*Calculate CHS*/ + uint32_t k = 16; + while ((k < 256) && ((disk_size / k / 63) > 1024)) + k *= 2; + + if (k == 256) + --k; + + const uint32_t cyl_size = 63 * k; + const uint32_t cyl_count = disk_size / cyl_size; + + struct ext4_mbr *mbr = (void *)parent->bdif->ph_bbuf; + memset(mbr, 0, sizeof(struct ext4_mbr)); + + mbr->disk_id = disk_id; + + uint32_t cyl_it = 0; + for (int i = 0; i < 4; ++i) { + uint32_t cyl_part = cyl_count * parts->division[i] / 100; + if (!cyl_part) + continue; + + uint32_t part_start = cyl_it * cyl_size; + uint32_t part_size = cyl_part * cyl_size; + + if (i == 0) { + part_start += 63; + part_size -= 63 * parent->bdif->ph_bsize; + } + + uint32_t cyl_end = cyl_part + cyl_it - 1; + + mbr->part_entry[i].status = 0; + mbr->part_entry[i].chs1[0] = i ? 0 : 1;; + mbr->part_entry[i].chs1[1] = (cyl_it >> 2) + 1; + mbr->part_entry[i].chs1[2] = cyl_it; + mbr->part_entry[i].type = 0x83; + mbr->part_entry[i].chs2[0] = k - 1; + mbr->part_entry[i].chs2[1] = (cyl_end >> 2) + 63; + mbr->part_entry[i].chs2[2] = cyl_end; + + mbr->part_entry[i].first_lba = part_start; + mbr->part_entry[i].sectors = part_size / parent->bdif->ph_bsize; + + cyl_it += cyl_part; + } + + mbr->signature = MBR_SIGNATURE; + r = ext4_block_writebytes(parent, 0, parent->bdif->ph_bbuf, 512); + if (r != EOK) + goto blockdev_fini; + + + blockdev_fini: + ext4_block_fini(parent); + return r; +} + +/** + * @} + */ + diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_mkfs.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_mkfs.c new file mode 100644 index 0000000..48acf17 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_mkfs.c @@ -0,0 +1,865 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_mkfs.c + * @brief + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_super.h" +#include "ext4_block_group.h" +#include "ext4_dir.h" +#include "ext4_dir_idx.h" +#include "ext4_fs.h" +#include "ext4_inode.h" +#include "ext4_ialloc.h" +#include "ext4_mkfs.h" + +#include +#include +#include + +struct fs_aux_info { + struct ext4_sblock *sb; + uint8_t *bg_desc_blk; + struct xattr_list_element *xattrs; + uint32_t first_data_block; + uint64_t len_blocks; + uint32_t inode_table_blocks; + uint32_t groups; + uint32_t bg_desc_blocks; + uint32_t default_i_flags; + uint32_t blocks_per_ind; + uint32_t blocks_per_dind; + uint32_t blocks_per_tind; +}; + +static inline int log_2(int j) +{ + int i; + + for (i = 0; j > 0; i++) + j >>= 1; + + return i - 1; +} + +static int sb2info(struct ext4_sblock *sb, struct ext4_mkfs_info *info) +{ + if (to_le16(sb->magic) != EXT4_SUPERBLOCK_MAGIC) + return EINVAL; + + info->block_size = 1024 << to_le32(sb->log_block_size); + info->blocks_per_group = to_le32(sb->blocks_per_group); + info->inodes_per_group = to_le32(sb->inodes_per_group); + info->inode_size = to_le16(sb->inode_size); + info->inodes = to_le32(sb->inodes_count); + info->feat_ro_compat = to_le32(sb->features_read_only); + info->feat_compat = to_le32(sb->features_compatible); + info->feat_incompat = to_le32(sb->features_incompatible); + info->bg_desc_reserve_blocks = to_le16(sb->s_reserved_gdt_blocks); + info->label = sb->volume_name; + info->len = (uint64_t)info->block_size * ext4_sb_get_blocks_cnt(sb); + info->dsc_size = to_le16(sb->desc_size); + memcpy(info->uuid, sb->uuid, UUID_SIZE); + + return EOK; +} + +static uint32_t compute_blocks_per_group(struct ext4_mkfs_info *info) +{ + return info->block_size * 8; +} + +static uint32_t compute_inodes(struct ext4_mkfs_info *info) +{ + return (uint32_t)EXT4_DIV_ROUND_UP(info->len, info->block_size) / 4; +} + +static uint32_t compute_inodes_per_group(struct ext4_mkfs_info *info) +{ + uint32_t blocks = (uint32_t)EXT4_DIV_ROUND_UP(info->len, info->block_size); + uint32_t block_groups = EXT4_DIV_ROUND_UP(blocks, info->blocks_per_group); + uint32_t inodes = EXT4_DIV_ROUND_UP(info->inodes, block_groups); + inodes = EXT4_ALIGN(inodes, (info->block_size / info->inode_size)); + + /* After properly rounding up the number of inodes/group, + * make sure to update the total inodes field in the info struct. + */ + info->inodes = inodes * block_groups; + + return inodes; +} + + +static uint32_t compute_journal_blocks(struct ext4_mkfs_info *info) +{ + uint32_t journal_blocks = (uint32_t)EXT4_DIV_ROUND_UP(info->len, + info->block_size) / 64; + if (journal_blocks < 1024) + journal_blocks = 1024; + if (journal_blocks > 32768) + journal_blocks = 32768; + return journal_blocks; +} + +static bool has_superblock(struct ext4_mkfs_info *info, uint32_t bgid) +{ + if (!(info->feat_ro_compat & EXT4_FRO_COM_SPARSE_SUPER)) + return true; + + return ext4_sb_sparse(bgid); +} + +static int create_fs_aux_info(struct fs_aux_info *aux_info, + struct ext4_mkfs_info *info) +{ + aux_info->first_data_block = (info->block_size > 1024) ? 0 : 1; + aux_info->len_blocks = info->len / info->block_size; + aux_info->inode_table_blocks = EXT4_DIV_ROUND_UP(info->inodes_per_group * + info->inode_size, info->block_size); + aux_info->groups = (uint32_t)EXT4_DIV_ROUND_UP(aux_info->len_blocks - + aux_info->first_data_block, info->blocks_per_group); + aux_info->blocks_per_ind = info->block_size / sizeof(uint32_t); + aux_info->blocks_per_dind = + aux_info->blocks_per_ind * aux_info->blocks_per_ind; + aux_info->blocks_per_tind = + aux_info->blocks_per_dind * aux_info->blocks_per_dind; + + aux_info->bg_desc_blocks = + EXT4_DIV_ROUND_UP(aux_info->groups * info->dsc_size, + info->block_size); + + aux_info->default_i_flags = EXT4_INODE_FLAG_NOATIME; + + uint32_t last_group_size = aux_info->len_blocks % info->blocks_per_group; + uint32_t last_header_size = 2 + aux_info->inode_table_blocks; + if (has_superblock(info, aux_info->groups - 1)) + last_header_size += 1 + aux_info->bg_desc_blocks + + info->bg_desc_reserve_blocks; + + if (last_group_size > 0 && last_group_size < last_header_size) { + aux_info->groups--; + aux_info->len_blocks -= last_group_size; + } + + aux_info->sb = ext4_calloc(1, EXT4_SUPERBLOCK_SIZE); + if (!aux_info->sb) + return ENOMEM; + + aux_info->bg_desc_blk = ext4_calloc(1, info->block_size); + if (!aux_info->bg_desc_blk) + return ENOMEM; + + aux_info->xattrs = NULL; + + + ext4_dbg(DEBUG_MKFS, DBG_INFO "create_fs_aux_info\n"); + ext4_dbg(DEBUG_MKFS, DBG_NONE "first_data_block: %"PRIu32"\n", + aux_info->first_data_block); + ext4_dbg(DEBUG_MKFS, DBG_NONE "len_blocks: %"PRIu64"\n", + aux_info->len_blocks); + ext4_dbg(DEBUG_MKFS, DBG_NONE "inode_table_blocks: %"PRIu32"\n", + aux_info->inode_table_blocks); + ext4_dbg(DEBUG_MKFS, DBG_NONE "groups: %"PRIu32"\n", + aux_info->groups); + ext4_dbg(DEBUG_MKFS, DBG_NONE "bg_desc_blocks: %"PRIu32"\n", + aux_info->bg_desc_blocks); + ext4_dbg(DEBUG_MKFS, DBG_NONE "default_i_flags: %"PRIu32"\n", + aux_info->default_i_flags); + ext4_dbg(DEBUG_MKFS, DBG_NONE "blocks_per_ind: %"PRIu32"\n", + aux_info->blocks_per_ind); + ext4_dbg(DEBUG_MKFS, DBG_NONE "blocks_per_dind: %"PRIu32"\n", + aux_info->blocks_per_dind); + ext4_dbg(DEBUG_MKFS, DBG_NONE "blocks_per_tind: %"PRIu32"\n", + aux_info->blocks_per_tind); + + return EOK; +} + +static void release_fs_aux_info(struct fs_aux_info *aux_info) +{ + if (aux_info->sb) + ext4_free(aux_info->sb); + if (aux_info->bg_desc_blk) + ext4_free(aux_info->bg_desc_blk); +} + + +/* Fill in the superblock memory buffer based on the filesystem parameters */ +static void fill_sb(struct fs_aux_info *aux_info, struct ext4_mkfs_info *info) +{ + struct ext4_sblock *sb = aux_info->sb; + + sb->inodes_count = to_le32(info->inodes_per_group * aux_info->groups); + + ext4_sb_set_blocks_cnt(sb, aux_info->len_blocks); + ext4_sb_set_free_blocks_cnt(sb, aux_info->len_blocks); + sb->free_inodes_count = to_le32(info->inodes_per_group * aux_info->groups); + + sb->reserved_blocks_count_lo = to_le32(0); + sb->first_data_block = to_le32(aux_info->first_data_block); + sb->log_block_size = to_le32(log_2(info->block_size / 1024)); + sb->log_cluster_size = to_le32(log_2(info->block_size / 1024)); + sb->blocks_per_group = to_le32(info->blocks_per_group); + sb->frags_per_group = to_le32(info->blocks_per_group); + sb->inodes_per_group = to_le32(info->inodes_per_group); + sb->mount_time = to_le32(0); + sb->write_time = to_le32(0); + sb->mount_count = to_le16(0); + sb->max_mount_count = to_le16(0xFFFF); + sb->magic = to_le16(EXT4_SUPERBLOCK_MAGIC); + sb->state = to_le16(EXT4_SUPERBLOCK_STATE_VALID_FS); + sb->errors = to_le16(EXT4_SUPERBLOCK_ERRORS_RO); + sb->minor_rev_level = to_le16(0); + sb->last_check_time = to_le32(0); + sb->check_interval = to_le32(0); + sb->creator_os = to_le32(EXT4_SUPERBLOCK_OS_LINUX); + sb->rev_level = to_le32(1); + sb->def_resuid = to_le16(0); + sb->def_resgid = to_le16(0); + + sb->first_inode = to_le32(EXT4_GOOD_OLD_FIRST_INO); + sb->inode_size = to_le16(info->inode_size); + sb->block_group_index = to_le16(0); + + sb->features_compatible = to_le32(info->feat_compat); + sb->features_incompatible = to_le32(info->feat_incompat); + sb->features_read_only = to_le32(info->feat_ro_compat); + + memcpy(sb->uuid, info->uuid, UUID_SIZE); + + memset(sb->volume_name, 0, sizeof(sb->volume_name)); + strncpy(sb->volume_name, info->label, sizeof(sb->volume_name)); + memset(sb->last_mounted, 0, sizeof(sb->last_mounted)); + + sb->algorithm_usage_bitmap = to_le32(0); + sb->s_prealloc_blocks = 0; + sb->s_prealloc_dir_blocks = 0; + sb->s_reserved_gdt_blocks = to_le16(info->bg_desc_reserve_blocks); + + if (info->feat_compat & EXT4_FCOM_HAS_JOURNAL) + sb->journal_inode_number = to_le32(EXT4_JOURNAL_INO); + + sb->journal_backup_type = 1; + sb->journal_dev = to_le32(0); + sb->last_orphan = to_le32(0); + sb->hash_seed[0] = to_le32(0x11111111); + sb->hash_seed[1] = to_le32(0x22222222); + sb->hash_seed[2] = to_le32(0x33333333); + sb->hash_seed[3] = to_le32(0x44444444); + sb->default_hash_version = EXT2_HTREE_HALF_MD4; + sb->checksum_type = 1; + sb->desc_size = to_le16(info->dsc_size); + sb->default_mount_opts = to_le32(0); + sb->first_meta_bg = to_le32(0); + sb->mkfs_time = to_le32(0); + + sb->reserved_blocks_count_hi = to_le32(0); + sb->min_extra_isize = to_le32(sizeof(struct ext4_inode) - + EXT4_GOOD_OLD_INODE_SIZE); + sb->want_extra_isize = to_le32(sizeof(struct ext4_inode) - + EXT4_GOOD_OLD_INODE_SIZE); + sb->flags = to_le32(EXT4_SUPERBLOCK_FLAGS_SIGNED_HASH); +} + + +static int write_bgroup_block(struct ext4_blockdev *bd, + struct fs_aux_info *aux_info, + struct ext4_mkfs_info *info, + uint32_t blk) +{ + int r = EOK; + uint32_t j; + struct ext4_block b; + + uint32_t block_size = ext4_sb_get_block_size(aux_info->sb); + + for (j = 0; j < aux_info->groups; j++) { + uint64_t bg_start_block = aux_info->first_data_block + + j * info->blocks_per_group; + uint32_t blk_off = 0; + + blk_off += aux_info->bg_desc_blocks; + if (has_superblock(info, j)) { + bg_start_block++; + blk_off += info->bg_desc_reserve_blocks; + } + + uint64_t dsc_blk = bg_start_block + blk; + + r = ext4_block_get_noread(bd, &b, dsc_blk); + if (r != EOK) + return r; + + memcpy(b.data, aux_info->bg_desc_blk, block_size); + + ext4_bcache_set_dirty(b.buf); + r = ext4_block_set(bd, &b); + if (r != EOK) + return r; + } + + return r; +} + +static int write_bgroups(struct ext4_blockdev *bd, struct fs_aux_info *aux_info, + struct ext4_mkfs_info *info) +{ + int r = EOK; + + struct ext4_block b; + struct ext4_bgroup *bg_desc; + + uint32_t i; + uint32_t bg_free_blk = 0; + uint64_t sb_free_blk = 0; + uint32_t block_size = ext4_sb_get_block_size(aux_info->sb); + uint32_t dsc_size = ext4_sb_get_desc_size(aux_info->sb); + uint32_t dsc_per_block = block_size / dsc_size; + uint32_t k = 0; + + for (i = 0; i < aux_info->groups; i++) { + uint64_t bg_start_block = aux_info->first_data_block + + aux_info->first_data_block + i * info->blocks_per_group; + uint32_t blk_off = 0; + + bg_desc = (void *)(aux_info->bg_desc_blk + k * dsc_size); + bg_free_blk = info->blocks_per_group - + aux_info->inode_table_blocks; + + bg_free_blk -= 2; + blk_off += aux_info->bg_desc_blocks; + + if (i == (aux_info->groups - 1)) + bg_free_blk -= aux_info->first_data_block; + + if (has_superblock(info, i)) { + bg_start_block++; + blk_off += info->bg_desc_reserve_blocks; + bg_free_blk -= info->bg_desc_reserve_blocks + 1; + bg_free_blk -= aux_info->bg_desc_blocks; + } + + ext4_bg_set_block_bitmap(bg_desc, aux_info->sb, + bg_start_block + blk_off + 1); + + ext4_bg_set_inode_bitmap(bg_desc, aux_info->sb, + bg_start_block + blk_off + 2); + + ext4_bg_set_inode_table_first_block(bg_desc, + aux_info->sb, + bg_start_block + blk_off + 3); + + ext4_bg_set_free_blocks_count(bg_desc, aux_info->sb, + bg_free_blk); + + ext4_bg_set_free_inodes_count(bg_desc, + aux_info->sb, to_le32(aux_info->sb->inodes_per_group)); + + ext4_bg_set_used_dirs_count(bg_desc, aux_info->sb, 0); + + ext4_bg_set_flag(bg_desc, + EXT4_BLOCK_GROUP_BLOCK_UNINIT | + EXT4_BLOCK_GROUP_INODE_UNINIT); + + sb_free_blk += bg_free_blk; + + r = ext4_block_get_noread(bd, &b, bg_start_block + blk_off + 1); + if (r != EOK) + return r; + memset(b.data, 0, block_size); + ext4_bcache_set_dirty(b.buf); + r = ext4_block_set(bd, &b); + if (r != EOK) + return r; + r = ext4_block_get_noread(bd, &b, bg_start_block + blk_off + 2); + if (r != EOK) + return r; + memset(b.data, 0, block_size); + ext4_bcache_set_dirty(b.buf); + r = ext4_block_set(bd, &b); + if (r != EOK) + return r; + + if (++k != dsc_per_block) + continue; + + k = 0; + r = write_bgroup_block(bd, aux_info, info, i / dsc_per_block); + if (r != EOK) + return r; + + } + + r = write_bgroup_block(bd, aux_info, info, i / dsc_per_block); + if (r != EOK) + return r; + + ext4_sb_set_free_blocks_cnt(aux_info->sb, sb_free_blk); + return r; +} + +static int write_sblocks(struct ext4_blockdev *bd, struct fs_aux_info *aux_info, + struct ext4_mkfs_info *info) +{ + uint64_t offset; + uint32_t i; + int r; + + /* write out the backup superblocks */ + for (i = 1; i < aux_info->groups; i++) { + if (has_superblock(info, i)) { + offset = info->block_size * (aux_info->first_data_block + + i * info->blocks_per_group); + + aux_info->sb->block_group_index = to_le16(i); + r = ext4_block_writebytes(bd, offset, aux_info->sb, + EXT4_SUPERBLOCK_SIZE); + if (r != EOK) + return r; + } + } + + /* write out the primary superblock */ + aux_info->sb->block_group_index = to_le16(0); + return ext4_block_writebytes(bd, 1024, aux_info->sb, + EXT4_SUPERBLOCK_SIZE); +} + + +int ext4_mkfs_read_info(struct ext4_blockdev *bd, struct ext4_mkfs_info *info) +{ + int r; + struct ext4_sblock *sb = NULL; + r = ext4_block_init(bd); + if (r != EOK) + return r; + + sb = ext4_malloc(EXT4_SUPERBLOCK_SIZE); + if (!sb) + goto Finish; + + + r = ext4_sb_read(bd, sb); + if (r != EOK) + goto Finish; + + r = sb2info(sb, info); + +Finish: + if (sb) + ext4_free(sb); + ext4_block_fini(bd); + return r; +} + +static int mkfs_init(struct ext4_blockdev *bd, struct ext4_mkfs_info *info) +{ + int r; + struct fs_aux_info aux_info; + memset(&aux_info, 0, sizeof(struct fs_aux_info)); + + r = create_fs_aux_info(&aux_info, info); + if (r != EOK) + goto Finish; + + fill_sb(&aux_info, info); + + r = write_bgroups(bd, &aux_info, info); + if (r != EOK) + goto Finish; + + r = write_sblocks(bd, &aux_info, info); + if (r != EOK) + goto Finish; + + Finish: + release_fs_aux_info(&aux_info); + return r; +} + +static int init_bgs(struct ext4_fs *fs) +{ + int r = EOK; + struct ext4_block_group_ref ref; + uint32_t i; + uint32_t bg_count = ext4_block_group_cnt(&fs->sb); + for (i = 0; i < bg_count; ++i) { + r = ext4_fs_get_block_group_ref(fs, i, &ref); + if (r != EOK) + break; + + r = ext4_fs_put_block_group_ref(&ref); + if (r != EOK) + break; + } + return r; +} + +static int alloc_inodes(struct ext4_fs *fs) +{ + int r = EOK; + int i; + struct ext4_inode_ref inode_ref; + for (i = 1; i < 12; ++i) { + int filetype = EXT4_DE_REG_FILE; + + switch (i) { + case EXT4_ROOT_INO: + case EXT4_GOOD_OLD_FIRST_INO: + filetype = EXT4_DE_DIR; + break; + default: + break; + } + + r = ext4_fs_alloc_inode(fs, &inode_ref, filetype); + if (r != EOK) + return r; + + ext4_inode_set_mode(&fs->sb, inode_ref.inode, 0); + + switch (i) { + case EXT4_ROOT_INO: + case EXT4_JOURNAL_INO: + ext4_fs_inode_blocks_init(fs, &inode_ref); + break; + } + + ext4_fs_put_inode_ref(&inode_ref); + } + + return r; +} + +static int create_dirs(struct ext4_fs *fs) +{ + int r = EOK; + struct ext4_inode_ref root; + struct ext4_inode_ref child; + + r = ext4_fs_get_inode_ref(fs, EXT4_ROOT_INO, &root); + if (r != EOK) + return r; + + r = ext4_fs_get_inode_ref(fs, EXT4_GOOD_OLD_FIRST_INO, &child); + if (r != EOK) + return r; + + ext4_inode_set_mode(&fs->sb, child.inode, + EXT4_INODE_MODE_DIRECTORY | 0777); + + ext4_inode_set_mode(&fs->sb, root.inode, + EXT4_INODE_MODE_DIRECTORY | 0777); + +#if CONFIG_DIR_INDEX_ENABLE + /* Initialize directory index if supported */ + if (ext4_sb_feature_com(&fs->sb, EXT4_FCOM_DIR_INDEX)) { + r = ext4_dir_dx_init(&root, &root); + if (r != EOK) + return r; + + r = ext4_dir_dx_init(&child, &root); + if (r != EOK) + return r; + + ext4_inode_set_flag(root.inode, EXT4_INODE_FLAG_INDEX); + ext4_inode_set_flag(child.inode, EXT4_INODE_FLAG_INDEX); + } else +#endif + { + r = ext4_dir_add_entry(&root, ".", strlen("."), &root); + if (r != EOK) + return r; + + r = ext4_dir_add_entry(&root, "..", strlen(".."), &root); + if (r != EOK) + return r; + + r = ext4_dir_add_entry(&child, ".", strlen("."), &child); + if (r != EOK) + return r; + + r = ext4_dir_add_entry(&child, "..", strlen(".."), &root); + if (r != EOK) + return r; + } + + r = ext4_dir_add_entry(&root, "lost+found", strlen("lost+found"), &child); + if (r != EOK) + return r; + + ext4_inode_set_links_cnt(root.inode, 3); + ext4_inode_set_links_cnt(child.inode, 2); + + child.dirty = true; + root.dirty = true; + ext4_fs_put_inode_ref(&child); + ext4_fs_put_inode_ref(&root); + return r; +} + +static int create_journal_inode(struct ext4_fs *fs, + struct ext4_mkfs_info *info) +{ + int ret; + struct ext4_inode_ref inode_ref; + uint64_t blocks_count; + + if (!info->journal) + return EOK; + + ret = ext4_fs_get_inode_ref(fs, EXT4_JOURNAL_INO, &inode_ref); + if (ret != EOK) + return ret; + + struct ext4_inode *inode = inode_ref.inode; + + ext4_inode_set_mode(&fs->sb, inode, EXT4_INODE_MODE_FILE | 0600); + ext4_inode_set_links_cnt(inode, 1); + + blocks_count = ext4_inode_get_blocks_count(&fs->sb, inode); + + while (blocks_count++ < info->journal_blocks) + { + ext4_fsblk_t fblock; + ext4_lblk_t iblock; + struct ext4_block blk; + + ret = ext4_fs_append_inode_dblk(&inode_ref, &fblock, &iblock); + if (ret != EOK) + goto Finish; + + if (iblock != 0) + continue; + + ret = ext4_block_get(fs->bdev, &blk, fblock); + if (ret != EOK) + goto Finish; + + + struct jbd_sb * jbd_sb = (struct jbd_sb * )blk.data; + memset(jbd_sb, 0, sizeof(struct jbd_sb)); + + jbd_sb->header.magic = to_be32(JBD_MAGIC_NUMBER); + jbd_sb->header.blocktype = to_be32(JBD_SUPERBLOCK_V2); + jbd_sb->blocksize = to_be32(info->block_size); + jbd_sb->maxlen = to_be32(info->journal_blocks); + jbd_sb->nr_users = to_be32(1); + jbd_sb->first = to_be32(1); + jbd_sb->sequence = to_be32(1); + + ext4_bcache_set_dirty(blk.buf); + ret = ext4_block_set(fs->bdev, &blk); + if (ret != EOK) + goto Finish; + } + + memcpy(fs->sb.journal_blocks, inode->blocks, sizeof(inode->blocks)); + + Finish: + ext4_fs_put_inode_ref(&inode_ref); + + return ret; +} + +int ext4_mkfs(struct ext4_fs *fs, struct ext4_blockdev *bd, + struct ext4_mkfs_info *info, int fs_type) +{ + int r; + + r = ext4_block_init(bd); + if (r != EOK) + return r; + + bd->fs = fs; + + if (info->len == 0) + info->len = bd->part_size; + + if (info->block_size == 0) + info->block_size = 4096; /*Set block size to default value*/ + + /* Round down the filesystem length to be a multiple of the block size */ + info->len &= ~((uint64_t)info->block_size - 1); + + if (info->journal_blocks == 0) + info->journal_blocks = compute_journal_blocks(info); + + if (info->blocks_per_group == 0) + info->blocks_per_group = compute_blocks_per_group(info); + + if (info->inodes == 0) + info->inodes = compute_inodes(info); + + if (info->inode_size == 0) + info->inode_size = 256; + + if (info->label == NULL) + info->label = ""; + + info->inodes_per_group = compute_inodes_per_group(info); + + switch (fs_type) { + case F_SET_EXT2: + info->feat_compat = EXT2_SUPPORTED_FCOM; + info->feat_ro_compat = EXT2_SUPPORTED_FRO_COM; + info->feat_incompat = EXT2_SUPPORTED_FINCOM; + break; + case F_SET_EXT3: + info->feat_compat = EXT3_SUPPORTED_FCOM; + info->feat_ro_compat = EXT3_SUPPORTED_FRO_COM; + info->feat_incompat = EXT3_SUPPORTED_FINCOM; + break; + case F_SET_EXT4: + info->feat_compat = EXT4_SUPPORTED_FCOM; + info->feat_ro_compat = EXT4_SUPPORTED_FRO_COM; + info->feat_incompat = EXT4_SUPPORTED_FINCOM; + break; + } + + /*TODO: handle this features some day...*/ + info->feat_incompat &= ~EXT4_FINCOM_META_BG; + info->feat_incompat &= ~EXT4_FINCOM_FLEX_BG; + info->feat_incompat &= ~EXT4_FINCOM_64BIT; + + info->feat_ro_compat &= ~EXT4_FRO_COM_METADATA_CSUM; + info->feat_ro_compat &= ~EXT4_FRO_COM_GDT_CSUM; + info->feat_ro_compat &= ~EXT4_FRO_COM_DIR_NLINK; + info->feat_ro_compat &= ~EXT4_FRO_COM_EXTRA_ISIZE; + info->feat_ro_compat &= ~EXT4_FRO_COM_HUGE_FILE; + + if (info->journal) + info->feat_compat |= EXT4_FCOM_HAS_JOURNAL; + + if (info->dsc_size == 0) { + + if (info->feat_incompat & EXT4_FINCOM_64BIT) + info->dsc_size = EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE; + else + info->dsc_size = EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE; + } + + info->bg_desc_reserve_blocks = 0; + + ext4_dbg(DEBUG_MKFS, DBG_INFO "Creating filesystem with parameters:\n"); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Size: %"PRIu64"\n", info->len); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Block size: %"PRIu32"\n", + info->block_size); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Blocks per group: %"PRIu32"\n", + info->blocks_per_group); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes per group: %"PRIu32"\n", + info->inodes_per_group); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Inode size: %"PRIu32"\n", + info->inode_size); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Inodes: %"PRIu32"\n", info->inodes); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Journal blocks: %"PRIu32"\n", + info->journal_blocks); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Features ro_compat: 0x%x\n", + info->feat_ro_compat); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Features compat: 0x%x\n", + info->feat_compat); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Features incompat: 0x%x\n", + info->feat_incompat); + ext4_dbg(DEBUG_MKFS, DBG_NONE "BG desc reserve: %"PRIu32"\n", + info->bg_desc_reserve_blocks); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Descriptor size: %"PRIu16"\n", + info->dsc_size); + ext4_dbg(DEBUG_MKFS, DBG_NONE "journal: %s\n", + info->journal ? "yes" : "no"); + ext4_dbg(DEBUG_MKFS, DBG_NONE "Label: %s\n", info->label); + + struct ext4_bcache bc; + + memset(&bc, 0, sizeof(struct ext4_bcache)); + ext4_block_set_lb_size(bd, info->block_size); + + r = ext4_bcache_init_dynamic(&bc, CONFIG_BLOCK_DEV_CACHE_SIZE, + info->block_size); + if (r != EOK) + goto block_fini; + + /*Bind block cache to block device*/ + r = ext4_block_bind_bcache(bd, &bc); + if (r != EOK) + goto cache_fini; + + r = ext4_block_cache_write_back(bd, 1); + if (r != EOK) + goto cache_fini; + + r = mkfs_init(bd, info); + if (r != EOK) + goto cache_fini; + + r = ext4_fs_init(fs, bd, false); + if (r != EOK) + goto cache_fini; + + r = init_bgs(fs); + if (r != EOK) + goto fs_fini; + + r = alloc_inodes(fs); + if (r != EOK) + goto fs_fini; + + r = create_dirs(fs); + if (r != EOK) + goto fs_fini; + + r = create_journal_inode(fs, info); + if (r != EOK) + goto fs_fini; + + fs_fini: + ext4_fs_fini(fs); + + cache_fini: + ext4_block_cache_write_back(bd, 0); + ext4_bcache_fini_dynamic(&bc); + + block_fini: + ext4_block_fini(bd); + + return r; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_super.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_super.c new file mode 100644 index 0000000..34cacd8 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_super.c @@ -0,0 +1,272 @@ +/* + * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * + * + * HelenOS: + * Copyright (c) 2012 Martin Sucha + * Copyright (c) 2012 Frantisek Princ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_super.h + * @brief Superblock operations. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_super.h" +#include "ext4_crc32.h" + +uint32_t ext4_block_group_cnt(struct ext4_sblock *s) +{ + uint64_t blocks_count = ext4_sb_get_blocks_cnt(s); + uint32_t blocks_per_group = ext4_get32(s, blocks_per_group); + + uint32_t block_groups_count = (uint32_t)(blocks_count / blocks_per_group); + + if (blocks_count % blocks_per_group) + block_groups_count++; + + return block_groups_count; +} + +uint32_t ext4_blocks_in_group_cnt(struct ext4_sblock *s, uint32_t bgid) +{ + uint32_t block_group_count = ext4_block_group_cnt(s); + uint32_t blocks_per_group = ext4_get32(s, blocks_per_group); + uint64_t total_blocks = ext4_sb_get_blocks_cnt(s); + + if (bgid < block_group_count - 1) + return blocks_per_group; + + return (uint32_t)(total_blocks - ((block_group_count - 1) * blocks_per_group)); +} + +uint32_t ext4_inodes_in_group_cnt(struct ext4_sblock *s, uint32_t bgid) +{ + uint32_t block_group_count = ext4_block_group_cnt(s); + uint32_t inodes_per_group = ext4_get32(s, inodes_per_group); + uint32_t total_inodes = ext4_get32(s, inodes_count); + + if (bgid < block_group_count - 1) + return inodes_per_group; + + return (total_inodes - ((block_group_count - 1) * inodes_per_group)); +} + +#if CONFIG_META_CSUM_ENABLE +static uint32_t ext4_sb_csum(struct ext4_sblock *s) +{ + + return ext4_crc32c(EXT4_CRC32_INIT, s, + offsetof(struct ext4_sblock, checksum)); +} +#else +#define ext4_sb_csum(...) 0 +#endif + +static bool ext4_sb_verify_csum(struct ext4_sblock *s) +{ + if (!ext4_sb_feature_ro_com(s, EXT4_FRO_COM_METADATA_CSUM)) + return true; + + if (s->checksum_type != to_le32(EXT4_CHECKSUM_CRC32C)) + return false; + + return s->checksum == to_le32(ext4_sb_csum(s)); +} + +static void ext4_sb_set_csum(struct ext4_sblock *s) +{ + if (!ext4_sb_feature_ro_com(s, EXT4_FRO_COM_METADATA_CSUM)) + return; + + s->checksum = to_le32(ext4_sb_csum(s)); +} + +int ext4_sb_write(struct ext4_blockdev *bdev, struct ext4_sblock *s) +{ + ext4_sb_set_csum(s); + return ext4_block_writebytes(bdev, EXT4_SUPERBLOCK_OFFSET, s, + EXT4_SUPERBLOCK_SIZE); +} + +int ext4_sb_read(struct ext4_blockdev *bdev, struct ext4_sblock *s) +{ + return ext4_block_readbytes(bdev, EXT4_SUPERBLOCK_OFFSET, s, + EXT4_SUPERBLOCK_SIZE); +} + +bool ext4_sb_check(struct ext4_sblock *s) +{ + if (ext4_get16(s, magic) != EXT4_SUPERBLOCK_MAGIC) + return false; + + if (ext4_get32(s, inodes_count) == 0) + return false; + + if (ext4_sb_get_blocks_cnt(s) == 0) + return false; + + if (ext4_get32(s, blocks_per_group) == 0) + return false; + + if (ext4_get32(s, inodes_per_group) == 0) + return false; + + if (ext4_get16(s, inode_size) < 128) + return false; + + if (ext4_get32(s, first_inode) < 11) + return false; + + if (ext4_sb_get_desc_size(s) < EXT4_MIN_BLOCK_GROUP_DESCRIPTOR_SIZE) + return false; + + if (ext4_sb_get_desc_size(s) > EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE) + return false; + + if (!ext4_sb_verify_csum(s)) + return false; + + return true; +} + +static inline int is_power_of(uint32_t a, uint32_t b) +{ + while (1) { + if (a < b) + return 0; + if (a == b) + return 1; + if ((a % b) != 0) + return 0; + a = a / b; + } +} + +bool ext4_sb_sparse(uint32_t group) +{ + if (group <= 1) + return 1; + + if (!(group & 1)) + return 0; + + return (is_power_of(group, 7) || is_power_of(group, 5) || + is_power_of(group, 3)); +} + +bool ext4_sb_is_super_in_bg(struct ext4_sblock *s, uint32_t group) +{ + if (ext4_sb_feature_ro_com(s, EXT4_FRO_COM_SPARSE_SUPER) && + !ext4_sb_sparse(group)) + return false; + return true; +} + +static uint32_t ext4_bg_num_gdb_meta(struct ext4_sblock *s, uint32_t group) +{ + uint32_t dsc_per_block = + ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s); + + uint32_t metagroup = group / dsc_per_block; + uint32_t first = metagroup * dsc_per_block; + uint32_t last = first + dsc_per_block - 1; + + if (group == first || group == first + 1 || group == last) + return 1; + return 0; +} + +static uint32_t ext4_bg_num_gdb_nometa(struct ext4_sblock *s, uint32_t group) +{ + if (!ext4_sb_is_super_in_bg(s, group)) + return 0; + uint32_t dsc_per_block = + ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s); + + uint32_t db_count = + (ext4_block_group_cnt(s) + dsc_per_block - 1) / dsc_per_block; + + if (ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG)) + return ext4_sb_first_meta_bg(s); + + return db_count; +} + +uint32_t ext4_bg_num_gdb(struct ext4_sblock *s, uint32_t group) +{ + uint32_t dsc_per_block = + ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s); + uint32_t first_meta_bg = ext4_sb_first_meta_bg(s); + uint32_t metagroup = group / dsc_per_block; + + if (!ext4_sb_feature_incom(s,EXT4_FINCOM_META_BG) || + metagroup < first_meta_bg) + return ext4_bg_num_gdb_nometa(s, group); + + return ext4_bg_num_gdb_meta(s, group); +} + +uint32_t ext4_num_base_meta_clusters(struct ext4_sblock *s, + uint32_t block_group) +{ + uint32_t num; + uint32_t dsc_per_block = + ext4_sb_get_block_size(s) / ext4_sb_get_desc_size(s); + + num = ext4_sb_is_super_in_bg(s, block_group); + + if (!ext4_sb_feature_incom(s, EXT4_FINCOM_META_BG) || + block_group < ext4_sb_first_meta_bg(s) * dsc_per_block) { + if (num) { + num += ext4_bg_num_gdb(s, block_group); + num += ext4_get16(s, s_reserved_gdt_blocks); + } + } else { + num += ext4_bg_num_gdb(s, block_group); + } + + uint32_t clustersize = 1024 << ext4_get32(s, log_cluster_size); + uint32_t cluster_ratio = clustersize / ext4_sb_get_block_size(s); + uint32_t v = + (num + cluster_ratio - 1) >> ext4_get32(s, log_cluster_size); + + return v; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_trans.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_trans.c new file mode 100644 index 0000000..7a91dbc --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_trans.c @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_trans.c + * @brief Ext4 transaction buffer operations. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_fs.h" +#include "ext4_journal.h" + +int ext4_trans_set_block_dirty(struct ext4_buf *buf) +{ + int r = EOK; +#if CONFIG_JOURNALING_ENABLE + struct ext4_fs *fs = buf->bc->bdev->fs; + struct ext4_block block = { + .lb_id = buf->lba, + .data = buf->data, + .buf = buf + }; + + if (fs->jbd_journal && fs->curr_trans) { + struct jbd_trans *trans = fs->curr_trans; + return jbd_trans_set_block_dirty(trans, &block); + } +#endif + ext4_bcache_set_dirty(buf); + return r; +} + +int ext4_trans_block_get_noread(struct ext4_blockdev *bdev, + struct ext4_block *b, + uint64_t lba) +{ + int r = ext4_block_get_noread(bdev, b, lba); + if (r != EOK) + return r; + + return r; +} + +int ext4_trans_block_get(struct ext4_blockdev *bdev, + struct ext4_block *b, + uint64_t lba) +{ + int r = ext4_block_get(bdev, b, lba); + if (r != EOK) + return r; + + return r; +} + +int ext4_trans_try_revoke_block(struct ext4_blockdev *bdev __unused, + uint64_t lba __unused) +{ + int r = EOK; +#if CONFIG_JOURNALING_ENABLE + struct ext4_fs *fs = bdev->fs; + if (fs->jbd_journal && fs->curr_trans) { + struct jbd_trans *trans = fs->curr_trans; + r = jbd_trans_try_revoke_block(trans, lba); + } else if (fs->jbd_journal) { + r = ext4_block_flush_lba(fs->bdev, lba); + } +#endif + return r; +} + +/** + * @} + */ diff --git a/code/drivers/fs/ext4/third_party/lwext4/src/ext4_xattr.c b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_xattr.c new file mode 100644 index 0000000..8a5ccf6 --- /dev/null +++ b/code/drivers/fs/ext4/third_party/lwext4/src/ext4_xattr.c @@ -0,0 +1,1564 @@ +/* + * Copyright (c) 2017 Grzegorz Kostka (kostka.grzegorz@gmail.com) + * Copyright (c) 2017 Kaho Ng (ngkaho1234@gmail.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + */ + +/** @addtogroup lwext4 + * @{ + */ +/** + * @file ext4_xattr.c + * @brief Extended Attribute manipulation. + */ + +#include "ext4_config.h" +#include "ext4_types.h" +#include "ext4_misc.h" +#include "ext4_errno.h" +#include "ext4_debug.h" + +#include "ext4_fs.h" +#include "ext4_trans.h" +#include "ext4_xattr.h" +#include "ext4_blockdev.h" +#include "ext4_super.h" +#include "ext4_crc32.h" +#include "ext4_block_group.h" +#include "ext4_balloc.h" +#include "ext4_inode.h" + +#include +#include + +#if CONFIG_XATTR_ENABLE + +/** + * @file ext4_xattr.c + * @brief Extended Attribute Manipulation + */ + +/* Extended Attribute(EA) */ + +/* Magic value in attribute blocks */ +#define EXT4_XATTR_MAGIC 0xEA020000 + +/* Maximum number of references to one attribute block */ +#define EXT4_XATTR_REFCOUNT_MAX 1024 + +/* Name indexes */ +#define EXT4_XATTR_INDEX_USER 1 +#define EXT4_XATTR_INDEX_POSIX_ACL_ACCESS 2 +#define EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT 3 +#define EXT4_XATTR_INDEX_TRUSTED 4 +#define EXT4_XATTR_INDEX_LUSTRE 5 +#define EXT4_XATTR_INDEX_SECURITY 6 +#define EXT4_XATTR_INDEX_SYSTEM 7 +#define EXT4_XATTR_INDEX_RICHACL 8 +#define EXT4_XATTR_INDEX_ENCRYPTION 9 + +#define EXT4_XATTR_PAD_BITS 2 +#define EXT4_XATTR_PAD (1 << EXT4_XATTR_PAD_BITS) +#define EXT4_XATTR_ROUND (EXT4_XATTR_PAD - 1) +#define EXT4_XATTR_LEN(name_len) \ + (((name_len) + EXT4_XATTR_ROUND + sizeof(struct ext4_xattr_entry)) & \ + ~EXT4_XATTR_ROUND) +#define EXT4_XATTR_NEXT(entry) \ + ((struct ext4_xattr_entry *)((char *)(entry) + \ + EXT4_XATTR_LEN((entry)->e_name_len))) +#define EXT4_XATTR_SIZE(size) (((size) + EXT4_XATTR_ROUND) & ~EXT4_XATTR_ROUND) +#define EXT4_XATTR_NAME(entry) ((char *)((entry) + 1)) + +#define EXT4_XATTR_IHDR(sb, raw_inode) \ + ((struct ext4_xattr_ibody_header *)((char *)raw_inode + \ + EXT4_GOOD_OLD_INODE_SIZE + \ + ext4_inode_get_extra_isize( \ + sb, raw_inode))) +#define EXT4_XATTR_IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr) + 1)) + +#define EXT4_XATTR_BHDR(block) ((struct ext4_xattr_header *)((block)->data)) +#define EXT4_XATTR_ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr)) +#define EXT4_XATTR_BFIRST(block) EXT4_XATTR_ENTRY(EXT4_XATTR_BHDR(block) + 1) +#define EXT4_XATTR_IS_LAST_ENTRY(entry) (*(uint32_t *)(entry) == 0) + +#define EXT4_ZERO_XATTR_VALUE ((void *)-1) + +#pragma pack(push, 1) + +struct ext4_xattr_header { + uint32_t h_magic; /* magic number for identification */ + uint32_t h_refcount; /* reference count */ + uint32_t h_blocks; /* number of disk blocks used */ + uint32_t h_hash; /* hash value of all attributes */ + uint32_t h_checksum; /* crc32c(uuid+id+xattrblock) */ + /* id = inum if refcount=1, blknum otherwise */ + uint32_t h_reserved[3]; /* zero right now */ +}; + +struct ext4_xattr_ibody_header { + uint32_t h_magic; /* magic number for identification */ +}; + +struct ext4_xattr_entry { + uint8_t e_name_len; /* length of name */ + uint8_t e_name_index; /* attribute name index */ + uint16_t e_value_offs; /* offset in disk block of value */ + uint32_t e_value_block; /* disk block attribute is stored on (n/i) */ + uint32_t e_value_size; /* size of attribute value */ + uint32_t e_hash; /* hash value of name and value */ +}; + +#pragma pack(pop) + + +#define NAME_HASH_SHIFT 5 +#define VALUE_HASH_SHIFT 16 + +static inline void ext4_xattr_compute_hash(struct ext4_xattr_header *header, + struct ext4_xattr_entry *entry) +{ + uint32_t hash = 0; + char *name = EXT4_XATTR_NAME(entry); + int n; + + for (n = 0; n < entry->e_name_len; n++) { + hash = (hash << NAME_HASH_SHIFT) ^ + (hash >> (8 * sizeof(hash) - NAME_HASH_SHIFT)) ^ *name++; + } + + if (entry->e_value_block == 0 && entry->e_value_size != 0) { + uint32_t *value = + (uint32_t *)((char *)header + to_le16(entry->e_value_offs)); + for (n = (to_le32(entry->e_value_size) + EXT4_XATTR_ROUND) >> + EXT4_XATTR_PAD_BITS; + n; n--) { + hash = (hash << VALUE_HASH_SHIFT) ^ + (hash >> (8 * sizeof(hash) - VALUE_HASH_SHIFT)) ^ + to_le32(*value++); + } + } + entry->e_hash = to_le32(hash); +} + +#define BLOCK_HASH_SHIFT 16 + +/* + * ext4_xattr_rehash() + * + * Re-compute the extended attribute hash value after an entry has changed. + */ +static void ext4_xattr_rehash(struct ext4_xattr_header *header, + struct ext4_xattr_entry *entry) +{ + struct ext4_xattr_entry *here; + uint32_t hash = 0; + + ext4_xattr_compute_hash(header, entry); + here = EXT4_XATTR_ENTRY(header + 1); + while (!EXT4_XATTR_IS_LAST_ENTRY(here)) { + if (!here->e_hash) { + /* Block is not shared if an entry's hash value == 0 */ + hash = 0; + break; + } + hash = (hash << BLOCK_HASH_SHIFT) ^ + (hash >> (8 * sizeof(hash) - BLOCK_HASH_SHIFT)) ^ + to_le32(here->e_hash); + here = EXT4_XATTR_NEXT(here); + } + header->h_hash = to_le32(hash); +} + +#if CONFIG_META_CSUM_ENABLE +static uint32_t ext4_xattr_block_checksum(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t blocknr, + struct ext4_xattr_header *header) +{ + uint32_t checksum = 0; + uint64_t le64_blocknr = blocknr; + struct ext4_sblock *sb = &inode_ref->fs->sb; + + if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) { + uint32_t orig_checksum; + + /* Preparation: temporarily set bg checksum to 0 */ + orig_checksum = header->h_checksum; + header->h_checksum = 0; + /* First calculate crc32 checksum against fs uuid */ + checksum = + ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid)); + /* Then calculate crc32 checksum block number */ + checksum = + ext4_crc32c(checksum, &le64_blocknr, sizeof(le64_blocknr)); + /* Finally calculate crc32 checksum against + * the entire xattr block */ + checksum = + ext4_crc32c(checksum, header, ext4_sb_get_block_size(sb)); + header->h_checksum = orig_checksum; + } + return checksum; +} +#else +#define ext4_xattr_block_checksum(...) 0 +#endif + +static void ext4_xattr_set_block_checksum(struct ext4_inode_ref *inode_ref, + ext4_fsblk_t blocknr __unused, + struct ext4_xattr_header *header) +{ + struct ext4_sblock *sb = &inode_ref->fs->sb; + if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) + return; + + header->h_checksum = + ext4_xattr_block_checksum(inode_ref, blocknr, header); +} + +struct xattr_prefix { + const char *prefix; + uint8_t name_index; +}; + +static const struct xattr_prefix prefix_tbl[] = { + {"user.", EXT4_XATTR_INDEX_USER}, + {"system.posix_acl_access", EXT4_XATTR_INDEX_POSIX_ACL_ACCESS}, + {"system.posix_acl_default", EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT}, + {"trusted.", EXT4_XATTR_INDEX_TRUSTED}, + {"security.", EXT4_XATTR_INDEX_SECURITY}, + {"system.", EXT4_XATTR_INDEX_SYSTEM}, + {"system.richacl", EXT4_XATTR_INDEX_RICHACL}, + {NULL, 0}, +}; + +const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len, + uint8_t *name_index, size_t *name_len, + bool *found) +{ + int i; + ext4_assert(name_index); + ext4_assert(found); + + *found = false; + + if (!full_name_len) { + if (name_len) + *name_len = 0; + + return NULL; + } + + for (i = 0; prefix_tbl[i].prefix; i++) { + size_t prefix_len = strlen(prefix_tbl[i].prefix); + if (full_name_len >= prefix_len && + !memcmp(full_name, prefix_tbl[i].prefix, prefix_len)) { + bool require_name = + prefix_tbl[i].prefix[prefix_len - 1] == '.'; + *name_index = prefix_tbl[i].name_index; + if (name_len) + *name_len = full_name_len - prefix_len; + + if (!(full_name_len - prefix_len) && require_name) + return NULL; + + *found = true; + if (require_name) + return full_name + prefix_len; + + return NULL; + } + } + if (name_len) + *name_len = 0; + + return NULL; +} + +const char *ext4_get_xattr_name_prefix(uint8_t name_index, + size_t *ret_prefix_len) +{ + int i; + + for (i = 0; prefix_tbl[i].prefix; i++) { + size_t prefix_len = strlen(prefix_tbl[i].prefix); + if (prefix_tbl[i].name_index == name_index) { + if (ret_prefix_len) + *ret_prefix_len = prefix_len; + + return prefix_tbl[i].prefix; + } + } + if (ret_prefix_len) + *ret_prefix_len = 0; + + return NULL; +} + +static const char ext4_xattr_empty_value; + +/** + * @brief Insert/Remove/Modify the given entry + * + * @param i The information of the given EA entry + * @param s Search context block + * @param dry_run Do not modify the content of the buffer + * + * @return Return EOK when finished, ENOSPC when there is no enough space + */ +static int ext4_xattr_set_entry(struct ext4_xattr_info *i, + struct ext4_xattr_search *s, bool dry_run) +{ + struct ext4_xattr_entry *last; + size_t free, min_offs = (char *)s->end - (char *)s->base, + name_len = i->name_len; + + /* + * If the entry is going to be removed but not found, return 0 to + * indicate success. + */ + if (!i->value && s->not_found) + return EOK; + + /* Compute min_offs and last. */ + last = s->first; + for (; !EXT4_XATTR_IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { + if (last->e_value_size) { + size_t offs = to_le16(last->e_value_offs); + if (offs < min_offs) + min_offs = offs; + } + } + + /* Calculate free space in the block. */ + free = min_offs - ((char *)last - (char *)s->base) - sizeof(uint32_t); + if (!s->not_found) + free += EXT4_XATTR_SIZE(s->here->e_value_size) + + EXT4_XATTR_LEN(s->here->e_name_len); + + if (i->value) { + /* See whether there is enough space to hold new entry */ + if (free < + EXT4_XATTR_SIZE(i->value_len) + EXT4_XATTR_LEN(name_len)) + return ENOSPC; + } + + /* Return EOK now if we do not intend to modify the content. */ + if (dry_run) + return EOK; + + /* First remove the old entry's data part */ + if (!s->not_found) { + size_t value_offs = to_le16(s->here->e_value_offs); + void *value = (char *)s->base + value_offs; + void *first_value = (char *)s->base + min_offs; + size_t value_size = + EXT4_XATTR_SIZE(to_le32(s->here->e_value_size)); + + if (value_offs) { + /* Remove the data part. */ + memmove((char *)first_value + value_size, first_value, + (char *)value - (char *)first_value); + + /* Zero the gap created */ + memset(first_value, 0, value_size); + + /* + * Calculate the new min_offs after removal of the old + * entry's data part + */ + min_offs += value_size; + } + + /* + * Adjust the value offset of entries which has value offset + * prior to the s->here. The offset of these entries won't be + * shifted if the size of the entry we removed is zero. + */ + for (last = s->first; !EXT4_XATTR_IS_LAST_ENTRY(last); + last = EXT4_XATTR_NEXT(last)) { + size_t offs = to_le16(last->e_value_offs); + + /* For zero-value-length entry, offs will be zero. */ + if (offs < value_offs) + last->e_value_offs = to_le16(offs + value_size); + } + } + + /* If caller wants us to insert... */ + if (i->value) { + size_t value_offs; + if (i->value_len) + value_offs = min_offs - EXT4_XATTR_SIZE(i->value_len); + else + value_offs = 0; + + if (!s->not_found) { + struct ext4_xattr_entry *here = s->here; + + /* Reuse the current entry we have got */ + here->e_value_offs = to_le16(value_offs); + here->e_value_size = to_le32(i->value_len); + } else { + /* Insert a new entry */ + last->e_name_len = (uint8_t)name_len; + last->e_name_index = i->name_index; + last->e_value_offs = to_le16(value_offs); + last->e_value_block = 0; + last->e_value_size = to_le32(i->value_len); + memcpy(EXT4_XATTR_NAME(last), i->name, name_len); + + /* Set valid last entry indicator */ + *(uint32_t *)EXT4_XATTR_NEXT(last) = 0; + + s->here = last; + } + + /* Insert the value's part */ + if (value_offs) { + memcpy((char *)s->base + value_offs, i->value, + i->value_len); + + /* Clear the padding bytes if there is */ + if (EXT4_XATTR_SIZE(i->value_len) != i->value_len) + memset((char *)s->base + value_offs + + i->value_len, + 0, EXT4_XATTR_SIZE(i->value_len) - + i->value_len); + } + } else { + size_t shift_offs; + + /* Remove the whole entry */ + shift_offs = (char *)EXT4_XATTR_NEXT(s->here) - (char *)s->here; + memmove(s->here, EXT4_XATTR_NEXT(s->here), + (char *)last + sizeof(uint32_t) - + (char *)EXT4_XATTR_NEXT(s->here)); + + /* Zero the gap created */ + memset((char *)last - shift_offs + sizeof(uint32_t), 0, + shift_offs); + + s->here = NULL; + } + + return EOK; +} + +static inline bool ext4_xattr_is_empty(struct ext4_xattr_search *s) +{ + if (!EXT4_XATTR_IS_LAST_ENTRY(s->first)) + return false; + + return true; +} + +/** + * @brief Find the entry according to given information + * + * @param i The information of the EA entry to be found, + * including name_index, name and the length of name + * @param s Search context block + */ +static void ext4_xattr_find_entry(struct ext4_xattr_info *i, + struct ext4_xattr_search *s) +{ + struct ext4_xattr_entry *entry = NULL; + + s->not_found = true; + s->here = NULL; + + /* + * Find the wanted EA entry by simply comparing the namespace, + * name and the length of name. + */ + for (entry = s->first; !EXT4_XATTR_IS_LAST_ENTRY(entry); + entry = EXT4_XATTR_NEXT(entry)) { + size_t name_len = entry->e_name_len; + const char *name = EXT4_XATTR_NAME(entry); + if (name_len == i->name_len && + entry->e_name_index == i->name_index && + !memcmp(name, i->name, name_len)) { + s->here = entry; + s->not_found = false; + i->value_len = to_le32(entry->e_value_size); + if (i->value_len) + i->value = (char *)s->base + + to_le16(entry->e_value_offs); + else + i->value = NULL; + + return; + } + } +} + +/** + * @brief Check whether the xattr block's content is valid + * + * @param inode_ref Inode reference + * @param block The block buffer to be validated + * + * @return true if @block is valid, false otherwise. + */ +static bool ext4_xattr_is_block_valid(struct ext4_inode_ref *inode_ref, + struct ext4_block *block) +{ + + void *base = block->data, + *end = block->data + ext4_sb_get_block_size(&inode_ref->fs->sb); + size_t min_offs = (char *)end - (char *)base; + struct ext4_xattr_header *header = EXT4_XATTR_BHDR(block); + struct ext4_xattr_entry *entry = EXT4_XATTR_BFIRST(block); + + /* + * Check whether the magic number in the header is correct. + */ + if (header->h_magic != to_le32(EXT4_XATTR_MAGIC)) + return false; + + /* + * The in-kernel filesystem driver only supports 1 block currently. + */ + if (header->h_blocks != to_le32(1)) + return false; + + /* + * Check if those entries are maliciously corrupted to inflict harm + * upon us. + */ + for (; !EXT4_XATTR_IS_LAST_ENTRY(entry); + entry = EXT4_XATTR_NEXT(entry)) { + if (!to_le32(entry->e_value_size) && + to_le16(entry->e_value_offs)) + return false; + + if ((char *)base + to_le16(entry->e_value_offs) + + to_le32(entry->e_value_size) > + (char *)end) + return false; + + /* + * The name length field should also be correct, + * also there should be an 4-byte zero entry at the + * end. + */ + if ((char *)EXT4_XATTR_NEXT(entry) + sizeof(uint32_t) > + (char *)end) + return false; + + if (to_le32(entry->e_value_size)) { + size_t offs = to_le16(entry->e_value_offs); + if (offs < min_offs) + min_offs = offs; + } + } + /* + * Entry field and data field do not override each other. + */ + if ((char *)base + min_offs < (char *)entry + sizeof(uint32_t)) + return false; + + return true; +} + +/** + * @brief Check whether the inode buffer's content is valid + * + * @param inode_ref Inode reference + * + * @return true if the inode buffer is valid, false otherwise. + */ +static bool ext4_xattr_is_ibody_valid(struct ext4_inode_ref *inode_ref) +{ + size_t min_offs; + void *base, *end; + struct ext4_fs *fs = inode_ref->fs; + struct ext4_xattr_ibody_header *iheader; + struct ext4_xattr_entry *entry; + size_t inode_size = ext4_get16(&fs->sb, inode_size); + + iheader = EXT4_XATTR_IHDR(&fs->sb, inode_ref->inode); + entry = EXT4_XATTR_IFIRST(iheader); + base = iheader; + end = (char *)inode_ref->inode + inode_size; + min_offs = (char *)end - (char *)base; + + /* + * Check whether the magic number in the header is correct. + */ + if (iheader->h_magic != to_le32(EXT4_XATTR_MAGIC)) + return false; + + /* + * Check if those entries are maliciously corrupted to inflict harm + * upon us. + */ + for (; !EXT4_XATTR_IS_LAST_ENTRY(entry); + entry = EXT4_XATTR_NEXT(entry)) { + if (!to_le32(entry->e_value_size) && + to_le16(entry->e_value_offs)) + return false; + + if ((char *)base + to_le16(entry->e_value_offs) + + to_le32(entry->e_value_size) > + (char *)end) + return false; + + /* + * The name length field should also be correct, + * also there should be an 4-byte zero entry at the + * end. + */ + if ((char *)EXT4_XATTR_NEXT(entry) + sizeof(uint32_t) > + (char *)end) + return false; + + if (to_le32(entry->e_value_size)) { + size_t offs = to_le16(entry->e_value_offs); + if (offs < min_offs) + min_offs = offs; + } + } + /* + * Entry field and data field do not override each other. + */ + if ((char *)base + min_offs < (char *)entry + sizeof(uint32_t)) + return false; + + return true; +} + +/** + * @brief An EA entry finder for inode buffer + */ +struct ext4_xattr_finder { + /** + * @brief The information of the EA entry to be find + */ + struct ext4_xattr_info i; + + /** + * @brief Search context block of the current search + */ + struct ext4_xattr_search s; + + /** + * @brief Inode reference to the corresponding inode + */ + struct ext4_inode_ref *inode_ref; +}; + +static void ext4_xattr_ibody_initialize(struct ext4_inode_ref *inode_ref) +{ + struct ext4_xattr_ibody_header *header; + struct ext4_fs *fs = inode_ref->fs; + size_t extra_isize = + ext4_inode_get_extra_isize(&fs->sb, inode_ref->inode); + size_t inode_size = ext4_get16(&fs->sb, inode_size); + if (!extra_isize) + return; + + header = EXT4_XATTR_IHDR(&fs->sb, inode_ref->inode); + memset(header, 0, inode_size - EXT4_GOOD_OLD_INODE_SIZE - extra_isize); + header->h_magic = to_le32(EXT4_XATTR_MAGIC); + inode_ref->dirty = true; +} + +/** + * @brief Initialize a given xattr block + * + * @param inode_ref Inode reference + * @param block xattr block buffer + */ +static void ext4_xattr_block_initialize(struct ext4_inode_ref *inode_ref, + struct ext4_block *block) +{ + struct ext4_xattr_header *header; + struct ext4_fs *fs = inode_ref->fs; + + memset(block->data, 0, ext4_sb_get_block_size(&fs->sb)); + + header = EXT4_XATTR_BHDR(block); + header->h_magic = to_le32(EXT4_XATTR_MAGIC); + header->h_refcount = to_le32(1); + header->h_blocks = to_le32(1); + + ext4_trans_set_block_dirty(block->buf); +} + +static void ext4_xattr_block_init_search(struct ext4_inode_ref *inode_ref, + struct ext4_xattr_search *s, + struct ext4_block *block) +{ + s->base = block->data; + s->end = block->data + ext4_sb_get_block_size(&inode_ref->fs->sb); + s->first = EXT4_XATTR_BFIRST(block); + s->here = NULL; + s->not_found = true; +} + +/** + * @brief Find an EA entry inside a xattr block + * + * @param inode_ref Inode reference + * @param finder The caller-provided finder block with + * information filled + * @param block The block buffer to be looked into + * + * @return Return EOK no matter the entry is found or not. + * If the IO operation or the buffer validation failed, + * return other value. + */ +static int ext4_xattr_block_find_entry(struct ext4_inode_ref *inode_ref, + struct ext4_xattr_finder *finder, + struct ext4_block *block) +{ + int ret = EOK; + + /* Initialize the caller-given finder */ + finder->inode_ref = inode_ref; + memset(&finder->s, 0, sizeof(finder->s)); + + if (ret != EOK) + return ret; + + /* Check the validity of the buffer */ + if (!ext4_xattr_is_block_valid(inode_ref, block)) + return EIO; + + ext4_xattr_block_init_search(inode_ref, &finder->s, block); + ext4_xattr_find_entry(&finder->i, &finder->s); + return EOK; +} + +/** + * @brief Find an EA entry inside an inode's extra space + * + * @param inode_ref Inode reference + * @param finder The caller-provided finder block with + * information filled + * + * @return Return EOK no matter the entry is found or not. + * If the IO operation or the buffer validation failed, + * return other value. + */ +static int ext4_xattr_ibody_find_entry(struct ext4_inode_ref *inode_ref, + struct ext4_xattr_finder *finder) +{ + struct ext4_fs *fs = inode_ref->fs; + struct ext4_xattr_ibody_header *iheader; + size_t extra_isize = + ext4_inode_get_extra_isize(&fs->sb, inode_ref->inode); + size_t inode_size = ext4_get16(&fs->sb, inode_size); + + /* Initialize the caller-given finder */ + finder->inode_ref = inode_ref; + memset(&finder->s, 0, sizeof(finder->s)); + + /* + * If there is no extra inode space + * set ext4_xattr_ibody_finder::s::not_found to true and return EOK + */ + if (!extra_isize) { + finder->s.not_found = true; + return EOK; + } + + /* Check the validity of the buffer */ + if (!ext4_xattr_is_ibody_valid(inode_ref)) + return EIO; + + iheader = EXT4_XATTR_IHDR(&fs->sb, inode_ref->inode); + finder->s.base = EXT4_XATTR_IFIRST(iheader); + finder->s.end = (char *)inode_ref->inode + inode_size; + finder->s.first = EXT4_XATTR_IFIRST(iheader); + ext4_xattr_find_entry(&finder->i, &finder->s); + return EOK; +} + +/** + * @brief Try to allocate a block holding EA entries. + * + * @param inode_ref Inode reference + * + * @return Error code + */ +static int ext4_xattr_try_alloc_block(struct ext4_inode_ref *inode_ref) +{ + int ret = EOK; + + ext4_fsblk_t xattr_block = 0; + xattr_block = + ext4_inode_get_file_acl(inode_ref->inode, &inode_ref->fs->sb); + + /* + * Only allocate a xattr block when there is no xattr block + * used by the inode. + */ + if (!xattr_block) { + ext4_fsblk_t goal = ext4_fs_inode_to_goal_block(inode_ref); + + ret = ext4_balloc_alloc_block(inode_ref, goal, &xattr_block); + if (ret != EOK) + goto Finish; + + ext4_inode_set_file_acl(inode_ref->inode, &inode_ref->fs->sb, + xattr_block); + } + +Finish: + return ret; +} + +/** + * @brief Try to free a block holding EA entries. + * + * @param inode_ref Inode reference + * + * @return Error code + */ +static void ext4_xattr_try_free_block(struct ext4_inode_ref *inode_ref) +{ + ext4_fsblk_t xattr_block; + xattr_block = + ext4_inode_get_file_acl(inode_ref->inode, &inode_ref->fs->sb); + /* + * Free the xattr block used by the inode when there is one. + */ + if (xattr_block) { + ext4_inode_set_file_acl(inode_ref->inode, &inode_ref->fs->sb, + 0); + ext4_balloc_free_block(inode_ref, xattr_block); + inode_ref->dirty = true; + } +} + +/** + * @brief Put a list of EA entries into a caller-provided buffer + * In order to make sure that @list buffer can fit in the data, + * the routine should be called twice. + * + * @param inode_ref Inode reference + * @param list A caller-provided buffer to hold a list of EA entries. + * If list == NULL, list_len will contain the size of + * the buffer required to hold these entries + * @param list_len The length of the data written to @list + * @return Error code + */ +int ext4_xattr_list(struct ext4_inode_ref *inode_ref, + struct ext4_xattr_list_entry *list, size_t *list_len) +{ + int ret = EOK; + size_t buf_len = 0; + struct ext4_fs *fs = inode_ref->fs; + struct ext4_xattr_ibody_header *iheader; + size_t extra_isize = + ext4_inode_get_extra_isize(&fs->sb, inode_ref->inode); + struct ext4_block block; + bool block_loaded = false; + ext4_fsblk_t xattr_block = 0; + struct ext4_xattr_entry *entry; + struct ext4_xattr_list_entry *list_prev = NULL; + xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb); + + /* + * If there is extra inode space and the xattr buffer in the + * inode is valid. + */ + if (extra_isize && ext4_xattr_is_ibody_valid(inode_ref)) { + iheader = EXT4_XATTR_IHDR(&fs->sb, inode_ref->inode); + entry = EXT4_XATTR_IFIRST(iheader); + + /* + * The format of the list should be like this: + * + * name_len indicates the length in bytes of the name + * of the EA entry. The string is null-terminated. + * + * list->name => (char *)(list + 1); + * list->next => (void *)((char *)(list + 1) + name_len + 1); + */ + for (; !EXT4_XATTR_IS_LAST_ENTRY(entry); + entry = EXT4_XATTR_NEXT(entry)) { + size_t name_len = entry->e_name_len; + if (list) { + list->name_index = entry->e_name_index; + list->name_len = name_len; + list->name = (char *)(list + 1); + memcpy(list->name, EXT4_XATTR_NAME(entry), + list->name_len); + + if (list_prev) + list_prev->next = list; + + list_prev = list; + list = (struct ext4_xattr_list_entry + *)(list->name + name_len + 1); + } + + /* + * Size calculation by pointer arithmetics. + */ + buf_len += + (char *)((struct ext4_xattr_list_entry *)0 + 1) + + name_len + 1 - + (char *)(struct ext4_xattr_list_entry *)0; + } + } + + /* + * If there is a xattr block used by the inode + */ + if (xattr_block) { + ret = ext4_trans_block_get(fs->bdev, &block, xattr_block); + if (ret != EOK) + goto out; + + block_loaded = true; + + /* + * As we don't allow the content in the block being invalid, + * bail out. + */ + if (!ext4_xattr_is_block_valid(inode_ref, &block)) { + ret = EIO; + goto out; + } + + entry = EXT4_XATTR_BFIRST(&block); + + /* + * The format of the list should be like this: + * + * name_len indicates the length in bytes of the name + * of the EA entry. The string is null-terminated. + * + * list->name => (char *)(list + 1); + * list->next => (void *)((char *)(list + 1) + name_len + 1); + * + * Same as above actually. + */ + for (; !EXT4_XATTR_IS_LAST_ENTRY(entry); + entry = EXT4_XATTR_NEXT(entry)) { + size_t name_len = entry->e_name_len; + if (list) { + list->name_index = entry->e_name_index; + list->name_len = name_len; + list->name = (char *)(list + 1); + memcpy(list->name, EXT4_XATTR_NAME(entry), + list->name_len); + + if (list_prev) + list_prev->next = list; + + list_prev = list; + list = (struct ext4_xattr_list_entry + *)(list->name + name_len + 1); + } + + /* + * Size calculation by pointer arithmetics. + */ + buf_len += + (char *)((struct ext4_xattr_list_entry *)0 + 1) + + name_len + 1 - + (char *)(struct ext4_xattr_list_entry *)0; + } + } + if (list_prev) + list_prev->next = NULL; +out: + if (ret == EOK && list_len) + *list_len = buf_len; + + if (block_loaded) + ext4_block_set(fs->bdev, &block); + + return ret; +} + +/** + * @brief Query EA entry's value with given name-index and name + * + * @param inode_ref Inode reference + * @param name_index Name-index + * @param name Name of the EA entry to be queried + * @param name_len Length of name in bytes + * @param buf Output buffer to hold content + * @param buf_len Output buffer's length + * @param data_len The length of data of the EA entry found + * + * @return Error code + */ +int ext4_xattr_get(struct ext4_inode_ref *inode_ref, uint8_t name_index, + const char *name, size_t name_len, void *buf, size_t buf_len, + size_t *data_len) +{ + int ret = EOK; + struct ext4_xattr_finder ibody_finder; + struct ext4_xattr_finder block_finder; + struct ext4_xattr_info i; + size_t value_len = 0; + size_t value_offs = 0; + struct ext4_fs *fs = inode_ref->fs; + ext4_fsblk_t xattr_block; + xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb); + + i.name_index = name_index; + i.name = name; + i.name_len = name_len; + i.value = 0; + i.value_len = 0; + if (data_len) + *data_len = 0; + + ibody_finder.i = i; + ret = ext4_xattr_ibody_find_entry(inode_ref, &ibody_finder); + if (ret != EOK) + goto out; + + if (!ibody_finder.s.not_found) { + value_len = to_le32(ibody_finder.s.here->e_value_size); + value_offs = to_le32(ibody_finder.s.here->e_value_offs); + if (buf_len && buf) { + void *data_loc = + (char *)ibody_finder.s.base + value_offs; + memcpy(buf, data_loc, + (buf_len < value_len) ? buf_len : value_len); + } + } else { + struct ext4_block block; + + /* Return ENODATA if there is no EA block */ + if (!xattr_block) { + ret = ENODATA; + goto out; + } + + block_finder.i = i; + ret = ext4_trans_block_get(fs->bdev, &block, xattr_block); + if (ret != EOK) + goto out; + + ret = ext4_xattr_block_find_entry(inode_ref, &block_finder, + &block); + if (ret != EOK) { + ext4_block_set(fs->bdev, &block); + goto out; + } + + /* Return ENODATA if entry is not found */ + if (block_finder.s.not_found) { + ext4_block_set(fs->bdev, &block); + ret = ENODATA; + goto out; + } + + value_len = to_le32(block_finder.s.here->e_value_size); + value_offs = to_le32(block_finder.s.here->e_value_offs); + if (buf_len && buf) { + void *data_loc = + (char *)block_finder.s.base + value_offs; + memcpy(buf, data_loc, + (buf_len < value_len) ? buf_len : value_len); + } + + /* + * Free the xattr block buffer returned by + * ext4_xattr_block_find_entry. + */ + ext4_block_set(fs->bdev, &block); + } + +out: + if (ret == EOK && data_len) + *data_len = value_len; + + return ret; +} + +/** + * @brief Try to copy the content of an xattr block to a newly-allocated + * block. If the operation fails, the block buffer provided by + * caller will be freed + * + * @param inode_ref Inode reference + * @param block The block buffer reference + * @param new_block The newly-allocated block buffer reference + * @param orig_block The block number of @block + * @param allocated a new block is allocated + * + * @return Error code + */ +static int ext4_xattr_copy_new_block(struct ext4_inode_ref *inode_ref, + struct ext4_block *block, + struct ext4_block *new_block, + ext4_fsblk_t *orig_block, bool *allocated) +{ + int ret = EOK; + ext4_fsblk_t xattr_block = 0; + struct ext4_xattr_header *header; + struct ext4_fs *fs = inode_ref->fs; + header = EXT4_XATTR_BHDR(block); + + if (orig_block) + *orig_block = block->lb_id; + + if (allocated) + *allocated = false; + + /* Only do copy when a block is referenced by more than one inode. */ + if (to_le32(header->h_refcount) > 1) { + ext4_fsblk_t goal = ext4_fs_inode_to_goal_block(inode_ref); + + /* Allocate a new block to be used by this inode */ + ret = ext4_balloc_alloc_block(inode_ref, goal, &xattr_block); + if (ret != EOK) + goto out; + + ret = ext4_trans_block_get(fs->bdev, new_block, xattr_block); + if (ret != EOK) + goto out; + + /* Copy the content of the whole block */ + memcpy(new_block->data, block->data, + ext4_sb_get_block_size(&inode_ref->fs->sb)); + + /* + * Decrement the reference count of the original xattr block + * by one + */ + header->h_refcount = to_le32(to_le32(header->h_refcount) - 1); + ext4_trans_set_block_dirty(block->buf); + ext4_trans_set_block_dirty(new_block->buf); + + header = EXT4_XATTR_BHDR(new_block); + header->h_refcount = to_le32(1); + + if (allocated) + *allocated = true; + } +out: + if (xattr_block) { + if (ret != EOK) + ext4_balloc_free_block(inode_ref, xattr_block); + else { + /* + * Modify the in-inode pointer to point to the new xattr block + */ + ext4_inode_set_file_acl(inode_ref->inode, &fs->sb, xattr_block); + inode_ref->dirty = true; + } + } + + return ret; +} + +/** + * @brief Given an EA entry's name, remove the EA entry + * + * @param inode_ref Inode reference + * @param name_index Name-index + * @param name Name of the EA entry to be removed + * @param name_len Length of name in bytes + * + * @return Error code + */ +int ext4_xattr_remove(struct ext4_inode_ref *inode_ref, uint8_t name_index, + const char *name, size_t name_len) +{ + int ret = EOK; + struct ext4_block block; + struct ext4_xattr_finder ibody_finder; + struct ext4_xattr_finder block_finder; + bool use_block = false; + bool block_loaded = false; + struct ext4_xattr_info i; + struct ext4_fs *fs = inode_ref->fs; + ext4_fsblk_t xattr_block; + + xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb); + + i.name_index = name_index; + i.name = name; + i.name_len = name_len; + i.value = NULL; + i.value_len = 0; + + ibody_finder.i = i; + block_finder.i = i; + + ret = ext4_xattr_ibody_find_entry(inode_ref, &ibody_finder); + if (ret != EOK) + goto out; + + if (ibody_finder.s.not_found && xattr_block) { + ret = ext4_trans_block_get(fs->bdev, &block, xattr_block); + if (ret != EOK) + goto out; + + block_loaded = true; + block_finder.i = i; + ret = ext4_xattr_block_find_entry(inode_ref, &block_finder, + &block); + if (ret != EOK) + goto out; + + /* Return ENODATA if entry is not found */ + if (block_finder.s.not_found) { + ret = ENODATA; + goto out; + } + use_block = true; + } + + if (use_block) { + bool allocated = false; + struct ext4_block new_block; + + /* + * There will be no effect when the xattr block is only referenced + * once. + */ + ret = ext4_xattr_copy_new_block(inode_ref, &block, &new_block, + &xattr_block, &allocated); + if (ret != EOK) + goto out; + + if (!allocated) { + /* Prevent double-freeing */ + block_loaded = false; + new_block = block; + } + + ret = ext4_xattr_block_find_entry(inode_ref, &block_finder, + &new_block); + if (ret != EOK) + goto out; + + /* Now remove the entry */ + ext4_xattr_set_entry(&i, &block_finder.s, false); + + if (ext4_xattr_is_empty(&block_finder.s)) { + ext4_block_set(fs->bdev, &new_block); + ext4_xattr_try_free_block(inode_ref); + } else { + struct ext4_xattr_header *header = + EXT4_XATTR_BHDR(&new_block); + header = EXT4_XATTR_BHDR(&new_block); + ext4_assert(block_finder.s.first); + ext4_xattr_rehash(header, block_finder.s.first); + ext4_xattr_set_block_checksum(inode_ref, + block.lb_id, + header); + + ext4_trans_set_block_dirty(new_block.buf); + ext4_block_set(fs->bdev, &new_block); + } + + } else { + /* Now remove the entry */ + ext4_xattr_set_entry(&i, &block_finder.s, false); + inode_ref->dirty = true; + } +out: + if (block_loaded) + ext4_block_set(fs->bdev, &block); + + return ret; +} + +/** + * @brief Insert/overwrite an EA entry into/in a xattr block + * + * @param inode_ref Inode reference + * @param i The information of the given EA entry + * + * @return Error code + */ +static int ext4_xattr_block_set(struct ext4_inode_ref *inode_ref, + struct ext4_xattr_info *i, + bool no_insert) +{ + int ret = EOK; + bool allocated = false; + struct ext4_fs *fs = inode_ref->fs; + struct ext4_block block, new_block; + ext4_fsblk_t orig_xattr_block; + + orig_xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb); + + ext4_assert(i->value); + if (!orig_xattr_block) { + struct ext4_xattr_search s; + struct ext4_xattr_header *header; + + /* If insertion of new entry is not allowed... */ + if (no_insert) { + ret = ENODATA; + goto out; + } + + ret = ext4_xattr_try_alloc_block(inode_ref); + if (ret != EOK) + goto out; + + orig_xattr_block = + ext4_inode_get_file_acl(inode_ref->inode, &fs->sb); + ret = ext4_trans_block_get(fs->bdev, &block, orig_xattr_block); + if (ret != EOK) { + ext4_xattr_try_free_block(inode_ref); + goto out; + } + + ext4_xattr_block_initialize(inode_ref, &block); + ext4_xattr_block_init_search(inode_ref, &s, &block); + + ret = ext4_xattr_set_entry(i, &s, false); + if (ret == EOK) { + header = EXT4_XATTR_BHDR(&block); + + ext4_assert(s.here); + ext4_assert(s.first); + ext4_xattr_compute_hash(header, s.here); + ext4_xattr_rehash(header, s.first); + ext4_xattr_set_block_checksum(inode_ref, + block.lb_id, + header); + ext4_trans_set_block_dirty(block.buf); + } + ext4_block_set(fs->bdev, &block); + if (ret != EOK) + ext4_xattr_try_free_block(inode_ref); + + } else { + struct ext4_xattr_finder finder; + struct ext4_xattr_header *header; + finder.i = *i; + ret = ext4_trans_block_get(fs->bdev, &block, orig_xattr_block); + if (ret != EOK) + goto out; + + header = EXT4_XATTR_BHDR(&block); + + /* + * Consider the following case when insertion of new + * entry is not allowed + */ + if (to_le32(header->h_refcount) > 1 && no_insert) { + /* + * There are other people referencing the + * same xattr block + */ + ret = ext4_xattr_block_find_entry(inode_ref, &finder, &block); + if (ret != EOK) { + ext4_block_set(fs->bdev, &block); + goto out; + } + if (finder.s.not_found) { + ext4_block_set(fs->bdev, &block); + ret = ENODATA; + goto out; + } + } + + /* + * There will be no effect when the xattr block is only referenced + * once. + */ + ret = ext4_xattr_copy_new_block(inode_ref, &block, &new_block, + &orig_xattr_block, &allocated); + if (ret != EOK) { + ext4_block_set(fs->bdev, &block); + goto out; + } + + if (allocated) { + ext4_block_set(fs->bdev, &block); + new_block = block; + } + + ret = ext4_xattr_block_find_entry(inode_ref, &finder, &block); + if (ret != EOK) { + ext4_block_set(fs->bdev, &block); + goto out; + } + + ret = ext4_xattr_set_entry(i, &finder.s, false); + if (ret == EOK) { + header = EXT4_XATTR_BHDR(&block); + + ext4_assert(finder.s.here); + ext4_assert(finder.s.first); + ext4_xattr_compute_hash(header, finder.s.here); + ext4_xattr_rehash(header, finder.s.first); + ext4_xattr_set_block_checksum(inode_ref, + block.lb_id, + header); + ext4_trans_set_block_dirty(block.buf); + } + ext4_block_set(fs->bdev, &block); + } +out: + return ret; +} + +/** + * @brief Remove an EA entry from a xattr block + * + * @param inode_ref Inode reference + * @param i The information of the given EA entry + * + * @return Error code + */ +static int ext4_xattr_block_remove(struct ext4_inode_ref *inode_ref, + struct ext4_xattr_info *i) +{ + int ret = EOK; + bool allocated = false; + const void *value = i->value; + struct ext4_fs *fs = inode_ref->fs; + struct ext4_xattr_finder finder; + struct ext4_block block, new_block; + struct ext4_xattr_header *header; + ext4_fsblk_t orig_xattr_block; + orig_xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb); + + ext4_assert(orig_xattr_block); + ret = ext4_trans_block_get(fs->bdev, &block, orig_xattr_block); + if (ret != EOK) + goto out; + + /* + * There will be no effect when the xattr block is only referenced + * once. + */ + ret = ext4_xattr_copy_new_block(inode_ref, &block, &new_block, + &orig_xattr_block, &allocated); + if (ret != EOK) { + ext4_block_set(fs->bdev, &block); + goto out; + } + + if (allocated) { + ext4_block_set(fs->bdev, &block); + block = new_block; + } + + ext4_xattr_block_find_entry(inode_ref, &finder, &block); + + if (!finder.s.not_found) { + i->value = NULL; + ret = ext4_xattr_set_entry(i, &finder.s, false); + i->value = value; + + header = EXT4_XATTR_BHDR(&block); + ext4_assert(finder.s.first); + ext4_xattr_rehash(header, finder.s.first); + ext4_xattr_set_block_checksum(inode_ref, + block.lb_id, + header); + ext4_trans_set_block_dirty(block.buf); + } + + ext4_block_set(fs->bdev, &block); +out: + return ret; +} + +/** + * @brief Insert an EA entry into a given inode reference + * + * @param inode_ref Inode reference + * @param name_index Name-index + * @param name Name of the EA entry to be inserted + * @param name_len Length of name in bytes + * @param value Input buffer to hold content + * @param value_len Length of input content + * + * @return Error code + */ +int ext4_xattr_set(struct ext4_inode_ref *inode_ref, uint8_t name_index, + const char *name, size_t name_len, const void *value, + size_t value_len) +{ + int ret = EOK; + struct ext4_fs *fs = inode_ref->fs; + struct ext4_xattr_finder ibody_finder; + struct ext4_xattr_info i; + bool block_found = false; + ext4_fsblk_t orig_xattr_block; + size_t extra_isize = + ext4_inode_get_extra_isize(&fs->sb, inode_ref->inode); + + i.name_index = name_index; + i.name = name; + i.name_len = name_len; + i.value = (value_len) ? value : &ext4_xattr_empty_value; + i.value_len = value_len; + + ibody_finder.i = i; + + orig_xattr_block = ext4_inode_get_file_acl(inode_ref->inode, &fs->sb); + + /* + * Even if entry is not found, search context block inside the + * finder is still valid and can be used to insert entry. + */ + ret = ext4_xattr_ibody_find_entry(inode_ref, &ibody_finder); + if (ret != EOK) { + ext4_xattr_ibody_initialize(inode_ref); + ext4_xattr_ibody_find_entry(inode_ref, &ibody_finder); + } + + if (ibody_finder.s.not_found) { + if (orig_xattr_block) { + block_found = true; + ret = ext4_xattr_block_set(inode_ref, &i, true); + if (ret == ENOSPC) + goto try_insert; + else if (ret == ENODATA) + goto try_insert; + else if (ret != EOK) + goto out; + + } else + goto try_insert; + + } else { + try_insert: + /* Only try to set entry in ibody if inode is sufficiently large */ + if (extra_isize) + ret = ext4_xattr_set_entry(&i, &ibody_finder.s, false); + else + ret = ENOSPC; + + if (ret == ENOSPC) { + if (!block_found) { + ret = ext4_xattr_block_set(inode_ref, &i, false); + ibody_finder.i.value = NULL; + ext4_xattr_set_entry(&ibody_finder.i, + &ibody_finder.s, false); + inode_ref->dirty = true; + } + + } else if (ret == EOK) { + if (block_found) + ret = ext4_xattr_block_remove(inode_ref, &i); + + inode_ref->dirty = true; + } + } + +out: + return ret; +} + +#endif + +/** + * @} + */ diff --git a/code/drivers/fs/fat/fat.c b/code/drivers/fs/fat/fat.c index 5b2b87e..5930eaa 100644 --- a/code/drivers/fs/fat/fat.c +++ b/code/drivers/fs/fat/fat.c @@ -1,22 +1,11 @@ -// FAT filesystem driver. This file owns filesystem operations and FAT-backed file access. +// FAT allocation, cache, and metadata core shared by the filesystem operation translation units. -#include "fat.h" - -#include "fat_disk.h" - -#ifndef FAT_DEBUG -#define FAT_DEBUG 0 -#endif - -#define FatDebugPrint(...) do { if (FAT_DEBUG) DbgPrint(__VA_ARGS__); } while (0) - -// FAT_VOL forward declaration (needed by cache helpers). -typedef struct FAT_VOL_S FAT_VOL; +#include "fat_internal.h" // FAT sector cache (fixes #1 and #5) // 32 direct-mapped slots, indexed by (lba % FAT_CACHE_SLOTS). -// Write-back: WriteFatEntry modifies the in-cache bytes and marks the slot -// dirty. FlushFatCache() propagates dirty slots to every FAT copy at once, +// Write-back: FatWriteEntry modifies the in-cache bytes and marks the slot +// dirty. FatFlushCache() propagates dirty slots to every FAT copy at once, // replacing the original per-entry 2x(read+write) pattern with a single // write per dirty sector per FAT copy. @@ -32,33 +21,6 @@ typedef struct { static FAT_CACHE_ENTRY gFatCache[FAT_CACHE_SLOTS]; -#define FAT_PATH_CACHE_SLOTS 64u -#define FAT_PATH_CACHE_MAX 512u -#define FAT_CHAIN_CACHE_SLOTS 16u -#define FAT_WRITE_RUN_MAX_SECTORS 128u -#define FAT_READ_RUN_MAX_SECTORS 512u - -typedef struct FAT_PATH_CACHE_ENTRY { - bool Valid; - uint64_t Generation; - uint32_t Hash; - size_t PathLength; - char Path[FAT_PATH_CACHE_MAX]; - uint32_t Cluster; - uint32_t Size; - uint8_t Attr; - uint64_t DirentLba; - uint32_t DirentOffset; -} FAT_PATH_CACHE_ENTRY; - -typedef struct FAT_CHAIN_CACHE_ENTRY { - bool Valid; - uint32_t LogicalStart; - uint32_t PhysicalStart; - uint32_t Length; - uint32_t NextCluster; -} FAT_CHAIN_CACHE_ENTRY; - // The FAT sector cache is global and direct-mapped. mmap-backed video can fault // file pages from multiple Bink worker threads at the same time; without // serialization one thread can evict/reload a cache slot while another thread is @@ -118,7 +80,7 @@ static KSTATUS FatCacheRead(FAT_VOL* v, uint64_t lba, uint8_t** out) { } // Evict current occupant: single-copy writeback on collision. - // The full multi-copy flush happens in FlushFatCache(). + // The full multi-copy flush happens in FatFlushCache(). if (e->valid && e->dirty && e->vol) { WriteSectorsRaw(e->vol, e->lba, 1, e->data); e->dirty = false; @@ -143,7 +105,7 @@ static void FatCacheMarkDirty(FAT_VOL* v, uint64_t lba) { } // Invalidates every FAT sector-cache entry. -static void FatCacheInvalidate(void) { +void FatCacheInvalidate(void) { int held = FatCacheAcquire(); for (int i = 0; i < FAT_CACHE_SLOTS; i++) { gFatCache[i].vol = (FAT_VOL*)0; @@ -154,7 +116,7 @@ static void FatCacheInvalidate(void) { } // Discards cache entries owned by one dead or destroyed mounted FAT instance. -static void FatCacheInvalidateVolume(FAT_VOL* volume) { +void FatCacheInvalidateVolume(FAT_VOL* volume) { if (!volume) return; int held = FatCacheAcquire(); @@ -168,46 +130,9 @@ static void FatCacheInvalidateVolume(FAT_VOL* volume) { FatCacheRelease(held); } -// FlushFatCache — write every dirty slot to all FAT copies, clear dirty flag. +// FatFlushCache — write every dirty slot to all FAT copies, clear dirty flag. // Defined after FAT_VOL is fully declared (below). -static void FlushFatCache(FAT_VOL* v); - -// Driver state. -struct FAT_VOL_S { - FAT_VARIANT Variant; - uint8_t DriveIndex; - uint64_t PartitionLba; // absolute LBA of the mounted volume - uint64_t PartitionSize; // sectors, best-effort bounds/documentation - uint32_t BytesPerSector; - uint32_t SectorsPerCluster; - uint32_t BytesPerCluster; - uint32_t ReservedSectors; - uint32_t NumFats; - uint32_t FatSize; // sectors per FAT copy - uint32_t RootCluster; - uint32_t RootEntryCount; - uint32_t RootDirSectors; - uint64_t RootDirStart; - uint64_t DataStart; // absolute LBA of cluster 2 - uint32_t TotalClusters; // usable data clusters - uint32_t NextFreeHint; // fix #3: start cluster for next free-scan - SPINLOCK PathCacheLock; - uint64_t PathCacheGeneration; - IO_FILESYSTEM_STATS Stats; - FAT_PATH_CACHE_ENTRY PathCache[FAT_PATH_CACHE_SLOTS]; - IO_VOLUME* Volume; // Active mount-list reference. - LIST_ENTRY MountLink; -}; -typedef struct { - uint32_t StartCluster; - uint32_t FileSize; - FAT_VOL* Vol; - uint64_t DirentLba; - uint32_t DirentOffset; - SPINLOCK ChainCacheLock; - uint32_t ChainCacheClock; - FAT_CHAIN_CACHE_ENTRY ChainCache[FAT_CHAIN_CACHE_SLOTS]; -} FAT_FILE; +void FatFlushCache(FAT_VOL* v); // Raw sector I/O (bypasses FAT cache; used for file data and cache eviction). static KSTATUS ReadSectorsRaw(FAT_VOL* v, uint64_t lba, @@ -223,11 +148,11 @@ static KSTATUS WriteSectorsRaw(FAT_VOL* v, uint64_t lba, } // Convenience aliases for file-data I/O. -static KSTATUS ReadSectors (FAT_VOL* v, uint64_t lba, uint32_t n, void* b) { return ReadSectorsRaw (v, lba, n, b); } +KSTATUS FatReadSectors (FAT_VOL* v, uint64_t lba, uint32_t n, void* b) { return ReadSectorsRaw (v, lba, n, b); } // Updates sectors. -static KSTATUS WriteSectors(FAT_VOL* v, uint64_t lba, uint32_t n, const void* b) { return WriteSectorsRaw(v, lba, n, b); } +KSTATUS FatWriteSectors(FAT_VOL* v, uint64_t lba, uint32_t n, const void* b) { return WriteSectorsRaw(v, lba, n, b); } -static const char* FatVariantName(FAT_VARIANT variant) { +const char* FatVariantName(FAT_VARIANT variant) { switch (variant) { case FatVariant16: return "FAT16"; case FatVariant32: return "FAT32"; @@ -240,34 +165,34 @@ static uint32_t FatEocThreshold(FAT_VOL* v) { return v && v->Variant == FatVariant16 ? FAT16_EOC : FAT32_EOC; } -static uint32_t FatEocMark(FAT_VOL* v) { +uint32_t FatEocMark(FAT_VOL* v) { return v && v->Variant == FatVariant16 ? FAT16_EOC_MARK : FAT32_EOC_MARK; } -static int FatIsDataCluster(FAT_VOL* v, uint32_t cluster) { +int FatIsDataCluster(FAT_VOL* v, uint32_t cluster) { return v && cluster >= 2u && cluster < FatEocThreshold(v); } -static uint32_t FatDirentFirstCluster(FAT_VOL* v, const DIRENT* d) { +uint32_t FatDirentFirstCluster(FAT_VOL* v, const DIRENT* d) { uint32_t lo = d ? d->FirstClusterLo : 0u; uint32_t hi = (v && v->Variant == FatVariant32 && d) ? d->FirstClusterHi : 0u; return (hi << 16) | lo; } -static void FatSetDirentFirstCluster(FAT_VOL* v, DIRENT* d, uint32_t cluster) { +void FatSetDirentFirstCluster(FAT_VOL* v, DIRENT* d, uint32_t cluster) { if (!d) return; d->FirstClusterLo = (uint16_t)(cluster & 0xFFFFu); d->FirstClusterHi = (v && v->Variant == FatVariant32) ? (uint16_t)(cluster >> 16) : 0u; } -static int FatIsFixedRoot(FAT_VOL* v, uint32_t dirCluster) { +int FatIsFixedRoot(FAT_VOL* v, uint32_t dirCluster) { return v && v->Variant == FatVariant16 && dirCluster == FAT_FIXED_ROOT_CLUSTER; } -static uint64_t ClusterToLba(FAT_VOL* v, uint32_t cluster); -static uint32_t FatEntry(FAT_VOL* v, uint32_t cluster); +uint64_t FatClusterToLba(FAT_VOL* v, uint32_t cluster); +uint32_t FatEntry(FAT_VOL* v, uint32_t cluster); -static int FatDirectoryFirstSegment(FAT_VOL* v, uint32_t dirCluster, +int FatDirectoryFirstSegment(FAT_VOL* v, uint32_t dirCluster, uint32_t* cursor, uint64_t* lba, uint32_t* sectors) { if (!v || !cursor || !lba || !sectors) return 0; @@ -280,12 +205,12 @@ static int FatDirectoryFirstSegment(FAT_VOL* v, uint32_t dirCluster, } if (!FatIsDataCluster(v, dirCluster)) return 0; *cursor = dirCluster; - *lba = ClusterToLba(v, dirCluster); + *lba = FatClusterToLba(v, dirCluster); *sectors = v->SectorsPerCluster; return 1; } -static int FatDirectoryNextSegment(FAT_VOL* v, uint32_t dirCluster, +int FatDirectoryNextSegment(FAT_VOL* v, uint32_t dirCluster, uint32_t* cursor, uint64_t* lba, uint32_t* sectors) { if (!v || !cursor || !lba || !sectors) return 0; @@ -300,18 +225,18 @@ static int FatDirectoryNextSegment(FAT_VOL* v, uint32_t dirCluster, uint32_t next = FatEntry(v, *cursor); if (!FatIsDataCluster(v, next)) return 0; *cursor = next; - *lba = ClusterToLba(v, next); + *lba = FatClusterToLba(v, next); *sectors = v->SectorsPerCluster; return 1; } // Implements the cluster To Lba operation. -static uint64_t ClusterToLba(FAT_VOL* v, uint32_t cluster) { +uint64_t FatClusterToLba(FAT_VOL* v, uint32_t cluster) { return v->DataStart + (uint64_t)(cluster - 2) * v->SectorsPerCluster; } -// FlushFatCache — needs full FAT_VOL definition. -static void FlushFatCache(FAT_VOL* v) { +// FatFlushCache — needs full FAT_VOL definition. +void FatFlushCache(FAT_VOL* v) { uint64_t fat0Start = (uint64_t)v->PartitionLba + v->ReservedSectors; int held = FatCacheAcquire(); @@ -335,7 +260,7 @@ static void FlushFatCache(FAT_VOL* v) { // FAT read / write (through cache). -static uint32_t FatEntry(FAT_VOL* v, uint32_t cluster) { +uint32_t FatEntry(FAT_VOL* v, uint32_t cluster) { if (!v || !FatIsDataCluster(v, cluster)) return 0; uint32_t bytesPerEntry = v->Variant == FatVariant16 ? 2u : 4u; @@ -359,7 +284,7 @@ static uint32_t FatEntry(FAT_VOL* v, uint32_t cluster) { } // Updates fat Entry. -static KSTATUS WriteFatEntry(FAT_VOL* v, uint32_t cluster, uint32_t value) { +KSTATUS FatWriteEntry(FAT_VOL* v, uint32_t cluster, uint32_t value) { if (!v || !FatIsDataCluster(v, cluster)) return NTLSTATUS_INVALID_PARAMETER; uint32_t bytesPerEntry = v->Variant == FatVariant16 ? 2u : 4u; @@ -389,7 +314,7 @@ static KSTATUS WriteFatEntry(FAT_VOL* v, uint32_t cluster, uint32_t value) { } // Cluster allocation (fix #3: next-free hint + two-phase wrap-around scan). -static uint32_t AllocClusterEx(FAT_VOL* v, int zeroCluster) { +uint32_t FatAllocClusterEx(FAT_VOL* v, int zeroCluster) { if (v->TotalClusters == 0) return 0; uint32_t total = v->TotalClusters; @@ -401,7 +326,7 @@ static uint32_t AllocClusterEx(FAT_VOL* v, int zeroCluster) { uint32_t hi = (pass == 0) ? total+2 : start; for (uint32_t c = lo; c < hi; c++) { if (FatEntry(v, c) == FAT_FREE) { - if (!KSUCCESS(WriteFatEntry(v, c, FatEocMark(v)))) return 0; + if (!KSUCCESS(FatWriteEntry(v, c, FatEocMark(v)))) return 0; v->NextFreeHint = c + 1; // Normal file growth zeroes newly allocated clusters so old @@ -413,7 +338,7 @@ static uint32_t AllocClusterEx(FAT_VOL* v, int zeroCluster) { uint8_t* zero = (uint8_t*)MmAllocatePool(v->BytesPerCluster); if (zero) { memset(zero, 0, v->BytesPerCluster); - WriteSectors(v, ClusterToLba(v, c), v->SectorsPerCluster, zero); + FatWriteSectors(v, FatClusterToLba(v, c), v->SectorsPerCluster, zero); MmFreePool(zero); } } @@ -424,24 +349,24 @@ static uint32_t AllocClusterEx(FAT_VOL* v, int zeroCluster) { return 0; } -static uint32_t AllocCluster(FAT_VOL* v) { - return AllocClusterEx(v, 1); +uint32_t FatAllocCluster(FAT_VOL* v) { + return FatAllocClusterEx(v, 1); } -static uint32_t AppendClusterEx(FAT_VOL* v, uint32_t lastCluster, int zeroCluster) { - uint32_t newCluster = AllocClusterEx(v, zeroCluster); +uint32_t FatAppendClusterEx(FAT_VOL* v, uint32_t lastCluster, int zeroCluster) { + uint32_t newCluster = FatAllocClusterEx(v, zeroCluster); if (!newCluster) return 0; - if (!KSUCCESS(WriteFatEntry(v, lastCluster, newCluster))) return 0; + if (!KSUCCESS(FatWriteEntry(v, lastCluster, newCluster))) return 0; return newCluster; } // Appends cluster. -static uint32_t AppendCluster(FAT_VOL* v, uint32_t lastCluster) { - return AppendClusterEx(v, lastCluster, 1); +uint32_t FatAppendCluster(FAT_VOL* v, uint32_t lastCluster) { + return FatAppendClusterEx(v, lastCluster, 1); } // Returns chain End. -static uint32_t GetChainEnd(FAT_VOL* v, uint32_t start, uint32_t* outCount) { +uint32_t FatGetChainEnd(FAT_VOL* v, uint32_t start, uint32_t* outCount) { uint32_t cluster = start, prev = start, count = 0; while (FatIsDataCluster(v, cluster)) { prev = cluster; count++; @@ -452,11 +377,11 @@ static uint32_t GetChainEnd(FAT_VOL* v, uint32_t start, uint32_t* outCount) { } // Releases cluster Chain. -static void FreeClusterChain(FAT_VOL* v, uint32_t start) { +void FatFreeClusterChain(FAT_VOL* v, uint32_t start) { uint32_t cluster = start; while (FatIsDataCluster(v, cluster)) { uint32_t next = FatEntry(v, cluster); - WriteFatEntry(v, cluster, FAT_FREE); + FatWriteEntry(v, cluster, FAT_FREE); if (cluster < v->NextFreeHint) v->NextFreeHint = cluster; cluster = next; } @@ -464,25 +389,6 @@ static void FreeClusterChain(FAT_VOL* v, uint32_t start) { // FAT timestamp and DIRENT metadata helpers. -typedef struct FAT_ENCODED_TIME { - uint16_t Date; - uint16_t Time; - uint8_t CreateTimeTenth; -} FAT_ENCODED_TIME; - -typedef struct FAT_DIRENT_UPDATE { - uint32_t Flags; - uint32_t Size; - uint32_t FirstCluster; - FAT_ENCODED_TIME Timestamp; -} FAT_DIRENT_UPDATE; - -#define FAT_DIRENT_UPDATE_SIZE 0x00000001u -#define FAT_DIRENT_UPDATE_FIRST_CLUSTER 0x00000002u -#define FAT_DIRENT_UPDATE_CREATE_TIME 0x00000004u -#define FAT_DIRENT_UPDATE_WRITE_TIME 0x00000008u -#define FAT_DIRENT_UPDATE_ACCESS_DATE 0x00000010u - // Implements the fat Is Leap Year operation. static int FatIsLeapYear(uint32_t year) { return ((year % 4u) == 0u && (year % 100u) != 0u) || ((year % 400u) == 0u); @@ -551,7 +457,7 @@ static uint8_t FatEncodeCreateTimeTenth(const SYS_SYSTEM_TIME* t) { } // Implements the fat Query Encoded Time operation. -static void FatQueryEncodedTime(FAT_ENCODED_TIME* out) { +void FatQueryEncodedTime(FAT_ENCODED_TIME* out) { SYS_SYSTEM_TIME now; memset(&now, 0, sizeof(now)); if (KeQuerySystemTime(&now) != NTLSTATUS_SUCCESS) { @@ -564,7 +470,7 @@ static void FatQueryEncodedTime(FAT_ENCODED_TIME* out) { out->CreateTimeTenth = FatEncodeCreateTimeTenth(&now); } -static void FatDecodeDateTime(uint16_t date, uint16_t time, uint8_t createTenth, +void FatDecodeDateTime(uint16_t date, uint16_t time, uint8_t createTenth, SYS_SYSTEM_TIME* out) { memset(out, 0, sizeof(*out)); uint32_t year = 1980u + ((date >> 9) & 0x7Fu); @@ -596,17 +502,17 @@ static void FatDecodeDateTime(uint16_t date, uint16_t time, uint8_t createTenth, } // Implements the fat Decode Access Date operation. -static void FatDecodeAccessDate(uint16_t date, SYS_SYSTEM_TIME* out) { +void FatDecodeAccessDate(uint16_t date, SYS_SYSTEM_TIME* out) { FatDecodeDateTime(date, 0, 0, out); } -static KSTATUS DirentUpdateMetadata(FAT_VOL* v, uint64_t lba, +KSTATUS FatDirentUpdateMetadata(FAT_VOL* v, uint64_t lba, uint32_t offset, const FAT_DIRENT_UPDATE* update) { if (!v || !update || !update->Flags) return NTLSTATUS_INVALID_PARAMETER; uint8_t* sec = (uint8_t*)MmAllocatePool(v->BytesPerSector); if (!sec) return NTLSTATUS_NO_MEMORY; - KSTATUS s = ReadSectors(v, lba, 1, sec); + KSTATUS s = FatReadSectors(v, lba, 1, sec); if (KSUCCESS(s)) { DIRENT* d = (DIRENT*)(sec + offset); if (update->Flags & FAT_DIRENT_UPDATE_SIZE) @@ -624,2801 +530,27 @@ static KSTATUS DirentUpdateMetadata(FAT_VOL* v, uint64_t lba, } if (update->Flags & FAT_DIRENT_UPDATE_ACCESS_DATE) d->LastAccDate = update->Timestamp.Date; - s = WriteSectors(v, lba, 1, sec); + s = FatWriteSectors(v, lba, 1, sec); } MmFreePool(sec); return s; } -static KSTATUS DirentUpdateSize(FAT_VOL* v, uint64_t lba, +KSTATUS FatDirentUpdateSize(FAT_VOL* v, uint64_t lba, uint32_t offset, uint32_t size) { FAT_DIRENT_UPDATE u; memset(&u, 0, sizeof(u)); FatQueryEncodedTime(&u.Timestamp); u.Flags = FAT_DIRENT_UPDATE_SIZE | FAT_DIRENT_UPDATE_WRITE_TIME; u.Size = size; - return DirentUpdateMetadata(v, lba, offset, &u); + return FatDirentUpdateMetadata(v, lba, offset, &u); } -static KSTATUS DirentUpdateFirstCluster(FAT_VOL* v, uint64_t lba, +KSTATUS FatDirentUpdateFirstCluster(FAT_VOL* v, uint64_t lba, uint32_t offset, uint32_t cluster) { FAT_DIRENT_UPDATE u; memset(&u, 0, sizeof(u)); u.Flags = FAT_DIRENT_UPDATE_FIRST_CLUSTER; u.FirstCluster = cluster; - return DirentUpdateMetadata(v, lba, offset, &u); -} - -// 8.3 name matching / assembly. - -static int MatchRaw83(const char dirName[8], const char dirExt[3], - const char wantName[8], const char wantExt[3]) { - return memcmp(dirName, wantName, 8) == 0 && - memcmp(dirExt, wantExt, 3) == 0; -} - -static char FatUpperChar(char c); -static int FatIsLowerChar(char c); -static int FatIsValidShortChar(char c); - -// Implements the make83 Name V operation. -static void Make83NameV(SYS_STRING_VIEW src, char name8[8], char ext3[3]) { - memset(name8, ' ', 8); - memset(ext3, ' ', 3); - if (!SysSvIsValid(src) || src.Length == 0) return; - - size_t baseStart = 0; - size_t dot = (size_t)-1; - if (src.Data[0] == '.' && src.Length > 1) { - baseStart = 1; - } else { - for (size_t i = 0; i < src.Length; i++) - if (src.Data[i] == '.') dot = i; - } - - size_t end = (dot != (size_t)-1) ? dot : src.Length; - int ni = 0; - for (size_t i = baseStart; i < end && ni < 8; i++) { - char c = FatUpperChar(src.Data[i]); - name8[ni++] = c; - } - - if (dot != (size_t)-1) { - int ei = 0; - for (size_t i = dot + 1; i < src.Length && ei < 3; i++) { - char c = FatUpperChar(src.Data[i]); - ext3[ei++] = c; - } - } -} - -// LFN assembly / matching. -#define MAX_LFN 256 - -// Implements the fat Upper Char operation. -static char FatUpperChar(char c) { - return (c >= 'a' && c <= 'z') ? (char)(c - 32) : c; -} - -// Implements the fat Is Lower Char operation. -static int FatIsLowerChar(char c) { - return c >= 'a' && c <= 'z'; -} - -// Implements the fat Is Valid Short Char operation. -static int FatIsValidShortChar(char c) { - c = FatUpperChar(c); - if (c >= 'A' && c <= 'Z') return 1; - if (c >= '0' && c <= '9') return 1; - switch (c) { - case '$': case '%': case '\'': case '-': case '_': case '@': - case '~': case '`': case '!': case '(': case ')': case '{': - case '}': case '^': case '#': case '&': - return 1; - default: - return 0; - } -} - -// True when the user spelling can refer to a normal FAT 8.3 entry. This is -// intentionally structural and case-insensitive: "doom1.wad" must still open -// an existing short entry named DOOM1.WAD. Leading-dot names are not 8.3. -static int FatNameIsStructurallyShort83V(SYS_STRING_VIEW name) { - if (!SysSvIsValid(name) || name.Length == 0) return 0; - if (name.Data[0] == '.') return 0; - - size_t dot = (size_t)-1; - for (size_t i = 0; i < name.Length; i++) { - char c = name.Data[i]; - if (c == '.') { - if (dot != (size_t)-1) return 0; - dot = i; - } else if (!FatIsValidShortChar(c)) { - return 0; - } - } - - size_t baseLen = (dot != (size_t)-1) ? dot : name.Length; - size_t extLen = (dot != (size_t)-1) ? (name.Length - dot - 1) : 0; - return baseLen >= 1 && baseLen <= 8 && extLen <= 3; -} - -// Implements the fat Component Has Lower V operation. -static int FatComponentHasLowerV(SYS_STRING_VIEW name, size_t start, size_t end) { - for (size_t i = start; i < end; i++) - if (FatIsLowerChar(name.Data[i])) return 1; - return 0; -} - -// Implements the fat Component Has Upper V operation. -static int FatComponentHasUpperV(SYS_STRING_VIEW name, size_t start, size_t end) { - for (size_t i = start; i < end; i++) - if (name.Data[i] >= 'A' && name.Data[i] <= 'Z') return 1; - return 0; -} - -static void FatSplitShortNameV(SYS_STRING_VIEW name, size_t* baseEnd, - size_t* extStart, size_t* extEnd) { - size_t dot = (size_t)-1; - for (size_t i = 0; i < name.Length; i++) - if (name.Data[i] == '.') dot = i; - if (dot == (size_t)-1) { - *baseEnd = name.Length; - *extStart = name.Length; - *extEnd = name.Length; - } else { - *baseEnd = dot; - *extStart = dot + 1u; - *extEnd = name.Length; - } -} - -// Creation uses LFN only when the spelling cannot be represented by a short -// entry, or when mixed-case 8.3 spelling would be lost. Plain lowercase -// 8.3 names are represented with FAT's NT case bits, not VFAT LFN entries. -static int FatNameNeedsLfnForCreateV(SYS_STRING_VIEW name) { - if (!FatNameIsStructurallyShort83V(name)) return 1; - - size_t baseEnd, extStart, extEnd; - FatSplitShortNameV(name, &baseEnd, &extStart, &extEnd); - - int baseLower = FatComponentHasLowerV(name, 0, baseEnd); - int baseUpper = FatComponentHasUpperV(name, 0, baseEnd); - int extLower = FatComponentHasLowerV(name, extStart, extEnd); - int extUpper = FatComponentHasUpperV(name, extStart, extEnd); - - if ((baseLower && baseUpper) || (extLower && extUpper)) return 1; - return 0; -} - -// Implements the fat Short Name Case Flags V operation. -static uint8_t FatShortNameCaseFlagsV(SYS_STRING_VIEW name) { - if (!FatNameIsStructurallyShort83V(name)) return 0; - size_t baseEnd, extStart, extEnd; - FatSplitShortNameV(name, &baseEnd, &extStart, &extEnd); - uint8_t flags = 0; - if (FatComponentHasLowerV(name, 0, baseEnd) && - !FatComponentHasUpperV(name, 0, baseEnd)) - flags |= FAT_NTRES_BASE_LOWER; - if (extStart < extEnd && - FatComponentHasLowerV(name, extStart, extEnd) && - !FatComponentHasUpperV(name, extStart, extEnd)) - flags |= FAT_NTRES_EXT_LOWER; - return flags; -} - -// Implements the lfn Checksum operation. -static uint8_t LfnChecksum(const char name8[8], const char ext3[3]) { - uint8_t sum = 0; - for (int i = 0; i < 11; i++) { - uint8_t c = (uint8_t)(i < 8 ? name8[i] : ext3[i - 8]); - sum = (uint8_t)(((sum & 1) ? 0x80 : 0) + (sum >> 1) + c); - } - return sum; -} - -// Implements the lfn Set Char operation. -static void LfnSetChar(LFN_ENTRY* e, int idx, uint16_t value) { - if (idx < 5) e->Name1[idx] = value; - else if (idx < 11) e->Name2[idx - 5] = value; - else e->Name3[idx - 11] = value; -} - -static void BuildLfnEntry(LFN_ENTRY* e, const char* name, size_t nameLen, - uint32_t ordinal, uint32_t lfnCount, - uint8_t checksum) { - memset(e, 0, sizeof(*e)); - e->Order = (uint8_t)ordinal; - if (ordinal == lfnCount) e->Order |= 0x40; - e->Attr = ATTR_LFN; - e->Type = 0; - e->Checksum = checksum; - e->FirstCluster = 0; - - size_t start = (size_t)(ordinal - 1) * 13u; - for (int i = 0; i < 13; i++) { - size_t pos = start + (size_t)i; - uint16_t ch; - if (pos < nameLen) ch = (uint8_t)name[pos]; - else if (pos == nameLen) ch = 0x0000; - else ch = 0xFFFF; - LfnSetChar(e, i, ch); - } -} - -// Creates legacy Broken Dot83. -static void MakeLegacyBrokenDot83(const char* src, char name8[8], char ext3[3]) { - // This exactly models the pre-fix bug for ".doomrc": empty name + "DOO". - memset(name8, ' ', 8); - memset(ext3, ' ', 3); - const char* dot = NULL; - for (const char* p = src; *p; p++) if (*p == '.') dot = p; - if (!dot) return; - int ei = 0; - for (const char* p = dot + 1; *p && ei < 3; p++) - ext3[ei++] = FatUpperChar(*p); -} - -static int ShortNameExistsRaw(FAT_VOL* v, uint32_t dirCluster, - const char name8[8], const char ext3[3]) { - uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!clusterBuf) return 1; // fail closed; caller should pick another alias - - uint32_t cursor; uint64_t lba; uint32_t sectors; - if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { - MmFreePool(clusterBuf); - return 0; - } - - do { - if (!KSUCCESS(ReadSectors(v, lba, sectors, clusterBuf))) break; - - uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); - DIRENT* dir = (DIRENT*)clusterBuf; - for (uint32_t i = 0; i < entries; i++) { - if (dir[i].Name[0] == 0x00) { MmFreePool(clusterBuf); return 0; } - if ((uint8_t)dir[i].Name[0] == 0xE5 || dir[i].Attr == ATTR_LFN) continue; - if (MatchRaw83(dir[i].Name, dir[i].Ext, name8, ext3)) { - MmFreePool(clusterBuf); - return 1; - } - } - } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); - - MmFreePool(clusterBuf); - return 0; -} - -// Implements the fat Alias Append Char operation. -static void FatAliasAppendChar(char* dst, int* len, int maxLen, char c) { - c = FatUpperChar(c); - if (!FatIsValidShortChar(c)) return; - if (*len < maxLen) dst[(*len)++] = c; -} - -// Builds alias Stem Ext V. -static void BuildAliasStemExtV(SYS_STRING_VIEW src, char stem[9], char ext3[3]) { - memset(stem, 0, 9); - memset(ext3, ' ', 3); - if (!SysSvIsValid(src)) return; - - size_t baseStart = 0; - size_t dot = (size_t)-1; - if (src.Length > 1 && src.Data[0] == '.') { - // Hidden/Unix-style names like .doomrc become alias stem DOOMRC. - baseStart = 1; - } else { - for (size_t i = 0; i < src.Length; i++) - if (src.Data[i] == '.') dot = i; - } - - int slen = 0; - size_t end = (dot != (size_t)-1) ? dot : src.Length; - for (size_t i = baseStart; i < end; i++) - FatAliasAppendChar(stem, &slen, 8, src.Data[i]); - if (slen == 0) { - memcpy(stem, "FILE", 4); - slen = 4; - } - stem[slen] = '\0'; - - if (dot != (size_t)-1 && dot + 1 < src.Length) { - int ei = 0; - for (size_t i = dot + 1; i < src.Length && ei < 3; i++) { - char c = FatUpperChar(src.Data[i]); - if (!FatIsValidShortChar(c)) continue; - ext3[ei++] = c; - } - } -} - -static void MakeAliasAttempt(const char stem[9], const char extIn[3], - uint32_t attempt, char name8[8], char ext3[3]) { - memset(name8, ' ', 8); - memcpy(ext3, extIn, 3); - - char suffix[8]; - int suffixLen = 0; - suffix[suffixLen++] = '~'; - char digits[7]; - int digitLen = 0; - uint32_t n = attempt; - do { - digits[digitLen++] = (char)('0' + (n % 10)); - n /= 10; - } while (n && digitLen < (int)sizeof(digits)); - for (int i = digitLen - 1; i >= 0; i--) - suffix[suffixLen++] = digits[i]; - - int stemLen = (int)strlen(stem); - int keep = 8 - suffixLen; - if (keep < 1) keep = 1; - if (stemLen < keep) keep = stemLen; - memcpy(name8, stem, keep); - memcpy(name8 + keep, suffix, suffixLen); -} - -static int MakeUniqueShortAliasV(FAT_VOL* v, uint32_t dirCluster, - SYS_STRING_VIEW longName, - char name8[8], char ext3[3]) { - char stem[9], extCandidate[3]; - BuildAliasStemExtV(longName, stem, extCandidate); - - for (uint32_t attempt = 1; attempt < 1000000; attempt++) { - MakeAliasAttempt(stem, extCandidate, attempt, name8, ext3); - if (!ShortNameExistsRaw(v, dirCluster, name8, ext3)) return 1; - } - return 0; -} - -// Creates legacy Broken Dot83 V. -static void MakeLegacyBrokenDot83V(SYS_STRING_VIEW src, char name8[8], char ext3[3]) { - // This exactly models the pre-fix bug for ".doomrc": empty name + "DOO". - memset(name8, ' ', 8); - memset(ext3, ' ', 3); - if (!SysSvIsValid(src)) return; - size_t dot = (size_t)-1; - for (size_t i = 0; i < src.Length; i++) if (src.Data[i] == '.') dot = i; - if (dot == (size_t)-1) return; - int ei = 0; - for (size_t i = dot + 1; i < src.Length && ei < 3; i++) - ext3[ei++] = FatUpperChar(src.Data[i]); -} - -static KSTATUS DeleteLegacyBrokenDotAliasV(FAT_VOL* v, uint32_t dirCluster, - SYS_STRING_VIEW longName) { - if (!SysSvIsValid(longName) || longName.Length < 2 || longName.Data[0] != '.') return NTLSTATUS_SUCCESS; - - char badName[8], badExt[3]; - MakeLegacyBrokenDot83V(longName, badName, badExt); - - uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!clusterBuf) return NTLSTATUS_NO_MEMORY; - - uint32_t cursor; uint64_t lba; uint32_t sectors; - if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { - MmFreePool(clusterBuf); - return NTLSTATUS_SUCCESS; - } - - do { - KSTATUS s = ReadSectors(v, lba, sectors, clusterBuf); - if (!KSUCCESS(s)) { MmFreePool(clusterBuf); return s; } - - uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); - DIRENT* dir = (DIRENT*)clusterBuf; - for (uint32_t i = 0; i < entries; i++) { - if (dir[i].Name[0] == 0x00) { MmFreePool(clusterBuf); return NTLSTATUS_SUCCESS; } - if ((uint8_t)dir[i].Name[0] == 0xE5 || dir[i].Attr == ATTR_LFN) continue; - if (MatchRaw83(dir[i].Name, dir[i].Ext, badName, badExt)) { - int emptyBase = 1; - for (int j = 0; j < 8; j++) if (dir[i].Name[j] != ' ') emptyBase = 0; - if (!emptyBase) continue; - - // Do not free the cluster chain here. The broken entry may have - // aliased existing data; deleting the directory entry is enough to - // hide it from hosts and avoids freeing clusters still owned by a - // valid file. - dir[i].Name[0] = (char)0xE5; - s = WriteSectors(v, lba, sectors, clusterBuf); - MmFreePool(clusterBuf); - return s; - } - } - } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); - - MmFreePool(clusterBuf); - return NTLSTATUS_SUCCESS; -} - -// Implements the lfn Append operation. -static void LfnAppend(char* lfn, const LFN_ENTRY* e) { - int seq = (e->Order & 0x3F) - 1; - if (seq < 0 || seq >= (MAX_LFN / 13)) return; - int pos = seq * 13; - uint16_t chars[13]; int idx = 0; - for (int i = 0; i < 5; i++) chars[idx++] = e->Name1[i]; - for (int i = 0; i < 6; i++) chars[idx++] = e->Name2[i]; - for (int i = 0; i < 2; i++) chars[idx++] = e->Name3[i]; - for (int i = 0; i < 13 && pos < MAX_LFN-1; i++, pos++) { - uint16_t c = chars[i]; - if (c == 0x0000 || c == 0xFFFF) { lfn[pos] = '\0'; break; } - lfn[pos] = (char)(c & 0x7F); - } -} - -// Directory search and view-native path traversal. -static int FatIsPathSep(char c) { - return c == '\\' || c == '/'; -} - -// Tests whether a non-empty FAT path view points at plausible kernel-owned storage. -static int FatStringViewDataIsPlausible(SYS_STRING_VIEW view) { - if (!SysSvIsValid(view)) return 0; - if (view.Length == 0) return 1; - uintptr_t data = (uintptr_t)view.Data; - return data >= 0x10000u; -} - -// Implements the name View Is Valid operation. -static int FatNameViewIsValid(SYS_STRING_VIEW name) { - if (!FatStringViewDataIsPlausible(name)) return 0; - if (name.Length == 0 || name.Length >= MAX_LFN) return 0; - for (size_t i = 0; i < name.Length; i++) { - char c = name.Data[i]; - if (c == '\0' || FatIsPathSep(c)) return 0; - } - return 1; -} - -// Implements the lfn Match V operation. -static int LfnMatchV(const char* lfn, SYS_STRING_VIEW want) { - if (!lfn || !SysSvIsValid(want)) return 0; - size_t i = 0; - while (lfn[i] && i < want.Length) { - char a = lfn[i], b = want.Data[i]; - if (a >= 'A' && a <= 'Z') a += 32; - if (b >= 'A' && b <= 'Z') b += 32; - if (a != b) return 0; - i++; - } - return lfn[i] == '\0' && i == want.Length; -} - -static int MatchName83V(const char* dirName, const char* dirExt, - SYS_STRING_VIEW want) { - char buf[13]; - int n = 0; - for (int i = 0; i < 8 && dirName[i] != ' '; i++) - buf[n++] = (char)(dirName[i] >= 'A' && dirName[i] <= 'Z' ? dirName[i]+32 : dirName[i]); - if (dirExt[0] != ' ') { - buf[n++] = '.'; - for (int i = 0; i < 3 && dirExt[i] != ' '; i++) - buf[n++] = (char)(dirExt[i] >= 'A' && dirExt[i] <= 'Z' ? dirExt[i]+32 : dirExt[i]); - } - - if ((size_t)n != want.Length) return 0; - for (int i = 0; i < n; i++) { - char a = buf[i], b = want.Data[i]; - if (a >= 'A' && a <= 'Z') a += 32; - if (b >= 'A' && b <= 'Z') b += 32; - if (a != b) return 0; - } - return 1; -} - -// Returns the path cache canonical byte used for hashes and comparisons. -static char FatPathCacheChar(char c) { - if (c == '/') return '\\'; - if (c >= 'A' && c <= 'Z') return (char)(c + 32); - return c; -} - -// Hashes a volume-relative path using the same case/separator rules as lookup. -static uint32_t FatPathCacheHash(SYS_STRING_VIEW path) { - uint32_t h = 2166136261u; - for (size_t i = 0; i < path.Length; i++) { - h ^= (uint8_t)FatPathCacheChar(path.Data[i]); - h *= 16777619u; - } - return h ? h : 1u; -} - -// Tests whether the cached path names the same FAT path as the caller's view. -static bool FatPathCachePathEquals(const FAT_PATH_CACHE_ENTRY* e, SYS_STRING_VIEW path) { - if (!e || e->PathLength != path.Length) return false; - for (size_t i = 0; i < path.Length; i++) { - if (FatPathCacheChar(e->Path[i]) != FatPathCacheChar(path.Data[i])) - return false; - } - return true; -} - -// Adds directory search counters without holding the lock during disk I/O. -static void FatPathCacheAddDirectoryStats(FAT_VOL* v, uint64_t clusters, uint64_t entries) { - if (!v) return; - uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); - v->Stats.DirectorySearches++; - v->Stats.DirectoryBlocksRead += clusters; - v->Stats.DirectoryEntriesScanned += entries; - KeReleaseSpinlock(&v->PathCacheLock, flags); -} - -// Looks up a positive path-cache entry for a full volume-relative path. -static bool FatPathCacheLookup(FAT_VOL* v, SYS_STRING_VIEW path, - uint32_t* outCluster, uint32_t* outSize, - uint8_t* outAttr, uint64_t* outDirentLba, - uint32_t* outDirentOffset) { - if (!v || path.Length == 0 || path.Length >= FAT_PATH_CACHE_MAX) - return false; - - uint32_t hash = FatPathCacheHash(path); - uint32_t slot = hash % FAT_PATH_CACHE_SLOTS; - bool hit = false; - - uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); - v->Stats.PathLookups++; - FAT_PATH_CACHE_ENTRY* e = &v->PathCache[slot]; - if (e->Valid && e->Generation == v->PathCacheGeneration && - e->Hash == hash && FatPathCachePathEquals(e, path)) { - if (outCluster) *outCluster = e->Cluster; - if (outSize) *outSize = e->Size; - if (outAttr) *outAttr = e->Attr; - if (outDirentLba) *outDirentLba = e->DirentLba; - if (outDirentOffset) *outDirentOffset = e->DirentOffset; - v->Stats.PathCacheHits++; - hit = true; - } else { - v->Stats.PathCacheMisses++; - } - KeReleaseSpinlock(&v->PathCacheLock, flags); - return hit; -} - -// Stores a positive path lookup in the volume-local path cache. -static void FatPathCacheStore(FAT_VOL* v, SYS_STRING_VIEW path, - uint32_t cluster, uint32_t size, uint8_t attr, - uint64_t direntLba, uint32_t direntOffset) { - if (!v || path.Length == 0 || path.Length >= FAT_PATH_CACHE_MAX) - return; - - uint32_t hash = FatPathCacheHash(path); - uint32_t slot = hash % FAT_PATH_CACHE_SLOTS; - - uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); - FAT_PATH_CACHE_ENTRY* e = &v->PathCache[slot]; - memset(e, 0, sizeof(*e)); - e->Valid = true; - e->Generation = v->PathCacheGeneration; - e->Hash = hash; - e->PathLength = path.Length; - memcpy(e->Path, path.Data, path.Length); - e->Path[path.Length] = '\0'; - e->Cluster = cluster; - e->Size = size; - e->Attr = attr; - e->DirentLba = direntLba; - e->DirentOffset = direntOffset; - v->Stats.PathCacheStores++; - KeReleaseSpinlock(&v->PathCacheLock, flags); -} - -// Invalidates every positive path-cache entry after a directory or size mutation. -static void FatPathCacheInvalidate(FAT_VOL* v) { - if (!v) return; - uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); - v->PathCacheGeneration++; - if (v->PathCacheGeneration == 0) - v->PathCacheGeneration = 1; - v->Stats.PathCacheInvalidations++; - v->Stats.PathCacheGeneration = v->PathCacheGeneration; - for (uint32_t i = 0; i < FAT_PATH_CACHE_SLOTS; i++) - v->PathCache[i].Valid = false; - KeReleaseSpinlock(&v->PathCacheLock, flags); -} - -// Returns the current filesystem cache and traversal counters. -void FatQueryStats(IO_VOLUME_PROVIDER* fs, IO_FILESYSTEM_STATS* out) { - if (!fs || !out) return; - FAT_VOL* v = (FAT_VOL*)fs->Context; - if (!v) { memset(out, 0, sizeof(*out)); return; } - - uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); - *out = v->Stats; - out->PathCacheGeneration = v->PathCacheGeneration; - KeReleaseSpinlock(&v->PathCacheLock, flags); -} - -// Records one data read span and its physically contiguous cluster merges. -static void FatStatsAddReadRun(FAT_VOL* v, uint32_t sectors, uint32_t merges) { - if (!v || !sectors) return; - uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); - v->Stats.DataReadRuns++; - v->Stats.DataReadSectors += sectors; - v->Stats.DataReadRunMerges += merges; - if (v->Stats.MaxDataReadSpanSectors < sectors) - v->Stats.MaxDataReadSpanSectors = sectors; - KeReleaseSpinlock(&v->PathCacheLock, flags); -} - -// Initializes the per-open cluster-chain cache for a FAT file object. -static void FatFileInitChainCache(FAT_FILE* f) { - if (!f) return; - f->ChainCacheLock = (SPINLOCK)SPINLOCK_INIT; - f->ChainCacheClock = 0; - memset(f->ChainCache, 0, sizeof(f->ChainCache)); -} - -// Invalidates cached FAT extents after the file's cluster chain changes. -static void FatFileInvalidateChainCache(FAT_FILE* f) { - if (!f) return; - uintptr_t flags = KeAcquireSpinlock(&f->ChainCacheLock); - memset(f->ChainCache, 0, sizeof(f->ChainCache)); - f->ChainCacheClock = 0; - KeReleaseSpinlock(&f->ChainCacheLock, flags); -} - -// Locates the best cached starting point for a logical cluster index. -static bool FatChainCacheLookupStart(FAT_FILE* f, uint32_t logicalIndex, - uint32_t* outLogical, uint32_t* outCluster) { - if (!f || !outLogical || !outCluster) - return false; - - bool hit = false; - uint32_t bestLogical = 0; - uint32_t bestCluster = 0; - - uintptr_t flags = KeAcquireSpinlock(&f->ChainCacheLock); - for (uint32_t i = 0; i < FAT_CHAIN_CACHE_SLOTS; i++) { - FAT_CHAIN_CACHE_ENTRY* e = &f->ChainCache[i]; - if (!e->Valid || e->Length == 0) - continue; - - uint32_t end = e->LogicalStart + e->Length; - if (logicalIndex >= e->LogicalStart && logicalIndex < end) { - *outLogical = logicalIndex; - *outCluster = e->PhysicalStart + (logicalIndex - e->LogicalStart); - hit = true; - break; - } - - if (end <= logicalIndex && FatIsDataCluster(f->Vol, e->NextCluster)) { - if (!hit || end > bestLogical) { - bestLogical = end; - bestCluster = e->NextCluster; - hit = true; - } - } - } - - if (hit && bestCluster) { - *outLogical = bestLogical; - *outCluster = bestCluster; - } - KeReleaseSpinlock(&f->ChainCacheLock, flags); - return hit; -} - -// Stores a physically contiguous FAT extent in the per-open chain cache. -static void FatChainCacheStore(FAT_FILE* f, uint32_t logicalStart, - uint32_t physicalStart, uint32_t length, - uint32_t nextCluster) { - if (!f || !FatIsDataCluster(f->Vol, physicalStart) || length == 0) - return; - - uintptr_t flags = KeAcquireSpinlock(&f->ChainCacheLock); - uint32_t slot = f->ChainCacheClock++ % FAT_CHAIN_CACHE_SLOTS; - FAT_CHAIN_CACHE_ENTRY* e = &f->ChainCache[slot]; - e->Valid = true; - e->LogicalStart = logicalStart; - e->PhysicalStart = physicalStart; - e->Length = length; - e->NextCluster = nextCluster; - KeReleaseSpinlock(&f->ChainCacheLock, flags); -} - -// Resolves a logical file cluster through the cached FAT-chain extent map. -static uint32_t FatResolveFileCluster(FAT_FILE* f, uint32_t logicalIndex) { - if (!f || !FatIsDataCluster(f->Vol, f->StartCluster)) - return 0; - if (logicalIndex == 0) - return f->StartCluster; - - uint32_t currentLogical = 0; - uint32_t cluster = f->StartCluster; - if (!FatChainCacheLookupStart(f, logicalIndex, ¤tLogical, &cluster)) { - currentLogical = 0; - cluster = f->StartCluster; - } - - while (currentLogical < logicalIndex && FatIsDataCluster(f->Vol, cluster)) { - cluster = FatEntry(f->Vol, cluster); - currentLogical++; - } - return cluster; -} - -// Builds and caches one physically contiguous read run from a starting cluster. -static bool FatBuildReadRun(FAT_FILE* f, uint32_t logicalIndex, - uint32_t startCluster, uint32_t maxSectors, - uint32_t* outRunSectors, uint32_t* outNextCluster) { - if (!f || !outRunSectors || !outNextCluster) - return false; - FAT_VOL* v = f->Vol; - if (!v || !FatIsDataCluster(v, startCluster)) - return false; - - uint32_t runSectors = v->SectorsPerCluster; - uint32_t runClusters = 1; - uint32_t last = startCluster; - uint32_t next = FatEntry(v, last); - - while (next == last + 1 && FatIsDataCluster(v, next) && - runSectors + v->SectorsPerCluster <= maxSectors) { - last = next; - runClusters++; - runSectors += v->SectorsPerCluster; - next = FatEntry(v, last); - } - - FatChainCacheStore(f, logicalIndex, startCluster, runClusters, next); - *outRunSectors = runSectors; - *outNextCluster = next; - return true; -} - -static int SearchDirV(FAT_VOL* v, uint32_t dirCluster, SYS_STRING_VIEW name, - uint32_t* outCluster, uint32_t* outSize, uint8_t* outAttr, - uint64_t* outDirentLba, uint32_t* outDirentOffset) { - if (!FatNameViewIsValid(name)) return 0; - - uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!clusterBuf) return 0; - - char lfn[MAX_LFN]; int hasLfn = 0; memset(lfn, 0, sizeof(lfn)); - char wantName83[8], wantExt83[3]; - int allowShortMatch = FatNameIsStructurallyShort83V(name); - Make83NameV(name, wantName83, wantExt83); - uint64_t clustersRead = 0; - uint64_t entriesScanned = 0; - int found = 0; - - uint32_t cursor; uint64_t lba; uint32_t sectors; - if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) - goto done; - - do { - if (!KSUCCESS(ReadSectors(v, lba, sectors, clusterBuf))) break; - clustersRead++; - - uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); - DIRENT* dir = (DIRENT*)clusterBuf; - - for (uint32_t i = 0; i < entries; i++) { - entriesScanned++; - if (dir[i].Name[0] == 0x00) goto done; - if ((uint8_t)dir[i].Name[0] == 0xE5) { hasLfn = 0; memset(lfn, 0, sizeof(lfn)); continue; } - if (dir[i].Attr == ATTR_LFN) { LfnAppend(lfn, (LFN_ENTRY*)&dir[i]); hasLfn = 1; continue; } - - int match = 0; - if (hasLfn && lfn[0]) match = LfnMatchV(lfn, name); - if (!match && allowShortMatch) - match = MatchName83V(dir[i].Name, dir[i].Ext, name); - if (!match && allowShortMatch) - match = MatchRaw83(dir[i].Name, dir[i].Ext, wantName83, wantExt83); - hasLfn = 0; memset(lfn, 0, sizeof(lfn)); - - if (match) { - *outCluster = FatDirentFirstCluster(v, &dir[i]); - *outSize = dir[i].FileSize; - *outAttr = dir[i].Attr; - if (outDirentLba || outDirentOffset) { - uint64_t eb = (uint64_t)i * sizeof(DIRENT); - if (outDirentLba) *outDirentLba = lba + eb / v->BytesPerSector; - if (outDirentOffset) *outDirentOffset = (uint32_t)(eb % v->BytesPerSector); - } - found = 1; - goto done; - } - } - } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); - -done: - FatPathCacheAddDirectoryStats(v, clustersRead, entriesScanned); - MmFreePool(clusterBuf); - return found; -} - -static int FatPathNextComponent(SYS_STRING_VIEW path, size_t* cursor, - SYS_STRING_VIEW* component) { - if (!cursor || !component || !SysSvIsValid(path)) return 0; - size_t pos = *cursor; - while (pos < path.Length && FatIsPathSep(path.Data[pos])) pos++; - if (pos >= path.Length) { - *cursor = pos; - *component = SYS_SV_NULL; - return 0; - } - - size_t start = pos; - while (pos < path.Length && !FatIsPathSep(path.Data[pos])) pos++; - *cursor = pos; - *component = SysSvFromParts(path.Data + start, pos - start); - return 1; -} - -static KSTATUS FatLookupPathV(FAT_VOL* v, SYS_STRING_VIEW path, - uint32_t* outCluster, uint32_t* outSize, - uint8_t* outAttr, - uint64_t* outDirentLba, uint32_t* outDirentOffset) { - if (!v || !outCluster || !outSize || !FatStringViewDataIsPlausible(path)) - return NTLSTATUS_INVALID_PARAMETER; - if (SysSvContainsByte(path, '\0')) - return NTLSTATUS_INVALID_PARAMETER; - - uint32_t cluster = v->RootCluster, size = 0; - uint8_t attr = ATTR_DIRECTORY; - uint64_t finalDirentLba = 0; - uint32_t finalDirentOffset = 0; - if (outDirentLba) *outDirentLba = 0; - if (outDirentOffset) *outDirentOffset = 0; - - if (FatPathCacheLookup(v, path, outCluster, outSize, &attr, - outDirentLba, outDirentOffset)) { - if (outAttr) *outAttr = attr; - return NTLSTATUS_SUCCESS; - } - - size_t cursor = 0; - SYS_STRING_VIEW component; - while (FatPathNextComponent(path, &cursor, &component)) { - if (component.Length >= MAX_LFN) return NTLSTATUS_BUFFER_TOO_SMALL; - if (!FatNameViewIsValid(component)) return NTLSTATUS_INVALID_PARAMETER; - if (!(attr & ATTR_DIRECTORY)) return NTLSTATUS_NOT_FOUND; - - uint32_t childCluster, childSize; uint8_t childAttr; - uint64_t childLba = 0; uint32_t childOffset = 0; - if (!SearchDirV(v, cluster, component, &childCluster, &childSize, - &childAttr, &childLba, &childOffset)) - return NTLSTATUS_NOT_FOUND; - - cluster = childCluster; size = childSize; attr = childAttr; - finalDirentLba = childLba; - finalDirentOffset = childOffset; - if (outDirentLba) *outDirentLba = childLba; - if (outDirentOffset) *outDirentOffset = childOffset; - } - - *outCluster = cluster; - *outSize = size; - if (outAttr) *outAttr = attr; - FatPathCacheStore(v, path, cluster, size, attr, - finalDirentLba, finalDirentOffset); - return NTLSTATUS_SUCCESS; -} - -static KSTATUS TraverseV(FAT_VOL* v, SYS_STRING_VIEW path, - uint32_t* outCluster, uint32_t* outSize) { - return FatLookupPathV(v, path, outCluster, outSize, NULL, NULL, NULL); -} - -static KSTATUS SplitPathV(SYS_STRING_VIEW path, SYS_STRING_VIEW* parent, - SYS_STRING_VIEW* name) { - if (!parent || !name || !SysSvIsValid(path)) return NTLSTATUS_INVALID_PARAMETER; - *parent = SYS_SV_NULL; - *name = SYS_SV_NULL; - - while (path.Length && FatIsPathSep(path.Data[path.Length - 1])) - path.Length--; - if (path.Length == 0) return NTLSTATUS_INVALID_PARAMETER; - - if (SysSvContainsByte(path, '\0')) return NTLSTATUS_INVALID_PARAMETER; - - size_t lastSep = (size_t)-1; - for (size_t i = 0; i < path.Length; i++) { - if (FatIsPathSep(path.Data[i])) lastSep = i; - } - - if (lastSep == (size_t)-1) { - *parent = SYS_SV_NULL; - *name = path; - } else { - *parent = SysSvFromParts(path.Data, lastSep); - *name = SysSvFromParts(path.Data + lastSep + 1, path.Length - lastSep - 1); - } - - if (!FatNameViewIsValid(*name)) return NTLSTATUS_INVALID_PARAMETER; - return NTLSTATUS_SUCCESS; -} - -// Find a free DIRENT slot. -static int FindFreeDirent(FAT_VOL* v, uint32_t dirCluster, - uint64_t* outLba, uint32_t* outOffset) { - uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!clusterBuf) return 0; - - uint32_t cursor; uint64_t lba; uint32_t sectors; - if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { - MmFreePool(clusterBuf); - return 0; - } - - do { - if (!KSUCCESS(ReadSectors(v, lba, sectors, clusterBuf))) { - MmFreePool(clusterBuf); return 0; - } - uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); - DIRENT* dir = (DIRENT*)clusterBuf; - for (uint32_t i = 0; i < entries; i++) { - uint8_t first = (uint8_t)dir[i].Name[0]; - if (first == 0xE5 || first == 0x00) { - uint64_t eb = (uint64_t)i * sizeof(DIRENT); - *outLba = lba + eb / v->BytesPerSector; - *outOffset = (uint32_t)(eb % v->BytesPerSector); - MmFreePool(clusterBuf); return 1; - } - } - } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); - - if (FatIsFixedRoot(v, dirCluster)) { - MmFreePool(clusterBuf); - return 0; - } - - uint32_t newCluster = AppendCluster(v, cursor); - if (newCluster) { - *outLba = ClusterToLba(v, newCluster); *outOffset = 0; - MmFreePool(clusterBuf); return 1; - } - MmFreePool(clusterBuf); - return 0; -} - -// Find a contiguous run of free directory entries. -// VFAT LFN creation requires N long-name entries to immediately precede the -// final short entry, so the run must live inside one readable/writeable segment. -static int FindFreeDirentRun(FAT_VOL* v, uint32_t dirCluster, uint32_t needed, - uint64_t* outLba, uint32_t* outFirstIndex, - uint32_t* outSectors) { - if (!needed) return 0; - - uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!clusterBuf) return 0; - - uint32_t cursor; uint64_t lba; uint32_t sectors; - if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { - MmFreePool(clusterBuf); - return 0; - } - - do { - if (!KSUCCESS(ReadSectors(v, lba, sectors, clusterBuf))) { - MmFreePool(clusterBuf); - return 0; - } - - uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); - DIRENT* dir = (DIRENT*)clusterBuf; - uint32_t runStart = 0, runLen = 0; - for (uint32_t i = 0; i < entries; i++) { - uint8_t first = (uint8_t)dir[i].Name[0]; - int freeEntry = (first == 0xE5 || first == 0x00); - if (freeEntry) { - if (runLen == 0) runStart = i; - runLen++; - if (runLen >= needed) { - *outLba = lba; - *outFirstIndex = runStart; - *outSectors = sectors; - MmFreePool(clusterBuf); - return 1; - } - } else { - runLen = 0; - } - } - } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); - - if (FatIsFixedRoot(v, dirCluster)) { - MmFreePool(clusterBuf); - return 0; - } - - uint32_t newCluster = AppendCluster(v, cursor); - if (newCluster) { - *outLba = ClusterToLba(v, newCluster); - *outFirstIndex = 0; - *outSectors = v->SectorsPerCluster; - MmFreePool(clusterBuf); - return 1; - } - - MmFreePool(clusterBuf); - return 0; -} - -// Finds the parent directory and final component for a create or delete path. -static KSTATUS FatResolveParentForMutation(FAT_VOL* v, SYS_STRING_VIEW path, - uint32_t* parentClusterOut, - SYS_STRING_VIEW* filenameOut) { - if (!v || !parentClusterOut || !filenameOut) - return NTLSTATUS_INVALID_PARAMETER; - - SYS_STRING_VIEW parentPath; - SYS_STRING_VIEW filenameView; - KSTATUS s = SplitPathV(path, &parentPath, &filenameView); - if (!KSUCCESS(s)) return s; - - uint32_t parentCluster = 0; - uint32_t parentSize = 0; - uint8_t parentAttr = 0; - s = FatLookupPathV(v, parentPath, &parentCluster, &parentSize, - &parentAttr, NULL, NULL); - if (!KSUCCESS(s)) return s; - if (!(parentAttr & ATTR_DIRECTORY)) return NTLSTATUS_NOT_FOUND; - - *parentClusterOut = parentCluster; - *filenameOut = filenameView; - return NTLSTATUS_SUCCESS; -} - -// Creates a FAT directory entry run for a new file or directory. -static KSTATUS FatCreateDirent(FAT_VOL* v, SYS_STRING_VIEW path, - uint8_t attr, uint32_t firstCluster, - uint32_t fileSize, - uint64_t* direntLbaOut, - uint32_t* direntOffsetOut) { - if (!v || !SysSvIsValid(path) || path.Length == 0) - return NTLSTATUS_INVALID_PARAMETER; - - uint32_t parentCluster = 0; - SYS_STRING_VIEW filenameView; - KSTATUS s = FatResolveParentForMutation(v, path, &parentCluster, &filenameView); - if (!KSUCCESS(s)) return s; - - { - uint32_t ec, es; - uint8_t ea; - uint64_t existingLba = 0; - uint32_t existingOffset = 0; - if (SearchDirV(v, parentCluster, filenameView, &ec, &es, &ea, - &existingLba, &existingOffset)) { - return NTLSTATUS_OBJECT_NAME_COLLISION; - } - } - - int useLfn = FatNameNeedsLfnForCreateV(filenameView); - char shortName[8], shortExt[3]; - if (useLfn) { - s = DeleteLegacyBrokenDotAliasV(v, parentCluster, filenameView); - if (!KSUCCESS(s)) return s; - if (!MakeUniqueShortAliasV(v, parentCluster, filenameView, shortName, shortExt)) - return NTLSTATUS_OBJECT_NAME_COLLISION; - } else { - Make83NameV(filenameView, shortName, shortExt); - } - - size_t nameLen = filenameView.Length; - if (nameLen == 0 || nameLen >= MAX_LFN) return NTLSTATUS_INVALID_PARAMETER; - uint32_t lfnCount = useLfn ? (uint32_t)((nameLen + 12u) / 13u) : 0; - uint32_t neededEntries = lfnCount + 1u; - - uint64_t clusterLba; uint32_t firstIndex; uint32_t entrySectors; - if (!FindFreeDirentRun(v, parentCluster, neededEntries, &clusterLba, &firstIndex, &entrySectors)) - return NTLSTATUS_NO_MEMORY; - - uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!clusterBuf) return NTLSTATUS_NO_MEMORY; - - s = ReadSectors(v, clusterLba, entrySectors, clusterBuf); - if (!KSUCCESS(s)) { MmFreePool(clusterBuf); return s; } - - DIRENT* dir = (DIRENT*)clusterBuf; - uint32_t shortIndex = firstIndex + lfnCount; - - if (useLfn) { - uint8_t checksum = LfnChecksum(shortName, shortExt); - for (uint32_t slot = 0; slot < lfnCount; slot++) { - uint32_t ordinal = lfnCount - slot; - BuildLfnEntry((LFN_ENTRY*)&dir[firstIndex + slot], filenameView.Data, - nameLen, ordinal, lfnCount, checksum); - } - } - - DIRENT* d = &dir[shortIndex]; - memset(d, 0, sizeof(DIRENT)); - memcpy(d->Name, shortName, 8); - memcpy(d->Ext, shortExt, 3); - d->Attr = attr; - d->Reserved = FatShortNameCaseFlagsV(filenameView); - FatSetDirentFirstCluster(v, d, firstCluster); - d->FileSize = fileSize; - { - FAT_ENCODED_TIME now; - FatQueryEncodedTime(&now); - d->CrtTimeTenth = now.CreateTimeTenth; - d->CrtTime = now.Time; - d->CrtDate = now.Date; - d->LastAccDate = now.Date; - d->WrtTime = now.Time; - d->WrtDate = now.Date; - } - - s = WriteSectors(v, clusterLba, entrySectors, clusterBuf); - MmFreePool(clusterBuf); - if (!KSUCCESS(s)) return s; - - uint64_t shortEntryByte = (uint64_t)shortIndex * sizeof(DIRENT); - uint64_t direntLba = clusterLba + shortEntryByte / v->BytesPerSector; - uint32_t direntOffset = (uint32_t)(shortEntryByte % v->BytesPerSector); - if (direntLbaOut) *direntLbaOut = direntLba; - if (direntOffsetOut) *direntOffsetOut = direntOffset; - - FatPathCacheInvalidate(v); - return NTLSTATUS_SUCCESS; -} - -// Initializes one dot entry in a new directory cluster. -static void FatInitDotEntry(FAT_VOL* v, DIRENT* d, const char* name, - uint32_t firstCluster, - const FAT_ENCODED_TIME* now) { - memset(d, 0, sizeof(*d)); - memset(d->Name, ' ', sizeof(d->Name)); - memset(d->Ext, ' ', sizeof(d->Ext)); - d->Name[0] = name[0]; - if (name[1]) d->Name[1] = name[1]; - d->Attr = ATTR_DIRECTORY; - FatSetDirentFirstCluster(v, d, firstCluster); - d->CrtTimeTenth = now->CreateTimeTenth; - d->CrtTime = now->Time; - d->CrtDate = now->Date; - d->LastAccDate = now->Date; - d->WrtTime = now->Time; - d->WrtDate = now->Date; -} - -// Writes the initial contents of a newly allocated FAT directory cluster. -static KSTATUS FatInitializeDirectoryCluster(FAT_VOL* v, uint32_t dirCluster, - uint32_t parentCluster) { - if (!v || dirCluster < 2) - return NTLSTATUS_INVALID_PARAMETER; - - uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!clusterBuf) return NTLSTATUS_NO_MEMORY; - memset(clusterBuf, 0, v->BytesPerCluster); - - FAT_ENCODED_TIME now; - FatQueryEncodedTime(&now); - DIRENT* dir = (DIRENT*)clusterBuf; - FatInitDotEntry(v, &dir[0], ".", dirCluster, &now); - FatInitDotEntry(v, &dir[1], "..", FatIsFixedRoot(v, parentCluster) ? 0u : parentCluster, &now); - - KSTATUS s = WriteSectors(v, ClusterToLba(v, dirCluster), - v->SectorsPerCluster, clusterBuf); - MmFreePool(clusterBuf); - return s; -} - -// Tests whether a FAT directory entry is one of the synthetic dot entries. -static int FatIsDotEntry(const DIRENT* d) { - if (!d || !(d->Attr & ATTR_DIRECTORY)) return 0; - if (d->Name[0] != '.') return 0; - if (d->Name[1] == ' ') - return 1; - return d->Name[1] == '.' && d->Name[2] == ' '; -} - -// Tests whether a directory contains any real child entries. -static KSTATUS FatDirectoryIsEmpty(FAT_VOL* v, uint32_t dirCluster, - int* emptyOut) { - if (!v || !emptyOut) - return NTLSTATUS_INVALID_PARAMETER; - - uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!clusterBuf) return NTLSTATUS_NO_MEMORY; - - uint32_t cursor; uint64_t lba; uint32_t sectors; - if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { - MmFreePool(clusterBuf); - return NTLSTATUS_INVALID_PARAMETER; - } - - do { - KSTATUS s = ReadSectors(v, lba, sectors, clusterBuf); - if (!KSUCCESS(s)) { MmFreePool(clusterBuf); return s; } - - uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); - DIRENT* dir = (DIRENT*)clusterBuf; - for (uint32_t i = 0; i < entries; i++) { - uint8_t first = (uint8_t)dir[i].Name[0]; - if (first == 0x00) { MmFreePool(clusterBuf); *emptyOut = 1; return NTLSTATUS_SUCCESS; } - if (first == 0xE5 || dir[i].Attr == ATTR_LFN) continue; - if ((dir[i].Attr & ATTR_VOLUME_ID) && !(dir[i].Attr & ATTR_DIRECTORY)) continue; - if (FatIsDotEntry(&dir[i])) continue; - - MmFreePool(clusterBuf); - *emptyOut = 0; - return NTLSTATUS_SUCCESS; - } - } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); - - MmFreePool(clusterBuf); - *emptyOut = 1; - return NTLSTATUS_SUCCESS; -} - -// Deletes the directory-entry run for one child name from a parent directory. -static KSTATUS FatDeleteDirentRunV(FAT_VOL* v, uint32_t parentCluster, - SYS_STRING_VIEW name) { - if (!v || !FatNameViewIsValid(name)) - return NTLSTATUS_INVALID_PARAMETER; - - uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!clusterBuf) return NTLSTATUS_NO_MEMORY; - - char lfn[MAX_LFN]; - int hasLfn = 0; - uint32_t lfnStart = 0; - memset(lfn, 0, sizeof(lfn)); - - char wantName83[8], wantExt83[3]; - int allowShortMatch = FatNameIsStructurallyShort83V(name); - Make83NameV(name, wantName83, wantExt83); - - uint32_t cursor; uint64_t lba; uint32_t sectors; - if (!FatDirectoryFirstSegment(v, parentCluster, &cursor, &lba, §ors)) { - MmFreePool(clusterBuf); - return NTLSTATUS_NOT_FOUND; - } - - do { - KSTATUS s = ReadSectors(v, lba, sectors, clusterBuf); - if (!KSUCCESS(s)) { MmFreePool(clusterBuf); return s; } - - uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); - DIRENT* dir = (DIRENT*)clusterBuf; - hasLfn = 0; - memset(lfn, 0, sizeof(lfn)); - for (uint32_t i = 0; i < entries; i++) { - uint8_t first = (uint8_t)dir[i].Name[0]; - if (first == 0x00) { MmFreePool(clusterBuf); return NTLSTATUS_NOT_FOUND; } - if (first == 0xE5) { hasLfn = 0; memset(lfn, 0, sizeof(lfn)); continue; } - if (dir[i].Attr == ATTR_LFN) { - if (!hasLfn) lfnStart = i; - LfnAppend(lfn, (LFN_ENTRY*)&dir[i]); - hasLfn = 1; - continue; - } - - int match = 0; - if (hasLfn && lfn[0]) match = LfnMatchV(lfn, name); - if (!match && allowShortMatch) - match = MatchName83V(dir[i].Name, dir[i].Ext, name); - if (!match && allowShortMatch) - match = MatchRaw83(dir[i].Name, dir[i].Ext, wantName83, wantExt83); - - if (match) { - uint32_t start = hasLfn ? lfnStart : i; - for (uint32_t j = start; j <= i; j++) - dir[j].Name[0] = (char)0xE5; - s = WriteSectors(v, lba, sectors, clusterBuf); - MmFreePool(clusterBuf); - if (KSUCCESS(s)) FatPathCacheInvalidate(v); - return s; - } - - hasLfn = 0; - memset(lfn, 0, sizeof(lfn)); - } - } while (FatDirectoryNextSegment(v, parentCluster, &cursor, &lba, §ors)); - - MmFreePool(clusterBuf); - return NTLSTATUS_NOT_FOUND; -} - -// Open / Close. -KSTATUS FatOpen(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path, void** fileCtx) { - if (!fs || !fileCtx || !SysSvIsValid(path) || path.Length == 0) - return NTLSTATUS_INVALID_PARAMETER; - - FAT_VOL* v = (FAT_VOL*)fs->Context; - uint32_t cluster, size; uint64_t direntLba; uint32_t direntOffset; - KSTATUS s = FatLookupPathV(v, path, &cluster, &size, NULL, &direntLba, &direntOffset); - if (!KSUCCESS(s)) return s; - - FAT_FILE* f = (FAT_FILE*)MmAllocatePool(sizeof(FAT_FILE)); - if (!f) return NTLSTATUS_NO_MEMORY; - f->StartCluster = cluster; f->FileSize = size; f->Vol = v; - f->DirentLba = direntLba; f->DirentOffset = direntOffset; - FatFileInitChainCache(f); - *fileCtx = f; - return NTLSTATUS_SUCCESS; -} - -// Flushes FAT metadata and the backing device write cache. -KSTATUS FatFlush(IO_VOLUME_PROVIDER* fs, void* fileCtx) { - (void)fs; - FAT_FILE* f = (FAT_FILE*)fileCtx; - if (!f || !f->Vol) - return NTLSTATUS_INVALID_PARAMETER; - - FlushFatCache(f->Vol); - return IoFlushDisk(f->Vol->DriveIndex); -} - -// Releases the requested object. -void FatClose(IO_VOLUME_PROVIDER* fs, void* fileCtx) { - (void)fs; - FAT_FILE* f = (FAT_FILE*)fileCtx; - FlushFatCache(f->Vol); - MmFreePool(fileCtx); -} - -// Read (fix #4: contiguous-run coalescing) -// Cluster-chain lookup is cached per open file, and data spans are allowed to -// grow beyond the AHCI bounce size because the device layer now splits large -// sector requests for the concrete controller. - -KSTATUS FatRead(IO_VOLUME_PROVIDER* fs, void* fileCtx, - uint64_t offset, void* buf, - size_t size, size_t* bytesRead) { - (void)fs; - if (!bytesRead) return NTLSTATUS_INVALID_PARAMETER; - *bytesRead = 0; - if (!fileCtx || !buf) return NTLSTATUS_INVALID_PARAMETER; - - FAT_FILE* f = (FAT_FILE*)fileCtx; - FAT_VOL* v = f->Vol; - if (offset >= f->FileSize || size == 0) return NTLSTATUS_SUCCESS; - if (size > (size_t)(f->FileSize - offset)) size = (size_t)(f->FileSize - offset); - size_t wanted = size; - - uint32_t initialOffsetInCluster = (uint32_t)(offset % v->BytesPerCluster); - uint64_t initialBytesCovered = (uint64_t)initialOffsetInCluster + (uint64_t)size; - uint32_t maxUsefulSectors = (uint32_t)((initialBytesCovered + 511u) / 512u); - if (maxUsefulSectors < v->SectorsPerCluster) maxUsefulSectors = v->SectorsPerCluster; - if (maxUsefulSectors > FAT_READ_RUN_MAX_SECTORS) - maxUsefulSectors = FAT_READ_RUN_MAX_SECTORS; - - uint8_t* runBuf = (uint8_t*)MmAllocatePool((size_t)maxUsefulSectors * 512u); - if (!runBuf) return NTLSTATUS_NO_MEMORY; - - size_t totalRead = 0; - uint8_t* out = (uint8_t*)buf; - KSTATUS readStatus = NTLSTATUS_SUCCESS; - uint32_t logicalCluster = (uint32_t)(offset / v->BytesPerCluster); - uint32_t offsetInCluster = (uint32_t)(offset % v->BytesPerCluster); - uint32_t cluster = FatResolveFileCluster(f, logicalCluster); - - while (size > 0 && FatIsDataCluster(v, cluster)) { - uint64_t bytesCovered = (uint64_t)offsetInCluster + (uint64_t)size; - uint32_t usefulSectors = (uint32_t)((bytesCovered + 511u) / 512u); - if (usefulSectors < v->SectorsPerCluster) usefulSectors = v->SectorsPerCluster; - if (usefulSectors > FAT_READ_RUN_MAX_SECTORS) - usefulSectors = FAT_READ_RUN_MAX_SECTORS; - if (usefulSectors > maxUsefulSectors) usefulSectors = maxUsefulSectors; - - uint32_t runSectors = 0; - uint32_t next = 0; - if (!FatBuildReadRun(f, logicalCluster, cluster, usefulSectors, - &runSectors, &next)) { - readStatus = NTLSTATUS_DEVICE_ERROR; - break; - } - - KSTATUS rs = ReadSectors(v, ClusterToLba(v, cluster), runSectors, runBuf); - if (!KSUCCESS(rs)) { - readStatus = rs; - break; - } - - uint32_t runBytes = runSectors * 512u; - size_t avail = runBytes - offsetInCluster; - size_t copy = avail < size ? avail : size; - uint32_t runClusters = runSectors / v->SectorsPerCluster; - uint32_t merges = runClusters ? runClusters - 1u : 0; - FatStatsAddReadRun(v, runSectors, merges); - - memcpy(out, runBuf + offsetInCluster, copy); - out += copy; - totalRead += copy; - size -= copy; - offsetInCluster = 0; - logicalCluster += runClusters; - cluster = next; - } - - if (totalRead != wanted) { - // The directory entry says the file has more data than the cluster chain - // or disk read actually supplied. Clear the caller's remainder so stale - // memory cannot be interpreted as file contents. - memset(out, 0, wanted - totalRead); - } - - MmFreePool(runBuf); - *bytesRead = totalRead; - if (totalRead != wanted) - return KSUCCESS(readStatus) ? NTLSTATUS_DEVICE_ERROR : readStatus; - return NTLSTATUS_SUCCESS; -} - -// Write (fix #4: contiguous-run coalescing) -// Same strategy as read. Whole-cluster-aligned runs skip the read-modify- -// write; partial head/tail clusters still require it. - -KSTATUS FatWrite(IO_VOLUME_PROVIDER* fs, void* fileCtx, - uint64_t offset, const void* buf, - size_t size, size_t* bytesWritten) { - (void)fs; - if (!size) { *bytesWritten = 0; return NTLSTATUS_SUCCESS; } - - FAT_FILE* f = (FAT_FILE*)fileCtx; - FAT_VOL* v = f->Vol; - const uint8_t* src = (const uint8_t*)buf; - FatFileInvalidateChainCache(f); - - // Ensure at least one cluster exists. - if (f->StartCluster < 2) { - uint32_t first = AllocCluster(v); - if (!first) { *bytesWritten = 0; return NTLSTATUS_NO_MEMORY; } - FlushFatCache(v); - f->StartCluster = first; - DirentUpdateFirstCluster(v, f->DirentLba, f->DirentOffset, first); - } - - // Walk to the starting cluster, allocating as needed. - uint32_t clusterIndex = (uint32_t)(offset / v->BytesPerCluster); - uint32_t offsetInCluster = (uint32_t)(offset % v->BytesPerCluster); - uint32_t cluster = f->StartCluster; - - for (uint32_t i = 0; i < clusterIndex; i++) { - uint32_t next = FatEntry(v, cluster); - if (!FatIsDataCluster(v, next)) { - next = AppendCluster(v, cluster); - if (!next) { *bytesWritten = 0; return NTLSTATUS_NO_MEMORY; } - } - cluster = next; - } - - uint8_t* runBuf = (uint8_t*)MmAllocatePool(FAT_WRITE_RUN_MAX_SECTORS * 512u); - if (!runBuf) { *bytesWritten = 0; return NTLSTATUS_NO_MEMORY; } - - size_t written = 0, remaining = size; - - while (remaining > 0 && cluster >= 2) { - // Build a contiguous run (only when cluster-aligned to avoid a - // partial read-modify-write spanning multiple clusters). - uint32_t runStart = cluster; - uint32_t runSectors = v->SectorsPerCluster; - uint32_t next = FatEntry(v, cluster); - - if (offsetInCluster == 0) { - while (next == cluster + 1 && FatIsDataCluster(v, next) - && runSectors + v->SectorsPerCluster <= FAT_WRITE_RUN_MAX_SECTORS) { - cluster = next; - next = FatEntry(v, cluster); - runSectors += v->SectorsPerCluster; - } - } - - uint32_t runBytes = runSectors * 512u; - size_t avail = runBytes - offsetInCluster; - size_t copy = avail < remaining ? avail : remaining; - - // Read existing data only for partial-cluster writes. - bool partial = (offsetInCluster > 0) || (copy < (size_t)runBytes); - if (partial) { - if (!KSUCCESS(ReadSectors(v, ClusterToLba(v, runStart), runSectors, runBuf))) - break; - } - - memcpy(runBuf + offsetInCluster, src, copy); - - if (!KSUCCESS(WriteSectors(v, ClusterToLba(v, runStart), runSectors, runBuf))) - break; - - src += copy; - written += copy; - remaining -= copy; - offsetInCluster = 0; - - if (remaining > 0) { - // Advance: if end-of-chain, append a new cluster. - cluster = next; - if (!FatIsDataCluster(v, cluster)) { - // Find the actual last cluster in the run we just wrote. - uint32_t last = runStart; - for (uint32_t tmp = FatEntry(v, last); - FatIsDataCluster(v, tmp); - tmp = FatEntry(v, last)) - last = tmp; - cluster = AppendCluster(v, last); - if (!cluster) break; - } - } - } - - MmFreePool(runBuf); - - uint64_t newEnd = offset + written; - if (written > 0) { - FAT_DIRENT_UPDATE u; - memset(&u, 0, sizeof(u)); - FatQueryEncodedTime(&u.Timestamp); - u.Flags = FAT_DIRENT_UPDATE_WRITE_TIME; - if (newEnd > f->FileSize) { - f->FileSize = (uint32_t)newEnd; - u.Flags |= FAT_DIRENT_UPDATE_SIZE; - u.Size = f->FileSize; - } - KSTATUS ms = DirentUpdateMetadata(v, f->DirentLba, f->DirentOffset, &u); - if (KSUCCESS(ms)) - FatPathCacheInvalidate(v); - } - - *bytesWritten = written; - return (written == size) ? NTLSTATUS_SUCCESS : NTLSTATUS_DEVICE_ERROR; -} - -// Create. -// Creates the requested file and fails if the name already exists. -KSTATUS FatCreate(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path, void** fileCtx) { - if (!fs || !fileCtx || !SysSvIsValid(path) || path.Length == 0) - return NTLSTATUS_INVALID_PARAMETER; - - FAT_VOL* v = (FAT_VOL*)fs->Context; - uint64_t direntLba = 0; - uint32_t direntOffset = 0; - KSTATUS s = FatCreateDirent(v, path, ATTR_ARCHIVE, 0, 0, - &direntLba, &direntOffset); - if (!KSUCCESS(s)) return s; - - FAT_FILE* f = (FAT_FILE*)MmAllocatePool(sizeof(FAT_FILE)); - if (!f) return NTLSTATUS_NO_MEMORY; - f->StartCluster = 0; - f->FileSize = 0; - f->Vol = v; - f->DirentLba = direntLba; - f->DirentOffset = direntOffset; - FatFileInitChainCache(f); - *fileCtx = f; - return NTLSTATUS_SUCCESS; -} - -// Creates a directory and initializes its dot entries. -KSTATUS FatCreateDir(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path) { - if (!fs || !SysSvIsValid(path) || path.Length == 0) - return NTLSTATUS_INVALID_PARAMETER; - - FAT_VOL* v = (FAT_VOL*)fs->Context; - uint32_t parentCluster = 0; - SYS_STRING_VIEW filenameView; - KSTATUS s = FatResolveParentForMutation(v, path, &parentCluster, &filenameView); - if (!KSUCCESS(s)) return s; - (void)filenameView; - - uint32_t dirCluster = AllocCluster(v); - if (!dirCluster) return NTLSTATUS_NO_MEMORY; - - s = FatInitializeDirectoryCluster(v, dirCluster, parentCluster); - if (!KSUCCESS(s)) { - FreeClusterChain(v, dirCluster); - FlushFatCache(v); - return s; - } - - FlushFatCache(v); - - s = FatCreateDirent(v, path, ATTR_DIRECTORY, dirCluster, 0, NULL, NULL); - if (!KSUCCESS(s)) { - FreeClusterChain(v, dirCluster); - FlushFatCache(v); - return s; - } - - FlushFatCache(v); - return NTLSTATUS_SUCCESS; -} - -// Removes an empty directory. -KSTATUS FatRemoveDir(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path) { - if (!fs || !SysSvIsValid(path) || path.Length == 0) - return NTLSTATUS_INVALID_PARAMETER; - - FAT_VOL* v = (FAT_VOL*)fs->Context; - uint32_t cluster = 0; - uint32_t size = 0; - uint8_t attr = 0; - uint64_t direntLba = 0; - uint32_t direntOffset = 0; - KSTATUS s = FatLookupPathV(v, path, &cluster, &size, &attr, - &direntLba, &direntOffset); - if (!KSUCCESS(s)) return s; - if (!direntLba) return NTLSTATUS_ACCESS_DENIED; - if (!(attr & ATTR_DIRECTORY)) return NTLSTATUS_ACCESS_DENIED; - - int empty = 0; - s = FatDirectoryIsEmpty(v, cluster, &empty); - if (!KSUCCESS(s)) return s; - if (!empty) return NTLSTATUS_ACCESS_DENIED; - - uint32_t parentCluster = 0; - SYS_STRING_VIEW filenameView; - s = FatResolveParentForMutation(v, path, &parentCluster, &filenameView); - if (!KSUCCESS(s)) return s; - - s = FatDeleteDirentRunV(v, parentCluster, filenameView); - if (!KSUCCESS(s)) return s; - - if (cluster >= 2) FreeClusterChain(v, cluster); - FlushFatCache(v); - FatPathCacheInvalidate(v); - return NTLSTATUS_SUCCESS; -} - -// Truncate. -KSTATUS FatTruncate(IO_VOLUME_PROVIDER* fs, void* fileCtx, uint64_t newSize, uint32_t fileFlags) { - (void)fs; - FAT_FILE* f = (FAT_FILE*)fileCtx; - FAT_VOL* v = f->Vol; - FatFileInvalidateChainCache(f); - int zeroNewClusters = (fileFlags & IO_FILE_FLAG_SKIP_ZERO_EXTEND) ? 0 : 1; - if ((uint32_t)newSize == f->FileSize) { - FAT_DIRENT_UPDATE u; - memset(&u, 0, sizeof(u)); - FatQueryEncodedTime(&u.Timestamp); - u.Flags = FAT_DIRENT_UPDATE_WRITE_TIME; - KSTATUS s = DirentUpdateMetadata(v, f->DirentLba, f->DirentOffset, &u); - if (KSUCCESS(s)) FatPathCacheInvalidate(v); - return s; - } - - if (newSize == 0) { - if (f->StartCluster >= 2) { - FreeClusterChain(v, f->StartCluster); - f->StartCluster = 0; - DirentUpdateFirstCluster(v, f->DirentLba, f->DirentOffset, 0); - } - f->FileSize = 0; - KSTATUS s = DirentUpdateSize(v, f->DirentLba, f->DirentOffset, 0); - FlushFatCache(v); - if (KSUCCESS(s)) FatPathCacheInvalidate(v); - return s; - } - - uint32_t newClusters = (uint32_t) - ((newSize + v->BytesPerCluster - 1) / v->BytesPerCluster); - - if (newSize < f->FileSize) { - if (f->StartCluster < 2) goto update_size; - uint32_t cluster = f->StartCluster, lastGood = cluster; - for (uint32_t i = 1; i < newClusters && FatIsDataCluster(v, cluster); i++) { - lastGood = cluster; cluster = FatEntry(v, cluster); - } - uint32_t tail = FatEntry(v, lastGood); - WriteFatEntry(v, lastGood, FatEocMark(v)); - FreeClusterChain(v, tail); - } else { - uint32_t curClusters = f->FileSize - ? (f->FileSize + v->BytesPerCluster - 1) / v->BytesPerCluster : 0; - uint32_t lastCluster = 0; - if (f->StartCluster < 2) { - uint32_t first = AllocClusterEx(v, zeroNewClusters); - if (!first) return NTLSTATUS_NO_MEMORY; - f->StartCluster = first; - DirentUpdateFirstCluster(v, f->DirentLba, f->DirentOffset, first); - lastCluster = first; curClusters = 1; - } else { - lastCluster = GetChainEnd(v, f->StartCluster, NULL); - } - for (uint32_t i = curClusters; i < newClusters; i++) { - uint32_t next = AppendClusterEx(v, lastCluster, zeroNewClusters); - if (!next) return NTLSTATUS_NO_MEMORY; - lastCluster = next; - } - } - -update_size: - f->FileSize = (uint32_t)newSize; - { KSTATUS s = DirentUpdateSize(v, f->DirentLba, f->DirentOffset, (uint32_t)newSize); - FlushFatCache(v); - if (KSUCCESS(s)) FatPathCacheInvalidate(v); - return s; } -} - -// Delete. -KSTATUS FatDelete(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path) { - if (!fs || !SysSvIsValid(path) || path.Length == 0) - return NTLSTATUS_INVALID_PARAMETER; - - FAT_VOL* v = (FAT_VOL*)fs->Context; - uint32_t cluster, size; uint64_t direntLba; uint32_t direntOffset; - KSTATUS s = FatLookupPathV(v, path, &cluster, &size, NULL, &direntLba, &direntOffset); - if (!KSUCCESS(s)) return s; - if (!direntLba) return NTLSTATUS_ACCESS_DENIED; - - uint32_t parentCluster = 0; - SYS_STRING_VIEW filenameView; - s = FatResolveParentForMutation(v, path, &parentCluster, &filenameView); - if (!KSUCCESS(s)) return s; - - // Delete the complete entry run so long-name metadata cannot survive the - // short entry and keep writable vvfat mirrors visibly present. - s = FatDeleteDirentRunV(v, parentCluster, filenameView); - if (!KSUCCESS(s)) return s; - - if (cluster >= 2) FreeClusterChain(v, cluster); - FlushFatCache(v); - FatPathCacheInvalidate(v); - return NTLSTATUS_SUCCESS; -} - -// Returns size. -uint64_t FatGetSize(IO_VOLUME_PROVIDER* fs, void* fileCtx) { - (void)fs; return ((FAT_FILE*)fileCtx)->FileSize; -} - -// Converts FAT attributes to the public file type. -static uint32_t FatFileTypeFromAttr(uint8_t attr) { - return (attr & ATTR_DIRECTORY) ? SYS_FILE_TYPE_DIRECTORY : SYS_FILE_TYPE_REGULAR; -} - -// Fills public metadata from one FAT directory entry. -static void FatFillStatFromDirent(FAT_VOL* v, const DIRENT* d, - uint64_t direntLba, uint32_t direntOffset, - SYS_FILE_STAT* out) { - memset(out, 0, sizeof(*out)); - out->Size = sizeof(*out); - out->Type = FatFileTypeFromAttr(d->Attr); - out->Attributes = d->Attr; - out->LinkCount = 1; - out->FileSize = d->FileSize; - - uint32_t firstCluster = FatDirentFirstCluster(v, d); - out->FileId = direntLba ^ - ((uint64_t)direntOffset << 32) ^ - ((uint64_t)firstCluster << 1); - - FatDecodeDateTime(d->CrtDate, d->CrtTime, d->CrtTimeTenth, &out->CreationTime); - FatDecodeAccessDate(d->LastAccDate, &out->LastAccessTime); - FatDecodeDateTime(d->WrtDate, d->WrtTime, 0, &out->LastWriteTime); - out->ChangeTime = out->LastWriteTime; -} - -// Fills public metadata for the filesystem root. -static void FatFillRootStat(FAT_VOL* v, SYS_FILE_STAT* out) { - memset(out, 0, sizeof(*out)); - out->Size = sizeof(*out); - out->Type = SYS_FILE_TYPE_DIRECTORY; - out->Attributes = SYS_FILE_ATTR_DIRECTORY; - out->LinkCount = 1; - out->FileSize = 0; - out->FileId = ((uint64_t)v->RootCluster << 1) ^ 0x524f4f54ULL; -} - -// Queries metadata for an open FAT file. -KSTATUS FatQueryInfo(IO_VOLUME_PROVIDER* fs, void* fileCtx, SYS_FILE_STAT* out) { - (void)fs; - if (!fileCtx || !out) return NTLSTATUS_INVALID_PARAMETER; - - FAT_FILE* f = (FAT_FILE*)fileCtx; - FAT_VOL* v = f->Vol; - if (!v) return NTLSTATUS_INVALID_PARAMETER; - - if (f->DirentLba == 0) { - if (f->StartCluster == v->RootCluster && f->FileSize == 0) { - FatFillRootStat(v, out); - } else { - memset(out, 0, sizeof(*out)); - out->Size = sizeof(*out); - out->Type = SYS_FILE_TYPE_REGULAR; - out->Attributes = SYS_FILE_ATTR_ARCHIVE; - out->LinkCount = 1; - out->FileSize = f->FileSize; - out->FileId = ((uint64_t)f->StartCluster << 1); - } - return NTLSTATUS_SUCCESS; - } - - uint8_t* sec = (uint8_t*)MmAllocatePool(v->BytesPerSector); - if (!sec) return NTLSTATUS_NO_MEMORY; - - KSTATUS s = ReadSectors(v, f->DirentLba, 1, sec); - if (!KSUCCESS(s)) { MmFreePool(sec); return s; } - - DIRENT* d = (DIRENT*)(sec + f->DirentOffset); - if ((uint8_t)d->Name[0] == 0x00 || (uint8_t)d->Name[0] == 0xE5) { - MmFreePool(sec); - return NTLSTATUS_NOT_FOUND; - } - - FatFillStatFromDirent(v, d, f->DirentLba, f->DirentOffset, out); - f->StartCluster = FatDirentFirstCluster(v, d); - f->FileSize = d->FileSize; - MmFreePool(sec); - return NTLSTATUS_SUCCESS; -} - -// Queries metadata for a FAT path. -KSTATUS FatStat(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path, SYS_FILE_STAT* out) { - if (!fs || !out || !SysSvIsValid(path)) return NTLSTATUS_INVALID_PARAMETER; - - FAT_VOL* v = (FAT_VOL*)fs->Context; - if (!v) return NTLSTATUS_INVALID_PARAMETER; - - uint32_t cluster = 0; - uint32_t size = 0; - uint8_t attr = ATTR_DIRECTORY; - uint64_t direntLba = 0; - uint32_t direntOffset = 0; - KSTATUS s = FatLookupPathV(v, path, &cluster, &size, &attr, &direntLba, &direntOffset); - if (!KSUCCESS(s)) return s; - (void)cluster; - - if (direntLba == 0) { - FatFillRootStat(v, out); - return NTLSTATUS_SUCCESS; - } - - uint8_t* sec = (uint8_t*)MmAllocatePool(v->BytesPerSector); - if (!sec) return NTLSTATUS_NO_MEMORY; - - s = ReadSectors(v, direntLba, 1, sec); - if (!KSUCCESS(s)) { MmFreePool(sec); return s; } - - DIRENT* d = (DIRENT*)(sec + direntOffset); - if ((uint8_t)d->Name[0] == 0x00 || (uint8_t)d->Name[0] == 0xE5) { - MmFreePool(sec); - return NTLSTATUS_NOT_FOUND; - } - - FatFillStatFromDirent(v, d, direntLba, direntOffset, out); - out->Type = FatFileTypeFromAttr(attr); - out->FileSize = size; - MmFreePool(sec); - return NTLSTATUS_SUCCESS; -} - -// Mixes FAT directory metadata into a per-file content generation. -static uint64_t FatDirentGeneration(FAT_VOL* v, const DIRENT* d) { - if (!d) - return 0; - - uint32_t firstCluster = FatDirentFirstCluster(v, d); - uint64_t generation = ((uint64_t)d->WrtDate << 48) | - ((uint64_t)d->WrtTime << 32) | - ((uint64_t)d->LastAccDate << 16) | - (uint64_t)d->Attr; - generation ^= ((uint64_t)d->CrtDate << 40); - generation ^= ((uint64_t)d->CrtTime << 24); - generation ^= ((uint64_t)d->CrtTimeTenth << 16); - generation ^= ((uint64_t)firstCluster << 1); - generation ^= ((uint64_t)d->FileSize << 17); - return generation ? generation : 1; -} - -// Returns a stable FAT file identity for cross-open image-section caches. -KSTATUS FatQueryIdentity(IO_VOLUME_PROVIDER* fs, void* fileCtx, IO_FILE_IDENTITY* out) { - (void)fs; - if (!fileCtx || !out) - return NTLSTATUS_INVALID_PARAMETER; - - FAT_FILE* f = (FAT_FILE*)fileCtx; - FAT_VOL* v = f->Vol; - if (!v) - return NTLSTATUS_INVALID_PARAMETER; - - uint32_t startCluster = f->StartCluster; - uint32_t fileSize = f->FileSize; - uint64_t generation = ((uint64_t)startCluster << 1) ^ - ((uint64_t)fileSize << 17); - - if (f->DirentLba != 0) { - uint8_t* sec = (uint8_t*)MmAllocatePool(v->BytesPerSector); - if (!sec) - return NTLSTATUS_NO_MEMORY; - - KSTATUS s = ReadSectors(v, f->DirentLba, 1, sec); - if (!KSUCCESS(s)) { - MmFreePool(sec); - return s; - } - - DIRENT* d = (DIRENT*)(sec + f->DirentOffset); - if ((uint8_t)d->Name[0] == 0x00 || (uint8_t)d->Name[0] == 0xE5) { - MmFreePool(sec); - return NTLSTATUS_NOT_FOUND; - } - - startCluster = FatDirentFirstCluster(v, d); - fileSize = d->FileSize; - generation = FatDirentGeneration(v, d); - MmFreePool(sec); - } - - out->FileId = f->DirentLba ^ - ((uint64_t)f->DirentOffset << 32) ^ - ((uint64_t)startCluster << 1); - out->Generation = generation ? generation : 1; - out->Size = fileSize; - return NTLSTATUS_SUCCESS; -} - -// Directory enumeration (fix #2: cluster buffer cached in context) -// Previously: allocate + ReadSectors + return 1 entry + free, every call. -// Now: buffer lives in FAT_DIR_CTX; ReadSectors fires only on cluster -// boundaries. - -typedef struct { - FAT_VOL* Vol; - uint32_t DirCluster; - uint32_t Cursor; - uint32_t LoadedCursor; - uint32_t LoadedSectors; - uint64_t LoadedLba; - uint32_t EntryIndex; - char LfnBuf[MAX_LFN]; - int HasLfn; - uint8_t* ClusterBuf; // allocated once in OpenDir, freed in CloseDir -} FAT_DIR_CTX; - -// Opens dir. -KSTATUS FatOpenDir(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW dirPath, void** dirCtx) { - if (!fs || !dirCtx || !SysSvIsValid(dirPath)) - return NTLSTATUS_INVALID_PARAMETER; - - FAT_VOL* v = (FAT_VOL*)fs->Context; - uint32_t cluster; - if (dirPath.Length == 0) { - cluster = v->RootCluster; - } else { - uint32_t size; - KSTATUS s = TraverseV(v, dirPath, &cluster, &size); - if (!KSUCCESS(s)) return s; - } - - uint32_t cursor; uint64_t lba; uint32_t sectors; - if (!FatDirectoryFirstSegment(v, cluster, &cursor, &lba, §ors)) - return NTLSTATUS_NOT_FOUND; - - FAT_DIR_CTX* ctx = (FAT_DIR_CTX*)MmAllocatePool(sizeof(FAT_DIR_CTX)); - if (!ctx) return NTLSTATUS_NO_MEMORY; - - ctx->ClusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); - if (!ctx->ClusterBuf) { MmFreePool(ctx); return NTLSTATUS_NO_MEMORY; } - - ctx->Vol = v; - ctx->DirCluster = cluster; - ctx->Cursor = cursor; - ctx->LoadedCursor = (uint32_t)-1; - ctx->LoadedSectors = sectors; - ctx->LoadedLba = lba; - ctx->EntryIndex = 0; - ctx->HasLfn = 0; - memset(ctx->LfnBuf, 0, sizeof(ctx->LfnBuf)); - *dirCtx = ctx; - return NTLSTATUS_SUCCESS; -} - -// Returns dir. -KSTATUS FatReadDir(IO_VOLUME_PROVIDER* fs, void* dirCtx, KDIR_ENTRY* out) { - (void)fs; - FAT_DIR_CTX* ctx = (FAT_DIR_CTX*)dirCtx; - FAT_VOL* v = ctx->Vol; - - while (true) { - if (ctx->Cursor != ctx->LoadedCursor) { - if (!KSUCCESS(ReadSectors(v, ctx->LoadedLba, ctx->LoadedSectors, ctx->ClusterBuf))) - return NTLSTATUS_NO_MORE_FILES; - ctx->LoadedCursor = ctx->Cursor; - } - - uint32_t epc = (ctx->LoadedSectors * v->BytesPerSector) / sizeof(DIRENT); - DIRENT* dir = (DIRENT*)ctx->ClusterBuf; - while (ctx->EntryIndex < epc) { - DIRENT* e = &dir[ctx->EntryIndex++]; - - if (e->Name[0] == 0x00) return NTLSTATUS_NO_MORE_FILES; - if ((uint8_t)e->Name[0] == 0xE5) { ctx->HasLfn = 0; memset(ctx->LfnBuf, 0, sizeof(ctx->LfnBuf)); continue; } - if (e->Attr == ATTR_LFN) { LfnAppend(ctx->LfnBuf, (LFN_ENTRY*)e); ctx->HasLfn = 1; continue; } - if (e->Attr & 0x08 && !(e->Attr & ATTR_DIRECTORY)) { ctx->HasLfn = 0; memset(ctx->LfnBuf, 0, sizeof(ctx->LfnBuf)); continue; } - - memset(out, 0, sizeof(*out)); - - if (ctx->HasLfn && ctx->LfnBuf[0]) { - size_t len = strlen(ctx->LfnBuf); - if (len >= KDIR_NAME_MAX) len = KDIR_NAME_MAX - 1; - memcpy(out->Name, ctx->LfnBuf, len); out->Name[len] = '\0'; - } else { - char name83[13]; int n = 0; - for (int i = 0; i < 8 && e->Name[i] != ' '; i++) - name83[n++] = (char)(e->Name[i] >= 'A' && e->Name[i] <= 'Z' ? e->Name[i]+32 : e->Name[i]); - if (e->Ext[0] != ' ') { - name83[n++] = '.'; - for (int i = 0; i < 3 && e->Ext[i] != ' '; i++) - name83[n++] = (char)(e->Ext[i] >= 'A' && e->Ext[i] <= 'Z' ? e->Ext[i]+32 : e->Ext[i]); - } - name83[n] = '\0'; - size_t len = (size_t)n; if (len >= KDIR_NAME_MAX) len = KDIR_NAME_MAX - 1; - memcpy(out->Name, name83, len); out->Name[len] = '\0'; - } - - out->Attr = e->Attr; - out->FileSize = e->FileSize; - FatDecodeDateTime(e->CrtDate, e->CrtTime, e->CrtTimeTenth, &out->CreateTime); - FatDecodeAccessDate(e->LastAccDate, &out->LastAccessTime); - FatDecodeDateTime(e->WrtDate, e->WrtTime, 0, &out->LastWriteTime); - ctx->HasLfn = 0; memset(ctx->LfnBuf, 0, sizeof(ctx->LfnBuf)); - return NTLSTATUS_SUCCESS; - } - - if (!FatDirectoryNextSegment(v, ctx->DirCluster, &ctx->Cursor, - &ctx->LoadedLba, &ctx->LoadedSectors)) - return NTLSTATUS_NO_MORE_FILES; - ctx->LoadedCursor = (uint32_t)-1; - ctx->EntryIndex = 0; - } -} - -// Releases dir. -void FatCloseDir(IO_VOLUME_PROVIDER* fs, void* dirCtx) { - (void)fs; - FAT_DIR_CTX* ctx = (FAT_DIR_CTX*)dirCtx; - MmFreePool(ctx->ClusterBuf); - MmFreePool(ctx); -} - -// FAT BPB validation. -typedef struct FAT_BPB_LAYOUT { - FAT_VARIANT Variant; - uint32_t TotalSectors; - uint32_t FatSize; - uint32_t RootDirSectors; - uint32_t DataSectors; - uint32_t TotalClusters; -} FAT_BPB_LAYOUT; - -static int FatComputeBpbLayout(const BPB* bpb, FAT_BPB_LAYOUT* out) { - if (!bpb || !out) return 0; - memset(out, 0, sizeof(*out)); - - if (bpb->BytesPerSector != 512) return 0; - if (bpb->SectorsPerCluster == 0) return 0; - if (bpb->NumFats == 0) return 0; - if (bpb->ReservedSectors == 0) return 0; - - uint32_t totalSectors = bpb->TotalSectors16 ? bpb->TotalSectors16 : bpb->TotalSectors32; - uint32_t fatSize = bpb->FatSize16 ? bpb->FatSize16 : bpb->FatSize32; - if (!totalSectors || !fatSize) return 0; - - uint32_t rootDirSectors = ((uint32_t)bpb->RootEntryCount * sizeof(DIRENT) + - (uint32_t)bpb->BytesPerSector - 1u) / - (uint32_t)bpb->BytesPerSector; - uint32_t metadataSectors = (uint32_t)bpb->ReservedSectors + - (uint32_t)bpb->NumFats * fatSize + - rootDirSectors; - if (totalSectors <= metadataSectors) return 0; - - uint32_t dataSectors = totalSectors - metadataSectors; - uint32_t totalClusters = dataSectors / bpb->SectorsPerCluster; - FAT_VARIANT variant; - if (totalClusters < 4085u) - variant = FatVariant12; - else if (totalClusters < 65525u) - variant = FatVariant16; - else - variant = FatVariant32; - - if (variant == FatVariant16) { - if (bpb->FatSize16 == 0 || bpb->RootEntryCount == 0) return 0; - } else if (variant == FatVariant32) { - if (bpb->FatSize16 != 0 || bpb->RootEntryCount != 0) return 0; - if (bpb->FatSize32 == 0 || bpb->RootCluster < 2) return 0; - } - - out->Variant = variant; - out->TotalSectors = totalSectors; - out->FatSize = fatSize; - out->RootDirSectors = rootDirSectors; - out->DataSectors = dataSectors; - out->TotalClusters = totalClusters; - return 1; -} - -// Native async read/write -// Metadata work (FAT traversal/allocation and partial-cluster read-modify -// preparation) still runs synchronously in the caller/completion context, but -// file data transfers are submitted as disk IRPs and completion is chained from -// the driver's IRP completion callback instead of the generic I/O worker. - -typedef struct FAT_ASYNC_RW { - LIST_ENTRY Link; - FAT_VOL* Vol; - FAT_FILE* File; - uint8_t IsWrite; - - uint64_t Offset; - uint32_t OffsetInCluster; - uint32_t Cluster; - uint32_t LogicalCluster; - size_t Remaining; - size_t Done; - size_t Requested; - - uint8_t* Dst; - const uint8_t* Src; - uint8_t* RunBuf; - - uint32_t CurrentNext; - uint32_t CurrentLast; - uint32_t CurrentClusters; - uint32_t CurrentOffset; - size_t CurrentCopy; - - IRP Irp; - DEVICE_OBJECT* CurrentDevice; - IO_FS_ASYNC_COMPLETE Complete; - void* CompleteContext; -} FAT_ASYNC_RW; - -static SPINLOCK gFatAsyncLock = SPINLOCK_INIT; -static LIST_ENTRY gFatAsyncQueue; -static SEMAPHORE* gFatAsyncSemaphore; -static volatile int gFatAsyncInitState; - -static void FatAsyncReadSubmitNext(FAT_ASYNC_RW* ctx); -static void FatAsyncWriteSubmitNext(FAT_ASYNC_RW* ctx); - -// Implements the async Worker operation. -static void FatAsyncWorker(void* arg) { - (void)arg; - - while (true) { - KSTATUS s = KeWaitSemaphore(gFatAsyncSemaphore, KE_WAIT_INFINITE); - if (!KSUCCESS(s)) { - KeYield(); - continue; - } - - uintptr_t flags = KeAcquireSpinlock(&gFatAsyncLock); - LIST_ENTRY* node = ListPopHead(&gFatAsyncQueue); - KeReleaseSpinlock(&gFatAsyncLock, flags); - if (!node) continue; - - FAT_ASYNC_RW* ctx = CONTAINING_RECORD(node, FAT_ASYNC_RW, Link); - if (ctx->IsWrite) - FatAsyncWriteSubmitNext(ctx); - else - FatAsyncReadSubmitNext(ctx); - } -} - -// Implements the ensure Async Worker Started operation. -static KSTATUS FatEnsureAsyncWorkerStarted(void) { - uintptr_t flags = KeAcquireSpinlock(&gFatAsyncLock); - if (gFatAsyncInitState == 2) { - KeReleaseSpinlock(&gFatAsyncLock, flags); - return NTLSTATUS_SUCCESS; - } - if (gFatAsyncInitState == 1) { - KeReleaseSpinlock(&gFatAsyncLock, flags); - while (gFatAsyncInitState == 1) - KeYield(); - return gFatAsyncInitState == 2 ? NTLSTATUS_SUCCESS : NTLSTATUS_FAILURE; - } - - gFatAsyncInitState = 1; - LIST_INIT(&gFatAsyncQueue); - KeReleaseSpinlock(&gFatAsyncLock, flags); - - KSTATUS s = KeSemaphoreCreate(0, 0x7fffffff, &gFatAsyncSemaphore); - if (KSUCCESS(s)) { - THREAD* worker = (THREAD*)0; - const KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); - s = KeCreateKernelThread((uintptr_t)FatAsyncWorker, 0, - ¶meters, &worker); - (void)worker; - } - - flags = KeAcquireSpinlock(&gFatAsyncLock); - gFatAsyncInitState = KSUCCESS(s) ? 2 : 0; - KeReleaseSpinlock(&gFatAsyncLock, flags); - return s; -} - -// Implements the async Queue Continuation operation. -static void FatAsyncQueueContinuation(FAT_ASYNC_RW* ctx) { - if (!ctx) return; - - uintptr_t flags = KeAcquireSpinlock(&gFatAsyncLock); - ListInsertTail(&gFatAsyncQueue, &ctx->Link); - KeReleaseSpinlock(&gFatAsyncLock, flags); - KeSignalSemaphore(gFatAsyncSemaphore); -} - -// Implements the async Finish operation. -static void FatAsyncFinish(FAT_ASYNC_RW* ctx, KSTATUS status, size_t information) { - if (!ctx) return; - if (ctx->CurrentDevice) { - IoDereferenceDeviceObject(ctx->CurrentDevice); - ctx->CurrentDevice = (DEVICE_OBJECT*)0; - } - if (ctx->RunBuf) MmFreePool(ctx->RunBuf); - IO_FS_ASYNC_COMPLETE complete = ctx->Complete; - void* completeContext = ctx->CompleteContext; - MmFreePool(ctx); - if (complete) complete(completeContext, status, information); -} - -// Implements the async Read Irp Complete operation. -static void FatAsyncReadIrpComplete(IRP* irp, void* completeCtx) { - FAT_ASYNC_RW* ctx = (FAT_ASYNC_RW*)completeCtx; - if (!ctx) return; - if (ctx->CurrentDevice) { - IoDereferenceDeviceObject(ctx->CurrentDevice); - ctx->CurrentDevice = (DEVICE_OBJECT*)0; - } - - if (!KSUCCESS(irp->Status)) { - FatAsyncFinish(ctx, irp->Status, ctx->Done); - return; - } - - memcpy(ctx->Dst, ctx->RunBuf + ctx->CurrentOffset, ctx->CurrentCopy); - ctx->Dst += ctx->CurrentCopy; - ctx->Done += ctx->CurrentCopy; - ctx->Remaining -= ctx->CurrentCopy; - ctx->OffsetInCluster = 0; - ctx->LogicalCluster += ctx->CurrentClusters; - ctx->Cluster = ctx->CurrentNext; - if (ctx->CurrentClusters) { - uint32_t sectors = ctx->CurrentClusters * ctx->Vol->SectorsPerCluster; - uint32_t merges = ctx->CurrentClusters - 1u; - FatStatsAddReadRun(ctx->Vol, sectors, merges); - } - - FatAsyncQueueContinuation(ctx); -} - -// Implements the async Read Submit Next operation. -static void FatAsyncReadSubmitNext(FAT_ASYNC_RW* ctx) { - if (ctx->Remaining == 0) { - FatAsyncFinish(ctx, NTLSTATUS_SUCCESS, ctx->Done); - return; - } - - if (!FatIsDataCluster(ctx->Vol, ctx->Cluster)) { - if (ctx->Done < ctx->Requested) - memset(ctx->Dst, 0, ctx->Requested - ctx->Done); - FatAsyncFinish(ctx, NTLSTATUS_DEVICE_ERROR, ctx->Done); - return; - } - - FAT_VOL* v = ctx->Vol; - uint32_t runStart = ctx->Cluster; - uint32_t runSectors = 0; - uint32_t next = 0; - if (!FatBuildReadRun(ctx->File, ctx->LogicalCluster, ctx->Cluster, - FAT_READ_RUN_MAX_SECTORS, &runSectors, &next)) { - FatAsyncFinish(ctx, NTLSTATUS_DEVICE_ERROR, ctx->Done); - return; - } - - uint32_t runBytes = runSectors * 512u; - size_t avail = runBytes - ctx->OffsetInCluster; - size_t copy = avail < ctx->Remaining ? avail : ctx->Remaining; - - ctx->CurrentNext = next; - ctx->CurrentLast = runStart + (runSectors / v->SectorsPerCluster) - 1u; - ctx->CurrentClusters = runSectors / v->SectorsPerCluster; - ctx->CurrentOffset = ctx->OffsetInCluster; - ctx->CurrentCopy = copy; - - ctx->CurrentDevice = IoReferenceDiskDevice(v->DriveIndex); - if (!ctx->CurrentDevice) { - FatAsyncFinish(ctx, NTLSTATUS_NOT_FOUND, ctx->Done); - return; - } - - IoBuildReadWriteIrp(&ctx->Irp, IRP_MJ_READ, - ClusterToLba(v, runStart) * 512ull, - runSectors * 512u, - ctx->RunBuf); - ctx->Irp.CompletionFn = FatAsyncReadIrpComplete; - ctx->Irp.CompletionCtx = ctx; - - KSTATUS s = IoSubmitIrp(ctx->CurrentDevice, &ctx->Irp); - if (s != NTLSTATUS_PENDING) { - IoDereferenceDeviceObject(ctx->CurrentDevice); - ctx->CurrentDevice = (DEVICE_OBJECT*)0; - FatAsyncFinish(ctx, s, ctx->Done); - } -} - -KSTATUS FatReadAsync(IO_VOLUME_PROVIDER* fs, void* fileCtx, - uint64_t offset, void* buf, size_t size, - IO_FS_ASYNC_COMPLETE complete, - void* completeContext) { - (void)fs; - if (!complete) return NTLSTATUS_INVALID_PARAMETER; - if (!fileCtx || (!buf && size)) { - complete(completeContext, NTLSTATUS_INVALID_PARAMETER, 0); - return NTLSTATUS_PENDING; - } - - FAT_FILE* f = (FAT_FILE*)fileCtx; - FAT_VOL* v = f->Vol; - if (offset >= f->FileSize || size == 0) { - complete(completeContext, NTLSTATUS_SUCCESS, 0); - return NTLSTATUS_PENDING; - } - if (offset + size > f->FileSize) - size = (size_t)(f->FileSize - offset); - - KSTATUS ws = FatEnsureAsyncWorkerStarted(); - if (!KSUCCESS(ws)) { - complete(completeContext, ws, 0); - return NTLSTATUS_PENDING; - } - - FAT_ASYNC_RW* ctx = (FAT_ASYNC_RW*)MmAllocatePool(sizeof(FAT_ASYNC_RW)); - if (!ctx) { - complete(completeContext, NTLSTATUS_NO_MEMORY, 0); - return NTLSTATUS_PENDING; - } - memset(ctx, 0, sizeof(*ctx)); - LIST_INIT(&ctx->Link); - - ctx->RunBuf = (uint8_t*)MmAllocatePool(FAT_READ_RUN_MAX_SECTORS * 512u); - if (!ctx->RunBuf) { - MmFreePool(ctx); - complete(completeContext, NTLSTATUS_NO_MEMORY, 0); - return NTLSTATUS_PENDING; - } - - ctx->Vol = v; - ctx->File = f; - ctx->IsWrite = 0; - ctx->Offset = offset; - ctx->OffsetInCluster = (uint32_t)(offset % v->BytesPerCluster); - ctx->LogicalCluster = (uint32_t)(offset / v->BytesPerCluster); - ctx->Cluster = FatResolveFileCluster(f, ctx->LogicalCluster); - ctx->Remaining = size; - ctx->Requested = size; - ctx->Dst = (uint8_t*)buf; - ctx->Complete = complete; - ctx->CompleteContext = completeContext; - - FatAsyncQueueContinuation(ctx); - return NTLSTATUS_PENDING; -} - -// Implements the async Write Irp Complete operation. -static void FatAsyncWriteIrpComplete(IRP* irp, void* completeCtx) { - FAT_ASYNC_RW* ctx = (FAT_ASYNC_RW*)completeCtx; - if (!ctx) return; - if (ctx->CurrentDevice) { - IoDereferenceDeviceObject(ctx->CurrentDevice); - ctx->CurrentDevice = (DEVICE_OBJECT*)0; - } - - if (!KSUCCESS(irp->Status)) { - FatAsyncFinish(ctx, irp->Status, ctx->Done); - return; - } - - ctx->Src += ctx->CurrentCopy; - ctx->Done += ctx->CurrentCopy; - ctx->Remaining -= ctx->CurrentCopy; - ctx->OffsetInCluster = 0; - ctx->Cluster = ctx->CurrentNext; - - FatAsyncQueueContinuation(ctx); -} - -// Implements the async Write Submit Next operation. -static void FatAsyncWriteSubmitNext(FAT_ASYNC_RW* ctx) { - if (ctx->Remaining == 0) { - uint64_t newEnd = ctx->Offset + ctx->Done; - if (ctx->Done > 0) { - FAT_DIRENT_UPDATE u; - memset(&u, 0, sizeof(u)); - FatQueryEncodedTime(&u.Timestamp); - u.Flags = FAT_DIRENT_UPDATE_WRITE_TIME; - if (newEnd > ctx->File->FileSize) { - ctx->File->FileSize = (uint32_t)newEnd; - u.Flags |= FAT_DIRENT_UPDATE_SIZE; - u.Size = ctx->File->FileSize; - } - DirentUpdateMetadata(ctx->Vol, ctx->File->DirentLba, - ctx->File->DirentOffset, &u); - } - FatAsyncFinish(ctx, NTLSTATUS_SUCCESS, ctx->Done); - return; - } - - FAT_VOL* v = ctx->Vol; - if (!FatIsDataCluster(ctx->Vol, ctx->Cluster)) { - if (FatIsDataCluster(ctx->Vol, ctx->CurrentLast)) { - ctx->Cluster = AppendCluster(v, ctx->CurrentLast); - if (!ctx->Cluster) { - FatAsyncFinish(ctx, NTLSTATUS_NO_MEMORY, ctx->Done); - return; - } - } else { - FatAsyncFinish(ctx, NTLSTATUS_DEVICE_ERROR, ctx->Done); - return; - } - } - - uint32_t runStart = ctx->Cluster; - uint32_t runSectors = v->SectorsPerCluster; - uint32_t last = ctx->Cluster; - uint32_t next = FatEntry(v, last); - - if (ctx->OffsetInCluster == 0) { - while (next == last + 1 && FatIsDataCluster(v, next) && - runSectors + v->SectorsPerCluster <= FAT_WRITE_RUN_MAX_SECTORS) { - last = next; - next = FatEntry(v, last); - runSectors += v->SectorsPerCluster; - } - } - - uint32_t runBytes = runSectors * 512u; - size_t avail = runBytes - ctx->OffsetInCluster; - size_t copy = avail < ctx->Remaining ? avail : ctx->Remaining; - - bool partial = (ctx->OffsetInCluster > 0) || (copy < (size_t)runBytes); - if (partial) { - KSTATUS rs = ReadSectors(v, ClusterToLba(v, runStart), runSectors, ctx->RunBuf); - if (!KSUCCESS(rs)) { - FatAsyncFinish(ctx, rs, ctx->Done); - return; - } - } - - memcpy(ctx->RunBuf + ctx->OffsetInCluster, ctx->Src, copy); - - ctx->CurrentNext = next; - ctx->CurrentLast = last; - ctx->CurrentOffset = ctx->OffsetInCluster; - ctx->CurrentCopy = copy; - - ctx->CurrentDevice = IoReferenceDiskDevice(v->DriveIndex); - if (!ctx->CurrentDevice) { - FatAsyncFinish(ctx, NTLSTATUS_NOT_FOUND, ctx->Done); - return; - } - - IoBuildReadWriteIrp(&ctx->Irp, IRP_MJ_WRITE, - ClusterToLba(v, runStart) * 512ull, - runSectors * 512u, - ctx->RunBuf); - ctx->Irp.CompletionFn = FatAsyncWriteIrpComplete; - ctx->Irp.CompletionCtx = ctx; - - KSTATUS s = IoSubmitIrp(ctx->CurrentDevice, &ctx->Irp); - if (s != NTLSTATUS_PENDING) { - IoDereferenceDeviceObject(ctx->CurrentDevice); - ctx->CurrentDevice = (DEVICE_OBJECT*)0; - FatAsyncFinish(ctx, s, ctx->Done); - } -} - -KSTATUS FatWriteAsync(IO_VOLUME_PROVIDER* fs, void* fileCtx, - uint64_t offset, const void* buf, size_t size, - IO_FS_ASYNC_COMPLETE complete, - void* completeContext) { - (void)fs; - if (!complete) return NTLSTATUS_INVALID_PARAMETER; - if (!fileCtx || (!buf && size)) { - complete(completeContext, NTLSTATUS_INVALID_PARAMETER, 0); - return NTLSTATUS_PENDING; - } - if (size == 0) { - complete(completeContext, NTLSTATUS_SUCCESS, 0); - return NTLSTATUS_PENDING; - } - - KSTATUS ws = FatEnsureAsyncWorkerStarted(); - if (!KSUCCESS(ws)) { - complete(completeContext, ws, 0); - return NTLSTATUS_PENDING; - } - - FAT_FILE* f = (FAT_FILE*)fileCtx; - FAT_VOL* v = f->Vol; - FatFileInvalidateChainCache(f); - - if (f->StartCluster < 2) { - uint32_t first = AllocCluster(v); - if (!first) { - complete(completeContext, NTLSTATUS_NO_MEMORY, 0); - return NTLSTATUS_PENDING; - } - f->StartCluster = first; - DirentUpdateFirstCluster(v, f->DirentLba, f->DirentOffset, first); - } - - uint32_t clusterIndex = (uint32_t)(offset / v->BytesPerCluster); - uint32_t cluster = f->StartCluster; - for (uint32_t i = 0; i < clusterIndex; i++) { - uint32_t next = FatEntry(v, cluster); - if (!FatIsDataCluster(v, next)) { - next = AppendCluster(v, cluster); - if (!next) { - complete(completeContext, NTLSTATUS_NO_MEMORY, 0); - return NTLSTATUS_PENDING; - } - } - cluster = next; - } - - FAT_ASYNC_RW* ctx = (FAT_ASYNC_RW*)MmAllocatePool(sizeof(FAT_ASYNC_RW)); - if (!ctx) { - complete(completeContext, NTLSTATUS_NO_MEMORY, 0); - return NTLSTATUS_PENDING; - } - memset(ctx, 0, sizeof(*ctx)); - LIST_INIT(&ctx->Link); - - ctx->RunBuf = (uint8_t*)MmAllocatePool(FAT_WRITE_RUN_MAX_SECTORS * 512u); - if (!ctx->RunBuf) { - MmFreePool(ctx); - complete(completeContext, NTLSTATUS_NO_MEMORY, 0); - return NTLSTATUS_PENDING; - } - - ctx->Vol = v; - ctx->File = f; - ctx->IsWrite = 1; - ctx->Offset = offset; - ctx->OffsetInCluster = (uint32_t)(offset % v->BytesPerCluster); - ctx->Cluster = cluster; - ctx->Remaining = size; - ctx->Requested = size; - ctx->Src = (const uint8_t*)buf; - ctx->Complete = complete; - ctx->CompleteContext = completeContext; - - FatAsyncQueueContinuation(ctx); - return NTLSTATUS_PENDING; -} - -// Driver entry and dynamic partition-backed mount instances. -static LIST_ENTRY gFatMounts; -static uint32_t gMountedCount; -static MUTEX gFatMountMutex; -static SPINLOCK gFatMountMutexInitLock = SPINLOCK_INIT; -static int gFatMountMutexReady; - -// Marks only this FAT mount's cache state unusable when its backing volume dies. -void FatVolumeDead(IO_VOLUME_PROVIDER* provider, IO_VOLUME_REMOVAL_REASON reason) { - (void)reason; - if (!provider || !provider->Context) - return; - FatCacheInvalidateVolume((FAT_VOL*)provider->Context); -} - -// Releases one dynamically allocated FAT mount after every volume reference has drained. -void FatDestroyVolumeProvider(IO_VOLUME_PROVIDER* provider) { - if (!provider || !provider->Context) - return; - FAT_VOL* volume = (FAT_VOL*)provider->Context; - FatCacheInvalidateVolume(volume); - memset(volume, 0, sizeof(*volume)); - MmFreePool(volume); -} - -// Initializes the mount-list mutex without allocating kernel objects. -static void FatMountEnsureMutex(void) { - if (gFatMountMutexReady) - return; - - uintptr_t flags = KeAcquireSpinlock(&gFatMountMutexInitLock); - if (!gFatMountMutexReady) { - memset(&gFatMountMutex, 0, sizeof(gFatMountMutex)); - ObInitializeObjectHeader(&gFatMountMutex.Header, ObjectTypeMutex); - gFatMountMutex.Lock = (SPINLOCK)SPINLOCK_INIT; - gFatMountMutex.Owner = (struct THREAD*)0; - LIST_INIT(&gFatMountMutex.WaitList); - LIST_INIT(&gFatMountMutex.OwnerLink); - gFatMountMutex.DonationClass = KeSchedulingNormal; - gFatMountMutex.DonationPriority = PriorityLow; - gFatMountMutex.HasDonation = 0; - gFatMountMutex.Abandoned = 0; - gFatMountMutexReady = 1; - } - KeReleaseSpinlock(&gFatMountMutexInitLock, flags); -} - -// Returns one active FAT mount for an exact partition extent while the mount mutex is held. -static FAT_VOL* FatFindMountLocked(const IO_PARTITION_INFO* partition) { - if (!partition) - return (FAT_VOL*)0; - for (LIST_ENTRY* entry = gFatMounts.Fwd; entry != &gFatMounts; entry = entry->Fwd) { - FAT_VOL* volume = CONTAINING_RECORD(entry, FAT_VOL, MountLink); - if (volume->DriveIndex == partition->DiskIndex && - volume->PartitionLba == partition->StartLba) { - return volume; - } - } - return (FAT_VOL*)0; -} - -// Probes one I/O-core partition extent and mounts it when its VBR describes supported FAT16/FAT32. -static bool FatTryMountPartition(const IO_PARTITION_INFO* partition) { - if (!partition || partition->SectorCount == 0 || !IoDiskDevicePresent(partition->DiskIndex)) - return false; - - FatMountEnsureMutex(); - if (!KE_MUTEX_ACQUIRE_SUCCEEDED(KeAcquireMutex(&gFatMountMutex, KE_WAIT_INFINITE))) - return false; - - if (FatFindMountLocked(partition)) { - KeReleaseMutex(&gFatMountMutex); - return false; - } - - FAT_VOL* v = (FAT_VOL*)MmAllocatePool(sizeof(FAT_VOL)); - if (!v) { - KeReleaseMutex(&gFatMountMutex); - return false; - } - memset(v, 0, sizeof(*v)); - LIST_INIT(&v->MountLink); - v->DriveIndex = partition->DiskIndex; - v->PartitionLba = partition->StartLba; - v->PartitionSize = partition->SectorCount; - - uint8_t sector[512]; - if (!KSUCCESS(ReadSectors(v, v->PartitionLba, 1, sector))) { - MmFreePool(v); - KeReleaseMutex(&gFatMountMutex); - return false; - } - - BPB* bpb = (BPB*)sector; - FAT_BPB_LAYOUT layout; - if (!FatComputeBpbLayout(bpb, &layout) || - (layout.Variant != FatVariant16 && layout.Variant != FatVariant32)) { - MmFreePool(v); - KeReleaseMutex(&gFatMountMutex); - return false; - } - - v->Variant = layout.Variant; - v->BytesPerSector = bpb->BytesPerSector; - v->SectorsPerCluster = bpb->SectorsPerCluster; - v->BytesPerCluster = bpb->BytesPerSector * bpb->SectorsPerCluster; - v->ReservedSectors = bpb->ReservedSectors; - v->NumFats = bpb->NumFats; - v->FatSize = layout.FatSize; - v->RootEntryCount = bpb->RootEntryCount; - v->RootDirSectors = layout.RootDirSectors; - v->RootDirStart = v->PartitionLba + bpb->ReservedSectors + - (uint64_t)bpb->NumFats * layout.FatSize; - v->RootCluster = v->Variant == FatVariant16 ? FAT_FIXED_ROOT_CLUSTER : bpb->RootCluster; - v->DataStart = v->RootDirStart + layout.RootDirSectors; - v->NextFreeHint = 2; - v->TotalClusters = layout.TotalClusters; - if (v->PartitionSize > layout.TotalSectors) - v->PartitionSize = layout.TotalSectors; - - FatDebugPrint("FAT: disk=%u lba=%llu %s layout fat=%u root-sectors=%u data=%u clusters=%u\n", - v->DriveIndex, (unsigned long long)v->PartitionLba, - FatVariantName(v->Variant), v->FatSize, - v->RootDirSectors, layout.DataSectors, v->TotalClusters); - - IO_VOLUME_PROVIDER provider; - FatInitProvider(&provider, v); - IO_VOLUME_SOURCE volParams; - volParams.Type = IoVolumeSourcePartition; - volParams.DiskIndex = v->DriveIndex; - memset(volParams.Reserved, 0, sizeof(volParams.Reserved)); - volParams.StartLba = v->PartitionLba; - volParams.SectorCount = v->PartitionSize; - - IO_VOLUME* volume = (IO_VOLUME*)0; - KSTATUS status = IoCreateVolume(&provider, &volParams, &volume); - if (!KSUCCESS(status)) { - if (volume) { - (void)IoRemoveVolume(volume, IoVolumeUnmounted); - IoReleaseVolume(volume); - } else { - MmFreePool(v); - } - DbgPrint("FAT: disk=%u lba=%llu mount failed status=0x%x\n", - partition->DiskIndex, (unsigned long long)partition->StartLba, status); - KeReleaseMutex(&gFatMountMutex); - return false; - } - - // Retain the IoCreateVolume caller reference while this provider remains on the active mount list. - v->Volume = volume; - ListInsertTail(&gFatMounts, &v->MountLink); - gMountedCount++; - uint32_t instanceId = IoGetVolumeInstanceId(volume); - uint32_t mountedCount = gMountedCount; - - DbgPrint("FAT: disk=%u lba=%llu %s mounted volume=%u; total=%u\n", - v->DriveIndex, (unsigned long long)v->PartitionLba, - FatVariantName(v->Variant), instanceId, mountedCount); - KeReleaseMutex(&gFatMountMutex); - return true; -} - -// Unmounts one exact FAT provider instance when its backing partition disappears. -static void FatUnmountPartition(const IO_PARTITION_INFO* partition, IO_PARTITION_CHANGE change) { - if (!partition) - return; - - FatMountEnsureMutex(); - if (!KE_MUTEX_ACQUIRE_SUCCEEDED(KeAcquireMutex(&gFatMountMutex, KE_WAIT_INFINITE))) - return; - - FAT_VOL* v = FatFindMountLocked(partition); - if (!v) { - KeReleaseMutex(&gFatMountMutex); - return; - } - - IO_VOLUME* volume = v->Volume; - uint32_t instanceId = IoGetVolumeInstanceId(volume); - ListRemove(&v->MountLink); - v->Volume = (IO_VOLUME*)0; - if (gMountedCount) - gMountedCount--; - uint32_t mountedCount = gMountedCount; - KeReleaseMutex(&gFatMountMutex); - - (void)IoRemoveVolume(volume, IoVolumeBackingRemoved); - IoReleaseVolume(volume); // Drop the active mount-list reference. - DbgPrint("FAT: disk=%u lba=%llu %s; unmounted volume=%u; total=%u\n", - partition->DiskIndex, (unsigned long long)partition->StartLba, - change == IoPartitionSurpriseRemoved ? "surprise removed" : "removed", - instanceId, mountedCount); -} - -// Receives immutable partition extents from the I/O core; FAT never parses partition tables. -static void FatPartitionChange(const IO_PARTITION_INFO* partition, - IO_PARTITION_CHANGE change, - void* context) { - (void)context; - switch (change) { - case IoPartitionArrived: - (void)FatTryMountPartition(partition); - break; - case IoPartitionSurpriseRemoved: - case IoPartitionRemoved: - FatUnmountPartition(partition, change); - break; - default: - break; - } -} - -// Initializes FAT and subscribes to the authoritative kernel partition stream. -KSTATUS DriverEntry(const NTL_DDK* ddk, NTL_DRIVER_INIT* init) { - DbgPrint("FAT: Init\n"); - if (!ddk) - return NTLSTATUS_INVALID_PARAMETER; - (void)init; - - FatCacheInvalidate(); - LIST_INIT(&gFatMounts); - gMountedCount = 0; - FatMountEnsureMutex(); - - KSTATUS status = IoRegisterPartitionChangeCallback(FatPartitionChange, NULL); - if (!KSUCCESS(status)) { - DbgPrint("FAT: partition callback registration failed status=0x%x\n", status); - return status; - } - - if (!gMountedCount) - DbgPrint("FAT: no mountable FAT partitions found; driver resident for partition changes.\n"); - DbgPrint("FAT: Init complete; mounted %u volume(s).\n", gMountedCount); - return NTLSTATUS_SUCCESS; + return FatDirentUpdateMetadata(v, lba, offset, &u); } diff --git a/code/drivers/fs/fat/fat.h b/code/drivers/fs/fat/fat.h index 25e40ef..38fcfd7 100644 --- a/code/drivers/fs/fat/fat.h +++ b/code/drivers/fs/fat/fat.h @@ -1,4 +1,4 @@ -// FAT driver internals shared by mount, directory, and file I/O code. +// FAT provider entry points shared by the driver build. #pragma once diff --git a/code/drivers/fs/fat/fat_async.c b/code/drivers/fs/fat/fat_async.c new file mode 100644 index 0000000..e40a07d --- /dev/null +++ b/code/drivers/fs/fat/fat_async.c @@ -0,0 +1,465 @@ +// FAT asynchronous file I/O. + +#include "fat_internal.h" + +// Native async read/write +// Metadata work (FAT traversal/allocation and partial-cluster read-modify +// preparation) still runs synchronously in the caller/completion context, but +// file data transfers are submitted as disk IRPs and completion is chained from +// the driver's IRP completion callback instead of the generic I/O worker. + +typedef struct FAT_ASYNC_RW { + LIST_ENTRY Link; + FAT_VOL* Vol; + FAT_FILE* File; + uint8_t IsWrite; + + uint64_t Offset; + uint32_t OffsetInCluster; + uint32_t Cluster; + uint32_t LogicalCluster; + size_t Remaining; + size_t Done; + size_t Requested; + + uint8_t* Dst; + const uint8_t* Src; + uint8_t* RunBuf; + + uint32_t CurrentNext; + uint32_t CurrentLast; + uint32_t CurrentClusters; + uint32_t CurrentOffset; + size_t CurrentCopy; + + IRP Irp; + DEVICE_OBJECT* CurrentDevice; + IO_FS_ASYNC_COMPLETE Complete; + void* CompleteContext; +} FAT_ASYNC_RW; + +static SPINLOCK gFatAsyncLock = SPINLOCK_INIT; +static LIST_ENTRY gFatAsyncQueue; +static SEMAPHORE* gFatAsyncSemaphore; +static volatile int gFatAsyncInitState; + +static void FatAsyncReadSubmitNext(FAT_ASYNC_RW* ctx); +static void FatAsyncWriteSubmitNext(FAT_ASYNC_RW* ctx); + +// Implements the async Worker operation. +static void FatAsyncWorker(void* arg) { + (void)arg; + + while (true) { + KSTATUS s = KeWaitSemaphore(gFatAsyncSemaphore, KE_WAIT_INFINITE); + if (!KSUCCESS(s)) { + KeYield(); + continue; + } + + uintptr_t flags = KeAcquireSpinlock(&gFatAsyncLock); + LIST_ENTRY* node = ListPopHead(&gFatAsyncQueue); + KeReleaseSpinlock(&gFatAsyncLock, flags); + if (!node) continue; + + FAT_ASYNC_RW* ctx = CONTAINING_RECORD(node, FAT_ASYNC_RW, Link); + if (ctx->IsWrite) + FatAsyncWriteSubmitNext(ctx); + else + FatAsyncReadSubmitNext(ctx); + } +} + +// Implements the ensure Async Worker Started operation. +static KSTATUS FatEnsureAsyncWorkerStarted(void) { + uintptr_t flags = KeAcquireSpinlock(&gFatAsyncLock); + if (gFatAsyncInitState == 2) { + KeReleaseSpinlock(&gFatAsyncLock, flags); + return NTLSTATUS_SUCCESS; + } + if (gFatAsyncInitState == 1) { + KeReleaseSpinlock(&gFatAsyncLock, flags); + while (gFatAsyncInitState == 1) + KeYield(); + return gFatAsyncInitState == 2 ? NTLSTATUS_SUCCESS : NTLSTATUS_FAILURE; + } + + gFatAsyncInitState = 1; + LIST_INIT(&gFatAsyncQueue); + KeReleaseSpinlock(&gFatAsyncLock, flags); + + KSTATUS s = KeSemaphoreCreate(0, 0x7fffffff, &gFatAsyncSemaphore); + if (KSUCCESS(s)) { + THREAD* worker = (THREAD*)0; + const KE_THREAD_CREATE_PARAMETERS parameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "fat.async"); + s = KeCreateKernelThread((uintptr_t)FatAsyncWorker, 0, + ¶meters, &worker); + (void)worker; + } + + flags = KeAcquireSpinlock(&gFatAsyncLock); + gFatAsyncInitState = KSUCCESS(s) ? 2 : 0; + KeReleaseSpinlock(&gFatAsyncLock, flags); + return s; +} + +// Implements the async Queue Continuation operation. +static void FatAsyncQueueContinuation(FAT_ASYNC_RW* ctx) { + if (!ctx) return; + + uintptr_t flags = KeAcquireSpinlock(&gFatAsyncLock); + ListInsertTail(&gFatAsyncQueue, &ctx->Link); + KeReleaseSpinlock(&gFatAsyncLock, flags); + KeSignalSemaphore(gFatAsyncSemaphore); +} + +// Implements the async Finish operation. +static void FatAsyncFinish(FAT_ASYNC_RW* ctx, KSTATUS status, size_t information) { + if (!ctx) return; + if (ctx->CurrentDevice) { + IoDereferenceDeviceObject(ctx->CurrentDevice); + ctx->CurrentDevice = (DEVICE_OBJECT*)0; + } + if (ctx->RunBuf) MmFreePool(ctx->RunBuf); + IO_FS_ASYNC_COMPLETE complete = ctx->Complete; + void* completeContext = ctx->CompleteContext; + MmFreePool(ctx); + if (complete) complete(completeContext, status, information); +} + +// Implements the async Read Irp Complete operation. +static void FatAsyncReadIrpComplete(IRP* irp, void* completeCtx) { + FAT_ASYNC_RW* ctx = (FAT_ASYNC_RW*)completeCtx; + if (!ctx) return; + if (ctx->CurrentDevice) { + IoDereferenceDeviceObject(ctx->CurrentDevice); + ctx->CurrentDevice = (DEVICE_OBJECT*)0; + } + + if (!KSUCCESS(irp->Status)) { + FatAsyncFinish(ctx, irp->Status, ctx->Done); + return; + } + + memcpy(ctx->Dst, ctx->RunBuf + ctx->CurrentOffset, ctx->CurrentCopy); + ctx->Dst += ctx->CurrentCopy; + ctx->Done += ctx->CurrentCopy; + ctx->Remaining -= ctx->CurrentCopy; + ctx->OffsetInCluster = 0; + ctx->LogicalCluster += ctx->CurrentClusters; + ctx->Cluster = ctx->CurrentNext; + if (ctx->CurrentClusters) { + uint32_t sectors = ctx->CurrentClusters * ctx->Vol->SectorsPerCluster; + uint32_t merges = ctx->CurrentClusters - 1u; + FatStatsAddReadRun(ctx->Vol, sectors, merges); + } + + FatAsyncQueueContinuation(ctx); +} + +// Implements the async Read Submit Next operation. +static void FatAsyncReadSubmitNext(FAT_ASYNC_RW* ctx) { + if (ctx->Remaining == 0) { + FatAsyncFinish(ctx, NTLSTATUS_SUCCESS, ctx->Done); + return; + } + + if (!FatIsDataCluster(ctx->Vol, ctx->Cluster)) { + if (ctx->Done < ctx->Requested) + memset(ctx->Dst, 0, ctx->Requested - ctx->Done); + FatAsyncFinish(ctx, NTLSTATUS_DEVICE_ERROR, ctx->Done); + return; + } + + FAT_VOL* v = ctx->Vol; + uint32_t runStart = ctx->Cluster; + uint32_t runSectors = 0; + uint32_t next = 0; + if (!FatBuildReadRun(ctx->File, ctx->LogicalCluster, ctx->Cluster, + FAT_READ_RUN_MAX_SECTORS, &runSectors, &next)) { + FatAsyncFinish(ctx, NTLSTATUS_DEVICE_ERROR, ctx->Done); + return; + } + + uint32_t runBytes = runSectors * 512u; + size_t avail = runBytes - ctx->OffsetInCluster; + size_t copy = avail < ctx->Remaining ? avail : ctx->Remaining; + + ctx->CurrentNext = next; + ctx->CurrentLast = runStart + (runSectors / v->SectorsPerCluster) - 1u; + ctx->CurrentClusters = runSectors / v->SectorsPerCluster; + ctx->CurrentOffset = ctx->OffsetInCluster; + ctx->CurrentCopy = copy; + + ctx->CurrentDevice = IoReferenceDiskDevice(v->DriveIndex); + if (!ctx->CurrentDevice) { + FatAsyncFinish(ctx, NTLSTATUS_NOT_FOUND, ctx->Done); + return; + } + + IoBuildReadWriteIrp(&ctx->Irp, IRP_MJ_READ, + FatClusterToLba(v, runStart) * 512ull, + runSectors * 512u, + ctx->RunBuf); + ctx->Irp.CompletionFn = FatAsyncReadIrpComplete; + ctx->Irp.CompletionCtx = ctx; + + KSTATUS s = IoSubmitIrp(ctx->CurrentDevice, &ctx->Irp); + if (s != NTLSTATUS_PENDING) { + IoDereferenceDeviceObject(ctx->CurrentDevice); + ctx->CurrentDevice = (DEVICE_OBJECT*)0; + FatAsyncFinish(ctx, s, ctx->Done); + } +} + +KSTATUS FatReadAsync(IO_VOLUME_PROVIDER* fs, void* fileCtx, + uint64_t offset, void* buf, size_t size, + IO_FS_ASYNC_COMPLETE complete, + void* completeContext) { + (void)fs; + if (!complete) return NTLSTATUS_INVALID_PARAMETER; + if (!fileCtx || (!buf && size)) { + complete(completeContext, NTLSTATUS_INVALID_PARAMETER, 0); + return NTLSTATUS_PENDING; + } + + FAT_FILE* f = (FAT_FILE*)fileCtx; + FAT_VOL* v = f->Vol; + if (offset >= f->FileSize || size == 0) { + complete(completeContext, NTLSTATUS_SUCCESS, 0); + return NTLSTATUS_PENDING; + } + if (offset + size > f->FileSize) + size = (size_t)(f->FileSize - offset); + + KSTATUS ws = FatEnsureAsyncWorkerStarted(); + if (!KSUCCESS(ws)) { + complete(completeContext, ws, 0); + return NTLSTATUS_PENDING; + } + + FAT_ASYNC_RW* ctx = (FAT_ASYNC_RW*)MmAllocatePool(sizeof(FAT_ASYNC_RW)); + if (!ctx) { + complete(completeContext, NTLSTATUS_NO_MEMORY, 0); + return NTLSTATUS_PENDING; + } + memset(ctx, 0, sizeof(*ctx)); + LIST_INIT(&ctx->Link); + + ctx->RunBuf = (uint8_t*)MmAllocatePool(FAT_READ_RUN_MAX_SECTORS * 512u); + if (!ctx->RunBuf) { + MmFreePool(ctx); + complete(completeContext, NTLSTATUS_NO_MEMORY, 0); + return NTLSTATUS_PENDING; + } + + ctx->Vol = v; + ctx->File = f; + ctx->IsWrite = 0; + ctx->Offset = offset; + ctx->OffsetInCluster = (uint32_t)(offset % v->BytesPerCluster); + ctx->LogicalCluster = (uint32_t)(offset / v->BytesPerCluster); + ctx->Cluster = FatResolveFileCluster(f, ctx->LogicalCluster); + ctx->Remaining = size; + ctx->Requested = size; + ctx->Dst = (uint8_t*)buf; + ctx->Complete = complete; + ctx->CompleteContext = completeContext; + + FatAsyncQueueContinuation(ctx); + return NTLSTATUS_PENDING; +} + +// Implements the async Write Irp Complete operation. +static void FatAsyncWriteIrpComplete(IRP* irp, void* completeCtx) { + FAT_ASYNC_RW* ctx = (FAT_ASYNC_RW*)completeCtx; + if (!ctx) return; + if (ctx->CurrentDevice) { + IoDereferenceDeviceObject(ctx->CurrentDevice); + ctx->CurrentDevice = (DEVICE_OBJECT*)0; + } + + if (!KSUCCESS(irp->Status)) { + FatAsyncFinish(ctx, irp->Status, ctx->Done); + return; + } + + ctx->Src += ctx->CurrentCopy; + ctx->Done += ctx->CurrentCopy; + ctx->Remaining -= ctx->CurrentCopy; + ctx->OffsetInCluster = 0; + ctx->Cluster = ctx->CurrentNext; + + FatAsyncQueueContinuation(ctx); +} + +// Implements the async Write Submit Next operation. +static void FatAsyncWriteSubmitNext(FAT_ASYNC_RW* ctx) { + if (ctx->Remaining == 0) { + uint64_t newEnd = ctx->Offset + ctx->Done; + if (ctx->Done > 0) { + FAT_DIRENT_UPDATE u; + memset(&u, 0, sizeof(u)); + FatQueryEncodedTime(&u.Timestamp); + u.Flags = FAT_DIRENT_UPDATE_WRITE_TIME; + if (newEnd > ctx->File->FileSize) { + ctx->File->FileSize = (uint32_t)newEnd; + u.Flags |= FAT_DIRENT_UPDATE_SIZE; + u.Size = ctx->File->FileSize; + } + FatDirentUpdateMetadata(ctx->Vol, ctx->File->DirentLba, + ctx->File->DirentOffset, &u); + } + FatAsyncFinish(ctx, NTLSTATUS_SUCCESS, ctx->Done); + return; + } + + FAT_VOL* v = ctx->Vol; + if (!FatIsDataCluster(ctx->Vol, ctx->Cluster)) { + if (FatIsDataCluster(ctx->Vol, ctx->CurrentLast)) { + ctx->Cluster = FatAppendCluster(v, ctx->CurrentLast); + if (!ctx->Cluster) { + FatAsyncFinish(ctx, NTLSTATUS_NO_MEMORY, ctx->Done); + return; + } + } else { + FatAsyncFinish(ctx, NTLSTATUS_DEVICE_ERROR, ctx->Done); + return; + } + } + + uint32_t runStart = ctx->Cluster; + uint32_t runSectors = v->SectorsPerCluster; + uint32_t last = ctx->Cluster; + uint32_t next = FatEntry(v, last); + + if (ctx->OffsetInCluster == 0) { + while (next == last + 1 && FatIsDataCluster(v, next) && + runSectors + v->SectorsPerCluster <= FAT_WRITE_RUN_MAX_SECTORS) { + last = next; + next = FatEntry(v, last); + runSectors += v->SectorsPerCluster; + } + } + + uint32_t runBytes = runSectors * 512u; + size_t avail = runBytes - ctx->OffsetInCluster; + size_t copy = avail < ctx->Remaining ? avail : ctx->Remaining; + + bool partial = (ctx->OffsetInCluster > 0) || (copy < (size_t)runBytes); + if (partial) { + KSTATUS rs = FatReadSectors(v, FatClusterToLba(v, runStart), runSectors, ctx->RunBuf); + if (!KSUCCESS(rs)) { + FatAsyncFinish(ctx, rs, ctx->Done); + return; + } + } + + memcpy(ctx->RunBuf + ctx->OffsetInCluster, ctx->Src, copy); + + ctx->CurrentNext = next; + ctx->CurrentLast = last; + ctx->CurrentOffset = ctx->OffsetInCluster; + ctx->CurrentCopy = copy; + + ctx->CurrentDevice = IoReferenceDiskDevice(v->DriveIndex); + if (!ctx->CurrentDevice) { + FatAsyncFinish(ctx, NTLSTATUS_NOT_FOUND, ctx->Done); + return; + } + + IoBuildReadWriteIrp(&ctx->Irp, IRP_MJ_WRITE, + FatClusterToLba(v, runStart) * 512ull, + runSectors * 512u, + ctx->RunBuf); + ctx->Irp.CompletionFn = FatAsyncWriteIrpComplete; + ctx->Irp.CompletionCtx = ctx; + + KSTATUS s = IoSubmitIrp(ctx->CurrentDevice, &ctx->Irp); + if (s != NTLSTATUS_PENDING) { + IoDereferenceDeviceObject(ctx->CurrentDevice); + ctx->CurrentDevice = (DEVICE_OBJECT*)0; + FatAsyncFinish(ctx, s, ctx->Done); + } +} + +KSTATUS FatWriteAsync(IO_VOLUME_PROVIDER* fs, void* fileCtx, + uint64_t offset, const void* buf, size_t size, + IO_FS_ASYNC_COMPLETE complete, + void* completeContext) { + (void)fs; + if (!complete) return NTLSTATUS_INVALID_PARAMETER; + if (!fileCtx || (!buf && size)) { + complete(completeContext, NTLSTATUS_INVALID_PARAMETER, 0); + return NTLSTATUS_PENDING; + } + if (size == 0) { + complete(completeContext, NTLSTATUS_SUCCESS, 0); + return NTLSTATUS_PENDING; + } + + KSTATUS ws = FatEnsureAsyncWorkerStarted(); + if (!KSUCCESS(ws)) { + complete(completeContext, ws, 0); + return NTLSTATUS_PENDING; + } + + FAT_FILE* f = (FAT_FILE*)fileCtx; + FAT_VOL* v = f->Vol; + FatFileInvalidateChainCache(f); + + if (f->StartCluster < 2) { + uint32_t first = FatAllocCluster(v); + if (!first) { + complete(completeContext, NTLSTATUS_NO_MEMORY, 0); + return NTLSTATUS_PENDING; + } + f->StartCluster = first; + FatDirentUpdateFirstCluster(v, f->DirentLba, f->DirentOffset, first); + } + + uint32_t clusterIndex = (uint32_t)(offset / v->BytesPerCluster); + uint32_t cluster = f->StartCluster; + for (uint32_t i = 0; i < clusterIndex; i++) { + uint32_t next = FatEntry(v, cluster); + if (!FatIsDataCluster(v, next)) { + next = FatAppendCluster(v, cluster); + if (!next) { + complete(completeContext, NTLSTATUS_NO_MEMORY, 0); + return NTLSTATUS_PENDING; + } + } + cluster = next; + } + + FAT_ASYNC_RW* ctx = (FAT_ASYNC_RW*)MmAllocatePool(sizeof(FAT_ASYNC_RW)); + if (!ctx) { + complete(completeContext, NTLSTATUS_NO_MEMORY, 0); + return NTLSTATUS_PENDING; + } + memset(ctx, 0, sizeof(*ctx)); + LIST_INIT(&ctx->Link); + + ctx->RunBuf = (uint8_t*)MmAllocatePool(FAT_WRITE_RUN_MAX_SECTORS * 512u); + if (!ctx->RunBuf) { + MmFreePool(ctx); + complete(completeContext, NTLSTATUS_NO_MEMORY, 0); + return NTLSTATUS_PENDING; + } + + ctx->Vol = v; + ctx->File = f; + ctx->IsWrite = 1; + ctx->Offset = offset; + ctx->OffsetInCluster = (uint32_t)(offset % v->BytesPerCluster); + ctx->Cluster = cluster; + ctx->Remaining = size; + ctx->Requested = size; + ctx->Src = (const uint8_t*)buf; + ctx->Complete = complete; + ctx->CompleteContext = completeContext; + + FatAsyncQueueContinuation(ctx); + return NTLSTATUS_PENDING; +} diff --git a/code/drivers/fs/fat/fat_directory.c b/code/drivers/fs/fat/fat_directory.c new file mode 100644 index 0000000..f4889af --- /dev/null +++ b/code/drivers/fs/fat/fat_directory.c @@ -0,0 +1,1269 @@ +// FAT name handling, path traversal, and directory mutation. + +#include "fat_internal.h" + +// 8.3 name matching / assembly. + +static int MatchRaw83(const char dirName[8], const char dirExt[3], + const char wantName[8], const char wantExt[3]) { + return memcmp(dirName, wantName, 8) == 0 && + memcmp(dirExt, wantExt, 3) == 0; +} + +static char FatUpperChar(char c); +static int FatIsLowerChar(char c); +static int FatIsValidShortChar(char c); + +// Implements the make83 Name V operation. +static void Make83NameV(SYS_STRING_VIEW src, char name8[8], char ext3[3]) { + memset(name8, ' ', 8); + memset(ext3, ' ', 3); + if (!SysSvIsValid(src) || src.Length == 0) return; + + size_t baseStart = 0; + size_t dot = (size_t)-1; + if (src.Data[0] == '.' && src.Length > 1) { + baseStart = 1; + } else { + for (size_t i = 0; i < src.Length; i++) + if (src.Data[i] == '.') dot = i; + } + + size_t end = (dot != (size_t)-1) ? dot : src.Length; + int ni = 0; + for (size_t i = baseStart; i < end && ni < 8; i++) { + char c = FatUpperChar(src.Data[i]); + name8[ni++] = c; + } + + if (dot != (size_t)-1) { + int ei = 0; + for (size_t i = dot + 1; i < src.Length && ei < 3; i++) { + char c = FatUpperChar(src.Data[i]); + ext3[ei++] = c; + } + } +} + +// LFN assembly / matching. + +// Implements the fat Upper Char operation. +static char FatUpperChar(char c) { + return (c >= 'a' && c <= 'z') ? (char)(c - 32) : c; +} + +// Implements the fat Is Lower Char operation. +static int FatIsLowerChar(char c) { + return c >= 'a' && c <= 'z'; +} + +// Implements the fat Is Valid Short Char operation. +static int FatIsValidShortChar(char c) { + c = FatUpperChar(c); + if (c >= 'A' && c <= 'Z') return 1; + if (c >= '0' && c <= '9') return 1; + switch (c) { + case '$': case '%': case '\'': case '-': case '_': case '@': + case '~': case '`': case '!': case '(': case ')': case '{': + case '}': case '^': case '#': case '&': + return 1; + default: + return 0; + } +} + +// True when the user spelling can refer to a normal FAT 8.3 entry. This is +// intentionally structural and case-insensitive: "doom1.wad" must still open +// an existing short entry named DOOM1.WAD. Leading-dot names are not 8.3. +static int FatNameIsStructurallyShort83V(SYS_STRING_VIEW name) { + if (!SysSvIsValid(name) || name.Length == 0) return 0; + if (name.Data[0] == '.') return 0; + + size_t dot = (size_t)-1; + for (size_t i = 0; i < name.Length; i++) { + char c = name.Data[i]; + if (c == '.') { + if (dot != (size_t)-1) return 0; + dot = i; + } else if (!FatIsValidShortChar(c)) { + return 0; + } + } + + size_t baseLen = (dot != (size_t)-1) ? dot : name.Length; + size_t extLen = (dot != (size_t)-1) ? (name.Length - dot - 1) : 0; + return baseLen >= 1 && baseLen <= 8 && extLen <= 3; +} + +// Implements the fat Component Has Lower V operation. +static int FatComponentHasLowerV(SYS_STRING_VIEW name, size_t start, size_t end) { + for (size_t i = start; i < end; i++) + if (FatIsLowerChar(name.Data[i])) return 1; + return 0; +} + +// Implements the fat Component Has Upper V operation. +static int FatComponentHasUpperV(SYS_STRING_VIEW name, size_t start, size_t end) { + for (size_t i = start; i < end; i++) + if (name.Data[i] >= 'A' && name.Data[i] <= 'Z') return 1; + return 0; +} + +static void FatSplitShortNameV(SYS_STRING_VIEW name, size_t* baseEnd, + size_t* extStart, size_t* extEnd) { + size_t dot = (size_t)-1; + for (size_t i = 0; i < name.Length; i++) + if (name.Data[i] == '.') dot = i; + if (dot == (size_t)-1) { + *baseEnd = name.Length; + *extStart = name.Length; + *extEnd = name.Length; + } else { + *baseEnd = dot; + *extStart = dot + 1u; + *extEnd = name.Length; + } +} + +// Creation uses LFN only when the spelling cannot be represented by a short +// entry, or when mixed-case 8.3 spelling would be lost. Plain lowercase +// 8.3 names are represented with FAT's NT case bits, not VFAT LFN entries. +static int FatNameNeedsLfnForCreateV(SYS_STRING_VIEW name) { + if (!FatNameIsStructurallyShort83V(name)) return 1; + + size_t baseEnd, extStart, extEnd; + FatSplitShortNameV(name, &baseEnd, &extStart, &extEnd); + + int baseLower = FatComponentHasLowerV(name, 0, baseEnd); + int baseUpper = FatComponentHasUpperV(name, 0, baseEnd); + int extLower = FatComponentHasLowerV(name, extStart, extEnd); + int extUpper = FatComponentHasUpperV(name, extStart, extEnd); + + if ((baseLower && baseUpper) || (extLower && extUpper)) return 1; + return 0; +} + +// Implements the fat Short Name Case Flags V operation. +static uint8_t FatShortNameCaseFlagsV(SYS_STRING_VIEW name) { + if (!FatNameIsStructurallyShort83V(name)) return 0; + size_t baseEnd, extStart, extEnd; + FatSplitShortNameV(name, &baseEnd, &extStart, &extEnd); + uint8_t flags = 0; + if (FatComponentHasLowerV(name, 0, baseEnd) && + !FatComponentHasUpperV(name, 0, baseEnd)) + flags |= FAT_NTRES_BASE_LOWER; + if (extStart < extEnd && + FatComponentHasLowerV(name, extStart, extEnd) && + !FatComponentHasUpperV(name, extStart, extEnd)) + flags |= FAT_NTRES_EXT_LOWER; + return flags; +} + +// Implements the lfn Checksum operation. +static uint8_t LfnChecksum(const char name8[8], const char ext3[3]) { + uint8_t sum = 0; + for (int i = 0; i < 11; i++) { + uint8_t c = (uint8_t)(i < 8 ? name8[i] : ext3[i - 8]); + sum = (uint8_t)(((sum & 1) ? 0x80 : 0) + (sum >> 1) + c); + } + return sum; +} + +// Implements the lfn Set Char operation. +static void LfnSetChar(LFN_ENTRY* e, int idx, uint16_t value) { + if (idx < 5) e->Name1[idx] = value; + else if (idx < 11) e->Name2[idx - 5] = value; + else e->Name3[idx - 11] = value; +} + +static void BuildLfnEntry(LFN_ENTRY* e, const char* name, size_t nameLen, + uint32_t ordinal, uint32_t lfnCount, + uint8_t checksum) { + memset(e, 0, sizeof(*e)); + e->Order = (uint8_t)ordinal; + if (ordinal == lfnCount) e->Order |= 0x40; + e->Attr = ATTR_LFN; + e->Type = 0; + e->Checksum = checksum; + e->FirstCluster = 0; + + size_t start = (size_t)(ordinal - 1) * 13u; + for (int i = 0; i < 13; i++) { + size_t pos = start + (size_t)i; + uint16_t ch; + if (pos < nameLen) ch = (uint8_t)name[pos]; + else if (pos == nameLen) ch = 0x0000; + else ch = 0xFFFF; + LfnSetChar(e, i, ch); + } +} + +// Creates legacy Broken Dot83. +static void MakeLegacyBrokenDot83(const char* src, char name8[8], char ext3[3]) { + // This exactly models the pre-fix bug for ".doomrc": empty name + "DOO". + memset(name8, ' ', 8); + memset(ext3, ' ', 3); + const char* dot = NULL; + for (const char* p = src; *p; p++) if (*p == '.') dot = p; + if (!dot) return; + int ei = 0; + for (const char* p = dot + 1; *p && ei < 3; p++) + ext3[ei++] = FatUpperChar(*p); +} + +static int ShortNameExistsRaw(FAT_VOL* v, uint32_t dirCluster, + const char name8[8], const char ext3[3]) { + uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!clusterBuf) return 1; // fail closed; caller should pick another alias + + uint32_t cursor; uint64_t lba; uint32_t sectors; + if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { + MmFreePool(clusterBuf); + return 0; + } + + do { + if (!KSUCCESS(FatReadSectors(v, lba, sectors, clusterBuf))) break; + + uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); + DIRENT* dir = (DIRENT*)clusterBuf; + for (uint32_t i = 0; i < entries; i++) { + if (dir[i].Name[0] == 0x00) { MmFreePool(clusterBuf); return 0; } + if ((uint8_t)dir[i].Name[0] == 0xE5 || dir[i].Attr == ATTR_LFN) continue; + if (MatchRaw83(dir[i].Name, dir[i].Ext, name8, ext3)) { + MmFreePool(clusterBuf); + return 1; + } + } + } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); + + MmFreePool(clusterBuf); + return 0; +} + +// Implements the fat Alias Append Char operation. +static void FatAliasAppendChar(char* dst, int* len, int maxLen, char c) { + c = FatUpperChar(c); + if (!FatIsValidShortChar(c)) return; + if (*len < maxLen) dst[(*len)++] = c; +} + +// Builds alias Stem Ext V. +static void BuildAliasStemExtV(SYS_STRING_VIEW src, char stem[9], char ext3[3]) { + memset(stem, 0, 9); + memset(ext3, ' ', 3); + if (!SysSvIsValid(src)) return; + + size_t baseStart = 0; + size_t dot = (size_t)-1; + if (src.Length > 1 && src.Data[0] == '.') { + // Hidden/Unix-style names like .doomrc become alias stem DOOMRC. + baseStart = 1; + } else { + for (size_t i = 0; i < src.Length; i++) + if (src.Data[i] == '.') dot = i; + } + + int slen = 0; + size_t end = (dot != (size_t)-1) ? dot : src.Length; + for (size_t i = baseStart; i < end; i++) + FatAliasAppendChar(stem, &slen, 8, src.Data[i]); + if (slen == 0) { + memcpy(stem, "FILE", 4); + slen = 4; + } + stem[slen] = '\0'; + + if (dot != (size_t)-1 && dot + 1 < src.Length) { + int ei = 0; + for (size_t i = dot + 1; i < src.Length && ei < 3; i++) { + char c = FatUpperChar(src.Data[i]); + if (!FatIsValidShortChar(c)) continue; + ext3[ei++] = c; + } + } +} + +static void MakeAliasAttempt(const char stem[9], const char extIn[3], + uint32_t attempt, char name8[8], char ext3[3]) { + memset(name8, ' ', 8); + memcpy(ext3, extIn, 3); + + char suffix[8]; + int suffixLen = 0; + suffix[suffixLen++] = '~'; + char digits[7]; + int digitLen = 0; + uint32_t n = attempt; + do { + digits[digitLen++] = (char)('0' + (n % 10)); + n /= 10; + } while (n && digitLen < (int)sizeof(digits)); + for (int i = digitLen - 1; i >= 0; i--) + suffix[suffixLen++] = digits[i]; + + int stemLen = (int)strlen(stem); + int keep = 8 - suffixLen; + if (keep < 1) keep = 1; + if (stemLen < keep) keep = stemLen; + memcpy(name8, stem, keep); + memcpy(name8 + keep, suffix, suffixLen); +} + +static int MakeUniqueShortAliasV(FAT_VOL* v, uint32_t dirCluster, + SYS_STRING_VIEW longName, + char name8[8], char ext3[3]) { + char stem[9], extCandidate[3]; + BuildAliasStemExtV(longName, stem, extCandidate); + + for (uint32_t attempt = 1; attempt < 1000000; attempt++) { + MakeAliasAttempt(stem, extCandidate, attempt, name8, ext3); + if (!ShortNameExistsRaw(v, dirCluster, name8, ext3)) return 1; + } + return 0; +} + +// Creates legacy Broken Dot83 V. +static void MakeLegacyBrokenDot83V(SYS_STRING_VIEW src, char name8[8], char ext3[3]) { + // This exactly models the pre-fix bug for ".doomrc": empty name + "DOO". + memset(name8, ' ', 8); + memset(ext3, ' ', 3); + if (!SysSvIsValid(src)) return; + size_t dot = (size_t)-1; + for (size_t i = 0; i < src.Length; i++) if (src.Data[i] == '.') dot = i; + if (dot == (size_t)-1) return; + int ei = 0; + for (size_t i = dot + 1; i < src.Length && ei < 3; i++) + ext3[ei++] = FatUpperChar(src.Data[i]); +} + +static KSTATUS DeleteLegacyBrokenDotAliasV(FAT_VOL* v, uint32_t dirCluster, + SYS_STRING_VIEW longName) { + if (!SysSvIsValid(longName) || longName.Length < 2 || longName.Data[0] != '.') return NTLSTATUS_SUCCESS; + + char badName[8], badExt[3]; + MakeLegacyBrokenDot83V(longName, badName, badExt); + + uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!clusterBuf) return NTLSTATUS_NO_MEMORY; + + uint32_t cursor; uint64_t lba; uint32_t sectors; + if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { + MmFreePool(clusterBuf); + return NTLSTATUS_SUCCESS; + } + + do { + KSTATUS s = FatReadSectors(v, lba, sectors, clusterBuf); + if (!KSUCCESS(s)) { MmFreePool(clusterBuf); return s; } + + uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); + DIRENT* dir = (DIRENT*)clusterBuf; + for (uint32_t i = 0; i < entries; i++) { + if (dir[i].Name[0] == 0x00) { MmFreePool(clusterBuf); return NTLSTATUS_SUCCESS; } + if ((uint8_t)dir[i].Name[0] == 0xE5 || dir[i].Attr == ATTR_LFN) continue; + if (MatchRaw83(dir[i].Name, dir[i].Ext, badName, badExt)) { + int emptyBase = 1; + for (int j = 0; j < 8; j++) if (dir[i].Name[j] != ' ') emptyBase = 0; + if (!emptyBase) continue; + + // Do not free the cluster chain here. The broken entry may have + // aliased existing data; deleting the directory entry is enough to + // hide it from hosts and avoids freeing clusters still owned by a + // valid file. + dir[i].Name[0] = (char)0xE5; + s = FatWriteSectors(v, lba, sectors, clusterBuf); + MmFreePool(clusterBuf); + return s; + } + } + } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); + + MmFreePool(clusterBuf); + return NTLSTATUS_SUCCESS; +} + +// Implements the lfn Append operation. +void FatLfnAppend(char* lfn, const LFN_ENTRY* e) { + int seq = (e->Order & 0x3F) - 1; + if (seq < 0 || seq >= (FAT_MAX_LFN / 13)) return; + int pos = seq * 13; + uint16_t chars[13]; int idx = 0; + for (int i = 0; i < 5; i++) chars[idx++] = e->Name1[i]; + for (int i = 0; i < 6; i++) chars[idx++] = e->Name2[i]; + for (int i = 0; i < 2; i++) chars[idx++] = e->Name3[i]; + for (int i = 0; i < 13 && pos < FAT_MAX_LFN-1; i++, pos++) { + uint16_t c = chars[i]; + if (c == 0x0000 || c == 0xFFFF) { lfn[pos] = '\0'; break; } + lfn[pos] = (char)(c & 0x7F); + } +} + +// Directory search and view-native path traversal. +static int FatIsPathSep(char c) { + return c == '\\' || c == '/'; +} + +// Tests whether a non-empty FAT path view points at plausible kernel-owned storage. +static int FatStringViewDataIsPlausible(SYS_STRING_VIEW view) { + if (!SysSvIsValid(view)) return 0; + if (view.Length == 0) return 1; + uintptr_t data = (uintptr_t)view.Data; + return data >= 0x10000u; +} + +// Implements the name View Is Valid operation. +static int FatNameViewIsValid(SYS_STRING_VIEW name) { + if (!FatStringViewDataIsPlausible(name)) return 0; + if (name.Length == 0 || name.Length >= FAT_MAX_LFN) return 0; + for (size_t i = 0; i < name.Length; i++) { + char c = name.Data[i]; + if (c == '\0' || FatIsPathSep(c)) return 0; + } + return 1; +} + +// Implements the lfn Match V operation. +static int LfnMatchV(const char* lfn, SYS_STRING_VIEW want) { + if (!lfn || !SysSvIsValid(want)) return 0; + size_t i = 0; + while (lfn[i] && i < want.Length) { + char a = lfn[i], b = want.Data[i]; + if (a >= 'A' && a <= 'Z') a += 32; + if (b >= 'A' && b <= 'Z') b += 32; + if (a != b) return 0; + i++; + } + return lfn[i] == '\0' && i == want.Length; +} + +static int MatchName83V(const char* dirName, const char* dirExt, + SYS_STRING_VIEW want) { + char buf[13]; + int n = 0; + for (int i = 0; i < 8 && dirName[i] != ' '; i++) + buf[n++] = (char)(dirName[i] >= 'A' && dirName[i] <= 'Z' ? dirName[i]+32 : dirName[i]); + if (dirExt[0] != ' ') { + buf[n++] = '.'; + for (int i = 0; i < 3 && dirExt[i] != ' '; i++) + buf[n++] = (char)(dirExt[i] >= 'A' && dirExt[i] <= 'Z' ? dirExt[i]+32 : dirExt[i]); + } + + if ((size_t)n != want.Length) return 0; + for (int i = 0; i < n; i++) { + char a = buf[i], b = want.Data[i]; + if (a >= 'A' && a <= 'Z') a += 32; + if (b >= 'A' && b <= 'Z') b += 32; + if (a != b) return 0; + } + return 1; +} + +// Returns the path cache canonical byte used for hashes and comparisons. +static char FatPathCacheChar(char c) { + if (c == '/') return '\\'; + if (c >= 'A' && c <= 'Z') return (char)(c + 32); + return c; +} + +// Hashes a volume-relative path using the same case/separator rules as lookup. +static uint32_t FatPathCacheHash(SYS_STRING_VIEW path) { + uint32_t h = 2166136261u; + for (size_t i = 0; i < path.Length; i++) { + h ^= (uint8_t)FatPathCacheChar(path.Data[i]); + h *= 16777619u; + } + return h ? h : 1u; +} + +// Tests whether the cached path names the same FAT path as the caller's view. +static bool FatPathCachePathEquals(const FAT_PATH_CACHE_ENTRY* e, SYS_STRING_VIEW path) { + if (!e || e->PathLength != path.Length) return false; + for (size_t i = 0; i < path.Length; i++) { + if (FatPathCacheChar(e->Path[i]) != FatPathCacheChar(path.Data[i])) + return false; + } + return true; +} + +// Adds directory search counters without holding the lock during disk I/O. +static void FatPathCacheAddDirectoryStats(FAT_VOL* v, uint64_t clusters, uint64_t entries) { + if (!v) return; + uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); + v->Stats.DirectorySearches++; + v->Stats.DirectoryBlocksRead += clusters; + v->Stats.DirectoryEntriesScanned += entries; + KeReleaseSpinlock(&v->PathCacheLock, flags); +} + +// Looks up a positive path-cache entry for a full volume-relative path. +static bool FatPathCacheLookup(FAT_VOL* v, SYS_STRING_VIEW path, + uint32_t* outCluster, uint32_t* outSize, + uint8_t* outAttr, uint64_t* outDirentLba, + uint32_t* outDirentOffset) { + if (!v || path.Length == 0 || path.Length >= FAT_PATH_CACHE_MAX) + return false; + + uint32_t hash = FatPathCacheHash(path); + uint32_t slot = hash % FAT_PATH_CACHE_SLOTS; + bool hit = false; + + uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); + v->Stats.PathLookups++; + FAT_PATH_CACHE_ENTRY* e = &v->PathCache[slot]; + if (e->Valid && e->Generation == v->PathCacheGeneration && + e->Hash == hash && FatPathCachePathEquals(e, path)) { + if (outCluster) *outCluster = e->Cluster; + if (outSize) *outSize = e->Size; + if (outAttr) *outAttr = e->Attr; + if (outDirentLba) *outDirentLba = e->DirentLba; + if (outDirentOffset) *outDirentOffset = e->DirentOffset; + v->Stats.PathCacheHits++; + hit = true; + } else { + v->Stats.PathCacheMisses++; + } + KeReleaseSpinlock(&v->PathCacheLock, flags); + return hit; +} + +// Stores a positive path lookup in the volume-local path cache. +static void FatPathCacheStore(FAT_VOL* v, SYS_STRING_VIEW path, + uint32_t cluster, uint32_t size, uint8_t attr, + uint64_t direntLba, uint32_t direntOffset) { + if (!v || path.Length == 0 || path.Length >= FAT_PATH_CACHE_MAX) + return; + + uint32_t hash = FatPathCacheHash(path); + uint32_t slot = hash % FAT_PATH_CACHE_SLOTS; + + uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); + FAT_PATH_CACHE_ENTRY* e = &v->PathCache[slot]; + memset(e, 0, sizeof(*e)); + e->Valid = true; + e->Generation = v->PathCacheGeneration; + e->Hash = hash; + e->PathLength = path.Length; + memcpy(e->Path, path.Data, path.Length); + e->Path[path.Length] = '\0'; + e->Cluster = cluster; + e->Size = size; + e->Attr = attr; + e->DirentLba = direntLba; + e->DirentOffset = direntOffset; + v->Stats.PathCacheStores++; + KeReleaseSpinlock(&v->PathCacheLock, flags); +} + +// Invalidates every positive path-cache entry after a directory or size mutation. +void FatPathCacheInvalidate(FAT_VOL* v) { + if (!v) return; + uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); + v->PathCacheGeneration++; + if (v->PathCacheGeneration == 0) + v->PathCacheGeneration = 1; + v->Stats.PathCacheInvalidations++; + v->Stats.PathCacheGeneration = v->PathCacheGeneration; + for (uint32_t i = 0; i < FAT_PATH_CACHE_SLOTS; i++) + v->PathCache[i].Valid = false; + KeReleaseSpinlock(&v->PathCacheLock, flags); +} + +// Returns the current filesystem cache and traversal counters. +void FatQueryStats(IO_VOLUME_PROVIDER* fs, IO_FILESYSTEM_STATS* out) { + if (!fs || !out) return; + FAT_VOL* v = (FAT_VOL*)fs->Context; + if (!v) { memset(out, 0, sizeof(*out)); return; } + + uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); + *out = v->Stats; + out->PathCacheGeneration = v->PathCacheGeneration; + KeReleaseSpinlock(&v->PathCacheLock, flags); +} + +// Records one data read span and its physically contiguous cluster merges. +void FatStatsAddReadRun(FAT_VOL* v, uint32_t sectors, uint32_t merges) { + if (!v || !sectors) return; + uintptr_t flags = KeAcquireSpinlock(&v->PathCacheLock); + v->Stats.DataReadRuns++; + v->Stats.DataReadSectors += sectors; + v->Stats.DataReadRunMerges += merges; + if (v->Stats.MaxDataReadSpanSectors < sectors) + v->Stats.MaxDataReadSpanSectors = sectors; + KeReleaseSpinlock(&v->PathCacheLock, flags); +} + +// Initializes the per-open cluster-chain cache for a FAT file object. +void FatFileInitChainCache(FAT_FILE* f) { + if (!f) return; + f->ChainCacheLock = (SPINLOCK)SPINLOCK_INIT; + f->ChainCacheClock = 0; + memset(f->ChainCache, 0, sizeof(f->ChainCache)); +} + +// Invalidates cached FAT extents after the file's cluster chain changes. +void FatFileInvalidateChainCache(FAT_FILE* f) { + if (!f) return; + uintptr_t flags = KeAcquireSpinlock(&f->ChainCacheLock); + memset(f->ChainCache, 0, sizeof(f->ChainCache)); + f->ChainCacheClock = 0; + KeReleaseSpinlock(&f->ChainCacheLock, flags); +} + +// Locates the best cached starting point for a logical cluster index. +static bool FatChainCacheLookupStart(FAT_FILE* f, uint32_t logicalIndex, + uint32_t* outLogical, uint32_t* outCluster) { + if (!f || !outLogical || !outCluster) + return false; + + bool hit = false; + uint32_t bestLogical = 0; + uint32_t bestCluster = 0; + + uintptr_t flags = KeAcquireSpinlock(&f->ChainCacheLock); + for (uint32_t i = 0; i < FAT_CHAIN_CACHE_SLOTS; i++) { + FAT_CHAIN_CACHE_ENTRY* e = &f->ChainCache[i]; + if (!e->Valid || e->Length == 0) + continue; + + uint32_t end = e->LogicalStart + e->Length; + if (logicalIndex >= e->LogicalStart && logicalIndex < end) { + *outLogical = logicalIndex; + *outCluster = e->PhysicalStart + (logicalIndex - e->LogicalStart); + hit = true; + break; + } + + if (end <= logicalIndex && FatIsDataCluster(f->Vol, e->NextCluster)) { + if (!hit || end > bestLogical) { + bestLogical = end; + bestCluster = e->NextCluster; + hit = true; + } + } + } + + if (hit && bestCluster) { + *outLogical = bestLogical; + *outCluster = bestCluster; + } + KeReleaseSpinlock(&f->ChainCacheLock, flags); + return hit; +} + +// Stores a physically contiguous FAT extent in the per-open chain cache. +static void FatChainCacheStore(FAT_FILE* f, uint32_t logicalStart, + uint32_t physicalStart, uint32_t length, + uint32_t nextCluster) { + if (!f || !FatIsDataCluster(f->Vol, physicalStart) || length == 0) + return; + + uintptr_t flags = KeAcquireSpinlock(&f->ChainCacheLock); + uint32_t slot = f->ChainCacheClock++ % FAT_CHAIN_CACHE_SLOTS; + FAT_CHAIN_CACHE_ENTRY* e = &f->ChainCache[slot]; + e->Valid = true; + e->LogicalStart = logicalStart; + e->PhysicalStart = physicalStart; + e->Length = length; + e->NextCluster = nextCluster; + KeReleaseSpinlock(&f->ChainCacheLock, flags); +} + +// Resolves a logical file cluster through the cached FAT-chain extent map. +uint32_t FatResolveFileCluster(FAT_FILE* f, uint32_t logicalIndex) { + if (!f || !FatIsDataCluster(f->Vol, f->StartCluster)) + return 0; + if (logicalIndex == 0) + return f->StartCluster; + + uint32_t currentLogical = 0; + uint32_t cluster = f->StartCluster; + if (!FatChainCacheLookupStart(f, logicalIndex, ¤tLogical, &cluster)) { + currentLogical = 0; + cluster = f->StartCluster; + } + + while (currentLogical < logicalIndex && FatIsDataCluster(f->Vol, cluster)) { + cluster = FatEntry(f->Vol, cluster); + currentLogical++; + } + return cluster; +} + +// Builds and caches one physically contiguous read run from a starting cluster. +bool FatBuildReadRun(FAT_FILE* f, uint32_t logicalIndex, + uint32_t startCluster, uint32_t maxSectors, + uint32_t* outRunSectors, uint32_t* outNextCluster) { + if (!f || !outRunSectors || !outNextCluster) + return false; + FAT_VOL* v = f->Vol; + if (!v || !FatIsDataCluster(v, startCluster)) + return false; + + uint32_t runSectors = v->SectorsPerCluster; + uint32_t runClusters = 1; + uint32_t last = startCluster; + uint32_t next = FatEntry(v, last); + + while (next == last + 1 && FatIsDataCluster(v, next) && + runSectors + v->SectorsPerCluster <= maxSectors) { + last = next; + runClusters++; + runSectors += v->SectorsPerCluster; + next = FatEntry(v, last); + } + + FatChainCacheStore(f, logicalIndex, startCluster, runClusters, next); + *outRunSectors = runSectors; + *outNextCluster = next; + return true; +} + +static int SearchDirV(FAT_VOL* v, uint32_t dirCluster, SYS_STRING_VIEW name, + uint32_t* outCluster, uint32_t* outSize, uint8_t* outAttr, + uint64_t* outDirentLba, uint32_t* outDirentOffset) { + if (!FatNameViewIsValid(name)) return 0; + + uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!clusterBuf) return 0; + + char lfn[FAT_MAX_LFN]; int hasLfn = 0; memset(lfn, 0, sizeof(lfn)); + char wantName83[8], wantExt83[3]; + int allowShortMatch = FatNameIsStructurallyShort83V(name); + Make83NameV(name, wantName83, wantExt83); + uint64_t clustersRead = 0; + uint64_t entriesScanned = 0; + int found = 0; + + uint32_t cursor; uint64_t lba; uint32_t sectors; + if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) + goto done; + + do { + if (!KSUCCESS(FatReadSectors(v, lba, sectors, clusterBuf))) break; + clustersRead++; + + uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); + DIRENT* dir = (DIRENT*)clusterBuf; + + for (uint32_t i = 0; i < entries; i++) { + entriesScanned++; + if (dir[i].Name[0] == 0x00) goto done; + if ((uint8_t)dir[i].Name[0] == 0xE5) { hasLfn = 0; memset(lfn, 0, sizeof(lfn)); continue; } + if (dir[i].Attr == ATTR_LFN) { FatLfnAppend(lfn, (LFN_ENTRY*)&dir[i]); hasLfn = 1; continue; } + + int match = 0; + if (hasLfn && lfn[0]) match = LfnMatchV(lfn, name); + if (!match && allowShortMatch) + match = MatchName83V(dir[i].Name, dir[i].Ext, name); + if (!match && allowShortMatch) + match = MatchRaw83(dir[i].Name, dir[i].Ext, wantName83, wantExt83); + hasLfn = 0; memset(lfn, 0, sizeof(lfn)); + + if (match) { + *outCluster = FatDirentFirstCluster(v, &dir[i]); + *outSize = dir[i].FileSize; + *outAttr = dir[i].Attr; + if (outDirentLba || outDirentOffset) { + uint64_t eb = (uint64_t)i * sizeof(DIRENT); + if (outDirentLba) *outDirentLba = lba + eb / v->BytesPerSector; + if (outDirentOffset) *outDirentOffset = (uint32_t)(eb % v->BytesPerSector); + } + found = 1; + goto done; + } + } + } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); + +done: + FatPathCacheAddDirectoryStats(v, clustersRead, entriesScanned); + MmFreePool(clusterBuf); + return found; +} + +static int FatPathNextComponent(SYS_STRING_VIEW path, size_t* cursor, + SYS_STRING_VIEW* component) { + if (!cursor || !component || !SysSvIsValid(path)) return 0; + size_t pos = *cursor; + while (pos < path.Length && FatIsPathSep(path.Data[pos])) pos++; + if (pos >= path.Length) { + *cursor = pos; + *component = SYS_SV_NULL; + return 0; + } + + size_t start = pos; + while (pos < path.Length && !FatIsPathSep(path.Data[pos])) pos++; + *cursor = pos; + *component = SysSvFromParts(path.Data + start, pos - start); + return 1; +} + +KSTATUS FatLookupPathV(FAT_VOL* v, SYS_STRING_VIEW path, + uint32_t* outCluster, uint32_t* outSize, + uint8_t* outAttr, + uint64_t* outDirentLba, uint32_t* outDirentOffset) { + if (!v || !outCluster || !outSize || !FatStringViewDataIsPlausible(path)) + return NTLSTATUS_INVALID_PARAMETER; + if (SysSvContainsByte(path, '\0')) + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t cluster = v->RootCluster, size = 0; + uint8_t attr = ATTR_DIRECTORY; + uint64_t finalDirentLba = 0; + uint32_t finalDirentOffset = 0; + if (outDirentLba) *outDirentLba = 0; + if (outDirentOffset) *outDirentOffset = 0; + + if (FatPathCacheLookup(v, path, outCluster, outSize, &attr, + outDirentLba, outDirentOffset)) { + if (outAttr) *outAttr = attr; + return NTLSTATUS_SUCCESS; + } + + size_t cursor = 0; + SYS_STRING_VIEW component; + while (FatPathNextComponent(path, &cursor, &component)) { + if (component.Length >= FAT_MAX_LFN) return NTLSTATUS_BUFFER_TOO_SMALL; + if (!FatNameViewIsValid(component)) return NTLSTATUS_INVALID_PARAMETER; + if (!(attr & ATTR_DIRECTORY)) return NTLSTATUS_NOT_FOUND; + + uint32_t childCluster, childSize; uint8_t childAttr; + uint64_t childLba = 0; uint32_t childOffset = 0; + if (!SearchDirV(v, cluster, component, &childCluster, &childSize, + &childAttr, &childLba, &childOffset)) + return NTLSTATUS_NOT_FOUND; + + cluster = childCluster; size = childSize; attr = childAttr; + finalDirentLba = childLba; + finalDirentOffset = childOffset; + if (outDirentLba) *outDirentLba = childLba; + if (outDirentOffset) *outDirentOffset = childOffset; + } + + *outCluster = cluster; + *outSize = size; + if (outAttr) *outAttr = attr; + FatPathCacheStore(v, path, cluster, size, attr, + finalDirentLba, finalDirentOffset); + return NTLSTATUS_SUCCESS; +} + +KSTATUS FatTraverseV(FAT_VOL* v, SYS_STRING_VIEW path, + uint32_t* outCluster, uint32_t* outSize) { + return FatLookupPathV(v, path, outCluster, outSize, NULL, NULL, NULL); +} + +static KSTATUS SplitPathV(SYS_STRING_VIEW path, SYS_STRING_VIEW* parent, + SYS_STRING_VIEW* name) { + if (!parent || !name || !SysSvIsValid(path)) return NTLSTATUS_INVALID_PARAMETER; + *parent = SYS_SV_NULL; + *name = SYS_SV_NULL; + + while (path.Length && FatIsPathSep(path.Data[path.Length - 1])) + path.Length--; + if (path.Length == 0) return NTLSTATUS_INVALID_PARAMETER; + + if (SysSvContainsByte(path, '\0')) return NTLSTATUS_INVALID_PARAMETER; + + size_t lastSep = (size_t)-1; + for (size_t i = 0; i < path.Length; i++) { + if (FatIsPathSep(path.Data[i])) lastSep = i; + } + + if (lastSep == (size_t)-1) { + *parent = SYS_SV_NULL; + *name = path; + } else { + *parent = SysSvFromParts(path.Data, lastSep); + *name = SysSvFromParts(path.Data + lastSep + 1, path.Length - lastSep - 1); + } + + if (!FatNameViewIsValid(*name)) return NTLSTATUS_INVALID_PARAMETER; + return NTLSTATUS_SUCCESS; +} + +// Find a free DIRENT slot. +static int FindFreeDirent(FAT_VOL* v, uint32_t dirCluster, + uint64_t* outLba, uint32_t* outOffset) { + uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!clusterBuf) return 0; + + uint32_t cursor; uint64_t lba; uint32_t sectors; + if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { + MmFreePool(clusterBuf); + return 0; + } + + do { + if (!KSUCCESS(FatReadSectors(v, lba, sectors, clusterBuf))) { + MmFreePool(clusterBuf); return 0; + } + uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); + DIRENT* dir = (DIRENT*)clusterBuf; + for (uint32_t i = 0; i < entries; i++) { + uint8_t first = (uint8_t)dir[i].Name[0]; + if (first == 0xE5 || first == 0x00) { + uint64_t eb = (uint64_t)i * sizeof(DIRENT); + *outLba = lba + eb / v->BytesPerSector; + *outOffset = (uint32_t)(eb % v->BytesPerSector); + MmFreePool(clusterBuf); return 1; + } + } + } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); + + if (FatIsFixedRoot(v, dirCluster)) { + MmFreePool(clusterBuf); + return 0; + } + + uint32_t newCluster = FatAppendCluster(v, cursor); + if (newCluster) { + *outLba = FatClusterToLba(v, newCluster); *outOffset = 0; + MmFreePool(clusterBuf); return 1; + } + MmFreePool(clusterBuf); + return 0; +} + +// Find a contiguous run of free directory entries. +// VFAT LFN creation requires N long-name entries to immediately precede the +// final short entry, so the run must live inside one readable/writeable segment. +static int FindFreeDirentRun(FAT_VOL* v, uint32_t dirCluster, uint32_t needed, + uint64_t* outLba, uint32_t* outFirstIndex, + uint32_t* outSectors) { + if (!needed) return 0; + + uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!clusterBuf) return 0; + + uint32_t cursor; uint64_t lba; uint32_t sectors; + if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { + MmFreePool(clusterBuf); + return 0; + } + + do { + if (!KSUCCESS(FatReadSectors(v, lba, sectors, clusterBuf))) { + MmFreePool(clusterBuf); + return 0; + } + + uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); + DIRENT* dir = (DIRENT*)clusterBuf; + uint32_t runStart = 0, runLen = 0; + for (uint32_t i = 0; i < entries; i++) { + uint8_t first = (uint8_t)dir[i].Name[0]; + int freeEntry = (first == 0xE5 || first == 0x00); + if (freeEntry) { + if (runLen == 0) runStart = i; + runLen++; + if (runLen >= needed) { + *outLba = lba; + *outFirstIndex = runStart; + *outSectors = sectors; + MmFreePool(clusterBuf); + return 1; + } + } else { + runLen = 0; + } + } + } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); + + if (FatIsFixedRoot(v, dirCluster)) { + MmFreePool(clusterBuf); + return 0; + } + + uint32_t newCluster = FatAppendCluster(v, cursor); + if (newCluster) { + *outLba = FatClusterToLba(v, newCluster); + *outFirstIndex = 0; + *outSectors = v->SectorsPerCluster; + MmFreePool(clusterBuf); + return 1; + } + + MmFreePool(clusterBuf); + return 0; +} + +// Finds the parent directory and final component for a create or delete path. +KSTATUS FatResolveParentForMutation(FAT_VOL* v, SYS_STRING_VIEW path, + uint32_t* parentClusterOut, + SYS_STRING_VIEW* filenameOut) { + if (!v || !parentClusterOut || !filenameOut) + return NTLSTATUS_INVALID_PARAMETER; + + SYS_STRING_VIEW parentPath; + SYS_STRING_VIEW filenameView; + KSTATUS s = SplitPathV(path, &parentPath, &filenameView); + if (!KSUCCESS(s)) return s; + + uint32_t parentCluster = 0; + uint32_t parentSize = 0; + uint8_t parentAttr = 0; + s = FatLookupPathV(v, parentPath, &parentCluster, &parentSize, + &parentAttr, NULL, NULL); + if (!KSUCCESS(s)) return s; + if (!(parentAttr & ATTR_DIRECTORY)) return NTLSTATUS_NOT_FOUND; + + *parentClusterOut = parentCluster; + *filenameOut = filenameView; + return NTLSTATUS_SUCCESS; +} + +// Creates a FAT directory entry run for a new file or directory. +KSTATUS FatCreateDirent(FAT_VOL* v, SYS_STRING_VIEW path, + uint8_t attr, uint32_t firstCluster, + uint32_t fileSize, + uint64_t* direntLbaOut, + uint32_t* direntOffsetOut) { + if (!v || !SysSvIsValid(path) || path.Length == 0) + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t parentCluster = 0; + SYS_STRING_VIEW filenameView; + KSTATUS s = FatResolveParentForMutation(v, path, &parentCluster, &filenameView); + if (!KSUCCESS(s)) return s; + + { + uint32_t ec, es; + uint8_t ea; + uint64_t existingLba = 0; + uint32_t existingOffset = 0; + if (SearchDirV(v, parentCluster, filenameView, &ec, &es, &ea, + &existingLba, &existingOffset)) { + return NTLSTATUS_OBJECT_NAME_COLLISION; + } + } + + int useLfn = FatNameNeedsLfnForCreateV(filenameView); + char shortName[8], shortExt[3]; + if (useLfn) { + s = DeleteLegacyBrokenDotAliasV(v, parentCluster, filenameView); + if (!KSUCCESS(s)) return s; + if (!MakeUniqueShortAliasV(v, parentCluster, filenameView, shortName, shortExt)) + return NTLSTATUS_OBJECT_NAME_COLLISION; + } else { + Make83NameV(filenameView, shortName, shortExt); + } + + size_t nameLen = filenameView.Length; + if (nameLen == 0 || nameLen >= FAT_MAX_LFN) return NTLSTATUS_INVALID_PARAMETER; + uint32_t lfnCount = useLfn ? (uint32_t)((nameLen + 12u) / 13u) : 0; + uint32_t neededEntries = lfnCount + 1u; + + uint64_t clusterLba; uint32_t firstIndex; uint32_t entrySectors; + if (!FindFreeDirentRun(v, parentCluster, neededEntries, &clusterLba, &firstIndex, &entrySectors)) + return NTLSTATUS_NO_MEMORY; + + uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!clusterBuf) return NTLSTATUS_NO_MEMORY; + + s = FatReadSectors(v, clusterLba, entrySectors, clusterBuf); + if (!KSUCCESS(s)) { MmFreePool(clusterBuf); return s; } + + DIRENT* dir = (DIRENT*)clusterBuf; + uint32_t shortIndex = firstIndex + lfnCount; + + if (useLfn) { + uint8_t checksum = LfnChecksum(shortName, shortExt); + for (uint32_t slot = 0; slot < lfnCount; slot++) { + uint32_t ordinal = lfnCount - slot; + BuildLfnEntry((LFN_ENTRY*)&dir[firstIndex + slot], filenameView.Data, + nameLen, ordinal, lfnCount, checksum); + } + } + + DIRENT* d = &dir[shortIndex]; + memset(d, 0, sizeof(DIRENT)); + memcpy(d->Name, shortName, 8); + memcpy(d->Ext, shortExt, 3); + d->Attr = attr; + d->Reserved = FatShortNameCaseFlagsV(filenameView); + FatSetDirentFirstCluster(v, d, firstCluster); + d->FileSize = fileSize; + { + FAT_ENCODED_TIME now; + FatQueryEncodedTime(&now); + d->CrtTimeTenth = now.CreateTimeTenth; + d->CrtTime = now.Time; + d->CrtDate = now.Date; + d->LastAccDate = now.Date; + d->WrtTime = now.Time; + d->WrtDate = now.Date; + } + + s = FatWriteSectors(v, clusterLba, entrySectors, clusterBuf); + MmFreePool(clusterBuf); + if (!KSUCCESS(s)) return s; + + uint64_t shortEntryByte = (uint64_t)shortIndex * sizeof(DIRENT); + uint64_t direntLba = clusterLba + shortEntryByte / v->BytesPerSector; + uint32_t direntOffset = (uint32_t)(shortEntryByte % v->BytesPerSector); + if (direntLbaOut) *direntLbaOut = direntLba; + if (direntOffsetOut) *direntOffsetOut = direntOffset; + + FatPathCacheInvalidate(v); + return NTLSTATUS_SUCCESS; +} + +// Initializes one dot entry in a new directory cluster. +static void FatInitDotEntry(FAT_VOL* v, DIRENT* d, const char* name, + uint32_t firstCluster, + const FAT_ENCODED_TIME* now) { + memset(d, 0, sizeof(*d)); + memset(d->Name, ' ', sizeof(d->Name)); + memset(d->Ext, ' ', sizeof(d->Ext)); + d->Name[0] = name[0]; + if (name[1]) d->Name[1] = name[1]; + d->Attr = ATTR_DIRECTORY; + FatSetDirentFirstCluster(v, d, firstCluster); + d->CrtTimeTenth = now->CreateTimeTenth; + d->CrtTime = now->Time; + d->CrtDate = now->Date; + d->LastAccDate = now->Date; + d->WrtTime = now->Time; + d->WrtDate = now->Date; +} + +// Writes the initial contents of a newly allocated FAT directory cluster. +KSTATUS FatInitializeDirectoryCluster(FAT_VOL* v, uint32_t dirCluster, + uint32_t parentCluster) { + if (!v || dirCluster < 2) + return NTLSTATUS_INVALID_PARAMETER; + + uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!clusterBuf) return NTLSTATUS_NO_MEMORY; + memset(clusterBuf, 0, v->BytesPerCluster); + + FAT_ENCODED_TIME now; + FatQueryEncodedTime(&now); + DIRENT* dir = (DIRENT*)clusterBuf; + FatInitDotEntry(v, &dir[0], ".", dirCluster, &now); + FatInitDotEntry(v, &dir[1], "..", FatIsFixedRoot(v, parentCluster) ? 0u : parentCluster, &now); + + KSTATUS s = FatWriteSectors(v, FatClusterToLba(v, dirCluster), + v->SectorsPerCluster, clusterBuf); + MmFreePool(clusterBuf); + return s; +} + +// Tests whether a FAT directory entry is one of the synthetic dot entries. +static int FatIsDotEntry(const DIRENT* d) { + if (!d || !(d->Attr & ATTR_DIRECTORY)) return 0; + if (d->Name[0] != '.') return 0; + if (d->Name[1] == ' ') + return 1; + return d->Name[1] == '.' && d->Name[2] == ' '; +} + +// Tests whether a directory contains any real child entries. +KSTATUS FatDirectoryIsEmpty(FAT_VOL* v, uint32_t dirCluster, + int* emptyOut) { + if (!v || !emptyOut) + return NTLSTATUS_INVALID_PARAMETER; + + uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!clusterBuf) return NTLSTATUS_NO_MEMORY; + + uint32_t cursor; uint64_t lba; uint32_t sectors; + if (!FatDirectoryFirstSegment(v, dirCluster, &cursor, &lba, §ors)) { + MmFreePool(clusterBuf); + return NTLSTATUS_INVALID_PARAMETER; + } + + do { + KSTATUS s = FatReadSectors(v, lba, sectors, clusterBuf); + if (!KSUCCESS(s)) { MmFreePool(clusterBuf); return s; } + + uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); + DIRENT* dir = (DIRENT*)clusterBuf; + for (uint32_t i = 0; i < entries; i++) { + uint8_t first = (uint8_t)dir[i].Name[0]; + if (first == 0x00) { MmFreePool(clusterBuf); *emptyOut = 1; return NTLSTATUS_SUCCESS; } + if (first == 0xE5 || dir[i].Attr == ATTR_LFN) continue; + if ((dir[i].Attr & ATTR_VOLUME_ID) && !(dir[i].Attr & ATTR_DIRECTORY)) continue; + if (FatIsDotEntry(&dir[i])) continue; + + MmFreePool(clusterBuf); + *emptyOut = 0; + return NTLSTATUS_SUCCESS; + } + } while (FatDirectoryNextSegment(v, dirCluster, &cursor, &lba, §ors)); + + MmFreePool(clusterBuf); + *emptyOut = 1; + return NTLSTATUS_SUCCESS; +} + +// Deletes the directory-entry run for one child name from a parent directory. +KSTATUS FatDeleteDirentRunV(FAT_VOL* v, uint32_t parentCluster, + SYS_STRING_VIEW name) { + if (!v || !FatNameViewIsValid(name)) + return NTLSTATUS_INVALID_PARAMETER; + + uint8_t* clusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!clusterBuf) return NTLSTATUS_NO_MEMORY; + + char lfn[FAT_MAX_LFN]; + int hasLfn = 0; + uint32_t lfnStart = 0; + memset(lfn, 0, sizeof(lfn)); + + char wantName83[8], wantExt83[3]; + int allowShortMatch = FatNameIsStructurallyShort83V(name); + Make83NameV(name, wantName83, wantExt83); + + uint32_t cursor; uint64_t lba; uint32_t sectors; + if (!FatDirectoryFirstSegment(v, parentCluster, &cursor, &lba, §ors)) { + MmFreePool(clusterBuf); + return NTLSTATUS_NOT_FOUND; + } + + do { + KSTATUS s = FatReadSectors(v, lba, sectors, clusterBuf); + if (!KSUCCESS(s)) { MmFreePool(clusterBuf); return s; } + + uint32_t entries = (sectors * v->BytesPerSector) / sizeof(DIRENT); + DIRENT* dir = (DIRENT*)clusterBuf; + hasLfn = 0; + memset(lfn, 0, sizeof(lfn)); + for (uint32_t i = 0; i < entries; i++) { + uint8_t first = (uint8_t)dir[i].Name[0]; + if (first == 0x00) { MmFreePool(clusterBuf); return NTLSTATUS_NOT_FOUND; } + if (first == 0xE5) { hasLfn = 0; memset(lfn, 0, sizeof(lfn)); continue; } + if (dir[i].Attr == ATTR_LFN) { + if (!hasLfn) lfnStart = i; + FatLfnAppend(lfn, (LFN_ENTRY*)&dir[i]); + hasLfn = 1; + continue; + } + + int match = 0; + if (hasLfn && lfn[0]) match = LfnMatchV(lfn, name); + if (!match && allowShortMatch) + match = MatchName83V(dir[i].Name, dir[i].Ext, name); + if (!match && allowShortMatch) + match = MatchRaw83(dir[i].Name, dir[i].Ext, wantName83, wantExt83); + + if (match) { + uint32_t start = hasLfn ? lfnStart : i; + for (uint32_t j = start; j <= i; j++) + dir[j].Name[0] = (char)0xE5; + s = FatWriteSectors(v, lba, sectors, clusterBuf); + MmFreePool(clusterBuf); + if (KSUCCESS(s)) FatPathCacheInvalidate(v); + return s; + } + + hasLfn = 0; + memset(lfn, 0, sizeof(lfn)); + } + } while (FatDirectoryNextSegment(v, parentCluster, &cursor, &lba, §ors)); + + MmFreePool(clusterBuf); + return NTLSTATUS_NOT_FOUND; +} diff --git a/code/drivers/fs/fat/fat_driver.c b/code/drivers/fs/fat/fat_driver.c new file mode 100644 index 0000000..d448831 --- /dev/null +++ b/code/drivers/fs/fat/fat_driver.c @@ -0,0 +1,298 @@ +// FAT mount discovery, BPB validation, and driver lifetime. + +#include "fat_internal.h" + +// FAT BPB validation. +typedef struct FAT_BPB_LAYOUT { + FAT_VARIANT Variant; + uint32_t TotalSectors; + uint32_t FatSize; + uint32_t RootDirSectors; + uint32_t DataSectors; + uint32_t TotalClusters; +} FAT_BPB_LAYOUT; + +static int FatComputeBpbLayout(const BPB* bpb, FAT_BPB_LAYOUT* out) { + if (!bpb || !out) return 0; + memset(out, 0, sizeof(*out)); + + if (bpb->BytesPerSector != 512) return 0; + if (bpb->SectorsPerCluster == 0) return 0; + if (bpb->NumFats == 0) return 0; + if (bpb->ReservedSectors == 0) return 0; + + uint32_t totalSectors = bpb->TotalSectors16 ? bpb->TotalSectors16 : bpb->TotalSectors32; + uint32_t fatSize = bpb->FatSize16 ? bpb->FatSize16 : bpb->FatSize32; + if (!totalSectors || !fatSize) return 0; + + uint32_t rootDirSectors = ((uint32_t)bpb->RootEntryCount * sizeof(DIRENT) + + (uint32_t)bpb->BytesPerSector - 1u) / + (uint32_t)bpb->BytesPerSector; + uint32_t metadataSectors = (uint32_t)bpb->ReservedSectors + + (uint32_t)bpb->NumFats * fatSize + + rootDirSectors; + if (totalSectors <= metadataSectors) return 0; + + uint32_t dataSectors = totalSectors - metadataSectors; + uint32_t totalClusters = dataSectors / bpb->SectorsPerCluster; + FAT_VARIANT variant; + if (totalClusters < 4085u) + variant = FatVariant12; + else if (totalClusters < 65525u) + variant = FatVariant16; + else + variant = FatVariant32; + + if (variant == FatVariant16) { + if (bpb->FatSize16 == 0 || bpb->RootEntryCount == 0) return 0; + } else if (variant == FatVariant32) { + if (bpb->FatSize16 != 0 || bpb->RootEntryCount != 0) return 0; + if (bpb->FatSize32 == 0 || bpb->RootCluster < 2) return 0; + } + + out->Variant = variant; + out->TotalSectors = totalSectors; + out->FatSize = fatSize; + out->RootDirSectors = rootDirSectors; + out->DataSectors = dataSectors; + out->TotalClusters = totalClusters; + return 1; +} + + +// Driver entry and dynamic partition-backed mount instances. +static LIST_ENTRY gFatMounts; +static uint32_t gMountedCount; +static MUTEX gFatMountMutex; +static SPINLOCK gFatMountMutexInitLock = SPINLOCK_INIT; +static int gFatMountMutexReady; + +// Marks only this FAT mount's cache state unusable when its backing volume dies. +void FatVolumeDead(IO_VOLUME_PROVIDER* provider, IO_VOLUME_REMOVAL_REASON reason) { + (void)reason; + if (!provider || !provider->Context) + return; + FatCacheInvalidateVolume((FAT_VOL*)provider->Context); +} + +// Releases one dynamically allocated FAT mount after every volume reference has drained. +void FatDestroyVolumeProvider(IO_VOLUME_PROVIDER* provider) { + if (!provider || !provider->Context) + return; + FAT_VOL* volume = (FAT_VOL*)provider->Context; + FatCacheInvalidateVolume(volume); + memset(volume, 0, sizeof(*volume)); + MmFreePool(volume); +} + +// Initializes the mount-list mutex without allocating kernel objects. +static void FatMountEnsureMutex(void) { + if (gFatMountMutexReady) + return; + + uintptr_t flags = KeAcquireSpinlock(&gFatMountMutexInitLock); + if (!gFatMountMutexReady) { + memset(&gFatMountMutex, 0, sizeof(gFatMountMutex)); + ObInitializeObjectHeader(&gFatMountMutex.Header, ObjectTypeMutex); + gFatMountMutex.Lock = (SPINLOCK)SPINLOCK_INIT; + gFatMountMutex.Owner = (struct THREAD*)0; + LIST_INIT(&gFatMountMutex.WaitList); + LIST_INIT(&gFatMountMutex.OwnerLink); + gFatMountMutex.DonationClass = KeSchedulingNormal; + gFatMountMutex.DonationPriority = PriorityLow; + gFatMountMutex.HasDonation = 0; + gFatMountMutex.Abandoned = 0; + gFatMountMutexReady = 1; + } + KeReleaseSpinlock(&gFatMountMutexInitLock, flags); +} + +// Returns one active FAT mount for an exact partition extent while the mount mutex is held. +static FAT_VOL* FatFindMountLocked(const IO_PARTITION_INFO* partition) { + if (!partition) + return (FAT_VOL*)0; + for (LIST_ENTRY* entry = gFatMounts.Fwd; entry != &gFatMounts; entry = entry->Fwd) { + FAT_VOL* volume = CONTAINING_RECORD(entry, FAT_VOL, MountLink); + if (volume->DriveIndex == partition->DiskIndex && + volume->PartitionLba == partition->StartLba) { + return volume; + } + } + return (FAT_VOL*)0; +} + +// Probes one I/O-core partition extent and mounts it when its VBR describes supported FAT16/FAT32. +static bool FatTryMountPartition(const IO_PARTITION_INFO* partition) { + if (!partition || partition->SectorCount == 0 || !IoDiskDevicePresent(partition->DiskIndex)) + return false; + + FatMountEnsureMutex(); + if (!KE_MUTEX_ACQUIRE_SUCCEEDED(KeAcquireMutex(&gFatMountMutex, KE_WAIT_INFINITE))) + return false; + + if (FatFindMountLocked(partition)) { + KeReleaseMutex(&gFatMountMutex); + return false; + } + + FAT_VOL* v = (FAT_VOL*)MmAllocatePool(sizeof(FAT_VOL)); + if (!v) { + KeReleaseMutex(&gFatMountMutex); + return false; + } + memset(v, 0, sizeof(*v)); + LIST_INIT(&v->MountLink); + v->DriveIndex = partition->DiskIndex; + v->PartitionLba = partition->StartLba; + v->PartitionSize = partition->SectorCount; + + uint8_t sector[512]; + if (!KSUCCESS(FatReadSectors(v, v->PartitionLba, 1, sector))) { + MmFreePool(v); + KeReleaseMutex(&gFatMountMutex); + return false; + } + + BPB* bpb = (BPB*)sector; + FAT_BPB_LAYOUT layout; + if (!FatComputeBpbLayout(bpb, &layout) || + (layout.Variant != FatVariant16 && layout.Variant != FatVariant32)) { + MmFreePool(v); + KeReleaseMutex(&gFatMountMutex); + return false; + } + + v->Variant = layout.Variant; + v->BytesPerSector = bpb->BytesPerSector; + v->SectorsPerCluster = bpb->SectorsPerCluster; + v->BytesPerCluster = bpb->BytesPerSector * bpb->SectorsPerCluster; + v->ReservedSectors = bpb->ReservedSectors; + v->NumFats = bpb->NumFats; + v->FatSize = layout.FatSize; + v->RootEntryCount = bpb->RootEntryCount; + v->RootDirSectors = layout.RootDirSectors; + v->RootDirStart = v->PartitionLba + bpb->ReservedSectors + + (uint64_t)bpb->NumFats * layout.FatSize; + v->RootCluster = v->Variant == FatVariant16 ? FAT_FIXED_ROOT_CLUSTER : bpb->RootCluster; + v->DataStart = v->RootDirStart + layout.RootDirSectors; + v->NextFreeHint = 2; + v->TotalClusters = layout.TotalClusters; + if (v->PartitionSize > layout.TotalSectors) + v->PartitionSize = layout.TotalSectors; + + FatDebugPrint("FAT: disk=%u lba=%llu %s layout fat=%u root-sectors=%u data=%u clusters=%u\n", + v->DriveIndex, (unsigned long long)v->PartitionLba, + FatVariantName(v->Variant), v->FatSize, + v->RootDirSectors, layout.DataSectors, v->TotalClusters); + + IO_VOLUME_PROVIDER provider; + FatInitProvider(&provider, v); + IO_VOLUME_SOURCE volParams; + volParams.Type = IoVolumeSourcePartition; + volParams.DiskIndex = v->DriveIndex; + memset(volParams.Reserved, 0, sizeof(volParams.Reserved)); + volParams.StartLba = v->PartitionLba; + volParams.SectorCount = v->PartitionSize; + + IO_VOLUME* volume = (IO_VOLUME*)0; + KSTATUS status = IoCreateVolume(&provider, &volParams, &volume); + if (!KSUCCESS(status)) { + if (volume) { + (void)IoRemoveVolume(volume, IoVolumeUnmounted); + IoReleaseVolume(volume); + } else { + MmFreePool(v); + } + DbgPrint("FAT: disk=%u lba=%llu mount failed status=0x%x\n", + partition->DiskIndex, (unsigned long long)partition->StartLba, status); + KeReleaseMutex(&gFatMountMutex); + return false; + } + + // Retain the IoCreateVolume caller reference while this provider remains on the active mount list. + v->Volume = volume; + ListInsertTail(&gFatMounts, &v->MountLink); + gMountedCount++; + uint32_t instanceId = IoGetVolumeInstanceId(volume); + uint32_t mountedCount = gMountedCount; + + DbgPrint("FAT: disk=%u lba=%llu %s mounted volume=%u; total=%u\n", + v->DriveIndex, (unsigned long long)v->PartitionLba, + FatVariantName(v->Variant), instanceId, mountedCount); + KeReleaseMutex(&gFatMountMutex); + return true; +} + +// Unmounts one exact FAT provider instance when its backing partition disappears. +static void FatUnmountPartition(const IO_PARTITION_INFO* partition, IO_PARTITION_CHANGE change) { + if (!partition) + return; + + FatMountEnsureMutex(); + if (!KE_MUTEX_ACQUIRE_SUCCEEDED(KeAcquireMutex(&gFatMountMutex, KE_WAIT_INFINITE))) + return; + + FAT_VOL* v = FatFindMountLocked(partition); + if (!v) { + KeReleaseMutex(&gFatMountMutex); + return; + } + + IO_VOLUME* volume = v->Volume; + uint32_t instanceId = IoGetVolumeInstanceId(volume); + ListRemove(&v->MountLink); + v->Volume = (IO_VOLUME*)0; + if (gMountedCount) + gMountedCount--; + uint32_t mountedCount = gMountedCount; + KeReleaseMutex(&gFatMountMutex); + + (void)IoRemoveVolume(volume, IoVolumeBackingRemoved); + IoReleaseVolume(volume); // Drop the active mount-list reference. + DbgPrint("FAT: disk=%u lba=%llu %s; unmounted volume=%u; total=%u\n", + partition->DiskIndex, (unsigned long long)partition->StartLba, + change == IoPartitionSurpriseRemoved ? "surprise removed" : "removed", + instanceId, mountedCount); +} + +// Receives immutable partition extents from the I/O core; FAT never parses partition tables. +static void FatPartitionChange(const IO_PARTITION_INFO* partition, + IO_PARTITION_CHANGE change, + void* context) { + (void)context; + switch (change) { + case IoPartitionArrived: + (void)FatTryMountPartition(partition); + break; + case IoPartitionSurpriseRemoved: + case IoPartitionRemoved: + FatUnmountPartition(partition, change); + break; + default: + break; + } +} + +// Initializes FAT and subscribes to the authoritative kernel partition stream. +KSTATUS DriverEntry(const NTL_DDK* ddk, NTL_DRIVER_INIT* init) { + DbgPrint("FAT: Init\n"); + if (!ddk) + return NTLSTATUS_INVALID_PARAMETER; + (void)init; + + FatCacheInvalidate(); + LIST_INIT(&gFatMounts); + gMountedCount = 0; + FatMountEnsureMutex(); + + KSTATUS status = IoRegisterPartitionChangeCallback(FatPartitionChange, NULL); + if (!KSUCCESS(status)) { + DbgPrint("FAT: partition callback registration failed status=0x%x\n", status); + return status; + } + + if (!gMountedCount) + DbgPrint("FAT: no mountable FAT partitions found; driver resident for partition changes.\n"); + DbgPrint("FAT: Init complete; mounted %u volume(s).\n", gMountedCount); + return NTLSTATUS_SUCCESS; +} diff --git a/code/drivers/fs/fat/fat_file.c b/code/drivers/fs/fat/fat_file.c new file mode 100644 index 0000000..1b37d78 --- /dev/null +++ b/code/drivers/fs/fat/fat_file.c @@ -0,0 +1,755 @@ +// FAT synchronous file and directory operations. + +#include "fat_internal.h" + +// Open / Close. +KSTATUS FatOpen(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path, void** fileCtx) { + if (!fs || !fileCtx || !SysSvIsValid(path) || path.Length == 0) + return NTLSTATUS_INVALID_PARAMETER; + + FAT_VOL* v = (FAT_VOL*)fs->Context; + uint32_t cluster, size; uint64_t direntLba; uint32_t direntOffset; + KSTATUS s = FatLookupPathV(v, path, &cluster, &size, NULL, &direntLba, &direntOffset); + if (!KSUCCESS(s)) return s; + + FAT_FILE* f = (FAT_FILE*)MmAllocatePool(sizeof(FAT_FILE)); + if (!f) return NTLSTATUS_NO_MEMORY; + f->StartCluster = cluster; f->FileSize = size; f->Vol = v; + f->DirentLba = direntLba; f->DirentOffset = direntOffset; + FatFileInitChainCache(f); + *fileCtx = f; + return NTLSTATUS_SUCCESS; +} + +// Flushes FAT metadata and the backing device write cache. +KSTATUS FatFlush(IO_VOLUME_PROVIDER* fs, void* fileCtx) { + (void)fs; + FAT_FILE* f = (FAT_FILE*)fileCtx; + if (!f || !f->Vol) + return NTLSTATUS_INVALID_PARAMETER; + + FatFlushCache(f->Vol); + return IoFlushDisk(f->Vol->DriveIndex); +} + +// Releases the requested object. +void FatClose(IO_VOLUME_PROVIDER* fs, void* fileCtx) { + (void)fs; + FAT_FILE* f = (FAT_FILE*)fileCtx; + FatFlushCache(f->Vol); + MmFreePool(fileCtx); +} + +// Read (fix #4: contiguous-run coalescing) +// Cluster-chain lookup is cached per open file, and data spans are allowed to +// grow beyond the AHCI bounce size because the device layer now splits large +// sector requests for the concrete controller. + +KSTATUS FatRead(IO_VOLUME_PROVIDER* fs, void* fileCtx, + uint64_t offset, void* buf, + size_t size, size_t* bytesRead) { + (void)fs; + if (!bytesRead) return NTLSTATUS_INVALID_PARAMETER; + *bytesRead = 0; + if (!fileCtx || !buf) return NTLSTATUS_INVALID_PARAMETER; + + FAT_FILE* f = (FAT_FILE*)fileCtx; + FAT_VOL* v = f->Vol; + if (offset >= f->FileSize || size == 0) return NTLSTATUS_SUCCESS; + if (size > (size_t)(f->FileSize - offset)) size = (size_t)(f->FileSize - offset); + size_t wanted = size; + + uint32_t initialOffsetInCluster = (uint32_t)(offset % v->BytesPerCluster); + uint64_t initialBytesCovered = (uint64_t)initialOffsetInCluster + (uint64_t)size; + uint32_t maxUsefulSectors = (uint32_t)((initialBytesCovered + 511u) / 512u); + if (maxUsefulSectors < v->SectorsPerCluster) maxUsefulSectors = v->SectorsPerCluster; + if (maxUsefulSectors > FAT_READ_RUN_MAX_SECTORS) + maxUsefulSectors = FAT_READ_RUN_MAX_SECTORS; + + uint8_t* runBuf = (uint8_t*)MmAllocatePool((size_t)maxUsefulSectors * 512u); + if (!runBuf) return NTLSTATUS_NO_MEMORY; + + size_t totalRead = 0; + uint8_t* out = (uint8_t*)buf; + KSTATUS readStatus = NTLSTATUS_SUCCESS; + uint32_t logicalCluster = (uint32_t)(offset / v->BytesPerCluster); + uint32_t offsetInCluster = (uint32_t)(offset % v->BytesPerCluster); + uint32_t cluster = FatResolveFileCluster(f, logicalCluster); + + while (size > 0 && FatIsDataCluster(v, cluster)) { + uint64_t bytesCovered = (uint64_t)offsetInCluster + (uint64_t)size; + uint32_t usefulSectors = (uint32_t)((bytesCovered + 511u) / 512u); + if (usefulSectors < v->SectorsPerCluster) usefulSectors = v->SectorsPerCluster; + if (usefulSectors > FAT_READ_RUN_MAX_SECTORS) + usefulSectors = FAT_READ_RUN_MAX_SECTORS; + if (usefulSectors > maxUsefulSectors) usefulSectors = maxUsefulSectors; + + uint32_t runSectors = 0; + uint32_t next = 0; + if (!FatBuildReadRun(f, logicalCluster, cluster, usefulSectors, + &runSectors, &next)) { + readStatus = NTLSTATUS_DEVICE_ERROR; + break; + } + + KSTATUS rs = FatReadSectors(v, FatClusterToLba(v, cluster), runSectors, runBuf); + if (!KSUCCESS(rs)) { + readStatus = rs; + break; + } + + uint32_t runBytes = runSectors * 512u; + size_t avail = runBytes - offsetInCluster; + size_t copy = avail < size ? avail : size; + uint32_t runClusters = runSectors / v->SectorsPerCluster; + uint32_t merges = runClusters ? runClusters - 1u : 0; + FatStatsAddReadRun(v, runSectors, merges); + + memcpy(out, runBuf + offsetInCluster, copy); + out += copy; + totalRead += copy; + size -= copy; + offsetInCluster = 0; + logicalCluster += runClusters; + cluster = next; + } + + if (totalRead != wanted) { + // The directory entry says the file has more data than the cluster chain + // or disk read actually supplied. Clear the caller's remainder so stale + // memory cannot be interpreted as file contents. + memset(out, 0, wanted - totalRead); + } + + MmFreePool(runBuf); + *bytesRead = totalRead; + if (totalRead != wanted) + return KSUCCESS(readStatus) ? NTLSTATUS_DEVICE_ERROR : readStatus; + return NTLSTATUS_SUCCESS; +} + +// Write (fix #4: contiguous-run coalescing) +// Same strategy as read. Whole-cluster-aligned runs skip the read-modify- +// write; partial head/tail clusters still require it. + +KSTATUS FatWrite(IO_VOLUME_PROVIDER* fs, void* fileCtx, + uint64_t offset, const void* buf, + size_t size, size_t* bytesWritten) { + (void)fs; + if (!size) { *bytesWritten = 0; return NTLSTATUS_SUCCESS; } + + FAT_FILE* f = (FAT_FILE*)fileCtx; + FAT_VOL* v = f->Vol; + const uint8_t* src = (const uint8_t*)buf; + FatFileInvalidateChainCache(f); + + // Ensure at least one cluster exists. + if (f->StartCluster < 2) { + uint32_t first = FatAllocCluster(v); + if (!first) { *bytesWritten = 0; return NTLSTATUS_NO_MEMORY; } + FatFlushCache(v); + f->StartCluster = first; + FatDirentUpdateFirstCluster(v, f->DirentLba, f->DirentOffset, first); + } + + // Walk to the starting cluster, allocating as needed. + uint32_t clusterIndex = (uint32_t)(offset / v->BytesPerCluster); + uint32_t offsetInCluster = (uint32_t)(offset % v->BytesPerCluster); + uint32_t cluster = f->StartCluster; + + for (uint32_t i = 0; i < clusterIndex; i++) { + uint32_t next = FatEntry(v, cluster); + if (!FatIsDataCluster(v, next)) { + next = FatAppendCluster(v, cluster); + if (!next) { *bytesWritten = 0; return NTLSTATUS_NO_MEMORY; } + } + cluster = next; + } + + uint8_t* runBuf = (uint8_t*)MmAllocatePool(FAT_WRITE_RUN_MAX_SECTORS * 512u); + if (!runBuf) { *bytesWritten = 0; return NTLSTATUS_NO_MEMORY; } + + size_t written = 0, remaining = size; + + while (remaining > 0 && cluster >= 2) { + // Build a contiguous run (only when cluster-aligned to avoid a + // partial read-modify-write spanning multiple clusters). + uint32_t runStart = cluster; + uint32_t runSectors = v->SectorsPerCluster; + uint32_t next = FatEntry(v, cluster); + + if (offsetInCluster == 0) { + while (next == cluster + 1 && FatIsDataCluster(v, next) + && runSectors + v->SectorsPerCluster <= FAT_WRITE_RUN_MAX_SECTORS) { + cluster = next; + next = FatEntry(v, cluster); + runSectors += v->SectorsPerCluster; + } + } + + uint32_t runBytes = runSectors * 512u; + size_t avail = runBytes - offsetInCluster; + size_t copy = avail < remaining ? avail : remaining; + + // Read existing data only for partial-cluster writes. + bool partial = (offsetInCluster > 0) || (copy < (size_t)runBytes); + if (partial) { + if (!KSUCCESS(FatReadSectors(v, FatClusterToLba(v, runStart), runSectors, runBuf))) + break; + } + + memcpy(runBuf + offsetInCluster, src, copy); + + if (!KSUCCESS(FatWriteSectors(v, FatClusterToLba(v, runStart), runSectors, runBuf))) + break; + + src += copy; + written += copy; + remaining -= copy; + offsetInCluster = 0; + + if (remaining > 0) { + // Advance: if end-of-chain, append a new cluster. + cluster = next; + if (!FatIsDataCluster(v, cluster)) { + // Find the actual last cluster in the run we just wrote. + uint32_t last = runStart; + for (uint32_t tmp = FatEntry(v, last); + FatIsDataCluster(v, tmp); + tmp = FatEntry(v, last)) + last = tmp; + cluster = FatAppendCluster(v, last); + if (!cluster) break; + } + } + } + + MmFreePool(runBuf); + + uint64_t newEnd = offset + written; + if (written > 0) { + FAT_DIRENT_UPDATE u; + memset(&u, 0, sizeof(u)); + FatQueryEncodedTime(&u.Timestamp); + u.Flags = FAT_DIRENT_UPDATE_WRITE_TIME; + if (newEnd > f->FileSize) { + f->FileSize = (uint32_t)newEnd; + u.Flags |= FAT_DIRENT_UPDATE_SIZE; + u.Size = f->FileSize; + } + KSTATUS ms = FatDirentUpdateMetadata(v, f->DirentLba, f->DirentOffset, &u); + if (KSUCCESS(ms)) + FatPathCacheInvalidate(v); + } + + *bytesWritten = written; + return (written == size) ? NTLSTATUS_SUCCESS : NTLSTATUS_DEVICE_ERROR; +} + +// Create. +// Creates the requested file and fails if the name already exists. +KSTATUS FatCreate(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path, void** fileCtx) { + if (!fs || !fileCtx || !SysSvIsValid(path) || path.Length == 0) + return NTLSTATUS_INVALID_PARAMETER; + + FAT_VOL* v = (FAT_VOL*)fs->Context; + uint64_t direntLba = 0; + uint32_t direntOffset = 0; + KSTATUS s = FatCreateDirent(v, path, ATTR_ARCHIVE, 0, 0, + &direntLba, &direntOffset); + if (!KSUCCESS(s)) return s; + + FAT_FILE* f = (FAT_FILE*)MmAllocatePool(sizeof(FAT_FILE)); + if (!f) return NTLSTATUS_NO_MEMORY; + f->StartCluster = 0; + f->FileSize = 0; + f->Vol = v; + f->DirentLba = direntLba; + f->DirentOffset = direntOffset; + FatFileInitChainCache(f); + *fileCtx = f; + return NTLSTATUS_SUCCESS; +} + +// Creates a directory and initializes its dot entries. +KSTATUS FatCreateDir(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path) { + if (!fs || !SysSvIsValid(path) || path.Length == 0) + return NTLSTATUS_INVALID_PARAMETER; + + FAT_VOL* v = (FAT_VOL*)fs->Context; + uint32_t parentCluster = 0; + SYS_STRING_VIEW filenameView; + KSTATUS s = FatResolveParentForMutation(v, path, &parentCluster, &filenameView); + if (!KSUCCESS(s)) return s; + (void)filenameView; + + uint32_t dirCluster = FatAllocCluster(v); + if (!dirCluster) return NTLSTATUS_NO_MEMORY; + + s = FatInitializeDirectoryCluster(v, dirCluster, parentCluster); + if (!KSUCCESS(s)) { + FatFreeClusterChain(v, dirCluster); + FatFlushCache(v); + return s; + } + + FatFlushCache(v); + + s = FatCreateDirent(v, path, ATTR_DIRECTORY, dirCluster, 0, NULL, NULL); + if (!KSUCCESS(s)) { + FatFreeClusterChain(v, dirCluster); + FatFlushCache(v); + return s; + } + + FatFlushCache(v); + return NTLSTATUS_SUCCESS; +} + +// Removes an empty directory. +KSTATUS FatRemoveDir(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path) { + if (!fs || !SysSvIsValid(path) || path.Length == 0) + return NTLSTATUS_INVALID_PARAMETER; + + FAT_VOL* v = (FAT_VOL*)fs->Context; + uint32_t cluster = 0; + uint32_t size = 0; + uint8_t attr = 0; + uint64_t direntLba = 0; + uint32_t direntOffset = 0; + KSTATUS s = FatLookupPathV(v, path, &cluster, &size, &attr, + &direntLba, &direntOffset); + if (!KSUCCESS(s)) return s; + if (!direntLba) return NTLSTATUS_ACCESS_DENIED; + if (!(attr & ATTR_DIRECTORY)) return NTLSTATUS_ACCESS_DENIED; + + int empty = 0; + s = FatDirectoryIsEmpty(v, cluster, &empty); + if (!KSUCCESS(s)) return s; + if (!empty) return NTLSTATUS_ACCESS_DENIED; + + uint32_t parentCluster = 0; + SYS_STRING_VIEW filenameView; + s = FatResolveParentForMutation(v, path, &parentCluster, &filenameView); + if (!KSUCCESS(s)) return s; + + s = FatDeleteDirentRunV(v, parentCluster, filenameView); + if (!KSUCCESS(s)) return s; + + if (cluster >= 2) FatFreeClusterChain(v, cluster); + FatFlushCache(v); + FatPathCacheInvalidate(v); + return NTLSTATUS_SUCCESS; +} + +// Truncate. +KSTATUS FatTruncate(IO_VOLUME_PROVIDER* fs, void* fileCtx, uint64_t newSize, uint32_t fileFlags) { + (void)fs; + FAT_FILE* f = (FAT_FILE*)fileCtx; + FAT_VOL* v = f->Vol; + FatFileInvalidateChainCache(f); + int zeroNewClusters = (fileFlags & IO_FILE_FLAG_SKIP_ZERO_EXTEND) ? 0 : 1; + if ((uint32_t)newSize == f->FileSize) { + FAT_DIRENT_UPDATE u; + memset(&u, 0, sizeof(u)); + FatQueryEncodedTime(&u.Timestamp); + u.Flags = FAT_DIRENT_UPDATE_WRITE_TIME; + KSTATUS s = FatDirentUpdateMetadata(v, f->DirentLba, f->DirentOffset, &u); + if (KSUCCESS(s)) FatPathCacheInvalidate(v); + return s; + } + + if (newSize == 0) { + if (f->StartCluster >= 2) { + FatFreeClusterChain(v, f->StartCluster); + f->StartCluster = 0; + FatDirentUpdateFirstCluster(v, f->DirentLba, f->DirentOffset, 0); + } + f->FileSize = 0; + KSTATUS s = FatDirentUpdateSize(v, f->DirentLba, f->DirentOffset, 0); + FatFlushCache(v); + if (KSUCCESS(s)) FatPathCacheInvalidate(v); + return s; + } + + uint32_t newClusters = (uint32_t) + ((newSize + v->BytesPerCluster - 1) / v->BytesPerCluster); + + if (newSize < f->FileSize) { + if (f->StartCluster < 2) goto update_size; + uint32_t cluster = f->StartCluster, lastGood = cluster; + for (uint32_t i = 1; i < newClusters && FatIsDataCluster(v, cluster); i++) { + lastGood = cluster; cluster = FatEntry(v, cluster); + } + uint32_t tail = FatEntry(v, lastGood); + FatWriteEntry(v, lastGood, FatEocMark(v)); + FatFreeClusterChain(v, tail); + } else { + uint32_t curClusters = f->FileSize + ? (f->FileSize + v->BytesPerCluster - 1) / v->BytesPerCluster : 0; + uint32_t lastCluster = 0; + if (f->StartCluster < 2) { + uint32_t first = FatAllocClusterEx(v, zeroNewClusters); + if (!first) return NTLSTATUS_NO_MEMORY; + f->StartCluster = first; + FatDirentUpdateFirstCluster(v, f->DirentLba, f->DirentOffset, first); + lastCluster = first; curClusters = 1; + } else { + lastCluster = FatGetChainEnd(v, f->StartCluster, NULL); + } + for (uint32_t i = curClusters; i < newClusters; i++) { + uint32_t next = FatAppendClusterEx(v, lastCluster, zeroNewClusters); + if (!next) return NTLSTATUS_NO_MEMORY; + lastCluster = next; + } + } + +update_size: + f->FileSize = (uint32_t)newSize; + { KSTATUS s = FatDirentUpdateSize(v, f->DirentLba, f->DirentOffset, (uint32_t)newSize); + FatFlushCache(v); + if (KSUCCESS(s)) FatPathCacheInvalidate(v); + return s; } +} + +// Delete. +KSTATUS FatDelete(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path) { + if (!fs || !SysSvIsValid(path) || path.Length == 0) + return NTLSTATUS_INVALID_PARAMETER; + + FAT_VOL* v = (FAT_VOL*)fs->Context; + uint32_t cluster, size; uint64_t direntLba; uint32_t direntOffset; + KSTATUS s = FatLookupPathV(v, path, &cluster, &size, NULL, &direntLba, &direntOffset); + if (!KSUCCESS(s)) return s; + if (!direntLba) return NTLSTATUS_ACCESS_DENIED; + + uint32_t parentCluster = 0; + SYS_STRING_VIEW filenameView; + s = FatResolveParentForMutation(v, path, &parentCluster, &filenameView); + if (!KSUCCESS(s)) return s; + + // Delete the complete entry run so long-name metadata cannot survive the + // short entry and keep writable vvfat mirrors visibly present. + s = FatDeleteDirentRunV(v, parentCluster, filenameView); + if (!KSUCCESS(s)) return s; + + if (cluster >= 2) FatFreeClusterChain(v, cluster); + FatFlushCache(v); + FatPathCacheInvalidate(v); + return NTLSTATUS_SUCCESS; +} + +// Returns size. +uint64_t FatGetSize(IO_VOLUME_PROVIDER* fs, void* fileCtx) { + (void)fs; return ((FAT_FILE*)fileCtx)->FileSize; +} + +// Converts FAT attributes to the public file type. +static uint32_t FatFileTypeFromAttr(uint8_t attr) { + return (attr & ATTR_DIRECTORY) ? SYS_FILE_TYPE_DIRECTORY : SYS_FILE_TYPE_REGULAR; +} + +// Fills public metadata from one FAT directory entry. +static void FatFillStatFromDirent(FAT_VOL* v, const DIRENT* d, + uint64_t direntLba, uint32_t direntOffset, + SYS_FILE_STAT* out) { + memset(out, 0, sizeof(*out)); + out->Size = sizeof(*out); + out->Type = FatFileTypeFromAttr(d->Attr); + out->Attributes = d->Attr; + out->LinkCount = 1; + out->FileSize = d->FileSize; + + uint32_t firstCluster = FatDirentFirstCluster(v, d); + out->FileId = direntLba ^ + ((uint64_t)direntOffset << 32) ^ + ((uint64_t)firstCluster << 1); + + FatDecodeDateTime(d->CrtDate, d->CrtTime, d->CrtTimeTenth, &out->CreationTime); + FatDecodeAccessDate(d->LastAccDate, &out->LastAccessTime); + FatDecodeDateTime(d->WrtDate, d->WrtTime, 0, &out->LastWriteTime); + out->ChangeTime = out->LastWriteTime; +} + +// Fills public metadata for the filesystem root. +static void FatFillRootStat(FAT_VOL* v, SYS_FILE_STAT* out) { + memset(out, 0, sizeof(*out)); + out->Size = sizeof(*out); + out->Type = SYS_FILE_TYPE_DIRECTORY; + out->Attributes = SYS_FILE_ATTR_DIRECTORY; + out->LinkCount = 1; + out->FileSize = 0; + out->FileId = ((uint64_t)v->RootCluster << 1) ^ 0x524f4f54ULL; +} + +// Queries metadata for an open FAT file. +KSTATUS FatQueryInfo(IO_VOLUME_PROVIDER* fs, void* fileCtx, SYS_FILE_STAT* out) { + (void)fs; + if (!fileCtx || !out) return NTLSTATUS_INVALID_PARAMETER; + + FAT_FILE* f = (FAT_FILE*)fileCtx; + FAT_VOL* v = f->Vol; + if (!v) return NTLSTATUS_INVALID_PARAMETER; + + if (f->DirentLba == 0) { + if (f->StartCluster == v->RootCluster && f->FileSize == 0) { + FatFillRootStat(v, out); + } else { + memset(out, 0, sizeof(*out)); + out->Size = sizeof(*out); + out->Type = SYS_FILE_TYPE_REGULAR; + out->Attributes = SYS_FILE_ATTR_ARCHIVE; + out->LinkCount = 1; + out->FileSize = f->FileSize; + out->FileId = ((uint64_t)f->StartCluster << 1); + } + return NTLSTATUS_SUCCESS; + } + + uint8_t* sec = (uint8_t*)MmAllocatePool(v->BytesPerSector); + if (!sec) return NTLSTATUS_NO_MEMORY; + + KSTATUS s = FatReadSectors(v, f->DirentLba, 1, sec); + if (!KSUCCESS(s)) { MmFreePool(sec); return s; } + + DIRENT* d = (DIRENT*)(sec + f->DirentOffset); + if ((uint8_t)d->Name[0] == 0x00 || (uint8_t)d->Name[0] == 0xE5) { + MmFreePool(sec); + return NTLSTATUS_NOT_FOUND; + } + + FatFillStatFromDirent(v, d, f->DirentLba, f->DirentOffset, out); + f->StartCluster = FatDirentFirstCluster(v, d); + f->FileSize = d->FileSize; + MmFreePool(sec); + return NTLSTATUS_SUCCESS; +} + +// Queries metadata for a FAT path. +KSTATUS FatStat(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW path, SYS_FILE_STAT* out) { + if (!fs || !out || !SysSvIsValid(path)) return NTLSTATUS_INVALID_PARAMETER; + + FAT_VOL* v = (FAT_VOL*)fs->Context; + if (!v) return NTLSTATUS_INVALID_PARAMETER; + + uint32_t cluster = 0; + uint32_t size = 0; + uint8_t attr = ATTR_DIRECTORY; + uint64_t direntLba = 0; + uint32_t direntOffset = 0; + KSTATUS s = FatLookupPathV(v, path, &cluster, &size, &attr, &direntLba, &direntOffset); + if (!KSUCCESS(s)) return s; + (void)cluster; + + if (direntLba == 0) { + FatFillRootStat(v, out); + return NTLSTATUS_SUCCESS; + } + + uint8_t* sec = (uint8_t*)MmAllocatePool(v->BytesPerSector); + if (!sec) return NTLSTATUS_NO_MEMORY; + + s = FatReadSectors(v, direntLba, 1, sec); + if (!KSUCCESS(s)) { MmFreePool(sec); return s; } + + DIRENT* d = (DIRENT*)(sec + direntOffset); + if ((uint8_t)d->Name[0] == 0x00 || (uint8_t)d->Name[0] == 0xE5) { + MmFreePool(sec); + return NTLSTATUS_NOT_FOUND; + } + + FatFillStatFromDirent(v, d, direntLba, direntOffset, out); + out->Type = FatFileTypeFromAttr(attr); + out->FileSize = size; + MmFreePool(sec); + return NTLSTATUS_SUCCESS; +} + +// Mixes FAT directory metadata into a per-file content generation. +static uint64_t FatDirentGeneration(FAT_VOL* v, const DIRENT* d) { + if (!d) + return 0; + + uint32_t firstCluster = FatDirentFirstCluster(v, d); + uint64_t generation = ((uint64_t)d->WrtDate << 48) | + ((uint64_t)d->WrtTime << 32) | + ((uint64_t)d->LastAccDate << 16) | + (uint64_t)d->Attr; + generation ^= ((uint64_t)d->CrtDate << 40); + generation ^= ((uint64_t)d->CrtTime << 24); + generation ^= ((uint64_t)d->CrtTimeTenth << 16); + generation ^= ((uint64_t)firstCluster << 1); + generation ^= ((uint64_t)d->FileSize << 17); + return generation ? generation : 1; +} + +// Returns a stable FAT file identity for cross-open image-section caches. +KSTATUS FatQueryIdentity(IO_VOLUME_PROVIDER* fs, void* fileCtx, IO_FILE_IDENTITY* out) { + (void)fs; + if (!fileCtx || !out) + return NTLSTATUS_INVALID_PARAMETER; + + FAT_FILE* f = (FAT_FILE*)fileCtx; + FAT_VOL* v = f->Vol; + if (!v) + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t startCluster = f->StartCluster; + uint32_t fileSize = f->FileSize; + uint64_t generation = ((uint64_t)startCluster << 1) ^ + ((uint64_t)fileSize << 17); + + if (f->DirentLba != 0) { + uint8_t* sec = (uint8_t*)MmAllocatePool(v->BytesPerSector); + if (!sec) + return NTLSTATUS_NO_MEMORY; + + KSTATUS s = FatReadSectors(v, f->DirentLba, 1, sec); + if (!KSUCCESS(s)) { + MmFreePool(sec); + return s; + } + + DIRENT* d = (DIRENT*)(sec + f->DirentOffset); + if ((uint8_t)d->Name[0] == 0x00 || (uint8_t)d->Name[0] == 0xE5) { + MmFreePool(sec); + return NTLSTATUS_NOT_FOUND; + } + + startCluster = FatDirentFirstCluster(v, d); + fileSize = d->FileSize; + generation = FatDirentGeneration(v, d); + MmFreePool(sec); + } + + out->FileId = f->DirentLba ^ + ((uint64_t)f->DirentOffset << 32) ^ + ((uint64_t)startCluster << 1); + out->Generation = generation ? generation : 1; + out->Size = fileSize; + return NTLSTATUS_SUCCESS; +} + +// Directory enumeration (fix #2: cluster buffer cached in context) +// Previously: allocate + FatReadSectors + return 1 entry + free, every call. +// Now: buffer lives in FAT_DIR_CTX; FatReadSectors fires only on cluster +// boundaries. + +typedef struct { + FAT_VOL* Vol; + uint32_t DirCluster; + uint32_t Cursor; + uint32_t LoadedCursor; + uint32_t LoadedSectors; + uint64_t LoadedLba; + uint32_t EntryIndex; + char LfnBuf[FAT_MAX_LFN]; + int HasLfn; + uint8_t* ClusterBuf; // allocated once in OpenDir, freed in CloseDir +} FAT_DIR_CTX; + +// Opens dir. +KSTATUS FatOpenDir(IO_VOLUME_PROVIDER* fs, SYS_STRING_VIEW dirPath, void** dirCtx) { + if (!fs || !dirCtx || !SysSvIsValid(dirPath)) + return NTLSTATUS_INVALID_PARAMETER; + + FAT_VOL* v = (FAT_VOL*)fs->Context; + uint32_t cluster; + if (dirPath.Length == 0) { + cluster = v->RootCluster; + } else { + uint32_t size; + KSTATUS s = FatTraverseV(v, dirPath, &cluster, &size); + if (!KSUCCESS(s)) return s; + } + + uint32_t cursor; uint64_t lba; uint32_t sectors; + if (!FatDirectoryFirstSegment(v, cluster, &cursor, &lba, §ors)) + return NTLSTATUS_NOT_FOUND; + + FAT_DIR_CTX* ctx = (FAT_DIR_CTX*)MmAllocatePool(sizeof(FAT_DIR_CTX)); + if (!ctx) return NTLSTATUS_NO_MEMORY; + + ctx->ClusterBuf = (uint8_t*)MmAllocatePool(v->BytesPerCluster); + if (!ctx->ClusterBuf) { MmFreePool(ctx); return NTLSTATUS_NO_MEMORY; } + + ctx->Vol = v; + ctx->DirCluster = cluster; + ctx->Cursor = cursor; + ctx->LoadedCursor = (uint32_t)-1; + ctx->LoadedSectors = sectors; + ctx->LoadedLba = lba; + ctx->EntryIndex = 0; + ctx->HasLfn = 0; + memset(ctx->LfnBuf, 0, sizeof(ctx->LfnBuf)); + *dirCtx = ctx; + return NTLSTATUS_SUCCESS; +} + +// Returns dir. +KSTATUS FatReadDir(IO_VOLUME_PROVIDER* fs, void* dirCtx, KDIR_ENTRY* out) { + (void)fs; + FAT_DIR_CTX* ctx = (FAT_DIR_CTX*)dirCtx; + FAT_VOL* v = ctx->Vol; + + while (true) { + if (ctx->Cursor != ctx->LoadedCursor) { + if (!KSUCCESS(FatReadSectors(v, ctx->LoadedLba, ctx->LoadedSectors, ctx->ClusterBuf))) + return NTLSTATUS_NO_MORE_FILES; + ctx->LoadedCursor = ctx->Cursor; + } + + uint32_t epc = (ctx->LoadedSectors * v->BytesPerSector) / sizeof(DIRENT); + DIRENT* dir = (DIRENT*)ctx->ClusterBuf; + while (ctx->EntryIndex < epc) { + DIRENT* e = &dir[ctx->EntryIndex++]; + + if (e->Name[0] == 0x00) return NTLSTATUS_NO_MORE_FILES; + if ((uint8_t)e->Name[0] == 0xE5) { ctx->HasLfn = 0; memset(ctx->LfnBuf, 0, sizeof(ctx->LfnBuf)); continue; } + if (e->Attr == ATTR_LFN) { FatLfnAppend(ctx->LfnBuf, (LFN_ENTRY*)e); ctx->HasLfn = 1; continue; } + if (e->Attr & 0x08 && !(e->Attr & ATTR_DIRECTORY)) { ctx->HasLfn = 0; memset(ctx->LfnBuf, 0, sizeof(ctx->LfnBuf)); continue; } + + memset(out, 0, sizeof(*out)); + + if (ctx->HasLfn && ctx->LfnBuf[0]) { + size_t len = strlen(ctx->LfnBuf); + if (len >= KDIR_NAME_MAX) len = KDIR_NAME_MAX - 1; + memcpy(out->Name, ctx->LfnBuf, len); out->Name[len] = '\0'; + } else { + char name83[13]; int n = 0; + for (int i = 0; i < 8 && e->Name[i] != ' '; i++) + name83[n++] = (char)(e->Name[i] >= 'A' && e->Name[i] <= 'Z' ? e->Name[i]+32 : e->Name[i]); + if (e->Ext[0] != ' ') { + name83[n++] = '.'; + for (int i = 0; i < 3 && e->Ext[i] != ' '; i++) + name83[n++] = (char)(e->Ext[i] >= 'A' && e->Ext[i] <= 'Z' ? e->Ext[i]+32 : e->Ext[i]); + } + name83[n] = '\0'; + size_t len = (size_t)n; if (len >= KDIR_NAME_MAX) len = KDIR_NAME_MAX - 1; + memcpy(out->Name, name83, len); out->Name[len] = '\0'; + } + + out->Attr = e->Attr; + out->FileSize = e->FileSize; + FatDecodeDateTime(e->CrtDate, e->CrtTime, e->CrtTimeTenth, &out->CreateTime); + FatDecodeAccessDate(e->LastAccDate, &out->LastAccessTime); + FatDecodeDateTime(e->WrtDate, e->WrtTime, 0, &out->LastWriteTime); + ctx->HasLfn = 0; memset(ctx->LfnBuf, 0, sizeof(ctx->LfnBuf)); + return NTLSTATUS_SUCCESS; + } + + if (!FatDirectoryNextSegment(v, ctx->DirCluster, &ctx->Cursor, + &ctx->LoadedLba, &ctx->LoadedSectors)) + return NTLSTATUS_NO_MORE_FILES; + ctx->LoadedCursor = (uint32_t)-1; + ctx->EntryIndex = 0; + } +} + +// Releases dir. +void FatCloseDir(IO_VOLUME_PROVIDER* fs, void* dirCtx) { + (void)fs; + FAT_DIR_CTX* ctx = (FAT_DIR_CTX*)dirCtx; + MmFreePool(ctx->ClusterBuf); + MmFreePool(ctx); +} diff --git a/code/drivers/fs/fat/fat_internal.h b/code/drivers/fs/fat/fat_internal.h new file mode 100644 index 0000000..0c5f439 --- /dev/null +++ b/code/drivers/fs/fat/fat_internal.h @@ -0,0 +1,152 @@ +// FAT private implementation model shared by the driver's translation units. + +#pragma once + +#include "fat.h" +#include "fat_disk.h" + +#define FAT_PATH_CACHE_SLOTS 64u +#define FAT_PATH_CACHE_MAX 512u +#define FAT_CHAIN_CACHE_SLOTS 16u +#define FAT_WRITE_RUN_MAX_SECTORS 128u +#define FAT_READ_RUN_MAX_SECTORS 512u +#define FAT_MAX_LFN 256 + +#ifndef FAT_DEBUG +#define FAT_DEBUG 0 +#endif + +#define FatDebugPrint(...) do { if (FAT_DEBUG) DbgPrint(__VA_ARGS__); } while (0) + +typedef struct FAT_PATH_CACHE_ENTRY { + bool Valid; + uint64_t Generation; + uint32_t Hash; + size_t PathLength; + char Path[FAT_PATH_CACHE_MAX]; + uint32_t Cluster; + uint32_t Size; + uint8_t Attr; + uint64_t DirentLba; + uint32_t DirentOffset; +} FAT_PATH_CACHE_ENTRY; + +typedef struct FAT_CHAIN_CACHE_ENTRY { + bool Valid; + uint32_t LogicalStart; + uint32_t PhysicalStart; + uint32_t Length; + uint32_t NextCluster; +} FAT_CHAIN_CACHE_ENTRY; + +struct FAT_VOL_S { + FAT_VARIANT Variant; + uint8_t DriveIndex; + uint64_t PartitionLba; + uint64_t PartitionSize; + uint32_t BytesPerSector; + uint32_t SectorsPerCluster; + uint32_t BytesPerCluster; + uint32_t ReservedSectors; + uint32_t NumFats; + uint32_t FatSize; + uint32_t RootCluster; + uint32_t RootEntryCount; + uint32_t RootDirSectors; + uint64_t RootDirStart; + uint64_t DataStart; + uint32_t TotalClusters; + uint32_t NextFreeHint; + SPINLOCK PathCacheLock; + uint64_t PathCacheGeneration; + IO_FILESYSTEM_STATS Stats; + FAT_PATH_CACHE_ENTRY PathCache[FAT_PATH_CACHE_SLOTS]; + IO_VOLUME* Volume; + LIST_ENTRY MountLink; +}; + +typedef struct FAT_FILE { + uint32_t StartCluster; + uint32_t FileSize; + FAT_VOL* Vol; + uint64_t DirentLba; + uint32_t DirentOffset; + SPINLOCK ChainCacheLock; + uint32_t ChainCacheClock; + FAT_CHAIN_CACHE_ENTRY ChainCache[FAT_CHAIN_CACHE_SLOTS]; +} FAT_FILE; + +typedef struct FAT_ENCODED_TIME { + uint16_t Date; + uint16_t Time; + uint8_t CreateTimeTenth; +} FAT_ENCODED_TIME; + +typedef struct FAT_DIRENT_UPDATE { + uint32_t Flags; + uint32_t Size; + uint32_t FirstCluster; + FAT_ENCODED_TIME Timestamp; +} FAT_DIRENT_UPDATE; + +#define FAT_DIRENT_UPDATE_SIZE 0x00000001u +#define FAT_DIRENT_UPDATE_FIRST_CLUSTER 0x00000002u +#define FAT_DIRENT_UPDATE_CREATE_TIME 0x00000004u +#define FAT_DIRENT_UPDATE_WRITE_TIME 0x00000008u +#define FAT_DIRENT_UPDATE_ACCESS_DATE 0x00000010u + +KSTATUS FatReadSectors(FAT_VOL* volume, uint64_t lba, uint32_t count, void* buffer); +KSTATUS FatWriteSectors(FAT_VOL* volume, uint64_t lba, uint32_t count, const void* buffer); +const char* FatVariantName(FAT_VARIANT variant); +uint32_t FatEocMark(FAT_VOL* volume); +int FatIsDataCluster(FAT_VOL* volume, uint32_t cluster); +uint32_t FatDirentFirstCluster(FAT_VOL* volume, const DIRENT* dirent); +void FatSetDirentFirstCluster(FAT_VOL* volume, DIRENT* dirent, uint32_t cluster); +int FatIsFixedRoot(FAT_VOL* volume, uint32_t dirCluster); +int FatDirectoryFirstSegment(FAT_VOL* volume, uint32_t dirCluster, + uint32_t* cursorOut, uint64_t* lbaOut, uint32_t* sectorsOut); +int FatDirectoryNextSegment(FAT_VOL* volume, uint32_t dirCluster, + uint32_t* cursorInOut, uint64_t* lbaOut, uint32_t* sectorsOut); +uint64_t FatClusterToLba(FAT_VOL* volume, uint32_t cluster); +uint32_t FatEntry(FAT_VOL* volume, uint32_t cluster); +KSTATUS FatWriteEntry(FAT_VOL* volume, uint32_t cluster, uint32_t value); +void FatFlushCache(FAT_VOL* volume); +uint32_t FatAllocClusterEx(FAT_VOL* volume, int zeroCluster); +uint32_t FatAllocCluster(FAT_VOL* volume); +uint32_t FatAppendClusterEx(FAT_VOL* volume, uint32_t lastCluster, int zeroCluster); +uint32_t FatAppendCluster(FAT_VOL* volume, uint32_t lastCluster); +uint32_t FatGetChainEnd(FAT_VOL* volume, uint32_t start, uint32_t* countOut); +void FatFreeClusterChain(FAT_VOL* volume, uint32_t start); +void FatQueryEncodedTime(FAT_ENCODED_TIME* out); +void FatDecodeDateTime(uint16_t date, uint16_t time, uint8_t createTenth, SYS_SYSTEM_TIME* out); +void FatDecodeAccessDate(uint16_t date, SYS_SYSTEM_TIME* out); +KSTATUS FatDirentUpdateMetadata(FAT_VOL* volume, uint64_t lba, uint32_t offset, + const FAT_DIRENT_UPDATE* update); +KSTATUS FatDirentUpdateSize(FAT_VOL* volume, uint64_t lba, uint32_t offset, uint32_t size); +KSTATUS FatDirentUpdateFirstCluster(FAT_VOL* volume, uint64_t lba, uint32_t offset, uint32_t cluster); +void FatLfnAppend(char* lfn, const LFN_ENTRY* entry); +void FatPathCacheInvalidate(FAT_VOL* volume); +void FatStatsAddReadRun(FAT_VOL* volume, uint32_t sectors, uint32_t merges); +void FatFileInitChainCache(FAT_FILE* file); +void FatFileInvalidateChainCache(FAT_FILE* file); +uint32_t FatResolveFileCluster(FAT_FILE* file, uint32_t logicalIndex); +bool FatBuildReadRun(FAT_FILE* file, uint32_t logicalIndex, + uint32_t startCluster, uint32_t maxSectors, + uint32_t* runSectorsOut, uint32_t* nextClusterOut); +KSTATUS FatLookupPathV(FAT_VOL* volume, SYS_STRING_VIEW path, + uint32_t* clusterOut, uint32_t* sizeOut, uint8_t* attrOut, + uint64_t* direntLbaOut, uint32_t* direntOffsetOut); +KSTATUS FatTraverseV(FAT_VOL* volume, SYS_STRING_VIEW path, + uint32_t* clusterOut, uint32_t* sizeOut); +KSTATUS FatResolveParentForMutation(FAT_VOL* volume, SYS_STRING_VIEW path, + uint32_t* parentClusterOut, SYS_STRING_VIEW* nameOut); +KSTATUS FatCreateDirent(FAT_VOL* volume, SYS_STRING_VIEW path, + uint8_t attr, uint32_t firstCluster, uint32_t size, + uint64_t* direntLbaOut, uint32_t* direntOffsetOut); +KSTATUS FatInitializeDirectoryCluster(FAT_VOL* volume, uint32_t dirCluster, + uint32_t parentCluster); +KSTATUS FatDirectoryIsEmpty(FAT_VOL* volume, uint32_t dirCluster, int* emptyOut); +KSTATUS FatDeleteDirentRunV(FAT_VOL* volume, uint32_t parentCluster, + SYS_STRING_VIEW name); +void FatCacheInvalidate(void); +void FatCacheInvalidateVolume(FAT_VOL* volume); diff --git a/code/drivers/fs/ntfs/ntfs3g/include/assert.h b/code/drivers/fs/ntfs/ntfs3g/include/assert.h new file mode 100644 index 0000000..e34b5f1 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/assert.h @@ -0,0 +1,7 @@ +#pragma once +#include +#ifndef NDEBUG +#define assert(expr) ((expr) ? (void)0 : abort()) +#else +#define assert(expr) ((void)0) +#endif diff --git a/code/drivers/fs/ntfs/ntfs3g/include/config.h b/code/drivers/fs/ntfs/ntfs3g/include/config.h new file mode 100644 index 0000000..57bc11d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/config.h @@ -0,0 +1,78 @@ +#pragma once + +/* Minimal autoconf surface for the NTLite libntfs-3g port. */ +#define PACKAGE "ntfs-3g" +#define PACKAGE_NAME "ntfs-3g" +#define PACKAGE_VERSION "2026.7.7" +#define VERSION "2026.7.7" + +#define HAVE_STDINT_H 1 +#define HAVE_STDDEF_H 1 +#define HAVE_CTYPE_H 1 +#define HAVE_STDLIB_H 1 +#define HAVE_STDIO_H 1 +#define HAVE_STDARG_H 1 +#define HAVE_ERRNO_H 1 +#define HAVE_STRING_H 1 +#define HAVE_LIMITS_H 1 +#define HAVE_TIME_H 1 +#define HAVE_LOCALE_H 1 +#define HAVE_UNISTD_H 1 +#define HAVE_SYS_TYPES_H 1 +#define HAVE_SYS_STAT_H 1 +#define HAVE_FCNTL_H 1 + +#define HAVE_CONFIG_H 1 +#define NO_NTFS_DEVICE_DEFAULT_IO_OPS 1 +#define DISABLE_PLUGINS 1 +#define CACHE_INODE_SIZE 0 +#define CACHE_NIDATA_SIZE 0 +#define CACHE_LOOKUP_SIZE 0 +#define CACHE_SECURID_SIZE 16 +#define CACHE_LEGACY_SIZE 8 + +/* Keep the first port deliberately small. */ +#undef POSIXACLS +#undef XATTR_MAPPINGS +#undef ENABLE_NFCONV +#undef HAVE_LIBGCRYPT +#undef HAVE_OPENSSL +#undef HAVE_ICONV +#undef HAVE_LANGINFO_H + +#ifndef _MSC_VER +#error This config is for the NTLite MSVC build only. +#endif + + +/* MSVC syntax compatibility. Keep this scoped to NTFS-3G through config.h; + * do not force-include it into unrelated translation units. Packed on-disk + * headers are guarded with #pragma pack in the vendored MSVC port. */ +#ifdef _MSC_VER +#ifndef __attribute__ +#define __attribute__(x) +#endif +#ifndef __inline__ +#define __inline__ __inline +#endif +#ifndef __restrict__ +#define __restrict__ __restrict +#endif +#ifndef __func__ +#define __func__ __FUNCTION__ +#endif +#endif + +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif + +/* MSVC provides the standard headers; fill only POSIX gaps it does not. */ +#ifndef O_ACCMODE +#define O_ACCMODE 0x0003 +#endif +#ifndef LITTLE_ENDIAN +#define LITTLE_ENDIAN 1234 +#define BIG_ENDIAN 4321 +#define BYTE_ORDER LITTLE_ENDIAN +#endif diff --git a/code/drivers/fs/ntfs/ntfs3g/include/ctype.h b/code/drivers/fs/ntfs/ntfs3g/include/ctype.h new file mode 100644 index 0000000..e08b784 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/ctype.h @@ -0,0 +1,21 @@ +#pragma once + +static __inline int isascii(int c) { return (unsigned)c <= 0x7fU; } +static __inline int isdigit(int c) { return c >= '0' && c <= '9'; } +static __inline int isxdigit(int c) { + return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); +} +static __inline int islower(int c) { return c >= 'a' && c <= 'z'; } +static __inline int isupper(int c) { return c >= 'A' && c <= 'Z'; } +static __inline int isalpha(int c) { return islower(c) || isupper(c); } +static __inline int isalnum(int c) { return isalpha(c) || isdigit(c); } +static __inline int isspace(int c) { + return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v'; +} +static __inline int isblank(int c) { return c == ' ' || c == '\t'; } +static __inline int iscntrl(int c) { return ((unsigned)c < 0x20U) || c == 0x7f; } +static __inline int isprint(int c) { return (unsigned)(c - 0x20) < 0x5fU; } +static __inline int isgraph(int c) { return (unsigned)(c - 0x21) < 0x5eU; } +static __inline int ispunct(int c) { return isgraph(c) && !isalnum(c); } +static __inline int tolower(int c) { return isupper(c) ? c + ('a' - 'A') : c; } +static __inline int toupper(int c) { return islower(c) ? c - ('a' - 'A') : c; } diff --git a/code/drivers/fs/ntfs/ntfs3g/include/grp.h b/code/drivers/fs/ntfs/ntfs3g/include/grp.h new file mode 100644 index 0000000..89701d5 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/grp.h @@ -0,0 +1,5 @@ +#pragma once +#include +struct group { char* gr_name; gid_t gr_gid; char** gr_mem; }; +struct group* getgrnam(const char* name); +struct group* getgrgid(gid_t gid); diff --git a/code/drivers/fs/ntfs/ntfs3g/include/pwd.h b/code/drivers/fs/ntfs/ntfs3g/include/pwd.h new file mode 100644 index 0000000..0d2cbe5 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/pwd.h @@ -0,0 +1,5 @@ +#pragma once +#include +struct passwd { char* pw_name; uid_t pw_uid; gid_t pw_gid; char* pw_dir; char* pw_shell; }; +struct passwd* getpwnam(const char* name); +struct passwd* getpwuid(uid_t uid); diff --git a/code/drivers/fs/ntfs/ntfs3g/include/stdio.h b/code/drivers/fs/ntfs/ntfs3g/include/stdio.h new file mode 100644 index 0000000..dcef233 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/stdio.h @@ -0,0 +1,24 @@ +#pragma once +#include +#include +#include + +#ifndef SEEK_SET +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 +#endif + +typedef struct NTL_NTFS3G_STDIO_FILE { int unused; } FILE; +extern FILE* stderr; +extern FILE* stdout; +extern FILE* stdin; + +int fprintf(FILE* stream, const char* format, ...); +int vfprintf(FILE* stream, const char* format, va_list args); +int fputs(const char* text, FILE* stream); +int puts(const char* text); + +int fflush(FILE* stream); +int snprintf(char* buffer, size_t size, const char* format, ...); +int vsnprintf(char* buffer, size_t size, const char* format, va_list args); diff --git a/code/drivers/fs/ntfs/ntfs3g/include/stdlib.h b/code/drivers/fs/ntfs/ntfs3g/include/stdlib.h new file mode 100644 index 0000000..7527807 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/stdlib.h @@ -0,0 +1,29 @@ +#pragma once +#include +#include + +void* malloc(size_t size); +void* calloc(size_t count, size_t size); +void* realloc(void* ptr, size_t size); +void free(void* ptr); +char* strdup(const char* text); +char* strndup(const char* text, size_t limit); +void qsort(void* base, size_t count, size_t size, + int (*compare)(const void*, const void*)); +void* bsearch(const void* key, const void* base, size_t count, size_t size, + int (*compare)(const void*, const void*)); +int abs(int value); +long labs(long value); +long long llabs(long long value); +long strtol(const char* text, char** end, int base); +unsigned long strtoul(const char* text, char** end, int base); +long long strtoll(const char* text, char** end, int base); +unsigned long long strtoull(const char* text, char** end, int base); +char* getenv(const char* name); +void abort(void); + +int ffs(int value); +void srandom(unsigned int seed); +long random(void); + +int atoi(const char* text); diff --git a/code/drivers/fs/ntfs/ntfs3g/include/strings.h b/code/drivers/fs/ntfs/ntfs3g/include/strings.h new file mode 100644 index 0000000..f2c6baf --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/strings.h @@ -0,0 +1,4 @@ +#pragma once +#include +int strcasecmp(const char* a, const char* b); +int strncasecmp(const char* a, const char* b, size_t count); diff --git a/code/drivers/fs/ntfs/ntfs3g/include/sys/stat.h b/code/drivers/fs/ntfs/ntfs3g/include/sys/stat.h new file mode 100644 index 0000000..aeec433 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/sys/stat.h @@ -0,0 +1,53 @@ +#pragma once +#include +#include + +#define S_IFMT 0170000 +#define S_IFREG 0100000 +#define S_IFDIR 0040000 +#define S_IFBLK 0060000 +#define S_IFCHR 0020000 +#define S_IFLNK 0120000 +#define S_IFIFO 0010000 +#define S_IFSOCK 0140000 +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) +#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) +#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) + +struct stat { + dev_t st_dev; + uint64_t st_ino; + mode_t st_mode; + uint32_t st_nlink; + uid_t st_uid; + gid_t st_gid; + dev_t st_rdev; + off_t st_size; + blksize_t st_blksize; + blkcnt_t st_blocks; + time_t st_atime; + time_t st_mtime; + time_t st_ctime; +}; + +#define S_IRUSR 0400 +#define S_IWUSR 0200 +#define S_IXUSR 0100 +#define S_IRGRP 0040 +#define S_IWGRP 0020 +#define S_IXGRP 0010 +#define S_IROTH 0004 +#define S_IWOTH 0002 +#define S_IXOTH 0001 +#define S_IRWXU 0700 +#define S_IRWXG 0070 +#define S_IRWXO 0007 +#define S_ISUID 04000 +#define S_ISGID 02000 +#define S_ISVTX 01000 +#define S_IEXEC (S_IXUSR | S_IXGRP | S_IXOTH) + +#define S_IREAD S_IRUSR +#define S_IWRITE S_IWUSR diff --git a/code/drivers/fs/ntfs/ntfs3g/include/sys/types.h b/code/drivers/fs/ntfs/ntfs3g/include/sys/types.h new file mode 100644 index 0000000..36fd51e --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/sys/types.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include + +typedef int64_t off_t; +typedef int64_t ssize_t; +typedef uint32_t mode_t; +typedef uint32_t uid_t; +typedef uint32_t gid_t; +typedef uint64_t dev_t; +typedef int64_t blkcnt_t; +typedef int64_t blksize_t; + +typedef int32_t pid_t; + +#define major(dev) ((unsigned int)(((uint64_t)(dev) >> 32) & 0xffffffffu)) +#define minor(dev) ((unsigned int)((uint64_t)(dev) & 0xffffffffu)) +#define makedev(maj, min) ((((uint64_t)(maj)) << 32) | (uint32_t)(min)) diff --git a/code/drivers/fs/ntfs/ntfs3g/include/syslog.h b/code/drivers/fs/ntfs/ntfs3g/include/syslog.h new file mode 100644 index 0000000..6e3cc56 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/syslog.h @@ -0,0 +1,9 @@ +#pragma once +#include +#define LOG_PID 0x01 +#define LOG_USER (1 << 3) +#define LOG_NOTICE 5 +void openlog(const char* ident, int option, int facility); +void closelog(void); +void syslog(int priority, const char* format, ...); +void vsyslog(int priority, const char* format, va_list args); diff --git a/code/drivers/fs/ntfs/ntfs3g/include/time.h b/code/drivers/fs/ntfs/ntfs3g/include/time.h new file mode 100644 index 0000000..0e3b22f --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/time.h @@ -0,0 +1,17 @@ +#pragma once + +/* Minimal time surface for the freestanding NTFS-3G driver. */ +#ifndef _TIME_T_DEFINED +#define _TIME_T_DEFINED +typedef __int64 time_t; +#endif + +#ifndef __timespec_defined +#define __timespec_defined 1 +struct timespec { + time_t tv_sec; + long tv_nsec; +}; +#endif + +time_t __cdecl time(time_t* out); diff --git a/code/drivers/fs/ntfs/ntfs3g/include/unistd.h b/code/drivers/fs/ntfs/ntfs3g/include/unistd.h new file mode 100644 index 0000000..768a4e2 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/include/unistd.h @@ -0,0 +1,22 @@ +#pragma once +#include +#define STDIN_FILENO 0 +#define STDOUT_FILENO 1 +#define STDERR_FILENO 2 +uid_t getuid(void); +uid_t geteuid(void); +gid_t getgid(void); +gid_t getegid(void); +int fsync(int fd); + +int fork(void); +void _exit(int status); +pid_t setsid(void); +int chdir(const char* path); +int open(const char* path, int flags, ...); +int dup2(int oldfd, int newfd); +int close(int fd); +int daemon(int nochdir, int noclose); + +pid_t getpid(void); +ssize_t read(int fd, void* buffer, size_t count); diff --git a/code/drivers/fs/ntfs/ntfs3g/ntfs3g_device.c b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_device.c new file mode 100644 index 0000000..75a07df --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_device.c @@ -0,0 +1,206 @@ +/* NTLite block-device backend for libntfs-3g. */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include "ntfs3g_device.h" + +#define NTFS3G_SECTOR_SIZE 512u + +static NTL_NTFS3G_DEVICE_CONTEXT* Ntfs3gContext(struct ntfs_device* dev) { + return dev ? (NTL_NTFS3G_DEVICE_CONTEXT*)dev->d_private : NULL; +} + +static int Ntfs3gDeviceOpen(struct ntfs_device* dev, int flags) { + NTL_NTFS3G_DEVICE_CONTEXT* ctx = Ntfs3gContext(dev); + if (!ctx) { errno = EINVAL; return -1; } + if (NDevOpen(dev)) { errno = EBUSY; return -1; } + if ((flags & O_ACCMODE) != O_RDWR) NDevSetReadOnly(dev); + else NDevClearReadOnly(dev); + NDevSetBlock(dev); + NDevSetOpen(dev); + ctx->Position = 0; + return 0; +} + +static int Ntfs3gDeviceClose(struct ntfs_device* dev) { + if (!dev || !NDevOpen(dev)) { errno = EBADF; return -1; } + NDevClearOpen(dev); + return 0; +} + +static s64 Ntfs3gDeviceSeek(struct ntfs_device* dev, s64 offset, int whence) { + NTL_NTFS3G_DEVICE_CONTEXT* ctx = Ntfs3gContext(dev); + if (!ctx) { errno = EINVAL; return -1; } + int64_t base; + if (whence == SEEK_SET) base = 0; + else if (whence == SEEK_CUR) base = ctx->Position; + else if (whence == SEEK_END) { + uint64_t bytes = ctx->PartitionSectors * (uint64_t)NTFS3G_SECTOR_SIZE; + if (bytes > INT64_MAX) { errno = EOVERFLOW; return -1; } + base = (int64_t)bytes; + } else { errno = EINVAL; return -1; } + if ((offset < 0 && base < -offset) || (offset > 0 && base > INT64_MAX - offset)) { + errno = EOVERFLOW; return -1; + } + base += offset; + if (base < 0) { errno = EINVAL; return -1; } + ctx->Position = base; + return base; +} + +static int Ntfs3gBounds(NTL_NTFS3G_DEVICE_CONTEXT* ctx, s64 offset, s64 count) { + if (!ctx || offset < 0 || count < 0) return 0; + uint64_t bytes = ctx->PartitionSectors * (uint64_t)NTFS3G_SECTOR_SIZE; + uint64_t uoff = (uint64_t)offset, ucount = (uint64_t)count; + return uoff <= bytes && ucount <= bytes - uoff; +} + +static s64 Ntfs3gPread(struct ntfs_device* dev, void* buffer, s64 count, s64 offset) { + NTL_NTFS3G_DEVICE_CONTEXT* ctx = Ntfs3gContext(dev); + if (!buffer && count) { errno = EINVAL; return -1; } + if (!Ntfs3gBounds(ctx, offset, count)) { errno = EIO; return -1; } + if (!count) return 0; + + uint8_t* out = (uint8_t*)buffer; + uint64_t pos = (uint64_t)offset; + uint64_t left = (uint64_t)count; + uint8_t* bounce = NULL; + + while (left) { + uint64_t sector = pos / NTFS3G_SECTOR_SIZE; + uint32_t inSector = (uint32_t)(pos % NTFS3G_SECTOR_SIZE); + if (!inSector && left >= NTFS3G_SECTOR_SIZE) { + uint64_t whole = left / NTFS3G_SECTOR_SIZE; + uint32_t batch = whole > 0x10000u ? 0x10000u : (uint32_t)whole; + KSTATUS s = IoReadDiskSectors(ctx->DiskIndex, ctx->PartitionLba + sector, batch, out); + if (!KSUCCESS(s)) { errno = EIO; if (bounce) MmFreePool(bounce); return -1; } + uint64_t bytes = (uint64_t)batch * NTFS3G_SECTOR_SIZE; + pos += bytes; out += bytes; left -= bytes; + continue; + } + if (!bounce) { + bounce = (uint8_t*)MmAllocatePool(NTFS3G_SECTOR_SIZE); + if (!bounce) { errno = ENOMEM; return -1; } + } + KSTATUS s = IoReadDiskSectors(ctx->DiskIndex, ctx->PartitionLba + sector, 1, bounce); + if (!KSUCCESS(s)) { errno = EIO; MmFreePool(bounce); return -1; } + uint32_t chunk = NTFS3G_SECTOR_SIZE - inSector; + if ((uint64_t)chunk > left) chunk = (uint32_t)left; + memcpy(out, bounce + inSector, chunk); + pos += chunk; out += chunk; left -= chunk; + } + if (bounce) MmFreePool(bounce); + return count; +} + +static s64 Ntfs3gPwrite(struct ntfs_device* dev, const void* buffer, s64 count, s64 offset) { + NTL_NTFS3G_DEVICE_CONTEXT* ctx = Ntfs3gContext(dev); + if (!buffer && count) { errno = EINVAL; return -1; } + if (NDevReadOnly(dev)) { errno = EROFS; return -1; } + if (!Ntfs3gBounds(ctx, offset, count)) { errno = EIO; return -1; } + if (!count) return 0; + + const uint8_t* in = (const uint8_t*)buffer; + uint64_t pos = (uint64_t)offset; + uint64_t left = (uint64_t)count; + uint8_t* bounce = NULL; + + while (left) { + uint64_t sector = pos / NTFS3G_SECTOR_SIZE; + uint32_t inSector = (uint32_t)(pos % NTFS3G_SECTOR_SIZE); + if (!inSector && left >= NTFS3G_SECTOR_SIZE) { + uint64_t whole = left / NTFS3G_SECTOR_SIZE; + uint32_t batch = whole > 0x10000u ? 0x10000u : (uint32_t)whole; + KSTATUS s = IoWriteDiskSectors(ctx->DiskIndex, ctx->PartitionLba + sector, batch, in); + if (!KSUCCESS(s)) { errno = EIO; if (bounce) MmFreePool(bounce); return -1; } + uint64_t bytes = (uint64_t)batch * NTFS3G_SECTOR_SIZE; + pos += bytes; in += bytes; left -= bytes; + continue; + } + if (!bounce) { + bounce = (uint8_t*)MmAllocatePool(NTFS3G_SECTOR_SIZE); + if (!bounce) { errno = ENOMEM; return -1; } + } + KSTATUS s = IoReadDiskSectors(ctx->DiskIndex, ctx->PartitionLba + sector, 1, bounce); + if (!KSUCCESS(s)) { errno = EIO; MmFreePool(bounce); return -1; } + uint32_t chunk = NTFS3G_SECTOR_SIZE - inSector; + if ((uint64_t)chunk > left) chunk = (uint32_t)left; + memcpy(bounce + inSector, in, chunk); + s = IoWriteDiskSectors(ctx->DiskIndex, ctx->PartitionLba + sector, 1, bounce); + if (!KSUCCESS(s)) { errno = EIO; MmFreePool(bounce); return -1; } + pos += chunk; in += chunk; left -= chunk; + } + if (bounce) MmFreePool(bounce); + NDevSetDirty(dev); + return count; +} + +static s64 Ntfs3gRead(struct ntfs_device* dev, void* buffer, s64 count) { + NTL_NTFS3G_DEVICE_CONTEXT* ctx = Ntfs3gContext(dev); + s64 done = Ntfs3gPread(dev, buffer, count, ctx ? ctx->Position : 0); + if (done > 0 && ctx) ctx->Position += done; + return done; +} + +static s64 Ntfs3gWrite(struct ntfs_device* dev, const void* buffer, s64 count) { + NTL_NTFS3G_DEVICE_CONTEXT* ctx = Ntfs3gContext(dev); + s64 done = Ntfs3gPwrite(dev, buffer, count, ctx ? ctx->Position : 0); + if (done > 0 && ctx) ctx->Position += done; + return done; +} + +static int Ntfs3gSync(struct ntfs_device* dev) { + NTL_NTFS3G_DEVICE_CONTEXT* ctx = Ntfs3gContext(dev); + if (!ctx) { errno = EINVAL; return -1; } + KSTATUS s = IoFlushDisk(ctx->DiskIndex); + if (!KSUCCESS(s)) { errno = EIO; return -1; } + NDevClearDirty(dev); + return 0; +} + +static int Ntfs3gStat(struct ntfs_device* dev, struct stat* st) { + NTL_NTFS3G_DEVICE_CONTEXT* ctx = Ntfs3gContext(dev); + if (!ctx || !st) { errno = EINVAL; return -1; } + memset(st, 0, sizeof(*st)); + st->st_mode = S_IFBLK; + st->st_size = (off_t)(ctx->PartitionSectors * (uint64_t)NTFS3G_SECTOR_SIZE); + st->st_blksize = NTFS3G_SECTOR_SIZE; + st->st_blocks = (blkcnt_t)ctx->PartitionSectors; + return 0; +} + +static int Ntfs3gIoctl(struct ntfs_device* dev, unsigned long request, void* argp) { + (void)dev; (void)request; (void)argp; + errno = ENOTSUP; + return -1; +} + +static struct ntfs_device_operations gNtfs3gOps = { + Ntfs3gDeviceOpen, + Ntfs3gDeviceClose, + Ntfs3gDeviceSeek, + Ntfs3gRead, + Ntfs3gWrite, + Ntfs3gPread, + Ntfs3gPwrite, + Ntfs3gSync, + Ntfs3gStat, + Ntfs3gIoctl, +}; + +struct ntfs_device* Ntfs3gCreateDevice(NTL_NTFS3G_DEVICE_CONTEXT* context) { + if (!context) { errno = EINVAL; return NULL; } + return ntfs_device_alloc("ntlite-ntfs", 0, &gNtfs3gOps, context); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/ntfs3g_device.h b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_device.h new file mode 100644 index 0000000..bd9850d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_device.h @@ -0,0 +1,12 @@ +#pragma once +#include +#include + +typedef struct NTL_NTFS3G_DEVICE_CONTEXT { + uint8_t DiskIndex; + uint64_t PartitionLba; + uint64_t PartitionSectors; + int64_t Position; +} NTL_NTFS3G_DEVICE_CONTEXT; + +struct ntfs_device* Ntfs3gCreateDevice(NTL_NTFS3G_DEVICE_CONTEXT* context); diff --git a/code/drivers/fs/ntfs/ntfs3g/ntfs3g_driver.c b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_driver.c new file mode 100644 index 0000000..a789ec3 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_driver.c @@ -0,0 +1,1154 @@ +/* NTFS-3G filesystem provider for NTLite. */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ntfs3g_device.h" + +#define NTL_NTFS3G_SECTOR_SIZE 512u + +typedef struct NTL_NTFS3G_VOLUME { + uint8_t DiskIndex; + uint64_t PartitionLba; + uint64_t PartitionSectors; + NTL_NTFS3G_DEVICE_CONTEXT DeviceContext; + struct ntfs_device* Device; + ntfs_volume* Ntfs; + IO_VOLUME* Volume; + LIST_ENTRY MountLink; + bool Dead; +} NTL_NTFS3G_VOLUME; + +typedef struct NTL_NTFS3G_FILE { + NTL_NTFS3G_VOLUME* Volume; + ntfs_inode* Inode; + ntfs_attr* Data; +} NTL_NTFS3G_FILE; + +typedef struct NTL_NTFS3G_DIR { + NTL_NTFS3G_VOLUME* Volume; + ntfs_inode* Inode; + s64 Position; +} NTL_NTFS3G_DIR; + +static LIST_ENTRY gNtfs3gMounts; +static uint32_t gNtfs3gMountedCount; +static MUTEX gNtfs3gMountMutex; +static MUTEX gNtfs3gFsMutex; +static SPINLOCK gNtfs3gMutexInitLock = SPINLOCK_INIT; +static int gNtfs3gMutexesReady; + +static void Ntfs3gListInit(LIST_ENTRY* head) { + head->Fwd = head; + head->Bwd = head; +} + +static void Ntfs3gInitStaticMutex(MUTEX* mutex) { + memset(mutex, 0, sizeof(*mutex)); + ObInitializeObjectHeader(&mutex->Header, ObjectTypeMutex); + mutex->Lock = (SPINLOCK)SPINLOCK_INIT; + mutex->Owner = (THREAD*)0; + Ntfs3gListInit(&mutex->WaitList); + Ntfs3gListInit(&mutex->OwnerLink); + mutex->DonationClass = KeSchedulingNormal; + mutex->DonationPriority = PriorityLow; +} + +static void Ntfs3gEnsureMutexes(void) { + if (gNtfs3gMutexesReady) + return; + uintptr_t flags = KeAcquireSpinlock(&gNtfs3gMutexInitLock); + if (!gNtfs3gMutexesReady) { + Ntfs3gInitStaticMutex(&gNtfs3gMountMutex); + Ntfs3gInitStaticMutex(&gNtfs3gFsMutex); + gNtfs3gMutexesReady = 1; + } + KeReleaseSpinlock(&gNtfs3gMutexInitLock, flags); +} + +static KSTATUS Ntfs3gAcquireFs(void) { + Ntfs3gEnsureMutexes(); + if (!KeCanWait()) + return NTLSTATUS_INVALID_IRQL; + KSTATUS status = KeAcquireMutex(&gNtfs3gFsMutex, KE_WAIT_INFINITE); + return KE_MUTEX_ACQUIRE_SUCCEEDED(status) ? NTLSTATUS_SUCCESS : status; +} + +static void Ntfs3gReleaseFs(void) { + KeReleaseMutex(&gNtfs3gFsMutex); +} + +static KSTATUS Ntfs3gMapErrno(int error) { + switch (error) { + case 0: return NTLSTATUS_SUCCESS; + case ENOENT: + case ENXIO: + case ENODEV: return NTLSTATUS_NOT_FOUND; + case EEXIST: return NTLSTATUS_ALREADY_EXISTS; + case ENOMEM: return NTLSTATUS_NO_MEMORY; + case ENOSPC: + case EFBIG: + case EMLINK: return NTLSTATUS_LIMIT_REACHED; + case EACCES: + case EPERM: + case EROFS: return NTLSTATUS_ACCESS_DENIED; + case ENOTSUP: return NTLSTATUS_NOT_SUPPORTED; + case EIO: return NTLSTATUS_DEVICE_ERROR; + case ENOTEMPTY: + case EISDIR: + case ENOTDIR: + case EINVAL: return NTLSTATUS_INVALID_PARAMETER; + default: return NTLSTATUS_FAILURE; + } +} + +static NTL_NTFS3G_VOLUME* Ntfs3gVolume(IO_VOLUME_PROVIDER* provider) { + return provider ? (NTL_NTFS3G_VOLUME*)provider->Context : (NTL_NTFS3G_VOLUME*)0; +} + +static char* Ntfs3gDupPath(SYS_STRING_VIEW path) { + if (path.Length > SIZE_MAX - 1) + return (char*)0; + char* out = (char*)MmAllocatePool(path.Length + 1); + if (!out) + return (char*)0; + if (path.Length) + memcpy(out, path.Data, path.Length); + out[path.Length] = '\0'; + return out; +} + +static bool Ntfs3gIsDirectory(const ntfs_inode* ni) { + return ni && ni->mrec && (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) != 0; +} + +static uint32_t Ntfs3gAttributes(const ntfs_inode* ni) { + if (!ni) + return 0; + uint32_t out = 0; + if (Ntfs3gIsDirectory(ni)) out |= SYS_FILE_ATTR_DIRECTORY; + if (ni->flags & FILE_ATTR_READONLY) out |= SYS_FILE_ATTR_READ_ONLY; + if (ni->flags & FILE_ATTR_HIDDEN) out |= SYS_FILE_ATTR_HIDDEN; + if (ni->flags & FILE_ATTR_SYSTEM) out |= SYS_FILE_ATTR_SYSTEM; + if (ni->flags & FILE_ATTR_ARCHIVE) out |= SYS_FILE_ATTR_ARCHIVE; + return out; +} + +static void Ntfs3gSetTime(SYS_SYSTEM_TIME* out, ntfs_time value) { + memset(out, 0, sizeof(*out)); + struct timespec ts = ntfs2timespec(value); + if (ts.tv_sec < 0) + return; + + uint64_t seconds = (uint64_t)ts.tv_sec; + out->EpochSeconds = seconds; + out->Nanosecond = ts.tv_nsec > 0 ? (uint32_t)ts.tv_nsec : 0; + out->Millisecond = (uint16_t)(out->Nanosecond / 1000000u); + out->Flags = SYS_TIME_FLAG_VALID; + out->TimeZoneMinutes = SYS_TIME_UNSPECIFIED_TZ; + + uint64_t days = seconds / 86400ull; + uint32_t rem = (uint32_t)(seconds % 86400ull); + out->Hour = (uint8_t)(rem / 3600u); + out->Minute = (uint8_t)((rem % 3600u) / 60u); + out->Second = (uint8_t)(rem % 60u); + out->DayOfWeek = (uint8_t)((days + 4u) % 7u); + + int64_t z = (int64_t)days + 719468ll; + int64_t era = (z >= 0 ? z : z - 146096ll) / 146097ll; + uint32_t doe = (uint32_t)(z - era * 146097ll); + uint32_t yoe = (doe - doe / 1460u + doe / 36524u - doe / 146096u) / 365u; + int64_t year = (int64_t)yoe + era * 400ll; + uint32_t doy = doe - (365u * yoe + yoe / 4u - yoe / 100u); + uint32_t mp = (5u * doy + 2u) / 153u; + uint32_t day = doy - (153u * mp + 2u) / 5u + 1u; + uint32_t month = mp + (mp < 10u ? 3u : (uint32_t)-9); + year += (month <= 2u); + out->Year = (uint16_t)year; + out->Month = (uint8_t)month; + out->Day = (uint8_t)day; +} + +static void Ntfs3gFillStat(NTL_NTFS3G_VOLUME* volume, ntfs_inode* ni, + ntfs_attr* data, SYS_FILE_STAT* out) { + memset(out, 0, sizeof(*out)); + out->Size = sizeof(*out); + out->Type = Ntfs3gIsDirectory(ni) ? SYS_FILE_TYPE_DIRECTORY : SYS_FILE_TYPE_REGULAR; + out->Attributes = Ntfs3gAttributes(ni); + out->LinkCount = ni && ni->mrec ? (uint32_t)le16_to_cpu(ni->mrec->link_count) : 1u; + if (data && data->data_size > 0) + out->FileSize = (uint64_t)data->data_size; + else if (ni && !Ntfs3gIsDirectory(ni) && ni->data_size > 0) + out->FileSize = (uint64_t)ni->data_size; + if (ni) + out->FileId = ni->mft_no; + if (volume && volume->Volume) + out->VolumeId = IoGetVolumeInstanceId(volume->Volume); + if (ni) { + Ntfs3gSetTime(&out->CreationTime, ni->creation_time); + Ntfs3gSetTime(&out->LastAccessTime, ni->last_access_time); + Ntfs3gSetTime(&out->LastWriteTime, ni->last_data_change_time); + Ntfs3gSetTime(&out->ChangeTime, ni->last_mft_change_time); + } +} + +static ntfs_attr* Ntfs3gOpenData(ntfs_inode* ni) { + return ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); +} + +static int Ntfs3gSplitPath(const char* path, char** parentOut, + const char** leafOut, ntfschar** unicodeOut, int* unicodeLenOut) { + *parentOut = (char*)0; + *leafOut = (const char*)0; + *unicodeOut = (ntfschar*)0; + *unicodeLenOut = 0; + if (!path || !*path) { + errno = EINVAL; + return -1; + } + + const char* slash = strrchr(path, '/'); + const char* leaf = slash ? slash + 1 : path; + if (!*leaf) { + errno = EINVAL; + return -1; + } + size_t parentLen = slash ? (size_t)(slash - path) : 0; + char* parent = (char*)MmAllocatePool(parentLen + 1); + if (!parent) { + errno = ENOMEM; + return -1; + } + if (parentLen) + memcpy(parent, path, parentLen); + parent[parentLen] = '\0'; + + ntfschar* unicode = (ntfschar*)0; + int unicodeLen = ntfs_mbstoucs(leaf, &unicode); + if (unicodeLen <= 0 || unicodeLen > 255) { + int saved = errno ? errno : EINVAL; + if (unicode) + ntfs_ucsfree(unicode); + MmFreePool(parent); + errno = saved; + return -1; + } + + *parentOut = parent; + *leafOut = leaf; + *unicodeOut = unicode; + *unicodeLenOut = unicodeLen; + return 0; +} + +static ntfs_inode* Ntfs3gOpenParent(NTL_NTFS3G_VOLUME* volume, const char* parentPath) { + return ntfs_pathname_to_inode(volume->Ntfs, (ntfs_inode*)0, parentPath ? parentPath : ""); +} + +static int Ntfs3gDeletePathLocked(NTL_NTFS3G_VOLUME* volume, const char* path, bool directory) { + char* parentPath = (char*)0; + const char* leaf = (const char*)0; + ntfschar* unicode = (ntfschar*)0; + int unicodeLen = 0; + if (Ntfs3gSplitPath(path, &parentPath, &leaf, &unicode, &unicodeLen)) + return -1; + (void)leaf; + + ntfs_inode* parent = Ntfs3gOpenParent(volume, parentPath); + if (!parent) { + int saved = errno; + ntfs_ucsfree(unicode); + MmFreePool(parentPath); + errno = saved; + return -1; + } + + u64 mref = ntfs_inode_lookup_by_name(parent, unicode, unicodeLen); + if (mref == (u64)-1) { + int saved = errno; + ntfs_inode_close(parent); + ntfs_ucsfree(unicode); + MmFreePool(parentPath); + errno = saved; + return -1; + } + + ntfs_inode* ni = ntfs_inode_open(volume->Ntfs, mref); + if (!ni) { + int saved = errno; + ntfs_inode_close(parent); + ntfs_ucsfree(unicode); + MmFreePool(parentPath); + errno = saved; + return -1; + } + + if (directory != Ntfs3gIsDirectory(ni)) { + ntfs_inode_close(ni); + ntfs_inode_close(parent); + ntfs_ucsfree(unicode); + MmFreePool(parentPath); + errno = directory ? ENOTDIR : EISDIR; + return -1; + } + if (directory && ntfs_check_empty_dir(ni)) { + int saved = errno; + ntfs_inode_close(ni); + ntfs_inode_close(parent); + ntfs_ucsfree(unicode); + MmFreePool(parentPath); + errno = saved; + return -1; + } + + /* ntfs_delete owns/closes both ni and parent on every return path. */ + int rc = ntfs_delete(volume->Ntfs, path, ni, parent, unicode, (u8)unicodeLen); + int saved = errno; + ntfs_ucsfree(unicode); + MmFreePool(parentPath); + errno = saved; + return rc; +} + +static KSTATUS Ntfs3gOpen(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path, void** fileCtx) { + if (!fileCtx) + return NTLSTATUS_INVALID_PARAMETER; + *fileCtx = (void*)0; + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (!volume || volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + char* p = Ntfs3gDupPath(path); + if (!p) + return NTLSTATUS_NO_MEMORY; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)MmAllocatePool(sizeof(*file)); + if (!file) { + MmFreePool(p); + return NTLSTATUS_NO_MEMORY; + } + memset(file, 0, sizeof(*file)); + file->Volume = volume; + + KSTATUS status = Ntfs3gAcquireFs(); + if (KSUCCESS(status)) { + file->Inode = ntfs_pathname_to_inode(volume->Ntfs, (ntfs_inode*)0, p); + if (!file->Inode) { + status = Ntfs3gMapErrno(errno); + } else if (Ntfs3gIsDirectory(file->Inode)) { + ntfs_inode_close(file->Inode); + file->Inode = (ntfs_inode*)0; + status = NTLSTATUS_INVALID_PARAMETER; + } else { + file->Data = Ntfs3gOpenData(file->Inode); + if (!file->Data) { + status = Ntfs3gMapErrno(errno); + ntfs_inode_close(file->Inode); + file->Inode = (ntfs_inode*)0; + } + } + Ntfs3gReleaseFs(); + } + MmFreePool(p); + if (!KSUCCESS(status)) { + MmFreePool(file); + return status; + } + *fileCtx = file; + return NTLSTATUS_SUCCESS; +} + +static KSTATUS Ntfs3gRead(IO_VOLUME_PROVIDER* provider, void* fileCtx, uint64_t offset, + void* buffer, size_t size, size_t* bytesRead) { + (void)provider; + if (!fileCtx || (!buffer && size) || !bytesRead) + return NTLSTATUS_INVALID_PARAMETER; + *bytesRead = 0; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)fileCtx; + if (file->Volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + if (offset > INT64_MAX) + return NTLSTATUS_LIMIT_REACHED; + if (size > (size_t)INT64_MAX) + size = (size_t)INT64_MAX; + + KSTATUS status = Ntfs3gAcquireFs(); + if (!KSUCCESS(status)) + return status; + s64 got = ntfs_attr_pread(file->Data, (s64)offset, (s64)size, buffer); + if (got < 0) { + status = Ntfs3gMapErrno(errno); + } else { + *bytesRead = (size_t)got; + if (got > 0) + ntfs_inode_update_times(file->Inode, NTFS_UPDATE_ATIME); + status = NTLSTATUS_SUCCESS; + } + Ntfs3gReleaseFs(); + return status; +} + +static KSTATUS Ntfs3gWrite(IO_VOLUME_PROVIDER* provider, void* fileCtx, uint64_t offset, + const void* buffer, size_t size, size_t* bytesWritten) { + (void)provider; + if (!fileCtx || (!buffer && size) || !bytesWritten) + return NTLSTATUS_INVALID_PARAMETER; + *bytesWritten = 0; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)fileCtx; + if (file->Volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + if (offset > INT64_MAX || size > (size_t)INT64_MAX || + (uint64_t)size > (uint64_t)INT64_MAX - offset) + return NTLSTATUS_LIMIT_REACHED; + + KSTATUS status = Ntfs3gAcquireFs(); + if (!KSUCCESS(status)) + return status; + s64 done = ntfs_attr_pwrite(file->Data, (s64)offset, (s64)size, buffer); + if (done < 0) { + status = Ntfs3gMapErrno(errno); + } else { + *bytesWritten = (size_t)done; + if (done > 0) { + file->Inode->flags |= FILE_ATTR_ARCHIVE; + ntfs_inode_update_times(file->Inode, NTFS_UPDATE_MCTIME); + } + status = NTLSTATUS_SUCCESS; + } + Ntfs3gReleaseFs(); + return status; +} + +static KSTATUS Ntfs3gCreate(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path, void** fileCtx) { + if (!fileCtx) + return NTLSTATUS_INVALID_PARAMETER; + *fileCtx = (void*)0; + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (!volume || volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + char* p = Ntfs3gDupPath(path); + if (!p) + return NTLSTATUS_NO_MEMORY; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)MmAllocatePool(sizeof(*file)); + if (!file) { + MmFreePool(p); + return NTLSTATUS_NO_MEMORY; + } + memset(file, 0, sizeof(*file)); + file->Volume = volume; + + KSTATUS status = Ntfs3gAcquireFs(); + if (KSUCCESS(status)) { + char* parentPath = (char*)0; + const char* leaf = (const char*)0; + ntfschar* unicode = (ntfschar*)0; + int unicodeLen = 0; + if (Ntfs3gSplitPath(p, &parentPath, &leaf, &unicode, &unicodeLen)) { + status = Ntfs3gMapErrno(errno); + } else { + (void)leaf; + ntfs_inode* parent = Ntfs3gOpenParent(volume, parentPath); + if (!parent) { + status = Ntfs3gMapErrno(errno); + } else { + file->Inode = ntfs_create(parent, const_cpu_to_le32(0), unicode, + (u8)unicodeLen, S_IFREG); + int saved = errno; + ntfs_inode_close(parent); + errno = saved; + if (!file->Inode) { + status = Ntfs3gMapErrno(errno); + } else { + file->Data = Ntfs3gOpenData(file->Inode); + if (!file->Data) { + status = Ntfs3gMapErrno(errno); + ntfs_inode_close(file->Inode); + file->Inode = (ntfs_inode*)0; + } + } + } + ntfs_ucsfree(unicode); + MmFreePool(parentPath); + } + Ntfs3gReleaseFs(); + } + MmFreePool(p); + if (!KSUCCESS(status)) { + MmFreePool(file); + return status; + } + *fileCtx = file; + return NTLSTATUS_SUCCESS; +} + +static KSTATUS Ntfs3gDelete(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path) { + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (!volume || volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + char* p = Ntfs3gDupPath(path); + if (!p) + return NTLSTATUS_NO_MEMORY; + KSTATUS status = Ntfs3gAcquireFs(); + if (KSUCCESS(status)) { + int rc = Ntfs3gDeletePathLocked(volume, p, false); + status = rc ? Ntfs3gMapErrno(errno) : NTLSTATUS_SUCCESS; + Ntfs3gReleaseFs(); + } + MmFreePool(p); + return status; +} + +static KSTATUS Ntfs3gRename(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW oldPath, + SYS_STRING_VIEW newPath) { + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (!volume || volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + char* oldp = Ntfs3gDupPath(oldPath); + char* newp = Ntfs3gDupPath(newPath); + if (!oldp || !newp) { + if (oldp) MmFreePool(oldp); + if (newp) MmFreePool(newp); + return NTLSTATUS_NO_MEMORY; + } + if (strcmp(oldp, newp) == 0) { + MmFreePool(oldp); + MmFreePool(newp); + return NTLSTATUS_SUCCESS; + } + + KSTATUS status = Ntfs3gAcquireFs(); + if (KSUCCESS(status)) { + ntfs_inode* existing = ntfs_pathname_to_inode(volume->Ntfs, (ntfs_inode*)0, newp); + if (existing) { + ntfs_inode_close(existing); + status = NTLSTATUS_ALREADY_EXISTS; + } else if (errno != ENOENT) { + status = Ntfs3gMapErrno(errno); + } else { + ntfs_inode* source = ntfs_pathname_to_inode(volume->Ntfs, (ntfs_inode*)0, oldp); + if (!source) { + status = Ntfs3gMapErrno(errno); + } else { + bool sourceDir = Ntfs3gIsDirectory(source); + char* parentPath = (char*)0; + const char* leaf = (const char*)0; + ntfschar* unicode = (ntfschar*)0; + int unicodeLen = 0; + if (Ntfs3gSplitPath(newp, &parentPath, &leaf, &unicode, &unicodeLen)) { + status = Ntfs3gMapErrno(errno); + ntfs_inode_close(source); + } else { + (void)leaf; + ntfs_inode* parent = Ntfs3gOpenParent(volume, parentPath); + if (!parent) { + status = Ntfs3gMapErrno(errno); + ntfs_inode_close(source); + } else if (ntfs_link(source, parent, unicode, (u8)unicodeLen)) { + status = Ntfs3gMapErrno(errno); + ntfs_inode_close(parent); + ntfs_inode_close(source); + } else { + ntfs_inode_close(parent); + ntfs_inode_close(source); + if (Ntfs3gDeletePathLocked(volume, oldp, sourceDir)) { + status = Ntfs3gMapErrno(errno); + (void)Ntfs3gDeletePathLocked(volume, newp, sourceDir); + } else { + status = NTLSTATUS_SUCCESS; + } + } + ntfs_ucsfree(unicode); + MmFreePool(parentPath); + } + } + } + Ntfs3gReleaseFs(); + } + MmFreePool(oldp); + MmFreePool(newp); + return status; +} + +static KSTATUS Ntfs3gCreateDir(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path) { + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (!volume || volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + char* p = Ntfs3gDupPath(path); + if (!p) + return NTLSTATUS_NO_MEMORY; + KSTATUS status = Ntfs3gAcquireFs(); + if (KSUCCESS(status)) { + char* parentPath = (char*)0; + const char* leaf = (const char*)0; + ntfschar* unicode = (ntfschar*)0; + int unicodeLen = 0; + if (Ntfs3gSplitPath(p, &parentPath, &leaf, &unicode, &unicodeLen)) { + status = Ntfs3gMapErrno(errno); + } else { + (void)leaf; + ntfs_inode* parent = Ntfs3gOpenParent(volume, parentPath); + if (!parent) { + status = Ntfs3gMapErrno(errno); + } else { + ntfs_inode* ni = ntfs_create(parent, const_cpu_to_le32(0), unicode, + (u8)unicodeLen, S_IFDIR); + int saved = errno; + ntfs_inode_close(parent); + errno = saved; + if (!ni) + status = Ntfs3gMapErrno(errno); + else { + ntfs_inode_close(ni); + status = NTLSTATUS_SUCCESS; + } + } + ntfs_ucsfree(unicode); + MmFreePool(parentPath); + } + Ntfs3gReleaseFs(); + } + MmFreePool(p); + return status; +} + +static KSTATUS Ntfs3gRemoveDir(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path) { + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (!volume || volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + char* p = Ntfs3gDupPath(path); + if (!p) + return NTLSTATUS_NO_MEMORY; + KSTATUS status = Ntfs3gAcquireFs(); + if (KSUCCESS(status)) { + int rc = Ntfs3gDeletePathLocked(volume, p, true); + status = rc ? Ntfs3gMapErrno(errno) : NTLSTATUS_SUCCESS; + Ntfs3gReleaseFs(); + } + MmFreePool(p); + return status; +} + +static KSTATUS Ntfs3gTruncate(IO_VOLUME_PROVIDER* provider, void* fileCtx, + uint64_t newSize, uint32_t fileFlags) { + (void)provider; + (void)fileFlags; + if (!fileCtx) + return NTLSTATUS_INVALID_PARAMETER; + if (newSize > INT64_MAX) + return NTLSTATUS_LIMIT_REACHED; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)fileCtx; + if (file->Volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + KSTATUS status = Ntfs3gAcquireFs(); + if (!KSUCCESS(status)) + return status; + if (ntfs_attr_truncate(file->Data, (s64)newSize)) { + status = Ntfs3gMapErrno(errno); + } else { + file->Inode->flags |= FILE_ATTR_ARCHIVE; + ntfs_inode_update_times(file->Inode, NTFS_UPDATE_MCTIME); + status = NTLSTATUS_SUCCESS; + } + Ntfs3gReleaseFs(); + return status; +} + +static uint64_t Ntfs3gGetSize(IO_VOLUME_PROVIDER* provider, void* fileCtx) { + (void)provider; + if (!fileCtx) + return 0; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)fileCtx; + return file->Data && file->Data->data_size > 0 ? (uint64_t)file->Data->data_size : 0; +} + +static KSTATUS Ntfs3gQueryInfo(IO_VOLUME_PROVIDER* provider, void* fileCtx, SYS_FILE_STAT* out) { + (void)provider; + if (!fileCtx || !out) + return NTLSTATUS_INVALID_PARAMETER; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)fileCtx; + if (file->Volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + KSTATUS status = Ntfs3gAcquireFs(); + if (!KSUCCESS(status)) + return status; + Ntfs3gFillStat(file->Volume, file->Inode, file->Data, out); + Ntfs3gReleaseFs(); + return NTLSTATUS_SUCCESS; +} + +static KSTATUS Ntfs3gStat(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path, SYS_FILE_STAT* out) { + if (!out) + return NTLSTATUS_INVALID_PARAMETER; + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (!volume || volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + char* p = Ntfs3gDupPath(path); + if (!p) + return NTLSTATUS_NO_MEMORY; + KSTATUS status = Ntfs3gAcquireFs(); + if (KSUCCESS(status)) { + ntfs_inode* ni = ntfs_pathname_to_inode(volume->Ntfs, (ntfs_inode*)0, p); + if (!ni) { + status = Ntfs3gMapErrno(errno); + } else { + ntfs_attr* data = (ntfs_attr*)0; + if (!Ntfs3gIsDirectory(ni)) + data = Ntfs3gOpenData(ni); + if (!Ntfs3gIsDirectory(ni) && !data) + status = Ntfs3gMapErrno(errno); + else { + Ntfs3gFillStat(volume, ni, data, out); + status = NTLSTATUS_SUCCESS; + } + if (data) ntfs_attr_close(data); + ntfs_inode_close(ni); + } + Ntfs3gReleaseFs(); + } + MmFreePool(p); + return status; +} + +static KSTATUS Ntfs3gQueryIdentity(IO_VOLUME_PROVIDER* provider, void* fileCtx, + IO_FILE_IDENTITY* out) { + (void)provider; + if (!fileCtx || !out) + return NTLSTATUS_INVALID_PARAMETER; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)fileCtx; + memset(out, 0, sizeof(*out)); + if (file->Volume->Volume) + out->VolumeId = IoGetVolumeInstanceId(file->Volume->Volume); + out->FileId = file->Inode->mft_no; + out->Generation = le16_to_cpu(file->Inode->mrec->sequence_number); + out->Size = file->Data->data_size > 0 ? (uint64_t)file->Data->data_size : 0; + return NTLSTATUS_SUCCESS; +} + +static KSTATUS Ntfs3gFlush(IO_VOLUME_PROVIDER* provider, void* fileCtx) { + (void)provider; + if (!fileCtx) + return NTLSTATUS_INVALID_PARAMETER; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)fileCtx; + if (file->Volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + KSTATUS status = Ntfs3gAcquireFs(); + if (!KSUCCESS(status)) + return status; + if (ntfs_inode_sync(file->Inode) || ntfs_device_sync(file->Volume->Device)) + status = Ntfs3gMapErrno(errno); + else + status = NTLSTATUS_SUCCESS; + Ntfs3gReleaseFs(); + return status; +} + +static void Ntfs3gClose(IO_VOLUME_PROVIDER* provider, void* fileCtx) { + (void)provider; + if (!fileCtx) + return; + NTL_NTFS3G_FILE* file = (NTL_NTFS3G_FILE*)fileCtx; + if (KSUCCESS(Ntfs3gAcquireFs())) { + if (file->Data) + ntfs_attr_close(file->Data); + if (file->Inode) + ntfs_inode_close(file->Inode); + Ntfs3gReleaseFs(); + } + MmFreePool(file); +} + +static void Ntfs3gQueryStats(IO_VOLUME_PROVIDER* provider, IO_FILESYSTEM_STATS* out) { + (void)provider; + if (out) + memset(out, 0, sizeof(*out)); +} + +static KSTATUS Ntfs3gOpenDir(IO_VOLUME_PROVIDER* provider, SYS_STRING_VIEW path, void** dirCtx) { + if (!dirCtx) + return NTLSTATUS_INVALID_PARAMETER; + *dirCtx = (void*)0; + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (!volume || volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + char* p = Ntfs3gDupPath(path); + if (!p) + return NTLSTATUS_NO_MEMORY; + NTL_NTFS3G_DIR* dir = (NTL_NTFS3G_DIR*)MmAllocatePool(sizeof(*dir)); + if (!dir) { + MmFreePool(p); + return NTLSTATUS_NO_MEMORY; + } + memset(dir, 0, sizeof(*dir)); + dir->Volume = volume; + + KSTATUS status = Ntfs3gAcquireFs(); + if (KSUCCESS(status)) { + dir->Inode = ntfs_pathname_to_inode(volume->Ntfs, (ntfs_inode*)0, p); + if (!dir->Inode) + status = Ntfs3gMapErrno(errno); + else if (!Ntfs3gIsDirectory(dir->Inode)) { + ntfs_inode_close(dir->Inode); + dir->Inode = (ntfs_inode*)0; + status = NTLSTATUS_INVALID_PARAMETER; + } + Ntfs3gReleaseFs(); + } + MmFreePool(p); + if (!KSUCCESS(status)) { + MmFreePool(dir); + return status; + } + *dirCtx = dir; + return NTLSTATUS_SUCCESS; +} + +typedef struct NTL_NTFS3G_DIRENT_CAPTURE { + char Name[SYS_DIR_NAME_MAX]; + MFT_REF Mref; + unsigned Type; + bool HideNtfsMetadata; + bool Got; +} NTL_NTFS3G_DIRENT_CAPTURE; + +static int Ntfs3gFillDir(void* context, const ntfschar* name, const int nameLen, + const int nameType, const s64 pos, const MFT_REF mref, + const unsigned type) { + (void)pos; + NTL_NTFS3G_DIRENT_CAPTURE* capture = (NTL_NTFS3G_DIRENT_CAPTURE*)context; + if (nameType == FILE_NAME_DOS) + return 0; + char* utf8 = (char*)0; + int len = ntfs_ucstombs(name, nameLen, &utf8, 0); + if (len < 0 || !utf8) + return 0; + if ((len == 1 && utf8[0] == '.') || (len == 2 && utf8[0] == '.' && utf8[1] == '.')) { + free(utf8); + return 0; + } + /* Match the old native NTFS provider: metadata names are hidden from + normal root listings, but pathname lookup still has access to them. */ + if (capture->HideNtfsMetadata && len > 0 && utf8[0] == '$') { + free(utf8); + return 0; + } + if (capture->Got) { + free(utf8); + errno = 0; + return 1; + } + size_t copy = (size_t)len; + if (copy >= sizeof(capture->Name)) + copy = sizeof(capture->Name) - 1; + memcpy(capture->Name, utf8, copy); + capture->Name[copy] = '\0'; + free(utf8); + capture->Mref = mref; + capture->Type = type; + capture->Got = true; + return 0; +} + +static KSTATUS Ntfs3gReadDir(IO_VOLUME_PROVIDER* provider, void* dirCtx, KDIR_ENTRY* out) { + (void)provider; + if (!dirCtx || !out) + return NTLSTATUS_INVALID_PARAMETER; + NTL_NTFS3G_DIR* dir = (NTL_NTFS3G_DIR*)dirCtx; + if (dir->Volume->Dead) + return NTLSTATUS_DEVICE_REMOVED; + memset(out, 0, sizeof(*out)); + + KSTATUS status = Ntfs3gAcquireFs(); + if (!KSUCCESS(status)) + return status; + NTL_NTFS3G_DIRENT_CAPTURE capture; + memset(&capture, 0, sizeof(capture)); + capture.HideNtfsMetadata = dir->Inode->mft_no == FILE_root; + errno = 0; + int rc = ntfs_readdir(dir->Inode, &dir->Position, &capture, Ntfs3gFillDir); + if (!capture.Got) { + status = rc == 0 ? NTLSTATUS_NO_MORE_FILES : Ntfs3gMapErrno(errno); + } else { + memcpy(out->Name, capture.Name, sizeof(out->Name)); + ntfs_inode* ni = ntfs_inode_open(dir->Volume->Ntfs, capture.Mref); + if (ni) { + ntfs_attr* data = (ntfs_attr*)0; + if (!Ntfs3gIsDirectory(ni)) + data = Ntfs3gOpenData(ni); + out->Attr = (uint8_t)Ntfs3gAttributes(ni); + if (data && data->data_size > 0) + out->FileSize = data->data_size > UINT32_MAX ? UINT32_MAX : (uint32_t)data->data_size; + SYS_FILE_STAT stat; + Ntfs3gFillStat(dir->Volume, ni, data, &stat); + out->CreateTime = stat.CreationTime; + out->LastAccessTime = stat.LastAccessTime; + out->LastWriteTime = stat.LastWriteTime; + if (data) ntfs_attr_close(data); + ntfs_inode_close(ni); + } else { + out->Attr = capture.Type == NTFS_DT_DIR ? SYS_FILE_ATTR_DIRECTORY : SYS_FILE_ATTR_ARCHIVE; + } + ntfs_inode_update_times(dir->Inode, NTFS_UPDATE_ATIME); + status = NTLSTATUS_SUCCESS; + } + Ntfs3gReleaseFs(); + return status; +} + +static void Ntfs3gCloseDir(IO_VOLUME_PROVIDER* provider, void* dirCtx) { + (void)provider; + if (!dirCtx) + return; + NTL_NTFS3G_DIR* dir = (NTL_NTFS3G_DIR*)dirCtx; + if (KSUCCESS(Ntfs3gAcquireFs())) { + if (dir->Inode) + ntfs_inode_close(dir->Inode); + Ntfs3gReleaseFs(); + } + MmFreePool(dir); +} + +static void Ntfs3gVolumeDead(IO_VOLUME_PROVIDER* provider, IO_VOLUME_REMOVAL_REASON reason) { + (void)reason; + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (volume) + volume->Dead = true; +} + +static void Ntfs3gDestroyProvider(IO_VOLUME_PROVIDER* provider) { + NTL_NTFS3G_VOLUME* volume = Ntfs3gVolume(provider); + if (!volume) + return; + if (KSUCCESS(Ntfs3gAcquireFs())) { + if (volume->Ntfs) { + (void)ntfs_umount(volume->Ntfs, TRUE); + volume->Ntfs = (ntfs_volume*)0; + volume->Device = (struct ntfs_device*)0; /* ntfs_umount frees it. */ + } else if (volume->Device) { + (void)ntfs_device_free(volume->Device); + volume->Device = (struct ntfs_device*)0; + } + Ntfs3gReleaseFs(); + } + memset(volume, 0, sizeof(*volume)); + MmFreePool(volume); +} + +static void Ntfs3gInitProvider(IO_VOLUME_PROVIDER* provider, NTL_NTFS3G_VOLUME* volume) { + memset(provider, 0, sizeof(*provider)); + provider->Open = Ntfs3gOpen; + provider->Read = Ntfs3gRead; + provider->Write = Ntfs3gWrite; + provider->Create = Ntfs3gCreate; + provider->Delete = Ntfs3gDelete; + provider->Rename = Ntfs3gRename; + provider->CreateDir = Ntfs3gCreateDir; + provider->RemoveDir = Ntfs3gRemoveDir; + provider->Truncate = Ntfs3gTruncate; + provider->GetSize = Ntfs3gGetSize; + provider->QueryInfo = Ntfs3gQueryInfo; + provider->Stat = Ntfs3gStat; + provider->QueryIdentity = Ntfs3gQueryIdentity; + provider->Flush = Ntfs3gFlush; + provider->Close = Ntfs3gClose; + provider->QueryStats = Ntfs3gQueryStats; + provider->OpenDir = Ntfs3gOpenDir; + provider->ReadDir = Ntfs3gReadDir; + provider->CloseDir = Ntfs3gCloseDir; + provider->VolumeDead = Ntfs3gVolumeDead; + provider->Destroy = Ntfs3gDestroyProvider; + provider->Context = volume; +} + +static NTL_NTFS3G_VOLUME* Ntfs3gFindMountLocked(const IO_PARTITION_INFO* partition) { + for (LIST_ENTRY* entry = gNtfs3gMounts.Fwd; entry != &gNtfs3gMounts; entry = entry->Fwd) { + NTL_NTFS3G_VOLUME* volume = CONTAINING_RECORD(entry, NTL_NTFS3G_VOLUME, MountLink); + if (volume->DiskIndex == partition->DiskIndex && volume->PartitionLba == partition->StartLba) + return volume; + } + return (NTL_NTFS3G_VOLUME*)0; +} + +static bool Ntfs3gProbePartition(const IO_PARTITION_INFO* partition) { + if (!partition || !partition->SectorCount) + return false; + uint8_t sector[NTL_NTFS3G_SECTOR_SIZE]; + if (!KSUCCESS(IoReadDiskSectors(partition->DiskIndex, partition->StartLba, 1, sector))) + return false; + return memcmp(sector + 3, "NTFS ", 8) == 0 && sector[510] == 0x55 && sector[511] == 0xAA; +} + +static bool Ntfs3gTryMountPartition(const IO_PARTITION_INFO* partition) { + if (!partition || partition->SectorCount > UINT64_MAX / NTL_NTFS3G_SECTOR_SIZE || + !IoDiskDevicePresent(partition->DiskIndex) || !Ntfs3gProbePartition(partition)) + return false; + Ntfs3gEnsureMutexes(); + if (!KE_MUTEX_ACQUIRE_SUCCEEDED(KeAcquireMutex(&gNtfs3gMountMutex, KE_WAIT_INFINITE))) + return false; + if (Ntfs3gFindMountLocked(partition)) { + KeReleaseMutex(&gNtfs3gMountMutex); + return false; + } + + NTL_NTFS3G_VOLUME* volume = (NTL_NTFS3G_VOLUME*)MmAllocatePool(sizeof(*volume)); + if (!volume) { + KeReleaseMutex(&gNtfs3gMountMutex); + return false; + } + memset(volume, 0, sizeof(*volume)); + Ntfs3gListInit(&volume->MountLink); + volume->DiskIndex = partition->DiskIndex; + volume->PartitionLba = partition->StartLba; + volume->PartitionSectors = partition->SectorCount; + volume->DeviceContext.DiskIndex = partition->DiskIndex; + volume->DeviceContext.PartitionLba = partition->StartLba; + volume->DeviceContext.PartitionSectors = partition->SectorCount; + + KSTATUS status = Ntfs3gAcquireFs(); + if (!KSUCCESS(status)) { + MmFreePool(volume); + KeReleaseMutex(&gNtfs3gMountMutex); + return false; + } + volume->Device = Ntfs3gCreateDevice(&volume->DeviceContext); + if (!volume->Device) { + status = Ntfs3gMapErrno(errno); + } else { + volume->Ntfs = ntfs_device_mount(volume->Device, NTFS_MNT_RECOVER | NTFS_MNT_EXCLUSIVE); + if (!volume->Ntfs) { + status = Ntfs3gMapErrno(errno); + (void)ntfs_device_free(volume->Device); + volume->Device = (struct ntfs_device*)0; + } else if (NVolReadOnly(volume->Ntfs)) { + (void)ntfs_umount(volume->Ntfs, TRUE); + volume->Ntfs = (ntfs_volume*)0; + volume->Device = (struct ntfs_device*)0; + status = NTLSTATUS_ACCESS_DENIED; + } else { + status = NTLSTATUS_SUCCESS; + } + } + Ntfs3gReleaseFs(); + + if (!KSUCCESS(status)) { + DbgPrint("NTFS3G: disk=%u lba=%llu mount failed status=0x%x errno=%d\n", + partition->DiskIndex, (unsigned long long)partition->StartLba, status, errno); + MmFreePool(volume); + KeReleaseMutex(&gNtfs3gMountMutex); + return false; + } + + IO_VOLUME_PROVIDER provider; + Ntfs3gInitProvider(&provider, volume); + IO_VOLUME_SOURCE source; + memset(&source, 0, sizeof(source)); + source.Type = IoVolumeSourcePartition; + source.DiskIndex = volume->DiskIndex; + source.StartLba = volume->PartitionLba; + source.SectorCount = volume->PartitionSectors; + + IO_VOLUME* ioVolume = (IO_VOLUME*)0; + status = IoCreateVolume(&provider, &source, &ioVolume); + if (!KSUCCESS(status)) { + if (ioVolume) { + (void)IoRemoveVolume(ioVolume, IoVolumeUnmounted); + IoReleaseVolume(ioVolume); + } else { + Ntfs3gDestroyProvider(&provider); + } + KeReleaseMutex(&gNtfs3gMountMutex); + return false; + } + + volume->Volume = ioVolume; + ListInsertTail(&gNtfs3gMounts, &volume->MountLink); + gNtfs3gMountedCount++; + DbgPrint("NTFS3G: disk=%u lba=%llu mounted volume=%u; total=%u\n", + volume->DiskIndex, (unsigned long long)volume->PartitionLba, + IoGetVolumeInstanceId(ioVolume), gNtfs3gMountedCount); + KeReleaseMutex(&gNtfs3gMountMutex); + return true; +} + +static void Ntfs3gUnmountPartition(const IO_PARTITION_INFO* partition, IO_PARTITION_CHANGE change) { + if (!partition) + return; + Ntfs3gEnsureMutexes(); + if (!KE_MUTEX_ACQUIRE_SUCCEEDED(KeAcquireMutex(&gNtfs3gMountMutex, KE_WAIT_INFINITE))) + return; + NTL_NTFS3G_VOLUME* volume = Ntfs3gFindMountLocked(partition); + if (!volume) { + KeReleaseMutex(&gNtfs3gMountMutex); + return; + } + IO_VOLUME* ioVolume = volume->Volume; + uint32_t instance = IoGetVolumeInstanceId(ioVolume); + ListRemove(&volume->MountLink); + volume->Volume = (IO_VOLUME*)0; + volume->Dead = true; + if (gNtfs3gMountedCount) + gNtfs3gMountedCount--; + uint32_t count = gNtfs3gMountedCount; + KeReleaseMutex(&gNtfs3gMountMutex); + + (void)IoRemoveVolume(ioVolume, IoVolumeBackingRemoved); + IoReleaseVolume(ioVolume); + DbgPrint("NTFS3G: disk=%u lba=%llu %s; unmounted volume=%u; total=%u\n", + partition->DiskIndex, (unsigned long long)partition->StartLba, + change == IoPartitionSurpriseRemoved ? "surprise removed" : "removed", + instance, count); +} + +static void Ntfs3gPartitionChange(const IO_PARTITION_INFO* partition, + IO_PARTITION_CHANGE change, void* context) { + (void)context; + switch (change) { + case IoPartitionArrived: + (void)Ntfs3gTryMountPartition(partition); + break; + case IoPartitionSurpriseRemoved: + case IoPartitionRemoved: + Ntfs3gUnmountPartition(partition, change); + break; + default: + break; + } +} + +KSTATUS DriverEntry(const NTL_DDK* ddk, NTL_DRIVER_INIT* init) { + DbgPrint("NTFS3G: Init (libntfs-3g replacement driver)\n"); + if (!ddk) + return NTLSTATUS_INVALID_PARAMETER; + if (init) + init->Name = "ntfs"; + Ntfs3gListInit(&gNtfs3gMounts); + gNtfs3gMountedCount = 0; + Ntfs3gEnsureMutexes(); + KSTATUS status = IoRegisterPartitionChangeCallback(Ntfs3gPartitionChange, (void*)0); + if (!KSUCCESS(status)) + return status; + DbgPrint("NTFS3G: Init complete; mounted %u volume(s).\n", gNtfs3gMountedCount); + return NTLSTATUS_SUCCESS; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/ntfs3g_msvc.patch b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_msvc.patch new file mode 100644 index 0000000..7caf120 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_msvc.patch @@ -0,0 +1,899 @@ +diff --git a/include/ntfs-3g/cache.h b/include/ntfs-3g/cache.h +index be63b1a..195222c 100644 +--- a/include/ntfs-3g/cache.h ++++ b/include/ntfs-3g/cache.h +@@ -29,7 +29,7 @@ struct CACHED_GENERIC { + struct CACHED_GENERIC *previous; + void *variable; + size_t varsize; +- union ALIGNMENT payload[0]; ++ union ALIGNMENT payload[1]; + } ; + + struct CACHED_INODE { +@@ -37,7 +37,7 @@ struct CACHED_INODE { + struct CACHED_INODE *previous; + const char *pathname; + size_t varsize; +- union ALIGNMENT payload[0]; ++ /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + u64 inum; + } ; +@@ -47,7 +47,7 @@ struct CACHED_NIDATA { + struct CACHED_NIDATA *previous; + const char *pathname; /* not used */ + size_t varsize; /* not used */ +- union ALIGNMENT payload[0]; ++ /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + u64 inum; + ntfs_inode *ni; +@@ -58,7 +58,7 @@ struct CACHED_LOOKUP { + struct CACHED_LOOKUP *previous; + const char *name; + size_t namesize; +- union ALIGNMENT payload[0]; ++ /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + u64 parent; + u64 inum; +@@ -93,7 +93,7 @@ struct CACHE_HEADER { + unsigned long hits; + int fixed_size; + int max_hash; +- struct CACHED_GENERIC entry[0]; ++ struct CACHED_GENERIC entry[1]; + } ; + + /* cast to generic, avoiding gcc warnings */ +diff --git a/include/ntfs-3g/layout.h b/include/ntfs-3g/layout.h +index ab6d453..05605c7 100644 +--- a/include/ntfs-3g/layout.h ++++ b/include/ntfs-3g/layout.h +@@ -1,6 +1,14 @@ +-#ifdef _MSC_VER +-#pragma pack(push, 1) +-#endif ++#ifdef _MSC_VER ++#pragma pack(push, 1) ++#endif ++#ifdef _MSC_VER ++#define NTFS_PACKED_ENUM_BEGIN(name, storage) enum ++#define NTFS_PACKED_ENUM_END(name, storage) ; typedef storage name ++#else ++#define NTFS_PACKED_ENUM_BEGIN(name, storage) typedef enum ++#define NTFS_PACKED_ENUM_END(name, storage) __attribute__((__packed__)) name ++#endif ++ + /* + * layout.h - Ntfs on-disk layout structures. Originated from the Linux-NTFS project. + * +@@ -263,14 +271,14 @@ typedef enum { + * index, that means an INDEX_ROOT and an INDEX_ALLOCATION with a name other + * than "$I30". It is unknown if it is limited to metadata files only. + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(MFT_RECORD_FLAGS, u16) { + MFT_RECORD_IN_USE = const_cpu_to_le16(0x0001), + MFT_RECORD_IS_DIRECTORY = const_cpu_to_le16(0x0002), + MFT_RECORD_IS_4 = const_cpu_to_le16(0x0004), + MFT_RECORD_IS_VIEW_INDEX = const_cpu_to_le16(0x0008), + MFT_REC_SPACE_FILLER = 0xffff, /* Just to make flags + 16-bit. */ +-} __attribute__((__packed__)) MFT_RECORD_FLAGS; ++} NTFS_PACKED_ENUM_END(MFT_RECORD_FLAGS, u16); + + /* + * mft references (aka file references or file record segment references) are +@@ -631,14 +639,14 @@ typedef struct { + /** + * enum ATTR_FLAGS - Attribute flags (16-bit). + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(ATTR_FLAGS, u16) { + ATTR_IS_COMPRESSED = const_cpu_to_le16(0x0001), + ATTR_COMPRESSION_MASK = const_cpu_to_le16(0x00ff), /* Compression + method mask. Also, first + illegal value. */ + ATTR_IS_ENCRYPTED = const_cpu_to_le16(0x4000), + ATTR_IS_SPARSE = const_cpu_to_le16(0x8000), +-} __attribute__((__packed__)) ATTR_FLAGS; ++} NTFS_PACKED_ENUM_END(ATTR_FLAGS, u16); + + /* + * Attribute compression. +@@ -710,11 +718,11 @@ typedef enum { + /** + * enum RESIDENT_ATTR_FLAGS - Flags of resident attributes (8-bit). + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(RESIDENT_ATTR_FLAGS, u8) { + RESIDENT_ATTR_IS_INDEXED = 0x01, /* Attribute is referenced in an index + (has implications for deleting and + modifying the attribute). */ +-} __attribute__((__packed__)) RESIDENT_ATTR_FLAGS; ++} NTFS_PACKED_ENUM_END(RESIDENT_ATTR_FLAGS, u8); + + /** + * struct ATTR_RECORD - Attribute record header. +@@ -759,7 +767,7 @@ typedef struct { + /* 22 */ RESIDENT_ATTR_FLAGS resident_flags; /* See above. */ + /* 23 */ s8 reservedR; /* Reserved/alignment to 8-byte + boundary. */ +-/* 24 */ void *resident_end[0]; /* Use offsetof(ATTR_RECORD, ++/* 24 */ void *resident_end[1]; /* Use offsetof(ATTR_RECORD, + resident_end) to get size of + a resident attribute. */ + } __attribute__((__packed__)); +@@ -805,17 +813,17 @@ typedef struct { + /* 56*/ sle64 initialized_size; /* Byte size of initialized + portion of the attribute value. Usually equals + data_size. */ +-/* 64 */ void *non_resident_end[0]; /* Use offsetof(ATTR_RECORD, +- non_resident_end) to get +- size of a non resident +- attribute. */ ++/* 64 */ union { ++ void *non_resident_end[1]; /* Marker for offsetof(). */ ++ sle64 compressed_size; ++ }; + /* sizeof(uncompressed attr) = 64*/ +-/* 64*/ sle64 compressed_size; /* Byte size of the attribute ++/* 64*/ /* compressed_size is overlaid above */ /* Byte size of the attribute + value after compression. Only present when + compressed. Always is a multiple of the + cluster size. Represents the actual amount of + disk space being used on the disk. */ +-/* 72 */ void *compressed_end[0]; ++/* 72 */ void *compressed_end[1]; + /* Use offsetof(ATTR_RECORD, compressed_end) to + get size of a compressed attribute. */ + /* sizeof(compressed attr) = 72*/ +@@ -828,7 +836,7 @@ typedef ATTR_RECORD ATTR_REC; + /** + * enum FILE_ATTR_FLAGS - File attribute flags (32-bit). + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(FILE_ATTR_FLAGS, u32) { + /* + * These flags are only present in the STANDARD_INFORMATION attribute + * (in the field file_attributes). +@@ -891,7 +899,7 @@ typedef enum { + * $FILE_NAME attributes. + */ + FILE_ATTR_VIEW_INDEX_PRESENT = const_cpu_to_le32(0x20000000), +-} __attribute__((__packed__)) FILE_ATTR_FLAGS; ++} NTFS_PACKED_ENUM_END(FILE_ATTR_FLAGS, u32); + + /* + * NOTE on times in NTFS: All times are in MS standard time format, i.e. they +@@ -932,7 +940,7 @@ typedef struct { + struct { + /* 36 */ u8 reserved12[12]; /* Reserved/alignment to 8-byte + boundary. */ +- /* 48 */ void *v1_end[0]; /* Marker for offsetof(). */ ++ /* 48 */ void *v1_end[1]; /* Marker for offsetof(). */ + } __attribute__((__packed__)); + /* sizeof() = 48 bytes */ + /* NTFS 3.0 */ +@@ -986,7 +994,7 @@ typedef struct { + partition. This, in contrast to disabling the + journal is a very fast process, so the user + won't even notice it. */ +- /* 72*/ void *v3_end[0]; /* Marker for offsetof(). */ ++ /* 72*/ void *v3_end[1]; /* Marker for offsetof(). */ + } __attribute__((__packed__)); + } __attribute__((__packed__)); + /* sizeof() = 72 bytes (NTFS 3.0) */ +@@ -1047,7 +1055,7 @@ typedef struct { + attribute value. */ + /* 24*/ le16 instance; /* If lowest_vcn = 0, the instance of the + attribute being referenced; otherwise 0. */ +-/* 26*/ ntfschar name[0]; /* Use when creating only. When reading use ++/* 26*/ ntfschar name[1]; /* Use when creating only. When reading use + name_offset to determine the location of the + name. */ + /* sizeof() = 26 + (attribute_name_length * 2) bytes */ +@@ -1062,7 +1070,7 @@ typedef struct { + * enum FILE_NAME_TYPE_FLAGS - Possible namespaces for filenames in ntfs. + * (8-bit). + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(FILE_NAME_TYPE_FLAGS, u8) { + FILE_NAME_POSIX = 0x00, + /* This is the largest namespace. It is case sensitive and + allows all Unicode characters except for: '\0' and '/'. +@@ -1088,7 +1096,7 @@ typedef enum { + /* 3 means that both the Win32 and the DOS filenames are + identical and hence have been saved in this single filename + record. */ +-} __attribute__((__packed__)) FILE_NAME_TYPE_FLAGS; ++} NTFS_PACKED_ENUM_END(FILE_NAME_TYPE_FLAGS, u8); + + /** + * struct FILE_NAME_ATTR - Attribute: Filename (0x30). +@@ -1141,7 +1149,7 @@ typedef struct { + /* 40*/ u8 file_name_length; /* Length of file name in + (Unicode) characters. */ + /* 41*/ FILE_NAME_TYPE_FLAGS file_name_type; /* Namespace of the file name.*/ +-/* 42*/ ntfschar file_name[0]; /* File name in Unicode. */ ++/* 42*/ ntfschar file_name[1]; /* File name in Unicode. */ + } __attribute__((__packed__)) FILE_NAME_ATTR; + + /** +@@ -1392,7 +1400,7 @@ typedef union { + * revision = 1, + * sub_authority_count = 2, + * identifier_authority = {0,0,0,0,0,5}, // SECURITY_NT_AUTHORITY +- * sub_authority[0] = 32, // SECURITY_BUILTIN_DOMAIN_RID ++ * sub_authority[1] = 32, // SECURITY_BUILTIN_DOMAIN_RID + * sub_authority[1] = 544 // DOMAIN_ALIAS_RID_ADMINS + */ + typedef struct { +@@ -1415,7 +1423,7 @@ typedef enum { + /** + * enum ACE_TYPES - The predefined ACE types (8-bit, see below). + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(ACE_TYPES, u8) { + ACCESS_MIN_MS_ACE_TYPE = 0, + ACCESS_ALLOWED_ACE_TYPE = 0, + ACCESS_DENIED_ACE_TYPE = 1, +@@ -1458,7 +1466,7 @@ typedef enum { + + /* Windows 10 and later */ + SYSTEM_PROCESS_TRUST_LABEL_ACE_TYPE = 20, +-} __attribute__((__packed__)) ACE_TYPES; ++} NTFS_PACKED_ENUM_END(ACE_TYPES, u8); + + /** + * enum ACE_FLAGS - The ACE flags (8-bit) for audit and inheritance. +@@ -1470,7 +1478,7 @@ typedef enum { + * FAILED_ACCESS_ACE_FLAG is only used with system audit and alarm ACE types + * to indicate that a message is generated (in Windows!) for failed accesses. + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(ACE_FLAGS, u8) { + /* The inheritance flags. */ + OBJECT_INHERIT_ACE = 0x01, + CONTAINER_INHERIT_ACE = 0x02, +@@ -1482,7 +1490,7 @@ typedef enum { + /* The audit flags. */ + SUCCESSFUL_ACCESS_ACE_FLAG = 0x40, + FAILED_ACCESS_ACE_FLAG = 0x80, +-} __attribute__((__packed__)) ACE_FLAGS; ++} NTFS_PACKED_ENUM_END(ACE_FLAGS, u8); + + /** + * struct ACE_HEADER - +@@ -1784,7 +1792,7 @@ typedef enum { + * and all pointer fields are expressed as offsets from the + * beginning of the security descriptor. + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(SECURITY_DESCRIPTOR_CONTROL, u16) { + SE_OWNER_DEFAULTED = const_cpu_to_le16(0x0001), + SE_GROUP_DEFAULTED = const_cpu_to_le16(0x0002), + SE_DACL_PRESENT = const_cpu_to_le16(0x0004), +@@ -1799,7 +1807,7 @@ typedef enum { + SE_SACL_PROTECTED = const_cpu_to_le16(0x2000), + SE_RM_CONTROL_VALID = const_cpu_to_le16(0x4000), + SE_SELF_RELATIVE = const_cpu_to_le16(0x8000), +-} __attribute__((__packed__)) SECURITY_DESCRIPTOR_CONTROL; ++} NTFS_PACKED_ENUM_END(SECURITY_DESCRIPTOR_CONTROL, u16); + + /** + * struct SECURITY_DESCRIPTOR_RELATIVE - +@@ -2007,13 +2015,13 @@ typedef struct { + * NOTE: Present only in FILE_Volume. + */ + typedef struct { +- ntfschar name[0]; /* The name of the volume in Unicode. */ ++ ntfschar name[1]; /* The name of the volume in Unicode. */ + } __attribute__((__packed__)) VOLUME_NAME; + + /** + * enum VOLUME_FLAGS - Possible flags for the volume (16-bit). + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(VOLUME_FLAGS, u16) { + VOLUME_IS_DIRTY = const_cpu_to_le16(0x0001), + VOLUME_RESIZE_LOG_FILE = const_cpu_to_le16(0x0002), + VOLUME_UPGRADE_ON_MOUNT = const_cpu_to_le16(0x0004), +@@ -2023,7 +2031,7 @@ typedef enum { + VOLUME_CHKDSK_UNDERWAY = const_cpu_to_le16(0x4000), + VOLUME_MODIFIED_BY_CHKDSK = const_cpu_to_le16(0x8000), + VOLUME_FLAGS_MASK = const_cpu_to_le16(0xc03f), +-} __attribute__((__packed__)) VOLUME_FLAGS; ++} NTFS_PACKED_ENUM_END(VOLUME_FLAGS, u16); + + /** + * struct VOLUME_INFORMATION - Attribute: Volume information (0x70). +@@ -2048,13 +2056,13 @@ typedef struct { + * Data contents of a file (i.e. the unnamed stream) or of a named stream. + */ + typedef struct { +- u8 data[0]; /* The file's data contents. */ ++ u8 data[1]; /* The file's data contents. */ + } __attribute__((__packed__)) DATA_ATTR; + + /** + * enum INDEX_HEADER_FLAGS - Index header flags (8-bit). + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(INDEX_HEADER_FLAGS, u8) { + /* When index header is in an index root attribute: */ + SMALL_INDEX = 0, /* The index is small enough to fit inside the + index root attribute and there is no index +@@ -2071,7 +2079,7 @@ typedef enum { + INDEX_NODE = 1, /* This node indexes other nodes, i.e. is not a + leaf node. */ + NODE_MASK = 1, /* Mask for accessing the *_NODE bits. */ +-} __attribute__((__packed__)) INDEX_HEADER_FLAGS; ++} NTFS_PACKED_ENUM_END(INDEX_HEADER_FLAGS, u8); + + /** + * struct INDEX_HEADER - +@@ -2282,7 +2290,7 @@ typedef enum { + /** + * enum INDEX_ENTRY_FLAGS - Index entry flags (16-bit). + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(INDEX_ENTRY_FLAGS, u16) { + INDEX_ENTRY_NODE = const_cpu_to_le16(1), /* This entry contains a + sub-node, i.e. a reference to an index + block in form of a virtual cluster +@@ -2292,7 +2300,7 @@ typedef enum { + entry does not represent a file but it + can point to a sub-node. */ + INDEX_ENTRY_SPACE_FILLER = 0xffff, /* Just to force 16-bit width. */ +-} __attribute__((__packed__)) INDEX_ENTRY_FLAGS; ++} NTFS_PACKED_ENUM_END(INDEX_ENTRY_FLAGS, u16); + + /** + * struct INDEX_ENTRY_HEADER - This the index entry header (see below). +@@ -2395,7 +2403,7 @@ typedef struct { + * number of clusters in the index allocation attribute. + */ + typedef struct { +- u8 bitmap[0]; /* Array of bits. */ ++ u8 bitmap[1]; /* Array of bits. */ + } __attribute__((__packed__)) BITMAP_ATTR; + + /** +@@ -2469,7 +2477,7 @@ typedef struct { + le32 reparse_tag; /* Reparse point type (inc. flags). */ + le16 reparse_data_length; /* Byte size of reparse data. */ + le16 reserved; /* Align to 8-byte boundary. */ +- u8 reparse_data[0]; /* Meaning depends on reparse_tag. */ ++ u8 reparse_data[1]; /* Meaning depends on reparse_tag. */ + } __attribute__((__packed__)) REPARSE_POINT; + + /** +@@ -2492,12 +2500,12 @@ typedef struct { + /** + * enum EA_FLAGS - Extended attribute flags (8-bit). + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(EA_FLAGS, u8) { + NEED_EA = 0x80, /* Indicate that the file to which the EA + belongs cannot be interpreted without + understanding the associated extended + attributes. */ +-} __attribute__((__packed__)) EA_FLAGS; ++} NTFS_PACKED_ENUM_END(EA_FLAGS, u8); + + /** + * struct EA_ATTR - Attribute: Extended attribute (EA) (0xe0). +@@ -2514,8 +2522,8 @@ typedef struct { + u8 name_length; /* Length of the name of the extended + attribute in bytes. */ + le16 value_length; /* Byte size of the EA's value. */ +- u8 name[0]; /* Name of the EA. */ +- u8 value[0]; /* The value of the EA. Immediately ++ u8 name[1]; /* Name of the EA. */ ++ u8 value[1]; /* The value of the EA. Immediately + follows the name. */ + } __attribute__((__packed__)) EA_ATTR; + +@@ -2526,7 +2534,7 @@ typedef struct { + * NTFS 3.0 during beta testing. + */ + typedef struct { +- /* Irrelevant as feature unused. */ ++ u8 _ntlite_unused; + } __attribute__((__packed__)) PROPERTY_SET; + + /** +@@ -2541,7 +2549,7 @@ typedef struct { + * attribute with the name $EFS. See below for the relevant structures. + */ + typedef struct { +- /* Can be anything the creator chooses. */ ++ u8 _ntlite_unused; + } __attribute__((__packed__)) LOGGED_UTILITY_STREAM; + + /* +@@ -2684,6 +2692,15 @@ typedef struct { + + typedef EFS_DF_CERTIFICATE_THUMBPRINT_HEADER EFS_DF_CERT_THUMBPRINT_HEADER; + ++#ifdef _MSC_VER ++typedef le64 INTX_FILE_TYPES; ++#define INTX_SYMBOLIC_LINK \ ++ ((INTX_FILE_TYPES)const_cpu_to_le64(0x014B4E4C78746E49ULL)) /* "IntxLNK\1" */ ++#define INTX_CHARACTER_DEVICE \ ++ ((INTX_FILE_TYPES)const_cpu_to_le64(0x0052484378746E49ULL)) /* "IntxCHR\0" */ ++#define INTX_BLOCK_DEVICE \ ++ ((INTX_FILE_TYPES)const_cpu_to_le64(0x004B4C4278746E49ULL)) /* "IntxBLK\0" */ ++#else + typedef enum { + INTX_SYMBOLIC_LINK = + const_cpu_to_le64(0x014B4E4C78746E49ULL), /* "IntxLNK\1" */ +@@ -2692,6 +2709,7 @@ typedef enum { + INTX_BLOCK_DEVICE = + const_cpu_to_le64(0x004B4C4278746E49ULL), /* "IntxBLK\0" */ + } INTX_FILE_TYPES; ++#endif + + typedef struct { + INTX_FILE_TYPES magic; /* Intx file magic. */ +@@ -2700,15 +2718,15 @@ typedef struct { + struct { + le64 major; /* Major device number. */ + le64 minor; /* Minor device number. */ +- void *device_end[0]; /* Marker for offsetof(). */ ++ void *device_end[1]; /* Marker for offsetof(). */ + } __attribute__((__packed__)); + /* For symbolic links. */ +- ntfschar target[0]; ++ ntfschar target[1]; + } __attribute__((__packed__)); + } __attribute__((__packed__)) INTX_FILE; + + #endif /* defined _NTFS_LAYOUT_H */ +- +-#ifdef _MSC_VER +-#pragma pack(pop) +-#endif ++ ++#ifdef _MSC_VER ++#pragma pack(pop) ++#endif +diff --git a/include/ntfs-3g/logfile.h b/include/ntfs-3g/logfile.h +index d1f1222..97c99c4 100644 +--- a/include/ntfs-3g/logfile.h ++++ b/include/ntfs-3g/logfile.h +@@ -1,3 +1,6 @@ ++#ifdef _MSC_VER ++#pragma pack(push, 1) ++#endif + /* + * logfile.h - Exports for $LogFile handling. Originated from the Linux-NTFS project. + * +@@ -335,7 +338,7 @@ typedef struct { + * + * (Or is it log record pages?) + */ +-typedef enum { ++NTFS_PACKED_ENUM_BEGIN(LOG_RECORD_FLAGS, u16) { + LOG_RECORD_MULTI_PAGE = const_cpu_to_le16(0x0001), /* ??? */ + /* The flags below were introduced in Windows 10 */ + LOG_RECORD_DELETING = const_cpu_to_le16(0x0002), +@@ -343,7 +346,7 @@ typedef enum { + LOG_RECORD_SIZE_PLACE_HOLDER = 0xffff, + /* This has nothing to do with the log record. It is only so + gcc knows to make the flags 16-bit. */ +-} __attribute__((__packed__)) LOG_RECORD_FLAGS; ++} NTFS_PACKED_ENUM_END(LOG_RECORD_FLAGS, u16); + + /** + * struct LOG_CLIENT_ID - The log client id structure identifying a log client. +@@ -413,7 +416,7 @@ typedef struct { + ATTRIBUTE_FLAGS attribute_flags; + leVCN target_vcn; + /* Now at ofs 0x50. */ +- leLCN lcn_list[0]; /* Only present if lcns_to_follow ++ leLCN lcn_list[1]; /* Only present if lcns_to_follow + is not 0. */ + } __attribute__((__packed__)); + struct { +@@ -421,7 +424,7 @@ typedef struct { + leLSN attributes_lsn; + leLSN names_lsn; + leLSN dirty_pages_lsn; +- le64 unknown_list[0]; ++ le64 unknown_list[1]; + } __attribute__((__packed__)); + } __attribute__((__packed__)); + } __attribute__((__packed__)) LOG_RECORD; +@@ -468,3 +471,7 @@ extern BOOL ntfs_is_logfile_clean(ntfs_attr *log_na, RESTART_PAGE_HEADER *rp); + extern int ntfs_empty_logfile(ntfs_attr *na); + + #endif /* defined _NTFS_LOGFILE_H */ ++ ++#ifdef _MSC_VER ++#pragma pack(pop) ++#endif +diff --git a/include/ntfs-3g/logging.h b/include/ntfs-3g/logging.h +index 82f39fe..8c9538c 100644 +--- a/include/ntfs-3g/logging.h ++++ b/include/ntfs-3g/logging.h +@@ -87,32 +87,28 @@ int ntfs_log_redirect(const char *function, const char *file, int line, + #define NTFS_LOG_FLAG_FUNCTION (1 << 3) /* Show the function name containing the message */ + #define NTFS_LOG_FLAG_ONLYNAME (1 << 4) /* Only display the filename, not the pathname */ + +-/* Macros to simplify logging. One for each level defined above. +- * Note, ntfs_log_debug/trace have effect only if DEBUG is defined. +- */ +-#define ntfs_log_critical(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_CRITICAL,NULL,FORMAT,##ARGS) +-#define ntfs_log_error(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_ERROR,NULL,FORMAT,##ARGS) +-#define ntfs_log_info(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_INFO,NULL,FORMAT,##ARGS) +-#define ntfs_log_perror(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_PERROR,NULL,FORMAT,##ARGS) +-#define ntfs_log_progress(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_PROGRESS,NULL,FORMAT,##ARGS) +-#define ntfs_log_quiet(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_QUIET,NULL,FORMAT,##ARGS) +-#define ntfs_log_verbose(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_VERBOSE,NULL,FORMAT,##ARGS) +-#define ntfs_log_warning(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_WARNING,NULL,FORMAT,##ARGS) +- +-/* By default debug and trace messages are compiled into the program, +- * but not displayed. +- */ ++/* Macros to simplify logging. Use standard C99 variadic macros so this ++ * header also parses with MSVC's C front-end. */ ++#define ntfs_log_critical(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_CRITICAL,NULL,__VA_ARGS__) ++#define ntfs_log_error(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_ERROR,NULL,__VA_ARGS__) ++#define ntfs_log_info(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_INFO,NULL,__VA_ARGS__) ++#define ntfs_log_perror(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_PERROR,NULL,__VA_ARGS__) ++#define ntfs_log_progress(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_PROGRESS,NULL,__VA_ARGS__) ++#define ntfs_log_quiet(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_QUIET,NULL,__VA_ARGS__) ++#define ntfs_log_verbose(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_VERBOSE,NULL,__VA_ARGS__) ++#define ntfs_log_warning(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_WARNING,NULL,__VA_ARGS__) ++ + #ifdef DEBUG +-#define ntfs_log_debug(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_DEBUG,NULL,FORMAT,##ARGS) +-#define ntfs_log_trace(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_TRACE,NULL,FORMAT,##ARGS) +-#define ntfs_log_enter(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_ENTER,NULL,FORMAT,##ARGS) +-#define ntfs_log_leave(FORMAT, ARGS...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_LEAVE,NULL,FORMAT,##ARGS) ++#define ntfs_log_debug(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_DEBUG,NULL,__VA_ARGS__) ++#define ntfs_log_trace(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_TRACE,NULL,__VA_ARGS__) ++#define ntfs_log_enter(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_ENTER,NULL,__VA_ARGS__) ++#define ntfs_log_leave(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_LEAVE,NULL,__VA_ARGS__) + #else +-#define ntfs_log_debug(FORMAT, ARGS...)do {} while (0) +-#define ntfs_log_trace(FORMAT, ARGS...)do {} while (0) +-#define ntfs_log_enter(FORMAT, ARGS...)do {} while (0) +-#define ntfs_log_leave(FORMAT, ARGS...)do {} while (0) +-#endif /* DEBUG */ ++#define ntfs_log_debug(...) do {} while (0) ++#define ntfs_log_trace(...) do {} while (0) ++#define ntfs_log_enter(...) do {} while (0) ++#define ntfs_log_leave(...) do {} while (0) ++#endif + + void ntfs_log_early_error(const char *format, ...) + __attribute__((format(printf, 1, 2))); +diff --git a/include/ntfs-3g/param.h b/include/ntfs-3g/param.h +index ebc8a0a..231dc76 100644 +--- a/include/ntfs-3g/param.h ++++ b/include/ntfs-3g/param.h +@@ -22,11 +22,21 @@ + #ifndef _NTFS_PARAM_H + #define _NTFS_PARAM_H + ++#ifndef CACHE_INODE_SIZE + #define CACHE_INODE_SIZE 32 /* inode cache, zero or >= 3 and not too big */ ++#endif ++#ifndef CACHE_NIDATA_SIZE + #define CACHE_NIDATA_SIZE 64 /* idata cache, zero or >= 3 and not too big */ ++#endif ++#ifndef CACHE_LOOKUP_SIZE + #define CACHE_LOOKUP_SIZE 64 /* lookup cache, zero or >= 3 and not too big */ ++#endif ++#ifndef CACHE_SECURID_SIZE + #define CACHE_SECURID_SIZE 16 /* securid cache, zero or >= 3 and not too big */ ++#endif ++#ifndef CACHE_LEGACY_SIZE + #define CACHE_LEGACY_SIZE 8 /* legacy cache size, zero or >= 3 and not too big */ ++#endif + + #define FORCE_FORMAT_v1x 0 /* Insert security data as in NTFS v1.x */ + #define OWNERFROMACL 1 /* Get the owner from ACL (not Windows owner) */ +diff --git a/include/ntfs-3g/security.h b/include/ntfs-3g/security.h +index c76ce9f..b747b61 100644 +--- a/include/ntfs-3g/security.h ++++ b/include/ntfs-3g/security.h +@@ -74,7 +74,7 @@ struct CACHED_PERMISSIONS_LEGACY { + struct CACHED_PERMISSIONS_LEGACY *previous; + void *variable; + size_t varsize; +- union ALIGNMENT payload[0]; ++ /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + u64 mft_no; + struct CACHED_PERMISSIONS perm; +@@ -89,7 +89,7 @@ struct CACHED_SECURID { + struct CACHED_SECURID *previous; + void *variable; + size_t varsize; +- union ALIGNMENT payload[0]; ++ /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + uid_t uid; + gid_t gid; +@@ -164,7 +164,7 @@ struct POSIX_ACL { + u8 version; + u8 flags; + u16 filler; +- struct POSIX_ACE ace[0]; ++ struct POSIX_ACE ace[1]; + } __attribute__((__packed__)); + + struct POSIX_SECURITY { +diff --git a/include/ntfs-3g/support.h b/include/ntfs-3g/support.h +index 6af4761..eadc08d 100644 +--- a/include/ntfs-3g/support.h ++++ b/include/ntfs-3g/support.h +@@ -67,19 +67,13 @@ + #define set_bit(bit, var) (var) |= 1 << (bit) + #define clear_bit(bit, var) (var) &= ~(1 << (bit)) + +-#define test_and_set_bit(bit, var) \ +-({ \ +- const BOOL old_state = test_bit(bit, var); \ +- set_bit(bit, var); \ +- old_state; \ +-}) ++#define test_and_set_bit(bit, var) \ ++ (test_bit((bit), (var)) ? (set_bit((bit), (var)), TRUE) : \ ++ (set_bit((bit), (var)), FALSE)) + +-#define test_and_clear_bit(bit, var) \ +-({ \ +- const BOOL old_state = test_bit(bit, var); \ +- clear_bit(bit, var); \ +- old_state; \ +-}) ++#define test_and_clear_bit(bit, var) \ ++ (test_bit((bit), (var)) ? (clear_bit((bit), (var)), TRUE) : \ ++ (clear_bit((bit), (var)), FALSE)) + + #endif /* defined _NTFS_SUPPORT_H */ + +diff --git a/libntfs-3g/attrib.c b/libntfs-3g/attrib.c +index 35da32e..b1ab81e 100644 +--- a/libntfs-3g/attrib.c ++++ b/libntfs-3g/attrib.c +@@ -6906,7 +6906,7 @@ static int stuff_hole(ntfs_attr *na, const s64 pos) + if (!ret && NAttrNonResident(na)) { + /* does the hole span over several compression block ? */ + if ((pos ^ na->initialized_size) +- & ~(na->compression_block_size - 1)) { ++ & ~((s64)na->compression_block_size - 1)) { + begin_size = ((na->initialized_size - 1) + | (na->compression_block_size - 1)) + + 1 - na->initialized_size; +diff --git a/libntfs-3g/attrlist.c b/libntfs-3g/attrlist.c +index 29d4085..0cb8473 100644 +--- a/libntfs-3g/attrlist.c ++++ b/libntfs-3g/attrlist.c +@@ -135,7 +135,7 @@ int ntfs_attrlist_entry_add(ntfs_inode *ni, ATTR_RECORD *attr) + } + + /* Determine size and allocate memory for new attribute list. */ +- entry_len = (sizeof(ATTR_LIST_ENTRY) + sizeof(ntfschar) * ++ entry_len = (offsetof(ATTR_LIST_ENTRY, name) + sizeof(ntfschar) * + attr->name_length + 7) & ~7; + new_al = ntfs_calloc(ni->attr_list_size + entry_len); + if (!new_al) +diff --git a/libntfs-3g/cache.c b/libntfs-3g/cache.c +index 2ad8d35..e577b52 100644 +--- a/libntfs-3g/cache.c ++++ b/libntfs-3g/cache.c +@@ -483,7 +483,7 @@ static struct CACHE_HEADER *ntfs_create_cache(const char *name, + size_t size; + int i; + +- size = sizeof(struct CACHE_HEADER) + item_count*full_item_size; ++ size = offsetof(struct CACHE_HEADER, entry) + item_count*full_item_size; + if (max_hash) + size += item_count*sizeof(struct HASH_ENTRY) + + max_hash*sizeof(struct HASH_ENTRY*); +@@ -499,7 +499,7 @@ static struct CACHE_HEADER *ntfs_create_cache(const char *name, + cache->dohash = (cache_hash)NULL; + cache->max_hash = 0; + } +- cache->fixed_size = full_item_size - sizeof(struct CACHED_GENERIC); ++ cache->fixed_size = full_item_size - offsetof(struct CACHED_GENERIC, payload); + cache->reads = 0; + cache->writes = 0; + cache->hits = 0; +diff --git a/libntfs-3g/dir.c b/libntfs-3g/dir.c +index 5cab5c5..ad82c6f 100644 +--- a/libntfs-3g/dir.c ++++ b/libntfs-3g/dir.c +@@ -841,10 +841,6 @@ static const ntfschar dotdot[3] = { const_cpu_to_le16('.'), + * union index_union - + * More helpers for ntfs_readdir(). + */ +-typedef union { +- INDEX_ROOT *ir; +- INDEX_ALLOCATION *ia; +-} index_union __attribute__((__transparent_union__)); + + /** + * enum INDEX_TYPE - +@@ -957,7 +953,7 @@ static u32 ntfs_dir_entry_type(ntfs_inode *dir_ni, MFT_REF mref, + * callback. + */ + static int ntfs_filldir(ntfs_inode *dir_ni, s64 *pos, u8 ivcn_bits, +- const INDEX_TYPE index_type, index_union iu, INDEX_ENTRY *ie, ++ const INDEX_TYPE index_type, void *index_block, INDEX_ENTRY *ie, + void *dirent, ntfs_filldir_t filldir) + { + FILE_NAME_ATTR *fn = &ie->key.file_name; +@@ -971,11 +967,11 @@ static int ntfs_filldir(ntfs_inode *dir_ni, s64 *pos, u8 ivcn_bits, + + /* Advance the position even if going to skip the entry. */ + if (index_type == INDEX_TYPE_ALLOCATION) +- *pos = (u8*)ie - (u8*)iu.ia + (sle64_to_cpu( +- iu.ia->index_block_vcn) << ivcn_bits) + ++ *pos = (u8*)ie - (u8*)(INDEX_ALLOCATION*)index_block + (sle64_to_cpu( ++ ((INDEX_ALLOCATION*)index_block)->index_block_vcn) << ivcn_bits) + + dir_ni->vol->mft_record_size; + else /* if (index_type == INDEX_TYPE_ROOT) */ +- *pos = (u8*)ie - (u8*)iu.ir; ++ *pos = (u8*)ie - (u8*)(INDEX_ROOT*)index_block; + mref = le64_to_cpu(ie->indexed_file); + metadata = (MREF(mref) != FILE_root) && (MREF(mref) < FILE_first_user); + /* Skip root directory self reference entry. */ +@@ -1500,7 +1496,7 @@ static ntfs_inode *__ntfs_create(ntfs_inode *dir_ni, le32 securid, + * Write STANDARD_INFORMATION v1.2 (no inheritance) or v3 + */ + if (securid) +- si_len = sizeof(STANDARD_INFORMATION); ++ si_len = offsetof(STANDARD_INFORMATION, v3_end); + else + si_len = offsetof(STANDARD_INFORMATION, v1_end); + si = ntfs_calloc(si_len); +@@ -1686,7 +1682,7 @@ static ntfs_inode *__ntfs_create(ntfs_inode *dir_ni, le32 securid, + free(data); + } + /* Create FILE_NAME attribute. */ +- fn_len = sizeof(FILE_NAME_ATTR) + name_len * sizeof(ntfschar); ++ fn_len = offsetof(FILE_NAME_ATTR, file_name) + name_len * sizeof(ntfschar); + fn = ntfs_calloc(fn_len); + if (!fn) { + err = errno; +@@ -2223,7 +2219,7 @@ static int ntfs_link_i(ntfs_inode *ni, ntfs_inode *dir_ni, const ntfschar *name, + } + + /* Create FILE_NAME attribute. */ +- fn_len = sizeof(FILE_NAME_ATTR) + name_len * sizeof(ntfschar); ++ fn_len = offsetof(FILE_NAME_ATTR, file_name) + name_len * sizeof(ntfschar); + fn = ntfs_calloc(fn_len); + if (!fn) { + err = errno; +diff --git a/libntfs-3g/index.c b/libntfs-3g/index.c +index 7b88c18..6de0e35 100644 +--- a/libntfs-3g/index.c ++++ b/libntfs-3g/index.c +@@ -1704,7 +1704,7 @@ int ntfs_index_add_filename(ntfs_inode *ni, FILE_NAME_ATTR *fn, MFT_REF mref) + } + + fn_size = (fn->file_name_length * sizeof(ntfschar)) + +- sizeof(FILE_NAME_ATTR); ++ offsetof(FILE_NAME_ATTR, file_name); + ie_size = (sizeof(INDEX_ENTRY_HEADER) + fn_size + 7) & ~7; + + ie = ntfs_calloc(ie_size); +diff --git a/libntfs-3g/inode.c b/libntfs-3g/inode.c +index 8819ff1..dc17a4c 100644 +--- a/libntfs-3g/inode.c ++++ b/libntfs-3g/inode.c +@@ -857,7 +857,7 @@ static int ntfs_inode_sync_file_name(ntfs_inode *ni, ntfs_inode *dir_ni) + err = errno; + continue; + } +- if (ntfs_index_lookup(fn, sizeof(FILE_NAME_ATTR), ictx)) { ++ if (ntfs_index_lookup(fn, offsetof(FILE_NAME_ATTR, file_name), ictx)) { + if (!err) { + if (errno == ENOENT) + err = EIO; +@@ -1148,7 +1148,7 @@ int ntfs_inode_add_attrlist(ntfs_inode *ni) + goto put_err_out; + } + +- ale_size = (sizeof(ATTR_LIST_ENTRY) + sizeof(ntfschar) * ++ ale_size = (offsetof(ATTR_LIST_ENTRY, name) + sizeof(ntfschar) * + ctx->attr->name_length + 7) & ~7; + al_len += ale_size; + +@@ -1165,7 +1165,7 @@ int ntfs_inode_add_attrlist(ntfs_inode *ni) + + /* Add attribute to attribute list. */ + ale->type = ctx->attr->type; +- ale->length = cpu_to_le16((sizeof(ATTR_LIST_ENTRY) + ++ ale->length = cpu_to_le16((offsetof(ATTR_LIST_ENTRY, name) + + sizeof(ntfschar) * ctx->attr->name_length + 7) & ~7); + ale->name_length = ctx->attr->name_length; + ale->name_offset = (u8 *)ale->name - (u8 *)ale; +diff --git a/libntfs-3g/ioctl.c b/libntfs-3g/ioctl.c +index b059a53..be13ede 100644 +--- a/libntfs-3g/ioctl.c ++++ b/libntfs-3g/ioctl.c +@@ -409,7 +409,11 @@ int ntfs_ioctl(ntfs_inode *ni, unsigned long cmd, + } + break; + #else ++#ifdef _MSC_VER ++#pragma message("ntfs-3g: trimming not supported (FITRIM or BLKDISCARD not defined)") ++#else + #warning Trimming not supported : FITRIM or BLKDISCARD not defined ++#endif + #endif + default : + ret = -EINVAL; +diff --git a/libntfs-3g/reparse.c b/libntfs-3g/reparse.c +index 54858ca..fca18a6 100644 +--- a/libntfs-3g/reparse.c ++++ b/libntfs-3g/reparse.c +@@ -66,7 +66,7 @@ struct MOUNT_POINT_REPARSE_DATA { /* reparse data for junctions */ + le16 subst_name_length; + le16 print_name_offset; + le16 print_name_length; +- char path_buffer[0]; /* above data assume this is char array */ ++ char path_buffer[1]; /* above data assume this is char array */ + } ; + + struct SYMLINK_REPARSE_DATA { /* reparse data for symlinks */ +@@ -75,12 +75,12 @@ struct SYMLINK_REPARSE_DATA { /* reparse data for symlinks */ + le16 print_name_offset; + le16 print_name_length; + le32 flags; /* 1 for full target, otherwise 0 */ +- char path_buffer[0]; /* above data assume this is char array */ ++ char path_buffer[1]; /* above data assume this is char array */ + } ; + + struct WSL_LINK_REPARSE_DATA { + le32 type; +- char link[0]; ++ char link[1]; + } ; + + struct REPARSE_INDEX { /* index entry in $Extend/$Reparse */ +diff --git a/libntfs-3g/security.c b/libntfs-3g/security.c +index d0e030e..6e6dc14 100644 +--- a/libntfs-3g/security.c ++++ b/libntfs-3g/security.c +@@ -1017,7 +1017,7 @@ static int update_secur_descr(ntfs_volume *vol, + if (!test_nino_flag(ni, v3_Extensions)) { + /* expand standard information attribute to v3.x */ + res = ntfs_attr_truncate(na, +- (s64)sizeof(STANDARD_INFORMATION)); ++ (s64)offsetof(STANDARD_INFORMATION, v3_end)); + ni->owner_id = const_cpu_to_le32(0); + ni->quota_charged = const_cpu_to_le64(0); + ni->usn = const_cpu_to_le64(0); +@@ -1091,7 +1091,7 @@ static int upgrade_secur_desc(ntfs_volume *vol, + if (na) { + /* expand standard information attribute to v3.x */ + res = ntfs_attr_truncate(na, +- (s64)sizeof(STANDARD_INFORMATION)); ++ (s64)offsetof(STANDARD_INFORMATION, v3_end)); + ni->owner_id = const_cpu_to_le32(0); + ni->quota_charged = const_cpu_to_le64(0); + ni->usn = const_cpu_to_le64(0); +diff --git a/libntfs-3g/xattrs.c b/libntfs-3g/xattrs.c +index 4e410de..97adb7d 100644 +--- a/libntfs-3g/xattrs.c ++++ b/libntfs-3g/xattrs.c +@@ -75,7 +75,7 @@ struct LE_POSIX_ACL { + u8 version; + u8 flags; + le16 filler; +- struct LE_POSIX_ACE ace[0]; ++ struct LE_POSIX_ACE ace[1]; + } __attribute__((__packed__)); + + #endif diff --git a/code/drivers/fs/ntfs/ntfs3g/ntfs3g_port.c b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_port.c new file mode 100644 index 0000000..e9f71b6 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_port.c @@ -0,0 +1,306 @@ +/* Small CRT/POSIX compatibility surface used by libntfs-3g on NTLite. */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +static int gNtfs3gErrno; +int _fltused; /* MSVC floating-point helper anchor required by printf.obj */ + +int* __cdecl _errno(void) { return &gNtfs3gErrno; } + +static FILE gNtfs3gStderr; +static FILE gNtfs3gStdout; +static FILE gNtfs3gStdin; +FILE* stderr = &gNtfs3gStderr; +FILE* stdout = &gNtfs3gStdout; +FILE* stdin = &gNtfs3gStdin; + +void* malloc(size_t size) { + if (!size) size = 1; + return MmAllocatePool(size); +} + +void* calloc(size_t count, size_t size) { + if (count && size > SIZE_MAX / count) { + errno = ENOMEM; + return NULL; + } + size_t total = count * size; + if (!total) total = 1; + void* p = MmAllocatePool(total); + if (!p) { + errno = ENOMEM; + return NULL; + } + memset(p, 0, total); + return p; +} + +void* realloc(void* ptr, size_t size) { + if (!ptr) return malloc(size); + if (!size) { + MmFreePool(ptr); + return NULL; + } + void* p = MmReallocatePool(ptr, size); + if (!p) errno = ENOMEM; + return p; +} + +void free(void* ptr) { + if (ptr) MmFreePool(ptr); +} + +char* strdup(const char* text) { + if (!text) return NULL; + size_t n = strlen(text) + 1; + char* p = (char*)malloc(n); + if (p) memcpy(p, text, n); + return p; +} + +char* strndup(const char* text, size_t limit) { + if (!text) return NULL; + size_t n = 0; + while (n < limit && text[n]) ++n; + char* p = (char*)malloc(n + 1); + if (!p) return NULL; + memcpy(p, text, n); + p[n] = '\0'; + return p; +} + +void qsort(void* base, size_t count, size_t size, + int (*compare)(const void*, const void*)) { + if (!base || !compare || size == 0 || count < 2) return; + unsigned char* bytes = (unsigned char*)base; + for (size_t i = 1; i < count; ++i) { + size_t j = i; + while (j) { + unsigned char* a = bytes + (j - 1) * size; + unsigned char* b = bytes + j * size; + if (compare(a, b) <= 0) break; + for (size_t k = 0; k < size; ++k) { + unsigned char t = a[k]; a[k] = b[k]; b[k] = t; + } + --j; + } + } +} + +void* bsearch(const void* key, const void* base, size_t count, size_t size, + int (*compare)(const void*, const void*)) { + if (!key || !base || !compare || !size) return NULL; + size_t lo = 0, hi = count; + const unsigned char* bytes = (const unsigned char*)base; + while (lo < hi) { + size_t mid = lo + (hi - lo) / 2; + const void* item = bytes + mid * size; + int c = compare(key, item); + if (c == 0) return (void*)item; + if (c < 0) hi = mid; else lo = mid + 1; + } + return NULL; +} + +int abs(int value) { return value < 0 ? -value : value; } +long labs(long value) { return value < 0 ? -value : value; } +long long llabs(long long value) { return value < 0 ? -value : value; } + +static int Ntfs3gDigit(int c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'z') return c - 'a' + 10; + if (c >= 'A' && c <= 'Z') return c - 'A' + 10; + return -1; +} + +static unsigned long long Ntfs3gParseUnsigned(const char* text, char** end, int base, bool* negative) { + const char* p = text; + while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') ++p; + *negative = false; + if (*p == '+' || *p == '-') { *negative = (*p == '-'); ++p; } + if (base == 0) { + if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { base = 16; p += 2; } + else if (p[0] == '0') base = 8; + else base = 10; + } else if (base == 16 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) { + p += 2; + } + unsigned long long value = 0; + const char* first = p; + for (;;) { + int d = Ntfs3gDigit((unsigned char)*p); + if (d < 0 || d >= base) break; + value = value * (unsigned)base + (unsigned)d; + ++p; + } + if (end) *end = (char*)(p == first ? text : p); + return value; +} + +long strtol(const char* text, char** end, int base) { + bool neg; unsigned long long v = Ntfs3gParseUnsigned(text, end, base, &neg); + return neg ? -(long)v : (long)v; +} +unsigned long strtoul(const char* text, char** end, int base) { + bool neg; unsigned long long v = Ntfs3gParseUnsigned(text, end, base, &neg); + return neg ? (unsigned long)(0 - (unsigned long)v) : (unsigned long)v; +} +long long strtoll(const char* text, char** end, int base) { + bool neg; unsigned long long v = Ntfs3gParseUnsigned(text, end, base, &neg); + return neg ? -(long long)v : (long long)v; +} +unsigned long long strtoull(const char* text, char** end, int base) { + bool neg; unsigned long long v = Ntfs3gParseUnsigned(text, end, base, &neg); + return neg ? 0ull - v : v; +} + +char* strerror(int error) { + switch (error) { + case 0: return "success"; + case EPERM: return "operation not permitted"; + case ENOENT: return "no such file or directory"; + case EIO: return "I/O error"; + case ENOMEM: return "out of memory"; + case EACCES: return "permission denied"; + case EBUSY: return "device or resource busy"; + case EEXIST: return "file exists"; + case ENODEV: return "no such device"; + case ENOTDIR: return "not a directory"; + case EISDIR: return "is a directory"; + case EINVAL: return "invalid argument"; + case ENOSPC: return "no space left on device"; + case EROFS: return "read-only filesystem"; + case ENOTEMPTY: return "directory not empty"; + case EOVERFLOW: return "value too large"; + case ENOTSUP: return "operation not supported"; + default: return "ntfs-3g error"; + } +} + +char* getenv(const char* name) { (void)name; return NULL; } +char* __cdecl setlocale(int category, const char* locale) { (void)category; (void)locale; return "C"; } + +uid_t getuid(void) { return 0; } +uid_t geteuid(void) { return 0; } +gid_t getgid(void) { return 0; } +gid_t getegid(void) { return 0; } +int fsync(int fd) { (void)fd; return 0; } + +int strcasecmp(const char* a, const char* b) { + while (*a && *b) { + unsigned char ca = (unsigned char)*a++, cb = (unsigned char)*b++; + if (ca >= 'A' && ca <= 'Z') ca += 'a' - 'A'; + if (cb >= 'A' && cb <= 'Z') cb += 'a' - 'A'; + if (ca != cb) return (int)ca - (int)cb; + } + return (unsigned char)*a - (unsigned char)*b; +} + +int strncasecmp(const char* a, const char* b, size_t count) { + while (count--) { + unsigned char ca = (unsigned char)*a++, cb = (unsigned char)*b++; + if (ca >= 'A' && ca <= 'Z') ca += 'a' - 'A'; + if (cb >= 'A' && cb <= 'Z') cb += 'a' - 'A'; + if (ca != cb || !ca || !cb) return (int)ca - (int)cb; + } + return 0; +} + +time_t __cdecl time(time_t* out) { + time_t now = (time_t)KeQueryUnixTimeSeconds(); + if (out) *out = now; + return now; +} + +static int Ntfs3gLogV(const char* format, va_list args) { + char buffer[512]; + int n = vsnprintf(buffer, sizeof(buffer), format, args); + buffer[sizeof(buffer) - 1] = '\0'; + DbgPrint("ntfs3g: %s", buffer); + return n; +} + +int vfprintf(FILE* stream, const char* format, va_list args) { + (void)stream; + return Ntfs3gLogV(format, args); +} + +int fprintf(FILE* stream, const char* format, ...) { + va_list args; + va_start(args, format); + int n = vfprintf(stream, format, args); + va_end(args); + return n; +} + +int fflush(FILE* stream) { (void)stream; return 0; } + +int fputs(const char* text, FILE* stream) { + (void)stream; + if (!text) return -1; + DbgPrint("ntfs3g: %s", text); + return (int)strlen(text); +} + +int puts(const char* text) { + int n = fputs(text, stdout); + DbgPrint("\n"); + return n; +} + +void abort(void) { + DbgPrint("ntfs3g: abort()\n"); + for (;;) { } +} + + +/* Minimal process/account/syslog compatibility used by libntfs-3g internals. + * The kernel driver never intentionally executes the daemon/process paths. */ +struct passwd; +struct group; +struct passwd* getpwnam(const char* name) { (void)name; return (struct passwd*)0; } +struct passwd* getpwuid(uid_t uid) { (void)uid; return (struct passwd*)0; } +struct group* getgrnam(const char* name) { (void)name; return (struct group*)0; } +struct group* getgrgid(gid_t gid) { (void)gid; return (struct group*)0; } +int fork(void) { errno = ENOSYS; return -1; } +void _exit(int status) { (void)status; abort(); } +pid_t setsid(void) { errno = ENOSYS; return (pid_t)-1; } +int chdir(const char* path) { (void)path; errno = ENOSYS; return -1; } +int open(const char* path, int flags, ...) { (void)path; (void)flags; errno = ENOSYS; return -1; } +int dup2(int oldfd, int newfd) { (void)oldfd; (void)newfd; errno = ENOSYS; return -1; } +int close(int fd) { (void)fd; errno = ENOSYS; return -1; } +void openlog(const char* ident, int option, int facility) { (void)ident; (void)option; (void)facility; } +void closelog(void) {} +void vsyslog(int priority, const char* format, va_list args) { + (void)priority; + char buffer[512]; + vsnprintf(buffer, sizeof(buffer), format, args); + DbgPrint("ntfs3g: %s", buffer); +} +void syslog(int priority, const char* format, ...) { + va_list args; + va_start(args, format); + vsyslog(priority, format, args); + va_end(args); +} + +static unsigned long ntfs3g_rand_state = 1; +void srandom(unsigned int seed) { ntfs3g_rand_state = seed ? seed : 1; } +long random(void) { ntfs3g_rand_state = ntfs3g_rand_state * 1103515245u + 12345u; return (long)((ntfs3g_rand_state >> 1) & 0x7fffffff); } +pid_t getpid(void) { return 1; } +ssize_t read(int fd, void* buffer, size_t count) { (void)fd; (void)buffer; (void)count; errno = ENOSYS; return -1; } + +int atoi(const char* text) { return (int)strtol(text, (char**)0, 10); } diff --git a/code/drivers/fs/ntfs/ntfs3g/ntfs3g_sources.txt b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_sources.txt new file mode 100644 index 0000000..7aa66ab --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/ntfs3g_sources.txt @@ -0,0 +1,36 @@ +# NTLite port + upstream libntfs-3g. Run vendor_ntfs3g.ps1 first. +drivers/fs/ntfs/ntfs3g/ntfs3g_port.c +drivers/fs/ntfs/ntfs3g/ntfs3g_device.c +drivers/fs/ntfs/ntfs3g/ntfs3g_driver.c + +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/acls.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/attrib.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/attrlist.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/bitmap.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/bootsect.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/cache.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/collate.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/compat.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/compress.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/debug.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/device.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/dir.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/ea.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/efs.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/index.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/inode.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/ioctl.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/lcnalloc.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/logfile.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/logging.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/mft.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/misc.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/mst.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/object_id.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/realpath.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/reparse.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/runlist.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/security.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/unistr.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/volume.c +drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/xattrs.c diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/AUTHORS b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/AUTHORS new file mode 100644 index 0000000..f9299b4 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/AUTHORS @@ -0,0 +1,23 @@ + +Present authors of ntfs-3g in alphabetical order: + +Jean-Pierre Andre +Alon Bar-Lev +Martin Bene +Dominique L Bouix +Csaba Henk +Bernhard Kaindl +Erik Larsson +Alejandro Pulver +Szabolcs Szakacsits +Miklos Szeredi + + +Past authors in alphabetical order: + +Anton Altaparmakov +Mario Emmenlauer +Yuval Fledel +Yura Pakhuchiy +Richard Russon + diff --git a/code/drivers/fs/ntfs/LICENCE.ntfs-efi b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/COPYING similarity index 95% rename from code/drivers/fs/ntfs/LICENCE.ntfs-efi rename to code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/COPYING index d159169..623b625 100644 --- a/code/drivers/fs/ntfs/LICENCE.ntfs-efi +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/COPYING @@ -1,12 +1,12 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. - Preamble + Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public @@ -15,7 +15,7 @@ software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to +the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not @@ -55,8 +55,8 @@ patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. - - GNU GENERAL PUBLIC LICENSE + + GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains @@ -110,7 +110,7 @@ above, provided that you also meet all of these conditions: License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) - + These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in @@ -168,7 +168,7 @@ access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. - + 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is @@ -225,7 +225,7 @@ impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. - + 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License @@ -255,7 +255,7 @@ make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. - NO WARRANTY + NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN @@ -277,9 +277,9 @@ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it @@ -303,16 +303,17 @@ the "copyright" line and a pointer to where the full notice is found. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: - Gnomovision version 69, Copyright (C) year name of author + Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. @@ -335,5 +336,5 @@ necessary. Here is a sample; alter the names: This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General +library. If this is what you want to do, use the GNU Library General Public License instead of this License. diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/COPYING.LIB b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/COPYING.LIB new file mode 100644 index 0000000..161a3d1 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/COPYING.LIB @@ -0,0 +1,482 @@ + GNU LIBRARY GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1991 Free Software Foundation, Inc. + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the library GPL. It is + numbered 2 because it goes with version 2 of the ordinary GPL.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Library General Public License, applies to some +specially designated Free Software Foundation software, and to any +other libraries whose authors decide to use it. You can use it for +your libraries, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if +you distribute copies of the library, or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link a program with the library, you must provide +complete object files to the recipients so that they can relink them +with the library, after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + Our method of protecting your rights has two steps: (1) copyright +the library, and (2) offer you this license which gives you legal +permission to copy, distribute and/or modify the library. + + Also, for each distributor's protection, we want to make certain +that everyone understands that there is no warranty for this free +library. If the library is modified by someone else and passed on, we +want its recipients to know that what they have is not the original +version, so that any problems introduced by others will not reflect on +the original authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that companies distributing free +software will individually obtain patent licenses, thus in effect +transforming the program into proprietary software. To prevent this, +we have made it clear that any patent must be licensed for everyone's +free use or not licensed at all. + + Most GNU software, including some libraries, is covered by the ordinary +GNU General Public License, which was designed for utility programs. This +license, the GNU Library General Public License, applies to certain +designated libraries. This license is quite different from the ordinary +one; be sure to read it in full, and don't assume that anything in it is +the same as in the ordinary license. + + The reason we have a separate public license for some libraries is that +they blur the distinction we usually make between modifying or adding to a +program and simply using it. Linking a program with a library, without +changing the library, is in some sense simply using the library, and is +analogous to running a utility program or application program. However, in +a textual and legal sense, the linked executable is a combined work, a +derivative of the original library, and the ordinary General Public License +treats it as such. + + Because of this blurred distinction, using the ordinary General +Public License for libraries did not effectively promote software +sharing, because most developers did not use the libraries. We +concluded that weaker conditions might promote sharing better. + + However, unrestricted linking of non-free programs would deprive the +users of those programs of all benefit from the free status of the +libraries themselves. This Library General Public License is intended to +permit developers of non-free programs to use free libraries, while +preserving your freedom as a user of such programs to change the free +libraries that are incorporated in them. (We have not seen how to achieve +this as regards changes in header files, but we have achieved it as regards +changes in the actual functions of the Library.) The hope is that this +will lead to faster development of free libraries. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, while the latter only +works together with the library. + + Note that it is possible for a library to be covered by the ordinary +General Public License rather than by this special one. + + GNU LIBRARY GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library which +contains a notice placed by the copyright holder or other authorized +party saying it may be distributed under the terms of this Library +General Public License (also called "this License"). Each licensee is +addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also compile or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + c) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + d) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the source code distributed need not include anything that is normally +distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Library General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, + MA 02111-1307, USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/CREDITS b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/CREDITS new file mode 100644 index 0000000..82cc124 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/CREDITS @@ -0,0 +1,61 @@ +The following people have contributed directly or indirectly +to the ntfs-3g project. + +Please let ntfs-3g-devel@lists.sf.net know if you believe +someone is missing, or if you prefer not to be listed. + +Dominique L Bouix +Csaba Henk +Max Khon +Auri Hautamäki +Gergely Erdelyi +Anton Altaparmakov +Peter Boross +Don Bright +Mario Emmenlauer +Yuval Fledel +Kano from Kanotix +Roland Kletzing +Maarten Lankhorst +Gergely Madarasz +Patrick McLean +Florent Mertens +Yura Pakhuchiy +Miklos Szeredi +Bartosz Taudul +Zhanglinbao +Wade Fitzpatrick +Carsten Einig +Adam Cecile +Bruno Damour +Ales Fruman +Curt McDowell +Thomas Franken +Jonatan Lambert +Klaus Knopper +Zhanglinbao +Ismail Donmez +Laszlo Dvornik +Pallaghy Ajtony +Szabolcs Szakacsits +Alexei Alexandrov +Albert D. Cahalan +Russ Christensen +Pete Curran +Andras Erdei +Matthew J. Fanto +Marcin Gibuła +Christophe Grenier +Ian Jackson +Carmelo Kintana +Jan Kratochvil +Lode Leroy +David Martínez Moreno +Giang Nguyen +Leonard Norrgård +Holger Ohmacht +Per Olofsson +Yuri Per +Richard Russon +Erik Sørnes + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ChangeLog b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ChangeLog new file mode 100644 index 0000000..68c1565 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ChangeLog @@ -0,0 +1,3 @@ +ChangeLog can be found at : + + https://github.com/tuxera/ntfs-3g/wiki diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/Makefile.am b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/Makefile.am new file mode 100644 index 0000000..0123e1f --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/Makefile.am @@ -0,0 +1,60 @@ + +AUTOMAKE_OPTIONS = gnu +ACLOCAL_AMFLAGS = -I m4 + +EXTRA_DIST = AUTHORS CREDITS COPYING NEWS autogen.sh + +MAINTAINERCLEANFILES=\ + $(srcdir)/configure \ + $(srcdir)/Makefile.in \ + $(srcdir)/aclocal.m4 \ + $(srcdir)/compile \ + $(srcdir)/depcomp \ + $(srcdir)/install-sh \ + $(srcdir)/ltmain.sh \ + $(srcdir)/missing \ + $(srcdir)/config.guess \ + $(srcdir)/config.sub \ + $(srcdir)/config.h.in \ + $(srcdir)/config.h.in~ \ + $(srcdir)/INSTALL \ + $(srcdir)/m4/ltsugar.m4 \ + $(srcdir)/m4/libtool.m4 \ + $(srcdir)/m4/ltversion.m4 \ + $(srcdir)/m4/lt~obsolete.m4 \ + $(srcdir)/m4/ltoptions.m4 + +SUBDIRS = include libfuse-lite libntfs-3g ntfsprogs src + +doc_DATA = README + +dist-hook: + $(MKDIR_P) "$(distdir)/m4" + +libtool: $(LIBTOOL_DEPS) + $(SHELL) ./config.status --recheck + +libs: +if FUSE_INTERNAL + (cd libfuse-lite && $(MAKE) libs) || exit 1; +endif + (cd libntfs-3g && $(MAKE) libs) || exit 1; + +libntfs: + (cd libntfs-3g && $(MAKE) libs) || exit 1; + +drivers: libs + (cd src && $(MAKE) drivers) || exit 1; + +ntfsprogs: libntfs + (cd ntfsprogs && $(MAKE)) || exit 1; + +if ENABLE_NTFSPROGS +strip: + (cd ntfsprogs && $(MAKE) strip) || exit 1; + +extra: extras + +extras: libs + (cd ntfsprogs && $(MAKE) extras) || exit 1; +endif diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/NEWS b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/NEWS new file mode 100644 index 0000000..1040fd8 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/NEWS @@ -0,0 +1,3 @@ +Project information can be found at : + + https://github.com/tuxera/ntfs-3g/ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/README b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/README new file mode 100644 index 0000000..aaa6341 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/README @@ -0,0 +1,166 @@ + +INTRODUCTION +============ + +The NTFS-3G driver is an open source, freely available read/write NTFS driver +for Linux, FreeBSD, macOS, NetBSD, OpenIndiana, QNX and Haiku. It provides +safe and fast handling of the Windows XP, Windows Server 2003, Windows 2000, +Windows Vista, Windows Server 2008, Windows 7, Windows 8, Windows Server 2012, +Windows Server 2016, Windows 10 and Windows Server 2019 NTFS file systems. + +The purpose of the project is to develop, quality assurance and support a +trustable, featureful and high performance solution for hardware platforms +and operating systems whose users need to reliably interoperate with NTFS. +Besides this practical goal, the project also aims to explore the limits +of the hybrid, kernel/user space filesystem driver approach, performance, +reliability and feature richness per invested effort wise. + +Besides the common file system features, NTFS-3G has support for file +ownership and permissions, POSIX ACLs, junction points, extended attributes +and creating internally compressed files (parameter files in the directory +.NTFS-3G may be required to enable them). The new compressed file formats +available in Windows 10 can also be read through a plugin. + +News, support answers, problem submission instructions, support and discussion +forums, and other information are available on the project web site at + + https://github.com/tuxera/ntfs-3g/wiki + +The project has been funded, supported and maintained since 2008 by Tuxera: + + https://tuxera.com + + +LICENSES +======== + +All the NTFS related components: the file system drivers, the ntfsprogs +utilities and the shared library libntfs-3g are distributed under the terms +of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. See the included file COPYING. + +The fuse-lite library is distributed under the terms of the GNU LGPLv2. +See the included file COPYING.LIB. + + +QUICK INSTALLATION +================== + +Most distributions have an up-to-date NTFS-3G package ready for use, and +the recommended way is to install it. + +If you need some specific customization, you can compile and install from +the released source code. Make sure you have the basic development tools +and the kernel includes the FUSE kernel module. Then unpack the source +tarball and type: + + ./configure + make + make install # or 'sudo make install' if you aren't root. + +Please note that NTFS-3G doesn't require the FUSE user space package any +more. + +The list of options for building specific configurations is displayed by +typing : + + ./configure --help + +Below are a few specific options to ./configure : + --disable-ntfsprogs : do not build the ntfsprogs tools, + --enable-extras : build more ntfsprogs tools, + --disable-plugins : disable support for plugins + --enable-posix-acls : enable support for Posix ACLs + --enable-xattr-mappings : enable system extended attributes mappings + --with-fuse=external : use external fuse (overriding Linux default) + +There are also a few make targets for building parts : + make libntfs : only build the libntfs-3g library + make libs : only build libntfs-3g (and libfuse-lite, if relevant) + make drivers : only build drivers and libraries, without ntfsprogs + make ntfsprogs : only build ntfsprogs and libntfs-3g, without drivers + + +USAGE +===== + +If there was no error during installation then the NTFS volume can be +read-write mounted for everybody the following way as the root user +(unmount the volume if it was already mounted, and replace /dev/sda1 +and /mnt/windows, if needed): + + mount -t ntfs-3g /dev/sda1 /mnt/windows +or + ntfs-3g /dev/sda1 /mnt/windows + +Please see the ntfs-3g manual page for more options and examples. + +You can also make NTFS to be mounted during boot by putting the below +line at the END(!) of the /etc/fstab file: + + /dev/sda1 /mnt/windows ntfs-3g defaults 0 0 + + +TESTING WITHOUT INSTALLING +========================= + +Newer versions of ntfs-3g can be tested without installing anything and +without disturbing an existing installation. Just configure and make as +shown previously. This will create the scripts ntfs-3g and lowntfs-3g +in the src directory, which you may activate for testing: + + ./configure + make + +then, as root: + src/ntfs-3g [-o mount-options] /dev/sda1 /mnt/windows + +And, to end the test, unmount the usual way: + umount /dev/sda1 + + +NTFS UTILITIES +============== + +The ntfsprogs directory includes utilities for doing all required tasks to +NTFS partitions. In general, just run a utility without any command line +options to display the version number and usage syntax. + +The following utilities are so far implemented: + +ntfsfix - Attempt to fix an NTFS partition and force Windows to check NTFS. + +mkntfs - Format a partition with the NTFS filesystem. See man 8 mkntfs for +command line options. + +ntfslabel - Display/change the label of an NTFS partition. See man 8 ntfslabel +for details. + +ntfsundelete - Recover deleted files from an NTFS volume. See man 8 +ntfsundelete for more details. + +ntfsresize - Resize NTFS volumes. See man 8 ntfsresize for details. + +ntfsclone - Efficiently create/restore an image of an NTFS partition. See +man 8 ntfsclone for details. + +ntfscluster - Locate the owner of any given sector or cluster on an NTFS +partition. See man 8 ntfscluster for details. + +ntfsinfo - Show some information about an NTFS partition or one of the files +or directories within it. See man 8 ntfsinfo for details. + +ntfsrecover - Recover updates committed by Windows but interrupted before +being synced. + +ntfsls - List information about files in a directory residing on an NTFS +partition. See man 8 ntfsls for details. + +ntfscat - Concatenate files and print their contents on the standard output. + +ntfscp - Overwrite files on an NTFS partition. + +ntfssecaudit - Audit the security metadata. + +ntfsusermap - Assistance for building a user mapping file. diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/TODO.ntfsprogs b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/TODO.ntfsprogs new file mode 100644 index 0000000..3a60421 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/TODO.ntfsprogs @@ -0,0 +1,126 @@ +Please keep in alphabetical order so utilities are easier to find. + +Thanks, + Anton + + +********** +* mkntfs * +********** + +- Correct support for creating volumes with larger sector sizes (mft record + size, cluster size, and index block size must be >= sector size), so for 1k, + 2k, and 4k sectors, we need to set the default mft record, cluster, and index + block size to be at least the sector size. +- Correct the odd last partition sector not being accessible under 2.4 kernels + by setting the device block size to the sector size (default is 1k on 2.4 + kernels and they can't cope with partial sectors). +- Got a report that creating a floppy with mkntfs failed. Difference between + this floppy and the floppy created by the special tool found on the net was + said to be that the bitmap is 256kib on the special floppy while mkntfs will + make it much smaller. Need to verify this and experiment with the bitmap + size to make it work. Note, reporter was using win2k. + + +************* +* ntfsclone * +************* + +- get rid of the unneeded lseek()'s during reads/writes (probably it + doesn't improve performance much, or any at all) +- catch if source and dest are the same +- disable consistency check for --metadata (e.g. if the check is crashing) +- option: --inode +- option: --data +- metadata cloning: skip more non-needed inodes +- manual: document LFS issues (smbfs' lfs option, nfs) +- manual: mention optimized seeks +- manual: optimal backup if disks have bad sectors +- manual: ntfsclone guarantees the restored image works only + if one restores to the exactly same partition. For example, + one can not copy system partition to a different partition: + minimum "hidden sectors" field and BOOT.INI need modifications. + We could do these adjustments optionally. +- check if kernel block size = GCD(page size, device size) makes + effect on performance (Al Viro says no) +- check whether the O_WRONLY -> O_RDWR change made effect on performance + + +*********** +* ntfscmp * +*********** + +- compare mft record headers +- exit status is 0 if inputs are the same, 1 if different, other if trouble +- optionally ignore less interesting fields (e.g. attribute instance) +- new option: --metadata mode +- unnamed resident attributes with same type are ignored +- code cleanup, remove many cross-util duplicates +- handle deleted records +- performance: special handling for sparse files + + +********** +* ntfscp * +********** + +- add ability to copy multiple files at once. + + +*********** +* ntfsfix * +*********** + +- Cleanup to use ntfs_attr_* API for editing $MFTMirr, $Volume, and $LogFile. + This has the immediate benefit of enabling attribute list support and making + the code simpler. +- On ntfs 3.0+ volumes need to disable the usn journal if it is active. This + means deleting file $UsnJrnl from /$Extend directory. +- On ntfs 3.0+ volumes need to mark the quota out of date? - Probably, but + it shouldn't cause any corruption not doing so for the moment so this is + not a showstopper bug for the first release. (AIA) + + +************* +* ntfslabel * +************* + +- Support ioctls for kernel driver and ntfsmount for reading/changing the label. + + +************* +* ntfsmount * +************* + + + +************** +* ntfsresize * +************** + +High priority + - move ntfs consistency check to libntfs (for ntfsck, ntfsclone, etc) + - use different exit codes (e.g. corrupt volume detected, unsupported case, + bad sectors, etc) + +Medium priority + - cope with the rare, unsupported cases, see man ntfsresize 'KNOWN ISSUES' + - save $Bitmap if it was modified and an error occures (e.g. bad sector). + - handle signals (^C, etc) + +Low priority + - fully support disks with bad sectors (attrlist attr, unknown bad sectors) + - move volume start + + +**************** +* ntfsundelete * +**************** + +- undelete by name rather than inode number +- support for compressed files +- support for internationalisation +- recover by type? +- mass undelete (using wildcards) +- display parent directory +- name "" to MFTn diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/autogen.sh b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/autogen.sh new file mode 100644 index 0000000..62e5439 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/autogen.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Run this to generate configure, Makefile.in's, etc + +(autoreconf --version) < /dev/null > /dev/null 2>&1 || { + (autoconf --version) < /dev/null > /dev/null 2>&1 || { + echo + echo "**Error**: You must have the GNU Build System (autoconf, automake, " + echo "libtool, etc) to update the ntfs-3g build system. Download the " + echo "appropriate packages for your distribution, or get the source " + echo "tar balls from ftp://ftp.gnu.org/pub/gnu/." + exit 1 + } + echo + echo "**Error**: Your version of autoconf is too old (you need at least 2.57)" + echo "to update the ntfs-3g build system. Download the appropriate " + echo "updated package for your distribution, or get the source tar ball " + echo "from ftp://ftp.gnu.org/pub/gnu/." + exit 1 +} + +echo Running autoreconf --verbose --install --force +autoreconf --verbose --install --force diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/configure.ac b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/configure.ac new file mode 100644 index 0000000..c82ab91 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/configure.ac @@ -0,0 +1,708 @@ +# +# configure.ac - Source file to generate "./configure" to prepare package for +# compilation. +# +# Copyright (c) 2000-2013 Anton Altaparmakov +# Copyright (c) 2003 Jan Kratochvil +# Copyright (c) 2005-2009 Szabolcs Szakacsits +# Copyright (C) 2007-2008 Alon Bar-Lev +# +# This program/include file is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as published +# by the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program/include file is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty +# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program (in the main directory of the NTFS-3G +# distribution in the file COPYING); if not, write to the Free Software +# Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +# Autoconf +AC_PREREQ(2.59) +AC_INIT([ntfs-3g],[2026.7.7],[ntfs-3g-devel@lists.sf.net]) +LIBNTFS_3G_VERSION="90" +AC_CONFIG_SRCDIR([src/ntfs-3g.c]) + +# Environment +AC_CANONICAL_HOST +AC_CANONICAL_TARGET + +# Automake +AM_INIT_AUTOMAKE([]) +AC_CONFIG_HEADERS([config.h]) +AC_CONFIG_MACRO_DIR([m4]) +AM_MAINTAINER_MODE + +# Options +AC_ARG_ENABLE( + [debug], + [AS_HELP_STRING([--enable-debug],[enable debugging code and output])], + , + [enable_debug="no"] +) + +AC_ARG_ENABLE( + [warnings], + [AS_HELP_STRING([--enable-warnings],[enable lots of compiler warnings])], + , + [enable_warnings="no"] +) + +AC_ARG_ENABLE( + [pedantic], + [AS_HELP_STRING([--enable-pedantic],[enable compile pedantic mode])], + , + [enable_pedantic="no"] +) + +AC_ARG_ENABLE( + [really-static], + [AS_HELP_STRING([--enable-really-static],[create fully static binaries])], + , + [enable_really_static="no"] +) + +AC_ARG_ENABLE( + [mount-helper], + [AS_HELP_STRING([--enable-mount-helper],[install mount helper @<:@default=enabled for linux@:>@])], + , + [ + case "${target_os}" in + linux*) enable_mount_helper="yes" ;; + *) enable_mount_helper="no" ;; + esac + ] +) + +AC_ARG_ENABLE( + [ldscript], + [AS_HELP_STRING([--enable-ldscript],[use ldscript instead of .so symlink])], + , + [enable_ldscript="no"] +) + +AC_ARG_ENABLE( + [ldconfig], + [AS_HELP_STRING([--disable-ldconfig],[do not update dynamic linker cache using ldconfig])], + , + [enable_ldconfig="yes"] +) + +AC_ARG_ENABLE( + [library], + [AS_HELP_STRING([--disable-library],[do not install libntfs-3g but link it into ntfs-3g])], + , + [enable_library="yes"] +) + +AC_ARG_ENABLE( + [mtab], + [AS_HELP_STRING([--disable-mtab],[disable and ignore usage of /etc/mtab])], + , + [enable_mtab="yes"] +) + +AC_ARG_ENABLE( + [posix-acls], + [AS_HELP_STRING([--enable-posix-acls],[enable POSIX ACL support])], + , + [enable_posix_acls="no"] +) + +AC_ARG_ENABLE( + [xattr-mappings], + [AS_HELP_STRING([--enable-xattr-mappings],[enable system extended attributes mappings])], + , + [enable_xattr_mappings="no"] +) + +AC_ARG_ENABLE( + [plugins], + [AS_HELP_STRING([--disable-plugins], [Disable external reparse point + plugins for the ntfs-3g FUSE driver])], + [if test x${enableval} = "xyes"; then disable_plugins="no"; fi], + [disable_plugins="no"] +) + +AC_ARG_ENABLE( + [device-default-io-ops], + [AS_HELP_STRING([--disable-device-default-io-ops],[install default IO ops])], + , + [enable_device_default_io_ops="yes"] +) + +AC_ARG_ENABLE( + [ntfs-3g], + [AS_HELP_STRING([--disable-ntfs-3g],[disable the ntfs-3g FUSE driver])], + , + [enable_ntfs_3g="yes"] +) + +AC_ARG_ENABLE( + [ntfsprogs], + [AS_HELP_STRING([--disable-ntfsprogs],[disable ntfsprogs utilities + (default=no)])], + , + [enable_ntfsprogs="yes"] +) + +AC_ARG_ENABLE(crypto, + AS_HELP_STRING(--enable-crypto,enable crypto related code and utilities + (default=no)), , + enable_crypto=no +) + +AC_ARG_ENABLE( + [extras], + [AS_HELP_STRING([--enable-extras],[enable extra ntfsprogs utilities + (default=no)])], + , + [enable_extras="no"] +) + +AC_ARG_ENABLE( + [quarantined], + [AS_HELP_STRING([--enable-quarantined],[enable quarantined ntfsprogs utilities + (default=no)])], + , + [enable_quarantined="no"] +) + +AC_ARG_ENABLE( + [nfconv], + [AS_HELP_STRING([--disable-nfconv],[disable the 'nfconv' patch, which adds support for Unicode normalization form conversion when built on Mac OS X @<:@default=enabled for Mac OS X@:>@])], + [enable_nfconv="no"], + [ + case "${target_os}" in + darwin*) enable_nfconv="yes" ;; + *) enable_nfconv="no" ;; + esac + ] +) + +# pthread_rwlock_t requires _GNU_SOURCE +AC_GNU_SOURCE + +# Programs +AC_PROG_CC(gcc cc) +AC_PROG_LN_S +AM_PROG_CC_C_O + +ifdef( + [LT_INIT], + [LT_INIT], + [AC_PROG_LIBTOOL] +) + +AC_PROG_INSTALL +PKG_PROG_PKG_CONFIG + +AC_PATH_PROG([MV], [mv]) +AC_PATH_PROG([RM], [rm]) +AC_PATH_PROG([SED], [sed]) +AC_ARG_VAR([LDCONFIG], [ldconfig utility]) +AC_PATH_PROG([LDCONFIG], [ldconfig], [true], [/sbin /usr/sbin $PATH]) + +# Environment +AC_MSG_CHECKING([Windows OS]) +case "${target}" in +*-mingw32*|*-winnt*|*-cygwin*) + AC_MSG_RESULT([yes]) + WINDOWS="yes" + AC_DEFINE( + [WINDOWS], + [1], + [Define to 1 if this is a Windows OS] + ) + ;; +*) + AC_MSG_RESULT([no]) + WINDOWS="no" + ;; +esac + +if test "x${enable_ntfs_3g}" != "xyes"; then + with_fuse="none" +elif test "x${with_fuse}" = "x"; then + AC_MSG_CHECKING([fuse compatibility]) + case "${target_os}" in + linux*|solaris*) + AC_ARG_WITH( + [fuse], + [AS_HELP_STRING([--with-fuse=],[Select FUSE library: internal or external @<:@default=internal@:>@])], + , + [with_fuse="internal"] + ) + ;; + darwin*|netbsd*|kfreebsd*-gnu) + with_fuse="external" + ;; + freebsd*) + AC_MSG_ERROR([Please see FreeBSD support at http://www.freshports.org/sysutils/fusefs-ntfs]) + ;; + *) + AC_MSG_ERROR([ntfs-3g can be built for Linux, FreeBSD, Mac OS X, NetBSD, and Solaris only.]) + ;; + esac + AC_MSG_RESULT([${with_fuse}]) +fi + +case "${target_os}" in +solaris*) + if test "x$GCC" != "xyes" ; then + AC_MSG_ERROR([ntfs-3g can be built only with gcc on Solaris. Install it by 'pkg install gcc-dev' and retry.)]) + fi + ;; +esac + +if test "${enable_ldscript}" = "yes"; then + AC_MSG_CHECKING([Output format]) + OUTPUT_FORMAT="$(${CC} ${CFLAGS} ${LDFLAGS} -Wl,--verbose 2>&1 | ${SED} -n 's/^OUTPUT_FORMAT("\([[^"]]*\)",.*/\1/p')" + if test -z "${OUTPUT_FORMAT}"; then + AC_MSG_RESULT([None]) + else + AC_MSG_RESULT([${OUTPUT_FORMAT}]) + OUTPUT_FORMAT="OUTPUT_FORMAT ( ${OUTPUT_FORMAT} )" + fi +fi + +# Libraries +if test "${with_fuse}" = "internal"; then + AC_CHECK_LIB( + [pthread], + [pthread_create], + [LIBFUSE_LITE_LIBS="${LIBFUSE_LITE_LIBS} -lpthread"], + [AC_MSG_ERROR([Cannot find pthread library])] + ) + AC_DEFINE( + [_REENTRANT], + [1], + [Required define if using POSIX threads] + ) + # required so that we re-compile anything + AC_DEFINE( + [FUSE_INTERNAL], + [1], + [Define to 1 if using internal fuse] + ) + + AC_MSG_CHECKING([Solaris OS]) + AC_LANG_PUSH([C]) + AC_COMPILE_IFELSE( + [ + AC_LANG_SOURCE( + [[#if !((defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__)))]] + [[#error "Not a Solaris system."]] + [[#endif]] + ) + ], + [ + AC_MSG_RESULT([yes]) + LIBFUSE_LITE_CFLAGS="${LIBFUSE_LITE_CFLAGS} -std=c99 -D__SOLARIS__ -D_XOPEN_SOURCE=600 -D__EXTENSIONS__" + LIBFUSE_LITE_LIBS="${LIBFUSE_LITE_LIBS} -lxnet" + ], + [ + AC_MSG_RESULT([no]) + ] + ) + AC_LANG_POP([C]) +elif test "${with_fuse}" = "external"; then + if test -z "$PKG_CONFIG"; then + AC_PATH_PROG(PKG_CONFIG, pkg-config, no) + fi + test "x${PKG_CONFIG}" = "xno" && AC_MSG_ERROR([pkg-config wasn't found! Please install from your vendor, or see http://pkg-config.freedesktop.org/wiki/]) + # Libraries often install their metadata .pc files in directories + # not searched by pkg-config. Let's workaround this. + export PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:/lib/pkgconfig:/usr/lib/pkgconfig:/opt/gnome/lib/pkgconfig:/usr/share/pkgconfig:/usr/local/lib/pkgconfig:$prefix/lib/pkgconfig:/opt/gnome/share/pkgconfig:/usr/local/share/pkgconfig + PKG_CHECK_MODULES( + [FUSE_MODULE], + [fuse >= 2.6.0], + , + [ + AC_MSG_ERROR([FUSE >= 2.6.0 was not found. Either older FUSE is still present, or FUSE is not fully installed (e.g. fuse, libfuse, libfuse2, libfuse-dev, etc packages). Source code: http://fuse.sf.net]) + ] + ) + FUSE_LIB_PATH=`$PKG_CONFIG --libs-only-L fuse | sed -e 's,/[/]*,/,g' -e 's,[ ]*$,,'` +fi + +# Autodetect whether we can build crypto stuff or not. +compile_crypto=false +if test "$enable_crypto" != "no"; then + have_libgcrypt=false + AM_PATH_LIBGCRYPT(1.2.2, [ have_libgcrypt=true ], + [ + if test "$enable_crypto" = "yes"; then + AC_MSG_ERROR([ntfsprogs crypto code requires the gcrypt library.]) + else + AC_MSG_WARN([ntfsprogs crypto code requires the gcrypt library.]) + fi + ]) + have_libgnutls=false + PKG_CHECK_MODULES(GNUTLS, gnutls >= 1.4.4, [ have_libgnutls=true ], + if test "$enable_crypto" = "yes"; then + AC_MSG_ERROR([ntfsprogs crypto code requires the gnutls library.]) + else + AC_MSG_WARN([ntfsprogs crypto code requires the gnutls library.]) + fi + ) + if test "$have_libgcrypt" = "true"; then + if test "$have_libgnutls" = "true"; then + compile_crypto=true + AC_DEFINE([ENABLE_CRYPTO], 1, + [Define this to 1 if you want to enable support of + encrypted files in libntfs and utilities.]) + fi + fi +fi +AM_CONDITIONAL(ENABLE_CRYPTO, $compile_crypto) + +# add --with-extra-includes and --with-extra-libs switch to ./configure +all_libraries="$all_libraries $USER_LDFLAGS" +all_includes="$all_includes $USER_INCLUDES" +AC_SUBST(all_includes) +AC_SUBST(all_libraries) + +# Specify support for generating DCE compliant UUIDs (aka GUIDs). We check if +# uuid/uuid.h header is present and the uuid library is present that goes with +# it and then check if uuid_generate() is present and usable. +# +# DCE UUIDs are enabled by default and can be disabled with the --disable-uuid +# option to the configure script. +AC_ARG_WITH(uuid, [ + --with-uuid@<:@=PFX@:>@ generate DCE compliant UUIDs, with optional prefix + to uuid library and headers @<:@default=detect@:>@ + --without-uuid do not generate DCE compliant UUIDs], + if test "$with_uuid" = "yes"; then + extrapath=default + elif test "$with_uuid" = "no"; then + extrapath= + else + extrapath=$with_uuid + fi, + extrapath=default +) +if test "x$extrapath" != "x"; then + if test "x$extrapath" != "xdefault"; then + MKNTFS_CPPFLAGS="$MKNTFS_CPPFLAGS -I$extrapath/include" + MKNTFS_LIBS="$MKNTFS_LIBS -L$extrapath/lib" + fi + + search_for_luuid="yes" + AC_CHECK_HEADER([uuid/uuid.h], + [], + [ + AC_MSG_WARN([ntfsprogs DCE compliant UUID generation code requires the uuid library.]) + search_for_luuid="no" + ], + ) + + if test "x$search_for_luuid" != "xno"; then + # Look for uuid_generate in the standard C library. + AC_CHECK_FUNC([uuid_generate], + [ + AC_DEFINE([ENABLE_UUID], 1, + [Define this to 1 if you want to enable + generation of DCE compliant UUIDs.]) + search_for_luuid="no" + ], + [], + ) + fi + + if test "x$search_for_luuid" != "xno"; then + # Look for uuid_generate in the 'uuid' library. + AC_CHECK_LIB([uuid], [uuid_generate], + [ + AC_DEFINE([ENABLE_UUID], 1, + [Define this to 1 if you want to enable + generation of DCE compliant UUIDs.]) + MKNTFS_LIBS="$MKNTFS_LIBS -luuid" + search_for_luuid="no" + ], + [], + ) + fi + + if test "x$search_for_luuid" != "xno"; then + AC_MSG_WARN([ntfsprogs DCE compliant UUID generation code requires the uuid library.]) + fi +fi + +# Specify support for obtaining the correct BIOS legacy geometry needed for +# Windows to boot in CHS mode. We check if hd.h header is present and the hd +# library is present that goes with it and then check if the hd_list() function +# is present and usable. +# +# Using the hd library is enabled by default and can be disabled with the +# --disable-hd option to the configure script. +AC_ARG_WITH(hd, [ + --with-hd@<:@=PFX@:>@ use Windows compliant disk geometry, with optional + prefix to hd library and headers @<:@default=detect@:>@ + --without-hd do not use Windows compliant disk geometry], + if test "$with_hd" = "yes"; then + extrapath2=default + elif test "$with_hd" = "no"; then + extrapath2= + else + extrapath2=$with_hd + fi, + extrapath2=default +) +if test "x$extrapath2" != "x"; then + if test "x$extrapath2" != "xdefault"; then + LIBNTFS_CPPFLAGS="$LIBNTFS_CPPFLAGS -I$extrapath2/include" + LIBNTFS_LIBS="$LIBNTFS_LIBS -L$extrapath2/lib" + fi + AC_CHECK_HEADER([hd.h], + AC_CHECK_LIB([hd], [hd_list], + AC_DEFINE([ENABLE_HD], 1, + [Define this to 1 if you want to enable use of Windows + compliant disk geometry.]) + LIBNTFS_LIBS="$LIBNTFS_LIBS -lhd" + NTFSPROGS_STATIC_LIBS="$NTFSPROGS_STATIC_LIBS -lhd", + AC_MSG_WARN([ntfsprogs Windows compliant geometry code requires the hd library.]), + ), + AC_MSG_WARN([ntfsprogs Windows compliant geometry code requires the hd library.]), + ) +fi + +# Checks for header files. +AC_HEADER_STDC +AC_HEADER_MAJOR +AC_CHECK_HEADERS([ctype.h fcntl.h libgen.h libintl.h limits.h locale.h \ + mntent.h stddef.h stdint.h stdlib.h stdio.h stdarg.h string.h \ + strings.h errno.h time.h unistd.h utime.h wchar.h getopt.h features.h \ + regex.h endian.h byteswap.h sys/byteorder.h sys/disk.h sys/endian.h \ + sys/param.h sys/ioctl.h sys/mount.h sys/stat.h sys/types.h \ + sys/vfs.h sys/statvfs.h linux/major.h linux/fd.h \ + linux/fs.h inttypes.h linux/hdreg.h \ + machine/endian.h windows.h syslog.h pwd.h malloc.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_C_BIGENDIAN( + , + [ + AC_DEFINE( + [WORDS_LITTLEENDIAN], + [1], + [Define to 1 if your processor stores words with the least significant + byte first (like Intel and VAX, unlike Motorola and SPARC).] + ) + ] + , +) +AC_C_CONST +AC_C_INLINE +AC_TYPE_OFF_T +AC_TYPE_SIZE_T +AC_STRUCT_ST_BLOCKS +AC_CHECK_MEMBERS([struct stat.st_rdev]) +AC_CHECK_MEMBERS([struct stat.st_atim]) +AC_CHECK_MEMBERS([struct stat.st_atimespec]) +AC_CHECK_MEMBERS([struct stat.st_atimensec]) + +# For the 'nfconv' patch (Mac OS X only): +case "${target_os}" in +darwin*) + if test "${enable_nfconv}" = "yes"; then + AC_CHECK_HEADER( + [CoreFoundation/CoreFoundation.h], + [ + LDFLAGS="${LDFLAGS} -framework CoreFoundation" + AC_DEFINE( + [ENABLE_NFCONV], + [1], + [Define to 1 if the nfconv patch should be enabled] + ) + ], + AC_MSG_ERROR([[Cannot find CoreFoundation required for 'nfconv' functionality Mac OS X. You may use the --disable-nfconv 'configure' option to avoid this error.]]) + ) + fi + ;; +esac + +# Checks for library functions. +AC_FUNC_GETMNTENT +AC_FUNC_MBRTOWC +AC_FUNC_MEMCMP +AC_FUNC_STAT +AC_FUNC_STRFTIME +AC_FUNC_UTIME_NULL +AC_FUNC_VPRINTF +AC_CHECK_FUNCS([ \ + atexit basename daemon dup2 fdatasync ffs getopt_long hasmntopt \ + mbsinit memmove memset realpath regcomp setlocale setxattr \ + strcasecmp strchr strdup strerror strnlen strsep strtol strtoul \ + sysconf utime utimensat gettimeofday clock_gettime fork memcpy random snprintf \ +]) +AC_SYS_LARGEFILE + +# The dlopen API might be in libc or in libdl. Check libc first, then +# fall back to libdl. +LIBDL="" +if test "x${disable_plugins}" = "xno"; then + AC_CHECK_LIB(c, dlopen, , + [AC_CHECK_LIB(dl, dlopen, [LIBDL="-ldl"], + [AC_MSG_ERROR(["Unable to find libdl (required for external plugin support)"])])]) +fi +AC_SUBST([LIBDL]) + +if test "$GCC" = "yes" ; then + # We add -Wall to enable some compiler warnings. + CFLAGS="${CFLAGS} -Wall" +fi + +if test "${enable_pedantic}" = "yes"; then + enable_warnings="yes" + CFLAGS="${CFLAGS} -pedantic" +fi + +if test "${enable_warnings}" = "yes"; then + CFLAGS="${CFLAGS} -W -Wall -Waggregate-return -Wbad-function-cast -Wcast-align -Wcast-qual -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Winline -Wmissing-declarations -Wmissing-format-attribute -Wmissing-noreturn -Wmissing-prototypes -Wmultichar -Wnested-externs -Wpointer-arith -Wredundant-decls -Wshadow -Wsign-compare -Wstrict-prototypes -Wundef -Wwrite-strings -Wformat -Wformat-security -Wuninitialized" +fi + +if test "${enable_debug}" = "yes"; then + CFLAGS="${CFLAGS} -ggdb3 -DDEBUG" + AC_DEFINE( + [ENABLE_DEBUG], + [1], + [Define to 1 if debug should be enabled] + ) +fi + +test "${enable_device_default_io_ops}" = "no" && AC_DEFINE( + [NO_NTFS_DEVICE_DEFAULT_IO_OPS], + [1], + [Don't use default IO ops] +) + +test "${enable_mtab}" = "no" && AC_DEFINE([IGNORE_MTAB], [1], [Don't update /etc/mtab]) +test "${enable_posix_acls}" != "no" && AC_DEFINE([POSIXACLS], [1], [POSIX ACL support]) +test "${enable_xattr_mappings}" != "no" && AC_DEFINE([XATTR_MAPPINGS], [1], [system extended attributes mappings]) +test "${disable_plugins}" != "no" && AC_DEFINE([DISABLE_PLUGINS], [1], [Define to 1 for disabling reparse plugins]) + +test "${enable_really_static}" = "yes" && enable_library="no" +test "${enable_library}" = "no" && enable_ldconfig="no" + +if test "x${DISTCHECK_HACK}" != "x"; then + enable_mount_helper="no" + enable_ldconfig="no" +fi + +# Settings +pkgconfigdir="\$(libdir)/pkgconfig" +ntfs3gincludedir="\$(includedir)/ntfs-3g" +# Executables should be installed to the root filesystem, otherwise +# automounting NTFS volumes can fail during boot if the driver binaries +# and their dependencies are on an unmounted partition. Use --exec-prefix +# to override this. +if test "x${exec_prefix}" = "xNONE"; then + rootbindir="/bin" + rootsbindir="/sbin" + rootlibdir="/lib${libdir##*/lib}" +else + rootbindir="\$(bindir)" + rootsbindir="\$(sbindir)" + rootlibdir="\$(libdir)" +fi +AC_SUBST([pkgconfigdir]) +AC_SUBST([ntfs3gincludedir]) +AC_SUBST([rootbindir]) +AC_SUBST([rootsbindir]) +AC_SUBST([rootlibdir]) +AC_SUBST([LIBNTFS_3G_VERSION]) +AC_SUBST([LIBFUSE_LITE_CFLAGS]) +AC_SUBST([LIBFUSE_LITE_LIBS]) +AC_SUBST([MKNTFS_CPPFLAGS]) +AC_SUBST([MKNTFS_LIBS]) +AC_SUBST([LIBNTFS_CPPFLAGS]) +AC_SUBST([LIBNTFS_LIBS]) +AC_SUBST([NTFSPROGS_STATIC_LIBS]) +AC_SUBST([OUTPUT_FORMAT]) +AM_CONDITIONAL([FUSE_INTERNAL], [test "${with_fuse}" = "internal"]) +AM_CONDITIONAL([GENERATE_LDSCRIPT], [test "${enable_ldscript}" = "yes"]) +AM_CONDITIONAL([WINDOWS], [test "${WINDOWS}" = "yes"]) +AM_CONDITIONAL([NTFS_DEVICE_DEFAULT_IO_OPS], [test "${enable_device_default_io_ops}" = "yes"]) +AM_CONDITIONAL([RUN_LDCONFIG], [test "${enable_ldconfig}" = "yes"]) +AM_CONDITIONAL([REALLYSTATIC], [test "${enable_really_static}" = "yes"]) +AM_CONDITIONAL([INSTALL_LIBRARY], [test "${enable_library}" = "yes"]) +AM_CONDITIONAL([ENABLE_MOUNT_HELPER], [test "${enable_mount_helper}" = "yes"]) +AM_CONDITIONAL([ENABLE_NTFS_3G], [test "${enable_ntfs_3g}" = "yes"]) +AM_CONDITIONAL([ENABLE_NTFSPROGS], [test "${enable_ntfsprogs}" = "yes"]) +AM_CONDITIONAL([ENABLE_EXTRAS], [test "${enable_extras}" = "yes"]) +AM_CONDITIONAL([ENABLE_QUARANTINED], [test "${enable_quarantined}" = "yes"]) +AM_CONDITIONAL([DISABLE_PLUGINS], [test "${disable_plugins}" != "no"]) + +# workaround for /dev/null; then + cat < + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#ifndef _FUSE_H_ +#define _FUSE_H_ + +/** @file + * + * This file defines the library interface of FUSE + */ + +#include "fuse_common.h" + +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* ----------------------------------------------------------- * + * Basic FUSE API * + * ----------------------------------------------------------- */ + +/** Handle for a FUSE filesystem */ +struct fuse; + +/** Structure containing a raw command */ +struct fuse_cmd; + +/** Function to add an entry in a readdir() operation + * + * @param buf the buffer passed to the readdir() operation + * @param name the file name of the directory entry + * @param stat file attributes, can be NULL + * @param off offset of the next entry or zero + * @return 1 if buffer is full, zero otherwise + */ +typedef int (*fuse_fill_dir_t) (void *buf, const char *name, + const struct stat *stbuf, off_t off); + +/** + * The file system operations: + * + * Most of these should work very similarly to the well known UNIX + * file system operations. A major exception is that instead of + * returning an error in 'errno', the operation should return the + * negated error value (-errno) directly. + * + * All methods are optional, but some are essential for a useful + * filesystem (e.g. getattr). Open, flush, release, fsync, opendir, + * releasedir, fsyncdir, access, create, ftruncate, fgetattr, lock, + * init and destroy are special purpose methods, without which a full + * featured filesystem can still be implemented. + * + * Almost all operations take a path which can be of any length. + * + * Changed in fuse 2.8.0 (regardless of API version) + * Previously, paths were limited to a length of PATH_MAX. + */ + +struct fuse_operations { + /** Get file attributes. + * + * Similar to stat(). The 'st_dev' and 'st_blksize' fields are + * ignored. The 'st_ino' field is ignored except if the 'use_ino' + * mount option is given. + */ + int (*getattr) (const char *, struct stat *); + + /** Read the target of a symbolic link + * + * The buffer should be filled with a null terminated string. The + * buffer size argument includes the space for the terminating + * null character. If the linkname is too long to fit in the + * buffer, it should be truncated. The return value should be 0 + * for success. + */ + int (*readlink) (const char *, char *, size_t); + + /** Create a file node + * + * This is called for creation of all non-directory, non-symlink + * nodes. If the filesystem defines a create() method, then for + * regular files that will be called instead. + */ + int (*mknod) (const char *, mode_t, dev_t); + + /** Create a directory + * + * Note that the mode argument may not have the type specification + * bits set, i.e. S_ISDIR(mode) can be false. To obtain the + * correct directory type bits use mode|S_IFDIR + * */ + int (*mkdir) (const char *, mode_t); + + /** Remove a file */ + int (*unlink) (const char *); + + /** Remove a directory */ + int (*rmdir) (const char *); + + /** Create a symbolic link */ + int (*symlink) (const char *, const char *); + + /** Rename a file */ + int (*rename) (const char *, const char *); + + /** Create a hard link to a file */ + int (*link) (const char *, const char *); + + /** Change the permission bits of a file */ + int (*chmod) (const char *, mode_t); + + /** Change the owner and group of a file */ + int (*chown) (const char *, uid_t, gid_t); + + /** Change the size of a file */ + int (*truncate) (const char *, off_t); + + /** Change the access and/or modification times of a file + * + * Deprecated, use utimens() instead. + */ + int (*utime) (const char *, struct utimbuf *); + + /** File open operation + * + * No creation (O_CREAT, O_EXCL) and by default also no + * truncation (O_TRUNC) flags will be passed to open(). If an + * application specifies O_TRUNC, fuse first calls truncate() + * and then open(). Only if 'atomic_o_trunc' has been + * specified and kernel version is 2.6.24 or later, O_TRUNC is + * passed on to open. + * + * Unless the 'default_permissions' mount option is given, + * open should check if the operation is permitted for the + * given flags. Optionally open may also return an arbitrary + * filehandle in the fuse_file_info structure, which will be + * passed to all file operations. + * + * Changed in version 2.2 + */ + int (*open) (const char *, struct fuse_file_info *); + + /** Read data from an open file + * + * Read should return exactly the number of bytes requested except + * on EOF or error, otherwise the rest of the data will be + * substituted with zeroes. An exception to this is when the + * 'direct_io' mount option is specified, in which case the return + * value of the read system call will reflect the return value of + * this operation. + * + * Changed in version 2.2 + */ + int (*read) (const char *, char *, size_t, off_t, + struct fuse_file_info *); + + /** Write data to an open file + * + * Write should return exactly the number of bytes requested + * except on error. An exception to this is when the 'direct_io' + * mount option is specified (see read operation). + * + * Changed in version 2.2 + */ + int (*write) (const char *, const char *, size_t, off_t, + struct fuse_file_info *); + + /** Get file system statistics + * + * The 'f_frsize', 'f_favail', 'f_fsid' and 'f_flag' fields are ignored + * + * Replaced 'struct statfs' parameter with 'struct statvfs' in + * version 2.5 + */ + int (*statfs) (const char *, struct statvfs *); + + /** Possibly flush cached data + * + * BIG NOTE: This is not equivalent to fsync(). It's not a + * request to sync dirty data. + * + * Flush is called on each close() of a file descriptor. So if a + * filesystem wants to return write errors in close() and the file + * has cached dirty data, this is a good place to write back data + * and return any errors. Since many applications ignore close() + * errors this is not always useful. + * + * NOTE: The flush() method may be called more than once for each + * open(). This happens if more than one file descriptor refers + * to an opened file due to dup(), dup2() or fork() calls. It is + * not possible to determine if a flush is final, so each flush + * should be treated equally. Multiple write-flush sequences are + * relatively rare, so this shouldn't be a problem. + * + * Filesystems shouldn't assume that flush will always be called + * after some writes, or that if will be called at all. + * + * Changed in version 2.2 + */ + int (*flush) (const char *, struct fuse_file_info *); + + /** Release an open file + * + * Release is called when there are no more references to an open + * file: all file descriptors are closed and all memory mappings + * are unmapped. + * + * For every open() call there will be exactly one release() call + * with the same flags and file descriptor. It is possible to + * have a file opened more than once, in which case only the last + * release will mean, that no more reads/writes will happen on the + * file. The return value of release is ignored. + * + * Changed in version 2.2 + */ + int (*release) (const char *, struct fuse_file_info *); + + /** Synchronize file contents + * + * If the datasync parameter is non-zero, then only the user data + * should be flushed, not the meta data. + * + * Changed in version 2.2 + */ + int (*fsync) (const char *, int, struct fuse_file_info *); + + /** Set extended attributes */ + int (*setxattr) (const char *, const char *, const char *, size_t, int); + + /** Get extended attributes */ + int (*getxattr) (const char *, const char *, char *, size_t); + + /** List extended attributes */ + int (*listxattr) (const char *, char *, size_t); + + /** Remove extended attributes */ + int (*removexattr) (const char *, const char *); + + /** Open directory + * + * This method should check if the open operation is permitted for + * this directory + * + * Introduced in version 2.3 + */ + int (*opendir) (const char *, struct fuse_file_info *); + + /** Read directory + * + * The filesystem may choose between two modes of operation: + * + * 1) The readdir implementation ignores the offset parameter, and + * passes zero to the filler function's offset. The filler + * function will not return '1' (unless an error happens), so the + * whole directory is read in a single readdir operation. + * + * 2) The readdir implementation keeps track of the offsets of the + * directory entries. It uses the offset parameter and always + * passes non-zero offset to the filler function. When the buffer + * is full (or an error happens) the filler function will return + * '1'. + * + * Introduced in version 2.3 + */ + int (*readdir) (const char *, void *, fuse_fill_dir_t, off_t, + struct fuse_file_info *); + + /** Release directory + * + * Introduced in version 2.3 + */ + int (*releasedir) (const char *, struct fuse_file_info *); + + /** Synchronize directory contents + * + * If the datasync parameter is non-zero, then only the user data + * should be flushed, not the meta data + * + * Introduced in version 2.3 + */ + int (*fsyncdir) (const char *, int, struct fuse_file_info *); + + /** + * Initialize filesystem + * + * The return value will passed in the private_data field of + * fuse_context to all file operations and as a parameter to the + * destroy() method. + * + * Introduced in version 2.3 + * Changed in version 2.6 + */ + void *(*init) (struct fuse_conn_info *conn); + + /** + * Clean up filesystem + * + * Called on filesystem exit. + * + * Introduced in version 2.3 + */ + void (*destroy) (void *); + + /** + * Check file access permissions + * + * This will be called for the access() system call. If the + * 'default_permissions' mount option is given, this method is not + * called. + * + * This method is not called under Linux kernel versions 2.4.x + * + * Introduced in version 2.5 + */ + int (*access) (const char *, int); + + /** + * Create and open a file + * + * If the file does not exist, first create it with the specified + * mode, and then open it. + * + * If this method is not implemented or under Linux kernel + * versions earlier than 2.6.15, the mknod() and open() methods + * will be called instead. + * + * Introduced in version 2.5 + */ + int (*create) (const char *, mode_t, struct fuse_file_info *); + + /** + * Change the size of an open file + * + * This method is called instead of the truncate() method if the + * truncation was invoked from an ftruncate() system call. + * + * If this method is not implemented or under Linux kernel + * versions earlier than 2.6.15, the truncate() method will be + * called instead. + * + * Introduced in version 2.5 + */ + int (*ftruncate) (const char *, off_t, struct fuse_file_info *); + + /** + * Get attributes from an open file + * + * This method is called instead of the getattr() method if the + * file information is available. + * + * Currently this is only called after the create() method if that + * is implemented (see above). Later it may be called for + * invocations of fstat() too. + * + * Introduced in version 2.5 + */ + int (*fgetattr) (const char *, struct stat *, struct fuse_file_info *); + + /** + * Perform POSIX file locking operation + * + * The cmd argument will be either F_GETLK, F_SETLK or F_SETLKW. + * + * For the meaning of fields in 'struct flock' see the man page + * for fcntl(2). The l_whence field will always be set to + * SEEK_SET. + * + * For checking lock ownership, the 'fuse_file_info->owner' + * argument must be used. + * + * For F_GETLK operation, the library will first check currently + * held locks, and if a conflicting lock is found it will return + * information without calling this method. This ensures, that + * for local locks the l_pid field is correctly filled in. The + * results may not be accurate in case of race conditions and in + * the presence of hard links, but it's unlikly that an + * application would rely on accurate GETLK results in these + * cases. If a conflicting lock is not found, this method will be + * called, and the filesystem may fill out l_pid by a meaningful + * value, or it may leave this field zero. + * + * For F_SETLK and F_SETLKW the l_pid field will be set to the pid + * of the process performing the locking operation. + * + * Note: if this method is not implemented, the kernel will still + * allow file locking to work locally. Hence it is only + * interesting for network filesystems and similar. + * + * Introduced in version 2.6 + */ + int (*lock) (const char *, struct fuse_file_info *, int cmd, + struct flock *); + + /** + * Change the access and modification times of a file with + * nanosecond resolution + * + * Introduced in version 2.6 + */ + int (*utimens) (const char *, const struct timespec tv[2]); + + /** + * Map block index within file to block index within device + * + * Note: This makes sense only for block device backed filesystems + * mounted with the 'blkdev' option + * + * Introduced in version 2.6 + */ + int (*bmap) (const char *, size_t blocksize, uint64_t *idx); + + /** + * Ioctl + * + * flags will have FUSE_IOCTL_COMPAT set for 32bit ioctls in + * 64bit environment. The size and direction of data is + * determined by _IOC_*() decoding of cmd. For _IOC_NONE, + * data will be NULL, for _IOC_WRITE data is out area, for + * _IOC_READ in area and if both are set in/out area. In all + * non-NULL cases, the area is of _IOC_SIZE(cmd) bytes. + * + * Introduced in version 2.8 + * + * Note : the unsigned long request submitted by the application + * is truncated to 32 bits, and forwarded as a signed int. + */ + int (*ioctl) (const char *, int cmd, void *arg, + struct fuse_file_info *, unsigned int flags, void *data); + + /* + * The flags below have been discarded, they should not be used + */ + unsigned int flag_nullpath_ok : 1; + /** + * Reserved flags, don't set + */ + unsigned int flag_reserved : 30; + +}; + +/** Extra context that may be needed by some filesystems + * + * The uid, gid and pid fields are not filled in case of a writepage + * operation. + */ +struct fuse_context { + /** Pointer to the fuse object */ + struct fuse *fuse; + + /** User ID of the calling process */ + uid_t uid; + + /** Group ID of the calling process */ + gid_t gid; + + /** Thread ID of the calling process */ + pid_t pid; + + /** Private filesystem data */ + void *private_data; + + /** Umask of the calling process (introduced in version 2.8) */ + mode_t umask; +}; + +/* ----------------------------------------------------------- * + * More detailed API * + * ----------------------------------------------------------- */ + +/** + * Create a new FUSE filesystem. + * + * @param ch the communication channel + * @param args argument vector + * @param op the filesystem operations + * @param op_size the size of the fuse_operations structure + * @param user_data user data supplied in the context during the init() method + * @return the created FUSE handle + */ +struct fuse *fuse_new(struct fuse_chan *ch, struct fuse_args *args, + const struct fuse_operations *op, size_t op_size, + void *user_data); + +/** + * Destroy the FUSE handle. + * + * The communication channel attached to the handle is also destroyed. + * + * NOTE: This function does not unmount the filesystem. If this is + * needed, call fuse_unmount() before calling this function. + * + * @param f the FUSE handle + */ +void fuse_destroy(struct fuse *f); + +/** + * FUSE event loop. + * + * Requests from the kernel are processed, and the appropriate + * operations are called. + * + * @param f the FUSE handle + * @return 0 if no error occurred, -1 otherwise + */ +int fuse_loop(struct fuse *f); + +/** + * Exit from event loop + * + * @param f the FUSE handle + */ +void fuse_exit(struct fuse *f); + +/** + * Get the current context + * + * The context is only valid for the duration of a filesystem + * operation, and thus must not be stored and used later. + * + * @return the context + */ +struct fuse_context *fuse_get_context(void); + +/** + * Check if a request has already been interrupted + * + * @param req request handle + * @return 1 if the request has been interrupted, 0 otherwise + */ +int fuse_interrupted(void); + +/* + * Stacking API + */ + +/** + * Fuse filesystem object + * + * This is opaque object represents a filesystem layer + */ +struct fuse_fs; + +/* + * These functions call the relevant filesystem operation, and return + * the result. + * + * If the operation is not defined, they return -ENOSYS, with the + * exception of fuse_fs_open, fuse_fs_release, fuse_fs_opendir, + * fuse_fs_releasedir and fuse_fs_statfs, which return 0. + */ + +int fuse_fs_getattr(struct fuse_fs *fs, const char *path, struct stat *buf); +int fuse_fs_fgetattr(struct fuse_fs *fs, const char *path, struct stat *buf, + struct fuse_file_info *fi); +int fuse_fs_rename(struct fuse_fs *fs, const char *oldpath, + const char *newpath); +int fuse_fs_unlink(struct fuse_fs *fs, const char *path); +int fuse_fs_rmdir(struct fuse_fs *fs, const char *path); +int fuse_fs_symlink(struct fuse_fs *fs, const char *linkname, + const char *path); +int fuse_fs_link(struct fuse_fs *fs, const char *oldpath, const char *newpath); +int fuse_fs_release(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_open(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_read(struct fuse_fs *fs, const char *path, char *buf, size_t size, + off_t off, struct fuse_file_info *fi); +int fuse_fs_write(struct fuse_fs *fs, const char *path, const char *buf, + size_t size, off_t off, struct fuse_file_info *fi); +int fuse_fs_fsync(struct fuse_fs *fs, const char *path, int datasync, + struct fuse_file_info *fi); +int fuse_fs_flush(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_statfs(struct fuse_fs *fs, const char *path, struct statvfs *buf); +int fuse_fs_opendir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_readdir(struct fuse_fs *fs, const char *path, void *buf, + fuse_fill_dir_t filler, off_t off, + struct fuse_file_info *fi); +int fuse_fs_fsyncdir(struct fuse_fs *fs, const char *path, int datasync, + struct fuse_file_info *fi); +int fuse_fs_releasedir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_create(struct fuse_fs *fs, const char *path, mode_t mode, + struct fuse_file_info *fi); +int fuse_fs_lock(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi, int cmd, struct flock *lock); +int fuse_fs_chmod(struct fuse_fs *fs, const char *path, mode_t mode); +int fuse_fs_chown(struct fuse_fs *fs, const char *path, uid_t uid, gid_t gid); +int fuse_fs_truncate(struct fuse_fs *fs, const char *path, off_t size); +int fuse_fs_ftruncate(struct fuse_fs *fs, const char *path, off_t size, + struct fuse_file_info *fi); +int fuse_fs_utimens(struct fuse_fs *fs, const char *path, + const struct timespec tv[2]); +int fuse_fs_access(struct fuse_fs *fs, const char *path, int mask); +int fuse_fs_readlink(struct fuse_fs *fs, const char *path, char *buf, + size_t len); +int fuse_fs_mknod(struct fuse_fs *fs, const char *path, mode_t mode, + dev_t rdev); +int fuse_fs_mkdir(struct fuse_fs *fs, const char *path, mode_t mode); +int fuse_fs_setxattr(struct fuse_fs *fs, const char *path, const char *name, + const char *value, size_t size, int flags); +int fuse_fs_getxattr(struct fuse_fs *fs, const char *path, const char *name, + char *value, size_t size); +int fuse_fs_listxattr(struct fuse_fs *fs, const char *path, char *list, + size_t size); +int fuse_fs_removexattr(struct fuse_fs *fs, const char *path, + const char *name); +int fuse_fs_bmap(struct fuse_fs *fs, const char *path, size_t blocksize, + uint64_t *idx); +int fuse_fs_ioctl(struct fuse_fs *fs, const char *path, int cmd, void *arg, + struct fuse_file_info *fi, unsigned int flags, void *data); +void fuse_fs_init(struct fuse_fs *fs, struct fuse_conn_info *conn); +void fuse_fs_destroy(struct fuse_fs *fs); + +/** + * Create a new fuse filesystem object + * + * This is usually called from the factory of a fuse module to create + * a new instance of a filesystem. + * + * @param op the filesystem operations + * @param op_size the size of the fuse_operations structure + * @param user_data user data supplied in the context during the init() method + * @return a new filesystem object + */ +struct fuse_fs *fuse_fs_new(const struct fuse_operations *op, size_t op_size, + void *user_data); + +#ifdef __SOLARIS__ + +/** + * Filesystem module + * + * Filesystem modules are registered with the FUSE_REGISTER_MODULE() + * macro. + * + * If the "-omodules=modname:..." option is present, filesystem + * objects are created and pushed onto the stack with the 'factory' + * function. + */ +struct fuse_module { + /** + * Name of filesystem + */ + const char *name; + + /** + * Factory for creating filesystem objects + * + * The function may use and remove options from 'args' that belong + * to this module. + * + * For now the 'fs' vector always contains exactly one filesystem. + * This is the filesystem which will be below the newly created + * filesystem in the stack. + * + * @param args the command line arguments + * @param fs NULL terminated filesystem object vector + * @return the new filesystem object + */ + struct fuse_fs *(*factory)(struct fuse_args *args, struct fuse_fs *fs[]); + + struct fuse_module *next; + struct fusemod_so *so; + int ctr; +}; + +#endif /* __SOLARIS__ */ + +/* ----------------------------------------------------------- * + * Advanced API for event handling, don't worry about this... * + * ----------------------------------------------------------- */ + +/* NOTE: the following functions are deprecated, and will be removed + from the 3.0 API. Use the lowlevel session functions instead */ + +/** Get session from fuse object */ +struct fuse_session *fuse_get_session(struct fuse *f); + +#ifdef __cplusplus +} +#endif + +#endif /* _FUSE_H_ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_common.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_common.h new file mode 100644 index 0000000..d1e3199 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_common.h @@ -0,0 +1,246 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +/** @file */ + +#if !defined(_FUSE_H_) && !defined(_FUSE_LOWLEVEL_H_) +#error "Never include directly; use or instead." +#endif + +#ifndef _FUSE_COMMON_H_ +#define _FUSE_COMMON_H_ + +#include "fuse_opt.h" +#include /* temporary */ +#include + +/** Major version of FUSE library interface */ +#define FUSE_MAJOR_VERSION 2 + +/** Minor version of FUSE library interface */ +#ifdef POSIXACLS +#define FUSE_MINOR_VERSION 8 +#else +#define FUSE_MINOR_VERSION 7 +#endif + +#define FUSE_MAKE_VERSION(maj, min) ((maj) * 10 + (min)) +#define FUSE_VERSION FUSE_MAKE_VERSION(FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION) + +/* This interface uses 64 bit off_t */ +#if defined(__SOLARIS__) && !defined(__x86_64__) && (_FILE_OFFSET_BITS != 64) +#error Please add -D_FILE_OFFSET_BITS=64 to your compile flags! +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef POSIXACLS +/* + * FUSE_CAP_DONT_MASK: don't apply umask to file mode on create operations + * FUSE_CAP_POSIX_ACL: process Posix ACLs within the kernel + */ +#define FUSE_CAP_DONT_MASK (1 << 6) +#define FUSE_CAP_POSIX_ACL (1 << 18) +#endif + +#define FUSE_CAP_BIG_WRITES (1 << 5) +#define FUSE_CAP_IOCTL_DIR (1 << 11) + +/** + * Ioctl flags + * + * FUSE_IOCTL_COMPAT: 32bit compat ioctl on 64bit machine + * FUSE_IOCTL_UNRESTRICTED: not restricted to well-formed ioctls, retry allowed + * FUSE_IOCTL_RETRY: retry with new iovecs + * FUSE_IOCTL_DIR: is a directory + */ +#define FUSE_IOCTL_COMPAT (1 << 0) +#define FUSE_IOCTL_UNRESTRICTED (1 << 1) +#define FUSE_IOCTL_RETRY (1 << 2) +#define FUSE_IOCTL_DIR (1 << 4) + +#define FUSE_IOCTL_MAX_IOV 256 + +/** + * Information about open files + * + * Changed in version 2.5 + */ +struct fuse_file_info { + /** Open flags. Available in open() and release() */ + int flags; + + /** Old file handle, don't use */ + unsigned long fh_old; + + /** In case of a write operation indicates if this was caused by a + writepage */ + int writepage; + + /** Can be filled in by open, to use direct I/O on this file. + Introduced in version 2.4 */ + unsigned int direct_io : 1; + + /** Can be filled in by open, to indicate, that cached file data + need not be invalidated. Introduced in version 2.4 */ + unsigned int keep_cache : 1; + + /** Indicates a flush operation. Set in flush operation, also + maybe set in highlevel lock operation and lowlevel release + operation. Introduced in version 2.6 */ + unsigned int flush : 1; + + /** Padding. Do not use*/ + unsigned int padding : 29; + + /** File handle. May be filled in by filesystem in open(). + Available in all other file operations */ + uint64_t fh; + + /** Lock owner id. Available in locking operations and flush */ + uint64_t lock_owner; +}; + +/** + * Connection information, passed to the ->init() method + * + * Some of the elements are read-write, these can be changed to + * indicate the value requested by the filesystem. The requested + * value must usually be smaller than the indicated value. + */ +struct fuse_conn_info { + /** + * Major version of the protocol (read-only) + */ + unsigned proto_major; + + /** + * Minor version of the protocol (read-only) + */ + unsigned proto_minor; + + /** + * Is asynchronous read supported (read-write) + */ + unsigned async_read; + + /** + * Maximum size of the write buffer + */ + unsigned max_write; + + /** + * Maximum readahead + */ + unsigned max_readahead; + + unsigned capable; + unsigned want; + /** + * For future use. + */ + unsigned reserved[25]; + }; + +struct fuse_session; +struct fuse_chan; + +/** + * Create a FUSE mountpoint + * + * Returns a control file descriptor suitable for passing to + * fuse_new() + * + * @param mountpoint the mount point path + * @param args argument vector + * @return the communication channel on success, NULL on failure + */ +struct fuse_chan *fuse_mount(const char *mountpoint, struct fuse_args *args); + +/** + * Umount a FUSE mountpoint + * + * @param mountpoint the mount point path + * @param ch the communication channel + */ +void fuse_unmount(const char *mountpoint, struct fuse_chan *ch); + +#ifdef __SOLARIS__ +/** + * Parse common options + * + * The following options are parsed: + * + * '-f' foreground + * '-d' '-odebug' foreground, but keep the debug option + * '-s' single threaded + * '-h' '--help' help + * '-ho' help without header + * '-ofsname=..' file system name, if not present, then set to the program + * name + * + * All parameters may be NULL + * + * @param args argument vector + * @param mountpoint the returned mountpoint, should be freed after use + * @param multithreaded set to 1 unless the '-s' option is present + * @param foreground set to 1 if one of the relevant options is present + * @return 0 on success, -1 on failure + */ +int fuse_parse_cmdline(struct fuse_args *args, char **mountpoint, + int *multithreaded, int *foreground); + +/** + * Go into the background + * + * @param foreground if true, stay in the foreground + * @return 0 on success, -1 on failure + */ +int fuse_daemonize(int foreground); + +#endif /* __SOLARIS__ */ + +/** + * Get the version of the library + * + * @return the version + */ +int fuse_version(void); + +/* ----------------------------------------------------------- * + * Signal handling * + * ----------------------------------------------------------- */ + +/** + * Exit session on HUP, TERM and INT signals and ignore PIPE signal + * + * Stores session in a global variable. May only be called once per + * process until fuse_remove_signal_handlers() is called. + * + * @param se the session to exit + * @return 0 on success, -1 on failure + */ +int fuse_set_signal_handlers(struct fuse_session *se); + +/** + * Restore default signal handlers + * + * Resets global session. After this fuse_set_signal_handlers() may + * be called again. + * + * @param se the same session as given in fuse_set_signal_handlers() + */ +void fuse_remove_signal_handlers(struct fuse_session *se); + +#ifdef __cplusplus +} +#endif + +#endif /* _FUSE_COMMON_H_ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_kernel.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_kernel.h new file mode 100644 index 0000000..6e7eb18 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_kernel.h @@ -0,0 +1,441 @@ +/* + This file defines the kernel interface of FUSE + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. + + This -- and only this -- header file may also be distributed under + the terms of the BSD Licence as follows: + + Copyright (C) 2001-2007 Miklos Szeredi. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + * + * 7.12 + * - add umask flag to input argument of open, mknod and mkdir +*/ + +#ifndef linux +#include +#define __u64 uint64_t +#define __u32 uint32_t +#define __s32 int32_t +#else +#include +#include +#endif + +/** Version number of this interface */ +#define FUSE_KERNEL_VERSION 7 + +/** Minor version number of this interface + * We introduce ourself as 7.18 (Posix ACLS : 7.12, IOCTL_DIR : 7.18) + * and we expect features features defined for 7.18, but not implemented + * here to not be triggered by ntfs-3g. + */ +#define FUSE_KERNEL_MINOR_VERSION 18 + +/* + * For binary compatibility with old kernels we accept falling back + * to 7.12 or earlier maximum version supported by the kernel + */ + +#define FUSE_KERNEL_MAJOR_FALLBACK 7 +#define FUSE_KERNEL_MINOR_FALLBACK 12 + +/** The node ID of the root inode */ +#define FUSE_ROOT_ID 1 + +/** The major number of the fuse character device */ +#define FUSE_MAJOR MISC_MAJOR + +/** The minor number of the fuse character device */ +#define FUSE_MINOR 229 + +/* Make sure all structures are padded to 64bit boundary, so 32bit + userspace works under 64bit kernels */ + +struct fuse_attr { + __u64 ino; + __u64 size; + __u64 blocks; + __u64 atime; + __u64 mtime; + __u64 ctime; + __u32 atimensec; + __u32 mtimensec; + __u32 ctimensec; + __u32 mode; + __u32 nlink; + __u32 uid; + __u32 gid; + __u32 rdev; + __u64 filling; /* JPA needed for minor >= 12, but meaning unknown */ +}; + +struct fuse_kstatfs { + __u64 blocks; + __u64 bfree; + __u64 bavail; + __u64 files; + __u64 ffree; + __u32 bsize; + __u32 namelen; + __u32 frsize; + __u32 padding; + __u32 spare[6]; +}; + +struct fuse_file_lock { + __u64 start; + __u64 end; + __u32 type; + __u32 pid; /* tgid */ +}; + +/** + * Bitmasks for fuse_setattr_in.valid + */ +#define FATTR_MODE (1 << 0) +#define FATTR_UID (1 << 1) +#define FATTR_GID (1 << 2) +#define FATTR_SIZE (1 << 3) +#define FATTR_ATIME (1 << 4) +#define FATTR_MTIME (1 << 5) +#define FATTR_FH (1 << 6) + +/** + * Flags returned by the OPEN request + * + * FOPEN_DIRECT_IO: bypass page cache for this open file + * FOPEN_KEEP_CACHE: don't invalidate the data cache on open + */ +#define FOPEN_DIRECT_IO (1 << 0) +#define FOPEN_KEEP_CACHE (1 << 1) + +/** + * INIT request/reply flags + * FUSE_BIG_WRITES: allow big writes to be issued to the file system + * FUSE_DONT_MASK: don't apply umask to file mode on create operations + * FUSE_HAS_IOCTL_DIR: kernel supports ioctl on directories + * FUSE_POSIX_ACL: kernel supports Posix ACLs + */ +#define FUSE_ASYNC_READ (1 << 0) +#define FUSE_POSIX_LOCKS (1 << 1) +#define FUSE_BIG_WRITES (1 << 5) +#define FUSE_DONT_MASK (1 << 6) +#define FUSE_HAS_IOCTL_DIR (1 << 11) +#define FUSE_POSIX_ACL (1 << 19) + +/** + * Release flags + */ +#define FUSE_RELEASE_FLUSH (1 << 0) + +enum fuse_opcode { + FUSE_LOOKUP = 1, + FUSE_FORGET = 2, /* no reply */ + FUSE_GETATTR = 3, + FUSE_SETATTR = 4, + FUSE_READLINK = 5, + FUSE_SYMLINK = 6, + FUSE_MKNOD = 8, + FUSE_MKDIR = 9, + FUSE_UNLINK = 10, + FUSE_RMDIR = 11, + FUSE_RENAME = 12, + FUSE_LINK = 13, + FUSE_OPEN = 14, + FUSE_READ = 15, + FUSE_WRITE = 16, + FUSE_STATFS = 17, + FUSE_RELEASE = 18, + FUSE_FSYNC = 20, + FUSE_SETXATTR = 21, + FUSE_GETXATTR = 22, + FUSE_LISTXATTR = 23, + FUSE_REMOVEXATTR = 24, + FUSE_FLUSH = 25, + FUSE_INIT = 26, + FUSE_OPENDIR = 27, + FUSE_READDIR = 28, + FUSE_RELEASEDIR = 29, + FUSE_FSYNCDIR = 30, + FUSE_GETLK = 31, + FUSE_SETLK = 32, + FUSE_SETLKW = 33, + FUSE_ACCESS = 34, + FUSE_CREATE = 35, + FUSE_INTERRUPT = 36, + FUSE_BMAP = 37, + FUSE_DESTROY = 38, + FUSE_IOCTL = 39, +}; + +/* The read buffer is required to be at least 8k, but may be much larger */ +#define FUSE_MIN_READ_BUFFER 8192 +#define FUSE_COMPAT_ENTRY_OUT_SIZE 120 /* JPA */ + +struct fuse_entry_out { + __u64 nodeid; /* Inode ID */ + __u64 generation; /* Inode generation: nodeid:gen must + be unique for the fs's lifetime */ + __u64 entry_valid; /* Cache timeout for the name */ + __u64 attr_valid; /* Cache timeout for the attributes */ + __u32 entry_valid_nsec; + __u32 attr_valid_nsec; + struct fuse_attr attr; +}; + +struct fuse_forget_in { + __u64 nlookup; +}; + +#define FUSE_COMPAT_FUSE_ATTR_OUT_SIZE 96 /* JPA */ + +struct fuse_attr_out { + __u64 attr_valid; /* Cache timeout for the attributes */ + __u32 attr_valid_nsec; + __u32 dummy; + struct fuse_attr attr; +}; + +#define FUSE_COMPAT_MKNOD_IN_SIZE 8 + +struct fuse_mknod_in { + __u32 mode; + __u32 rdev; + __u32 umask; + __u32 padding; +}; + +struct fuse_mkdir_in { + __u32 mode; + __u32 umask; +}; + +struct fuse_rename_in { + __u64 newdir; +}; + +struct fuse_link_in { + __u64 oldnodeid; +}; + +struct fuse_setattr_in { + __u32 valid; + __u32 padding; + __u64 fh; + __u64 size; + __u64 unused1; + __u64 atime; + __u64 mtime; + __u64 unused2; + __u32 atimensec; + __u32 mtimensec; + __u32 unused3; + __u32 mode; + __u32 unused4; + __u32 uid; + __u32 gid; + __u32 unused5; +}; + +struct fuse_open_in { + __u32 flags; + __u32 mode; /* unused for protocol < 7.12 */ +}; + +struct fuse_create_in { + __u32 flags; + __u32 mode; + __u32 umask; + __u32 padding; +}; + +struct fuse_open_out { + __u64 fh; + __u32 open_flags; + __u32 padding; +}; + +struct fuse_release_in { + __u64 fh; + __u32 flags; + __u32 release_flags; + __u64 lock_owner; +}; + +struct fuse_flush_in { + __u64 fh; + __u32 unused; + __u32 padding; + __u64 lock_owner; +}; + +struct fuse_read_in { + __u64 fh; + __u64 offset; + __u32 size; + __u32 padding; +}; + +#define FUSE_COMPAT_WRITE_IN_SIZE 24 /* JPA */ + +struct fuse_write_in { + __u64 fh; + __u64 offset; + __u32 size; + __u32 write_flags; + __u64 lock_owner; /* JPA */ + __u32 flags; /* JPA */ + __u32 padding; /* JPA */ +}; + +struct fuse_write_out { + __u32 size; + __u32 padding; +}; + +#define FUSE_COMPAT_STATFS_SIZE 48 + +struct fuse_statfs_out { + struct fuse_kstatfs st; +}; + +struct fuse_fsync_in { + __u64 fh; + __u32 fsync_flags; + __u32 padding; +}; + +struct fuse_setxattr_in { + __u32 size; + __u32 flags; +}; + +struct fuse_getxattr_in { + __u32 size; + __u32 padding; +}; + +struct fuse_getxattr_out { + __u32 size; + __u32 padding; +}; + +struct fuse_lk_in { + __u64 fh; + __u64 owner; + struct fuse_file_lock lk; +}; + +struct fuse_lk_out { + struct fuse_file_lock lk; +}; + +struct fuse_access_in { + __u32 mask; + __u32 padding; +}; + +struct fuse_init_in { + __u32 major; + __u32 minor; + __u32 max_readahead; + __u32 flags; +}; + +struct fuse_init_out { + __u32 major; + __u32 minor; + __u32 max_readahead; + __u32 flags; + __u32 unused; + __u32 max_write; +}; + +struct fuse_interrupt_in { + __u64 unique; +}; + +struct fuse_bmap_in { + __u64 block; + __u32 blocksize; + __u32 padding; +}; + +struct fuse_bmap_out { + __u64 block; +}; + +struct fuse_ioctl_in { + __u64 fh; + __u32 flags; + __u32 cmd; + __u64 arg; + __u32 in_size; + __u32 out_size; +}; + +struct fuse_ioctl_iovec { + __u64 base; + __u64 len; +}; + +struct fuse_ioctl_out { + __s32 result; + __u32 flags; + __u32 in_iovs; + __u32 out_iovs; +}; + +struct fuse_in_header { + __u32 len; + __u32 opcode; + __u64 unique; + __u64 nodeid; + __u32 uid; + __u32 gid; + __u32 pid; + __u32 padding; +}; + +struct fuse_out_header { + __u32 len; + __s32 error; + __u64 unique; +}; + +struct fuse_dirent { + __u64 ino; + __u64 off; + __u32 namelen; + __u32 type; + char name[0]; +}; + +#define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name) +#define FUSE_DIRENT_ALIGN(x) (((x) + sizeof(__u64) - 1) & ~(sizeof(__u64) - 1)) +#define FUSE_DIRENT_SIZE(d) \ + FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + (d)->namelen) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_lowlevel.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_lowlevel.h new file mode 100644 index 0000000..3b10c35 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_lowlevel.h @@ -0,0 +1,1417 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#ifndef _FUSE_LOWLEVEL_H_ +#define _FUSE_LOWLEVEL_H_ + +/** @file + * + * Low level API + */ + +#include "fuse_common.h" + +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* ----------------------------------------------------------- * + * Miscellaneous definitions * + * ----------------------------------------------------------- */ + +/** The node ID of the root inode */ +#define FUSE_ROOT_ID 1 + +/** Inode number type */ +typedef unsigned long fuse_ino_t; + +/** Request pointer type */ +typedef struct fuse_req *fuse_req_t; + +/** + * Session + * + * This provides hooks for processing requests, and exiting + */ +struct fuse_session; + +/** + * Channel + * + * A communication channel, providing hooks for sending and receiving + * messages + */ +struct fuse_chan; + +/** Directory entry parameters supplied to fuse_reply_entry() */ +struct fuse_entry_param { + /** Unique inode number + * + * In lookup, zero means negative entry (from version 2.5) + * Returning ENOENT also means negative entry, but by setting zero + * ino the kernel may cache negative entries for entry_timeout + * seconds. + */ + fuse_ino_t ino; + + /** Generation number for this entry. + * + * The ino/generation pair should be unique for the filesystem's + * lifetime. It must be non-zero, otherwise FUSE will treat it as an + * error. + */ + unsigned long generation; + + /** Inode attributes. + * + * Even if attr_timeout == 0, attr must be correct. For example, + * for open(), FUSE uses attr.st_size from lookup() to determine + * how many bytes to request. If this value is not correct, + * incorrect data will be returned. + */ + struct stat attr; + + /** Validity timeout (in seconds) for the attributes */ + double attr_timeout; + + /** Validity timeout (in seconds) for the name */ + double entry_timeout; +}; + +/** Additional context associated with requests */ +struct fuse_ctx { + /** User ID of the calling process */ + uid_t uid; + + /** Group ID of the calling process */ + gid_t gid; + + /** Thread ID of the calling process */ + pid_t pid; + + /** Umask of the calling process (introduced in version 2.8) */ + mode_t umask; +}; + +/* 'to_set' flags in setattr */ +#define FUSE_SET_ATTR_MODE (1 << 0) +#define FUSE_SET_ATTR_UID (1 << 1) +#define FUSE_SET_ATTR_GID (1 << 2) +#define FUSE_SET_ATTR_SIZE (1 << 3) +#define FUSE_SET_ATTR_ATIME (1 << 4) +#define FUSE_SET_ATTR_MTIME (1 << 5) +#define FUSE_SET_ATTR_ATIME_NOW (1 << 7) +#define FUSE_SET_ATTR_MTIME_NOW (1 << 8) + +/* ----------------------------------------------------------- * + * Request methods and replies * + * ----------------------------------------------------------- */ + +/** + * Low level filesystem operations + * + * Most of the methods (with the exception of init and destroy) + * receive a request handle (fuse_req_t) as their first argument. + * This handle must be passed to one of the specified reply functions. + * + * This may be done inside the method invocation, or after the call + * has returned. The request handle is valid until one of the reply + * functions is called. + * + * Other pointer arguments (name, fuse_file_info, etc) are not valid + * after the call has returned, so if they are needed later, their + * contents have to be copied. + * + * The filesystem sometimes needs to handle a return value of -ENOENT + * from the reply function, which means, that the request was + * interrupted, and the reply discarded. For example if + * fuse_reply_open() return -ENOENT means, that the release method for + * this file will not be called. + */ +struct fuse_lowlevel_ops { + /** + * Initialize filesystem + * + * Called before any other filesystem method + * + * There's no reply to this function + * + * @param userdata the user data passed to fuse_lowlevel_new() + */ + void (*init) (void *userdata, struct fuse_conn_info *conn); + + /** + * Clean up filesystem + * + * Called on filesystem exit + * + * There's no reply to this function + * + * @param userdata the user data passed to fuse_lowlevel_new() + */ + void (*destroy) (void *userdata); + + /** + * Look up a directory entry by name and get its attributes. + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name the name to look up + */ + void (*lookup) (fuse_req_t req, fuse_ino_t parent, const char *name); + + /** + * Forget about an inode + * + * The nlookup parameter indicates the number of lookups + * previously performed on this inode. + * + * If the filesystem implements inode lifetimes, it is recommended + * that inodes acquire a single reference on each lookup, and lose + * nlookup references on each forget. + * + * The filesystem may ignore forget calls, if the inodes don't + * need to have a limited lifetime. + * + * On unmount it is not guaranteed, that all referenced inodes + * will receive a forget message. + * + * Valid replies: + * fuse_reply_none + * + * @param req request handle + * @param ino the inode number + * @param nlookup the number of lookups to forget + */ + void (*forget) (fuse_req_t req, fuse_ino_t ino, unsigned long nlookup); + + /** + * Get file attributes + * + * Valid replies: + * fuse_reply_attr + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi for future use, currently always NULL + */ + void (*getattr) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Set file attributes + * + * In the 'attr' argument only members indicated by the 'to_set' + * bitmask contain valid values. Other members contain undefined + * values. + * + * If the setattr was invoked from the ftruncate() system call + * under Linux kernel versions 2.6.15 or later, the fi->fh will + * contain the value set by the open method or will be undefined + * if the open method didn't set any value. Otherwise (not + * ftruncate call, or kernel version earlier than 2.6.15) the fi + * parameter will be NULL. + * + * Valid replies: + * fuse_reply_attr + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param attr the attributes + * @param to_set bit mask of attributes which should be set + * @param fi file information, or NULL + * + * Changed in version 2.5: + * file information filled in for ftruncate + */ + void (*setattr) (fuse_req_t req, fuse_ino_t ino, struct stat *attr, + int to_set, struct fuse_file_info *fi); + + /** + * Read symbolic link + * + * Valid replies: + * fuse_reply_readlink + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + */ + void (*readlink) (fuse_req_t req, fuse_ino_t ino); + + /** + * Create file node + * + * Create a regular file, character device, block device, fifo or + * socket node. + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to create + * @param mode file type and mode with which to create the new file + * @param rdev the device number (only valid if created file is a device) + */ + void (*mknod) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, dev_t rdev); + + /** + * Create a directory + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to create + * @param mode with which to create the new file + */ + void (*mkdir) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode); + + /** + * Remove a file + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to remove + */ + void (*unlink) (fuse_req_t req, fuse_ino_t parent, const char *name); + + /** + * Remove a directory + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to remove + */ + void (*rmdir) (fuse_req_t req, fuse_ino_t parent, const char *name); + + /** + * Create a symbolic link + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param link the contents of the symbolic link + * @param parent inode number of the parent directory + * @param name to create + */ + void (*symlink) (fuse_req_t req, const char *link, fuse_ino_t parent, + const char *name); + + /** Rename a file + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the old parent directory + * @param name old name + * @param newparent inode number of the new parent directory + * @param newname new name + */ + void (*rename) (fuse_req_t req, fuse_ino_t parent, const char *name, + fuse_ino_t newparent, const char *newname); + + /** + * Create a hard link + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param ino the old inode number + * @param newparent inode number of the new parent directory + * @param newname new name to create + */ + void (*link) (fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, + const char *newname); + + /** + * Open a file + * + * Open flags (with the exception of O_CREAT, O_EXCL, O_NOCTTY and + * O_TRUNC) are available in fi->flags. + * + * Filesystem may store an arbitrary file handle (pointer, index, + * etc) in fi->fh, and use this in other all other file operations + * (read, write, flush, release, fsync). + * + * Filesystem may also implement stateless file I/O and not store + * anything in fi->fh. + * + * There are also some flags (direct_io, keep_cache) which the + * filesystem may set in fi, to change the way the file is opened. + * See fuse_file_info structure in for more details. + * + * Valid replies: + * fuse_reply_open + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*open) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Read data + * + * Read should send exactly the number of bytes requested except + * on EOF or error, otherwise the rest of the data will be + * substituted with zeroes. An exception to this is when the file + * has been opened in 'direct_io' mode, in which case the return + * value of the read system call will reflect the return value of + * this operation. + * + * fi->fh will contain the value set by the open method, or will + * be undefined if the open method didn't set any value. + * + * Valid replies: + * fuse_reply_buf + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param size number of bytes to read + * @param off offset to read from + * @param fi file information + */ + void (*read) (fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, + struct fuse_file_info *fi); + + /** + * Write data + * + * Write should return exactly the number of bytes requested + * except on error. An exception to this is when the file has + * been opened in 'direct_io' mode, in which case the return value + * of the write system call will reflect the return value of this + * operation. + * + * fi->fh will contain the value set by the open method, or will + * be undefined if the open method didn't set any value. + * + * Valid replies: + * fuse_reply_write + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param buf data to write + * @param size number of bytes to write + * @param off offset to write to + * @param fi file information + */ + void (*write) (fuse_req_t req, fuse_ino_t ino, const char *buf, + size_t size, off_t off, struct fuse_file_info *fi); + + /** + * Flush method + * + * This is called on each close() of the opened file. + * + * Since file descriptors can be duplicated (dup, dup2, fork), for + * one open call there may be many flush calls. + * + * Filesystems shouldn't assume that flush will always be called + * after some writes, or that if will be called at all. + * + * fi->fh will contain the value set by the open method, or will + * be undefined if the open method didn't set any value. + * + * NOTE: the name of the method is misleading, since (unlike + * fsync) the filesystem is not forced to flush pending writes. + * One reason to flush data, is if the filesystem wants to return + * write errors. + * + * If the filesystem supports file locking operations (setlk, + * getlk) it should remove all locks belonging to 'fi->owner'. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*flush) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Release an open file + * + * Release is called when there are no more references to an open + * file: all file descriptors are closed and all memory mappings + * are unmapped. + * + * For every open call there will be exactly one release call. + * + * The filesystem may reply with an error, but error values are + * not returned to close() or munmap() which triggered the + * release. + * + * fi->fh will contain the value set by the open method, or will + * be undefined if the open method didn't set any value. + * fi->flags will contain the same flags as for open. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*release) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Synchronize file contents + * + * If the datasync parameter is non-zero, then only the user data + * should be flushed, not the meta data. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param datasync flag indicating if only data should be flushed + * @param fi file information + */ + void (*fsync) (fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *fi); + + /** + * Open a directory + * + * Filesystem may store an arbitrary file handle (pointer, index, + * etc) in fi->fh, and use this in other all other directory + * stream operations (readdir, releasedir, fsyncdir). + * + * Filesystem may also implement stateless directory I/O and not + * store anything in fi->fh, though that makes it impossible to + * implement standard conforming directory stream operations in + * case the contents of the directory can change between opendir + * and releasedir. + * + * Valid replies: + * fuse_reply_open + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*opendir) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Read directory + * + * Send a buffer filled using fuse_add_direntry(), with size not + * exceeding the requested size. Send an empty buffer on end of + * stream. + * + * fi->fh will contain the value set by the opendir method, or + * will be undefined if the opendir method didn't set any value. + * + * Valid replies: + * fuse_reply_buf + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param size maximum number of bytes to send + * @param off offset to continue reading the directory stream + * @param fi file information + */ + void (*readdir) (fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, + struct fuse_file_info *fi); + + /** + * Release an open directory + * + * For every opendir call there will be exactly one releasedir + * call. + * + * fi->fh will contain the value set by the opendir method, or + * will be undefined if the opendir method didn't set any value. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*releasedir) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Synchronize directory contents + * + * If the datasync parameter is non-zero, then only the directory + * contents should be flushed, not the meta data. + * + * fi->fh will contain the value set by the opendir method, or + * will be undefined if the opendir method didn't set any value. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param datasync flag indicating if only data should be flushed + * @param fi file information + */ + void (*fsyncdir) (fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *fi); + + /** + * Get file system statistics + * + * Valid replies: + * fuse_reply_statfs + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number, zero means "undefined" + */ + void (*statfs) (fuse_req_t req, fuse_ino_t ino); + + /** + * Set an extended attribute + * + * Valid replies: + * fuse_reply_err + */ + void (*setxattr) (fuse_req_t req, fuse_ino_t ino, const char *name, + const char *value, size_t size, int flags); + + /** + * Get an extended attribute + * + * If size is zero, the size of the value should be sent with + * fuse_reply_xattr. + * + * If the size is non-zero, and the value fits in the buffer, the + * value should be sent with fuse_reply_buf. + * + * If the size is too small for the value, the ERANGE error should + * be sent. + * + * Valid replies: + * fuse_reply_buf + * fuse_reply_xattr + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param name of the extended attribute + * @param size maximum size of the value to send + */ + void (*getxattr) (fuse_req_t req, fuse_ino_t ino, const char *name, + size_t size); + + /** + * List extended attribute names + * + * If size is zero, the total size of the attribute list should be + * sent with fuse_reply_xattr. + * + * If the size is non-zero, and the null character separated + * attribute list fits in the buffer, the list should be sent with + * fuse_reply_buf. + * + * If the size is too small for the list, the ERANGE error should + * be sent. + * + * Valid replies: + * fuse_reply_buf + * fuse_reply_xattr + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param size maximum size of the list to send + */ + void (*listxattr) (fuse_req_t req, fuse_ino_t ino, size_t size); + + /** + * Remove an extended attribute + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param name of the extended attribute + */ + void (*removexattr) (fuse_req_t req, fuse_ino_t ino, const char *name); + + /** + * Check file access permissions + * + * This will be called for the access() system call. If the + * 'default_permissions' mount option is given, this method is not + * called. + * + * This method is not called under Linux kernel versions 2.4.x + * + * Introduced in version 2.5 + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param mask requested access mode + */ + void (*access) (fuse_req_t req, fuse_ino_t ino, int mask); + + /** + * Create and open a file + * + * If the file does not exist, first create it with the specified + * mode, and then open it. + * + * Open flags (with the exception of O_NOCTTY) are available in + * fi->flags. + * + * Filesystem may store an arbitrary file handle (pointer, index, + * etc) in fi->fh, and use this in other all other file operations + * (read, write, flush, release, fsync). + * + * There are also some flags (direct_io, keep_cache) which the + * filesystem may set in fi, to change the way the file is opened. + * See fuse_file_info structure in for more details. + * + * If this method is not implemented or under Linux kernel + * versions earlier than 2.6.15, the mknod() and open() methods + * will be called instead. + * + * Introduced in version 2.5 + * + * Valid replies: + * fuse_reply_create + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to create + * @param mode file type and mode with which to create the new file + * @param fi file information + */ + void (*create) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, struct fuse_file_info *fi); + + /** + * Test for a POSIX file lock + * + * Introduced in version 2.6 + * + * Valid replies: + * fuse_reply_lock + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + * @param lock the region/type to test + */ + void (*getlk) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct flock *lock); + + /** + * Acquire, modify or release a POSIX file lock + * + * For POSIX threads (NPTL) there's a 1-1 relation between pid and + * owner, but otherwise this is not always the case. For checking + * lock ownership, 'fi->owner' must be used. The l_pid field in + * 'struct flock' should only be used to fill in this field in + * getlk(). + * + * Note: if the locking methods are not implemented, the kernel + * will still allow file locking to work locally. Hence these are + * only interesting for network filesystems and similar. + * + * Introduced in version 2.6 + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + * @param lock the region/type to test + * @param sleep locking operation may sleep + */ + void (*setlk) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, + struct flock *lock, int sleep); + + /** + * Map block index within file to block index within device + * + * Note: This makes sense only for block device backed filesystems + * mounted with the 'blkdev' option + * + * Introduced in version 2.6 + * + * Valid replies: + * fuse_reply_bmap + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param blocksize unit of block index + * @param idx block index within file + */ + void (*bmap) (fuse_req_t req, fuse_ino_t ino, size_t blocksize, + uint64_t idx); + /** + * Ioctl + * + * Note: For unrestricted ioctls (not allowed for FUSE + * servers), data in and out areas can be discovered by giving + * iovs and setting FUSE_IOCTL_RETRY in @flags. For + * restricted ioctls, kernel prepares in/out data area + * according to the information encoded in cmd. + * + * Introduced in version 2.8 + * + * Note : the unsigned long request submitted by the application + * is truncated to 32 bits, and forwarded as a signed int. + * + * Valid replies: + * fuse_reply_ioctl_retry + * fuse_reply_ioctl + * fuse_reply_ioctl_iov + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param cmd ioctl command + * @param arg ioctl argument + * @param fi file information + * @param flags for FUSE_IOCTL_* flags + * @param in_buf data fetched from the caller + * @param in_bufsz number of fetched bytes + * @param out_bufsz maximum size of output data + */ + void (*ioctl) (fuse_req_t req, fuse_ino_t ino, int cmd, void *arg, + struct fuse_file_info *fi, unsigned flags, + const void *in_buf, size_t in_bufsz, size_t out_bufsz); + +}; + +/** + * Reply with an error code or success + * + * Possible requests: + * all except forget + * + * unlink, rmdir, rename, flush, release, fsync, fsyncdir, setxattr, + * removexattr and setlk may send a zero code + * + * @param req request handle + * @param err the positive error value, or zero for success + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_err(fuse_req_t req, int err); + +/** + * Don't send reply + * + * Possible requests: + * forget + * + * @param req request handle + */ +void fuse_reply_none(fuse_req_t req); + +/** + * Reply with a directory entry + * + * Possible requests: + * lookup, mknod, mkdir, symlink, link + * + * @param req request handle + * @param e the entry parameters + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e); + +/** + * Reply with a directory entry and open parameters + * + * currently the following members of 'fi' are used: + * fh, direct_io, keep_cache + * + * Possible requests: + * create + * + * @param req request handle + * @param e the entry parameters + * @param fi file information + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, + const struct fuse_file_info *fi); + +/** + * Reply with attributes + * + * Possible requests: + * getattr, setattr + * + * @param req request handle + * @param the attributes + * @param attr_timeout validity timeout (in seconds) for the attributes + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_attr(fuse_req_t req, const struct stat *attr, + double attr_timeout); + +/** + * Reply with the contents of a symbolic link + * + * Possible requests: + * readlink + * + * @param req request handle + * @param link symbolic link contents + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_readlink(fuse_req_t req, const char *link); + +/** + * Reply with open parameters + * + * currently the following members of 'fi' are used: + * fh, direct_io, keep_cache + * + * Possible requests: + * open, opendir + * + * @param req request handle + * @param fi file information + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi); + +/** + * Reply with number of bytes written + * + * Possible requests: + * write + * + * @param req request handle + * @param count the number of bytes written + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_write(fuse_req_t req, size_t count); + +/** + * Reply with data + * + * Possible requests: + * read, readdir, getxattr, listxattr + * + * @param req request handle + * @param buf buffer containing data + * @param size the size of data in bytes + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size); + +#ifdef POSIXACLS +/** + * Reply with data vector + * + * Possible requests: + * read, readdir, getxattr, listxattr + * + * @param req request handle + * @param iov the vector containing the data + * @param count the size of vector + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count); +#endif + +/** + * Reply with filesystem statistics + * + * Possible requests: + * statfs + * + * @param req request handle + * @param stbuf filesystem statistics + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf); + +/** + * Reply with needed buffer size + * + * Possible requests: + * getxattr, listxattr + * + * @param req request handle + * @param count the buffer size needed in bytes + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_xattr(fuse_req_t req, size_t count); + +/** + * Reply with file lock information + * + * Possible requests: + * getlk + * + * @param req request handle + * @param lock the lock information + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_lock(fuse_req_t req, struct flock *lock); + +/** + * Reply with block index + * + * Possible requests: + * bmap + * + * @param req request handle + * @param idx block index within device + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_bmap(fuse_req_t req, uint64_t idx); + +/* ----------------------------------------------------------- * + * Filling a buffer in readdir * + * ----------------------------------------------------------- */ + +/** + * Add a directory entry to the buffer + * + * Buffer needs to be large enough to hold the entry. Of it's not, + * then the entry is not filled in but the size of the entry is still + * returned. The caller can check this by comparing the bufsize + * parameter with the returned entry size. If the entry size is + * larger than the buffer size, the operation failed. + * + * From the 'stbuf' argument the st_ino field and bits 12-15 of the + * st_mode field are used. The other fields are ignored. + * + * Note: offsets do not necessarily represent physical offsets, and + * could be any marker, that enables the implementation to find a + * specific point in the directory stream. + * + * @param req request handle + * @param buf the point where the new entry will be added to the buffer + * @param bufsize remaining size of the buffer + * @param the name of the entry + * @param stbuf the file attributes + * @param off the offset of the next entry + * @return the space needed for the entry + */ +size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, + const char *name, const struct stat *stbuf, + off_t off); + +/** + * Reply to finish ioctl + * + * Possible requests: + * ioctl + * + * @param req request handle + * @param result result to be passed to the caller + * @param buf buffer containing output data + * @param size length of output data + */ +int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size); + + +/* ----------------------------------------------------------- * + * Utility functions * + * ----------------------------------------------------------- */ + +/** + * Get the userdata from the request + * + * @param req request handle + * @return the user data passed to fuse_lowlevel_new() + */ +void *fuse_req_userdata(fuse_req_t req); + +/** + * Get the context from the request + * + * The pointer returned by this function will only be valid for the + * request's lifetime + * + * @param req request handle + * @return the context structure + */ +const struct fuse_ctx *fuse_req_ctx(fuse_req_t req); + +/** + * Callback function for an interrupt + * + * @param req interrupted request + * @param data user data + */ +typedef void (*fuse_interrupt_func_t)(fuse_req_t req, void *data); + +/** + * Register/unregister callback for an interrupt + * + * If an interrupt has already happened, then the callback function is + * called from within this function, hence it's not possible for + * interrupts to be lost. + * + * @param req request handle + * @param func the callback function or NULL for unregister + * @parm data user data passed to the callback function + */ +void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, + void *data); + +/** + * Check if a request has already been interrupted + * + * @param req request handle + * @return 1 if the request has been interrupted, 0 otherwise + */ +int fuse_req_interrupted(fuse_req_t req); + +/* ----------------------------------------------------------- * + * Filesystem setup * + * ----------------------------------------------------------- */ + +#ifdef __SOLARIS__ + +/* Deprecated, don't use */ +int fuse_lowlevel_is_lib_option(const char *opt); + +#endif /* __SOLARIS__ */ + +/** + * Create a low level session + * + * @param args argument vector + * @param op the low level filesystem operations + * @param op_size sizeof(struct fuse_lowlevel_ops) + * @param userdata user data + * @return the created session object, or NULL on failure + */ +struct fuse_session *fuse_lowlevel_new(struct fuse_args *args, + const struct fuse_lowlevel_ops *op, + size_t op_size, void *userdata); + +/* ----------------------------------------------------------- * + * Session interface * + * ----------------------------------------------------------- */ + +/** + * Session operations + * + * This is used in session creation + */ +struct fuse_session_ops { + /** + * Hook to process a request (mandatory) + * + * @param data user data passed to fuse_session_new() + * @param buf buffer containing the raw request + * @param len request length + * @param ch channel on which the request was received + */ + void (*process) (void *data, const char *buf, size_t len, + struct fuse_chan *ch); + + /** + * Hook for session exit and reset (optional) + * + * @param data user data passed to fuse_session_new() + * @param val exited status (1 - exited, 0 - not exited) + */ + void (*exit) (void *data, int val); + + /** + * Hook for querying the current exited status (optional) + * + * @param data user data passed to fuse_session_new() + * @return 1 if exited, 0 if not exited + */ + int (*exited) (void *data); + + /** + * Hook for cleaning up the channel on destroy (optional) + * + * @param data user data passed to fuse_session_new() + */ + void (*destroy) (void *data); +}; + +/** + * Create a new session + * + * @param op session operations + * @param data user data + * @return new session object, or NULL on failure + */ +struct fuse_session *fuse_session_new(struct fuse_session_ops *op, void *data); + +/** + * Assign a channel to a session + * + * Note: currently only a single channel may be assigned. This may + * change in the future + * + * If a session is destroyed, the assigned channel is also destroyed + * + * @param se the session + * @param ch the channel + */ +void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch); + +/** + * Remove a channel from a session + * + * If the channel is not assigned to a session, then this is a no-op + * + * @param ch the channel to remove + */ +void fuse_session_remove_chan(struct fuse_chan *ch); + +/** + * Iterate over the channels assigned to a session + * + * The iterating function needs to start with a NULL channel, and + * after that needs to pass the previously returned channel to the + * function. + * + * @param se the session + * @param ch the previous channel, or NULL + * @return the next channel, or NULL if no more channels exist + */ +struct fuse_chan *fuse_session_next_chan(struct fuse_session *se, + struct fuse_chan *ch); + +/** + * Process a raw request + * + * @param se the session + * @param buf buffer containing the raw request + * @param len request length + * @param ch channel on which the request was received + */ +void fuse_session_process(struct fuse_session *se, const char *buf, size_t len, + struct fuse_chan *ch); + +/** + * Destroy a session + * + * @param se the session + */ +void fuse_session_destroy(struct fuse_session *se); + +/** + * Exit a session + * + * @param se the session + */ +void fuse_session_exit(struct fuse_session *se); + +/** + * Reset the exited status of a session + * + * @param se the session + */ +void fuse_session_reset(struct fuse_session *se); + +/** + * Query the exited status of a session + * + * @param se the session + * @return 1 if exited, 0 if not exited + */ +int fuse_session_exited(struct fuse_session *se); + +/** + * Enter a single threaded event loop + * + * @param se the session + * @return 0 on success, -1 on error + */ +int fuse_session_loop(struct fuse_session *se); + +/** + * Enter a multi-threaded event loop + * + * @param se the session + * @return 0 on success, -1 on error + */ +int fuse_session_loop_mt(struct fuse_session *se); + +/* ----------------------------------------------------------- * + * Channel interface * + * ----------------------------------------------------------- */ + +/** + * Channel operations + * + * This is used in channel creation + */ +struct fuse_chan_ops { + /** + * Hook for receiving a raw request + * + * @param ch pointer to the channel + * @param buf the buffer to store the request in + * @param size the size of the buffer + * @return the actual size of the raw request, or -1 on error + */ + int (*receive)(struct fuse_chan **chp, char *buf, size_t size); + + /** + * Hook for sending a raw reply + * + * A return value of -ENOENT means, that the request was + * interrupted, and the reply was discarded + * + * @param ch the channel + * @param iov vector of blocks + * @param count the number of blocks in vector + * @return zero on success, -errno on failure + */ + int (*send)(struct fuse_chan *ch, const struct iovec iov[], + size_t count); + + /** + * Destroy the channel + * + * @param ch the channel + */ + void (*destroy)(struct fuse_chan *ch); +}; + +/** + * Create a new channel + * + * @param op channel operations + * @param fd file descriptor of the channel + * @param bufsize the minimal receive buffer size + * @param data user data + * @return the new channel object, or NULL on failure + */ +struct fuse_chan *fuse_chan_new(struct fuse_chan_ops *op, int fd, + size_t bufsize, void *data); + +/** + * Query the file descriptor of the channel + * + * @param ch the channel + * @return the file descriptor passed to fuse_chan_new() + */ +int fuse_chan_fd(struct fuse_chan *ch); + +/** + * Query the minimal receive buffer size + * + * @param ch the channel + * @return the buffer size passed to fuse_chan_new() + */ +size_t fuse_chan_bufsize(struct fuse_chan *ch); + +/** + * Query the user data + * + * @param ch the channel + * @return the user data passed to fuse_chan_new() + */ +void *fuse_chan_data(struct fuse_chan *ch); + +/** + * Query the session to which this channel is assigned + * + * @param ch the channel + * @return the session, or NULL if the channel is not assigned + */ +struct fuse_session *fuse_chan_session(struct fuse_chan *ch); + +/** + * Receive a raw request + * + * A return value of -ENODEV means, that the filesystem was unmounted + * + * @param ch pointer to the channel + * @param buf the buffer to store the request in + * @param size the size of the buffer + * @return the actual size of the raw request, or -errno on error + */ +int fuse_chan_recv(struct fuse_chan **ch, char *buf, size_t size); + +/** + * Send a raw reply + * + * A return value of -ENOENT means, that the request was + * interrupted, and the reply was discarded + * + * @param ch the channel + * @param iov vector of blocks + * @param count the number of blocks in vector + * @return zero on success, -errno on failure + */ +int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[], + size_t count); + +/** + * Destroy a channel + * + * @param ch the channel + */ +void fuse_chan_destroy(struct fuse_chan *ch); + +#ifdef __cplusplus +} +#endif + +#endif /* _FUSE_LOWLEVEL_H_ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_lowlevel_compat.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_lowlevel_compat.h new file mode 100644 index 0000000..753d46b --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_lowlevel_compat.h @@ -0,0 +1,16 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +/* these definitions provide source compatibility to prior versions. + Do not include this file directly! */ + +size_t fuse_dirent_size(size_t namelen); + +char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf, + off_t off); + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_opt.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_opt.h new file mode 100644 index 0000000..7ae08af --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/fuse-lite/fuse_opt.h @@ -0,0 +1,261 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#ifndef _FUSE_OPT_H_ +#define _FUSE_OPT_H_ + +/** @file + * + * This file defines the option parsing interface of FUSE + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Option description + * + * This structure describes a single option, and and action associated + * with it, in case it matches. + * + * More than one such match may occur, in which case the action for + * each match is executed. + * + * There are three possible actions in case of a match: + * + * i) An integer (int or unsigned) variable determined by 'offset' is + * set to 'value' + * + * ii) The processing function is called, with 'value' as the key + * + * iii) An integer (any) or string (char *) variable determined by + * 'offset' is set to the value of an option parameter + * + * 'offset' should normally be either set to + * + * - 'offsetof(struct foo, member)' actions i) and iii) + * + * - -1 action ii) + * + * The 'offsetof()' macro is defined in the header. + * + * The template determines which options match, and also have an + * effect on the action. Normally the action is either i) or ii), but + * if a format is present in the template, then action iii) is + * performed. + * + * The types of templates are: + * + * 1) "-x", "-foo", "--foo", "--foo-bar", etc. These match only + * themselves. Invalid values are "--" and anything beginning + * with "-o" + * + * 2) "foo", "foo-bar", etc. These match "-ofoo", "-ofoo-bar" or + * the relevant option in a comma separated option list + * + * 3) "bar=", "--foo=", etc. These are variations of 1) and 2) + * which have a parameter + * + * 4) "bar=%s", "--foo=%lu", etc. Same matching as above but perform + * action iii). + * + * 5) "-x ", etc. Matches either "-xparam" or "-x param" as + * two separate arguments + * + * 6) "-x %s", etc. Combination of 4) and 5) + * + * If the format is "%s", memory is allocated for the string unlike + * with scanf(). + */ +struct fuse_opt { + /** Matching template and optional parameter formatting */ + const char *templ; + + /** + * Offset of variable within 'data' parameter of fuse_opt_parse() + * or -1 + */ + unsigned long offset; + + /** + * Value to set the variable to, or to be passed as 'key' to the + * processing function. Ignored if template has a format + */ + int value; +}; + +/** + * Key option. In case of a match, the processing function will be + * called with the specified key. + */ +#define FUSE_OPT_KEY(templ, key) { templ, -1U, key } + +/** + * Last option. An array of 'struct fuse_opt' must end with a NULL + * template value + */ +#define FUSE_OPT_END { .templ = NULL } + +/** + * Argument list + */ +struct fuse_args { + /** Argument count */ + int argc; + + /** Argument vector. NULL terminated */ + char **argv; + + /** Is 'argv' allocated? */ + int allocated; +}; + +/** + * Initializer for 'struct fuse_args' + */ +#define FUSE_ARGS_INIT(argc, argv) { argc, argv, 0 } + +/** + * Key value passed to the processing function if an option did not + * match any template + */ +#define FUSE_OPT_KEY_OPT -1 + +/** + * Key value passed to the processing function for all non-options + * + * Non-options are the arguments beginning with a charater other than + * '-' or all arguments after the special '--' option + */ +#define FUSE_OPT_KEY_NONOPT -2 + +/** + * Special key value for options to keep + * + * Argument is not passed to processing function, but behave as if the + * processing function returned 1 + */ +#define FUSE_OPT_KEY_KEEP -3 + +/** + * Special key value for options to discard + * + * Argument is not passed to processing function, but behave as if the + * processing function returned zero + */ +#define FUSE_OPT_KEY_DISCARD -4 + +/** + * Processing function + * + * This function is called if + * - option did not match any 'struct fuse_opt' + * - argument is a non-option + * - option did match and offset was set to -1 + * + * The 'arg' parameter will always contain the whole argument or + * option including the parameter if exists. A two-argument option + * ("-x foo") is always converted to single arguemnt option of the + * form "-xfoo" before this function is called. + * + * Options of the form '-ofoo' are passed to this function without the + * '-o' prefix. + * + * The return value of this function determines whether this argument + * is to be inserted into the output argument vector, or discarded. + * + * @param data is the user data passed to the fuse_opt_parse() function + * @param arg is the whole argument or option + * @param key determines why the processing function was called + * @param outargs the current output argument list + * @return -1 on error, 0 if arg is to be discarded, 1 if arg should be kept + */ +typedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key, + struct fuse_args *outargs); + +/** + * Option parsing function + * + * If 'args' was returned from a previous call to fuse_opt_parse() or + * it was constructed from + * + * A NULL 'args' is equivalent to an empty argument vector + * + * A NULL 'opts' is equivalent to an 'opts' array containing a single + * end marker + * + * A NULL 'proc' is equivalent to a processing function always + * returning '1' + * + * @param args is the input and output argument list + * @param data is the user data + * @param opts is the option description array + * @param proc is the processing function + * @return -1 on error, 0 on success + */ +int fuse_opt_parse(struct fuse_args *args, void *data, + const struct fuse_opt opts[], fuse_opt_proc_t proc); + +/** + * Add an option to a comma separated option list + * + * @param opts is a pointer to an option list, may point to a NULL value + * @param opt is the option to add + * @return -1 on allocation error, 0 on success + */ +int fuse_opt_add_opt(char **opts, const char *opt); + +/** + * Add an argument to a NULL terminated argument vector + * + * @param args is the structure containing the current argument list + * @param arg is the new argument to add + * @return -1 on allocation error, 0 on success + */ +int fuse_opt_add_arg(struct fuse_args *args, const char *arg); + +/** + * Add an argument at the specified position in a NULL terminated + * argument vector + * + * Adds the argument to the N-th position. This is useful for adding + * options at the beggining of the array which must not come after the + * special '--' option. + * + * @param args is the structure containing the current argument list + * @param pos is the position at which to add the argument + * @param arg is the new argument to add + * @return -1 on allocation error, 0 on success + */ +int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg); + +/** + * Free the contents of argument list + * + * The structure itself is not freed + * + * @param args is the structure containing the argument list + */ +void fuse_opt_free_args(struct fuse_args *args); + + +/** + * Check if an option matches + * + * @param opts is the option description array + * @param opt is the option to match + * @return 1 if a match is found, 0 if not + */ +int fuse_opt_match(const struct fuse_opt opts[], const char *opt); + +#ifdef __cplusplus +} +#endif + +#endif /* _FUSE_OPT_H_ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/Makefile.am b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/Makefile.am new file mode 100644 index 0000000..7217c58 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/Makefile.am @@ -0,0 +1,50 @@ + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +headers = \ + acls.h \ + attrib.h \ + attrlist.h \ + bitmap.h \ + bootsect.h \ + cache.h \ + collate.h \ + compat.h \ + compress.h \ + debug.h \ + device.h \ + device_io.h \ + dir.h \ + ea.h \ + efs.h \ + endians.h \ + index.h \ + inode.h \ + ioctl.h \ + layout.h \ + lcnalloc.h \ + logfile.h \ + logging.h \ + mft.h \ + misc.h \ + mst.h \ + ntfstime.h \ + object_id.h \ + param.h \ + plugin.h \ + realpath.h \ + reparse.h \ + runlist.h \ + security.h \ + support.h \ + types.h \ + unistr.h \ + volume.h \ + xattrs.h + +if INSTALL_LIBRARY +ntfs3ginclude_HEADERS = $(headers) +else +noinst_HEADERS = $(headers) +endif + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/acls.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/acls.h new file mode 100644 index 0000000..4f36389 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/acls.h @@ -0,0 +1,186 @@ +/* + * + * Copyright (c) 2007-2008 Jean-Pierre Andre + * + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef ACLS_H +#define ACLS_H + +#include "endians.h" + +/* + * JPA configuration modes for security.c / acls.c + * should be moved to some config file + */ + +#define BUFSZ 1024 /* buffer size to read mapping file */ +#define MAPPINGFILE ".NTFS-3G/UserMapping" /* default mapping file */ +#define LINESZ 120 /* maximum useful size of a mapping line */ +#define CACHE_PERMISSIONS_BITS 6 /* log2 of unitary allocation of permissions */ +#define CACHE_PERMISSIONS_SIZE 262144 /* max cacheable permissions */ + +/* + * Matching of ntfs permissions to Linux permissions + * these constants are adapted to endianness + * when setting, set them all + * when checking, check one is present + */ + + /* flags which are set to mean exec, write or read */ + +#define FILE_READ (FILE_READ_DATA) +#define FILE_WRITE (FILE_WRITE_DATA | FILE_APPEND_DATA \ + | READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA) +#define FILE_EXEC (FILE_EXECUTE) +#define DIR_READ FILE_LIST_DIRECTORY +#define DIR_WRITE (FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY | FILE_DELETE_CHILD \ + | READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA) +#define DIR_EXEC (FILE_TRAVERSE) + + /* flags tested for meaning exec, write or read */ + /* tests for write allow for interpretation of a sticky bit */ + +#define FILE_GREAD (FILE_READ_DATA | GENERIC_READ) +#define FILE_GWRITE (FILE_WRITE_DATA | FILE_APPEND_DATA | GENERIC_WRITE) +#define FILE_GEXEC (FILE_EXECUTE | GENERIC_EXECUTE) +#define DIR_GREAD (FILE_LIST_DIRECTORY | GENERIC_READ) +#define DIR_GWRITE (FILE_ADD_FILE | FILE_ADD_SUBDIRECTORY | GENERIC_WRITE) +#define DIR_GEXEC (FILE_TRAVERSE | GENERIC_EXECUTE) + + /* standard owner (and administrator) rights */ + +#define OWNER_RIGHTS (DELETE | READ_CONTROL | WRITE_DAC | WRITE_OWNER \ + | SYNCHRONIZE \ + | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES \ + | FILE_READ_EA | FILE_WRITE_EA) + + /* standard world rights */ + +#define WORLD_RIGHTS (READ_CONTROL | FILE_READ_ATTRIBUTES | FILE_READ_EA \ + | SYNCHRONIZE) + + /* inheritance flags for files and directories */ + +#define FILE_INHERITANCE NO_PROPAGATE_INHERIT_ACE +#define DIR_INHERITANCE (OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE) + +/* + * To identify NTFS ACL meaning Posix ACL granted to root + * we use rights always granted to anybody, so they have no impact + * either on Windows or on Linux. + */ + +#define ROOT_OWNER_UNMARK SYNCHRONIZE /* ACL granted to root as owner */ +#define ROOT_GROUP_UNMARK FILE_READ_EA /* ACL granted to root as group */ + +/* + * Maximum SID size and a type large enough to hold it + */ + +#define MAX_SID_SIZE (8 + SID_MAX_SUB_AUTHORITIES*4) +typedef char BIGSID[MAX_SID_SIZE]; + +/* + * Struct to hold the input mapping file + * (private to this module) + */ + +struct MAPLIST { + struct MAPLIST *next; + char *uidstr; /* uid text from the same record */ + char *gidstr; /* gid text from the same record */ + char *sidstr; /* sid text from the same record */ + char maptext[LINESZ + 1]; +}; + +typedef int (*FILEREADER)(void *fileid, char *buf, size_t size, off_t pos); + +/* + * Constants defined in acls.c + */ + +extern const SID *adminsid; +extern const SID *worldsid; + +/* + * Functions defined in acls.c + */ + +BOOL ntfs_valid_descr(const char *securattr, unsigned int attrsz); +BOOL ntfs_valid_pattern(const SID *sid); +BOOL ntfs_valid_sid(const SID *sid); +BOOL ntfs_same_sid(const SID *first, const SID *second); + +BOOL ntfs_is_user_sid(const SID *usid); + + +int ntfs_sid_size(const SID * sid); +unsigned int ntfs_attr_size(const char *attr); + +const SID *ntfs_find_usid(const struct MAPPING *usermapping, + uid_t uid, SID *pdefsid); +const SID *ntfs_find_gsid(const struct MAPPING *groupmapping, + gid_t gid, SID *pdefsid); +uid_t ntfs_find_user(const struct MAPPING *usermapping, const SID *usid); +gid_t ntfs_find_group(const struct MAPPING *groupmapping, const SID * gsid); +const SID *ntfs_acl_owner(const char *secattr); + +#if POSIXACLS + +BOOL ntfs_valid_posix(const struct POSIX_SECURITY *pxdesc); +void ntfs_sort_posix(struct POSIX_SECURITY *pxdesc); +int ntfs_merge_mode_posix(struct POSIX_SECURITY *pxdesc, mode_t mode); +struct POSIX_SECURITY *ntfs_build_inherited_posix( + const struct POSIX_SECURITY *pxdesc, mode_t mode, + mode_t umask, BOOL isdir); +struct POSIX_SECURITY *ntfs_build_basic_posix( + const struct POSIX_SECURITY *pxdesc, mode_t mode, + mode_t umask, BOOL isdir); +struct POSIX_SECURITY *ntfs_replace_acl(const struct POSIX_SECURITY *oldpxdesc, + const struct POSIX_ACL *newacl, int count, BOOL deflt); +struct POSIX_SECURITY *ntfs_build_permissions_posix( + struct MAPPING* const mapping[], + const char *securattr, + const SID *usid, const SID *gsid, BOOL isdir); +struct POSIX_SECURITY *ntfs_merge_descr_posix(const struct POSIX_SECURITY *first, + const struct POSIX_SECURITY *second); +char *ntfs_build_descr_posix(struct MAPPING* const mapping[], + struct POSIX_SECURITY *pxdesc, + int isdir, const SID *usid, const SID *gsid); + +#endif /* POSIXACLS */ + +int ntfs_inherit_acl_extra_size(const ACL *acl, const SID *usid, + const SID *gsid); +int ntfs_inherit_acl(const ACL *oldacl, ACL *newacl, + const SID *usid, const SID *gsid, + BOOL fordir, le16 inherited); +int ntfs_build_permissions(const char *securattr, + const SID *usid, const SID *gsid, BOOL isdir); +char *ntfs_build_descr(mode_t mode, + int isdir, const SID * usid, const SID * gsid); +struct MAPLIST *ntfs_read_mapping(FILEREADER reader, void *fileid); +struct MAPPING *ntfs_do_user_mapping(struct MAPLIST *firstitem); +struct MAPPING *ntfs_do_group_mapping(struct MAPLIST *firstitem); +void ntfs_free_mapping(struct MAPPING *mapping[]); + +#endif /* ACLS_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/attrib.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/attrib.h new file mode 100644 index 0000000..f2a180c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/attrib.h @@ -0,0 +1,404 @@ +/* + * attrib.h - Exports for attribute handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2004 Anton Altaparmakov + * Copyright (c) 2004-2005 Yura Pakhuchiy + * Copyright (c) 2006-2007 Szabolcs Szakacsits + * Copyright (c) 2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_ATTRIB_H +#define _NTFS_ATTRIB_H + +/* Forward declarations */ +typedef struct _ntfs_attr ntfs_attr; +typedef struct _ntfs_attr_search_ctx ntfs_attr_search_ctx; + +#include "types.h" +#include "inode.h" +#include "unistr.h" +#include "runlist.h" +#include "volume.h" +#include "debug.h" +#include "logging.h" + +extern ntfschar AT_UNNAMED[]; +extern ntfschar STREAM_SDS[]; + +/* The little endian Unicode string $TXF_DATA as a global constant. */ +extern ntfschar TXF_DATA[10]; + +/** + * enum ntfs_lcn_special_values - special return values for ntfs_*_vcn_to_lcn() + * + * Special return values for ntfs_rl_vcn_to_lcn() and ntfs_attr_vcn_to_lcn(). + * + * TODO: Describe them. + */ +typedef enum { + LCN_HOLE = -1, /* Keep this as highest value or die! */ + LCN_RL_NOT_MAPPED = -2, + LCN_ENOENT = -3, + LCN_EINVAL = -4, + LCN_EIO = -5, +} ntfs_lcn_special_values; + +typedef enum { /* ways of processing holes when expanding */ + HOLES_NO, + HOLES_OK, + HOLES_DELAY, + HOLES_NONRES +} hole_type; + +/** + * struct ntfs_attr_search_ctx - search context used in attribute search functions + * @mrec: buffer containing mft record to search + * @attr: attribute record in @mrec where to begin/continue search + * @is_first: if true lookup_attr() begins search with @attr, else after @attr + * + * Structure must be initialized to zero before the first call to one of the + * attribute search functions. Initialize @mrec to point to the mft record to + * search, and @attr to point to the first attribute within @mrec (not necessary + * if calling the _first() functions), and set @is_first to TRUE (not necessary + * if calling the _first() functions). + * + * If @is_first is TRUE, the search begins with @attr. If @is_first is FALSE, + * the search begins after @attr. This is so that, after the first call to one + * of the search attribute functions, we can call the function again, without + * any modification of the search context, to automagically get the next + * matching attribute. + */ +struct _ntfs_attr_search_ctx { + MFT_RECORD *mrec; + ATTR_RECORD *attr; + BOOL is_first; + ntfs_inode *ntfs_ino; + ATTR_LIST_ENTRY *al_entry; + ntfs_inode *base_ntfs_ino; + MFT_RECORD *base_mrec; + ATTR_RECORD *base_attr; +}; + +extern void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx); +extern ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, + MFT_RECORD *mrec); +extern void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx); + +extern int ntfs_attr_lookup(const ATTR_TYPES type, const ntfschar *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const VCN lowest_vcn, const u8 *val, const u32 val_len, + ntfs_attr_search_ctx *ctx); + +extern int ntfs_attr_position(const ATTR_TYPES type, ntfs_attr_search_ctx *ctx); + +extern ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol, + const ATTR_TYPES type); + +/** + * ntfs_attrs_walk - syntactic sugar for walking all attributes in an inode + * @ctx: initialised attribute search context + * + * Syntactic sugar for walking attributes in an inode. + * + * Return 0 on success and -1 on error with errno set to the error code from + * ntfs_attr_lookup(). + * + * Example: When you want to enumerate all attributes in an open ntfs inode + * @ni, you can simply do: + * + * int err; + * ntfs_attr_search_ctx *ctx = ntfs_attr_get_search_ctx(ni, NULL); + * if (!ctx) + * // Error code is in errno. Handle this case. + * while (!(err = ntfs_attrs_walk(ctx))) { + * ATTR_RECORD *attr = ctx->attr; + * // attr now contains the next attribute. Do whatever you want + * // with it and then just continue with the while loop. + * } + * if (err && errno != ENOENT) + * // Ooops. An error occurred! You should handle this case. + * // Now finished with all attributes in the inode. + */ +static __inline__ int ntfs_attrs_walk(ntfs_attr_search_ctx *ctx) +{ + return ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 0, + NULL, 0, ctx); +} + +/** + * struct ntfs_attr - ntfs in memory non-resident attribute structure + * @rl: if not NULL, the decompressed runlist + * @ni: base ntfs inode to which this attribute belongs + * @type: attribute type + * @name: Unicode name of the attribute + * @name_len: length of @name in Unicode characters + * @state: NTFS attribute specific flags describing this attribute + * @allocated_size: copy from the attribute record + * @data_size: copy from the attribute record + * @initialized_size: copy from the attribute record + * @compressed_size: copy from the attribute record + * @compression_block_size: size of a compression block (cb) + * @compression_block_size_bits: log2 of the size of a cb + * @compression_block_clusters: number of clusters per cb + * + * This structure exists purely to provide a mechanism of caching the runlist + * of an attribute. If you want to operate on a particular attribute extent, + * you should not be using this structure at all. If you want to work with a + * resident attribute, you should not be using this structure at all. As a + * fail-safe check make sure to test NAttrNonResident() and if it is false, you + * know you shouldn't be using this structure. + * + * If you want to work on a resident attribute or on a specific attribute + * extent, you should use ntfs_lookup_attr() to retrieve the attribute (extent) + * record, edit that, and then write back the mft record (or set the + * corresponding ntfs inode dirty for delayed write back). + * + * @rl is the decompressed runlist of the attribute described by this + * structure. Obviously this only makes sense if the attribute is not resident, + * i.e. NAttrNonResident() is true. If the runlist hasn't been decompressed yet + * @rl is NULL, so be prepared to cope with @rl == NULL. + * + * @ni is the base ntfs inode of the attribute described by this structure. + * + * @type is the attribute type (see layout.h for the definition of ATTR_TYPES), + * @name and @name_len are the little endian Unicode name and the name length + * in Unicode characters of the attribute, respectively. + * + * @state contains NTFS attribute specific flags describing this attribute + * structure. See ntfs_attr_state_bits above. + */ +struct _ntfs_attr { + runlist_element *rl; + ntfs_inode *ni; + ATTR_TYPES type; + ATTR_FLAGS data_flags; + ntfschar *name; + u32 name_len; + unsigned long state; + s64 allocated_size; + s64 data_size; + s64 initialized_size; + s64 compressed_size; + u32 compression_block_size; + u8 compression_block_size_bits; + u8 compression_block_clusters; + s8 unused_runs; /* pre-reserved entries available */ +}; + +/** + * enum ntfs_attr_state_bits - bits for the state field in the ntfs_attr + * structure + */ +typedef enum { + NA_Initialized, /* 1: structure is initialized. */ + NA_NonResident, /* 1: Attribute is not resident. */ + NA_BeingNonResident, /* 1: Attribute is being made not resident. */ + NA_FullyMapped, /* 1: Attribute has been fully mapped */ + NA_DataAppending, /* 1: Attribute is being appended to */ + NA_ComprClosing, /* 1: Compressed attribute is being closed */ + NA_RunlistDirty, /* 1: Runlist has been updated */ +} ntfs_attr_state_bits; + +#define test_nattr_flag(na, flag) test_bit(NA_##flag, (na)->state) +#define set_nattr_flag(na, flag) set_bit(NA_##flag, (na)->state) +#define clear_nattr_flag(na, flag) clear_bit(NA_##flag, (na)->state) + +#define NAttrInitialized(na) test_nattr_flag(na, Initialized) +#define NAttrSetInitialized(na) set_nattr_flag(na, Initialized) +#define NAttrClearInitialized(na) clear_nattr_flag(na, Initialized) + +#define NAttrNonResident(na) test_nattr_flag(na, NonResident) +#define NAttrSetNonResident(na) set_nattr_flag(na, NonResident) +#define NAttrClearNonResident(na) clear_nattr_flag(na, NonResident) + +#define NAttrBeingNonResident(na) test_nattr_flag(na, BeingNonResident) +#define NAttrSetBeingNonResident(na) set_nattr_flag(na, BeingNonResident) +#define NAttrClearBeingNonResident(na) clear_nattr_flag(na, BeingNonResident) + +#define NAttrFullyMapped(na) test_nattr_flag(na, FullyMapped) +#define NAttrSetFullyMapped(na) set_nattr_flag(na, FullyMapped) +#define NAttrClearFullyMapped(na) clear_nattr_flag(na, FullyMapped) + +#define NAttrDataAppending(na) test_nattr_flag(na, DataAppending) +#define NAttrSetDataAppending(na) set_nattr_flag(na, DataAppending) +#define NAttrClearDataAppending(na) clear_nattr_flag(na, DataAppending) + +#define NAttrRunlistDirty(na) test_nattr_flag(na, RunlistDirty) +#define NAttrSetRunlistDirty(na) set_nattr_flag(na, RunlistDirty) +#define NAttrClearRunlistDirty(na) clear_nattr_flag(na, RunlistDirty) + +#define NAttrComprClosing(na) test_nattr_flag(na, ComprClosing) +#define NAttrSetComprClosing(na) set_nattr_flag(na, ComprClosing) +#define NAttrClearComprClosing(na) clear_nattr_flag(na, ComprClosing) + +#define GenNAttrIno(func_name, flag) \ +extern int NAttr##func_name(ntfs_attr *na); \ +extern void NAttrSet##func_name(ntfs_attr *na); \ +extern void NAttrClear##func_name(ntfs_attr *na); + +GenNAttrIno(Compressed, FILE_ATTR_COMPRESSED) +GenNAttrIno(Encrypted, FILE_ATTR_ENCRYPTED) +GenNAttrIno(Sparse, FILE_ATTR_SPARSE_FILE) +#undef GenNAttrIno + +/** + * union attr_val - Union of all known attribute values + * + * For convenience. Used in the attr structure. + */ +typedef union { + u8 _default; /* Unnamed u8 to serve as default when just using + a_val without specifying any of the below. */ + STANDARD_INFORMATION std_inf; + ATTR_LIST_ENTRY al_entry; + FILE_NAME_ATTR filename; + OBJECT_ID_ATTR obj_id; + SECURITY_DESCRIPTOR_ATTR sec_desc; + VOLUME_NAME vol_name; + VOLUME_INFORMATION vol_inf; + DATA_ATTR data; + INDEX_ROOT index_root; + INDEX_BLOCK index_blk; + BITMAP_ATTR bmp; + REPARSE_POINT reparse; + EA_INFORMATION ea_inf; + EA_ATTR ea; + PROPERTY_SET property_set; + LOGGED_UTILITY_STREAM logged_util_stream; + EFS_ATTR_HEADER efs; +} attr_val; + +extern void ntfs_attr_init(ntfs_attr *na, const BOOL non_resident, + const ATTR_FLAGS data_flags, const BOOL encrypted, + const BOOL sparse, + const s64 allocated_size, const s64 data_size, + const s64 initialized_size, const s64 compressed_size, + const u8 compression_unit); + + /* warning : in the following "name" has to be freeable */ + /* or one of constants AT_UNNAMED, NTFS_INDEX_I30 or STREAM_SDS */ +extern ntfs_attr *ntfs_attr_open(ntfs_inode *ni, const ATTR_TYPES type, + ntfschar *name, u32 name_len); +extern void ntfs_attr_close(ntfs_attr *na); + +extern s64 ntfs_attr_pread(ntfs_attr *na, const s64 pos, s64 count, + void *b); +extern s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count, + const void *b); +extern int ntfs_attr_pclose(ntfs_attr *na); + +extern void *ntfs_attr_readall(ntfs_inode *ni, const ATTR_TYPES type, + ntfschar *name, u32 name_len, s64 *data_size); + +extern s64 ntfs_attr_mst_pread(ntfs_attr *na, const s64 pos, + const s64 bk_cnt, const u32 bk_size, void *dst); +extern s64 ntfs_attr_mst_pwrite(ntfs_attr *na, const s64 pos, + s64 bk_cnt, const u32 bk_size, void *src); + +extern int ntfs_attr_map_runlist(ntfs_attr *na, VCN vcn); +extern int ntfs_attr_map_whole_runlist(ntfs_attr *na); + +extern LCN ntfs_attr_vcn_to_lcn(ntfs_attr *na, const VCN vcn); +extern runlist_element *ntfs_attr_find_vcn(ntfs_attr *na, const VCN vcn); + +extern int ntfs_attr_size_bounds_check(const ntfs_volume *vol, + const ATTR_TYPES type, const s64 size); +extern int ntfs_attr_can_be_resident(const ntfs_volume *vol, + const ATTR_TYPES type); +int ntfs_attr_make_non_resident(ntfs_attr *na, + ntfs_attr_search_ctx *ctx); +int ntfs_attr_force_non_resident(ntfs_attr *na); +extern int ntfs_make_room_for_attr(MFT_RECORD *m, u8 *pos, u32 size); + +extern int ntfs_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type, + const ntfschar *name, u8 name_len, const u8 *val, u32 size, + ATTR_FLAGS flags); +extern int ntfs_non_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type, + const ntfschar *name, u8 name_len, VCN lowest_vcn, + int dataruns_size, ATTR_FLAGS flags); +extern int ntfs_attr_record_rm(ntfs_attr_search_ctx *ctx); + +extern int ntfs_attr_add(ntfs_inode *ni, ATTR_TYPES type, + ntfschar *name, u8 name_len, const u8 *val, s64 size); +extern int ntfs_attr_set_flags(ntfs_inode *ni, ATTR_TYPES type, + const ntfschar *name, u8 name_len, ATTR_FLAGS flags, + ATTR_FLAGS mask); +extern int ntfs_attr_rm(ntfs_attr *na); + +extern int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size); + +extern int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a, + const u32 new_size); + +extern int ntfs_attr_record_move_to(ntfs_attr_search_ctx *ctx, ntfs_inode *ni); +extern int ntfs_attr_record_move_away(ntfs_attr_search_ctx *ctx, int extra); + +extern int ntfs_attr_update_mapping_pairs(ntfs_attr *na, VCN from_vcn); + +extern int ntfs_attr_truncate(ntfs_attr *na, const s64 newsize); +extern int ntfs_attr_truncate_solid(ntfs_attr *na, const s64 newsize); + +/** + * get_attribute_value_length - return the length of the value of an attribute + * @a: pointer to a buffer containing the attribute record + * + * Return the byte size of the attribute value of the attribute @a (as it + * would be after eventual decompression and filling in of holes if sparse). + * If we return 0, check errno. If errno is 0 the actual length was 0, + * otherwise errno describes the error. + * + * FIXME: Describe possible errnos. + */ +extern s64 ntfs_get_attribute_value_length(const ATTR_RECORD *a); + +/** + * get_attribute_value - return the attribute value of an attribute + * @vol: volume on which the attribute is present + * @a: attribute to get the value of + * @b: destination buffer for the attribute value + * + * Make a copy of the attribute value of the attribute @a into the destination + * buffer @b. Note, that the size of @b has to be at least equal to the value + * returned by get_attribute_value_length(@a). + * + * Return number of bytes copied. If this is zero check errno. If errno is 0 + * then nothing was read due to a zero-length attribute value, otherwise + * errno describes the error. + */ +extern s64 ntfs_get_attribute_value(const ntfs_volume *vol, + const ATTR_RECORD *a, u8 *b); + +extern void ntfs_attr_name_free(char **name); +extern char *ntfs_attr_name_get(const ntfschar *uname, const int uname_len); +extern int ntfs_attr_exist(ntfs_inode *ni, const ATTR_TYPES type, + const ntfschar *name, u32 name_len); +extern int ntfs_attr_remove(ntfs_inode *ni, const ATTR_TYPES type, + ntfschar *name, u32 name_len); +extern s64 ntfs_attr_get_free_bits(ntfs_attr *na); +extern int ntfs_attr_data_read(ntfs_inode *ni, + ntfschar *stream_name, int stream_name_len, + char *buf, size_t size, off_t offset); +extern int ntfs_attr_data_write(ntfs_inode *ni, + ntfschar *stream_name, int stream_name_len, + const char *buf, size_t size, off_t offset); +extern int ntfs_attr_shrink_size(ntfs_inode *ni, ntfschar *stream_name, + int stream_name_len, off_t offset); +extern int ntfs_attr_inconsistent(const ATTR_RECORD *a, const MFT_REF mref); + +#endif /* defined _NTFS_ATTRIB_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/attrlist.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/attrlist.h new file mode 100644 index 0000000..2952e48 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/attrlist.h @@ -0,0 +1,51 @@ +/* + * attrlist.h - Exports for attribute list attribute handling. + * Originated from Linux-NTFS project. + * + * Copyright (c) 2004 Anton Altaparmakov + * Copyright (c) 2004 Yura Pakhuchiy + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_ATTRLIST_H +#define _NTFS_ATTRLIST_H + +#include "attrib.h" + +extern int ntfs_attrlist_need(ntfs_inode *ni); + +extern int ntfs_attrlist_entry_add(ntfs_inode *ni, ATTR_RECORD *attr); +extern int ntfs_attrlist_entry_rm(ntfs_attr_search_ctx *ctx); + +/** + * ntfs_attrlist_mark_dirty - set the attribute list dirty + * @ni: ntfs inode which base inode contain dirty attribute list + * + * Set the attribute list dirty so it is written out later (at the latest at + * ntfs_inode_close() time). + * + * This function cannot fail. + */ +static __inline__ void ntfs_attrlist_mark_dirty(ntfs_inode *ni) +{ + if (ni->nr_extents == -1) + NInoAttrListSetDirty(ni->base_ni); + else + NInoAttrListSetDirty(ni); +} + +#endif /* defined _NTFS_ATTRLIST_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/bitmap.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/bitmap.h new file mode 100644 index 0000000..10b5f6c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/bitmap.h @@ -0,0 +1,96 @@ +/* + * bitmap.h - Exports for bitmap handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2004 Anton Altaparmakov + * Copyright (c) 2004-2005 Richard Russon + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_BITMAP_H +#define _NTFS_BITMAP_H + +#include "types.h" +#include "attrib.h" + +/* + * NOTES: + * + * - Operations are 8-bit only to ensure the functions work both on little + * and big endian machines! So don't make them 32-bit ops! + * - bitmap starts at bit = 0 and ends at bit = bitmap size - 1. + * - _Caller_ has to make sure that the bit to operate on is less than the + * size of the bitmap. + */ + +extern void ntfs_bit_set(u8 *bitmap, const u64 bit, const u8 new_value); +extern char ntfs_bit_get(const u8 *bitmap, const u64 bit); +extern char ntfs_bit_get_and_set(u8 *bitmap, const u64 bit, const u8 new_value); +extern int ntfs_bitmap_set_run(ntfs_attr *na, s64 start_bit, s64 count); +extern int ntfs_bitmap_clear_run(ntfs_attr *na, s64 start_bit, s64 count); + +/** + * ntfs_bitmap_set_bit - set a bit in a bitmap + * @na: attribute containing the bitmap + * @bit: bit to set + * + * Set the @bit in the bitmap described by the attribute @na. + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +static __inline__ int ntfs_bitmap_set_bit(ntfs_attr *na, s64 bit) +{ + return ntfs_bitmap_set_run(na, bit, 1); +} + +/** + * ntfs_bitmap_clear_bit - clear a bit in a bitmap + * @na: attribute containing the bitmap + * @bit: bit to clear + * + * Clear @bit in the bitmap described by the attribute @na. + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +static __inline__ int ntfs_bitmap_clear_bit(ntfs_attr *na, s64 bit) +{ + return ntfs_bitmap_clear_run(na, bit, 1); +} + +/* + * rol32 - rotate a 32-bit value left + * + * @word: value to rotate + * @shift: bits to roll + */ +static __inline__ u32 ntfs_rol32(u32 word, unsigned int shift) +{ + return (word << shift) | (word >> (32 - shift)); +} + +/* + * ror32 - rotate a 32-bit value right + * + * @word: value to rotate + * @shift: bits to roll + */ +static __inline__ u32 ntfs_ror32(u32 word, unsigned int shift) +{ + return (word >> shift) | (word << (32 - shift)); +} + +#endif /* defined _NTFS_BITMAP_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/bootsect.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/bootsect.h new file mode 100644 index 0000000..a299e82 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/bootsect.h @@ -0,0 +1,42 @@ +/* + * bootsect.h - Exports for bootsector record handling. + * Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2002 Anton Altaparmakov + * Copyright (c) 2006 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_BOOTSECT_H +#define _NTFS_BOOTSECT_H + +#include "types.h" +#include "volume.h" +#include "layout.h" + +/** + * ntfs_boot_sector_is_ntfs - check a boot sector for describing an ntfs volume + * @b: buffer containing the boot sector + * + * This function checks the boot sector in @b for describing a valid ntfs + * volume. Return TRUE if @b is a valid NTFS boot sector or FALSE otherwise. + */ +extern BOOL ntfs_boot_sector_is_ntfs(NTFS_BOOT_SECTOR *b); +extern int ntfs_boot_sector_parse(ntfs_volume *vol, const NTFS_BOOT_SECTOR *bs); + +#endif /* defined _NTFS_BOOTSECT_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/cache.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/cache.h new file mode 100644 index 0000000..195222c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/cache.h @@ -0,0 +1,118 @@ +/* + * cache.h : deal with indexed LRU caches + * + * Copyright (c) 2008-2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_CACHE_H_ +#define _NTFS_CACHE_H_ + +#include "volume.h" + +struct CACHED_GENERIC { + struct CACHED_GENERIC *next; + struct CACHED_GENERIC *previous; + void *variable; + size_t varsize; + union ALIGNMENT payload[1]; +} ; + +struct CACHED_INODE { + struct CACHED_INODE *next; + struct CACHED_INODE *previous; + const char *pathname; + size_t varsize; + /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + u64 inum; +} ; + +struct CACHED_NIDATA { + struct CACHED_NIDATA *next; + struct CACHED_NIDATA *previous; + const char *pathname; /* not used */ + size_t varsize; /* not used */ + /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + u64 inum; + ntfs_inode *ni; +} ; + +struct CACHED_LOOKUP { + struct CACHED_LOOKUP *next; + struct CACHED_LOOKUP *previous; + const char *name; + size_t namesize; + /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + u64 parent; + u64 inum; +} ; + +enum { + CACHE_FREE = 1, + CACHE_NOHASH = 2 +} ; + +typedef int (*cache_compare)(const struct CACHED_GENERIC *cached, + const struct CACHED_GENERIC *item); +typedef void (*cache_free)(const struct CACHED_GENERIC *cached); +typedef int (*cache_hash)(const struct CACHED_GENERIC *cached); + +struct HASH_ENTRY { + struct HASH_ENTRY *next; + struct CACHED_GENERIC *entry; +} ; + +struct CACHE_HEADER { + const char *name; + struct CACHED_GENERIC *most_recent_entry; + struct CACHED_GENERIC *oldest_entry; + struct CACHED_GENERIC *free_entry; + struct HASH_ENTRY *free_hash; + struct HASH_ENTRY **first_hash; + cache_free dofree; + cache_hash dohash; + unsigned long reads; + unsigned long writes; + unsigned long hits; + int fixed_size; + int max_hash; + struct CACHED_GENERIC entry[1]; +} ; + + /* cast to generic, avoiding gcc warnings */ +#define GENERIC(pstr) ((const struct CACHED_GENERIC*)(const void*)(pstr)) + +struct CACHED_GENERIC *ntfs_fetch_cache(struct CACHE_HEADER *cache, + const struct CACHED_GENERIC *wanted, + cache_compare compare); +struct CACHED_GENERIC *ntfs_enter_cache(struct CACHE_HEADER *cache, + const struct CACHED_GENERIC *item, + cache_compare compare); +int ntfs_invalidate_cache(struct CACHE_HEADER *cache, + const struct CACHED_GENERIC *item, + cache_compare compare, int flags); +int ntfs_remove_cache(struct CACHE_HEADER *cache, + struct CACHED_GENERIC *item, int flags); + +void ntfs_create_lru_caches(ntfs_volume *vol); +void ntfs_free_lru_caches(ntfs_volume *vol); + +#endif /* _NTFS_CACHE_H_ */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/collate.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/collate.h new file mode 100644 index 0000000..fe38383 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/collate.h @@ -0,0 +1,34 @@ +/* + * collate.h - Defines for NTFS collation handling. Originated from the Linux-NTFS + * project. + * + * Copyright (c) 2004 Anton Altaparmakov + * Copyright (c) 2005 Yura Pakhuchiy + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_COLLATE_H +#define _NTFS_COLLATE_H + +#include "types.h" +#include "volume.h" + +#define NTFS_COLLATION_ERROR -2 + +extern COLLATE ntfs_get_collate_function(COLLATION_RULES); + +#endif /* _NTFS_COLLATE_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/compat.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/compat.h new file mode 100644 index 0000000..288a86a --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/compat.h @@ -0,0 +1,94 @@ +/* + * compat.h - Tweaks for compatibility with non-Linux systems. + * + * Copyright (c) 2002 Richard Russon + * Copyright (c) 2002-2004 Anton Altaparmakov + * Copyright (c) 2008-2009 Szabolcs Szakacsits + * Copyright (c) 2019 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_COMPAT_H +#define _NTFS_COMPAT_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#ifdef HAVE_SYS_PARAM_H +#include +#endif + +#include /* ENODATA */ + +#ifndef ENODATA +#define ENODATA ENOENT +#endif + +#ifndef ELIBBAD +#define ELIBBAD ENOEXEC +#endif + +#ifndef ELIBACC +#define ELIBACC ENOENT +#endif + +/* xattr APIs in macOS differs from Linux ones in that they expect the special + * error code ENOATTR to be returned when an attribute cannot be found. So + * define NTFS_NOXATTR_ERRNO to the appropriate "no xattr found" errno value for + * the platform. */ +#if defined(__APPLE__) || defined(__DARWIN__) +#define NTFS_NOXATTR_ERRNO ENOATTR +#else +#define NTFS_NOXATTR_ERRNO ENODATA +#endif + +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif + +#ifndef HAVE_FFS +extern int ffs(int i); +#endif /* HAVE_FFS */ + +#ifndef HAVE_DAEMON +extern int daemon(int nochdir, int noclose); +#endif /* HAVE_DAEMON */ + +#ifndef HAVE_STRSEP +extern char *strsep(char **stringp, const char *delim); +#endif /* HAVE_STRSEP */ + +#ifdef WINDOWS + +#define HAVE_STDIO_H /* mimic config.h */ +#define HAVE_STDARG_H + +#define atoll _atoi64 +#define fdatasync commit +#define __inline__ inline +#define __attribute__(X) /*nothing*/ + +#else /* !defined WINDOWS */ + +#ifndef O_BINARY +#define O_BINARY 0 /* unix is binary by default */ +#endif + +#endif /* defined WINDOWS */ + +#endif /* defined _NTFS_COMPAT_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/compress.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/compress.h new file mode 100644 index 0000000..c256932 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/compress.h @@ -0,0 +1,41 @@ +/* + * compress.h - Exports for compressed attribute handling. + * Originated from the Linux-NTFS project. + * + * Copyright (c) 2004 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_COMPRESS_H +#define _NTFS_COMPRESS_H + +#include "types.h" +#include "attrib.h" + +extern s64 ntfs_compressed_attr_pread(ntfs_attr *na, s64 pos, s64 count, + void *b); + +extern s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *brl, s64 wpos, + s64 offs, s64 to_write, s64 rounded, + const void *b, int compressed_part, + VCN *update_from); + +extern int ntfs_compressed_close(ntfs_attr *na, runlist_element *brl, + s64 offs, VCN *update_from); + +#endif /* defined _NTFS_COMPRESS_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/debug.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/debug.h new file mode 100644 index 0000000..f7f3c6f --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/debug.h @@ -0,0 +1,47 @@ +/* + * debug.h - Debugging output functions. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002-2004 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_DEBUG_H +#define _NTFS_DEBUG_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "logging.h" + +struct _runlist_element; + +#ifdef DEBUG +extern void ntfs_debug_runlist_dump(const struct _runlist_element *rl); +#else +static __inline__ void ntfs_debug_runlist_dump(const struct _runlist_element *rl __attribute__((unused))) {} +#endif + +#define NTFS_BUG(msg) \ +{ \ + int ___i = 1; \ + ntfs_log_critical("Bug in %s(): %s\n", __FUNCTION__, msg); \ + ntfs_log_debug("Forcing segmentation fault!"); \ + ___i = ((int*)NULL)[___i]; \ +} + +#endif /* defined _NTFS_DEBUG_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/device.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/device.h new file mode 100644 index 0000000..709cb4f --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/device.h @@ -0,0 +1,145 @@ +/* + * device.h - Exports for low level device io. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2013 Anton Altaparmakov + * Copyright (c) 2008-2013 Tuxera Inc. + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_DEVICE_H +#define _NTFS_DEVICE_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "device_io.h" +#include "types.h" +#include "support.h" +#include "volume.h" + +/** + * enum ntfs_device_state_bits - + * + * Defined bits for the state field in the ntfs_device structure. + */ +typedef enum { + ND_Open, /* 1: Device is open. */ + ND_ReadOnly, /* 1: Device is read-only. */ + ND_Dirty, /* 1: Device is dirty, needs sync. */ + ND_Block, /* 1: Device is a block device. */ + ND_Sync, /* 1: Device is mounted with "-o sync" */ +} ntfs_device_state_bits; + +#define test_ndev_flag(nd, flag) test_bit(ND_##flag, (nd)->d_state) +#define set_ndev_flag(nd, flag) set_bit(ND_##flag, (nd)->d_state) +#define clear_ndev_flag(nd, flag) clear_bit(ND_##flag, (nd)->d_state) + +#define NDevOpen(nd) test_ndev_flag(nd, Open) +#define NDevSetOpen(nd) set_ndev_flag(nd, Open) +#define NDevClearOpen(nd) clear_ndev_flag(nd, Open) + +#define NDevReadOnly(nd) test_ndev_flag(nd, ReadOnly) +#define NDevSetReadOnly(nd) set_ndev_flag(nd, ReadOnly) +#define NDevClearReadOnly(nd) clear_ndev_flag(nd, ReadOnly) + +#define NDevDirty(nd) test_ndev_flag(nd, Dirty) +#define NDevSetDirty(nd) set_ndev_flag(nd, Dirty) +#define NDevClearDirty(nd) clear_ndev_flag(nd, Dirty) + +#define NDevBlock(nd) test_ndev_flag(nd, Block) +#define NDevSetBlock(nd) set_ndev_flag(nd, Block) +#define NDevClearBlock(nd) clear_ndev_flag(nd, Block) + +#define NDevSync(nd) test_ndev_flag(nd, Sync) +#define NDevSetSync(nd) set_ndev_flag(nd, Sync) +#define NDevClearSync(nd) clear_ndev_flag(nd, Sync) + +/** + * struct ntfs_device - + * + * The ntfs device structure defining all operations needed to access the low + * level device underlying the ntfs volume. + * + * Note d_heads and d_sectors_per_track are only set as a result of a call to + * either ntfs_device_heads_get() or ntfs_device_sectors_per_track_get() (both + * calls will set up both fields or if getting them failed they will be left at + * -1). + */ +struct ntfs_device { + struct ntfs_device_operations *d_ops; /* Device operations. */ + unsigned long d_state; /* State of the device. */ + char *d_name; /* Name of device. */ + void *d_private; /* Private data used by the + device operations. */ + int d_heads; /* Disk geometry: number of + heads or -1. */ + int d_sectors_per_track; /* Disk geometry: number of + sectors per track or -1. */ +}; + +struct stat; + +/** + * struct ntfs_device_operations - + * + * The ntfs device operations defining all operations that can be performed on + * the low level device described by an ntfs device structure. + */ +struct ntfs_device_operations { + int (*open)(struct ntfs_device *dev, int flags); + int (*close)(struct ntfs_device *dev); + s64 (*seek)(struct ntfs_device *dev, s64 offset, int whence); + s64 (*read)(struct ntfs_device *dev, void *buf, s64 count); + s64 (*write)(struct ntfs_device *dev, const void *buf, s64 count); + s64 (*pread)(struct ntfs_device *dev, void *buf, s64 count, s64 offset); + s64 (*pwrite)(struct ntfs_device *dev, const void *buf, s64 count, + s64 offset); + int (*sync)(struct ntfs_device *dev); + int (*stat)(struct ntfs_device *dev, struct stat *buf); + int (*ioctl)(struct ntfs_device *dev, unsigned long request, + void *argp); +}; + +extern struct ntfs_device *ntfs_device_alloc(const char *name, const long state, + struct ntfs_device_operations *dops, void *priv_data); +extern int ntfs_device_free(struct ntfs_device *dev); +extern int ntfs_device_sync(struct ntfs_device *dev); + +extern s64 ntfs_pread(struct ntfs_device *dev, const s64 pos, s64 count, + void *b); +extern s64 ntfs_pwrite(struct ntfs_device *dev, const s64 pos, s64 count, + const void *b); + +extern s64 ntfs_mst_pread(struct ntfs_device *dev, const s64 pos, s64 count, + const u32 bksize, void *b); +extern s64 ntfs_mst_pwrite(struct ntfs_device *dev, const s64 pos, s64 count, + const u32 bksize, void *b); + +extern s64 ntfs_cluster_read(const ntfs_volume *vol, const s64 lcn, + const s64 count, void *b); +extern s64 ntfs_cluster_write(const ntfs_volume *vol, const s64 lcn, + const s64 count, const void *b); + +extern s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size); +extern s64 ntfs_device_partition_start_sector_get(struct ntfs_device *dev); +extern int ntfs_device_heads_get(struct ntfs_device *dev); +extern int ntfs_device_sectors_per_track_get(struct ntfs_device *dev); +extern int ntfs_device_sector_size_get(struct ntfs_device *dev); +extern int ntfs_device_block_size_set(struct ntfs_device *dev, int block_size); + +#endif /* defined _NTFS_DEVICE_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/device_io.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/device_io.h new file mode 100644 index 0000000..66ad243 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/device_io.h @@ -0,0 +1,88 @@ +/* + * device_io.h - Exports for default device io. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2006 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_DEVICE_IO_H +#define _NTFS_DEVICE_IO_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifndef NO_NTFS_DEVICE_DEFAULT_IO_OPS + +#if defined(linux) || defined(__uClinux__) || defined(__sun) \ + || defined(__APPLE__) || defined(__DARWIN__) + /* Make sure the presence of means compiling for Windows */ +#undef HAVE_WINDOWS_H +#endif + +#ifndef HAVE_WINDOWS_H + +/* Not for Windows use standard Unix style low level device operations. */ +#define ntfs_device_default_io_ops ntfs_device_unix_io_ops + +#else /* HAVE_WINDOWS_H */ + +#ifndef HDIO_GETGEO +# define HDIO_GETGEO 0x301 +/** + * struct hd_geometry - + */ +struct hd_geometry { + unsigned char heads; + unsigned char sectors; + unsigned short cylinders; + unsigned long start; +}; +#endif +#ifndef BLKGETSIZE +# define BLKGETSIZE 0x1260 +#endif +#ifndef BLKSSZGET +# define BLKSSZGET 0x1268 +#endif +#ifndef BLKGETSIZE64 +# define BLKGETSIZE64 0x80041272 +#endif +#ifndef BLKBSZSET +# define BLKBSZSET 0x40041271 +#endif + +/* On Windows (and Cygwin) : use Win32 low level device operations. */ +#define ntfs_device_default_io_ops ntfs_device_win32_io_ops + +/* A few useful functions */ +int ntfs_win32_set_sparse(int); +int ntfs_win32_ftruncate(int fd, s64 size); +int ntfs_device_win32_ftruncate(struct ntfs_device*, s64); + +#endif /* HAVE_WINDOWS_H */ + + +/* Forward declaration. */ +struct ntfs_device_operations; + +extern struct ntfs_device_operations ntfs_device_default_io_ops; + +#endif /* NO_NTFS_DEVICE_DEFAULT_IO_OPS */ + +#endif /* defined _NTFS_DEVICE_IO_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/dir.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/dir.h new file mode 100644 index 0000000..6016915 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/dir.h @@ -0,0 +1,132 @@ +/* + * dir.h - Exports for directory handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002 Anton Altaparmakov + * Copyright (c) 2005-2006 Yura Pakhuchiy + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2005-2008 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_DIR_H +#define _NTFS_DIR_H + +#include "types.h" + +#define PATH_SEP '/' + +/* + * We do not have these under DJGPP, so define our version that do not conflict + * with other S_IFs defined under DJGPP. + */ +#ifdef DJGPP +#ifndef S_IFLNK +#define S_IFLNK 0120000 +#endif +#ifndef S_ISLNK +#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) +#endif +#ifndef S_IFSOCK +#define S_IFSOCK 0140000 +#endif +#ifndef S_ISSOCK +#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) +#endif +#endif + +/* + * The little endian Unicode strings $I30, $SII, $SDH, $O, $Q, $R + * as a global constant. + */ +extern ntfschar NTFS_INDEX_I30[5]; +extern ntfschar NTFS_INDEX_SII[5]; +extern ntfschar NTFS_INDEX_SDH[5]; +extern ntfschar NTFS_INDEX_O[3]; +extern ntfschar NTFS_INDEX_Q[3]; +extern ntfschar NTFS_INDEX_R[3]; + +extern u64 ntfs_inode_lookup_by_name(ntfs_inode *dir_ni, + const ntfschar *uname, const int uname_len); +extern u64 ntfs_inode_lookup_by_mbsname(ntfs_inode *dir_ni, const char *name); +extern void ntfs_inode_update_mbsname(ntfs_inode *dir_ni, const char *name, + u64 inum); + +extern ntfs_inode *ntfs_pathname_to_inode(ntfs_volume *vol, ntfs_inode *parent, + const char *pathname); +extern ntfs_inode *ntfs_create(ntfs_inode *dir_ni, le32 securid, + const ntfschar *name, u8 name_len, mode_t type); +extern ntfs_inode *ntfs_create_device(ntfs_inode *dir_ni, le32 securid, + const ntfschar *name, u8 name_len, mode_t type, dev_t dev); +extern ntfs_inode *ntfs_create_symlink(ntfs_inode *dir_ni, le32 securid, + const ntfschar *name, u8 name_len, const ntfschar *target, + int target_len); +extern int ntfs_check_empty_dir(ntfs_inode *ni); +extern int ntfs_delete(ntfs_volume *vol, const char *path, + ntfs_inode *ni, ntfs_inode *dir_ni, const ntfschar *name, + u8 name_len); + +extern int ntfs_link(ntfs_inode *ni, ntfs_inode *dir_ni, const ntfschar *name, + u8 name_len); + +/* + * File types (adapted from include ) + */ +#define NTFS_DT_UNKNOWN 0 +#define NTFS_DT_FIFO 1 +#define NTFS_DT_CHR 2 +#define NTFS_DT_DIR 4 +#define NTFS_DT_BLK 6 +#define NTFS_DT_REG 8 +#define NTFS_DT_LNK 10 +#define NTFS_DT_SOCK 12 +#define NTFS_DT_WHT 14 +#define NTFS_DT_REPARSE 32 + +/* + * This is the "ntfs_filldir" function type, used by ntfs_readdir() to let + * the caller specify what kind of dirent layout it wants to have. + * This allows the caller to read directories into their application or + * to have different dirent layouts depending on the binary type. + */ +typedef int (*ntfs_filldir_t)(void *dirent, const ntfschar *name, + const int name_len, const int name_type, const s64 pos, + const MFT_REF mref, const unsigned dt_type); + +extern int ntfs_readdir(ntfs_inode *dir_ni, s64 *pos, + void *dirent, ntfs_filldir_t filldir); + +ntfs_inode *ntfs_dir_parent_inode(ntfs_inode *ni); +u32 ntfs_interix_types(ntfs_inode *ni); + +int ntfs_get_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni, + char *value, size_t size); +int ntfs_set_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni, + const char *value, size_t size, int flags); +int ntfs_remove_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni); +int ntfs_dir_link_cnt(ntfs_inode *ni); + +#if CACHE_INODE_SIZE + +struct CACHED_GENERIC; + +extern int ntfs_dir_inode_hash(const struct CACHED_GENERIC *cached); +extern int ntfs_dir_lookup_hash(const struct CACHED_GENERIC *cached); + +#endif + +#endif /* defined _NTFS_DIR_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/ea.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/ea.h new file mode 100644 index 0000000..cd61fa2 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/ea.h @@ -0,0 +1,37 @@ +/* + * + * Copyright (c) 2014-2021 Jean-Pierre Andre + * + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef EA_H +#define EA_H + +int ntfs_ea_check_wsldev(ntfs_inode *ni, dev_t *rdevp); + +int ntfs_ea_set_wsl_not_symlink(ntfs_inode *ni, mode_t mode, dev_t dev); + +int ntfs_get_ntfs_ea(ntfs_inode *ni, char *value, size_t size); + +int ntfs_set_ntfs_ea(ntfs_inode *ni, const char *value, size_t size, int flags); + +int ntfs_remove_ntfs_ea(ntfs_inode *ni); + +#endif /* EA_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/efs.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/efs.h new file mode 100644 index 0000000..6eada06 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/efs.h @@ -0,0 +1,30 @@ +/* + * + * Copyright (c) 2009 Martin Bene + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef EFS_H +#define EFS_H + +int ntfs_get_efs_info(ntfs_inode *ni, char *value, size_t size); + +int ntfs_set_efs_info(ntfs_inode *ni, + const char *value, size_t size, int flags); +int ntfs_efs_fixup_attribute(ntfs_attr_search_ctx *ctx, ntfs_attr *na); + +#endif /* EFS_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/endians.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/endians.h new file mode 100644 index 0000000..948295f --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/endians.h @@ -0,0 +1,305 @@ +/* + * endians.h - Definitions related to handling of byte ordering. + * Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2005 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_ENDIANS_H +#define _NTFS_ENDIANS_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +/* + * Notes: + * We define the conversion functions including typecasts since the + * defaults don't necessarily perform appropriate typecasts. + * Also, using our own functions means that we can change them if it + * turns out that we do need to use the unaligned access macros on + * architectures requiring aligned memory accesses... + */ + +#ifdef HAVE_ENDIAN_H +#include +#endif +#ifdef HAVE_SYS_ENDIAN_H +#include +#endif +#ifdef HAVE_MACHINE_ENDIAN_H +#include +#endif +#ifdef HAVE_SYS_BYTEORDER_H +#include +#endif +#ifdef HAVE_SYS_PARAM_H +#include +#endif + +#ifndef __BYTE_ORDER +# if defined(_BYTE_ORDER) +# define __BYTE_ORDER _BYTE_ORDER +# define __LITTLE_ENDIAN _LITTLE_ENDIAN +# define __BIG_ENDIAN _BIG_ENDIAN +# elif defined(BYTE_ORDER) +# define __BYTE_ORDER BYTE_ORDER +# define __LITTLE_ENDIAN LITTLE_ENDIAN +# define __BIG_ENDIAN BIG_ENDIAN +# elif defined(__BYTE_ORDER__) && defined(__LITTLE_ENDIAN__) && \ + defined(__BIG_ENDIAN__) +# define __BYTE_ORDER __BYTE_ORDER__ +# define __LITTLE_ENDIAN __LITTLE_ENDIAN__ +# define __BIG_ENDIAN __BIG_ENDIAN__ +# elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \ + defined(__ORDER_BIG_ENDIAN__) +# define __BYTE_ORDER __BYTE_ORDER__ +# define __LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ +# define __BIG_ENDIAN __ORDER_BIG_ENDIAN__ +# elif (defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)) || \ + defined(WORDS_LITTLEENDIAN) +# define __BYTE_ORDER 1 +# define __LITTLE_ENDIAN 1 +# define __BIG_ENDIAN 0 +# elif (!defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)) || \ + defined(WORDS_BIGENDIAN) +# define __BYTE_ORDER 0 +# define __LITTLE_ENDIAN 1 +# define __BIG_ENDIAN 0 +# else +# error "__BYTE_ORDER is not defined." +# endif +#endif + +#define __ntfs_bswap_constant_16(x) \ + (u16)((((u16)(x) & 0xff00) >> 8) | \ + (((u16)(x) & 0x00ff) << 8)) + +#define __ntfs_bswap_constant_32(x) \ + (u32)((((u32)(x) & 0xff000000u) >> 24) | \ + (((u32)(x) & 0x00ff0000u) >> 8) | \ + (((u32)(x) & 0x0000ff00u) << 8) | \ + (((u32)(x) & 0x000000ffu) << 24)) + +#define __ntfs_bswap_constant_64(x) \ + (u64)((((u64)(x) & 0xff00000000000000ull) >> 56) | \ + (((u64)(x) & 0x00ff000000000000ull) >> 40) | \ + (((u64)(x) & 0x0000ff0000000000ull) >> 24) | \ + (((u64)(x) & 0x000000ff00000000ull) >> 8) | \ + (((u64)(x) & 0x00000000ff000000ull) << 8) | \ + (((u64)(x) & 0x0000000000ff0000ull) << 24) | \ + (((u64)(x) & 0x000000000000ff00ull) << 40) | \ + (((u64)(x) & 0x00000000000000ffull) << 56)) + +#ifdef HAVE_BYTESWAP_H +# include +#else +# define bswap_16(x) __ntfs_bswap_constant_16(x) +# define bswap_32(x) __ntfs_bswap_constant_32(x) +# define bswap_64(x) __ntfs_bswap_constant_64(x) +#endif + +#if defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN) + +#define __le16_to_cpu(x) (x) +#define __le32_to_cpu(x) (x) +#define __le64_to_cpu(x) (x) + +#define __cpu_to_le16(x) (x) +#define __cpu_to_le32(x) (x) +#define __cpu_to_le64(x) (x) + +#define __constant_le16_to_cpu(x) (x) +#define __constant_le32_to_cpu(x) (x) +#define __constant_le64_to_cpu(x) (x) + +#define __constant_cpu_to_le16(x) (x) +#define __constant_cpu_to_le32(x) (x) +#define __constant_cpu_to_le64(x) (x) + +#define __be16_to_cpu(x) bswap_16(x) +#define __be32_to_cpu(x) bswap_32(x) +#define __be64_to_cpu(x) bswap_64(x) + +#define __cpu_to_be16(x) bswap_16(x) +#define __cpu_to_be32(x) bswap_32(x) +#define __cpu_to_be64(x) bswap_64(x) + +#define __constant_be16_to_cpu(x) __ntfs_bswap_constant_16((u16)(x)) +#define __constant_be32_to_cpu(x) __ntfs_bswap_constant_32((u32)(x)) +#define __constant_be64_to_cpu(x) __ntfs_bswap_constant_64((u64)(x)) + +#define __constant_cpu_to_be16(x) __ntfs_bswap_constant_16((u16)(x)) +#define __constant_cpu_to_be32(x) __ntfs_bswap_constant_32((u32)(x)) +#define __constant_cpu_to_be64(x) __ntfs_bswap_constant_64((u64)(x)) + +#elif defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN) + +#define __le16_to_cpu(x) bswap_16(x) +#define __le32_to_cpu(x) bswap_32(x) +#define __le64_to_cpu(x) bswap_64(x) + +#define __cpu_to_le16(x) bswap_16(x) +#define __cpu_to_le32(x) bswap_32(x) +#define __cpu_to_le64(x) bswap_64(x) + +#define __constant_le16_to_cpu(x) __ntfs_bswap_constant_16((u16)(x)) +#define __constant_le32_to_cpu(x) __ntfs_bswap_constant_32((u32)(x)) +#define __constant_le64_to_cpu(x) __ntfs_bswap_constant_64((u64)(x)) + +#define __constant_cpu_to_le16(x) __ntfs_bswap_constant_16((u16)(x)) +#define __constant_cpu_to_le32(x) __ntfs_bswap_constant_32((u32)(x)) +#define __constant_cpu_to_le64(x) __ntfs_bswap_constant_64((u64)(x)) + +#define __be16_to_cpu(x) (x) +#define __be32_to_cpu(x) (x) +#define __be64_to_cpu(x) (x) + +#define __cpu_to_be16(x) (x) +#define __cpu_to_be32(x) (x) +#define __cpu_to_be64(x) (x) + +#define __constant_be16_to_cpu(x) (x) +#define __constant_be32_to_cpu(x) (x) +#define __constant_be64_to_cpu(x) (x) + +#define __constant_cpu_to_be16(x) (x) +#define __constant_cpu_to_be32(x) (x) +#define __constant_cpu_to_be64(x) (x) + +#else + +#error "You must define __BYTE_ORDER to be __LITTLE_ENDIAN or __BIG_ENDIAN." + +#endif + +/* Unsigned from LE to CPU conversion. */ + +#define le16_to_cpu(x) (u16)__le16_to_cpu((u16)(x)) +#define le32_to_cpu(x) (u32)__le32_to_cpu((u32)(x)) +#define le64_to_cpu(x) (u64)__le64_to_cpu((u64)(x)) + +#define le16_to_cpup(x) (u16)__le16_to_cpu(*(const u16*)(x)) +#define le32_to_cpup(x) (u32)__le32_to_cpu(*(const u32*)(x)) +#define le64_to_cpup(x) (u64)__le64_to_cpu(*(const u64*)(x)) + +/* Signed from LE to CPU conversion. */ + +#define sle16_to_cpu(x) (s16)__le16_to_cpu((s16)(x)) +#define sle32_to_cpu(x) (s32)__le32_to_cpu((s32)(x)) +#define sle64_to_cpu(x) (s64)__le64_to_cpu((s64)(x)) + +#define sle16_to_cpup(x) (s16)__le16_to_cpu(*(s16*)(x)) +#define sle32_to_cpup(x) (s32)__le32_to_cpu(*(s32*)(x)) +#define sle64_to_cpup(x) (s64)__le64_to_cpu(*(s64*)(x)) + +/* Unsigned from CPU to LE conversion. */ + +#define cpu_to_le16(x) (u16)__cpu_to_le16((u16)(x)) +#define cpu_to_le32(x) (u32)__cpu_to_le32((u32)(x)) +#define cpu_to_le64(x) (u64)__cpu_to_le64((u64)(x)) + +#define cpu_to_le16p(x) (u16)__cpu_to_le16(*(u16*)(x)) +#define cpu_to_le32p(x) (u32)__cpu_to_le32(*(u32*)(x)) +#define cpu_to_le64p(x) (u64)__cpu_to_le64(*(u64*)(x)) + +/* Signed from CPU to LE conversion. */ + +#define cpu_to_sle16(x) (s16)__cpu_to_le16((s16)(x)) +#define cpu_to_sle32(x) (s32)__cpu_to_le32((s32)(x)) +#define cpu_to_sle64(x) (s64)__cpu_to_le64((s64)(x)) + +#define cpu_to_sle16p(x) (s16)__cpu_to_le16(*(s16*)(x)) +#define cpu_to_sle32p(x) (s32)__cpu_to_le32(*(s32*)(x)) +#define cpu_to_sle64p(x) (s64)__cpu_to_le64(*(s64*)(x)) + +/* Unsigned from BE to CPU conversion. */ + +#define be16_to_cpu(x) (u16)__be16_to_cpu((u16)(x)) +#define be32_to_cpu(x) (u32)__be32_to_cpu((u32)(x)) +#define be64_to_cpu(x) (u64)__be64_to_cpu((u64)(x)) + +#define be16_to_cpup(x) (u16)__be16_to_cpu(*(const u16*)(x)) +#define be32_to_cpup(x) (u32)__be32_to_cpu(*(const u32*)(x)) +#define be64_to_cpup(x) (u64)__be64_to_cpu(*(const u64*)(x)) + +/* Signed from BE to CPU conversion. */ + +#define sbe16_to_cpu(x) (s16)__be16_to_cpu((s16)(x)) +#define sbe32_to_cpu(x) (s32)__be32_to_cpu((s32)(x)) +#define sbe64_to_cpu(x) (s64)__be64_to_cpu((s64)(x)) + +#define sbe16_to_cpup(x) (s16)__be16_to_cpu(*(s16*)(x)) +#define sbe32_to_cpup(x) (s32)__be32_to_cpu(*(s32*)(x)) +#define sbe64_to_cpup(x) (s64)__be64_to_cpu(*(s64*)(x)) + +/* Unsigned from CPU to BE conversion. */ + +#define cpu_to_be16(x) (u16)__cpu_to_be16((u16)(x)) +#define cpu_to_be32(x) (u32)__cpu_to_be32((u32)(x)) +#define cpu_to_be64(x) (u64)__cpu_to_be64((u64)(x)) + +#define cpu_to_be16p(x) (u16)__cpu_to_be16(*(u16*)(x)) +#define cpu_to_be32p(x) (u32)__cpu_to_be32(*(u32*)(x)) +#define cpu_to_be64p(x) (u64)__cpu_to_be64(*(u64*)(x)) + +/* Signed from CPU to BE conversion. */ + +#define cpu_to_sbe16(x) (s16)__cpu_to_be16((s16)(x)) +#define cpu_to_sbe32(x) (s32)__cpu_to_be32((s32)(x)) +#define cpu_to_sbe64(x) (s64)__cpu_to_be64((s64)(x)) + +#define cpu_to_sbe16p(x) (s16)__cpu_to_be16(*(s16*)(x)) +#define cpu_to_sbe32p(x) (s32)__cpu_to_be32(*(s32*)(x)) +#define cpu_to_sbe64p(x) (s64)__cpu_to_be64(*(s64*)(x)) + +/* Constant endianness conversion defines. */ + +#define const_le16_to_cpu(x) ((u16) __constant_le16_to_cpu(x)) +#define const_le32_to_cpu(x) ((u32) __constant_le32_to_cpu(x)) +#define const_le64_to_cpu(x) ((u64) __constant_le64_to_cpu(x)) + +#define const_cpu_to_le16(x) ((le16) __constant_cpu_to_le16(x)) +#define const_cpu_to_le32(x) ((le32) __constant_cpu_to_le32(x)) +#define const_cpu_to_le64(x) ((le64) __constant_cpu_to_le64(x)) + +#define const_sle16_to_cpu(x) ((s16) __constant_le16_to_cpu((le16) x)) +#define const_sle32_to_cpu(x) ((s32) __constant_le32_to_cpu((le32) x)) +#define const_sle64_to_cpu(x) ((s64) __constant_le64_to_cpu((le64) x)) + +#define const_cpu_to_sle16(x) ((sle16) __constant_cpu_to_le16((u16) x)) +#define const_cpu_to_sle32(x) ((sle32) __constant_cpu_to_le32((u32) x)) +#define const_cpu_to_sle64(x) ((sle64) __constant_cpu_to_le64((u64) x)) + +#define const_be16_to_cpu(x) ((u16) __constant_be16_to_cpu(x))) +#define const_be32_to_cpu(x) ((u32) __constant_be32_to_cpu(x))) +#define const_be64_to_cpu(x) ((u64) __constant_be64_to_cpu(x))) + +#define const_cpu_to_be16(x) ((be16) __constant_cpu_to_be16(x)) +#define const_cpu_to_be32(x) ((be32) __constant_cpu_to_be32(x)) +#define const_cpu_to_be64(x) ((be64) __constant_cpu_to_be64(x)) + +#define const_sbe16_to_cpu(x) ((s16) __constant_be16_to_cpu((be16) x)) +#define const_sbe32_to_cpu(x) ((s32) __constant_be32_to_cpu((be32) x)) +#define const_sbe64_to_cpu(x) ((s64) __constant_be64_to_cpu((be64) x)) + +#define const_cpu_to_sbe16(x) ((sbe16) __constant_cpu_to_be16((u16) x)) +#define const_cpu_to_sbe32(x) ((sbe32) __constant_cpu_to_be32((u32) x)) +#define const_cpu_to_sbe64(x) ((sbe64) __constant_cpu_to_be64((u64) x)) + +#endif /* defined _NTFS_ENDIANS_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/index.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/index.h new file mode 100644 index 0000000..1d5845d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/index.h @@ -0,0 +1,173 @@ +/* + * index.h - Defines for NTFS index handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2004 Anton Altaparmakov + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2005 Yura Pakhuchiy + * Copyright (c) 2006-2008 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_INDEX_H +#define _NTFS_INDEX_H + +/* Convenience macros to test the versions of gcc. + * Use them like this: + * #if __GNUC_PREREQ (2,8) + * ... code requiring gcc 2.8 or later ... + * #endif + * Note - they won't work for gcc1 or glibc1, since the _MINOR macros + * were not defined then. + */ + +#ifndef __GNUC_PREREQ +# if defined __GNUC__ && defined __GNUC_MINOR__ +# define __GNUC_PREREQ(maj, min) \ + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) +# else +# define __GNUC_PREREQ(maj, min) 0 +# endif +#endif + +/* allows us to warn about unused results of certain function calls */ +#ifndef __attribute_warn_unused_result__ +# if __GNUC_PREREQ (3,4) +# define __attribute_warn_unused_result__ \ + __attribute__ ((__warn_unused_result__)) +# else +# define __attribute_warn_unused_result__ /* empty */ +# endif +#endif + +#include "attrib.h" +#include "types.h" +#include "layout.h" +#include "inode.h" +#include "mft.h" + +#define VCN_INDEX_ROOT_PARENT ((VCN)-2) + +#define MAX_PARENT_VCN 32 + +typedef int (*COLLATE)(ntfs_volume *vol, const void *data1, int len1, + const void *data2, int len2); + +/** + * struct ntfs_index_context - + * @ni: inode containing the @entry described by this context + * @name: name of the index described by this context + * @name_len: length of the index name + * @entry: index entry (points into @ir or @ia) + * @data: index entry data (points into @entry) + * @data_len: length in bytes of @data + * @is_in_root: TRUE if @entry is in @ir or FALSE if it is in @ia + * @ir: index root if @is_in_root or NULL otherwise + * @actx: attribute search context if in root or NULL otherwise + * @ia: index block if @is_in_root is FALSE or NULL otherwise + * @ia_na: opened INDEX_ALLOCATION attribute + * @parent_pos: parent entries' positions in the index block + * @parent_vcn: entry's parent node or VCN_INDEX_ROOT_PARENT + * @new_vcn: new VCN if we need to create a new index block + * @median: move to the parent if splitting index blocks + * @ib_dirty: TRUE if index block was changed + * @block_size: index block size + * @vcn_size_bits: VCN size bits for this index block + * + * @ni is the inode this context belongs to. + * + * @entry is the index entry described by this context. @data and @data_len + * are the index entry data and its length in bytes, respectively. @data + * simply points into @entry. This is probably what the user is interested in. + * + * If @is_in_root is TRUE, @entry is in the index root attribute @ir described + * by the attribute search context @actx and inode @ni. @ia and + * @ib_dirty are undefined in this case. + * + * If @is_in_root is FALSE, @entry is in the index allocation attribute and @ia + * point to the index allocation block and VCN where it's placed, + * respectively. @ir and @actx are NULL in this case. @ia_na is opened + * INDEX_ALLOCATION attribute. @ib_dirty is TRUE if index block was changed and + * FALSE otherwise. + * + * To obtain a context call ntfs_index_ctx_get(). + * + * When finished with the @entry and its @data, call ntfs_index_ctx_put() to + * free the context and other associated resources. + * + * If the index entry was modified, call ntfs_index_entry_mark_dirty() before + * the call to ntfs_index_ctx_put() to ensure that the changes are written + * to disk. + */ +typedef struct { + ntfs_inode *ni; + ntfschar *name; + u32 name_len; + INDEX_ENTRY *entry; + void *data; + u16 data_len; + COLLATE collate; + BOOL is_in_root; + INDEX_ROOT *ir; + ntfs_attr_search_ctx *actx; + INDEX_BLOCK *ib; + ntfs_attr *ia_na; + int parent_pos[MAX_PARENT_VCN]; /* parent entries' positions */ + VCN parent_vcn[MAX_PARENT_VCN]; /* entry's parent nodes */ + int pindex; /* maximum it's the number of the parent nodes */ + BOOL ib_dirty; + BOOL bad_index; + u32 block_size; + u8 vcn_size_bits; +} ntfs_index_context; + +extern ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *ni, + ntfschar *name, u32 name_len); +extern void ntfs_index_ctx_put(ntfs_index_context *ictx); +extern void ntfs_index_ctx_reinit(ntfs_index_context *ictx); + +extern int ntfs_index_block_inconsistent(const INDEX_BLOCK *ib, u32 block_size, + u64 inum, VCN vcn); +extern int ntfs_index_entry_inconsistent(const INDEX_ENTRY *ie, + COLLATION_RULES collation_rule, u64 inum); +extern int ntfs_ie_stream_inconsistent(const INDEX_HEADER *ih, u64 inum); +extern int ntfs_index_lookup(const void *key, const int key_len, + ntfs_index_context *ictx) __attribute_warn_unused_result__; + +extern INDEX_ENTRY *ntfs_index_next(INDEX_ENTRY *ie, + ntfs_index_context *ictx); + +extern int ntfs_index_add_filename(ntfs_inode *ni, FILE_NAME_ATTR *fn, + MFT_REF mref); +extern int ntfs_index_remove(ntfs_inode *dir_ni, ntfs_inode *ni, + const void *key, const int keylen); + +extern INDEX_ROOT *ntfs_index_root_get(ntfs_inode *ni, ATTR_RECORD *attr); + +extern VCN ntfs_ie_get_vcn(INDEX_ENTRY *ie); + +extern void ntfs_index_entry_mark_dirty(ntfs_index_context *ictx); + +extern char *ntfs_ie_filename_get(INDEX_ENTRY *ie); +extern void ntfs_ie_filename_dump(INDEX_ENTRY *ie); +extern void ntfs_ih_filename_dump(INDEX_HEADER *ih); + +/* the following was added by JPA for use in security.c */ +extern int ntfs_ie_add(ntfs_index_context *icx, INDEX_ENTRY *ie); +extern int ntfs_index_rm(ntfs_index_context *icx); + +#endif /* _NTFS_INDEX_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/inode.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/inode.h new file mode 100644 index 0000000..8d58d24 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/inode.h @@ -0,0 +1,225 @@ +/* + * inode.h - Defines for NTFS inode handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2001-2004 Anton Altaparmakov + * Copyright (c) 2004-2007 Yura Pakhuchiy + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2006-2008 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_INODE_H +#define _NTFS_INODE_H + +/* Forward declaration */ +typedef struct _ntfs_inode ntfs_inode; + +#include "types.h" +#include "layout.h" +#include "support.h" +#include "volume.h" +#include "ntfstime.h" + +/** + * enum ntfs_inode_state_bits - + * + * Defined bits for the state field in the ntfs_inode structure. + * (f) = files only, (d) = directories only + */ +typedef enum { + NI_Dirty, /* 1: Mft record needs to be written to disk. */ + + /* The NI_AttrList* tests only make sense for base inodes. */ + NI_AttrList, /* 1: Mft record contains an attribute list. */ + NI_AttrListDirty, /* 1: Attribute list needs to be written to the + mft record and then to disk. */ + NI_FileNameDirty, /* 1: FILE_NAME attributes need to be updated + in the index. */ + NI_v3_Extensions, /* 1: JPA v3.x extensions present. */ + NI_TimesSet, /* 1: Use times which were set */ + NI_KnownSize, /* 1: Set if sizes are meaningful */ +} ntfs_inode_state_bits; + +#define test_nino_flag(ni, flag) test_bit(NI_##flag, (ni)->state) +#define set_nino_flag(ni, flag) set_bit(NI_##flag, (ni)->state) +#define clear_nino_flag(ni, flag) clear_bit(NI_##flag, (ni)->state) + +#define test_and_set_nino_flag(ni, flag) \ + test_and_set_bit(NI_##flag, (ni)->state) +#define test_and_clear_nino_flag(ni, flag) \ + test_and_clear_bit(NI_##flag, (ni)->state) + +#define NInoDirty(ni) test_nino_flag(ni, Dirty) +#define NInoSetDirty(ni) set_nino_flag(ni, Dirty) +#define NInoClearDirty(ni) clear_nino_flag(ni, Dirty) +#define NInoTestAndSetDirty(ni) test_and_set_nino_flag(ni, Dirty) +#define NInoTestAndClearDirty(ni) test_and_clear_nino_flag(ni, Dirty) + +#define NInoAttrList(ni) test_nino_flag(ni, AttrList) +#define NInoSetAttrList(ni) set_nino_flag(ni, AttrList) +#define NInoClearAttrList(ni) clear_nino_flag(ni, AttrList) + + +#define test_nino_al_flag(ni, flag) test_nino_flag(ni, AttrList##flag) +#define set_nino_al_flag(ni, flag) set_nino_flag(ni, AttrList##flag) +#define clear_nino_al_flag(ni, flag) clear_nino_flag(ni, AttrList##flag) + +#define test_and_set_nino_al_flag(ni, flag) \ + test_and_set_nino_flag(ni, AttrList##flag) +#define test_and_clear_nino_al_flag(ni, flag) \ + test_and_clear_nino_flag(ni, AttrList##flag) + +#define NInoAttrListDirty(ni) test_nino_al_flag(ni, Dirty) +#define NInoAttrListSetDirty(ni) set_nino_al_flag(ni, Dirty) +#define NInoAttrListClearDirty(ni) clear_nino_al_flag(ni, Dirty) +#define NInoAttrListTestAndSetDirty(ni) test_and_set_nino_al_flag(ni, Dirty) +#define NInoAttrListTestAndClearDirty(ni) test_and_clear_nino_al_flag(ni, Dirty) + +#define NInoFileNameDirty(ni) test_nino_flag(ni, FileNameDirty) +#define NInoFileNameSetDirty(ni) set_nino_flag(ni, FileNameDirty) +#define NInoFileNameClearDirty(ni) clear_nino_flag(ni, FileNameDirty) +#define NInoFileNameTestAndSetDirty(ni) \ + test_and_set_nino_flag(ni, FileNameDirty) +#define NInoFileNameTestAndClearDirty(ni) \ + test_and_clear_nino_flag(ni, FileNameDirty) + +/** + * struct _ntfs_inode - The NTFS in-memory inode structure. + * + * It is just used as an extension to the fields already provided in the VFS + * inode. + */ +struct _ntfs_inode { + u64 mft_no; /* Inode / mft record number. */ + MFT_RECORD *mrec; /* The actual mft record of the inode. */ + ntfs_volume *vol; /* Pointer to the ntfs volume of this inode. */ + unsigned long state; /* NTFS specific flags describing this inode. + See ntfs_inode_state_bits above. */ + FILE_ATTR_FLAGS flags; /* Flags describing the file. + (Copy from STANDARD_INFORMATION) */ + /* + * Attribute list support (for use by the attribute lookup functions). + * Setup during ntfs_open_inode() for all inodes with attribute lists. + * Only valid if NI_AttrList is set in state. + */ + u32 attr_list_size; /* Length of attribute list value in bytes. */ + u8 *attr_list; /* Attribute list value itself. */ + /* Below fields are always valid. */ + s32 nr_extents; /* For a base mft record, the number of + attached extent inodes (0 if none), for + extent records this is -1. */ + union { /* This union is only used if nr_extents != 0. */ + ntfs_inode **extent_nis;/* For nr_extents > 0, array of the + ntfs inodes of the extent mft + records belonging to this base + inode which have been loaded. */ + ntfs_inode *base_ni; /* For nr_extents == -1, the ntfs + inode of the base mft record. */ + }; + + /* Below fields are valid only for base inode. */ + + /* + * These two fields are used to sync filename index and guaranteed to be + * correct, however value in index itself maybe wrong (windows itself + * do not update them properly). + * For directories, they hold the index size, provided the + * flag KnownSize is set. + */ + s64 data_size; /* Data size of unnamed DATA attribute + (or INDEX_ROOT for directories) */ + s64 allocated_size; /* Allocated size stored in the filename + index. (NOTE: Equal to allocated size of + the unnamed data attribute for normal or + encrypted files and to compressed size + of the unnamed data attribute for sparse or + compressed files.) */ + + /* + * These four fields are copy of relevant fields from + * STANDARD_INFORMATION attribute and used to sync it and FILE_NAME + * attribute in the index. + */ + ntfs_time creation_time; + ntfs_time last_data_change_time; + ntfs_time last_mft_change_time; + ntfs_time last_access_time; + /* NTFS 3.x extensions added by JPA */ + /* only if NI_v3_Extensions is set in state */ + le32 owner_id; + le32 security_id; + le64 quota_charged; + le64 usn; +}; + +typedef enum { + NTFS_UPDATE_ATIME = 1 << 0, + NTFS_UPDATE_MTIME = 1 << 1, + NTFS_UPDATE_CTIME = 1 << 2, +} ntfs_time_update_flags; + +#define NTFS_UPDATE_MCTIME (NTFS_UPDATE_MTIME | NTFS_UPDATE_CTIME) +#define NTFS_UPDATE_AMCTIME (NTFS_UPDATE_ATIME | NTFS_UPDATE_MCTIME) + +extern ntfs_inode *ntfs_inode_base(ntfs_inode *ni); + +extern ntfs_inode *ntfs_inode_allocate(ntfs_volume *vol); + +extern ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref); + +extern int ntfs_inode_close(ntfs_inode *ni); +extern int ntfs_inode_close_in_dir(ntfs_inode *ni, ntfs_inode *dir_ni); + +#if CACHE_NIDATA_SIZE + +struct CACHED_GENERIC; + +extern int ntfs_inode_real_close(ntfs_inode *ni); +extern void ntfs_inode_invalidate(ntfs_volume *vol, const MFT_REF mref); +extern void ntfs_inode_nidata_free(const struct CACHED_GENERIC *cached); +extern int ntfs_inode_nidata_hash(const struct CACHED_GENERIC *item); + +#endif + + +extern ntfs_inode *ntfs_extent_inode_open(ntfs_inode *base_ni, + const leMFT_REF mref); + +extern int ntfs_inode_attach_all_extents(ntfs_inode *ni); + +extern void ntfs_inode_mark_dirty(ntfs_inode *ni); + +extern void ntfs_inode_update_times(ntfs_inode *ni, ntfs_time_update_flags mask); + +extern int ntfs_inode_sync(ntfs_inode *ni); + +extern int ntfs_inode_add_attrlist(ntfs_inode *ni); + +extern int ntfs_inode_free_space(ntfs_inode *ni, int size); + +extern int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *a); + +extern int ntfs_inode_get_times(ntfs_inode *ni, char *value, size_t size); + +extern int ntfs_inode_set_times(ntfs_inode *ni, const char *value, + size_t size, int flags); + +/* debugging */ +#define debug_double_inode(num, type) +#define debug_cached_inode(ni) + +#endif /* defined _NTFS_INODE_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/ioctl.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/ioctl.h new file mode 100644 index 0000000..2b25c78 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/ioctl.h @@ -0,0 +1,35 @@ +/* + * + * Copyright (c) 2014 Jean-Pierre Andre + * + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef IOCTL_H +#define IOCTL_H + +/* + * Using an "unsigned long cmd" internally, like in for Linux + * Note however that fuse truncates the arg to 32 bits, and that + * some commands (e.g. FITRIM) do not fit in a signed 32 bit field. + */ +int ntfs_ioctl(ntfs_inode *ni, unsigned long cmd, void *arg, + unsigned int flags, void *data); + +#endif /* IOCTL_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/layout.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/layout.h new file mode 100644 index 0000000..05605c7 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/layout.h @@ -0,0 +1,2732 @@ +#ifdef _MSC_VER +#pragma pack(push, 1) +#endif +#ifdef _MSC_VER +#define NTFS_PACKED_ENUM_BEGIN(name, storage) enum +#define NTFS_PACKED_ENUM_END(name, storage) ; typedef storage name +#else +#define NTFS_PACKED_ENUM_BEGIN(name, storage) typedef enum +#define NTFS_PACKED_ENUM_END(name, storage) __attribute__((__packed__)) name +#endif + +/* + * layout.h - Ntfs on-disk layout structures. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2005 Anton Altaparmakov + * Copyright (c) 2005 Yura Pakhuchiy + * Copyright (c) 2005-2006 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_LAYOUT_H +#define _NTFS_LAYOUT_H + +#include "types.h" +#include "endians.h" +#include "support.h" + +/* The NTFS oem_id */ +#define magicNTFS const_cpu_to_le64(0x202020205346544e) /* "NTFS " */ +#define NTFS_SB_MAGIC 0x5346544e /* 'NTFS' */ + +/* + * Location of bootsector on partition: + * The standard NTFS_BOOT_SECTOR is on sector 0 of the partition. + * On NT4 and above there is one backup copy of the boot sector to + * be found on the last sector of the partition (not normally accessible + * from within Windows as the bootsector contained number of sectors + * value is one less than the actual value!). + * On versions of NT 3.51 and earlier, the backup copy was located at + * number of sectors/2 (integer divide), i.e. in the middle of the volume. + */ + +/** + * struct BIOS_PARAMETER_BLOCK - BIOS parameter block (bpb) structure. + */ +typedef struct { + le16 bytes_per_sector; /* Size of a sector in bytes. */ + u8 sectors_per_cluster; /* Size of a cluster in sectors. */ + le16 reserved_sectors; /* zero */ + u8 fats; /* zero */ + le16 root_entries; /* zero */ + le16 sectors; /* zero */ + u8 media_type; /* 0xf8 = hard disk */ + le16 sectors_per_fat; /* zero */ +/*0x0d*/le16 sectors_per_track; /* Required to boot Windows. */ +/*0x0f*/le16 heads; /* Required to boot Windows. */ +/*0x11*/le32 hidden_sectors; /* Offset to the start of the partition + relative to the disk in sectors. + Required to boot Windows. */ +/*0x15*/le32 large_sectors; /* zero */ +/* sizeof() = 25 (0x19) bytes */ +} __attribute__((__packed__)) BIOS_PARAMETER_BLOCK; + +/** + * struct NTFS_BOOT_SECTOR - NTFS boot sector structure. + */ +typedef struct { + u8 jump[3]; /* Irrelevant (jump to boot up code).*/ + le64 oem_id; /* Magic "NTFS ". */ +/*0x0b*/BIOS_PARAMETER_BLOCK bpb; /* See BIOS_PARAMETER_BLOCK. */ + u8 physical_drive; /* 0x00 floppy, 0x80 hard disk */ + u8 current_head; /* zero */ + u8 extended_boot_signature; /* 0x80 */ + u8 reserved2; /* zero */ +/*0x28*/sle64 number_of_sectors; /* Number of sectors in volume. Gives + maximum volume size of 2^63 sectors. + Assuming standard sector size of 512 + bytes, the maximum byte size is + approx. 4.7x10^21 bytes. (-; */ + sle64 mft_lcn; /* Cluster location of mft data. */ + sle64 mftmirr_lcn; /* Cluster location of copy of mft. */ + s8 clusters_per_mft_record; /* Mft record size in clusters. */ + u8 reserved0[3]; /* zero */ + s8 clusters_per_index_record; /* Index block size in clusters. */ + u8 reserved1[3]; /* zero */ + le64 volume_serial_number; /* Irrelevant (serial number). */ + le32 checksum; /* Boot sector checksum. */ +/*0x54*/u8 bootstrap[426]; /* Irrelevant (boot up code). */ + le16 end_of_sector_marker; /* End of bootsector magic. Always is + 0xaa55 in little endian. */ +/* sizeof() = 512 (0x200) bytes */ +} __attribute__((__packed__)) NTFS_BOOT_SECTOR; + +/** + * enum NTFS_RECORD_TYPES - + * + * Magic identifiers present at the beginning of all ntfs record containing + * records (like mft records for example). + */ +typedef enum { + /* Found in $MFT/$DATA. */ + magic_FILE = const_cpu_to_le32(0x454c4946), /* Mft entry. */ + magic_INDX = const_cpu_to_le32(0x58444e49), /* Index buffer. */ + magic_HOLE = const_cpu_to_le32(0x454c4f48), /* ? (NTFS 3.0+?) */ + + /* Found in $LogFile/$DATA. */ + magic_RSTR = const_cpu_to_le32(0x52545352), /* Restart page. */ + magic_RCRD = const_cpu_to_le32(0x44524352), /* Log record page. */ + + /* Found in $LogFile/$DATA. (May be found in $MFT/$DATA, also?) */ + magic_CHKD = const_cpu_to_le32(0x444b4843), /* Modified by chkdsk. */ + + /* Found in all ntfs record containing records. */ + magic_BAAD = const_cpu_to_le32(0x44414142), /* Failed multi sector + transfer was detected. */ + + /* + * Found in $LogFile/$DATA when a page is full or 0xff bytes and is + * thus not initialized. User has to initialize the page before using + * it. + */ + magic_empty = const_cpu_to_le32(0xffffffff),/* Record is empty and has + to be initialized before + it can be used. */ +} NTFS_RECORD_TYPES; + +/* + * Generic magic comparison macros. Finally found a use for the ## preprocessor + * operator! (-8 + */ +#define ntfs_is_magic(x, m) ( (u32)(x) == (u32)magic_##m ) +#define ntfs_is_magicp(p, m) ( *(u32*)(p) == (u32)magic_##m ) + +/* + * Specialised magic comparison macros for the NTFS_RECORD_TYPES defined above. + */ +#define ntfs_is_file_record(x) ( ntfs_is_magic (x, FILE) ) +#define ntfs_is_file_recordp(p) ( ntfs_is_magicp(p, FILE) ) +#define ntfs_is_mft_record(x) ( ntfs_is_file_record(x) ) +#define ntfs_is_mft_recordp(p) ( ntfs_is_file_recordp(p) ) +#define ntfs_is_indx_record(x) ( ntfs_is_magic (x, INDX) ) +#define ntfs_is_indx_recordp(p) ( ntfs_is_magicp(p, INDX) ) +#define ntfs_is_hole_record(x) ( ntfs_is_magic (x, HOLE) ) +#define ntfs_is_hole_recordp(p) ( ntfs_is_magicp(p, HOLE) ) + +#define ntfs_is_rstr_record(x) ( ntfs_is_magic (x, RSTR) ) +#define ntfs_is_rstr_recordp(p) ( ntfs_is_magicp(p, RSTR) ) +#define ntfs_is_rcrd_record(x) ( ntfs_is_magic (x, RCRD) ) +#define ntfs_is_rcrd_recordp(p) ( ntfs_is_magicp(p, RCRD) ) + +#define ntfs_is_chkd_record(x) ( ntfs_is_magic (x, CHKD) ) +#define ntfs_is_chkd_recordp(p) ( ntfs_is_magicp(p, CHKD) ) + +#define ntfs_is_baad_record(x) ( ntfs_is_magic (x, BAAD) ) +#define ntfs_is_baad_recordp(p) ( ntfs_is_magicp(p, BAAD) ) + +#define ntfs_is_empty_record(x) ( ntfs_is_magic (x, empty) ) +#define ntfs_is_empty_recordp(p) ( ntfs_is_magicp(p, empty) ) + + +/* + * The size of a logical sector in bytes, used as the sequence number stride for + * multi-sector transfers. This is intended to be less than or equal to the + * physical sector size, since if this were greater than the physical sector + * size, then incomplete multi-sector transfers may not be detected. + */ +#define NTFS_BLOCK_SIZE 512 +#define NTFS_BLOCK_SIZE_BITS 9 + +/** + * struct NTFS_RECORD - + * + * The Update Sequence Array (usa) is an array of the le16 values which belong + * to the end of each sector protected by the update sequence record in which + * this array is contained. Note that the first entry is the Update Sequence + * Number (usn), a cyclic counter of how many times the protected record has + * been written to disk. The values 0 and -1 (ie. 0xffff) are not used. All + * last le16's of each sector have to be equal to the usn (during reading) or + * are set to it (during writing). If they are not, an incomplete multi sector + * transfer has occurred when the data was written. + * The maximum size for the update sequence array is fixed to: + * maximum size = usa_ofs + (usa_count * 2) = 510 bytes + * The 510 bytes comes from the fact that the last le16 in the array has to + * (obviously) finish before the last le16 of the first 512-byte sector. + * This formula can be used as a consistency check in that usa_ofs + + * (usa_count * 2) has to be less than or equal to 510. + */ +typedef struct { + NTFS_RECORD_TYPES magic;/* A four-byte magic identifying the + record type and/or status. */ + le16 usa_ofs; /* Offset to the Update Sequence Array (usa) + from the start of the ntfs record. */ + le16 usa_count; /* Number of le16 sized entries in the usa + including the Update Sequence Number (usn), + thus the number of fixups is the usa_count + minus 1. */ +} __attribute__((__packed__)) NTFS_RECORD; + +/** + * enum NTFS_SYSTEM_FILES - System files mft record numbers. + * + * All these files are always marked as used in the bitmap attribute of the + * mft; presumably in order to avoid accidental allocation for random other + * mft records. Also, the sequence number for each of the system files is + * always equal to their mft record number and it is never modified. + */ +typedef enum { + FILE_MFT = 0, /* Master file table (mft). Data attribute + contains the entries and bitmap attribute + records which ones are in use (bit==1). */ + FILE_MFTMirr = 1, /* Mft mirror: copy of first four mft records + in data attribute. If cluster size > 4kiB, + copy of first N mft records, with + N = cluster_size / mft_record_size. */ + FILE_LogFile = 2, /* Journalling log in data attribute. */ + FILE_Volume = 3, /* Volume name attribute and volume information + attribute (flags and ntfs version). Windows + refers to this file as volume DASD (Direct + Access Storage Device). */ + FILE_AttrDef = 4, /* Array of attribute definitions in data + attribute. */ + FILE_root = 5, /* Root directory. */ + FILE_Bitmap = 6, /* Allocation bitmap of all clusters (lcns) in + data attribute. */ + FILE_Boot = 7, /* Boot sector (always at cluster 0) in data + attribute. */ + FILE_BadClus = 8, /* Contains all bad clusters in the non-resident + data attribute. */ + FILE_Secure = 9, /* Shared security descriptors in data attribute + and two indexes into the descriptors. + Appeared in Windows 2000. Before that, this + file was named $Quota but was unused. */ + FILE_UpCase = 10, /* Uppercase equivalents of all 65536 Unicode + characters in data attribute. */ + FILE_Extend = 11, /* Directory containing other system files (eg. + $ObjId, $Quota, $Reparse and $UsnJrnl). This + is new to NTFS3.0. */ + FILE_reserved12 = 12, /* Reserved for future use (records 12-15). */ + FILE_reserved13 = 13, + FILE_reserved14 = 14, + FILE_mft_data = 15, /* Reserved for first extent of $MFT:$DATA */ + FILE_first_user = 16, /* First user file, used as test limit for + whether to allow opening a file or not. */ +} NTFS_SYSTEM_FILES; + +/** + * enum MFT_RECORD_FLAGS - + * + * These are the so far known MFT_RECORD_* flags (16-bit) which contain + * information about the mft record in which they are present. + * + * MFT_RECORD_IS_4 exists on all $Extend sub-files. + * It seems that it marks it is a metadata file with MFT record >24, however, + * it is unknown if it is limited to metadata files only. + * + * MFT_RECORD_IS_VIEW_INDEX exists on every metafile with a non directory + * index, that means an INDEX_ROOT and an INDEX_ALLOCATION with a name other + * than "$I30". It is unknown if it is limited to metadata files only. + */ +NTFS_PACKED_ENUM_BEGIN(MFT_RECORD_FLAGS, u16) { + MFT_RECORD_IN_USE = const_cpu_to_le16(0x0001), + MFT_RECORD_IS_DIRECTORY = const_cpu_to_le16(0x0002), + MFT_RECORD_IS_4 = const_cpu_to_le16(0x0004), + MFT_RECORD_IS_VIEW_INDEX = const_cpu_to_le16(0x0008), + MFT_REC_SPACE_FILLER = 0xffff, /* Just to make flags + 16-bit. */ +} NTFS_PACKED_ENUM_END(MFT_RECORD_FLAGS, u16); + +/* + * mft references (aka file references or file record segment references) are + * used whenever a structure needs to refer to a record in the mft. + * + * A reference consists of a 48-bit index into the mft and a 16-bit sequence + * number used to detect stale references. + * + * For error reporting purposes we treat the 48-bit index as a signed quantity. + * + * The sequence number is a circular counter (skipping 0) describing how many + * times the referenced mft record has been (re)used. This has to match the + * sequence number of the mft record being referenced, otherwise the reference + * is considered stale and removed (FIXME: only ntfsck or the driver itself?). + * + * If the sequence number is zero it is assumed that no sequence number + * consistency checking should be performed. + * + * FIXME: Since inodes are 32-bit as of now, the driver needs to always check + * for high_part being 0 and if not either BUG(), cause a panic() or handle + * the situation in some other way. This shouldn't be a problem as a volume has + * to become HUGE in order to need more than 32-bits worth of mft records. + * Assuming the standard mft record size of 1kb only the records (never mind + * the non-resident attributes, etc.) would require 4Tb of space on their own + * for the first 32 bits worth of records. This is only if some strange person + * doesn't decide to foul play and make the mft sparse which would be a really + * horrible thing to do as it would trash our current driver implementation. )-: + * Do I hear screams "we want 64-bit inodes!" ?!? (-; + * + * FIXME: The mft zone is defined as the first 12% of the volume. This space is + * reserved so that the mft can grow contiguously and hence doesn't become + * fragmented. Volume free space includes the empty part of the mft zone and + * when the volume's free 88% are used up, the mft zone is shrunk by a factor + * of 2, thus making more space available for more files/data. This process is + * repeated every time there is no more free space except for the mft zone until + * there really is no more free space. + */ + +/* + * Typedef the MFT_REF as a 64-bit value for easier handling. + * Also define two unpacking macros to get to the reference (MREF) and + * sequence number (MSEQNO) respectively. + * The _LE versions are to be applied on little endian MFT_REFs. + * Note: The _LE versions will return a CPU endian formatted value! + */ +#define MFT_REF_MASK_CPU 0x0000ffffffffffffULL +#define MFT_REF_MASK_LE const_cpu_to_le64(MFT_REF_MASK_CPU) + +typedef u64 MFT_REF; +typedef le64 leMFT_REF; /* a little-endian MFT_MREF */ + +#define MK_MREF(m, s) ((MFT_REF)(((MFT_REF)(s) << 48) | \ + ((MFT_REF)(m) & MFT_REF_MASK_CPU))) +#define MK_LE_MREF(m, s) const_cpu_to_le64(((MFT_REF)(((MFT_REF)(s) << 48) | \ + ((MFT_REF)(m) & MFT_REF_MASK_CPU)))) + +#define MREF(x) ((u64)((x) & MFT_REF_MASK_CPU)) +#define MSEQNO(x) ((u16)(((x) >> 48) & 0xffff)) +#define MREF_LE(x) ((u64)(const_le64_to_cpu(x) & MFT_REF_MASK_CPU)) +#define MSEQNO_LE(x) ((u16)((const_le64_to_cpu(x) >> 48) & 0xffff)) + +#define IS_ERR_MREF(x) (((x) & 0x0000800000000000ULL) ? 1 : 0) +#define ERR_MREF(x) ((u64)((s64)(x))) +#define MREF_ERR(x) ((int)((s64)(x))) + +/** + * struct MFT_RECORD - An MFT record layout (NTFS 3.1+) + * + * The mft record header present at the beginning of every record in the mft. + * This is followed by a sequence of variable length attribute records which + * is terminated by an attribute of type AT_END which is a truncated attribute + * in that it only consists of the attribute type code AT_END and none of the + * other members of the attribute structure are present. + */ +typedef struct { +/*Ofs*/ +/* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ + NTFS_RECORD_TYPES magic;/* Usually the magic is "FILE". */ + le16 usa_ofs; /* See NTFS_RECORD definition above. */ + le16 usa_count; /* See NTFS_RECORD definition above. */ + +/* 8*/ leLSN lsn; /* $LogFile sequence number for this record. + Changed every time the record is modified. */ +/* 16*/ le16 sequence_number; /* Number of times this mft record has been + reused. (See description for MFT_REF + above.) NOTE: The increment (skipping zero) + is done when the file is deleted. NOTE: If + this is zero it is left zero. */ +/* 18*/ le16 link_count; /* Number of hard links, i.e. the number of + directory entries referencing this record. + NOTE: Only used in mft base records. + NOTE: When deleting a directory entry we + check the link_count and if it is 1 we + delete the file. Otherwise we delete the + FILE_NAME_ATTR being referenced by the + directory entry from the mft record and + decrement the link_count. + FIXME: Careful with Win32 + DOS names! */ +/* 20*/ le16 attrs_offset; /* Byte offset to the first attribute in this + mft record from the start of the mft record. + NOTE: Must be aligned to 8-byte boundary. */ +/* 22*/ MFT_RECORD_FLAGS flags; /* Bit array of MFT_RECORD_FLAGS. When a file + is deleted, the MFT_RECORD_IN_USE flag is + set to zero. */ +/* 24*/ le32 bytes_in_use; /* Number of bytes used in this mft record. + NOTE: Must be aligned to 8-byte boundary. */ +/* 28*/ le32 bytes_allocated; /* Number of bytes allocated for this mft + record. This should be equal to the mft + record size. */ +/* 32*/ leMFT_REF base_mft_record; + /* This is zero for base mft records. + When it is not zero it is a mft reference + pointing to the base mft record to which + this record belongs (this is then used to + locate the attribute list attribute present + in the base record which describes this + extension record and hence might need + modification when the extension record + itself is modified, also locating the + attribute list also means finding the other + potential extents, belonging to the non-base + mft record). */ +/* 40*/ le16 next_attr_instance; /* The instance number that will be + assigned to the next attribute added to this + mft record. NOTE: Incremented each time + after it is used. NOTE: Every time the mft + record is reused this number is set to zero. + NOTE: The first instance number is always 0. + */ +/* The below fields are specific to NTFS 3.1+ (Windows XP and above): */ +/* 42*/ le16 reserved; /* Reserved/alignment. */ +/* 44*/ le32 mft_record_number; /* Number of this mft record. */ +/* sizeof() = 48 bytes */ +/* + * When (re)using the mft record, we place the update sequence array at this + * offset, i.e. before we start with the attributes. This also makes sense, + * otherwise we could run into problems with the update sequence array + * containing in itself the last two bytes of a sector which would mean that + * multi sector transfer protection wouldn't work. As you can't protect data + * by overwriting it since you then can't get it back... + * When reading we obviously use the data from the ntfs record header. + */ +} __attribute__((__packed__)) MFT_RECORD; + +/** + * struct MFT_RECORD_OLD - An MFT record layout (NTFS <=3.0) + * + * This is the version without the NTFS 3.1+ specific fields. + */ +typedef struct { +/*Ofs*/ +/* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ + NTFS_RECORD_TYPES magic;/* Usually the magic is "FILE". */ + le16 usa_ofs; /* See NTFS_RECORD definition above. */ + le16 usa_count; /* See NTFS_RECORD definition above. */ + +/* 8*/ leLSN lsn; /* $LogFile sequence number for this record. + Changed every time the record is modified. */ +/* 16*/ le16 sequence_number; /* Number of times this mft record has been + reused. (See description for MFT_REF + above.) NOTE: The increment (skipping zero) + is done when the file is deleted. NOTE: If + this is zero it is left zero. */ +/* 18*/ le16 link_count; /* Number of hard links, i.e. the number of + directory entries referencing this record. + NOTE: Only used in mft base records. + NOTE: When deleting a directory entry we + check the link_count and if it is 1 we + delete the file. Otherwise we delete the + FILE_NAME_ATTR being referenced by the + directory entry from the mft record and + decrement the link_count. + FIXME: Careful with Win32 + DOS names! */ +/* 20*/ le16 attrs_offset; /* Byte offset to the first attribute in this + mft record from the start of the mft record. + NOTE: Must be aligned to 8-byte boundary. */ +/* 22*/ MFT_RECORD_FLAGS flags; /* Bit array of MFT_RECORD_FLAGS. When a file + is deleted, the MFT_RECORD_IN_USE flag is + set to zero. */ +/* 24*/ le32 bytes_in_use; /* Number of bytes used in this mft record. + NOTE: Must be aligned to 8-byte boundary. */ +/* 28*/ le32 bytes_allocated; /* Number of bytes allocated for this mft + record. This should be equal to the mft + record size. */ +/* 32*/ leMFT_REF base_mft_record; + /* This is zero for base mft records. + When it is not zero it is a mft reference + pointing to the base mft record to which + this record belongs (this is then used to + locate the attribute list attribute present + in the base record which describes this + extension record and hence might need + modification when the extension record + itself is modified, also locating the + attribute list also means finding the other + potential extents, belonging to the non-base + mft record). */ +/* 40*/ le16 next_attr_instance; /* The instance number that will be + assigned to the next attribute added to this + mft record. NOTE: Incremented each time + after it is used. NOTE: Every time the mft + record is reused this number is set to zero. + NOTE: The first instance number is always 0. + */ +/* sizeof() = 42 bytes */ +/* + * When (re)using the mft record, we place the update sequence array at this + * offset, i.e. before we start with the attributes. This also makes sense, + * otherwise we could run into problems with the update sequence array + * containing in itself the last two bytes of a sector which would mean that + * multi sector transfer protection wouldn't work. As you can't protect data + * by overwriting it since you then can't get it back... + * When reading we obviously use the data from the ntfs record header. + */ +} __attribute__((__packed__)) MFT_RECORD_OLD; + +/** + * enum ATTR_TYPES - System defined attributes (32-bit). + * + * Each attribute type has a corresponding attribute name (Unicode string of + * maximum 64 character length) as described by the attribute definitions + * present in the data attribute of the $AttrDef system file. + * + * On NTFS 3.0 volumes the names are just as the types are named in the below + * enum exchanging AT_ for the dollar sign ($). If that isn't a revealing + * choice of symbol... (-; + */ +typedef enum { + AT_UNUSED = const_cpu_to_le32( 0), + AT_STANDARD_INFORMATION = const_cpu_to_le32( 0x10), + AT_ATTRIBUTE_LIST = const_cpu_to_le32( 0x20), + AT_FILE_NAME = const_cpu_to_le32( 0x30), + AT_OBJECT_ID = const_cpu_to_le32( 0x40), + AT_SECURITY_DESCRIPTOR = const_cpu_to_le32( 0x50), + AT_VOLUME_NAME = const_cpu_to_le32( 0x60), + AT_VOLUME_INFORMATION = const_cpu_to_le32( 0x70), + AT_DATA = const_cpu_to_le32( 0x80), + AT_INDEX_ROOT = const_cpu_to_le32( 0x90), + AT_INDEX_ALLOCATION = const_cpu_to_le32( 0xa0), + AT_BITMAP = const_cpu_to_le32( 0xb0), + AT_REPARSE_POINT = const_cpu_to_le32( 0xc0), + AT_EA_INFORMATION = const_cpu_to_le32( 0xd0), + AT_EA = const_cpu_to_le32( 0xe0), + AT_PROPERTY_SET = const_cpu_to_le32( 0xf0), + AT_LOGGED_UTILITY_STREAM = const_cpu_to_le32( 0x100), + AT_FIRST_USER_DEFINED_ATTRIBUTE = const_cpu_to_le32( 0x1000), + AT_END = const_cpu_to_le32(0xffffffff), +} ATTR_TYPES; + +/** + * enum COLLATION_RULES - The collation rules for sorting views/indexes/etc + * (32-bit). + * + * COLLATION_BINARY - Collate by binary compare where the first byte is most + * significant. + * COLLATION_FILE_NAME - Collate Unicode strings by comparing their 16-bit + * coding units, primarily ignoring case using the volume's $UpCase table, + * but falling back to a case-sensitive comparison if the names are equal + * ignoring case. + * COLLATION_UNICODE_STRING - TODO: this is not yet implemented and still needs + * to be properly documented --- is it really the same as + * COLLATION_FILE_NAME? + * COLLATION_NTOFS_ULONG - Sorting is done according to ascending le32 key + * values. E.g. used for $SII index in FILE_Secure, which sorts by + * security_id (le32). + * COLLATION_NTOFS_SID - Sorting is done according to ascending SID values. + * E.g. used for $O index in FILE_Extend/$Quota. + * COLLATION_NTOFS_SECURITY_HASH - Sorting is done first by ascending hash + * values and second by ascending security_id values. E.g. used for $SDH + * index in FILE_Secure. + * COLLATION_NTOFS_ULONGS - Sorting is done according to a sequence of ascending + * le32 key values. E.g. used for $O index in FILE_Extend/$ObjId, which + * sorts by object_id (16-byte), by splitting up the object_id in four + * le32 values and using them as individual keys. E.g. take the following + * two security_ids, stored as follows on disk: + * 1st: a1 61 65 b7 65 7b d4 11 9e 3d 00 e0 81 10 42 59 + * 2nd: 38 14 37 d2 d2 f3 d4 11 a5 21 c8 6b 79 b1 97 45 + * To compare them, they are split into four le32 values each, like so: + * 1st: 0xb76561a1 0x11d47b65 0xe0003d9e 0x59421081 + * 2nd: 0xd2371438 0x11d4f3d2 0x6bc821a5 0x4597b179 + * Now, it is apparent why the 2nd object_id collates after the 1st: the + * first le32 value of the 1st object_id is less than the first le32 of + * the 2nd object_id. If the first le32 values of both object_ids were + * equal then the second le32 values would be compared, etc. + */ +typedef enum { + COLLATION_BINARY = const_cpu_to_le32(0), + COLLATION_FILE_NAME = const_cpu_to_le32(1), + COLLATION_UNICODE_STRING = const_cpu_to_le32(2), + COLLATION_NTOFS_ULONG = const_cpu_to_le32(16), + COLLATION_NTOFS_SID = const_cpu_to_le32(17), + COLLATION_NTOFS_SECURITY_HASH = const_cpu_to_le32(18), + COLLATION_NTOFS_ULONGS = const_cpu_to_le32(19), +} COLLATION_RULES; + +/** + * enum ATTR_DEF_FLAGS - + * + * The flags (32-bit) describing attribute properties in the attribute + * definition structure. FIXME: This information is based on Regis's + * information and, according to him, it is not certain and probably + * incomplete. The INDEXABLE flag is fairly certainly correct as only the file + * name attribute has this flag set and this is the only attribute indexed in + * NT4. + */ +typedef enum { + ATTR_DEF_INDEXABLE = const_cpu_to_le32(0x02), /* Attribute can be + indexed. */ + ATTR_DEF_MULTIPLE = const_cpu_to_le32(0x04), /* Attribute type + can be present multiple times in the + mft records of an inode. */ + ATTR_DEF_NOT_ZERO = const_cpu_to_le32(0x08), /* Attribute value + must contain at least one non-zero + byte. */ + ATTR_DEF_INDEXED_UNIQUE = const_cpu_to_le32(0x10), /* Attribute must be + indexed and the attribute value must be + unique for the attribute type in all of + the mft records of an inode. */ + ATTR_DEF_NAMED_UNIQUE = const_cpu_to_le32(0x20), /* Attribute must be + named and the name must be unique for + the attribute type in all of the mft + records of an inode. */ + ATTR_DEF_RESIDENT = const_cpu_to_le32(0x40), /* Attribute must be + resident. */ + ATTR_DEF_ALWAYS_LOG = const_cpu_to_le32(0x80), /* Always log + modifications to this attribute, + regardless of whether it is resident or + non-resident. Without this, only log + modifications if the attribute is + resident. */ +} ATTR_DEF_FLAGS; + +/** + * struct ATTR_DEF - + * + * The data attribute of FILE_AttrDef contains a sequence of attribute + * definitions for the NTFS volume. With this, it is supposed to be safe for an + * older NTFS driver to mount a volume containing a newer NTFS version without + * damaging it (that's the theory. In practice it's: not damaging it too much). + * Entries are sorted by attribute type. The flags describe whether the + * attribute can be resident/non-resident and possibly other things, but the + * actual bits are unknown. + */ +typedef struct { +/*hex ofs*/ +/* 0*/ ntfschar name[0x40]; /* Unicode name of the attribute. Zero + terminated. */ +/* 80*/ ATTR_TYPES type; /* Type of the attribute. */ +/* 84*/ le32 display_rule; /* Default display rule. + FIXME: What does it mean? (AIA) */ +/* 88*/ COLLATION_RULES collation_rule; /* Default collation rule. */ +/* 8c*/ ATTR_DEF_FLAGS flags; /* Flags describing the attribute. */ +/* 90*/ sle64 min_size; /* Optional minimum attribute size. */ +/* 98*/ sle64 max_size; /* Maximum size of attribute. */ +/* sizeof() = 0xa0 or 160 bytes */ +} __attribute__((__packed__)) ATTR_DEF; + +/** + * enum ATTR_FLAGS - Attribute flags (16-bit). + */ +NTFS_PACKED_ENUM_BEGIN(ATTR_FLAGS, u16) { + ATTR_IS_COMPRESSED = const_cpu_to_le16(0x0001), + ATTR_COMPRESSION_MASK = const_cpu_to_le16(0x00ff), /* Compression + method mask. Also, first + illegal value. */ + ATTR_IS_ENCRYPTED = const_cpu_to_le16(0x4000), + ATTR_IS_SPARSE = const_cpu_to_le16(0x8000), +} NTFS_PACKED_ENUM_END(ATTR_FLAGS, u16); + +/* + * Attribute compression. + * + * Only the data attribute is ever compressed in the current ntfs driver in + * Windows. Further, compression is only applied when the data attribute is + * non-resident. Finally, to use compression, the maximum allowed cluster size + * on a volume is 4kib. + * + * The compression method is based on independently compressing blocks of X + * clusters, where X is determined from the compression_unit value found in the + * non-resident attribute record header (more precisely: X = 2^compression_unit + * clusters). On Windows NT/2k, X always is 16 clusters (compression_unit = 4). + * + * There are three different cases of how a compression block of X clusters + * can be stored: + * + * 1) The data in the block is all zero (a sparse block): + * This is stored as a sparse block in the runlist, i.e. the runlist + * entry has length = X and lcn = -1. The mapping pairs array actually + * uses a delta_lcn value length of 0, i.e. delta_lcn is not present at + * all, which is then interpreted by the driver as lcn = -1. + * NOTE: Even uncompressed files can be sparse on NTFS 3.0 volumes, then + * the same principles apply as above, except that the length is not + * restricted to being any particular value. + * + * 2) The data in the block is not compressed: + * This happens when compression doesn't reduce the size of the block + * in clusters. I.e. if compression has a small effect so that the + * compressed data still occupies X clusters, then the uncompressed data + * is stored in the block. + * This case is recognised by the fact that the runlist entry has + * length = X and lcn >= 0. The mapping pairs array stores this as + * normal with a run length of X and some specific delta_lcn, i.e. + * delta_lcn has to be present. + * + * 3) The data in the block is compressed: + * The common case. This case is recognised by the fact that the run + * list entry has length L < X and lcn >= 0. The mapping pairs array + * stores this as normal with a run length of X and some specific + * delta_lcn, i.e. delta_lcn has to be present. This runlist entry is + * immediately followed by a sparse entry with length = X - L and + * lcn = -1. The latter entry is to make up the vcn counting to the + * full compression block size X. + * + * In fact, life is more complicated because adjacent entries of the same type + * can be coalesced. This means that one has to keep track of the number of + * clusters handled and work on a basis of X clusters at a time being one + * block. An example: if length L > X this means that this particular runlist + * entry contains a block of length X and part of one or more blocks of length + * L - X. Another example: if length L < X, this does not necessarily mean that + * the block is compressed as it might be that the lcn changes inside the block + * and hence the following runlist entry describes the continuation of the + * potentially compressed block. The block would be compressed if the + * following runlist entry describes at least X - L sparse clusters, thus + * making up the compression block length as described in point 3 above. (Of + * course, there can be several runlist entries with small lengths so that the + * sparse entry does not follow the first data containing entry with + * length < X.) + * + * NOTE: At the end of the compressed attribute value, there most likely is not + * just the right amount of data to make up a compression block, thus this data + * is not even attempted to be compressed. It is just stored as is, unless + * the number of clusters it occupies is reduced when compressed in which case + * it is stored as a compressed compression block, complete with sparse + * clusters at the end. + */ + +/** + * enum RESIDENT_ATTR_FLAGS - Flags of resident attributes (8-bit). + */ +NTFS_PACKED_ENUM_BEGIN(RESIDENT_ATTR_FLAGS, u8) { + RESIDENT_ATTR_IS_INDEXED = 0x01, /* Attribute is referenced in an index + (has implications for deleting and + modifying the attribute). */ +} NTFS_PACKED_ENUM_END(RESIDENT_ATTR_FLAGS, u8); + +/** + * struct ATTR_RECORD - Attribute record header. + * + * Always aligned to 8-byte boundary. + */ +typedef struct { +/*Ofs*/ +/* 0*/ ATTR_TYPES type; /* The (32-bit) type of the attribute. */ +/* 4*/ le32 length; /* Byte size of the resident part of the + attribute (aligned to 8-byte boundary). + Used to get to the next attribute. */ +/* 8*/ u8 non_resident; /* If 0, attribute is resident. + If 1, attribute is non-resident. */ +/* 9*/ u8 name_length; /* Unicode character size of name of attribute. + 0 if unnamed. */ +/* 10*/ le16 name_offset; /* If name_length != 0, the byte offset to the + beginning of the name from the attribute + record. Note that the name is stored as a + Unicode string. When creating, place offset + just at the end of the record header. Then, + follow with attribute value or mapping pairs + array, resident and non-resident attributes + respectively, aligning to an 8-byte + boundary. */ +/* 12*/ ATTR_FLAGS flags; /* Flags describing the attribute. */ +/* 14*/ le16 instance; /* The instance of this attribute record. This + number is unique within this mft record (see + MFT_RECORD/next_attribute_instance notes + above for more details). */ +/* 16*/ union { + /* Resident attributes. */ + struct { +/* 16 */ le32 value_length; /* Byte size of attribute value. */ +/* 20 */ le16 value_offset; /* Byte offset of the attribute + value from the start of the + attribute record. When creating, + align to 8-byte boundary if we + have a name present as this might + not have a length of a multiple + of 8-bytes. */ +/* 22 */ RESIDENT_ATTR_FLAGS resident_flags; /* See above. */ +/* 23 */ s8 reservedR; /* Reserved/alignment to 8-byte + boundary. */ +/* 24 */ void *resident_end[1]; /* Use offsetof(ATTR_RECORD, + resident_end) to get size of + a resident attribute. */ + } __attribute__((__packed__)); + /* Non-resident attributes. */ + struct { +/* 16*/ leVCN lowest_vcn; /* Lowest valid virtual cluster number + for this portion of the attribute value or + 0 if this is the only extent (usually the + case). - Only when an attribute list is used + does lowest_vcn != 0 ever occur. */ +/* 24*/ leVCN highest_vcn; /* Highest valid vcn of this extent of + the attribute value. - Usually there is only one + portion, so this usually equals the attribute + value size in clusters minus 1. Can be -1 for + zero length files. Can be 0 for "single extent" + attributes. */ +/* 32*/ le16 mapping_pairs_offset; /* Byte offset from the + beginning of the structure to the mapping pairs + array which contains the mappings between the + vcns and the logical cluster numbers (lcns). + When creating, place this at the end of this + record header aligned to 8-byte boundary. */ +/* 34*/ u8 compression_unit; /* The compression unit expressed + as the log to the base 2 of the number of + clusters in a compression unit. 0 means not + compressed. (This effectively limits the + compression unit size to be a power of two + clusters.) WinNT4 only uses a value of 4. */ +/* 35*/ u8 reserved1[5]; /* Align to 8-byte boundary. */ +/* The sizes below are only used when lowest_vcn is zero, as otherwise it would + be difficult to keep them up-to-date.*/ +/* 40*/ sle64 allocated_size; /* Byte size of disk space + allocated to hold the attribute value. Always + is a multiple of the cluster size. When a file + is compressed, this field is a multiple of the + compression block size (2^compression_unit) and + it represents the logically allocated space + rather than the actual on disk usage. For this + use the compressed_size (see below). */ +/* 48*/ sle64 data_size; /* Byte size of the attribute + value. Can be larger than allocated_size if + attribute value is compressed or sparse. */ +/* 56*/ sle64 initialized_size; /* Byte size of initialized + portion of the attribute value. Usually equals + data_size. */ +/* 64 */ union { + void *non_resident_end[1]; /* Marker for offsetof(). */ + sle64 compressed_size; + }; +/* sizeof(uncompressed attr) = 64*/ +/* 64*/ /* compressed_size is overlaid above */ /* Byte size of the attribute + value after compression. Only present when + compressed. Always is a multiple of the + cluster size. Represents the actual amount of + disk space being used on the disk. */ +/* 72 */ void *compressed_end[1]; + /* Use offsetof(ATTR_RECORD, compressed_end) to + get size of a compressed attribute. */ +/* sizeof(compressed attr) = 72*/ + } __attribute__((__packed__)); + } __attribute__((__packed__)); +} __attribute__((__packed__)) ATTR_RECORD; + +typedef ATTR_RECORD ATTR_REC; + +/** + * enum FILE_ATTR_FLAGS - File attribute flags (32-bit). + */ +NTFS_PACKED_ENUM_BEGIN(FILE_ATTR_FLAGS, u32) { + /* + * These flags are only present in the STANDARD_INFORMATION attribute + * (in the field file_attributes). + */ + FILE_ATTR_READONLY = const_cpu_to_le32(0x00000001), + FILE_ATTR_HIDDEN = const_cpu_to_le32(0x00000002), + FILE_ATTR_SYSTEM = const_cpu_to_le32(0x00000004), + /* Old DOS volid. Unused in NT. = const_cpu_to_le32(0x00000008), */ + + FILE_ATTR_DIRECTORY = const_cpu_to_le32(0x00000010), + /* FILE_ATTR_DIRECTORY is not considered valid in NT. It is reserved + for the DOS SUBDIRECTORY flag. */ + FILE_ATTR_ARCHIVE = const_cpu_to_le32(0x00000020), + FILE_ATTR_DEVICE = const_cpu_to_le32(0x00000040), + FILE_ATTR_NORMAL = const_cpu_to_le32(0x00000080), + + FILE_ATTR_TEMPORARY = const_cpu_to_le32(0x00000100), + FILE_ATTR_SPARSE_FILE = const_cpu_to_le32(0x00000200), + FILE_ATTR_REPARSE_POINT = const_cpu_to_le32(0x00000400), + FILE_ATTR_COMPRESSED = const_cpu_to_le32(0x00000800), + + FILE_ATTR_OFFLINE = const_cpu_to_le32(0x00001000), + FILE_ATTR_NOT_CONTENT_INDEXED = const_cpu_to_le32(0x00002000), + FILE_ATTR_ENCRYPTED = const_cpu_to_le32(0x00004000), + /* Supposed to mean no data locally, possibly repurposed */ + FILE_ATTRIBUTE_RECALL_ON_OPEN = const_cpu_to_le32(0x00040000), + + FILE_ATTR_VALID_FLAGS = const_cpu_to_le32(0x00047fb7), + /* FILE_ATTR_VALID_FLAGS masks out the old DOS VolId and the + FILE_ATTR_DEVICE and preserves everything else. This mask + is used to obtain all flags that are valid for reading. */ + FILE_ATTR_VALID_SET_FLAGS = const_cpu_to_le32(0x000031a7), + /* FILE_ATTR_VALID_SET_FLAGS masks out the old DOS VolId, the + FILE_ATTR_DEVICE, FILE_ATTR_DIRECTORY, FILE_ATTR_SPARSE_FILE, + FILE_ATTR_REPARSE_POINT, FILE_ATRE_COMPRESSED and FILE_ATTR_ENCRYPTED + and preserves the rest. This mask is used to to obtain all flags that + are valid for setting. */ + + /** + * FILE_ATTR_I30_INDEX_PRESENT - Is it a directory? + * + * This is a copy of the MFT_RECORD_IS_DIRECTORY bit from the mft + * record, telling us whether this is a directory or not, i.e. whether + * it has an index root attribute named "$I30" or not. + * + * This flag is only present in the FILE_NAME attribute (in the + * file_attributes field). + */ + FILE_ATTR_I30_INDEX_PRESENT = const_cpu_to_le32(0x10000000), + + /** + * FILE_ATTR_VIEW_INDEX_PRESENT - Does have a non-directory index? + * + * This is a copy of the MFT_RECORD_IS_VIEW_INDEX bit from the mft + * record, telling us whether this file has a view index present (eg. + * object id index, quota index, one of the security indexes and the + * reparse points index). + * + * This flag is only present in the $STANDARD_INFORMATION and + * $FILE_NAME attributes. + */ + FILE_ATTR_VIEW_INDEX_PRESENT = const_cpu_to_le32(0x20000000), +} NTFS_PACKED_ENUM_END(FILE_ATTR_FLAGS, u32); + +/* + * NOTE on times in NTFS: All times are in MS standard time format, i.e. they + * are the number of 100-nanosecond intervals since 1st January 1601, 00:00:00 + * universal coordinated time (UTC). (In Linux time starts 1st January 1970, + * 00:00:00 UTC and is stored as the number of 1-second intervals since then.) + */ + +/** + * struct STANDARD_INFORMATION - Attribute: Standard information (0x10). + * + * NOTE: Always resident. + * NOTE: Present in all base file records on a volume. + * NOTE: There is conflicting information about the meaning of each of the time + * fields but the meaning as defined below has been verified to be + * correct by practical experimentation on Windows NT4 SP6a and is hence + * assumed to be the one and only correct interpretation. + */ +typedef struct { +/*Ofs*/ +/* 0*/ sle64 creation_time; /* Time file was created. Updated when + a filename is changed(?). */ +/* 8*/ sle64 last_data_change_time; /* Time the data attribute was last + modified. */ +/* 16*/ sle64 last_mft_change_time; /* Time this mft record was last + modified. */ +/* 24*/ sle64 last_access_time; /* Approximate time when the file was + last accessed (obviously this is not + updated on read-only volumes). In + Windows this is only updated when + accessed if some time delta has + passed since the last update. Also, + last access times updates can be + disabled altogether for speed. */ +/* 32*/ FILE_ATTR_FLAGS file_attributes; /* Flags describing the file. */ +/* 36*/ union { + /* NTFS 1.2 (and previous, presumably) */ + struct { + /* 36 */ u8 reserved12[12]; /* Reserved/alignment to 8-byte + boundary. */ + /* 48 */ void *v1_end[1]; /* Marker for offsetof(). */ + } __attribute__((__packed__)); +/* sizeof() = 48 bytes */ + /* NTFS 3.0 */ + struct { +/* + * If a volume has been upgraded from a previous NTFS version, then these + * fields are present only if the file has been accessed since the upgrade. + * Recognize the difference by comparing the length of the resident attribute + * value. If it is 48, then the following fields are missing. If it is 72 then + * the fields are present. Maybe just check like this: + * if (resident.ValueLength < sizeof(STANDARD_INFORMATION)) { + * Assume NTFS 1.2- format. + * If (volume version is 3.0+) + * Upgrade attribute to NTFS 3.0 format. + * else + * Use NTFS 1.2- format for access. + * } else + * Use NTFS 3.0 format for access. + * Only problem is that it might be legal to set the length of the value to + * arbitrarily large values thus spoiling this check. - But chkdsk probably + * views that as a corruption, assuming that it behaves like this for all + * attributes. + */ + /* 36*/ le32 maximum_versions; /* Maximum allowed versions for + file. Zero if version numbering is disabled. */ + /* 40*/ le32 version_number; /* This file's version (if any). + Set to zero if maximum_versions is zero. */ + /* 44*/ le32 class_id; /* Class id from bidirectional + class id index (?). */ + /* 48*/ le32 owner_id; /* Owner_id of the user owning + the file. Translate via $Q index in FILE_Extend + /$Quota to the quota control entry for the user + owning the file. Zero if quotas are disabled. */ + /* 52*/ le32 security_id; /* Security_id for the file. + Translate via $SII index and $SDS data stream + in FILE_Secure to the security descriptor. */ + /* 56*/ le64 quota_charged; /* Byte size of the charge to + the quota for all streams of the file. Note: Is + zero if quotas are disabled. */ + /* 64*/ le64 usn; /* Last update sequence number + of the file. This is a direct index into the + change (aka usn) journal file. It is zero if + the usn journal is disabled. + NOTE: To disable the journal need to delete + the journal file itself and to then walk the + whole mft and set all Usn entries in all mft + records to zero! (This can take a while!) + The journal is FILE_Extend/$UsnJrnl. Win2k + will recreate the journal and initiate + logging if necessary when mounting the + partition. This, in contrast to disabling the + journal is a very fast process, so the user + won't even notice it. */ + /* 72*/ void *v3_end[1]; /* Marker for offsetof(). */ + } __attribute__((__packed__)); + } __attribute__((__packed__)); +/* sizeof() = 72 bytes (NTFS 3.0) */ +} __attribute__((__packed__)) STANDARD_INFORMATION; + +/** + * struct ATTR_LIST_ENTRY - Attribute: Attribute list (0x20). + * + * - Can be either resident or non-resident. + * - Value consists of a sequence of variable length, 8-byte aligned, + * ATTR_LIST_ENTRY records. + * - The attribute list attribute contains one entry for each attribute of + * the file in which the list is located, except for the list attribute + * itself. The list is sorted: first by attribute type, second by attribute + * name (if present), third by instance number. The extents of one + * non-resident attribute (if present) immediately follow after the initial + * extent. They are ordered by lowest_vcn and have their instance set to zero. + * It is not allowed to have two attributes with all sorting keys equal. + * - Further restrictions: + * - If not resident, the vcn to lcn mapping array has to fit inside the + * base mft record. + * - The attribute list attribute value has a maximum size of 256kb. This + * is imposed by the Windows cache manager. + * - Attribute lists are only used when the attributes of mft record do not + * fit inside the mft record despite all attributes (that can be made + * non-resident) having been made non-resident. This can happen e.g. when: + * - File has a large number of hard links (lots of file name + * attributes present). + * - The mapping pairs array of some non-resident attribute becomes so + * large due to fragmentation that it overflows the mft record. + * - The security descriptor is very complex (not applicable to + * NTFS 3.0 volumes). + * - There are many named streams. + */ +typedef struct { +/*Ofs*/ +/* 0*/ ATTR_TYPES type; /* Type of referenced attribute. */ +/* 4*/ le16 length; /* Byte size of this entry. */ +/* 6*/ u8 name_length; /* Size in Unicode chars of the name of the + attribute or 0 if unnamed. */ +/* 7*/ u8 name_offset; /* Byte offset to beginning of attribute name + (always set this to where the name would + start even if unnamed). */ +/* 8*/ leVCN lowest_vcn; /* Lowest virtual cluster number of this portion + of the attribute value. This is usually 0. It + is non-zero for the case where one attribute + does not fit into one mft record and thus + several mft records are allocated to hold + this attribute. In the latter case, each mft + record holds one extent of the attribute and + there is one attribute list entry for each + extent. NOTE: This is DEFINITELY a signed + value! The windows driver uses cmp, followed + by jg when comparing this, thus it treats it + as signed. */ +/* 16*/ leMFT_REF mft_reference;/* The reference of the mft record holding + the ATTR_RECORD for this portion of the + attribute value. */ +/* 24*/ le16 instance; /* If lowest_vcn = 0, the instance of the + attribute being referenced; otherwise 0. */ +/* 26*/ ntfschar name[1]; /* Use when creating only. When reading use + name_offset to determine the location of the + name. */ +/* sizeof() = 26 + (attribute_name_length * 2) bytes */ +} __attribute__((__packed__)) ATTR_LIST_ENTRY; + +/* + * The maximum allowed length for a file name. + */ +#define NTFS_MAX_NAME_LEN 255 + +/** + * enum FILE_NAME_TYPE_FLAGS - Possible namespaces for filenames in ntfs. + * (8-bit). + */ +NTFS_PACKED_ENUM_BEGIN(FILE_NAME_TYPE_FLAGS, u8) { + FILE_NAME_POSIX = 0x00, + /* This is the largest namespace. It is case sensitive and + allows all Unicode characters except for: '\0' and '/'. + Beware that in WinNT/2k files which eg have the same name + except for their case will not be distinguished by the + standard utilities and thus a "del filename" will delete + both "filename" and "fileName" without warning. */ + FILE_NAME_WIN32 = 0x01, + /* The standard WinNT/2k NTFS long filenames. Case insensitive. + All Unicode chars except: '\0', '"', '*', '/', ':', '<', + '>', '?', '\' and '|'. Trailing dots and spaces are allowed, + even though on Windows a filename with such a suffix can only + be created and accessed using a WinNT-style path, i.e. + \\?\-prefixed. (If a regular path is used, Windows will + strip the trailing dots and spaces, which makes such + filenames incompatible with most Windows software.) */ + FILE_NAME_DOS = 0x02, + /* The standard DOS filenames (8.3 format). Uppercase only. + All 8-bit characters greater space, except: '"', '*', '+', + ',', '/', ':', ';', '<', '=', '>', '?' and '\'. Trailing + dots and spaces are forbidden. */ + FILE_NAME_WIN32_AND_DOS = 0x03, + /* 3 means that both the Win32 and the DOS filenames are + identical and hence have been saved in this single filename + record. */ +} NTFS_PACKED_ENUM_END(FILE_NAME_TYPE_FLAGS, u8); + +/** + * struct FILE_NAME_ATTR - Attribute: Filename (0x30). + * + * NOTE: Always resident. + * NOTE: All fields, except the parent_directory, are only updated when the + * filename is changed. Until then, they just become out of sync with + * reality and the more up to date values are present in the standard + * information attribute. + * NOTE: There is conflicting information about the meaning of each of the time + * fields but the meaning as defined below has been verified to be + * correct by practical experimentation on Windows NT4 SP6a and is hence + * assumed to be the one and only correct interpretation. + */ +typedef struct { +/*hex ofs*/ +/* 0*/ leMFT_REF parent_directory; /* Directory this filename is + referenced from. */ +/* 8*/ sle64 creation_time; /* Time file was created. */ +/* 10*/ sle64 last_data_change_time; /* Time the data attribute was last + modified. */ +/* 18*/ sle64 last_mft_change_time; /* Time this mft record was last + modified. */ +/* 20*/ sle64 last_access_time; /* Last time this mft record was + accessed. */ +/* 28*/ sle64 allocated_size; /* Byte size of on-disk allocated space + for the data attribute. So for + normal $DATA, this is the + allocated_size from the unnamed + $DATA attribute and for compressed + and/or sparse $DATA, this is the + compressed_size from the unnamed + $DATA attribute. NOTE: This is a + multiple of the cluster size. */ +/* 30*/ sle64 data_size; /* Byte size of actual data in data + attribute. */ +/* 38*/ FILE_ATTR_FLAGS file_attributes; /* Flags describing the file. */ +/* 3c*/ union { + /* 3c*/ struct { + /* 3c*/ le16 packed_ea_size; /* Size of the buffer needed to + pack the extended attributes + (EAs), if such are present.*/ + /* 3e*/ le16 reserved; /* Reserved for alignment. */ + } __attribute__((__packed__)); + /* 3c*/ le32 reparse_point_tag; /* Type of reparse point, + present only in reparse + points and only if there are + no EAs. */ + } __attribute__((__packed__)); +/* 40*/ u8 file_name_length; /* Length of file name in + (Unicode) characters. */ +/* 41*/ FILE_NAME_TYPE_FLAGS file_name_type; /* Namespace of the file name.*/ +/* 42*/ ntfschar file_name[1]; /* File name in Unicode. */ +} __attribute__((__packed__)) FILE_NAME_ATTR; + +/** + * struct GUID - GUID structures store globally unique identifiers (GUID). + * + * A GUID is a 128-bit value consisting of one group of eight hexadecimal + * digits, followed by three groups of four hexadecimal digits each, followed + * by one group of twelve hexadecimal digits. GUIDs are Microsoft's + * implementation of the distributed computing environment (DCE) universally + * unique identifier (UUID). + * + * Example of a GUID: + * 1F010768-5A73-BC91-0010-A52216A7227B + */ +typedef struct { + le32 data1; /* The first eight hexadecimal digits of the GUID. */ + le16 data2; /* The first group of four hexadecimal digits. */ + le16 data3; /* The second group of four hexadecimal digits. */ + u8 data4[8]; /* The first two bytes are the third group of four + hexadecimal digits. The remaining six bytes are the + final 12 hexadecimal digits. */ +} __attribute__((__packed__)) GUID; + +/** + * struct OBJ_ID_INDEX_DATA - FILE_Extend/$ObjId contains an index named $O. + * + * This index contains all object_ids present on the volume as the index keys + * and the corresponding mft_record numbers as the index entry data parts. + * + * The data part (defined below) also contains three other object_ids: + * birth_volume_id - object_id of FILE_Volume on which the file was first + * created. Optional (i.e. can be zero). + * birth_object_id - object_id of file when it was first created. Usually + * equals the object_id. Optional (i.e. can be zero). + * domain_id - Reserved (always zero). + */ +typedef struct { + leMFT_REF mft_reference; /* Mft record containing the object_id + in the index entry key. */ + union { + struct { + GUID birth_volume_id; + GUID birth_object_id; + GUID domain_id; + } __attribute__((__packed__)); + u8 extended_info[48]; + } __attribute__((__packed__)); +} __attribute__((__packed__)) OBJ_ID_INDEX_DATA; + +/** + * struct OBJECT_ID_ATTR - Attribute: Object id (NTFS 3.0+) (0x40). + * + * NOTE: Always resident. + */ +typedef struct { + GUID object_id; /* Unique id assigned to the + file.*/ + /* The following fields are optional. The attribute value size is 16 + bytes, i.e. sizeof(GUID), if these are not present at all. Note, + the entries can be present but one or more (or all) can be zero + meaning that that particular value(s) is(are) not defined. Note, + when the fields are missing here, it is well possible that they are + to be found within the $Extend/$ObjId system file indexed under the + above object_id. */ + union { + struct { + GUID birth_volume_id; /* Unique id of volume on which + the file was first created.*/ + GUID birth_object_id; /* Unique id of file when it was + first created. */ + GUID domain_id; /* Reserved, zero. */ + } __attribute__((__packed__)); + u8 extended_info[48]; + } __attribute__((__packed__)); +} __attribute__((__packed__)) OBJECT_ID_ATTR; + +#if 0 +/** + * enum IDENTIFIER_AUTHORITIES - + * + * The pre-defined IDENTIFIER_AUTHORITIES used as SID_IDENTIFIER_AUTHORITY in + * the SID structure (see below). + */ +typedef enum { /* SID string prefix. */ + SECURITY_NULL_SID_AUTHORITY = {0, 0, 0, 0, 0, 0}, /* S-1-0 */ + SECURITY_WORLD_SID_AUTHORITY = {0, 0, 0, 0, 0, 1}, /* S-1-1 */ + SECURITY_LOCAL_SID_AUTHORITY = {0, 0, 0, 0, 0, 2}, /* S-1-2 */ + SECURITY_CREATOR_SID_AUTHORITY = {0, 0, 0, 0, 0, 3}, /* S-1-3 */ + SECURITY_NON_UNIQUE_AUTHORITY = {0, 0, 0, 0, 0, 4}, /* S-1-4 */ + SECURITY_NT_SID_AUTHORITY = {0, 0, 0, 0, 0, 5}, /* S-1-5 */ +} IDENTIFIER_AUTHORITIES; +#endif + +/** + * enum RELATIVE_IDENTIFIERS - + * + * These relative identifiers (RIDs) are used with the above identifier + * authorities to make up universal well-known SIDs. + * + * Note: The relative identifier (RID) refers to the portion of a SID, which + * identifies a user or group in relation to the authority that issued the SID. + * For example, the universal well-known SID Creator Owner ID (S-1-3-0) is + * made up of the identifier authority SECURITY_CREATOR_SID_AUTHORITY (3) and + * the relative identifier SECURITY_CREATOR_OWNER_RID (0). + */ +typedef enum { /* Identifier authority. */ + SECURITY_NULL_RID = 0, /* S-1-0 */ + SECURITY_WORLD_RID = 0, /* S-1-1 */ + SECURITY_LOCAL_RID = 0, /* S-1-2 */ + + SECURITY_CREATOR_OWNER_RID = 0, /* S-1-3 */ + SECURITY_CREATOR_GROUP_RID = 1, /* S-1-3 */ + + SECURITY_CREATOR_OWNER_SERVER_RID = 2, /* S-1-3 */ + SECURITY_CREATOR_GROUP_SERVER_RID = 3, /* S-1-3 */ + + SECURITY_DIALUP_RID = 1, + SECURITY_NETWORK_RID = 2, + SECURITY_BATCH_RID = 3, + SECURITY_INTERACTIVE_RID = 4, + SECURITY_SERVICE_RID = 6, + SECURITY_ANONYMOUS_LOGON_RID = 7, + SECURITY_PROXY_RID = 8, + SECURITY_ENTERPRISE_CONTROLLERS_RID=9, + SECURITY_SERVER_LOGON_RID = 9, + SECURITY_PRINCIPAL_SELF_RID = 0xa, + SECURITY_AUTHENTICATED_USER_RID = 0xb, + SECURITY_RESTRICTED_CODE_RID = 0xc, + SECURITY_TERMINAL_SERVER_RID = 0xd, + + SECURITY_LOGON_IDS_RID = 5, + SECURITY_LOGON_IDS_RID_COUNT = 3, + + SECURITY_LOCAL_SYSTEM_RID = 0x12, + + SECURITY_NT_NON_UNIQUE = 0x15, + + SECURITY_BUILTIN_DOMAIN_RID = 0x20, + + /* + * Well-known domain relative sub-authority values (RIDs). + */ + + /* Users. */ + DOMAIN_USER_RID_ADMIN = 0x1f4, + DOMAIN_USER_RID_GUEST = 0x1f5, + DOMAIN_USER_RID_KRBTGT = 0x1f6, + + /* Groups. */ + DOMAIN_GROUP_RID_ADMINS = 0x200, + DOMAIN_GROUP_RID_USERS = 0x201, + DOMAIN_GROUP_RID_GUESTS = 0x202, + DOMAIN_GROUP_RID_COMPUTERS = 0x203, + DOMAIN_GROUP_RID_CONTROLLERS = 0x204, + DOMAIN_GROUP_RID_CERT_ADMINS = 0x205, + DOMAIN_GROUP_RID_SCHEMA_ADMINS = 0x206, + DOMAIN_GROUP_RID_ENTERPRISE_ADMINS= 0x207, + DOMAIN_GROUP_RID_POLICY_ADMINS = 0x208, + + /* Aliases. */ + DOMAIN_ALIAS_RID_ADMINS = 0x220, + DOMAIN_ALIAS_RID_USERS = 0x221, + DOMAIN_ALIAS_RID_GUESTS = 0x222, + DOMAIN_ALIAS_RID_POWER_USERS = 0x223, + + DOMAIN_ALIAS_RID_ACCOUNT_OPS = 0x224, + DOMAIN_ALIAS_RID_SYSTEM_OPS = 0x225, + DOMAIN_ALIAS_RID_PRINT_OPS = 0x226, + DOMAIN_ALIAS_RID_BACKUP_OPS = 0x227, + + DOMAIN_ALIAS_RID_REPLICATOR = 0x228, + DOMAIN_ALIAS_RID_RAS_SERVERS = 0x229, + DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x22a, +} RELATIVE_IDENTIFIERS; + +/* + * The universal well-known SIDs: + * + * NULL_SID S-1-0-0 + * WORLD_SID S-1-1-0 + * LOCAL_SID S-1-2-0 + * CREATOR_OWNER_SID S-1-3-0 + * CREATOR_GROUP_SID S-1-3-1 + * CREATOR_OWNER_SERVER_SID S-1-3-2 + * CREATOR_GROUP_SERVER_SID S-1-3-3 + * + * (Non-unique IDs) S-1-4 + * + * NT well-known SIDs: + * + * NT_AUTHORITY_SID S-1-5 + * DIALUP_SID S-1-5-1 + * + * NETWORD_SID S-1-5-2 + * BATCH_SID S-1-5-3 + * INTERACTIVE_SID S-1-5-4 + * SERVICE_SID S-1-5-6 + * ANONYMOUS_LOGON_SID S-1-5-7 (aka null logon session) + * PROXY_SID S-1-5-8 + * SERVER_LOGON_SID S-1-5-9 (aka domain controller account) + * SELF_SID S-1-5-10 (self RID) + * AUTHENTICATED_USER_SID S-1-5-11 + * RESTRICTED_CODE_SID S-1-5-12 (running restricted code) + * TERMINAL_SERVER_SID S-1-5-13 (running on terminal server) + * + * (Logon IDs) S-1-5-5-X-Y + * + * (NT non-unique IDs) S-1-5-0x15-... + * + * (Built-in domain) S-1-5-0x20 + */ + +/** + * union SID_IDENTIFIER_AUTHORITY - A 48-bit value used in the SID structure + * + * NOTE: This is stored as a big endian number. + */ +typedef union { + struct { + be16 high_part; /* High 16-bits. */ + be32 low_part; /* Low 32-bits. */ + } __attribute__((__packed__)); + u8 value[6]; /* Value as individual bytes. */ +} __attribute__((__packed__)) SID_IDENTIFIER_AUTHORITY; + +/** + * struct SID - + * + * The SID structure is a variable-length structure used to uniquely identify + * users or groups. SID stands for security identifier. + * + * The standard textual representation of the SID is of the form: + * S-R-I-S-S... + * Where: + * - The first "S" is the literal character 'S' identifying the following + * digits as a SID. + * - R is the revision level of the SID expressed as a sequence of digits + * in decimal. + * - I is the 48-bit identifier_authority, expressed as digits in decimal, + * if I < 2^32, or hexadecimal prefixed by "0x", if I >= 2^32. + * - S... is one or more sub_authority values, expressed as digits in + * decimal. + * + * Example SID; the domain-relative SID of the local Administrators group on + * Windows NT/2k: + * S-1-5-32-544 + * This translates to a SID with: + * revision = 1, + * sub_authority_count = 2, + * identifier_authority = {0,0,0,0,0,5}, // SECURITY_NT_AUTHORITY + * sub_authority[1] = 32, // SECURITY_BUILTIN_DOMAIN_RID + * sub_authority[1] = 544 // DOMAIN_ALIAS_RID_ADMINS + */ +typedef struct { + u8 revision; + u8 sub_authority_count; + SID_IDENTIFIER_AUTHORITY identifier_authority; + le32 sub_authority[1]; /* At least one sub_authority. */ +} __attribute__((__packed__)) SID; + +/** + * enum SID_CONSTANTS - Current constants for SIDs. + */ +typedef enum { + SID_REVISION = 1, /* Current revision level. */ + SID_MAX_SUB_AUTHORITIES = 15, /* Maximum number of those. */ + SID_RECOMMENDED_SUB_AUTHORITIES = 1, /* Will change to around 6 in + a future revision. */ +} SID_CONSTANTS; + +/** + * enum ACE_TYPES - The predefined ACE types (8-bit, see below). + */ +NTFS_PACKED_ENUM_BEGIN(ACE_TYPES, u8) { + ACCESS_MIN_MS_ACE_TYPE = 0, + ACCESS_ALLOWED_ACE_TYPE = 0, + ACCESS_DENIED_ACE_TYPE = 1, + SYSTEM_AUDIT_ACE_TYPE = 2, + SYSTEM_ALARM_ACE_TYPE = 3, /* Not implemented as of Win2k. */ + ACCESS_MAX_MS_V2_ACE_TYPE = 3, + + ACCESS_ALLOWED_COMPOUND_ACE_TYPE= 4, + ACCESS_MAX_MS_V3_ACE_TYPE = 4, + + /* The following are Win2k only. */ + ACCESS_MIN_MS_OBJECT_ACE_TYPE = 5, + ACCESS_ALLOWED_OBJECT_ACE_TYPE = 5, + ACCESS_DENIED_OBJECT_ACE_TYPE = 6, + SYSTEM_AUDIT_OBJECT_ACE_TYPE = 7, + SYSTEM_ALARM_OBJECT_ACE_TYPE = 8, + ACCESS_MAX_MS_OBJECT_ACE_TYPE = 8, + + ACCESS_MAX_MS_V4_ACE_TYPE = 8, + + /* This one is for WinNT&2k. */ + ACCESS_MAX_MS_ACE_TYPE = 8, + + /* Windows XP and later */ + ACCESS_ALLOWED_CALLBACK_ACE_TYPE = 9, + ACCESS_DENIED_CALLBACK_ACE_TYPE = 10, + ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE = 11, + ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE = 12, + SYSTEM_AUDIT_CALLBACK_ACE_TYPE = 13, + SYSTEM_ALARM_CALLBACK_ACE_TYPE = 14, /* reserved */ + SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE = 15, + SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE = 16, /* reserved */ + + /* Windows Vista and later */ + SYSTEM_MANDATORY_LABEL_ACE_TYPE = 17, + + /* Windows 8 and later */ + SYSTEM_RESOURCE_ATTRIBUTE_ACE_TYPE = 18, + SYSTEM_SCOPED_POLICY_ID_ACE_TYPE = 19, + + /* Windows 10 and later */ + SYSTEM_PROCESS_TRUST_LABEL_ACE_TYPE = 20, +} NTFS_PACKED_ENUM_END(ACE_TYPES, u8); + +/** + * enum ACE_FLAGS - The ACE flags (8-bit) for audit and inheritance. + * + * SUCCESSFUL_ACCESS_ACE_FLAG is only used with system audit and alarm ACE + * types to indicate that a message is generated (in Windows!) for successful + * accesses. + * + * FAILED_ACCESS_ACE_FLAG is only used with system audit and alarm ACE types + * to indicate that a message is generated (in Windows!) for failed accesses. + */ +NTFS_PACKED_ENUM_BEGIN(ACE_FLAGS, u8) { + /* The inheritance flags. */ + OBJECT_INHERIT_ACE = 0x01, + CONTAINER_INHERIT_ACE = 0x02, + NO_PROPAGATE_INHERIT_ACE = 0x04, + INHERIT_ONLY_ACE = 0x08, + INHERITED_ACE = 0x10, /* Win2k only. */ + VALID_INHERIT_FLAGS = 0x1f, + + /* The audit flags. */ + SUCCESSFUL_ACCESS_ACE_FLAG = 0x40, + FAILED_ACCESS_ACE_FLAG = 0x80, +} NTFS_PACKED_ENUM_END(ACE_FLAGS, u8); + +/** + * struct ACE_HEADER - + * + * An ACE is an access-control entry in an access-control list (ACL). + * An ACE defines access to an object for a specific user or group or defines + * the types of access that generate system-administration messages or alarms + * for a specific user or group. The user or group is identified by a security + * identifier (SID). + * + * Each ACE starts with an ACE_HEADER structure (aligned on 4-byte boundary), + * which specifies the type and size of the ACE. The format of the subsequent + * data depends on the ACE type. + */ +typedef struct { + ACE_TYPES type; /* Type of the ACE. */ + ACE_FLAGS flags; /* Flags describing the ACE. */ + le16 size; /* Size in bytes of the ACE. */ +} __attribute__((__packed__)) ACE_HEADER; + +/** + * enum ACCESS_MASK - The access mask (32-bit). + * + * Defines the access rights. + */ +typedef enum { + /* + * The specific rights (bits 0 to 15). Depend on the type of the + * object being secured by the ACE. + */ + + /* Specific rights for files and directories are as follows: */ + + /* Right to read data from the file. (FILE) */ + FILE_READ_DATA = const_cpu_to_le32(0x00000001), + /* Right to list contents of a directory. (DIRECTORY) */ + FILE_LIST_DIRECTORY = const_cpu_to_le32(0x00000001), + + /* Right to write data to the file. (FILE) */ + FILE_WRITE_DATA = const_cpu_to_le32(0x00000002), + /* Right to create a file in the directory. (DIRECTORY) */ + FILE_ADD_FILE = const_cpu_to_le32(0x00000002), + + /* Right to append data to the file. (FILE) */ + FILE_APPEND_DATA = const_cpu_to_le32(0x00000004), + /* Right to create a subdirectory. (DIRECTORY) */ + FILE_ADD_SUBDIRECTORY = const_cpu_to_le32(0x00000004), + + /* Right to read extended attributes. (FILE/DIRECTORY) */ + FILE_READ_EA = const_cpu_to_le32(0x00000008), + + /* Right to write extended attributes. (FILE/DIRECTORY) */ + FILE_WRITE_EA = const_cpu_to_le32(0x00000010), + + /* Right to execute a file. (FILE) */ + FILE_EXECUTE = const_cpu_to_le32(0x00000020), + /* Right to traverse the directory. (DIRECTORY) */ + FILE_TRAVERSE = const_cpu_to_le32(0x00000020), + + /* + * Right to delete a directory and all the files it contains (its + * children), even if the files are read-only. (DIRECTORY) + */ + FILE_DELETE_CHILD = const_cpu_to_le32(0x00000040), + + /* Right to read file attributes. (FILE/DIRECTORY) */ + FILE_READ_ATTRIBUTES = const_cpu_to_le32(0x00000080), + + /* Right to change file attributes. (FILE/DIRECTORY) */ + FILE_WRITE_ATTRIBUTES = const_cpu_to_le32(0x00000100), + + /* + * The standard rights (bits 16 to 23). Are independent of the type of + * object being secured. + */ + + /* Right to delete the object. */ + DELETE = const_cpu_to_le32(0x00010000), + + /* + * Right to read the information in the object's security descriptor, + * not including the information in the SACL. I.e. right to read the + * security descriptor and owner. + */ + READ_CONTROL = const_cpu_to_le32(0x00020000), + + /* Right to modify the DACL in the object's security descriptor. */ + WRITE_DAC = const_cpu_to_le32(0x00040000), + + /* Right to change the owner in the object's security descriptor. */ + WRITE_OWNER = const_cpu_to_le32(0x00080000), + + /* + * Right to use the object for synchronization. Enables a process to + * wait until the object is in the signalled state. Some object types + * do not support this access right. + */ + SYNCHRONIZE = const_cpu_to_le32(0x00100000), + + /* + * The following STANDARD_RIGHTS_* are combinations of the above for + * convenience and are defined by the Win32 API. + */ + + /* These are currently defined to READ_CONTROL. */ + STANDARD_RIGHTS_READ = const_cpu_to_le32(0x00020000), + STANDARD_RIGHTS_WRITE = const_cpu_to_le32(0x00020000), + STANDARD_RIGHTS_EXECUTE = const_cpu_to_le32(0x00020000), + + /* Combines DELETE, READ_CONTROL, WRITE_DAC, and WRITE_OWNER access. */ + STANDARD_RIGHTS_REQUIRED = const_cpu_to_le32(0x000f0000), + + /* + * Combines DELETE, READ_CONTROL, WRITE_DAC, WRITE_OWNER, and + * SYNCHRONIZE access. + */ + STANDARD_RIGHTS_ALL = const_cpu_to_le32(0x001f0000), + + /* + * The access system ACL and maximum allowed access types (bits 24 to + * 25, bits 26 to 27 are reserved). + */ + ACCESS_SYSTEM_SECURITY = const_cpu_to_le32(0x01000000), + MAXIMUM_ALLOWED = const_cpu_to_le32(0x02000000), + + /* + * The generic rights (bits 28 to 31). These map onto the standard and + * specific rights. + */ + + /* Read, write, and execute access. */ + GENERIC_ALL = const_cpu_to_le32(0x10000000), + + /* Execute access. */ + GENERIC_EXECUTE = const_cpu_to_le32(0x20000000), + + /* + * Write access. For files, this maps onto: + * FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA | + * FILE_WRITE_EA | STANDARD_RIGHTS_WRITE | SYNCHRONIZE + * For directories, the mapping has the same numerical value. See + * above for the descriptions of the rights granted. + */ + GENERIC_WRITE = const_cpu_to_le32(0x40000000), + + /* + * Read access. For files, this maps onto: + * FILE_READ_ATTRIBUTES | FILE_READ_DATA | FILE_READ_EA | + * STANDARD_RIGHTS_READ | SYNCHRONIZE + * For directories, the mapping has the same numerical value. See + * above for the descriptions of the rights granted. + */ + GENERIC_READ = const_cpu_to_le32(0x80000000), +} ACCESS_MASK; + +/** + * struct GENERIC_MAPPING - + * + * The generic mapping array. Used to denote the mapping of each generic + * access right to a specific access mask. + * + * FIXME: What exactly is this and what is it for? (AIA) + */ +typedef struct { + ACCESS_MASK generic_read; + ACCESS_MASK generic_write; + ACCESS_MASK generic_execute; + ACCESS_MASK generic_all; +} __attribute__((__packed__)) GENERIC_MAPPING; + +/* + * The predefined ACE type structures are as defined below. + */ + +/** + * struct ACCESS_DENIED_ACE - + * + * ACCESS_ALLOWED_ACE, ACCESS_DENIED_ACE, SYSTEM_AUDIT_ACE, SYSTEM_ALARM_ACE + */ +typedef struct { +/* 0 ACE_HEADER; -- Unfolded here as gcc doesn't like unnamed structs. */ + ACE_TYPES type; /* Type of the ACE. */ + ACE_FLAGS flags; /* Flags describing the ACE. */ + le16 size; /* Size in bytes of the ACE. */ + +/* 4*/ ACCESS_MASK mask; /* Access mask associated with the ACE. */ +/* 8*/ SID sid; /* The SID associated with the ACE. */ +} __attribute__((__packed__)) ACCESS_ALLOWED_ACE, ACCESS_DENIED_ACE, + SYSTEM_AUDIT_ACE, SYSTEM_ALARM_ACE; + +/** + * enum OBJECT_ACE_FLAGS - The object ACE flags (32-bit). + */ +typedef enum { + ACE_OBJECT_TYPE_PRESENT = const_cpu_to_le32(1), + ACE_INHERITED_OBJECT_TYPE_PRESENT = const_cpu_to_le32(2), +} OBJECT_ACE_FLAGS; + +/** + * struct ACCESS_ALLOWED_OBJECT_ACE - + */ +typedef struct { +/* 0 ACE_HEADER; -- Unfolded here as gcc doesn't like unnamed structs. */ + ACE_TYPES type; /* Type of the ACE. */ + ACE_FLAGS flags; /* Flags describing the ACE. */ + le16 size; /* Size in bytes of the ACE. */ + +/* 4*/ ACCESS_MASK mask; /* Access mask associated with the ACE. */ +/* 8*/ OBJECT_ACE_FLAGS object_flags; /* Flags describing the object ACE. */ +/* 12*/ GUID object_type; +/* 28*/ GUID inherited_object_type; +/* 44*/ SID sid; /* The SID associated with the ACE. */ +} __attribute__((__packed__)) ACCESS_ALLOWED_OBJECT_ACE, + ACCESS_DENIED_OBJECT_ACE, + SYSTEM_AUDIT_OBJECT_ACE, + SYSTEM_ALARM_OBJECT_ACE; + +/** + * struct ACL - An ACL is an access-control list (ACL). + * + * An ACL starts with an ACL header structure, which specifies the size of + * the ACL and the number of ACEs it contains. The ACL header is followed by + * zero or more access control entries (ACEs). The ACL as well as each ACE + * are aligned on 4-byte boundaries. + */ +typedef struct { + u8 revision; /* Revision of this ACL. */ + u8 alignment1; + le16 size; /* Allocated space in bytes for ACL. Includes this + header, the ACEs and the remaining free space. */ + le16 ace_count; /* Number of ACEs in the ACL. */ + le16 alignment2; +/* sizeof() = 8 bytes */ +} __attribute__((__packed__)) ACL; + +/** + * enum ACL_CONSTANTS - Current constants for ACLs. + */ +typedef enum { + /* Current revision. */ + ACL_REVISION = 2, + ACL_REVISION_DS = 4, + + /* History of revisions. */ + ACL_REVISION1 = 1, + MIN_ACL_REVISION = 2, + ACL_REVISION2 = 2, + ACL_REVISION3 = 3, + ACL_REVISION4 = 4, + MAX_ACL_REVISION = 4, +} ACL_CONSTANTS; + +/** + * enum SECURITY_DESCRIPTOR_CONTROL - + * + * The security descriptor control flags (16-bit). + * + * SE_OWNER_DEFAULTED - This boolean flag, when set, indicates that the + * SID pointed to by the Owner field was provided by a + * defaulting mechanism rather than explicitly provided by the + * original provider of the security descriptor. This may + * affect the treatment of the SID with respect to inheritance + * of an owner. + * + * SE_GROUP_DEFAULTED - This boolean flag, when set, indicates that the + * SID in the Group field was provided by a defaulting mechanism + * rather than explicitly provided by the original provider of + * the security descriptor. This may affect the treatment of + * the SID with respect to inheritance of a primary group. + * + * SE_DACL_PRESENT - This boolean flag, when set, indicates that the + * security descriptor contains a discretionary ACL. If this + * flag is set and the Dacl field of the SECURITY_DESCRIPTOR is + * null, then a null ACL is explicitly being specified. + * + * SE_DACL_DEFAULTED - This boolean flag, when set, indicates that the + * ACL pointed to by the Dacl field was provided by a defaulting + * mechanism rather than explicitly provided by the original + * provider of the security descriptor. This may affect the + * treatment of the ACL with respect to inheritance of an ACL. + * This flag is ignored if the DaclPresent flag is not set. + * + * SE_SACL_PRESENT - This boolean flag, when set, indicates that the + * security descriptor contains a system ACL pointed to by the + * Sacl field. If this flag is set and the Sacl field of the + * SECURITY_DESCRIPTOR is null, then an empty (but present) + * ACL is being specified. + * + * SE_SACL_DEFAULTED - This boolean flag, when set, indicates that the + * ACL pointed to by the Sacl field was provided by a defaulting + * mechanism rather than explicitly provided by the original + * provider of the security descriptor. This may affect the + * treatment of the ACL with respect to inheritance of an ACL. + * This flag is ignored if the SaclPresent flag is not set. + * + * SE_SELF_RELATIVE - This boolean flag, when set, indicates that the + * security descriptor is in self-relative form. In this form, + * all fields of the security descriptor are contiguous in memory + * and all pointer fields are expressed as offsets from the + * beginning of the security descriptor. + */ +NTFS_PACKED_ENUM_BEGIN(SECURITY_DESCRIPTOR_CONTROL, u16) { + SE_OWNER_DEFAULTED = const_cpu_to_le16(0x0001), + SE_GROUP_DEFAULTED = const_cpu_to_le16(0x0002), + SE_DACL_PRESENT = const_cpu_to_le16(0x0004), + SE_DACL_DEFAULTED = const_cpu_to_le16(0x0008), + SE_SACL_PRESENT = const_cpu_to_le16(0x0010), + SE_SACL_DEFAULTED = const_cpu_to_le16(0x0020), + SE_DACL_AUTO_INHERIT_REQ = const_cpu_to_le16(0x0100), + SE_SACL_AUTO_INHERIT_REQ = const_cpu_to_le16(0x0200), + SE_DACL_AUTO_INHERITED = const_cpu_to_le16(0x0400), + SE_SACL_AUTO_INHERITED = const_cpu_to_le16(0x0800), + SE_DACL_PROTECTED = const_cpu_to_le16(0x1000), + SE_SACL_PROTECTED = const_cpu_to_le16(0x2000), + SE_RM_CONTROL_VALID = const_cpu_to_le16(0x4000), + SE_SELF_RELATIVE = const_cpu_to_le16(0x8000), +} NTFS_PACKED_ENUM_END(SECURITY_DESCRIPTOR_CONTROL, u16); + +/** + * struct SECURITY_DESCRIPTOR_RELATIVE - + * + * Self-relative security descriptor. Contains the owner and group SIDs as well + * as the sacl and dacl ACLs inside the security descriptor itself. + */ +typedef struct { + u8 revision; /* Revision level of the security descriptor. */ + u8 alignment; + SECURITY_DESCRIPTOR_CONTROL control; /* Flags qualifying the type of + the descriptor as well as the following fields. */ + le32 owner; /* Byte offset to a SID representing an object's + owner. If this is NULL, no owner SID is present in + the descriptor. */ + le32 group; /* Byte offset to a SID representing an object's + primary group. If this is NULL, no primary group + SID is present in the descriptor. */ + le32 sacl; /* Byte offset to a system ACL. Only valid, if + SE_SACL_PRESENT is set in the control field. If + SE_SACL_PRESENT is set but sacl is NULL, a NULL ACL + is specified. */ + le32 dacl; /* Byte offset to a discretionary ACL. Only valid, if + SE_DACL_PRESENT is set in the control field. If + SE_DACL_PRESENT is set but dacl is NULL, a NULL ACL + (unconditionally granting access) is specified. */ +/* sizeof() = 0x14 bytes */ +} __attribute__((__packed__)) SECURITY_DESCRIPTOR_RELATIVE; + +/** + * struct SECURITY_DESCRIPTOR - Absolute security descriptor. + * + * Does not contain the owner and group SIDs, nor the sacl and dacl ACLs inside + * the security descriptor. Instead, it contains pointers to these structures + * in memory. Obviously, absolute security descriptors are only useful for in + * memory representations of security descriptors. + * + * On disk, a self-relative security descriptor is used. + */ +typedef struct { + u8 revision; /* Revision level of the security descriptor. */ + u8 alignment; + SECURITY_DESCRIPTOR_CONTROL control; /* Flags qualifying the type of + the descriptor as well as the following fields. */ + SID *owner; /* Points to a SID representing an object's owner. If + this is NULL, no owner SID is present in the + descriptor. */ + SID *group; /* Points to a SID representing an object's primary + group. If this is NULL, no primary group SID is + present in the descriptor. */ + ACL *sacl; /* Points to a system ACL. Only valid, if + SE_SACL_PRESENT is set in the control field. If + SE_SACL_PRESENT is set but sacl is NULL, a NULL ACL + is specified. */ + ACL *dacl; /* Points to a discretionary ACL. Only valid, if + SE_DACL_PRESENT is set in the control field. If + SE_DACL_PRESENT is set but dacl is NULL, a NULL ACL + (unconditionally granting access) is specified. */ +} __attribute__((__packed__)) SECURITY_DESCRIPTOR; + +/** + * enum SECURITY_DESCRIPTOR_CONSTANTS - + * + * Current constants for security descriptors. + */ +typedef enum { + /* Current revision. */ + SECURITY_DESCRIPTOR_REVISION = 1, + SECURITY_DESCRIPTOR_REVISION1 = 1, + + /* The sizes of both the absolute and relative security descriptors is + the same as pointers, at least on ia32 architecture are 32-bit. */ + SECURITY_DESCRIPTOR_MIN_LENGTH = sizeof(SECURITY_DESCRIPTOR), +} SECURITY_DESCRIPTOR_CONSTANTS; + +/* + * Attribute: Security descriptor (0x50). + * + * A standard self-relative security descriptor. + * + * NOTE: Can be resident or non-resident. + * NOTE: Not used in NTFS 3.0+, as security descriptors are stored centrally + * in FILE_Secure and the correct descriptor is found using the security_id + * from the standard information attribute. + */ +typedef SECURITY_DESCRIPTOR_RELATIVE SECURITY_DESCRIPTOR_ATTR; + +/* + * On NTFS 3.0+, all security descriptors are stored in FILE_Secure. Only one + * referenced instance of each unique security descriptor is stored. + * + * FILE_Secure contains no unnamed data attribute, i.e. it has zero length. It + * does, however, contain two indexes ($SDH and $SII) as well as a named data + * stream ($SDS). + * + * Every unique security descriptor is assigned a unique security identifier + * (security_id, not to be confused with a SID). The security_id is unique for + * the NTFS volume and is used as an index into the $SII index, which maps + * security_ids to the security descriptor's storage location within the $SDS + * data attribute. The $SII index is sorted by ascending security_id. + * + * A simple hash is computed from each security descriptor. This hash is used + * as an index into the $SDH index, which maps security descriptor hashes to + * the security descriptor's storage location within the $SDS data attribute. + * The $SDH index is sorted by security descriptor hash and is stored in a B+ + * tree. When searching $SDH (with the intent of determining whether or not a + * new security descriptor is already present in the $SDS data stream), if a + * matching hash is found, but the security descriptors do not match, the + * search in the $SDH index is continued, searching for a next matching hash. + * + * When a precise match is found, the security_id corresponding to the security + * descriptor in the $SDS attribute is read from the found $SDH index entry and + * is stored in the $STANDARD_INFORMATION attribute of the file/directory to + * which the security descriptor is being applied. The $STANDARD_INFORMATION + * attribute is present in all base mft records (i.e. in all files and + * directories). + * + * If a match is not found, the security descriptor is assigned a new unique + * security_id and is added to the $SDS data attribute. Then, entries + * referencing the this security descriptor in the $SDS data attribute are + * added to the $SDH and $SII indexes. + * + * Note: Entries are never deleted from FILE_Secure, even if nothing + * references an entry any more. + */ + +/** + * struct SECURITY_DESCRIPTOR_HEADER - + * + * This header precedes each security descriptor in the $SDS data stream. + * This is also the index entry data part of both the $SII and $SDH indexes. + */ +typedef struct { + le32 hash; /* Hash of the security descriptor. */ + le32 security_id; /* The security_id assigned to the descriptor. */ + le64 offset; /* Byte offset of this entry in the $SDS stream. */ + le32 length; /* Size in bytes of this entry in $SDS stream. */ +} __attribute__((__packed__)) SECURITY_DESCRIPTOR_HEADER; + +/** + * struct SDH_INDEX_DATA - + */ +typedef struct { + le32 hash; /* Hash of the security descriptor. */ + le32 security_id; /* The security_id assigned to the descriptor. */ + le64 offset; /* Byte offset of this entry in the $SDS stream. */ + le32 length; /* Size in bytes of this entry in $SDS stream. */ + le32 reserved_II; /* Padding - always unicode "II" or zero. This field + isn't counted in INDEX_ENTRY's data_length. */ +} __attribute__((__packed__)) SDH_INDEX_DATA; + +/** + * struct SII_INDEX_DATA - + */ +typedef SECURITY_DESCRIPTOR_HEADER SII_INDEX_DATA; + +/** + * struct SDS_ENTRY - + * + * The $SDS data stream contains the security descriptors, aligned on 16-byte + * boundaries, sorted by security_id in a B+ tree. Security descriptors cannot + * cross 256kib boundaries (this restriction is imposed by the Windows cache + * manager). Each security descriptor is contained in a SDS_ENTRY structure. + * Also, each security descriptor is stored twice in the $SDS stream with a + * fixed offset of 0x40000 bytes (256kib, the Windows cache manager's max size) + * between them; i.e. if a SDS_ENTRY specifies an offset of 0x51d0, then the + * the first copy of the security descriptor will be at offset 0x51d0 in the + * $SDS data stream and the second copy will be at offset 0x451d0. + */ +typedef struct { +/* 0 SECURITY_DESCRIPTOR_HEADER; -- Unfolded here as gcc doesn't like + unnamed structs. */ + le32 hash; /* Hash of the security descriptor. */ + le32 security_id; /* The security_id assigned to the descriptor. */ + le64 offset; /* Byte offset of this entry in the $SDS stream. */ + le32 length; /* Size in bytes of this entry in $SDS stream. */ +/* 20*/ SECURITY_DESCRIPTOR_RELATIVE sid; /* The self-relative security + descriptor. */ +} __attribute__((__packed__)) SDS_ENTRY; + +/** + * struct SII_INDEX_KEY - The index entry key used in the $SII index. + * + * The collation type is COLLATION_NTOFS_ULONG. + */ +typedef struct { + le32 security_id; /* The security_id assigned to the descriptor. */ +} __attribute__((__packed__)) SII_INDEX_KEY; + +/** + * struct SDH_INDEX_KEY - The index entry key used in the $SDH index. + * + * The keys are sorted first by hash and then by security_id. + * The collation rule is COLLATION_NTOFS_SECURITY_HASH. + */ +typedef struct { + le32 hash; /* Hash of the security descriptor. */ + le32 security_id; /* The security_id assigned to the descriptor. */ +} __attribute__((__packed__)) SDH_INDEX_KEY; + +/** + * struct VOLUME_NAME - Attribute: Volume name (0x60). + * + * NOTE: Always resident. + * NOTE: Present only in FILE_Volume. + */ +typedef struct { + ntfschar name[1]; /* The name of the volume in Unicode. */ +} __attribute__((__packed__)) VOLUME_NAME; + +/** + * enum VOLUME_FLAGS - Possible flags for the volume (16-bit). + */ +NTFS_PACKED_ENUM_BEGIN(VOLUME_FLAGS, u16) { + VOLUME_IS_DIRTY = const_cpu_to_le16(0x0001), + VOLUME_RESIZE_LOG_FILE = const_cpu_to_le16(0x0002), + VOLUME_UPGRADE_ON_MOUNT = const_cpu_to_le16(0x0004), + VOLUME_MOUNTED_ON_NT4 = const_cpu_to_le16(0x0008), + VOLUME_DELETE_USN_UNDERWAY = const_cpu_to_le16(0x0010), + VOLUME_REPAIR_OBJECT_ID = const_cpu_to_le16(0x0020), + VOLUME_CHKDSK_UNDERWAY = const_cpu_to_le16(0x4000), + VOLUME_MODIFIED_BY_CHKDSK = const_cpu_to_le16(0x8000), + VOLUME_FLAGS_MASK = const_cpu_to_le16(0xc03f), +} NTFS_PACKED_ENUM_END(VOLUME_FLAGS, u16); + +/** + * struct VOLUME_INFORMATION - Attribute: Volume information (0x70). + * + * NOTE: Always resident. + * NOTE: Present only in FILE_Volume. + * NOTE: Windows 2000 uses NTFS 3.0 while Windows NT4 service pack 6a uses + * NTFS 1.2. I haven't personally seen other values yet. + */ +typedef struct { + le64 reserved; /* Not used (yet?). */ + u8 major_ver; /* Major version of the ntfs format. */ + u8 minor_ver; /* Minor version of the ntfs format. */ + VOLUME_FLAGS flags; /* Bit array of VOLUME_* flags. */ +} __attribute__((__packed__)) VOLUME_INFORMATION; + +/** + * struct DATA_ATTR - Attribute: Data attribute (0x80). + * + * NOTE: Can be resident or non-resident. + * + * Data contents of a file (i.e. the unnamed stream) or of a named stream. + */ +typedef struct { + u8 data[1]; /* The file's data contents. */ +} __attribute__((__packed__)) DATA_ATTR; + +/** + * enum INDEX_HEADER_FLAGS - Index header flags (8-bit). + */ +NTFS_PACKED_ENUM_BEGIN(INDEX_HEADER_FLAGS, u8) { + /* When index header is in an index root attribute: */ + SMALL_INDEX = 0, /* The index is small enough to fit inside the + index root attribute and there is no index + allocation attribute present. */ + LARGE_INDEX = 1, /* The index is too large to fit in the index + root attribute and/or an index allocation + attribute is present. */ + /* + * When index header is in an index block, i.e. is part of index + * allocation attribute: + */ + LEAF_NODE = 0, /* This is a leaf node, i.e. there are no more + nodes branching off it. */ + INDEX_NODE = 1, /* This node indexes other nodes, i.e. is not a + leaf node. */ + NODE_MASK = 1, /* Mask for accessing the *_NODE bits. */ +} NTFS_PACKED_ENUM_END(INDEX_HEADER_FLAGS, u8); + +/** + * struct INDEX_HEADER - + * + * This is the header for indexes, describing the INDEX_ENTRY records, which + * follow the INDEX_HEADER. Together the index header and the index entries + * make up a complete index. + * + * IMPORTANT NOTE: The offset, length and size structure members are counted + * relative to the start of the index header structure and not relative to the + * start of the index root or index allocation structures themselves. + */ +typedef struct { +/* 0*/ le32 entries_offset; /* Byte offset from the INDEX_HEADER to first + INDEX_ENTRY, aligned to 8-byte boundary. */ +/* 4*/ le32 index_length; /* Data size in byte of the INDEX_ENTRY's, + including the INDEX_HEADER, aligned to 8. */ +/* 8*/ le32 allocated_size; /* Allocated byte size of this index (block), + multiple of 8 bytes. See more below. */ + /* + For the index root attribute, the above two numbers are always + equal, as the attribute is resident and it is resized as needed. + + For the index allocation attribute, the attribute is not resident + and the allocated_size is equal to the index_block_size specified + by the corresponding INDEX_ROOT attribute minus the INDEX_BLOCK + size not counting the INDEX_HEADER part (i.e. minus -24). + */ +/* 12*/ INDEX_HEADER_FLAGS ih_flags; /* Bit field of INDEX_HEADER_FLAGS. */ +/* 13*/ u8 reserved[3]; /* Reserved/align to 8-byte boundary.*/ +/* sizeof() == 16 */ +} __attribute__((__packed__)) INDEX_HEADER; + +/** + * struct INDEX_ROOT - Attribute: Index root (0x90). + * + * NOTE: Always resident. + * + * This is followed by a sequence of index entries (INDEX_ENTRY structures) + * as described by the index header. + * + * When a directory is small enough to fit inside the index root then this + * is the only attribute describing the directory. When the directory is too + * large to fit in the index root, on the other hand, two additional attributes + * are present: an index allocation attribute, containing sub-nodes of the B+ + * directory tree (see below), and a bitmap attribute, describing which virtual + * cluster numbers (vcns) in the index allocation attribute are in use by an + * index block. + * + * NOTE: The root directory (FILE_root) contains an entry for itself. Other + * directories do not contain entries for themselves, though. + */ +typedef struct { +/* 0*/ ATTR_TYPES type; /* Type of the indexed attribute. Is + $FILE_NAME for directories, zero + for view indexes. No other values + allowed. */ +/* 4*/ COLLATION_RULES collation_rule; /* Collation rule used to sort the + index entries. If type is $FILE_NAME, + this must be COLLATION_FILE_NAME. */ +/* 8*/ le32 index_block_size; /* Size of index block in bytes (in + the index allocation attribute). */ +/* 12*/ s8 clusters_per_index_block; /* Size of index block in clusters (in + the index allocation attribute), when + an index block is >= than a cluster, + otherwise sectors per index block. */ +/* 13*/ u8 reserved[3]; /* Reserved/align to 8-byte boundary. */ +/* 16*/ INDEX_HEADER index; /* Index header describing the + following index entries. */ +/* sizeof()= 32 bytes */ +} __attribute__((__packed__)) INDEX_ROOT; + +/** + * struct INDEX_BLOCK - Attribute: Index allocation (0xa0). + * + * NOTE: Always non-resident (doesn't make sense to be resident anyway!). + * + * This is an array of index blocks. Each index block starts with an + * INDEX_BLOCK structure containing an index header, followed by a sequence of + * index entries (INDEX_ENTRY structures), as described by the INDEX_HEADER. + */ +typedef struct { +/* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ + NTFS_RECORD_TYPES magic;/* Magic is "INDX". */ + le16 usa_ofs; /* See NTFS_RECORD definition. */ + le16 usa_count; /* See NTFS_RECORD definition. */ + +/* 8*/ leLSN lsn; /* $LogFile sequence number of the last + modification of this index block. */ +/* 16*/ leVCN index_block_vcn; /* Virtual cluster number of the index block. */ +/* 24*/ INDEX_HEADER index; /* Describes the following index entries. */ +/* sizeof()= 40 (0x28) bytes */ +/* + * When creating the index block, we place the update sequence array at this + * offset, i.e. before we start with the index entries. This also makes sense, + * otherwise we could run into problems with the update sequence array + * containing in itself the last two bytes of a sector which would mean that + * multi sector transfer protection wouldn't work. As you can't protect data + * by overwriting it since you then can't get it back... + * When reading use the data from the ntfs record header. + */ +} __attribute__((__packed__)) INDEX_BLOCK; + +typedef INDEX_BLOCK INDEX_ALLOCATION; + +/** + * struct REPARSE_INDEX_KEY - + * + * The system file FILE_Extend/$Reparse contains an index named $R listing + * all reparse points on the volume. The index entry keys are as defined + * below. Note, that there is no index data associated with the index entries. + * + * The index entries are sorted by the index key file_id. The collation rule is + * COLLATION_NTOFS_ULONGS. FIXME: Verify whether the reparse_tag is not the + * primary key / is not a key at all. (AIA) + */ +typedef struct { + le32 reparse_tag; /* Reparse point type (inc. flags). */ + leMFT_REF file_id; /* Mft record of the file containing the + reparse point attribute. */ +} __attribute__((__packed__)) REPARSE_INDEX_KEY; + +/** + * enum QUOTA_FLAGS - Quota flags (32-bit). + */ +typedef enum { + /* The user quota flags. Names explain meaning. */ + QUOTA_FLAG_DEFAULT_LIMITS = const_cpu_to_le32(0x00000001), + QUOTA_FLAG_LIMIT_REACHED = const_cpu_to_le32(0x00000002), + QUOTA_FLAG_ID_DELETED = const_cpu_to_le32(0x00000004), + + QUOTA_FLAG_USER_MASK = const_cpu_to_le32(0x00000007), + /* Bit mask for user quota flags. */ + + /* These flags are only present in the quota defaults index entry, + i.e. in the entry where owner_id = QUOTA_DEFAULTS_ID. */ + QUOTA_FLAG_TRACKING_ENABLED = const_cpu_to_le32(0x00000010), + QUOTA_FLAG_ENFORCEMENT_ENABLED = const_cpu_to_le32(0x00000020), + QUOTA_FLAG_TRACKING_REQUESTED = const_cpu_to_le32(0x00000040), + QUOTA_FLAG_LOG_THRESHOLD = const_cpu_to_le32(0x00000080), + QUOTA_FLAG_LOG_LIMIT = const_cpu_to_le32(0x00000100), + QUOTA_FLAG_OUT_OF_DATE = const_cpu_to_le32(0x00000200), + QUOTA_FLAG_CORRUPT = const_cpu_to_le32(0x00000400), + QUOTA_FLAG_PENDING_DELETES = const_cpu_to_le32(0x00000800), +} QUOTA_FLAGS; + +/** + * struct QUOTA_CONTROL_ENTRY - + * + * The system file FILE_Extend/$Quota contains two indexes $O and $Q. Quotas + * are on a per volume and per user basis. + * + * The $Q index contains one entry for each existing user_id on the volume. The + * index key is the user_id of the user/group owning this quota control entry, + * i.e. the key is the owner_id. The user_id of the owner of a file, i.e. the + * owner_id, is found in the standard information attribute. The collation rule + * for $Q is COLLATION_NTOFS_ULONG. + * + * The $O index contains one entry for each user/group who has been assigned + * a quota on that volume. The index key holds the SID of the user_id the + * entry belongs to, i.e. the owner_id. The collation rule for $O is + * COLLATION_NTOFS_SID. + * + * The $O index entry data is the user_id of the user corresponding to the SID. + * This user_id is used as an index into $Q to find the quota control entry + * associated with the SID. + * + * The $Q index entry data is the quota control entry and is defined below. + */ +typedef struct { + le32 version; /* Currently equals 2. */ + QUOTA_FLAGS flags; /* Flags describing this quota entry. */ + le64 bytes_used; /* How many bytes of the quota are in use. */ + sle64 change_time; /* Last time this quota entry was changed. */ + sle64 threshold; /* Soft quota (-1 if not limited). */ + sle64 limit; /* Hard quota (-1 if not limited). */ + sle64 exceeded_time; /* How long the soft quota has been exceeded. */ +/* The below field is NOT present for the quota defaults entry. */ + SID sid; /* The SID of the user/object associated with + this quota entry. If this field is missing + then the INDEX_ENTRY is padded to a multiple + of 8 with zeros which are not counted in + the data_length field. If the sid is present + then this structure is padded with zeros to + a multiple of 8 and the padding is counted in + the INDEX_ENTRY's data_length. */ +} __attribute__((__packed__)) QUOTA_CONTROL_ENTRY; + +/** + * struct QUOTA_O_INDEX_DATA - + */ +typedef struct { + le32 owner_id; + le32 unknown; /* Always 32. Seems to be padding and it's not + counted in the INDEX_ENTRY's data_length. + This field shouldn't be really here. */ +} __attribute__((__packed__)) QUOTA_O_INDEX_DATA; + +/** + * enum PREDEFINED_OWNER_IDS - Predefined owner_id values (32-bit). + */ +typedef enum { + QUOTA_INVALID_ID = const_cpu_to_le32(0x00000000), + QUOTA_DEFAULTS_ID = const_cpu_to_le32(0x00000001), + QUOTA_FIRST_USER_ID = const_cpu_to_le32(0x00000100), +} PREDEFINED_OWNER_IDS; + +/** + * enum INDEX_ENTRY_FLAGS - Index entry flags (16-bit). + */ +NTFS_PACKED_ENUM_BEGIN(INDEX_ENTRY_FLAGS, u16) { + INDEX_ENTRY_NODE = const_cpu_to_le16(1), /* This entry contains a + sub-node, i.e. a reference to an index + block in form of a virtual cluster + number (see below). */ + INDEX_ENTRY_END = const_cpu_to_le16(2), /* This signifies the last + entry in an index block. The index + entry does not represent a file but it + can point to a sub-node. */ + INDEX_ENTRY_SPACE_FILLER = 0xffff, /* Just to force 16-bit width. */ +} NTFS_PACKED_ENUM_END(INDEX_ENTRY_FLAGS, u16); + +/** + * struct INDEX_ENTRY_HEADER - This the index entry header (see below). + * + * ========================================================== + * !!!!! SEE DESCRIPTION OF THE FIELDS AT INDEX_ENTRY !!!!! + * ========================================================== + */ +typedef struct { +/* 0*/ union { + leMFT_REF indexed_file; + struct { + le16 data_offset; + le16 data_length; + le32 reservedV; + } __attribute__((__packed__)); + } __attribute__((__packed__)); +/* 8*/ le16 length; +/* 10*/ le16 key_length; +/* 12*/ INDEX_ENTRY_FLAGS flags; +/* 14*/ le16 reserved; +/* sizeof() = 16 bytes */ +} __attribute__((__packed__)) INDEX_ENTRY_HEADER; + +/** + * struct INDEX_ENTRY - This is an index entry. + * + * A sequence of such entries follows each INDEX_HEADER structure. Together + * they make up a complete index. The index follows either an index root + * attribute or an index allocation attribute. + * + * NOTE: Before NTFS 3.0 only filename attributes were indexed. + */ +typedef struct { +/* 0 INDEX_ENTRY_HEADER; -- Unfolded here as gcc dislikes unnamed structs. */ + union { /* Only valid when INDEX_ENTRY_END is not set. */ + leMFT_REF indexed_file; /* The mft reference of the file + described by this index + entry. Used for directory + indexes. */ + struct { /* Used for views/indexes to find the entry's data. */ + le16 data_offset; /* Data byte offset from this + INDEX_ENTRY. Follows the + index key. */ + le16 data_length; /* Data length in bytes. */ + le32 reservedV; /* Reserved (zero). */ + } __attribute__((__packed__)); + } __attribute__((__packed__)); +/* 8*/ le16 length; /* Byte size of this index entry, multiple of + 8-bytes. Size includes INDEX_ENTRY_HEADER + and the optional subnode VCN. See below. */ +/* 10*/ le16 key_length; /* Byte size of the key value, which is in the + index entry. It follows field reserved. Not + multiple of 8-bytes. */ +/* 12*/ INDEX_ENTRY_FLAGS ie_flags; /* Bit field of INDEX_ENTRY_* flags. */ +/* 14*/ le16 reserved; /* Reserved/align to 8-byte boundary. */ +/* End of INDEX_ENTRY_HEADER */ +/* 16*/ union { /* The key of the indexed attribute. NOTE: Only present + if INDEX_ENTRY_END bit in flags is not set. NOTE: On + NTFS versions before 3.0 the only valid key is the + FILE_NAME_ATTR. On NTFS 3.0+ the following + additional index keys are defined: */ + FILE_NAME_ATTR file_name;/* $I30 index in directories. */ + SII_INDEX_KEY sii; /* $SII index in $Secure. */ + SDH_INDEX_KEY sdh; /* $SDH index in $Secure. */ + GUID object_id; /* $O index in FILE_Extend/$ObjId: The + object_id of the mft record found in + the data part of the index. */ + REPARSE_INDEX_KEY reparse; /* $R index in + FILE_Extend/$Reparse. */ + SID sid; /* $O index in FILE_Extend/$Quota: + SID of the owner of the user_id. */ + le32 owner_id; /* $Q index in FILE_Extend/$Quota: + user_id of the owner of the quota + control entry in the data part of + the index. */ + } __attribute__((__packed__)) key; + /* The (optional) index data is inserted here when creating. + leVCN vcn; If INDEX_ENTRY_NODE bit in ie_flags is set, the last + eight bytes of this index entry contain the virtual + cluster number of the index block that holds the + entries immediately preceding the current entry. + + If the key_length is zero, then the vcn immediately + follows the INDEX_ENTRY_HEADER. + + The address of the vcn of "ie" INDEX_ENTRY is given by + (char*)ie + le16_to_cpu(ie->length) - sizeof(VCN) + */ +} __attribute__((__packed__)) INDEX_ENTRY; + +/** + * struct BITMAP_ATTR - Attribute: Bitmap (0xb0). + * + * Contains an array of bits (aka a bitfield). + * + * When used in conjunction with the index allocation attribute, each bit + * corresponds to one index block within the index allocation attribute. Thus + * the number of bits in the bitmap * index block size / cluster size is the + * number of clusters in the index allocation attribute. + */ +typedef struct { + u8 bitmap[1]; /* Array of bits. */ +} __attribute__((__packed__)) BITMAP_ATTR; + +/** + * enum PREDEFINED_REPARSE_TAGS - + * + * The reparse point tag defines the type of the reparse point. It also + * includes several flags, which further describe the reparse point. + * + * The reparse point tag is an unsigned 32-bit value divided in three parts: + * + * 1. The least significant 16 bits (i.e. bits 0 to 15) specify the type of + * the reparse point. + * 2. The 12 bits after this (i.e. bits 16 to 27) are reserved for future use. + * 3. The most significant four bits are flags describing the reparse point. + * They are defined as follows: + * bit 28: Directory bit. If set, the directory is not a surrogate + * and can be used the usual way. + * bit 29: Name surrogate bit. If set, the filename is an alias for + * another object in the system. + * bit 30: High-latency bit. If set, accessing the first byte of data will + * be slow. (E.g. the data is stored on a tape drive.) + * bit 31: Microsoft bit. If set, the tag is owned by Microsoft. User + * defined tags have to use zero here. + * 4. Moreover, on Windows 10 : + * Some flags may be used in bits 12 to 15 to further describe the + * reparse point. + */ +typedef enum { + IO_REPARSE_TAG_DIRECTORY = const_cpu_to_le32(0x10000000), + IO_REPARSE_TAG_IS_ALIAS = const_cpu_to_le32(0x20000000), + IO_REPARSE_TAG_IS_HIGH_LATENCY = const_cpu_to_le32(0x40000000), + IO_REPARSE_TAG_IS_MICROSOFT = const_cpu_to_le32(0x80000000), + + IO_REPARSE_TAG_RESERVED_ZERO = const_cpu_to_le32(0x00000000), + IO_REPARSE_TAG_RESERVED_ONE = const_cpu_to_le32(0x00000001), + IO_REPARSE_TAG_RESERVED_RANGE = const_cpu_to_le32(0x00000001), + + IO_REPARSE_TAG_CSV = const_cpu_to_le32(0x80000009), + IO_REPARSE_TAG_DEDUP = const_cpu_to_le32(0x80000013), + IO_REPARSE_TAG_DFS = const_cpu_to_le32(0x8000000A), + IO_REPARSE_TAG_DFSR = const_cpu_to_le32(0x80000012), + IO_REPARSE_TAG_HSM = const_cpu_to_le32(0xC0000004), + IO_REPARSE_TAG_HSM2 = const_cpu_to_le32(0x80000006), + IO_REPARSE_TAG_MOUNT_POINT = const_cpu_to_le32(0xA0000003), + IO_REPARSE_TAG_NFS = const_cpu_to_le32(0x80000014), + IO_REPARSE_TAG_SIS = const_cpu_to_le32(0x80000007), + IO_REPARSE_TAG_SYMLINK = const_cpu_to_le32(0xA000000C), + IO_REPARSE_TAG_WIM = const_cpu_to_le32(0x80000008), + IO_REPARSE_TAG_DFM = const_cpu_to_le32(0x80000016), + IO_REPARSE_TAG_WOF = const_cpu_to_le32(0x80000017), + IO_REPARSE_TAG_WCI = const_cpu_to_le32(0x80000018), + IO_REPARSE_TAG_CLOUD = const_cpu_to_le32(0x9000001A), + IO_REPARSE_TAG_APPEXECLINK = const_cpu_to_le32(0x8000001B), + IO_REPARSE_TAG_GVFS = const_cpu_to_le32(0x9000001C), + IO_REPARSE_TAG_LX_SYMLINK = const_cpu_to_le32(0xA000001D), + IO_REPARSE_TAG_AF_UNIX = const_cpu_to_le32(0x80000023), + IO_REPARSE_TAG_LX_FIFO = const_cpu_to_le32(0x80000024), + IO_REPARSE_TAG_LX_CHR = const_cpu_to_le32(0x80000025), + IO_REPARSE_TAG_LX_BLK = const_cpu_to_le32(0x80000026), + + IO_REPARSE_TAG_VALID_VALUES = const_cpu_to_le32(0xf000ffff), + IO_REPARSE_PLUGIN_SELECT = const_cpu_to_le32(0xffff0fff), +} PREDEFINED_REPARSE_TAGS; + +/** + * struct REPARSE_POINT - Attribute: Reparse point (0xc0). + * + * NOTE: Can be resident or non-resident. + */ +typedef struct { + le32 reparse_tag; /* Reparse point type (inc. flags). */ + le16 reparse_data_length; /* Byte size of reparse data. */ + le16 reserved; /* Align to 8-byte boundary. */ + u8 reparse_data[1]; /* Meaning depends on reparse_tag. */ +} __attribute__((__packed__)) REPARSE_POINT; + +/** + * struct EA_INFORMATION - Attribute: Extended attribute information (0xd0). + * + * NOTE: Always resident. + */ +typedef struct { + le16 ea_length; /* Byte size of the packed extended + attributes. */ + le16 need_ea_count; /* The number of extended attributes which have + the NEED_EA bit set. */ + le32 ea_query_length; /* Byte size of the buffer required to query + the extended attributes when calling + ZwQueryEaFile() in Windows NT/2k. I.e. the + byte size of the unpacked extended + attributes. */ +} __attribute__((__packed__)) EA_INFORMATION; + +/** + * enum EA_FLAGS - Extended attribute flags (8-bit). + */ +NTFS_PACKED_ENUM_BEGIN(EA_FLAGS, u8) { + NEED_EA = 0x80, /* Indicate that the file to which the EA + belongs cannot be interpreted without + understanding the associated extended + attributes. */ +} NTFS_PACKED_ENUM_END(EA_FLAGS, u8); + +/** + * struct EA_ATTR - Attribute: Extended attribute (EA) (0xe0). + * + * Like the attribute list and the index buffer list, the EA attribute value is + * a sequence of EA_ATTR variable length records. + * + * FIXME: It appears weird that the EA name is not Unicode. Is it true? + * FIXME: It seems that name is always uppercased. Is it true? + */ +typedef struct { + le32 next_entry_offset; /* Offset to the next EA_ATTR. */ + EA_FLAGS flags; /* Flags describing the EA. */ + u8 name_length; /* Length of the name of the extended + attribute in bytes. */ + le16 value_length; /* Byte size of the EA's value. */ + u8 name[1]; /* Name of the EA. */ + u8 value[1]; /* The value of the EA. Immediately + follows the name. */ +} __attribute__((__packed__)) EA_ATTR; + +/** + * struct PROPERTY_SET - Attribute: Property set (0xf0). + * + * Intended to support Native Structure Storage (NSS) - a feature removed from + * NTFS 3.0 during beta testing. + */ +typedef struct { + u8 _ntlite_unused; +} __attribute__((__packed__)) PROPERTY_SET; + +/** + * struct LOGGED_UTILITY_STREAM - Attribute: Logged utility stream (0x100). + * + * NOTE: Can be resident or non-resident. + * + * Operations on this attribute are logged to the journal ($LogFile) like + * normal metadata changes. + * + * Used by the Encrypting File System (EFS). All encrypted files have this + * attribute with the name $EFS. See below for the relevant structures. + */ +typedef struct { + u8 _ntlite_unused; +} __attribute__((__packed__)) LOGGED_UTILITY_STREAM; + +/* + * $EFS Data Structure: + * + * The following information is about the data structures that are contained + * inside a logged utility stream (0x100) with a name of "$EFS". + * + * The stream starts with an instance of EFS_ATTR_HEADER. + * + * Next, at offsets offset_to_ddf_array and offset_to_drf_array (unless any of + * them is 0) there is a EFS_DF_ARRAY_HEADER immediately followed by a sequence + * of multiple data decryption/recovery fields. + * + * Each data decryption/recovery field starts with a EFS_DF_HEADER and the next + * one (if it exists) can be found by adding EFS_DF_HEADER->df_length bytes to + * the offset of the beginning of the current EFS_DF_HEADER. + * + * The data decryption/recovery field contains an EFS_DF_CERTIFICATE_HEADER, a + * SID, an optional GUID, an optional container name, a non-optional user name, + * and the encrypted FEK. + * + * Note all the below are best guesses so may have mistakes/inaccuracies. + * Corrections/clarifications/additions are always welcome! + * + * Ntfs.sys takes an EFS value length of <= 0x54 or > 0x40000 to BSOD, i.e. it + * is invalid. + */ + +/** + * struct EFS_ATTR_HEADER - "$EFS" header. + * + * The header of the Logged utility stream (0x100) attribute named "$EFS". + */ +typedef struct { +/* 0*/ le32 length; /* Length of EFS attribute in bytes. */ + le32 state; /* Always 0? */ + le32 version; /* Efs version. Always 2? */ + le32 crypto_api_version; /* Always 0? */ +/* 16*/ u8 unknown4[16]; /* MD5 hash of decrypted FEK? This field is + created with a call to UuidCreate() so is + unlikely to be an MD5 hash and is more + likely to be GUID of this encrytped file + or something like that. */ +/* 32*/ u8 unknown5[16]; /* MD5 hash of DDFs? */ +/* 48*/ u8 unknown6[16]; /* MD5 hash of DRFs? */ +/* 64*/ le32 offset_to_ddf_array;/* Offset in bytes to the array of data + decryption fields (DDF), see below. Zero if + no DDFs are present. */ + le32 offset_to_drf_array;/* Offset in bytes to the array of data + recovery fields (DRF), see below. Zero if + no DRFs are present. */ + le32 reserved; /* Reserved. */ +} __attribute__((__packed__)) EFS_ATTR_HEADER; + +/** + * struct EFS_DF_ARRAY_HEADER - + */ +typedef struct { + le32 df_count; /* Number of data decryption/recovery fields in + the array. */ +} __attribute__((__packed__)) EFS_DF_ARRAY_HEADER; + +/** + * struct EFS_DF_HEADER - + */ +typedef struct { +/* 0*/ le32 df_length; /* Length of this data decryption/recovery + field in bytes. */ + le32 cred_header_offset; /* Offset in bytes to the credential header. */ + le32 fek_size; /* Size in bytes of the encrypted file + encryption key (FEK). */ + le32 fek_offset; /* Offset in bytes to the FEK from the start of + the data decryption/recovery field. */ +/* 16*/ le32 unknown1; /* always 0? Might be just padding. */ +} __attribute__((__packed__)) EFS_DF_HEADER; + +/** + * struct EFS_DF_CREDENTIAL_HEADER - + */ +typedef struct { +/* 0*/ le32 cred_length; /* Length of this credential in bytes. */ + le32 sid_offset; /* Offset in bytes to the user's sid from start + of this structure. Zero if no sid is + present. */ +/* 8*/ le32 type; /* Type of this credential: + 1 = CryptoAPI container. + 2 = Unexpected type. + 3 = Certificate thumbprint. + other = Unknown type. */ + union { + /* CryptoAPI container. */ + struct { +/* 12*/ le32 container_name_offset; /* Offset in bytes to + the name of the container from start of this + structure (may not be zero). */ +/* 16*/ le32 provider_name_offset; /* Offset in bytes to + the name of the provider from start of this + structure (may not be zero). */ + le32 public_key_blob_offset; /* Offset in bytes to + the public key blob from start of this + structure. */ +/* 24*/ le32 public_key_blob_size; /* Size in bytes of + public key blob. */ + } __attribute__((__packed__)); + /* Certificate thumbprint. */ + struct { +/* 12*/ le32 cert_thumbprint_header_size; /* Size in + bytes of the header of the certificate + thumbprint. */ +/* 16*/ le32 cert_thumbprint_header_offset; /* Offset in + bytes to the header of the certificate + thumbprint from start of this structure. */ + le32 unknown1; /* Always 0? Might be padding... */ + le32 unknown2; /* Always 0? Might be padding... */ + } __attribute__((__packed__)); + } __attribute__((__packed__)); +} __attribute__((__packed__)) EFS_DF_CREDENTIAL_HEADER; + +typedef EFS_DF_CREDENTIAL_HEADER EFS_DF_CRED_HEADER; + +/** + * struct EFS_DF_CERTIFICATE_THUMBPRINT_HEADER - + */ +typedef struct { +/* 0*/ le32 thumbprint_offset; /* Offset in bytes to the thumbprint. */ + le32 thumbprint_size; /* Size of thumbprint in bytes. */ +/* 8*/ le32 container_name_offset; /* Offset in bytes to the name of the + container from start of this + structure or 0 if no name present. */ + le32 provider_name_offset; /* Offset in bytes to the name of the + cryptographic provider from start of + this structure or 0 if no name + present. */ +/* 16*/ le32 user_name_offset; /* Offset in bytes to the user name + from start of this structure or 0 if + no user name present. (This is also + known as lpDisplayInformation.) */ +} __attribute__((__packed__)) EFS_DF_CERTIFICATE_THUMBPRINT_HEADER; + +typedef EFS_DF_CERTIFICATE_THUMBPRINT_HEADER EFS_DF_CERT_THUMBPRINT_HEADER; + +#ifdef _MSC_VER +typedef le64 INTX_FILE_TYPES; +#define INTX_SYMBOLIC_LINK \ + ((INTX_FILE_TYPES)const_cpu_to_le64(0x014B4E4C78746E49ULL)) /* "IntxLNK\1" */ +#define INTX_CHARACTER_DEVICE \ + ((INTX_FILE_TYPES)const_cpu_to_le64(0x0052484378746E49ULL)) /* "IntxCHR\0" */ +#define INTX_BLOCK_DEVICE \ + ((INTX_FILE_TYPES)const_cpu_to_le64(0x004B4C4278746E49ULL)) /* "IntxBLK\0" */ +#else +typedef enum { + INTX_SYMBOLIC_LINK = + const_cpu_to_le64(0x014B4E4C78746E49ULL), /* "IntxLNK\1" */ + INTX_CHARACTER_DEVICE = + const_cpu_to_le64(0x0052484378746E49ULL), /* "IntxCHR\0" */ + INTX_BLOCK_DEVICE = + const_cpu_to_le64(0x004B4C4278746E49ULL), /* "IntxBLK\0" */ +} INTX_FILE_TYPES; +#endif + +typedef struct { + INTX_FILE_TYPES magic; /* Intx file magic. */ + union { + /* For character and block devices. */ + struct { + le64 major; /* Major device number. */ + le64 minor; /* Minor device number. */ + void *device_end[1]; /* Marker for offsetof(). */ + } __attribute__((__packed__)); + /* For symbolic links. */ + ntfschar target[1]; + } __attribute__((__packed__)); +} __attribute__((__packed__)) INTX_FILE; + +#endif /* defined _NTFS_LAYOUT_H */ + +#ifdef _MSC_VER +#pragma pack(pop) +#endif diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/lcnalloc.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/lcnalloc.h new file mode 100644 index 0000000..cbf4c5c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/lcnalloc.h @@ -0,0 +1,51 @@ +/* + * lcnalloc.h - Exports for cluster (de)allocation. Originated from the Linux-NTFS + * project. + * + * Copyright (c) 2002 Anton Altaparmakov + * Copyright (c) 2004 Yura Pakhuchiy + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_LCNALLOC_H +#define _NTFS_LCNALLOC_H + +#include "types.h" +#include "runlist.h" +#include "volume.h" + +/** + * enum NTFS_CLUSTER_ALLOCATION_ZONES - + */ +typedef enum { + FIRST_ZONE = 0, /* For sanity checking. */ + MFT_ZONE = 0, /* Allocate from $MFT zone. */ + DATA_ZONE = 1, /* Allocate from $DATA zone. */ + LAST_ZONE = 1, /* For sanity checking. */ +} NTFS_CLUSTER_ALLOCATION_ZONES; + +extern runlist *ntfs_cluster_alloc(ntfs_volume *vol, VCN start_vcn, s64 count, + LCN start_lcn, const NTFS_CLUSTER_ALLOCATION_ZONES zone); + +extern int ntfs_cluster_free_from_rl(ntfs_volume *vol, runlist *rl); +extern int ntfs_cluster_free_basic(ntfs_volume *vol, s64 lcn, s64 count); + +extern int ntfs_cluster_free(ntfs_volume *vol, ntfs_attr *na, VCN start_vcn, + s64 count); + +#endif /* defined _NTFS_LCNALLOC_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/logfile.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/logfile.h new file mode 100644 index 0000000..97c99c4 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/logfile.h @@ -0,0 +1,477 @@ +#ifdef _MSC_VER +#pragma pack(push, 1) +#endif +/* + * logfile.h - Exports for $LogFile handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2005 Anton Altaparmakov + * Copyright (c) 2016 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_LOGFILE_H +#define _NTFS_LOGFILE_H + +#include "types.h" +#include "endians.h" +#include "layout.h" + +/* + * Journal ($LogFile) organization: + * + * Two restart areas present in the first two pages (restart pages, one restart + * area in each page). When the volume is dismounted they should be identical, + * except for the update sequence array which usually has a different update + * sequence number. + * + * These are followed by log records organized in pages headed by a log record + * header going up to log file size. Not all pages contain log records when a + * volume is first formatted, but as the volume ages, all records will be used. + * When the log file fills up, the records at the beginning are purged (by + * modifying the oldest_lsn to a higher value presumably) and writing begins + * at the beginning of the file. Effectively, the log file is viewed as a + * circular entity. + * + * NOTE: Windows NT, 2000, and XP all use log file version 1.1 but they accept + * versions <= 1.x, including 0.-1. (Yes, that is a minus one in there!) We + * probably only want to support 1.1 as this seems to be the current version + * and we don't know how that differs from the older versions. The only + * exception is if the journal is clean as marked by the two restart pages + * then it doesn't matter whether we are on an earlier version. We can just + * reinitialize the logfile and start again with version 1.1. + */ + +/* Some $LogFile related constants. */ +#define MaxLogFileSize 0x100000000ULL +#define DefaultLogPageSize 4096 +#define MinLogRecordPages 48 + +/** + * struct RESTART_PAGE_HEADER - Log file restart page header. + * + * Begins the restart area. + */ +typedef struct { +/*Ofs*/ +/* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ +/* 0*/ NTFS_RECORD_TYPES magic;/* The magic is "RSTR". */ +/* 4*/ le16 usa_ofs; /* See NTFS_RECORD definition in layout.h. + When creating, set this to be immediately + after this header structure (without any + alignment). */ +/* 6*/ le16 usa_count; /* See NTFS_RECORD definition in layout.h. */ + +/* 8*/ leLSN chkdsk_lsn; /* The last log file sequence number found by + chkdsk. Only used when the magic is changed + to "CHKD". Otherwise this is zero. */ +/* 16*/ le32 system_page_size; /* Byte size of system pages when the log file + was created, has to be >= 512 and a power of + 2. Use this to calculate the required size + of the usa (usa_count) and add it to usa_ofs. + Then verify that the result is less than the + value of the restart_area_offset. */ +/* 20*/ le32 log_page_size; /* Byte size of log file pages, has to be >= + 512 and a power of 2. The default is 4096 + and is used when the system page size is + between 4096 and 8192. Otherwise this is + set to the system page size instead. */ +/* 24*/ le16 restart_area_offset;/* Byte offset from the start of this header to + the RESTART_AREA. Value has to be aligned + to 8-byte boundary. When creating, set this + to be after the usa. */ +/* 26*/ sle16 minor_ver; /* Log file minor version. Only check if major + version is 1. */ +/* 28*/ sle16 major_ver; /* Log file major version. We only support + version 1.1. */ +/* 30*/ le16 usn; +/* sizeof() = 32 (0x20) bytes */ +} __attribute__((__packed__)) RESTART_PAGE_HEADER; + +/* + * Constant for the log client indices meaning that there are no client records + * in this particular client array. Also inside the client records themselves, + * this means that there are no client records preceding or following this one. + */ +#define LOGFILE_NO_CLIENT_CPU 0xffff +#define LOGFILE_NO_CLIENT const_cpu_to_le16(LOGFILE_NO_CLIENT_CPU) + +/* + * These are the so far known RESTART_AREA_* flags (16-bit) which contain + * information about the log file in which they are present. + */ +enum { + RESTART_VOLUME_IS_CLEAN = const_cpu_to_le16(0x0002), + RESTART_SPACE_FILLER = 0xffff, /* gcc: Force enum bit width to 16. */ +} __attribute__((__packed__)); + +typedef le16 RESTART_AREA_FLAGS; + +/** + * struct RESTART_AREA - Log file restart area record. + * + * The offset of this record is found by adding the offset of the + * RESTART_PAGE_HEADER to the restart_area_offset value found in it. + * See notes at restart_area_offset above. + */ +typedef struct { +/*Ofs*/ +/* 0*/ leLSN current_lsn; /* The current, i.e. last LSN inside the log + when the restart area was last written. + This happens often but what is the interval? + Is it just fixed time or is it every time a + check point is written or something else? + On create set to 0. */ +/* 8*/ le16 log_clients; /* Number of log client records in the array of + log client records which follows this + restart area. Must be 1. */ +/* 10*/ le16 client_free_list; /* The index of the first free log client record + in the array of log client records. + LOGFILE_NO_CLIENT means that there are no + free log client records in the array. + If != LOGFILE_NO_CLIENT, check that + log_clients > client_free_list. On Win2k + and presumably earlier, on a clean volume + this is != LOGFILE_NO_CLIENT, and it should + be 0, i.e. the first (and only) client + record is free and thus the logfile is + closed and hence clean. A dirty volume + would have left the logfile open and hence + this would be LOGFILE_NO_CLIENT. On WinXP + and presumably later, the logfile is always + open, even on clean shutdown so this should + always be LOGFILE_NO_CLIENT. */ +/* 12*/ le16 client_in_use_list;/* The index of the first in-use log client + record in the array of log client records. + LOGFILE_NO_CLIENT means that there are no + in-use log client records in the array. If + != LOGFILE_NO_CLIENT check that log_clients + > client_in_use_list. On Win2k and + presumably earlier, on a clean volume this + is LOGFILE_NO_CLIENT, i.e. there are no + client records in use and thus the logfile + is closed and hence clean. A dirty volume + would have left the logfile open and hence + this would be != LOGFILE_NO_CLIENT, and it + should be 0, i.e. the first (and only) + client record is in use. On WinXP and + presumably later, the logfile is always + open, even on clean shutdown so this should + always be 0. */ +/* 14*/ RESTART_AREA_FLAGS flags;/* Flags modifying LFS behaviour. On Win2k + and presumably earlier this is always 0. On + WinXP and presumably later, if the logfile + was shutdown cleanly, the second bit, + RESTART_VOLUME_IS_CLEAN, is set. This bit + is cleared when the volume is mounted by + WinXP and set when the volume is dismounted, + thus if the logfile is dirty, this bit is + clear. Thus we don't need to check the + Windows version to determine if the logfile + is clean. Instead if the logfile is closed, + we know it must be clean. If it is open and + this bit is set, we also know it must be + clean. If on the other hand the logfile is + open and this bit is clear, we can be almost + certain that the logfile is dirty. */ +/* 16*/ le32 seq_number_bits; /* How many bits to use for the sequence + number. This is calculated as 67 - the + number of bits required to store the logfile + size in bytes and this can be used in with + the specified file_size as a consistency + check. */ +/* 20*/ le16 restart_area_length;/* Length of the restart area including the + client array. Following checks required if + version matches. Otherwise, skip them. + restart_area_offset + restart_area_length + has to be <= system_page_size. Also, + restart_area_length has to be >= + client_array_offset + (log_clients * + sizeof(log client record)). */ +/* 22*/ le16 client_array_offset;/* Offset from the start of this record to + the first log client record if versions are + matched. When creating, set this to be + after this restart area structure, aligned + to 8-bytes boundary. If the versions do not + match, this is ignored and the offset is + assumed to be (sizeof(RESTART_AREA) + 7) & + ~7, i.e. rounded up to first 8-byte + boundary. Either way, client_array_offset + has to be aligned to an 8-byte boundary. + Also, restart_area_offset + + client_array_offset has to be <= 510. + Finally, client_array_offset + (log_clients + * sizeof(log client record)) has to be <= + system_page_size. On Win2k and presumably + earlier, this is 0x30, i.e. immediately + following this record. On WinXP and + presumably later, this is 0x40, i.e. there + are 16 extra bytes between this record and + the client array. This probably means that + the RESTART_AREA record is actually bigger + in WinXP and later. */ +/* 24*/ sle64 file_size; /* Usable byte size of the log file. If the + restart_area_offset + the offset of the + file_size are > 510 then corruption has + occurred. This is the very first check when + starting with the restart_area as if it + fails it means that some of the above values + will be corrupted by the multi sector + transfer protection. The file_size has to + be rounded down to be a multiple of the + log_page_size in the RESTART_PAGE_HEADER and + then it has to be at least big enough to + store the two restart pages and 48 (0x30) + log record pages. */ +/* 32*/ le32 last_lsn_data_length;/* Length of data of last LSN, not including + the log record header. On create set to + 0. */ +/* 36*/ le16 log_record_header_length;/* Byte size of the log record header. + If the version matches then check that the + value of log_record_header_length is a + multiple of 8, i.e. + (log_record_header_length + 7) & ~7 == + log_record_header_length. When creating set + it to sizeof(LOG_RECORD_HEADER), aligned to + 8 bytes. */ +/* 38*/ le16 log_page_data_offset;/* Offset to the start of data in a log record + page. Must be a multiple of 8. On create + set it to immediately after the update + sequence array of the log record page. */ +/* 40*/ le32 restart_log_open_count;/* A counter that gets incremented every + time the logfile is restarted which happens + at mount time when the logfile is opened. + When creating set to a random value. Win2k + sets it to the low 32 bits of the current + system time in NTFS format (see time.h). */ +/* 44*/ le32 reserved; /* Reserved/alignment to 8-byte boundary. */ +/* sizeof() = 48 (0x30) bytes */ +} __attribute__((__packed__)) RESTART_AREA; + +/** + * struct LOG_CLIENT_RECORD - Log client record. + * + * The offset of this record is found by adding the offset of the + * RESTART_AREA to the client_array_offset value found in it. + */ +typedef struct { +/*Ofs*/ +/* 0*/ leLSN oldest_lsn; /* Oldest LSN needed by this client. On create + set to 0. */ +/* 8*/ leLSN client_restart_lsn;/* LSN at which this client needs to restart + the volume, i.e. the current position within + the log file. At present, if clean this + should = current_lsn in restart area but it + probably also = current_lsn when dirty most + of the time. At create set to 0. */ +/* 16*/ le16 prev_client; /* The offset to the previous log client record + in the array of log client records. + LOGFILE_NO_CLIENT means there is no previous + client record, i.e. this is the first one. + This is always LOGFILE_NO_CLIENT. */ +/* 18*/ le16 next_client; /* The offset to the next log client record in + the array of log client records. + LOGFILE_NO_CLIENT means there are no next + client records, i.e. this is the last one. + This is always LOGFILE_NO_CLIENT. */ +/* 20*/ le16 seq_number; /* On Win2k and presumably earlier, this is set + to zero every time the logfile is restarted + and it is incremented when the logfile is + closed at dismount time. Thus it is 0 when + dirty and 1 when clean. On WinXP and + presumably later, this is always 0. */ +/* 22*/ u8 reserved[6]; /* Reserved/alignment. */ +/* 28*/ le32 client_name_length;/* Length of client name in bytes. Should + always be 8. */ +/* 32*/ ntfschar client_name[64];/* Name of the client in Unicode. Should + always be "NTFS" with the remaining bytes + set to 0. */ +/* sizeof() = 160 (0xa0) bytes */ +} __attribute__((__packed__)) LOG_CLIENT_RECORD; + +/** + * struct RECORD_PAGE_HEADER - Log page record page header. + * + * Each log page begins with this header and is followed by several LOG_RECORD + * structures, starting at offset 0x40 (the size of this structure and the + * following update sequence array and then aligned to 8 byte boundary, but is + * this specified anywhere?). + */ +typedef struct { +/* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ + NTFS_RECORD_TYPES magic;/* Usually the magic is "RCRD". */ + le16 usa_ofs; /* See NTFS_RECORD definition in layout.h. + When creating, set this to be immediately + after this header structure (without any + alignment). */ + le16 usa_count; /* See NTFS_RECORD definition in layout.h. */ + + union { + leLSN last_lsn; + sle64 file_offset; + } __attribute__((__packed__)) copy; + le32 flags; + le16 page_count; + le16 page_position; + le16 next_record_offset; + le16 reserved[3]; + leLSN last_end_lsn; +} __attribute__((__packed__)) RECORD_PAGE_HEADER; + +/** + * enum LOG_RECORD_FLAGS - Possible 16-bit flags for log records. + * + * Some flags describe what kind of update is being logged. + * + * (Or is it log record pages?) + */ +NTFS_PACKED_ENUM_BEGIN(LOG_RECORD_FLAGS, u16) { + LOG_RECORD_MULTI_PAGE = const_cpu_to_le16(0x0001), /* ??? */ + /* The flags below were introduced in Windows 10 */ + LOG_RECORD_DELETING = const_cpu_to_le16(0x0002), + LOG_RECORD_ADDING = const_cpu_to_le16(0x0004), + LOG_RECORD_SIZE_PLACE_HOLDER = 0xffff, + /* This has nothing to do with the log record. It is only so + gcc knows to make the flags 16-bit. */ +} NTFS_PACKED_ENUM_END(LOG_RECORD_FLAGS, u16); + +/** + * struct LOG_CLIENT_ID - The log client id structure identifying a log client. + */ +typedef struct { + le16 seq_number; + le16 client_index; +} __attribute__((__packed__)) LOG_CLIENT_ID; + +/* + * LOG_RECORD_TYPE : types of log records + */ + +enum { + LOG_STANDARD = const_cpu_to_le32(1), + LOG_CHECKPOINT = const_cpu_to_le32(2), + LOG_RECORD_TYPE_PLACE_HOLDER = 0xffffffffU +} ; +typedef le32 LOG_RECORD_TYPE; + +/* + * ATTRIBUTE_FLAGS : flags describing the kind of NTFS record + * is being updated. + * These flags were introduced in Vista, only two flags are known? + */ + +enum { + ACTS_ON_MFT = const_cpu_to_le16(2), + ACTS_ON_INDX = const_cpu_to_le16(8), + ATTRIBUTE_FLAGS_PLACE_HOLDER = 0xffff, +} ; +typedef le16 ATTRIBUTE_FLAGS; + +#define LOG_RECORD_HEAD_SZ 0x30 /* size of header of struct LOG_RECORD */ + +/** + * struct LOG_RECORD - Log record header. + * + * Each log record seems to have a constant size of 0x70 bytes. + */ +typedef struct { + leLSN this_lsn; + leLSN client_previous_lsn; + leLSN client_undo_next_lsn; + le32 client_data_length; + LOG_CLIENT_ID client_id; + LOG_RECORD_TYPE record_type; + le32 transaction_id; + LOG_RECORD_FLAGS log_record_flags; + le16 reserved_or_alignment[3]; +/* Now are at ofs 0x30 into struct. */ + le16 redo_operation; + le16 undo_operation; + le16 redo_offset; + le16 redo_length; + union { + struct { + le16 undo_offset; + le16 undo_length; + le16 target_attribute; + le16 lcns_to_follow; /* Number of lcn_list entries + following this entry. */ +/* Now at ofs 0x40. */ + le16 record_offset; + le16 attribute_offset; + le16 cluster_index; + ATTRIBUTE_FLAGS attribute_flags; + leVCN target_vcn; +/* Now at ofs 0x50. */ + leLCN lcn_list[1]; /* Only present if lcns_to_follow + is not 0. */ + } __attribute__((__packed__)); + struct { + leLSN transaction_lsn; + leLSN attributes_lsn; + leLSN names_lsn; + leLSN dirty_pages_lsn; + le64 unknown_list[1]; + } __attribute__((__packed__)); + } __attribute__((__packed__)); +} __attribute__((__packed__)) LOG_RECORD; + +/** + * struct BITMAP_ACTION - Bitmap change being logged + */ + +struct BITMAP_ACTION { + le32 firstbit; + le32 count; +} ; + +/** + * struct ATTR - Attribute record. + * + * The format of an attribute record has changed from Windows 10. + * The old format was 44 bytes long, despite having 8 bytes fields, + * and this leads to alignment problems in arrays. + * This problem does not occur in the new format, which is shorter. + * The format being used can generally be determined from size. + */ +typedef struct { /* Format up to Win10 (44 bytes) */ + le64 unknown1; + le64 unknown2; + le64 inode; + leLSN lsn; + le32 unknown3; + le32 type; + le32 unknown4; +} __attribute__((__packed__)) ATTR_OLD; + +typedef struct { /* Format since Win10 (40 bytes) */ + le64 unknown1; + le64 unknown2; + le32 type; + le32 unknown3; + le64 inode; + leLSN lsn; +} __attribute__((__packed__)) ATTR_NEW; + +extern BOOL ntfs_check_logfile(ntfs_attr *log_na, RESTART_PAGE_HEADER **rp); +extern BOOL ntfs_is_logfile_clean(ntfs_attr *log_na, RESTART_PAGE_HEADER *rp); +extern int ntfs_empty_logfile(ntfs_attr *na); + +#endif /* defined _NTFS_LOGFILE_H */ + +#ifdef _MSC_VER +#pragma pack(pop) +#endif diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/logging.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/logging.h new file mode 100644 index 0000000..8c9538c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/logging.h @@ -0,0 +1,117 @@ +/* + * logging.h - Centralised logging. Originated from the Linux-NTFS project. + * + * Copyright (c) 2005 Richard Russon + * Copyright (c) 2007-2008 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _LOGGING_H_ +#define _LOGGING_H_ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDARG_H +#include +#endif + +#include "types.h" + +/* Function prototype for the logging handlers */ +typedef int (ntfs_log_handler)(const char *function, const char *file, int line, + u32 level, void *data, const char *format, va_list args); + +/* Set the logging handler from one of the functions, below. */ +void ntfs_log_set_handler(ntfs_log_handler *handler + __attribute__((format(printf, 6, 0)))); + +/* Logging handlers */ +ntfs_log_handler ntfs_log_handler_syslog __attribute__((format(printf, 6, 0))); +ntfs_log_handler ntfs_log_handler_fprintf __attribute__((format(printf, 6, 0))); +ntfs_log_handler ntfs_log_handler_null __attribute__((format(printf, 6, 0))); +ntfs_log_handler ntfs_log_handler_stdout __attribute__((format(printf, 6, 0))); +ntfs_log_handler ntfs_log_handler_outerr __attribute__((format(printf, 6, 0))); +ntfs_log_handler ntfs_log_handler_stderr __attribute__((format(printf, 6, 0))); + +/* Enable/disable certain log levels */ +u32 ntfs_log_set_levels(u32 levels); +u32 ntfs_log_clear_levels(u32 levels); +u32 ntfs_log_get_levels(void); + +/* Enable/disable certain log flags */ +u32 ntfs_log_set_flags(u32 flags); +u32 ntfs_log_clear_flags(u32 flags); +u32 ntfs_log_get_flags(void); + +/* Turn command-line options into logging flags */ +BOOL ntfs_log_parse_option(const char *option); + +int ntfs_log_redirect(const char *function, const char *file, int line, + u32 level, void *data, const char *format, ...) + __attribute__((format(printf, 6, 7))); + +/* Logging levels - Determine what gets logged */ +#define NTFS_LOG_LEVEL_DEBUG (1 << 0) /* x = 42 */ +#define NTFS_LOG_LEVEL_TRACE (1 << 1) /* Entering function x() */ +#define NTFS_LOG_LEVEL_QUIET (1 << 2) /* Quietable output */ +#define NTFS_LOG_LEVEL_INFO (1 << 3) /* Volume needs defragmenting */ +#define NTFS_LOG_LEVEL_VERBOSE (1 << 4) /* Forced to continue */ +#define NTFS_LOG_LEVEL_PROGRESS (1 << 5) /* 54% complete */ +#define NTFS_LOG_LEVEL_WARNING (1 << 6) /* You should backup before starting */ +#define NTFS_LOG_LEVEL_ERROR (1 << 7) /* Operation failed, no damage done */ +#define NTFS_LOG_LEVEL_PERROR (1 << 8) /* Message : standard error description */ +#define NTFS_LOG_LEVEL_CRITICAL (1 << 9) /* Operation failed,damage may have occurred */ +#define NTFS_LOG_LEVEL_ENTER (1 << 10) /* Enter a function */ +#define NTFS_LOG_LEVEL_LEAVE (1 << 11) /* Leave a function */ + +/* Logging style flags - Manage the style of the output */ +#define NTFS_LOG_FLAG_PREFIX (1 << 0) /* Prefix messages with "ERROR: ", etc */ +#define NTFS_LOG_FLAG_FILENAME (1 << 1) /* Show the file origin of the message */ +#define NTFS_LOG_FLAG_LINE (1 << 2) /* Show the line number of the message */ +#define NTFS_LOG_FLAG_FUNCTION (1 << 3) /* Show the function name containing the message */ +#define NTFS_LOG_FLAG_ONLYNAME (1 << 4) /* Only display the filename, not the pathname */ + +/* Macros to simplify logging. Use standard C99 variadic macros so this + * header also parses with MSVC's C front-end. */ +#define ntfs_log_critical(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_CRITICAL,NULL,__VA_ARGS__) +#define ntfs_log_error(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_ERROR,NULL,__VA_ARGS__) +#define ntfs_log_info(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_INFO,NULL,__VA_ARGS__) +#define ntfs_log_perror(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_PERROR,NULL,__VA_ARGS__) +#define ntfs_log_progress(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_PROGRESS,NULL,__VA_ARGS__) +#define ntfs_log_quiet(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_QUIET,NULL,__VA_ARGS__) +#define ntfs_log_verbose(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_VERBOSE,NULL,__VA_ARGS__) +#define ntfs_log_warning(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_WARNING,NULL,__VA_ARGS__) + +#ifdef DEBUG +#define ntfs_log_debug(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_DEBUG,NULL,__VA_ARGS__) +#define ntfs_log_trace(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_TRACE,NULL,__VA_ARGS__) +#define ntfs_log_enter(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_ENTER,NULL,__VA_ARGS__) +#define ntfs_log_leave(...) ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__,NTFS_LOG_LEVEL_LEAVE,NULL,__VA_ARGS__) +#else +#define ntfs_log_debug(...) do {} while (0) +#define ntfs_log_trace(...) do {} while (0) +#define ntfs_log_enter(...) do {} while (0) +#define ntfs_log_leave(...) do {} while (0) +#endif + +void ntfs_log_early_error(const char *format, ...) + __attribute__((format(printf, 1, 2))); + +#endif /* _LOGGING_H_ */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/mft.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/mft.h new file mode 100644 index 0000000..b135efc --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/mft.h @@ -0,0 +1,134 @@ +/* + * mft.h - Exports for MFT record handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2002 Anton Altaparmakov + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2006-2008 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_MFT_H +#define _NTFS_MFT_H + +#include "volume.h" +#include "inode.h" +#include "layout.h" +#include "logging.h" + +extern int ntfs_mft_records_read(const ntfs_volume *vol, const MFT_REF mref, + const s64 count, MFT_RECORD *b); + +/** + * ntfs_mft_record_read - read a record from the mft + * @vol: volume to read from + * @mref: mft record number to read + * @b: output data buffer + * + * Read the mft record specified by @mref from volume @vol into buffer @b. + * Return 0 on success or -1 on error, with errno set to the error code. + * + * The read mft record is mst deprotected and is hence ready to use. The caller + * should check the record with is_baad_record() in case mst deprotection + * failed. + * + * NOTE: @b has to be at least of size vol->mft_record_size. + */ +static __inline__ int ntfs_mft_record_read(const ntfs_volume *vol, + const MFT_REF mref, MFT_RECORD *b) +{ + int ret; + + ntfs_log_enter("Entering for inode %lld\n", (long long)MREF(mref)); + ret = ntfs_mft_records_read(vol, mref, 1, b); + ntfs_log_leave("\n"); + return ret; +} + +extern int ntfs_mft_record_check(const ntfs_volume *vol, const MFT_REF mref, + MFT_RECORD *m); + +extern int ntfs_file_record_read(const ntfs_volume *vol, const MFT_REF mref, + MFT_RECORD **mrec, ATTR_RECORD **attr); + +extern int ntfs_mft_records_write(const ntfs_volume *vol, const MFT_REF mref, + const s64 count, MFT_RECORD *b); + +/** + * ntfs_mft_record_write - write an mft record to disk + * @vol: volume to write to + * @mref: mft record number to write + * @b: data buffer containing the mft record to write + * + * Write the mft record specified by @mref from buffer @b to volume @vol. + * Return 0 on success or -1 on error, with errno set to the error code. + * + * Before the mft record is written, it is mst protected. After the write, it + * is deprotected again, thus resulting in an increase in the update sequence + * number inside the buffer @b. + * + * NOTE: @b has to be at least of size vol->mft_record_size. + */ +static __inline__ int ntfs_mft_record_write(const ntfs_volume *vol, + const MFT_REF mref, MFT_RECORD *b) +{ + int ret; + + ntfs_log_enter("Entering for inode %lld\n", (long long)MREF(mref)); + ret = ntfs_mft_records_write(vol, mref, 1, b); + ntfs_log_leave("\n"); + return ret; +} + +/** + * ntfs_mft_record_get_data_size - return number of bytes used in mft record @b + * @m: mft record to get the data size of + * + * Takes the mft record @m and returns the number of bytes used in the record + * or 0 on error (i.e. @m is not a valid mft record). Zero is not a valid size + * for an mft record as it at least has to have the MFT_RECORD itself and a + * zero length attribute of type AT_END, thus making the minimum size 56 bytes. + * + * Aside: The size is independent of NTFS versions 1.x/3.x because the 8-byte + * alignment of the first attribute mask the difference in MFT_RECORD size + * between NTFS 1.x and 3.x. Also, you would expect every mft record to + * contain an update sequence array as well but that could in theory be + * non-existent (don't know if Windows' NTFS driver/chkdsk wouldn't view this + * as corruption in itself though). + */ +static __inline__ u32 ntfs_mft_record_get_data_size(const MFT_RECORD *m) +{ + if (!m || !ntfs_is_mft_record(m->magic)) + return 0; + /* Get the number of used bytes and return it. */ + return le32_to_cpu(m->bytes_in_use); +} + +extern int ntfs_mft_record_layout(const ntfs_volume *vol, const MFT_REF mref, + MFT_RECORD *mrec); + +extern int ntfs_mft_record_format(const ntfs_volume *vol, const MFT_REF mref); + +extern ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, ntfs_inode *base_ni); + +extern ntfs_inode *ntfs_mft_rec_alloc(ntfs_volume *vol, BOOL mft_data); + +extern int ntfs_mft_record_free(ntfs_volume *vol, ntfs_inode *ni); + +extern int ntfs_mft_usn_dec(MFT_RECORD *mrec); + +#endif /* defined _NTFS_MFT_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/misc.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/misc.h new file mode 100644 index 0000000..c5568b3 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/misc.h @@ -0,0 +1,32 @@ +/* + * misc.h : miscellaneous exports + * - memory allocation + * + * Copyright (c) 2008 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_MISC_H_ +#define _NTFS_MISC_H_ + +void *ntfs_calloc(size_t size); +void *ntfs_malloc(size_t size); +void *ntfs_realloc(void *ptr, size_t size); +void ntfs_free(void *ptr); + +#endif /* _NTFS_MISC_H_ */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/mst.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/mst.h new file mode 100644 index 0000000..d6ca6f2 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/mst.h @@ -0,0 +1,37 @@ +/* + * mst.h - Exports for multi sector transfer fixup functions. + * Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2002 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_MST_H +#define _NTFS_MST_H + +#include "types.h" +#include "layout.h" +#include "volume.h" + +extern int ntfs_mst_post_read_fixup(NTFS_RECORD *b, const u32 size); +extern int ntfs_mst_post_read_fixup_warn(NTFS_RECORD *b, const u32 size, + BOOL warn); +extern int ntfs_mst_pre_write_fixup(NTFS_RECORD *b, const u32 size); +extern void ntfs_mst_post_write_fixup(NTFS_RECORD *b); + +#endif /* defined _NTFS_MST_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/ntfstime.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/ntfstime.h new file mode 100644 index 0000000..f3a89dd --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/ntfstime.h @@ -0,0 +1,131 @@ +/* + * ntfstime.h - NTFS time related functions. Originated from the Linux-NTFS project. + * + * Copyright (c) 2005 Anton Altaparmakov + * Copyright (c) 2005 Yura Pakhuchiy + * Copyright (c) 2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_NTFSTIME_H +#define _NTFS_NTFSTIME_H + +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_GETTIMEOFDAY +#include +#endif + +#include "types.h" + +/* + * assume "struct timespec" is not defined if st_mtime is not defined + */ +#if !defined(st_mtime) & !defined(__timespec_defined) +struct timespec { + time_t tv_sec; + long tv_nsec; +} ; +#endif + +/* + * There are four times more conversions of internal representation + * to ntfs representation than any other conversion, so the most + * efficient internal representation is ntfs representation + * (with low endianness) + */ +typedef sle64 ntfs_time; + +#define NTFS_TIME_OFFSET ((s64)(369 * 365 + 89) * 24 * 3600 * 10000000) + +/** + * ntfs2timespec - Convert an NTFS time to Unix time + * @ntfs_time: An NTFS time in 100ns units since 1601 + * + * NTFS stores times as the number of 100ns intervals since January 1st 1601 at + * 00:00 UTC. This system will not suffer from Y2K problems until ~57000AD. + * + * Return: A Unix time (number of seconds since 1970, and nanoseconds) + */ +static __inline__ struct timespec ntfs2timespec(ntfs_time ntfstime) +{ + struct timespec spec; + s64 cputime; + + cputime = sle64_to_cpu(ntfstime); + spec.tv_sec = (cputime - (NTFS_TIME_OFFSET)) / 10000000; + spec.tv_nsec = (cputime - (NTFS_TIME_OFFSET) + - (s64)spec.tv_sec*10000000)*100; + /* force zero nsec for overflowing dates */ + if ((spec.tv_nsec < 0) || (spec.tv_nsec > 999999999)) + spec.tv_nsec = 0; + return (spec); +} + +/** + * timespec2ntfs - Convert Linux time to NTFS time + * @utc_time: Linux time to convert to NTFS + * + * Convert the Linux time @utc_time to its corresponding NTFS time. + * + * Linux stores time in a long at present and measures it as the number of + * 1-second intervals since 1st January 1970, 00:00:00 UTC + * with a separated non-negative nanosecond value + * + * NTFS uses Microsoft's standard time format which is stored in a sle64 and is + * measured as the number of 100 nano-second intervals since 1st January 1601, + * 00:00:00 UTC. + * + * Return: An NTFS time (100ns units since Jan 1601) + */ +static __inline__ ntfs_time timespec2ntfs(struct timespec spec) +{ + s64 units; + + units = (s64)spec.tv_sec * 10000000 + + NTFS_TIME_OFFSET + spec.tv_nsec/100; + return (cpu_to_sle64(units)); +} + +/* + * Return the current time in ntfs format + */ + +static __inline__ ntfs_time ntfs_current_time(void) +{ + struct timespec now; + +#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_SYS_CLOCK_GETTIME) + clock_gettime(CLOCK_REALTIME, &now); +#elif defined(HAVE_GETTIMEOFDAY) + struct timeval microseconds; + + gettimeofday(µseconds, (struct timezone*)NULL); + now.tv_sec = microseconds.tv_sec; + now.tv_nsec = microseconds.tv_usec*1000; +#else + now.tv_sec = time((time_t*)NULL); + now.tv_nsec = 0; +#endif + return (timespec2ntfs(now)); +} + +#endif /* _NTFS_NTFSTIME_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/object_id.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/object_id.h new file mode 100644 index 0000000..31af9fd --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/object_id.h @@ -0,0 +1,35 @@ +/* + * + * Copyright (c) 2008 Jean-Pierre Andre + * + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef OBJECT_ID_H +#define OBJECT_ID_H + +int ntfs_get_ntfs_object_id(ntfs_inode *ni, char *value, size_t size); + +int ntfs_set_ntfs_object_id(ntfs_inode *ni, const char *value, + size_t size, int flags); +int ntfs_remove_ntfs_object_id(ntfs_inode *ni); + +int ntfs_delete_object_id_index(ntfs_inode *ni); + +#endif /* OBJECT_ID_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/param.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/param.h new file mode 100644 index 0000000..231dc76 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/param.h @@ -0,0 +1,173 @@ +/* + * param.h - Parameter values for ntfs-3g + * + * Copyright (c) 2009-2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_PARAM_H +#define _NTFS_PARAM_H + +#ifndef CACHE_INODE_SIZE +#define CACHE_INODE_SIZE 32 /* inode cache, zero or >= 3 and not too big */ +#endif +#ifndef CACHE_NIDATA_SIZE +#define CACHE_NIDATA_SIZE 64 /* idata cache, zero or >= 3 and not too big */ +#endif +#ifndef CACHE_LOOKUP_SIZE +#define CACHE_LOOKUP_SIZE 64 /* lookup cache, zero or >= 3 and not too big */ +#endif +#ifndef CACHE_SECURID_SIZE +#define CACHE_SECURID_SIZE 16 /* securid cache, zero or >= 3 and not too big */ +#endif +#ifndef CACHE_LEGACY_SIZE +#define CACHE_LEGACY_SIZE 8 /* legacy cache size, zero or >= 3 and not too big */ +#endif + +#define FORCE_FORMAT_v1x 0 /* Insert security data as in NTFS v1.x */ +#define OWNERFROMACL 1 /* Get the owner from ACL (not Windows owner) */ + + /* default security sub-authorities */ +enum { + DEFSECAUTH1 = -1153374643, /* 3141592653 */ + DEFSECAUTH2 = 589793238, + DEFSECAUTH3 = 462843383, + DEFSECBASE = 10000 +}; + +/* + * Parameters for formatting + */ + + /* Up to Windows 10, the cluster size was limited to 64K */ +#define NTFS_MAX_CLUSTER_SIZE 2097152 /* Windows 10 Creators allows 2MB */ + +/* + * Parameters for compression + */ + + /* default option for compression */ +#define DEFAULT_COMPRESSION 1 + /* (log2 of) number of clusters in a compression block for new files */ +#define STANDARD_COMPRESSION_UNIT 4 + /* maximum cluster size for allowing compression for new files */ +#define MAX_COMPRESSION_CLUSTER_SIZE 4096 + +/* + * Parameters for default options + */ + +#define DEFAULT_DMTIME 60 /* default 1mn for delay_mtime */ + +/* + * Use of big write buffers + * + * With small volumes, the cluster allocator may fail to allocate + * enough clusters when the volume is nearly full. At most a run + * can be allocated per bitmap chunk. So, there is a danger when the + * number of chunks (capacity/(32768*clsiz)) is less than the number + * of clusters in the biggest write buffer (131072/clsiz). Hence + * a safe minimal capacity is 4GB + */ + +#define SAFE_CAPACITY_FOR_BIG_WRITES 0x100000000LL + +/* + * Parameters for runlists + */ + + /* only update the final extent of a runlist when appending data */ +#define PARTIAL_RUNLIST_UPDATING 1 + +/* + * Parameters for upper-case table + */ + + /* Create upper-case tables as defined by Windows 6.1 (Win7) */ +#define UPCASE_MAJOR 6 +#define UPCASE_MINOR 1 + +/* + * Parameters for user and xattr mappings + */ + +#define XATTRMAPPINGFILE ".NTFS-3G/XattrMapping" /* default mapping file */ + +/* + * Parameters for path canonicalization + */ + +#define MAPPERNAMELTH 256 + +/* + * Permission checking modes for high level and low level + * + * The choices for high and low lowel are independent, they have + * no effect on the library + * + * Stick to the recommended values unless you understand the consequences + * on protection and performances. Use of cacheing is good for + * performances, but bad on security with internal fuse or external + * fuse older than 2.8 + * + * On Linux, cacheing is discouraged for the high level interface + * in order to get proper support of hard links. As a consequence, + * having access control in the file system leads to fewer requests + * to the file system and fewer context switches. + * + * Irrespective of the selected mode, cacheing is always used + * in read-only mounts + * + * Possible values for high level : + * 1 : no cache, kernel control (recommended) + * 4 : no cache, file system control + * 6 : kernel/fuse cache, file system control (OpenIndiana only) + * 7 : no cache, kernel control for ACLs + * + * Possible values for low level : + * 2 : no cache, kernel control + * 3 : use kernel/fuse cache, kernel control (recommended) + * 5 : no cache, file system control + * 6 : kernel/fuse cache, file system control (OpenIndiana only) + * 8 : no cache, kernel control for ACLs + * 9 : kernel/fuse cache, kernel control for ACLs (target) + * + * Use of options 7, 8 and 9 requires a fuse module upgrade + * When Posix ACLs are selected in the configure options, a value + * of 6 is added in the mount report. + */ + +#define TIMEOUT_RO 600 /* Attribute time out for read-only mounts */ +#if defined(__sun) && defined(__SVR4) +/* + * Access control by kernel is not implemented on OpenIndiana, + * however care is taken of cacheing hard-linked files. + */ +#define HPERMSCONFIG 6 +#define LPERMSCONFIG 6 +#else /* defined(__sun) && defined(__SVR4) */ +/* + * Cacheing by kernel is buggy on Linux when access control is done + * by the file system, and also when using hard-linked files on + * the fuse high level interface. + * Also ACL checks by recent kernels do not prove satisfactory. + */ +#define HPERMSCONFIG 1 +#define LPERMSCONFIG 3 +#endif /* defined(__sun) && defined(__SVR4) */ + +#endif /* defined _NTFS_PARAM_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/plugin.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/plugin.h new file mode 100644 index 0000000..88e733c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/plugin.h @@ -0,0 +1,194 @@ +/* + * plugin.h : define interface for plugin development + * + * Copyright (c) 2015-2021 Jean-Pierre Andre + * + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * This file defines the interface to ntfs-3g plugins which + * add support for processing some type of reparse points. + */ + +#ifndef _NTFS_PLUGIN_H +#define _NTFS_PLUGIN_H + +#include "layout.h" +#include "inode.h" +#include "dir.h" + +struct fuse_file_info; +struct stat; + + /* + * The plugin operations currently defined. + * These functions should return a non-negative value when they + * succeed, or a negative errno value when they fail. + * They must not close or free their arguments. + * The file system must be left in a consistent state after + * each individual call. + * If an operation is not defined, an EOPNOTSUPP error is + * returned to caller. + */ +typedef struct plugin_operations { + /* + * Set the attributes st_size, st_blocks and st_mode + * into a struct stat. The returned st_mode must at least + * define the file type. Depending on the permissions options + * used for mounting, the umask will be applied to the returned + * permissions, or the permissions will be changed according + * to the ACL set on the file. + */ + int (*getattr)(ntfs_inode *ni, const REPARSE_POINT *reparse, + struct stat *stbuf); + + /* + * Open a file for reading or writing + * The field fi->flags indicates the kind of opening. + * The field fi->fh may be used to store some information which + * will be available to subsequent reads and writes. When used + * this field must be non-null. + * The returned value is zero for success and a negative errno + * value for failure. + */ + int (*open)(ntfs_inode *ni, const REPARSE_POINT *reparse, + struct fuse_file_info *fi); + + /* + * Release an open file or directory + * This is only called if fi->fh has been set to a non-null + * value while opening. It may be used to free some context + * specific to the open file or directory + * The returned value is zero for success or a negative errno + * value for failure. + */ + int (*release)(ntfs_inode *ni, const REPARSE_POINT *reparse, + struct fuse_file_info *fi); + + /* + * Read from an open file + * The returned value is the count of bytes which were read + * or a negative errno value for failure. + * If the returned value is positive, the access time stamp + * will be updated after the call. + */ + int (*read)(ntfs_inode *ni, const REPARSE_POINT *reparse, + char *buf, size_t size, + off_t offset, struct fuse_file_info *fi); + + /* + * Write to an open file + * The file system must be left consistent after each write call, + * the file itself must be at least deletable if the application + * writing to it is killed for some reason. + * The returned value is the count of bytes which were written + * or a negative errno value for failure. + * If the returned value is positive, the modified time stamp + * will be updated after the call. + */ + int (*write)(ntfs_inode *ni, const REPARSE_POINT *reparse, + const char *buf, size_t size, + off_t offset, struct fuse_file_info *fi); + + /* + * Get a symbolic link + * The symbolic link must be returned in an allocated buffer, + * encoded in a zero terminated multibyte string compatible + * with the locale mount option. + * The returned value is zero for success or a negative errno + * value for failure. + */ + int (*readlink)(ntfs_inode *ni, const REPARSE_POINT *reparse, + char **pbuf); + + /* + * Truncate a file (shorten or append zeroes) + * The returned value is zero for success or a negative errno + * value for failure. + * If the returned value is zero, the modified time stamp + * will be updated after the call. + */ + int (*truncate)(ntfs_inode *ni, const REPARSE_POINT *reparse, + off_t size); + /* + * Open a directory + * The field fi->flags indicates the kind of opening. + * The field fi->fh may be used to store some information which + * will be available to subsequent readdir(). When used + * this field must be non-null and freed in release(). + * The returned value is zero for success and a negative errno + * value for failure. + */ + int (*opendir)(ntfs_inode *ni, const REPARSE_POINT *reparse, + struct fuse_file_info *fi); + + /* + * Get entries from a directory + * + * Use the filldir() function with fillctx argument to return + * the directory entries. + * Names "." and ".." are expected to be returned. + * The returned value is zero for success and a negative errno + * value for failure. + */ + int (*readdir)(ntfs_inode *ni, const REPARSE_POINT *reparse, + s64 *pos, void *fillctx, ntfs_filldir_t filldir, + struct fuse_file_info *fi); + /* + * Create a new file of any type + * + * The returned value is a pointer to the inode created, or + * NULL if failed, with errno telling why. + */ + ntfs_inode *(*create)(ntfs_inode *dir_ni, const REPARSE_POINT *reparse, + le32 securid, ntfschar *name, int name_len, + mode_t type); + /* + * Link a new name to a file or directory + * Linking a directory is needed for renaming a directory + * The returned value is zero for success or a negative errno + * value for failure. + * If the returned value is zero, the modified time stamp + * will be updated after the call. + */ + int (*link)(ntfs_inode *dir_ni, const REPARSE_POINT *reparse, + ntfs_inode *ni, ntfschar *name, int name_len); + /* + * Unlink a name from a directory + * The argument pathname may be NULL + * The returned value is zero for success or a negative errno + * value for failure. + */ + int (*unlink)(ntfs_inode *dir_ni, const REPARSE_POINT *reparse, + const char *pathname, + ntfs_inode *ni, ntfschar *name, int name_len); +} plugin_operations_t; + + +/* + * Plugin initialization routine + * Returns the entry table if successful, otherwise returns NULL + * and sets errno (e.g. to EINVAL if the tag is not supported by + * the plugin.) + */ +typedef const struct plugin_operations *(*plugin_init_t)(le32 tag); +const struct plugin_operations *init(le32 tag); + +#endif /* _NTFS_PLUGIN_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/realpath.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/realpath.h new file mode 100644 index 0000000..970d2af --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/realpath.h @@ -0,0 +1,24 @@ +/* + * realpath.h - realpath() aware of device mapper + */ + +#ifndef REALPATH_H +#define REALPATH_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_REALPATH +#define ntfs_realpath realpath +#else +extern char *ntfs_realpath(const char *path, char *resolved_path); +#endif + +#ifdef linux +extern char *ntfs_realpath_canonicalize(const char *path, char *resolved_path); +#else +#define ntfs_realpath_canonicalize ntfs_realpath +#endif + +#endif /* REALPATH_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/reparse.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/reparse.h new file mode 100644 index 0000000..c617951 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/reparse.h @@ -0,0 +1,51 @@ +/* + * + * Copyright (c) 2008 Jean-Pierre Andre + * + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef REPARSE_H +#define REPARSE_H + +char *ntfs_make_symlink(ntfs_inode *ni, const char *mnt_point); + +BOOL ntfs_possible_symlink(ntfs_inode *ni); + +int ntfs_get_ntfs_reparse_data(ntfs_inode *ni, char *value, size_t size); + +char *ntfs_get_abslink(ntfs_volume *vol, ntfschar *junction, + int count, const char *mnt_point, BOOL isdir); + +REPARSE_POINT *ntfs_get_reparse_point(ntfs_inode *ni); + +int ntfs_reparse_check_wsl(ntfs_inode *ni, const REPARSE_POINT *reparse); + +int ntfs_reparse_set_wsl_symlink(ntfs_inode *ni, + const ntfschar *target, int target_len); + +int ntfs_reparse_set_wsl_not_symlink(ntfs_inode *ni, mode_t mode); + +int ntfs_set_ntfs_reparse_data(ntfs_inode *ni, const char *value, + size_t size, int flags); +int ntfs_remove_ntfs_reparse_data(ntfs_inode *ni); + +int ntfs_delete_reparse_index(ntfs_inode *ni); + +#endif /* REPARSE_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/runlist.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/runlist.h new file mode 100644 index 0000000..4b73af9 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/runlist.h @@ -0,0 +1,90 @@ +/* + * runlist.h - Exports for runlist handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002 Anton Altaparmakov + * Copyright (c) 2002 Richard Russon + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_RUNLIST_H +#define _NTFS_RUNLIST_H + +#include "types.h" + +/* Forward declarations */ +typedef struct _runlist_element runlist_element; +typedef runlist_element runlist; + +#include "attrib.h" +#include "volume.h" + +/** + * struct _runlist_element - in memory vcn to lcn mapping array element. + * @vcn: starting vcn of the current array element + * @lcn: starting lcn of the current array element + * @length: length in clusters of the current array element + * + * The last vcn (in fact the last vcn + 1) is reached when length == 0. + * + * When lcn == -1 this means that the count vcns starting at vcn are not + * physically allocated (i.e. this is a hole / data is sparse). + */ +struct _runlist_element {/* In memory vcn to lcn mapping structure element. */ + VCN vcn; /* vcn = Starting virtual cluster number. */ + LCN lcn; /* lcn = Starting logical cluster number. */ + s64 length; /* Run length in clusters. */ +}; + +extern runlist_element *ntfs_rl_extend(ntfs_attr *na, runlist_element *rl, + int more_entries); + +extern LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn); + +extern s64 ntfs_rl_pread(const ntfs_volume *vol, const runlist_element *rl, + const s64 pos, s64 count, void *b); +extern s64 ntfs_rl_pwrite(const ntfs_volume *vol, const runlist_element *rl, + s64 ofs, const s64 pos, s64 count, void *b); + +extern runlist_element *ntfs_runlists_merge(runlist_element *drl, + runlist_element *srl); + +extern runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol, + const ATTR_RECORD *attr, runlist_element *old_rl); + +extern int ntfs_get_nr_significant_bytes(const s64 n); + +extern int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol, + const runlist_element *rl, const VCN start_vcn, int max_size); + +extern int ntfs_write_significant_bytes(u8 *dst, const u8 *dst_max, + const s64 n); + +extern int ntfs_mapping_pairs_build(const ntfs_volume *vol, u8 *dst, + const int dst_len, const runlist_element *rl, + const VCN start_vcn, runlist_element const **stop_rl); + +extern int ntfs_rl_truncate(runlist **arl, const VCN start_vcn); + +extern int ntfs_rl_sparse(runlist *rl); +extern s64 ntfs_rl_get_compressed_size(ntfs_volume *vol, runlist *rl); + +#ifdef NTFS_TEST +int test_rl_main(int argc, char *argv[]); +#endif + +#endif /* defined _NTFS_RUNLIST_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/security.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/security.h new file mode 100644 index 0000000..b747b61 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/security.h @@ -0,0 +1,338 @@ +/* + * security.h - Exports for handling security/ACLs in NTFS. + * Originated from the Linux-NTFS project. + * + * Copyright (c) 2004 Anton Altaparmakov + * Copyright (c) 2005-2006 Szabolcs Szakacsits + * Copyright (c) 2007-2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_SECURITY_H +#define _NTFS_SECURITY_H + +#include "types.h" +#include "layout.h" +#include "inode.h" +#include "dir.h" +#include "endians.h" + +#ifndef POSIXACLS +#define POSIXACLS 0 +#endif + +/* + * item in the mapping list + */ + +struct MAPPING { + struct MAPPING *next; + int xid; /* linux id : uid or gid */ + SID *sid; /* Windows id : usid or gsid */ + int grcnt; /* group count (for users only) */ + gid_t *groups; /* groups which the user is member of */ +}; + +/* + * Entry in the permissions cache + * Note : this cache is not organized as a generic cache + */ + +struct CACHED_PERMISSIONS { + uid_t uid; + gid_t gid; + le32 inh_fileid; + le32 inh_dirid; +#if POSIXACLS + struct POSIX_SECURITY *pxdesc; + unsigned int pxdescsize:16; +#endif + unsigned int mode:12; + unsigned int valid:1; +} ; + +/* + * Entry in the permissions cache for directories with no security_id + */ + +struct CACHED_PERMISSIONS_LEGACY { + struct CACHED_PERMISSIONS_LEGACY *next; + struct CACHED_PERMISSIONS_LEGACY *previous; + void *variable; + size_t varsize; + /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + u64 mft_no; + struct CACHED_PERMISSIONS perm; +} ; + +/* + * Entry in the securid cache + */ + +struct CACHED_SECURID { + struct CACHED_SECURID *next; + struct CACHED_SECURID *previous; + void *variable; + size_t varsize; + /* zero-length generic payload omitted for MSVC */ + /* above fields must match "struct CACHED_GENERIC" */ + uid_t uid; + gid_t gid; + unsigned int dmode; + le32 securid; +} ; + +/* + * Header of the security cache + * (has no cache structure by itself) + */ + +struct CACHED_PERMISSIONS_HEADER { + unsigned int last; + /* statistics for permissions */ + unsigned long p_writes; + unsigned long p_reads; + unsigned long p_hits; +} ; + +/* + * The whole permissions cache + */ + +struct PERMISSIONS_CACHE { + struct CACHED_PERMISSIONS_HEADER head; + struct CACHED_PERMISSIONS *cachetable[1]; /* array of variable size */ +} ; + +/* + * Security flags values + */ + +enum { + SECURITY_DEFAULT, /* rely on fuse for permissions checking */ + SECURITY_RAW, /* force same ownership/permissions on files */ + SECURITY_ACL, /* enable Posix ACLs (when compiled in) */ + SECURITY_ADDSECURIDS, /* upgrade old security descriptors */ + SECURITY_STATICGRPS, /* use static groups for access control */ + SECURITY_WANTED /* a security related option was present */ +} ; + +/* + * Security context, needed by most security functions + */ + +enum { MAPUSERS, MAPGROUPS, MAPCOUNT } ; + +struct SECURITY_CONTEXT { + ntfs_volume *vol; + struct MAPPING *mapping[MAPCOUNT]; + struct PERMISSIONS_CACHE **pseccache; + uid_t uid; /* uid of user requesting (not the mounter) */ + gid_t gid; /* gid of user requesting (not the mounter) */ + pid_t tid; /* thread id of thread requesting */ + mode_t umask; /* umask of requesting thread */ + } ; + +#if POSIXACLS + +/* + * Posix ACL structures + */ + +struct POSIX_ACE { + u16 tag; + u16 perms; + s32 id; +} __attribute__((__packed__)); + +struct POSIX_ACL { + u8 version; + u8 flags; + u16 filler; + struct POSIX_ACE ace[1]; +} __attribute__((__packed__)); + +struct POSIX_SECURITY { + mode_t mode; + int acccnt; + int defcnt; + int firstdef; + u16 tagsset; + u16 filler; + struct POSIX_ACL acl; +} ; + +/* + * Posix tags, cpu-endian 16 bits + */ + +enum { + POSIX_ACL_USER_OBJ = 1, + POSIX_ACL_USER = 2, + POSIX_ACL_GROUP_OBJ = 4, + POSIX_ACL_GROUP = 8, + POSIX_ACL_MASK = 16, + POSIX_ACL_OTHER = 32, + POSIX_ACL_SPECIAL = 64 /* internal use only */ +} ; + +#define POSIX_ACL_EXTENSIONS (POSIX_ACL_USER | POSIX_ACL_GROUP | POSIX_ACL_MASK) + +/* + * Posix permissions, cpu-endian 16 bits + */ + +enum { + POSIX_PERM_X = 1, + POSIX_PERM_W = 2, + POSIX_PERM_R = 4, + POSIX_PERM_DENIAL = 64 /* internal use only */ +} ; + +#define POSIX_VERSION 2 + +#endif + +extern BOOL ntfs_guid_is_zero(const GUID *guid); +extern char *ntfs_guid_to_mbs(const GUID *guid, char *guid_str); + +extern int ntfs_sid_to_mbs_size(const SID *sid); +extern char *ntfs_sid_to_mbs(const SID *sid, char *sid_str, + size_t sid_str_size); +extern void ntfs_generate_guid(GUID *guid); +extern int ntfs_sd_add_everyone(ntfs_inode *ni); + +extern le32 ntfs_security_hash(const SECURITY_DESCRIPTOR_RELATIVE *sd, + const u32 len); + +int ntfs_build_mapping(struct SECURITY_CONTEXT *scx, const char *usermap_path, + BOOL allowdef); +int ntfs_get_owner_mode(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, struct stat*); +int ntfs_set_mode(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, mode_t mode); +BOOL ntfs_allowed_as_owner(struct SECURITY_CONTEXT *scx, ntfs_inode *ni); +int ntfs_allowed_access(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, int accesstype); +int ntfs_allowed_create(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, gid_t *pgid, mode_t *pdsetgid); +BOOL old_ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx, + const char *path, int accesstype); + +#if POSIXACLS +le32 ntfs_alloc_securid(struct SECURITY_CONTEXT *scx, + uid_t uid, gid_t gid, ntfs_inode *dir_ni, + mode_t mode, BOOL isdir); +#else +le32 ntfs_alloc_securid(struct SECURITY_CONTEXT *scx, + uid_t uid, gid_t gid, mode_t mode, BOOL isdir); +#endif +int ntfs_set_owner(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + uid_t uid, gid_t gid); +int ntfs_set_ownmod(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, uid_t uid, gid_t gid, mode_t mode); +#if POSIXACLS +int ntfs_set_owner_mode(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, uid_t uid, gid_t gid, + mode_t mode, struct POSIX_SECURITY *pxdesc); +#else +int ntfs_set_owner_mode(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, uid_t uid, gid_t gid, mode_t mode); +#endif +le32 ntfs_inherited_id(struct SECURITY_CONTEXT *scx, + ntfs_inode *dir_ni, BOOL fordir); +int ntfs_open_secure(ntfs_volume *vol); +int ntfs_close_secure(ntfs_volume *vol); + +void ntfs_destroy_security_context(struct SECURITY_CONTEXT *scx); + +#if POSIXACLS + +int ntfs_set_inherited_posix(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, uid_t uid, gid_t gid, + ntfs_inode *dir_ni, mode_t mode); +int ntfs_get_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + const char *name, char *value, size_t size); +int ntfs_set_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + const char *name, const char *value, size_t size, + int flags); +int ntfs_remove_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + const char *name); +#endif + +int ntfs_get_ntfs_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + char *value, size_t size); +int ntfs_set_ntfs_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + const char *value, size_t size, int flags); + +int ntfs_get_ntfs_attrib(ntfs_inode *ni, char *value, size_t size); +int ntfs_set_ntfs_attrib(ntfs_inode *ni, + const char *value, size_t size, int flags); + + +/* + * Security API for direct access to security descriptors + * based on Win32 API + */ + +#define MAGIC_API 0x09042009 + +struct SECURITY_API { + u32 magic; + struct SECURITY_CONTEXT security; + struct PERMISSIONS_CACHE *seccache; +} ; + +/* + * The following constants are used in interfacing external programs. + * They are not to be stored on disk and must be defined in their + * native cpu representation. + * When disk representation (le) is needed, use SE_DACL_PRESENT, etc. + */ +enum { OWNER_SECURITY_INFORMATION = 1, + GROUP_SECURITY_INFORMATION = 2, + DACL_SECURITY_INFORMATION = 4, + SACL_SECURITY_INFORMATION = 8 +} ; + +int ntfs_get_file_security(struct SECURITY_API *scapi, + const char *path, u32 selection, + char *buf, u32 buflen, u32 *psize); +int ntfs_set_file_security(struct SECURITY_API *scapi, + const char *path, u32 selection, const char *attr); +int ntfs_get_file_attributes(struct SECURITY_API *scapi, + const char *path); +BOOL ntfs_set_file_attributes(struct SECURITY_API *scapi, + const char *path, s32 attrib); +BOOL ntfs_read_directory(struct SECURITY_API *scapi, + const char *path, ntfs_filldir_t callback, void *context); +int ntfs_read_sds(struct SECURITY_API *scapi, + char *buf, u32 size, u32 offset); +INDEX_ENTRY *ntfs_read_sii(struct SECURITY_API *scapi, + INDEX_ENTRY *entry); +INDEX_ENTRY *ntfs_read_sdh(struct SECURITY_API *scapi, + INDEX_ENTRY *entry); +struct SECURITY_API *ntfs_initialize_file_security(const char *device, + unsigned long flags); +BOOL ntfs_leave_file_security(struct SECURITY_API *scx); + +int ntfs_get_usid(struct SECURITY_API *scapi, uid_t uid, char *buf); +int ntfs_get_gsid(struct SECURITY_API *scapi, gid_t gid, char *buf); +int ntfs_get_user(struct SECURITY_API *scapi, const SID *usid); +int ntfs_get_group(struct SECURITY_API *scapi, const SID *gsid); + +#endif /* defined _NTFS_SECURITY_H */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/support.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/support.h new file mode 100644 index 0000000..eadc08d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/support.h @@ -0,0 +1,79 @@ +/* + * support.h - Useful definitions and macros. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2004 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_SUPPORT_H +#define _NTFS_SUPPORT_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDDEF_H +#include +#endif + +/* + * Our mailing list. Use this define to prevent typos in email address. + */ +#define NTFS_DEV_LIST "ntfs-3g-devel@lists.sf.net" + +/* + * Generic macro to convert pointers to values for comparison purposes. + */ +#ifndef p2n +#define p2n(p) ((ptrdiff_t)((ptrdiff_t*)(p))) +#endif + +/* + * The classic min and max macros. + */ +#ifndef min +#define min(a,b) ((a) <= (b) ? (a) : (b)) +#endif + +#ifndef max +#define max(a,b) ((a) >= (b) ? (a) : (b)) +#endif + +/* + * Useful macro for determining the offset of a struct member. + */ +#ifndef offsetof +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#endif + +/* + * Simple bit operation macros. NOTE: These are NOT atomic. + */ +#define test_bit(bit, var) ((var) & (1 << (bit))) +#define set_bit(bit, var) (var) |= 1 << (bit) +#define clear_bit(bit, var) (var) &= ~(1 << (bit)) + +#define test_and_set_bit(bit, var) \ + (test_bit((bit), (var)) ? (set_bit((bit), (var)), TRUE) : \ + (set_bit((bit), (var)), FALSE)) + +#define test_and_clear_bit(bit, var) \ + (test_bit((bit), (var)) ? (clear_bit((bit), (var)), TRUE) : \ + (clear_bit((bit), (var)), FALSE)) + +#endif /* defined _NTFS_SUPPORT_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/types.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/types.h new file mode 100644 index 0000000..43d91b2 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/types.h @@ -0,0 +1,140 @@ +/* + * types.h - Misc type definitions not related to on-disk structure. + * Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2004 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_TYPES_H +#define _NTFS_TYPES_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#if HAVE_STDINT_H || !HAVE_CONFIG_H +#include +#endif +#ifdef HAVE_SYS_TYPES_H +#include +#endif + +typedef uint8_t u8; /* Unsigned types of an exact size */ +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; + +typedef int8_t s8; /* Signed types of an exact size */ +typedef int16_t s16; +typedef int32_t s32; +typedef int64_t s64; + +typedef u16 le16; +typedef u32 le32; +typedef u64 le64; + +typedef u16 be16; +typedef u32 be32; +typedef u64 be64; + +/* + * Declare s{l,b}e{16,32,64} to be unsigned because we do not want sign + * extension on BE architectures. + */ +typedef u16 sle16; +typedef u32 sle32; +typedef u64 sle64; + +typedef u16 sbe16; +typedef u32 sbe32; +typedef u64 sbe64; + +typedef le16 ntfschar; /* 2-byte Unicode character type. */ +#define UCHAR_T_SIZE_BITS 1 + +/* + * Clusters are signed 64-bit values on NTFS volumes. We define two types, LCN + * and VCN, to allow for type checking and better code readability. + */ +typedef s64 VCN; +typedef sle64 leVCN; +typedef s64 LCN; +typedef sle64 leLCN; + +/* + * The NTFS journal $LogFile uses log sequence numbers which are signed 64-bit + * values. We define our own type LSN, to allow for type checking and better + * code readability. + */ +typedef s64 LSN; +typedef sle64 leLSN; + +/* + * Cygwin has a collision between our BOOL and 's + * As long as this file will be included after were fine. + */ +#ifndef _WINDEF_H +/** + * enum BOOL - These are just to make the code more readable... + */ +typedef enum { +#ifndef FALSE + FALSE = 0, +#endif +#ifndef NO + NO = 0, +#endif +#ifndef ZERO + ZERO = 0, +#endif +#ifndef TRUE + TRUE = 1, +#endif +#ifndef YES + YES = 1, +#endif +#ifndef ONE + ONE = 1, +#endif +} BOOL; +#endif /* defined _WINDEF_H */ + +/** + * enum IGNORE_CASE_BOOL - + */ +typedef enum { + CASE_SENSITIVE = 0, + IGNORE_CASE = 1, +} IGNORE_CASE_BOOL; + +#define STATUS_OK (0) +#define STATUS_ERROR (-1) +#define STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT (-2) +#define STATUS_KEEP_SEARCHING (-3) +#define STATUS_NOT_FOUND (-4) + +/* + * Force alignment in a struct if required by processor + */ +union ALIGNMENT { + u64 u64align; + void *ptralign; +} ; + +#endif /* defined _NTFS_TYPES_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/unistr.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/unistr.h new file mode 100644 index 0000000..28048ed --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/unistr.h @@ -0,0 +1,121 @@ +/* + * unistr.h - Exports for Unicode string handling. Originated from the Linux-NTFS + * project. + * + * Copyright (c) 2000-2004 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_UNISTR_H +#define _NTFS_UNISTR_H + +#include "types.h" +#include "layout.h" + +extern BOOL ntfs_names_are_equal(const ntfschar *s1, size_t s1_len, + const ntfschar *s2, size_t s2_len, const IGNORE_CASE_BOOL ic, + const ntfschar *upcase, const u32 upcase_size); + +extern int ntfs_names_full_collate(const ntfschar *name1, const u32 name1_len, + const ntfschar *name2, const u32 name2_len, + const IGNORE_CASE_BOOL ic, + const ntfschar *upcase, const u32 upcase_len); + +extern int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n); + +extern int ntfs_ucsncasecmp(const ntfschar *s1, const ntfschar *s2, size_t n, + const ntfschar *upcase, const u32 upcase_size); + +extern u32 ntfs_ucsnlen(const ntfschar *s, u32 maxlen); + +extern ntfschar *ntfs_ucsndup(const ntfschar *s, u32 maxlen); + +extern void ntfs_name_upcase(ntfschar *name, u32 name_len, + const ntfschar *upcase, const u32 upcase_len); + +extern void ntfs_name_locase(ntfschar *name, u32 name_len, + const ntfschar *locase, const u32 locase_len); + +extern void ntfs_file_value_upcase(FILE_NAME_ATTR *file_name_attr, + const ntfschar *upcase, const u32 upcase_len); + +extern int ntfs_ucstombs(const ntfschar *ins, const int ins_len, char **outs, + int outs_len); +extern int ntfs_mbstoucs(const char *ins, ntfschar **outs); + +extern char *ntfs_uppercase_mbs(const char *low, + const ntfschar *upcase, u32 upcase_len); + +extern void ntfs_upcase_table_build(ntfschar *uc, u32 uc_len); +extern u32 ntfs_upcase_build_default(ntfschar **upcase); +extern ntfschar *ntfs_locase_table_build(const ntfschar *uc, u32 uc_cnt); + +extern ntfschar *ntfs_str2ucs(const char *s, int *len); + +extern void ntfs_ucsfree(ntfschar *ucs); + +extern BOOL ntfs_forbidden_chars(const ntfschar *name, int len, BOOL strict); +extern BOOL ntfs_forbidden_names(ntfs_volume *vol, + const ntfschar *name, int len, BOOL strict); +extern BOOL ntfs_collapsible_chars(ntfs_volume *vol, + const ntfschar *shortname, int shortlen, + const ntfschar *longname, int longlen); + +extern int ntfs_set_char_encoding(const char *locale); + +#if defined(__APPLE__) || defined(__DARWIN__) +/** + * Mac OS X only. + * + * Sets file name Unicode normalization form conversion on or off. + * normalize=0 : Off + * normalize=1 : On + * If set to on, all filenames returned by ntfs-3g will be converted to the NFD + * normalization form, while all filenames recieved by ntfs-3g will be converted to the NFC + * normalization form. Since Windows and most other OS:es use the NFC form while Mac OS X + * mostly uses NFD, this conversion increases compatibility between Mac applications and + * NTFS-3G. + * + * @param normalize decides whether or not the string functions will do automatic filename + * normalization when converting to and from UTF-8. 0 means normalization is disabled, + * 1 means it is enabled. + * @return -1 if the argument was invalid or an error occurred, 0 if all went well. + */ +extern int ntfs_macosx_normalize_filenames(int normalize); + +/** + * Mac OS X only. + * + * Normalizes the input string "utf8_string" to one of the normalization forms NFD or NFC. + * The parameter "composed" decides whether output should be in composed, NFC, form + * (composed == 1) or decomposed, NFD, form (composed == 0). + * Input is assumed to be properly UTF-8 encoded and null-terminated. Output will be a newly + * ntfs_calloc'ed string encoded in UTF-8. It is the callers responsibility to free(...) the + * allocated string when it's no longer needed. + * + * @param utf8_string the input string, which may be in any normalization form. + * @param target a pointer where the resulting string will be stored. + * @param composed decides which composition form to normalize the input string to. 0 means + * composed form (NFC), 1 means decomposed form (NFD). + * @return -1 if the normalization failed for some reason, otherwise the length of the + * normalized string stored in target. + */ +extern int ntfs_macosx_normalize_utf8(const char *utf8_string, char **target, int composed); +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + +#endif /* defined _NTFS_UNISTR_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/volume.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/volume.h new file mode 100644 index 0000000..42800a2 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/volume.h @@ -0,0 +1,323 @@ +/* + * volume.h - Exports for NTFS volume handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2004 Anton Altaparmakov + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2005-2006 Yura Pakhuchiy + * Copyright (c) 2005-2009 Szabolcs Szakacsits + * Copyright (c) 2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_VOLUME_H +#define _NTFS_VOLUME_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_SYS_PARAM_H +#include +#endif + /* Do not #include here : conflicts with */ +#ifdef HAVE_MNTENT_H +#include +#endif + +/* Forward declaration */ +typedef struct _ntfs_volume ntfs_volume; + +#include "param.h" +#include "types.h" +#include "support.h" +#include "device.h" +#include "inode.h" +#include "attrib.h" +#include "index.h" + +/** + * enum ntfs_mount_flags - + * + * Flags for the ntfs_mount() function. + */ +enum { + NTFS_MNT_NONE = 0x00000000, + NTFS_MNT_RDONLY = 0x00000001, + NTFS_MNT_MAY_RDONLY = 0x02000000, /* Allow fallback to ro */ + NTFS_MNT_FORENSIC = 0x04000000, /* No modification during + * mount. */ + NTFS_MNT_EXCLUSIVE = 0x08000000, + NTFS_MNT_RECOVER = 0x10000000, + NTFS_MNT_IGNORE_HIBERFILE = 0x20000000, +}; +typedef unsigned long ntfs_mount_flags; + +/** + * enum ntfs_mounted_flags - + * + * Flags returned by the ntfs_check_if_mounted() function. + */ +typedef enum { + NTFS_MF_MOUNTED = 1, /* Device is mounted. */ + NTFS_MF_ISROOT = 2, /* Device is mounted as system root. */ + NTFS_MF_READONLY = 4, /* Device is mounted read-only. */ +} ntfs_mounted_flags; + +extern int ntfs_check_if_mounted(const char *file, unsigned long *mnt_flags); + +typedef enum { + NTFS_VOLUME_OK = 0, + NTFS_VOLUME_SYNTAX_ERROR = 11, + NTFS_VOLUME_NOT_NTFS = 12, + NTFS_VOLUME_CORRUPT = 13, + NTFS_VOLUME_HIBERNATED = 14, + NTFS_VOLUME_UNCLEAN_UNMOUNT = 15, + NTFS_VOLUME_LOCKED = 16, + NTFS_VOLUME_RAID = 17, + NTFS_VOLUME_UNKNOWN_REASON = 18, + NTFS_VOLUME_NO_PRIVILEGE = 19, + NTFS_VOLUME_OUT_OF_MEMORY = 20, + NTFS_VOLUME_FUSE_ERROR = 21, + NTFS_VOLUME_INSECURE = 22 +} ntfs_volume_status; + +typedef enum { + NTFS_FILES_INTERIX, + NTFS_FILES_WSL, +} ntfs_volume_special_files; + +/** + * enum ntfs_volume_state_bits - + * + * Defined bits for the state field in the ntfs_volume structure. + */ +typedef enum { + NV_ReadOnly, /* 1: Volume is read-only. */ + NV_CaseSensitive, /* 1: Volume is mounted case-sensitive. */ + NV_LogFileEmpty, /* 1: $logFile journal is empty. */ + NV_ShowSysFiles, /* 1: Show NTFS metafiles. */ + NV_ShowHidFiles, /* 1: Show files marked hidden. */ + NV_HideDotFiles, /* 1: Set hidden flag on dot files */ + NV_Compression, /* 1: allow compression */ + NV_NoFixupWarn, /* 1: Do not log fixup errors */ + NV_FreeSpaceKnown, /* 1: The free space is now known */ +} ntfs_volume_state_bits; + +#define test_nvol_flag(nv, flag) test_bit(NV_##flag, (nv)->state) +#define set_nvol_flag(nv, flag) set_bit(NV_##flag, (nv)->state) +#define clear_nvol_flag(nv, flag) clear_bit(NV_##flag, (nv)->state) + +#define NVolReadOnly(nv) test_nvol_flag(nv, ReadOnly) +#define NVolSetReadOnly(nv) set_nvol_flag(nv, ReadOnly) +#define NVolClearReadOnly(nv) clear_nvol_flag(nv, ReadOnly) + +#define NVolCaseSensitive(nv) test_nvol_flag(nv, CaseSensitive) +#define NVolSetCaseSensitive(nv) set_nvol_flag(nv, CaseSensitive) +#define NVolClearCaseSensitive(nv) clear_nvol_flag(nv, CaseSensitive) + +#define NVolLogFileEmpty(nv) test_nvol_flag(nv, LogFileEmpty) +#define NVolSetLogFileEmpty(nv) set_nvol_flag(nv, LogFileEmpty) +#define NVolClearLogFileEmpty(nv) clear_nvol_flag(nv, LogFileEmpty) + +#define NVolShowSysFiles(nv) test_nvol_flag(nv, ShowSysFiles) +#define NVolSetShowSysFiles(nv) set_nvol_flag(nv, ShowSysFiles) +#define NVolClearShowSysFiles(nv) clear_nvol_flag(nv, ShowSysFiles) + +#define NVolShowHidFiles(nv) test_nvol_flag(nv, ShowHidFiles) +#define NVolSetShowHidFiles(nv) set_nvol_flag(nv, ShowHidFiles) +#define NVolClearShowHidFiles(nv) clear_nvol_flag(nv, ShowHidFiles) + +#define NVolHideDotFiles(nv) test_nvol_flag(nv, HideDotFiles) +#define NVolSetHideDotFiles(nv) set_nvol_flag(nv, HideDotFiles) +#define NVolClearHideDotFiles(nv) clear_nvol_flag(nv, HideDotFiles) + +#define NVolCompression(nv) test_nvol_flag(nv, Compression) +#define NVolSetCompression(nv) set_nvol_flag(nv, Compression) +#define NVolClearCompression(nv) clear_nvol_flag(nv, Compression) + +#define NVolNoFixupWarn(nv) test_nvol_flag(nv, NoFixupWarn) +#define NVolSetNoFixupWarn(nv) set_nvol_flag(nv, NoFixupWarn) +#define NVolClearNoFixupWarn(nv) clear_nvol_flag(nv, NoFixupWarn) + +#define NVolFreeSpaceKnown(nv) test_nvol_flag(nv, FreeSpaceKnown) +#define NVolSetFreeSpaceKnown(nv) set_nvol_flag(nv, FreeSpaceKnown) +#define NVolClearFreeSpaceKnown(nv) clear_nvol_flag(nv, FreeSpaceKnown) + +/* + * NTFS version 1.1 and 1.2 are used by Windows NT4. + * NTFS version 2.x is used by Windows 2000 Beta + * NTFS version 3.0 is used by Windows 2000. + * NTFS version 3.1 is used by Windows XP, 2003 and Vista. + */ + +#define NTFS_V1_1(major, minor) ((major) == 1 && (minor) == 1) +#define NTFS_V1_2(major, minor) ((major) == 1 && (minor) == 2) +#define NTFS_V2_X(major, minor) ((major) == 2) +#define NTFS_V3_0(major, minor) ((major) == 3 && (minor) == 0) +#define NTFS_V3_1(major, minor) ((major) == 3 && (minor) == 1) + +#define NTFS_BUF_SIZE 8192 + +/** + * struct _ntfs_volume - structure describing an open volume in memory. + */ +struct _ntfs_volume { + union { + struct ntfs_device *dev; /* NTFS device associated with + the volume. */ + void *sb; /* For kernel porting compatibility. */ + }; + char *vol_name; /* Name of the volume. */ + unsigned long state; /* NTFS specific flags describing this volume. + See ntfs_volume_state_bits above. */ + + ntfs_inode *vol_ni; /* ntfs_inode structure for FILE_Volume. */ + u8 major_ver; /* Ntfs major version of volume. */ + u8 minor_ver; /* Ntfs minor version of volume. */ + le16 flags; /* Bit array of VOLUME_* flags. */ + + u16 sector_size; /* Byte size of a sector. */ + u8 sector_size_bits; /* Log(2) of the byte size of a sector. */ + u32 cluster_size; /* Byte size of a cluster. */ + u32 mft_record_size; /* Byte size of a mft record. */ + u32 indx_record_size; /* Byte size of a INDX record. */ + u8 cluster_size_bits; /* Log(2) of the byte size of a cluster. */ + u8 mft_record_size_bits;/* Log(2) of the byte size of a mft record. */ + u8 indx_record_size_bits;/* Log(2) of the byte size of a INDX record. */ + + /* Variables used by the cluster and mft allocators. */ + u8 mft_zone_multiplier; /* Initial mft zone multiplier. */ + u8 full_zones; /* cluster zones which are full */ + s64 mft_data_pos; /* Mft record number at which to allocate the + next mft record. */ + LCN mft_zone_start; /* First cluster of the mft zone. */ + LCN mft_zone_end; /* First cluster beyond the mft zone. */ + LCN mft_zone_pos; /* Current position in the mft zone. */ + LCN data1_zone_pos; /* Current position in the first data zone. */ + LCN data2_zone_pos; /* Current position in the second data zone. */ + + s64 nr_clusters; /* Volume size in clusters, hence also the + number of bits in lcn_bitmap. */ + ntfs_inode *lcnbmp_ni; /* ntfs_inode structure for FILE_Bitmap. */ + ntfs_attr *lcnbmp_na; /* ntfs_attr structure for the data attribute + of FILE_Bitmap. Each bit represents a + cluster on the volume, bit 0 representing + lcn 0 and so on. A set bit means that the + cluster and vice versa. */ + + LCN mft_lcn; /* Logical cluster number of the data attribute + for FILE_MFT. */ + ntfs_inode *mft_ni; /* ntfs_inode structure for FILE_MFT. */ + ntfs_attr *mft_na; /* ntfs_attr structure for the data attribute + of FILE_MFT. */ + ntfs_attr *mftbmp_na; /* ntfs_attr structure for the bitmap attribute + of FILE_MFT. Each bit represents an mft + record in the $DATA attribute, bit 0 + representing mft record 0 and so on. A set + bit means that the mft record is in use and + vice versa. */ + + ntfs_inode *secure_ni; /* ntfs_inode structure for FILE $Secure */ + ntfs_index_context *secure_xsii; /* index for using $Secure:$SII */ + ntfs_index_context *secure_xsdh; /* index for using $Secure:$SDH */ + int secure_reentry; /* check for non-rentries */ + unsigned int secure_flags; /* flags, see security.h for values */ + + int mftmirr_size; /* Size of the FILE_MFTMirr in mft records. */ + LCN mftmirr_lcn; /* Logical cluster number of the data attribute + for FILE_MFTMirr. */ + ntfs_inode *mftmirr_ni; /* ntfs_inode structure for FILE_MFTMirr. */ + ntfs_attr *mftmirr_na; /* ntfs_attr structure for the data attribute + of FILE_MFTMirr. */ + + ntfschar *upcase; /* Upper case equivalents of all 65536 2-byte + Unicode characters. Obtained from + FILE_UpCase. */ + u32 upcase_len; /* Length in Unicode characters of the upcase + table. */ + ntfschar *locase; /* Lower case equivalents of all 65536 2-byte + Unicode characters. Only if option + case_ignore is set. */ + + ATTR_DEF *attrdef; /* Attribute definitions. Obtained from + FILE_AttrDef. */ + s32 attrdef_len; /* Size of the attribute definition table in + bytes. */ + + s64 free_clusters; /* Track the number of free clusters which + greatly improves statfs() performance */ + s64 free_mft_records; /* Same for free mft records (see above) */ + BOOL efs_raw; /* volume is mounted for raw access to + efs-encrypted files */ + ntfs_volume_special_files special_files; /* Implementation of special files */ + const char *abs_mnt_point; /* Mount point */ +#ifdef XATTR_MAPPINGS + struct XATTRMAPPING *xattr_mapping; +#endif /* XATTR_MAPPINGS */ +#if CACHE_INODE_SIZE + struct CACHE_HEADER *xinode_cache; +#endif +#if CACHE_NIDATA_SIZE + struct CACHE_HEADER *nidata_cache; +#endif +#if CACHE_LOOKUP_SIZE + struct CACHE_HEADER *lookup_cache; +#endif +#if CACHE_SECURID_SIZE + struct CACHE_HEADER *securid_cache; +#endif +#if CACHE_LEGACY_SIZE + struct CACHE_HEADER *legacy_cache; +#endif +}; + +extern const char *ntfs_home; + +extern ntfs_volume *ntfs_volume_alloc(void); + +extern ntfs_volume *ntfs_volume_startup(struct ntfs_device *dev, + ntfs_mount_flags flags); + +extern ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, + ntfs_mount_flags flags); + +extern ntfs_volume *ntfs_mount(const char *name, ntfs_mount_flags flags); +extern int ntfs_umount(ntfs_volume *vol, const BOOL force); + +extern int ntfs_version_is_supported(ntfs_volume *vol); +extern int ntfs_volume_check_hiberfile(ntfs_volume *vol, int verbose); +extern int ntfs_logfile_reset(ntfs_volume *vol); + +extern int ntfs_volume_write_flags(ntfs_volume *vol, const le16 flags); + +extern int ntfs_volume_error(int err); +extern void ntfs_mount_error(const char *vol, const char *mntpoint, int err); + +extern int ntfs_volume_get_free_space(ntfs_volume *vol); +extern int ntfs_volume_rename(ntfs_volume *vol, const ntfschar *label, + int label_len); + +extern int ntfs_set_shown_files(ntfs_volume *vol, + BOOL show_sys_files, BOOL show_hid_files, BOOL hide_dot_files); +extern int ntfs_set_locale(void); +extern int ntfs_set_ignore_case(ntfs_volume *vol); + +#endif /* defined _NTFS_VOLUME_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/xattrs.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/xattrs.h new file mode 100644 index 0000000..c5faeac --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/include/ntfs-3g/xattrs.h @@ -0,0 +1,97 @@ +/* + * xattrs.h : definitions related to system extended attributes + * + * Copyright (c) 2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_XATTRS_H_ +#define _NTFS_XATTRS_H_ + +/* + * Flags that modify setxattr() semantics. These flags are also used by a + * number of libntfs-3g functions, such as ntfs_set_ntfs_acl(), which were + * originally tied to extended attributes support but now can be used by + * applications even if the platform does not support extended attributes. + * + * Careful: applications including this header should define HAVE_SETXATTR or + * HAVE_SYS_XATTR_H if the platform supports extended attributes. Otherwise the + * defined flags values may be incorrect (they will be correct for Linux but not + * necessarily for other platforms). + */ +#if defined(HAVE_SETXATTR) || defined(HAVE_SYS_XATTR_H) +#include +#else +#include "compat.h" /* may be needed for ENODATA definition */ +#define XATTR_CREATE 1 +#define XATTR_REPLACE 2 +#endif + +/* + * Identification of data mapped to the system name space + */ + +enum SYSTEMXATTRS { + XATTR_UNMAPPED, + XATTR_NTFS_ACL, + XATTR_NTFS_ATTRIB, + XATTR_NTFS_ATTRIB_BE, + XATTR_NTFS_EFSINFO, + XATTR_NTFS_REPARSE_DATA, + XATTR_NTFS_OBJECT_ID, + XATTR_NTFS_DOS_NAME, + XATTR_NTFS_TIMES, + XATTR_NTFS_TIMES_BE, + XATTR_NTFS_CRTIME, + XATTR_NTFS_CRTIME_BE, + XATTR_NTFS_EA, + XATTR_POSIX_ACC, + XATTR_POSIX_DEF +} ; + +struct XATTRMAPPING { + struct XATTRMAPPING *next; + enum SYSTEMXATTRS xattr; + char name[1]; /* variable length */ +} ; + +#ifdef XATTR_MAPPINGS + +struct XATTRMAPPING *ntfs_xattr_build_mapping(ntfs_volume *vol, + const char *path); +void ntfs_xattr_free_mapping(struct XATTRMAPPING*); + +#endif /* XATTR_MAPPINGS */ + +enum SYSTEMXATTRS ntfs_xattr_system_type(const char *name, + ntfs_volume *vol); + +struct SECURITY_CONTEXT; + +int ntfs_xattr_system_getxattr(struct SECURITY_CONTEXT *scx, + enum SYSTEMXATTRS attr, + ntfs_inode *ni, ntfs_inode *dir_ni, + char *value, size_t size); +int ntfs_xattr_system_setxattr(struct SECURITY_CONTEXT *scx, + enum SYSTEMXATTRS attr, + ntfs_inode *ni, ntfs_inode *dir_ni, + const char *value, size_t size, int flags); +int ntfs_xattr_system_removexattr(struct SECURITY_CONTEXT *scx, + enum SYSTEMXATTRS attr, + ntfs_inode *ni, ntfs_inode *dir_ni); + +#endif /* _NTFS_XATTRS_H_ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/Makefile.am b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/Makefile.am new file mode 100644 index 0000000..ba2934c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/Makefile.am @@ -0,0 +1,34 @@ + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +if FUSE_INTERNAL +noinst_LTLIBRARIES = libfuse-lite.la +endif + +libfuse_lite_la_CFLAGS= \ + $(AM_CFLAGS) \ + $(LIBFUSE_LITE_CFLAGS) + +libfuse_lite_la_CPPFLAGS= \ + $(AM_CPPFLAGS) \ + -I$(top_srcdir)/include/fuse-lite + +libfuse_lite_la_LIBADD = $(LIBFUSE_LITE_LIBS) + +libfuse_lite_la_SOURCES = \ + fuse.c \ + fuse_i.h \ + fuse_kern_chan.c \ + fuse_loop.c \ + fuse_lowlevel.c \ + fuse_misc.h \ + fuse_opt.c \ + fuse_session.c \ + fuse_signals.c \ + fusermount.c \ + helper.c \ + mount.c \ + mount_util.c \ + mount_util.h + +libs: libfuse-lite.la diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse.c new file mode 100644 index 0000000..3d653e6 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse.c @@ -0,0 +1,3249 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#ifdef __SOLARIS__ +/* For pthread_rwlock_t */ +#define _GNU_SOURCE +#endif /* __SOLARIS__ */ + +#include "config.h" +#include "fuse_i.h" +#include "fuse_lowlevel.h" +#include "fuse_opt.h" +#include "fuse_misc.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __SOLARIS__ +#define FUSE_MAX_PATH 4096 +#endif /* __SOLARIS__ */ + +#define FUSE_DEFAULT_INTR_SIGNAL SIGUSR1 + +#define FUSE_UNKNOWN_INO 0xffffffff +#define OFFSET_MAX 0x7fffffffffffffffLL + +struct fuse_config { + unsigned int uid; + unsigned int gid; + unsigned int umask; + double entry_timeout; + double negative_timeout; + double attr_timeout; + double ac_attr_timeout; + int ac_attr_timeout_set; + int debug; + int hard_remove; + int use_ino; + int readdir_ino; + int set_mode; + int set_uid; + int set_gid; + int direct_io; + int kernel_cache; + int intr; + int intr_signal; + int help; +#ifdef __SOLARIS__ + int auto_cache; + char *modules; +#endif /* __SOLARIS__ */ +}; + +struct fuse_fs { + struct fuse_operations op; + void *user_data; +#ifdef __SOLARIS__ + struct fuse_module *m; +#endif /* __SOLARIS__ */ +}; + +#ifdef __SOLARIS__ +struct fusemod_so { + void *handle; + int ctr; +}; +#endif /* __SOLARIS__ */ + +struct fuse { + struct fuse_session *se; + struct node **name_table; + size_t name_table_size; + struct node **id_table; + size_t id_table_size; + fuse_ino_t ctr; + unsigned int generation; + unsigned int hidectr; + pthread_mutex_t lock; + pthread_rwlock_t tree_lock; + struct fuse_config conf; + int intr_installed; + struct fuse_fs *fs; +}; + +struct lock { + int type; + off_t start; + off_t end; + pid_t pid; + uint64_t owner; + struct lock *next; +}; + +struct node { + struct node *name_next; + struct node *id_next; + fuse_ino_t nodeid; + unsigned int generation; + int refctr; + struct node *parent; + char *name; + uint64_t nlookup; + int open_count; + int is_hidden; +#ifdef __SOLARIS__ + struct timespec stat_updated; + struct timespec mtime; + off_t size; + int cache_valid; +#endif /* __SOLARIS__ */ + struct lock *locks; +}; + +struct fuse_dh { + pthread_mutex_t lock; + struct fuse *fuse; + fuse_req_t req; + char *contents; + unsigned len; + unsigned size; + unsigned needlen; + int filled; + uint64_t fh; + int error; + fuse_ino_t nodeid; +}; + +struct fuse_context_i { + struct fuse_context ctx; + fuse_req_t req; +}; + +static pthread_key_t fuse_context_key; +static pthread_mutex_t fuse_context_lock = PTHREAD_MUTEX_INITIALIZER; +static int fuse_context_ref; + +#ifdef __SOLARIS__ + +static struct fusemod_so *fuse_current_so; +static struct fuse_module *fuse_modules; + +static int fuse_load_so_name(const char *soname) +{ + struct fusemod_so *so; + + so = calloc(1, sizeof(struct fusemod_so)); + if (!so) { + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; + } + + fuse_current_so = so; + so->handle = dlopen(soname, RTLD_NOW); + fuse_current_so = NULL; + if (!so->handle) { + fprintf(stderr, "fuse: %s\n", dlerror()); + goto err; + } + if (!so->ctr) { + fprintf(stderr, "fuse: %s did not register any modules", soname); + goto err; + } + return 0; + + err: + if (so->handle) + dlclose(so->handle); + free(so); + return -1; +} + +static int fuse_load_so_module(const char *module) +{ + int res; + char *soname = malloc(strlen(module) + 64); + if (!soname) { + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; + } + sprintf(soname, "libfusemod_%s.so", module); + res = fuse_load_so_name(soname); + free(soname); + return res; +} + +static struct fuse_module *fuse_find_module(const char *module) +{ + struct fuse_module *m; + for (m = fuse_modules; m; m = m->next) { + if (strcmp(module, m->name) == 0) { + m->ctr++; + break; + } + } + return m; +} + +static struct fuse_module *fuse_get_module(const char *module) +{ + struct fuse_module *m; + + pthread_mutex_lock(&fuse_context_lock); + m = fuse_find_module(module); + if (!m) { + int err = fuse_load_so_module(module); + if (!err) + m = fuse_find_module(module); + } + pthread_mutex_unlock(&fuse_context_lock); + return m; +} + +static void fuse_put_module(struct fuse_module *m) +{ + pthread_mutex_lock(&fuse_context_lock); + assert(m->ctr > 0); + m->ctr--; + if (!m->ctr && m->so) { + struct fusemod_so *so = m->so; + assert(so->ctr > 0); + so->ctr--; + if (!so->ctr) { + struct fuse_module **mp; + for (mp = &fuse_modules; *mp;) { + if ((*mp)->so == so) + *mp = (*mp)->next; + else + mp = &(*mp)->next; + } + dlclose(so->handle); + free(so); + } + } + pthread_mutex_unlock(&fuse_context_lock); +} +#endif /* __SOLARIS__ */ + +static struct node *get_node_nocheck(struct fuse *f, fuse_ino_t nodeid) +{ + size_t hash = nodeid % f->id_table_size; + struct node *node; + + for (node = f->id_table[hash]; node != NULL; node = node->id_next) + if (node->nodeid == nodeid) + return node; + + return NULL; +} + +static struct node *get_node(struct fuse *f, fuse_ino_t nodeid) +{ + struct node *node = get_node_nocheck(f, nodeid); + if (!node) { + fprintf(stderr, "fuse internal error: node %llu not found\n", + (unsigned long long) nodeid); + abort(); + } + return node; +} + +static void free_node(struct node *node) +{ + free(node->name); + free(node); +} + +static void unhash_id(struct fuse *f, struct node *node) +{ + size_t hash = node->nodeid % f->id_table_size; + struct node **nodep = &f->id_table[hash]; + + for (; *nodep != NULL; nodep = &(*nodep)->id_next) + if (*nodep == node) { + *nodep = node->id_next; + return; + } +} + +static void hash_id(struct fuse *f, struct node *node) +{ + size_t hash = node->nodeid % f->id_table_size; + node->id_next = f->id_table[hash]; + f->id_table[hash] = node; +} + +static unsigned int name_hash(struct fuse *f, fuse_ino_t parent, + const char *name) +{ + unsigned int hash = *name; + + if (hash) + for (name += 1; *name != '\0'; name++) + hash = (hash << 5) - hash + *name; + + return (hash + parent) % f->name_table_size; +} + +static void unref_node(struct fuse *f, struct node *node); + +static void unhash_name(struct fuse *f, struct node *node) +{ + if (node->name) { + size_t hash = name_hash(f, node->parent->nodeid, node->name); + struct node **nodep = &f->name_table[hash]; + + for (; *nodep != NULL; nodep = &(*nodep)->name_next) + if (*nodep == node) { + *nodep = node->name_next; + node->name_next = NULL; + unref_node(f, node->parent); + free(node->name); + node->name = NULL; + node->parent = NULL; + return; + } + fprintf(stderr, "fuse internal error: unable to unhash node: %llu\n", + (unsigned long long) node->nodeid); + abort(); + } +} + +static int hash_name(struct fuse *f, struct node *node, fuse_ino_t parentid, + const char *name) +{ + size_t hash = name_hash(f, parentid, name); + struct node *parent = get_node(f, parentid); + node->name = strdup(name); + if (node->name == NULL) + return -1; + + parent->refctr ++; + node->parent = parent; + node->name_next = f->name_table[hash]; + f->name_table[hash] = node; + return 0; +} + +static void delete_node(struct fuse *f, struct node *node) +{ + if (f->conf.debug) + fprintf(stderr, "delete: %llu\n", (unsigned long long) node->nodeid); + + assert(!node->name); + unhash_id(f, node); + free_node(node); +} + +static void unref_node(struct fuse *f, struct node *node) +{ + assert(node->refctr > 0); + node->refctr --; + if (!node->refctr) + delete_node(f, node); +} + +static fuse_ino_t next_id(struct fuse *f) +{ + do { + f->ctr = (f->ctr + 1) & 0xffffffff; + if (!f->ctr) + f->generation ++; + } while (f->ctr == 0 || f->ctr == FUSE_UNKNOWN_INO || + get_node_nocheck(f, f->ctr) != NULL); + return f->ctr; +} + +static struct node *lookup_node(struct fuse *f, fuse_ino_t parent, + const char *name) +{ + size_t hash = name_hash(f, parent, name); + struct node *node; + + for (node = f->name_table[hash]; node != NULL; node = node->name_next) + if (node->parent->nodeid == parent && strcmp(node->name, name) == 0) + return node; + + return NULL; +} + +static struct node *find_node(struct fuse *f, fuse_ino_t parent, + const char *name) +{ + struct node *node; + + pthread_mutex_lock(&f->lock); + node = lookup_node(f, parent, name); + if (node == NULL) { + node = (struct node *) calloc(1, sizeof(struct node)); + if (node == NULL) + goto out_err; + + node->refctr = 1; + node->nodeid = next_id(f); + node->open_count = 0; + node->is_hidden = 0; + node->generation = f->generation; + if (hash_name(f, node, parent, name) == -1) { + free(node); + node = NULL; + goto out_err; + } + hash_id(f, node); + } + node->nlookup ++; + out_err: + pthread_mutex_unlock(&f->lock); + return node; +} + +#ifndef __SOLARIS__ +static char *add_name(char **buf, unsigned *bufsize, char *s, const char *name) +#else /* __SOLARIS__ */ +static char *add_name(char *buf, char *s, const char *name) +#endif /* __SOLARIS__ */ +{ + size_t len = strlen(name); + +#ifndef __SOLARIS__ + if (s - len <= *buf) { + unsigned pathlen = *bufsize - (s - *buf); + unsigned newbufsize = *bufsize; + char *newbuf; + + while (newbufsize < pathlen + len + 1) { + if (newbufsize >= 0x80000000) + newbufsize = 0xffffffff; + else + newbufsize *= 2; + } + + newbuf = realloc(*buf, newbufsize); + if (newbuf == NULL) + return NULL; + + *buf = newbuf; + s = newbuf + newbufsize - pathlen; + memmove(s, newbuf + *bufsize - pathlen, pathlen); + *bufsize = newbufsize; + } + s -= len; +#else /* ! __SOLARIS__ */ + s -= len; + if (s <= buf) { + fprintf(stderr, "fuse: path too long: ...%s\n", s + len); + return NULL; + } +#endif /* __SOLARIS__ */ + memcpy(s, name, len); + s--; + *s = '/'; + + return s; +} + +static char *get_path_name(struct fuse *f, fuse_ino_t nodeid, const char *name) +{ +#ifdef __SOLARIS__ + char buf[FUSE_MAX_PATH]; + char *s = buf + FUSE_MAX_PATH - 1; + struct node *node; + + *s = '\0'; + + if (name != NULL) { + s = add_name(buf, s, name); + if (s == NULL) + return NULL; + } + + pthread_mutex_lock(&f->lock); + for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID; + node = node->parent) { + if (node->name == NULL) { + s = NULL; + break; + } + + s = add_name(buf, s, node->name); + if (s == NULL) + break; + } + pthread_mutex_unlock(&f->lock); + + if (node == NULL || s == NULL) + return NULL; + else if (*s == '\0') + return strdup("/"); + else + return strdup(s); + +#else /* __SOLARIS__ */ + + unsigned bufsize = 256; + char *buf; + char *s; + struct node *node; + + buf = malloc(bufsize); + if (buf == NULL) + return NULL; + + s = buf + bufsize - 1; + *s = '\0'; + + if (name != NULL) { + s = add_name(&buf, &bufsize, s, name); + if (s == NULL) + goto out_free; + } + + pthread_mutex_lock(&f->lock); + for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID; + node = node->parent) { + if (node->name == NULL) { + s = NULL; + break; + } + + s = add_name(&buf, &bufsize, s, node->name); + if (s == NULL) + break; + } + pthread_mutex_unlock(&f->lock); + + if (node == NULL || s == NULL) + goto out_free; + + if (s[0]) + memmove(buf, s, bufsize - (s - buf)); + else + strcpy(buf, "/"); + return buf; + +out_free: + free(buf); + return NULL; +#endif /* __SOLARIS__ */ +} + +static char *get_path(struct fuse *f, fuse_ino_t nodeid) +{ + return get_path_name(f, nodeid, NULL); +} + +static void forget_node(struct fuse *f, fuse_ino_t nodeid, uint64_t nlookup) +{ + struct node *node; + if (nodeid == FUSE_ROOT_ID) + return; + pthread_mutex_lock(&f->lock); + node = get_node(f, nodeid); + assert(node->nlookup >= nlookup); + node->nlookup -= nlookup; + if (!node->nlookup) { + unhash_name(f, node); + unref_node(f, node); + } + pthread_mutex_unlock(&f->lock); +} + +static void remove_node(struct fuse *f, fuse_ino_t dir, const char *name) +{ + struct node *node; + + pthread_mutex_lock(&f->lock); + node = lookup_node(f, dir, name); + if (node != NULL) + unhash_name(f, node); + pthread_mutex_unlock(&f->lock); +} + +static int rename_node(struct fuse *f, fuse_ino_t olddir, const char *oldname, + fuse_ino_t newdir, const char *newname, int hide) +{ + struct node *node; + struct node *newnode; + int err = 0; + + pthread_mutex_lock(&f->lock); + node = lookup_node(f, olddir, oldname); + newnode = lookup_node(f, newdir, newname); + if (node == NULL) + goto out; + + if (newnode != NULL) { + if (hide) { + fprintf(stderr, "fuse: hidden file got created during hiding\n"); + err = -EBUSY; + goto out; + } + unhash_name(f, newnode); + } + + unhash_name(f, node); + if (hash_name(f, node, newdir, newname) == -1) { + err = -ENOMEM; + goto out; + } + + if (hide) + node->is_hidden = 1; + + out: + pthread_mutex_unlock(&f->lock); + return err; +} + +static void set_stat(struct fuse *f, fuse_ino_t nodeid, struct stat *stbuf) +{ + if (!f->conf.use_ino) + stbuf->st_ino = nodeid; + if (f->conf.set_mode) + stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->conf.umask); + if (f->conf.set_uid) + stbuf->st_uid = f->conf.uid; + if (f->conf.set_gid) + stbuf->st_gid = f->conf.gid; +} + +static struct fuse *req_fuse(fuse_req_t req) +{ + return (struct fuse *) fuse_req_userdata(req); +} + +static void fuse_intr_sighandler(int sig) +{ + (void) sig; + /* Nothing to do */ +} + +struct fuse_intr_data { + pthread_t id; + pthread_cond_t cond; + int finished; +}; + +static void fuse_interrupt(fuse_req_t req, void *d_) +{ + struct fuse_intr_data *d = d_; + struct fuse *f = req_fuse(req); + + if (d->id == pthread_self()) + return; + + pthread_mutex_lock(&f->lock); + while (!d->finished) { + struct timeval now; + struct timespec timeout; + + pthread_kill(d->id, f->conf.intr_signal); + gettimeofday(&now, NULL); + timeout.tv_sec = now.tv_sec + 1; + timeout.tv_nsec = now.tv_usec * 1000; + pthread_cond_timedwait(&d->cond, &f->lock, &timeout); + } + pthread_mutex_unlock(&f->lock); +} + +static void fuse_do_finish_interrupt(struct fuse *f, fuse_req_t req, + struct fuse_intr_data *d) +{ + pthread_mutex_lock(&f->lock); + d->finished = 1; + pthread_cond_broadcast(&d->cond); + pthread_mutex_unlock(&f->lock); + fuse_req_interrupt_func(req, NULL, NULL); + pthread_cond_destroy(&d->cond); +} + +static void fuse_do_prepare_interrupt(fuse_req_t req, struct fuse_intr_data *d) +{ + d->id = pthread_self(); + pthread_cond_init(&d->cond, NULL); + d->finished = 0; + fuse_req_interrupt_func(req, fuse_interrupt, d); +} + +static void fuse_finish_interrupt(struct fuse *f, fuse_req_t req, + struct fuse_intr_data *d) +{ + if (f->conf.intr) + fuse_do_finish_interrupt(f, req, d); +} + +static void fuse_prepare_interrupt(struct fuse *f, fuse_req_t req, + struct fuse_intr_data *d) +{ + if (f->conf.intr) + fuse_do_prepare_interrupt(req, d); +} + +int fuse_fs_getattr(struct fuse_fs *fs, const char *path, struct stat *buf) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.getattr) + return fs->op.getattr(path, buf); + else + return -ENOSYS; +} + +int fuse_fs_fgetattr(struct fuse_fs *fs, const char *path, struct stat *buf, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.fgetattr) + return fs->op.fgetattr(path, buf, fi); + else if (fs->op.getattr) + return fs->op.getattr(path, buf); + else + return -ENOSYS; +} + +int fuse_fs_rename(struct fuse_fs *fs, const char *oldpath, + const char *newpath) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.rename) + return fs->op.rename(oldpath, newpath); + else + return -ENOSYS; +} + +int fuse_fs_unlink(struct fuse_fs *fs, const char *path) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.unlink) + return fs->op.unlink(path); + else + return -ENOSYS; +} + +int fuse_fs_rmdir(struct fuse_fs *fs, const char *path) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.rmdir) + return fs->op.rmdir(path); + else + return -ENOSYS; +} + +int fuse_fs_symlink(struct fuse_fs *fs, const char *linkname, const char *path) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.symlink) + return fs->op.symlink(linkname, path); + else + return -ENOSYS; +} + +int fuse_fs_link(struct fuse_fs *fs, const char *oldpath, const char *newpath) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.link) + return fs->op.link(oldpath, newpath); + else + return -ENOSYS; +} + +int fuse_fs_release(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.release) + return fs->op.release(path, fi); + else + return 0; +} + +int fuse_fs_opendir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.opendir) + return fs->op.opendir(path, fi); + else + return 0; +} + +int fuse_fs_open(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.open) + return fs->op.open(path, fi); + else + return 0; +} + +int fuse_fs_read(struct fuse_fs *fs, const char *path, char *buf, size_t size, + off_t off, struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.read) + return fs->op.read(path, buf, size, off, fi); + else + return -ENOSYS; +} + +int fuse_fs_write(struct fuse_fs *fs, const char *path, const char *buf, + size_t size, off_t off, struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.write) + return fs->op.write(path, buf, size, off, fi); + else + return -ENOSYS; +} + +int fuse_fs_fsync(struct fuse_fs *fs, const char *path, int datasync, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.fsync) + return fs->op.fsync(path, datasync, fi); + else + return -ENOSYS; +} + +int fuse_fs_fsyncdir(struct fuse_fs *fs, const char *path, int datasync, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.fsyncdir) + return fs->op.fsyncdir(path, datasync, fi); + else + return -ENOSYS; +} + +int fuse_fs_flush(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.flush) + return fs->op.flush(path, fi); + else + return -ENOSYS; +} + +int fuse_fs_statfs(struct fuse_fs *fs, const char *path, struct statvfs *buf) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.statfs) + return fs->op.statfs(path, buf); + else { + buf->f_namemax = 255; + buf->f_bsize = 512; + return 0; + } +} + +int fuse_fs_releasedir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.releasedir) + return fs->op.releasedir(path, fi); + else + return 0; +} + +int fuse_fs_readdir(struct fuse_fs *fs, const char *path, void *buf, + fuse_fill_dir_t filler, off_t off, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.readdir) + return fs->op.readdir(path, buf, filler, off, fi); + else + return -ENOSYS; +} + +int fuse_fs_create(struct fuse_fs *fs, const char *path, mode_t mode, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.create) + return fs->op.create(path, mode, fi); + else + return -ENOSYS; +} + +int fuse_fs_lock(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi, int cmd, struct flock *lock) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.lock) + return fs->op.lock(path, fi, cmd, lock); + else + return -ENOSYS; +} + +int fuse_fs_chown(struct fuse_fs *fs, const char *path, uid_t uid, gid_t gid) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.chown) + return fs->op.chown(path, uid, gid); + else + return -ENOSYS; +} + +int fuse_fs_truncate(struct fuse_fs *fs, const char *path, off_t size) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.truncate) + return fs->op.truncate(path, size); + else + return -ENOSYS; +} + +int fuse_fs_ftruncate(struct fuse_fs *fs, const char *path, off_t size, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.ftruncate) + return fs->op.ftruncate(path, size, fi); + else if (fs->op.truncate) + return fs->op.truncate(path, size); + else + return -ENOSYS; +} + +int fuse_fs_utimens(struct fuse_fs *fs, const char *path, + const struct timespec tv[2]) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.utimens) + return fs->op.utimens(path, tv); + else if(fs->op.utime) { + struct utimbuf buf; + buf.actime = tv[0].tv_sec; + buf.modtime = tv[1].tv_sec; + return fs->op.utime(path, &buf); + } else + return -ENOSYS; +} + +int fuse_fs_access(struct fuse_fs *fs, const char *path, int mask) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.access) + return fs->op.access(path, mask); + else + return -ENOSYS; +} + +int fuse_fs_readlink(struct fuse_fs *fs, const char *path, char *buf, + size_t len) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.readlink) + return fs->op.readlink(path, buf, len); + else + return -ENOSYS; +} + +int fuse_fs_mknod(struct fuse_fs *fs, const char *path, mode_t mode, + dev_t rdev) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.mknod) + return fs->op.mknod(path, mode, rdev); + else + return -ENOSYS; +} + +int fuse_fs_mkdir(struct fuse_fs *fs, const char *path, mode_t mode) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.mkdir) + return fs->op.mkdir(path, mode); + else + return -ENOSYS; +} + +int fuse_fs_setxattr(struct fuse_fs *fs, const char *path, const char *name, + const char *value, size_t size, int flags) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.setxattr) + return fs->op.setxattr(path, name, value, size, flags); + else + return -ENOSYS; +} + +int fuse_fs_getxattr(struct fuse_fs *fs, const char *path, const char *name, + char *value, size_t size) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.getxattr) + return fs->op.getxattr(path, name, value, size); + else + return -ENOSYS; +} + +int fuse_fs_listxattr(struct fuse_fs *fs, const char *path, char *list, + size_t size) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.listxattr) + return fs->op.listxattr(path, list, size); + else + return -ENOSYS; +} + +int fuse_fs_bmap(struct fuse_fs *fs, const char *path, size_t blocksize, + uint64_t *idx) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.bmap) + return fs->op.bmap(path, blocksize, idx); + else + return -ENOSYS; +} + +int fuse_fs_removexattr(struct fuse_fs *fs, const char *path, const char *name) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.removexattr) + return fs->op.removexattr(path, name); + else + return -ENOSYS; +} + +int fuse_fs_ioctl(struct fuse_fs *fs, const char *path, int cmd, void *arg, + struct fuse_file_info *fi, unsigned int flags, void *data) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.ioctl) { +/* + if (fs->debug) + fprintf(stderr, "ioctl[%llu] 0x%x flags: 0x%x\n", + (unsigned long long) fi->fh, cmd, flags); +*/ + return fs->op.ioctl(path, cmd, arg, fi, flags, data); + } else + return -ENOSYS; +} + +static int is_open(struct fuse *f, fuse_ino_t dir, const char *name) +{ + struct node *node; + int isopen = 0; + pthread_mutex_lock(&f->lock); + node = lookup_node(f, dir, name); + if (node && node->open_count > 0) + isopen = 1; + pthread_mutex_unlock(&f->lock); + return isopen; +} + +static char *hidden_name(struct fuse *f, fuse_ino_t dir, const char *oldname, + char *newname, size_t bufsize) +{ + struct stat buf; + struct node *node; + struct node *newnode; + char *newpath; + int res; + int failctr = 10; + + do { + pthread_mutex_lock(&f->lock); + node = lookup_node(f, dir, oldname); + if (node == NULL) { + pthread_mutex_unlock(&f->lock); + return NULL; + } + do { + f->hidectr ++; + snprintf(newname, bufsize, ".fuse_hidden%08x%08x", + (unsigned int) node->nodeid, f->hidectr); + newnode = lookup_node(f, dir, newname); + } while(newnode); + pthread_mutex_unlock(&f->lock); + + newpath = get_path_name(f, dir, newname); + if (!newpath) + break; + + res = fuse_fs_getattr(f->fs, newpath, &buf); + if (res == -ENOENT) + break; + free(newpath); + newpath = NULL; + } while(res == 0 && --failctr); + + return newpath; +} + +static int hide_node(struct fuse *f, const char *oldpath, + fuse_ino_t dir, const char *oldname) +{ + char newname[64]; + char *newpath; + int err = -EBUSY; + + newpath = hidden_name(f, dir, oldname, newname, sizeof(newname)); + if (newpath) { + err = fuse_fs_rename(f->fs, oldpath, newpath); + if (!err) + err = rename_node(f, dir, oldname, dir, newname, 1); + free(newpath); + } + return err; +} + +#ifdef __SOLARIS__ + +static int mtime_eq(const struct stat *stbuf, const struct timespec *ts) +{ + return stbuf->st_mtime == ts->tv_sec && ST_MTIM_NSEC(stbuf) == ts->tv_nsec; +} + +#ifndef CLOCK_MONOTONIC +#define CLOCK_MONOTONIC CLOCK_REALTIME +#endif + +static void curr_time(struct timespec *now) +{ + static clockid_t clockid = CLOCK_MONOTONIC; + int res = clock_gettime(clockid, now); + if (res == -1 && errno == EINVAL) { + clockid = CLOCK_REALTIME; + res = clock_gettime(clockid, now); + } + if (res == -1) { + perror("fuse: clock_gettime"); + abort(); + } +} + +static void update_stat(struct node *node, const struct stat *stbuf) +{ + if (node->cache_valid && (!mtime_eq(stbuf, &node->mtime) || + stbuf->st_size != node->size)) + node->cache_valid = 0; + node->mtime.tv_sec = stbuf->st_mtime; + node->mtime.tv_nsec = ST_MTIM_NSEC(stbuf); + node->size = stbuf->st_size; + curr_time(&node->stat_updated); +} + +#endif /* __SOLARIS__ */ + +static int lookup_path(struct fuse *f, fuse_ino_t nodeid, + const char *name, const char *path, + struct fuse_entry_param *e, struct fuse_file_info *fi) +{ + int res; + + memset(e, 0, sizeof(struct fuse_entry_param)); + if (fi) + res = fuse_fs_fgetattr(f->fs, path, &e->attr, fi); + else + res = fuse_fs_getattr(f->fs, path, &e->attr); + if (res == 0) { + struct node *node; + + node = find_node(f, nodeid, name); + if (node == NULL) + res = -ENOMEM; + else { + e->ino = node->nodeid; + e->generation = node->generation; + e->entry_timeout = f->conf.entry_timeout; + e->attr_timeout = f->conf.attr_timeout; +#ifdef __SOLARIS__ + if (f->conf.auto_cache) { + pthread_mutex_lock(&f->lock); + update_stat(node, &e->attr); + pthread_mutex_unlock(&f->lock); + } +#endif /* __SOLARIS__ */ + set_stat(f, e->ino, &e->attr); + if (f->conf.debug) + fprintf(stderr, " NODEID: %lu\n", (unsigned long) e->ino); + } + } + return res; +} + +static struct fuse_context_i *fuse_get_context_internal(void) +{ + struct fuse_context_i *c; + + c = (struct fuse_context_i *) pthread_getspecific(fuse_context_key); + if (c == NULL) { + c = (struct fuse_context_i *) malloc(sizeof(struct fuse_context_i)); + if (c == NULL) { + /* This is hard to deal with properly, so just abort. If + memory is so low that the context cannot be allocated, + there's not much hope for the filesystem anyway */ + fprintf(stderr, "fuse: failed to allocate thread specific data\n"); + abort(); + } + pthread_setspecific(fuse_context_key, c); + } + return c; +} + +static void fuse_freecontext(void *data) +{ + free(data); +} + +static int fuse_create_context_key(void) +{ + int err = 0; + pthread_mutex_lock(&fuse_context_lock); + if (!fuse_context_ref) { + err = pthread_key_create(&fuse_context_key, fuse_freecontext); + if (err) { + fprintf(stderr, "fuse: failed to create thread specific key: %s\n", + strerror(err)); + pthread_mutex_unlock(&fuse_context_lock); + return -1; + } + } + fuse_context_ref++; + pthread_mutex_unlock(&fuse_context_lock); + return 0; +} + +static void fuse_delete_context_key(void) +{ + pthread_mutex_lock(&fuse_context_lock); + fuse_context_ref--; + if (!fuse_context_ref) { + free(pthread_getspecific(fuse_context_key)); + pthread_key_delete(fuse_context_key); + } + pthread_mutex_unlock(&fuse_context_lock); +} + +static struct fuse *req_fuse_prepare(fuse_req_t req) +{ + struct fuse_context_i *c = fuse_get_context_internal(); + const struct fuse_ctx *ctx = fuse_req_ctx(req); + c->req = req; + c->ctx.fuse = req_fuse(req); + c->ctx.uid = ctx->uid; + c->ctx.gid = ctx->gid; + c->ctx.pid = ctx->pid; +#ifdef POSIXACLS + c->ctx.umask = ctx->umask; +#endif + return c->ctx.fuse; +} + +#ifndef __SOLARIS__ +static void reply_err(fuse_req_t req, int err) +#else /* __SOLARIS__ */ +static inline void reply_err(fuse_req_t req, int err) +#endif /* __SOLARIS__ */ +{ + /* fuse_reply_err() uses non-negated errno values */ + fuse_reply_err(req, -err); +} + +static void reply_entry(fuse_req_t req, const struct fuse_entry_param *e, + int err) +{ + if (!err) { + struct fuse *f = req_fuse(req); +#ifdef __SOLARIS__ + /* Skip forget for negative result */ + if ((fuse_reply_entry(req, e) == -ENOENT) + && (e->ino != 0)) + forget_node(f, e->ino, 1); +#else /* __SOLARIS__ */ + if (fuse_reply_entry(req, e) == -ENOENT) + forget_node(f, e->ino, 1); +#endif + } else + reply_err(req, err); +} + +void fuse_fs_init(struct fuse_fs *fs, struct fuse_conn_info *conn) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.init) + fs->user_data = fs->op.init(conn); +} + +static void fuse_lib_init(void *data, struct fuse_conn_info *conn) +{ + struct fuse *f = (struct fuse *) data; + struct fuse_context_i *c = fuse_get_context_internal(); + + memset(c, 0, sizeof(*c)); + c->ctx.fuse = f; + fuse_fs_init(f->fs, conn); +} + +void fuse_fs_destroy(struct fuse_fs *fs) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.destroy) + fs->op.destroy(fs->user_data); +#ifdef __SOLARIS__ + if (fs->m) + fuse_put_module(fs->m); +#endif /* __SOLARIS__ */ + free(fs); +} + +static void fuse_lib_destroy(void *data) +{ + struct fuse *f = (struct fuse *) data; + struct fuse_context_i *c = fuse_get_context_internal(); + + memset(c, 0, sizeof(*c)); + c->ctx.fuse = f; + fuse_fs_destroy(f->fs); + f->fs = NULL; +} + +static void fuse_lib_lookup(fuse_req_t req, fuse_ino_t parent, + const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path_name(f, parent, name); + if (path != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "LOOKUP %s\n", path); + fuse_prepare_interrupt(f, req, &d); + err = lookup_path(f, parent, name, path, &e, NULL); + if (err == -ENOENT && f->conf.negative_timeout != 0.0) { + e.ino = 0; + e.entry_timeout = f->conf.negative_timeout; + err = 0; + } + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_entry(req, &e, err); +} + +static void fuse_lib_forget(fuse_req_t req, fuse_ino_t ino, + unsigned long nlookup) +{ + struct fuse *f = req_fuse(req); + if (f->conf.debug) + fprintf(stderr, "FORGET %llu/%lu\n", (unsigned long long)ino, nlookup); + forget_node(f, ino, nlookup); + fuse_reply_none(req); +} + +static void fuse_lib_getattr(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct stat buf; + char *path; + int err; + + (void) fi; + memset(&buf, 0, sizeof(buf)); + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_getattr(f->fs, path, &buf); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + if (!err) { +#ifdef __SOLARIS__ + if (f->conf.auto_cache) { + pthread_mutex_lock(&f->lock); + update_stat(get_node(f, ino), &buf); + pthread_mutex_unlock(&f->lock); + } +#endif /* __SOLARIS__ */ + set_stat(f, ino, &buf); + fuse_reply_attr(req, &buf, f->conf.attr_timeout); + } else + reply_err(req, err); +} + +int fuse_fs_chmod(struct fuse_fs *fs, const char *path, mode_t mode) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.chmod) + return fs->op.chmod(path, mode); + else + return -ENOSYS; +} + +static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, + int valid, struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct stat buf; + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = 0; + if (!err && (valid & FUSE_SET_ATTR_MODE)) + err = fuse_fs_chmod(f->fs, path, attr->st_mode); + if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID))) { + uid_t uid = + (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t) -1; + gid_t gid = + (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t) -1; + err = fuse_fs_chown(f->fs, path, uid, gid); + } + if (!err && (valid & FUSE_SET_ATTR_SIZE)) { + if (fi) + err = fuse_fs_ftruncate(f->fs, path, attr->st_size, fi); + else + err = fuse_fs_truncate(f->fs, path, attr->st_size); + } +#ifdef HAVE_UTIMENSAT + if (!err && + (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME))) { + struct timespec tv[2]; + + tv[0].tv_sec = 0; + tv[1].tv_sec = 0; + tv[0].tv_nsec = UTIME_OMIT; + tv[1].tv_nsec = UTIME_OMIT; + + if (valid & FUSE_SET_ATTR_ATIME_NOW) + tv[0].tv_nsec = UTIME_NOW; + else if (valid & FUSE_SET_ATTR_ATIME) + tv[0] = attr->st_atim; + + if (valid & FUSE_SET_ATTR_MTIME_NOW) + tv[1].tv_nsec = UTIME_NOW; + else if (valid & FUSE_SET_ATTR_MTIME) + tv[1] = attr->st_mtim; + + err = fuse_fs_utimens(f->fs, path, tv); + } else +#endif + if (!err && (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) == + (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) { + struct timespec tv[2]; + tv[0].tv_sec = attr->st_atime; + tv[0].tv_nsec = ST_ATIM_NSEC(attr); + tv[1].tv_sec = attr->st_mtime; + tv[1].tv_nsec = ST_MTIM_NSEC(attr); + err = fuse_fs_utimens(f->fs, path, tv); + } + if (!err) + err = fuse_fs_getattr(f->fs, path, &buf); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + if (!err) { +#ifdef __SOLARIS__ + if (f->conf.auto_cache) { + pthread_mutex_lock(&f->lock); + update_stat(get_node(f, ino), &buf); + pthread_mutex_unlock(&f->lock); + } +#endif /* __SOLARIS__ */ + set_stat(f, ino, &buf); + fuse_reply_attr(req, &buf, f->conf.attr_timeout); + } else + reply_err(req, err); +} + +static void fuse_lib_access(fuse_req_t req, fuse_ino_t ino, int mask) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "ACCESS %s 0%o\n", path, mask); + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_access(f->fs, path, mask); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_err(req, err); +} + +static void fuse_lib_readlink(fuse_req_t req, fuse_ino_t ino) +{ + struct fuse *f = req_fuse_prepare(req); + char linkname[PATH_MAX + 1]; + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_readlink(f->fs, path, linkname, sizeof(linkname)); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + if (!err) { + linkname[PATH_MAX] = '\0'; + fuse_reply_readlink(req, linkname); + } else + reply_err(req, err); +} + +static void fuse_lib_mknod(fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, dev_t rdev) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path_name(f, parent, name); + if (path) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "MKNOD %s\n", path); + fuse_prepare_interrupt(f, req, &d); + err = -ENOSYS; + if (S_ISREG(mode)) { + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = O_CREAT | O_EXCL | O_WRONLY; + err = fuse_fs_create(f->fs, path, mode, &fi); + if (!err) { + err = lookup_path(f, parent, name, path, &e, &fi); + fuse_fs_release(f->fs, path, &fi); + } + } + if (err == -ENOSYS) { + err = fuse_fs_mknod(f->fs, path, mode, rdev); + if (!err) + err = lookup_path(f, parent, name, path, &e, NULL); + } + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_entry(req, &e, err); +} + +static void fuse_lib_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path_name(f, parent, name); + if (path != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "MKDIR %s\n", path); + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_mkdir(f->fs, path, mode); + if (!err) + err = lookup_path(f, parent, name, path, &e, NULL); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_entry(req, &e, err); +} + +static void fuse_lib_unlink(fuse_req_t req, fuse_ino_t parent, + const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_wrlock(&f->tree_lock); + path = get_path_name(f, parent, name); + if (path != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "UNLINK %s\n", path); + fuse_prepare_interrupt(f, req, &d); + if (!f->conf.hard_remove && is_open(f, parent, name)) + err = hide_node(f, path, parent, name); + else { + err = fuse_fs_unlink(f->fs, path); + if (!err) + remove_node(f, parent, name); + } + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_err(req, err); +} + +static void fuse_lib_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_wrlock(&f->tree_lock); + path = get_path_name(f, parent, name); + if (path != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "RMDIR %s\n", path); + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_rmdir(f->fs, path); + fuse_finish_interrupt(f, req, &d); + if (!err) + remove_node(f, parent, name); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_err(req, err); +} + +static void fuse_lib_symlink(fuse_req_t req, const char *linkname, + fuse_ino_t parent, const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path_name(f, parent, name); + if (path != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "SYMLINK %s\n", path); + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_symlink(f->fs, linkname, path); + if (!err) + err = lookup_path(f, parent, name, path, &e, NULL); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_entry(req, &e, err); +} + +static void fuse_lib_rename(fuse_req_t req, fuse_ino_t olddir, + const char *oldname, fuse_ino_t newdir, + const char *newname) +{ + struct fuse *f = req_fuse_prepare(req); + char *oldpath; + char *newpath; + int err; + + err = -ENOENT; + pthread_rwlock_wrlock(&f->tree_lock); + oldpath = get_path_name(f, olddir, oldname); + if (oldpath != NULL) { + newpath = get_path_name(f, newdir, newname); + if (newpath != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "RENAME %s -> %s\n", oldpath, newpath); + err = 0; + fuse_prepare_interrupt(f, req, &d); + if (!f->conf.hard_remove && is_open(f, newdir, newname)) + err = hide_node(f, newpath, newdir, newname); + if (!err) { + err = fuse_fs_rename(f->fs, oldpath, newpath); + if (!err) + err = rename_node(f, olddir, oldname, newdir, newname, 0); + } + fuse_finish_interrupt(f, req, &d); + free(newpath); + } + free(oldpath); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_err(req, err); +} + +static void fuse_lib_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, + const char *newname) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *oldpath; + char *newpath; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + oldpath = get_path(f, ino); + if (oldpath != NULL) { + newpath = get_path_name(f, newparent, newname); + if (newpath != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "LINK %s\n", newpath); + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_link(f->fs, oldpath, newpath); + if (!err) + err = lookup_path(f, newparent, newname, newpath, &e, NULL); + fuse_finish_interrupt(f, req, &d); + free(newpath); + } + free(oldpath); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_entry(req, &e, err); +} + +static void fuse_do_release(struct fuse *f, fuse_ino_t ino, const char *path, + struct fuse_file_info *fi) +{ + struct node *node; + int unlink_hidden = 0; + + fuse_fs_release(f->fs, path ? path : "-", fi); + + pthread_mutex_lock(&f->lock); + node = get_node(f, ino); + assert(node->open_count > 0); + --node->open_count; + if (node->is_hidden && !node->open_count) { + unlink_hidden = 1; + node->is_hidden = 0; + } + pthread_mutex_unlock(&f->lock); + + if(unlink_hidden && path) + fuse_fs_unlink(f->fs, path); +} + +static void fuse_lib_create(fuse_req_t req, fuse_ino_t parent, + const char *name, mode_t mode, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + struct fuse_entry_param e; + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path_name(f, parent, name); + if (path) { + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_create(f->fs, path, mode, fi); + if (!err) { + err = lookup_path(f, parent, name, path, &e, fi); + if (err) + fuse_fs_release(f->fs, path, fi); + else if (!S_ISREG(e.attr.st_mode)) { + err = -EIO; + fuse_fs_release(f->fs, path, fi); + forget_node(f, e.ino, 1); + } else { + if (f->conf.direct_io) + fi->direct_io = 1; + if (f->conf.kernel_cache) + fi->keep_cache = 1; + + } + } + fuse_finish_interrupt(f, req, &d); + } + if (!err) { + pthread_mutex_lock(&f->lock); + get_node(f, e.ino)->open_count++; + pthread_mutex_unlock(&f->lock); + if (fuse_reply_create(req, &e, fi) == -ENOENT) { + /* The open syscall was interrupted, so it must be cancelled */ + fuse_prepare_interrupt(f, req, &d); + fuse_do_release(f, e.ino, path, fi); + fuse_finish_interrupt(f, req, &d); + forget_node(f, e.ino, 1); + } else if (f->conf.debug) { + fprintf(stderr, " CREATE[%llu] flags: 0x%x %s\n", + (unsigned long long) fi->fh, fi->flags, path); + } + } else + reply_err(req, err); + + if (path) + free(path); + + pthread_rwlock_unlock(&f->tree_lock); +} + +#ifdef __SOLARIS__ + +static double diff_timespec(const struct timespec *t1, + const struct timespec *t2) +{ + return (t1->tv_sec - t2->tv_sec) + + ((double) t1->tv_nsec - (double) t2->tv_nsec) / 1000000000.0; +} + +static void open_auto_cache(struct fuse *f, fuse_ino_t ino, const char *path, + struct fuse_file_info *fi) +{ + struct node *node; + + pthread_mutex_lock(&f->lock); + node = get_node(f, ino); + if (node->cache_valid) { + struct timespec now; + + curr_time(&now); + if (diff_timespec(&now, &node->stat_updated) > f->conf.ac_attr_timeout) { + struct stat stbuf; + int err; + pthread_mutex_unlock(&f->lock); + err = fuse_fs_fgetattr(f->fs, path, &stbuf, fi); + pthread_mutex_lock(&f->lock); + if (!err) + update_stat(node, &stbuf); + else + node->cache_valid = 0; + } + } + if (node->cache_valid) + fi->keep_cache = 1; + + node->cache_valid = 1; + pthread_mutex_unlock(&f->lock); +} + +#endif /* __SOLARIS__ */ + +static void fuse_lib_open(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path = NULL; + int err = 0; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path) { + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_open(f->fs, path, fi); + if (!err) { + if (f->conf.direct_io) + fi->direct_io = 1; + if (f->conf.kernel_cache) + fi->keep_cache = 1; +#ifdef __SOLARIS__ + + if (f->conf.auto_cache) + open_auto_cache(f, ino, path, fi); +#endif /* __SOLARIS__ */ + } + fuse_finish_interrupt(f, req, &d); + } + if (!err) { + pthread_mutex_lock(&f->lock); + get_node(f, ino)->open_count++; + pthread_mutex_unlock(&f->lock); + if (fuse_reply_open(req, fi) == -ENOENT) { + /* The open syscall was interrupted, so it must be cancelled */ + fuse_prepare_interrupt(f, req, &d); + fuse_do_release(f, ino, path, fi); + fuse_finish_interrupt(f, req, &d); + } else if (f->conf.debug) { + fprintf(stderr, "OPEN[%llu] flags: 0x%x %s\n", + (unsigned long long) fi->fh, fi->flags, path); + } + } else + reply_err(req, err); + + if (path) + free(path); + pthread_rwlock_unlock(&f->tree_lock); +} + +static void fuse_lib_read(fuse_req_t req, fuse_ino_t ino, size_t size, + off_t off, struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + char *buf; + int res; + + buf = (char *) malloc(size); + if (buf == NULL) { + reply_err(req, -ENOMEM); + return; + } + + res = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "READ[%llu] %lu bytes from %llu\n", + (unsigned long long) fi->fh, (unsigned long) size, + (unsigned long long) off); + + fuse_prepare_interrupt(f, req, &d); + res = fuse_fs_read(f->fs, path, buf, size, off, fi); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + + if (res >= 0) { + if (f->conf.debug) + fprintf(stderr, " READ[%llu] %u bytes\n", + (unsigned long long)fi->fh, res); + if ((size_t) res > size) + fprintf(stderr, "fuse: read too many bytes"); + fuse_reply_buf(req, buf, res); + } else + reply_err(req, res); + + free(buf); +} + +static void fuse_lib_write(fuse_req_t req, fuse_ino_t ino, const char *buf, + size_t size, off_t off, struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int res; + + res = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "WRITE%s[%llu] %lu bytes to %llu\n", + fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh, + (unsigned long) size, (unsigned long long) off); + + fuse_prepare_interrupt(f, req, &d); + res = fuse_fs_write(f->fs, path, buf, size, off, fi); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + + if (res >= 0) { + if (f->conf.debug) + fprintf(stderr, " WRITE%s[%llu] %u bytes\n", + fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh, + res); + if ((size_t) res > size) + fprintf(stderr, "fuse: wrote too many bytes"); + fuse_reply_write(req, res); + } else + reply_err(req, res); +} + +static void fuse_lib_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "FSYNC[%llu]\n", (unsigned long long) fi->fh); + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_fsync(f->fs, path, datasync, fi); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_err(req, err); +} + +static struct fuse_dh *get_dirhandle(const struct fuse_file_info *llfi, + struct fuse_file_info *fi) +{ + struct fuse_dh *dh = (struct fuse_dh *) (uintptr_t) llfi->fh; + memset(fi, 0, sizeof(struct fuse_file_info)); + fi->fh = dh->fh; + fi->fh_old = dh->fh; + return dh; +} + +static void fuse_lib_opendir(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *llfi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + struct fuse_dh *dh; + struct fuse_file_info fi; + char *path; + int err; + + dh = (struct fuse_dh *) malloc(sizeof(struct fuse_dh)); + if (dh == NULL) { + reply_err(req, -ENOMEM); + return; + } + memset(dh, 0, sizeof(struct fuse_dh)); + dh->fuse = f; + dh->contents = NULL; + dh->len = 0; + dh->filled = 0; + dh->nodeid = ino; + fuse_mutex_init(&dh->lock); + + llfi->fh = (uintptr_t) dh; + + memset(&fi, 0, sizeof(fi)); + fi.flags = llfi->flags; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_opendir(f->fs, path, &fi); + fuse_finish_interrupt(f, req, &d); + dh->fh = fi.fh; + } + if (!err) { + if (fuse_reply_open(req, llfi) == -ENOENT) { + /* The opendir syscall was interrupted, so it must be cancelled */ + fuse_prepare_interrupt(f, req, &d); + fuse_fs_releasedir(f->fs, path, &fi); + fuse_finish_interrupt(f, req, &d); + pthread_mutex_destroy(&dh->lock); + free(dh); + } + } else { + reply_err(req, err); + pthread_mutex_destroy(&dh->lock); + free(dh); + } + free(path); + pthread_rwlock_unlock(&f->tree_lock); +} + +static int extend_contents(struct fuse_dh *dh, unsigned minsize) +{ + if (minsize > dh->size) { + char *newptr; + unsigned newsize = dh->size; + if (!newsize) + newsize = 1024; +#ifndef __SOLARIS__ + while (newsize < minsize) { + if (newsize >= 0x80000000) + newsize = 0xffffffff; + else + newsize *= 2; + } +#else /* __SOLARIS__ */ + while (newsize < minsize) + newsize *= 2; +#endif /* __SOLARIS__ */ + + newptr = (char *) realloc(dh->contents, newsize); + if (!newptr) { + dh->error = -ENOMEM; + return -1; + } + dh->contents = newptr; + dh->size = newsize; + } + return 0; +} + +static int fill_dir(void *dh_, const char *name, const struct stat *statp, + off_t off) +{ + struct fuse_dh *dh = (struct fuse_dh *) dh_; + struct stat stbuf; + size_t newlen; + + if (statp) + stbuf = *statp; + else { + memset(&stbuf, 0, sizeof(stbuf)); + stbuf.st_ino = FUSE_UNKNOWN_INO; + } + + if (!dh->fuse->conf.use_ino) { + stbuf.st_ino = FUSE_UNKNOWN_INO; + if (dh->fuse->conf.readdir_ino) { + struct node *node; + pthread_mutex_lock(&dh->fuse->lock); + node = lookup_node(dh->fuse, dh->nodeid, name); + if (node) + stbuf.st_ino = (ino_t) node->nodeid; + pthread_mutex_unlock(&dh->fuse->lock); + } + } + + if (off) { + if (extend_contents(dh, dh->needlen) == -1) + return 1; + + dh->filled = 0; + newlen = dh->len + fuse_add_direntry(dh->req, dh->contents + dh->len, + dh->needlen - dh->len, name, + &stbuf, off); + if (newlen > dh->needlen) + return 1; + } else { + newlen = dh->len + fuse_add_direntry(dh->req, NULL, 0, name, NULL, 0); + if (extend_contents(dh, newlen) == -1) + return 1; + + fuse_add_direntry(dh->req, dh->contents + dh->len, dh->size - dh->len, + name, &stbuf, newlen); + } + dh->len = newlen; + return 0; +} + +static int readdir_fill(struct fuse *f, fuse_req_t req, fuse_ino_t ino, + size_t size, off_t off, struct fuse_dh *dh, + struct fuse_file_info *fi) +{ + int err = -ENOENT; + char *path; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + + dh->len = 0; + dh->error = 0; + dh->needlen = size; + dh->filled = 1; + dh->req = req; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_readdir(f->fs, path, dh, fill_dir, off, fi); + fuse_finish_interrupt(f, req, &d); + dh->req = NULL; + if (!err) + err = dh->error; + if (err) + dh->filled = 0; + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + return err; +} + +static void fuse_lib_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, + off_t off, struct fuse_file_info *llfi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_file_info fi; + struct fuse_dh *dh = get_dirhandle(llfi, &fi); + + pthread_mutex_lock(&dh->lock); + /* According to SUS, directory contents need to be refreshed on + rewinddir() */ + if (!off) + dh->filled = 0; + + if (!dh->filled) { + int err = readdir_fill(f, req, ino, size, off, dh, &fi); + if (err) { + reply_err(req, err); + goto out; + } + } + if (dh->filled) { + if ((off >= 0) && (off < dh->len)) { + if (off + size > dh->len) + size = dh->len - off; + } else + size = 0; + } else { + size = dh->len; + off = 0; + } + fuse_reply_buf(req, dh->contents + off, size); + out: + pthread_mutex_unlock(&dh->lock); +} + +static void fuse_lib_releasedir(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *llfi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + struct fuse_file_info fi; + struct fuse_dh *dh = get_dirhandle(llfi, &fi); + char *path; + + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + fuse_prepare_interrupt(f, req, &d); + fuse_fs_releasedir(f->fs, path ? path : "-", &fi); + fuse_finish_interrupt(f, req, &d); + if (path) + free(path); + pthread_rwlock_unlock(&f->tree_lock); + pthread_mutex_lock(&dh->lock); + pthread_mutex_unlock(&dh->lock); + pthread_mutex_destroy(&dh->lock); + free(dh->contents); + free(dh); + reply_err(req, 0); +} + +static void fuse_lib_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *llfi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_file_info fi; + char *path; + int err; + + get_dirhandle(llfi, &fi); + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_fsyncdir(f->fs, path, datasync, &fi); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_err(req, err); +} + +static void fuse_lib_statfs(fuse_req_t req, fuse_ino_t ino) +{ + struct fuse *f = req_fuse_prepare(req); + struct statvfs buf; + char *path; + int err; + + memset(&buf, 0, sizeof(buf)); + pthread_rwlock_rdlock(&f->tree_lock); + if (!ino) { + err = -ENOMEM; + path = strdup("/"); + } else { + err = -ENOENT; + path = get_path(f, ino); + } + if (path) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_statfs(f->fs, path, &buf); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + + if (!err) + fuse_reply_statfs(req, &buf); + else + reply_err(req, err); +} + +static void fuse_lib_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, + const char *value, size_t size, int flags) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_setxattr(f->fs, path, name, value, size, flags); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_err(req, err); +} + +static int common_getxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino, + const char *name, char *value, size_t size) +{ + int err; + char *path; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_getxattr(f->fs, path, name, value, size); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + return err; +} + +static void fuse_lib_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, + size_t size) +{ + struct fuse *f = req_fuse_prepare(req); + int res; + + if (size) { + char *value = (char *) malloc(size); + if (value == NULL) { + reply_err(req, -ENOMEM); + return; + } + res = common_getxattr(f, req, ino, name, value, size); + if (res > 0) + fuse_reply_buf(req, value, res); + else + reply_err(req, res); + free(value); + } else { + res = common_getxattr(f, req, ino, name, NULL, 0); + if (res >= 0) + fuse_reply_xattr(req, res); + else + reply_err(req, res); + } +} + +static int common_listxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino, + char *list, size_t size) +{ + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_listxattr(f->fs, path, list, size); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + return err; +} + +static void fuse_lib_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) +{ + struct fuse *f = req_fuse_prepare(req); + int res; + + if (size) { + char *list = (char *) malloc(size); + if (list == NULL) { + reply_err(req, -ENOMEM); + return; + } + res = common_listxattr(f, req, ino, list, size); + if (res > 0) + fuse_reply_buf(req, list, res); + else + reply_err(req, res); + free(list); + } else { + res = common_listxattr(f, req, ino, NULL, 0); + if (res >= 0) + fuse_reply_xattr(req, res); + else + reply_err(req, res); + } +} + +static void fuse_lib_removexattr(fuse_req_t req, fuse_ino_t ino, + const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_removexattr(f->fs, path, name); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + reply_err(req, err); +} + +static struct lock *locks_conflict(struct node *node, const struct lock *lock) +{ + struct lock *l; + + for (l = node->locks; l; l = l->next) + if (l->owner != lock->owner && + lock->start <= l->end && l->start <= lock->end && + (l->type == F_WRLCK || lock->type == F_WRLCK)) + break; + + return l; +} + +static void delete_lock(struct lock **lockp) +{ + struct lock *l = *lockp; + *lockp = l->next; + free(l); +} + +static void insert_lock(struct lock **pos, struct lock *lock) +{ + lock->next = *pos; + *pos = lock; +} + +static int locks_insert(struct node *node, struct lock *lock) +{ + struct lock **lp; + struct lock *newl1 = NULL; + struct lock *newl2 = NULL; + + if (lock->type != F_UNLCK || lock->start != 0 || lock->end != OFFSET_MAX) { + newl1 = malloc(sizeof(struct lock)); + newl2 = malloc(sizeof(struct lock)); + + if (!newl1 || !newl2) { + free(newl1); + free(newl2); + return -ENOLCK; + } + } + + for (lp = &node->locks; *lp;) { + struct lock *l = *lp; + if (l->owner != lock->owner) + goto skip; + + if (lock->type == l->type) { + if (l->end < lock->start - 1) + goto skip; + if (lock->end < l->start - 1) + break; + if (l->start <= lock->start && lock->end <= l->end) + goto out; + if (l->start < lock->start) + lock->start = l->start; + if (lock->end < l->end) + lock->end = l->end; + goto delete; + } else { + if (l->end < lock->start) + goto skip; + if (lock->end < l->start) + break; + if (lock->start <= l->start && l->end <= lock->end) + goto delete; + if (l->end <= lock->end) { + l->end = lock->start - 1; + goto skip; + } + if (lock->start <= l->start) { + l->start = lock->end + 1; + break; + } + *newl2 = *l; + newl2->start = lock->end + 1; + l->end = lock->start - 1; + insert_lock(&l->next, newl2); + newl2 = NULL; + } + skip: + lp = &l->next; + continue; + + delete: + delete_lock(lp); + } + if (lock->type != F_UNLCK) { + *newl1 = *lock; + insert_lock(lp, newl1); + newl1 = NULL; + } +out: + free(newl1); + free(newl2); + return 0; +} + +static void flock_to_lock(struct flock *flock, struct lock *lock) +{ + memset(lock, 0, sizeof(struct lock)); + lock->type = flock->l_type; + lock->start = flock->l_start; + lock->end = flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX; + lock->pid = flock->l_pid; +} + +static void lock_to_flock(struct lock *lock, struct flock *flock) +{ + flock->l_type = lock->type; + flock->l_start = lock->start; + flock->l_len = (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1; + flock->l_pid = lock->pid; +} + +static int fuse_flush_common(struct fuse *f, fuse_req_t req, fuse_ino_t ino, + const char *path, struct fuse_file_info *fi) +{ + struct fuse_intr_data d; + struct flock lock; + struct lock l; + int err; + int errlock; + + fuse_prepare_interrupt(f, req, &d); + memset(&lock, 0, sizeof(lock)); + lock.l_type = F_UNLCK; + lock.l_whence = SEEK_SET; + err = fuse_fs_flush(f->fs, path, fi); + errlock = fuse_fs_lock(f->fs, path, fi, F_SETLK, &lock); + fuse_finish_interrupt(f, req, &d); + + if (errlock != -ENOSYS) { + flock_to_lock(&lock, &l); + l.owner = fi->lock_owner; + pthread_mutex_lock(&f->lock); + locks_insert(get_node(f, ino), &l); + pthread_mutex_unlock(&f->lock); + + /* if op.lock() is defined FLUSH is needed regardless of op.flush() */ + if (err == -ENOSYS) + err = 0; + } + return err; +} + +static void fuse_lib_release(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path; + int err = 0; + + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (f->conf.debug) + fprintf(stderr, "RELEASE%s[%llu] flags: 0x%x\n", + fi->flush ? "+FLUSH" : "", + (unsigned long long) fi->fh, fi->flags); + + if (fi->flush) { + err = fuse_flush_common(f, req, ino, path, fi); + if (err == -ENOSYS) + err = 0; + } + + fuse_prepare_interrupt(f, req, &d); + fuse_do_release(f, ino, path, fi); + fuse_finish_interrupt(f, req, &d); + free(path); + pthread_rwlock_unlock(&f->tree_lock); + + reply_err(req, err); +} + +static void fuse_lib_flush(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path && f->conf.debug) + fprintf(stderr, "FLUSH[%llu]\n", (unsigned long long) fi->fh); + err = fuse_flush_common(f, req, ino, path, fi); + free(path); + pthread_rwlock_unlock(&f->tree_lock); + reply_err(req, err); +} + +static int fuse_lock_common(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct flock *lock, + int cmd) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_lock(f->fs, path, fi, cmd, lock); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + return err; +} + +static void fuse_lib_getlk(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct flock *lock) +{ + int err; + struct lock l; + struct lock *conflict; + struct fuse *f = req_fuse(req); + + flock_to_lock(lock, &l); + l.owner = fi->lock_owner; + pthread_mutex_lock(&f->lock); + conflict = locks_conflict(get_node(f, ino), &l); + if (conflict) + lock_to_flock(conflict, lock); + pthread_mutex_unlock(&f->lock); + if (!conflict) + err = fuse_lock_common(req, ino, fi, lock, F_GETLK); + else + err = 0; + + if (!err) + fuse_reply_lock(req, lock); + else + reply_err(req, err); +} + +static void fuse_lib_setlk(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct flock *lock, + int should_sleep) +{ + int err = fuse_lock_common(req, ino, fi, lock, should_sleep ? F_SETLKW : F_SETLK); + if (!err) { + struct fuse *f = req_fuse(req); + struct lock l; + flock_to_lock(lock, &l); + l.owner = fi->lock_owner; + pthread_mutex_lock(&f->lock); + locks_insert(get_node(f, ino), &l); + pthread_mutex_unlock(&f->lock); + } + reply_err(req, err); +} + +static void fuse_lib_bmap(fuse_req_t req, fuse_ino_t ino, size_t blocksize, + uint64_t idx) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path; + int err; + + err = -ENOENT; + pthread_rwlock_rdlock(&f->tree_lock); + path = get_path(f, ino); + if (path != NULL) { + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_bmap(f->fs, path, blocksize, &idx); + fuse_finish_interrupt(f, req, &d); + free(path); + } + pthread_rwlock_unlock(&f->tree_lock); + if (!err) + fuse_reply_bmap(req, idx); + else + reply_err(req, err); +} + +static void fuse_lib_ioctl(fuse_req_t req, fuse_ino_t ino, int cmd, void *arg, + struct fuse_file_info *llfi, unsigned int flags, + const void *in_buf, size_t in_bufsz, + size_t out_bufsz) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + struct fuse_file_info fi; + char *path, *out_buf = NULL; + int err; + + err = -EPERM; + if (flags & FUSE_IOCTL_UNRESTRICTED) + goto err; + + if (flags & FUSE_IOCTL_DIR) + get_dirhandle(llfi, &fi); + else + fi = *llfi; + + if (out_bufsz) { + err = -ENOMEM; + out_buf = malloc(out_bufsz); + if (!out_buf) + goto err; + } + + assert(!in_bufsz || !out_bufsz || in_bufsz == out_bufsz); + if (out_buf) + memcpy(out_buf, in_buf, in_bufsz); + + path = get_path(f, ino); /* Should be get_path_nullok() */ + if (!path) { + err = ENOENT; + goto err; + } + + fuse_prepare_interrupt(f, req, &d); + + /* Note : const qualifier dropped */ + err = fuse_fs_ioctl(f->fs, path, cmd, arg, &fi, flags, + out_buf ? (void*)out_buf : (void*)(uintptr_t)in_buf); + + fuse_finish_interrupt(f, req, &d); + free(path); + + if (err >= 0) { /* not an error */ + fuse_reply_ioctl(req, err, out_buf, out_bufsz); + goto out; + } +err: + reply_err(req, err); +out: + free(out_buf); +} + +static struct fuse_lowlevel_ops fuse_path_ops = { + .init = fuse_lib_init, + .destroy = fuse_lib_destroy, + .lookup = fuse_lib_lookup, + .forget = fuse_lib_forget, + .getattr = fuse_lib_getattr, + .setattr = fuse_lib_setattr, + .access = fuse_lib_access, + .readlink = fuse_lib_readlink, + .mknod = fuse_lib_mknod, + .mkdir = fuse_lib_mkdir, + .unlink = fuse_lib_unlink, + .rmdir = fuse_lib_rmdir, + .symlink = fuse_lib_symlink, + .rename = fuse_lib_rename, + .link = fuse_lib_link, + .create = fuse_lib_create, + .open = fuse_lib_open, + .read = fuse_lib_read, + .write = fuse_lib_write, + .flush = fuse_lib_flush, + .release = fuse_lib_release, + .fsync = fuse_lib_fsync, + .opendir = fuse_lib_opendir, + .readdir = fuse_lib_readdir, + .releasedir = fuse_lib_releasedir, + .fsyncdir = fuse_lib_fsyncdir, + .statfs = fuse_lib_statfs, + .setxattr = fuse_lib_setxattr, + .getxattr = fuse_lib_getxattr, + .listxattr = fuse_lib_listxattr, + .removexattr = fuse_lib_removexattr, + .getlk = fuse_lib_getlk, + .setlk = fuse_lib_setlk, + .bmap = fuse_lib_bmap, + .ioctl = fuse_lib_ioctl, +}; + +struct fuse_session *fuse_get_session(struct fuse *f) +{ + return f->se; +} + +int fuse_loop(struct fuse *f) +{ + if (f) + return fuse_session_loop(f->se); + else + return -1; +} + +void fuse_exit(struct fuse *f) +{ + fuse_session_exit(f->se); +} + +struct fuse_context *fuse_get_context(void) +{ + return &fuse_get_context_internal()->ctx; +} + +int fuse_interrupted(void) +{ + return fuse_req_interrupted(fuse_get_context_internal()->req); +} + +enum { + KEY_HELP, +}; + +#define FUSE_LIB_OPT(t, p, v) { t, offsetof(struct fuse_config, p), v } + +static const struct fuse_opt fuse_lib_opts[] = { + FUSE_OPT_KEY("-h", KEY_HELP), + FUSE_OPT_KEY("--help", KEY_HELP), + FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP), + FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP), + FUSE_LIB_OPT("debug", debug, 1), + FUSE_LIB_OPT("-d", debug, 1), + FUSE_LIB_OPT("hard_remove", hard_remove, 1), + FUSE_LIB_OPT("use_ino", use_ino, 1), + FUSE_LIB_OPT("readdir_ino", readdir_ino, 1), + FUSE_LIB_OPT("direct_io", direct_io, 1), + FUSE_LIB_OPT("kernel_cache", kernel_cache, 1), +#ifdef __SOLARIS__ + FUSE_LIB_OPT("auto_cache", auto_cache, 1), + FUSE_LIB_OPT("noauto_cache", auto_cache, 0), +#endif /* __SOLARIS__ */ + FUSE_LIB_OPT("umask=", set_mode, 1), + FUSE_LIB_OPT("umask=%o", umask, 0), + FUSE_LIB_OPT("uid=", set_uid, 1), + FUSE_LIB_OPT("uid=%d", uid, 0), + FUSE_LIB_OPT("gid=", set_gid, 1), + FUSE_LIB_OPT("gid=%d", gid, 0), + FUSE_LIB_OPT("entry_timeout=%lf", entry_timeout, 0), + FUSE_LIB_OPT("attr_timeout=%lf", attr_timeout, 0), + FUSE_LIB_OPT("ac_attr_timeout=%lf", ac_attr_timeout, 0), + FUSE_LIB_OPT("ac_attr_timeout=", ac_attr_timeout_set, 1), + FUSE_LIB_OPT("negative_timeout=%lf", negative_timeout, 0), + FUSE_LIB_OPT("intr", intr, 1), + FUSE_LIB_OPT("intr_signal=%d", intr_signal, 0), +#ifdef __SOLARIS__ + FUSE_LIB_OPT("modules=%s", modules, 0), +#endif /* __SOLARIS__ */ + FUSE_OPT_END +}; + +static void fuse_lib_help(void) +{ + fprintf(stderr, +" -o hard_remove immediate removal (don't hide files)\n" +" -o use_ino let filesystem set inode numbers\n" +" -o readdir_ino try to fill in d_ino in readdir\n" +" -o direct_io use direct I/O\n" +" -o kernel_cache cache files in kernel\n" +#ifdef __SOLARIS__ +" -o [no]auto_cache enable caching based on modification times (off)\n" +#endif /* __SOLARIS__ */ +" -o umask=M set file permissions (octal)\n" +" -o uid=N set file owner\n" +" -o gid=N set file group\n" +" -o entry_timeout=T cache timeout for names (1.0s)\n" +" -o negative_timeout=T cache timeout for deleted names (0.0s)\n" +" -o attr_timeout=T cache timeout for attributes (1.0s)\n" +" -o ac_attr_timeout=T auto cache timeout for attributes (attr_timeout)\n" +" -o intr allow requests to be interrupted\n" +" -o intr_signal=NUM signal to send on interrupt (%i)\n" +#ifdef __SOLARIS__ +" -o modules=M1[:M2...] names of modules to push onto filesystem stack\n" +#endif /* __SOLARIS__ */ +"\n", FUSE_DEFAULT_INTR_SIGNAL); +} + +#ifdef __SOLARIS__ + +static void fuse_lib_help_modules(void) +{ + struct fuse_module *m; + fprintf(stderr, "\nModule options:\n"); + pthread_mutex_lock(&fuse_context_lock); + for (m = fuse_modules; m; m = m->next) { + struct fuse_fs *fs = NULL; + struct fuse_fs *newfs; + struct fuse_args args = FUSE_ARGS_INIT(0, NULL); + if (fuse_opt_add_arg(&args, "") != -1 && + fuse_opt_add_arg(&args, "-h") != -1) { + fprintf(stderr, "\n[%s]\n", m->name); + newfs = m->factory(&args, &fs); + assert(newfs == NULL); + } + fuse_opt_free_args(&args); + } + pthread_mutex_unlock(&fuse_context_lock); +} + +int fuse_is_lib_option(const char *opt) +{ + return fuse_lowlevel_is_lib_option(opt) || + fuse_opt_match(fuse_lib_opts, opt); +} + +#endif /* __SOLARIS__ */ + +static int fuse_lib_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + (void) arg; (void) outargs; + + if (key == KEY_HELP) { + struct fuse_config *conf = (struct fuse_config *) data; + fuse_lib_help(); + conf->help = 1; + } + + return 1; +} + +static int fuse_init_intr_signal(int signum, int *installed) +{ + struct sigaction old_sa; + + if (sigaction(signum, NULL, &old_sa) == -1) { + perror("fuse: cannot get old signal handler"); + return -1; + } + + if (old_sa.sa_handler == SIG_DFL) { + struct sigaction sa; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = fuse_intr_sighandler; + sigemptyset(&sa.sa_mask); + + if (sigaction(signum, &sa, NULL) == -1) { + perror("fuse: cannot set interrupt signal handler"); + return -1; + } + *installed = 1; + } + return 0; +} + +static void fuse_restore_intr_signal(int signum) +{ + struct sigaction sa; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = SIG_DFL; + sigaction(signum, &sa, NULL); +} + +#ifdef __SOLARIS__ + +static int fuse_push_module(struct fuse *f, const char *module, + struct fuse_args *args) +{ + struct fuse_fs *newfs; + struct fuse_module *m = fuse_get_module(module); + struct fuse_fs *fs[2]; + + fs[0] = f->fs; + fs[1] = NULL; + if (!m) + return -1; + + newfs = m->factory(args, fs); + if (!newfs) { + fuse_put_module(m); + return -1; + } + newfs->m = m; + f->fs = newfs; + return 0; +} + +#endif /* __SOLARIS__ */ + +struct fuse_fs *fuse_fs_new(const struct fuse_operations *op, size_t op_size, + void *user_data) +{ + struct fuse_fs *fs; + + if (sizeof(struct fuse_operations) < op_size) { + fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n"); + op_size = sizeof(struct fuse_operations); + } + + fs = (struct fuse_fs *) calloc(1, sizeof(struct fuse_fs)); + if (!fs) { + fprintf(stderr, "fuse: failed to allocate fuse_fs object\n"); + return NULL; + } + + fs->user_data = user_data; + if (op) + memcpy(&fs->op, op, op_size); + return fs; +} + +struct fuse *fuse_new(struct fuse_chan *ch, struct fuse_args *args, + const struct fuse_operations *op, size_t op_size, + void *user_data) +{ + struct fuse *f; + struct node *root; + struct fuse_fs *fs; + struct fuse_lowlevel_ops llop = fuse_path_ops; + + if (fuse_create_context_key() == -1) + goto out; + + f = (struct fuse *) calloc(1, sizeof(struct fuse)); + if (f == NULL) { + fprintf(stderr, "fuse: failed to allocate fuse object\n"); + goto out_delete_context_key; + } + + fs = fuse_fs_new(op, op_size, user_data); + if (!fs) + goto out_free; + + f->fs = fs; + + /* Oh f**k, this is ugly! */ + if (!fs->op.lock) { + llop.getlk = NULL; + llop.setlk = NULL; + } + + f->conf.entry_timeout = 1.0; + f->conf.attr_timeout = 1.0; + f->conf.negative_timeout = 0.0; + f->conf.intr_signal = FUSE_DEFAULT_INTR_SIGNAL; + + if (fuse_opt_parse(args, &f->conf, fuse_lib_opts, fuse_lib_opt_proc) == -1) + goto out_free_fs; + +#ifdef __SOLARIS__ + if (f->conf.modules) { + char *module; + char *next; + + for (module = f->conf.modules; module; module = next) { + char *p; + for (p = module; *p && *p != ':'; p++); + next = *p ? p + 1 : NULL; + *p = '\0'; + if (module[0] && fuse_push_module(f, module, args) == -1) + goto out_free_fs; + } + } +#endif /* __SOLARIS__ */ + + if (!f->conf.ac_attr_timeout_set) + f->conf.ac_attr_timeout = f->conf.attr_timeout; + +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + /* + * In FreeBSD, we always use these settings as inode numbers are needed to + * make getcwd(3) work. + */ + f->conf.readdir_ino = 1; +#endif + + f->se = fuse_lowlevel_new(args, &llop, sizeof(llop), f); + + if (f->se == NULL) { +#ifdef __SOLARIS__ + if (f->conf.help) + fuse_lib_help_modules(); +#endif /* __SOLARIS__ */ + goto out_free_fs; + } + + fuse_session_add_chan(f->se, ch); + + f->ctr = 0; + f->generation = 0; + /* FIXME: Dynamic hash table */ + f->name_table_size = 14057; + f->name_table = (struct node **) + calloc(1, sizeof(struct node *) * f->name_table_size); + if (f->name_table == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + goto out_free_session; + } + + f->id_table_size = 14057; + f->id_table = (struct node **) + calloc(1, sizeof(struct node *) * f->id_table_size); + if (f->id_table == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + goto out_free_name_table; + } + + fuse_mutex_init(&f->lock); + pthread_rwlock_init(&f->tree_lock, NULL); + + root = (struct node *) calloc(1, sizeof(struct node)); + if (root == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + goto out_free_id_table; + } + + root->name = strdup("/"); + if (root->name == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + goto out_free_root; + } + + if (f->conf.intr && + fuse_init_intr_signal(f->conf.intr_signal, &f->intr_installed) == -1) + goto out_free_root_name; + + root->parent = NULL; + root->nodeid = FUSE_ROOT_ID; + root->generation = 0; + root->refctr = 1; + root->nlookup = 1; + hash_id(f, root); + + return f; + + out_free_root_name: + free(root->name); + out_free_root: + free(root); + out_free_id_table: + free(f->id_table); + out_free_name_table: + free(f->name_table); + out_free_session: + fuse_session_destroy(f->se); + out_free_fs: + /* Horrible compatibility hack to stop the destructor from being + called on the filesystem without init being called first */ + fs->op.destroy = NULL; + fuse_fs_destroy(f->fs); +#ifdef __SOLARIS__ + free(f->conf.modules); +#endif /* __SOLARIS__ */ + out_free: + free(f); + out_delete_context_key: + fuse_delete_context_key(); + out: + return NULL; +} + +void fuse_destroy(struct fuse *f) +{ + size_t i; + + if (f->conf.intr && f->intr_installed) + fuse_restore_intr_signal(f->conf.intr_signal); + + if (f->fs) { + struct fuse_context_i *c = fuse_get_context_internal(); + + memset(c, 0, sizeof(*c)); + c->ctx.fuse = f; + + for (i = 0; i < f->id_table_size; i++) { + struct node *node; + + for (node = f->id_table[i]; node != NULL; node = node->id_next) { + if (node->is_hidden) { + char *path = get_path(f, node->nodeid); + if (path) { + fuse_fs_unlink(f->fs, path); + free(path); + } + } + } + } + } + for (i = 0; i < f->id_table_size; i++) { + struct node *node; + struct node *next; + + for (node = f->id_table[i]; node != NULL; node = next) { + next = node->id_next; + free_node(node); + } + } + free(f->id_table); + free(f->name_table); + pthread_mutex_destroy(&f->lock); + pthread_rwlock_destroy(&f->tree_lock); + fuse_session_destroy(f->se); +#ifdef __SOLARIS__ + free(f->conf.modules); +#endif /* __SOLARIS__ */ + free(f); + fuse_delete_context_key(); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_i.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_i.h new file mode 100644 index 0000000..38c45c7 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_i.h @@ -0,0 +1,25 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "fuse.h" + +struct fuse_session; +struct fuse_chan; +struct fuse_lowlevel_ops; +struct fuse_req; + +struct fuse_cmd { + char *buf; + size_t buflen; + struct fuse_chan *ch; +}; + +struct fuse_chan *fuse_kern_chan_new(int fd); + +void fuse_kern_unmount(const char *mountpoint, int fd); +int fuse_kern_mount(const char *mountpoint, struct fuse_args *args); diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_kern_chan.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_kern_chan.c new file mode 100644 index 0000000..e9963b5 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_kern_chan.c @@ -0,0 +1,96 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include "fuse_lowlevel.h" +#include "fuse_kernel.h" +#include "fuse_i.h" + +#include +#include +#include +#include + +static int fuse_kern_chan_receive(struct fuse_chan **chp, char *buf, + size_t size) +{ + struct fuse_chan *ch = *chp; + int err; + ssize_t res; + struct fuse_session *se = fuse_chan_session(ch); + assert(se != NULL); + + restart: + res = read(fuse_chan_fd(ch), buf, size); + err = errno; + + if (fuse_session_exited(se)) + return 0; + if (res == -1) { + /* ENOENT means the operation was interrupted, it's safe + to restart */ + if (err == ENOENT) + goto restart; + + if (err == ENODEV) { + fuse_session_exit(se); + return 0; + } + /* Errors occuring during normal operation: EINTR (read + interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem + umounted) */ + if (err != EINTR && err != EAGAIN) + perror("fuse: reading device"); + return -err; + } + if ((size_t) res < sizeof(struct fuse_in_header)) { + fprintf(stderr, "short read on fuse device\n"); + return -EIO; + } + return res; +} + +static int fuse_kern_chan_send(struct fuse_chan *ch, const struct iovec iov[], + size_t count) +{ + if (iov) { + ssize_t res = writev(fuse_chan_fd(ch), iov, count); + int err = errno; + + if (res == -1) { + struct fuse_session *se = fuse_chan_session(ch); + + assert(se != NULL); + + /* ENOENT means the operation was interrupted */ + if (!fuse_session_exited(se) && err != ENOENT) + perror("fuse: writing device"); + return -err; + } + } + return 0; +} + +static void fuse_kern_chan_destroy(struct fuse_chan *ch) +{ + close(fuse_chan_fd(ch)); +} + +#define MIN_BUFSIZE 0x21000 + +struct fuse_chan *fuse_kern_chan_new(int fd) +{ + struct fuse_chan_ops op = { + .receive = fuse_kern_chan_receive, + .send = fuse_kern_chan_send, + .destroy = fuse_kern_chan_destroy, + }; + size_t bufsize = getpagesize() + 0x1000; + bufsize = bufsize < MIN_BUFSIZE ? MIN_BUFSIZE : bufsize; + return fuse_chan_new(&op, fd, bufsize, NULL); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_loop.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_loop.c new file mode 100644 index 0000000..0b592e5 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_loop.c @@ -0,0 +1,40 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include "fuse_lowlevel.h" + +#include +#include +#include + +int fuse_session_loop(struct fuse_session *se) +{ + int res = 0; + struct fuse_chan *ch = fuse_session_next_chan(se, NULL); + size_t bufsize = fuse_chan_bufsize(ch); + char *buf = (char *) malloc(bufsize); + if (!buf) { + fprintf(stderr, "fuse: failed to allocate read buffer\n"); + return -1; + } + + while (!fuse_session_exited(se)) { + struct fuse_chan *tmpch = ch; + res = fuse_chan_recv(&tmpch, buf, bufsize); + if (res == -EINTR) + continue; + if (res <= 0) + break; + fuse_session_process(se, buf, res, tmpch); + } + + free(buf); + fuse_session_reset(se); + return res < 0 ? -1 : 0; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_lowlevel.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_lowlevel.c new file mode 100644 index 0000000..a2a5827 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_lowlevel.c @@ -0,0 +1,1466 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include "fuse_lowlevel.h" +#include "fuse_kernel.h" +#include "fuse_opt.h" +#include "fuse_i.h" +#include "fuse_misc.h" +#include "fuse_lowlevel_compat.h" + +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef MAJOR_IN_MKDEV +#include +#endif +#ifdef MAJOR_IN_SYSMACROS +#include +#endif + +#define PARAM(inarg) (((const char *)(inarg)) + sizeof(*(inarg))) +#define OFFSET_MAX 0x7fffffffffffffffLL + +struct fuse_ll; + +struct fuse_req { + struct fuse_ll *f; + uint64_t unique; + int ctr; + pthread_mutex_t lock; + struct fuse_ctx ctx; + struct fuse_chan *ch; + int interrupted; + union { + struct { + uint64_t unique; + } i; + struct { + fuse_interrupt_func_t func; + void *data; + } ni; + } u; + struct fuse_req *next; + struct fuse_req *prev; +}; + +struct fuse_ll { + int debug; + int allow_root; + struct fuse_lowlevel_ops op; + int got_init; + void *userdata; + uid_t owner; + struct fuse_conn_info conn; + struct fuse_req list; + struct fuse_req interrupts; + pthread_mutex_t lock; + int got_destroy; +}; + +static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr) +{ + attr->ino = stbuf->st_ino; + attr->mode = stbuf->st_mode; + attr->nlink = stbuf->st_nlink; + attr->uid = stbuf->st_uid; + attr->gid = stbuf->st_gid; +#if defined(__SOLARIS__) && defined(_LP64) + /* Must pack the device the old way (attr->rdev limited to 32 bits) */ + attr->rdev = ((major(stbuf->st_rdev) & 0x3fff) << 18) + | (minor(stbuf->st_rdev) & 0x3ffff); +#else + attr->rdev = stbuf->st_rdev; +#endif + attr->size = stbuf->st_size; + attr->blocks = stbuf->st_blocks; + attr->atime = stbuf->st_atime; + attr->mtime = stbuf->st_mtime; + attr->ctime = stbuf->st_ctime; + attr->atimensec = ST_ATIM_NSEC(stbuf); + attr->mtimensec = ST_MTIM_NSEC(stbuf); + attr->ctimensec = ST_CTIM_NSEC(stbuf); +#ifdef POSIXACLS + attr->filling = 0; /* JPA trying to be safe */ +#endif +} + +static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf) +{ + stbuf->st_mode = attr->mode; + stbuf->st_uid = attr->uid; + stbuf->st_gid = attr->gid; + stbuf->st_size = attr->size; + stbuf->st_atime = attr->atime; + stbuf->st_mtime = attr->mtime; + ST_ATIM_NSEC_SET(stbuf, attr->atimensec); + ST_MTIM_NSEC_SET(stbuf, attr->mtimensec); +} + +static size_t iov_length(const struct iovec *iov, size_t count) +{ + size_t seg; + size_t ret = 0; + + for (seg = 0; seg < count; seg++) + ret += iov[seg].iov_len; + return ret; +} + +static void list_init_req(struct fuse_req *req) +{ + req->next = req; + req->prev = req; +} + +static void list_del_req(struct fuse_req *req) +{ + struct fuse_req *prev = req->prev; + struct fuse_req *next = req->next; + prev->next = next; + next->prev = prev; +} + +static void list_add_req(struct fuse_req *req, struct fuse_req *next) +{ + struct fuse_req *prev = next->prev; + req->next = next; + req->prev = prev; + prev->next = req; + next->prev = req; +} + +static void destroy_req(fuse_req_t req) +{ + pthread_mutex_destroy(&req->lock); + free(req); +} + +static void free_req(fuse_req_t req) +{ + int ctr; + struct fuse_ll *f = req->f; + + pthread_mutex_lock(&req->lock); + req->u.ni.func = NULL; + req->u.ni.data = NULL; + pthread_mutex_unlock(&req->lock); + + pthread_mutex_lock(&f->lock); + list_del_req(req); + ctr = --req->ctr; + pthread_mutex_unlock(&f->lock); + if (!ctr) + destroy_req(req); +} + +static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov, + int count) +{ + struct fuse_out_header out; + int res; + + if (error <= -1000 || error > 0) { + fprintf(stderr, "fuse: bad error value: %i\n", error); + error = -ERANGE; + } + + out.unique = req->unique; + out.error = error; + iov[0].iov_base = &out; + iov[0].iov_len = sizeof(struct fuse_out_header); + out.len = iov_length(iov, count); + + if (req->f->debug) + fprintf(stderr, " unique: %llu, error: %i (%s), outsize: %i\n", + (unsigned long long) out.unique, out.error, + strerror(-out.error), out.len); + res = fuse_chan_send(req->ch, iov, count); + free_req(req); + + return res; +} + +static int send_reply(fuse_req_t req, int error, const void *arg, + size_t argsize) +{ + struct iovec iov[2]; + int count = 1; + if (argsize) { + /* Note : const qualifier dropped */ + iov[1].iov_base = (void *)(uintptr_t) arg; + iov[1].iov_len = argsize; + count++; + } + return send_reply_iov(req, error, iov, count); +} + +#if 0 /* not used */ +int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count) +{ + int res; + struct iovec *padded_iov; + + padded_iov = malloc((count + 1) * sizeof(struct iovec)); + if (padded_iov == NULL) + return fuse_reply_err(req, -ENOMEM); + + memcpy(padded_iov + 1, iov, count * sizeof(struct iovec)); + count++; + + res = send_reply_iov(req, 0, padded_iov, count); + free(padded_iov); + + return res; +} +#endif + +size_t fuse_dirent_size(size_t namelen) +{ + return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen); +} + +char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf, + off_t off) +{ + unsigned namelen = strlen(name); + unsigned entlen = FUSE_NAME_OFFSET + namelen; + unsigned entsize = fuse_dirent_size(namelen); + unsigned padlen = entsize - entlen; + struct fuse_dirent *dirent = (struct fuse_dirent *) buf; + + dirent->ino = stbuf->st_ino; + dirent->off = off; + dirent->namelen = namelen; + dirent->type = (stbuf->st_mode & 0170000) >> 12; + memcpy(dirent->name, name, namelen); + if (padlen) + memset(buf + entlen, 0, padlen); + + return buf + entsize; +} + +size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, + const char *name, const struct stat *stbuf, off_t off) +{ + size_t entsize; + + (void) req; + entsize = fuse_dirent_size(strlen(name)); + if (entsize <= bufsize && buf) + fuse_add_dirent(buf, name, stbuf, off); + return entsize; +} + +static void convert_statfs(const struct statvfs *stbuf, + struct fuse_kstatfs *kstatfs) +{ + kstatfs->bsize = stbuf->f_bsize; + kstatfs->frsize = stbuf->f_frsize; + kstatfs->blocks = stbuf->f_blocks; + kstatfs->bfree = stbuf->f_bfree; + kstatfs->bavail = stbuf->f_bavail; + kstatfs->files = stbuf->f_files; + kstatfs->ffree = stbuf->f_ffree; + kstatfs->namelen = stbuf->f_namemax; +} + +static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize) +{ + return send_reply(req, 0, arg, argsize); +} + +int fuse_reply_err(fuse_req_t req, int err) +{ + return send_reply(req, -err, NULL, 0); +} + +void fuse_reply_none(fuse_req_t req) +{ + fuse_chan_send(req->ch, NULL, 0); + free_req(req); +} + +static unsigned long calc_timeout_sec(double t) +{ + if (t > (double) ULONG_MAX) + return ULONG_MAX; + else if (t < 0.0) + return 0; + else + return (unsigned long) t; +} + +static unsigned int calc_timeout_nsec(double t) +{ + unsigned long secs = calc_timeout_sec(t); + double f = t - (double)secs; + if (f < 0.0) + return 0; + else if (f >= 0.999999999) + return 999999999; + else + return (unsigned int) (f * 1.0e9); +} + +static void fill_entry(struct fuse_entry_out *arg, + const struct fuse_entry_param *e) +{ + arg->nodeid = e->ino; + arg->generation = e->generation; + arg->entry_valid = calc_timeout_sec(e->entry_timeout); + arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout); + arg->attr_valid = calc_timeout_sec(e->attr_timeout); + arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout); + convert_stat(&e->attr, &arg->attr); +} + +static void fill_open(struct fuse_open_out *arg, + const struct fuse_file_info *f) +{ + arg->fh = f->fh; + if (f->direct_io) + arg->open_flags |= FOPEN_DIRECT_IO; + if (f->keep_cache) + arg->open_flags |= FOPEN_KEEP_CACHE; +} + +int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e) +{ + struct fuse_entry_out arg; + + /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant + negative entry */ + if (!e->ino && req->f->conn.proto_minor < 4) + return fuse_reply_err(req, ENOENT); + + memset(&arg, 0, sizeof(arg)); + fill_entry(&arg, e); + return send_reply_ok(req, &arg, (req->f->conn.proto_minor >= 12 + ? sizeof(arg) : FUSE_COMPAT_ENTRY_OUT_SIZE)); +} + +int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, + const struct fuse_file_info *f) +{ + struct { + struct fuse_entry_out e; + struct fuse_open_out o; + } arg; + + memset(&arg, 0, sizeof(arg)); + fill_entry(&arg.e, e); + if (req->f->conn.proto_minor < 12) { + fill_open((struct fuse_open_out*) + ((char*)&arg + FUSE_COMPAT_ENTRY_OUT_SIZE), f); + return send_reply_ok(req, &arg, + FUSE_COMPAT_ENTRY_OUT_SIZE + sizeof(struct fuse_open_out)); + } else { + fill_open(&arg.o, f); + return send_reply_ok(req, &arg, sizeof(arg)); + } +} + +int fuse_reply_attr(fuse_req_t req, const struct stat *attr, + double attr_timeout) +{ + struct fuse_attr_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.attr_valid = calc_timeout_sec(attr_timeout); + arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout); + convert_stat(attr, &arg.attr); + + return send_reply_ok(req, &arg, (req->f->conn.proto_minor >= 12 + ? sizeof(arg) : FUSE_COMPAT_FUSE_ATTR_OUT_SIZE)); +} + +int fuse_reply_readlink(fuse_req_t req, const char *linkname) +{ + return send_reply_ok(req, linkname, strlen(linkname)); +} + +int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f) +{ + struct fuse_open_out arg; + + memset(&arg, 0, sizeof(arg)); + fill_open(&arg, f); + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_write(fuse_req_t req, size_t count) +{ + struct fuse_write_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.size = count; + + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size) +{ + return send_reply_ok(req, buf, size); +} + +int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf) +{ + struct fuse_statfs_out arg; + size_t size = req->f->conn.proto_minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(arg); + + memset(&arg, 0, sizeof(arg)); + convert_statfs(stbuf, &arg.st); + + return send_reply_ok(req, &arg, size); +} + +int fuse_reply_xattr(fuse_req_t req, size_t count) +{ + struct fuse_getxattr_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.size = count; + + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_lock(fuse_req_t req, struct flock *lock) +{ + struct fuse_lk_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.lk.type = lock->l_type; + if (lock->l_type != F_UNLCK) { + arg.lk.start = lock->l_start; + if (lock->l_len == 0) + arg.lk.end = OFFSET_MAX; + else + arg.lk.end = lock->l_start + lock->l_len - 1; + } + arg.lk.pid = lock->l_pid; + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_bmap(fuse_req_t req, uint64_t idx) +{ + struct fuse_bmap_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.block = idx; + + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size) +{ + struct fuse_ioctl_out arg; + struct iovec iov[3]; + size_t count = 1; + + memset(&arg, 0, sizeof(arg)); + arg.result = result; + iov[count].iov_base = &arg; + iov[count].iov_len = sizeof(arg); + count++; + + if (size) { + /* Note : const qualifier dropped */ + iov[count].iov_base = (char *)(uintptr_t) buf; + iov[count].iov_len = size; + count++; + } + + return send_reply_iov(req, 0, iov, count); +} + +static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const char *name = (const char *) inarg; + + if (req->f->op.lookup) + req->f->op.lookup(req, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_forget_in *arg = (const struct fuse_forget_in *) inarg; + + if (req->f->op.forget) + req->f->op.forget(req, nodeid, arg->nlookup); + else + fuse_reply_none(req); +} + +static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + (void) inarg; + + if (req->f->op.getattr) + req->f->op.getattr(req, nodeid, NULL); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_setattr_in *arg = (const struct fuse_setattr_in *) inarg; + + if (req->f->op.setattr) { + struct fuse_file_info *fi = NULL; + struct fuse_file_info fi_store; + struct stat stbuf; + memset(&stbuf, 0, sizeof(stbuf)); + convert_attr(arg, &stbuf); + if (arg->valid & FATTR_FH) { + memset(&fi_store, 0, sizeof(fi_store)); + fi = &fi_store; + fi->fh = arg->fh; + fi->fh_old = fi->fh; + } + req->f->op.setattr(req, nodeid, &stbuf, arg->valid & ~FATTR_FH, fi); + } else + fuse_reply_err(req, ENOSYS); +} + +static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_access_in *arg = (const struct fuse_access_in *) inarg; + + if (req->f->op.access) + req->f->op.access(req, nodeid, arg->mask); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + (void) inarg; + + if (req->f->op.readlink) + req->f->op.readlink(req, nodeid); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_mknod_in *arg = (const struct fuse_mknod_in *) inarg; + const char *name = PARAM(arg); + + if (req->f->conn.proto_minor >= 12) + req->ctx.umask = arg->umask; + else + name = (const char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE; + + if (req->f->op.mknod) { +#if defined(__SOLARIS__) && defined(_LP64) + /* + * Must unpack the device, as arg->rdev is limited to 32 bits, + * and must have the same format in 32-bit and 64-bit builds. + */ + req->f->op.mknod(req, nodeid, name, arg->mode, + makedev((arg->rdev >> 18) & 0x3fff, + arg->rdev & 0x3ffff)); +#else + req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev); +#endif + } else + fuse_reply_err(req, ENOSYS); +} + +static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_mkdir_in *arg = (const struct fuse_mkdir_in *) inarg; + + if (req->f->conn.proto_minor >= 12) + req->ctx.umask = arg->umask; + + if (req->f->op.mkdir) + req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const char *name = (const char *) inarg; + + if (req->f->op.unlink) + req->f->op.unlink(req, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const char *name = (const char *) inarg; + + if (req->f->op.rmdir) + req->f->op.rmdir(req, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const char *name = (const char *) inarg; + const char *linkname = ((const char *) inarg) + strlen((const char *) inarg) + 1; + + if (req->f->op.symlink) + req->f->op.symlink(req, linkname, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_rename_in *arg = (const struct fuse_rename_in *) inarg; + const char *oldname = PARAM(arg); + const char *newname = oldname + strlen(oldname) + 1; + + if (req->f->op.rename) + req->f->op.rename(req, nodeid, oldname, arg->newdir, newname); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_link_in *arg = (const struct fuse_link_in *) inarg; + + if (req->f->op.link) + req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg)); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_create_in *arg = (const struct fuse_create_in *) inarg; + + if (req->f->op.create) { + struct fuse_file_info fi; + const char *name = PARAM(arg); + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + + if (req->f->conn.proto_minor >= 12) + req->ctx.umask = arg->umask; + else + name = (const char *) inarg + sizeof(struct fuse_open_in); + + req->f->op.create(req, nodeid, name, arg->mode, &fi); + } else + fuse_reply_err(req, ENOSYS); +} + +static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_open_in *arg = (const struct fuse_open_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + + if (req->f->op.open) + req->f->op.open(req, nodeid, &fi); + else + fuse_reply_open(req, &fi); +} + +static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_read_in *arg = (const struct fuse_read_in *) inarg; + + if (req->f->op.read) { + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + req->f->op.read(req, nodeid, arg->size, arg->offset, &fi); + } else + fuse_reply_err(req, ENOSYS); +} + +static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_write_in *arg = (const struct fuse_write_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + fi.writepage = arg->write_flags & 1; + + if (req->f->op.write) { + const char *buf; + + if (req->f->conn.proto_minor >= 12) + buf = PARAM(arg); + else + buf = ((const char*)arg) + FUSE_COMPAT_WRITE_IN_SIZE; + req->f->op.write(req, nodeid, buf, arg->size, arg->offset, &fi); + } else + fuse_reply_err(req, ENOSYS); +} + +static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_flush_in *arg = (const struct fuse_flush_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + fi.flush = 1; + if (req->f->conn.proto_minor >= 7) + fi.lock_owner = arg->lock_owner; + + if (req->f->op.flush) + req->f->op.flush(req, nodeid, &fi); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_release_in *arg = (const struct fuse_release_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + fi.fh = arg->fh; + fi.fh_old = fi.fh; + if (req->f->conn.proto_minor >= 8) { + fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0; + fi.lock_owner = arg->lock_owner; + } + + if (req->f->op.release) + req->f->op.release(req, nodeid, &fi); + else + fuse_reply_err(req, 0); +} + +static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_fsync_in *arg = (const struct fuse_fsync_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.fsync) + req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_open_in *arg = (const struct fuse_open_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + + if (req->f->op.opendir) + req->f->op.opendir(req, nodeid, &fi); + else + fuse_reply_open(req, &fi); +} + +static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_read_in *arg = (const struct fuse_read_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.readdir) + req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_release_in *arg = (const struct fuse_release_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.releasedir) + req->f->op.releasedir(req, nodeid, &fi); + else + fuse_reply_err(req, 0); +} + +static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_fsync_in *arg = (const struct fuse_fsync_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.fsyncdir) + req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + (void) nodeid; + (void) inarg; + + if (req->f->op.statfs) + req->f->op.statfs(req, nodeid); + else { + struct statvfs buf = { + .f_namemax = 255, + .f_bsize = 512, + }; + fuse_reply_statfs(req, &buf); + } +} + +static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_setxattr_in *arg = (const struct fuse_setxattr_in *) inarg; + const char *name = PARAM(arg); + const char *value = name + strlen(name) + 1; + + if (req->f->op.setxattr) + req->f->op.setxattr(req, nodeid, name, value, arg->size, arg->flags); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_getxattr_in *arg = (const struct fuse_getxattr_in *) inarg; + + if (req->f->op.getxattr) + req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_getxattr_in *arg = (const struct fuse_getxattr_in *) inarg; + + if (req->f->op.listxattr) + req->f->op.listxattr(req, nodeid, arg->size); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const char *name = (const char *) inarg; + + if (req->f->op.removexattr) + req->f->op.removexattr(req, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void convert_fuse_file_lock(const struct fuse_file_lock *fl, + struct flock *flock) +{ + memset(flock, 0, sizeof(struct flock)); + flock->l_type = fl->type; + flock->l_whence = SEEK_SET; + flock->l_start = fl->start; + if (fl->end == OFFSET_MAX) + flock->l_len = 0; + else + flock->l_len = fl->end - fl->start + 1; + flock->l_pid = fl->pid; +} + +static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_lk_in *arg = (const struct fuse_lk_in *) inarg; + struct fuse_file_info fi; + struct flock flock; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.lock_owner = arg->owner; + + convert_fuse_file_lock(&arg->lk, &flock); + if (req->f->op.getlk) + req->f->op.getlk(req, nodeid, &fi, &flock); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid, + const void *inarg, int should_sleep) +{ + const struct fuse_lk_in *arg = (const struct fuse_lk_in *) inarg; + struct fuse_file_info fi; + struct flock flock; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.lock_owner = arg->owner; + + convert_fuse_file_lock(&arg->lk, &flock); + if (req->f->op.setlk) + req->f->op.setlk(req, nodeid, &fi, &flock, should_sleep); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + do_setlk_common(req, nodeid, inarg, 0); +} + +static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + do_setlk_common(req, nodeid, inarg, 1); +} + +static int find_interrupted(struct fuse_ll *f, struct fuse_req *req) +{ + struct fuse_req *curr; + + for (curr = f->list.next; curr != &f->list; curr = curr->next) { + if (curr->unique == req->u.i.unique) { + curr->ctr++; + pthread_mutex_unlock(&f->lock); + + /* Ugh, ugly locking */ + pthread_mutex_lock(&curr->lock); + pthread_mutex_lock(&f->lock); + curr->interrupted = 1; + pthread_mutex_unlock(&f->lock); + if (curr->u.ni.func) + curr->u.ni.func(curr, curr->u.ni.data); + pthread_mutex_unlock(&curr->lock); + + pthread_mutex_lock(&f->lock); + curr->ctr--; + if (!curr->ctr) + destroy_req(curr); + + return 1; + } + } + for (curr = f->interrupts.next; curr != &f->interrupts; + curr = curr->next) { + if (curr->u.i.unique == req->u.i.unique) + return 1; + } + return 0; +} + +static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_interrupt_in *arg = (const struct fuse_interrupt_in *) inarg; + struct fuse_ll *f = req->f; + + (void) nodeid; + if (f->debug) + fprintf(stderr, "INTERRUPT: %llu\n", (unsigned long long) arg->unique); + + req->u.i.unique = arg->unique; + + pthread_mutex_lock(&f->lock); + if (find_interrupted(f, req)) + destroy_req(req); + else + list_add_req(req, &f->interrupts); + pthread_mutex_unlock(&f->lock); +} + +static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req) +{ + struct fuse_req *curr; + + for (curr = f->interrupts.next; curr != &f->interrupts; curr = curr->next) { + if (curr->u.i.unique == req->unique) { + req->interrupted = 1; + list_del_req(curr); + free(curr); + return NULL; + } + } + curr = f->interrupts.next; + if (curr != &f->interrupts) { + list_del_req(curr); + list_init_req(curr); + return curr; + } else + return NULL; +} + +static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_bmap_in *arg = (const struct fuse_bmap_in *) inarg; + + if (req->f->op.bmap) + req->f->op.bmap(req, nodeid, arg->blocksize, arg->block); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_ioctl_in *arg = (const struct fuse_ioctl_in *) inarg; + unsigned int flags = arg->flags; + const void *in_buf = arg->in_size ? PARAM(arg) : NULL; + struct fuse_file_info fi; + + if (flags & FUSE_IOCTL_DIR && + !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) { + fuse_reply_err(req, ENOTTY); + return; + } + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + +/* TODO JPA (need req->ioctl_64bit in obscure fuse_req_t) +// probably a 64 bit ioctl on a 32-bit cpu +// this is to forward a request from the kernel + if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 && + !(flags & FUSE_IOCTL_32BIT)) { + req->ioctl_64bit = 1; + } +*/ + + if (req->f->op.ioctl) + req->f->op.ioctl(req, nodeid, arg->cmd, + (void *)(uintptr_t)arg->arg, &fi, flags, + in_buf, arg->in_size, arg->out_size); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + const struct fuse_init_in *arg = (const struct fuse_init_in *) inarg; + struct fuse_init_out outarg; + struct fuse_ll *f = req->f; + size_t bufsize = fuse_chan_bufsize(req->ch); + + (void) nodeid; + if (f->debug) { + fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor); + if (arg->major > 7 || (arg->major == 7 && arg->minor >= 6)) { + fprintf(stderr, "flags=0x%08x\n", arg->flags); + fprintf(stderr, "max_readahead=0x%08x\n", arg->max_readahead); + } + } + f->conn.proto_major = arg->major; + f->conn.proto_minor = arg->minor; + + if (arg->major < 7) { + fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n", + arg->major, arg->minor); + fuse_reply_err(req, EPROTO); + return; + } + + if (arg->major > 7 || (arg->major == 7 && arg->minor >= 6)) { + if (f->conn.async_read) + f->conn.async_read = arg->flags & FUSE_ASYNC_READ; + if (arg->max_readahead < f->conn.max_readahead) + f->conn.max_readahead = arg->max_readahead; +#ifdef POSIXACLS + if (arg->flags & FUSE_DONT_MASK) + f->conn.capable |= FUSE_CAP_DONT_MASK; + if (arg->flags & FUSE_POSIX_ACL) + f->conn.capable |= FUSE_CAP_POSIX_ACL; +#endif + if (arg->flags & FUSE_BIG_WRITES) + f->conn.capable |= FUSE_CAP_BIG_WRITES; + if (arg->flags & FUSE_HAS_IOCTL_DIR) + f->conn.capable |= FUSE_CAP_IOCTL_DIR; + } else { + f->conn.async_read = 0; + f->conn.max_readahead = 0; + } + + if (bufsize < FUSE_MIN_READ_BUFFER) { + fprintf(stderr, "fuse: warning: buffer size too small: %zu\n", + bufsize); + bufsize = FUSE_MIN_READ_BUFFER; + } + + bufsize -= 4096; + if (bufsize < f->conn.max_write) + f->conn.max_write = bufsize; + + f->got_init = 1; + if (f->op.init) + f->op.init(f->userdata, &f->conn); + + memset(&outarg, 0, sizeof(outarg)); + outarg.major = FUSE_KERNEL_VERSION; + /* + * Suggest using protocol 7.18 when available, and fallback + * to 7.12 or even earlier when running on an old kernel. + * Protocol 7.12 has the ability to process the umask + * conditionnally (as needed if POSIXACLS is set) + * Protocol 7.18 has the ability to process the ioctls + */ + if (arg->major > 7 || (arg->major == 7 && arg->minor >= 18)) { + outarg.minor = FUSE_KERNEL_MINOR_VERSION; + if (f->conn.want & FUSE_CAP_IOCTL_DIR) + outarg.flags |= FUSE_HAS_IOCTL_DIR; +#ifdef POSIXACLS + if (f->conn.want & FUSE_CAP_DONT_MASK) + outarg.flags |= FUSE_DONT_MASK; + if (f->conn.want & FUSE_CAP_POSIX_ACL) + outarg.flags |= FUSE_POSIX_ACL; +#endif + } else { + /* Never use a version more recent than supported by the kernel */ + if ((arg->major < FUSE_KERNEL_MAJOR_FALLBACK) + || ((arg->major == FUSE_KERNEL_MAJOR_FALLBACK) + && (arg->minor < FUSE_KERNEL_MINOR_FALLBACK))) { + outarg.major = arg->major; + outarg.minor = arg->minor; + } else { + outarg.major = FUSE_KERNEL_MAJOR_FALLBACK; + outarg.minor = FUSE_KERNEL_MINOR_FALLBACK; +#ifdef POSIXACLS + if (f->conn.want & FUSE_CAP_DONT_MASK) + outarg.flags |= FUSE_DONT_MASK; + if (f->conn.want & FUSE_CAP_POSIX_ACL) + outarg.flags |= FUSE_POSIX_ACL; +#endif + } + } + if (f->conn.async_read) + outarg.flags |= FUSE_ASYNC_READ; + if (f->op.getlk && f->op.setlk) + outarg.flags |= FUSE_POSIX_LOCKS; + if (f->conn.want & FUSE_CAP_BIG_WRITES) + outarg.flags |= FUSE_BIG_WRITES; + outarg.max_readahead = f->conn.max_readahead; + outarg.max_write = f->conn.max_write; + + if (f->debug) { + fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor); + fprintf(stderr, " flags=0x%08x\n", outarg.flags); + fprintf(stderr, " max_readahead=0x%08x\n", outarg.max_readahead); + fprintf(stderr, " max_write=0x%08x\n", outarg.max_write); + } + + send_reply_ok(req, &outarg, arg->minor < 5 ? 8 : sizeof(outarg)); +} + +static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_ll *f = req->f; + + (void) nodeid; + (void) inarg; + + f->got_destroy = 1; + if (f->op.destroy) + f->op.destroy(f->userdata); + + send_reply_ok(req, NULL, 0); +} + +void *fuse_req_userdata(fuse_req_t req) +{ + return req->f->userdata; +} + +const struct fuse_ctx *fuse_req_ctx(fuse_req_t req) +{ + return &req->ctx; +} + +void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, + void *data) +{ + pthread_mutex_lock(&req->lock); + req->u.ni.func = func; + req->u.ni.data = data; + if (req->interrupted && func) + func(req, data); + pthread_mutex_unlock(&req->lock); +} + +int fuse_req_interrupted(fuse_req_t req) +{ + int interrupted; + + pthread_mutex_lock(&req->f->lock); + interrupted = req->interrupted; + pthread_mutex_unlock(&req->f->lock); + + return interrupted; +} + +static struct { + void (*func)(fuse_req_t, fuse_ino_t, const void *); + const char *name; +} fuse_ll_ops[] = { + [FUSE_LOOKUP] = { do_lookup, "LOOKUP" }, + [FUSE_FORGET] = { do_forget, "FORGET" }, + [FUSE_GETATTR] = { do_getattr, "GETATTR" }, + [FUSE_SETATTR] = { do_setattr, "SETATTR" }, + [FUSE_READLINK] = { do_readlink, "READLINK" }, + [FUSE_SYMLINK] = { do_symlink, "SYMLINK" }, + [FUSE_MKNOD] = { do_mknod, "MKNOD" }, + [FUSE_MKDIR] = { do_mkdir, "MKDIR" }, + [FUSE_UNLINK] = { do_unlink, "UNLINK" }, + [FUSE_RMDIR] = { do_rmdir, "RMDIR" }, + [FUSE_RENAME] = { do_rename, "RENAME" }, + [FUSE_LINK] = { do_link, "LINK" }, + [FUSE_OPEN] = { do_open, "OPEN" }, + [FUSE_READ] = { do_read, "READ" }, + [FUSE_WRITE] = { do_write, "WRITE" }, + [FUSE_STATFS] = { do_statfs, "STATFS" }, + [FUSE_RELEASE] = { do_release, "RELEASE" }, + [FUSE_FSYNC] = { do_fsync, "FSYNC" }, + [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" }, + [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" }, + [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" }, + [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" }, + [FUSE_FLUSH] = { do_flush, "FLUSH" }, + [FUSE_INIT] = { do_init, "INIT" }, + [FUSE_OPENDIR] = { do_opendir, "OPENDIR" }, + [FUSE_READDIR] = { do_readdir, "READDIR" }, + [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" }, + [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" }, + [FUSE_GETLK] = { do_getlk, "GETLK" }, + [FUSE_SETLK] = { do_setlk, "SETLK" }, + [FUSE_SETLKW] = { do_setlkw, "SETLKW" }, + [FUSE_ACCESS] = { do_access, "ACCESS" }, + [FUSE_CREATE] = { do_create, "CREATE" }, + [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" }, + [FUSE_BMAP] = { do_bmap, "BMAP" }, + [FUSE_IOCTL] = { do_ioctl, "IOCTL" }, + [FUSE_DESTROY] = { do_destroy, "DESTROY" }, +}; + +#define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0])) + +static const char *opname(enum fuse_opcode opcode) +{ + if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) + return "???"; + else + return fuse_ll_ops[opcode].name; +} + +static void fuse_ll_process(void *data, const char *buf, size_t len, + struct fuse_chan *ch) +{ + struct fuse_ll *f = (struct fuse_ll *) data; + const struct fuse_in_header *in = (const struct fuse_in_header *) buf; + const void *inarg = buf + sizeof(struct fuse_in_header); + struct fuse_req *req; + + if (f->debug) + fprintf(stderr, "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu\n", + (unsigned long long) in->unique, + opname((enum fuse_opcode) in->opcode), in->opcode, + (unsigned long) in->nodeid, len); + + req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req)); + if (req == NULL) { + fprintf(stderr, "fuse: failed to allocate request\n"); + return; + } + + req->f = f; + req->unique = in->unique; + req->ctx.uid = in->uid; + req->ctx.gid = in->gid; + req->ctx.pid = in->pid; + req->ch = ch; + req->ctr = 1; + list_init_req(req); + fuse_mutex_init(&req->lock); + + if (!f->got_init && in->opcode != FUSE_INIT) + fuse_reply_err(req, EIO); + else if (f->allow_root && in->uid != f->owner && in->uid != 0 && + in->opcode != FUSE_INIT && in->opcode != FUSE_READ && + in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC && + in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR && + in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) { + fuse_reply_err(req, EACCES); + } else if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) + fuse_reply_err(req, ENOSYS); + else { + if (in->opcode != FUSE_INTERRUPT) { + struct fuse_req *intr; + pthread_mutex_lock(&f->lock); + intr = check_interrupt(f, req); + list_add_req(req, &f->list); + pthread_mutex_unlock(&f->lock); + if (intr) + fuse_reply_err(intr, EAGAIN); + } + fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg); + } +} + +enum { + KEY_HELP, + KEY_VERSION, +}; + +static struct fuse_opt fuse_ll_opts[] = { + { "debug", offsetof(struct fuse_ll, debug), 1 }, + { "-d", offsetof(struct fuse_ll, debug), 1 }, + { "allow_root", offsetof(struct fuse_ll, allow_root), 1 }, + { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 }, + { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 }, + { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 }, + { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 }, + FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD), + FUSE_OPT_KEY("-h", KEY_HELP), + FUSE_OPT_KEY("--help", KEY_HELP), + FUSE_OPT_KEY("-V", KEY_VERSION), + FUSE_OPT_KEY("--version", KEY_VERSION), + FUSE_OPT_END +}; + +static void fuse_ll_version(void) +{ + fprintf(stderr, "using FUSE kernel interface version %i.%i\n", + FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); +} + +static void fuse_ll_help(void) +{ + fprintf(stderr, +" -o max_write=N set maximum size of write requests\n" +" -o max_readahead=N set maximum readahead\n" +" -o async_read perform reads asynchronously (default)\n" +" -o sync_read perform reads synchronously\n"); +} + +static int fuse_ll_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + (void) data; (void) outargs; + + switch (key) { + case KEY_HELP: + fuse_ll_help(); + break; + + case KEY_VERSION: + fuse_ll_version(); + break; + + default: + fprintf(stderr, "fuse: unknown option `%s'\n", arg); + } + + return -1; +} + +#ifdef __SOLARIS__ + +int fuse_lowlevel_is_lib_option(const char *opt) +{ + return fuse_opt_match(fuse_ll_opts, opt); +} + +#endif /* __SOLARIS__ */ + +static void fuse_ll_destroy(void *data) +{ + struct fuse_ll *f = (struct fuse_ll *) data; + + if (f->got_init && !f->got_destroy) { + if (f->op.destroy) + f->op.destroy(f->userdata); + } + + pthread_mutex_destroy(&f->lock); + free(f); +} + +struct fuse_session *fuse_lowlevel_new(struct fuse_args *args, + const struct fuse_lowlevel_ops *op, + size_t op_size, void *userdata) +{ + struct fuse_ll *f; + struct fuse_session *se; + struct fuse_session_ops sop = { + .process = fuse_ll_process, + .destroy = fuse_ll_destroy, + }; + + if (sizeof(struct fuse_lowlevel_ops) < op_size) { + fprintf(stderr, "fuse: warning: library too old, some operations may not work\n"); + op_size = sizeof(struct fuse_lowlevel_ops); + } + + f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll)); + if (f == NULL) { + fprintf(stderr, "fuse: failed to allocate fuse object\n"); + goto out; + } + + f->conn.async_read = 1; + f->conn.max_write = UINT_MAX; + f->conn.max_readahead = UINT_MAX; + list_init_req(&f->list); + list_init_req(&f->interrupts); + fuse_mutex_init(&f->lock); + + if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1) + goto out_free; + + memcpy(&f->op, op, op_size); + f->owner = getuid(); + f->userdata = userdata; + + se = fuse_session_new(&sop, f); + if (!se) + goto out_free; + + return se; + + out_free: + free(f); + out: + return NULL; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_misc.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_misc.h new file mode 100644 index 0000000..8630989 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_misc.h @@ -0,0 +1,51 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include + +#ifndef USE_UCLIBC +#define fuse_mutex_init(mut) pthread_mutex_init(mut, NULL) +#else +static inline void fuse_mutex_init(pthread_mutex_t *mut) +{ + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); + pthread_mutex_init(mut, &attr); + pthread_mutexattr_destroy(&attr); +} +#endif + +#ifdef HAVE_STRUCT_STAT_ST_ATIM +/* Linux */ +#define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atim.tv_nsec) +#define ST_CTIM_NSEC(stbuf) ((stbuf)->st_ctim.tv_nsec) +#define ST_MTIM_NSEC(stbuf) ((stbuf)->st_mtim.tv_nsec) +#define ST_ATIM_NSEC_SET(stbuf, val) (stbuf)->st_atim.tv_nsec = (val) +#define ST_MTIM_NSEC_SET(stbuf, val) (stbuf)->st_mtim.tv_nsec = (val) +#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC) +/* FreeBSD */ +#define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atimespec.tv_nsec) +#define ST_CTIM_NSEC(stbuf) ((stbuf)->st_ctimespec.tv_nsec) +#define ST_MTIM_NSEC(stbuf) ((stbuf)->st_mtimespec.tv_nsec) +#define ST_ATIM_NSEC_SET(stbuf, val) (stbuf)->st_atimespec.tv_nsec = (val) +#define ST_MTIM_NSEC_SET(stbuf, val) (stbuf)->st_mtimespec.tv_nsec = (val) +#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC) +#define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atimensec) +#define ST_CTIM_NSEC(stbuf) ((stbuf)->st_ctimensec) +#define ST_MTIM_NSEC(stbuf) ((stbuf)->st_mtimensec) +#define ST_ATIM_NSEC_SET(stbuf, val) (stbuf)->st_atimensec = (val) +#define ST_MTIM_NSEC_SET(stbuf, val) (stbuf)->st_mtimensec = (val) +#else +#define ST_ATIM_NSEC(stbuf) 0 +#define ST_CTIM_NSEC(stbuf) 0 +#define ST_MTIM_NSEC(stbuf) 0 +#define ST_ATIM_NSEC_SET(stbuf, val) do { } while (0) +#define ST_MTIM_NSEC_SET(stbuf, val) do { } while (0) +#endif diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_opt.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_opt.c new file mode 100644 index 0000000..80b0777 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_opt.c @@ -0,0 +1,382 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include "fuse_opt.h" + +#include +#include +#include +#include + +struct fuse_opt_context { + void *data; + const struct fuse_opt *opt; + fuse_opt_proc_t proc; + int argctr; + int argc; + char **argv; + struct fuse_args outargs; + char *opts; + int nonopt; +}; + +void fuse_opt_free_args(struct fuse_args *args) +{ + if (args) { + if (args->argv && args->allocated) { + int i; + for (i = 0; i < args->argc; i++) + free(args->argv[i]); + free(args->argv); + } + args->argc = 0; + args->argv = NULL; + args->allocated = 0; + } +} + +static int alloc_failed(void) +{ + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; +} + +int fuse_opt_add_arg(struct fuse_args *args, const char *arg) +{ + char **newargv; + char *newarg; + + assert(!args->argv || args->allocated); + + newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *)); + newarg = newargv ? strdup(arg) : NULL; + if (!newargv || !newarg) + return alloc_failed(); + + args->argv = newargv; + args->allocated = 1; + args->argv[args->argc++] = newarg; + args->argv[args->argc] = NULL; + return 0; +} + +int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg) +{ + assert(pos <= args->argc); + if (fuse_opt_add_arg(args, arg) == -1) + return -1; + + if (pos != args->argc - 1) { + char *newarg = args->argv[args->argc - 1]; + memmove(&args->argv[pos + 1], &args->argv[pos], + sizeof(char *) * (args->argc - pos - 1)); + args->argv[pos] = newarg; + } + return 0; +} + +static int next_arg(struct fuse_opt_context *ctx, const char *opt) +{ + if (ctx->argctr + 1 >= ctx->argc) { + fprintf(stderr, "fuse: missing argument after `%s'\n", opt); + return -1; + } + ctx->argctr++; + return 0; +} + +static int add_arg(struct fuse_opt_context *ctx, const char *arg) +{ + return fuse_opt_add_arg(&ctx->outargs, arg); +} + +int fuse_opt_add_opt(char **opts, const char *opt) +{ + char *newopts; + if (!*opts) + newopts = strdup(opt); + else { + unsigned oldlen = strlen(*opts); + newopts = realloc(*opts, oldlen + 1 + strlen(opt) + 1); + if (newopts) { + newopts[oldlen] = ','; + strcpy(newopts + oldlen + 1, opt); + } + } + if (!newopts) + return alloc_failed(); + + *opts = newopts; + return 0; +} + +static int add_opt(struct fuse_opt_context *ctx, const char *opt) +{ + return fuse_opt_add_opt(&ctx->opts, opt); +} + +static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key, + int iso) +{ + if (key == FUSE_OPT_KEY_DISCARD) + return 0; + + if (key != FUSE_OPT_KEY_KEEP && ctx->proc) { + int res = ctx->proc(ctx->data, arg, key, &ctx->outargs); + if (res == -1 || !res) + return res; + } + if (iso) + return add_opt(ctx, arg); + else + return add_arg(ctx, arg); +} + +static int match_template(const char *t, const char *arg, unsigned *sepp) +{ + int arglen = strlen(arg); + const char *sep = strchr(t, '='); + sep = sep ? sep : strchr(t, ' '); + if (sep && (!sep[1] || sep[1] == '%')) { + int tlen = sep - t; + if (sep[0] == '=') + tlen ++; + if (arglen >= tlen && strncmp(arg, t, tlen) == 0) { + *sepp = sep - t; + return 1; + } + } + if (strcmp(t, arg) == 0) { + *sepp = 0; + return 1; + } + return 0; +} + +static const struct fuse_opt *find_opt(const struct fuse_opt *opt, + const char *arg, unsigned *sepp) +{ + for (; opt && opt->templ; opt++) + if (match_template(opt->templ, arg, sepp)) + return opt; + return NULL; +} + +int fuse_opt_match(const struct fuse_opt *opts, const char *opt) +{ + unsigned dummy; + return find_opt(opts, opt, &dummy) ? 1 : 0; +} + +static int process_opt_param(void *var, const char *format, const char *param, + const char *arg) +{ + assert(format[0] == '%'); + if (format[1] == 's') { + char *copy = strdup(param); + if (!copy) + return alloc_failed(); + + *(char **) var = copy; + } else { + if (sscanf(param, format, var) != 1) { + fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg); + return -1; + } + } + return 0; +} + +static int process_opt(struct fuse_opt_context *ctx, + const struct fuse_opt *opt, unsigned sep, + const char *arg, int iso) +{ + if (opt->offset == -1U) { + if (call_proc(ctx, arg, opt->value, iso) == -1) + return -1; + } else { + void *var = (char *)ctx->data + opt->offset; + if (sep && opt->templ[sep + 1]) { + const char *param = arg + sep; + if (opt->templ[sep] == '=') + param ++; + if (process_opt_param(var, opt->templ + sep + 1, + param, arg) == -1) + return -1; + } else + *(int *)var = opt->value; + } + return 0; +} + +static int process_opt_sep_arg(struct fuse_opt_context *ctx, + const struct fuse_opt *opt, unsigned sep, + const char *arg, int iso) +{ + int res; + char *newarg; + char *param; + + if (next_arg(ctx, arg) == -1) + return -1; + + param = ctx->argv[ctx->argctr]; + newarg = malloc(sep + strlen(param) + 1); + if (!newarg) + return alloc_failed(); + + memcpy(newarg, arg, sep); + strcpy(newarg + sep, param); + res = process_opt(ctx, opt, sep, newarg, iso); + free(newarg); + + return res; +} + +static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso) +{ + unsigned sep; + const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep); + if (opt) { + for (; opt; opt = find_opt(opt + 1, arg, &sep)) { + int res; + if (sep && opt->templ[sep] == ' ' && !arg[sep]) + res = process_opt_sep_arg(ctx, opt, sep, arg, iso); + else + res = process_opt(ctx, opt, sep, arg, iso); + if (res == -1) + return -1; + } + return 0; + } else + return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso); +} + +static int process_real_option_group(struct fuse_opt_context *ctx, char *opts) +{ + char *sep; + + do { + int res; +#ifdef __SOLARIS__ + /* + * On Solaris, the device name contains commas, so the + * option "fsname" has to be the last one and its commas + * should not be interpreted as option separators. + * This had to be hardcoded because the option "fsname" + * may be found though not present in option list. + */ + if (!strncmp(opts,"fsname=",7)) + sep = (char*)NULL; + else +#endif /* __SOLARIS__ */ + { + sep = strchr(opts, ','); + if (sep) + *sep = '\0'; + } + res = process_gopt(ctx, opts, 1); + if (res == -1) + return -1; + opts = sep + 1; + } while (sep); + + return 0; +} + +static int process_option_group(struct fuse_opt_context *ctx, const char *opts) +{ + int res; + char *copy; + const char *sep = strchr(opts, ','); + if (!sep) + return process_gopt(ctx, opts, 1); + + copy = strdup(opts); + if (!copy) { + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; + } + res = process_real_option_group(ctx, copy); + free(copy); + return res; +} + +static int process_one(struct fuse_opt_context *ctx, const char *arg) +{ + if (ctx->nonopt || arg[0] != '-') + return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0); + else if (arg[1] == 'o') { + if (arg[2]) + return process_option_group(ctx, arg + 2); + else { + if (next_arg(ctx, arg) == -1) + return -1; + + return process_option_group(ctx, ctx->argv[ctx->argctr]); + } + } else if (arg[1] == '-' && !arg[2]) { + if (add_arg(ctx, arg) == -1) + return -1; + ctx->nonopt = ctx->outargs.argc; + return 0; + } else + return process_gopt(ctx, arg, 0); +} + +static int opt_parse(struct fuse_opt_context *ctx) +{ + if (ctx->argc) { + if (add_arg(ctx, ctx->argv[0]) == -1) + return -1; + } + + for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++) + if (process_one(ctx, ctx->argv[ctx->argctr]) == -1) + return -1; + + if (ctx->opts) { + if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 || + fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1) + return -1; + } + if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc) { + free(ctx->outargs.argv[ctx->outargs.argc - 1]); + ctx->outargs.argv[--ctx->outargs.argc] = NULL; + } + + return 0; +} + +int fuse_opt_parse(struct fuse_args *args, void *data, + const struct fuse_opt opts[], fuse_opt_proc_t proc) +{ + int res; + struct fuse_opt_context ctx = { + .data = data, + .opt = opts, + .proc = proc, + }; + + if (!args || !args->argv || !args->argc) + return 0; + + ctx.argc = args->argc; + ctx.argv = args->argv; + + res = opt_parse(&ctx); + if (res != -1) { + struct fuse_args tmp = *args; + *args = ctx.outargs; + ctx.outargs = tmp; + } + free(ctx.opts); + fuse_opt_free_args(&ctx.outargs); + return res; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_session.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_session.c new file mode 100644 index 0000000..3773303 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_session.c @@ -0,0 +1,193 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include "fuse_lowlevel.h" +#include "fuse_lowlevel_compat.h" + +#include +#include +#include +#include +#include + +struct fuse_session { + struct fuse_session_ops op; + + void *data; + + volatile int exited; + + struct fuse_chan *ch; +}; + +struct fuse_chan { + struct fuse_chan_ops op; + + struct fuse_session *se; + + int fd; + + size_t bufsize; + + void *data; +}; + +struct fuse_session *fuse_session_new(struct fuse_session_ops *op, void *data) +{ + struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se)); + if (se == NULL) { + fprintf(stderr, "fuse: failed to allocate session\n"); + return NULL; + } + + memset(se, 0, sizeof(*se)); + se->op = *op; + se->data = data; + + return se; +} + +void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch) +{ + assert(se->ch == NULL); + assert(ch->se == NULL); + se->ch = ch; + ch->se = se; +} + +void fuse_session_remove_chan(struct fuse_chan *ch) +{ + struct fuse_session *se = ch->se; + if (se) { + assert(se->ch == ch); + se->ch = NULL; + ch->se = NULL; + } +} + +struct fuse_chan *fuse_session_next_chan(struct fuse_session *se, + struct fuse_chan *ch) +{ + assert(ch == NULL || ch == se->ch); + if (ch == NULL) + return se->ch; + else + return NULL; +} + +void fuse_session_process(struct fuse_session *se, const char *buf, size_t len, + struct fuse_chan *ch) +{ + se->op.process(se->data, buf, len, ch); +} + +void fuse_session_destroy(struct fuse_session *se) +{ + if (se->op.destroy) + se->op.destroy(se->data); + if (se->ch != NULL) + fuse_chan_destroy(se->ch); + free(se); +} + +void fuse_session_exit(struct fuse_session *se) +{ + if (se->op.exit) + se->op.exit(se->data, 1); + se->exited = 1; +} + +void fuse_session_reset(struct fuse_session *se) +{ + if (se->op.exit) + se->op.exit(se->data, 0); + se->exited = 0; +} + +int fuse_session_exited(struct fuse_session *se) +{ + if (se->op.exited) + return se->op.exited(se->data); + else + return se->exited; +} + +static struct fuse_chan *fuse_chan_new_common(struct fuse_chan_ops *op, int fd, + size_t bufsize, void *data) +{ + struct fuse_chan *ch = (struct fuse_chan *) malloc(sizeof(*ch)); + if (ch == NULL) { + fprintf(stderr, "fuse: failed to allocate channel\n"); + return NULL; + } + + memset(ch, 0, sizeof(*ch)); + ch->op = *op; + ch->fd = fd; + ch->bufsize = bufsize; + ch->data = data; + + return ch; +} + +struct fuse_chan *fuse_chan_new(struct fuse_chan_ops *op, int fd, + size_t bufsize, void *data) +{ + return fuse_chan_new_common(op, fd, bufsize, data); +} + +int fuse_chan_fd(struct fuse_chan *ch) +{ + return ch->fd; +} + +size_t fuse_chan_bufsize(struct fuse_chan *ch) +{ + return ch->bufsize; +} + +void *fuse_chan_data(struct fuse_chan *ch) +{ + return ch->data; +} + +struct fuse_session *fuse_chan_session(struct fuse_chan *ch) +{ + return ch->se; +} + +int fuse_chan_recv(struct fuse_chan **chp, char *buf, size_t size) +{ + struct fuse_chan *ch = *chp; + + return ch->op.receive(chp, buf, size); +} + +#ifdef __SOLARIS__ +int fuse_chan_receive(struct fuse_chan *ch, char *buf, size_t size) +{ + int res; + + res = fuse_chan_recv(&ch, buf, size); + return res >= 0 ? res : (res != -EINTR && res != -EAGAIN) ? -1 : 0; +} +#endif /* __SOLARIS__ */ + +int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[], size_t count) +{ + return ch->op.send(ch, iov, count); +} + +void fuse_chan_destroy(struct fuse_chan *ch) +{ + fuse_session_remove_chan(ch); + if (ch->op.destroy) + ch->op.destroy(ch); + free(ch); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_signals.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_signals.c new file mode 100644 index 0000000..d5b3d09 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fuse_signals.c @@ -0,0 +1,73 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include "fuse_lowlevel.h" + +#include +#include +#include + +static struct fuse_session *fuse_instance; + +static void exit_handler(int sig) +{ + (void) sig; + if (fuse_instance) + fuse_session_exit(fuse_instance); +} + +static int set_one_signal_handler(int sig, void (*handler)(int), int remove) +{ + struct sigaction sa; + struct sigaction old_sa; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = remove ? SIG_DFL : handler; + sigemptyset(&(sa.sa_mask)); + sa.sa_flags = 0; + + if (sigaction(sig, NULL, &old_sa) == -1) { + perror("fuse: cannot get old signal handler"); + return -1; + } + + if (old_sa.sa_handler == (remove ? handler : SIG_DFL) && + sigaction(sig, &sa, NULL) == -1) { + perror("fuse: cannot set signal handler"); + return -1; + } + return 0; +} + +int fuse_set_signal_handlers(struct fuse_session *se) +{ + if (set_one_signal_handler(SIGHUP, exit_handler, 0) == -1 || + set_one_signal_handler(SIGINT, exit_handler, 0) == -1 || + set_one_signal_handler(SIGTERM, exit_handler, 0) == -1 || + set_one_signal_handler(SIGPIPE, SIG_IGN, 0) == -1) + return -1; + + fuse_instance = se; + return 0; +} + +void fuse_remove_signal_handlers(struct fuse_session *se) +{ + if (fuse_instance != se) + fprintf(stderr, + "fuse: fuse_remove_signal_handlers: unknown session\n"); + else + fuse_instance = NULL; + + set_one_signal_handler(SIGHUP, exit_handler, 1); + set_one_signal_handler(SIGINT, exit_handler, 1); + set_one_signal_handler(SIGTERM, exit_handler, 1); + set_one_signal_handler(SIGPIPE, SIG_IGN, 1); +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fusermount.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fusermount.c new file mode 100644 index 0000000..680fee1 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/fusermount.c @@ -0,0 +1,705 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. +*/ +/* This program does the mounting and unmounting of FUSE filesystems */ + +#include + +#include "mount_util.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __SOLARIS__ +#include +#else /* __SOLARIS__ */ +#include +#include +#include +#endif /* __SOLARIS__ */ + +#include +#include +#include +#include +#include + +#define FUSE_DEV_NEW "/dev/fuse" + +#ifndef MS_DIRSYNC +#define MS_DIRSYNC 128 +#endif + +static const char *progname = "ntfs-3g-mount"; + +static int mount_max = 1000; + +int drop_privs(void); +int restore_privs(void); + +#ifdef __SOLARIS__ + +/* + * fusermount is not implemented in fuse-lite for Solaris, + * only the minimal functions are provided. + */ + +/* + * Solaris doesn't have setfsuid/setfsgid. + * This doesn't really matter anyway as this program shouldn't be made + * suid on Solaris. It should instead be used via a profile with the + * sys_mount privilege. + */ + +int drop_privs(void) +{ + return (0); +} + +int restore_privs(void) +{ + return (0); +} + +#else /* __SOLARIS__ */ + +static const char *get_user_name(void) +{ + struct passwd *pw = getpwuid(getuid()); + if (pw != NULL && pw->pw_name != NULL) + return pw->pw_name; + else { + fprintf(stderr, "%s: could not determine username\n", progname); + return NULL; + } +} + +int drop_privs(void) +{ + if (!getegid()) { + + gid_t new_gid = getgid(); + + if (setresgid(-1, new_gid, getegid()) < 0) { + perror("priv drop: setresgid failed"); + return -1; + } + if (getegid() != new_gid){ + perror("dropping group privilege failed"); + return -1; + } + } + + if (!geteuid()) { + + uid_t new_uid = getuid(); + + if (setresuid(-1, new_uid, geteuid()) < 0) { + perror("priv drop: setresuid failed"); + return -1; + } + if (geteuid() != new_uid){ + perror("dropping user privilege failed"); + return -1; + } + } + + return 0; +} + +int restore_privs(void) +{ + if (geteuid()) { + + uid_t ruid, euid, suid; + + if (getresuid(&ruid, &euid, &suid) < 0) { + perror("priv restore: getresuid failed"); + return -1; + } + if (setresuid(-1, suid, -1) < 0) { + perror("priv restore: setresuid failed"); + return -1; + } + if (geteuid() != suid) { + perror("restoring privilege failed"); + return -1; + } + } + + if (getegid()) { + + gid_t rgid, egid, sgid; + + if (getresgid(&rgid, &egid, &sgid) < 0) { + perror("priv restore: getresgid failed"); + return -1; + } + if (setresgid(-1, sgid, -1) < 0) { + perror("priv restore: setresgid failed"); + return -1; + } + if (getegid() != sgid){ + perror("restoring group privilege failed"); + return -1; + } + } + + return 0; +} + +#ifndef IGNORE_MTAB +static int add_mount(const char *source, const char *mnt, const char *type, + const char *opts) +{ + return fuse_mnt_add_mount(progname, source, mnt, type, opts); +} + +static int count_fuse_fs(void) +{ + struct mntent *entp; + int count = 0; + const char *mtab = _PATH_MOUNTED; + FILE *fp = setmntent(mtab, "r"); + if (fp == NULL) { + fprintf(stderr, "%s: failed to open %s: %s\n", progname, mtab, + strerror(errno)); + return -1; + } + while ((entp = getmntent(fp)) != NULL) { + if (strcmp(entp->mnt_type, "fuse") == 0 || + strncmp(entp->mnt_type, "fuse.", 5) == 0) + count ++; + } + endmntent(fp); + return count; +} + + +#else /* IGNORE_MTAB */ +static int count_fuse_fs() +{ + return 0; +} + +static int add_mount(const char *source, const char *mnt, const char *type, + const char *opts) +{ + (void) source; + (void) mnt; + (void) type; + (void) opts; + return 0; +} +#endif /* IGNORE_MTAB */ + +static int begins_with(const char *s, const char *beg) +{ + if (strncmp(s, beg, strlen(beg)) == 0) + return 1; + else + return 0; +} + +struct mount_flags { + const char *opt; + unsigned long flag; + int on; + int safe; +}; + +static struct mount_flags mount_flags[] = { + {"rw", MS_RDONLY, 0, 1}, + {"ro", MS_RDONLY, 1, 1}, + {"suid", MS_NOSUID, 0, 0}, + {"nosuid", MS_NOSUID, 1, 1}, + {"dev", MS_NODEV, 0, 0}, + {"nodev", MS_NODEV, 1, 1}, + {"exec", MS_NOEXEC, 0, 1}, + {"noexec", MS_NOEXEC, 1, 1}, + {"async", MS_SYNCHRONOUS, 0, 1}, + {"sync", MS_SYNCHRONOUS, 1, 1}, + {"atime", MS_NOATIME, 0, 1}, + {"noatime", MS_NOATIME, 1, 1}, + {"dirsync", MS_DIRSYNC, 1, 1}, + {NULL, 0, 0, 0} +}; + +static int find_mount_flag(const char *s, unsigned len, int *on, int *flag) +{ + int i; + + for (i = 0; mount_flags[i].opt != NULL; i++) { + const char *opt = mount_flags[i].opt; + if (strlen(opt) == len && strncmp(opt, s, len) == 0) { + *on = mount_flags[i].on; + *flag = mount_flags[i].flag; + if (!mount_flags[i].safe && getuid() != 0) { + *flag = 0; + fprintf(stderr, "%s: unsafe option '%s' ignored\n", + progname, opt); + } + return 1; + } + } + return 0; +} + +static int add_option(char **optsp, const char *opt, unsigned expand) +{ + char *newopts; + if (*optsp == NULL) + newopts = strdup(opt); + else { + unsigned oldsize = strlen(*optsp); + unsigned newsize = oldsize + 1 + strlen(opt) + expand + 1; + newopts = (char *) realloc(*optsp, newsize); + if (newopts) + sprintf(newopts + oldsize, ",%s", opt); + } + if (newopts == NULL) { + fprintf(stderr, "%s: failed to allocate memory\n", progname); + return -1; + } + *optsp = newopts; + return 0; +} + +static int get_mnt_opts(int flags, char *opts, char **mnt_optsp) +{ + int i; + int l; + + if (!(flags & MS_RDONLY) && add_option(mnt_optsp, "rw", 0) == -1) + return -1; + + for (i = 0; mount_flags[i].opt != NULL; i++) { + if (mount_flags[i].on && (flags & mount_flags[i].flag) && + add_option(mnt_optsp, mount_flags[i].opt, 0) == -1) + return -1; + } + + if (add_option(mnt_optsp, opts, 0) == -1) + return -1; + /* remove comma from end of opts*/ + l = strlen(*mnt_optsp); + if ((*mnt_optsp)[l-1] == ',') + (*mnt_optsp)[l-1] = '\0'; + if (getuid() != 0) { + const char *user = get_user_name(); + if (user == NULL) + return -1; + + if (add_option(mnt_optsp, "user=", strlen(user)) == -1) + return -1; + strcat(*mnt_optsp, user); + } + return 0; +} + +static int opt_eq(const char *s, unsigned len, const char *opt) +{ + if(strlen(opt) == len && strncmp(s, opt, len) == 0) + return 1; + else + return 0; +} + +static int get_string_opt(const char *s, unsigned len, const char *opt, + char **val) +{ + unsigned opt_len = strlen(opt); + + if (*val) + free(*val); + *val = (char *) malloc(len - opt_len + 1); + if (!*val) { + fprintf(stderr, "%s: failed to allocate memory\n", progname); + return 0; + } + + memcpy(*val, s + opt_len, len - opt_len); + (*val)[len - opt_len] = '\0'; + return 1; +} + +static int do_mount(const char *mnt, char **typep, mode_t rootmode, + int fd, const char *opts, const char *dev, char **sourcep, + char **mnt_optsp) +{ + int res; + int flags = MS_NOSUID | MS_NODEV; + char *optbuf; + char *mnt_opts = NULL; + const char *s; + char *d; + char *fsname = NULL; + char *source = NULL; + char *type = NULL; + int blkdev = 0; + + optbuf = (char *) malloc(strlen(opts) + 128); + if (!optbuf) { + fprintf(stderr, "%s: failed to allocate memory\n", progname); + return -1; + } + + for (s = opts, d = optbuf; *s;) { + unsigned len; + const char *fsname_str = "fsname="; + for (len = 0; s[len] && s[len] != ','; len++); + if (begins_with(s, fsname_str)) { + if (!get_string_opt(s, len, fsname_str, &fsname)) + goto err; + } else if (opt_eq(s, len, "blkdev")) { + blkdev = 1; + } else if (!begins_with(s, "fd=") && + !begins_with(s, "rootmode=") && + !begins_with(s, "user_id=") && + !begins_with(s, "group_id=")) { + int on; + int flag; + int skip_option = 0; + if (opt_eq(s, len, "large_read")) { + struct utsname utsname; + unsigned kmaj, kmin; + res = uname(&utsname); + if (res == 0 && + sscanf(utsname.release, "%u.%u", &kmaj, &kmin) == 2 && + (kmaj > 2 || (kmaj == 2 && kmin > 4))) { + fprintf(stderr, "%s: note: 'large_read' mount option is " + "deprecated for %i.%i kernels\n", progname, kmaj, kmin); + skip_option = 1; + } + } + if (!skip_option) { + if (find_mount_flag(s, len, &on, &flag)) { + if (on) + flags |= flag; + else + flags &= ~flag; + } else { + memcpy(d, s, len); + d += len; + *d++ = ','; + } + } + } + s += len; + if (*s) + s++; + } + *d = '\0'; + res = get_mnt_opts(flags, optbuf, &mnt_opts); + if (res == -1) + goto err; + + sprintf(d, "fd=%i,rootmode=%o,user_id=%i,group_id=%i", + fd, rootmode, getuid(), getgid()); + + source = malloc((fsname ? strlen(fsname) : 0) + strlen(dev) + 32); + + type = malloc(32); + if (!type || !source) { + fprintf(stderr, "%s: failed to allocate memory\n", progname); + goto err; + } + + strcpy(type, blkdev ? "fuseblk" : "fuse"); + + if (fsname) + strcpy(source, fsname); + else + strcpy(source, dev); + + if (restore_privs()) + goto err; + + res = mount(source, mnt, type, flags, optbuf); + if (res == -1 && errno == EINVAL) { + /* It could be an old version not supporting group_id */ + sprintf(d, "fd=%i,rootmode=%o,user_id=%i", fd, rootmode, getuid()); + res = mount(source, mnt, type, flags, optbuf); + } + + if (drop_privs()) + goto err; + + if (res == -1) { + int errno_save = errno; + if (blkdev && errno == ENODEV && !fuse_mnt_check_fuseblk()) + fprintf(stderr, "%s: 'fuseblk' support missing\n", progname); + else { + fprintf(stderr, "%s: mount failed: %s\n", progname, strerror(errno_save)); + if (errno_save == EPERM) + fprintf(stderr, "User doesn't have privilege to mount. " + "For more information\nplease see: " + "http://tuxera.com/community/ntfs-3g-faq/#unprivileged\n"); + } + goto err; + } else { + *sourcep = source; + *typep = type; + *mnt_optsp = mnt_opts; + } +out: + free(fsname); + free(optbuf); + return res; +err: + free(source); + free(type); + free(mnt_opts); + res = -1; + goto out; +} + +static int check_perm(const char **mntp, struct stat *stbuf, int *currdir_fd, + int *mountpoint_fd) +{ + int res; + const char *mnt = *mntp; + const char *origmnt = mnt; + + res = stat(mnt, stbuf); + if (res == -1) { + fprintf(stderr, "%s: failed to access mountpoint %s: %s\n", + progname, mnt, strerror(errno)); + return -1; + } + + /* No permission checking is done for root */ + if (getuid() == 0) + return 0; + + if (S_ISDIR(stbuf->st_mode)) { + *currdir_fd = open(".", O_RDONLY); + if (*currdir_fd == -1) { + fprintf(stderr, "%s: failed to open current directory: %s\n", + progname, strerror(errno)); + return -1; + } + res = chdir(mnt); + if (res == -1) { + fprintf(stderr, "%s: failed to chdir to mountpoint: %s\n", + progname, strerror(errno)); + return -1; + } + mnt = *mntp = "."; + res = lstat(mnt, stbuf); + if (res == -1) { + fprintf(stderr, "%s: failed to access mountpoint %s: %s\n", + progname, origmnt, strerror(errno)); + return -1; + } + + if ((stbuf->st_mode & S_ISVTX) && stbuf->st_uid != getuid()) { + fprintf(stderr, "%s: mountpoint %s not owned by user\n", + progname, origmnt); + return -1; + } + + res = access(mnt, W_OK); + if (res == -1) { + fprintf(stderr, "%s: user has no write access to mountpoint %s\n", + progname, origmnt); + return -1; + } + } else if (S_ISREG(stbuf->st_mode)) { + static char procfile[256]; + *mountpoint_fd = open(mnt, O_WRONLY); + if (*mountpoint_fd == -1) { + fprintf(stderr, "%s: failed to open %s: %s\n", progname, mnt, + strerror(errno)); + return -1; + } + res = fstat(*mountpoint_fd, stbuf); + if (res == -1) { + fprintf(stderr, "%s: failed to access mountpoint %s: %s\n", + progname, mnt, strerror(errno)); + return -1; + } + if (!S_ISREG(stbuf->st_mode)) { + fprintf(stderr, "%s: mountpoint %s is no longer a regular file\n", + progname, mnt); + return -1; + } + + sprintf(procfile, "/proc/self/fd/%i", *mountpoint_fd); + *mntp = procfile; + } else { + fprintf(stderr, + "%s: mountpoint %s is not a directory or a regular file\n", + progname, mnt); + return -1; + } + + + return 0; +} + +static int try_open(const char *dev, char **devp) +{ + int fd; + + if (restore_privs()) + return -1; + fd = open(dev, O_RDWR); + if (drop_privs()) + return -1; + if (fd != -1) { + *devp = strdup(dev); + if (*devp == NULL) { + fprintf(stderr, "%s: failed to allocate memory\n", progname); + close(fd); + fd = -1; + } + } else if (errno == ENODEV || + errno == ENOENT) /* check for ENOENT too, for the udev case */ + return -2; + else { + fprintf(stderr, "%s: failed to open %s: %s\n", progname, dev, + strerror(errno)); + } + return fd; +} + +static int open_fuse_device(char **devp) +{ + int fd; + + fd = try_open(FUSE_DEV_NEW, devp); + if (fd >= -1) + return fd; + + fprintf(stderr, "%s: fuse device is missing, try 'modprobe fuse' as root\n", + progname); + + return -1; +} + + +static int mount_fuse(const char *mnt, const char *opts) +{ + int res; + int fd; + char *dev; + struct stat stbuf; + char *type = NULL; + char *source = NULL; + char *mnt_opts = NULL; + const char *real_mnt = mnt; + int currdir_fd = -1; + int mountpoint_fd = -1; + + fd = open_fuse_device(&dev); + if (fd == -1) + return -1; + + if (getuid() != 0 && mount_max != -1) { + if (count_fuse_fs() >= mount_max) { + fprintf(stderr, "%s: too many mounted FUSE filesystems (%d+)\n", + progname, mount_max); + goto err; + } + } + + res = check_perm(&real_mnt, &stbuf, &currdir_fd, &mountpoint_fd); + if (res != -1) + res = do_mount(real_mnt, &type, stbuf.st_mode & S_IFMT, fd, opts, dev, + &source, &mnt_opts); + + if (currdir_fd != -1) { + __attribute__((unused))int ignored_fchdir_status = + fchdir(currdir_fd); + close(currdir_fd); + } + if (mountpoint_fd != -1) + close(mountpoint_fd); + + if (res == -1) + goto err; + + if (restore_privs()) + goto err; + + if (geteuid() == 0) { + + if (setgroups(0, NULL) == -1) { + perror("priv drop: setgroups failed"); + goto err; + } + + res = add_mount(source, mnt, type, mnt_opts); + if (res == -1) { + umount2(mnt, 2); /* lazy umount */ + drop_privs(); + goto err; + } + } + + if (drop_privs()) + goto err; +out: + free(source); + free(type); + free(mnt_opts); + free(dev); + + return fd; +err: + close(fd); + fd = -1; + goto out; +} + +int fusermount(int unmount, int quiet, int lazy, const char *opts, + const char *origmnt) +{ + int res = -1; + char *mnt; + mode_t old_umask; + + mnt = fuse_mnt_resolve_path(progname, origmnt); + if (mnt == NULL) + return -1; + + old_umask = umask(033); + + if (unmount) { + + if (restore_privs()) + goto out; + + if (geteuid() == 0) + res = fuse_mnt_umount(progname, mnt, lazy); + else { + res = umount2(mnt, lazy ? 2 : 0); + if (res == -1 && !quiet) + fprintf(stderr, "%s: failed to unmount %s: %s\n", progname, + mnt, strerror(errno)); + } + + if (drop_privs()) + res = -1; + + } else + res = mount_fuse(mnt, opts); +out: + umask(old_umask); + free(mnt); + return res; +} + +#endif /* __SOLARIS__ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/helper.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/helper.c new file mode 100644 index 0000000..ebd7d77 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/helper.c @@ -0,0 +1,61 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "config.h" +#include "fuse_i.h" +#include "fuse_lowlevel.h" + +struct fuse_chan *fuse_mount(const char *mountpoint, struct fuse_args *args) +{ + struct fuse_chan *ch; + int fd; + +#ifdef __SOLARIS__ + /* + * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos + * would ensue. + */ + do { + fd = open("/dev/null", O_RDWR); + if (fd > 2) + close(fd); + } while (fd >= 0 && fd <= 2); +#endif /* __SOLARIS__ */ + + fd = fuse_kern_mount(mountpoint, args); + if (fd == -1) + return NULL; + + ch = fuse_kern_chan_new(fd); + if (!ch) + fuse_kern_unmount(mountpoint, fd); + + return ch; +} + +void fuse_unmount(const char *mountpoint, struct fuse_chan *ch) +{ + int fd = ch ? fuse_chan_fd(ch) : -1; + fuse_kern_unmount(mountpoint, fd); + fuse_chan_destroy(ch); +} + +int fuse_version(void) +{ + return FUSE_VERSION; +} + +#ifdef __SOLARIS__ +#undef fuse_main +int fuse_main(void); +int fuse_main(void) +{ + fprintf(stderr, "fuse_main(): This function does not exist\n"); + return -1; +} +#endif /* __SOLARIS__ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/mount.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/mount.c new file mode 100644 index 0000000..6ae29d8 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/mount.c @@ -0,0 +1,734 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "config.h" +#include "fuse_i.h" +#include "fuse_opt.h" +#include "mount_util.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __SOLARIS__ + +#define FUSERMOUNT_PROG "fusermount" +#define FUSE_COMMFD_ENV "_FUSE_COMMFD" + +#ifndef FUSERMOUNT_DIR +#define FUSERMOUNT_DIR "/usr" +#endif /* FUSERMOUNT_DIR */ + +#ifndef HAVE_FORK +#define fork() vfork() +#endif + +#endif /* __SOLARIS__ */ + +#ifndef MS_DIRSYNC +#define MS_DIRSYNC 128 +#endif + +enum { + KEY_KERN_FLAG, + KEY_KERN_OPT, + KEY_FUSERMOUNT_OPT, + KEY_SUBTYPE_OPT, + KEY_MTAB_OPT, + KEY_ALLOW_ROOT, + KEY_RO, + KEY_HELP, + KEY_VERSION, +}; + +struct mount_opts { + int allow_other; + int allow_root; + int ishelp; + int flags; +#ifdef __SOLARIS__ + int nonempty; + int blkdev; + char *fsname; + char *subtype; + char *subtype_opt; +#else + int blkdev; + char *fsname; +#endif + char *mtab_opts; + char *fusermount_opts; + char *kernel_opts; +}; + +#define FUSE_MOUNT_OPT(t, p) { t, offsetof(struct mount_opts, p), 1 } + +static const struct fuse_opt fuse_mount_opts[] = { +#ifdef __SOLARIS__ + FUSE_MOUNT_OPT("allow_other", allow_other), + FUSE_MOUNT_OPT("allow_root", allow_root), + FUSE_MOUNT_OPT("nonempty", nonempty), + FUSE_MOUNT_OPT("blkdev", blkdev), + FUSE_MOUNT_OPT("fsname=%s", fsname), + FUSE_MOUNT_OPT("subtype=%s", subtype), + FUSE_OPT_KEY("allow_other", KEY_KERN_OPT), + FUSE_OPT_KEY("allow_root", KEY_ALLOW_ROOT), + FUSE_OPT_KEY("nonempty", KEY_FUSERMOUNT_OPT), + FUSE_OPT_KEY("blkdev", KEY_FUSERMOUNT_OPT), + FUSE_OPT_KEY("fsname=", KEY_FUSERMOUNT_OPT), + FUSE_OPT_KEY("subtype=", KEY_SUBTYPE_OPT), + FUSE_OPT_KEY("large_read", KEY_KERN_OPT), + FUSE_OPT_KEY("blksize=", KEY_KERN_OPT), + FUSE_OPT_KEY("default_permissions", KEY_KERN_OPT), + FUSE_OPT_KEY("max_read=", KEY_KERN_OPT), + FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP), + FUSE_OPT_KEY("user=", KEY_MTAB_OPT), + FUSE_OPT_KEY("-r", KEY_RO), + FUSE_OPT_KEY("ro", KEY_KERN_FLAG), + FUSE_OPT_KEY("rw", KEY_KERN_FLAG), + FUSE_OPT_KEY("suid", KEY_KERN_FLAG), + FUSE_OPT_KEY("nosuid", KEY_KERN_FLAG), + FUSE_OPT_KEY("-g", KEY_KERN_FLAG), + FUSE_OPT_KEY("-m", KEY_KERN_FLAG), + FUSE_OPT_KEY("-O", KEY_KERN_FLAG), + FUSE_OPT_KEY("setuid", KEY_KERN_OPT), + FUSE_OPT_KEY("nosetuid", KEY_KERN_OPT), + FUSE_OPT_KEY("devices", KEY_KERN_OPT), + FUSE_OPT_KEY("nodevices", KEY_KERN_OPT), + FUSE_OPT_KEY("exec", KEY_KERN_OPT), + FUSE_OPT_KEY("noexec", KEY_KERN_OPT), + FUSE_OPT_KEY("nbmand", KEY_KERN_OPT), + FUSE_OPT_KEY("nonbmand", KEY_KERN_OPT), +#else /* __SOLARIS__ */ + FUSE_MOUNT_OPT("allow_other", allow_other), + FUSE_MOUNT_OPT("allow_root", allow_root), + FUSE_MOUNT_OPT("blkdev", blkdev), + FUSE_MOUNT_OPT("fsname=%s", fsname), + FUSE_OPT_KEY("allow_other", KEY_KERN_OPT), + FUSE_OPT_KEY("allow_root", KEY_ALLOW_ROOT), + FUSE_OPT_KEY("blkdev", KEY_FUSERMOUNT_OPT), + FUSE_OPT_KEY("fsname=", KEY_FUSERMOUNT_OPT), + FUSE_OPT_KEY("large_read", KEY_KERN_OPT), + FUSE_OPT_KEY("blksize=", KEY_KERN_OPT), + FUSE_OPT_KEY("default_permissions", KEY_KERN_OPT), + FUSE_OPT_KEY("context=", KEY_KERN_OPT), + FUSE_OPT_KEY("max_read=", KEY_KERN_OPT), + FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP), + FUSE_OPT_KEY("user=", KEY_MTAB_OPT), + FUSE_OPT_KEY("-r", KEY_RO), + FUSE_OPT_KEY("ro", KEY_KERN_FLAG), + FUSE_OPT_KEY("rw", KEY_KERN_FLAG), + FUSE_OPT_KEY("suid", KEY_KERN_FLAG), + FUSE_OPT_KEY("nosuid", KEY_KERN_FLAG), + FUSE_OPT_KEY("dev", KEY_KERN_FLAG), + FUSE_OPT_KEY("nodev", KEY_KERN_FLAG), + FUSE_OPT_KEY("exec", KEY_KERN_FLAG), + FUSE_OPT_KEY("noexec", KEY_KERN_FLAG), + FUSE_OPT_KEY("async", KEY_KERN_FLAG), + FUSE_OPT_KEY("sync", KEY_KERN_FLAG), + FUSE_OPT_KEY("dirsync", KEY_KERN_FLAG), + FUSE_OPT_KEY("atime", KEY_KERN_FLAG), + FUSE_OPT_KEY("noatime", KEY_KERN_FLAG), +#endif /* __SOLARIS__ */ + FUSE_OPT_KEY("-h", KEY_HELP), + FUSE_OPT_KEY("--help", KEY_HELP), + FUSE_OPT_KEY("-V", KEY_VERSION), + FUSE_OPT_KEY("--version", KEY_VERSION), + FUSE_OPT_END +}; + +#ifdef __SOLARIS__ + +static void mount_help(void) +{ + fprintf(stderr, + " -o allow_other allow access to other users\n" + " -o allow_root allow access to root\n" + " -o nonempty allow mounts over non-empty file/dir\n" + " -o default_permissions enable permission checking by kernel\n" + " -o fsname=NAME set filesystem name\n" + " -o subtype=NAME set filesystem type\n" + " -o large_read issue large read requests (2.4 only)\n" + " -o max_read=N set maximum size of read requests\n" + "\n" + ); +} + +static void exec_fusermount(const char *argv[]) +{ + execv(FUSERMOUNT_DIR "/" FUSERMOUNT_PROG, (char **) argv); + execvp(FUSERMOUNT_PROG, (char **) argv); +} + +static void mount_version(void) +{ + int pid = fork(); + if (!pid) { + const char *argv[] = { FUSERMOUNT_PROG, "--version", NULL }; + exec_fusermount(argv); + _exit(1); + } else if (pid != -1) + waitpid(pid, NULL, 0); +} + +#endif /* __SOLARIS__ */ + +struct mount_flags { + const char *opt; + unsigned long flag; + int on; +}; + +static struct mount_flags mount_flags[] = { + {"rw", MS_RDONLY, 0}, + {"ro", MS_RDONLY, 1}, + {"suid", MS_NOSUID, 0}, + {"nosuid", MS_NOSUID, 1}, +#ifndef __SOLARIS__ + {"dev", MS_NODEV, 0}, + {"nodev", MS_NODEV, 1}, + {"exec", MS_NOEXEC, 0}, + {"noexec", MS_NOEXEC, 1}, + {"async", MS_SYNCHRONOUS, 0}, + {"sync", MS_SYNCHRONOUS, 1}, + {"atime", MS_NOATIME, 0}, + {"noatime", MS_NOATIME, 1}, + {"dirsync", MS_DIRSYNC, 1}, +#else /* __SOLARIS__ */ + {"-g", MS_GLOBAL, 1}, /* 1eaf4 */ + {"-m", MS_NOMNTTAB, 1}, /* 1eb00 */ + {"-O", MS_OVERLAY, 1}, /* 1eb0c */ +#endif /* __SOLARIS__ */ + {NULL, 0, 0} +}; + +#ifdef __SOLARIS__ + +/* + * See comments in fuse_kern_mount() + */ +struct solaris_mount_opts { + int nosuid; + int setuid; + int nosetuid; + int devices; + int nodevices; +}; + +#define SOLARIS_MOUNT_OPT(t, p, n) \ + { t, offsetof(struct solaris_mount_opts, p), n } +static const struct fuse_opt solaris_mnt_opts[] = { + SOLARIS_MOUNT_OPT("suid", setuid, 1), + SOLARIS_MOUNT_OPT("suid", devices, 1), + SOLARIS_MOUNT_OPT("nosuid", nosuid, 1), + SOLARIS_MOUNT_OPT("setuid", setuid, 1), + SOLARIS_MOUNT_OPT("nosetuid", nosetuid, 1), + SOLARIS_MOUNT_OPT("devices", devices, 1), + SOLARIS_MOUNT_OPT("nodevices", nodevices, 1), + FUSE_OPT_END +}; + +#endif /* __SOLARIS__ */ + +static void set_mount_flag(const char *s, int *flags) +{ + int i; + + for (i = 0; mount_flags[i].opt != NULL; i++) { + const char *opt = mount_flags[i].opt; + if (strcmp(opt, s) == 0) { + if (mount_flags[i].on) + *flags |= mount_flags[i].flag; + else + *flags &= ~mount_flags[i].flag; + return; + } + } + fprintf(stderr, "fuse: internal error, can't find mount flag\n"); + abort(); +} + +static int fuse_mount_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + struct mount_opts *mo = data; + + switch (key) { + case KEY_ALLOW_ROOT: + if (fuse_opt_add_opt(&mo->kernel_opts, "allow_other") == -1 || + fuse_opt_add_arg(outargs, "-oallow_root") == -1) + return -1; + return 0; + + case KEY_RO: + arg = "ro"; + /* fall through */ + case KEY_KERN_FLAG: + set_mount_flag(arg, &mo->flags); + return 0; + + case KEY_KERN_OPT: + return fuse_opt_add_opt(&mo->kernel_opts, arg); + + case KEY_FUSERMOUNT_OPT: + return fuse_opt_add_opt(&mo->fusermount_opts, arg); + +#ifdef __SOLARIS__ + case KEY_SUBTYPE_OPT: + return fuse_opt_add_opt(&mo->subtype_opt, arg); +#endif /* __SOLARIS__ */ + + case KEY_MTAB_OPT: + return fuse_opt_add_opt(&mo->mtab_opts, arg); + + case KEY_HELP: +#ifdef __SOLARIS__ + mount_help(); +#endif /* __SOLARIS__ */ + mo->ishelp = 1; + break; + + case KEY_VERSION: +#ifdef __SOLARIS__ + mount_version(); +#endif /* __SOLARIS__ */ + mo->ishelp = 1; + break; + } + return 1; +} + +#ifdef __SOLARIS__ + +/* return value: + * >= 0 => fd + * -1 => error + */ +static int receive_fd(int fd) +{ + struct msghdr msg; + struct iovec iov; + char buf[1]; + int rv; + size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)]; + struct cmsghdr *cmsg; + + iov.iov_base = buf; + iov.iov_len = 1; + + msg.msg_name = 0; + msg.msg_namelen = 0; + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + /* old BSD implementations should use msg_accrights instead of + * msg_control; the interface is different. */ + msg.msg_control = ccmsg; + msg.msg_controllen = sizeof(ccmsg); + + while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR); + if (rv == -1) { + perror("recvmsg"); + return -1; + } + if(!rv) { + /* EOF */ + return -1; + } + + cmsg = CMSG_FIRSTHDR(&msg); + if (cmsg->cmsg_type != SCM_RIGHTS) { + fprintf(stderr, "got control message of unknown type %d\n", + cmsg->cmsg_type); + return -1; + } + return *(int*)CMSG_DATA(cmsg); +} + +#endif /* __SOLARIS__ */ + +void fuse_kern_unmount(const char *mountpoint, int fd) +{ + int res; +#ifdef __SOLARIS__ + int pid; +#endif /* __SOLARIS__ */ + + if (!mountpoint) + return; + + if (fd != -1) { + struct pollfd pfd; + + pfd.fd = fd; + pfd.events = 0; + res = poll(&pfd, 1, 0); + /* If file poll returns POLLERR on the device file descriptor, + then the filesystem is already unmounted */ + if (res == 1 && (pfd.revents & POLLERR)) + return; + /* + * Need to close file descriptor, otherwise synchronous umount + * would recurse into filesystem, and deadlock. + */ + close(fd); + } +#ifndef __SOLARIS__ + fusermount(1, 0, 1, "", mountpoint); +#else /* __SOLARIS__ */ + if (geteuid() == 0) { + fuse_mnt_umount("fuse", mountpoint, 1); + return; + } + + res = umount2(mountpoint, 2); + if (res == 0) + return; + + pid = fork(); + if(pid == -1) + return; + + if(pid == 0) { + const char *argv[] = + { FUSERMOUNT_PROG, "-u", "-q", "-z", "--", mountpoint, NULL }; + + exec_fusermount(argv); + _exit(1); + } + waitpid(pid, NULL, 0); +#endif /* __SOLARIS__ */ +} + +#ifdef __SOLARIS__ + +static int fuse_mount_fusermount(const char *mountpoint, const char *opts, + int quiet) +{ + int fds[2], pid; + int res; + int rv; + + if (!mountpoint) { + fprintf(stderr, "fuse: missing mountpoint\n"); + return -1; + } + + res = socketpair(PF_UNIX, SOCK_STREAM, 0, fds); + if(res == -1) { + perror("fuse: socketpair() failed"); + return -1; + } + + pid = fork(); + if(pid == -1) { + perror("fuse: fork() failed"); + close(fds[0]); + close(fds[1]); + return -1; + } + + if(pid == 0) { + char env[10]; + const char *argv[32]; + int a = 0; + + if (quiet) { + int fd = open("/dev/null", O_RDONLY); + dup2(fd, 1); + dup2(fd, 2); + } + + argv[a++] = FUSERMOUNT_PROG; + if (opts) { + argv[a++] = "-o"; + argv[a++] = opts; + } + argv[a++] = "--"; + argv[a++] = mountpoint; + argv[a++] = NULL; + + close(fds[1]); + fcntl(fds[0], F_SETFD, 0); + snprintf(env, sizeof(env), "%i", fds[0]); + setenv(FUSE_COMMFD_ENV, env, 1); + exec_fusermount(argv); + perror("fuse: failed to exec fusermount"); + _exit(1); + } + + close(fds[0]); + rv = receive_fd(fds[1]); + close(fds[1]); + waitpid(pid, NULL, 0); /* bury zombie */ + + return rv; +} + +static int fuse_mount_sys(const char *mnt, struct mount_opts *mo, + const char *mnt_opts) +{ + char tmp[128]; + const char *devname = "/dev/fuse"; + char *source = NULL; + char *type = NULL; + struct stat stbuf; + int fd; + int res; + + if (!mnt) { + fprintf(stderr, "fuse: missing mountpoint\n"); + return -1; + } + + res = lstat(mnt, &stbuf); + if (res == -1) { + fprintf(stderr ,"fuse: failed to access mountpoint %s: %s\n", + mnt, strerror(errno)); + return -1; + } + + if (!mo->nonempty) { + res = fuse_mnt_check_empty("fuse", mnt, stbuf.st_mode, stbuf.st_size); + if (res == -1) + return -1; + } + + fd = open(devname, O_RDWR); + if (fd == -1) { + if (errno == ENODEV || errno == ENOENT) + fprintf(stderr, + "fuse: device not found, try 'modprobe fuse' first\n"); + else + fprintf(stderr, "fuse: failed to open %s: %s\n", devname, + strerror(errno)); + return -1; + } + + snprintf(tmp, sizeof(tmp), "fd=%i,rootmode=%o,user_id=%i,group_id=%i", fd, + stbuf.st_mode & S_IFMT, getuid(), getgid()); + + res = fuse_opt_add_opt(&mo->kernel_opts, tmp); + if (res == -1) + goto out_close; + + source = malloc((mo->fsname ? strlen(mo->fsname) : 0) + + (mo->subtype ? strlen(mo->subtype) : 0) + + strlen(devname) + 32); + + type = malloc((mo->subtype ? strlen(mo->subtype) : 0) + 32); + if (!type || !source) { + fprintf(stderr, "fuse: failed to allocate memory\n"); + goto out_close; + } + + strcpy(type, mo->blkdev ? "fuseblk" : "fuse"); + if (mo->subtype) { + strcat(type, "."); + strcat(type, mo->subtype); + } + strcpy(source, + mo->fsname ? mo->fsname : (mo->subtype ? mo->subtype : devname)); + + /* JPA added two final zeroes */ + res = mount(source, mnt, MS_OPTIONSTR|mo->flags, type, NULL, 0, + mo->kernel_opts, MAX_MNTOPT_STR, 0, 0); + + if (res == -1 && errno == EINVAL && mo->subtype) { + /* Probably missing subtype support */ + strcpy(type, mo->blkdev ? "fuseblk" : "fuse"); + if (mo->fsname) { + if (!mo->blkdev) + sprintf(source, "%s#%s", mo->subtype, mo->fsname); + } else { + strcpy(source, type); + } + /* JPA two null args added */ + res = mount(source, mnt, MS_OPTIONSTR|mo->flags, type, NULL, 0, + mo->kernel_opts, MAX_MNTOPT_STR, 0, 0); + } + if (res == -1) { + /* + * Maybe kernel doesn't support unprivileged mounts, in this + * case try falling back to fusermount + */ + if (errno == EPERM) { + res = -2; + } else { + int errno_save = errno; + if (mo->blkdev && errno == ENODEV && !fuse_mnt_check_fuseblk()) + fprintf(stderr, "fuse: 'fuseblk' support missing\n"); + else + fprintf(stderr, "fuse: mount failed: %s\n", + strerror(errno_save)); + } + + goto out_close; + } + + return fd; + + out_umount: + umount2(mnt, 2); /* lazy umount */ + out_close: + free(type); + free(source); + close(fd); + return res; +} + +#endif /* __SOLARIS__ */ + +static int get_mnt_flag_opts(char **mnt_optsp, int flags) +{ + int i; + + if (!(flags & MS_RDONLY) && fuse_opt_add_opt(mnt_optsp, "rw") == -1) + return -1; + + for (i = 0; mount_flags[i].opt != NULL; i++) { + if (mount_flags[i].on && (flags & mount_flags[i].flag) && + fuse_opt_add_opt(mnt_optsp, mount_flags[i].opt) == -1) + return -1; + } + return 0; +} + +int fuse_kern_mount(const char *mountpoint, struct fuse_args *args) +{ + struct mount_opts mo; + int res = -1; + char *mnt_opts = NULL; +#ifdef __SOLARIS__ + struct solaris_mount_opts smo; + struct fuse_args sa = FUSE_ARGS_INIT(0, NULL); +#endif /* __SOLARIS__ */ + + memset(&mo, 0, sizeof(mo)); +#ifndef __SOLARIS__ + if (getuid()) + mo.flags = MS_NOSUID | MS_NODEV; +#else /* __SOLARIS__ */ + mo.flags = 0; + memset(&smo, 0, sizeof(smo)); + if (args != NULL) { + while (args->argv[sa.argc] != NULL) + fuse_opt_add_arg(&sa, args->argv[sa.argc]); + } +#endif /* __SOLARIS__ */ + + if (args && + fuse_opt_parse(args, &mo, fuse_mount_opts, fuse_mount_opt_proc) == -1) +#ifndef __SOLARIS__ + return -1; +#else /* __SOLARIS__ */ + goto out; /* if SOLARIS, clean up 'sa' */ + + /* + * In Solaris, nosuid is equivalent to nosetuid + nodevices. We only + * have MS_NOSUID for mount flags (no MS_(NO)SETUID, etc.). But if + * we set that as a default, it restricts specifying just nosetuid + * or nodevices; there is no way for the user to specify setuid + + * nodevices or vice-verse. So we parse the existing options, then + * add restrictive defaults if needed. + */ + if (fuse_opt_parse(&sa, &smo, solaris_mnt_opts, NULL) == -1) + goto out; + if (smo.nosuid || (!smo.nodevices && !smo.devices + && !smo.nosetuid && !smo.setuid)) { + mo.flags |= MS_NOSUID; + } else { + /* + * Defaults; if neither nodevices|devices,nosetuid|setuid has + * been specified, add the default negative option string. If + * both have been specified (i.e., -osuid,nosuid), leave them + * alone; the last option will have precedence. + */ + if (!smo.nodevices && !smo.devices) + if (fuse_opt_add_opt(&mo.kernel_opts, "nodevices") == -1) + goto out; + if (!smo.nosetuid && !smo.setuid) + if (fuse_opt_add_opt(&mo.kernel_opts, "nosetuid") == -1) + goto out; + } +#endif /* __SOLARIS__ */ + + if (mo.allow_other && mo.allow_root) { + fprintf(stderr, "fuse: 'allow_other' and 'allow_root' options are mutually exclusive\n"); + goto out; + } + res = -1; + if (mo.ishelp) + goto out; + + if (get_mnt_flag_opts(&mnt_opts, mo.flags) == -1) + goto out; +#ifndef __SOLARIS__ + if (!(mo.flags & MS_NODEV) && fuse_opt_add_opt(&mnt_opts, "dev") == -1) + goto out; + if (!(mo.flags & MS_NOSUID) && fuse_opt_add_opt(&mnt_opts, "suid") == -1) + goto out; + if (mo.kernel_opts && fuse_opt_add_opt(&mnt_opts, mo.kernel_opts) == -1) + goto out; + if (mo.mtab_opts && fuse_opt_add_opt(&mnt_opts, mo.mtab_opts) == -1) + goto out; + if (mo.fusermount_opts && fuse_opt_add_opt(&mnt_opts, mo.fusermount_opts) < 0) + goto out; + res = fusermount(0, 0, 0, mnt_opts ? mnt_opts : "", mountpoint); +#else /* __SOLARIS__ */ + if (mo.kernel_opts && fuse_opt_add_opt(&mnt_opts, mo.kernel_opts) == -1) + goto out; + if (mo.mtab_opts && fuse_opt_add_opt(&mnt_opts, mo.mtab_opts) == -1) + goto out; + res = fuse_mount_sys(mountpoint, &mo, mnt_opts); + if (res == -2) { + if (mo.fusermount_opts && + fuse_opt_add_opt(&mnt_opts, mo.fusermount_opts) == -1) + goto out; + + if (mo.subtype) { + char *tmp_opts = NULL; + + res = -1; + if (fuse_opt_add_opt(&tmp_opts, mnt_opts) == -1 || + fuse_opt_add_opt(&tmp_opts, mo.subtype_opt) == -1) { + free(tmp_opts); + goto out; + } + + res = fuse_mount_fusermount(mountpoint, tmp_opts, 1); + free(tmp_opts); + if (res == -1) + res = fuse_mount_fusermount(mountpoint, mnt_opts, 0); + } else { + res = fuse_mount_fusermount(mountpoint, mnt_opts, 0); + } + } +#endif /* __SOLARIS__ */ + +out: + free(mnt_opts); +#ifdef __SOLARIS__ + fuse_opt_free_args(&sa); + free(mo.subtype); + free(mo.subtype_opt); +#endif /* __SOLARIS__ */ + free(mo.fsname); + free(mo.fusermount_opts); + free(mo.kernel_opts); + free(mo.mtab_opts); + return res; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/mount_util.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/mount_util.c new file mode 100644 index 0000000..8b31722 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/mount_util.c @@ -0,0 +1,476 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "config.h" +#include "mount_util.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __SOLARIS__ +#else /* __SOLARIS__ */ +#include +#include +#include +#endif /* __SOLARIS__ */ + +#ifdef __SOLARIS__ + +char *mkdtemp(char *template); + +#ifndef _PATH_MOUNTED +#define _PATH_MOUNTED "/etc/mnttab" +#endif /* _PATH_MOUNTED */ + +#ifndef IGNORE_MTAB +static int mtab_needs_update(const char *mnt) +{ + struct stat stbuf; + + /* If mtab is within new mount, don't touch it */ + if (strncmp(mnt, _PATH_MOUNTED, strlen(mnt)) == 0 && + _PATH_MOUNTED[strlen(mnt)] == '/') + return 0; + + if (lstat(_PATH_MOUNTED, &stbuf) != -1 && S_ISLNK(stbuf.st_mode)) + return 0; + + return 1; +} +#endif /* IGNORE_MTAB */ + +int fuse_mnt_add_mount(const char *progname, const char *fsname, + const char *mnt, const char *type, const char *opts) +{ + int res; + int status; + +#ifndef IGNORE_MTAB + if (!mtab_needs_update(mnt)) + return 0; +#endif /* IGNORE_MTAB */ + + res = fork(); + if (res == -1) { + fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno)); + return -1; + } + if (res == 0) { + char *env = NULL; + char templ[] = "/tmp/fusermountXXXXXX"; + char *tmp; + + setuid(geteuid()); + + /* + * hide in a directory, where mount isn't able to resolve + * fsname as a valid path + */ + tmp = mkdtemp(templ); + if (!tmp) { + fprintf(stderr, "%s: failed to create temporary directory\n", + progname); + exit(1); + } + if (chdir(tmp)) { + fprintf(stderr, "%s: failed to chdir to %s: %s\n", + progname, tmp, strerror(errno)); + exit(1); + } + rmdir(tmp); + execle("/sbin/mount", "/sbin/mount", "-F", type, "-o", opts, + fsname, mnt, NULL, &env); + fprintf(stderr, "%s: failed to execute /sbin/mount: %s\n", progname, + strerror(errno)); + exit(1); + } + res = waitpid(res, &status, 0); + if (res == -1) { + fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno)); + return -1; + } + if (status != 0) + return -1; + + return 0; +} + +int fuse_mnt_umount(const char *progname, const char *mnt, int lazy) +{ + int res; + int status; + +#ifndef IGNORE_MTAB + if (!mtab_needs_update(mnt)) + return 0; +#endif /* IGNORE_MTAB */ + + res = fork(); + if (res == -1) { + fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno)); + return -1; + } + if (res == 0) { + char *env = NULL; + + setuid(geteuid()); + if (lazy) { + execle("/sbin/umount", "/sbin/umount", mnt, + NULL, &env); + } else { + execle("/sbin/umount", "/sbin/umount", "-f", mnt, + NULL, &env); + } + fprintf(stderr, "%s: failed to execute /sbin/umount: %s\n", progname, + strerror(errno)); + exit(1); + } + res = waitpid(res, &status, 0); + if (res == -1) { + fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno)); + return -1; + } + if (status != 0) + return -1; + + return 0; +} + +char *fuse_mnt_resolve_path(const char *progname, const char *orig) +{ + char buf[PATH_MAX]; + char *copy; + char *dst; + char *end; + char *lastcomp; + const char *toresolv; + + if (!orig[0]) { + fprintf(stderr, "%s: invalid mountpoint '%s'\n", progname, orig); + return NULL; + } + + copy = strdup(orig); + if (copy == NULL) { + fprintf(stderr, "%s: failed to allocate memory\n", progname); + return NULL; + } + + toresolv = copy; + lastcomp = NULL; + for (end = copy + strlen(copy) - 1; end > copy && *end == '/'; end --); + if (end[0] != '/') { + char *tmp; + end[1] = '\0'; + tmp = strrchr(copy, '/'); + if (tmp == NULL) { + lastcomp = copy; + toresolv = "."; + } else { + lastcomp = tmp + 1; + if (tmp == copy) + toresolv = "/"; + } + if (strcmp(lastcomp, ".") == 0 || strcmp(lastcomp, "..") == 0) { + lastcomp = NULL; + toresolv = copy; + } + else if (tmp) + tmp[0] = '\0'; + } + if (realpath(toresolv, buf) == NULL) { + fprintf(stderr, "%s: bad mount point %s: %s\n", progname, orig, + strerror(errno)); + free(copy); + return NULL; + } + if (lastcomp == NULL) + dst = strdup(buf); + else { + dst = (char *) malloc(strlen(buf) + 1 + strlen(lastcomp) + 1); + if (dst) { + unsigned buflen = strlen(buf); + if (buflen && buf[buflen-1] == '/') + sprintf(dst, "%s%s", buf, lastcomp); + else + sprintf(dst, "%s/%s", buf, lastcomp); + } + } + free(copy); + if (dst == NULL) + fprintf(stderr, "%s: failed to allocate memory\n", progname); + return dst; +} + +int fuse_mnt_check_empty(const char *progname, const char *mnt, + mode_t rootmode, off_t rootsize) +{ + int isempty = 1; + + if (S_ISDIR(rootmode)) { + struct dirent *ent; + DIR *dp = opendir(mnt); + if (dp == NULL) { + fprintf(stderr, "%s: failed to open mountpoint for reading: %s\n", + progname, strerror(errno)); + return -1; + } + while ((ent = readdir(dp)) != NULL) { + if (strcmp(ent->d_name, ".") != 0 && + strcmp(ent->d_name, "..") != 0) { + isempty = 0; + break; + } + } + closedir(dp); + } else if (rootsize) + isempty = 0; + + if (!isempty) { + fprintf(stderr, "%s: mountpoint is not empty\n", progname); + fprintf(stderr, "%s: if you are sure this is safe, use the 'nonempty' mount option\n", progname); + return -1; + } + return 0; +} + +int fuse_mnt_check_fuseblk(void) +{ + char buf[256]; + FILE *f = fopen("/proc/filesystems", "r"); + if (!f) + return 1; + + while (fgets(buf, sizeof(buf), f)) + if (strstr(buf, "fuseblk\n")) { + fclose(f); + return 1; + } + + fclose(f); + return 0; +} + +#else /* __SOLARIS__ */ + +static int mtab_needs_update(const char *mnt) +{ + int res; + struct stat stbuf; + + /* If mtab is within new mount, don't touch it */ + if (strncmp(mnt, _PATH_MOUNTED, strlen(mnt)) == 0 && + _PATH_MOUNTED[strlen(mnt)] == '/') + return 0; + + /* + * Skip mtab update if /etc/mtab: + * + * - doesn't exist, + * - is a symlink, + * - is on a read-only filesystem. + */ + res = lstat(_PATH_MOUNTED, &stbuf); + if (res == -1) { + if (errno == ENOENT) + return 0; + } else { + if (S_ISLNK(stbuf.st_mode)) + return 0; + + res = access(_PATH_MOUNTED, W_OK); + if (res == -1 && errno == EROFS) + return 0; + } + + return 1; +} + +int fuse_mnt_add_mount(const char *progname, const char *fsname, + const char *mnt, const char *type, const char *opts) +{ + int res; + + if (!mtab_needs_update(mnt)) + return 0; + + res = fork(); + if (res == -1) { + fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno)); + return 0; + } + if (res == 0) { + char *env = NULL; + char templ[] = "/tmp/fusermountXXXXXX"; + char *tmp; + + if (setuid(geteuid())) + fprintf(stderr, "%s: failed to setuid : %s\n", progname, + strerror(errno)); + + /* + * hide in a directory, where mount isn't able to resolve + * fsname as a valid path + */ + tmp = mkdtemp(templ); + if (!tmp) { + fprintf(stderr, "%s: failed to create temporary directory\n", + progname); + exit(1); + } + if (chdir(tmp)) { + fprintf(stderr, "%s: failed to chdir to %s: %s\n", + progname, tmp, strerror(errno)); + exit(1); + } + rmdir(tmp); + execle("/bin/mount", "/bin/mount", "-i", "-f", "-t", type, "-o", opts, + fsname, mnt, NULL, &env); + fprintf(stderr, "%s: failed to execute /bin/mount: %s\n", progname, + strerror(errno)); + exit(1); + } + return 0; +} + +int fuse_mnt_umount(const char *progname, const char *mnt, int lazy) +{ + int res; + int status; + + if (!mtab_needs_update(mnt)) { + res = umount2(mnt, lazy ? 2 : 0); + if (res == -1) + fprintf(stderr, "%s: failed to unmount %s: %s\n", progname, + mnt, strerror(errno)); + return res; + } + + res = fork(); + if (res == -1) { + fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno)); + return -1; + } + if (res == 0) { + char *env = NULL; + + if (setuid(geteuid())) + fprintf(stderr, "%s: failed to setuid : %s\n", progname, + strerror(errno)); + if (lazy) { + execle("/bin/umount", "/bin/umount", "-i", mnt, "-l", + NULL, &env); + } else { + execle("/bin/umount", "/bin/umount", "-i", mnt, + NULL, &env); + } + fprintf(stderr, "%s: failed to execute /bin/umount: %s\n", progname, + strerror(errno)); + exit(1); + } + res = waitpid(res, &status, 0); + if (res == -1) { + fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno)); + return -1; + } + if (status != 0) + return -1; + + return 0; +} + +char *fuse_mnt_resolve_path(const char *progname, const char *orig) +{ + char buf[PATH_MAX]; + char *copy; + char *dst; + char *end; + char *lastcomp; + const char *toresolv; + + if (!orig[0]) { + fprintf(stderr, "%s: invalid mountpoint '%s'\n", progname, orig); + return NULL; + } + + copy = strdup(orig); + if (copy == NULL) { + fprintf(stderr, "%s: failed to allocate memory\n", progname); + return NULL; + } + + toresolv = copy; + lastcomp = NULL; + for (end = copy + strlen(copy) - 1; end > copy && *end == '/'; end --); + if (end[0] != '/') { + char *tmp; + end[1] = '\0'; + tmp = strrchr(copy, '/'); + if (tmp == NULL) { + lastcomp = copy; + toresolv = "."; + } else { + lastcomp = tmp + 1; + if (tmp == copy) + toresolv = "/"; + } + if (strcmp(lastcomp, ".") == 0 || strcmp(lastcomp, "..") == 0) { + lastcomp = NULL; + toresolv = copy; + } + else if (tmp) + tmp[0] = '\0'; + } + if (realpath(toresolv, buf) == NULL) { + fprintf(stderr, "%s: bad mount point %s: %s\n", progname, orig, + strerror(errno)); + free(copy); + return NULL; + } + if (lastcomp == NULL) + dst = strdup(buf); + else { + dst = (char *) malloc(strlen(buf) + 1 + strlen(lastcomp) + 1); + if (dst) { + unsigned buflen = strlen(buf); + if (buflen && buf[buflen-1] == '/') + sprintf(dst, "%s%s", buf, lastcomp); + else + sprintf(dst, "%s/%s", buf, lastcomp); + } + } + free(copy); + if (dst == NULL) + fprintf(stderr, "%s: failed to allocate memory\n", progname); + return dst; +} + +int fuse_mnt_check_fuseblk(void) +{ + char buf[256]; + FILE *f = fopen("/proc/filesystems", "r"); + if (!f) + return 1; + + while (fgets(buf, sizeof(buf), f)) + if (strstr(buf, "fuseblk\n")) { + fclose(f); + return 1; + } + + fclose(f); + return 0; +} + +#endif /* __SOLARIS__ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/mount_util.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/mount_util.h new file mode 100644 index 0000000..0318385 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libfuse-lite/mount_util.h @@ -0,0 +1,27 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include + +int fuse_mnt_add_mount(const char *progname, const char *fsname, + const char *mnt, const char *type, const char *opts); +int fuse_mnt_umount(const char *progname, const char *mnt, int lazy); +char *fuse_mnt_resolve_path(const char *progname, const char *orig); +int fuse_mnt_check_fuseblk(void); + +#ifdef __SOLARIS__ + +int fuse_mnt_check_empty(const char *progname, const char *mnt, + mode_t rootmode, off_t rootsize); + +#else /* __SOLARIS__ */ + +int fusermount(int unmount, int quiet, int lazy, const char *opts, + const char *origmnt); + +#endif /* __SOLARIS__ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/Makefile.am b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/Makefile.am new file mode 100644 index 0000000..6feba7d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/Makefile.am @@ -0,0 +1,84 @@ + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +if INSTALL_LIBRARY +rootlib_LTLIBRARIES=#Create directory +lib_LTLIBRARIES = libntfs-3g.la +pkgconfig_DATA = libntfs-3g.pc +else +noinst_LTLIBRARIES = libntfs-3g.la +endif + +libntfs_3g_la_CFLAGS = $(AM_CFLAGS) +libntfs_3g_la_CPPFLAGS= $(AM_CPPFLAGS) $(LIBNTFS_CPPFLAGS) -I$(top_srcdir)/include/ntfs-3g +libntfs_3g_la_LIBADD = $(LIBNTFS_LIBS) +libntfs_3g_la_LDFLAGS = -version-info $(LIBNTFS_3G_VERSION) -no-undefined + +libntfs_3g_la_SOURCES = \ + acls.c \ + attrib.c \ + attrlist.c \ + bitmap.c \ + bootsect.c \ + cache.c \ + collate.c \ + compat.c \ + compress.c \ + debug.c \ + device.c \ + dir.c \ + ea.c \ + efs.c \ + index.c \ + inode.c \ + ioctl.c \ + lcnalloc.c \ + logfile.c \ + logging.c \ + mft.c \ + misc.c \ + mst.c \ + object_id.c \ + realpath.c \ + reparse.c \ + runlist.c \ + security.c \ + unistr.c \ + volume.c \ + xattrs.c + +if NTFS_DEVICE_DEFAULT_IO_OPS +if WINDOWS +libntfs_3g_la_SOURCES += win32_io.c +else +libntfs_3g_la_SOURCES += unix_io.c +endif +endif + +# We may need to move .so files to root +# And create ldscript or symbolic link from /usr +install-exec-hook: install-rootlibLTLIBRARIES +if INSTALL_LIBRARY + if [ ! "$(rootlibdir)" -ef "$(libdir)" ]; then \ + $(MV) -f "$(DESTDIR)/$(libdir)"/libntfs-3g.so* "$(DESTDIR)/$(rootlibdir)"; \ + fi +if GENERATE_LDSCRIPT + if [ ! "$(rootlibdir)" -ef "$(libdir)" ]; then \ + $(install_sh_PROGRAM) "libntfs-3g.script.so" "$(DESTDIR)/$(libdir)/libntfs-3g.so"; \ + fi +else + if [ ! "$(rootlibdir)" -ef "$(libdir)" ]; then \ + $(LN_S) "$(rootlibdir)/libntfs-3g.so" "$(DESTDIR)/$(libdir)/libntfs-3g.so"; \ + fi +endif +endif + +uninstall-local: +if INSTALL_LIBRARY + $(RM) -f "$(DESTDIR)/$(rootlibdir)"/libntfs-3g.so* +endif + +if ENABLE_NTFSPROGS +libs: $(lib_LTLIBRARIES) +endif + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/acls.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/acls.c new file mode 100644 index 0000000..21e9682 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/acls.c @@ -0,0 +1,4617 @@ +/** + * acls.c - General function to process NTFS ACLs + * + * This module is part of ntfs-3g library, but may also be + * integrated in tools running over Linux or Windows + * + * Copyright (c) 2007-2017 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_SYSLOG_H +#include +#endif +#include +#include +#include + +#include "types.h" +#include "layout.h" +#include "security.h" +#include "acls.h" +#include "misc.h" + +/* + * A few useful constants + */ + +/* + * null SID (S-1-0-0) + */ + +static const char nullsidbytes[] = { + 1, /* revision */ + 1, /* auth count */ + 0, 0, 0, 0, 0, 0, /* base */ + 0, 0, 0, 0 /* 1st level */ + }; + +static const SID *nullsid = (const SID*)nullsidbytes; + +/* + * SID for world (S-1-1-0) + */ + +static const char worldsidbytes[] = { + 1, /* revision */ + 1, /* auth count */ + 0, 0, 0, 0, 0, 1, /* base */ + 0, 0, 0, 0 /* 1st level */ +} ; + +const SID *worldsid = (const SID*)worldsidbytes; + +/* + * SID for authenticated user (S-1-5-11) + */ + +static const char authsidbytes[] = { + 1, /* revision */ + 1, /* auth count */ + 0, 0, 0, 0, 0, 5, /* base */ + 11, 0, 0, 0 /* 1st level */ +}; + +static const SID *authsid = (const SID*)authsidbytes; + +/* + * SID for administrator + */ + +static const char adminsidbytes[] = { + 1, /* revision */ + 2, /* auth count */ + 0, 0, 0, 0, 0, 5, /* base */ + 32, 0, 0, 0, /* 1st level */ + 32, 2, 0, 0 /* 2nd level */ +}; + +const SID *adminsid = (const SID*)adminsidbytes; + +/* + * SID for system + */ + +static const char systemsidbytes[] = { + 1, /* revision */ + 1, /* auth count */ + 0, 0, 0, 0, 0, 5, /* base */ + 18, 0, 0, 0 /* 1st level */ + }; + +static const SID *systemsid = (const SID*)systemsidbytes; + +/* + * SID for generic creator-owner + * S-1-3-0 + */ + +static const char ownersidbytes[] = { + 1, /* revision */ + 1, /* auth count */ + 0, 0, 0, 0, 0, 3, /* base */ + 0, 0, 0, 0 /* 1st level */ +} ; + +static const SID *ownersid = (const SID*)ownersidbytes; + +/* + * SID for generic creator-group + * S-1-3-1 + */ + +static const char groupsidbytes[] = { + 1, /* revision */ + 1, /* auth count */ + 0, 0, 0, 0, 0, 3, /* base */ + 1, 0, 0, 0 /* 1st level */ +} ; + +static const SID *groupsid = (const SID*)groupsidbytes; + +/* + * Determine the size of a SID + */ + +int ntfs_sid_size(const SID * sid) +{ + return (sid->sub_authority_count * 4 + 8); +} + +/* + * Test whether two SID are equal + */ + +BOOL ntfs_same_sid(const SID *first, const SID *second) +{ + int size; + + size = ntfs_sid_size(first); + return ((ntfs_sid_size(second) == size) + && !memcmp(first, second, size)); +} + +/* + * Test whether a SID means "world user" + * Local users group recognized as world + * Also interactive users so that /Users/Public is world accessible, + * but only if Posix ACLs are not enabled (if Posix ACLs are enabled, + * access to /Users/Public should be done by defining interactive users + * as a mapped group.) + */ + +static int is_world_sid(const SID * usid) +{ + return ( + /* check whether S-1-1-0 : world */ + ((usid->sub_authority_count == 1) + && (usid->identifier_authority.high_part == const_cpu_to_be16(0)) + && (usid->identifier_authority.low_part == const_cpu_to_be32(1)) + && (usid->sub_authority[0] == const_cpu_to_le32(0))) + + /* check whether S-1-5-32-545 : local user */ + || ((usid->sub_authority_count == 2) + && (usid->identifier_authority.high_part == const_cpu_to_be16(0)) + && (usid->identifier_authority.low_part == const_cpu_to_be32(5)) + && (usid->sub_authority[0] == const_cpu_to_le32(32)) + && (usid->sub_authority[1] == const_cpu_to_le32(545))) + + /* check whether S-1-5-11 : authenticated user */ + || ((usid->sub_authority_count == 1) + && (usid->identifier_authority.high_part == const_cpu_to_be16(0)) + && (usid->identifier_authority.low_part == const_cpu_to_be32(5)) + && (usid->sub_authority[0] == const_cpu_to_le32(11))) + +#if !POSIXACLS + /* check whether S-1-5-4 : interactive user */ + || ((usid->sub_authority_count == 1) + && (usid->identifier_authority.high_part == const_cpu_to_be16(0)) + && (usid->identifier_authority.low_part == const_cpu_to_be32(5)) + && (usid->sub_authority[0] == const_cpu_to_le32(4))) +#endif /* !POSIXACLS */ + ); +} + +/* + * Test whether a SID means "some user (or group)" + * Currently we only check for S-1-5-21... but we should + * probably test for other configurations + */ + +BOOL ntfs_is_user_sid(const SID *usid) +{ + return ((usid->sub_authority_count == 5) + && (usid->identifier_authority.high_part == const_cpu_to_be16(0)) + && (usid->identifier_authority.low_part == const_cpu_to_be32(5)) + && (usid->sub_authority[0] == const_cpu_to_le32(21))); +} + +/* + * Test whether a SID means "some special group" + * Currently we only check for a few S-1-5-n but we should + * probably test for other configurations. + * + * This is useful for granting access to /Users/Public for + * specific users when the Posix ACLs are enabled. + */ + +static BOOL ntfs_known_group_sid(const SID *usid) +{ + /* count == 1 excludes S-1-5-5-X-Y (logon) */ + return ((usid->sub_authority_count == 1) + && (usid->identifier_authority.high_part == const_cpu_to_be16(0)) + && (usid->identifier_authority.low_part == const_cpu_to_be32(5)) + && (le32_to_cpu(usid->sub_authority[0]) >= 1) + && (le32_to_cpu(usid->sub_authority[0]) <= 6)); +} + +/* + * Determine the size of a security attribute + * whatever the order of fields + */ + +unsigned int ntfs_attr_size(const char *attr) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const ACL *pdacl; + const ACL *psacl; + const SID *psid; + unsigned int offdacl; + unsigned int offsacl; + unsigned int offowner; + unsigned int offgroup; + unsigned int endsid; + unsigned int endacl; + unsigned int attrsz; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + /* + * First check group, which is the last field in all descriptors + * we build, and in most descriptors built by Windows + */ + attrsz = sizeof(SECURITY_DESCRIPTOR_RELATIVE); + offgroup = le32_to_cpu(phead->group); + if (offgroup >= attrsz) { + /* find end of GSID */ + psid = (const SID*)&attr[offgroup]; + endsid = offgroup + ntfs_sid_size(psid); + if (endsid > attrsz) attrsz = endsid; + } + offowner = le32_to_cpu(phead->owner); + if (offowner >= attrsz) { + /* find end of USID */ + psid = (const SID*)&attr[offowner]; + endsid = offowner + ntfs_sid_size(psid); + attrsz = endsid; + } + offsacl = le32_to_cpu(phead->sacl); + if (offsacl >= attrsz) { + /* find end of SACL */ + psacl = (const ACL*)&attr[offsacl]; + endacl = offsacl + le16_to_cpu(psacl->size); + if (endacl > attrsz) + attrsz = endacl; + } + + + /* find end of DACL */ + offdacl = le32_to_cpu(phead->dacl); + if (offdacl >= attrsz) { + pdacl = (const ACL*)&attr[offdacl]; + endacl = offdacl + le16_to_cpu(pdacl->size); + if (endacl > attrsz) + attrsz = endacl; + } + return (attrsz); +} + +/** + * ntfs_valid_sid - determine if a SID is valid + * @sid: SID for which to determine if it is valid + * + * Determine if the SID pointed to by @sid is valid. + * + * Return TRUE if it is valid and FALSE otherwise. + */ +BOOL ntfs_valid_sid(const SID *sid) +{ + return sid && sid->revision == SID_REVISION && + sid->sub_authority_count <= SID_MAX_SUB_AUTHORITIES; +} + +/* + * Check whether a SID is acceptable for an implicit + * mapping pattern. + * It should have been already checked it is a valid user SID. + * + * The last authority reference has to be >= 1000 (Windows usage) + * and <= 0x7fffffff, so that 30 bits from a uid and 30 more bits + * from a gid an be inserted with no overflow. + */ + +BOOL ntfs_valid_pattern(const SID *sid) +{ + int cnt; + u32 auth; + le32 leauth; + + cnt = sid->sub_authority_count; + leauth = sid->sub_authority[cnt-1]; + auth = le32_to_cpu(leauth); + return ((auth >= 1000) && (auth <= 0x7fffffff)); +} + +/* + * Compute the uid or gid associated to a SID + * through an implicit mapping + * + * Returns 0 (root) if it does not match pattern + */ + +static u32 findimplicit(const SID *xsid, const SID *pattern, int parity) +{ + BIGSID defsid; + SID *psid; + u32 xid; /* uid or gid */ + int cnt; + u32 carry; + le32 leauth; + u32 uauth; + u32 xlast; + u32 rlast; + + memcpy(&defsid,pattern,ntfs_sid_size(pattern)); + psid = (SID*)&defsid; + cnt = psid->sub_authority_count; + xid = 0; + if (xsid->sub_authority_count == cnt) { + psid->sub_authority[cnt-1] = xsid->sub_authority[cnt-1]; + leauth = xsid->sub_authority[cnt-1]; + xlast = le32_to_cpu(leauth); + leauth = pattern->sub_authority[cnt-1]; + rlast = le32_to_cpu(leauth); + + if ((xlast > rlast) && !((xlast ^ rlast ^ parity) & 1)) { + /* direct check for basic situation */ + if (ntfs_same_sid(psid,xsid)) + xid = ((xlast - rlast) >> 1) & 0x3fffffff; + else { + /* + * check whether part of mapping had to be + * recorded in a higher level authority + */ + carry = 1; + do { + leauth = psid->sub_authority[cnt-2]; + uauth = le32_to_cpu(leauth) + 1; + psid->sub_authority[cnt-2] + = cpu_to_le32(uauth); + } while (!ntfs_same_sid(psid,xsid) + && (++carry < 4)); + if (carry < 4) + xid = (((xlast - rlast) >> 1) + & 0x3fffffff) | (carry << 30); + } + } + } + return (xid); +} + +/* + * Find usid mapped to a Linux user + * Returns NULL if not found + */ + +const SID *ntfs_find_usid(const struct MAPPING* usermapping, + uid_t uid, SID *defusid) +{ + const struct MAPPING *p; + const SID *sid; + le32 leauth; + u32 uauth; + int cnt; + + if (!uid) + sid = adminsid; + else { + p = usermapping; + while (p && p->xid && ((uid_t)p->xid != uid)) + p = p->next; + if (p && !p->xid) { + /* + * default pattern has been reached : + * build an implicit SID according to pattern + * (the pattern format was checked while reading + * the mapping file) + */ + memcpy(defusid, p->sid, ntfs_sid_size(p->sid)); + cnt = defusid->sub_authority_count; + leauth = defusid->sub_authority[cnt-1]; + uauth = le32_to_cpu(leauth) + 2*(uid & 0x3fffffff); + defusid->sub_authority[cnt-1] = cpu_to_le32(uauth); + if (uid & 0xc0000000) { + leauth = defusid->sub_authority[cnt-2]; + uauth = le32_to_cpu(leauth) + ((uid >> 30) & 3); + defusid->sub_authority[cnt-2] = cpu_to_le32(uauth); + } + sid = defusid; + } else + sid = (p ? p->sid : (const SID*)NULL); + } + return (sid); +} + +/* + * Find Linux group mapped to a gsid + * Returns 0 (root) if not found + */ + +const SID *ntfs_find_gsid(const struct MAPPING* groupmapping, + gid_t gid, SID *defgsid) +{ + const struct MAPPING *p; + const SID *sid; + le32 leauth; + u32 uauth; + int cnt; + + if (!gid) + sid = adminsid; + else { + p = groupmapping; + while (p && p->xid && ((gid_t)p->xid != gid)) + p = p->next; + if (p && !p->xid) { + /* + * default pattern has been reached : + * build an implicit SID according to pattern + * (the pattern format was checked while reading + * the mapping file) + */ + memcpy(defgsid, p->sid, ntfs_sid_size(p->sid)); + cnt = defgsid->sub_authority_count; + leauth = defgsid->sub_authority[cnt-1]; + uauth = le32_to_cpu(leauth) + 2*(gid & 0x3fffffff) + 1; + defgsid->sub_authority[cnt-1] = cpu_to_le32(uauth); + if (gid & 0xc0000000) { + leauth = defgsid->sub_authority[cnt-2]; + uauth = le32_to_cpu(leauth) + ((gid >> 30) & 3); + defgsid->sub_authority[cnt-2] = cpu_to_le32(uauth); + } + sid = defgsid; + } else + sid = (p ? p->sid : (const SID*)NULL); + } + return (sid); +} + +/* + * Find Linux owner mapped to a usid + * Returns 0 (root) if not found + */ + +uid_t ntfs_find_user(const struct MAPPING* usermapping, const SID *usid) +{ + uid_t uid; + const struct MAPPING *p; + + p = usermapping; + while (p && p->xid && !ntfs_same_sid(usid, p->sid)) + p = p->next; + if (p && !p->xid) + /* + * No explicit mapping found, try implicit mapping + */ + uid = findimplicit(usid,p->sid,0); + else + uid = (p ? p->xid : 0); + return (uid); +} + +/* + * Find Linux group mapped to a gsid + * Returns 0 (root) if not found + */ + +gid_t ntfs_find_group(const struct MAPPING* groupmapping, const SID * gsid) +{ + gid_t gid; + const struct MAPPING *p; + + p = groupmapping; + while (p && p->xid && !ntfs_same_sid(gsid, p->sid)) + p = p->next; + if (p && !p->xid) + /* + * No explicit mapping found, try implicit mapping + */ + gid = findimplicit(gsid,p->sid,1); + else + gid = (p ? p->xid : 0); + return (gid); +} + +/* + * Check the validity of the ACEs in a DACL or SACL + */ + +static BOOL valid_acl(const ACL *pacl, unsigned int end) +{ + const ACCESS_ALLOWED_ACE *pace; + unsigned int offace; + unsigned int acecnt; + unsigned int acesz; + unsigned int nace; + unsigned int wantsz; + BOOL ok; + + ok = TRUE; + acecnt = le16_to_cpu(pacl->ace_count); + offace = sizeof(ACL); + for (nace = 0; (nace < acecnt) && ok; nace++) { + /* be sure the beginning is within range */ + if ((offace + sizeof(ACCESS_ALLOWED_ACE)) > end) + ok = FALSE; + else { + pace = (const ACCESS_ALLOWED_ACE*) + &((const char*)pacl)[offace]; + acesz = le16_to_cpu(pace->size); + if (acesz < sizeof(ACE_HEADER)) + ok = FALSE; + else switch (pace->type) { + case ACCESS_ALLOWED_ACE_TYPE : + case ACCESS_DENIED_ACE_TYPE : + wantsz = ntfs_sid_size(&pace->sid) + 8; + if (((offace + acesz) > end) + || !ntfs_valid_sid(&pace->sid) + || (wantsz != acesz)) + ok = FALSE; + break; + case SYSTEM_AUDIT_ACE_TYPE : + case ACCESS_ALLOWED_CALLBACK_ACE_TYPE : + case ACCESS_DENIED_CALLBACK_ACE_TYPE : + case SYSTEM_AUDIT_CALLBACK_ACE_TYPE : + case SYSTEM_MANDATORY_LABEL_ACE_TYPE : + case SYSTEM_RESOURCE_ATTRIBUTE_ACE_TYPE : + case SYSTEM_SCOPED_POLICY_ID_ACE_TYPE : + /* Extra data after the SID */ + wantsz = ntfs_sid_size(&pace->sid) + 8; + if (((offace + acesz) > end) + || !ntfs_valid_sid(&pace->sid) + || (wantsz > acesz)) + ok = FALSE; + break; + default : + /* SID at a different location */ + if ((offace + acesz) > end) + ok = FALSE; + break; + } + offace += acesz; + } + } + return (ok); +} + +/* + * Do sanity checks on security descriptors read from storage + * basically, we make sure that every field holds within + * allocated storage + * Should not be called with a NULL argument + * returns TRUE if considered safe + * if not, error should be logged by caller + */ + +BOOL ntfs_valid_descr(const char *securattr, unsigned int attrsz) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const ACL *pdacl; + const ACL *psacl; + unsigned int offdacl; + unsigned int offsacl; + unsigned int offowner; + unsigned int offgroup; + BOOL ok; + + ok = TRUE; + + /* + * first check overall size if within allocation range + * and a DACL is present + * and owner and group SID are valid + */ + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)securattr; + offdacl = le32_to_cpu(phead->dacl); + offsacl = le32_to_cpu(phead->sacl); + offowner = le32_to_cpu(phead->owner); + offgroup = le32_to_cpu(phead->group); + pdacl = (const ACL*)&securattr[offdacl]; + psacl = (const ACL*)&securattr[offsacl]; + + /* + * size check occurs before the above pointers are used + * + * "DR Watson" standard directory on WinXP has an + * old revision and no DACL though SE_DACL_PRESENT is set + */ + if ((attrsz >= sizeof(SECURITY_DESCRIPTOR_RELATIVE)) + && (phead->revision == SECURITY_DESCRIPTOR_REVISION) + && (offowner >= sizeof(SECURITY_DESCRIPTOR_RELATIVE)) + && ((offowner + 2) < attrsz) + && (offgroup >= sizeof(SECURITY_DESCRIPTOR_RELATIVE)) + && ((offgroup + 2) < attrsz) + && (!offdacl + || ((offdacl >= sizeof(SECURITY_DESCRIPTOR_RELATIVE)) + && (offdacl+sizeof(ACL) <= attrsz))) + && (!offsacl + || ((offsacl >= sizeof(SECURITY_DESCRIPTOR_RELATIVE)) + && (offsacl+sizeof(ACL) <= attrsz))) + && !(phead->owner & const_cpu_to_le32(3)) + && !(phead->group & const_cpu_to_le32(3)) + && !(phead->dacl & const_cpu_to_le32(3)) + && !(phead->sacl & const_cpu_to_le32(3)) + && (ntfs_attr_size(securattr) <= attrsz) + && ntfs_valid_sid((const SID*)&securattr[offowner]) + && ntfs_valid_sid((const SID*)&securattr[offgroup]) + /* + * if there is an ACL, as indicated by offdacl, + * require SE_DACL_PRESENT + * but "Dr Watson" has SE_DACL_PRESENT though no DACL + */ + && (!offdacl + || ((phead->control & SE_DACL_PRESENT) + && ((pdacl->revision == ACL_REVISION) + || (pdacl->revision == ACL_REVISION_DS)))) + /* same for SACL */ + && (!offsacl + || ((phead->control & SE_SACL_PRESENT) + && ((psacl->revision == ACL_REVISION) + || (psacl->revision == ACL_REVISION_DS))))) { + /* + * Check the DACL and SACL if present + */ + if ((offdacl && !valid_acl(pdacl,attrsz - offdacl)) + || (offsacl && !valid_acl(psacl,attrsz - offsacl))) + ok = FALSE; + } else + ok = FALSE; + return (ok); +} + +/** + * ntfs_inherit_acl_extra_size: compute creator SID inheritance slack + * @acl: ACL to scan + * @usid: owner SID to substitute for CREATOR_OWNER + * @gsid: group SID to substitute for CREATOR_GROUP + * + * Walks @acl bounded by acl->size, adding slack for ALLOW and DENY ACEs + * whose SID matches either creator placeholder. ntfs_inherit_acl() can + * replace those placeholders by @usid or @gsid and, for directories, can + * also keep a verbatim copy for child inheritance. Count the worst-case + * extra bytes so the inherited descriptor allocation cannot be overrun. + * + * Return: extra bytes needed, or 0 if @acl is NULL or structurally rejected. + */ + +int ntfs_inherit_acl_extra_size(const ACL *acl, + const SID *usid, const SID *gsid) +{ + const ACCESS_ALLOWED_ACE *ace; + unsigned int off; + unsigned int acl_size; + unsigned int acesz; + unsigned int sidsz; + int usidsz; + int gsidsz; + int ownersidsz; + int groupsidsz; + int oldcnt; + int nace; + int extra; + BOOL usid_is_group_sid; + + extra = 0; + if (!acl || !usid || !gsid) + return (0); + acl_size = le16_to_cpu(acl->size); + if (acl_size < sizeof(ACL)) + return (0); + oldcnt = le16_to_cpu(acl->ace_count); + usidsz = ntfs_sid_size(usid); + gsidsz = ntfs_sid_size(gsid); + ownersidsz = sizeof(ownersidbytes); + groupsidsz = sizeof(groupsidbytes); + usid_is_group_sid = ntfs_same_sid(usid, groupsid); + off = sizeof(ACL); + for (nace = 0; nace < oldcnt; nace++) { + if (off + 8 > acl_size) + break; + ace = (const ACCESS_ALLOWED_ACE*)((const char*)acl + off); + acesz = le16_to_cpu(ace->size); + if (acesz < 8 || acesz > acl_size - off) + break; + switch (ace->type) { + case ACCESS_ALLOWED_ACE_TYPE : + case ACCESS_DENIED_ACE_TYPE : + if ((acesz >= 8 + sizeof(ownersidbytes)) + && ntfs_valid_sid(&ace->sid)) { + sidsz = ntfs_sid_size(&ace->sid); + if (sidsz <= acesz - 8) { + if (ntfs_same_sid(&ace->sid, ownersid)) + { + extra += usidsz - ownersidsz + + 20; + if (usid_is_group_sid) + extra += gsidsz - + groupsidsz + 20; + } + if (ntfs_same_sid(&ace->sid, groupsid)) + extra += gsidsz - groupsidsz + + 20; + } + } + break; + default : + break; + } + off += acesz; + } + return extra; +} + +/* + * Copy the inheritable parts of an ACL + * + * Returns the size of the new ACL + * or zero if nothing is inheritable + */ + +int ntfs_inherit_acl(const ACL *oldacl, ACL *newacl, + const SID *usid, const SID *gsid, BOOL fordir, + le16 inherited) +{ + unsigned int src; + unsigned int dst; + int oldcnt; + int newcnt; + unsigned int selection; + int nace; + int acesz; + int usidsz; + int gsidsz; + BOOL acceptable; + const ACCESS_ALLOWED_ACE *poldace; + ACCESS_ALLOWED_ACE *pnewace; + ACCESS_ALLOWED_ACE *pauthace; + ACCESS_ALLOWED_ACE *pownerace; + + pauthace = (ACCESS_ALLOWED_ACE*)NULL; + pownerace = (ACCESS_ALLOWED_ACE*)NULL; + usidsz = ntfs_sid_size(usid); + gsidsz = ntfs_sid_size(gsid); + + /* ACL header */ + + newacl->revision = ACL_REVISION; + newacl->alignment1 = 0; + newacl->alignment2 = const_cpu_to_le16(0); + src = dst = sizeof(ACL); + + selection = (fordir ? CONTAINER_INHERIT_ACE : OBJECT_INHERIT_ACE); + newcnt = 0; + oldcnt = le16_to_cpu(oldacl->ace_count); + for (nace = 0; nace < oldcnt; nace++) { + poldace = (const ACCESS_ALLOWED_ACE*)((const char*)oldacl + src); + acesz = le16_to_cpu(poldace->size); + src += acesz; + /* + * Currently only ACE for file or directory access are + * processed. More information needed about what to do + * for other types (whose SID may be at a different location) + */ + switch (poldace->type) { + case ACCESS_ALLOWED_ACE_TYPE : + case ACCESS_DENIED_ACE_TYPE : + acceptable = TRUE; + break; + default : + acceptable = FALSE; + break; + } + /* + * Extract inheritance for access, including inheritance for + * access from an ACE with is both applied and inheritable. + * + * must not output OBJECT_INHERIT_ACE or CONTAINER_INHERIT_ACE + * + * According to MSDN : + * "For a case in which a container object inherits an ACE + * "that is both effective on the container and inheritable + * "by its descendants, the container may inherit two ACEs. + * "This occurs if the inheritable ACE contains generic + * "information." + */ + if ((poldace->flags & selection) + && acceptable + && (!fordir + || (poldace->flags & NO_PROPAGATE_INHERIT_ACE) + || (poldace->mask & (GENERIC_ALL | GENERIC_READ + | GENERIC_WRITE | GENERIC_EXECUTE))) + && !ntfs_same_sid(&poldace->sid, ownersid) + && !ntfs_same_sid(&poldace->sid, groupsid)) { + pnewace = (ACCESS_ALLOWED_ACE*) + ((char*)newacl + dst); + memcpy(pnewace,poldace,acesz); + /* reencode GENERIC_ALL */ + if (pnewace->mask & GENERIC_ALL) { + pnewace->mask &= ~GENERIC_ALL; + if (fordir) + pnewace->mask |= OWNER_RIGHTS + | DIR_READ + | DIR_WRITE + | DIR_EXEC; + else + /* + * The last flag is not defined for a file, + * however Windows sets it, so do the same + */ + pnewace->mask |= OWNER_RIGHTS + | FILE_READ + | FILE_WRITE + | FILE_EXEC + | const_cpu_to_le32(0x40); + } + /* reencode GENERIC_READ (+ EXECUTE) */ + if (pnewace->mask & GENERIC_READ) { + if (fordir) + pnewace->mask |= OWNER_RIGHTS + | DIR_READ + | DIR_EXEC; + else + pnewace->mask |= OWNER_RIGHTS + | FILE_READ + | FILE_EXEC; + pnewace->mask &= ~(GENERIC_READ + | GENERIC_EXECUTE + | WRITE_DAC + | WRITE_OWNER + | DELETE | FILE_WRITE_EA + | FILE_WRITE_ATTRIBUTES); + } + /* reencode GENERIC_WRITE */ + if (pnewace->mask & GENERIC_WRITE) { + if (fordir) + pnewace->mask |= OWNER_RIGHTS + | DIR_WRITE; + else + pnewace->mask |= OWNER_RIGHTS + | FILE_WRITE; + pnewace->mask &= ~(GENERIC_WRITE + | WRITE_DAC + | WRITE_OWNER + | FILE_DELETE_CHILD); + } + /* remove inheritance flags */ + pnewace->flags &= ~(OBJECT_INHERIT_ACE + | CONTAINER_INHERIT_ACE + | INHERIT_ONLY_ACE); + /* + * Group similar ACE for authenticated users + * (should probably be done for other SIDs) + */ + if ((poldace->type == ACCESS_ALLOWED_ACE_TYPE) + && ntfs_same_sid(&poldace->sid, authsid)) { + if (pauthace) { + pauthace->flags |= pnewace->flags; + pauthace->mask |= pnewace->mask; + } else { + pauthace = pnewace; + if (inherited) + pnewace->flags |= INHERITED_ACE; + dst += acesz; + newcnt++; + } + } else { + if (inherited) + pnewace->flags |= INHERITED_ACE; + dst += acesz; + newcnt++; + } + } + /* + * Inheritance for access, specific to + * creator-owner (and creator-group) + */ + if ((fordir || !inherited + || (poldace->flags + & (CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE))) + && acceptable) { + pnewace = (ACCESS_ALLOWED_ACE*) + ((char*)newacl + dst); + memcpy(pnewace,poldace,acesz); + /* + * Replace generic creator-owner and + * creator-group by owner and group + * (but keep for further inheritance) + */ + if (ntfs_same_sid(&pnewace->sid, ownersid)) { + memcpy(&pnewace->sid, usid, usidsz); + pnewace->size = cpu_to_le16(usidsz + 8); + /* remove inheritance flags */ + pnewace->flags &= ~(OBJECT_INHERIT_ACE + | CONTAINER_INHERIT_ACE + | INHERIT_ONLY_ACE); + if (inherited) + pnewace->flags |= INHERITED_ACE; + if ((pnewace->type == ACCESS_ALLOWED_ACE_TYPE) + && pownerace + && !(pnewace->flags & ~pownerace->flags)) { + pownerace->mask |= pnewace->mask; + } else { + dst += usidsz + 8; + newcnt++; + } + } + if (ntfs_same_sid(&pnewace->sid, groupsid)) { + memcpy(&pnewace->sid, gsid, gsidsz); + pnewace->size = cpu_to_le16(gsidsz + 8); + /* remove inheritance flags */ + pnewace->flags &= ~(OBJECT_INHERIT_ACE + | CONTAINER_INHERIT_ACE + | INHERIT_ONLY_ACE); + if (inherited) + pnewace->flags |= INHERITED_ACE; + dst += gsidsz + 8; + newcnt++; + } + } + + /* + * inheritance for further inheritance + * + * Situations leading to output CONTAINER_INHERIT_ACE + * or OBJECT_INHERIT_ACE + */ + if (fordir + && (poldace->flags + & (CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE))) { + pnewace = (ACCESS_ALLOWED_ACE*) + ((char*)newacl + dst); + memcpy(pnewace,poldace,acesz); + if ((poldace->flags & OBJECT_INHERIT_ACE) + && !(poldace->flags & (CONTAINER_INHERIT_ACE + | NO_PROPAGATE_INHERIT_ACE))) + pnewace->flags |= INHERIT_ONLY_ACE; + if (acceptable + && (poldace->flags & CONTAINER_INHERIT_ACE) + && !(poldace->flags & NO_PROPAGATE_INHERIT_ACE) + && !ntfs_same_sid(&poldace->sid, ownersid) + && !ntfs_same_sid(&poldace->sid, groupsid)) { + if ((poldace->mask & (GENERIC_ALL | GENERIC_READ + | GENERIC_WRITE | GENERIC_EXECUTE))) + pnewace->flags |= INHERIT_ONLY_ACE; + else + pnewace->flags &= ~INHERIT_ONLY_ACE; + } + if (inherited) + pnewace->flags |= INHERITED_ACE; + /* + * Prepare grouping similar ACE for authenticated users + */ + if ((poldace->type == ACCESS_ALLOWED_ACE_TYPE) + && !pauthace + && !(pnewace->flags & INHERIT_ONLY_ACE) + && ntfs_same_sid(&poldace->sid, authsid)) { + pauthace = pnewace; + } + /* + * Prepare grouping similar ACE for owner + */ + if ((poldace->type == ACCESS_ALLOWED_ACE_TYPE) + && !pownerace + && !(pnewace->flags & INHERIT_ONLY_ACE) + && ntfs_same_sid(&poldace->sid, usid)) { + pownerace = pnewace; + } + dst += acesz; + newcnt++; + } + } + /* + * Adjust header if something was inherited + */ + if (dst > sizeof(ACL)) { + newacl->ace_count = cpu_to_le16(newcnt); + newacl->size = cpu_to_le16(dst); + } else + dst = 0; + return (dst); +} + +#if POSIXACLS + +/* + * Do sanity checks on a Posix descriptor + * Should not be called with a NULL argument + * returns TRUE if considered safe + * if not, error should be logged by caller + */ + +BOOL ntfs_valid_posix(const struct POSIX_SECURITY *pxdesc) +{ + const struct POSIX_ACL *pacl; + int i; + BOOL ok; + u16 tag; + u32 id; + int perms; + struct { + u16 previous; + u32 previousid; + u16 tagsset; + mode_t mode; + int owners; + int groups; + int others; + } checks[2], *pchk; + + for (i=0; i<2; i++) { + checks[i].mode = 0; + checks[i].tagsset = 0; + checks[i].owners = 0; + checks[i].groups = 0; + checks[i].others = 0; + checks[i].previous = 0; + checks[i].previousid = 0; + } + ok = TRUE; + pacl = &pxdesc->acl; + /* + * header (strict for now) + */ + if ((pacl->version != POSIX_VERSION) + || (pacl->flags != 0) + || (pacl->filler != 0)) + ok = FALSE; + /* + * Reject multiple owner, group or other + * but do not require them to be present + * Also check the ACEs are in correct order + * which implies there is no duplicates + */ + for (i=0; iacccnt + pxdesc->defcnt; i++) { + if (i >= pxdesc->firstdef) + pchk = &checks[1]; + else + pchk = &checks[0]; + perms = pacl->ace[i].perms; + tag = pacl->ace[i].tag; + pchk->tagsset |= tag; + id = pacl->ace[i].id; + if (perms & ~7) ok = FALSE; + if ((tag < pchk->previous) + || ((tag == pchk->previous) + && (id <= pchk->previousid))) + ok = FALSE; + pchk->previous = tag; + pchk->previousid = id; + switch (tag) { + case POSIX_ACL_USER_OBJ : + if (pchk->owners++) + ok = FALSE; + if (id != (u32)-1) + ok = FALSE; + pchk->mode |= perms << 6; + break; + case POSIX_ACL_GROUP_OBJ : + if (pchk->groups++) + ok = FALSE; + if (id != (u32)-1) + ok = FALSE; + pchk->mode = (pchk->mode & 07707) | (perms << 3); + break; + case POSIX_ACL_OTHER : + if (pchk->others++) + ok = FALSE; + if (id != (u32)-1) + ok = FALSE; + pchk->mode |= perms; + break; + case POSIX_ACL_USER : + case POSIX_ACL_GROUP : + if (id == (u32)-1) + ok = FALSE; + break; + case POSIX_ACL_MASK : + if (id != (u32)-1) + ok = FALSE; + pchk->mode = (pchk->mode & 07707) | (perms << 3); + break; + default : + ok = FALSE; + break; + } + } + if ((pxdesc->acccnt > 0) + && ((checks[0].owners != 1) || (checks[0].groups != 1) + || (checks[0].others != 1))) + ok = FALSE; + /* do not check owner, group or other are present in */ + /* the default ACL, Windows does not necessarily set them */ + /* descriptor */ + if (pxdesc->defcnt && (pxdesc->acccnt > pxdesc->firstdef)) + ok = FALSE; + if ((pxdesc->acccnt < 0) || (pxdesc->defcnt < 0)) + ok = FALSE; + /* check mode, unless null or no tag set */ + if (pxdesc->mode + && checks[0].tagsset + && (checks[0].mode != (pxdesc->mode & 0777))) + ok = FALSE; + /* check tagsset */ + if (pxdesc->tagsset != checks[0].tagsset) + ok = FALSE; + return (ok); +} + +/* + * Set standard header data into a Posix ACL + * The mode argument should provide the 3 upper bits of target mode + */ + +static mode_t posix_header(struct POSIX_SECURITY *pxdesc, mode_t basemode) +{ + mode_t mode; + u16 tagsset; + struct POSIX_ACE *pace; + int i; + + mode = basemode & 07000; + tagsset = 0; + for (i=0; iacccnt; i++) { + pace = &pxdesc->acl.ace[i]; + tagsset |= pace->tag; + switch(pace->tag) { + case POSIX_ACL_USER_OBJ : + mode |= (pace->perms & 7) << 6; + break; + case POSIX_ACL_GROUP_OBJ : + case POSIX_ACL_MASK : + mode = (mode & 07707) | ((pace->perms & 7) << 3); + break; + case POSIX_ACL_OTHER : + mode |= pace->perms & 7; + break; + default : + break; + } + } + pxdesc->tagsset = tagsset; + pxdesc->mode = mode; + pxdesc->acl.version = POSIX_VERSION; + pxdesc->acl.flags = 0; + pxdesc->acl.filler = 0; + return (mode); +} + +/* + * Sort ACEs in a Posix ACL + * This is useful for always getting reusable converted ACLs, + * it also helps in merging ACEs. + * Repeated tag+id are allowed and not merged here. + * + * Tags should be in ascending sequence and for a repeatable tag + * ids should be in ascending sequence. + */ + +void ntfs_sort_posix(struct POSIX_SECURITY *pxdesc) +{ + struct POSIX_ACL *pacl; + struct POSIX_ACE ace; + int i; + int offs; + BOOL done; + u16 tag; + u16 previous; + u32 id; + u32 previousid; + + + /* + * Check sequencing of tag+id in access ACE's + */ + pacl = &pxdesc->acl; + do { + done = TRUE; + previous = pacl->ace[0].tag; + previousid = pacl->ace[0].id; + for (i=1; iacccnt; i++) { + tag = pacl->ace[i].tag; + id = pacl->ace[i].id; + + if ((tag < previous) + || ((tag == previous) && (id < previousid))) { + done = FALSE; + memcpy(&ace,&pacl->ace[i-1],sizeof(struct POSIX_ACE)); + memcpy(&pacl->ace[i-1],&pacl->ace[i],sizeof(struct POSIX_ACE)); + memcpy(&pacl->ace[i],&ace,sizeof(struct POSIX_ACE)); + } else { + previous = tag; + previousid = id; + } + } + } while (!done); + /* + * Same for default ACEs + */ + do { + done = TRUE; + if ((pxdesc->defcnt) > 1) { + offs = pxdesc->firstdef; + previous = pacl->ace[offs].tag; + previousid = pacl->ace[offs].id; + for (i=offs+1; idefcnt; i++) { + tag = pacl->ace[i].tag; + id = pacl->ace[i].id; + + if ((tag < previous) + || ((tag == previous) && (id < previousid))) { + done = FALSE; + memcpy(&ace,&pacl->ace[i-1],sizeof(struct POSIX_ACE)); + memcpy(&pacl->ace[i-1],&pacl->ace[i],sizeof(struct POSIX_ACE)); + memcpy(&pacl->ace[i],&ace,sizeof(struct POSIX_ACE)); + } else { + previous = tag; + previousid = id; + } + } + } + } while (!done); +} + +/* + * Merge a new mode into a Posix descriptor + * The Posix descriptor is not reallocated, its size is unchanged + * + * returns 0 if ok + */ + +int ntfs_merge_mode_posix(struct POSIX_SECURITY *pxdesc, mode_t mode) +{ + int i; + BOOL maskfound; + struct POSIX_ACE *pace; + int todo; + + maskfound = FALSE; + todo = POSIX_ACL_USER_OBJ | POSIX_ACL_GROUP_OBJ | POSIX_ACL_OTHER; + for (i=pxdesc->acccnt-1; i>=0; i--) { + pace = &pxdesc->acl.ace[i]; + switch(pace->tag) { + case POSIX_ACL_USER_OBJ : + pace->perms = (mode >> 6) & 7; + todo &= ~POSIX_ACL_USER_OBJ; + break; + case POSIX_ACL_GROUP_OBJ : + if (!maskfound) + pace->perms = (mode >> 3) & 7; + todo &= ~POSIX_ACL_GROUP_OBJ; + break; + case POSIX_ACL_MASK : + pace->perms = (mode >> 3) & 7; + maskfound = TRUE; + break; + case POSIX_ACL_OTHER : + pace->perms = mode & 7; + todo &= ~POSIX_ACL_OTHER; + break; + default : + break; + } + } + pxdesc->mode = mode; + return (todo ? -1 : 0); +} + +/* + * Replace an access or default Posix ACL + * The resulting ACL is checked for validity + * + * Returns a new ACL or NULL if there is a problem + */ + +struct POSIX_SECURITY *ntfs_replace_acl(const struct POSIX_SECURITY *oldpxdesc, + const struct POSIX_ACL *newacl, int count, BOOL deflt) +{ + struct POSIX_SECURITY *newpxdesc; + size_t newsize; + int offset; + int oldoffset; + int i; + + if (deflt) + newsize = sizeof(struct POSIX_SECURITY) + + (oldpxdesc->acccnt + count)*sizeof(struct POSIX_ACE); + else + newsize = sizeof(struct POSIX_SECURITY) + + (oldpxdesc->defcnt + count)*sizeof(struct POSIX_ACE); + newpxdesc = (struct POSIX_SECURITY*)malloc(newsize); + if (newpxdesc) { + if (deflt) { + offset = oldpxdesc->acccnt; + newpxdesc->acccnt = oldpxdesc->acccnt; + newpxdesc->defcnt = count; + newpxdesc->firstdef = offset; + /* copy access ACEs */ + for (i=0; iacccnt; i++) + newpxdesc->acl.ace[i] = oldpxdesc->acl.ace[i]; + /* copy default ACEs */ + for (i=0; iacl.ace[i + offset] = newacl->ace[i]; + } else { + offset = count; + newpxdesc->acccnt = count; + newpxdesc->defcnt = oldpxdesc->defcnt; + newpxdesc->firstdef = count; + /* copy access ACEs */ + for (i=0; iacl.ace[i] = newacl->ace[i]; + /* copy default ACEs */ + oldoffset = oldpxdesc->firstdef; + for (i=0; idefcnt; i++) + newpxdesc->acl.ace[i + offset] = oldpxdesc->acl.ace[i + oldoffset]; + } + /* assume special flags unchanged */ + posix_header(newpxdesc, oldpxdesc->mode); + if (!ntfs_valid_posix(newpxdesc)) { + /* do not log, this is an application error */ + free(newpxdesc); + newpxdesc = (struct POSIX_SECURITY*)NULL; + errno = EINVAL; + } + } else + errno = ENOMEM; + return (newpxdesc); +} + +/* + * Build a basic Posix ACL from a mode and umask, + * ignoring inheritance from the parent directory + */ + +struct POSIX_SECURITY *ntfs_build_basic_posix( + const struct POSIX_SECURITY *pxdesc __attribute__((unused)), + mode_t mode, mode_t mask, BOOL isdir __attribute__((unused))) +{ + struct POSIX_SECURITY *pydesc; + struct POSIX_ACE *pyace; + + pydesc = (struct POSIX_SECURITY*)malloc( + sizeof(struct POSIX_SECURITY) + 3*sizeof(struct POSIX_ACE)); + if (pydesc) { + pyace = &pydesc->acl.ace[0]; + pyace->tag = POSIX_ACL_USER_OBJ; + pyace->perms = ((mode & ~mask) >> 6) & 7; + pyace->id = -1; + pyace = &pydesc->acl.ace[1]; + pyace->tag = POSIX_ACL_GROUP_OBJ; + pyace->perms = ((mode & ~mask) >> 3) & 7; + pyace->id = -1; + pyace = &pydesc->acl.ace[2]; + pyace->tag = POSIX_ACL_OTHER; + pyace->perms = (mode & ~mask) & 7; + pyace->id = -1; + pydesc->mode = mode; + pydesc->tagsset = POSIX_ACL_USER_OBJ + | POSIX_ACL_GROUP_OBJ + | POSIX_ACL_OTHER; + pydesc->acccnt = 3; + pydesc->defcnt = 0; + pydesc->firstdef = 6; + pydesc->filler = 0; + pydesc->acl.version = POSIX_VERSION; + pydesc->acl.flags = 0; + pydesc->acl.filler = 0; + } else + errno = ENOMEM; + return (pydesc); +} + +/* + * Build an inherited Posix descriptor from parent + * descriptor (if any) restricted to creation mode + * + * Returns the inherited descriptor or NULL if there is a problem + */ + +struct POSIX_SECURITY *ntfs_build_inherited_posix( + const struct POSIX_SECURITY *pxdesc, mode_t mode, + mode_t mask, BOOL isdir) +{ + struct POSIX_SECURITY *pydesc; + struct POSIX_ACE *pyace; + int count; + int defcnt; + int size; + int i; + s16 tagsset; + + if (pxdesc && pxdesc->defcnt) { + if (isdir) + count = 2*pxdesc->defcnt + 3; + else + count = pxdesc->defcnt + 3; + } else + count = 3; + pydesc = (struct POSIX_SECURITY*)malloc( + sizeof(struct POSIX_SECURITY) + count*sizeof(struct POSIX_ACE)); + if (pydesc) { + /* + * Copy inherited tags and adapt perms + * Use requested mode, ignoring umask + * (not possible with older versions of fuse) + */ + tagsset = 0; + defcnt = (pxdesc ? pxdesc->defcnt : 0); + for (i=defcnt-1; i>=0; i--) { + pyace = &pydesc->acl.ace[i]; + *pyace = pxdesc->acl.ace[pxdesc->firstdef + i]; + switch (pyace->tag) { + case POSIX_ACL_USER_OBJ : + pyace->perms &= (mode >> 6) & 7; + break; + case POSIX_ACL_GROUP_OBJ : + if (!(tagsset & POSIX_ACL_MASK)) + pyace->perms &= (mode >> 3) & 7; + break; + case POSIX_ACL_OTHER : + pyace->perms &= mode & 7; + break; + case POSIX_ACL_MASK : + pyace->perms &= (mode >> 3) & 7; + break; + default : + break; + } + tagsset |= pyace->tag; + } + pydesc->acccnt = defcnt; + /* + * If some standard tags were missing, append them from mode + * and sort the list + * Here we have to use the umask'ed mode + */ + if (~tagsset & (POSIX_ACL_USER_OBJ + | POSIX_ACL_GROUP_OBJ | POSIX_ACL_OTHER)) { + i = defcnt; + /* owner was missing */ + if (!(tagsset & POSIX_ACL_USER_OBJ)) { + pyace = &pydesc->acl.ace[i]; + pyace->tag = POSIX_ACL_USER_OBJ; + pyace->id = -1; + pyace->perms = ((mode & ~mask) >> 6) & 7; + tagsset |= POSIX_ACL_USER_OBJ; + i++; + } + /* owning group was missing */ + if (!(tagsset & POSIX_ACL_GROUP_OBJ)) { + pyace = &pydesc->acl.ace[i]; + pyace->tag = POSIX_ACL_GROUP_OBJ; + pyace->id = -1; + pyace->perms = ((mode & ~mask) >> 3) & 7; + tagsset |= POSIX_ACL_GROUP_OBJ; + i++; + } + /* other was missing */ + if (!(tagsset & POSIX_ACL_OTHER)) { + pyace = &pydesc->acl.ace[i]; + pyace->tag = POSIX_ACL_OTHER; + pyace->id = -1; + pyace->perms = mode & ~mask & 7; + tagsset |= POSIX_ACL_OTHER; + i++; + } + pydesc->acccnt = i; + pydesc->firstdef = i; + pydesc->defcnt = 0; + ntfs_sort_posix(pydesc); + } + + /* + * append as a default ACL if a directory + */ + pydesc->firstdef = pydesc->acccnt; + if (defcnt && isdir) { + size = sizeof(struct POSIX_ACE)*defcnt; + memcpy(&pydesc->acl.ace[pydesc->firstdef], + &pxdesc->acl.ace[pxdesc->firstdef],size); + pydesc->defcnt = defcnt; + } else { + pydesc->defcnt = 0; + } + /* assume special bits are not inherited */ + posix_header(pydesc, mode & 07000); + if (!ntfs_valid_posix(pydesc)) { + ntfs_log_error("Error building an inherited Posix desc\n"); + errno = EIO; + free(pydesc); + pydesc = (struct POSIX_SECURITY*)NULL; + } + } else + errno = ENOMEM; + return (pydesc); +} + +static int merge_lists_posix(struct POSIX_ACE *targetace, + const struct POSIX_ACE *firstace, + const struct POSIX_ACE *secondace, + int firstcnt, int secondcnt) +{ + int k; + + k = 0; + /* + * No list is exhausted : + * if same tag+id in both list : + * ignore ACE from second list + * else take the one with smaller tag+id + */ + while ((firstcnt > 0) && (secondcnt > 0)) + if ((firstace->tag == secondace->tag) + && (firstace->id == secondace->id)) { + secondace++; + secondcnt--; + } else + if ((firstace->tag < secondace->tag) + || ((firstace->tag == secondace->tag) + && (firstace->id < secondace->id))) { + targetace->tag = firstace->tag; + targetace->id = firstace->id; + targetace->perms = firstace->perms; + firstace++; + targetace++; + firstcnt--; + k++; + } else { + targetace->tag = secondace->tag; + targetace->id = secondace->id; + targetace->perms = secondace->perms; + secondace++; + targetace++; + secondcnt--; + k++; + } + /* + * One list is exhausted, copy the other one + */ + while (firstcnt > 0) { + targetace->tag = firstace->tag; + targetace->id = firstace->id; + targetace->perms = firstace->perms; + firstace++; + targetace++; + firstcnt--; + k++; + } + while (secondcnt > 0) { + targetace->tag = secondace->tag; + targetace->id = secondace->id; + targetace->perms = secondace->perms; + secondace++; + targetace++; + secondcnt--; + k++; + } + return (k); +} + +/* + * Merge two Posix ACLs + * The input ACLs have to be adequately sorted + * + * Returns the merged ACL, which is allocated and has to be freed by caller, + * or NULL if failed + */ + +struct POSIX_SECURITY *ntfs_merge_descr_posix(const struct POSIX_SECURITY *first, + const struct POSIX_SECURITY *second) +{ + struct POSIX_SECURITY *pxdesc; + struct POSIX_ACE *targetace; + const struct POSIX_ACE *firstace; + const struct POSIX_ACE *secondace; + size_t size; + int k; + + size = sizeof(struct POSIX_SECURITY) + + (first->acccnt + first->defcnt + + second->acccnt + second->defcnt)*sizeof(struct POSIX_ACE); + pxdesc = (struct POSIX_SECURITY*)malloc(size); + if (pxdesc) { + /* + * merge access ACEs + */ + firstace = first->acl.ace; + secondace = second->acl.ace; + targetace = pxdesc->acl.ace; + k = merge_lists_posix(targetace,firstace,secondace, + first->acccnt,second->acccnt); + pxdesc->acccnt = k; + /* + * merge default ACEs + */ + pxdesc->firstdef = k; + firstace = &first->acl.ace[first->firstdef]; + secondace = &second->acl.ace[second->firstdef]; + targetace = &pxdesc->acl.ace[k]; + k = merge_lists_posix(targetace,firstace,secondace, + first->defcnt,second->defcnt); + pxdesc->defcnt = k; + /* + * build header + */ + pxdesc->acl.version = POSIX_VERSION; + pxdesc->acl.flags = 0; + pxdesc->acl.filler = 0; + pxdesc->mode = 0; + pxdesc->tagsset = 0; + } else + errno = ENOMEM; + return (pxdesc); +} + +struct BUILD_CONTEXT { + BOOL isdir; + BOOL adminowns; + BOOL groupowns; + u16 selfuserperms; + u16 selfgrpperms; + u16 grpperms; + u16 othperms; + u16 mask; + u16 designates; + u16 withmask; + u16 rootspecial; +} ; + + + +static BOOL build_user_denials(ACL *pacl, + const SID *usid, struct MAPPING* const mapping[], + ACE_FLAGS flags, const struct POSIX_ACE *pxace, + struct BUILD_CONTEXT *pset) +{ + BIGSID defsid; + ACCESS_ALLOWED_ACE *pdace; + const SID *sid; + int sidsz; + int pos; + int acecnt; + le32 grants; + le32 denials; + u16 perms; + u16 mixperms; + u16 tag; + BOOL rejected; + BOOL rootuser; + BOOL avoidmask; + + rejected = FALSE; + tag = pxace->tag; + perms = pxace->perms; + rootuser = FALSE; + pos = le16_to_cpu(pacl->size); + acecnt = le16_to_cpu(pacl->ace_count); + avoidmask = (pset->mask == (POSIX_PERM_R | POSIX_PERM_W | POSIX_PERM_X)) + && ((pset->designates && pset->withmask) + || (!pset->designates && !pset->withmask)); + if (tag == POSIX_ACL_USER_OBJ) { + sid = usid; + sidsz = ntfs_sid_size(sid); + grants = OWNER_RIGHTS; + } else { + if (pxace->id) { + sid = ntfs_find_usid(mapping[MAPUSERS], + pxace->id, (SID*)&defsid); + grants = WORLD_RIGHTS; + } else { + sid = adminsid; + rootuser = TRUE; + grants = WORLD_RIGHTS & ~ROOT_OWNER_UNMARK; + } + if (sid) { + sidsz = ntfs_sid_size(sid); + /* + * Insert denial of complement of mask for + * each designated user (except root) + * WRITE_OWNER is inserted so that + * the mask can be identified + */ + if (!avoidmask && !rootuser) { + denials = WRITE_OWNER; + pdace = (ACCESS_DENIED_ACE*)&((char*)pacl)[pos]; + if (pset->isdir) { + if (!(pset->mask & POSIX_PERM_X)) + denials |= DIR_EXEC; + if (!(pset->mask & POSIX_PERM_W)) + denials |= DIR_WRITE; + if (!(pset->mask & POSIX_PERM_R)) + denials |= DIR_READ; + } else { + if (!(pset->mask & POSIX_PERM_X)) + denials |= FILE_EXEC; + if (!(pset->mask & POSIX_PERM_W)) + denials |= FILE_WRITE; + if (!(pset->mask & POSIX_PERM_R)) + denials |= FILE_READ; + } + if (rootuser) + grants &= ~ROOT_OWNER_UNMARK; + pdace->type = ACCESS_DENIED_ACE_TYPE; + pdace->flags = flags; + pdace->size = cpu_to_le16(sidsz + 8); + pdace->mask = denials; + memcpy((char*)&pdace->sid, sid, sidsz); + pos += sidsz + 8; + acecnt++; + } + } else + rejected = TRUE; + } + if (!rejected) { + if (pset->isdir) { + if (perms & POSIX_PERM_X) + grants |= DIR_EXEC; + if (perms & POSIX_PERM_W) + grants |= DIR_WRITE; + if (perms & POSIX_PERM_R) + grants |= DIR_READ; + } else { + if (perms & POSIX_PERM_X) + grants |= FILE_EXEC; + if (perms & POSIX_PERM_W) + grants |= FILE_WRITE; + if (perms & POSIX_PERM_R) + grants |= FILE_READ; + } + + /* a possible ACE to deny owner what he/she would */ + /* induely get from administrator, group or world */ + /* unless owner is administrator or group */ + + denials = const_cpu_to_le32(0); + pdace = (ACCESS_DENIED_ACE*)&((char*)pacl)[pos]; + if (!pset->adminowns && !rootuser) { + if (!pset->groupowns) { + mixperms = pset->grpperms | pset->othperms; + if (tag == POSIX_ACL_USER_OBJ) + mixperms |= pset->selfuserperms; + if (pset->isdir) { + if (mixperms & POSIX_PERM_X) + denials |= DIR_EXEC; + if (mixperms & POSIX_PERM_W) + denials |= DIR_WRITE; + if (mixperms & POSIX_PERM_R) + denials |= DIR_READ; + } else { + if (mixperms & POSIX_PERM_X) + denials |= FILE_EXEC; + if (mixperms & POSIX_PERM_W) + denials |= FILE_WRITE; + if (mixperms & POSIX_PERM_R) + denials |= FILE_READ; + } + } else { + mixperms = ~pset->grpperms & pset->othperms; + if (tag == POSIX_ACL_USER_OBJ) + mixperms |= pset->selfuserperms; + if (pset->isdir) { + if (mixperms & POSIX_PERM_X) + denials |= DIR_EXEC; + if (mixperms & POSIX_PERM_W) + denials |= DIR_WRITE; + if (mixperms & POSIX_PERM_R) + denials |= DIR_READ; + } else { + if (mixperms & POSIX_PERM_X) + denials |= FILE_EXEC; + if (mixperms & POSIX_PERM_W) + denials |= FILE_WRITE; + if (mixperms & POSIX_PERM_R) + denials |= FILE_READ; + } + } + denials &= ~grants; + if (denials) { + pdace->type = ACCESS_DENIED_ACE_TYPE; + pdace->flags = flags; + pdace->size = cpu_to_le16(sidsz + 8); + pdace->mask = denials; + memcpy((char*)&pdace->sid, sid, sidsz); + pos += sidsz + 8; + acecnt++; + } + } + } + pacl->size = cpu_to_le16(pos); + pacl->ace_count = cpu_to_le16(acecnt); + return (!rejected); +} + +static BOOL build_user_grants(ACL *pacl, + const SID *usid, struct MAPPING* const mapping[], + ACE_FLAGS flags, const struct POSIX_ACE *pxace, + struct BUILD_CONTEXT *pset) +{ + BIGSID defsid; + ACCESS_ALLOWED_ACE *pgace; + const SID *sid; + int sidsz; + int pos; + int acecnt; + le32 grants; + u16 perms; + u16 tag; + BOOL rejected; + BOOL rootuser; + + rejected = FALSE; + tag = pxace->tag; + perms = pxace->perms; + rootuser = FALSE; + pos = le16_to_cpu(pacl->size); + acecnt = le16_to_cpu(pacl->ace_count); + if (tag == POSIX_ACL_USER_OBJ) { + sid = usid; + sidsz = ntfs_sid_size(sid); + grants = OWNER_RIGHTS; + } else { + if (pxace->id) { + sid = ntfs_find_usid(mapping[MAPUSERS], + pxace->id, (SID*)&defsid); + if (sid) + sidsz = ntfs_sid_size(sid); + else + rejected = TRUE; + grants = WORLD_RIGHTS; + } else { + sid = adminsid; + sidsz = ntfs_sid_size(sid); + rootuser = TRUE; + grants = WORLD_RIGHTS & ~ROOT_OWNER_UNMARK; + } + } + if (!rejected) { + if (pset->isdir) { + if (perms & POSIX_PERM_X) + grants |= DIR_EXEC; + if (perms & POSIX_PERM_W) + grants |= DIR_WRITE; + if (perms & POSIX_PERM_R) + grants |= DIR_READ; + } else { + if (perms & POSIX_PERM_X) + grants |= FILE_EXEC; + if (perms & POSIX_PERM_W) + grants |= FILE_WRITE; + if (perms & POSIX_PERM_R) + grants |= FILE_READ; + } + if (rootuser) + grants &= ~ROOT_OWNER_UNMARK; + pgace = (ACCESS_DENIED_ACE*)&((char*)pacl)[pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + pgace->size = cpu_to_le16(sidsz + 8); + pgace->flags = flags; + pgace->mask = grants; + memcpy((char*)&pgace->sid, sid, sidsz); + pos += sidsz + 8; + acecnt = le16_to_cpu(pacl->ace_count) + 1; + pacl->ace_count = cpu_to_le16(acecnt); + pacl->size = cpu_to_le16(pos); + } + return (!rejected); +} + + + /* a grant ACE for group */ + /* unless group-obj has the same rights as world */ + /* but present if group is owner or owner is administrator */ + /* this ACE will be inserted after denials for group */ + +static BOOL build_group_denials_grant(ACL *pacl, + const SID *gsid, struct MAPPING* const mapping[], + ACE_FLAGS flags, const struct POSIX_ACE *pxace, + struct BUILD_CONTEXT *pset) +{ + BIGSID defsid; + ACCESS_ALLOWED_ACE *pdace; + ACCESS_ALLOWED_ACE *pgace; + const SID *sid; + int sidsz; + int pos; + int acecnt; + le32 grants; + le32 denials; + u16 perms; + u16 mixperms; + u16 tag; + BOOL avoidmask; + BOOL rootgroup; + BOOL rejected; + + rejected = FALSE; + tag = pxace->tag; + perms = pxace->perms; + pos = le16_to_cpu(pacl->size); + acecnt = le16_to_cpu(pacl->ace_count); + rootgroup = FALSE; + avoidmask = (pset->mask == (POSIX_PERM_R | POSIX_PERM_W | POSIX_PERM_X)) + && ((pset->designates && pset->withmask) + || (!pset->designates && !pset->withmask)); + if (tag == POSIX_ACL_GROUP_OBJ) + sid = gsid; + else + if (pxace->id) + sid = ntfs_find_gsid(mapping[MAPGROUPS], + pxace->id, (SID*)&defsid); + else { + sid = adminsid; + rootgroup = TRUE; + } + if (sid) { + sidsz = ntfs_sid_size(sid); + /* + * Insert denial of complement of mask for + * each group + * WRITE_OWNER is inserted so that + * the mask can be identified + * Note : this mask may lead on Windows to + * deny rights to administrators belonging + * to some user group + */ + if ((!avoidmask && !rootgroup) + || (pset->rootspecial + && (tag == POSIX_ACL_GROUP_OBJ))) { + denials = WRITE_OWNER; + pdace = (ACCESS_DENIED_ACE*)&((char*)pacl)[pos]; + if (pset->isdir) { + if (!(pset->mask & POSIX_PERM_X)) + denials |= DIR_EXEC; + if (!(pset->mask & POSIX_PERM_W)) + denials |= DIR_WRITE; + if (!(pset->mask & POSIX_PERM_R)) + denials |= DIR_READ; + } else { + if (!(pset->mask & POSIX_PERM_X)) + denials |= FILE_EXEC; + if (!(pset->mask & POSIX_PERM_W)) + denials |= FILE_WRITE; + if (!(pset->mask & POSIX_PERM_R)) + denials |= FILE_READ; + } + pdace->type = ACCESS_DENIED_ACE_TYPE; + pdace->flags = flags; + pdace->size = cpu_to_le16(sidsz + 8); + pdace->mask = denials; + memcpy((char*)&pdace->sid, sid, sidsz); + pos += sidsz + 8; + acecnt++; + } + } else + rejected = TRUE; + if (!rejected + && (pset->adminowns + || pset->groupowns + || avoidmask + || rootgroup + || (perms != pset->othperms))) { + grants = WORLD_RIGHTS; + if (rootgroup) + grants &= ~ROOT_GROUP_UNMARK; + if (pset->isdir) { + if (perms & POSIX_PERM_X) + grants |= DIR_EXEC; + if (perms & POSIX_PERM_W) + grants |= DIR_WRITE; + if (perms & POSIX_PERM_R) + grants |= DIR_READ; + } else { + if (perms & POSIX_PERM_X) + grants |= FILE_EXEC; + if (perms & POSIX_PERM_W) + grants |= FILE_WRITE; + if (perms & POSIX_PERM_R) + grants |= FILE_READ; + } + + /* a possible ACE to deny group what it would get from world */ + /* or administrator, unless owner is administrator or group */ + + denials = const_cpu_to_le32(0); + pdace = (ACCESS_DENIED_ACE*)&((char*)pacl)[pos]; + if (!pset->adminowns + && !pset->groupowns + && !rootgroup) { + mixperms = pset->othperms; + if (tag == POSIX_ACL_GROUP_OBJ) + mixperms |= pset->selfgrpperms; + if (pset->isdir) { + if (mixperms & POSIX_PERM_X) + denials |= DIR_EXEC; + if (mixperms & POSIX_PERM_W) + denials |= DIR_WRITE; + if (mixperms & POSIX_PERM_R) + denials |= DIR_READ; + } else { + if (mixperms & POSIX_PERM_X) + denials |= FILE_EXEC; + if (mixperms & POSIX_PERM_W) + denials |= FILE_WRITE; + if (mixperms & POSIX_PERM_R) + denials |= FILE_READ; + } + denials &= ~(grants | OWNER_RIGHTS); + if (denials) { + pdace->type = ACCESS_DENIED_ACE_TYPE; + pdace->flags = flags; + pdace->size = cpu_to_le16(sidsz + 8); + pdace->mask = denials; + memcpy((char*)&pdace->sid, sid, sidsz); + pos += sidsz + 8; + acecnt++; + } + } + + /* now insert grants to group if more than world */ + if (pset->adminowns + || pset->groupowns + || (avoidmask && (pset->designates || pset->withmask)) + || (perms & ~pset->othperms) + || (pset->rootspecial + && (tag == POSIX_ACL_GROUP_OBJ)) + || (tag == POSIX_ACL_GROUP)) { + if (rootgroup) + grants &= ~ROOT_GROUP_UNMARK; + pgace = (ACCESS_DENIED_ACE*)&((char*)pacl)[pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + pgace->flags = flags; + pgace->size = cpu_to_le16(sidsz + 8); + pgace->mask = grants; + memcpy((char*)&pgace->sid, sid, sidsz); + pos += sidsz + 8; + acecnt++; + } + } + pacl->size = cpu_to_le16(pos); + pacl->ace_count = cpu_to_le16(acecnt); + return (!rejected); +} + + +/* + * Build an ACL composed of several ACE's + * returns size of ACL or zero if failed + * + * Three schemes are defined : + * + * 1) if root is neither owner nor group up to 7 ACE's are set up : + * - denials to owner (preventing grants to world or group to apply) + * + mask denials to designated user (unless mask allows all) + * + denials to designated user + * - grants to owner (always present - first grant) + * + grants to designated user + * + mask denial to group (unless mask allows all) + * - denials to group (preventing grants to world to apply) + * - grants to group (unless group has no more than world rights) + * + mask denials to designated group (unless mask allows all) + * + grants to designated group + * + denials to designated group + * - grants to world (unless none) + * - full privileges to administrator, always present + * - full privileges to system, always present + * + * The same scheme is applied for Posix ACLs, with the mask represented + * as denials prepended to grants for designated users and groups + * + * This is inspired by an Internet Draft from Marius Aamodt Eriksen + * for mapping NFSv4 ACLs to Posix ACLs (draft-ietf-nfsv4-acl-mapping-00.txt) + * More recent versions of the draft (draft-ietf-nfsv4-acl-mapping-05.txt) + * are not followed, as they ignore the Posix mask and lead to + * loss of compatibility with Linux implementations on other fs. + * + * Note that denials to group are located after grants to owner. + * This only occurs in the unfrequent situation where world + * has more rights than group and cannot be avoided if owner and other + * have some common right which is denied to group (eg for mode 745 + * executing has to be denied to group, but not to owner or world). + * This rare situation is processed by Windows correctly, but + * Windows utilities may want to change the order, with a + * consequence of applying the group denials to the Windows owner. + * The interpretation on Linux is not affected by the order change. + * + * 2) if root is either owner or group, two problems arise : + * - granting full rights to administrator (as needed to transpose + * to Windows rights bypassing granting to root) would imply + * Linux permissions to always be seen as rwx, no matter the chmod + * - there is no different SID to separate an administrator owner + * from an administrator group. Hence Linux permissions for owner + * would always be similar to permissions to group. + * + * as a work-around, up to 5 ACE's are set up if owner or group : + * - grants to owner, always present at first position + * - grants to group, always present + * - grants to world, unless none + * - full privileges to administrator, always present + * - full privileges to system, always present + * + * On Windows, these ACE's are processed normally, though they + * are redundant (owner, group and administrator are the same, + * as a consequence any denials would damage administrator rights) + * but on Linux, privileges to administrator are ignored (they + * are not needed as root has always full privileges), and + * neither grants to group are applied to owner, nor grants to + * world are applied to owner or group. + * + * 3) finally a similar situation arises when group is owner (they + * have the same SID), but is not root. + * In this situation up to 6 ACE's are set up : + * + * - denials to owner (preventing grants to world to apply) + * - grants to owner (always present) + * - grants to group (unless groups has same rights as world) + * - grants to world (unless none) + * - full privileges to administrator, always present + * - full privileges to system, always present + * + * On Windows, these ACE's are processed normally, though they + * are redundant (as owner and group are the same), but this has + * no impact on administrator rights + * + * Special flags (S_ISVTX, S_ISGID, S_ISUID) : + * an extra null ACE is inserted to hold these flags, using + * the same conventions as cygwin. + * + */ + +static int buildacls_posix(struct MAPPING* const mapping[], + char *secattr, int offs, const struct POSIX_SECURITY *pxdesc, + int isdir, const SID *usid, const SID *gsid) +{ + struct BUILD_CONTEXT aceset[2], *pset; + BOOL adminowns; + BOOL groupowns; + ACL *pacl; + ACCESS_ALLOWED_ACE *pgace; + ACCESS_ALLOWED_ACE *pdace; + const struct POSIX_ACE *pxace; + BOOL ok; + mode_t mode; + u16 tag; + u16 perms; + ACE_FLAGS flags; + int pos; + int i; + int k; + BIGSID defsid; + const SID *sid; + int acecnt; + int usidsz; + int wsidsz; + int asidsz; + int ssidsz; + int nsidsz; + le32 grants; + + usidsz = ntfs_sid_size(usid); + wsidsz = ntfs_sid_size(worldsid); + asidsz = ntfs_sid_size(adminsid); + ssidsz = ntfs_sid_size(systemsid); + mode = pxdesc->mode; + /* adminowns and groupowns are used for both lists */ + adminowns = ntfs_same_sid(usid, adminsid) + || ntfs_same_sid(gsid, adminsid); + groupowns = !adminowns && ntfs_same_sid(usid, gsid); + + ok = TRUE; + + /* ACL header */ + pacl = (ACL*)&secattr[offs]; + pacl->revision = ACL_REVISION; + pacl->alignment1 = 0; + pacl->size = cpu_to_le16(sizeof(ACL) + usidsz + 8); + pacl->ace_count = const_cpu_to_le16(0); + pacl->alignment2 = const_cpu_to_le16(0); + + /* + * Determine what is allowed to some group or world + * to prevent designated users or other groups to get + * rights from groups or world + * Do the same if owner and group appear as designated + * user or group + * Also get global mask + */ + for (k=0; k<2; k++) { + pset = &aceset[k]; + pset->selfuserperms = 0; + pset->selfgrpperms = 0; + pset->grpperms = 0; + pset->othperms = 0; + pset->mask = (POSIX_PERM_R | POSIX_PERM_W | POSIX_PERM_X); + pset->designates = 0; + pset->withmask = 0; + pset->rootspecial = 0; + pset->adminowns = adminowns; + pset->groupowns = groupowns; + pset->isdir = isdir; + } + + for (i=pxdesc->acccnt+pxdesc->defcnt-1; i>=0; i--) { + if (i >= pxdesc->acccnt) { + pset = &aceset[1]; + pxace = &pxdesc->acl.ace[i + pxdesc->firstdef - pxdesc->acccnt]; + } else { + pset = &aceset[0]; + pxace = &pxdesc->acl.ace[i]; + } + switch (pxace->tag) { + case POSIX_ACL_USER : + pset->designates++; + if (pxace->id) { + sid = ntfs_find_usid(mapping[MAPUSERS], + pxace->id, (SID*)&defsid); + if (sid && ntfs_same_sid(sid,usid)) + pset->selfuserperms |= pxace->perms; + } else + /* root as designated user is processed apart */ + pset->rootspecial = TRUE; + break; + case POSIX_ACL_GROUP : + pset->designates++; + if (pxace->id) { + sid = ntfs_find_gsid(mapping[MAPUSERS], + pxace->id, (SID*)&defsid); + if (sid && ntfs_same_sid(sid,gsid)) + pset->selfgrpperms |= pxace->perms; + } else + /* root as designated group is processed apart */ + pset->rootspecial = TRUE; + /* fall through */ + case POSIX_ACL_GROUP_OBJ : + pset->grpperms |= pxace->perms; + break; + case POSIX_ACL_OTHER : + pset->othperms = pxace->perms; + break; + case POSIX_ACL_MASK : + pset->withmask++; + pset->mask = pxace->perms; + default : + break; + } + } + +if (pxdesc->defcnt && (pxdesc->firstdef != pxdesc->acccnt)) { +ntfs_log_error("** error : access and default not consecutive\n"); +return (0); +} + /* + * First insert all denials for owner and each + * designated user (with mask if needed) + */ + + pacl->ace_count = const_cpu_to_le16(0); + pacl->size = const_cpu_to_le16(sizeof(ACL)); + for (i=0; (i<(pxdesc->acccnt + pxdesc->defcnt)) && ok; i++) { + if (i >= pxdesc->acccnt) { + flags = INHERIT_ONLY_ACE + | OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE; + pset = &aceset[1]; + pxace = &pxdesc->acl.ace[i + pxdesc->firstdef - pxdesc->acccnt]; + } else { + if (pxdesc->defcnt) + flags = NO_PROPAGATE_INHERIT_ACE; + else + flags = (isdir ? DIR_INHERITANCE + : FILE_INHERITANCE); + pset = &aceset[0]; + pxace = &pxdesc->acl.ace[i]; + } + tag = pxace->tag; + perms = pxace->perms; + switch (tag) { + + /* insert denial ACEs for each owner or allowed user */ + + case POSIX_ACL_USER : + case POSIX_ACL_USER_OBJ : + + ok = build_user_denials(pacl, + usid, mapping, flags, pxace, pset); + break; + default : + break; + } + } + + /* + * for directories, insert a world execution denial + * inherited to plain files. + * This is to prevent Windows from granting execution + * of files through inheritance from parent directory + */ + + if (isdir && ok) { + pos = le16_to_cpu(pacl->size); + pdace = (ACCESS_DENIED_ACE*) &secattr[offs + pos]; + pdace->type = ACCESS_DENIED_ACE_TYPE; + pdace->flags = INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE; + pdace->size = cpu_to_le16(wsidsz + 8); + pdace->mask = FILE_EXEC; + memcpy((char*)&pdace->sid, worldsid, wsidsz); + pos += wsidsz + 8; + acecnt = le16_to_cpu(pacl->ace_count) + 1; + pacl->ace_count = cpu_to_le16(acecnt); + pacl->size = cpu_to_le16(pos); + } + + /* + * now insert (if needed) + * - grants to owner and designated users + * - mask and denials for all groups + * - grants to other + */ + + for (i=0; (i<(pxdesc->acccnt + pxdesc->defcnt)) && ok; i++) { + if (i >= pxdesc->acccnt) { + flags = INHERIT_ONLY_ACE + | OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE; + pset = &aceset[1]; + pxace = &pxdesc->acl.ace[i + pxdesc->firstdef - pxdesc->acccnt]; + } else { + if (pxdesc->defcnt) + flags = NO_PROPAGATE_INHERIT_ACE; + else + flags = (isdir ? DIR_INHERITANCE + : FILE_INHERITANCE); + pset = &aceset[0]; + pxace = &pxdesc->acl.ace[i]; + } + tag = pxace->tag; + perms = pxace->perms; + switch (tag) { + + /* ACE for each owner or allowed user */ + + case POSIX_ACL_USER : + case POSIX_ACL_USER_OBJ : + ok = build_user_grants(pacl,usid, + mapping,flags,pxace,pset); + break; + + case POSIX_ACL_GROUP_OBJ : + /* denials and grants for group when needed */ + if (pset->groupowns && !pset->adminowns + && (pset->grpperms == pset->othperms) + && !pset->designates && !pset->withmask) { + ok = TRUE; + } else { + ok = build_group_denials_grant(pacl,gsid, + mapping,flags,pxace,pset); + } + break; + + case POSIX_ACL_GROUP : + + /* denials and grants for designated groups */ + + ok = build_group_denials_grant(pacl,gsid, + mapping,flags,pxace,pset); + break; + + case POSIX_ACL_OTHER : + + /* grants for other users */ + + pos = le16_to_cpu(pacl->size); + pgace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + grants = WORLD_RIGHTS; + if (isdir) { + if (perms & POSIX_PERM_X) + grants |= DIR_EXEC; + if (perms & POSIX_PERM_W) + grants |= DIR_WRITE; + if (perms & POSIX_PERM_R) + grants |= DIR_READ; + } else { + if (perms & POSIX_PERM_X) + grants |= FILE_EXEC; + if (perms & POSIX_PERM_W) + grants |= FILE_WRITE; + if (perms & POSIX_PERM_R) + grants |= FILE_READ; + } + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + pgace->flags = flags; + pgace->size = cpu_to_le16(wsidsz + 8); + pgace->mask = grants; + memcpy((char*)&pgace->sid, worldsid, wsidsz); + pos += wsidsz + 8; + acecnt = le16_to_cpu(pacl->ace_count) + 1; + pacl->ace_count = cpu_to_le16(acecnt); + pacl->size = cpu_to_le16(pos); + break; + } + } + + if (!ok) { + errno = EINVAL; + pos = 0; + } else { + /* an ACE for administrators */ + /* always full access */ + + pos = le16_to_cpu(pacl->size); + acecnt = le16_to_cpu(pacl->ace_count); + if (isdir) + flags = OBJECT_INHERIT_ACE + | CONTAINER_INHERIT_ACE; + else + flags = NO_PROPAGATE_INHERIT_ACE; + pgace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + pgace->flags = flags; + pgace->size = cpu_to_le16(asidsz + 8); + grants = OWNER_RIGHTS | FILE_READ | FILE_WRITE | FILE_EXEC; + pgace->mask = grants; + memcpy((char*)&pgace->sid, adminsid, asidsz); + pos += asidsz + 8; + acecnt++; + + /* an ACE for system (needed ?) */ + /* always full access */ + + pgace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + pgace->flags = flags; + pgace->size = cpu_to_le16(ssidsz + 8); + grants = OWNER_RIGHTS | FILE_READ | FILE_WRITE | FILE_EXEC; + pgace->mask = grants; + memcpy((char*)&pgace->sid, systemsid, ssidsz); + pos += ssidsz + 8; + acecnt++; + + /* a null ACE to hold special flags */ + /* using the same representation as cygwin */ + + if (mode & (S_ISVTX | S_ISGID | S_ISUID)) { + nsidsz = ntfs_sid_size(nullsid); + pgace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + pgace->flags = NO_PROPAGATE_INHERIT_ACE; + pgace->size = cpu_to_le16(nsidsz + 8); + grants = const_cpu_to_le32(0); + if (mode & S_ISUID) + grants |= FILE_APPEND_DATA; + if (mode & S_ISGID) + grants |= FILE_WRITE_DATA; + if (mode & S_ISVTX) + grants |= FILE_READ_DATA; + pgace->mask = grants; + memcpy((char*)&pgace->sid, nullsid, nsidsz); + pos += nsidsz + 8; + acecnt++; + } + + /* fix ACL header */ + pacl->size = cpu_to_le16(pos); + pacl->ace_count = cpu_to_le16(acecnt); + } + return (ok ? pos : 0); +} + +#endif /* POSIXACLS */ + +static int buildacls(char *secattr, int offs, mode_t mode, int isdir, + const SID * usid, const SID * gsid) +{ + ACL *pacl; + ACCESS_ALLOWED_ACE *pgace; + ACCESS_ALLOWED_ACE *pdace; + BOOL adminowns; + BOOL groupowns; + ACE_FLAGS gflags; + int pos; + int acecnt; + int usidsz; + int gsidsz; + int wsidsz; + int asidsz; + int ssidsz; + int nsidsz; + le32 grants; + le32 denials; + + usidsz = ntfs_sid_size(usid); + gsidsz = ntfs_sid_size(gsid); + wsidsz = ntfs_sid_size(worldsid); + asidsz = ntfs_sid_size(adminsid); + ssidsz = ntfs_sid_size(systemsid); + adminowns = ntfs_same_sid(usid, adminsid) + || ntfs_same_sid(gsid, adminsid); + groupowns = !adminowns && ntfs_same_sid(usid, gsid); + + /* ACL header */ + pacl = (ACL*)&secattr[offs]; + pacl->revision = ACL_REVISION; + pacl->alignment1 = 0; + pacl->size = cpu_to_le16(sizeof(ACL) + usidsz + 8); + pacl->ace_count = const_cpu_to_le16(1); + pacl->alignment2 = const_cpu_to_le16(0); + pos = sizeof(ACL); + acecnt = 0; + + /* compute a grant ACE for owner */ + /* this ACE will be inserted after denial for owner */ + + grants = OWNER_RIGHTS; + if (isdir) { + gflags = DIR_INHERITANCE; + if (mode & S_IXUSR) + grants |= DIR_EXEC; + if (mode & S_IWUSR) + grants |= DIR_WRITE; + if (mode & S_IRUSR) + grants |= DIR_READ; + } else { + gflags = FILE_INHERITANCE; + if (mode & S_IXUSR) + grants |= FILE_EXEC; + if (mode & S_IWUSR) + grants |= FILE_WRITE; + if (mode & S_IRUSR) + grants |= FILE_READ; + } + + /* a possible ACE to deny owner what he/she would */ + /* induely get from administrator, group or world */ + /* unless owner is administrator or group */ + + denials = const_cpu_to_le32(0); + pdace = (ACCESS_DENIED_ACE*) &secattr[offs + pos]; + if (!adminowns) { + if (!groupowns) { + if (isdir) { + pdace->flags = DIR_INHERITANCE; + if (mode & (S_IXGRP | S_IXOTH)) + denials |= DIR_EXEC; + if (mode & (S_IWGRP | S_IWOTH)) + denials |= DIR_WRITE; + if (mode & (S_IRGRP | S_IROTH)) + denials |= DIR_READ; + } else { + pdace->flags = FILE_INHERITANCE; + if (mode & (S_IXGRP | S_IXOTH)) + denials |= FILE_EXEC; + if (mode & (S_IWGRP | S_IWOTH)) + denials |= FILE_WRITE; + if (mode & (S_IRGRP | S_IROTH)) + denials |= FILE_READ; + } + } else { + if (isdir) { + pdace->flags = DIR_INHERITANCE; + if ((mode & S_IXOTH) && !(mode & S_IXGRP)) + denials |= DIR_EXEC; + if ((mode & S_IWOTH) && !(mode & S_IWGRP)) + denials |= DIR_WRITE; + if ((mode & S_IROTH) && !(mode & S_IRGRP)) + denials |= DIR_READ; + } else { + pdace->flags = FILE_INHERITANCE; + if ((mode & S_IXOTH) && !(mode & S_IXGRP)) + denials |= FILE_EXEC; + if ((mode & S_IWOTH) && !(mode & S_IWGRP)) + denials |= FILE_WRITE; + if ((mode & S_IROTH) && !(mode & S_IRGRP)) + denials |= FILE_READ; + } + } + denials &= ~grants; + if (denials) { + pdace->type = ACCESS_DENIED_ACE_TYPE; + pdace->size = cpu_to_le16(usidsz + 8); + pdace->mask = denials; + memcpy((char*)&pdace->sid, usid, usidsz); + pos += usidsz + 8; + acecnt++; + } + } + /* + * for directories, a world execution denial + * inherited to plain files + */ + + if (isdir) { + pdace = (ACCESS_DENIED_ACE*) &secattr[offs + pos]; + pdace->type = ACCESS_DENIED_ACE_TYPE; + pdace->flags = INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE; + pdace->size = cpu_to_le16(wsidsz + 8); + pdace->mask = FILE_EXEC; + memcpy((char*)&pdace->sid, worldsid, wsidsz); + pos += wsidsz + 8; + acecnt++; + } + + + /* now insert grants to owner */ + pgace = (ACCESS_ALLOWED_ACE*) &secattr[offs + pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + pgace->size = cpu_to_le16(usidsz + 8); + pgace->flags = gflags; + pgace->mask = grants; + memcpy((char*)&pgace->sid, usid, usidsz); + pos += usidsz + 8; + acecnt++; + + /* a grant ACE for group */ + /* unless group has the same rights as world */ + /* but present if group is owner or owner is administrator */ + /* this ACE will be inserted after denials for group */ + + if (adminowns + || (((mode >> 3) ^ mode) & 7)) { + grants = WORLD_RIGHTS; + if (isdir) { + gflags = DIR_INHERITANCE; + if (mode & S_IXGRP) + grants |= DIR_EXEC; + if (mode & S_IWGRP) + grants |= DIR_WRITE; + if (mode & S_IRGRP) + grants |= DIR_READ; + } else { + gflags = FILE_INHERITANCE; + if (mode & S_IXGRP) + grants |= FILE_EXEC; + if (mode & S_IWGRP) + grants |= FILE_WRITE; + if (mode & S_IRGRP) + grants |= FILE_READ; + } + + /* a possible ACE to deny group what it would get from world */ + /* or administrator, unless owner is administrator or group */ + + denials = const_cpu_to_le32(0); + pdace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + if (!adminowns && !groupowns) { + if (isdir) { + pdace->flags = DIR_INHERITANCE; + if (mode & S_IXOTH) + denials |= DIR_EXEC; + if (mode & S_IWOTH) + denials |= DIR_WRITE; + if (mode & S_IROTH) + denials |= DIR_READ; + } else { + pdace->flags = FILE_INHERITANCE; + if (mode & S_IXOTH) + denials |= FILE_EXEC; + if (mode & S_IWOTH) + denials |= FILE_WRITE; + if (mode & S_IROTH) + denials |= FILE_READ; + } + denials &= ~(grants | OWNER_RIGHTS); + if (denials) { + pdace->type = ACCESS_DENIED_ACE_TYPE; + pdace->size = cpu_to_le16(gsidsz + 8); + pdace->mask = denials; + memcpy((char*)&pdace->sid, gsid, gsidsz); + pos += gsidsz + 8; + acecnt++; + } + } + + if (adminowns + || groupowns + || ((mode >> 3) & ~mode & 7)) { + /* now insert grants to group */ + /* if more rights than other */ + pgace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + pgace->flags = gflags; + pgace->size = cpu_to_le16(gsidsz + 8); + pgace->mask = grants; + memcpy((char*)&pgace->sid, gsid, gsidsz); + pos += gsidsz + 8; + acecnt++; + } + } + + /* an ACE for world users */ + + pgace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + grants = WORLD_RIGHTS; + if (isdir) { + pgace->flags = DIR_INHERITANCE; + if (mode & S_IXOTH) + grants |= DIR_EXEC; + if (mode & S_IWOTH) + grants |= DIR_WRITE; + if (mode & S_IROTH) + grants |= DIR_READ; + } else { + pgace->flags = FILE_INHERITANCE; + if (mode & S_IXOTH) + grants |= FILE_EXEC; + if (mode & S_IWOTH) + grants |= FILE_WRITE; + if (mode & S_IROTH) + grants |= FILE_READ; + } + pgace->size = cpu_to_le16(wsidsz + 8); + pgace->mask = grants; + memcpy((char*)&pgace->sid, worldsid, wsidsz); + pos += wsidsz + 8; + acecnt++; + + /* an ACE for administrators */ + /* always full access */ + + pgace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + if (isdir) + pgace->flags = DIR_INHERITANCE; + else + pgace->flags = FILE_INHERITANCE; + pgace->size = cpu_to_le16(asidsz + 8); + grants = OWNER_RIGHTS | FILE_READ | FILE_WRITE | FILE_EXEC; + pgace->mask = grants; + memcpy((char*)&pgace->sid, adminsid, asidsz); + pos += asidsz + 8; + acecnt++; + + /* an ACE for system (needed ?) */ + /* always full access */ + + pgace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + if (isdir) + pgace->flags = DIR_INHERITANCE; + else + pgace->flags = FILE_INHERITANCE; + pgace->size = cpu_to_le16(ssidsz + 8); + grants = OWNER_RIGHTS | FILE_READ | FILE_WRITE | FILE_EXEC; + pgace->mask = grants; + memcpy((char*)&pgace->sid, systemsid, ssidsz); + pos += ssidsz + 8; + acecnt++; + + /* a null ACE to hold special flags */ + /* using the same representation as cygwin */ + + if (mode & (S_ISVTX | S_ISGID | S_ISUID)) { + nsidsz = ntfs_sid_size(nullsid); + pgace = (ACCESS_ALLOWED_ACE*)&secattr[offs + pos]; + pgace->type = ACCESS_ALLOWED_ACE_TYPE; + pgace->flags = NO_PROPAGATE_INHERIT_ACE; + pgace->size = cpu_to_le16(nsidsz + 8); + grants = const_cpu_to_le32(0); + if (mode & S_ISUID) + grants |= FILE_APPEND_DATA; + if (mode & S_ISGID) + grants |= FILE_WRITE_DATA; + if (mode & S_ISVTX) + grants |= FILE_READ_DATA; + pgace->mask = grants; + memcpy((char*)&pgace->sid, nullsid, nsidsz); + pos += nsidsz + 8; + acecnt++; + } + + /* fix ACL header */ + pacl->size = cpu_to_le16(pos); + pacl->ace_count = cpu_to_le16(acecnt); + return (pos); +} + +#if POSIXACLS + +/* + * Build a full security descriptor from a Posix ACL + * returns descriptor in allocated memory, must free() after use + */ + +char *ntfs_build_descr_posix(struct MAPPING* const mapping[], + struct POSIX_SECURITY *pxdesc, + int isdir, const SID *usid, const SID *gsid) +{ + int newattrsz; + SECURITY_DESCRIPTOR_RELATIVE *pnhead; + char *newattr; + int aclsz; + int usidsz; + int gsidsz; + int wsidsz; + int asidsz; + int ssidsz; + int k; + + usidsz = ntfs_sid_size(usid); + gsidsz = ntfs_sid_size(gsid); + wsidsz = ntfs_sid_size(worldsid); + asidsz = ntfs_sid_size(adminsid); + ssidsz = ntfs_sid_size(systemsid); + + /* allocate enough space for the new security attribute */ + newattrsz = sizeof(SECURITY_DESCRIPTOR_RELATIVE) /* header */ + + usidsz + gsidsz /* usid and gsid */ + + sizeof(ACL) /* acl header */ + + 2*(8 + usidsz) /* two possible ACE for user */ + + 3*(8 + gsidsz) /* three possible ACE for group and mask */ + + 8 + wsidsz /* one ACE for world */ + + 8 + asidsz /* one ACE for admin */ + + 8 + ssidsz; /* one ACE for system */ + if (isdir) /* a world denial for directories */ + newattrsz += 8 + wsidsz; + if (pxdesc->mode & 07000) /* a NULL ACE for special modes */ + newattrsz += 8 + ntfs_sid_size(nullsid); + /* account for non-owning users and groups */ + for (k=0; kacccnt; k++) { + if ((pxdesc->acl.ace[k].tag == POSIX_ACL_USER) + || (pxdesc->acl.ace[k].tag == POSIX_ACL_GROUP)) + newattrsz += 3*MAX_SID_SIZE; + } + /* account for default ACE's */ + newattrsz += 2*MAX_SID_SIZE*pxdesc->defcnt; + newattr = (char*)ntfs_malloc(newattrsz); + if (newattr) { + /* build the main header part */ + pnhead = (SECURITY_DESCRIPTOR_RELATIVE*)newattr; + pnhead->revision = SECURITY_DESCRIPTOR_REVISION; + pnhead->alignment = 0; + /* + * The flag SE_DACL_PROTECTED prevents the ACL + * to be changed in an inheritance after creation + */ + pnhead->control = SE_DACL_PRESENT | SE_DACL_PROTECTED + | SE_SELF_RELATIVE; + /* + * Windows prefers ACL first, do the same to + * get the same hash value and avoid duplication + */ + /* build permissions */ + aclsz = buildacls_posix(mapping,newattr, + sizeof(SECURITY_DESCRIPTOR_RELATIVE), + pxdesc, isdir, usid, gsid); + if (aclsz && ((int)(sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz + usidsz + gsidsz) <= newattrsz)) { + /* append usid and gsid */ + memcpy(&newattr[sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz], usid, usidsz); + memcpy(&newattr[sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz + usidsz], gsid, gsidsz); + /* positions of ACL, USID and GSID into header */ + pnhead->owner = + cpu_to_le32(sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz); + pnhead->group = + cpu_to_le32(sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz + usidsz); + pnhead->sacl = const_cpu_to_le32(0); + pnhead->dacl = + const_cpu_to_le32(sizeof(SECURITY_DESCRIPTOR_RELATIVE)); + } else { + /* ACL failure (errno set) or overflow */ + free(newattr); + newattr = (char*)NULL; + if (aclsz) { + /* hope error was detected before overflowing */ + ntfs_log_error("Security descriptor is longer than expected\n"); + errno = EIO; + } + } + } else + errno = ENOMEM; + return (newattr); +} + +#endif /* POSIXACLS */ + +/* + * Build a full security descriptor + * returns descriptor in allocated memory, must free() after use + */ + +char *ntfs_build_descr(mode_t mode, + int isdir, const SID * usid, const SID * gsid) +{ + int newattrsz; + SECURITY_DESCRIPTOR_RELATIVE *pnhead; + char *newattr; + int aclsz; + int usidsz; + int gsidsz; + int wsidsz; + int asidsz; + int ssidsz; + + usidsz = ntfs_sid_size(usid); + gsidsz = ntfs_sid_size(gsid); + wsidsz = ntfs_sid_size(worldsid); + asidsz = ntfs_sid_size(adminsid); + ssidsz = ntfs_sid_size(systemsid); + + /* allocate enough space for the new security attribute */ + newattrsz = sizeof(SECURITY_DESCRIPTOR_RELATIVE) /* header */ + + usidsz + gsidsz /* usid and gsid */ + + sizeof(ACL) /* acl header */ + + 2*(8 + usidsz) /* two possible ACE for user */ + + 2*(8 + gsidsz) /* two possible ACE for group */ + + 8 + wsidsz /* one ACE for world */ + + 8 + asidsz /* one ACE for admin */ + + 8 + ssidsz; /* one ACE for system */ + if (isdir) /* a world denial for directories */ + newattrsz += 8 + wsidsz; + if (mode & 07000) /* a NULL ACE for special modes */ + newattrsz += 8 + ntfs_sid_size(nullsid); + newattr = (char*)ntfs_malloc(newattrsz); + if (newattr) { + /* build the main header part */ + pnhead = (SECURITY_DESCRIPTOR_RELATIVE*) newattr; + pnhead->revision = SECURITY_DESCRIPTOR_REVISION; + pnhead->alignment = 0; + /* + * The flag SE_DACL_PROTECTED prevents the ACL + * to be changed in an inheritance after creation + */ + pnhead->control = SE_DACL_PRESENT | SE_DACL_PROTECTED + | SE_SELF_RELATIVE; + /* + * Windows prefers ACL first, do the same to + * get the same hash value and avoid duplication + */ + /* build permissions */ + aclsz = buildacls(newattr, + sizeof(SECURITY_DESCRIPTOR_RELATIVE), + mode, isdir, usid, gsid); + if (((int)sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz + usidsz + gsidsz) <= newattrsz) { + /* append usid and gsid */ + memcpy(&newattr[sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz], usid, usidsz); + memcpy(&newattr[sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz + usidsz], gsid, gsidsz); + /* positions of ACL, USID and GSID into header */ + pnhead->owner = + cpu_to_le32(sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz); + pnhead->group = + cpu_to_le32(sizeof(SECURITY_DESCRIPTOR_RELATIVE) + + aclsz + usidsz); + pnhead->sacl = const_cpu_to_le32(0); + pnhead->dacl = + const_cpu_to_le32(sizeof(SECURITY_DESCRIPTOR_RELATIVE)); + } else { + /* hope error was detected before overflowing */ + free(newattr); + newattr = (char*)NULL; + ntfs_log_error("Security descriptor is longer than expected\n"); + errno = EIO; + } + } else + errno = ENOMEM; + return (newattr); +} + +/* + * Create a mode_t permission set + * from owner, group and world grants as represented in ACEs + */ + +static int merge_permissions(BOOL isdir, + le32 owner, le32 group, le32 world, le32 special) + +{ + int perm; + + perm = 0; + /* build owner permission */ + if (owner) { + if (isdir) { + /* exec if any of list, traverse */ + if (owner & DIR_GEXEC) + perm |= S_IXUSR; + /* write if any of addfile, adddir, delchild */ + if (owner & DIR_GWRITE) + perm |= S_IWUSR; + /* read if any of list */ + if (owner & DIR_GREAD) + perm |= S_IRUSR; + } else { + /* exec if execute or generic execute */ + if (owner & FILE_GEXEC) + perm |= S_IXUSR; + /* write if any of writedata or generic write */ + if (owner & FILE_GWRITE) + perm |= S_IWUSR; + /* read if any of readdata or generic read */ + if (owner & FILE_GREAD) + perm |= S_IRUSR; + } + } + /* build group permission */ + if (group) { + if (isdir) { + /* exec if any of list, traverse */ + if (group & DIR_GEXEC) + perm |= S_IXGRP; + /* write if any of addfile, adddir, delchild */ + if (group & DIR_GWRITE) + perm |= S_IWGRP; + /* read if any of list */ + if (group & DIR_GREAD) + perm |= S_IRGRP; + } else { + /* exec if execute */ + if (group & FILE_GEXEC) + perm |= S_IXGRP; + /* write if any of writedata, appenddata */ + if (group & FILE_GWRITE) + perm |= S_IWGRP; + /* read if any of readdata */ + if (group & FILE_GREAD) + perm |= S_IRGRP; + } + } + /* build world permission */ + if (world) { + if (isdir) { + /* exec if any of list, traverse */ + if (world & DIR_GEXEC) + perm |= S_IXOTH; + /* write if any of addfile, adddir, delchild */ + if (world & DIR_GWRITE) + perm |= S_IWOTH; + /* read if any of list */ + if (world & DIR_GREAD) + perm |= S_IROTH; + } else { + /* exec if execute */ + if (world & FILE_GEXEC) + perm |= S_IXOTH; + /* write if any of writedata, appenddata */ + if (world & FILE_GWRITE) + perm |= S_IWOTH; + /* read if any of readdata */ + if (world & FILE_GREAD) + perm |= S_IROTH; + } + } + /* build special permission flags */ + if (special) { + if (special & FILE_APPEND_DATA) + perm |= S_ISUID; + if (special & FILE_WRITE_DATA) + perm |= S_ISGID; + if (special & FILE_READ_DATA) + perm |= S_ISVTX; + } + return (perm); +} + +#if POSIXACLS + +/* + * Normalize a Posix ACL either from a sorted raw set of + * access ACEs or default ACEs + * (standard case : different owner, group and administrator) + */ + +static int norm_std_permissions_posix(struct POSIX_SECURITY *posix_desc, + BOOL groupowns, int start, int count, int target) +{ + int j,k; + s32 id; + u16 tag; + u16 tagsset; + struct POSIX_ACE *pxace; + mode_t grantgrps; + mode_t grantwrld; + mode_t denywrld; + mode_t allow; + mode_t deny; + mode_t perms; + mode_t mode; + + mode = 0; + tagsset = 0; + /* + * Determine what is granted to some group or world + * Also get denials to world which are meant to prevent + * execution flags to be inherited by plain files + */ + pxace = posix_desc->acl.ace; + grantgrps = 0; + grantwrld = 0; + denywrld = 0; + for (j=start; j<(start + count); j++) { + if (pxace[j].perms & POSIX_PERM_DENIAL) { + /* deny world exec unless for default */ + if ((pxace[j].tag == POSIX_ACL_OTHER) + && !start) + denywrld = pxace[j].perms; + } else { + switch (pxace[j].tag) { + case POSIX_ACL_GROUP_OBJ : + grantgrps |= pxace[j].perms; + break; + case POSIX_ACL_GROUP : + if (pxace[j].id) + grantgrps |= pxace[j].perms; + break; + case POSIX_ACL_OTHER : + grantwrld = pxace[j].perms; + break; + default : + break; + } + } + } + /* + * Collect groups of ACEs related to the same id + * and determine what is granted and what is denied. + * It is important the ACEs have been sorted + */ + j = start; + k = target; + while (j < (start + count)) { + tag = pxace[j].tag; + id = pxace[j].id; + if (pxace[j].perms & POSIX_PERM_DENIAL) { + deny = pxace[j].perms | denywrld; + allow = 0; + } else { + deny = denywrld; + allow = pxace[j].perms; + } + j++; + while ((j < (start + count)) + && (pxace[j].tag == tag) + && (pxace[j].id == id)) { + if (pxace[j].perms & POSIX_PERM_DENIAL) + deny |= pxace[j].perms; + else + allow |= pxace[j].perms; + j++; + } + /* + * Build the permissions equivalent to grants and denials + */ + if (groupowns) { + if (tag == POSIX_ACL_MASK) + perms = ~deny; + else + perms = allow & ~deny; + } else + switch (tag) { + case POSIX_ACL_USER_OBJ : + perms = (allow | grantgrps | grantwrld) & ~deny; + break; + case POSIX_ACL_USER : + if (id) + perms = (allow | grantgrps | grantwrld) + & ~deny; + else + perms = allow; + break; + case POSIX_ACL_GROUP_OBJ : + perms = (allow | grantwrld) & ~deny; + break; + case POSIX_ACL_GROUP : + if (id) + perms = (allow | grantwrld) & ~deny; + else + perms = allow; + break; + case POSIX_ACL_MASK : + perms = ~deny; + break; + default : + perms = allow & ~deny; + break; + } + /* + * Store into a Posix ACE + */ + if (tag != POSIX_ACL_SPECIAL) { + pxace[k].tag = tag; + pxace[k].id = id; + pxace[k].perms = perms + & (POSIX_PERM_R | POSIX_PERM_W | POSIX_PERM_X); + tagsset |= tag; + k++; + } + switch (tag) { + case POSIX_ACL_USER_OBJ : + mode |= ((perms & 7) << 6); + break; + case POSIX_ACL_GROUP_OBJ : + case POSIX_ACL_MASK : + mode = (mode & 07707) | ((perms & 7) << 3); + break; + case POSIX_ACL_OTHER : + mode |= perms & 7; + break; + case POSIX_ACL_SPECIAL : + mode |= (perms & (S_ISVTX | S_ISUID | S_ISGID)); + break; + default : + break; + } + } + if (!start) { /* not satisfactory */ + posix_desc->mode = mode; + posix_desc->tagsset = tagsset; + } + return (k - target); +} + +#endif /* POSIXACLS */ + +/* + * Interpret an ACL and extract meaningful grants + * (standard case : different owner, group and administrator) + */ + +static int build_std_permissions(const char *securattr, + const SID *usid, const SID *gsid, BOOL isdir) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const ACL *pacl; + const ACCESS_ALLOWED_ACE *pace; + int offdacl; + int offace; + int acecnt; + int nace; + BOOL noown; + le32 special; + le32 allowown, allowgrp, allowall; + le32 denyown, denygrp, denyall; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)securattr; + offdacl = le32_to_cpu(phead->dacl); + pacl = (const ACL*)&securattr[offdacl]; + special = const_cpu_to_le32(0); + allowown = allowgrp = allowall = const_cpu_to_le32(0); + denyown = denygrp = denyall = const_cpu_to_le32(0); + noown = TRUE; + if (offdacl) { + acecnt = le16_to_cpu(pacl->ace_count); + offace = offdacl + sizeof(ACL); + } else { + acecnt = 0; + offace = 0; + } + for (nace = 0; nace < acecnt; nace++) { + pace = (const ACCESS_ALLOWED_ACE*)&securattr[offace]; + if (!(pace->flags & INHERIT_ONLY_ACE)) { + if (ntfs_same_sid(usid, &pace->sid) + || ntfs_same_sid(ownersid, &pace->sid)) { + noown = FALSE; + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) + allowown |= pace->mask; + else if (pace->type == ACCESS_DENIED_ACE_TYPE) + denyown |= pace->mask; + } else + if (ntfs_same_sid(gsid, &pace->sid) + && !(pace->mask & WRITE_OWNER)) { + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) + allowgrp |= pace->mask; + else if (pace->type == ACCESS_DENIED_ACE_TYPE) + denygrp |= pace->mask; + } else + if (is_world_sid((const SID*)&pace->sid)) { + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) + allowall |= pace->mask; + else + if (pace->type == ACCESS_DENIED_ACE_TYPE) + denyall |= pace->mask; + } else + if ((ntfs_same_sid((const SID*)&pace->sid,nullsid)) + && (pace->type == ACCESS_ALLOWED_ACE_TYPE)) + special |= pace->mask; + } + offace += le16_to_cpu(pace->size); + } + /* + * No indication about owner's rights : grant basic rights + * This happens for files created by Windows in directories + * created by Linux and owned by root, because Windows + * merges the admin ACEs + */ + if (noown) + allowown = (FILE_READ_DATA | FILE_WRITE_DATA | FILE_EXECUTE); + /* + * Add to owner rights granted to group or world + * unless denied personaly, and add to group rights + * granted to world unless denied specifically + */ + allowown |= (allowgrp | allowall); + allowgrp |= allowall; + return (merge_permissions(isdir, + allowown & ~(denyown | denyall), + allowgrp & ~(denygrp | denyall), + allowall & ~denyall, + special)); +} + +/* + * Interpret an ACL and extract meaningful grants + * (special case : owner and group are the same, + * and not administrator) + */ + +static int build_owngrp_permissions(const char *securattr, + const SID *usid, BOOL isdir) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const ACL *pacl; + const ACCESS_ALLOWED_ACE *pace; + int offdacl; + int offace; + int acecnt; + int nace; + le32 special; + BOOL grppresent; + BOOL ownpresent; + le32 allowown, allowgrp, allowall; + le32 denyown, denygrp, denyall; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)securattr; + offdacl = le32_to_cpu(phead->dacl); + pacl = (const ACL*)&securattr[offdacl]; + special = const_cpu_to_le32(0); + allowown = allowgrp = allowall = const_cpu_to_le32(0); + denyown = denygrp = denyall = const_cpu_to_le32(0); + ownpresent = FALSE; + grppresent = FALSE; + if (offdacl) { + acecnt = le16_to_cpu(pacl->ace_count); + offace = offdacl + sizeof(ACL); + } else { + acecnt = 0; + offace = 0; + } + for (nace = 0; nace < acecnt; nace++) { + pace = (const ACCESS_ALLOWED_ACE*)&securattr[offace]; + if (!(pace->flags & INHERIT_ONLY_ACE)) { + if ((ntfs_same_sid(usid, &pace->sid) + || ntfs_same_sid(ownersid, &pace->sid)) + && (pace->mask & WRITE_OWNER)) { + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) { + allowown |= pace->mask; + ownpresent = TRUE; + } + } else + if (ntfs_same_sid(usid, &pace->sid) + && (!(pace->mask & WRITE_OWNER))) { + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) { + allowgrp |= pace->mask; + grppresent = TRUE; + } + } else + if (is_world_sid((const SID*)&pace->sid)) { + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) + allowall |= pace->mask; + else + if (pace->type == ACCESS_DENIED_ACE_TYPE) + denyall |= pace->mask; + } else + if ((ntfs_same_sid((const SID*)&pace->sid,nullsid)) + && (pace->type == ACCESS_ALLOWED_ACE_TYPE)) + special |= pace->mask; + } + offace += le16_to_cpu(pace->size); + } + if (!ownpresent) + allowown = allowall; + if (!grppresent) + allowgrp = allowall; + return (merge_permissions(isdir, + allowown & ~(denyown | denyall), + allowgrp & ~(denygrp | denyall), + allowall & ~denyall, + special)); +} + +#if POSIXACLS + +/* + * Normalize a Posix ACL either from a sorted raw set of + * access ACEs or default ACEs + * (special case : owner or/and group is administrator) + */ + +static int norm_ownadmin_permissions_posix(struct POSIX_SECURITY *posix_desc, + int start, int count, int target) +{ + int j,k; + s32 id; + u16 tag; + u16 tagsset; + struct POSIX_ACE *pxace; + mode_t denywrld; + mode_t allow; + mode_t deny; + mode_t perms; + mode_t mode; + + mode = 0; + pxace = posix_desc->acl.ace; + tagsset = 0; + denywrld = 0; + /* + * Get denials to world which are meant to prevent + * execution flags to be inherited by plain files + */ + for (j=start; j<(start + count); j++) { + if (pxace[j].perms & POSIX_PERM_DENIAL) { + /* deny world exec not for default */ + if ((pxace[j].tag == POSIX_ACL_OTHER) + && !start) + denywrld = pxace[j].perms; + } + } + /* + * Collect groups of ACEs related to the same id + * and determine what is granted (denials are ignored) + * It is important the ACEs have been sorted + */ + j = start; + k = target; + deny = 0; + while (j < (start + count)) { + allow = 0; + tag = pxace[j].tag; + id = pxace[j].id; + if (tag == POSIX_ACL_MASK) { + deny = pxace[j].perms; + j++; + while ((j < (start + count)) + && (pxace[j].tag == POSIX_ACL_MASK)) + j++; + } else { + if (!(pxace[j].perms & POSIX_PERM_DENIAL)) + allow = pxace[j].perms; + j++; + while ((j < (start + count)) + && (pxace[j].tag == tag) + && (pxace[j].id == id)) { + if (!(pxace[j].perms & POSIX_PERM_DENIAL)) + allow |= pxace[j].perms; + j++; + } + } + + /* + * Store the grants into a Posix ACE + */ + if (tag == POSIX_ACL_MASK) + perms = ~deny; + else + perms = allow & ~denywrld; + if (tag != POSIX_ACL_SPECIAL) { + pxace[k].tag = tag; + pxace[k].id = id; + pxace[k].perms = perms + & (POSIX_PERM_R | POSIX_PERM_W | POSIX_PERM_X); + tagsset |= tag; + k++; + } + switch (tag) { + case POSIX_ACL_USER_OBJ : + mode |= ((perms & 7) << 6); + break; + case POSIX_ACL_GROUP_OBJ : + case POSIX_ACL_MASK : + mode = (mode & 07707) | ((perms & 7) << 3); + break; + case POSIX_ACL_OTHER : + mode |= perms & 7; + break; + case POSIX_ACL_SPECIAL : + mode |= perms & (S_ISVTX | S_ISUID | S_ISGID); + break; + default : + break; + } + } + if (!start) { /* not satisfactory */ + posix_desc->mode = mode; + posix_desc->tagsset = tagsset; + } + return (k - target); +} + +#endif /* POSIXACLS */ + +/* + * Interpret an ACL and extract meaningful grants + * (special case : owner or/and group is administrator) + */ + + +static int build_ownadmin_permissions(const char *securattr, + const SID *usid, const SID *gsid, BOOL isdir) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const ACL *pacl; + const ACCESS_ALLOWED_ACE *pace; + int offdacl; + int offace; + int acecnt; + int nace; + BOOL firstapply; + int isforeign; + le32 special; + le32 allowown, allowgrp, allowall; + le32 denyown, denygrp, denyall; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)securattr; + offdacl = le32_to_cpu(phead->dacl); + pacl = (const ACL*)&securattr[offdacl]; + special = const_cpu_to_le32(0); + allowown = allowgrp = allowall = const_cpu_to_le32(0); + denyown = denygrp = denyall = const_cpu_to_le32(0); + if (offdacl) { + acecnt = le16_to_cpu(pacl->ace_count); + offace = offdacl + sizeof(ACL); + } else { + acecnt = 0; + offace = 0; + } + firstapply = TRUE; + isforeign = 3; + for (nace = 0; nace < acecnt; nace++) { + pace = (const ACCESS_ALLOWED_ACE*)&securattr[offace]; + if (!(pace->flags & INHERIT_ONLY_ACE) + && !(~pace->mask & (ROOT_OWNER_UNMARK | ROOT_GROUP_UNMARK))) { + if ((ntfs_same_sid(usid, &pace->sid) + || ntfs_same_sid(ownersid, &pace->sid)) + && (((pace->mask & WRITE_OWNER) && firstapply))) { + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) { + allowown |= pace->mask; + isforeign &= ~1; + } else + if (pace->type == ACCESS_DENIED_ACE_TYPE) + denyown |= pace->mask; + } else + if (ntfs_same_sid(gsid, &pace->sid) + && (!(pace->mask & WRITE_OWNER))) { + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) { + allowgrp |= pace->mask; + isforeign &= ~2; + } else + if (pace->type == ACCESS_DENIED_ACE_TYPE) + denygrp |= pace->mask; + } else if (is_world_sid((const SID*)&pace->sid)) { + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) + allowall |= pace->mask; + else + if (pace->type == ACCESS_DENIED_ACE_TYPE) + denyall |= pace->mask; + } + firstapply = FALSE; + } else + if (!(pace->flags & INHERIT_ONLY_ACE)) + if ((ntfs_same_sid((const SID*)&pace->sid,nullsid)) + && (pace->type == ACCESS_ALLOWED_ACE_TYPE)) + special |= pace->mask; + offace += le16_to_cpu(pace->size); + } + if (isforeign) { + allowown |= (allowgrp | allowall); + allowgrp |= allowall; + } + return (merge_permissions(isdir, + allowown & ~(denyown | denyall), + allowgrp & ~(denygrp | denyall), + allowall & ~denyall, + special)); +} + +#if OWNERFROMACL + +/* + * Define the owner of a file as the first user allowed + * to change the owner, instead of the user defined as owner. + * + * This produces better approximations for files written by a + * Windows user in an inheritable directory owned by another user, + * as the access rights are inheritable but the ownership is not. + * + * An important case is the directories "Documents and Settings/user" + * which the users must have access to, though Windows considers them + * as owned by administrator. + */ + +const SID *ntfs_acl_owner(const char *securattr) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const SID *usid; + const ACL *pacl; + const ACCESS_ALLOWED_ACE *pace; + int offdacl; + int offace; + int acecnt; + int nace; + BOOL found; + + found = FALSE; + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)securattr; + offdacl = le32_to_cpu(phead->dacl); + if (offdacl) { + pacl = (const ACL*)&securattr[offdacl]; + acecnt = le16_to_cpu(pacl->ace_count); + offace = offdacl + sizeof(ACL); + nace = 0; + do { + pace = (const ACCESS_ALLOWED_ACE*)&securattr[offace]; + if ((pace->mask & WRITE_OWNER) + && (pace->type == ACCESS_ALLOWED_ACE_TYPE) + && ntfs_is_user_sid(&pace->sid)) + found = TRUE; + offace += le16_to_cpu(pace->size); + } while (!found && (++nace < acecnt)); + } + if (found) + usid = &pace->sid; + else + usid = (const SID*)&securattr[le32_to_cpu(phead->owner)]; + return (usid); +} + +#else + +/* + * Special case for files owned by administrator with full + * access granted to a mapped user : consider this user as the tenant + * of the file. + * + * This situation cannot be represented with Linux concepts and can + * only be found for files or directories created by Windows. + * Typical situation : directory "Documents and Settings/user" which + * is on the path to user's files and must be given access to user + * only. + * + * Check file is owned by administrator and no user has rights before + * calling. + * Returns the uid of tenant or zero if none + */ + + +static uid_t find_tenant(struct MAPPING *const mapping[], + const char *securattr) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const ACL *pacl; + const ACCESS_ALLOWED_ACE *pace; + int offdacl; + int offace; + int acecnt; + int nace; + uid_t tid; + uid_t xid; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)securattr; + offdacl = le32_to_cpu(phead->dacl); + pacl = (const ACL*)&securattr[offdacl]; + tid = 0; + if (offdacl) { + acecnt = le16_to_cpu(pacl->ace_count); + offace = offdacl + sizeof(ACL); + } else + acecnt = 0; + for (nace = 0; nace < acecnt; nace++) { + pace = (const ACCESS_ALLOWED_ACE*)&securattr[offace]; + if ((pace->type == ACCESS_ALLOWED_ACE_TYPE) + && (pace->mask & DIR_WRITE)) { + xid = ntfs_find_user(mapping[MAPUSERS], &pace->sid); + if (xid) tid = xid; + } + offace += le16_to_cpu(pace->size); + } + return (tid); +} + +#endif /* OWNERFROMACL */ + +#if POSIXACLS + +/* + * Build Posix permissions from an ACL + * returns a pointer to the requested permissions + * or a null pointer (with errno set) if there is a problem + * + * If the NTFS ACL was created according to our rules, the retrieved + * Posix ACL should be the exact ACL which was set. However if + * the NTFS ACL was built by a different tool, the result could + * be a a poor approximation of what was expected + */ + +struct POSIX_SECURITY *ntfs_build_permissions_posix( + struct MAPPING *const mapping[], + const char *securattr, + const SID *usid, const SID *gsid, BOOL isdir) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + struct POSIX_SECURITY *pxdesc; + const ACL *pacl; + const ACCESS_ALLOWED_ACE *pace; + struct POSIX_ACE *pxace; + struct { + uid_t prevuid; + gid_t prevgid; + int groupmasks; + s16 tagsset; + BOOL gotowner; + BOOL gotownermask; + BOOL gotgroup; + mode_t permswrld; + } ctx[2], *pctx; + int offdacl; + int offace; + int alloccnt; + int acecnt; + uid_t uid; + gid_t gid; + int i,j; + int k,l; + BOOL ignore; + BOOL adminowns; + BOOL groupowns; + BOOL firstinh; + BOOL genericinh; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)securattr; + offdacl = le32_to_cpu(phead->dacl); + if (offdacl) { + pacl = (const ACL*)&securattr[offdacl]; + acecnt = le16_to_cpu(pacl->ace_count); + offace = offdacl + sizeof(ACL); + } else { + acecnt = 0; + offace = 0; + } + adminowns = FALSE; + groupowns = ntfs_same_sid(gsid,usid); + firstinh = FALSE; + genericinh = FALSE; + /* + * Build a raw posix security descriptor + * by just translating permissions and ids + * + * The worst case number of ACE entries consists of: + * - 'acecount' ACE entries from the main loop (see below) + * iterating over the 'securattr' array. + * - 1 ACE entry which may be added when creating world + * permissions if none exist. + * - 1 ACE entry which may be added when setting basic owner + * permissions if none exist (both lists). + * - 1 ACE entry which may be added when duplicating world + * permissions as group_obj permissions if none exist. + * - 'acecount + 2' ACE entries which may be added when + * duplicating world permissions as group permissions if they + * were converted to masks and the masks are not followed by a + * group entry. + * - 1 ACE entry which may be added when inserting a default + * mask if none is present and there are designated users or + * groups. + * + * This amounts to 2*acecnt + 6 ACE entries in the worst case. + */ + alloccnt = 2*acecnt + 6; + pxdesc = (struct POSIX_SECURITY*)malloc( + sizeof(struct POSIX_SECURITY) + + alloccnt*sizeof(struct POSIX_ACE)); + if (!pxdesc) { + ntfs_log_perror("Unable to allocate %zu bytes for " + "POSIX_SECURITY structure", + (size_t)(sizeof(struct POSIX_SECURITY) + + alloccnt*sizeof(struct POSIX_ACE))); + errno = ENOMEM; + return NULL; + } + k = 0; + l = alloccnt; + for (i=0; i<2; i++) { + pctx = &ctx[i]; + pctx->permswrld = 0; + pctx->prevuid = -1; + pctx->prevgid = -1; + pctx->groupmasks = 0; + pctx->tagsset = 0; + pctx->gotowner = FALSE; + pctx->gotgroup = FALSE; + pctx->gotownermask = FALSE; + } + for (j=0; jflags & INHERIT_ONLY_ACE) { + pxace = &pxdesc->acl.ace[l - 1]; + pctx = &ctx[1]; + } else { + pxace = &pxdesc->acl.ace[k]; + pctx = &ctx[0]; + } + ignore = FALSE; + /* + * grants for root as a designated user or group + */ + if ((~pace->mask & (ROOT_OWNER_UNMARK | ROOT_GROUP_UNMARK)) + && (pace->type == ACCESS_ALLOWED_ACE_TYPE) + && ntfs_same_sid(&pace->sid, adminsid)) { + pxace->tag = (pace->mask & ROOT_OWNER_UNMARK ? POSIX_ACL_GROUP : POSIX_ACL_USER); + pxace->id = 0; + if ((pace->mask & (GENERIC_ALL | WRITE_OWNER)) + && (pace->flags & INHERIT_ONLY_ACE)) + ignore = genericinh = TRUE; + } else + if (ntfs_same_sid(usid, &pace->sid)) { + pxace->id = -1; + /* + * Owner has no write-owner right : + * a group was defined same as owner + * or admin was owner or group : + * denials are meant to owner + * and grants are meant to group + */ + if (!(pace->mask & (WRITE_OWNER | GENERIC_ALL)) + && (pace->type == ACCESS_ALLOWED_ACE_TYPE)) { + if (ntfs_same_sid(gsid,usid)) { + pxace->tag = POSIX_ACL_GROUP_OBJ; + pxace->id = -1; + } else { + if (ntfs_same_sid(&pace->sid,usid)) + groupowns = TRUE; + gid = ntfs_find_group(mapping[MAPGROUPS],&pace->sid); + if (gid) { + pxace->tag = POSIX_ACL_GROUP; + pxace->id = gid; + pctx->prevgid = gid; + } else { + uid = ntfs_find_user(mapping[MAPUSERS],&pace->sid); + if (uid) { + pxace->tag = POSIX_ACL_USER; + pxace->id = uid; + } else + ignore = TRUE; + } + } + } else { + /* + * when group owns, late denials for owner + * mean group mask + */ + if ((pace->type == ACCESS_DENIED_ACE_TYPE) + && (pace->mask & WRITE_OWNER)) { + pxace->tag = POSIX_ACL_MASK; + pctx->gotownermask = TRUE; + if (pctx->gotowner) + pctx->groupmasks++; + } else { + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) + pctx->gotowner = TRUE; + if (pctx->gotownermask && !pctx->gotowner) { + uid = ntfs_find_user(mapping[MAPUSERS],&pace->sid); + pxace->id = uid; + pxace->tag = POSIX_ACL_USER; + } else + pxace->tag = POSIX_ACL_USER_OBJ; + /* system ignored, and admin */ + /* ignored at first position */ + if (pace->flags & INHERIT_ONLY_ACE) { + if ((firstinh && ntfs_same_sid(&pace->sid,adminsid)) + || ntfs_same_sid(&pace->sid,systemsid)) + ignore = TRUE; + if (!firstinh) { + firstinh = TRUE; + } + } else { + if ((adminowns && ntfs_same_sid(&pace->sid,adminsid)) + || ntfs_same_sid(&pace->sid,systemsid)) + ignore = TRUE; + if (ntfs_same_sid(usid,adminsid)) + adminowns = TRUE; + } + } + } + } else if (ntfs_same_sid(gsid, &pace->sid)) { + if ((pace->type == ACCESS_DENIED_ACE_TYPE) + && (pace->mask & WRITE_OWNER)) { + pxace->tag = POSIX_ACL_MASK; + pxace->id = -1; + if (pctx->gotowner) + pctx->groupmasks++; + } else { + if (pctx->gotgroup || (pctx->groupmasks > 1)) { + gid = ntfs_find_group(mapping[MAPGROUPS],&pace->sid); + if (gid) { + pxace->id = gid; + pxace->tag = POSIX_ACL_GROUP; + pctx->prevgid = gid; + } else + ignore = TRUE; + } else { + pxace->id = -1; + pxace->tag = POSIX_ACL_GROUP_OBJ; + if (pace->type == ACCESS_ALLOWED_ACE_TYPE) + pctx->gotgroup = TRUE; + } + + if (ntfs_same_sid(gsid,adminsid) + || ntfs_same_sid(gsid,systemsid)) { + if (pace->mask & (WRITE_OWNER | GENERIC_ALL)) + ignore = TRUE; + if (ntfs_same_sid(gsid,adminsid)) + adminowns = TRUE; + else + genericinh = ignore; + } + } + } else if (is_world_sid((const SID*)&pace->sid)) { + pxace->id = -1; + pxace->tag = POSIX_ACL_OTHER; + if ((pace->type == ACCESS_DENIED_ACE_TYPE) + && (pace->flags & INHERIT_ONLY_ACE)) + ignore = TRUE; + } else if (ntfs_same_sid((const SID*)&pace->sid,nullsid)) { + pxace->id = -1; + pxace->tag = POSIX_ACL_SPECIAL; + } else { + uid = ntfs_find_user(mapping[MAPUSERS],&pace->sid); + if (uid) { + if ((pace->type == ACCESS_DENIED_ACE_TYPE) + && (pace->mask & WRITE_OWNER) + && (pctx->prevuid != uid)) { + pxace->id = -1; + pxace->tag = POSIX_ACL_MASK; + } else { + pxace->id = uid; + pxace->tag = POSIX_ACL_USER; + } + pctx->prevuid = uid; + } else { + gid = ntfs_find_group(mapping[MAPGROUPS],&pace->sid); + if (gid) { + if ((pace->type == ACCESS_DENIED_ACE_TYPE) + && (pace->mask & WRITE_OWNER) + && (pctx->prevgid != gid)) { + pxace->tag = POSIX_ACL_MASK; + pctx->groupmasks++; + } else { + pxace->tag = POSIX_ACL_GROUP; + } + pxace->id = gid; + pctx->prevgid = gid; + } else { + /* + * do not grant rights to unknown + * people and do not define root as a + * designated user or group + */ + ignore = TRUE; + } + } + } + if (((pace->type == ACCESS_ALLOWED_ACE_TYPE) + || (pace->type == ACCESS_DENIED_ACE_TYPE)) + && !ignore) { + pxace->perms = 0; + /* specific decoding for vtx/uid/gid */ + if (pxace->tag == POSIX_ACL_SPECIAL) { + if (pace->mask & FILE_APPEND_DATA) + pxace->perms |= S_ISUID; + if (pace->mask & FILE_WRITE_DATA) + pxace->perms |= S_ISGID; + if (pace->mask & FILE_READ_DATA) + pxace->perms |= S_ISVTX; + } else + if (isdir) { + if (pace->mask & DIR_GEXEC) + pxace->perms |= POSIX_PERM_X; + if (pace->mask & DIR_GWRITE) + pxace->perms |= POSIX_PERM_W; + if (pace->mask & DIR_GREAD) + pxace->perms |= POSIX_PERM_R; + if ((pace->mask & GENERIC_ALL) + && (pace->flags & INHERIT_ONLY_ACE)) + pxace->perms |= POSIX_PERM_X + | POSIX_PERM_W + | POSIX_PERM_R; + } else { + if (pace->mask & FILE_GEXEC) + pxace->perms |= POSIX_PERM_X; + if (pace->mask & FILE_GWRITE) + pxace->perms |= POSIX_PERM_W; + if (pace->mask & FILE_GREAD) + pxace->perms |= POSIX_PERM_R; + } + + if (pace->type != ACCESS_ALLOWED_ACE_TYPE) + pxace->perms |= POSIX_PERM_DENIAL; + else + if (pxace->tag == POSIX_ACL_OTHER) + pctx->permswrld |= pxace->perms; + pctx->tagsset |= pxace->tag; + if (pace->flags & INHERIT_ONLY_ACE) { + l--; + } else { + k++; + } + } + offace += le16_to_cpu(pace->size); + } + /* + * Create world perms if none (both lists) + */ + for (i=0; i<2; i++) + if ((genericinh || !i) + && !(ctx[i].tagsset & POSIX_ACL_OTHER)) { + if (i) + pxace = &pxdesc->acl.ace[--l]; + else + pxace = &pxdesc->acl.ace[k++]; + pxace->tag = POSIX_ACL_OTHER; + pxace->id = -1; + pxace->perms = 0; + ctx[i].tagsset |= POSIX_ACL_OTHER; + ctx[i].permswrld = 0; + } + /* + * Set basic owner perms if none (both lists) + * This happens for files created by Windows in directories + * created by Linux and owned by root, because Windows + * merges the admin ACEs + */ + for (i=0; i<2; i++) + if (!(ctx[i].tagsset & POSIX_ACL_USER_OBJ) + && (ctx[i].tagsset & POSIX_ACL_OTHER)) { + if (i) + pxace = &pxdesc->acl.ace[--l]; + else + pxace = &pxdesc->acl.ace[k++]; + pxace->tag = POSIX_ACL_USER_OBJ; + pxace->id = -1; + pxace->perms = POSIX_PERM_R | POSIX_PERM_W | POSIX_PERM_X; + ctx[i].tagsset |= POSIX_ACL_USER_OBJ; + } + /* + * Duplicate world perms as group_obj perms if none + */ + for (i=0; i<2; i++) + if ((ctx[i].tagsset & POSIX_ACL_OTHER) + && !(ctx[i].tagsset & POSIX_ACL_GROUP_OBJ)) { + if (i) + pxace = &pxdesc->acl.ace[--l]; + else + pxace = &pxdesc->acl.ace[k++]; + pxace->tag = POSIX_ACL_GROUP_OBJ; + pxace->id = -1; + pxace->perms = ctx[i].permswrld; + ctx[i].tagsset |= POSIX_ACL_GROUP_OBJ; + } + /* + * Also duplicate world perms as group perms if they + * were converted to mask and not followed by a group entry + */ + if (ctx[0].groupmasks) { + for (j=k-2; j>=0; j--) { + if ((pxdesc->acl.ace[j].tag == POSIX_ACL_MASK) + && (pxdesc->acl.ace[j].id != -1) + && ((pxdesc->acl.ace[j+1].tag != POSIX_ACL_GROUP) + || (pxdesc->acl.ace[j+1].id + != pxdesc->acl.ace[j].id))) { + pxace = &pxdesc->acl.ace[k]; + pxace->tag = POSIX_ACL_GROUP; + pxace->id = pxdesc->acl.ace[j].id; + pxace->perms = ctx[0].permswrld; + ctx[0].tagsset |= POSIX_ACL_GROUP; + k++; + } + if (pxdesc->acl.ace[j].tag == POSIX_ACL_MASK) + pxdesc->acl.ace[j].id = -1; + } + } + if (ctx[1].groupmasks) { + for (j=l; j<(alloccnt-1); j++) { + if ((pxdesc->acl.ace[j].tag == POSIX_ACL_MASK) + && (pxdesc->acl.ace[j].id != -1) + && ((pxdesc->acl.ace[j+1].tag != POSIX_ACL_GROUP) + || (pxdesc->acl.ace[j+1].id + != pxdesc->acl.ace[j].id))) { + pxace = &pxdesc->acl.ace[l - 1]; + pxace->tag = POSIX_ACL_GROUP; + pxace->id = pxdesc->acl.ace[j].id; + pxace->perms = ctx[1].permswrld; + ctx[1].tagsset |= POSIX_ACL_GROUP; + l--; + } + if (pxdesc->acl.ace[j].tag == POSIX_ACL_MASK) + pxdesc->acl.ace[j].id = -1; + } + } + + /* + * Insert default mask if none present and + * there are designated users or groups + * (the space for it has not beed used) + */ + for (i=0; i<2; i++) + if ((ctx[i].tagsset & (POSIX_ACL_USER | POSIX_ACL_GROUP)) + && !(ctx[i].tagsset & POSIX_ACL_MASK)) { + if (i) + pxace = &pxdesc->acl.ace[--l]; + else + pxace = &pxdesc->acl.ace[k++]; + pxace->tag = POSIX_ACL_MASK; + pxace->id = -1; + pxace->perms = POSIX_PERM_DENIAL; + ctx[i].tagsset |= POSIX_ACL_MASK; + } + + if (k > l) { + ntfs_log_error("Posix descriptor is longer than expected\n"); + errno = EIO; + free(pxdesc); + pxdesc = (struct POSIX_SECURITY*)NULL; + } else { + pxdesc->acccnt = k; + pxdesc->defcnt = alloccnt - l; + pxdesc->firstdef = l; + pxdesc->tagsset = ctx[0].tagsset; + pxdesc->acl.version = POSIX_VERSION; + pxdesc->acl.flags = 0; + pxdesc->acl.filler = 0; + ntfs_sort_posix(pxdesc); + if (adminowns) { + k = norm_ownadmin_permissions_posix(pxdesc, + 0, pxdesc->acccnt, 0); + pxdesc->acccnt = k; + l = norm_ownadmin_permissions_posix(pxdesc, + pxdesc->firstdef, pxdesc->defcnt, k); + pxdesc->firstdef = k; + pxdesc->defcnt = l; + } else { + k = norm_std_permissions_posix(pxdesc,groupowns, + 0, pxdesc->acccnt, 0); + pxdesc->acccnt = k; + l = norm_std_permissions_posix(pxdesc,groupowns, + pxdesc->firstdef, pxdesc->defcnt, k); + pxdesc->firstdef = k; + pxdesc->defcnt = l; + } + } + if (pxdesc && !ntfs_valid_posix(pxdesc)) { + ntfs_log_error("Invalid Posix descriptor built\n"); + errno = EIO; + free(pxdesc); + pxdesc = (struct POSIX_SECURITY*)NULL; + } + return (pxdesc); +} + +#endif /* POSIXACLS */ + +/* + * Build unix-style (mode_t) permissions from an ACL + * returns the requested permissions + * or a negative result (with errno set) if there is a problem + */ + +int ntfs_build_permissions(const char *securattr, + const SID *usid, const SID *gsid, BOOL isdir) +{ + int perm; + BOOL adminowns; + BOOL groupowns; + + adminowns = ntfs_same_sid(usid,adminsid) + || ntfs_same_sid(gsid,adminsid); + groupowns = !adminowns && ntfs_same_sid(gsid,usid); + if (adminowns) + perm = build_ownadmin_permissions(securattr, usid, gsid, isdir); + else + if (groupowns) + perm = build_owngrp_permissions(securattr, usid, isdir); + else + perm = build_std_permissions(securattr, usid, gsid, isdir); + return (perm); +} + +/* + * The following must be in some library... + */ + +static unsigned long atoul(const char *p) +{ /* must be somewhere ! */ + unsigned long v; + + v = 0; + while ((*p >= '0') && (*p <= '9')) + v = v * 10 + (*p++) - '0'; + return (v); +} + +/* + * Build an internal representation of a SID + * Returns a copy in allocated memory if it succeeds + * The SID is checked to be a valid user one. + */ + +static SID *encodesid(const char *sidstr) +{ + SID *sid; + int cnt; + BIGSID bigsid; + SID *bsid; + u32 auth; + const char *p; + + sid = (SID*) NULL; + if (!strncmp(sidstr, "S-1-", 4)) { + bsid = (SID*)&bigsid; + bsid->revision = SID_REVISION; + p = &sidstr[4]; + auth = atoul(p); + bsid->identifier_authority.high_part = const_cpu_to_be16(0); + bsid->identifier_authority.low_part = cpu_to_be32(auth); + cnt = 0; + p = strchr(p, '-'); + while (p && (cnt < 8)) { + p++; + auth = atoul(p); + bsid->sub_authority[cnt] = cpu_to_le32(auth); + p = strchr(p, '-'); + cnt++; + } + bsid->sub_authority_count = cnt; + if ((cnt > 0) && ntfs_valid_sid(bsid) + && (ntfs_is_user_sid(bsid) || ntfs_known_group_sid(bsid))) { + sid = (SID*) ntfs_malloc(4 * cnt + 8); + if (sid) + memcpy(sid, bsid, 4 * cnt + 8); + } + } + return (sid); +} + +/* + * Get a single mapping item from buffer + * + * Always reads a full line, truncating long lines + * Refills buffer when exhausted + * Returns pointer to item, or NULL when there is no more + */ + +static struct MAPLIST *getmappingitem(FILEREADER reader, void *fileid, + off_t *poffs, char *buf, int *psrc, s64 *psize) +{ + int src; + int dst; + char *q; + char *pu; + char *pg; + int gotend; + struct MAPLIST *item; + + src = *psrc; + dst = 0; + /* allocate and get a full line */ + item = (struct MAPLIST*)ntfs_malloc(sizeof(struct MAPLIST)); + if (item) { + do { + gotend = 0; + while ((src < *psize) + && (buf[src] != '\n')) { + if (dst < LINESZ) + item->maptext[dst++] = buf[src]; + src++; + } + if (src >= *psize) { + *poffs += *psize; + *psize = reader(fileid, buf, (size_t)BUFSZ, *poffs); + src = 0; + } else { + gotend = 1; + src++; + item->maptext[dst] = '\0'; + dst = 0; + } + } while (*psize && ((item->maptext[0] == '#') || !gotend)); + if (gotend) { + pu = pg = (char*)NULL; + /* decompose into uid, gid and sid */ + item->uidstr = item->maptext; + item->gidstr = strchr(item->uidstr, ':'); + if (item->gidstr) { + pu = item->gidstr++; + item->sidstr = strchr(item->gidstr, ':'); + if (item->sidstr) { + pg = item->sidstr++; + q = strchr(item->sidstr, ':'); + if (q) *q = 0; + } + } + if (pu && pg) + *pu = *pg = '\0'; + else { + ntfs_log_early_error("Bad mapping item \"%s\"\n", + item->maptext); + free(item); + item = (struct MAPLIST*)NULL; + } + } else { + free(item); /* free unused item */ + item = (struct MAPLIST*)NULL; + } + } + *psrc = src; + return (item); +} + +/* + * Read user mapping file and split into their attribute. + * Parameters are kept as text in a chained list until logins + * are converted to uid. + * Returns the head of list, if any + * + * If an absolute path is provided, the mapping file is assumed + * to be located in another mounted file system, and plain read() + * are used to get its contents. + * If a relative path is provided, the mapping file is assumed + * to be located on the current file system, and internal IO + * have to be used since we are still mounting and we have not + * entered the fuse loop yet. + */ + +struct MAPLIST *ntfs_read_mapping(FILEREADER reader, void *fileid) +{ + char buf[BUFSZ]; + struct MAPLIST *item; + struct MAPLIST *firstitem; + struct MAPLIST *lastitem; + int src; + off_t offs; + s64 size; + + firstitem = (struct MAPLIST*)NULL; + lastitem = (struct MAPLIST*)NULL; + offs = 0; + size = reader(fileid, buf, (size_t)BUFSZ, (off_t)0); + if (size > 0) { + src = 0; + do { + item = getmappingitem(reader, fileid, &offs, + buf, &src, &size); + if (item) { + item->next = (struct MAPLIST*)NULL; + if (lastitem) + lastitem->next = item; + else + firstitem = item; + lastitem = item; + } + } while (item); + } + return (firstitem); +} + +/* + * Free memory used to store the user mapping + * The only purpose is to facilitate the detection of memory leaks + */ + +void ntfs_free_mapping(struct MAPPING *mapping[]) +{ + struct MAPPING *user; + struct MAPPING *group; + + /* free user mappings */ + while (mapping[MAPUSERS]) { + user = mapping[MAPUSERS]; + /* do not free SIDs used for group mappings */ + group = mapping[MAPGROUPS]; + while (group && (group->sid != user->sid)) + group = group->next; + if (!group) + free(user->sid); + /* free group list if any */ + if (user->grcnt) + free(user->groups); + /* unchain item and free */ + mapping[MAPUSERS] = user->next; + free(user); + } + /* free group mappings */ + while (mapping[MAPGROUPS]) { + group = mapping[MAPGROUPS]; + free(group->sid); + /* unchain item and free */ + mapping[MAPGROUPS] = group->next; + free(group); + } +} + + +/* + * Build the user mapping list + * user identification may be given in symbolic or numeric format + * + * ! Note ! : does getpwnam() read /etc/passwd or some other file ? + * if so there is a possible recursion into fuse if this + * file is on NTFS, and fuse is not recursion safe. + */ + +struct MAPPING *ntfs_do_user_mapping(struct MAPLIST *firstitem) +{ + struct MAPLIST *item; + struct MAPPING *firstmapping; + struct MAPPING *lastmapping; + struct MAPPING *mapping; + struct passwd *pwd; + SID *sid; + int uid; + + firstmapping = (struct MAPPING*)NULL; + lastmapping = (struct MAPPING*)NULL; + for (item = firstitem; item; item = item->next) { + if ((item->uidstr[0] >= '0') && (item->uidstr[0] <= '9')) + uid = atoi(item->uidstr); + else { + uid = 0; + if (item->uidstr[0]) { + pwd = getpwnam(item->uidstr); + if (pwd) + uid = pwd->pw_uid; + else + ntfs_log_early_error("Invalid user \"%s\"\n", + item->uidstr); + } + } + /* + * Records with no uid and no gid are inserted + * to define the implicit mapping pattern + */ + if (uid + || (!item->uidstr[0] && !item->gidstr[0])) { + sid = encodesid(item->sidstr); + if (sid && ntfs_known_group_sid(sid)) { + ntfs_log_error("Bad user SID %s\n", + item->sidstr); + free(sid); + sid = (SID*)NULL; + } + if (sid && !item->uidstr[0] && !item->gidstr[0] + && !ntfs_valid_pattern(sid)) { + ntfs_log_error("Bad implicit SID pattern %s\n", + item->sidstr); + sid = (SID*)NULL; + } + if (sid) { + mapping = + (struct MAPPING*) + ntfs_malloc(sizeof(struct MAPPING)); + if (mapping) { + mapping->sid = sid; + mapping->xid = uid; + mapping->grcnt = 0; + mapping->next = (struct MAPPING*)NULL; + if (lastmapping) + lastmapping->next = mapping; + else + firstmapping = mapping; + lastmapping = mapping; + } + } + } + } + return (firstmapping); +} + +/* + * Build the group mapping list + * group identification may be given in symbolic or numeric format + * + * gid not associated to a uid are processed first in order + * to favour real groups + * + * ! Note ! : does getgrnam() read /etc/group or some other file ? + * if so there is a possible recursion into fuse if this + * file is on NTFS, and fuse is not recursion safe. + */ + +struct MAPPING *ntfs_do_group_mapping(struct MAPLIST *firstitem) +{ + struct MAPLIST *item; + struct MAPPING *firstmapping; + struct MAPPING *lastmapping; + struct MAPPING *mapping; + struct group *grp; + BOOL secondstep; + BOOL ok; + int step; + SID *sid; + int gid; + + firstmapping = (struct MAPPING*)NULL; + lastmapping = (struct MAPPING*)NULL; + for (step=1; step<=2; step++) { + for (item = firstitem; item; item = item->next) { + secondstep = (item->uidstr[0] != '\0') + || !item->gidstr[0]; + ok = (step == 1 ? !secondstep : secondstep); + if ((item->gidstr[0] >= '0') + && (item->gidstr[0] <= '9')) + gid = atoi(item->gidstr); + else { + gid = 0; + if (item->gidstr[0]) { + grp = getgrnam(item->gidstr); + if (grp) + gid = grp->gr_gid; + else + ntfs_log_early_error("Invalid group \"%s\"\n", + item->gidstr); + } + } + /* + * Records with no uid and no gid are inserted in the + * second step to define the implicit mapping pattern + */ + if (ok + && (gid + || (!item->uidstr[0] && !item->gidstr[0]))) { + sid = encodesid(item->sidstr); + if (sid && !item->uidstr[0] && !item->gidstr[0] + && !ntfs_valid_pattern(sid)) { + /* error already logged */ + sid = (SID*)NULL; + } + if (sid) { + mapping = (struct MAPPING*) + ntfs_malloc(sizeof(struct MAPPING)); + if (mapping) { + mapping->sid = sid; + mapping->xid = gid; + /* special groups point to themselves */ + if (ntfs_known_group_sid(sid)) { + mapping->groups = + (gid_t*)&mapping->xid; + mapping->grcnt = 1; + } else + mapping->grcnt = 0; + + + mapping->next = (struct MAPPING*)NULL; + if (lastmapping) + lastmapping->next = mapping; + else + firstmapping = mapping; + lastmapping = mapping; + } + } + } + } + } + return (firstmapping); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/attrib.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/attrib.c new file mode 100644 index 0000000..b1ab81e --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/attrib.c @@ -0,0 +1,7264 @@ +/** + * attrib.c - Attribute handling code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2010 Anton Altaparmakov + * Copyright (c) 2002-2005 Richard Russon + * Copyright (c) 2002-2008 Szabolcs Szakacsits + * Copyright (c) 2004-2007 Yura Pakhuchiy + * Copyright (c) 2007-2021 Jean-Pierre Andre + * Copyright (c) 2010 Erik Larsson + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif + +#include "param.h" +#include "compat.h" +#include "attrib.h" +#include "attrlist.h" +#include "device.h" +#include "mft.h" +#include "debug.h" +#include "mst.h" +#include "volume.h" +#include "types.h" +#include "layout.h" +#include "inode.h" +#include "runlist.h" +#include "lcnalloc.h" +#include "dir.h" +#include "compress.h" +#include "bitmap.h" +#include "logging.h" +#include "misc.h" +#include "efs.h" + +ntfschar AT_UNNAMED[] = { const_cpu_to_le16('\0') }; +ntfschar STREAM_SDS[] = { const_cpu_to_le16('$'), + const_cpu_to_le16('S'), + const_cpu_to_le16('D'), + const_cpu_to_le16('S'), + const_cpu_to_le16('\0') }; + +ntfschar TXF_DATA[] = { const_cpu_to_le16('$'), + const_cpu_to_le16('T'), + const_cpu_to_le16('X'), + const_cpu_to_le16('F'), + const_cpu_to_le16('_'), + const_cpu_to_le16('D'), + const_cpu_to_le16('A'), + const_cpu_to_le16('T'), + const_cpu_to_le16('A'), + const_cpu_to_le16('\0') }; + +static int NAttrFlag(ntfs_attr *na, FILE_ATTR_FLAGS flag) +{ + if (na->type == AT_DATA && na->name == AT_UNNAMED) + return (na->ni->flags & flag); + return 0; +} + +static void NAttrSetFlag(ntfs_attr *na, FILE_ATTR_FLAGS flag) +{ + if (na->type == AT_DATA && na->name == AT_UNNAMED) + na->ni->flags |= flag; + else + ntfs_log_trace("Denied setting flag %d for not unnamed data " + "attribute\n", le32_to_cpu(flag)); +} + +static void NAttrClearFlag(ntfs_attr *na, FILE_ATTR_FLAGS flag) +{ + if (na->type == AT_DATA && na->name == AT_UNNAMED) + na->ni->flags &= ~flag; +} + +#define GenNAttrIno(func_name, flag) \ +int NAttr##func_name(ntfs_attr *na) { return NAttrFlag (na, flag); } \ +void NAttrSet##func_name(ntfs_attr *na) { NAttrSetFlag (na, flag); } \ +void NAttrClear##func_name(ntfs_attr *na){ NAttrClearFlag(na, flag); } + +GenNAttrIno(Compressed, FILE_ATTR_COMPRESSED) +GenNAttrIno(Encrypted, FILE_ATTR_ENCRYPTED) +GenNAttrIno(Sparse, FILE_ATTR_SPARSE_FILE) + +/** + * ntfs_get_attribute_value_length - Find the length of an attribute + * @a: + * + * Description... + * + * Returns: + */ +s64 ntfs_get_attribute_value_length(const ATTR_RECORD *a) +{ + if (!a) { + errno = EINVAL; + return 0; + } + errno = 0; + if (a->non_resident) + return sle64_to_cpu(a->data_size); + + return (s64)le32_to_cpu(a->value_length); +} + +/** + * ntfs_get_attribute_value - Get a copy of an attribute + * @vol: + * @a: + * @b: + * + * Description... + * + * Returns: + */ +s64 ntfs_get_attribute_value(const ntfs_volume *vol, + const ATTR_RECORD *a, u8 *b) +{ + runlist *rl; + s64 total, r; + int i; + + /* Sanity checks. */ + if (!vol || !a || !b) { + errno = EINVAL; + return 0; + } + /* Complex attribute? */ + /* + * Ignore the flags in case they are not zero for an attribute list + * attribute. Windows does not complain about invalid flags and chkdsk + * does not detect or fix them so we need to cope with it, too. + */ + if (a->type != AT_ATTRIBUTE_LIST && a->flags) { + ntfs_log_error("Non-zero (%04x) attribute flags. Cannot handle " + "this yet.\n", le16_to_cpu(a->flags)); + errno = EOPNOTSUPP; + return 0; + } + if (!a->non_resident) { + /* Attribute is resident. */ + + /* Sanity check. */ + if (le32_to_cpu(a->value_length) + le16_to_cpu(a->value_offset) + > le32_to_cpu(a->length)) { + return 0; + } + + memcpy(b, (const char*)a + le16_to_cpu(a->value_offset), + le32_to_cpu(a->value_length)); + errno = 0; + return (s64)le32_to_cpu(a->value_length); + } + + /* Attribute is not resident. */ + + /* If no data, return 0. */ + if (!(a->data_size)) { + errno = 0; + return 0; + } + /* + * FIXME: What about attribute lists?!? (AIA) + */ + /* Decompress the mapping pairs array into a runlist. */ + rl = ntfs_mapping_pairs_decompress(vol, a, NULL); + if (!rl) { + errno = EINVAL; + return 0; + } + /* + * FIXED: We were overflowing here in a nasty fashion when we + * reach the last cluster in the runlist as the buffer will + * only be big enough to hold data_size bytes while we are + * reading in allocated_size bytes which is usually larger + * than data_size, since the actual data is unlikely to have a + * size equal to a multiple of the cluster size! + * FIXED2: We were also overflowing here in the same fashion + * when the data_size was more than one run smaller than the + * allocated size which happens with Windows XP sometimes. + */ + /* Now load all clusters in the runlist into b. */ + for (i = 0, total = 0; rl[i].length; i++) { + if (total + (rl[i].length << vol->cluster_size_bits) >= + sle64_to_cpu(a->data_size)) { + unsigned char *intbuf = NULL; + s64 intlth; + /* + * We have reached the last run so we were going to + * overflow when executing the ntfs_pread() which is + * BAAAAAAAD! + * Temporary fix: + * Allocate a new buffer with size: + * rl[i].length << vol->cluster_size_bits, do the + * read into our buffer, then memcpy the correct + * amount of data into the caller supplied buffer, + * free our buffer, and continue. + * We have reached the end of data size so we were + * going to overflow in the same fashion. + * Temporary fix: same as above. + * + * For safety, limit the amount to read to the + * needed size, knowing that the whole attribute + * size has been checked to be <= 0x40000. + */ + intlth = (sle64_to_cpu(a->data_size) - total + + vol->cluster_size - 1) + >> vol->cluster_size_bits; + if (rl[i].length < intlth) + intlth = rl[i].length; + intbuf = (u8*)ntfs_malloc(intlth + << vol->cluster_size_bits); + if (!intbuf) { + free(rl); + return 0; + } + /* + * FIXME: If compressed file: Only read if lcn != -1. + * Otherwise, we are dealing with a sparse run and we + * just memset the user buffer to 0 for the length of + * the run, which should be 16 (= compression unit + * size). + * FIXME: Really only when file is compressed, or can + * we have sparse runs in uncompressed files as well? + * - Yes we can, in sparse files! But not necessarily + * size of 16, just run length. + */ + r = ntfs_pread(vol->dev, + rl[i].lcn << vol->cluster_size_bits, + intlth << vol->cluster_size_bits, + intbuf); + if (r != intlth << vol->cluster_size_bits) { +#define ESTR "Error reading attribute value" + if (r == -1) + ntfs_log_perror(ESTR); + else if (r < intlth << + vol->cluster_size_bits) { + ntfs_log_debug(ESTR ": Ran out of input data.\n"); + errno = EIO; + } else { + ntfs_log_debug(ESTR ": unknown error\n"); + errno = EIO; + } +#undef ESTR + free(rl); + free(intbuf); + return 0; + } + memcpy(b + total, intbuf, sle64_to_cpu(a->data_size) - + total); + free(intbuf); + total = sle64_to_cpu(a->data_size); + break; + } + /* + * FIXME: If compressed file: Only read if lcn != -1. + * Otherwise, we are dealing with a sparse run and we just + * memset the user buffer to 0 for the length of the run, which + * should be 16 (= compression unit size). + * FIXME: Really only when file is compressed, or can + * we have sparse runs in uncompressed files as well? + * - Yes we can, in sparse files! But not necessarily size of + * 16, just run length. + */ + r = ntfs_pread(vol->dev, rl[i].lcn << vol->cluster_size_bits, + rl[i].length << vol->cluster_size_bits, + b + total); + if (r != rl[i].length << vol->cluster_size_bits) { +#define ESTR "Error reading attribute value" + if (r == -1) + ntfs_log_perror(ESTR); + else if (r < rl[i].length << vol->cluster_size_bits) { + ntfs_log_debug(ESTR ": Ran out of input data.\n"); + errno = EIO; + } else { + ntfs_log_debug(ESTR ": unknown error\n"); + errno = EIO; + } +#undef ESTR + free(rl); + return 0; + } + total += r; + } + free(rl); + return total; +} + +/* Already cleaned up code below, but still look for FIXME:... */ + +/** + * __ntfs_attr_init - primary initialization of an ntfs attribute structure + * @na: ntfs attribute to initialize + * @ni: ntfs inode with which to initialize the ntfs attribute + * @type: attribute type + * @name: attribute name in little endian Unicode or NULL + * @name_len: length of attribute @name in Unicode characters (if @name given) + * + * Initialize the ntfs attribute @na with @ni, @type, @name, and @name_len. + */ +static void __ntfs_attr_init(ntfs_attr *na, ntfs_inode *ni, + const ATTR_TYPES type, ntfschar *name, const u32 name_len) +{ + na->rl = NULL; + na->ni = ni; + na->type = type; + na->name = name; + if (name) + na->name_len = name_len; + else + na->name_len = 0; +} + +/** + * ntfs_attr_init - initialize an ntfs_attr with data sizes and status + * @na: + * @non_resident: + * @compressed: + * @encrypted: + * @sparse: + * @allocated_size: + * @data_size: + * @initialized_size: + * @compressed_size: + * @compression_unit: + * + * Final initialization for an ntfs attribute. + */ +void ntfs_attr_init(ntfs_attr *na, const BOOL non_resident, + const ATTR_FLAGS data_flags, + const BOOL encrypted, const BOOL sparse, + const s64 allocated_size, const s64 data_size, + const s64 initialized_size, const s64 compressed_size, + const u8 compression_unit) +{ + if (!NAttrInitialized(na)) { + na->data_flags = data_flags; + if (non_resident) + NAttrSetNonResident(na); + if (data_flags & ATTR_COMPRESSION_MASK) + NAttrSetCompressed(na); + if (encrypted) + NAttrSetEncrypted(na); + if (sparse) + NAttrSetSparse(na); + na->allocated_size = allocated_size; + na->data_size = data_size; + na->initialized_size = initialized_size; + if ((data_flags & ATTR_COMPRESSION_MASK) || sparse) { + ntfs_volume *vol = na->ni->vol; + + na->compressed_size = compressed_size; + na->compression_block_clusters = 1 << compression_unit; + na->compression_block_size = 1 << (compression_unit + + vol->cluster_size_bits); + na->compression_block_size_bits = ffs( + na->compression_block_size) - 1; + } + NAttrSetInitialized(na); + } +} + +/** + * ntfs_attr_open - open an ntfs attribute for access + * @ni: open ntfs inode in which the ntfs attribute resides + * @type: attribute type + * @name: attribute name in little endian Unicode or AT_UNNAMED or NULL + * @name_len: length of attribute @name in Unicode characters (if @name given) + * + * Allocate a new ntfs attribute structure, initialize it with @ni, @type, + * @name, and @name_len, then return it. Return NULL on error with + * errno set to the error code. + * + * If @name is AT_UNNAMED look specifically for an unnamed attribute. If you + * do not care whether the attribute is named or not set @name to NULL. In + * both those cases @name_len is not used at all. + */ +ntfs_attr *ntfs_attr_open(ntfs_inode *ni, const ATTR_TYPES type, + ntfschar *name, u32 name_len) +{ + ntfs_attr_search_ctx *ctx; + ntfs_attr *na = NULL; + ntfschar *newname = NULL; + ATTR_RECORD *a; + le16 cs; + + ntfs_log_enter("Entering for inode %lld, attr 0x%x.\n", + (unsigned long long)ni->mft_no, le32_to_cpu(type)); + + if (!ni || !ni->vol || !ni->mrec) { + errno = EINVAL; + goto out; + } + na = ntfs_calloc(sizeof(ntfs_attr)); + if (!na) + goto out; + if (!name_len) + name = (ntfschar*)NULL; + if (name && name != AT_UNNAMED && name != NTFS_INDEX_I30) { + /* A null char leads to a short name and unallocated bytes */ + if (ntfs_ucsnlen(name, name_len) != name_len) { + ntfs_log_error("Null character in attribute name" + " of inode %lld\n",(long long)ni->mft_no); + errno = EIO; + goto err_out; + } + name = ntfs_ucsndup(name, name_len); + if (!name) + goto err_out; + newname = name; + } + + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + goto err_out; + + if (ntfs_attr_lookup(type, name, name_len, 0, 0, NULL, 0, ctx)) + goto put_err_out; + + a = ctx->attr; + + if (!name) { + if (a->name_length) { + ntfschar *attr_name; + + attr_name = (ntfschar*)((u8*)a + + le16_to_cpu(a->name_offset)); + /* A null character leads to illegal memory access */ + if (ntfs_ucsnlen(attr_name, a->name_length) + != a->name_length) { + ntfs_log_error("Null character in attribute" + " name in inode %lld\n", + (long long)ni->mft_no); + errno = EIO; + goto put_err_out; + } + name = ntfs_ucsndup(attr_name, a->name_length); + if (!name) + goto put_err_out; + newname = name; + name_len = a->name_length; + } else { + name = AT_UNNAMED; + name_len = 0; + } + } + + __ntfs_attr_init(na, ni, type, name, name_len); + + /* + * Wipe the flags in case they are not zero for an attribute list + * attribute. Windows does not complain about invalid flags and chkdsk + * does not detect or fix them so we need to cope with it, too. + */ + if (type == AT_ATTRIBUTE_LIST) + a->flags = const_cpu_to_le16(0); + + if ((type == AT_DATA) + && (a->non_resident ? !a->initialized_size : !a->value_length)) { + /* + * Define/redefine the compression state if stream is + * empty, based on the compression mark on parent + * directory (for unnamed data streams) or on current + * inode (for named data streams). The compression mark + * may change any time, the compression state can only + * change when stream is wiped out. + * + * Also prevent compression on NTFS version < 3.0 + * or cluster size > 4K or compression is disabled + */ + a->flags &= ~ATTR_COMPRESSION_MASK; + if ((ni->flags & FILE_ATTR_COMPRESSED) + && (ni->vol->major_ver >= 3) + && NVolCompression(ni->vol) + && (ni->vol->cluster_size <= MAX_COMPRESSION_CLUSTER_SIZE)) + a->flags |= ATTR_IS_COMPRESSED; + } + + cs = a->flags & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE); + + /* a file may be sparse though its unnamed data is not (cf $UsnJrnl) */ + if (na->type == AT_DATA && na->name == AT_UNNAMED && + (((a->flags & ATTR_IS_SPARSE) && !NAttrSparse(na)) || + (!(a->flags & ATTR_IS_ENCRYPTED) != !NAttrEncrypted(na)))) { + errno = EIO; + ntfs_log_perror("Inode %lld has corrupt attribute flags " + "(0x%x <> 0x%x)",(unsigned long long)ni->mft_no, + le16_to_cpu(a->flags), le32_to_cpu(na->ni->flags)); + goto put_err_out; + } + + if (a->non_resident) { + if (((a->flags & ATTR_COMPRESSION_MASK) + || a->compression_unit) + && (ni->vol->major_ver < 3)) { + errno = EIO; + ntfs_log_perror("Compressed inode %lld not allowed" + " on NTFS %d.%d", + (unsigned long long)ni->mft_no, + ni->vol->major_ver, + ni->vol->major_ver); + goto put_err_out; + } + if ((a->flags & ATTR_COMPRESSION_MASK) + && !a->compression_unit) { + errno = EIO; + ntfs_log_perror("Compressed inode %lld attr 0x%x has " + "no compression unit", + (unsigned long long)ni->mft_no, le32_to_cpu(type)); + goto put_err_out; + } + if ((a->flags & ATTR_COMPRESSION_MASK) + && (a->compression_unit + != STANDARD_COMPRESSION_UNIT)) { + errno = EIO; + ntfs_log_perror("Compressed inode %lld attr 0x%lx has " + "an unsupported compression unit %d", + (unsigned long long)ni->mft_no, + (long)le32_to_cpu(type), + (int)a->compression_unit); + goto put_err_out; + } + ntfs_attr_init(na, TRUE, a->flags, + a->flags & ATTR_IS_ENCRYPTED, + a->flags & ATTR_IS_SPARSE, + sle64_to_cpu(a->allocated_size), + sle64_to_cpu(a->data_size), + sle64_to_cpu(a->initialized_size), + cs ? sle64_to_cpu(a->compressed_size) : 0, + cs ? a->compression_unit : 0); + } else { + s64 l = le32_to_cpu(a->value_length); + ntfs_attr_init(na, FALSE, a->flags, + a->flags & ATTR_IS_ENCRYPTED, + a->flags & ATTR_IS_SPARSE, (l + 7) & ~7, l, l, + cs ? (l + 7) & ~7 : 0, 0); + } + ntfs_attr_put_search_ctx(ctx); +out: + ntfs_log_leave("\n"); + return na; + +put_err_out: + ntfs_attr_put_search_ctx(ctx); +err_out: + free(newname); + free(na); + na = NULL; + goto out; +} + +/** + * ntfs_attr_close - free an ntfs attribute structure + * @na: ntfs attribute structure to free + * + * Release all memory associated with the ntfs attribute @na and then release + * @na itself. + */ +void ntfs_attr_close(ntfs_attr *na) +{ + if (!na) + return; + if (NAttrNonResident(na) && na->rl) + free(na->rl); + /* Don't release if using an internal constant. */ + if (na->name != AT_UNNAMED && na->name != NTFS_INDEX_I30 + && na->name != STREAM_SDS) + free(na->name); + free(na); +} + +/** + * ntfs_attr_map_runlist - map (a part of) a runlist of an ntfs attribute + * @na: ntfs attribute for which to map (part of) a runlist + * @vcn: map runlist part containing this vcn + * + * Map the part of a runlist containing the @vcn of the ntfs attribute @na. + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +int ntfs_attr_map_runlist(ntfs_attr *na, VCN vcn) +{ + LCN lcn; + ntfs_attr_search_ctx *ctx; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x, vcn 0x%llx.\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type), (long long)vcn); + + lcn = ntfs_rl_vcn_to_lcn(na->rl, vcn); + if (lcn >= 0 || lcn == LCN_HOLE || lcn == LCN_ENOENT) + return 0; + + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + return -1; + + /* Find the attribute in the mft record. */ + if (!ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE, + vcn, NULL, 0, ctx)) { + runlist_element *rl; + + /* Decode the runlist. */ + rl = ntfs_mapping_pairs_decompress(na->ni->vol, ctx->attr, + na->rl); + if (rl) { + na->rl = rl; + ntfs_attr_put_search_ctx(ctx); + return 0; + } + } + + ntfs_attr_put_search_ctx(ctx); + return -1; +} + +#if PARTIAL_RUNLIST_UPDATING + +/* + * Map the runlist of an attribute from some point to the end + * + * Returns 0 if success, + * -1 if it failed (errno telling why) + */ + +static int ntfs_attr_map_partial_runlist(ntfs_attr *na, VCN vcn) +{ + VCN last_vcn; + VCN highest_vcn; + VCN needed; + runlist_element *rl; + ATTR_RECORD *a; + BOOL startseen; + ntfs_attr_search_ctx *ctx; + BOOL done; + BOOL newrunlist; + + if (NAttrFullyMapped(na)) + return 0; + + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + return -1; + + /* Get the last vcn in the attribute. */ + last_vcn = na->allocated_size >> na->ni->vol->cluster_size_bits; + + needed = vcn; + highest_vcn = 0; + startseen = FALSE; + done = FALSE; + rl = (runlist_element*)NULL; + do { + newrunlist = FALSE; + /* Find the attribute in the mft record. */ + if (!ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE, + needed, NULL, 0, ctx)) { + + a = ctx->attr; + /* Decode and merge the runlist. */ + if (ntfs_rl_vcn_to_lcn(na->rl, needed) + == LCN_RL_NOT_MAPPED) { + rl = ntfs_mapping_pairs_decompress(na->ni->vol, + a, na->rl); + newrunlist = TRUE; + } else + rl = na->rl; + if (rl) { + na->rl = rl; + highest_vcn = sle64_to_cpu(a->highest_vcn); + if (highest_vcn < needed) { + /* corruption detection on unchanged runlists */ + if (newrunlist + && ((highest_vcn + 1) < last_vcn)) { + ntfs_log_error("Corrupt attribute list\n"); + rl = (runlist_element*)NULL; + errno = EIO; + } + done = TRUE; + } + needed = highest_vcn + 1; + if (!a->lowest_vcn) + startseen = TRUE; + } + } else { + done = TRUE; + } + } while (rl && !done && (needed < last_vcn)); + ntfs_attr_put_search_ctx(ctx); + /* + * Make sure we reached the end, unless the last + * runlist was modified earlier (using HOLES_DELAY + * leads to have a visibility over attributes which + * have not yet been fully updated) + */ + if (done && newrunlist && (needed < last_vcn)) { + ntfs_log_error("End of runlist not reached\n"); + rl = (runlist_element*)NULL; + errno = EIO; + } + /* mark fully mapped if we did so */ + if (rl && startseen) + NAttrSetFullyMapped(na); + return (rl ? 0 : -1); +} + +#endif + +/** + * ntfs_attr_map_whole_runlist - map the whole runlist of an ntfs attribute + * @na: ntfs attribute for which to map the runlist + * + * Map the whole runlist of the ntfs attribute @na. For an attribute made up + * of only one attribute extent this is the same as calling + * ntfs_attr_map_runlist(na, 0) but for an attribute with multiple extents this + * will map the runlist fragments from each of the extents thus giving access + * to the entirety of the disk allocation of an attribute. + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +int ntfs_attr_map_whole_runlist(ntfs_attr *na) +{ + VCN next_vcn, last_vcn, highest_vcn; + ntfs_attr_search_ctx *ctx; + ntfs_volume *vol = na->ni->vol; + ATTR_RECORD *a; + int ret = -1; + int not_mapped; + + ntfs_log_enter("Entering for inode %llu, attr 0x%x.\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type)); + + /* avoid multiple full runlist mappings */ + if (NAttrFullyMapped(na)) { + ret = 0; + goto out; + } + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + goto out; + + /* Map all attribute extents one by one. */ + next_vcn = last_vcn = highest_vcn = 0; + a = NULL; + while (1) { + runlist_element *rl; + + not_mapped = 0; + if (ntfs_rl_vcn_to_lcn(na->rl, next_vcn) == LCN_RL_NOT_MAPPED) + not_mapped = 1; + + if (ntfs_attr_lookup(na->type, na->name, na->name_len, + CASE_SENSITIVE, next_vcn, NULL, 0, ctx)) + break; + + a = ctx->attr; + + if (not_mapped) { + /* Decode the runlist. */ + rl = ntfs_mapping_pairs_decompress(na->ni->vol, + a, na->rl); + if (!rl) + goto err_out; + na->rl = rl; + } + + /* Are we in the first extent? */ + if (!next_vcn) { + if (a->lowest_vcn) { + errno = EIO; + ntfs_log_perror("First extent of inode %llu " + "attribute has non-zero lowest_vcn", + (unsigned long long)na->ni->mft_no); + goto err_out; + } + /* Get the last vcn in the attribute. */ + last_vcn = sle64_to_cpu(a->allocated_size) >> + vol->cluster_size_bits; + } + + /* Get the lowest vcn for the next extent. */ + highest_vcn = sle64_to_cpu(a->highest_vcn); + next_vcn = highest_vcn + 1; + + /* Only one extent or error, which we catch below. */ + if (next_vcn <= 0) { + errno = ENOENT; + break; + } + + /* Avoid endless loops due to corruption. */ + if (next_vcn < sle64_to_cpu(a->lowest_vcn)) { + errno = EIO; + ntfs_log_perror("Inode %llu has corrupt attribute list", + (unsigned long long)na->ni->mft_no); + goto err_out; + } + } + if (!a) { + ntfs_log_perror("Couldn't find attribute for runlist mapping"); + goto err_out; + } + /* + * Cannot check highest_vcn when the last runlist has + * been modified earlier, as runlists and sizes may be + * updated without highest_vcn being in sync, when + * HOLES_DELAY is used + */ + if (not_mapped && highest_vcn && highest_vcn != last_vcn - 1) { + errno = EIO; + ntfs_log_perror("Failed to load full runlist: inode: %llu " + "highest_vcn: 0x%llx last_vcn: 0x%llx", + (unsigned long long)na->ni->mft_no, + (long long)highest_vcn, (long long)last_vcn); + goto err_out; + } + if (errno == ENOENT) { + NAttrSetFullyMapped(na); + ret = 0; + } +err_out: + ntfs_attr_put_search_ctx(ctx); +out: + ntfs_log_leave("\n"); + return ret; +} + +/** + * ntfs_attr_vcn_to_lcn - convert a vcn into a lcn given an ntfs attribute + * @na: ntfs attribute whose runlist to use for conversion + * @vcn: vcn to convert + * + * Convert the virtual cluster number @vcn of an attribute into a logical + * cluster number (lcn) of a device using the runlist @na->rl to map vcns to + * their corresponding lcns. + * + * If the @vcn is not mapped yet, attempt to map the attribute extent + * containing the @vcn and retry the vcn to lcn conversion. + * + * Since lcns must be >= 0, we use negative return values with special meaning: + * + * Return value Meaning / Description + * ========================================== + * -1 = LCN_HOLE Hole / not allocated on disk. + * -3 = LCN_ENOENT There is no such vcn in the attribute. + * -4 = LCN_EINVAL Input parameter error. + * -5 = LCN_EIO Corrupt fs, disk i/o error, or not enough memory. + */ +LCN ntfs_attr_vcn_to_lcn(ntfs_attr *na, const VCN vcn) +{ + LCN lcn; + BOOL is_retry = FALSE; + + if (!na || !NAttrNonResident(na) || vcn < 0) + return (LCN)LCN_EINVAL; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x.\n", (unsigned long + long)na->ni->mft_no, le32_to_cpu(na->type)); +retry: + /* Convert vcn to lcn. If that fails map the runlist and retry once. */ + lcn = ntfs_rl_vcn_to_lcn(na->rl, vcn); + if (lcn >= 0) + return lcn; + if (!is_retry && !ntfs_attr_map_runlist(na, vcn)) { + is_retry = TRUE; + goto retry; + } + /* + * If the attempt to map the runlist failed, or we are getting + * LCN_RL_NOT_MAPPED despite having mapped the attribute extent + * successfully, something is really badly wrong... + */ + if (!is_retry || lcn == (LCN)LCN_RL_NOT_MAPPED) + return (LCN)LCN_EIO; + /* lcn contains the appropriate error code. */ + return lcn; +} + +/** + * ntfs_attr_find_vcn - find a vcn in the runlist of an ntfs attribute + * @na: ntfs attribute whose runlist to search + * @vcn: vcn to find + * + * Find the virtual cluster number @vcn in the runlist of the ntfs attribute + * @na and return the the address of the runlist element containing the @vcn. + * + * Note you need to distinguish between the lcn of the returned runlist + * element being >= 0 and LCN_HOLE. In the later case you have to return zeroes + * on read and allocate clusters on write. You need to update the runlist, the + * attribute itself as well as write the modified mft record to disk. + * + * If there is an error return NULL with errno set to the error code. The + * following error codes are defined: + * EINVAL Input parameter error. + * ENOENT There is no such vcn in the runlist. + * ENOMEM Not enough memory. + * EIO I/O error or corrupt metadata. + */ +runlist_element *ntfs_attr_find_vcn(ntfs_attr *na, const VCN vcn) +{ + runlist_element *rl; + BOOL is_retry = FALSE; + + if (!na || !NAttrNonResident(na) || vcn < 0) { + errno = EINVAL; + return NULL; + } + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x, vcn %llx\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type), + (long long)vcn); +retry: + rl = na->rl; + if (!rl) + goto map_rl; + if (vcn < rl[0].vcn) + goto map_rl; + while (rl->length) { + if (vcn < rl[1].vcn) { + if (rl->lcn >= (LCN)LCN_HOLE) + return rl; + break; + } + rl++; + } + switch (rl->lcn) { + case (LCN)LCN_RL_NOT_MAPPED: + goto map_rl; + case (LCN)LCN_ENOENT: + errno = ENOENT; + break; + case (LCN)LCN_EINVAL: + errno = EINVAL; + break; + default: + errno = EIO; + break; + } + return NULL; +map_rl: + /* The @vcn is in an unmapped region, map the runlist and retry. */ + if (!is_retry && !ntfs_attr_map_runlist(na, vcn)) { + is_retry = TRUE; + goto retry; + } + /* + * If we already retried or the mapping attempt failed something has + * gone badly wrong. EINVAL and ENOENT coming from a failed mapping + * attempt are equivalent to errors for us as they should not happen + * in our code paths. + */ + if (is_retry || errno == EINVAL || errno == ENOENT) + errno = EIO; + return NULL; +} + +/** + * ntfs_attr_pread_i - see description at ntfs_attr_pread() + */ +static s64 ntfs_attr_pread_i(ntfs_attr *na, const s64 pos, s64 count, void *b) +{ + s64 br, to_read, ofs, total, total2, max_read, max_init; + ntfs_volume *vol; + runlist_element *rl; + u16 efs_padding_length; + + /* Sanity checking arguments is done in ntfs_attr_pread(). */ + + if ((na->data_flags & ATTR_COMPRESSION_MASK) && NAttrNonResident(na)) { + if ((na->data_flags & ATTR_COMPRESSION_MASK) + == ATTR_IS_COMPRESSED) + return ntfs_compressed_attr_pread(na, pos, count, b); + else { + /* compression mode not supported */ + errno = EOPNOTSUPP; + return -1; + } + } + /* + * Encrypted non-resident attributes are not supported. We return + * access denied, which is what Windows NT4 does, too. + * However, allow if mounted with efs_raw option + */ + vol = na->ni->vol; + if (!vol->efs_raw && NAttrEncrypted(na) && NAttrNonResident(na)) { + errno = EACCES; + return -1; + } + + if (!count) + return 0; + /* + * Truncate reads beyond end of attribute, + * but round to next 512 byte boundary for encrypted + * attributes with efs_raw mount option + */ + max_read = na->data_size; + max_init = na->initialized_size; + if (na->ni->vol->efs_raw + && (na->data_flags & ATTR_IS_ENCRYPTED) + && NAttrNonResident(na)) { + if (na->data_size != na->initialized_size) { + ntfs_log_error("uninitialized encrypted file not supported\n"); + errno = EINVAL; + return -1; + } + max_init = max_read = ((na->data_size + 511) & ~511) + 2; + } + if (pos + count > max_read) { + if (pos >= max_read) + return 0; + count = max_read - pos; + } + /* If it is a resident attribute, get the value from the mft record. */ + if (!NAttrNonResident(na)) { + ntfs_attr_search_ctx *ctx; + char *val; + + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + return -1; + if (ntfs_attr_lookup(na->type, na->name, na->name_len, 0, + 0, NULL, 0, ctx)) { +res_err_out: + ntfs_attr_put_search_ctx(ctx); + return -1; + } + val = (char*)ctx->attr + le16_to_cpu(ctx->attr->value_offset); + if (val < (char*)ctx->attr || val + + le32_to_cpu(ctx->attr->value_length) > + (char*)ctx->mrec + vol->mft_record_size) { + errno = EIO; + ntfs_log_perror("%s: Sanity check failed", __FUNCTION__); + goto res_err_out; + } + memcpy(b, val + pos, count); + ntfs_attr_put_search_ctx(ctx); + return count; + } + total = total2 = 0; + /* Zero out reads beyond initialized size. */ + if (pos + count > max_init) { + if (pos >= max_init) { + memset(b, 0, count); + return count; + } + total2 = pos + count - max_init; + count -= total2; + memset((u8*)b + count, 0, total2); + } + /* + * for encrypted non-resident attributes with efs_raw set + * the last two bytes aren't read from disk but contain + * the number of padding bytes so original size can be + * restored + */ + if (na->ni->vol->efs_raw && + (na->data_flags & ATTR_IS_ENCRYPTED) && + ((pos + count) > max_init-2)) { + efs_padding_length = 511 - ((na->data_size - 1) & 511); + if (pos+count == max_init) { + if (count == 1) { + *((u8*)b+count-1) = (u8)(efs_padding_length >> 8); + count--; + total2++; + } else { + *(le16*)((u8*)b+count-2) = cpu_to_le16(efs_padding_length); + count -= 2; + total2 +=2; + } + } else { + *((u8*)b+count-1) = (u8)(efs_padding_length & 0xff); + count--; + total2++; + } + } + + /* Find the runlist element containing the vcn. */ + rl = ntfs_attr_find_vcn(na, pos >> vol->cluster_size_bits); + if (!rl) { + /* + * If the vcn is not present it is an out of bounds read. + * However, we already truncated the read to the data_size, + * so getting this here is an error. + */ + if (errno == ENOENT) { + errno = EIO; + ntfs_log_perror("%s: Failed to find VCN #1", __FUNCTION__); + } + return -1; + } + /* + * Gather the requested data into the linear destination buffer. Note, + * a partial final vcn is taken care of by the @count capping of read + * length. + */ + ofs = pos - (rl->vcn << vol->cluster_size_bits); + for (; count; rl++, ofs = 0) { + if (rl->lcn == LCN_RL_NOT_MAPPED) { + rl = ntfs_attr_find_vcn(na, rl->vcn); + if (!rl) { + if (errno == ENOENT) { + errno = EIO; + ntfs_log_perror("%s: Failed to find VCN #2", + __FUNCTION__); + } + goto rl_err_out; + } + /* Needed for case when runs merged. */ + ofs = pos + total - (rl->vcn << vol->cluster_size_bits); + } + if (!rl->length) { + errno = EIO; + ntfs_log_perror("%s: Zero run length", __FUNCTION__); + goto rl_err_out; + } + if (rl->lcn < (LCN)0) { + if (rl->lcn != (LCN)LCN_HOLE) { + ntfs_log_perror("%s: Bad run (%lld)", + __FUNCTION__, + (long long)rl->lcn); + goto rl_err_out; + } + /* It is a hole, just zero the matching @b range. */ + to_read = min(count, (rl->length << + vol->cluster_size_bits) - ofs); + memset(b, 0, to_read); + /* Update progress counters. */ + total += to_read; + count -= to_read; + b = (u8*)b + to_read; + continue; + } + /* It is a real lcn, read it into @dst. */ + to_read = min(count, (rl->length << vol->cluster_size_bits) - + ofs); +retry: + ntfs_log_trace("Reading %lld bytes from vcn %lld, lcn %lld, ofs" + " %lld.\n", (long long)to_read, (long long)rl->vcn, + (long long )rl->lcn, (long long)ofs); + br = ntfs_pread(vol->dev, (rl->lcn << vol->cluster_size_bits) + + ofs, to_read, b); + /* If everything ok, update progress counters and continue. */ + if (br > 0) { + total += br; + count -= br; + b = (u8*)b + br; + } + if (br == to_read) + continue; + /* If the syscall was interrupted, try again. */ + if (br == (s64)-1 && errno == EINTR) + goto retry; + if (total) + return total; + if (!br) + errno = EIO; + ntfs_log_perror("%s: ntfs_pread failed", __FUNCTION__); + return -1; + } + /* Finally, return the number of bytes read. */ + return total + total2; +rl_err_out: + if (total) + return total; + errno = EIO; + return -1; +} + +/** + * ntfs_attr_pread - read from an attribute specified by an ntfs_attr structure + * @na: ntfs attribute to read from + * @pos: byte position in the attribute to begin reading from + * @count: number of bytes to read + * @b: output data buffer + * + * This function will read @count bytes starting at offset @pos from the ntfs + * attribute @na into the data buffer @b. + * + * On success, return the number of successfully read bytes. If this number is + * lower than @count this means that the read reached end of file or that an + * error was encountered during the read so that the read is partial. 0 means + * end of file or nothing was read (also return 0 when @count is 0). + * + * On error and nothing has been read, return -1 with errno set appropriately + * to the return code of ntfs_pread(), or to EINVAL in case of invalid + * arguments. + */ +s64 ntfs_attr_pread(ntfs_attr *na, const s64 pos, s64 count, void *b) +{ + s64 ret; + + if (!na || !na->ni || !na->ni->vol || !b || pos < 0 || count < 0) { + errno = EINVAL; + ntfs_log_perror("%s: na=%p b=%p pos=%lld count=%lld", + __FUNCTION__, na, b, (long long)pos, + (long long)count); + return -1; + } + + ntfs_log_enter("Entering for inode %lld attr 0x%x pos %lld count " + "%lld\n", (unsigned long long)na->ni->mft_no, + le32_to_cpu(na->type), (long long)pos, (long long)count); + + ret = ntfs_attr_pread_i(na, pos, count, b); + + ntfs_log_leave("\n"); + return ret; +} + +static int ntfs_attr_fill_zero(ntfs_attr *na, s64 pos, s64 count) +{ + char *buf; + s64 written, size, end = pos + count; + s64 ofsi; + const runlist_element *rli; + ntfs_volume *vol; + int ret = -1; + + ntfs_log_trace("pos %lld, count %lld\n", (long long)pos, + (long long)count); + + if (!na || pos < 0 || count < 0) { + errno = EINVAL; + goto err_out; + } + + buf = ntfs_calloc(NTFS_BUF_SIZE); + if (!buf) + goto err_out; + + rli = na->rl; + ofsi = 0; + vol = na->ni->vol; + while (pos < end) { + while (rli->length && (ofsi + (rli->length << + vol->cluster_size_bits) <= pos)) { + ofsi += (rli->length << vol->cluster_size_bits); + rli++; + } + size = min(end - pos, NTFS_BUF_SIZE); + /* + * If the zeroed block is fully within a hole, + * we need not write anything, so advance as far + * as possible within the hole. + */ + if ((rli->lcn == (LCN)LCN_HOLE) + && (ofsi <= pos) + && (ofsi + (rli->length << vol->cluster_size_bits) + >= (pos + size))) { + size = min(end - pos, ofsi - pos + + (rli->length << vol->cluster_size_bits)); + pos += size; + } else { + written = ntfs_rl_pwrite(vol, rli, ofsi, pos, + size, buf); + if (written <= 0) { + ntfs_log_perror("Failed to zero space"); + goto err_free; + } + pos += written; + } + } + + ret = 0; +err_free: + free(buf); +err_out: + return ret; +} + +static int ntfs_attr_fill_hole(ntfs_attr *na, s64 count, s64 *ofs, + runlist_element **rl, VCN *update_from) +{ + s64 to_write; + s64 need; + ntfs_volume *vol = na->ni->vol; + int eo, ret = -1; + runlist *rlc; + LCN lcn_seek_from = -1; + VCN cur_vcn, from_vcn; + + if (na->ni->mft_no == FILE_Bitmap) { + /* + * Filling a hole in the main bitmap implies allocating + * clusters, which is likely to imply updating the + * bitmap in a cluster being allocated. + * Not supported now, could lead to endless recursions. + */ + ntfs_log_error("Corrupt $BitMap not fully allocated\n"); + errno = EIO; + goto err_out; + } + to_write = min(count, ((*rl)->length << vol->cluster_size_bits) - *ofs); + + cur_vcn = (*rl)->vcn; + from_vcn = (*rl)->vcn + (*ofs >> vol->cluster_size_bits); + + ntfs_log_trace("count: %lld, cur_vcn: %lld, from: %lld, to: %lld, ofs: " + "%lld\n", (long long)count, (long long)cur_vcn, + (long long)from_vcn, (long long)to_write, (long long)*ofs); + + /* Map the runlist to be able to update mapping pairs later. */ +#if PARTIAL_RUNLIST_UPDATING + if (!na->rl) { + if (ntfs_attr_map_whole_runlist(na)) + goto err_out; + } else { + /* make sure the run ahead of hole is mapped */ + if ((*rl)->lcn == LCN_HOLE) { + if (ntfs_attr_map_partial_runlist(na, + (cur_vcn ? cur_vcn - 1 : cur_vcn))) + goto err_out; + } + } +#else + if (ntfs_attr_map_whole_runlist(na)) + goto err_out; +#endif + + /* Restore @*rl, it probably get lost during runlist mapping. */ + *rl = ntfs_attr_find_vcn(na, cur_vcn); + if (!*rl) { + ntfs_log_error("Failed to find run after mapping runlist. " + "Please report to %s.\n", NTFS_DEV_LIST); + errno = EIO; + goto err_out; + } + + /* Search backwards to find the best lcn to start seek from. */ + rlc = *rl; + while (rlc->vcn) { + rlc--; + if (rlc->lcn >= 0) { + /* + * avoid fragmenting a compressed file + * Windows does not do that, and that may + * not be desirable for files which can + * be updated + */ + if (na->data_flags & ATTR_COMPRESSION_MASK) + lcn_seek_from = rlc->lcn + rlc->length; + else + lcn_seek_from = rlc->lcn + (from_vcn - rlc->vcn); + break; + } + } + if (lcn_seek_from == -1) { + /* Backwards search failed, search forwards. */ + rlc = *rl; + while (rlc->length) { + rlc++; + if (rlc->lcn >= 0) { + lcn_seek_from = rlc->lcn - (rlc->vcn - from_vcn); + if (lcn_seek_from < -1) + lcn_seek_from = -1; + break; + } + } + } + + need = ((*ofs + to_write - 1) >> vol->cluster_size_bits) + + 1 + (*rl)->vcn - from_vcn; + if ((na->data_flags & ATTR_COMPRESSION_MASK) + && (need < na->compression_block_clusters)) { + /* + * for a compressed file, be sure to allocate the full + * compression block, as we may need space to decompress + * existing compressed data. + * So allocate the space common to compression block + * and existing hole. + */ + VCN alloc_vcn; + + if ((from_vcn & -na->compression_block_clusters) <= (*rl)->vcn) + alloc_vcn = (*rl)->vcn; + else + alloc_vcn = from_vcn & -na->compression_block_clusters; + need = (alloc_vcn | (na->compression_block_clusters - 1)) + + 1 - alloc_vcn; + if (need > (*rl)->length) { + ntfs_log_error("Cannot allocate %lld clusters" + " within a hole of %lld\n", + (long long)need, + (long long)(*rl)->length); + errno = EIO; + goto err_out; + } + rlc = ntfs_cluster_alloc(vol, alloc_vcn, need, + lcn_seek_from, DATA_ZONE); + } else + rlc = ntfs_cluster_alloc(vol, from_vcn, need, + lcn_seek_from, DATA_ZONE); + if (!rlc) + goto err_out; + if (na->data_flags & (ATTR_COMPRESSION_MASK | ATTR_IS_SPARSE)) + na->compressed_size += need << vol->cluster_size_bits; + + *rl = ntfs_runlists_merge(na->rl, rlc); + NAttrSetRunlistDirty(na); + /* + * For a compressed attribute, we must be sure there are two + * available entries, so reserve them before it gets too late. + */ + if (*rl && (na->data_flags & ATTR_COMPRESSION_MASK)) { + runlist_element *oldrl = na->rl; + na->rl = *rl; + *rl = ntfs_rl_extend(na,*rl,2); + if (!*rl) na->rl = oldrl; /* restore to original if failed */ + } + if (!*rl) { + eo = errno; + ntfs_log_perror("Failed to merge runlists"); + if (ntfs_cluster_free_from_rl(vol, rlc)) { + ntfs_log_perror("Failed to free hot clusters. " + "Please run chkdsk /f"); + } + errno = eo; + goto err_out; + } + na->unused_runs = 2; + na->rl = *rl; + if ((*update_from == -1) || (from_vcn < *update_from)) + *update_from = from_vcn; + *rl = ntfs_attr_find_vcn(na, cur_vcn); + if (!*rl) { + /* + * It's definitely a BUG, if we failed to find @cur_vcn, because + * we missed it during instantiating of the hole. + */ + ntfs_log_error("Failed to find run after hole instantiation. " + "Please report to %s.\n", NTFS_DEV_LIST); + errno = EIO; + goto err_out; + } + /* If leaved part of the hole go to the next run. */ + if ((*rl)->lcn < 0) + (*rl)++; + /* Now LCN shoudn't be less than 0. */ + if ((*rl)->lcn < 0) { + ntfs_log_error("BUG! LCN is lesser than 0. " + "Please report to the %s.\n", NTFS_DEV_LIST); + errno = EIO; + goto err_out; + } + if (*ofs) { + /* Clear non-sparse region from @cur_vcn to @*ofs. */ + if (ntfs_attr_fill_zero(na, cur_vcn << vol->cluster_size_bits, + *ofs)) + goto err_out; + } + if ((*rl)->vcn < cur_vcn) { + /* + * Clusters that replaced hole are merged with + * previous run, so we need to update offset. + */ + *ofs += (cur_vcn - (*rl)->vcn) << vol->cluster_size_bits; + } + if ((*rl)->vcn > cur_vcn) { + /* + * We left part of the hole, so we need to update offset + */ + *ofs -= ((*rl)->vcn - cur_vcn) << vol->cluster_size_bits; + } + + ret = 0; +err_out: + return ret; +} + +static int stuff_hole(ntfs_attr *na, const s64 pos); + +/* + * Split an existing hole for overwriting with data + * The hole may have to be split into two or three parts, so + * that the overwritten part fits within a single compression block + * + * No cluster allocation is needed, this will be done later in + * standard hole filling, hence no need to reserve runs for + * future needs. + * + * Returns the number of clusters with existing compressed data + * in the compression block to be written to + * (or the full block, if it was a full hole) + * -1 if there were an error + */ + +static int split_compressed_hole(ntfs_attr *na, runlist_element **prl, + s64 pos, s64 count, VCN *update_from) +{ + int compressed_part; + int cluster_size_bits = na->ni->vol->cluster_size_bits; + runlist_element *rl = *prl; + + compressed_part + = na->compression_block_clusters; + /* reserve entries in runlist if we have to split */ + if (rl->length > na->compression_block_clusters) { + *prl = ntfs_rl_extend(na,*prl,2); + if (!*prl) { + compressed_part = -1; + } else { + rl = *prl; + na->unused_runs = 2; + } + } + if (*prl && (rl->length > na->compression_block_clusters)) { + /* + * Locate the update part relative to beginning of + * current run + */ + int beginwrite = (pos >> cluster_size_bits) - rl->vcn; + s32 endblock = (((pos + count - 1) >> cluster_size_bits) + | (na->compression_block_clusters - 1)) + 1 - rl->vcn; + + compressed_part = na->compression_block_clusters + - (rl->length & (na->compression_block_clusters - 1)); + if ((beginwrite + compressed_part) >= na->compression_block_clusters) + compressed_part = na->compression_block_clusters; + /* + * if the run ends beyond end of needed block + * we have to split the run + */ + if (endblock < rl[0].length) { + runlist_element *xrl; + int n; + + /* + * we have to split into three parts if the run + * does not end within the first compression block. + * This means the hole begins before the + * compression block. + */ + if (endblock > na->compression_block_clusters) { + if (na->unused_runs < 2) { +ntfs_log_error("No free run, case 1\n"); + } + na->unused_runs -= 2; + xrl = rl; + n = 0; + while (xrl->length) { + xrl++; + n++; + } + do { + xrl[2] = *xrl; + xrl--; + } while (xrl != rl); + rl[1].length = na->compression_block_clusters; + rl[2].length = rl[0].length - endblock; + rl[0].length = endblock + - na->compression_block_clusters; + rl[1].lcn = LCN_HOLE; + rl[2].lcn = LCN_HOLE; + rl[1].vcn = rl[0].vcn + rl[0].length; + rl[2].vcn = rl[1].vcn + + na->compression_block_clusters; + rl = ++(*prl); + } else { + /* + * split into two parts and use the + * first one + */ + if (!na->unused_runs) { +ntfs_log_error("No free run, case 2\n"); + } + na->unused_runs--; + xrl = rl; + n = 0; + while (xrl->length) { + xrl++; + n++; + } + do { + xrl[1] = *xrl; + xrl--; + } while (xrl != rl); + if (beginwrite < endblock) { + /* we will write into the first part of hole */ + rl[1].length = rl[0].length - endblock; + rl[0].length = endblock; + rl[1].vcn = rl[0].vcn + rl[0].length; + rl[1].lcn = LCN_HOLE; + } else { + /* we will write into the second part of hole */ +// impossible ? + rl[1].length = rl[0].length - endblock; + rl[0].length = endblock; + rl[1].vcn = rl[0].vcn + rl[0].length; + rl[1].lcn = LCN_HOLE; + rl = ++(*prl); + } + } + } else { + if (rl[1].length) { + runlist_element *xrl; + int n; + + /* + * split into two parts and use the + * last one + */ + if (!na->unused_runs) { +ntfs_log_error("No free run, case 4\n"); + } + na->unused_runs--; + xrl = rl; + n = 0; + while (xrl->length) { + xrl++; + n++; + } + do { + xrl[1] = *xrl; + xrl--; + } while (xrl != rl); + } else { + rl[2].lcn = rl[1].lcn; + rl[2].vcn = rl[1].vcn; + rl[2].length = rl[1].length; + } + rl[1].vcn -= na->compression_block_clusters; + rl[1].lcn = LCN_HOLE; + rl[1].length = na->compression_block_clusters; + rl[0].length -= na->compression_block_clusters; + if (pos >= (rl[1].vcn << cluster_size_bits)) { + rl = ++(*prl); + } + } + NAttrSetRunlistDirty(na); + if ((*update_from == -1) || ((*prl)->vcn < *update_from)) + *update_from = (*prl)->vcn; + } + return (compressed_part); +} + +/* + * Borrow space from adjacent hole for appending data + * The hole may have to be split so that the end of hole is not + * affected by cluster allocation and overwriting + * Cluster allocation is needed for the overwritten compression block + * + * Must always leave two unused entries in the runlist + * + * Returns the number of clusters with existing compressed data + * in the compression block to be written to + * -1 if there were an error + */ + +static int borrow_from_hole(ntfs_attr *na, runlist_element **prl, + s64 pos, s64 count, VCN *update_from, BOOL wasnonresident) +{ + int compressed_part = 0; + int cluster_size_bits = na->ni->vol->cluster_size_bits; + runlist_element *rl = *prl; + s32 endblock; + long long allocated; + runlist_element *zrl; + int irl; + BOOL undecided; + BOOL nothole; + + /* check whether the compression block is fully allocated */ + endblock = (((pos + count - 1) >> cluster_size_bits) | (na->compression_block_clusters - 1)) + 1 - rl->vcn; + allocated = 0; + zrl = rl; + irl = 0; + while (zrl->length && (zrl->lcn >= 0) && (allocated < endblock)) { + allocated += zrl->length; + zrl++; + irl++; + } + + undecided = (allocated < endblock) && (zrl->lcn == LCN_RL_NOT_MAPPED); + nothole = (allocated >= endblock) || (zrl->lcn != LCN_HOLE); + + if (undecided || nothole) { + runlist_element *orl = na->rl; + s64 olcn = (*prl)->lcn; +#if PARTIAL_RUNLIST_UPDATING + VCN prevblock; +#endif + /* + * Map the runlist, unless it has not been created. + * If appending data, a partial mapping from the + * end of previous block will do. + */ + irl = *prl - na->rl; +#if PARTIAL_RUNLIST_UPDATING + prevblock = pos >> cluster_size_bits; + if (prevblock) + prevblock--; + if (!NAttrBeingNonResident(na) + && (NAttrDataAppending(na) + ? ntfs_attr_map_partial_runlist(na,prevblock) + : ntfs_attr_map_whole_runlist(na))) { +#else + if (!NAttrBeingNonResident(na) + && ntfs_attr_map_whole_runlist(na)) { +#endif + rl = (runlist_element*)NULL; + } else { + /* + * Mapping the runlist may cause its relocation, + * and relocation may be at the same place with + * relocated contents. + * Have to find the current run again when this + * happens. + */ + if ((na->rl != orl) || ((*prl)->lcn != olcn)) { + zrl = &na->rl[irl]; + while (zrl->length && (zrl->lcn != olcn)) + zrl++; + *prl = zrl; + } + if (!(*prl)->length) { + ntfs_log_error("Mapped run not found," + " inode %lld lcn 0x%llx\n", + (long long)na->ni->mft_no, + (long long)olcn); + rl = (runlist_element*)NULL; + } else { + rl = ntfs_rl_extend(na,*prl,2); + na->unused_runs = 2; + } + } + *prl = rl; + if (rl && undecided) { + allocated = 0; + zrl = rl; + irl = 0; + while (zrl->length && (zrl->lcn >= 0) + && (allocated < endblock)) { + allocated += zrl->length; + zrl++; + irl++; + } + } + } + /* + * compression block not fully allocated and followed + * by a hole : we must allocate in the hole. + */ + if (rl && (allocated < endblock) && (zrl->lcn == LCN_HOLE)) { + s64 xofs; + + /* + * split the hole if not fully needed + */ + if ((allocated + zrl->length) > endblock) { + runlist_element *xrl; + + *prl = ntfs_rl_extend(na,*prl,1); + if (*prl) { + /* beware : rl was reallocated */ + rl = *prl; + zrl = &rl[irl]; + na->unused_runs = 0; + xrl = zrl; + while (xrl->length) xrl++; + do { + xrl[1] = *xrl; + } while (xrl-- != zrl); + zrl->length = endblock - allocated; + zrl[1].length -= zrl->length; + zrl[1].vcn = zrl->vcn + zrl->length; + NAttrSetRunlistDirty(na); + } + } + if (*prl) { + if (wasnonresident) + compressed_part = na->compression_block_clusters + - zrl->length; + xofs = 0; + if (ntfs_attr_fill_hole(na, + zrl->length << cluster_size_bits, + &xofs, &zrl, update_from)) + compressed_part = -1; + else { + /* go back to initial cluster, now reallocated */ + while (zrl->vcn > (pos >> cluster_size_bits)) + zrl--; + *prl = zrl; + } + } + } + if (!*prl) { + ntfs_log_error("No elements to borrow from a hole\n"); + compressed_part = -1; + } else + if ((*update_from == -1) || ((*prl)->vcn < *update_from)) + *update_from = (*prl)->vcn; + return (compressed_part); +} + +static int ntfs_attr_truncate_i(ntfs_attr *na, const s64 newsize, + hole_type holes); + +/** + * ntfs_attr_pwrite - positioned write to an ntfs attribute + * @na: ntfs attribute to write to + * @pos: position in the attribute to write to + * @count: number of bytes to write + * @b: data buffer to write to disk + * + * This function will write @count bytes from data buffer @b to ntfs attribute + * @na at position @pos. + * + * On success, return the number of successfully written bytes. If this number + * is lower than @count this means that an error was encountered during the + * write so that the write is partial. 0 means nothing was written (also return + * 0 when @count is 0). + * + * On error and nothing has been written, return -1 with errno set + * appropriately to the return code of ntfs_pwrite(), or to EINVAL in case of + * invalid arguments. + */ +static s64 ntfs_attr_pwrite_i(ntfs_attr *na, const s64 pos, s64 count, + const void *b) +{ + s64 written, to_write, ofs, old_initialized_size, old_data_size; + s64 total = 0; + VCN update_from = -1; + ntfs_volume *vol; + s64 fullcount; + ntfs_attr_search_ctx *ctx = NULL; + runlist_element *rl; + s64 hole_end; + int eo; + int compressed_part; + struct { + unsigned int undo_initialized_size : 1; + unsigned int undo_data_size : 1; + } need_to = { 0, 0 }; + BOOL wasnonresident = FALSE; + BOOL compressed; + + vol = na->ni->vol; + compressed = (na->data_flags & ATTR_COMPRESSION_MASK) + != const_cpu_to_le16(0); + na->unused_runs = 0; /* prepare overflow checks */ + /* + * Encrypted attributes are only supported in raw mode. We return + * access denied, which is what Windows NT4 does, too. + * Moreover a file cannot be both encrypted and compressed. + */ + if ((na->data_flags & ATTR_IS_ENCRYPTED) + && (compressed || !vol->efs_raw)) { + errno = EACCES; + goto errno_set; + } + /* + * Fill the gap, when writing beyond the end of a compressed + * file. This will make recursive calls + */ + if (compressed + && (na->type == AT_DATA) + && (pos > na->initialized_size) + && stuff_hole(na,pos)) + goto errno_set; + /* If this is a compressed attribute it needs special treatment. */ + wasnonresident = NAttrNonResident(na) != 0; + /* + * Compression is restricted to data streams and + * only ATTR_IS_COMPRESSED compression mode is supported. + */ + if (compressed + && ((na->type != AT_DATA) + || ((na->data_flags & ATTR_COMPRESSION_MASK) + != ATTR_IS_COMPRESSED))) { + errno = EOPNOTSUPP; + goto errno_set; + } + + if (!count) + goto out; + /* for a compressed file, get prepared to reserve a full block */ + fullcount = count; + /* If the write reaches beyond the end, extend the attribute. */ + old_data_size = na->data_size; + /* identify whether this is appending to a non resident data attribute */ + if ((na->type == AT_DATA) && (pos >= old_data_size) + && NAttrNonResident(na)) + NAttrSetDataAppending(na); + if (pos + count > na->data_size) { +#if PARTIAL_RUNLIST_UPDATING + /* + * When appending data, the attribute is first extended + * before being filled with data. This may cause the + * attribute to be made temporarily sparse, which + * implies reformating the inode and reorganizing the + * full runlist. To avoid unnecessary reorganization, + * we avoid sparse testing until the data is filled in. + */ + if (ntfs_attr_truncate_i(na, pos + count, + (NAttrDataAppending(na) ? + HOLES_DELAY : HOLES_OK))) { + ntfs_log_perror("Failed to enlarge attribute"); + goto errno_set; + } + /* + * If we avoided updating the runlist, we must be sure + * to cancel the enlargement and put back the runlist to + * a clean state if we get into some error. + */ + if (NAttrDataAppending(na)) + need_to.undo_data_size = 1; +#else + if (ntfs_attr_truncate_i(na, pos + count, HOLES_OK)) { + ntfs_log_perror("Failed to enlarge attribute"); + goto errno_set; + } +#endif + /* resizing may change the compression mode */ + compressed = (na->data_flags & ATTR_COMPRESSION_MASK) + != const_cpu_to_le16(0); + need_to.undo_data_size = 1; + } + /* + * For compressed data, a single full block was allocated + * to deal with compression, possibly in a previous call. + * We are not able to process several blocks because + * some clusters are freed after compression and + * new allocations have to be done before proceeding, + * so truncate the requested count if needed (big buffers). + */ + if (compressed) { + fullcount = (pos | (na->compression_block_size - 1)) + 1 - pos; + if (count > fullcount) + count = fullcount; + } + old_initialized_size = na->initialized_size; + /* If it is a resident attribute, write the data to the mft record. */ + if (!NAttrNonResident(na)) { + char *val; + + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + goto err_out; + if (ntfs_attr_lookup(na->type, na->name, na->name_len, 0, + 0, NULL, 0, ctx)) { + ntfs_log_perror("%s: lookup failed", __FUNCTION__); + goto err_out; + } + val = (char*)ctx->attr + le16_to_cpu(ctx->attr->value_offset); + if (val < (char*)ctx->attr || val + + le32_to_cpu(ctx->attr->value_length) > + (char*)ctx->mrec + vol->mft_record_size) { + errno = EIO; + ntfs_log_perror("%s: Sanity check failed", __FUNCTION__); + goto err_out; + } + memcpy(val + pos, b, count); + if (ntfs_mft_record_write(vol, ctx->ntfs_ino->mft_no, + ctx->mrec)) { + /* + * NOTE: We are in a bad state at this moment. We have + * dirtied the mft record but we failed to commit it to + * disk. Since we have read the mft record ok before, + * it is unlikely to fail writing it, so is ok to just + * return error here... (AIA) + */ + ntfs_log_perror("%s: failed to write mft record", __FUNCTION__); + goto err_out; + } + ntfs_attr_put_search_ctx(ctx); + total = count; + goto out; + } + + /* Handle writes beyond initialized_size. */ + + if (pos + count > na->initialized_size) { +#if PARTIAL_RUNLIST_UPDATING + /* + * When appending, we only need to map the end of the runlist, + * starting at the last previously allocated run, so that + * we are able a new one to it. + * However, for compressed file, we need the full compression + * block, which may be split in several extents. + */ + if (compressed && !NAttrDataAppending(na)) { + if (ntfs_attr_map_whole_runlist(na)) + goto err_out; + } else { + VCN block_begin; + + if (NAttrDataAppending(na) + || (pos < na->initialized_size)) + block_begin = pos >> vol->cluster_size_bits; + else + block_begin = na->initialized_size >> vol->cluster_size_bits; + + if (compressed) + block_begin &= -na->compression_block_clusters; + if (block_begin) + block_begin--; + if (ntfs_attr_map_partial_runlist(na, block_begin)) + goto err_out; + if ((update_from == -1) || (block_begin < update_from)) + update_from = block_begin; + } +#else + if (ntfs_attr_map_whole_runlist(na)) + goto err_out; +#endif + /* + * For a compressed attribute, we must be sure there is an + * available entry, and, when reopening a compressed file, + * we may need to split a hole. So reserve the entries + * before it gets too late. + */ + if (compressed) { + na->rl = ntfs_rl_extend(na,na->rl,2); + if (!na->rl) + goto err_out; + na->unused_runs = 2; + } + /* Set initialized_size to @pos + @count. */ + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + goto err_out; + if (ntfs_attr_lookup(na->type, na->name, na->name_len, 0, + 0, NULL, 0, ctx)) + goto err_out; + + /* If write starts beyond initialized_size, zero the gap. */ + if (pos > na->initialized_size) + if (ntfs_attr_fill_zero(na, na->initialized_size, + pos - na->initialized_size)) + goto err_out; + + ctx->attr->initialized_size = cpu_to_sle64(pos + count); + /* fix data_size for compressed files */ + if (compressed) { + na->data_size = pos + count; + ctx->attr->data_size = ctx->attr->initialized_size; + } + if (ntfs_mft_record_write(vol, ctx->ntfs_ino->mft_no, + ctx->mrec)) { + /* + * Undo the change in the in-memory copy and send it + * back for writing. + */ + ctx->attr->initialized_size = + cpu_to_sle64(old_initialized_size); + ntfs_mft_record_write(vol, ctx->ntfs_ino->mft_no, + ctx->mrec); + goto err_out; + } + na->initialized_size = pos + count; +#if CACHE_NIDATA_SIZE + if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY + ? na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30 + : na->type == AT_DATA && na->name == AT_UNNAMED) { + na->ni->data_size = na->data_size; + if ((compressed || NAttrSparse(na)) + && NAttrNonResident(na)) + na->ni->allocated_size = na->compressed_size; + else + na->ni->allocated_size = na->allocated_size; + set_nino_flag(na->ni,KnownSize); + } +#endif + ntfs_attr_put_search_ctx(ctx); + ctx = NULL; + /* + * NOTE: At this point the initialized_size in the mft record + * has been updated BUT there is random data on disk thus if + * we decide to abort, we MUST change the initialized_size + * again. + */ + need_to.undo_initialized_size = 1; + } + /* Find the runlist element containing the vcn. */ + rl = ntfs_attr_find_vcn(na, pos >> vol->cluster_size_bits); + if (!rl) { + /* + * If the vcn is not present it is an out of bounds write. + * However, we already extended the size of the attribute, + * so getting this here must be an error of some kind. + */ + if (errno == ENOENT) { + errno = EIO; + ntfs_log_perror("%s: Failed to find VCN #3", __FUNCTION__); + } + goto err_out; + } + /* + * Determine if there is compressed data in the current + * compression block (when appending to an existing file). + * If so, decompression will be needed, and the full block + * must be allocated to be identified as uncompressed. + * This comes in two variants, depending on whether + * compression has saved at least one cluster. + * The compressed size can never be over full size by + * more than 485 (maximum for 15 compression blocks + * compressed to 4098 and the last 3640 bytes compressed + * to 3640 + 3640/8 = 4095, with 15*2 + 4095 - 3640 = 485) + * This is less than the smallest cluster, so the hole is + * is never beyond the cluster next to the position of + * the first uncompressed byte to write. + */ + compressed_part = 0; + if (compressed) { + if ((rl->lcn == (LCN)LCN_HOLE) + && wasnonresident) { + if (rl->length < na->compression_block_clusters) + /* + * the needed block is in a hole smaller + * than the compression block : we can use + * it fully + */ + compressed_part + = na->compression_block_clusters + - rl->length; + else { + /* + * the needed block is in a hole bigger + * than the compression block : we must + * split the hole and use it partially + */ + compressed_part = split_compressed_hole(na, + &rl, pos, count, &update_from); + } + } else { + if (rl->lcn >= 0) { + /* + * the needed block contains data, make + * sure the full compression block is + * allocated. Borrow from hole if needed + */ + compressed_part = borrow_from_hole(na, + &rl, pos, count, &update_from, + wasnonresident); + } + } + + if (compressed_part < 0) + goto err_out; + + /* just making non-resident, so not yet compressed */ + if (NAttrBeingNonResident(na) + && (compressed_part < na->compression_block_clusters)) + compressed_part = 0; + } + ofs = pos - (rl->vcn << vol->cluster_size_bits); + /* + * Scatter the data from the linear data buffer to the volume. Note, a + * partial final vcn is taken care of by the @count capping of write + * length. + */ + for (hole_end = 0; count; rl++, ofs = 0) { + if (rl->lcn == LCN_RL_NOT_MAPPED) { + rl = ntfs_attr_find_vcn(na, rl->vcn); + if (!rl) { + if (errno == ENOENT) { + errno = EIO; + ntfs_log_perror("%s: Failed to find VCN" + " #4", __FUNCTION__); + } + goto rl_err_out; + } + /* Needed for case when runs merged. */ + ofs = pos + total - (rl->vcn << vol->cluster_size_bits); + } + if (!rl->length) { + errno = EIO; + ntfs_log_perror("%s: Zero run length", __FUNCTION__); + goto rl_err_out; + } + if (rl->lcn < (LCN)0) { + hole_end = rl->vcn + rl->length; + + if (rl->lcn != (LCN)LCN_HOLE) { + errno = EIO; + ntfs_log_perror("%s: Unexpected LCN (%lld)", + __FUNCTION__, + (long long)rl->lcn); + goto rl_err_out; + } + if (ntfs_attr_fill_hole(na, fullcount, &ofs, &rl, + &update_from)) + goto err_out; + } + if (compressed) { + while (rl->length + && (ofs >= (rl->length << vol->cluster_size_bits))) { + ofs -= rl->length << vol->cluster_size_bits; + rl++; + } + } + + /* It is a real lcn, write it to the volume. */ + to_write = min(count, (rl->length << vol->cluster_size_bits) - ofs); +retry: + ntfs_log_trace("Writing %lld bytes to vcn %lld, lcn %lld, ofs " + "%lld.\n", (long long)to_write, (long long)rl->vcn, + (long long)rl->lcn, (long long)ofs); + if (!NVolReadOnly(vol)) { + + s64 wpos = (rl->lcn << vol->cluster_size_bits) + ofs; + s64 wend = (rl->vcn << vol->cluster_size_bits) + ofs + to_write; + u32 bsize = vol->cluster_size; + /* Byte size needed to zero fill a cluster */ + s64 rounding = ((wend + bsize - 1) & ~(s64)(bsize - 1)) - wend; + /** + * Zero fill to cluster boundary if we're writing at the + * end of the attribute or into an ex-sparse cluster. + * This will cause the kernel not to seek and read disk + * blocks during write(2) to fill the end of the buffer + * which increases write speed by 2-10 fold typically. + * + * This is done even for compressed files, because + * data is generally first written uncompressed. + */ + if (rounding && ((wend == na->initialized_size) || + (wend < (hole_end << vol->cluster_size_bits)))){ + + char *cb; + + rounding += to_write; + + cb = ntfs_malloc(rounding); + if (!cb) + goto err_out; + + memcpy(cb, b, to_write); + memset(cb + to_write, 0, rounding - to_write); + + if (compressed) { + written = ntfs_compressed_pwrite(na, + rl, wpos, ofs, to_write, + rounding, cb, compressed_part, + &update_from); + } else { + written = ntfs_pwrite(vol->dev, wpos, + rounding, cb); + if (written == rounding) + written = to_write; + } + + free(cb); + } else { + if (compressed) { + written = ntfs_compressed_pwrite(na, + rl, wpos, ofs, to_write, + to_write, b, compressed_part, + &update_from); + } else + written = ntfs_pwrite(vol->dev, wpos, + to_write, b); + } + } else + written = to_write; + /* If everything ok, update progress counters and continue. */ + if (written > 0) { + total += written; + count -= written; + fullcount -= written; + b = (const u8*)b + written; + } + if (written != to_write) { + /* Partial write cannot be dealt with, stop there */ + /* If the syscall was interrupted, try again. */ + if (written == (s64)-1 && errno == EINTR) + goto retry; + if (!written) + errno = EIO; + goto rl_err_out; + } + compressed_part = 0; + } +done: + if (ctx) + ntfs_attr_put_search_ctx(ctx); + /* + * Update mapping pairs if needed. + * For a compressed file, we try to make a partial update + * of the mapping list. This makes a difference only if + * inode extents were needed. + */ + if (NAttrRunlistDirty(na)) { + if (ntfs_attr_update_mapping_pairs(na, + (update_from < 0 ? 0 : update_from))) { + /* + * FIXME: trying to recover by goto rl_err_out; + * could cause driver hang by infinite looping. + */ + total = -1; + goto out; + } + if (!wasnonresident) + NAttrClearBeingNonResident(na); + NAttrClearDataAppending(na); + } +out: + return total; +rl_err_out: + eo = errno; + if (total) { + if (need_to.undo_initialized_size) { + if (pos + total > na->initialized_size) + goto done; + /* + * TODO: Need to try to change initialized_size. If it + * succeeds goto done, otherwise goto err_out. (AIA) + */ + goto err_out; + } + goto done; + } + errno = eo; +err_out: + eo = errno; + if (need_to.undo_initialized_size) { + int err; + + err = 0; + if (!ctx) { + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + err = 1; + } else + ntfs_attr_reinit_search_ctx(ctx); + if (!err) { + err = ntfs_attr_lookup(na->type, na->name, + na->name_len, 0, 0, NULL, 0, ctx); + if (!err) { + na->initialized_size = old_initialized_size; + ctx->attr->initialized_size = cpu_to_sle64( + old_initialized_size); + err = ntfs_mft_record_write(vol, + ctx->ntfs_ino->mft_no, + ctx->mrec); + } + } + if (err) { + /* + * FIXME: At this stage could try to recover by filling + * old_initialized_size -> new_initialized_size with + * data or at least zeroes. (AIA) + */ + ntfs_log_error("Eeek! Failed to recover from error. " + "Leaving metadata in inconsistent " + "state! Run chkdsk!\n"); + } + } + if (ctx) + ntfs_attr_put_search_ctx(ctx); + /* Update mapping pairs if needed. */ + if (NAttrRunlistDirty(na)) + ntfs_attr_update_mapping_pairs(na, 0); + /* Restore original data_size if needed. */ + if (need_to.undo_data_size + && ntfs_attr_truncate_i(na, old_data_size, HOLES_OK)) + ntfs_log_perror("Failed to restore data_size"); + errno = eo; +errno_set: + total = -1; + goto out; +} + +s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count, const void *b) +{ + s64 total; + s64 written; + + ntfs_log_enter("Entering for inode %lld, attr 0x%x, pos 0x%llx, count " + "0x%llx.\n", (long long)na->ni->mft_no, le32_to_cpu(na->type), + (long long)pos, (long long)count); + + total = 0; + if (!na || !na->ni || !na->ni->vol || !b || pos < 0 || count < 0) { + errno = EINVAL; + written = -1; + ntfs_log_perror("%s", __FUNCTION__); + goto out; + } + + /* + * Compressed attributes may be written partially, so + * we may have to iterate. + */ + do { + written = ntfs_attr_pwrite_i(na, pos + total, + count - total, (const u8*)b + total); + if (written > 0) + total += written; + } while ((written > 0) && (total < count)); +out : + ntfs_log_leave("\n"); + return (total > 0 ? total : written); +} + + +int ntfs_attr_pclose(ntfs_attr *na) +{ + s64 ofs; + int failed; + BOOL ok = TRUE; + VCN update_from = -1; + ntfs_volume *vol; + ntfs_attr_search_ctx *ctx = NULL; + runlist_element *rl; + int eo; + int compressed_part; + BOOL compressed; + + ntfs_log_enter("Entering for inode 0x%llx, attr 0x%x.\n", + (unsigned long long)na->ni->mft_no, + le32_to_cpu(na->type)); + + if (!na || !na->ni || !na->ni->vol) { + errno = EINVAL; + ntfs_log_perror("%s", __FUNCTION__); + goto errno_set; + } + vol = na->ni->vol; + na->unused_runs = 0; + compressed = (na->data_flags & ATTR_COMPRESSION_MASK) + != const_cpu_to_le16(0); + /* + * Encrypted non-resident attributes are not supported. We return + * access denied, which is what Windows NT4 does, too. + */ + if (NAttrEncrypted(na) && NAttrNonResident(na)) { + errno = EACCES; + goto errno_set; + } + /* If this is not a compressed attribute get out */ + /* same if it is resident */ + if (!compressed || !NAttrNonResident(na)) + goto out; + + /* safety check : no recursion on close */ + if (NAttrComprClosing(na)) { + errno = EIO; + ntfs_log_error("Bad ntfs_attr_pclose" + " recursion on inode %lld\n", + (long long)na->ni->mft_no); + goto out; + } + NAttrSetComprClosing(na); + /* + * For a compressed attribute, we must be sure there are two + * available entries, so reserve them before it gets too late. + */ + if (ntfs_attr_map_whole_runlist(na)) + goto err_out; + na->rl = ntfs_rl_extend(na,na->rl,2); + if (!na->rl) + goto err_out; + na->unused_runs = 2; + /* Find the runlist element containing the terminal vcn. */ + rl = ntfs_attr_find_vcn(na, (na->initialized_size - 1) >> vol->cluster_size_bits); + if (!rl) { + /* + * If the vcn is not present it is an out of bounds write. + * However, we have already written the last byte uncompressed, + * so getting this here must be an error of some kind. + */ + if (errno == ENOENT) { + errno = EIO; + ntfs_log_perror("%s: Failed to find VCN #5", __FUNCTION__); + } + goto err_out; + } + /* + * Scatter the data from the linear data buffer to the volume. Note, a + * partial final vcn is taken care of by the @count capping of write + * length. + */ + compressed_part = 0; + if (rl->lcn >= 0) { + runlist_element *xrl; + + xrl = rl; + do { + xrl++; + } while (xrl->lcn >= 0); + compressed_part = (-xrl->length) + & (na->compression_block_clusters - 1); + } else + if (rl->lcn == (LCN)LCN_HOLE) { + if (rl->length < na->compression_block_clusters) + compressed_part + = na->compression_block_clusters + - rl->length; + else + compressed_part + = na->compression_block_clusters; + } + /* done, if the last block set was compressed */ + if (compressed_part) + goto out; + + ofs = na->initialized_size - (rl->vcn << vol->cluster_size_bits); + + if (rl->lcn == LCN_RL_NOT_MAPPED) { + rl = ntfs_attr_find_vcn(na, rl->vcn); + if (!rl) { + if (errno == ENOENT) { + errno = EIO; + ntfs_log_perror("%s: Failed to find VCN" + " #6", __FUNCTION__); + } + goto rl_err_out; + } + /* Needed for case when runs merged. */ + ofs = na->initialized_size - (rl->vcn << vol->cluster_size_bits); + } + if (!rl->length) { + errno = EIO; + ntfs_log_perror("%s: Zero run length", __FUNCTION__); + goto rl_err_out; + } + if (rl->lcn < (LCN)0) { + if (rl->lcn != (LCN)LCN_HOLE) { + errno = EIO; + ntfs_log_perror("%s: Unexpected LCN (%lld)", + __FUNCTION__, + (long long)rl->lcn); + goto rl_err_out; + } + + if (ntfs_attr_fill_hole(na, (s64)0, &ofs, &rl, &update_from)) + goto err_out; + } + while (rl->length + && (ofs >= (rl->length << vol->cluster_size_bits))) { + ofs -= rl->length << vol->cluster_size_bits; + rl++; + } + +retry: + failed = 0; + if (update_from < 0) update_from = 0; + if (!NVolReadOnly(vol)) { + failed = ntfs_compressed_close(na, rl, ofs, &update_from); +#if CACHE_NIDATA_SIZE + if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY + ? na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30 + : na->type == AT_DATA && na->name == AT_UNNAMED) { + na->ni->data_size = na->data_size; + na->ni->allocated_size = na->compressed_size; + set_nino_flag(na->ni,KnownSize); + } +#endif + } + if (failed) { + /* If the syscall was interrupted, try again. */ + if (errno == EINTR) + goto retry; + else + goto rl_err_out; + } + if (ctx) + ntfs_attr_put_search_ctx(ctx); + /* Update mapping pairs if needed. */ + if (NAttrFullyMapped(na)) + if (ntfs_attr_update_mapping_pairs(na, update_from)) { + /* + * FIXME: trying to recover by goto rl_err_out; + * could cause driver hang by infinite looping. + */ + ok = FALSE; + goto out; + } +out: + NAttrClearComprClosing(na); + ntfs_log_leave("\n"); + return (!ok); +rl_err_out: + /* + * need not restore old sizes, only compressed_size + * can have changed. It has been set according to + * the current runlist while updating the mapping pairs, + * and must be kept consistent with the runlists. + */ +err_out: + eo = errno; + if (ctx) + ntfs_attr_put_search_ctx(ctx); + /* Update mapping pairs if needed. */ + if (NAttrFullyMapped(na)) + ntfs_attr_update_mapping_pairs(na, 0); + errno = eo; +errno_set: + ok = FALSE; + goto out; +} + +/** + * ntfs_attr_mst_pread - multi sector transfer protected ntfs attribute read + * @na: multi sector transfer protected ntfs attribute to read from + * @pos: byte position in the attribute to begin reading from + * @bk_cnt: number of mst protected blocks to read + * @bk_size: size of each mst protected block in bytes + * @dst: output data buffer + * + * This function will read @bk_cnt blocks of size @bk_size bytes each starting + * at offset @pos from the ntfs attribute @na into the data buffer @b. + * + * On success, the multi sector transfer fixups are applied and the number of + * read blocks is returned. If this number is lower than @bk_cnt this means + * that the read has either reached end of attribute or that an error was + * encountered during the read so that the read is partial. 0 means end of + * attribute or nothing to read (also return 0 when @bk_cnt or @bk_size are 0). + * + * On error and nothing has been read, return -1 with errno set appropriately + * to the return code of ntfs_attr_pread() or to EINVAL in case of invalid + * arguments. + * + * NOTE: If an incomplete multi sector transfer is detected the magic is + * changed to BAAD but no error is returned, i.e. it is possible that any of + * the returned blocks have multi sector transfer errors. This should be + * detected by the caller by checking each block with is_baad_recordp(&block). + * The reasoning is that we want to fixup as many blocks as possible and we + * want to return even bad ones to the caller so, e.g. in case of ntfsck, the + * errors can be repaired. + */ +s64 ntfs_attr_mst_pread(ntfs_attr *na, const s64 pos, const s64 bk_cnt, + const u32 bk_size, void *dst) +{ + s64 br; + u8 *end; + BOOL warn; + + ntfs_log_trace("Entering for inode 0x%llx, attr type 0x%x, pos 0x%llx.\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type), + (long long)pos); + if (bk_cnt < 0 || bk_size % NTFS_BLOCK_SIZE) { + errno = EINVAL; + ntfs_log_perror("%s", __FUNCTION__); + return -1; + } + br = ntfs_attr_pread(na, pos, bk_cnt * bk_size, dst); + if (br <= 0) + return br; + br /= bk_size; + /* log errors unless silenced */ + warn = !na->ni || !na->ni->vol || !NVolNoFixupWarn(na->ni->vol); + for (end = (u8*)dst + br * bk_size; (u8*)dst < end; dst = (u8*)dst + + bk_size) + ntfs_mst_post_read_fixup_warn((NTFS_RECORD*)dst, bk_size, warn); + /* Finally, return the number of blocks read. */ + return br; +} + +/** + * ntfs_attr_mst_pwrite - multi sector transfer protected ntfs attribute write + * @na: multi sector transfer protected ntfs attribute to write to + * @pos: position in the attribute to write to + * @bk_cnt: number of mst protected blocks to write + * @bk_size: size of each mst protected block in bytes + * @src: data buffer to write to disk + * + * This function will write @bk_cnt blocks of size @bk_size bytes each from + * data buffer @b to multi sector transfer (mst) protected ntfs attribute @na + * at position @pos. + * + * On success, return the number of successfully written blocks. If this number + * is lower than @bk_cnt this means that an error was encountered during the + * write so that the write is partial. 0 means nothing was written (also + * return 0 when @bk_cnt or @bk_size are 0). + * + * On error and nothing has been written, return -1 with errno set + * appropriately to the return code of ntfs_attr_pwrite(), or to EINVAL in case + * of invalid arguments. + * + * NOTE: We mst protect the data, write it, then mst deprotect it using a quick + * deprotect algorithm (no checking). This saves us from making a copy before + * the write and at the same time causes the usn to be incremented in the + * buffer. This conceptually fits in better with the idea that cached data is + * always deprotected and protection is performed when the data is actually + * going to hit the disk and the cache is immediately deprotected again + * simulating an mst read on the written data. This way cache coherency is + * achieved. + */ +s64 ntfs_attr_mst_pwrite(ntfs_attr *na, const s64 pos, s64 bk_cnt, + const u32 bk_size, void *src) +{ + s64 written, i; + + ntfs_log_trace("Entering for inode 0x%llx, attr type 0x%x, pos 0x%llx.\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type), + (long long)pos); + if (bk_cnt < 0 || bk_size % NTFS_BLOCK_SIZE) { + errno = EINVAL; + return -1; + } + if (!bk_cnt) + return 0; + /* Prepare data for writing. */ + for (i = 0; i < bk_cnt; ++i) { + int err; + + err = ntfs_mst_pre_write_fixup((NTFS_RECORD*) + ((u8*)src + i * bk_size), bk_size); + if (err < 0) { + /* Abort write at this position. */ + ntfs_log_perror("%s #1", __FUNCTION__); + if (!i) + return err; + bk_cnt = i; + break; + } + } + /* Write the prepared data. */ + written = ntfs_attr_pwrite(na, pos, bk_cnt * bk_size, src); + if (written <= 0) { + ntfs_log_perror("%s: written=%lld", __FUNCTION__, + (long long)written); + } + /* Quickly deprotect the data again. */ + for (i = 0; i < bk_cnt; ++i) + ntfs_mst_post_write_fixup((NTFS_RECORD*)((u8*)src + i * + bk_size)); + if (written <= 0) + return written; + /* Finally, return the number of complete blocks written. */ + return written / bk_size; +} + +/** + * ntfs_attr_find - find (next) attribute in mft record + * @type: attribute type to find + * @name: attribute name to find (optional, i.e. NULL means don't care) + * @name_len: attribute name length (only needed if @name present) + * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) + * @val: attribute value to find (optional, resident attributes only) + * @val_len: attribute value length + * @ctx: search context with mft record and attribute to search from + * + * You shouldn't need to call this function directly. Use lookup_attr() instead. + * + * ntfs_attr_find() takes a search context @ctx as parameter and searches the + * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an + * attribute of @type, optionally @name and @val. If found, ntfs_attr_find() + * returns 0 and @ctx->attr will point to the found attribute. + * + * If not found, ntfs_attr_find() returns -1, with errno set to ENOENT and + * @ctx->attr will point to the attribute before which the attribute being + * searched for would need to be inserted if such an action were to be desired. + * + * On actual error, ntfs_attr_find() returns -1 with errno set to the error + * code but not to ENOENT. In this case @ctx->attr is undefined and in + * particular do not rely on it not changing. + * + * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it + * is FALSE, the search begins after @ctx->attr. + * + * If @type is AT_UNUSED, return the first found attribute, i.e. one can + * enumerate all attributes by setting @type to AT_UNUSED and then calling + * ntfs_attr_find() repeatedly until it returns -1 with errno set to ENOENT to + * indicate that there are no more entries. During the enumeration, each + * successful call of ntfs_attr_find() will return the next attribute in the + * mft record @ctx->mrec. + * + * If @type is AT_END, seek to the end and return -1 with errno set to ENOENT. + * AT_END is not a valid attribute, its length is zero for example, thus it is + * safer to return error instead of success in this case. This also allows us + * to interoperate cleanly with ntfs_external_attr_find(). + * + * If @name is AT_UNNAMED search for an unnamed attribute. If @name is present + * but not AT_UNNAMED search for a named attribute matching @name. Otherwise, + * match both named and unnamed attributes. + * + * If @ic is IGNORE_CASE, the @name comparison is not case sensitive and + * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record + * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at + * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case + * sensitive. When @name is present, @name_len is the @name length in Unicode + * characters. + * + * If @name is not present (NULL), we assume that the unnamed attribute is + * being searched for. + * + * Finally, the resident attribute value @val is looked for, if present. + * If @val is not present (NULL), @val_len is ignored. + * + * ntfs_attr_find() only searches the specified mft record and it ignores the + * presence of an attribute list attribute (unless it is the one being searched + * for, obviously). If you need to take attribute lists into consideration, use + * ntfs_attr_lookup() instead (see below). This also means that you cannot use + * ntfs_attr_find() to search for extent records of non-resident attributes, as + * extents with lowest_vcn != 0 are usually described by the attribute list + * attribute only. - Note that it is possible that the first extent is only in + * the attribute list while the last extent is in the base mft record, so don't + * rely on being able to find the first extent in the base mft record. + * + * Warning: Never use @val when looking for attribute types which can be + * non-resident as this most likely will result in a crash! + */ +static int ntfs_attr_find(const ATTR_TYPES type, const ntfschar *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx) +{ + ATTR_RECORD *a; + ntfs_volume *vol; + ntfschar *upcase; + ptrdiff_t offs; + ptrdiff_t space; + u32 upcase_len; + + ntfs_log_trace("attribute type 0x%x.\n", le32_to_cpu(type)); + + if (ctx->ntfs_ino) { + vol = ctx->ntfs_ino->vol; + upcase = vol->upcase; + upcase_len = vol->upcase_len; + } else { + if (name && name != AT_UNNAMED) { + errno = EINVAL; + ntfs_log_perror("%s", __FUNCTION__); + return -1; + } + vol = NULL; + upcase = NULL; + upcase_len = 0; + } + /* + * Iterate over attributes in mft record starting at @ctx->attr, or the + * attribute following that, if @ctx->is_first is TRUE. + */ + if (ctx->is_first) { + a = ctx->attr; + ctx->is_first = FALSE; + } else + a = (ATTR_RECORD*)((char*)ctx->attr + + le32_to_cpu(ctx->attr->length)); + for (;; a = (ATTR_RECORD*)((char*)a + le32_to_cpu(a->length))) { + /* + * Make sure the attribute fully lies within the MFT record + * and we can safely access its minimal fields. + */ + offs = p2n(a) - p2n(ctx->mrec); + space = le32_to_cpu(ctx->mrec->bytes_in_use) - offs; + if ((offs < 0) + || (((space < (ptrdiff_t)offsetof(ATTR_RECORD, + resident_end)) + || (space < (ptrdiff_t)le32_to_cpu(a->length))) + && ((space < 4) || (a->type != AT_END)))) + break; + ctx->attr = a; + if (((type != AT_UNUSED) && (le32_to_cpu(a->type) > + le32_to_cpu(type))) || + (a->type == AT_END)) { + errno = ENOENT; + return -1; + } + if (!a->length) + break; + /* If this is an enumeration return this attribute. */ + if (type == AT_UNUSED) + return 0; + if (a->type != type) + continue; + /* + * If @name is AT_UNNAMED we want an unnamed attribute. + * If @name is present, compare the two names. + * Otherwise, match any attribute. + */ + if (name == AT_UNNAMED) { + /* The search failed if the found attribute is named. */ + if (a->name_length) { + errno = ENOENT; + return -1; + } + } else { + register int rc; + + if (a->name_length + && ((le16_to_cpu(a->name_offset) + + a->name_length * sizeof(ntfschar)) + > le32_to_cpu(a->length))) { + ntfs_log_error("Corrupt attribute name" + " in MFT record %lld\n", + (long long)ctx->ntfs_ino->mft_no); + break; + } + if (name && ((rc = ntfs_names_full_collate(name, + name_len, (ntfschar*)((char*)a + + le16_to_cpu(a->name_offset)), + a->name_length, ic, + upcase, upcase_len)))) { + /* + * If @name collates before a->name, + * there is no matching attribute. + */ + if (rc < 0) { + errno = ENOENT; + return -1; + } + /* If the strings are not equal, continue search. */ + continue; + } + } + /* + * The names match or @name not present and attribute is + * unnamed. If no @val specified, we have found the attribute + * and are done. + */ + if (!val) + return 0; + /* @val is present; compare values. */ + else { + register int rc; + + rc = memcmp(val, (char*)a +le16_to_cpu(a->value_offset), + min(val_len, + le32_to_cpu(a->value_length))); + /* + * If @val collates before the current attribute's + * value, there is no matching attribute. + */ + if (!rc) { + register u32 avl; + avl = le32_to_cpu(a->value_length); + if (val_len == avl) + return 0; + if (val_len < avl) { + errno = ENOENT; + return -1; + } + } else if (rc < 0) { + errno = ENOENT; + return -1; + } + } + } + errno = EIO; + ntfs_log_perror("%s: Corrupt inode (%lld)", __FUNCTION__, + ctx->ntfs_ino ? (long long)ctx->ntfs_ino->mft_no : -1); + return -1; +} + +void ntfs_attr_name_free(char **name) +{ + if (*name) { + free(*name); + *name = NULL; + } +} + +char *ntfs_attr_name_get(const ntfschar *uname, const int uname_len) +{ + char *name = NULL; + int name_len; + + name_len = ntfs_ucstombs(uname, uname_len, &name, 0); + if (name_len < 0) { + ntfs_log_perror("ntfs_ucstombs"); + return NULL; + + } else if (name_len > 0) + return name; + + ntfs_attr_name_free(&name); + return NULL; +} + +/** + * ntfs_external_attr_find - find an attribute in the attribute list of an inode + * @type: attribute type to find + * @name: attribute name to find (optional, i.e. NULL means don't care) + * @name_len: attribute name length (only needed if @name present) + * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) + * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only) + * @val: attribute value to find (optional, resident attributes only) + * @val_len: attribute value length + * @ctx: search context with mft record and attribute to search from + * + * You shouldn't need to call this function directly. Use ntfs_attr_lookup() + * instead. + * + * Find an attribute by searching the attribute list for the corresponding + * attribute list entry. Having found the entry, map the mft record for read + * if the attribute is in a different mft record/inode, find the attribute in + * there and return it. + * + * If @type is AT_UNUSED, return the first found attribute, i.e. one can + * enumerate all attributes by setting @type to AT_UNUSED and then calling + * ntfs_external_attr_find() repeatedly until it returns -1 with errno set to + * ENOENT to indicate that there are no more entries. During the enumeration, + * each successful call of ntfs_external_attr_find() will return the next + * attribute described by the attribute list of the base mft record described + * by the search context @ctx. + * + * If @type is AT_END, seek to the end of the base mft record ignoring the + * attribute list completely and return -1 with errno set to ENOENT. AT_END is + * not a valid attribute, its length is zero for example, thus it is safer to + * return error instead of success in this case. + * + * If @name is AT_UNNAMED search for an unnamed attribute. If @name is present + * but not AT_UNNAMED search for a named attribute matching @name. Otherwise, + * match both named and unnamed attributes. + * + * On first search @ctx->ntfs_ino must be the inode of the base mft record and + * @ctx must have been obtained from a call to ntfs_attr_get_search_ctx(). + * On subsequent calls, @ctx->ntfs_ino can be any extent inode, too + * (@ctx->base_ntfs_ino is then the base inode). + * + * After finishing with the attribute/mft record you need to call + * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any + * mapped extent inodes, etc). + * + * Return 0 if the search was successful and -1 if not, with errno set to the + * error code. + * + * On success, @ctx->attr is the found attribute, it is in mft record + * @ctx->mrec, and @ctx->al_entry is the attribute list entry for this + * attribute with @ctx->base_* being the base mft record to which @ctx->attr + * belongs. + * + * On error ENOENT, i.e. attribute not found, @ctx->attr is set to the + * attribute which collates just after the attribute being searched for in the + * base ntfs inode, i.e. if one wants to add the attribute to the mft record + * this is the correct place to insert it into, and if there is not enough + * space, the attribute should be placed in an extent mft record. + * @ctx->al_entry points to the position within @ctx->base_ntfs_ino->attr_list + * at which the new attribute's attribute list entry should be inserted. The + * other @ctx fields, base_ntfs_ino, base_mrec, and base_attr are set to NULL. + * The only exception to this is when @type is AT_END, in which case + * @ctx->al_entry is set to NULL also (see above). + * + * The following error codes are defined: + * ENOENT Attribute not found, not an error as such. + * EINVAL Invalid arguments. + * EIO I/O error or corrupt data structures found. + * ENOMEM Not enough memory to allocate necessary buffers. + */ +static int ntfs_external_attr_find(ATTR_TYPES type, const ntfschar *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const VCN lowest_vcn, const u8 *val, const u32 val_len, + ntfs_attr_search_ctx *ctx) +{ + ntfs_inode *base_ni, *ni; + ntfs_volume *vol; + ATTR_LIST_ENTRY *al_entry, *next_al_entry; + u8 *al_start, *al_end; + ATTR_RECORD *a; + ntfschar *al_name; + ptrdiff_t offs; + ptrdiff_t space; + u32 al_name_len; + BOOL is_first_search = FALSE; + + ni = ctx->ntfs_ino; + base_ni = ctx->base_ntfs_ino; + ntfs_log_trace("Entering for inode %lld, attribute type 0x%x.\n", + (unsigned long long)ni->mft_no, le32_to_cpu(type)); + if (!base_ni) { + /* First call happens with the base mft record. */ + base_ni = ctx->base_ntfs_ino = ctx->ntfs_ino; + ctx->base_mrec = ctx->mrec; + } + if (ni == base_ni) + ctx->base_attr = ctx->attr; + if (type == AT_END) + goto not_found; + vol = base_ni->vol; + al_start = base_ni->attr_list; + al_end = al_start + base_ni->attr_list_size; + if (!ctx->al_entry) { + ctx->al_entry = (ATTR_LIST_ENTRY*)al_start; + is_first_search = TRUE; + } + /* + * Iterate over entries in attribute list starting at @ctx->al_entry, + * or the entry following that, if @ctx->is_first is TRUE. + */ + if (ctx->is_first) { + al_entry = ctx->al_entry; + ctx->is_first = FALSE; + /* + * If an enumeration and the first attribute is higher than + * the attribute list itself, need to return the attribute list + * attribute. + */ + if ((type == AT_UNUSED) && is_first_search && + le32_to_cpu(al_entry->type) > + le32_to_cpu(AT_ATTRIBUTE_LIST)) + goto find_attr_list_attr; + } else { + /* Check for small entry */ + if (((p2n(al_end) - p2n(ctx->al_entry)) + < (long)offsetof(ATTR_LIST_ENTRY, name)) + || (le16_to_cpu(ctx->al_entry->length) & 7) + || (le16_to_cpu(ctx->al_entry->length) + < offsetof(ATTR_LIST_ENTRY, name))) + goto corrupt; + + al_entry = (ATTR_LIST_ENTRY*)((char*)ctx->al_entry + + le16_to_cpu(ctx->al_entry->length)); + if ((u8*)al_entry == al_end) + goto not_found; + /* Preliminary check for small entry */ + if ((p2n(al_end) - p2n(al_entry)) + < (long)offsetof(ATTR_LIST_ENTRY, name)) + goto corrupt; + /* + * If this is an enumeration and the attribute list attribute + * is the next one in the enumeration sequence, just return the + * attribute list attribute from the base mft record as it is + * not listed in the attribute list itself. + */ + if ((type == AT_UNUSED) && le32_to_cpu(ctx->al_entry->type) < + le32_to_cpu(AT_ATTRIBUTE_LIST) && + le32_to_cpu(al_entry->type) > + le32_to_cpu(AT_ATTRIBUTE_LIST)) { + int rc; +find_attr_list_attr: + + /* Check for bogus calls. */ + if (name || name_len || val || val_len || lowest_vcn) { + errno = EINVAL; + ntfs_log_perror("%s", __FUNCTION__); + return -1; + } + + /* We want the base record. */ + ctx->ntfs_ino = base_ni; + ctx->mrec = ctx->base_mrec; + ctx->is_first = TRUE; + /* Sanity checks are performed elsewhere. */ + ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec + + le16_to_cpu(ctx->mrec->attrs_offset)); + + /* Find the attribute list attribute. */ + rc = ntfs_attr_find(AT_ATTRIBUTE_LIST, NULL, 0, + IGNORE_CASE, NULL, 0, ctx); + + /* + * Setup the search context so the correct + * attribute is returned next time round. + */ + ctx->al_entry = al_entry; + ctx->is_first = TRUE; + + /* Got it. Done. */ + if (!rc) + return 0; + + /* Error! If other than not found return it. */ + if (errno != ENOENT) + return rc; + + /* Not found?!? Absurd! */ + errno = EIO; + ntfs_log_error("Attribute list wasn't found"); + return -1; + } + } + for (;; al_entry = next_al_entry) { + /* Out of bounds check. */ + if ((u8*)al_entry < base_ni->attr_list || + (u8*)al_entry > al_end) + break; /* Inode is corrupt. */ + ctx->al_entry = al_entry; + /* Catch the end of the attribute list. */ + if ((u8*)al_entry == al_end) + goto not_found; + + if ((((u8*)al_entry + offsetof(ATTR_LIST_ENTRY, name)) > al_end) + || ((u8*)al_entry + le16_to_cpu(al_entry->length) > al_end) + || (le16_to_cpu(al_entry->length) & 7) + || (le16_to_cpu(al_entry->length) + < offsetof(ATTR_LIST_ENTRY, name_length)) + || (al_entry->name_length + && ((u8*)al_entry + al_entry->name_offset + + al_entry->name_length * sizeof(ntfschar)) + > al_end)) + break; /* corrupt */ + + next_al_entry = (ATTR_LIST_ENTRY*)((u8*)al_entry + + le16_to_cpu(al_entry->length)); + if (type != AT_UNUSED) { + if (le32_to_cpu(al_entry->type) > le32_to_cpu(type)) + goto not_found; + if (type != al_entry->type) + continue; + } + al_name_len = al_entry->name_length; + al_name = (ntfschar*)((u8*)al_entry + al_entry->name_offset); + /* + * If !@type we want the attribute represented by this + * attribute list entry. + */ + if (type == AT_UNUSED) + goto is_enumeration; + /* + * If @name is AT_UNNAMED we want an unnamed attribute. + * If @name is present, compare the two names. + * Otherwise, match any attribute. + */ + if (name == AT_UNNAMED) { + if (al_name_len) + goto not_found; + } else { + int rc; + + if (name && ((rc = ntfs_names_full_collate(name, + name_len, al_name, al_name_len, ic, + vol->upcase, vol->upcase_len)))) { + + /* + * If @name collates before al_name, + * there is no matching attribute. + */ + if (rc < 0) + goto not_found; + /* If the strings are not equal, continue search. */ + continue; + } + } + /* + * The names match or @name not present and attribute is + * unnamed. Now check @lowest_vcn. Continue search if the + * next attribute list entry still fits @lowest_vcn. Otherwise + * we have reached the right one or the search has failed. + */ + if (lowest_vcn && (u8*)next_al_entry >= al_start && + (u8*)next_al_entry + 6 < al_end && + (u8*)next_al_entry + le16_to_cpu( + next_al_entry->length) <= al_end && + sle64_to_cpu(next_al_entry->lowest_vcn) <= + lowest_vcn && + next_al_entry->type == al_entry->type && + next_al_entry->name_length == al_name_len && + ntfs_names_are_equal((ntfschar*)((char*) + next_al_entry + + next_al_entry->name_offset), + next_al_entry->name_length, + al_name, al_name_len, CASE_SENSITIVE, + vol->upcase, vol->upcase_len)) + continue; +is_enumeration: + if (MREF_LE(al_entry->mft_reference) == ni->mft_no) { + if (MSEQNO_LE(al_entry->mft_reference) != + le16_to_cpu( + ni->mrec->sequence_number)) { + ntfs_log_error("Found stale mft reference in " + "attribute list!\n"); + break; + } + } else { /* Mft references do not match. */ + /* Do we want the base record back? */ + if (MREF_LE(al_entry->mft_reference) == + base_ni->mft_no) { + ni = ctx->ntfs_ino = base_ni; + ctx->mrec = ctx->base_mrec; + } else { + /* We want an extent record. */ + if (!vol->mft_na) { + ntfs_log_perror("$MFT not ready for " + "opening an extent to inode %lld\n", + (long long)base_ni->mft_no); + break; + } + ni = ntfs_extent_inode_open(base_ni, + al_entry->mft_reference); + if (!ni) + break; + ctx->ntfs_ino = ni; + ctx->mrec = ni->mrec; + } + } + a = ctx->attr = (ATTR_RECORD*)((char*)ctx->mrec + + le16_to_cpu(ctx->mrec->attrs_offset)); + /* + * ctx->ntfs_ino, ctx->mrec, and ctx->attr now point to the + * mft record containing the attribute represented by the + * current al_entry. + * + * We could call into ntfs_attr_find() to find the right + * attribute in this mft record but this would be less + * efficient and not quite accurate as ntfs_attr_find() ignores + * the attribute instance numbers for example which become + * important when one plays with attribute lists. Also, because + * a proper match has been found in the attribute list entry + * above, the comparison can now be optimized. So it is worth + * re-implementing a simplified ntfs_attr_find() here. + * + * Use a manual loop so we can still use break and continue + * with the same meanings as above. + */ +do_next_attr_loop: + /* + * Make sure the attribute fully lies within the MFT record + * and we can safely access its minimal fields. + */ + offs = p2n(a) - p2n(ctx->mrec); + space = le32_to_cpu(ctx->mrec->bytes_in_use) - offs; + if (offs < 0) + break; + if ((space >= 4) && (a->type == AT_END)) + continue; + if ((space < (ptrdiff_t)offsetof(ATTR_RECORD, resident_end)) + || (space < (ptrdiff_t)le32_to_cpu(a->length))) + break; + if (al_entry->instance != a->instance) + goto do_next_attr; + /* + * If the type and/or the name are/is mismatched between the + * attribute list entry and the attribute record, there is + * corruption so we break and return error EIO. + */ + if (al_entry->type != a->type) + break; + if (!ntfs_names_are_equal((ntfschar*)((char*)a + + le16_to_cpu(a->name_offset)), + a->name_length, al_name, + al_name_len, CASE_SENSITIVE, + vol->upcase, vol->upcase_len)) + break; + ctx->attr = a; + /* + * If no @val specified or @val specified and it matches, we + * have found it! Also, if !@type, it is an enumeration, so we + * want the current attribute. + */ + if ((type == AT_UNUSED) || !val || (!a->non_resident && + le32_to_cpu(a->value_length) == val_len && + !memcmp((char*)a + le16_to_cpu(a->value_offset), + val, val_len))) { + return 0; + } +do_next_attr: + /* Proceed to the next attribute in the current mft record. */ + a = (ATTR_RECORD*)((char*)a + le32_to_cpu(a->length)); + goto do_next_attr_loop; + } +corrupt : + if (ni != base_ni) { + ctx->ntfs_ino = base_ni; + ctx->mrec = ctx->base_mrec; + ctx->attr = ctx->base_attr; + } + errno = EIO; + ntfs_log_error("Corrupt attribute list entry in MFT record %lld\n", + (long long)base_ni->mft_no); + return -1; +not_found: + /* + * If we were looking for AT_END or we were enumerating and reached the + * end, we reset the search context @ctx and use ntfs_attr_find() to + * seek to the end of the base mft record. + */ + if (type == AT_UNUSED || type == AT_END) { + ntfs_attr_reinit_search_ctx(ctx); + return ntfs_attr_find(AT_END, name, name_len, ic, val, val_len, + ctx); + } + /* + * The attribute wasn't found. Before we return, we want to ensure + * @ctx->mrec and @ctx->attr indicate the position at which the + * attribute should be inserted in the base mft record. Since we also + * want to preserve @ctx->al_entry we cannot reinitialize the search + * context using ntfs_attr_reinit_search_ctx() as this would set + * @ctx->al_entry to NULL. Thus we do the necessary bits manually (see + * ntfs_attr_init_search_ctx() below). Note, we _only_ preserve + * @ctx->al_entry as the remaining fields (base_*) are identical to + * their non base_ counterparts and we cannot set @ctx->base_attr + * correctly yet as we do not know what @ctx->attr will be set to by + * the call to ntfs_attr_find() below. + */ + ctx->mrec = ctx->base_mrec; + ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec + + le16_to_cpu(ctx->mrec->attrs_offset)); + ctx->is_first = TRUE; + ctx->ntfs_ino = ctx->base_ntfs_ino; + ctx->base_ntfs_ino = NULL; + ctx->base_mrec = NULL; + ctx->base_attr = NULL; + /* + * In case there are multiple matches in the base mft record, need to + * keep enumerating until we get an attribute not found response (or + * another error), otherwise we would keep returning the same attribute + * over and over again and all programs using us for enumeration would + * lock up in a tight loop. + */ + { + int ret; + + do { + ret = ntfs_attr_find(type, name, name_len, ic, val, + val_len, ctx); + } while (!ret); + return ret; + } +} + +/* + * Check the consistency of an attribute + * + * Do the general consistency checks of the selected attribute : + * - the required fields can be accessed + * - the variable fields do not overflow + * - the attribute is [non-]resident if it must be + * - miscelleaneous checks + * + * Returns 0 if the checks pass + * -1 with errno = EIO otherwise + */ + +int ntfs_attr_inconsistent(const ATTR_RECORD *a, const MFT_REF mref) +{ + const FILE_NAME_ATTR *fn; + const INDEX_ROOT *ir; + u64 inum; + int ret; + + /* + * The attribute was found to fully lie within the MFT + * record, now make sure its relevant parts (name, runlist, + * value) also lie within. The first step is to make sure + * the attribute has the minimum length so that accesses to + * the lengths and offsets of these parts are safe. + */ + ret = 0; + inum = MREF(mref); + if (a->non_resident) { + if ((a->non_resident != 1) + || (le32_to_cpu(a->length) + < offsetof(ATTR_RECORD, non_resident_end)) + || (le16_to_cpu(a->mapping_pairs_offset) + >= le32_to_cpu(a->length)) + || (a->name_length + && (((u32)le16_to_cpu(a->name_offset) + + a->name_length * sizeof(ntfschar)) + > le32_to_cpu(a->length))) + || (le64_to_cpu(a->highest_vcn) + < le64_to_cpu(a->lowest_vcn))) { + ntfs_log_error("Corrupt non resident attribute" + " 0x%x in MFT record %lld\n", + (int)le32_to_cpu(a->type), + (long long)inum); + errno = EIO; + ret = -1; + } + } else { + if ((le32_to_cpu(a->length) + < offsetof(ATTR_RECORD, resident_end)) + || (le32_to_cpu(a->value_length) & 0xff000000) + || (a->value_length + && ((le16_to_cpu(a->value_offset) + + le32_to_cpu(a->value_length)) + > le32_to_cpu(a->length))) + || (a->name_length + && (((u32)le16_to_cpu(a->name_offset) + + a->name_length * sizeof(ntfschar)) + > le32_to_cpu(a->length)))) { + ntfs_log_error("Corrupt resident attribute" + " 0x%x in MFT record %lld\n", + (int)le32_to_cpu(a->type), + (long long)inum); + errno = EIO; + ret = -1; + } + } + if (!ret) { + /* + * Checking whether an attribute must be [non-]resident + * is hard-coded for well-known ones. This should be + * done through ntfs_attr_can_be_non_resident(), based on + * $AttrDef, but this would give an easy way to bypass + * the checks. + * Attributes which are not well-known are not checked. + * + * Note : at this stage we know that a->length and + * a->value_length cannot look like being negative. + */ + switch(a->type) { + case AT_FILE_NAME : + /* Check file names are resident and do not overflow */ + fn = (const FILE_NAME_ATTR*)((const u8*)a + + le16_to_cpu(a->value_offset)); + if (a->non_resident + || (le32_to_cpu(a->value_length) + < offsetof(FILE_NAME_ATTR, file_name)) + || !fn->file_name_length + || ((fn->file_name_length * sizeof(ntfschar) + + offsetof(FILE_NAME_ATTR, file_name)) + > le32_to_cpu(a->value_length))) { + ntfs_log_error("Corrupt file name" + " attribute in MFT record %lld.\n", + (long long)inum); + errno = EIO; + ret = -1; + } + break; + case AT_INDEX_ROOT : + /* Check root index is resident and does not overflow */ + ir = (const INDEX_ROOT*)((const u8*)a + + le16_to_cpu(a->value_offset)); + /* index.allocated_size may overflow while resizing */ + if (a->non_resident + || (le32_to_cpu(a->value_length) + < offsetof(INDEX_ROOT, index.reserved)) + || (le32_to_cpu(ir->index_block_size) + < NTFS_BLOCK_SIZE) + || (le32_to_cpu(ir->index_block_size) + & (le32_to_cpu(ir->index_block_size) - 1)) + || (le32_to_cpu(ir->index.entries_offset) + < sizeof(INDEX_HEADER)) + || (le32_to_cpu(ir->index.index_length) + < le32_to_cpu(ir->index.entries_offset)) + || (le32_to_cpu(ir->index.allocated_size) + < le32_to_cpu(ir->index.index_length)) + || (le32_to_cpu(a->value_length) + < (le32_to_cpu(ir->index.allocated_size) + + offsetof(INDEX_ROOT, reserved)))) { + ntfs_log_error("Corrupt index root" + " in MFT record %lld.\n", + (long long)inum); + errno = EIO; + ret = -1; + } else if (ntfs_ie_stream_inconsistent(&ir->index, inum)) { + errno = EIO; + ret = -1; + } + break; + case AT_STANDARD_INFORMATION : + if (a->non_resident + || (le32_to_cpu(a->value_length) + < offsetof(STANDARD_INFORMATION, + v1_end))) { + ntfs_log_error("Corrupt standard information" + " in MFT record %lld\n", + (long long)inum); + errno = EIO; + ret = -1; + } + break; + case AT_OBJECT_ID : + if (a->non_resident + || (le32_to_cpu(a->value_length) + < sizeof(GUID))) { + ntfs_log_error("Corrupt object id" + " in MFT record %lld\n", + (long long)inum); + errno = EIO; + ret = -1; + } + break; + case AT_VOLUME_NAME : + case AT_EA_INFORMATION : + if (a->non_resident) { + ntfs_log_error("Attribute 0x%x in MFT record" + " %lld should be resident.\n", + (int)le32_to_cpu(a->type), + (long long)inum); + errno = EIO; + ret = -1; + } + break; + case AT_VOLUME_INFORMATION : + if (a->non_resident + || (le32_to_cpu(a->value_length) + < sizeof(VOLUME_INFORMATION))) { + ntfs_log_error("Corrupt volume information" + " in MFT record %lld\n", + (long long)inum); + errno = EIO; + ret = -1; + } + break; + case AT_INDEX_ALLOCATION : + if (!a->non_resident) { + ntfs_log_error("Corrupt index allocation" + " in MFT record %lld", + (long long)inum); + errno = EIO; + ret = -1; + } + break; + default : + break; + } + } + return (ret); +} + +/** + * ntfs_attr_lookup - find an attribute in an ntfs inode + * @type: attribute type to find + * @name: attribute name to find (optional, i.e. NULL means don't care) + * @name_len: attribute name length (only needed if @name present) + * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) + * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only) + * @val: attribute value to find (optional, resident attributes only) + * @val_len: attribute value length + * @ctx: search context with mft record and attribute to search from + * + * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must + * be the base mft record and @ctx must have been obtained from a call to + * ntfs_attr_get_search_ctx(). + * + * This function transparently handles attribute lists and @ctx is used to + * continue searches where they were left off at. + * + * If @type is AT_UNUSED, return the first found attribute, i.e. one can + * enumerate all attributes by setting @type to AT_UNUSED and then calling + * ntfs_attr_lookup() repeatedly until it returns -1 with errno set to ENOENT + * to indicate that there are no more entries. During the enumeration, each + * successful call of ntfs_attr_lookup() will return the next attribute, with + * the current attribute being described by the search context @ctx. + * + * If @type is AT_END, seek to the end of the base mft record ignoring the + * attribute list completely and return -1 with errno set to ENOENT. AT_END is + * not a valid attribute, its length is zero for example, thus it is safer to + * return error instead of success in this case. It should never be needed to + * do this, but we implement the functionality because it allows for simpler + * code inside ntfs_external_attr_find(). + * + * If @name is AT_UNNAMED search for an unnamed attribute. If @name is present + * but not AT_UNNAMED search for a named attribute matching @name. Otherwise, + * match both named and unnamed attributes. + * + * After finishing with the attribute/mft record you need to call + * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any + * mapped extent inodes, etc). + * + * Return 0 if the search was successful and -1 if not, with errno set to the + * error code. + * + * On success, @ctx->attr is the found attribute, it is in mft record + * @ctx->mrec, and @ctx->al_entry is the attribute list entry for this + * attribute with @ctx->base_* being the base mft record to which @ctx->attr + * belongs. If no attribute list attribute is present @ctx->al_entry and + * @ctx->base_* are NULL. + * + * On error ENOENT, i.e. attribute not found, @ctx->attr is set to the + * attribute which collates just after the attribute being searched for in the + * base ntfs inode, i.e. if one wants to add the attribute to the mft record + * this is the correct place to insert it into, and if there is not enough + * space, the attribute should be placed in an extent mft record. + * @ctx->al_entry points to the position within @ctx->base_ntfs_ino->attr_list + * at which the new attribute's attribute list entry should be inserted. The + * other @ctx fields, base_ntfs_ino, base_mrec, and base_attr are set to NULL. + * The only exception to this is when @type is AT_END, in which case + * @ctx->al_entry is set to NULL also (see above). + * + * + * The following error codes are defined: + * ENOENT Attribute not found, not an error as such. + * EINVAL Invalid arguments. + * EIO I/O error or corrupt data structures found. + * ENOMEM Not enough memory to allocate necessary buffers. + */ +int ntfs_attr_lookup(const ATTR_TYPES type, const ntfschar *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const VCN lowest_vcn, const u8 *val, const u32 val_len, + ntfs_attr_search_ctx *ctx) +{ + ntfs_volume *vol; + ntfs_inode *base_ni; + int ret = -1; + + ntfs_log_enter("Entering for attribute type 0x%x\n", le32_to_cpu(type)); + + if (!ctx || !ctx->mrec || !ctx->attr || (name && name != AT_UNNAMED && + (!ctx->ntfs_ino || !(vol = ctx->ntfs_ino->vol) || + !vol->upcase || !vol->upcase_len))) { + errno = EINVAL; + ntfs_log_perror("%s", __FUNCTION__); + goto out; + } + + if (ctx->base_ntfs_ino) + base_ni = ctx->base_ntfs_ino; + else + base_ni = ctx->ntfs_ino; + if (!base_ni || !NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST) + ret = ntfs_attr_find(type, name, name_len, ic, val, val_len, ctx); + else + ret = ntfs_external_attr_find(type, name, name_len, ic, + lowest_vcn, val, val_len, ctx); +out: + ntfs_log_leave("\n"); + return ret; +} + +/** + * ntfs_attr_position - find given or next attribute type in an ntfs inode + * @type: attribute type to start lookup + * @ctx: search context with mft record and attribute to search from + * + * Find an attribute type in an ntfs inode or the next attribute which is not + * the AT_END attribute. Please see more details at ntfs_attr_lookup. + * + * Return 0 if the search was successful and -1 if not, with errno set to the + * error code. + * + * The following error codes are defined: + * EINVAL Invalid arguments. + * EIO I/O error or corrupt data structures found. + * ENOMEM Not enough memory to allocate necessary buffers. + * ENOSPC No attribute was found after 'type', only AT_END. + */ +int ntfs_attr_position(const ATTR_TYPES type, ntfs_attr_search_ctx *ctx) +{ + if (ntfs_attr_lookup(type, NULL, 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { + if (errno != ENOENT) + return -1; + if (ctx->attr->type == AT_END) { + errno = ENOSPC; + return -1; + } + } + return 0; +} + +/** + * ntfs_attr_init_search_ctx - initialize an attribute search context + * @ctx: attribute search context to initialize + * @ni: ntfs inode with which to initialize the search context + * @mrec: mft record with which to initialize the search context + * + * Initialize the attribute search context @ctx with @ni and @mrec. + */ +static void ntfs_attr_init_search_ctx(ntfs_attr_search_ctx *ctx, + ntfs_inode *ni, MFT_RECORD *mrec) +{ + if (!mrec) + mrec = ni->mrec; + ctx->mrec = mrec; + /* Sanity checks are performed elsewhere. */ + ctx->attr = (ATTR_RECORD*)((u8*)mrec + le16_to_cpu(mrec->attrs_offset)); + ctx->is_first = TRUE; + ctx->ntfs_ino = ni; + ctx->al_entry = NULL; + ctx->base_ntfs_ino = NULL; + ctx->base_mrec = NULL; + ctx->base_attr = NULL; +} + +/** + * ntfs_attr_reinit_search_ctx - reinitialize an attribute search context + * @ctx: attribute search context to reinitialize + * + * Reinitialize the attribute search context @ctx. + * + * This is used when a search for a new attribute is being started to reset + * the search context to the beginning. + */ +void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx) +{ + if (!ctx->base_ntfs_ino) { + /* No attribute list. */ + ctx->is_first = TRUE; + /* Sanity checks are performed elsewhere. */ + ctx->attr = (ATTR_RECORD*)((u8*)ctx->mrec + + le16_to_cpu(ctx->mrec->attrs_offset)); + /* + * This needs resetting due to ntfs_external_attr_find() which + * can leave it set despite having zeroed ctx->base_ntfs_ino. + */ + ctx->al_entry = NULL; + return; + } /* Attribute list. */ + ntfs_attr_init_search_ctx(ctx, ctx->base_ntfs_ino, ctx->base_mrec); + return; +} + +/** + * ntfs_attr_get_search_ctx - allocate/initialize a new attribute search context + * @ni: ntfs inode with which to initialize the search context + * @mrec: mft record with which to initialize the search context + * + * Allocate a new attribute search context, initialize it with @ni and @mrec, + * and return it. Return NULL on error with errno set. + * + * @mrec can be NULL, in which case the mft record is taken from @ni. + * + * Note: For low level utilities which know what they are doing we allow @ni to + * be NULL and @mrec to be set. Do NOT do this unless you understand the + * implications!!! For example it is no longer safe to call ntfs_attr_lookup(). + */ +ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec) +{ + ntfs_attr_search_ctx *ctx; + + if (!ni && !mrec) { + errno = EINVAL; + ntfs_log_perror("NULL arguments"); + return NULL; + } + ctx = ntfs_malloc(sizeof(ntfs_attr_search_ctx)); + if (ctx) + ntfs_attr_init_search_ctx(ctx, ni, mrec); + return ctx; +} + +/** + * ntfs_attr_put_search_ctx - release an attribute search context + * @ctx: attribute search context to free + * + * Release the attribute search context @ctx. + */ +void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx) +{ + // NOTE: save errno if it could change and function stays void! + free(ctx); +} + +/** + * ntfs_attr_find_in_attrdef - find an attribute in the $AttrDef system file + * @vol: ntfs volume to which the attribute belongs + * @type: attribute type which to find + * + * Search for the attribute definition record corresponding to the attribute + * @type in the $AttrDef system file. + * + * Return the attribute type definition record if found and NULL if not found + * or an error occurred. On error the error code is stored in errno. The + * following error codes are defined: + * ENOENT - The attribute @type is not specified in $AttrDef. + * EINVAL - Invalid parameters (e.g. @vol is not valid). + */ +ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol, + const ATTR_TYPES type) +{ + ATTR_DEF *ad; + + if (!vol || !vol->attrdef || !type) { + errno = EINVAL; + ntfs_log_perror("%s: type=%d", __FUNCTION__, le32_to_cpu(type)); + return NULL; + } + for (ad = vol->attrdef; ((ptrdiff_t)((u8*)ad - (u8*)vol->attrdef + + sizeof(ATTR_DEF)) <= vol->attrdef_len) + && ad->type; ++ad) { + /* We haven't found it yet, carry on searching. */ + if (le32_to_cpu(ad->type) < le32_to_cpu(type)) + continue; + /* We found the attribute; return it. */ + if (ad->type == type) + return ad; + /* We have gone too far already. No point in continuing. */ + break; + } + errno = ENOENT; + ntfs_log_perror("%s: type=%d", __FUNCTION__, le32_to_cpu(type)); + return NULL; +} + +/** + * ntfs_attr_size_bounds_check - check a size of an attribute type for validity + * @vol: ntfs volume to which the attribute belongs + * @type: attribute type which to check + * @size: size which to check + * + * Check whether the @size in bytes is valid for an attribute of @type on the + * ntfs volume @vol. This information is obtained from $AttrDef system file. + * + * Return 0 if valid and -1 if not valid or an error occurred. On error the + * error code is stored in errno. The following error codes are defined: + * ERANGE - @size is not valid for the attribute @type. + * ENOENT - The attribute @type is not specified in $AttrDef. + * EINVAL - Invalid parameters (e.g. @size is < 0 or @vol is not valid). + */ +int ntfs_attr_size_bounds_check(const ntfs_volume *vol, const ATTR_TYPES type, + const s64 size) +{ + ATTR_DEF *ad; + s64 min_size, max_size; + + if (size < 0) { + errno = EINVAL; + ntfs_log_perror("%s: size=%lld", __FUNCTION__, + (long long)size); + return -1; + } + + /* + * $ATTRIBUTE_LIST shouldn't be greater than 0x40000, otherwise + * Windows would crash. This is not listed in the AttrDef. + */ + if (type == AT_ATTRIBUTE_LIST && size > 0x40000) { + errno = ERANGE; + ntfs_log_perror("Too large attrlist (%lld)", (long long)size); + return -1; + } + + ad = ntfs_attr_find_in_attrdef(vol, type); + if (!ad) + return -1; + + min_size = sle64_to_cpu(ad->min_size); + max_size = sle64_to_cpu(ad->max_size); + + /* The $AttrDef generated by Windows specifies 2 as min_size for the + * volume name attribute, but in reality Windows sets it to 0 when + * clearing the volume name. If we want to be able to clear the volume + * name we must also accept 0 as min_size, despite the $AttrDef + * definition. */ + if(type == AT_VOLUME_NAME) + min_size = 0; + + if ((min_size && (size < min_size)) || + ((max_size > 0) && (size > max_size))) { + errno = ERANGE; + ntfs_log_perror("Attr type %d size check failed (min,size,max=" + "%lld,%lld,%lld)", le32_to_cpu(type), (long long)min_size, + (long long)size, (long long)max_size); + return -1; + } + return 0; +} + +/** + * ntfs_attr_can_be_non_resident - check if an attribute can be non-resident + * @vol: ntfs volume to which the attribute belongs + * @type: attribute type to check + * @name: attribute name to check + * @name_len: attribute name length + * + * Check whether the attribute of @type and @name with name length @name_len on + * the ntfs volume @vol is allowed to be non-resident. This information is + * obtained from $AttrDef system file and is augmented by rules imposed by + * Microsoft (e.g. see http://support.microsoft.com/kb/974729/). + * + * Return 0 if the attribute is allowed to be non-resident and -1 if not or an + * error occurred. On error the error code is stored in errno. The following + * error codes are defined: + * EPERM - The attribute is not allowed to be non-resident. + * ENOENT - The attribute @type is not specified in $AttrDef. + * EINVAL - Invalid parameters (e.g. @vol is not valid). + */ +static int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, const ATTR_TYPES type, + const ntfschar *name, int name_len) +{ + ATTR_DEF *ad; + BOOL allowed; + + /* + * Microsoft has decreed that $LOGGED_UTILITY_STREAM attributes with a + * name of $TXF_DATA must be resident despite the entry for + * $LOGGED_UTILITY_STREAM in $AttrDef allowing them to be non-resident. + * Failure to obey this on the root directory mft record of a volume + * causes Windows Vista and later to see the volume as a RAW volume and + * thus cannot mount it at all. + */ + if ((type == AT_LOGGED_UTILITY_STREAM) + && name + && ntfs_names_are_equal(TXF_DATA, 9, name, name_len, + CASE_SENSITIVE, vol->upcase, vol->upcase_len)) + allowed = FALSE; + else { + /* Find the attribute definition record in $AttrDef. */ + ad = ntfs_attr_find_in_attrdef(vol, type); + if (!ad) + return -1; + /* Check the flags and return the result. */ + allowed = !(ad->flags & ATTR_DEF_RESIDENT); + } + if (!allowed) { + errno = EPERM; + ntfs_log_trace("Attribute can't be non-resident\n"); + return -1; + } + return 0; +} + +/** + * ntfs_attr_can_be_resident - check if an attribute can be resident + * @vol: ntfs volume to which the attribute belongs + * @type: attribute type which to check + * + * Check whether the attribute of @type on the ntfs volume @vol is allowed to + * be resident. This information is derived from our ntfs knowledge and may + * not be completely accurate, especially when user defined attributes are + * present. Basically we allow everything to be resident except for index + * allocation and extended attribute attributes. + * + * Return 0 if the attribute is allowed to be resident and -1 if not or an + * error occurred. On error the error code is stored in errno. The following + * error codes are defined: + * EPERM - The attribute is not allowed to be resident. + * EINVAL - Invalid parameters (e.g. @vol is not valid). + * + * Warning: In the system file $MFT the attribute $Bitmap must be non-resident + * otherwise windows will not boot (blue screen of death)! We cannot + * check for this here as we don't know which inode's $Bitmap is being + * asked about so the caller needs to special case this. + */ +int ntfs_attr_can_be_resident(const ntfs_volume *vol, const ATTR_TYPES type) +{ + if (!vol || !vol->attrdef || !type) { + errno = EINVAL; + return -1; + } + if (type != AT_INDEX_ALLOCATION) + return 0; + + ntfs_log_trace("Attribute can't be resident\n"); + errno = EPERM; + return -1; +} + +/** + * ntfs_make_room_for_attr - make room for an attribute inside an mft record + * @m: mft record + * @pos: position at which to make space + * @size: byte size to make available at this position + * + * @pos points to the attribute in front of which we want to make space. + * + * Return 0 on success or -1 on error. On error the error code is stored in + * errno. Possible error codes are: + * ENOSPC - There is not enough space available to complete operation. The + * caller has to make space before calling this. + * EINVAL - Input parameters were faulty. + */ +int ntfs_make_room_for_attr(MFT_RECORD *m, u8 *pos, u32 size) +{ + u32 biu; + + ntfs_log_trace("Entering for pos 0x%d, size %u.\n", + (int)(pos - (u8*)m), (unsigned) size); + + /* Make size 8-byte alignment. */ + size = (size + 7) & ~7; + + /* Rigorous consistency checks. */ + if (!m || !pos || pos < (u8*)m) { + errno = EINVAL; + ntfs_log_perror("%s: pos=%p m=%p", __FUNCTION__, pos, m); + return -1; + } + /* The -8 is for the attribute terminator. */ + if (pos - (u8*)m > (int)le32_to_cpu(m->bytes_in_use) - 8) { + errno = EINVAL; + return -1; + } + /* Nothing to do. */ + if (!size) + return 0; + + biu = le32_to_cpu(m->bytes_in_use); + /* Do we have enough space? */ + if (biu + size > le32_to_cpu(m->bytes_allocated) || + pos + size > (u8*)m + le32_to_cpu(m->bytes_allocated)) { + errno = ENOSPC; + ntfs_log_trace("No enough space in the MFT record\n"); + return -1; + } + /* Move everything after pos to pos + size. */ + memmove(pos + size, pos, biu - (pos - (u8*)m)); + /* Update mft record. */ + m->bytes_in_use = cpu_to_le32(biu + size); + return 0; +} + +/** + * ntfs_resident_attr_record_add - add resident attribute to inode + * @ni: opened ntfs inode to which MFT record add attribute + * @type: type of the new attribute + * @name: name of the new attribute + * @name_len: name length of the new attribute + * @val: value of the new attribute + * @size: size of new attribute (length of @val, if @val != NULL) + * @flags: flags of the new attribute + * + * Return offset to attribute from the beginning of the mft record on success + * and -1 on error. On error the error code is stored in errno. + * Possible error codes are: + * EINVAL - Invalid arguments passed to function. + * EEXIST - Attribute of such type and with same name already exists. + * EIO - I/O error occurred or damaged filesystem. + */ +int ntfs_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type, + const ntfschar *name, u8 name_len, const u8 *val, + u32 size, ATTR_FLAGS data_flags) +{ + ntfs_attr_search_ctx *ctx; + u32 length; + ATTR_RECORD *a; + MFT_RECORD *m; + int err, offset; + ntfs_inode *base_ni; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x, flags 0x%x.\n", + (long long) ni->mft_no, (unsigned) le32_to_cpu(type), (unsigned) le16_to_cpu(data_flags)); + + if (!ni || (!name && name_len)) { + errno = EINVAL; + return -1; + } + + if (ntfs_attr_can_be_resident(ni->vol, type)) { + if (errno == EPERM) + ntfs_log_trace("Attribute can't be resident.\n"); + else + ntfs_log_trace("ntfs_attr_can_be_resident failed.\n"); + return -1; + } + + /* Locate place where record should be. */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return -1; + /* + * Use ntfs_attr_find instead of ntfs_attr_lookup to find place for + * attribute in @ni->mrec, not any extent inode in case if @ni is base + * file record. + */ + if (!ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, val, size, + ctx)) { + err = EEXIST; + ntfs_log_trace("Attribute already present.\n"); + goto put_err_out; + } + if (errno != ENOENT) { + err = EIO; + goto put_err_out; + } + a = ctx->attr; + m = ctx->mrec; + + /* Make room for attribute. */ + length = offsetof(ATTR_RECORD, resident_end) + + ((name_len * sizeof(ntfschar) + 7) & ~7) + + ((size + 7) & ~7); + if (ntfs_make_room_for_attr(ctx->mrec, (u8*) ctx->attr, length)) { + err = errno; + ntfs_log_trace("Failed to make room for attribute.\n"); + goto put_err_out; + } + + /* Setup record fields. */ + offset = ((u8*)a - (u8*)m); + a->type = type; + a->length = cpu_to_le32(length); + a->non_resident = 0; + a->name_length = name_len; + a->name_offset = (name_len + ? const_cpu_to_le16(offsetof(ATTR_RECORD, resident_end)) + : const_cpu_to_le16(0)); + a->flags = data_flags; + a->instance = m->next_attr_instance; + a->value_length = cpu_to_le32(size); + a->value_offset = cpu_to_le16(length - ((size + 7) & ~7)); + if (val) + memcpy((u8*)a + le16_to_cpu(a->value_offset), val, size); + else + memset((u8*)a + le16_to_cpu(a->value_offset), 0, size); + if (type == AT_FILE_NAME) + a->resident_flags = RESIDENT_ATTR_IS_INDEXED; + else + a->resident_flags = 0; + if (name_len) + memcpy((u8*)a + le16_to_cpu(a->name_offset), + name, sizeof(ntfschar) * name_len); + m->next_attr_instance = + cpu_to_le16((le16_to_cpu(m->next_attr_instance) + 1) & 0xffff); + if (ni->nr_extents == -1) + base_ni = ni->base_ni; + else + base_ni = ni; + if (type != AT_ATTRIBUTE_LIST && NInoAttrList(base_ni)) { + if (ntfs_attrlist_entry_add(ni, a)) { + err = errno; + ntfs_attr_record_resize(m, a, 0); + ntfs_log_trace("Failed add attribute entry to " + "ATTRIBUTE_LIST.\n"); + goto put_err_out; + } + } + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY + ? type == AT_INDEX_ROOT && name == NTFS_INDEX_I30 + : type == AT_DATA && name == AT_UNNAMED) { + ni->data_size = size; + ni->allocated_size = (size + 7) & ~7; + set_nino_flag(ni,KnownSize); + } + ntfs_inode_mark_dirty(ni); + ntfs_attr_put_search_ctx(ctx); + return offset; +put_err_out: + ntfs_attr_put_search_ctx(ctx); + errno = err; + return -1; +} + +/** + * ntfs_non_resident_attr_record_add - add extent of non-resident attribute + * @ni: opened ntfs inode to which MFT record add attribute + * @type: type of the new attribute extent + * @name: name of the new attribute extent + * @name_len: name length of the new attribute extent + * @lowest_vcn: lowest vcn of the new attribute extent + * @dataruns_size: dataruns size of the new attribute extent + * @flags: flags of the new attribute extent + * + * Return offset to attribute from the beginning of the mft record on success + * and -1 on error. On error the error code is stored in errno. + * Possible error codes are: + * EINVAL - Invalid arguments passed to function. + * EEXIST - Attribute of such type, with same lowest vcn and with same + * name already exists. + * EIO - I/O error occurred or damaged filesystem. + */ +int ntfs_non_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type, + const ntfschar *name, u8 name_len, VCN lowest_vcn, int dataruns_size, + ATTR_FLAGS flags) +{ + ntfs_attr_search_ctx *ctx; + u32 length; + ATTR_RECORD *a; + MFT_RECORD *m; + ntfs_inode *base_ni; + int err, offset; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld, " + "dataruns_size %d, flags 0x%x.\n", + (long long) ni->mft_no, (unsigned) le32_to_cpu(type), + (long long) lowest_vcn, dataruns_size, (unsigned) le16_to_cpu(flags)); + + if (!ni || dataruns_size <= 0 || (!name && name_len)) { + errno = EINVAL; + return -1; + } + + if (ntfs_attr_can_be_non_resident(ni->vol, type, name, name_len)) { + if (errno == EPERM) + ntfs_log_perror("Attribute can't be non resident"); + else + ntfs_log_perror("ntfs_attr_can_be_non_resident failed"); + return -1; + } + + /* Locate place where record should be. */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return -1; + /* + * Use ntfs_attr_find instead of ntfs_attr_lookup to find place for + * attribute in @ni->mrec, not any extent inode in case if @ni is base + * file record. + */ + if (!ntfs_attr_find(type, name, name_len, CASE_SENSITIVE, NULL, 0, + ctx)) { + err = EEXIST; + ntfs_log_perror("Attribute 0x%x already present", le32_to_cpu(type)); + goto put_err_out; + } + if (errno != ENOENT) { + ntfs_log_perror("ntfs_attr_find failed"); + err = EIO; + goto put_err_out; + } + a = ctx->attr; + m = ctx->mrec; + + /* Make room for attribute. */ + dataruns_size = (dataruns_size + 7) & ~7; + length = offsetof(ATTR_RECORD, compressed_size) + ((sizeof(ntfschar) * + name_len + 7) & ~7) + dataruns_size + + ((flags & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE)) ? + sizeof(a->compressed_size) : 0); + if (ntfs_make_room_for_attr(ctx->mrec, (u8*) ctx->attr, length)) { + err = errno; + ntfs_log_perror("Failed to make room for attribute"); + goto put_err_out; + } + + /* Setup record fields. */ + a->type = type; + a->length = cpu_to_le32(length); + a->non_resident = 1; + a->name_length = name_len; + a->name_offset = cpu_to_le16(offsetof(ATTR_RECORD, compressed_size) + + ((flags & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE)) ? + sizeof(a->compressed_size) : 0)); + a->flags = flags; + a->instance = m->next_attr_instance; + a->lowest_vcn = cpu_to_sle64(lowest_vcn); + a->mapping_pairs_offset = cpu_to_le16(length - dataruns_size); + a->compression_unit = (flags & ATTR_IS_COMPRESSED) + ? STANDARD_COMPRESSION_UNIT : 0; + /* If @lowest_vcn == 0, than setup empty attribute. */ + if (!lowest_vcn) { + a->highest_vcn = const_cpu_to_sle64(-1); + a->allocated_size = const_cpu_to_sle64(0); + a->data_size = const_cpu_to_sle64(0); + a->initialized_size = const_cpu_to_sle64(0); + /* Set empty mapping pairs. */ + *((u8*)a + le16_to_cpu(a->mapping_pairs_offset)) = 0; + } + if (name_len) + memcpy((u8*)a + le16_to_cpu(a->name_offset), + name, sizeof(ntfschar) * name_len); + m->next_attr_instance = + cpu_to_le16((le16_to_cpu(m->next_attr_instance) + 1) & 0xffff); + if (ni->nr_extents == -1) + base_ni = ni->base_ni; + else + base_ni = ni; + if (type != AT_ATTRIBUTE_LIST && NInoAttrList(base_ni)) { + if (ntfs_attrlist_entry_add(ni, a)) { + err = errno; + ntfs_log_perror("Failed add attr entry to attrlist"); + ntfs_attr_record_resize(m, a, 0); + goto put_err_out; + } + } + ntfs_inode_mark_dirty(ni); + /* + * Locate offset from start of the MFT record where new attribute is + * placed. We need relookup it, because record maybe moved during + * update of attribute list. + */ + ntfs_attr_reinit_search_ctx(ctx); + if (ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE, + lowest_vcn, NULL, 0, ctx)) { + ntfs_log_perror("%s: attribute lookup failed", __FUNCTION__); + ntfs_attr_put_search_ctx(ctx); + return -1; + + } + offset = (u8*)ctx->attr - (u8*)ctx->mrec; + ntfs_attr_put_search_ctx(ctx); + return offset; +put_err_out: + ntfs_attr_put_search_ctx(ctx); + errno = err; + return -1; +} + +/** + * ntfs_attr_record_rm - remove attribute extent + * @ctx: search context describing the attribute which should be removed + * + * If this function succeed, user should reinit search context if he/she wants + * use it anymore. + * + * Return 0 on success and -1 on error. On error the error code is stored in + * errno. Possible error codes are: + * EINVAL - Invalid arguments passed to function. + * EIO - I/O error occurred or damaged filesystem. + */ +int ntfs_attr_record_rm(ntfs_attr_search_ctx *ctx) +{ + ntfs_inode *base_ni, *ni; + ATTR_TYPES type; + + if (!ctx || !ctx->ntfs_ino || !ctx->mrec || !ctx->attr) { + errno = EINVAL; + return -1; + } + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x.\n", + (long long) ctx->ntfs_ino->mft_no, + (unsigned) le32_to_cpu(ctx->attr->type)); + type = ctx->attr->type; + ni = ctx->ntfs_ino; + if (ctx->base_ntfs_ino) + base_ni = ctx->base_ntfs_ino; + else + base_ni = ctx->ntfs_ino; + + /* Remove attribute itself. */ + if (ntfs_attr_record_resize(ctx->mrec, ctx->attr, 0)) { + ntfs_log_trace("Couldn't remove attribute record. Bug or damaged MFT " + "record.\n"); + if (NInoAttrList(base_ni) && type != AT_ATTRIBUTE_LIST) + if (ntfs_attrlist_entry_add(ni, ctx->attr)) + ntfs_log_trace("Rollback failed. Leaving inconstant " + "metadata.\n"); + errno = EIO; + return -1; + } + ntfs_inode_mark_dirty(ni); + + /* + * Remove record from $ATTRIBUTE_LIST if present and we don't want + * delete $ATTRIBUTE_LIST itself. + */ + if (NInoAttrList(base_ni) && type != AT_ATTRIBUTE_LIST) { + if (ntfs_attrlist_entry_rm(ctx)) { + ntfs_log_trace("Couldn't delete record from " + "$ATTRIBUTE_LIST.\n"); + return -1; + } + } + + /* Post $ATTRIBUTE_LIST delete setup. */ + if (type == AT_ATTRIBUTE_LIST) { + if (NInoAttrList(base_ni) && base_ni->attr_list) + free(base_ni->attr_list); + base_ni->attr_list = NULL; + NInoClearAttrList(base_ni); + NInoAttrListClearDirty(base_ni); + } + + /* Free MFT record, if it doesn't contain attributes. */ + if (le32_to_cpu(ctx->mrec->bytes_in_use) - + le16_to_cpu(ctx->mrec->attrs_offset) == 8) { + if (ntfs_mft_record_free(ni->vol, ni)) { + // FIXME: We need rollback here. + ntfs_log_trace("Couldn't free MFT record.\n"); + errno = EIO; + return -1; + } + /* Remove done if we freed base inode. */ + if (ni == base_ni) + return 0; + } + + if (type == AT_ATTRIBUTE_LIST || !NInoAttrList(base_ni)) + return 0; + + /* Remove attribute list if we don't need it any more. */ + if (!ntfs_attrlist_need(base_ni)) { + ntfs_attr_reinit_search_ctx(ctx); + if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + /* + * FIXME: Should we succeed here? Definitely something + * goes wrong because NInoAttrList(base_ni) returned + * that we have got attribute list. + */ + ntfs_log_trace("Couldn't find attribute list. Succeed " + "anyway.\n"); + return 0; + } + /* Deallocate clusters. */ + if (ctx->attr->non_resident) { + runlist *al_rl; + + al_rl = ntfs_mapping_pairs_decompress(base_ni->vol, + ctx->attr, NULL); + if (!al_rl) { + ntfs_log_trace("Couldn't decompress attribute list " + "runlist. Succeed anyway.\n"); + return 0; + } + if (ntfs_cluster_free_from_rl(base_ni->vol, al_rl)) { + ntfs_log_trace("Leaking clusters! Run chkdsk. " + "Couldn't free clusters from " + "attribute list runlist.\n"); + } + free(al_rl); + } + /* Remove attribute record itself. */ + if (ntfs_attr_record_rm(ctx)) { + /* + * FIXME: Should we succeed here? BTW, chkdsk doesn't + * complain if it find MFT record with attribute list, + * but without extents. + */ + ntfs_log_trace("Couldn't remove attribute list. Succeed " + "anyway.\n"); + return 0; + } + } + return 0; +} + +/** + * ntfs_attr_add - add attribute to inode + * @ni: opened ntfs inode to which add attribute + * @type: type of the new attribute + * @name: name in unicode of the new attribute + * @name_len: name length in unicode characters of the new attribute + * @val: value of new attribute + * @size: size of the new attribute / length of @val (if specified) + * + * @val should always be specified for always resident attributes (eg. FILE_NAME + * attribute), for attributes that can become non-resident @val can be NULL + * (eg. DATA attribute). @size can be specified even if @val is NULL, in this + * case data size will be equal to @size and initialized size will be equal + * to 0. + * + * If inode haven't got enough space to add attribute, add attribute to one of + * it extents, if no extents present or no one of them have enough space, than + * allocate new extent and add attribute to it. + * + * If on one of this steps attribute list is needed but not present, than it is + * added transparently to caller. So, this function should not be called with + * @type == AT_ATTRIBUTE_LIST, if you really need to add attribute list call + * ntfs_inode_add_attrlist instead. + * + * On success return 0. On error return -1 with errno set to the error code. + */ +int ntfs_attr_add(ntfs_inode *ni, ATTR_TYPES type, + ntfschar *name, u8 name_len, const u8 *val, s64 size) +{ + u32 attr_rec_size; + int err, i, offset; + BOOL is_resident; + BOOL can_be_non_resident = FALSE; + ntfs_inode *attr_ni; + ntfs_attr *na; + ATTR_FLAGS data_flags; + + if (!ni || size < 0 || type == AT_ATTRIBUTE_LIST) { + errno = EINVAL; + ntfs_log_perror("%s: ni=%p size=%lld", __FUNCTION__, ni, + (long long)size); + return -1; + } + + ntfs_log_trace("Entering for inode %lld, attr %x, size %lld.\n", + (long long)ni->mft_no, le32_to_cpu(type), (long long)size); + + if (ni->nr_extents == -1) + ni = ni->base_ni; + + /* Check the attribute type and the size. */ + if (ntfs_attr_size_bounds_check(ni->vol, type, size)) { + if (errno == ENOENT) + errno = EIO; + return -1; + } + + /* Sanity checks for always resident attributes. */ + if (ntfs_attr_can_be_non_resident(ni->vol, type, name, name_len)) { + if (errno != EPERM) { + err = errno; + ntfs_log_perror("ntfs_attr_can_be_non_resident failed"); + goto err_out; + } + /* @val is mandatory. */ + if (!val) { + errno = EINVAL; + ntfs_log_perror("val is mandatory for always resident " + "attributes"); + return -1; + } + if (size > ni->vol->mft_record_size) { + errno = ERANGE; + ntfs_log_perror("Attribute is too big"); + return -1; + } + } else + can_be_non_resident = TRUE; + + /* + * Determine resident or not will be new attribute. We add 8 to size in + * non resident case for mapping pairs. + */ + if (!ntfs_attr_can_be_resident(ni->vol, type)) { + is_resident = TRUE; + } else { + if (errno != EPERM) { + err = errno; + ntfs_log_perror("ntfs_attr_can_be_resident failed"); + goto err_out; + } + is_resident = FALSE; + } + /* Calculate attribute record size. */ + if (is_resident) + attr_rec_size = offsetof(ATTR_RECORD, resident_end) + + ((name_len * sizeof(ntfschar) + 7) & ~7) + + ((size + 7) & ~7); + else + attr_rec_size = offsetof(ATTR_RECORD, non_resident_end) + + ((name_len * sizeof(ntfschar) + 7) & ~7) + 8; + + /* + * If we have enough free space for the new attribute in the base MFT + * record, then add attribute to it. + */ + if (le32_to_cpu(ni->mrec->bytes_allocated) - + le32_to_cpu(ni->mrec->bytes_in_use) >= attr_rec_size) { + attr_ni = ni; + goto add_attr_record; + } + + /* Try to add to extent inodes. */ + if (ntfs_inode_attach_all_extents(ni)) { + err = errno; + ntfs_log_perror("Failed to attach all extents to inode"); + goto err_out; + } + for (i = 0; i < ni->nr_extents; i++) { + attr_ni = ni->extent_nis[i]; + if (le32_to_cpu(attr_ni->mrec->bytes_allocated) - + le32_to_cpu(attr_ni->mrec->bytes_in_use) >= + attr_rec_size) + goto add_attr_record; + } + + /* There is no extent that contain enough space for new attribute. */ + if (!NInoAttrList(ni)) { + /* Add attribute list not present, add it and retry. */ + if (ntfs_inode_add_attrlist(ni)) { + err = errno; + ntfs_log_perror("Failed to add attribute list"); + goto err_out; + } + return ntfs_attr_add(ni, type, name, name_len, val, size); + } + /* Allocate new extent. */ + attr_ni = ntfs_mft_record_alloc(ni->vol, ni); + if (!attr_ni) { + err = errno; + ntfs_log_perror("Failed to allocate extent record"); + goto err_out; + } + +add_attr_record: + if ((ni->flags & FILE_ATTR_COMPRESSED) + && (ni->vol->major_ver >= 3) + && NVolCompression(ni->vol) + && (ni->vol->cluster_size <= MAX_COMPRESSION_CLUSTER_SIZE) + && ((type == AT_DATA) + || ((type == AT_INDEX_ROOT) && (name == NTFS_INDEX_I30)))) + data_flags = ATTR_IS_COMPRESSED; + else + data_flags = const_cpu_to_le16(0); + if (is_resident) { + /* Add resident attribute. */ + offset = ntfs_resident_attr_record_add(attr_ni, type, name, + name_len, val, size, data_flags); + if (offset < 0) { + if (errno == ENOSPC && can_be_non_resident) + goto add_non_resident; + err = errno; + ntfs_log_perror("Failed to add resident attribute"); + goto free_err_out; + } + return 0; + } + +add_non_resident: + /* Add non resident attribute. */ + offset = ntfs_non_resident_attr_record_add(attr_ni, type, name, + name_len, 0, 8, data_flags); + if (offset < 0) { + err = errno; + ntfs_log_perror("Failed to add non resident attribute"); + goto free_err_out; + } + + /* If @size == 0, we are done. */ + if (!size) + return 0; + + /* Open new attribute and resize it. */ + na = ntfs_attr_open(ni, type, name, name_len); + if (!na) { + err = errno; + ntfs_log_perror("Failed to open just added attribute"); + goto rm_attr_err_out; + } + /* Resize and set attribute value. */ + if (ntfs_attr_truncate_i(na, size, HOLES_OK) || + (val && (ntfs_attr_pwrite(na, 0, size, val) != size))) { + err = errno; + ntfs_log_perror("Failed to initialize just added attribute"); + if (ntfs_attr_rm(na)) + ntfs_log_perror("Failed to remove just added attribute"); + ntfs_attr_close(na); + goto err_out; + } + ntfs_attr_close(na); + return 0; + +rm_attr_err_out: + /* Remove just added attribute. */ + if (ntfs_attr_record_resize(attr_ni->mrec, + (ATTR_RECORD*)((u8*)attr_ni->mrec + offset), 0)) + ntfs_log_perror("Failed to remove just added attribute #2"); +free_err_out: + /* Free MFT record, if it doesn't contain attributes. */ + if (le32_to_cpu(attr_ni->mrec->bytes_in_use) - + le16_to_cpu(attr_ni->mrec->attrs_offset) == 8) + if (ntfs_mft_record_free(attr_ni->vol, attr_ni)) + ntfs_log_perror("Failed to free MFT record"); +err_out: + errno = err; + return -1; +} + +/* + * Change an attribute flag + */ + +int ntfs_attr_set_flags(ntfs_inode *ni, ATTR_TYPES type, const ntfschar *name, + u8 name_len, ATTR_FLAGS flags, ATTR_FLAGS mask) +{ + ntfs_attr_search_ctx *ctx; + int res; + + res = -1; + /* Search for designated attribute */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (ctx) { + if (!ntfs_attr_lookup(type, name, name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + /* do the requested change (all small endian le16) */ + ctx->attr->flags = (ctx->attr->flags & ~mask) + | (flags & mask); + NInoSetDirty(ni); + res = 0; + } + ntfs_attr_put_search_ctx(ctx); + } + return (res); +} + + +/** + * ntfs_attr_rm - remove attribute from ntfs inode + * @na: opened ntfs attribute to delete + * + * Remove attribute and all it's extents from ntfs inode. If attribute was non + * resident also free all clusters allocated by attribute. + * + * Return 0 on success or -1 on error with errno set to the error code. + */ +int ntfs_attr_rm(ntfs_attr *na) +{ + ntfs_attr_search_ctx *ctx; + int ret = 0; + + if (!na) { + ntfs_log_trace("Invalid arguments passed.\n"); + errno = EINVAL; + return -1; + } + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x.\n", + (long long) na->ni->mft_no, le32_to_cpu(na->type)); + + /* Free cluster allocation. */ + if (NAttrNonResident(na)) { + if (ntfs_attr_map_whole_runlist(na)) + return -1; + if (ntfs_cluster_free(na->ni->vol, na, 0, -1) < 0) { + ntfs_log_trace("Failed to free cluster allocation. Leaving " + "inconstant metadata.\n"); + ret = -1; + } + } + + /* Search for attribute extents and remove them all. */ + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + return -1; + while (!ntfs_attr_lookup(na->type, na->name, na->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + if (ntfs_attr_record_rm(ctx)) { + ntfs_log_trace("Failed to remove attribute extent. Leaving " + "inconstant metadata.\n"); + ret = -1; + } + ntfs_attr_reinit_search_ctx(ctx); + } + ntfs_attr_put_search_ctx(ctx); + if (errno != ENOENT) { + ntfs_log_trace("Attribute lookup failed. Probably leaving inconstant " + "metadata.\n"); + ret = -1; + } + + return ret; +} + +/** + * ntfs_attr_record_resize - resize an attribute record + * @m: mft record containing attribute record + * @a: attribute record to resize + * @new_size: new size in bytes to which to resize the attribute record @a + * + * Resize the attribute record @a, i.e. the resident part of the attribute, in + * the mft record @m to @new_size bytes. + * + * Return 0 on success and -1 on error with errno set to the error code. + * The following error codes are defined: + * ENOSPC - Not enough space in the mft record @m to perform the resize. + * Note that on error no modifications have been performed whatsoever. + * + * Warning: If you make a record smaller without having copied all the data you + * are interested in the data may be overwritten! + */ +int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size) +{ + u32 old_size, alloc_size, attr_size; + + old_size = le32_to_cpu(m->bytes_in_use); + alloc_size = le32_to_cpu(m->bytes_allocated); + attr_size = le32_to_cpu(a->length); + + ntfs_log_trace("Sizes: old=%u alloc=%u attr=%u new=%u\n", + (unsigned)old_size, (unsigned)alloc_size, + (unsigned)attr_size, (unsigned)new_size); + + /* Align to 8 bytes, just in case the caller hasn't. */ + new_size = (new_size + 7) & ~7; + + /* If the actual attribute length has changed, move things around. */ + if (new_size != attr_size) { + + u32 new_muse = old_size - attr_size + new_size; + + /* Not enough space in this mft record. */ + if (new_muse > alloc_size) { + errno = ENOSPC; + ntfs_log_trace("Not enough space in the MFT record " + "(%u > %u)\n", new_muse, alloc_size); + return -1; + } + + if (a->type == AT_INDEX_ROOT && new_size > attr_size && + new_muse + 120 > alloc_size && old_size + 120 <= alloc_size) { + errno = ENOSPC; + ntfs_log_trace("Too big INDEX_ROOT (%u > %u)\n", + new_muse, alloc_size); + return STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT; + } + + /* Move attributes following @a to their new location. */ + if (((u8 *)m + old_size) < ((u8 *)a + attr_size)) { + ntfs_log_error("Attribute 0x%x overflows" + " from MFT record\n", + (int)le32_to_cpu(a->type)); + errno = EIO; + return (-1); + } + memmove((u8 *)a + new_size, (u8 *)a + attr_size, + old_size - ((u8 *)a - (u8 *)m) - attr_size); + + /* Adjust @m to reflect the change in used space. */ + m->bytes_in_use = cpu_to_le32(new_muse); + + /* Adjust @a to reflect the new size. */ + if (new_size >= offsetof(ATTR_REC, length) + sizeof(a->length)) + a->length = cpu_to_le32(new_size); + } + return 0; +} + +/** + * ntfs_resident_attr_value_resize - resize the value of a resident attribute + * @m: mft record containing attribute record + * @a: attribute record whose value to resize + * @new_size: new size in bytes to which to resize the attribute value of @a + * + * Resize the value of the attribute @a in the mft record @m to @new_size bytes. + * If the value is made bigger, the newly "allocated" space is cleared. + * + * Return 0 on success and -1 on error with errno set to the error code. + * The following error codes are defined: + * ENOSPC - Not enough space in the mft record @m to perform the resize. + * Note that on error no modifications have been performed whatsoever. + */ +int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a, + const u32 new_size) +{ + int ret; + + ntfs_log_trace("Entering for new size %u.\n", (unsigned)new_size); + + if (!a->value_length) { + /* Offset is unsafe when no value */ + int offset = ((offsetof(ATTR_RECORD, resident_end) + + a->name_length*sizeof(ntfschar) - 1) | 7) + 1; + a->value_offset = cpu_to_le16(offset); + } + + /* Resize the resident part of the attribute record. */ + if ((ret = ntfs_attr_record_resize(m, a, (le16_to_cpu(a->value_offset) + + new_size + 7) & ~7)) < 0) + return ret; + /* + * If we made the attribute value bigger, clear the area between the + * old size and @new_size. + */ + if (new_size > le32_to_cpu(a->value_length)) + memset((u8*)a + le16_to_cpu(a->value_offset) + + le32_to_cpu(a->value_length), 0, new_size - + le32_to_cpu(a->value_length)); + /* Finally update the length of the attribute value. */ + a->value_length = cpu_to_le32(new_size); + return 0; +} + +/** + * ntfs_attr_record_move_to - move attribute record to target inode + * @ctx: attribute search context describing the attribute record + * @ni: opened ntfs inode to which move attribute record + * + * If this function succeed, user should reinit search context if he/she wants + * use it anymore. + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +int ntfs_attr_record_move_to(ntfs_attr_search_ctx *ctx, ntfs_inode *ni) +{ + ntfs_attr_search_ctx *nctx; + ATTR_RECORD *a; + int err; + + if (!ctx || !ctx->attr || !ctx->ntfs_ino || !ni) { + ntfs_log_trace("Invalid arguments passed.\n"); + errno = EINVAL; + return -1; + } + + ntfs_log_trace("Entering for ctx->attr->type 0x%x, ctx->ntfs_ino->mft_no " + "0x%llx, ni->mft_no 0x%llx.\n", + (unsigned) le32_to_cpu(ctx->attr->type), + (long long) ctx->ntfs_ino->mft_no, + (long long) ni->mft_no); + + if (ctx->ntfs_ino == ni) + return 0; + + if (!ctx->al_entry) { + ntfs_log_trace("Inode should contain attribute list to use this " + "function.\n"); + errno = EINVAL; + return -1; + } + + /* Find place in MFT record where attribute will be moved. */ + a = ctx->attr; + nctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!nctx) + return -1; + + /* + * Use ntfs_attr_find instead of ntfs_attr_lookup to find place for + * attribute in @ni->mrec, not any extent inode in case if @ni is base + * file record. + */ + if (!ntfs_attr_find(a->type, (ntfschar*)((u8*)a + le16_to_cpu( + a->name_offset)), a->name_length, CASE_SENSITIVE, NULL, + 0, nctx)) { + ntfs_log_trace("Attribute of such type, with same name already " + "present in this MFT record.\n"); + err = EEXIST; + goto put_err_out; + } + if (errno != ENOENT) { + err = errno; + ntfs_log_debug("Attribute lookup failed.\n"); + goto put_err_out; + } + + /* Make space and move attribute. */ + if (ntfs_make_room_for_attr(ni->mrec, (u8*) nctx->attr, + le32_to_cpu(a->length))) { + err = errno; + ntfs_log_trace("Couldn't make space for attribute.\n"); + goto put_err_out; + } + memcpy(nctx->attr, a, le32_to_cpu(a->length)); + nctx->attr->instance = nctx->mrec->next_attr_instance; + nctx->mrec->next_attr_instance = cpu_to_le16( + (le16_to_cpu(nctx->mrec->next_attr_instance) + 1) & 0xffff); + ntfs_attr_record_resize(ctx->mrec, a, 0); + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_inode_mark_dirty(ni); + + /* Update attribute list. */ + ctx->al_entry->mft_reference = + MK_LE_MREF(ni->mft_no, le16_to_cpu(ni->mrec->sequence_number)); + ctx->al_entry->instance = nctx->attr->instance; + ntfs_attrlist_mark_dirty(ni); + + ntfs_attr_put_search_ctx(nctx); + return 0; +put_err_out: + ntfs_attr_put_search_ctx(nctx); + errno = err; + return -1; +} + +/** + * ntfs_attr_record_move_away - move away attribute record from it's mft record + * @ctx: attribute search context describing the attribute record + * @extra: minimum amount of free space in the new holder of record + * + * New attribute record holder must have free @extra bytes after moving + * attribute record to it. + * + * If this function succeed, user should reinit search context if he/she wants + * use it anymore. + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +int ntfs_attr_record_move_away(ntfs_attr_search_ctx *ctx, int extra) +{ + ntfs_inode *base_ni, *ni; + MFT_RECORD *m; + int i; + + if (!ctx || !ctx->attr || !ctx->ntfs_ino || extra < 0) { + errno = EINVAL; + ntfs_log_perror("%s: ctx=%p ctx->attr=%p extra=%d", __FUNCTION__, + ctx, ctx ? ctx->attr : NULL, extra); + return -1; + } + + ntfs_log_trace("Entering for attr 0x%x, inode %llu\n", + (unsigned) le32_to_cpu(ctx->attr->type), + (unsigned long long)ctx->ntfs_ino->mft_no); + + if (ctx->ntfs_ino->nr_extents == -1) + base_ni = ctx->base_ntfs_ino; + else + base_ni = ctx->ntfs_ino; + + if (!NInoAttrList(base_ni)) { + errno = EINVAL; + ntfs_log_perror("Inode %llu has no attrlist", + (unsigned long long)base_ni->mft_no); + return -1; + } + + if (ntfs_inode_attach_all_extents(ctx->ntfs_ino)) { + ntfs_log_perror("Couldn't attach extents, inode=%llu", + (unsigned long long)base_ni->mft_no); + return -1; + } + + /* Walk through all extents and try to move attribute to them. */ + for (i = 0; i < base_ni->nr_extents; i++) { + ni = base_ni->extent_nis[i]; + m = ni->mrec; + + if (ctx->ntfs_ino->mft_no == ni->mft_no) + continue; + + if (le32_to_cpu(m->bytes_allocated) - + le32_to_cpu(m->bytes_in_use) < + le32_to_cpu(ctx->attr->length) + extra) + continue; + + /* + * ntfs_attr_record_move_to can fail if extent with other lowest + * VCN already present in inode we trying move record to. So, + * do not return error. + */ + if (!ntfs_attr_record_move_to(ctx, ni)) + return 0; + } + + /* + * Failed to move attribute to one of the current extents, so allocate + * new extent and move attribute to it. + */ + ni = ntfs_mft_record_alloc(base_ni->vol, base_ni); + if (!ni) { + ntfs_log_perror("Couldn't allocate MFT record"); + return -1; + } + if (ntfs_attr_record_move_to(ctx, ni)) { + ntfs_log_perror("Couldn't move attribute to MFT record"); + return -1; + } + return 0; +} + +/** + * ntfs_attr_make_non_resident - convert a resident to a non-resident attribute + * @na: open ntfs attribute to make non-resident + * @ctx: ntfs search context describing the attribute + * + * Convert a resident ntfs attribute to a non-resident one. + * + * Return 0 on success and -1 on error with errno set to the error code. The + * following error codes are defined: + * EPERM - The attribute is not allowed to be non-resident. + * TODO: others... + * + * NOTE to self: No changes in the attribute list are required to move from + * a resident to a non-resident attribute. + * + * Warning: We do not set the inode dirty and we do not write out anything! + * We expect the caller to do this as this is a fairly low level + * function and it is likely there will be further changes made. + */ +int ntfs_attr_make_non_resident(ntfs_attr *na, + ntfs_attr_search_ctx *ctx) +{ + s64 new_allocated_size, bw; + ntfs_volume *vol = na->ni->vol; + ATTR_REC *a = ctx->attr; + runlist *rl; + int mp_size, mp_ofs, name_ofs, arec_size, err; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x.\n", (unsigned long + long)na->ni->mft_no, le32_to_cpu(na->type)); + + /* Some preliminary sanity checking. */ + if (NAttrNonResident(na)) { + ntfs_log_trace("Eeek! Trying to make non-resident attribute " + "non-resident. Aborting...\n"); + errno = EINVAL; + return -1; + } + + /* Check that the attribute is allowed to be non-resident. */ + if (ntfs_attr_can_be_non_resident(vol, na->type, na->name, na->name_len)) + return -1; + + new_allocated_size = (le32_to_cpu(a->value_length) + vol->cluster_size + - 1) & ~(vol->cluster_size - 1); + + if (new_allocated_size > 0) { + if ((a->flags & ATTR_COMPRESSION_MASK) + == ATTR_IS_COMPRESSED) { + /* must allocate full compression blocks */ + new_allocated_size = ((new_allocated_size - 1) + | ((1L << (STANDARD_COMPRESSION_UNIT + + vol->cluster_size_bits)) - 1)) + 1; + } + /* Start by allocating clusters to hold the attribute value. */ + rl = ntfs_cluster_alloc(vol, 0, new_allocated_size >> + vol->cluster_size_bits, -1, DATA_ZONE); + if (!rl) + return -1; + } else + rl = NULL; + /* + * Setup the in-memory attribute structure to be non-resident so that + * we can use ntfs_attr_pwrite(). + */ + NAttrSetNonResident(na); + NAttrSetBeingNonResident(na); + na->rl = rl; + na->allocated_size = new_allocated_size; + na->data_size = na->initialized_size = le32_to_cpu(a->value_length); + /* + * FIXME: For now just clear all of these as we don't support them when + * writing. + */ + NAttrClearSparse(na); + NAttrClearEncrypted(na); + if ((a->flags & ATTR_COMPRESSION_MASK) == ATTR_IS_COMPRESSED) { + /* set compression writing parameters */ + na->compression_block_size + = 1 << (STANDARD_COMPRESSION_UNIT + vol->cluster_size_bits); + na->compression_block_clusters = 1 << STANDARD_COMPRESSION_UNIT; + } + + if (rl) { + /* Now copy the attribute value to the allocated cluster(s). */ + bw = ntfs_attr_pwrite(na, 0, le32_to_cpu(a->value_length), + (u8*)a + le16_to_cpu(a->value_offset)); + if (bw != le32_to_cpu(a->value_length)) { + err = errno; + ntfs_log_debug("Eeek! Failed to write out attribute value " + "(bw = %lli, errno = %i). " + "Aborting...\n", (long long)bw, err); + if (bw >= 0) + err = EIO; + goto cluster_free_err_out; + } + } + /* Determine the size of the mapping pairs array. */ + mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, INT_MAX); + if (mp_size < 0) { + err = errno; + ntfs_log_debug("Eeek! Failed to get size for mapping pairs array. " + "Aborting...\n"); + goto cluster_free_err_out; + } + /* Calculate new offsets for the name and the mapping pairs array. */ + if (na->ni->flags & FILE_ATTR_COMPRESSED) + name_ofs = (sizeof(ATTR_REC) + 7) & ~7; + else + name_ofs = (sizeof(ATTR_REC) - sizeof(a->compressed_size) + 7) & ~7; + mp_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7; + /* + * Determine the size of the resident part of the non-resident + * attribute record. (Not compressed thus no compressed_size element + * present.) + */ + arec_size = (mp_ofs + mp_size + 7) & ~7; + + /* Resize the resident part of the attribute record. */ + if (ntfs_attr_record_resize(ctx->mrec, a, arec_size) < 0) { + err = errno; + goto cluster_free_err_out; + } + + /* + * Convert the resident part of the attribute record to describe a + * non-resident attribute. + */ + a->non_resident = 1; + + /* Move the attribute name if it exists and update the offset. */ + if (a->name_length) + memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset), + a->name_length * sizeof(ntfschar)); + a->name_offset = cpu_to_le16(name_ofs); + + /* Setup the fields specific to non-resident attributes. */ + a->lowest_vcn = const_cpu_to_sle64(0); + a->highest_vcn = cpu_to_sle64((new_allocated_size - 1) >> + vol->cluster_size_bits); + + a->mapping_pairs_offset = cpu_to_le16(mp_ofs); + + /* + * Update the flags to match the in-memory ones. + * However cannot change the compression state if we had + * a fuse_file_info open with a mark for release. + * The decisions about compression can only be made when + * creating/recreating the stream, not when making non resident. + */ + a->flags &= ~(ATTR_IS_SPARSE | ATTR_IS_ENCRYPTED); + if ((a->flags & ATTR_COMPRESSION_MASK) == ATTR_IS_COMPRESSED) { + /* support only ATTR_IS_COMPRESSED compression mode */ + a->compression_unit = STANDARD_COMPRESSION_UNIT; + a->compressed_size = const_cpu_to_sle64(0); + } else { + a->compression_unit = 0; + a->flags &= ~ATTR_COMPRESSION_MASK; + na->data_flags = a->flags; + } + + memset(&a->reserved1, 0, sizeof(a->reserved1)); + + a->allocated_size = cpu_to_sle64(new_allocated_size); + a->data_size = a->initialized_size = cpu_to_sle64(na->data_size); + + /* Generate the mapping pairs array in the attribute record. */ + if (ntfs_mapping_pairs_build(vol, (u8*)a + mp_ofs, arec_size - mp_ofs, + rl, 0, NULL) < 0) { + // FIXME: Eeek! We need rollback! (AIA) + ntfs_log_trace("Eeek! Failed to build mapping pairs. Leaving " + "corrupt attribute record on disk. In memory " + "runlist is still intact! Error code is %i. " + "FIXME: Need to rollback instead!\n", errno); + return -1; + } + + /* Done! */ + return 0; + +cluster_free_err_out: + if (rl && ntfs_cluster_free(vol, na, 0, -1) < 0) + ntfs_log_trace("Eeek! Failed to release allocated clusters in error " + "code path. Leaving inconsistent metadata...\n"); + NAttrClearNonResident(na); + NAttrClearFullyMapped(na); + na->allocated_size = na->data_size; + na->rl = NULL; + free(rl); + errno = err; + return -1; +} + + +static int ntfs_resident_attr_resize(ntfs_attr *na, const s64 newsize); + +/** + * ntfs_resident_attr_resize - resize a resident, open ntfs attribute + * @na: resident ntfs attribute to resize + * @newsize: new size (in bytes) to which to resize the attribute + * + * Change the size of a resident, open ntfs attribute @na to @newsize bytes. + * Can also be used to force an attribute non-resident. In this case, the + * size cannot be changed. + * + * On success return 0 + * On error return values are: + * STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT + * STATUS_ERROR - otherwise + * The following error codes are defined: + * ENOMEM - Not enough memory to complete operation. + * ERANGE - @newsize is not valid for the attribute type of @na. + * ENOSPC - There is no enough space in base mft to resize $ATTRIBUTE_LIST. + */ +static int ntfs_resident_attr_resize_i(ntfs_attr *na, const s64 newsize, + hole_type holes) +{ + ntfs_attr_search_ctx *ctx; + ntfs_volume *vol; + ntfs_inode *ni; + int err, ret = STATUS_ERROR; + + ntfs_log_trace("Inode 0x%llx attr 0x%x new size %lld\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type), + (long long)newsize); + + /* Get the attribute record that needs modification. */ + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + return -1; + if (ntfs_attr_lookup(na->type, na->name, na->name_len, 0, 0, NULL, 0, + ctx)) { + err = errno; + ntfs_log_perror("ntfs_attr_lookup failed"); + goto put_err_out; + } + vol = na->ni->vol; + /* + * Check the attribute type and the corresponding minimum and maximum + * sizes against @newsize and fail if @newsize is out of bounds. + */ + if (ntfs_attr_size_bounds_check(vol, na->type, newsize) < 0) { + err = errno; + if (err == ENOENT) + err = EIO; + ntfs_log_perror("%s: bounds check failed", __FUNCTION__); + goto put_err_out; + } + /* + * If @newsize is bigger than the mft record we need to make the + * attribute non-resident if the attribute type supports it. If it is + * smaller we can go ahead and attempt the resize. + */ + if ((newsize < vol->mft_record_size) && (holes != HOLES_NONRES)) { + /* Perform the resize of the attribute record. */ + if (!(ret = ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr, + newsize))) { + /* Update attribute size everywhere. */ + na->data_size = na->initialized_size = newsize; + na->allocated_size = (newsize + 7) & ~7; + if ((na->data_flags & ATTR_COMPRESSION_MASK) + || NAttrSparse(na)) + na->compressed_size = na->allocated_size; + if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY + ? na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30 + : na->type == AT_DATA && na->name == AT_UNNAMED) { + na->ni->data_size = na->data_size; + if (((na->data_flags & ATTR_COMPRESSION_MASK) + || NAttrSparse(na)) + && NAttrNonResident(na)) + na->ni->allocated_size + = na->compressed_size; + else + na->ni->allocated_size + = na->allocated_size; + set_nino_flag(na->ni,KnownSize); + if (na->type == AT_DATA) + NInoFileNameSetDirty(na->ni); + } + goto resize_done; + } + /* Prefer AT_INDEX_ALLOCATION instead of AT_ATTRIBUTE_LIST */ + if (ret == STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT) { + err = errno; + goto put_err_out; + } + } + /* There is not enough space in the mft record to perform the resize. */ + + /* Make the attribute non-resident if possible. */ + if (!ntfs_attr_make_non_resident(na, ctx)) { + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_put_search_ctx(ctx); + /* + * do not truncate when forcing non-resident, this + * could cause the attribute to be made resident again, + * so size changes are not allowed. + */ + if (holes == HOLES_NONRES) { + ret = 0; + if (newsize != na->data_size) { + ntfs_log_error("Cannot change size when" + " forcing non-resident\n"); + errno = EIO; + ret = STATUS_ERROR; + } + return (ret); + } + /* Resize non-resident attribute */ + return ntfs_attr_truncate_i(na, newsize, holes); + } else if (errno != ENOSPC && errno != EPERM) { + err = errno; + ntfs_log_perror("Failed to make attribute non-resident"); + goto put_err_out; + } + + /* Try to make other attributes non-resident and retry each time. */ + ntfs_attr_init_search_ctx(ctx, NULL, na->ni->mrec); + while (!ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx)) { + ntfs_attr *tna; + ATTR_RECORD *a; + + a = ctx->attr; + if (a->non_resident) + continue; + + /* + * Check out whether convert is reasonable. Assume that mapping + * pairs will take 8 bytes. + */ + if (le32_to_cpu(a->length) <= offsetof(ATTR_RECORD, + compressed_size) + ((a->name_length * + sizeof(ntfschar) + 7) & ~7) + 8) + continue; + + tna = ntfs_attr_open(na->ni, a->type, (ntfschar*)((u8*)a + + le16_to_cpu(a->name_offset)), a->name_length); + if (!tna) { + err = errno; + ntfs_log_perror("Couldn't open attribute"); + goto put_err_out; + } + if (ntfs_attr_make_non_resident(tna, ctx)) { + ntfs_attr_close(tna); + continue; + } + if ((tna->type == AT_DATA) && !tna->name_len) { + /* + * If we had to make the unnamed data attribute + * non-resident, propagate its new allocated size + * to all name attributes and directory indexes + */ + tna->ni->allocated_size = tna->allocated_size; + NInoFileNameSetDirty(tna->ni); + } + if (((tna->data_flags & ATTR_COMPRESSION_MASK) + == ATTR_IS_COMPRESSED) + && ntfs_attr_pclose(tna)) { + err = errno; + ntfs_attr_close(tna); + goto put_err_out; + } + ntfs_inode_mark_dirty(tna->ni); + ntfs_attr_close(tna); + ntfs_attr_put_search_ctx(ctx); + return ntfs_resident_attr_resize_i(na, newsize, holes); + } + /* Check whether error occurred. */ + if (errno != ENOENT) { + err = errno; + ntfs_log_perror("%s: Attribute lookup failed 1", __FUNCTION__); + goto put_err_out; + } + + /* + * The standard information and attribute list attributes can't be + * moved out from the base MFT record, so try to move out others. + */ + if (na->type==AT_STANDARD_INFORMATION || na->type==AT_ATTRIBUTE_LIST) { + ntfs_attr_put_search_ctx(ctx); + if (!NInoAttrList(na->ni) && ntfs_inode_add_attrlist(na->ni)) { + ntfs_log_perror("Could not add attribute list"); + return -1; + } + if (ntfs_inode_free_space(na->ni, offsetof(ATTR_RECORD, + non_resident_end) + 8)) { + ntfs_log_perror("Could not free space in MFT record"); + return -1; + } + return ntfs_resident_attr_resize_i(na, newsize, holes); + } + + /* + * Move the attribute to a new mft record, creating an attribute list + * attribute or modifying it if it is already present. + */ + + /* Point search context back to attribute which we need resize. */ + ntfs_attr_init_search_ctx(ctx, na->ni, NULL); + if (ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + ntfs_log_perror("%s: Attribute lookup failed 2", __FUNCTION__); + err = errno; + goto put_err_out; + } + + /* + * Check whether attribute is already single in this MFT record. + * 8 added for the attribute terminator. + */ + if (le32_to_cpu(ctx->mrec->bytes_in_use) == + le16_to_cpu(ctx->mrec->attrs_offset) + + le32_to_cpu(ctx->attr->length) + 8) { + err = ENOSPC; + ntfs_log_trace("MFT record is filled with one attribute\n"); + ret = STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT; + goto put_err_out; + } + + /* Add attribute list if not present. */ + if (na->ni->nr_extents == -1) + ni = na->ni->base_ni; + else + ni = na->ni; + if (!NInoAttrList(ni)) { + ntfs_attr_put_search_ctx(ctx); + if (ntfs_inode_add_attrlist(ni)) + return -1; + return ntfs_resident_attr_resize_i(na, newsize, holes); + } + /* Allocate new mft record. */ + ni = ntfs_mft_record_alloc(vol, ni); + if (!ni) { + err = errno; + ntfs_log_perror("Couldn't allocate new MFT record"); + goto put_err_out; + } + /* Move attribute to it. */ + if (ntfs_attr_record_move_to(ctx, ni)) { + err = errno; + ntfs_log_perror("Couldn't move attribute to new MFT record"); + goto put_err_out; + } + /* Update ntfs attribute. */ + if (na->ni->nr_extents == -1) + na->ni = ni; + + ntfs_attr_put_search_ctx(ctx); + /* Try to perform resize once again. */ + return ntfs_resident_attr_resize_i(na, newsize, holes); + +resize_done: + /* + * Set the inode (and its base inode if it exists) dirty so it is + * written out later. + */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_put_search_ctx(ctx); + return 0; +put_err_out: + ntfs_attr_put_search_ctx(ctx); + errno = err; + return ret; +} + +static int ntfs_resident_attr_resize(ntfs_attr *na, const s64 newsize) +{ + int ret; + + ntfs_log_enter("Entering\n"); + ret = ntfs_resident_attr_resize_i(na, newsize, HOLES_OK); + ntfs_log_leave("\n"); + return ret; +} + +/* + * Force an attribute to be made non-resident without + * changing its size. + * + * This is particularly needed when the attribute has no data, + * as the non-resident variant requires more space in the MFT + * record, and may imply expelling some other attribute. + * + * As a consequence the existing ntfs_attr_search_ctx's have to + * be closed or reinitialized. + * + * returns 0 if successful, + * < 0 if failed, with errno telling why + */ + +int ntfs_attr_force_non_resident(ntfs_attr *na) +{ + int res; + + res = ntfs_resident_attr_resize_i(na, na->data_size, HOLES_NONRES); + if (!res && !NAttrNonResident(na)) { + res = -1; + errno = EIO; + ntfs_log_error("Failed to force non-resident\n"); + } + return (res); +} + +/** + * ntfs_attr_make_resident - convert a non-resident to a resident attribute + * @na: open ntfs attribute to make resident + * @ctx: ntfs search context describing the attribute + * + * Convert a non-resident ntfs attribute to a resident one. + * + * Return 0 on success and -1 on error with errno set to the error code. The + * following error codes are defined: + * EINVAL - Invalid arguments passed. + * EPERM - The attribute is not allowed to be resident. + * EIO - I/O error, damaged inode or bug. + * ENOSPC - There is no enough space to perform conversion. + * EOPNOTSUPP - Requested conversion is not supported yet. + * + * Warning: We do not set the inode dirty and we do not write out anything! + * We expect the caller to do this as this is a fairly low level + * function and it is likely there will be further changes made. + */ +static int ntfs_attr_make_resident(ntfs_attr *na, ntfs_attr_search_ctx *ctx) +{ + ntfs_volume *vol = na->ni->vol; + ATTR_REC *a = ctx->attr; + int name_ofs, val_ofs, err = EIO; + s64 arec_size, bytes_read; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x.\n", (unsigned long + long)na->ni->mft_no, le32_to_cpu(na->type)); + + /* Should be called for the first extent of the attribute. */ + if (sle64_to_cpu(a->lowest_vcn)) { + ntfs_log_trace("Eeek! Should be called for the first extent of the " + "attribute. Aborting...\n"); + errno = EINVAL; + return -1; + } + + /* Some preliminary sanity checking. */ + if (!NAttrNonResident(na)) { + ntfs_log_trace("Eeek! Trying to make resident attribute resident. " + "Aborting...\n"); + errno = EINVAL; + return -1; + } + + /* Make sure this is not $MFT/$BITMAP or Windows will not boot! */ + if (na->type == AT_BITMAP && na->ni->mft_no == FILE_MFT) { + errno = EPERM; + return -1; + } + + /* Check that the attribute is allowed to be resident. */ + if (ntfs_attr_can_be_resident(vol, na->type)) + return -1; + + if (na->data_flags & ATTR_IS_ENCRYPTED) { + ntfs_log_trace("Making encrypted streams resident is not " + "implemented yet.\n"); + errno = EOPNOTSUPP; + return -1; + } + + /* Work out offsets into and size of the resident attribute. */ + name_ofs = 24; /* = sizeof(resident_ATTR_REC); */ + val_ofs = (name_ofs + a->name_length * sizeof(ntfschar) + 7) & ~7; + arec_size = (val_ofs + na->data_size + 7) & ~7; + + /* Sanity check the size before we start modifying the attribute. */ + if (le32_to_cpu(ctx->mrec->bytes_in_use) - le32_to_cpu(a->length) + + arec_size > le32_to_cpu(ctx->mrec->bytes_allocated)) { + errno = ENOSPC; + ntfs_log_trace("Not enough space to make attribute resident\n"); + return -1; + } + + /* Read and cache the whole runlist if not already done. */ + if (ntfs_attr_map_whole_runlist(na)) + return -1; + + /* Move the attribute name if it exists and update the offset. */ + if (a->name_length) { + memmove((u8*)a + name_ofs, (u8*)a + le16_to_cpu(a->name_offset), + a->name_length * sizeof(ntfschar)); + } + a->name_offset = cpu_to_le16(name_ofs); + + /* Resize the resident part of the attribute record. */ + if (ntfs_attr_record_resize(ctx->mrec, a, arec_size) < 0) { + /* + * Bug, because ntfs_attr_record_resize should not fail (we + * already checked that attribute fits MFT record). + */ + ntfs_log_error("BUG! Failed to resize attribute record. " + "Please report to the %s. Aborting...\n", + NTFS_DEV_LIST); + errno = EIO; + return -1; + } + + /* Convert the attribute record to describe a resident attribute. */ + a->non_resident = 0; + a->flags = const_cpu_to_le16(0); + a->value_length = cpu_to_le32(na->data_size); + a->value_offset = cpu_to_le16(val_ofs); + /* + * If a data stream was wiped out, adjust the compression mode + * to current state of compression flag + */ + if (!na->data_size + && (na->type == AT_DATA) + && (na->ni->vol->major_ver >= 3) + && NVolCompression(na->ni->vol) + && (na->ni->vol->cluster_size <= MAX_COMPRESSION_CLUSTER_SIZE) + && (na->ni->flags & FILE_ATTR_COMPRESSED)) { + a->flags |= ATTR_IS_COMPRESSED; + na->data_flags = a->flags; + } + /* + * File names cannot be non-resident so we would never see this here + * but at least it serves as a reminder that there may be attributes + * for which we do need to set this flag. (AIA) + */ + if (a->type == AT_FILE_NAME) + a->resident_flags = RESIDENT_ATTR_IS_INDEXED; + else + a->resident_flags = 0; + a->reservedR = 0; + + /* Sanity fixup... Shouldn't really happen. (AIA) */ + if (na->initialized_size > na->data_size) + na->initialized_size = na->data_size; + + /* Copy data from run list to resident attribute value. */ + bytes_read = ntfs_rl_pread(vol, na->rl, 0, na->initialized_size, + (u8*)a + val_ofs); + if (bytes_read != na->initialized_size) { + if (bytes_read < 0) + err = errno; + ntfs_log_trace("Eeek! Failed to read attribute data. Leaving " + "inconstant metadata. Run chkdsk. " + "Aborting...\n"); + errno = err; + return -1; + } + + /* Clear memory in gap between initialized_size and data_size. */ + if (na->initialized_size < na->data_size) + memset((u8*)a + val_ofs + na->initialized_size, 0, + na->data_size - na->initialized_size); + + /* + * Deallocate clusters from the runlist. + * + * NOTE: We can use ntfs_cluster_free() because we have already mapped + * the whole run list and thus it doesn't matter that the attribute + * record is in a transiently corrupted state at this moment in time. + */ + if (ntfs_cluster_free(vol, na, 0, -1) < 0) { + ntfs_log_perror("Eeek! Failed to release allocated clusters"); + ntfs_log_trace("Ignoring error and leaving behind wasted " + "clusters.\n"); + } + + /* Throw away the now unused runlist. */ + free(na->rl); + na->rl = NULL; + + /* Update in-memory struct ntfs_attr. */ + NAttrClearNonResident(na); + NAttrClearFullyMapped(na); + NAttrClearSparse(na); + NAttrClearEncrypted(na); + na->initialized_size = na->data_size; + na->allocated_size = na->compressed_size = (na->data_size + 7) & ~7; + na->compression_block_size = 0; + na->compression_block_size_bits = na->compression_block_clusters = 0; + return 0; +} + +/* + * If we are in the first extent, then set/clean sparse bit, + * update allocated and compressed size. + */ +static int ntfs_attr_update_meta(ATTR_RECORD *a, ntfs_attr *na, MFT_RECORD *m, + hole_type holes, ntfs_attr_search_ctx *ctx) +{ + int sparse, ret = 0; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type)); + + if (a->lowest_vcn) + goto out; + + a->allocated_size = cpu_to_sle64(na->allocated_size); + + /* Update sparse bit, unless this is an intermediate state */ + if (holes == HOLES_DELAY) + sparse = (a->flags & ATTR_IS_SPARSE) != const_cpu_to_le16(0); + else { + sparse = ntfs_rl_sparse(na->rl); + if (sparse == -1) { + errno = EIO; + goto error; + } + } + + /* Check whether attribute becomes sparse, unless check is delayed. */ + if ((holes != HOLES_DELAY) + && sparse + && !(a->flags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))) { + /* + * Move attribute to another mft record, if attribute is too + * small to add compressed_size field to it and we have no + * free space in the current mft record. + */ + if ((le32_to_cpu(a->length) - + le16_to_cpu(a->mapping_pairs_offset) == 8) + && !(le32_to_cpu(m->bytes_allocated) - + le32_to_cpu(m->bytes_in_use))) { + + if (!NInoAttrList(na->ni)) { + ntfs_attr_put_search_ctx(ctx); + if (ntfs_inode_add_attrlist(na->ni)) + goto leave; + goto retry; + } + if (ntfs_attr_record_move_away(ctx, 8)) { + ntfs_log_perror("Failed to move attribute"); + goto error; + } + ntfs_attr_put_search_ctx(ctx); + goto retry; + } + if (!(le32_to_cpu(a->length) - le16_to_cpu( + a->mapping_pairs_offset))) { + errno = EIO; + ntfs_log_perror("Mapping pairs space is 0"); + goto error; + } + + NAttrSetSparse(na); + a->flags |= ATTR_IS_SPARSE; + na->data_flags = a->flags; + a->compression_unit = STANDARD_COMPRESSION_UNIT; /* Windows + set it so, even if attribute is not actually compressed. */ + + memmove((u8*)a + le16_to_cpu(a->name_offset) + 8, + (u8*)a + le16_to_cpu(a->name_offset), + a->name_length * sizeof(ntfschar)); + + a->name_offset = cpu_to_le16(le16_to_cpu(a->name_offset) + 8); + + a->mapping_pairs_offset = + cpu_to_le16(le16_to_cpu(a->mapping_pairs_offset) + 8); + } + + /* Attribute no longer sparse. */ + if (!sparse && (a->flags & ATTR_IS_SPARSE) && + !(a->flags & ATTR_IS_COMPRESSED)) { + + NAttrClearSparse(na); + a->flags &= ~ATTR_IS_SPARSE; + na->data_flags = a->flags; + a->compression_unit = 0; + + memmove((u8*)a + le16_to_cpu(a->name_offset) - 8, + (u8*)a + le16_to_cpu(a->name_offset), + a->name_length * sizeof(ntfschar)); + + if (le16_to_cpu(a->name_offset) >= 8) + a->name_offset = cpu_to_le16(le16_to_cpu(a->name_offset) - 8); + + a->mapping_pairs_offset = + cpu_to_le16(le16_to_cpu(a->mapping_pairs_offset) - 8); + } + + /* Update compressed size if required. */ + if (NAttrFullyMapped(na) + && (sparse || (na->data_flags & ATTR_COMPRESSION_MASK))) { + s64 new_compr_size; + + new_compr_size = ntfs_rl_get_compressed_size(na->ni->vol, na->rl); + if (new_compr_size == -1) + goto error; + + na->compressed_size = new_compr_size; + a->compressed_size = cpu_to_sle64(new_compr_size); + } + /* + * Set FILE_NAME dirty flag, to update sparse bit and + * allocated size in the index. + */ + if (na->type == AT_DATA && na->name == AT_UNNAMED) { + if (sparse || (na->data_flags & ATTR_COMPRESSION_MASK)) + na->ni->allocated_size = na->compressed_size; + else + na->ni->allocated_size = na->allocated_size; + NInoFileNameSetDirty(na->ni); + } +out: + return ret; +leave: ret = -1; goto out; /* return -1 */ +retry: ret = -2; goto out; +error: ret = -3; goto out; +} + +#define NTFS_VCN_DELETE_MARK -2 +/** + * ntfs_attr_update_mapping_pairs_i - see ntfs_attr_update_mapping_pairs + */ +static int ntfs_attr_update_mapping_pairs_i(ntfs_attr *na, VCN from_vcn, + hole_type holes) +{ + ntfs_attr_search_ctx *ctx; + ntfs_inode *ni, *base_ni; + MFT_RECORD *m; + ATTR_RECORD *a; + VCN stop_vcn; + const runlist_element *stop_rl; + int err, mp_size, cur_max_mp_size, exp_max_mp_size, ret = -1; + BOOL finished_build; + BOOL first_updated = FALSE; + +retry: + if (!na || !na->rl) { + errno = EINVAL; + ntfs_log_perror("%s: na=%p", __FUNCTION__, na); + return -1; + } + + ntfs_log_trace("Entering for inode %llu, attr 0x%x\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type)); + + if (!NAttrNonResident(na)) { + errno = EINVAL; + ntfs_log_perror("%s: resident attribute", __FUNCTION__); + return -1; + } + +#if PARTIAL_RUNLIST_UPDATING + /* + * For a file just been made sparse, we will have + * to reformat the first extent, so be sure the + * runlist is fully mapped and fully processed. + * Same if the file was sparse and is not any more. + * Note : not needed if the full runlist is to be processed + */ + if ((holes != HOLES_DELAY) + && (!NAttrFullyMapped(na) || from_vcn) + && !(na->data_flags & ATTR_IS_COMPRESSED)) { + BOOL changed; + + if (!(na->data_flags & ATTR_IS_SPARSE)) { + int sparse = 0; + runlist_element *xrl; + + /* + * If attribute was not sparse, we only + * have to check whether there is a hole + * in the updated region. + */ + for (xrl = na->rl; xrl->length; xrl++) { + if (xrl->lcn < 0) { + if (xrl->lcn == LCN_HOLE) { + sparse = 1; + break; + } + if (xrl->lcn != LCN_RL_NOT_MAPPED) { + sparse = -1; + break; + } + } + } + if (sparse < 0) { + ntfs_log_error("Could not check whether sparse\n"); + errno = EIO; + return (-1); + } + changed = sparse > 0; + } else { + /* + * If attribute was sparse, the compressed + * size has been maintained, and it gives + * and easy way to check whether the + * attribute is still sparse. + */ + changed = (((na->data_size - 1) + | (na->ni->vol->cluster_size - 1)) + 1) + == na->compressed_size; + } + if (changed) { + if (ntfs_attr_map_whole_runlist(na)) { + ntfs_log_error("Could not map whole for sparse change\n"); + errno = EIO; + return (-1); + } + from_vcn = 0; + } + } +#endif + if (na->ni->nr_extents == -1) + base_ni = na->ni->base_ni; + else + base_ni = na->ni; + + ctx = ntfs_attr_get_search_ctx(base_ni, NULL); + if (!ctx) + return -1; + + /* Fill attribute records with new mapping pairs. */ + stop_vcn = 0; + stop_rl = na->rl; + finished_build = FALSE; + while (!ntfs_attr_lookup(na->type, na->name, na->name_len, + CASE_SENSITIVE, from_vcn, NULL, 0, ctx)) { + a = ctx->attr; + m = ctx->mrec; + if (!a->lowest_vcn) + first_updated = TRUE; + /* + * If runlist is updating not from the beginning, then set + * @stop_vcn properly, i.e. to the lowest vcn of record that + * contain @from_vcn. Also we do not need @from_vcn anymore, + * set it to 0 to make ntfs_attr_lookup enumerate attributes. + */ + if (from_vcn) { + LCN first_lcn; + + stop_vcn = sle64_to_cpu(a->lowest_vcn); + from_vcn = 0; + /* + * Check whether the first run we need to update is + * the last run in runlist, if so, then deallocate + * all attrubute extents starting this one. + */ + first_lcn = ntfs_rl_vcn_to_lcn(na->rl, stop_vcn); + if (first_lcn == LCN_EINVAL) { + errno = EIO; + ntfs_log_perror("Bad runlist"); + goto put_err_out; + } + if (first_lcn == LCN_ENOENT || + first_lcn == LCN_RL_NOT_MAPPED) + finished_build = TRUE; + } + + /* + * Check whether we finished mapping pairs build, if so mark + * extent as need to delete (by setting highest vcn to + * NTFS_VCN_DELETE_MARK (-2), we shall check it later and + * delete extent) and continue search. + */ + if (finished_build) { + ntfs_log_trace("Mark attr 0x%x for delete in inode " + "%lld.\n", (unsigned)le32_to_cpu(a->type), + (long long)ctx->ntfs_ino->mft_no); + a->highest_vcn = cpu_to_sle64(NTFS_VCN_DELETE_MARK); + ntfs_inode_mark_dirty(ctx->ntfs_ino); + continue; + } + + switch (ntfs_attr_update_meta(a, na, m, holes, ctx)) { + case -1: return -1; + case -2: goto retry; + case -3: goto put_err_out; + } + + /* + * Determine maximum possible length of mapping pairs, + * if we shall *not* expand space for mapping pairs. + */ + cur_max_mp_size = le32_to_cpu(a->length) - + le16_to_cpu(a->mapping_pairs_offset); + /* + * Determine maximum possible length of mapping pairs in the + * current mft record, if we shall expand space for mapping + * pairs. + */ + exp_max_mp_size = le32_to_cpu(m->bytes_allocated) - + le32_to_cpu(m->bytes_in_use) + cur_max_mp_size; + /* Get the size for the rest of mapping pairs array. */ + mp_size = ntfs_get_size_for_mapping_pairs(na->ni->vol, stop_rl, + stop_vcn, exp_max_mp_size); + if (mp_size <= 0) { + ntfs_log_perror("%s: get MP size failed", __FUNCTION__); + goto put_err_out; + } + /* Test mapping pairs for fitting in the current mft record. */ + if (mp_size > exp_max_mp_size) { + /* + * Mapping pairs of $ATTRIBUTE_LIST attribute must fit + * in the base mft record. Try to move out other + * attributes and try again. + */ + if (na->type == AT_ATTRIBUTE_LIST) { + ntfs_attr_put_search_ctx(ctx); + if (ntfs_inode_free_space(na->ni, mp_size - + cur_max_mp_size)) { + ntfs_log_perror("Attribute list is too " + "big. Defragment the " + "volume\n"); + return -1; + } + goto retry; + } + + /* Add attribute list if it isn't present, and retry. */ + if (!NInoAttrList(base_ni)) { + ntfs_attr_put_search_ctx(ctx); + if (ntfs_inode_add_attrlist(base_ni)) { + ntfs_log_perror("Can not add attrlist"); + return -1; + } + goto retry; + } + + /* + * Set mapping pairs size to maximum possible for this + * mft record. We shall write the rest of mapping pairs + * to another MFT records. + */ + mp_size = exp_max_mp_size; + } + + /* Change space for mapping pairs if we need it. */ + if (((mp_size + 7) & ~7) != cur_max_mp_size) { + if (ntfs_attr_record_resize(m, a, + le16_to_cpu(a->mapping_pairs_offset) + + mp_size)) { + errno = EIO; + ntfs_log_perror("Failed to resize attribute"); + goto put_err_out; + } + } + + /* Update lowest vcn. */ + a->lowest_vcn = cpu_to_sle64(stop_vcn); + ntfs_inode_mark_dirty(ctx->ntfs_ino); + if ((ctx->ntfs_ino->nr_extents == -1 || + NInoAttrList(ctx->ntfs_ino)) && + ctx->attr->type != AT_ATTRIBUTE_LIST) { + ctx->al_entry->lowest_vcn = cpu_to_sle64(stop_vcn); + ntfs_attrlist_mark_dirty(ctx->ntfs_ino); + } + + /* + * Generate the new mapping pairs array directly into the + * correct destination, i.e. the attribute record itself. + */ + if (!ntfs_mapping_pairs_build(na->ni->vol, (u8*)a + le16_to_cpu( + a->mapping_pairs_offset), mp_size, na->rl, + stop_vcn, &stop_rl)) + finished_build = TRUE; + if (stop_rl) + stop_vcn = stop_rl->vcn; + else + stop_vcn = 0; + if (!finished_build && errno != ENOSPC) { + ntfs_log_perror("Failed to build mapping pairs"); + goto put_err_out; + } + a->highest_vcn = cpu_to_sle64(stop_vcn - 1); + } + /* Check whether error occurred. */ + if (errno != ENOENT) { + ntfs_log_perror("%s: Attribute lookup failed", __FUNCTION__); + goto put_err_out; + } + /* + * If the base extent was skipped in the above process, + * we still may have to update the sizes. + */ + if (!first_updated) { + le16 spcomp; + + ntfs_attr_reinit_search_ctx(ctx); + if (!ntfs_attr_lookup(na->type, na->name, na->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + a = ctx->attr; + a->allocated_size = cpu_to_sle64(na->allocated_size); + spcomp = na->data_flags + & (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE); + if (spcomp) + a->compressed_size = cpu_to_sle64(na->compressed_size); + /* Updating sizes taints the extent holding the attr */ + if (ctx->ntfs_ino) + NInoSetDirty(ctx->ntfs_ino); + if ((na->type == AT_DATA) && (na->name == AT_UNNAMED)) { + na->ni->allocated_size + = (spcomp + ? na->compressed_size + : na->allocated_size); + NInoFileNameSetDirty(na->ni); + } + } else { + ntfs_log_error("Failed to update sizes in base extent\n"); + goto put_err_out; + } + } + + /* Deallocate not used attribute extents and return with success. */ + if (finished_build) { + ntfs_attr_reinit_search_ctx(ctx); + ntfs_log_trace("Deallocate marked extents.\n"); + while (!ntfs_attr_lookup(na->type, na->name, na->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + if (sle64_to_cpu(ctx->attr->highest_vcn) != + NTFS_VCN_DELETE_MARK) + continue; + /* Remove unused attribute record. */ + if (ntfs_attr_record_rm(ctx)) { + ntfs_log_perror("Could not remove unused attr"); + goto put_err_out; + } + ntfs_attr_reinit_search_ctx(ctx); + } + if (errno != ENOENT) { + ntfs_log_perror("%s: Attr lookup failed", __FUNCTION__); + goto put_err_out; + } + ntfs_log_trace("Deallocate done.\n"); + ntfs_attr_put_search_ctx(ctx); + goto ok; + } + ntfs_attr_put_search_ctx(ctx); + ctx = NULL; + + /* Allocate new MFT records for the rest of mapping pairs. */ + while (1) { + /* Calculate size of rest mapping pairs. */ + mp_size = ntfs_get_size_for_mapping_pairs(na->ni->vol, + na->rl, stop_vcn, INT_MAX); + if (mp_size <= 0) { + ntfs_log_perror("%s: get mp size failed", __FUNCTION__); + goto put_err_out; + } + /* Allocate new mft record, with special case for mft itself */ + if (!na->ni->mft_no) + ni = ntfs_mft_rec_alloc(na->ni->vol, + na->type == AT_DATA); + else + ni = ntfs_mft_record_alloc(na->ni->vol, base_ni); + if (!ni) { + ntfs_log_perror("Could not allocate new MFT record"); + goto put_err_out; + } + m = ni->mrec; + /* + * If mapping size exceed available space, set them to + * possible maximum. + */ + cur_max_mp_size = le32_to_cpu(m->bytes_allocated) - + le32_to_cpu(m->bytes_in_use) - + (offsetof(ATTR_RECORD, compressed_size) + + (((na->data_flags & ATTR_COMPRESSION_MASK) + || NAttrSparse(na)) ? + sizeof(a->compressed_size) : 0)) - + ((sizeof(ntfschar) * na->name_len + 7) & ~7); + if (mp_size > cur_max_mp_size) + mp_size = cur_max_mp_size; + /* Add attribute extent to new record. */ + err = ntfs_non_resident_attr_record_add(ni, na->type, + na->name, na->name_len, stop_vcn, mp_size, + na->data_flags); + if (err == -1) { + err = errno; + ntfs_log_perror("Could not add attribute extent"); + if (ntfs_mft_record_free(na->ni->vol, ni)) + ntfs_log_perror("Could not free MFT record"); + errno = err; + goto put_err_out; + } + a = (ATTR_RECORD*)((u8*)m + err); + + err = ntfs_mapping_pairs_build(na->ni->vol, (u8*)a + + le16_to_cpu(a->mapping_pairs_offset), mp_size, na->rl, + stop_vcn, &stop_rl); + if (stop_rl) + stop_vcn = stop_rl->vcn; + else + stop_vcn = 0; + if (err < 0 && errno != ENOSPC) { + err = errno; + ntfs_log_perror("Failed to build MP"); + if (ntfs_mft_record_free(na->ni->vol, ni)) + ntfs_log_perror("Couldn't free MFT record"); + errno = err; + goto put_err_out; + } + a->highest_vcn = cpu_to_sle64(stop_vcn - 1); + ntfs_inode_mark_dirty(ni); + /* All mapping pairs has been written. */ + if (!err) + break; + } +ok: + NAttrClearRunlistDirty(na); + ret = 0; +out: + return ret; +put_err_out: + if (ctx) + ntfs_attr_put_search_ctx(ctx); + goto out; +} +#undef NTFS_VCN_DELETE_MARK + +/** + * ntfs_attr_update_mapping_pairs - update mapping pairs for ntfs attribute + * @na: non-resident ntfs open attribute for which we need update + * @from_vcn: update runlist starting this VCN + * + * Build mapping pairs from @na->rl and write them to the disk. Also, this + * function updates sparse bit, allocated and compressed size (allocates/frees + * space for this field if required). + * + * @na->allocated_size should be set to correct value for the new runlist before + * call to this function. Vice-versa @na->compressed_size will be calculated and + * set to correct value during this function. + * + * FIXME: This function does not update sparse bit and compressed size correctly + * if called with @from_vcn != 0. + * + * FIXME: Rewrite without using NTFS_VCN_DELETE_MARK define. + * + * On success return 0 and on error return -1 with errno set to the error code. + * The following error codes are defined: + * EINVAL - Invalid arguments passed. + * ENOMEM - Not enough memory to complete operation. + * ENOSPC - There is no enough space in base mft to resize $ATTRIBUTE_LIST + * or there is no free MFT records left to allocate. + */ +int ntfs_attr_update_mapping_pairs(ntfs_attr *na, VCN from_vcn) +{ + int ret; + + ntfs_log_enter("Entering\n"); + ret = ntfs_attr_update_mapping_pairs_i(na, from_vcn, HOLES_OK); + ntfs_log_leave("\n"); + return ret; +} + +/** + * ntfs_non_resident_attr_shrink - shrink a non-resident, open ntfs attribute + * @na: non-resident ntfs attribute to shrink + * @newsize: new size (in bytes) to which to shrink the attribute + * + * Reduce the size of a non-resident, open ntfs attribute @na to @newsize bytes. + * + * On success return 0 and on error return -1 with errno set to the error code. + * The following error codes are defined: + * ENOMEM - Not enough memory to complete operation. + * ERANGE - @newsize is not valid for the attribute type of @na. + */ +static int ntfs_non_resident_attr_shrink(ntfs_attr *na, const s64 newsize) +{ + ntfs_volume *vol; + ntfs_attr_search_ctx *ctx; + VCN first_free_vcn; + s64 nr_freed_clusters; + int err; + + ntfs_log_trace("Inode 0x%llx attr 0x%x new size %lld\n", (unsigned long long) + na->ni->mft_no, le32_to_cpu(na->type), (long long)newsize); + + vol = na->ni->vol; + + /* + * Check the attribute type and the corresponding minimum size + * against @newsize and fail if @newsize is too small. + */ + if (ntfs_attr_size_bounds_check(vol, na->type, newsize) < 0) { + if (errno == ERANGE) { + ntfs_log_trace("Eeek! Size bounds check failed. " + "Aborting...\n"); + } else if (errno == ENOENT) + errno = EIO; + return -1; + } + + /* The first cluster outside the new allocation. */ + if (na->data_flags & ATTR_COMPRESSION_MASK) + /* + * For compressed files we must keep full compressions blocks, + * but currently we do not decompress/recompress the last + * block to truncate the data, so we may leave more allocated + * clusters than really needed. + */ + first_free_vcn = (((newsize - 1) + | (na->compression_block_size - 1)) + 1) + >> vol->cluster_size_bits; + else + first_free_vcn = (newsize + vol->cluster_size - 1) >> + vol->cluster_size_bits; + /* + * Compare the new allocation with the old one and only deallocate + * clusters if there is a change. + */ + if ((na->allocated_size >> vol->cluster_size_bits) != first_free_vcn) { + if (ntfs_attr_map_whole_runlist(na)) { + ntfs_log_trace("Eeek! ntfs_attr_map_whole_runlist " + "failed.\n"); + return -1; + } + /* Deallocate all clusters starting with the first free one. */ + nr_freed_clusters = ntfs_cluster_free(vol, na, first_free_vcn, + -1); + if (nr_freed_clusters < 0) { + ntfs_log_trace("Eeek! Freeing of clusters failed. " + "Aborting...\n"); + return -1; + } + + /* Truncate the runlist itself. */ + if (ntfs_rl_truncate(&na->rl, first_free_vcn)) { + /* + * Failed to truncate the runlist, so just throw it + * away, it will be mapped afresh on next use. + */ + free(na->rl); + na->rl = NULL; + ntfs_log_trace("Eeek! Run list truncation failed.\n"); + return -1; + } + NAttrSetRunlistDirty(na); + + /* Prepare to mapping pairs update. */ + na->allocated_size = first_free_vcn << vol->cluster_size_bits; + /* Write mapping pairs for new runlist. */ + if (ntfs_attr_update_mapping_pairs(na, 0 /*first_free_vcn*/)) { + ntfs_log_trace("Eeek! Mapping pairs update failed. " + "Leaving inconstant metadata. " + "Run chkdsk.\n"); + return -1; + } + } + + /* Get the first attribute record. */ + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) + return -1; + + if (ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + err = errno; + if (err == ENOENT) + err = EIO; + ntfs_log_trace("Eeek! Lookup of first attribute extent failed. " + "Leaving inconstant metadata.\n"); + goto put_err_out; + } + + /* Update data and initialized size. */ + na->data_size = newsize; + ctx->attr->data_size = cpu_to_sle64(newsize); + if (newsize < na->initialized_size) { + na->initialized_size = newsize; + ctx->attr->initialized_size = cpu_to_sle64(newsize); + } + /* Update data size in the index. */ + if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + if (na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30) { + na->ni->data_size = na->data_size; + na->ni->allocated_size = na->allocated_size; + set_nino_flag(na->ni,KnownSize); + } + } else { + if (na->type == AT_DATA && na->name == AT_UNNAMED) { + na->ni->data_size = na->data_size; + NInoFileNameSetDirty(na->ni); + } + } + + /* If the attribute now has zero size, make it resident. */ + if (!newsize) { + if (!(na->data_flags & ATTR_IS_ENCRYPTED) + && ntfs_attr_make_resident(na, ctx)) { + /* If couldn't make resident, just continue. */ + if (errno != EPERM) + ntfs_log_error("Failed to make attribute " + "resident. Leaving as is...\n"); + } + } + + /* Set the inode dirty so it is written out later. */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + /* Done! */ + ntfs_attr_put_search_ctx(ctx); + return 0; +put_err_out: + ntfs_attr_put_search_ctx(ctx); + errno = err; + return -1; +} + +/** + * ntfs_non_resident_attr_expand - expand a non-resident, open ntfs attribute + * @na: non-resident ntfs attribute to expand + * @newsize: new size (in bytes) to which to expand the attribute + * + * Expand the size of a non-resident, open ntfs attribute @na to @newsize bytes, + * by allocating new clusters. + * + * On success return 0 and on error return -1 with errno set to the error code. + * The following error codes are defined: + * ENOMEM - Not enough memory to complete operation. + * ERANGE - @newsize is not valid for the attribute type of @na. + * ENOSPC - There is no enough space in base mft to resize $ATTRIBUTE_LIST. + */ +static int ntfs_non_resident_attr_expand_i(ntfs_attr *na, const s64 newsize, + hole_type holes) +{ + LCN lcn_seek_from; + VCN first_free_vcn; + ntfs_volume *vol; + ntfs_attr_search_ctx *ctx; + runlist *rl, *rln; + s64 org_alloc_size; + int err; + + ntfs_log_trace("Inode %lld, attr 0x%x, new size %lld old size %lld\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type), + (long long)newsize, (long long)na->data_size); + + vol = na->ni->vol; + + /* + * Check the attribute type and the corresponding maximum size + * against @newsize and fail if @newsize is too big. + */ + if (ntfs_attr_size_bounds_check(vol, na->type, newsize) < 0) { + if (errno == ENOENT) + errno = EIO; + ntfs_log_perror("%s: bounds check failed", __FUNCTION__); + return -1; + } + + if (na->type == AT_DATA) + NAttrSetDataAppending(na); + /* Save for future use. */ + org_alloc_size = na->allocated_size; + /* The first cluster outside the new allocation. */ + first_free_vcn = (newsize + vol->cluster_size - 1) >> + vol->cluster_size_bits; + /* + * Compare the new allocation with the old one and only allocate + * clusters if there is a change. + */ + if ((na->allocated_size >> vol->cluster_size_bits) < first_free_vcn) { +#if PARTIAL_RUNLIST_UPDATING + s64 start_update; + + /* + * Update from the last previously allocated run, + * as we may have to expand an existing hole. + */ + start_update = na->allocated_size >> vol->cluster_size_bits; + if (start_update) + start_update--; + if (ntfs_attr_map_partial_runlist(na, start_update)) { + ntfs_log_perror("failed to map partial runlist"); + return -1; + } +#else + if (ntfs_attr_map_whole_runlist(na)) { + ntfs_log_perror("ntfs_attr_map_whole_runlist failed"); + return -1; + } +#endif + + /* + * If we extend $DATA attribute on NTFS 3+ volume, we can add + * sparse runs instead of real allocation of clusters. + */ + if ((na->type == AT_DATA) && (vol->major_ver >= 3) + && (holes != HOLES_NO)) { + rl = ntfs_malloc(0x1000); + if (!rl) + return -1; + + rl[0].vcn = (na->allocated_size >> + vol->cluster_size_bits); + rl[0].lcn = LCN_HOLE; + rl[0].length = first_free_vcn - + (na->allocated_size >> vol->cluster_size_bits); + rl[1].vcn = first_free_vcn; + rl[1].lcn = LCN_ENOENT; + rl[1].length = 0; + } else { + /* + * Determine first after last LCN of attribute. + * We will start seek clusters from this LCN to avoid + * fragmentation. If there are no valid LCNs in the + * attribute let the cluster allocator choose the + * starting LCN. + */ + lcn_seek_from = -1; + if (na->rl->length) { + /* Seek to the last run list element. */ + for (rl = na->rl; (rl + 1)->length; rl++) + ; + /* + * If the last LCN is a hole or similar seek + * back to last valid LCN. + */ + while (rl->lcn < 0 && rl != na->rl) + rl--; + /* + * Only set lcn_seek_from it the LCN is valid. + */ + if (rl->lcn >= 0) + lcn_seek_from = rl->lcn + rl->length; + } + + rl = ntfs_cluster_alloc(vol, na->allocated_size >> + vol->cluster_size_bits, first_free_vcn - + (na->allocated_size >> + vol->cluster_size_bits), lcn_seek_from, + DATA_ZONE); + if (!rl) { + ntfs_log_perror("Cluster allocation failed " + "(%lld)", + (long long)first_free_vcn - + ((long long)na->allocated_size >> + vol->cluster_size_bits)); + return -1; + } + } + + /* Append new clusters to attribute runlist. */ + rln = ntfs_runlists_merge(na->rl, rl); + if (!rln) { + /* Failed, free just allocated clusters. */ + err = errno; + ntfs_log_perror("Run list merge failed"); + ntfs_cluster_free_from_rl(vol, rl); + free(rl); + errno = err; + return -1; + } + na->rl = rln; + NAttrSetRunlistDirty(na); + + /* Prepare to mapping pairs update. */ + na->allocated_size = first_free_vcn << vol->cluster_size_bits; +#if PARTIAL_RUNLIST_UPDATING + /* + * Write mapping pairs for new runlist, unless this is + * a temporary state before appending data. + * If the update is not done, we must be sure to do + * it later, and to get to a clean state even on errors. + */ + if ((holes != HOLES_DELAY) + && ntfs_attr_update_mapping_pairs_i(na, start_update, + holes)) { +#else + /* Write mapping pairs for new runlist. */ + if (ntfs_attr_update_mapping_pairs(na, 0)) { +#endif + err = errno; + ntfs_log_perror("Mapping pairs update failed"); + goto rollback; + } + } + + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) { + err = errno; + if (na->allocated_size == org_alloc_size) { + errno = err; + return -1; + } else + goto rollback; + } + + if (ntfs_attr_lookup(na->type, na->name, na->name_len, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + err = errno; + ntfs_log_perror("Lookup of first attribute extent failed"); + if (err == ENOENT) + err = EIO; + if (na->allocated_size != org_alloc_size) { + ntfs_attr_put_search_ctx(ctx); + goto rollback; + } else + goto put_err_out; + } + + /* Update data size. */ + na->data_size = newsize; + ctx->attr->data_size = cpu_to_sle64(newsize); + /* Update data size in the index. */ + if (na->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + if (na->type == AT_INDEX_ROOT && na->name == NTFS_INDEX_I30) { + na->ni->data_size = na->data_size; + na->ni->allocated_size = na->allocated_size; + set_nino_flag(na->ni,KnownSize); + } + } else { + if (na->type == AT_DATA && na->name == AT_UNNAMED) { + na->ni->data_size = na->data_size; + NInoFileNameSetDirty(na->ni); + } + } + /* Set the inode dirty so it is written out later. */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + /* Done! */ + ntfs_attr_put_search_ctx(ctx); + return 0; +rollback: + /* Free allocated clusters. */ + if (ntfs_cluster_free(vol, na, org_alloc_size >> + vol->cluster_size_bits, -1) < 0) { + err = EIO; + ntfs_log_perror("Leaking clusters"); + } + /* Now, truncate the runlist itself. */ + if (ntfs_rl_truncate(&na->rl, org_alloc_size >> + vol->cluster_size_bits)) { + /* + * Failed to truncate the runlist, so just throw it away, it + * will be mapped afresh on next use. + */ + free(na->rl); + na->rl = NULL; + ntfs_log_perror("Couldn't truncate runlist. Rollback failed"); + } else { + NAttrSetRunlistDirty(na); + /* Prepare to mapping pairs update. */ + na->allocated_size = org_alloc_size; + /* Restore mapping pairs. */ + if (ntfs_attr_update_mapping_pairs(na, 0 /*na->allocated_size >> + vol->cluster_size_bits*/)) { + ntfs_log_perror("Failed to restore old mapping pairs"); + } + } + errno = err; + return -1; +put_err_out: + ntfs_attr_put_search_ctx(ctx); + errno = err; + return -1; +} + + +static int ntfs_non_resident_attr_expand(ntfs_attr *na, const s64 newsize, + hole_type holes) +{ + int ret; + + ntfs_log_enter("Entering\n"); + ret = ntfs_non_resident_attr_expand_i(na, newsize, holes); + ntfs_log_leave("\n"); + return ret; +} + +/** + * ntfs_attr_truncate - resize an ntfs attribute + * @na: open ntfs attribute to resize + * @newsize: new size (in bytes) to which to resize the attribute + * @holes: how to create a hole if expanding + * + * Change the size of an open ntfs attribute @na to @newsize bytes. If the + * attribute is made bigger and the attribute is resident the newly + * "allocated" space is cleared and if the attribute is non-resident the + * newly allocated space is marked as not initialised and no real allocation + * on disk is performed. + * + * On success return 0. + * On error return values are: + * STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT + * STATUS_ERROR - otherwise + * The following error codes are defined: + * EINVAL - Invalid arguments were passed to the function. + * EOPNOTSUPP - The desired resize is not implemented yet. + * EACCES - Encrypted attribute. + */ +static int ntfs_attr_truncate_i(ntfs_attr *na, const s64 newsize, + hole_type holes) +{ + int ret = STATUS_ERROR; + s64 fullsize; + BOOL compressed; + + if (!na || newsize < 0 || + (na->ni->mft_no == FILE_MFT && na->type == AT_DATA)) { + ntfs_log_trace("Invalid arguments passed.\n"); + errno = EINVAL; + return STATUS_ERROR; + } + + ntfs_log_enter("Entering for inode %lld, attr 0x%x, size %lld\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type), + (long long)newsize); + + if (na->data_size == newsize) { + ntfs_log_trace("Size is already ok\n"); + ret = STATUS_OK; + goto out; + } + /* + * Encrypted attributes are not supported. We return access denied, + * which is what Windows NT4 does, too. + */ + if ((na->data_flags & ATTR_IS_ENCRYPTED) && !na->ni->vol->efs_raw) { + errno = EACCES; + ntfs_log_trace("Cannot truncate encrypted attribute\n"); + goto out; + } + /* + * TODO: Implement making handling of compressed attributes. + * Currently we can only expand the attribute or delete it, + * and only for ATTR_IS_COMPRESSED. This is however possible + * for resident attributes when there is no open fuse context + * (important case : $INDEX_ROOT:$I30) + */ + compressed = (na->data_flags & ATTR_COMPRESSION_MASK) + != const_cpu_to_le16(0); + if (compressed + && NAttrNonResident(na) + && ((na->data_flags & ATTR_COMPRESSION_MASK) != ATTR_IS_COMPRESSED)) { + errno = EOPNOTSUPP; + ntfs_log_perror("Failed to truncate compressed attribute"); + goto out; + } + if (NAttrNonResident(na)) { + /* + * For compressed data, the last block must be fully + * allocated, and we do not know the size of compression + * block until the attribute has been made non-resident. + * Moreover we can only process a single compression + * block at a time (from where we are about to write), + * so we silently do not allocate more. + * + * Note : do not request upsizing of compressed files + * unless being able to face the consequences ! + */ + if (compressed && newsize && (newsize > na->data_size)) + fullsize = (na->initialized_size + | (na->compression_block_size - 1)) + 1; + else + fullsize = newsize; + if (fullsize > na->data_size) + ret = ntfs_non_resident_attr_expand(na, fullsize, + holes); + else + ret = ntfs_non_resident_attr_shrink(na, fullsize); + } else + ret = ntfs_resident_attr_resize_i(na, newsize, holes); +out: + ntfs_log_leave("Return status %d\n", ret); + return ret; +} + +/* + * Resize an attribute, creating a hole if relevant + */ + +int ntfs_attr_truncate(ntfs_attr *na, const s64 newsize) +{ + int r; + + r = ntfs_attr_truncate_i(na, newsize, HOLES_OK); + NAttrClearDataAppending(na); + NAttrClearBeingNonResident(na); + return (r); +} + +/* + * Resize an attribute, avoiding hole creation + */ + +int ntfs_attr_truncate_solid(ntfs_attr *na, const s64 newsize) +{ + return (ntfs_attr_truncate_i(na, newsize, HOLES_NO)); +} + +/* + * Stuff a hole in a compressed file + * + * An unallocated hole must be aligned on compression block size. + * If needed current block and target block are stuffed with zeroes. + * + * Returns 0 if succeeded, + * -1 if it failed (as explained in errno) + */ + +static int stuff_hole(ntfs_attr *na, const s64 pos) +{ + s64 size; + s64 begin_size; + s64 end_size; + char *buf; + int ret; + + ret = 0; + /* + * If the attribute is resident, the compression block size + * is not defined yet and we can make no decision. + * So we first try resizing to the target and if the + * attribute is still resident, we're done + */ + if (!NAttrNonResident(na)) { + ret = ntfs_resident_attr_resize(na, pos); + if (!ret && !NAttrNonResident(na)) + na->initialized_size = na->data_size = pos; + } + if (!ret && NAttrNonResident(na)) { + /* does the hole span over several compression block ? */ + if ((pos ^ na->initialized_size) + & ~((s64)na->compression_block_size - 1)) { + begin_size = ((na->initialized_size - 1) + | (na->compression_block_size - 1)) + + 1 - na->initialized_size; + end_size = pos & (na->compression_block_size - 1); + size = (begin_size > end_size ? begin_size : end_size); + } else { + /* short stuffing in a single compression block */ + begin_size = size = pos - na->initialized_size; + end_size = 0; + } + if (size) + buf = (char*)ntfs_malloc(size); + else + buf = (char*)NULL; + if (buf || !size) { + memset(buf,0,size); + /* stuff into current block */ + if (begin_size + && (ntfs_attr_pwrite(na, + na->initialized_size, begin_size, buf) + != begin_size)) + ret = -1; + /* create an unstuffed hole */ + if (!ret + && ((na->initialized_size + end_size) < pos) + && ntfs_non_resident_attr_expand(na, + pos - end_size, HOLES_OK)) + ret = -1; + else + na->initialized_size + = na->data_size = pos - end_size; + /* stuff into the target block */ + if (!ret && end_size + && (ntfs_attr_pwrite(na, + na->initialized_size, end_size, buf) + != end_size)) + ret = -1; + if (buf) + free(buf); + } else + ret = -1; + } + /* make absolutely sure we have reached the target */ + if (!ret && (na->initialized_size != pos)) { + ntfs_log_error("Failed to stuff a compressed file" + "target %lld reached %lld\n", + (long long)pos, (long long)na->initialized_size); + errno = EIO; + ret = -1; + } + return (ret); +} + +/** + * ntfs_attr_readall - read the entire data from an ntfs attribute + * @ni: open ntfs inode in which the ntfs attribute resides + * @type: attribute type + * @name: attribute name in little endian Unicode or AT_UNNAMED or NULL + * @name_len: length of attribute @name in Unicode characters (if @name given) + * @data_size: if non-NULL then store here the data size + * + * This function will read the entire content of an ntfs attribute. + * If @name is AT_UNNAMED then look specifically for an unnamed attribute. + * If @name is NULL then the attribute could be either named or not. + * In both those cases @name_len is not used at all. + * + * On success a buffer is allocated with the content of the attribute + * and which needs to be freed when it's not needed anymore. If the + * @data_size parameter is non-NULL then the data size is set there. + * + * On error NULL is returned with errno set to the error code. + */ +void *ntfs_attr_readall(ntfs_inode *ni, const ATTR_TYPES type, + ntfschar *name, u32 name_len, s64 *data_size) +{ + ntfs_attr *na; + void *data, *ret = NULL; + s64 size; + + ntfs_log_enter("Entering\n"); + + na = ntfs_attr_open(ni, type, name, name_len); + if (!na) { + ntfs_log_perror("ntfs_attr_open failed, inode %lld attr 0x%lx", + (long long)ni->mft_no,(long)le32_to_cpu(type)); + goto err_exit; + } + /* + * Consistency check : restrict to 65536 bytes. + * index bitmaps may need more, but still limited by + * the number of clusters. + */ + if (((u64)na->data_size > 65536) + && ((type != AT_BITMAP) + || ((u64)na->data_size > + (u64)((ni->vol->nr_clusters + 7) >> 3)))) { + ntfs_log_error("Corrupt attribute 0x%lx in inode %lld\n", + (long)le32_to_cpu(type),(long long)ni->mft_no); + errno = EOVERFLOW; + goto out; + } + data = ntfs_malloc(na->data_size); + if (!data) + goto out; + + size = ntfs_attr_pread(na, 0, na->data_size, data); + if (size != na->data_size) { + ntfs_log_perror("ntfs_attr_pread failed"); + free(data); + goto out; + } + ret = data; + if (data_size) + *data_size = size; +out: + ntfs_attr_close(na); +err_exit: + ntfs_log_leave("\n"); + return ret; +} + +/* + * Read some data from a data attribute + * + * Returns the amount of data read, negative if there was an error + */ + +int ntfs_attr_data_read(ntfs_inode *ni, + ntfschar *stream_name, int stream_name_len, + char *buf, size_t size, off_t offset) +{ + ntfs_attr *na = NULL; + int res, total = 0; + + na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len); + if (!na) { + res = -errno; + goto exit; + } + if ((size_t)offset < (size_t)na->data_size) { + if (offset + size > (size_t)na->data_size) + size = na->data_size - offset; + while (size) { + res = ntfs_attr_pread(na, offset, size, buf + total); + if ((off_t)res < (off_t)size) + ntfs_log_perror("ntfs_attr_pread partial read " + "(%lld : %lld <> %d)", + (long long)offset, + (long long)size, res); + if (res <= 0) { + res = -errno; + goto exit; + } + size -= res; + offset += res; + total += res; + } + } + res = total; +exit: + if (na) + ntfs_attr_close(na); + return res; +} + + +/* + * Write some data into a data attribute + * + * Returns the amount of data written, negative if there was an error + */ + +int ntfs_attr_data_write(ntfs_inode *ni, ntfschar *stream_name, + int stream_name_len, const char *buf, size_t size, off_t offset) +{ + ntfs_attr *na = NULL; + int res, total = 0; + + na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len); + if (!na) { + res = -errno; + goto exit; + } + while (size) { + res = ntfs_attr_pwrite(na, offset, size, buf + total); + if (res < (s64)size) + ntfs_log_perror("ntfs_attr_pwrite partial write (%lld: " + "%lld <> %d)", (long long)offset, + (long long)size, res); + if (res <= 0) { + res = -errno; + goto exit; + } + size -= res; + offset += res; + total += res; + } + res = total; +exit: + if (na) + ntfs_attr_close(na); + return res; +} + +/* + * Shrink the size of a data attribute if needed + * + * For non-resident attributes only. + * The space remains allocated. + * + * Returns 0 if successful + * -1 if failed, with errno telling why + */ + + +int ntfs_attr_shrink_size(ntfs_inode *ni, ntfschar *stream_name, + int stream_name_len, off_t offset) +{ + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *a; + int res; + + res = -1; + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (ctx) { + if (!ntfs_attr_lookup(AT_DATA, stream_name, stream_name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + a = ctx->attr; + + if (a->non_resident + && (sle64_to_cpu(a->initialized_size) > offset)) { + a->initialized_size = cpu_to_sle64(offset); + a->data_size = a->initialized_size; + } + res = 0; + } + ntfs_attr_put_search_ctx(ctx); + } + return (res); +} + +int ntfs_attr_exist(ntfs_inode *ni, const ATTR_TYPES type, const ntfschar *name, + u32 name_len) +{ + ntfs_attr_search_ctx *ctx; + int ret; + + ntfs_log_trace("Entering\n"); + + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return 0; + + ret = ntfs_attr_lookup(type, name, name_len, CASE_SENSITIVE, 0, NULL, 0, + ctx); + + ntfs_attr_put_search_ctx(ctx); + + return !ret; +} + +int ntfs_attr_remove(ntfs_inode *ni, const ATTR_TYPES type, ntfschar *name, + u32 name_len) +{ + ntfs_attr *na; + int ret; + + ntfs_log_trace("Entering\n"); + + if (!ni) { + ntfs_log_error("%s: NULL inode pointer", __FUNCTION__); + errno = EINVAL; + return -1; + } + + na = ntfs_attr_open(ni, type, name, name_len); + if (!na) { + /* do not log removal of non-existent stream */ + if (type != AT_DATA) { + ntfs_log_perror("Failed to open attribute 0x%02x of inode " + "0x%llx", le32_to_cpu(type), (unsigned long long)ni->mft_no); + } + return -1; + } + + ret = ntfs_attr_rm(na); + if (ret) + ntfs_log_perror("Failed to remove attribute 0x%02x of inode " + "0x%llx", le32_to_cpu(type), (unsigned long long)ni->mft_no); + ntfs_attr_close(na); + + return ret; +} + +/* Below macros are 32-bit ready. */ +#define BCX(x) ((x) - (((x) >> 1) & 0x77777777) - \ + (((x) >> 2) & 0x33333333) - \ + (((x) >> 3) & 0x11111111)) +#define BITCOUNT(x) (((BCX(x) + (BCX(x) >> 4)) & 0x0F0F0F0F) % 255) + +static u8 *ntfs_init_lut256(void) +{ + int i; + u8 *lut; + + lut = ntfs_malloc(256); + if (lut) + for(i = 0; i < 256; i++) + *(lut + i) = 8 - BITCOUNT(i); + return lut; +} + +s64 ntfs_attr_get_free_bits(ntfs_attr *na) +{ + u8 *buf, *lut; + s64 br = 0; + s64 total = 0; + s64 nr_free = 0; + + lut = ntfs_init_lut256(); + if (!lut) + return -1; + + buf = ntfs_malloc(65536); + if (!buf) + goto out; + + while (1) { + u32 *p; + br = ntfs_attr_pread(na, total, 65536, buf); + if (br <= 0) + break; + total += br; + p = (u32 *)buf + br / 4 - 1; + for (; (u8 *)p >= buf; p--) { + nr_free += lut[ *p & 255] + + lut[(*p >> 8) & 255] + + lut[(*p >> 16) & 255] + + lut[(*p >> 24) ]; + } + switch (br % 4) { + case 3: nr_free += lut[*(buf + br - 3)]; + /* FALLTHRU */ + case 2: nr_free += lut[*(buf + br - 2)]; + /* FALLTHRU */ + case 1: nr_free += lut[*(buf + br - 1)]; + } + } + free(buf); +out: + free(lut); + if (!total || br < 0) + return -1; + return nr_free; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/attrlist.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/attrlist.c new file mode 100644 index 0000000..0cb8473 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/attrlist.c @@ -0,0 +1,314 @@ +/** + * attrlist.c - Attribute list attribute handling code. Originated from the Linux-NTFS + * project. + * + * Copyright (c) 2004-2005 Anton Altaparmakov + * Copyright (c) 2004-2005 Yura Pakhuchiy + * Copyright (c) 2006 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "types.h" +#include "layout.h" +#include "attrib.h" +#include "attrlist.h" +#include "debug.h" +#include "unistr.h" +#include "logging.h" +#include "misc.h" + +/** + * ntfs_attrlist_need - check whether inode need attribute list + * @ni: opened ntfs inode for which perform check + * + * Check whether all are attributes belong to one MFT record, in that case + * attribute list is not needed. + * + * Return 1 if inode need attribute list, 0 if not, -1 on error with errno set + * to the error code. If function succeed errno set to 0. The following error + * codes are defined: + * EINVAL - Invalid arguments passed to function or attribute haven't got + * attribute list. + */ +int ntfs_attrlist_need(ntfs_inode *ni) +{ + ATTR_LIST_ENTRY *ale; + + if (!ni) { + ntfs_log_trace("Invalid arguments.\n"); + errno = EINVAL; + return -1; + } + + ntfs_log_trace("Entering for inode 0x%llx.\n", (long long) ni->mft_no); + + if (!NInoAttrList(ni)) { + ntfs_log_trace("Inode haven't got attribute list.\n"); + errno = EINVAL; + return -1; + } + + if (!ni->attr_list) { + ntfs_log_trace("Corrupt in-memory struct.\n"); + errno = EINVAL; + return -1; + } + + errno = 0; + ale = (ATTR_LIST_ENTRY *)ni->attr_list; + while ((u8*)ale < ni->attr_list + ni->attr_list_size) { + if (MREF_LE(ale->mft_reference) != ni->mft_no) + return 1; + ale = (ATTR_LIST_ENTRY *)((u8*)ale + le16_to_cpu(ale->length)); + } + return 0; +} + +/** + * ntfs_attrlist_entry_add - add an attribute list attribute entry + * @ni: opened ntfs inode, which contains that attribute + * @attr: attribute record to add to attribute list + * + * Return 0 on success and -1 on error with errno set to the error code. The + * following error codes are defined: + * EINVAL - Invalid arguments passed to function. + * ENOMEM - Not enough memory to allocate necessary buffers. + * EIO - I/O error occurred or damaged filesystem. + * EEXIST - Such attribute already present in attribute list. + */ +int ntfs_attrlist_entry_add(ntfs_inode *ni, ATTR_RECORD *attr) +{ + ATTR_LIST_ENTRY *ale; + leMFT_REF mref; + ntfs_attr *na = NULL; + ntfs_attr_search_ctx *ctx; + u8 *new_al; + int entry_len, entry_offset, err; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x.\n", + (long long) ni->mft_no, + (unsigned) le32_to_cpu(attr->type)); + + if (!ni || !attr) { + ntfs_log_trace("Invalid arguments.\n"); + errno = EINVAL; + return -1; + } + + mref = MK_LE_MREF(ni->mft_no, le16_to_cpu(ni->mrec->sequence_number)); + + if (ni->nr_extents == -1) + ni = ni->base_ni; + + if (!NInoAttrList(ni)) { + ntfs_log_trace("Attribute list isn't present.\n"); + errno = ENOENT; + return -1; + } + + /* Determine size and allocate memory for new attribute list. */ + entry_len = (offsetof(ATTR_LIST_ENTRY, name) + sizeof(ntfschar) * + attr->name_length + 7) & ~7; + new_al = ntfs_calloc(ni->attr_list_size + entry_len); + if (!new_al) + return -1; + + /* Find place for the new entry. */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) { + err = errno; + goto err_out; + } + if (!ntfs_attr_lookup(attr->type, (attr->name_length) ? (ntfschar*) + ((u8*)attr + le16_to_cpu(attr->name_offset)) : + AT_UNNAMED, attr->name_length, CASE_SENSITIVE, + (attr->non_resident) ? sle64_to_cpu(attr->lowest_vcn) : + 0, (attr->non_resident) ? NULL : ((u8*)attr + + le16_to_cpu(attr->value_offset)), (attr->non_resident) ? + 0 : le32_to_cpu(attr->value_length), ctx)) { + /* Found some extent, check it to be before new extent. */ + if (ctx->al_entry->lowest_vcn == attr->lowest_vcn) { + err = EEXIST; + ntfs_log_trace("Such attribute already present in the " + "attribute list.\n"); + ntfs_attr_put_search_ctx(ctx); + goto err_out; + } + /* Add new entry after this extent. */ + ale = (ATTR_LIST_ENTRY*)((u8*)ctx->al_entry + + le16_to_cpu(ctx->al_entry->length)); + } else { + /* Check for real errors. */ + if (errno != ENOENT) { + err = errno; + ntfs_log_trace("Attribute lookup failed.\n"); + ntfs_attr_put_search_ctx(ctx); + goto err_out; + } + /* No previous extents found. */ + ale = ctx->al_entry; + } + /* Don't need it anymore, @ctx->al_entry points to @ni->attr_list. */ + ntfs_attr_put_search_ctx(ctx); + + /* Determine new entry offset. */ + entry_offset = ((u8 *)ale - ni->attr_list); + /* Set pointer to new entry. */ + ale = (ATTR_LIST_ENTRY *)(new_al + entry_offset); + /* Zero it to fix valgrind warning. */ + memset(ale, 0, entry_len); + /* Form new entry. */ + ale->type = attr->type; + ale->length = cpu_to_le16(entry_len); + ale->name_length = attr->name_length; + ale->name_offset = offsetof(ATTR_LIST_ENTRY, name); + if (attr->non_resident) + ale->lowest_vcn = attr->lowest_vcn; + else + ale->lowest_vcn = const_cpu_to_sle64(0); + ale->mft_reference = mref; + ale->instance = attr->instance; + memcpy(ale->name, (u8 *)attr + le16_to_cpu(attr->name_offset), + attr->name_length * sizeof(ntfschar)); + + /* Resize $ATTRIBUTE_LIST to new length. */ + na = ntfs_attr_open(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0); + if (!na) { + err = errno; + ntfs_log_trace("Failed to open $ATTRIBUTE_LIST attribute.\n"); + goto err_out; + } + if (ntfs_attr_truncate(na, ni->attr_list_size + entry_len)) { + err = errno; + ntfs_log_trace("$ATTRIBUTE_LIST resize failed.\n"); + goto err_out; + } + + /* Copy entries from old attribute list to new. */ + memcpy(new_al, ni->attr_list, entry_offset); + memcpy(new_al + entry_offset + entry_len, ni->attr_list + + entry_offset, ni->attr_list_size - entry_offset); + + /* Set new runlist. */ + free(ni->attr_list); + ni->attr_list = new_al; + ni->attr_list_size = ni->attr_list_size + entry_len; + NInoAttrListSetDirty(ni); + /* Done! */ + ntfs_attr_close(na); + return 0; +err_out: + if (na) + ntfs_attr_close(na); + free(new_al); + errno = err; + return -1; +} + +/** + * ntfs_attrlist_entry_rm - remove an attribute list attribute entry + * @ctx: attribute search context describing the attribute list entry + * + * Remove the attribute list entry @ctx->al_entry from the attribute list. + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +int ntfs_attrlist_entry_rm(ntfs_attr_search_ctx *ctx) +{ + u8 *new_al; + int new_al_len; + ntfs_inode *base_ni; + ntfs_attr *na; + ATTR_LIST_ENTRY *ale; + int err; + + if (!ctx || !ctx->ntfs_ino || !ctx->al_entry) { + ntfs_log_trace("Invalid arguments.\n"); + errno = EINVAL; + return -1; + } + + if (ctx->base_ntfs_ino) + base_ni = ctx->base_ntfs_ino; + else + base_ni = ctx->ntfs_ino; + ale = ctx->al_entry; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x, lowest_vcn %lld.\n", + (long long) ctx->ntfs_ino->mft_no, + (unsigned) le32_to_cpu(ctx->al_entry->type), + (long long) sle64_to_cpu(ctx->al_entry->lowest_vcn)); + + if (!NInoAttrList(base_ni)) { + ntfs_log_trace("Attribute list isn't present.\n"); + errno = ENOENT; + return -1; + } + + /* Allocate memory for new attribute list. */ + new_al_len = base_ni->attr_list_size - le16_to_cpu(ale->length); + new_al = ntfs_calloc(new_al_len); + if (!new_al) + return -1; + + /* Reisze $ATTRIBUTE_LIST to new length. */ + na = ntfs_attr_open(base_ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0); + if (!na) { + err = errno; + ntfs_log_trace("Failed to open $ATTRIBUTE_LIST attribute.\n"); + goto err_out; + } + if (ntfs_attr_truncate(na, new_al_len)) { + err = errno; + ntfs_log_trace("$ATTRIBUTE_LIST resize failed.\n"); + goto err_out; + } + + /* Copy entries from old attribute list to new. */ + memcpy(new_al, base_ni->attr_list, (u8*)ale - base_ni->attr_list); + memcpy(new_al + ((u8*)ale - base_ni->attr_list), (u8*)ale + le16_to_cpu( + ale->length), new_al_len - ((u8*)ale - base_ni->attr_list)); + + /* Set new runlist. */ + free(base_ni->attr_list); + base_ni->attr_list = new_al; + base_ni->attr_list_size = new_al_len; + NInoAttrListSetDirty(base_ni); + /* Done! */ + ntfs_attr_close(na); + return 0; +err_out: + if (na) + ntfs_attr_close(na); + free(new_al); + errno = err; + return -1; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/bitmap.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/bitmap.c new file mode 100644 index 0000000..65162a2 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/bitmap.c @@ -0,0 +1,300 @@ +/** + * bitmap.c - Bitmap handling code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002-2006 Anton Altaparmakov + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2004-2008 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "types.h" +#include "attrib.h" +#include "bitmap.h" +#include "debug.h" +#include "logging.h" +#include "misc.h" + +/** + * ntfs_bit_set - set a bit in a field of bits + * @bitmap: field of bits + * @bit: bit to set + * @new_value: value to set bit to (0 or 1) + * + * Set the bit @bit in the @bitmap to @new_value. Ignore all errors. + */ +void ntfs_bit_set(u8 *bitmap, const u64 bit, const u8 new_value) +{ + if (!bitmap || new_value > 1) + return; + if (!new_value) + bitmap[bit >> 3] &= ~(1 << (bit & 7)); + else + bitmap[bit >> 3] |= (1 << (bit & 7)); +} + +/** + * ntfs_bit_get - get value of a bit in a field of bits + * @bitmap: field of bits + * @bit: bit to get + * + * Get and return the value of the bit @bit in @bitmap (0 or 1). + * Return -1 on error. + */ +char ntfs_bit_get(const u8 *bitmap, const u64 bit) +{ + if (!bitmap) + return -1; + return (bitmap[bit >> 3] >> (bit & 7)) & 1; +} + +/** + * ntfs_bit_get_and_set - get value of a bit in a field of bits and set it + * @bitmap: field of bits + * @bit: bit to get/set + * @new_value: value to set bit to (0 or 1) + * + * Return the value of the bit @bit and set it to @new_value (0 or 1). + * Return -1 on error. + */ +char ntfs_bit_get_and_set(u8 *bitmap, const u64 bit, const u8 new_value) +{ + register u8 old_bit, shift; + + if (!bitmap || new_value > 1) + return -1; + shift = bit & 7; + old_bit = (bitmap[bit >> 3] >> shift) & 1; + if (new_value != old_bit) + bitmap[bit >> 3] ^= 1 << shift; + return old_bit; +} + +/** + * ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value + * @na: attribute containing the bitmap + * @start_bit: first bit to set + * @count: number of bits to set + * @value: value to set the bits to (i.e. 0 or 1) + * + * Set @count bits starting at bit @start_bit in the bitmap described by the + * attribute @na to @value, where @value is either 0 or 1. + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +static int ntfs_bitmap_set_bits_in_run(ntfs_attr *na, s64 start_bit, + s64 count, int value) +{ + s64 bufsize, br; + u8 *buf, *lastbyte_buf; + int bit, firstbyte, lastbyte, lastbyte_pos, tmp, ret = -1; + + if (!na || start_bit < 0 || count < 0) { + errno = EINVAL; + ntfs_log_perror("%s: Invalid argument (%p, %lld, %lld)", + __FUNCTION__, na, (long long)start_bit, (long long)count); + return -1; + } + + bit = start_bit & 7; + if (bit) + firstbyte = 1; + else + firstbyte = 0; + + /* Calculate the required buffer size in bytes, capping it at 8kiB. */ + bufsize = ((count - (bit ? 8 - bit : 0) + 7) >> 3) + firstbyte; + if (bufsize > 8192) + bufsize = 8192; + + buf = ntfs_malloc(bufsize); + if (!buf) + return -1; + + /* Depending on @value, zero or set all bits in the allocated buffer. */ + memset(buf, value ? 0xff : 0, bufsize); + + /* If there is a first partial byte... */ + if (bit) { + /* read it in... */ + br = ntfs_attr_pread(na, start_bit >> 3, 1, buf); + if (br != 1) { + if (br >= 0) + errno = EIO; + goto free_err_out; + } + /* and set or clear the appropriate bits in it. */ + while ((bit & 7) && count--) { + if (value) + *buf |= 1 << bit++; + else + *buf &= ~(1 << bit++); + } + /* Update @start_bit to the new position. */ + start_bit = (start_bit + 7) & ~7; + } + + /* Loop until @count reaches zero. */ + lastbyte = 0; + lastbyte_buf = NULL; + bit = count & 7; + do { + /* If there is a last partial byte... */ + if (count > 0 && bit) { + lastbyte_pos = ((count + 7) >> 3) + firstbyte; + if (!lastbyte_pos) { + // FIXME: Eeek! BUG! + ntfs_log_error("Lastbyte is zero. Leaving " + "inconsistent metadata.\n"); + errno = EIO; + goto free_err_out; + } + /* and it is in the currently loaded bitmap window... */ + if (lastbyte_pos <= bufsize) { + lastbyte_buf = buf + lastbyte_pos - 1; + + /* read the byte in... */ + br = ntfs_attr_pread(na, (start_bit + count) >> + 3, 1, lastbyte_buf); + if (br != 1) { + // FIXME: Eeek! We need rollback! (AIA) + if (br >= 0) + errno = EIO; + ntfs_log_perror("Reading of last byte " + "failed (%lld). Leaving inconsistent " + "metadata", (long long)br); + goto free_err_out; + } + /* and set/clear the appropriate bits in it. */ + while (bit && count--) { + if (value) + *lastbyte_buf |= 1 << --bit; + else + *lastbyte_buf &= ~(1 << --bit); + } + /* We don't want to come back here... */ + bit = 0; + /* We have a last byte that we have handled. */ + lastbyte = 1; + } + } + + /* Write the prepared buffer to disk. */ + tmp = (start_bit >> 3) - firstbyte; + br = ntfs_attr_pwrite(na, tmp, bufsize, buf); + if (br != bufsize) { + // FIXME: Eeek! We need rollback! (AIA) + if (br >= 0) + errno = EIO; + ntfs_log_perror("Failed to write buffer to bitmap " + "(%lld != %lld). Leaving inconsistent metadata", + (long long)br, (long long)bufsize); + goto free_err_out; + } + + /* Update counters. */ + tmp = (bufsize - firstbyte - lastbyte) << 3; + if (firstbyte) { + firstbyte = 0; + /* + * Re-set the partial first byte so a subsequent write + * of the buffer does not have stale, incorrect bits. + */ + *buf = value ? 0xff : 0; + } + start_bit += tmp; + count -= tmp; + if (bufsize > (tmp = (count + 7) >> 3)) + bufsize = tmp; + + if (lastbyte && count != 0) { + // FIXME: Eeek! BUG! + ntfs_log_error("Last buffer but count is not zero " + "(%lld). Leaving inconsistent metadata.\n", + (long long)count); + errno = EIO; + goto free_err_out; + } + } while (count > 0); + + ret = 0; + +free_err_out: + free(buf); + return ret; +} + +/** + * ntfs_bitmap_set_run - set a run of bits in a bitmap + * @na: attribute containing the bitmap + * @start_bit: first bit to set + * @count: number of bits to set + * + * Set @count bits starting at bit @start_bit in the bitmap described by the + * attribute @na. + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +int ntfs_bitmap_set_run(ntfs_attr *na, s64 start_bit, s64 count) +{ + int ret; + + ntfs_log_enter("Set from bit %lld, count %lld\n", + (long long)start_bit, (long long)count); + ret = ntfs_bitmap_set_bits_in_run(na, start_bit, count, 1); + ntfs_log_leave("\n"); + return ret; +} + +/** + * ntfs_bitmap_clear_run - clear a run of bits in a bitmap + * @na: attribute containing the bitmap + * @start_bit: first bit to clear + * @count: number of bits to clear + * + * Clear @count bits starting at bit @start_bit in the bitmap described by the + * attribute @na. + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +int ntfs_bitmap_clear_run(ntfs_attr *na, s64 start_bit, s64 count) +{ + int ret; + + ntfs_log_enter("Clear from bit %lld, count %lld\n", + (long long)start_bit, (long long)count); + ret = ntfs_bitmap_set_bits_in_run(na, start_bit, count, 0); + ntfs_log_leave("\n"); + return ret; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/bootsect.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/bootsect.c new file mode 100644 index 0000000..279247d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/bootsect.c @@ -0,0 +1,310 @@ +/** + * bootsect.c - Boot sector handling code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2006 Anton Altaparmakov + * Copyright (c) 2003-2008 Szabolcs Szakacsits + * Copyright (c) 2005 Yura Pakhuchiy + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "param.h" +#include "compat.h" +#include "bootsect.h" +#include "debug.h" +#include "logging.h" + +/** + * ntfs_boot_sector_is_ntfs - check if buffer contains a valid ntfs boot sector + * @b: buffer containing putative boot sector to analyze + * @silent: if zero, output progress messages to stderr + * + * Check if the buffer @b contains a valid ntfs boot sector. The buffer @b + * must be at least 512 bytes in size. + * + * If @silent is zero, output progress messages to stderr. Otherwise, do not + * output any messages (except when configured with --enable-debug in which + * case warning/debug messages may be displayed). + * + * Return TRUE if @b contains a valid ntfs boot sector and FALSE if not. + */ +BOOL ntfs_boot_sector_is_ntfs(NTFS_BOOT_SECTOR *b) +{ + u32 i; + BOOL ret = FALSE; + u16 sectors_per_cluster; + + ntfs_log_debug("Beginning bootsector check.\n"); + + ntfs_log_debug("Checking OEMid, NTFS signature.\n"); + if (b->oem_id != const_cpu_to_le64(0x202020205346544eULL)) { /* "NTFS " */ + ntfs_log_error("NTFS signature is missing.\n"); + goto not_ntfs; + } + + ntfs_log_debug("Checking bytes per sector.\n"); + if (le16_to_cpu(b->bpb.bytes_per_sector) < 256 || + le16_to_cpu(b->bpb.bytes_per_sector) > 4096) { + ntfs_log_error("Unexpected bytes per sector value (%d).\n", + le16_to_cpu(b->bpb.bytes_per_sector)); + goto not_ntfs; + } + + ntfs_log_debug("Checking sectors per cluster.\n"); + switch (b->bpb.sectors_per_cluster) { + case 1: case 2: case 4: case 8: case 16: case 32: case 64: case 128: + break; + default: + if ((b->bpb.sectors_per_cluster < 240) + || (b->bpb.sectors_per_cluster > 253)) { + if (b->bpb.sectors_per_cluster > 128) + ntfs_log_error("Unexpected sectors" + " per cluster value (code 0x%x)\n", + b->bpb.sectors_per_cluster); + else + ntfs_log_error("Unexpected sectors" + " per cluster value (%d).\n", + b->bpb.sectors_per_cluster); + goto not_ntfs; + } + } + + ntfs_log_debug("Checking cluster size.\n"); + if (b->bpb.sectors_per_cluster > 128) + sectors_per_cluster = 1 << (256 - b->bpb.sectors_per_cluster); + else + sectors_per_cluster = b->bpb.sectors_per_cluster; + i = (u32)le16_to_cpu(b->bpb.bytes_per_sector) * sectors_per_cluster; + if (i > NTFS_MAX_CLUSTER_SIZE) { + ntfs_log_error("Unexpected cluster size (%d).\n", i); + goto not_ntfs; + } + + ntfs_log_debug("Checking reserved fields are zero.\n"); + if (le16_to_cpu(b->bpb.reserved_sectors) || + le16_to_cpu(b->bpb.root_entries) || + le16_to_cpu(b->bpb.sectors) || + le16_to_cpu(b->bpb.sectors_per_fat) || + le32_to_cpu(b->bpb.large_sectors) || + b->bpb.fats) { + ntfs_log_error("Reserved fields aren't zero " + "(%d, %d, %d, %d, %d, %d).\n", + le16_to_cpu(b->bpb.reserved_sectors), + le16_to_cpu(b->bpb.root_entries), + le16_to_cpu(b->bpb.sectors), + le16_to_cpu(b->bpb.sectors_per_fat), + le32_to_cpu(b->bpb.large_sectors), + b->bpb.fats); + goto not_ntfs; + } + + ntfs_log_debug("Checking clusters per mft record.\n"); + if ((u8)b->clusters_per_mft_record < 0xe1 || + (u8)b->clusters_per_mft_record > 0xf7) { + switch (b->clusters_per_mft_record) { + case 1: case 2: case 4: case 8: case 0x10: case 0x20: case 0x40: + break; + default: + ntfs_log_error("Unexpected clusters per mft record " + "(%d).\n", b->clusters_per_mft_record); + goto not_ntfs; + } + } + + ntfs_log_debug("Checking clusters per index block.\n"); + if ((u8)b->clusters_per_index_record < 0xe1 || + (u8)b->clusters_per_index_record > 0xf7) { + switch (b->clusters_per_index_record) { + case 1: case 2: case 4: case 8: case 0x10: case 0x20: case 0x40: + break; + default: + ntfs_log_error("Unexpected clusters per index record " + "(%d).\n", b->clusters_per_index_record); + goto not_ntfs; + } + } + + /* MFT and MFTMirr may not overlap the boot sector or be the same */ + if (((s64)sle64_to_cpu(b->mft_lcn) <= 0) + || ((s64)sle64_to_cpu(b->mftmirr_lcn) <= 0) + || (b->mft_lcn == b->mftmirr_lcn)) { + ntfs_log_error("Invalid location of MFT or MFTMirr.\n"); + goto not_ntfs; + } + + if (b->end_of_sector_marker != const_cpu_to_le16(0xaa55)) + ntfs_log_debug("Warning: Bootsector has invalid end of sector " + "marker.\n"); + + ntfs_log_debug("Bootsector check completed successfully.\n"); + + ret = TRUE; +not_ntfs: + return ret; +} + +static const char *last_sector_error = +"HINTS: Either the volume is a RAID/LDM but it wasn't setup yet,\n" +" or it was not setup correctly (e.g. by not using mdadm --build ...),\n" +" or a wrong device is tried to be mounted,\n" +" or the partition table is corrupt (partition is smaller than NTFS),\n" +" or the NTFS boot sector is corrupt (NTFS size is not valid).\n"; + +/** + * ntfs_boot_sector_parse - setup an ntfs volume from an ntfs boot sector + * @vol: ntfs_volume to setup + * @bs: buffer containing ntfs boot sector to parse + * + * Parse the ntfs bootsector @bs and setup the ntfs volume @vol with the + * obtained values. + * + * Return 0 on success or -1 on error with errno set to the error code EINVAL. + */ +int ntfs_boot_sector_parse(ntfs_volume *vol, const NTFS_BOOT_SECTOR *bs) +{ + s64 sectors; + u16 sectors_per_cluster; + s8 c; + + /* We return -1 with errno = EINVAL on error. */ + errno = EINVAL; + + vol->sector_size = le16_to_cpu(bs->bpb.bytes_per_sector); + vol->sector_size_bits = ffs(vol->sector_size) - 1; + ntfs_log_debug("SectorSize = 0x%x\n", vol->sector_size); + ntfs_log_debug("SectorSizeBits = %u\n", vol->sector_size_bits); + /* + * The bounds checks on mft_lcn and mft_mirr_lcn (i.e. them being + * below or equal the number_of_clusters) really belong in the + * ntfs_boot_sector_is_ntfs but in this way we can just do this once. + */ + if (bs->bpb.sectors_per_cluster > 128) + sectors_per_cluster = 1 << (256 - bs->bpb.sectors_per_cluster); + else + sectors_per_cluster = bs->bpb.sectors_per_cluster; + ntfs_log_debug("SectorsPerCluster = 0x%x\n", sectors_per_cluster); + if (sectors_per_cluster & (sectors_per_cluster - 1)) { + ntfs_log_error("sectors_per_cluster (%d) is not a power of 2." + "\n", sectors_per_cluster); + return -1; + } + + sectors = sle64_to_cpu(bs->number_of_sectors); + ntfs_log_debug("NumberOfSectors = %lld\n", (long long)sectors); + if (!sectors) { + ntfs_log_error("Volume size is set to zero.\n"); + return -1; + } + if (vol->dev->d_ops->seek(vol->dev, + (sectors - 1) << vol->sector_size_bits, + SEEK_SET) == -1) { + ntfs_log_perror("Failed to read last sector (%lld)", + (long long)(sectors - 1)); + ntfs_log_error("%s", last_sector_error); + return -1; + } + + vol->nr_clusters = sectors >> (ffs(sectors_per_cluster) - 1); + + vol->mft_lcn = sle64_to_cpu(bs->mft_lcn); + vol->mftmirr_lcn = sle64_to_cpu(bs->mftmirr_lcn); + ntfs_log_debug("MFT LCN = %lld\n", (long long)vol->mft_lcn); + ntfs_log_debug("MFTMirr LCN = %lld\n", (long long)vol->mftmirr_lcn); + if ((vol->mft_lcn < 0 || vol->mft_lcn > vol->nr_clusters) || + (vol->mftmirr_lcn < 0 || vol->mftmirr_lcn > vol->nr_clusters)) { + ntfs_log_error("$MFT LCN (%lld) or $MFTMirr LCN (%lld) is " + "greater than the number of clusters (%lld).\n", + (long long)vol->mft_lcn, (long long)vol->mftmirr_lcn, + (long long)vol->nr_clusters); + return -1; + } + + vol->cluster_size = sectors_per_cluster * vol->sector_size; + if (vol->cluster_size & (vol->cluster_size - 1)) { + ntfs_log_error("cluster_size (%d) is not a power of 2.\n", + vol->cluster_size); + return -1; + } + vol->cluster_size_bits = ffs(vol->cluster_size) - 1; + /* + * Need to get the clusters per mft record and handle it if it is + * negative. Then calculate the mft_record_size. A value of 0x80 is + * illegal, thus signed char is actually ok! + */ + c = bs->clusters_per_mft_record; + ntfs_log_debug("ClusterSize = 0x%x\n", (unsigned)vol->cluster_size); + ntfs_log_debug("ClusterSizeBits = %u\n", vol->cluster_size_bits); + ntfs_log_debug("ClustersPerMftRecord = 0x%x\n", c); + /* + * When clusters_per_mft_record is negative, it means that it is to + * be taken to be the negative base 2 logarithm of the mft_record_size + * min bytes. Then: + * mft_record_size = 2^(-clusters_per_mft_record) bytes. + */ + if (c < 0) + vol->mft_record_size = 1 << -c; + else + vol->mft_record_size = c << vol->cluster_size_bits; + if (vol->mft_record_size & (vol->mft_record_size - 1)) { + ntfs_log_error("mft_record_size (%d) is not a power of 2.\n", + vol->mft_record_size); + return -1; + } + vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1; + ntfs_log_debug("MftRecordSize = 0x%x\n", (unsigned)vol->mft_record_size); + ntfs_log_debug("MftRecordSizeBits = %u\n", vol->mft_record_size_bits); + /* Same as above for INDX record. */ + c = bs->clusters_per_index_record; + ntfs_log_debug("ClustersPerINDXRecord = 0x%x\n", c); + if (c < 0) + vol->indx_record_size = 1 << -c; + else + vol->indx_record_size = c << vol->cluster_size_bits; + vol->indx_record_size_bits = ffs(vol->indx_record_size) - 1; + ntfs_log_debug("INDXRecordSize = 0x%x\n", (unsigned)vol->indx_record_size); + ntfs_log_debug("INDXRecordSizeBits = %u\n", vol->indx_record_size_bits); + /* + * Work out the size of the MFT mirror in number of mft records. If the + * cluster size is less than or equal to the size taken by four mft + * records, the mft mirror stores the first four mft records. If the + * cluster size is bigger than the size taken by four mft records, the + * mft mirror contains as many mft records as will fit into one + * cluster. + */ + if (vol->cluster_size <= 4 * vol->mft_record_size) + vol->mftmirr_size = 4; + else + vol->mftmirr_size = vol->cluster_size / vol->mft_record_size; + return 0; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/cache.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/cache.c new file mode 100644 index 0000000..e577b52 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/cache.c @@ -0,0 +1,606 @@ +/** + * cache.c : deal with LRU caches + * + * Copyright (c) 2008-2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif + +#include "types.h" +#include "security.h" +#include "cache.h" +#include "misc.h" +#include "logging.h" + +/* + * General functions to deal with LRU caches + * + * The cached data have to be organized in a structure in which + * the first fields must follow a mandatory pattern and further + * fields may contain any fixed size data. They are stored in an + * LRU list. + * + * A compare function must be provided for finding a wanted entry + * in the cache. Another function may be provided for invalidating + * an entry to facilitate multiple invalidation. + * + * These functions never return error codes. When there is a + * shortage of memory, data is simply not cached. + * When there is a hashing bug, hashing is dropped, and sequential + * searches are used. + */ + +/* + * Enter a new hash index, after a new record has been inserted + * + * Do not call when a record has been modified (with no key change) + */ + +static void inserthashindex(struct CACHE_HEADER *cache, + struct CACHED_GENERIC *current) +{ + int h; + struct HASH_ENTRY *link; + struct HASH_ENTRY *first; + + if (cache->dohash) { + h = cache->dohash(current); + if ((h >= 0) && (h < cache->max_hash)) { + /* get a free link and insert at top of hash list */ + link = cache->free_hash; + if (link) { + cache->free_hash = link->next; + first = cache->first_hash[h]; + if (first) + link->next = first; + else + link->next = NULL; + link->entry = current; + cache->first_hash[h] = link; + } else { + ntfs_log_error("No more hash entries," + " cache %s hashing dropped\n", + cache->name); + cache->dohash = (cache_hash)NULL; + } + } else { + ntfs_log_error("Illegal hash value," + " cache %s hashing dropped\n", + cache->name); + cache->dohash = (cache_hash)NULL; + } + } +} + +/* + * Drop a hash index when a record is about to be deleted + */ + +static void drophashindex(struct CACHE_HEADER *cache, + const struct CACHED_GENERIC *current, int hash) +{ + struct HASH_ENTRY *link; + struct HASH_ENTRY *previous; + + if (cache->dohash) { + if ((hash >= 0) && (hash < cache->max_hash)) { + /* find the link and unlink */ + link = cache->first_hash[hash]; + previous = (struct HASH_ENTRY*)NULL; + while (link && (link->entry != current)) { + previous = link; + link = link->next; + } + if (link) { + if (previous) + previous->next = link->next; + else + cache->first_hash[hash] = link->next; + link->next = cache->free_hash; + cache->free_hash = link; + } else { + ntfs_log_error("Bad hash list," + " cache %s hashing dropped\n", + cache->name); + cache->dohash = (cache_hash)NULL; + } + } else { + ntfs_log_error("Illegal hash value," + " cache %s hashing dropped\n", + cache->name); + cache->dohash = (cache_hash)NULL; + } + } +} + +/* + * Fetch an entry from cache + * + * returns the cache entry, or NULL if not available + * The returned entry may be modified, but not freed + */ + +struct CACHED_GENERIC *ntfs_fetch_cache(struct CACHE_HEADER *cache, + const struct CACHED_GENERIC *wanted, cache_compare compare) +{ + struct CACHED_GENERIC *current; + struct CACHED_GENERIC *previous; + struct HASH_ENTRY *link; + int h; + + current = (struct CACHED_GENERIC*)NULL; + if (cache) { + if (cache->dohash) { + /* + * When possible, use the hash table to + * locate the entry if present + */ + h = cache->dohash(wanted); + link = cache->first_hash[h]; + while (link && compare(link->entry, wanted)) + link = link->next; + if (link) + current = link->entry; + } + if (!cache->dohash) { + /* + * Search sequentially in LRU list if no hash table + * or if hashing has just failed + */ + current = cache->most_recent_entry; + while (current + && compare(current, wanted)) { + current = current->next; + } + } + if (current) { + previous = current->previous; + cache->hits++; + if (previous) { + /* + * found and not at head of list, unlink from current + * position and relink as head of list + */ + previous->next = current->next; + if (current->next) + current->next->previous + = current->previous; + else + cache->oldest_entry + = current->previous; + current->next = cache->most_recent_entry; + current->previous + = (struct CACHED_GENERIC*)NULL; + cache->most_recent_entry->previous = current; + cache->most_recent_entry = current; + } + } + cache->reads++; + } + return (current); +} + +/* + * Enter an inode number into cache + * returns the cache entry or NULL if not possible + */ + +struct CACHED_GENERIC *ntfs_enter_cache(struct CACHE_HEADER *cache, + const struct CACHED_GENERIC *item, + cache_compare compare) +{ + struct CACHED_GENERIC *current; + struct CACHED_GENERIC *before; + struct HASH_ENTRY *link; + int h; + + current = (struct CACHED_GENERIC*)NULL; + if (cache) { + if (cache->dohash) { + /* + * When possible, use the hash table to + * find out whether the entry if present + */ + h = cache->dohash(item); + link = cache->first_hash[h]; + while (link && compare(link->entry, item)) + link = link->next; + if (link) { + current = link->entry; + } + } + if (!cache->dohash) { + /* + * Search sequentially in LRU list to locate the end, + * and find out whether the entry is already in list + * As we normally go to the end, no statistics is + * kept. + */ + current = cache->most_recent_entry; + while (current + && compare(current, item)) { + current = current->next; + } + } + + if (!current) { + /* + * Not in list, get a free entry or reuse the + * last entry, and relink as head of list + * Note : we assume at least three entries, so + * before, previous and first are different when + * an entry is reused. + */ + + if (cache->free_entry) { + current = cache->free_entry; + cache->free_entry = cache->free_entry->next; + if (item->varsize) { + current->variable = ntfs_malloc( + item->varsize); + } else + current->variable = (void*)NULL; + current->varsize = item->varsize; + if (!cache->oldest_entry) + cache->oldest_entry = current; + } else { + /* reusing the oldest entry */ + current = cache->oldest_entry; + before = current->previous; + before->next = (struct CACHED_GENERIC*)NULL; + if (cache->dohash) + drophashindex(cache,current, + cache->dohash(current)); + if (cache->dofree) + cache->dofree(current); + cache->oldest_entry = current->previous; + if (item->varsize) { + if (current->varsize) + current->variable = realloc( + current->variable, + item->varsize); + else + current->variable = ntfs_malloc( + item->varsize); + } else { + if (current->varsize) + free(current->variable); + current->variable = (void*)NULL; + } + current->varsize = item->varsize; + } + current->next = cache->most_recent_entry; + current->previous = (struct CACHED_GENERIC*)NULL; + if (cache->most_recent_entry) + cache->most_recent_entry->previous = current; + cache->most_recent_entry = current; + memcpy(current->payload, item->payload, cache->fixed_size); + if (item->varsize) { + if (current->variable) { + memcpy(current->variable, + item->variable, item->varsize); + } else { + /* + * no more memory for variable part + * recycle entry in free list + * not an error, just uncacheable + */ + cache->most_recent_entry = current->next; + current->next = cache->free_entry; + cache->free_entry = current; + current = (struct CACHED_GENERIC*)NULL; + } + } else { + current->variable = (void*)NULL; + current->varsize = 0; + } + if (cache->dohash && current) + inserthashindex(cache,current); + } + cache->writes++; + } + return (current); +} + +/* + * Invalidate a cache entry + * The entry is moved to the free entry list + * A specific function may be called for entry deletion + */ + +static void do_invalidate(struct CACHE_HEADER *cache, + struct CACHED_GENERIC *current, int flags) +{ + struct CACHED_GENERIC *previous; + + previous = current->previous; + if ((flags & CACHE_FREE) && cache->dofree) + cache->dofree(current); + /* + * Relink into free list + */ + if (current->next) + current->next->previous = current->previous; + else + cache->oldest_entry = current->previous; + if (previous) + previous->next = current->next; + else + cache->most_recent_entry = current->next; + current->next = cache->free_entry; + cache->free_entry = current; + if (current->variable) + free(current->variable); + current->varsize = 0; + } + + +/* + * Invalidate entries in cache + * + * Several entries may have to be invalidated (at least for inodes + * associated to directories which have been renamed), a different + * compare function may be provided to select entries to invalidate + * + * Returns the number of deleted entries, this can be used by + * the caller to signal a cache corruption if the entry was + * supposed to be found. + */ + +int ntfs_invalidate_cache(struct CACHE_HEADER *cache, + const struct CACHED_GENERIC *item, cache_compare compare, + int flags) +{ + struct CACHED_GENERIC *current; + struct CACHED_GENERIC *next; + struct HASH_ENTRY *link; + int count; + int h; + + current = (struct CACHED_GENERIC*)NULL; + count = 0; + if (cache) { + if (!(flags & CACHE_NOHASH) && cache->dohash) { + /* + * When possible, use the hash table to + * find out whether the entry if present + */ + h = cache->dohash(item); + link = cache->first_hash[h]; + while (link) { + if (compare(link->entry, item)) + link = link->next; + else { + current = link->entry; + link = link->next; + if (current) { + drophashindex(cache,current,h); + do_invalidate(cache, + current,flags); + count++; + } + } + } + } + if ((flags & CACHE_NOHASH) || !cache->dohash) { + /* + * Search sequentially in LRU list + */ + current = cache->most_recent_entry; + while (current) { + if (!compare(current, item)) { + next = current->next; + if (cache->dohash) + drophashindex(cache,current, + cache->dohash(current)); + do_invalidate(cache,current,flags); + current = next; + count++; + } else { + current = current->next; + } + } + } + } + return (count); +} + +int ntfs_remove_cache(struct CACHE_HEADER *cache, + struct CACHED_GENERIC *item, int flags) +{ + int count; + + count = 0; + if (cache) { + if (cache->dohash) + drophashindex(cache,item,cache->dohash(item)); + do_invalidate(cache,item,flags); + count++; + } + return (count); +} + +/* + * Free memory allocated to a cache + */ + +static void ntfs_free_cache(struct CACHE_HEADER *cache) +{ + struct CACHED_GENERIC *entry; + + if (cache) { + for (entry=cache->most_recent_entry; entry; entry=entry->next) { + if (cache->dofree) + cache->dofree(entry); + if (entry->variable) + free(entry->variable); + } + free(cache); + } +} + +/* + * Create a cache + * + * Returns the cache header, or NULL if the cache could not be created + */ + +static struct CACHE_HEADER *ntfs_create_cache(const char *name, + cache_free dofree, cache_hash dohash, + int full_item_size, + int item_count, int max_hash) +{ + struct CACHE_HEADER *cache; + struct CACHED_GENERIC *pc; + struct CACHED_GENERIC *qc; + struct HASH_ENTRY *ph; + struct HASH_ENTRY *qh; + struct HASH_ENTRY **px; + size_t size; + int i; + + size = offsetof(struct CACHE_HEADER, entry) + item_count*full_item_size; + if (max_hash) + size += item_count*sizeof(struct HASH_ENTRY) + + max_hash*sizeof(struct HASH_ENTRY*); + cache = (struct CACHE_HEADER*)ntfs_malloc(size); + if (cache) { + /* header */ + cache->name = name; + cache->dofree = dofree; + if (dohash && max_hash) { + cache->dohash = dohash; + cache->max_hash = max_hash; + } else { + cache->dohash = (cache_hash)NULL; + cache->max_hash = 0; + } + cache->fixed_size = full_item_size - offsetof(struct CACHED_GENERIC, payload); + cache->reads = 0; + cache->writes = 0; + cache->hits = 0; + /* chain the data entries, and mark an invalid entry */ + cache->most_recent_entry = (struct CACHED_GENERIC*)NULL; + cache->oldest_entry = (struct CACHED_GENERIC*)NULL; + cache->free_entry = &cache->entry[0]; + pc = &cache->entry[0]; + for (i=0; i<(item_count - 1); i++) { + qc = (struct CACHED_GENERIC*)((char*)pc + + full_item_size); + pc->next = qc; + pc->variable = (void*)NULL; + pc->varsize = 0; + pc = qc; + } + /* special for the last entry */ + pc->next = (struct CACHED_GENERIC*)NULL; + pc->variable = (void*)NULL; + pc->varsize = 0; + + if (max_hash) { + /* chain the hash entries */ + ph = (struct HASH_ENTRY*)(((char*)pc) + full_item_size); + cache->free_hash = ph; + for (i=0; i<(item_count - 1); i++) { + qh = &ph[1]; + ph->next = qh; + ph = qh; + } + /* special for the last entry */ + if (item_count) { + ph->next = (struct HASH_ENTRY*)NULL; + } + /* create and initialize the hash indexes */ + px = (struct HASH_ENTRY**)&ph[1]; + cache->first_hash = px; + for (i=0; ifree_hash = (struct HASH_ENTRY*)NULL; + cache->first_hash = (struct HASH_ENTRY**)NULL; + } + } + return (cache); +} + +/* + * Create all LRU caches + * + * No error return, if creation is not possible, cacheing will + * just be not available + */ + +void ntfs_create_lru_caches(ntfs_volume *vol) +{ +#if CACHE_INODE_SIZE + /* inode cache */ + vol->xinode_cache = ntfs_create_cache("inode",(cache_free)NULL, + ntfs_dir_inode_hash, sizeof(struct CACHED_INODE), + CACHE_INODE_SIZE, 2*CACHE_INODE_SIZE); +#endif +#if CACHE_NIDATA_SIZE + /* idata cache */ + vol->nidata_cache = ntfs_create_cache("nidata", + ntfs_inode_nidata_free, ntfs_inode_nidata_hash, + sizeof(struct CACHED_NIDATA), + CACHE_NIDATA_SIZE, 2*CACHE_NIDATA_SIZE); +#endif +#if CACHE_LOOKUP_SIZE + /* lookup cache */ + vol->lookup_cache = ntfs_create_cache("lookup", + (cache_free)NULL, ntfs_dir_lookup_hash, + sizeof(struct CACHED_LOOKUP), + CACHE_LOOKUP_SIZE, 2*CACHE_LOOKUP_SIZE); +#endif + vol->securid_cache = ntfs_create_cache("securid",(cache_free)NULL, + (cache_hash)NULL,sizeof(struct CACHED_SECURID), CACHE_SECURID_SIZE, 0); +#if CACHE_LEGACY_SIZE + vol->legacy_cache = ntfs_create_cache("legacy",(cache_free)NULL, + (cache_hash)NULL, sizeof(struct CACHED_PERMISSIONS_LEGACY), CACHE_LEGACY_SIZE, 0); +#endif +} + +/* + * Free all LRU caches + */ + +void ntfs_free_lru_caches(ntfs_volume *vol) +{ +#if CACHE_INODE_SIZE + ntfs_free_cache(vol->xinode_cache); +#endif +#if CACHE_NIDATA_SIZE + ntfs_free_cache(vol->nidata_cache); +#endif +#if CACHE_LOOKUP_SIZE + ntfs_free_cache(vol->lookup_cache); +#endif + ntfs_free_cache(vol->securid_cache); +#if CACHE_LEGACY_SIZE + ntfs_free_cache(vol->legacy_cache); +#endif +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/collate.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/collate.c new file mode 100644 index 0000000..5f7a015 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/collate.c @@ -0,0 +1,271 @@ +/** + * collate.c - NTFS collation handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2004 Anton Altaparmakov + * Copyright (c) 2005 Yura Pakhuchiy + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "attrib.h" +#include "index.h" +#include "collate.h" +#include "debug.h" +#include "unistr.h" +#include "logging.h" + +/** + * ntfs_collate_binary - Which of two binary objects should be listed first + * @vol: unused + * @data1: + * @data1_len: + * @data2: + * @data2_len: + * + * Description... + * + * Returns: + */ +static int ntfs_collate_binary(ntfs_volume *vol __attribute__((unused)), + const void *data1, const int data1_len, + const void *data2, const int data2_len) +{ + int rc; + + ntfs_log_trace("Entering.\n"); + rc = memcmp(data1, data2, min(data1_len, data2_len)); + if (!rc && (data1_len != data2_len)) { + if (data1_len < data2_len) + rc = -1; + else + rc = 1; + } + ntfs_log_trace("Done, returning %i.\n", rc); + return rc; +} + +/** + * ntfs_collate_ntofs_ulong - Which of two long ints should be listed first + * @vol: unused + * @data1: + * @data1_len: + * @data2: + * @data2_len: + * + * Description... + * + * Returns: + */ +static int ntfs_collate_ntofs_ulong(ntfs_volume *vol __attribute__((unused)), + const void *data1, const int data1_len, + const void *data2, const int data2_len) +{ + int rc; + u32 d1, d2; + + ntfs_log_trace("Entering.\n"); + if (data1_len != data2_len || data1_len != 4) { + ntfs_log_error("data1_len or/and data2_len not equal to 4.\n"); + return NTFS_COLLATION_ERROR; + } + d1 = le32_to_cpup(data1); + d2 = le32_to_cpup(data2); + if (d1 < d2) + rc = -1; + else { + if (d1 == d2) + rc = 0; + else + rc = 1; + } + ntfs_log_trace("Done, returning %i.\n", rc); + return rc; +} + +/** + * ntfs_collate_ntofs_ulongs - Which of two le32 arrays should be listed first + * + * Returns: -1, 0 or 1 depending of how the arrays compare + */ + +static int ntfs_collate_ntofs_ulongs(ntfs_volume *vol __attribute__((unused)), + const void *data1, const int data1_len, + const void *data2, const int data2_len) +{ + int rc; + int len; + const le32 *p1, *p2; + u32 d1, d2; + + ntfs_log_trace("Entering.\n"); + if ((data1_len != data2_len) || (data1_len <= 0) || (data1_len & 3)) { + ntfs_log_error("data1_len or data2_len not valid\n"); + return NTFS_COLLATION_ERROR; + } + p1 = (const le32*)data1; + p2 = (const le32*)data2; + len = data1_len; + do { + d1 = le32_to_cpup(p1); + p1++; + d2 = le32_to_cpup(p2); + p2++; + } while ((d1 == d2) && ((len -= 4) > 0)); + if (d1 < d2) + rc = -1; + else { + if (d1 == d2) + rc = 0; + else + rc = 1; + } + ntfs_log_trace("Done, returning %i.\n", rc); + return rc; +} + +/** + * ntfs_collate_ntofs_security_hash - Which of two security descriptors + * should be listed first + * @vol: unused + * @data1: + * @data1_len: + * @data2: + * @data2_len: + * + * JPA compare two security hash keys made of two unsigned le32 + * + * Returns: -1, 0 or 1 depending of how the keys compare + */ +static int ntfs_collate_ntofs_security_hash(ntfs_volume *vol __attribute__((unused)), + const void *data1, const int data1_len, + const void *data2, const int data2_len) +{ + int rc; + u32 d1, d2; + const le32 *p1, *p2; + + ntfs_log_trace("Entering.\n"); + if (data1_len != data2_len || data1_len != 8) { + ntfs_log_error("data1_len or/and data2_len not equal to 8.\n"); + return NTFS_COLLATION_ERROR; + } + p1 = (const le32*)data1; + p2 = (const le32*)data2; + d1 = le32_to_cpup(p1); + d2 = le32_to_cpup(p2); + if (d1 < d2) + rc = -1; + else { + if (d1 > d2) + rc = 1; + else { + p1++; + p2++; + d1 = le32_to_cpup(p1); + d2 = le32_to_cpup(p2); + if (d1 < d2) + rc = -1; + else { + if (d1 > d2) + rc = 1; + else + rc = 0; + } + } + } + ntfs_log_trace("Done, returning %i.\n", rc); + return rc; +} + +/** + * ntfs_collate_file_name - Which of two filenames should be listed first + * @vol: + * @data1: + * @data1_len: unused + * @data2: + * @data2_len: unused + * + * Description... + * + * Returns: + */ +static int ntfs_collate_file_name(ntfs_volume *vol, + const void *data1, const int data1_len __attribute__((unused)), + const void *data2, const int data2_len __attribute__((unused))) +{ + const FILE_NAME_ATTR *file_name_attr1; + const FILE_NAME_ATTR *file_name_attr2; + int rc; + + ntfs_log_trace("Entering.\n"); + file_name_attr1 = (const FILE_NAME_ATTR*)data1; + file_name_attr2 = (const FILE_NAME_ATTR*)data2; + rc = ntfs_names_full_collate( + (ntfschar*)&file_name_attr1->file_name, + file_name_attr1->file_name_length, + (ntfschar*)&file_name_attr2->file_name, + file_name_attr2->file_name_length, + CASE_SENSITIVE, vol->upcase, vol->upcase_len); + ntfs_log_trace("Done, returning %i.\n", rc); + return rc; +} + +/* + * Get a pointer to appropriate collation function. + * + * Returns NULL if the needed function is not implemented + */ + +COLLATE ntfs_get_collate_function(COLLATION_RULES cr) +{ + COLLATE collate; + + switch (cr) { + case COLLATION_BINARY : + collate = ntfs_collate_binary; + break; + case COLLATION_FILE_NAME : + collate = ntfs_collate_file_name; + break; + case COLLATION_NTOFS_SECURITY_HASH : + collate = ntfs_collate_ntofs_security_hash; + break; + case COLLATION_NTOFS_ULONG : + collate = ntfs_collate_ntofs_ulong; + break; + case COLLATION_NTOFS_ULONGS : + collate = ntfs_collate_ntofs_ulongs; + break; + default : + errno = EOPNOTSUPP; + collate = (COLLATE)NULL; + break; + } + return (collate); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/compat.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/compat.c new file mode 100644 index 0000000..63114a4 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/compat.c @@ -0,0 +1,250 @@ +/** + * compat.c - Tweaks for Windows compatibility + * + * Copyright (c) 2002 Richard Russon + * Copyright (c) 2002-2004 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "compat.h" + +#ifndef HAVE_FFS +/** + * ffs - Find the first set bit in an int + * @x: + * + * Description... + * + * Returns: + */ +int ffs(int x) +{ + int r = 1; + + if (!x) + return 0; + if (!(x & 0xffff)) { + x >>= 16; + r += 16; + } + if (!(x & 0xff)) { + x >>= 8; + r += 8; + } + if (!(x & 0xf)) { + x >>= 4; + r += 4; + } + if (!(x & 3)) { + x >>= 2; + r += 2; + } + if (!(x & 1)) { + x >>= 1; + r += 1; + } + return r; +} +#endif /* HAVE_FFS */ + +#ifndef HAVE_DAEMON +/* ************************************************************ + * From: src.opensolaris.org + * src/lib/libresolv2/common/bsd/daemon.c + */ +/* + * Copyright (c) 1997-2000 by Sun Microsystems, Inc. + * All rights reserved. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static const char sccsid[] = "@(#)daemon.c 8.1 (Berkeley) 6/4/93"; +static const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.1 2008-08-16 15:17:44 jpandre Exp $"; +#endif /* LIBC_SCCS and not lint */ + +/* + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif + +int daemon(int nochdir, int noclose) { + int fd; + + switch (fork()) { + case -1: + return (-1); + case 0: + break; + default: + _exit(0); + } + + if (setsid() == -1) + return (-1); + + if (!nochdir) + (void)chdir("/"); + + if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) { + (void)dup2(fd, 0); + (void)dup2(fd, 1); + (void)dup2(fd, 2); + if (fd > 2) + (void)close (fd); + } + return (0); +} +/* + * End: src/lib/libresolv2/common/bsd/daemon.c + *************************************************************/ +#endif /* HAVE_DAEMON */ + +#ifndef HAVE_STRSEP +/* ************************************************************ + * From: src.opensolaris.org + * src/lib/libresolv2/common/bsd/strsep.c + */ +/* + * Copyright (c) 1997, by Sun Microsystems, Inc. + * All rights reserved. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static const char sccsid[] = "strsep.c 8.1 (Berkeley) 6/4/93"; +static const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.1 2008-08-16 15:17:44 jpandre Exp $"; +#endif /* LIBC_SCCS and not lint */ + +/* + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif + +/* + * Get next token from string *stringp, where tokens are possibly-empty + * strings separated by characters from delim. + * + * Writes NULs into the string at *stringp to end tokens. + * delim need not remain constant from call to call. + * On return, *stringp points past the last NUL written (if there might + * be further tokens), or is NULL (if there are definitely no more tokens). + * + * If *stringp is NULL, strsep returns NULL. + */ +char *strsep(char **stringp, const char *delim) { + char *s; + const char *spanp; + int c, sc; + char *tok; + + if ((s = *stringp) == NULL) + return (NULL); + for (tok = s;;) { + c = *s++; + spanp = delim; + do { + if ((sc = *spanp++) == c) { + if (c == 0) + s = NULL; + else + s[-1] = 0; + *stringp = s; + return (tok); + } + } while (sc != 0); + } + /* NOTREACHED */ +} + +/* + * End: src/lib/libresolv2/common/bsd/strsep.c + *************************************************************/ +#endif /* HAVE_STRSEP */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/compress.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/compress.c new file mode 100644 index 0000000..50d9daf --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/compress.c @@ -0,0 +1,2020 @@ +/** + * compress.c - Compressed attribute handling code. Originated from the Linux-NTFS + * project. + * + * Copyright (c) 2004-2005 Anton Altaparmakov + * Copyright (c) 2004-2006 Szabolcs Szakacsits + * Copyright (c) 2005 Yura Pakhuchiy + * Copyright (c) 2009-2014 Jean-Pierre Andre + * Copyright (c) 2014 Eric Biggers + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "attrib.h" +#include "debug.h" +#include "volume.h" +#include "types.h" +#include "layout.h" +#include "runlist.h" +#include "compress.h" +#include "lcnalloc.h" +#include "logging.h" +#include "misc.h" + +#undef le16_to_cpup +/* the standard le16_to_cpup() crashes for unaligned data on some processors */ +#define le16_to_cpup(p) (*(u8*)(p) + (((u8*)(p))[1] << 8)) + +/** + * enum ntfs_compression_constants - constants used in the compression code + */ +typedef enum { + /* Token types and access mask. */ + NTFS_SYMBOL_TOKEN = 0, + NTFS_PHRASE_TOKEN = 1, + NTFS_TOKEN_MASK = 1, + + /* Compression sub-block constants. */ + NTFS_SB_SIZE_MASK = 0x0fff, + NTFS_SB_SIZE = 0x1000, + NTFS_SB_IS_COMPRESSED = 0x8000, +} ntfs_compression_constants; + +/* Match length at or above which ntfs_best_match() will stop searching for + * longer matches. */ +#define NICE_MATCH_LEN 18 + +/* Maximum number of potential matches that ntfs_best_match() will consider at + * each position. */ +#define MAX_SEARCH_DEPTH 24 + +/* log base 2 of the number of entries in the hash table for match-finding. */ +#define HASH_SHIFT 14 + +/* Constant for the multiplicative hash function. */ +#define HASH_MULTIPLIER 0x1E35A7BD + +struct COMPRESS_CONTEXT { + const unsigned char *inbuf; + int bufsize; + int size; + int rel; + int mxsz; + s16 head[1 << HASH_SHIFT]; + s16 prev[NTFS_SB_SIZE]; +} ; + +/* + * Hash the next 3-byte sequence in the input buffer + */ +static inline unsigned int ntfs_hash(const u8 *p) +{ + u32 str; + u32 hash; + +#if defined(__i386__) || defined(__x86_64__) + /* Unaligned access allowed, and little endian CPU. + * Callers ensure that at least 4 (not 3) bytes are remaining. */ + str = *(const u32 *)p & 0xFFFFFF; +#else + str = ((u32)p[0] << 0) | ((u32)p[1] << 8) | ((u32)p[2] << 16); +#endif + + hash = str * HASH_MULTIPLIER; + + /* High bits are more random than the low bits. */ + return hash >> (32 - HASH_SHIFT); +} + +/* + * Search for the longest sequence matching current position + * + * A hash table, each entry of which points to a chain of sequence + * positions sharing the corresponding hash code, is maintained to speed up + * searching for matches. To maintain the hash table, either + * ntfs_best_match() or ntfs_skip_position() has to be called for each + * consecutive position. + * + * This function is heavily used; it has to be optimized carefully. + * + * This function sets pctx->size and pctx->rel to the length and offset, + * respectively, of the longest match found. + * + * The minimum match length is assumed to be 3, and the maximum match + * length is assumed to be pctx->mxsz. If this function produces + * pctx->size < 3, then no match was found. + * + * Note: for the following reasons, this function is not guaranteed to find + * *the* longest match up to pctx->mxsz: + * + * (1) If this function finds a match of NICE_MATCH_LEN bytes or greater, + * it ends early because a match this long is good enough and it's not + * worth spending more time searching. + * + * (2) If this function considers MAX_SEARCH_DEPTH matches with a single + * position, it ends early and returns the longest match found so far. + * This saves a lot of time on degenerate inputs. + */ +static void ntfs_best_match(struct COMPRESS_CONTEXT *pctx, const int i, + int best_len) +{ + const u8 * const inbuf = pctx->inbuf; + const u8 * const strptr = &inbuf[i]; /* String we're matching against */ + s16 * const prev = pctx->prev; + const int max_len = min(pctx->bufsize - i, pctx->mxsz); + const int nice_len = min(NICE_MATCH_LEN, max_len); + int depth_remaining = MAX_SEARCH_DEPTH; + const u8 *best_matchptr = strptr; + unsigned int hash; + s16 cur_match; + const u8 *matchptr; + int len; + + if (max_len < 4) + goto out; + + /* Insert the current sequence into the appropriate hash chain. */ + hash = ntfs_hash(strptr); + cur_match = pctx->head[hash]; + prev[i] = cur_match; + pctx->head[hash] = i; + + if (best_len >= max_len) { + /* Lazy match is being attempted, but there aren't enough length + * bits remaining to code a longer match. */ + goto out; + } + + /* Search the appropriate hash chain for matches. */ + + for (; cur_match >= 0 && depth_remaining--; + cur_match = prev[cur_match]) + { + + matchptr = &inbuf[cur_match]; + + /* Considering the potential match at 'matchptr': is it longer + * than 'best_len'? + * + * The bytes at index 'best_len' are the most likely to differ, + * so check them first. + * + * The bytes at indices 'best_len - 1' and '0' are less + * important to check separately. But doing so still gives a + * slight performance improvement, at least on x86_64, probably + * because they create separate branches for the CPU to predict + * independently of the branches in the main comparison loops. + */ + if (matchptr[best_len] != strptr[best_len] || + matchptr[best_len - 1] != strptr[best_len - 1] || + matchptr[0] != strptr[0]) + goto next_match; + + for (len = 1; len < best_len - 1; len++) + if (matchptr[len] != strptr[len]) + goto next_match; + + /* The match is the longest found so far --- + * at least 'best_len' + 1 bytes. Continue extending it. */ + + best_matchptr = matchptr; + + do { + if (++best_len >= nice_len) { + /* 'nice_len' reached; don't waste time + * searching for longer matches. Extend the + * match as far as possible and terminate the + * search. */ + while (best_len < max_len && + (best_matchptr[best_len] == + strptr[best_len])) + { + best_len++; + } + goto out; + } + } while (best_matchptr[best_len] == strptr[best_len]); + + /* Found a longer match, but 'nice_len' not yet reached. */ + + next_match: + /* Continue to next match in the chain. */ + ; + } + + /* Reached end of chain, or ended early due to reaching the maximum + * search depth. */ + +out: + /* Return the longest match we were able to find. */ + pctx->size = best_len; + pctx->rel = best_matchptr - strptr; /* given as a negative number! */ +} + +/* + * Advance the match-finder, but don't search for matches. + */ +static void ntfs_skip_position(struct COMPRESS_CONTEXT *pctx, const int i) +{ + unsigned int hash; + + if (pctx->bufsize - i < 4) + return; + + /* Insert the current sequence into the appropriate hash chain. */ + hash = ntfs_hash(pctx->inbuf + i); + pctx->prev[i] = pctx->head[hash]; + pctx->head[hash] = i; +} + +/* + * Compress a 4096-byte block + * + * Returns a header of two bytes followed by the compressed data. + * If compression is not effective, the header and an uncompressed + * block is returned. + * + * Note : two bytes may be output before output buffer overflow + * is detected, so a 4100-bytes output buffer must be reserved. + * + * Returns the size of the compressed block, including the + * header (minimal size is 2, maximum size is 4098) + * 0 if an error has been met. + */ + +static unsigned int ntfs_compress_block(const char *inbuf, const int bufsize, + char *outbuf) +{ + struct COMPRESS_CONTEXT *pctx; + int i; /* current position */ + int j; /* end of best match from current position */ + int k; /* end of best match from next position */ + int offs; /* offset to best match */ + int bp; /* bits to store offset */ + int bp_cur; /* saved bits to store offset at current position */ + int mxoff; /* max match offset : 1 << bp */ + unsigned int xout; + unsigned int q; /* aggregated offset and size */ + int have_match; /* do we have a match at the current position? */ + char *ptag; /* location reserved for a tag */ + int tag; /* current value of tag */ + int ntag; /* count of bits still undefined in tag */ + + pctx = ntfs_malloc(sizeof(struct COMPRESS_CONTEXT)); + if (!pctx) { + errno = ENOMEM; + return 0; + } + + /* All hash chains start as empty. The special value '-1' indicates the + * end of each hash chain. */ + memset(pctx->head, 0xFF, sizeof(pctx->head)); + + pctx->inbuf = (const unsigned char*)inbuf; + pctx->bufsize = bufsize; + xout = 2; + i = 0; + bp = 4; + mxoff = 1 << bp; + pctx->mxsz = (1 << (16 - bp)) + 2; + have_match = 0; + tag = 0; + ntag = 8; + ptag = &outbuf[xout++]; + + while ((i < bufsize) && (xout < (NTFS_SB_SIZE + 2))) { + + /* This implementation uses "lazy" parsing: it always chooses + * the longest match, unless the match at the next position is + * longer. This is the same strategy used by the high + * compression modes of zlib. */ + + if (!have_match) { + /* Find the longest match at the current position. But + * first adjust the maximum match length if needed. + * (This loop might need to run more than one time in + * the case that we just output a long match.) */ + while (mxoff < i) { + bp++; + mxoff <<= 1; + pctx->mxsz = (pctx->mxsz + 2) >> 1; + } + ntfs_best_match(pctx, i, 2); + } + + if (pctx->size >= 3) { + + /* Found a match at the current position. */ + + j = i + pctx->size; + bp_cur = bp; + offs = pctx->rel; + + if (pctx->size >= NICE_MATCH_LEN) { + + /* Choose long matches immediately. */ + + q = (~offs << (16 - bp_cur)) + (j - i - 3); + outbuf[xout++] = q & 255; + outbuf[xout++] = (q >> 8) & 255; + tag |= (1 << (8 - ntag)); + + if (j == bufsize) { + /* Shortcut if the match extends to the + * end of the buffer. */ + i = j; + --ntag; + break; + } + i += 1; + do { + ntfs_skip_position(pctx, i); + } while (++i != j); + have_match = 0; + } else { + /* Check for a longer match at the next + * position. */ + + /* Doesn't need to be while() since we just + * adjusted the maximum match length at the + * previous position. */ + if (mxoff < i + 1) { + bp++; + mxoff <<= 1; + pctx->mxsz = (pctx->mxsz + 2) >> 1; + } + ntfs_best_match(pctx, i + 1, pctx->size); + k = i + 1 + pctx->size; + + if (k > (j + 1)) { + /* Next match is longer. + * Output a literal. */ + outbuf[xout++] = inbuf[i++]; + have_match = 1; + } else { + /* Next match isn't longer. + * Output the current match. */ + q = (~offs << (16 - bp_cur)) + + (j - i - 3); + outbuf[xout++] = q & 255; + outbuf[xout++] = (q >> 8) & 255; + tag |= (1 << (8 - ntag)); + + /* The minimum match length is 3, and + * we've run two bytes through the + * matchfinder already. So the minimum + * number of positions we need to skip + * is 1. */ + i += 2; + do { + ntfs_skip_position(pctx, i); + } while (++i != j); + have_match = 0; + } + } + } else { + /* No match at current position. Output a literal. */ + outbuf[xout++] = inbuf[i++]; + have_match = 0; + } + + /* Store the tag if fully used. */ + if (!--ntag) { + *ptag = tag; + ntag = 8; + ptag = &outbuf[xout++]; + tag = 0; + } + } + + /* Store the last tag if partially used. */ + if (ntag == 8) + xout--; + else + *ptag = tag; + + /* Determine whether to store the data compressed or uncompressed. */ + + if ((i >= bufsize) && (xout < (NTFS_SB_SIZE + 2))) { + /* Compressed. */ + outbuf[0] = (xout - 3) & 255; + outbuf[1] = 0xb0 + (((xout - 3) >> 8) & 15); + } else { + /* Uncompressed. */ + memcpy(&outbuf[2], inbuf, bufsize); + if (bufsize < NTFS_SB_SIZE) + memset(&outbuf[bufsize + 2], 0, NTFS_SB_SIZE - bufsize); + outbuf[0] = 0xff; + outbuf[1] = 0x3f; + xout = NTFS_SB_SIZE + 2; + } + + /* Free the compression context and return the total number of bytes + * written to 'outbuf'. */ + free(pctx); + return (xout); +} + +/** + * ntfs_decompress - decompress a compression block into an array of pages + * @dest: buffer to which to write the decompressed data + * @dest_size: size of buffer @dest in bytes + * @cb_start: compression block to decompress + * @cb_size: size of compression block @cb_start in bytes + * + * This decompresses the compression block @cb_start into the destination + * buffer @dest. + * + * @cb_start is a pointer to the compression block which needs decompressing + * and @cb_size is the size of @cb_start in bytes (8-64kiB). + * + * Return 0 on success. On error in the compressed stream, set errno to + * EOVERFLOW and return -1. + */ +static int ntfs_decompress(u8 *dest, const u32 dest_size, + u8 *const cb_start, const u32 cb_size) +{ + /* + * Pointers into the compressed data, i.e. the compression block (cb), + * and the therein contained sub-blocks (sb). + */ + u8 *cb_end = cb_start + cb_size; /* End of cb. */ + u8 *cb = cb_start; /* Current position in cb. */ + u8 *cb_sb_start = cb; /* Beginning of the current sb in the cb. */ + u8 *cb_sb_end; /* End of current sb / beginning of next sb. */ + /* Variables for uncompressed data / destination. */ + u8 *dest_end = dest + dest_size; /* End of dest buffer. */ + u8 *dest_sb_start; /* Start of current sub-block in dest. */ + u8 *dest_sb_end; /* End of current sb in dest. */ + /* Variables for tag and token parsing. */ + u8 tag; /* Current tag. */ + int token; /* Loop counter for the eight tokens in tag. */ + u16 hdr; /* Current sub-block header. */ + + ntfs_log_trace("Entering, cb_size = 0x%x.\n", (unsigned)cb_size); +do_next_sb: + { + ntfs_log_debug("Beginning sub-block at offset = %d in the " + "cb.\n", + (int)(cb - cb_start)); + /* + * Have we reached the end of the compression block or the end + * of the decompressed data? + * + * Check dest == dest_end before reading the next two-byte sub- + * block header. The compressed pointer can legally be near the + * end of the buffer when the requested output has already been + * produced. + */ + if (cb == cb_end || dest == dest_end) + goto complete; + /* A sub-block header is two bytes. */ + if ((size_t)(cb_end - cb) < 2) + goto return_overflow; + hdr = le16_to_cpup((le16*)cb); + if (!hdr) + goto complete; + /* Setup offset for the current sub-block destination. */ + dest_sb_start = dest; + dest_sb_end = dest + NTFS_SB_SIZE; + /* Check that we are still within allowed boundaries. */ + if (dest_sb_end > dest_end) + goto return_overflow; + /* Does the minimum size of a compressed sb overflow valid + * range? */ + if ((size_t)(cb_end - cb) < 6) + goto return_overflow; + /* Setup the current sub-block source pointers and validate + * range. */ + cb_sb_start = cb; + cb_sb_end = cb_sb_start + (hdr & NTFS_SB_SIZE_MASK) + 3; + if (cb_sb_end > cb_end) + goto return_overflow; + /* Now, we are ready to process the current sub-block (sb). */ + if (!(hdr & NTFS_SB_IS_COMPRESSED)) { + ntfs_log_debug("Found uncompressed sub-block.\n"); + /* This sb is not compressed, just copy it into + * destination. */ + /* Advance source position to first data byte. */ + cb += 2; + /* An uncompressed sb must be full size. */ + if (cb_sb_end - cb != NTFS_SB_SIZE) + goto return_overflow; + /* Copy the block and advance the source position. */ + memcpy(dest, cb, NTFS_SB_SIZE); + cb += NTFS_SB_SIZE; + /* Advance destination position to next sub-block. */ + dest += NTFS_SB_SIZE; + goto do_next_sb; + } + ntfs_log_debug("Found compressed sub-block.\n"); + /* This sb is compressed, decompress it into destination. */ + /* Forward to the first tag in the sub-block. */ + cb += 2; +do_next_tag: + { + /* Check we are still in range. */ + if (cb > cb_sb_end || dest > dest_sb_end) + goto return_overflow; + /* + * Finish this compressed sub-block when either its + * compressed input is exhausted or its 4 KiB output + * slot is full. If output becomes full first, skip the + * remaining compressed bytes in this sub-block instead + * of interpreting them as more tags/tokens. + */ + if (cb == cb_sb_end || dest == dest_sb_end) + goto finish_compressed_sb; + /* Get the next tag and advance to first token. */ + tag = *cb++; + /* Parse the eight tokens described by the tag. */ + for (token = 0; token < 8; token++, tag >>= 1) { + u16 lg, pt, length, max_non_overlap; + register u16 i; + u8 *dest_back_addr; + + /* Check if we are done / still in range. */ + if (cb > cb_sb_end || dest > dest_sb_end) + goto return_overflow; + if (cb == cb_sb_end || dest == dest_sb_end) + goto finish_compressed_sb; + /* Determine token type and parse + * appropriately. */ + if ((tag & NTFS_TOKEN_MASK) == + NTFS_SYMBOL_TOKEN) + { + /* + * We have a symbol token, copy the + * symbol across, and advance the source + * and destination positions. + */ + *dest++ = *cb++; + /* Continue with the next token. */ + continue; + } + /* + * We have a phrase token. Make sure it is not + * the first token in the sb as this is illegal + * and would confuse the code below. + */ + if (dest == dest_sb_start) + goto return_overflow; + /* + * A phrase token is a two-byte compressed word. + * The generic token check above only proves + * that at least one byte remains, which is + * enough for a symbol token but not for a + * phrase token. + */ + if ((size_t)(cb_sb_end - cb) < 2) + goto return_overflow; + /* + * Determine the number of bytes to go back (p) + * and the number of bytes to copy (l). We use + * an optimized algorithm in which we first + * calculate log2(current destination position + * in sb), which allows determination of l and p + * in O(1) rather than O(n). We just need an + * arch-optimized log2() function now. + */ + lg = 0; + for (i = dest - dest_sb_start - 1; i >= 0x10; + i >>= 1) + { + lg++; + } + /* Get the phrase token into i. */ + pt = le16_to_cpup((le16*)cb); + /* + * Calculate starting position of the byte + * sequence in the destination using the fact + * that p = (pt >> (12 - lg)) + 1 and make sure + * we don't go too far back. + */ + dest_back_addr = dest - (pt >> (12 - lg)) - 1; + if (dest_back_addr < dest_sb_start) + goto return_overflow; + /* Now calculate the length of the byte + * sequence. */ + length = (pt & (0xfff >> lg)) + 3; + /* Verify destination is in range. */ + if (dest + length > dest_sb_end) + goto return_overflow; + /* The number of non-overlapping bytes. */ + max_non_overlap = dest - dest_back_addr; + if (length <= max_non_overlap) { + /* The byte sequence doesn't overlap, + * just copy it. */ + memcpy(dest, dest_back_addr, length); + /* Advance destination pointer. */ + dest += length; + } else { + /* + * The byte sequence does overlap, copy + * non-overlapping part and then do a + * slow byte by byte copy for the + * overlapping part. Also, advance the + * destination pointer. + */ + memcpy(dest, dest_back_addr, + max_non_overlap); + dest += max_non_overlap; + dest_back_addr += max_non_overlap; + length -= max_non_overlap; + while (length--) + *dest++ = *dest_back_addr++; + } + /* Advance source position and continue with the + * next token. */ + cb += 2; + } + /* No tokens left in the current tag. Continue with the + * next tag. */ + goto do_next_tag; + } + } +finish_compressed_sb: + /* Check if the decompressed sub-block was not full-length. */ + if (dest < dest_sb_end) { + size_t nr_bytes = dest_sb_end - dest; + + ntfs_log_debug("Filling incomplete sub-block with zeroes.\n"); + /* Zero remainder and update destination position. */ + memset(dest, 0, nr_bytes); + dest += nr_bytes; + } + /* + * If the output sub-block filled before the compressed sub-block input + * was exhausted, skip the remaining compressed bytes in this sub-block. + */ + cb = cb_sb_end; + goto do_next_sb; +complete: + if (dest_end > dest) + memset(dest, 0, dest_end - dest); + ntfs_log_debug("Completed. Returning success (0).\n"); + return 0; +return_overflow: + errno = EOVERFLOW; + ntfs_log_perror("Failed to decompress file"); + return -1; +} + +/** + * ntfs_is_cb_compressed - internal function, do not use + * + * This is a very specialised function determining if a cb is compressed or + * uncompressed. It is assumed that checking for a sparse cb has already been + * performed and that the cb is not sparse. It makes all sorts of other + * assumptions as well and hence it is not useful anywhere other than where it + * is used at the moment. Please, do not make this function available for use + * outside of compress.c as it is bound to confuse people and not do what they + * want. + * + * Return TRUE on errors so that the error will be detected later on in the + * code. Might be a bit confusing to debug but there really should never be + * errors coming from here. + */ +static BOOL ntfs_is_cb_compressed(ntfs_attr *na, runlist_element *rl, + VCN cb_start_vcn, int cb_clusters) +{ + /* + * The simplest case: the run starting at @cb_start_vcn contains + * @cb_clusters clusters which are all not sparse, thus the cb is not + * compressed. + */ +restart: + cb_clusters -= rl->length - (cb_start_vcn - rl->vcn); + while (cb_clusters > 0) { + /* Go to the next run. */ + rl++; + /* Map the next runlist fragment if it is not mapped. */ + if (rl->lcn < LCN_HOLE || !rl->length) { + cb_start_vcn = rl->vcn; + rl = ntfs_attr_find_vcn(na, rl->vcn); + if (!rl || rl->lcn < LCN_HOLE || !rl->length) + return TRUE; + /* + * If the runs were merged need to deal with the + * resulting partial run so simply restart. + */ + if (rl->vcn < cb_start_vcn) + goto restart; + } + /* If the current run is sparse, the cb is compressed. */ + if (rl->lcn == LCN_HOLE) + return TRUE; + /* If the whole cb is not sparse, it is not compressed. */ + if (rl->length >= cb_clusters) + return FALSE; + cb_clusters -= rl->length; + }; + /* All cb_clusters were not sparse thus the cb is not compressed. */ + return FALSE; +} + +/** + * ntfs_compressed_attr_pread - read from a compressed attribute + * @na: ntfs attribute to read from + * @pos: byte position in the attribute to begin reading from + * @count: number of bytes to read + * @b: output data buffer + * + * NOTE: You probably want to be using attrib.c::ntfs_attr_pread() instead. + * + * This function will read @count bytes starting at offset @pos from the + * compressed ntfs attribute @na into the data buffer @b. + * + * On success, return the number of successfully read bytes. If this number + * is lower than @count this means that the read reached end of file or that + * an error was encountered during the read so that the read is partial. + * 0 means end of file or nothing was read (also return 0 when @count is 0). + * + * On error and nothing has been read, return -1 with errno set appropriately + * to the return code of ntfs_pread(), or to EINVAL in case of invalid + * arguments. + */ +s64 ntfs_compressed_attr_pread(ntfs_attr *na, s64 pos, s64 count, void *b) +{ + s64 br, to_read, ofs, total, total2; + u64 cb_size_mask; + VCN start_vcn, vcn, end_vcn; + ntfs_volume *vol; + runlist_element *rl; + u8 *dest, *cb, *cb_pos, *cb_end; + u32 cb_size; + int err; + ATTR_FLAGS data_flags; + FILE_ATTR_FLAGS compression; + unsigned int nr_cbs, cb_clusters; + + ntfs_log_trace("Entering for inode 0x%llx, attr 0x%x, pos 0x%llx, count 0x%llx.\n", + (unsigned long long)na->ni->mft_no, le32_to_cpu(na->type), + (long long)pos, (long long)count); + data_flags = na->data_flags; + compression = na->ni->flags & FILE_ATTR_COMPRESSED; + if (!na || !na->ni || !na->ni->vol || !b + || ((data_flags & ATTR_COMPRESSION_MASK) + != ATTR_IS_COMPRESSED) + || pos < 0 || count < 0) { + errno = EINVAL; + return -1; + } + /* + * Encrypted attributes are not supported. We return access denied, + * which is what Windows NT4 does, too. + */ + if (NAttrEncrypted(na)) { + errno = EACCES; + return -1; + } + if (!count) + return 0; + /* Truncate reads beyond end of attribute. */ + if (pos + count > na->data_size) { + if (pos >= na->data_size) { + return 0; + } + count = na->data_size - pos; + } + /* If it is a resident attribute, simply use ntfs_attr_pread(). */ + if (!NAttrNonResident(na)) + return ntfs_attr_pread(na, pos, count, b); + if (na->compression_block_size < NTFS_SB_SIZE) { + ntfs_log_error("Unsupported compression block size %ld\n", + (long)na->compression_block_size); + errno = EOVERFLOW; + return (-1); + } + total = total2 = 0; + /* Zero out reads beyond initialized size. */ + if (pos + count > na->initialized_size) { + if (pos >= na->initialized_size) { + memset(b, 0, count); + return count; + } + total2 = pos + count - na->initialized_size; + count -= total2; + memset((u8*)b + count, 0, total2); + } + vol = na->ni->vol; + cb_size = na->compression_block_size; + cb_size_mask = cb_size - 1UL; + cb_clusters = na->compression_block_clusters; + + /* Need a temporary buffer for each loaded compression block. */ + cb = (u8*)ntfs_malloc(cb_size); + if (!cb) + return -1; + + /* Need a temporary buffer for each uncompressed block. */ + dest = (u8*)ntfs_malloc(cb_size); + if (!dest) { + free(cb); + return -1; + } + /* + * The first vcn in the first compression block (cb) which we need to + * decompress. + */ + start_vcn = (pos & ~cb_size_mask) >> vol->cluster_size_bits; + /* Offset in the uncompressed cb at which to start reading data. */ + ofs = pos & cb_size_mask; + /* + * The first vcn in the cb after the last cb which we need to + * decompress. + */ + end_vcn = ((pos + count + cb_size - 1) & ~cb_size_mask) >> + vol->cluster_size_bits; + /* Number of compression blocks (cbs) in the wanted vcn range. */ + nr_cbs = (end_vcn - start_vcn) << vol->cluster_size_bits >> + na->compression_block_size_bits; + cb_end = cb + cb_size; +do_next_cb: + nr_cbs--; + cb_pos = cb; + vcn = start_vcn; + start_vcn += cb_clusters; + + /* Check whether the compression block is sparse. */ + rl = ntfs_attr_find_vcn(na, vcn); + if (!rl || rl->lcn < LCN_HOLE) { + free(cb); + free(dest); + if (total) + return total; + /* FIXME: Do we want EIO or the error code? (AIA) */ + errno = EIO; + return -1; + } + if (rl->lcn == LCN_HOLE) { + /* Sparse cb, zero out destination range overlapping the cb. */ + ntfs_log_debug("Found sparse compression block.\n"); + to_read = min(count, cb_size - ofs); + memset(b, 0, to_read); + ofs = 0; + total += to_read; + count -= to_read; + b = (u8*)b + to_read; + } else if (!ntfs_is_cb_compressed(na, rl, vcn, cb_clusters)) { + s64 tdata_size, tinitialized_size; + /* + * Uncompressed cb, read it straight into the destination range + * overlapping the cb. + */ + ntfs_log_debug("Found uncompressed compression block.\n"); + /* + * Read the uncompressed data into the destination buffer. + * NOTE: We cheat a little bit here by marking the attribute as + * not compressed in the ntfs_attr structure so that we can + * read the data by simply using ntfs_attr_pread(). (-8 + * NOTE: we have to modify data_size and initialized_size + * temporarily as well... + */ + to_read = min(count, cb_size - ofs); + ofs += vcn << vol->cluster_size_bits; + NAttrClearCompressed(na); + na->data_flags &= ~ATTR_COMPRESSION_MASK; + tdata_size = na->data_size; + tinitialized_size = na->initialized_size; + na->data_size = na->initialized_size = na->allocated_size; + do { + br = ntfs_attr_pread(na, ofs, to_read, b); + if (br <= 0) { + if (!br) { + ntfs_log_error("Failed to read an" + " uncompressed cluster," + " inode %lld offs 0x%llx\n", + (long long)na->ni->mft_no, + (long long)ofs); + errno = EIO; + } + err = errno; + na->data_size = tdata_size; + na->initialized_size = tinitialized_size; + na->ni->flags |= compression; + na->data_flags = data_flags; + free(cb); + free(dest); + if (total) + return total; + errno = err; + return br; + } + total += br; + count -= br; + b = (u8*)b + br; + to_read -= br; + ofs += br; + } while (to_read > 0); + na->data_size = tdata_size; + na->initialized_size = tinitialized_size; + na->ni->flags |= compression; + na->data_flags = data_flags; + ofs = 0; + } else { + s64 tdata_size, tinitialized_size; + u32 decompsz; + + /* + * Compressed cb, decompress it into the temporary buffer, then + * copy the data to the destination range overlapping the cb. + */ + ntfs_log_debug("Found compressed compression block.\n"); + /* + * Read the compressed data into the temporary buffer. + * NOTE: We cheat a little bit here by marking the attribute as + * not compressed in the ntfs_attr structure so that we can + * read the raw, compressed data by simply using + * ntfs_attr_pread(). (-8 + * NOTE: We have to modify data_size and initialized_size + * temporarily as well... + */ + to_read = cb_size; + NAttrClearCompressed(na); + na->data_flags &= ~ATTR_COMPRESSION_MASK; + tdata_size = na->data_size; + tinitialized_size = na->initialized_size; + na->data_size = na->initialized_size = na->allocated_size; + do { + br = ntfs_attr_pread(na, + (vcn << vol->cluster_size_bits) + + (cb_pos - cb), to_read, cb_pos); + if (br <= 0) { + if (!br) { + ntfs_log_error("Failed to read a" + " compressed cluster, " + " inode %lld offs 0x%llx\n", + (long long)na->ni->mft_no, + (long long)(vcn << vol->cluster_size_bits)); + errno = EIO; + } + err = errno; + na->data_size = tdata_size; + na->initialized_size = tinitialized_size; + na->ni->flags |= compression; + na->data_flags = data_flags; + free(cb); + free(dest); + if (total) + return total; + errno = err; + return br; + } + cb_pos += br; + to_read -= br; + } while (to_read > 0); + na->data_size = tdata_size; + na->initialized_size = tinitialized_size; + na->ni->flags |= compression; + na->data_flags = data_flags; + /* Just a precaution. */ + if (cb_pos + 2 <= cb_end) + *(u16*)cb_pos = 0; + ntfs_log_debug("Successfully read the compression block.\n"); + /* Do not decompress beyond the requested block */ + to_read = min(count, cb_size - ofs); + decompsz = ((ofs + to_read - 1) | (NTFS_SB_SIZE - 1)) + 1; + if (ntfs_decompress(dest, decompsz, cb, cb_size) < 0) { + err = errno; + free(cb); + free(dest); + if (total) + return total; + errno = err; + return -1; + } + memcpy(b, dest + ofs, to_read); + total += to_read; + count -= to_read; + b = (u8*)b + to_read; + ofs = 0; + } + /* Do we have more work to do? */ + if (nr_cbs) + goto do_next_cb; + /* We no longer need the buffers. */ + free(cb); + free(dest); + /* Return number of bytes read. */ + return total + total2; +} + +/* + * Read data from a set of clusters + * + * Returns the amount of data read + */ + +static u32 read_clusters(ntfs_volume *vol, const runlist_element *rl, + s64 offs, u32 to_read, char *inbuf) +{ + u32 count; + int xgot; + u32 got; + s64 xpos; + BOOL first; + char *xinbuf; + const runlist_element *xrl; + + got = 0; + xrl = rl; + xinbuf = inbuf; + first = TRUE; + do { + count = xrl->length << vol->cluster_size_bits; + xpos = xrl->lcn << vol->cluster_size_bits; + if (first) { + count -= offs; + xpos += offs; + } + if ((to_read - got) < count) + count = to_read - got; + xgot = ntfs_pread(vol->dev, xpos, count, xinbuf); + if (xgot == (int)count) { + got += count; + xpos += count; + xinbuf += count; + xrl++; + } + first = FALSE; + } while ((xgot == (int)count) && (got < to_read)); + return (got); +} + +/* + * Write data to a set of clusters + * + * Returns the amount of data written + */ + +static s32 write_clusters(ntfs_volume *vol, const runlist_element *rl, + s64 offs, s32 to_write, const char *outbuf) +{ + s32 count; + s32 put, xput; + s64 xpos; + BOOL first; + const char *xoutbuf; + const runlist_element *xrl; + + put = 0; + xrl = rl; + xoutbuf = outbuf; + first = TRUE; + do { + count = xrl->length << vol->cluster_size_bits; + xpos = xrl->lcn << vol->cluster_size_bits; + if (first) { + count -= offs; + xpos += offs; + } + if ((to_write - put) < count) + count = to_write - put; + xput = ntfs_pwrite(vol->dev, xpos, count, xoutbuf); + if (xput == count) { + put += count; + xpos += count; + xoutbuf += count; + xrl++; + } + first = FALSE; + } while ((xput == count) && (put < to_write)); + return (put); +} + + +/* + * Compress and write a set of blocks + * + * returns the size actually written (rounded to a full cluster) + * or 0 if all zeroes (nothing is written) + * or -1 if could not compress (nothing is written) + * or -2 if there were an irrecoverable error (errno set) + */ + +static s32 ntfs_comp_set(ntfs_attr *na, runlist_element *rl, + s64 offs, u32 insz, const char *inbuf) +{ + ntfs_volume *vol; + char *outbuf; + char *pbuf; + u32 compsz; + s32 written; + s32 rounded; + unsigned int clsz; + u32 p; + unsigned int sz; + unsigned int bsz; + BOOL fail; + BOOL allzeroes; + /* a single compressed zero */ + static char onezero[] = { 0x01, 0xb0, 0x00, 0x00 } ; + /* a couple of compressed zeroes */ + static char twozeroes[] = { 0x02, 0xb0, 0x00, 0x00, 0x00 } ; + /* more compressed zeroes, to be followed by some count */ + static char morezeroes[] = { 0x03, 0xb0, 0x02, 0x00 } ; + + vol = na->ni->vol; + written = -1; /* default return */ + clsz = 1 << vol->cluster_size_bits; + /* may need 2 extra bytes per block and 2 more bytes */ + outbuf = (char*)ntfs_malloc(na->compression_block_size + + 2*(na->compression_block_size/NTFS_SB_SIZE) + + 2); + if (outbuf) { + fail = FALSE; + compsz = 0; + allzeroes = TRUE; + for (p=0; (p na->compression_block_size)) + fail = TRUE; + else { + if (allzeroes) { + /* check whether this is all zeroes */ + switch (sz) { + case 4 : + allzeroes = !memcmp( + pbuf,onezero,4); + break; + case 5 : + allzeroes = !memcmp( + pbuf,twozeroes,5); + break; + case 6 : + allzeroes = !memcmp( + pbuf,morezeroes,4); + break; + default : + allzeroes = FALSE; + break; + } + } + compsz += sz; + } + } + if (!fail && !allzeroes) { + /* add a couple of null bytes, space has been checked */ + outbuf[compsz++] = 0; + outbuf[compsz++] = 0; + /* write a full cluster, to avoid partial reading */ + rounded = ((compsz - 1) | (clsz - 1)) + 1; + memset(&outbuf[compsz], 0, rounded - compsz); + written = write_clusters(vol, rl, offs, rounded, outbuf); + if (written != rounded) { + /* + * TODO : previously written text has been + * spoilt, should return a specific error + */ + ntfs_log_error("error writing compressed data\n"); + errno = EIO; + written = -2; + } + } else + if (!fail) + written = 0; + free(outbuf); + } + return (written); +} + +/* + * Check the validity of a compressed runlist + * The check starts at the beginning of current run and ends + * at the end of runlist + * errno is set if the runlist is not valid + */ + +static BOOL valid_compressed_run(ntfs_attr *na, runlist_element *rl, + BOOL fullcheck, const char *text) +{ + runlist_element *xrl; + const char *err; + BOOL ok = TRUE; + + xrl = rl; + while (xrl->vcn & (na->compression_block_clusters - 1)) + xrl--; + err = (const char*)NULL; + while (xrl->length) { + if ((xrl->vcn + xrl->length) != xrl[1].vcn) + err = "Runs not adjacent"; + if (xrl->lcn == LCN_HOLE) { + if ((xrl->vcn + xrl->length) + & (na->compression_block_clusters - 1)) { + err = "Invalid hole"; + } + if (fullcheck && (xrl[1].lcn == LCN_HOLE)) { + err = "Adjacent holes"; + } + } + if (err) { + ntfs_log_error("%s at %s index %ld inode %lld\n", + err, text, (long)(xrl - na->rl), + (long long)na->ni->mft_no); + errno = EIO; + ok = FALSE; + err = (const char*)NULL; + } + xrl++; + } + return (ok); +} + +/* + * Free unneeded clusters after overwriting compressed data + * + * This generally requires one or two empty slots at the end of runlist, + * but we do not want to reallocate the runlist here because + * there are many pointers to it. + * So the empty slots have to be reserved beforehand + * + * Returns zero unless some error occurred (described by errno) + * + * +======= start of block =====+ + * 0 |A chunk may overflow | <-- rl usedcnt : A + B + * |A on previous block | then B + * |A | + * +-- end of allocated chunk --+ freelength : C + * |B | (incl overflow) + * +== end of compressed data ==+ + * |C | <-- freerl freecnt : C + D + * |C chunk may overflow | + * |C on next block | + * +-- end of allocated chunk --+ + * |D | + * |D chunk may overflow | + * 15 |D on next block | + * +======== end of block ======+ + * + */ + +static int ntfs_compress_overwr_free(ntfs_attr *na, runlist_element *rl, + s32 usedcnt, s32 freecnt, VCN *update_from) +{ + BOOL beginhole; + BOOL mergeholes; + s32 oldlength; + s32 freelength; + s64 freelcn; + s64 freevcn; + runlist_element *freerl; + ntfs_volume *vol; + s32 carry; + int res; + + vol = na->ni->vol; + res = 0; + freelcn = rl->lcn + usedcnt; + freevcn = rl->vcn + usedcnt; + freelength = rl->length - usedcnt; + beginhole = !usedcnt && !rl->vcn; + /* can merge with hole before ? */ + mergeholes = !usedcnt + && rl[0].vcn + && (rl[-1].lcn == LCN_HOLE); + /* truncate current run, carry to subsequent hole */ + carry = freelength; + oldlength = rl->length; + if (mergeholes) { + /* merging with a hole before */ + freerl = rl; + } else { + rl->length -= freelength; /* warning : can be zero */ + freerl = ++rl; + } + if (!mergeholes && (usedcnt || beginhole)) { + s32 freed; + runlist_element *frl; + runlist_element *erl; + int holes = 0; + BOOL threeparts; + + /* free the unneeded clusters from initial run, then freerl */ + threeparts = (freelength > freecnt); + freed = 0; + frl = freerl; + if (freelength) { + res = ntfs_cluster_free_basic(vol,freelcn, + (threeparts ? freecnt : freelength)); + if (!res) + freed += (threeparts ? freecnt : freelength); + if (!usedcnt) { + holes++; + freerl--; + freerl->length += (threeparts + ? freecnt : freelength); + if (freerl->vcn < *update_from) + *update_from = freerl->vcn; + } + } + while (!res && frl->length && (freed < freecnt)) { + if (frl->length <= (freecnt - freed)) { + res = ntfs_cluster_free_basic(vol, frl->lcn, + frl->length); + if (!res) { + freed += frl->length; + frl->lcn = LCN_HOLE; + frl->length += carry; + carry = 0; + holes++; + } + } else { + res = ntfs_cluster_free_basic(vol, frl->lcn, + freecnt - freed); + if (!res) { + frl->lcn += freecnt - freed; + frl->vcn += freecnt - freed; + frl->length -= freecnt - freed; + freed = freecnt; + } + } + frl++; + } + na->compressed_size -= freed << vol->cluster_size_bits; + switch (holes) { + case 0 : + /* there are no hole, must insert one */ + /* space for hole has been prereserved */ + if (freerl->lcn == LCN_HOLE) { + if (threeparts) { + erl = freerl; + while (erl->length) + erl++; + do { + erl[2] = *erl; + } while (erl-- != freerl); + + freerl[1].length = freelength - freecnt; + freerl->length = freecnt; + freerl[1].lcn = freelcn + freecnt; + freerl[1].vcn = freevcn + freecnt; + freerl[2].lcn = LCN_HOLE; + freerl[2].vcn = freerl[1].vcn + + freerl[1].length; + freerl->vcn = freevcn; + } else { + freerl->vcn = freevcn; + freerl->length += freelength; + } + } else { + erl = freerl; + while (erl->length) + erl++; + if (threeparts) { + do { + erl[2] = *erl; + } while (erl-- != freerl); + freerl[1].lcn = freelcn + freecnt; + freerl[1].vcn = freevcn + freecnt; + freerl[1].length = oldlength - usedcnt - freecnt; + } else { + do { + erl[1] = *erl; + } while (erl-- != freerl); + } + freerl->lcn = LCN_HOLE; + freerl->vcn = freevcn; + freerl->length = freecnt; + } + break; + case 1 : + /* there is a single hole, may have to merge */ + freerl->vcn = freevcn; + freerl->length = freecnt; + if (freerl[1].lcn == LCN_HOLE) { + freerl->length += freerl[1].length; + erl = freerl; + do { + erl++; + *erl = erl[1]; + } while (erl->length); + } + break; + default : + /* there were several holes, must merge them */ + freerl->lcn = LCN_HOLE; + freerl->vcn = freevcn; + freerl->length = freecnt; + if (freerl[holes].lcn == LCN_HOLE) { + freerl->length += freerl[holes].length; + holes++; + } + erl = freerl; + do { + erl++; + *erl = erl[holes - 1]; + } while (erl->length); + break; + } + } else { + s32 freed; + runlist_element *frl; + runlist_element *xrl; + + freed = 0; + frl = freerl--; + if (freerl->vcn < *update_from) + *update_from = freerl->vcn; + while (!res && frl->length && (freed < freecnt)) { + if (frl->length <= (freecnt - freed)) { + freerl->length += frl->length; + freed += frl->length; + res = ntfs_cluster_free_basic(vol, frl->lcn, + frl->length); + frl++; + } else { + freerl->length += freecnt - freed; + res = ntfs_cluster_free_basic(vol, frl->lcn, + freecnt - freed); + frl->lcn += freecnt - freed; + frl->vcn += freecnt - freed; + frl->length -= freecnt - freed; + freed = freecnt; + } + } + /* remove unneded runlist entries */ + xrl = freerl; + /* group with next run if also a hole */ + if (frl->length && (frl->lcn == LCN_HOLE)) { + xrl->length += frl->length; + frl++; + } + while (frl->length) { + *++xrl = *frl++; + } + *++xrl = *frl; /* terminator */ + na->compressed_size -= freed << vol->cluster_size_bits; + } + return (res); +} + + +/* + * Free unneeded clusters after compression + * + * This generally requires one or two empty slots at the end of runlist, + * but we do not want to reallocate the runlist here because + * there are many pointers to it. + * So the empty slots have to be reserved beforehand + * + * Returns zero unless some error occurred (described by errno) + */ + +static int ntfs_compress_free(ntfs_attr *na, runlist_element *rl, + s64 used, s64 reserved, BOOL appending, + VCN *update_from) +{ + s32 freecnt; + s32 usedcnt; + int res; + s64 freelcn; + s64 freevcn; + s32 freelength; + BOOL mergeholes; + BOOL beginhole; + ntfs_volume *vol; + runlist_element *freerl; + + res = -1; /* default return */ + vol = na->ni->vol; + freecnt = (reserved - used) >> vol->cluster_size_bits; + usedcnt = (reserved >> vol->cluster_size_bits) - freecnt; + if (rl->vcn < *update_from) + *update_from = rl->vcn; + /* skip entries fully used, if any */ + while (rl->length && (rl->length < usedcnt)) { + usedcnt -= rl->length; /* must be > 0 */ + rl++; + } + if (rl->length) { + /* + * Splitting the current allocation block requires + * an extra runlist element to create the hole. + * The required entry has been prereserved when + * mapping the runlist. + */ + /* get the free part in initial run */ + freelcn = rl->lcn + usedcnt; + freevcn = rl->vcn + usedcnt; + /* new count of allocated clusters */ + if (!((freevcn + freecnt) + & (na->compression_block_clusters - 1))) { + if (!appending) + res = ntfs_compress_overwr_free(na,rl, + usedcnt,freecnt,update_from); + else { + freelength = rl->length - usedcnt; + beginhole = !usedcnt && !rl->vcn; + mergeholes = !usedcnt + && rl[0].vcn + && (rl[-1].lcn == LCN_HOLE); + if (mergeholes) { + s32 carry; + + /* shorten the runs which have free space */ + carry = freecnt; + freerl = rl; + while (freerl->length < carry) { + carry -= freerl->length; + freerl++; + } + freerl->length = carry; + freerl = rl; + } else { + rl->length = usedcnt; /* can be zero ? */ + freerl = ++rl; + } + if ((freelength > 0) + && !mergeholes + && (usedcnt || beginhole)) { + /* + * move the unused part to the end. Doing so, + * the vcn will be out of order. This does + * not harm, the vcn are meaningless now, and + * only the lcn are meaningful for freeing. + */ + /* locate current end */ + while (rl->length) + rl++; + /* new terminator relocated */ + rl[1].vcn = rl->vcn; + rl[1].lcn = LCN_ENOENT; + rl[1].length = 0; + /* hole, currently allocated */ + rl->vcn = freevcn; + rl->lcn = freelcn; + rl->length = freelength; + } else { + /* why is this different from the begin hole case ? */ + if ((freelength > 0) + && !mergeholes + && !usedcnt) { + freerl--; + freerl->length = freelength; + if (freerl->vcn < *update_from) + *update_from + = freerl->vcn; + } + } + /* free the hole */ + res = ntfs_cluster_free_from_rl(vol,freerl); + if (!res) { + na->compressed_size -= freecnt + << vol->cluster_size_bits; + if (mergeholes) { + /* merge with adjacent hole */ + freerl--; + freerl->length += freecnt; + } else { + if (beginhole) + freerl--; + /* mark hole as free */ + freerl->lcn = LCN_HOLE; + freerl->vcn = freevcn; + freerl->length = freecnt; + } + if (freerl->vcn < *update_from) + *update_from = freerl->vcn; + /* and set up the new end */ + freerl[1].lcn = LCN_ENOENT; + freerl[1].vcn = freevcn + freecnt; + freerl[1].length = 0; + } + } + } else { + ntfs_log_error("Bad end of a compression block set\n"); + errno = EIO; + } + } else { + ntfs_log_error("No cluster to free after compression\n"); + errno = EIO; + } + NAttrSetRunlistDirty(na); + return (res); +} + +/* + * Read existing data, decompress and append buffer + * Do nothing if something fails + */ + +static int ntfs_read_append(ntfs_attr *na, const runlist_element *rl, + s64 offs, u32 compsz, s32 pos, BOOL appending, + char *outbuf, s64 to_write, const void *b) +{ + int fail = 1; + char *compbuf; + u32 decompsz; + u32 got; + + if (compsz == na->compression_block_size) { + /* if the full block was requested, it was a hole */ + memset(outbuf,0,compsz); + memcpy(&outbuf[pos],b,to_write); + fail = 0; + } else { + compbuf = (char*)ntfs_malloc(compsz); + if (compbuf) { + /* must align to full block for decompression */ + if (appending) + decompsz = ((pos - 1) | (NTFS_SB_SIZE - 1)) + 1; + else + decompsz = na->compression_block_size; + got = read_clusters(na->ni->vol, rl, offs, + compsz, compbuf); + if ((got == compsz) + && !ntfs_decompress((u8*)outbuf,decompsz, + (u8*)compbuf,compsz)) { + memcpy(&outbuf[pos],b,to_write); + fail = 0; + } + free(compbuf); + } + } + return (fail); +} + +/* + * Flush a full compression block + * + * returns the size actually written (rounded to a full cluster) + * or 0 if could not compress (and written uncompressed) + * or -1 if there were an irrecoverable error (errno set) + */ + +static s32 ntfs_flush(ntfs_attr *na, runlist_element *rl, s64 offs, + char *outbuf, s32 count, BOOL compress, + BOOL appending, VCN *update_from) +{ + s32 rounded; + s32 written; + int clsz; + + if (compress) { + written = ntfs_comp_set(na, rl, offs, count, outbuf); + if (written == -1) + compress = FALSE; + if ((written >= 0) + && ntfs_compress_free(na,rl,offs + written, + offs + na->compression_block_size, appending, + update_from)) + written = -1; + } else + written = 0; + if (!compress) { + clsz = 1 << na->ni->vol->cluster_size_bits; + rounded = ((count - 1) | (clsz - 1)) + 1; + if (rounded > count) + memset(&outbuf[count], 0, rounded - count); + written = write_clusters(na->ni->vol, rl, + offs, rounded, outbuf); + if (written != rounded) + written = -1; + } + return (written); +} + +/* + * Write some data to be compressed. + * Compression only occurs when a few clusters (usually 16) are + * full. When this occurs an extra runlist slot may be needed, so + * it has to be reserved beforehand. + * + * Returns the size of uncompressed data written, + * or negative if an error occurred. + * When the returned size is less than requested, new clusters have + * to be allocated before the function is called again. + */ + +s64 ntfs_compressed_pwrite(ntfs_attr *na, runlist_element *wrl, s64 wpos, + s64 offs, s64 to_write, s64 rounded, + const void *b, int compressed_part, + VCN *update_from) +{ + ntfs_volume *vol; + runlist_element *brl; /* entry containing the beginning of block */ + int compression_length; + s64 written; + s64 to_read; + s64 to_flush; + s64 roffs; + s64 got; + s64 start_vcn; + s64 nextblock; + s64 endwrite; + u32 compsz; + char *inbuf; + char *outbuf; + BOOL fail; + BOOL done; + BOOL compress; + BOOL appending; + + if (!valid_compressed_run(na,wrl,FALSE,"begin compressed write")) { + return (-1); + } + if ((*update_from < 0) + || (compressed_part < 0) + || (compressed_part > (int)na->compression_block_clusters)) { + ntfs_log_error("Bad update vcn or compressed_part %d for compressed write\n", + compressed_part); + errno = EIO; + return (-1); + } + /* make sure there are two unused entries in runlist */ + if (na->unused_runs < 2) { + ntfs_log_error("No unused runs for compressed write\n"); + errno = EIO; + return (-1); + } + if (na->compression_block_size < NTFS_SB_SIZE) { + ntfs_log_error("Unsupported compression block size %ld\n", + (long)na->compression_block_size); + errno = EOVERFLOW; + return (-1); + } + if (wrl->vcn < *update_from) + *update_from = wrl->vcn; + written = -1; /* default return */ + vol = na->ni->vol; + compression_length = na->compression_block_clusters; + compress = FALSE; + done = FALSE; + /* + * Cannot accept writing beyond the current compression set + * because when compression occurs, clusters are freed + * and have to be reallocated. + * (cannot happen with standard fuse 4K buffers) + * Caller has to avoid this situation, or face consequences. + */ + nextblock = ((offs + (wrl->vcn << vol->cluster_size_bits)) + | (na->compression_block_size - 1)) + 1; + /* determine whether we are appending to file */ + endwrite = offs + to_write + (wrl->vcn << vol->cluster_size_bits); + appending = endwrite >= na->initialized_size; + if (endwrite >= nextblock) { + /* it is time to compress */ + compress = TRUE; + /* only process what we can */ + to_write = rounded = nextblock + - (offs + (wrl->vcn << vol->cluster_size_bits)); + } + start_vcn = 0; + fail = FALSE; + brl = wrl; + roffs = 0; + /* + * If we are about to compress or we need to decompress + * existing data, we have to process a full set of blocks. + * So relocate the parameters to the beginning of allocation + * containing the first byte of the set of blocks. + */ + if (compress || compressed_part) { + /* find the beginning of block */ + start_vcn = (wrl->vcn + (offs >> vol->cluster_size_bits)) + & -compression_length; + if (start_vcn < *update_from) + *update_from = start_vcn; + while (brl->vcn && (brl->vcn > start_vcn)) { + /* jumping back a hole means big trouble */ + if (brl->lcn == (LCN)LCN_HOLE) { + ntfs_log_error("jump back over a hole when appending\n"); + fail = TRUE; + errno = EIO; + } + brl--; + offs += brl->length << vol->cluster_size_bits; + } + roffs = (start_vcn - brl->vcn) << vol->cluster_size_bits; + } + if (compressed_part && !fail) { + /* + * The set of compression blocks contains compressed data + * (we are reopening an existing file to append to it) + * Decompress the data and append + */ + compsz = (s32)compressed_part << vol->cluster_size_bits; + outbuf = (char*)ntfs_malloc(na->compression_block_size); + if (outbuf) { + if (appending) { + to_read = offs - roffs; + to_flush = to_read + to_write; + } else { + to_read = na->data_size + - (brl->vcn << vol->cluster_size_bits); + if (to_read > na->compression_block_size) + to_read = na->compression_block_size; + to_flush = to_read; + } + if (!ntfs_read_append(na, brl, roffs, compsz, + (s32)(offs - roffs), appending, + outbuf, to_write, b)) { + written = ntfs_flush(na, brl, roffs, + outbuf, to_flush, compress, appending, + update_from); + if (written >= 0) { + written = to_write; + done = TRUE; + } + } + free(outbuf); + } + } else { + if (compress && !fail) { + /* + * we are filling up a block, read the full set + * of blocks and compress it + */ + inbuf = (char*)ntfs_malloc(na->compression_block_size); + if (inbuf) { + to_read = offs - roffs; + if (to_read) + got = read_clusters(vol, brl, roffs, + to_read, inbuf); + else + got = 0; + if (got == to_read) { + memcpy(&inbuf[to_read],b,to_write); + written = ntfs_comp_set(na, brl, roffs, + to_read + to_write, inbuf); + /* + * if compression was not successful, + * only write the part which was requested + */ + if ((written >= 0) + /* free the unused clusters */ + && !ntfs_compress_free(na,brl, + written + roffs, + na->compression_block_size + + roffs, + appending, update_from)) { + done = TRUE; + written = to_write; + } + } + free(inbuf); + } + } + if (!done) { + /* + * if the compression block is not full, or + * if compression failed for whatever reason, + * write uncompressed + */ + /* check we are not overflowing current allocation */ + if ((wpos + rounded) + > ((wrl->lcn + wrl->length) + << vol->cluster_size_bits)) { + ntfs_log_error("writing on unallocated clusters\n"); + errno = EIO; + } else { + written = ntfs_pwrite(vol->dev, wpos, + rounded, b); + if (written == rounded) + written = to_write; + } + } + } + if ((written >= 0) + && !valid_compressed_run(na,wrl,TRUE,"end compressed write")) + written = -1; + return (written); +} + +/* + * Close a file written compressed. + * This compresses the last partial compression block of the file. + * Two empty runlist slots have to be reserved beforehand. + * + * Returns zero if closing is successful. + */ + +int ntfs_compressed_close(ntfs_attr *na, runlist_element *wrl, s64 offs, + VCN *update_from) +{ + ntfs_volume *vol; + runlist_element *brl; /* entry containing the beginning of block */ + int compression_length; + s64 written; + s64 to_read; + s64 roffs; + s64 got; + s64 start_vcn; + char *inbuf; + BOOL fail; + BOOL done; + + if (na->unused_runs < 2) { + ntfs_log_error("No unused runs for compressed close\n"); + errno = EIO; + return (-1); + } + if (*update_from < 0) { + ntfs_log_error("Bad update vcn for compressed close\n"); + errno = EIO; + return (-1); + } + if (na->compression_block_size < NTFS_SB_SIZE) { + ntfs_log_error("Unsupported compression block size %ld\n", + (long)na->compression_block_size); + errno = EOVERFLOW; + return (-1); + } + if (wrl->vcn < *update_from) + *update_from = wrl->vcn; + vol = na->ni->vol; + compression_length = na->compression_block_clusters; + done = FALSE; + /* + * There generally is an uncompressed block at end of file, + * read the full block and compress it + */ + inbuf = (char*)ntfs_malloc(na->compression_block_size); + if (inbuf) { + start_vcn = (wrl->vcn + (offs >> vol->cluster_size_bits)) + & -compression_length; + if (start_vcn < *update_from) + *update_from = start_vcn; + to_read = offs + ((wrl->vcn - start_vcn) + << vol->cluster_size_bits); + brl = wrl; + fail = FALSE; + while (brl->vcn && (brl->vcn > start_vcn)) { + if (brl->lcn == (LCN)LCN_HOLE) { + ntfs_log_error("jump back over a hole when closing\n"); + fail = TRUE; + errno = EIO; + } + brl--; + } + if (!fail) { + /* roffs can be an offset from another uncomp block */ + roffs = (start_vcn - brl->vcn) + << vol->cluster_size_bits; + if (to_read) { + got = read_clusters(vol, brl, roffs, to_read, + inbuf); + if (got == to_read) { + written = ntfs_comp_set(na, brl, roffs, + to_read, inbuf); + if ((written >= 0) + /* free the unused clusters */ + && !ntfs_compress_free(na,brl, + written + roffs, + na->compression_block_size + roffs, + TRUE, update_from)) { + done = TRUE; + } else + /* if compression failed, leave uncompressed */ + if (written == -1) + done = TRUE; + } + } else + done = TRUE; + free(inbuf); + } + } + if (done && !valid_compressed_run(na,wrl,TRUE,"end compressed close")) + done = FALSE; + return (!done); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/debug.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/debug.c new file mode 100644 index 0000000..f193483 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/debug.c @@ -0,0 +1,79 @@ +/** + * debug.c - Debugging output functions. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002-2004 Anton Altaparmakov + * Copyright (c) 2004-2006 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "types.h" +#include "runlist.h" +#include "debug.h" +#include "logging.h" + +#ifdef DEBUG +/** + * ntfs_debug_runlist_dump - Dump a runlist. + * @rl: + * + * Description... + * + * Returns: + */ +void ntfs_debug_runlist_dump(const runlist_element *rl) +{ + int i = 0; + const char *lcn_str[5] = { "LCN_HOLE ", "LCN_RL_NOT_MAPPED", + "LCN_ENOENT ", "LCN_EINVAL ", + "LCN_unknown " }; + + ntfs_log_debug("NTFS-fs DEBUG: Dumping runlist (values in hex):\n"); + if (!rl) { + ntfs_log_debug("Run list not present.\n"); + return; + } + ntfs_log_debug("VCN LCN Run length\n"); + do { + LCN lcn = (rl + i)->lcn; + + if (lcn < (LCN)0) { + int idx = -lcn - 1; + + if (idx > -LCN_EINVAL - 1) + idx = 4; + ntfs_log_debug("%-16lld %s %-16lld%s\n", + (long long)rl[i].vcn, lcn_str[idx], + (long long)rl[i].length, + rl[i].length ? "" : " (runlist end)"); + } else + ntfs_log_debug("%-16lld %-16lld %-16lld%s\n", + (long long)rl[i].vcn, (long long)rl[i].lcn, + (long long)rl[i].length, + rl[i].length ? "" : " (runlist end)"); + } while (rl[i++].length); +} + +#endif + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/device.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/device.c new file mode 100644 index 0000000..a5c32f4 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/device.c @@ -0,0 +1,932 @@ +/** + * device.c - Low level device io functions. Originated from the Linux-NTFS project. + * + * Copyright (c) 2004-2013 Anton Altaparmakov + * Copyright (c) 2004-2006 Szabolcs Szakacsits + * Copyright (c) 2010 Jean-Pierre Andre + * Copyright (c) 2008-2013 Tuxera Inc. + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_SYS_IOCTL_H +#include +#endif +#ifdef HAVE_SYS_PARAM_H +#include +#endif +#ifdef HAVE_SYS_MOUNT_H +#include +#endif +#ifdef HAVE_SYS_DISK_H +#include +#endif +#ifdef HAVE_LINUX_FD_H +#include +#endif +#ifdef HAVE_LINUX_HDREG_H +#include +#endif +#ifdef ENABLE_HD +#include +#endif + +#include "types.h" +#include "mst.h" +#include "debug.h" +#include "device.h" +#include "logging.h" +#include "misc.h" + +#if defined(linux) && defined(_IO) && !defined(BLKGETSIZE) +#define BLKGETSIZE _IO(0x12,96) /* Get device size in 512-byte blocks. */ +#endif +#if defined(linux) && defined(_IOR) && !defined(BLKGETSIZE64) +#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* Get device size in bytes. */ +#endif +#if defined(linux) && !defined(HDIO_GETGEO) +#define HDIO_GETGEO 0x0301 /* Get device geometry. */ +#endif +#if defined(linux) && defined(_IO) && !defined(BLKSSZGET) +# define BLKSSZGET _IO(0x12,104) /* Get device sector size in bytes. */ +#endif +#if defined(linux) && defined(_IO) && !defined(BLKBSZSET) +# define BLKBSZSET _IOW(0x12,113,size_t) /* Set device block size in bytes. */ +#endif + +/** + * ntfs_device_alloc - allocate an ntfs device structure and pre-initialize it + * @name: name of the device (must be present) + * @state: initial device state (usually zero) + * @dops: ntfs device operations to use with the device (must be present) + * @priv_data: pointer to private data (optional) + * + * Allocate an ntfs device structure and pre-initialize it with the user- + * specified device operations @dops, device state @state, device name @name, + * and optional private data @priv_data. + * + * Note, @name is copied and can hence be freed after this functions returns. + * + * On success return a pointer to the allocated ntfs device structure and on + * error return NULL with errno set to the error code returned by ntfs_malloc(). + */ +struct ntfs_device *ntfs_device_alloc(const char *name, const long state, + struct ntfs_device_operations *dops, void *priv_data) +{ + struct ntfs_device *dev; + + if (!name) { + errno = EINVAL; + return NULL; + } + + dev = ntfs_malloc(sizeof(struct ntfs_device)); + if (dev) { + if (!(dev->d_name = strdup(name))) { + int eo = errno; + free(dev); + errno = eo; + return NULL; + } + dev->d_ops = dops; + dev->d_state = state; + dev->d_private = priv_data; + dev->d_heads = -1; + dev->d_sectors_per_track = -1; + } + return dev; +} + +/** + * ntfs_device_free - free an ntfs device structure + * @dev: ntfs device structure to free + * + * Free the ntfs device structure @dev. + * + * Return 0 on success or -1 on error with errno set to the error code. The + * following error codes are defined: + * EINVAL Invalid pointer @dev. + * EBUSY Device is still open. Close it before freeing it! + */ +int ntfs_device_free(struct ntfs_device *dev) +{ + if (!dev) { + errno = EINVAL; + return -1; + } + if (NDevOpen(dev)) { + errno = EBUSY; + return -1; + } + free(dev->d_name); + free(dev); + return 0; +} + +/* + * Sync the device + * + * returns zero if successful. + */ + +int ntfs_device_sync(struct ntfs_device *dev) +{ + int ret; + struct ntfs_device_operations *dops; + + if (NDevDirty(dev)) { + dops = dev->d_ops; + ret = dops->sync(dev); + } else + ret = 0; + return ret; +} + +/** + * ntfs_pread - positioned read from disk + * @dev: device to read from + * @pos: position in device to read from + * @count: number of bytes to read + * @b: output data buffer + * + * This function will read @count bytes from device @dev at position @pos into + * the data buffer @b. + * + * On success, return the number of successfully read bytes. If this number is + * lower than @count this means that we have either reached end of file or + * encountered an error during the read so that the read is partial. 0 means + * end of file or nothing to read (@count is 0). + * + * On error and nothing has been read, return -1 with errno set appropriately + * to the return code of either seek, read, or set to EINVAL in case of + * invalid arguments. + */ +s64 ntfs_pread(struct ntfs_device *dev, const s64 pos, s64 count, void *b) +{ + s64 br, total; + struct ntfs_device_operations *dops; + + ntfs_log_trace("pos %lld, count %lld\n",(long long)pos,(long long)count); + + if (!b || count < 0 || pos < 0) { + errno = EINVAL; + return -1; + } + if (!count) + return 0; + + dops = dev->d_ops; + + for (total = 0; count; count -= br, total += br) { + br = dops->pread(dev, (char*)b + total, count, pos + total); + /* If everything ok, continue. */ + if (br > 0) + continue; + /* If EOF or error return number of bytes read. */ + if (!br || total) + return total; + /* Nothing read and error, return error status. */ + return br; + } + /* Finally, return the number of bytes read. */ + return total; +} + +/** + * ntfs_pwrite - positioned write to disk + * @dev: device to write to + * @pos: position in file descriptor to write to + * @count: number of bytes to write + * @b: data buffer to write to disk + * + * This function will write @count bytes from data buffer @b to the device @dev + * at position @pos. + * + * On success, return the number of successfully written bytes. If this number + * is lower than @count this means that the write has been interrupted in + * flight or that an error was encountered during the write so that the write + * is partial. 0 means nothing was written (also return 0 when @count is 0). + * + * On error and nothing has been written, return -1 with errno set + * appropriately to the return code of either seek, write, or set + * to EINVAL in case of invalid arguments. + */ +s64 ntfs_pwrite(struct ntfs_device *dev, const s64 pos, s64 count, + const void *b) +{ + s64 written, total, ret = -1; + struct ntfs_device_operations *dops; + + ntfs_log_trace("pos %lld, count %lld\n",(long long)pos,(long long)count); + + if (!b || count < 0 || pos < 0) { + errno = EINVAL; + goto out; + } + if (!count) + return 0; + if (NDevReadOnly(dev)) { + errno = EROFS; + goto out; + } + + dops = dev->d_ops; + + NDevSetDirty(dev); + for (total = 0; count; count -= written, total += written) { + written = dops->pwrite(dev, (const char*)b + total, count, + pos + total); + /* If everything ok, continue. */ + if (written > 0) + continue; + /* + * If nothing written or error return number of bytes written. + */ + if (!written || total) + break; + /* Nothing written and error, return error status. */ + total = written; + break; + } + if (NDevSync(dev) && total && dops->sync(dev)) { + total--; /* on sync error, return partially written */ + } + ret = total; +out: + return ret; +} + +/** + * ntfs_mst_pread - multi sector transfer (mst) positioned read + * @dev: device to read from + * @pos: position in file descriptor to read from + * @count: number of blocks to read + * @bksize: size of each block that needs mst deprotecting + * @b: output data buffer + * + * Multi sector transfer (mst) positioned read. This function will read @count + * blocks of size @bksize bytes each from device @dev at position @pos into the + * the data buffer @b. + * + * On success, return the number of successfully read blocks. If this number is + * lower than @count this means that we have reached end of file, that the read + * was interrupted, or that an error was encountered during the read so that + * the read is partial. 0 means end of file or nothing was read (also return 0 + * when @count or @bksize are 0). + * + * On error and nothing was read, return -1 with errno set appropriately to the + * return code of either seek, read, or set to EINVAL in case of invalid + * arguments. + * + * NOTE: If an incomplete multi sector transfer has been detected the magic + * will have been changed to magic_BAAD but no error will be returned. Thus it + * is possible that we return count blocks as being read but that any number + * (between zero and count!) of these blocks is actually subject to a multi + * sector transfer error. This should be detected by the caller by checking for + * the magic being "BAAD". + */ +s64 ntfs_mst_pread(struct ntfs_device *dev, const s64 pos, s64 count, + const u32 bksize, void *b) +{ + s64 br, i; + + if (bksize & (bksize - 1) || bksize % NTFS_BLOCK_SIZE) { + errno = EINVAL; + return -1; + } + /* Do the read. */ + br = ntfs_pread(dev, pos, count * bksize, b); + if (br < 0) + return br; + /* + * Apply fixups to successfully read data, disregarding any errors + * returned from the MST fixup function. This is because we want to + * fixup everything possible and we rely on the fact that the "BAAD" + * magic will be detected later on. + */ + count = br / bksize; + for (i = 0; i < count; ++i) + ntfs_mst_post_read_fixup((NTFS_RECORD*) + ((u8*)b + i * bksize), bksize); + /* Finally, return the number of complete blocks read. */ + return count; +} + +/** + * ntfs_mst_pwrite - multi sector transfer (mst) positioned write + * @dev: device to write to + * @pos: position in file descriptor to write to + * @count: number of blocks to write + * @bksize: size of each block that needs mst protecting + * @b: data buffer to write to disk + * + * Multi sector transfer (mst) positioned write. This function will write + * @count blocks of size @bksize bytes each from data buffer @b to the device + * @dev at position @pos. + * + * On success, return the number of successfully written blocks. If this number + * is lower than @count this means that the write has been interrupted or that + * an error was encountered during the write so that the write is partial. 0 + * means nothing was written (also return 0 when @count or @bksize are 0). + * + * On error and nothing has been written, return -1 with errno set + * appropriately to the return code of either seek, write, or set + * to EINVAL in case of invalid arguments. + * + * NOTE: We mst protect the data, write it, then mst deprotect it using a quick + * deprotect algorithm (no checking). This saves us from making a copy before + * the write and at the same time causes the usn to be incremented in the + * buffer. This conceptually fits in better with the idea that cached data is + * always deprotected and protection is performed when the data is actually + * going to hit the disk and the cache is immediately deprotected again + * simulating an mst read on the written data. This way cache coherency is + * achieved. + */ +s64 ntfs_mst_pwrite(struct ntfs_device *dev, const s64 pos, s64 count, + const u32 bksize, void *b) +{ + s64 written, i; + + if (count < 0 || bksize % NTFS_BLOCK_SIZE) { + errno = EINVAL; + return -1; + } + if (!count) + return 0; + /* Prepare data for writing. */ + for (i = 0; i < count; ++i) { + int err; + + err = ntfs_mst_pre_write_fixup((NTFS_RECORD*) + ((u8*)b + i * bksize), bksize); + if (err < 0) { + /* Abort write at this position. */ + if (!i) + return err; + count = i; + break; + } + } + /* Write the prepared data. */ + written = ntfs_pwrite(dev, pos, count * bksize, b); + /* Quickly deprotect the data again. */ + for (i = 0; i < count; ++i) + ntfs_mst_post_write_fixup((NTFS_RECORD*)((u8*)b + i * bksize)); + if (written <= 0) + return written; + /* Finally, return the number of complete blocks written. */ + return written / bksize; +} + +/** + * ntfs_cluster_read - read ntfs clusters + * @vol: volume to read from + * @lcn: starting logical cluster number + * @count: number of clusters to read + * @b: output data buffer + * + * Read @count ntfs clusters starting at logical cluster number @lcn from + * volume @vol into buffer @b. Return number of clusters read or -1 on error, + * with errno set to the error code. + */ +s64 ntfs_cluster_read(const ntfs_volume *vol, const s64 lcn, const s64 count, + void *b) +{ + s64 br; + + if (!vol || lcn < 0 || count < 0) { + errno = EINVAL; + return -1; + } + if (vol->nr_clusters < lcn + count) { + errno = ESPIPE; + ntfs_log_perror("Trying to read outside of volume " + "(%lld < %lld)", (long long)vol->nr_clusters, + (long long)lcn + count); + return -1; + } + br = ntfs_pread(vol->dev, lcn << vol->cluster_size_bits, + count << vol->cluster_size_bits, b); + if (br < 0) { + ntfs_log_perror("Error reading cluster(s)"); + return br; + } + return br >> vol->cluster_size_bits; +} + +/** + * ntfs_cluster_write - write ntfs clusters + * @vol: volume to write to + * @lcn: starting logical cluster number + * @count: number of clusters to write + * @b: data buffer to write to disk + * + * Write @count ntfs clusters starting at logical cluster number @lcn from + * buffer @b to volume @vol. Return the number of clusters written or -1 on + * error, with errno set to the error code. + */ +s64 ntfs_cluster_write(const ntfs_volume *vol, const s64 lcn, + const s64 count, const void *b) +{ + s64 bw; + + if (!vol || lcn < 0 || count < 0) { + errno = EINVAL; + return -1; + } + if (vol->nr_clusters < lcn + count) { + errno = ESPIPE; + ntfs_log_perror("Trying to write outside of volume " + "(%lld < %lld)", (long long)vol->nr_clusters, + (long long)lcn + count); + return -1; + } + if (!NVolReadOnly(vol)) + bw = ntfs_pwrite(vol->dev, lcn << vol->cluster_size_bits, + count << vol->cluster_size_bits, b); + else + bw = count << vol->cluster_size_bits; + if (bw < 0) { + ntfs_log_perror("Error writing cluster(s)"); + return bw; + } + return bw >> vol->cluster_size_bits; +} + +/** + * ntfs_device_offset_valid - test if a device offset is valid + * @dev: open device + * @ofs: offset to test for validity + * + * Test if the offset @ofs is an existing location on the device described + * by the open device structure @dev. + * + * Return 0 if it is valid and -1 if it is not valid. + */ +static int ntfs_device_offset_valid(struct ntfs_device *dev, s64 ofs) +{ + char ch; + + if (dev->d_ops->seek(dev, ofs, SEEK_SET) >= 0 && + dev->d_ops->read(dev, &ch, 1) == 1) + return 0; + return -1; +} + +/** + * ntfs_device_size_get - return the size of a device in blocks + * @dev: open device + * @block_size: block size in bytes in which to return the result + * + * Return the number of @block_size sized blocks in the device described by the + * open device @dev. + * + * Adapted from e2fsutils-1.19, Copyright (C) 1995 Theodore Ts'o. + * + * On error return -1 with errno set to the error code. + */ +s64 ntfs_device_size_get(struct ntfs_device *dev, int block_size) +{ + s64 high, low; + + if (!dev || block_size <= 0 || (block_size - 1) & block_size) { + errno = EINVAL; + return -1; + } +#ifdef BLKGETSIZE64 + { u64 size; + + if (dev->d_ops->ioctl(dev, BLKGETSIZE64, &size) >= 0) { + ntfs_log_debug("BLKGETSIZE64 nr bytes = %llu (0x%llx)\n", + (unsigned long long)size, + (unsigned long long)size); + return (s64)size / block_size; + } + } +#endif +#ifdef BLKGETSIZE + { unsigned long size; + + if (dev->d_ops->ioctl(dev, BLKGETSIZE, &size) >= 0) { + ntfs_log_debug("BLKGETSIZE nr 512 byte blocks = %lu (0x%lx)\n", + size, size); + return (s64)size * 512 / block_size; + } + } +#endif +#ifdef FDGETPRM + { struct floppy_struct this_floppy; + + if (dev->d_ops->ioctl(dev, FDGETPRM, &this_floppy) >= 0) { + ntfs_log_debug("FDGETPRM nr 512 byte blocks = %lu (0x%lx)\n", + (unsigned long)this_floppy.size, + (unsigned long)this_floppy.size); + return (s64)this_floppy.size * 512 / block_size; + } + } +#endif +#ifdef DIOCGMEDIASIZE + { + /* FreeBSD */ + off_t size; + + if (dev->d_ops->ioctl(dev, DIOCGMEDIASIZE, &size) >= 0) { + ntfs_log_debug("DIOCGMEDIASIZE nr bytes = %llu (0x%llx)\n", + (unsigned long long)size, + (unsigned long long)size); + return (s64)size / block_size; + } + } +#endif +#ifdef DKIOCGETBLOCKCOUNT + { + /* Mac OS X */ + uint64_t blocks; + int sector_size; + + sector_size = ntfs_device_sector_size_get(dev); + if (sector_size >= 0 && dev->d_ops->ioctl(dev, + DKIOCGETBLOCKCOUNT, &blocks) >= 0) + { + ntfs_log_debug("DKIOCGETBLOCKCOUNT nr blocks = %llu (0x%llx)\n", + (unsigned long long) blocks, + (unsigned long long) blocks); + return blocks * sector_size / block_size; + } + } +#endif + /* + * We couldn't figure it out by using a specialized ioctl, + * so do binary search to find the size of the device. + */ + low = 0LL; + for (high = 1024LL; !ntfs_device_offset_valid(dev, high); high <<= 1) + low = high; + while (low < high - 1LL) { + const s64 mid = (low + high) / 2; + + if (!ntfs_device_offset_valid(dev, mid)) + low = mid; + else + high = mid; + } + dev->d_ops->seek(dev, 0LL, SEEK_SET); + return (low + 1LL) / block_size; +} + +/** + * ntfs_device_partition_start_sector_get - get starting sector of a partition + * @dev: open device + * + * On success, return the starting sector of the partition @dev in the parent + * block device of @dev. On error return -1 with errno set to the error code. + * + * The following error codes are defined: + * EINVAL Input parameter error + * EOPNOTSUPP System does not support HDIO_GETGEO ioctl + * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO + */ +s64 ntfs_device_partition_start_sector_get(struct ntfs_device *dev) +{ + if (!dev) { + errno = EINVAL; + return -1; + } +#ifdef HDIO_GETGEO + { struct hd_geometry geo; + + if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) { + ntfs_log_debug("HDIO_GETGEO start_sect = %lu (0x%lx)\n", + geo.start, geo.start); + return geo.start; + } + } +#else + errno = EOPNOTSUPP; +#endif + return -1; +} + +static int ntfs_device_get_geo(struct ntfs_device *dev) +{ + int err; + + if (!dev) { + errno = EINVAL; + return -1; + } + err = EOPNOTSUPP; +#ifdef ENABLE_HD + { + hd_data_t *hddata; + hd_t *hd, *devlist, *partlist = NULL; + str_list_t *names; + hd_res_t *res; + const int d_name_len = strlen(dev->d_name) + 1; + int done = 0; + + hddata = calloc(1, sizeof(*hddata)); + if (!hddata) { + err = ENOMEM; + goto skip_hd; + } + /* List all "disk" class devices on the system. */ + devlist = hd_list(hddata, hw_disk, 1, NULL); + if (!devlist) { + free(hddata); + err = ENOMEM; + goto skip_hd; + } + /* + * Loop over each disk device looking for the device with the + * same unix name as @dev. + */ + for (hd = devlist; hd; hd = hd->next) { + if (hd->unix_dev_name && !strncmp(dev->d_name, + hd->unix_dev_name, d_name_len)) + goto got_hd; + if (hd->unix_dev_name2 && !strncmp(dev->d_name, + hd->unix_dev_name2, d_name_len)) + goto got_hd; + for (names = hd->unix_dev_names; names; + names = names->next) { + if (names->str && !strncmp(dev->d_name, + names->str, d_name_len)) + goto got_hd; + } + } + /* + * Device was not a whole disk device. Unless it is a file it + * is likely to be a partition device. List all "partition" + * class devices on the system. + */ + partlist = hd_list(hddata, hw_partition, 1, NULL); + for (hd = partlist; hd; hd = hd->next) { + if (hd->unix_dev_name && !strncmp(dev->d_name, + hd->unix_dev_name, d_name_len)) + goto got_part_hd; + if (hd->unix_dev_name2 && !strncmp(dev->d_name, + hd->unix_dev_name2, d_name_len)) + goto got_part_hd; + for (names = hd->unix_dev_names; names; + names = names->next) { + if (names->str && !strncmp(dev->d_name, + names->str, d_name_len)) + goto got_part_hd; + } + } + /* Failed to find the device. Stop trying and clean up. */ + goto end_hd; +got_part_hd: + /* Get the whole block device the partition device is on. */ + hd = hd_get_device_by_idx(hddata, hd->attached_to); + if (!hd) + goto end_hd; +got_hd: + /* + * @hd is now the whole block device either being formatted or + * that the partition being formatted is on. + * + * Loop over each resource of the disk device looking for the + * BIOS legacy geometry obtained from EDD which is what Windows + * needs to boot. + */ + for (res = hd->res; res; res = res->next) { + /* geotype 3 is BIOS legacy. */ + if (res->any.type != res_disk_geo || + res->disk_geo.geotype != 3) + continue; + dev->d_heads = res->disk_geo.heads; + dev->d_sectors_per_track = res->disk_geo.sectors; + done = 1; + } +end_hd: + if (partlist) + hd_free_hd_list(partlist); + hd_free_hd_list(devlist); + hd_free_hd_data(hddata); + free(hddata); + if (done) { + ntfs_log_debug("EDD/BIOD legacy heads = %u, sectors " + "per track = %u\n", dev->d_heads, + dev->d_sectors_per_track); + return 0; + } + } +skip_hd: +#endif +#ifdef HDIO_GETGEO + { struct hd_geometry geo; + + if (!dev->d_ops->ioctl(dev, HDIO_GETGEO, &geo)) { + dev->d_heads = geo.heads; + dev->d_sectors_per_track = geo.sectors; + ntfs_log_debug("HDIO_GETGEO heads = %u, sectors per " + "track = %u\n", dev->d_heads, + dev->d_sectors_per_track); + return 0; + } + err = errno; + } +#endif + errno = err; + return -1; +} + +/** + * ntfs_device_heads_get - get number of heads of device + * @dev: open device + * + * On success, return the number of heads on the device @dev. On error return + * -1 with errno set to the error code. + * + * The following error codes are defined: + * EINVAL Input parameter error + * EOPNOTSUPP System does not support HDIO_GETGEO ioctl + * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO + * ENOMEM Not enough memory to complete the request + */ +int ntfs_device_heads_get(struct ntfs_device *dev) +{ + if (!dev) { + errno = EINVAL; + return -1; + } + if (dev->d_heads == -1) { + if (ntfs_device_get_geo(dev) == -1) + return -1; + if (dev->d_heads == -1) { + errno = EINVAL; + return -1; + } + } + return dev->d_heads; +} + +/** + * ntfs_device_sectors_per_track_get - get number of sectors per track of device + * @dev: open device + * + * On success, return the number of sectors per track on the device @dev. On + * error return -1 with errno set to the error code. + * + * The following error codes are defined: + * EINVAL Input parameter error + * EOPNOTSUPP System does not support HDIO_GETGEO ioctl + * ENOTTY @dev is a file or a device not supporting HDIO_GETGEO + * ENOMEM Not enough memory to complete the request + */ +int ntfs_device_sectors_per_track_get(struct ntfs_device *dev) +{ + if (!dev) { + errno = EINVAL; + return -1; + } + if (dev->d_sectors_per_track == -1) { + if (ntfs_device_get_geo(dev) == -1) + return -1; + if (dev->d_sectors_per_track == -1) { + errno = EINVAL; + return -1; + } + } + return dev->d_sectors_per_track; +} + +/** + * ntfs_device_sector_size_get - get sector size of a device + * @dev: open device + * + * On success, return the sector size in bytes of the device @dev. + * On error return -1 with errno set to the error code. + * + * The following error codes are defined: + * EINVAL Input parameter error + * EOPNOTSUPP System does not support BLKSSZGET ioctl + * ENOTTY @dev is a file or a device not supporting BLKSSZGET + */ +int ntfs_device_sector_size_get(struct ntfs_device *dev) +{ + if (!dev) { + errno = EINVAL; + return -1; + } +#ifdef BLKSSZGET + { + int sect_size = 0; + + if (!dev->d_ops->ioctl(dev, BLKSSZGET, §_size)) { + ntfs_log_debug("BLKSSZGET sector size = %d bytes\n", + sect_size); + return sect_size; + } + } +#elif defined(DIOCGSECTORSIZE) + { + /* FreeBSD */ + size_t sect_size = 0; + + if (!dev->d_ops->ioctl(dev, DIOCGSECTORSIZE, §_size)) { + ntfs_log_debug("DIOCGSECTORSIZE sector size = %d bytes\n", + (int) sect_size); + return sect_size; + } + } +#elif defined(DKIOCGETBLOCKSIZE) + { + /* Mac OS X */ + uint32_t sect_size = 0; + + if (!dev->d_ops->ioctl(dev, DKIOCGETBLOCKSIZE, §_size)) { + ntfs_log_debug("DKIOCGETBLOCKSIZE sector size = %d bytes\n", + (int) sect_size); + return sect_size; + } + } +#else + errno = EOPNOTSUPP; +#endif + return -1; +} + +/** + * ntfs_device_block_size_set - set block size of a device + * @dev: open device + * @block_size: block size to set @dev to + * + * On success, return 0. + * On error return -1 with errno set to the error code. + * + * The following error codes are defined: + * EINVAL Input parameter error + * EOPNOTSUPP System does not support BLKBSZSET ioctl + * ENOTTY @dev is a file or a device not supporting BLKBSZSET + */ +int ntfs_device_block_size_set(struct ntfs_device *dev, + int block_size __attribute__((unused))) +{ + if (!dev) { + errno = EINVAL; + return -1; + } +#ifdef BLKBSZSET + { + size_t s_block_size = block_size; + if (!dev->d_ops->ioctl(dev, BLKBSZSET, &s_block_size)) { + ntfs_log_debug("Used BLKBSZSET to set block size to " + "%d bytes.\n", block_size); + return 0; + } + /* If not a block device, pretend it was successful. */ + if (!NDevBlock(dev)) + return 0; + } +#else + /* If not a block device, pretend it was successful. */ + if (!NDevBlock(dev)) + return 0; + errno = EOPNOTSUPP; +#endif + return -1; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/dir.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/dir.c new file mode 100644 index 0000000..ad82c6f --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/dir.c @@ -0,0 +1,2912 @@ +/** + * dir.c - Directory handling code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002-2005 Anton Altaparmakov + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2004-2008 Szabolcs Szakacsits + * Copyright (c) 2005-2007 Yura Pakhuchiy + * Copyright (c) 2008-2021 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif + +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef MAJOR_IN_MKDEV +#include +#endif +#ifdef MAJOR_IN_SYSMACROS +#include +#endif + +#include "param.h" +#include "types.h" +#include "debug.h" +#include "attrib.h" +#include "inode.h" +#include "dir.h" +#include "volume.h" +#include "mft.h" +#include "index.h" +#include "ntfstime.h" +#include "lcnalloc.h" +#include "logging.h" +#include "cache.h" +#include "misc.h" +#include "security.h" +#include "reparse.h" +#include "object_id.h" +#include "xattrs.h" +#include "ea.h" + +/* + * The little endian Unicode strings "$I30", "$SII", "$SDH", "$O" + * and "$Q" as global constants. + */ +ntfschar NTFS_INDEX_I30[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('I'), + const_cpu_to_le16('3'), const_cpu_to_le16('0'), + const_cpu_to_le16('\0') }; +ntfschar NTFS_INDEX_SII[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('S'), + const_cpu_to_le16('I'), const_cpu_to_le16('I'), + const_cpu_to_le16('\0') }; +ntfschar NTFS_INDEX_SDH[5] = { const_cpu_to_le16('$'), const_cpu_to_le16('S'), + const_cpu_to_le16('D'), const_cpu_to_le16('H'), + const_cpu_to_le16('\0') }; +ntfschar NTFS_INDEX_O[3] = { const_cpu_to_le16('$'), const_cpu_to_le16('O'), + const_cpu_to_le16('\0') }; +ntfschar NTFS_INDEX_Q[3] = { const_cpu_to_le16('$'), const_cpu_to_le16('Q'), + const_cpu_to_le16('\0') }; +ntfschar NTFS_INDEX_R[3] = { const_cpu_to_le16('$'), const_cpu_to_le16('R'), + const_cpu_to_le16('\0') }; + +#if CACHE_INODE_SIZE + +/* + * Pathname hashing + * + * Based on first char and second char (which may be '\0') + */ + +int ntfs_dir_inode_hash(const struct CACHED_GENERIC *cached) +{ + const char *path; + const unsigned char *name; + + path = (const char*)cached->variable; + if (!path) { + ntfs_log_error("Bad inode cache entry\n"); + return (-1); + } + name = (const unsigned char*)strrchr(path,'/'); + if (!name) + name = (const unsigned char*)path; + return (((name[0] << 1) + name[1] + strlen((const char*)name)) + % (2*CACHE_INODE_SIZE)); +} + +/* + * Pathname comparing for entering/fetching from cache + */ + +static int inode_cache_compare(const struct CACHED_GENERIC *cached, + const struct CACHED_GENERIC *wanted) +{ + return (!cached->variable + || strcmp(cached->variable, wanted->variable)); +} + +/* + * Pathname comparing for invalidating entries in cache + * + * A partial path is compared in order to invalidate all paths + * related to a renamed directory + * inode numbers are also checked, as deleting a long name may + * imply deleting a short name and conversely + * + * Only use associated with a CACHE_NOHASH flag + */ + +static int inode_cache_inv_compare(const struct CACHED_GENERIC *cached, + const struct CACHED_GENERIC *wanted) +{ + int len; + BOOL different; + const struct CACHED_INODE *w; + const struct CACHED_INODE *c; + + w = (const struct CACHED_INODE*)wanted; + c = (const struct CACHED_INODE*)cached; + if (w->pathname) { + len = strlen(w->pathname); + different = !cached->variable + || ((w->inum != MREF(c->inum)) + && (strncmp(c->pathname, w->pathname, len) + || ((c->pathname[len] != '\0') + && (c->pathname[len] != '/')))); + } else + different = !c->pathname + || (w->inum != MREF(c->inum)); + return (different); +} + +#endif + +#if CACHE_LOOKUP_SIZE + +/* + * File name comparing for entering/fetching from lookup cache + */ + +static int lookup_cache_compare(const struct CACHED_GENERIC *cached, + const struct CACHED_GENERIC *wanted) +{ + const struct CACHED_LOOKUP *c = (const struct CACHED_LOOKUP*) cached; + const struct CACHED_LOOKUP *w = (const struct CACHED_LOOKUP*) wanted; + return (!c->name + || (c->parent != w->parent) + || (c->namesize != w->namesize) + || memcmp(c->name, w->name, c->namesize)); +} + +/* + * Inode number comparing for invalidating lookup cache + * + * All entries with designated inode number are invalidated + * + * Only use associated with a CACHE_NOHASH flag + */ + +static int lookup_cache_inv_compare(const struct CACHED_GENERIC *cached, + const struct CACHED_GENERIC *wanted) +{ + const struct CACHED_LOOKUP *c = (const struct CACHED_LOOKUP*) cached; + const struct CACHED_LOOKUP *w = (const struct CACHED_LOOKUP*) wanted; + return (!c->name + || (c->parent != w->parent) + || (MREF(c->inum) != MREF(w->inum))); +} + +/* + * Lookup hashing + * + * Based on first, second and and last char + */ + +int ntfs_dir_lookup_hash(const struct CACHED_GENERIC *cached) +{ + const unsigned char *name; + int count; + unsigned int val; + + name = (const unsigned char*)cached->variable; + count = cached->varsize; + if (!name || !count) { + ntfs_log_error("Bad lookup cache entry\n"); + return (-1); + } + val = (name[0] << 2) + (name[1] << 1) + name[count - 1] + count; + return (val % (2*CACHE_LOOKUP_SIZE)); +} + +#endif + +/** + * ntfs_inode_lookup_by_name - find an inode in a directory given its name + * @dir_ni: ntfs inode of the directory in which to search for the name + * @uname: Unicode name for which to search in the directory + * @uname_len: length of the name @uname in Unicode characters + * + * Look for an inode with name @uname in the directory with inode @dir_ni. + * ntfs_inode_lookup_by_name() walks the contents of the directory looking for + * the Unicode name. If the name is found in the directory, the corresponding + * inode number (>= 0) is returned as a mft reference in cpu format, i.e. it + * is a 64-bit number containing the sequence number. + * + * On error, return -1 with errno set to the error code. If the inode is is not + * found errno is ENOENT. + * + * Note, @uname_len does not include the (optional) terminating NULL character. + * + * Note, we look for a case sensitive match first but we also look for a case + * insensitive match at the same time. If we find a case insensitive match, we + * save that for the case that we don't find an exact match, where we return + * the mft reference of the case insensitive match. + * + * If the volume is mounted with the case sensitive flag set, then we only + * allow exact matches. + */ +u64 ntfs_inode_lookup_by_name(ntfs_inode *dir_ni, + const ntfschar *uname, const int uname_len) +{ + VCN vcn; + u64 mref = 0; + s64 br; + ntfs_volume *vol = dir_ni->vol; + ntfs_attr_search_ctx *ctx; + INDEX_ROOT *ir; + INDEX_ENTRY *ie; + INDEX_ALLOCATION *ia; + IGNORE_CASE_BOOL case_sensitivity; + u8 *index_end; + ntfs_attr *ia_na; + int eo, rc; + u32 index_block_size; + u8 index_vcn_size_bits; + + ntfs_log_trace("Entering\n"); + + if (!dir_ni || !dir_ni->mrec || !uname || uname_len <= 0) { + errno = EINVAL; + return -1; + } + + ctx = ntfs_attr_get_search_ctx(dir_ni, NULL); + if (!ctx) + return -1; + + /* Find the index root attribute in the mft record. */ + if (ntfs_attr_lookup(AT_INDEX_ROOT, NTFS_INDEX_I30, 4, CASE_SENSITIVE, 0, NULL, + 0, ctx)) { + ntfs_log_perror("Index root attribute missing in directory inode " + "%lld", (unsigned long long)dir_ni->mft_no); + goto put_err_out; + } + case_sensitivity = (NVolCaseSensitive(vol) ? CASE_SENSITIVE : IGNORE_CASE); + /* Get to the index root value. */ + ir = (INDEX_ROOT*)((u8*)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + index_block_size = le32_to_cpu(ir->index_block_size); + if (index_block_size < NTFS_BLOCK_SIZE || + index_block_size & (index_block_size - 1)) { + ntfs_log_error("Index block size %u is invalid.\n", + (unsigned)index_block_size); + goto put_err_out; + } + /* Consistency check of ir done while fetching attribute */ + index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); + /* The first index entry. */ + ie = (INDEX_ENTRY*)((u8*)&ir->index + + le32_to_cpu(ir->index.entries_offset)); + /* + * Loop until we exceed valid memory (corruption case) or until we + * reach the last entry. + */ + for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { + /* Bounds checks. */ + if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie + + sizeof(INDEX_ENTRY_HEADER) > index_end || + (u8*)ie + le16_to_cpu(ie->length) > + index_end) { + ntfs_log_error("Index root entry out of bounds in" + " inode %lld\n", + (unsigned long long)dir_ni->mft_no); + goto put_err_out; + } + /* + * The last entry cannot contain a name. It can however contain + * a pointer to a child node in the B+tree so we just break out. + */ + if (ie->ie_flags & INDEX_ENTRY_END) + break; + + /* The file name must not overflow from the entry */ + if (ntfs_index_entry_inconsistent(ie, COLLATION_FILE_NAME, + dir_ni->mft_no)) { + errno = EIO; + goto put_err_out; + } + /* + * Not a perfect match, need to do full blown collation so we + * know which way in the B+tree we have to go. + */ + rc = ntfs_names_full_collate(uname, uname_len, + (ntfschar*)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length, + case_sensitivity, vol->upcase, vol->upcase_len); + /* + * If uname collates before the name of the current entry, there + * is definitely no such name in this index but we might need to + * descend into the B+tree so we just break out of the loop. + */ + if (rc == -1) + break; + /* The names are not equal, continue the search. */ + if (rc) + continue; + /* + * Perfect match, this will never happen as the + * ntfs_are_names_equal() call will have gotten a match but we + * still treat it correctly. + */ + mref = le64_to_cpu(ie->indexed_file); + ntfs_attr_put_search_ctx(ctx); + return mref; + } + /* + * We have finished with this index without success. Check for the + * presence of a child node and if not present return error code + * ENOENT, unless we have got the mft reference of a matching name + * cached in mref in which case return mref. + */ + if (!(ie->ie_flags & INDEX_ENTRY_NODE)) { + ntfs_attr_put_search_ctx(ctx); + if (mref) + return mref; + ntfs_log_debug("Entry not found - between root entries.\n"); + errno = ENOENT; + return -1; + } /* Child node present, descend into it. */ + + /* Open the index allocation attribute. */ + ia_na = ntfs_attr_open(dir_ni, AT_INDEX_ALLOCATION, NTFS_INDEX_I30, 4); + if (!ia_na) { + ntfs_log_perror("Failed to open index allocation (inode %lld)", + (unsigned long long)dir_ni->mft_no); + goto put_err_out; + } + + /* Allocate a buffer for the current index block. */ + ia = ntfs_malloc(index_block_size); + if (!ia) { + ntfs_attr_close(ia_na); + goto put_err_out; + } + + /* Determine the size of a vcn in the directory index. */ + if (vol->cluster_size <= index_block_size) { + index_vcn_size_bits = vol->cluster_size_bits; + } else { + index_vcn_size_bits = NTFS_BLOCK_SIZE_BITS; + } + + /* Get the starting vcn of the index_block holding the child node. */ + vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8)); + +descend_into_child_node: + + /* Read the index block starting at vcn. */ + br = ntfs_attr_mst_pread(ia_na, vcn << index_vcn_size_bits, 1, + index_block_size, ia); + if (br != 1) { + if (br != -1) + errno = EIO; + ntfs_log_perror("Failed to read vcn 0x%llx from inode %lld", + (unsigned long long)vcn, + (unsigned long long)ia_na->ni->mft_no); + goto close_err_out; + } + + if (ntfs_index_block_inconsistent((INDEX_BLOCK*)ia, index_block_size, + ia_na->ni->mft_no, vcn)) { + errno = EIO; + goto close_err_out; + } + index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); + + /* The first index entry. */ + ie = (INDEX_ENTRY*)((u8*)&ia->index + + le32_to_cpu(ia->index.entries_offset)); + /* + * Iterate similar to above big loop but applied to index buffer, thus + * loop until we exceed valid memory (corruption case) or until we + * reach the last entry. + */ + for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { + /* Bounds check. */ + if ((u8*)ie < (u8*)ia || (u8*)ie + + sizeof(INDEX_ENTRY_HEADER) > index_end || + (u8*)ie + le16_to_cpu(ie->length) > + index_end) { + ntfs_log_error("Index entry out of bounds in directory " + "inode %lld.\n", + (unsigned long long)dir_ni->mft_no); + errno = EIO; + goto close_err_out; + } + /* + * The last entry cannot contain a name. It can however contain + * a pointer to a child node in the B+tree so we just break out. + */ + if (ie->ie_flags & INDEX_ENTRY_END) + break; + + /* The file name must not overflow from the entry */ + if (ntfs_index_entry_inconsistent(ie, COLLATION_FILE_NAME, + dir_ni->mft_no)) { + errno = EIO; + goto close_err_out; + } + /* + * Not a perfect match, need to do full blown collation so we + * know which way in the B+tree we have to go. + */ + rc = ntfs_names_full_collate(uname, uname_len, + (ntfschar*)&ie->key.file_name.file_name, + ie->key.file_name.file_name_length, + case_sensitivity, vol->upcase, vol->upcase_len); + /* + * If uname collates before the name of the current entry, there + * is definitely no such name in this index but we might need to + * descend into the B+tree so we just break out of the loop. + */ + if (rc == -1) + break; + /* The names are not equal, continue the search. */ + if (rc) + continue; + mref = le64_to_cpu(ie->indexed_file); + free(ia); + ntfs_attr_close(ia_na); + ntfs_attr_put_search_ctx(ctx); + return mref; + } + /* + * We have finished with this index buffer without success. Check for + * the presence of a child node. + */ + if (ie->ie_flags & INDEX_ENTRY_NODE) { + if ((ia->index.ih_flags & NODE_MASK) == LEAF_NODE) { + ntfs_log_error("Index entry with child node found in a leaf " + "node in directory inode %lld.\n", + (unsigned long long)dir_ni->mft_no); + errno = EIO; + goto close_err_out; + } + /* Child node present, descend into it. */ + vcn = sle64_to_cpup((sle64*)((u8*)ie + le16_to_cpu(ie->length) - 8)); + if (vcn >= 0) + goto descend_into_child_node; + ntfs_log_error("Negative child node vcn in directory inode " + "0x%llx.\n", (unsigned long long)dir_ni->mft_no); + errno = EIO; + goto close_err_out; + } + free(ia); + ntfs_attr_close(ia_na); + ntfs_attr_put_search_ctx(ctx); + /* + * No child node present, return error code ENOENT, unless we have got + * the mft reference of a matching name cached in mref in which case + * return mref. + */ + if (mref) + return mref; + ntfs_log_debug("Entry not found.\n"); + errno = ENOENT; + return -1; +put_err_out: + eo = EIO; + ntfs_log_debug("Corrupt directory. Aborting lookup.\n"); +eo_put_err_out: + ntfs_attr_put_search_ctx(ctx); + errno = eo; + return -1; +close_err_out: + eo = errno; + free(ia); + ntfs_attr_close(ia_na); + goto eo_put_err_out; +} + +/* + * Lookup a file in a directory from its UTF-8 name + * + * The name is first fetched from cache if one is defined + * + * Returns the inode number + * or -1 if not possible (errno tells why) + */ + +u64 ntfs_inode_lookup_by_mbsname(ntfs_inode *dir_ni, const char *name) +{ + int uname_len; + ntfschar *uname = (ntfschar*)NULL; + u64 inum; + char *cached_name; + const char *const_name; + + if (!NVolCaseSensitive(dir_ni->vol)) { + cached_name = ntfs_uppercase_mbs(name, + dir_ni->vol->upcase, dir_ni->vol->upcase_len); + const_name = cached_name; + } else { + cached_name = (char*)NULL; + const_name = name; + } + if (const_name) { +#if CACHE_LOOKUP_SIZE + + /* + * fetch inode from cache + */ + + if (dir_ni->vol->lookup_cache) { + struct CACHED_LOOKUP item; + struct CACHED_LOOKUP *cached; + + item.name = const_name; + item.namesize = strlen(const_name) + 1; + item.parent = dir_ni->mft_no; + cached = (struct CACHED_LOOKUP*)ntfs_fetch_cache( + dir_ni->vol->lookup_cache, + GENERIC(&item), lookup_cache_compare); + if (cached) { + inum = cached->inum; + if (inum == (u64)-1) + errno = ENOENT; + } else { + /* Generate unicode name. */ + uname_len = ntfs_mbstoucs(name, &uname); + if (uname_len >= 0) { + inum = ntfs_inode_lookup_by_name(dir_ni, + uname, uname_len); + item.inum = inum; + /* enter into cache, even if not found */ + ntfs_enter_cache(dir_ni->vol->lookup_cache, + GENERIC(&item), + lookup_cache_compare); + free(uname); + } else + inum = (s64)-1; + } + } else +#endif + { + /* Generate unicode name. */ + uname_len = ntfs_mbstoucs(cached_name, &uname); + if (uname_len >= 0) + inum = ntfs_inode_lookup_by_name(dir_ni, + uname, uname_len); + else + inum = (s64)-1; + } + if (cached_name) + free(cached_name); + } else + inum = (s64)-1; + return (inum); +} + +/* + * Update a cache lookup record when a name has been defined + * + * The UTF-8 name is required + */ + +void ntfs_inode_update_mbsname(ntfs_inode *dir_ni, const char *name, u64 inum) +{ +#if CACHE_LOOKUP_SIZE + struct CACHED_LOOKUP item; + struct CACHED_LOOKUP *cached; + char *cached_name; + + if (dir_ni->vol->lookup_cache) { + if (!NVolCaseSensitive(dir_ni->vol)) { + cached_name = ntfs_uppercase_mbs(name, + dir_ni->vol->upcase, dir_ni->vol->upcase_len); + item.name = cached_name; + } else { + cached_name = (char*)NULL; + item.name = name; + } + if (item.name) { + item.namesize = strlen(item.name) + 1; + item.parent = dir_ni->mft_no; + item.inum = inum; + cached = (struct CACHED_LOOKUP*)ntfs_enter_cache( + dir_ni->vol->lookup_cache, + GENERIC(&item), lookup_cache_compare); + if (cached) + cached->inum = inum; + if (cached_name) + free(cached_name); + } + } +#endif +} + +/** + * ntfs_pathname_to_inode - Find the inode which represents the given pathname + * @vol: An ntfs volume obtained from ntfs_mount + * @parent: A directory inode to begin the search (may be NULL) + * @pathname: Pathname to be located + * + * Take an ASCII pathname and find the inode that represents it. The function + * splits the path and then descends the directory tree. If @parent is NULL, + * then the root directory '.' will be used as the base for the search. + * + * Return: inode Success, the pathname was valid + * NULL Error, the pathname was invalid, or some other error occurred + */ +ntfs_inode *ntfs_pathname_to_inode(ntfs_volume *vol, ntfs_inode *parent, + const char *pathname) +{ + u64 inum; + int len, err = 0; + char *p, *q; + ntfs_inode *ni; + ntfs_inode *result = NULL; + ntfschar *unicode = NULL; + char *ascii = NULL; +#if CACHE_INODE_SIZE + struct CACHED_INODE item; + struct CACHED_INODE *cached; + char *fullname; +#endif + + if (!vol || !pathname) { + errno = EINVAL; + return NULL; + } + + ntfs_log_trace("path: '%s'\n", pathname); + + ascii = strdup(pathname); + if (!ascii) { + ntfs_log_error("Out of memory.\n"); + err = ENOMEM; + goto out; + } + + p = ascii; + /* Remove leading /'s. */ + while (p && *p && *p == PATH_SEP) + p++; +#if CACHE_INODE_SIZE + fullname = p; + if (p[0] && (p[strlen(p)-1] == PATH_SEP)) + ntfs_log_error("Unnormalized path %s\n",ascii); +#endif + if (parent) { + ni = parent; + } else { +#if CACHE_INODE_SIZE + /* + * fetch inode for full path from cache + */ + if (*fullname) { + item.pathname = fullname; + item.varsize = strlen(fullname) + 1; + cached = (struct CACHED_INODE*)ntfs_fetch_cache( + vol->xinode_cache, GENERIC(&item), + inode_cache_compare); + } else + cached = (struct CACHED_INODE*)NULL; + if (cached) { + /* + * return opened inode if found in cache + */ + inum = MREF(cached->inum); + ni = ntfs_inode_open(vol, inum); + if (!ni) { + ntfs_log_debug("Cannot open inode %llu: %s.\n", + (unsigned long long)inum, p); + err = EIO; + } + result = ni; + goto out; + } +#endif + ni = ntfs_inode_open(vol, FILE_root); + if (!ni) { + ntfs_log_debug("Couldn't open the inode of the root " + "directory.\n"); + err = EIO; + result = (ntfs_inode*)NULL; + goto out; + } + } + + while (p && *p) { + /* Find the end of the first token. */ + q = strchr(p, PATH_SEP); + if (q != NULL) { + *q = '\0'; + } +#if CACHE_INODE_SIZE + /* + * fetch inode for partial path from cache + */ + cached = (struct CACHED_INODE*)NULL; + if (!parent) { + item.pathname = fullname; + item.varsize = strlen(fullname) + 1; + cached = (struct CACHED_INODE*)ntfs_fetch_cache( + vol->xinode_cache, GENERIC(&item), + inode_cache_compare); + if (cached) { + inum = cached->inum; + } + } + /* + * if not in cache, translate, search, then + * insert into cache if found + */ + if (!cached) { + len = ntfs_mbstoucs(p, &unicode); + if (len < 0) { + ntfs_log_perror("Could not convert filename to Unicode:" + " '%s'", p); + err = errno; + goto close; + } else if (len > NTFS_MAX_NAME_LEN) { + err = ENAMETOOLONG; + goto close; + } + inum = ntfs_inode_lookup_by_name(ni, unicode, len); + if (!parent && (inum != (u64) -1)) { + item.inum = inum; + ntfs_enter_cache(vol->xinode_cache, + GENERIC(&item), + inode_cache_compare); + } + } +#else + len = ntfs_mbstoucs(p, &unicode); + if (len < 0) { + ntfs_log_perror("Could not convert filename to Unicode:" + " '%s'", p); + err = errno; + goto close; + } else if (len > NTFS_MAX_NAME_LEN) { + err = ENAMETOOLONG; + goto close; + } + inum = ntfs_inode_lookup_by_name(ni, unicode, len); +#endif + if (inum == (u64) -1) { + ntfs_log_debug("Couldn't find name '%s' in pathname " + "'%s'.\n", p, pathname); + err = ENOENT; + goto close; + } + + if (ni != parent) + if (ntfs_inode_close(ni)) { + err = errno; + goto out; + } + + inum = MREF(inum); + ni = ntfs_inode_open(vol, inum); + if (!ni) { + ntfs_log_debug("Cannot open inode %llu: %s.\n", + (unsigned long long)inum, p); + err = EIO; + goto close; + } + + free(unicode); + unicode = NULL; + + if (q) *q++ = PATH_SEP; /* JPA */ + p = q; + while (p && *p && *p == PATH_SEP) + p++; + } + + result = ni; + ni = NULL; +close: + if (ni && (ni != parent)) + if (ntfs_inode_close(ni) && !err) + err = errno; +out: + free(ascii); + free(unicode); + if (err) + errno = err; + return result; +} + +/* + * The little endian Unicode string ".." for ntfs_readdir(). + */ +static const ntfschar dotdot[3] = { const_cpu_to_le16('.'), + const_cpu_to_le16('.'), + const_cpu_to_le16('\0') }; + +/* + * union index_union - + * More helpers for ntfs_readdir(). + */ + +/** + * enum INDEX_TYPE - + * More helpers for ntfs_readdir(). + */ +typedef enum { + INDEX_TYPE_ROOT, /* index root */ + INDEX_TYPE_ALLOCATION, /* index allocation */ +} INDEX_TYPE; + +/* + * Decode Interix file types + * + * Non-Interix types are returned as plain files, because a + * Windows user may force patterns very similar to Interix, + * and most metadata files have such similar patters. + */ + +u32 ntfs_interix_types(ntfs_inode *ni) +{ + ntfs_attr *na; + u32 dt_type; + le64 magic; + + dt_type = NTFS_DT_UNKNOWN; + na = ntfs_attr_open(ni, AT_DATA, NULL, 0); + if (na) { + /* + * Unrecognized patterns (eg HID + SYST for metadata) + * are plain files or directories + */ + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + dt_type = NTFS_DT_DIR; + else + dt_type = NTFS_DT_REG; + if (na->data_size <= 1) { + if (!(ni->flags & FILE_ATTR_HIDDEN)) + dt_type = (na->data_size ? + NTFS_DT_SOCK : NTFS_DT_FIFO); + } else { + if ((na->data_size >= (s64)sizeof(magic)) + && (ntfs_attr_pread(na, 0, sizeof(magic), &magic) + == sizeof(magic))) { + if (magic == INTX_SYMBOLIC_LINK) + dt_type = NTFS_DT_LNK; + else if (magic == INTX_BLOCK_DEVICE) + dt_type = NTFS_DT_BLK; + else if (magic == INTX_CHARACTER_DEVICE) + dt_type = NTFS_DT_CHR; + } + } + ntfs_attr_close(na); + } + return (dt_type); +} + +/* + * Decode file types + * + * Better only use for Interix types and junctions, + * unneeded complexity when used for plain files or directories + * + * Error cases are logged and returned as unknown. + */ + +static u32 ntfs_dir_entry_type(ntfs_inode *dir_ni, MFT_REF mref, + FILE_ATTR_FLAGS attributes) +{ + ntfs_inode *ni; + u32 dt_type; + + dt_type = NTFS_DT_UNKNOWN; + ni = ntfs_inode_open(dir_ni->vol, mref); + if (ni) { + if (attributes & FILE_ATTR_REPARSE_POINT) + dt_type = (ntfs_possible_symlink(ni) + ? NTFS_DT_LNK : NTFS_DT_REPARSE); + else + if ((attributes & FILE_ATTR_SYSTEM) + && !(attributes & FILE_ATTR_I30_INDEX_PRESENT)) + dt_type = ntfs_interix_types(ni); + else + dt_type = (attributes + & FILE_ATTR_I30_INDEX_PRESENT + ? NTFS_DT_DIR : NTFS_DT_REG); + if (ntfs_inode_close(ni)) { + /* anything special worth doing ? */ + ntfs_log_error("Failed to close inode %lld\n", + (long long)MREF(mref)); + } + } + if (dt_type == NTFS_DT_UNKNOWN) + ntfs_log_error("Could not decode the type of inode %lld\n", + (long long)MREF(mref)); + return (dt_type); +} + +/** + * ntfs_filldir - ntfs specific filldir method + * @dir_ni: ntfs inode of current directory + * @pos: current position in directory + * @ivcn_bits: log(2) of index vcn size + * @index_type: specifies whether @iu is an index root or an index allocation + * @iu: index root or index block to which @ie belongs + * @ie: current index entry + * @dirent: context for filldir callback supplied by the caller + * @filldir: filldir callback supplied by the caller + * + * Pass information specifying the current directory entry @ie to the @filldir + * callback. + */ +static int ntfs_filldir(ntfs_inode *dir_ni, s64 *pos, u8 ivcn_bits, + const INDEX_TYPE index_type, void *index_block, INDEX_ENTRY *ie, + void *dirent, ntfs_filldir_t filldir) +{ + FILE_NAME_ATTR *fn = &ie->key.file_name; + unsigned dt_type; + BOOL metadata; + ntfschar *loname; + int res; + MFT_REF mref; + + ntfs_log_trace("Entering.\n"); + + /* Advance the position even if going to skip the entry. */ + if (index_type == INDEX_TYPE_ALLOCATION) + *pos = (u8*)ie - (u8*)(INDEX_ALLOCATION*)index_block + (sle64_to_cpu( + ((INDEX_ALLOCATION*)index_block)->index_block_vcn) << ivcn_bits) + + dir_ni->vol->mft_record_size; + else /* if (index_type == INDEX_TYPE_ROOT) */ + *pos = (u8*)ie - (u8*)(INDEX_ROOT*)index_block; + mref = le64_to_cpu(ie->indexed_file); + metadata = (MREF(mref) != FILE_root) && (MREF(mref) < FILE_first_user); + /* Skip root directory self reference entry. */ + if (MREF_LE(ie->indexed_file) == FILE_root) + return 0; + if ((ie->key.file_name.file_attributes + & (FILE_ATTR_REPARSE_POINT | FILE_ATTR_SYSTEM)) + && !metadata) + dt_type = ntfs_dir_entry_type(dir_ni, mref, + ie->key.file_name.file_attributes); + else if (ie->key.file_name.file_attributes + & FILE_ATTR_I30_INDEX_PRESENT) + dt_type = NTFS_DT_DIR; + else + dt_type = NTFS_DT_REG; + + /* return metadata files and hidden files if requested */ + if ((!metadata && (NVolShowHidFiles(dir_ni->vol) + || !(fn->file_attributes & FILE_ATTR_HIDDEN))) + || (NVolShowSysFiles(dir_ni->vol) && (NVolShowHidFiles(dir_ni->vol) + || metadata))) { + if (NVolCaseSensitive(dir_ni->vol)) { + res = filldir(dirent, fn->file_name, + fn->file_name_length, + fn->file_name_type, *pos, + mref, dt_type); + } else { + loname = (ntfschar*)ntfs_malloc(2*fn->file_name_length); + if (loname) { + memcpy(loname, fn->file_name, + 2*fn->file_name_length); + ntfs_name_locase(loname, fn->file_name_length, + dir_ni->vol->locase, + dir_ni->vol->upcase_len); + res = filldir(dirent, loname, + fn->file_name_length, + fn->file_name_type, *pos, + mref, dt_type); + free(loname); + } else + res = -1; + } + } else + res = 0; + return (res); +} + +/** + * ntfs_mft_get_parent_ref - find mft reference of parent directory of an inode + * @ni: ntfs inode whose parent directory to find + * + * Find the parent directory of the ntfs inode @ni. To do this, find the first + * file name attribute in the mft record of @ni and return the parent mft + * reference from that. + * + * Note this only makes sense for directories, since files can be hard linked + * from multiple directories and there is no way for us to tell which one is + * being looked for. + * + * Technically directories can have hard links, too, but we consider that as + * illegal as Linux/UNIX do not support directory hard links. + * + * Return the mft reference of the parent directory on success or -1 on error + * with errno set to the error code. + */ +static MFT_REF ntfs_mft_get_parent_ref(ntfs_inode *ni) +{ + MFT_REF mref; + ntfs_attr_search_ctx *ctx; + FILE_NAME_ATTR *fn; + int eo; + + ntfs_log_trace("Entering.\n"); + + if (!ni) { + errno = EINVAL; + return ERR_MREF(-1); + } + + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return ERR_MREF(-1); + if (ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx)) { + ntfs_log_error("No file name found in inode %lld\n", + (unsigned long long)ni->mft_no); + goto err_out; + } + if (ctx->attr->non_resident) { + ntfs_log_error("File name attribute must be resident (inode " + "%lld)\n", (unsigned long long)ni->mft_no); + goto io_err_out; + } + fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + mref = le64_to_cpu(fn->parent_directory); + ntfs_attr_put_search_ctx(ctx); + return mref; +io_err_out: + errno = EIO; +err_out: + eo = errno; + ntfs_attr_put_search_ctx(ctx); + errno = eo; + return ERR_MREF(-1); +} + +/** + * ntfs_readdir - read the contents of an ntfs directory + * @dir_ni: ntfs inode of current directory + * @pos: current position in directory + * @dirent: context for filldir callback supplied by the caller + * @filldir: filldir callback supplied by the caller + * + * Parse the index root and the index blocks that are marked in use in the + * index bitmap and hand each found directory entry to the @filldir callback + * supplied by the caller. + * + * Return 0 on success or -1 on error with errno set to the error code. + * + * Note: Index blocks are parsed in ascending vcn order, from which follows + * that the directory entries are not returned sorted. + */ +int ntfs_readdir(ntfs_inode *dir_ni, s64 *pos, + void *dirent, ntfs_filldir_t filldir) +{ + s64 i_size, br, ia_pos, bmp_pos, ia_start; + ntfs_volume *vol; + ntfs_attr *ia_na, *bmp_na = NULL; + ntfs_attr_search_ctx *ctx = NULL; + u8 *index_end, *bmp = NULL; + INDEX_ROOT *ir; + INDEX_ENTRY *ie; + INDEX_ALLOCATION *ia = NULL; + int rc, ir_pos, bmp_buf_size, bmp_buf_pos, eo; + u32 index_block_size; + u8 index_block_size_bits, index_vcn_size_bits; + + ntfs_log_trace("Entering.\n"); + + if (!dir_ni || !pos || !filldir) { + errno = EINVAL; + return -1; + } + + if (!(dir_ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)) { + errno = ENOTDIR; + return -1; + } + + vol = dir_ni->vol; + + ntfs_log_trace("Entering for inode %lld, *pos 0x%llx.\n", + (unsigned long long)dir_ni->mft_no, (long long)*pos); + + /* Open the index allocation attribute. */ + ia_na = ntfs_attr_open(dir_ni, AT_INDEX_ALLOCATION, NTFS_INDEX_I30, 4); + if (!ia_na) { + if (errno != ENOENT) { + ntfs_log_perror("Failed to open index allocation attribute. " + "Directory inode %lld is corrupt or bug", + (unsigned long long)dir_ni->mft_no); + return -1; + } + i_size = 0; + } else + i_size = ia_na->data_size; + + rc = 0; + + /* Are we at end of dir yet? */ + if (*pos >= i_size + vol->mft_record_size) + goto done; + + /* Emulate . and .. for all directories. */ + if (!*pos) { + rc = filldir(dirent, dotdot, 1, FILE_NAME_POSIX, *pos, + MK_MREF(dir_ni->mft_no, + le16_to_cpu(dir_ni->mrec->sequence_number)), + NTFS_DT_DIR); + if (rc) + goto err_out; + ++*pos; + } + if (*pos == 1) { + MFT_REF parent_mref; + + parent_mref = ntfs_mft_get_parent_ref(dir_ni); + if (parent_mref == ERR_MREF(-1)) { + ntfs_log_perror("Parent directory not found"); + goto dir_err_out; + } + + rc = filldir(dirent, dotdot, 2, FILE_NAME_POSIX, *pos, + parent_mref, NTFS_DT_DIR); + if (rc) + goto err_out; + ++*pos; + } + + ctx = ntfs_attr_get_search_ctx(dir_ni, NULL); + if (!ctx) + goto err_out; + + /* Get the offset into the index root attribute. */ + ir_pos = (int)*pos; + /* Find the index root attribute in the mft record. */ + if (ntfs_attr_lookup(AT_INDEX_ROOT, NTFS_INDEX_I30, 4, CASE_SENSITIVE, 0, NULL, + 0, ctx)) { + ntfs_log_perror("Index root attribute missing in directory inode " + "%lld", (unsigned long long)dir_ni->mft_no); + goto dir_err_out; + } + /* Get to the index root value. */ + ir = (INDEX_ROOT*)((u8*)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + + /* Determine the size of a vcn in the directory index. */ + index_block_size = le32_to_cpu(ir->index_block_size); + if (index_block_size < NTFS_BLOCK_SIZE || + index_block_size & (index_block_size - 1)) { + ntfs_log_error("Index block size %u is invalid.\n", + (unsigned)index_block_size); + goto dir_err_out; + } + index_block_size_bits = ffs(index_block_size) - 1; + if (vol->cluster_size <= index_block_size) { + index_vcn_size_bits = vol->cluster_size_bits; + } else { + index_vcn_size_bits = NTFS_BLOCK_SIZE_BITS; + } + + /* Are we jumping straight into the index allocation attribute? */ + if (*pos >= vol->mft_record_size) { + ntfs_attr_put_search_ctx(ctx); + ctx = NULL; + goto skip_index_root; + } + + index_end = (u8*)&ir->index + le32_to_cpu(ir->index.index_length); + /* The first index entry. */ + ie = (INDEX_ENTRY*)((u8*)&ir->index + + le32_to_cpu(ir->index.entries_offset)); + /* + * Loop until we exceed valid memory (corruption case) or until we + * reach the last entry or until filldir tells us it has had enough + * or signals an error (both covered by the rc test). + */ + for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { + ntfs_log_debug("In index root, offset %d.\n", (int)((u8*)ie - (u8*)ir)); + /* Bounds checks. */ + if ((u8*)ie < (u8*)ctx->mrec || (u8*)ie + + sizeof(INDEX_ENTRY_HEADER) > index_end || + (u8*)ie + le16_to_cpu(ie->length) > + index_end) { + ntfs_log_error("Index root entry out of bounds in" + " inode %lld\n", + (unsigned long long)dir_ni->mft_no); + goto dir_err_out; + } + /* The last entry cannot contain a name. */ + if (ie->ie_flags & INDEX_ENTRY_END) + break; + + if (!le16_to_cpu(ie->length)) + goto dir_err_out; + + /* Skip index root entry if continuing previous readdir. */ + if (ir_pos > (u8*)ie - (u8*)ir) + continue; + + /* The file name must not overflow from the entry */ + if (ntfs_index_entry_inconsistent(ie, COLLATION_FILE_NAME, + dir_ni->mft_no)) { + errno = EIO; + goto dir_err_out; + } + /* + * Submit the directory entry to ntfs_filldir(), which will + * invoke the filldir() callback as appropriate. + */ + rc = ntfs_filldir(dir_ni, pos, index_vcn_size_bits, + INDEX_TYPE_ROOT, ir, ie, dirent, filldir); + if (rc) { + ntfs_attr_put_search_ctx(ctx); + ctx = NULL; + goto err_out; + } + } + ntfs_attr_put_search_ctx(ctx); + ctx = NULL; + + /* If there is no index allocation attribute we are finished. */ + if (!ia_na) + goto EOD; + + /* Advance *pos to the beginning of the index allocation. */ + *pos = vol->mft_record_size; + +skip_index_root: + + if (!ia_na) + goto done; + + /* Allocate a buffer for the current index block. */ + ia = ntfs_malloc(index_block_size); + if (!ia) + goto err_out; + + bmp_na = ntfs_attr_open(dir_ni, AT_BITMAP, NTFS_INDEX_I30, 4); + if (!bmp_na) { + ntfs_log_perror("Failed to open index bitmap attribute"); + goto dir_err_out; + } + + /* Get the offset into the index allocation attribute. */ + ia_pos = *pos - vol->mft_record_size; + + bmp_pos = ia_pos >> index_block_size_bits; + if (bmp_pos >> 3 >= bmp_na->data_size) { + ntfs_log_error("Current index position exceeds index bitmap " + "size.\n"); + goto dir_err_out; + } + + bmp_buf_size = min(bmp_na->data_size - (bmp_pos >> 3), 4096); + bmp = ntfs_malloc(bmp_buf_size); + if (!bmp) + goto err_out; + + br = ntfs_attr_pread(bmp_na, bmp_pos >> 3, bmp_buf_size, bmp); + if (br != bmp_buf_size) { + if (br != -1) + errno = EIO; + ntfs_log_perror("Failed to read from index bitmap attribute"); + goto err_out; + } + + bmp_buf_pos = 0; + /* If the index block is not in use find the next one that is. */ + while (!(bmp[bmp_buf_pos >> 3] & (1 << (bmp_buf_pos & 7)))) { +find_next_index_buffer: + bmp_pos++; + bmp_buf_pos++; + /* If we have reached the end of the bitmap, we are done. */ + if (bmp_pos >> 3 >= bmp_na->data_size) + goto EOD; + ia_pos = bmp_pos << index_block_size_bits; + if (bmp_buf_pos >> 3 < bmp_buf_size) + continue; + /* Read next chunk from the index bitmap. */ + bmp_buf_pos = 0; + if ((bmp_pos >> 3) + bmp_buf_size > bmp_na->data_size) + bmp_buf_size = bmp_na->data_size - (bmp_pos >> 3); + br = ntfs_attr_pread(bmp_na, bmp_pos >> 3, bmp_buf_size, bmp); + if (br != bmp_buf_size) { + if (br != -1) + errno = EIO; + ntfs_log_perror("Failed to read from index bitmap attribute"); + goto err_out; + } + } + + ntfs_log_debug("Handling index block 0x%llx.\n", (long long)bmp_pos); + + /* Read the index block starting at bmp_pos. */ + br = ntfs_attr_mst_pread(ia_na, bmp_pos << index_block_size_bits, 1, + index_block_size, ia); + if (br != 1) { + if (br != -1) + errno = EIO; + ntfs_log_perror("Failed to read index block"); + goto err_out; + } + + ia_start = ia_pos & ~(s64)(index_block_size - 1); + if (ntfs_index_block_inconsistent((INDEX_BLOCK*)ia, index_block_size, + ia_na->ni->mft_no, ia_start >> index_vcn_size_bits)) { + goto dir_err_out; + } + index_end = (u8*)&ia->index + le32_to_cpu(ia->index.index_length); + + /* The first index entry. */ + ie = (INDEX_ENTRY*)((u8*)&ia->index + + le32_to_cpu(ia->index.entries_offset)); + /* + * Loop until we exceed valid memory (corruption case) or until we + * reach the last entry or until ntfs_filldir tells us it has had + * enough or signals an error (both covered by the rc test). + */ + for (;; ie = (INDEX_ENTRY*)((u8*)ie + le16_to_cpu(ie->length))) { + ntfs_log_debug("In index allocation, offset 0x%llx.\n", + (long long)ia_start + ((u8*)ie - (u8*)ia)); + /* Bounds checks. */ + if ((u8*)ie < (u8*)ia || (u8*)ie + + sizeof(INDEX_ENTRY_HEADER) > index_end || + (u8*)ie + le16_to_cpu(ie->length) > + index_end) { + ntfs_log_error("Index entry out of bounds in directory inode " + "%lld.\n", (unsigned long long)dir_ni->mft_no); + goto dir_err_out; + } + /* The last entry cannot contain a name. */ + if (ie->ie_flags & INDEX_ENTRY_END) + break; + + if (!le16_to_cpu(ie->length)) + goto dir_err_out; + + /* Skip index entry if continuing previous readdir. */ + if (ia_pos - ia_start > (u8*)ie - (u8*)ia) + continue; + + /* The file name must not overflow from the entry */ + if (ntfs_index_entry_inconsistent(ie, COLLATION_FILE_NAME, + dir_ni->mft_no)) { + errno = EIO; + goto dir_err_out; + } + /* + * Submit the directory entry to ntfs_filldir(), which will + * invoke the filldir() callback as appropriate. + */ + rc = ntfs_filldir(dir_ni, pos, index_vcn_size_bits, + INDEX_TYPE_ALLOCATION, ia, ie, dirent, filldir); + if (rc) + goto err_out; + } + goto find_next_index_buffer; +EOD: + /* We are finished, set *pos to EOD. */ + *pos = i_size + vol->mft_record_size; +done: + free(ia); + free(bmp); + if (bmp_na) + ntfs_attr_close(bmp_na); + if (ia_na) + ntfs_attr_close(ia_na); + ntfs_log_debug("EOD, *pos 0x%llx, returning 0.\n", (long long)*pos); + return 0; +dir_err_out: + errno = EIO; +err_out: + eo = errno; + ntfs_log_trace("failed.\n"); + if (ctx) + ntfs_attr_put_search_ctx(ctx); + free(ia); + free(bmp); + if (bmp_na) + ntfs_attr_close(bmp_na); + if (ia_na) + ntfs_attr_close(ia_na); + errno = eo; + return -1; +} + + +/** + * __ntfs_create - create object on ntfs volume + * @dir_ni: ntfs inode for directory in which create new object + * @securid: id of inheritable security descriptor, 0 if none + * @name: unicode name of new object + * @name_len: length of the name in unicode characters + * @type: type of the object to create + * @dev: major and minor device numbers (obtained from makedev()) + * @target: target in unicode (only for symlinks) + * @target_len: length of target in unicode characters + * + * Internal, use ntfs_create{,_device,_symlink} wrappers instead. + * + * @type can be: + * S_IFREG to create regular file + * S_IFDIR to create directory + * S_IFBLK to create block device + * S_IFCHR to create character device + * S_IFLNK to create symbolic link + * S_IFIFO to create FIFO + * S_IFSOCK to create socket + * other values are invalid. + * + * @dev is used only if @type is S_IFBLK or S_IFCHR, in other cases its value + * ignored. + * + * @target and @target_len are used only if @type is S_IFLNK, in other cases + * their value ignored. + * + * Return opened ntfs inode that describes created object on success or NULL + * on error with errno set to the error code. + */ +static ntfs_inode *__ntfs_create(ntfs_inode *dir_ni, le32 securid, + const ntfschar *name, u8 name_len, mode_t type, dev_t dev, + const ntfschar *target, int target_len) +{ + ntfs_inode *ni; + int rollback_data = 0, rollback_sd = 0; + int rollback_dir = 0; + FILE_NAME_ATTR *fn = NULL; + STANDARD_INFORMATION *si = NULL; + int err, fn_len, si_len; + ntfs_volume_special_files special_files; + + ntfs_log_trace("Entering.\n"); + + /* Sanity checks. */ + if (!dir_ni || !name || !name_len) { + ntfs_log_error("Invalid arguments.\n"); + errno = EINVAL; + return NULL; + } + + ni = ntfs_mft_record_alloc(dir_ni->vol, NULL); + if (!ni) + return NULL; +#if CACHE_NIDATA_SIZE + ntfs_inode_invalidate(dir_ni->vol, ni->mft_no); +#endif + special_files = dir_ni->vol->special_files; + /* + * Create STANDARD_INFORMATION attribute. + * JPA Depending on available inherited security descriptor, + * Write STANDARD_INFORMATION v1.2 (no inheritance) or v3 + */ + if (securid) + si_len = offsetof(STANDARD_INFORMATION, v3_end); + else + si_len = offsetof(STANDARD_INFORMATION, v1_end); + si = ntfs_calloc(si_len); + if (!si) { + err = errno; + goto err_out; + } + si->creation_time = ni->creation_time; + si->last_data_change_time = ni->last_data_change_time; + si->last_mft_change_time = ni->last_mft_change_time; + si->last_access_time = ni->last_access_time; + if (securid) { + set_nino_flag(ni, v3_Extensions); + ni->owner_id = si->owner_id = const_cpu_to_le32(0); + ni->security_id = si->security_id = securid; + ni->quota_charged = si->quota_charged = const_cpu_to_le64(0); + ni->usn = si->usn = const_cpu_to_le64(0); + } else + clear_nino_flag(ni, v3_Extensions); + if (!S_ISREG(type) && !S_ISDIR(type)) { + switch (special_files) { + case NTFS_FILES_WSL : + if (!S_ISLNK(type)) { + si->file_attributes + = FILE_ATTRIBUTE_RECALL_ON_OPEN; + ni->flags = FILE_ATTRIBUTE_RECALL_ON_OPEN; + } + break; + default : + si->file_attributes = FILE_ATTR_SYSTEM; + ni->flags = FILE_ATTR_SYSTEM; + break; + } + } + ni->flags |= FILE_ATTR_ARCHIVE; + if (NVolHideDotFiles(dir_ni->vol) + && (name_len > 1) + && (name[0] == const_cpu_to_le16('.')) + && (name[1] != const_cpu_to_le16('.'))) + ni->flags |= FILE_ATTR_HIDDEN; + /* + * Set compression flag according to parent directory + * unless NTFS version < 3.0 or cluster size > 4K + * or compression has been disabled + */ + if ((dir_ni->flags & FILE_ATTR_COMPRESSED) + && (dir_ni->vol->major_ver >= 3) + && NVolCompression(dir_ni->vol) + && (dir_ni->vol->cluster_size <= MAX_COMPRESSION_CLUSTER_SIZE) + && (S_ISREG(type) || S_ISDIR(type))) + ni->flags |= FILE_ATTR_COMPRESSED; + /* Add STANDARD_INFORMATION to inode. */ + if (ntfs_attr_add(ni, AT_STANDARD_INFORMATION, AT_UNNAMED, 0, + (u8*)si, si_len)) { + err = errno; + ntfs_log_error("Failed to add STANDARD_INFORMATION " + "attribute.\n"); + goto err_out; + } + + if (!securid) { + if (ntfs_sd_add_everyone(ni)) { + err = errno; + goto err_out; + } + rollback_sd = 1; + } + + if (S_ISDIR(type)) { + INDEX_ROOT *ir = NULL; + INDEX_ENTRY *ie; + int ir_len, index_len; + + /* Create INDEX_ROOT attribute. */ + index_len = sizeof(INDEX_HEADER) + sizeof(INDEX_ENTRY_HEADER); + ir_len = offsetof(INDEX_ROOT, index) + index_len; + ir = ntfs_calloc(ir_len); + if (!ir) { + err = errno; + goto err_out; + } + ir->type = AT_FILE_NAME; + ir->collation_rule = COLLATION_FILE_NAME; + ir->index_block_size = cpu_to_le32(ni->vol->indx_record_size); + if (ni->vol->cluster_size <= ni->vol->indx_record_size) + ir->clusters_per_index_block = + ni->vol->indx_record_size >> + ni->vol->cluster_size_bits; + else + ir->clusters_per_index_block = + ni->vol->indx_record_size >> + NTFS_BLOCK_SIZE_BITS; + ir->index.entries_offset = const_cpu_to_le32(sizeof(INDEX_HEADER)); + ir->index.index_length = cpu_to_le32(index_len); + ir->index.allocated_size = cpu_to_le32(index_len); + ie = (INDEX_ENTRY*)((u8*)ir + sizeof(INDEX_ROOT)); + ie->length = const_cpu_to_le16(sizeof(INDEX_ENTRY_HEADER)); + ie->key_length = const_cpu_to_le16(0); + ie->ie_flags = INDEX_ENTRY_END; + /* Add INDEX_ROOT attribute to inode. */ + if (ntfs_attr_add(ni, AT_INDEX_ROOT, NTFS_INDEX_I30, 4, + (u8*)ir, ir_len)) { + err = errno; + free(ir); + ntfs_log_error("Failed to add INDEX_ROOT attribute.\n"); + goto err_out; + } + free(ir); + } else { + INTX_FILE *data; + int data_len; + + switch (type) { + case S_IFBLK: + case S_IFCHR: + switch (special_files) { + case NTFS_FILES_WSL : + data_len = 0; + data = (INTX_FILE*)NULL; + break; + default : + data_len = offsetof(INTX_FILE, + device_end); + data = (INTX_FILE*)ntfs_malloc( + data_len); + if (!data) { + err = errno; + goto err_out; + } + data->major = cpu_to_le64(major(dev)); + data->minor = cpu_to_le64(minor(dev)); + if (type == S_IFBLK) + data->magic + = INTX_BLOCK_DEVICE; + if (type == S_IFCHR) + data->magic + = INTX_CHARACTER_DEVICE; + break; + } + break; + case S_IFLNK: + switch (special_files) { + case NTFS_FILES_WSL : + data_len = 0; + data = (INTX_FILE*)NULL; + break; + default : + data_len = sizeof(INTX_FILE_TYPES) + + target_len * sizeof(ntfschar); + data = (INTX_FILE*)ntfs_malloc( + data_len); + if (!data) { + err = errno; + goto err_out; + } + data->magic = INTX_SYMBOLIC_LINK; + memcpy(data->target, target, + target_len * sizeof(ntfschar)); + break; + } + break; + case S_IFSOCK: + data = NULL; + if (special_files == NTFS_FILES_WSL) + data_len = 0; + else + data_len = 1; + break; + default: /* FIFO or regular file. */ + data = NULL; + data_len = 0; + break; + } + /* Add DATA attribute to inode. */ + if (ntfs_attr_add(ni, AT_DATA, AT_UNNAMED, 0, (u8*)data, + data_len)) { + err = errno; + ntfs_log_error("Failed to add DATA attribute.\n"); + free(data); + goto err_out; + } + rollback_data = 1; + free(data); + } + /* Create FILE_NAME attribute. */ + fn_len = offsetof(FILE_NAME_ATTR, file_name) + name_len * sizeof(ntfschar); + fn = ntfs_calloc(fn_len); + if (!fn) { + err = errno; + goto err_out; + } + fn->parent_directory = MK_LE_MREF(dir_ni->mft_no, + le16_to_cpu(dir_ni->mrec->sequence_number)); + fn->file_name_length = name_len; + fn->file_name_type = FILE_NAME_POSIX; + if (S_ISDIR(type)) + fn->file_attributes = FILE_ATTR_I30_INDEX_PRESENT; + if (!S_ISREG(type) && !S_ISDIR(type)) { + if (special_files == NTFS_FILES_INTERIX) + fn->file_attributes = FILE_ATTR_SYSTEM; + } else + fn->file_attributes |= ni->flags & FILE_ATTR_COMPRESSED; + fn->file_attributes |= FILE_ATTR_ARCHIVE; + fn->file_attributes |= ni->flags & FILE_ATTR_HIDDEN; + fn->creation_time = ni->creation_time; + fn->last_data_change_time = ni->last_data_change_time; + fn->last_mft_change_time = ni->last_mft_change_time; + fn->last_access_time = ni->last_access_time; + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + fn->data_size = fn->allocated_size = const_cpu_to_sle64(0); + else { + fn->data_size = cpu_to_sle64(ni->data_size); + fn->allocated_size = cpu_to_sle64(ni->allocated_size); + } + memcpy(fn->file_name, name, name_len * sizeof(ntfschar)); + /* Add FILE_NAME attribute to inode. */ + if (ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8*)fn, fn_len)) { + err = errno; + ntfs_log_error("Failed to add FILE_NAME attribute.\n"); + goto err_out; + } + /* Add FILE_NAME attribute to index. */ + if (ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no, + le16_to_cpu(ni->mrec->sequence_number)))) { + err = errno; + ntfs_log_perror("Failed to add entry to the index"); + goto err_out; + } + rollback_dir = 1; + /* Set hard links count and directory flag. */ + ni->mrec->link_count = const_cpu_to_le16(1); + if (S_ISDIR(type)) + ni->mrec->flags |= MFT_RECORD_IS_DIRECTORY; + /* Add reparse data */ + if (special_files == NTFS_FILES_WSL) { + switch (type) { + case S_IFLNK : + err = ntfs_reparse_set_wsl_symlink(ni, target, + target_len); + break; + case S_IFIFO : + case S_IFSOCK : + case S_IFCHR : + case S_IFBLK : + err = ntfs_reparse_set_wsl_not_symlink(ni, + type); + if (!err) { + err = ntfs_ea_set_wsl_not_symlink(ni, + type, dev); + if (err) + ntfs_remove_ntfs_reparse_data(ni); + } + break; + default : + err = 0; + break; + } + if (err) { + err = errno; + goto err_out; + } + } + ntfs_inode_mark_dirty(ni); + /* Done! */ + free(fn); + free(si); + ntfs_log_trace("Done.\n"); + return ni; +err_out: + ntfs_log_trace("Failed.\n"); + + if (rollback_dir) + ntfs_index_remove(dir_ni, ni, fn, fn_len); + + if (rollback_sd) + ntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0); + + if (rollback_data) + ntfs_attr_remove(ni, AT_DATA, AT_UNNAMED, 0); + /* + * Free extent MFT records (should not exist any with current + * ntfs_create implementation, but for any case if something will be + * changed in the future). + */ + while (ni->nr_extents) + if (ntfs_mft_record_free(ni->vol, *(ni->extent_nis))) { + err = errno; + ntfs_log_error("Failed to free extent MFT record. " + "Leaving inconsistent metadata.\n"); + } + if (ntfs_mft_record_free(ni->vol, ni)) + ntfs_log_error("Failed to free MFT record. " + "Leaving inconsistent metadata. Run chkdsk.\n"); + free(fn); + free(si); + errno = err; + return NULL; +} + +/** + * Some wrappers around __ntfs_create() ... + */ + +ntfs_inode *ntfs_create(ntfs_inode *dir_ni, le32 securid, const ntfschar *name, + u8 name_len, mode_t type) +{ + if (type != S_IFREG && type != S_IFDIR && type != S_IFIFO && + type != S_IFSOCK) { + ntfs_log_error("Invalid arguments.\n"); + return NULL; + } + return __ntfs_create(dir_ni, securid, name, name_len, type, 0, NULL, 0); +} + +ntfs_inode *ntfs_create_device(ntfs_inode *dir_ni, le32 securid, + const ntfschar *name, u8 name_len, mode_t type, dev_t dev) +{ + if (type != S_IFCHR && type != S_IFBLK) { + ntfs_log_error("Invalid arguments.\n"); + return NULL; + } + return __ntfs_create(dir_ni, securid, name, name_len, type, dev, NULL, 0); +} + +ntfs_inode *ntfs_create_symlink(ntfs_inode *dir_ni, le32 securid, + const ntfschar *name, u8 name_len, const ntfschar *target, + int target_len) +{ + if (!target || !target_len) { + ntfs_log_error("%s: Invalid argument (%p, %d)\n", __FUNCTION__, + target, target_len); + return NULL; + } + return __ntfs_create(dir_ni, securid, name, name_len, S_IFLNK, 0, + target, target_len); +} + +int ntfs_check_empty_dir(ntfs_inode *ni) +{ + ntfs_attr *na; + int ret = 0; + + if (!(ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)) + return 0; + + na = ntfs_attr_open(ni, AT_INDEX_ROOT, NTFS_INDEX_I30, 4); + if (!na) { + errno = EIO; + ntfs_log_perror("Failed to open directory"); + return -1; + } + + /* Non-empty directory? */ + if ((na->data_size != sizeof(INDEX_ROOT) + sizeof(INDEX_ENTRY_HEADER))){ + /* Both ENOTEMPTY and EEXIST are ok. We use the more common. */ + errno = ENOTEMPTY; + ntfs_log_debug("Directory is not empty\n"); + ret = -1; + } + + ntfs_attr_close(na); + return ret; +} + +static int ntfs_check_unlinkable_dir(ntfs_inode *ni, FILE_NAME_ATTR *fn) +{ + int link_count = le16_to_cpu(ni->mrec->link_count); + int ret; + + ret = ntfs_check_empty_dir(ni); + if (!ret || errno != ENOTEMPTY) + return ret; + /* + * Directory is non-empty, so we can unlink only if there is more than + * one "real" hard link, i.e. links aren't different DOS and WIN32 names + */ + if ((link_count == 1) || + (link_count == 2 && fn->file_name_type == FILE_NAME_DOS)) { + errno = ENOTEMPTY; + ntfs_log_debug("Non-empty directory without hard links\n"); + goto no_hardlink; + } + + ret = 0; +no_hardlink: + return ret; +} + +/** + * ntfs_delete - delete file or directory from ntfs volume + * @ni: ntfs inode for object to delte + * @dir_ni: ntfs inode for directory in which delete object + * @name: unicode name of the object to delete + * @name_len: length of the name in unicode characters + * + * @ni is always closed after the call to this function (even if it failed), + * user does not need to call ntfs_inode_close himself. + * + * Return 0 on success or -1 on error with errno set to the error code. + */ +int ntfs_delete(ntfs_volume *vol, const char *pathname, + ntfs_inode *ni, ntfs_inode *dir_ni, const ntfschar *name, + u8 name_len) +{ + ntfs_attr_search_ctx *actx = NULL; + FILE_NAME_ATTR *fn = NULL; + BOOL looking_for_dos_name = FALSE, looking_for_win32_name = FALSE; + BOOL case_sensitive_match = TRUE; + int err = 0; +#if CACHE_NIDATA_SIZE + int i; +#endif +#if CACHE_INODE_SIZE + struct CACHED_INODE item; + const char *p; + u64 inum = (u64)-1; + int count; +#endif +#if CACHE_LOOKUP_SIZE + struct CACHED_LOOKUP lkitem; +#endif + + ntfs_log_trace("Entering.\n"); + + if (!ni || !dir_ni || !name || !name_len) { + ntfs_log_error("Invalid arguments.\n"); + errno = EINVAL; + goto err_out; + } + if (ni->nr_extents == -1) + ni = ni->base_ni; + if (dir_ni->nr_extents == -1) + dir_ni = dir_ni->base_ni; + /* + * Search for FILE_NAME attribute with such name. If it's in POSIX or + * WIN32_AND_DOS namespace, then simply remove it from index and inode. + * If filename in DOS or in WIN32 namespace, then remove DOS name first, + * only then remove WIN32 name. + */ + actx = ntfs_attr_get_search_ctx(ni, NULL); + if (!actx) + goto err_out; +search: + while (!(err = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, + CASE_SENSITIVE, 0, NULL, 0, actx))) { + #ifdef DEBUG + char *s; + #endif + IGNORE_CASE_BOOL case_sensitive = IGNORE_CASE; + + fn = (FILE_NAME_ATTR*)((u8*)actx->attr + + le16_to_cpu(actx->attr->value_offset)); + #ifdef DEBUG + s = ntfs_attr_name_get(fn->file_name, fn->file_name_length); + ntfs_log_trace("name: '%s' type: %d dos: %d win32: %d " + "case: %d\n", s, fn->file_name_type, + looking_for_dos_name, looking_for_win32_name, + case_sensitive_match); + ntfs_attr_name_free(&s); + #endif + if (looking_for_dos_name) { + if (fn->file_name_type == FILE_NAME_DOS) + break; + else + continue; + } + if (looking_for_win32_name) { + if (fn->file_name_type == FILE_NAME_WIN32) + break; + else + continue; + } + + /* Ignore hard links from other directories */ + if (dir_ni->mft_no != MREF_LE(fn->parent_directory)) { + ntfs_log_debug("MFT record numbers don't match " + "(%llu != %llu)\n", + (long long unsigned)dir_ni->mft_no, + (long long unsigned)MREF_LE(fn->parent_directory)); + continue; + } + if (case_sensitive_match + || ((fn->file_name_type == FILE_NAME_POSIX) + && NVolCaseSensitive(ni->vol))) + case_sensitive = CASE_SENSITIVE; + + if (ntfs_names_are_equal(fn->file_name, fn->file_name_length, + name, name_len, case_sensitive, + ni->vol->upcase, ni->vol->upcase_len)){ + + if (fn->file_name_type == FILE_NAME_WIN32) { + looking_for_dos_name = TRUE; + ntfs_attr_reinit_search_ctx(actx); + continue; + } + if (fn->file_name_type == FILE_NAME_DOS) + looking_for_dos_name = TRUE; + break; + } + } + if (err) { + /* + * If case sensitive search failed, then try once again + * ignoring case. + */ + if (errno == ENOENT && case_sensitive_match) { + case_sensitive_match = FALSE; + ntfs_attr_reinit_search_ctx(actx); + goto search; + } + goto err_out; + } + + if (ntfs_check_unlinkable_dir(ni, fn) < 0) + goto err_out; + + if (ntfs_index_remove(dir_ni, ni, fn, le32_to_cpu(actx->attr->value_length))) + goto err_out; + + /* + * Keep the last name in place, this is useful for undeletion + * (Windows also does so), however delete the name if it were + * in an extent, to avoid leaving an attribute list. + */ + if ((ni->mrec->link_count == const_cpu_to_le16(1)) && !actx->base_ntfs_ino) { + /* make sure to not loop to another search */ + looking_for_dos_name = FALSE; + } else { + if (ntfs_attr_record_rm(actx)) + goto err_out; + } + + ni->mrec->link_count = cpu_to_le16(le16_to_cpu( + ni->mrec->link_count) - 1); + + ntfs_inode_mark_dirty(ni); + if (looking_for_dos_name) { + looking_for_dos_name = FALSE; + looking_for_win32_name = TRUE; + ntfs_attr_reinit_search_ctx(actx); + goto search; + } + /* TODO: Update object id, quota and securiry indexes if required. */ + /* + * If hard link count is not equal to zero then we are done. In other + * case there are no reference to this inode left, so we should free all + * non-resident attributes and mark all MFT record as not in use. + */ +#if CACHE_LOOKUP_SIZE + /* invalidate entry in lookup cache */ + lkitem.name = (const char*)NULL; + lkitem.namesize = 0; + lkitem.inum = ni->mft_no; + lkitem.parent = dir_ni->mft_no; + ntfs_invalidate_cache(vol->lookup_cache, GENERIC(&lkitem), + lookup_cache_inv_compare, CACHE_NOHASH); +#endif +#if CACHE_INODE_SIZE + inum = ni->mft_no; + if (pathname) { + /* invalide cache entry, even if there was an error */ + /* Remove leading /'s. */ + p = pathname; + while (*p == PATH_SEP) + p++; + if (p[0] && (p[strlen(p)-1] == PATH_SEP)) + ntfs_log_error("Unnormalized path %s\n",pathname); + item.pathname = p; + item.varsize = strlen(p); + } else { + item.pathname = (const char*)NULL; + item.varsize = 0; + } + item.inum = inum; + count = ntfs_invalidate_cache(vol->xinode_cache, GENERIC(&item), + inode_cache_inv_compare, CACHE_NOHASH); + if (pathname && !count) + ntfs_log_error("Could not delete inode cache entry for %s\n", + pathname); +#endif + if (ni->mrec->link_count) { + ntfs_inode_update_times(ni, NTFS_UPDATE_CTIME); + goto ok; + } + if (ntfs_delete_reparse_index(ni)) { + /* + * Failed to remove the reparse index : proceed anyway + * This is not a critical error, the entry is useless + * because of sequence_number, and stopping file deletion + * would be much worse as the file is not referenced now. + */ + err = errno; + } + if (ntfs_delete_object_id_index(ni)) { + /* + * Failed to remove the object id index : proceed anyway + * This is not a critical error. + */ + err = errno; + } + ntfs_attr_reinit_search_ctx(actx); + while (!ntfs_attrs_walk(actx)) { + if (actx->attr->non_resident) { + runlist *rl; + + rl = ntfs_mapping_pairs_decompress(ni->vol, actx->attr, + NULL); + if (!rl) { + err = errno; + ntfs_log_error("Failed to decompress runlist. " + "Leaving inconsistent metadata.\n"); + continue; + } + if (ntfs_cluster_free_from_rl(ni->vol, rl)) { + err = errno; + ntfs_log_error("Failed to free clusters. " + "Leaving inconsistent metadata.\n"); + continue; + } + free(rl); + } + } + if (errno != ENOENT) { + err = errno; + ntfs_log_error("Attribute enumeration failed. " + "Probably leaving inconsistent metadata.\n"); + } + /* All extents should be attached after attribute walk. */ +#if CACHE_NIDATA_SIZE + /* + * Disconnect extents before deleting them, so they are + * not wrongly moved to cache through the chainings + */ + for (i=ni->nr_extents-1; i>=0; i--) { + ni->extent_nis[i]->base_ni = (ntfs_inode*)NULL; + ni->extent_nis[i]->nr_extents = 0; + if (ntfs_mft_record_free(ni->vol, ni->extent_nis[i])) { + err = errno; + ntfs_log_error("Failed to free extent MFT record. " + "Leaving inconsistent metadata.\n"); + } + } + free(ni->extent_nis); + ni->nr_extents = 0; + ni->extent_nis = (ntfs_inode**)NULL; +#else + while (ni->nr_extents) + if (ntfs_mft_record_free(ni->vol, *(ni->extent_nis))) { + err = errno; + ntfs_log_error("Failed to free extent MFT record. " + "Leaving inconsistent metadata.\n"); + } +#endif + debug_double_inode(ni->mft_no,0); + if (ntfs_mft_record_free(ni->vol, ni)) { + err = errno; + ntfs_log_error("Failed to free base MFT record. " + "Leaving inconsistent metadata.\n"); + } + ni = NULL; +ok: + ntfs_inode_update_times(dir_ni, NTFS_UPDATE_MCTIME); +out: + if (actx) + ntfs_attr_put_search_ctx(actx); + if (ntfs_inode_close(dir_ni) && !err) + err = errno; + if (ntfs_inode_close(ni) && !err) + err = errno; + if (err) { + errno = err; + ntfs_log_debug("Could not delete file: %s\n", strerror(errno)); + return -1; + } + ntfs_log_trace("Done.\n"); + return 0; +err_out: + err = errno; + goto out; +} + +/** + * ntfs_link - create hard link for file or directory + * @ni: ntfs inode for object to create hard link + * @dir_ni: ntfs inode for directory in which new link should be placed + * @name: unicode name of the new link + * @name_len: length of the name in unicode characters + * + * NOTE: At present we allow creating hardlinks to directories, we use them + * in a temporary state during rename. But it's defenitely bad idea to have + * hard links to directories as a result of operation. + * FIXME: Create internal __ntfs_link that allows hard links to a directories + * and external ntfs_link that do not. Write ntfs_rename that uses __ntfs_link. + * + * Return 0 on success or -1 on error with errno set to the error code. + */ +static int ntfs_link_i(ntfs_inode *ni, ntfs_inode *dir_ni, const ntfschar *name, + u8 name_len, FILE_NAME_TYPE_FLAGS nametype) +{ + FILE_NAME_ATTR *fn = NULL; + int fn_len, err; + + ntfs_log_trace("Entering.\n"); + + if (!ni || !dir_ni || !name || !name_len || + ni->mft_no == dir_ni->mft_no) { + err = EINVAL; + ntfs_log_perror("ntfs_link wrong arguments"); + goto err_out; + } + + if (NVolHideDotFiles(dir_ni->vol)) { + /* Set hidden flag according to the latest name */ + if ((name_len > 1) + && (name[0] == const_cpu_to_le16('.')) + && (name[1] != const_cpu_to_le16('.'))) + ni->flags |= FILE_ATTR_HIDDEN; + else + ni->flags &= ~FILE_ATTR_HIDDEN; + } + + /* Create FILE_NAME attribute. */ + fn_len = offsetof(FILE_NAME_ATTR, file_name) + name_len * sizeof(ntfschar); + fn = ntfs_calloc(fn_len); + if (!fn) { + err = errno; + goto err_out; + } + fn->parent_directory = MK_LE_MREF(dir_ni->mft_no, + le16_to_cpu(dir_ni->mrec->sequence_number)); + fn->file_name_length = name_len; + fn->file_name_type = nametype; + fn->file_attributes = ni->flags; + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + fn->file_attributes |= FILE_ATTR_I30_INDEX_PRESENT; + fn->data_size = fn->allocated_size = const_cpu_to_sle64(0); + } else { + fn->allocated_size = cpu_to_sle64(ni->allocated_size); + fn->data_size = cpu_to_sle64(ni->data_size); + } + fn->creation_time = ni->creation_time; + fn->last_data_change_time = ni->last_data_change_time; + fn->last_mft_change_time = ni->last_mft_change_time; + fn->last_access_time = ni->last_access_time; + memcpy(fn->file_name, name, name_len * sizeof(ntfschar)); + /* Add FILE_NAME attribute to index. */ + if (ntfs_index_add_filename(dir_ni, fn, MK_MREF(ni->mft_no, + le16_to_cpu(ni->mrec->sequence_number)))) { + err = errno; + ntfs_log_perror("Failed to add filename to the index"); + goto err_out; + } + /* Add FILE_NAME attribute to inode. */ + if (ntfs_attr_add(ni, AT_FILE_NAME, AT_UNNAMED, 0, (u8*)fn, fn_len)) { + ntfs_log_error("Failed to add FILE_NAME attribute.\n"); + err = errno; + /* Try to remove just added attribute from index. */ + if (ntfs_index_remove(dir_ni, ni, fn, fn_len)) + goto rollback_failed; + goto err_out; + } + /* Increment hard links count. */ + ni->mrec->link_count = cpu_to_le16(le16_to_cpu( + ni->mrec->link_count) + 1); + /* Done! */ + ntfs_inode_mark_dirty(ni); + free(fn); + ntfs_log_trace("Done.\n"); + return 0; +rollback_failed: + ntfs_log_error("Rollback failed. Leaving inconsistent metadata.\n"); +err_out: + free(fn); + errno = err; + return -1; +} + +int ntfs_link(ntfs_inode *ni, ntfs_inode *dir_ni, const ntfschar *name, + u8 name_len) +{ + return (ntfs_link_i(ni, dir_ni, name, name_len, FILE_NAME_POSIX)); +} + +/* + * Get a parent directory from an inode entry + * + * This is only used in situations where the path used to access + * the current file is not known for sure. The result may be different + * from the path when the file is linked in several parent directories. + * + * Currently this is only used for translating ".." in the target + * of a Vista relative symbolic link + */ + +ntfs_inode *ntfs_dir_parent_inode(ntfs_inode *ni) +{ + ntfs_inode *dir_ni = (ntfs_inode*)NULL; + u64 inum; + FILE_NAME_ATTR *fn; + ntfs_attr_search_ctx *ctx; + + if (ni->mft_no != FILE_root) { + /* find the name in the attributes */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return ((ntfs_inode*)NULL); + + if (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + /* We know this will always be resident. */ + fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + inum = le64_to_cpu(fn->parent_directory); + if (inum != (u64)-1) { + dir_ni = ntfs_inode_open(ni->vol, MREF(inum)); + } + } + ntfs_attr_put_search_ctx(ctx); + } + return (dir_ni); +} + +#define MAX_DOS_NAME_LENGTH 12 + +/* + * Get a DOS name for a file in designated directory + * + * Not allowed if there are several non-dos names (EMLINK) + * + * Returns size if found + * 0 if not found + * -1 if there was an error (described by errno) + */ + +static int get_dos_name(ntfs_inode *ni, u64 dnum, ntfschar *dosname) +{ + size_t outsize = 0; + int namecount = 0; + FILE_NAME_ATTR *fn; + ntfs_attr_search_ctx *ctx; + + /* find the name in the attributes */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return -1; + + while (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + /* We know this will always be resident. */ + fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + + if (fn->file_name_type != FILE_NAME_DOS) + namecount++; + if ((fn->file_name_type & FILE_NAME_DOS) + && (MREF_LE(fn->parent_directory) == dnum)) { + /* + * Found a DOS or WIN32+DOS name for the entry + * copy name, after truncation for safety + */ + outsize = fn->file_name_length; +/* TODO : reject if name is too long ? */ + if (outsize > MAX_DOS_NAME_LENGTH) + outsize = MAX_DOS_NAME_LENGTH; + memcpy(dosname,fn->file_name,outsize*sizeof(ntfschar)); + } + } + ntfs_attr_put_search_ctx(ctx); + if ((outsize > 0) && (namecount > 1)) { + outsize = -1; + errno = EMLINK; /* this error implies there is a dos name */ + } + return (outsize); +} + + +/* + * Get a long name for a file in designated directory + * + * Not allowed if there are several non-dos names (EMLINK) + * + * Returns size if found + * 0 if not found + * -1 if there was an error (described by errno) + */ + +static int get_long_name(ntfs_inode *ni, u64 dnum, ntfschar *longname) +{ + size_t outsize = 0; + int namecount = 0; + FILE_NAME_ATTR *fn; + ntfs_attr_search_ctx *ctx; + + /* find the name in the attributes */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return -1; + + /* first search for WIN32 or DOS+WIN32 names */ + while (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + /* We know this will always be resident. */ + fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + + if (fn->file_name_type != FILE_NAME_DOS) + namecount++; + if ((fn->file_name_type & FILE_NAME_WIN32) + && (MREF_LE(fn->parent_directory) == dnum)) { + /* + * Found a WIN32 or WIN32+DOS name for the entry + * copy name + */ + outsize = fn->file_name_length; + memcpy(longname,fn->file_name,outsize*sizeof(ntfschar)); + } + } + if (namecount > 1) { + ntfs_attr_put_search_ctx(ctx); + errno = EMLINK; + return -1; + } + /* if not found search for POSIX names */ + if (!outsize) { + ntfs_attr_reinit_search_ctx(ctx); + while (!ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + /* We know this will always be resident. */ + fn = (FILE_NAME_ATTR*)((u8*)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + + if ((fn->file_name_type == FILE_NAME_POSIX) + && (MREF_LE(fn->parent_directory) == dnum)) { + /* + * Found a POSIX name for the entry + * copy name + */ + outsize = fn->file_name_length; + memcpy(longname,fn->file_name,outsize*sizeof(ntfschar)); + } + } + } + ntfs_attr_put_search_ctx(ctx); + return (outsize); +} + + +/* + * Get the ntfs DOS name into an extended attribute + */ + +int ntfs_get_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni, + char *value, size_t size) +{ + int outsize = 0; + char *outname = (char*)NULL; + u64 dnum; + int doslen; + ntfschar dosname[MAX_DOS_NAME_LENGTH]; + + dnum = dir_ni->mft_no; + doslen = get_dos_name(ni, dnum, dosname); + if (doslen > 0) { + /* + * Found a DOS name for the entry, make + * uppercase and encode into the buffer + * if there is enough space + */ + ntfs_name_upcase(dosname, doslen, + ni->vol->upcase, ni->vol->upcase_len); + outsize = ntfs_ucstombs(dosname, doslen, &outname, 0); + if (outsize < 0) { + ntfs_log_error("Cannot represent dosname in current locale.\n"); + outsize = -errno; + } else { + if (value && (outsize <= (int)size)) + memcpy(value, outname, outsize); + else + if (size && (outsize > (int)size)) + outsize = -ERANGE; + free(outname); + } + } else { + if (doslen == 0) + errno = ENODATA; + outsize = -errno; + } + return (outsize); +} + +/* + * Change the name space of an existing file or directory + * + * Returns the old namespace if successful + * -1 if an error occurred (described by errno) + */ + +static int set_namespace(ntfs_inode *ni, ntfs_inode *dir_ni, + const ntfschar *name, int len, + FILE_NAME_TYPE_FLAGS nametype) +{ + ntfs_attr_search_ctx *actx; + ntfs_index_context *icx; + FILE_NAME_ATTR *fnx; + FILE_NAME_ATTR *fn = NULL; + BOOL found; + int lkup; + int ret; + + ret = -1; + actx = ntfs_attr_get_search_ctx(ni, NULL); + if (actx) { + found = FALSE; + do { + lkup = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, + CASE_SENSITIVE, 0, NULL, 0, actx); + if (!lkup) { + fn = (FILE_NAME_ATTR*)((u8*)actx->attr + + le16_to_cpu(actx->attr->value_offset)); + found = (MREF_LE(fn->parent_directory) + == dir_ni->mft_no) + && !memcmp(fn->file_name, name, + len*sizeof(ntfschar)); + } + } while (!lkup && !found); + if (found) { + icx = ntfs_index_ctx_get(dir_ni, NTFS_INDEX_I30, 4); + if (icx) { + lkup = ntfs_index_lookup((char*)fn, len, icx); + if (!lkup && icx->data && icx->data_len) { + fnx = (FILE_NAME_ATTR*)icx->data; + ret = fn->file_name_type; + fn->file_name_type = nametype; + fnx->file_name_type = nametype; + ntfs_inode_mark_dirty(ni); + ntfs_index_entry_mark_dirty(icx); + } + ntfs_index_ctx_put(icx); + } + } + ntfs_attr_put_search_ctx(actx); + } + return (ret); +} + +/* + * Set a DOS name to a file and adjust name spaces + * + * If the new names are collapsible (same uppercased chars) : + * + * - the existing DOS name or DOS+Win32 name is made Posix + * - if it was a real DOS name, the existing long name is made DOS+Win32 + * and the existing DOS name is deleted + * - finally the existing long name is made DOS+Win32 unless already done + * + * If the new names are not collapsible : + * + * - insert the short name as a DOS name + * - delete the old long name or existing short name + * - insert the new long name (as a Win32 or DOS+Win32 name) + * + * Deleting the old long name will not delete the file + * provided the old name was in the Posix name space, + * because the alternate name has been set before. + * + * The inodes of file and parent directory are always closed + * + * Returns 0 if successful + * -1 if failed + */ + +static int set_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni, + const ntfschar *shortname, int shortlen, + const ntfschar *longname, int longlen, + const ntfschar *deletename, int deletelen, BOOL existed) +{ + unsigned int linkcount; + ntfs_volume *vol; + BOOL collapsible; + BOOL deleted; + BOOL done; + FILE_NAME_TYPE_FLAGS oldnametype; + u64 dnum; + u64 fnum; + int res; + + res = -1; + vol = ni->vol; + dnum = dir_ni->mft_no; + fnum = ni->mft_no; + /* save initial link count */ + linkcount = le16_to_cpu(ni->mrec->link_count); + + /* check whether the same name may be used as DOS and WIN32 */ + collapsible = ntfs_collapsible_chars(ni->vol, shortname, shortlen, + longname, longlen); + if (collapsible) { + deleted = FALSE; + done = FALSE; + if (existed) { + oldnametype = set_namespace(ni, dir_ni, deletename, + deletelen, FILE_NAME_POSIX); + if (oldnametype == FILE_NAME_DOS) { + if (set_namespace(ni, dir_ni, longname, longlen, + FILE_NAME_WIN32_AND_DOS) >= 0) { + if (!ntfs_delete(vol, + (const char*)NULL, ni, dir_ni, + deletename, deletelen)) + res = 0; + deleted = TRUE; + } else + done = TRUE; + } + } + if (!deleted) { + if (!done && (set_namespace(ni, dir_ni, + longname, longlen, + FILE_NAME_WIN32_AND_DOS) >= 0)) + res = 0; + ntfs_inode_update_times(ni, NTFS_UPDATE_CTIME); + ntfs_inode_update_times(dir_ni, NTFS_UPDATE_MCTIME); + if (ntfs_inode_close_in_dir(ni,dir_ni) && !res) + res = -1; + if (ntfs_inode_close(dir_ni) && !res) + res = -1; + } + } else { + if (!ntfs_link_i(ni, dir_ni, shortname, shortlen, + FILE_NAME_DOS) + /* make sure a new link was recorded */ + && (le16_to_cpu(ni->mrec->link_count) > linkcount)) { + /* delete the existing long name or short name */ +// is it ok to not provide the path ? + if (!ntfs_delete(vol, (char*)NULL, ni, dir_ni, + deletename, deletelen)) { + /* delete closes the inodes, so have to open again */ + dir_ni = ntfs_inode_open(vol, dnum); + if (dir_ni) { + ni = ntfs_inode_open(vol, fnum); + if (ni) { + if (!ntfs_link_i(ni, dir_ni, + longname, longlen, + FILE_NAME_WIN32)) + res = 0; + if (ntfs_inode_close_in_dir(ni, + dir_ni) + && !res) + res = -1; + } + if (ntfs_inode_close(dir_ni) && !res) + res = -1; + } + } + } else { + ntfs_inode_close_in_dir(ni,dir_ni); + ntfs_inode_close(dir_ni); + } + } + return (res); +} + + +/* + * Set the ntfs DOS name into an extended attribute + * + * The DOS name will be added as another file name attribute + * using the existing file name information from the original + * name or overwriting the DOS Name if one exists. + * + * The inode of the file is always closed + */ + +int ntfs_set_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni, + const char *value, size_t size, int flags) +{ + int res = 0; + int longlen = 0; + int shortlen = 0; + char newname[3*MAX_DOS_NAME_LENGTH + 1]; + ntfschar oldname[MAX_DOS_NAME_LENGTH]; + int oldlen; + u64 dnum; + BOOL closed = FALSE; + ntfschar *shortname = NULL; + ntfschar longname[NTFS_MAX_NAME_LEN]; + + /* copy the string to insert a null char, and truncate */ + if (size > 3*MAX_DOS_NAME_LENGTH) + size = 3*MAX_DOS_NAME_LENGTH; + strncpy(newname, value, size); + /* a long name may be truncated badly and be untranslatable */ + newname[size] = 0; + /* convert the string to the NTFS wide chars, and truncate */ + shortlen = ntfs_mbstoucs(newname, &shortname); + if (shortlen > MAX_DOS_NAME_LENGTH) + shortlen = MAX_DOS_NAME_LENGTH; + + /* Make sure the short name has valid chars. + * Note: the short name cannot end with dot or space, but the + * corresponding long name can. */ + if ((shortlen < 0) + || ntfs_forbidden_names(ni->vol,shortname,shortlen,TRUE)) { + ntfs_inode_close_in_dir(ni,dir_ni); + ntfs_inode_close(dir_ni); + res = -errno; + return res; + } + dnum = dir_ni->mft_no; + longlen = get_long_name(ni, dnum, longname); + if (longlen > 0) { + oldlen = get_dos_name(ni, dnum, oldname); + if ((oldlen >= 0) + && !ntfs_forbidden_names(ni->vol, longname, longlen, + FALSE)) { + if (oldlen > 0) { + if (flags & XATTR_CREATE) { + res = -1; + errno = EEXIST; + } else + if ((shortlen == oldlen) + && !memcmp(shortname,oldname, + oldlen*sizeof(ntfschar))) + /* already set, done */ + res = 0; + else { + res = set_dos_name(ni, dir_ni, + shortname, shortlen, + longname, longlen, + oldname, oldlen, TRUE); + closed = TRUE; + } + } else { + if (flags & XATTR_REPLACE) { + res = -1; + errno = ENODATA; + } else { + res = set_dos_name(ni, dir_ni, + shortname, shortlen, + longname, longlen, + longname, longlen, FALSE); + closed = TRUE; + } + } + } else + res = -1; + } else { + res = -1; + if (!longlen) + errno = ENOENT; + } + free(shortname); + if (!closed) { + ntfs_inode_close_in_dir(ni,dir_ni); + ntfs_inode_close(dir_ni); + } + return (res ? -1 : 0); +} + +/* + * Delete the ntfs DOS name + */ + +int ntfs_remove_ntfs_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni) +{ + int res; + int oldnametype; + int longlen = 0; + int shortlen; + u64 dnum; + ntfs_volume *vol; + BOOL deleted = FALSE; + ntfschar shortname[MAX_DOS_NAME_LENGTH]; + ntfschar longname[NTFS_MAX_NAME_LEN]; + + res = -1; + vol = ni->vol; + dnum = dir_ni->mft_no; + longlen = get_long_name(ni, dnum, longname); + if (longlen > 0) { + shortlen = get_dos_name(ni, dnum, shortname); + if (shortlen >= 0) { + /* migrate the long name as Posix */ + oldnametype = set_namespace(ni,dir_ni,longname,longlen, + FILE_NAME_POSIX); + switch (oldnametype) { + case FILE_NAME_WIN32_AND_DOS : + /* name was Win32+DOS : done */ + res = 0; + break; + case FILE_NAME_DOS : + /* name was DOS, make it back to DOS */ + set_namespace(ni,dir_ni,longname,longlen, + FILE_NAME_DOS); + errno = ENOENT; + break; + case FILE_NAME_WIN32 : + /* name was Win32, make it Posix and delete */ + if (set_namespace(ni,dir_ni,shortname,shortlen, + FILE_NAME_POSIX) >= 0) { + if (!ntfs_delete(vol, + (const char*)NULL, ni, + dir_ni, shortname, + shortlen)) + res = 0; + deleted = TRUE; + } else { + /* + * DOS name has been found, but cannot + * migrate to Posix : something bad + * has happened + */ + errno = EIO; + ntfs_log_error("Could not change" + " DOS name of inode %lld to Posix\n", + (long long)ni->mft_no); + } + break; + default : + /* name was Posix or not found : error */ + errno = ENOENT; + break; + } + } + } else { + if (!longlen) + errno = ENOENT; + res = -1; + } + if (!deleted) { + ntfs_inode_close_in_dir(ni,dir_ni); + ntfs_inode_close(dir_ni); + } + return (res); +} + +/* + * Increment the count of subdirectories + * (excluding entries with a short name) + */ + +static int nlink_increment(void *nlink_ptr, + const ntfschar *name __attribute__((unused)), + const int len __attribute__((unused)), + const int type, + const s64 pos __attribute__((unused)), + const MFT_REF mref __attribute__((unused)), + const unsigned int dt_type) +{ + if ((dt_type == NTFS_DT_DIR) && (type != FILE_NAME_DOS)) + (*((int*)nlink_ptr))++; + return (0); +} + +/* + * Compute the number of hard links according to Posix + * For a directory count the subdirectories whose name is not + * a short one, but count "." and ".." + * Otherwise count the names, excluding the short ones. + * + * if there is an error, a null count is returned. + */ + +int ntfs_dir_link_cnt(ntfs_inode *ni) +{ + ntfs_attr_search_ctx *actx; + FILE_NAME_ATTR *fn; + s64 pos; + int err = 0; + int nlink = 0; + + if (!ni) { + ntfs_log_error("Invalid argument.\n"); + errno = EINVAL; + goto err_out; + } + if (ni->nr_extents == -1) + ni = ni->base_ni; + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + /* + * Directory : scan the directory and count + * subdirectories whose name is not DOS-only. + * The directory names are ignored, but "." and ".." + * are taken into account. + */ + pos = 0; + err = ntfs_readdir(ni, &pos, &nlink, nlink_increment); + if (err) + nlink = 0; + } else { + /* + * Non-directory : search for FILE_NAME attributes, + * and count those which are not DOS-only ones. + */ + actx = ntfs_attr_get_search_ctx(ni, NULL); + if (!actx) + goto err_out; + while (!(err = ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, + CASE_SENSITIVE, 0, NULL, 0, actx))) { + fn = (FILE_NAME_ATTR*)((u8*)actx->attr + + le16_to_cpu(actx->attr->value_offset)); + if (fn->file_name_type != FILE_NAME_DOS) + nlink++; + } + if (err && (errno != ENOENT)) + nlink = 0; + ntfs_attr_put_search_ctx(actx); + } + if (!nlink) + ntfs_log_perror("Failed to compute nlink of inode %lld", + (long long)ni->mft_no); +err_out : + return (nlink); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/ea.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/ea.c new file mode 100644 index 0000000..b86930a --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/ea.c @@ -0,0 +1,519 @@ +/** + * ea.c - Processing of EA's + * + * This module is part of ntfs-3g library + * + * Copyright (c) 2014-2021 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef MAJOR_IN_MKDEV +#include +#endif +#ifdef MAJOR_IN_SYSMACROS +#include +#endif + +#include "types.h" +#include "param.h" +#include "layout.h" +#include "attrib.h" +#include "index.h" +#include "dir.h" +#include "ea.h" +#include "misc.h" +#include "logging.h" +#include "xattrs.h" + +static const char lxdev[] = "$LXDEV"; +static const char lxmod[] = "$LXMOD"; + + +/* + * Create a needed attribute (EA or EA_INFORMATION) + * + * Returns 0 if successful, + * -1 otherwise, with errno indicating why it failed. + */ + +static int ntfs_need_ea(ntfs_inode *ni, ATTR_TYPES type, int size, int flags) +{ + u8 dummy; + int res; + + res = 0; + if (!ntfs_attr_exist(ni,type, AT_UNNAMED,0)) { + if (!(flags & XATTR_REPLACE)) { + /* + * no needed attribute : add one, + * apparently, this does not feed the new value in + * Note : NTFS version must be >= 3 + */ + if (ni->vol->major_ver >= 3) { + res = ntfs_attr_add(ni, type, + AT_UNNAMED,0,&dummy,(s64)size); + if (!res) { + NInoFileNameSetDirty(ni); + } + NInoSetDirty(ni); + } else { + errno = EOPNOTSUPP; + res = -1; + } + } else { + errno = ENODATA; + res = -1; + } + } + return (res); +} + +/* + * Restore the old EA_INFORMATION or delete the current one, + * when EA cannot be updated. + * + * As this is used in the context of some other error, the caller + * is responsible for returning the proper error, and errno is + * left unchanged. + * Only double errors are logged here. + */ + +static void restore_ea_info(ntfs_attr *nai, const EA_INFORMATION *old_ea_info) +{ + s64 written; + int olderrno; + + olderrno = errno; + if (old_ea_info) { + written = ntfs_attr_pwrite(nai, 0, sizeof(EA_INFORMATION), + old_ea_info); + if ((size_t)written != sizeof(EA_INFORMATION)) { + ntfs_log_error("Could not restore the EA_INFORMATION," + " possible inconsistency in inode %lld\n", + (long long)nai->ni->mft_no); + } + } else { + if (ntfs_attr_rm(nai)) { + ntfs_log_error("Could not delete the EA_INFORMATION," + " possible inconsistency in inode %lld\n", + (long long)nai->ni->mft_no); + } + } + errno = olderrno; +} + +/* + * Update both EA and EA_INFORMATION + */ + +static int ntfs_update_ea(ntfs_inode *ni, const char *value, size_t size, + const EA_INFORMATION *ea_info, + const EA_INFORMATION *old_ea_info) +{ + ntfs_attr *na; + ntfs_attr *nai; + int res; + + res = 0; + nai = ntfs_attr_open(ni, AT_EA_INFORMATION, AT_UNNAMED, 0); + if (nai) { + na = ntfs_attr_open(ni, AT_EA, AT_UNNAMED, 0); + if (na) { + /* + * Set EA_INFORMATION first, it is easier to + * restore the old value, if setting EA fails. + */ + if (ntfs_attr_pwrite(nai, 0, sizeof(EA_INFORMATION), + ea_info) + != (s64)sizeof(EA_INFORMATION)) { + res = -errno; + } else { + if (((na->data_size > (s64)size) + && ntfs_attr_truncate(na, size)) + || (ntfs_attr_pwrite(na, 0, size, value) + != (s64)size)) { + res = -errno; + if (old_ea_info) + restore_ea_info(nai, + old_ea_info); + } + } + ntfs_attr_close(na); + } + ntfs_attr_close(nai); + } else { + res = -errno; + } + return (res); +} + +/* + * Return the existing EA + * + * The EA_INFORMATION is not examined and the consistency of the + * existing EA is not checked. + * + * If successful, the full attribute is returned unchanged + * and its size is returned. + * If the designated buffer is too small, the needed size is + * returned, and the buffer is left unchanged. + * If there is an error, a negative value is returned and errno + * is set according to the error. + */ + +int ntfs_get_ntfs_ea(ntfs_inode *ni, char *value, size_t size) +{ + s64 ea_size; + void *ea_buf; + int res = 0; + + if (ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) { + ea_buf = ntfs_attr_readall(ni, AT_EA, (ntfschar*)NULL, 0, + &ea_size); + if (ea_buf) { + if (value && (ea_size <= (s64)size)) + memcpy(value, ea_buf, ea_size); + free(ea_buf); + res = ea_size; + } else { + ntfs_log_error("Failed to read EA from inode %lld\n", + (long long)ni->mft_no); + errno = ENODATA; + res = -errno; + } + } else { + errno = ENODATA; + res = -errno; + } + return (res); +} + +/* + * Set a new EA, and set EA_INFORMATION accordingly + * + * This is roughly the same as ZwSetEaFile() on Windows, however + * the "offset to next" of the last EA should not be cleared. + * + * Consistency of the new EA is first checked. + * + * EA_INFORMATION is set first, and it is restored to its former + * state if setting EA fails. + * + * Returns 0 if successful + * a negative value if an error occurred. + */ + +int ntfs_set_ntfs_ea(ntfs_inode *ni, const char *value, size_t size, int flags) +{ + EA_INFORMATION ea_info; + EA_INFORMATION *old_ea_info; + s64 old_ea_size; + int res; + size_t offs; + size_t nextoffs; + BOOL ok; + int ea_count; + int ea_packed; + const EA_ATTR *p_ea; + + res = -1; + if (value && (size > 0)) { + /* do consistency checks */ + offs = 0; + ok = TRUE; + ea_count = 0; + ea_packed = 0; + nextoffs = 0; + while (ok && (offs < size)) { + p_ea = (const EA_ATTR*)&value[offs]; + nextoffs = offs + le32_to_cpu(p_ea->next_entry_offset); + /* null offset to next not allowed */ + ok = (nextoffs > offs) + && (nextoffs <= size) + && !(nextoffs & 3) + && p_ea->name_length + /* zero sized value are allowed */ + && ((offs + offsetof(EA_ATTR,name) + + p_ea->name_length + 1 + + le16_to_cpu(p_ea->value_length)) + <= nextoffs) + && ((offs + offsetof(EA_ATTR,name) + + p_ea->name_length + 1 + + le16_to_cpu(p_ea->value_length)) + >= (nextoffs - 3)) + && !p_ea->name[p_ea->name_length]; + /* name not checked, as chkdsk accepts any chars */ + if (ok) { + if (p_ea->flags & NEED_EA) + ea_count++; + /* + * Assume ea_packed includes : + * 4 bytes for header (flags and lengths) + * + name length + 1 + * + value length + */ + ea_packed += 5 + p_ea->name_length + + le16_to_cpu(p_ea->value_length); + offs = nextoffs; + } + } + /* + * EA and REPARSE_POINT compatibility not checked any more, + * required by Windows 10, but having both may lead to + * problems with earlier versions. + */ + if (ok) { + ea_info.ea_length = cpu_to_le16(ea_packed); + ea_info.need_ea_count = cpu_to_le16(ea_count); + ea_info.ea_query_length = cpu_to_le32(nextoffs); + + old_ea_size = 0; + old_ea_info = NULL; + /* Try to save the old EA_INFORMATION */ + if (ntfs_attr_exist(ni, AT_EA_INFORMATION, + AT_UNNAMED, 0)) { + old_ea_info = ntfs_attr_readall(ni, + AT_EA_INFORMATION, + (ntfschar*)NULL, 0, &old_ea_size); + } + /* + * no EA or EA_INFORMATION : add them + */ + if (!ntfs_need_ea(ni, AT_EA_INFORMATION, + sizeof(EA_INFORMATION), flags) + && !ntfs_need_ea(ni, AT_EA, 0, flags)) { + res = ntfs_update_ea(ni, value, size, + &ea_info, old_ea_info); + } else { + res = -errno; + } + if (old_ea_info) + free(old_ea_info); + } else { + errno = EINVAL; + res = -errno; + } + } else { + errno = EINVAL; + res = -errno; + } + return (res); +} + +/* + * Remove the EA (including EA_INFORMATION) + * + * EA_INFORMATION is removed first, and it is restored to its former + * state if removing EA fails. + * + * Returns 0, or -1 if there is a problem + */ + +int ntfs_remove_ntfs_ea(ntfs_inode *ni) +{ + EA_INFORMATION *old_ea_info; + s64 old_ea_size; + int res; + ntfs_attr *na; + ntfs_attr *nai; + + res = 0; + if (ni) { + /* + * open and delete the EA_INFORMATION and the EA + */ + nai = ntfs_attr_open(ni, AT_EA_INFORMATION, AT_UNNAMED, 0); + if (nai) { + na = ntfs_attr_open(ni, AT_EA, AT_UNNAMED, 0); + if (na) { + /* Try to save the old EA_INFORMATION */ + old_ea_info = ntfs_attr_readall(ni, + AT_EA_INFORMATION, + (ntfschar*)NULL, 0, &old_ea_size); + res = ntfs_attr_rm(na); + NInoFileNameSetDirty(ni); + if (!res) { + res = ntfs_attr_rm(nai); + if (res && old_ea_info) { + /* + * Failed to remove the EA, try to + * restore the EA_INFORMATION + */ + restore_ea_info(nai, + old_ea_info); + } + } else { + ntfs_log_error("Failed to remove the" + " EA_INFORMATION from inode %lld\n", + (long long)ni->mft_no); + } + free(old_ea_info); + ntfs_attr_close(na); + } else { + /* EA_INFORMATION present, but no EA */ + res = ntfs_attr_rm(nai); + NInoFileNameSetDirty(ni); + } + ntfs_attr_close(nai); + } else { + errno = ENODATA; + res = -1; + } + NInoSetDirty(ni); + } else { + errno = EINVAL; + res = -1; + } + return (res ? -1 : 0); +} + +/* + * Check for the presence of an EA "$LXDEV" (used by WSL) + * and return its value as a device address + * + * Returns zero if successful + * -1 if failed, with errno set + */ + +int ntfs_ea_check_wsldev(ntfs_inode *ni, dev_t *rdevp) +{ + const EA_ATTR *p_ea; + int bufsize; + char *buf; + int lth; + int res; + int offset; + int next; + BOOL found; + struct { + le32 major; + le32 minor; + } device; + + res = -EOPNOTSUPP; + bufsize = 256; /* expected to be enough */ + buf = (char*)malloc(bufsize); + if (buf) { + lth = ntfs_get_ntfs_ea(ni, buf, bufsize); + /* retry if short buf */ + if (lth > bufsize) { + free(buf); + bufsize = lth; + buf = (char*)malloc(bufsize); + if (buf) + lth = ntfs_get_ntfs_ea(ni, buf, bufsize); + } + } + if (buf && (lth > 0) && (lth <= bufsize)) { + offset = 0; + found = FALSE; + do { + p_ea = (const EA_ATTR*)&buf[offset]; + next = le32_to_cpu(p_ea->next_entry_offset); + found = ((next > (int)(sizeof(lxdev) + sizeof(device))) + && (p_ea->name_length == (sizeof(lxdev) - 1)) + && (p_ea->value_length + == const_cpu_to_le16(sizeof(device))) + && !memcmp(p_ea->name, lxdev, sizeof(lxdev))); + if (!found) + offset += next; + } while (!found && (next > 0) && (offset < lth)); + if (found) { + /* beware of alignment */ + memcpy(&device, &p_ea->name[p_ea->name_length + 1], + sizeof(device)); + *rdevp = makedev(le32_to_cpu(device.major), + le32_to_cpu(device.minor)); + res = 0; + } + } + free(buf); + return (res); +} + +int ntfs_ea_set_wsl_not_symlink(ntfs_inode *ni, mode_t type, dev_t dev) +{ + le32 mode; + struct { + le32 major; + le32 minor; + } device; + struct EA_WSL { + struct EA_LXMOD { /* always inserted */ + EA_ATTR base; + char name[sizeof(lxmod)]; + char value[sizeof(mode)]; + char stuff[3 & -(sizeof(lxmod) + sizeof(mode))]; + } mod; + struct EA_LXDEV { /* char or block devices only */ + EA_ATTR base; + char name[sizeof(lxdev)]; + char value[sizeof(device)]; + char stuff[3 & -(sizeof(lxdev) + sizeof(device))]; + } dev; + } attr; + int len; + int res; + + memset(&attr, 0, sizeof(attr)); + mode = cpu_to_le32((u32)(type | 0644)); + attr.mod.base.next_entry_offset + = const_cpu_to_le32(sizeof(attr.mod)); + attr.mod.base.flags = 0; + attr.mod.base.name_length = sizeof(lxmod) - 1; + attr.mod.base.value_length = const_cpu_to_le16(sizeof(mode)); + memcpy(attr.mod.name, lxmod, sizeof(lxmod)); + memcpy(attr.mod.value, &mode, sizeof(mode)); + len = sizeof(attr.mod); + + if (S_ISCHR(type) || S_ISBLK(type)) { + device.major = cpu_to_le32(major(dev)); + device.minor = cpu_to_le32(minor(dev)); + attr.dev.base.next_entry_offset + = const_cpu_to_le32(sizeof(attr.dev)); + attr.dev.base.flags = 0; + attr.dev.base.name_length = sizeof(lxdev) - 1; + attr.dev.base.value_length = const_cpu_to_le16(sizeof(device)); + memcpy(attr.dev.name, lxdev, sizeof(lxdev)); + memcpy(attr.dev.value, &device, sizeof(device)); + len += sizeof(attr.dev); + } + res = ntfs_set_ntfs_ea(ni, (char*)&attr, len, 0); + return (res); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/efs.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/efs.c new file mode 100644 index 0000000..7b87cfc --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/efs.c @@ -0,0 +1,430 @@ +/** + * efs.c - Limited processing of encrypted files + * + * This module is part of ntfs-3g library + * + * Copyright (c) 2009 Martin Bene + * Copyright (c) 2009-2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif + +#ifdef HAVE_SYS_SYSMACROS_H +#include +#endif + +#include "types.h" +#include "debug.h" +#include "attrib.h" +#include "inode.h" +#include "dir.h" +#include "efs.h" +#include "index.h" +#include "logging.h" +#include "misc.h" +#include "efs.h" +#include "xattrs.h" + +static ntfschar logged_utility_stream_name[] = { + const_cpu_to_le16('$'), + const_cpu_to_le16('E'), + const_cpu_to_le16('F'), + const_cpu_to_le16('S'), + const_cpu_to_le16(0) +} ; + + +/* + * Get the ntfs EFS info into an extended attribute + */ + +int ntfs_get_efs_info(ntfs_inode *ni, char *value, size_t size) +{ + EFS_ATTR_HEADER *efs_info; + s64 attr_size = 0; + + if (ni) { + if (ni->flags & FILE_ATTR_ENCRYPTED) { + efs_info = (EFS_ATTR_HEADER*)ntfs_attr_readall(ni, + AT_LOGGED_UTILITY_STREAM,(ntfschar*)NULL, 0, + &attr_size); + if (efs_info + && (le32_to_cpu(efs_info->length) == attr_size)) { + if (attr_size <= (s64)size) { + if (value) + memcpy(value,efs_info,attr_size); + else { + errno = EFAULT; + attr_size = 0; + } + } else + if (size) { + errno = ERANGE; + attr_size = 0; + } + free (efs_info); + } else { + if (efs_info) { + free(efs_info); + ntfs_log_error("Bad efs_info for inode %lld\n", + (long long)ni->mft_no); + } else { + ntfs_log_error("Could not get efsinfo" + " for inode %lld\n", + (long long)ni->mft_no); + } + errno = EIO; + attr_size = 0; + } + } else { + errno = ENODATA; + ntfs_log_trace("Inode %lld is not encrypted\n", + (long long)ni->mft_no); + } + } + return (attr_size ? (int)attr_size : -errno); +} + +/* + * Fix all encrypted AT_DATA attributes of an inode + * + * The fix may require making an attribute non resident, which + * requires more space in the MFT record, and may cause some + * attribute to be expelled and the full record to be reorganized. + * When this happens, the search for data attributes has to be + * reinitialized. + * + * Returns zero if successful. + * -1 if there is a problem. + */ + +static int fixup_loop(ntfs_inode *ni) +{ + ntfs_attr_search_ctx *ctx; + ntfs_attr *na; + ATTR_RECORD *a; + BOOL restart; + int cnt; + int maxcnt; + int res = 0; + + maxcnt = 0; + do { + restart = FALSE; + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) { + ntfs_log_error("Failed to get ctx for efs\n"); + res = -1; + } + cnt = 0; + while (!restart && !res + && !ntfs_attr_lookup(AT_DATA, NULL, 0, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + cnt++; + a = ctx->attr; + na = ntfs_attr_open(ctx->ntfs_ino, AT_DATA, + (ntfschar*)((u8*)a + le16_to_cpu(a->name_offset)), + a->name_length); + if (!na) { + ntfs_log_error("can't open DATA Attribute\n"); + res = -1; + } + if (na && !(ctx->attr->flags & ATTR_IS_ENCRYPTED)) { + if (!NAttrNonResident(na) + && ntfs_attr_make_non_resident(na, ctx)) { + /* + * ntfs_attr_make_non_resident fails if there + * is not enough space in the MFT record. + * When this happens, force making non-resident + * so that some other attribute is expelled. + */ + if (ntfs_attr_force_non_resident(na)) { + res = -1; + } else { + /* make sure there is some progress */ + if (cnt <= maxcnt) { + errno = EIO; + ntfs_log_error("Multiple failure" + " making non resident\n"); + res = -1; + } else { + ntfs_attr_put_search_ctx(ctx); + ctx = (ntfs_attr_search_ctx*)NULL; + restart = TRUE; + maxcnt = cnt; + } + } + } + if (!restart && !res + && ntfs_efs_fixup_attribute(ctx, na)) { + ntfs_log_error("Error in efs fixup of AT_DATA Attribute\n"); + res = -1; + } + } + if (na) + ntfs_attr_close(na); + } + } while (restart && !res); + if (ctx) + ntfs_attr_put_search_ctx(ctx); + return (res); +} + +/* + * Set the efs data from an extended attribute + * Warning : the new data is not checked + * Returns 0, or -1 if there is a problem + */ + +int ntfs_set_efs_info(ntfs_inode *ni, const char *value, size_t size, + int flags) + +{ + int res; + int written; + ntfs_attr *na; + const EFS_ATTR_HEADER *info_header; + + res = 0; + if (ni && value && size) { + if (ni->flags & (FILE_ATTR_ENCRYPTED | FILE_ATTR_COMPRESSED)) { + if (ni->flags & FILE_ATTR_ENCRYPTED) { + ntfs_log_trace("Inode %lld already encrypted\n", + (long long)ni->mft_no); + errno = EEXIST; + } else { + /* + * Possible problem : if encrypted file was + * restored in a compressed directory, it was + * restored as compressed. + * TODO : decompress first. + */ + ntfs_log_error("Inode %lld cannot be encrypted and compressed\n", + (long long)ni->mft_no); + errno = EIO; + } + return -1; + } + info_header = (const EFS_ATTR_HEADER*)value; + /* make sure we get a likely efsinfo */ + if (le32_to_cpu(info_header->length) != size) { + errno = EINVAL; + return (-1); + } + if (!ntfs_attr_exist(ni,AT_LOGGED_UTILITY_STREAM, + (ntfschar*)NULL,0)) { + if (!(flags & XATTR_REPLACE)) { + /* + * no logged_utility_stream attribute : add one, + * apparently, this does not feed the new value in + */ + res = ntfs_attr_add(ni,AT_LOGGED_UTILITY_STREAM, + logged_utility_stream_name,4, + (u8*)NULL,(s64)size); + } else { + errno = ENODATA; + res = -1; + } + } else { + errno = EEXIST; + res = -1; + } + if (!res) { + /* + * open and update the existing efs data + */ + na = ntfs_attr_open(ni, AT_LOGGED_UTILITY_STREAM, + logged_utility_stream_name, 4); + if (na) { + /* resize attribute */ + res = ntfs_attr_truncate(na, (s64)size); + /* overwrite value if any */ + if (!res && value) { + written = (int)ntfs_attr_pwrite(na, + (s64)0, (s64)size, value); + if (written != (s64)size) { + ntfs_log_error("Failed to " + "update efs data\n"); + errno = EIO; + res = -1; + } + } + ntfs_attr_close(na); + } else + res = -1; + } + if (!res) { + /* Don't handle AT_DATA Attribute(s) if inode is a directory */ + if (!(ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)) { + /* iterate over AT_DATA attributes */ + /* set encrypted flag, truncate attribute to match padding bytes */ + + if (fixup_loop(ni)) + return -1; + } + ni->flags |= FILE_ATTR_ENCRYPTED; + NInoSetDirty(ni); + NInoFileNameSetDirty(ni); + } + } else { + errno = EINVAL; + res = -1; + } + return (res ? -1 : 0); +} + +/* + * Fixup raw encrypted AT_DATA Attribute + * read padding length from last two bytes + * truncate attribute, make non-resident, + * set data size to match padding length + * set ATTR_IS_ENCRYPTED flag on attribute + * + * Return 0 if successful + * -1 if failed (errno tells why) + */ + +int ntfs_efs_fixup_attribute(ntfs_attr_search_ctx *ctx, ntfs_attr *na) +{ + s64 newsize; + s64 oldsize; + le16 appended_bytes; + u16 padding_length; + ntfs_inode *ni; + BOOL close_ctx = FALSE; + + if (!na) { + ntfs_log_error("no na specified for efs_fixup_attribute\n"); + goto err_out; + } + if (!ctx) { + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (!ctx) { + ntfs_log_error("Failed to get ctx for efs\n"); + goto err_out; + } + close_ctx = TRUE; + if (ntfs_attr_lookup(AT_DATA, na->name, na->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + ntfs_log_error("attr lookup for AT_DATA attribute failed in efs fixup\n"); + goto err_out; + } + } else { + if (!NAttrNonResident(na)) { + ntfs_log_error("Cannot make non resident" + " when a context has been allocated\n"); + goto err_out; + } + } + + /* no extra bytes are added to void attributes */ + oldsize = na->data_size; + if (oldsize) { + /* make sure size is valid for a raw encrypted stream */ + if ((oldsize & 511) != 2) { + ntfs_log_error("Bad raw encrypted stream\n"); + goto err_out; + } + /* read padding length from last two bytes of attribute */ + if (ntfs_attr_pread(na, oldsize - 2, 2, &appended_bytes) != 2) { + ntfs_log_error("Error reading padding length\n"); + goto err_out; + } + padding_length = le16_to_cpu(appended_bytes); + if (padding_length > 511 || padding_length > na->data_size-2) { + errno = EINVAL; + ntfs_log_error("invalid padding length %d for data_size %lld\n", + padding_length, (long long)oldsize); + goto err_out; + } + newsize = oldsize - padding_length - 2; + /* + * truncate attribute to possibly free clusters allocated + * for the last two bytes, but do not truncate to new size + * to avoid losing useful data + */ + if (ntfs_attr_truncate(na, oldsize - 2)) { + ntfs_log_error("Error truncating attribute\n"); + goto err_out; + } + } else + newsize = 0; + + /* + * Encrypted AT_DATA Attributes MUST be non-resident + * This has to be done after the attribute is resized, as + * resizing down to zero may cause the attribute to be made + * resident. + */ + if (!NAttrNonResident(na) + && ntfs_attr_make_non_resident(na, ctx)) { + if (!close_ctx + || ntfs_attr_force_non_resident(na)) { + ntfs_log_error("Error making DATA attribute non-resident\n"); + goto err_out; + } else { + /* + * must reinitialize context after forcing + * non-resident. We need a context for updating + * the state, and at this point, we are sure + * the context is not used elsewhere. + */ + ntfs_attr_reinit_search_ctx(ctx); + if (ntfs_attr_lookup(AT_DATA, na->name, na->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + ntfs_log_error("attr lookup for AT_DATA attribute failed in efs fixup\n"); + goto err_out; + } + } + } + ni = na->ni; + if (!na->name_len) { + ni->data_size = newsize; + ni->allocated_size = na->allocated_size; + } + NInoSetDirty(ni); + NInoFileNameSetDirty(ni); + + ctx->attr->data_size = cpu_to_sle64(newsize); + if (sle64_to_cpu(ctx->attr->initialized_size) > newsize) + ctx->attr->initialized_size = ctx->attr->data_size; + ctx->attr->flags |= ATTR_IS_ENCRYPTED; + if (close_ctx) + ntfs_attr_put_search_ctx(ctx); + + return (0); +err_out: + if (close_ctx && ctx) + ntfs_attr_put_search_ctx(ctx); + return (-1); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/index.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/index.c new file mode 100644 index 0000000..6de0e35 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/index.c @@ -0,0 +1,2270 @@ +/** + * index.c - NTFS index handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2004-2005 Anton Altaparmakov + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2005-2006 Yura Pakhuchiy + * Copyright (c) 2005-2008 Szabolcs Szakacsits + * Copyright (c) 2007-2021 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "attrib.h" +#include "debug.h" +#include "index.h" +#include "collate.h" +#include "mst.h" +#include "dir.h" +#include "logging.h" +#include "bitmap.h" +#include "reparse.h" +#include "misc.h" + +/** + * ntfs_index_entry_mark_dirty - mark an index entry dirty + * @ictx: ntfs index context describing the index entry + * + * Mark the index entry described by the index entry context @ictx dirty. + * + * If the index entry is in the index root attribute, simply mark the inode + * containing the index root attribute dirty. This ensures the mftrecord, and + * hence the index root attribute, will be written out to disk later. + * + * If the index entry is in an index block belonging to the index allocation + * attribute, set ib_dirty to TRUE, thus index block will be updated during + * ntfs_index_ctx_put. + */ +void ntfs_index_entry_mark_dirty(ntfs_index_context *ictx) +{ + if (ictx->is_in_root) + ntfs_inode_mark_dirty(ictx->actx->ntfs_ino); + else if (ictx->ib != NULL) { + ictx->ib_dirty = TRUE; + } +} + +static s64 ntfs_ib_vcn_to_pos(ntfs_index_context *icx, VCN vcn) +{ + return vcn << icx->vcn_size_bits; +} + +static VCN ntfs_ib_pos_to_vcn(ntfs_index_context *icx, s64 pos) +{ + return pos >> icx->vcn_size_bits; +} + +static int ntfs_ib_write(ntfs_index_context *icx, INDEX_BLOCK *ib) +{ + s64 ret, vcn = sle64_to_cpu(ib->index_block_vcn); + + ntfs_log_trace("vcn: %lld\n", (long long)vcn); + + ret = ntfs_attr_mst_pwrite(icx->ia_na, ntfs_ib_vcn_to_pos(icx, vcn), + 1, icx->block_size, ib); + if (ret != 1) { + ntfs_log_perror("Failed to write index block %lld, inode %llu", + (long long)vcn, (unsigned long long)icx->ni->mft_no); + return STATUS_ERROR; + } + + return STATUS_OK; +} + +static int ntfs_icx_ib_write(ntfs_index_context *icx) +{ + if (ntfs_ib_write(icx, icx->ib)) + return STATUS_ERROR; + + icx->ib_dirty = FALSE; + + return STATUS_OK; +} + +/** + * ntfs_index_ctx_get - allocate and initialize a new index context + * @ni: ntfs inode with which to initialize the context + * @name: name of the which context describes + * @name_len: length of the index name + * + * Allocate a new index context, initialize it with @ni and return it. + * Return NULL if allocation failed. + */ +ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *ni, + ntfschar *name, u32 name_len) +{ + ntfs_index_context *icx; + + ntfs_log_trace("Entering\n"); + + if (!ni) { + errno = EINVAL; + return NULL; + } + if (ni->nr_extents == -1) + ni = ni->base_ni; + icx = ntfs_calloc(sizeof(ntfs_index_context)); + if (icx) + *icx = (ntfs_index_context) { + .ni = ni, + .name = name, + .name_len = name_len, + }; + return icx; +} + +static void ntfs_index_ctx_free(ntfs_index_context *icx) +{ + ntfs_log_trace("Entering\n"); + + if (!icx->bad_index && !icx->entry) + return; + + if (icx->actx) + ntfs_attr_put_search_ctx(icx->actx); + + if (!icx->is_in_root) { + if (icx->ib_dirty) { + /* FIXME: Error handling!!! */ + ntfs_ib_write(icx, icx->ib); + } + free(icx->ib); + } + + ntfs_attr_close(icx->ia_na); +} + +/** + * ntfs_index_ctx_put - release an index context + * @icx: index context to free + * + * Release the index context @icx, releasing all associated resources. + */ +void ntfs_index_ctx_put(ntfs_index_context *icx) +{ + ntfs_index_ctx_free(icx); + free(icx); +} + +/** + * ntfs_index_ctx_reinit - reinitialize an index context + * @icx: index context to reinitialize + * + * Reinitialize the index context @icx so it can be used for ntfs_index_lookup. + */ +void ntfs_index_ctx_reinit(ntfs_index_context *icx) +{ + ntfs_log_trace("Entering\n"); + + ntfs_index_ctx_free(icx); + + *icx = (ntfs_index_context) { + .ni = icx->ni, + .name = icx->name, + .name_len = icx->name_len, + }; +} + +static leVCN *ntfs_ie_get_vcn_addr(INDEX_ENTRY *ie) +{ + return (leVCN *)((u8 *)ie + le16_to_cpu(ie->length) - sizeof(leVCN)); +} + +/** + * Get the subnode vcn to which the index entry refers. + */ +VCN ntfs_ie_get_vcn(INDEX_ENTRY *ie) +{ + return sle64_to_cpup(ntfs_ie_get_vcn_addr(ie)); +} + +static INDEX_ENTRY *ntfs_ie_get_first(INDEX_HEADER *ih) +{ + return (INDEX_ENTRY *)((u8 *)ih + le32_to_cpu(ih->entries_offset)); +} + +static INDEX_ENTRY *ntfs_ie_get_next(INDEX_ENTRY *ie) +{ + return (INDEX_ENTRY *)((char *)ie + le16_to_cpu(ie->length)); +} + +static u8 *ntfs_ie_get_end(INDEX_HEADER *ih) +{ + /* FIXME: check if it isn't overflowing the index block size */ + return (u8 *)ih + le32_to_cpu(ih->index_length); +} + +static int ntfs_ie_end(INDEX_ENTRY *ie) +{ + return ie->ie_flags & INDEX_ENTRY_END || !ie->length; +} + +/** + * Find the last entry in the index block + */ +static INDEX_ENTRY *ntfs_ie_get_last(INDEX_ENTRY *ie, char *ies_end) +{ + ntfs_log_trace("Entering\n"); + + while ((char *)ie < ies_end && !ntfs_ie_end(ie)) + ie = ntfs_ie_get_next(ie); + + return ie; +} + +static INDEX_ENTRY *ntfs_ie_get_by_pos(INDEX_HEADER *ih, int pos) +{ + INDEX_ENTRY *ie; + + ntfs_log_trace("pos: %d\n", pos); + + ie = ntfs_ie_get_first(ih); + + while (pos-- > 0) + ie = ntfs_ie_get_next(ie); + + return ie; +} + +static INDEX_ENTRY *ntfs_ie_prev(INDEX_HEADER *ih, INDEX_ENTRY *ie) +{ + INDEX_ENTRY *ie_prev = NULL; + INDEX_ENTRY *tmp; + + ntfs_log_trace("Entering\n"); + + tmp = ntfs_ie_get_first(ih); + + while (tmp != ie) { + ie_prev = tmp; + tmp = ntfs_ie_get_next(tmp); + } + + return ie_prev; +} + +char *ntfs_ie_filename_get(INDEX_ENTRY *ie) +{ + FILE_NAME_ATTR *fn; + + fn = (FILE_NAME_ATTR *)&ie->key; + return ntfs_attr_name_get(fn->file_name, fn->file_name_length); +} + +void ntfs_ie_filename_dump(INDEX_ENTRY *ie) +{ + char *s; + + s = ntfs_ie_filename_get(ie); + ntfs_log_debug("'%s' ", s); + ntfs_attr_name_free(&s); +} + +void ntfs_ih_filename_dump(INDEX_HEADER *ih) +{ + INDEX_ENTRY *ie; + + ntfs_log_trace("Entering\n"); + + ie = ntfs_ie_get_first(ih); + while (!ntfs_ie_end(ie)) { + ntfs_ie_filename_dump(ie); + ie = ntfs_ie_get_next(ie); + } +} + +static int ntfs_ih_numof_entries(INDEX_HEADER *ih) +{ + int n; + INDEX_ENTRY *ie; + u8 *end; + + ntfs_log_trace("Entering\n"); + + end = ntfs_ie_get_end(ih); + ie = ntfs_ie_get_first(ih); + for (n = 0; !ntfs_ie_end(ie) && (u8 *)ie < end; n++) + ie = ntfs_ie_get_next(ie); + return n; +} + +static int ntfs_ih_one_entry(INDEX_HEADER *ih) +{ + return (ntfs_ih_numof_entries(ih) == 1); +} + +static int ntfs_ih_zero_entry(INDEX_HEADER *ih) +{ + return (ntfs_ih_numof_entries(ih) == 0); +} + +static void ntfs_ie_delete(INDEX_HEADER *ih, INDEX_ENTRY *ie) +{ + u32 new_size; + + ntfs_log_trace("Entering\n"); + + new_size = le32_to_cpu(ih->index_length) - le16_to_cpu(ie->length); + ih->index_length = cpu_to_le32(new_size); + memmove(ie, (u8 *)ie + le16_to_cpu(ie->length), + new_size - ((u8 *)ie - (u8 *)ih)); +} + +static void ntfs_ie_set_vcn(INDEX_ENTRY *ie, VCN vcn) +{ + *ntfs_ie_get_vcn_addr(ie) = cpu_to_sle64(vcn); +} + +/** + * Insert @ie index entry at @pos entry. Used @ih values should be ok already. + */ +static void ntfs_ie_insert(INDEX_HEADER *ih, INDEX_ENTRY *ie, INDEX_ENTRY *pos) +{ + int ie_size = le16_to_cpu(ie->length); + + ntfs_log_trace("Entering\n"); + + ih->index_length = cpu_to_le32(le32_to_cpu(ih->index_length) + ie_size); + memmove((u8 *)pos + ie_size, pos, + le32_to_cpu(ih->index_length) - ((u8 *)pos - (u8 *)ih) - ie_size); + memcpy(pos, ie, ie_size); +} + +static INDEX_ENTRY *ntfs_ie_dup(INDEX_ENTRY *ie) +{ + INDEX_ENTRY *dup; + + ntfs_log_trace("Entering\n"); + + dup = ntfs_malloc(le16_to_cpu(ie->length)); + if (dup) + memcpy(dup, ie, le16_to_cpu(ie->length)); + + return dup; +} + +static INDEX_ENTRY *ntfs_ie_dup_novcn(INDEX_ENTRY *ie) +{ + INDEX_ENTRY *dup; + int size = le16_to_cpu(ie->length); + + ntfs_log_trace("Entering\n"); + + if (ie->ie_flags & INDEX_ENTRY_NODE) + size -= sizeof(VCN); + + dup = ntfs_malloc(size); + if (dup) { + memcpy(dup, ie, size); + dup->ie_flags &= ~INDEX_ENTRY_NODE; + dup->length = cpu_to_le16(size); + } + return dup; +} + +static INDEX_ROOT *ntfs_ir_lookup(ntfs_inode *ni, ntfschar *name, + u32 name_len, ntfs_attr_search_ctx **ctx) +{ + ATTR_RECORD *a; + INDEX_ROOT *ir = NULL; + + ntfs_log_trace("Entering\n"); + + *ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!*ctx) + return NULL; + + if (ntfs_attr_lookup(AT_INDEX_ROOT, name, name_len, CASE_SENSITIVE, + 0, NULL, 0, *ctx)) { + ntfs_log_perror("Failed to lookup $INDEX_ROOT"); + goto err_out; + } + + a = (*ctx)->attr; + if (a->non_resident) { + errno = EINVAL; + ntfs_log_perror("Non-resident $INDEX_ROOT detected"); + goto err_out; + } + + ir = (INDEX_ROOT *)((char *)a + le16_to_cpu(a->value_offset)); +err_out: + if (!ir) { + ntfs_attr_put_search_ctx(*ctx); + *ctx = NULL; + } + return ir; +} + +static INDEX_ROOT *ntfs_ir_lookup2(ntfs_inode *ni, ntfschar *name, u32 len) +{ + ntfs_attr_search_ctx *ctx; + INDEX_ROOT *ir; + + ir = ntfs_ir_lookup(ni, name, len, &ctx); + if (ir) + ntfs_attr_put_search_ctx(ctx); + return ir; +} + +/* + * Check the consistency of an index block + * + * Make sure the index block does not overflow from the index record. + * The size of block is assumed to have been checked to be what is + * defined in the index root. + * + * Returns 0 if no error was found + * -1 otherwise (with errno unchanged) + * + * |<--->| offsetof(INDEX_BLOCK, index) + * | |<--->| sizeof(INDEX_HEADER) + * | | | + * | | | seq index entries unused + * |=====|=====|=====|===========================|==============| + * | | | | | + * | |<--------->| entries_offset | | + * | |<---------------- index_length ------->| | + * | |<--------------------- allocated_size --------------->| + * |<--------------------------- block_size ------------------->| + * + * size(INDEX_HEADER) <= ent_offset < ind_length <= alloc_size < bk_size + */ + +int ntfs_index_block_inconsistent(const INDEX_BLOCK *ib, u32 block_size, + u64 inum, VCN vcn) +{ + u32 ib_size = (unsigned)le32_to_cpu(ib->index.allocated_size) + + offsetof(INDEX_BLOCK, index); + + if (!ntfs_is_indx_record(ib->magic)) { + ntfs_log_error("Corrupt index block signature: vcn %lld inode " + "%llu\n", (long long)vcn, + (unsigned long long)inum); + return -1; + } + + if (sle64_to_cpu(ib->index_block_vcn) != vcn) { + ntfs_log_error("Corrupt index block: VCN (%lld) is different " + "from expected VCN (%lld) in inode %llu\n", + (long long)sle64_to_cpu(ib->index_block_vcn), + (long long)vcn, + (unsigned long long)inum); + return -1; + } + + if (ib_size != block_size) { + ntfs_log_error("Corrupt index block : VCN (%lld) of inode %llu " + "has a size (%u) differing from the index " + "specified size (%u)\n", (long long)vcn, + (unsigned long long)inum, ib_size, + (unsigned int)block_size); + return -1; + } + if (le32_to_cpu(ib->index.entries_offset) < sizeof(INDEX_HEADER)) { + ntfs_log_error("Invalid index entry offset in inode %lld\n", + (unsigned long long)inum); + return -1; + } + if (le32_to_cpu(ib->index.index_length) + <= le32_to_cpu(ib->index.entries_offset)) { + ntfs_log_error("No space for index entries in inode %lld\n", + (unsigned long long)inum); + return -1; + } + if (le32_to_cpu(ib->index.allocated_size) + < le32_to_cpu(ib->index.index_length)) { + ntfs_log_error("Index entries overflow in inode %lld\n", + (unsigned long long)inum); + return -1; + } + if (ntfs_ie_stream_inconsistent(&ib->index, inum)) + return -1; + + return (0); +} + + +/* + * Check the consistency of an index entry + * + * Make sure data and key do not overflow from entry. + * As a side effect, an entry with zero length is rejected. + * This entry must be a full one (no INDEX_ENTRY_END flag), and its + * length must have been checked beforehand to not overflow from the + * index record. + * + * Returns 0 if no error was found + * -1 otherwise (with errno unchanged) + */ + +int ntfs_index_entry_inconsistent(const INDEX_ENTRY *ie, + COLLATION_RULES collation_rule, u64 inum) +{ + int ret; + + ret = 0; + if (ie->key_length + && ((le16_to_cpu(ie->key_length) + offsetof(INDEX_ENTRY, key)) + > le16_to_cpu(ie->length))) { + ntfs_log_error("Overflow from index entry in inode %lld\n", + (long long)inum); + ret = -1; + + } else + if (collation_rule == COLLATION_FILE_NAME) { + if ((offsetof(INDEX_ENTRY, key.file_name.file_name) + + ie->key.file_name.file_name_length + * sizeof(ntfschar)) + > le16_to_cpu(ie->length)) { + ntfs_log_error("File name overflow from index" + " entry in inode %lld\n", + (long long)inum); + ret = -1; + } + } else { + if (ie->data_length + && ((le16_to_cpu(ie->data_offset) + + le16_to_cpu(ie->data_length)) + > le16_to_cpu(ie->length))) { + ntfs_log_error("Data overflow from index" + " entry in inode %lld\n", + (long long)inum); + ret = -1; + } + } + return (ret); +} + +int ntfs_ie_stream_inconsistent(const INDEX_HEADER *ih, u64 inum) +{ + const u8 *ies_start = (const u8 *)ih + le32_to_cpu(ih->entries_offset); + const u8 *ies_end = (const u8 *)ih + le32_to_cpu(ih->index_length); + const u8 *ie; + + ntfs_log_trace("Entering\n"); + + for (ie = ies_start; ie < ies_end; ) { + u32 len; + const INDEX_ENTRY *ent = (const INDEX_ENTRY *)ie; + + if ((size_t)(ies_end - ie) < sizeof(INDEX_ENTRY_HEADER)) + goto err; + len = le16_to_cpu(ent->length); + if (len < sizeof(INDEX_ENTRY_HEADER) || (len & 7)) + goto err; + if ((size_t)(ies_end - ie) < len) + goto err; + if (ent->ie_flags & INDEX_ENTRY_END) { + /* END must terminate the stream exactly. */ + if (ie + len != ies_end) + goto err; + return 0; + } + ie += len; + } +err: + ntfs_log_error("Corrupt index entry stream in inode %lld\n", + (long long)inum); + return -1; +} + +/** + * Find a key in the index block. + * + * Return values: + * STATUS_OK with errno set to ESUCCESS if we know for sure that the + * entry exists and @ie_out points to this entry. + * STATUS_NOT_FOUND with errno set to ENOENT if we know for sure the + * entry doesn't exist and @ie_out is the insertion point. + * STATUS_KEEP_SEARCHING if we can't answer the above question and + * @vcn will contain the node index block. + * STATUS_ERROR with errno set if on unexpected error during lookup. + */ +static int ntfs_ie_lookup(const void *key, const int key_len, + ntfs_index_context *icx, INDEX_HEADER *ih, + VCN *vcn, INDEX_ENTRY **ie_out) +{ + INDEX_ENTRY *ie; + u8 *index_end; + int rc, item = 0; + + ntfs_log_trace("Entering\n"); + + index_end = ntfs_ie_get_end(ih); + + /* + * Loop until we exceed valid memory (corruption case) or until we + * reach the last entry. + */ + for (ie = ntfs_ie_get_first(ih); ; ie = ntfs_ie_get_next(ie)) { + /* Bounds checks. */ + if ((u8 *)ie + sizeof(INDEX_ENTRY_HEADER) > index_end || + (u8 *)ie + le16_to_cpu(ie->length) > index_end) { + errno = ERANGE; + ntfs_log_error("Index entry out of bounds in inode " + "%llu.\n", + (unsigned long long)icx->ni->mft_no); + return STATUS_ERROR; + } + /* + * The last entry cannot contain a key. It can however contain + * a pointer to a child node in the B+tree so we just break out. + */ + if (ntfs_ie_end(ie)) + break; + /* + * Not a perfect match, need to do full blown collation so we + * know which way in the B+tree we have to go. + */ + if (!icx->collate) { + ntfs_log_error("Collation function not defined\n"); + errno = EOPNOTSUPP; + return STATUS_ERROR; + } + /* Make sure key and data do not overflow from entry */ + if (ntfs_index_entry_inconsistent(ie, icx->ir->collation_rule, + icx->ni->mft_no)) { + errno = EIO; + return STATUS_ERROR; + } + rc = icx->collate(icx->ni->vol, key, key_len, + &ie->key, le16_to_cpu(ie->key_length)); + if (rc == NTFS_COLLATION_ERROR) { + ntfs_log_error("Collation error. Perhaps a filename " + "contains invalid characters?\n"); + errno = ERANGE; + return STATUS_ERROR; + } + /* + * If @key collates before the key of the current entry, there + * is definitely no such key in this index but we might need to + * descend into the B+tree so we just break out of the loop. + */ + if (rc == -1) + break; + + if (!rc) { + *ie_out = ie; + errno = 0; + icx->parent_pos[icx->pindex] = item; + return STATUS_OK; + } + + item++; + } + /* + * We have finished with this index block without success. Check for the + * presence of a child node and if not present return with errno ENOENT, + * otherwise we will keep searching in another index block. + */ + if (!(ie->ie_flags & INDEX_ENTRY_NODE)) { + ntfs_log_debug("Index entry wasn't found.\n"); + *ie_out = ie; + errno = ENOENT; + return STATUS_NOT_FOUND; + } + + /* Get the starting vcn of the index_block holding the child node. */ + *vcn = ntfs_ie_get_vcn(ie); + if (*vcn < 0) { + errno = EINVAL; + ntfs_log_perror("Negative vcn in inode %llu", + (unsigned long long)icx->ni->mft_no); + return STATUS_ERROR; + } + + ntfs_log_trace("Parent entry number %d\n", item); + icx->parent_pos[icx->pindex] = item; + + return STATUS_KEEP_SEARCHING; +} + +static ntfs_attr *ntfs_ia_open(ntfs_index_context *icx, ntfs_inode *ni) +{ + ntfs_attr *na; + + na = ntfs_attr_open(ni, AT_INDEX_ALLOCATION, icx->name, icx->name_len); + if (!na) { + ntfs_log_perror("Failed to open index allocation of inode " + "%llu", (unsigned long long)ni->mft_no); + return NULL; + } + + return na; +} + +static int ntfs_ib_read(ntfs_index_context *icx, VCN vcn, INDEX_BLOCK *dst) +{ + s64 pos, ret; + + ntfs_log_trace("vcn: %lld\n", (long long)vcn); + + pos = ntfs_ib_vcn_to_pos(icx, vcn); + + ret = ntfs_attr_mst_pread(icx->ia_na, pos, 1, icx->block_size, (u8 *)dst); + if (ret != 1) { + if (ret == -1) + ntfs_log_perror("Failed to read index block"); + else + ntfs_log_error("Failed to read full index block at " + "%lld\n", (long long)pos); + return -1; + } + + if (ntfs_index_block_inconsistent((INDEX_BLOCK*)dst, icx->block_size, + icx->ia_na->ni->mft_no, vcn)) { + errno = EIO; + return -1; + } + + return 0; +} + +static int ntfs_icx_parent_inc(ntfs_index_context *icx) +{ + icx->pindex++; + if (icx->pindex >= MAX_PARENT_VCN) { + errno = EOPNOTSUPP; + ntfs_log_perror("Index is over %d level deep", MAX_PARENT_VCN); + return STATUS_ERROR; + } + return STATUS_OK; +} + +static int ntfs_icx_parent_dec(ntfs_index_context *icx) +{ + icx->pindex--; + if (icx->pindex < 0) { + errno = EINVAL; + ntfs_log_perror("Corrupt index pointer (%d)", icx->pindex); + return STATUS_ERROR; + } + return STATUS_OK; +} + +/** + * ntfs_index_lookup - find a key in an index and return its index entry + * @key: [IN] key for which to search in the index + * @key_len: [IN] length of @key in bytes + * @icx: [IN/OUT] context describing the index and the returned entry + * + * Before calling ntfs_index_lookup(), @icx must have been obtained from a + * call to ntfs_index_ctx_get(). + * + * Look for the @key in the index specified by the index lookup context @icx. + * ntfs_index_lookup() walks the contents of the index looking for the @key. + * + * If the @key is found in the index, 0 is returned and @icx is setup to + * describe the index entry containing the matching @key. @icx->entry is the + * index entry and @icx->data and @icx->data_len are the index entry data and + * its length in bytes, respectively. + * + * If the @key is not found in the index, -1 is returned, errno = ENOENT and + * @icx is setup to describe the index entry whose key collates immediately + * after the search @key, i.e. this is the position in the index at which + * an index entry with a key of @key would need to be inserted. + * + * If an error occurs return -1, set errno to error code and @icx is left + * untouched. + * + * When finished with the entry and its data, call ntfs_index_ctx_put() to free + * the context and other associated resources. + * + * If the index entry was modified, call ntfs_index_entry_mark_dirty() before + * the call to ntfs_index_ctx_put() to ensure that the changes are written + * to disk. + */ +int ntfs_index_lookup(const void *key, const int key_len, ntfs_index_context *icx) +{ + VCN old_vcn, vcn; + ntfs_inode *ni = icx->ni; + INDEX_ROOT *ir; + INDEX_ENTRY *ie; + INDEX_BLOCK *ib = NULL; + int ret, err = 0; + + ntfs_log_trace("Entering\n"); + + if (!key || key_len <= 0) { + errno = EINVAL; + ntfs_log_perror("key: %p key_len: %d", key, key_len); + return -1; + } + + ir = ntfs_ir_lookup(ni, icx->name, icx->name_len, &icx->actx); + if (!ir) { + if (errno == ENOENT) + errno = EIO; + return -1; + } + + icx->block_size = le32_to_cpu(ir->index_block_size); + if (icx->block_size < NTFS_BLOCK_SIZE) { + errno = EINVAL; + ntfs_log_perror("Index block size (%d) is smaller than the " + "sector size (%d)", icx->block_size, NTFS_BLOCK_SIZE); + goto err_out; + } + + if (ni->vol->cluster_size <= icx->block_size) + icx->vcn_size_bits = ni->vol->cluster_size_bits; + else + icx->vcn_size_bits = NTFS_BLOCK_SIZE_BITS; + /* get the appropriate collation function */ + icx->ir = ir; + icx->collate = ntfs_get_collate_function(ir->collation_rule); + if (!icx->collate) { + err = errno = EOPNOTSUPP; + ntfs_log_perror("Unknown collation rule 0x%x", + (unsigned)le32_to_cpu(ir->collation_rule)); + goto err_out; + } + + old_vcn = VCN_INDEX_ROOT_PARENT; + ret = ntfs_ie_lookup(key, key_len, icx, &ir->index, &vcn, &ie); + if (ret == STATUS_ERROR) { + err = errno; + goto err_lookup; + } + + icx->ir = ir; + + if (ret != STATUS_KEEP_SEARCHING) { + /* STATUS_OK or STATUS_NOT_FOUND */ + err = errno; + icx->is_in_root = TRUE; + icx->parent_vcn[icx->pindex] = old_vcn; + goto done; + } + + /* Child node present, descend into it. */ + + icx->ia_na = ntfs_ia_open(icx, ni); + if (!icx->ia_na) + goto err_out; + + ib = ntfs_malloc(icx->block_size); + if (!ib) { + err = errno; + goto err_out; + } + +descend_into_child_node: + + icx->parent_vcn[icx->pindex] = old_vcn; + if (ntfs_icx_parent_inc(icx)) { + err = errno; + goto err_out; + } + old_vcn = vcn; + + ntfs_log_debug("Descend into node with VCN %lld\n", (long long)vcn); + + if (ntfs_ib_read(icx, vcn, ib)) + goto err_out; + + ret = ntfs_ie_lookup(key, key_len, icx, &ib->index, &vcn, &ie); + if (ret != STATUS_KEEP_SEARCHING) { + err = errno; + if (ret == STATUS_ERROR) + goto err_out; + + /* STATUS_OK or STATUS_NOT_FOUND */ + icx->is_in_root = FALSE; + icx->ib = ib; + icx->parent_vcn[icx->pindex] = vcn; + goto done; + } + + if ((ib->index.ih_flags & NODE_MASK) == LEAF_NODE) { + ntfs_log_error("Index entry with child node found in a leaf " + "node in inode 0x%llx.\n", + (unsigned long long)ni->mft_no); + goto err_out; + } + + goto descend_into_child_node; +err_out: + icx->bad_index = TRUE; /* Force icx->* to be freed */ +err_lookup: + if (icx->actx) { + ntfs_attr_put_search_ctx(icx->actx); + icx->actx = NULL; + } + free(ib); + if (!err) + err = EIO; + errno = err; + return -1; +done: + icx->entry = ie; + icx->data = (u8 *)ie + offsetof(INDEX_ENTRY, key); + icx->data_len = le16_to_cpu(ie->key_length); + ntfs_log_trace("Done.\n"); + if (err) { + errno = err; + return -1; + } + return 0; + +} + +static INDEX_BLOCK *ntfs_ib_alloc(VCN ib_vcn, u32 ib_size, + INDEX_HEADER_FLAGS node_type) +{ + INDEX_BLOCK *ib; + int ih_size = sizeof(INDEX_HEADER); + + ntfs_log_trace("ib_vcn: %lld ib_size: %u\n", (long long)ib_vcn, ib_size); + + ib = ntfs_calloc(ib_size); + if (!ib) + return NULL; + + ib->magic = magic_INDX; + ib->usa_ofs = const_cpu_to_le16(sizeof(INDEX_BLOCK)); + ib->usa_count = cpu_to_le16(ib_size / NTFS_BLOCK_SIZE + 1); + /* Set USN to 1 */ + *(le16 *)((char *)ib + le16_to_cpu(ib->usa_ofs)) = const_cpu_to_le16(1); + ib->lsn = const_cpu_to_sle64(0); + + ib->index_block_vcn = cpu_to_sle64(ib_vcn); + + ib->index.entries_offset = cpu_to_le32((ih_size + + le16_to_cpu(ib->usa_count) * 2 + 7) & ~7); + ib->index.index_length = const_cpu_to_le32(0); + ib->index.allocated_size = cpu_to_le32(ib_size - + (sizeof(INDEX_BLOCK) - ih_size)); + ib->index.ih_flags = node_type; + + return ib; +} + +/** + * Find the median by going through all the entries + */ +static INDEX_ENTRY *ntfs_ie_get_median(INDEX_HEADER *ih) +{ + INDEX_ENTRY *ie, *ie_start; + u8 *ie_end; + int i = 0, median; + + ntfs_log_trace("Entering\n"); + + ie = ie_start = ntfs_ie_get_first(ih); + ie_end = (u8 *)ntfs_ie_get_end(ih); + + while ((u8 *)ie < ie_end && !ntfs_ie_end(ie)) { + ie = ntfs_ie_get_next(ie); + i++; + } + /* + * NOTE: this could be also the entry at the half of the index block. + */ + median = i / 2 - 1; + + ntfs_log_trace("Entries: %d median: %d\n", i, median); + + for (i = 0, ie = ie_start; i <= median; i++) + ie = ntfs_ie_get_next(ie); + + return ie; +} + +static s64 ntfs_ibm_vcn_to_pos(ntfs_index_context *icx, VCN vcn) +{ + return ntfs_ib_vcn_to_pos(icx, vcn) / icx->block_size; +} + +static s64 ntfs_ibm_pos_to_vcn(ntfs_index_context *icx, s64 pos) +{ + return ntfs_ib_pos_to_vcn(icx, pos * icx->block_size); +} + +static int ntfs_ibm_add(ntfs_index_context *icx) +{ + u8 bmp[8]; + + ntfs_log_trace("Entering\n"); + + if (ntfs_attr_exist(icx->ni, AT_BITMAP, icx->name, icx->name_len)) + return STATUS_OK; + /* + * AT_BITMAP must be at least 8 bytes. + */ + memset(bmp, 0, sizeof(bmp)); + if (ntfs_attr_add(icx->ni, AT_BITMAP, icx->name, icx->name_len, + bmp, sizeof(bmp))) { + ntfs_log_perror("Failed to add AT_BITMAP"); + return STATUS_ERROR; + } + + return STATUS_OK; +} + +static int ntfs_ibm_modify(ntfs_index_context *icx, VCN vcn, int set) +{ + u8 byte; + s64 pos = ntfs_ibm_vcn_to_pos(icx, vcn); + u32 bpos = pos / 8; + u32 bit = 1 << (pos % 8); + ntfs_attr *na; + int ret = STATUS_ERROR; + + ntfs_log_trace("%s vcn: %lld\n", set ? "set" : "clear", (long long)vcn); + + na = ntfs_attr_open(icx->ni, AT_BITMAP, icx->name, icx->name_len); + if (!na) { + ntfs_log_perror("Failed to open $BITMAP attribute"); + return -1; + } + + if (set) { + if (na->data_size < bpos + 1) { + if (ntfs_attr_truncate(na, (na->data_size + 8) & ~7)) { + ntfs_log_perror("Failed to truncate AT_BITMAP"); + goto err_na; + } + } + } + + if (ntfs_attr_pread(na, bpos, 1, &byte) != 1) { + ntfs_log_perror("Failed to read $BITMAP"); + goto err_na; + } + + if (set) + byte |= bit; + else + byte &= ~bit; + + if (ntfs_attr_pwrite(na, bpos, 1, &byte) != 1) { + ntfs_log_perror("Failed to write $Bitmap"); + goto err_na; + } + + ret = STATUS_OK; +err_na: + ntfs_attr_close(na); + return ret; +} + + +static int ntfs_ibm_set(ntfs_index_context *icx, VCN vcn) +{ + return ntfs_ibm_modify(icx, vcn, 1); +} + +static int ntfs_ibm_clear(ntfs_index_context *icx, VCN vcn) +{ + return ntfs_ibm_modify(icx, vcn, 0); +} + +static VCN ntfs_ibm_get_free(ntfs_index_context *icx) +{ + u8 *bm; + int bit; + s64 vcn, byte, size; + + ntfs_log_trace("Entering\n"); + + bm = ntfs_attr_readall(icx->ni, AT_BITMAP, icx->name, icx->name_len, + &size); + if (!bm) + return (VCN)-1; + + for (byte = 0; byte < size; byte++) { + + if (bm[byte] == 255) + continue; + + for (bit = 0; bit < 8; bit++) { + if (!(bm[byte] & (1 << bit))) { + vcn = ntfs_ibm_pos_to_vcn(icx, byte * 8 + bit); + goto out; + } + } + } + + vcn = ntfs_ibm_pos_to_vcn(icx, size * 8); +out: + ntfs_log_trace("allocated vcn: %lld\n", (long long)vcn); + + if (ntfs_ibm_set(icx, vcn)) + vcn = (VCN)-1; + + free(bm); + return vcn; +} + +static INDEX_BLOCK *ntfs_ir_to_ib(INDEX_ROOT *ir, VCN ib_vcn) +{ + u32 ib_size; + INDEX_BLOCK *ib; + INDEX_ENTRY *ie_last; + char *ies_start, *ies_end; + int i; + + ntfs_log_trace("Entering\n"); + + ib_size = le32_to_cpu(ir->index_block_size); + ib = ntfs_ib_alloc(ib_vcn, ib_size, LEAF_NODE); + if (!ib) + return NULL; + + ies_start = (char *)ntfs_ie_get_first(&ir->index); + ies_end = (char *)ntfs_ie_get_end(&ir->index); + ie_last = ntfs_ie_get_last((INDEX_ENTRY *)ies_start, ies_end); + /* + * Copy all entries, including the termination entry + * as well, which can never have any data. + */ + i = (char *)ie_last - ies_start + le16_to_cpu(ie_last->length); + + if (offsetof(INDEX_BLOCK, index) + le32_to_cpu(ib->index.entries_offset) + + i > ib_size) + { + ntfs_log_error("Last entry in index root overflows the index " + "block size: %d (index block size: %lu)\n", + i, (unsigned long)ib_size); + free(ib); + errno = EIO; + return NULL; + } + + memcpy(ntfs_ie_get_first(&ib->index), ies_start, i); + + ib->index.ih_flags = ir->index.ih_flags; + ib->index.index_length = cpu_to_le32(i + + le32_to_cpu(ib->index.entries_offset)); + return ib; +} + +static void ntfs_ir_nill(INDEX_ROOT *ir) +{ + INDEX_ENTRY *ie_last; + char *ies_start, *ies_end; + + ntfs_log_trace("Entering\n"); + /* + * TODO: This function could be much simpler. + */ + ies_start = (char *)ntfs_ie_get_first(&ir->index); + ies_end = (char *)ntfs_ie_get_end(&ir->index); + ie_last = ntfs_ie_get_last((INDEX_ENTRY *)ies_start, ies_end); + /* + * Move the index root termination entry forward + */ + if ((char *)ie_last > ies_start) { + memmove(ies_start, (char *)ie_last, le16_to_cpu(ie_last->length)); + ie_last = (INDEX_ENTRY *)ies_start; + } +} + +static int ntfs_ib_copy_tail(ntfs_index_context *icx, INDEX_BLOCK *src, + INDEX_ENTRY *median, VCN new_vcn) +{ + u8 *ies_end; + INDEX_ENTRY *ie_head; /* first entry after the median */ + int tail_size, dst_capacity, ret; + INDEX_BLOCK *dst; + + ntfs_log_trace("Entering\n"); + + dst = ntfs_ib_alloc(new_vcn, icx->block_size, + src->index.ih_flags & NODE_MASK); + if (!dst) + return STATUS_ERROR; + + ie_head = ntfs_ie_get_next(median); + + ies_end = (u8 *)ntfs_ie_get_end(&src->index); + tail_size = ies_end - (u8 *)ie_head; + dst_capacity = (int)(le32_to_cpu(dst->index.allocated_size) + - le32_to_cpu(dst->index.entries_offset)); + + /* + * src->index.entries_offset (on-disk, only required to be >= + * sizeof(INDEX_HEADER) by ntfs_index_block_inconsistent) may be smaller + * than dst's, which ntfs_ib_alloc fixes (40 on a 4 KiB block). When + * the median lands near the start of the source stream the gap lets + * tail_size exceed dst's usable space. The < 0 guard catches a + * negative tail_size before memcpy sign-extends it into a huge size_t. + */ + if (tail_size < 0 || tail_size > dst_capacity) { + ntfs_log_error("Invalid tail_size %d (dst capacity %d) in " + "ntfs_ib_copy_tail\n", tail_size, dst_capacity); + free(dst); + errno = EIO; + return STATUS_ERROR; + } + + memcpy(ntfs_ie_get_first(&dst->index), ie_head, tail_size); + + dst->index.index_length = cpu_to_le32(tail_size + + le32_to_cpu(dst->index.entries_offset)); + ret = ntfs_ib_write(icx, dst); + + free(dst); + return ret; +} + +static int ntfs_ib_cut_tail(ntfs_index_context *icx, INDEX_BLOCK *ib, + INDEX_ENTRY *ie) +{ + char *ies_start, *ies_end; + INDEX_ENTRY *ie_last; + + ntfs_log_trace("Entering\n"); + + ies_start = (char *)ntfs_ie_get_first(&ib->index); + ies_end = (char *)ntfs_ie_get_end(&ib->index); + + ie_last = ntfs_ie_get_last((INDEX_ENTRY *)ies_start, ies_end); + if (ie_last->ie_flags & INDEX_ENTRY_NODE) + ntfs_ie_set_vcn(ie_last, ntfs_ie_get_vcn(ie)); + + memcpy(ie, ie_last, le16_to_cpu(ie_last->length)); + + ib->index.index_length = cpu_to_le32(((char *)ie - ies_start) + + le16_to_cpu(ie->length) + le32_to_cpu(ib->index.entries_offset)); + + if (ntfs_ib_write(icx, ib)) + return STATUS_ERROR; + + return STATUS_OK; +} + +static int ntfs_ia_add(ntfs_index_context *icx) +{ + ntfs_log_trace("Entering\n"); + + if (ntfs_ibm_add(icx)) + return -1; + + if (!ntfs_attr_exist(icx->ni, AT_INDEX_ALLOCATION, icx->name, icx->name_len)) { + + if (ntfs_attr_add(icx->ni, AT_INDEX_ALLOCATION, icx->name, + icx->name_len, NULL, 0)) { + ntfs_log_perror("Failed to add AT_INDEX_ALLOCATION"); + return -1; + } + } + + icx->ia_na = ntfs_ia_open(icx, icx->ni); + if (!icx->ia_na) + return -1; + + return 0; +} + +static int ntfs_ir_reparent(ntfs_index_context *icx) +{ + ntfs_attr_search_ctx *ctx = NULL; + INDEX_ROOT *ir; + INDEX_ENTRY *ie; + INDEX_BLOCK *ib = NULL; + VCN new_ib_vcn; + int ix_root_size; + int ret = STATUS_ERROR; + + ntfs_log_trace("Entering\n"); + + ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); + if (!ir) + goto out; + + if ((ir->index.ih_flags & NODE_MASK) == SMALL_INDEX) + if (ntfs_ia_add(icx)) + goto out; + + new_ib_vcn = ntfs_ibm_get_free(icx); + if (new_ib_vcn == -1) + goto out; + + ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); + if (!ir) + goto clear_bmp; + + ib = ntfs_ir_to_ib(ir, new_ib_vcn); + if (ib == NULL) { + ntfs_log_perror("Failed to move index root to index block"); + goto clear_bmp; + } + + if (ntfs_ib_write(icx, ib)) + goto clear_bmp; + +retry : + ir = ntfs_ir_lookup(icx->ni, icx->name, icx->name_len, &ctx); + if (!ir) + goto clear_bmp; + + ntfs_ir_nill(ir); + + ie = ntfs_ie_get_first(&ir->index); + ie->ie_flags |= INDEX_ENTRY_NODE; + ie->length = const_cpu_to_le16(sizeof(INDEX_ENTRY_HEADER) + sizeof(VCN)); + + ir->index.ih_flags = LARGE_INDEX; + ir->index.index_length = cpu_to_le32(le32_to_cpu(ir->index.entries_offset) + + le16_to_cpu(ie->length)); + ir->index.allocated_size = ir->index.index_length; + ix_root_size = sizeof(INDEX_ROOT) - sizeof(INDEX_HEADER) + + le32_to_cpu(ir->index.allocated_size); + if (ntfs_resident_attr_value_resize(ctx->mrec, ctx->attr, + ix_root_size)) { + /* + * When there is no space to build a non-resident + * index, we may have to move the root to an extent + */ + if ((errno == ENOSPC) + && (ctx->al_entry || !ntfs_inode_add_attrlist(icx->ni))) { + ntfs_attr_put_search_ctx(ctx); + ctx = (ntfs_attr_search_ctx*)NULL; + ir = ntfs_ir_lookup(icx->ni, icx->name, icx->name_len, + &ctx); + if (ir + && !ntfs_attr_record_move_away(ctx, ix_root_size + - le32_to_cpu(ctx->attr->value_length))) { + ntfs_attr_put_search_ctx(ctx); + ctx = (ntfs_attr_search_ctx*)NULL; + goto retry; + } + } + /* FIXME: revert index root */ + goto clear_bmp; + } + /* + * FIXME: do it earlier if we have enough space in IR (should always), + * so in error case we wouldn't lose the IB. + */ + ntfs_ie_set_vcn(ie, new_ib_vcn); + + ret = STATUS_OK; +err_out: + free(ib); + ntfs_attr_put_search_ctx(ctx); +out: + return ret; +clear_bmp: + ntfs_ibm_clear(icx, new_ib_vcn); + goto err_out; +} + +/** + * ntfs_ir_truncate - Truncate index root attribute + * + * Returns STATUS_OK, STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT or STATUS_ERROR. + */ +static int ntfs_ir_truncate(ntfs_index_context *icx, int data_size) +{ + ntfs_attr *na; + int ret; + + ntfs_log_trace("Entering\n"); + + na = ntfs_attr_open(icx->ni, AT_INDEX_ROOT, icx->name, icx->name_len); + if (!na) { + ntfs_log_perror("Failed to open INDEX_ROOT"); + return STATUS_ERROR; + } + /* + * INDEX_ROOT must be resident and its entries can be moved to + * INDEX_BLOCK, so ENOSPC isn't a real error. + */ + ret = ntfs_attr_truncate(na, data_size + offsetof(INDEX_ROOT, index)); + if (ret == STATUS_OK) { + + icx->ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); + if (!icx->ir) + return STATUS_ERROR; + + icx->ir->index.allocated_size = cpu_to_le32(data_size); + + } else if (ret == STATUS_ERROR) + ntfs_log_perror("Failed to truncate INDEX_ROOT"); + + ntfs_attr_close(na); + return ret; +} + +/** + * ntfs_ir_make_space - Make more space for the index root attribute + * + * On success return STATUS_OK or STATUS_KEEP_SEARCHING. + * On error return STATUS_ERROR. + */ +static int ntfs_ir_make_space(ntfs_index_context *icx, int data_size) +{ + int ret; + + ntfs_log_trace("Entering\n"); + + ret = ntfs_ir_truncate(icx, data_size); + if (ret == STATUS_RESIDENT_ATTRIBUTE_FILLED_MFT) { + + ret = ntfs_ir_reparent(icx); + if (ret == STATUS_OK) + ret = STATUS_KEEP_SEARCHING; + else + ntfs_log_perror("Failed to nodify INDEX_ROOT"); + } + + return ret; +} + +/* + * NOTE: 'ie' must be a copy of a real index entry. + */ +static int ntfs_ie_add_vcn(INDEX_ENTRY **ie) +{ + INDEX_ENTRY *p, *old = *ie; + + old->length = cpu_to_le16(le16_to_cpu(old->length) + sizeof(VCN)); + p = realloc(old, le16_to_cpu(old->length)); + if (!p) + return STATUS_ERROR; + + p->ie_flags |= INDEX_ENTRY_NODE; + *ie = p; + + return STATUS_OK; +} + +static int ntfs_ih_insert(INDEX_HEADER *ih, INDEX_ENTRY *orig_ie, VCN new_vcn, + int pos) +{ + INDEX_ENTRY *ie_node, *ie; + int ret = STATUS_ERROR; + VCN old_vcn; + + ntfs_log_trace("Entering\n"); + + ie = ntfs_ie_dup(orig_ie); + if (!ie) + return STATUS_ERROR; + + if (!(ie->ie_flags & INDEX_ENTRY_NODE)) + if (ntfs_ie_add_vcn(&ie)) + goto out; + + ie_node = ntfs_ie_get_by_pos(ih, pos); + old_vcn = ntfs_ie_get_vcn(ie_node); + ntfs_ie_set_vcn(ie_node, new_vcn); + + ntfs_ie_insert(ih, ie, ie_node); + ntfs_ie_set_vcn(ie_node, old_vcn); + ret = STATUS_OK; +out: + free(ie); + + return ret; +} + +static VCN ntfs_icx_parent_vcn(ntfs_index_context *icx) +{ + return icx->parent_vcn[icx->pindex]; +} + +static VCN ntfs_icx_parent_pos(ntfs_index_context *icx) +{ + return icx->parent_pos[icx->pindex]; +} + + +static int ntfs_ir_insert_median(ntfs_index_context *icx, INDEX_ENTRY *median, + VCN new_vcn) +{ + u32 new_size; + int ret; + + ntfs_log_trace("Entering\n"); + + icx->ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); + if (!icx->ir) + return STATUS_ERROR; + + new_size = le32_to_cpu(icx->ir->index.index_length) + + le16_to_cpu(median->length); + if (!(median->ie_flags & INDEX_ENTRY_NODE)) + new_size += sizeof(VCN); + + ret = ntfs_ir_make_space(icx, new_size); + if (ret != STATUS_OK) + return ret; + + icx->ir = ntfs_ir_lookup2(icx->ni, icx->name, icx->name_len); + if (!icx->ir) + return STATUS_ERROR; + + return ntfs_ih_insert(&icx->ir->index, median, new_vcn, + ntfs_icx_parent_pos(icx)); +} + +static int ntfs_ib_split(ntfs_index_context *icx, INDEX_BLOCK *ib); + +/** + * On success return STATUS_OK or STATUS_KEEP_SEARCHING. + * On error return STATUS_ERROR. + */ +static int ntfs_ib_insert(ntfs_index_context *icx, INDEX_ENTRY *ie, VCN new_vcn) +{ + INDEX_BLOCK *ib; + u32 idx_size, allocated_size; + int err = STATUS_ERROR; + VCN old_vcn; + + ntfs_log_trace("Entering\n"); + + ib = ntfs_malloc(icx->block_size); + if (!ib) + return -1; + + old_vcn = ntfs_icx_parent_vcn(icx); + + if (ntfs_ib_read(icx, old_vcn, ib)) + goto err_out; + + idx_size = le32_to_cpu(ib->index.index_length); + allocated_size = le32_to_cpu(ib->index.allocated_size); + /* FIXME: sizeof(VCN) should be included only if ie has no VCN */ + if (idx_size + le16_to_cpu(ie->length) + sizeof(VCN) > allocated_size) { + err = ntfs_ib_split(icx, ib); + if (err == STATUS_OK) + err = STATUS_KEEP_SEARCHING; + goto err_out; + } + + if (ntfs_ih_insert(&ib->index, ie, new_vcn, ntfs_icx_parent_pos(icx))) + goto err_out; + + if (ntfs_ib_write(icx, ib)) + goto err_out; + + err = STATUS_OK; +err_out: + free(ib); + return err; +} + +/** + * ntfs_ib_split - Split an index block + * + * On success return STATUS_OK or STATUS_KEEP_SEARCHING. + * On error return is STATUS_ERROR. + */ +static int ntfs_ib_split(ntfs_index_context *icx, INDEX_BLOCK *ib) +{ + INDEX_ENTRY *median; + VCN new_vcn; + int ret; + + ntfs_log_trace("Entering\n"); + + if (ntfs_icx_parent_dec(icx)) + return STATUS_ERROR; + + median = ntfs_ie_get_median(&ib->index); + new_vcn = ntfs_ibm_get_free(icx); + if (new_vcn == -1) + return STATUS_ERROR; + + if (ntfs_ib_copy_tail(icx, ib, median, new_vcn)) { + ntfs_ibm_clear(icx, new_vcn); + return STATUS_ERROR; + } + + if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) + ret = ntfs_ir_insert_median(icx, median, new_vcn); + else + ret = ntfs_ib_insert(icx, median, new_vcn); + + if (ret != STATUS_OK) { + ntfs_ibm_clear(icx, new_vcn); + return ret; + } + + ret = ntfs_ib_cut_tail(icx, ib, median); + + return ret; +} + +/* JPA static */ +int ntfs_ie_add(ntfs_index_context *icx, INDEX_ENTRY *ie) +{ + INDEX_HEADER *ih; + int allocated_size, new_size; + int ret = STATUS_ERROR; + +#ifdef DEBUG +/* removed by JPA to make function usable for security indexes + char *fn; + fn = ntfs_ie_filename_get(ie); + ntfs_log_trace("file: '%s'\n", fn); + ntfs_attr_name_free(&fn); +*/ +#endif + + while (1) { + + if (!ntfs_index_lookup(&ie->key, le16_to_cpu(ie->key_length), icx)) { + errno = EEXIST; + ntfs_log_perror("Index already have such entry"); + goto err_out; + } + if (errno != ENOENT) { + ntfs_log_perror("Failed to find place for new entry"); + goto err_out; + } + + if (icx->is_in_root) + ih = &icx->ir->index; + else + ih = &icx->ib->index; + + allocated_size = le32_to_cpu(ih->allocated_size); + new_size = le32_to_cpu(ih->index_length) + le16_to_cpu(ie->length); + + if (new_size <= allocated_size) + break; + + ntfs_log_trace("index block sizes: allocated: %d needed: %d\n", + allocated_size, new_size); + + if (icx->is_in_root) { + if (ntfs_ir_make_space(icx, new_size) == STATUS_ERROR) + goto err_out; + } else { + if (ntfs_ib_split(icx, icx->ib) == STATUS_ERROR) + goto err_out; + } + + ntfs_inode_mark_dirty(icx->actx->ntfs_ino); + ntfs_index_ctx_reinit(icx); + } + + ntfs_ie_insert(ih, ie, icx->entry); + ntfs_index_entry_mark_dirty(icx); + + ret = STATUS_OK; +err_out: + ntfs_log_trace("%s\n", ret ? "Failed" : "Done"); + return ret; +} + +/** + * ntfs_index_add_filename - add filename to directory index + * @ni: ntfs inode describing directory to which index add filename + * @fn: FILE_NAME attribute to add + * @mref: reference of the inode which @fn describes + * + * Return 0 on success or -1 on error with errno set to the error code. + */ +int ntfs_index_add_filename(ntfs_inode *ni, FILE_NAME_ATTR *fn, MFT_REF mref) +{ + INDEX_ENTRY *ie; + ntfs_index_context *icx; + int fn_size, ie_size, err, ret = -1; + + ntfs_log_trace("Entering\n"); + + if (!ni || !fn) { + ntfs_log_error("Invalid arguments.\n"); + errno = EINVAL; + return -1; + } + + fn_size = (fn->file_name_length * sizeof(ntfschar)) + + offsetof(FILE_NAME_ATTR, file_name); + ie_size = (sizeof(INDEX_ENTRY_HEADER) + fn_size + 7) & ~7; + + ie = ntfs_calloc(ie_size); + if (!ie) + return -1; + + ie->indexed_file = cpu_to_le64(mref); + ie->length = cpu_to_le16(ie_size); + ie->key_length = cpu_to_le16(fn_size); + memcpy(&ie->key, fn, fn_size); + + icx = ntfs_index_ctx_get(ni, NTFS_INDEX_I30, 4); + if (!icx) + goto out; + + ret = ntfs_ie_add(icx, ie); + err = errno; + ntfs_index_ctx_put(icx); + errno = err; +out: + free(ie); + return ret; +} + +static int ntfs_ih_takeout(ntfs_index_context *icx, INDEX_HEADER *ih, + INDEX_ENTRY *ie, INDEX_BLOCK *ib) +{ + INDEX_ENTRY *ie_roam; + int freed_space; + BOOL full; + int ret = STATUS_ERROR; + + ntfs_log_trace("Entering\n"); + + full = ih->index_length == ih->allocated_size; + ie_roam = ntfs_ie_dup_novcn(ie); + if (!ie_roam) + return STATUS_ERROR; + + ntfs_ie_delete(ih, ie); + + if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) { + /* + * Recover the space which may have been freed + * while deleting an entry from root index + */ + freed_space = le32_to_cpu(ih->allocated_size) + - le32_to_cpu(ih->index_length); + if (full && (freed_space > 0) && !(freed_space & 7)) { + ntfs_ir_truncate(icx, le32_to_cpu(ih->index_length)); + /* do nothing if truncation fails */ + } + ntfs_inode_mark_dirty(icx->actx->ntfs_ino); + } else + if (ntfs_ib_write(icx, ib)) + goto out; + + ntfs_index_ctx_reinit(icx); + + ret = ntfs_ie_add(icx, ie_roam); +out: + free(ie_roam); + return ret; +} + +/** + * Used if an empty index block to be deleted has END entry as the parent + * in the INDEX_ROOT which is the only one there. + */ +static void ntfs_ir_leafify(ntfs_index_context *icx, INDEX_HEADER *ih) +{ + INDEX_ENTRY *ie; + + ntfs_log_trace("Entering\n"); + + ie = ntfs_ie_get_first(ih); + ie->ie_flags &= ~INDEX_ENTRY_NODE; + ie->length = cpu_to_le16(le16_to_cpu(ie->length) - sizeof(VCN)); + + ih->index_length = cpu_to_le32(le32_to_cpu(ih->index_length) - sizeof(VCN)); + ih->ih_flags &= ~LARGE_INDEX; + + /* Not fatal error */ + ntfs_ir_truncate(icx, le32_to_cpu(ih->index_length)); +} + +/** + * Used if an empty index block to be deleted has END entry as the parent + * in the INDEX_ROOT which is not the only one there. + */ +static int ntfs_ih_reparent_end(ntfs_index_context *icx, INDEX_HEADER *ih, + INDEX_BLOCK *ib) +{ + INDEX_ENTRY *ie, *ie_prev; + + ntfs_log_trace("Entering\n"); + + ie = ntfs_ie_get_by_pos(ih, ntfs_icx_parent_pos(icx)); + ie_prev = ntfs_ie_prev(ih, ie); + + ntfs_ie_set_vcn(ie, ntfs_ie_get_vcn(ie_prev)); + + return ntfs_ih_takeout(icx, ih, ie_prev, ib); +} + +static int ntfs_index_rm_leaf(ntfs_index_context *icx) +{ + INDEX_BLOCK *ib = NULL; + INDEX_HEADER *parent_ih; + INDEX_ENTRY *ie; + int ret = STATUS_ERROR; + + ntfs_log_trace("pindex: %d\n", icx->pindex); + + if (ntfs_icx_parent_dec(icx)) + return STATUS_ERROR; + + if (ntfs_ibm_clear(icx, icx->parent_vcn[icx->pindex + 1])) + return STATUS_ERROR; + + if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) + parent_ih = &icx->ir->index; + else { + ib = ntfs_malloc(icx->block_size); + if (!ib) + return STATUS_ERROR; + + if (ntfs_ib_read(icx, ntfs_icx_parent_vcn(icx), ib)) + goto out; + + parent_ih = &ib->index; + } + + ie = ntfs_ie_get_by_pos(parent_ih, ntfs_icx_parent_pos(icx)); + if (!ntfs_ie_end(ie)) { + ret = ntfs_ih_takeout(icx, parent_ih, ie, ib); + goto out; + } + + if (ntfs_ih_zero_entry(parent_ih)) { + + if (ntfs_icx_parent_vcn(icx) == VCN_INDEX_ROOT_PARENT) { + ntfs_ir_leafify(icx, parent_ih); + goto ok; + } + + ret = ntfs_index_rm_leaf(icx); + goto out; + } + + if (ntfs_ih_reparent_end(icx, parent_ih, ib)) + goto out; +ok: + ret = STATUS_OK; +out: + free(ib); + return ret; +} + +static int ntfs_index_rm_node(ntfs_index_context *icx) +{ + int entry_pos, pindex; + VCN vcn; + INDEX_BLOCK *ib = NULL; + INDEX_ENTRY *ie_succ, *ie, *entry = icx->entry; + INDEX_HEADER *ih; + u32 new_size; + int delta, ret = STATUS_ERROR; + + ntfs_log_trace("Entering\n"); + + if (!icx->ia_na) { + icx->ia_na = ntfs_ia_open(icx, icx->ni); + if (!icx->ia_na) + return STATUS_ERROR; + } + + ib = ntfs_malloc(icx->block_size); + if (!ib) + return STATUS_ERROR; + + ie_succ = ntfs_ie_get_next(icx->entry); + entry_pos = icx->parent_pos[icx->pindex]++; + pindex = icx->pindex; +descend: + vcn = ntfs_ie_get_vcn(ie_succ); + if (ntfs_ib_read(icx, vcn, ib)) + goto out; + + ie_succ = ntfs_ie_get_first(&ib->index); + + if (ntfs_icx_parent_inc(icx)) + goto out; + + icx->parent_vcn[icx->pindex] = vcn; + icx->parent_pos[icx->pindex] = 0; + + if ((ib->index.ih_flags & NODE_MASK) == INDEX_NODE) + goto descend; + + if (ntfs_ih_zero_entry(&ib->index)) { + errno = EIO; + ntfs_log_perror("Empty index block"); + goto out; + } + + ie = ntfs_ie_dup(ie_succ); + if (!ie) + goto out; + + if (ntfs_ie_add_vcn(&ie)) + goto out2; + + ntfs_ie_set_vcn(ie, ntfs_ie_get_vcn(icx->entry)); + + if (icx->is_in_root) + ih = &icx->ir->index; + else + ih = &icx->ib->index; + + delta = le16_to_cpu(ie->length) - le16_to_cpu(icx->entry->length); + new_size = le32_to_cpu(ih->index_length) + delta; + if (delta > 0) { + if (icx->is_in_root) { + ret = ntfs_ir_make_space(icx, new_size); + if (ret != STATUS_OK) + goto out2; + + ih = &icx->ir->index; + entry = ntfs_ie_get_by_pos(ih, entry_pos); + + } else if (new_size > le32_to_cpu(ih->allocated_size)) { + icx->pindex = pindex; + ret = ntfs_ib_split(icx, icx->ib); + if (ret == STATUS_OK) + ret = STATUS_KEEP_SEARCHING; + goto out2; + } + } + + ntfs_ie_delete(ih, entry); + ntfs_ie_insert(ih, ie, entry); + + if (icx->is_in_root) { + if (ntfs_ir_truncate(icx, new_size)) + goto out2; + } else + if (ntfs_icx_ib_write(icx)) + goto out2; + + ntfs_ie_delete(&ib->index, ie_succ); + + if (ntfs_ih_zero_entry(&ib->index)) { + if (ntfs_index_rm_leaf(icx)) + goto out2; + } else + if (ntfs_ib_write(icx, ib)) + goto out2; + + ret = STATUS_OK; +out2: + free(ie); +out: + free(ib); + return ret; +} + +/** + * ntfs_index_rm - remove entry from the index + * @icx: index context describing entry to delete + * + * Delete entry described by @icx from the index. Index context is always + * reinitialized after use of this function, so it can be used for index + * lookup once again. + * + * Return 0 on success or -1 on error with errno set to the error code. + */ +/*static JPA*/ +int ntfs_index_rm(ntfs_index_context *icx) +{ + INDEX_HEADER *ih; + int err, ret = STATUS_OK; + + ntfs_log_trace("Entering\n"); + + if (!icx || (!icx->ib && !icx->ir) || ntfs_ie_end(icx->entry)) { + ntfs_log_error("Invalid arguments.\n"); + errno = EINVAL; + goto err_out; + } + if (icx->is_in_root) + ih = &icx->ir->index; + else + ih = &icx->ib->index; + + if (icx->entry->ie_flags & INDEX_ENTRY_NODE) { + + ret = ntfs_index_rm_node(icx); + + } else if (icx->is_in_root || !ntfs_ih_one_entry(ih)) { + + ntfs_ie_delete(ih, icx->entry); + + if (icx->is_in_root) { + err = ntfs_ir_truncate(icx, le32_to_cpu(ih->index_length)); + if (err != STATUS_OK) + goto err_out; + } else + if (ntfs_icx_ib_write(icx)) + goto err_out; + } else { + if (ntfs_index_rm_leaf(icx)) + goto err_out; + } +out: + return ret; +err_out: + ret = STATUS_ERROR; + goto out; +} + +int ntfs_index_remove(ntfs_inode *dir_ni, + ntfs_inode *ni __attribute__((unused)), + const void *key, const int keylen) +{ + int ret = STATUS_ERROR; + ntfs_index_context *icx; + + icx = ntfs_index_ctx_get(dir_ni, NTFS_INDEX_I30, 4); + if (!icx) + return -1; + + while (1) { + + if (ntfs_index_lookup(key, keylen, icx)) + goto err_out; + + ret = ntfs_index_rm(icx); + if (ret == STATUS_ERROR) + goto err_out; + else if (ret == STATUS_OK) + break; + + ntfs_inode_mark_dirty(icx->actx->ntfs_ino); + ntfs_index_ctx_reinit(icx); + } + + ntfs_inode_mark_dirty(icx->actx->ntfs_ino); +out: + ntfs_index_ctx_put(icx); + return ret; +err_out: + ret = STATUS_ERROR; + ntfs_log_perror("Delete failed"); + goto out; +} + +/** + * ntfs_index_root_get - read the index root of an attribute + * @ni: open ntfs inode in which the ntfs attribute resides + * @attr: attribute for which we want its index root + * + * This function will read the related index root an ntfs attribute. + * + * On success a buffer is allocated with the content of the index root + * and which needs to be freed when it's not needed anymore. + * + * On error NULL is returned with errno set to the error code. + */ +INDEX_ROOT *ntfs_index_root_get(ntfs_inode *ni, ATTR_RECORD *attr) +{ + ntfs_attr_search_ctx *ctx; + ntfschar *name; + INDEX_ROOT *root = NULL; + + name = (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset)); + + if (!ntfs_ir_lookup(ni, name, attr->name_length, &ctx)) + return NULL; + + root = ntfs_malloc(sizeof(INDEX_ROOT)); + if (!root) + goto out; + + *root = *((INDEX_ROOT *)((u8 *)ctx->attr + + le16_to_cpu(ctx->attr->value_offset))); +out: + ntfs_attr_put_search_ctx(ctx); + return root; +} + + +/* + * Walk down the index tree (leaf bound) + * until there are no subnode in the first index entry + * returns the entry at the bottom left in subnode + */ + +static INDEX_ENTRY *ntfs_index_walk_down(INDEX_ENTRY *ie, + ntfs_index_context *ictx) +{ + INDEX_ENTRY *entry; + s64 vcn; + + entry = ie; + do { + vcn = ntfs_ie_get_vcn(entry); + if (ictx->is_in_root) { + + /* down from level zero */ + + ictx->ir = (INDEX_ROOT*)NULL; + ictx->ib = (INDEX_BLOCK*)ntfs_malloc(ictx->block_size); + ictx->pindex = 1; + ictx->is_in_root = FALSE; + } else { + + /* down from non-zero level */ + + ictx->pindex++; + if (ictx->pindex >= MAX_PARENT_VCN) { + errno = EOPNOTSUPP; + ntfs_log_perror("Index is over %d level deep", + MAX_PARENT_VCN); + entry = (INDEX_ENTRY*)NULL; + break; + } + } + ictx->parent_pos[ictx->pindex] = 0; + ictx->parent_vcn[ictx->pindex] = vcn; + if (!ntfs_ib_read(ictx,vcn,ictx->ib)) { + ictx->entry = ntfs_ie_get_first(&ictx->ib->index); + entry = ictx->entry; + } else + entry = (INDEX_ENTRY*)NULL; + } while (entry && (entry->ie_flags & INDEX_ENTRY_NODE)); + return (entry); +} + +/* + * Walk up the index tree (root bound) + * until there is a valid data entry in parent + * returns the parent entry or NULL if no more parent + */ + +static INDEX_ENTRY *ntfs_index_walk_up(INDEX_ENTRY *ie, + ntfs_index_context *ictx) +{ + INDEX_ENTRY *entry; + s64 vcn; + + entry = ie; + if (ictx->pindex > 0) { + do { + ictx->pindex--; + if (!ictx->pindex) { + + /* we have reached the root */ + + free(ictx->ib); + ictx->ib = (INDEX_BLOCK*)NULL; + ictx->is_in_root = TRUE; + /* a new search context is to be allocated */ + if (ictx->actx) + free(ictx->actx); + ictx->ir = ntfs_ir_lookup(ictx->ni, + ictx->name, ictx->name_len, + &ictx->actx); + if (ictx->ir) + entry = ntfs_ie_get_by_pos( + &ictx->ir->index, + ictx->parent_pos[ictx->pindex]); + else + entry = (INDEX_ENTRY*)NULL; + } else { + /* up into non-root node */ + vcn = ictx->parent_vcn[ictx->pindex]; + if (!ntfs_ib_read(ictx,vcn,ictx->ib)) { + entry = ntfs_ie_get_by_pos( + &ictx->ib->index, + ictx->parent_pos[ictx->pindex]); + } else + entry = (INDEX_ENTRY*)NULL; + } + ictx->entry = entry; + } while (entry && (ictx->pindex > 0) + && (entry->ie_flags & INDEX_ENTRY_END)); + } else + entry = (INDEX_ENTRY*)NULL; + return (entry); +} + +/* + * Get next entry in an index according to collating sequence. + * Must be initialized through a ntfs_index_lookup() + * + * Returns next entry or NULL if none + * + * Sample layout : + * + * +---+---+---+---+---+---+---+---+ n ptrs to subnodes + * | | | 10| 25| 33| | | | n-1 keys in between + * +---+---+---+---+---+---+---+---+ no key in last entry + * | A | A + * | | | +-------------------------------+ + * +--------------------------+ | +-----+ | + * | +--+ | | + * V | V | + * +---+---+---+---+---+---+---+---+ | +---+---+---+---+---+---+---+---+ + * | 11| 12| 13| 14| 15| 16| 17| | | | 26| 27| 28| 29| 30| 31| 32| | + * +---+---+---+---+---+---+---+---+ | +---+---+---+---+---+---+---+---+ + * | | + * +-----------------------+ | + * | | + * +---+---+---+---+---+---+---+---+ + * | 18| 19| 20| 21| 22| 23| 24| | + * +---+---+---+---+---+---+---+---+ + */ + +INDEX_ENTRY *ntfs_index_next(INDEX_ENTRY *ie, ntfs_index_context *ictx) +{ + INDEX_ENTRY *next; + le16 flags; + + /* + * lookup() may have returned an invalid node + * when searching for a partial key + * if this happens, walk up + */ + + if (ie->ie_flags & INDEX_ENTRY_END) + next = ntfs_index_walk_up(ie, ictx); + else { + /* + * get next entry in same node + * there is always one after any entry with data + */ + + next = (INDEX_ENTRY*)((char*)ie + le16_to_cpu(ie->length)); + ++ictx->parent_pos[ictx->pindex]; + flags = next->ie_flags; + + /* walk down if it has a subnode */ + + if (flags & INDEX_ENTRY_NODE) { + next = ntfs_index_walk_down(next,ictx); + } else { + + /* walk up it has no subnode, nor data */ + + if (flags & INDEX_ENTRY_END) { + next = ntfs_index_walk_up(next, ictx); + } + } + } + /* return NULL if stuck at end of a block */ + + if (next && (next->ie_flags & INDEX_ENTRY_END)) + next = (INDEX_ENTRY*)NULL; + return (next); +} + + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/inode.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/inode.c new file mode 100644 index 0000000..dc17a4c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/inode.c @@ -0,0 +1,1610 @@ +/** + * inode.c - Inode handling code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002-2005 Anton Altaparmakov + * Copyright (c) 2002-2008 Szabolcs Szakacsits + * Copyright (c) 2004-2007 Yura Pakhuchiy + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2009-2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "param.h" +#include "compat.h" +#include "types.h" +#include "volume.h" +#include "cache.h" +#include "inode.h" +#include "attrib.h" +#include "debug.h" +#include "mft.h" +#include "attrlist.h" +#include "runlist.h" +#include "lcnalloc.h" +#include "index.h" +#include "dir.h" +#include "ntfstime.h" +#include "logging.h" +#include "misc.h" +#include "xattrs.h" + +ntfs_inode *ntfs_inode_base(ntfs_inode *ni) +{ + if (ni->nr_extents == -1) + return ni->base_ni; + return ni; +} + +/** + * ntfs_inode_mark_dirty - set the inode (and its base inode if it exists) dirty + * @ni: ntfs inode to set dirty + * + * Set the inode @ni dirty so it is written out later (at the latest at + * ntfs_inode_close() time). If @ni is an extent inode, set the base inode + * dirty, too. + * + * This function cannot fail. + */ +void ntfs_inode_mark_dirty(ntfs_inode *ni) +{ + NInoSetDirty(ni); + if (ni->nr_extents == -1) + NInoSetDirty(ni->base_ni); +} + +/** + * __ntfs_inode_allocate - Create and initialise an NTFS inode object + * @vol: + * + * Description... + * + * Returns: + */ +static ntfs_inode *__ntfs_inode_allocate(ntfs_volume *vol) +{ + ntfs_inode *ni; + + ni = (ntfs_inode*)ntfs_calloc(sizeof(ntfs_inode)); + if (ni) + ni->vol = vol; + return ni; +} + +/** + * ntfs_inode_allocate - Create an NTFS inode object + * @vol: + * + * Description... + * + * Returns: + */ +ntfs_inode *ntfs_inode_allocate(ntfs_volume *vol) +{ + return __ntfs_inode_allocate(vol); +} + +/** + * __ntfs_inode_release - Destroy an NTFS inode object + * @ni: + * + * Description... + * + * Returns: + */ +static void __ntfs_inode_release(ntfs_inode *ni) +{ + if (NInoDirty(ni)) + ntfs_log_error("Releasing dirty inode %lld!\n", + (long long)ni->mft_no); + if (NInoAttrList(ni) && ni->attr_list) + free(ni->attr_list); + free(ni->mrec); + free(ni); + return; +} + +/** + * ntfs_inode_open - open an inode ready for access + * @vol: volume to get the inode from + * @mref: inode number / mft record number to open + * + * Allocate an ntfs_inode structure and initialize it for the given inode + * specified by @mref. @mref specifies the inode number / mft record to read, + * including the sequence number, which can be 0 if no sequence number checking + * is to be performed. + * + * Then, allocate a buffer for the mft record, read the mft record from the + * volume @vol, and attach it to the ntfs_inode structure (->mrec). The + * mft record is mst deprotected and sanity checked for validity and we abort + * if deprotection or checks fail. + * + * Finally, search for an attribute list attribute in the mft record and if one + * is found, load the attribute list attribute value and attach it to the + * ntfs_inode structure (->attr_list). Also set the NI_AttrList bit to indicate + * this. + * + * Return a pointer to the ntfs_inode structure on success or NULL on error, + * with errno set to the error code. + */ +static ntfs_inode *ntfs_inode_real_open(ntfs_volume *vol, const MFT_REF mref) +{ + s64 l; + ntfs_inode *ni = NULL; + ntfs_attr_search_ctx *ctx; + STANDARD_INFORMATION *std_info; + le32 lthle; + int olderrno; + + ntfs_log_enter("Entering for inode %lld\n", (long long)MREF(mref)); + if (!vol) { + errno = EINVAL; + goto out; + } + ni = __ntfs_inode_allocate(vol); + if (!ni) + goto out; + if (ntfs_file_record_read(vol, mref, &ni->mrec, NULL)) + goto err_out; + if (!(ni->mrec->flags & MFT_RECORD_IN_USE)) { + errno = ENOENT; + goto err_out; + } + ni->mft_no = MREF(mref); + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + goto err_out; + /* Receive some basic information about inode. */ + if (ntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED, + 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { + if (!ni->mrec->base_mft_record) + ntfs_log_perror("No STANDARD_INFORMATION in base record" + " %lld", (long long)MREF(mref)); + goto put_err_out; + } + lthle = ctx->attr->value_length; + if (le32_to_cpu(lthle) < offsetof(STANDARD_INFORMATION, owner_id)) { + ntfs_log_error("Corrupt STANDARD_INFORMATION in base" + " record %lld\n", + (long long)MREF(mref)); + goto put_err_out; + } + std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + ni->flags = std_info->file_attributes; + ni->creation_time = std_info->creation_time; + ni->last_data_change_time = std_info->last_data_change_time; + ni->last_mft_change_time = std_info->last_mft_change_time; + ni->last_access_time = std_info->last_access_time; + /* Insert v3 extensions if present */ + /* length may be seen as 48 (v1.x) or 72 (v3.x) */ + if (le32_to_cpu(lthle) >= offsetof(STANDARD_INFORMATION, v3_end)) { + set_nino_flag(ni, v3_Extensions); + ni->owner_id = std_info->owner_id; + ni->security_id = std_info->security_id; + ni->quota_charged = std_info->quota_charged; + ni->usn = std_info->usn; + } else { + clear_nino_flag(ni, v3_Extensions); + ni->owner_id = const_cpu_to_le32(0); + ni->security_id = const_cpu_to_le32(0); + } + /* Set attribute list information. */ + olderrno = errno; + if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, AT_UNNAMED, 0, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + if (errno != ENOENT) + goto put_err_out; + /* Attribute list attribute does not present. */ + /* restore previous errno to avoid misinterpretation */ + errno = olderrno; + goto get_size; + } + NInoSetAttrList(ni); + l = ntfs_get_attribute_value_length(ctx->attr); + if (!l) + goto put_err_out; + if ((u64)l > 0x40000) { + errno = EIO; + ntfs_log_perror("Too large attrlist attribute (%llu), inode " + "%lld", (long long)l, (long long)MREF(mref)); + goto put_err_out; + } + ni->attr_list_size = l; + ni->attr_list = ntfs_malloc(ni->attr_list_size); + if (!ni->attr_list) + goto put_err_out; + l = ntfs_get_attribute_value(vol, ctx->attr, ni->attr_list); + if (!l) + goto put_err_out; + if (l != ni->attr_list_size) { + errno = EIO; + ntfs_log_perror("Unexpected attrlist size (%lld <> %u), inode " + "%lld", (long long)l, ni->attr_list_size, + (long long)MREF(mref)); + goto put_err_out; + } +get_size: + olderrno = errno; + if (ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, NULL, 0, ctx)) { + if (errno != ENOENT) + goto put_err_out; + /* Directory or special file. */ + /* restore previous errno to avoid misinterpretation */ + errno = olderrno; + ni->data_size = ni->allocated_size = 0; + } else { + if (ctx->attr->non_resident) { + ni->data_size = sle64_to_cpu(ctx->attr->data_size); + if (ctx->attr->flags & + (ATTR_IS_COMPRESSED | ATTR_IS_SPARSE)) + ni->allocated_size = sle64_to_cpu( + ctx->attr->compressed_size); + else + ni->allocated_size = sle64_to_cpu( + ctx->attr->allocated_size); + } else { + ni->data_size = le32_to_cpu(ctx->attr->value_length); + ni->allocated_size = (ni->data_size + 7) & ~7; + } + set_nino_flag(ni,KnownSize); + } + ntfs_attr_put_search_ctx(ctx); +out: + ntfs_log_leave("\n"); + return ni; + +put_err_out: + ntfs_attr_put_search_ctx(ctx); +err_out: + __ntfs_inode_release(ni); + ni = NULL; + goto out; +} + +/** + * ntfs_inode_close - close an ntfs inode and free all associated memory + * @ni: ntfs inode to close + * + * Make sure the ntfs inode @ni is clean. + * + * If the ntfs inode @ni is a base inode, close all associated extent inodes, + * then deallocate all memory attached to it, and finally free the ntfs inode + * structure itself. + * + * If it is an extent inode, we disconnect it from its base inode before we + * destroy it. + * + * It is OK to pass NULL to this function, it is just noop in this case. + * + * Return 0 on success or -1 on error with errno set to the error code. On + * error, @ni has not been freed. The user should attempt to handle the error + * and call ntfs_inode_close() again. The following error codes are defined: + * + * EBUSY @ni and/or its attribute list runlist is/are dirty and the + * attempt to write it/them to disk failed. + * EINVAL @ni is invalid (probably it is an extent inode). + * EIO I/O error while trying to write inode to disk. + */ + +int ntfs_inode_real_close(ntfs_inode *ni) +{ + int ret = -1; + + if (!ni) + return 0; + + ntfs_log_enter("Entering for inode %lld\n", (long long)ni->mft_no); + + /* If we have dirty metadata, write it out. */ + if (NInoDirty(ni) || NInoAttrListDirty(ni)) { + if (ntfs_inode_sync(ni)) { + if (errno != EIO) + errno = EBUSY; + goto err; + } + } + /* Is this a base inode with mapped extent inodes? */ + if (ni->nr_extents > 0) { + while (ni->nr_extents > 0) { + if (ntfs_inode_real_close(ni->extent_nis[0])) { + if (errno != EIO) + errno = EBUSY; + goto err; + } + } + } else if (ni->nr_extents == -1) { + ntfs_inode **tmp_nis; + ntfs_inode *base_ni; + s32 i; + + /* + * If the inode is an extent inode, disconnect it from the + * base inode before destroying it. + */ + base_ni = ni->base_ni; + for (i = 0; i < base_ni->nr_extents; ++i) { + tmp_nis = base_ni->extent_nis; + if (tmp_nis[i] != ni) + continue; + /* Found it. Disconnect. */ + memmove(tmp_nis + i, tmp_nis + i + 1, + (base_ni->nr_extents - i - 1) * + sizeof(ntfs_inode *)); + /* Buffer should be for multiple of four extents. */ + if ((--base_ni->nr_extents) & 3) { + i = -1; + break; + } + /* + * ElectricFence is unhappy with realloc(x,0) as free(x) + * thus we explicitly separate these two cases. + */ + if (base_ni->nr_extents) { + /* Resize the memory buffer. */ + tmp_nis = realloc(tmp_nis, base_ni->nr_extents * + sizeof(ntfs_inode *)); + /* Ignore errors, they don't really matter. */ + if (tmp_nis) + base_ni->extent_nis = tmp_nis; + } else if (tmp_nis) { + free(tmp_nis); + base_ni->extent_nis = (ntfs_inode**)NULL; + } + /* Allow for error checking. */ + i = -1; + break; + } + + /* + * We could successfully sync, so only log this error + * and try to sync other inode extents too. + */ + if (i != -1) + ntfs_log_error("Extent inode %lld was not found\n", + (long long)ni->mft_no); + } + + __ntfs_inode_release(ni); + ret = 0; +err: + ntfs_log_leave("\n"); + return ret; +} + +#if CACHE_NIDATA_SIZE + +/* + * Free an inode structure when there is not more space + * in the cache + */ + +void ntfs_inode_nidata_free(const struct CACHED_GENERIC *cached) +{ + ntfs_inode_real_close(((const struct CACHED_NIDATA*)cached)->ni); +} + +/* + * Compute a hash value for an inode entry + */ + +int ntfs_inode_nidata_hash(const struct CACHED_GENERIC *item) +{ + return (((const struct CACHED_NIDATA*)item)->inum + % (2*CACHE_NIDATA_SIZE)); +} + +/* + * inum comparing for entering/fetching from cache + */ + +static int idata_cache_compare(const struct CACHED_GENERIC *cached, + const struct CACHED_GENERIC *wanted) +{ + return (((const struct CACHED_NIDATA*)cached)->inum + != ((const struct CACHED_NIDATA*)wanted)->inum); +} + +/* + * Invalidate an inode entry when not needed anymore. + * The entry should have been synced, it may be reused later, + * if it is requested before it is dropped from cache. + */ + +void ntfs_inode_invalidate(ntfs_volume *vol, const MFT_REF mref) +{ + struct CACHED_NIDATA item; + + item.inum = MREF(mref); + item.ni = (ntfs_inode*)NULL; + item.pathname = (const char*)NULL; + item.varsize = 0; + ntfs_invalidate_cache(vol->nidata_cache, + GENERIC(&item),idata_cache_compare,CACHE_FREE); +} + +#endif + +/* + * Open an inode + * + * When possible, an entry recorded in the cache is reused + * + * **NEVER REOPEN** an inode, this can lead to a duplicated + * cache entry (hard to detect), and to an obsolete one being + * reused. System files are however protected from being cached. + */ + +ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref) +{ + ntfs_inode *ni; +#if CACHE_NIDATA_SIZE + struct CACHED_NIDATA item; + struct CACHED_NIDATA *cached; + + /* fetch idata from cache */ + item.inum = MREF(mref); + debug_double_inode(item.inum,1); + item.pathname = (const char*)NULL; + item.varsize = 0; + cached = (struct CACHED_NIDATA*)ntfs_fetch_cache(vol->nidata_cache, + GENERIC(&item),idata_cache_compare); + if (cached) { + ni = cached->ni; + /* do not keep open entries in cache */ + ntfs_remove_cache(vol->nidata_cache, + (struct CACHED_GENERIC*)cached,0); + } else { + ni = ntfs_inode_real_open(vol, mref); + } + if (!ni) { + debug_double_inode(item.inum, 0); + } +#else + ni = ntfs_inode_real_open(vol, mref); +#endif + return (ni); +} + +/* + * Close an inode entry + * + * If cacheing is in use, the entry is synced and kept available + * in cache for further use. + * + * System files (inode < 16 or having the IS_4 flag) are protected + * against being cached. + */ + +int ntfs_inode_close(ntfs_inode *ni) +{ + int res; +#if CACHE_NIDATA_SIZE + BOOL dirty; + struct CACHED_NIDATA item; + + if (ni) { + debug_double_inode(ni->mft_no,0); + /* do not cache system files : could lead to double entries */ + if (ni->vol && ni->vol->nidata_cache + && ((ni->mft_no == FILE_root) + || ((ni->mft_no >= FILE_first_user) + && !(ni->mrec->flags & MFT_RECORD_IS_4)))) { + /* If we have dirty metadata, write it out. */ + dirty = NInoDirty(ni) || NInoAttrListDirty(ni); + if (dirty) { + res = ntfs_inode_sync(ni); + /* do a real close if sync failed */ + if (res) + ntfs_inode_real_close(ni); + } else + res = 0; + + if (!res) { + /* feed idata into cache */ + item.inum = ni->mft_no; + item.ni = ni; + item.pathname = (const char*)NULL; + item.varsize = 0; + debug_cached_inode(ni); + ntfs_enter_cache(ni->vol->nidata_cache, + GENERIC(&item), idata_cache_compare); + } + } else { + /* cache not ready or system file, really close */ + res = ntfs_inode_real_close(ni); + } + } else + res = 0; +#else + res = ntfs_inode_real_close(ni); +#endif + return (res); +} + +/** + * ntfs_extent_inode_open - load an extent inode and attach it to its base + * @base_ni: base ntfs inode + * @mref: mft reference of the extent inode to load (in little endian) + * + * First check if the extent inode @mref is already attached to the base ntfs + * inode @base_ni, and if so, return a pointer to the attached extent inode. + * + * If the extent inode is not already attached to the base inode, allocate an + * ntfs_inode structure and initialize it for the given inode @mref. @mref + * specifies the inode number / mft record to read, including the sequence + * number, which can be 0 if no sequence number checking is to be performed. + * + * Then, allocate a buffer for the mft record, read the mft record from the + * volume @base_ni->vol, and attach it to the ntfs_inode structure (->mrec). + * The mft record is mst deprotected and sanity checked for validity and we + * abort if deprotection or checks fail. + * + * Finally attach the ntfs inode to its base inode @base_ni and return a + * pointer to the ntfs_inode structure on success or NULL on error, with errno + * set to the error code. + * + * Note, extent inodes are never closed directly. They are automatically + * disposed off by the closing of the base inode. + */ +ntfs_inode *ntfs_extent_inode_open(ntfs_inode *base_ni, const leMFT_REF mref) +{ + u64 mft_no = MREF_LE(mref); + VCN extent_vcn; + runlist_element *rl; + ntfs_volume *vol; + ntfs_inode *ni = NULL; + ntfs_inode **extent_nis; + int i; + + if (!base_ni) { + errno = EINVAL; + ntfs_log_perror("%s", __FUNCTION__); + return NULL; + } + + ntfs_log_enter("Opening extent inode %lld (base mft record %lld).\n", + (unsigned long long)mft_no, + (unsigned long long)base_ni->mft_no); + + if (!base_ni->mft_no) { + /* + * When getting extents of MFT, we must be sure + * they are in the MFT part which has already + * been mapped, otherwise we fall into an endless + * recursion. + * Situations have been met where extents locations + * are described in themselves. + * This is a severe error which chkdsk cannot fix. + */ + vol = base_ni->vol; + extent_vcn = mft_no << vol->mft_record_size_bits + >> vol->cluster_size_bits; + rl = vol->mft_na->rl; + if (rl) { + while (rl->length + && ((rl->vcn + rl->length) <= extent_vcn)) + rl++; + } + if (!rl || (rl->lcn < 0)) { + ntfs_log_error("MFT is corrupt, cannot read" + " its unmapped extent record %lld\n", + (long long)mft_no); + ntfs_log_error("Note : chkdsk cannot fix this," + " try ntfsfix\n"); + errno = EIO; + ni = (ntfs_inode*)NULL; + goto out; + } + } + + /* Is the extent inode already open and attached to the base inode? */ + if (base_ni->nr_extents > 0) { + extent_nis = base_ni->extent_nis; + for (i = 0; i < base_ni->nr_extents; i++) { + u16 seq_no; + + ni = extent_nis[i]; + if (mft_no != ni->mft_no) + continue; + /* Verify the sequence number if given. */ + seq_no = MSEQNO_LE(mref); + if (seq_no && seq_no != le16_to_cpu( + ni->mrec->sequence_number)) { + errno = EIO; + ntfs_log_perror("Found stale extent mft " + "reference mft=%lld", + (long long)ni->mft_no); + goto out; + } + goto out; + } + } + /* Wasn't there, we need to load the extent inode. */ + ni = __ntfs_inode_allocate(base_ni->vol); + if (!ni) + goto out; + if (ntfs_file_record_read(base_ni->vol, le64_to_cpu(mref), &ni->mrec, NULL)) + goto err_out; + ni->mft_no = mft_no; + ni->nr_extents = -1; + ni->base_ni = base_ni; + /* Attach extent inode to base inode, reallocating memory if needed. */ + if (!(base_ni->nr_extents & 3)) { + i = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *); + + extent_nis = ntfs_malloc(i); + if (!extent_nis) + goto err_out; + if (base_ni->nr_extents) { + memcpy(extent_nis, base_ni->extent_nis, + i - 4 * sizeof(ntfs_inode *)); + free(base_ni->extent_nis); + } + base_ni->extent_nis = extent_nis; + } + base_ni->extent_nis[base_ni->nr_extents++] = ni; +out: + ntfs_log_leave("\n"); + return ni; +err_out: + __ntfs_inode_release(ni); + ni = NULL; + goto out; +} + +/** + * ntfs_inode_attach_all_extents - attach all extents for target inode + * @ni: opened ntfs inode for which perform attach + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +int ntfs_inode_attach_all_extents(ntfs_inode *ni) +{ + ATTR_LIST_ENTRY *ale; + u64 prev_attached = 0; + + if (!ni) { + ntfs_log_trace("Invalid arguments.\n"); + errno = EINVAL; + return -1; + } + + if (ni->nr_extents == -1) + ni = ni->base_ni; + + ntfs_log_trace("Entering for inode 0x%llx.\n", (long long) ni->mft_no); + + /* Inode haven't got attribute list, thus nothing to attach. */ + if (!NInoAttrList(ni)) + return 0; + + if (!ni->attr_list) { + ntfs_log_trace("Corrupt in-memory struct.\n"); + errno = EINVAL; + return -1; + } + + /* Walk through attribute list and attach all extents. */ + errno = 0; + ale = (ATTR_LIST_ENTRY *)ni->attr_list; + while ((u8*)ale < ni->attr_list + ni->attr_list_size) { + if (ni->mft_no != MREF_LE(ale->mft_reference) && + prev_attached != MREF_LE(ale->mft_reference)) { + if (!ntfs_extent_inode_open(ni, ale->mft_reference)) { + ntfs_log_trace("Couldn't attach extent inode.\n"); + return -1; + } + prev_attached = MREF_LE(ale->mft_reference); + } + ale = (ATTR_LIST_ENTRY *)((u8*)ale + le16_to_cpu(ale->length)); + } + return 0; +} + +/** + * ntfs_inode_sync_standard_information - update standard information attribute + * @ni: ntfs inode to update standard information + * + * Return 0 on success or -1 on error with errno set to the error code. + */ +static int ntfs_inode_sync_standard_information(ntfs_inode *ni) +{ + ntfs_attr_search_ctx *ctx; + STANDARD_INFORMATION *std_info; + u32 lth; + le32 lthle; + + ntfs_log_trace("Entering for inode %lld\n", (long long)ni->mft_no); + + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return -1; + if (ntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED, + 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { + ntfs_log_perror("Failed to sync standard info (inode %lld)", + (long long)ni->mft_no); + ntfs_attr_put_search_ctx(ctx); + return -1; + } + std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + std_info->file_attributes = ni->flags; + if (!test_nino_flag(ni, TimesSet)) { + std_info->creation_time = ni->creation_time; + std_info->last_data_change_time = ni->last_data_change_time; + std_info->last_mft_change_time = ni->last_mft_change_time; + std_info->last_access_time = ni->last_access_time; + } + + /* JPA update v3.x extensions, ensuring consistency */ + + lthle = ctx->attr->value_length; + lth = le32_to_cpu(lthle); + if (test_nino_flag(ni, v3_Extensions) + && (lth < offsetof(STANDARD_INFORMATION, v3_end))) + ntfs_log_error("bad sync of standard information\n"); + + if (lth >= offsetof(STANDARD_INFORMATION, v3_end)) { + std_info->owner_id = ni->owner_id; + std_info->security_id = ni->security_id; + std_info->quota_charged = ni->quota_charged; + std_info->usn = ni->usn; + } + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_put_search_ctx(ctx); + return 0; +} + +/** + * ntfs_inode_sync_file_name - update FILE_NAME attributes + * @ni: ntfs inode to update FILE_NAME attributes + * + * Update all FILE_NAME attributes for inode @ni in the index. + * + * Return 0 on success or -1 on error with errno set to the error code. + */ +static int ntfs_inode_sync_file_name(ntfs_inode *ni, ntfs_inode *dir_ni) +{ + ntfs_attr_search_ctx *ctx = NULL; + ntfs_index_context *ictx; + ntfs_inode *index_ni; + FILE_NAME_ATTR *fn; + FILE_NAME_ATTR *fnx; + REPARSE_POINT *rpp; + le32 reparse_tag; + int err = 0; + + ntfs_log_trace("Entering for inode %lld\n", (long long)ni->mft_no); + + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) { + err = errno; + goto err_out; + } + /* Collect the reparse tag, if any */ + reparse_tag = const_cpu_to_le32(0); + if (ni->flags & FILE_ATTR_REPARSE_POINT) { + if (!ntfs_attr_lookup(AT_REPARSE_POINT, NULL, + 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { + rpp = (REPARSE_POINT*)((u8 *)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + reparse_tag = rpp->reparse_tag; + } + ntfs_attr_reinit_search_ctx(ctx); + } + /* Walk through all FILE_NAME attributes and update them. */ + while (!ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, 0, 0, NULL, 0, ctx)) { + fn = (FILE_NAME_ATTR *)((u8 *)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + if (MREF_LE(fn->parent_directory) == ni->mft_no) { + /* + * WARNING: We cheat here and obtain 2 attribute + * search contexts for one inode (first we obtained + * above, second will be obtained inside + * ntfs_index_lookup), it's acceptable for library, + * but will deadlock in the kernel. + */ + index_ni = ni; + } else + if (dir_ni) + index_ni = dir_ni; + else + index_ni = ntfs_inode_open(ni->vol, + le64_to_cpu(fn->parent_directory)); + if (!index_ni) { + if (!err) + err = errno; + ntfs_log_perror("Failed to open inode %lld with index", + (long long)MREF_LE(fn->parent_directory)); + continue; + } + ictx = ntfs_index_ctx_get(index_ni, NTFS_INDEX_I30, 4); + if (!ictx) { + if (!err) + err = errno; + ntfs_log_perror("Failed to get index ctx, inode %lld", + (long long)index_ni->mft_no); + if ((ni != index_ni) && !dir_ni + && ntfs_inode_close(index_ni) && !err) + err = errno; + continue; + } + if (ntfs_index_lookup(fn, offsetof(FILE_NAME_ATTR, file_name), ictx)) { + if (!err) { + if (errno == ENOENT) + err = EIO; + else + err = errno; + } + ntfs_log_perror("Index lookup failed, inode %lld", + (long long)index_ni->mft_no); + ntfs_index_ctx_put(ictx); + if (ni != index_ni && ntfs_inode_close(index_ni) && !err) + err = errno; + continue; + } + /* Update flags and file size. */ + fnx = (FILE_NAME_ATTR *)ictx->data; + fnx->file_attributes = + (fnx->file_attributes & ~FILE_ATTR_VALID_FLAGS) | + (ni->flags & FILE_ATTR_VALID_FLAGS); + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + fnx->data_size = fnx->allocated_size + = const_cpu_to_sle64(0); + else { + fnx->allocated_size = cpu_to_sle64(ni->allocated_size); + fnx->data_size = cpu_to_sle64(ni->data_size); + /* + * The file name record has also to be fixed if some + * attribute update implied the unnamed data to be + * made non-resident + */ + fn->allocated_size = fnx->allocated_size; + } + /* update or clear the reparse tag in the index */ + fnx->reparse_point_tag = reparse_tag; + if (!test_nino_flag(ni, TimesSet)) { + fnx->creation_time = ni->creation_time; + fnx->last_data_change_time = ni->last_data_change_time; + fnx->last_mft_change_time = ni->last_mft_change_time; + fnx->last_access_time = ni->last_access_time; + } else { + fnx->creation_time = fn->creation_time; + fnx->last_data_change_time = fn->last_data_change_time; + fnx->last_mft_change_time = fn->last_mft_change_time; + fnx->last_access_time = fn->last_access_time; + } + ntfs_index_entry_mark_dirty(ictx); + ntfs_index_ctx_put(ictx); + if ((ni != index_ni) && !dir_ni + && ntfs_inode_close(index_ni) && !err) + err = errno; + } + /* Check for real error occurred. */ + if (errno != ENOENT) { + err = errno; + ntfs_log_perror("Attribute lookup failed, inode %lld", + (long long)ni->mft_no); + goto err_out; + } + ntfs_attr_put_search_ctx(ctx); + if (err) { + errno = err; + return -1; + } + return 0; +err_out: + if (ctx) + ntfs_attr_put_search_ctx(ctx); + errno = err; + return -1; +} + +/** + * ntfs_inode_sync - write the inode (and its dirty extents) to disk + * @ni: ntfs inode to write + * + * Write the inode @ni to disk as well as its dirty extent inodes if such + * exist and @ni is a base inode. If @ni is an extent inode, only @ni is + * written completely disregarding its base inode and any other extent inodes. + * + * For a base inode with dirty extent inodes if any writes fail for whatever + * reason, the failing inode is skipped and the sync process is continued. At + * the end the error condition that brought about the failure is returned. Thus + * the smallest amount of data loss possible occurs. + * + * Return 0 on success or -1 on error with errno set to the error code. + * The following error codes are defined: + * EINVAL - Invalid arguments were passed to the function. + * EBUSY - Inode and/or one of its extents is busy, try again later. + * EIO - I/O error while writing the inode (or one of its extents). + */ +static int ntfs_inode_sync_in_dir(ntfs_inode *ni, ntfs_inode *dir_ni) +{ + int ret = 0; + int err = 0; + if (!ni) { + errno = EINVAL; + ntfs_log_error("Failed to sync NULL inode\n"); + return -1; + } + + ntfs_log_enter("Entering for inode %lld\n", (long long)ni->mft_no); + + /* Update STANDARD_INFORMATION. */ + if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 && + ntfs_inode_sync_standard_information(ni)) { + if (!err || errno == EIO) { + err = errno; + if (err != EIO) + err = EBUSY; + } + } + + /* Update FILE_NAME's in the index. */ + if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 && + NInoFileNameTestAndClearDirty(ni) && + ntfs_inode_sync_file_name(ni, dir_ni)) { + if (!err || errno == EIO) { + err = errno; + if (err != EIO) + err = EBUSY; + } + ntfs_log_perror("Failed to sync FILE_NAME (inode %lld)", + (long long)ni->mft_no); + NInoFileNameSetDirty(ni); + } + + /* Write out attribute list from cache to disk. */ + if ((ni->mrec->flags & MFT_RECORD_IN_USE) && ni->nr_extents != -1 && + NInoAttrList(ni) && NInoAttrListTestAndClearDirty(ni)) { + ntfs_attr *na; + + na = ntfs_attr_open(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0); + if (!na) { + if (!err || errno == EIO) { + err = errno; + if (err != EIO) + err = EBUSY; + ntfs_log_perror("Attribute list sync failed " + "(open, inode %lld)", + (long long)ni->mft_no); + } + NInoAttrListSetDirty(ni); + goto sync_inode; + } + + if (na->data_size == ni->attr_list_size) { + if (ntfs_attr_pwrite(na, 0, ni->attr_list_size, + ni->attr_list) != ni->attr_list_size) { + if (!err || errno == EIO) { + err = errno; + if (err != EIO) + err = EBUSY; + ntfs_log_perror("Attribute list sync " + "failed (write, inode %lld)", + (long long)ni->mft_no); + } + NInoAttrListSetDirty(ni); + } + } else { + err = EIO; + ntfs_log_error("Attribute list sync failed (bad size, " + "inode %lld)\n", (long long)ni->mft_no); + NInoAttrListSetDirty(ni); + } + ntfs_attr_close(na); + } + +sync_inode: + /* Write this inode out to the $MFT (and $MFTMirr if applicable). */ + if (NInoTestAndClearDirty(ni)) { + if (ntfs_mft_record_write(ni->vol, ni->mft_no, ni->mrec)) { + if (!err || errno == EIO) { + err = errno; + if (err != EIO) + err = EBUSY; + } + NInoSetDirty(ni); + ntfs_log_perror("MFT record sync failed, inode %lld", + (long long)ni->mft_no); + } + } + + /* If this is a base inode with extents write all dirty extents, too. */ + if (ni->nr_extents > 0) { + s32 i; + + for (i = 0; i < ni->nr_extents; ++i) { + ntfs_inode *eni; + + eni = ni->extent_nis[i]; + if (!NInoTestAndClearDirty(eni)) + continue; + + if (ntfs_mft_record_write(eni->vol, eni->mft_no, + eni->mrec)) { + if (!err || errno == EIO) { + err = errno; + if (err != EIO) + err = EBUSY; + } + NInoSetDirty(eni); + ntfs_log_perror("Extent MFT record sync failed," + " inode %lld/%lld", + (long long)ni->mft_no, + (long long)eni->mft_no); + } + } + } + + if (err) { + errno = err; + ret = -1; + } + + ntfs_log_leave("\n"); + return ret; +} + +int ntfs_inode_sync(ntfs_inode *ni) +{ + return (ntfs_inode_sync_in_dir(ni, (ntfs_inode*)NULL)); +} + +/* + * Close an inode with an open parent inode + */ + +int ntfs_inode_close_in_dir(ntfs_inode *ni, ntfs_inode *dir_ni) +{ + int res; + + res = ntfs_inode_sync_in_dir(ni, dir_ni); + if (res) { + if (errno != EIO) + errno = EBUSY; + } else + res = ntfs_inode_close(ni); + return (res); +} + +/** + * ntfs_inode_add_attrlist - add attribute list to inode and fill it + * @ni: opened ntfs inode to which add attribute list + * + * Return 0 on success or -1 on error with errno set to the error code. + * The following error codes are defined: + * EINVAL - Invalid arguments were passed to the function. + * EEXIST - Attribute list already exist. + * EIO - Input/Ouput error occurred. + * ENOMEM - Not enough memory to perform add. + */ +int ntfs_inode_add_attrlist(ntfs_inode *ni) +{ + int err; + ntfs_attr_search_ctx *ctx; + u8 *al = NULL, *aln; + int al_len = 0; + ATTR_LIST_ENTRY *ale = NULL; + ntfs_attr *na; + + if (!ni) { + errno = EINVAL; + ntfs_log_perror("%s", __FUNCTION__); + return -1; + } + + ntfs_log_trace("inode %llu\n", (unsigned long long) ni->mft_no); + + if (NInoAttrList(ni) || ni->nr_extents) { + errno = EEXIST; + ntfs_log_perror("Inode already has attribute list"); + return -1; + } + + /* Form attribute list. */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) { + err = errno; + goto err_out; + } + /* Walk through all attributes. */ + while (!ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx)) { + + int ale_size; + + if (ctx->attr->type == AT_ATTRIBUTE_LIST) { + err = EIO; + ntfs_log_perror("Attribute list already present"); + goto put_err_out; + } + + ale_size = (offsetof(ATTR_LIST_ENTRY, name) + sizeof(ntfschar) * + ctx->attr->name_length + 7) & ~7; + al_len += ale_size; + + aln = realloc(al, al_len); + if (!aln) { + err = errno; + ntfs_log_perror("Failed to realloc %d bytes", al_len); + goto put_err_out; + } + ale = (ATTR_LIST_ENTRY *)(aln + ((u8 *)ale - al)); + al = aln; + + memset(ale, 0, ale_size); + + /* Add attribute to attribute list. */ + ale->type = ctx->attr->type; + ale->length = cpu_to_le16((offsetof(ATTR_LIST_ENTRY, name) + + sizeof(ntfschar) * ctx->attr->name_length + 7) & ~7); + ale->name_length = ctx->attr->name_length; + ale->name_offset = (u8 *)ale->name - (u8 *)ale; + if (ctx->attr->non_resident) + ale->lowest_vcn = ctx->attr->lowest_vcn; + else + ale->lowest_vcn = const_cpu_to_sle64(0); + ale->mft_reference = MK_LE_MREF(ni->mft_no, + le16_to_cpu(ni->mrec->sequence_number)); + ale->instance = ctx->attr->instance; + memcpy(ale->name, (u8 *)ctx->attr + + le16_to_cpu(ctx->attr->name_offset), + ctx->attr->name_length * sizeof(ntfschar)); + ale = (ATTR_LIST_ENTRY *)(al + al_len); + } + /* Check for real error occurred. */ + if (errno != ENOENT) { + err = errno; + ntfs_log_perror("%s: Attribute lookup failed, inode %lld", + __FUNCTION__, (long long)ni->mft_no); + goto put_err_out; + } + + /* Set in-memory attribute list. */ + ni->attr_list = al; + ni->attr_list_size = al_len; + NInoSetAttrList(ni); + NInoAttrListSetDirty(ni); + + /* Free space if there is not enough it for $ATTRIBUTE_LIST. */ + if (le32_to_cpu(ni->mrec->bytes_allocated) - + le32_to_cpu(ni->mrec->bytes_in_use) < + offsetof(ATTR_RECORD, resident_end)) { + if (ntfs_inode_free_space(ni, + offsetof(ATTR_RECORD, resident_end))) { + /* Failed to free space. */ + err = errno; + ntfs_log_perror("Failed to free space for attrlist"); + goto rollback; + } + } + + /* Add $ATTRIBUTE_LIST to mft record. */ + if (ntfs_resident_attr_record_add(ni, + AT_ATTRIBUTE_LIST, NULL, 0, NULL, 0, const_cpu_to_le16(0)) < 0) { + err = errno; + ntfs_log_perror("Couldn't add $ATTRIBUTE_LIST to MFT"); + goto rollback; + } + + /* Resize it. */ + na = ntfs_attr_open(ni, AT_ATTRIBUTE_LIST, AT_UNNAMED, 0); + if (!na) { + err = errno; + ntfs_log_perror("Failed to open just added $ATTRIBUTE_LIST"); + goto remove_attrlist_record; + } + if (ntfs_attr_truncate(na, al_len)) { + err = errno; + ntfs_log_perror("Failed to resize just added $ATTRIBUTE_LIST"); + ntfs_attr_close(na); + goto remove_attrlist_record;; + } + + ntfs_attr_put_search_ctx(ctx); + ntfs_attr_close(na); + return 0; + +remove_attrlist_record: + /* Prevent ntfs_attr_recorm_rm from freeing attribute list. */ + ni->attr_list = NULL; + NInoClearAttrList(ni); + /* Remove $ATTRIBUTE_LIST record. */ + ntfs_attr_reinit_search_ctx(ctx); + if (!ntfs_attr_lookup(AT_ATTRIBUTE_LIST, NULL, 0, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + if (ntfs_attr_record_rm(ctx)) + ntfs_log_perror("Rollback failed to remove attrlist"); + } else + ntfs_log_perror("Rollback failed to find attrlist"); + /* Setup back in-memory runlist. */ + ni->attr_list = al; + ni->attr_list_size = al_len; + NInoSetAttrList(ni); +rollback: + /* + * Scan attribute list for attributes that placed not in the base MFT + * record and move them to it. + */ + ntfs_attr_reinit_search_ctx(ctx); + ale = (ATTR_LIST_ENTRY*)al; + while ((u8*)ale < al + al_len) { + if (MREF_LE(ale->mft_reference) != ni->mft_no) { + if (!ntfs_attr_lookup(ale->type, ale->name, + ale->name_length, + CASE_SENSITIVE, + sle64_to_cpu(ale->lowest_vcn), + NULL, 0, ctx)) { + if (ntfs_attr_record_move_to(ctx, ni)) + ntfs_log_perror("Rollback failed to " + "move attribute"); + } else + ntfs_log_perror("Rollback failed to find attr"); + ntfs_attr_reinit_search_ctx(ctx); + } + ale = (ATTR_LIST_ENTRY*)((u8*)ale + le16_to_cpu(ale->length)); + } + /* Remove in-memory attribute list. */ + ni->attr_list = NULL; + ni->attr_list_size = 0; + NInoClearAttrList(ni); + NInoAttrListClearDirty(ni); +put_err_out: + ntfs_attr_put_search_ctx(ctx); +err_out: + free(al); + errno = err; + return -1; +} + +/** + * ntfs_inode_free_space - free space in the MFT record of an inode + * @ni: ntfs inode in which MFT record needs more free space + * @size: amount of space needed to free + * + * Return 0 on success or -1 on error with errno set to the error code. + */ +int ntfs_inode_free_space(ntfs_inode *ni, int size) +{ + ntfs_attr_search_ctx *ctx; + int freed; + + if (!ni || size < 0) { + errno = EINVAL; + ntfs_log_perror("%s: ni=%p size=%d", __FUNCTION__, ni, size); + return -1; + } + + ntfs_log_trace("Entering for inode %lld, size %d\n", + (unsigned long long)ni->mft_no, size); + + freed = (le32_to_cpu(ni->mrec->bytes_allocated) - + le32_to_cpu(ni->mrec->bytes_in_use)); + + if (size <= freed) + return 0; + + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return -1; + /* + * $STANDARD_INFORMATION and $ATTRIBUTE_LIST must stay in the base MFT + * record, so position search context on the first attribute after them. + */ + if (ntfs_attr_position(AT_FILE_NAME, ctx)) + goto put_err_out; + + while (1) { + int record_size; + /* + * Check whether attribute is from different MFT record. If so, + * find next, because we don't need such. + */ + while (ctx->ntfs_ino->mft_no != ni->mft_no) { +retry: + if (ntfs_attr_position(AT_UNUSED, ctx)) + goto put_err_out; + } + + if (ntfs_inode_base(ctx->ntfs_ino)->mft_no == FILE_MFT && + ctx->attr->type == AT_DATA) + goto retry; + + if (ctx->attr->type == AT_INDEX_ROOT) + goto retry; + + record_size = le32_to_cpu(ctx->attr->length); + + if (ntfs_attr_record_move_away(ctx, 0)) { + ntfs_log_perror("Failed to move out attribute #2"); + break; + } + freed += record_size; + + /* Check whether we are done. */ + if (size <= freed) { + ntfs_attr_put_search_ctx(ctx); + return 0; + } + /* + * Reposition to first attribute after $STANDARD_INFORMATION + * and $ATTRIBUTE_LIST instead of simply skipping this attribute + * because in the case when we have got only in-memory attribute + * list then ntfs_attr_lookup will fail when it tries to find + * $ATTRIBUTE_LIST. + */ + ntfs_attr_reinit_search_ctx(ctx); + if (ntfs_attr_position(AT_FILE_NAME, ctx)) + break; + } +put_err_out: + ntfs_attr_put_search_ctx(ctx); + if (errno == ENOSPC) + ntfs_log_trace("No attributes left that could be moved out.\n"); + return -1; +} + +/** + * ntfs_inode_update_times - update selected time fields for ntfs inode + * @ni: ntfs inode for which update time fields + * @mask: select which time fields should be updated + * + * This function updates time fields to current time. Fields to update are + * selected using @mask (see enum @ntfs_time_update_flags for posssible values). + */ +void ntfs_inode_update_times(ntfs_inode *ni, ntfs_time_update_flags mask) +{ + ntfs_time now; + + if (!ni) { + ntfs_log_error("%s(): Invalid arguments.\n", __FUNCTION__); + return; + } + + if ((ni->mft_no < FILE_first_user && ni->mft_no != FILE_root) || + NVolReadOnly(ni->vol) || !mask) + return; + + now = ntfs_current_time(); + if (mask & NTFS_UPDATE_ATIME) + ni->last_access_time = now; + if (mask & NTFS_UPDATE_MTIME) + ni->last_data_change_time = now; + if (mask & NTFS_UPDATE_CTIME) + ni->last_mft_change_time = now; + + NInoFileNameSetDirty(ni); + NInoSetDirty(ni); +} + +/** + * ntfs_inode_badclus_bad - check for $Badclus:$Bad data attribute + * @mft_no: mft record number where @attr is present + * @attr: attribute record used to check for the $Bad attribute + * + * Check if the mft record given by @mft_no and @attr contains the bad sector + * list. Please note that mft record numbers describing $Badclus extent inodes + * will not match the current $Badclus:$Bad check. + * + * On success return 1 if the file is $Badclus:$Bad, otherwise return 0. + * On error return -1 with errno set to the error code. + */ +int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *attr) +{ + int len, ret = 0; + ntfschar *ustr; + + if (!attr) { + ntfs_log_error("Invalid argument.\n"); + errno = EINVAL; + return -1; + } + + if (mft_no != FILE_BadClus) + return 0; + + if (attr->type != AT_DATA) + return 0; + + if ((ustr = ntfs_str2ucs("$Bad", &len)) == NULL) { + ntfs_log_perror("Couldn't convert '$Bad' to Unicode"); + return -1; + } + + if (ustr && ntfs_names_are_equal(ustr, len, + (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset)), + attr->name_length, 0, NULL, 0)) + ret = 1; + + ntfs_ucsfree(ustr); + + return ret; +} + +/* + * Get high precision NTFS times + * + * They are returned in following order : create, update, access, change + * provided they fit in requested size. + * + * Returns the modified size if successfull (or 32 if buffer size is null) + * -errno if failed + */ + +int ntfs_inode_get_times(ntfs_inode *ni, char *value, size_t size) +{ + ntfs_attr_search_ctx *ctx; + STANDARD_INFORMATION *std_info; + u64 *times; + int ret; + + ret = 0; + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (ctx) { + if (ntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED, + 0, CASE_SENSITIVE, 0, NULL, 0, ctx)) { + ntfs_log_perror("Failed to get standard info (inode %lld)", + (long long)ni->mft_no); + } else { + std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + if (value && (size >= 8)) { + times = (u64*)value; + times[0] = sle64_to_cpu(std_info->creation_time); + ret = 8; + if (size >= 16) { + times[1] = sle64_to_cpu(std_info->last_data_change_time); + ret = 16; + } + if (size >= 24) { + times[2] = sle64_to_cpu(std_info->last_access_time); + ret = 24; + } + if (size >= 32) { + times[3] = sle64_to_cpu(std_info->last_mft_change_time); + ret = 32; + } + } else + if (!size) + ret = 32; + else + ret = -ERANGE; + } + ntfs_attr_put_search_ctx(ctx); + } + return (ret ? ret : -errno); +} + +/* + * Set high precision NTFS times + * + * They are expected in this order : create, update, access + * provided they are present in input. The change time is set to + * current time. + * + * The times are inserted directly in the standard_information and + * file names attributes to avoid manipulating low precision times + * + * Returns 0 if success + * -1 if there were an error (described by errno) + */ + +int ntfs_inode_set_times(ntfs_inode *ni, const char *value, size_t size, + int flags) +{ + ntfs_attr_search_ctx *ctx; + STANDARD_INFORMATION *std_info; + FILE_NAME_ATTR *fn; + u64 times[4]; + ntfs_time now; + int cnt; + int ret; + + ret = -1; + if ((size >= 8) && !(flags & XATTR_CREATE)) { + /* Copy, to avoid alignment issue encountered on ARM */ + memcpy(times, value, + (size < sizeof(times) ? size : sizeof(times))); + now = ntfs_current_time(); + /* update the standard information attribute */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (ctx) { + if (ntfs_attr_lookup(AT_STANDARD_INFORMATION, + AT_UNNAMED, 0, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + ntfs_log_perror("Failed to get standard info (inode %lld)", + (long long)ni->mft_no); + } else { + std_info = (STANDARD_INFORMATION *)((u8 *)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + /* + * Mark times set to avoid overwriting + * them when the inode is closed. + * The inode structure must also be updated + * (with loss of precision) because of cacheing. + * TODO : use NTFS precision in inode, and + * return sub-second times in getattr() + */ + set_nino_flag(ni, TimesSet); + std_info->creation_time = cpu_to_sle64(times[0]); + ni->creation_time + = std_info->creation_time; + if (size >= 16) { + std_info->last_data_change_time = cpu_to_sle64(times[1]); + ni->last_data_change_time + = std_info->last_data_change_time; + } + if (size >= 24) { + std_info->last_access_time = cpu_to_sle64(times[2]); + ni->last_access_time + = std_info->last_access_time; + } + std_info->last_mft_change_time = now; + ni->last_mft_change_time = now; + ntfs_inode_mark_dirty(ctx->ntfs_ino); + NInoFileNameSetDirty(ni); + + /* update the file names attributes */ + ntfs_attr_reinit_search_ctx(ctx); + cnt = 0; + while (!ntfs_attr_lookup(AT_FILE_NAME, + AT_UNNAMED, 0, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + fn = (FILE_NAME_ATTR*)((u8 *)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + fn->creation_time + = cpu_to_sle64(times[0]); + if (size >= 16) + fn->last_data_change_time + = cpu_to_sle64(times[1]); + if (size >= 24) + fn->last_access_time + = cpu_to_sle64(times[2]); + fn->last_mft_change_time = now; + cnt++; + } + if (cnt) + ret = 0; + else { + ntfs_log_perror("Failed to get file names (inode %lld)", + (long long)ni->mft_no); + } + } + ntfs_attr_put_search_ctx(ctx); + } + } else + if (size < 8) + errno = ERANGE; + else + errno = EEXIST; + return (ret); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/ioctl.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/ioctl.c new file mode 100644 index 0000000..be13ede --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/ioctl.c @@ -0,0 +1,423 @@ +/** + * ioctl.c - Processing of ioctls + * + * This module is part of ntfs-3g library + * + * Copyright (c) 2014-2019 Jean-Pierre Andre + * Copyright (c) 2014 Red Hat, Inc. + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_INTTYPES_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif +#include +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef MAJOR_IN_MKDEV +#include +#endif +#ifdef MAJOR_IN_SYSMACROS +#include +#endif + +#ifdef HAVE_SYS_STAT_H +#include +#endif + +#ifdef HAVE_LINUX_FS_H +#include +#endif + +#include "compat.h" +#include "debug.h" +#include "bitmap.h" +#include "attrib.h" +#include "inode.h" +#include "layout.h" +#include "volume.h" +#include "index.h" +#include "logging.h" +#include "ntfstime.h" +#include "unistr.h" +#include "dir.h" +#include "security.h" +#include "ioctl.h" +#include "misc.h" + +#if defined(FITRIM) && defined(BLKDISCARD) + +/* Issue a TRIM request to the underlying device for the given clusters. */ +static int fstrim_clusters(ntfs_volume *vol, LCN lcn, s64 length) +{ + struct ntfs_device *dev = vol->dev; + uint64_t range[2]; + + ntfs_log_debug("fstrim_clusters: %lld length %lld\n", + (long long) lcn, (long long) length); + + range[0] = lcn << vol->cluster_size_bits; + range[1] = length << vol->cluster_size_bits; + + if (dev->d_ops->ioctl(dev, BLKDISCARD, range) == -1) { + ntfs_log_debug("fstrim_one_cluster: ioctl failed: %m\n"); + return -errno; + } + return 0; +} + +static int read_line(const char *path, char *line, size_t max_bytes) +{ + FILE *fp; + + fp = fopen(path, "r"); + if (fp == NULL) + return -errno; + if (fgets(line, max_bytes, fp) == NULL) { + int ret = -EIO; /* fgets doesn't set errno */ + fclose(fp); + return ret; + } + fclose (fp); + return 0; +} + +static int read_u64(const char *path, u64 *n) +{ + char line[64]; + int ret; + + ret = read_line(path, line, sizeof line); + if (ret) + return ret; + if (sscanf(line, "%" SCNu64, n) != 1) + return -EINVAL; + return 0; +} + +/* Find discard limits for current backing device. + */ +static int fstrim_limits(ntfs_volume *vol, + u64 *discard_alignment, + u64 *discard_granularity, + u64 *discard_max_bytes) +{ + struct stat statbuf; + char path1[40]; /* holds "/sys/dev/block/%d:%d" */ + char path2[40 + sizeof(path1)]; /* less than 40 bytes more than path1 */ + int ret; + + /* Stat the backing device. Caller has ensured it is a block device. */ + if (stat(vol->dev->d_name, &statbuf) == -1) { + ntfs_log_debug("fstrim_limits: could not stat %s\n", + vol->dev->d_name); + return -errno; + } + + /* For whole devices, + * /sys/dev/block/MAJOR:MINOR/discard_alignment + * /sys/dev/block/MAJOR:MINOR/queue/discard_granularity + * /sys/dev/block/MAJOR:MINOR/queue/discard_max_bytes + * will exist. + * For partitions, we also need to check the parent device: + * /sys/dev/block/MAJOR:MINOR/../queue/discard_granularity + * /sys/dev/block/MAJOR:MINOR/../queue/discard_max_bytes + */ + snprintf(path1, sizeof path1, "/sys/dev/block/%d:%d", + major(statbuf.st_rdev), minor(statbuf.st_rdev)); + + snprintf(path2, sizeof path2, "%s/discard_alignment", path1); + ret = read_u64(path2, discard_alignment); + if (ret) { + if (ret != -ENOENT) + return ret; + else + /* We would expect this file to exist on all + * modern kernels. But for the sake of very + * old kernels: + */ + goto not_found; + } + + snprintf(path2, sizeof path2, "%s/queue/discard_granularity", path1); + ret = read_u64(path2, discard_granularity); + if (ret) { + if (ret != -ENOENT) + return ret; + else { + snprintf(path2, sizeof path2, + "%s/../queue/discard_granularity", path1); + ret = read_u64(path2, discard_granularity); + if (ret) { + if (ret != -ENOENT) + return ret; + else + goto not_found; + } + } + } + + snprintf(path2, sizeof path2, "%s/queue/discard_max_bytes", path1); + ret = read_u64(path2, discard_max_bytes); + if (ret) { + if (ret != -ENOENT) + return ret; + else { + snprintf(path2, sizeof path2, + "%s/../queue/discard_max_bytes", path1); + ret = read_u64(path2, discard_max_bytes); + if (ret) { + if (ret != -ENOENT) + return ret; + else + goto not_found; + } + } + } + + return 0; + +not_found: + /* If we reach here then we didn't find the device. This is + * not an error, but set discard_max_bytes = 0 to indicate + * that discard is not available. + */ + *discard_alignment = 0; + *discard_granularity = 0; + *discard_max_bytes = 0; + return 0; +} + +static inline LCN align_up(ntfs_volume *vol, LCN lcn, u64 granularity) +{ + u64 aligned; + + aligned = (lcn << vol->cluster_size_bits) + granularity - 1; + aligned -= aligned % granularity; + return (aligned >> vol->cluster_size_bits); +} + +static inline u64 align_down(ntfs_volume *vol, u64 count, u64 granularity) +{ + u64 aligned; + + aligned = count << vol->cluster_size_bits; + aligned -= aligned % granularity; + return (aligned >> vol->cluster_size_bits); +} + +#define FSTRIM_BUFSIZ 4096 + +/* Trim the filesystem. + * + * Free blocks between 'start' and 'start+len-1' (both byte offsets) + * are found and TRIM requests are sent to the block device. 'minlen' + * is the minimum continguous free range to discard. + */ +static int fstrim(ntfs_volume *vol, void *data, u64 *trimmed) +{ + struct fstrim_range *range = data; + u64 start = range->start; + u64 len = range->len; + u64 minlen = range->minlen; + u64 discard_alignment, discard_granularity, discard_max_bytes; + u8 *buf = NULL; + LCN start_buf; + int ret; + + ntfs_log_debug("fstrim: start=%llu len=%llu minlen=%llu\n", + (unsigned long long) start, + (unsigned long long) len, + (unsigned long long) minlen); + + *trimmed = 0; + + /* Fail if user tries to use the fstrim -o/-l/-m options. + * XXX We could fix these limitations in future. + */ + if (start != 0 || len != (uint64_t)-1) { + ntfs_log_error("fstrim: setting start or length is not supported\n"); + return -EINVAL; + } + if (minlen > vol->cluster_size) { + ntfs_log_error("fstrim: minlen > cluster size is not supported\n"); + return -EINVAL; + } + + /* Only block devices are supported. It would be possible to + * support backing files (ie. without using loop) but the + * ioctls used to punch holes in files are completely + * different. + */ + if (!NDevBlock(vol->dev)) { + ntfs_log_error("fstrim: not supported for non-block-device\n"); + return -EOPNOTSUPP; + } + + ret = fstrim_limits(vol, &discard_alignment, + &discard_granularity, &discard_max_bytes); + if (ret) + return ret; + if (discard_alignment != 0) { + ntfs_log_error("fstrim: backing device is not aligned for discards\n"); + return -EOPNOTSUPP; + } + + if (discard_max_bytes == 0) { + ntfs_log_error("fstrim: backing device does not support discard (discard_max_bytes == 0)\n"); + return -EOPNOTSUPP; + } + + /* Sync the device before doing anything. */ + ret = ntfs_device_sync(vol->dev); + if (ret) + return ret; + + /* Read through the bitmap. */ + buf = ntfs_malloc(FSTRIM_BUFSIZ); + if (buf == NULL) + return -errno; + for (start_buf = 0; start_buf < vol->nr_clusters; + start_buf += FSTRIM_BUFSIZ * 8) { + s64 count; + s64 br; + LCN end_buf, start_lcn; + + /* start_buf is LCN of first cluster in the current buffer. + * end_buf is LCN of last cluster + 1 in the current buffer. + */ + end_buf = start_buf + FSTRIM_BUFSIZ*8; + if (end_buf > vol->nr_clusters) + end_buf = vol->nr_clusters; + count = (end_buf - start_buf) / 8; + + br = ntfs_attr_pread(vol->lcnbmp_na, start_buf/8, count, buf); + if (br != count) { + if (br >= 0) + ret = -EIO; + else + ret = -errno; + goto free_out; + } + + /* Trim the clusters in large as possible blocks, but + * not larger than discard_max_bytes, and compatible + * with the supported trim granularity. + */ + for (start_lcn = start_buf; start_lcn < end_buf; ++start_lcn) { + if (!ntfs_bit_get(buf, start_lcn-start_buf)) { + LCN end_lcn; + LCN aligned_lcn; + u64 aligned_count; + + /* Cluster 'start_lcn' is not in use, + * find end of this run. + */ + end_lcn = start_lcn+1; + while (end_lcn < end_buf && + (u64) (end_lcn-start_lcn) << vol->cluster_size_bits + < discard_max_bytes && + !ntfs_bit_get(buf, end_lcn-start_buf)) + end_lcn++; + aligned_lcn = align_up(vol, start_lcn, + discard_granularity); + if (aligned_lcn >= end_lcn) + aligned_count = 0; + else { + aligned_count = + align_down(vol, + end_lcn - aligned_lcn, + discard_granularity); + } + if (aligned_count) { + ret = fstrim_clusters(vol, + aligned_lcn, aligned_count); + if (ret) + goto free_out; + + *trimmed += aligned_count + << vol->cluster_size_bits; + } + start_lcn = end_lcn-1; + } + } + } + + ret = 0; +free_out: + free(buf); + return ret; +} + +#endif /* FITRIM && BLKDISCARD */ + +int ntfs_ioctl(ntfs_inode *ni, unsigned long cmd, + void *arg __attribute__((unused)), + unsigned int flags __attribute__((unused)), void *data) +{ + int ret = 0; + + switch (cmd) { +#if defined(FITRIM) && defined(BLKDISCARD) + case FITRIM: + if (!ni || !data) + ret = -EINVAL; + else { + u64 trimmed; + struct fstrim_range *range = (struct fstrim_range*)data; + + ret = fstrim(ni->vol, data, &trimmed); + range->len = trimmed; + } + break; +#else +#ifdef _MSC_VER +#pragma message("ntfs-3g: trimming not supported (FITRIM or BLKDISCARD not defined)") +#else +#warning Trimming not supported : FITRIM or BLKDISCARD not defined +#endif +#endif + default : + ret = -EINVAL; + break; + } + return (ret); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/lcnalloc.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/lcnalloc.c new file mode 100644 index 0000000..a1c1ee3 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/lcnalloc.c @@ -0,0 +1,774 @@ +/** + * lcnalloc.c - Cluster (de)allocation code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002-2004 Anton Altaparmakov + * Copyright (c) 2004 Yura Pakhuchiy + * Copyright (c) 2004-2008 Szabolcs Szakacsits + * Copyright (c) 2008-2009 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "types.h" +#include "attrib.h" +#include "bitmap.h" +#include "debug.h" +#include "runlist.h" +#include "volume.h" +#include "lcnalloc.h" +#include "logging.h" +#include "misc.h" + +/* + * Plenty possibilities for big optimizations all over in the cluster + * allocation, however at the moment the dominant bottleneck (~ 90%) is + * the update of the mapping pairs which converges to the cubic Faulhaber's + * formula as the function of the number of extents (fragments, runs). + */ +#define NTFS_LCNALLOC_BSIZE 4096 +#define NTFS_LCNALLOC_SKIP NTFS_LCNALLOC_BSIZE + +enum { + ZONE_MFT = 1, + ZONE_DATA1 = 2, + ZONE_DATA2 = 4 +} ; + +static void ntfs_cluster_set_zone_pos(LCN start, LCN end, LCN *pos, LCN tc) +{ + ntfs_log_trace("pos: %lld tc: %lld\n", (long long)*pos, (long long)tc); + + if (tc >= end) + *pos = start; + else if (tc >= start) + *pos = tc; +} + +static void ntfs_cluster_update_zone_pos(ntfs_volume *vol, u8 zone, LCN tc) +{ + ntfs_log_trace("tc = %lld, zone = %d\n", (long long)tc, zone); + + if (zone == ZONE_MFT) + ntfs_cluster_set_zone_pos(vol->mft_lcn, vol->mft_zone_end, + &vol->mft_zone_pos, tc); + else if (zone == ZONE_DATA1) + ntfs_cluster_set_zone_pos(vol->mft_zone_end, vol->nr_clusters, + &vol->data1_zone_pos, tc); + else /* zone == ZONE_DATA2 */ + ntfs_cluster_set_zone_pos(0, vol->mft_zone_start, + &vol->data2_zone_pos, tc); +} + +/* + * Unmark full zones when a cluster has been freed in a full zone + * + * Next allocation will reuse the freed cluster + */ + +static void update_full_status(ntfs_volume *vol, LCN lcn) +{ + if (lcn >= vol->mft_zone_end) { + if (vol->full_zones & ZONE_DATA1) { + ntfs_cluster_update_zone_pos(vol, ZONE_DATA1, lcn); + vol->full_zones &= ~ZONE_DATA1; + } + } else + if (lcn < vol->mft_zone_start) { + if (vol->full_zones & ZONE_DATA2) { + ntfs_cluster_update_zone_pos(vol, ZONE_DATA2, lcn); + vol->full_zones &= ~ZONE_DATA2; + } + } else { + if (vol->full_zones & ZONE_MFT) { + ntfs_cluster_update_zone_pos(vol, ZONE_MFT, lcn); + vol->full_zones &= ~ZONE_MFT; + } + } +} + +static s64 max_empty_bit_range(unsigned char *buf, int size) +{ + int i, j, run = 0; + int max_range = 0; + s64 start_pos = -1; + + ntfs_log_trace("Entering\n"); + + i = 0; + while (i < size) { + switch (*buf) { + case 0 : + do { + buf++; + run += 8; + i++; + } while ((i < size) && !*buf); + break; + case 255 : + if (run > max_range) { + max_range = run; + start_pos = (s64)i * 8 - run; + } + run = 0; + do { + buf++; + i++; + } while ((i < size) && (*buf == 255)); + break; + default : + for (j = 0; j < 8; j++) { + + int bit = *buf & (1 << j); + + if (bit) { + if (run > max_range) { + max_range = run; + start_pos = (s64)i * 8 + (j - run); + } + run = 0; + } else + run++; + } + i++; + buf++; + + } + } + + if (run > max_range) + start_pos = (s64)i * 8 - run; + + return start_pos; +} + +static int bitmap_writeback(ntfs_volume *vol, s64 pos, s64 size, void *b, + u8 *writeback) +{ + s64 written; + + ntfs_log_trace("Entering\n"); + + if (!*writeback) + return 0; + + *writeback = 0; + + written = ntfs_attr_pwrite(vol->lcnbmp_na, pos, size, b); + if (written != size) { + if (!written) + errno = EIO; + ntfs_log_perror("Bitmap write error (%lld, %lld)", + (long long)pos, (long long)size); + return -1; + } + + return 0; +} + +/** + * ntfs_cluster_alloc - allocate clusters on an ntfs volume + * @vol: mounted ntfs volume on which to allocate the clusters + * @start_vcn: vcn to use for the first allocated cluster + * @count: number of clusters to allocate + * @start_lcn: starting lcn at which to allocate the clusters (or -1 if none) + * @zone: zone from which to allocate the clusters + * + * Allocate @count clusters preferably starting at cluster @start_lcn or at the + * current allocator position if @start_lcn is -1, on the mounted ntfs volume + * @vol. @zone is either DATA_ZONE for allocation of normal clusters and + * MFT_ZONE for allocation of clusters for the master file table, i.e. the + * $MFT/$DATA attribute. + * + * On success return a runlist describing the allocated cluster(s). + * + * On error return NULL with errno set to the error code. + * + * Notes on the allocation algorithm + * ================================= + * + * There are two data zones. First is the area between the end of the mft zone + * and the end of the volume, and second is the area between the start of the + * volume and the start of the mft zone. On unmodified/standard NTFS 1.x + * volumes, the second data zone doesn't exist due to the mft zone being + * expanded to cover the start of the volume in order to reserve space for the + * mft bitmap attribute. + * + * The complexity stems from the need of implementing the mft vs data zoned + * approach and from the fact that we have access to the lcn bitmap via up to + * NTFS_LCNALLOC_BSIZE bytes at a time, so we need to cope with crossing over + * boundaries of two buffers. Further, the fact that the allocator allows for + * caller supplied hints as to the location of where allocation should begin + * and the fact that the allocator keeps track of where in the data zones the + * next natural allocation should occur, contribute to the complexity of the + * function. But it should all be worthwhile, because this allocator: + * 1) implements MFT zone reservation + * 2) causes reduction in fragmentation. + * The code is not optimized for speed. + */ +runlist *ntfs_cluster_alloc(ntfs_volume *vol, VCN start_vcn, s64 count, + LCN start_lcn, const NTFS_CLUSTER_ALLOCATION_ZONES zone) +{ + LCN zone_start, zone_end; /* current search range */ + LCN last_read_pos, lcn; + LCN bmp_pos; /* current bit position inside the bitmap */ + LCN prev_lcn = 0, prev_run_len = 0; + s64 clusters, br; + runlist *rl = NULL, *trl; + u8 *buf, *byte, bit, writeback; + u8 pass = 1; /* 1: inside zone; 2: start of zone */ + u8 search_zone; /* 4: data2 (start) 1: mft (middle) 2: data1 (end) */ + u8 done_zones = 0; + u8 has_guess, used_zone_pos; + int err = 0, rlpos, rlsize, buf_size; + + ntfs_log_enter("Entering with count = 0x%llx, start_lcn = 0x%llx, " + "zone = %s_ZONE.\n", (long long)count, (long long) + start_lcn, zone == MFT_ZONE ? "MFT" : "DATA"); + + if (!vol || count < 0 || start_lcn < -1 || !vol->lcnbmp_na || + (s8)zone < FIRST_ZONE || zone > LAST_ZONE) { + errno = EINVAL; + ntfs_log_perror("%s: vcn: %lld, count: %lld, lcn: %lld", + __FUNCTION__, (long long)start_vcn, + (long long)count, (long long)start_lcn); + goto out; + } + + /* Return empty runlist if @count == 0 */ + if (!count) { + rl = ntfs_malloc(0x1000); + if (rl) { + rl[0].vcn = start_vcn; + rl[0].lcn = LCN_RL_NOT_MAPPED; + rl[0].length = 0; + } + goto out; + } + + buf = ntfs_malloc(NTFS_LCNALLOC_BSIZE); + if (!buf) + goto out; + /* + * If no @start_lcn was requested, use the current zone + * position otherwise use the requested @start_lcn. + */ + has_guess = 1; + zone_start = start_lcn; + + if (zone_start < 0) { + if (zone == DATA_ZONE) + zone_start = vol->data1_zone_pos; + else + zone_start = vol->mft_zone_pos; + has_guess = 0; + } + + used_zone_pos = has_guess ? 0 : 1; + + if (!zone_start || zone_start == vol->mft_zone_start || + zone_start == vol->mft_zone_end) + pass = 2; + + if (zone_start < vol->mft_zone_start) { + zone_end = vol->mft_zone_start; + search_zone = ZONE_DATA2; + } else if (zone_start < vol->mft_zone_end) { + zone_end = vol->mft_zone_end; + search_zone = ZONE_MFT; + } else { + zone_end = vol->nr_clusters; + search_zone = ZONE_DATA1; + } + + bmp_pos = zone_start; + + /* Loop until all clusters are allocated. */ + clusters = count; + rlpos = rlsize = 0; + while (1) { + /* check whether we have exhausted the current zone */ + if (search_zone & vol->full_zones) + goto zone_pass_done; + last_read_pos = bmp_pos >> 3; + br = ntfs_attr_pread(vol->lcnbmp_na, last_read_pos, + NTFS_LCNALLOC_BSIZE, buf); + if (br <= 0) { + if (!br) + goto zone_pass_done; + err = errno; + ntfs_log_perror("Reading $BITMAP failed"); + goto err_ret; + } + /* + * We might have read less than NTFS_LCNALLOC_BSIZE bytes + * if we are close to the end of the attribute. + */ + buf_size = (int)br << 3; + lcn = bmp_pos & 7; + bmp_pos &= ~7; + writeback = 0; + + while (lcn < buf_size) { + byte = buf + (lcn >> 3); + bit = 1 << (lcn & 7); + if (has_guess) { + if (*byte & bit) { + has_guess = 0; + break; + } + } else { + lcn = max_empty_bit_range(buf, br); + if (lcn < 0) + break; + has_guess = 1; + continue; + } + + /* First free bit is at lcn + bmp_pos. */ + + /* Reallocate memory if necessary. */ + if ((rlpos + 2) * (int)sizeof(runlist) >= rlsize) { + rlsize += 4096; + trl = realloc(rl, rlsize); + if (!trl) { + err = ENOMEM; + ntfs_log_perror("realloc() failed"); + goto wb_err_ret; + } + rl = trl; + } + + /* Allocate the bitmap bit. */ + *byte |= bit; + writeback = 1; + if (NVolFreeSpaceKnown(vol)) { + if (vol->free_clusters <= 0) + ntfs_log_error("Non-positive free" + " clusters (%lld)!\n", + (long long)vol->free_clusters); + else + vol->free_clusters--; + } + + /* + * Coalesce with previous run if adjacent LCNs. + * Otherwise, append a new run. + */ + if (prev_lcn == lcn + bmp_pos - prev_run_len && rlpos) { + ntfs_log_debug("Cluster coalesce: prev_lcn: " + "%lld lcn: %lld bmp_pos: %lld " + "prev_run_len: %lld\n", + (long long)prev_lcn, + (long long)lcn, (long long)bmp_pos, + (long long)prev_run_len); + rl[rlpos - 1].length = ++prev_run_len; + } else { + if (rlpos) + rl[rlpos].vcn = rl[rlpos - 1].vcn + + prev_run_len; + else { + rl[rlpos].vcn = start_vcn; + ntfs_log_debug("Start_vcn: %lld\n", + (long long)start_vcn); + } + + rl[rlpos].lcn = prev_lcn = lcn + bmp_pos; + rl[rlpos].length = prev_run_len = 1; + rlpos++; + } + + ntfs_log_debug("RUN: %-16lld %-16lld %-16lld\n", + (long long)rl[rlpos - 1].vcn, + (long long)rl[rlpos - 1].lcn, + (long long)rl[rlpos - 1].length); + /* Done? */ + if (!--clusters) { + if (used_zone_pos) + ntfs_cluster_update_zone_pos(vol, + search_zone, lcn + bmp_pos + 1 + + NTFS_LCNALLOC_SKIP); + goto done_ret; + } + + lcn++; + } + + if (bitmap_writeback(vol, last_read_pos, br, buf, &writeback)) { + err = errno; + goto err_ret; + } + + if (!used_zone_pos) { + + used_zone_pos = 1; + + if (search_zone == ZONE_MFT) + zone_start = vol->mft_zone_pos; + else if (search_zone == ZONE_DATA1) + zone_start = vol->data1_zone_pos; + else + zone_start = vol->data2_zone_pos; + + if (!zone_start || zone_start == vol->mft_zone_start || + zone_start == vol->mft_zone_end) + pass = 2; + bmp_pos = zone_start; + } else + bmp_pos += buf_size; + + if (bmp_pos < zone_end) + continue; + +zone_pass_done: + ntfs_log_trace("Finished current zone pass(%i).\n", pass); + if (pass == 1) { + pass = 2; + zone_end = zone_start; + + if (search_zone == ZONE_MFT) + zone_start = vol->mft_zone_start; + else if (search_zone == ZONE_DATA1) + zone_start = vol->mft_zone_end; + else + zone_start = 0; + + /* Sanity check. */ + if (zone_end < zone_start) + zone_end = zone_start; + + bmp_pos = zone_start; + + continue; + } + /* pass == 2 */ +done_zones_check: + done_zones |= search_zone; + vol->full_zones |= search_zone; + if (done_zones < (ZONE_MFT + ZONE_DATA1 + ZONE_DATA2)) { + ntfs_log_trace("Switching zone.\n"); + pass = 1; + if (rlpos) { + LCN tc = rl[rlpos - 1].lcn + + rl[rlpos - 1].length + NTFS_LCNALLOC_SKIP; + + if (used_zone_pos) + ntfs_cluster_update_zone_pos(vol, + search_zone, tc); + } + + switch (search_zone) { + case ZONE_MFT: + ntfs_log_trace("Zone switch: mft -> data1\n"); +switch_to_data1_zone: search_zone = ZONE_DATA1; + zone_start = vol->data1_zone_pos; + zone_end = vol->nr_clusters; + if (zone_start == vol->mft_zone_end) + pass = 2; + break; + case ZONE_DATA1: + ntfs_log_trace("Zone switch: data1 -> data2\n"); + search_zone = ZONE_DATA2; + zone_start = vol->data2_zone_pos; + zone_end = vol->mft_zone_start; + if (!zone_start) + pass = 2; + break; + case ZONE_DATA2: + if (!(done_zones & ZONE_DATA1)) { + ntfs_log_trace("data2 -> data1\n"); + goto switch_to_data1_zone; + } + ntfs_log_trace("Zone switch: data2 -> mft\n"); + search_zone = ZONE_MFT; + zone_start = vol->mft_zone_pos; + zone_end = vol->mft_zone_end; + if (zone_start == vol->mft_zone_start) + pass = 2; + break; + } + + bmp_pos = zone_start; + + if (zone_start == zone_end) { + ntfs_log_trace("Empty zone, skipped.\n"); + goto done_zones_check; + } + + continue; + } + + ntfs_log_trace("All zones are finished, no space on device.\n"); + err = ENOSPC; + goto err_ret; + } +done_ret: + ntfs_log_debug("At done_ret.\n"); + /* Add runlist terminator element. */ + rl[rlpos].vcn = rl[rlpos - 1].vcn + rl[rlpos - 1].length; + rl[rlpos].lcn = LCN_RL_NOT_MAPPED; + rl[rlpos].length = 0; + if (bitmap_writeback(vol, last_read_pos, br, buf, &writeback)) { + err = errno; + goto err_ret; + } +done_err_ret: + free(buf); + if (err) { + errno = err; + ntfs_log_perror("Failed to allocate clusters"); + rl = NULL; + } +out: + ntfs_log_leave("\n"); + return rl; + +wb_err_ret: + ntfs_log_trace("At wb_err_ret.\n"); + if (bitmap_writeback(vol, last_read_pos, br, buf, &writeback)) + err = errno; +err_ret: + ntfs_log_trace("At err_ret.\n"); + if (rl) { + /* Add runlist terminator element. */ + rl[rlpos].vcn = rl[rlpos - 1].vcn + rl[rlpos - 1].length; + rl[rlpos].lcn = LCN_RL_NOT_MAPPED; + rl[rlpos].length = 0; + ntfs_debug_runlist_dump(rl); + ntfs_cluster_free_from_rl(vol, rl); + free(rl); + rl = NULL; + } + goto done_err_ret; +} + +/** + * ntfs_cluster_free_from_rl - free clusters from runlist + * @vol: mounted ntfs volume on which to free the clusters + * @rl: runlist from which deallocate clusters + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +int ntfs_cluster_free_from_rl(ntfs_volume *vol, runlist *rl) +{ + s64 nr_freed = 0; + int ret = -1; + + ntfs_log_trace("Entering.\n"); + + for (; rl->length; rl++) { + + ntfs_log_trace("Dealloc lcn 0x%llx, len 0x%llx.\n", + (long long)rl->lcn, (long long)rl->length); + + if (rl->lcn >= 0) { + update_full_status(vol,rl->lcn); + if (ntfs_bitmap_clear_run(vol->lcnbmp_na, rl->lcn, + rl->length)) { + ntfs_log_perror("Cluster deallocation failed " + "(%lld, %lld)", + (long long)rl->lcn, + (long long)rl->length); + goto out; + } + nr_freed += rl->length ; + } + } + + ret = 0; +out: + vol->free_clusters += nr_freed; + if (NVolFreeSpaceKnown(vol) + && (vol->free_clusters > vol->nr_clusters)) + ntfs_log_error("Too many free clusters (%lld > %lld)!", + (long long)vol->free_clusters, + (long long)vol->nr_clusters); + return ret; +} + +/* + * Basic cluster run free + * Returns 0 if successful + */ + +int ntfs_cluster_free_basic(ntfs_volume *vol, s64 lcn, s64 count) +{ + s64 nr_freed = 0; + int ret = -1; + + ntfs_log_trace("Entering.\n"); + ntfs_log_trace("Dealloc lcn 0x%llx, len 0x%llx.\n", + (long long)lcn, (long long)count); + + if (lcn >= 0) { + update_full_status(vol,lcn); + if (ntfs_bitmap_clear_run(vol->lcnbmp_na, lcn, + count)) { + ntfs_log_perror("Cluster deallocation failed " + "(%lld, %lld)", + (long long)lcn, + (long long)count); + goto out; + } + nr_freed += count; + } + ret = 0; +out: + vol->free_clusters += nr_freed; + if (vol->free_clusters > vol->nr_clusters) + ntfs_log_error("Too many free clusters (%lld > %lld)!", + (long long)vol->free_clusters, + (long long)vol->nr_clusters); + return ret; +} + +/** + * ntfs_cluster_free - free clusters on an ntfs volume + * @vol: mounted ntfs volume on which to free the clusters + * @na: attribute whose runlist describes the clusters to free + * @start_vcn: vcn in @rl at which to start freeing clusters + * @count: number of clusters to free or -1 for all clusters + * + * Free @count clusters starting at the cluster @start_vcn in the runlist + * described by the attribute @na from the mounted ntfs volume @vol. + * + * If @count is -1, all clusters from @start_vcn to the end of the runlist + * are deallocated. + * + * On success return the number of deallocated clusters (not counting sparse + * clusters) and on error return -1 with errno set to the error code. + */ +int ntfs_cluster_free(ntfs_volume *vol, ntfs_attr *na, VCN start_vcn, s64 count) +{ + runlist *rl; + s64 delta, to_free, nr_freed = 0; + int ret = -1; + + if (!vol || !vol->lcnbmp_na || !na || start_vcn < 0 || + (count < 0 && count != -1)) { + ntfs_log_trace("Invalid arguments!\n"); + errno = EINVAL; + return -1; + } + + ntfs_log_enter("Entering for inode 0x%llx, attr 0x%x, count 0x%llx, " + "vcn 0x%llx.\n", (unsigned long long)na->ni->mft_no, + le32_to_cpu(na->type), (long long)count, (long long)start_vcn); + + rl = ntfs_attr_find_vcn(na, start_vcn); + if (!rl) { + if (errno == ENOENT) + ret = 0; + goto leave; + } + + if (rl->lcn < 0 && rl->lcn != LCN_HOLE) { + errno = EIO; + ntfs_log_perror("%s: Unexpected lcn (%lld)", __FUNCTION__, + (long long)rl->lcn); + goto leave; + } + + /* Find the starting cluster inside the run that needs freeing. */ + delta = start_vcn - rl->vcn; + + /* The number of clusters in this run that need freeing. */ + to_free = rl->length - delta; + if (count >= 0 && to_free > count) + to_free = count; + + if (rl->lcn != LCN_HOLE) { + /* Do the actual freeing of the clusters in this run. */ + update_full_status(vol,rl->lcn + delta); + if (ntfs_bitmap_clear_run(vol->lcnbmp_na, rl->lcn + delta, + to_free)) + goto leave; + nr_freed = to_free; + } + + /* Go to the next run and adjust the number of clusters left to free. */ + ++rl; + if (count >= 0) + count -= to_free; + + /* + * Loop over the remaining runs, using @count as a capping value, and + * free them. + */ + for (; rl->length && count != 0; ++rl) { + // FIXME: Need to try ntfs_attr_map_runlist() for attribute + // list support! (AIA) + if (rl->lcn < 0 && rl->lcn != LCN_HOLE) { + // FIXME: Eeek! We need rollback! (AIA) + errno = EIO; + ntfs_log_perror("%s: Invalid lcn (%lli)", + __FUNCTION__, (long long)rl->lcn); + goto out; + } + + /* The number of clusters in this run that need freeing. */ + to_free = rl->length; + if (count >= 0 && to_free > count) + to_free = count; + + if (rl->lcn != LCN_HOLE) { + update_full_status(vol,rl->lcn); + if (ntfs_bitmap_clear_run(vol->lcnbmp_na, rl->lcn, + to_free)) { + // FIXME: Eeek! We need rollback! (AIA) + ntfs_log_perror("%s: Clearing bitmap run failed", + __FUNCTION__); + goto out; + } + nr_freed += to_free; + } + + if (count >= 0) + count -= to_free; + } + + if (count != -1 && count != 0) { + // FIXME: Eeek! BUG() + errno = EIO; + ntfs_log_perror("%s: count still not zero (%lld)", __FUNCTION__, + (long long)count); + goto out; + } + + ret = nr_freed; +out: + vol->free_clusters += nr_freed ; + if (vol->free_clusters > vol->nr_clusters) + ntfs_log_error("Too many free clusters (%lld > %lld)!", + (long long)vol->free_clusters, + (long long)vol->nr_clusters); +leave: + ntfs_log_leave("\n"); + return ret; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/libntfs-3g.pc.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/libntfs-3g.pc.in new file mode 100644 index 0000000..5f2fea7 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/libntfs-3g.pc.in @@ -0,0 +1,10 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: libntfs-3g +Description: NTFS-3G Read/Write Driver Library +Version: @PACKAGE_VERSION@ +Cflags: -I${includedir} +Libs: @LIBFUSE_LITE_LIBS@ -L${libdir} -lntfs-3g diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/libntfs-3g.script.so.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/libntfs-3g.script.so.in new file mode 100644 index 0000000..d050cfa --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/libntfs-3g.script.so.in @@ -0,0 +1,2 @@ +@OUTPUT_FORMAT@ +GROUP ( @rootlibdir@/libntfs-3g.so ) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/logfile.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/logfile.c new file mode 100644 index 0000000..9c3155e --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/logfile.c @@ -0,0 +1,765 @@ +/** + * logfile.c - NTFS journal handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002-2005 Anton Altaparmakov + * Copyright (c) 2005 Yura Pakhuchiy + * Copyright (c) 2005-2009 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "attrib.h" +#include "debug.h" +#include "logfile.h" +#include "volume.h" +#include "mst.h" +#include "logging.h" +#include "misc.h" + +/** + * ntfs_check_restart_page_header - check the page header for consistency + * @rp: restart page header to check + * @pos: position in logfile at which the restart page header resides + * + * Check the restart page header @rp for consistency and return TRUE if it is + * consistent and FALSE otherwise. + * + * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not + * require the full restart page. + */ +static BOOL ntfs_check_restart_page_header(RESTART_PAGE_HEADER *rp, s64 pos) +{ + u32 logfile_system_page_size, logfile_log_page_size; + u16 ra_ofs, usa_count, usa_ofs, usa_end = 0; + BOOL have_usa = TRUE; + + ntfs_log_trace("Entering.\n"); + /* + * If the system or log page sizes are smaller than the ntfs block size + * or either is not a power of 2 we cannot handle this log file. + */ + logfile_system_page_size = le32_to_cpu(rp->system_page_size); + logfile_log_page_size = le32_to_cpu(rp->log_page_size); + if (logfile_system_page_size < NTFS_BLOCK_SIZE || + logfile_log_page_size < NTFS_BLOCK_SIZE || + logfile_system_page_size & + (logfile_system_page_size - 1) || + logfile_log_page_size & (logfile_log_page_size - 1)) { + ntfs_log_error("$LogFile uses unsupported page size.\n"); + return FALSE; + } + /* + * We must be either at !pos (1st restart page) or at pos = system page + * size (2nd restart page). + */ + if (pos && pos != logfile_system_page_size) { + ntfs_log_error("Found restart area in incorrect " + "position in $LogFile.\n"); + return FALSE; + } + /* + * We only know how to handle version 1.1 and 2.0, though + * version 2.0 is probably related to cached metadata in + * Windows 8, and we will refuse to mount. + * Nevertheless, do all the relevant checks before rejecting. + */ + if (((rp->major_ver != const_cpu_to_sle16(1)) + || (rp->minor_ver != const_cpu_to_sle16(1))) + && ((rp->major_ver != const_cpu_to_sle16(2)) + || (rp->minor_ver != const_cpu_to_sle16(0)))) { + ntfs_log_error("$LogFile version %i.%i is not " + "supported.\n (This driver supports version " + "1.1 and 2.0 only.)\n", + (int)sle16_to_cpu(rp->major_ver), + (int)sle16_to_cpu(rp->minor_ver)); + return FALSE; + } + /* + * If chkdsk has been run the restart page may not be protected by an + * update sequence array. + */ + if (ntfs_is_chkd_record(rp->magic) && !le16_to_cpu(rp->usa_count)) { + have_usa = FALSE; + goto skip_usa_checks; + } + /* Verify the size of the update sequence array. */ + usa_count = 1 + (logfile_system_page_size >> NTFS_BLOCK_SIZE_BITS); + if (usa_count != le16_to_cpu(rp->usa_count)) { + ntfs_log_error("$LogFile restart page specifies " + "inconsistent update sequence array count.\n"); + return FALSE; + } + /* Verify the position of the update sequence array. */ + usa_ofs = le16_to_cpu(rp->usa_ofs); + usa_end = usa_ofs + usa_count * sizeof(u16); + if (usa_ofs < offsetof(RESTART_PAGE_HEADER, usn) || + usa_end > NTFS_BLOCK_SIZE - sizeof(u16)) { + ntfs_log_error("$LogFile restart page specifies " + "inconsistent update sequence array offset.\n"); + return FALSE; + } +skip_usa_checks: + /* + * Verify the position of the restart area. It must be: + * - aligned to 8-byte boundary, + * - after the update sequence array, and + * - within the system page size. + */ + ra_ofs = le16_to_cpu(rp->restart_area_offset); + if (ra_ofs & 7 || (have_usa ? ra_ofs < usa_end : + ra_ofs < offsetof(RESTART_PAGE_HEADER, usn)) || + ra_ofs > logfile_system_page_size) { + ntfs_log_error("$LogFile restart page specifies " + "inconsistent restart area offset.\n"); + return FALSE; + } + /* + * Only restart pages modified by chkdsk are allowed to have chkdsk_lsn + * set. + */ + if (!ntfs_is_chkd_record(rp->magic) && sle64_to_cpu(rp->chkdsk_lsn)) { + ntfs_log_error("$LogFile restart page is not modified " + "by chkdsk but a chkdsk LSN is specified.\n"); + return FALSE; + } + ntfs_log_trace("Done.\n"); + return TRUE; +} + +/** + * ntfs_check_restart_area - check the restart area for consistency + * @rp: restart page whose restart area to check + * + * Check the restart area of the restart page @rp for consistency and return + * TRUE if it is consistent and FALSE otherwise. + * + * This function assumes that the restart page header has already been + * consistency checked. + * + * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not + * require the full restart page. + */ +static BOOL ntfs_check_restart_area(RESTART_PAGE_HEADER *rp) +{ + u64 file_size; + RESTART_AREA *ra; + u16 ra_ofs, ra_len, ca_ofs; + u8 fs_bits; + + ntfs_log_trace("Entering.\n"); + ra_ofs = le16_to_cpu(rp->restart_area_offset); + ra = (RESTART_AREA*)((u8*)rp + ra_ofs); + /* + * Everything before ra->file_size must be before the first word + * protected by an update sequence number. This ensures that it is + * safe to access ra->client_array_offset. + */ + if (ra_ofs + offsetof(RESTART_AREA, file_size) > + NTFS_BLOCK_SIZE - sizeof(u16)) { + ntfs_log_error("$LogFile restart area specifies " + "inconsistent file offset.\n"); + return FALSE; + } + /* + * Now that we can access ra->client_array_offset, make sure everything + * up to the log client array is before the first word protected by an + * update sequence number. This ensures we can access all of the + * restart area elements safely. Also, the client array offset must be + * aligned to an 8-byte boundary. + */ + ca_ofs = le16_to_cpu(ra->client_array_offset); + if (((ca_ofs + 7) & ~7) != ca_ofs || + ra_ofs + ca_ofs > (u16)(NTFS_BLOCK_SIZE - + sizeof(u16))) { + ntfs_log_error("$LogFile restart area specifies " + "inconsistent client array offset.\n"); + return FALSE; + } + /* + * The restart area must end within the system page size both when + * calculated manually and as specified by ra->restart_area_length. + * Also, the calculated length must not exceed the specified length. + */ + ra_len = ca_ofs + le16_to_cpu(ra->log_clients) * + sizeof(LOG_CLIENT_RECORD); + if ((u32)(ra_ofs + ra_len) > le32_to_cpu(rp->system_page_size) || + (u32)(ra_ofs + le16_to_cpu(ra->restart_area_length)) > + le32_to_cpu(rp->system_page_size) || + ra_len > le16_to_cpu(ra->restart_area_length)) { + ntfs_log_error("$LogFile restart area is out of bounds " + "of the system page size specified by the " + "restart page header and/or the specified " + "restart area length is inconsistent.\n"); + return FALSE; + } + /* + * The ra->client_free_list and ra->client_in_use_list must be either + * LOGFILE_NO_CLIENT or less than ra->log_clients or they are + * overflowing the client array. + */ + if ((ra->client_free_list != LOGFILE_NO_CLIENT && + le16_to_cpu(ra->client_free_list) >= + le16_to_cpu(ra->log_clients)) || + (ra->client_in_use_list != LOGFILE_NO_CLIENT && + le16_to_cpu(ra->client_in_use_list) >= + le16_to_cpu(ra->log_clients))) { + ntfs_log_error("$LogFile restart area specifies " + "overflowing client free and/or in use lists.\n"); + return FALSE; + } + /* + * Check ra->seq_number_bits against ra->file_size for consistency. + * We cannot just use ffs() because the file size is not a power of 2. + */ + file_size = (u64)sle64_to_cpu(ra->file_size); + fs_bits = 0; + while (file_size) { + file_size >>= 1; + fs_bits++; + } + if (le32_to_cpu(ra->seq_number_bits) != (u32)(67 - fs_bits)) { + ntfs_log_error("$LogFile restart area specifies " + "inconsistent sequence number bits.\n"); + return FALSE; + } + /* The log record header length must be a multiple of 8. */ + if (((le16_to_cpu(ra->log_record_header_length) + 7) & ~7) != + le16_to_cpu(ra->log_record_header_length)) { + ntfs_log_error("$LogFile restart area specifies " + "inconsistent log record header length.\n"); + return FALSE; + } + /* Ditto for the log page data offset. */ + if (((le16_to_cpu(ra->log_page_data_offset) + 7) & ~7) != + le16_to_cpu(ra->log_page_data_offset)) { + ntfs_log_error("$LogFile restart area specifies " + "inconsistent log page data offset.\n"); + return FALSE; + } + ntfs_log_trace("Done.\n"); + return TRUE; +} + +/** + * ntfs_check_log_client_array - check the log client array for consistency + * @rp: restart page whose log client array to check + * + * Check the log client array of the restart page @rp for consistency and + * return TRUE if it is consistent and FALSE otherwise. + * + * This function assumes that the restart page header and the restart area have + * already been consistency checked. + * + * Unlike ntfs_check_restart_page_header() and ntfs_check_restart_area(), this + * function needs @rp->system_page_size bytes in @rp, i.e. it requires the full + * restart page and the page must be multi sector transfer deprotected. + */ +static BOOL ntfs_check_log_client_array(RESTART_PAGE_HEADER *rp) +{ + RESTART_AREA *ra; + LOG_CLIENT_RECORD *ca, *cr; + u16 nr_clients, idx; + BOOL in_free_list, idx_is_first; + u32 offset_clients; + + ntfs_log_trace("Entering.\n"); + /* The restart area must be fully within page */ + if ((le16_to_cpu(rp->restart_area_offset) + sizeof(RESTART_AREA)) + > le32_to_cpu(rp->system_page_size)) + goto err_out; + ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); + offset_clients = le16_to_cpu(rp->restart_area_offset) + + le16_to_cpu(ra->client_array_offset); + /* The clients' records must begin within page */ + if (offset_clients >= le32_to_cpu(rp->system_page_size)) + goto err_out; + ca = (LOG_CLIENT_RECORD*)((u8*)ra + + le16_to_cpu(ra->client_array_offset)); + /* + * Check the ra->client_free_list first and then check the + * ra->client_in_use_list. Check each of the log client records in + * each of the lists and check that the array does not overflow the + * ra->log_clients value. Also keep track of the number of records + * visited as there cannot be more than ra->log_clients records and + * that way we detect eventual loops in within a list. + */ + nr_clients = le16_to_cpu(ra->log_clients); + idx = le16_to_cpu(ra->client_free_list); + in_free_list = TRUE; +check_list: + for (idx_is_first = TRUE; idx != LOGFILE_NO_CLIENT_CPU; nr_clients--, + idx = le16_to_cpu(cr->next_client)) { + if (!nr_clients || idx >= le16_to_cpu(ra->log_clients)) + goto err_out; + /* The client record must be fully within page */ + if ((offset_clients + (idx + 1)*sizeof(LOG_CLIENT_RECORD)) + > le32_to_cpu(rp->system_page_size)) + goto err_out; + /* Set @cr to the current log client record. */ + cr = ca + idx; + /* The first log client record must not have a prev_client. */ + if (idx_is_first) { + if (cr->prev_client != LOGFILE_NO_CLIENT) + goto err_out; + idx_is_first = FALSE; + } + } + /* Switch to and check the in use list if we just did the free list. */ + if (in_free_list) { + in_free_list = FALSE; + idx = le16_to_cpu(ra->client_in_use_list); + goto check_list; + } + ntfs_log_trace("Done.\n"); + return TRUE; +err_out: + ntfs_log_error("$LogFile log client array is corrupt.\n"); + return FALSE; +} + +/** + * ntfs_check_and_load_restart_page - check the restart page for consistency + * @log_na: opened ntfs attribute for journal $LogFile + * @rp: restart page to check + * @pos: position in @log_na at which the restart page resides + * @wrp: [OUT] copy of the multi sector transfer deprotected restart page + * @lsn: [OUT] set to the current logfile lsn on success + * + * Check the restart page @rp for consistency and return 0 if it is consistent + * and errno otherwise. The restart page may have been modified by chkdsk in + * which case its magic is CHKD instead of RSTR. + * + * This function only needs NTFS_BLOCK_SIZE bytes in @rp, i.e. it does not + * require the full restart page. + * + * If @wrp is not NULL, on success, *@wrp will point to a buffer containing a + * copy of the complete multi sector transfer deprotected page. On failure, + * *@wrp is undefined. + * + * Similarly, if @lsn is not NULL, on success *@lsn will be set to the current + * logfile lsn according to this restart page. On failure, *@lsn is undefined. + * + * The following error codes are defined: + * EINVAL - The restart page is inconsistent. + * ENOMEM - Not enough memory to load the restart page. + * EIO - Failed to reading from $LogFile. + */ +static int ntfs_check_and_load_restart_page(ntfs_attr *log_na, + RESTART_PAGE_HEADER *rp, s64 pos, RESTART_PAGE_HEADER **wrp, + LSN *lsn) +{ + RESTART_AREA *ra; + RESTART_PAGE_HEADER *trp; + int err; + + ntfs_log_trace("Entering.\n"); + /* Check the restart page header for consistency. */ + if (!ntfs_check_restart_page_header(rp, pos)) { + /* Error output already done inside the function. */ + return EINVAL; + } + /* Check the restart area for consistency. */ + if (!ntfs_check_restart_area(rp)) { + /* Error output already done inside the function. */ + return EINVAL; + } + ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); + /* + * Allocate a buffer to store the whole restart page so we can multi + * sector transfer deprotect it. + * For safety, make sure this is consistent with the usa_count + * and shorter than the full log size + */ + if ((le32_to_cpu(rp->system_page_size) + > (u32)(le16_to_cpu(rp->usa_count) - 1)*NTFS_BLOCK_SIZE) + || (le32_to_cpu(rp->system_page_size) + > le64_to_cpu(log_na->data_size))) + return (EINVAL); + trp = ntfs_malloc(le32_to_cpu(rp->system_page_size)); + if (!trp) + return errno; + /* + * Read the whole of the restart page into the buffer. If it fits + * completely inside @rp, just copy it from there. Otherwise read it + * from disk. + */ + if (le32_to_cpu(rp->system_page_size) <= NTFS_BLOCK_SIZE) + memcpy(trp, rp, le32_to_cpu(rp->system_page_size)); + else if (ntfs_attr_pread(log_na, pos, + le32_to_cpu(rp->system_page_size), trp) != + le32_to_cpu(rp->system_page_size)) { + err = errno; + ntfs_log_error("Failed to read whole restart page into the " + "buffer.\n"); + if (err != ENOMEM) + err = EIO; + goto err_out; + } + /* + * Perform the multi sector transfer deprotection on the buffer if the + * restart page is protected. + */ + if ((!ntfs_is_chkd_record(trp->magic) || le16_to_cpu(trp->usa_count)) + && ntfs_mst_post_read_fixup((NTFS_RECORD*)trp, + le32_to_cpu(rp->system_page_size))) { + /* + * A multi sector tranfer error was detected. We only need to + * abort if the restart page contents exceed the multi sector + * transfer fixup of the first sector. + */ + if (le16_to_cpu(rp->restart_area_offset) + + le16_to_cpu(ra->restart_area_length) > + NTFS_BLOCK_SIZE - (int)sizeof(u16)) { + ntfs_log_error("Multi sector transfer error " + "detected in $LogFile restart page.\n"); + err = EINVAL; + goto err_out; + } + } + /* + * If the restart page is modified by chkdsk or there are no active + * logfile clients, the logfile is consistent. Otherwise, need to + * check the log client records for consistency, too. + */ + err = 0; + if (ntfs_is_rstr_record(rp->magic) && + ra->client_in_use_list != LOGFILE_NO_CLIENT) { + if (!ntfs_check_log_client_array(trp)) { + err = EINVAL; + goto err_out; + } + } + if (lsn) { + if (ntfs_is_rstr_record(rp->magic)) + *lsn = sle64_to_cpu(ra->current_lsn); + else /* if (ntfs_is_chkd_record(rp->magic)) */ + *lsn = sle64_to_cpu(rp->chkdsk_lsn); + } + ntfs_log_trace("Done.\n"); + if (wrp) + *wrp = trp; + else { +err_out: + free(trp); + } + return err; +} + +/** + * ntfs_check_logfile - check in the journal if the volume is consistent + * @log_na: ntfs attribute of loaded journal $LogFile to check + * @rp: [OUT] on success this is a copy of the current restart page + * + * Check the $LogFile journal for consistency and return TRUE if it is + * consistent and FALSE if not. On success, the current restart page is + * returned in *@rp. Caller must call ntfs_free(*@rp) when finished with it. + * + * At present we only check the two restart pages and ignore the log record + * pages. + * + * Note that the MstProtected flag is not set on the $LogFile inode and hence + * when reading pages they are not deprotected. This is because we do not know + * if the $LogFile was created on a system with a different page size to ours + * yet and mst deprotection would fail if our page size is smaller. + */ +BOOL ntfs_check_logfile(ntfs_attr *log_na, RESTART_PAGE_HEADER **rp) +{ + s64 size, pos; + LSN rstr1_lsn, rstr2_lsn; + ntfs_volume *vol = log_na->ni->vol; + u8 *kaddr = NULL; + RESTART_PAGE_HEADER *rstr1_ph = NULL; + RESTART_PAGE_HEADER *rstr2_ph = NULL; + int log_page_size, err; + BOOL logfile_is_empty = TRUE; + u8 log_page_bits; + + ntfs_log_trace("Entering.\n"); + /* An empty $LogFile must have been clean before it got emptied. */ + if (NVolLogFileEmpty(vol)) + goto is_empty; + size = log_na->data_size; + /* Make sure the file doesn't exceed the maximum allowed size. */ + if (size > (s64)MaxLogFileSize) + size = MaxLogFileSize; + log_page_size = DefaultLogPageSize; + /* + * Use generic_ffs() instead of ffs() to enable the compiler to + * optimize log_page_size and log_page_bits into constants. + */ + log_page_bits = ffs(log_page_size) - 1; + size &= ~(log_page_size - 1); + + /* + * Ensure the log file is big enough to store at least the two restart + * pages and the minimum number of log record pages. + */ + if (size < log_page_size * 2 || (size - log_page_size * 2) >> + log_page_bits < MinLogRecordPages) { + ntfs_log_error("$LogFile is too small.\n"); + return FALSE; + } + /* Allocate memory for restart page. */ + kaddr = ntfs_malloc(NTFS_BLOCK_SIZE); + if (!kaddr) + return FALSE; + /* + * Read through the file looking for a restart page. Since the restart + * page header is at the beginning of a page we only need to search at + * what could be the beginning of a page (for each page size) rather + * than scanning the whole file byte by byte. If all potential places + * contain empty and uninitialized records, the log file can be assumed + * to be empty. + */ + for (pos = 0; pos < size; pos <<= 1) { + /* + * Read first NTFS_BLOCK_SIZE bytes of potential restart page. + */ + if (ntfs_attr_pread(log_na, pos, NTFS_BLOCK_SIZE, kaddr) != + NTFS_BLOCK_SIZE) { + ntfs_log_error("Failed to read first NTFS_BLOCK_SIZE " + "bytes of potential restart page.\n"); + goto err_out; + } + + /* + * A non-empty block means the logfile is not empty while an + * empty block after a non-empty block has been encountered + * means we are done. + */ + if (!ntfs_is_empty_recordp((le32*)kaddr)) + logfile_is_empty = FALSE; + else if (!logfile_is_empty) + break; + /* + * A log record page means there cannot be a restart page after + * this so no need to continue searching. + */ + if (ntfs_is_rcrd_recordp((le32*)kaddr)) + break; + /* If not a (modified by chkdsk) restart page, continue. */ + if (!ntfs_is_rstr_recordp((le32*)kaddr) && + !ntfs_is_chkd_recordp((le32*)kaddr)) { + if (!pos) + pos = NTFS_BLOCK_SIZE >> 1; + continue; + } + /* + * Check the (modified by chkdsk) restart page for consistency + * and get a copy of the complete multi sector transfer + * deprotected restart page. + */ + err = ntfs_check_and_load_restart_page(log_na, + (RESTART_PAGE_HEADER*)kaddr, pos, + !rstr1_ph ? &rstr1_ph : &rstr2_ph, + !rstr1_ph ? &rstr1_lsn : &rstr2_lsn); + if (!err) { + /* + * If we have now found the first (modified by chkdsk) + * restart page, continue looking for the second one. + */ + if (!pos) { + pos = NTFS_BLOCK_SIZE >> 1; + continue; + } + /* + * We have now found the second (modified by chkdsk) + * restart page, so we can stop looking. + */ + break; + } + /* + * Error output already done inside the function. Note, we do + * not abort if the restart page was invalid as we might still + * find a valid one further in the file. + */ + if (err != EINVAL) + goto err_out; + /* Continue looking. */ + if (!pos) + pos = NTFS_BLOCK_SIZE >> 1; + } + if (kaddr) { + free(kaddr); + kaddr = NULL; + } + if (logfile_is_empty) { + NVolSetLogFileEmpty(vol); +is_empty: + ntfs_log_trace("Done. ($LogFile is empty.)\n"); + return TRUE; + } + if (!rstr1_ph) { + if (rstr2_ph) + ntfs_log_error("BUG: rstr2_ph isn't NULL!\n"); + ntfs_log_error("Did not find any restart pages in " + "$LogFile and it was not empty.\n"); + return FALSE; + } + /* If both restart pages were found, use the more recent one. */ + if (rstr2_ph) { + /* + * If the second restart area is more recent, switch to it. + * Otherwise just throw it away. + */ + if (rstr2_lsn > rstr1_lsn) { + ntfs_log_debug("Using second restart page as it is more " + "recent.\n"); + free(rstr1_ph); + rstr1_ph = rstr2_ph; + /* rstr1_lsn = rstr2_lsn; */ + } else { + ntfs_log_debug("Using first restart page as it is more " + "recent.\n"); + free(rstr2_ph); + } + rstr2_ph = NULL; + } + /* All consistency checks passed. */ + if (rp) + *rp = rstr1_ph; + else + free(rstr1_ph); + ntfs_log_trace("Done.\n"); + return TRUE; +err_out: + free(kaddr); + free(rstr1_ph); + free(rstr2_ph); + return FALSE; +} + +/** + * ntfs_is_logfile_clean - check in the journal if the volume is clean + * @log_na: ntfs attribute of loaded journal $LogFile to check + * @rp: copy of the current restart page + * + * Analyze the $LogFile journal and return TRUE if it indicates the volume was + * shutdown cleanly and FALSE if not. + * + * At present we only look at the two restart pages and ignore the log record + * pages. This is a little bit crude in that there will be a very small number + * of cases where we think that a volume is dirty when in fact it is clean. + * This should only affect volumes that have not been shutdown cleanly but did + * not have any pending, non-check-pointed i/o, i.e. they were completely idle + * at least for the five seconds preceding the unclean shutdown. + * + * This function assumes that the $LogFile journal has already been consistency + * checked by a call to ntfs_check_logfile() and in particular if the $LogFile + * is empty this function requires that NVolLogFileEmpty() is true otherwise an + * empty volume will be reported as dirty. + */ +BOOL ntfs_is_logfile_clean(ntfs_attr *log_na, RESTART_PAGE_HEADER *rp) +{ + RESTART_AREA *ra; + + ntfs_log_trace("Entering.\n"); + /* An empty $LogFile must have been clean before it got emptied. */ + if (NVolLogFileEmpty(log_na->ni->vol)) { + ntfs_log_trace("$LogFile is empty\n"); + return TRUE; + } + if (!rp) { + ntfs_log_error("Restart page header is NULL\n"); + return FALSE; + } + if (!ntfs_is_rstr_record(rp->magic) && + !ntfs_is_chkd_record(rp->magic)) { + ntfs_log_error("Restart page buffer is invalid\n"); + return FALSE; + } + + ra = (RESTART_AREA*)((u8*)rp + le16_to_cpu(rp->restart_area_offset)); + /* + * If the $LogFile has active clients, i.e. it is open, and we do not + * have the RESTART_VOLUME_IS_CLEAN bit set in the restart area flags, + * we assume there was an unclean shutdown. + */ + if (ra->client_in_use_list != LOGFILE_NO_CLIENT && + !(ra->flags & RESTART_VOLUME_IS_CLEAN)) { + ntfs_log_error("The disk contains an unclean file system (%d, " + "%d).\n", le16_to_cpu(ra->client_in_use_list), + le16_to_cpu(ra->flags)); + return FALSE; + } + /* $LogFile indicates a clean shutdown. */ + ntfs_log_trace("$LogFile indicates a clean shutdown\n"); + return TRUE; +} + +/** + * ntfs_empty_logfile - empty the contents of the $LogFile journal + * @na: ntfs attribute of journal $LogFile to empty + * + * Empty the contents of the $LogFile journal @na and return 0 on success and + * -1 on error. + * + * This function assumes that the $LogFile journal has already been consistency + * checked by a call to ntfs_check_logfile() and that ntfs_is_logfile_clean() + * has been used to ensure that the $LogFile is clean. + */ +int ntfs_empty_logfile(ntfs_attr *na) +{ + s64 pos, count; + char buf[NTFS_BUF_SIZE]; + + ntfs_log_trace("Entering.\n"); + + if (NVolLogFileEmpty(na->ni->vol)) + return 0; + + if (!NAttrNonResident(na)) { + errno = EIO; + ntfs_log_perror("Resident $LogFile $DATA attribute"); + return -1; + } + + memset(buf, -1, NTFS_BUF_SIZE); + + pos = 0; + while ((count = na->data_size - pos) > 0) { + + if (count > NTFS_BUF_SIZE) + count = NTFS_BUF_SIZE; + + count = ntfs_attr_pwrite(na, pos, count, buf); + if (count <= 0) { + ntfs_log_perror("Failed to reset $LogFile"); + if (count != -1) + errno = EIO; + return -1; + } + pos += count; + } + + NVolSetLogFileEmpty(na->ni->vol); + + return 0; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/logging.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/logging.c new file mode 100644 index 0000000..ccccbb8 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/logging.c @@ -0,0 +1,637 @@ +/** + * logging.c - Centralised logging. Originated from the Linux-NTFS project. + * + * Copyright (c) 2005 Richard Russon + * Copyright (c) 2005-2008 Szabolcs Szakacsits + * Copyright (c) 2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_SYSLOG_H +#include +#endif + +#include "logging.h" +#include "misc.h" + +#ifndef PATH_SEP +#define PATH_SEP '/' +#endif + +#ifdef DEBUG +static int tab; +#endif + +/* Some gcc 3.x, 4.[01].X crash with internal compiler error. */ +#if __GNUC__ <= 3 || (__GNUC__ == 4 && __GNUC_MINOR__ <= 1) +# define BROKEN_GCC_FORMAT_ATTRIBUTE +#else +# define BROKEN_GCC_FORMAT_ATTRIBUTE __attribute__((format(printf, 6, 0))) +#endif + +/** + * struct ntfs_logging - Control info for the logging system + * @levels: Bitfield of logging levels + * @flags: Flags which affect the output style + * @handler: Function to perform the actual logging + */ +struct ntfs_logging { + u32 levels; + u32 flags; + ntfs_log_handler *handler BROKEN_GCC_FORMAT_ATTRIBUTE; +}; + +/** + * ntfs_log + * This struct controls all the logging within the library and tools. + */ +static struct ntfs_logging ntfs_log = { +#ifdef DEBUG + NTFS_LOG_LEVEL_DEBUG | NTFS_LOG_LEVEL_TRACE | NTFS_LOG_LEVEL_ENTER | + NTFS_LOG_LEVEL_LEAVE | +#endif + NTFS_LOG_LEVEL_INFO | NTFS_LOG_LEVEL_QUIET | NTFS_LOG_LEVEL_WARNING | + NTFS_LOG_LEVEL_ERROR | NTFS_LOG_LEVEL_PERROR | NTFS_LOG_LEVEL_CRITICAL | + NTFS_LOG_LEVEL_PROGRESS, + NTFS_LOG_FLAG_ONLYNAME, +#ifdef DEBUG + ntfs_log_handler_outerr +#else + ntfs_log_handler_null +#endif +}; + + +/** + * ntfs_log_get_levels - Get a list of the current logging levels + * + * Find out which logging levels are enabled. + * + * Returns: Log levels in a 32-bit field + */ +u32 ntfs_log_get_levels(void) +{ + return ntfs_log.levels; +} + +/** + * ntfs_log_set_levels - Enable extra logging levels + * @levels: 32-bit field of log levels to set + * + * Enable one or more logging levels. + * The logging levels are named: NTFS_LOG_LEVEL_*. + * + * Returns: Log levels that were enabled before the call + */ +u32 ntfs_log_set_levels(u32 levels) +{ + u32 old; + old = ntfs_log.levels; + ntfs_log.levels |= levels; + return old; +} + +/** + * ntfs_log_clear_levels - Disable some logging levels + * @levels: 32-bit field of log levels to clear + * + * Disable one or more logging levels. + * The logging levels are named: NTFS_LOG_LEVEL_*. + * + * Returns: Log levels that were enabled before the call + */ +u32 ntfs_log_clear_levels(u32 levels) +{ + u32 old; + old = ntfs_log.levels; + ntfs_log.levels &= (~levels); + return old; +} + + +/** + * ntfs_log_get_flags - Get a list of logging style flags + * + * Find out which logging flags are enabled. + * + * Returns: Logging flags in a 32-bit field + */ +u32 ntfs_log_get_flags(void) +{ + return ntfs_log.flags; +} + +/** + * ntfs_log_set_flags - Enable extra logging style flags + * @flags: 32-bit field of logging flags to set + * + * Enable one or more logging flags. + * The log flags are named: NTFS_LOG_LEVEL_*. + * + * Returns: Logging flags that were enabled before the call + */ +u32 ntfs_log_set_flags(u32 flags) +{ + u32 old; + old = ntfs_log.flags; + ntfs_log.flags |= flags; + return old; +} + +/** + * ntfs_log_clear_flags - Disable some logging styles + * @flags: 32-bit field of logging flags to clear + * + * Disable one or more logging flags. + * The log flags are named: NTFS_LOG_LEVEL_*. + * + * Returns: Logging flags that were enabled before the call + */ +u32 ntfs_log_clear_flags(u32 flags) +{ + u32 old; + old = ntfs_log.flags; + ntfs_log.flags &= (~flags); + return old; +} + + +/** + * ntfs_log_get_stream - Default output streams for logging levels + * @level: Log level + * + * By default, urgent messages are sent to "stderr". + * Other messages are sent to "stdout". + * + * Returns: "string" Prefix to be used + */ +static FILE * ntfs_log_get_stream(u32 level) +{ + FILE *stream; + + switch (level) { + case NTFS_LOG_LEVEL_INFO: + case NTFS_LOG_LEVEL_QUIET: + case NTFS_LOG_LEVEL_PROGRESS: + case NTFS_LOG_LEVEL_VERBOSE: + stream = stdout; + break; + + case NTFS_LOG_LEVEL_DEBUG: + case NTFS_LOG_LEVEL_TRACE: + case NTFS_LOG_LEVEL_ENTER: + case NTFS_LOG_LEVEL_LEAVE: + case NTFS_LOG_LEVEL_WARNING: + case NTFS_LOG_LEVEL_ERROR: + case NTFS_LOG_LEVEL_CRITICAL: + case NTFS_LOG_LEVEL_PERROR: + default: + stream = stderr; + break; + } + + return stream; +} + +/** + * ntfs_log_get_prefix - Default prefixes for logging levels + * @level: Log level to be prefixed + * + * Prefixing the logging output can make it easier to parse. + * + * Returns: "string" Prefix to be used + */ +static const char * ntfs_log_get_prefix(u32 level) +{ + const char *prefix; + + switch (level) { + case NTFS_LOG_LEVEL_DEBUG: + prefix = "DEBUG: "; + break; + case NTFS_LOG_LEVEL_TRACE: + prefix = "TRACE: "; + break; + case NTFS_LOG_LEVEL_QUIET: + prefix = "QUIET: "; + break; + case NTFS_LOG_LEVEL_INFO: + prefix = "INFO: "; + break; + case NTFS_LOG_LEVEL_VERBOSE: + prefix = "VERBOSE: "; + break; + case NTFS_LOG_LEVEL_PROGRESS: + prefix = "PROGRESS: "; + break; + case NTFS_LOG_LEVEL_WARNING: + prefix = "WARNING: "; + break; + case NTFS_LOG_LEVEL_ERROR: + prefix = "ERROR: "; + break; + case NTFS_LOG_LEVEL_PERROR: + prefix = "ERROR: "; + break; + case NTFS_LOG_LEVEL_CRITICAL: + prefix = "CRITICAL: "; + break; + default: + prefix = ""; + break; + } + + return prefix; +} + + +/** + * ntfs_log_set_handler - Provide an alternate logging handler + * @handler: function to perform the logging + * + * This alternate handler will be called for all future logging requests. + * If no @handler is specified, logging will revert to the default handler. + */ +void ntfs_log_set_handler(ntfs_log_handler *handler) +{ + if (handler) { + ntfs_log.handler = handler; +#ifdef HAVE_SYSLOG_H + if (handler == ntfs_log_handler_syslog) + openlog("ntfs-3g", LOG_PID, LOG_USER); +#endif + } else + ntfs_log.handler = ntfs_log_handler_null; +} + +/** + * ntfs_log_redirect - Pass on the request to the real handler + * @function: Function in which the log line occurred + * @file: File in which the log line occurred + * @line: Line number on which the log line occurred + * @level: Level at which the line is logged + * @data: User specified data, possibly specific to a handler + * @format: printf-style formatting string + * @...: Arguments to be formatted + * + * This is just a redirector function. The arguments are simply passed to the + * main logging handler (as defined in the global logging struct @ntfs_log). + * + * Returns: -1 Error occurred + * 0 Message wasn't logged + * num Number of output characters + */ +int ntfs_log_redirect(const char *function, const char *file, + int line, u32 level, void *data, const char *format, ...) +{ + int olderr = errno; + int ret; + va_list args; + + if (!(ntfs_log.levels & level)) /* Don't log this message */ + return 0; + + va_start(args, format); + errno = olderr; + ret = ntfs_log.handler(function, file, line, level, data, format, args); + va_end(args); + + errno = olderr; + return ret; +} + + +/** + * ntfs_log_handler_syslog - syslog logging handler + * @function: Function in which the log line occurred + * @file: File in which the log line occurred + * @line: Line number on which the log line occurred + * @level: Level at which the line is logged + * @data: User specified data, possibly specific to a handler + * @format: printf-style formatting string + * @args: Arguments to be formatted + * + * A simple syslog logging handler. Ignores colors. + * + * Returns: -1 Error occurred + * 0 Message wasn't logged + * num Number of output characters + */ + + +#ifdef HAVE_SYSLOG_H + +#define LOG_LINE_LEN 512 + +int ntfs_log_handler_syslog(const char *function __attribute__((unused)), + const char *file __attribute__((unused)), + int line __attribute__((unused)), u32 level, + void *data __attribute__((unused)), + const char *format, va_list args) +{ + char logbuf[LOG_LINE_LEN]; + int ret, olderr = errno; + +#ifndef DEBUG + if ((level & NTFS_LOG_LEVEL_PERROR) && errno == ENOSPC) + return 1; +#endif + ret = vsnprintf(logbuf, LOG_LINE_LEN, format, args); + if (ret < 0) { + vsyslog(LOG_NOTICE, format, args); + ret = 1; + goto out; + } + + if ((LOG_LINE_LEN > ret + 3) && (level & NTFS_LOG_LEVEL_PERROR)) { + strncat(logbuf, ": ", LOG_LINE_LEN - ret - 1); + strncat(logbuf, strerror(olderr), LOG_LINE_LEN - (ret + 3)); + ret = strlen(logbuf); + } + + syslog(LOG_NOTICE, "%s", logbuf); +out: + errno = olderr; + return ret; +} +#endif + +/* + * Early logging before the logs are redirected + * + * (not quite satisfactory : this appears before the ntfs-g banner, + * and with a different pid) + */ + +void ntfs_log_early_error(const char *format, ...) +{ + va_list args; + + va_start(args, format); +#ifdef HAVE_SYSLOG_H + openlog("ntfs-3g", LOG_PID, LOG_USER); + ntfs_log_handler_syslog(NULL, NULL, 0, + NTFS_LOG_LEVEL_ERROR, NULL, + format, args); +#else + vfprintf(stderr,format,args); +#endif + va_end(args); +} + +/** + * ntfs_log_handler_fprintf - Basic logging handler + * @function: Function in which the log line occurred + * @file: File in which the log line occurred + * @line: Line number on which the log line occurred + * @level: Level at which the line is logged + * @data: User specified data, possibly specific to a handler + * @format: printf-style formatting string + * @args: Arguments to be formatted + * + * A simple logging handler. This is where the log line is finally displayed. + * It is more likely that you will want to set the handler to either + * ntfs_log_handler_outerr or ntfs_log_handler_stderr. + * + * Note: For this handler, @data is a pointer to a FILE output stream. + * If @data is NULL, nothing will be displayed. + * + * Returns: -1 Error occurred + * 0 Message wasn't logged + * num Number of output characters + */ +int ntfs_log_handler_fprintf(const char *function, const char *file, + int line, u32 level, void *data, const char *format, va_list args) +{ +#ifdef DEBUG + int i; +#endif + int ret = 0; + int olderr = errno; + FILE *stream; + + if (!data) /* Interpret data as a FILE stream. */ + return 0; /* If it's NULL, we can't do anything. */ + stream = (FILE*)data; + +#ifdef DEBUG + if (level == NTFS_LOG_LEVEL_LEAVE) { + if (tab) + tab--; + return 0; + } + + for (i = 0; i < tab; i++) + ret += fprintf(stream, " "); +#endif + if ((ntfs_log.flags & NTFS_LOG_FLAG_ONLYNAME) && + (strchr(file, PATH_SEP))) /* Abbreviate the filename */ + file = strrchr(file, PATH_SEP) + 1; + + if (ntfs_log.flags & NTFS_LOG_FLAG_PREFIX) /* Prefix the output */ + ret += fprintf(stream, "%s", ntfs_log_get_prefix(level)); + + if (ntfs_log.flags & NTFS_LOG_FLAG_FILENAME) /* Source filename */ + ret += fprintf(stream, "%s ", file); + + if (ntfs_log.flags & NTFS_LOG_FLAG_LINE) /* Source line number */ + ret += fprintf(stream, "(%d) ", line); + + if ((ntfs_log.flags & NTFS_LOG_FLAG_FUNCTION) || /* Source function */ + (level & NTFS_LOG_LEVEL_TRACE) || (level & NTFS_LOG_LEVEL_ENTER)) + ret += fprintf(stream, "%s(): ", function); + + ret += vfprintf(stream, format, args); + + if (level & NTFS_LOG_LEVEL_PERROR) + ret += fprintf(stream, ": %s\n", strerror(olderr)); + +#ifdef DEBUG + if (level == NTFS_LOG_LEVEL_ENTER) + tab++; +#endif + fflush(stream); + errno = olderr; + return ret; +} + +/** + * ntfs_log_handler_null - Null logging handler (no output) + * @function: Function in which the log line occurred + * @file: File in which the log line occurred + * @line: Line number on which the log line occurred + * @level: Level at which the line is logged + * @data: User specified data, possibly specific to a handler + * @format: printf-style formatting string + * @args: Arguments to be formatted + * + * This handler produces no output. It provides a way to temporarily disable + * logging, without having to change the levels and flags. + * + * Returns: 0 Message wasn't logged + */ +int ntfs_log_handler_null(const char *function __attribute__((unused)), const char *file __attribute__((unused)), + int line __attribute__((unused)), u32 level __attribute__((unused)), void *data __attribute__((unused)), + const char *format __attribute__((unused)), va_list args __attribute__((unused))) +{ + return 0; +} + +/** + * ntfs_log_handler_stdout - All logs go to stdout + * @function: Function in which the log line occurred + * @file: File in which the log line occurred + * @line: Line number on which the log line occurred + * @level: Level at which the line is logged + * @data: User specified data, possibly specific to a handler + * @format: printf-style formatting string + * @args: Arguments to be formatted + * + * Display a log message to stdout. + * + * Note: For this handler, @data is a pointer to a FILE output stream. + * If @data is NULL, then stdout will be used. + * + * Note: This function calls ntfs_log_handler_fprintf to do the main work. + * + * Returns: -1 Error occurred + * 0 Message wasn't logged + * num Number of output characters + */ +int ntfs_log_handler_stdout(const char *function, const char *file, + int line, u32 level, void *data, const char *format, va_list args) +{ + if (!data) + data = stdout; + + return ntfs_log_handler_fprintf(function, file, line, level, data, format, args); +} + +/** + * ntfs_log_handler_outerr - Logs go to stdout/stderr depending on level + * @function: Function in which the log line occurred + * @file: File in which the log line occurred + * @line: Line number on which the log line occurred + * @level: Level at which the line is logged + * @data: User specified data, possibly specific to a handler + * @format: printf-style formatting string + * @args: Arguments to be formatted + * + * Display a log message. The output stream will be determined by the log + * level. + * + * Note: For this handler, @data is a pointer to a FILE output stream. + * If @data is NULL, the function ntfs_log_get_stream will be called + * + * Note: This function calls ntfs_log_handler_fprintf to do the main work. + * + * Returns: -1 Error occurred + * 0 Message wasn't logged + * num Number of output characters + */ +int ntfs_log_handler_outerr(const char *function, const char *file, + int line, u32 level, void *data, const char *format, va_list args) +{ + if (!data) + data = ntfs_log_get_stream(level); + + return ntfs_log_handler_fprintf(function, file, line, level, data, format, args); +} + +/** + * ntfs_log_handler_stderr - All logs go to stderr + * @function: Function in which the log line occurred + * @file: File in which the log line occurred + * @line: Line number on which the log line occurred + * @level: Level at which the line is logged + * @data: User specified data, possibly specific to a handler + * @format: printf-style formatting string + * @args: Arguments to be formatted + * + * Display a log message to stderr. + * + * Note: For this handler, @data is a pointer to a FILE output stream. + * If @data is NULL, then stdout will be used. + * + * Note: This function calls ntfs_log_handler_fprintf to do the main work. + * + * Returns: -1 Error occurred + * 0 Message wasn't logged + * num Number of output characters + */ +int ntfs_log_handler_stderr(const char *function, const char *file, + int line, u32 level, void *data, const char *format, va_list args) +{ + if (!data) + data = stderr; + + return ntfs_log_handler_fprintf(function, file, line, level, data, format, args); +} + + +/** + * ntfs_log_parse_option - Act upon command line options + * @option: Option flag + * + * Delegate some of the work of parsing the command line. All the options begin + * with "--log-". Options cause log levels to be enabled in @ntfs_log (the + * global logging structure). + * + * Note: The "colour" option changes the logging handler. + * + * Returns: TRUE Option understood + * FALSE Invalid log option + */ +BOOL ntfs_log_parse_option(const char *option) +{ + if (strcmp(option, "--log-debug") == 0) { + ntfs_log_set_levels(NTFS_LOG_LEVEL_DEBUG); + return TRUE; + } else if (strcmp(option, "--log-verbose") == 0) { + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + return TRUE; + } else if (strcmp(option, "--log-quiet") == 0) { + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + return TRUE; + } else if (strcmp(option, "--log-trace") == 0) { + ntfs_log_set_levels(NTFS_LOG_LEVEL_TRACE); + return TRUE; + } + + ntfs_log_debug("Unknown logging option '%s'\n", option); + return FALSE; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/mft.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/mft.c new file mode 100644 index 0000000..cd2b415 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/mft.c @@ -0,0 +1,2059 @@ +/** + * mft.c - Mft record handling code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2004 Anton Altaparmakov + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2004-2008 Szabolcs Szakacsits + * Copyright (c) 2005 Yura Pakhuchiy + * Copyright (c) 2014-2021 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif +#include + +#include "compat.h" +#include "types.h" +#include "device.h" +#include "debug.h" +#include "bitmap.h" +#include "attrib.h" +#include "inode.h" +#include "volume.h" +#include "layout.h" +#include "lcnalloc.h" +#include "mft.h" +#include "logging.h" +#include "misc.h" + +/** + * ntfs_mft_records_read - read records from the mft from disk + * @vol: volume to read from + * @mref: starting mft record number to read + * @count: number of mft records to read + * @b: output data buffer + * + * Read @count mft records starting at @mref from volume @vol into buffer + * @b. Return 0 on success or -1 on error, with errno set to the error + * code. + * + * If any of the records exceed the initialized size of the $MFT/$DATA + * attribute, i.e. they cannot possibly be allocated mft records, assume this + * is a bug and return error code ESPIPE. + * + * The read mft records are mst deprotected and are hence ready to use. The + * caller should check each record with is_baad_record() in case mst + * deprotection failed. + * + * NOTE: @b has to be at least of size @count * vol->mft_record_size. + */ +int ntfs_mft_records_read(const ntfs_volume *vol, const MFT_REF mref, + const s64 count, MFT_RECORD *b) +{ + s64 br; + VCN m; + + ntfs_log_trace("inode %llu\n", (unsigned long long)MREF(mref)); + + if (!vol || !vol->mft_na || !b || count < 0) { + errno = EINVAL; + ntfs_log_perror("%s: b=%p count=%lld mft=%llu", __FUNCTION__, + b, (long long)count, (unsigned long long)MREF(mref)); + return -1; + } + m = MREF(mref); + /* Refuse to read non-allocated mft records. */ + if (m + count > vol->mft_na->initialized_size >> + vol->mft_record_size_bits) { + errno = ESPIPE; + ntfs_log_perror("Trying to read non-allocated mft records " + "(%lld > %lld)", (long long)m + count, + (long long)vol->mft_na->initialized_size >> + vol->mft_record_size_bits); + return -1; + } + br = ntfs_attr_mst_pread(vol->mft_na, m << vol->mft_record_size_bits, + count, vol->mft_record_size, b); + if (br != count) { + if (br != -1) + errno = EIO; + ntfs_log_perror("Failed to read of MFT, mft=%llu count=%lld " + "br=%lld", (long long)m, (long long)count, + (long long)br); + return -1; + } + return 0; +} + +/** + * ntfs_mft_records_write - write mft records to disk + * @vol: volume to write to + * @mref: starting mft record number to write + * @count: number of mft records to write + * @b: data buffer containing the mft records to write + * + * Write @count mft records starting at @mref from data buffer @b to volume + * @vol. Return 0 on success or -1 on error, with errno set to the error code. + * + * If any of the records exceed the initialized size of the $MFT/$DATA + * attribute, i.e. they cannot possibly be allocated mft records, assume this + * is a bug and return error code ESPIPE. + * + * Before the mft records are written, they are mst protected. After the write, + * they are deprotected again, thus resulting in an increase in the update + * sequence number inside the data buffer @b. + * + * If any mft records are written which are also represented in the mft mirror + * $MFTMirr, we make a copy of the relevant parts of the data buffer @b into a + * temporary buffer before we do the actual write. Then if at least one mft + * record was successfully written, we write the appropriate mft records from + * the copied buffer to the mft mirror, too. + */ +int ntfs_mft_records_write(const ntfs_volume *vol, const MFT_REF mref, + const s64 count, MFT_RECORD *b) +{ + s64 bw; + VCN m; + void *bmirr = NULL; + int cnt = 0, res = 0; + + if (!vol || !vol->mft_na || vol->mftmirr_size <= 0 || !b || count < 0) { + errno = EINVAL; + return -1; + } + m = MREF(mref); + /* Refuse to write non-allocated mft records. */ + if (m + count > vol->mft_na->initialized_size >> + vol->mft_record_size_bits) { + errno = ESPIPE; + ntfs_log_perror("Trying to write non-allocated mft records " + "(%lld > %lld)", (long long)m + count, + (long long)vol->mft_na->initialized_size >> + vol->mft_record_size_bits); + return -1; + } + if (m < vol->mftmirr_size) { + if (!vol->mftmirr_na) { + errno = EINVAL; + return -1; + } + cnt = vol->mftmirr_size - m; + if (cnt > count) + cnt = count; + if ((m + cnt) > vol->mftmirr_na->initialized_size >> + vol->mft_record_size_bits) { + errno = ESPIPE; + ntfs_log_perror("Trying to write non-allocated mftmirr" + " records (%lld > %lld)", (long long)m + cnt, + (long long)vol->mftmirr_na->initialized_size >> + vol->mft_record_size_bits); + return -1; + } + bmirr = ntfs_malloc(cnt * vol->mft_record_size); + if (!bmirr) + return -1; + memcpy(bmirr, b, cnt * vol->mft_record_size); + } + bw = ntfs_attr_mst_pwrite(vol->mft_na, m << vol->mft_record_size_bits, + count, vol->mft_record_size, b); + if (bw != count) { + if (bw != -1) + errno = EIO; + if (bw >= 0) + ntfs_log_debug("Error: partial write while writing $Mft " + "record(s)!\n"); + else + ntfs_log_perror("Error writing $Mft record(s)"); + res = errno; + } + if (bmirr && bw > 0) { + if (bw < cnt) + cnt = bw; + bw = ntfs_attr_mst_pwrite(vol->mftmirr_na, + m << vol->mft_record_size_bits, cnt, + vol->mft_record_size, bmirr); + if (bw != cnt) { + if (bw != -1) + errno = EIO; + ntfs_log_debug("Error: failed to sync $MFTMirr! Run " + "chkdsk.\n"); + res = errno; + } + } + free(bmirr); + if (!res) + return res; + errno = res; + return -1; +} + +/* + * Check the consistency of an MFT record + * + * Make sure its general fields are safe, then examine all its + * attributes and apply generic checks to them. + * The attribute checks are skipped when a record is being read in + * order to collect its sequence number for creating a new record. + * + * Returns 0 if the checks are successful + * -1 with errno = EIO otherwise + */ + +int ntfs_mft_record_check(const ntfs_volume *vol, const MFT_REF mref, + MFT_RECORD *m) +{ + ATTR_RECORD *a; + ATTR_TYPES previous_type; + int ret = -1; + u32 offset; + s32 space; + + if (!ntfs_is_file_record(m->magic)) { + if (!NVolNoFixupWarn(vol)) + ntfs_log_error("Record %llu has no FILE magic (0x%x)\n", + (unsigned long long)MREF(mref), + (int)le32_to_cpu(*(le32*)m)); + goto err_out; + } + + if (le32_to_cpu(m->bytes_allocated) != vol->mft_record_size) { + ntfs_log_error("Record %llu has corrupt allocation size " + "(%u <> %u)\n", (unsigned long long)MREF(mref), + vol->mft_record_size, + le32_to_cpu(m->bytes_allocated)); + goto err_out; + } + if (!NVolNoFixupWarn(vol) + && (le32_to_cpu(m->bytes_in_use) > vol->mft_record_size)) { + ntfs_log_error("Record %llu has corrupt in-use size " + "(%u > %u)\n", (unsigned long long)MREF(mref), + (int)le32_to_cpu(m->bytes_in_use), + (int)vol->mft_record_size); + goto err_out; + } + if (le16_to_cpu(m->attrs_offset) & 7) { + ntfs_log_error("Attributes badly aligned in record %llu\n", + (unsigned long long)MREF(mref)); + goto err_out; + } + + a = (ATTR_RECORD *)((char *)m + le16_to_cpu(m->attrs_offset)); + if (p2n(a) < p2n(m) || (char *)a > (char *)m + vol->mft_record_size) { + ntfs_log_error("Record %llu is corrupt\n", + (unsigned long long)MREF(mref)); + goto err_out; + } + + if (!NVolNoFixupWarn(vol)) { + offset = le16_to_cpu(m->attrs_offset); + space = le32_to_cpu(m->bytes_in_use) - offset; + a = (ATTR_RECORD*)((char*)m + offset); + previous_type = AT_STANDARD_INFORMATION; + while ((space >= (s32)offsetof(ATTR_RECORD, resident_end)) + && (a->type != AT_END) + && (le32_to_cpu(a->type) >= le32_to_cpu(previous_type))) { + if ((le32_to_cpu(a->length) <= (u32)space) + && !(le32_to_cpu(a->length) & 7)) { + if (!ntfs_attr_inconsistent(a, mref)) { + previous_type = a->type; + offset += le32_to_cpu(a->length); + space -= le32_to_cpu(a->length); + a = (ATTR_RECORD*)((char*)m + offset); + } else + goto err_out; + } else { + ntfs_log_error("Corrupted MFT record %llu\n", + (unsigned long long)MREF(mref)); + goto err_out; + } + } + /* We are supposed to reach an AT_END */ + if ((space < 4) || (a->type != AT_END)) { + ntfs_log_error("Bad end of MFT record %llu\n", + (unsigned long long)MREF(mref)); + goto err_out; + } + } + + ret = 0; +err_out: + if (ret) + errno = EIO; + return ret; +} + +/** + * ntfs_file_record_read - read a FILE record from the mft from disk + * @vol: volume to read from + * @mref: mft reference specifying mft record to read + * @mrec: address of pointer in which to return the mft record + * @attr: address of pointer in which to return the first attribute + * + * Read a FILE record from the mft of @vol from the storage medium. @mref + * specifies the mft record to read, including the sequence number, which can + * be 0 if no sequence number checking is to be performed. + * + * The function allocates a buffer large enough to hold the mft record and + * reads the record into the buffer (mst deprotecting it in the process). + * *@mrec is then set to point to the buffer. + * + * If @attr is not NULL, *@attr is set to point to the first attribute in the + * mft record, i.e. *@attr is a pointer into *@mrec. + * + * Return 0 on success, or -1 on error, with errno set to the error code. + * + * The read mft record is checked for having the magic FILE, + * and for having a matching sequence number (if MSEQNO(*@mref) != 0). + * If either of these fails, -1 is returned and errno is set to EIO. If you get + * this, but you still want to read the mft record (e.g. in order to correct + * it), use ntfs_mft_record_read() directly. + * + * Note: Caller has to free *@mrec when finished. + * + * Note: We do not check if the mft record is flagged in use. The caller can + * check if desired. + */ +int ntfs_file_record_read(const ntfs_volume *vol, const MFT_REF mref, + MFT_RECORD **mrec, ATTR_RECORD **attr) +{ + MFT_RECORD *m; + + if (!vol || !mrec) { + errno = EINVAL; + ntfs_log_perror("%s: mrec=%p", __FUNCTION__, mrec); + return -1; + } + + m = *mrec; + if (!m) { + m = ntfs_malloc(vol->mft_record_size); + if (!m) + return -1; + } + if (ntfs_mft_record_read(vol, mref, m)) + goto err_out; + + if (ntfs_mft_record_check(vol, mref, m)) + goto err_out; + + if (MSEQNO(mref) && MSEQNO(mref) != le16_to_cpu(m->sequence_number)) { + ntfs_log_error("Record %llu has wrong SeqNo (%d <> %d)\n", + (unsigned long long)MREF(mref), MSEQNO(mref), + le16_to_cpu(m->sequence_number)); + errno = EIO; + goto err_out; + } + *mrec = m; + if (attr) + *attr = (ATTR_RECORD*)((char*)m + le16_to_cpu(m->attrs_offset)); + return 0; +err_out: + if (m != *mrec) + free(m); + return -1; +} + +/** + * ntfs_mft_record_layout - layout an mft record into a memory buffer + * @vol: volume to which the mft record will belong + * @mref: mft reference specifying the mft record number + * @mrec: destination buffer of size >= @vol->mft_record_size bytes + * + * Layout an empty, unused mft record with the mft reference @mref into the + * buffer @m. The volume @vol is needed because the mft record structure was + * modified in NTFS 3.1 so we need to know which volume version this mft record + * will be used on. + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +int ntfs_mft_record_layout(const ntfs_volume *vol, const MFT_REF mref, + MFT_RECORD *mrec) +{ + ATTR_RECORD *a; + + if (!vol || !mrec) { + errno = EINVAL; + ntfs_log_perror("%s: mrec=%p", __FUNCTION__, mrec); + return -1; + } + /* Aligned to 2-byte boundary. */ + if (vol->major_ver < 3 || (vol->major_ver == 3 && !vol->minor_ver)) + mrec->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD_OLD) + 1) & ~1); + else { + /* Abort if mref is > 32 bits. */ + if (MREF(mref) & 0x0000ffff00000000ull) { + errno = ERANGE; + ntfs_log_perror("Mft reference exceeds 32 bits"); + return -1; + } + mrec->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD) + 1) & ~1); + /* + * Set the NTFS 3.1+ specific fields while we know that the + * volume version is 3.1+. + */ + mrec->reserved = const_cpu_to_le16(0); + mrec->mft_record_number = cpu_to_le32(MREF(mref)); + } + mrec->magic = magic_FILE; + if (vol->mft_record_size >= NTFS_BLOCK_SIZE) + mrec->usa_count = cpu_to_le16(vol->mft_record_size / + NTFS_BLOCK_SIZE + 1); + else { + mrec->usa_count = const_cpu_to_le16(1); + ntfs_log_error("Sector size is bigger than MFT record size. " + "Setting usa_count to 1. If Windows chkdsk " + "reports this as corruption, please email %s " + "stating that you saw this message and that " + "the file system created was corrupt. " + "Thank you.\n", NTFS_DEV_LIST); + } + /* Set the update sequence number to 1. */ + *(le16*)((u8*)mrec + le16_to_cpu(mrec->usa_ofs)) = const_cpu_to_le16(1); + mrec->lsn = const_cpu_to_sle64(0ll); + mrec->sequence_number = const_cpu_to_le16(1); + mrec->link_count = const_cpu_to_le16(0); + /* Aligned to 8-byte boundary. */ + mrec->attrs_offset = cpu_to_le16((le16_to_cpu(mrec->usa_ofs) + + (le16_to_cpu(mrec->usa_count) << 1) + 7) & ~7); + mrec->flags = const_cpu_to_le16(0); + /* + * Using attrs_offset plus eight bytes (for the termination attribute), + * aligned to 8-byte boundary. + */ + mrec->bytes_in_use = cpu_to_le32((le16_to_cpu(mrec->attrs_offset) + 8 + + 7) & ~7); + mrec->bytes_allocated = cpu_to_le32(vol->mft_record_size); + mrec->base_mft_record = const_cpu_to_le64((MFT_REF)0); + mrec->next_attr_instance = const_cpu_to_le16(0); + a = (ATTR_RECORD*)((u8*)mrec + le16_to_cpu(mrec->attrs_offset)); + a->type = AT_END; + a->length = const_cpu_to_le32(0); + /* Finally, clear the unused part of the mft record. */ + memset((u8*)a + 8, 0, vol->mft_record_size - ((u8*)a + 8 - (u8*)mrec)); + return 0; +} + +/** + * ntfs_mft_record_format - format an mft record on an ntfs volume + * @vol: volume on which to format the mft record + * @mref: mft reference specifying mft record to format + * + * Format the mft record with the mft reference @mref in $MFT/$DATA, i.e. lay + * out an empty, unused mft record in memory and write it to the volume @vol. + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +int ntfs_mft_record_format(const ntfs_volume *vol, const MFT_REF mref) +{ + MFT_RECORD *m; + int ret = -1; + + ntfs_log_enter("Entering\n"); + + m = ntfs_calloc(vol->mft_record_size); + if (!m) + goto out; + + if (ntfs_mft_record_layout(vol, mref, m)) + goto free_m; + + if (ntfs_mft_record_write(vol, mref, m)) + goto free_m; + + ret = 0; +free_m: + free(m); +out: + ntfs_log_leave("\n"); + return ret; +} + +static const char *es = " Leaving inconsistent metadata. Run chkdsk."; + +/** + * ntfs_ffz - Find the first unset (zero) bit in a word + * @word: + * + * Description... + * + * Returns: + */ +static inline unsigned int ntfs_ffz(unsigned int word) +{ + return ffs(~word) - 1; +} + +static int ntfs_is_mft(ntfs_inode *ni) +{ + if (ni && ni->mft_no == FILE_MFT) + return 1; + return 0; +} + +#ifndef PAGE_SIZE +#define PAGE_SIZE 4096 +#endif + +#define RESERVED_MFT_RECORDS 64 + +/** + * ntfs_mft_bitmap_find_free_rec - find a free mft record in the mft bitmap + * @vol: volume on which to search for a free mft record + * @base_ni: open base inode if allocating an extent mft record or NULL + * + * Search for a free mft record in the mft bitmap attribute on the ntfs volume + * @vol. + * + * If @base_ni is NULL start the search at the default allocator position. + * + * If @base_ni is not NULL start the search at the mft record after the base + * mft record @base_ni. + * + * Return the free mft record on success and -1 on error with errno set to the + * error code. An error code of ENOSPC means that there are no free mft + * records in the currently initialized mft bitmap. + */ +static int ntfs_mft_bitmap_find_free_rec(ntfs_volume *vol, ntfs_inode *base_ni) +{ + s64 pass_end, ll, data_pos, pass_start, ofs, bit; + ntfs_attr *mftbmp_na; + u8 *buf, *byte; + unsigned int size; + u8 pass, b; + int ret = -1; + + ntfs_log_enter("Entering\n"); + + mftbmp_na = vol->mftbmp_na; + /* + * Set the end of the pass making sure we do not overflow the mft + * bitmap. + */ + size = PAGE_SIZE; + pass_end = vol->mft_na->allocated_size >> vol->mft_record_size_bits; + ll = mftbmp_na->initialized_size << 3; + if (pass_end > ll) + pass_end = ll; + pass = 1; + if (!base_ni) + data_pos = vol->mft_data_pos; + else + data_pos = base_ni->mft_no + 1; + if (data_pos < RESERVED_MFT_RECORDS) + data_pos = RESERVED_MFT_RECORDS; + if (data_pos >= pass_end) { + data_pos = RESERVED_MFT_RECORDS; + pass = 2; + /* This happens on a freshly formatted volume. */ + if (data_pos >= pass_end) { + errno = ENOSPC; + goto leave; + } + } + if (ntfs_is_mft(base_ni)) { + data_pos = 0; + pass = 2; + } + pass_start = data_pos; + buf = ntfs_malloc(PAGE_SIZE); + if (!buf) + goto leave; + + ntfs_log_debug("Starting bitmap search: pass %u, pass_start 0x%llx, " + "pass_end 0x%llx, data_pos 0x%llx.\n", pass, + (long long)pass_start, (long long)pass_end, + (long long)data_pos); +#ifdef DEBUG + byte = NULL; + b = 0; +#endif + /* Loop until a free mft record is found. */ + for (; pass <= 2; size = PAGE_SIZE) { + /* Cap size to pass_end. */ + ofs = data_pos >> 3; + ll = ((pass_end + 7) >> 3) - ofs; + if (size > ll) + size = ll; + ll = ntfs_attr_pread(mftbmp_na, ofs, size, buf); + if (ll < 0) { + ntfs_log_perror("Failed to read $MFT bitmap"); + free(buf); + goto leave; + } + ntfs_log_debug("Read 0x%llx bytes.\n", (long long)ll); + /* If we read at least one byte, search @buf for a zero bit. */ + if (ll) { + size = ll << 3; + bit = data_pos & 7; + data_pos &= ~7ull; + ntfs_log_debug("Before inner for loop: size 0x%x, " + "data_pos 0x%llx, bit 0x%llx, " + "*byte 0x%hhx, b %u.\n", size, + (long long)data_pos, (long long)bit, + (u8) (byte ? *byte : -1), b); + for (; bit < size && data_pos + bit < pass_end; + bit &= ~7ull, bit += 8) { + /* + * If we're extending $MFT and running out of the first + * mft record (base record) then give up searching since + * no guarantee that the found record will be accessible. + */ + if (ntfs_is_mft(base_ni) && bit > 400) + goto out; + + byte = buf + (bit >> 3); + if (*byte == 0xff) + continue; + + /* Note: ffz() result must be zero based. */ + b = ntfs_ffz((unsigned long)*byte); + if (b < 8 && b >= (bit & 7)) { + free(buf); + ret = data_pos + (bit & ~7ull) + b; + goto leave; + } + } + ntfs_log_debug("After inner for loop: size 0x%x, " + "data_pos 0x%llx, bit 0x%llx, " + "*byte 0x%hhx, b %u.\n", size, + (long long)data_pos, (long long)bit, + (u8) (byte ? *byte : -1), b); + data_pos += size; + /* + * If the end of the pass has not been reached yet, + * continue searching the mft bitmap for a zero bit. + */ + if (data_pos < pass_end) + continue; + } + /* Do the next pass. */ + pass++; + if (pass == 2) { + /* + * Starting the second pass, in which we scan the first + * part of the zone which we omitted earlier. + */ + pass_end = pass_start; + data_pos = pass_start = RESERVED_MFT_RECORDS; + ntfs_log_debug("pass %i, pass_start 0x%llx, pass_end " + "0x%llx.\n", pass, (long long)pass_start, + (long long)pass_end); + if (data_pos >= pass_end) + break; + } + } + /* No free mft records in currently initialized mft bitmap. */ +out: + free(buf); + errno = ENOSPC; +leave: + ntfs_log_leave("\n"); + return ret; +} + +static int ntfs_mft_attr_extend(ntfs_attr *na) +{ + int ret = STATUS_ERROR; + ntfs_log_enter("Entering\n"); + + if (!NInoAttrList(na->ni)) { + if (ntfs_inode_add_attrlist(na->ni)) { + ntfs_log_perror("%s: Can not add attrlist #3", __FUNCTION__); + goto out; + } + /* We can't sync the $MFT inode since its runlist is bogus. */ + ret = STATUS_KEEP_SEARCHING; + goto out; + } + + if (ntfs_attr_update_mapping_pairs(na, 0)) { + ntfs_log_perror("%s: MP update failed", __FUNCTION__); + goto out; + } + + ret = STATUS_OK; +out: + ntfs_log_leave("\n"); + return ret; +} + +/** + * ntfs_mft_bitmap_extend_allocation_i - see ntfs_mft_bitmap_extend_allocation + */ +static int ntfs_mft_bitmap_extend_allocation_i(ntfs_volume *vol) +{ + LCN lcn; + s64 ll = 0; /* silence compiler warning */ + ntfs_attr *mftbmp_na; + runlist_element *rl, *rl2 = NULL; /* silence compiler warning */ + ntfs_attr_search_ctx *ctx; + MFT_RECORD *m = NULL; /* silence compiler warning */ + ATTR_RECORD *a = NULL; /* silence compiler warning */ + int err, mp_size; + int ret = STATUS_ERROR; + u32 old_alen = 0; /* silence compiler warning */ + BOOL mp_rebuilt = FALSE; + BOOL update_mp = FALSE; + + mftbmp_na = vol->mftbmp_na; + /* + * Determine the last lcn of the mft bitmap. The allocated size of the + * mft bitmap cannot be zero so we are ok to do this. + */ + rl = ntfs_attr_find_vcn(mftbmp_na, (mftbmp_na->allocated_size - 1) >> + vol->cluster_size_bits); + if (!rl || !rl->length || rl->lcn < 0) { + ntfs_log_error("Failed to determine last allocated " + "cluster of mft bitmap attribute.\n"); + if (rl) + errno = EIO; + return STATUS_ERROR; + } + lcn = rl->lcn + rl->length; + + rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE); + if (!rl2) { + ntfs_log_error("Failed to allocate a cluster for " + "the mft bitmap.\n"); + return STATUS_ERROR; + } + rl = ntfs_runlists_merge(mftbmp_na->rl, rl2); + if (!rl) { + err = errno; + ntfs_log_error("Failed to merge runlists for mft " + "bitmap.\n"); + if (ntfs_cluster_free_from_rl(vol, rl2)) + ntfs_log_error("Failed to deallocate " + "cluster.%s\n", es); + free(rl2); + errno = err; + return STATUS_ERROR; + } + mftbmp_na->rl = rl; + ntfs_log_debug("Adding one run to mft bitmap.\n"); + /* Find the last run in the new runlist. */ + for (; rl[1].length; rl++) + ; + /* + * Update the attribute record as well. Note: @rl is the last + * (non-terminator) runlist element of mft bitmap. + */ + ctx = ntfs_attr_get_search_ctx(mftbmp_na->ni, NULL); + if (!ctx) + goto undo_alloc; + + if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name, + mftbmp_na->name_len, 0, rl[1].vcn, NULL, 0, ctx)) { + ntfs_log_error("Failed to find last attribute extent of " + "mft bitmap attribute.\n"); + goto undo_alloc; + } + m = ctx->mrec; + a = ctx->attr; + ll = sle64_to_cpu(a->lowest_vcn); + rl2 = ntfs_attr_find_vcn(mftbmp_na, ll); + if (!rl2 || !rl2->length) { + ntfs_log_error("Failed to determine previous last " + "allocated cluster of mft bitmap attribute.\n"); + if (rl2) + errno = EIO; + goto undo_alloc; + } + /* Get the size for the new mapping pairs array for this extent. */ + mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, INT_MAX); + if (mp_size <= 0) { + ntfs_log_error("Get size for mapping pairs failed for " + "mft bitmap attribute extent.\n"); + goto undo_alloc; + } + /* Expand the attribute record if necessary. */ + old_alen = le32_to_cpu(a->length); + if (ntfs_attr_record_resize(m, a, mp_size + + le16_to_cpu(a->mapping_pairs_offset))) { + ntfs_log_info("extending $MFT bitmap\n"); + ret = ntfs_mft_attr_extend(vol->mftbmp_na); + if (ret == STATUS_OK) + goto ok; + if (ret == STATUS_ERROR) { + ntfs_log_perror("%s: ntfs_mft_attr_extend failed", __FUNCTION__); + update_mp = TRUE; + } + goto undo_alloc; + } + mp_rebuilt = TRUE; + /* Generate the mapping pairs array directly into the attr record. */ + if (ntfs_mapping_pairs_build(vol, (u8*)a + + le16_to_cpu(a->mapping_pairs_offset), mp_size, rl2, ll, + NULL)) { + ntfs_log_error("Failed to build mapping pairs array for " + "mft bitmap attribute.\n"); + errno = EIO; + goto undo_alloc; + } + /* Update the highest_vcn. */ + a->highest_vcn = cpu_to_sle64(rl[1].vcn - 1); + /* + * We now have extended the mft bitmap allocated_size by one cluster. + * Reflect this in the ntfs_attr structure and the attribute record. + */ + if (a->lowest_vcn) { + /* + * We are not in the first attribute extent, switch to it, but + * first ensure the changes will make it to disk later. + */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_reinit_search_ctx(ctx); + if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name, + mftbmp_na->name_len, 0, 0, NULL, 0, ctx)) { + ntfs_log_error("Failed to find first attribute " + "extent of mft bitmap attribute.\n"); + goto restore_undo_alloc; + } + a = ctx->attr; + } +ok: + mftbmp_na->allocated_size += vol->cluster_size; + a->allocated_size = cpu_to_sle64(mftbmp_na->allocated_size); + /* Ensure the changes make it to disk. */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_put_search_ctx(ctx); + return STATUS_OK; + +restore_undo_alloc: + err = errno; + ntfs_attr_reinit_search_ctx(ctx); + if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name, + mftbmp_na->name_len, 0, rl[1].vcn, NULL, 0, ctx)) { + ntfs_log_error("Failed to find last attribute extent of " + "mft bitmap attribute.%s\n", es); + ntfs_attr_put_search_ctx(ctx); + mftbmp_na->allocated_size += vol->cluster_size; + /* + * The only thing that is now wrong is ->allocated_size of the + * base attribute extent which chkdsk should be able to fix. + */ + errno = err; + return STATUS_ERROR; + } + m = ctx->mrec; + a = ctx->attr; + a->highest_vcn = cpu_to_sle64(rl[1].vcn - 2); + errno = err; +undo_alloc: + err = errno; + + /* Remove the last run from the runlist. */ + lcn = rl->lcn; + rl->lcn = rl[1].lcn; + rl->length = 0; + + /* FIXME: use an ntfs_cluster_free_* function */ + if (ntfs_bitmap_clear_bit(vol->lcnbmp_na, lcn)) + ntfs_log_error("Failed to free cluster.%s\n", es); + else + vol->free_clusters++; + if (mp_rebuilt) { + if (ntfs_mapping_pairs_build(vol, (u8*)a + + le16_to_cpu(a->mapping_pairs_offset), + old_alen - le16_to_cpu(a->mapping_pairs_offset), + rl2, ll, NULL)) + ntfs_log_error("Failed to restore mapping " + "pairs array.%s\n", es); + if (ntfs_attr_record_resize(m, a, old_alen)) + ntfs_log_error("Failed to restore attribute " + "record.%s\n", es); + ntfs_inode_mark_dirty(ctx->ntfs_ino); + } + if (update_mp) { + if (ntfs_attr_update_mapping_pairs(vol->mftbmp_na, 0)) + ntfs_log_perror("%s: MP update failed", __FUNCTION__); + } + if (ctx) + ntfs_attr_put_search_ctx(ctx); + errno = err; + return ret; +} + +/** + * ntfs_mft_bitmap_extend_allocation - extend mft bitmap attribute by a cluster + * @vol: volume on which to extend the mft bitmap attribute + * + * Extend the mft bitmap attribute on the ntfs volume @vol by one cluster. + * + * Note: Only changes allocated_size, i.e. does not touch initialized_size or + * data_size. + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +static int ntfs_mft_bitmap_extend_allocation(ntfs_volume *vol) +{ + int ret; + + ntfs_log_enter("Entering\n"); + ret = ntfs_mft_bitmap_extend_allocation_i(vol); + ntfs_log_leave("\n"); + return ret; +} +/** + * ntfs_mft_bitmap_extend_initialized - extend mft bitmap initialized data + * @vol: volume on which to extend the mft bitmap attribute + * + * Extend the initialized portion of the mft bitmap attribute on the ntfs + * volume @vol by 8 bytes. + * + * Note: Only changes initialized_size and data_size, i.e. requires that + * allocated_size is big enough to fit the new initialized_size. + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +static int ntfs_mft_bitmap_extend_initialized(ntfs_volume *vol) +{ + s64 old_data_size, old_initialized_size, ll; + ntfs_attr *mftbmp_na; + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *a; + int err; + int ret = -1; + + ntfs_log_enter("Entering\n"); + + mftbmp_na = vol->mftbmp_na; + ctx = ntfs_attr_get_search_ctx(mftbmp_na->ni, NULL); + if (!ctx) + goto out; + + if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name, + mftbmp_na->name_len, 0, 0, NULL, 0, ctx)) { + ntfs_log_error("Failed to find first attribute extent of " + "mft bitmap attribute.\n"); + err = errno; + goto put_err_out; + } + a = ctx->attr; + old_data_size = mftbmp_na->data_size; + old_initialized_size = mftbmp_na->initialized_size; + mftbmp_na->initialized_size += 8; + a->initialized_size = cpu_to_sle64(mftbmp_na->initialized_size); + if (mftbmp_na->initialized_size > mftbmp_na->data_size) { + mftbmp_na->data_size = mftbmp_na->initialized_size; + a->data_size = cpu_to_sle64(mftbmp_na->data_size); + } + /* Ensure the changes make it to disk. */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_put_search_ctx(ctx); + /* Initialize the mft bitmap attribute value with zeroes. */ + ll = 0; + ll = ntfs_attr_pwrite(mftbmp_na, old_initialized_size, 8, &ll); + if (ll == 8) { + ntfs_log_debug("Wrote eight initialized bytes to mft bitmap.\n"); + ret = 0; + goto out; + } + ntfs_log_error("Failed to write to mft bitmap.\n"); + err = errno; + if (ll >= 0) + err = EIO; + /* Try to recover from the error. */ + ctx = ntfs_attr_get_search_ctx(mftbmp_na->ni, NULL); + if (!ctx) + goto err_out; + + if (ntfs_attr_lookup(mftbmp_na->type, mftbmp_na->name, + mftbmp_na->name_len, 0, 0, NULL, 0, ctx)) { + ntfs_log_error("Failed to find first attribute extent of " + "mft bitmap attribute.%s\n", es); +put_err_out: + ntfs_attr_put_search_ctx(ctx); + goto err_out; + } + a = ctx->attr; + mftbmp_na->initialized_size = old_initialized_size; + a->initialized_size = cpu_to_sle64(old_initialized_size); + if (mftbmp_na->data_size != old_data_size) { + mftbmp_na->data_size = old_data_size; + a->data_size = cpu_to_sle64(old_data_size); + } + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_put_search_ctx(ctx); + ntfs_log_debug("Restored status of mftbmp: allocated_size 0x%llx, " + "data_size 0x%llx, initialized_size 0x%llx.\n", + (long long)mftbmp_na->allocated_size, + (long long)mftbmp_na->data_size, + (long long)mftbmp_na->initialized_size); +err_out: + errno = err; +out: + ntfs_log_leave("\n"); + return ret; +} + +/** + * ntfs_mft_data_extend_allocation - extend mft data attribute + * @vol: volume on which to extend the mft data attribute + * + * Extend the mft data attribute on the ntfs volume @vol by 16 mft records + * worth of clusters or if not enough space for this by one mft record worth + * of clusters. + * + * Note: Only changes allocated_size, i.e. does not touch initialized_size or + * data_size. + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +static int ntfs_mft_data_extend_allocation(ntfs_volume *vol) +{ + LCN lcn; + VCN old_last_vcn; + s64 min_nr, nr, ll = 0; /* silence compiler warning */ + ntfs_attr *mft_na; + runlist_element *rl, *rl2; + ntfs_attr_search_ctx *ctx; + MFT_RECORD *m = NULL; /* silence compiler warning */ + ATTR_RECORD *a = NULL; /* silence compiler warning */ + int err, mp_size; + int ret = STATUS_ERROR; + u32 old_alen = 0; /* silence compiler warning */ + BOOL mp_rebuilt = FALSE; + BOOL update_mp = FALSE; + + ntfs_log_enter("Extending mft data allocation.\n"); + + mft_na = vol->mft_na; + /* + * Determine the preferred allocation location, i.e. the last lcn of + * the mft data attribute. The allocated size of the mft data + * attribute cannot be zero so we are ok to do this. + */ + rl = ntfs_attr_find_vcn(mft_na, + (mft_na->allocated_size - 1) >> vol->cluster_size_bits); + + if (!rl || !rl->length || rl->lcn < 0) { + ntfs_log_error("Failed to determine last allocated " + "cluster of mft data attribute.\n"); + if (rl) + errno = EIO; + goto out; + } + + lcn = rl->lcn + rl->length; + ntfs_log_debug("Last lcn of mft data attribute is 0x%llx.\n", (long long)lcn); + /* Minimum allocation is one mft record worth of clusters. */ + min_nr = vol->mft_record_size >> vol->cluster_size_bits; + if (!min_nr) + min_nr = 1; + /* Want to allocate 16 mft records worth of clusters. */ + nr = vol->mft_record_size << 4 >> vol->cluster_size_bits; + if (!nr) + nr = min_nr; + + old_last_vcn = rl[1].vcn; + do { + rl2 = ntfs_cluster_alloc(vol, old_last_vcn, nr, lcn, MFT_ZONE); + if (rl2) + break; + if (errno != ENOSPC || nr == min_nr) { + ntfs_log_perror("Failed to allocate (%lld) clusters " + "for $MFT", (long long)nr); + goto out; + } + /* + * There is not enough space to do the allocation, but there + * might be enough space to do a minimal allocation so try that + * before failing. + */ + nr = min_nr; + ntfs_log_debug("Retrying mft data allocation with minimal cluster " + "count %lli.\n", (long long)nr); + } while (1); + + ntfs_log_debug("Allocated %lld clusters.\n", (long long)nr); + + rl = ntfs_runlists_merge(mft_na->rl, rl2); + if (!rl) { + err = errno; + ntfs_log_error("Failed to merge runlists for mft data " + "attribute.\n"); + if (ntfs_cluster_free_from_rl(vol, rl2)) + ntfs_log_error("Failed to deallocate clusters " + "from the mft data attribute.%s\n", es); + free(rl2); + errno = err; + goto out; + } + mft_na->rl = rl; + + /* Find the last run in the new runlist. */ + for (; rl[1].length; rl++) + ; + /* Update the attribute record as well. */ + ctx = ntfs_attr_get_search_ctx(mft_na->ni, NULL); + if (!ctx) + goto undo_alloc; + + if (ntfs_attr_lookup(mft_na->type, mft_na->name, mft_na->name_len, 0, + rl[1].vcn, NULL, 0, ctx)) { + ntfs_log_error("Failed to find last attribute extent of " + "mft data attribute.\n"); + goto undo_alloc; + } + m = ctx->mrec; + a = ctx->attr; + ll = sle64_to_cpu(a->lowest_vcn); + rl2 = ntfs_attr_find_vcn(mft_na, ll); + if (!rl2 || !rl2->length) { + ntfs_log_error("Failed to determine previous last " + "allocated cluster of mft data attribute.\n"); + if (rl2) + errno = EIO; + goto undo_alloc; + } + /* Get the size for the new mapping pairs array for this extent. */ + mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, INT_MAX); + if (mp_size <= 0) { + ntfs_log_error("Get size for mapping pairs failed for " + "mft data attribute extent.\n"); + goto undo_alloc; + } + /* Expand the attribute record if necessary. */ + old_alen = le32_to_cpu(a->length); + if (ntfs_attr_record_resize(m, a, + mp_size + le16_to_cpu(a->mapping_pairs_offset))) { + ret = ntfs_mft_attr_extend(vol->mft_na); + if (ret == STATUS_OK) + goto ok; + if (ret == STATUS_ERROR) { + ntfs_log_perror("%s: ntfs_mft_attr_extend failed", __FUNCTION__); + update_mp = TRUE; + } + goto undo_alloc; + } + mp_rebuilt = TRUE; + /* + * Generate the mapping pairs array directly into the attribute record. + */ + if (ntfs_mapping_pairs_build(vol, + (u8*)a + le16_to_cpu(a->mapping_pairs_offset), mp_size, + rl2, ll, NULL)) { + ntfs_log_error("Failed to build mapping pairs array of " + "mft data attribute.\n"); + errno = EIO; + goto undo_alloc; + } + /* Update the highest_vcn. */ + a->highest_vcn = cpu_to_sle64(rl[1].vcn - 1); + /* + * We now have extended the mft data allocated_size by nr clusters. + * Reflect this in the ntfs_attr structure and the attribute record. + * @rl is the last (non-terminator) runlist element of mft data + * attribute. + */ + if (a->lowest_vcn) { + /* + * We are not in the first attribute extent, switch to it, but + * first ensure the changes will make it to disk later. + */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_reinit_search_ctx(ctx); + if (ntfs_attr_lookup(mft_na->type, mft_na->name, + mft_na->name_len, 0, 0, NULL, 0, ctx)) { + ntfs_log_error("Failed to find first attribute " + "extent of mft data attribute.\n"); + goto restore_undo_alloc; + } + a = ctx->attr; + } +ok: + mft_na->allocated_size += nr << vol->cluster_size_bits; + a->allocated_size = cpu_to_sle64(mft_na->allocated_size); + /* Ensure the changes make it to disk. */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_put_search_ctx(ctx); + ret = STATUS_OK; +out: + ntfs_log_leave("\n"); + return ret; + +restore_undo_alloc: + err = errno; + ntfs_attr_reinit_search_ctx(ctx); + if (ntfs_attr_lookup(mft_na->type, mft_na->name, mft_na->name_len, 0, + rl[1].vcn, NULL, 0, ctx)) { + ntfs_log_error("Failed to find last attribute extent of " + "mft data attribute.%s\n", es); + ntfs_attr_put_search_ctx(ctx); + mft_na->allocated_size += nr << vol->cluster_size_bits; + /* + * The only thing that is now wrong is ->allocated_size of the + * base attribute extent which chkdsk should be able to fix. + */ + errno = err; + ret = STATUS_ERROR; + goto out; + } + m = ctx->mrec; + a = ctx->attr; + a->highest_vcn = cpu_to_sle64(old_last_vcn - 1); + errno = err; +undo_alloc: + err = errno; + if (ntfs_cluster_free(vol, mft_na, old_last_vcn, -1) < 0) + ntfs_log_error("Failed to free clusters from mft data " + "attribute.%s\n", es); + if (ntfs_rl_truncate(&mft_na->rl, old_last_vcn)) + ntfs_log_error("Failed to truncate mft data attribute " + "runlist.%s\n", es); + if (mp_rebuilt) { + if (ntfs_mapping_pairs_build(vol, (u8*)a + + le16_to_cpu(a->mapping_pairs_offset), + old_alen - le16_to_cpu(a->mapping_pairs_offset), + rl2, ll, NULL)) + ntfs_log_error("Failed to restore mapping pairs " + "array.%s\n", es); + if (ntfs_attr_record_resize(m, a, old_alen)) + ntfs_log_error("Failed to restore attribute " + "record.%s\n", es); + ntfs_inode_mark_dirty(ctx->ntfs_ino); + } + if (update_mp) { + if (ntfs_attr_update_mapping_pairs(vol->mft_na, 0)) + ntfs_log_perror("%s: MP update failed", __FUNCTION__); + } + if (ctx) + ntfs_attr_put_search_ctx(ctx); + errno = err; + goto out; +} + + +static int ntfs_mft_record_init(ntfs_volume *vol, s64 size) +{ + int ret = -1; + ntfs_attr *mft_na; + s64 old_data_initialized, old_data_size; + ntfs_attr_search_ctx *ctx; + + ntfs_log_enter("Entering\n"); + + /* NOTE: Caller must sanity check vol, vol->mft_na and vol->mftbmp_na */ + + mft_na = vol->mft_na; + + /* + * The mft record is outside the initialized data. Extend the mft data + * attribute until it covers the allocated record. The loop is only + * actually traversed more than once when a freshly formatted volume + * is first written to so it optimizes away nicely in the common case. + */ + ntfs_log_debug("Status of mft data before extension: " + "allocated_size 0x%llx, data_size 0x%llx, " + "initialized_size 0x%llx.\n", + (long long)mft_na->allocated_size, + (long long)mft_na->data_size, + (long long)mft_na->initialized_size); + while (size > mft_na->allocated_size) { + if (ntfs_mft_data_extend_allocation(vol) == STATUS_ERROR) + goto out; + ntfs_log_debug("Status of mft data after allocation extension: " + "allocated_size 0x%llx, data_size 0x%llx, " + "initialized_size 0x%llx.\n", + (long long)mft_na->allocated_size, + (long long)mft_na->data_size, + (long long)mft_na->initialized_size); + } + + old_data_initialized = mft_na->initialized_size; + old_data_size = mft_na->data_size; + + /* + * Extend mft data initialized size (and data size of course) to reach + * the allocated mft record, formatting the mft records along the way. + * Note: We only modify the ntfs_attr structure as that is all that is + * needed by ntfs_mft_record_format(). We will update the attribute + * record itself in one fell swoop later on. + */ + while (size > mft_na->initialized_size) { + s64 ll2 = mft_na->initialized_size >> vol->mft_record_size_bits; + mft_na->initialized_size += vol->mft_record_size; + if (mft_na->initialized_size > mft_na->data_size) + mft_na->data_size = mft_na->initialized_size; + ntfs_log_debug("Initializing mft record 0x%llx.\n", (long long)ll2); + if (ntfs_mft_record_format(vol, ll2) < 0) { + ntfs_log_perror("Failed to format mft record"); + goto undo_data_init; + } + } + + /* Update the mft data attribute record to reflect the new sizes. */ + ctx = ntfs_attr_get_search_ctx(mft_na->ni, NULL); + if (!ctx) + goto undo_data_init; + + if (ntfs_attr_lookup(mft_na->type, mft_na->name, mft_na->name_len, 0, + 0, NULL, 0, ctx)) { + ntfs_log_error("Failed to find first attribute extent of " + "mft data attribute.\n"); + ntfs_attr_put_search_ctx(ctx); + goto undo_data_init; + } + ctx->attr->initialized_size = cpu_to_sle64(mft_na->initialized_size); + ctx->attr->data_size = cpu_to_sle64(mft_na->data_size); + ctx->attr->allocated_size = cpu_to_sle64(mft_na->allocated_size); + + /* Ensure the changes make it to disk. */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_put_search_ctx(ctx); + ntfs_log_debug("Status of mft data after mft record initialization: " + "allocated_size 0x%llx, data_size 0x%llx, " + "initialized_size 0x%llx.\n", + (long long)mft_na->allocated_size, + (long long)mft_na->data_size, + (long long)mft_na->initialized_size); + + /* Sanity checks. */ + if (mft_na->data_size > mft_na->allocated_size || + mft_na->initialized_size > mft_na->data_size) + NTFS_BUG("mft_na sanity checks failed"); + + /* Sync MFT to minimize data loss if there won't be clean unmount. */ + if (ntfs_inode_sync(mft_na->ni)) + goto undo_data_init; + + ret = 0; +out: + ntfs_log_leave("\n"); + return ret; + +undo_data_init: + mft_na->initialized_size = old_data_initialized; + mft_na->data_size = old_data_size; + goto out; +} + +static int ntfs_mft_rec_init(ntfs_volume *vol, s64 size) +{ + int ret = -1; + ntfs_attr *mft_na; + s64 old_data_initialized, old_data_size; + ntfs_attr_search_ctx *ctx; + + ntfs_log_enter("Entering\n"); + + mft_na = vol->mft_na; + + if (size > mft_na->allocated_size || size > mft_na->initialized_size) { + errno = EIO; + ntfs_log_perror("%s: unexpected $MFT sizes, see below", __FUNCTION__); + ntfs_log_error("$MFT: size=%lld allocated_size=%lld " + "data_size=%lld initialized_size=%lld\n", + (long long)size, + (long long)mft_na->allocated_size, + (long long)mft_na->data_size, + (long long)mft_na->initialized_size); + goto out; + } + + old_data_initialized = mft_na->initialized_size; + old_data_size = mft_na->data_size; + + /* Update the mft data attribute record to reflect the new sizes. */ + ctx = ntfs_attr_get_search_ctx(mft_na->ni, NULL); + if (!ctx) + goto undo_data_init; + + if (ntfs_attr_lookup(mft_na->type, mft_na->name, mft_na->name_len, 0, + 0, NULL, 0, ctx)) { + ntfs_log_error("Failed to find first attribute extent of " + "mft data attribute.\n"); + ntfs_attr_put_search_ctx(ctx); + goto undo_data_init; + } + ctx->attr->initialized_size = cpu_to_sle64(mft_na->initialized_size); + ctx->attr->data_size = cpu_to_sle64(mft_na->data_size); + + /* CHECKME: ctx->attr->allocation_size is already ok? */ + + /* Ensure the changes make it to disk. */ + ntfs_inode_mark_dirty(ctx->ntfs_ino); + ntfs_attr_put_search_ctx(ctx); + + /* Sanity checks. */ + if (mft_na->data_size > mft_na->allocated_size || + mft_na->initialized_size > mft_na->data_size) + NTFS_BUG("mft_na sanity checks failed"); +out: + ntfs_log_leave("\n"); + return ret; + +undo_data_init: + mft_na->initialized_size = old_data_initialized; + mft_na->data_size = old_data_size; + goto out; +} + +ntfs_inode *ntfs_mft_rec_alloc(ntfs_volume *vol, BOOL mft_data) +{ + s64 ll, bit; + ntfs_attr *mft_na, *mftbmp_na; + MFT_RECORD *m; + ntfs_inode *ni = NULL; + ntfs_inode *base_ni; + int err; + le16 seq_no, usn; + BOOL forced_mft_data; + + ntfs_log_enter("Entering\n"); + + mft_na = vol->mft_na; + mftbmp_na = vol->mftbmp_na; + + base_ni = mft_na->ni; + + /* + * The first extent containing $MFT:$AT_DATA is better located + * in record 15 to make sure it can be read at mount time. + * The record 15 is prereserved as a base inode with no + * extents and no name, and it is marked in use. + */ + forced_mft_data = FALSE; + if (mft_data) { + ntfs_inode *ext_ni = ntfs_inode_open(vol, FILE_mft_data); + /* + * If record 15 cannot be opened, it is probably in + * use as an extent. Apply standard procedure for + * further extents. + */ + if (ext_ni) { + /* + * Make sure record 15 is a base extent and it has + * no name. A base inode with no name cannot be in use. + * The test based on base_mft_record fails for + * extents of MFT, so we need a special check. + * If already used, apply standard procedure. + */ + if (!ext_ni->mrec->base_mft_record + && !ext_ni->mrec->link_count) + forced_mft_data = TRUE; + ntfs_inode_close(ext_ni); + /* Double-check, in case it is used for MFT */ + if (forced_mft_data && base_ni->nr_extents) { + int i; + + for (i=0; inr_extents; i++) { + if (base_ni->extent_nis[i] + && (base_ni->extent_nis[i]->mft_no + == FILE_mft_data)) + forced_mft_data = FALSE; + } + } + } + } + if (forced_mft_data) + bit = FILE_mft_data; + else + bit = ntfs_mft_bitmap_find_free_rec(vol, base_ni); + if (bit >= 0) + goto found_free_rec; + + if (errno != ENOSPC) + goto out; + + errno = ENOSPC; + /* strerror() is intentionally used below, we want to log this error. */ + ntfs_log_error("No free mft record for $MFT: %s\n", strerror(errno)); + goto err_out; + +found_free_rec: + if (ntfs_bitmap_set_bit(mftbmp_na, bit)) { + ntfs_log_error("Failed to allocate bit in mft bitmap #2\n"); + goto err_out; + } + + ll = (bit + 1) << vol->mft_record_size_bits; + if (ll > mft_na->initialized_size) + if (ntfs_mft_rec_init(vol, ll) < 0) + goto undo_mftbmp_alloc; + /* + * We now have allocated and initialized the mft record. Need to read + * it from disk and re-format it, preserving the sequence number if it + * is not zero as well as the update sequence number if it is not zero + * or -1 (0xffff). + */ + m = ntfs_malloc(vol->mft_record_size); + if (!m) + goto undo_mftbmp_alloc; + + if (ntfs_mft_record_read(vol, bit, m)) { + free(m); + goto undo_mftbmp_alloc; + } + /* Sanity check that the mft record is really not in use. */ + if (!forced_mft_data + && (ntfs_is_file_record(m->magic) + && (m->flags & MFT_RECORD_IN_USE))) { + ntfs_log_error("Inode %lld is used but it wasn't marked in " + "$MFT bitmap. Fixed.\n", (long long)bit); + free(m); + goto undo_mftbmp_alloc; + } + + /* + * Retrieve the former seq_no and usn so that the new record + * cannot be mistaken for the former one. + * However the original record may just be garbage, so + * use some sensible value when they cannot be retrieved. + */ + seq_no = m->sequence_number; + if (le16_to_cpu(m->usa_ofs) <= (NTFS_BLOCK_SIZE - 2)) + usn = *(le16*)((u8*)m + (le16_to_cpu(m->usa_ofs) & -2)); + else + usn = const_cpu_to_le16(1); + if (ntfs_mft_record_layout(vol, bit, m)) { + ntfs_log_error("Failed to re-format mft record.\n"); + free(m); + goto undo_mftbmp_alloc; + } + if (seq_no) + m->sequence_number = seq_no; + seq_no = usn; + if (seq_no && seq_no != const_cpu_to_le16(0xffff)) + *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn; + /* Set the mft record itself in use. */ + m->flags |= MFT_RECORD_IN_USE; + /* Now need to open an ntfs inode for the mft record. */ + ni = ntfs_inode_allocate(vol); + if (!ni) { + ntfs_log_error("Failed to allocate buffer for inode.\n"); + free(m); + goto undo_mftbmp_alloc; + } + ni->mft_no = bit; + ni->mrec = m; + /* + * If we are allocating an extent mft record, make the opened inode an + * extent inode and attach it to the base inode. Also, set the base + * mft record reference in the extent inode. + */ + ni->nr_extents = -1; + ni->base_ni = base_ni; + m->base_mft_record = MK_LE_MREF(base_ni->mft_no, + le16_to_cpu(base_ni->mrec->sequence_number)); + /* + * Attach the extent inode to the base inode, reallocating + * memory if needed. + */ + if (!(base_ni->nr_extents & 3)) { + ntfs_inode **extent_nis; + int i; + + i = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *); + extent_nis = ntfs_malloc(i); + if (!extent_nis) { + free(m); + free(ni); + goto undo_mftbmp_alloc; + } + if (base_ni->nr_extents) { + memcpy(extent_nis, base_ni->extent_nis, + i - 4 * sizeof(ntfs_inode *)); + free(base_ni->extent_nis); + } + base_ni->extent_nis = extent_nis; + } + base_ni->extent_nis[base_ni->nr_extents++] = ni; + + /* Make sure the allocated inode is written out to disk later. */ + ntfs_inode_mark_dirty(ni); + /* Initialize time, allocated and data size in ntfs_inode struct. */ + ni->data_size = ni->allocated_size = 0; + ni->flags = const_cpu_to_le32(0); + ni->creation_time = ni->last_data_change_time = + ni->last_mft_change_time = + ni->last_access_time = ntfs_current_time(); + /* Update the default mft allocation position if it was used. */ + if (!base_ni) + vol->mft_data_pos = bit + 1; + /* Return the opened, allocated inode of the allocated mft record. */ + ntfs_log_error("allocated %sinode %lld\n", + base_ni ? "extent " : "", (long long)bit); +out: + ntfs_log_leave("\n"); + return ni; + +undo_mftbmp_alloc: + err = errno; + if (ntfs_bitmap_clear_bit(mftbmp_na, bit)) + ntfs_log_error("Failed to clear bit in mft bitmap.%s\n", es); + errno = err; +err_out: + if (!errno) + errno = EIO; + ni = NULL; + goto out; +} + +/** + * ntfs_mft_record_alloc - allocate an mft record on an ntfs volume + * @vol: volume on which to allocate the mft record + * @base_ni: open base inode if allocating an extent mft record or NULL + * + * Allocate an mft record in $MFT/$DATA of an open ntfs volume @vol. + * + * If @base_ni is NULL make the mft record a base mft record and allocate it at + * the default allocator position. + * + * If @base_ni is not NULL make the allocated mft record an extent record, + * allocate it starting at the mft record after the base mft record and attach + * the allocated and opened ntfs inode to the base inode @base_ni. + * + * On success return the now opened ntfs (extent) inode of the mft record. + * + * On error return NULL with errno set to the error code. + * + * To find a free mft record, we scan the mft bitmap for a zero bit. To + * optimize this we start scanning at the place specified by @base_ni or if + * @base_ni is NULL we start where we last stopped and we perform wrap around + * when we reach the end. Note, we do not try to allocate mft records below + * number 24 because numbers 0 to 15 are the defined system files anyway and 16 + * to 24 are used for storing extension mft records or used by chkdsk to store + * its log. However the record number 15 is dedicated to the first extent to + * the $DATA attribute of $MFT. This is required to avoid the possibility + * of creating a run list with a circular dependence which once written to disk + * can never be read in again. Windows will only use records 16 to 24 for + * normal files if the volume is completely out of space. We never use them + * which means that when the volume is really out of space we cannot create any + * more files while Windows can still create up to 8 small files. We can start + * doing this at some later time, it does not matter much for now. + * + * When scanning the mft bitmap, we only search up to the last allocated mft + * record. If there are no free records left in the range 24 to number of + * allocated mft records, then we extend the $MFT/$DATA attribute in order to + * create free mft records. We extend the allocated size of $MFT/$DATA by 16 + * records at a time or one cluster, if cluster size is above 16kiB. If there + * is not sufficient space to do this, we try to extend by a single mft record + * or one cluster, if cluster size is above the mft record size, but we only do + * this if there is enough free space, which we know from the values returned + * by the failed cluster allocation function when we tried to do the first + * allocation. + * + * No matter how many mft records we allocate, we initialize only the first + * allocated mft record, incrementing mft data size and initialized size + * accordingly, open an ntfs_inode for it and return it to the caller, unless + * there are less than 24 mft records, in which case we allocate and initialize + * mft records until we reach record 24 which we consider as the first free mft + * record for use by normal files. + * + * If during any stage we overflow the initialized data in the mft bitmap, we + * extend the initialized size (and data size) by 8 bytes, allocating another + * cluster if required. The bitmap data size has to be at least equal to the + * number of mft records in the mft, but it can be bigger, in which case the + * superfluous bits are padded with zeroes. + * + * Thus, when we return successfully (return value non-zero), we will have: + * - initialized / extended the mft bitmap if necessary, + * - initialized / extended the mft data if necessary, + * - set the bit corresponding to the mft record being allocated in the + * mft bitmap, + * - open an ntfs_inode for the allocated mft record, and we will + * - return the ntfs_inode. + * + * On error (return value zero), nothing will have changed. If we had changed + * anything before the error occurred, we will have reverted back to the + * starting state before returning to the caller. Thus, except for bugs, we + * should always leave the volume in a consistent state when returning from + * this function. + * + * Note, this function cannot make use of most of the normal functions, like + * for example for attribute resizing, etc, because when the run list overflows + * the base mft record and an attribute list is used, it is very important that + * the extension mft records used to store the $DATA attribute of $MFT can be + * reached without having to read the information contained inside them, as + * this would make it impossible to find them in the first place after the + * volume is dismounted. $MFT/$BITMAP probably does not need to follow this + * rule because the bitmap is not essential for finding the mft records, but on + * the other hand, handling the bitmap in this special way would make life + * easier because otherwise there might be circular invocations of functions + * when reading the bitmap but if we are careful, we should be able to avoid + * all problems. + */ +ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, ntfs_inode *base_ni) +{ + s64 ll, bit; + ntfs_attr *mft_na, *mftbmp_na; + MFT_RECORD *m; + ntfs_inode *ni = NULL; + int err; + u32 usa_ofs; + le16 seq_no, usn; + BOOL oldwarn; + + if (base_ni) + ntfs_log_enter("Entering (allocating an extent mft record for " + "base mft record %lld).\n", + (long long)base_ni->mft_no); + else + ntfs_log_enter("Entering (allocating a base mft record)\n"); + if (!vol || !vol->mft_na || !vol->mftbmp_na) { + errno = EINVAL; + goto out; + } + + if (ntfs_is_mft(base_ni)) { + ni = ntfs_mft_rec_alloc(vol, FALSE); + goto out; + } + + mft_na = vol->mft_na; + mftbmp_na = vol->mftbmp_na; +retry: + bit = ntfs_mft_bitmap_find_free_rec(vol, base_ni); + if (bit >= 0) { + ntfs_log_debug("found free record (#1) at %lld\n", + (long long)bit); + goto found_free_rec; + } + if (errno != ENOSPC) + goto out; + /* + * No free mft records left. If the mft bitmap already covers more + * than the currently used mft records, the next records are all free, + * so we can simply allocate the first unused mft record. + * Note: We also have to make sure that the mft bitmap at least covers + * the first 24 mft records as they are special and whilst they may not + * be in use, we do not allocate from them. + */ + ll = mft_na->initialized_size >> vol->mft_record_size_bits; + if (mftbmp_na->initialized_size << 3 > ll && + mftbmp_na->initialized_size > RESERVED_MFT_RECORDS / 8) { + bit = ll; + if (bit < RESERVED_MFT_RECORDS) + bit = RESERVED_MFT_RECORDS; + ntfs_log_debug("found free record (#2) at %lld\n", + (long long)bit); + goto found_free_rec; + } + /* + * The mft bitmap needs to be expanded until it covers the first unused + * mft record that we can allocate. + * Note: The smallest mft record we allocate is mft record 24. + */ + ntfs_log_debug("Status of mftbmp before extension: allocated_size 0x%llx, " + "data_size 0x%llx, initialized_size 0x%llx.\n", + (long long)mftbmp_na->allocated_size, + (long long)mftbmp_na->data_size, + (long long)mftbmp_na->initialized_size); + if (mftbmp_na->initialized_size + 8 > mftbmp_na->allocated_size) { + + const s64 old_allocated_size = mftbmp_na->allocated_size; + int ret = ntfs_mft_bitmap_extend_allocation(vol); + + if (ret == STATUS_ERROR) + goto err_out; + if (ret == STATUS_KEEP_SEARCHING) { + ret = ntfs_mft_bitmap_extend_allocation(vol); + if (ret != STATUS_OK) + goto err_out; + } + + ntfs_log_debug("Status of mftbmp after allocation extension: " + "allocated_size 0x%llx, data_size 0x%llx, " + "initialized_size 0x%llx.\n", + (long long)mftbmp_na->allocated_size, + (long long)mftbmp_na->data_size, + (long long)mftbmp_na->initialized_size); + + vol->free_mft_records += + (mftbmp_na->allocated_size - old_allocated_size) << 3; + } + /* + * We now have sufficient allocated space, extend the initialized_size + * as well as the data_size if necessary and fill the new space with + * zeroes. + */ + bit = mftbmp_na->initialized_size << 3; + if (ntfs_mft_bitmap_extend_initialized(vol)) + goto err_out; + ntfs_log_debug("Status of mftbmp after initialized extension: " + "allocated_size 0x%llx, data_size 0x%llx, " + "initialized_size 0x%llx.\n", + (long long)mftbmp_na->allocated_size, + (long long)mftbmp_na->data_size, + (long long)mftbmp_na->initialized_size); + ntfs_log_debug("found free record (#3) at %lld\n", (long long)bit); +found_free_rec: + /* @bit is the found free mft record, allocate it in the mft bitmap. */ + if (ntfs_bitmap_set_bit(mftbmp_na, bit)) { + ntfs_log_error("Failed to allocate bit in mft bitmap.\n"); + goto err_out; + } + + /* The mft bitmap is now uptodate. Deal with mft data attribute now. */ + ll = (bit + 1) << vol->mft_record_size_bits; + if (ll > mft_na->initialized_size) + if (ntfs_mft_record_init(vol, ll) < 0) + goto undo_mftbmp_alloc; + + /* + * We now have allocated and initialized the mft record. Need to read + * it from disk and re-format it, preserving the sequence number if it + * is not zero as well as the update sequence number if it is not zero + * or -1 (0xffff). + */ + m = ntfs_malloc(vol->mft_record_size); + if (!m) + goto undo_mftbmp_alloc; + + /* + * As this is allocating a new record, do not expect it to have + * been initialized previously, so do not warn over bad fixups + * (hence avoid warn flooding when an NTFS partition has been wiped). + */ + oldwarn = !NVolNoFixupWarn(vol); + NVolSetNoFixupWarn(vol); + if (ntfs_mft_record_read(vol, bit, m)) { + if (oldwarn) + NVolClearNoFixupWarn(vol); + free(m); + goto undo_mftbmp_alloc; + } + if (oldwarn) + NVolClearNoFixupWarn(vol); + + /* Sanity check that the mft record is really not in use. */ + if (ntfs_is_file_record(m->magic) && (m->flags & MFT_RECORD_IN_USE)) { + ntfs_log_error("Inode %lld is used but it wasn't marked in " + "$MFT bitmap. Fixed.\n", (long long)bit); + free(m); + goto retry; + } + seq_no = m->sequence_number; + /* + * As ntfs_mft_record_read() returns what has been read + * even when the fixups have been found bad, we have to + * check where we fetch the initial usn from. + */ + usa_ofs = le16_to_cpu(m->usa_ofs); + if (!(usa_ofs & 1) && (usa_ofs < NTFS_BLOCK_SIZE)) { + usn = *(le16*)((u8*)m + usa_ofs); + } else + usn = const_cpu_to_le16(1); + if (ntfs_mft_record_layout(vol, bit, m)) { + ntfs_log_error("Failed to re-format mft record.\n"); + free(m); + goto undo_mftbmp_alloc; + } + if (seq_no) + m->sequence_number = seq_no; + seq_no = usn; + if (seq_no && seq_no != const_cpu_to_le16(0xffff)) + *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn; + /* Set the mft record itself in use. */ + m->flags |= MFT_RECORD_IN_USE; + /* Now need to open an ntfs inode for the mft record. */ + ni = ntfs_inode_allocate(vol); + if (!ni) { + ntfs_log_error("Failed to allocate buffer for inode.\n"); + free(m); + goto undo_mftbmp_alloc; + } + ni->mft_no = bit; + ni->mrec = m; + /* + * If we are allocating an extent mft record, make the opened inode an + * extent inode and attach it to the base inode. Also, set the base + * mft record reference in the extent inode. + */ + if (base_ni) { + ni->nr_extents = -1; + ni->base_ni = base_ni; + m->base_mft_record = MK_LE_MREF(base_ni->mft_no, + le16_to_cpu(base_ni->mrec->sequence_number)); + /* + * Attach the extent inode to the base inode, reallocating + * memory if needed. + */ + if (!(base_ni->nr_extents & 3)) { + ntfs_inode **extent_nis; + int i; + + i = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *); + extent_nis = ntfs_malloc(i); + if (!extent_nis) { + free(m); + free(ni); + goto undo_mftbmp_alloc; + } + if (base_ni->nr_extents) { + memcpy(extent_nis, base_ni->extent_nis, + i - 4 * sizeof(ntfs_inode *)); + free(base_ni->extent_nis); + } + base_ni->extent_nis = extent_nis; + } + base_ni->extent_nis[base_ni->nr_extents++] = ni; + } + /* Make sure the allocated inode is written out to disk later. */ + ntfs_inode_mark_dirty(ni); + /* Initialize time, allocated and data size in ntfs_inode struct. */ + ni->data_size = ni->allocated_size = 0; + ni->flags = const_cpu_to_le32(0); + ni->creation_time = ni->last_data_change_time = + ni->last_mft_change_time = + ni->last_access_time = ntfs_current_time(); + /* Update the default mft allocation position if it was used. */ + if (!base_ni) + vol->mft_data_pos = bit + 1; + /* Return the opened, allocated inode of the allocated mft record. */ + ntfs_log_debug("allocated %sinode 0x%llx.\n", + base_ni ? "extent " : "", (long long)bit); + vol->free_mft_records--; +out: + ntfs_log_leave("\n"); + return ni; + +undo_mftbmp_alloc: + err = errno; + if (ntfs_bitmap_clear_bit(mftbmp_na, bit)) + ntfs_log_error("Failed to clear bit in mft bitmap.%s\n", es); + errno = err; +err_out: + if (!errno) + errno = EIO; + ni = NULL; + goto out; +} + +/** + * ntfs_mft_record_free - free an mft record on an ntfs volume + * @vol: volume on which to free the mft record + * @ni: open ntfs inode of the mft record to free + * + * Free the mft record of the open inode @ni on the mounted ntfs volume @vol. + * Note that this function calls ntfs_inode_close() internally and hence you + * cannot use the pointer @ni any more after this function returns success. + * + * On success return 0 and on error return -1 with errno set to the error code. + */ +int ntfs_mft_record_free(ntfs_volume *vol, ntfs_inode *ni) +{ + u64 mft_no; + int err; + u16 seq_no; + le16 old_seq_no; + + ntfs_log_trace("Entering for inode 0x%llx.\n", (long long) ni->mft_no); + + if (!vol || !vol->mftbmp_na || !ni) { + errno = EINVAL; + return -1; + } + + /* Cache the mft reference for later. */ + mft_no = ni->mft_no; + + /* Mark the mft record as not in use. */ + ni->mrec->flags &= ~MFT_RECORD_IN_USE; + + /* Increment the sequence number, skipping zero, if it is not zero. */ + old_seq_no = ni->mrec->sequence_number; + seq_no = le16_to_cpu(old_seq_no); + if (seq_no == 0xffff) + seq_no = 1; + else if (seq_no) + seq_no++; + ni->mrec->sequence_number = cpu_to_le16(seq_no); + + /* Set the inode dirty and write it out. */ + ntfs_inode_mark_dirty(ni); + if (ntfs_inode_sync(ni)) { + err = errno; + goto sync_rollback; + } + + /* Clear the bit in the $MFT/$BITMAP corresponding to this record. */ + if (ntfs_bitmap_clear_bit(vol->mftbmp_na, mft_no)) { + err = errno; + // FIXME: If ntfs_bitmap_clear_run() guarantees rollback on + // error, this could be changed to goto sync_rollback; + goto bitmap_rollback; + } + + /* Throw away the now freed inode. */ +#if CACHE_NIDATA_SIZE + if (!ntfs_inode_real_close(ni)) { +#else + if (!ntfs_inode_close(ni)) { +#endif + vol->free_mft_records++; + return 0; + } + err = errno; + + /* Rollback what we did... */ +bitmap_rollback: + if (ntfs_bitmap_set_bit(vol->mftbmp_na, mft_no)) + ntfs_log_debug("Eeek! Rollback failed in ntfs_mft_record_free(). " + "Leaving inconsistent metadata!\n"); +sync_rollback: + ni->mrec->flags |= MFT_RECORD_IN_USE; + ni->mrec->sequence_number = old_seq_no; + ntfs_inode_mark_dirty(ni); + errno = err; + return -1; +} + +/** + * ntfs_mft_usn_dec - Decrement USN by one + * @mrec: pointer to an mft record + * + * On success return 0 and on error return -1 with errno set. + */ +int ntfs_mft_usn_dec(MFT_RECORD *mrec) +{ + u16 usn; + le16 *usnp; + + if (!mrec) { + errno = EINVAL; + return -1; + } + usnp = (le16*)((char*)mrec + le16_to_cpu(mrec->usa_ofs)); + usn = le16_to_cpup(usnp); + if (usn-- <= 1) + usn = 0xfffe; + *usnp = cpu_to_le16(usn); + + return 0; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/misc.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/misc.c new file mode 100644 index 0000000..03fb592 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/misc.c @@ -0,0 +1,77 @@ +/** + * misc.c : miscellaneous : + * - dealing with errors in memory allocation + * + * Copyright (c) 2008 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif + +#include "types.h" +#include "misc.h" +#include "logging.h" + +/** + * ntfs_calloc + * + * Return a pointer to the allocated memory or NULL if the request fails. + */ +void *ntfs_calloc(size_t size) +{ + void *p; + + p = calloc(1, size); + if (!p) + ntfs_log_perror("Failed to calloc %lld bytes", (long long)size); + return p; +} + +void *ntfs_malloc(size_t size) +{ + void *p; + + p = malloc(size); + if (!p) + ntfs_log_perror("Failed to malloc %lld bytes", (long long)size); + return p; +} + +void *ntfs_realloc(void *ptr, size_t size) +{ + void *p; + + p = realloc(ptr, size); + if (!p) + ntfs_log_perror("Failed to realloc %lld bytes", + (long long)size); + return p; +} + +void ntfs_free(void *p) +{ + free(p); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/mst.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/mst.c new file mode 100644 index 0000000..af17edb --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/mst.c @@ -0,0 +1,257 @@ +/** + * mst.c - Multi sector fixup handling code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2004 Anton Altaparmakov + * Copyright (c) 2006-2009 Szabolcs Szakacsits + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "mst.h" +#include "logging.h" + +/* + * Basic validation of a NTFS multi-sector record. The record size must be a + * multiple of the logical sector size; and the update sequence array must be + * properly aligned, of the expected length, and must end before the last le16 + * in the first logical sector. + */ +static BOOL +is_valid_record(u32 size, u16 usa_ofs, u16 usa_count) +{ + return size % NTFS_BLOCK_SIZE == 0 && + usa_ofs % 2 == 0 && + usa_count == 1 + (size / NTFS_BLOCK_SIZE) && + usa_ofs + ((u32)usa_count * 2) <= NTFS_BLOCK_SIZE - 2; +} + +/** + * ntfs_mst_post_read_fixup - deprotect multi sector transfer protected data + * @b: pointer to the data to deprotect + * @size: size in bytes of @b + * + * Perform the necessary post read multi sector transfer fixups and detect the + * presence of incomplete multi sector transfers. - In that case, overwrite the + * magic of the ntfs record header being processed with "BAAD" (in memory only!) + * and abort processing. + * + * Return 0 on success and -1 on error, with errno set to the error code. The + * following error codes are defined: + * EINVAL Invalid arguments or invalid NTFS record in buffer @b. + * EIO Multi sector transfer error was detected. Magic of the NTFS + * record in @b will have been set to "BAAD". + */ +int ntfs_mst_post_read_fixup_warn(NTFS_RECORD *b, const u32 size, + BOOL warn) +{ + u16 usa_ofs, usa_count, usn; + u16 *usa_pos, *data_pos; + + ntfs_log_trace("Entering\n"); + + /* Setup the variables. */ + usa_ofs = le16_to_cpu(b->usa_ofs); + usa_count = le16_to_cpu(b->usa_count); + + if (!is_valid_record(size, usa_ofs, usa_count)) { + errno = EINVAL; + if (warn) { + ntfs_log_perror("%s: magic: 0x%08lx size: %ld " + " usa_ofs: %d usa_count: %u", + __FUNCTION__, + (long)le32_to_cpu(*(le32 *)b), + (long)size, (int)usa_ofs, + (unsigned int)usa_count); + } + return -1; + } + /* Position of usn in update sequence array. */ + usa_pos = (u16*)b + usa_ofs/sizeof(u16); + /* + * The update sequence number which has to be equal to each of the + * u16 values before they are fixed up. Note no need to care for + * endianness since we are comparing and moving data for on disk + * structures which means the data is consistent. - If it is + * consistency the wrong endianness it doesn't make any difference. + */ + usn = *usa_pos; + /* + * Position in protected data of first u16 that needs fixing up. + */ + data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1; + /* + * Check for incomplete multi sector transfer(s). + */ + while (--usa_count) { + if (*data_pos != usn) { + /* + * Incomplete multi sector transfer detected! )-: + * Set the magic to "BAAD" and return failure. + * Note that magic_BAAD is already converted to le32. + */ + errno = EIO; + ntfs_log_perror("Incomplete multi-sector transfer: " + "magic: 0x%08x size: %d usa_ofs: %d usa_count:" + " %d data: %d usn: %d", le32_to_cpu(*(le32 *)b), size, + usa_ofs, usa_count, *data_pos, usn); + b->magic = magic_BAAD; + return -1; + } + data_pos += NTFS_BLOCK_SIZE/sizeof(u16); + } + /* Re-setup the variables. */ + usa_count = le16_to_cpu(b->usa_count); + data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1; + /* Fixup all sectors. */ + while (--usa_count) { + /* + * Increment position in usa and restore original data from + * the usa into the data buffer. + */ + *data_pos = *(++usa_pos); + /* Increment position in data as well. */ + data_pos += NTFS_BLOCK_SIZE/sizeof(u16); + } + return 0; +} + +/* + * Deprotect multi sector transfer protected data + * with a warning if an error is found. + */ + +int ntfs_mst_post_read_fixup(NTFS_RECORD *b, const u32 size) +{ + return (ntfs_mst_post_read_fixup_warn(b,size,TRUE)); +} + +/** + * ntfs_mst_pre_write_fixup - apply multi sector transfer protection + * @b: pointer to the data to protect + * @size: size in bytes of @b + * + * Perform the necessary pre write multi sector transfer fixup on the data + * pointer to by @b of @size. + * + * Return 0 if fixups applied successfully or -1 if no fixups were performed + * due to errors. In that case errno i set to the error code (EINVAL). + * + * NOTE: We consider the absence / invalidity of an update sequence array to + * mean error. This means that you have to create a valid update sequence + * array header in the ntfs record before calling this function, otherwise it + * will fail (the header needs to contain the position of the update sequence + * array together with the number of elements in the array). You also need to + * initialise the update sequence number before calling this function + * otherwise a random word will be used (whatever was in the record at that + * position at that time). + */ +int ntfs_mst_pre_write_fixup(NTFS_RECORD *b, const u32 size) +{ + u16 usa_ofs, usa_count, usn; + le16 le_usn; + le16 *usa_pos, *data_pos; + + ntfs_log_trace("Entering\n"); + + /* Sanity check + only fixup if it makes sense. */ + if (!b || ntfs_is_baad_record(b->magic) || + ntfs_is_hole_record(b->magic)) { + errno = EINVAL; + ntfs_log_perror("%s: bad argument", __FUNCTION__); + return -1; + } + /* Setup the variables. */ + usa_ofs = le16_to_cpu(b->usa_ofs); + usa_count = le16_to_cpu(b->usa_count); + + if (!is_valid_record(size, usa_ofs, usa_count)) { + errno = EINVAL; + ntfs_log_perror("%s", __FUNCTION__); + return -1; + } + /* Position of usn in update sequence array. */ + usa_pos = (le16*)((u8*)b + usa_ofs); + /* + * Cyclically increment the update sequence number + * (skipping 0 and -1, i.e. 0xffff). + */ + usn = le16_to_cpup(usa_pos) + 1; + if (usn == 0xffff || !usn) + usn = 1; + le_usn = cpu_to_le16(usn); + *usa_pos = le_usn; + /* Position in data of first le16 that needs fixing up. */ + data_pos = (le16*)b + NTFS_BLOCK_SIZE/sizeof(le16) - 1; + /* Fixup all sectors. */ + while (--usa_count) { + /* + * Increment the position in the usa and save the + * original data from the data buffer into the usa. + */ + *(++usa_pos) = *data_pos; + /* Apply fixup to data. */ + *data_pos = le_usn; + /* Increment position in data as well. */ + data_pos += NTFS_BLOCK_SIZE/sizeof(le16); + } + return 0; +} + +/** + * ntfs_mst_post_write_fixup - deprotect multi sector transfer protected data + * @b: pointer to the data to deprotect + * + * Perform the necessary post write multi sector transfer fixup, not checking + * for any errors, because we assume we have just used + * ntfs_mst_pre_write_fixup(), thus the data will be fine or we would never + * have gotten here. + */ +void ntfs_mst_post_write_fixup(NTFS_RECORD *b) +{ + u16 *usa_pos, *data_pos; + + u16 usa_ofs = le16_to_cpu(b->usa_ofs); + u16 usa_count = le16_to_cpu(b->usa_count); + + ntfs_log_trace("Entering\n"); + + /* Position of usn in update sequence array. */ + usa_pos = (u16*)b + usa_ofs/sizeof(u16); + + /* Position in protected data of first u16 that needs fixing up. */ + data_pos = (u16*)b + NTFS_BLOCK_SIZE/sizeof(u16) - 1; + + /* Fixup all sectors. */ + while (--usa_count) { + /* + * Increment position in usa and restore original data from + * the usa into the data buffer. + */ + *data_pos = *(++usa_pos); + + /* Increment position in data as well. */ + data_pos += NTFS_BLOCK_SIZE/sizeof(u16); + } +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/object_id.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/object_id.c new file mode 100644 index 0000000..090070f --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/object_id.c @@ -0,0 +1,636 @@ +/** + * object_id.c - Processing of object ids + * + * This module is part of ntfs-3g library + * + * Copyright (c) 2009-2019 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_SYS_SYSMACROS_H +#include +#endif + +#include "compat.h" +#include "types.h" +#include "debug.h" +#include "attrib.h" +#include "inode.h" +#include "dir.h" +#include "volume.h" +#include "mft.h" +#include "index.h" +#include "lcnalloc.h" +#include "object_id.h" +#include "logging.h" +#include "misc.h" +#include "xattrs.h" + +/* + * Endianness considerations + * + * According to RFC 4122, GUIDs should be printed with the most + * significant byte first, and the six fields be compared individually + * for ordering. RFC 4122 does not define the internal representation. + * + * Windows apparently stores the first three fields in little endian + * order, and the last two fields in big endian order. + * + * Here we always copy disk images with no endianness change, + * and, for indexing, GUIDs are compared as if they were a sequence + * of four little-endian unsigned 32 bit integers (as Windows + * does it that way.) + * + * --------------------- begin from RFC 4122 ---------------------- + * Consider each field of the UUID to be an unsigned integer as shown + * in the table in section Section 4.1.2. Then, to compare a pair of + * UUIDs, arithmetically compare the corresponding fields from each + * UUID in order of significance and according to their data type. + * Two UUIDs are equal if and only if all the corresponding fields + * are equal. + * + * UUIDs, as defined in this document, can also be ordered + * lexicographically. For a pair of UUIDs, the first one follows the + * second if the most significant field in which the UUIDs differ is + * greater for the first UUID. The second precedes the first if the + * most significant field in which the UUIDs differ is greater for + * the second UUID. + * + * The fields are encoded as 16 octets, with the sizes and order of the + * fields defined above, and with each field encoded with the Most + * Significant Byte first (known as network byte order). Note that the + * field names, particularly for multiplexed fields, follow historical + * practice. + * + * 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | time_low | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | time_mid | time_hi_and_version | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |clk_seq_hi_res | clk_seq_low | node (0-1) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * | node (2-5) | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * ---------------------- end from RFC 4122 ----------------------- + */ + +typedef struct { + union { + /* alignment may be needed to evaluate collations */ + u32 alignment; + GUID guid; + } object_id; +} OBJECT_ID_INDEX_KEY; + +typedef struct { + le64 file_id; + GUID birth_volume_id; + GUID birth_object_id; + GUID domain_id; +} OBJECT_ID_INDEX_DATA; // known as OBJ_ID_INDEX_DATA + +struct OBJECT_ID_INDEX { /* index entry in $Extend/$ObjId */ + INDEX_ENTRY_HEADER header; + OBJECT_ID_INDEX_KEY key; + OBJECT_ID_INDEX_DATA data; +} ; + +static ntfschar objid_index_name[] = { const_cpu_to_le16('$'), + const_cpu_to_le16('O') }; + +/* + * Set the index for a new object id + * + * Returns 0 if success + * -1 if failure, explained by errno + */ + +static int set_object_id_index(ntfs_inode *ni, ntfs_index_context *xo, + const OBJECT_ID_ATTR *object_id) +{ + struct OBJECT_ID_INDEX indx; + u64 file_id_cpu; + le64 file_id; + le16 seqn; + + seqn = ni->mrec->sequence_number; + file_id_cpu = MK_MREF(ni->mft_no,le16_to_cpu(seqn)); + file_id = cpu_to_le64(file_id_cpu); + indx.header.data_offset = const_cpu_to_le16( + sizeof(INDEX_ENTRY_HEADER) + + sizeof(OBJECT_ID_INDEX_KEY)); + indx.header.data_length = const_cpu_to_le16( + sizeof(OBJECT_ID_INDEX_DATA)); + indx.header.reservedV = const_cpu_to_le32(0); + indx.header.length = const_cpu_to_le16( + sizeof(struct OBJECT_ID_INDEX)); + indx.header.key_length = const_cpu_to_le16( + sizeof(OBJECT_ID_INDEX_KEY)); + indx.header.flags = const_cpu_to_le16(0); + indx.header.reserved = const_cpu_to_le16(0); + + memcpy(&indx.key.object_id,object_id,sizeof(GUID)); + + indx.data.file_id = file_id; + memcpy(&indx.data.birth_volume_id, + &object_id->birth_volume_id,sizeof(GUID)); + memcpy(&indx.data.birth_object_id, + &object_id->birth_object_id,sizeof(GUID)); + memcpy(&indx.data.domain_id, + &object_id->domain_id,sizeof(GUID)); + ntfs_index_ctx_reinit(xo); + return (ntfs_ie_add(xo,(INDEX_ENTRY*)&indx)); +} + +/* + * Open the $Extend/$ObjId file and its index + * + * Return the index context if opened + * or NULL if an error occurred (errno tells why) + * + * The index has to be freed and inode closed when not needed any more. + */ + +static ntfs_index_context *open_object_id_index(ntfs_volume *vol) +{ + u64 inum; + ntfs_inode *ni; + ntfs_inode *dir_ni; + ntfs_index_context *xo; + + /* do not use path_name_to inode - could reopen root */ + dir_ni = ntfs_inode_open(vol, FILE_Extend); + ni = (ntfs_inode*)NULL; + if (dir_ni) { + inum = ntfs_inode_lookup_by_mbsname(dir_ni,"$ObjId"); + if (inum != (u64)-1) + ni = ntfs_inode_open(vol, inum); + ntfs_inode_close(dir_ni); + } + if (ni) { + xo = ntfs_index_ctx_get(ni, objid_index_name, 2); + if (!xo) { + ntfs_inode_close(ni); + } + } else + xo = (ntfs_index_context*)NULL; + return (xo); +} + + +/* + * Merge object_id data stored in the index into + * a full object_id struct. + * + * returns 0 if merging successful + * -1 if no data could be merged. This is generally not an error + */ + +static int merge_index_data(ntfs_inode *ni, + const OBJECT_ID_ATTR *objectid_attr, + OBJECT_ID_ATTR *full_objectid) +{ + OBJECT_ID_INDEX_KEY key; + struct OBJECT_ID_INDEX *entry; + ntfs_index_context *xo; + ntfs_inode *xoni; + int res; + + res = -1; + xo = open_object_id_index(ni->vol); + if (xo) { + memcpy(&key.object_id,objectid_attr,sizeof(GUID)); + if (!ntfs_index_lookup(&key, + sizeof(OBJECT_ID_INDEX_KEY), xo)) { + entry = (struct OBJECT_ID_INDEX*)xo->entry; + /* make sure inode numbers match */ + if (entry + && (MREF(le64_to_cpu(entry->data.file_id)) + == ni->mft_no)) { + memcpy(&full_objectid->birth_volume_id, + &entry->data.birth_volume_id, + sizeof(GUID)); + memcpy(&full_objectid->birth_object_id, + &entry->data.birth_object_id, + sizeof(GUID)); + memcpy(&full_objectid->domain_id, + &entry->data.domain_id, + sizeof(GUID)); + res = 0; + } + } + xoni = xo->ni; + ntfs_index_ctx_put(xo); + ntfs_inode_close(xoni); + } + return (res); +} + + +/* + * Remove an object id index entry if attribute present + * + * Returns the size of existing object id + * (the existing object_d is returned) + * -1 if failure, explained by errno + */ + +static int remove_object_id_index(ntfs_attr *na, ntfs_index_context *xo, + OBJECT_ID_ATTR *old_attr) +{ + OBJECT_ID_INDEX_KEY key; + struct OBJECT_ID_INDEX *entry; + s64 size; + int ret; + + ret = na->data_size; + if (ret) { + /* read the existing object id attribute */ + size = ntfs_attr_pread(na, 0, sizeof(GUID), old_attr); + if (size >= (s64)sizeof(GUID)) { + memcpy(&key.object_id, + &old_attr->object_id,sizeof(GUID)); + if (!ntfs_index_lookup(&key, + sizeof(OBJECT_ID_INDEX_KEY), xo)) { + entry = (struct OBJECT_ID_INDEX*)xo->entry; + memcpy(&old_attr->birth_volume_id, + &entry->data.birth_volume_id, + sizeof(GUID)); + memcpy(&old_attr->birth_object_id, + &entry->data.birth_object_id, + sizeof(GUID)); + memcpy(&old_attr->domain_id, + &entry->data.domain_id, + sizeof(GUID)); + if (ntfs_index_rm(xo)) + ret = -1; + } + } else { + ret = -1; + errno = ENODATA; + } + } + return (ret); +} + + +/* + * Update the object id and index + * + * The object_id attribute should have been created and the + * non-duplication of the GUID should have been checked before. + * + * Returns 0 if success + * -1 if failure, explained by errno + * If could not remove the existing index, nothing is done, + * If could not write the new data, no index entry is inserted + * If failed to insert the index, data is removed + */ + +static int update_object_id(ntfs_inode *ni, ntfs_index_context *xo, + const OBJECT_ID_ATTR *value, size_t size) +{ + OBJECT_ID_ATTR old_attr; + ntfs_attr *na; + int oldsize; + int written; + int res; + + res = 0; + + na = ntfs_attr_open(ni, AT_OBJECT_ID, AT_UNNAMED, 0); + if (na) { + memset(&old_attr, 0, sizeof(OBJECT_ID_ATTR)); + /* remove the existing index entry */ + oldsize = remove_object_id_index(na,xo,&old_attr); + if (oldsize < 0) + res = -1; + else { + /* resize attribute */ + res = ntfs_attr_truncate(na, (s64)sizeof(GUID)); + /* write the object_id in attribute */ + if (!res && value) { + written = (int)ntfs_attr_pwrite(na, + (s64)0, (s64)sizeof(GUID), + &value->object_id); + if (written != (s64)sizeof(GUID)) { + ntfs_log_error("Failed to update " + "object id\n"); + errno = EIO; + res = -1; + } + } + /* overwrite index data with new value */ + memcpy(&old_attr, value, + (size < sizeof(OBJECT_ID_ATTR) + ? size : sizeof(OBJECT_ID_ATTR))); + if (!res + && set_object_id_index(ni,xo,&old_attr)) { + /* + * If cannot index, try to remove the object + * id and log the error. There will be an + * inconsistency if removal fails. + */ + ntfs_attr_rm(na); + ntfs_log_error("Failed to index object id." + " Possible corruption.\n"); + } + } + ntfs_attr_close(na); + NInoSetDirty(ni); + } else + res = -1; + return (res); +} + +/* + * Add a (dummy) object id to an inode if it does not exist + * + * returns 0 if attribute was inserted (or already present) + * -1 if adding failed (explained by errno) + */ + +static int add_object_id(ntfs_inode *ni, int flags) +{ + int res; + u8 dummy; + + res = -1; /* default return */ + if (!ntfs_attr_exist(ni,AT_OBJECT_ID, AT_UNNAMED,0)) { + if (!(flags & XATTR_REPLACE)) { + /* + * no object id attribute : add one, + * apparently, this does not feed the new value in + * Note : NTFS version must be >= 3 + */ + if (ni->vol->major_ver >= 3) { + res = ntfs_attr_add(ni, AT_OBJECT_ID, + AT_UNNAMED, 0, &dummy, (s64)0); + NInoSetDirty(ni); + } else + errno = EOPNOTSUPP; + } else + errno = ENODATA; + } else { + if (flags & XATTR_CREATE) + errno = EEXIST; + else + res = 0; + } + return (res); +} + + +/* + * Delete an object_id index entry + * + * Returns 0 if success + * -1 if failure, explained by errno + */ + +int ntfs_delete_object_id_index(ntfs_inode *ni) +{ + ntfs_index_context *xo; + ntfs_inode *xoni; + ntfs_attr *na; + OBJECT_ID_ATTR old_attr; + int res; + + res = 0; + na = ntfs_attr_open(ni, AT_OBJECT_ID, AT_UNNAMED, 0); + if (na) { + /* + * read the existing object id + * and un-index it + */ + xo = open_object_id_index(ni->vol); + if (xo) { + if (remove_object_id_index(na,xo,&old_attr) < 0) + res = -1; + xoni = xo->ni; + ntfs_index_entry_mark_dirty(xo); + NInoSetDirty(xoni); + ntfs_index_ctx_put(xo); + ntfs_inode_close(xoni); + } + ntfs_attr_close(na); + } + return (res); +} + + +/* + * Get the ntfs object id into an extended attribute + * + * If present, the object_id from the attribute and the GUIDs + * from the index are returned (formatted as OBJECT_ID_ATTR) + * + * Returns the global size (can be 0, 16 or 64) + * and the buffer is updated if it is long enough + */ + +int ntfs_get_ntfs_object_id(ntfs_inode *ni, char *value, size_t size) +{ + OBJECT_ID_ATTR full_objectid; + OBJECT_ID_ATTR *objectid_attr; + s64 attr_size; + int full_size; + + full_size = 0; /* default to no data and some error to be defined */ + if (ni) { + objectid_attr = (OBJECT_ID_ATTR*)ntfs_attr_readall(ni, + AT_OBJECT_ID,(ntfschar*)NULL, 0, &attr_size); + if (objectid_attr) { + /* restrict to only GUID present in attr */ + if (attr_size == sizeof(GUID)) { + memcpy(&full_objectid.object_id, + objectid_attr,sizeof(GUID)); + full_size = sizeof(GUID); + /* get data from index, if any */ + if (!merge_index_data(ni, objectid_attr, + &full_objectid)) { + full_size = sizeof(OBJECT_ID_ATTR); + } + if (full_size <= (s64)size) { + if (value) + memcpy(value,&full_objectid, + full_size); + else + errno = EINVAL; + } + } else { + /* unexpected size, better return unsupported */ + errno = EOPNOTSUPP; + full_size = 0; + } + free(objectid_attr); + } else + errno = ENODATA; + } + return (full_size ? (int)full_size : -errno); +} + +/* + * Set the object id from an extended attribute + * + * The first 16 bytes are the new object id, they can be followed + * by the birth volume id, the birth object id and the domain id. + * If they are not present, their previous value is kept. + * Only the object id is stored into the attribute, all the fields + * are stored into the index. + * + * Returns 0, or -1 if there is a problem + */ + +int ntfs_set_ntfs_object_id(ntfs_inode *ni, + const char *value, size_t size, int flags) +{ + OBJECT_ID_INDEX_KEY key; + ntfs_inode *xoni; + ntfs_index_context *xo; + int res; + + res = 0; + if (ni && value && (size >= sizeof(GUID))) { + xo = open_object_id_index(ni->vol); + if (xo) { + /* make sure the GUID was not used elsewhere */ + memcpy(&key.object_id, value, sizeof(GUID)); + if ((ntfs_index_lookup(&key, + sizeof(OBJECT_ID_INDEX_KEY), xo)) + || (MREF_LE(((struct OBJECT_ID_INDEX*)xo->entry) + ->data.file_id) == ni->mft_no)) { + ntfs_index_ctx_reinit(xo); + res = add_object_id(ni, flags); + if (!res) { + /* update value and index */ + res = update_object_id(ni,xo, + (const OBJECT_ID_ATTR*)value, + size); + } + } else { + /* GUID is present elsewhere */ + res = -1; + errno = EEXIST; + } + xoni = xo->ni; + ntfs_index_entry_mark_dirty(xo); + NInoSetDirty(xoni); + ntfs_index_ctx_put(xo); + ntfs_inode_close(xoni); + } else { + res = -1; + } + } else { + errno = EINVAL; + res = -1; + } + return (res ? -1 : 0); +} + +/* + * Remove the object id + * + * Returns 0, or -1 if there is a problem + */ + +int ntfs_remove_ntfs_object_id(ntfs_inode *ni) +{ + int res; + int olderrno; + ntfs_attr *na; + ntfs_inode *xoni; + ntfs_index_context *xo; + int oldsize; + OBJECT_ID_ATTR old_attr; + + res = 0; + if (ni) { + /* + * open and delete the object id + */ + na = ntfs_attr_open(ni, AT_OBJECT_ID, + AT_UNNAMED,0); + if (na) { + /* first remove index (old object id needed) */ + xo = open_object_id_index(ni->vol); + if (xo) { + oldsize = remove_object_id_index(na,xo, + &old_attr); + if (oldsize < 0) { + res = -1; + } else { + /* now remove attribute */ + res = ntfs_attr_rm(na); + if (res + && (oldsize > (int)sizeof(GUID))) { + /* + * If we could not remove the + * attribute, try to restore the + * index and log the error. There + * will be an inconsistency if + * the reindexing fails. + */ + set_object_id_index(ni, xo, + &old_attr); + ntfs_log_error( + "Failed to remove object id." + " Possible corruption.\n"); + } + } + + xoni = xo->ni; + ntfs_index_entry_mark_dirty(xo); + NInoSetDirty(xoni); + ntfs_index_ctx_put(xo); + ntfs_inode_close(xoni); + } + olderrno = errno; + ntfs_attr_close(na); + /* avoid errno pollution */ + if (errno == ENOENT) + errno = olderrno; + } else { + errno = ENODATA; + res = -1; + } + NInoSetDirty(ni); + } else { + errno = EINVAL; + res = -1; + } + return (res ? -1 : 0); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/realpath.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/realpath.c new file mode 100644 index 0000000..d56667e --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/realpath.c @@ -0,0 +1,103 @@ +/* + * realpath.c - realpath() aware of device mapper + * Originated from the util-linux project. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#ifdef HAVE_LIMITS_H +#include +#endif +#ifdef HAVE_CTYPE_H +#include +#endif + +#include "param.h" +#include "realpath.h" + +/* If there is no realpath() on the system, provide a dummy one. */ +#ifndef HAVE_REALPATH +char *ntfs_realpath(const char *path, char *resolved_path) +{ + strncpy(resolved_path, path, PATH_MAX); + resolved_path[PATH_MAX] = '\0'; + return resolved_path; +} +#endif + + +#ifdef linux + +/* + * Converts private "dm-N" names to "/dev/mapper/" + * + * Since 2.6.29 (patch 784aae735d9b0bba3f8b9faef4c8b30df3bf0128) kernel sysfs + * provides the real DM device names in /sys/block//dm/name + */ +static char * +canonicalize_dm_name(const char *ptname, char *canonical) +{ + FILE *f; + size_t sz; + char name[MAPPERNAMELTH + 16]; + char path[sizeof(name) + 16]; + char *res = NULL; + + snprintf(path, sizeof(path), "/sys/block/%s/dm/name", ptname); + if (!(f = fopen(path, "r"))) + return NULL; + + /* read "\n" from sysfs */ + if (fgets(name, sizeof(name), f) && (sz = strlen(name)) > 1) { + name[sz - 1] = '\0'; + snprintf(path, sizeof(path), "/dev/mapper/%s", name); + res = strcpy(canonical, path); + } + fclose(f); + return res; +} + +/* + * Canonicalize a device path + * + * Workaround from "basinilya" for fixing device mapper paths. + * + * Background (Phillip Susi, 2011-04-09) + * - ntfs-3g canonicalizes the device name so that if you mount with + * /dev/mapper/foo, the device name listed in mtab is /dev/dm-n, + * so you can not umount /dev/mapper/foo + * - umount won't even recognize and translate /dev/dm-n to the mount + * point, apparently because of the '-' involved. Editing mtab and + * removing the '-' allows you to umount /dev/dmn successfully. + * + * This code restores the devmapper name after canonicalization, + * until a proper fix is implemented. + */ + +char *ntfs_realpath_canonicalize(const char *path, char *canonical) +{ + char *p; + + if (path == NULL) + return NULL; + + if (!ntfs_realpath(path, canonical)) + return NULL; + + p = strrchr(canonical, '/'); + if (p && strncmp(p, "/dm-", 4) == 0 && isdigit(*(p + 4))) { + p = canonicalize_dm_name(p+1, canonical); + if (p) + return p; + } + + return canonical; +} + +#endif diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/reparse.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/reparse.c new file mode 100644 index 0000000..fca18a6 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/reparse.c @@ -0,0 +1,1455 @@ +/** + * reparse.c - Processing of reparse points + * + * This module is part of ntfs-3g library + * + * Copyright (c) 2008-2021 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef MAJOR_IN_MKDEV +#include +#endif +#ifdef MAJOR_IN_SYSMACROS +#include +#endif + +#include "compat.h" +#include "types.h" +#include "debug.h" +#include "layout.h" +#include "attrib.h" +#include "inode.h" +#include "dir.h" +#include "volume.h" +#include "mft.h" +#include "index.h" +#include "lcnalloc.h" +#include "logging.h" +#include "misc.h" +#include "reparse.h" +#include "xattrs.h" +#include "ea.h" + +struct MOUNT_POINT_REPARSE_DATA { /* reparse data for junctions */ + le16 subst_name_offset; + le16 subst_name_length; + le16 print_name_offset; + le16 print_name_length; + char path_buffer[1]; /* above data assume this is char array */ +} ; + +struct SYMLINK_REPARSE_DATA { /* reparse data for symlinks */ + le16 subst_name_offset; + le16 subst_name_length; + le16 print_name_offset; + le16 print_name_length; + le32 flags; /* 1 for full target, otherwise 0 */ + char path_buffer[1]; /* above data assume this is char array */ +} ; + +struct WSL_LINK_REPARSE_DATA { + le32 type; + char link[1]; +} ; + +struct REPARSE_INDEX { /* index entry in $Extend/$Reparse */ + INDEX_ENTRY_HEADER header; + REPARSE_INDEX_KEY key; + le32 filling; +} ; + +static const ntfschar dir_junction_head[] = { + const_cpu_to_le16('\\'), + const_cpu_to_le16('?'), + const_cpu_to_le16('?'), + const_cpu_to_le16('\\') +} ; + +static const ntfschar vol_junction_head[] = { + const_cpu_to_le16('\\'), + const_cpu_to_le16('?'), + const_cpu_to_le16('?'), + const_cpu_to_le16('\\'), + const_cpu_to_le16('V'), + const_cpu_to_le16('o'), + const_cpu_to_le16('l'), + const_cpu_to_le16('u'), + const_cpu_to_le16('m'), + const_cpu_to_le16('e'), + const_cpu_to_le16('{'), +} ; + +static ntfschar reparse_index_name[] = { const_cpu_to_le16('$'), + const_cpu_to_le16('R') }; + +static const char mappingdir[] = ".NTFS-3G/"; + +/* + * Fix a file name with doubtful case in some directory index + * and return the name with the casing used in directory. + * + * Should only be used to translate paths stored with case insensitivity + * (such as directory junctions) when no case conflict is expected. + * If there some ambiguity, the name which collates first is returned. + * + * The name is converted to upper case and searched the usual way. + * The collation rules for file names are such that we should get the + * first candidate if any. + */ + +static u64 ntfs_fix_file_name(ntfs_inode *dir_ni, ntfschar *uname, + int uname_len) +{ + ntfs_volume *vol = dir_ni->vol; + ntfs_index_context *icx; + u64 mref; + le64 lemref; + int lkup; + int olderrno; + int i; + u32 cpuchar; + INDEX_ENTRY *entry; + FILE_NAME_ATTR *found; + struct { + FILE_NAME_ATTR attr; + ntfschar file_name[NTFS_MAX_NAME_LEN + 1]; + } find; + + mref = (u64)-1; /* default return (not found) */ + icx = ntfs_index_ctx_get(dir_ni, NTFS_INDEX_I30, 4); + if (icx) { + if (uname_len > NTFS_MAX_NAME_LEN) + uname_len = NTFS_MAX_NAME_LEN; + find.attr.file_name_length = uname_len; + for (i=0; iupcase_len) + && (le16_to_cpu(vol->upcase[cpuchar]) < cpuchar)) + find.attr.file_name[i] = vol->upcase[cpuchar]; + else + find.attr.file_name[i] = uname[i]; + } + olderrno = errno; + lkup = ntfs_index_lookup((char*)&find, uname_len, icx); + if (errno == ENOENT) + errno = olderrno; + /* + * We generally only get the first matching candidate, + * so we still have to check whether this is a real match + */ + if (icx->entry && (icx->entry->ie_flags & INDEX_ENTRY_END)) + /* get next entry if reaching end of block */ + entry = ntfs_index_next(icx->entry, icx); + else + entry = icx->entry; + if (entry) { + found = &entry->key.file_name; + if (lkup + && ntfs_names_are_equal(find.attr.file_name, + find.attr.file_name_length, + found->file_name, found->file_name_length, + IGNORE_CASE, + vol->upcase, vol->upcase_len)) + lkup = 0; + if (!lkup) { + /* + * name found : + * fix original name and return inode + */ + lemref = entry->indexed_file; + mref = le64_to_cpu(lemref); + if (NVolCaseSensitive(vol) || !vol->locase) { + for (i=0; ifile_name_length; + i++) + uname[i] = found->file_name[i]; + } else { + for (i=0; ifile_name_length; + i++) { + u16 u = le16_to_cpu(found-> + file_name[i]); + uname[i] = (u < vol->upcase_len) + ? vol->locase[u] + : found-> + file_name[i]; + } + } + } + } + ntfs_index_ctx_put(icx); + } + return (mref); +} + +/* + * Search for a directory junction or a symbolic link + * along the target path, with target defined as a full absolute path + * + * Returns the path translated to a Linux path + * or NULL if the path is not valid + */ + +static char *search_absolute(ntfs_volume *vol, ntfschar *path, + int count, BOOL isdir) +{ + ntfs_inode *ni; + u64 inum; + char *target; + int start; + int len; + + target = (char*)NULL; /* default return */ + ni = ntfs_inode_open(vol, (MFT_REF)FILE_root); + if (ni) { + start = 0; + /* + * Examine and translate the path, until we reach either + * - the end, + * - an unknown item + * - a non-directory + * - another reparse point, + * A reparse point is not dereferenced, it will be + * examined later when the translated path is dereferenced, + * however the final part of the path will not be adjusted + * to correct case. + */ + do { + len = 0; + while (((start + len) < count) + && (path[start + len] != const_cpu_to_le16('\\'))) + len++; + inum = ntfs_fix_file_name(ni, &path[start], len); + ntfs_inode_close(ni); + ni = (ntfs_inode*)NULL; + if (inum != (u64)-1) { + inum = MREF(inum); + ni = ntfs_inode_open(vol, inum); + start += len; + if (start < count) + path[start++] = const_cpu_to_le16('/'); + } + } while (ni + && (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + && !(ni->flags & FILE_ATTR_REPARSE_POINT) + && (start < count)); + if (ni + && ((ni->mrec->flags & MFT_RECORD_IS_DIRECTORY ? isdir : !isdir) + || (ni->flags & FILE_ATTR_REPARSE_POINT))) + if (ntfs_ucstombs(path, count, &target, 0) < 0) { + if (target) { + free(target); + target = (char*)NULL; + } + } + if (ni) + ntfs_inode_close(ni); + } + return (target); +} + +/* + * Search for a symbolic link along the target path, + * with the target defined as a relative path + * + * Note : the path used to access the current inode, may be + * different from the one implied in the target definition, + * when an inode has names in several directories. + * + * Returns the path translated to a Linux path + * or NULL if the path is not valid + */ + +static char *search_relative(ntfs_inode *ni, ntfschar *path, int count) +{ + char *target = (char*)NULL; + ntfs_inode *curni; + ntfs_inode *newni; + u64 inum; + int pos; + int lth; + BOOL ok; + BOOL morelinks; + int max = 32; /* safety */ + + pos = 0; + ok = TRUE; + morelinks = FALSE; + curni = ntfs_dir_parent_inode(ni); + /* + * Examine and translate the path, until we reach either + * - the end, + * - an unknown item + * - a non-directory + * - another reparse point, + * A reparse point is not dereferenced, it will be + * examined later when the translated path is dereferenced, + * however the final part of the path will not be adjusted + * to correct case. + */ + while (curni && ok && !morelinks && (pos < (count - 1)) && --max) { + if ((count >= (pos + 2)) + && (path[pos] == const_cpu_to_le16('.')) + && (path[pos+1] == const_cpu_to_le16('\\'))) { + path[pos+1] = const_cpu_to_le16('/'); + pos += 2; + } else { + if ((count >= (pos + 3)) + && (path[pos] == const_cpu_to_le16('.')) + &&(path[pos+1] == const_cpu_to_le16('.')) + && (path[pos+2] == const_cpu_to_le16('\\'))) { + path[pos+2] = const_cpu_to_le16('/'); + pos += 3; + newni = ntfs_dir_parent_inode(curni); + if (curni != ni) + ntfs_inode_close(curni); + curni = newni; + if (!curni) + ok = FALSE; + } else { + lth = 0; + while (((pos + lth) < count) + && (path[pos + lth] != const_cpu_to_le16('\\'))) + lth++; + if (lth > 0) + inum = ntfs_fix_file_name(curni,&path[pos],lth); + else + inum = (u64)-1; + if (!lth + || ((curni != ni) + && ntfs_inode_close(curni)) + || (inum == (u64)-1)) + ok = FALSE; + else { + curni = ntfs_inode_open(ni->vol, MREF(inum)); + if (!curni) + ok = FALSE; + else { + if (curni->flags & FILE_ATTR_REPARSE_POINT) + morelinks = TRUE; + if (ok && ((pos + lth) < count)) { + path[pos + lth] = const_cpu_to_le16('/'); + pos += lth + 1; + if (morelinks + && ntfs_inode_close(curni)) + ok = FALSE; + } else { + pos += lth; + if (!morelinks + && (ni->mrec->flags ^ curni->mrec->flags) + & MFT_RECORD_IS_DIRECTORY) + ok = FALSE; + if (ntfs_inode_close(curni)) + ok = FALSE; + } + } + } + } + } + } + + if (ok && (ntfs_ucstombs(path, count, &target, 0) < 0)) { + free(target); // needed ? + target = (char*)NULL; + } + return (target); +} + +/* + * Check whether a drive letter has been defined in .NTFS-3G + * + * Returns 1 if found, + * 0 if not found, + * -1 if there was an error (described by errno) + */ + +static int ntfs_drive_letter(ntfs_volume *vol, ntfschar letter) +{ + char defines[NTFS_MAX_NAME_LEN + 5]; + char *drive; + int ret; + int sz; + int olderrno; + ntfs_inode *ni; + + ret = -1; + drive = (char*)NULL; + sz = ntfs_ucstombs(&letter, 1, &drive, 0); + if (sz > 0) { + strcpy(defines,mappingdir); + if ((*drive >= 'a') && (*drive <= 'z')) + *drive += 'A' - 'a'; + strcat(defines,drive); + strcat(defines,":"); + olderrno = errno; + ni = ntfs_pathname_to_inode(vol, NULL, defines); + if (ni && !ntfs_inode_close(ni)) + ret = 1; + else + if (errno == ENOENT) { + ret = 0; + /* avoid errno pollution */ + errno = olderrno; + } + } + if (drive) + free(drive); + return (ret); +} + +/* + * Check whether reparse data describes a valid wsl special file + * which is either a socket, a fifo, or a character or block device + * + * Return zero if valid, otherwise returns a negative error code + */ + +int ntfs_reparse_check_wsl(ntfs_inode *ni, const REPARSE_POINT *reparse) +{ + int res; + + res = -EOPNOTSUPP; + switch (reparse->reparse_tag) { + case IO_REPARSE_TAG_AF_UNIX : + case IO_REPARSE_TAG_LX_FIFO : + case IO_REPARSE_TAG_LX_CHR : + case IO_REPARSE_TAG_LX_BLK : + if (!reparse->reparse_data_length + && (ni->flags & FILE_ATTRIBUTE_RECALL_ON_OPEN)) + res = 0; + break; + default : + break; + } + if (res) + errno = EOPNOTSUPP; + return (res); +} + +/* + * Do some sanity checks on reparse data + * + * Microsoft reparse points have an 8-byte header whereas + * non-Microsoft reparse points have a 24-byte header. In each case, + * 'reparse_data_length' must equal the number of non-header bytes. + * + * If the reparse data looks like a junction point or symbolic + * link, more checks can be done. + * + */ + +static BOOL valid_reparse_data(ntfs_inode *ni, + const REPARSE_POINT *reparse_attr, size_t size) +{ + BOOL ok; + unsigned int offs; + unsigned int lth; + const struct MOUNT_POINT_REPARSE_DATA *mount_point_data; + const struct SYMLINK_REPARSE_DATA *symlink_data; + const struct WSL_LINK_REPARSE_DATA *wsl_reparse_data; + + ok = ni && reparse_attr + && (size >= sizeof(REPARSE_POINT)) + && (reparse_attr->reparse_tag != IO_REPARSE_TAG_RESERVED_ZERO) + && (((size_t)le16_to_cpu(reparse_attr->reparse_data_length) + + sizeof(REPARSE_POINT) + + ((reparse_attr->reparse_tag & + IO_REPARSE_TAG_IS_MICROSOFT) ? 0 : sizeof(GUID))) == size); + if (ok) { + switch (reparse_attr->reparse_tag) { + case IO_REPARSE_TAG_MOUNT_POINT : + if (size < sizeof(REPARSE_POINT) + + sizeof(struct MOUNT_POINT_REPARSE_DATA)) { + ok = FALSE; + break; + } + mount_point_data = (const struct MOUNT_POINT_REPARSE_DATA*) + reparse_attr->reparse_data; + offs = le16_to_cpu(mount_point_data->subst_name_offset); + lth = le16_to_cpu(mount_point_data->subst_name_length); + /* consistency checks */ + if (!(ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + || ((size_t)((sizeof(REPARSE_POINT) + + sizeof(struct MOUNT_POINT_REPARSE_DATA) + + offs + lth)) > size)) + ok = FALSE; + break; + case IO_REPARSE_TAG_SYMLINK : + if (size < sizeof(REPARSE_POINT) + + sizeof(struct SYMLINK_REPARSE_DATA)) { + ok = FALSE; + break; + } + symlink_data = (const struct SYMLINK_REPARSE_DATA*) + reparse_attr->reparse_data; + offs = le16_to_cpu(symlink_data->subst_name_offset); + lth = le16_to_cpu(symlink_data->subst_name_length); + if ((size_t)((sizeof(REPARSE_POINT) + + sizeof(struct SYMLINK_REPARSE_DATA) + + offs + lth)) > size) + ok = FALSE; + break; + case IO_REPARSE_TAG_LX_SYMLINK : + wsl_reparse_data = (const struct WSL_LINK_REPARSE_DATA*) + reparse_attr->reparse_data; + if ((le16_to_cpu(reparse_attr->reparse_data_length) + <= sizeof(wsl_reparse_data->type)) + || (wsl_reparse_data->type != const_cpu_to_le32(2))) + ok = FALSE; + break; + case IO_REPARSE_TAG_AF_UNIX : + case IO_REPARSE_TAG_LX_FIFO : + case IO_REPARSE_TAG_LX_CHR : + case IO_REPARSE_TAG_LX_BLK : + if (reparse_attr->reparse_data_length + || !(ni->flags & FILE_ATTRIBUTE_RECALL_ON_OPEN)) + ok = FALSE; + break; + default : + break; + } + } + if (!ok) + errno = EINVAL; + return (ok); +} + +/* + * Check and translate the target of a junction point or + * a full absolute symbolic link. + * + * A full target definition begins with "\??\" or "\\?\" + * + * The fully defined target is redefined as a relative link, + * - either to the target if found on the same device. + * - or into the /.NTFS-3G directory for the user to define + * In the first situation, the target is translated to case-sensitive path. + * + * returns the target converted to a relative symlink + * or NULL if there were some problem, as described by errno + */ + +static char *ntfs_get_fulllink(ntfs_volume *vol, ntfschar *junction, + int count, const char *mnt_point, BOOL isdir) +{ + char *target; + char *fulltarget; + int sz; + char *q; + enum { DIR_JUNCTION, VOL_JUNCTION, NO_JUNCTION } kind; + + target = (char*)NULL; + fulltarget = (char*)NULL; + /* + * For a valid directory junction we want \??\x:\ + * where \ is an individual char and x a non-null char + */ + if ((count >= 7) + && !memcmp(junction,dir_junction_head,8) + && junction[4] + && (junction[5] == const_cpu_to_le16(':')) + && (junction[6] == const_cpu_to_le16('\\'))) + kind = DIR_JUNCTION; + else + /* + * For a valid volume junction we want \\?\Volume{ + * and a final \ (where \ is an individual char) + */ + if ((count >= 12) + && !memcmp(junction,vol_junction_head,22) + && (junction[count-1] == const_cpu_to_le16('\\'))) + kind = VOL_JUNCTION; + else + kind = NO_JUNCTION; + /* + * Directory junction with an explicit path and + * no specific definition for the drive letter : + * try to interpret as a target on the same volume + */ + if ((kind == DIR_JUNCTION) + && (count >= 7) + && junction[7] + && !ntfs_drive_letter(vol, junction[4])) { + target = search_absolute(vol,&junction[7],count - 7, isdir); + if (target) { + fulltarget = (char*)ntfs_malloc(strlen(mnt_point) + + strlen(target) + 2); + if (fulltarget) { + strcpy(fulltarget,mnt_point); + strcat(fulltarget,"/"); + strcat(fulltarget,target); + } + free(target); + } + } + /* + * Volume junctions or directory junctions with + * target not found on current volume : + * link to /.NTFS-3G/target which the user can + * define as a symbolic link to the real target + */ + if (((kind == DIR_JUNCTION) && !fulltarget) + || (kind == VOL_JUNCTION)) { + sz = ntfs_ucstombs(&junction[4], + (kind == VOL_JUNCTION ? count - 5 : count - 4), + &target, 0); + if ((sz > 0) && target) { + /* reverse slashes */ + for (q=target; *q; q++) + if (*q == '\\') + *q = '/'; + /* force uppercase drive letter */ + if ((target[1] == ':') + && (target[0] >= 'a') + && (target[0] <= 'z')) + target[0] += 'A' - 'a'; + fulltarget = (char*)ntfs_malloc(strlen(mnt_point) + + sizeof(mappingdir) + strlen(target) + 1); + if (fulltarget) { + strcpy(fulltarget,mnt_point); + strcat(fulltarget,"/"); + strcat(fulltarget,mappingdir); + strcat(fulltarget,target); + } + } + if (target) + free(target); + } + return (fulltarget); +} + +/* + * Check and translate the target of an absolute symbolic link. + * + * An absolute target definition begins with "\" or "x:\" + * + * The absolute target is redefined as a relative link, + * - either to the target if found on the same device. + * - or into the /.NTFS-3G directory for the user to define + * In the first situation, the target is translated to case-sensitive path. + * + * returns the target converted to a relative symlink + * or NULL if there were some problem, as described by errno + */ + +char *ntfs_get_abslink(ntfs_volume *vol, ntfschar *junction, int count, + const char *mnt_point __attribute__((unused)), + BOOL isdir) +{ + char *target; + char *fulltarget; + int sz; + char *q; + enum { FULL_PATH, ABS_PATH, REJECTED_PATH } kind; + + target = (char*)NULL; + fulltarget = (char*)NULL; + /* + * For a full valid path we want x:\ + * where \ is an individual char and x a non-null char + */ + if ((count >= 3) + && junction[0] + && (junction[1] == const_cpu_to_le16(':')) + && (junction[2] == const_cpu_to_le16('\\'))) + kind = FULL_PATH; + else + /* + * For an absolute path we want an initial \ + */ + if ((count >= 0) + && (junction[0] == const_cpu_to_le16('\\'))) + kind = ABS_PATH; + else + kind = REJECTED_PATH; + /* + * Full path, with a drive letter and + * no specific definition for the drive letter : + * try to interpret as a target on the same volume. + * Do the same for an abs path with no drive letter. + */ + if (((kind == FULL_PATH) + && (count >= 3) + && junction[3] + && !ntfs_drive_letter(vol, junction[0])) + || (kind == ABS_PATH)) { + if (kind == ABS_PATH) + target = search_absolute(vol, &junction[1], + count - 1, isdir); + else + target = search_absolute(vol, &junction[3], + count - 3, isdir); + if (target) { + fulltarget = (char*)ntfs_malloc( + strlen(vol->abs_mnt_point) + + strlen(target) + 2); + if (fulltarget) { + strcpy(fulltarget,vol->abs_mnt_point); + strcat(fulltarget,"/"); + strcat(fulltarget,target); + } + free(target); + } + } + /* + * full path with target not found on current volume : + * link to /.NTFS-3G/target which the user can + * define as a symbolic link to the real target + */ + if ((kind == FULL_PATH) && !fulltarget) { + sz = ntfs_ucstombs(&junction[0], + count,&target, 0); + if ((sz > 0) && target) { + /* reverse slashes */ + for (q=target; *q; q++) + if (*q == '\\') + *q = '/'; + /* force uppercase drive letter */ + if ((target[1] == ':') + && (target[0] >= 'a') + && (target[0] <= 'z')) + target[0] += 'A' - 'a'; + fulltarget = (char*)ntfs_malloc( + strlen(vol->abs_mnt_point) + + sizeof(mappingdir) + strlen(target) + 1); + if (fulltarget) { + strcpy(fulltarget,vol->abs_mnt_point); + strcat(fulltarget,"/"); + strcat(fulltarget,mappingdir); + strcat(fulltarget,target); + } + } + if (target) + free(target); + } + return (fulltarget); +} + +/* + * Check and translate the target of a relative symbolic link. + * + * A relative target definition does not begin with "\" + * + * The original definition of relative target is kept, it is just + * translated to a case-sensitive path. + * + * returns the target converted to a relative symlink + * or NULL if there were some problem, as described by errno + */ + +static char *ntfs_get_rellink(ntfs_inode *ni, ntfschar *junction, int count) +{ + char *target; + + target = search_relative(ni,junction,count); + return (target); +} + +/* + * Get the target for a junction point or symbolic link + * Should only be called for files or directories with reparse data + * + * returns the target converted to a relative path, or NULL + * if some error occurred, as described by errno + * errno is EOPNOTSUPP if the reparse point is not a valid + * symbolic link or directory junction + */ + +char *ntfs_make_symlink(ntfs_inode *ni, const char *mnt_point) +{ + s64 attr_size = 0; + char *target; + unsigned int offs; + unsigned int lth; + ntfs_volume *vol; + REPARSE_POINT *reparse_attr; + struct MOUNT_POINT_REPARSE_DATA *mount_point_data; + struct SYMLINK_REPARSE_DATA *symlink_data; + struct WSL_LINK_REPARSE_DATA *wsl_link_data; + enum { FULL_TARGET, ABS_TARGET, REL_TARGET } kind; + ntfschar *p; + BOOL bad; + BOOL isdir; + + target = (char*)NULL; + bad = TRUE; + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + != const_cpu_to_le16(0); + vol = ni->vol; + reparse_attr = (REPARSE_POINT*)ntfs_attr_readall(ni, + AT_REPARSE_POINT,(ntfschar*)NULL, 0, &attr_size); + if (reparse_attr && attr_size + && valid_reparse_data(ni, reparse_attr, attr_size)) { + switch (reparse_attr->reparse_tag) { + case IO_REPARSE_TAG_MOUNT_POINT : + mount_point_data = (struct MOUNT_POINT_REPARSE_DATA*) + reparse_attr->reparse_data; + offs = le16_to_cpu(mount_point_data->subst_name_offset); + lth = le16_to_cpu(mount_point_data->subst_name_length); + /* reparse data consistency has been checked */ + target = ntfs_get_fulllink(vol, + (ntfschar*)&mount_point_data->path_buffer[offs], + lth/2, mnt_point, isdir); + if (target) + bad = FALSE; + break; + case IO_REPARSE_TAG_SYMLINK : + symlink_data = (struct SYMLINK_REPARSE_DATA*) + reparse_attr->reparse_data; + offs = le16_to_cpu(symlink_data->subst_name_offset); + lth = le16_to_cpu(symlink_data->subst_name_length); + p = (ntfschar*)&symlink_data->path_buffer[offs]; + /* + * Predetermine the kind of target, + * the called function has to make a full check + */ + if (*p++ == const_cpu_to_le16('\\')) { + if ((*p == const_cpu_to_le16('?')) + || (*p == const_cpu_to_le16('\\'))) + kind = FULL_TARGET; + else + kind = ABS_TARGET; + } else + if (*p == const_cpu_to_le16(':')) + kind = ABS_TARGET; + else + kind = REL_TARGET; + p--; + /* reparse data consistency has been checked */ + switch (kind) { + case FULL_TARGET : + if (!(symlink_data->flags + & const_cpu_to_le32(1))) { + target = ntfs_get_fulllink(vol, + p, lth/2, + mnt_point, isdir); + if (target) + bad = FALSE; + } + break; + case ABS_TARGET : + if (symlink_data->flags + & const_cpu_to_le32(1)) { + target = ntfs_get_abslink(vol, + p, lth/2, + mnt_point, isdir); + if (target) + bad = FALSE; + } + break; + case REL_TARGET : + if (symlink_data->flags + & const_cpu_to_le32(1)) { + target = ntfs_get_rellink(ni, + p, lth/2); + if (target) + bad = FALSE; + } + break; + } + break; + case IO_REPARSE_TAG_LX_SYMLINK : + wsl_link_data = (struct WSL_LINK_REPARSE_DATA*) + reparse_attr->reparse_data; + if (wsl_link_data->type == const_cpu_to_le32(2)) { + lth = le16_to_cpu( + reparse_attr->reparse_data_length) + - sizeof(wsl_link_data->type); + target = (char*)ntfs_malloc(lth + 1); + if (target) { + memcpy(target, wsl_link_data->link, + lth); + target[lth] = 0; + bad = FALSE; + } + } + break; + } + free(reparse_attr); + } + if (bad) + errno = EOPNOTSUPP; + return (target); +} + +/* + * Check whether a reparse point looks like a junction point + * or a symbolic link. + * Should only be called for files or directories with reparse data + * + * The validity of the target is not checked. + */ + +BOOL ntfs_possible_symlink(ntfs_inode *ni) +{ + s64 attr_size = 0; + REPARSE_POINT *reparse_attr; + BOOL possible; + + possible = FALSE; + reparse_attr = (REPARSE_POINT*)ntfs_attr_readall(ni, + AT_REPARSE_POINT,(ntfschar*)NULL, 0, &attr_size); + if (reparse_attr && attr_size) { + switch (reparse_attr->reparse_tag) { + case IO_REPARSE_TAG_MOUNT_POINT : + case IO_REPARSE_TAG_SYMLINK : + case IO_REPARSE_TAG_LX_SYMLINK : + possible = TRUE; + default : ; + } + free(reparse_attr); + } + return (possible); +} + + +/* + * Set the index for new reparse data + * + * Returns 0 if success + * -1 if failure, explained by errno + */ + +static int set_reparse_index(ntfs_inode *ni, ntfs_index_context *xr, + le32 reparse_tag) +{ + struct REPARSE_INDEX indx; + u64 file_id_cpu; + le64 file_id; + le16 seqn; + + seqn = ni->mrec->sequence_number; + file_id_cpu = MK_MREF(ni->mft_no,le16_to_cpu(seqn)); + file_id = cpu_to_le64(file_id_cpu); + indx.header.data_offset = const_cpu_to_le16( + sizeof(INDEX_ENTRY_HEADER) + + sizeof(REPARSE_INDEX_KEY)); + indx.header.data_length = const_cpu_to_le16(0); + indx.header.reservedV = const_cpu_to_le32(0); + indx.header.length = const_cpu_to_le16( + sizeof(struct REPARSE_INDEX)); + indx.header.key_length = const_cpu_to_le16( + sizeof(REPARSE_INDEX_KEY)); + indx.header.flags = const_cpu_to_le16(0); + indx.header.reserved = const_cpu_to_le16(0); + indx.key.reparse_tag = reparse_tag; + /* danger on processors which require proper alignment ! */ + memcpy(&indx.key.file_id, &file_id, 8); + indx.filling = const_cpu_to_le32(0); + ntfs_index_ctx_reinit(xr); + return (ntfs_ie_add(xr,(INDEX_ENTRY*)&indx)); +} + + +/* + * Remove a reparse data index entry if attribute present + * + * Returns the size of existing reparse data + * (the existing reparse tag is returned) + * -1 if failure, explained by errno + */ + +static int remove_reparse_index(ntfs_attr *na, ntfs_index_context *xr, + le32 *preparse_tag) +{ + REPARSE_INDEX_KEY key; + u64 file_id_cpu; + le64 file_id; + s64 size; + le16 seqn; + int ret; + + ret = na->data_size; + if (ret) { + /* read the existing reparse_tag */ + size = ntfs_attr_pread(na, 0, 4, preparse_tag); + if (size == 4) { + seqn = na->ni->mrec->sequence_number; + file_id_cpu = MK_MREF(na->ni->mft_no,le16_to_cpu(seqn)); + file_id = cpu_to_le64(file_id_cpu); + key.reparse_tag = *preparse_tag; + /* danger on processors which require proper alignment ! */ + memcpy(&key.file_id, &file_id, 8); + if (!ntfs_index_lookup(&key, sizeof(REPARSE_INDEX_KEY), xr) + && ntfs_index_rm(xr)) + ret = -1; + } else { + ret = -1; + errno = ENODATA; + } + } + return (ret); +} + +/* + * Open the $Extend/$Reparse file and its index + * + * Return the index context if opened + * or NULL if an error occurred (errno tells why) + * + * The index has to be freed and inode closed when not needed any more. + */ + +static ntfs_index_context *open_reparse_index(ntfs_volume *vol) +{ + u64 inum; + ntfs_inode *ni; + ntfs_inode *dir_ni; + ntfs_index_context *xr; + + /* do not use path_name_to inode - could reopen root */ + dir_ni = ntfs_inode_open(vol, FILE_Extend); + ni = (ntfs_inode*)NULL; + if (dir_ni) { + inum = ntfs_inode_lookup_by_mbsname(dir_ni,"$Reparse"); + if (inum != (u64)-1) + ni = ntfs_inode_open(vol, inum); + ntfs_inode_close(dir_ni); + } + if (ni) { + xr = ntfs_index_ctx_get(ni, reparse_index_name, 2); + if (!xr) { + ntfs_inode_close(ni); + } + } else + xr = (ntfs_index_context*)NULL; + return (xr); +} + + +/* + * Update the reparse data and index + * + * The reparse data attribute should have been created, and + * an existing index is expected if there is an existing value. + * + * Returns 0 if success + * -1 if failure, explained by errno + * If could not remove the existing index, nothing is done, + * If could not write the new data, no index entry is inserted + * If failed to insert the index, data is removed + */ + +static int update_reparse_data(ntfs_inode *ni, ntfs_index_context *xr, + const char *value, size_t size) +{ + int res; + int written; + int oldsize; + ntfs_attr *na; + le32 reparse_tag; + + res = 0; + na = ntfs_attr_open(ni, AT_REPARSE_POINT, AT_UNNAMED, 0); + if (na) { + /* remove the existing reparse data */ + oldsize = remove_reparse_index(na,xr,&reparse_tag); + if (oldsize < 0) + res = -1; + else { + /* resize attribute */ + res = ntfs_attr_truncate(na, (s64)size); + /* overwrite value if any */ + if (!res && value) { + written = (int)ntfs_attr_pwrite(na, + (s64)0, (s64)size, value); + if (written != (s64)size) { + ntfs_log_error("Failed to update " + "reparse data\n"); + errno = EIO; + res = -1; + } + } + if (!res + && set_reparse_index(ni,xr, + ((const REPARSE_POINT*)value)->reparse_tag) + && (oldsize > 0)) { + /* + * If cannot index, try to remove the reparse + * data and log the error. There will be an + * inconsistency if removal fails. + */ + ntfs_attr_rm(na); + ntfs_log_error("Failed to index reparse data." + " Possible corruption.\n"); + } + } + ntfs_attr_close(na); + NInoSetDirty(ni); + } else + res = -1; + return (res); +} + + +/* + * Delete a reparse index entry + * + * Returns 0 if success + * -1 if failure, explained by errno + */ + +int ntfs_delete_reparse_index(ntfs_inode *ni) +{ + ntfs_index_context *xr; + ntfs_inode *xrni; + ntfs_attr *na; + le32 reparse_tag; + int res; + + res = 0; + na = ntfs_attr_open(ni, AT_REPARSE_POINT, AT_UNNAMED, 0); + if (na) { + /* + * read the existing reparse data (the tag is enough) + * and un-index it + */ + xr = open_reparse_index(ni->vol); + if (xr) { + if (remove_reparse_index(na,xr,&reparse_tag) < 0) + res = -1; + xrni = xr->ni; + ntfs_index_entry_mark_dirty(xr); + NInoSetDirty(xrni); + ntfs_index_ctx_put(xr); + ntfs_inode_close(xrni); + } + ntfs_attr_close(na); + } + return (res); +} + + +/* + * Get the ntfs reparse data into an extended attribute + * + * Returns the reparse data size + * and the buffer is updated if it is long enough + */ + +int ntfs_get_ntfs_reparse_data(ntfs_inode *ni, char *value, size_t size) +{ + REPARSE_POINT *reparse_attr; + s64 attr_size; + + attr_size = 0; /* default to no data and no error */ + if (ni) { + if (ni->flags & FILE_ATTR_REPARSE_POINT) { + reparse_attr = (REPARSE_POINT*)ntfs_attr_readall(ni, + AT_REPARSE_POINT,(ntfschar*)NULL, 0, &attr_size); + if (reparse_attr) { + if (attr_size <= (s64)size) { + if (value) + memcpy(value,reparse_attr, + attr_size); + else + errno = EINVAL; + } + free(reparse_attr); + } + } else + errno = ENODATA; + } + return (attr_size ? (int)attr_size : -errno); +} + +/* + * Set the reparse data from an extended attribute + * + * Warning : the new data is not checked + * + * Returns 0, or -1 if there is a problem + */ + +int ntfs_set_ntfs_reparse_data(ntfs_inode *ni, + const char *value, size_t size, int flags) +{ + int res; + u8 dummy; + ntfs_inode *xrni; + ntfs_index_context *xr; + + res = 0; + /* + * reparse data compatibily with EA is not checked + * any more, it is required by Windows 10, but may + * lead to problems with earlier versions. + */ + if (ni && valid_reparse_data(ni, (const REPARSE_POINT*)value, size)) { + xr = open_reparse_index(ni->vol); + if (xr) { + if (!ntfs_attr_exist(ni,AT_REPARSE_POINT, + AT_UNNAMED,0)) { + if (!(flags & XATTR_REPLACE)) { + /* + * no reparse data attribute : add one, + * apparently, this does not feed the new value in + * Note : NTFS version must be >= 3 + */ + if (ni->vol->major_ver >= 3) { + res = ntfs_attr_add(ni, + AT_REPARSE_POINT, + AT_UNNAMED,0,&dummy, + (s64)0); + if (!res) { + ni->flags |= + FILE_ATTR_REPARSE_POINT; + NInoFileNameSetDirty(ni); + } + NInoSetDirty(ni); + } else { + errno = EOPNOTSUPP; + res = -1; + } + } else { + errno = ENODATA; + res = -1; + } + } else { + if (flags & XATTR_CREATE) { + errno = EEXIST; + res = -1; + } + } + if (!res) { + /* update value and index */ + res = update_reparse_data(ni,xr,value,size); + } + xrni = xr->ni; + ntfs_index_entry_mark_dirty(xr); + NInoSetDirty(xrni); + ntfs_index_ctx_put(xr); + ntfs_inode_close(xrni); + } else { + res = -1; + } + } else { + errno = EINVAL; + res = -1; + } + return (res ? -1 : 0); +} + +/* + * Remove the reparse data + * + * Returns 0, or -1 if there is a problem + */ + +int ntfs_remove_ntfs_reparse_data(ntfs_inode *ni) +{ + int res; + int olderrno; + ntfs_attr *na; + ntfs_inode *xrni; + ntfs_index_context *xr; + le32 reparse_tag; + + res = 0; + if (ni) { + /* + * open and delete the reparse data + */ + na = ntfs_attr_open(ni, AT_REPARSE_POINT, + AT_UNNAMED,0); + if (na) { + /* first remove index (reparse data needed) */ + xr = open_reparse_index(ni->vol); + if (xr) { + if (remove_reparse_index(na,xr, + &reparse_tag) < 0) { + res = -1; + } else { + /* now remove attribute */ + res = ntfs_attr_rm(na); + if (!res) { + ni->flags &= + ~FILE_ATTR_REPARSE_POINT; + NInoFileNameSetDirty(ni); + } else { + /* + * If we could not remove the + * attribute, try to restore the + * index and log the error. There + * will be an inconsistency if + * the reindexing fails. + */ + set_reparse_index(ni, xr, + reparse_tag); + ntfs_log_error( + "Failed to remove reparse data." + " Possible corruption.\n"); + } + } + xrni = xr->ni; + ntfs_index_entry_mark_dirty(xr); + NInoSetDirty(xrni); + ntfs_index_ctx_put(xr); + ntfs_inode_close(xrni); + } + olderrno = errno; + ntfs_attr_close(na); + /* avoid errno pollution */ + if (errno == ENOENT) + errno = olderrno; + } else { + errno = ENODATA; + res = -1; + } + NInoSetDirty(ni); + } else { + errno = EINVAL; + res = -1; + } + return (res ? -1 : 0); +} + +/* + * Set reparse data for a WSL type symlink + */ + +int ntfs_reparse_set_wsl_symlink(ntfs_inode *ni, + const ntfschar *target, int target_len) +{ + int res; + int len; + int reparse_len; + char *utarget; + REPARSE_POINT *reparse; + struct WSL_LINK_REPARSE_DATA *data; + + res = -1; + utarget = (char*)NULL; + len = ntfs_ucstombs(target, target_len, &utarget, 0); + if (len > 0) { + reparse_len = sizeof(REPARSE_POINT) + sizeof(data->type) + len; + reparse = (REPARSE_POINT*)malloc(reparse_len); + if (reparse) { + data = (struct WSL_LINK_REPARSE_DATA*) + reparse->reparse_data; + reparse->reparse_tag = IO_REPARSE_TAG_LX_SYMLINK; + reparse->reparse_data_length + = cpu_to_le16(sizeof(data->type) + len); + reparse->reserved = const_cpu_to_le16(0); + data->type = const_cpu_to_le32(2); + memcpy(data->link, utarget, len); + res = ntfs_set_ntfs_reparse_data(ni, + (char*)reparse, reparse_len, 0); + free(reparse); + } + } + free(utarget); + return (res); +} + +/* + * Set reparse data for a WSL special file other than a symlink + * (socket, fifo, character or block device) + */ + +int ntfs_reparse_set_wsl_not_symlink(ntfs_inode *ni, mode_t mode) +{ + int res; + int len; + int reparse_len; + le32 reparse_tag; + REPARSE_POINT *reparse; + + res = -1; + len = 0; + switch (mode) { + case S_IFSOCK : + reparse_tag = IO_REPARSE_TAG_AF_UNIX; + break; + case S_IFIFO : + reparse_tag = IO_REPARSE_TAG_LX_FIFO; + break; + case S_IFCHR : + reparse_tag = IO_REPARSE_TAG_LX_CHR; + break; + case S_IFBLK : + reparse_tag = IO_REPARSE_TAG_LX_BLK; + break; + default : + len = -1; + errno = EOPNOTSUPP; + break; + } + if (len >= 0) { + reparse_len = sizeof(REPARSE_POINT) + len; + reparse = (REPARSE_POINT*)malloc(reparse_len); + if (reparse) { + reparse->reparse_tag = reparse_tag; + reparse->reparse_data_length = cpu_to_le16(len); + reparse->reserved = const_cpu_to_le16(0); + res = ntfs_set_ntfs_reparse_data(ni, + (char*)reparse, reparse_len, 0); + free(reparse); + } + } + return (res); +} + + +/* + * Get the reparse data into a buffer + * + * Returns the buffer if the reparse data exists and is valid + * NULL otherwise (with errno set according to the cause). + * When a buffer is returned, it has to be freed by caller. + */ + +REPARSE_POINT *ntfs_get_reparse_point(ntfs_inode *ni) +{ + s64 attr_size = 0; + REPARSE_POINT *reparse_attr; + + reparse_attr = (REPARSE_POINT*)NULL; + if (ni) { + reparse_attr = (REPARSE_POINT*)ntfs_attr_readall(ni, + AT_REPARSE_POINT,(ntfschar*)NULL, 0, &attr_size); + if (reparse_attr + && !valid_reparse_data(ni, reparse_attr, attr_size)) { + free(reparse_attr); + reparse_attr = (REPARSE_POINT*)NULL; + errno = EINVAL; + } + } else + errno = EINVAL; + return (reparse_attr); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/runlist.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/runlist.c new file mode 100644 index 0000000..cb01e5a --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/runlist.c @@ -0,0 +1,2191 @@ +/** + * runlist.c - Run list handling code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2002-2005 Anton Altaparmakov + * Copyright (c) 2002-2005 Richard Russon + * Copyright (c) 2002-2008 Szabolcs Szakacsits + * Copyright (c) 2004 Yura Pakhuchiy + * Copyright (c) 2007-2022 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "compat.h" +#include "types.h" +#include "volume.h" +#include "layout.h" +#include "debug.h" +#include "device.h" +#include "logging.h" +#include "misc.h" + +/** + * ntfs_rl_mm - runlist memmove + * @base: + * @dst: + * @src: + * @size: + * + * Description... + * + * Returns: + */ +static void ntfs_rl_mm(runlist_element *base, int dst, int src, int size) +{ + if ((dst != src) && (size > 0)) + memmove(base + dst, base + src, size * sizeof(*base)); +} + +/** + * ntfs_rl_mc - runlist memory copy + * @dstbase: + * @dst: + * @srcbase: + * @src: + * @size: + * + * Description... + * + * Returns: + */ +static void ntfs_rl_mc(runlist_element *dstbase, int dst, + runlist_element *srcbase, int src, int size) +{ + if (size > 0) + memcpy(dstbase + dst, srcbase + src, size * sizeof(*dstbase)); +} + +/** + * ntfs_rl_realloc - Reallocate memory for runlists + * @rl: original runlist + * @old_size: number of runlist elements in the original runlist @rl + * @new_size: number of runlist elements we need space for + * + * As the runlists grow, more memory will be required. To prevent large + * numbers of small reallocations of memory, this function returns a 4kiB block + * of memory. + * + * N.B. If the new allocation doesn't require a different number of 4kiB + * blocks in memory, the function will return the original pointer. + * + * On success, return a pointer to the newly allocated, or recycled, memory. + * On error, return NULL with errno set to the error code. + */ +static runlist_element *ntfs_rl_realloc(runlist_element *rl, int old_size, + int new_size) +{ + old_size = (old_size * sizeof(runlist_element) + 0xfff) & ~0xfff; + new_size = (new_size * sizeof(runlist_element) + 0xfff) & ~0xfff; + if (old_size == new_size) + return rl; + return realloc(rl, new_size); +} + +/* + * Extend a runlist by some entry count + * The runlist may have to be reallocated + * + * Returns the reallocated runlist + * or NULL if reallocation was not possible (with errno set) + * the runlist is left unchanged if the reallocation fails + */ + +runlist_element *ntfs_rl_extend(ntfs_attr *na, runlist_element *rl, + int more_entries) +{ + runlist_element *newrl; + int last; + int irl; + + if (na->rl && rl) { + irl = (int)(rl - na->rl); + last = irl; + while (na->rl[last].length) + last++; + newrl = ntfs_rl_realloc(na->rl,last+1,last+more_entries+1); + if (!newrl) { + errno = ENOMEM; + rl = (runlist_element*)NULL; + } else { + na->rl = newrl; + rl = &newrl[irl]; + } + } else { + ntfs_log_error("Cannot extend unmapped runlist"); + errno = EIO; + rl = (runlist_element*)NULL; + } + return (rl); +} + +/** + * ntfs_rl_are_mergeable - test if two runlists can be joined together + * @dst: original runlist + * @src: new runlist to test for mergeability with @dst + * + * Test if two runlists can be joined together. For this, their VCNs and LCNs + * must be adjacent. + * + * Return: TRUE Success, the runlists can be merged. + * FALSE Failure, the runlists cannot be merged. + */ +static BOOL ntfs_rl_are_mergeable(runlist_element *dst, runlist_element *src) +{ + if (!dst || !src) { + ntfs_log_debug("Eeek. ntfs_rl_are_mergeable() invoked with NULL " + "pointer!\n"); + return FALSE; + } + + /* We can merge unmapped regions even if they are misaligned. */ + if ((dst->lcn == LCN_RL_NOT_MAPPED) && (src->lcn == LCN_RL_NOT_MAPPED)) + return TRUE; + /* If the runs are misaligned, we cannot merge them. */ + if ((dst->vcn + dst->length) != src->vcn) + return FALSE; + /* If both runs are non-sparse and contiguous, we can merge them. */ + if ((dst->lcn >= 0) && (src->lcn >= 0) && + ((dst->lcn + dst->length) == src->lcn)) + return TRUE; + /* If we are merging two holes, we can merge them. */ + if ((dst->lcn == LCN_HOLE) && (src->lcn == LCN_HOLE)) + return TRUE; + /* Cannot merge. */ + return FALSE; +} + +/** + * __ntfs_rl_merge - merge two runlists without testing if they can be merged + * @dst: original, destination runlist + * @src: new runlist to merge with @dst + * + * Merge the two runlists, writing into the destination runlist @dst. The + * caller must make sure the runlists can be merged or this will corrupt the + * destination runlist. + */ +static void __ntfs_rl_merge(runlist_element *dst, runlist_element *src) +{ + dst->length += src->length; +} + +/** + * ntfs_rl_append - append a runlist after a given element + * @dst: original runlist to be worked on + * @dsize: number of elements in @dst (including end marker) + * @src: runlist to be inserted into @dst + * @ssize: number of elements in @src (excluding end marker) + * @loc: append the new runlist @src after this element in @dst + * + * Append the runlist @src after element @loc in @dst. Merge the right end of + * the new runlist, if necessary. Adjust the size of the hole before the + * appended runlist. + * + * On success, return a pointer to the new, combined, runlist. Note, both + * runlists @dst and @src are deallocated before returning so you cannot use + * the pointers for anything any more. (Strictly speaking the returned runlist + * may be the same as @dst but this is irrelevant.) + * + * On error, return NULL, with errno set to the error code. Both runlists are + * left unmodified. + */ +static runlist_element *ntfs_rl_append(runlist_element *dst, int dsize, + runlist_element *src, int ssize, int loc) +{ + BOOL right = FALSE; /* Right end of @src needs merging */ + int marker; /* End of the inserted runs */ + + if (!dst || !src) { + ntfs_log_debug("Eeek. ntfs_rl_append() invoked with NULL " + "pointer!\n"); + errno = EINVAL; + return NULL; + } + + /* First, check if the right hand end needs merging. */ + if ((loc + 1) < dsize) + right = ntfs_rl_are_mergeable(src + ssize - 1, dst + loc + 1); + + /* Space required: @dst size + @src size, less one if we merged. */ + dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - right); + if (!dst) + return NULL; + /* + * We are guaranteed to succeed from here so can start modifying the + * original runlists. + */ + + /* First, merge the right hand end, if necessary. */ + if (right) + __ntfs_rl_merge(src + ssize - 1, dst + loc + 1); + + /* marker - First run after the @src runs that have been inserted */ + marker = loc + ssize + 1; + + /* Move the tail of @dst out of the way, then copy in @src. */ + ntfs_rl_mm(dst, marker, loc + 1 + right, dsize - loc - 1 - right); + ntfs_rl_mc(dst, loc + 1, src, 0, ssize); + + /* Adjust the size of the preceding hole. */ + dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn; + + /* We may have changed the length of the file, so fix the end marker */ + if (dst[marker].lcn == LCN_ENOENT) + dst[marker].vcn = dst[marker-1].vcn + dst[marker-1].length; + + return dst; +} + +/** + * ntfs_rl_insert - insert a runlist into another + * @dst: original runlist to be worked on + * @dsize: number of elements in @dst (including end marker) + * @src: new runlist to be inserted + * @ssize: number of elements in @src (excluding end marker) + * @loc: insert the new runlist @src before this element in @dst + * + * Insert the runlist @src before element @loc in the runlist @dst. Merge the + * left end of the new runlist, if necessary. Adjust the size of the hole + * after the inserted runlist. + * + * On success, return a pointer to the new, combined, runlist. Note, both + * runlists @dst and @src are deallocated before returning so you cannot use + * the pointers for anything any more. (Strictly speaking the returned runlist + * may be the same as @dst but this is irrelevant.) + * + * On error, return NULL, with errno set to the error code. Both runlists are + * left unmodified. + */ +static runlist_element *ntfs_rl_insert(runlist_element *dst, int dsize, + runlist_element *src, int ssize, int loc) +{ + BOOL left = FALSE; /* Left end of @src needs merging */ + BOOL disc = FALSE; /* Discontinuity between @dst and @src */ + int marker; /* End of the inserted runs */ + + if (!dst || !src) { + ntfs_log_debug("Eeek. ntfs_rl_insert() invoked with NULL " + "pointer!\n"); + errno = EINVAL; + return NULL; + } + + /* disc => Discontinuity between the end of @dst and the start of @src. + * This means we might need to insert a "notmapped" run. + */ + if (loc == 0) + disc = (src[0].vcn > 0); + else { + s64 merged_length; + + left = ntfs_rl_are_mergeable(dst + loc - 1, src); + + merged_length = dst[loc - 1].length; + if (left) + merged_length += src->length; + + disc = (src[0].vcn > dst[loc - 1].vcn + merged_length); + } + + /* Space required: @dst size + @src size, less one if we merged, plus + * one if there was a discontinuity. + */ + dst = ntfs_rl_realloc(dst, dsize, dsize + ssize - left + disc); + if (!dst) + return NULL; + /* + * We are guaranteed to succeed from here so can start modifying the + * original runlist. + */ + + if (left) + __ntfs_rl_merge(dst + loc - 1, src); + + /* + * marker - First run after the @src runs that have been inserted + * Nominally: marker = @loc + @ssize (location + number of runs in @src) + * If "left", then the first run in @src has been merged with one in @dst. + * If "disc", then @dst and @src don't meet and we need an extra run to fill the gap. + */ + marker = loc + ssize - left + disc; + + /* Move the tail of @dst out of the way, then copy in @src. */ + ntfs_rl_mm(dst, marker, loc, dsize - loc); + ntfs_rl_mc(dst, loc + disc, src, left, ssize - left); + + /* Adjust the VCN of the first run after the insertion ... */ + dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length; + /* ... and the length. */ + if (dst[marker].lcn == LCN_HOLE || dst[marker].lcn == LCN_RL_NOT_MAPPED) + dst[marker].length = dst[marker + 1].vcn - dst[marker].vcn; + + /* Writing beyond the end of the file and there's a discontinuity. */ + if (disc) { + if (loc > 0) { + dst[loc].vcn = dst[loc - 1].vcn + dst[loc - 1].length; + dst[loc].length = dst[loc + 1].vcn - dst[loc].vcn; + } else { + dst[loc].vcn = 0; + dst[loc].length = dst[loc + 1].vcn; + } + dst[loc].lcn = LCN_RL_NOT_MAPPED; + } + return dst; +} + +/** + * ntfs_rl_replace - overwrite a runlist element with another runlist + * @dst: original runlist to be worked on + * @dsize: number of elements in @dst (including end marker) + * @src: new runlist to be inserted + * @ssize: number of elements in @src (excluding end marker) + * @loc: index in runlist @dst to overwrite with @src + * + * Replace the runlist element @dst at @loc with @src. Merge the left and + * right ends of the inserted runlist, if necessary. + * + * On success, return a pointer to the new, combined, runlist. Note, both + * runlists @dst and @src are deallocated before returning so you cannot use + * the pointers for anything any more. (Strictly speaking the returned runlist + * may be the same as @dst but this is irrelevant.) + * + * On error, return NULL, with errno set to the error code. Both runlists are + * left unmodified. + */ +static runlist_element *ntfs_rl_replace(runlist_element *dst, int dsize, + runlist_element *src, int ssize, + int loc) +{ + signed delta; + BOOL left = FALSE; /* Left end of @src needs merging */ + BOOL right = FALSE; /* Right end of @src needs merging */ + int tail; /* Start of tail of @dst */ + int marker; /* End of the inserted runs */ + + if (!dst || !src) { + ntfs_log_debug("Eeek. ntfs_rl_replace() invoked with NULL " + "pointer!\n"); + errno = EINVAL; + return NULL; + } + + /* First, see if the left and right ends need merging. */ + if ((loc + 1) < dsize) + right = ntfs_rl_are_mergeable(src + ssize - 1, dst + loc + 1); + if (loc > 0) + left = ntfs_rl_are_mergeable(dst + loc - 1, src); + + /* Allocate some space. We'll need less if the left, right, or both + * ends get merged. The -1 accounts for the run being replaced. + */ + delta = ssize - 1 - left - right; + if (delta > 0) { + dst = ntfs_rl_realloc(dst, dsize, dsize + delta); + if (!dst) + return NULL; + } + /* + * We are guaranteed to succeed from here so can start modifying the + * original runlists. + */ + + /* First, merge the left and right ends, if necessary. */ + if (right) + __ntfs_rl_merge(src + ssize - 1, dst + loc + 1); + if (left) + __ntfs_rl_merge(dst + loc - 1, src); + + /* + * tail - Offset of the tail of @dst + * Nominally: @tail = @loc + 1 (location, skipping the replaced run) + * If "right", then one of @dst's runs is already merged into @src. + */ + tail = loc + right + 1; + + /* + * marker - First run after the @src runs that have been inserted + * Nominally: @marker = @loc + @ssize (location + number of runs in @src) + * If "left", then the first run in @src has been merged with one in @dst. + */ + marker = loc + ssize - left; + + /* Move the tail of @dst out of the way, then copy in @src. */ + ntfs_rl_mm(dst, marker, tail, dsize - tail); + ntfs_rl_mc(dst, loc, src, left, ssize - left); + + /* We may have changed the length of the file, so fix the end marker */ + if (((dsize - tail) > 0) && (dst[marker].lcn == LCN_ENOENT)) + dst[marker].vcn = dst[marker - 1].vcn + dst[marker - 1].length; + + return dst; +} + +/** + * ntfs_rl_split - insert a runlist into the centre of a hole + * @dst: original runlist to be worked on + * @dsize: number of elements in @dst (including end marker) + * @src: new runlist to be inserted + * @ssize: number of elements in @src (excluding end marker) + * @loc: index in runlist @dst at which to split and insert @src + * + * Split the runlist @dst at @loc into two and insert @new in between the two + * fragments. No merging of runlists is necessary. Adjust the size of the + * holes either side. + * + * On success, return a pointer to the new, combined, runlist. Note, both + * runlists @dst and @src are deallocated before returning so you cannot use + * the pointers for anything any more. (Strictly speaking the returned runlist + * may be the same as @dst but this is irrelevant.) + * + * On error, return NULL, with errno set to the error code. Both runlists are + * left unmodified. + */ +static runlist_element *ntfs_rl_split(runlist_element *dst, int dsize, + runlist_element *src, int ssize, int loc) +{ + if (!dst || !src) { + ntfs_log_debug("Eeek. ntfs_rl_split() invoked with NULL pointer!\n"); + errno = EINVAL; + return NULL; + } + + /* Space required: @dst size + @src size + one new hole. */ + dst = ntfs_rl_realloc(dst, dsize, dsize + ssize + 1); + if (!dst) + return dst; + /* + * We are guaranteed to succeed from here so can start modifying the + * original runlists. + */ + + /* Move the tail of @dst out of the way, then copy in @src. */ + ntfs_rl_mm(dst, loc + 1 + ssize, loc, dsize - loc); + ntfs_rl_mc(dst, loc + 1, src, 0, ssize); + + /* Adjust the size of the holes either size of @src. */ + dst[loc].length = dst[loc+1].vcn - dst[loc].vcn; + dst[loc+ssize+1].vcn = dst[loc+ssize].vcn + dst[loc+ssize].length; + dst[loc+ssize+1].length = dst[loc+ssize+2].vcn - dst[loc+ssize+1].vcn; + + return dst; +} + + +/** + * ntfs_runlists_merge_i - see ntfs_runlists_merge + */ +static runlist_element *ntfs_runlists_merge_i(runlist_element *drl, + runlist_element *srl) +{ + int di, si; /* Current index into @[ds]rl. */ + int sstart; /* First index with lcn > LCN_RL_NOT_MAPPED. */ + int dins; /* Index into @drl at which to insert @srl. */ + int dend, send; /* Last index into @[ds]rl. */ + int dfinal, sfinal; /* The last index into @[ds]rl with + lcn >= LCN_HOLE. */ + int marker = 0; + VCN marker_vcn = 0; + + ntfs_log_debug("dst:\n"); + ntfs_debug_runlist_dump(drl); + ntfs_log_debug("src:\n"); + ntfs_debug_runlist_dump(srl); + + /* Check for silly calling... */ + if (!srl) + return drl; + + /* Check for the case where the first mapping is being done now. */ + if (!drl) { + drl = srl; + /* Complete the source runlist if necessary. */ + if (drl[0].vcn) { + /* Scan to the end of the source runlist. */ + for (dend = 0; drl[dend].length; dend++) + ; + dend++; + drl = ntfs_rl_realloc(drl, dend, dend + 1); + if (!drl) + return drl; + /* Insert start element at the front of the runlist. */ + ntfs_rl_mm(drl, 1, 0, dend); + drl[0].vcn = 0; + drl[0].lcn = LCN_RL_NOT_MAPPED; + drl[0].length = drl[1].vcn; + } + goto finished; + } + + si = di = 0; + + /* Skip any unmapped start element(s) in the source runlist. */ + while (srl[si].length && srl[si].lcn < (LCN)LCN_HOLE) + si++; + + /* Can't have an entirely unmapped source runlist. */ + if (!srl[si].length) { + errno = EINVAL; + ntfs_log_perror("%s: unmapped source runlist", __FUNCTION__); + return NULL; + } + + /* Record the starting points. */ + sstart = si; + + /* + * Skip forward in @drl until we reach the position where @srl needs to + * be inserted. If we reach the end of @drl, @srl just needs to be + * appended to @drl. + */ + for (; drl[di].length; di++) { + if (drl[di].vcn + drl[di].length > srl[sstart].vcn) + break; + } + dins = di; + + /* Sanity check for illegal overlaps. */ + if ((drl[di].vcn == srl[si].vcn) && (drl[di].lcn >= 0) && + (srl[si].lcn >= 0)) { + errno = ERANGE; + ntfs_log_perror("Run lists overlap. Cannot merge"); + return NULL; + } + + /* Scan to the end of both runlists in order to know their sizes. */ + for (send = si; srl[send].length; send++) + ; + for (dend = di; drl[dend].length; dend++) + ; + + if (srl[send].lcn == (LCN)LCN_ENOENT) + marker_vcn = srl[marker = send].vcn; + + /* Scan to the last element with lcn >= LCN_HOLE. */ + for (sfinal = send; sfinal >= 0 && srl[sfinal].lcn < LCN_HOLE; sfinal--) + ; + for (dfinal = dend; dfinal >= 0 && drl[dfinal].lcn < LCN_HOLE; dfinal--) + ; + + { + BOOL start; + BOOL finish; + int ds = dend + 1; /* Number of elements in drl & srl */ + int ss = sfinal - sstart + 1; + + start = ((drl[dins].lcn < LCN_RL_NOT_MAPPED) || /* End of file */ + (drl[dins].vcn == srl[sstart].vcn)); /* Start of hole */ + finish = ((drl[dins].lcn >= LCN_RL_NOT_MAPPED) && /* End of file */ + ((drl[dins].vcn + drl[dins].length) <= /* End of hole */ + (srl[send - 1].vcn + srl[send - 1].length))); + + /* Or we'll lose an end marker */ + if (finish && !drl[dins].length) + ss++; + if (marker && (drl[dins].vcn + drl[dins].length > srl[send - 1].vcn)) + finish = FALSE; + + ntfs_log_debug("dfinal = %i, dend = %i\n", dfinal, dend); + ntfs_log_debug("sstart = %i, sfinal = %i, send = %i\n", sstart, sfinal, send); + ntfs_log_debug("start = %i, finish = %i\n", start, finish); + ntfs_log_debug("ds = %i, ss = %i, dins = %i\n", ds, ss, dins); + + if (start) { + if (finish) + drl = ntfs_rl_replace(drl, ds, srl + sstart, ss, dins); + else + drl = ntfs_rl_insert(drl, ds, srl + sstart, ss, dins); + } else { + if (finish) + drl = ntfs_rl_append(drl, ds, srl + sstart, ss, dins); + else + drl = ntfs_rl_split(drl, ds, srl + sstart, ss, dins); + } + if (!drl) { + ntfs_log_perror("Merge failed"); + return drl; + } + free(srl); + if (marker) { + ntfs_log_debug("Triggering marker code.\n"); + for (ds = dend; drl[ds].length; ds++) + ; + /* We only need to care if @srl ended after @drl. */ + if (drl[ds].vcn <= marker_vcn) { + int slots = 0; + + if (drl[ds].vcn == marker_vcn) { + ntfs_log_debug("Old marker = %lli, replacing with " + "LCN_ENOENT.\n", + (long long)drl[ds].lcn); + drl[ds].lcn = (LCN)LCN_ENOENT; + goto finished; + } + /* + * We need to create an unmapped runlist element in + * @drl or extend an existing one before adding the + * ENOENT terminator. + */ + if (drl[ds].lcn == (LCN)LCN_ENOENT) { + ds--; + slots = 1; + } + if (drl[ds].lcn != (LCN)LCN_RL_NOT_MAPPED) { + /* Add an unmapped runlist element. */ + if (!slots) { + /* FIXME/TODO: We need to have the + * extra memory already! (AIA) + */ + drl = ntfs_rl_realloc(drl, ds, ds + 2); + if (!drl) + goto critical_error; + slots = 2; + } + ds++; + /* Need to set vcn if it isn't set already. */ + if (slots != 1) + drl[ds].vcn = drl[ds - 1].vcn + + drl[ds - 1].length; + drl[ds].lcn = (LCN)LCN_RL_NOT_MAPPED; + /* We now used up a slot. */ + slots--; + } + drl[ds].length = marker_vcn - drl[ds].vcn; + /* Finally add the ENOENT terminator. */ + ds++; + if (!slots) { + /* FIXME/TODO: We need to have the extra + * memory already! (AIA) + */ + drl = ntfs_rl_realloc(drl, ds, ds + 1); + if (!drl) + goto critical_error; + } + drl[ds].vcn = marker_vcn; + drl[ds].lcn = (LCN)LCN_ENOENT; + drl[ds].length = (s64)0; + } + } + } + +finished: + /* The merge was completed successfully. */ + ntfs_log_debug("Merged runlist:\n"); + ntfs_debug_runlist_dump(drl); + return drl; + +critical_error: + /* Critical error! We cannot afford to fail here. */ + ntfs_log_perror("libntfs: Critical error"); + ntfs_log_debug("Forcing segmentation fault!\n"); + marker_vcn = ((runlist*)NULL)->lcn; + return drl; +} + +/** + * ntfs_runlists_merge - merge two runlists into one + * @drl: original runlist to be worked on + * @srl: new runlist to be merged into @drl + * + * First we sanity check the two runlists @srl and @drl to make sure that they + * are sensible and can be merged. The runlist @srl must be either after the + * runlist @drl or completely within a hole (or unmapped region) in @drl. + * + * Merging of runlists is necessary in two cases: + * 1. When attribute lists are used and a further extent is being mapped. + * 2. When new clusters are allocated to fill a hole or extend a file. + * + * There are four possible ways @srl can be merged. It can: + * - be inserted at the beginning of a hole, + * - split the hole in two and be inserted between the two fragments, + * - be appended at the end of a hole, or it can + * - replace the whole hole. + * It can also be appended to the end of the runlist, which is just a variant + * of the insert case. + * + * On success, return a pointer to the new, combined, runlist. Note, both + * runlists @drl and @srl are deallocated before returning so you cannot use + * the pointers for anything any more. (Strictly speaking the returned runlist + * may be the same as @dst but this is irrelevant.) + * + * On error, return NULL, with errno set to the error code. Both runlists are + * left unmodified. The following error codes are defined: + * ENOMEM Not enough memory to allocate runlist array. + * EINVAL Invalid parameters were passed in. + * ERANGE The runlists overlap and cannot be merged. + */ +runlist_element *ntfs_runlists_merge(runlist_element *drl, + runlist_element *srl) +{ + runlist_element *rl; + + ntfs_log_enter("Entering\n"); + rl = ntfs_runlists_merge_i(drl, srl); + ntfs_log_leave("\n"); + return rl; +} + +/** + * ntfs_mapping_pairs_decompress - convert mapping pairs array to runlist + * @vol: ntfs volume on which the attribute resides + * @attr: attribute record whose mapping pairs array to decompress + * @old_rl: optional runlist in which to insert @attr's runlist + * + * Decompress the attribute @attr's mapping pairs array into a runlist. On + * success, return the decompressed runlist. + * + * If @old_rl is not NULL, decompressed runlist is inserted into the + * appropriate place in @old_rl and the resultant, combined runlist is + * returned. The original @old_rl is deallocated. + * + * On error, return NULL with errno set to the error code. @old_rl is left + * unmodified in that case. + * + * The following error codes are defined: + * ENOMEM Not enough memory to allocate runlist array. + * EIO Corrupt runlist. + * EINVAL Invalid parameters were passed in. + * ERANGE The two runlists overlap. + * + * FIXME: For now we take the conceptionally simplest approach of creating the + * new runlist disregarding the already existing one and then splicing the + * two into one, if that is possible (we check for overlap and discard the new + * runlist if overlap present before returning NULL, with errno = ERANGE). + */ +static runlist_element *ntfs_mapping_pairs_decompress_i(const ntfs_volume *vol, + const ATTR_RECORD *attr, runlist_element *old_rl) +{ + VCN vcn; /* Current vcn. */ + LCN lcn; /* Current lcn. */ + s64 deltaxcn; /* Change in [vl]cn. */ + runlist_element *rl; /* The output runlist. */ + const u8 *buf; /* Current position in mapping pairs array. */ + const u8 *attr_end; /* End of attribute. */ + int err, rlsize; /* Size of runlist buffer. */ + u16 rlpos; /* Current runlist position in units of + runlist_elements. */ + u8 b; /* Current byte offset in buf. */ + + ntfs_log_trace("Entering for attr 0x%x.\n", + (unsigned)le32_to_cpu(attr->type)); + /* Make sure attr exists and is non-resident. */ + if (!attr || !attr->non_resident || + sle64_to_cpu(attr->lowest_vcn) < (VCN)0) { + errno = EINVAL; + return NULL; + } + /* Start at vcn = lowest_vcn and lcn 0. */ + vcn = sle64_to_cpu(attr->lowest_vcn); + lcn = 0; + /* Get start of the mapping pairs array. */ + buf = (const u8*)attr + le16_to_cpu(attr->mapping_pairs_offset); + attr_end = (const u8*)attr + le32_to_cpu(attr->length); + if (buf < (const u8*)attr || buf > attr_end) { + ntfs_log_debug("Corrupt attribute.\n"); + errno = EIO; + return NULL; + } + /* Current position in runlist array. */ + rlpos = 0; + /* Allocate first 4kiB block and set current runlist size to 4kiB. */ + rlsize = 0x1000; + rl = ntfs_malloc(rlsize); + if (!rl) + return NULL; + /* Insert unmapped starting element if necessary. */ + if (vcn) { + rl->vcn = (VCN)0; + rl->lcn = (LCN)LCN_RL_NOT_MAPPED; + rl->length = vcn; + rlpos++; + } + while (buf < attr_end && *buf) { + /* + * Allocate more memory if needed, including space for the + * not-mapped and terminator elements. + */ + if ((int)((rlpos + 3) * sizeof(*old_rl)) > rlsize) { + runlist_element *rl2; + + rlsize += 0x1000; + rl2 = realloc(rl, rlsize); + if (!rl2) { + int eo = errno; + free(rl); + errno = eo; + return NULL; + } + rl = rl2; + } + /* Enter the current vcn into the current runlist element. */ + rl[rlpos].vcn = vcn; + /* + * Get the change in vcn, i.e. the run length in clusters. + * Doing it this way ensures that we signextend negative values. + * A negative run length doesn't make any sense, but hey, I + * didn't make up the NTFS specs and Windows NT4 treats the run + * length as a signed value so that's how it is... + */ + b = *buf & 0xf; + if (b) { + if (buf + b > attr_end) + goto io_error; + for (deltaxcn = (s8)buf[b--]; b; b--) + deltaxcn = (deltaxcn << 8) + buf[b]; + } else { /* The length entry is compulsory. */ + ntfs_log_debug("Missing length entry in mapping pairs " + "array.\n"); + deltaxcn = (s64)-1; + } + /* + * Assume a negative length to indicate data corruption and + * hence clean-up and return NULL. + */ + if (deltaxcn < 0) { + ntfs_log_debug("Invalid length in mapping pairs array.\n"); + goto err_out; + } + /* + * Enter the current run length into the current runlist + * element. + */ + rl[rlpos].length = deltaxcn; + /* Increment the current vcn by the current run length. */ + vcn += deltaxcn; + /* + * There might be no lcn change at all, as is the case for + * sparse clusters on NTFS 3.0+, in which case we set the lcn + * to LCN_HOLE. + */ + if (!(*buf & 0xf0)) + rl[rlpos].lcn = (LCN)LCN_HOLE; + else { + /* Get the lcn change which really can be negative. */ + u8 b2 = *buf & 0xf; + b = b2 + ((*buf >> 4) & 0xf); + if (buf + b > attr_end) + goto io_error; + for (deltaxcn = (s8)buf[b--]; b > b2; b--) + deltaxcn = (deltaxcn << 8) + buf[b]; + /* Change the current lcn to it's new value. */ + lcn += deltaxcn; +#ifdef DEBUG + /* + * On NTFS 1.2-, apparently can have lcn == -1 to + * indicate a hole. But we haven't verified ourselves + * whether it is really the lcn or the deltaxcn that is + * -1. So if either is found give us a message so we + * can investigate it further! + */ + if (vol->major_ver < 3) { + if (deltaxcn == (LCN)-1) + ntfs_log_debug("lcn delta == -1\n"); + if (lcn == (LCN)-1) + ntfs_log_debug("lcn == -1\n"); + } +#endif + /* Check lcn is not below -1. */ + if (lcn < (LCN)-1) { + ntfs_log_debug("Invalid LCN < -1 in mapping pairs " + "array.\n"); + goto err_out; + } + /* chkdsk accepts zero-sized runs only for holes */ + if ((lcn != (LCN)-1) && !rl[rlpos].length) { + ntfs_log_debug( + "Invalid zero-sized data run.\n"); + goto err_out; + } + /* Enter the current lcn into the runlist element. */ + rl[rlpos].lcn = lcn; + } + /* Get to the next runlist element, skipping zero-sized holes */ + if (rl[rlpos].length) + rlpos++; + /* Increment the buffer position to the next mapping pair. */ + buf += (*buf & 0xf) + ((*buf >> 4) & 0xf) + 1; + } + if (buf >= attr_end) + goto io_error; + /* + * If there is a highest_vcn specified, it must be equal to the final + * vcn in the runlist - 1, or something has gone badly wrong. + */ + deltaxcn = sle64_to_cpu(attr->highest_vcn); + if (deltaxcn && vcn - 1 != deltaxcn) { +mpa_err: + ntfs_log_debug("Corrupt mapping pairs array in non-resident " + "attribute.\n"); + goto err_out; + } + + /* + * If this is the base of runlist (if 'lowest_vcn' is 0), then + * 'allocated_size' is valid, and we can use it to compute the total + * number of clusters across all extents. If the runlist covers all + * clusters, then it fits into a single extent and we can terminate + * the runlist with LCN_NOENT. Otherwise, we must terminate the runlist + * with LCN_RL_NOT_MAPPED and let the caller look for more extents. + */ + if (!attr->lowest_vcn) { + VCN num_clusters; + + num_clusters = ((sle64_to_cpu(attr->allocated_size) + + vol->cluster_size - 1) >> + vol->cluster_size_bits); + + if (num_clusters > vcn) { + /* + * The runlist doesn't cover all the clusters, so there + * must be more extents. + */ + ntfs_log_debug("More extents to follow; vcn = 0x%llx, " + "num_clusters = 0x%llx\n", + (long long)vcn, + (long long)num_clusters); + rl[rlpos].vcn = vcn; + vcn += rl[rlpos].length = num_clusters - vcn; + rl[rlpos].lcn = (LCN)LCN_RL_NOT_MAPPED; + rlpos++; + } else if (vcn > num_clusters) { + /* + * There are more VCNs in the runlist than expected, so + * the runlist is corrupt. + */ + ntfs_log_error("Corrupt attribute. vcn = 0x%llx, " + "num_clusters = 0x%llx\n", + (long long)vcn, + (long long)num_clusters); + goto mpa_err; + } + rl[rlpos].lcn = (LCN)LCN_ENOENT; + } else /* Not the base extent. There may be more extents to follow. */ + rl[rlpos].lcn = (LCN)LCN_RL_NOT_MAPPED; + + /* Setup terminating runlist element. */ + rl[rlpos].vcn = vcn; + rl[rlpos].length = (s64)0; + /* If no existing runlist was specified, we are done. */ + if (!old_rl || !old_rl[0].length) { + ntfs_log_debug("Mapping pairs array successfully decompressed:\n"); + ntfs_debug_runlist_dump(rl); + if (old_rl) + free(old_rl); + return rl; + } + /* Now combine the new and old runlists checking for overlaps. */ + if (rl[0].length) + old_rl = ntfs_runlists_merge(old_rl, rl); + else + free(rl); + if (old_rl) + return old_rl; + err = errno; + free(rl); + ntfs_log_debug("Failed to merge runlists.\n"); + errno = err; + return NULL; +io_error: + ntfs_log_debug("Corrupt attribute.\n"); +err_out: + free(rl); + errno = EIO; + return NULL; +} + +runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol, + const ATTR_RECORD *attr, runlist_element *old_rl) +{ + runlist_element *rle; + + ntfs_log_enter("Entering\n"); + rle = ntfs_mapping_pairs_decompress_i(vol, attr, old_rl); + ntfs_log_leave("\n"); + return rle; +} + +/** + * ntfs_rl_vcn_to_lcn - convert a vcn into a lcn given a runlist + * @rl: runlist to use for conversion + * @vcn: vcn to convert + * + * Convert the virtual cluster number @vcn of an attribute into a logical + * cluster number (lcn) of a device using the runlist @rl to map vcns to their + * corresponding lcns. + * + * Since lcns must be >= 0, we use negative return values with special meaning: + * + * Return value Meaning / Description + * ================================================== + * -1 = LCN_HOLE Hole / not allocated on disk. + * -2 = LCN_RL_NOT_MAPPED This is part of the runlist which has not been + * inserted into the runlist yet. + * -3 = LCN_ENOENT There is no such vcn in the attribute. + * -4 = LCN_EINVAL Input parameter error. + */ +LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn) +{ + int i; + + if (vcn < (VCN)0) + return (LCN)LCN_EINVAL; + /* + * If rl is NULL, assume that we have found an unmapped runlist. The + * caller can then attempt to map it and fail appropriately if + * necessary. + */ + if (!rl) + return (LCN)LCN_RL_NOT_MAPPED; + + /* Catch out of lower bounds vcn. */ + if (vcn < rl[0].vcn) + return (LCN)LCN_ENOENT; + + for (i = 0; rl[i].length; i++) { + if (vcn < rl[i+1].vcn) { + if (rl[i].lcn >= (LCN)0) + return rl[i].lcn + (vcn - rl[i].vcn); + return rl[i].lcn; + } + } + /* + * The terminator element is setup to the correct value, i.e. one of + * LCN_HOLE, LCN_RL_NOT_MAPPED, or LCN_ENOENT. + */ + if (rl[i].lcn < (LCN)0) + return rl[i].lcn; + /* Just in case... We could replace this with BUG() some day. */ + return (LCN)LCN_ENOENT; +} + +/** + * ntfs_rl_pread - gather read from disk + * @vol: ntfs volume to read from + * @rl: runlist specifying where to read the data from + * @pos: byte position within runlist @rl at which to begin the read + * @count: number of bytes to read + * @b: data buffer into which to read from disk + * + * This function will read @count bytes from the volume @vol to the data buffer + * @b gathering the data as specified by the runlist @rl. The read begins at + * offset @pos into the runlist @rl. + * + * On success, return the number of successfully read bytes. If this number is + * lower than @count this means that the read reached end of file or that an + * error was encountered during the read so that the read is partial. 0 means + * nothing was read (also return 0 when @count is 0). + * + * On error and nothing has been read, return -1 with errno set appropriately + * to the return code of ntfs_pread(), or to EINVAL in case of invalid + * arguments. + * + * NOTE: If we encounter EOF while reading we return EIO because we assume that + * the run list must point to valid locations within the ntfs volume. + */ +s64 ntfs_rl_pread(const ntfs_volume *vol, const runlist_element *rl, + const s64 pos, s64 count, void *b) +{ + s64 bytes_read, to_read, ofs, total; + int err = EIO; + + if (!vol || !rl || pos < 0 || count < 0) { + errno = EINVAL; + ntfs_log_perror("Failed to read runlist [vol: %p rl: %p " + "pos: %lld count: %lld]", vol, rl, + (long long)pos, (long long)count); + return -1; + } + if (!count) + return count; + /* Seek in @rl to the run containing @pos. */ + for (ofs = 0; rl->length && (ofs + (rl->length << + vol->cluster_size_bits) <= pos); rl++) + ofs += (rl->length << vol->cluster_size_bits); + /* Offset in the run at which to begin reading. */ + ofs = pos - ofs; + for (total = 0LL; count; rl++, ofs = 0) { + if (!rl->length) + goto rl_err_out; + if (rl->lcn < (LCN)0) { + if (rl->lcn != (LCN)LCN_HOLE) + goto rl_err_out; + /* It is a hole. Just fill buffer @b with zeroes. */ + to_read = min(count, (rl->length << + vol->cluster_size_bits) - ofs); + memset(b, 0, to_read); + /* Update counters and proceed with next run. */ + total += to_read; + count -= to_read; + b = (u8*)b + to_read; + continue; + } + /* It is a real lcn, read it from the volume. */ + to_read = min(count, (rl->length << vol->cluster_size_bits) - + ofs); +retry: + bytes_read = ntfs_pread(vol->dev, (rl->lcn << + vol->cluster_size_bits) + ofs, to_read, b); + /* If everything ok, update progress counters and continue. */ + if (bytes_read > 0) { + total += bytes_read; + count -= bytes_read; + b = (u8*)b + bytes_read; + continue; + } + /* If the syscall was interrupted, try again. */ + if (bytes_read == (s64)-1 && errno == EINTR) + goto retry; + if (bytes_read == (s64)-1) + err = errno; + goto rl_err_out; + } + /* Finally, return the number of bytes read. */ + return total; +rl_err_out: + if (total) + return total; + errno = err; + return -1; +} + +/** + * ntfs_rl_pwrite - scatter write to disk + * @vol: ntfs volume to write to + * @rl: runlist entry specifying where to write the data to + * @ofs: offset in file for runlist element indicated in @rl + * @pos: byte position from runlist beginning at which to begin the write + * @count: number of bytes to write + * @b: data buffer to write to disk + * + * This function will write @count bytes from data buffer @b to the volume @vol + * scattering the data as specified by the runlist @rl. The write begins at + * offset @pos into the runlist @rl. If a run is sparse then the related buffer + * data is ignored which means that the caller must ensure they are consistent. + * + * On success, return the number of successfully written bytes. If this number + * is lower than @count this means that the write has been interrupted in + * flight or that an error was encountered during the write so that the write + * is partial. 0 means nothing was written (also return 0 when @count is 0). + * + * On error and nothing has been written, return -1 with errno set + * appropriately to the return code of ntfs_pwrite(), or to to EINVAL in case + * of invalid arguments. + */ +s64 ntfs_rl_pwrite(const ntfs_volume *vol, const runlist_element *rl, + s64 ofs, const s64 pos, s64 count, void *b) +{ + s64 written, to_write, total = 0; + int err = EIO; + + if (!vol || !rl || pos < 0 || count < 0) { + errno = EINVAL; + ntfs_log_perror("Failed to write runlist [vol: %p rl: %p " + "pos: %lld count: %lld]", vol, rl, + (long long)pos, (long long)count); + goto errno_set; + } + if (!count) + goto out; + /* Seek in @rl to the run containing @pos. */ + while (rl->length && (ofs + (rl->length << + vol->cluster_size_bits) <= pos)) { + ofs += (rl->length << vol->cluster_size_bits); + rl++; + } + /* Offset in the run at which to begin writing. */ + ofs = pos - ofs; + for (total = 0LL; count; rl++, ofs = 0) { + if (!rl->length) + goto rl_err_out; + if (rl->lcn < (LCN)0) { + + if (rl->lcn != (LCN)LCN_HOLE) + goto rl_err_out; + + to_write = min(count, (rl->length << + vol->cluster_size_bits) - ofs); + + total += to_write; + count -= to_write; + b = (u8*)b + to_write; + continue; + } + /* It is a real lcn, write it to the volume. */ + to_write = min(count, (rl->length << vol->cluster_size_bits) - + ofs); +retry: + if (!NVolReadOnly(vol)) + written = ntfs_pwrite(vol->dev, (rl->lcn << + vol->cluster_size_bits) + ofs, + to_write, b); + else + written = to_write; + /* If everything ok, update progress counters and continue. */ + if (written > 0) { + total += written; + count -= written; + b = (u8*)b + written; + continue; + } + /* If the syscall was interrupted, try again. */ + if (written == (s64)-1 && errno == EINTR) + goto retry; + if (written == (s64)-1) + err = errno; + goto rl_err_out; + } +out: + return total; +rl_err_out: + if (total) + goto out; + errno = err; +errno_set: + total = -1; + goto out; +} + +/** + * ntfs_get_nr_significant_bytes - get number of bytes needed to store a number + * @n: number for which to get the number of bytes for + * + * Return the number of bytes required to store @n unambiguously as + * a signed number. + * + * This is used in the context of the mapping pairs array to determine how + * many bytes will be needed in the array to store a given logical cluster + * number (lcn) or a specific run length. + * + * Return the number of bytes written. This function cannot fail. + */ +int ntfs_get_nr_significant_bytes(const s64 n) +{ + u64 l; + int i; + + l = (n < 0 ? ~n : n); + i = 1; + if (l >= 128) { + l >>= 7; + do { + i++; + l >>= 8; + } while (l); + } + return i; +} + +/** + * ntfs_get_size_for_mapping_pairs - get bytes needed for mapping pairs array + * @vol: ntfs volume (needed for the ntfs version) + * @rl: runlist for which to determine the size of the mapping pairs + * @start_vcn: vcn at which to start the mapping pairs array + * + * Walk the runlist @rl and calculate the size in bytes of the mapping pairs + * array corresponding to the runlist @rl, starting at vcn @start_vcn. This + * for example allows us to allocate a buffer of the right size when building + * the mapping pairs array. + * + * If @rl is NULL, just return 1 (for the single terminator byte). + * + * Return the calculated size in bytes on success. On error, return -1 with + * errno set to the error code. The following error codes are defined: + * EINVAL - Run list contains unmapped elements. Make sure to only pass + * fully mapped runlists to this function. + * - @start_vcn is invalid. + * EIO - The runlist is corrupt. + */ +int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol, + const runlist_element *rl, const VCN start_vcn, int max_size) +{ + LCN prev_lcn; + int rls; + + if (start_vcn < 0) { + ntfs_log_trace("start_vcn %lld (should be >= 0)\n", + (long long) start_vcn); + errno = EINVAL; + goto errno_set; + } + if (!rl) { + if (start_vcn) { + ntfs_log_trace("rl NULL, start_vcn %lld (should be > 0)\n", + (long long) start_vcn); + errno = EINVAL; + goto errno_set; + } + rls = 1; + goto out; + } + /* Skip to runlist element containing @start_vcn. */ + while (rl->length && start_vcn >= rl[1].vcn) + rl++; + if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn) { + errno = EINVAL; + goto errno_set; + } + prev_lcn = 0; + /* Always need the terminating zero byte. */ + rls = 1; + /* Do the first partial run if present. */ + if (start_vcn > rl->vcn) { + s64 delta; + + /* We know rl->length != 0 already. */ + if (rl->length < 0 || rl->lcn < LCN_HOLE) + goto err_out; + delta = start_vcn - rl->vcn; + /* Header byte + length. */ + rls += 1 + ntfs_get_nr_significant_bytes(rl->length - delta); + /* + * If the logical cluster number (lcn) denotes a hole and we + * are on NTFS 3.0+, we don't store it at all, i.e. we need + * zero space. On earlier NTFS versions we just store the lcn. + * Note: this assumes that on NTFS 1.2-, holes are stored with + * an lcn of -1 and not a delta_lcn of -1 (unless both are -1). + */ + if (rl->lcn >= 0 || vol->major_ver < 3) { + prev_lcn = rl->lcn; + if (rl->lcn >= 0) + prev_lcn += delta; + /* Change in lcn. */ + rls += ntfs_get_nr_significant_bytes(prev_lcn); + } + /* Go to next runlist element. */ + rl++; + } + /* Do the full runs. */ + for (; rl->length && (rls <= max_size); rl++) { + if (rl->length < 0 || rl->lcn < LCN_HOLE) + goto err_out; + /* Header byte + length. */ + rls += 1 + ntfs_get_nr_significant_bytes(rl->length); + /* + * If the logical cluster number (lcn) denotes a hole and we + * are on NTFS 3.0+, we don't store it at all, i.e. we need + * zero space. On earlier NTFS versions we just store the lcn. + * Note: this assumes that on NTFS 1.2-, holes are stored with + * an lcn of -1 and not a delta_lcn of -1 (unless both are -1). + */ + if (rl->lcn >= 0 || vol->major_ver < 3) { + /* Change in lcn. */ + rls += ntfs_get_nr_significant_bytes(rl->lcn - + prev_lcn); + prev_lcn = rl->lcn; + } + } +out: + return rls; +err_out: + if (rl->lcn == LCN_RL_NOT_MAPPED) + errno = EINVAL; + else + errno = EIO; +errno_set: + rls = -1; + goto out; +} + +/** + * ntfs_write_significant_bytes - write the significant bytes of a number + * @dst: destination buffer to write to + * @dst_max: pointer to last byte of destination buffer for bounds checking + * @n: number whose significant bytes to write + * + * Store in @dst, the minimum bytes of the number @n which are required to + * identify @n unambiguously as a signed number, taking care not to exceed + * @dest_max, the maximum position within @dst to which we are allowed to + * write. + * + * This is used when building the mapping pairs array of a runlist to compress + * a given logical cluster number (lcn) or a specific run length to the minimum + * size possible. + * + * Return the number of bytes written on success. On error, i.e. the + * destination buffer @dst is too small, return -1 with errno set ENOSPC. + */ +int ntfs_write_significant_bytes(u8 *dst, const u8 *dst_max, const s64 n) +{ + s64 l = n; + int i; + + i = 0; + if (dst > dst_max) + goto err_out; + *dst++ = l; + i++; + while ((l > 0x7f) || (l < -0x80)) { + if (dst > dst_max) + goto err_out; + l >>= 8; + *dst++ = l; + i++; + } + return i; +err_out: + errno = ENOSPC; + return -1; +} + +/** + * ntfs_mapping_pairs_build - build the mapping pairs array from a runlist + * @vol: ntfs volume (needed for the ntfs version) + * @dst: destination buffer to which to write the mapping pairs array + * @dst_len: size of destination buffer @dst in bytes + * @rl: runlist for which to build the mapping pairs array + * @start_vcn: vcn at which to start the mapping pairs array + * @stop_vcn: first vcn outside destination buffer on success or ENOSPC error + * + * Create the mapping pairs array from the runlist @rl, starting at vcn + * @start_vcn and save the array in @dst. @dst_len is the size of @dst in + * bytes and it should be at least equal to the value obtained by calling + * ntfs_get_size_for_mapping_pairs(). + * + * If @rl is NULL, just write a single terminator byte to @dst. + * + * On success or ENOSPC error, if @stop_vcn is not NULL, *@stop_vcn is set to + * the first vcn outside the destination buffer. Note that on error @dst has + * been filled with all the mapping pairs that will fit, thus it can be treated + * as partial success, in that a new attribute extent needs to be created or the + * next extent has to be used and the mapping pairs build has to be continued + * with @start_vcn set to *@stop_vcn. + * + * Return 0 on success. On error, return -1 with errno set to the error code. + * The following error codes are defined: + * EINVAL - Run list contains unmapped elements. Make sure to only pass + * fully mapped runlists to this function. + * - @start_vcn is invalid. + * EIO - The runlist is corrupt. + * ENOSPC - The destination buffer is too small. + */ +int ntfs_mapping_pairs_build(const ntfs_volume *vol, u8 *dst, + const int dst_len, const runlist_element *rl, + const VCN start_vcn, runlist_element const **stop_rl) +{ + LCN prev_lcn; + u8 *dst_max, *dst_next; + s8 len_len, lcn_len; + int ret = 0; + + if (start_vcn < 0) + goto val_err; + if (!rl) { + if (start_vcn) + goto val_err; + if (stop_rl) + *stop_rl = rl; + if (dst_len < 1) + goto nospc_err; + goto ok; + } + /* Skip to runlist element containing @start_vcn. */ + while (rl->length && start_vcn >= rl[1].vcn) + rl++; + if ((!rl->length && start_vcn > rl->vcn) || start_vcn < rl->vcn) + goto val_err; + /* + * @dst_max is used for bounds checking in + * ntfs_write_significant_bytes(). + */ + dst_max = dst + dst_len - 1; + prev_lcn = 0; + /* Do the first partial run if present. */ + if (start_vcn > rl->vcn) { + s64 delta; + + /* We know rl->length != 0 already. */ + if (rl->length < 0 || rl->lcn < LCN_HOLE) + goto err_out; + delta = start_vcn - rl->vcn; + /* Write length. */ + len_len = ntfs_write_significant_bytes(dst + 1, dst_max, + rl->length - delta); + if (len_len < 0) + goto size_err; + /* + * If the logical cluster number (lcn) denotes a hole and we + * are on NTFS 3.0+, we don't store it at all, i.e. we need + * zero space. On earlier NTFS versions we just write the lcn + * change. FIXME: Do we need to write the lcn change or just + * the lcn in that case? Not sure as I have never seen this + * case on NT4. - We assume that we just need to write the lcn + * change until someone tells us otherwise... (AIA) + */ + if (rl->lcn >= 0 || vol->major_ver < 3) { + prev_lcn = rl->lcn; + if (rl->lcn >= 0) + prev_lcn += delta; + /* Write change in lcn. */ + lcn_len = ntfs_write_significant_bytes(dst + 1 + + len_len, dst_max, prev_lcn); + if (lcn_len < 0) + goto size_err; + } else + lcn_len = 0; + dst_next = dst + len_len + lcn_len + 1; + if (dst_next > dst_max) + goto size_err; + /* Update header byte. */ + *dst = lcn_len << 4 | len_len; + /* Position at next mapping pairs array element. */ + dst = dst_next; + /* Go to next runlist element. */ + rl++; + } + /* Do the full runs. */ + for (; rl->length; rl++) { + if (rl->length < 0 || rl->lcn < LCN_HOLE) + goto err_out; + /* Write length. */ + len_len = ntfs_write_significant_bytes(dst + 1, dst_max, + rl->length); + if (len_len < 0) + goto size_err; + /* + * If the logical cluster number (lcn) denotes a hole and we + * are on NTFS 3.0+, we don't store it at all, i.e. we need + * zero space. On earlier NTFS versions we just write the lcn + * change. FIXME: Do we need to write the lcn change or just + * the lcn in that case? Not sure as I have never seen this + * case on NT4. - We assume that we just need to write the lcn + * change until someone tells us otherwise... (AIA) + */ + if (rl->lcn >= 0 || vol->major_ver < 3) { + /* Write change in lcn. */ + lcn_len = ntfs_write_significant_bytes(dst + 1 + + len_len, dst_max, rl->lcn - prev_lcn); + if (lcn_len < 0) + goto size_err; + prev_lcn = rl->lcn; + } else + lcn_len = 0; + dst_next = dst + len_len + lcn_len + 1; + if (dst_next > dst_max) + goto size_err; + /* Update header byte. */ + *dst = lcn_len << 4 | len_len; + /* Position at next mapping pairs array element. */ + dst += 1 + len_len + lcn_len; + } + /* Set stop vcn. */ + if (stop_rl) + *stop_rl = rl; +ok: + /* Add terminator byte. */ + *dst = 0; +out: + return ret; +size_err: + /* Set stop vcn. */ + if (stop_rl) + *stop_rl = rl; + /* Add terminator byte. */ + *dst = 0; +nospc_err: + errno = ENOSPC; + goto errno_set; +val_err: + errno = EINVAL; + goto errno_set; +err_out: + if (rl->lcn == LCN_RL_NOT_MAPPED) + errno = EINVAL; + else + errno = EIO; +errno_set: + ret = -1; + goto out; +} + +/** + * ntfs_rl_truncate - truncate a runlist starting at a specified vcn + * @arl: address of runlist to truncate + * @start_vcn: first vcn which should be cut off + * + * Truncate the runlist *@arl starting at vcn @start_vcn as well as the memory + * buffer holding the runlist. + * + * Return 0 on success and -1 on error with errno set to the error code. + * + * NOTE: @arl is the address of the runlist. We need the address so we can + * modify the pointer to the runlist with the new, reallocated memory buffer. + */ +int ntfs_rl_truncate(runlist **arl, const VCN start_vcn) +{ + runlist *rl; + /* BOOL is_end = FALSE; */ + + if (!arl || !*arl) { + errno = EINVAL; + if (!arl) + ntfs_log_perror("rl_truncate error: arl: %p", arl); + else + ntfs_log_perror("rl_truncate error:" + " arl: %p *arl: %p", arl, *arl); + return -1; + } + + rl = *arl; + + if (start_vcn < rl->vcn) { + errno = EINVAL; + ntfs_log_perror("Start_vcn lies outside front of runlist"); + return -1; + } + + /* Find the starting vcn in the run list. */ + while (rl->length) { + if (start_vcn < rl[1].vcn) + break; + rl++; + } + + if (!rl->length) { + errno = EIO; + ntfs_log_trace("Truncating already truncated runlist?\n"); + return -1; + } + + /* Truncate the run. */ + rl->length = start_vcn - rl->vcn; + + /* + * If a run was partially truncated, make the following runlist + * element a terminator instead of the truncated runlist + * element itself. + */ + if (rl->length) { + ++rl; +/* + if (!rl->length) + is_end = TRUE; +*/ + rl->vcn = start_vcn; + rl->length = 0; + } + rl->lcn = (LCN)LCN_ENOENT; + /** + * Reallocate memory if necessary. + * FIXME: Below code is broken, because runlist allocations must be + * a multiple of 4096. The code caused crashes and corruptions. + */ +/* + if (!is_end) { + size_t new_size = (rl - *arl + 1) * sizeof(runlist_element); + rl = realloc(*arl, new_size); + if (rl) + *arl = rl; + } +*/ + return 0; +} + +/** + * ntfs_rl_sparse - check whether runlist have sparse regions or not. + * @rl: runlist to check + * + * Return 1 if have, 0 if not, -1 on error with errno set to the error code. + */ +int ntfs_rl_sparse(runlist *rl) +{ + runlist *rlc; + + if (!rl) { + errno = EINVAL; + ntfs_log_perror("%s: ", __FUNCTION__); + return -1; + } + + for (rlc = rl; rlc->length; rlc++) + if (rlc->lcn < 0) { + if (rlc->lcn != LCN_HOLE) { + errno = EINVAL; + ntfs_log_perror("%s: bad runlist", __FUNCTION__); + return -1; + } + return 1; + } + return 0; +} + +/** + * ntfs_rl_get_compressed_size - calculate length of non sparse regions + * @vol: ntfs volume (need for cluster size) + * @rl: runlist to calculate for + * + * Return compressed size or -1 on error with errno set to the error code. + */ +s64 ntfs_rl_get_compressed_size(ntfs_volume *vol, runlist *rl) +{ + runlist *rlc; + s64 ret = 0; + + if (!rl) { + errno = EINVAL; + ntfs_log_perror("%s: ", __FUNCTION__); + return -1; + } + + for (rlc = rl; rlc->length; rlc++) { + if (rlc->lcn < 0) { + if (rlc->lcn != LCN_HOLE) { + errno = EINVAL; + ntfs_log_perror("%s: bad runlist", __FUNCTION__); + return -1; + } + } else + ret += rlc->length; + } + return ret << vol->cluster_size_bits; +} + + +#ifdef NTFS_TEST +/** + * test_rl_helper + */ +#define MKRL(R,V,L,S) \ + (R)->vcn = V; \ + (R)->lcn = L; \ + (R)->length = S; +/* +} +*/ +/** + * test_rl_dump_runlist - Runlist test: Display the contents of a runlist + * @rl: + * + * Description... + * + * Returns: + */ +static void test_rl_dump_runlist(const runlist_element *rl) +{ + int abbr = 0; /* abbreviate long lists */ + int len = 0; + int i; + const char *lcn_str[5] = { "HOLE", "NOTMAP", "ENOENT", "XXXX" }; + + if (!rl) { + printf(" Run list not present.\n"); + return; + } + + if (abbr) + for (len = 0; rl[len].length; len++) ; + + printf(" VCN LCN len\n"); + for (i = 0; ; i++, rl++) { + LCN lcn = rl->lcn; + + if ((abbr) && (len > 20)) { + if (i == 4) + printf(" ...\n"); + if ((i > 3) && (i < (len - 3))) + continue; + } + + if (lcn < (LCN)0) { + int ind = -lcn - 1; + + if (ind > -LCN_ENOENT - 1) + ind = 3; + printf("%8lld %8s %8lld\n", + rl->vcn, lcn_str[ind], rl->length); + } else + printf("%8lld %8lld %8lld\n", + rl->vcn, rl->lcn, rl->length); + if (!rl->length) + break; + } + if ((abbr) && (len > 20)) + printf(" (%d entries)\n", len+1); + printf("\n"); +} + +/** + * test_rl_runlists_merge - Runlist test: Merge two runlists + * @drl: + * @srl: + * + * Description... + * + * Returns: + */ +static runlist_element * test_rl_runlists_merge(runlist_element *drl, runlist_element *srl) +{ + runlist_element *res = NULL; + + printf("dst:\n"); + test_rl_dump_runlist(drl); + printf("src:\n"); + test_rl_dump_runlist(srl); + + res = ntfs_runlists_merge(drl, srl); + + printf("res:\n"); + test_rl_dump_runlist(res); + + return res; +} + +/** + * test_rl_read_buffer - Runlist test: Read a file containing a runlist + * @file: + * @buf: + * @bufsize: + * + * Description... + * + * Returns: + */ +static int test_rl_read_buffer(const char *file, u8 *buf, int bufsize) +{ + FILE *fptr; + + fptr = fopen(file, "r"); + if (!fptr) { + printf("open %s\n", file); + return 0; + } + + if (fread(buf, bufsize, 1, fptr) == 99) { + printf("read %s\n", file); + return 0; + } + + fclose(fptr); + return 1; +} + +/** + * test_rl_pure_src - Runlist test: Complicate the simple tests a little + * @contig: + * @multi: + * @vcn: + * @len: + * + * Description... + * + * Returns: + */ +static runlist_element * test_rl_pure_src(BOOL contig, BOOL multi, int vcn, int len) +{ + runlist_element *result; + int fudge; + + if (contig) + fudge = 0; + else + fudge = 999; + + result = ntfs_malloc(4096); + if (!result) + return NULL; + + if (multi) { + MKRL(result+0, vcn + (0*len/4), fudge + vcn + 1000 + (0*len/4), len / 4) + MKRL(result+1, vcn + (1*len/4), fudge + vcn + 1000 + (1*len/4), len / 4) + MKRL(result+2, vcn + (2*len/4), fudge + vcn + 1000 + (2*len/4), len / 4) + MKRL(result+3, vcn + (3*len/4), fudge + vcn + 1000 + (3*len/4), len / 4) + MKRL(result+4, vcn + (4*len/4), LCN_RL_NOT_MAPPED, 0) + } else { + MKRL(result+0, vcn, fudge + vcn + 1000, len) + MKRL(result+1, vcn + len, LCN_RL_NOT_MAPPED, 0) + } + return result; +} + +/** + * test_rl_pure_test - Runlist test: Perform tests using simple runlists + * @test: + * @contig: + * @multi: + * @vcn: + * @len: + * @file: + * @size: + * + * Description... + * + * Returns: + */ +static void test_rl_pure_test(int test, BOOL contig, BOOL multi, int vcn, int len, runlist_element *file, int size) +{ + runlist_element *src; + runlist_element *dst; + runlist_element *res; + + src = test_rl_pure_src(contig, multi, vcn, len); + dst = ntfs_malloc(4096); + if (!src || !dst) { + printf("Test %2d ---------- FAILED! (no free memory?)\n", test); + return; + } + + memcpy(dst, file, size); + + printf("Test %2d ----------\n", test); + res = test_rl_runlists_merge(dst, src); + + free(res); +} + +/** + * test_rl_pure - Runlist test: Create tests using simple runlists + * @contig: + * @multi: + * + * Description... + * + * Returns: + */ +static void test_rl_pure(char *contig, char *multi) +{ + /* VCN, LCN, len */ + static runlist_element file1[] = { + { 0, -1, 100 }, /* HOLE */ + { 100, 1100, 100 }, /* DATA */ + { 200, -1, 100 }, /* HOLE */ + { 300, 1300, 100 }, /* DATA */ + { 400, -1, 100 }, /* HOLE */ + { 500, -3, 0 } /* NOENT */ + }; + static runlist_element file2[] = { + { 0, 1000, 100 }, /* DATA */ + { 100, -1, 100 }, /* HOLE */ + { 200, -3, 0 } /* NOENT */ + }; + static runlist_element file3[] = { + { 0, 1000, 100 }, /* DATA */ + { 100, -3, 0 } /* NOENT */ + }; + static runlist_element file4[] = { + { 0, -3, 0 } /* NOENT */ + }; + static runlist_element file5[] = { + { 0, -2, 100 }, /* NOTMAP */ + { 100, 1100, 100 }, /* DATA */ + { 200, -2, 100 }, /* NOTMAP */ + { 300, 1300, 100 }, /* DATA */ + { 400, -2, 100 }, /* NOTMAP */ + { 500, -3, 0 } /* NOENT */ + }; + static runlist_element file6[] = { + { 0, 1000, 100 }, /* DATA */ + { 100, -2, 100 }, /* NOTMAP */ + { 200, -3, 0 } /* NOENT */ + }; + BOOL c, m; + + if (strcmp(contig, "contig") == 0) + c = TRUE; + else if (strcmp(contig, "noncontig") == 0) + c = FALSE; + else { + printf("rl pure [contig|noncontig] [single|multi]\n"); + return; + } + if (strcmp(multi, "multi") == 0) + m = TRUE; + else if (strcmp(multi, "single") == 0) + m = FALSE; + else { + printf("rl pure [contig|noncontig] [single|multi]\n"); + return; + } + + test_rl_pure_test(1, c, m, 0, 40, file1, sizeof(file1)); + test_rl_pure_test(2, c, m, 40, 40, file1, sizeof(file1)); + test_rl_pure_test(3, c, m, 60, 40, file1, sizeof(file1)); + test_rl_pure_test(4, c, m, 0, 100, file1, sizeof(file1)); + test_rl_pure_test(5, c, m, 200, 40, file1, sizeof(file1)); + test_rl_pure_test(6, c, m, 240, 40, file1, sizeof(file1)); + test_rl_pure_test(7, c, m, 260, 40, file1, sizeof(file1)); + test_rl_pure_test(8, c, m, 200, 100, file1, sizeof(file1)); + test_rl_pure_test(9, c, m, 400, 40, file1, sizeof(file1)); + test_rl_pure_test(10, c, m, 440, 40, file1, sizeof(file1)); + test_rl_pure_test(11, c, m, 460, 40, file1, sizeof(file1)); + test_rl_pure_test(12, c, m, 400, 100, file1, sizeof(file1)); + test_rl_pure_test(13, c, m, 160, 100, file2, sizeof(file2)); + test_rl_pure_test(14, c, m, 100, 140, file2, sizeof(file2)); + test_rl_pure_test(15, c, m, 200, 40, file2, sizeof(file2)); + test_rl_pure_test(16, c, m, 240, 40, file2, sizeof(file2)); + test_rl_pure_test(17, c, m, 100, 40, file3, sizeof(file3)); + test_rl_pure_test(18, c, m, 140, 40, file3, sizeof(file3)); + test_rl_pure_test(19, c, m, 0, 40, file4, sizeof(file4)); + test_rl_pure_test(20, c, m, 40, 40, file4, sizeof(file4)); + test_rl_pure_test(21, c, m, 0, 40, file5, sizeof(file5)); + test_rl_pure_test(22, c, m, 40, 40, file5, sizeof(file5)); + test_rl_pure_test(23, c, m, 60, 40, file5, sizeof(file5)); + test_rl_pure_test(24, c, m, 0, 100, file5, sizeof(file5)); + test_rl_pure_test(25, c, m, 200, 40, file5, sizeof(file5)); + test_rl_pure_test(26, c, m, 240, 40, file5, sizeof(file5)); + test_rl_pure_test(27, c, m, 260, 40, file5, sizeof(file5)); + test_rl_pure_test(28, c, m, 200, 100, file5, sizeof(file5)); + test_rl_pure_test(29, c, m, 400, 40, file5, sizeof(file5)); + test_rl_pure_test(30, c, m, 440, 40, file5, sizeof(file5)); + test_rl_pure_test(31, c, m, 460, 40, file5, sizeof(file5)); + test_rl_pure_test(32, c, m, 400, 100, file5, sizeof(file5)); + test_rl_pure_test(33, c, m, 160, 100, file6, sizeof(file6)); + test_rl_pure_test(34, c, m, 100, 140, file6, sizeof(file6)); +} + +/** + * test_rl_zero - Runlist test: Merge a zero-length runlist + * + * Description... + * + * Returns: + */ +static void test_rl_zero(void) +{ + runlist_element *jim = NULL; + runlist_element *bob = NULL; + + bob = calloc(3, sizeof(runlist_element)); + if (!bob) + return; + + MKRL(bob+0, 10, 99, 5) + MKRL(bob+1, 15, LCN_RL_NOT_MAPPED, 0) + + jim = test_rl_runlists_merge(jim, bob); + if (!jim) + return; + + free(jim); +} + +/** + * test_rl_frag_combine - Runlist test: Perform tests using fragmented files + * @vol: + * @attr1: + * @attr2: + * @attr3: + * + * Description... + * + * Returns: + */ +static void test_rl_frag_combine(ntfs_volume *vol, ATTR_RECORD *attr1, ATTR_RECORD *attr2, ATTR_RECORD *attr3) +{ + runlist_element *run1; + runlist_element *run2; + runlist_element *run3; + + run1 = ntfs_mapping_pairs_decompress(vol, attr1, NULL); + if (!run1) + return; + + run2 = ntfs_mapping_pairs_decompress(vol, attr2, NULL); + if (!run2) + return; + + run1 = test_rl_runlists_merge(run1, run2); + + run3 = ntfs_mapping_pairs_decompress(vol, attr3, NULL); + if (!run3) + return; + + run1 = test_rl_runlists_merge(run1, run3); + + free(run1); +} + +/** + * test_rl_frag - Runlist test: Create tests using very fragmented files + * @test: + * + * Description... + * + * Returns: + */ +static void test_rl_frag(char *test) +{ + ntfs_volume vol; + ATTR_RECORD *attr1 = ntfs_malloc(1024); + ATTR_RECORD *attr2 = ntfs_malloc(1024); + ATTR_RECORD *attr3 = ntfs_malloc(1024); + + if (!attr1 || !attr2 || !attr3) + goto out; + + vol.sb = NULL; + vol.sector_size_bits = 9; + vol.cluster_size = 2048; + vol.cluster_size_bits = 11; + vol.major_ver = 3; + + if (!test_rl_read_buffer("runlist-data/attr1.bin", (u8*) attr1, 1024)) + goto out; + if (!test_rl_read_buffer("runlist-data/attr2.bin", (u8*) attr2, 1024)) + goto out; + if (!test_rl_read_buffer("runlist-data/attr3.bin", (u8*) attr3, 1024)) + goto out; + + if (strcmp(test, "123") == 0) test_rl_frag_combine(&vol, attr1, attr2, attr3); + else if (strcmp(test, "132") == 0) test_rl_frag_combine(&vol, attr1, attr3, attr2); + else if (strcmp(test, "213") == 0) test_rl_frag_combine(&vol, attr2, attr1, attr3); + else if (strcmp(test, "231") == 0) test_rl_frag_combine(&vol, attr2, attr3, attr1); + else if (strcmp(test, "312") == 0) test_rl_frag_combine(&vol, attr3, attr1, attr2); + else if (strcmp(test, "321") == 0) test_rl_frag_combine(&vol, attr3, attr2, attr1); + else + printf("Frag: No such test '%s'\n", test); + +out: + free(attr1); + free(attr2); + free(attr3); +} + +/** + * test_rl_main - Runlist test: Program start (main) + * @argc: + * @argv: + * + * Description... + * + * Returns: + */ +int test_rl_main(int argc, char *argv[]) +{ + if ((argc == 2) && (strcmp(argv[1], "zero") == 0)) test_rl_zero(); + else if ((argc == 3) && (strcmp(argv[1], "frag") == 0)) test_rl_frag(argv[2]); + else if ((argc == 4) && (strcmp(argv[1], "pure") == 0)) test_rl_pure(argv[2], argv[3]); + else + printf("rl [zero|frag|pure] {args}\n"); + + return 0; +} + +#endif + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/security.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/security.c new file mode 100644 index 0000000..6e6dc14 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/security.c @@ -0,0 +1,5423 @@ +/** + * security.c - Handling security/ACLs in NTFS. Originated from the Linux-NTFS project. + * + * Copyright (c) 2004 Anton Altaparmakov + * Copyright (c) 2005-2006 Szabolcs Szakacsits + * Copyright (c) 2006 Yura Pakhuchiy + * Copyright (c) 2007-2015 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif + +#include +#include +#include + +#include "compat.h" +#include "param.h" +#include "types.h" +#include "layout.h" +#include "attrib.h" +#include "index.h" +#include "dir.h" +#include "bitmap.h" +#include "security.h" +#include "acls.h" +#include "cache.h" +#include "misc.h" +#include "xattrs.h" + +/* + * JPA NTFS constants or structs + * should be moved to layout.h + */ + +#define ALIGN_SDS_BLOCK 0x40000 /* Alignment for a $SDS block */ +#define ALIGN_SDS_ENTRY 16 /* Alignment for a $SDS entry */ +#define STUFFSZ 0x4000 /* unitary stuffing size for $SDS */ +#define FIRST_SECURITY_ID 0x100 /* Lowest security id */ + + /* Mask for attributes which can be forced */ +#define FILE_ATTR_SETTABLE ( FILE_ATTR_READONLY \ + | FILE_ATTR_HIDDEN \ + | FILE_ATTR_SYSTEM \ + | FILE_ATTR_ARCHIVE \ + | FILE_ATTR_TEMPORARY \ + | FILE_ATTR_OFFLINE \ + | FILE_ATTR_NOT_CONTENT_INDEXED ) + +struct SII { /* this is an image of an $SII index entry */ + le16 offs; + le16 size; + le32 fill1; + le16 indexsz; + le16 indexksz; + le16 flags; + le16 fill2; + le32 keysecurid; + + /* did not find official description for the following */ + le32 hash; + le32 securid; + le32 dataoffsl; /* documented as badly aligned */ + le32 dataoffsh; + le32 datasize; +} ; + +struct SDH { /* this is an image of an $SDH index entry */ + le16 offs; + le16 size; + le32 fill1; + le16 indexsz; + le16 indexksz; + le16 flags; + le16 fill2; + le32 keyhash; + le32 keysecurid; + + /* did not find official description for the following */ + le32 hash; + le32 securid; + le32 dataoffsl; + le32 dataoffsh; + le32 datasize; + le32 fill3; + } ; + +/* + * A few useful constants + */ + +static ntfschar sii_stream[] = { const_cpu_to_le16('$'), + const_cpu_to_le16('S'), + const_cpu_to_le16('I'), + const_cpu_to_le16('I'), + const_cpu_to_le16(0) }; +static ntfschar sdh_stream[] = { const_cpu_to_le16('$'), + const_cpu_to_le16('S'), + const_cpu_to_le16('D'), + const_cpu_to_le16('H'), + const_cpu_to_le16(0) }; + +/* + * null SID (S-1-0-0) + */ + +extern const SID *nullsid; + +/* + * The zero GUID. + */ + +static const GUID __zero_guid = { const_cpu_to_le32(0), const_cpu_to_le16(0), + const_cpu_to_le16(0), { 0, 0, 0, 0, 0, 0, 0, 0 } }; +static const GUID *const zero_guid = &__zero_guid; + +/** + * ntfs_guid_is_zero - check if a GUID is zero + * @guid: [IN] guid to check + * + * Return TRUE if @guid is a valid pointer to a GUID and it is the zero GUID + * and FALSE otherwise. + */ +BOOL ntfs_guid_is_zero(const GUID *guid) +{ + return (memcmp(guid, zero_guid, sizeof(*zero_guid))); +} + +/** + * ntfs_guid_to_mbs - convert a GUID to a multi byte string + * @guid: [IN] guid to convert + * @guid_str: [OUT] string in which to return the GUID (optional) + * + * Convert the GUID pointed to by @guid to a multi byte string of the form + * "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX". Therefore, @guid_str (if not NULL) + * needs to be able to store at least 37 bytes. + * + * If @guid_str is not NULL it will contain the converted GUID on return. If + * it is NULL a string will be allocated and this will be returned. The caller + * is responsible for free()ing the string in that case. + * + * On success return the converted string and on failure return NULL with errno + * set to the error code. + */ +char *ntfs_guid_to_mbs(const GUID *guid, char *guid_str) +{ + char *_guid_str; + int res; + + if (!guid) { + errno = EINVAL; + return NULL; + } + _guid_str = guid_str; + if (!_guid_str) { + _guid_str = (char*)ntfs_malloc(37); + if (!_guid_str) + return _guid_str; + } + res = snprintf(_guid_str, 37, + "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", + (unsigned int)le32_to_cpu(guid->data1), + le16_to_cpu(guid->data2), le16_to_cpu(guid->data3), + guid->data4[0], guid->data4[1], + guid->data4[2], guid->data4[3], guid->data4[4], + guid->data4[5], guid->data4[6], guid->data4[7]); + if (res == 36) + return _guid_str; + if (!guid_str) + free(_guid_str); + errno = EINVAL; + return NULL; +} + +/** + * ntfs_sid_to_mbs_size - determine maximum size for the string of a SID + * @sid: [IN] SID for which to determine the maximum string size + * + * Determine the maximum multi byte string size in bytes which is needed to + * store the standard textual representation of the SID pointed to by @sid. + * See ntfs_sid_to_mbs(), below. + * + * On success return the maximum number of bytes needed to store the multi byte + * string and on failure return -1 with errno set to the error code. + */ +int ntfs_sid_to_mbs_size(const SID *sid) +{ + int size, i; + + if (!ntfs_valid_sid(sid)) { + errno = EINVAL; + return -1; + } + /* Start with "S-". */ + size = 2; + /* + * Add the SID_REVISION. Hopefully the compiler will optimize this + * away as SID_REVISION is a constant. + */ + for (i = SID_REVISION; i > 0; i /= 10) + size++; + /* Add the "-". */ + size++; + /* + * Add the identifier authority. If it needs to be in decimal, the + * maximum is 2^32-1 = 4294967295 = 10 characters. If it needs to be + * in hexadecimal, then maximum is 0x665544332211 = 14 characters. + */ + if (!sid->identifier_authority.high_part) + size += 10; + else + size += 14; + /* + * Finally, add the sub authorities. For each we have a "-" followed + * by a decimal which can be up to 2^32-1 = 4294967295 = 10 characters. + */ + size += (1 + 10) * sid->sub_authority_count; + /* We need the zero byte at the end, too. */ + size++; + return size * sizeof(char); +} + +/** + * ntfs_sid_to_mbs - convert a SID to a multi byte string + * @sid: [IN] SID to convert + * @sid_str: [OUT] string in which to return the SID (optional) + * @sid_str_size: [IN] size in bytes of @sid_str + * + * Convert the SID pointed to by @sid to its standard textual representation. + * @sid_str (if not NULL) needs to be able to store at least + * ntfs_sid_to_mbs_size() bytes. @sid_str_size is the size in bytes of + * @sid_str if @sid_str is not NULL. + * + * The standard textual representation of the SID is of the form: + * S-R-I-S-S... + * Where: + * - The first "S" is the literal character 'S' identifying the following + * digits as a SID. + * - R is the revision level of the SID expressed as a sequence of digits + * in decimal. + * - I is the 48-bit identifier_authority, expressed as digits in decimal, + * if I < 2^32, or hexadecimal prefixed by "0x", if I >= 2^32. + * - S... is one or more sub_authority values, expressed as digits in + * decimal. + * + * If @sid_str is not NULL it will contain the converted SUID on return. If it + * is NULL a string will be allocated and this will be returned. The caller is + * responsible for free()ing the string in that case. + * + * On success return the converted string and on failure return NULL with errno + * set to the error code. + */ +char *ntfs_sid_to_mbs(const SID *sid, char *sid_str, size_t sid_str_size) +{ + u64 u; + le32 leauth; + char *s; + int i, j, cnt; + + /* + * No need to check @sid if !@sid_str since ntfs_sid_to_mbs_size() will + * check @sid, too. 8 is the minimum SID string size. + */ + if (sid_str && (sid_str_size < 8 || !ntfs_valid_sid(sid))) { + errno = EINVAL; + return NULL; + } + /* Allocate string if not provided. */ + if (!sid_str) { + cnt = ntfs_sid_to_mbs_size(sid); + if (cnt < 0) + return NULL; + s = (char*)ntfs_malloc(cnt); + if (!s) + return s; + sid_str = s; + /* So we know we allocated it. */ + sid_str_size = 0; + } else { + s = sid_str; + cnt = sid_str_size; + } + /* Start with "S-R-". */ + i = snprintf(s, cnt, "S-%hhu-", (unsigned char)sid->revision); + if (i < 0 || i >= cnt) + goto err_out; + s += i; + cnt -= i; + /* Add the identifier authority. */ + for (u = i = 0, j = 40; i < 6; i++, j -= 8) + u += (u64)sid->identifier_authority.value[i] << j; + if (!sid->identifier_authority.high_part) + i = snprintf(s, cnt, "%lu", (unsigned long)u); + else + i = snprintf(s, cnt, "0x%llx", (unsigned long long)u); + if (i < 0 || i >= cnt) + goto err_out; + s += i; + cnt -= i; + /* Finally, add the sub authorities. */ + for (j = 0; j < sid->sub_authority_count; j++) { + leauth = sid->sub_authority[j]; + i = snprintf(s, cnt, "-%u", (unsigned int) + le32_to_cpu(leauth)); + if (i < 0 || i >= cnt) + goto err_out; + s += i; + cnt -= i; + } + return sid_str; +err_out: + if (i >= cnt) + i = EMSGSIZE; + else + i = errno; + if (!sid_str_size) + free(sid_str); + errno = i; + return NULL; +} + +/** + * ntfs_generate_guid - generatates a random current guid. + * @guid: [OUT] pointer to a GUID struct to hold the generated guid. + * + * perhaps not a very good random number generator though... + */ +void ntfs_generate_guid(GUID *guid) +{ + unsigned int i; + u8 *p = (u8 *)guid; + + /* this is called at most once from mkntfs */ + srandom(time((time_t*)NULL) ^ (getpid() << 16)); + for (i = 0; i < sizeof(GUID); i++) { + p[i] = (u8)(random() & 0xFF); + if (i == 7) + p[7] = (p[7] & 0x0F) | 0x40; + if (i == 8) + p[8] = (p[8] & 0x3F) | 0x80; + } +} + +/** + * ntfs_security_hash - calculate the hash of a security descriptor + * @sd: self-relative security descriptor whose hash to calculate + * @length: size in bytes of the security descritor @sd + * + * Calculate the hash of the self-relative security descriptor @sd of length + * @length bytes. + * + * This hash is used in the $Secure system file as the primary key for the $SDH + * index and is also stored in the header of each security descriptor in the + * $SDS data stream as well as in the index data of both the $SII and $SDH + * indexes. In all three cases it forms part of the SDS_ENTRY_HEADER + * structure. + * + * Return the calculated security hash in little endian. + */ +le32 ntfs_security_hash(const SECURITY_DESCRIPTOR_RELATIVE *sd, const u32 len) +{ + const le32 *pos = (const le32*)sd; + const le32 *end = pos + (len >> 2); + u32 hash = 0; + + while (pos < end) { + hash = le32_to_cpup(pos) + ntfs_rol32(hash, 3); + pos++; + } + return cpu_to_le32(hash); +} + +/* + * Get the first entry of current index block + * cut and pasted form ntfs_ie_get_first() in index.c + */ + +static INDEX_ENTRY *ntfs_ie_get_first(INDEX_HEADER *ih) +{ + return (INDEX_ENTRY*)((u8*)ih + le32_to_cpu(ih->entries_offset)); +} + +/* + * Stuff a 256KB block into $SDS before writing descriptors + * into the block. + * + * This prevents $SDS from being automatically declared as sparse + * when the second copy of the first security descriptor is written + * 256KB further ahead. + * + * Having $SDS declared as a sparse file is not wrong by itself + * and chkdsk leaves it as a sparse file. It does however complain + * and add a sparse flag (0x0200) into field file_attributes of + * STANDARD_INFORMATION of $Secure. This probably means that a + * sparse attribute (ATTR_IS_SPARSE) is only allowed in sparse + * files (FILE_ATTR_SPARSE_FILE). + * + * Windows normally does not convert to sparse attribute or sparse + * file. Stuffing is just a way to get to the same result. + */ + +static int entersecurity_stuff(ntfs_volume *vol, off_t offs) +{ + int res; + int written; + unsigned long total; + char *stuff; + + res = 0; + total = 0; + stuff = (char*)ntfs_malloc(STUFFSZ); + if (stuff) { + memset(stuff, 0, STUFFSZ); + do { + written = ntfs_attr_data_write(vol->secure_ni, + STREAM_SDS, 4, stuff, STUFFSZ, offs); + if (written == STUFFSZ) { + total += STUFFSZ; + offs += STUFFSZ; + } else { + errno = ENOSPC; + res = -1; + } + } while (!res && (total < ALIGN_SDS_BLOCK)); + free(stuff); + } else { + errno = ENOMEM; + res = -1; + } + return (res); +} + +/* + * Enter a new security descriptor into $Secure (data only) + * it has to be written twice with an offset of 256KB + * + * Should only be called by entersecurityattr() to ensure consistency + * + * Returns zero if sucessful + */ + +static int entersecurity_data(ntfs_volume *vol, + const SECURITY_DESCRIPTOR_RELATIVE *attr, s64 attrsz, + le32 hash, le32 keyid, off_t offs, int gap) +{ + int res; + int written1; + int written2; + char *fullattr; + int fullsz; + SECURITY_DESCRIPTOR_HEADER *phsds; + + res = -1; + fullsz = attrsz + gap + sizeof(SECURITY_DESCRIPTOR_HEADER); + fullattr = (char*)ntfs_malloc(fullsz); + if (fullattr) { + /* + * Clear the gap from previous descriptor + * this could be useful for appending the second + * copy to the end of file. When creating a new + * 256K block, the gap is cleared while writing + * the first copy + */ + if (gap) + memset(fullattr,0,gap); + memcpy(&fullattr[gap + sizeof(SECURITY_DESCRIPTOR_HEADER)], + attr,attrsz); + phsds = (SECURITY_DESCRIPTOR_HEADER*)&fullattr[gap]; + phsds->hash = hash; + phsds->security_id = keyid; + phsds->offset = cpu_to_le64(offs); + phsds->length = cpu_to_le32(fullsz - gap); + written1 = ntfs_attr_data_write(vol->secure_ni, + STREAM_SDS, 4, fullattr, fullsz, + offs - gap); + written2 = ntfs_attr_data_write(vol->secure_ni, + STREAM_SDS, 4, fullattr, fullsz, + offs - gap + ALIGN_SDS_BLOCK); + if ((written1 == fullsz) + && (written2 == written1)) { + /* + * Make sure the data size for $SDS marks the end + * of the last security attribute. Windows uses + * this to determine where the next attribute will + * be written, which causes issues if chkdsk had + * previously deleted the last entries without + * adjusting the size. + */ + res = ntfs_attr_shrink_size(vol->secure_ni,STREAM_SDS, + 4, offs - gap + ALIGN_SDS_BLOCK + fullsz); + } else + errno = ENOSPC; + free(fullattr); + } else + errno = ENOMEM; + return (res); +} + +/* + * Enter a new security descriptor in $Secure (indexes only) + * + * Should only be called by entersecurityattr() to ensure consistency + * + * Returns zero if sucessful + */ + +static int entersecurity_indexes(ntfs_volume *vol, s64 attrsz, + le32 hash, le32 keyid, off_t offs) +{ + union { + struct { + le32 dataoffsl; + le32 dataoffsh; + } parts; + le64 all; + } realign; + int res; + ntfs_index_context *xsii; + ntfs_index_context *xsdh; + struct SII newsii; + struct SDH newsdh; + + res = -1; + /* enter a new $SII record */ + + xsii = vol->secure_xsii; + ntfs_index_ctx_reinit(xsii); + newsii.offs = const_cpu_to_le16(20); + newsii.size = const_cpu_to_le16(sizeof(struct SII) - 20); + newsii.fill1 = const_cpu_to_le32(0); + newsii.indexsz = const_cpu_to_le16(sizeof(struct SII)); + newsii.indexksz = const_cpu_to_le16(sizeof(SII_INDEX_KEY)); + newsii.flags = const_cpu_to_le16(0); + newsii.fill2 = const_cpu_to_le16(0); + newsii.keysecurid = keyid; + newsii.hash = hash; + newsii.securid = keyid; + realign.all = cpu_to_le64(offs); + newsii.dataoffsh = realign.parts.dataoffsh; + newsii.dataoffsl = realign.parts.dataoffsl; + newsii.datasize = cpu_to_le32(attrsz + + sizeof(SECURITY_DESCRIPTOR_HEADER)); + if (!ntfs_ie_add(xsii,(INDEX_ENTRY*)&newsii)) { + + /* enter a new $SDH record */ + + xsdh = vol->secure_xsdh; + ntfs_index_ctx_reinit(xsdh); + newsdh.offs = const_cpu_to_le16(24); + newsdh.size = const_cpu_to_le16( + sizeof(SECURITY_DESCRIPTOR_HEADER)); + newsdh.fill1 = const_cpu_to_le32(0); + newsdh.indexsz = const_cpu_to_le16( + sizeof(struct SDH)); + newsdh.indexksz = const_cpu_to_le16( + sizeof(SDH_INDEX_KEY)); + newsdh.flags = const_cpu_to_le16(0); + newsdh.fill2 = const_cpu_to_le16(0); + newsdh.keyhash = hash; + newsdh.keysecurid = keyid; + newsdh.hash = hash; + newsdh.securid = keyid; + newsdh.dataoffsh = realign.parts.dataoffsh; + newsdh.dataoffsl = realign.parts.dataoffsl; + newsdh.datasize = cpu_to_le32(attrsz + + sizeof(SECURITY_DESCRIPTOR_HEADER)); + /* special filler value, Windows generally */ + /* fills with 0x00490049, sometimes with zero */ + newsdh.fill3 = const_cpu_to_le32(0x00490049); + if (!ntfs_ie_add(xsdh,(INDEX_ENTRY*)&newsdh)) + res = 0; + } + return (res); +} + +/* + * Enter a new security descriptor in $Secure (data and indexes) + * Returns id of entry, or zero if there is a problem. + * (should not be called for NTFS version < 3.0) + * + * important : calls have to be serialized, however no locking is + * needed while fuse is not multithreaded + */ + +static le32 entersecurityattr(ntfs_volume *vol, + const SECURITY_DESCRIPTOR_RELATIVE *attr, s64 attrsz, + le32 hash) +{ + union { + struct { + le32 dataoffsl; + le32 dataoffsh; + } parts; + le64 all; + } realign; + le32 securid; + le32 keyid; + u32 newkey; + off_t offs; + int gap; + int size; + BOOL found; + struct SII *psii; + INDEX_ENTRY *entry; + INDEX_ENTRY *next; + ntfs_index_context *xsii; + int retries; + ntfs_attr *na; + int olderrno; + + /* find the first available securid beyond the last key */ + /* in $Secure:$SII. This also determines the first */ + /* available location in $Secure:$SDS, as this stream */ + /* is always appended to and the id's are allocated */ + /* in sequence */ + + securid = const_cpu_to_le32(0); + xsii = vol->secure_xsii; + ntfs_index_ctx_reinit(xsii); + offs = size = 0; + keyid = const_cpu_to_le32(-1); + olderrno = errno; + found = !ntfs_index_lookup((char*)&keyid, + sizeof(SII_INDEX_KEY), xsii); + if (!found && (errno != ENOENT)) { + ntfs_log_perror("Inconsistency in index $SII"); + psii = (struct SII*)NULL; + } else { + /* restore errno to avoid misinterpretation */ + errno = olderrno; + entry = xsii->entry; + psii = (struct SII*)xsii->entry; + } + if (psii) { + /* + * Get last entry in block, but must get first one + * one first, as we should already be beyond the + * last one. For some reason the search for the last + * entry sometimes does not return the last block... + * we assume this can only happen in root block + */ + if (xsii->is_in_root) + entry = ntfs_ie_get_first + ((INDEX_HEADER*)&xsii->ir->index); + else + entry = ntfs_ie_get_first + ((INDEX_HEADER*)&xsii->ib->index); + /* + * All index blocks should be at least half full + * so there always is a last entry but one, + * except when creating the first entry in index root. + * This was however found not to be true : chkdsk + * sometimes deletes all the (unused) keys in the last + * index block without rebalancing the tree. + * When this happens, a new search is restarted from + * the smallest key. + */ + keyid = const_cpu_to_le32(0); + retries = 0; + while (entry) { + next = ntfs_index_next(entry,xsii); + if (next) { + psii = (struct SII*)next; + /* save last key and */ + /* available position */ + keyid = psii->keysecurid; + realign.parts.dataoffsh + = psii->dataoffsh; + realign.parts.dataoffsl + = psii->dataoffsl; + offs = le64_to_cpu(realign.all); + size = le32_to_cpu(psii->datasize); + } + entry = next; + if (!entry && !keyid && !retries) { + /* search failed, retry from smallest key */ + ntfs_index_ctx_reinit(xsii); + found = !ntfs_index_lookup((char*)&keyid, + sizeof(SII_INDEX_KEY), xsii); + if (!found && (errno != ENOENT)) { + ntfs_log_perror("Index $SII is broken"); + psii = (struct SII*)NULL; + } else { + /* restore errno */ + errno = olderrno; + entry = xsii->entry; + psii = (struct SII*)entry; + } + if (psii + && !(psii->flags & INDEX_ENTRY_END)) { + /* save first key and */ + /* available position */ + keyid = psii->keysecurid; + realign.parts.dataoffsh + = psii->dataoffsh; + realign.parts.dataoffsl + = psii->dataoffsl; + offs = le64_to_cpu(realign.all); + size = le32_to_cpu(psii->datasize); + } + retries++; + } + } + } + if (!keyid) { + /* + * could not find any entry, before creating the first + * entry, make a double check by making sure size of $SII + * is less than needed for one entry + */ + securid = const_cpu_to_le32(0); + na = ntfs_attr_open(vol->secure_ni,AT_INDEX_ROOT,sii_stream,4); + if (na) { + if ((size_t)na->data_size < (sizeof(struct SII) + + sizeof(INDEX_ENTRY_HEADER))) { + ntfs_log_error("Creating the first security_id\n"); + securid = const_cpu_to_le32(FIRST_SECURITY_ID); + } + ntfs_attr_close(na); + } + if (!securid) { + ntfs_log_error("Error creating a security_id\n"); + errno = EIO; + } + } else { + newkey = le32_to_cpu(keyid) + 1; + securid = cpu_to_le32(newkey); + } + /* + * The security attr has to be written twice 256KB + * apart. This implies that offsets like + * 0x40000*odd_integer must be left available for + * the second copy. So align to next block when + * the last byte overflows on a wrong block. + */ + + if (securid) { + gap = (-size) & (ALIGN_SDS_ENTRY - 1); + offs += gap + size; + if ((offs + attrsz + sizeof(SECURITY_DESCRIPTOR_HEADER) - 1) + & ALIGN_SDS_BLOCK) { + offs = ((offs + attrsz + + sizeof(SECURITY_DESCRIPTOR_HEADER) - 1) + | (ALIGN_SDS_BLOCK - 1)) + 1; + } + if (!(offs & (ALIGN_SDS_BLOCK - 1))) + entersecurity_stuff(vol, offs); + /* + * now write the security attr to storage : + * first data, then SII, then SDH + * If failure occurs while writing SDS, data will never + * be accessed through indexes, and will be overwritten + * by the next allocated descriptor + * If failure occurs while writing SII, the id has not + * recorded and will be reallocated later + * If failure occurs while writing SDH, the space allocated + * in SDS or SII will not be reused, an inconsistency + * will persist with no significant consequence + */ + if (entersecurity_data(vol, attr, attrsz, hash, securid, offs, gap) + || entersecurity_indexes(vol, attrsz, hash, securid, offs)) + securid = const_cpu_to_le32(0); + } + /* inode now is dirty, synchronize it all */ + ntfs_index_entry_mark_dirty(vol->secure_xsii); + ntfs_index_ctx_reinit(vol->secure_xsii); + ntfs_index_entry_mark_dirty(vol->secure_xsdh); + ntfs_index_ctx_reinit(vol->secure_xsdh); + NInoSetDirty(vol->secure_ni); + if (ntfs_inode_sync(vol->secure_ni)) + ntfs_log_perror("Could not sync $Secure\n"); + return (securid); +} + +/* + * Find a matching security descriptor in $Secure, + * if none, allocate a new id and write the descriptor to storage + * Returns id of entry, or zero if there is a problem. + * + * important : calls have to be serialized, however no locking is + * needed while fuse is not multithreaded + */ + +static le32 setsecurityattr(ntfs_volume *vol, + const SECURITY_DESCRIPTOR_RELATIVE *attr, s64 attrsz) +{ + struct SDH *psdh; /* this is an image of index (le) */ + union { + struct { + le32 dataoffsl; + le32 dataoffsh; + } parts; + le64 all; + } realign; + BOOL found; + BOOL collision; + size_t size; + size_t rdsize; + s64 offs; + int res; + ntfs_index_context *xsdh; + char *oldattr; + SDH_INDEX_KEY key; + INDEX_ENTRY *entry; + le32 securid; + le32 hash; + int olderrno; + + hash = ntfs_security_hash(attr,attrsz); + oldattr = (char*)NULL; + securid = const_cpu_to_le32(0); + res = 0; + xsdh = vol->secure_xsdh; + if (vol->secure_ni && xsdh && !vol->secure_reentry++) { + ntfs_index_ctx_reinit(xsdh); + /* + * find the nearest key as (hash,0) + * (do not search for partial key : in case of collision, + * it could return a key which is not the first one which + * collides) + */ + key.hash = hash; + key.security_id = const_cpu_to_le32(0); + olderrno = errno; + found = !ntfs_index_lookup((char*)&key, + sizeof(SDH_INDEX_KEY), xsdh); + if (!found && (errno != ENOENT)) + ntfs_log_perror("Inconsistency in index $SDH"); + else { + /* restore errno to avoid misinterpretation */ + errno = olderrno; + entry = xsdh->entry; + found = FALSE; + /* + * lookup() may return a node with no data, + * if so get next + */ + if (entry->ie_flags & INDEX_ENTRY_END) + entry = ntfs_index_next(entry,xsdh); + do { + collision = FALSE; + psdh = (struct SDH*)entry; + if (psdh) + size = (size_t) le32_to_cpu(psdh->datasize) + - sizeof(SECURITY_DESCRIPTOR_HEADER); + else size = 0; + /* if hash is not the same, the key is not present */ + if (psdh && (size > 0) + && (psdh->keyhash == hash)) { + /* if hash is the same */ + /* check the whole record */ + realign.parts.dataoffsh = psdh->dataoffsh; + realign.parts.dataoffsl = psdh->dataoffsl; + offs = le64_to_cpu(realign.all) + + sizeof(SECURITY_DESCRIPTOR_HEADER); + oldattr = (char*)ntfs_malloc(size); + if (oldattr) { + rdsize = ntfs_attr_data_read( + vol->secure_ni, + STREAM_SDS, 4, + oldattr, size, offs); + found = (rdsize == size) + && !memcmp(oldattr,attr,size); + free(oldattr); + /* if the records do not compare */ + /* (hash collision), try next one */ + if (!found) { + entry = ntfs_index_next( + entry,xsdh); + collision = TRUE; + } + } else + res = ENOMEM; + } + } while (collision && entry); + if (found) + securid = psdh->keysecurid; + else { + if (res) { + errno = res; + securid = const_cpu_to_le32(0); + } else { + /* + * no matching key : + * have to build a new one + */ + securid = entersecurityattr(vol, + attr, attrsz, hash); + } + } + } + } + if (--vol->secure_reentry) + ntfs_log_perror("Reentry error, check no multithreading\n"); + return (securid); +} + + +/* + * Update the security descriptor of a file + * Either as an attribute (complying with pre v3.x NTFS version) + * or, when possible, as an entry in $Secure (for NTFS v3.x) + * + * returns 0 if success + */ + +static int update_secur_descr(ntfs_volume *vol, + char *newattr, ntfs_inode *ni) +{ + int newattrsz; + int written; + int res; + ntfs_attr *na; + + newattrsz = ntfs_attr_size(newattr); + +#if !FORCE_FORMAT_v1x + if ((vol->major_ver < 3) || !vol->secure_ni) { +#endif + + /* update for NTFS format v1.x */ + + /* update the old security attribute */ + na = ntfs_attr_open(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0); + if (na) { + /* resize attribute */ + res = ntfs_attr_truncate(na, (s64) newattrsz); + /* overwrite value */ + if (!res) { + written = (int)ntfs_attr_pwrite(na, (s64) 0, + (s64) newattrsz, newattr); + if (written != newattrsz) { + ntfs_log_error("Failed to update " + "a v1.x security descriptor\n"); + errno = EIO; + res = -1; + } + } + + ntfs_attr_close(na); + /* if old security attribute was found, also */ + /* truncate standard information attribute to v1.x */ + /* this is needed when security data is wanted */ + /* as v1.x though volume is formatted for v3.x */ + na = ntfs_attr_open(ni, AT_STANDARD_INFORMATION, + AT_UNNAMED, 0); + if (na) { + clear_nino_flag(ni, v3_Extensions); + /* + * Truncating the record does not sweep extensions + * from copy in memory. Clear security_id to be safe + */ + ni->security_id = const_cpu_to_le32(0); + res = ntfs_attr_truncate(na, (s64)48); + ntfs_attr_close(na); + clear_nino_flag(ni, v3_Extensions); + } + } else { + /* + * insert the new security attribute if there + * were none + */ + res = ntfs_attr_add(ni, AT_SECURITY_DESCRIPTOR, + AT_UNNAMED, 0, (u8*)newattr, + (s64) newattrsz); + } +#if !FORCE_FORMAT_v1x + } else { + + /* update for NTFS format v3.x */ + + le32 securid; + + securid = setsecurityattr(vol, + (const SECURITY_DESCRIPTOR_RELATIVE*)newattr, + (s64)newattrsz); + if (securid) { + na = ntfs_attr_open(ni, AT_STANDARD_INFORMATION, + AT_UNNAMED, 0); + if (na) { + res = 0; + if (!test_nino_flag(ni, v3_Extensions)) { + /* expand standard information attribute to v3.x */ + res = ntfs_attr_truncate(na, + (s64)offsetof(STANDARD_INFORMATION, v3_end)); + ni->owner_id = const_cpu_to_le32(0); + ni->quota_charged = const_cpu_to_le64(0); + ni->usn = const_cpu_to_le64(0); + ntfs_attr_remove(ni, + AT_SECURITY_DESCRIPTOR, + AT_UNNAMED, 0); + } + set_nino_flag(ni, v3_Extensions); + ni->security_id = securid; + ntfs_attr_close(na); + } else { + ntfs_log_error("Failed to update " + "standard informations\n"); + errno = EIO; + res = -1; + } + } else + res = -1; + } +#endif + + /* mark node as dirty */ + NInoSetDirty(ni); + return (res); +} + +/* + * Upgrade the security descriptor of a file + * This is intended to allow graceful upgrades for files which + * were created in previous versions, with a security attributes + * and no security id. + * + * It will allocate a security id and replace the individual + * security attribute by a reference to the global one + * + * Special files are not upgraded (currently / and files in + * directories /$*) + * + * Though most code is similar to update_secur_desc() it has + * been kept apart to facilitate the further processing of + * special cases or even to remove it if found dangerous. + * + * returns 0 if success, + * 1 if not upgradable. This is not an error. + * -1 if there is a problem + */ + +static int upgrade_secur_desc(ntfs_volume *vol, + const char *attr, ntfs_inode *ni) +{ + int attrsz; + int res; + le32 securid; + ntfs_attr *na; + + /* + * upgrade requires NTFS format v3.x + * also refuse upgrading for special files + * whose number is less than FILE_first_user + */ + + if ((vol->major_ver >= 3) + && (ni->mft_no >= FILE_first_user)) { + attrsz = ntfs_attr_size(attr); + securid = setsecurityattr(vol, + (const SECURITY_DESCRIPTOR_RELATIVE*)attr, + (s64)attrsz); + if (securid) { + na = ntfs_attr_open(ni, AT_STANDARD_INFORMATION, + AT_UNNAMED, 0); + if (na) { + /* expand standard information attribute to v3.x */ + res = ntfs_attr_truncate(na, + (s64)offsetof(STANDARD_INFORMATION, v3_end)); + ni->owner_id = const_cpu_to_le32(0); + ni->quota_charged = const_cpu_to_le64(0); + ni->usn = const_cpu_to_le64(0); + ntfs_attr_remove(ni, AT_SECURITY_DESCRIPTOR, + AT_UNNAMED, 0); + set_nino_flag(ni, v3_Extensions); + ni->security_id = securid; + ntfs_attr_close(na); + } else { + ntfs_log_error("Failed to upgrade " + "standard informations\n"); + errno = EIO; + res = -1; + } + } else + res = -1; + /* mark node as dirty */ + NInoSetDirty(ni); + } else + res = 1; + + return (res); +} + +/* + * Optional simplified checking of group membership + * + * This only takes into account the groups defined in + * /etc/group at initialization time. + * It does not take into account the groups dynamically set by + * setgroups() nor the changes in /etc/group since initialization + * + * This optional method could be useful if standard checking + * leads to a performance concern. + * + * Should not be called for user root, however the group may be root + * + */ + +static BOOL staticgroupmember(struct SECURITY_CONTEXT *scx, uid_t uid, gid_t gid) +{ + BOOL ingroup; + int grcnt; + gid_t *groups; + struct MAPPING *user; + + ingroup = FALSE; + if (uid) { + user = scx->mapping[MAPUSERS]; + while (user && ((uid_t)user->xid != uid)) + user = user->next; + if (user) { + groups = user->groups; + grcnt = user->grcnt; + while ((--grcnt >= 0) && (groups[grcnt] != gid)) { } + ingroup = (grcnt >= 0); + } + } + return (ingroup); +} + +#if defined(__sun) && defined (__SVR4) + +/* + * Check whether current thread owner is member of file group + * Solaris/OpenIndiana version + * Should not be called for user root, however the group may be root + * + * The group list is available in "/proc/$PID/cred" + * + */ + +static BOOL groupmember(struct SECURITY_CONTEXT *scx, uid_t uid, gid_t gid) +{ + typedef struct prcred { + uid_t pr_euid; /* effective user id */ + uid_t pr_ruid; /* real user id */ + uid_t pr_suid; /* saved user id (from exec) */ + gid_t pr_egid; /* effective group id */ + gid_t pr_rgid; /* real group id */ + gid_t pr_sgid; /* saved group id (from exec) */ + int pr_ngroups; /* number of supplementary groups */ + gid_t pr_groups[1]; /* array of supplementary groups */ + } prcred_t; + enum { readset = 16 }; + + prcred_t basecreds; + gid_t groups[readset]; + char filename[64]; + int fd; + int k; + int cnt; + gid_t *p; + BOOL ismember; + int got; + pid_t tid; + + if (scx->vol->secure_flags & (1 << SECURITY_STATICGRPS)) + ismember = staticgroupmember(scx, uid, gid); + else { + ismember = FALSE; /* default return */ + tid = scx->tid; + sprintf(filename,"/proc/%u/cred",tid); + fd = open(filename,O_RDONLY); + if (fd >= 0) { + got = read(fd, &basecreds, sizeof(prcred_t)); + if (got == sizeof(prcred_t)) { + if (basecreds.pr_egid == gid) + ismember = TRUE; + p = basecreds.pr_groups; + cnt = 1; + k = 0; + while (!ismember + && (k < basecreds.pr_ngroups) + && (cnt > 0) + && (*p != gid)) { + k++; + cnt--; + p++; + if (cnt <= 0) { + got = read(fd, groups, + readset*sizeof(gid_t)); + cnt = got/sizeof(gid_t); + p = groups; + } + } + if ((cnt > 0) + && (k < basecreds.pr_ngroups)) + ismember = TRUE; + } + close(fd); + } + } + return (ismember); +} + +#else /* defined(__sun) && defined (__SVR4) */ + +/* + * Check whether current thread owner is member of file group + * Linux version + * Should not be called for user root, however the group may be root + * + * As indicated by Miklos Szeredi : + * + * The group list is available in + * + * /proc/$PID/task/$TID/status + * + * and fuse supplies TID in get_fuse_context()->pid. The only problem is + * finding out PID, for which I have no good solution, except to iterate + * through all processes. This is rather slow, but may be speeded up + * with caching and heuristics (for single threaded programs PID = TID). + * + * The following implementation gets the group list from + * /proc/$TID/task/$TID/status which apparently exists and + * contains the same data. + */ + +static BOOL groupmember(struct SECURITY_CONTEXT *scx, uid_t uid, gid_t gid) +{ + static char key[] = "\nGroups:"; + char buf[BUFSZ+1]; + char filename[64]; + enum { INKEY, INSEP, INNUM, INEND } state; + int fd; + char c; + int matched; + BOOL ismember; + int got; + char *p; + gid_t grp; + pid_t tid; + + if (scx->vol->secure_flags & (1 << SECURITY_STATICGRPS)) + ismember = staticgroupmember(scx, uid, gid); + else { + ismember = FALSE; /* default return */ + tid = scx->tid; + sprintf(filename,"/proc/%u/task/%u/status",tid,tid); + fd = open(filename,O_RDONLY); + if (fd >= 0) { + got = read(fd, buf, BUFSZ); + buf[got] = 0; + state = INKEY; + matched = 0; + p = buf; + grp = 0; + /* + * A simple automaton to process lines like + * Groups: 14 500 513 + */ + do { + c = *p++; + if (!c) { + /* refill buffer */ + got = read(fd, buf, BUFSZ); + buf[got] = 0; + p = buf; + c = *p++; /* 0 at end of file */ + } + switch (state) { + case INKEY : + if (key[matched] == c) { + if (!key[++matched]) + state = INSEP; + } else + if (key[0] == c) + matched = 1; + else + matched = 0; + break; + case INSEP : + if ((c >= '0') && (c <= '9')) { + grp = c - '0'; + state = INNUM; + } else + if ((c != ' ') && (c != '\t')) + state = INEND; + break; + case INNUM : + if ((c >= '0') && (c <= '9')) + grp = grp*10 + c - '0'; + else { + ismember = (grp == gid); + if ((c != ' ') && (c != '\t')) + state = INEND; + else + state = INSEP; + } + default : + break; + } + } while (!ismember && c && (state != INEND)); + close(fd); + if (!c) + ntfs_log_error("No group record found in %s\n",filename); + } else + ntfs_log_error("Could not open %s\n",filename); + } + return (ismember); +} + +#endif /* defined(__sun) && defined (__SVR4) */ + +#if POSIXACLS + +/* + * Extract the basic permissions from a Posix ACL + * + * This is only to be used when Posix ACLs are compiled in, + * but not enabled in the mount options. + * + * it replaces the permission mask by the group permissions. + * If special groups are mapped, they are also considered as world. + */ + +static int ntfs_basic_perms(const struct SECURITY_CONTEXT *scx, + const struct POSIX_SECURITY *pxdesc) +{ + int k; + int perms; + const struct POSIX_ACE *pace; + const struct MAPPING* group; + + k = 0; + perms = pxdesc->mode; + for (k=0; k < pxdesc->acccnt; k++) { + pace = &pxdesc->acl.ace[k]; + if (pace->tag == POSIX_ACL_GROUP_OBJ) + perms = (perms & 07707) + | ((pace->perms & 7) << 3); + else + if (pace->tag == POSIX_ACL_GROUP) { + group = scx->mapping[MAPGROUPS]; + while (group && (group->xid != pace->id)) + group = group->next; + if (group && group->grcnt + && (*(group->groups) == (gid_t)pace->id)) + perms |= pace->perms & 7; + } + } + return (perms); +} + +#endif /* POSIXACLS */ + +/* + * Cacheing is done two-way : + * - from uid, gid and perm to securid (CACHED_SECURID) + * - from a securid to uid, gid and perm (CACHED_PERMISSIONS) + * + * CACHED_SECURID data is kept in a most-recent-first list + * which should not be too long to be efficient. Its optimal + * size is depends on usage and is hard to determine. + * + * CACHED_PERMISSIONS data is kept in a two-level indexed array. It + * is optimal at the expense of storage. Use of a most-recent-first + * list would save memory and provide similar performances for + * standard usage, but not for file servers with too many file + * owners + * + * CACHED_PERMISSIONS_LEGACY is a special case for CACHED_PERMISSIONS + * for legacy directories which were not allocated a security_id + * it is organized in a most-recent-first list. + * + * In main caches, data is never invalidated, as the meaning of + * a security_id only changes when user mapping is changed, which + * current implies remounting. However returned entries may be + * overwritten at next update, so data has to be copied elsewhere + * before another cache update is made. + * In legacy cache, data has to be invalidated when protection is + * changed. + * + * Though the same data may be found in both list, they + * must be kept separately : the interpretation of ACL + * in both direction are approximations which could be non + * reciprocal for some configuration of the user mapping data + * + * During the process of recompiling ntfs-3g from a tgz archive, + * security processing added 7.6% to the cpu time used by ntfs-3g + * and 30% if the cache is disabled. + */ + +static struct PERMISSIONS_CACHE *create_caches(struct SECURITY_CONTEXT *scx, + u32 securindex) +{ + struct PERMISSIONS_CACHE *cache; + unsigned int index1; + unsigned int i; + + cache = (struct PERMISSIONS_CACHE*)NULL; + /* create the first permissions blocks */ + index1 = securindex >> CACHE_PERMISSIONS_BITS; + cache = (struct PERMISSIONS_CACHE*) + ntfs_malloc(sizeof(struct PERMISSIONS_CACHE) + + index1*sizeof(struct CACHED_PERMISSIONS*)); + if (cache) { + cache->head.last = index1; + cache->head.p_reads = 0; + cache->head.p_hits = 0; + cache->head.p_writes = 0; + *scx->pseccache = cache; + for (i=0; i<=index1; i++) + cache->cachetable[i] + = (struct CACHED_PERMISSIONS*)NULL; + } + return (cache); +} + +/* + * Free memory used by caches + * The only purpose is to facilitate the detection of memory leaks + */ + +static void free_caches(struct SECURITY_CONTEXT *scx) +{ + unsigned int index1; + struct PERMISSIONS_CACHE *pseccache; + + pseccache = *scx->pseccache; + if (pseccache) { + for (index1=0; index1<=pseccache->head.last; index1++) + if (pseccache->cachetable[index1]) { +#if POSIXACLS + struct CACHED_PERMISSIONS *cacheentry; + unsigned int index2; + + for (index2=0; index2<(1<< CACHE_PERMISSIONS_BITS); index2++) { + cacheentry = &pseccache->cachetable[index1][index2]; + if (cacheentry->valid + && cacheentry->pxdesc) + free(cacheentry->pxdesc); + } +#endif + free(pseccache->cachetable[index1]); + } + free(pseccache); + } +} + +static int compare(const struct CACHED_SECURID *cached, + const struct CACHED_SECURID *item) +{ +#if POSIXACLS + size_t csize; + size_t isize; + + /* only compare data and sizes */ + csize = (cached->variable ? + sizeof(struct POSIX_ACL) + + (((struct POSIX_SECURITY*)cached->variable)->acccnt + + ((struct POSIX_SECURITY*)cached->variable)->defcnt) + *sizeof(struct POSIX_ACE) : + 0); + isize = (item->variable ? + sizeof(struct POSIX_ACL) + + (((struct POSIX_SECURITY*)item->variable)->acccnt + + ((struct POSIX_SECURITY*)item->variable)->defcnt) + *sizeof(struct POSIX_ACE) : + 0); + return ((cached->uid != item->uid) + || (cached->gid != item->gid) + || (cached->dmode != item->dmode) + || (csize != isize) + || (csize + && isize + && memcmp(&((struct POSIX_SECURITY*)cached->variable)->acl, + &((struct POSIX_SECURITY*)item->variable)->acl, csize))); +#else + return ((cached->uid != item->uid) + || (cached->gid != item->gid) + || (cached->dmode != item->dmode)); +#endif +} + +static int leg_compare(const struct CACHED_PERMISSIONS_LEGACY *cached, + const struct CACHED_PERMISSIONS_LEGACY *item) +{ + return (cached->mft_no != item->mft_no); +} + +/* + * Resize permission cache table + * do not call unless resizing is needed + * + * If allocation fails, the cache size is not updated + * Lack of memory is not considered as an error, the cache is left + * consistent and errno is not set. + */ + +static void resize_cache(struct SECURITY_CONTEXT *scx, + u32 securindex) +{ + struct PERMISSIONS_CACHE *oldcache; + struct PERMISSIONS_CACHE *newcache; + int newcnt; + int oldcnt; + unsigned int index1; + unsigned int i; + + oldcache = *scx->pseccache; + index1 = securindex >> CACHE_PERMISSIONS_BITS; + newcnt = index1 + 1; + if (newcnt <= ((CACHE_PERMISSIONS_SIZE + + (1 << CACHE_PERMISSIONS_BITS) + - 1) >> CACHE_PERMISSIONS_BITS)) { + /* expand cache beyond current end, do not use realloc() */ + /* to avoid losing data when there is no more memory */ + oldcnt = oldcache->head.last + 1; + newcache = (struct PERMISSIONS_CACHE*) + ntfs_malloc( + sizeof(struct PERMISSIONS_CACHE) + + (newcnt - 1)*sizeof(struct CACHED_PERMISSIONS*)); + if (newcache) { + memcpy(newcache,oldcache, + sizeof(struct PERMISSIONS_CACHE) + + (oldcnt - 1)*sizeof(struct CACHED_PERMISSIONS*)); + free(oldcache); + /* mark new entries as not valid */ + for (i=newcache->head.last+1; i<=index1; i++) + newcache->cachetable[i] + = (struct CACHED_PERMISSIONS*)NULL; + newcache->head.last = index1; + *scx->pseccache = newcache; + } + } +} + +/* + * Enter uid, gid and mode into cache, if possible + * + * returns the updated or created cache entry, + * or NULL if not possible (typically if there is no + * security id associated) + */ + +#if POSIXACLS +static struct CACHED_PERMISSIONS *enter_cache(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, uid_t uid, gid_t gid, + struct POSIX_SECURITY *pxdesc) +#else +static struct CACHED_PERMISSIONS *enter_cache(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, uid_t uid, gid_t gid, mode_t mode) +#endif +{ + struct CACHED_PERMISSIONS *cacheentry; + struct CACHED_PERMISSIONS *cacheblock; + struct PERMISSIONS_CACHE *pcache; + u32 securindex; +#if POSIXACLS + int pxsize; + struct POSIX_SECURITY *pxcached; +#endif + unsigned int index1; + unsigned int index2; + int i; + + /* cacheing is only possible if a security_id has been defined */ + if (test_nino_flag(ni, v3_Extensions) + && ni->security_id) { + /* + * Immediately test the most frequent situation + * where the entry exists + */ + securindex = le32_to_cpu(ni->security_id); + index1 = securindex >> CACHE_PERMISSIONS_BITS; + index2 = securindex & ((1 << CACHE_PERMISSIONS_BITS) - 1); + pcache = *scx->pseccache; + if (pcache + && (pcache->head.last >= index1) + && pcache->cachetable[index1]) { + cacheentry = &pcache->cachetable[index1][index2]; + cacheentry->uid = uid; + cacheentry->gid = gid; +#if POSIXACLS + if (cacheentry->valid && cacheentry->pxdesc) + free(cacheentry->pxdesc); + if (pxdesc) { + pxsize = sizeof(struct POSIX_SECURITY) + + (pxdesc->acccnt + pxdesc->defcnt)*sizeof(struct POSIX_ACE); + pxcached = (struct POSIX_SECURITY*)malloc(pxsize); + if (pxcached) { + memcpy(pxcached, pxdesc, pxsize); + cacheentry->pxdesc = pxcached; + } else { + cacheentry->valid = 0; + cacheentry = (struct CACHED_PERMISSIONS*)NULL; + } + cacheentry->mode = pxdesc->mode & 07777; + } else + cacheentry->pxdesc = (struct POSIX_SECURITY*)NULL; +#else + cacheentry->mode = mode & 07777; +#endif + cacheentry->inh_fileid = const_cpu_to_le32(0); + cacheentry->inh_dirid = const_cpu_to_le32(0); + cacheentry->valid = 1; + pcache->head.p_writes++; + } else { + if (!pcache) { + /* create the first cache block */ + pcache = create_caches(scx, securindex); + } else { + if (index1 > pcache->head.last) { + resize_cache(scx, securindex); + pcache = *scx->pseccache; + } + } + /* allocate block, if cache table was allocated */ + if (pcache && (index1 <= pcache->head.last)) { + cacheblock = (struct CACHED_PERMISSIONS*) + malloc(sizeof(struct CACHED_PERMISSIONS) + << CACHE_PERMISSIONS_BITS); + pcache->cachetable[index1] = cacheblock; + for (i=0; i<(1 << CACHE_PERMISSIONS_BITS); i++) + cacheblock[i].valid = 0; + cacheentry = &cacheblock[index2]; + if (cacheentry) { + cacheentry->uid = uid; + cacheentry->gid = gid; +#if POSIXACLS + if (pxdesc) { + pxsize = sizeof(struct POSIX_SECURITY) + + (pxdesc->acccnt + pxdesc->defcnt)*sizeof(struct POSIX_ACE); + pxcached = (struct POSIX_SECURITY*)malloc(pxsize); + if (pxcached) { + memcpy(pxcached, pxdesc, pxsize); + cacheentry->pxdesc = pxcached; + } else { + cacheentry->valid = 0; + cacheentry = (struct CACHED_PERMISSIONS*)NULL; + } + cacheentry->mode = pxdesc->mode & 07777; + } else + cacheentry->pxdesc = (struct POSIX_SECURITY*)NULL; +#else + cacheentry->mode = mode & 07777; +#endif + cacheentry->inh_fileid = const_cpu_to_le32(0); + cacheentry->inh_dirid = const_cpu_to_le32(0); + cacheentry->valid = 1; + pcache->head.p_writes++; + } + } else + cacheentry = (struct CACHED_PERMISSIONS*)NULL; + } + } else { + cacheentry = (struct CACHED_PERMISSIONS*)NULL; +#if CACHE_LEGACY_SIZE + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + struct CACHED_PERMISSIONS_LEGACY wanted; + struct CACHED_PERMISSIONS_LEGACY *legacy; + + wanted.perm.uid = uid; + wanted.perm.gid = gid; +#if POSIXACLS + wanted.perm.mode = pxdesc->mode & 07777; + wanted.perm.inh_fileid = const_cpu_to_le32(0); + wanted.perm.inh_dirid = const_cpu_to_le32(0); + wanted.mft_no = ni->mft_no; + wanted.variable = (void*)pxdesc; + wanted.varsize = sizeof(struct POSIX_SECURITY) + + (pxdesc->acccnt + pxdesc->defcnt)*sizeof(struct POSIX_ACE); +#else + wanted.perm.mode = mode & 07777; + wanted.perm.inh_fileid = const_cpu_to_le32(0); + wanted.perm.inh_dirid = const_cpu_to_le32(0); + wanted.mft_no = ni->mft_no; + wanted.variable = (void*)NULL; + wanted.varsize = 0; +#endif + legacy = (struct CACHED_PERMISSIONS_LEGACY*)ntfs_enter_cache( + scx->vol->legacy_cache, GENERIC(&wanted), + (cache_compare)leg_compare); + if (legacy) { + cacheentry = &legacy->perm; +#if POSIXACLS + /* + * give direct access to the cached pxdesc + * in the permissions structure + */ + cacheentry->pxdesc = legacy->variable; +#endif + } + } +#endif + } + return (cacheentry); +} + +/* + * Fetch owner, group and permission of a file, if cached + * + * Beware : do not use the returned entry after a cache update : + * the cache may be relocated making the returned entry meaningless + * + * returns the cache entry, or NULL if not available + */ + +static struct CACHED_PERMISSIONS *fetch_cache(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni) +{ + struct CACHED_PERMISSIONS *cacheentry; + struct PERMISSIONS_CACHE *pcache; + u32 securindex; + unsigned int index1; + unsigned int index2; + + /* cacheing is only possible if a security_id has been defined */ + cacheentry = (struct CACHED_PERMISSIONS*)NULL; + if (test_nino_flag(ni, v3_Extensions) + && (ni->security_id)) { + securindex = le32_to_cpu(ni->security_id); + index1 = securindex >> CACHE_PERMISSIONS_BITS; + index2 = securindex & ((1 << CACHE_PERMISSIONS_BITS) - 1); + pcache = *scx->pseccache; + if (pcache + && (pcache->head.last >= index1) + && pcache->cachetable[index1]) { + cacheentry = &pcache->cachetable[index1][index2]; + /* reject if entry is not valid */ + if (!cacheentry->valid) + cacheentry = (struct CACHED_PERMISSIONS*)NULL; + else + pcache->head.p_hits++; + if (pcache) + pcache->head.p_reads++; + } + } +#if CACHE_LEGACY_SIZE + else { + cacheentry = (struct CACHED_PERMISSIONS*)NULL; + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + struct CACHED_PERMISSIONS_LEGACY wanted; + struct CACHED_PERMISSIONS_LEGACY *legacy; + + wanted.mft_no = ni->mft_no; + wanted.variable = (void*)NULL; + wanted.varsize = 0; + legacy = (struct CACHED_PERMISSIONS_LEGACY*)ntfs_fetch_cache( + scx->vol->legacy_cache, GENERIC(&wanted), + (cache_compare)leg_compare); + if (legacy) cacheentry = &legacy->perm; + } + } +#endif +#if POSIXACLS + if (cacheentry && !cacheentry->pxdesc) { + ntfs_log_error("No Posix descriptor in cache\n"); + cacheentry = (struct CACHED_PERMISSIONS*)NULL; + } +#endif + return (cacheentry); +} + +/* + * Retrieve a security attribute from $Secure + */ + +static char *retrievesecurityattr(ntfs_volume *vol, SII_INDEX_KEY id) +{ + struct SII *psii; + union { + struct { + le32 dataoffsl; + le32 dataoffsh; + } parts; + le64 all; + } realign; + int found; + size_t size; + size_t rdsize; + s64 offs; + ntfs_inode *ni; + ntfs_index_context *xsii; + char *securattr; + + securattr = (char*)NULL; + ni = vol->secure_ni; + xsii = vol->secure_xsii; + if (ni && xsii) { + ntfs_index_ctx_reinit(xsii); + found = + !ntfs_index_lookup((char*)&id, + sizeof(SII_INDEX_KEY), xsii); + if (found) { + psii = (struct SII*)xsii->entry; + size = + (size_t) le32_to_cpu(psii->datasize) + - sizeof(SECURITY_DESCRIPTOR_HEADER); + /* work around bad alignment problem */ + realign.parts.dataoffsh = psii->dataoffsh; + realign.parts.dataoffsl = psii->dataoffsl; + offs = le64_to_cpu(realign.all) + + sizeof(SECURITY_DESCRIPTOR_HEADER); + + securattr = (char*)ntfs_malloc(size); + if (securattr) { + rdsize = ntfs_attr_data_read( + ni, STREAM_SDS, 4, + securattr, size, offs); + if ((rdsize != size) + || !ntfs_valid_descr(securattr, + rdsize)) { + /* error to be logged by caller */ + free(securattr); + securattr = (char*)NULL; + } + } + } else + if (errno != ENOENT) + ntfs_log_perror("Inconsistency in index $SII"); + } + if (!securattr) { + ntfs_log_error("Failed to retrieve a security descriptor\n"); + errno = EIO; + } + return (securattr); +} + +/* + * Get the security descriptor associated to a file + * + * Either : + * - read the security descriptor attribute (v1.x format) + * - or find the descriptor in $Secure:$SDS (v3.x format) + * + * in both case, sanity checks are done on the attribute and + * the descriptor can be assumed safe + * + * The returned descriptor is dynamically allocated and has to be freed + */ + +static char *getsecurityattr(ntfs_volume *vol, ntfs_inode *ni) +{ + SII_INDEX_KEY securid; + char *securattr; + s64 readallsz; + + /* + * Warning : in some situations, after fixing by chkdsk, + * v3_Extensions are marked present (long standard informations) + * with a default security descriptor inserted in an + * attribute + */ + if (test_nino_flag(ni, v3_Extensions) + && vol->secure_ni && ni->security_id) { + /* get v3.x descriptor in $Secure */ + securid.security_id = ni->security_id; + securattr = retrievesecurityattr(vol,securid); + if (!securattr) + ntfs_log_error("Bad security descriptor for 0x%lx\n", + (long)le32_to_cpu(ni->security_id)); + } else { + /* get v1.x security attribute */ + readallsz = 0; + securattr = ntfs_attr_readall(ni, AT_SECURITY_DESCRIPTOR, + AT_UNNAMED, 0, &readallsz); + if (securattr && !ntfs_valid_descr(securattr, readallsz)) { + ntfs_log_error("Bad security descriptor for inode %lld\n", + (long long)ni->mft_no); + free(securattr); + securattr = (char*)NULL; + } + } + if (!securattr) { + /* + * in some situations, there is no security + * descriptor, and chkdsk does not detect or fix + * anything. This could be a normal situation. + * When this happens, simulate a descriptor with + * minimum rights, so that a real descriptor can + * be created by chown or chmod + */ + ntfs_log_error("No security descriptor found for inode %lld\n", + (long long)ni->mft_no); + securattr = ntfs_build_descr(0, 0, adminsid, adminsid); + } + return (securattr); +} + +#if POSIXACLS + +/* + * Determine which access types to a file are allowed + * according to the relation of current process to the file + * + * When Posix ACLs are compiled in but not enabled in the mount + * options POSIX_ACL_USER, POSIX_ACL_GROUP and POSIX_ACL_MASK + * are ignored. + */ + +static int access_check_posix(struct SECURITY_CONTEXT *scx, + struct POSIX_SECURITY *pxdesc, mode_t request, + uid_t uid, gid_t gid) +{ + struct POSIX_ACE *pxace; + int userperms; + int groupperms; + int mask; + BOOL somegroup; + BOOL needgroups; + BOOL noacl; + mode_t perms; + int i; + + noacl = !(scx->vol->secure_flags & (1 << SECURITY_ACL)); + if (noacl) + perms = ntfs_basic_perms(scx, pxdesc); + else + perms = pxdesc->mode; + /* owner and root access */ + if (!scx->uid || (uid == scx->uid)) { + if (!scx->uid) { + /* root access if owner or other execution */ + if (perms & 0101) + perms |= 01777; + else { + /* root access if some group execution */ + groupperms = 0; + mask = 7; + for (i=pxdesc->acccnt-1; i>=0 ; i--) { + pxace = &pxdesc->acl.ace[i]; + switch (pxace->tag) { + case POSIX_ACL_USER_OBJ : + case POSIX_ACL_GROUP_OBJ : + groupperms |= pxace->perms; + break; + case POSIX_ACL_GROUP : + if (!noacl) + groupperms + |= pxace->perms; + break; + case POSIX_ACL_MASK : + if (!noacl) + mask = pxace->perms & 7; + break; + default : + break; + } + } + perms = (groupperms & mask & 1) | 6; + } + } else + perms &= 07700; + } else { + /* + * analyze designated users, get mask + * and identify whether we need to check + * the group memberships. The groups are + * not needed when all groups have the + * same permissions as other for the + * requested modes. + */ + userperms = -1; + groupperms = -1; + needgroups = FALSE; + mask = 7; + for (i=pxdesc->acccnt-1; i>=0 ; i--) { + pxace = &pxdesc->acl.ace[i]; + switch (pxace->tag) { + case POSIX_ACL_USER : + if (!noacl + && ((uid_t)pxace->id == scx->uid)) + userperms = pxace->perms; + break; + case POSIX_ACL_MASK : + if (!noacl) + mask = pxace->perms & 7; + break; + case POSIX_ACL_GROUP_OBJ : + if (((pxace->perms & mask) ^ perms) + & (request >> 6) & 7) + needgroups = TRUE; + break; + case POSIX_ACL_GROUP : + if (!noacl + && (((pxace->perms & mask) ^ perms) + & (request >> 6) & 7)) + needgroups = TRUE; + break; + default : + break; + } + } + /* designated users */ + if (userperms >= 0) + perms = (perms & 07000) + (userperms & mask); + else if (!needgroups) + perms &= 07007; + else { + /* owning group */ + if (!(~(perms >> 3) & request & mask) + && ((gid == scx->gid) + || groupmember(scx, scx->uid, gid))) + perms &= 07070; + else if (!noacl) { + /* other groups */ + groupperms = -1; + somegroup = FALSE; + for (i=pxdesc->acccnt-1; i>=0 ; i--) { + pxace = &pxdesc->acl.ace[i]; + if ((pxace->tag == POSIX_ACL_GROUP) + && groupmember(scx, scx->uid, pxace->id)) { + if (!(~pxace->perms & request & mask)) + groupperms = pxace->perms; + somegroup = TRUE; + } + } + if (groupperms >= 0) + perms = (perms & 07000) + (groupperms & mask); + else + if (somegroup) + perms = 0; + else + perms &= 07007; + } else + perms &= 07007; + } + } + return (perms); +} + +/* + * Get permissions to access a file + * Takes into account the relation of user to file (owner, group, ...) + * Do no use as mode of the file + * Do no call if default_permissions is set + * + * returns -1 if there is a problem + */ + +static int ntfs_get_perm(struct SECURITY_CONTEXT *scx, + ntfs_inode * ni, mode_t request) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const struct CACHED_PERMISSIONS *cached; + char *securattr; + const SID *usid; /* owner of file/directory */ + const SID *gsid; /* group of file/directory */ + uid_t uid; + gid_t gid; + int perm; + BOOL isdir; + struct POSIX_SECURITY *pxdesc; + + if (!scx->mapping[MAPUSERS]) + perm = 07777; + else { + /* check whether available in cache */ + cached = fetch_cache(scx,ni); + if (cached) { + uid = cached->uid; + gid = cached->gid; + perm = access_check_posix(scx,cached->pxdesc,request,uid,gid); + } else { + perm = 0; /* default to no permission */ + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + != const_cpu_to_le16(0); + securattr = getsecurityattr(scx->vol, ni); + if (securattr) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*) + securattr; + gsid = (const SID*)& + securattr[le32_to_cpu(phead->group)]; + gid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); +#if OWNERFROMACL + usid = ntfs_acl_owner(securattr); + pxdesc = ntfs_build_permissions_posix(scx->mapping,securattr, + usid, gsid, isdir); + if (pxdesc) + perm = pxdesc->mode & 07777; + else + perm = -1; + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#else + usid = (const SID*)& + securattr[le32_to_cpu(phead->owner)]; + pxdesc = ntfs_build_permissions_posix(scx,securattr, + usid, gsid, isdir); + if (pxdesc) + perm = pxdesc->mode & 07777; + else + perm = -1; + if (!perm && ntfs_same_sid(usid, adminsid)) { + uid = find_tenant(scx, securattr); + if (uid) + perm = 0700; + } else + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#endif + /* + * Create a security id if there were none + * and upgrade option is selected + */ + if (!test_nino_flag(ni, v3_Extensions) + && (perm >= 0) + && (scx->vol->secure_flags + & (1 << SECURITY_ADDSECURIDS))) { + upgrade_secur_desc(scx->vol, + securattr, ni); + /* + * fetch owner and group for cacheing + * if there is a securid + */ + } + if (test_nino_flag(ni, v3_Extensions) + && (perm >= 0)) { + enter_cache(scx, ni, uid, + gid, pxdesc); + } + if (pxdesc) { + perm = access_check_posix(scx,pxdesc,request,uid,gid); + free(pxdesc); + } + free(securattr); + } else { + perm = -1; + uid = gid = 0; + } + } + } + return (perm); +} + +/* + * Get a Posix ACL + * + * returns size or -errno if there is a problem + * if size was too small, no copy is done and errno is not set, + * the caller is expected to issue a new call + */ + +int ntfs_get_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + const char *name, char *value, size_t size) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + struct POSIX_SECURITY *pxdesc; + const struct CACHED_PERMISSIONS *cached; + char *securattr; + const SID *usid; /* owner of file/directory */ + const SID *gsid; /* group of file/directory */ + uid_t uid; + gid_t gid; + BOOL isdir; + size_t outsize; + + outsize = 0; /* default to error */ + if (!scx->mapping[MAPUSERS]) + errno = ENOTSUP; + else { + /* check whether available in cache */ + cached = fetch_cache(scx,ni); + if (cached) + pxdesc = cached->pxdesc; + else { + securattr = getsecurityattr(scx->vol, ni); + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + != const_cpu_to_le16(0); + if (securattr) { + phead = + (const SECURITY_DESCRIPTOR_RELATIVE*) + securattr; + gsid = (const SID*)& + securattr[le32_to_cpu(phead->group)]; +#if OWNERFROMACL + usid = ntfs_acl_owner(securattr); +#else + usid = (const SID*)& + securattr[le32_to_cpu(phead->owner)]; +#endif + pxdesc = ntfs_build_permissions_posix(scx->mapping,securattr, + usid, gsid, isdir); + + /* + * fetch owner and group for cacheing + */ + if (pxdesc) { + /* + * Create a security id if there were none + * and upgrade option is selected + */ + if (!test_nino_flag(ni, v3_Extensions) + && (scx->vol->secure_flags + & (1 << SECURITY_ADDSECURIDS))) { + upgrade_secur_desc(scx->vol, + securattr, ni); + } +#if OWNERFROMACL + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#else + if (!(pxdesc->mode & 07777) + && ntfs_same_sid(usid, adminsid)) { + uid = find_tenant(scx, + securattr); + } else + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#endif + gid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); + if (pxdesc->tagsset & POSIX_ACL_EXTENSIONS) + enter_cache(scx, ni, uid, + gid, pxdesc); + } + free(securattr); + } else + pxdesc = (struct POSIX_SECURITY*)NULL; + } + + if (pxdesc) { + if (ntfs_valid_posix(pxdesc)) { + if (!strcmp(name,"system.posix_acl_default")) { + if (ni->mrec->flags + & MFT_RECORD_IS_DIRECTORY) + outsize = sizeof(struct POSIX_ACL) + + pxdesc->defcnt*sizeof(struct POSIX_ACE); + else { + /* + * getting default ACL from plain file : + * return EACCES if size > 0 as + * indicated in the man, but return ok + * if size == 0, so that ls does not + * display an error + */ + if (size > 0) { + outsize = 0; + errno = EACCES; + } else + outsize = sizeof(struct POSIX_ACL); + } + if (outsize && (outsize <= size)) { + memcpy(value,&pxdesc->acl,sizeof(struct POSIX_ACL)); + memcpy(&value[sizeof(struct POSIX_ACL)], + &pxdesc->acl.ace[pxdesc->firstdef], + outsize-sizeof(struct POSIX_ACL)); + } + } else { + outsize = sizeof(struct POSIX_ACL) + + pxdesc->acccnt*sizeof(struct POSIX_ACE); + if (outsize <= size) + memcpy(value,&pxdesc->acl,outsize); + } + } else { + outsize = 0; + errno = EIO; + ntfs_log_error("Invalid Posix ACL built\n"); + } + if (!cached) + free(pxdesc); + } else + outsize = 0; + } + return (outsize ? (int)outsize : -errno); +} + +#else /* POSIXACLS */ + + +/* + * Get permissions to access a file + * Takes into account the relation of user to file (owner, group, ...) + * Do no use as mode of the file + * + * returns -1 if there is a problem + */ + +static int ntfs_get_perm(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, mode_t request) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const struct CACHED_PERMISSIONS *cached; + char *securattr; + const SID *usid; /* owner of file/directory */ + const SID *gsid; /* group of file/directory */ + BOOL isdir; + uid_t uid; + gid_t gid; + int perm; + + if (!scx->mapping[MAPUSERS] || (!scx->uid && !(request & S_IEXEC))) + perm = 07777; + else { + /* check whether available in cache */ + cached = fetch_cache(scx,ni); + if (cached) { + perm = cached->mode; + uid = cached->uid; + gid = cached->gid; + } else { + perm = 0; /* default to no permission */ + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + != const_cpu_to_le16(0); + securattr = getsecurityattr(scx->vol, ni); + if (securattr) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*) + securattr; + gsid = (const SID*)& + securattr[le32_to_cpu(phead->group)]; + gid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); +#if OWNERFROMACL + usid = ntfs_acl_owner(securattr); + perm = ntfs_build_permissions(securattr, + usid, gsid, isdir); + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#else + usid = (const SID*)& + securattr[le32_to_cpu(phead->owner)]; + perm = ntfs_build_permissions(securattr, + usid, gsid, isdir); + if (!perm && ntfs_same_sid(usid, adminsid)) { + uid = find_tenant(scx, securattr); + if (uid) + perm = 0700; + } else + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#endif + /* + * Create a security id if there were none + * and upgrade option is selected + */ + if (!test_nino_flag(ni, v3_Extensions) + && (perm >= 0) + && (scx->vol->secure_flags + & (1 << SECURITY_ADDSECURIDS))) { + upgrade_secur_desc(scx->vol, + securattr, ni); + /* + * fetch owner and group for cacheing + * if there is a securid + */ + } + if (test_nino_flag(ni, v3_Extensions) + && (perm >= 0)) { + enter_cache(scx, ni, uid, + gid, perm); + } + free(securattr); + } else { + perm = -1; + uid = gid = 0; + } + } + if (perm >= 0) { + if (!scx->uid) { + /* root access and execution */ + if (perm & 0111) + perm |= 01777; + else + perm = 0; + } else + if (uid == scx->uid) + perm &= 07700; + else + /* + * avoid checking group membership + * when the requested perms for group + * are the same as perms for other + */ + if ((gid == scx->gid) + || ((((perm >> 3) ^ perm) + & (request >> 6) & 7) + && groupmember(scx, scx->uid, gid))) + perm &= 07070; + else + perm &= 07007; + } + } + return (perm); +} + +#endif /* POSIXACLS */ + +/* + * Get an NTFS ACL + * + * Returns size or -errno if there is a problem + * if size was too small, no copy is done and errno is not set, + * the caller is expected to issue a new call + */ + +int ntfs_get_ntfs_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + char *value, size_t size) +{ + char *securattr; + size_t outsize; + + outsize = 0; /* default to no data and no error */ + securattr = getsecurityattr(scx->vol, ni); + if (securattr) { + outsize = ntfs_attr_size(securattr); + if (outsize <= size) { + memcpy(value,securattr,outsize); + } + free(securattr); + } + return (outsize ? (int)outsize : -errno); +} + +/* + * Get owner, group and permissions in an stat structure + * returns permissions, or -1 if there is a problem + */ + +int ntfs_get_owner_mode(struct SECURITY_CONTEXT *scx, + ntfs_inode * ni, struct stat *stbuf) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + char *securattr; + const SID *usid; /* owner of file/directory */ + const SID *gsid; /* group of file/directory */ + const struct CACHED_PERMISSIONS *cached; + int perm; + BOOL isdir; +#if POSIXACLS + struct POSIX_SECURITY *pxdesc; +#endif + + if (!scx->mapping[MAPUSERS]) + perm = 07777; + else { + /* check whether available in cache */ + cached = fetch_cache(scx,ni); + if (cached) { +#if POSIXACLS + if (!(scx->vol->secure_flags & (1 << SECURITY_ACL)) + && cached->pxdesc) + perm = ntfs_basic_perms(scx,cached->pxdesc); + else +#endif + perm = cached->mode; + stbuf->st_uid = cached->uid; + stbuf->st_gid = cached->gid; + stbuf->st_mode = (stbuf->st_mode & ~07777) + perm; + } else { + perm = -1; /* default to error */ + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + != const_cpu_to_le16(0); + securattr = getsecurityattr(scx->vol, ni); + if (securattr) { + phead = + (const SECURITY_DESCRIPTOR_RELATIVE*) + securattr; + gsid = (const SID*)& + securattr[le32_to_cpu(phead->group)]; +#if OWNERFROMACL + usid = ntfs_acl_owner(securattr); +#else + usid = (const SID*)& + securattr[le32_to_cpu(phead->owner)]; +#endif +#if POSIXACLS + pxdesc = ntfs_build_permissions_posix( + scx->mapping, securattr, + usid, gsid, isdir); + if (pxdesc) { + if (!(scx->vol->secure_flags + & (1 << SECURITY_ACL))) + perm = ntfs_basic_perms(scx, + pxdesc); + else + perm = pxdesc->mode & 07777; + } else + perm = -1; +#else + perm = ntfs_build_permissions(securattr, + usid, gsid, isdir); +#endif + /* + * fetch owner and group for cacheing + */ + if (perm >= 0) { + /* + * Create a security id if there were none + * and upgrade option is selected + */ + if (!test_nino_flag(ni, v3_Extensions) + && (scx->vol->secure_flags + & (1 << SECURITY_ADDSECURIDS))) { + upgrade_secur_desc(scx->vol, + securattr, ni); + } +#if OWNERFROMACL + stbuf->st_uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#else + if (!perm && ntfs_same_sid(usid, adminsid)) { + stbuf->st_uid = + find_tenant(scx, + securattr); + if (stbuf->st_uid) + perm = 0700; + } else + stbuf->st_uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#endif + stbuf->st_gid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); + stbuf->st_mode = + (stbuf->st_mode & ~07777) + perm; +#if POSIXACLS + enter_cache(scx, ni, stbuf->st_uid, + stbuf->st_gid, pxdesc); + free(pxdesc); +#else + enter_cache(scx, ni, stbuf->st_uid, + stbuf->st_gid, perm); +#endif + } + free(securattr); + } + } + } + return (perm); +} + +#if POSIXACLS + +/* + * Get the base for a Posix inheritance and + * build an inherited Posix descriptor + */ + +static struct POSIX_SECURITY *inherit_posix(struct SECURITY_CONTEXT *scx, + ntfs_inode *dir_ni, mode_t mode, BOOL isdir) +{ + const struct CACHED_PERMISSIONS *cached; + const SECURITY_DESCRIPTOR_RELATIVE *phead; + struct POSIX_SECURITY *pxdesc; + struct POSIX_SECURITY *pydesc; + char *securattr; + const SID *usid; + const SID *gsid; + uid_t uid; + gid_t gid; + + pydesc = (struct POSIX_SECURITY*)NULL; + /* check whether parent directory is available in cache */ + cached = fetch_cache(scx,dir_ni); + if (cached) { + uid = cached->uid; + gid = cached->gid; + pxdesc = cached->pxdesc; + if (pxdesc) { + if (scx->vol->secure_flags & (1 << SECURITY_ACL)) + pydesc = ntfs_build_inherited_posix(pxdesc, + mode, scx->umask, isdir); + else + pydesc = ntfs_build_basic_posix(pxdesc, + mode, scx->umask, isdir); + } + } else { + securattr = getsecurityattr(scx->vol, dir_ni); + if (securattr) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*) + securattr; + gsid = (const SID*)& + securattr[le32_to_cpu(phead->group)]; + gid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); +#if OWNERFROMACL + usid = ntfs_acl_owner(securattr); + pxdesc = ntfs_build_permissions_posix(scx->mapping,securattr, + usid, gsid, TRUE); + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#else + usid = (const SID*)& + securattr[le32_to_cpu(phead->owner)]; + pxdesc = ntfs_build_permissions_posix(scx->mapping,securattr, + usid, gsid, TRUE); + if (pxdesc && ntfs_same_sid(usid, adminsid)) { + uid = find_tenant(scx, securattr); + } else + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); +#endif + if (pxdesc) { + /* + * Create a security id if there were none + * and upgrade option is selected + */ + if (!test_nino_flag(dir_ni, v3_Extensions) + && (scx->vol->secure_flags + & (1 << SECURITY_ADDSECURIDS))) { + upgrade_secur_desc(scx->vol, + securattr, dir_ni); + /* + * fetch owner and group for cacheing + * if there is a securid + */ + } + if (test_nino_flag(dir_ni, v3_Extensions)) { + enter_cache(scx, dir_ni, uid, + gid, pxdesc); + } + if (scx->vol->secure_flags + & (1 << SECURITY_ACL)) + pydesc = ntfs_build_inherited_posix( + pxdesc, mode, + scx->umask, isdir); + else + pydesc = ntfs_build_basic_posix( + pxdesc, mode, + scx->umask, isdir); + free(pxdesc); + } + free(securattr); + } + } + return (pydesc); +} + +/* + * Allocate a security_id for a file being created + * + * Returns zero if not possible (NTFS v3.x required) + */ + +le32 ntfs_alloc_securid(struct SECURITY_CONTEXT *scx, + uid_t uid, gid_t gid, ntfs_inode *dir_ni, + mode_t mode, BOOL isdir) +{ +#if !FORCE_FORMAT_v1x + const struct CACHED_SECURID *cached; + struct CACHED_SECURID wanted; + struct POSIX_SECURITY *pxdesc; + char *newattr; + int newattrsz; + const SID *usid; + const SID *gsid; + BIGSID defusid; + BIGSID defgsid; + le32 securid; +#endif + + securid = const_cpu_to_le32(0); + +#if !FORCE_FORMAT_v1x + + pxdesc = inherit_posix(scx, dir_ni, mode, isdir); + if (pxdesc) { + /* check whether target securid is known in cache */ + + wanted.uid = uid; + wanted.gid = gid; + wanted.dmode = pxdesc->mode & mode & 07777; + if (isdir) wanted.dmode |= 0x10000; + wanted.variable = (void*)pxdesc; + wanted.varsize = sizeof(struct POSIX_SECURITY) + + (pxdesc->acccnt + pxdesc->defcnt)*sizeof(struct POSIX_ACE); + cached = (const struct CACHED_SECURID*)ntfs_fetch_cache( + scx->vol->securid_cache, GENERIC(&wanted), + (cache_compare)compare); + /* quite simple, if we are lucky */ + if (cached) + securid = cached->securid; + + /* not in cache : make sure we can create ids */ + + if (!cached && (scx->vol->major_ver >= 3)) { + usid = ntfs_find_usid(scx->mapping[MAPUSERS],uid,(SID*)&defusid); + gsid = ntfs_find_gsid(scx->mapping[MAPGROUPS],gid,(SID*)&defgsid); + if (!usid || !gsid) { + ntfs_log_error("File created by an unmapped user/group %d/%d\n", + (int)uid, (int)gid); + usid = gsid = adminsid; + } + newattr = ntfs_build_descr_posix(scx->mapping, pxdesc, + isdir, usid, gsid); + if (newattr) { + newattrsz = ntfs_attr_size(newattr); + securid = setsecurityattr(scx->vol, + (const SECURITY_DESCRIPTOR_RELATIVE*)newattr, + newattrsz); + if (securid) { + /* update cache, for subsequent use */ + wanted.securid = securid; + ntfs_enter_cache(scx->vol->securid_cache, + GENERIC(&wanted), + (cache_compare)compare); + } + free(newattr); + } else { + /* + * could not build new security attribute + * errno set by ntfs_build_descr() + */ + } + } + free(pxdesc); + } +#endif + return (securid); +} + +/* + * Apply Posix inheritance to a newly created file + * (for NTFS 1.x only : no securid) + */ + +int ntfs_set_inherited_posix(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, uid_t uid, gid_t gid, + ntfs_inode *dir_ni, mode_t mode) +{ + struct POSIX_SECURITY *pxdesc; + char *newattr; + const SID *usid; + const SID *gsid; + BIGSID defusid; + BIGSID defgsid; + BOOL isdir; + int res; + + res = -1; + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) != const_cpu_to_le16(0); + pxdesc = inherit_posix(scx, dir_ni, mode, isdir); + if (pxdesc) { + usid = ntfs_find_usid(scx->mapping[MAPUSERS],uid,(SID*)&defusid); + gsid = ntfs_find_gsid(scx->mapping[MAPGROUPS],gid,(SID*)&defgsid); + if (!usid || !gsid) { + ntfs_log_error("File created by an unmapped user/group %d/%d\n", + (int)uid, (int)gid); + usid = gsid = adminsid; + } + newattr = ntfs_build_descr_posix(scx->mapping, pxdesc, + isdir, usid, gsid); + if (newattr) { + /* Adjust Windows read-only flag */ + res = update_secur_descr(scx->vol, newattr, ni); + if (!res && !isdir) { + if (mode & S_IWUSR) + ni->flags &= ~FILE_ATTR_READONLY; + else + ni->flags |= FILE_ATTR_READONLY; + } +#if CACHE_LEGACY_SIZE + /* also invalidate legacy cache */ + if (isdir && !ni->security_id) { + struct CACHED_PERMISSIONS_LEGACY legacy; + + legacy.mft_no = ni->mft_no; + legacy.variable = pxdesc; + legacy.varsize = sizeof(struct POSIX_SECURITY) + + (pxdesc->acccnt + pxdesc->defcnt)*sizeof(struct POSIX_ACE); + ntfs_invalidate_cache(scx->vol->legacy_cache, + GENERIC(&legacy), + (cache_compare)leg_compare,0); + } +#endif + free(newattr); + + } else { + /* + * could not build new security attribute + * errno set by ntfs_build_descr() + */ + } + } + return (res); +} + +#else + +le32 ntfs_alloc_securid(struct SECURITY_CONTEXT *scx, + uid_t uid, gid_t gid, mode_t mode, BOOL isdir) +{ +#if !FORCE_FORMAT_v1x + const struct CACHED_SECURID *cached; + struct CACHED_SECURID wanted; + char *newattr; + int newattrsz; + const SID *usid; + const SID *gsid; + BIGSID defusid; + BIGSID defgsid; + le32 securid; +#endif + + securid = const_cpu_to_le32(0); + +#if !FORCE_FORMAT_v1x + /* check whether target securid is known in cache */ + + wanted.uid = uid; + wanted.gid = gid; + wanted.dmode = mode & 07777; + if (isdir) wanted.dmode |= 0x10000; + wanted.variable = (void*)NULL; + wanted.varsize = 0; + cached = (const struct CACHED_SECURID*)ntfs_fetch_cache( + scx->vol->securid_cache, GENERIC(&wanted), + (cache_compare)compare); + /* quite simple, if we are lucky */ + if (cached) + securid = cached->securid; + + /* not in cache : make sure we can create ids */ + + if (!cached && (scx->vol->major_ver >= 3)) { + usid = ntfs_find_usid(scx->mapping[MAPUSERS],uid,(SID*)&defusid); + gsid = ntfs_find_gsid(scx->mapping[MAPGROUPS],gid,(SID*)&defgsid); + if (!usid || !gsid) { + ntfs_log_error("File created by an unmapped user/group %d/%d\n", + (int)uid, (int)gid); + usid = gsid = adminsid; + } + newattr = ntfs_build_descr(mode, isdir, usid, gsid); + if (newattr) { + newattrsz = ntfs_attr_size(newattr); + securid = setsecurityattr(scx->vol, + (const SECURITY_DESCRIPTOR_RELATIVE*)newattr, + newattrsz); + if (securid) { + /* update cache, for subsequent use */ + wanted.securid = securid; + ntfs_enter_cache(scx->vol->securid_cache, + GENERIC(&wanted), + (cache_compare)compare); + } + free(newattr); + } else { + /* + * could not build new security attribute + * errno set by ntfs_build_descr() + */ + } + } +#endif + return (securid); +} + +#endif + +/* + * Update ownership and mode of a file, reusing an existing + * security descriptor when possible + * + * Returns zero if successful + */ + +#if POSIXACLS +int ntfs_set_owner_mode(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + uid_t uid, gid_t gid, mode_t mode, + struct POSIX_SECURITY *pxdesc) +#else +int ntfs_set_owner_mode(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + uid_t uid, gid_t gid, mode_t mode) +#endif +{ + int res; + const struct CACHED_SECURID *cached; + struct CACHED_SECURID wanted; + char *newattr; + const SID *usid; + const SID *gsid; + BIGSID defusid; + BIGSID defgsid; + BOOL isdir; + + res = 0; + + /* check whether target securid is known in cache */ + + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) != const_cpu_to_le16(0); + wanted.uid = uid; + wanted.gid = gid; + wanted.dmode = mode & 07777; + if (isdir) wanted.dmode |= 0x10000; +#if POSIXACLS + wanted.variable = (void*)pxdesc; + if (pxdesc) + wanted.varsize = sizeof(struct POSIX_SECURITY) + + (pxdesc->acccnt + pxdesc->defcnt)*sizeof(struct POSIX_ACE); + else + wanted.varsize = 0; +#else + wanted.variable = (void*)NULL; + wanted.varsize = 0; +#endif + if (test_nino_flag(ni, v3_Extensions)) { + cached = (const struct CACHED_SECURID*)ntfs_fetch_cache( + scx->vol->securid_cache, GENERIC(&wanted), + (cache_compare)compare); + /* quite simple, if we are lucky */ + if (cached) { + ni->security_id = cached->securid; + NInoSetDirty(ni); + /* adjust Windows read-only flag */ + if (!isdir) { + if (mode & S_IWUSR) + ni->flags &= ~FILE_ATTR_READONLY; + else + ni->flags |= FILE_ATTR_READONLY; + NInoFileNameSetDirty(ni); + } + } + } else cached = (struct CACHED_SECURID*)NULL; + + if (!cached) { + /* + * Do not use usid and gsid from former attributes, + * but recompute them to get repeatable results + * which can be kept in cache. + */ + usid = ntfs_find_usid(scx->mapping[MAPUSERS],uid,(SID*)&defusid); + gsid = ntfs_find_gsid(scx->mapping[MAPGROUPS],gid,(SID*)&defgsid); + if (!usid || !gsid) { + ntfs_log_error("File made owned by an unmapped user/group %d/%d\n", + uid, gid); + usid = gsid = adminsid; + } +#if POSIXACLS + if (pxdesc) + newattr = ntfs_build_descr_posix(scx->mapping, pxdesc, + isdir, usid, gsid); + else + newattr = ntfs_build_descr(mode, + isdir, usid, gsid); +#else + newattr = ntfs_build_descr(mode, + isdir, usid, gsid); +#endif + if (newattr) { + res = update_secur_descr(scx->vol, newattr, ni); + if (!res) { + /* adjust Windows read-only flag */ + if (!isdir) { + if (mode & S_IWUSR) + ni->flags &= ~FILE_ATTR_READONLY; + else + ni->flags |= FILE_ATTR_READONLY; + NInoFileNameSetDirty(ni); + } + /* update cache, for subsequent use */ + if (test_nino_flag(ni, v3_Extensions)) { + wanted.securid = ni->security_id; + ntfs_enter_cache(scx->vol->securid_cache, + GENERIC(&wanted), + (cache_compare)compare); + } +#if CACHE_LEGACY_SIZE + /* also invalidate legacy cache */ + if (isdir && !ni->security_id) { + struct CACHED_PERMISSIONS_LEGACY legacy; + + legacy.mft_no = ni->mft_no; +#if POSIXACLS + legacy.variable = wanted.variable; + legacy.varsize = wanted.varsize; +#else + legacy.variable = (void*)NULL; + legacy.varsize = 0; +#endif + ntfs_invalidate_cache(scx->vol->legacy_cache, + GENERIC(&legacy), + (cache_compare)leg_compare,0); + } +#endif + } + free(newattr); + } else { + /* + * could not build new security attribute + * errno set by ntfs_build_descr() + */ + res = -1; + } + } + return (res); +} + +/* + * Check whether user has ownership rights on a file + * + * Returns TRUE if allowed + * if not, errno tells why + */ + +BOOL ntfs_allowed_as_owner(struct SECURITY_CONTEXT *scx, ntfs_inode *ni) +{ + const struct CACHED_PERMISSIONS *cached; + char *oldattr; + const SID *usid; + uid_t processuid; + uid_t uid; + BOOL gotowner; + int allowed; + + processuid = scx->uid; +/* TODO : use CAP_FOWNER process capability */ + /* + * Always allow for root + * Also always allow if no mapping has been defined + */ + if (!scx->mapping[MAPUSERS] || !processuid) + allowed = TRUE; + else { + gotowner = FALSE; /* default */ + /* get the owner, either from cache or from old attribute */ + cached = fetch_cache(scx, ni); + if (cached) { + uid = cached->uid; + gotowner = TRUE; + } else { + oldattr = getsecurityattr(scx->vol, ni); + if (oldattr) { +#if OWNERFROMACL + usid = ntfs_acl_owner(oldattr); +#else + const SECURITY_DESCRIPTOR_RELATIVE *phead; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*) + oldattr; + usid = (const SID*)&oldattr + [le32_to_cpu(phead->owner)]; +#endif + uid = ntfs_find_user(scx->mapping[MAPUSERS], + usid); + gotowner = TRUE; + free(oldattr); + } + } +/* TODO : use CAP_FOWNER process capability */ + if (gotowner + && (!processuid || (processuid == uid))) + allowed = TRUE; + else { + allowed = FALSE; + errno = EPERM; + } + } + return (allowed); +} + + +#if POSIXACLS + +/* + * Set a new access or default Posix ACL to a file + * (or remove ACL if no input data) + * Validity of input data is checked after merging + * + * Returns 0, or -1 if there is a problem which errno describes + */ + +int ntfs_set_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + const char *name, const char *value, size_t size, + int flags) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const struct CACHED_PERMISSIONS *cached; + char *oldattr; + uid_t processuid; + const SID *usid; + const SID *gsid; + uid_t uid; + uid_t gid; + int res; + BOOL isdir; + BOOL deflt; + BOOL exist; + int count; + struct POSIX_SECURITY *oldpxdesc; + struct POSIX_SECURITY *newpxdesc; + + /* get the current pxsec, either from cache or from old attribute */ + res = -1; + deflt = !strcmp(name,"system.posix_acl_default"); + if (size) + count = (size - sizeof(struct POSIX_ACL)) / sizeof(struct POSIX_ACE); + else + count = 0; + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) != const_cpu_to_le16(0); + newpxdesc = (struct POSIX_SECURITY*)NULL; + if ((!value + || (((const struct POSIX_ACL*)value)->version == POSIX_VERSION)) + && (!deflt || isdir || (!size && !value))) { + cached = fetch_cache(scx, ni); + if (cached) { + uid = cached->uid; + gid = cached->gid; + oldpxdesc = cached->pxdesc; + if (oldpxdesc) { + newpxdesc = ntfs_replace_acl(oldpxdesc, + (const struct POSIX_ACL*)value,count,deflt); + } + } else { + oldattr = getsecurityattr(scx->vol, ni); + if (oldattr) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)oldattr; +#if OWNERFROMACL + usid = ntfs_acl_owner(oldattr); +#else + usid = (const SID*)&oldattr[le32_to_cpu(phead->owner)]; +#endif + gsid = (const SID*)&oldattr[le32_to_cpu(phead->group)]; + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); + gid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); + oldpxdesc = ntfs_build_permissions_posix(scx->mapping, + oldattr, usid, gsid, isdir); + if (oldpxdesc) { + if (deflt) + exist = oldpxdesc->defcnt > 0; + else + exist = oldpxdesc->acccnt > 3; + if ((exist && (flags & XATTR_CREATE)) + || (!exist && (flags & XATTR_REPLACE))) { + errno = (exist ? EEXIST : ENODATA); + } else { + newpxdesc = ntfs_replace_acl(oldpxdesc, + (const struct POSIX_ACL*)value,count,deflt); + } + free(oldpxdesc); + } + free(oldattr); + } + } + } else + errno = EINVAL; + + if (newpxdesc) { + processuid = scx->uid; +/* TODO : use CAP_FOWNER process capability */ + if (!processuid || (uid == processuid)) { + /* + * clear setgid if file group does + * not match process group + */ + if (processuid && (gid != scx->gid) + && !groupmember(scx, scx->uid, gid)) { + newpxdesc->mode &= ~S_ISGID; + } + res = ntfs_set_owner_mode(scx, ni, uid, gid, + newpxdesc->mode, newpxdesc); + } else + errno = EPERM; + free(newpxdesc); + } + return (res ? -1 : 0); +} + +/* + * Remove a default Posix ACL from a file + * + * Returns 0, or -1 if there is a problem which errno describes + */ + +int ntfs_remove_posix_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + const char *name) +{ + return (ntfs_set_posix_acl(scx, ni, name, + (const char*)NULL, 0, 0)); +} + +#endif + +/* + * Set a new NTFS ACL to a file + * + * Returns 0, or -1 if there is a problem + */ + +int ntfs_set_ntfs_acl(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + const char *value, size_t size, int flags) +{ + char *attr; + int res; + + res = -1; + if ((size > 0) + && !(flags & XATTR_CREATE) + && ntfs_valid_descr(value,size) + && (ntfs_attr_size(value) == size)) { + /* need copying in order to write */ + attr = (char*)ntfs_malloc(size); + if (attr) { + memcpy(attr,value,size); + res = update_secur_descr(scx->vol, attr, ni); + /* + * No need to invalidate standard caches : + * the relation between a securid and + * the associated protection is unchanged, + * only the relation between a file and + * its securid and protection is changed. + */ +#if CACHE_LEGACY_SIZE + /* + * we must however invalidate the legacy + * cache, which is based on inode numbers. + * For safety, invalidate even if updating + * failed. + */ + if ((ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + && !ni->security_id) { + struct CACHED_PERMISSIONS_LEGACY legacy; + + legacy.mft_no = ni->mft_no; + legacy.variable = (char*)NULL; + legacy.varsize = 0; + ntfs_invalidate_cache(scx->vol->legacy_cache, + GENERIC(&legacy), + (cache_compare)leg_compare,0); + } +#endif + free(attr); + } else + errno = ENOMEM; + } else + errno = EINVAL; + return (res ? -1 : 0); +} + + +/* + * Set new permissions to a file + * Checks user mapping has been defined before request for setting + * + * rejected if request is not originated by owner or root + * + * returns 0 on success + * -1 on failure, with errno = EIO + */ + +int ntfs_set_mode(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, mode_t mode) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const struct CACHED_PERMISSIONS *cached; + char *oldattr; + const SID *usid; + const SID *gsid; + uid_t processuid; + uid_t uid; + uid_t gid; + int res; +#if POSIXACLS + BOOL isdir; + int pxsize; + const struct POSIX_SECURITY *oldpxdesc; + struct POSIX_SECURITY *newpxdesc = (struct POSIX_SECURITY*)NULL; +#endif + + /* get the current owner, either from cache or from old attribute */ + res = 0; + cached = fetch_cache(scx, ni); + if (cached) { + uid = cached->uid; + gid = cached->gid; +#if POSIXACLS + oldpxdesc = cached->pxdesc; + if (oldpxdesc) { + /* must copy before merging */ + pxsize = sizeof(struct POSIX_SECURITY) + + (oldpxdesc->acccnt + oldpxdesc->defcnt)*sizeof(struct POSIX_ACE); + newpxdesc = (struct POSIX_SECURITY*)malloc(pxsize); + if (newpxdesc) { + memcpy(newpxdesc, oldpxdesc, pxsize); + if (ntfs_merge_mode_posix(newpxdesc, mode)) + res = -1; + } else + res = -1; + } else + newpxdesc = (struct POSIX_SECURITY*)NULL; +#endif + } else { + oldattr = getsecurityattr(scx->vol, ni); + if (oldattr) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)oldattr; +#if OWNERFROMACL + usid = ntfs_acl_owner(oldattr); +#else + usid = (const SID*)&oldattr[le32_to_cpu(phead->owner)]; +#endif + gsid = (const SID*)&oldattr[le32_to_cpu(phead->group)]; + uid = ntfs_find_user(scx->mapping[MAPUSERS],usid); + gid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); +#if POSIXACLS + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) != const_cpu_to_le16(0); + newpxdesc = ntfs_build_permissions_posix(scx->mapping, + oldattr, usid, gsid, isdir); + if (!newpxdesc || ntfs_merge_mode_posix(newpxdesc, mode)) + res = -1; +#endif + free(oldattr); + } else + res = -1; + } + + if (!res) { + processuid = scx->uid; +/* TODO : use CAP_FOWNER process capability */ + if (!processuid || (uid == processuid)) { + /* + * clear setgid if file group does + * not match process group + */ + if (processuid && (gid != scx->gid) + && !groupmember(scx, scx->uid, gid)) + mode &= ~S_ISGID; +#if POSIXACLS + if (newpxdesc) { + newpxdesc->mode = mode; + res = ntfs_set_owner_mode(scx, ni, uid, gid, + mode, newpxdesc); + } else + res = ntfs_set_owner_mode(scx, ni, uid, gid, + mode, newpxdesc); +#else + res = ntfs_set_owner_mode(scx, ni, uid, gid, mode); +#endif + } else { + errno = EPERM; + res = -1; /* neither owner nor root */ + } + } else { + /* + * Should not happen : a default descriptor is generated + * by getsecurityattr() when there are none + */ + ntfs_log_error("File has no security descriptor\n"); + res = -1; + errno = EIO; + } +#if POSIXACLS + if (newpxdesc) free(newpxdesc); +#endif + return (res ? -1 : 0); +} + +/* + * Create a default security descriptor for files whose descriptor + * cannot be inherited + */ + +int ntfs_sd_add_everyone(ntfs_inode *ni) +{ + /* JPA SECURITY_DESCRIPTOR_ATTR *sd; */ + SECURITY_DESCRIPTOR_RELATIVE *sd; + ACL *acl; + ACCESS_ALLOWED_ACE *ace; + SID *sid; + int ret, sd_len; + + /* Create SECURITY_DESCRIPTOR attribute (everyone has full access). */ + /* + * Calculate security descriptor length. We have 2 sub-authorities in + * owner and group SIDs, but structure SID contain only one, so add + * 4 bytes to every SID. + */ + sd_len = sizeof(SECURITY_DESCRIPTOR_ATTR) + 2 * (sizeof(SID) + 4) + + sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE); + sd = (SECURITY_DESCRIPTOR_RELATIVE*)ntfs_calloc(sd_len); + if (!sd) + return -1; + + sd->revision = SECURITY_DESCRIPTOR_REVISION; + sd->control = SE_DACL_PRESENT | SE_SELF_RELATIVE; + + sid = (SID*)((u8*)sd + sizeof(SECURITY_DESCRIPTOR_ATTR)); + sid->revision = SID_REVISION; + sid->sub_authority_count = 2; + sid->sub_authority[0] = const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + sid->sub_authority[1] = const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + sid->identifier_authority.value[5] = 5; + sd->owner = cpu_to_le32((u8*)sid - (u8*)sd); + + sid = (SID*)((u8*)sid + sizeof(SID) + 4); + sid->revision = SID_REVISION; + sid->sub_authority_count = 2; + sid->sub_authority[0] = const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + sid->sub_authority[1] = const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + sid->identifier_authority.value[5] = 5; + sd->group = cpu_to_le32((u8*)sid - (u8*)sd); + + acl = (ACL*)((u8*)sid + sizeof(SID) + 4); + acl->revision = ACL_REVISION; + acl->size = const_cpu_to_le16(sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE)); + acl->ace_count = const_cpu_to_le16(1); + sd->dacl = cpu_to_le32((u8*)acl - (u8*)sd); + + ace = (ACCESS_ALLOWED_ACE*)((u8*)acl + sizeof(ACL)); + ace->type = ACCESS_ALLOWED_ACE_TYPE; + ace->flags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE; + ace->size = const_cpu_to_le16(sizeof(ACCESS_ALLOWED_ACE)); + ace->mask = const_cpu_to_le32(0x1f01ff); /* FIXME */ + ace->sid.revision = SID_REVISION; + ace->sid.sub_authority_count = 1; + ace->sid.sub_authority[0] = const_cpu_to_le32(0); + ace->sid.identifier_authority.value[5] = 1; + + ret = ntfs_attr_add(ni, AT_SECURITY_DESCRIPTOR, AT_UNNAMED, 0, (u8*)sd, + sd_len); + if (ret) + ntfs_log_perror("Failed to add initial SECURITY_DESCRIPTOR"); + + free(sd); + return ret; +} + +/* + * Check whether user can access a file in a specific way + * + * Returns 1 if access is allowed, including user is root or no + * user mapping defined + * 2 if sticky and accesstype is S_IWRITE + S_IEXEC + S_ISVTX + * 0 and sets errno if there is a problem or if access + * is not allowed + * + * This is used for Posix ACL and checking creation of DOS file names + */ + +int ntfs_allowed_access(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, + int accesstype) /* access type required (S_Ixxx values) */ +{ + int perm; + int res; + int allow; + struct stat stbuf; + + /* + * Always allow for root unless execution is requested. + * (was checked by fuse until kernel 2.6.29) + * Also always allow if no mapping has been defined + */ + if (!scx->mapping[MAPUSERS] + || (!scx->uid + && (!(accesstype & S_IEXEC) + || (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)))) + allow = 1; + else { + perm = ntfs_get_perm(scx, ni, accesstype); + if (perm >= 0) { + res = EACCES; + switch (accesstype) { + case S_IEXEC: + allow = (perm & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0; + break; + case S_IWRITE: + allow = (perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0; + break; + case S_IWRITE + S_IEXEC: + allow = ((perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0) + && ((perm & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0); + break; + case S_IREAD: + allow = (perm & (S_IRUSR | S_IRGRP | S_IROTH)) != 0; + break; + case S_IREAD + S_IEXEC: + allow = ((perm & (S_IRUSR | S_IRGRP | S_IROTH)) != 0) + && ((perm & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0); + break; + case S_IREAD + S_IWRITE: + allow = ((perm & (S_IRUSR | S_IRGRP | S_IROTH)) != 0) + && ((perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0); + break; + case S_IWRITE + S_IEXEC + S_ISVTX: + if (perm & S_ISVTX) { + if ((ntfs_get_owner_mode(scx,ni,&stbuf) >= 0) + && (stbuf.st_uid == scx->uid)) + allow = 1; + else + allow = 2; + } else + allow = ((perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0) + && ((perm & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0); + break; + case S_IREAD + S_IWRITE + S_IEXEC: + allow = ((perm & (S_IRUSR | S_IRGRP | S_IROTH)) != 0) + && ((perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0) + && ((perm & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0); + break; + default : + res = EINVAL; + allow = 0; + break; + } + if (!allow) + errno = res; + } else + allow = 0; + } + return (allow); +} + +/* + * Check whether user can create a file (or directory) + * + * Returns TRUE if access is allowed, + * Also returns the gid and dsetgid applicable to the created file + */ + +int ntfs_allowed_create(struct SECURITY_CONTEXT *scx, + ntfs_inode *dir_ni, gid_t *pgid, mode_t *pdsetgid) +{ + int perm; + int res; + int allow; + struct stat stbuf; + + /* + * Always allow for root. + * Also always allow if no mapping has been defined + */ + if (!scx->mapping[MAPUSERS]) + perm = 0777; + else + perm = ntfs_get_perm(scx, dir_ni, S_IWRITE + S_IEXEC); + if (!scx->mapping[MAPUSERS] + || !scx->uid) { + allow = 1; + } else { + perm = ntfs_get_perm(scx, dir_ni, S_IWRITE + S_IEXEC); + if (perm >= 0) { + res = EACCES; + allow = ((perm & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0) + && ((perm & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0); + if (!allow) + errno = res; + } else + allow = 0; + } + *pgid = scx->gid; + *pdsetgid = 0; + /* return directory group if S_ISGID is set */ + if (allow && (perm & S_ISGID)) { + if (ntfs_get_owner_mode(scx, dir_ni, &stbuf) >= 0) { + *pdsetgid = stbuf.st_mode & S_ISGID; + if (perm & S_ISGID) + *pgid = stbuf.st_gid; + } + } + return (allow); +} + +#if 0 /* not needed any more */ + +/* + * Check whether user can access the parent directory + * of a file in a specific way + * + * Returns true if access is allowed, including user is root and + * no user mapping defined + * + * Sets errno if there is a problem or if not allowed + * + * This is used for Posix ACL and checking creation of DOS file names + */ + +BOOL old_ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx, + const char *path, int accesstype) +{ + int allow; + char *dirpath; + char *name; + ntfs_inode *ni; + ntfs_inode *dir_ni; + struct stat stbuf; + + allow = 0; + dirpath = strdup(path); + if (dirpath) { + /* the root of file system is seen as a parent of itself */ + /* is that correct ? */ + name = strrchr(dirpath, '/'); + *name = 0; + dir_ni = ntfs_pathname_to_inode(scx->vol, NULL, dirpath); + if (dir_ni) { + allow = ntfs_allowed_access(scx, + dir_ni, accesstype); + ntfs_inode_close(dir_ni); + /* + * for an not-owned sticky directory, have to + * check whether file itself is owned + */ + if ((accesstype == (S_IWRITE + S_IEXEC + S_ISVTX)) + && (allow == 2)) { + ni = ntfs_pathname_to_inode(scx->vol, NULL, + path); + allow = FALSE; + if (ni) { + allow = (ntfs_get_owner_mode(scx,ni,&stbuf) >= 0) + && (stbuf.st_uid == scx->uid); + ntfs_inode_close(ni); + } + } + } + free(dirpath); + } + return (allow); /* errno is set if not allowed */ +} + +#endif + +/* + * Define a new owner/group to a file + * + * returns zero if successful + */ + +int ntfs_set_owner(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + uid_t uid, gid_t gid) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const struct CACHED_PERMISSIONS *cached; + char *oldattr; + const SID *usid; + const SID *gsid; + uid_t fileuid; + uid_t filegid; + mode_t mode; + int perm; + BOOL isdir; + int res; +#if POSIXACLS + struct POSIX_SECURITY *pxdesc; + BOOL pxdescbuilt = FALSE; +#endif + + res = 0; + /* get the current owner and mode from cache or security attributes */ + oldattr = (char*)NULL; + cached = fetch_cache(scx,ni); + if (cached) { + fileuid = cached->uid; + filegid = cached->gid; + mode = cached->mode; +#if POSIXACLS + pxdesc = cached->pxdesc; + if (!pxdesc) + res = -1; +#endif + } else { + fileuid = 0; + filegid = 0; + mode = 0; + oldattr = getsecurityattr(scx->vol, ni); + if (oldattr) { + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + != const_cpu_to_le16(0); + phead = (const SECURITY_DESCRIPTOR_RELATIVE*) + oldattr; + gsid = (const SID*) + &oldattr[le32_to_cpu(phead->group)]; +#if OWNERFROMACL + usid = ntfs_acl_owner(oldattr); +#else + usid = (const SID*) + &oldattr[le32_to_cpu(phead->owner)]; +#endif +#if POSIXACLS + pxdesc = ntfs_build_permissions_posix(scx->mapping, oldattr, + usid, gsid, isdir); + if (pxdesc) { + pxdescbuilt = TRUE; + fileuid = ntfs_find_user(scx->mapping[MAPUSERS],usid); + filegid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); + mode = perm = pxdesc->mode; + } else + res = -1; +#else + mode = perm = ntfs_build_permissions(oldattr, + usid, gsid, isdir); + if (perm >= 0) { + fileuid = ntfs_find_user(scx->mapping[MAPUSERS],usid); + filegid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); + } else + res = -1; +#endif + free(oldattr); + } else + res = -1; + } + if (!res) { + /* check requested by root */ + /* or chgrp requested by owner to an owned group */ + if (!scx->uid + || ((((int)uid < 0) || (uid == fileuid)) + && ((gid == scx->gid) || groupmember(scx, scx->uid, gid)) + && (fileuid == scx->uid))) { + /* replace by the new usid and gsid */ + /* or reuse old gid and sid for cacheing */ + if ((int)uid < 0) + uid = fileuid; + if ((int)gid < 0) + gid = filegid; +#if !defined(__sun) || !defined (__SVR4) + /* clear setuid and setgid if owner has changed */ + /* unless request originated by root */ + if (uid && (fileuid != uid)) + mode &= 01777; +#endif +#if POSIXACLS + res = ntfs_set_owner_mode(scx, ni, uid, gid, + mode, pxdesc); +#else + res = ntfs_set_owner_mode(scx, ni, uid, gid, mode); +#endif + } else { + res = -1; /* neither owner nor root */ + errno = EPERM; + } +#if POSIXACLS + if (pxdescbuilt) + free(pxdesc); +#endif + } else { + /* + * Should not happen : a default descriptor is generated + * by getsecurityattr() when there are none + */ + ntfs_log_error("File has no security descriptor\n"); + res = -1; + errno = EIO; + } + return (res ? -1 : 0); +} + +/* + * Define new owner/group and mode to a file + * + * returns zero if successful + */ + +int ntfs_set_ownmod(struct SECURITY_CONTEXT *scx, ntfs_inode *ni, + uid_t uid, gid_t gid, const mode_t mode) +{ + const struct CACHED_PERMISSIONS *cached; + char *oldattr; + uid_t fileuid; + uid_t filegid; + int res; +#if POSIXACLS + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const SID *usid; + const SID *gsid; + BOOL isdir; + const struct POSIX_SECURITY *oldpxdesc; + struct POSIX_SECURITY *newpxdesc = (struct POSIX_SECURITY*)NULL; + int pxsize; +#endif + + res = 0; + /* get the current owner and mode from cache or security attributes */ + oldattr = (char*)NULL; + cached = fetch_cache(scx,ni); + if (cached) { + fileuid = cached->uid; + filegid = cached->gid; +#if POSIXACLS + oldpxdesc = cached->pxdesc; + if (oldpxdesc) { + /* must copy before merging */ + pxsize = sizeof(struct POSIX_SECURITY) + + (oldpxdesc->acccnt + oldpxdesc->defcnt)*sizeof(struct POSIX_ACE); + newpxdesc = (struct POSIX_SECURITY*)malloc(pxsize); + if (newpxdesc) { + memcpy(newpxdesc, oldpxdesc, pxsize); + if (ntfs_merge_mode_posix(newpxdesc, mode)) + res = -1; + } else + res = -1; + } +#endif + } else { + fileuid = 0; + filegid = 0; + oldattr = getsecurityattr(scx->vol, ni); + if (oldattr) { +#if POSIXACLS + isdir = (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + != const_cpu_to_le16(0); + phead = (const SECURITY_DESCRIPTOR_RELATIVE*) + oldattr; + gsid = (const SID*) + &oldattr[le32_to_cpu(phead->group)]; +#if OWNERFROMACL + usid = ntfs_acl_owner(oldattr); +#else + usid = (const SID*) + &oldattr[le32_to_cpu(phead->owner)]; +#endif + newpxdesc = ntfs_build_permissions_posix(scx->mapping, oldattr, + usid, gsid, isdir); + if (!newpxdesc || ntfs_merge_mode_posix(newpxdesc, mode)) + res = -1; + else { + fileuid = ntfs_find_user(scx->mapping[MAPUSERS],usid); + filegid = ntfs_find_group(scx->mapping[MAPGROUPS],gsid); + } +#endif + free(oldattr); + } else + res = -1; + } + if (!res) { + /* check requested by root */ + /* or chgrp requested by owner to an owned group */ + if (!scx->uid + || ((((int)uid < 0) || (uid == fileuid)) + && ((gid == scx->gid) || groupmember(scx, scx->uid, gid)) + && (fileuid == scx->uid))) { + /* replace by the new usid and gsid */ + /* or reuse old gid and sid for cacheing */ + if ((int)uid < 0) + uid = fileuid; + if ((int)gid < 0) + gid = filegid; +#if POSIXACLS + res = ntfs_set_owner_mode(scx, ni, uid, gid, + mode, newpxdesc); +#else + res = ntfs_set_owner_mode(scx, ni, uid, gid, mode); +#endif + } else { + res = -1; /* neither owner nor root */ + errno = EPERM; + } + } else { + /* + * Should not happen : a default descriptor is generated + * by getsecurityattr() when there are none + */ + ntfs_log_error("File has no security descriptor\n"); + res = -1; + errno = EIO; + } +#if POSIXACLS + free(newpxdesc); +#endif + return (res ? -1 : 0); +} + +/* + * Build a security id for a descriptor inherited from + * parent directory the Windows way + */ + +static le32 build_inherited_id(struct SECURITY_CONTEXT *scx, + const char *parentattr, BOOL fordir) +{ + const SECURITY_DESCRIPTOR_RELATIVE *pphead; + const ACL *ppacl; + const SID *usid; + const SID *gsid; + BIGSID defusid; + BIGSID defgsid; + int offpacl; + int offgroup; + SECURITY_DESCRIPTOR_RELATIVE *pnhead; + ACL *pnacl; + int parentattrsz; + char *newattr; + int newattrsz; + int aclsz; + int usidsz; + int gsidsz; + int pos; + le32 securid; + + parentattrsz = ntfs_attr_size(parentattr); + pphead = (const SECURITY_DESCRIPTOR_RELATIVE*)parentattr; + if (scx->mapping[MAPUSERS]) { + usid = ntfs_find_usid(scx->mapping[MAPUSERS], scx->uid, (SID*)&defusid); + gsid = ntfs_find_gsid(scx->mapping[MAPGROUPS], scx->gid, (SID*)&defgsid); +#if OWNERFROMACL + /* Get approximation of parent owner when cannot map */ + if (!gsid) + gsid = adminsid; + if (!usid) { + usid = ntfs_acl_owner(parentattr); + if (!ntfs_is_user_sid(gsid)) + gsid = usid; + } +#else + /* Define owner as root when cannot map */ + if (!usid) + usid = adminsid; + if (!gsid) + gsid = adminsid; +#endif + } else { + /* + * If there is no user mapping and this is not a root + * user, we have to get owner and group from somewhere, + * and the parent directory has to contribute. + * Windows never has to do that, because it can always + * rely on a user mapping + */ + if (!scx->uid) + usid = adminsid; + else { +#if OWNERFROMACL + usid = ntfs_acl_owner(parentattr); +#else + int offowner; + + offowner = le32_to_cpu(pphead->owner); + usid = (const SID*)&parentattr[offowner]; +#endif + } + if (!scx->gid) + gsid = adminsid; + else { + offgroup = le32_to_cpu(pphead->group); + gsid = (const SID*)&parentattr[offgroup]; + } + } + /* + * new attribute is smaller than parent's + * except for differences in SIDs which appear in + * owner, group and possible grants and denials in + * generic creator-owner and creator-group ACEs. + * For directories, an ACE may be duplicated for + * access and inheritance, so we double the count. + */ + usidsz = ntfs_sid_size(usid); + gsidsz = ntfs_sid_size(gsid); + newattrsz = parentattrsz + 3*usidsz + 3*gsidsz; + /* + * The +3*usidsz + 3*gsidsz slack above only covers a few creator SID + * expansions during ntfs_inherit_acl(). Add worst-case slack for every + * ALLOW/DENY creator-owner and creator-group ACE in both the parent + * DACL and SACL. + */ + if (pphead->dacl) { + offpacl = le32_to_cpu(pphead->dacl); + if ((unsigned int)offpacl + sizeof(ACL) <= + (unsigned int)parentattrsz) { + ppacl = (const ACL*)&parentattr[offpacl]; + newattrsz += ntfs_inherit_acl_extra_size(ppacl, + usid, gsid); + } + } + if (pphead->sacl) { + offpacl = le32_to_cpu(pphead->sacl); + if ((unsigned int)offpacl + sizeof(ACL) <= + (unsigned int)parentattrsz) { + ppacl = (const ACL*)&parentattr[offpacl]; + newattrsz += ntfs_inherit_acl_extra_size(ppacl, + usid, gsid); + } + } + if (fordir) + newattrsz *= 2; + newattr = (char*)ntfs_malloc(newattrsz); + if (newattr) { + pnhead = (SECURITY_DESCRIPTOR_RELATIVE*)newattr; + pnhead->revision = SECURITY_DESCRIPTOR_REVISION; + pnhead->alignment = 0; + pnhead->control = (pphead->control + & (SE_DACL_AUTO_INHERITED | SE_SACL_AUTO_INHERITED)) + | SE_SELF_RELATIVE; + pos = sizeof(SECURITY_DESCRIPTOR_RELATIVE); + /* + * locate and inherit DACL + * do not test SE_DACL_PRESENT (wrong for "DR Watson") + */ + pnhead->dacl = const_cpu_to_le32(0); + if (pphead->dacl) { + offpacl = le32_to_cpu(pphead->dacl); + ppacl = (const ACL*)&parentattr[offpacl]; + pnacl = (ACL*)&newattr[pos]; + aclsz = ntfs_inherit_acl(ppacl, pnacl, usid, gsid, + fordir, pphead->control + & SE_DACL_AUTO_INHERITED); + if (aclsz) { + pnhead->dacl = cpu_to_le32(pos); + pos += aclsz; + pnhead->control |= SE_DACL_PRESENT; + } + } + /* + * locate and inherit SACL + */ + pnhead->sacl = const_cpu_to_le32(0); + if (pphead->sacl) { + offpacl = le32_to_cpu(pphead->sacl); + ppacl = (const ACL*)&parentattr[offpacl]; + pnacl = (ACL*)&newattr[pos]; + aclsz = ntfs_inherit_acl(ppacl, pnacl, usid, gsid, + fordir, pphead->control + & SE_SACL_AUTO_INHERITED); + if (aclsz) { + pnhead->sacl = cpu_to_le32(pos); + pos += aclsz; + pnhead->control |= SE_SACL_PRESENT; + } + } + /* + * inherit or redefine owner + */ + memcpy(&newattr[pos],usid,usidsz); + pnhead->owner = cpu_to_le32(pos); + pos += usidsz; + /* + * inherit or redefine group + */ + memcpy(&newattr[pos],gsid,gsidsz); + pnhead->group = cpu_to_le32(pos); + pos += gsidsz; + securid = setsecurityattr(scx->vol, + (SECURITY_DESCRIPTOR_RELATIVE*)newattr, pos); + free(newattr); + } else + securid = const_cpu_to_le32(0); + return (securid); +} + +/* + * Get an inherited security id + * + * For Windows compatibility, the normal initial permission setting + * may be inherited from the parent directory instead of being + * defined by the creation arguments. + * + * The following creates an inherited id for that purpose. + * + * Note : the owner and group of parent directory are also + * inherited (which is not the case on Windows) if no user mapping + * is defined. + * + * Returns the inherited id, or zero if not possible (eg on NTFS 1.x) + */ + +le32 ntfs_inherited_id(struct SECURITY_CONTEXT *scx, + ntfs_inode *dir_ni, BOOL fordir) +{ + struct CACHED_PERMISSIONS *cached; + char *parentattr; + le32 securid; + + securid = const_cpu_to_le32(0); + cached = (struct CACHED_PERMISSIONS*)NULL; + /* + * Try to get inherited id from cache, possible when + * the current process owns the parent directory + */ + if (test_nino_flag(dir_ni, v3_Extensions) + && dir_ni->security_id) { + cached = fetch_cache(scx, dir_ni); + if (cached + && (cached->uid == scx->uid) && (cached->gid == scx->gid)) + securid = (fordir ? cached->inh_dirid + : cached->inh_fileid); + } + /* + * Not cached or not available in cache, compute it all + * Note : if parent directory has no id, it is not cacheable + */ + if (!securid) { + parentattr = getsecurityattr(scx->vol, dir_ni); + if (parentattr) { + securid = build_inherited_id(scx, + parentattr, fordir); + free(parentattr); + /* + * Store the result into cache for further use + * if the current process owns the parent directory + */ + if (securid) { + cached = fetch_cache(scx, dir_ni); + if (cached + && (cached->uid == scx->uid) + && (cached->gid == scx->gid)) { + if (fordir) + cached->inh_dirid = securid; + else + cached->inh_fileid = securid; + } + } + } + } + return (securid); +} + +/* + * Link a group to a member of group + * + * Returns 0 if OK, -1 (and errno set) if error + */ + +static int link_single_group(struct MAPPING *usermapping, struct passwd *user, + gid_t gid) +{ + struct group *group; + char **grmem; + int grcnt; + gid_t *groups; + int res; + + res = 0; + group = getgrgid(gid); + if (group && group->gr_mem) { + grcnt = usermapping->grcnt; + groups = usermapping->groups; + grmem = group->gr_mem; + while (*grmem && strcmp(user->pw_name, *grmem)) + grmem++; + if (*grmem) { + if (!grcnt) + groups = (gid_t*)malloc(sizeof(gid_t)); + else + groups = (gid_t*)realloc(groups, + (grcnt+1)*sizeof(gid_t)); + if (groups) + groups[grcnt++] = gid; + else { + res = -1; + errno = ENOMEM; + } + } + usermapping->grcnt = grcnt; + usermapping->groups = groups; + } + return (res); +} + + +/* + * Statically link group to users + * This is based on groups defined in /etc/group and does not take + * the groups dynamically set by setgroups() nor any changes in + * /etc/group into account + * + * Only mapped groups and root group are linked to mapped users + * + * Returns 0 if OK, -1 (and errno set) if error + * + */ + +static int link_group_members(struct SECURITY_CONTEXT *scx) +{ + struct MAPPING *usermapping; + struct MAPPING *groupmapping; + struct passwd *user; + int res; + + res = 0; + for (usermapping=scx->mapping[MAPUSERS]; usermapping && !res; + usermapping=usermapping->next) { + usermapping->grcnt = 0; + usermapping->groups = (gid_t*)NULL; + user = getpwuid(usermapping->xid); + if (user && user->pw_name) { + for (groupmapping=scx->mapping[MAPGROUPS]; + groupmapping && !res; + groupmapping=groupmapping->next) { + if (link_single_group(usermapping, user, + groupmapping->xid)) + res = -1; + } + if (!res && link_single_group(usermapping, + user, (gid_t)0)) + res = -1; + } + } + return (res); +} + +/* + * Apply default single user mapping + * returns zero if successful + */ + +static int ntfs_do_default_mapping(struct SECURITY_CONTEXT *scx, + uid_t uid, gid_t gid, const SID *usid) +{ + struct MAPPING *usermapping; + struct MAPPING *groupmapping; + SID *sid; + int sidsz; + int res; + + res = -1; + sidsz = ntfs_sid_size(usid); + sid = (SID*)ntfs_malloc(sidsz); + if (sid) { + memcpy(sid,usid,sidsz); + usermapping = (struct MAPPING*)ntfs_malloc(sizeof(struct MAPPING)); + if (usermapping) { + groupmapping = (struct MAPPING*)ntfs_malloc(sizeof(struct MAPPING)); + if (groupmapping) { + usermapping->sid = sid; + usermapping->xid = uid; + usermapping->next = (struct MAPPING*)NULL; + groupmapping->sid = sid; + groupmapping->xid = gid; + groupmapping->next = (struct MAPPING*)NULL; + scx->mapping[MAPUSERS] = usermapping; + scx->mapping[MAPGROUPS] = groupmapping; + res = 0; + } + } + } + return (res); +} + +/* + * Make sure there are no ambiguous mapping + * Ambiguous mapping may lead to undesired configurations and + * we had rather be safe until the consequences are understood + */ + +#if 0 /* not activated for now */ + +static BOOL check_mapping(const struct MAPPING *usermapping, + const struct MAPPING *groupmapping) +{ + const struct MAPPING *mapping1; + const struct MAPPING *mapping2; + BOOL ambiguous; + + ambiguous = FALSE; + for (mapping1=usermapping; mapping1; mapping1=mapping1->next) + for (mapping2=mapping1->next; mapping2; mapping1=mapping2->next) + if (ntfs_same_sid(mapping1->sid,mapping2->sid)) { + if (mapping1->xid != mapping2->xid) + ambiguous = TRUE; + } else { + if (mapping1->xid == mapping2->xid) + ambiguous = TRUE; + } + for (mapping1=groupmapping; mapping1; mapping1=mapping1->next) + for (mapping2=mapping1->next; mapping2; mapping1=mapping2->next) + if (ntfs_same_sid(mapping1->sid,mapping2->sid)) { + if (mapping1->xid != mapping2->xid) + ambiguous = TRUE; + } else { + if (mapping1->xid == mapping2->xid) + ambiguous = TRUE; + } + return (ambiguous); +} + +#endif + +#if 0 /* not used any more */ + +/* + * Try and apply default single user mapping + * returns zero if successful + */ + +static int ntfs_default_mapping(struct SECURITY_CONTEXT *scx) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + ntfs_inode *ni; + char *securattr; + const SID *usid; + int res; + + res = -1; + ni = ntfs_pathname_to_inode(scx->vol, NULL, "/."); + if (ni) { + securattr = getsecurityattr(scx->vol, ni); + if (securattr) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)securattr; + usid = (SID*)&securattr[le32_to_cpu(phead->owner)]; + if (ntfs_is_user_sid(usid)) + res = ntfs_do_default_mapping(scx, + scx->uid, scx->gid, usid); + free(securattr); + } + ntfs_inode_close(ni); + } + return (res); +} + +#endif + +/* + * Basic read from a user mapping file on another volume + */ + +static int basicread(void *fileid, char *buf, size_t size, off_t offs __attribute__((unused))) +{ + return (read(*(int*)fileid, buf, size)); +} + + +/* + * Read from a user mapping file on current NTFS partition + */ + +static int localread(void *fileid, char *buf, size_t size, off_t offs) +{ + return (ntfs_attr_data_read((ntfs_inode*)fileid, + AT_UNNAMED, 0, buf, size, offs)); +} + +/* + * Build the user mapping + * - according to a mapping file if defined (or default present), + * - or try default single user mapping if possible + * + * The mapping is specific to a mounted device + * No locking done, mounting assumed non multithreaded + * + * returns zero if mapping is successful + * (failure should not be interpreted as an error) + */ + +int ntfs_build_mapping(struct SECURITY_CONTEXT *scx, const char *usermap_path, + BOOL allowdef) +{ + struct MAPLIST *item; + struct MAPLIST *firstitem; + struct MAPPING *usermapping; + struct MAPPING *groupmapping; + ntfs_inode *ni; + int fd; + static struct { + u8 revision; + u8 levels; + be16 highbase; + be32 lowbase; + le32 level1; + le32 level2; + le32 level3; + le32 level4; + le32 level5; + } defmap = { + 1, 5, const_cpu_to_be16(0), const_cpu_to_be32(5), + const_cpu_to_le32(21), + const_cpu_to_le32(DEFSECAUTH1), const_cpu_to_le32(DEFSECAUTH2), + const_cpu_to_le32(DEFSECAUTH3), const_cpu_to_le32(DEFSECBASE) + } ; + + /* be sure not to map anything until done */ + scx->mapping[MAPUSERS] = (struct MAPPING*)NULL; + scx->mapping[MAPGROUPS] = (struct MAPPING*)NULL; + + if (!usermap_path) usermap_path = MAPPINGFILE; + if (usermap_path[0] == '/') { + fd = open(usermap_path,O_RDONLY); + if (fd > 0) { + firstitem = ntfs_read_mapping(basicread, (void*)&fd); + close(fd); + } else + firstitem = (struct MAPLIST*)NULL; + } else { + ni = ntfs_pathname_to_inode(scx->vol, NULL, usermap_path); + if (ni) { + firstitem = ntfs_read_mapping(localread, ni); + ntfs_inode_close(ni); + } else + firstitem = (struct MAPLIST*)NULL; + } + + + if (firstitem) { + usermapping = ntfs_do_user_mapping(firstitem); + groupmapping = ntfs_do_group_mapping(firstitem); + if (usermapping && groupmapping) { + scx->mapping[MAPUSERS] = usermapping; + scx->mapping[MAPGROUPS] = groupmapping; + } else + ntfs_log_error("There were no valid user or no valid group\n"); + /* now we can free the memory copy of input text */ + /* and rely on internal representation */ + while (firstitem) { + item = firstitem->next; + free(firstitem); + firstitem = item; + } + } else { + /* no mapping file, try a default mapping */ + if (allowdef) { + if (!ntfs_do_default_mapping(scx, + 0, 0, (const SID*)&defmap)) + ntfs_log_info("Using default user mapping\n"); + } + } + return (!scx->mapping[MAPUSERS] || link_group_members(scx)); +} + + +/* + * Get the ntfs attribute into an extended attribute + * The attribute is returned according to cpu endianness + */ + +int ntfs_get_ntfs_attrib(ntfs_inode *ni, char *value, size_t size) +{ + u32 attrib; + size_t outsize; + + outsize = 0; /* default to no data and no error */ + if (ni) { + attrib = le32_to_cpu(ni->flags); + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + attrib |= const_le32_to_cpu(FILE_ATTR_DIRECTORY); + else + attrib &= ~const_le32_to_cpu(FILE_ATTR_DIRECTORY); + if (!attrib) + attrib |= const_le32_to_cpu(FILE_ATTR_NORMAL); + outsize = sizeof(FILE_ATTR_FLAGS); + if (size >= outsize) { + if (value) + memcpy(value,&attrib,outsize); + else + errno = EINVAL; + } + } + return (outsize ? (int)outsize : -errno); +} + +/* + * Return the ntfs attribute into an extended attribute + * The attribute is expected according to cpu endianness + * + * Returns 0, or -1 if there is a problem + */ + +int ntfs_set_ntfs_attrib(ntfs_inode *ni, + const char *value, size_t size, int flags) +{ + u32 attrib; + le32 settable; + ATTR_FLAGS dirflags; + int res; + + res = -1; + if (ni && value && (size >= sizeof(FILE_ATTR_FLAGS))) { + if (!(flags & XATTR_CREATE)) { + /* copy to avoid alignment problems */ + memcpy(&attrib,value,sizeof(FILE_ATTR_FLAGS)); + settable = FILE_ATTR_SETTABLE; + res = 0; + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + /* + * Accept changing compression for a directory + * and set index root accordingly + */ + settable |= FILE_ATTR_COMPRESSED; + if ((ni->flags ^ cpu_to_le32(attrib)) + & FILE_ATTR_COMPRESSED) { + if (ni->flags & FILE_ATTR_COMPRESSED) + dirflags = const_cpu_to_le16(0); + else + dirflags = ATTR_IS_COMPRESSED; + res = ntfs_attr_set_flags(ni, + AT_INDEX_ROOT, + NTFS_INDEX_I30, 4, + dirflags, + ATTR_COMPRESSION_MASK); + } + } + if (!res) { + ni->flags = (ni->flags & ~settable) + | (cpu_to_le32(attrib) & settable); + NInoFileNameSetDirty(ni); + NInoSetDirty(ni); + } + } else + errno = EEXIST; + } else + errno = EINVAL; + return (res ? -1 : 0); +} + + +/* + * Open the volume's security descriptor index ($Secure) + * + * returns 0 if it succeeds + * -1 with errno set if it fails and the volume is NTFS v3.0+ + */ +int ntfs_open_secure(ntfs_volume *vol) +{ + ntfs_inode *ni; + ntfs_index_context *sii; + ntfs_index_context *sdh; + + if (vol->secure_ni) /* Already open? */ + return 0; + + ni = ntfs_pathname_to_inode(vol, NULL, "$Secure"); + if (!ni) + goto err; + + if (ni->mft_no != FILE_Secure) { + ntfs_log_error("$Secure does not have expected inode number!"); + errno = EINVAL; + goto err_close_ni; + } + + /* Allocate the needed index contexts. */ + sii = ntfs_index_ctx_get(ni, sii_stream, 4); + if (!sii) + goto err_close_ni; + + sdh = ntfs_index_ctx_get(ni, sdh_stream, 4); + if (!sdh) + goto err_close_sii; + + vol->secure_xsdh = sdh; + vol->secure_xsii = sii; + vol->secure_ni = ni; + return 0; + +err_close_sii: + ntfs_index_ctx_put(sii); +err_close_ni: + ntfs_inode_close(ni); +err: + /* Failing on NTFS pre-v3.0 is expected. */ + if (vol->major_ver < 3) + return 0; + ntfs_log_perror("Failed to open $Secure"); + return -1; +} + +/* + * Close the volume's security descriptor index ($Secure) + * + * returns 0 if it succeeds + * -1 with errno set if it fails + */ +int ntfs_close_secure(ntfs_volume *vol) +{ + int res = 0; + + if (vol->secure_ni) { + ntfs_index_ctx_put(vol->secure_xsdh); + ntfs_index_ctx_put(vol->secure_xsii); + res = ntfs_inode_close(vol->secure_ni); + vol->secure_ni = NULL; + } + return res; +} + +/* + * Destroy a security context + * Allocated memory is freed to facilitate the detection of memory leaks + */ +void ntfs_destroy_security_context(struct SECURITY_CONTEXT *scx) +{ + ntfs_free_mapping(scx->mapping); + free_caches(scx); +} + +/* + * API for direct access to security descriptors + * based on Win32 API + */ + + +/* + * Selective feeding of a security descriptor into user buffer + * + * Returns TRUE if successful + */ + +static BOOL feedsecurityattr(const char *attr, u32 selection, + char *buf, u32 buflen, u32 *psize) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + SECURITY_DESCRIPTOR_RELATIVE *pnhead; + const ACL *pdacl; + const ACL *psacl; + const SID *pusid; + const SID *pgsid; + unsigned int offdacl; + unsigned int offsacl; + unsigned int offowner; + unsigned int offgroup; + unsigned int daclsz; + unsigned int saclsz; + unsigned int usidsz; + unsigned int gsidsz; + unsigned int size; /* size of requested attributes */ + BOOL ok; + unsigned int pos; + unsigned int avail; + le16 control; + + avail = 0; + control = SE_SELF_RELATIVE; + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + size = sizeof(SECURITY_DESCRIPTOR_RELATIVE); + + /* locate DACL if requested and available */ + if (phead->dacl && (selection & DACL_SECURITY_INFORMATION)) { + offdacl = le32_to_cpu(phead->dacl); + pdacl = (const ACL*)&attr[offdacl]; + daclsz = le16_to_cpu(pdacl->size); + size += daclsz; + avail |= DACL_SECURITY_INFORMATION; + } else + offdacl = daclsz = 0; + + /* locate owner if requested and available */ + offowner = le32_to_cpu(phead->owner); + if (offowner && (selection & OWNER_SECURITY_INFORMATION)) { + /* find end of USID */ + pusid = (const SID*)&attr[offowner]; + usidsz = ntfs_sid_size(pusid); + size += usidsz; + avail |= OWNER_SECURITY_INFORMATION; + } else + offowner = usidsz = 0; + + /* locate group if requested and available */ + offgroup = le32_to_cpu(phead->group); + if (offgroup && (selection & GROUP_SECURITY_INFORMATION)) { + /* find end of GSID */ + pgsid = (const SID*)&attr[offgroup]; + gsidsz = ntfs_sid_size(pgsid); + size += gsidsz; + avail |= GROUP_SECURITY_INFORMATION; + } else + offgroup = gsidsz = 0; + + /* locate SACL if requested and available */ + if (phead->sacl && (selection & SACL_SECURITY_INFORMATION)) { + /* find end of SACL */ + offsacl = le32_to_cpu(phead->sacl); + psacl = (const ACL*)&attr[offsacl]; + saclsz = le16_to_cpu(psacl->size); + size += saclsz; + avail |= SACL_SECURITY_INFORMATION; + } else + offsacl = saclsz = 0; + + /* + * Check having enough size in destination buffer + * (required size is returned nevertheless so that + * the request can be reissued with adequate size) + */ + if (size > buflen) { + *psize = size; + errno = EINVAL; + ok = FALSE; + } else { + if (selection & OWNER_SECURITY_INFORMATION) + control |= phead->control & SE_OWNER_DEFAULTED; + if (selection & GROUP_SECURITY_INFORMATION) + control |= phead->control & SE_GROUP_DEFAULTED; + if (selection & DACL_SECURITY_INFORMATION) + control |= phead->control + & (SE_DACL_PRESENT + | SE_DACL_DEFAULTED + | SE_DACL_AUTO_INHERITED + | SE_DACL_PROTECTED); + if (selection & SACL_SECURITY_INFORMATION) + control |= phead->control + & (SE_SACL_PRESENT + | SE_SACL_DEFAULTED + | SE_SACL_AUTO_INHERITED + | SE_SACL_PROTECTED); + /* + * copy header and feed new flags, even if no detailed data + */ + memcpy(buf,attr,sizeof(SECURITY_DESCRIPTOR_RELATIVE)); + pnhead = (SECURITY_DESCRIPTOR_RELATIVE*)buf; + pnhead->control = control; + pos = sizeof(SECURITY_DESCRIPTOR_RELATIVE); + + /* copy DACL if requested and available */ + if (selection & avail & DACL_SECURITY_INFORMATION) { + pnhead->dacl = cpu_to_le32(pos); + memcpy(&buf[pos],&attr[offdacl],daclsz); + pos += daclsz; + } else + pnhead->dacl = const_cpu_to_le32(0); + + /* copy SACL if requested and available */ + if (selection & avail & SACL_SECURITY_INFORMATION) { + pnhead->sacl = cpu_to_le32(pos); + memcpy(&buf[pos],&attr[offsacl],saclsz); + pos += saclsz; + } else + pnhead->sacl = const_cpu_to_le32(0); + + /* copy owner if requested and available */ + if (selection & avail & OWNER_SECURITY_INFORMATION) { + pnhead->owner = cpu_to_le32(pos); + memcpy(&buf[pos],&attr[offowner],usidsz); + pos += usidsz; + } else + pnhead->owner = const_cpu_to_le32(0); + + /* copy group if requested and available */ + if (selection & avail & GROUP_SECURITY_INFORMATION) { + pnhead->group = cpu_to_le32(pos); + memcpy(&buf[pos],&attr[offgroup],gsidsz); + pos += gsidsz; + } else + pnhead->group = const_cpu_to_le32(0); + if (pos != size) + ntfs_log_error("Error in security descriptor size\n"); + *psize = size; + ok = TRUE; + } + + return (ok); +} + +/* + * Merge a new security descriptor into the old one + * and assign to designated file + * + * Returns TRUE if successful + */ + +static BOOL mergesecurityattr(ntfs_volume *vol, const char *oldattr, + const char *newattr, u32 selection, ntfs_inode *ni) +{ + const SECURITY_DESCRIPTOR_RELATIVE *oldhead; + const SECURITY_DESCRIPTOR_RELATIVE *newhead; + SECURITY_DESCRIPTOR_RELATIVE *targhead; + const ACL *pdacl; + const ACL *psacl; + const SID *powner; + const SID *pgroup; + int offdacl; + int offsacl; + int offowner; + int offgroup; + unsigned int size; + le16 control; + char *target; + int pos; + int oldattrsz; + int newattrsz; + BOOL ok; + + ok = FALSE; /* default return */ + oldhead = (const SECURITY_DESCRIPTOR_RELATIVE*)oldattr; + newhead = (const SECURITY_DESCRIPTOR_RELATIVE*)newattr; + oldattrsz = ntfs_attr_size(oldattr); + newattrsz = ntfs_attr_size(newattr); + target = (char*)ntfs_malloc(oldattrsz + newattrsz); + if (target) { + targhead = (SECURITY_DESCRIPTOR_RELATIVE*)target; + pos = sizeof(SECURITY_DESCRIPTOR_RELATIVE); + control = SE_SELF_RELATIVE; + /* + * copy new DACL if selected + * or keep old DACL if any + */ + if ((selection & DACL_SECURITY_INFORMATION) ? + newhead->dacl : oldhead->dacl) { + if (selection & DACL_SECURITY_INFORMATION) { + offdacl = le32_to_cpu(newhead->dacl); + pdacl = (const ACL*)&newattr[offdacl]; + } else { + offdacl = le32_to_cpu(oldhead->dacl); + pdacl = (const ACL*)&oldattr[offdacl]; + } + size = le16_to_cpu(pdacl->size); + memcpy(&target[pos], pdacl, size); + targhead->dacl = cpu_to_le32(pos); + pos += size; + } else + targhead->dacl = const_cpu_to_le32(0); + if (selection & DACL_SECURITY_INFORMATION) { + control |= newhead->control + & (SE_DACL_PRESENT + | SE_DACL_DEFAULTED + | SE_DACL_PROTECTED); + if (newhead->control & SE_DACL_AUTO_INHERIT_REQ) + control |= SE_DACL_AUTO_INHERITED; + } else + control |= oldhead->control + & (SE_DACL_PRESENT + | SE_DACL_DEFAULTED + | SE_DACL_AUTO_INHERITED + | SE_DACL_PROTECTED); + /* + * copy new SACL if selected + * or keep old SACL if any + */ + if ((selection & SACL_SECURITY_INFORMATION) ? + newhead->sacl : oldhead->sacl) { + if (selection & SACL_SECURITY_INFORMATION) { + offsacl = le32_to_cpu(newhead->sacl); + psacl = (const ACL*)&newattr[offsacl]; + } else { + offsacl = le32_to_cpu(oldhead->sacl); + psacl = (const ACL*)&oldattr[offsacl]; + } + size = le16_to_cpu(psacl->size); + memcpy(&target[pos], psacl, size); + targhead->sacl = cpu_to_le32(pos); + pos += size; + } else + targhead->sacl = const_cpu_to_le32(0); + if (selection & SACL_SECURITY_INFORMATION) { + control |= newhead->control + & (SE_SACL_PRESENT + | SE_SACL_DEFAULTED + | SE_SACL_PROTECTED); + if (newhead->control & SE_SACL_AUTO_INHERIT_REQ) + control |= SE_SACL_AUTO_INHERITED; + } else + control |= oldhead->control + & (SE_SACL_PRESENT + | SE_SACL_DEFAULTED + | SE_SACL_AUTO_INHERITED + | SE_SACL_PROTECTED); + /* + * copy new OWNER if selected + * or keep old OWNER if any + */ + if ((selection & OWNER_SECURITY_INFORMATION) ? + newhead->owner : oldhead->owner) { + if (selection & OWNER_SECURITY_INFORMATION) { + offowner = le32_to_cpu(newhead->owner); + powner = (const SID*)&newattr[offowner]; + } else { + offowner = le32_to_cpu(oldhead->owner); + powner = (const SID*)&oldattr[offowner]; + } + size = ntfs_sid_size(powner); + memcpy(&target[pos], powner, size); + targhead->owner = cpu_to_le32(pos); + pos += size; + } else + targhead->owner = const_cpu_to_le32(0); + if (selection & OWNER_SECURITY_INFORMATION) + control |= newhead->control & SE_OWNER_DEFAULTED; + else + control |= oldhead->control & SE_OWNER_DEFAULTED; + /* + * copy new GROUP if selected + * or keep old GROUP if any + */ + if ((selection & GROUP_SECURITY_INFORMATION) ? + newhead->group : oldhead->group) { + if (selection & GROUP_SECURITY_INFORMATION) { + offgroup = le32_to_cpu(newhead->group); + pgroup = (const SID*)&newattr[offgroup]; + control |= newhead->control + & SE_GROUP_DEFAULTED; + } else { + offgroup = le32_to_cpu(oldhead->group); + pgroup = (const SID*)&oldattr[offgroup]; + control |= oldhead->control + & SE_GROUP_DEFAULTED; + } + size = ntfs_sid_size(pgroup); + memcpy(&target[pos], pgroup, size); + targhead->group = cpu_to_le32(pos); + pos += size; + } else + targhead->group = const_cpu_to_le32(0); + if (selection & GROUP_SECURITY_INFORMATION) + control |= newhead->control & SE_GROUP_DEFAULTED; + else + control |= oldhead->control & SE_GROUP_DEFAULTED; + targhead->revision = SECURITY_DESCRIPTOR_REVISION; + targhead->alignment = 0; + targhead->control = control; + ok = !update_secur_descr(vol, target, ni); + free(target); + } + return (ok); +} + +/* + * Return the security descriptor of a file + * This is intended to be similar to GetFileSecurity() from Win32 + * in order to facilitate the development of portable tools + * + * returns zero if unsuccessful (following Win32 conventions) + * -1 if no securid + * the securid if any + * + * The Win32 API is : + * + * BOOL WINAPI GetFileSecurity( + * __in LPCTSTR lpFileName, + * __in SECURITY_INFORMATION RequestedInformation, + * __out_opt PSECURITY_DESCRIPTOR pSecurityDescriptor, + * __in DWORD nLength, + * __out LPDWORD lpnLengthNeeded + * ); + * + */ + +int ntfs_get_file_security(struct SECURITY_API *scapi, + const char *path, u32 selection, + char *buf, u32 buflen, u32 *psize) +{ + ntfs_inode *ni; + char *attr; + int res; + + res = 0; /* default return */ + if (scapi && (scapi->magic == MAGIC_API)) { + ni = ntfs_pathname_to_inode(scapi->security.vol, NULL, path); + if (ni) { + attr = getsecurityattr(scapi->security.vol, ni); + if (attr) { + if (feedsecurityattr(attr,selection, + buf,buflen,psize)) { + if (test_nino_flag(ni, v3_Extensions) + && ni->security_id) + res = le32_to_cpu( + ni->security_id); + else + res = -1; + } + free(attr); + } + ntfs_inode_close(ni); + } else + errno = ENOENT; + if (!res) *psize = 0; + } else + errno = EINVAL; /* do not clear *psize */ + return (res); +} + + +/* + * Set the security descriptor of a file or directory + * This is intended to be similar to SetFileSecurity() from Win32 + * in order to facilitate the development of portable tools + * + * returns zero if unsuccessful (following Win32 conventions) + * -1 if no securid + * the securid if any + * + * The Win32 API is : + * + * BOOL WINAPI SetFileSecurity( + * __in LPCTSTR lpFileName, + * __in SECURITY_INFORMATION SecurityInformation, + * __in PSECURITY_DESCRIPTOR pSecurityDescriptor + * ); + */ + +int ntfs_set_file_security(struct SECURITY_API *scapi, + const char *path, u32 selection, const char *attr) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + ntfs_inode *ni; + int attrsz; + BOOL missing; + char *oldattr; + int res; + + res = 0; /* default return */ + if (scapi && (scapi->magic == MAGIC_API) && attr) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + attrsz = ntfs_attr_size(attr); + /* if selected, owner and group must be present or defaulted */ + missing = ((selection & OWNER_SECURITY_INFORMATION) + && !phead->owner + && !(phead->control & SE_OWNER_DEFAULTED)) + || ((selection & GROUP_SECURITY_INFORMATION) + && !phead->group + && !(phead->control & SE_GROUP_DEFAULTED)); + if (!missing + && (phead->control & SE_SELF_RELATIVE) + && ntfs_valid_descr(attr, attrsz)) { + ni = ntfs_pathname_to_inode(scapi->security.vol, + NULL, path); + if (ni) { + oldattr = getsecurityattr(scapi->security.vol, + ni); + if (oldattr) { + if (mergesecurityattr( + scapi->security.vol, + oldattr, attr, + selection, ni)) { + if (test_nino_flag(ni, + v3_Extensions)) + res = le32_to_cpu( + ni->security_id); + else + res = -1; + } + free(oldattr); + } + ntfs_inode_close(ni); + } + } else + errno = EINVAL; + } else + errno = EINVAL; + return (res); +} + + +/* + * Return the attributes of a file + * This is intended to be similar to GetFileAttributes() from Win32 + * in order to facilitate the development of portable tools + * + * returns -1 if unsuccessful (Win32 : INVALID_FILE_ATTRIBUTES) + * + * The Win32 API is : + * + * DWORD WINAPI GetFileAttributes( + * __in LPCTSTR lpFileName + * ); + */ + +int ntfs_get_file_attributes(struct SECURITY_API *scapi, const char *path) +{ + ntfs_inode *ni; + s32 attrib; + + attrib = -1; /* default return */ + if (scapi && (scapi->magic == MAGIC_API) && path) { + ni = ntfs_pathname_to_inode(scapi->security.vol, NULL, path); + if (ni) { + attrib = le32_to_cpu(ni->flags); + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + attrib |= const_le32_to_cpu(FILE_ATTR_DIRECTORY); + else + attrib &= ~const_le32_to_cpu(FILE_ATTR_DIRECTORY); + if (!attrib) + attrib |= const_le32_to_cpu(FILE_ATTR_NORMAL); + + ntfs_inode_close(ni); + } else + errno = ENOENT; + } else + errno = EINVAL; /* do not clear *psize */ + return (attrib); +} + + +/* + * Set attributes to a file or directory + * This is intended to be similar to SetFileAttributes() from Win32 + * in order to facilitate the development of portable tools + * + * Only a few flags can be set (same list as Win32) + * + * returns zero if unsuccessful (following Win32 conventions) + * nonzero if successful + * + * The Win32 API is : + * + * BOOL WINAPI SetFileAttributes( + * __in LPCTSTR lpFileName, + * __in DWORD dwFileAttributes + * ); + */ + +BOOL ntfs_set_file_attributes(struct SECURITY_API *scapi, + const char *path, s32 attrib) +{ + ntfs_inode *ni; + le32 settable; + ATTR_FLAGS dirflags; + int res; + + res = 0; /* default return */ + if (scapi && (scapi->magic == MAGIC_API) && path) { + ni = ntfs_pathname_to_inode(scapi->security.vol, NULL, path); + if (ni) { + settable = FILE_ATTR_SETTABLE; + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + /* + * Accept changing compression for a directory + * and set index root accordingly + */ + settable |= FILE_ATTR_COMPRESSED; + if ((ni->flags ^ cpu_to_le32(attrib)) + & FILE_ATTR_COMPRESSED) { + if (ni->flags & FILE_ATTR_COMPRESSED) + dirflags = const_cpu_to_le16(0); + else + dirflags = ATTR_IS_COMPRESSED; + res = ntfs_attr_set_flags(ni, + AT_INDEX_ROOT, + NTFS_INDEX_I30, 4, + dirflags, + ATTR_COMPRESSION_MASK); + } + } + if (!res) { + ni->flags = (ni->flags & ~settable) + | (cpu_to_le32(attrib) & settable); + NInoSetDirty(ni); + NInoFileNameSetDirty(ni); + } + if (!ntfs_inode_close(ni)) + res = -1; + } else + errno = ENOENT; + } + return (res); +} + + +BOOL ntfs_read_directory(struct SECURITY_API *scapi, + const char *path, ntfs_filldir_t callback, void *context) +{ + ntfs_inode *ni; + BOOL ok; + s64 pos; + + ok = FALSE; /* default return */ + if (scapi && (scapi->magic == MAGIC_API) && callback) { + ni = ntfs_pathname_to_inode(scapi->security.vol, NULL, path); + if (ni) { + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + pos = 0; + ntfs_readdir(ni,&pos,context,callback); + ok = !ntfs_inode_close(ni); + } else { + ntfs_inode_close(ni); + errno = ENOTDIR; + } + } else + errno = ENOENT; + } else + errno = EINVAL; /* do not clear *psize */ + return (ok); +} + +/* + * read $SDS (for auditing security data) + * + * Returns the number or read bytes, or -1 if there is an error + */ + +int ntfs_read_sds(struct SECURITY_API *scapi, + char *buf, u32 size, u32 offset) +{ + int got; + + got = -1; /* default return */ + if (scapi && (scapi->magic == MAGIC_API)) { + if (scapi->security.vol->secure_ni) + got = ntfs_attr_data_read(scapi->security.vol->secure_ni, + STREAM_SDS, 4, buf, size, offset); + else + errno = EOPNOTSUPP; + } else + errno = EINVAL; + return (got); +} + +/* + * read $SII (for auditing security data) + * + * Returns next entry, or NULL if there is an error + */ + +INDEX_ENTRY *ntfs_read_sii(struct SECURITY_API *scapi, + INDEX_ENTRY *entry) +{ + SII_INDEX_KEY key; + INDEX_ENTRY *ret; + BOOL found; + ntfs_index_context *xsii; + + ret = (INDEX_ENTRY*)NULL; /* default return */ + if (scapi && (scapi->magic == MAGIC_API)) { + xsii = scapi->security.vol->secure_xsii; + if (xsii) { + if (!entry) { + key.security_id = const_cpu_to_le32(0); + found = !ntfs_index_lookup((char*)&key, + sizeof(SII_INDEX_KEY), xsii); + /* not supposed to find */ + if (!found && (errno == ENOENT)) + ret = xsii->entry; + } else + ret = ntfs_index_next(entry,xsii); + if (!ret) + errno = ENODATA; + } else + errno = EOPNOTSUPP; + } else + errno = EINVAL; + return (ret); +} + +/* + * read $SDH (for auditing security data) + * + * Returns next entry, or NULL if there is an error + */ + +INDEX_ENTRY *ntfs_read_sdh(struct SECURITY_API *scapi, + INDEX_ENTRY *entry) +{ + SDH_INDEX_KEY key; + INDEX_ENTRY *ret; + BOOL found; + ntfs_index_context *xsdh; + + ret = (INDEX_ENTRY*)NULL; /* default return */ + if (scapi && (scapi->magic == MAGIC_API)) { + xsdh = scapi->security.vol->secure_xsdh; + if (xsdh) { + if (!entry) { + key.hash = const_cpu_to_le32(0); + key.security_id = const_cpu_to_le32(0); + found = !ntfs_index_lookup((char*)&key, + sizeof(SDH_INDEX_KEY), xsdh); + /* not supposed to find */ + if (!found && (errno == ENOENT)) + ret = xsdh->entry; + } else + ret = ntfs_index_next(entry,xsdh); + if (!ret) + errno = ENODATA; + } else errno = ENOTSUP; + } else + errno = EINVAL; + return (ret); +} + +/* + * Get the mapped user SID + * A buffer of 40 bytes has to be supplied + * + * returns the size of the SID, or zero and errno set if not found + */ + +int ntfs_get_usid(struct SECURITY_API *scapi, uid_t uid, char *buf) +{ + const SID *usid; + BIGSID defusid; + int size; + + size = 0; + if (scapi && (scapi->magic == MAGIC_API)) { + usid = ntfs_find_usid(scapi->security.mapping[MAPUSERS], uid, (SID*)&defusid); + if (usid) { + size = ntfs_sid_size(usid); + memcpy(buf,usid,size); + } else + errno = ENODATA; + } else + errno = EINVAL; + return (size); +} + +/* + * Get the mapped group SID + * A buffer of 40 bytes has to be supplied + * + * returns the size of the SID, or zero and errno set if not found + */ + +int ntfs_get_gsid(struct SECURITY_API *scapi, gid_t gid, char *buf) +{ + const SID *gsid; + BIGSID defgsid; + int size; + + size = 0; + if (scapi && (scapi->magic == MAGIC_API)) { + gsid = ntfs_find_gsid(scapi->security.mapping[MAPGROUPS], gid, (SID*)&defgsid); + if (gsid) { + size = ntfs_sid_size(gsid); + memcpy(buf,gsid,size); + } else + errno = ENODATA; + } else + errno = EINVAL; + return (size); +} + +/* + * Get the user mapped to a SID + * + * returns the uid, or -1 if not found + */ + +int ntfs_get_user(struct SECURITY_API *scapi, const SID *usid) +{ + int uid; + + uid = -1; + if (scapi && (scapi->magic == MAGIC_API) && ntfs_valid_sid(usid)) { + if (ntfs_same_sid(usid,adminsid)) + uid = 0; + else { + uid = ntfs_find_user(scapi->security.mapping[MAPUSERS], usid); + if (!uid) { + uid = -1; + errno = ENODATA; + } + } + } else + errno = EINVAL; + return (uid); +} + +/* + * Get the group mapped to a SID + * + * returns the uid, or -1 if not found + */ + +int ntfs_get_group(struct SECURITY_API *scapi, const SID *gsid) +{ + int gid; + + gid = -1; + if (scapi && (scapi->magic == MAGIC_API) && ntfs_valid_sid(gsid)) { + if (ntfs_same_sid(gsid,adminsid)) + gid = 0; + else { + gid = ntfs_find_group(scapi->security.mapping[MAPGROUPS], gsid); + if (!gid) { + gid = -1; + errno = ENODATA; + } + } + } else + errno = EINVAL; + return (gid); +} + +/* + * Initializations before calling ntfs_get_file_security() + * ntfs_set_file_security() and ntfs_read_directory() + * + * Only allowed for root + * + * Returns an (obscured) struct SECURITY_API* needed for further calls + * NULL if not root (EPERM) or device is mounted (EBUSY) + */ + +struct SECURITY_API *ntfs_initialize_file_security(const char *device, + unsigned long flags) +{ + ntfs_volume *vol; + unsigned long mntflag; + int mnt; + struct SECURITY_API *scapi; + struct SECURITY_CONTEXT *scx; + + scapi = (struct SECURITY_API*)NULL; + mnt = ntfs_check_if_mounted(device, &mntflag); + if (!mnt && !(mntflag & NTFS_MF_MOUNTED) && !getuid()) { + vol = ntfs_mount(device, flags); + if (vol) { + scapi = (struct SECURITY_API*) + ntfs_malloc(sizeof(struct SECURITY_API)); + if (!ntfs_volume_get_free_space(vol) + && scapi) { + scapi->magic = MAGIC_API; + scapi->seccache = (struct PERMISSIONS_CACHE*)NULL; + scx = &scapi->security; + scx->vol = vol; + scx->uid = getuid(); + scx->gid = getgid(); + scx->pseccache = &scapi->seccache; + scx->vol->secure_flags = 0; + /* accept no mapping and no $Secure */ + ntfs_build_mapping(scx,(const char*)NULL,TRUE); + } else { + if (scapi) + free(scapi); + else + errno = ENOMEM; + mnt = ntfs_umount(vol,FALSE); + scapi = (struct SECURITY_API*)NULL; + } + } + } else + if (getuid()) + errno = EPERM; + else + errno = EBUSY; + return (scapi); +} + +/* + * Leaving after ntfs_initialize_file_security() + * + * Returns FALSE if FAILED + */ + +BOOL ntfs_leave_file_security(struct SECURITY_API *scapi) +{ + int ok; + ntfs_volume *vol; + + ok = FALSE; + if (scapi && (scapi->magic == MAGIC_API) && scapi->security.vol) { + vol = scapi->security.vol; + ntfs_destroy_security_context(&scapi->security); + free(scapi); + if (!ntfs_umount(vol, 0)) + ok = TRUE; + } + return (ok); +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/unistr.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/unistr.c new file mode 100644 index 0000000..db8ddf4 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/unistr.c @@ -0,0 +1,1757 @@ +/** + * unistr.c - Unicode string handling. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2004 Anton Altaparmakov + * Copyright (c) 2002-2009 Szabolcs Szakacsits + * Copyright (c) 2008-2015 Jean-Pierre Andre + * Copyright (c) 2008 Bernhard Kaindl + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_WCHAR_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_LOCALE_H +#include +#endif + +#if defined(__APPLE__) || defined(__DARWIN__) +#ifdef ENABLE_NFCONV +#include +#endif /* ENABLE_NFCONV */ +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + +#include "compat.h" +#include "attrib.h" +#include "types.h" +#include "unistr.h" +#include "debug.h" +#include "logging.h" +#include "misc.h" + +#ifndef ALLOW_BROKEN_UNICODE +/* Erik allowing broken UTF-16 surrogate pairs and U+FFFE and U+FFFF by default, + * open to debate. */ +#define ALLOW_BROKEN_UNICODE 1 +#endif /* !defined(ALLOW_BROKEN_UNICODE) */ + +/* + * IMPORTANT + * ========= + * + * All these routines assume that the Unicode characters are in little endian + * encoding inside the strings!!! + */ + +static int use_utf8 = 1; /* use UTF-8 encoding for file names */ + +#if defined(__APPLE__) || defined(__DARWIN__) +#ifdef ENABLE_NFCONV +/** + * This variable controls whether or not automatic normalization form conversion + * should be performed when translating NTFS unicode file names to UTF-8. + * Defaults to on, but can be controlled from the outside using the function + * int ntfs_macosx_normalize_filenames(int normalize); + */ +static int nfconvert_utf8 = 1; +#endif /* ENABLE_NFCONV */ +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + +/* + * This is used by the name collation functions to quickly determine what + * characters are (in)valid. + */ +#if 0 +static const u8 legal_ansi_char_array[0x40] = { + 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, + + 0x17, 0x07, 0x18, 0x17, 0x17, 0x17, 0x17, 0x17, + 0x17, 0x17, 0x18, 0x16, 0x16, 0x17, 0x07, 0x00, + + 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, + 0x17, 0x17, 0x04, 0x16, 0x18, 0x16, 0x18, 0x18, +}; +#endif + +/** + * ntfs_names_are_equal - compare two Unicode names for equality + * @s1: name to compare to @s2 + * @s1_len: length in Unicode characters of @s1 + * @s2: name to compare to @s1 + * @s2_len: length in Unicode characters of @s2 + * @ic: ignore case bool + * @upcase: upcase table (only if @ic == IGNORE_CASE) + * @upcase_size: length in Unicode characters of @upcase (if present) + * + * Compare the names @s1 and @s2 and return TRUE (1) if the names are + * identical, or FALSE (0) if they are not identical. If @ic is IGNORE_CASE, + * the @upcase table is used to perform a case insensitive comparison. + */ +BOOL ntfs_names_are_equal(const ntfschar *s1, size_t s1_len, + const ntfschar *s2, size_t s2_len, + const IGNORE_CASE_BOOL ic, + const ntfschar *upcase, const u32 upcase_size) +{ + if (s1_len != s2_len) + return FALSE; + if (!s1_len) + return TRUE; + if (ic == CASE_SENSITIVE) + return ntfs_ucsncmp(s1, s2, s1_len) ? FALSE: TRUE; + return ntfs_ucsncasecmp(s1, s2, s1_len, upcase, upcase_size) ? FALSE: + TRUE; +} + +/* + * ntfs_names_full_collate() fully collate two Unicode names + * + * @name1: first Unicode name to compare + * @name1_len: length of first Unicode name to compare + * @name2: second Unicode name to compare + * @name2_len: length of second Unicode name to compare + * @ic: either CASE_SENSITIVE or IGNORE_CASE (see below) + * @upcase: upcase table + * @upcase_len: upcase table size + * + * If @ic is CASE_SENSITIVE, then the names are compared primarily ignoring + * case, but if the names are equal ignoring case, then they are compared + * case-sensitively. As an example, "abc" would collate before "BCD" (since + * "abc" and "BCD" differ ignoring case and 'A' < 'B') but after "ABC" (since + * "ABC" and "abc" are equal ignoring case and 'A' < 'a'). This matches the + * collation order of filenames as indexed in NTFS directories. + * + * If @ic is IGNORE_CASE, then the names are only compared case-insensitively + * and are considered to match if and only if they are equal ignoring case. + * + * Returns: + * -1 if the first name collates before the second one, + * 0 if the names match, or + * 1 if the second name collates before the first one + */ +int ntfs_names_full_collate(const ntfschar *name1, const u32 name1_len, + const ntfschar *name2, const u32 name2_len, + const IGNORE_CASE_BOOL ic, const ntfschar *upcase, + const u32 upcase_len) +{ + u32 cnt; + u16 c1, c2; + u16 u1, u2; + +#ifdef DEBUG + if (!name1 || !name2 || !upcase || !upcase_len) { + ntfs_log_debug("ntfs_names_collate received NULL pointer!\n"); + exit(1); + } +#endif + cnt = min(name1_len, name2_len); + if (cnt > 0) { + if (ic == CASE_SENSITIVE) { + while (--cnt && (*name1 == *name2)) { + name1++; + name2++; + } + u1 = c1 = le16_to_cpu(*name1); + u2 = c2 = le16_to_cpu(*name2); + if (u1 < upcase_len) + u1 = le16_to_cpu(upcase[u1]); + if (u2 < upcase_len) + u2 = le16_to_cpu(upcase[u2]); + if ((u1 == u2) && cnt) + do { + name1++; + u1 = le16_to_cpu(*name1); + name2++; + u2 = le16_to_cpu(*name2); + if (u1 < upcase_len) + u1 = le16_to_cpu(upcase[u1]); + if (u2 < upcase_len) + u2 = le16_to_cpu(upcase[u2]); + } while ((u1 == u2) && --cnt); + if (u1 < u2) + return -1; + if (u1 > u2) + return 1; + if (name1_len < name2_len) + return -1; + if (name1_len > name2_len) + return 1; + if (c1 < c2) + return -1; + if (c1 > c2) + return 1; + } else { + do { + u1 = le16_to_cpu(*name1); + name1++; + u2 = le16_to_cpu(*name2); + name2++; + if (u1 < upcase_len) + u1 = le16_to_cpu(upcase[u1]); + if (u2 < upcase_len) + u2 = le16_to_cpu(upcase[u2]); + } while ((u1 == u2) && --cnt); + if (u1 < u2) + return -1; + if (u1 > u2) + return 1; + if (name1_len < name2_len) + return -1; + if (name1_len > name2_len) + return 1; + } + } else { + if (name1_len < name2_len) + return -1; + if (name1_len > name2_len) + return 1; + } + return 0; +} + +/** + * ntfs_ucsncmp - compare two little endian Unicode strings + * @s1: first string + * @s2: second string + * @n: maximum unicode characters to compare + * + * Compare the first @n characters of the Unicode strings @s1 and @s2, + * The strings in little endian format and appropriate le16_to_cpu() + * conversion is performed on non-little endian machines. + * + * The function returns an integer less than, equal to, or greater than zero + * if @s1 (or the first @n Unicode characters thereof) is found, respectively, + * to be less than, to match, or be greater than @s2. + */ +int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n) +{ + u16 c1, c2; + size_t i; + +#ifdef DEBUG + if (!s1 || !s2) { + ntfs_log_debug("ntfs_wcsncmp() received NULL pointer!\n"); + exit(1); + } +#endif + for (i = 0; i < n; ++i) { + c1 = le16_to_cpu(s1[i]); + c2 = le16_to_cpu(s2[i]); + if (c1 < c2) + return -1; + if (c1 > c2) + return 1; + if (!c1) + break; + } + return 0; +} + +/** + * ntfs_ucsncasecmp - compare two little endian Unicode strings, ignoring case + * @s1: first string + * @s2: second string + * @n: maximum unicode characters to compare + * @upcase: upcase table + * @upcase_size: upcase table size in Unicode characters + * + * Compare the first @n characters of the Unicode strings @s1 and @s2, + * ignoring case. The strings in little endian format and appropriate + * le16_to_cpu() conversion is performed on non-little endian machines. + * + * Each character is uppercased using the @upcase table before the comparison. + * + * The function returns an integer less than, equal to, or greater than zero + * if @s1 (or the first @n Unicode characters thereof) is found, respectively, + * to be less than, to match, or be greater than @s2. + */ +int ntfs_ucsncasecmp(const ntfschar *s1, const ntfschar *s2, size_t n, + const ntfschar *upcase, const u32 upcase_size) +{ + u16 c1, c2; + size_t i; + +#ifdef DEBUG + if (!s1 || !s2 || !upcase) { + ntfs_log_debug("ntfs_wcsncasecmp() received NULL pointer!\n"); + exit(1); + } +#endif + for (i = 0; i < n; ++i) { + if ((c1 = le16_to_cpu(s1[i])) < upcase_size) + c1 = le16_to_cpu(upcase[c1]); + if ((c2 = le16_to_cpu(s2[i])) < upcase_size) + c2 = le16_to_cpu(upcase[c2]); + if (c1 < c2) + return -1; + if (c1 > c2) + return 1; + if (!c1) + break; + } + return 0; +} + +/** + * ntfs_ucsnlen - determine the length of a little endian Unicode string + * @s: pointer to Unicode string + * @maxlen: maximum length of string @s + * + * Return the number of Unicode characters in the little endian Unicode + * string @s up to a maximum of maxlen Unicode characters, not including + * the terminating (ntfschar)'\0'. If there is no (ntfschar)'\0' between @s + * and @s + @maxlen, @maxlen is returned. + * + * This function never looks beyond @s + @maxlen. + */ +u32 ntfs_ucsnlen(const ntfschar *s, u32 maxlen) +{ + u32 i; + + for (i = 0; i < maxlen; i++) { + if (!le16_to_cpu(s[i])) + break; + } + return i; +} + +/** + * ntfs_ucsndup - duplicate little endian Unicode string + * @s: pointer to Unicode string + * @maxlen: maximum length of string @s + * + * Return a pointer to a new little endian Unicode string which is a duplicate + * of the string s. Memory for the new string is obtained with ntfs_malloc(3), + * and can be freed with free(3). + * + * A maximum of @maxlen Unicode characters are copied and a terminating + * (ntfschar)'\0' little endian Unicode character is added. + * + * This function never looks beyond @s + @maxlen. + * + * Return a pointer to the new little endian Unicode string on success and NULL + * on failure with errno set to the error code. + */ +ntfschar *ntfs_ucsndup(const ntfschar *s, u32 maxlen) +{ + ntfschar *dst; + u32 len; + + len = ntfs_ucsnlen(s, maxlen); + dst = ntfs_malloc((len + 1) * sizeof(ntfschar)); + if (dst) { + memcpy(dst, s, len * sizeof(ntfschar)); + dst[len] = const_cpu_to_le16(L'\0'); + } + return dst; +} + +/** + * ntfs_name_upcase - Map an Unicode name to its uppercase equivalent + * @name: + * @name_len: + * @upcase: + * @upcase_len: + * + * Description... + * + * Returns: + */ +void ntfs_name_upcase(ntfschar *name, u32 name_len, const ntfschar *upcase, + const u32 upcase_len) +{ + u32 i; + u16 u; + + for (i = 0; i < name_len; i++) + if ((u = le16_to_cpu(name[i])) < upcase_len) + name[i] = upcase[u]; +} + +/** + * ntfs_name_locase - Map a Unicode name to its lowercase equivalent + */ +void ntfs_name_locase(ntfschar *name, u32 name_len, const ntfschar *locase, + const u32 locase_len) +{ + u32 i; + u16 u; + + if (locase) + for (i = 0; i < name_len; i++) + if ((u = le16_to_cpu(name[i])) < locase_len) + name[i] = locase[u]; +} + +/** + * ntfs_file_value_upcase - Convert a filename to upper case + * @file_name_attr: + * @upcase: + * @upcase_len: + * + * Description... + * + * Returns: + */ +void ntfs_file_value_upcase(FILE_NAME_ATTR *file_name_attr, + const ntfschar *upcase, const u32 upcase_len) +{ + ntfs_name_upcase((ntfschar*)&file_name_attr->file_name, + file_name_attr->file_name_length, upcase, upcase_len); +} + +/* + NTFS uses Unicode (UTF-16LE [NTFS-3G uses UCS-2LE, which is enough + for now]) for path names, but the Unicode code points need to be + converted before a path can be accessed under NTFS. For 7 bit ASCII/ANSI, + glibc does this even without a locale in a hard-coded fashion as that + appears to be is easy because the low 7-bit ASCII range appears to be + available in all charsets but it does not convert anything if + there was some error with the locale setup or none set up like + when mount is called during early boot where he (by policy) do + not use locales (and may be not available if /usr is not yet mounted), + so this patch fixes the resulting issues for systems which use + UTF-8 and for others, specifying the locale in fstab brings them + the encoding which they want. + + If no locale is defined or there was a problem with setting one + up and whenever nl_langinfo(CODESET) returns a sting starting with + "ANSI", use an internal UCS-2LE <-> UTF-8 codeset converter to fix + the bug where NTFS-3G does not show any path names which include + international characters!!! (and also fails on creating them) as result. + + Author: Bernhard Kaindl + Jean-Pierre Andre made it compliant with RFC3629/RFC2781. +*/ + +/* + * Return the number of bytes in UTF-8 needed (without the terminating null) to + * store the given UTF-16LE string. + * + * On error, -1 is returned, and errno is set to the error code. The following + * error codes can be expected: + * EILSEQ The input string is not valid UTF-16LE (only possible + * if compiled without ALLOW_BROKEN_UNICODE). + * ENAMETOOLONG The length of the UTF-8 string in bytes (without the + * terminating null) would exceed @outs_len. + */ +static int utf16_to_utf8_size(const ntfschar *ins, const int ins_len, int outs_len) +{ + int i, ret = -1; + int count = 0; + BOOL surrog; + + surrog = FALSE; + for (i = 0; i < ins_len && ins[i] && count <= outs_len; i++) { + unsigned short c = le16_to_cpu(ins[i]); + if (surrog) { + if ((c >= 0xdc00) && (c < 0xe000)) { + surrog = FALSE; + count += 4; + } else { +#if ALLOW_BROKEN_UNICODE + /* The first UTF-16 unit of a surrogate pair has + * a value between 0xd800 and 0xdc00. It can be + * encoded as an individual UTF-8 sequence if we + * cannot combine it with the next UTF-16 unit + * unit as a surrogate pair. */ + surrog = FALSE; + count += 3; + + --i; + continue; +#else + goto fail; +#endif /* ALLOW_BROKEN_UNICODE */ + } + } else + if (c < 0x80) + count++; + else if (c < 0x800) + count += 2; + else if (c < 0xd800) + count += 3; + else if (c < 0xdc00) + surrog = TRUE; +#if ALLOW_BROKEN_UNICODE + else if (c < 0xe000) + count += 3; + else if (c >= 0xe000) +#else + else if ((c >= 0xe000) && (c < 0xfffe)) +#endif /* ALLOW_BROKEN_UNICODE */ + count += 3; + else + goto fail; + } + + if (surrog && count <= outs_len) { +#if ALLOW_BROKEN_UNICODE + count += 3; /* ending with a single surrogate */ +#else + goto fail; +#endif /* ALLOW_BROKEN_UNICODE */ + } + + if (count > outs_len) { + errno = ENAMETOOLONG; + goto out; + } + + ret = count; +out: + return ret; +fail: + errno = EILSEQ; + goto out; +} + +/* + * ntfs_utf16_to_utf8 - convert a little endian UTF16LE string to an UTF-8 string + * @ins: input utf16 string buffer + * @ins_len: length of input string in utf16 characters + * @outs: on return contains the (allocated) output multibyte string + * @outs_len: length of output buffer in bytes (ignored if *@outs is NULL) + * + * Return -1 with errno set if string has invalid byte sequence or too long. + */ +static int ntfs_utf16_to_utf8(const ntfschar *ins, const int ins_len, + char **outs, int outs_len) +{ +#if defined(__APPLE__) || defined(__DARWIN__) +#ifdef ENABLE_NFCONV + char *original_outs_value = *outs; + int original_outs_len = outs_len; +#endif /* ENABLE_NFCONV */ +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + + char *t; + int i, size, ret = -1; + int halfpair; + + halfpair = 0; + if (!*outs) { + /* If no output buffer was provided, we will allocate one and + * limit its length to PATH_MAX. Note: we follow the standard + * convention of PATH_MAX including the terminating null. */ + outs_len = PATH_MAX; + } + + /* The size *with* the terminating null is limited to @outs_len, + * so the size *without* the terminating null is limited to one less. */ + size = utf16_to_utf8_size(ins, ins_len, outs_len - 1); + + if (size < 0) + goto out; + + if (!*outs) { + outs_len = size + 1; + *outs = ntfs_malloc(outs_len); + if (!*outs) + goto out; + } + + t = *outs; + + for (i = 0; i < ins_len && ins[i]; i++) { + unsigned short c = le16_to_cpu(ins[i]); + /* size not double-checked */ + if (halfpair) { + if ((c >= 0xdc00) && (c < 0xe000)) { + *t++ = 0xf0 + (((halfpair + 64) >> 8) & 7); + *t++ = 0x80 + (((halfpair + 64) >> 2) & 63); + *t++ = 0x80 + ((c >> 6) & 15) + ((halfpair & 3) << 4); + *t++ = 0x80 + (c & 63); + halfpair = 0; + } else { +#if ALLOW_BROKEN_UNICODE + /* The first UTF-16 unit of a surrogate pair has + * a value between 0xd800 and 0xdc00. It can be + * encoded as an individual UTF-8 sequence if we + * cannot combine it with the next UTF-16 unit + * unit as a surrogate pair. */ + *t++ = 0xe0 | (halfpair >> 12); + *t++ = 0x80 | ((halfpair >> 6) & 0x3f); + *t++ = 0x80 | (halfpair & 0x3f); + halfpair = 0; + + --i; + continue; +#else + goto fail; +#endif /* ALLOW_BROKEN_UNICODE */ + } + } else if (c < 0x80) { + *t++ = c; + } else { + if (c < 0x800) { + *t++ = (0xc0 | ((c >> 6) & 0x3f)); + *t++ = 0x80 | (c & 0x3f); + } else if (c < 0xd800) { + *t++ = 0xe0 | (c >> 12); + *t++ = 0x80 | ((c >> 6) & 0x3f); + *t++ = 0x80 | (c & 0x3f); + } else if (c < 0xdc00) + halfpair = c; +#if ALLOW_BROKEN_UNICODE + else if (c < 0xe000) { + *t++ = 0xe0 | (c >> 12); + *t++ = 0x80 | ((c >> 6) & 0x3f); + *t++ = 0x80 | (c & 0x3f); + } +#endif /* ALLOW_BROKEN_UNICODE */ + else if (c >= 0xe000) { + *t++ = 0xe0 | (c >> 12); + *t++ = 0x80 | ((c >> 6) & 0x3f); + *t++ = 0x80 | (c & 0x3f); + } else + goto fail; + } + } +#if ALLOW_BROKEN_UNICODE + if (halfpair) { /* ending with a single surrogate */ + *t++ = 0xe0 | (halfpair >> 12); + *t++ = 0x80 | ((halfpair >> 6) & 0x3f); + *t++ = 0x80 | (halfpair & 0x3f); + } +#endif /* ALLOW_BROKEN_UNICODE */ + *t = '\0'; + +#if defined(__APPLE__) || defined(__DARWIN__) +#ifdef ENABLE_NFCONV + if(nfconvert_utf8 && (t - *outs) > 0) { + char *new_outs = NULL; + int new_outs_len = ntfs_macosx_normalize_utf8(*outs, &new_outs, 0); // Normalize to decomposed form + if(new_outs_len >= 0 && new_outs != NULL) { + if(original_outs_value != *outs) { + // We have allocated outs ourselves. + free(*outs); + *outs = new_outs; + t = *outs + new_outs_len; + } + else { + // We need to copy new_outs into the fixed outs buffer. + memset(*outs, 0, original_outs_len); + strncpy(*outs, new_outs, original_outs_len-1); + t = *outs + original_outs_len; + free(new_outs); + } + } + else { + ntfs_log_error("Failed to normalize NTFS string to UTF-8 NFD: %s\n", *outs); + ntfs_log_error(" new_outs=0x%p\n", new_outs); + ntfs_log_error(" new_outs_len=%d\n", new_outs_len); + } + } +#endif /* ENABLE_NFCONV */ +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + + ret = t - *outs; +out: + return ret; +fail: + errno = EILSEQ; + goto out; +} + +/* + * Return the amount of 16-bit elements in UTF-16LE needed + * (without the terminating null) to store given UTF-8 string. + * + * Return -1 with errno set if it's longer than PATH_MAX or string is invalid. + * + * Note: This does not check whether the input sequence is a valid utf8 string, + * and should be used only in context where such check is made! + */ +static int utf8_to_utf16_size(const char *s) +{ + int ret = -1; + unsigned int byte; + size_t count = 0; + + while ((byte = *((const unsigned char *)s++))) { + if (++count >= PATH_MAX) + goto fail; + if (byte >= 0xc0) { + if (byte >= 0xF5) { + errno = EILSEQ; + goto out; + } + if (!*s) + break; + if (byte >= 0xC0) + s++; + if (!*s) + break; + if (byte >= 0xE0) + s++; + if (!*s) + break; + if (byte >= 0xF0) { + s++; + if (++count >= PATH_MAX) + goto fail; + } + } + } + ret = count; +out: + return ret; +fail: + errno = ENAMETOOLONG; + goto out; +} +/* + * This converts one UTF-8 sequence to cpu-endian Unicode value + * within range U+0 .. U+10ffff and excluding U+D800 .. U+DFFF + * + * Return the number of used utf8 bytes or -1 with errno set + * if sequence is invalid. + */ +static int utf8_to_unicode(u32 *wc, const char *s) +{ + unsigned int byte = *((const unsigned char *)s); + + /* single byte */ + if (byte == 0) { + *wc = (u32) 0; + return 0; + } else if (byte < 0x80) { + *wc = (u32) byte; + return 1; + /* double byte */ + } else if (byte < 0xc2) { + goto fail; + } else if (byte < 0xE0) { + if ((s[1] & 0xC0) == 0x80) { + *wc = ((u32)(byte & 0x1F) << 6) + | ((u32)(s[1] & 0x3F)); + return 2; + } else + goto fail; + /* three-byte */ + } else if (byte < 0xF0) { + if (((s[1] & 0xC0) == 0x80) && ((s[2] & 0xC0) == 0x80)) { + *wc = ((u32)(byte & 0x0F) << 12) + | ((u32)(s[1] & 0x3F) << 6) + | ((u32)(s[2] & 0x3F)); + /* Check valid ranges */ +#if ALLOW_BROKEN_UNICODE + if (((*wc >= 0x800) && (*wc <= 0xD7FF)) + || ((*wc >= 0xD800) && (*wc <= 0xDFFF)) + || ((*wc >= 0xe000) && (*wc <= 0xFFFF))) + return 3; +#else + if (((*wc >= 0x800) && (*wc <= 0xD7FF)) + || ((*wc >= 0xe000) && (*wc <= 0xFFFD))) + return 3; +#endif /* ALLOW_BROKEN_UNICODE */ + } + goto fail; + /* four-byte */ + } else if (byte < 0xF5) { + if (((s[1] & 0xC0) == 0x80) && ((s[2] & 0xC0) == 0x80) + && ((s[3] & 0xC0) == 0x80)) { + *wc = ((u32)(byte & 0x07) << 18) + | ((u32)(s[1] & 0x3F) << 12) + | ((u32)(s[2] & 0x3F) << 6) + | ((u32)(s[3] & 0x3F)); + /* Check valid ranges */ + if ((*wc <= 0x10ffff) && (*wc >= 0x10000)) + return 4; + } + goto fail; + } +fail: + errno = EILSEQ; + return -1; +} + +/** + * ntfs_utf8_to_utf16 - convert a UTF-8 string to a UTF-16LE string + * @ins: input multibyte string buffer + * @outs: on return contains the (allocated) output utf16 string + * @outs_len: length of output buffer in utf16 characters + * + * Return -1 with errno set. + */ +static int ntfs_utf8_to_utf16(const char *ins, ntfschar **outs) +{ +#if defined(__APPLE__) || defined(__DARWIN__) +#ifdef ENABLE_NFCONV + char *new_ins = NULL; + if(nfconvert_utf8) { + int new_ins_len; + new_ins_len = ntfs_macosx_normalize_utf8(ins, &new_ins, 1); // Normalize to composed form + if(new_ins_len >= 0) + ins = new_ins; + else + ntfs_log_error("Failed to normalize NTFS string to UTF-8 NFC: %s\n", ins); + } +#endif /* ENABLE_NFCONV */ +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + const char *t = ins; + u32 wc; + BOOL allocated; + ntfschar *outpos; + int shorts, ret = -1; + + shorts = utf8_to_utf16_size(ins); + if (shorts < 0) + goto fail; + + allocated = FALSE; + if (!*outs) { + *outs = ntfs_malloc((shorts + 1) * sizeof(ntfschar)); + if (!*outs) + goto fail; + allocated = TRUE; + } + + outpos = *outs; + + while(1) { + int m = utf8_to_unicode(&wc, t); + if (m <= 0) { + if (m < 0) { + /* do not leave space allocated if failed */ + if (allocated) { + free(*outs); + *outs = (ntfschar*)NULL; + } + goto fail; + } + *outpos++ = const_cpu_to_le16(0); + break; + } + if (wc < 0x10000) + *outpos++ = cpu_to_le16(wc); + else { + wc -= 0x10000; + *outpos++ = cpu_to_le16((wc >> 10) + 0xd800); + *outpos++ = cpu_to_le16((wc & 0x3ff) + 0xdc00); + } + t += m; + } + + ret = --outpos - *outs; +fail: +#if defined(__APPLE__) || defined(__DARWIN__) +#ifdef ENABLE_NFCONV + if(new_ins != NULL) + free(new_ins); +#endif /* ENABLE_NFCONV */ +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + return ret; +} + +/** + * ntfs_ucstombs - convert a little endian Unicode string to a multibyte string + * @ins: input Unicode string buffer + * @ins_len: length of input string in Unicode characters + * @outs: on return contains the (allocated) output multibyte string + * @outs_len: length of output buffer in bytes (ignored if *@outs is NULL) + * + * Convert the input little endian, 2-byte Unicode string @ins, of length + * @ins_len into the multibyte string format dictated by the current locale. + * + * If *@outs is NULL, the function allocates the string and the caller is + * responsible for calling free(*@outs); when finished with it. + * + * On success the function returns the number of bytes written to the output + * string *@outs (>= 0), not counting the terminating NULL byte. If the output + * string buffer was allocated, *@outs is set to it. + * + * On error, -1 is returned, and errno is set to the error code. The following + * error codes can be expected: + * EINVAL Invalid arguments (e.g. @ins or @outs is NULL). + * EILSEQ The input string cannot be represented as a multibyte + * sequence according to the current locale. + * ENAMETOOLONG Destination buffer is too small for input string. + * ENOMEM Not enough memory to allocate destination buffer. + */ +int ntfs_ucstombs(const ntfschar *ins, const int ins_len, char **outs, + int outs_len) +{ + char *mbs; + int mbs_len; +#ifdef MB_CUR_MAX + wchar_t wc; + int i, o; + int cnt = 0; +#ifdef HAVE_MBSINIT + mbstate_t mbstate; +#endif +#endif /* MB_CUR_MAX */ + + if (!ins || !outs) { + errno = EINVAL; + return -1; + } + mbs = *outs; + mbs_len = outs_len; + if (mbs && !mbs_len) { + errno = ENAMETOOLONG; + return -1; + } + if (use_utf8) + return ntfs_utf16_to_utf8(ins, ins_len, outs, outs_len); +#ifdef MB_CUR_MAX + if (!mbs) { + mbs_len = (ins_len + 1) * MB_CUR_MAX; + mbs = ntfs_malloc(mbs_len); + if (!mbs) + return -1; + } +#ifdef HAVE_MBSINIT + memset(&mbstate, 0, sizeof(mbstate)); +#else + wctomb(NULL, 0); +#endif + for (i = o = 0; i < ins_len; i++) { + /* Reallocate memory if necessary or abort. */ + if ((int)(o + MB_CUR_MAX) > mbs_len) { + char *tc; + if (mbs == *outs) { + errno = ENAMETOOLONG; + return -1; + } + tc = ntfs_malloc((mbs_len + 64) & ~63); + if (!tc) + goto err_out; + memcpy(tc, mbs, mbs_len); + mbs_len = (mbs_len + 64) & ~63; + free(mbs); + mbs = tc; + } + /* Convert the LE Unicode character to a CPU wide character. */ + wc = (wchar_t)le16_to_cpu(ins[i]); + if (!wc) + break; + /* Convert the CPU endian wide character to multibyte. */ +#ifdef HAVE_MBSINIT + cnt = wcrtomb(mbs + o, wc, &mbstate); +#else + cnt = wctomb(mbs + o, wc); +#endif + if (cnt == -1) + goto err_out; + if (cnt <= 0) { + ntfs_log_debug("Eeek. cnt <= 0, cnt = %i\n", cnt); + errno = EINVAL; + goto err_out; + } + o += cnt; + } +#ifdef HAVE_MBSINIT + /* Make sure we are back in the initial state. */ + if (!mbsinit(&mbstate)) { + ntfs_log_debug("Eeek. mbstate not in initial state!\n"); + errno = EILSEQ; + goto err_out; + } +#endif + /* Now write the NULL character. */ + mbs[o] = '\0'; + if (*outs != mbs) + *outs = mbs; + return o; +err_out: + if (mbs != *outs) { + int eo = errno; + free(mbs); + errno = eo; + } +#else /* MB_CUR_MAX */ + errno = EILSEQ; +#endif /* MB_CUR_MAX */ + return -1; +} + +/** + * ntfs_mbstoucs - convert a multibyte string to a little endian Unicode string + * @ins: input multibyte string buffer + * @outs: on return contains the (allocated) output Unicode string + * + * Convert the input multibyte string @ins, from the current locale into the + * corresponding little endian, 2-byte Unicode string. + * + * The function allocates the string and the caller is responsible for calling + * free(*@outs); when finished with it. + * + * On success the function returns the number of Unicode characters written to + * the output string *@outs (>= 0), not counting the terminating Unicode NULL + * character. + * + * On error, -1 is returned, and errno is set to the error code. The following + * error codes can be expected: + * EINVAL Invalid arguments (e.g. @ins or @outs is NULL). + * EILSEQ The input string cannot be represented as a Unicode + * string according to the current locale. + * ENAMETOOLONG Destination buffer is too small for input string. + * ENOMEM Not enough memory to allocate destination buffer. + */ +int ntfs_mbstoucs(const char *ins, ntfschar **outs) +{ +#ifdef MB_CUR_MAX + ntfschar *ucs; + const char *s; + wchar_t wc; + int i, o, cnt, ins_len, ucs_len, ins_size; +#ifdef HAVE_MBSINIT + mbstate_t mbstate; +#endif +#endif /* MB_CUR_MAX */ + + if (!ins || !outs) { + errno = EINVAL; + return -1; + } + + if (use_utf8) + return ntfs_utf8_to_utf16(ins, outs); + +#ifdef MB_CUR_MAX + /* Determine the size of the multi-byte string in bytes. */ + ins_size = strlen(ins); + /* Determine the length of the multi-byte string. */ + s = ins; +#if defined(HAVE_MBSINIT) + memset(&mbstate, 0, sizeof(mbstate)); + ins_len = mbsrtowcs(NULL, (const char **)&s, 0, &mbstate); +#ifdef __CYGWIN32__ + if (!ins_len && *ins) { + /* Older Cygwin had broken mbsrtowcs() implementation. */ + ins_len = strlen(ins); + } +#endif +#elif !defined(DJGPP) + ins_len = mbstowcs(NULL, s, 0); +#else + /* Eeek!!! DJGPP has broken mbstowcs() implementation!!! */ + ins_len = strlen(ins); +#endif + if (ins_len == -1) + return ins_len; +#ifdef HAVE_MBSINIT + if ((s != ins) || !mbsinit(&mbstate)) { +#else + if (s != ins) { +#endif + errno = EILSEQ; + return -1; + } + /* Add the NULL terminator. */ + ins_len++; + ucs_len = ins_len; + ucs = ntfs_malloc(ucs_len * sizeof(ntfschar)); + if (!ucs) + return -1; +#ifdef HAVE_MBSINIT + memset(&mbstate, 0, sizeof(mbstate)); +#else + mbtowc(NULL, NULL, 0); +#endif + for (i = o = cnt = 0; i < ins_size; i += cnt, o++) { + /* Reallocate memory if necessary. */ + if (o >= ucs_len) { + ntfschar *tc; + ucs_len = (ucs_len * sizeof(ntfschar) + 64) & ~63; + tc = realloc(ucs, ucs_len); + if (!tc) + goto err_out; + ucs = tc; + ucs_len /= sizeof(ntfschar); + } + /* Convert the multibyte character to a wide character. */ +#ifdef HAVE_MBSINIT + cnt = mbrtowc(&wc, ins + i, ins_size - i, &mbstate); +#else + cnt = mbtowc(&wc, ins + i, ins_size - i); +#endif + if (!cnt) + break; + if (cnt == -1) + goto err_out; + if (cnt < -1) { + ntfs_log_trace("Eeek. cnt = %i\n", cnt); + errno = EINVAL; + goto err_out; + } + /* Make sure we are not overflowing the NTFS Unicode set. */ + if ((unsigned long)wc >= (unsigned long)(1 << + (8 * sizeof(ntfschar)))) { + errno = EILSEQ; + goto err_out; + } + /* Convert the CPU wide character to a LE Unicode character. */ + ucs[o] = cpu_to_le16(wc); + } +#ifdef HAVE_MBSINIT + /* Make sure we are back in the initial state. */ + if (!mbsinit(&mbstate)) { + ntfs_log_trace("Eeek. mbstate not in initial state!\n"); + errno = EILSEQ; + goto err_out; + } +#endif + /* Now write the NULL character. */ + ucs[o] = const_cpu_to_le16(L'\0'); + *outs = ucs; + return o; +err_out: + free(ucs); +#else /* MB_CUR_MAX */ + errno = EILSEQ; +#endif /* MB_CUR_MAX */ + return -1; +} + +/* + * Turn a UTF8 name uppercase + * + * Returns an allocated uppercase name which has to be freed by caller + * or NULL if there is an error (described by errno) + */ + +char *ntfs_uppercase_mbs(const char *low, + const ntfschar *upcase, u32 upcase_size) +{ + int size; + char *upp; + u32 wc; + int n; + const char *s; + char *t; + + size = strlen(low); + upp = (char*)ntfs_malloc(3*size + 1); + if (upp) { + s = low; + t = upp; + do { + n = utf8_to_unicode(&wc, s); + if (n > 0) { + if (wc < upcase_size) + wc = le16_to_cpu(upcase[wc]); + if (wc < 0x80) + *t++ = wc; + else if (wc < 0x800) { + *t++ = (0xc0 | ((wc >> 6) & 0x3f)); + *t++ = 0x80 | (wc & 0x3f); + } else if (wc < 0x10000) { + *t++ = 0xe0 | (wc >> 12); + *t++ = 0x80 | ((wc >> 6) & 0x3f); + *t++ = 0x80 | (wc & 0x3f); + } else { + *t++ = 0xf0 | ((wc >> 18) & 7); + *t++ = 0x80 | ((wc >> 12) & 63); + *t++ = 0x80 | ((wc >> 6) & 0x3f); + *t++ = 0x80 | (wc & 0x3f); + } + s += n; + } + } while (n > 0); + if (n < 0) { + free(upp); + upp = (char*)NULL; + errno = EILSEQ; + } else { + *t = 0; + } + } + return (upp); +} + +/** + * ntfs_upcase_table_build - build the default upcase table for NTFS + * @uc: destination buffer where to store the built table + * @uc_len: size of destination buffer in bytes + * + * ntfs_upcase_table_build() builds the default upcase table for NTFS and + * stores it in the caller supplied buffer @uc of size @uc_len. + * + * Note, @uc_len must be at least 128kiB in size or bad things will happen! + */ +void ntfs_upcase_table_build(ntfschar *uc, u32 uc_len) +{ + struct NEWUPPERCASE { + unsigned short first; + unsigned short last; + short diff; + unsigned char step; + unsigned char osmajor; + unsigned char osminor; + } ; + + /* + * This is the table as defined by Windows XP + */ + static int uc_run_table[][3] = { /* Start, End, Add */ + {0x0061, 0x007B, -32}, {0x0451, 0x045D, -80}, {0x1F70, 0x1F72, 74}, + {0x00E0, 0x00F7, -32}, {0x045E, 0x0460, -80}, {0x1F72, 0x1F76, 86}, + {0x00F8, 0x00FF, -32}, {0x0561, 0x0587, -48}, {0x1F76, 0x1F78, 100}, + {0x0256, 0x0258, -205}, {0x1F00, 0x1F08, 8}, {0x1F78, 0x1F7A, 128}, + {0x028A, 0x028C, -217}, {0x1F10, 0x1F16, 8}, {0x1F7A, 0x1F7C, 112}, + {0x03AC, 0x03AD, -38}, {0x1F20, 0x1F28, 8}, {0x1F7C, 0x1F7E, 126}, + {0x03AD, 0x03B0, -37}, {0x1F30, 0x1F38, 8}, {0x1FB0, 0x1FB2, 8}, + {0x03B1, 0x03C2, -32}, {0x1F40, 0x1F46, 8}, {0x1FD0, 0x1FD2, 8}, + {0x03C2, 0x03C3, -31}, {0x1F51, 0x1F52, 8}, {0x1FE0, 0x1FE2, 8}, + {0x03C3, 0x03CC, -32}, {0x1F53, 0x1F54, 8}, {0x1FE5, 0x1FE6, 7}, + {0x03CC, 0x03CD, -64}, {0x1F55, 0x1F56, 8}, {0x2170, 0x2180, -16}, + {0x03CD, 0x03CF, -63}, {0x1F57, 0x1F58, 8}, {0x24D0, 0x24EA, -26}, + {0x0430, 0x0450, -32}, {0x1F60, 0x1F68, 8}, {0xFF41, 0xFF5B, -32}, + {0} + }; + static int uc_dup_table[][2] = { /* Start, End */ + {0x0100, 0x012F}, {0x01A0, 0x01A6}, {0x03E2, 0x03EF}, {0x04CB, 0x04CC}, + {0x0132, 0x0137}, {0x01B3, 0x01B7}, {0x0460, 0x0481}, {0x04D0, 0x04EB}, + {0x0139, 0x0149}, {0x01CD, 0x01DD}, {0x0490, 0x04BF}, {0x04EE, 0x04F5}, + {0x014A, 0x0178}, {0x01DE, 0x01EF}, {0x04BF, 0x04BF}, {0x04F8, 0x04F9}, + {0x0179, 0x017E}, {0x01F4, 0x01F5}, {0x04C1, 0x04C4}, {0x1E00, 0x1E95}, + {0x018B, 0x018B}, {0x01FA, 0x0218}, {0x04C7, 0x04C8}, {0x1EA0, 0x1EF9}, + {0} + }; + static int uc_byte_table[][2] = { /* Offset, Value */ + {0x00FF, 0x0178}, {0x01AD, 0x01AC}, {0x01F3, 0x01F1}, {0x0269, 0x0196}, + {0x0183, 0x0182}, {0x01B0, 0x01AF}, {0x0253, 0x0181}, {0x026F, 0x019C}, + {0x0185, 0x0184}, {0x01B9, 0x01B8}, {0x0254, 0x0186}, {0x0272, 0x019D}, + {0x0188, 0x0187}, {0x01BD, 0x01BC}, {0x0259, 0x018F}, {0x0275, 0x019F}, + {0x018C, 0x018B}, {0x01C6, 0x01C4}, {0x025B, 0x0190}, {0x0283, 0x01A9}, + {0x0192, 0x0191}, {0x01C9, 0x01C7}, {0x0260, 0x0193}, {0x0288, 0x01AE}, + {0x0199, 0x0198}, {0x01CC, 0x01CA}, {0x0263, 0x0194}, {0x0292, 0x01B7}, + {0x01A8, 0x01A7}, {0x01DD, 0x018E}, {0x0268, 0x0197}, + {0} + }; + +/* + * Changes which were applied to later Windows versions + * + * md5 for $UpCase from Winxp : 6fa3db2468275286210751e869d36373 + * Vista : 2f03b5a69d486ff3864cecbd07f24440 + * Win8 : 7ff498a44e45e77374cc7c962b1b92f2 + */ + static const struct NEWUPPERCASE newuppercase[] = { + /* from Windows 6.0 (Vista) */ + { 0x37b, 0x37d, 0x82, 1, 6, 0 }, + { 0x1f80, 0x1f87, 0x8, 1, 6, 0 }, + { 0x1f90, 0x1f97, 0x8, 1, 6, 0 }, + { 0x1fa0, 0x1fa7, 0x8, 1, 6, 0 }, + { 0x2c30, 0x2c5e, -0x30, 1, 6, 0 }, + { 0x2d00, 0x2d25, -0x1c60, 1, 6, 0 }, + { 0x2c68, 0x2c6c, -0x1, 2, 6, 0 }, + { 0x219, 0x21f, -0x1, 2, 6, 0 }, + { 0x223, 0x233, -0x1, 2, 6, 0 }, + { 0x247, 0x24f, -0x1, 2, 6, 0 }, + { 0x3d9, 0x3e1, -0x1, 2, 6, 0 }, + { 0x48b, 0x48f, -0x1, 2, 6, 0 }, + { 0x4fb, 0x513, -0x1, 2, 6, 0 }, + { 0x2c81, 0x2ce3, -0x1, 2, 6, 0 }, + { 0x3f8, 0x3fb, -0x1, 3, 6, 0 }, + { 0x4c6, 0x4ce, -0x1, 4, 6, 0 }, + { 0x23c, 0x242, -0x1, 6, 6, 0 }, + { 0x4ed, 0x4f7, -0x1, 10, 6, 0 }, + { 0x450, 0x45d, -0x50, 13, 6, 0 }, + { 0x2c61, 0x2c76, -0x1, 21, 6, 0 }, + { 0x1fcc, 0x1ffc, -0x9, 48, 6, 0 }, + { 0x180, 0x180, 0xc3, 1, 6, 0 }, + { 0x195, 0x195, 0x61, 1, 6, 0 }, + { 0x19a, 0x19a, 0xa3, 1, 6, 0 }, + { 0x19e, 0x19e, 0x82, 1, 6, 0 }, + { 0x1bf, 0x1bf, 0x38, 1, 6, 0 }, + { 0x1f9, 0x1f9, -0x1, 1, 6, 0 }, + { 0x23a, 0x23a, 0x2a2b, 1, 6, 0 }, + { 0x23e, 0x23e, 0x2a28, 1, 6, 0 }, + { 0x26b, 0x26b, 0x29f7, 1, 6, 0 }, + { 0x27d, 0x27d, 0x29e7, 1, 6, 0 }, + { 0x280, 0x280, -0xda, 1, 6, 0 }, + { 0x289, 0x289, -0x45, 1, 6, 0 }, + { 0x28c, 0x28c, -0x47, 1, 6, 0 }, + { 0x3f2, 0x3f2, 0x7, 1, 6, 0 }, + { 0x4cf, 0x4cf, -0xf, 1, 6, 0 }, + { 0x1d7d, 0x1d7d, 0xee6, 1, 6, 0 }, + { 0x1fb3, 0x1fb3, 0x9, 1, 6, 0 }, + { 0x214e, 0x214e, -0x1c, 1, 6, 0 }, + { 0x2184, 0x2184, -0x1, 1, 6, 0 }, + /* from Windows 6.1 (Win7) */ + { 0x23a, 0x23e, 0x0, 4, 6, 1 }, + { 0x250, 0x250, 0x2a1f, 2, 6, 1 }, + { 0x251, 0x251, 0x2a1c, 2, 6, 1 }, + { 0x271, 0x271, 0x29fd, 2, 6, 1 }, + { 0x371, 0x373, -0x1, 2, 6, 1 }, + { 0x377, 0x377, -0x1, 2, 6, 1 }, + { 0x3c2, 0x3c2, 0x0, 2, 6, 1 }, + { 0x3d7, 0x3d7, -0x8, 2, 6, 1 }, + { 0x515, 0x523, -0x1, 2, 6, 1 }, + /* below, -0x75fc stands for 0x8a04 and truncation */ + { 0x1d79, 0x1d79, -0x75fc, 2, 6, 1 }, + { 0x1efb, 0x1eff, -0x1, 2, 6, 1 }, + { 0x1fc3, 0x1ff3, 0x9, 48, 6, 1 }, + { 0x1fcc, 0x1ffc, 0x0, 48, 6, 1 }, + { 0x2c65, 0x2c65, -0x2a2b, 2, 6, 1 }, + { 0x2c66, 0x2c66, -0x2a28, 2, 6, 1 }, + { 0x2c73, 0x2c73, -0x1, 2, 6, 1 }, + { 0xa641, 0xa65f, -0x1, 2, 6, 1 }, + { 0xa663, 0xa66d, -0x1, 2, 6, 1 }, + { 0xa681, 0xa697, -0x1, 2, 6, 1 }, + { 0xa723, 0xa72f, -0x1, 2, 6, 1 }, + { 0xa733, 0xa76f, -0x1, 2, 6, 1 }, + { 0xa77a, 0xa77c, -0x1, 2, 6, 1 }, + { 0xa77f, 0xa787, -0x1, 2, 6, 1 }, + { 0xa78c, 0xa78c, -0x1, 2, 6, 1 }, + /* end mark */ + { 0 } + } ; + + int i, r; + int k, off; + const struct NEWUPPERCASE *puc; + + memset((char*)uc, 0, uc_len); + uc_len >>= 1; + if (uc_len > 65536) + uc_len = 65536; + for (i = 0; (u32)i < uc_len; i++) + uc[i] = cpu_to_le16(i); + for (r = 0; uc_run_table[r][0]; r++) { + off = uc_run_table[r][2]; + for (i = uc_run_table[r][0]; i < uc_run_table[r][1]; i++) + uc[i] = cpu_to_le16(i + off); + } + for (r = 0; uc_dup_table[r][0]; r++) + for (i = uc_dup_table[r][0]; i < uc_dup_table[r][1]; i += 2) + uc[i + 1] = cpu_to_le16(i); + for (r = 0; uc_byte_table[r][0]; r++) { + k = uc_byte_table[r][1]; + uc[uc_byte_table[r][0]] = cpu_to_le16(k); + } + for (r=0; newuppercase[r].first; r++) { + puc = &newuppercase[r]; + if ((puc->osmajor < UPCASE_MAJOR) + || ((puc->osmajor == UPCASE_MAJOR) + && (puc->osminor <= UPCASE_MINOR))) { + off = puc->diff; + for (i = puc->first; i <= puc->last; i += puc->step) + uc[i] = cpu_to_le16(i + off); + } + } +} + +/* + * Allocate and build the default upcase table + * + * Returns the number of entries + * 0 if failed + */ + +#define UPCASE_LEN 65536 /* default number of entries in upcase */ + +u32 ntfs_upcase_build_default(ntfschar **upcase) +{ + u32 upcase_len = 0; + + *upcase = (ntfschar*)ntfs_malloc(UPCASE_LEN*2); + if (*upcase) { + ntfs_upcase_table_build(*upcase, UPCASE_LEN*2); + upcase_len = UPCASE_LEN; + } + return (upcase_len); +} + +/* + * Build a table for converting to lower case + * + * This is only meaningful when there is a single lower case + * character leading to an upper case one, and currently the + * only exception is the greek letter sigma which has a single + * upper case glyph (code U+03A3), but two lower case glyphs + * (code U+03C3 and U+03C2, the latter to be used at the end + * of a word). In the following implementation the upper case + * sigma will be lowercased as U+03C3. + */ + +ntfschar *ntfs_locase_table_build(const ntfschar *uc, u32 uc_cnt) +{ + ntfschar *lc; + u32 upp; + u32 i; + + lc = (ntfschar*)ntfs_malloc(uc_cnt*sizeof(ntfschar)); + if (lc) { + for (i=0; i NTFS_MAX_NAME_LEN) { + free(ucs); + errno = ENAMETOOLONG; + return NULL; + } + if (!ucs || !*len) { + ucs = AT_UNNAMED; + *len = 0; + } + return ucs; +} + +/** + * ntfs_ucsfree - free memory allocated by ntfs_str2ucs() + * @ucs input string to be freed + * + * Free memory at @ucs and which was allocated by ntfs_str2ucs. + * + * Return value: none. + */ +void ntfs_ucsfree(ntfschar *ucs) +{ + if (ucs && (ucs != AT_UNNAMED)) + free(ucs); +} + +/* + * Check whether a name contains no chars forbidden + * for DOS or Win32 use + * + * If @strict is TRUE, then trailing dots and spaces are forbidden. + * These names are technically allowed in the Win32 namespace, but + * they can be problematic. See comment for FILE_NAME_WIN32. + * + * If there is a bad char, errno is set to EINVAL + */ + +BOOL ntfs_forbidden_chars(const ntfschar *name, int len, BOOL strict) +{ + BOOL forbidden; + int ch; + int i; + static const u32 mainset = (1L << ('\"' - 0x20)) + | (1L << ('*' - 0x20)) + | (1L << ('/' - 0x20)) + | (1L << (':' - 0x20)) + | (1L << ('<' - 0x20)) + | (1L << ('>' - 0x20)) + | (1L << ('?' - 0x20)); + + forbidden = (len == 0) || + (strict && (name[len-1] == const_cpu_to_le16(' ') || + name[len-1] == const_cpu_to_le16('.'))); + for (i=0; i= 3)) { + /* + * Rough hash check to tell whether the first couple of chars + * may be one of CO PR AU NU LP or lowercase variants. + */ + h = ((le16_to_cpu(name[0]) & 31)*48) + ^ ((le16_to_cpu(name[1]) & 31)*165); + if ((h % 23) == 17) { + /* do a full check, depending on the third char */ + switch (le16_to_cpu(name[2]) & ~0x20) { + case 'N' : + if (((len == 3) || (name[3] == dot)) + && (!ntfs_ucsncasecmp(name, con, 3, + vol->upcase, vol->upcase_len) + || !ntfs_ucsncasecmp(name, prn, 3, + vol->upcase, vol->upcase_len))) + forbidden = TRUE; + break; + case 'X' : + if (((len == 3) || (name[3] == dot)) + && !ntfs_ucsncasecmp(name, aux, 3, + vol->upcase, vol->upcase_len)) + forbidden = TRUE; + break; + case 'L' : + if (((len == 3) || (name[3] == dot)) + && !ntfs_ucsncasecmp(name, nul, 3, + vol->upcase, vol->upcase_len)) + forbidden = TRUE; + break; + case 'M' : + if ((len > 3) + && (le16_to_cpu(name[3]) >= '1') + && (le16_to_cpu(name[3]) <= '9') + && ((len == 4) || (name[4] == dot)) + && !ntfs_ucsncasecmp(name, com, 3, + vol->upcase, vol->upcase_len)) + forbidden = TRUE; + break; + case 'T' : + if ((len > 3) + && (le16_to_cpu(name[3]) >= '1') + && (le16_to_cpu(name[3]) <= '9') + && ((len == 4) || (name[4] == dot)) + && !ntfs_ucsncasecmp(name, lpt, 3, + vol->upcase, vol->upcase_len)) + forbidden = TRUE; + break; + } + } + } + + if (forbidden) + errno = EINVAL; + return (forbidden); +} + +/* + * Check whether the same name can be used as a DOS and + * a Win32 name + * + * The names must be the same, or the short name the uppercase + * variant of the long name + */ + +BOOL ntfs_collapsible_chars(ntfs_volume *vol, + const ntfschar *shortname, int shortlen, + const ntfschar *longname, int longlen) +{ + BOOL collapsible; + unsigned int ch; + unsigned int cs; + int i; + + collapsible = shortlen == longlen; + for (i=0; collapsible && (i= vol->upcase_len) + || (cs >= vol->upcase_len) + || (vol->upcase[cs] != vol->upcase[ch]))) + collapsible = FALSE; + } + return (collapsible); +} + +/* + * Define the character encoding to be used. + * Use UTF-8 unless specified otherwise. + */ + +int ntfs_set_char_encoding(const char *locale) +{ + use_utf8 = 0; + if (!locale || strstr(locale,"utf8") || strstr(locale,"UTF8") + || strstr(locale,"utf-8") || strstr(locale,"UTF-8")) + use_utf8 = 1; + else + if (setlocale(LC_ALL, locale)) + use_utf8 = 0; + else { + ntfs_log_error("Invalid locale, encoding to UTF-8\n"); + use_utf8 = 1; + } + return 0; /* always successful */ +} + +#if defined(__APPLE__) || defined(__DARWIN__) + +int ntfs_macosx_normalize_filenames(int normalize) { +#ifdef ENABLE_NFCONV + if (normalize == 0 || normalize == 1) { + nfconvert_utf8 = normalize; + return 0; + } + else { + return -1; + } +#else + return -1; +#endif /* ENABLE_NFCONV */ +} + +int ntfs_macosx_normalize_utf8(const char *utf8_string, char **target, + int composed) +{ +#ifdef ENABLE_NFCONV + /* For this code to compile, the CoreFoundation framework must be fed to + * the linker. */ + CFStringRef cfSourceString; + CFMutableStringRef cfMutableString; + CFRange rangeToProcess; + CFIndex requiredBufferLength; + char *result = NULL; + int resultLength = -1; + + /* Convert the UTF-8 string to a CFString. */ + cfSourceString = CFStringCreateWithCString(kCFAllocatorDefault, + utf8_string, kCFStringEncodingUTF8); + if (cfSourceString == NULL) { + ntfs_log_error("CFStringCreateWithCString failed!\n"); + return -2; + } + + /* Create a mutable string from cfSourceString that we are free to + * modify. */ + cfMutableString = CFStringCreateMutableCopy(kCFAllocatorDefault, 0, + cfSourceString); + CFRelease(cfSourceString); /* End-of-life. */ + if (cfMutableString == NULL) { + ntfs_log_error("CFStringCreateMutableCopy failed!\n"); + return -3; + } + + /* Normalize the mutable string to the desired normalization form. */ + CFStringNormalize(cfMutableString, (composed != 0 ? + kCFStringNormalizationFormC : kCFStringNormalizationFormD)); + + /* Store the resulting string in a '\0'-terminated UTF-8 encoded char* + * buffer. */ + rangeToProcess = CFRangeMake(0, CFStringGetLength(cfMutableString)); + if (CFStringGetBytes(cfMutableString, rangeToProcess, + kCFStringEncodingUTF8, 0, false, NULL, 0, + &requiredBufferLength) > 0) + { + resultLength = sizeof(char) * (requiredBufferLength + 1); + result = ntfs_calloc(resultLength); + + if (result != NULL) { + if (CFStringGetBytes(cfMutableString, rangeToProcess, + kCFStringEncodingUTF8, 0, false, + (UInt8*) result, resultLength - 1, + &requiredBufferLength) <= 0) + { + ntfs_log_error("Could not perform UTF-8 " + "conversion of normalized " + "CFMutableString.\n"); + free(result); + result = NULL; + } + } + else { + ntfs_log_error("Could not perform a ntfs_calloc of %d " + "bytes for char *result.\n", resultLength); + } + } + else { + ntfs_log_error("Could not perform check for required length of " + "UTF-8 conversion of normalized CFMutableString.\n"); + } + + CFRelease(cfMutableString); + + if (result != NULL) { + *target = result; + return resultLength - 1; + } + else { + return -1; + } +#else + return -1; +#endif /* ENABLE_NFCONV */ +} +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/unix_io.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/unix_io.c new file mode 100644 index 0000000..5495a6a --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/unix_io.c @@ -0,0 +1,392 @@ +/** + * unix_io.c - Unix style disk io functions. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2006 Anton Altaparmakov + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_SYS_IOCTL_H +#include +#endif +#ifdef HAVE_LINUX_FD_H +#include +#endif +#ifdef HAVE_LINUX_FS_H +#include +#endif + +#include "types.h" +#include "mst.h" +#include "debug.h" +#include "device.h" +#include "logging.h" +#include "misc.h" + +#define DEV_FD(dev) (*(int *)dev->d_private) + +/* Define to nothing if not present on this system. */ +#ifndef O_EXCL +# define O_EXCL 0 +#endif + +/** + * fsync replacement which makes every effort to try to get the data down to + * disk, using different means for different operating systems. Specifically, + * it issues the proper fcntl for Mac OS X or does fsync where it is available + * or as a last resort calls the fsync function. Information on this problem + * was retrieved from: + * http://mirror.linux.org.au/pub/linux.conf.au/2007/video/talks/278.pdf + */ +static int ntfs_fsync(int fildes) +{ + int ret = -1; +#if defined(__APPLE__) || defined(__DARWIN__) +# ifndef F_FULLFSYNC +# error "Mac OS X: F_FULLFSYNC is not defined. Either you didn't include fcntl.h or you're using an older, unsupported version of Mac OS X (pre-10.3)." +# endif + /* + * Apple has disabled fsync() for internal disk drives in OS X. + * To force a synchronization of disk contents, we use a Mac OS X + * specific fcntl, F_FULLFSYNC. + */ + ret = fcntl(fildes, F_FULLFSYNC, NULL); + if (ret) { + /* + * If we are not on a file system that supports this, + * then fall back to a plain fsync. + */ + ret = fsync(fildes); + } +#else + ret = fsync(fildes); +#endif + return ret; +} + +/** + * ntfs_device_unix_io_open - Open a device and lock it exclusively + * @dev: + * @flags: + * + * Description... + * + * Returns: + */ +static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags) +{ + struct flock flk; + struct stat sbuf; + int err; + + if (NDevOpen(dev)) { + errno = EBUSY; + return -1; + } + if (stat(dev->d_name, &sbuf)) { + ntfs_log_perror("Failed to access '%s'", dev->d_name); + return -1; + } + if (S_ISBLK(sbuf.st_mode)) + NDevSetBlock(dev); + + dev->d_private = ntfs_malloc(sizeof(int)); + if (!dev->d_private) + return -1; + /* + * Open file for exclusive access if mounting r/w. + * Fuseblk takes care about block devices. + */ + if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR) + flags |= O_EXCL; + *(int*)dev->d_private = open(dev->d_name, flags); + if (*(int*)dev->d_private == -1) { + err = errno; + /* if permission error and rw, retry read-only */ + if ((err == EACCES) && ((flags & O_RDWR) == O_RDWR)) + err = EROFS; + goto err_out; + } +#ifdef HAVE_LINUX_FS_H + /* Check whether the device was forced read-only */ + if (NDevBlock(dev) && ((flags & O_RDWR) == O_RDWR)) { + int r; + int state; + + r = ioctl(DEV_FD(dev), BLKROGET, &state); + if (!r && state) { + err = EROFS; + if (close(DEV_FD(dev))) + err = errno; + goto err_out; + } + } +#endif + + if ((flags & O_RDWR) != O_RDWR) + NDevSetReadOnly(dev); + + memset(&flk, 0, sizeof(flk)); + if (NDevReadOnly(dev)) + flk.l_type = F_RDLCK; + else + flk.l_type = F_WRLCK; + flk.l_whence = SEEK_SET; + flk.l_start = flk.l_len = 0LL; + if (fcntl(DEV_FD(dev), F_SETLK, &flk)) { + err = errno; + ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ? + "read" : "write", dev->d_name); + if (close(DEV_FD(dev))) + ntfs_log_perror("Failed to close '%s'", dev->d_name); + goto err_out; + } + + NDevSetOpen(dev); + return 0; +err_out: + free(dev->d_private); + dev->d_private = NULL; + errno = err; + return -1; +} + +/** + * ntfs_device_unix_io_close - Close the device, releasing the lock + * @dev: + * + * Description... + * + * Returns: + */ +static int ntfs_device_unix_io_close(struct ntfs_device *dev) +{ + struct flock flk; + + if (!NDevOpen(dev)) { + errno = EBADF; + ntfs_log_perror("Device %s is not open", dev->d_name); + return -1; + } + if (NDevDirty(dev)) + if (ntfs_fsync(DEV_FD(dev))) { + ntfs_log_perror("Failed to fsync device %s", dev->d_name); + return -1; + } + + memset(&flk, 0, sizeof(flk)); + flk.l_type = F_UNLCK; + flk.l_whence = SEEK_SET; + flk.l_start = flk.l_len = 0LL; + if (fcntl(DEV_FD(dev), F_SETLK, &flk)) + ntfs_log_perror("Could not unlock %s", dev->d_name); + if (close(DEV_FD(dev))) { + ntfs_log_perror("Failed to close device %s", dev->d_name); + return -1; + } + NDevClearOpen(dev); + free(dev->d_private); + dev->d_private = NULL; + return 0; +} + +/** + * ntfs_device_unix_io_seek - Seek to a place on the device + * @dev: + * @offset: + * @whence: + * + * Description... + * + * Returns: + */ +static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset, + int whence) +{ + return lseek(DEV_FD(dev), offset, whence); +} + +/** + * ntfs_device_unix_io_read - Read from the device, from the current location + * @dev: + * @buf: + * @count: + * + * Description... + * + * Returns: + */ +static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf, + s64 count) +{ + return read(DEV_FD(dev), buf, count); +} + +/** + * ntfs_device_unix_io_write - Write to the device, at the current location + * @dev: + * @buf: + * @count: + * + * Description... + * + * Returns: + */ +static s64 ntfs_device_unix_io_write(struct ntfs_device *dev, const void *buf, + s64 count) +{ + if (NDevReadOnly(dev)) { + errno = EROFS; + return -1; + } + NDevSetDirty(dev); + return write(DEV_FD(dev), buf, count); +} + +/** + * ntfs_device_unix_io_pread - Perform a positioned read from the device + * @dev: + * @buf: + * @count: + * @offset: + * + * Description... + * + * Returns: + */ +static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf, + s64 count, s64 offset) +{ + return pread(DEV_FD(dev), buf, count, offset); +} + +/** + * ntfs_device_unix_io_pwrite - Perform a positioned write to the device + * @dev: + * @buf: + * @count: + * @offset: + * + * Description... + * + * Returns: + */ +static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf, + s64 count, s64 offset) +{ + if (NDevReadOnly(dev)) { + errno = EROFS; + return -1; + } + NDevSetDirty(dev); + return pwrite(DEV_FD(dev), buf, count, offset); +} + +/** + * ntfs_device_unix_io_sync - Flush any buffered changes to the device + * @dev: + * + * Description... + * + * Returns: + */ +static int ntfs_device_unix_io_sync(struct ntfs_device *dev) +{ + int res = 0; + + if (!NDevReadOnly(dev)) { + res = ntfs_fsync(DEV_FD(dev)); + if (res) + ntfs_log_perror("Failed to sync device %s", dev->d_name); + else + NDevClearDirty(dev); + } + return res; +} + +/** + * ntfs_device_unix_io_stat - Get information about the device + * @dev: + * @buf: + * + * Description... + * + * Returns: + */ +static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf) +{ + return fstat(DEV_FD(dev), buf); +} + +/** + * ntfs_device_unix_io_ioctl - Perform an ioctl on the device + * @dev: + * @request: + * @argp: + * + * Description... + * + * Returns: + */ +static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev, + unsigned long request, void *argp) +{ + return ioctl(DEV_FD(dev), request, argp); +} + +/** + * Device operations for working with unix style devices and files. + */ +struct ntfs_device_operations ntfs_device_unix_io_ops = { + .open = ntfs_device_unix_io_open, + .close = ntfs_device_unix_io_close, + .seek = ntfs_device_unix_io_seek, + .read = ntfs_device_unix_io_read, + .write = ntfs_device_unix_io_write, + .pread = ntfs_device_unix_io_pread, + .pwrite = ntfs_device_unix_io_pwrite, + .sync = ntfs_device_unix_io_sync, + .stat = ntfs_device_unix_io_stat, + .ioctl = ntfs_device_unix_io_ioctl, +}; diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/volume.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/volume.c new file mode 100644 index 0000000..a6d467b --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/volume.c @@ -0,0 +1,2013 @@ +/** + * volume.c - NTFS volume handling code. Originated from the Linux-NTFS project. + * + * Copyright (c) 2000-2006 Anton Altaparmakov + * Copyright (c) 2002-2009 Szabolcs Szakacsits + * Copyright (c) 2004-2005 Richard Russon + * Copyright (c) 2010 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif +#ifdef HAVE_LOCALE_H +#include +#endif + +#if defined(__sun) && defined (__SVR4) +#include +#endif + +#include "param.h" +#include "compat.h" +#include "volume.h" +#include "attrib.h" +#include "mft.h" +#include "bootsect.h" +#include "device.h" +#include "debug.h" +#include "inode.h" +#include "runlist.h" +#include "logfile.h" +#include "dir.h" +#include "logging.h" +#include "cache.h" +#include "realpath.h" +#include "misc.h" +#include "security.h" + +const char *ntfs_home = +"News, support and information: https://github.com/tuxera/ntfs-3g/\n"; + +static const char *invalid_ntfs_msg = +"The device '%s' doesn't seem to have a valid NTFS.\n" +"Maybe the wrong device is used? Or the whole disk instead of a\n" +"partition (e.g. /dev/sda, not /dev/sda1)? Or the other way around?\n"; + +static const char *corrupt_volume_msg = +"NTFS is either inconsistent, or there is a hardware fault, or it's a\n" +"SoftRAID/FakeRAID hardware. In the first case run chkdsk /f on Windows\n" +"then reboot into Windows twice. The usage of the /f parameter is very\n" +"important! If the device is a SoftRAID/FakeRAID then first activate\n" +"it and mount a different device under the /dev/mapper/ directory, (e.g.\n" +"/dev/mapper/nvidia_eahaabcc1). Please see the 'dmraid' documentation\n" +"for more details.\n"; + +static const char *hibernated_volume_msg = +"The NTFS partition is in an unsafe state. Please resume and shutdown\n" +"Windows fully (no hibernation or fast restarting), or mount the volume\n" +"read-only with the 'ro' mount option.\n"; + +static const char *fallback_readonly_msg = +"Falling back to read-only mount because the NTFS partition is in an\n" +"unsafe state. Please resume and shutdown Windows fully (no hibernation\n" +"or fast restarting.)\n"; + +static const char *unclean_journal_msg = +"Write access is denied because the disk wasn't safely powered\n" +"off and the 'norecover' mount option was specified.\n"; + +static const char *opened_volume_msg = +"Mount is denied because the NTFS volume is already exclusively opened.\n" +"The volume may be already mounted, or another software may use it which\n" +"could be identified for example by the help of the 'fuser' command.\n"; + +static const char *fakeraid_msg = +"Either the device is missing or it's powered down, or you have\n" +"SoftRAID hardware and must use an activated, different device under\n" +"/dev/mapper/, (e.g. /dev/mapper/nvidia_eahaabcc1) to mount NTFS.\n" +"Please see the 'dmraid' documentation for help.\n"; + +static const char *access_denied_msg = +"Please check '%s' and the ntfs-3g binary permissions,\n" +"and the mounting user ID. More explanation is provided at\n" +"https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ\n"; + +/** + * ntfs_volume_alloc - Create an NTFS volume object and initialise it + * + * Description... + * + * Returns: + */ +ntfs_volume *ntfs_volume_alloc(void) +{ + return ntfs_calloc(sizeof(ntfs_volume)); +} + +static void ntfs_attr_free(ntfs_attr **na) +{ + if (na && *na) { + ntfs_attr_close(*na); + *na = NULL; + } +} + +static int ntfs_inode_free(ntfs_inode **ni) +{ + int ret = -1; + + if (ni && *ni) { + ret = ntfs_inode_close(*ni); + *ni = NULL; + } + + return ret; +} + +static void ntfs_error_set(int *err) +{ + if (!*err) + *err = errno; +} + +/** + * __ntfs_volume_release - Destroy an NTFS volume object + * @v: + * + * Description... + * + * Returns: + */ +static int __ntfs_volume_release(ntfs_volume *v) +{ + int err = 0; + + if (ntfs_close_secure(v)) + ntfs_error_set(&err); + + if (ntfs_inode_free(&v->vol_ni)) + ntfs_error_set(&err); + /* + * FIXME: Inodes must be synced before closing + * attributes, otherwise unmount could fail. + */ + if (v->lcnbmp_ni && NInoDirty(v->lcnbmp_ni)) + ntfs_inode_sync(v->lcnbmp_ni); + ntfs_attr_free(&v->lcnbmp_na); + if (ntfs_inode_free(&v->lcnbmp_ni)) + ntfs_error_set(&err); + + if (v->mft_ni && NInoDirty(v->mft_ni)) + ntfs_inode_sync(v->mft_ni); + ntfs_attr_free(&v->mftbmp_na); + ntfs_attr_free(&v->mft_na); + if (ntfs_inode_free(&v->mft_ni)) + ntfs_error_set(&err); + + if (v->mftmirr_ni && NInoDirty(v->mftmirr_ni)) + ntfs_inode_sync(v->mftmirr_ni); + ntfs_attr_free(&v->mftmirr_na); + if (ntfs_inode_free(&v->mftmirr_ni)) + ntfs_error_set(&err); + + if (v->dev) { + struct ntfs_device *dev = v->dev; + + if (dev->d_ops->sync(dev)) + ntfs_error_set(&err); + if (dev->d_ops->close(dev)) + ntfs_error_set(&err); + } + + ntfs_free_lru_caches(v); + free(v->vol_name); + free(v->upcase); + if (v->locase) free(v->locase); + free(v->attrdef); + free(v); + + errno = err; + return errno ? -1 : 0; +} + +static int ntfs_attr_setup_flag(ntfs_inode *ni) +{ + STANDARD_INFORMATION *si; + s64 lth; + int r; + + si = (STANDARD_INFORMATION*)ntfs_attr_readall(ni, + AT_STANDARD_INFORMATION, AT_UNNAMED, 0, <h); + if (si) { + if ((u64)lth >= offsetof(STANDARD_INFORMATION, owner_id)) + ni->flags = si->file_attributes; + free(si); + r = 0; + } else { + ntfs_log_error("Failed to get standard information of $MFT\n"); + r = -1; + } + return (r); +} + +/** + * ntfs_mft_load - load the $MFT and setup the ntfs volume with it + * @vol: ntfs volume whose $MFT to load + * + * Load $MFT from @vol and setup @vol with it. After calling this function the + * volume @vol is ready for use by all read access functions provided by the + * ntfs library. + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +static int ntfs_mft_load(ntfs_volume *vol) +{ + VCN next_vcn, last_vcn, highest_vcn; + s64 l; + MFT_RECORD *mb = NULL; + ntfs_attr_search_ctx *ctx = NULL; + ATTR_RECORD *a; + int eo; + + /* Manually setup an ntfs_inode. */ + vol->mft_ni = ntfs_inode_allocate(vol); + mb = ntfs_malloc(vol->mft_record_size); + if (!vol->mft_ni || !mb) { + ntfs_log_perror("Error allocating memory for $MFT"); + goto error_exit; + } + vol->mft_ni->mft_no = 0; + vol->mft_ni->mrec = mb; + /* Can't use any of the higher level functions yet! */ + l = ntfs_mst_pread(vol->dev, vol->mft_lcn << vol->cluster_size_bits, 1, + vol->mft_record_size, mb); + if (l != 1) { + if (l != -1) + errno = EIO; + ntfs_log_perror("Error reading $MFT"); + goto error_exit; + } + + if (ntfs_mft_record_check(vol, 0, mb)) + goto error_exit; + + ctx = ntfs_attr_get_search_ctx(vol->mft_ni, NULL); + if (!ctx) + goto error_exit; + + /* Find the $ATTRIBUTE_LIST attribute in $MFT if present. */ + if (ntfs_attr_lookup(AT_ATTRIBUTE_LIST, AT_UNNAMED, 0, 0, 0, NULL, 0, + ctx)) { + if (errno != ENOENT) { + ntfs_log_error("$MFT has corrupt attribute list.\n"); + goto io_error_exit; + } + goto mft_has_no_attr_list; + } + NInoSetAttrList(vol->mft_ni); + l = ntfs_get_attribute_value_length(ctx->attr); + if (l <= 0 || l > 0x40000) { + ntfs_log_error("$MFT/$ATTR_LIST invalid length (%lld).\n", + (long long)l); + goto io_error_exit; + } + vol->mft_ni->attr_list_size = l; + vol->mft_ni->attr_list = ntfs_malloc(l); + if (!vol->mft_ni->attr_list) + goto error_exit; + + l = ntfs_get_attribute_value(vol, ctx->attr, vol->mft_ni->attr_list); + if (!l) { + ntfs_log_error("Failed to get value of $MFT/$ATTR_LIST.\n"); + goto io_error_exit; + } + if ((l != vol->mft_ni->attr_list_size) + || (l < (s64)offsetof(ATTR_LIST_ENTRY, name))) { + ntfs_log_error("Partial read of $MFT/$ATTR_LIST (%lld != " + "%u or < %d).\n", (long long)l, + vol->mft_ni->attr_list_size, + (int)offsetof(ATTR_LIST_ENTRY, name)); + goto io_error_exit; + } + +mft_has_no_attr_list: + + if (ntfs_attr_setup_flag(vol->mft_ni)) + goto error_exit; + + /* We now have a fully setup ntfs inode for $MFT in vol->mft_ni. */ + + /* Get an ntfs attribute for $MFT/$DATA and set it up, too. */ + vol->mft_na = ntfs_attr_open(vol->mft_ni, AT_DATA, AT_UNNAMED, 0); + if (!vol->mft_na) { + ntfs_log_perror("Failed to open ntfs attribute"); + goto error_exit; + } + /* Read all extents from the $DATA attribute in $MFT. */ + ntfs_attr_reinit_search_ctx(ctx); + last_vcn = vol->mft_na->allocated_size >> vol->cluster_size_bits; + highest_vcn = next_vcn = 0; + a = NULL; + while (!ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, next_vcn, NULL, 0, + ctx)) { + runlist_element *nrl; + + a = ctx->attr; + /* $MFT must be non-resident. */ + if (!a->non_resident) { + ntfs_log_error("$MFT must be non-resident.\n"); + goto io_error_exit; + } + /* $MFT must be uncompressed and unencrypted. */ + if (a->flags & ATTR_COMPRESSION_MASK || + a->flags & ATTR_IS_ENCRYPTED) { + ntfs_log_error("$MFT must be uncompressed and " + "unencrypted.\n"); + goto io_error_exit; + } + /* + * Decompress the mapping pairs array of this extent and merge + * the result into the existing runlist. No need for locking + * as we have exclusive access to the inode at this time and we + * are a mount in progress task, too. + */ + nrl = ntfs_mapping_pairs_decompress(vol, a, vol->mft_na->rl); + if (!nrl) { + ntfs_log_perror("ntfs_mapping_pairs_decompress() failed"); + goto error_exit; + } + /* Make sure $DATA is the MFT itself */ + if (nrl->lcn != vol->mft_lcn) { + ntfs_log_perror("The MFT is not self-contained"); + goto error_exit; + } + vol->mft_na->rl = nrl; + + /* Get the lowest vcn for the next extent. */ + highest_vcn = sle64_to_cpu(a->highest_vcn); + next_vcn = highest_vcn + 1; + + /* Only one extent or error, which we catch below. */ + if (next_vcn <= 0) + break; + + /* Avoid endless loops due to corruption. */ + if (next_vcn < sle64_to_cpu(a->lowest_vcn)) { + ntfs_log_error("$MFT has corrupt attribute list.\n"); + goto io_error_exit; + } + } + if (!a) { + ntfs_log_error("$MFT/$DATA attribute not found.\n"); + goto io_error_exit; + } + if (highest_vcn && highest_vcn != last_vcn - 1) { + ntfs_log_error("Failed to load runlist for $MFT/$DATA.\n"); + ntfs_log_error("highest_vcn = 0x%llx, last_vcn - 1 = 0x%llx\n", + (long long)highest_vcn, (long long)last_vcn - 1); + goto io_error_exit; + } + /* Done with the $Mft mft record. */ + ntfs_attr_put_search_ctx(ctx); + ctx = NULL; + + /* Update the size fields in the inode. */ + vol->mft_ni->data_size = vol->mft_na->data_size; + vol->mft_ni->allocated_size = vol->mft_na->allocated_size; + set_nino_flag(vol->mft_ni, KnownSize); + + /* + * The volume is now setup so we can use all read access functions. + */ + vol->mftbmp_na = ntfs_attr_open(vol->mft_ni, AT_BITMAP, AT_UNNAMED, 0); + if (!vol->mftbmp_na) { + ntfs_log_perror("Failed to open $MFT/$BITMAP"); + goto error_exit; + } + return 0; +io_error_exit: + errno = EIO; +error_exit: + eo = errno; + if (ctx) + ntfs_attr_put_search_ctx(ctx); + if (vol->mft_na) { + ntfs_attr_close(vol->mft_na); + vol->mft_na = NULL; + } + if (vol->mft_ni) { + ntfs_inode_close(vol->mft_ni); + vol->mft_ni = NULL; + } + errno = eo; + return -1; +} + +/** + * ntfs_mftmirr_load - load the $MFTMirr and setup the ntfs volume with it + * @vol: ntfs volume whose $MFTMirr to load + * + * Load $MFTMirr from @vol and setup @vol with it. After calling this function + * the volume @vol is ready for use by all write access functions provided by + * the ntfs library (assuming ntfs_mft_load() has been called successfully + * beforehand). + * + * Return 0 on success and -1 on error with errno set to the error code. + */ +static int ntfs_mftmirr_load(ntfs_volume *vol) +{ + int err; + + vol->mftmirr_ni = ntfs_inode_open(vol, FILE_MFTMirr); + if (!vol->mftmirr_ni) { + ntfs_log_perror("Failed to open inode $MFTMirr"); + return -1; + } + + vol->mftmirr_na = ntfs_attr_open(vol->mftmirr_ni, AT_DATA, AT_UNNAMED, 0); + if (!vol->mftmirr_na) { + ntfs_log_perror("Failed to open $MFTMirr/$DATA"); + goto error_exit; + } + + if (ntfs_attr_map_runlist(vol->mftmirr_na, 0) < 0) { + ntfs_log_perror("Failed to map runlist of $MFTMirr/$DATA"); + goto error_exit; + } + if (vol->mftmirr_na->rl->lcn != vol->mftmirr_lcn) { + ntfs_log_error("Bad $MFTMirr lcn 0x%llx, want 0x%llx\n", + (long long)vol->mftmirr_na->rl->lcn, + (long long)vol->mftmirr_lcn); + goto error_exit; + } + + return 0; + +error_exit: + err = errno; + if (vol->mftmirr_na) { + ntfs_attr_close(vol->mftmirr_na); + vol->mftmirr_na = NULL; + } + ntfs_inode_close(vol->mftmirr_ni); + vol->mftmirr_ni = NULL; + errno = err; + return -1; +} + +/** + * ntfs_volume_startup - allocate and setup an ntfs volume + * @dev: device to open + * @flags: optional mount flags + * + * Load, verify, and parse bootsector; load and setup $MFT and $MFTMirr. After + * calling this function, the volume is setup sufficiently to call all read + * and write access functions provided by the library. + * + * Return the allocated volume structure on success and NULL on error with + * errno set to the error code. + */ +ntfs_volume *ntfs_volume_startup(struct ntfs_device *dev, + ntfs_mount_flags flags) +{ + LCN mft_zone_size, mft_lcn; + s64 br; + ntfs_volume *vol; + NTFS_BOOT_SECTOR *bs; + int eo; + + if (!dev || !dev->d_ops || !dev->d_name) { + errno = EINVAL; + ntfs_log_perror("%s: dev = %p", __FUNCTION__, dev); + return NULL; + } + + bs = ntfs_malloc(sizeof(NTFS_BOOT_SECTOR)); + if (!bs) + return NULL; + + /* Allocate the volume structure. */ + vol = ntfs_volume_alloc(); + if (!vol) + goto error_exit; + + /* Create the default upcase table. */ + vol->upcase_len = ntfs_upcase_build_default(&vol->upcase); + if (!vol->upcase_len || !vol->upcase) + goto error_exit; + + /* Default with no locase table and case sensitive file names */ + vol->locase = (ntfschar*)NULL; + NVolSetCaseSensitive(vol); + + /* by default, all files are shown and not marked hidden */ + NVolSetShowSysFiles(vol); + NVolSetShowHidFiles(vol); + NVolClearHideDotFiles(vol); + /* set default compression */ +#if DEFAULT_COMPRESSION + NVolSetCompression(vol); +#else + NVolClearCompression(vol); +#endif + if (flags & NTFS_MNT_RDONLY) + NVolSetReadOnly(vol); + + /* ...->open needs bracketing to compile with glibc 2.7 */ + if ((dev->d_ops->open)(dev, NVolReadOnly(vol) ? O_RDONLY: O_RDWR)) { + if (!NVolReadOnly(vol) && (errno == EROFS)) { + if ((dev->d_ops->open)(dev, O_RDONLY)) { + ntfs_log_perror("Error opening read-only '%s'", + dev->d_name); + goto error_exit; + } else { + ntfs_log_info("Error opening '%s' read-write\n", + dev->d_name); + NVolSetReadOnly(vol); + } + } else { + ntfs_log_perror("Error opening '%s'", dev->d_name); + goto error_exit; + } + } + /* Attach the device to the volume. */ + vol->dev = dev; + + /* Now read the bootsector. */ + br = ntfs_pread(dev, 0, sizeof(NTFS_BOOT_SECTOR), bs); + if (br != sizeof(NTFS_BOOT_SECTOR)) { + if (br != -1) + errno = EINVAL; + if (!br) + ntfs_log_error("Failed to read bootsector (size=0)\n"); + else + ntfs_log_perror("Error reading bootsector"); + goto error_exit; + } + if (!ntfs_boot_sector_is_ntfs(bs)) { + errno = EINVAL; + goto error_exit; + } + if (ntfs_boot_sector_parse(vol, bs) < 0) + goto error_exit; + + free(bs); + bs = NULL; + /* Now set the device block size to the sector size. */ + if (ntfs_device_block_size_set(vol->dev, vol->sector_size)) + ntfs_log_debug("Failed to set the device block size to the " + "sector size. This may affect performance " + "but should be harmless otherwise. Error: " + "%s\n", strerror(errno)); + + /* We now initialize the cluster allocator. */ + vol->full_zones = 0; + mft_zone_size = vol->nr_clusters >> 3; /* 12.5% */ + + /* Setup the mft zone. */ + vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn; + ntfs_log_debug("mft_zone_pos = 0x%llx\n", (long long)vol->mft_zone_pos); + + /* + * Calculate the mft_lcn for an unmodified NTFS volume (see mkntfs + * source) and if the actual mft_lcn is in the expected place or even + * further to the front of the volume, extend the mft_zone to cover the + * beginning of the volume as well. This is in order to protect the + * area reserved for the mft bitmap as well within the mft_zone itself. + * On non-standard volumes we don't protect it as the overhead would be + * higher than the speed increase we would get by doing it. + */ + mft_lcn = (8192 + 2 * vol->cluster_size - 1) / vol->cluster_size; + if (mft_lcn * vol->cluster_size < 16 * 1024) + mft_lcn = (16 * 1024 + vol->cluster_size - 1) / + vol->cluster_size; + if (vol->mft_zone_start <= mft_lcn) + vol->mft_zone_start = 0; + ntfs_log_debug("mft_zone_start = 0x%llx\n", (long long)vol->mft_zone_start); + + /* + * Need to cap the mft zone on non-standard volumes so that it does + * not point outside the boundaries of the volume. We do this by + * halving the zone size until we are inside the volume. + */ + vol->mft_zone_end = vol->mft_lcn + mft_zone_size; + while (vol->mft_zone_end >= vol->nr_clusters) { + mft_zone_size >>= 1; + if (!mft_zone_size) { + errno = EINVAL; + goto error_exit; + } + vol->mft_zone_end = vol->mft_lcn + mft_zone_size; + } + ntfs_log_debug("mft_zone_end = 0x%llx\n", (long long)vol->mft_zone_end); + + /* + * Set the current position within each data zone to the start of the + * respective zone. + */ + vol->data1_zone_pos = vol->mft_zone_end; + ntfs_log_debug("data1_zone_pos = %lld\n", (long long)vol->data1_zone_pos); + vol->data2_zone_pos = 0; + ntfs_log_debug("data2_zone_pos = %lld\n", (long long)vol->data2_zone_pos); + + /* Set the mft data allocation position to mft record 24. */ + vol->mft_data_pos = 24; + + /* + * The cluster allocator is now fully operational. + */ + + /* Need to setup $MFT so we can use the library read functions. */ + if (ntfs_mft_load(vol) < 0) { + ntfs_log_perror("Failed to load $MFT"); + goto error_exit; + } + + /* Need to setup $MFTMirr so we can use the write functions, too. */ + if (ntfs_mftmirr_load(vol) < 0) { + ntfs_log_perror("Failed to load $MFTMirr"); + goto error_exit; + } + return vol; +error_exit: + eo = errno; + free(bs); + if (vol) + __ntfs_volume_release(vol); + errno = eo; + return NULL; +} + +/** + * ntfs_volume_check_logfile - check logfile on target volume + * @vol: volume on which to check logfile + * + * Return 0 on success and -1 on error with errno set error code. + */ +static int ntfs_volume_check_logfile(ntfs_volume *vol) +{ + ntfs_inode *ni; + ntfs_attr *na = NULL; + RESTART_PAGE_HEADER *rp = NULL; + int err = 0; + + ni = ntfs_inode_open(vol, FILE_LogFile); + if (!ni) { + ntfs_log_perror("Failed to open inode FILE_LogFile"); + errno = EIO; + return -1; + } + + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + ntfs_log_perror("Failed to open $FILE_LogFile/$DATA"); + err = EIO; + goto out; + } + + if (!ntfs_check_logfile(na, &rp) || !ntfs_is_logfile_clean(na, rp)) + err = EOPNOTSUPP; + /* + * If the latest restart page was identified as version + * 2.0, then Windows may have kept a cached copy of + * metadata for fast restarting, and we should not mount. + * Hibernation will be seen the same way on a non + * Windows-system partition, so we have to use the same + * error code (EPERM). + * The restart page may also be identified as version 2.0 + * when access to the file system is terminated abruptly + * by unplugging or power cut, so mounting is also rejected + * after such an event. + */ + if (rp + && (rp->major_ver == const_cpu_to_le16(2)) + && (rp->minor_ver == const_cpu_to_le16(0))) { + ntfs_log_error("Metadata kept in Windows cache, refused to mount.\n"); + err = EPERM; + } + free(rp); + ntfs_attr_close(na); +out: + if (ntfs_inode_close(ni)) + ntfs_error_set(&err); + if (err) { + errno = err; + return -1; + } + return 0; +} + +/** + * ntfs_hiberfile_open - Find and open '/hiberfil.sys' + * @vol: An ntfs volume obtained from ntfs_mount + * + * Return: inode Success, hiberfil.sys is valid + * NULL hiberfil.sys doesn't exist or some other error occurred + */ +static ntfs_inode *ntfs_hiberfile_open(ntfs_volume *vol) +{ + u64 inode; + ntfs_inode *ni_root; + ntfs_inode *ni_hibr = NULL; + ntfschar *unicode = NULL; + int unicode_len; + const char *hiberfile = "hiberfil.sys"; + + if (!vol) { + errno = EINVAL; + return NULL; + } + + ni_root = ntfs_inode_open(vol, FILE_root); + if (!ni_root) { + ntfs_log_debug("Couldn't open the root directory.\n"); + return NULL; + } + + unicode_len = ntfs_mbstoucs(hiberfile, &unicode); + if (unicode_len < 0) { + ntfs_log_perror("Couldn't convert 'hiberfil.sys' to Unicode"); + goto out; + } + + inode = ntfs_inode_lookup_by_name(ni_root, unicode, unicode_len); + if (inode == (u64)-1) { + ntfs_log_debug("Couldn't find file '%s'.\n", hiberfile); + goto out; + } + + inode = MREF(inode); + ni_hibr = ntfs_inode_open(vol, inode); + if (!ni_hibr) { + ntfs_log_debug("Couldn't open inode %lld.\n", (long long)inode); + goto out; + } +out: + if (ntfs_inode_close(ni_root)) { + ntfs_inode_close(ni_hibr); + ni_hibr = NULL; + } + free(unicode); + return ni_hibr; +} + + +#define NTFS_HIBERFILE_HEADER_SIZE 4096 + +/** + * ntfs_volume_check_hiberfile - check hiberfil.sys whether Windows is + * hibernated on the target volume + * @vol: volume on which to check hiberfil.sys + * + * Return: 0 if Windows isn't hibernated for sure + * -1 otherwise and errno is set to the appropriate value + */ +int ntfs_volume_check_hiberfile(ntfs_volume *vol, int verbose) +{ + ntfs_inode *ni; + ntfs_attr *na = NULL; + int bytes_read, err; + char *buf = NULL; + + ni = ntfs_hiberfile_open(vol); + if (!ni) { + if (errno == ENOENT) + return 0; + return -1; + } + + buf = ntfs_malloc(NTFS_HIBERFILE_HEADER_SIZE); + if (!buf) + goto out; + + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + ntfs_log_perror("Failed to open hiberfil.sys data attribute"); + goto out; + } + + bytes_read = ntfs_attr_pread(na, 0, NTFS_HIBERFILE_HEADER_SIZE, buf); + if (bytes_read == -1) { + ntfs_log_perror("Failed to read hiberfil.sys"); + goto out; + } + if (bytes_read < NTFS_HIBERFILE_HEADER_SIZE) { + if (verbose) + ntfs_log_error("Hibernated non-system partition, " + "refused to mount.\n"); + errno = EPERM; + goto out; + } + if ((memcmp(buf, "hibr", 4) == 0) + || (memcmp(buf, "HIBR", 4) == 0)) { + if (verbose) + ntfs_log_error("Windows is hibernated, refused to mount.\n"); + errno = EPERM; + goto out; + } + /* All right, all header bytes are zero */ + errno = 0; +out: + if (na) + ntfs_attr_close(na); + free(buf); + err = errno; + if (ntfs_inode_close(ni)) + ntfs_error_set(&err); + errno = err; + return errno ? -1 : 0; +} + +/* + * Make sure a LOGGED_UTILITY_STREAM attribute named "$TXF_DATA" + * on the root directory is resident. + * When it is non-resident, the partition cannot be mounted on Vista + * (see http://support.microsoft.com/kb/974729) + * + * We take care to avoid this situation, however this can be a + * consequence of having used an older version (including older + * Windows version), so we had better fix it. + * + * Returns 0 if unneeded or successful + * -1 if there was an error, explained by errno + */ + +static int fix_txf_data(ntfs_volume *vol) +{ + void *txf_data; + s64 txf_data_size; + ntfs_inode *ni; + ntfs_attr *na; + int res; + + res = 0; + ntfs_log_debug("Loading root directory\n"); + ni = ntfs_inode_open(vol, FILE_root); + if (!ni) { + ntfs_log_perror("Failed to open root directory"); + res = -1; + } else { + /* Get the $TXF_DATA attribute */ + na = ntfs_attr_open(ni, AT_LOGGED_UTILITY_STREAM, TXF_DATA, 9); + if (na) { + if (NAttrNonResident(na)) { + /* + * Fix the attribute by truncating, then + * rewriting it. + */ + ntfs_log_debug("Making $TXF_DATA resident\n"); + txf_data = ntfs_attr_readall(ni, + AT_LOGGED_UTILITY_STREAM, + TXF_DATA, 9, &txf_data_size); + if (txf_data) { + if (ntfs_attr_truncate(na, 0) + || (ntfs_attr_pwrite(na, 0, + txf_data_size, txf_data) + != txf_data_size)) + res = -1; + free(txf_data); + } + if (res) + ntfs_log_error("Failed to make $TXF_DATA resident\n"); + else + ntfs_log_error("$TXF_DATA made resident\n"); + } + ntfs_attr_close(na); + } + if (ntfs_inode_close(ni)) { + ntfs_log_perror("Failed to close root"); + res = -1; + } + } + return (res); +} + +/** + * ntfs_device_mount - open ntfs volume + * @dev: device to open + * @flags: optional mount flags + * + * This function mounts an ntfs volume. @dev should describe the device which + * to mount as the ntfs volume. + * + * @flags is an optional second parameter. The same flags are used as for + * the mount system call (man 2 mount). Currently only the following flag + * is implemented: + * NTFS_MNT_RDONLY - mount volume read-only + * + * The function opens the device @dev and verifies that it contains a valid + * bootsector. Then, it allocates an ntfs_volume structure and initializes + * some of the values inside the structure from the information stored in the + * bootsector. It proceeds to load the necessary system files and completes + * setting up the structure. + * + * Return the allocated volume structure on success and NULL on error with + * errno set to the error code. + */ +ntfs_volume *ntfs_device_mount(struct ntfs_device *dev, ntfs_mount_flags flags) +{ + s64 l; + ntfs_volume *vol; + u8 *m = NULL, *m2 = NULL; + ntfs_attr_search_ctx *ctx = NULL; + ntfs_inode *ni; + ntfs_attr *na; + ATTR_RECORD *a; + VOLUME_INFORMATION *vinf; + ntfschar *vname; + u32 record_size; + int i, j, eo; + unsigned int k; + u32 u; + BOOL need_fallback_ro; + + need_fallback_ro = FALSE; + vol = ntfs_volume_startup(dev, flags); + if (!vol) + return NULL; + + /* Load data from $MFT and $MFTMirr and compare the contents. */ + m = ntfs_malloc(vol->mftmirr_size << vol->mft_record_size_bits); + m2 = ntfs_malloc(vol->mftmirr_size << vol->mft_record_size_bits); + if (!m || !m2) + goto error_exit; + + l = ntfs_attr_mst_pread(vol->mft_na, 0, vol->mftmirr_size, + vol->mft_record_size, m); + if (l != vol->mftmirr_size) { + if (l == -1) + ntfs_log_perror("Failed to read $MFT"); + else { + ntfs_log_error("Failed to read $MFT, unexpected length " + "(%lld != %d).\n", (long long)l, + vol->mftmirr_size); + errno = EIO; + } + goto error_exit; + } + for (i = 0; (i < l) && (i < FILE_first_user); ++i) + if (ntfs_mft_record_check(vol, FILE_MFT + i, + (MFT_RECORD*)(m + i*vol->mft_record_size))) + goto error_exit; + l = ntfs_attr_mst_pread(vol->mftmirr_na, 0, vol->mftmirr_size, + vol->mft_record_size, m2); + if (l != vol->mftmirr_size) { + if (l == -1) { + ntfs_log_perror("Failed to read $MFTMirr"); + goto error_exit; + } + vol->mftmirr_size = l; + } + for (i = 0; (i < l) && (i < FILE_first_user); ++i) + if (ntfs_mft_record_check(vol, FILE_MFT + i, + (MFT_RECORD*)(m2 + i*vol->mft_record_size))) + goto error_exit; + ntfs_log_debug("Comparing $MFTMirr to $MFT...\n"); + /* Windows 10 does not update the full $MFTMirr any more */ + for (i = 0; (i < vol->mftmirr_size) && (i < FILE_first_user); ++i) { + MFT_RECORD *mrec, *mrec2; + const char *ESTR[12] = { "$MFT", "$MFTMirr", "$LogFile", + "$Volume", "$AttrDef", "root directory", "$Bitmap", + "$Boot", "$BadClus", "$Secure", "$UpCase", "$Extend" }; + const char *s; + + if (i < 12) + s = ESTR[i]; + else if (i < 16) + s = "system file"; + else + s = "mft record"; + + mrec = (MFT_RECORD*)(m + i * vol->mft_record_size); + if (mrec->flags & MFT_RECORD_IN_USE) { + if (ntfs_is_baad_record(mrec->magic)) { + ntfs_log_error("$MFT error: Incomplete multi " + "sector transfer detected in " + "'%s'.\n", s); + goto io_error_exit; + } + if (!ntfs_is_mft_record(mrec->magic)) { + ntfs_log_error("$MFT error: Invalid mft " + "record for '%s'.\n", s); + goto io_error_exit; + } + } + mrec2 = (MFT_RECORD*)(m2 + i * vol->mft_record_size); + if (mrec2->flags & MFT_RECORD_IN_USE) { + if (ntfs_is_baad_record(mrec2->magic)) { + ntfs_log_error("$MFTMirr error: Incomplete " + "multi sector transfer " + "detected in '%s'.\n", s); + goto io_error_exit; + } + if (!ntfs_is_mft_record(mrec2->magic)) { + ntfs_log_error("$MFTMirr error: Invalid mft " + "record for '%s'.\n", s); + goto io_error_exit; + } + } + record_size = ntfs_mft_record_get_data_size(mrec); + if ((record_size <= sizeof(MFT_RECORD)) + || (record_size > vol->mft_record_size) + || memcmp(mrec, mrec2, record_size)) { + ntfs_log_error("$MFTMirr does not match $MFT (record " + "%d).\n", i); + goto io_error_exit; + } + } + + free(m2); + free(m); + m = m2 = NULL; + + /* Now load the bitmap from $Bitmap. */ + ntfs_log_debug("Loading $Bitmap...\n"); + vol->lcnbmp_ni = ntfs_inode_open(vol, FILE_Bitmap); + if (!vol->lcnbmp_ni) { + ntfs_log_perror("Failed to open inode FILE_Bitmap"); + goto error_exit; + } + + vol->lcnbmp_na = ntfs_attr_open(vol->lcnbmp_ni, AT_DATA, AT_UNNAMED, 0); + if (!vol->lcnbmp_na) { + ntfs_log_perror("Failed to open ntfs attribute"); + goto error_exit; + } + + if (vol->lcnbmp_na->data_size > vol->lcnbmp_na->allocated_size) { + ntfs_log_error("Corrupt cluster map size (%lld > %lld)\n", + (long long)vol->lcnbmp_na->data_size, + (long long)vol->lcnbmp_na->allocated_size); + goto io_error_exit; + } + + /* Now load the upcase table from $UpCase. */ + ntfs_log_debug("Loading $UpCase...\n"); + ni = ntfs_inode_open(vol, FILE_UpCase); + if (!ni) { + ntfs_log_perror("Failed to open inode FILE_UpCase"); + goto error_exit; + } + /* Get an ntfs attribute for $UpCase/$DATA. */ + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + ntfs_log_perror("Failed to open ntfs attribute"); + ntfs_inode_close(ni); + goto error_exit; + } + /* + * Note: Normally, the upcase table has a length equal to 65536 + * 2-byte Unicode characters. Anyway we currently can only process + * such characters. + */ + if ((na->data_size - 2) & ~0x1fffeULL) { + ntfs_log_error("Error: Upcase table is invalid (want size even " + "<= 131072).\n"); + errno = EINVAL; + goto bad_upcase; + } + if (vol->upcase_len != na->data_size >> 1) { + vol->upcase_len = na->data_size >> 1; + /* Throw away default table. */ + free(vol->upcase); + vol->upcase = ntfs_malloc(na->data_size); + if (!vol->upcase) + goto bad_upcase; + } + /* Read in the $DATA attribute value into the buffer. */ + l = ntfs_attr_pread(na, 0, na->data_size, vol->upcase); + if (l != na->data_size) { + ntfs_log_error("Failed to read $UpCase, unexpected length " + "(%lld != %lld).\n", (long long)l, + (long long)na->data_size); + errno = EIO; + goto bad_upcase; + } + /* Done with the $UpCase mft record. */ + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) { + ntfs_log_perror("Failed to close $UpCase"); + goto error_exit; + } + /* Consistency check of $UpCase, restricted to plain ASCII chars */ + k = 0x20; + while ((k < vol->upcase_len) + && (k < 0x7f) + && (le16_to_cpu(vol->upcase[k]) + == ((k < 'a') || (k > 'z') ? k : k + 'A' - 'a'))) + k++; + if (k < 0x7f) { + ntfs_log_error("Corrupted file $UpCase\n"); + goto io_error_exit; + } + + /* + * Now load $Volume and set the version information and flags in the + * vol structure accordingly. + */ + ntfs_log_debug("Loading $Volume...\n"); + vol->vol_ni = ntfs_inode_open(vol, FILE_Volume); + if (!vol->vol_ni) { + ntfs_log_perror("Failed to open inode FILE_Volume"); + goto error_exit; + } + /* Get a search context for the $Volume/$VOLUME_INFORMATION lookup. */ + ctx = ntfs_attr_get_search_ctx(vol->vol_ni, NULL); + if (!ctx) + goto error_exit; + + /* Find the $VOLUME_INFORMATION attribute. */ + if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, AT_UNNAMED, 0, 0, 0, NULL, + 0, ctx)) { + ntfs_log_perror("$VOLUME_INFORMATION attribute not found in " + "$Volume"); + goto error_exit; + } + a = ctx->attr; + /* Has to be resident. */ + if (a->non_resident) { + ntfs_log_error("Attribute $VOLUME_INFORMATION must be " + "resident but it isn't.\n"); + errno = EIO; + goto error_exit; + } + /* Get a pointer to the value of the attribute. */ + vinf = (VOLUME_INFORMATION*)(le16_to_cpu(a->value_offset) + (char*)a); + /* Sanity checks. */ + if ((char*)vinf + le32_to_cpu(a->value_length) > (char*)ctx->mrec + + le32_to_cpu(ctx->mrec->bytes_in_use) || + le16_to_cpu(a->value_offset) + le32_to_cpu( + a->value_length) > le32_to_cpu(a->length)) { + ntfs_log_error("$VOLUME_INFORMATION in $Volume is corrupt.\n"); + errno = EIO; + goto error_exit; + } + /* Setup vol from the volume information attribute value. */ + vol->major_ver = vinf->major_ver; + vol->minor_ver = vinf->minor_ver; + /* Do not use le16_to_cpu() macro here as our VOLUME_FLAGS are + defined using cpu_to_le16() macro and hence are consistent. */ + vol->flags = vinf->flags; + /* + * Reinitialize the search context for the $Volume/$VOLUME_NAME lookup. + */ + ntfs_attr_reinit_search_ctx(ctx); + if (ntfs_attr_lookup(AT_VOLUME_NAME, AT_UNNAMED, 0, 0, 0, NULL, 0, + ctx)) { + if (errno != ENOENT) { + ntfs_log_perror("Failed to lookup of $VOLUME_NAME in " + "$Volume failed"); + goto error_exit; + } + /* + * Attribute not present. This has been seen in the field. + * Treat this the same way as if the attribute was present but + * had zero length. + */ + vol->vol_name = ntfs_malloc(1); + if (!vol->vol_name) + goto error_exit; + vol->vol_name[0] = '\0'; + } else { + a = ctx->attr; + /* Has to be resident. */ + if (a->non_resident) { + ntfs_log_error("$VOLUME_NAME must be resident.\n"); + errno = EIO; + goto error_exit; + } + /* Get a pointer to the value of the attribute. */ + vname = (ntfschar*)(le16_to_cpu(a->value_offset) + (char*)a); + u = le32_to_cpu(a->value_length) / 2; + /* + * Convert Unicode volume name to current locale multibyte + * format. + */ + vol->vol_name = NULL; + if (ntfs_ucstombs(vname, u, &vol->vol_name, 0) == -1) { + ntfs_log_perror("Volume name could not be converted " + "to current locale"); + ntfs_log_debug("Forcing name into ASCII by replacing " + "non-ASCII characters with underscores.\n"); + vol->vol_name = ntfs_malloc(u + 1); + if (!vol->vol_name) + goto error_exit; + + for (j = 0; j < (s32)u; j++) { + u16 uc = le16_to_cpu(vname[j]); + if (uc > 0xff) + uc = (u16)'_'; + vol->vol_name[j] = (char)uc; + } + vol->vol_name[u] = '\0'; + } + } + ntfs_attr_put_search_ctx(ctx); + ctx = NULL; + /* Now load the attribute definitions from $AttrDef. */ + ntfs_log_debug("Loading $AttrDef...\n"); + ni = ntfs_inode_open(vol, FILE_AttrDef); + if (!ni) { + ntfs_log_perror("Failed to open $AttrDef"); + goto error_exit; + } + /* Get an ntfs attribute for $AttrDef/$DATA. */ + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + ntfs_log_perror("Failed to open ntfs attribute"); + goto error_exit; + } + /* Check we don't overflow 24-bits. */ + if ((u64)na->data_size > 0xffffffLL) { + ntfs_log_error("Attribute definition table is too big (max " + "24-bit allowed).\n"); + errno = EINVAL; + goto error_exit; + } + vol->attrdef_len = na->data_size; + vol->attrdef = ntfs_malloc(na->data_size); + if (!vol->attrdef) + goto error_exit; + /* Read in the $DATA attribute value into the buffer. */ + l = ntfs_attr_pread(na, 0, na->data_size, vol->attrdef); + if (l != na->data_size) { + ntfs_log_error("Failed to read $AttrDef, unexpected length " + "(%lld != %lld).\n", (long long)l, + (long long)na->data_size); + errno = EIO; + goto error_exit; + } + /* Done with the $AttrDef mft record. */ + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) { + ntfs_log_perror("Failed to close $AttrDef"); + goto error_exit; + } + + /* Open $Secure. */ + if (ntfs_open_secure(vol)) + goto error_exit; + + /* + * Check for dirty logfile and hibernated Windows. + * We care only about read-write mounts. + */ + if (!(flags & (NTFS_MNT_RDONLY | NTFS_MNT_FORENSIC))) { + if (!(flags & NTFS_MNT_IGNORE_HIBERFILE) && + ntfs_volume_check_hiberfile(vol, 1) < 0) { + if (flags & NTFS_MNT_MAY_RDONLY) + need_fallback_ro = TRUE; + else + goto error_exit; + } + if (ntfs_volume_check_logfile(vol) < 0) { + /* Always reject cached metadata for now */ + if (!(flags & NTFS_MNT_RECOVER) || (errno == EPERM)) { + if (flags & NTFS_MNT_MAY_RDONLY) + need_fallback_ro = TRUE; + else + goto error_exit; + } else { + ntfs_log_info("The file system wasn't safely " + "closed on Windows. Fixing.\n"); + if (ntfs_logfile_reset(vol)) + goto error_exit; + } + } + /* make $TXF_DATA resident if present on the root directory */ + if (!(flags & NTFS_MNT_RDONLY) && !need_fallback_ro) { + if (fix_txf_data(vol)) + goto error_exit; + } + } + if (need_fallback_ro) { + NVolSetReadOnly(vol); + ntfs_log_error("%s", fallback_readonly_msg); + } + + return vol; +bad_upcase : + ntfs_attr_close(na); + ntfs_inode_close(ni); + goto error_exit; +io_error_exit: + errno = EIO; +error_exit: + eo = errno; + if (ctx) + ntfs_attr_put_search_ctx(ctx); + free(m); + free(m2); + __ntfs_volume_release(vol); + errno = eo; + return NULL; +} + +/* + * Set appropriate flags for showing NTFS metafiles + * or files marked as hidden. + * Not set in ntfs_mount() to avoid breaking existing tools. + */ + +int ntfs_set_shown_files(ntfs_volume *vol, + BOOL show_sys_files, BOOL show_hid_files, + BOOL hide_dot_files) +{ + int res; + + res = -1; + if (vol) { + NVolClearShowSysFiles(vol); + NVolClearShowHidFiles(vol); + NVolClearHideDotFiles(vol); + if (show_sys_files) + NVolSetShowSysFiles(vol); + if (show_hid_files) + NVolSetShowHidFiles(vol); + if (hide_dot_files) + NVolSetHideDotFiles(vol); + res = 0; + } + if (res) + ntfs_log_error("Failed to set file visibility\n"); + return (res); +} + +/* + * Set ignore case mode + */ + +int ntfs_set_ignore_case(ntfs_volume *vol) +{ + int res; + + res = -1; + if (vol && vol->upcase) { + vol->locase = ntfs_locase_table_build(vol->upcase, + vol->upcase_len); + if (vol->locase) { + NVolClearCaseSensitive(vol); + res = 0; + } + } + if (res) + ntfs_log_error("Failed to set ignore_case mode\n"); + return (res); +} + +/** + * ntfs_mount - open ntfs volume + * @name: name of device/file to open + * @flags: optional mount flags + * + * This function mounts an ntfs volume. @name should contain the name of the + * device/file to mount as the ntfs volume. + * + * @flags is an optional second parameter. The same flags are used as for + * the mount system call (man 2 mount). Currently only the following flags + * is implemented: + * NTFS_MNT_RDONLY - mount volume read-only + * + * The function opens the device or file @name and verifies that it contains a + * valid bootsector. Then, it allocates an ntfs_volume structure and initializes + * some of the values inside the structure from the information stored in the + * bootsector. It proceeds to load the necessary system files and completes + * setting up the structure. + * + * Return the allocated volume structure on success and NULL on error with + * errno set to the error code. + * + * Note, that a copy is made of @name, and hence it can be discarded as + * soon as the function returns. + */ +ntfs_volume *ntfs_mount(const char *name __attribute__((unused)), + ntfs_mount_flags flags __attribute__((unused))) +{ +#ifndef NO_NTFS_DEVICE_DEFAULT_IO_OPS + struct ntfs_device *dev; + ntfs_volume *vol; + + /* Allocate an ntfs_device structure. */ + dev = ntfs_device_alloc(name, 0, &ntfs_device_default_io_ops, NULL); + if (!dev) + return NULL; + /* Call ntfs_device_mount() to do the actual mount. */ + vol = ntfs_device_mount(dev, flags); + if (!vol) { + int eo = errno; + ntfs_device_free(dev); + errno = eo; + } else + ntfs_create_lru_caches(vol); + return vol; +#else + /* + * ntfs_mount() makes no sense if NO_NTFS_DEVICE_DEFAULT_IO_OPS is + * defined as there are no device operations available in libntfs in + * this case. + */ + errno = EOPNOTSUPP; + return NULL; +#endif +} + +/** + * ntfs_umount - close ntfs volume + * @vol: address of ntfs_volume structure of volume to close + * @force: if true force close the volume even if it is busy + * + * Deallocate all structures (including @vol itself) associated with the ntfs + * volume @vol. + * + * Return 0 on success. On error return -1 with errno set appropriately + * (most likely to one of EAGAIN, EBUSY or EINVAL). The EAGAIN error means that + * an operation is in progress and if you try the close later the operation + * might be completed and the close succeed. + * + * If @force is true (i.e. not zero) this function will close the volume even + * if this means that data might be lost. + * + * @vol must have previously been returned by a call to ntfs_mount(). + * + * @vol itself is deallocated and should no longer be dereferenced after this + * function returns success. If it returns an error then nothing has been done + * so it is safe to continue using @vol. + */ +int ntfs_umount(ntfs_volume *vol, const BOOL force __attribute__((unused))) +{ + struct ntfs_device *dev; + int ret; + + if (!vol) { + errno = EINVAL; + return -1; + } + dev = vol->dev; + ret = __ntfs_volume_release(vol); + ntfs_device_free(dev); + return ret; +} + +#ifdef HAVE_MNTENT_H + +/** + * ntfs_mntent_check - desc + * + * If you are wanting to use this, you actually wanted to use + * ntfs_check_if_mounted(), you just didn't realize. (-: + * + * See description of ntfs_check_if_mounted(), below. + */ +static int ntfs_mntent_check(const char *file, unsigned long *mnt_flags) +{ + struct mntent *mnt; + char *real_file = NULL, *real_fsname = NULL; + FILE *f; + int err = 0; + + real_file = ntfs_malloc(PATH_MAX + 1); + if (!real_file) + return -1; + real_fsname = ntfs_malloc(PATH_MAX + 1); + if (!real_fsname) { + err = errno; + goto exit; + } + if (!ntfs_realpath_canonicalize(file, real_file)) { + err = errno; + goto exit; + } + f = setmntent("/proc/mounts", "r"); + if (!f && !(f = setmntent(MOUNTED, "r"))) { + err = errno; + goto exit; + } + while ((mnt = getmntent(f))) { + if (!ntfs_realpath_canonicalize(mnt->mnt_fsname, real_fsname)) + continue; + if (!strcmp(real_file, real_fsname)) + break; + } + endmntent(f); + if (!mnt) + goto exit; + *mnt_flags = NTFS_MF_MOUNTED; + if (!strcmp(mnt->mnt_dir, "/")) + *mnt_flags |= NTFS_MF_ISROOT; +#ifdef HAVE_HASMNTOPT + if (hasmntopt(mnt, "ro") && !hasmntopt(mnt, "rw")) + *mnt_flags |= NTFS_MF_READONLY; +#endif +exit: + free(real_file); + free(real_fsname); + if (err) { + errno = err; + return -1; + } + return 0; +} + +#else /* HAVE_MNTENT_H */ + +#if defined(__sun) && defined (__SVR4) + +static int ntfs_mntent_check(const char *file, unsigned long *mnt_flags) +{ + struct mnttab *mnt = NULL; + char *real_file = NULL, *real_fsname = NULL; + FILE *f; + int err = 0; + + real_file = (char*)ntfs_malloc(PATH_MAX + 1); + if (!real_file) + return -1; + real_fsname = (char*)ntfs_malloc(PATH_MAX + 1); + mnt = (struct mnttab*)ntfs_malloc(MNT_LINE_MAX + 1); + if (!real_fsname || !mnt) { + err = errno; + goto exit; + } + if (!ntfs_realpath_canonicalize(file, real_file)) { + err = errno; + goto exit; + } + if (!(f = fopen(MNTTAB, "r"))) { + err = errno; + goto exit; + } + while (!getmntent(f, mnt)) { + if (!ntfs_realpath_canonicalize(mnt->mnt_special, real_fsname)) + continue; + if (!strcmp(real_file, real_fsname)) { + *mnt_flags = NTFS_MF_MOUNTED; + if (!strcmp(mnt->mnt_mountp, "/")) + *mnt_flags |= NTFS_MF_ISROOT; + if (hasmntopt(mnt, "ro") && !hasmntopt(mnt, "rw")) + *mnt_flags |= NTFS_MF_READONLY; + break; + } + } + fclose(f); +exit: + free(mnt); + free(real_file); + free(real_fsname); + if (err) { + errno = err; + return -1; + } + return 0; +} + +#endif /* defined(__sun) && defined (__SVR4) */ +#endif /* HAVE_MNTENT_H */ + +/** + * ntfs_check_if_mounted - check if an ntfs volume is currently mounted + * @file: device file to check + * @mnt_flags: pointer into which to return the ntfs mount flags (see volume.h) + * + * If the running system does not support the {set,get,end}mntent() calls, + * just return 0 and set *@mnt_flags to zero. + * + * When the system does support the calls, ntfs_check_if_mounted() first tries + * to find the device @file in /etc/mtab (or wherever this is kept on the + * running system). If it is not found, assume the device is not mounted and + * return 0 and set *@mnt_flags to zero. + * + * If the device @file is found, set the NTFS_MF_MOUNTED flags in *@mnt_flags. + * + * Further if @file is mounted as the file system root ("/"), set the flag + * NTFS_MF_ISROOT in *@mnt_flags. + * + * Finally, check if the file system is mounted read-only, and if so set the + * NTFS_MF_READONLY flag in *@mnt_flags. + * + * On success return 0 with *@mnt_flags set to the ntfs mount flags. + * + * On error return -1 with errno set to the error code. + */ +int ntfs_check_if_mounted(const char *file __attribute__((unused)), + unsigned long *mnt_flags) +{ + *mnt_flags = 0; +#if defined(HAVE_MNTENT_H) || (defined(__sun) && defined (__SVR4)) + return ntfs_mntent_check(file, mnt_flags); +#else + return 0; +#endif +} + +/** + * ntfs_version_is_supported - check if NTFS version is supported. + * @vol: ntfs volume whose version we're interested in. + * + * The function checks if the NTFS volume version is known or not. + * Version 1.1 and 1.2 are used by Windows NT3.x and NT4. + * Version 2.x is used by Windows 2000 Betas. + * Version 3.0 is used by Windows 2000. + * Version 3.1 is used by Windows XP, Windows Server 2003 and Longhorn. + * + * Return 0 if NTFS version is supported otherwise -1 with errno set. + * + * The following error codes are defined: + * EOPNOTSUPP - Unknown NTFS version + * EINVAL - Invalid argument + */ +int ntfs_version_is_supported(ntfs_volume *vol) +{ + u8 major, minor; + + if (!vol) { + errno = EINVAL; + return -1; + } + + major = vol->major_ver; + minor = vol->minor_ver; + + if (NTFS_V1_1(major, minor) || NTFS_V1_2(major, minor)) + return 0; + + if (NTFS_V2_X(major, minor)) + return 0; + + if (NTFS_V3_0(major, minor) || NTFS_V3_1(major, minor)) + return 0; + + errno = EOPNOTSUPP; + return -1; +} + +/** + * ntfs_logfile_reset - "empty" $LogFile data attribute value + * @vol: ntfs volume whose $LogFile we intend to reset. + * + * Fill the value of the $LogFile data attribute, i.e. the contents of + * the file, with 0xff's, thus marking the journal as empty. + * + * FIXME(?): We might need to zero the LSN field of every single mft + * record as well. (But, first try without doing that and see what + * happens, since chkdsk might pickup the pieces and do it for us...) + * + * On success return 0. + * + * On error return -1 with errno set to the error code. + */ +int ntfs_logfile_reset(ntfs_volume *vol) +{ + ntfs_inode *ni; + ntfs_attr *na; + int eo; + + if (!vol) { + errno = EINVAL; + return -1; + } + + ni = ntfs_inode_open(vol, FILE_LogFile); + if (!ni) { + ntfs_log_perror("Failed to open inode FILE_LogFile"); + return -1; + } + + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + eo = errno; + ntfs_log_perror("Failed to open $FILE_LogFile/$DATA"); + goto error_exit; + } + + if (ntfs_empty_logfile(na)) { + eo = errno; + ntfs_attr_close(na); + goto error_exit; + } + + ntfs_attr_close(na); + return ntfs_inode_close(ni); + +error_exit: + ntfs_inode_close(ni); + errno = eo; + return -1; +} + +/** + * ntfs_volume_write_flags - set the flags of an ntfs volume + * @vol: ntfs volume where we set the volume flags + * @flags: new flags + * + * Set the on-disk volume flags in the mft record of $Volume and + * on volume @vol to @flags. + * + * Return 0 if successful and -1 if not with errno set to the error code. + */ +int ntfs_volume_write_flags(ntfs_volume *vol, const le16 flags) +{ + ATTR_RECORD *a; + VOLUME_INFORMATION *c; + ntfs_attr_search_ctx *ctx; + int ret = -1; /* failure */ + + if (!vol || !vol->vol_ni) { + errno = EINVAL; + return -1; + } + /* Get a pointer to the volume information attribute. */ + ctx = ntfs_attr_get_search_ctx(vol->vol_ni, NULL); + if (!ctx) + return -1; + + if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, AT_UNNAMED, 0, 0, 0, NULL, + 0, ctx)) { + ntfs_log_error("Attribute $VOLUME_INFORMATION was not found " + "in $Volume!\n"); + goto err_out; + } + a = ctx->attr; + /* Sanity check. */ + if (a->non_resident) { + ntfs_log_error("Attribute $VOLUME_INFORMATION must be resident " + "but it isn't.\n"); + errno = EIO; + goto err_out; + } + /* Get a pointer to the value of the attribute. */ + c = (VOLUME_INFORMATION*)(le16_to_cpu(a->value_offset) + (char*)a); + /* Sanity checks. */ + if ((char*)c + le32_to_cpu(a->value_length) > (char*)ctx->mrec + + le32_to_cpu(ctx->mrec->bytes_in_use) || + le16_to_cpu(a->value_offset) + + le32_to_cpu(a->value_length) > le32_to_cpu(a->length)) { + ntfs_log_error("Attribute $VOLUME_INFORMATION in $Volume is " + "corrupt!\n"); + errno = EIO; + goto err_out; + } + /* Set the volume flags. */ + vol->flags = c->flags = flags & VOLUME_FLAGS_MASK; + /* Write them to disk. */ + ntfs_inode_mark_dirty(vol->vol_ni); + if (ntfs_inode_sync(vol->vol_ni)) + goto err_out; + + ret = 0; /* success */ +err_out: + ntfs_attr_put_search_ctx(ctx); + return ret; +} + +int ntfs_volume_error(int err) +{ + int ret; + + switch (err) { + case 0: + ret = NTFS_VOLUME_OK; + break; + case EINVAL: + ret = NTFS_VOLUME_NOT_NTFS; + break; + case EIO: + ret = NTFS_VOLUME_CORRUPT; + break; + case EPERM: + /* + * Hibernation and fast restarting are seen the + * same way on a non Windows-system partition. + */ + ret = NTFS_VOLUME_HIBERNATED; + break; + case EOPNOTSUPP: + ret = NTFS_VOLUME_UNCLEAN_UNMOUNT; + break; + case EBUSY: + ret = NTFS_VOLUME_LOCKED; + break; + case ENXIO: + ret = NTFS_VOLUME_RAID; + break; + case EACCES: + ret = NTFS_VOLUME_NO_PRIVILEGE; + break; + default: + ret = NTFS_VOLUME_UNKNOWN_REASON; + break; + } + return ret; +} + + +void ntfs_mount_error(const char *volume, const char *mntpoint, int err) +{ + switch (err) { + case NTFS_VOLUME_NOT_NTFS: + ntfs_log_error(invalid_ntfs_msg, volume); + break; + case NTFS_VOLUME_CORRUPT: + ntfs_log_error("%s", corrupt_volume_msg); + break; + case NTFS_VOLUME_HIBERNATED: + ntfs_log_error(hibernated_volume_msg, volume, mntpoint); + break; + case NTFS_VOLUME_UNCLEAN_UNMOUNT: + ntfs_log_error("%s", unclean_journal_msg); + break; + case NTFS_VOLUME_LOCKED: + ntfs_log_error("%s", opened_volume_msg); + break; + case NTFS_VOLUME_RAID: + ntfs_log_error("%s", fakeraid_msg); + break; + case NTFS_VOLUME_NO_PRIVILEGE: + ntfs_log_error(access_denied_msg, volume); + break; + } +} + +int ntfs_set_locale(void) +{ + const char *locale; + + locale = setlocale(LC_ALL, ""); + if (!locale) { + locale = setlocale(LC_ALL, NULL); + ntfs_log_error("Couldn't set local environment, using default " + "'%s'.\n", locale); + return 1; + } + return 0; +} + +/* + * Feed the counts of free clusters and free mft records + */ + +int ntfs_volume_get_free_space(ntfs_volume *vol) +{ + ntfs_attr *na; + int ret; + + ret = -1; /* default return */ + vol->free_clusters = ntfs_attr_get_free_bits(vol->lcnbmp_na); + if (vol->free_clusters < 0) { + ntfs_log_perror("Failed to read NTFS $Bitmap"); + } else { + na = vol->mftbmp_na; + vol->free_mft_records = ntfs_attr_get_free_bits(na); + + if (vol->free_mft_records >= 0) + vol->free_mft_records += (na->allocated_size - na->data_size) << 3; + + if (vol->free_mft_records < 0) + ntfs_log_perror("Failed to calculate free MFT records"); + else { + NVolSetFreeSpaceKnown(vol); + ret = 0; + } + } + return (ret); +} + +/** + * ntfs_volume_rename - change the current label on a volume + * @vol: volume to change the label on + * @label: the new label + * @label_len: the length of @label in ntfschars including the terminating NULL + * character, which is mandatory (the value can not exceed 128) + * + * Change the label on the volume @vol to @label. + */ +int ntfs_volume_rename(ntfs_volume *vol, const ntfschar *label, int label_len) +{ + ntfs_attr *na; + char *old_vol_name; + char *new_vol_name = NULL; + int new_vol_name_len; + int err; + + if (NVolReadOnly(vol)) { + ntfs_log_error("Refusing to change label on read-only mounted " + "volume.\n"); + errno = EROFS; + return -1; + } + + label_len *= sizeof(ntfschar); + if (label_len > 0x100) { + ntfs_log_error("New label is too long. Maximum %u characters " + "allowed.\n", + (unsigned)(0x100 / sizeof(ntfschar))); + errno = ERANGE; + return -1; + } + + na = ntfs_attr_open(vol->vol_ni, AT_VOLUME_NAME, AT_UNNAMED, 0); + if (!na) { + if (errno != ENOENT) { + err = errno; + ntfs_log_perror("Lookup of $VOLUME_NAME attribute " + "failed"); + goto err_out; + } + + /* The volume name attribute does not exist. Need to add it. */ + if (ntfs_attr_add(vol->vol_ni, AT_VOLUME_NAME, AT_UNNAMED, 0, + (const u8*) label, label_len)) + { + err = errno; + ntfs_log_perror("Encountered error while adding " + "$VOLUME_NAME attribute"); + goto err_out; + } + } + else { + s64 written; + + if (NAttrNonResident(na)) { + err = errno; + ntfs_log_error("Error: Attribute $VOLUME_NAME must be " + "resident.\n"); + goto err_out; + } + + if (na->data_size != label_len) { + if (ntfs_attr_truncate(na, label_len)) { + err = errno; + ntfs_log_perror("Error resizing resident " + "attribute"); + goto err_out; + } + } + + if (label_len) { + written = ntfs_attr_pwrite(na, 0, label_len, label); + if (written == -1) { + err = errno; + ntfs_log_perror("Error when writing " + "$VOLUME_NAME data"); + goto err_out; + } + else if (written != label_len) { + err = EIO; + ntfs_log_error("Partial write when writing " + "$VOLUME_NAME data."); + goto err_out; + + } + } + } + + new_vol_name_len = + ntfs_ucstombs(label, label_len, &new_vol_name, 0); + if (new_vol_name_len == -1) { + err = errno; + ntfs_log_perror("Error while decoding new volume name"); + goto err_out; + } + + old_vol_name = vol->vol_name; + vol->vol_name = new_vol_name; + free(old_vol_name); + + err = 0; +err_out: + if (na) + ntfs_attr_close(na); + if (err) + errno = err; + return err ? -1 : 0; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/win32_io.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/win32_io.c new file mode 100644 index 0000000..9c72dee --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/win32_io.c @@ -0,0 +1,2040 @@ +/* + * win32_io.c - A stdio-like disk I/O implementation for low-level disk access + * on Win32. Can access an NTFS volume while it is mounted. + * Originated from the Linux-NTFS project. + * + * Copyright (c) 2003-2004 Lode Leroy + * Copyright (c) 2003-2006 Anton Altaparmakov + * Copyright (c) 2004-2005 Yuval Fledel + * Copyright (c) 2012-2014 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_WINDOWS_H +#define BOOL WINBOOL /* avoid conflicting definitions of BOOL */ +#include +#undef BOOL +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif + +/* + * Definitions needed for + */ +#ifndef _ANONYMOUS_UNION +#define _ANONYMOUS_UNION +#define _ANONYMOUS_STRUCT +typedef unsigned long long DWORD64; +#endif + +typedef struct { + DWORD data1; /* The first eight hexadecimal digits of the GUID. */ + WORD data2; /* The first group of four hexadecimal digits. */ + WORD data3; /* The second group of four hexadecimal digits. */ + char data4[8]; /* The first two bytes are the third group of four + hexadecimal digits. The remaining six bytes are the + final 12 hexadecimal digits. */ +} GUID; + +#include + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_CTYPE_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#define stat stat64 +#define st_blocks st_rdev /* emulate st_blocks, missing in Windows */ +#endif + +/* Prevent volume.h from being be loaded, as it conflicts with winnt.h. */ +#define _NTFS_VOLUME_H +struct ntfs_volume; +typedef struct ntfs_volume ntfs_volume; + +#include "debug.h" +#include "types.h" +#include "device.h" +#include "misc.h" + +#define cpu_to_le16(x) (x) +#define const_cpu_to_le16(x) (x) + +#ifndef MAX_PATH +#define MAX_PATH 1024 +#endif + +#ifndef NTFS_BLOCK_SIZE +#define NTFS_BLOCK_SIZE 512 +#define NTFS_BLOCK_SIZE_BITS 9 +#endif + +#ifndef INVALID_SET_FILE_POINTER +#define INVALID_SET_FILE_POINTER ((DWORD)-1) +#endif + +#ifndef IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS +#define IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS 5636096 +#endif + +#ifndef IOCTL_DISK_GET_DRIVE_GEOMETRY +#define IOCTL_DISK_GET_DRIVE_GEOMETRY 0x70000 +#endif + +#ifndef IOCTL_GET_DISK_LENGTH_INFO +#define IOCTL_GET_DISK_LENGTH_INFO 0x7405c +#endif + +#ifndef FSCTL_ALLOW_EXTENDED_DASD_IO +#define FSCTL_ALLOW_EXTENDED_DASD_IO 0x90083 +#endif + +/* Windows 2k+ imports. */ +typedef HANDLE (WINAPI *LPFN_FINDFIRSTVOLUME)(LPTSTR, DWORD); +typedef BOOL (WINAPI *LPFN_FINDNEXTVOLUME)(HANDLE, LPTSTR, DWORD); +typedef BOOL (WINAPI *LPFN_FINDVOLUMECLOSE)(HANDLE); +typedef BOOL (WINAPI *LPFN_SETFILEPOINTEREX)(HANDLE, LARGE_INTEGER, + PLARGE_INTEGER, DWORD); + +static LPFN_FINDFIRSTVOLUME fnFindFirstVolume = NULL; +static LPFN_FINDNEXTVOLUME fnFindNextVolume = NULL; +static LPFN_FINDVOLUMECLOSE fnFindVolumeClose = NULL; +static LPFN_SETFILEPOINTEREX fnSetFilePointerEx = NULL; + +#ifdef UNICODE +#define FNPOSTFIX "W" +#else +#define FNPOSTFIX "A" +#endif + +enum { /* see http://msdn.microsoft.com/en-us/library/cc704588(v=prot.10).aspx */ + STATUS_UNKNOWN = -1, + STATUS_SUCCESS = 0x00000000, + STATUS_BUFFER_OVERFLOW = 0x80000005, + STATUS_INVALID_HANDLE = 0xC0000008, + STATUS_INVALID_PARAMETER = 0xC000000D, + STATUS_INVALID_DEVICE_REQUEST = 0xC0000010, + STATUS_END_OF_FILE = 0xC0000011, + STATUS_CONFLICTING_ADDRESSES = 0xC0000018, + STATUS_NO_MATCH = 0xC000001E, + STATUS_ACCESS_DENIED = 0xC0000022, + STATUS_BUFFER_TOO_SMALL = 0xC0000023, + STATUS_OBJECT_TYPE_MISMATCH = 0xC0000024, + STATUS_FILE_NOT_FOUND = 0xC0000028, + STATUS_OBJECT_NAME_INVALID = 0xC0000033, + STATUS_OBJECT_NAME_NOT_FOUND = 0xC0000034, + STATUS_SHARING_VIOLATION = 0xC0000043, + STATUS_INVALID_PARAMETER_1 = 0xC00000EF, + STATUS_IO_DEVICE_ERROR = 0xC0000185, + STATUS_GUARD_PAGE_VIOLATION = 0x80000001 + } ; + +typedef u32 NTSTATUS; /* do not let the compiler choose the size */ +#ifdef __x86_64__ +typedef unsigned long long ULONG_PTR; /* an integer the same size as a pointer */ +#else +typedef unsigned long ULONG_PTR; /* an integer the same size as a pointer */ +#endif + +HANDLE get_osfhandle(int); /* from msvcrt.dll */ + +/* + * A few needed definitions not included in + */ + +typedef struct _IO_STATUS_BLOCK { + union { + NTSTATUS Status; + PVOID Pointer; + }; + ULONG_PTR Information; +} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK; + +typedef struct _UNICODE_STRING { + USHORT Length; + USHORT MaximumLength; +#ifdef __x86_64__ + u32 padding; +#endif + PWSTR Buffer; +} UNICODE_STRING, *PUNICODE_STRING; + +typedef struct _OBJECT_ATTRIBUTES { + ULONG Length; +#ifdef __x86_64__ + u32 padding1; + HANDLE RootDirectory; + PUNICODE_STRING ObjectName; + ULONG Attributes; + u32 padding2; +#else + HANDLE RootDirectory; + PUNICODE_STRING ObjectName; + ULONG Attributes; +#endif + PVOID SecurityDescriptor; + PVOID SecurityQualityOfService; +} OBJECT_ATTRIBUTES, *POBJECT_ATTRIBUTES; + +#define FILE_OPEN 1 +#define FILE_CREATE 2 +#define FILE_OVERWRITE 4 +#define FILE_SYNCHRONOUS_IO_ALERT 0x10 +#define FILE_SYNCHRONOUS_IO_NONALERT 0x20 +#define OBJ_CASE_INSENSITIVE 0x40 + +typedef void (WINAPI *PIO_APC_ROUTINE)(void*, PIO_STATUS_BLOCK, ULONG); + +extern WINAPI NTSTATUS NtOpenFile( + PHANDLE FileHandle, + ACCESS_MASK DesiredAccess, + POBJECT_ATTRIBUTES ObjectAttributes, + PIO_STATUS_BLOCK IoStatusBlock, + ULONG ShareAccess, + ULONG OpenOptions +); + +extern WINAPI NTSTATUS NtReadFile( + HANDLE FileHandle, + HANDLE Event, + PIO_APC_ROUTINE ApcRoutine, + PVOID ApcContext, + PIO_STATUS_BLOCK IoStatusBlock, + PVOID Buffer, + ULONG Length, + PLARGE_INTEGER ByteOffset, + PULONG Key +); + +extern WINAPI NTSTATUS NtWriteFile( + HANDLE FileHandle, + HANDLE Event, + PIO_APC_ROUTINE ApcRoutine, + PVOID ApcContext, + PIO_STATUS_BLOCK IoStatusBlock, + LPCVOID Buffer, + ULONG Length, + PLARGE_INTEGER ByteOffset, + PULONG Key +); + +extern NTSTATUS WINAPI NtClose( + HANDLE Handle +); + +extern NTSTATUS WINAPI NtDeviceIoControlFile( + HANDLE FileHandle, + HANDLE Event, + PIO_APC_ROUTINE ApcRoutine, + PVOID ApcContext, + PIO_STATUS_BLOCK IoStatusBlock, + ULONG IoControlCode, + PVOID InputBuffer, + ULONG InputBufferLength, + PVOID OutputBuffer, + ULONG OutputBufferLength +); + +extern NTSTATUS WINAPI NtFsControlFile( + HANDLE FileHandle, + HANDLE Event, + PIO_APC_ROUTINE ApcRoutine, + PVOID ApcContext, + PIO_STATUS_BLOCK IoStatusBlock, + ULONG FsControlCode, + PVOID InputBuffer, + ULONG InputBufferLength, + PVOID OutputBuffer, + ULONG OutputBufferLength +); + +/** + * struct win32_fd - + */ +typedef struct { + HANDLE handle; + s64 pos; /* Logical current position on the volume. */ + s64 part_start; + s64 part_length; + int part_hidden_sectors; + s64 geo_size, geo_cylinders; + s32 geo_sector_size; + s64 volume_size; + DWORD geo_sectors, geo_heads; + HANDLE vol_handle; + BOOL ntdll; +} win32_fd; + +/** + * ntfs_w32error_to_errno - convert a win32 error code to the unix one + * @w32error: the win32 error code + * + * Limited to a relatively small but useful number of codes. + */ +static int ntfs_w32error_to_errno(unsigned int w32error) +{ + ntfs_log_trace("Converting w32error 0x%x.\n",w32error); + switch (w32error) { + case ERROR_INVALID_FUNCTION: + return EBADRQC; + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_NAME: + return ENOENT; + case ERROR_TOO_MANY_OPEN_FILES: + return EMFILE; + case ERROR_ACCESS_DENIED: + return EACCES; + case ERROR_INVALID_HANDLE: + return EBADF; + case ERROR_NOT_ENOUGH_MEMORY: + return ENOMEM; + case ERROR_OUTOFMEMORY: + return ENOSPC; + case ERROR_INVALID_DRIVE: + case ERROR_BAD_UNIT: + return ENODEV; + case ERROR_WRITE_PROTECT: + return EROFS; + case ERROR_NOT_READY: + case ERROR_SHARING_VIOLATION: + return EBUSY; + case ERROR_BAD_COMMAND: + return EINVAL; + case ERROR_SEEK: + case ERROR_NEGATIVE_SEEK: + return ESPIPE; + case ERROR_NOT_SUPPORTED: + return EOPNOTSUPP; + case ERROR_BAD_NETPATH: + return ENOSHARE; + default: + /* generic message */ + return ENOMSG; + } +} + +static int ntfs_ntstatus_to_errno(NTSTATUS status) +{ + ntfs_log_trace("Converting w32error 0x%x.\n",w32error); + switch (status) { + case STATUS_INVALID_HANDLE : + case STATUS_INVALID_PARAMETER : + case STATUS_OBJECT_NAME_INVALID : + case STATUS_INVALID_DEVICE_REQUEST : + return (EINVAL); + case STATUS_ACCESS_DENIED : + return (EACCES); + case STATUS_IO_DEVICE_ERROR : + case STATUS_END_OF_FILE : + return (EIO); + case STATUS_SHARING_VIOLATION : + return (EBUSY); + default: + /* generic message */ + return ENOMSG; + } +} + +/** + * libntfs_SetFilePointerEx - emulation for SetFilePointerEx() + * + * We use this to emulate SetFilePointerEx() when it is not present. This can + * happen since SetFilePointerEx() only exists in Win2k+. + */ +static BOOL WINAPI libntfs_SetFilePointerEx(HANDLE hFile, + LARGE_INTEGER liDistanceToMove, + PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod) +{ + liDistanceToMove.u.LowPart = SetFilePointer(hFile, + liDistanceToMove.u.LowPart, + &liDistanceToMove.u.HighPart, dwMoveMethod); + SetLastError(NO_ERROR); + if (liDistanceToMove.u.LowPart == INVALID_SET_FILE_POINTER && + GetLastError() != NO_ERROR) { + if (lpNewFilePointer) + lpNewFilePointer->QuadPart = -1; + return FALSE; + } + if (lpNewFilePointer) + lpNewFilePointer->QuadPart = liDistanceToMove.QuadPart; + return TRUE; +} + +/** + * ntfs_device_win32_init_imports - initialize the function pointers + * + * The Find*Volume and SetFilePointerEx functions exist only on win2k+, as such + * we cannot just staticly import them. + * + * This function initializes the imports if the functions do exist and in the + * SetFilePointerEx case, we emulate the function ourselves if it is not + * present. + * + * Note: The values are cached, do be afraid to run it more than once. + */ +static void ntfs_device_win32_init_imports(void) +{ + HMODULE kernel32 = GetModuleHandle("kernel32"); + if (!kernel32) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("kernel32.dll could not be imported.\n"); + } + if (!fnSetFilePointerEx) { + if (kernel32) + fnSetFilePointerEx = (LPFN_SETFILEPOINTEREX) + GetProcAddress(kernel32, + "SetFilePointerEx"); + /* + * If we did not get kernel32.dll or it is not Win2k+, emulate + * SetFilePointerEx(). + */ + if (!fnSetFilePointerEx) { + ntfs_log_debug("SetFilePointerEx() not found in " + "kernel32.dll: Enabling emulation.\n"); + fnSetFilePointerEx = libntfs_SetFilePointerEx; + } + } + /* Cannot do lookups if we could not get kernel32.dll... */ + if (!kernel32) + return; + if (!fnFindFirstVolume) + fnFindFirstVolume = (LPFN_FINDFIRSTVOLUME) + GetProcAddress(kernel32, "FindFirstVolume" + FNPOSTFIX); + if (!fnFindNextVolume) + fnFindNextVolume = (LPFN_FINDNEXTVOLUME) + GetProcAddress(kernel32, "FindNextVolume" + FNPOSTFIX); + if (!fnFindVolumeClose) + fnFindVolumeClose = (LPFN_FINDVOLUMECLOSE) + GetProcAddress(kernel32, "FindVolumeClose"); +} + +/** + * ntfs_device_unix_status_flags_to_win32 - convert unix->win32 open flags + * @flags: unix open status flags + * + * Supported flags are O_RDONLY, O_WRONLY and O_RDWR. + */ +static __inline__ int ntfs_device_unix_status_flags_to_win32(int flags) +{ + int win_mode; + + switch (flags & O_ACCMODE) { + case O_RDONLY: + win_mode = GENERIC_READ; + break; + case O_WRONLY: + win_mode = GENERIC_WRITE; + break; + case O_RDWR: + win_mode = GENERIC_READ | GENERIC_WRITE; + break; + default: + /* error */ + ntfs_log_trace("Unknown status flags.\n"); + win_mode = 0; + } + return win_mode; +} + + +/** + * ntfs_device_win32_simple_open_file - just open a file via win32 API + * @filename: name of the file to open + * @handle: pointer the a HANDLE in which to put the result + * @flags: unix open status flags + * @locking: will the function gain an exclusive lock on the file? + * + * Supported flags are O_RDONLY, O_WRONLY and O_RDWR. + * + * Return 0 if o.k. + * -1 if not, and errno set. In this case handle is trashed. + */ +static int ntfs_device_win32_simple_open_file(const char *filename, + HANDLE *handle, int flags, BOOL locking) +{ + *handle = CreateFile(filename, + ntfs_device_unix_status_flags_to_win32(flags), + locking ? 0 : (FILE_SHARE_WRITE | FILE_SHARE_READ), + NULL, (flags & O_CREAT ? OPEN_ALWAYS : OPEN_EXISTING), + 0, NULL); + if (*handle == INVALID_HANDLE_VALUE) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("CreateFile(%s) failed.\n", filename); + return -1; + } + return 0; +} + +/** + * ntfs_device_win32_lock - lock the volume + * @handle: a win32 HANDLE for a volume to lock + * + * Locking a volume means no one can access its contents. + * Exiting the process automatically unlocks the volume, except in old NT4s. + * + * Return 0 if o.k. + * -1 if not, and errno set. + */ +static int ntfs_device_win32_lock(HANDLE handle) +{ + DWORD i; + + if (!DeviceIoControl(handle, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &i, + NULL)) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("Couldn't lock volume.\n"); + return -1; + } + ntfs_log_debug("Volume locked.\n"); + return 0; +} + +/** + * ntfs_device_win32_unlock - unlock the volume + * @handle: the win32 HANDLE which the volume was locked with + * + * Return 0 if o.k. + * -1 if not, and errno set. + */ +static int ntfs_device_win32_unlock(HANDLE handle) +{ + DWORD i; + + if (!DeviceIoControl(handle, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &i, + NULL)) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("Couldn't unlock volume.\n"); + return -1; + } + ntfs_log_debug("Volume unlocked.\n"); + return 0; +} + +static int ntfs_device_win32_setlock(HANDLE handle, ULONG code) +{ + IO_STATUS_BLOCK io_status; + NTSTATUS res; + + io_status.Status = STATUS_SUCCESS; + io_status.Information = 0; + res = NtFsControlFile(handle,(HANDLE)NULL, + (PIO_APC_ROUTINE)NULL,(void*)NULL, + &io_status, code, + (char*)NULL,0,(char*)NULL,0); + if (res != STATUS_SUCCESS) + errno = ntfs_ntstatus_to_errno(res); + return (res == STATUS_SUCCESS ? 0 : -1); +} + +/** + * ntfs_device_win32_dismount - dismount a volume + * @handle: a win32 HANDLE for a volume to dismount + * + * Dismounting means the system will refresh the volume in the first change it + * gets. Usefull after altering the file structures. + * The volume must be locked by the current process while dismounting. + * A side effect is that the volume is also unlocked, but you must not rely om + * this. + * + * Return 0 if o.k. + * -1 if not, and errno set. + */ +static int ntfs_device_win32_dismount(HANDLE handle) +{ + DWORD i; + + if (!DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, + &i, NULL)) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("Couldn't dismount volume.\n"); + return -1; + } + ntfs_log_debug("Volume dismounted.\n"); + return 0; +} + +/** + * ntfs_device_win32_getsize - get file size via win32 API + * @handle: pointer the file HANDLE obtained via open + * + * Only works on ordinary files. + * + * Return The file size if o.k. + * -1 if not, and errno set. + */ +static s64 ntfs_device_win32_getsize(HANDLE handle) +{ + LONG loword, hiword; + + SetLastError(NO_ERROR); + hiword = 0; + loword = SetFilePointer(handle, 0, &hiword, 2); + if ((loword == INVALID_SET_FILE_POINTER) + && (GetLastError() != NO_ERROR)) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("Couldn't get file size.\n"); + return -1; + } + return ((s64)hiword << 32) + (ULONG)loword; +} + +/** + * ntfs_device_win32_getdisklength - get disk size via win32 API + * @handle: pointer the file HANDLE obtained via open + * @argp: pointer to result buffer + * + * Only works on PhysicalDriveX type handles. + * + * Return The disk size if o.k. + * -1 if not, and errno set. + */ +static s64 ntfs_device_win32_getdisklength(HANDLE handle) +{ + GET_LENGTH_INFORMATION buf; + DWORD i; + + if (!DeviceIoControl(handle, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &buf, + sizeof(buf), &i, NULL)) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("Couldn't get disk length.\n"); + return -1; + } + ntfs_log_debug("Disk length: %lld.\n", buf.Length.QuadPart); + return buf.Length.QuadPart; +} + +/** + * ntfs_device_win32_getntfssize - get NTFS volume size via win32 API + * @handle: pointer the file HANDLE obtained via open + * @argp: pointer to result buffer + * + * Only works on NTFS volume handles. + * An annoying bug in windows is that an NTFS volume does not occupy the entire + * partition, namely not the last sector (which holds the backup boot sector, + * and normally not interesting). + * Use this function to get the length of the accessible space through a given + * volume handle. + * + * Return The volume size if o.k. + * -1 if not, and errno set. + */ +static s64 ntfs_device_win32_getntfssize(HANDLE handle) +{ + s64 rvl; +#ifdef FSCTL_GET_NTFS_VOLUME_DATA + DWORD i; + NTFS_VOLUME_DATA_BUFFER buf; + + if (!DeviceIoControl(handle, FSCTL_GET_NTFS_VOLUME_DATA, NULL, 0, &buf, + sizeof(buf), &i, NULL)) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("Couldn't get NTFS volume length.\n"); + return -1; + } + rvl = buf.NumberSectors.QuadPart * buf.BytesPerSector; + ntfs_log_debug("NTFS volume length: 0x%llx.\n", (long long)rvl); +#else + errno = EINVAL; + rvl = -1; +#endif + return rvl; +} + +/** + * ntfs_device_win32_getgeo - get CHS information of a drive + * @handle: an open handle to the PhysicalDevice + * @fd: a win_fd structure that will be filled + * + * Return 0 if o.k. + * -1 if not, and errno set. + * + * In Windows NT+: fills size, sectors, and cylinders and sets heads to -1. + * In Windows XP+: fills size, sectors, cylinders, and heads. + * + * Note: In pre XP, this requires write permission, even though nothing is + * actually written. + * + * If fails, sets sectors, cylinders, heads, and size to -1. + */ +static int ntfs_device_win32_getgeo(HANDLE handle, win32_fd *fd) +{ + DWORD i; + BOOL rvl; + BYTE b[sizeof(DISK_GEOMETRY) + sizeof(DISK_PARTITION_INFO) + + sizeof(DISK_DETECTION_INFO) + 512]; + + rvl = DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, + 0, &b, sizeof(b), &i, NULL); + if (rvl) { + ntfs_log_debug("GET_DRIVE_GEOMETRY_EX detected.\n"); + DISK_DETECTION_INFO *ddi = (PDISK_DETECTION_INFO) + (((PBYTE)(&((PDISK_GEOMETRY_EX)b)->Data)) + + (((PDISK_PARTITION_INFO) + (&((PDISK_GEOMETRY_EX)b)->Data))-> + SizeOfPartitionInfo)); + fd->geo_cylinders = ((DISK_GEOMETRY*)&b)->Cylinders.QuadPart; + fd->geo_sectors = ((DISK_GEOMETRY*)&b)->SectorsPerTrack; + fd->geo_size = ((DISK_GEOMETRY_EX*)&b)->DiskSize.QuadPart; + fd->geo_sector_size = NTFS_BLOCK_SIZE; + switch (ddi->DetectionType) { + case DetectInt13: + fd->geo_cylinders = ddi->Int13.MaxCylinders; + fd->geo_sectors = ddi->Int13.SectorsPerTrack; + fd->geo_heads = ddi->Int13.MaxHeads; + return 0; + case DetectExInt13: + fd->geo_cylinders = ddi->ExInt13.ExCylinders; + fd->geo_sectors = ddi->ExInt13.ExSectorsPerTrack; + fd->geo_heads = ddi->ExInt13.ExHeads; + return 0; + case DetectNone: + default: + break; + } + } else + fd->geo_heads = -1; + rvl = DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, + &b, sizeof(b), &i, NULL); + if (rvl) { + ntfs_log_debug("GET_DRIVE_GEOMETRY detected.\n"); + fd->geo_cylinders = ((DISK_GEOMETRY*)&b)->Cylinders.QuadPart; + fd->geo_sectors = ((DISK_GEOMETRY*)&b)->SectorsPerTrack; + fd->geo_size = fd->geo_cylinders * fd->geo_sectors * + ((DISK_GEOMETRY*)&b)->TracksPerCylinder * + ((DISK_GEOMETRY*)&b)->BytesPerSector; + fd->geo_sector_size = ((DISK_GEOMETRY*)&b)->BytesPerSector; + return 0; + } + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("Couldn't retrieve disk geometry.\n"); + fd->geo_cylinders = -1; + fd->geo_sectors = -1; + fd->geo_size = -1; + fd->geo_sector_size = NTFS_BLOCK_SIZE; + return -1; +} + +static int ntfs_device_win32_getntgeo(HANDLE handle, win32_fd *fd) +{ + DISK_GEOMETRY geo; + NTSTATUS st; + IO_STATUS_BLOCK status; + u64 bytes; + int res; + + res = -1; + fd->geo_cylinders = 0; + fd->geo_sectors = 0; + fd->geo_size = 1073741824; + fd->geo_sectors = fd->geo_size >> 9; + fd->geo_sector_size = NTFS_BLOCK_SIZE; + + st = NtDeviceIoControlFile(handle, (HANDLE)NULL, + (PIO_APC_ROUTINE)NULL, (void*)NULL, + &status, IOCTL_DISK_GET_DRIVE_GEOMETRY, (void*)NULL, 0, + (void*)&geo, sizeof(geo)); + if (st == STATUS_SUCCESS) { + /* over-estimate the (rounded) number of cylinders */ + fd->geo_cylinders = geo.Cylinders.QuadPart + 1; + fd->geo_sectors = fd->geo_cylinders + *geo.TracksPerCylinder*geo.SectorsPerTrack; + fd->geo_size = fd->geo_sectors*geo.BytesPerSector; + fd->geo_sector_size = geo.BytesPerSector; + res = 0; + /* try to get the exact sector count */ + st = NtDeviceIoControlFile(handle, (HANDLE)NULL, + (PIO_APC_ROUTINE)NULL, (void*)NULL, + &status, IOCTL_GET_DISK_LENGTH_INFO, + (void*)NULL, 0, + (void*)&bytes, sizeof(bytes)); + if (st == STATUS_SUCCESS) { + fd->geo_size = bytes; + fd->geo_sectors = bytes/geo.BytesPerSector; + } + } + return (res); +} + +/** + * ntfs_device_win32_open_file - open a file via win32 API + * @filename: name of the file to open + * @fd: pointer to win32 file device in which to put the result + * @flags: unix open status flags + * + * Return 0 if o.k. + * -1 if not, and errno set. + */ +static __inline__ int ntfs_device_win32_open_file(char *filename, win32_fd *fd, + int flags) +{ + HANDLE handle; + int mode; + + if (ntfs_device_win32_simple_open_file(filename, &handle, flags, + FALSE)) { + /* open error */ + return -1; + } + mode = flags & O_ACCMODE; + if ((mode == O_RDWR) || (mode == O_WRONLY)) { + DWORD bytes; + + /* try making sparse (but ignore errors) */ + DeviceIoControl(handle, FSCTL_SET_SPARSE, + (void*)NULL, 0, (void*)NULL, 0, + &bytes, (LPOVERLAPPED)NULL); + } + /* fill fd */ + fd->handle = handle; + fd->part_start = 0; + fd->part_length = ntfs_device_win32_getsize(handle); + fd->pos = 0; + fd->part_hidden_sectors = -1; + fd->geo_size = -1; /* used as a marker that this is a file */ + fd->vol_handle = INVALID_HANDLE_VALUE; + fd->geo_sector_size = 512; /* will be adjusted from the boot sector */ + fd->ntdll = FALSE; + return 0; +} + +/** + * ntfs_device_win32_open_drive - open a drive via win32 API + * @drive_id: drive to open + * @fd: pointer to win32 file device in which to put the result + * @flags: unix open status flags + * + * return 0 if o.k. + * -1 if not, and errno set. + */ +static __inline__ int ntfs_device_win32_open_drive(int drive_id, win32_fd *fd, + int flags) +{ + HANDLE handle; + int err; + char filename[MAX_PATH]; + + sprintf(filename, "\\\\.\\PhysicalDrive%d", drive_id); + if ((err = ntfs_device_win32_simple_open_file(filename, &handle, flags, + TRUE))) { + /* open error */ + return err; + } + /* store the drive geometry */ + ntfs_device_win32_getgeo(handle, fd); + /* Just to be sure */ + if (fd->geo_size == -1) + fd->geo_size = ntfs_device_win32_getdisklength(handle); + /* fill fd */ + fd->ntdll = FALSE; + fd->handle = handle; + fd->part_start = 0; + fd->part_length = fd->geo_size; + fd->pos = 0; + fd->part_hidden_sectors = -1; + fd->vol_handle = INVALID_HANDLE_VALUE; + return 0; +} + +/** + * ntfs_device_win32_open_lowlevel - open a drive via low level win32 API + * @drive_id: drive to open + * @fd: pointer to win32 file device in which to put the result + * @flags: unix open status flags + * + * return 0 if o.k. + * -1 if not, and errno set. + */ +static __inline__ int ntfs_device_win32_open_lowlevel(int drive_id, + win32_fd *fd, int flags) +{ + HANDLE handle; + NTSTATUS st; + ACCESS_MASK access; + ULONG share; + OBJECT_ATTRIBUTES attr; + IO_STATUS_BLOCK io_status; + UNICODE_STRING unicode_name; + ntfschar unicode_buffer[7]; + int mode; + static const ntfschar unicode_init[] = { + const_cpu_to_le16('\\'), const_cpu_to_le16('?'), + const_cpu_to_le16('?'), const_cpu_to_le16('\\'), + const_cpu_to_le16(' '), const_cpu_to_le16(':'), + const_cpu_to_le16(0) + }; + + memcpy(unicode_buffer, unicode_init, sizeof(unicode_buffer)); + unicode_buffer[4] = cpu_to_le16(drive_id + 'A'); + unicode_name.Buffer = unicode_buffer; + unicode_name.Length = 6*sizeof(ntfschar); + unicode_name.MaximumLength = 6*sizeof(ntfschar); + + attr.Length = sizeof(OBJECT_ATTRIBUTES); + attr.RootDirectory = (HANDLE*)NULL; + attr.ObjectName = &unicode_name; + attr.Attributes = OBJ_CASE_INSENSITIVE; + attr.SecurityDescriptor = (void*)NULL; + attr.SecurityQualityOfService = (void*)NULL; + + io_status.Status = 0; + io_status.Information = 0; + mode = flags & O_ACCMODE; + share = (mode == O_RDWR ? + 0 : FILE_SHARE_READ | FILE_SHARE_WRITE); + access = (mode == O_RDWR ? + FILE_READ_DATA | FILE_WRITE_DATA | SYNCHRONIZE + : FILE_READ_DATA | SYNCHRONIZE); + + st = NtOpenFile(&handle, access, + &attr, &io_status, + share, + FILE_SYNCHRONOUS_IO_ALERT); + if (st != STATUS_SUCCESS) { + errno = ntfs_ntstatus_to_errno(st); + return (-1); + } + ntfs_device_win32_setlock(handle,FSCTL_LOCK_VOLUME); + /* store the drive geometry */ + ntfs_device_win32_getntgeo(handle, fd); + fd->ntdll = TRUE; + /* allow accessing the full partition */ + st = NtFsControlFile(handle, (HANDLE)NULL, + (PIO_APC_ROUTINE)NULL, + (PVOID)NULL, &io_status, + FSCTL_ALLOW_EXTENDED_DASD_IO, + NULL, 0, NULL, 0); + if (st != STATUS_SUCCESS) { + errno = ntfs_ntstatus_to_errno(st); + NtClose(handle); + return (-1); + } + /* fill fd */ + fd->handle = handle; + fd->part_start = 0; + fd->part_length = fd->geo_size; + fd->pos = 0; + fd->part_hidden_sectors = -1; + fd->vol_handle = INVALID_HANDLE_VALUE; + return 0; +} + +/** + * ntfs_device_win32_open_volume_for_partition - find and open a volume + * + * Windows NT/2k/XP handles volumes instead of partitions. + * This function gets the partition details and return an open volume handle. + * That volume is the one whose only physical location on disk is the described + * partition. + * + * The function required Windows 2k/XP, otherwise it fails (gracefully). + * + * Return success: a valid open volume handle. + * fail : INVALID_HANDLE_VALUE + */ +static HANDLE ntfs_device_win32_open_volume_for_partition(unsigned int drive_id, + s64 part_offset, s64 part_length, int flags) +{ + HANDLE vol_find_handle; + TCHAR vol_name[MAX_PATH]; + + /* Make sure all the required imports exist. */ + if (!fnFindFirstVolume || !fnFindNextVolume || !fnFindVolumeClose) { + ntfs_log_trace("Required dll imports not found.\n"); + return INVALID_HANDLE_VALUE; + } + /* Start iterating through volumes. */ + ntfs_log_trace("Entering with drive_id=%d, part_offset=%lld, " + "path_length=%lld, flags=%d.\n", drive_id, + (unsigned long long)part_offset, + (unsigned long long)part_length, flags); + vol_find_handle = fnFindFirstVolume(vol_name, MAX_PATH); + /* If a valid handle could not be aquired, reply with "don't know". */ + if (vol_find_handle == INVALID_HANDLE_VALUE) { + ntfs_log_trace("FindFirstVolume failed.\n"); + return INVALID_HANDLE_VALUE; + } + do { + int vol_name_length; + HANDLE handle; + + /* remove trailing '/' from vol_name */ +#ifdef UNICODE + vol_name_length = wcslen(vol_name); +#else + vol_name_length = strlen(vol_name); +#endif + if (vol_name_length>0) + vol_name[vol_name_length-1]=0; + + ntfs_log_debug("Processing %s.\n", vol_name); + /* open the file */ + handle = CreateFile(vol_name, + ntfs_device_unix_status_flags_to_win32(flags), + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, + OPEN_EXISTING, 0, NULL); + if (handle != INVALID_HANDLE_VALUE) { + DWORD bytesReturned; +#define EXTENTS_SIZE sizeof(VOLUME_DISK_EXTENTS) + 9 * sizeof(DISK_EXTENT) + char extents[EXTENTS_SIZE]; + + /* Check physical locations. */ + if (DeviceIoControl(handle, + IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, + NULL, 0, extents, EXTENTS_SIZE, + &bytesReturned, NULL)) { + if (((VOLUME_DISK_EXTENTS *)extents)-> + NumberOfDiskExtents == 1) { + DISK_EXTENT *extent = &(( + VOLUME_DISK_EXTENTS *) + extents)->Extents[0]; + if ((extent->DiskNumber==drive_id) && + (extent->StartingOffset. + QuadPart==part_offset) + && (extent-> + ExtentLength.QuadPart + == part_length)) { + /* + * Eureka! (Archimedes, 287 BC, + * "I have found it!") + */ + fnFindVolumeClose( + vol_find_handle); + return handle; + } + } + } + } else + ntfs_log_trace("getExtents() Failed.\n"); + } while (fnFindNextVolume(vol_find_handle, vol_name, MAX_PATH)); + /* End of iteration through volumes. */ + ntfs_log_trace("Closing, volume was not found.\n"); + fnFindVolumeClose(vol_find_handle); + return INVALID_HANDLE_VALUE; +} + +/** + * ntfs_device_win32_find_partition - locates partition details by id. + * @handle: HANDLE to the PhysicalDrive + * @partition_id: the partition number to locate + * @part_offset: pointer to where to put the offset to the partition + * @part_length: pointer to where to put the length of the partition + * @hidden_sectors: pointer to where to put the hidden sectors + * + * This function requires an open PhysicalDrive handle and a partition_id. + * If a partition with the required id is found on the supplied device, + * the partition attributes are returned back. + * + * Returns: TRUE if found, and sets the output parameters. + * FALSE if not and errno is set to the error code. + */ +static BOOL ntfs_device_win32_find_partition(HANDLE handle, DWORD partition_id, + s64 *part_offset, s64 *part_length, int *hidden_sectors) +{ + DRIVE_LAYOUT_INFORMATION *drive_layout; + unsigned int err, buf_size, part_count; + DWORD i; + + /* + * There is no way to know the required buffer, so if the ioctl fails, + * try doubling the buffer size each time until the ioctl succeeds. + */ + part_count = 8; + do { + buf_size = sizeof(DRIVE_LAYOUT_INFORMATION) + + part_count * sizeof(PARTITION_INFORMATION); + drive_layout = (DRIVE_LAYOUT_INFORMATION*)ntfs_malloc(buf_size); + if (!drive_layout) { + errno = ENOMEM; + return FALSE; + } + if (DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_LAYOUT, NULL, + 0, (BYTE*)drive_layout, buf_size, &i, NULL)) + break; + err = GetLastError(); + free(drive_layout); + if (err != ERROR_INSUFFICIENT_BUFFER) { + ntfs_log_trace("GetDriveLayout failed.\n"); + errno = ntfs_w32error_to_errno(err); + return FALSE; + } + ntfs_log_debug("More than %u partitions.\n", part_count); + part_count <<= 1; + if (part_count > 512) { + ntfs_log_trace("GetDriveLayout failed: More than 512 " + "partitions?\n"); + errno = ENOBUFS; + return FALSE; + } + } while (1); + for (i = 0; i < drive_layout->PartitionCount; i++) { + if (drive_layout->PartitionEntry[i].PartitionNumber == + partition_id) { + *part_offset = drive_layout->PartitionEntry[i]. + StartingOffset.QuadPart; + *part_length = drive_layout->PartitionEntry[i]. + PartitionLength.QuadPart; + *hidden_sectors = drive_layout->PartitionEntry[i]. + HiddenSectors; + free(drive_layout); + return TRUE; + } + } + free(drive_layout); + errno = ENOENT; + return FALSE; +} + +/** + * ntfs_device_win32_open_partition - open a partition via win32 API + * @drive_id: drive to open + * @partition_id: partition to open + * @fd: win32 file device to return + * @flags: unix open status flags + * + * Return 0 if o.k. + * -1 if not, and errno set. + * + * When fails, fd contents may have not been preserved. + */ +static int ntfs_device_win32_open_partition(int drive_id, + unsigned int partition_id, win32_fd *fd, int flags) +{ + s64 part_start, part_length; + HANDLE handle; + int err, hidden_sectors; + char drive_name[MAX_PATH]; + + sprintf(drive_name, "\\\\.\\PhysicalDrive%d", drive_id); + /* Open the entire device without locking, ask questions later */ + if ((err = ntfs_device_win32_simple_open_file(drive_name, &handle, + flags, FALSE))) { + /* error */ + return err; + } + if (ntfs_device_win32_find_partition(handle, partition_id, &part_start, + &part_length, &hidden_sectors)) { + s64 tmp; + HANDLE vol_handle = ntfs_device_win32_open_volume_for_partition( + drive_id, part_start, part_length, flags); + /* Store the drive geometry. */ + ntfs_device_win32_getgeo(handle, fd); + fd->handle = handle; + fd->pos = 0; + fd->part_start = part_start; + fd->part_length = part_length; + fd->part_hidden_sectors = hidden_sectors; + fd->geo_sector_size = 512; + fd->ntdll = FALSE; + tmp = ntfs_device_win32_getntfssize(vol_handle); + if (tmp > 0) + fd->geo_size = tmp; + else + fd->geo_size = fd->part_length; + if (vol_handle != INVALID_HANDLE_VALUE) { + if (((flags & O_RDWR) == O_RDWR) && + ntfs_device_win32_lock(vol_handle)) { + CloseHandle(vol_handle); + CloseHandle(handle); + return -1; + } + fd->vol_handle = vol_handle; + } else { + if ((flags & O_RDWR) == O_RDWR) { + /* Access if read-write, no volume found. */ + ntfs_log_trace("Partitions containing Spanned/" + "Mirrored volumes are not " + "supported in R/W status " + "yet.\n"); + CloseHandle(handle); + errno = EOPNOTSUPP; + return -1; + } + fd->vol_handle = INVALID_HANDLE_VALUE; + } + return 0; + } else { + ntfs_log_debug("Partition %u not found on drive %d.\n", + partition_id, drive_id); + CloseHandle(handle); + errno = ENODEV; + return -1; + } +} + +/** + * ntfs_device_win32_open - open a device + * @dev: a pointer to the NTFS_DEVICE to open + * @flags: unix open status flags + * + * @dev->d_name must hold the device name, the rest is ignored. + * Supported flags are O_RDONLY, O_WRONLY and O_RDWR. + * + * If name is in format "(hd[0-9],[0-9])" then open a partition. + * If name is in format "(hd[0-9])" then open a volume. + * Otherwise open a file. + */ +static int ntfs_device_win32_open(struct ntfs_device *dev, int flags) +{ + int drive_id = 0, numparams; + unsigned int part = 0; + char drive_char; + win32_fd fd; + int err; + + if (NDevOpen(dev)) { + errno = EBUSY; + return -1; + } + ntfs_device_win32_init_imports(); + numparams = sscanf(dev->d_name, "/dev/hd%c%u", &drive_char, &part); + if (!numparams + && (dev->d_name[1] == ':') + && (dev->d_name[2] == '\0')) { + drive_char = dev->d_name[0]; + numparams = 3; + drive_id = toupper(drive_char) - 'A'; + } + switch (numparams) { + case 0: + ntfs_log_debug("win32_open(%s) -> file.\n", dev->d_name); + err = ntfs_device_win32_open_file(dev->d_name, &fd, flags); + break; + case 1: + ntfs_log_debug("win32_open(%s) -> drive %d.\n", dev->d_name, + drive_id); + err = ntfs_device_win32_open_drive(drive_id, &fd, flags); + break; + case 2: + ntfs_log_debug("win32_open(%s) -> drive %d, part %u.\n", + dev->d_name, drive_id, part); + err = ntfs_device_win32_open_partition(drive_id, part, &fd, + flags); + break; + case 3: + ntfs_log_debug("win32_open(%s) -> drive %c:\n", + dev->d_name, drive_char); + err = ntfs_device_win32_open_lowlevel(drive_id, &fd, + flags); + break; + default: + ntfs_log_debug("win32_open(%s) -> unknwon file format.\n", + dev->d_name); + err = -1; + } + if (err) + return err; + ntfs_log_debug("win32_open(%s) -> %p, offset 0x%llx.\n", dev->d_name, + dev, fd.part_start); + /* Setup our read-only flag. */ + if ((flags & O_RDWR) != O_RDWR) + NDevSetReadOnly(dev); + dev->d_private = (win32_fd*)ntfs_malloc(sizeof(win32_fd)); + memcpy(dev->d_private, &fd, sizeof(win32_fd)); + NDevSetOpen(dev); + NDevClearDirty(dev); + return 0; +} + +/** + * ntfs_device_win32_seek - change current logical file position + * @dev: ntfs device obtained via ->open + * @offset: required offset from the whence anchor + * @whence: whence anchor specifying what @offset is relative to + * + * Return the new position on the volume on success and -1 on error with errno + * set to the error code. + * + * @whence may be one of the following: + * SEEK_SET - Offset is relative to file start. + * SEEK_CUR - Offset is relative to current position. + * SEEK_END - Offset is relative to end of file. + */ +static s64 ntfs_device_win32_seek(struct ntfs_device *dev, s64 offset, + int whence) +{ + s64 abs_ofs; + win32_fd *fd = (win32_fd *)dev->d_private; + + ntfs_log_trace("seek offset = 0x%llx, whence = %d.\n", offset, whence); + switch (whence) { + case SEEK_SET: + abs_ofs = offset; + break; + case SEEK_CUR: + abs_ofs = fd->pos + offset; + break; + case SEEK_END: + /* End of partition != end of disk. */ + if (fd->part_length == -1) { + ntfs_log_trace("Position relative to end of disk not " + "implemented.\n"); + errno = EOPNOTSUPP; + return -1; + } + abs_ofs = fd->part_length + offset; + break; + default: + ntfs_log_trace("Wrong mode %d.\n", whence); + errno = EINVAL; + return -1; + } + if ((abs_ofs < 0) + || (fd->ntdll && (abs_ofs > fd->part_length))) { + ntfs_log_trace("Seeking outsize seekable area.\n"); + errno = EINVAL; + return -1; + } + fd->pos = abs_ofs; + return abs_ofs; +} + +/** + * ntfs_device_win32_pio - positioned low level i/o + * @fd: win32 device descriptor obtained via ->open + * @pos: at which position to do i/o from/to + * @count: how many bytes should be transfered + * @b: source/destination buffer + * @write: TRUE if write transfer and FALSE if read transfer + * + * On success returns the number of bytes transfered (can be < @count) and on + * error returns -1 and errno set. Transfer starts from position @pos on @fd. + * + * Notes: + * - @pos, @buf, and @count must be aligned to geo_sector_size + * - When dealing with volumes, a single call must not span both volume + * and disk extents. + * - Does not use/set @fd->pos. + */ +static s64 ntfs_device_win32_pio(win32_fd *fd, const s64 pos, + const s64 count, void *rbuf, const void *wbuf) +{ + LARGE_INTEGER li; + HANDLE handle; + DWORD bt; + BOOL res; + s64 bytes; + + ntfs_log_trace("pos = 0x%llx, count = 0x%llx, direction = %s.\n", + (long long)pos, (long long)count, write ? "write" : + "read"); + li.QuadPart = pos; + if (fd->vol_handle != INVALID_HANDLE_VALUE && pos < fd->geo_size) { + ntfs_log_debug("Transfering via vol_handle.\n"); + handle = fd->vol_handle; + } else { + ntfs_log_debug("Transfering via handle.\n"); + handle = fd->handle; + li.QuadPart += fd->part_start; + } + + if (fd->ntdll) { + IO_STATUS_BLOCK io_status; + NTSTATUS res; + LARGE_INTEGER offset; + + io_status.Status = STATUS_SUCCESS; + io_status.Information = 0; + offset.QuadPart = pos; + if (wbuf) { + res = NtWriteFile(fd->handle,(HANDLE)NULL, + (PIO_APC_ROUTINE)NULL,(void*)NULL, + &io_status, wbuf, count, + &offset, (PULONG)NULL); + } else { + res = NtReadFile(fd->handle,(HANDLE)NULL, + (PIO_APC_ROUTINE)NULL,(void*)NULL, + &io_status, rbuf, count, + &offset, (PULONG)NULL); + } + if (res == STATUS_SUCCESS) { + bytes = io_status.Information; + } else { + bytes = -1; + errno = ntfs_ntstatus_to_errno(res); + } + } else { + if (!fnSetFilePointerEx(handle, li, NULL, FILE_BEGIN)) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("SetFilePointer failed.\n"); + return -1; + } + if (wbuf) + res = WriteFile(handle, wbuf, count, &bt, NULL); + else + res = ReadFile(handle, rbuf, count, &bt, NULL); + bytes = bt; + if (!res) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("%sFile() failed.\n", write ? + "Write" : "Read"); + return -1; + } + if (rbuf && !pos) { + /* get the sector size from the boot sector */ + char *boot = (char*)rbuf; + fd->geo_sector_size = (boot[11] & 255) + + ((boot[12] & 255) << 8); + } + } + return bytes; +} + +/** + * ntfs_device_win32_pread_simple - positioned simple read + * @fd: win32 device descriptor obtained via ->open + * @pos: at which position to read from + * @count: how many bytes should be read + * @b: a pointer to where to put the contents + * + * On success returns the number of bytes read (can be < @count) and on error + * returns -1 and errno set. Read starts from position @pos. + * + * Notes: + * - @pos, @buf, and @count must be aligned to geo_sector_size. + * - When dealing with volumes, a single call must not span both volume + * and disk extents. + * - Does not use/set @fd->pos. + */ +static inline s64 ntfs_device_win32_pread_simple(win32_fd *fd, const s64 pos, + const s64 count, void *b) +{ + return ntfs_device_win32_pio(fd, pos, count, b, (void*)NULL); +} + +/** + * ntfs_device_win32_read - read bytes from an ntfs device + * @dev: ntfs device obtained via ->open + * @b: pointer to where to put the contents + * @count: how many bytes should be read + * + * On success returns the number of bytes actually read (can be < @count). + * On error returns -1 with errno set. + */ +static s64 ntfs_device_win32_read(struct ntfs_device *dev, void *b, s64 count) +{ + s64 old_pos, to_read, i, br = 0; + win32_fd *fd = (win32_fd *)dev->d_private; + BYTE *alignedbuffer; + int old_ofs, ofs; + + old_pos = fd->pos; + old_ofs = ofs = old_pos & (fd->geo_sector_size - 1); + to_read = (ofs + count + fd->geo_sector_size - 1) & + ~(s64)(fd->geo_sector_size - 1); + /* Impose maximum of 2GB to be on the safe side. */ + if (to_read > 0x80000000) { + int delta = to_read - count; + to_read = 0x80000000; + count = to_read - delta; + } + ntfs_log_trace("fd = %p, b = %p, count = 0x%llx, pos = 0x%llx, " + "ofs = %i, to_read = 0x%llx.\n", fd, b, + (long long)count, (long long)old_pos, ofs, + (long long)to_read); + if (!((unsigned long)b & (fd->geo_sector_size - 1)) && !old_ofs && + !(count & (fd->geo_sector_size - 1))) + alignedbuffer = b; + else { + alignedbuffer = (BYTE *)VirtualAlloc(NULL, to_read, MEM_COMMIT, + PAGE_READWRITE); + if (!alignedbuffer) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("VirtualAlloc failed for read.\n"); + return -1; + } + } + if (fd->vol_handle != INVALID_HANDLE_VALUE && old_pos < fd->geo_size) { + s64 vol_to_read = fd->geo_size - old_pos; + if (count > vol_to_read) { + br = ntfs_device_win32_pread_simple(fd, + old_pos & ~(s64)(fd->geo_sector_size - 1), + ofs + vol_to_read, alignedbuffer); + if (br == -1) + goto read_error; + to_read -= br; + if (br < ofs) { + br = 0; + goto read_partial; + } + br -= ofs; + fd->pos += br; + ofs = fd->pos & (fd->geo_sector_size - 1); + if (br != vol_to_read) + goto read_partial; + } + } + i = ntfs_device_win32_pread_simple(fd, + fd->pos & ~(s64)(fd->geo_sector_size - 1), to_read, + alignedbuffer + br); + if (i == -1) { + if (br) + goto read_partial; + goto read_error; + } + if (i < ofs) + goto read_partial; + i -= ofs; + br += i; + if (br > count) + br = count; + fd->pos = old_pos + br; +read_partial: + if (alignedbuffer != b) { + memcpy((void*)b, alignedbuffer + old_ofs, br); + VirtualFree(alignedbuffer, 0, MEM_RELEASE); + } + return br; +read_error: + if (alignedbuffer != b) + VirtualFree(alignedbuffer, 0, MEM_RELEASE); + return -1; +} + +/** + * ntfs_device_win32_close - close an open ntfs deivce + * @dev: ntfs device obtained via ->open + * + * Return 0 if o.k. + * -1 if not, and errno set. Note if error fd->vol_handle is trashed. + */ +static int ntfs_device_win32_close(struct ntfs_device *dev) +{ + win32_fd *fd = (win32_fd *)dev->d_private; + BOOL rvl; + + ntfs_log_trace("Closing device %p.\n", dev); + if (!NDevOpen(dev)) { + errno = EBADF; + return -1; + } + if (fd->vol_handle != INVALID_HANDLE_VALUE) { + if (!NDevReadOnly(dev)) { + ntfs_device_win32_dismount(fd->vol_handle); + ntfs_device_win32_unlock(fd->vol_handle); + } + if (!CloseHandle(fd->vol_handle)) + ntfs_log_trace("CloseHandle() failed for volume.\n"); + } + if (fd->ntdll) { + ntfs_device_win32_setlock(fd->handle,FSCTL_UNLOCK_VOLUME); + rvl = NtClose(fd->handle) == STATUS_SUCCESS; + } else + rvl = CloseHandle(fd->handle); + NDevClearOpen(dev); + free(fd); + if (!rvl) { + errno = ntfs_w32error_to_errno(GetLastError()); + if (fd->ntdll) + ntfs_log_trace("NtClose() failed.\n"); + else + ntfs_log_trace("CloseHandle() failed.\n"); + return -1; + } + return 0; +} + +/** + * ntfs_device_win32_sync - flush write buffers to disk + * @dev: ntfs device obtained via ->open + * + * Return 0 if o.k. + * -1 if not, and errno set. + * + * Note: Volume syncing works differently in windows. + * Disk cannot be synced in windows. + */ +static int ntfs_device_win32_sync(struct ntfs_device *dev) +{ + int err = 0; + BOOL to_clear = TRUE; + + if (!NDevReadOnly(dev) && NDevDirty(dev)) { + win32_fd *fd = (win32_fd *)dev->d_private; + + if ((fd->vol_handle != INVALID_HANDLE_VALUE) && + !FlushFileBuffers(fd->vol_handle)) { + to_clear = FALSE; + err = ntfs_w32error_to_errno(GetLastError()); + } + if (!FlushFileBuffers(fd->handle)) { + to_clear = FALSE; + if (!err) + err = ntfs_w32error_to_errno(GetLastError()); + } + if (!to_clear) { + ntfs_log_trace("Could not sync.\n"); + errno = err; + return -1; + } + NDevClearDirty(dev); + } + return 0; +} + +/** + * ntfs_device_win32_pwrite_simple - positioned simple write + * @fd: win32 device descriptor obtained via ->open + * @pos: at which position to write to + * @count: how many bytes should be written + * @b: a pointer to the data to write + * + * On success returns the number of bytes written and on error returns -1 and + * errno set. Write starts from position @pos. + * + * Notes: + * - @pos, @buf, and @count must be aligned to geo_sector_size. + * - When dealing with volumes, a single call must not span both volume + * and disk extents. + * - Does not use/set @fd->pos. + */ +static inline s64 ntfs_device_win32_pwrite_simple(win32_fd *fd, const s64 pos, + const s64 count, const void *b) +{ + return ntfs_device_win32_pio(fd, pos, count, (void*)NULL, b); +} + +/** + * ntfs_device_win32_write - write bytes to an ntfs device + * @dev: ntfs device obtained via ->open + * @b: pointer to the data to write + * @count: how many bytes should be written + * + * On success returns the number of bytes actually written. + * On error returns -1 with errno set. + */ +static s64 ntfs_device_win32_write(struct ntfs_device *dev, const void *b, + s64 count) +{ + s64 old_pos, to_write, i, bw = 0; + win32_fd *fd = (win32_fd *)dev->d_private; + const BYTE *alignedbuffer; + BYTE *readbuffer; + int old_ofs, ofs; + + old_pos = fd->pos; + old_ofs = ofs = old_pos & (fd->geo_sector_size - 1); + to_write = (ofs + count + fd->geo_sector_size - 1) & + ~(s64)(fd->geo_sector_size - 1); + /* Impose maximum of 2GB to be on the safe side. */ + if (to_write > 0x80000000) { + int delta = to_write - count; + to_write = 0x80000000; + count = to_write - delta; + } + ntfs_log_trace("fd = %p, b = %p, count = 0x%llx, pos = 0x%llx, " + "ofs = %i, to_write = 0x%llx.\n", fd, b, + (long long)count, (long long)old_pos, ofs, + (long long)to_write); + if (NDevReadOnly(dev)) { + ntfs_log_trace("Can't write on a R/O device.\n"); + errno = EROFS; + return -1; + } + if (!count) + return 0; + NDevSetDirty(dev); + readbuffer = (BYTE*)NULL; + if (!((unsigned long)b & (fd->geo_sector_size - 1)) && !old_ofs && + !(count & (fd->geo_sector_size - 1))) + alignedbuffer = (const BYTE *)b; + else { + s64 end; + + readbuffer = (BYTE *)VirtualAlloc(NULL, to_write, + MEM_COMMIT, PAGE_READWRITE); + if (!readbuffer) { + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("VirtualAlloc failed for write.\n"); + return -1; + } + /* Read first sector if start of write not sector aligned. */ + if (ofs) { + i = ntfs_device_win32_pread_simple(fd, + old_pos & ~(s64)(fd->geo_sector_size - 1), + fd->geo_sector_size, readbuffer); + if (i != fd->geo_sector_size) { + if (i >= 0) + errno = EIO; + goto write_error; + } + } + /* + * Read last sector if end of write not sector aligned and last + * sector is either not the same as the first sector or it is + * the same as the first sector but this has not been read in + * yet, i.e. the start of the write is sector aligned. + */ + end = old_pos + count; + if ((end & (fd->geo_sector_size - 1)) && + ((to_write > fd->geo_sector_size) || !ofs)) { + i = ntfs_device_win32_pread_simple(fd, + end & ~(s64)(fd->geo_sector_size - 1), + fd->geo_sector_size, readbuffer + + to_write - fd->geo_sector_size); + if (i != fd->geo_sector_size) { + if (i >= 0) + errno = EIO; + goto write_error; + } + } + /* Copy the data to be written into @readbuffer. */ + memcpy(readbuffer + ofs, b, count); + alignedbuffer = readbuffer; + } + if (fd->vol_handle != INVALID_HANDLE_VALUE && old_pos < fd->geo_size) { + s64 vol_to_write = fd->geo_size - old_pos; + if (count > vol_to_write) { + bw = ntfs_device_win32_pwrite_simple(fd, + old_pos & ~(s64)(fd->geo_sector_size - 1), + ofs + vol_to_write, alignedbuffer); + if (bw == -1) + goto write_error; + to_write -= bw; + if (bw < ofs) { + bw = 0; + goto write_partial; + } + bw -= ofs; + fd->pos += bw; + ofs = fd->pos & (fd->geo_sector_size - 1); + if (bw != vol_to_write) + goto write_partial; + } + } + i = ntfs_device_win32_pwrite_simple(fd, + fd->pos & ~(s64)(fd->geo_sector_size - 1), to_write, + alignedbuffer + bw); + if (i == -1) { + if (bw) + goto write_partial; + goto write_error; + } + if (i < ofs) + goto write_partial; + i -= ofs; + bw += i; + if (bw > count) + bw = count; + fd->pos = old_pos + bw; +write_partial: + if (readbuffer) + VirtualFree(readbuffer, 0, MEM_RELEASE); + return bw; +write_error: + bw = -1; + goto write_partial; +} + +/** + * ntfs_device_win32_stat - get a unix-like stat structure for an ntfs device + * @dev: ntfs device obtained via ->open + * @buf: pointer to the stat structure to fill + * + * Note: Only st_mode, st_size, and st_blocks are filled. + * + * Return 0 if o.k. + * -1 if not and errno set. in this case handle is trashed. + */ +static int ntfs_device_win32_stat(struct ntfs_device *dev, struct stat *buf) +{ + win32_fd *fd = (win32_fd *)dev->d_private; + mode_t st_mode; + + if ((dev->d_name[1] == ':') && (dev->d_name[2] == '\0')) + st_mode = S_IFBLK; + else + switch (GetFileType(fd->handle)) { + case FILE_TYPE_CHAR: + st_mode = S_IFCHR; + break; + case FILE_TYPE_DISK: + st_mode = S_IFREG; + break; + case FILE_TYPE_PIPE: + st_mode = S_IFIFO; + break; + default: + st_mode = 0; + } + memset(buf, 0, sizeof(struct stat)); + buf->st_mode = st_mode; + buf->st_size = fd->part_length; + if (buf->st_size != -1) + buf->st_blocks = buf->st_size >> 9; + else + buf->st_size = 0; + return 0; +} + +#ifdef HDIO_GETGEO +/** + * ntfs_win32_hdio_getgeo - get drive geometry + * @dev: ntfs device obtained via ->open + * @argp: pointer to where to put the output + * + * Note: Works on windows NT/2k/XP only. + * + * Return 0 if o.k. + * -1 if not, and errno set. Note if error fd->handle is trashed. + */ +static __inline__ int ntfs_win32_hdio_getgeo(struct ntfs_device *dev, + struct hd_geometry *argp) +{ + win32_fd *fd = (win32_fd *)dev->d_private; + + argp->heads = fd->geo_heads; + argp->sectors = fd->geo_sectors; + argp->cylinders = fd->geo_cylinders; + argp->start = fd->part_hidden_sectors; + return 0; +} +#endif + +/** + * ntfs_win32_blksszget - get block device sector size + * @dev: ntfs device obtained via ->open + * @argp: pointer to where to put the output + * + * Note: Works on windows NT/2k/XP only. + * + * Return 0 if o.k. + * -1 if not, and errno set. Note if error fd->handle is trashed. + */ +static __inline__ int ntfs_win32_blksszget(struct ntfs_device *dev,int *argp) +{ + win32_fd *fd = (win32_fd *)dev->d_private; + DWORD bytesReturned; + DISK_GEOMETRY dg; + + if (DeviceIoControl(fd->handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, + &dg, sizeof(DISK_GEOMETRY), &bytesReturned, NULL)) { + /* success */ + *argp = dg.BytesPerSector; + return 0; + } + errno = ntfs_w32error_to_errno(GetLastError()); + ntfs_log_trace("GET_DRIVE_GEOMETRY failed.\n"); + return -1; +} + +static int ntfs_device_win32_ioctl(struct ntfs_device *dev, + unsigned long request, void *argp) +{ +#if defined(BLKGETSIZE) | defined(BLKGETSIZE64) + win32_fd *fd = (win32_fd *)dev->d_private; +#endif + + ntfs_log_trace("win32_ioctl(0x%lx) called.\n", request); + switch (request) { +#if defined(BLKGETSIZE) + case BLKGETSIZE: + ntfs_log_debug("BLKGETSIZE detected.\n"); + if (fd->part_length >= 0) { + *(int *)argp = (int)(fd->part_length / 512); + return 0; + } + errno = EOPNOTSUPP; + return -1; +#endif +#if defined(BLKGETSIZE64) + case BLKGETSIZE64: + ntfs_log_debug("BLKGETSIZE64 detected.\n"); + if (fd->part_length >= 0) { + *(s64 *)argp = fd->part_length; + return 0; + } + errno = EOPNOTSUPP; + return -1; +#endif +#ifdef HDIO_GETGEO + case HDIO_GETGEO: + ntfs_log_debug("HDIO_GETGEO detected.\n"); + return ntfs_win32_hdio_getgeo(dev, (struct hd_geometry *)argp); +#endif +#ifdef BLKSSZGET + case BLKSSZGET: + ntfs_log_debug("BLKSSZGET detected.\n"); + if (fd && !fd->ntdll) { + *(int*)argp = fd->geo_sector_size; + return (0); + } else + return ntfs_win32_blksszget(dev, (int *)argp); +#endif +#ifdef BLKBSZSET + case BLKBSZSET: + ntfs_log_debug("BLKBSZSET detected.\n"); + /* Nothing to do on Windows. */ + return 0; +#endif + default: + ntfs_log_debug("unimplemented ioctl 0x%lx.\n", request); + errno = EOPNOTSUPP; + return -1; + } +} + +static s64 ntfs_device_win32_pread(struct ntfs_device *dev, void *b, + s64 count, s64 offset) +{ + s64 got; + win32_fd *fd; + + /* read the fast way if sector aligned */ + fd = (win32_fd*)dev->d_private; + if (!((count | offset) & (fd->geo_sector_size - 1))) { + got = ntfs_device_win32_pio(fd, offset, count, b, (void*)NULL); + } else { + if (ntfs_device_win32_seek(dev, offset, 0) == -1) + got = 0; + else + got = ntfs_device_win32_read(dev, b, count); + } + + return (got); +} + +static s64 ntfs_device_win32_pwrite(struct ntfs_device *dev, const void *b, + s64 count, s64 offset) +{ + s64 put; + win32_fd *fd; + + /* write the fast way if sector aligned */ + fd = (win32_fd*)dev->d_private; + if (!((count | offset) & (fd->geo_sector_size - 1))) { + put = ntfs_device_win32_pio(fd, offset, count, (void*)NULL, b); + } else { + if (ntfs_device_win32_seek(dev, offset, 0) == -1) + put = 0; + else + put = ntfs_device_win32_write(dev, b, count); + } + return (put); +} + +struct ntfs_device_operations ntfs_device_win32_io_ops = { + .open = ntfs_device_win32_open, + .close = ntfs_device_win32_close, + .seek = ntfs_device_win32_seek, + .read = ntfs_device_win32_read, + .write = ntfs_device_win32_write, + .pread = ntfs_device_win32_pread, + .pwrite = ntfs_device_win32_pwrite, + .sync = ntfs_device_win32_sync, + .stat = ntfs_device_win32_stat, + .ioctl = ntfs_device_win32_ioctl +}; + +/* + * Mark an open file as sparse + * + * This is only called by ntfsclone when cloning a volume to a file. + * The argument is the target file, not a volume. + * + * Returns 0 if successful. + */ + +int ntfs_win32_set_sparse(int fd) +{ + BOOL ok; + HANDLE handle; + DWORD bytes; + + handle = get_osfhandle(fd); + if (handle == INVALID_HANDLE_VALUE) + ok = FALSE; + else + ok = DeviceIoControl(handle, FSCTL_SET_SPARSE, + (void*)NULL, 0, (void*)NULL, 0, + &bytes, (LPOVERLAPPED)NULL); + return (!ok); +} + +/* + * Resize an open file + * + * This is only called by ntfsclone when cloning a volume to a file. + * The argument must designate a file, not a volume. + * + * Returns 0 if successful. + */ + +static int win32_ftruncate(HANDLE handle, s64 size) +{ + BOOL ok; + LONG hsize, lsize; + LONG ohsize, olsize; + + if (handle == INVALID_HANDLE_VALUE) + ok = FALSE; + else { + SetLastError(NO_ERROR); + /* save original position */ + ohsize = 0; + olsize = SetFilePointer(handle, 0, &ohsize, 1); + hsize = size >> 32; + lsize = size & 0xffffffff; + ok = (SetFilePointer(handle, lsize, &hsize, 0) == (DWORD)lsize) + && (GetLastError() == NO_ERROR) + && SetEndOfFile(handle); + /* restore original position, even if above failed */ + SetFilePointer(handle, olsize, &ohsize, 0); + if (GetLastError() != NO_ERROR) + ok = FALSE; + } + if (!ok) + errno = EINVAL; + return (ok ? 0 : -1); +} + +int ntfs_device_win32_ftruncate(struct ntfs_device *dev, s64 size) +{ + win32_fd *fd; + int ret; + + ret = -1; + fd = (win32_fd*)dev->d_private; + if (fd && !fd->ntdll) + ret = win32_ftruncate(fd->handle, size); + return (ret); +} + +int ntfs_win32_ftruncate(int fd, s64 size) +{ + int ret; + HANDLE handle; + + handle = get_osfhandle(fd); + ret = win32_ftruncate(handle, size); + return (ret); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/xattrs.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/xattrs.c new file mode 100644 index 0000000..97adb7d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/libntfs-3g/xattrs.c @@ -0,0 +1,808 @@ +/** + * xattrs.c : common functions to deal with system extended attributes + * + * Copyright (c) 2010-2014 Jean-Pierre Andre + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "types.h" +#include "param.h" +#include "layout.h" +#include "attrib.h" +#include "index.h" +#include "dir.h" +#include "security.h" +#include "acls.h" +#include "efs.h" +#include "reparse.h" +#include "object_id.h" +#include "ea.h" +#include "misc.h" +#include "logging.h" +#include "xattrs.h" + +#if POSIXACLS +#if __BYTE_ORDER == __BIG_ENDIAN + +/* + * Posix ACL structures + */ + +struct LE_POSIX_ACE { + le16 tag; + le16 perms; + le32 id; +} __attribute__((__packed__)); + +struct LE_POSIX_ACL { + u8 version; + u8 flags; + le16 filler; + struct LE_POSIX_ACE ace[1]; +} __attribute__((__packed__)); + +#endif +#endif + +static const char nf_ns_xattr_ntfs_acl[] = "system.ntfs_acl"; +static const char nf_ns_xattr_attrib[] = "system.ntfs_attrib"; +static const char nf_ns_xattr_attrib_be[] = "system.ntfs_attrib_be"; +static const char nf_ns_xattr_efsinfo[] = "system.ntfs_efsinfo"; +static const char nf_ns_xattr_reparse[] = "system.ntfs_reparse_data"; +static const char nf_ns_xattr_object_id[] = "system.ntfs_object_id"; +static const char nf_ns_xattr_dos_name[] = "system.ntfs_dos_name"; +static const char nf_ns_xattr_times[] = "system.ntfs_times"; +static const char nf_ns_xattr_times_be[] = "system.ntfs_times_be"; +static const char nf_ns_xattr_crtime[] = "system.ntfs_crtime"; +static const char nf_ns_xattr_crtime_be[] = "system.ntfs_crtime_be"; +static const char nf_ns_xattr_ea[] = "system.ntfs_ea"; +static const char nf_ns_xattr_posix_access[] = "system.posix_acl_access"; +static const char nf_ns_xattr_posix_default[] = "system.posix_acl_default"; + +static const char nf_ns_alt_xattr_efsinfo[] = "user.ntfs.efsinfo"; + +struct XATTRNAME { + enum SYSTEMXATTRS xattr; + const char *name; +} ; + +static struct XATTRNAME nf_ns_xattr_names[] = { + { XATTR_NTFS_ACL, nf_ns_xattr_ntfs_acl }, + { XATTR_NTFS_ATTRIB, nf_ns_xattr_attrib }, + { XATTR_NTFS_ATTRIB_BE, nf_ns_xattr_attrib_be }, + { XATTR_NTFS_EFSINFO, nf_ns_xattr_efsinfo }, + { XATTR_NTFS_REPARSE_DATA, nf_ns_xattr_reparse }, + { XATTR_NTFS_OBJECT_ID, nf_ns_xattr_object_id }, + { XATTR_NTFS_DOS_NAME, nf_ns_xattr_dos_name }, + { XATTR_NTFS_TIMES, nf_ns_xattr_times }, + { XATTR_NTFS_TIMES_BE, nf_ns_xattr_times_be }, + { XATTR_NTFS_CRTIME, nf_ns_xattr_crtime }, + { XATTR_NTFS_CRTIME_BE, nf_ns_xattr_crtime_be }, + { XATTR_NTFS_EA, nf_ns_xattr_ea }, + { XATTR_POSIX_ACC, nf_ns_xattr_posix_access }, + { XATTR_POSIX_DEF, nf_ns_xattr_posix_default }, + { XATTR_UNMAPPED, (char*)NULL } /* terminator */ +}; + +/* + * Make an integer big-endian + * + * Swap bytes on a small-endian computer and does nothing on a + * big-endian computer. + */ + +static void fix_big_endian(char *p, int size) +{ +#if __BYTE_ORDER == __LITTLE_ENDIAN + int i,j; + int c; + + i = 0; + j = size - 1; + while (i < j) { + c = p[i]; + p[i++] = p[j]; + p[j--] = c; + } +#endif +} + +#if POSIXACLS +#if __BYTE_ORDER == __BIG_ENDIAN + +/* + * Make a Posix ACL CPU endian + */ + +static int le_acl_to_cpu(const struct LE_POSIX_ACL *le_acl, size_t size, + struct POSIX_ACL *acl) +{ + int i; + int cnt; + + acl->version = le_acl->version; + acl->flags = le_acl->flags; + acl->filler = 0; + cnt = (size - sizeof(struct LE_POSIX_ACL)) / sizeof(struct LE_POSIX_ACE); + for (i=0; iace[i].tag = le16_to_cpu(le_acl->ace[i].tag); + acl->ace[i].perms = le16_to_cpu(le_acl->ace[i].perms); + acl->ace[i].id = le32_to_cpu(le_acl->ace[i].id); + } + return (0); +} + +/* + * Make a Posix ACL little endian + */ + +int cpu_to_le_acl(const struct POSIX_ACL *acl, size_t size, + struct LE_POSIX_ACL *le_acl) +{ + int i; + int cnt; + + le_acl->version = acl->version; + le_acl->flags = acl->flags; + le_acl->filler = const_cpu_to_le16(0); + cnt = (size - sizeof(struct POSIX_ACL)) / sizeof(struct POSIX_ACE); + for (i=0; iace[i].tag = cpu_to_le16(acl->ace[i].tag); + le_acl->ace[i].perms = cpu_to_le16(acl->ace[i].perms); + le_acl->ace[i].id = cpu_to_le32(acl->ace[i].id); + } + return (0); +} + +#endif +#endif + +/* + * Determine whether an extended attribute is mapped to + * internal data (original name in system namespace, or renamed) + */ + +enum SYSTEMXATTRS ntfs_xattr_system_type(const char *name, + ntfs_volume *vol) +{ + struct XATTRNAME *p; + enum SYSTEMXATTRS ret; +#ifdef XATTR_MAPPINGS + const struct XATTRMAPPING *q; +#endif /* XATTR_MAPPINGS */ + + p = nf_ns_xattr_names; + while (p->name && strcmp(p->name,name)) + p++; + ret = p->xattr; +#ifdef XATTR_MAPPINGS + if (!p->name && vol && vol->xattr_mapping) { + q = vol->xattr_mapping; + while (q && strcmp(q->name,name)) + q = q->next; + if (q) + ret = q->xattr; + } +#else /* XATTR_MAPPINGS */ + if (!p->name + && vol + && vol->efs_raw + && !strcmp(nf_ns_alt_xattr_efsinfo,name)) + ret = XATTR_NTFS_EFSINFO; +#endif /* XATTR_MAPPINGS */ + return (ret); +} + +#ifdef XATTR_MAPPINGS + +/* + * Basic read from a user mapping file on another volume + */ + +static int basicread(void *fileid, char *buf, size_t size, off_t offs __attribute__((unused))) +{ + return (read(*(int*)fileid, buf, size)); +} + + +/* + * Read from a user mapping file on current NTFS partition + */ + +static int localread(void *fileid, char *buf, size_t size, off_t offs) +{ + return (ntfs_attr_data_read((ntfs_inode*)fileid, + AT_UNNAMED, 0, buf, size, offs)); +} + +/* + * Get a single mapping item from buffer + * + * Always reads a full line, truncating long lines + * Refills buffer when exhausted + * Returns pointer to item, or NULL when there is no more + * Note : errors are logged, but not returned +// TODO partially share with acls.c + */ + +static struct XATTRMAPPING *getmappingitem(FILEREADER reader, void *fileid, + off_t *poffs, char *buf, int *psrc, s64 *psize) +{ + int src; + int dst; + char *pe; + char *ps; + char *pu; + enum SYSTEMXATTRS xattr; + int gotend; + char maptext[LINESZ]; + struct XATTRMAPPING *item; + + src = *psrc; + dst = 0; + do { + gotend = 0; + while ((src < *psize) + && (buf[src] != '\n')) { + /* ignore spaces */ + if ((dst < LINESZ) + && (buf[src] != '\r') + && (buf[src] != '\t') + && (buf[src] != ' ')) + maptext[dst++] = buf[src]; + src++; + } + if (src >= *psize) { + *poffs += *psize; + *psize = reader(fileid, buf, (size_t)BUFSZ, *poffs); + src = 0; + } else { + gotend = 1; + src++; + maptext[dst] = '\0'; + dst = 0; + } + } while (*psize && ((maptext[0] == '#') || !gotend)); + item = (struct XATTRMAPPING*)NULL; + if (gotend) { + /* decompose into system name and user name */ + ps = maptext; + pu = strchr(maptext,':'); + if (pu) { + *pu++ = 0; + pe = strchr(pu,':'); + if (pe) + *pe = 0; + /* check name validity */ + if ((strlen(pu) < 6) || strncmp(pu,"user.",5)) + pu = (char*)NULL; + xattr = ntfs_xattr_system_type(ps, + (ntfs_volume*)NULL); + if (xattr == XATTR_UNMAPPED) + pu = (char*)NULL; + } + if (pu) { + item = (struct XATTRMAPPING*)ntfs_malloc( + sizeof(struct XATTRMAPPING) + + strlen(pu)); + if (item) { + item->xattr = xattr; + strcpy(item->name,pu); + item->next = (struct XATTRMAPPING*)NULL; + } + } else { + ntfs_log_early_error("Bad xattr mapping item, aborting\n"); + } + } + *psrc = src; + return (item); +} + +/* + * Read xattr mapping file and split into their attribute. + * Parameters are kept in a chained list. + * Returns the head of list, if any + * Errors are logged, but not returned + * + * If an absolute path is provided, the mapping file is assumed + * to be located in another mounted file system, and plain read() + * are used to get its contents. + * If a relative path is provided, the mapping file is assumed + * to be located on the current file system, and internal IO + * have to be used since we are still mounting and we have not + * entered the fuse loop yet. + */ + +static struct XATTRMAPPING *ntfs_read_xattr_mapping(FILEREADER reader, + void *fileid) +{ + char buf[BUFSZ]; + struct XATTRMAPPING *item; + struct XATTRMAPPING *current; + struct XATTRMAPPING *firstitem; + struct XATTRMAPPING *lastitem; + BOOL duplicated; + int src; + off_t offs; + s64 size; + + firstitem = (struct XATTRMAPPING*)NULL; + lastitem = (struct XATTRMAPPING*)NULL; + offs = 0; + size = reader(fileid, buf, (size_t)BUFSZ, (off_t)0); + if (size > 0) { + src = 0; + do { + item = getmappingitem(reader, fileid, &offs, + buf, &src, &size); + if (item) { + /* check no double mapping */ + duplicated = FALSE; + for (current=firstitem; current; current=current->next) + if ((current->xattr == item->xattr) + || !strcmp(current->name,item->name)) + duplicated = TRUE; + if (duplicated) { + free(item); + ntfs_log_early_error("Conflicting xattr mapping ignored\n"); + } else { + item->next = (struct XATTRMAPPING*)NULL; + if (lastitem) + lastitem->next = item; + else + firstitem = item; + lastitem = item; + } + } + } while (item); + } + return (firstitem); +} + +/* + * Build the extended attribute mappings to user namespace + * + * Note : no error is returned. If we refused mounting when there + * is an error it would be too difficult to fix the offending file + */ + +struct XATTRMAPPING *ntfs_xattr_build_mapping(ntfs_volume *vol, + const char *xattrmap_path) +{ + struct XATTRMAPPING *firstmapping; + struct XATTRMAPPING *mapping; + BOOL user_efs; + BOOL notfound; + ntfs_inode *ni; + int fd; + + firstmapping = (struct XATTRMAPPING*)NULL; + notfound = FALSE; + if (!xattrmap_path) + xattrmap_path = XATTRMAPPINGFILE; + if (xattrmap_path[0] == '/') { + fd = open(xattrmap_path,O_RDONLY); + if (fd > 0) { + firstmapping = ntfs_read_xattr_mapping(basicread, (void*)&fd); + close(fd); + } else + notfound = TRUE; + } else { + ni = ntfs_pathname_to_inode(vol, NULL, xattrmap_path); + if (ni) { + firstmapping = ntfs_read_xattr_mapping(localread, ni); + ntfs_inode_close(ni); + } else + notfound = TRUE; + } + if (notfound && strcmp(xattrmap_path, XATTRMAPPINGFILE)) { + ntfs_log_early_error("Could not open \"%s\"\n",xattrmap_path); + } + if (vol->efs_raw) { + user_efs = TRUE; + for (mapping=firstmapping; mapping; mapping=mapping->next) + if (mapping->xattr == XATTR_NTFS_EFSINFO) + user_efs = FALSE; + } else + user_efs = FALSE; + if (user_efs) { + mapping = (struct XATTRMAPPING*)ntfs_malloc( + sizeof(struct XATTRMAPPING) + + strlen(nf_ns_alt_xattr_efsinfo)); + if (mapping) { + mapping->next = firstmapping; + mapping->xattr = XATTR_NTFS_EFSINFO; + strcpy(mapping->name,nf_ns_alt_xattr_efsinfo); + firstmapping = mapping; + } + } + return (firstmapping); +} + +void ntfs_xattr_free_mapping(struct XATTRMAPPING *mapping) +{ + struct XATTRMAPPING *p, *q; + + p = mapping; + while (p) { + q = p->next; + free(p); + p = q; + } +} + +#endif /* XATTR_MAPPINGS */ + +/* + * Get an NTFS attribute into an extended attribute + * + * Returns the non-negative size of attribute if successful, + * or negative, with errno set, when fails + * Note : the size is returned even if no buffer is provided + * for returning the attribute, or if it is zero-sized. + */ + +int ntfs_xattr_system_getxattr(struct SECURITY_CONTEXT *scx, + enum SYSTEMXATTRS attr, + ntfs_inode *ni, ntfs_inode *dir_ni, + char *value, size_t size) +{ + int res; + int i; +#if POSIXACLS +#if __BYTE_ORDER == __BIG_ENDIAN + struct POSIX_ACL *acl; +#endif +#endif + + switch (attr) { + case XATTR_NTFS_ACL : + res = ntfs_get_ntfs_acl(scx, ni, value, size); + break; +#if POSIXACLS +#if __BYTE_ORDER == __BIG_ENDIAN + case XATTR_POSIX_ACC : + acl = (struct POSIX_ACL*)ntfs_malloc(size); + if (acl) { + res = ntfs_get_posix_acl(scx, ni, + nf_ns_xattr_posix_access, (char*)acl, size); + if (res > 0) { + if (cpu_to_le_acl(acl,res, + (struct LE_POSIX_ACL*)value)) + res = -errno; + } + free(acl); + } else + res = -errno; + break; + case XATTR_POSIX_DEF : + acl = (struct POSIX_ACL*)ntfs_malloc(size); + if (acl) { + res = ntfs_get_posix_acl(scx, ni, + nf_ns_xattr_posix_default, (char*)acl, size); + if (res > 0) { + if (cpu_to_le_acl(acl,res, + (struct LE_POSIX_ACL*)value)) + res = -errno; + } + free(acl); + } else + res = -errno; + break; +#else + case XATTR_POSIX_ACC : + res = ntfs_get_posix_acl(scx, ni, nf_ns_xattr_posix_access, + value, size); + break; + case XATTR_POSIX_DEF : + res = ntfs_get_posix_acl(scx, ni, nf_ns_xattr_posix_default, + value, size); + break; +#endif +#endif + case XATTR_NTFS_ATTRIB : + res = ntfs_get_ntfs_attrib(ni, value, size); + break; + case XATTR_NTFS_ATTRIB_BE : + res = ntfs_get_ntfs_attrib(ni, value, size); + if ((res == 4) && value) { + if (size >= 4) + fix_big_endian(value,4); + else + res = -EINVAL; + } + break; + case XATTR_NTFS_EFSINFO : + if (ni->vol->efs_raw) + res = ntfs_get_efs_info(ni, value, size); + else + res = -EPERM; + break; + case XATTR_NTFS_REPARSE_DATA : + res = ntfs_get_ntfs_reparse_data(ni, value, size); + break; + case XATTR_NTFS_OBJECT_ID : + res = ntfs_get_ntfs_object_id(ni, value, size); + break; + case XATTR_NTFS_DOS_NAME: + if (dir_ni) + res = ntfs_get_ntfs_dos_name(ni, dir_ni, value, size); + else + res = -errno; + break; + case XATTR_NTFS_TIMES: + res = ntfs_inode_get_times(ni, value, size); + break; + case XATTR_NTFS_TIMES_BE: + res = ntfs_inode_get_times(ni, value, size); + if ((res > 0) && value) { + for (i=0; (i+1)*sizeof(u64)<=(unsigned int)res; i++) + fix_big_endian(&value[i*sizeof(u64)], + sizeof(u64)); + } + break; + case XATTR_NTFS_CRTIME: + res = ntfs_inode_get_times(ni, value, + (size >= sizeof(u64) ? sizeof(u64) : size)); + break; + case XATTR_NTFS_CRTIME_BE: + res = ntfs_inode_get_times(ni, value, + (size >= sizeof(u64) ? sizeof(u64) : size)); + if ((res >= (int)sizeof(u64)) && value) + fix_big_endian(value,sizeof(u64)); + break; + case XATTR_NTFS_EA : + res = ntfs_get_ntfs_ea(ni, value, size); + break; + default : + errno = EOPNOTSUPP; + res = -errno; + break; + } + return (res); +} + +/* + * Set an NTFS attribute from an extended attribute + * + * Returns 0 if successful, + * non-zero, with errno set, when fails + */ + +int ntfs_xattr_system_setxattr(struct SECURITY_CONTEXT *scx, + enum SYSTEMXATTRS attr, + ntfs_inode *ni, ntfs_inode *dir_ni, + const char *value, size_t size, int flags) +{ + int res; + int i; + char buf[4*sizeof(u64)]; +#if POSIXACLS +#if __BYTE_ORDER == __BIG_ENDIAN + struct POSIX_ACL *acl; +#endif +#endif + + switch (attr) { + case XATTR_NTFS_ACL : + res = ntfs_set_ntfs_acl(scx, ni, value, size, flags); + break; +#if POSIXACLS +#if __BYTE_ORDER == __BIG_ENDIAN + case XATTR_POSIX_ACC : + acl = (struct POSIX_ACL*)ntfs_malloc(size); + if (acl) { + if (!le_acl_to_cpu((const struct LE_POSIX_ACL*)value, + size, acl)) { + res = ntfs_set_posix_acl(scx ,ni , + nf_ns_xattr_posix_access, + (char*)acl, size, flags); + } else + res = -errno; + free(acl); + } else + res = -errno; + break; + case XATTR_POSIX_DEF : + acl = (struct POSIX_ACL*)ntfs_malloc(size); + if (acl) { + if (!le_acl_to_cpu((const struct LE_POSIX_ACL*)value, + size, acl)) { + res = ntfs_set_posix_acl(scx ,ni , + nf_ns_xattr_posix_default, + (char*)acl, size, flags); + } else + res = -errno; + free(acl); + } else + res = -errno; + break; +#else + case XATTR_POSIX_ACC : + res = ntfs_set_posix_acl(scx ,ni , nf_ns_xattr_posix_access, + value, size, flags); + break; + case XATTR_POSIX_DEF : + res = ntfs_set_posix_acl(scx, ni, nf_ns_xattr_posix_default, + value, size, flags); + break; +#endif +#endif + case XATTR_NTFS_ATTRIB : + res = ntfs_set_ntfs_attrib(ni, value, size, flags); + break; + case XATTR_NTFS_ATTRIB_BE : + if (value && (size >= 4)) { + memcpy(buf,value,4); + fix_big_endian(buf,4); + res = ntfs_set_ntfs_attrib(ni, buf, 4, flags); + } else + res = ntfs_set_ntfs_attrib(ni, value, size, flags); + break; + case XATTR_NTFS_EFSINFO : + if (ni->vol->efs_raw) + res = ntfs_set_efs_info(ni, value, size, flags); + else { + errno = EPERM; + res = -EPERM; + } + break; + case XATTR_NTFS_REPARSE_DATA : + res = ntfs_set_ntfs_reparse_data(ni, value, size, flags); + break; + case XATTR_NTFS_OBJECT_ID : + res = ntfs_set_ntfs_object_id(ni, value, size, flags); + break; + case XATTR_NTFS_DOS_NAME: + if (dir_ni) + /* warning : this closes both inodes */ + res = ntfs_set_ntfs_dos_name(ni, dir_ni, value, + size, flags); + else { + errno = EINVAL; + res = -errno; + } + break; + case XATTR_NTFS_TIMES: + res = ntfs_inode_set_times(ni, value, size, flags); + break; + case XATTR_NTFS_TIMES_BE: + if (value && (size > 0) && (size <= 4*sizeof(u64))) { + memcpy(buf,value,size); + for (i=0; (i+1)*sizeof(u64)<=size; i++) + fix_big_endian(&buf[i*sizeof(u64)], + sizeof(u64)); + res = ntfs_inode_set_times(ni, buf, size, flags); + } else + res = ntfs_inode_set_times(ni, value, size, flags); + break; + case XATTR_NTFS_CRTIME: + res = ntfs_inode_set_times(ni, value, + (size >= sizeof(u64) ? sizeof(u64) : size), flags); + break; + case XATTR_NTFS_CRTIME_BE: + if (value && (size >= sizeof(u64))) { + memcpy(buf,value,sizeof(u64)); + fix_big_endian(buf,sizeof(u64)); + res = ntfs_inode_set_times(ni, buf, sizeof(u64), flags); + } else + res = ntfs_inode_set_times(ni, value, size, flags); + break; + case XATTR_NTFS_EA : + res = ntfs_set_ntfs_ea(ni, value, size, flags); + break; + default : + errno = EOPNOTSUPP; + res = -errno; + break; + } + return (res); +} + +int ntfs_xattr_system_removexattr(struct SECURITY_CONTEXT *scx, + enum SYSTEMXATTRS attr, + ntfs_inode *ni, ntfs_inode *dir_ni) +{ + int res; + + res = 0; + switch (attr) { + /* + * Removal of NTFS ACL, ATTRIB, EFSINFO or TIMES + * is never allowed + */ + case XATTR_NTFS_ACL : + case XATTR_NTFS_ATTRIB : + case XATTR_NTFS_ATTRIB_BE : + case XATTR_NTFS_EFSINFO : + case XATTR_NTFS_TIMES : + case XATTR_NTFS_TIMES_BE : + case XATTR_NTFS_CRTIME : + case XATTR_NTFS_CRTIME_BE : + res = -EPERM; + break; +#if POSIXACLS + case XATTR_POSIX_ACC : + case XATTR_POSIX_DEF : + if (ni) { + if (!ntfs_allowed_as_owner(scx, ni) + || ntfs_remove_posix_acl(scx, ni, + (attr == XATTR_POSIX_ACC ? + nf_ns_xattr_posix_access : + nf_ns_xattr_posix_default))) + res = -errno; + } else + res = -errno; + break; +#endif + case XATTR_NTFS_REPARSE_DATA : + if (ni) { + if (!ntfs_allowed_as_owner(scx, ni) + || ntfs_remove_ntfs_reparse_data(ni)) + res = -errno; + } else + res = -errno; + break; + case XATTR_NTFS_OBJECT_ID : + if (ni) { + if (!ntfs_allowed_as_owner(scx, ni) + || ntfs_remove_ntfs_object_id(ni)) + res = -errno; + } else + res = -errno; + break; + case XATTR_NTFS_DOS_NAME: + if (ni && dir_ni) { + if (ntfs_remove_ntfs_dos_name(ni,dir_ni)) + res = -errno; + /* ni and dir_ni have been closed */ + } else + res = -errno; + break; + case XATTR_NTFS_EA : + res = ntfs_remove_ntfs_ea(ni); + break; + default : + errno = EOPNOTSUPP; + res = -errno; + break; + } + return (res); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/m4/.keep b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/m4/.keep new file mode 100644 index 0000000..e69de29 diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/Makefile.am b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/Makefile.am new file mode 100644 index 0000000..babaf34 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/Makefile.am @@ -0,0 +1,195 @@ +if REALLYSTATIC +AM_LIBS = $(top_builddir)/libntfs-3g/.libs/libntfs-3g.a $(NTFSPROGS_STATIC_LIBS) +# older builds may need -static instead of newer -all-static +AM_LFLAGS = -static +STATIC_LINK = $(CC) $(AM_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ +else +AM_LIBS = $(top_builddir)/libntfs-3g/libntfs-3g.la +AM_LFLAGS = $(all_libraries) +LIBTOOL_LINK = $(LIBTOOL) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ +endif + +# Workaround to make REALLYSTATIC work with automake 1.5. +LINK=$(STATIC_LINK) $(LIBTOOL_LINK) + +if ENABLE_NTFSPROGS + +bin_PROGRAMS = ntfsfix ntfsinfo ntfscluster ntfsls ntfscat ntfscmp +sbin_PROGRAMS = mkntfs ntfslabel ntfsundelete ntfsresize ntfsclone \ + ntfscp +EXTRA_PROGRAM_NAMES = ntfswipe ntfstruncate ntfsrecover \ + ntfsusermap ntfssecaudit + +QUARANTINED_PROGRAM_NAMES = ntfsdump_logfile ntfsmftalloc ntfsmove ntfsck \ + ntfsfallocate + +man_MANS = mkntfs.8 \ + ntfscat.8 \ + ntfsclone.8 \ + ntfscluster.8 \ + ntfscmp.8 \ + ntfscp.8 \ + ntfsdecrypt.8 \ + ntfsfallocate.8 \ + ntfsfix.8 \ + ntfsinfo.8 \ + ntfslabel.8 \ + ntfsls.8 \ + ntfsprogs.8 \ + ntfsresize.8 \ + ntfsundelete.8 + +EXTRA_MANS = ntfsrecover.8 \ + ntfssecaudit.8 \ + ntfstruncate.8 \ + ntfsusermap.8 \ + ntfswipe.8 + +CLEANFILES = $(EXTRA_PROGRAMS) + +MAINTAINERCLEANFILES = Makefile.in + +if ENABLE_CRYPTO +EXTRA_PROGRAM_NAMES += ntfsdecrypt +endif + +if ENABLE_EXTRAS +bin_PROGRAMS += $(EXTRA_PROGRAM_NAMES) +if ENABLE_QUARANTINED +bin_PROGRAMS += $(QUARANTINED_PROGRAM_NAMES) +endif +man_MANS += $(EXTRA_MANS) +else +EXTRA_PROGRAMS = $(EXTRA_PROGRAM_NAMES) +endif + +# Set the include path. +AM_CPPFLAGS = -I$(top_srcdir)/include/ntfs-3g $(all_includes) + +ntfsfix_SOURCES = ntfsfix.c utils.c utils.h +ntfsfix_LDADD = $(AM_LIBS) +ntfsfix_LDFLAGS = $(AM_LFLAGS) + +mkntfs_CPPFLAGS = $(AM_CPPFLAGS) $(MKNTFS_CPPFLAGS) +mkntfs_SOURCES = attrdef.c attrdef.h boot.c boot.h sd.c sd.h mkntfs.c utils.c utils.h +mkntfs_LDADD = $(AM_LIBS) $(MKNTFS_LIBS) +mkntfs_LDFLAGS = $(AM_LFLAGS) + +ntfslabel_SOURCES = ntfslabel.c utils.c utils.h +ntfslabel_LDADD = $(AM_LIBS) +ntfslabel_LDFLAGS = $(AM_LFLAGS) + +ntfsinfo_SOURCES = ntfsinfo.c utils.c utils.h +ntfsinfo_LDADD = $(AM_LIBS) +ntfsinfo_LDFLAGS = $(AM_LFLAGS) + +ntfsundelete_SOURCES = ntfsundelete.c ntfsundelete.h utils.c utils.h list.h +ntfsundelete_LDADD = $(AM_LIBS) +ntfsundelete_LDFLAGS = $(AM_LFLAGS) + +ntfsresize_SOURCES = ntfsresize.c utils.c utils.h +ntfsresize_LDADD = $(AM_LIBS) +ntfsresize_LDFLAGS = $(AM_LFLAGS) + +ntfsclone_SOURCES = ntfsclone.c utils.c utils.h +ntfsclone_LDADD = $(AM_LIBS) +ntfsclone_LDFLAGS = $(AM_LFLAGS) + +ntfscluster_SOURCES = ntfscluster.c ntfscluster.h cluster.c cluster.h utils.c utils.h +ntfscluster_LDADD = $(AM_LIBS) +ntfscluster_LDFLAGS = $(AM_LFLAGS) + +ntfsls_SOURCES = ntfsls.c utils.c utils.h list.h +ntfsls_LDADD = $(AM_LIBS) +ntfsls_LDFLAGS = $(AM_LFLAGS) + +ntfscat_SOURCES = ntfscat.c ntfscat.h utils.c utils.h +ntfscat_LDADD = $(AM_LIBS) +ntfscat_LDFLAGS = $(AM_LFLAGS) + +ntfscp_SOURCES = ntfscp.c utils.c utils.h +ntfscp_LDADD = $(AM_LIBS) +ntfscp_LDFLAGS = $(AM_LFLAGS) + +ntfsck_SOURCES = ntfsck.c utils.c utils.h +ntfsck_LDADD = $(AM_LIBS) +ntfsck_LDFLAGS = $(AM_LFLAGS) + +ntfscmp_SOURCES = ntfscmp.c utils.c utils.h +ntfscmp_LDADD = $(AM_LIBS) +ntfscmp_LDFLAGS = $(AM_LFLAGS) + +ntfsrecover_SOURCES = playlog.c ntfsrecover.c utils.c utils.h ntfsrecover.h +ntfsrecover_LDADD = $(AM_LIBS) $(NTFSRECOVER_LIBS) +ntfsrecover_LDFLAGS = $(AM_LFLAGS) + +ntfsusermap_SOURCES = ntfsusermap.c utils.c utils.h +ntfsusermap_LDADD = $(AM_LIBS) $(NTFSRECOVER_LIBS) +ntfsusermap_LDFLAGS = $(AM_LFLAGS) + +ntfssecaudit_SOURCES = ntfssecaudit.c utils.c utils.h +ntfssecaudit_LDADD = $(AM_LIBS) $(NTFSRECOVER_LIBS) +ntfssecaudit_LDFLAGS = $(AM_LFLAGS) + +# We don't distribute these + +ntfstruncate_SOURCES = attrdef.c ntfstruncate.c utils.c utils.h +ntfstruncate_LDADD = $(AM_LIBS) +ntfstruncate_LDFLAGS = $(AM_LFLAGS) + +ntfsmftalloc_SOURCES = ntfsmftalloc.c utils.c utils.h +ntfsmftalloc_LDADD = $(AM_LIBS) +ntfsmftalloc_LDFLAGS = $(AM_LFLAGS) + +ntfsmove_SOURCES = ntfsmove.c ntfsmove.h utils.c utils.h +ntfsmove_LDADD = $(AM_LIBS) +ntfsmove_LDFLAGS = $(AM_LFLAGS) + +ntfswipe_SOURCES = ntfswipe.c ntfswipe.h utils.c utils.h +ntfswipe_LDADD = $(AM_LIBS) +ntfswipe_LDFLAGS = $(AM_LFLAGS) + +ntfsdump_logfile_SOURCES= ntfsdump_logfile.c +ntfsdump_logfile_LDADD = $(AM_LIBS) +ntfsdump_logfile_LDFLAGS= $(AM_LFLAGS) + +ntfsfallocate_SOURCES = ntfsfallocate.c utils.c utils.h +ntfsfallocate_LDADD = $(AM_LIBS) +ntfsfallocate_LDFLAGS = $(AM_LFLAGS) + +if ENABLE_CRYPTO +ntfsdecrypt_SOURCES = ntfsdecrypt.c utils.c utils.h +ntfsdecrypt_LDADD = $(AM_LIBS) $(GNUTLS_LIBS) $(LIBGCRYPT_LIBS) +ntfsdecrypt_LDFLAGS = $(AM_LFLAGS) +ntfsdecrypt_CFLAGS = $(GNUTLS_CFLAGS) $(LIBGCRYPT_CFLAGS) +endif + +# Extra targets + +strip: $(bin_PROGRAMS) $(sbin_PROGRAMS) + $(STRIP) $^ + +libs: + (cd ../libntfs-3g && $(MAKE) libs) || exit 1; + +extra: extras + +extras: libs $(EXTRA_PROGRAMS) + +# mkfs.ntfs[.8] hard link + +if ENABLE_MOUNT_HELPER +install-exec-hook: + $(INSTALL) -d $(DESTDIR)/$(sbindir) + $(LN_S) -f $(sbindir)/mkntfs $(DESTDIR)$(sbindir)/mkfs.ntfs + +install-data-hook: + $(INSTALL) -d $(DESTDIR)$(man8dir) + $(LN_S) -f mkntfs.8 $(DESTDIR)$(man8dir)/mkfs.ntfs.8 + +uninstall-local: + $(RM) -f $(DESTDIR)/sbin/mkfs.ntfs + $(RM) -f $(DESTDIR)$(man8dir)/mkfs.ntfs.8 +endif + +endif diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/attrdef.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/attrdef.c new file mode 100644 index 0000000..36501e5 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/attrdef.c @@ -0,0 +1,168 @@ +#include "attrdef.h" + +/** + * attrdef_ntfs3x_array + */ +const unsigned char attrdef_ntfs3x_array[2560] = { +0x24, 0x00, 0x53, 0x00, 0x54, 0x00, 0x41, 0x00, 0x4E, 0x00, 0x44, 0x00, 0x41, 0x00, 0x52, 0x00, +0x44, 0x00, 0x5F, 0x00, 0x49, 0x00, 0x4E, 0x00, 0x46, 0x00, 0x4F, 0x00, 0x52, 0x00, 0x4D, 0x00, +0x41, 0x00, 0x54, 0x00, 0x49, 0x00, 0x4F, 0x00, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, +0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x24, 0x00, 0x41, 0x00, 0x54, 0x00, 0x54, 0x00, 0x52, 0x00, 0x49, 0x00, 0x42, 0x00, 0x55, 0x00, +0x54, 0x00, 0x45, 0x00, 0x5F, 0x00, 0x4C, 0x00, 0x49, 0x00, 0x53, 0x00, 0x54, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0x24, 0x00, 0x46, 0x00, 0x49, 0x00, 0x4C, 0x00, 0x45, 0x00, 0x5F, 0x00, 0x4E, 0x00, 0x41, 0x00, +0x4D, 0x00, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x00, +0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x24, 0x00, 0x4F, 0x00, 0x42, 0x00, 0x4A, 0x00, 0x45, 0x00, 0x43, 0x00, 0x54, 0x00, 0x5F, 0x00, +0x49, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x24, 0x00, 0x53, 0x00, 0x45, 0x00, 0x43, 0x00, 0x55, 0x00, 0x52, 0x00, 0x49, 0x00, 0x54, 0x00, +0x59, 0x00, 0x5F, 0x00, 0x44, 0x00, 0x45, 0x00, 0x53, 0x00, 0x43, 0x00, 0x52, 0x00, 0x49, 0x00, +0x50, 0x00, 0x54, 0x00, 0x4F, 0x00, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0x24, 0x00, 0x56, 0x00, 0x4F, 0x00, 0x4C, 0x00, 0x55, 0x00, 0x4D, 0x00, 0x45, 0x00, 0x5F, 0x00, +0x4E, 0x00, 0x41, 0x00, 0x4D, 0x00, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, +0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x24, 0x00, 0x56, 0x00, 0x4F, 0x00, 0x4C, 0x00, 0x55, 0x00, 0x4D, 0x00, 0x45, 0x00, 0x5F, 0x00, +0x49, 0x00, 0x4E, 0x00, 0x46, 0x00, 0x4F, 0x00, 0x52, 0x00, 0x4D, 0x00, 0x41, 0x00, 0x54, 0x00, +0x49, 0x00, 0x4F, 0x00, 0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, +0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x24, 0x00, 0x44, 0x00, 0x41, 0x00, 0x54, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0x24, 0x00, 0x49, 0x00, 0x4E, 0x00, 0x44, 0x00, 0x45, 0x00, 0x58, 0x00, 0x5F, 0x00, 0x52, 0x00, +0x4F, 0x00, 0x4F, 0x00, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0x24, 0x00, 0x49, 0x00, 0x4E, 0x00, 0x44, 0x00, 0x45, 0x00, 0x58, 0x00, 0x5F, 0x00, 0x41, 0x00, +0x4C, 0x00, 0x4C, 0x00, 0x4F, 0x00, 0x43, 0x00, 0x41, 0x00, 0x54, 0x00, 0x49, 0x00, 0x4F, 0x00, +0x4E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0xA0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0x24, 0x00, 0x42, 0x00, 0x49, 0x00, 0x54, 0x00, 0x4D, 0x00, 0x41, 0x00, 0x50, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0xB0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +0x24, 0x00, 0x52, 0x00, 0x45, 0x00, 0x50, 0x00, 0x41, 0x00, 0x52, 0x00, 0x53, 0x00, 0x45, 0x00, +0x5F, 0x00, 0x50, 0x00, 0x4F, 0x00, 0x49, 0x00, 0x4E, 0x00, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x24, 0x00, 0x45, 0x00, 0x41, 0x00, 0x5F, 0x00, 0x49, 0x00, 0x4E, 0x00, 0x46, 0x00, 0x4F, 0x00, +0x52, 0x00, 0x4D, 0x00, 0x41, 0x00, 0x54, 0x00, 0x49, 0x00, 0x4F, 0x00, 0x4E, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0xD0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, +0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x24, 0x00, 0x45, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, +0x24, 0x00, 0x4C, 0x00, 0x4F, 0x00, 0x47, 0x00, 0x47, 0x00, 0x45, 0x00, 0x44, 0x00, 0x5F, 0x00, +0x55, 0x00, 0x54, 0x00, 0x49, 0x00, 0x4C, 0x00, 0x49, 0x00, 0x54, 0x00, 0x59, 0x00, 0x5F, 0x00, +0x53, 0x00, 0x54, 0x00, 0x52, 0x00, 0x45, 0x00, 0x41, 0x00, 0x4D, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 +}; + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/attrdef.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/attrdef.h new file mode 100644 index 0000000..0c66439 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/attrdef.h @@ -0,0 +1,7 @@ +#ifndef _NTFS_ATTRDEF_H_ +#define _NTFS_ATTRDEF_H_ + +extern const unsigned char attrdef_ntfs3x_array[2560]; + +#endif /* _NTFS_ATTRDEF_H_ */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/boot.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/boot.c new file mode 100644 index 0000000..fe8d6ac --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/boot.c @@ -0,0 +1,103 @@ +/* + * NTFS bootsector, adapted from the vfat one. + */ + +/* mkfs.fat.c - utility to create FAT/MS-DOS filesystems + * Copyright (C) 1991 Linus Torvalds + * Copyright (C) 1992-1993 Remy Card + * Copyright (C) 1993-1994 David Hudson + * Copyright (C) 1998 H. Peter Anvin + * Copyright (C) 1998-2005 Roman Hodek + * Copyright (C) 2008-2014 Daniel Baumann + * Copyright (C) 2015 Andreas Bombe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * The complete text of the GNU General Public License + * can be found in /usr/share/common-licenses/GPL-3 file. + */ + +#include "boot.h" + +#define BOOTCODE_SIZE 4136 + +/* The "boot code" we put into the filesystem... it writes a message and + * tells the user to try again */ + +#define MSG_OFFSET_OFFSET 3 + +const unsigned char boot_array[BOOTCODE_SIZE] = + + "\xeb\x52\x90" /* jump to code at 0x54 (0x7c54) */ + "NTFS \0" /* NTFS signature */ + + "\0\0\0\0\0\0\0\0\0\0\0\0" /* 72 bytes for device parameters */ + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + /* Boot code run at location 0x7c54 */ + "\x0e" /* push cs */ + "\x1f" /* pop ds */ + "\xbe\x71\x7c" /* mov si, offset message_txt (at location 0x7c71) */ + /* write_msg: */ + "\xac" /* lodsb */ + "\x22\xc0" /* and al, al */ + "\x74\x0b" /* jz key_press */ + "\x56" /* push si */ + "\xb4\x0e" /* mov ah, 0eh */ + "\xbb\x07\x00" /* mov bx, 0007h */ + "\xcd\x10" /* int 10h */ + "\x5e" /* pop si */ + "\xeb\xf0" /* jmp write_msg */ + /* key_press: */ + "\x32\xe4" /* xor ah, ah */ + "\xcd\x16" /* int 16h */ + "\xcd\x19" /* int 19h */ + "\xeb\xfe" /* foo: jmp foo */ + /* message_txt: */ + "This is not a bootable disk. Please insert a bootable floppy and\r\n" + "press any key to try again ... \r\n" + /* At location 0xd4, 298 bytes to reach 0x1fe */ + /* 298 = 4 blocks of 72 then 10 */ + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + "\0\0\0\0\0\0\0\0\0\0\0\0" + + "\0\0\0\0\0\0\0\0\0\0" + /* Boot signature at 0x1fe */ + "\x55\xaa"; diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/boot.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/boot.h new file mode 100644 index 0000000..45d7992 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/boot.h @@ -0,0 +1,7 @@ +#ifndef _NTFS_BOOT_H_ +#define _NTFS_BOOT_H_ + +extern const unsigned char boot_array[4136]; + +#endif /* _NTFS_BOOT_H_ */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/cluster.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/cluster.c new file mode 100644 index 0000000..82188f4 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/cluster.c @@ -0,0 +1,132 @@ +/** + * cluster - Part of the Linux-NTFS project. + * + * Copyright (c) 2002-2003 Richard Russon + * Copyright (c) 2014 Jean-Pierre Andre + * + * This function will locate the owner of any given sector or cluster range. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif + +#include "cluster.h" +#include "utils.h" +#include "logging.h" + +/** + * cluster_find + */ +int cluster_find(ntfs_volume *vol, LCN c_begin, LCN c_end, cluster_cb *cb, void *data) +{ + int j; + int result = -1; + struct mft_search_ctx *m_ctx = NULL; + ntfs_attr_search_ctx *a_ctx = NULL; + s64 count; + BOOL found; + ATTR_RECORD *rec; + runlist *runs; + + if (!vol || !cb) + return -1; + + m_ctx = mft_get_search_ctx(vol); + m_ctx->flags_search = FEMR_IN_USE | FEMR_BASE_RECORD; + count = 0; + + while (mft_next_record(m_ctx) == 0) { + + if (!(m_ctx->flags_match & FEMR_BASE_RECORD)) + continue; + + ntfs_log_verbose("Inode: %llu\n", (unsigned long long) + m_ctx->inode->mft_no); + + a_ctx = ntfs_attr_get_search_ctx(m_ctx->inode, NULL); + + found = FALSE; + while ((rec = find_attribute(AT_UNUSED, a_ctx))) { + + if (!rec->non_resident) { + ntfs_log_verbose("0x%02x skipped - attr is resident\n", + (int)le32_to_cpu(a_ctx->attr->type)); + continue; + } + + runs = ntfs_mapping_pairs_decompress(vol, a_ctx->attr, NULL); + if (!runs) { + ntfs_log_error("Couldn't read the data runs.\n"); + goto done; + } + + ntfs_log_verbose("\t[0x%02X]\n", + (int)le32_to_cpu(a_ctx->attr->type)); + + ntfs_log_verbose("\t\tVCN\tLCN\tLength\n"); + for (j = 0; runs[j].length > 0; j++) { + LCN a_begin = runs[j].lcn; + LCN a_end = a_begin + runs[j].length - 1; + + if (a_begin < 0) + continue; // sparse, discontiguous, etc + + ntfs_log_verbose("\t\t%lld\t%lld-%lld (%lld)\n", + (long long)runs[j].vcn, + (long long)runs[j].lcn, + (long long)(runs[j].lcn + + runs[j].length - 1), + (long long)runs[j].length); + //dprint list + + if ((a_begin > c_end) || (a_end < c_begin)) + continue; // before or after search range + + if ((*cb) (m_ctx->inode, a_ctx->attr, runs+j, data)) + return 1; + found = TRUE; + } + } + + ntfs_attr_put_search_ctx(a_ctx); + a_ctx = NULL; + if (found) + count++; + } + + if (count > 1) + ntfs_log_info("* %lld inodes found\n",(long long)count); + else + ntfs_log_info("* %s inode found\n", (count ? "one" : "no")); + result = 0; +done: + ntfs_attr_put_search_ctx(a_ctx); + mft_put_search_ctx(m_ctx); + + return result; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/cluster.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/cluster.h new file mode 100644 index 0000000..4bc1827 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/cluster.h @@ -0,0 +1,39 @@ +/* + * cluster - Part of the Linux-NTFS project. + * + * Copyright (c) 2003 Richard Russon + * + * This function will locate the owner of any given sector or cluster range. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _CLUSTER_H_ +#define _CLUSTER_H_ + +#include "types.h" +#include "volume.h" + +typedef struct { + int x; +} ntfs_cluster; + +typedef int (cluster_cb)(ntfs_inode *ino, ATTR_RECORD *attr, runlist_element *run, void *data); + +int cluster_find(ntfs_volume *vol, LCN c_begin, LCN c_end, cluster_cb *cb, void *data); + +#endif /* _CLUSTER_H_ */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/list.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/list.h new file mode 100644 index 0000000..4db45d4 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/list.h @@ -0,0 +1,194 @@ +/* + * list.h - Linked list implementation. Part of the Linux-NTFS project. + * + * Copyright (c) 2000-2002 Anton Altaparmakov and others + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_LIST_H +#define _NTFS_LIST_H + +/** + * struct ntfs_list_head - Simple doubly linked list implementation. + * + * Copied from Linux kernel 2.4.2-ac18 into Linux-NTFS (with minor + * modifications). - AIA + * + * Some of the internal functions ("__xxx") are useful when + * manipulating whole lists rather than single entries, as + * sometimes we already know the next/prev entries and we can + * generate better code by using them directly rather than + * using the generic single-entry routines. + */ +struct ntfs_list_head { + struct ntfs_list_head *next, *prev; +}; + +#define NTFS_LIST_HEAD_INIT(name) { &(name), &(name) } + +#define NTFS_LIST_HEAD(name) \ + struct ntfs_list_head name = NTFS_LIST_HEAD_INIT(name) + +#define NTFS_INIT_LIST_HEAD(ptr) do { \ + (ptr)->next = (ptr); (ptr)->prev = (ptr); \ +} while (0) + +/** + * __ntfs_list_add - Insert a new entry between two known consecutive entries. + * @new: + * @prev: + * @next: + * + * This is only for internal list manipulation where we know the prev/next + * entries already! + */ +static __inline__ void __ntfs_list_add(struct ntfs_list_head * new, + struct ntfs_list_head * prev, struct ntfs_list_head * next) +{ + next->prev = new; + new->next = next; + new->prev = prev; + prev->next = new; +} + +/** + * ntfs_list_add - add a new entry + * @new: new entry to be added + * @head: list head to add it after + * + * Insert a new entry after the specified head. + * This is good for implementing stacks. + */ +static __inline__ void ntfs_list_add(struct ntfs_list_head *new, + struct ntfs_list_head *head) +{ + __ntfs_list_add(new, head, head->next); +} + +/** + * ntfs_list_add_tail - add a new entry + * @new: new entry to be added + * @head: list head to add it before + * + * Insert a new entry before the specified head. + * This is useful for implementing queues. + */ +static __inline__ void ntfs_list_add_tail(struct ntfs_list_head *new, + struct ntfs_list_head *head) +{ + __ntfs_list_add(new, head->prev, head); +} + +/** + * __ntfs_list_del - + * @prev: + * @next: + * + * Delete a list entry by making the prev/next entries point to each other. + * + * This is only for internal list manipulation where we know the prev/next + * entries already! + */ +static __inline__ void __ntfs_list_del(struct ntfs_list_head * prev, + struct ntfs_list_head * next) +{ + next->prev = prev; + prev->next = next; +} + +/** + * ntfs_list_del - deletes entry from list. + * @entry: the element to delete from the list. + * + * Note: ntfs_list_empty on entry does not return true after this, the entry is + * in an undefined state. + */ +static __inline__ void ntfs_list_del(struct ntfs_list_head *entry) +{ + __ntfs_list_del(entry->prev, entry->next); +} + +/** + * ntfs_list_del_init - deletes entry from list and reinitialize it. + * @entry: the element to delete from the list. + */ +static __inline__ void ntfs_list_del_init(struct ntfs_list_head *entry) +{ + __ntfs_list_del(entry->prev, entry->next); + NTFS_INIT_LIST_HEAD(entry); +} + +/** + * ntfs_list_empty - tests whether a list is empty + * @head: the list to test. + */ +static __inline__ int ntfs_list_empty(struct ntfs_list_head *head) +{ + return head->next == head; +} + +/** + * ntfs_list_splice - join two lists + * @list: the new list to add. + * @head: the place to add it in the first list. + */ +static __inline__ void ntfs_list_splice(struct ntfs_list_head *list, + struct ntfs_list_head *head) +{ + struct ntfs_list_head *first = list->next; + + if (first != list) { + struct ntfs_list_head *last = list->prev; + struct ntfs_list_head *at = head->next; + + first->prev = head; + head->next = first; + + last->next = at; + at->prev = last; + } +} + +/** + * ntfs_list_entry - get the struct for this entry + * @ptr: the &struct ntfs_list_head pointer. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_struct within the struct. + */ +#define ntfs_list_entry(ptr, type, member) \ + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) + +/** + * ntfs_list_for_each - iterate over a list + * @pos: the &struct ntfs_list_head to use as a loop counter. + * @head: the head for your list. + */ +#define ntfs_list_for_each(pos, head) \ + for (pos = (head)->next; pos != (head); pos = pos->next) + +/** + * ntfs_list_for_each_safe - iterate over a list safe against removal of list entry + * @pos: the &struct ntfs_list_head to use as a loop counter. + * @n: another &struct ntfs_list_head to use as temporary storage + * @head: the head for your list. + */ +#define ntfs_list_for_each_safe(pos, n, head) \ + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) + +#endif /* defined _NTFS_LIST_H */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/mkntfs.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/mkntfs.8.in new file mode 100644 index 0000000..b4f61b6 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/mkntfs.8.in @@ -0,0 +1,292 @@ +.\" Copyright (c) 2001\-2006 Anton Altaparmakov. +.\" Copyright (c) 2005 Richard Russon. +.\" Copyright (c) 2005\-2006 Szabolcs Szakacsits. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH MKNTFS 8 "January 2006" "ntfs-3g @VERSION@" +.SH NAME +mkntfs \- create an NTFS file system +.SH SYNOPSIS +.B mkntfs +[\fIoptions\fR] \fIdevice \fR[\fInumber\-of\-sectors\fR] +.PP +.B mkntfs +[ +.B \-C +] +[ +.B \-c +.I cluster\-size +] +[ +.B \-F +] +[ +.B \-f +] +[ +.B \-H +.I heads +] +[ +.B \-h +] +[ +.B \-I +] +[ +.B \-L +.I volume\-label +] +[ +.B \-l +] +[ +.B \-n +] +[ +.B \-p +.I part\-start\-sect +] +[ +.B \-Q +] +[ +.B \-q +] +[ +.B \-S +.I sectors\-per\-track +] +[ +.B \-s +.I sector\-size +] +[ +.B \-T +] +[ +.B \-U +] +[ +.B \-V +] +[ +.B \-v +] +[ +.B \-z +.I mft\-zone\-multiplier +] +[ +.B \-\-debug +] +.I device +[ +.I number\-of\-sectors +] +.SH DESCRIPTION +.B mkntfs +is used to create an NTFS file system on a device (usually a disk partition) +or file. +.I device +is the special file corresponding to the device (e.g +.IR /dev/hdXX ). +.I number\-of\-sectors +is the number of sectors on the device. If omitted, +.B mkntfs +automagically figures the file system size. +.SH OPTIONS +Below is a summary of all the options that +.B mkntfs +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.SS Basic options +.TP +\fB\-f\fR, \fB\-\-fast\fR, \fB\-Q\fR, \fB\-\-quick\fR +Perform quick (fast) format. This will skip both zeroing of the volume and bad +sector checking. +.TP +\fB\-L\fR, \fB\-\-label\fR STRING +Set the volume label for the filesystem. +.TP +\fB\-C\fR, \fB\-\-enable\-compression\fR +Enable compression on the volume. +.TP +\fB\-n\fR, \fB\-\-no\-action\fR +Causes +.B mkntfs +to not actually create a filesystem, but display what it would do if it were +to create a filesystem. All steps of the format are carried out except the +actual writing to the device. +.SS Advanced options +.TP +\fB\-c\fR, \fB\-\-cluster\-size\fR BYTES +Specify the size of clusters in bytes. Valid cluster size values are powers of +two, with at least 256, and at most 2097152 bytes (2MB) per cluster. If omitted, +.B mkntfs +uses 4096 bytes as the default cluster size. +.sp +Note that the default cluster size is set to be at least equal to the sector +size as a cluster cannot be smaller than a sector. Also, note that values +greater than 4096 have the side effect that compression is disabled on the +volume (due to limitations in the NTFS compression algorithm currently in use +by Windows). +.TP +\fB\-s\fR, \fB\-\-sector\-size\fR BYTES +Specify the size of sectors in bytes. Valid sector size values are 256, 512, +1024, 2048 and 4096 bytes per sector. If omitted, +.B mkntfs +attempts to determine the +.I sector\-size +automatically and if that fails a default of 512 bytes per sector is used. +.TP +\fB\-p\fR, \fB\-\-partition\-start\fR SECTOR +Specify the partition start sector. The maximum is 4294967295 (2^32\-1). If +omitted, +.B mkntfs +attempts to determine +.I part\-start\-sect +automatically and if that fails or the value is oversized, a +default of 0 is used. The partition is usable despite a wrong value, +however note that a correct +.I part\-start\-sect +is required for Windows to be able to boot from the created volume. +.TP +\fB\-H\fR, \fB\-\-heads\fR NUM +Specify the number of heads. The maximum is 65535 (0xffff). If omitted, +.B mkntfs +attempts to determine the number of +.I heads +automatically and if that fails a default of 0 is used. Note that +.I heads +is required for Windows to be able to boot from the created volume. +.TP +\fB\-S\fR, \fB\-\-sectors\-per\-track\fR NUM +Specify the number of sectors per track. The maximum is 65535 (0xffff). If +omitted, +.B mkntfs +attempts to determine the number of +.I sectors\-per\-track +automatically and if that fails a default of 0 is used. Note that +.I sectors\-per\-track +is required for Windows to be able to boot from the created volume. +.TP +\fB\-z\fR, \fB\-\-mft\-zone\-multiplier\fR NUM +Set the MFT zone multiplier, which determines the size of the MFT zone to use +on the volume. The MFT zone is the area at the beginning of the volume reserved +for the master file table (MFT), which stores the on disk inodes (MFT records). +It is noteworthy that small files are stored entirely within the inode; +thus, if you expect to use the volume for storing large numbers of very small +files, it is useful to set the zone multiplier to a higher value. Note, that +the MFT zone is resized on the fly as required during operation of the NTFS +driver but choosing a good value will reduce fragmentation. Valid values +are 1, 2, 3 and 4. The values have the following meaning: +.TS +box; +lB lB +lB lB +c l. +MFT zone MFT zone size +multiplier (% of volume size) +1 12.5% (default) +2 25.0% +3 37.5% +4 50.0% +.TE +.sp +.TP +\fB\-T\fR, \fB\-\-zero\-time\fR +Fake the time to be 00:00:00 UTC, Jan 1, 1970 instead of the current system +time. This is only really useful for debugging purposes. +.TP +\fB\-U\fR, \fB\-\-with\-uuid\fR +Generate a random volume UUID. +.TP +\fB\-I\fR, \fB\-\-no\-indexing\fR +Disable content indexing on the volume. (This is only meaningful on +Windows 2000 and later. Windows NT 4.0 and earlier ignore this as they do +not implement content indexing at all.) +.TP +\fB\-F\fR, \fB\-\-force\fR +Force +.B mkntfs +to run, even if the specified +.I device +is not a block special device, or appears to be mounted. +.SS Output options +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Quiet execution; only errors are written to stderr, no output to stdout +occurs at all. Useful if +.B mkntfs +is run in a script. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Verbose execution. +.TP +\fB\-\-debug\fR +Really verbose execution; includes the verbose output from the +.B \-v +option as well as additional output useful for debugging +.B mkntfs. +.SS Help options +.TP +\fB\-V\fR, \fB\-\-version\fR +Print the version number of +.B mkntfs +and exit. +.TP +\fB\-l\fR, \fB\-\-license\fR +Print the licensing information of +.B mkntfs +and exit. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.SH KNOWN ISSUES +When applying chkdsk to a file system, it sometimes throws a warning +"Correcting errors in the uppercase file." The uppercase file is created +while formatting and it defines the mapping of lower case characters to +upper case ones, as needed to sort file names in directories. The warning +means that the uppercase file defined on the file system is not the same as +the one used by the Windows OS on which chkdsk is running, and this may +happen because newer versions of Windows take into account new characters +defined by the Unicode consortium. +.P +Currently, mkntfs creates the uppercase table so that no warning is thrown +by Windows Vista, Windows 7 or Windows 8. A warning may be thrown by +other Windows versions, or if chkdsk is applied in succession on different +Windows versions. +.SH BUGS +If you find a bug please send an email describing the problem to the +development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B mkntfs +was written by Anton Altaparmakov, Richard Russon, Erik Sornes and Szabolcs Szakacsits. +It was ported to ntfs-3g by Erik Larsson and Jean-Pierre Andre. +.SH AVAILABILITY +.B mkntfs +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR badblocks (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/mkntfs.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/mkntfs.c new file mode 100644 index 0000000..eb4363c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/mkntfs.c @@ -0,0 +1,5205 @@ +/** + * mkntfs - Part of the Linux-NTFS project. + * + * Copyright (c) 2000-2011 Anton Altaparmakov + * Copyright (c) 2001-2005 Richard Russon + * Copyright (c) 2002-2006 Szabolcs Szakacsits + * Copyright (c) 2005 Erik Sornes + * Copyright (c) 2007 Yura Pakhuchiy + * Copyright (c) 2010-2018 Jean-Pierre Andre + * + * This utility will create an NTFS 1.2 or 3.1 volume on a user + * specified (block) device. + * + * Some things (option handling and determination of mount status) have been + * adapted from e2fsprogs-1.19 and lib/ext2fs/ismounted.c and misc/mke2fs.c in + * particular. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS source + * in the file COPYING); if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif +#ifdef HAVE_LIBGEN_H +#include +#endif +#ifdef ENABLE_UUID +#include +#endif + + +#ifdef HAVE_GETOPT_H +#include +#else + extern char *optarg; + extern int optind; +#endif + +#ifdef HAVE_LINUX_MAJOR_H +# include +# ifndef MAJOR +# define MAJOR(dev) ((dev) >> 8) +# define MINOR(dev) ((dev) & 0xff) +# endif +# ifndef IDE_DISK_MAJOR +# ifndef IDE0_MAJOR +# define IDE0_MAJOR 3 +# define IDE1_MAJOR 22 +# define IDE2_MAJOR 33 +# define IDE3_MAJOR 34 +# define IDE4_MAJOR 56 +# define IDE5_MAJOR 57 +# define IDE6_MAJOR 88 +# define IDE7_MAJOR 89 +# define IDE8_MAJOR 90 +# define IDE9_MAJOR 91 +# endif +# define IDE_DISK_MAJOR(M) \ + ((M) == IDE0_MAJOR || (M) == IDE1_MAJOR || \ + (M) == IDE2_MAJOR || (M) == IDE3_MAJOR || \ + (M) == IDE4_MAJOR || (M) == IDE5_MAJOR || \ + (M) == IDE6_MAJOR || (M) == IDE7_MAJOR || \ + (M) == IDE8_MAJOR || (M) == IDE9_MAJOR) +# endif +# ifndef SCSI_DISK_MAJOR +# ifndef SCSI_DISK0_MAJOR +# define SCSI_DISK0_MAJOR 8 +# define SCSI_DISK1_MAJOR 65 +# define SCSI_DISK7_MAJOR 71 +# endif +# define SCSI_DISK_MAJOR(M) \ + ((M) == SCSI_DISK0_MAJOR || \ + ((M) >= SCSI_DISK1_MAJOR && \ + (M) <= SCSI_DISK7_MAJOR)) +# endif +#endif + +#include "param.h" +#include "security.h" +#include "types.h" +#include "attrib.h" +#include "bitmap.h" +#include "bootsect.h" +#include "device.h" +#include "dir.h" +#include "mft.h" +#include "mst.h" +#include "runlist.h" +#include "utils.h" +#include "ntfstime.h" +#include "sd.h" +#include "boot.h" +#include "attrdef.h" +/* #include "version.h" */ +#include "logging.h" +#include "support.h" +#include "unistr.h" +#include "misc.h" + +#if defined(__sun) && defined (__SVR4) +#undef basename +#define basename(name) name +#endif + +typedef enum { WRITE_STANDARD, WRITE_BITMAP, WRITE_LOGFILE } WRITE_TYPE; + +#ifdef NO_NTFS_DEVICE_DEFAULT_IO_OPS +#error "No default device io operations! Cannot build mkntfs. \ +You need to run ./configure without the --disable-default-device-io-ops \ +switch if you want to be able to build the NTFS utilities." +#endif + +/* Page size on ia32. Can change to 8192 on Alpha. */ +#define NTFS_PAGE_SIZE 4096 + +static char EXEC_NAME[] = "mkntfs"; + +struct BITMAP_ALLOCATION { + struct BITMAP_ALLOCATION *next; + LCN lcn; /* first allocated cluster */ + s64 length; /* count of consecutive clusters */ +} ; + + /* Upcase $Info, used since Windows 8 */ +struct UPCASEINFO { + le32 len; + le32 filler; + le64 crc; + le32 osmajor; + le32 osminor; + le32 build; + le16 packmajor; + le16 packminor; +} ; + +/** + * global variables + */ +static u8 *g_buf = NULL; +static int g_mft_bitmap_byte_size = 0; +static u8 *g_mft_bitmap = NULL; +static int g_lcn_bitmap_byte_size = 0; +static int g_dynamic_buf_size = 0; +static u8 *g_dynamic_buf = NULL; +static struct UPCASEINFO *g_upcaseinfo = NULL; +static runlist *g_rl_mft = NULL; +static runlist *g_rl_mft_bmp = NULL; +static runlist *g_rl_mftmirr = NULL; +static runlist *g_rl_logfile = NULL; +static runlist *g_rl_boot = NULL; +static runlist *g_rl_bad = NULL; +static INDEX_ALLOCATION *g_index_block = NULL; +static ntfs_volume *g_vol = NULL; +static int g_mft_size = 0; +static long long g_mft_lcn = 0; /* lcn of $MFT, $DATA attribute */ +static long long g_mftmirr_lcn = 0; /* lcn of $MFTMirr, $DATA */ +static long long g_logfile_lcn = 0; /* lcn of $LogFile, $DATA */ +static int g_logfile_size = 0; /* in bytes, determined from volume_size */ +static long long g_mft_zone_end = 0; /* Determined from volume_size and mft_zone_multiplier, in clusters */ +static long long g_num_bad_blocks = 0; /* Number of bad clusters */ +static long long *g_bad_blocks = NULL; /* Array of bad clusters */ + +static struct BITMAP_ALLOCATION *g_allocation = NULL; /* Head of cluster allocations */ + +/** + * struct mkntfs_options + */ +static struct mkntfs_options { + char *dev_name; /* Name of the device, or file, to use */ + BOOL enable_compression; /* -C, enables compression of all files on the volume by default. */ + BOOL quick_format; /* -f or -Q, fast format, don't zero the volume first. */ + BOOL force; /* -F, force fs creation. */ + long heads; /* -H, number of heads on device */ + BOOL disable_indexing; /* -I, disables indexing of file contents on the volume by default. */ + BOOL no_action; /* -n, do not write to device, only display what would be done. */ + long long part_start_sect; /* -p, start sector of partition on parent device */ + long sector_size; /* -s, in bytes, power of 2, default is 512 bytes. */ + long sectors_per_track; /* -S, number of sectors per track on device */ + BOOL use_epoch_time; /* -T, fake the time to be 00:00:00 UTC, Jan 1, 1970. */ + long mft_zone_multiplier; /* -z, value from 1 to 4. Default is 1. */ + long long num_sectors; /* size of device in sectors */ + long cluster_size; /* -c, format with this cluster-size */ + BOOL with_uuid; /* -U, request setting an uuid */ + char *label; /* -L, volume label */ +} opts; + + +/** + * mkntfs_license + */ +static void mkntfs_license(void) +{ + ntfs_log_info("%s", ntfs_gpl); +} + +/** + * mkntfs_usage + */ +static void mkntfs_usage(void) +{ + ntfs_log_info("\nUsage: %s [options] device [number-of-sectors]\n" +"\n" +"Basic options:\n" +" -f, --fast Perform a quick format\n" +" -Q, --quick Perform a quick format\n" +" -L, --label STRING Set the volume label\n" +" -C, --enable-compression Enable compression on the volume\n" +" -I, --no-indexing Disable indexing on the volume\n" +" -n, --no-action Do not write to disk\n" +"\n" +"Advanced options:\n" +" -c, --cluster-size BYTES Specify the cluster size for the volume\n" +" -s, --sector-size BYTES Specify the sector size for the device\n" +" -p, --partition-start SECTOR Specify the partition start sector\n" +" -H, --heads NUM Specify the number of heads\n" +" -S, --sectors-per-track NUM Specify the number of sectors per track\n" +" -z, --mft-zone-multiplier NUM Set the MFT zone multiplier\n" +" -T, --zero-time Fake the time to be 00:00 UTC, Jan 1, 1970\n" +" -F, --force Force execution despite errors\n" +"\n" +"Output options:\n" +" -q, --quiet Quiet execution\n" +" -v, --verbose Verbose execution\n" +" --debug Very verbose execution\n" +"\n" +"Help options:\n" +" -V, --version Display version\n" +" -l, --license Display licensing information\n" +" -h, --help Display this help\n" +"\n", basename(EXEC_NAME)); + ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * mkntfs_version + */ +static void mkntfs_version(void) +{ + ntfs_log_info("\n%s v%s (libntfs-3g)\n\n", EXEC_NAME, VERSION); + ntfs_log_info("Create an NTFS volume on a user specified (block) " + "device.\n\n"); + ntfs_log_info("Copyright (c) 2000-2007 Anton Altaparmakov\n"); + ntfs_log_info("Copyright (c) 2001-2005 Richard Russon\n"); + ntfs_log_info("Copyright (c) 2002-2006 Szabolcs Szakacsits\n"); + ntfs_log_info("Copyright (c) 2005 Erik Sornes\n"); + ntfs_log_info("Copyright (c) 2007 Yura Pakhuchiy\n"); + ntfs_log_info("Copyright (c) 2010-2018 Jean-Pierre Andre\n"); + ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/* + * crc64, adapted from http://rpm5.org/docs/api/digest_8c-source.html + * ECMA-182 polynomial, see + * http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-182.pdf + */ + /* make sure the needed types are defined */ +#undef byte +#undef uint32_t +#undef uint64_t +#define byte u8 +#define uint32_t u32 +#define uint64_t u64 +static uint64_t crc64(uint64_t crc, const byte * data, size_t size) + /*@*/ +{ + static uint64_t polynomial = 0x9a6c9329ac4bc9b5ULL; + static uint64_t xorout = 0xffffffffffffffffULL; + static uint64_t table[256]; + + crc ^= xorout; + + if (data == NULL) { + /* generate the table of CRC remainders for all possible bytes */ + uint64_t c; + uint32_t i, j; + for (i = 0; i < 256; i++) { + c = i; + for (j = 0; j < 8; j++) { + if (c & 1) + c = polynomial ^ (c >> 1); + else + c = (c >> 1); + } + table[i] = c; + } + } else + while (size) { + crc = table[(crc ^ *data) & 0xff] ^ (crc >> 8); + size--; + data++; + } + + crc ^= xorout; + + return crc; +} + +/* + * Mark a run of clusters as allocated + * + * Returns FALSE if unsuccessful + */ + +static BOOL bitmap_allocate(LCN lcn, s64 length) +{ + BOOL done; + struct BITMAP_ALLOCATION *p; + struct BITMAP_ALLOCATION *q; + struct BITMAP_ALLOCATION *newall; + + done = TRUE; + if (length) { + p = g_allocation; + q = (struct BITMAP_ALLOCATION*)NULL; + /* locate the first run which starts beyond the requested lcn */ + while (p && (p->lcn <= lcn)) { + q = p; + p = p->next; + } + /* make sure the requested lcns were not allocated */ + if ((q && ((q->lcn + q->length) > lcn)) + || (p && ((lcn + length) > p->lcn))) { + ntfs_log_error("Bitmap allocation error\n"); + done = FALSE; + } + if (q && ((q->lcn + q->length) == lcn)) { + /* extend current run, no overlapping possible */ + q->length += length; + } else { + newall = (struct BITMAP_ALLOCATION*) + ntfs_malloc(sizeof(struct BITMAP_ALLOCATION)); + if (newall) { + newall->lcn = lcn; + newall->length = length; + newall->next = p; + if (q) q->next = newall; + else g_allocation = newall; + } else { + done = FALSE; + ntfs_log_perror("Not enough memory"); + } + } + } + return (done); +} + +/* + * Mark a run of cluster as not allocated + * + * Returns FALSE if unsuccessful + * (freeing free clusters is not considered as an error) + */ + +static BOOL bitmap_deallocate(LCN lcn, s64 length) +{ + BOOL done; + struct BITMAP_ALLOCATION *p; + struct BITMAP_ALLOCATION *q; + LCN first, last; + s64 begin_length, end_length; + + done = TRUE; + if (length) { + p = g_allocation; + q = (struct BITMAP_ALLOCATION*)NULL; + /* locate a run which has a common portion */ + while (p) { + first = (p->lcn > lcn ? p->lcn : lcn); + last = ((p->lcn + p->length) < (lcn + length) + ? p->lcn + p->length : lcn + length); + if (first < last) { + /* get the parts which must be kept */ + begin_length = first - p->lcn; + end_length = p->lcn + p->length - last; + /* delete the entry */ + if (q) + q->next = p->next; + else + g_allocation = p->next; + free(p); + /* reallocate the beginning and the end */ + if (begin_length + && !bitmap_allocate(first - begin_length, + begin_length)) + done = FALSE; + if (end_length + && !bitmap_allocate(last, end_length)) + done = FALSE; + /* restart a full search */ + p = g_allocation; + q = (struct BITMAP_ALLOCATION*)NULL; + } else { + q = p; + p = p->next; + } + } + } + return (done); +} + +/* + * Get the allocation status of a single cluster + * and mark as allocated + * + * Returns 1 if the cluster was previously allocated + */ + +static int bitmap_get_and_set(LCN lcn, unsigned long length) +{ + struct BITMAP_ALLOCATION *p; + struct BITMAP_ALLOCATION *q; + int bit; + + if (length == 1) { + p = g_allocation; + q = (struct BITMAP_ALLOCATION*)NULL; + /* locate the first run which starts beyond the requested lcn */ + while (p && (p->lcn <= lcn)) { + q = p; + p = p->next; + } + if (q && (q->lcn <= lcn) && ((q->lcn + q->length) > lcn)) + bit = 1; /* was allocated */ + else { + bitmap_allocate(lcn, length); + bit = 0; + } + } else { + ntfs_log_error("Can only allocate a single cluster at a time\n"); + bit = 0; + } + return (bit); +} + +/* + * Build a section of the bitmap according to allocation + */ + +static void bitmap_build(u8 *buf, LCN lcn, s64 length) +{ + struct BITMAP_ALLOCATION *p; + LCN first, last; + int j; /* byte number */ + int bn; /* bit number */ + + for (j=0; (8*j)next) { + first = (p->lcn > lcn ? p->lcn : lcn); + last = ((p->lcn + p->length) < (lcn + length) + ? p->lcn + p->length : lcn + length); + if (first < last) { + bn = first - lcn; + /* initial partial byte, if any */ + while ((bn < (last - lcn)) && (bn & 7)) { + buf[bn >> 3] |= 1 << (bn & 7); + bn++; + } + /* full bytes */ + while (bn < (last - lcn - 7)) { + buf[bn >> 3] = 255; + bn += 8; + } + /* final partial byte, if any */ + while (bn < (last - lcn)) { + buf[bn >> 3] |= 1 << (bn & 7); + bn++; + } + } + } +} + +/** + * mkntfs_parse_long + */ +static BOOL mkntfs_parse_long(const char *string, const char *name, long *num) +{ + char *end = NULL; + long tmp; + + if (!string || !name || !num) + return FALSE; + + if (*num >= 0) { + ntfs_log_error("You may only specify the %s once.\n", name); + return FALSE; + } + + tmp = strtol(string, &end, 0); + if (end && *end) { + ntfs_log_error("Cannot understand the %s '%s'.\n", name, string); + return FALSE; + } else { + *num = tmp; + return TRUE; + } +} + +/** + * mkntfs_parse_llong + */ +static BOOL mkntfs_parse_llong(const char *string, const char *name, + long long *num) +{ + char *end = NULL; + long long tmp; + + if (!string || !name || !num) + return FALSE; + + if (*num >= 0) { + ntfs_log_error("You may only specify the %s once.\n", name); + return FALSE; + } + + tmp = strtoll(string, &end, 0); + if (end && *end) { + ntfs_log_error("Cannot understand the %s '%s'.\n", name, + string); + return FALSE; + } else { + *num = tmp; + return TRUE; + } +} + +/** + * mkntfs_init_options + */ +static void mkntfs_init_options(struct mkntfs_options *opts2) +{ + if (!opts2) + return; + + memset(opts2, 0, sizeof(*opts2)); + + /* Mark all the numeric options as "unset". */ + opts2->cluster_size = -1; + opts2->heads = -1; + opts2->mft_zone_multiplier = -1; + opts2->num_sectors = -1; + opts2->part_start_sect = -1; + opts2->sector_size = -1; + opts2->sectors_per_track = -1; +} + +/** + * mkntfs_parse_options + */ +static int mkntfs_parse_options(int argc, char *argv[], struct mkntfs_options *opts2) +{ + static const char *sopt = "-c:CfFhH:IlL:np:qQs:S:TUvVz:"; + static const struct option lopt[] = { + { "cluster-size", required_argument, NULL, 'c' }, + { "debug", no_argument, NULL, 'Z' }, + { "enable-compression", no_argument, NULL, 'C' }, + { "fast", no_argument, NULL, 'f' }, + { "force", no_argument, NULL, 'F' }, + { "heads", required_argument, NULL, 'H' }, + { "help", no_argument, NULL, 'h' }, + { "label", required_argument, NULL, 'L' }, + { "license", no_argument, NULL, 'l' }, + { "mft-zone-multiplier",required_argument, NULL, 'z' }, + { "no-action", no_argument, NULL, 'n' }, + { "no-indexing", no_argument, NULL, 'I' }, + { "partition-start", required_argument, NULL, 'p' }, + { "quick", no_argument, NULL, 'Q' }, + { "quiet", no_argument, NULL, 'q' }, + { "sector-size", required_argument, NULL, 's' }, + { "sectors-per-track", required_argument, NULL, 'S' }, + { "with-uuid", no_argument, NULL, 'U' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { "zero-time", no_argument, NULL, 'T' }, + { NULL, 0, NULL, 0 } + }; + + int c = -1; + int lic = 0; + int help = 0; + int err = 0; + int ver = 0; + + if (!argv || !opts2) { + ntfs_log_error("Internal error: invalid parameters to " + "mkntfs_options.\n"); + return FALSE; + } + + opterr = 0; /* We'll handle the errors, thank you. */ + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A device, or a number of sectors */ + if (!opts2->dev_name) + opts2->dev_name = argv[optind - 1]; + else if (!mkntfs_parse_llong(optarg, + "number of sectors", + &opts2->num_sectors)) + err++; + break; + case 'C': + opts2->enable_compression = TRUE; + break; + case 'c': + if (!mkntfs_parse_long(optarg, "cluster size", + &opts2->cluster_size)) + err++; + break; + case 'F': + opts2->force = TRUE; + break; + case 'f': /* fast */ + case 'Q': /* quick */ + opts2->quick_format = TRUE; + break; + case 'H': + if (!mkntfs_parse_long(optarg, "heads", &opts2->heads)) + err++; + break; + case 'h': + help++; /* display help */ + break; + case 'I': + opts2->disable_indexing = TRUE; + break; + case 'L': + if (!opts2->label) { + opts2->label = optarg; + } else { + ntfs_log_error("You may only specify the label " + "once.\n"); + err++; + } + break; + case 'l': + lic++; /* display the license */ + break; + case 'n': + opts2->no_action = TRUE; + break; + case 'p': + if (!mkntfs_parse_llong(optarg, "partition start", + &opts2->part_start_sect)) + err++; + break; + case 'q': + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET | + NTFS_LOG_LEVEL_VERBOSE | + NTFS_LOG_LEVEL_PROGRESS); + break; + case 's': + if (!mkntfs_parse_long(optarg, "sector size", + &opts2->sector_size)) + err++; + break; + case 'S': + if (!mkntfs_parse_long(optarg, "sectors per track", + &opts2->sectors_per_track)) + err++; + break; + case 'T': + opts2->use_epoch_time = TRUE; + break; + case 'U': + opts2->with_uuid = TRUE; + break; + case 'v': + ntfs_log_set_levels(NTFS_LOG_LEVEL_QUIET | + NTFS_LOG_LEVEL_VERBOSE | + NTFS_LOG_LEVEL_PROGRESS); + break; + case 'V': + ver++; /* display version info */ + break; + case 'Z': /* debug - turn on everything */ + ntfs_log_set_levels(NTFS_LOG_LEVEL_DEBUG | + NTFS_LOG_LEVEL_TRACE | + NTFS_LOG_LEVEL_VERBOSE | + NTFS_LOG_LEVEL_QUIET); + break; + case 'z': + if (!mkntfs_parse_long(optarg, "mft zone multiplier", + &opts2->mft_zone_multiplier)) + err++; + break; + default: + if (ntfs_log_parse_option (argv[optind-1])) + break; + if (((optopt == 'c') || (optopt == 'H') || + (optopt == 'L') || (optopt == 'p') || + (optopt == 's') || (optopt == 'S') || + (optopt == 'N') || (optopt == 'z')) && + (!optarg)) { + ntfs_log_error("Option '%s' requires an " + "argument.\n", argv[optind-1]); + } else if (optopt != '?') { + ntfs_log_error("Unknown option '%s'.\n", + argv[optind - 1]); + } + err++; + break; + } + } + + if (!err && !help && !ver && !lic) { + if (opts2->dev_name == NULL) { + if (argc > 1) + ntfs_log_error("You must specify a device.\n"); + err++; + } + } + + if (ver) + mkntfs_version(); + if (lic) + mkntfs_license(); + if (err || help) + mkntfs_usage(); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver || lic ? 0 : -1)); +} + + +/** + * mkntfs_time + */ +static ntfs_time mkntfs_time(void) +{ + struct timespec ts; + + ts.tv_sec = 0; + ts.tv_nsec = 0; + if (!opts.use_epoch_time) { +#ifdef HAVE_GETTIMEOFDAY + struct timeval tv = { 0, 0, }; + + if (!gettimeofday(&tv, NULL)) { + ts.tv_sec = tv.tv_sec; + ts.tv_nsec = tv.tv_usec * 1000L; + } + else +#endif + { + ts.tv_sec = time(NULL); + } + } + return timespec2ntfs(ts); +} + +/** + * append_to_bad_blocks + */ +static BOOL append_to_bad_blocks(unsigned long long block) +{ + long long *new_buf; + + if (!(g_num_bad_blocks & 15)) { + new_buf = realloc(g_bad_blocks, (g_num_bad_blocks + 16) * + sizeof(long long)); + if (!new_buf) { + ntfs_log_perror("Reallocating memory for bad blocks " + "list failed"); + return FALSE; + } + g_bad_blocks = new_buf; + } + g_bad_blocks[g_num_bad_blocks++] = block; + return TRUE; +} + +/** + * mkntfs_write + */ +static long long mkntfs_write(struct ntfs_device *dev, + const void *b, long long count) +{ + long long bytes_written, total; + int retry; + + if (opts.no_action) + return count; + total = 0LL; + retry = 0; + do { + bytes_written = dev->d_ops->write(dev, b, count); + if (bytes_written == -1LL) { + retry = errno; + ntfs_log_perror("Error writing to %s", dev->d_name); + errno = retry; + return bytes_written; + } else if (!bytes_written) { + retry++; + } else { + count -= bytes_written; + total += bytes_written; + } + } while (count && retry < 3); + if (count) + ntfs_log_error("Failed to complete writing to %s after three retries." + "\n", dev->d_name); + return total; +} + +/** + * Build and write a part of the global bitmap + * without overflowing from the allocated buffer + * + * mkntfs_bitmap_write + */ +static s64 mkntfs_bitmap_write(struct ntfs_device *dev, + s64 offset, s64 length) +{ + s64 partial_length; + s64 written; + + partial_length = length; + if (partial_length > g_dynamic_buf_size) + partial_length = g_dynamic_buf_size; + /* create a partial bitmap section, and write it */ + bitmap_build(g_dynamic_buf,offset << 3,partial_length << 3); + written = dev->d_ops->write(dev, g_dynamic_buf, partial_length); + return (written); +} + +/** + * Build and write a part of the log file + * without overflowing from the allocated buffer + * + * mkntfs_logfile_write + */ +static s64 mkntfs_logfile_write(struct ntfs_device *dev, + s64 offset __attribute__((unused)), s64 length) +{ + s64 partial_length; + s64 written; + + partial_length = length; + if (partial_length > g_dynamic_buf_size) + partial_length = g_dynamic_buf_size; + /* create a partial bad cluster section, and write it */ + memset(g_dynamic_buf, -1, partial_length); + written = dev->d_ops->write(dev, g_dynamic_buf, partial_length); + return (written); +} + +/** + * ntfs_rlwrite - Write to disk the clusters contained in the runlist @rl + * taking the data from @val. Take @val_len bytes from @val and pad the + * rest with zeroes. + * + * If the @rl specifies a completely sparse file, @val is allowed to be NULL. + * + * @inited_size if not NULL points to an output variable which will contain + * the actual number of bytes written to disk. I.e. this will not include + * sparse bytes for example. + * + * Return the number of bytes written (minus padding) or -1 on error. Errno + * will be set to the error code. + */ +static s64 ntfs_rlwrite(struct ntfs_device *dev, const runlist *rl, + const u8 *val, const s64 val_len, s64 *inited_size, + WRITE_TYPE write_type) +{ + s64 bytes_written, total, length, delta; + int retry, i; + + if (inited_size) + *inited_size = 0LL; + if (opts.no_action) + return val_len; + total = 0LL; + delta = 0LL; + for (i = 0; rl[i].length; i++) { + length = rl[i].length * g_vol->cluster_size; + /* Don't write sparse runs. */ + if (rl[i].lcn == -1) { + total += length; + if (!val) + continue; + /* TODO: Check that *val is really zero at pos and len. */ + continue; + } + /* + * Break up the write into the real data write and then a write + * of zeroes between the end of the real data and the end of + * the (last) run. + */ + if (total + length > val_len) { + delta = length; + length = val_len - total; + delta -= length; + } + if (dev->d_ops->seek(dev, rl[i].lcn * g_vol->cluster_size, + SEEK_SET) == (off_t)-1) + return -1LL; + retry = 0; + do { + /* use specific functions if buffer is not prefilled */ + switch (write_type) { + case WRITE_BITMAP : + bytes_written = mkntfs_bitmap_write(dev, + total, length); + break; + case WRITE_LOGFILE : + bytes_written = mkntfs_logfile_write(dev, + total, length); + break; + default : + bytes_written = dev->d_ops->write(dev, + val + total, length); + break; + } + if (bytes_written == -1LL) { + retry = errno; + ntfs_log_perror("Error writing to %s", + dev->d_name); + errno = retry; + return bytes_written; + } + if (bytes_written) { + length -= bytes_written; + total += bytes_written; + if (inited_size) + *inited_size += bytes_written; + } else { + retry++; + } + } while (length && retry < 3); + if (length) { + ntfs_log_error("Failed to complete writing to %s after three " + "retries.\n", dev->d_name); + return total; + } + } + if (delta) { + int eo; + char *b = ntfs_calloc(delta); + if (!b) + return -1; + bytes_written = mkntfs_write(dev, b, delta); + eo = errno; + free(b); + errno = eo; + if (bytes_written == -1LL) + return bytes_written; + } + return total; +} + +/** + * make_room_for_attribute - make room for an attribute inside an mft record + * @m: mft record + * @pos: position at which to make space + * @size: byte size to make available at this position + * + * @pos points to the attribute in front of which we want to make space. + * + * Return 0 on success or -errno on error. Possible error codes are: + * + * -ENOSPC There is not enough space available to complete + * operation. The caller has to make space before calling + * this. + * -EINVAL Can only occur if mkntfs was compiled with -DDEBUG. Means + * the input parameters were faulty. + */ +static int make_room_for_attribute(MFT_RECORD *m, char *pos, const u32 size) +{ + u32 biu; + + if (!size) + return 0; +#ifdef DEBUG + /* + * Rigorous consistency checks. Always return -EINVAL even if more + * appropriate codes exist for simplicity of parsing the return value. + */ + if (size != ((size + 7) & ~7)) { + ntfs_log_error("make_room_for_attribute() received non 8-byte aligned " + "size.\n"); + return -EINVAL; + } + if (!m || !pos) + return -EINVAL; + if (pos < (char*)m || pos + size < (char*)m || + pos > (char*)m + le32_to_cpu(m->bytes_allocated) || + pos + size > (char*)m + le32_to_cpu(m->bytes_allocated)) + return -EINVAL; + /* The -8 is for the attribute terminator. */ + if (pos - (char*)m > (int)le32_to_cpu(m->bytes_in_use) - 8) + return -EINVAL; +#endif + biu = le32_to_cpu(m->bytes_in_use); + /* Do we have enough space? */ + if (biu + size > le32_to_cpu(m->bytes_allocated)) + return -ENOSPC; + /* Move everything after pos to pos + size. */ + memmove(pos + size, pos, biu - (pos - (char*)m)); + /* Update mft record. */ + m->bytes_in_use = cpu_to_le32(biu + size); + return 0; +} + +/** + * deallocate_scattered_clusters + */ +static void deallocate_scattered_clusters(const runlist *rl) +{ + int i; + + if (!rl) + return; + /* Iterate over all runs in the runlist @rl. */ + for (i = 0; rl[i].length; i++) { + /* Skip sparse runs. */ + if (rl[i].lcn == -1LL) + continue; + /* Deallocate the current run. */ + bitmap_deallocate(rl[i].lcn, rl[i].length); + } +} + +/** + * allocate_scattered_clusters + * @clusters: Amount of clusters to allocate. + * + * Allocate @clusters and create a runlist of the allocated clusters. + * + * Return the allocated runlist. Caller has to free the runlist when finished + * with it. + * + * On error return NULL and errno is set to the error code. + * + * TODO: We should be returning the size as well, but for mkntfs this is not + * necessary. + */ +static runlist * allocate_scattered_clusters(s64 clusters) +{ + runlist *rl = NULL, *rlt; + VCN vcn = 0LL; + LCN lcn, end, prev_lcn = 0LL; + int rlpos = 0; + int rlsize = 0; + s64 prev_run_len = 0LL; + char bit; + + end = g_vol->nr_clusters; + /* Loop until all clusters are allocated. */ + while (clusters) { + /* Loop in current zone until we run out of free clusters. */ + for (lcn = g_mft_zone_end; lcn < end; lcn++) { + bit = bitmap_get_and_set(lcn,1); + if (bit) + continue; + /* + * Reallocate memory if necessary. Make sure we have + * enough for the terminator entry as well. + */ + if ((rlpos + 2) * (int)sizeof(runlist) >= rlsize) { + rlsize += 4096; /* PAGE_SIZE */ + rlt = realloc(rl, rlsize); + if (!rlt) + goto err_end; + rl = rlt; + } + /* Coalesce with previous run if adjacent LCNs. */ + if (prev_lcn == lcn - prev_run_len) { + rl[rlpos - 1].length = ++prev_run_len; + vcn++; + } else { + rl[rlpos].vcn = vcn++; + rl[rlpos].lcn = lcn; + prev_lcn = lcn; + rl[rlpos].length = 1LL; + prev_run_len = 1LL; + rlpos++; + } + /* Done? */ + if (!--clusters) { + /* Add terminator element and return. */ + rl[rlpos].vcn = vcn; + rl[rlpos].lcn = 0LL; + rl[rlpos].length = 0LL; + return rl; + } + + } + /* Switch to next zone, decreasing mft zone by factor 2. */ + end = g_mft_zone_end; + g_mft_zone_end >>= 1; + /* Have we run out of space on the volume? */ + if (g_mft_zone_end <= 0) + goto err_end; + } + return rl; +err_end: + if (rl) { + /* Add terminator element. */ + rl[rlpos].vcn = vcn; + rl[rlpos].lcn = -1LL; + rl[rlpos].length = 0LL; + /* Deallocate all allocated clusters. */ + deallocate_scattered_clusters(rl); + /* Free the runlist. */ + free(rl); + } + return NULL; +} + +/** + * ntfs_attr_find - find (next) attribute in mft record + * @type: attribute type to find + * @name: attribute name to find (optional, i.e. NULL means don't care) + * @name_len: attribute name length (only needed if @name present) + * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) + * @val: attribute value to find (optional, resident attributes only) + * @val_len: attribute value length + * @ctx: search context with mft record and attribute to search from + * + * You shouldn't need to call this function directly. Use lookup_attr() instead. + * + * ntfs_attr_find() takes a search context @ctx as parameter and searches the + * mft record specified by @ctx->mrec, beginning at @ctx->attr, for an + * attribute of @type, optionally @name and @val. If found, ntfs_attr_find() + * returns 0 and @ctx->attr will point to the found attribute. + * + * If not found, ntfs_attr_find() returns -1, with errno set to ENOENT and + * @ctx->attr will point to the attribute before which the attribute being + * searched for would need to be inserted if such an action were to be desired. + * + * On actual error, ntfs_attr_find() returns -1 with errno set to the error + * code but not to ENOENT. In this case @ctx->attr is undefined and in + * particular do not rely on it not changing. + * + * If @ctx->is_first is TRUE, the search begins with @ctx->attr itself. If it + * is FALSE, the search begins after @ctx->attr. + * + * If @type is AT_UNUSED, return the first found attribute, i.e. one can + * enumerate all attributes by setting @type to AT_UNUSED and then calling + * ntfs_attr_find() repeatedly until it returns -1 with errno set to ENOENT to + * indicate that there are no more entries. During the enumeration, each + * successful call of ntfs_attr_find() will return the next attribute in the + * mft record @ctx->mrec. + * + * If @type is AT_END, seek to the end and return -1 with errno set to ENOENT. + * AT_END is not a valid attribute, its length is zero for example, thus it is + * safer to return error instead of success in this case. This also allows us + * to interoperate cleanly with ntfs_external_attr_find(). + * + * If @name is AT_UNNAMED search for an unnamed attribute. If @name is present + * but not AT_UNNAMED search for a named attribute matching @name. Otherwise, + * match both named and unnamed attributes. + * + * If @ic is IGNORE_CASE, the @name comparison is not case sensitive and + * @ctx->ntfs_ino must be set to the ntfs inode to which the mft record + * @ctx->mrec belongs. This is so we can get at the ntfs volume and hence at + * the upcase table. If @ic is CASE_SENSITIVE, the comparison is case + * sensitive. When @name is present, @name_len is the @name length in Unicode + * characters. + * + * If @name is not present (NULL), we assume that the unnamed attribute is + * being searched for. + * + * Finally, the resident attribute value @val is looked for, if present. + * If @val is not present (NULL), @val_len is ignored. + * + * ntfs_attr_find() only searches the specified mft record and it ignores the + * presence of an attribute list attribute (unless it is the one being searched + * for, obviously). If you need to take attribute lists into consideration, use + * ntfs_attr_lookup() instead (see below). This also means that you cannot use + * ntfs_attr_find() to search for extent records of non-resident attributes, as + * extents with lowest_vcn != 0 are usually described by the attribute list + * attribute only. - Note that it is possible that the first extent is only in + * the attribute list while the last extent is in the base mft record, so don't + * rely on being able to find the first extent in the base mft record. + * + * Warning: Never use @val when looking for attribute types which can be + * non-resident as this most likely will result in a crash! + */ +static int mkntfs_attr_find(const ATTR_TYPES type, const ntfschar *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const u8 *val, const u32 val_len, ntfs_attr_search_ctx *ctx) +{ + ATTR_RECORD *a; + ntfschar *upcase = g_vol->upcase; + u32 upcase_len = g_vol->upcase_len; + + /* + * Iterate over attributes in mft record starting at @ctx->attr, or the + * attribute following that, if @ctx->is_first is TRUE. + */ + if (ctx->is_first) { + a = ctx->attr; + ctx->is_first = FALSE; + } else { + a = (ATTR_RECORD*)((char*)ctx->attr + + le32_to_cpu(ctx->attr->length)); + } + for (;; a = (ATTR_RECORD*)((char*)a + le32_to_cpu(a->length))) { + if (p2n(a) < p2n(ctx->mrec) || (char*)a > (char*)ctx->mrec + + le32_to_cpu(ctx->mrec->bytes_allocated)) + break; + ctx->attr = a; + if (((type != AT_UNUSED) && (le32_to_cpu(a->type) > + le32_to_cpu(type))) || + (a->type == AT_END)) { + errno = ENOENT; + return -1; + } + if (!a->length) + break; + /* If this is an enumeration return this attribute. */ + if (type == AT_UNUSED) + return 0; + if (a->type != type) + continue; + /* + * If @name is AT_UNNAMED we want an unnamed attribute. + * If @name is present, compare the two names. + * Otherwise, match any attribute. + */ + if (name == AT_UNNAMED) { + /* The search failed if the found attribute is named. */ + if (a->name_length) { + errno = ENOENT; + return -1; + } + } else if (name && !ntfs_names_are_equal(name, name_len, + (ntfschar*)((char*)a + le16_to_cpu(a->name_offset)), + a->name_length, ic, upcase, upcase_len)) { + int rc; + + rc = ntfs_names_full_collate(name, name_len, + (ntfschar*)((char*)a + + le16_to_cpu(a->name_offset)), + a->name_length, IGNORE_CASE, + upcase, upcase_len); + /* + * If @name collates before a->name, there is no + * matching attribute. + */ + if (rc == -1) { + errno = ENOENT; + return -1; + } + /* If the strings are not equal, continue search. */ + if (rc) + continue; + rc = ntfs_names_full_collate(name, name_len, + (ntfschar*)((char*)a + + le16_to_cpu(a->name_offset)), + a->name_length, CASE_SENSITIVE, + upcase, upcase_len); + if (rc == -1) { + errno = ENOENT; + return -1; + } + if (rc) + continue; + } + /* + * The names match or @name not present and attribute is + * unnamed. If no @val specified, we have found the attribute + * and are done. + */ + if (!val) { + return 0; + /* @val is present; compare values. */ + } else { + int rc; + + rc = memcmp(val, (char*)a +le16_to_cpu(a->value_offset), + min(val_len, + le32_to_cpu(a->value_length))); + /* + * If @val collates before the current attribute's + * value, there is no matching attribute. + */ + if (!rc) { + u32 avl; + avl = le32_to_cpu(a->value_length); + if (val_len == avl) + return 0; + if (val_len < avl) { + errno = ENOENT; + return -1; + } + } else if (rc < 0) { + errno = ENOENT; + return -1; + } + } + } + ntfs_log_trace("File is corrupt. Run chkdsk.\n"); + errno = EIO; + return -1; +} + +/** + * ntfs_attr_lookup - find an attribute in an ntfs inode + * @type: attribute type to find + * @name: attribute name to find (optional, i.e. NULL means don't care) + * @name_len: attribute name length (only needed if @name present) + * @ic: IGNORE_CASE or CASE_SENSITIVE (ignored if @name not present) + * @lowest_vcn: lowest vcn to find (optional, non-resident attributes only) + * @val: attribute value to find (optional, resident attributes only) + * @val_len: attribute value length + * @ctx: search context with mft record and attribute to search from + * + * Find an attribute in an ntfs inode. On first search @ctx->ntfs_ino must + * be the base mft record and @ctx must have been obtained from a call to + * ntfs_attr_get_search_ctx(). + * + * This function transparently handles attribute lists and @ctx is used to + * continue searches where they were left off at. + * + * If @type is AT_UNUSED, return the first found attribute, i.e. one can + * enumerate all attributes by setting @type to AT_UNUSED and then calling + * ntfs_attr_lookup() repeatedly until it returns -1 with errno set to ENOENT + * to indicate that there are no more entries. During the enumeration, each + * successful call of ntfs_attr_lookup() will return the next attribute, with + * the current attribute being described by the search context @ctx. + * + * If @type is AT_END, seek to the end of the base mft record ignoring the + * attribute list completely and return -1 with errno set to ENOENT. AT_END is + * not a valid attribute, its length is zero for example, thus it is safer to + * return error instead of success in this case. It should never be needed to + * do this, but we implement the functionality because it allows for simpler + * code inside ntfs_external_attr_find(). + * + * If @name is AT_UNNAMED search for an unnamed attribute. If @name is present + * but not AT_UNNAMED search for a named attribute matching @name. Otherwise, + * match both named and unnamed attributes. + * + * After finishing with the attribute/mft record you need to call + * ntfs_attr_put_search_ctx() to cleanup the search context (unmapping any + * mapped extent inodes, etc). + * + * Return 0 if the search was successful and -1 if not, with errno set to the + * error code. + * + * On success, @ctx->attr is the found attribute, it is in mft record + * @ctx->mrec, and @ctx->al_entry is the attribute list entry for this + * attribute with @ctx->base_* being the base mft record to which @ctx->attr + * belongs. If no attribute list attribute is present @ctx->al_entry and + * @ctx->base_* are NULL. + * + * On error ENOENT, i.e. attribute not found, @ctx->attr is set to the + * attribute which collates just after the attribute being searched for in the + * base ntfs inode, i.e. if one wants to add the attribute to the mft record + * this is the correct place to insert it into, and if there is not enough + * space, the attribute should be placed in an extent mft record. + * @ctx->al_entry points to the position within @ctx->base_ntfs_ino->attr_list + * at which the new attribute's attribute list entry should be inserted. The + * other @ctx fields, base_ntfs_ino, base_mrec, and base_attr are set to NULL. + * The only exception to this is when @type is AT_END, in which case + * @ctx->al_entry is set to NULL also (see above). + * + * The following error codes are defined: + * ENOENT Attribute not found, not an error as such. + * EINVAL Invalid arguments. + * EIO I/O error or corrupt data structures found. + * ENOMEM Not enough memory to allocate necessary buffers. + */ +static int mkntfs_attr_lookup(const ATTR_TYPES type, const ntfschar *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const VCN lowest_vcn __attribute__((unused)), const u8 *val, + const u32 val_len, ntfs_attr_search_ctx *ctx) +{ + ntfs_inode *base_ni; + + if (!ctx || !ctx->mrec || !ctx->attr) { + errno = EINVAL; + return -1; + } + if (ctx->base_ntfs_ino) + base_ni = ctx->base_ntfs_ino; + else + base_ni = ctx->ntfs_ino; + if (!base_ni || !NInoAttrList(base_ni) || type == AT_ATTRIBUTE_LIST) + return mkntfs_attr_find(type, name, name_len, ic, val, val_len, + ctx); + errno = EOPNOTSUPP; + return -1; +} + +/** + * insert_positioned_attr_in_mft_record + * + * Create a non-resident attribute with a predefined on disk location + * specified by the runlist @rl. The clusters specified by @rl are assumed to + * be allocated already. + * + * Return 0 on success and -errno on error. + */ +static int insert_positioned_attr_in_mft_record(MFT_RECORD *m, + const ATTR_TYPES type, const char *name, u32 name_len, + const IGNORE_CASE_BOOL ic, const ATTR_FLAGS flags, + const runlist *rl, const u8 *val, const s64 val_len) +{ + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *a; + u16 hdr_size; + int asize, mpa_size, err, i; + s64 bw = 0, inited_size; + VCN highest_vcn; + ntfschar *uname = NULL; + int uname_len = 0; + /* + if (base record) + attr_lookup(); + else + */ + + uname = ntfs_str2ucs(name, &uname_len); + if (!uname) + return -errno; + + /* Check if the attribute is already there. */ + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_error("Failed to allocate attribute search context.\n"); + err = -ENOMEM; + goto err_out; + } + if (ic == IGNORE_CASE) { + ntfs_log_error("FIXME: Hit unimplemented code path #1.\n"); + err = -EOPNOTSUPP; + goto err_out; + } + if (!mkntfs_attr_lookup(type, uname, uname_len, ic, 0, NULL, 0, ctx)) { + err = -EEXIST; + goto err_out; + } + if (errno != ENOENT) { + ntfs_log_error("Corrupt inode.\n"); + err = -errno; + goto err_out; + } + a = ctx->attr; + if (flags & ATTR_COMPRESSION_MASK) { + ntfs_log_error("Compressed attributes not supported yet.\n"); + /* FIXME: Compress attribute into a temporary buffer, set */ + /* val accordingly and save the compressed size. */ + err = -EOPNOTSUPP; + goto err_out; + } + if (flags & (ATTR_IS_ENCRYPTED | ATTR_IS_SPARSE)) { + ntfs_log_error("Encrypted/sparse attributes not supported.\n"); + err = -EOPNOTSUPP; + goto err_out; + } + if (flags & ATTR_COMPRESSION_MASK) { + hdr_size = 72; + /* FIXME: This compression stuff is all wrong. Never mind for */ + /* now. (AIA) */ + if (val_len) + mpa_size = 0; /* get_size_for_compressed_mapping_pairs(rl); */ + else + mpa_size = 0; + } else { + hdr_size = 64; + if (val_len) { + mpa_size = ntfs_get_size_for_mapping_pairs(g_vol, rl, 0, INT_MAX); + if (mpa_size < 0) { + err = -errno; + ntfs_log_error("Failed to get size for mapping " + "pairs.\n"); + goto err_out; + } + } else { + mpa_size = 0; + } + } + /* Mapping pairs array and next attribute must be 8-byte aligned. */ + asize = (((int)hdr_size + ((name_len + 7) & ~7) + mpa_size) + 7) & ~7; + /* Get the highest vcn. */ + for (i = 0, highest_vcn = 0LL; rl[i].length; i++) + highest_vcn += rl[i].length; + /* Does the value fit inside the allocated size? */ + if (highest_vcn * g_vol->cluster_size < val_len) { + ntfs_log_error("BUG: Allocated size is smaller than data size!\n"); + err = -EINVAL; + goto err_out; + } + err = make_room_for_attribute(m, (char*)a, asize); + if (err == -ENOSPC) { + /* + * FIXME: Make space! (AIA) + * can we make it non-resident? if yes, do that. + * does it fit now? yes -> do it. + * m's $DATA or $BITMAP+$INDEX_ALLOCATION resident? + * yes -> make non-resident + * does it fit now? yes -> do it. + * make all attributes non-resident + * does it fit now? yes -> do it. + * m is a base record? yes -> allocate extension record + * does the new attribute fit in there? yes -> do it. + * split up runlist into extents and place each in an extension + * record. + * FIXME: the check for needing extension records should be + * earlier on as it is very quick: asize > m->bytes_allocated? + */ + err = -EOPNOTSUPP; + goto err_out; +#ifdef DEBUG + } else if (err == -EINVAL) { + ntfs_log_error("BUG(): in insert_positioned_attribute_in_mft_" + "record(): make_room_for_attribute() returned " + "error: EINVAL!\n"); + goto err_out; +#endif + } + a->type = type; + a->length = cpu_to_le32(asize); + a->non_resident = 1; + a->name_length = name_len; + a->name_offset = cpu_to_le16(hdr_size); + a->flags = flags; + a->instance = m->next_attr_instance; + m->next_attr_instance = cpu_to_le16((le16_to_cpu(m->next_attr_instance) + + 1) & 0xffff); + a->lowest_vcn = const_cpu_to_sle64(0); + a->highest_vcn = cpu_to_sle64(highest_vcn - 1LL); + a->mapping_pairs_offset = cpu_to_le16(hdr_size + ((name_len + 7) & ~7)); + memset(a->reserved1, 0, sizeof(a->reserved1)); + /* FIXME: Allocated size depends on compression. */ + a->allocated_size = cpu_to_sle64(highest_vcn * g_vol->cluster_size); + a->data_size = cpu_to_sle64(val_len); + if (name_len) + memcpy((char*)a + hdr_size, uname, name_len << 1); + if (flags & ATTR_COMPRESSION_MASK) { + if (flags & ATTR_COMPRESSION_MASK & ~ATTR_IS_COMPRESSED) { + ntfs_log_error("Unknown compression format. Reverting " + "to standard compression.\n"); + a->flags &= ~ATTR_COMPRESSION_MASK; + a->flags |= ATTR_IS_COMPRESSED; + } + a->compression_unit = 4; + inited_size = val_len; + /* FIXME: Set the compressed size. */ + a->compressed_size = const_cpu_to_sle64(0); + /* FIXME: Write out the compressed data. */ + /* FIXME: err = build_mapping_pairs_compressed(); */ + err = -EOPNOTSUPP; + } else { + a->compression_unit = 0; + if ((type == AT_DATA) + && (m->mft_record_number + == const_cpu_to_le32(FILE_LogFile))) + bw = ntfs_rlwrite(g_vol->dev, rl, val, val_len, + &inited_size, WRITE_LOGFILE); + else + bw = ntfs_rlwrite(g_vol->dev, rl, val, val_len, + &inited_size, WRITE_STANDARD); + if (bw != val_len) { + ntfs_log_error("Error writing non-resident attribute " + "value.\n"); + return -errno; + } + err = ntfs_mapping_pairs_build(g_vol, (u8*)a + hdr_size + + ((name_len + 7) & ~7), mpa_size, rl, 0, NULL); + } + a->initialized_size = cpu_to_sle64(inited_size); + if (err < 0 || bw != val_len) { + /* FIXME: Handle error. */ + /* deallocate clusters */ + /* remove attribute */ + if (err >= 0) + err = -EIO; + ntfs_log_error("insert_positioned_attr_in_mft_record failed " + "with error %i.\n", err < 0 ? err : (int)bw); + } +err_out: + if (ctx) + ntfs_attr_put_search_ctx(ctx); + ntfs_ucsfree(uname); + return err; +} + +/** + * insert_non_resident_attr_in_mft_record + * + * Return 0 on success and -errno on error. + */ +static int insert_non_resident_attr_in_mft_record(MFT_RECORD *m, + const ATTR_TYPES type, const char *name, u32 name_len, + const IGNORE_CASE_BOOL ic, const ATTR_FLAGS flags, + const u8 *val, const s64 val_len, + WRITE_TYPE write_type) +{ + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *a; + u16 hdr_size; + int asize, mpa_size, err, i; + runlist *rl = NULL; + s64 bw = 0; + ntfschar *uname = NULL; + int uname_len = 0; + /* + if (base record) + attr_lookup(); + else + */ + + uname = ntfs_str2ucs(name, &uname_len); + if (!uname) + return -errno; + + /* Check if the attribute is already there. */ + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_error("Failed to allocate attribute search context.\n"); + err = -ENOMEM; + goto err_out; + } + if (ic == IGNORE_CASE) { + ntfs_log_error("FIXME: Hit unimplemented code path #2.\n"); + err = -EOPNOTSUPP; + goto err_out; + } + if (!mkntfs_attr_lookup(type, uname, uname_len, ic, 0, NULL, 0, ctx)) { + err = -EEXIST; + goto err_out; + } + if (errno != ENOENT) { + ntfs_log_error("Corrupt inode.\n"); + err = -errno; + goto err_out; + } + a = ctx->attr; + if (flags & ATTR_COMPRESSION_MASK) { + ntfs_log_error("Compressed attributes not supported yet.\n"); + /* FIXME: Compress attribute into a temporary buffer, set */ + /* val accordingly and save the compressed size. */ + err = -EOPNOTSUPP; + goto err_out; + } + if (flags & (ATTR_IS_ENCRYPTED | ATTR_IS_SPARSE)) { + ntfs_log_error("Encrypted/sparse attributes not supported.\n"); + err = -EOPNOTSUPP; + goto err_out; + } + if (val_len) { + rl = allocate_scattered_clusters((val_len + + g_vol->cluster_size - 1) / g_vol->cluster_size); + if (!rl) { + err = -errno; + ntfs_log_perror("Failed to allocate scattered clusters"); + goto err_out; + } + } else { + rl = NULL; + } + if (flags & ATTR_COMPRESSION_MASK) { + hdr_size = 72; + /* FIXME: This compression stuff is all wrong. Never mind for */ + /* now. (AIA) */ + if (val_len) + mpa_size = 0; /* get_size_for_compressed_mapping_pairs(rl); */ + else + mpa_size = 0; + } else { + hdr_size = 64; + if (val_len) { + mpa_size = ntfs_get_size_for_mapping_pairs(g_vol, rl, 0, INT_MAX); + if (mpa_size < 0) { + err = -errno; + ntfs_log_error("Failed to get size for mapping " + "pairs.\n"); + goto err_out; + } + } else { + mpa_size = 0; + } + } + /* Mapping pairs array and next attribute must be 8-byte aligned. */ + asize = (((int)hdr_size + ((name_len + 7) & ~7) + mpa_size) + 7) & ~7; + err = make_room_for_attribute(m, (char*)a, asize); + if (err == -ENOSPC) { + /* + * FIXME: Make space! (AIA) + * can we make it non-resident? if yes, do that. + * does it fit now? yes -> do it. + * m's $DATA or $BITMAP+$INDEX_ALLOCATION resident? + * yes -> make non-resident + * does it fit now? yes -> do it. + * make all attributes non-resident + * does it fit now? yes -> do it. + * m is a base record? yes -> allocate extension record + * does the new attribute fit in there? yes -> do it. + * split up runlist into extents and place each in an extension + * record. + * FIXME: the check for needing extension records should be + * earlier on as it is very quick: asize > m->bytes_allocated? + */ + err = -EOPNOTSUPP; + goto err_out; +#ifdef DEBUG + } else if (err == -EINVAL) { + ntfs_log_error("BUG(): in insert_non_resident_attribute_in_" + "mft_record(): make_room_for_attribute() " + "returned error: EINVAL!\n"); + goto err_out; +#endif + } + a->type = type; + a->length = cpu_to_le32(asize); + a->non_resident = 1; + a->name_length = name_len; + a->name_offset = cpu_to_le16(hdr_size); + a->flags = flags; + a->instance = m->next_attr_instance; + m->next_attr_instance = cpu_to_le16((le16_to_cpu(m->next_attr_instance) + + 1) & 0xffff); + a->lowest_vcn = const_cpu_to_sle64(0); + for (i = 0; rl[i].length; i++) + ; + a->highest_vcn = cpu_to_sle64(rl[i].vcn - 1); + a->mapping_pairs_offset = cpu_to_le16(hdr_size + ((name_len + 7) & ~7)); + memset(a->reserved1, 0, sizeof(a->reserved1)); + /* FIXME: Allocated size depends on compression. */ + a->allocated_size = cpu_to_sle64((val_len + (g_vol->cluster_size - 1)) & + ~(g_vol->cluster_size - 1)); + a->data_size = cpu_to_sle64(val_len); + a->initialized_size = cpu_to_sle64(val_len); + if (name_len) + memcpy((char*)a + hdr_size, uname, name_len << 1); + if (flags & ATTR_COMPRESSION_MASK) { + if (flags & ATTR_COMPRESSION_MASK & ~ATTR_IS_COMPRESSED) { + ntfs_log_error("Unknown compression format. Reverting " + "to standard compression.\n"); + a->flags &= ~ATTR_COMPRESSION_MASK; + a->flags |= ATTR_IS_COMPRESSED; + } + a->compression_unit = 4; + /* FIXME: Set the compressed size. */ + a->compressed_size = const_cpu_to_sle64(0); + /* FIXME: Write out the compressed data. */ + /* FIXME: err = build_mapping_pairs_compressed(); */ + err = -EOPNOTSUPP; + } else { + a->compression_unit = 0; + bw = ntfs_rlwrite(g_vol->dev, rl, val, val_len, NULL, + write_type); + if (bw != val_len) { + ntfs_log_error("Error writing non-resident attribute " + "value.\n"); + return -errno; + } + err = ntfs_mapping_pairs_build(g_vol, (u8*)a + hdr_size + + ((name_len + 7) & ~7), mpa_size, rl, 0, NULL); + } + if (err < 0 || bw != val_len) { + /* FIXME: Handle error. */ + /* deallocate clusters */ + /* remove attribute */ + if (err >= 0) + err = -EIO; + ntfs_log_error("insert_non_resident_attr_in_mft_record failed with " + "error %lld.\n", (long long) (err < 0 ? err : bw)); + } +err_out: + if (ctx) + ntfs_attr_put_search_ctx(ctx); + ntfs_ucsfree(uname); + free(rl); + return err; +} + +/** + * insert_resident_attr_in_mft_record + * + * Return 0 on success and -errno on error. + */ +static int insert_resident_attr_in_mft_record(MFT_RECORD *m, + const ATTR_TYPES type, const char *name, u32 name_len, + const IGNORE_CASE_BOOL ic, const ATTR_FLAGS flags, + const RESIDENT_ATTR_FLAGS res_flags, + const u8 *val, const u32 val_len) +{ + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *a; + int asize, err; + ntfschar *uname = NULL; + int uname_len = 0; + /* + if (base record) + mkntfs_attr_lookup(); + else + */ + + uname = ntfs_str2ucs(name, &uname_len); + if (!uname) + return -errno; + + /* Check if the attribute is already there. */ + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_error("Failed to allocate attribute search context.\n"); + err = -ENOMEM; + goto err_out; + } + if (ic == IGNORE_CASE) { + ntfs_log_error("FIXME: Hit unimplemented code path #3.\n"); + err = -EOPNOTSUPP; + goto err_out; + } + if (!mkntfs_attr_lookup(type, uname, uname_len, ic, 0, val, val_len, + ctx)) { + err = -EEXIST; + goto err_out; + } + if (errno != ENOENT) { + ntfs_log_error("Corrupt inode.\n"); + err = -errno; + goto err_out; + } + a = ctx->attr; + /* sizeof(resident attribute record header) == 24 */ + asize = ((24 + ((name_len*2 + 7) & ~7) + val_len) + 7) & ~7; + err = make_room_for_attribute(m, (char*)a, asize); + if (err == -ENOSPC) { + /* + * FIXME: Make space! (AIA) + * can we make it non-resident? if yes, do that. + * does it fit now? yes -> do it. + * m's $DATA or $BITMAP+$INDEX_ALLOCATION resident? + * yes -> make non-resident + * does it fit now? yes -> do it. + * make all attributes non-resident + * does it fit now? yes -> do it. + * m is a base record? yes -> allocate extension record + * does the new attribute fit in there? yes -> do it. + * split up runlist into extents and place each in an extension + * record. + * FIXME: the check for needing extension records should be + * earlier on as it is very quick: asize > m->bytes_allocated? + */ + err = -EOPNOTSUPP; + goto err_out; + } +#ifdef DEBUG + if (err == -EINVAL) { + ntfs_log_error("BUG(): in insert_resident_attribute_in_mft_" + "record(): make_room_for_attribute() returned " + "error: EINVAL!\n"); + goto err_out; + } +#endif + a->type = type; + a->length = cpu_to_le32(asize); + a->non_resident = 0; + a->name_length = name_len; + if (type == AT_OBJECT_ID) + a->name_offset = const_cpu_to_le16(0); + else + a->name_offset = const_cpu_to_le16(24); + a->flags = flags; + a->instance = m->next_attr_instance; + m->next_attr_instance = cpu_to_le16((le16_to_cpu(m->next_attr_instance) + + 1) & 0xffff); + a->value_length = cpu_to_le32(val_len); + a->value_offset = cpu_to_le16(24 + ((name_len*2 + 7) & ~7)); + a->resident_flags = res_flags; + a->reservedR = 0; + if (name_len) + memcpy((char*)a + 24, uname, name_len << 1); + if (val_len) + memcpy((char*)a + le16_to_cpu(a->value_offset), val, val_len); +err_out: + if (ctx) + ntfs_attr_put_search_ctx(ctx); + ntfs_ucsfree(uname); + return err; +} + + +/** + * add_attr_std_info + * + * Return 0 on success or -errno on error. + */ +static int add_attr_std_info(MFT_RECORD *m, const FILE_ATTR_FLAGS flags, + le32 security_id) +{ + STANDARD_INFORMATION si; + int err, sd_size; + + sd_size = 48; + + si.creation_time = mkntfs_time(); + si.last_data_change_time = si.creation_time; + si.last_mft_change_time = si.creation_time; + si.last_access_time = si.creation_time; + si.file_attributes = flags; /* already LE */ + si.maximum_versions = const_cpu_to_le32(0); + si.version_number = const_cpu_to_le32(0); + si.class_id = const_cpu_to_le32(0); + si.security_id = security_id; + if (si.security_id != const_cpu_to_le32(0)) + sd_size = 72; + /* FIXME: $Quota support... */ + si.owner_id = const_cpu_to_le32(0); + si.quota_charged = const_cpu_to_le64(0ULL); + /* FIXME: $UsnJrnl support... Not needed on fresh w2k3-volume */ + si.usn = const_cpu_to_le64(0ULL); + /* NTFS 1.2: size of si = 48, NTFS 3.[01]: size of si = 72 */ + err = insert_resident_attr_in_mft_record(m, AT_STANDARD_INFORMATION, + NULL, 0, CASE_SENSITIVE, const_cpu_to_le16(0), + 0, (u8*)&si, sd_size); + if (err < 0) + ntfs_log_perror("add_attr_std_info failed"); + return err; +} + +/* + * Tell whether the unnamed data is non resident + */ + +static BOOL non_resident_unnamed_data(MFT_RECORD *m) +{ + ATTR_RECORD *a; + ntfs_attr_search_ctx *ctx; + BOOL nonres; + + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (ctx && !mkntfs_attr_find(AT_DATA, + (const ntfschar*)NULL, 0, CASE_SENSITIVE, + (u8*)NULL, 0, ctx)) { + a = ctx->attr; + nonres = a->non_resident != 0; + } else { + ntfs_log_error("BUG: Unnamed data not found\n"); + nonres = TRUE; + } + if (ctx) + ntfs_attr_put_search_ctx(ctx); + return (nonres); +} + +/* + * Get the time stored in the standard information attribute + */ + +static ntfs_time stdinfo_time(MFT_RECORD *m) +{ + STANDARD_INFORMATION *si; + ntfs_attr_search_ctx *ctx; + ntfs_time info_time; + + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (ctx && !mkntfs_attr_find(AT_STANDARD_INFORMATION, + (const ntfschar*)NULL, 0, CASE_SENSITIVE, + (u8*)NULL, 0, ctx)) { + si = (STANDARD_INFORMATION*)((char*)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + info_time = si->creation_time; + } else { + ntfs_log_error("BUG: Standard information not found\n"); + info_time = mkntfs_time(); + } + if (ctx) + ntfs_attr_put_search_ctx(ctx); + return (info_time); +} + +/** + * add_attr_file_name + * + * Return 0 on success or -errno on error. + */ +static int add_attr_file_name(MFT_RECORD *m, const leMFT_REF parent_dir, + const s64 allocated_size, const s64 data_size, + const FILE_ATTR_FLAGS flags, const u16 packed_ea_size, + const u32 reparse_point_tag, const char *file_name, + const FILE_NAME_TYPE_FLAGS file_name_type) +{ + ntfs_attr_search_ctx *ctx; + STANDARD_INFORMATION *si; + FILE_NAME_ATTR *fn; + int i, fn_size; + ntfschar *uname; + + /* Check if the attribute is already there. */ + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_error("Failed to get attribute search context.\n"); + return -ENOMEM; + } + if (mkntfs_attr_lookup(AT_STANDARD_INFORMATION, AT_UNNAMED, 0, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + int eo = errno; + ntfs_log_error("BUG: Standard information attribute not " + "present in file record.\n"); + ntfs_attr_put_search_ctx(ctx); + return -eo; + } + si = (STANDARD_INFORMATION*)((char*)ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + i = (strlen(file_name) + 1) * sizeof(ntfschar); + fn_size = sizeof(FILE_NAME_ATTR) + i; + fn = ntfs_malloc(fn_size); + if (!fn) { + ntfs_attr_put_search_ctx(ctx); + return -errno; + } + fn->parent_directory = parent_dir; + + fn->creation_time = si->creation_time; + fn->last_data_change_time = si->last_data_change_time; + fn->last_mft_change_time = si->last_mft_change_time; + fn->last_access_time = si->last_access_time; + ntfs_attr_put_search_ctx(ctx); + + fn->allocated_size = cpu_to_sle64(allocated_size); + fn->data_size = cpu_to_sle64(data_size); + fn->file_attributes = flags; + /* These are in a union so can't have both. */ + if (packed_ea_size && reparse_point_tag) { + free(fn); + return -EINVAL; + } + if (packed_ea_size) { + fn->packed_ea_size = cpu_to_le16(packed_ea_size); + fn->reserved = const_cpu_to_le16(0); + } else { + fn->reparse_point_tag = cpu_to_le32(reparse_point_tag); + } + fn->file_name_type = file_name_type; + uname = fn->file_name; + i = ntfs_mbstoucs_libntfscompat(file_name, &uname, i); + if (i < 1) { + free(fn); + return -EINVAL; + } + if (i > 0xff) { + free(fn); + return -ENAMETOOLONG; + } + /* No terminating null in file names. */ + fn->file_name_length = i; + fn_size = sizeof(FILE_NAME_ATTR) + i * sizeof(ntfschar); + i = insert_resident_attr_in_mft_record(m, AT_FILE_NAME, NULL, 0, + CASE_SENSITIVE, const_cpu_to_le16(0), + RESIDENT_ATTR_IS_INDEXED, (u8*)fn, fn_size); + free(fn); + if (i < 0) + ntfs_log_error("add_attr_file_name failed: %s\n", strerror(-i)); + return i; +} + +/** + * add_attr_object_id - + * + * Note we insert only a basic object id which only has the GUID and none of + * the extended fields. This is because we currently only use this function + * when creating the object id for the volume. + * + * Return 0 on success or -errno on error. + */ +static int add_attr_object_id(MFT_RECORD *m, const GUID *object_id) +{ + OBJECT_ID_ATTR oi; + int err; + + oi = (OBJECT_ID_ATTR) { + .object_id = *object_id, + }; + err = insert_resident_attr_in_mft_record(m, AT_OBJECT_ID, NULL, + 0, CASE_SENSITIVE, const_cpu_to_le16(0), + 0, (u8*)&oi, sizeof(oi.object_id)); + if (err < 0) + ntfs_log_error("add_attr_vol_info failed: %s\n", strerror(-err)); + return err; +} + +/** + * add_attr_sd + * + * Create the security descriptor attribute adding the security descriptor @sd + * of length @sd_len to the mft record @m. + * + * Return 0 on success or -errno on error. + */ +static int add_attr_sd(MFT_RECORD *m, const u8 *sd, const s64 sd_len) +{ + int err; + + /* Does it fit? NO: create non-resident. YES: create resident. */ + if (le32_to_cpu(m->bytes_in_use) + 24 + sd_len > + le32_to_cpu(m->bytes_allocated)) + err = insert_non_resident_attr_in_mft_record(m, + AT_SECURITY_DESCRIPTOR, NULL, 0, + CASE_SENSITIVE, const_cpu_to_le16(0), sd, + sd_len, WRITE_STANDARD); + else + err = insert_resident_attr_in_mft_record(m, + AT_SECURITY_DESCRIPTOR, NULL, 0, + CASE_SENSITIVE, const_cpu_to_le16(0), 0, sd, + sd_len); + if (err < 0) + ntfs_log_error("add_attr_sd failed: %s\n", strerror(-err)); + return err; +} + +/** + * add_attr_data + * + * Return 0 on success or -errno on error. + */ +static int add_attr_data(MFT_RECORD *m, const char *name, const u32 name_len, + const IGNORE_CASE_BOOL ic, const ATTR_FLAGS flags, + const u8 *val, const s64 val_len) +{ + int err; + + /* + * Does it fit? NO: create non-resident. YES: create resident. + * + * FIXME: Introduced arbitrary limit of mft record allocated size - 512. + * This is to get around the problem that if $Bitmap/$DATA becomes too + * big, but is just small enough to be resident, we would make it + * resident, and later run out of space when creating the other + * attributes and this would cause us to abort as making resident + * attributes non-resident is not supported yet. + * The proper fix is to support making resident attribute non-resident. + */ + if (le32_to_cpu(m->bytes_in_use) + 24 + val_len > + min(le32_to_cpu(m->bytes_allocated), + le32_to_cpu(m->bytes_allocated) - 512)) + err = insert_non_resident_attr_in_mft_record(m, AT_DATA, name, + name_len, ic, flags, val, val_len, + WRITE_STANDARD); + else + err = insert_resident_attr_in_mft_record(m, AT_DATA, name, + name_len, ic, flags, 0, val, val_len); + + if (err < 0) + ntfs_log_error("add_attr_data failed: %s\n", strerror(-err)); + return err; +} + +/** + * add_attr_data_positioned + * + * Create a non-resident data attribute with a predefined on disk location + * specified by the runlist @rl. The clusters specified by @rl are assumed to + * be allocated already. + * + * Return 0 on success or -errno on error. + */ +static int add_attr_data_positioned(MFT_RECORD *m, const char *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const ATTR_FLAGS flags, const runlist *rl, + const u8 *val, const s64 val_len) +{ + int err; + + err = insert_positioned_attr_in_mft_record(m, AT_DATA, name, name_len, + ic, flags, rl, val, val_len); + if (err < 0) + ntfs_log_error("add_attr_data_positioned failed: %s\n", + strerror(-err)); + return err; +} + +/** + * add_attr_vol_name + * + * Create volume name attribute specifying the volume name @vol_name as a null + * terminated char string of length @vol_name_len (number of characters not + * including the terminating null), which is converted internally to a little + * endian ntfschar string. The name is at least 1 character long (though + * Windows accepts zero characters), and at most 128 characters long (not + * counting the terminating null). + * + * Return 0 on success or -errno on error. + */ +static int add_attr_vol_name(MFT_RECORD *m, const char *vol_name, + const int vol_name_len __attribute__((unused))) +{ + ntfschar *uname = NULL; + int uname_len = 0; + int i; + + if (vol_name) { + uname_len = ntfs_mbstoucs(vol_name, &uname); + if (uname_len < 0) + return -errno; + if (uname_len > 128) { + free(uname); + return -ENAMETOOLONG; + } + } + i = insert_resident_attr_in_mft_record(m, AT_VOLUME_NAME, NULL, 0, + CASE_SENSITIVE, const_cpu_to_le16(0), + 0, (u8*)uname, uname_len*sizeof(ntfschar)); + free(uname); + if (i < 0) + ntfs_log_error("add_attr_vol_name failed: %s\n", strerror(-i)); + return i; +} + +/** + * add_attr_vol_info + * + * Return 0 on success or -errno on error. + */ +static int add_attr_vol_info(MFT_RECORD *m, const VOLUME_FLAGS flags, + const u8 major_ver, const u8 minor_ver) +{ + VOLUME_INFORMATION vi; + int err; + + memset(&vi, 0, sizeof(vi)); + vi.major_ver = major_ver; + vi.minor_ver = minor_ver; + vi.flags = flags & VOLUME_FLAGS_MASK; + err = insert_resident_attr_in_mft_record(m, AT_VOLUME_INFORMATION, NULL, + 0, CASE_SENSITIVE, const_cpu_to_le16(0), + 0, (u8*)&vi, sizeof(vi)); + if (err < 0) + ntfs_log_error("add_attr_vol_info failed: %s\n", strerror(-err)); + return err; +} + +/** + * add_attr_index_root + * + * Return 0 on success or -errno on error. + */ +static int add_attr_index_root(MFT_RECORD *m, const char *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const ATTR_TYPES indexed_attr_type, + const COLLATION_RULES collation_rule, + const u32 index_block_size) +{ + INDEX_ROOT *r; + INDEX_ENTRY_HEADER *e; + int err, val_len; + + val_len = sizeof(INDEX_ROOT) + sizeof(INDEX_ENTRY_HEADER); + r = ntfs_malloc(val_len); + if (!r) + return -errno; + r->type = (indexed_attr_type == AT_FILE_NAME) + ? AT_FILE_NAME : const_cpu_to_le32(0); + if (indexed_attr_type == AT_FILE_NAME && + collation_rule != COLLATION_FILE_NAME) { + free(r); + ntfs_log_error("add_attr_index_root: indexed attribute is $FILE_NAME " + "but collation rule is not COLLATION_FILE_NAME.\n"); + return -EINVAL; + } + r->collation_rule = collation_rule; + r->index_block_size = cpu_to_le32(index_block_size); + if (index_block_size >= g_vol->cluster_size) { + if (index_block_size % g_vol->cluster_size) { + ntfs_log_error("add_attr_index_root: index block size is not " + "a multiple of the cluster size.\n"); + free(r); + return -EINVAL; + } + r->clusters_per_index_block = index_block_size / + g_vol->cluster_size; + } else { /* if (g_vol->cluster_size > index_block_size) */ + if (index_block_size & (index_block_size - 1)) { + ntfs_log_error("add_attr_index_root: index block size is not " + "a power of 2.\n"); + free(r); + return -EINVAL; + } + if (index_block_size < (u32)opts.sector_size) { + ntfs_log_error("add_attr_index_root: index block size " + "is smaller than the sector size.\n"); + free(r); + return -EINVAL; + } + r->clusters_per_index_block = index_block_size + >> NTFS_BLOCK_SIZE_BITS; + } + memset(&r->reserved, 0, sizeof(r->reserved)); + r->index.entries_offset = const_cpu_to_le32(sizeof(INDEX_HEADER)); + r->index.index_length = const_cpu_to_le32(sizeof(INDEX_HEADER) + + sizeof(INDEX_ENTRY_HEADER)); + r->index.allocated_size = r->index.index_length; + r->index.ih_flags = SMALL_INDEX; + memset(&r->index.reserved, 0, sizeof(r->index.reserved)); + e = (INDEX_ENTRY_HEADER*)((u8*)&r->index + + le32_to_cpu(r->index.entries_offset)); + /* + * No matter whether this is a file index or a view as this is a + * termination entry, hence no key value / data is associated with it + * at all. Thus, we just need the union to be all zero. + */ + e->indexed_file = const_cpu_to_le64(0LL); + e->length = const_cpu_to_le16(sizeof(INDEX_ENTRY_HEADER)); + e->key_length = const_cpu_to_le16(0); + e->flags = INDEX_ENTRY_END; + e->reserved = const_cpu_to_le16(0); + err = insert_resident_attr_in_mft_record(m, AT_INDEX_ROOT, name, + name_len, ic, const_cpu_to_le16(0), 0, + (u8*)r, val_len); + free(r); + if (err < 0) + ntfs_log_error("add_attr_index_root failed: %s\n", strerror(-err)); + return err; +} + +/** + * add_attr_index_alloc + * + * Return 0 on success or -errno on error. + */ +static int add_attr_index_alloc(MFT_RECORD *m, const char *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const u8 *index_alloc_val, const u32 index_alloc_val_len) +{ + int err; + + err = insert_non_resident_attr_in_mft_record(m, AT_INDEX_ALLOCATION, + name, name_len, ic, const_cpu_to_le16(0), + index_alloc_val, index_alloc_val_len, WRITE_STANDARD); + if (err < 0) + ntfs_log_error("add_attr_index_alloc failed: %s\n", strerror(-err)); + return err; +} + +/** + * add_attr_bitmap + * + * Return 0 on success or -errno on error. + */ +static int add_attr_bitmap(MFT_RECORD *m, const char *name, const u32 name_len, + const IGNORE_CASE_BOOL ic, const u8 *bitmap, + const u32 bitmap_len) +{ + int err; + + /* Does it fit? NO: create non-resident. YES: create resident. */ + if (le32_to_cpu(m->bytes_in_use) + 24 + bitmap_len > + le32_to_cpu(m->bytes_allocated)) + err = insert_non_resident_attr_in_mft_record(m, AT_BITMAP, name, + name_len, ic, const_cpu_to_le16(0), bitmap, + bitmap_len, WRITE_STANDARD); + else + err = insert_resident_attr_in_mft_record(m, AT_BITMAP, name, + name_len, ic, const_cpu_to_le16(0), 0, + bitmap, bitmap_len); + + if (err < 0) + ntfs_log_error("add_attr_bitmap failed: %s\n", strerror(-err)); + return err; +} + +/** + * add_attr_bitmap_positioned + * + * Create a non-resident bitmap attribute with a predefined on disk location + * specified by the runlist @rl. The clusters specified by @rl are assumed to + * be allocated already. + * + * Return 0 on success or -errno on error. + */ +static int add_attr_bitmap_positioned(MFT_RECORD *m, const char *name, + const u32 name_len, const IGNORE_CASE_BOOL ic, + const runlist *rl, const u8 *bitmap, const u32 bitmap_len) +{ + int err; + + err = insert_positioned_attr_in_mft_record(m, AT_BITMAP, name, name_len, + ic, const_cpu_to_le16(0), rl, bitmap, bitmap_len); + if (err < 0) + ntfs_log_error("add_attr_bitmap_positioned failed: %s\n", + strerror(-err)); + return err; +} + + +/** + * upgrade_to_large_index + * + * Create bitmap and index allocation attributes, modify index root + * attribute accordingly and move all of the index entries from the index root + * into the index allocation. + * + * Return 0 on success or -errno on error. + */ +static int upgrade_to_large_index(MFT_RECORD *m, const char *name, + u32 name_len, const IGNORE_CASE_BOOL ic, + INDEX_ALLOCATION **idx) +{ + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *a; + INDEX_ROOT *r; + INDEX_ENTRY *re; + INDEX_ALLOCATION *ia_val = NULL; + ntfschar *uname = NULL; + int uname_len = 0; + u8 bmp[8]; + char *re_start, *re_end; + int i, err, index_block_size; + + uname = ntfs_str2ucs(name, &uname_len); + if (!uname) + return -errno; + + /* Find the index root attribute. */ + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_error("Failed to allocate attribute search context.\n"); + ntfs_ucsfree(uname); + return -ENOMEM; + } + if (ic == IGNORE_CASE) { + ntfs_log_error("FIXME: Hit unimplemented code path #4.\n"); + err = -EOPNOTSUPP; + ntfs_ucsfree(uname); + goto err_out; + } + err = mkntfs_attr_lookup(AT_INDEX_ROOT, uname, uname_len, ic, 0, NULL, 0, + ctx); + ntfs_ucsfree(uname); + if (err) { + err = -ENOTDIR; + goto err_out; + } + a = ctx->attr; + if (a->non_resident || a->flags) { + err = -EINVAL; + goto err_out; + } + r = (INDEX_ROOT*)((char*)a + le16_to_cpu(a->value_offset)); + re_end = (char*)r + le32_to_cpu(a->value_length); + re_start = (char*)&r->index + le32_to_cpu(r->index.entries_offset); + re = (INDEX_ENTRY*)re_start; + index_block_size = le32_to_cpu(r->index_block_size); + memset(bmp, 0, sizeof(bmp)); + ntfs_bit_set(bmp, 0ULL, 1); + /* Bitmap has to be at least 8 bytes in size. */ + err = add_attr_bitmap(m, name, name_len, ic, bmp, sizeof(bmp)); + if (err) + goto err_out; + ia_val = ntfs_calloc(index_block_size); + if (!ia_val) { + err = -errno; + goto err_out; + } + /* Setup header. */ + ia_val->magic = magic_INDX; + ia_val->usa_ofs = const_cpu_to_le16(sizeof(INDEX_ALLOCATION)); + if (index_block_size >= NTFS_BLOCK_SIZE) { + ia_val->usa_count = cpu_to_le16(index_block_size / + NTFS_BLOCK_SIZE + 1); + } else { + ia_val->usa_count = const_cpu_to_le16(1); + ntfs_log_error("Sector size is bigger than index block size. " + "Setting usa_count to 1. If Windows chkdsk " + "reports this as corruption, please email %s " + "stating that you saw this message and that " + "the filesystem created was corrupt. " + "Thank you.", NTFS_DEV_LIST); + } + /* Set USN to 1. */ + *(le16*)((char*)ia_val + le16_to_cpu(ia_val->usa_ofs)) = + const_cpu_to_le16(1); + ia_val->lsn = const_cpu_to_sle64(0); + ia_val->index_block_vcn = const_cpu_to_sle64(0); + ia_val->index.ih_flags = LEAF_NODE; + /* Align to 8-byte boundary. */ + ia_val->index.entries_offset = cpu_to_le32((sizeof(INDEX_HEADER) + + le16_to_cpu(ia_val->usa_count) * 2 + 7) & ~7); + ia_val->index.allocated_size = cpu_to_le32(index_block_size - + (sizeof(INDEX_ALLOCATION) - sizeof(INDEX_HEADER))); + /* Find the last entry in the index root and save it in re. */ + while ((char*)re < re_end && !(re->ie_flags & INDEX_ENTRY_END)) { + /* Next entry in index root. */ + re = (INDEX_ENTRY*)((char*)re + le16_to_cpu(re->length)); + } + /* Copy all the entries including the termination entry. */ + i = (char*)re - re_start + le16_to_cpu(re->length); + memcpy((char*)&ia_val->index + + le32_to_cpu(ia_val->index.entries_offset), re_start, i); + /* Finish setting up index allocation. */ + ia_val->index.index_length = cpu_to_le32(i + + le32_to_cpu(ia_val->index.entries_offset)); + /* Move the termination entry forward to the beginning if necessary. */ + if ((char*)re > re_start) { + memmove(re_start, (char*)re, le16_to_cpu(re->length)); + re = (INDEX_ENTRY*)re_start; + } + /* Now fixup empty index root with pointer to index allocation VCN 0. */ + r->index.ih_flags = LARGE_INDEX; + re->ie_flags |= INDEX_ENTRY_NODE; + if (le16_to_cpu(re->length) < sizeof(INDEX_ENTRY_HEADER) + sizeof(VCN)) + re->length = cpu_to_le16(le16_to_cpu(re->length) + sizeof(VCN)); + r->index.index_length = cpu_to_le32(le32_to_cpu(r->index.entries_offset) + + le16_to_cpu(re->length)); + r->index.allocated_size = r->index.index_length; + /* Resize index root attribute. */ + if (ntfs_resident_attr_value_resize(m, a, sizeof(INDEX_ROOT) - + sizeof(INDEX_HEADER) + + le32_to_cpu(r->index.allocated_size))) { + /* TODO: Remove the added bitmap! */ + /* Revert index root from index allocation. */ + err = -errno; + goto err_out; + } + /* Set VCN pointer to 0LL. */ + *(leVCN*)((char*)re + le16_to_cpu(re->length) - sizeof(VCN)) = + const_cpu_to_sle64(0); + err = ntfs_mst_pre_write_fixup((NTFS_RECORD*)ia_val, index_block_size); + if (err) { + err = -errno; + ntfs_log_error("ntfs_mst_pre_write_fixup() failed in " + "upgrade_to_large_index.\n"); + goto err_out; + } + err = add_attr_index_alloc(m, name, name_len, ic, (u8*)ia_val, + index_block_size); + ntfs_mst_post_write_fixup((NTFS_RECORD*)ia_val); + if (err) { + /* TODO: Remove the added bitmap! */ + /* Revert index root from index allocation. */ + goto err_out; + } + *idx = ia_val; + ntfs_attr_put_search_ctx(ctx); + return 0; +err_out: + ntfs_attr_put_search_ctx(ctx); + free(ia_val); + return err; +} + +/** + * make_room_for_index_entry_in_index_block + * + * Create space of @size bytes at position @pos inside the index block @idx. + * + * Return 0 on success or -errno on error. + */ +static int make_room_for_index_entry_in_index_block(INDEX_BLOCK *idx, + INDEX_ENTRY *pos, u32 size) +{ + u32 biu; + + if (!size) + return 0; +#ifdef DEBUG + /* + * Rigorous consistency checks. Always return -EINVAL even if more + * appropriate codes exist for simplicity of parsing the return value. + */ + if (size != ((size + 7) & ~7)) { + ntfs_log_error("make_room_for_index_entry_in_index_block() received " + "non 8-byte aligned size.\n"); + return -EINVAL; + } + if (!idx || !pos) + return -EINVAL; + if ((char*)pos < (char*)idx || (char*)pos + size < (char*)idx || + (char*)pos > (char*)idx + sizeof(INDEX_BLOCK) - + sizeof(INDEX_HEADER) + + le32_to_cpu(idx->index.allocated_size) || + (char*)pos + size > (char*)idx + sizeof(INDEX_BLOCK) - + sizeof(INDEX_HEADER) + + le32_to_cpu(idx->index.allocated_size)) + return -EINVAL; + /* The - sizeof(INDEX_ENTRY_HEADER) is for the index terminator. */ + if ((char*)pos - (char*)&idx->index > + (int)le32_to_cpu(idx->index.index_length) + - (int)sizeof(INDEX_ENTRY_HEADER)) + return -EINVAL; +#endif + biu = le32_to_cpu(idx->index.index_length); + /* Do we have enough space? */ + if (biu + size > le32_to_cpu(idx->index.allocated_size)) + return -ENOSPC; + /* Move everything after pos to pos + size. */ + memmove((char*)pos + size, (char*)pos, biu - ((char*)pos - + (char*)&idx->index)); + /* Update index block. */ + idx->index.index_length = cpu_to_le32(biu + size); + return 0; +} + +/** + * ntfs_index_keys_compare + * + * not all types of COLLATION_RULES supported yet... + * added as needed.. (remove this comment when all are added) + */ +static int ntfs_index_keys_compare(u8 *key1, u8 *key2, int key1_length, + int key2_length, COLLATION_RULES collation_rule) +{ + u32 u1, u2; + int i; + + if (collation_rule == COLLATION_NTOFS_ULONG) { + /* i.e. $SII or $QUOTA-$Q */ + u1 = le32_to_cpup((const le32*)key1); + u2 = le32_to_cpup((const le32*)key2); + if (u1 < u2) + return -1; + if (u1 > u2) + return 1; + /* u1 == u2 */ + return 0; + } + if (collation_rule == COLLATION_NTOFS_ULONGS) { + /* i.e $OBJID-$O */ + i = 0; + while (i < min(key1_length, key2_length)) { + u1 = le32_to_cpup((const le32*)(key1 + i)); + u2 = le32_to_cpup((const le32*)(key2 + i)); + if (u1 < u2) + return -1; + if (u1 > u2) + return 1; + /* u1 == u2 */ + i += sizeof(u32); + } + if (key1_length < key2_length) + return -1; + if (key1_length > key2_length) + return 1; + return 0; + } + if (collation_rule == COLLATION_NTOFS_SECURITY_HASH) { + /* i.e. $SDH */ + u1 = le32_to_cpu(((SDH_INDEX_KEY*)key1)->hash); + u2 = le32_to_cpu(((SDH_INDEX_KEY*)key2)->hash); + if (u1 < u2) + return -1; + if (u1 > u2) + return 1; + /* u1 == u2 */ + u1 = le32_to_cpu(((SDH_INDEX_KEY*)key1)->security_id); + u2 = le32_to_cpu(((SDH_INDEX_KEY*)key2)->security_id); + if (u1 < u2) + return -1; + if (u1 > u2) + return 1; + return 0; + } + if (collation_rule == COLLATION_NTOFS_SID) { + /* i.e. $QUOTA-O */ + i = memcmp(key1, key2, min(key1_length, key2_length)); + if (!i) { + if (key1_length < key2_length) + return -1; + if (key1_length > key2_length) + return 1; + } + return i; + } + ntfs_log_critical("ntfs_index_keys_compare called without supported " + "collation rule.\n"); + return 0; /* Claim they're equal. What else can we do? */ +} + +/** + * insert_index_entry_in_res_dir_index + * + * i.e. insert an index_entry in some named index_root + * simplified search method, works for mkntfs + */ +static int insert_index_entry_in_res_dir_index(INDEX_ENTRY *idx, u32 idx_size, + MFT_RECORD *m, ntfschar *name, u32 name_size, ATTR_TYPES type) +{ + ntfs_attr_search_ctx *ctx; + INDEX_HEADER *idx_header; + INDEX_ENTRY *idx_entry, *idx_end; + ATTR_RECORD *a; + COLLATION_RULES collation_rule; + int err, i; + + err = 0; + /* does it fit ?*/ + if (g_vol->mft_record_size > idx_size + le32_to_cpu(m->bytes_allocated)) + return -ENOSPC; + /* find the INDEX_ROOT attribute:*/ + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_error("Failed to allocate attribute search " + "context.\n"); + err = -ENOMEM; + goto err_out; + } + if (mkntfs_attr_lookup(AT_INDEX_ROOT, name, name_size, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + err = -EEXIST; + goto err_out; + } + /* found attribute */ + a = (ATTR_RECORD*)ctx->attr; + collation_rule = ((INDEX_ROOT*)((u8*)a + + le16_to_cpu(a->value_offset)))->collation_rule; + idx_header = (INDEX_HEADER*)((u8*)a + le16_to_cpu(a->value_offset) + + 0x10); + idx_entry = (INDEX_ENTRY*)((u8*)idx_header + + le32_to_cpu(idx_header->entries_offset)); + idx_end = (INDEX_ENTRY*)((u8*)idx_entry + + le32_to_cpu(idx_header->index_length)); + /* + * Loop until we exceed valid memory (corruption case) or until we + * reach the last entry. + */ + if (type == AT_FILE_NAME) { + while (((u8*)idx_entry < (u8*)idx_end) && + !(idx_entry->ie_flags & INDEX_ENTRY_END)) { + /* + i = ntfs_file_values_compare(&idx->key.file_name, + &idx_entry->key.file_name, 1, + IGNORE_CASE, g_vol->upcase, + g_vol->upcase_len); + */ + i = ntfs_names_full_collate(idx->key.file_name.file_name, idx->key.file_name.file_name_length, + idx_entry->key.file_name.file_name, idx_entry->key.file_name.file_name_length, + IGNORE_CASE, g_vol->upcase, + g_vol->upcase_len); + /* + * If @file_name collates before ie->key.file_name, + * there is no matching index entry. + */ + if (i == -1) + break; + /* If file names are not equal, continue search. */ + if (i) + goto do_next; + if (idx->key.file_name.file_name_type != + FILE_NAME_POSIX || + idx_entry->key.file_name.file_name_type + != FILE_NAME_POSIX) + return -EEXIST; + /* + i = ntfs_file_values_compare(&idx->key.file_name, + &idx_entry->key.file_name, 1, + CASE_SENSITIVE, g_vol->upcase, + g_vol->upcase_len); + */ + i = ntfs_names_full_collate(idx->key.file_name.file_name, idx->key.file_name.file_name_length, + idx_entry->key.file_name.file_name, idx_entry->key.file_name.file_name_length, + CASE_SENSITIVE, g_vol->upcase, + g_vol->upcase_len); + if (!i) + return -EEXIST; + if (i == -1) + break; +do_next: + idx_entry = (INDEX_ENTRY*)((u8*)idx_entry + + le16_to_cpu(idx_entry->length)); + } + } else if (type == AT_UNUSED) { /* case view */ + while (((u8*)idx_entry < (u8*)idx_end) && + !(idx_entry->ie_flags & INDEX_ENTRY_END)) { + i = ntfs_index_keys_compare((u8*)idx + 0x10, + (u8*)idx_entry + 0x10, + le16_to_cpu(idx->key_length), + le16_to_cpu(idx_entry->key_length), + collation_rule); + if (!i) + return -EEXIST; + if (i == -1) + break; + idx_entry = (INDEX_ENTRY*)((u8*)idx_entry + + le16_to_cpu(idx_entry->length)); + } + } else + return -EINVAL; + memmove((u8*)idx_entry + idx_size, (u8*)idx_entry, + le32_to_cpu(m->bytes_in_use) - + ((u8*)idx_entry - (u8*)m)); + memcpy((u8*)idx_entry, (u8*)idx, idx_size); + /* Adjust various offsets, etc... */ + m->bytes_in_use = cpu_to_le32(le32_to_cpu(m->bytes_in_use) + idx_size); + a->length = cpu_to_le32(le32_to_cpu(a->length) + idx_size); + a->value_length = cpu_to_le32(le32_to_cpu(a->value_length) + idx_size); + idx_header->index_length = cpu_to_le32( + le32_to_cpu(idx_header->index_length) + idx_size); + idx_header->allocated_size = cpu_to_le32( + le32_to_cpu(idx_header->allocated_size) + idx_size); +err_out: + if (ctx) + ntfs_attr_put_search_ctx(ctx); + return err; +} + +/** + * initialize_secure + * + * initializes $Secure's $SDH and $SII indexes from $SDS datastream + */ +static int initialize_secure(char *sds, u32 sds_size, MFT_RECORD *m) +{ + int err, sdh_size, sii_size; + SECURITY_DESCRIPTOR_HEADER *sds_header; + INDEX_ENTRY *idx_entry_sdh, *idx_entry_sii; + SDH_INDEX_DATA *sdh_data; + SII_INDEX_DATA *sii_data; + + sds_header = (SECURITY_DESCRIPTOR_HEADER*)sds; + sdh_size = sizeof(INDEX_ENTRY_HEADER); + sdh_size += sizeof(SDH_INDEX_KEY) + sizeof(SDH_INDEX_DATA); + sii_size = sizeof(INDEX_ENTRY_HEADER); + sii_size += sizeof(SII_INDEX_KEY) + sizeof(SII_INDEX_DATA); + idx_entry_sdh = ntfs_calloc(sizeof(INDEX_ENTRY)); + if (!idx_entry_sdh) + return -errno; + idx_entry_sii = ntfs_calloc(sizeof(INDEX_ENTRY)); + if (!idx_entry_sii) { + free(idx_entry_sdh); + return -errno; + } + err = 0; + + while ((char*)sds_header < (char*)sds + sds_size) { + if (!sds_header->length) + break; + /* SDH index entry */ + idx_entry_sdh->data_offset = const_cpu_to_le16(0x18); + idx_entry_sdh->data_length = const_cpu_to_le16(0x14); + idx_entry_sdh->reservedV = const_cpu_to_le32(0x00); + idx_entry_sdh->length = const_cpu_to_le16(0x30); + idx_entry_sdh->key_length = const_cpu_to_le16(0x08); + idx_entry_sdh->ie_flags = const_cpu_to_le16(0x00); + idx_entry_sdh->reserved = const_cpu_to_le16(0x00); + idx_entry_sdh->key.sdh.hash = sds_header->hash; + idx_entry_sdh->key.sdh.security_id = sds_header->security_id; + sdh_data = (SDH_INDEX_DATA*)((u8*)idx_entry_sdh + + le16_to_cpu(idx_entry_sdh->data_offset)); + sdh_data->hash = sds_header->hash; + sdh_data->security_id = sds_header->security_id; + sdh_data->offset = sds_header->offset; + sdh_data->length = sds_header->length; + sdh_data->reserved_II = const_cpu_to_le32(0x00490049); + + /* SII index entry */ + idx_entry_sii->data_offset = const_cpu_to_le16(0x14); + idx_entry_sii->data_length = const_cpu_to_le16(0x14); + idx_entry_sii->reservedV = const_cpu_to_le32(0x00); + idx_entry_sii->length = const_cpu_to_le16(0x28); + idx_entry_sii->key_length = const_cpu_to_le16(0x04); + idx_entry_sii->ie_flags = const_cpu_to_le16(0x00); + idx_entry_sii->reserved = const_cpu_to_le16(0x00); + idx_entry_sii->key.sii.security_id = sds_header->security_id; + sii_data = (SII_INDEX_DATA*)((u8*)idx_entry_sii + + le16_to_cpu(idx_entry_sii->data_offset)); + sii_data->hash = sds_header->hash; + sii_data->security_id = sds_header->security_id; + sii_data->offset = sds_header->offset; + sii_data->length = sds_header->length; + if ((err = insert_index_entry_in_res_dir_index(idx_entry_sdh, + sdh_size, m, NTFS_INDEX_SDH, 4, AT_UNUSED))) + break; + if ((err = insert_index_entry_in_res_dir_index(idx_entry_sii, + sii_size, m, NTFS_INDEX_SII, 4, AT_UNUSED))) + break; + sds_header = (SECURITY_DESCRIPTOR_HEADER*)((u8*)sds_header + + ((le32_to_cpu(sds_header->length) + 15) & ~15)); + } + free(idx_entry_sdh); + free(idx_entry_sii); + return err; +} + +/** + * initialize_quota + * + * initialize $Quota with the default quota index-entries. + */ +static int initialize_quota(MFT_RECORD *m) +{ + int o_size, q1_size, q2_size, err, i; + INDEX_ENTRY *idx_entry_o, *idx_entry_q1, *idx_entry_q2; + QUOTA_O_INDEX_DATA *idx_entry_o_data; + QUOTA_CONTROL_ENTRY *idx_entry_q1_data, *idx_entry_q2_data; + + err = 0; + /* q index entry num 1 */ + q1_size = 0x48; + idx_entry_q1 = ntfs_calloc(q1_size); + if (!idx_entry_q1) + return errno; + idx_entry_q1->data_offset = const_cpu_to_le16(0x14); + idx_entry_q1->data_length = const_cpu_to_le16(0x30); + idx_entry_q1->reservedV = const_cpu_to_le32(0x00); + idx_entry_q1->length = const_cpu_to_le16(0x48); + idx_entry_q1->key_length = const_cpu_to_le16(0x04); + idx_entry_q1->ie_flags = const_cpu_to_le16(0x00); + idx_entry_q1->reserved = const_cpu_to_le16(0x00); + idx_entry_q1->key.owner_id = const_cpu_to_le32(0x01); + idx_entry_q1_data = (QUOTA_CONTROL_ENTRY*)((char*)idx_entry_q1 + + le16_to_cpu(idx_entry_q1->data_offset)); + idx_entry_q1_data->version = const_cpu_to_le32(0x02); + idx_entry_q1_data->flags = QUOTA_FLAG_DEFAULT_LIMITS; + idx_entry_q1_data->bytes_used = const_cpu_to_le64(0x00); + idx_entry_q1_data->change_time = mkntfs_time(); + idx_entry_q1_data->threshold = const_cpu_to_sle64(-1); + idx_entry_q1_data->limit = const_cpu_to_sle64(-1); + idx_entry_q1_data->exceeded_time = const_cpu_to_sle64(0); + err = insert_index_entry_in_res_dir_index(idx_entry_q1, q1_size, m, + NTFS_INDEX_Q, 2, AT_UNUSED); + free(idx_entry_q1); + if (err) + return err; + /* q index entry num 2 */ + q2_size = 0x58; + idx_entry_q2 = ntfs_calloc(q2_size); + if (!idx_entry_q2) + return errno; + idx_entry_q2->data_offset = const_cpu_to_le16(0x14); + idx_entry_q2->data_length = const_cpu_to_le16(0x40); + idx_entry_q2->reservedV = const_cpu_to_le32(0x00); + idx_entry_q2->length = const_cpu_to_le16(0x58); + idx_entry_q2->key_length = const_cpu_to_le16(0x04); + idx_entry_q2->ie_flags = const_cpu_to_le16(0x00); + idx_entry_q2->reserved = const_cpu_to_le16(0x00); + idx_entry_q2->key.owner_id = QUOTA_FIRST_USER_ID; + idx_entry_q2_data = (QUOTA_CONTROL_ENTRY*)((char*)idx_entry_q2 + + le16_to_cpu(idx_entry_q2->data_offset)); + idx_entry_q2_data->version = const_cpu_to_le32(0x02); + idx_entry_q2_data->flags = QUOTA_FLAG_DEFAULT_LIMITS; + idx_entry_q2_data->bytes_used = const_cpu_to_le64(0x00); + idx_entry_q2_data->change_time = mkntfs_time(); + idx_entry_q2_data->threshold = const_cpu_to_sle64(-1); + idx_entry_q2_data->limit = const_cpu_to_sle64(-1); + idx_entry_q2_data->exceeded_time = const_cpu_to_sle64(0); + idx_entry_q2_data->sid.revision = 1; + idx_entry_q2_data->sid.sub_authority_count = 2; + for (i = 0; i < 5; i++) + idx_entry_q2_data->sid.identifier_authority.value[i] = 0; + idx_entry_q2_data->sid.identifier_authority.value[5] = 0x05; + idx_entry_q2_data->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + idx_entry_q2_data->sid.sub_authority[1] = + const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + err = insert_index_entry_in_res_dir_index(idx_entry_q2, q2_size, m, + NTFS_INDEX_Q, 2, AT_UNUSED); + free(idx_entry_q2); + if (err) + return err; + o_size = 0x28; + idx_entry_o = ntfs_calloc(o_size); + if (!idx_entry_o) + return errno; + idx_entry_o->data_offset = const_cpu_to_le16(0x20); + idx_entry_o->data_length = const_cpu_to_le16(0x04); + idx_entry_o->reservedV = const_cpu_to_le32(0x00); + idx_entry_o->length = const_cpu_to_le16(0x28); + idx_entry_o->key_length = const_cpu_to_le16(0x10); + idx_entry_o->ie_flags = const_cpu_to_le16(0x00); + idx_entry_o->reserved = const_cpu_to_le16(0x00); + idx_entry_o->key.sid.revision = 0x01; + idx_entry_o->key.sid.sub_authority_count = 0x02; + for (i = 0; i < 5; i++) + idx_entry_o->key.sid.identifier_authority.value[i] = 0; + idx_entry_o->key.sid.identifier_authority.value[5] = 0x05; + idx_entry_o->key.sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + idx_entry_o->key.sid.sub_authority[1] = + const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + idx_entry_o_data = (QUOTA_O_INDEX_DATA*)((char*)idx_entry_o + + le16_to_cpu(idx_entry_o->data_offset)); + idx_entry_o_data->owner_id = QUOTA_FIRST_USER_ID; + /* 20 00 00 00 padding after here on ntfs 3.1. 3.0 is unchecked. */ + idx_entry_o_data->unknown = const_cpu_to_le32(32); + err = insert_index_entry_in_res_dir_index(idx_entry_o, o_size, m, + NTFS_INDEX_O, 2, AT_UNUSED); + free(idx_entry_o); + + return err; +} + +/** + * insert_file_link_in_dir_index + * + * Insert the fully completed FILE_NAME_ATTR @file_name which is inside + * the file with mft reference @file_ref into the index (allocation) block + * @idx (which belongs to @file_ref's parent directory). + * + * Return 0 on success or -errno on error. + */ +static int insert_file_link_in_dir_index(INDEX_BLOCK *idx, leMFT_REF file_ref, + FILE_NAME_ATTR *file_name, u32 file_name_size) +{ + int err, i; + INDEX_ENTRY *ie; + char *index_end; + + /* + * Lookup dir entry @file_name in dir @idx to determine correct + * insertion location. FIXME: Using a very oversimplified lookup + * method which is sufficient for mkntfs but no good whatsoever in + * real world scenario. (AIA) + */ + + index_end = (char*)&idx->index + le32_to_cpu(idx->index.index_length); + ie = (INDEX_ENTRY*)((char*)&idx->index + + le32_to_cpu(idx->index.entries_offset)); + /* + * Loop until we exceed valid memory (corruption case) or until we + * reach the last entry. + */ + while ((char*)ie < index_end && !(ie->ie_flags & INDEX_ENTRY_END)) { +#if 0 +#ifdef DEBUG + ntfs_log_debug("file_name_attr1->file_name_length = %i\n", + file_name->file_name_length); + if (file_name->file_name_length) { + char *__buf = NULL; + i = ntfs_ucstombs((ntfschar*)&file_name->file_name, + file_name->file_name_length, &__buf, 0); + if (i < 0) + ntfs_log_debug("Name contains non-displayable " + "Unicode characters.\n"); + ntfs_log_debug("file_name_attr1->file_name = %s\n", + __buf); + free(__buf); + } + ntfs_log_debug("file_name_attr2->file_name_length = %i\n", + ie->key.file_name.file_name_length); + if (ie->key.file_name.file_name_length) { + char *__buf = NULL; + i = ntfs_ucstombs(ie->key.file_name.file_name, + ie->key.file_name.file_name_length + 1, &__buf, + 0); + if (i < 0) + ntfs_log_debug("Name contains non-displayable " + "Unicode characters.\n"); + ntfs_log_debug("file_name_attr2->file_name = %s\n", + __buf); + free(__buf); + } +#endif +#endif + /* + i = ntfs_file_values_compare(file_name, + (FILE_NAME_ATTR*)&ie->key.file_name, 1, + IGNORE_CASE, g_vol->upcase, g_vol->upcase_len); + */ + i = ntfs_names_full_collate(file_name->file_name, file_name->file_name_length, + ((FILE_NAME_ATTR*)&ie->key.file_name)->file_name, ((FILE_NAME_ATTR*)&ie->key.file_name)->file_name_length, + IGNORE_CASE, g_vol->upcase, g_vol->upcase_len); + /* + * If @file_name collates before ie->key.file_name, there is no + * matching index entry. + */ + if (i == -1) + break; + /* If file names are not equal, continue search. */ + if (i) + goto do_next; + /* File names are equal when compared ignoring case. */ + /* + * If BOTH file names are in the POSIX namespace, do a case + * sensitive comparison as well. Otherwise the names match so + * we return -EEXIST. FIXME: There are problems with this in a + * real world scenario, when one is POSIX and one isn't, but + * fine for mkntfs where we don't use POSIX namespace at all + * and hence this following code is luxury. (AIA) + */ + if (file_name->file_name_type != FILE_NAME_POSIX || + ie->key.file_name.file_name_type != FILE_NAME_POSIX) + return -EEXIST; + /* + i = ntfs_file_values_compare(file_name, + (FILE_NAME_ATTR*)&ie->key.file_name, 1, + CASE_SENSITIVE, g_vol->upcase, + g_vol->upcase_len); + */ + i = ntfs_names_full_collate(file_name->file_name, file_name->file_name_length, + ((FILE_NAME_ATTR*)&ie->key.file_name)->file_name, ((FILE_NAME_ATTR*)&ie->key.file_name)->file_name_length, + CASE_SENSITIVE, g_vol->upcase, g_vol->upcase_len); + if (i == -1) + break; + /* Complete match. Bugger. Can't insert. */ + if (!i) + return -EEXIST; +do_next: +#ifdef DEBUG + /* Next entry. */ + if (!ie->length) { + ntfs_log_debug("BUG: ie->length is zero, breaking out " + "of loop.\n"); + break; + } +#endif + ie = (INDEX_ENTRY*)((char*)ie + le16_to_cpu(ie->length)); + }; + i = (sizeof(INDEX_ENTRY_HEADER) + file_name_size + 7) & ~7; + err = make_room_for_index_entry_in_index_block(idx, ie, i); + if (err) { + ntfs_log_error("make_room_for_index_entry_in_index_block " + "failed: %s\n", strerror(-err)); + return err; + } + /* Create entry in place and copy file name attribute value. */ + ie->indexed_file = file_ref; + ie->length = cpu_to_le16(i); + ie->key_length = cpu_to_le16(file_name_size); + ie->ie_flags = const_cpu_to_le16(0); + ie->reserved = const_cpu_to_le16(0); + memcpy((char*)&ie->key.file_name, (char*)file_name, file_name_size); + return 0; +} + +/** + * create_hardlink_res + * + * Create a file_name_attribute in the mft record @m_file which points to the + * parent directory with mft reference @ref_parent. + * + * Then, insert an index entry with this file_name_attribute in the index + * root @idx of the index_root attribute of the parent directory. + * + * @ref_file is the mft reference of @m_file. + * + * Return 0 on success or -errno on error. + */ +static int create_hardlink_res(MFT_RECORD *m_parent, const leMFT_REF ref_parent, + MFT_RECORD *m_file, const leMFT_REF ref_file, + const s64 allocated_size, const s64 data_size, + const FILE_ATTR_FLAGS flags, const u16 packed_ea_size, + const u32 reparse_point_tag, const char *file_name, + const FILE_NAME_TYPE_FLAGS file_name_type) +{ + FILE_NAME_ATTR *fn; + int i, fn_size, idx_size; + INDEX_ENTRY *idx_entry_new; + ntfschar *uname; + + /* Create the file_name attribute. */ + i = (strlen(file_name) + 1) * sizeof(ntfschar); + fn_size = sizeof(FILE_NAME_ATTR) + i; + fn = ntfs_malloc(fn_size); + if (!fn) + return -errno; + fn->parent_directory = ref_parent; + fn->creation_time = stdinfo_time(m_file); + fn->last_data_change_time = fn->creation_time; + fn->last_mft_change_time = fn->creation_time; + fn->last_access_time = fn->creation_time; + fn->allocated_size = cpu_to_sle64(allocated_size); + fn->data_size = cpu_to_sle64(data_size); + fn->file_attributes = flags; + /* These are in a union so can't have both. */ + if (packed_ea_size && reparse_point_tag) { + free(fn); + return -EINVAL; + } + if (packed_ea_size) { + free(fn); + return -EINVAL; + } + if (packed_ea_size) { + fn->packed_ea_size = cpu_to_le16(packed_ea_size); + fn->reserved = const_cpu_to_le16(0); + } else { + fn->reparse_point_tag = cpu_to_le32(reparse_point_tag); + } + fn->file_name_type = file_name_type; + uname = fn->file_name; + i = ntfs_mbstoucs_libntfscompat(file_name, &uname, i); + if (i < 1) { + free(fn); + return -EINVAL; + } + if (i > 0xff) { + free(fn); + return -ENAMETOOLONG; + } + /* No terminating null in file names. */ + fn->file_name_length = i; + fn_size = sizeof(FILE_NAME_ATTR) + i * sizeof(ntfschar); + /* Increment the link count of @m_file. */ + i = le16_to_cpu(m_file->link_count); + if (i == 0xffff) { + ntfs_log_error("Too many hardlinks present already.\n"); + free(fn); + return -EINVAL; + } + m_file->link_count = cpu_to_le16(i + 1); + /* Add the file_name to @m_file. */ + i = insert_resident_attr_in_mft_record(m_file, AT_FILE_NAME, NULL, 0, + CASE_SENSITIVE, const_cpu_to_le16(0), + RESIDENT_ATTR_IS_INDEXED, (u8*)fn, fn_size); + if (i < 0) { + ntfs_log_error("create_hardlink failed adding file name " + "attribute: %s\n", strerror(-i)); + free(fn); + /* Undo link count increment. */ + m_file->link_count = cpu_to_le16( + le16_to_cpu(m_file->link_count) - 1); + return i; + } + /* Insert the index entry for file_name in @idx. */ + idx_size = (fn_size + 7) & ~7; + idx_entry_new = ntfs_calloc(idx_size + 0x10); + if (!idx_entry_new) + return -errno; + idx_entry_new->indexed_file = ref_file; + idx_entry_new->length = cpu_to_le16(idx_size + 0x10); + idx_entry_new->key_length = cpu_to_le16(fn_size); + memcpy((u8*)idx_entry_new + 0x10, (u8*)fn, fn_size); + i = insert_index_entry_in_res_dir_index(idx_entry_new, idx_size + 0x10, + m_parent, NTFS_INDEX_I30, 4, AT_FILE_NAME); + if (i < 0) { + ntfs_log_error("create_hardlink failed inserting index entry: " + "%s\n", strerror(-i)); + /* FIXME: Remove the file name attribute from @m_file. */ + free(idx_entry_new); + free(fn); + /* Undo link count increment. */ + m_file->link_count = cpu_to_le16( + le16_to_cpu(m_file->link_count) - 1); + return i; + } + free(idx_entry_new); + free(fn); + return 0; +} + +/** + * create_hardlink + * + * Create a file_name_attribute in the mft record @m_file which points to the + * parent directory with mft reference @ref_parent. + * + * Then, insert an index entry with this file_name_attribute in the index + * block @idx of the index allocation attribute of the parent directory. + * + * @ref_file is the mft reference of @m_file. + * + * Return 0 on success or -errno on error. + */ +static int create_hardlink(INDEX_BLOCK *idx, const leMFT_REF ref_parent, + MFT_RECORD *m_file, const leMFT_REF ref_file, + const s64 allocated_size, const s64 data_size, + const FILE_ATTR_FLAGS flags, const u16 packed_ea_size, + const u32 reparse_point_tag, const char *file_name, + const FILE_NAME_TYPE_FLAGS file_name_type) +{ + FILE_NAME_ATTR *fn; + int i, fn_size; + ntfschar *uname; + + /* Create the file_name attribute. */ + i = (strlen(file_name) + 1) * sizeof(ntfschar); + fn_size = sizeof(FILE_NAME_ATTR) + i; + fn = ntfs_malloc(fn_size); + if (!fn) + return -errno; + fn->parent_directory = ref_parent; + fn->creation_time = stdinfo_time(m_file); + fn->last_data_change_time = fn->creation_time; + fn->last_mft_change_time = fn->creation_time; + fn->last_access_time = fn->creation_time; + /* allocated size depends on unnamed data being resident */ + if (allocated_size && non_resident_unnamed_data(m_file)) + fn->allocated_size = cpu_to_sle64(allocated_size); + else + fn->allocated_size = cpu_to_sle64((data_size + 7) & -8); + fn->data_size = cpu_to_sle64(data_size); + fn->file_attributes = flags; + /* These are in a union so can't have both. */ + if (packed_ea_size && reparse_point_tag) { + free(fn); + return -EINVAL; + } + if (packed_ea_size) { + fn->packed_ea_size = cpu_to_le16(packed_ea_size); + fn->reserved = const_cpu_to_le16(0); + } else { + fn->reparse_point_tag = cpu_to_le32(reparse_point_tag); + } + fn->file_name_type = file_name_type; + uname = fn->file_name; + i = ntfs_mbstoucs_libntfscompat(file_name, &uname, i); + if (i < 1) { + free(fn); + return -EINVAL; + } + if (i > 0xff) { + free(fn); + return -ENAMETOOLONG; + } + /* No terminating null in file names. */ + fn->file_name_length = i; + fn_size = sizeof(FILE_NAME_ATTR) + i * sizeof(ntfschar); + /* Increment the link count of @m_file. */ + i = le16_to_cpu(m_file->link_count); + if (i == 0xffff) { + ntfs_log_error("Too many hardlinks present already.\n"); + free(fn); + return -EINVAL; + } + m_file->link_count = cpu_to_le16(i + 1); + /* Add the file_name to @m_file. */ + i = insert_resident_attr_in_mft_record(m_file, AT_FILE_NAME, NULL, 0, + CASE_SENSITIVE, const_cpu_to_le16(0), + RESIDENT_ATTR_IS_INDEXED, (u8*)fn, fn_size); + if (i < 0) { + ntfs_log_error("create_hardlink failed adding file name attribute: " + "%s\n", strerror(-i)); + free(fn); + /* Undo link count increment. */ + m_file->link_count = cpu_to_le16( + le16_to_cpu(m_file->link_count) - 1); + return i; + } + /* Insert the index entry for file_name in @idx. */ + i = insert_file_link_in_dir_index(idx, ref_file, fn, fn_size); + if (i < 0) { + ntfs_log_error("create_hardlink failed inserting index entry: %s\n", + strerror(-i)); + /* FIXME: Remove the file name attribute from @m_file. */ + free(fn); + /* Undo link count increment. */ + m_file->link_count = cpu_to_le16( + le16_to_cpu(m_file->link_count) - 1); + return i; + } + free(fn); + return 0; +} + +/** + * index_obj_id_insert + * + * Insert an index entry with the key @guid and data pointing to the mft record + * @ref in the $O index root of the mft record @m (which must be the mft record + * for $ObjId). + * + * Return 0 on success or -errno on error. + */ +static int index_obj_id_insert(MFT_RECORD *m, const GUID *guid, + const leMFT_REF ref) +{ + INDEX_ENTRY *idx_entry_new; + int data_ofs, idx_size, err; + OBJ_ID_INDEX_DATA *oi; + + /* + * Insert the index entry for the object id in the index. + * + * First determine the size of the index entry to be inserted. This + * consists of the index entry header, followed by the index key, i.e. + * the GUID, followed by the index data, i.e. OBJ_ID_INDEX_DATA. + */ + data_ofs = (sizeof(INDEX_ENTRY_HEADER) + sizeof(GUID) + 7) & ~7; + idx_size = (data_ofs + sizeof(OBJ_ID_INDEX_DATA) + 7) & ~7; + idx_entry_new = ntfs_calloc(idx_size); + if (!idx_entry_new) + return -errno; + idx_entry_new->data_offset = cpu_to_le16(data_ofs); + idx_entry_new->data_length = + const_cpu_to_le16(sizeof(OBJ_ID_INDEX_DATA)); + idx_entry_new->length = cpu_to_le16(idx_size); + idx_entry_new->key_length = const_cpu_to_le16(sizeof(GUID)); + idx_entry_new->key.object_id = *guid; + oi = (OBJ_ID_INDEX_DATA*)((u8*)idx_entry_new + data_ofs); + oi->mft_reference = ref; + err = insert_index_entry_in_res_dir_index(idx_entry_new, idx_size, m, + NTFS_INDEX_O, 2, AT_UNUSED); + free(idx_entry_new); + if (err < 0) { + ntfs_log_error("index_obj_id_insert failed inserting index " + "entry: %s\n", strerror(-err)); + return err; + } + return 0; +} + +/** + * mkntfs_cleanup + */ +static void mkntfs_cleanup(void) +{ + struct BITMAP_ALLOCATION *p, *q; + + /* Close the volume */ + if (g_vol) { + if (g_vol->dev) { + if (NDevOpen(g_vol->dev) && g_vol->dev->d_ops->close(g_vol->dev)) + ntfs_log_perror("Warning: Could not close %s", g_vol->dev->d_name); + ntfs_device_free(g_vol->dev); + } + free(g_vol->vol_name); + free(g_vol->attrdef); + free(g_vol->upcase); + free(g_vol); + g_vol = NULL; + } + + /* Free any memory we've used */ + free(g_bad_blocks); g_bad_blocks = NULL; + free(g_buf); g_buf = NULL; + free(g_index_block); g_index_block = NULL; + free(g_dynamic_buf); g_dynamic_buf = NULL; + free(g_mft_bitmap); g_mft_bitmap = NULL; + free(g_rl_bad); g_rl_bad = NULL; + free(g_rl_boot); g_rl_boot = NULL; + free(g_rl_logfile); g_rl_logfile = NULL; + free(g_rl_mft); g_rl_mft = NULL; + free(g_rl_mft_bmp); g_rl_mft_bmp = NULL; + free(g_rl_mftmirr); g_rl_mftmirr = NULL; + + p = g_allocation; + while (p) { + q = p->next; + free(p); + p = q; + } +} + + +/** + * mkntfs_open_partition - + */ +static BOOL mkntfs_open_partition(ntfs_volume *vol) +{ + BOOL result = FALSE; + int i; + struct stat sbuf; + unsigned long mnt_flags; + + /* + * Allocate and initialize an ntfs device structure and attach it to + * the volume. + */ + vol->dev = ntfs_device_alloc(opts.dev_name, 0, &ntfs_device_default_io_ops, NULL); + if (!vol->dev) { + ntfs_log_perror("Could not create device"); + goto done; + } + + /* Open the device for reading or reading and writing. */ + if (opts.no_action) { + ntfs_log_quiet("Running in READ-ONLY mode!\n"); + i = O_RDONLY; + } else { + i = O_RDWR; + } + if (vol->dev->d_ops->open(vol->dev, i)) { + if (errno == ENOENT) + ntfs_log_error("The device doesn't exist; did you specify it correctly?\n"); + else + ntfs_log_perror("Could not open %s", vol->dev->d_name); + goto done; + } + /* Verify we are dealing with a block device. */ + if (vol->dev->d_ops->stat(vol->dev, &sbuf)) { + ntfs_log_perror("Error getting information about %s", vol->dev->d_name); + goto done; + } + + if (!S_ISBLK(sbuf.st_mode)) { + ntfs_log_error("%s is not a block device.\n", vol->dev->d_name); + if (!opts.force) { + ntfs_log_error("Refusing to make a filesystem here!\n"); + goto done; + } + if (!opts.num_sectors) { + if (!sbuf.st_size && !sbuf.st_blocks) { + ntfs_log_error("You must specify the number of sectors.\n"); + goto done; + } + if (opts.sector_size) { + if (sbuf.st_size) + opts.num_sectors = sbuf.st_size / opts.sector_size; + else + opts.num_sectors = ((s64)sbuf.st_blocks << 9) / opts.sector_size; + } else { + if (sbuf.st_size) + opts.num_sectors = sbuf.st_size / 512; + else + opts.num_sectors = sbuf.st_blocks; + opts.sector_size = 512; + } + } + ntfs_log_warning("mkntfs forced anyway.\n"); +#ifdef HAVE_LINUX_MAJOR_H + } else if ((IDE_DISK_MAJOR(MAJOR(sbuf.st_rdev)) && + MINOR(sbuf.st_rdev) % 64 == 0) || + (SCSI_DISK_MAJOR(MAJOR(sbuf.st_rdev)) && + MINOR(sbuf.st_rdev) % 16 == 0)) { + ntfs_log_error("%s is entire device, not just one partition.\n", vol->dev->d_name); + if (!opts.force) { + ntfs_log_error("Refusing to make a filesystem here!\n"); + goto done; + } + ntfs_log_warning("mkntfs forced anyway.\n"); +#endif + } + /* Make sure the file system is not mounted. */ + if (ntfs_check_if_mounted(vol->dev->d_name, &mnt_flags)) { + ntfs_log_perror("Failed to determine whether %s is mounted", vol->dev->d_name); + } else if (mnt_flags & NTFS_MF_MOUNTED) { + ntfs_log_error("%s is mounted.\n", vol->dev->d_name); + if (!opts.force) { + ntfs_log_error("Refusing to make a filesystem here!\n"); + goto done; + } + ntfs_log_warning("mkntfs forced anyway. Hope /etc/mtab is incorrect.\n"); + } + result = TRUE; +done: + return result; +} + +/** + * mkntfs_get_page_size - detect the system's memory page size. + */ +static long mkntfs_get_page_size(void) +{ + long page_size; +#ifdef _SC_PAGESIZE + page_size = sysconf(_SC_PAGESIZE); + if (page_size < 0) +#endif + { + ntfs_log_warning("Failed to determine system page size. " + "Assuming safe default of 4096 bytes.\n"); + return 4096; + } + ntfs_log_debug("System page size is %li bytes.\n", page_size); + return page_size; +} + +/** + * mkntfs_override_vol_params - + */ +static BOOL mkntfs_override_vol_params(ntfs_volume *vol) +{ + s64 volume_size; + long page_size; + int i; + BOOL winboot = TRUE; + + /* If user didn't specify the sector size, determine it now. */ + if (opts.sector_size < 0) { + opts.sector_size = ntfs_device_sector_size_get(vol->dev); + if (opts.sector_size < 0) { + ntfs_log_warning("The sector size was not specified " + "for %s and it could not be obtained " + "automatically. It has been set to 512 " + "bytes.\n", vol->dev->d_name); + opts.sector_size = 512; + } + } + /* Validate sector size. */ + if ((opts.sector_size - 1) & opts.sector_size) { + ntfs_log_error("The sector size is invalid. It must be a " + "power of two, e.g. 512, 1024.\n"); + return FALSE; + } + if (opts.sector_size < 256 || opts.sector_size > 4096) { + ntfs_log_error("The sector size is invalid. The minimum size " + "is 256 bytes and the maximum is 4096 bytes.\n"); + return FALSE; + } + ntfs_log_debug("sector size = %ld bytes\n", opts.sector_size); + /* Now set the device block size to the sector size. */ + if (ntfs_device_block_size_set(vol->dev, opts.sector_size)) + ntfs_log_debug("Failed to set the device block size to the " + "sector size. This may cause problems when " + "creating the backup boot sector and also may " + "affect performance but should be harmless " + "otherwise. Error: %s\n", strerror(errno)); + /* If user didn't specify the number of sectors, determine it now. */ + if (opts.num_sectors < 0) { + opts.num_sectors = ntfs_device_size_get(vol->dev, + opts.sector_size); + if (opts.num_sectors <= 0) { + ntfs_log_error("Couldn't determine the size of %s. " + "Please specify the number of sectors " + "manually.\n", vol->dev->d_name); + return FALSE; + } + } + ntfs_log_debug("number of sectors = %lld (0x%llx)\n", opts.num_sectors, + opts.num_sectors); + /* + * Reserve the last sector for the backup boot sector unless the + * sector size is less than 512 bytes in which case reserve 512 bytes + * worth of sectors. + */ + i = 1; + if (opts.sector_size < 512) + i = 512 / opts.sector_size; + opts.num_sectors -= i; + /* If user didn't specify the partition start sector, determine it. */ + if (opts.part_start_sect < 0) { + opts.part_start_sect = ntfs_device_partition_start_sector_get( + vol->dev); + if (opts.part_start_sect < 0) { + ntfs_log_warning("The partition start sector was not " + "specified for %s and it could not be obtained " + "automatically. It has been set to 0.\n", + vol->dev->d_name); + opts.part_start_sect = 0; + winboot = FALSE; + } else if (opts.part_start_sect >> 32) { + ntfs_log_warning("The partition start sector was not " + "specified for %s and the automatically " + "determined value is too large (%lld). " + "It has been set to 0.\n", + vol->dev->d_name, + (long long)opts.part_start_sect); + opts.part_start_sect = 0; + winboot = FALSE; + } + } else if (opts.part_start_sect >> 32) { + ntfs_log_error("Invalid partition start sector. Maximum is " + "4294967295 (2^32-1).\n"); + return FALSE; + } + /* If user didn't specify the sectors per track, determine it now. */ + if (opts.sectors_per_track < 0) { + opts.sectors_per_track = ntfs_device_sectors_per_track_get( + vol->dev); + if (opts.sectors_per_track < 0) { + ntfs_log_warning("The number of sectors per track was " + "not specified for %s and it could not be " + "obtained automatically. It has been set to " + "0.\n", vol->dev->d_name); + opts.sectors_per_track = 0; + winboot = FALSE; + } else if (opts.sectors_per_track > 65535) { + ntfs_log_warning("The number of sectors per track was " + "not specified for %s and the automatically " + "determined value is too large. It has been " + "set to 0.\n", vol->dev->d_name); + opts.sectors_per_track = 0; + winboot = FALSE; + } + } else if (opts.sectors_per_track > 65535) { + ntfs_log_error("Invalid number of sectors per track. Maximum " + "is 65535.\n"); + return FALSE; + } + /* If user didn't specify the number of heads, determine it now. */ + if (opts.heads < 0) { + opts.heads = ntfs_device_heads_get(vol->dev); + if (opts.heads < 0) { + ntfs_log_warning("The number of heads was not " + "specified for %s and it could not be obtained " + "automatically. It has been set to 0.\n", + vol->dev->d_name); + opts.heads = 0; + winboot = FALSE; + } else if (opts.heads > 65535) { + ntfs_log_warning("The number of heads was not " + "specified for %s and the automatically " + "determined value is too large. It has been " + "set to 0.\n", vol->dev->d_name); + opts.heads = 0; + winboot = FALSE; + } + } else if (opts.heads > 65535) { + ntfs_log_error("Invalid number of heads. Maximum is 65535.\n"); + return FALSE; + } + volume_size = opts.num_sectors * opts.sector_size; + /* Validate volume size. */ + if (volume_size < (1 << 20)) { /* 1MiB */ + ntfs_log_error("Device is too small (%llikiB). Minimum NTFS " + "volume size is 1MiB.\n", + (long long)(volume_size / 1024)); + return FALSE; + } + ntfs_log_debug("volume size = %llikiB\n", + (long long)(volume_size / 1024)); + /* If user didn't specify the cluster size, determine it now. */ + if (!vol->cluster_size) { + /* + * Windows Vista always uses 4096 bytes as the default cluster + * size regardless of the volume size so we do it, too. + */ + vol->cluster_size = 4096; + /* For small volumes on devices with large sector sizes. */ + if (vol->cluster_size < (u32)opts.sector_size) + vol->cluster_size = opts.sector_size; + /* + * For huge volumes, grow the cluster size until the number of + * clusters fits into 32 bits or the cluster size exceeds the + * maximum limit of NTFS_MAX_CLUSTER_SIZE. + */ + while (volume_size >> (ffs(vol->cluster_size) - 1 + 32)) { + vol->cluster_size <<= 1; + if (vol->cluster_size >= NTFS_MAX_CLUSTER_SIZE) { + ntfs_log_error("Device is too large to hold an " + "NTFS volume (maximum size is " + "256TiB).\n"); + return FALSE; + } + } + ntfs_log_quiet("Cluster size has been automatically set to %u " + "bytes.\n", (unsigned)vol->cluster_size); + } + /* Validate cluster size. */ + if (vol->cluster_size & (vol->cluster_size - 1)) { + ntfs_log_error("The cluster size is invalid. It must be a " + "power of two, e.g. 1024, 4096.\n"); + return FALSE; + } + if (vol->cluster_size < (u32)opts.sector_size) { + ntfs_log_error("The cluster size is invalid. It must be equal " + "to, or larger than, the sector size.\n"); + return FALSE; + } + /* Before Windows 10 Creators, the limit was 128 */ + if (vol->cluster_size > 4096 * (u32)opts.sector_size) { + ntfs_log_error("The cluster size is invalid. It cannot be " + "more that 4096 times the size of the sector " + "size.\n"); + return FALSE; + } + if (vol->cluster_size > NTFS_MAX_CLUSTER_SIZE) { + ntfs_log_error("The cluster size is invalid. The maximum " + "cluster size is %lu bytes (%lukiB).\n", + (unsigned long)NTFS_MAX_CLUSTER_SIZE, + (unsigned long)(NTFS_MAX_CLUSTER_SIZE >> 10)); + return FALSE; + } + vol->cluster_size_bits = ffs(vol->cluster_size) - 1; + ntfs_log_debug("cluster size = %u bytes\n", + (unsigned int)vol->cluster_size); + if (vol->cluster_size > 4096) { + if (opts.enable_compression) { + if (!opts.force) { + ntfs_log_error("Windows cannot use compression " + "when the cluster size is " + "larger than 4096 bytes.\n"); + return FALSE; + } + opts.enable_compression = 0; + } + ntfs_log_warning("Windows cannot use compression when the " + "cluster size is larger than 4096 bytes. " + "Compression has been disabled for this " + "volume.\n"); + } + vol->nr_clusters = volume_size / vol->cluster_size; + /* + * Check the cluster_size and num_sectors for consistency with + * sector_size and num_sectors. And check both of these for consistency + * with volume_size. + */ + if ((vol->nr_clusters != ((opts.num_sectors * opts.sector_size) / + vol->cluster_size) || + (volume_size / opts.sector_size) != opts.num_sectors || + (volume_size / vol->cluster_size) != + vol->nr_clusters)) { + /* XXX is this code reachable? */ + ntfs_log_error("Illegal combination of volume/cluster/sector " + "size and/or cluster/sector number.\n"); + return FALSE; + } + ntfs_log_debug("number of clusters = %llu (0x%llx)\n", + (unsigned long long)vol->nr_clusters, + (unsigned long long)vol->nr_clusters); + /* Number of clusters must fit within 32 bits (Win2k limitation). */ + if (vol->nr_clusters >> 32) { + if (vol->cluster_size >= 65536) { + ntfs_log_error("Device is too large to hold an NTFS " + "volume (maximum size is 256TiB).\n"); + return FALSE; + } + ntfs_log_error("Number of clusters exceeds 32 bits. Please " + "try again with a larger\ncluster size or " + "leave the cluster size unspecified and the " + "smallest possible cluster size for the size " + "of the device will be used.\n"); + return FALSE; + } + page_size = mkntfs_get_page_size(); + /* + * Set the mft record size. By default this is 1024 but it has to be + * at least as big as a sector and not bigger than a page on the system + * or the NTFS kernel driver will not be able to mount the volume. + * TODO: The mft record size should be user specifiable just like the + * "inode size" can be specified on other Linux/Unix file systems. + */ + vol->mft_record_size = 1024; + if (vol->mft_record_size < (u32)opts.sector_size) + vol->mft_record_size = opts.sector_size; + if (vol->mft_record_size > (unsigned long)page_size) + ntfs_log_warning("Mft record size (%u bytes) exceeds system " + "page size (%li bytes). You will not be able " + "to mount this volume using the NTFS kernel " + "driver.\n", (unsigned)vol->mft_record_size, + page_size); + vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1; + ntfs_log_debug("mft record size = %u bytes\n", + (unsigned)vol->mft_record_size); + /* + * Set the index record size. By default this is 4096 but it has to be + * at least as big as a sector and not bigger than a page on the system + * or the NTFS kernel driver will not be able to mount the volume. + * FIXME: Should we make the index record size to be user specifiable? + */ + vol->indx_record_size = 4096; + if (vol->indx_record_size < (u32)opts.sector_size) + vol->indx_record_size = opts.sector_size; + if (vol->indx_record_size > (unsigned long)page_size) + ntfs_log_warning("Index record size (%u bytes) exceeds system " + "page size (%li bytes). You will not be able " + "to mount this volume using the NTFS kernel " + "driver.\n", (unsigned)vol->indx_record_size, + page_size); + vol->indx_record_size_bits = ffs(vol->indx_record_size) - 1; + ntfs_log_debug("index record size = %u bytes\n", + (unsigned)vol->indx_record_size); + if (!winboot) { + ntfs_log_warning("To boot from a device, Windows needs the " + "'partition start sector', the 'sectors per " + "track' and the 'number of heads' to be " + "set.\n"); + ntfs_log_warning("Windows will not be able to boot from this " + "device.\n"); + } + return TRUE; +} + +/** + * mkntfs_initialize_bitmaps - + */ +static BOOL mkntfs_initialize_bitmaps(void) +{ + u64 i; + int mft_bitmap_size; + + /* Determine lcn bitmap byte size and allocate it. */ + g_lcn_bitmap_byte_size = (g_vol->nr_clusters + 7) >> 3; + /* Needs to be multiple of 8 bytes. */ + g_lcn_bitmap_byte_size = (g_lcn_bitmap_byte_size + 7) & ~7; + i = (g_lcn_bitmap_byte_size + g_vol->cluster_size - 1) & + ~(g_vol->cluster_size - 1); + ntfs_log_debug("g_lcn_bitmap_byte_size = %i, allocated = %llu\n", + g_lcn_bitmap_byte_size, (unsigned long long)i); + g_dynamic_buf_size = mkntfs_get_page_size(); + g_dynamic_buf = (u8*)ntfs_calloc(g_dynamic_buf_size); + if (!g_dynamic_buf) + return FALSE; + /* + * $Bitmap can overlap the end of the volume. Any bits in this region + * must be set. This region also encompasses the backup boot sector. + */ + if (!bitmap_allocate(g_vol->nr_clusters, + ((s64)g_lcn_bitmap_byte_size << 3) - g_vol->nr_clusters)) + return (FALSE); + /* + * Mft size is 27 (NTFS 3.0+) mft records or one cluster, whichever is + * bigger. + */ + g_mft_size = 27; + g_mft_size *= g_vol->mft_record_size; + if (g_mft_size < (s32)g_vol->cluster_size) + g_mft_size = g_vol->cluster_size; + ntfs_log_debug("MFT size = %i (0x%x) bytes\n", g_mft_size, g_mft_size); + /* Determine mft bitmap size and allocate it. */ + mft_bitmap_size = g_mft_size / g_vol->mft_record_size; + /* Convert to bytes, at least one. */ + g_mft_bitmap_byte_size = (mft_bitmap_size + 7) >> 3; + /* Mft bitmap is allocated in multiples of 8 bytes. */ + g_mft_bitmap_byte_size = (g_mft_bitmap_byte_size + 7) & ~7; + ntfs_log_debug("mft_bitmap_size = %i, g_mft_bitmap_byte_size = %i\n", + mft_bitmap_size, g_mft_bitmap_byte_size); + g_mft_bitmap = ntfs_calloc(g_mft_bitmap_byte_size); + if (!g_mft_bitmap) + return FALSE; + /* Create runlist for mft bitmap. */ + g_rl_mft_bmp = ntfs_malloc(2 * sizeof(runlist)); + if (!g_rl_mft_bmp) + return FALSE; + + g_rl_mft_bmp[0].vcn = 0LL; + /* Mft bitmap is right after $Boot's data. */ + i = (8192 + g_vol->cluster_size - 1) / g_vol->cluster_size; + g_rl_mft_bmp[0].lcn = i; + /* + * Size is always one cluster, even though valid data size and + * initialized data size are only 8 bytes. + */ + g_rl_mft_bmp[1].vcn = 1LL; + g_rl_mft_bmp[0].length = 1LL; + g_rl_mft_bmp[1].lcn = -1LL; + g_rl_mft_bmp[1].length = 0LL; + /* Allocate cluster for mft bitmap. */ + return (bitmap_allocate(i,1)); +} + +/** + * mkntfs_initialize_rl_mft - + */ +static BOOL mkntfs_initialize_rl_mft(void) +{ + int j; + BOOL done; + + /* If user didn't specify the mft lcn, determine it now. */ + if (!g_mft_lcn) { + /* + * We start at the higher value out of 16kiB and just after the + * mft bitmap. + */ + g_mft_lcn = g_rl_mft_bmp[0].lcn + g_rl_mft_bmp[0].length; + if (g_mft_lcn * g_vol->cluster_size < 16 * 1024) + g_mft_lcn = (16 * 1024 + g_vol->cluster_size - 1) / + g_vol->cluster_size; + } + ntfs_log_debug("$MFT logical cluster number = 0x%llx\n", g_mft_lcn); + /* Determine MFT zone size. */ + g_mft_zone_end = g_vol->nr_clusters; + switch (opts.mft_zone_multiplier) { /* % of volume size in clusters */ + case 4: + g_mft_zone_end = g_mft_zone_end >> 1; /* 50% */ + break; + case 3: + g_mft_zone_end = g_mft_zone_end * 3 >> 3;/* 37.5% */ + break; + case 2: + g_mft_zone_end = g_mft_zone_end >> 2; /* 25% */ + break; + case 1: + default: + g_mft_zone_end = g_mft_zone_end >> 3; /* 12.5% */ + break; + } + ntfs_log_debug("MFT zone size = %lldkiB\n", g_mft_zone_end << + g_vol->cluster_size_bits >> 10 /* >> 10 == / 1024 */); + /* + * The mft zone begins with the mft data attribute, not at the beginning + * of the device. + */ + g_mft_zone_end += g_mft_lcn; + /* Create runlist for mft. */ + g_rl_mft = ntfs_malloc(2 * sizeof(runlist)); + if (!g_rl_mft) + return FALSE; + + g_rl_mft[0].vcn = 0LL; + g_rl_mft[0].lcn = g_mft_lcn; + /* rounded up division by cluster size */ + j = (g_mft_size + g_vol->cluster_size - 1) / g_vol->cluster_size; + g_rl_mft[1].vcn = j; + g_rl_mft[0].length = j; + g_rl_mft[1].lcn = -1LL; + g_rl_mft[1].length = 0LL; + /* Allocate clusters for mft. */ + bitmap_allocate(g_mft_lcn,j); + /* Determine mftmirr_lcn (middle of volume). */ + g_mftmirr_lcn = (opts.num_sectors * opts.sector_size >> 1) + / g_vol->cluster_size; + ntfs_log_debug("$MFTMirr logical cluster number = 0x%llx\n", + g_mftmirr_lcn); + /* Create runlist for mft mirror. */ + g_rl_mftmirr = ntfs_malloc(2 * sizeof(runlist)); + if (!g_rl_mftmirr) + return FALSE; + + g_rl_mftmirr[0].vcn = 0LL; + g_rl_mftmirr[0].lcn = g_mftmirr_lcn; + /* + * The mft mirror is either 4kb (the first four records) or one cluster + * in size, which ever is bigger. In either case, it contains a + * byte-for-byte identical copy of the beginning of the mft (i.e. either + * the first four records (4kb) or the first cluster worth of records, + * whichever is bigger). + */ + j = (4 * g_vol->mft_record_size + g_vol->cluster_size - 1) / g_vol->cluster_size; + g_rl_mftmirr[1].vcn = j; + g_rl_mftmirr[0].length = j; + g_rl_mftmirr[1].lcn = -1LL; + g_rl_mftmirr[1].length = 0LL; + /* Allocate clusters for mft mirror. */ + done = bitmap_allocate(g_mftmirr_lcn,j); + g_logfile_lcn = g_mftmirr_lcn + j; + ntfs_log_debug("$LogFile logical cluster number = 0x%llx\n", + g_logfile_lcn); + return (done); +} + +/** + * mkntfs_initialize_rl_logfile - + */ +static BOOL mkntfs_initialize_rl_logfile(void) +{ + int j; + u64 volume_size; + + /* Create runlist for log file. */ + g_rl_logfile = ntfs_malloc(2 * sizeof(runlist)); + if (!g_rl_logfile) + return FALSE; + + + volume_size = g_vol->nr_clusters << g_vol->cluster_size_bits; + + g_rl_logfile[0].vcn = 0LL; + g_rl_logfile[0].lcn = g_logfile_lcn; + /* + * Determine logfile_size from volume_size (rounded up to a cluster), + * making sure it does not overflow the end of the volume. + */ + if (volume_size < 2048LL * 1024) /* < 2MiB */ + g_logfile_size = 256LL * 1024; /* -> 256kiB */ + else if (volume_size < 4000000LL) /* < 4MB */ + g_logfile_size = 512LL * 1024; /* -> 512kiB */ + else if (volume_size <= 200LL * 1024 * 1024) /* < 200MiB */ + g_logfile_size = 2048LL * 1024; /* -> 2MiB */ + else { + /* + * FIXME: The $LogFile size is 64 MiB upwards from 12GiB but + * the "200" divider below apparently approximates "100" or + * some other value as the volume size decreases. For example: + * Volume size LogFile size Ratio + * 8799808 46048 191.100 + * 8603248 45072 190.877 + * 7341704 38768 189.375 + * 6144828 32784 187.433 + * 4192932 23024 182.111 + */ + if (volume_size >= 12LL << 30) /* > 12GiB */ + g_logfile_size = 64 << 20; /* -> 64MiB */ + else + g_logfile_size = (volume_size / 200) & + ~(g_vol->cluster_size - 1); + } + j = g_logfile_size / g_vol->cluster_size; + while (g_rl_logfile[0].lcn + j >= g_vol->nr_clusters) { + /* + * $Logfile would overflow volume. Need to make it smaller than + * the standard size. It's ok as we are creating a non-standard + * volume anyway if it is that small. + */ + g_logfile_size >>= 1; + j = g_logfile_size / g_vol->cluster_size; + } + g_logfile_size = (g_logfile_size + g_vol->cluster_size - 1) & + ~(g_vol->cluster_size - 1); + ntfs_log_debug("$LogFile (journal) size = %ikiB\n", + g_logfile_size / 1024); + /* + * FIXME: The 256kiB limit is arbitrary. Should find out what the real + * minimum requirement for Windows is so it doesn't blue screen. + */ + if (g_logfile_size < 256 << 10) { + ntfs_log_error("$LogFile would be created with invalid size. " + "This is not allowed as it would cause Windows " + "to blue screen and during boot.\n"); + return FALSE; + } + g_rl_logfile[1].vcn = j; + g_rl_logfile[0].length = j; + g_rl_logfile[1].lcn = -1LL; + g_rl_logfile[1].length = 0LL; + /* Allocate clusters for log file. */ + return (bitmap_allocate(g_logfile_lcn,j)); +} + +/** + * mkntfs_initialize_rl_boot - + */ +static BOOL mkntfs_initialize_rl_boot(void) +{ + int j; + /* Create runlist for $Boot. */ + g_rl_boot = ntfs_malloc(2 * sizeof(runlist)); + if (!g_rl_boot) + return FALSE; + + g_rl_boot[0].vcn = 0LL; + g_rl_boot[0].lcn = 0LL; + /* + * $Boot is always 8192 (0x2000) bytes or 1 cluster, whichever is + * bigger. + */ + j = (8192 + g_vol->cluster_size - 1) / g_vol->cluster_size; + g_rl_boot[1].vcn = j; + g_rl_boot[0].length = j; + g_rl_boot[1].lcn = -1LL; + g_rl_boot[1].length = 0LL; + /* Allocate clusters for $Boot. */ + return (bitmap_allocate(0,j)); +} + +/** + * mkntfs_initialize_rl_bad - + */ +static BOOL mkntfs_initialize_rl_bad(void) +{ + /* Create runlist for $BadClus, $DATA named stream $Bad. */ + g_rl_bad = ntfs_malloc(2 * sizeof(runlist)); + if (!g_rl_bad) + return FALSE; + + g_rl_bad[0].vcn = 0LL; + g_rl_bad[0].lcn = -1LL; + /* + * $BadClus named stream $Bad contains the whole volume as a single + * sparse runlist entry. + */ + g_rl_bad[1].vcn = g_vol->nr_clusters; + g_rl_bad[0].length = g_vol->nr_clusters; + g_rl_bad[1].lcn = -1LL; + g_rl_bad[1].length = 0LL; + + /* TODO: Mark bad blocks as such. */ + return TRUE; +} + +/** + * mkntfs_fill_device_with_zeroes - + */ +static BOOL mkntfs_fill_device_with_zeroes(void) +{ + /* + * If not quick format, fill the device with 0s. + * FIXME: Except bad blocks! (AIA) + */ + int i; + ssize_t bw; + unsigned long long position; + float progress_inc = (float)g_vol->nr_clusters / 100; + u64 volume_size; + + volume_size = g_vol->nr_clusters << g_vol->cluster_size_bits; + + ntfs_log_progress("Initializing device with zeroes: 0%%"); + for (position = 0; position < (unsigned long long)g_vol->nr_clusters; + position++) { + if (!(position % (int)(progress_inc+1))) { + ntfs_log_progress("\b\b\b\b%3.0f%%", position / + progress_inc); + } + bw = mkntfs_write(g_vol->dev, g_buf, g_vol->cluster_size); + if (bw != (ssize_t)g_vol->cluster_size) { + if (bw != -1 || errno != EIO) { + ntfs_log_error("This should not happen.\n"); + return FALSE; + } + if (!position) { + ntfs_log_error("Error: Cluster zero is bad. " + "Cannot create NTFS file " + "system.\n"); + return FALSE; + } + /* Add the baddie to our bad blocks list. */ + if (!append_to_bad_blocks(position)) + return FALSE; + ntfs_log_quiet("\nFound bad cluster (%lld). Adding to " + "list of bad blocks.\nInitializing " + "device with zeroes: %3.0f%%", position, + position / progress_inc); + /* Seek to next cluster. */ + g_vol->dev->d_ops->seek(g_vol->dev, + ((off_t)position + 1) * + g_vol->cluster_size, SEEK_SET); + } + } + ntfs_log_progress("\b\b\b\b100%%"); + position = (volume_size & (g_vol->cluster_size - 1)) / + opts.sector_size; + for (i = 0; (unsigned long)i < position; i++) { + bw = mkntfs_write(g_vol->dev, g_buf, opts.sector_size); + if (bw != opts.sector_size) { + if (bw != -1 || errno != EIO) { + ntfs_log_error("This should not happen.\n"); + return FALSE; + } else if (i + 1ull == position) { + ntfs_log_error("Error: Bad cluster found in " + "location reserved for system " + "file $Boot.\n"); + return FALSE; + } + /* Seek to next sector. */ + g_vol->dev->d_ops->seek(g_vol->dev, + opts.sector_size, SEEK_CUR); + } + } + ntfs_log_progress(" - Done.\n"); + return TRUE; +} + +/** + * mkntfs_sync_index_record + * + * (ERSO) made a function out of this, but the reason for doing that + * disappeared during coding.... + */ +static BOOL mkntfs_sync_index_record(INDEX_ALLOCATION* idx, MFT_RECORD* m, + ntfschar* name, u32 name_len) +{ + int i, err; + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *a; + long long lw; + runlist *rl_index = NULL; + + i = 5 * sizeof(ntfschar); + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_perror("Failed to allocate attribute search context"); + return FALSE; + } + /* FIXME: This should be IGNORE_CASE! */ + if (mkntfs_attr_lookup(AT_INDEX_ALLOCATION, name, name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + ntfs_attr_put_search_ctx(ctx); + ntfs_log_error("BUG: $INDEX_ALLOCATION attribute not found.\n"); + return FALSE; + } + a = ctx->attr; + rl_index = ntfs_mapping_pairs_decompress(g_vol, a, NULL); + if (!rl_index) { + ntfs_attr_put_search_ctx(ctx); + ntfs_log_error("Failed to decompress runlist of $INDEX_ALLOCATION " + "attribute.\n"); + return FALSE; + } + if (sle64_to_cpu(a->initialized_size) < i) { + ntfs_attr_put_search_ctx(ctx); + free(rl_index); + ntfs_log_error("BUG: $INDEX_ALLOCATION attribute too short.\n"); + return FALSE; + } + ntfs_attr_put_search_ctx(ctx); + i = sizeof(INDEX_BLOCK) - sizeof(INDEX_HEADER) + + le32_to_cpu(idx->index.allocated_size); + err = ntfs_mst_pre_write_fixup((NTFS_RECORD*)idx, i); + if (err) { + free(rl_index); + ntfs_log_error("ntfs_mst_pre_write_fixup() failed while " + "syncing index block.\n"); + return FALSE; + } + lw = ntfs_rlwrite(g_vol->dev, rl_index, (u8*)idx, i, NULL, + WRITE_STANDARD); + free(rl_index); + if (lw != i) { + ntfs_log_error("Error writing $INDEX_ALLOCATION.\n"); + return FALSE; + } + /* No more changes to @idx below here so no need for fixup: */ + /* ntfs_mst_post_write_fixup((NTFS_RECORD*)idx); */ + return TRUE; +} + +/** + * create_file_volume - + */ +static BOOL create_file_volume(MFT_RECORD *m, leMFT_REF root_ref, + VOLUME_FLAGS fl, const GUID *volume_guid) +{ + int i, err; + u8 *sd; + + ntfs_log_verbose("Creating $Volume (mft record 3)\n"); + m = (MFT_RECORD*)(g_buf + 3 * g_vol->mft_record_size); + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(FILE_Volume, FILE_Volume), 0LL, 0LL, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0, + "$Volume", FILE_NAME_WIN32_AND_DOS); + if (!err) { + init_system_file_sd(FILE_Volume, &sd, &i); + err = add_attr_sd(m, sd, i); + } + if (!err) + err = add_attr_data(m, NULL, 0, CASE_SENSITIVE, + const_cpu_to_le16(0), NULL, 0); + if (!err) + err = add_attr_vol_name(m, g_vol->vol_name, g_vol->vol_name ? + strlen(g_vol->vol_name) : 0); + if (!err) { + if (fl & VOLUME_IS_DIRTY) + ntfs_log_quiet("Setting the volume dirty so check " + "disk runs on next reboot into " + "Windows.\n"); + err = add_attr_vol_info(m, fl, g_vol->major_ver, + g_vol->minor_ver); + } + if (!err && opts.with_uuid) + err = add_attr_object_id(m, volume_guid); + if (err < 0) { + ntfs_log_error("Couldn't create $Volume: %s\n", + strerror(-err)); + return FALSE; + } + return TRUE; +} + +/** + * create_backup_boot_sector + * + * Return 0 on success or -1 if it couldn't be created. + */ +static int create_backup_boot_sector(u8 *buff) +{ + const char *s; + ssize_t bw; + int size, e; + + ntfs_log_verbose("Creating backup boot sector.\n"); + /* + * Write the first max(512, opts.sector_size) bytes from buf to the + * last sector, but limit that to 8192 bytes of written data since that + * is how big $Boot is (and how big our buffer is).. + */ + size = 512; + if (size < opts.sector_size) + size = opts.sector_size; + if (g_vol->dev->d_ops->seek(g_vol->dev, (opts.num_sectors + 1) * + opts.sector_size - size, SEEK_SET) == (off_t)-1) { + ntfs_log_perror("Seek failed"); + goto bb_err; + } + if (size > 8192) + size = 8192; + bw = mkntfs_write(g_vol->dev, buff, size); + if (bw == size) + return 0; + e = errno; + if (bw == -1LL) + s = strerror(e); + else + s = "unknown error"; + /* At least some 2.4 kernels return EIO instead of ENOSPC. */ + if (bw != -1LL || (bw == -1LL && e != ENOSPC && e != EIO)) { + ntfs_log_critical("Couldn't write backup boot sector: %s\n", s); + return -1; + } +bb_err: + ntfs_log_error("Couldn't write backup boot sector. This is due to a " + "limitation in the\nLinux kernel. This is not a major " + "problem as Windows check disk will create the\n" + "backup boot sector when it is run on your next boot " + "into Windows.\n"); + return -1; +} + +/** + * mkntfs_create_root_structures - + */ +static BOOL mkntfs_create_root_structures(void) +{ + NTFS_BOOT_SECTOR *bs; + MFT_RECORD *m; + leMFT_REF root_ref; + leMFT_REF extend_ref; + int i; + int j; + int err; + u8 *sd; + FILE_ATTR_FLAGS extend_flags; + VOLUME_FLAGS volume_flags = const_cpu_to_le16(0); + int sectors_per_cluster; + int nr_sysfiles; + int buf_sds_first_size; + char *buf_sds; + GUID vol_guid; + + ntfs_log_quiet("Creating NTFS volume structures.\n"); + nr_sysfiles = 27; + /* + * Setup an empty mft record. Note, we can just give 0 as the mft + * reference as we are creating an NTFS 1.2 volume for which the mft + * reference is ignored by ntfs_mft_record_layout(). + * + * Copy the mft record onto all 16 records in the buffer and setup the + * sequence numbers of each system file to equal the mft record number + * of that file (only for $MFT is the sequence number 1 rather than 0). + */ + for (i = 0; i < nr_sysfiles; i++) { + if (ntfs_mft_record_layout(g_vol, 0, m = (MFT_RECORD *)(g_buf + + i * g_vol->mft_record_size))) { + ntfs_log_error("Failed to layout system mft records." + "\n"); + return FALSE; + } + if (i == 0 || i > 23) + m->sequence_number = const_cpu_to_le16(1); + else + m->sequence_number = cpu_to_le16(i); + } + /* + * If only one cluster contains all system files then + * fill the rest of it with empty, formatted records. + */ + if (nr_sysfiles * (s32)g_vol->mft_record_size < g_mft_size) { + for (i = nr_sysfiles; + i * (s32)g_vol->mft_record_size < g_mft_size; i++) { + m = (MFT_RECORD *)(g_buf + i * g_vol->mft_record_size); + if (ntfs_mft_record_layout(g_vol, 0, m)) { + ntfs_log_error("Failed to layout mft record." + "\n"); + return FALSE; + } + m->flags = const_cpu_to_le16(0); + m->sequence_number = cpu_to_le16(i); + } + } + /* + * Create the 16 system files, adding the system information attribute + * to each as well as marking them in use in the mft bitmap. + */ + for (i = 0; i < nr_sysfiles; i++) { + le32 file_attrs; + + m = (MFT_RECORD*)(g_buf + i * g_vol->mft_record_size); + if (i < 16 || i > 23) { + m->mft_record_number = cpu_to_le32(i); + m->flags |= MFT_RECORD_IN_USE; + ntfs_bit_set(g_mft_bitmap, 0LL + i, 1); + } + file_attrs = FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM; + if (i == FILE_root) { + file_attrs |= FILE_ATTR_ARCHIVE; + if (opts.disable_indexing) + file_attrs |= FILE_ATTR_NOT_CONTENT_INDEXED; + if (opts.enable_compression) + file_attrs |= FILE_ATTR_COMPRESSED; + } + /* setting specific security_id flag and */ + /* file permissions for ntfs 3.x */ + if (i == 0 || i == 1 || i == 2 || i == 6 || i == 8 || + i == 10) { + add_attr_std_info(m, file_attrs, + const_cpu_to_le32(0x0100)); + } else if (i == 9) { + file_attrs |= FILE_ATTR_VIEW_INDEX_PRESENT; + add_attr_std_info(m, file_attrs, + const_cpu_to_le32(0x0101)); + } else if (i == 11) { + add_attr_std_info(m, file_attrs, + const_cpu_to_le32(0x0101)); + } else if (i == 24 || i == 25 || i == 26) { + file_attrs |= FILE_ATTR_ARCHIVE; + file_attrs |= FILE_ATTR_VIEW_INDEX_PRESENT; + add_attr_std_info(m, file_attrs, + const_cpu_to_le32(0x0101)); + } else { + add_attr_std_info(m, file_attrs, + const_cpu_to_le32(0x00)); + } + } + /* The root directory mft reference. */ + root_ref = MK_LE_MREF(FILE_root, FILE_root); + extend_ref = MK_LE_MREF(11,11); + ntfs_log_verbose("Creating root directory (mft record 5)\n"); + m = (MFT_RECORD*)(g_buf + 5 * g_vol->mft_record_size); + m->flags |= MFT_RECORD_IS_DIRECTORY; + m->link_count = cpu_to_le16(le16_to_cpu(m->link_count) + 1); + err = add_attr_file_name(m, root_ref, 0LL, 0LL, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM | + FILE_ATTR_I30_INDEX_PRESENT, 0, 0, ".", + FILE_NAME_WIN32_AND_DOS); + if (!err) { + init_root_sd(&sd, &i); + err = add_attr_sd(m, sd, i); + } + /* FIXME: This should be IGNORE_CASE */ + if (!err) + err = add_attr_index_root(m, "$I30", 4, CASE_SENSITIVE, + AT_FILE_NAME, COLLATION_FILE_NAME, + g_vol->indx_record_size); + /* FIXME: This should be IGNORE_CASE */ + if (!err) + err = upgrade_to_large_index(m, "$I30", 4, CASE_SENSITIVE, + &g_index_block); + if (!err) { + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *a; + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_perror("Failed to allocate attribute search " + "context"); + return FALSE; + } + /* There is exactly one file name so this is ok. */ + if (mkntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + ntfs_attr_put_search_ctx(ctx); + ntfs_log_error("BUG: $FILE_NAME attribute not found." + "\n"); + return FALSE; + } + a = ctx->attr; + err = insert_file_link_in_dir_index(g_index_block, root_ref, + (FILE_NAME_ATTR*)((char*)a + + le16_to_cpu(a->value_offset)), + le32_to_cpu(a->value_length)); + ntfs_attr_put_search_ctx(ctx); + } + if (err) { + ntfs_log_error("Couldn't create root directory: %s\n", + strerror(-err)); + return FALSE; + } + /* Add all other attributes, on a per-file basis for clarity. */ + ntfs_log_verbose("Creating $MFT (mft record 0)\n"); + m = (MFT_RECORD*)g_buf; + err = add_attr_data_positioned(m, NULL, 0, CASE_SENSITIVE, + const_cpu_to_le16(0), g_rl_mft, g_buf, g_mft_size); + if (!err) + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(FILE_MFT, 1), + ((g_mft_size - 1) + | (g_vol->cluster_size - 1)) + 1, + g_mft_size, FILE_ATTR_HIDDEN | + FILE_ATTR_SYSTEM, 0, 0, "$MFT", + FILE_NAME_WIN32_AND_DOS); + /* mft_bitmap is not modified in mkntfs; no need to sync it later. */ + if (!err) + err = add_attr_bitmap_positioned(m, NULL, 0, CASE_SENSITIVE, + g_rl_mft_bmp, + g_mft_bitmap, g_mft_bitmap_byte_size); + if (err < 0) { + ntfs_log_error("Couldn't create $MFT: %s\n", strerror(-err)); + return FALSE; + } + ntfs_log_verbose("Creating $MFTMirr (mft record 1)\n"); + m = (MFT_RECORD*)(g_buf + 1 * g_vol->mft_record_size); + err = add_attr_data_positioned(m, NULL, 0, CASE_SENSITIVE, + const_cpu_to_le16(0), g_rl_mftmirr, g_buf, + g_rl_mftmirr[0].length * g_vol->cluster_size); + if (!err) + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(FILE_MFTMirr, FILE_MFTMirr), + g_rl_mftmirr[0].length * g_vol->cluster_size, + g_rl_mftmirr[0].length * g_vol->cluster_size, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0, + "$MFTMirr", FILE_NAME_WIN32_AND_DOS); + if (err < 0) { + ntfs_log_error("Couldn't create $MFTMirr: %s\n", + strerror(-err)); + return FALSE; + } + ntfs_log_verbose("Creating $LogFile (mft record 2)\n"); + m = (MFT_RECORD*)(g_buf + 2 * g_vol->mft_record_size); + err = add_attr_data_positioned(m, NULL, 0, CASE_SENSITIVE, + const_cpu_to_le16(0), g_rl_logfile, + (const u8*)NULL, g_logfile_size); + if (!err) + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(FILE_LogFile, FILE_LogFile), + g_logfile_size, g_logfile_size, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0, + "$LogFile", FILE_NAME_WIN32_AND_DOS); + if (err < 0) { + ntfs_log_error("Couldn't create $LogFile: %s\n", + strerror(-err)); + return FALSE; + } + ntfs_log_verbose("Creating $AttrDef (mft record 4)\n"); + m = (MFT_RECORD*)(g_buf + 4 * g_vol->mft_record_size); + err = add_attr_data(m, NULL, 0, CASE_SENSITIVE, const_cpu_to_le16(0), + (u8*)g_vol->attrdef, g_vol->attrdef_len); + if (!err) + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(FILE_AttrDef, FILE_AttrDef), + (g_vol->attrdef_len + g_vol->cluster_size - 1) & + ~(g_vol->cluster_size - 1), g_vol->attrdef_len, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0, + "$AttrDef", FILE_NAME_WIN32_AND_DOS); + if (!err) { + init_system_file_sd(FILE_AttrDef, &sd, &i); + err = add_attr_sd(m, sd, i); + } + if (err < 0) { + ntfs_log_error("Couldn't create $AttrDef: %s\n", + strerror(-err)); + return FALSE; + } + ntfs_log_verbose("Creating $Bitmap (mft record 6)\n"); + m = (MFT_RECORD*)(g_buf + 6 * g_vol->mft_record_size); + /* the data attribute of $Bitmap must be non-resident or otherwise */ + /* windows 2003 will regard the volume as corrupt (ERSO) */ + if (!err) + err = insert_non_resident_attr_in_mft_record(m, + AT_DATA, NULL, 0, CASE_SENSITIVE, + const_cpu_to_le16(0), (const u8*)NULL, + g_lcn_bitmap_byte_size, WRITE_BITMAP); + + + if (!err) + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(FILE_Bitmap, FILE_Bitmap), + (g_lcn_bitmap_byte_size + g_vol->cluster_size - + 1) & ~(g_vol->cluster_size - 1), + g_lcn_bitmap_byte_size, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0, + "$Bitmap", FILE_NAME_WIN32_AND_DOS); + if (err < 0) { + ntfs_log_error("Couldn't create $Bitmap: %s\n", strerror(-err)); + return FALSE; + } + ntfs_log_verbose("Creating $Boot (mft record 7)\n"); + m = (MFT_RECORD*)(g_buf + 7 * g_vol->mft_record_size); + bs = ntfs_calloc(8192); + if (!bs) + return FALSE; + memcpy(bs, boot_array, sizeof(boot_array)); + /* + * Create the boot sector in bs. Note, that bs is already zeroed + * in the boot sector section and that it has the NTFS OEM id/magic + * already inserted, so no need to worry about these things. + */ + bs->bpb.bytes_per_sector = cpu_to_le16(opts.sector_size); + sectors_per_cluster = g_vol->cluster_size / opts.sector_size; + if (sectors_per_cluster > 128) + bs->bpb.sectors_per_cluster = 257 - ffs(sectors_per_cluster); + else + bs->bpb.sectors_per_cluster = sectors_per_cluster; + bs->bpb.media_type = 0xf8; /* hard disk */ + bs->bpb.sectors_per_track = cpu_to_le16(opts.sectors_per_track); + ntfs_log_debug("sectors per track = %ld (0x%lx)\n", + opts.sectors_per_track, opts.sectors_per_track); + bs->bpb.heads = cpu_to_le16(opts.heads); + ntfs_log_debug("heads = %ld (0x%lx)\n", opts.heads, opts.heads); + bs->bpb.hidden_sectors = cpu_to_le32(opts.part_start_sect); + ntfs_log_debug("hidden sectors = %llu (0x%llx)\n", opts.part_start_sect, + opts.part_start_sect); + bs->physical_drive = 0x80; /* boot from hard disk */ + bs->extended_boot_signature = 0x80; /* everybody sets this, so we do */ + bs->number_of_sectors = cpu_to_sle64(opts.num_sectors); + bs->mft_lcn = cpu_to_sle64(g_mft_lcn); + bs->mftmirr_lcn = cpu_to_sle64(g_mftmirr_lcn); + if (g_vol->mft_record_size >= g_vol->cluster_size) { + bs->clusters_per_mft_record = g_vol->mft_record_size / + g_vol->cluster_size; + } else { + bs->clusters_per_mft_record = -(ffs(g_vol->mft_record_size) - + 1); + if ((u32)(1 << -bs->clusters_per_mft_record) != + g_vol->mft_record_size) { + ntfs_log_error("BUG: calculated clusters_per_mft_record" + " is wrong (= 0x%x)\n", + bs->clusters_per_mft_record); + free(bs); + return FALSE; + } + } + ntfs_log_debug("clusters per mft record = %i (0x%x)\n", + bs->clusters_per_mft_record, + bs->clusters_per_mft_record); + if (g_vol->indx_record_size >= g_vol->cluster_size) { + bs->clusters_per_index_record = g_vol->indx_record_size / + g_vol->cluster_size; + } else { + bs->clusters_per_index_record = -g_vol->indx_record_size_bits; + if ((1 << -bs->clusters_per_index_record) != + (s32)g_vol->indx_record_size) { + ntfs_log_error("BUG: calculated " + "clusters_per_index_record is wrong " + "(= 0x%x)\n", + bs->clusters_per_index_record); + free(bs); + return FALSE; + } + } + ntfs_log_debug("clusters per index block = %i (0x%x)\n", + bs->clusters_per_index_record, + bs->clusters_per_index_record); + /* Generate a 64-bit random number for the serial number. */ + bs->volume_serial_number = cpu_to_le64(((u64)random() << 32) | + ((u64)random() & 0xffffffff)); + /* + * Leave zero for now as NT4 leaves it zero, too. If want it later, see + * ../libntfs/bootsect.c for how to calculate it. + */ + bs->checksum = const_cpu_to_le32(0); + /* Make sure the bootsector is ok. */ + if (!ntfs_boot_sector_is_ntfs(bs)) { + free(bs); + ntfs_log_error("FATAL: Generated boot sector is invalid!\n"); + return FALSE; + } + err = add_attr_data_positioned(m, NULL, 0, CASE_SENSITIVE, + const_cpu_to_le16(0), g_rl_boot, (u8*)bs, 8192); + if (!err) + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(FILE_Boot, FILE_Boot), + (8192 + g_vol->cluster_size - 1) & + ~(g_vol->cluster_size - 1), 8192, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0, + "$Boot", FILE_NAME_WIN32_AND_DOS); + if (!err) { + init_system_file_sd(FILE_Boot, &sd, &i); + err = add_attr_sd(m, sd, i); + } + if (err < 0) { + free(bs); + ntfs_log_error("Couldn't create $Boot: %s\n", strerror(-err)); + return FALSE; + } + if (create_backup_boot_sector((u8*)bs)) { + /* + * Pre-2.6 kernels couldn't access the last sector if it was + * odd and we failed to set the device block size to the sector + * size, hence we schedule chkdsk to create it. + */ + volume_flags |= VOLUME_IS_DIRTY; + } + free(bs); + /* + * We cheat a little here and if the user has requested all times to be + * set to zero then we set the GUID to zero as well. This options is + * only used for development purposes so that should be fine. + */ + if (!opts.use_epoch_time) { + /* Generate a GUID for the volume. */ +#ifdef ENABLE_UUID + uuid_generate((void*)&vol_guid); +#else + ntfs_generate_guid(&vol_guid); +#endif + } else + memset(&vol_guid, 0, sizeof(vol_guid)); + if (!create_file_volume(m, root_ref, volume_flags, &vol_guid)) + return FALSE; + ntfs_log_verbose("Creating $BadClus (mft record 8)\n"); + m = (MFT_RECORD*)(g_buf + 8 * g_vol->mft_record_size); + /* FIXME: This should be IGNORE_CASE */ + /* Create a sparse named stream of size equal to the volume size. */ + err = add_attr_data_positioned(m, "$Bad", 4, CASE_SENSITIVE, + const_cpu_to_le16(0), g_rl_bad, NULL, + g_vol->nr_clusters * g_vol->cluster_size); + if (!err) { + err = add_attr_data(m, NULL, 0, CASE_SENSITIVE, + const_cpu_to_le16(0), NULL, 0); + } + if (!err) { + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(FILE_BadClus, FILE_BadClus), + 0LL, 0LL, FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, + 0, 0, "$BadClus", FILE_NAME_WIN32_AND_DOS); + } + if (err < 0) { + ntfs_log_error("Couldn't create $BadClus: %s\n", + strerror(-err)); + return FALSE; + } + /* create $Secure (NTFS 3.0+) */ + ntfs_log_verbose("Creating $Secure (mft record 9)\n"); + m = (MFT_RECORD*)(g_buf + 9 * g_vol->mft_record_size); + m->flags |= MFT_RECORD_IS_VIEW_INDEX; + if (!err) + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(9, 9), 0LL, 0LL, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM | + FILE_ATTR_VIEW_INDEX_PRESENT, 0, 0, + "$Secure", FILE_NAME_WIN32_AND_DOS); + buf_sds = NULL; + buf_sds_first_size = 0; + if (!err) { + int buf_sds_size; + + buf_sds_first_size = 0xfc; + buf_sds_size = 0x40000 + buf_sds_first_size; + buf_sds = ntfs_calloc(buf_sds_size); + if (!buf_sds) + return FALSE; + init_secure_sds(buf_sds); + memcpy(buf_sds + 0x40000, buf_sds, buf_sds_first_size); + err = add_attr_data(m, "$SDS", 4, CASE_SENSITIVE, + const_cpu_to_le16(0), (u8*)buf_sds, + buf_sds_size); + } + /* FIXME: This should be IGNORE_CASE */ + if (!err) + err = add_attr_index_root(m, "$SDH", 4, CASE_SENSITIVE, + AT_UNUSED, COLLATION_NTOFS_SECURITY_HASH, + g_vol->indx_record_size); + /* FIXME: This should be IGNORE_CASE */ + if (!err) + err = add_attr_index_root(m, "$SII", 4, CASE_SENSITIVE, + AT_UNUSED, COLLATION_NTOFS_ULONG, + g_vol->indx_record_size); + if (!err) + err = initialize_secure(buf_sds, buf_sds_first_size, m); + free(buf_sds); + if (err < 0) { + ntfs_log_error("Couldn't create $Secure: %s\n", + strerror(-err)); + return FALSE; + } + ntfs_log_verbose("Creating $UpCase (mft record 0xa)\n"); + m = (MFT_RECORD*)(g_buf + 0xa * g_vol->mft_record_size); + err = add_attr_data(m, NULL, 0, CASE_SENSITIVE, const_cpu_to_le16(0), + (u8*)g_vol->upcase, g_vol->upcase_len << 1); + /* + * The $Info only exists since Windows 8, but it apparently + * does not disturb chkdsk from earlier versions. + */ + if (!err) + err = add_attr_data(m, "$Info", 5, CASE_SENSITIVE, + const_cpu_to_le16(0), + (u8*)g_upcaseinfo, sizeof(struct UPCASEINFO)); + if (!err) + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(FILE_UpCase, FILE_UpCase), + ((g_vol->upcase_len << 1) + + g_vol->cluster_size - 1) & + ~(g_vol->cluster_size - 1), + g_vol->upcase_len << 1, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM, 0, 0, + "$UpCase", FILE_NAME_WIN32_AND_DOS); + if (err < 0) { + ntfs_log_error("Couldn't create $UpCase: %s\n", strerror(-err)); + return FALSE; + } + ntfs_log_verbose("Creating $Extend (mft record 11)\n"); + /* + * $Extend index must be resident. Otherwise, w2k3 will regard the + * volume as corrupt. (ERSO) + */ + m = (MFT_RECORD*)(g_buf + 11 * g_vol->mft_record_size); + m->flags |= MFT_RECORD_IS_DIRECTORY; + if (!err) + err = create_hardlink(g_index_block, root_ref, m, + MK_LE_MREF(11, 11), 0LL, 0LL, + FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM | + FILE_ATTR_I30_INDEX_PRESENT, 0, 0, + "$Extend", FILE_NAME_WIN32_AND_DOS); + /* FIXME: This should be IGNORE_CASE */ + if (!err) + err = add_attr_index_root(m, "$I30", 4, CASE_SENSITIVE, + AT_FILE_NAME, COLLATION_FILE_NAME, + g_vol->indx_record_size); + if (err < 0) { + ntfs_log_error("Couldn't create $Extend: %s\n", + strerror(-err)); + return FALSE; + } + /* NTFS reserved system files (mft records 0xc-0xf) */ + for (i = 0xc; i < 0x10; i++) { + ntfs_log_verbose("Creating system file (mft record 0x%x)\n", i); + m = (MFT_RECORD*)(g_buf + i * g_vol->mft_record_size); + err = add_attr_data(m, NULL, 0, CASE_SENSITIVE, + const_cpu_to_le16(0), NULL, 0); + if (!err) { + init_system_file_sd(i, &sd, &j); + err = add_attr_sd(m, sd, j); + } + if (err < 0) { + ntfs_log_error("Couldn't create system file %i (0x%x): " + "%s\n", i, i, strerror(-err)); + return FALSE; + } + } + /* create systemfiles for ntfs volumes (3.1) */ + /* starting with file 24 (ignoring file 16-23) */ + extend_flags = FILE_ATTR_HIDDEN | FILE_ATTR_SYSTEM | + FILE_ATTR_ARCHIVE | FILE_ATTR_VIEW_INDEX_PRESENT; + ntfs_log_verbose("Creating $Quota (mft record 24)\n"); + m = (MFT_RECORD*)(g_buf + 24 * g_vol->mft_record_size); + m->flags |= MFT_RECORD_IS_4; + m->flags |= MFT_RECORD_IS_VIEW_INDEX; + if (!err) + err = create_hardlink_res((MFT_RECORD*)(g_buf + + 11 * g_vol->mft_record_size), extend_ref, m, + MK_LE_MREF(24, 1), 0LL, 0LL, extend_flags, + 0, 0, "$Quota", FILE_NAME_WIN32_AND_DOS); + /* FIXME: This should be IGNORE_CASE */ + if (!err) + err = add_attr_index_root(m, "$Q", 2, CASE_SENSITIVE, AT_UNUSED, + COLLATION_NTOFS_ULONG, g_vol->indx_record_size); + /* FIXME: This should be IGNORE_CASE */ + if (!err) + err = add_attr_index_root(m, "$O", 2, CASE_SENSITIVE, AT_UNUSED, + COLLATION_NTOFS_SID, g_vol->indx_record_size); + if (!err) + err = initialize_quota(m); + if (err < 0) { + ntfs_log_error("Couldn't create $Quota: %s\n", strerror(-err)); + return FALSE; + } + ntfs_log_verbose("Creating $ObjId (mft record 25)\n"); + m = (MFT_RECORD*)(g_buf + 25 * g_vol->mft_record_size); + m->flags |= MFT_RECORD_IS_4; + m->flags |= MFT_RECORD_IS_VIEW_INDEX; + if (!err) + err = create_hardlink_res((MFT_RECORD*)(g_buf + + 11 * g_vol->mft_record_size), extend_ref, + m, MK_LE_MREF(25, 1), 0LL, 0LL, + extend_flags, 0, 0, "$ObjId", + FILE_NAME_WIN32_AND_DOS); + + /* FIXME: This should be IGNORE_CASE */ + if (!err) + err = add_attr_index_root(m, "$O", 2, CASE_SENSITIVE, AT_UNUSED, + COLLATION_NTOFS_ULONGS, + g_vol->indx_record_size); + if (!err && opts.with_uuid) + err = index_obj_id_insert(m, &vol_guid, + MK_LE_MREF(FILE_Volume, FILE_Volume)); + if (err < 0) { + ntfs_log_error("Couldn't create $ObjId: %s\n", + strerror(-err)); + return FALSE; + } + ntfs_log_verbose("Creating $Reparse (mft record 26)\n"); + m = (MFT_RECORD*)(g_buf + 26 * g_vol->mft_record_size); + m->flags |= MFT_RECORD_IS_4; + m->flags |= MFT_RECORD_IS_VIEW_INDEX; + if (!err) + err = create_hardlink_res((MFT_RECORD*)(g_buf + + 11 * g_vol->mft_record_size), + extend_ref, m, MK_LE_MREF(26, 1), + 0LL, 0LL, extend_flags, 0, 0, + "$Reparse", FILE_NAME_WIN32_AND_DOS); + /* FIXME: This should be IGNORE_CASE */ + if (!err) + err = add_attr_index_root(m, "$R", 2, CASE_SENSITIVE, AT_UNUSED, + COLLATION_NTOFS_ULONGS, g_vol->indx_record_size); + if (err < 0) { + ntfs_log_error("Couldn't create $Reparse: %s\n", + strerror(-err)); + return FALSE; + } + return TRUE; +} + +/** + * mkntfs_redirect + */ +static int mkntfs_redirect(struct mkntfs_options *opts2) +{ + u64 upcase_crc; + int result = 1; + ntfs_attr_search_ctx *ctx = NULL; + long long lw, pos; + ATTR_RECORD *a; + MFT_RECORD *m; + int i, err; + + if (!opts2) { + ntfs_log_error("Internal error: invalid parameters to mkntfs_options.\n"); + goto done; + } + /* Initialize the random number generator with the current time. */ + srandom(sle64_to_cpu(mkntfs_time())/10000000); + /* Allocate and initialize ntfs_volume structure g_vol. */ + g_vol = ntfs_volume_alloc(); + if (!g_vol) { + ntfs_log_perror("Could not create volume"); + goto done; + } + /* Create NTFS 3.1 (Windows XP/Vista) volumes. */ + g_vol->major_ver = 3; + g_vol->minor_ver = 1; + /* Transfer some options to the volume. */ + if (opts.label) { + g_vol->vol_name = strdup(opts.label); + if (!g_vol->vol_name) { + ntfs_log_perror("Could not copy volume name"); + goto done; + } + } + if (opts.cluster_size >= 0) + g_vol->cluster_size = opts.cluster_size; + /* Length is in unicode characters. */ + g_vol->upcase_len = ntfs_upcase_build_default(&g_vol->upcase); + /* Since Windows 8, there is a $Info stream in $UpCase */ + g_upcaseinfo = + (struct UPCASEINFO*)ntfs_malloc(sizeof(struct UPCASEINFO)); + if (!g_vol->upcase_len || !g_upcaseinfo) + goto done; + /* If the CRC is correct, chkdsk does not warn about obsolete table */ + crc64(0,(byte*)NULL,0); /* initialize the crc computation */ + upcase_crc = crc64(0,(byte*)g_vol->upcase, + g_vol->upcase_len * sizeof(ntfschar)); + /* keep the version fields as zero */ + memset(g_upcaseinfo, 0, sizeof(struct UPCASEINFO)); + g_upcaseinfo->len = const_cpu_to_le32(sizeof(struct UPCASEINFO)); + g_upcaseinfo->crc = cpu_to_le64(upcase_crc); + g_vol->attrdef = ntfs_malloc(sizeof(attrdef_ntfs3x_array)); + if (!g_vol->attrdef) { + ntfs_log_perror("Could not create attrdef structure"); + goto done; + } + memcpy(g_vol->attrdef, attrdef_ntfs3x_array, + sizeof(attrdef_ntfs3x_array)); + g_vol->attrdef_len = sizeof(attrdef_ntfs3x_array); + /* Open the partition. */ + if (!mkntfs_open_partition(g_vol)) + goto done; + /* + * Decide on the sector size, cluster size, mft record and index record + * sizes as well as the number of sectors/tracks/heads/size, etc. + */ + if (!mkntfs_override_vol_params(g_vol)) + goto done; + /* Initialize $Bitmap and $MFT/$BITMAP related stuff. */ + if (!mkntfs_initialize_bitmaps()) + goto done; + /* Initialize MFT & set g_logfile_lcn. */ + if (!mkntfs_initialize_rl_mft()) + goto done; + /* Initialize $LogFile. */ + if (!mkntfs_initialize_rl_logfile()) + goto done; + /* Initialize $Boot. */ + if (!mkntfs_initialize_rl_boot()) + goto done; + /* Allocate a buffer large enough to hold the mft. */ + g_buf = ntfs_calloc(g_mft_size); + if (!g_buf) + goto done; + /* Create runlist for $BadClus, $DATA named stream $Bad. */ + if (!mkntfs_initialize_rl_bad()) + goto done; + /* If not quick format, fill the device with 0s. */ + if (!opts.quick_format) { + if (!mkntfs_fill_device_with_zeroes()) + goto done; + } + /* Create NTFS volume structures. */ + if (!mkntfs_create_root_structures()) + goto done; + /* + * - Do not step onto bad blocks!!! + * - If any bad blocks were specified or found, modify $BadClus, + * allocating the bad clusters in $Bitmap. + * - C&w bootsector backup bootsector (backup in last sector of the + * partition). + * - If NTFS 3.0+, c&w $Secure file and $Extend directory with the + * corresponding special files in it, i.e. $ObjId, $Quota, $Reparse, + * and $UsnJrnl. And others? Or not all necessary? + * - RE: Populate $root with the system files (and $Extend directory if + * applicable). Possibly should move this as far to the top as + * possible and update during each subsequent c&w of each system file. + */ + ntfs_log_verbose("Syncing root directory index record.\n"); + if (!mkntfs_sync_index_record(g_index_block, (MFT_RECORD*)(g_buf + 5 * + g_vol->mft_record_size), NTFS_INDEX_I30, 4)) + goto done; + + ntfs_log_verbose("Syncing $Bitmap.\n"); + m = (MFT_RECORD*)(g_buf + 6 * g_vol->mft_record_size); + + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_perror("Could not create an attribute search context"); + goto done; + } + + if (mkntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + ntfs_log_error("BUG: $DATA attribute not found.\n"); + goto done; + } + + a = ctx->attr; + if (a->non_resident) { + runlist *rl = ntfs_mapping_pairs_decompress(g_vol, a, NULL); + if (!rl) { + ntfs_log_error("ntfs_mapping_pairs_decompress() failed\n"); + goto done; + } + lw = ntfs_rlwrite(g_vol->dev, rl, (const u8*)NULL, + g_lcn_bitmap_byte_size, NULL, WRITE_BITMAP); + err = errno; + free(rl); + if (lw != g_lcn_bitmap_byte_size) { + ntfs_log_error("ntfs_rlwrite: %s\n", lw == -1 ? + strerror(err) : "unknown error"); + goto done; + } + } else { + /* Error : the bitmap must be created non resident */ + ntfs_log_error("Error : the global bitmap is resident\n"); + goto done; + } + + /* + * No need to sync $MFT/$BITMAP as that has never been modified since + * its creation. + */ + ntfs_log_verbose("Syncing $MFT.\n"); + pos = g_mft_lcn * g_vol->cluster_size; + lw = 1; + for (i = 0; i < g_mft_size / (s32)g_vol->mft_record_size; i++) { + if (!opts.no_action) + lw = ntfs_mst_pwrite(g_vol->dev, pos, 1, g_vol->mft_record_size, g_buf + i * g_vol->mft_record_size); + if (lw != 1) { + ntfs_log_error("ntfs_mst_pwrite: %s\n", lw == -1 ? + strerror(errno) : "unknown error"); + goto done; + } + pos += g_vol->mft_record_size; + } + ntfs_log_verbose("Updating $MFTMirr.\n"); + pos = g_mftmirr_lcn * g_vol->cluster_size; + lw = 1; + for (i = 0; i < g_rl_mftmirr[0].length * g_vol->cluster_size / g_vol->mft_record_size; i++) { + m = (MFT_RECORD*)(g_buf + i * g_vol->mft_record_size); + /* + * Decrement the usn by one, so it becomes the same as the one + * in $MFT once it is mst protected. - This is as we need the + * $MFTMirr to have the exact same byte by byte content as + * $MFT, rather than just equivalent meaning content. + */ + if (ntfs_mft_usn_dec(m)) { + ntfs_log_error("ntfs_mft_usn_dec"); + goto done; + } + if (!opts.no_action) + lw = ntfs_mst_pwrite(g_vol->dev, pos, 1, g_vol->mft_record_size, g_buf + i * g_vol->mft_record_size); + if (lw != 1) { + ntfs_log_error("ntfs_mst_pwrite: %s\n", lw == -1 ? + strerror(errno) : "unknown error"); + goto done; + } + pos += g_vol->mft_record_size; + } + ntfs_log_verbose("Syncing device.\n"); + if (g_vol->dev->d_ops->sync(g_vol->dev)) { + ntfs_log_error("Syncing device. FAILED"); + goto done; + } + ntfs_log_quiet("mkntfs completed successfully. Have a nice day.\n"); + result = 0; +done: + ntfs_attr_put_search_ctx(ctx); + mkntfs_cleanup(); /* Device is unlocked and closed here */ + return result; +} + + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char *argv[]) +{ + int result = 1; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + utils_set_locale(); + + mkntfs_init_options(&opts); /* Set up the options */ + + /* Read the command line options */ + result = mkntfs_parse_options(argc, argv, &opts); + + if (result < 0) + result = mkntfs_redirect(&opts); + + return result; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscat.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscat.8.in new file mode 100644 index 0000000..d594b9d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscat.8.in @@ -0,0 +1,136 @@ +.\" Copyright (c) 2003\-2005 Richard Russon. +.\" Copyright (c) 2007 Yura Pakhuchiy. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSCAT 8 "September 2007" "ntfs-3g @VERSION@" +.SH NAME +ntfscat \- print NTFS files and streams on the standard output +.SH SYNOPSIS +[\fIoptions\fR] \fIdevice \fR[\fIfile\fR] +.SH DESCRIPTION +.B ntfscat +will read a file or stream from an NTFS volume and display the contents +on the standard output. +.PP +The case of the filename passed to +.B ntfscat +is ignored. +.SH OPTIONS +Below is a summary of all the options that +.B ntfscat +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-a\fR, \fB\-\-attribute\fR TYPE +Display the contents of a particular attribute type. By default, the unnamed +$DATA attribute will be shown. The attribute can be specified by a number +in decimal or hexadecimal, or by name. +.TS +box; +lB lB lB +l l l. +Hex Decimal Name +0x10 16 "$STANDARD_INFORMATION" +0x20 32 "$ATTRIBUTE_LIST" +0x30 48 "$FILE_NAME" +0x40 64 "$OBJECT_ID" +0x50 80 "$SECURITY_DESCRIPTOR" +0x60 96 "$VOLUME_NAME" +0x70 112 "$VOLUME_INFORMATION" +0x80 128 "$DATA" +0x90 144 "$INDEX_ROOT" +0xA0 160 "$INDEX_ALLOCATION" +0xB0 176 "$BITMAP" +0xC0 192 "$REPARSE_POINT" +0xD0 208 "$EA_INFORMATION" +0xE0 224 "$EA" +0xF0 240 "$PROPERTY_SET" +0x100 256 "$LOGGED_UTILITY_STREAM" +.TE +.sp +.sp +.B Notes +The attribute names may be given without the leading $ symbol. +.br +If you use the $ symbol, you must quote the name to prevent the shell +interpreting the name. +.TP +\fB\-n\fR, \fB\-\-attribute\-name\fR NAME +Display this named attribute, stream. +.TP +\fB\-i\fR, \fB\-\-inode\fR NUM +Specify a file by its inode number instead of its name. +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not using a mounted volume. +Use this option with caution. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Suppress some debug/warning/error messages. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license +.BR ntfscat . +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Display more debug/warning/error messages. +.SH EXAMPLES +Display the contents of a file in the root of an NTFS volume. +.RS +.sp +.B ntfscat /dev/hda1 boot.ini +.sp +.RE +Display the contents of a file in a subdirectory of an NTFS volume. +.RS +.sp +.B ntfscat /dev/hda1 /winnt/system32/drivers/etc/hosts +.sp +.RE +Display the contents of the $INDEX_ROOT attribute of the root directory (inode +5). +.RS +.sp +.B ntfscat /dev/hda1 \-a INDEX_ROOT \-i 5 | hexdump \-C +.sp +.RE +.SH BUGS +There are no known problems with +.BR ntfscat . +If you find a bug please send an email describing the problem to the +development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfscat +was written by Richard Russon, Anton Altaparmakov and Szabolcs Szakacsits. +It was ported to ntfs-3g by Erik Larsson. +.SH AVAILABILITY +.B ntfscat +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +Read \fBlibntfs\fR(8) for details how to access encrypted files. +.sp +.BR libntfs (8), +.BR ntfsls (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscat.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscat.c new file mode 100644 index 0000000..0f71181 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscat.c @@ -0,0 +1,462 @@ +/** + * ntfscat - Part of the Linux-NTFS project. + * + * Copyright (c) 2003-2005 Richard Russon + * Copyright (c) 2003-2005 Anton Altaparmakov + * Copyright (c) 2003-2005 Szabolcs Szakacsits + * Copyright (c) 2007 Yura Pakhuchiy + * + * This utility will concatenate files and print on the standard output. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif + +#include "types.h" +#include "attrib.h" +#include "utils.h" +#include "volume.h" +#include "debug.h" +#include "dir.h" +#include "ntfscat.h" +/* #include "version.h" */ +#include "utils.h" + +static const char *EXEC_NAME = "ntfscat"; +static struct options opts; + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + ntfs_log_info("\n%s v%s (libntfs-3g) - Concatenate files and print " + "on the standard output.\n\n", EXEC_NAME, VERSION); + ntfs_log_info("Copyright (c) 2003-2005 Richard Russon\n"); + ntfs_log_info("Copyright (c) 2003-2005 Anton Altaparmakov\n"); + ntfs_log_info("Copyright (c) 2003-2005 Szabolcs Szakacsits\n"); + ntfs_log_info("Copyright (c) 2007 Yura Pakhuchiy\n"); + ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + ntfs_log_info("\nUsage: %s [options] device [file]\n\n" + " -a, --attribute TYPE Display this attribute type\n" + " -n, --attribute-name NAME Display this attribute name\n" + " -i, --inode NUM Display this inode\n\n" + " -f, --force Use less caution\n" + " -h, --help Print this help\n" + " -q, --quiet Less output\n" + " -V, --version Version information\n" + " -v, --verbose More output\n\n", +// Does not work for compressed files at present so leave undocumented... +// " -r --raw Display the raw data (e.g. for compressed or encrypted file)", + EXEC_NAME); + ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * parse_attribute - Read an attribute name, or number + * @value: String to be parsed + * @attr: Resulting attribute id (on success) + * + * Read a string representing an attribute. It may be a decimal, octal or + * hexadecimal number, or the attribute name in full. The leading $ sign is + * optional. + * + * Return: 1 Success, a valid attribute name or number + * 0 Error, not an attribute name or number + */ +static int parse_attribute(const char *value, ATTR_TYPES *attr) +{ + static const char *attr_name[] = { + "$STANDARD_INFORMATION", + "$ATTRIBUTE_LIST", + "$FILE_NAME", + "$OBJECT_ID", + "$SECURITY_DESCRIPTOR", + "$VOLUME_NAME", + "$VOLUME_INFORMATION", + "$DATA", + "$INDEX_ROOT", + "$INDEX_ALLOCATION", + "$BITMAP", + "$REPARSE_POINT", + "$EA_INFORMATION", + "$EA", + "$PROPERTY_SET", + "$LOGGED_UTILITY_STREAM", + NULL + }; + + int i; + long num; + + for (i = 0; attr_name[i]; i++) { + if ((strcmp(value, attr_name[i]) == 0) || + (strcmp(value, attr_name[i] + 1) == 0)) { + *attr = (ATTR_TYPES)cpu_to_le32((i + 1) * 16); + return 1; + } + } + + num = strtol(value, NULL, 0); + if ((num > 0) && (num < 257)) { + *attr = (ATTR_TYPES)cpu_to_le32(num); + return 1; + } + + return 0; +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char **argv) +{ + static const char *sopt = "-a:fh?i:n:qVvr"; + static const struct option lopt[] = { + { "attribute", required_argument, NULL, 'a' }, + { "attribute-name", required_argument, NULL, 'n' }, + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "inode", required_argument, NULL, 'i' }, + { "quiet", no_argument, NULL, 'q' }, + { "version", no_argument, NULL, 'V' }, + { "verbose", no_argument, NULL, 'v' }, + { "raw", no_argument, NULL, 'r' }, + { NULL, 0, NULL, 0 } + }; + + int c = -1; + int err = 0; + int ver = 0; + int help = 0; + int levels = 0; + ATTR_TYPES attr = AT_UNUSED; + + opterr = 0; /* We'll handle the errors, thank you. */ + + opts.inode = -1; + opts.attr = const_cpu_to_le32(-1); + opts.attr_name = NULL; + opts.attr_name_len = 0; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opts.device) { + opts.device = argv[optind - 1]; + } else if (!opts.file) { + opts.file = argv[optind - 1]; + } else { + ntfs_log_error("You must specify exactly one " + "file.\n"); + err++; + } + break; + case 'a': + if (opts.attr != const_cpu_to_le32(-1)) { + ntfs_log_error("You must specify exactly one " + "attribute.\n"); + } else if (parse_attribute(optarg, &attr) > 0) { + opts.attr = attr; + break; + } else { + ntfs_log_error("Couldn't parse attribute.\n"); + } + err++; + break; + case 'f': + opts.force++; + break; + case 'h': + help++; + break; + case 'i': + if (opts.inode != -1) + ntfs_log_error("You must specify exactly one inode.\n"); + else if (utils_parse_size(optarg, &opts.inode, FALSE)) + break; + else + ntfs_log_error("Couldn't parse inode number.\n"); + err++; + break; + + case 'n': + opts.attr_name_len = ntfs_mbstoucs(optarg, + &opts.attr_name); + if (opts.attr_name_len < 0) { + ntfs_log_perror("Invalid attribute name '%s'", + optarg); + usage(); + } + break; + + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 'V': + ver++; + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'r': + opts.raw = TRUE; + break; + case '?': + if (strncmp (argv[optind-1], "--log-", 6) == 0) { + if (!ntfs_log_parse_option (argv[optind-1])) + err++; + break; + } + /* fall through */ + default: + ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); + err++; + break; + } + } + + /* Make sure we're in sync with the log levels */ + levels = ntfs_log_get_levels(); + if (levels & NTFS_LOG_LEVEL_VERBOSE) + opts.verbose++; + if (!(levels & NTFS_LOG_LEVEL_QUIET)) + opts.quiet++; + + if (help || ver) { + opts.quiet = 0; + } else { + if (opts.device == NULL) { + ntfs_log_error("You must specify a device.\n"); + err++; + + } else if (opts.file == NULL && opts.inode == -1) { + ntfs_log_error("You must specify a file or inode " + "with the -i option.\n"); + err++; + + } else if (opts.file != NULL && opts.inode != -1) { + ntfs_log_error("You can't specify both a file and inode.\n"); + err++; + } + + if (opts.quiet && opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose at the " + "same time.\n"); + err++; + } + } + + if (ver) + version(); + if (help || err) + usage(); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver ? 0 : -1)); +} + +/** + * index_get_size - Find the INDX block size from the index root + * @inode: Inode of the directory to be checked + * + * Find the size of a directory's INDX block from the INDEX_ROOT attribute. + * + * Return: n Success, the INDX blocks are n bytes in size + * 0 Error, not a directory + */ +static int index_get_size(ntfs_inode *inode) +{ + ATTR_RECORD *attr90; + INDEX_ROOT *iroot; + + attr90 = find_first_attribute(AT_INDEX_ROOT, inode->mrec); + if (!attr90) + return 0; // not a directory + + iroot = (INDEX_ROOT*)((u8*)attr90 + le16_to_cpu(attr90->value_offset)); + return le32_to_cpu(iroot->index_block_size); +} + +/** + * cat + */ +static int cat(ntfs_volume *vol, ntfs_inode *inode, ATTR_TYPES type, + ntfschar *name, int namelen) +{ + char *buffer; + ntfs_attr *attr; + s64 bytes_read, written; + s64 offset; + u32 block_size; + u32 bufsize = 4096; + + attr = ntfs_attr_open(inode, type, name, namelen); + if (!attr) { + ntfs_log_error("Cannot find attribute type 0x%x.\n", + le32_to_cpu(type)); + return 1; + } + + if ((inode->mft_no < 2) && (attr->type == AT_DATA)) + block_size = vol->mft_record_size; + else if (attr->type == AT_INDEX_ALLOCATION) + block_size = index_get_size(inode); + else + block_size = 0; + + if (bufsize < block_size) { + bufsize = block_size; + } + + buffer = malloc(bufsize); + if (!buffer) { + return 1; + } + + offset = 0; + for (;;) { + if (!opts.raw && block_size > 0) { + // These types have fixup + bytes_read = ntfs_attr_mst_pread(attr, offset, 1, block_size, buffer); + if (bytes_read > 0) + bytes_read *= block_size; + } else { + bytes_read = ntfs_attr_pread(attr, offset, bufsize, buffer); + } + //ntfs_log_info("read %lld bytes\n", bytes_read); + if (bytes_read == -1) { + ntfs_log_perror("ERROR: Couldn't read file"); + break; + } + if (!bytes_read) + break; + + written = fwrite(buffer, 1, bytes_read, stdout); + if (written != bytes_read) { + ntfs_log_perror("ERROR: Couldn't output all data!"); + break; + } + offset += bytes_read; + } + + ntfs_attr_close(attr); + free(buffer); + return 0; +} + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char *argv[]) +{ + ntfs_volume *vol; + ntfs_inode *inode; + ATTR_TYPES attr; + int res; + int result = 1; + + ntfs_log_set_handler(ntfs_log_handler_stderr); + + res = parse_options(argc, argv); + if (res >= 0) + return (res); + + utils_set_locale(); + + vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY | + (opts.force ? NTFS_MNT_RECOVER : 0)); + if (!vol) { + ntfs_log_perror("ERROR: couldn't mount volume"); + return 1; + } + + if (opts.inode != -1) + inode = ntfs_inode_open(vol, opts.inode); + else { +#ifdef HAVE_WINDOWS_H + char *unix_name; + + unix_name = ntfs_utils_unix_path(opts.file); + if (unix_name) { + inode = ntfs_pathname_to_inode(vol, NULL, + unix_name); + free(unix_name); + } else + inode = (ntfs_inode*)NULL; +#else + inode = ntfs_pathname_to_inode(vol, NULL, opts.file); +#endif + } + + if (!inode) { + ntfs_log_perror("ERROR: Couldn't open inode"); + return 1; + } + + attr = AT_DATA; + if (opts.attr != const_cpu_to_le32(-1)) + attr = opts.attr; + + result = cat(vol, inode, attr, opts.attr_name, opts.attr_name_len); + + ntfs_inode_close(inode); + ntfs_umount(vol, FALSE); + + return result; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscat.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscat.h new file mode 100644 index 0000000..cf474b4 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscat.h @@ -0,0 +1,46 @@ +/* + * ntfscat - Part of the Linux-NTFS project. + * + * Copyright (c) 2003 Richard Russon + * Copyright (c) 2003 Anton Altaparmakov + * + * This utility will concatenate files and print on the standard output. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFSCAT_H_ +#define _NTFSCAT_H_ + +#include "types.h" +#include "layout.h" + +struct options { + char *device; /* Device/File to work with */ + char *file; /* File to display */ + s64 inode; /* Inode to work with */ + ATTR_TYPES attr; /* Attribute type to display */ + ntfschar *attr_name; /* Attribute name to display */ + int attr_name_len; /* Attribute name length */ + int force; /* Override common sense */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + BOOL raw; /* Raw data output */ +}; + +#endif /* _NTFSCAT_H_ */ + + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsck.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsck.c new file mode 100644 index 0000000..8c12641 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsck.c @@ -0,0 +1,884 @@ +/** + * ntfsck - Part of the Linux-NTFS project. + * + * Copyright (c) 2006 Yuval Fledel + * + * This utility will check and fix errors on an NTFS volume. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif + +#include +#include +#include +#include +#include + +#include "cluster.h" +#include "utils.h" + +#define RETURN_FS_ERRORS_CORRECTED (1) +#define RETURN_SYSTEM_NEEDS_REBOOT (2) +#define RETURN_FS_ERRORS_LEFT_UNCORRECTED (4) +#define RETURN_OPERATIONAL_ERROR (8) +#define RETURN_USAGE_OR_SYNTAX_ERROR (16) +#define RETURN_CANCELLED_BY_USER (32) +/* Where did 64 go? */ +#define RETURN_SHARED_LIBRARY_ERROR (128) + +/* todo: command line: (everything is optional) + * fsck-frontend options: + * -C [fd] : display progress bar (send it to the file descriptor if specified) + * -T : don't show the title on startup + * fsck-checker options: + * -a : auto-repair. no questions. (optional: if marked clean and -f not specified, just check if mounable) + * -p : auto-repair safe. no questions (optional: same) + * -n : only check. no repair. + * -r : interactively repair. + * -y : always yes. + * -v : verbose. + * -V : version. + * taken from fsck.ext2 + * -b sb : use the superblock from sb. For corrupted volumes. (do we want separete boot/mft options?) + * -c : use badblocks(8) to find bad blocks (R/O mode) and add the findings to $Bad. + * -C fd : write competion info to fd. If 0, print a completion bar. + * -d : debugging output. + * -D : rebalance indices. + * -f : force checking even if marked clean. + * -F : flush buffers before beginning. (for time-benchmarking) + * -k : When used with -c, don't erase previous $Bad items. + * -n : Open fs as readonly. assume always no. (why is it needed if -r is not specified?) + * -t : Print time statistics. + * taken from fsck.reiserfs + * --rebuild-sb : try to find $MFT start and rebuild the boot sector. + * --rebuild-tree : scan for items and rebuild the indices that point to them (0x30, $SDS, etc.) + * --clean-reserved: zero rezerved fields. (use with care!) + * --adjust-size -z: insert a sparse hole if the data_size is larger than the size marked in the runlist. + * --logfile file : report corruptions (unlike other errors) to a file instead of stderr. + * --nolog : don't report corruptions at all. + * --quiet -q : no progress bar. + * taken from fsck.msdos + * -w : flush after every write. + * - do n passes. (only 2 in fsck.msdos. second should not report errors. Bonus: stop when error list does not change) + * taken from fsck.jfs + * --omit-journal-reply: self-descriptive (why would someone do that?) + * --replay-journal-only: self-descriptive. don't check otherwise. + * taken from fsck.xfs + * -s : only serious errors should be reported. + * -i ino : verbose behaviour only for inode ino. + * -b bno : verbose behaviour only for cluster bno. + * -L : zero log. + * inspired by others + * - don't do cluster accounting. + * - don't do mft record accounting. + * - don't do file names accounting. + * - don't do security_id accounting. + * - don't check acl inheritance problems. + * - undelete unused mft records. (bonus: different options for 100% salvagable and less) + * - error-level-report n: only report errors above this error level + * - error-level-repair n: only repair errors below this error level + * - don't fail on ntfsclone metadata pruning. + * signals: + * SIGUSR1 : start displaying progress bar + * SIGUSR2 : stop displaying progress bar. + */ + +/* Assuming NO_NTFS_DEVICE_DEFAULT_IO_OPS is not set */ + +static int errors = 0; +static int unsupported = 0; + +static short bytes_per_sector, sectors_per_cluster; +//static s64 mft_offset, mftmirr_offset; +static s64 current_mft_record; + +/** + * This is just a preliminary volume. + * Filled while checking the boot sector and used in the preliminary MFT check. + */ +//static ntfs_volume vol; + +static runlist_element *mft_rl, *mft_bitmap_rl; + +#define check_failed(FORMAT, ARGS...) \ + do { \ + errors++; \ + ntfs_log_redirect(__FUNCTION__,__FILE__,__LINE__, \ + NTFS_LOG_LEVEL_ERROR,NULL,FORMAT,##ARGS); \ + } while (0); + +/** + * 0 success. + * 1 fail. + */ +static int assert_u32_equal(u32 val, u32 ok, const char *name) +{ + if (val!=ok) { + check_failed("Assertion failed for '%lld:%s'. should be 0x%x, " + "was 0x%x.\n", (long long)current_mft_record, name, + (int)ok, (int)val); + //errors++; + return 1; + } + return 0; +} + +static int assert_u32_noteq(u32 val, u32 wrong, const char *name) +{ + if (val==wrong) { + check_failed("Assertion failed for '%lld:%s'. should not be " + "0x%x.\n", (long long)current_mft_record, name, + (int)wrong); + return 1; + } + return 0; +} + +static int assert_u32_lesseq(u32 val1, u32 val2, const char *name) +{ + if (val1 > val2) { + check_failed("Assertion failed for '%s'. 0x%x > 0x%x\n", + name, (int)val1, (int)val2); + //errors++; + return 1; + } + return 0; +} + +static int assert_u32_less(u32 val1, u32 val2, const char *name) +{ + if (val1 >= val2) { + check_failed("Assertion failed for '%s'. 0x%x >= 0x%x\n", + name, (int)val1, (int)val2); + //errors++; + return 1; + } + return 0; +} + +/** + * Return: 0 ok, 1 error. + * + * todo: may we use ntfs_boot_sector_is_ntfs() instead? + * It already does the checks but will not be able to fix anything. + */ +static BOOL verify_boot_sector(struct ntfs_device *dev, ntfs_volume *rawvol) +{ + u8 buf[512]; + NTFS_BOOT_SECTOR *ntfs_boot = (NTFS_BOOT_SECTOR *)&buf; + //u32 bytes_per_cluster; + + current_mft_record = 9; + + if (ntfs_pread(dev, 0, sizeof(buf), buf) != sizeof(buf)) { + check_failed("Failed to read boot sector.\n"); + return 1; + } + + if ((buf[0]!=0xeb) || + ((buf[1]!=0x52) && (buf[1]!=0x5b)) || + (buf[2]!=0x90)) { + check_failed("Boot sector: Bad jump.\n"); + } + if (ntfs_boot->oem_id != magicNTFS) { + check_failed("Boot sector: Bad NTFS magic.\n"); + } + bytes_per_sector = le16_to_cpu(ntfs_boot->bpb.bytes_per_sector); + if (!bytes_per_sector) { + check_failed("Boot sector: Bytes per sector is 0.\n"); + } + if (bytes_per_sector%512) { + check_failed("Boot sector: Bytes per sector is not a multiple" + " of 512.\n"); + } + sectors_per_cluster = ntfs_boot->bpb.sectors_per_cluster; + + // todo: if partition, query bios and match heads/tracks? */ + + // Initialize some values into rawvol. We will need those later. + rawvol->dev = dev; + ntfs_boot_sector_parse(rawvol, (NTFS_BOOT_SECTOR *)buf); + + return 0; +} + +/** + * Load the runlist of the attribute. + * + * Return NULL if an error. + * The caller is responsible on freeing the allocated memory if the result is not NULL. + * + * Set size_of_file_record to some reasonable size when in doubt (the Windows default is 1024.) + * + * attr_type must be little endian. + * + * This function has code duplication with check_file_record() and + * check_attr_record() but its goal is to be less strict. Thus the + * duplicated checks are the minimal required for not crashing. + * + * Assumes dev is open. + */ +static runlist *load_runlist(ntfs_volume *rawvol, s64 offset_to_file_record, ATTR_TYPES attr_type, u32 size_of_file_record) +{ + u8 *buf; + u16 attrs_offset; + u32 length; + ATTR_RECORD *attr_rec; + + if (size_of_file_record<22) // offset to attrs_offset + return NULL; + + buf = (u8*)ntfs_malloc(size_of_file_record); + if (!buf) + return NULL; + + if (ntfs_pread(rawvol->dev, offset_to_file_record, size_of_file_record, buf) != + size_of_file_record) { + check_failed("Failed to read file record at offset %lld (0x%llx).\n", + (long long)offset_to_file_record, + (long long)offset_to_file_record); + return NULL; + } + + attrs_offset = le16_to_cpu(((MFT_RECORD*)buf)->attrs_offset); + // first attribute must be after the header. + if (attrs_offset<42) { + check_failed("First attribute must be after the header (%u).\n", (int)attrs_offset); + } + attr_rec = (ATTR_RECORD *)(buf + attrs_offset); + //printf("uv1.\n"); + + while ((u8*)attr_rec<=buf+size_of_file_record-4) { + + //printf("Attr type: 0x%x.\n", attr_rec->type); + // Check attribute record. (Only what is in the buffer) + if (attr_rec->type==AT_END) { + check_failed("Attribute 0x%x not found in file record at offset %lld (0x%llx).\n", (int)le32_to_cpu(attr_rec->type), + (long long)offset_to_file_record, + (long long)offset_to_file_record); + return NULL; + } + if ((u8*)attr_rec>buf+size_of_file_record-8) { + // not AT_END yet no room for the length field. + check_failed("Attribute 0x%x is not AT_END, yet no " + "room for the length field.\n", + (int)le32_to_cpu(attr_rec->type)); + return NULL; + } + + length = le32_to_cpu(attr_rec->length); + + // Check that this attribute does not overflow the mft_record + if ((u8*)attr_rec+length >= buf+size_of_file_record) { + check_failed("Attribute (0x%x) is larger than FILE record at offset %lld (0x%llx).\n", + (int)le32_to_cpu(attr_rec->type), + (long long)offset_to_file_record, + (long long)offset_to_file_record); + return NULL; + } + // todo: what ATTRIBUTE_LIST (0x20)? + + if (attr_rec->type==attr_type) { + // Eurika! + + // ntfs_mapping_pairs_decompress only use two values from vol. Just fake it. + // todo: it will also use vol->major_ver if defined(DEBUG). But only for printing purposes. + + // Assume ntfs_boot_sector_parse() was called. + return ntfs_mapping_pairs_decompress(rawvol, attr_rec, NULL); + } + + attr_rec = (ATTR_RECORD*)((u8*)attr_rec+length); + } + // If we got here, there was an overflow. + check_failed("file record corrupted at offset %lld (0x%llx).\n", + (long long)offset_to_file_record, + (long long)offset_to_file_record); + return NULL; +} + +/** + * Return: >=0 last VCN + * LCN_EINVAL error. + */ +static VCN get_last_vcn(runlist *rl) +{ + VCN res; + + if (!rl) + return LCN_EINVAL; + + res = LCN_EINVAL; + while (rl->length) { + ntfs_log_verbose("vcn: %lld, length: %lld.\n", + (long long)rl->vcn, (long long)rl->length); + if (rl->vcn<0) + res = rl->vcn; + else + res = rl->vcn + rl->length; + rl++; + } + + return res; +} + +static u32 mft_bitmap_records; +static u8 *mft_bitmap_buf; + +/** + * Assumes mft_bitmap_rl is initialized. + * return: 0 ok. + * RETURN_OPERATIONAL_ERROR on error. + */ +static int mft_bitmap_load(ntfs_volume *rawvol) +{ + VCN vcn; + u32 mft_bitmap_length; + + vcn = get_last_vcn(mft_bitmap_rl); + if (vcn<=LCN_EINVAL) { + mft_bitmap_buf = NULL; + /* This case should not happen, not even with on-disk errors */ + goto error; + } + + mft_bitmap_length = vcn * rawvol->cluster_size; + mft_bitmap_records = 8 * mft_bitmap_length * rawvol->cluster_size / + rawvol->mft_record_size; + + //printf("sizes: %d, %d.\n", mft_bitmap_length, mft_bitmap_records); + + mft_bitmap_buf = (u8*)ntfs_malloc(mft_bitmap_length); + if (!mft_bitmap_buf) + goto error; + if (ntfs_rl_pread(rawvol, mft_bitmap_rl, 0, mft_bitmap_length, + mft_bitmap_buf)!=mft_bitmap_length) + goto error; + return 0; +error: + mft_bitmap_records = 0; + ntfs_log_error("Could not load $MFT/Bitmap.\n"); + return RETURN_OPERATIONAL_ERROR; +} + +/** + * -1 Error. + * 0 Unused record + * 1 Used record + * + * Assumes mft_bitmap_rl was initialized. + */ +static int mft_bitmap_get_bit(s64 mft_no) +{ + if (mft_no>=mft_bitmap_records) + return -1; + return ntfs_bit_get(mft_bitmap_buf, mft_no); +} + +/** + * @attr_rec: The attribute record to check + * @mft_rec: The parent FILE record. + * @buflen: The size of the FILE record. + * + * Return: + * NULL: Fatal error occured. Not sure where is the next record. + * otherwise: pointer to the next attribute record. + * + * The function only check fields that are inside this attr record. + * + * Assumes mft_rec is current_mft_record. + */ +static ATTR_REC *check_attr_record(ATTR_REC *attr_rec, MFT_RECORD *mft_rec, + u16 buflen) +{ + u16 name_offset; + u16 attrs_offset = le16_to_cpu(mft_rec->attrs_offset); + u32 attr_type = le32_to_cpu(attr_rec->type); + u32 length = le32_to_cpu(attr_rec->length); + + // Check that this attribute does not overflow the mft_record + if ((u8*)attr_rec+length >= ((u8*)mft_rec)+buflen) { + check_failed("Attribute (0x%x) is larger than FILE record (%lld).\n", + (int)attr_type, (long long)current_mft_record); + return NULL; + } + + // Attr type must be a multiple of 0x10 and 0x10<=x<=0x100. + if ((attr_type & ~0x0F0) && (attr_type != 0x100)) { + check_failed("Unknown attribute type 0x%x.\n", + (int)attr_type); + goto check_attr_record_next_attr; + } + + if (length<24) { + check_failed("Attribute %lld:0x%x Length too short (%u).\n", + (long long)current_mft_record, (int)attr_type, + (int)length); + goto check_attr_record_next_attr; + } + + // If this is the first attribute: + // todo: instance number must be smaller than next_instance. + if ((u8*)attr_rec == ((u8*)mft_rec) + attrs_offset) { + if (!mft_rec->base_mft_record) + assert_u32_equal(attr_type, 0x10, + "First attribute type"); + // The following not always holds. + // attr 0x10 becomes instance 1 and attr 0x40 becomes 0. + //assert_u32_equal(attr_rec->instance, 0, + // "First attribute instance number"); + } else { + assert_u32_noteq(attr_type, 0x10, + "Not-first attribute type"); + // The following not always holds. + //assert_u32_noteq(attr_rec->instance, 0, + // "Not-first attribute instance number"); + } + //if (current_mft_record==938 || current_mft_record==1683 || current_mft_record==3152 || current_mft_record==22410) + //printf("Attribute %lld:0x%x instance: %u isbase:%d.\n", + // current_mft_record, (int)attr_type, (int)le16_to_cpu(attr_rec->instance), (int)mft_rec->base_mft_record); + // todo: instance is unique. + + // Check flags. + if (attr_rec->flags & ~(const_cpu_to_le16(0xc0ff))) { + check_failed("Attribute %lld:0x%x Unknown flags (0x%x).\n", + (long long)current_mft_record, (int)attr_type, + (int)le16_to_cpu(attr_rec->flags)); + } + + if (attr_rec->non_resident>1) { + check_failed("Attribute %lld:0x%x Unknown non-resident " + "flag (0x%x).\n", (long long)current_mft_record, + (int)attr_type, (int)attr_rec->non_resident); + goto check_attr_record_next_attr; + } + + name_offset = le16_to_cpu(attr_rec->name_offset); + /* + * todo: name must be legal unicode. + * Not really, information below in urls is about filenames, but I + * believe it also applies to attribute names. (Yura) + * http://blogs.msdn.com/michkap/archive/2006/09/24/769540.aspx + * http://blogs.msdn.com/michkap/archive/2006/09/10/748699.aspx + */ + + if (attr_rec->non_resident) { + // Non-Resident + + // Make sure all the fields exist. + if (length<64) { + check_failed("Non-resident attribute %lld:0x%x too short (%u).\n", + (long long)current_mft_record, (int)attr_type, + (int)length); + goto check_attr_record_next_attr; + } + if (attr_rec->compression_unit && (length<72)) { + check_failed("Compressed attribute %lld:0x%x too short (%u).\n", + (long long)current_mft_record, (int)attr_type, + (int)length); + goto check_attr_record_next_attr; + } + + // todo: name comes before mapping pairs, and after the header. + // todo: length==mapping_pairs_offset+length of compressed mapping pairs. + // todo: mapping_pairs_offset is 8-byte aligned. + + // todo: lowest vcn <= highest_vcn + // todo: if base record -> lowest vcn==0 + // todo: lowest_vcn!=0 -> attribute list is used. + // todo: lowest_vcn & highest_vcn are in the drive (0<=xvalue_offset); + u32 value_length = le32_to_cpu(attr_rec->value_length); + // Resident + if (attr_rec->name_length) { + if (name_offset < 24) + check_failed("Resident attribute with " + "name intersecting header.\n"); + if (value_offset < name_offset + + attr_rec->name_length) + check_failed("Named resident attribute " + "with value before name.\n"); + } + // if resident, length==value_length+value_offset + //assert_u32_equal(le32_to_cpu(attr_rec->value_length)+ + // value_offset, length, + // "length==value_length+value_offset"); + // if resident, length==value_length+value_offset + if (value_length+value_offset > length) { + check_failed("value_length(%d)+value_offset(%d)>length(%d) for attribute 0x%x.\n", (int)value_length, (int)value_offset, (int)length, (int)attr_type); + return NULL; + } + + // Check resident_flags. + if (attr_rec->resident_flags>0x01) { + check_failed("Unknown resident flags (0x%x) for attribute 0x%x.\n", (int)attr_rec->resident_flags, (int)attr_type); + } else if (attr_rec->resident_flags && (attr_type!=0x30)) { + check_failed("Resident flags mark attribute 0x%x as indexed.\n", (int)attr_type); + } + + // reservedR is 0. + assert_u32_equal(attr_rec->reservedR, 0, "Resident Reserved"); + + // todo: attribute must not be 0xa0 (not sure about 0xb0, 0xe0, 0xf0) + // todo: check content well-formness per attr_type. + } + return 0; +check_attr_record_next_attr: + return (ATTR_REC *)(((u8 *)attr_rec) + length); +} + +/** + * All checks that can be satisfied only by data from the buffer. + * No other [MFT records/metadata files] are required. + * + * The buffer is changed by removing the Update Sequence. + * + * Return: + * 0 Everything's cool. + * else Consider this record as damaged. + */ +static BOOL check_file_record(u8 *buffer, u16 buflen) +{ + u16 usa_count, usa_ofs, attrs_offset, usa; + u32 bytes_in_use, bytes_allocated, i; + MFT_RECORD *mft_rec = (MFT_RECORD *)buffer; + ATTR_REC *attr_rec; + + // check record magic + assert_u32_equal(le32_to_cpu(mft_rec->magic), le32_to_cpu(magic_FILE), "FILE record magic"); + // todo: records 16-23 must be filled in order. + // todo: what to do with magic_BAAD? + + // check usa_count+offset to update seq <= attrs_offset < + // bytes_in_use <= bytes_allocated <= buflen. + usa_ofs = le16_to_cpu(mft_rec->usa_ofs); + usa_count = le16_to_cpu(mft_rec->usa_count); + attrs_offset = le16_to_cpu(mft_rec->attrs_offset); + bytes_in_use = le32_to_cpu(mft_rec->bytes_in_use); + bytes_allocated = le32_to_cpu(mft_rec->bytes_allocated); + if (assert_u32_lesseq(usa_ofs+usa_count, attrs_offset, + "usa_ofs+usa_count <= attrs_offset") || + assert_u32_less(attrs_offset, bytes_in_use, + "attrs_offset < bytes_in_use") || + assert_u32_lesseq(bytes_in_use, bytes_allocated, + "bytes_in_use <= bytes_allocated") || + assert_u32_lesseq(bytes_allocated, buflen, + "bytes_allocated <= max_record_size")) { + return 1; + } + + + // We should know all the flags. + if (le16_to_cpu(mft_rec->flags) > 0xf) { + check_failed("Unknown MFT record flags (0x%x).\n", + (unsigned int)le16_to_cpu(mft_rec->flags)); + } + // todo: flag in_use must be on. + + // Remove update seq & check it. + usa = *(u16*)(buffer+usa_ofs); // The value that should be at the end of every sector. + if (assert_u32_equal(usa_count-1, buflen/NTFS_BLOCK_SIZE, "USA length")) + return (1); + for (i=1;itype==AT_END) { + // Done. + return 0; + } + if ((u8*)attr_rec>buffer+buflen-8) { + // not AT_END yet no room for the length field. + check_failed("Attribute 0x%x is not AT_END, yet no " + "room for the length field.\n", + (int)le32_to_cpu(attr_rec->type)); + return 1; + } + + attr_rec = check_attr_record(attr_rec, mft_rec, buflen); + if (!attr_rec) + return 1; + } + // If we got here, there was an overflow. + return 1; + + // todo: an attribute should be at the offset to first attribute, and the offset should be inside the buffer. It should have the value of "next attribute id". + // todo: if base record, it should start with attribute 0x10. + + // Highlevel check of attributes. + // todo: Attributes are well-formed. + // todo: Room for next attribute in the end of the previous record. + + return FALSE; +} + +static void replay_log(ntfs_volume *vol __attribute__((unused))) +{ + // At this time, only check that the log is fully replayed. + ntfs_log_warning("Unsupported: replay_log()\n"); + // todo: if logfile is clean, return success. + unsupported++; +} + +static void verify_mft_record(ntfs_volume *vol, s64 mft_num) +{ + u8 *buffer; + int is_used; + + current_mft_record = mft_num; + + is_used = mft_bitmap_get_bit(mft_num); + if (is_used<0) { + ntfs_log_error("Error getting bit value for record %lld.\n", + (long long)mft_num); + } else if (!is_used) { + ntfs_log_verbose("Record %lld unused. Skipping.\n", + (long long)mft_num); + return; + } + + buffer = ntfs_malloc(vol->mft_record_size); + if (!buffer) + goto verify_mft_record_error; + + ntfs_log_verbose("MFT record %lld\n", (long long)mft_num); + if (ntfs_attr_pread(vol->mft_na, mft_num*vol->mft_record_size, vol->mft_record_size, buffer) < 0) { + ntfs_log_perror("Couldn't read $MFT record %lld", (long long)mft_num); + goto verify_mft_record_error; + } + + check_file_record(buffer, vol->mft_record_size); + // todo: if offset to first attribute >= 0x30, number of mft record should match. + // todo: Match the "record is used" with the mft bitmap. + // todo: if this is not base, check that the parent is a base, and is in use, and pointing to this record. + + // todo: if base record: for each extent record: + // todo: verify_file_record + // todo: hard link count should be the number of 0x30 attributes. + // todo: Order of attributes. + // todo: make sure compression_unit is the same. + + return; +verify_mft_record_error: + + if (buffer) + free(buffer); + errors++; +} + +/** + * This function serves as bootstraping for the more comprehensive checks. + * It will load the MFT runlist and MFT/Bitmap runlist. + * It should not depend on other checks or we may have a circular dependancy. + * Also, this loadng must be forgiving, unlike the comprehensive checks. + */ +static int verify_mft_preliminary(ntfs_volume *rawvol) +{ + current_mft_record = 0; + s64 mft_offset, mftmirr_offset; + int res; + + ntfs_log_trace("Entering verify_mft_preliminary().\n"); + // todo: get size_of_file_record from boot sector + // Load the first segment of the $MFT/DATA runlist. + mft_offset = rawvol->mft_lcn * rawvol->cluster_size; + mftmirr_offset = rawvol->mftmirr_lcn * rawvol->cluster_size; + mft_rl = load_runlist(rawvol, mft_offset, AT_DATA, 1024); + if (!mft_rl) { + check_failed("Loading $MFT runlist failed. Trying $MFTMirr.\n"); + mft_rl = load_runlist(rawvol, mftmirr_offset, AT_DATA, 1024); + } + if (!mft_rl) { + check_failed("Loading $MFTMirr runlist failed too. Aborting.\n"); + return RETURN_FS_ERRORS_LEFT_UNCORRECTED | RETURN_OPERATIONAL_ERROR; + } + // TODO: else { recover $MFT } // Use $MFTMirr to recover $MFT. + // todo: support loading the next runlist extents when ATTRIBUTE_LIST is used on $MFT. + // If attribute list: Gradually load mft runlist. (parse runlist from first file record, check all referenced file records, continue with the next file record). If no attribute list, just load it. + + // Load the runlist of $MFT/Bitmap. + // todo: what about ATTRIBUTE_LIST? Can we reuse code? + mft_bitmap_rl = load_runlist(rawvol, mft_offset, AT_BITMAP, 1024); + if (!mft_bitmap_rl) { + check_failed("Loading $MFT/Bitmap runlist failed. Trying $MFTMirr.\n"); + mft_bitmap_rl = load_runlist(rawvol, mftmirr_offset, AT_BITMAP, 1024); + } + if (!mft_bitmap_rl) { + check_failed("Loading $MFTMirr/Bitmap runlist failed too. Aborting.\n"); + return RETURN_FS_ERRORS_LEFT_UNCORRECTED; + // todo: rebuild the bitmap by using the "in_use" file record flag or by filling it with 1's. + } + + /* Load $MFT/Bitmap */ + if ((res = mft_bitmap_load(rawvol))) + return res; + return -1; /* FIXME: Just added to fix compiler warning without + thinking about what should be here. (Yura) */ +} + +static void check_volume(ntfs_volume *vol) +{ + s64 mft_num, nr_mft_records; + + ntfs_log_warning("Unsupported: check_volume()\n"); + unsupported++; + + // For each mft record, verify that it contains a valid file record. + nr_mft_records = vol->mft_na->initialized_size >> + vol->mft_record_size_bits; + ntfs_log_info("Checking %lld MFT records.\n", (long long)nr_mft_records); + + for (mft_num=0; mft_num < nr_mft_records; mft_num++) { + verify_mft_record(vol, mft_num); + } + + // todo: Check metadata files. + + // todo: Second pass on mft records. Now check the contents as well. + // todo: When going through runlists, build a bitmap. + + // todo: cluster accounting. + return; +} + +static int reset_dirty(ntfs_volume *vol) +{ + le16 flags; + + if (!(vol->flags | VOLUME_IS_DIRTY)) + return 0; + + ntfs_log_verbose("Resetting dirty flag.\n"); + + flags = vol->flags & ~VOLUME_IS_DIRTY; + + if (ntfs_volume_write_flags(vol, flags)) { + ntfs_log_error("Error setting volume flags.\n"); + return -1; + } + return 0; +} + +/** + * main - Does just what C99 claim it does. + * + * For more details on arguments and results, check the man page. + */ +int main(int argc, char **argv) +{ + struct ntfs_device *dev; + ntfs_volume rawvol; + ntfs_volume *vol; + const char *name; + int ret; + + if (argc != 2) + return RETURN_USAGE_OR_SYNTAX_ERROR; + name = argv[1]; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + //ntfs_log_set_levels(NTFS_LOG_LEVEL_DEBUG | NTFS_LOG_LEVEL_TRACE | NTFS_LOG_LEVEL_QUIET | NTFS_LOG_LEVEL_INFO | NTFS_LOG_LEVEL_VERBOSE | NTFS_LOG_LEVEL_PROGRESS); + + /* Allocate an ntfs_device structure. */ + dev = ntfs_device_alloc(name, 0, &ntfs_device_default_io_ops, NULL); + if (!dev) + return RETURN_OPERATIONAL_ERROR; + if (dev->d_ops->open(dev, O_RDONLY)) { //O_RDWR/O_RDONLY? + ntfs_log_perror("Error opening partition device"); + ntfs_device_free(dev); + return RETURN_OPERATIONAL_ERROR; + } + + if ((ret = verify_boot_sector(dev,&rawvol))) { + dev->d_ops->close(dev); + return ret; + } + ntfs_log_verbose("Boot sector verification complete. Proceeding to $MFT"); + + verify_mft_preliminary(&rawvol); + + /* ntfs_device_mount() expects the device to be closed. */ + if (dev->d_ops->close(dev)) + ntfs_log_perror("Failed to close the device."); + + // at this point we know that the volume is valid enough for mounting. + + /* Call ntfs_device_mount() to do the actual mount. */ + vol = ntfs_device_mount(dev, NTFS_MNT_RDONLY); + if (!vol) { + ntfs_device_free(dev); + return 2; + } + + replay_log(vol); + + if (vol->flags & VOLUME_IS_DIRTY) + ntfs_log_warning("Volume is dirty.\n"); + + check_volume(vol); + + if (errors) + ntfs_log_info("Errors found.\n"); + if (unsupported) + ntfs_log_info("Unsupported cases found.\n"); + + if (!errors && !unsupported) { + reset_dirty(vol); + } + + ntfs_umount(vol, FALSE); + + if (errors) + return 2; + if (unsupported) + return 1; + return 0; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsclone.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsclone.8.in new file mode 100644 index 0000000..aa04dde --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsclone.8.in @@ -0,0 +1,397 @@ +.\" Copyright (c) 2003\-2005 Richard Russon. +.\" Copyright (c) 2003\-2006 Szabolcs Szakacsits. +.\" Copyright (c) 2004 Per Olofsson. +.\" Copyright (c) 2010\-2013 Jean-Pierre Andre. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSCLONE 8 "February 2013" "ntfs-3g @VERSION@" +.SH NAME +ntfsclone \- Efficiently clone, image, restore or rescue an NTFS +.SH SYNOPSIS +.B ntfsclone +[\fIOPTIONS\fR] +.I SOURCE +.br +.B ntfsclone \-\-save\-image +[\fIOPTIONS\fR] +.I SOURCE +.br +.B ntfsclone \-\-restore\-image +[\fIOPTIONS\fR] +.I SOURCE +.br +.B ntfsclone \-\-metadata +[\fIOPTIONS\fR] +.I SOURCE +.SH DESCRIPTION +.B ntfsclone +will efficiently clone (copy, save, backup, restore) or rescue an NTFS +filesystem to a sparse file, image, device (partition) or standard output. +It works at disk sector level and +copies only the used data. Unused disk space becomes zero (cloning to +sparse file), encoded with control codes (saving in special image format), +left unchanged (cloning to a disk/partition) or +filled with zeros (cloning to standard output). + +.B ntfsclone +can be useful to make backups, an exact snapshot of an NTFS filesystem +and restore it later on, or for developers to test NTFS read/write +functionality, troubleshoot/investigate users' issues using the clone +without the risk of destroying the original filesystem. + +The clone, if not using the special image format, is an exact copy of the +original NTFS filesystem from sector to sector thus it can be also mounted +just like the original NTFS filesystem. +For example if you clone to a file and the kernel has loopback device and +NTFS support then the file can be mounted as +.RS +.sp +.B mount \-t ntfs \-o loop ntfsclone.img /mnt/ntfsclone +.sp +.RE +.SS Windows Cloning +If you want to copy, move or restore a system or boot partition to another +computer, or to a different disk or partition (e.g. hda1\->hda2, hda1\->hdb1 +or to a different disk sector offset) then you will need to take extra care. + +Usually, Windows will not be able to boot, unless you copy, move or restore +NTFS to the same partition which starts at the same sector on the same type +of disk having the same BIOS legacy cylinder setting as the original +partition and disk had. + +The ntfsclone utility guarantees to make an exact copy of NTFS but it +won't deal with booting issues. This is by design: ntfsclone is a +filesystem, not system utility. Its aim is only NTFS cloning, not Windows +cloning. Hereby ntfsclone can be used as a very fast and reliable +build block for Windows cloning but itself it's not enough. +.SS Sparse Files +A file is sparse if it has unallocated blocks (holes). The reported size of +such files are always higher than the disk space consumed by them. The +.BR du +command can tell the real disk space used by a sparse file. +The holes are always read as zeros. All major Linux filesystem like, +ext2, ext3, reiserfs, Reiser4, JFS and XFS, supports +sparse files but for example the ISO 9600 CD\-ROM filesystem doesn't. +.SS Handling Large Sparse Files +As of today Linux provides inadequate support for managing (tar, +cp, gzip, gunzip, bzip2, bunzip2, cat, etc) large sparse files. +The only main Linux filesystem +having support for efficient sparse file handling is XFS by the +XFS_IOC_GETBMAPX +.BR ioctl (2) . +However none of the common utilities supports it. +This means when you tar, cp, gzip, bzip2, etc a large sparse file +they will always read the entire file, even if you use the "sparse support" +options. + +.BR bzip2 (1) +compresses large sparse files much better than +.BR gzip (1) +but it does so +also much slower. Moreover neither of them handles large sparse +files efficiently during uncompression from disk space usage point +of view. + +At present the most efficient way, both speed and space\-wise, to +compress and uncompress large sparse files by common tools +would be using +.BR tar (1) +with the options +.B \-S +(handle sparse files "efficiently") and +.B \-j +(filter the archive through bzip2). Although +.BR tar +still reads and analyses the entire file, it doesn't pass on the +large data blocks having only zeros to filters and it also avoids +writing large amount of zeros to the disk needlessly. But since +.BR tar +can't create an archive from the standard input, you can't do this +in\-place by just reading +.BR ntfsclone +standard output. Even more sadly, using the \-S option results +serious data loss since the end of 2004 and the GNU +.BR tar +maintainers didn't release fixed versions until the present day. +.SS The Special Image Format +It's also possible, actually it's recommended, to save an NTFS filesystem +to a special image format. +Instead of representing unallocated blocks as holes, they are +encoded using control codes. Thus, the image saves space without +requiring sparse file support. The image format is ideal for streaming +filesystem images over the network and similar, and can be used as a +replacement for Ghost or Partition Image if it is combined with other +tools. The downside is that you can't mount the image directly, you +need to restore it first. + +To save an image using the special image format, use the +.B \-s +or the +.B \-\-save\-image +option. To restore an image, use the +.B \-r +or the +.B \-\-restore\-image +option. Note that you can restore images from standard input by +using '\-' as the +.I SOURCE +file. +.SS Metadata\-only Cloning +One of the features of +.BR ntfsclone +is that, it can also save only the NTFS metadata using the option +.B \-m +or +.B \-\-metadata +and the clone still will be +mountable. In this case all non\-metadata file content will be lost and +reading them back will result always zeros. + +The metadata\-only image can be compressed very +well, usually to not more than 1\-8 MB thus it's easy to transfer +for investigation, troubleshooting. + +In this mode of ntfsclone, +.B NONE +of the user's data is saved, including the resident user's data +embedded into metadata. All is filled with zeros. +Moreover all the file timestamps, deleted and unused spaces inside +the metadata are filled with zeros. Thus this mode is inappropriate +for example for forensic analyses. +This mode may be combined with \fB\-\-save\-image\fP to create a +special image format file instead of a sparse file. + +Please note, filenames are not wiped out. They might contain +sensitive information, so think twice before sending such an +image to anybody. +.SH OPTIONS +Below is a summary of all the options that +.B ntfsclone +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.B \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.B "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-o\fR, \fB\-\-output\fR FILE +Clone NTFS to the non\-existent +.IR FILE . +If +.I FILE +is '\-' then clone to the +standard output. This option cannot be used for creating a partition, +use \fB\-\-overwrite\fR for an existing partition. +.TP +\fB\-O\fR, \fB\-\-overwrite\fR FILE +Clone NTFS to +.IR FILE , +which can be an existing partition or a regular file which will be +overwritten if it exists. +.TP +\fB\-s\fR, \fB\-\-save\-image\fR +Save to the special image format. This is the most efficient way space and +speed\-wise if imaging is done to the standard output, e.g. for image +compression, encryption or streaming through a network. +.TP +\fB\-r\fR, \fB\-\-restore\-image\fR +Restore from the special image format specified by +.I SOURCE +argument. If the +.I SOURCE +is '\-' then the image is read from the standard input. +.TP +\fB\-n\fR, \fB\-\-no\-action\fR +Test the consistency of a saved image by simulating its restoring without +writing anything. The NTFS data contained in the image is not tested. +The option \fB\-\-restore\-image\fR must also be present, and the options +\fB\-\-output\fR and \fB\-\-overwrite\fR must be omitted. +.TP +\fB\-\-rescue\fR +Ignore disk read errors so disks having bad sectors, e.g. dying disks, can be +rescued the most efficiently way, with minimal stress on them. Ntfsclone works +at the lowest, sector level in this mode too thus more data can be rescued. +The contents of the unreadable sectors are filled by character '?' and the +beginning of such sectors are marked by "BadSectoR\\0". +.TP +\fB\-m\fR, \fB\-\-metadata\fR +Clone +.B ONLY METADATA +(for NTFS experts). Only cloning to a (sparse) file is allowed, unless used +the option \fB\-\-save\-image\fP is also used. +You can't metadata\-only clone to a device. +.TP +\fB\-\-ignore\-fs\-check\fR +Ignore the result of the filesystem consistency check. This option is allowed +to be used only with the +.B \-\-metadata +option, for the safety of user's data. The clusters which cause the +inconsistency are saved too. +.TP +\fB\-t\fR, \fB\-\-preserve\-timestamps\fR +Do not wipe the timestamps, to be used only with the +.B \-\-metadata +option. +.TP +\fB\-\-full\-logfile\fR +Include the Windows log file in the copy. This is only useful for +extracting metadata, saving or cloning a file system which was not +properly unmounted from Windows. +.TP +\fB\-\-new\-serial\fR, or +.TP +\fB\-\-new\-half\-serial\fR +Set a new random serial number to the clone. The serial number is a 64 +bit number used to identify the device during the mounting process, so +it has to be changed to enable the original file system +and the clone to be mounted at the same time on the same computer. + +The option \fB\-\-new\-half\-serial\fP only changes the upper part of the +serial number, keeping the lower part which is used by Windows unchanged. + +The options \fB\-\-new\-serial\fP and \fB\-\-new\-half\-serial\fP can +only be used when cloning a file system of restoring from an image. + +The serial number is not the volume UUID used by Windows +to locate files which have been moved to another volume. + +.TP +\fB\-f\fR, \fB\-\-force\fR +Forces ntfsclone to proceed if the filesystem is marked +"dirty" for consistency check. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Do not display any progress-bars during operation. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.SH EXIT CODES +The exit code is 0 on success, non\-zero otherwise. +.SH EXAMPLES +Clone NTFS on /dev/hda1 to /dev/hdc1: +.RS +.sp +.B ntfsclone \-\-overwrite /dev/hdc1 /dev/hda1 +.sp +.RE +Save an NTFS to a file in the special image format: +.RS +.sp +.B ntfsclone \-\-save\-image \-\-output backup.img /dev/hda1 +.sp +.RE +Restore an NTFS from a special image file to its original partition: +.RS +.sp +.B ntfsclone \-\-restore\-image \-\-overwrite /dev/hda1 backup.img +.sp +.RE +Save an NTFS into a compressed image file: +.RS +.sp +.B ntfsclone \-\-save\-image \-o \- /dev/hda1 | gzip \-c > backup.img.gz +.sp +.RE +Restore an NTFS volume from a compressed image file: +.RS +.sp +.B gunzip \-c backup.img.gz | \\\\ +.br +.B ntfsclone \-\-restore\-image \-\-overwrite /dev/hda1 \- +.sp +.RE +Backup an NTFS volume to a remote host, using ssh. Please note, that +ssh may ask for a password! +.RS +.sp +.B ntfsclone \-\-save\-image \-\-output \- /dev/hda1 | \\\\ +.br +.B gzip \-c | ssh host 'cat > backup.img.gz' +.sp +.RE +Restore an NTFS volume from a remote host via ssh. Please note, that +ssh may ask for a password! +.RS +.sp +.B ssh host 'cat backup.img.gz' | gunzip \-c | \\\\ +.br +.B ntfsclone \-\-restore\-image \-\-overwrite /dev/hda1 \- +.sp +.RE +Stream an image file from a web server and restore it to a partition: +.RS +.sp +.B wget \-qO \- http://server/backup.img | \\\\ +.br +.B ntfsclone \-\-restore\-image \-\-overwrite /dev/hda1 \- +.sp +.RE +Clone an NTFS volume to a non\-existent file: +.RS +.sp +.B ntfsclone \-\-output ntfs\-clone.img /dev/hda1 +.sp +.RE +Pack NTFS metadata for NTFS experts. Please note that bzip2 runs +very long but results usually at least 10 times smaller archives +than gzip on a sparse file. +.RS +.sp +.B ntfsclone \-\-metadata \-\-output ntfsmeta.img /dev/hda1 +.br +.B bzip2 ntfsmeta.img +.sp +Or, outputting to a compressed image : +.br +.B ntfsclone \-mst \-\-output - /dev/hda1 | bzip2 > ntfsmeta.bz2 +.sp +.RE +Unpacking NTFS metadata into a sparse file: +.RS +.sp +.B bunzip2 \-c ntfsmeta.img.bz2 | \\\\ +.br +.B cp \-\-sparse=always /proc/self/fd/0 ntfsmeta.img +.sp +.RE +.SH KNOWN ISSUES +There are no known problems with +.BR ntfsclone . +If you think you have found a problem then please send an email describing it +to the development team: +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.sp +Sometimes it might appear ntfsclone froze if the clone is on ReiserFS +and even CTRL\-C won't stop it. This is not a bug in ntfsclone, however +it's due to ReiserFS being extremely inefficient creating large +sparse files and not handling signals during this operation. This +ReiserFS problem was improved in kernel 2.4.22. +XFS, JFS and ext3 don't have this problem. +.hy +.SH AUTHORS +.B ntfsclone +was written by Szabolcs Szakacsits with contributions from Per Olofsson +(special image format support) and Anton Altaparmakov. +It was ported to ntfs-3g by Erik Larsson and Jean-Pierre Andre. +.SH AVAILABILITY +.B ntfsclone +is part of the +.B ntfs-3g +package and is available at: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfsresize (8) +.BR ntfsprogs (8) +.BR xfs_copy (8) +.BR debugreiserfs (8) +.BR e2image (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsclone.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsclone.c new file mode 100644 index 0000000..f8709c7 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsclone.c @@ -0,0 +1,2928 @@ +/** + * ntfsclone - Part of the Linux-NTFS project. + * + * Copyright (c) 2003-2006 Szabolcs Szakacsits + * Copyright (c) 2004-2006 Anton Altaparmakov + * Copyright (c) 2010-2018 Jean-Pierre Andre + * Special image format support copyright (c) 2004 Per Olofsson + * + * Clone NTFS data and/or metadata to a sparse file, image, device or stdout. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#include "config.h" + +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_SYS_IOCTL_H +#include +#endif +#ifdef HAVE_SYS_VFS_H +#include +#endif +#ifdef HAVE_SYS_STATVFS_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_SYS_MOUNT_H +#include +#endif + +/* + * FIXME: ntfsclone do bad things about endians handling. Fix it and remove + * this note and define. + */ +#define NTFS_DO_NOT_CHECK_ENDIANS + +#include "param.h" +#include "debug.h" +#include "types.h" +#include "support.h" +#include "endians.h" +#include "bootsect.h" +#include "device.h" +#include "attrib.h" +#include "mst.h" +#include "volume.h" +#include "mft.h" +#include "bitmap.h" +#include "inode.h" +#include "index.h" +#include "dir.h" +#include "runlist.h" +#include "ntfstime.h" +#include "utils.h" +/* #include "version.h" */ +#include "misc.h" + +#if defined(linux) && defined(_IO) && !defined(BLKGETSIZE) +#define BLKGETSIZE _IO(0x12,96) /* Get device size in 512-byte blocks. */ +#endif +#if defined(linux) && defined(_IOR) && !defined(BLKGETSIZE64) +#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* Get device size in bytes. */ +#endif + +#if defined(linux) || defined(__uClinux__) || defined(__sun) \ + || defined(__APPLE__) || defined(__DARWIN__) + /* Make sure the presence of means compiling for Windows */ +#undef HAVE_WINDOWS_H +#endif + +#if defined(__sun) | defined(HAVE_WINDOWS_H) +#define NO_STATFS 1 /* statfs(2) and f_type are not universal */ +#endif + +#ifdef HAVE_WINDOWS_H +/* + * Replacements for functions which do not exist on Windows + */ +int setmode(int, int); /* from msvcrt.dll */ + +#define getpid() (0) +#define srandom(seed) srand(seed) +#define random() rand() +#define fsync(fd) (0) +#define ioctl(fd,code,buf) (-1) +#define ftruncate(fd, size) ntfs_device_win32_ftruncate(dev_out, size) +#define BINWMODE "wb" +#else +#define BINWMODE "w" +#endif + +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +static const char *EXEC_NAME = "ntfsclone"; + +static const char *bad_sectors_warning_msg = +"*************************************************************************\n" +"* WARNING: The disk has one or more bad sectors. This means that damage *\n" +"* has occurred on the disk surface, possibly caused by deterioration of *\n" +"* the physical media, manufacturing faults or other reasons. The *\n" +"* reliability of the disk may stay stable or degrade fast. *\n" +"* Use the --rescue option to efficiently save as much data as possible! *\n" +"*************************************************************************\n"; + +static const char *dirty_volume_msg = +"Volume '%s' is scheduled for a check or it was shutdown \n" +"uncleanly. Please boot Windows or use the --force option to progress.\n"; + +static struct { + int verbose; + int quiet; + int debug; + int force; + int overwrite; + int std_out; + int blkdev_out; /* output file is block device */ + int metadata; /* metadata only cloning */ + int no_action; /* do not really restore */ + int ignore_fs_check; + int rescue; + int save_image; + int new_serial; + int metadata_image; + int preserve_timestamps; + int adjust_sector_size; + int full_logfile; + int restore_image; + char *output; + char *volume; +#ifndef NO_STATFS + struct statfs stfs; +#endif +} opt; + +struct bitmap { + s64 size; + u8 *bm; +}; + +struct progress_bar { + u64 start; + u64 stop; + int resolution; + float unit; +}; + +typedef struct { + ntfs_inode *ni; /* inode being processed */ + ntfs_attr_search_ctx *ctx; /* inode attribute being processed */ + s64 inuse; /* number of clusters in use */ + int more_use; /* possibly allocated clusters */ + LCN current_lcn; +} ntfs_walk_clusters_ctx; + +typedef int (ntfs_walk_op)(ntfs_inode *ni, void *data); + +struct ntfs_walk_cluster { + ntfs_walk_op *inode_op; /* not implemented yet */ + ntfs_walk_clusters_ctx *image; +}; + + +static ntfs_volume *vol = NULL; +static struct bitmap lcn_bitmap; + +static int fd_in; +static int fd_out; +static FILE *stream_out = (FILE*)NULL; +struct ntfs_device *dev_out = (struct ntfs_device*)NULL; +static FILE *msg_out = NULL; + +static int wipe = 0; +static unsigned int nr_used_mft_records = 0; +static unsigned int wiped_unused_mft_data = 0; +static unsigned int wiped_unused_mft = 0; +static unsigned int wiped_resident_data = 0; +static unsigned int wiped_timestamp_data = 0; + +static le64 volume_serial_number; /* new random serial number */ +static u64 full_device_size; /* full size, including the backup boot sector */ + +static BOOL image_is_host_endian = FALSE; + +#define IMAGE_MAGIC "\0ntfsclone-image" +#define IMAGE_MAGIC_SIZE 16 +#define IMAGE_OFFSET_OFFSET 46 /* must be the same for all versions ! */ +#define IMAGE_HDR_ALIGN 8 /* alignment wanted after header */ + +/* This is the first endianness safe format version. */ +#define NTFSCLONE_IMG_VER_MAJOR_ENDIANNESS_SAFE 10 +#define NTFSCLONE_IMG_VER_MINOR_ENDIANNESS_SAFE 0 + +/* + * Set the version to 10.0 to avoid colisions with old ntfsclone which + * stupidly used the volume version as the image version... )-: I hope NTFS + * never reaches version 10.0 and if it does one day I hope no-one is using + * such an old ntfsclone by then... + * + * NOTE: Only bump the minor version if the image format and header are still + * backwards compatible. Otherwise always bump the major version. If in + * doubt, bump the major version. + * + * Moved to 10.1 : Alternate boot sector now saved. Still compatible. + */ +#define NTFSCLONE_IMG_VER_MAJOR 10 +#define NTFSCLONE_IMG_VER_MINOR 1 + +enum { CMD_GAP, CMD_NEXT } ; + +/* All values are in little endian. */ +static struct image_hdr { + char magic[IMAGE_MAGIC_SIZE]; + u8 major_ver; + u8 minor_ver; + /* the following is aligned dangerously (too late...) */ + le32 cluster_size; + le64 device_size; + sle64 nr_clusters; + le64 inuse; + le32 offset_to_image_data; /* From start of image_hdr. */ +} __attribute__((__packed__)) image_hdr; + +static int compare_bitmaps(struct bitmap *a, BOOL copy); + +#define NTFSCLONE_IMG_HEADER_SIZE_OLD \ + (offsetof(struct image_hdr, offset_to_image_data)) + +#define NTFS_MBYTE (1000 * 1000) + +#define ERR_PREFIX "ERROR" +#define PERR_PREFIX ERR_PREFIX "(%d): " +#define NERR_PREFIX ERR_PREFIX ": " + +#define LAST_METADATA_INODE 11 + +#define NTFS_SECTOR_SIZE 512 + +#define rounded_up_division(a, b) (((a) + (b - 1)) / (b)) + +#define read_all(f, p, n) io_all((f), (p), (n), 0) +#define write_all(f, p, n) io_all((f), (p), (n), 1) + +__attribute__((format(printf, 1, 2))) +static void Printf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(msg_out, fmt, ap); + va_end(ap); + fflush(msg_out); +} + +__attribute__((format(printf, 1, 2))) +static void perr_printf(const char *fmt, ...) +{ + va_list ap; + int eo = errno; + + Printf(PERR_PREFIX, eo); + va_start(ap, fmt); + vfprintf(msg_out, fmt, ap); + va_end(ap); + Printf(": %s\n", strerror(eo)); + fflush(msg_out); +} + +__attribute__((format(printf, 1, 2))) +static void err_printf(const char *fmt, ...) +{ + va_list ap; + + Printf(NERR_PREFIX); + va_start(ap, fmt); + vfprintf(msg_out, fmt, ap); + va_end(ap); + fflush(msg_out); +} + +__attribute__((noreturn)) +__attribute__((format(printf, 1, 2))) +static void err_exit(const char *fmt, ...) +{ + va_list ap; + + Printf(NERR_PREFIX); + va_start(ap, fmt); + vfprintf(msg_out, fmt, ap); + va_end(ap); + fflush(msg_out); + if (vol) + ntfs_umount(vol,FALSE); + exit(1); +} + +__attribute__((noreturn)) +__attribute__((format(printf, 1, 2))) +static void perr_exit(const char *fmt, ...) +{ + va_list ap; + int eo = errno; + + Printf(PERR_PREFIX, eo); + va_start(ap, fmt); + vfprintf(msg_out, fmt, ap); + va_end(ap); + Printf(": %s\n", strerror(eo)); + fflush(msg_out); + if (vol) + ntfs_umount(vol,FALSE); + exit(1); +} + + +__attribute__((noreturn)) +static void usage(int ret) +{ + fprintf(stderr, "\nUsage: %s [OPTIONS] SOURCE\n" + " Efficiently clone NTFS to a sparse file, image, device or standard output.\n" + "\n" + " -o, --output FILE Clone NTFS to the non-existent FILE\n" + " -O, --overwrite FILE Clone NTFS to FILE, overwriting if exists\n" + " -s, --save-image Save to the special image format\n" + " -r, --restore-image Restore from the special image format\n" + " --rescue Continue after disk read errors\n" + " -m, --metadata Clone *only* metadata (for NTFS experts)\n" + " -n, --no-action Test restoring, without outputting anything\n" + " --ignore-fs-check Ignore the filesystem check result\n" + " --new-serial Set a new serial number\n" + " --new-half-serial Set a partial new serial number\n" + " -S, --adjust-sector-size\n" + " Adjust NTFS sector size to suit target device\n" + " -t, --preserve-timestamps Do not clear the timestamps\n" + " -q, --quiet Do not display any progress bars\n" + " -f, --force Force to progress (DANGEROUS)\n" + " --full-logfile Include the full logfile in metadata output\n" + " -h, --help Display this help\n" +#ifdef DEBUG + " -d, --debug Show debug information\n" +#endif + " -V, --version Display version information\n" + "\n" + " If FILE is '-' then send the image to the standard output. If SOURCE is '-'\n" + " and --restore-image is used then read the image from the standard input.\n" + "\n", EXEC_NAME); + fprintf(stderr, "%s%s", ntfs_bugs, ntfs_home); + exit(ret); +} + +/** + * version + */ +__attribute__((noreturn)) +static void version(void) +{ + fprintf(stderr, + "Efficiently clone, image, restore or rescue an NTFS Volume.\n\n" + "Copyright (c) 2003-2006 Szabolcs Szakacsits\n" + "Copyright (c) 2004-2006 Anton Altaparmakov\n" + "Copyright (c) 2010-2018 Jean-Pierre Andre\n\n"); + fprintf(stderr, "%s\n%s%s", ntfs_gpl, ntfs_bugs, ntfs_home); + exit(0); +} + +static void parse_options(int argc, char **argv) +{ + static const char *sopt = "-dfhmno:O:qrstV"; + static const struct option lopt[] = { +#ifdef DEBUG + { "debug", no_argument, NULL, 'd' }, +#endif + { "quiet", no_argument, NULL, 'q' }, + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "metadata", no_argument, NULL, 'm' }, + { "no-action", no_argument, NULL, 'n' }, + { "output", required_argument, NULL, 'o' }, + { "overwrite", required_argument, NULL, 'O' }, + { "restore-image", no_argument, NULL, 'r' }, + { "ignore-fs-check", no_argument, NULL, 'C' }, + { "rescue", no_argument, NULL, 'R' }, + { "new-serial", no_argument, NULL, 'I' }, + { "new-half-serial", no_argument, NULL, 'i' }, + { "full-logfile", no_argument, NULL, 'l' }, + { "save-image", no_argument, NULL, 's' }, + { "adjust-sector-size", no_argument, NULL, 'S' }, + { "preserve-timestamps", no_argument, NULL, 't' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + + int c; + + memset(&opt, 0, sizeof(opt)); + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (opt.volume) + usage(1); + opt.volume = argv[optind-1]; + break; + case 'd': + opt.debug++; + break; + case 'q': + opt.quiet++; + break; + case 'f': + opt.force++; + break; + case 'h': + usage(0); + case '?': + usage(1); + case 'i': /* not proposed as a short option */ + opt.new_serial |= 1; + break; + case 'I': /* not proposed as a short option */ + opt.new_serial |= 2; + break; + case 'l': + opt.full_logfile++; + break; + case 'm': + opt.metadata++; + break; + case 'n': + opt.no_action++; + break; + case 'O': + opt.overwrite++; + /* FALLTHRU */ + case 'o': + if (opt.output) + usage(1); + opt.output = optarg; + break; + case 'r': + opt.restore_image++; + break; + case 'C': + opt.ignore_fs_check++; + break; + case 'R': + opt.rescue++; + break; + case 's': + opt.save_image++; + break; + case 'S': + opt.adjust_sector_size++; + break; + case 't': + opt.preserve_timestamps++; + break; + case 'V': + version(); + break; + default: + err_printf("Unknown option '%s'.\n", argv[optind-1]); + usage(1); + } + } + + if (!opt.no_action && (opt.output == NULL)) { + err_printf("You must specify an output file.\n"); + usage(1); + } + + if (!opt.no_action && (strcmp(opt.output, "-") == 0)) + opt.std_out++; + + if (opt.volume == NULL) { + err_printf("You must specify a device file.\n"); + usage(1); + } + + if (!opt.restore_image && !strcmp(opt.volume, "-")) { + err_printf("Only special images can be read from standard input\n"); + usage(1); + } + + if (opt.metadata && opt.save_image) { + opt.metadata_image++; + opt.save_image = 0; + } + + if (opt.metadata && opt.restore_image) + err_exit("Restoring only metadata from an image is not " + "supported!\n"); + + if (opt.metadata && !opt.metadata_image && opt.std_out) + err_exit("Cloning only metadata to stdout isn't supported!\n"); + + if (opt.ignore_fs_check && !opt.metadata && !opt.rescue) + err_exit("Filesystem check can be ignored only for metadata " + "cloning or rescue situations!\n"); + + if (opt.save_image && opt.restore_image) + err_exit("Saving and restoring an image at the same time " + "is not supported!\n"); + + if (opt.no_action && !opt.restore_image) + err_exit("A restoring test requires the restore option!\n"); + + if (opt.no_action && opt.output) + err_exit("A restoring test requires not defining any output!\n"); + + if (!opt.no_action && !opt.std_out) { + struct stat st; +#ifdef HAVE_WINDOWS_H + BOOL blkdev = opt.output[0] && (opt.output[1] == ':') + && !opt.output[2]; + + if (!blkdev && (stat(opt.output, &st) == -1)) { +#else + if (stat(opt.output, &st) == -1) { +#endif + if (errno != ENOENT) + perr_exit("Couldn't access '%s'", opt.output); + } else { + if (!opt.overwrite) + err_exit("Output file '%s' already exists.\n" + "Use option --overwrite if you want to" + " replace its content.\n", opt.output); + +#ifdef HAVE_WINDOWS_H + if (blkdev) { +#else + if (S_ISBLK(st.st_mode)) { +#endif + opt.blkdev_out = 1; + if (opt.metadata && !opt.force) + err_exit("Cloning only metadata to a " + "block device does not usually " + "make sense, aborting...\n" + "If you were instructed to do " + "this by a developer and/or are " + "sure that this is what you want " + "to do, run this utility again " + "but this time add the force " + "option, i.e. add '--force' to " + "the command line arguments."); + } + } + } + + /* + * Send messages, debug information and library messages to stdout, + * but, if outputing to stdout send them to stderr + */ + if (opt.std_out) { + msg_out = stderr; + ntfs_log_set_handler(ntfs_log_handler_stderr); + } else { + msg_out = stdout; + ntfs_log_set_handler(ntfs_log_handler_outerr); + } +} + +/* + * Initialize the random number generator with the current + * time, and generate a 64-bit random number for the serial + * number + */ +static void generate_serial_number(void) { + u64 sn; + + /* different values for parallel processes */ + srandom(time((time_t*)NULL) ^ (getpid() << 16)); + sn = ((u64)random() << 32) | ((u64)random() & 0xffffffff); + volume_serial_number = cpu_to_le64(sn); +} + +static void progress_init(struct progress_bar *p, u64 start, u64 stop, int res) +{ + p->start = start; + p->stop = stop; + p->unit = 100.0 / (stop - start); + p->resolution = res; +} + + +static void progress_update(struct progress_bar *p, u64 current) +{ + float percent = p->unit * current; + + if (opt.quiet) + return; + + if (current != p->stop) { + if ((current - p->start) % p->resolution) + return; + Printf("%6.2f percent completed\r", percent); + } else + Printf("100.00 percent completed\n"); + fflush(msg_out); +} + +static s64 is_critical_metadata(ntfs_walk_clusters_ctx *image, runlist *rl) +{ + s64 inode = image->ni->mft_no; + + if (inode <= LAST_METADATA_INODE) { + + /* Don't save bad sectors (both $Bad and unnamed are ignored */ + if (inode == FILE_BadClus && image->ctx->attr->type == AT_DATA) + return 0; + + if ((inode != FILE_LogFile) || opt.full_logfile) + return rl->length; + + if (image->ctx->attr->type == AT_DATA) { + + /* Save at least the first 16 KiB of FILE_LogFile */ + s64 s = (s64)16384 - rl->vcn * vol->cluster_size; + if (s > 0) { + s = rounded_up_division(s, vol->cluster_size); + if (rl->length < s) + s = rl->length; + return s; + } + return 0; + } + } + + if (image->ctx->attr->type != AT_DATA) + return rl->length; + + return 0; +} + +static off_t tellin(int in) +{ + return (lseek(in, 0, SEEK_CUR)); +} + +static int io_all(void *fd, void *buf, int count, int do_write) +{ + int i; + struct ntfs_device *dev = fd; + + while (count > 0) { + if (do_write) { + if (opt.no_action) { + i = count; + } else { + if (opt.save_image || opt.metadata_image) + i = fwrite(buf, 1, count, stream_out); +#ifdef HAVE_WINDOWS_H + else if (dev_out) + i = dev_out->d_ops->write(dev_out, + buf, count); +#endif + else + i = write(*(int *)fd, buf, count); + } + } else if (opt.restore_image) + i = read(*(int *)fd, buf, count); + else + i = dev->d_ops->read(dev, buf, count); + if (i < 0) { + if (errno != EAGAIN && errno != EINTR) + return -1; + } else if (i == 0 && !do_write && opt.restore_image) { + return -1; + } else { + count -= i; + buf = i + (char *) buf; + } + } + return 0; +} + + +static void rescue_sector(void *fd, u32 bytes_per_sector, off_t pos, void *buff) +{ + const char badsector_magic[] = "BadSectoR"; + struct ntfs_device *dev = fd; + + if (opt.restore_image) { + if (!opt.no_action + && (lseek(*(int *)fd, pos, SEEK_SET) == (off_t)-1)) + perr_exit("lseek"); + } else { + if (vol->dev->d_ops->seek(dev, pos, SEEK_SET) == (off_t)-1) + perr_exit("seek input"); + } + + if (read_all(fd, buff, bytes_per_sector) == -1) { + Printf("WARNING: Can't read sector at %llu, lost data.\n", + (unsigned long long)pos); + memset(buff, '?', bytes_per_sector); + memmove(buff, badsector_magic, sizeof(badsector_magic)); + } +} + +/* + * Read a cluster, try to rescue if cannot read + */ + +static void read_rescue(void *fd, char *buff, u32 csize, u32 bytes_per_sector, + u64 rescue_lcn) +{ + off_t rescue_pos; + + if (read_all(fd, buff, csize) == -1) { + + if (errno != EIO) + perr_exit("read_all"); + else if (opt.rescue){ + u32 i; + + rescue_pos = (off_t)(rescue_lcn * csize); + for (i = 0; i < csize; i += bytes_per_sector) + rescue_sector(fd, bytes_per_sector, + rescue_pos + i, buff + i); + } else { + Printf("%s", bad_sectors_warning_msg); + err_exit("Disk is faulty, can't make full backup!"); + } + } +} + +static void copy_cluster(int rescue, u64 rescue_lcn, u64 lcn) +{ + char *buff; + /* vol is NULL if opt.restore_image is set */ + s32 csize = le32_to_cpu(image_hdr.cluster_size); + BOOL backup_bootsector; + void *fd = (void *)&fd_in; + off_t rescue_pos; + NTFS_BOOT_SECTOR *bs; + le64 mask; + static u16 bytes_per_sector = NTFS_SECTOR_SIZE; + + if (!opt.restore_image) { + csize = vol->cluster_size; + bytes_per_sector = vol->sector_size; + fd = vol->dev; + } + + rescue_pos = (off_t)(rescue_lcn * csize); + buff = (char*)ntfs_malloc(csize); + if (!buff) + err_exit("Not enough memory"); + + /* possible partial cluster holding the backup boot sector */ + backup_bootsector = (lcn + 1)*csize >= full_device_size; + if (backup_bootsector) { + csize = full_device_size - lcn*csize; + if (csize < 0) { + err_exit("Corrupted input, copy aborted"); + } + } + +// need reading when not about to write ? + if (read_all(fd, buff, csize) == -1) { + + if (errno != EIO) { + if (!errno && opt.restore_image) + err_exit("Short image file...\n"); + else + perr_exit("read_all"); + } + else if (rescue){ + s32 i; + for (i = 0; i < csize; i += bytes_per_sector) + rescue_sector(fd, bytes_per_sector, + rescue_pos + i, buff + i); + } else { + Printf("%s", bad_sectors_warning_msg); + err_exit("Disk is faulty, can't make full backup!"); + } + } + + /* Set the new serial number if requested */ + if (opt.new_serial + && !opt.save_image + && (!lcn || backup_bootsector)) { + /* + * For updating the backup boot sector, we need to + * know the sector size, but this is not recorded + * in the image header, so we collect it on the fly + * while reading the first boot sector. + */ + if (!lcn) { + bs = (NTFS_BOOT_SECTOR*)buff; + bytes_per_sector = le16_to_cpu(bs->bpb.bytes_per_sector); + if ((bytes_per_sector > csize) + || (bytes_per_sector < NTFS_SECTOR_SIZE)) + bytes_per_sector = NTFS_SECTOR_SIZE; + } else + bs = (NTFS_BOOT_SECTOR*)(buff + + csize - bytes_per_sector); + if (opt.new_serial & 2) + bs->volume_serial_number = volume_serial_number; + else { + mask = const_cpu_to_le64(~0x0ffffffffULL); + bs->volume_serial_number + = (volume_serial_number & mask) + | (bs->volume_serial_number & ~mask); + } + /* Show the new full serial after merging */ + if (!lcn) + Printf("New serial number : 0x%llx\n", + (long long)le64_to_cpu( + bs->volume_serial_number)); + } + + if (opt.save_image || (opt.metadata_image && wipe)) { + char cmd = CMD_NEXT; + if (write_all(&fd_out, &cmd, sizeof(cmd)) == -1) + perr_exit("write_all"); + } + + if ((!opt.metadata_image || wipe) + && (write_all(&fd_out, buff, csize) == -1)) { +#ifndef NO_STATFS + int err = errno; + perr_printf("Write failed"); + if (err == EIO && opt.stfs.f_type == 0x517b) + Printf("Apparently you tried to clone to a remote " + "Windows computer but they don't\nhave " + "efficient sparse file handling by default. " + "Please try a different method.\n"); + exit(1); +#else + perr_printf("Write failed"); +#endif + } + free(buff); +} + +static s64 lseek_out(int fd, s64 pos, int mode) +{ + s64 ret; + + if (dev_out) + ret = (dev_out->d_ops->seek)(dev_out, pos, mode); + else + ret = lseek(fd, pos, mode); + return (ret); +} + +static void lseek_to_cluster(s64 lcn) +{ + off_t pos; + + pos = (off_t)(lcn * vol->cluster_size); + + if (vol->dev->d_ops->seek(vol->dev, pos, SEEK_SET) == (off_t)-1) + perr_exit("lseek input"); + + if (opt.std_out || opt.save_image || opt.metadata_image) + return; + + if (lseek_out(fd_out, pos, SEEK_SET) == (off_t)-1) + perr_exit("lseek output"); +} + +static void gap_to_cluster(s64 gap) +{ + sle64 count; + char buf[1 + sizeof(count)]; + + if (gap) { + count = cpu_to_sle64(gap); + buf[0] = CMD_GAP; + memcpy(&buf[1], &count, sizeof(count)); + if (write_all(&fd_out, buf, sizeof(buf)) == -1) + perr_exit("write_all"); + } +} + +static void image_skip_clusters(s64 count) +{ + if (opt.save_image && count > 0) { + sle64 count_buf; + char buff[1 + sizeof(count)]; + + buff[0] = CMD_GAP; + count_buf = cpu_to_sle64(count); + memcpy(buff + 1, &count_buf, sizeof(count_buf)); + + if (write_all(&fd_out, buff, sizeof(buff)) == -1) + perr_exit("write_all"); + } +} + +static void write_image_hdr(void) +{ + char alignment[IMAGE_HDR_ALIGN]; + + if (opt.save_image || opt.metadata_image) { + int alignsize = le32_to_cpu(image_hdr.offset_to_image_data) + - sizeof(image_hdr); + memset(alignment,0,IMAGE_HDR_ALIGN); + if ((alignsize < 0) + || write_all(&fd_out, &image_hdr, sizeof(image_hdr)) + || write_all(&fd_out, alignment, alignsize)) + perr_exit("write_all"); + } +} + +static void clone_ntfs(u64 nr_clusters, int more_use) +{ + u64 cl, last_cl; /* current and last used cluster */ + void *buf; + u32 csize = vol->cluster_size; + u64 p_counter = 0; + char alignment[IMAGE_HDR_ALIGN]; + struct progress_bar progress; + + if (opt.save_image) + Printf("Saving NTFS to image ...\n"); + else + Printf("Cloning NTFS ...\n"); + + if (opt.new_serial) + generate_serial_number(); + + buf = ntfs_calloc(csize); + if (!buf) + perr_exit("clone_ntfs"); + + progress_init(&progress, p_counter, nr_clusters, 100); + + if (opt.save_image) { + int alignsize = le32_to_cpu(image_hdr.offset_to_image_data) + - sizeof(image_hdr); + memset(alignment,0,IMAGE_HDR_ALIGN); + if ((alignsize < 0) + || write_all(&fd_out, &image_hdr, sizeof(image_hdr)) + || write_all(&fd_out, alignment, alignsize)) + perr_exit("write_all"); + } + + /* save suspicious clusters if required */ + if (more_use && opt.ignore_fs_check) { + compare_bitmaps(&lcn_bitmap, TRUE); + } + /* Examine up to the alternate boot sector */ + for (last_cl = cl = 0; cl <= (u64)vol->nr_clusters; cl++) { + + if (ntfs_bit_get(lcn_bitmap.bm, cl)) { + progress_update(&progress, ++p_counter); + lseek_to_cluster(cl); + image_skip_clusters(cl - last_cl - 1); + + copy_cluster(opt.rescue, cl, cl); + last_cl = cl; + continue; + } + + if (opt.std_out && !opt.save_image) { + progress_update(&progress, ++p_counter); + if (write_all(&fd_out, buf, csize) == -1) + perr_exit("write_all"); + } + } + image_skip_clusters(cl - last_cl - 1); + free(buf); +} + +static void write_empty_clusters(s32 csize, s64 count, + struct progress_bar *progress, u64 *p_counter) +{ + s64 i; + char *buff; + + buff = (char*)ntfs_malloc(csize); + if (!buff) + err_exit("Not enough memory"); + + memset(buff, 0, csize); + + for (i = 0; i < count; i++) { + if (write_all(&fd_out, buff, csize) == -1) + perr_exit("write_all"); + progress_update(progress, ++(*p_counter)); + } + free(buff); +} + +static void restore_image(void) +{ + s64 pos = 0, count; + s32 csize = le32_to_cpu(image_hdr.cluster_size); + char cmd; + u64 p_counter = 0; + struct progress_bar progress; + + Printf("Restoring NTFS from image ...\n"); + + progress_init(&progress, p_counter, opt.std_out ? + (u64)sle64_to_cpu(image_hdr.nr_clusters) + 1 : + le64_to_cpu(image_hdr.inuse) + 1, + 100); + + if (opt.new_serial) + generate_serial_number(); + + /* Restore up to the alternate boot sector */ + while (pos <= sle64_to_cpu(image_hdr.nr_clusters)) { + if (read_all(&fd_in, &cmd, sizeof(cmd)) == -1) { + if (pos == sle64_to_cpu(image_hdr.nr_clusters)) { + /* alternate boot sector no present in old images */ + Printf("Warning : no alternate boot" + " sector in image\n"); + break; + } else + perr_exit("read_all"); + } + + if (cmd == CMD_GAP) { + if (!image_is_host_endian) { + sle64 lecount; + + /* little endian image, on any computer */ + if (read_all(&fd_in, &lecount, + sizeof(lecount)) == -1) + perr_exit("read_all"); + count = sle64_to_cpu(lecount); + } else { + /* big endian image on big endian computer */ + if (read_all(&fd_in, &count, + sizeof(count)) == -1) + perr_exit("read_all"); + } + if (!count) + err_exit("Bad offset at input location 0x%llx\n", + (long long)tellin(fd_in) - 9); + if (opt.std_out) { + if ((!p_counter && count) || (count < 0)) + err_exit("Cannot restore a metadata" + " image to stdout\n"); + else + write_empty_clusters(csize, count, + &progress, &p_counter); + } else { + if (((pos + count) < 0) + || ((pos + count) + > sle64_to_cpu(image_hdr.nr_clusters))) + err_exit("restore_image: corrupt image " + "at input offset %lld\n", + (long long)tellin(fd_in) - 9); + else { + if (!opt.no_action + && (lseek_out(fd_out, count * csize, + SEEK_CUR) == (off_t)-1)) + perr_exit("restore_image: lseek"); + } + } + pos += count; + } else if (cmd == CMD_NEXT) { + copy_cluster(0, 0, pos); + pos++; + progress_update(&progress, ++p_counter); + } else + err_exit("Invalid command code %d at input offset 0x%llx\n", + cmd, (long long)tellin(fd_in) - 1); + } +} + +static void wipe_index_entry_timestams(INDEX_ENTRY *e) +{ + static const struct timespec zero_time = { .tv_sec = 0, .tv_nsec = 0 }; + sle64 timestamp = timespec2ntfs(zero_time); + + /* FIXME: can fall into infinite loop if corrupted */ + while (!(e->ie_flags & INDEX_ENTRY_END)) { + + e->key.file_name.creation_time = timestamp; + e->key.file_name.last_data_change_time = timestamp; + e->key.file_name.last_mft_change_time = timestamp; + e->key.file_name.last_access_time = timestamp; + + wiped_timestamp_data += 32; + + e = (INDEX_ENTRY *)((u8 *)e + le16_to_cpu(e->length)); + } +} + +static void wipe_index_allocation_timestamps(ntfs_inode *ni, ATTR_RECORD *attr) +{ + INDEX_ALLOCATION *indexa, *tmp_indexa; + INDEX_ENTRY *entry; + INDEX_ROOT *indexr; + u8 *bitmap, *byte; + int bit; + ntfs_attr *na; + ntfschar *name; + u32 name_len; + + indexr = ntfs_index_root_get(ni, attr); + if (!indexr) { + perr_printf("Failed to read $INDEX_ROOT attribute of inode " + "%lld", (long long)ni->mft_no); + return; + } + + if (indexr->type != AT_FILE_NAME) + goto out_indexr; + + name = (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset)); + name_len = attr->name_length; + + byte = bitmap = ntfs_attr_readall(ni, AT_BITMAP, name, name_len, + NULL); + if (!byte) { + perr_printf("Failed to read $BITMAP attribute"); + goto out_indexr; + } + + na = ntfs_attr_open(ni, AT_INDEX_ALLOCATION, name, name_len); + if (!na) { + perr_printf("Failed to open $INDEX_ALLOCATION attribute"); + goto out_bitmap; + } + + if (!na->data_size) + goto out_na; + + tmp_indexa = indexa = ntfs_malloc(na->data_size); + if (!tmp_indexa) + goto out_na; + + if (ntfs_attr_pread(na, 0, na->data_size, indexa) != na->data_size) { + perr_printf("Failed to read $INDEX_ALLOCATION attribute"); + goto out_indexa; + } + + bit = 0; + while ((u8 *)tmp_indexa < (u8 *)indexa + na->data_size) { + if (*byte & (1 << bit)) { + if (ntfs_mst_post_read_fixup((NTFS_RECORD *)tmp_indexa, + le32_to_cpu( + indexr->index_block_size))) { + perr_printf("Damaged INDX record"); + goto out_indexa; + } + entry = (INDEX_ENTRY *)((u8 *)tmp_indexa + le32_to_cpu( + tmp_indexa->index.entries_offset) + 0x18); + + wipe_index_entry_timestams(entry); + + if (ntfs_mft_usn_dec((MFT_RECORD *)tmp_indexa)) + perr_exit("ntfs_mft_usn_dec"); + + if (ntfs_mst_pre_write_fixup((NTFS_RECORD *)tmp_indexa, + le32_to_cpu( + indexr->index_block_size))) { + perr_printf("INDX write fixup failed"); + goto out_indexa; + } + } + tmp_indexa = (INDEX_ALLOCATION *)((u8 *)tmp_indexa + + le32_to_cpu(indexr->index_block_size)); + bit++; + if (bit > 7) { + bit = 0; + byte++; + } + } + if (ntfs_rl_pwrite(vol, na->rl, 0, 0, na->data_size, indexa) != na->data_size) + perr_printf("ntfs_rl_pwrite failed for inode %lld", + (long long)ni->mft_no); +out_indexa: + free(indexa); +out_na: + ntfs_attr_close(na); +out_bitmap: + free(bitmap); +out_indexr: + free(indexr); +} + +static void wipe_index_root_timestamps(ATTR_RECORD *attr, sle64 timestamp) +{ + INDEX_ENTRY *entry; + INDEX_ROOT *iroot; + + iroot = (INDEX_ROOT *)((u8 *)attr + le16_to_cpu(attr->value_offset)); + entry = (INDEX_ENTRY *)((u8 *)iroot + + le32_to_cpu(iroot->index.entries_offset) + 0x10); + + while (!(entry->ie_flags & INDEX_ENTRY_END)) { + + if (iroot->type == AT_FILE_NAME) { + + entry->key.file_name.creation_time = timestamp; + entry->key.file_name.last_access_time = timestamp; + entry->key.file_name.last_data_change_time = timestamp; + entry->key.file_name.last_mft_change_time = timestamp; + + wiped_timestamp_data += 32; + + } else if (ntfs_names_are_equal(NTFS_INDEX_Q, + sizeof(NTFS_INDEX_Q) / 2 - 1, + (ntfschar *)((char *)attr + + le16_to_cpu(attr->name_offset)), + attr->name_length, CASE_SENSITIVE, NULL, 0)) { + + QUOTA_CONTROL_ENTRY *quota_q; + + quota_q = (QUOTA_CONTROL_ENTRY *)((u8 *)entry + + le16_to_cpu(entry->data_offset)); + /* + * FIXME: no guarantee it's indeed /$Extend/$Quota:$Q. + * For now, as a minimal safeguard, we check only for + * quota version 2 ... + */ + if (le32_to_cpu(quota_q->version) == 2) { + quota_q->change_time = timestamp; + wiped_timestamp_data += 4; + } + } + + entry = (INDEX_ENTRY*)((u8*)entry + le16_to_cpu(entry->length)); + } +} + +#define WIPE_TIMESTAMPS(atype, attr, timestamp) \ +do { \ + atype *ats; \ + ats = (atype *)((char *)(attr) + le16_to_cpu((attr)->value_offset)); \ + \ + ats->creation_time = (timestamp); \ + ats->last_data_change_time = (timestamp); \ + ats->last_mft_change_time= (timestamp); \ + ats->last_access_time = (timestamp); \ + \ + wiped_timestamp_data += 32; \ + \ +} while (0) + +static void wipe_timestamps(ntfs_walk_clusters_ctx *image) +{ + static const struct timespec zero_time = { .tv_sec = 0, .tv_nsec = 0 }; + ATTR_RECORD *a = image->ctx->attr; + sle64 timestamp = timespec2ntfs(zero_time); + + if (a->type == AT_FILE_NAME) + WIPE_TIMESTAMPS(FILE_NAME_ATTR, a, timestamp); + + else if (a->type == AT_STANDARD_INFORMATION) + WIPE_TIMESTAMPS(STANDARD_INFORMATION, a, timestamp); + + else if (a->type == AT_INDEX_ROOT) + wipe_index_root_timestamps(a, timestamp); +} + +static void wipe_resident_data(ntfs_walk_clusters_ctx *image) +{ + ATTR_RECORD *a; + u32 i; + int n = 0; + u8 *p; + + a = image->ctx->attr; + p = (u8*)a + le16_to_cpu(a->value_offset); + + if (image->ni->mft_no <= LAST_METADATA_INODE) + return; + + if (a->type != AT_DATA) + return; + + for (i = 0; i < le32_to_cpu(a->value_length); i++) { + if (p[i]) { + p[i] = 0; + n++; + } + } + + wiped_resident_data += n; +} + +static int wipe_data(char *p, int pos, int len) +{ + int wiped = 0; + + for (p += pos; --len >= 0;) { + if (p[len]) { + p[len] = 0; + wiped++; + } + } + + return wiped; +} + +static void wipe_unused_mft_data(ntfs_inode *ni) +{ + int unused; + MFT_RECORD *m = ni->mrec; + + /* FIXME: broken MFTMirr update was fixed in libntfs, check if OK now */ + if (ni->mft_no <= LAST_METADATA_INODE) + return; + + unused = le32_to_cpu(m->bytes_allocated) - le32_to_cpu(m->bytes_in_use); + wiped_unused_mft_data += wipe_data((char *)m, + le32_to_cpu(m->bytes_in_use), unused); +} + +static void wipe_unused_mft(ntfs_inode *ni) +{ + int unused; + MFT_RECORD *m = ni->mrec; + + /* FIXME: broken MFTMirr update was fixed in libntfs, check if OK now */ + if (ni->mft_no <= LAST_METADATA_INODE) + return; + + unused = le32_to_cpu(m->bytes_in_use) - sizeof(MFT_RECORD); + wiped_unused_mft += wipe_data((char *)m, sizeof(MFT_RECORD), unused); +} + +static void clone_logfile_parts(ntfs_walk_clusters_ctx *image, runlist *rl) +{ + s64 offset = 0, lcn, vcn; + + while (1) { + + vcn = offset / image->ni->vol->cluster_size; + lcn = ntfs_rl_vcn_to_lcn(rl, vcn); + if (lcn < 0) + break; + + lseek_to_cluster(lcn); + + if ((lcn + 1) != image->current_lcn) { + /* do not duplicate a cluster */ + if (opt.metadata_image && wipe) + gap_to_cluster(lcn - image->current_lcn); + + copy_cluster(opt.rescue, lcn, lcn); + } + image->current_lcn = lcn + 1; + if (opt.metadata_image && !wipe) + image->inuse++; + + if (offset == 0) + offset = NTFS_BLOCK_SIZE >> 1; + else + offset <<= 1; + } +} + +/* + * In-memory wiping of MFT record or MFTMirr record + * (only for metadata images) + * + * The resident data and (optionally) the timestamps are wiped. + */ + +static void wipe_mft(char *mrec, u32 mrecsz, u64 mft_no) +{ + ntfs_walk_clusters_ctx image; + ntfs_attr_search_ctx *ctx; + ntfs_inode ni; + + ni.mft_no = mft_no; + ni.mrec = (MFT_RECORD*)mrec; + ni.vol = vol; /* Hmm */ + image.ni = ∋ + ntfs_mst_post_read_fixup_warn((NTFS_RECORD*)mrec,mrecsz,FALSE); + wipe_unused_mft_data(&ni); + if (!(((MFT_RECORD*)mrec)->flags & MFT_RECORD_IN_USE)) { + wipe_unused_mft(&ni); + } else { + /* ctx with no ntfs_inode prevents from searching external attrs */ + if (!(ctx = ntfs_attr_get_search_ctx((ntfs_inode*)NULL, (MFT_RECORD*)mrec))) + perr_exit("ntfs_get_attr_search_ctx"); + + while (!ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 0, + NULL, 0, ctx)) { + if (ctx->attr->type == AT_END) + break; + + image.ctx = ctx; + if (!ctx->attr->non_resident + && (mft_no > LAST_METADATA_INODE)) + wipe_resident_data(&image); + if (!opt.preserve_timestamps) + wipe_timestamps(&image); + } + ntfs_attr_put_search_ctx(ctx); + } + ntfs_mft_usn_dec((MFT_RECORD*)mrec); + ntfs_mst_pre_write_fixup((NTFS_RECORD*)mrec,mrecsz); +} + +/* + * In-memory wiping of a directory record (I30) + * (only for metadata images) + * + * The timestamps are (optionally) wiped + */ + +static void wipe_indx(char *mrec, u32 mrecsz) +{ + INDEX_ENTRY *entry; + INDEX_ALLOCATION *indexa; + + if (ntfs_mst_post_read_fixup((NTFS_RECORD *)mrec, mrecsz)) { + perr_printf("Damaged INDX record"); + goto out_indexa; + } + indexa = (INDEX_ALLOCATION*)mrec; + /* + * The index bitmap is not checked, obsoleted records are + * wiped if they pass the safety checks + */ + if ((indexa->magic == magic_INDX) + && (le32_to_cpu(indexa->index.entries_offset) >= sizeof(INDEX_HEADER)) + && (le32_to_cpu(indexa->index.allocated_size) <= mrecsz)) { + entry = (INDEX_ENTRY *)((u8 *)mrec + le32_to_cpu( + indexa->index.entries_offset) + 0x18); + wipe_index_entry_timestams(entry); + } + + if (ntfs_mft_usn_dec((MFT_RECORD *)mrec)) + perr_exit("ntfs_mft_usn_dec"); + + if (ntfs_mst_pre_write_fixup((NTFS_RECORD *)mrec, mrecsz)) { + perr_printf("INDX write fixup failed"); + goto out_indexa; + } +out_indexa : ; +} + +/* + * Output a set of related clusters (MFT record or index block) + */ + +static void write_set(char *buff, u32 csize, s64 *current_lcn, + runlist_element *rl, u32 wi, u32 wj, u32 cnt) +{ + u32 k; + s64 target_lcn; + char cmd = CMD_NEXT; + + for (k=0; k= rl[wi].length) { + wj = 0; + wi++; + } + } +} + +/* + * Copy and wipe the full MFT or MFTMirr data. + * (only for metadata images) + * + * Data are read and written by full clusters, but the wiping is done + * per MFT record. + */ + +static void copy_wipe_mft(ntfs_walk_clusters_ctx *image, runlist *rl) +{ + char *buff; + void *fd; + s64 mft_no; + u32 mft_record_size; + u32 csize; + u32 buff_size; + u32 bytes_per_sector; + u32 records_per_set; + u32 clusters_per_set; + u32 wi,wj; /* indexes for reading */ + u32 ri,rj; /* indexes for writing */ + u32 k; /* lcn within run */ + u32 r; /* mft_record within set */ + s64 current_lcn; + + current_lcn = image->current_lcn; + mft_record_size = image->ni->vol->mft_record_size; + csize = image->ni->vol->cluster_size; + bytes_per_sector = image->ni->vol->sector_size; + fd = image->ni->vol->dev; + /* + * Depending on the sizes, there may be several records + * per cluster, or several clusters per record. + * Anyway, records are read and rescued by full clusters. + */ + if (csize >= mft_record_size) { + records_per_set = csize/mft_record_size; + clusters_per_set = 1; + buff_size = csize; + } else { + clusters_per_set = mft_record_size/csize; + records_per_set = 1; + buff_size = mft_record_size; + } + buff = (char*)ntfs_malloc(buff_size); + if (!buff) + err_exit("Not enough memory"); + + mft_no = 0; + ri = rj = 0; + wi = wj = 0; + if (rl[ri].length) + lseek_to_cluster(rl[ri].lcn); + while (rl[ri].length) { + for (k=0; (k= rl[ri].length) { + rj = 0; + if (rl[++ri].length) + lseek_to_cluster(rl[ri].lcn); + } + } + if (k == clusters_per_set) { + for (r=0; r= rl[wi].length)) + wj -= rl[wi++].length; + } else { + err_exit("Short last MFT record\n"); + } + } + image->current_lcn = current_lcn; + free(buff); +} + +/* + * Copy and wipe the non-resident part of a directory index + * (only for metadata images) + * + * Data are read and written by full clusters, but the wiping is done + * per index record. + */ + +static void copy_wipe_i30(ntfs_walk_clusters_ctx *image, runlist *rl) +{ + char *buff; + void *fd; + u32 indx_record_size; + u32 csize; + u32 buff_size; + u32 bytes_per_sector; + u32 records_per_set; + u32 clusters_per_set; + u32 wi,wj; /* indexes for reading */ + u32 ri,rj; /* indexes for writing */ + u32 k; /* lcn within run */ + u32 r; /* mft_record within set */ + s64 current_lcn; + + current_lcn = image->current_lcn; + csize = image->ni->vol->cluster_size; + bytes_per_sector = image->ni->vol->sector_size; + fd = image->ni->vol->dev; + /* + * Depending on the sizes, there may be several records + * per cluster, or several clusters per record. + * Anyway, records are read and rescued by full clusters. + */ + indx_record_size = image->ni->vol->indx_record_size; + if (csize >= indx_record_size) { + records_per_set = csize/indx_record_size; + clusters_per_set = 1; + buff_size = csize; + } else { + clusters_per_set = indx_record_size/csize; + records_per_set = 1; + buff_size = indx_record_size; + } + buff = (char*)ntfs_malloc(buff_size); + if (!buff) + err_exit("Not enough memory"); + + ri = rj = 0; + wi = wj = 0; + if (rl[ri].length) + lseek_to_cluster(rl[ri].lcn); + while (rl[ri].length) { + for (k=0; (k= rl[ri].length) { + rj = 0; + if (rl[++ri].length) + lseek_to_cluster(rl[ri].lcn); + } + } + if (k == clusters_per_set) { + /* wipe records_per_set records */ + if (!opt.preserve_timestamps) + for (r=0; r= rl[wi].length)) + wj -= rl[wi++].length; + } else { + err_exit("Short last directory index record\n"); + } + } + image->current_lcn = current_lcn; + free(buff); +} + +static void dump_clusters(ntfs_walk_clusters_ctx *image, runlist *rl) +{ + s64 i, len; /* number of clusters to copy */ + + if (opt.restore_image) + err_exit("Bug : invalid dump_clusters()\n"); + + if ((opt.std_out && !opt.metadata_image) || !opt.metadata) + return; + if (!(len = is_critical_metadata(image, rl))) + return; + + lseek_to_cluster(rl->lcn); + if (opt.metadata_image ? wipe : !wipe) { + if (opt.metadata_image) + gap_to_cluster(rl->lcn - image->current_lcn); + /* FIXME: this could give pretty suboptimal performance */ + for (i = 0; i < len; i++) + copy_cluster(opt.rescue, rl->lcn + i, rl->lcn + i); + if (opt.metadata_image) + image->current_lcn = rl->lcn + len; + } +} + +static void walk_runs(struct ntfs_walk_cluster *walk) +{ + int i, j; + runlist *rl; + ATTR_RECORD *a; + ntfs_attr_search_ctx *ctx; + BOOL mft_data; + BOOL index_i30; + + ctx = walk->image->ctx; + a = ctx->attr; + + if (!a->non_resident) { + if (wipe) { + wipe_resident_data(walk->image); + if (!opt.preserve_timestamps) + wipe_timestamps(walk->image); + } + return; + } + + if (wipe + && !opt.preserve_timestamps + && walk->image->ctx->attr->type == AT_INDEX_ALLOCATION) + wipe_index_allocation_timestamps(walk->image->ni, a); + + if (!(rl = ntfs_mapping_pairs_decompress(vol, a, NULL))) + perr_exit("ntfs_decompress_mapping_pairs"); + + /* special wipings for MFT records and directory indexes */ + mft_data = ((walk->image->ni->mft_no == FILE_MFT) + || (walk->image->ni->mft_no == FILE_MFTMirr)) + && (a->type == AT_DATA); + index_i30 = (walk->image->ctx->attr->type == AT_INDEX_ALLOCATION) + && (a->name_length == 4) + && !memcmp((char*)a + le16_to_cpu(a->name_offset), + NTFS_INDEX_I30,8); + + for (i = 0; rl[i].length; i++) { + s64 lcn = rl[i].lcn; + s64 lcn_length = rl[i].length; + + if (lcn == LCN_HOLE || lcn == LCN_RL_NOT_MAPPED) + continue; + + /* FIXME: ntfs_mapping_pairs_decompress should return error */ + if (lcn < 0 || lcn_length < 0) + err_exit("Corrupt runlist in inode %lld attr %x LCN " + "%llx length %llx\n", + (long long)ctx->ntfs_ino->mft_no, + (unsigned int)le32_to_cpu(a->type), + (long long)lcn, (long long)lcn_length); + + if (opt.metadata_image ? wipe && !mft_data && !index_i30 : !wipe) + dump_clusters(walk->image, rl + i); + + for (j = 0; j < lcn_length; j++) { + u64 k = (u64)lcn + j; + if (ntfs_bit_get_and_set(lcn_bitmap.bm, k, 1)) { + if (opt.ignore_fs_check) + Printf("Cluster %llu is referenced" + " twice!\n", + (unsigned long long)k); + else + err_exit("Cluster %llu referenced" + " twice!\nYou didn't shutdown" + " your Windows properly?\n", + (unsigned long long)k); + } + } + + if (!opt.metadata_image) + walk->image->inuse += lcn_length; + /* + * For a metadata image, we have to compute the + * number of metadata clusters for the percentages + * to be displayed correctly while restoring. + */ + if (!wipe && opt.metadata_image) { + if ((walk->image->ni->mft_no == FILE_LogFile) + && (walk->image->ctx->attr->type == AT_DATA)) { + /* 16 KiB of FILE_LogFile */ + walk->image->inuse + += is_critical_metadata(walk->image,rl); + } else { + if ((walk->image->ni->mft_no + <= LAST_METADATA_INODE) + || (walk->image->ctx->attr->type != AT_DATA)) + walk->image->inuse += lcn_length; + } + } + } + if (wipe && opt.metadata_image) { + ntfs_attr *na; + /* + * Non-resident metadata has to be wiped globally, + * because its logical blocks may be larger than + * a cluster and split over two extents. + */ + if (mft_data && !a->lowest_vcn) { + na = ntfs_attr_open(walk->image->ni, + AT_DATA, NULL, 0); + if (na) { + na->rl = rl; + rl = (runlist_element*)NULL; + if (!ntfs_attr_map_whole_runlist(na)) { + copy_wipe_mft(walk->image,na->rl); + } else + perr_exit("Failed to map data of inode %lld", + (long long)walk->image->ni->mft_no); + ntfs_attr_close(na); + } else + perr_exit("Failed to open data of inode %lld", + (long long)walk->image->ni->mft_no); + } + if (index_i30 && !a->lowest_vcn) { + na = ntfs_attr_open(walk->image->ni, + AT_INDEX_ALLOCATION, NTFS_INDEX_I30, 4); + if (na) { + na->rl = rl; + rl = (runlist_element*)NULL; + if (!ntfs_attr_map_whole_runlist(na)) { + copy_wipe_i30(walk->image,na->rl); + } else + perr_exit("Failed to map index of inode %lld", + (long long)walk->image->ni->mft_no); + ntfs_attr_close(na); + } else + perr_exit("Failed to open index of inode %lld", + (long long)walk->image->ni->mft_no); + } + } + if (opt.metadata + && (opt.metadata_image || !wipe) + && (walk->image->ni->mft_no == FILE_LogFile) + && (walk->image->ctx->attr->type == AT_DATA)) + clone_logfile_parts(walk->image, rl); + + free(rl); +} + + +static void walk_attributes(struct ntfs_walk_cluster *walk) +{ + ntfs_attr_search_ctx *ctx; + + if (!(ctx = ntfs_attr_get_search_ctx(walk->image->ni, NULL))) + perr_exit("ntfs_get_attr_search_ctx"); + + while (!ntfs_attrs_walk(ctx)) { + if (ctx->attr->type == AT_END) + break; + + walk->image->ctx = ctx; + walk_runs(walk); + } + + ntfs_attr_put_search_ctx(ctx); +} + +/* + * Compare the actual bitmap to the list of clusters + * allocated to identified files. + * + * Clusters found in use, though not marked in the bitmap are copied + * if the option --ignore-fs-checks is set. + */ + +static int compare_bitmaps(struct bitmap *a, BOOL copy) +{ + s64 i, pos, count; + int mismatch = 0; + int more_use = 0; + s64 new_cl; + u8 bm[NTFS_BUF_SIZE]; + + Printf("Accounting clusters ...\n"); + + pos = 0; + new_cl = 0; + while (1) { + count = ntfs_attr_pread(vol->lcnbmp_na, pos, NTFS_BUF_SIZE, bm); + if (count == -1) + perr_exit("Couldn't get $Bitmap $DATA"); + + if (count == 0) { + /* the backup bootsector need not be accounted for */ + if (((vol->nr_clusters + 7) >> 3) > pos) + err_exit("$Bitmap size is smaller than expected" + " (%lld < %lld)\n", + (long long)pos, (long long)a->size); + break; + } + + for (i = 0; i < count; i++, pos++) { + s64 cl; /* current cluster */ + + if (a->size <= pos) + goto done; + + if (a->bm[pos] == bm[i]) + continue; + + for (cl = pos * 8; cl < (pos + 1) * 8; cl++) { + char bit; + + bit = ntfs_bit_get(a->bm, cl); + if (bit == ntfs_bit_get(bm, i * 8 + cl % 8)) + continue; + + if (!bit) + more_use++; + if (opt.ignore_fs_check && !bit && copy) { + lseek_to_cluster(cl); + if (opt.save_image + || (opt.metadata + && opt.metadata_image)) { + gap_to_cluster(cl - new_cl); + new_cl = cl + 1; + } + copy_cluster(opt.rescue, cl, cl); + } + + if (++mismatch > 10) + continue; + + Printf("Cluster accounting failed at %lld " + "(0x%llx): %s cluster in $Bitmap\n", + (long long)cl, (unsigned long long)cl, + bit ? "missing" : "extra"); + } + } + } +done: + if (mismatch) { + Printf("Totally %d cluster accounting mismatches.\n", mismatch); + if (opt.ignore_fs_check) { + Printf("WARNING: The NTFS inconsistency was overruled " + "by the --ignore-fs-check option.\n"); + if (new_cl) { + gap_to_cluster(-new_cl); + } + return (more_use); + } + err_exit("Filesystem check failed! Windows wasn't shutdown " + "properly or inconsistent\nfilesystem. Please run " + "chkdsk /f on Windows then reboot it TWICE.\n"); + } + return (more_use); +} + + +static void mft_record_write_with_same_usn(ntfs_volume *volume, ntfs_inode *ni) +{ + if (ntfs_mft_usn_dec(ni->mrec)) + perr_exit("ntfs_mft_usn_dec"); + + if (ntfs_mft_record_write(volume, ni->mft_no, ni->mrec)) + perr_exit("ntfs_mft_record_write"); +} + +static void mft_inode_write_with_same_usn(ntfs_volume *volume, ntfs_inode *ni) +{ + s32 i; + + mft_record_write_with_same_usn(volume, ni); + + if (ni->nr_extents <= 0) + return; + + for (i = 0; i < ni->nr_extents; ++i) { + ntfs_inode *eni = ni->extent_nis[i]; + mft_record_write_with_same_usn(volume, eni); + } +} + +static int walk_clusters(ntfs_volume *volume, struct ntfs_walk_cluster *walk) +{ + s64 inode = 0; + s64 last_mft_rec; + u64 nr_clusters; + ntfs_inode *ni; + struct progress_bar progress; + + if (opt.restore_image || (!opt.metadata && wipe)) + err_exit("Bug : invalid walk_clusters()\n"); + Printf("Scanning volume ...\n"); + + last_mft_rec = (volume->mft_na->initialized_size >> + volume->mft_record_size_bits) - 1; + walk->image->current_lcn = 0; + progress_init(&progress, inode, last_mft_rec, 100); + + NVolSetNoFixupWarn(volume); + for (; inode <= last_mft_rec; inode++) { + + int err, deleted_inode; + MFT_REF mref = (MFT_REF)inode; + + progress_update(&progress, inode); + + /* FIXME: Terrible kludge for libntfs not being able to return + a deleted MFT record as inode */ + ni = ntfs_calloc(sizeof(ntfs_inode)); + if (!ni) + perr_exit("walk_clusters"); + + ni->vol = volume; + + err = ntfs_file_record_read(volume, mref, &ni->mrec, NULL); + if (err == -1) { + free(ni); + continue; + } + + deleted_inode = !(ni->mrec->flags & MFT_RECORD_IN_USE); + + if (deleted_inode && !opt.metadata_image) { + + ni->mft_no = MREF(mref); + if (wipe) { + wipe_unused_mft(ni); + wipe_unused_mft_data(ni); + mft_record_write_with_same_usn(volume, ni); + } + } + + free(ni->mrec); + free(ni); + + if (deleted_inode) + continue; + + if ((ni = ntfs_inode_open(volume, mref)) == NULL) { + /* FIXME: continue only if it make sense, e.g. + MFT record not in use based on $MFT bitmap */ + if (errno == EIO || errno == ENOENT) + continue; + perr_exit("Reading inode %lld failed", + (long long)inode); + } + + if (wipe) + nr_used_mft_records++; + + if (ni->mrec->base_mft_record) + goto out; + + walk->image->ni = ni; + walk_attributes(walk); +out: + if (wipe && !opt.metadata_image) { + int i; + + wipe_unused_mft_data(ni); + for (i = 0; i < ni->nr_extents; ++i) { + wipe_unused_mft_data(ni->extent_nis[i]); + } + mft_inode_write_with_same_usn(volume, ni); + } + + if (ntfs_inode_close(ni)) + perr_exit("ntfs_inode_close for inode %lld", + (long long)inode); + } + if (opt.metadata) { + if (opt.metadata_image && wipe && opt.ignore_fs_check) { + gap_to_cluster(-walk->image->current_lcn); + compare_bitmaps(&lcn_bitmap, TRUE); + walk->image->current_lcn = 0; + } + if (opt.metadata_image ? wipe : !wipe) { + /* also get the backup bootsector */ + nr_clusters = vol->nr_clusters; + lseek_to_cluster(nr_clusters); + if (opt.metadata_image && wipe) + gap_to_cluster(nr_clusters + - walk->image->current_lcn); + copy_cluster(opt.rescue, nr_clusters, nr_clusters); + walk->image->current_lcn = nr_clusters; + } + /* Not counted, for compatibility with older versions */ + if (!opt.metadata_image) + walk->image->inuse++; + } + return 0; +} + + +/* + * $Bitmap can overlap the end of the volume. Any bits in this region + * must be set. This region also encompasses the backup boot sector. + */ +static void bitmap_file_data_fixup(s64 cluster, struct bitmap *bm) +{ + for (; cluster < bm->size << 3; cluster++) + ntfs_bit_set(bm->bm, (u64)cluster, 1); +} + + +/* + * Allocate a block of memory with one bit for each cluster of the disk. + * All the bits are set to 0, except those representing the region beyond the + * end of the disk. + */ +static void setup_lcn_bitmap(void) +{ + /* Determine lcn bitmap byte size and allocate it. */ + /* include the alternate boot sector in the bitmap count */ + lcn_bitmap.size = rounded_up_division(vol->nr_clusters + 1, 8); + + lcn_bitmap.bm = ntfs_calloc(lcn_bitmap.size); + if (!lcn_bitmap.bm) + perr_exit("Failed to allocate internal buffer"); + + bitmap_file_data_fixup(vol->nr_clusters, &lcn_bitmap); +} + + +static s64 volume_size(ntfs_volume *volume, s64 nr_clusters) +{ + return nr_clusters * volume->cluster_size; +} + + +static void print_volume_size(const char *str, s64 bytes) +{ + Printf("%s: %lld bytes (%lld MB)\n", str, (long long)bytes, + (long long)rounded_up_division(bytes, NTFS_MBYTE)); +} + + +static void print_disk_usage(const char *spacer, u32 cluster_size, + s64 nr_clusters, s64 inuse) +{ + s64 total, used; + + total = nr_clusters * cluster_size; + used = inuse * cluster_size; + + Printf("Space in use %s: %lld MB (%.1f%%) ", spacer, + (long long)rounded_up_division(used, NTFS_MBYTE), + 100.0 * ((float)used / total)); + + Printf("\n"); +} + +static void print_image_info(void) +{ + Printf("Ntfsclone image version: %d.%d\n", + image_hdr.major_ver, image_hdr.minor_ver); + Printf("Cluster size : %u bytes\n", + (unsigned)le32_to_cpu(image_hdr.cluster_size)); + print_volume_size("Image volume size ", + sle64_to_cpu(image_hdr.nr_clusters) * + le32_to_cpu(image_hdr.cluster_size)); + Printf("Image device size : %lld bytes\n", + (long long)le64_to_cpu(image_hdr.device_size)); + print_disk_usage(" ", le32_to_cpu(image_hdr.cluster_size), + sle64_to_cpu(image_hdr.nr_clusters), + le64_to_cpu(image_hdr.inuse)); + Printf("Offset to image data : %u (0x%x) bytes\n", + (unsigned)le32_to_cpu(image_hdr.offset_to_image_data), + (unsigned)le32_to_cpu(image_hdr.offset_to_image_data)); +} + +static void check_if_mounted(const char *device, unsigned long new_mntflag) +{ + unsigned long mntflag; + + if (ntfs_check_if_mounted(device, &mntflag)) + perr_exit("Failed to check '%s' mount state", device); + + if (mntflag & NTFS_MF_MOUNTED) { + if (!(mntflag & NTFS_MF_READONLY)) + err_exit("Device '%s' is mounted read-write. " + "You must 'umount' it first.\n", device); + if (!new_mntflag) + err_exit("Device '%s' is mounted. " + "You must 'umount' it first.\n", device); + } +} + +/** + * mount_volume - + * + * First perform some checks to determine if the volume is already mounted, or + * is dirty (Windows wasn't shutdown properly). If everything is OK, then mount + * the volume (load the metadata into memory). + */ +static void mount_volume(unsigned long new_mntflag) +{ + check_if_mounted(opt.volume, new_mntflag); + + if (!(vol = ntfs_mount(opt.volume, new_mntflag))) { + + int err = errno; + + perr_printf("Opening '%s' as NTFS failed", opt.volume); + if (err == EINVAL) { + Printf("Apparently device '%s' doesn't have a " + "valid NTFS. Maybe you selected\nthe whole " + "disk instead of a partition (e.g. /dev/hda, " + "not /dev/hda1)?\n", opt.volume); + } + /* + * Retry with recovering the log file enabled. + * Normally avoided in order to get the original log file + * data, but needed when remounting the metadata of a + * volume improperly unmounted from Windows. + * If the full log file was requested, it must be kept + * as is, so we just remount read-only. + */ + if (!(new_mntflag & (NTFS_MNT_RDONLY | NTFS_MNT_RECOVER))) { + if (opt.full_logfile) { + Printf("Retrying read-only to ignore" + " the log file...\n"); + vol = ntfs_mount(opt.volume, + new_mntflag | NTFS_MNT_RDONLY); + } else { + Printf("Trying to recover...\n"); + vol = ntfs_mount(opt.volume, + new_mntflag | NTFS_MNT_RECOVER); + } + Printf("... %s\n",(vol ? "Successful" : "Failed")); + } + if (!vol) + exit(1); + } + + if (vol->flags & VOLUME_IS_DIRTY) + if (opt.force-- <= 0) + err_exit(dirty_volume_msg, opt.volume); + + if (NTFS_MAX_CLUSTER_SIZE < vol->cluster_size) + err_exit("Cluster size %u is too large!\n", + (unsigned int)vol->cluster_size); + + Printf("NTFS volume version: %d.%d\n", vol->major_ver, vol->minor_ver); + if (ntfs_version_is_supported(vol)) + perr_exit("Unknown NTFS version"); + + Printf("Cluster size : %u bytes\n", + (unsigned int)vol->cluster_size); + print_volume_size("Current volume size", + volume_size(vol, vol->nr_clusters)); +} + +static struct ntfs_walk_cluster backup_clusters = { NULL, NULL }; + +static int device_offset_valid(int fd, s64 ofs) +{ + char ch; + + if (lseek(fd, ofs, SEEK_SET) >= 0 && read(fd, &ch, 1) == 1) + return 0; + return -1; +} + +static s64 device_size_get(int fd) +{ + s64 high, low; +#ifdef BLKGETSIZE64 + { u64 size; + + if (ioctl(fd, BLKGETSIZE64, &size) >= 0) { + ntfs_log_debug("BLKGETSIZE64 nr bytes = %llu " + "(0x%llx).\n", (unsigned long long)size, + (unsigned long long)size); + return (s64)size; + } + } +#endif +#ifdef BLKGETSIZE + { unsigned long size; + + if (ioctl(fd, BLKGETSIZE, &size) >= 0) { + ntfs_log_debug("BLKGETSIZE nr 512 byte blocks = %lu " + "(0x%lx).\n", size, size); + return (s64)size * 512; + } + } +#endif +#ifdef FDGETPRM + { struct floppy_struct this_floppy; + + if (ioctl(fd, FDGETPRM, &this_floppy) >= 0) { + ntfs_log_debug("FDGETPRM nr 512 byte blocks = %lu " + "(0x%lx).\n", this_floppy.size, + this_floppy.size); + return (s64)this_floppy.size * 512; + } + } +#endif + /* + * We couldn't figure it out by using a specialized ioctl, + * so do binary search to find the size of the device. + */ + low = 0LL; + for (high = 1024LL; !device_offset_valid(fd, high); high <<= 1) + low = high; + while (low < high - 1LL) { + const s64 mid = (low + high) / 2; + + if (!device_offset_valid(fd, mid)) + low = mid; + else + high = mid; + } + lseek(fd, 0LL, SEEK_SET); + return (low + 1LL); +} + +static void fsync_clone(int fd) +{ + Printf("Syncing ...\n"); + if (opt.save_image && stream_out && fflush(stream_out)) + perr_exit("fflush"); + if (fsync(fd) && errno != EINVAL) + perr_exit("fsync"); +} + +static void set_filesize(s64 filesize) +{ +#ifndef NO_STATFS + long fs_type = 0; /* Unknown filesystem type */ + + if (fstatfs(fd_out, &opt.stfs) == -1) + Printf("WARNING: Couldn't get filesystem type: " + "%s\n", strerror(errno)); + else + fs_type = opt.stfs.f_type; + + if (fs_type == 0x52654973) + Printf("WARNING: You're using ReiserFS, it has very poor " + "performance creating\nlarge sparse files. The next " + "operation might take a very long time!\n" + "Creating sparse output file ...\n"); + else if (fs_type == 0x517b) + Printf("WARNING: You're using SMBFS and if the remote share " + "isn't Samba but a Windows\ncomputer then the clone " + "operation will be very inefficient and may fail!\n"); +#endif + + if (!opt.no_action && (ftruncate(fd_out, filesize) == -1)) { + int err = errno; + perr_printf("ftruncate failed for file '%s'", opt.output); +#ifndef NO_STATFS + if (fs_type) + Printf("Destination filesystem type is 0x%lx.\n", + (unsigned long)fs_type); +#endif + if (err == E2BIG) { + Printf("Your system or the destination filesystem " + "doesn't support large files.\n"); +#ifndef NO_STATFS + if (fs_type == 0x517b) { + Printf("SMBFS needs minimum Linux kernel " + "version 2.4.25 and\n the 'lfs' option" + "\nfor smbmount to have large " + "file support.\n"); + } +#endif + } else if (err == EPERM) { + Printf("Apparently the destination filesystem doesn't " + "support sparse files.\nYou can overcome this " + "by using the more efficient --save-image " + "option\nof ntfsclone. Use the --restore-image " + "option to restore the image.\n"); + } + exit(1); + } + /* + * If truncate just created a sparse file, the ability + * to generically store big files has been checked, but no + * space has been reserved and available space has probably + * not been checked. Better reset the file so that we write + * sequentially to the end. + */ + if (!opt.no_action) { +#ifdef HAVE_WINDOWS_H + if (ftruncate(fd_out, 0)) + Printf("Failed to reset the output file.\n"); +#else + struct stat st; + int s; + + s = fstat(fd_out, &st); + if (s || (!st.st_blocks && ftruncate(fd_out, 0))) + Printf("Failed to reset the output file.\n"); +#endif + /* Proceed even if ftruncate failed */ + } +} + +static s64 open_image(void) +{ + if (strcmp(opt.volume, "-") == 0) { + if ((fd_in = fileno(stdin)) == -1) + perr_exit("fileno for stdin failed"); +#ifdef HAVE_WINDOWS_H + if (setmode(fd_in,O_BINARY) == -1) + perr_exit("setting binary stdin failed"); +#endif + } else { + if ((fd_in = open(opt.volume, O_RDONLY | O_BINARY)) == -1) + perr_exit("failed to open image"); + } + if (read_all(&fd_in, &image_hdr, NTFSCLONE_IMG_HEADER_SIZE_OLD) == -1) + perr_exit("read_all"); + if (memcmp(image_hdr.magic, IMAGE_MAGIC, IMAGE_MAGIC_SIZE) != 0) + err_exit("Input file is not an image! (invalid magic)\n"); + if (image_hdr.major_ver < NTFSCLONE_IMG_VER_MAJOR_ENDIANNESS_SAFE) { + image_hdr.major_ver = NTFSCLONE_IMG_VER_MAJOR; + image_hdr.minor_ver = NTFSCLONE_IMG_VER_MINOR; +#if (__BYTE_ORDER == __BIG_ENDIAN) + /* + * old image read on a big endian computer, + * assuming it was created big endian and read cpu-wise, + * so we should translate to little endian + */ + Printf("Old image format detected. If the image was created " + "on a little endian architecture it will not " + "work. Use a more recent version of " + "ntfsclone to recreate the image.\n"); + image_hdr.cluster_size = cpu_to_le32(image_hdr.cluster_size); + image_hdr.device_size = cpu_to_sle64(image_hdr.device_size); + image_hdr.nr_clusters = cpu_to_sle64(image_hdr.nr_clusters); + image_hdr.inuse = cpu_to_sle64(image_hdr.inuse); +#endif + image_hdr.offset_to_image_data = + const_cpu_to_le32((sizeof(image_hdr) + + IMAGE_HDR_ALIGN - 1) & -IMAGE_HDR_ALIGN); + image_is_host_endian = TRUE; + } else { + /* safe image : little endian data */ + le32 offset_to_image_data; + int delta; + + if (image_hdr.major_ver > NTFSCLONE_IMG_VER_MAJOR) + err_exit("Do not know how to handle image format " + "version %d.%d. Please obtain a " + "newer version of ntfsclone.\n", + image_hdr.major_ver, + image_hdr.minor_ver); + /* Read the image header data offset. */ + if (read_all(&fd_in, &offset_to_image_data, + sizeof(offset_to_image_data)) == -1) + perr_exit("read_all"); + /* do not translate little endian data */ + image_hdr.offset_to_image_data = offset_to_image_data; + /* + * Read any fields from the header that we have not read yet so + * that the input stream is positioned correctly. This means + * we can support future minor versions that just extend the + * header in a backwards compatible way. + */ + delta = le32_to_cpu(offset_to_image_data) + - (NTFSCLONE_IMG_HEADER_SIZE_OLD + + sizeof(image_hdr.offset_to_image_data)); + if (delta > 0) { + char *dummy_buf; + + dummy_buf = malloc(delta); + if (!dummy_buf) + perr_exit("malloc dummy_buffer"); + if (read_all(&fd_in, dummy_buf, delta) == -1) + perr_exit("read_all"); + free(dummy_buf); + } + } + return le64_to_cpu(image_hdr.device_size); +} + +static s64 open_volume(void) +{ + s64 device_size; + + mount_volume(NTFS_MNT_RDONLY); + + device_size = ntfs_device_size_get(vol->dev, 1); + if (device_size <= 0) + err_exit("Couldn't get device size (%lld)!\n", + (long long)device_size); + + print_volume_size("Current device size", device_size); + + if (device_size < vol->nr_clusters * vol->cluster_size) + err_exit("Current NTFS volume size is bigger than the device " + "size (%lld)!\nCorrupt partition table or incorrect " + "device partitioning?\n", (long long)device_size); + + return device_size; +} + +static void initialise_image_hdr(s64 device_size, s64 inuse) +{ + memcpy(image_hdr.magic, IMAGE_MAGIC, IMAGE_MAGIC_SIZE); + image_hdr.major_ver = NTFSCLONE_IMG_VER_MAJOR; + image_hdr.minor_ver = NTFSCLONE_IMG_VER_MINOR; + image_hdr.cluster_size = cpu_to_le32(vol->cluster_size); + image_hdr.device_size = cpu_to_le64(device_size); + image_hdr.nr_clusters = cpu_to_sle64(vol->nr_clusters); + image_hdr.inuse = cpu_to_le64(inuse); + image_hdr.offset_to_image_data = cpu_to_le32((sizeof(image_hdr) + + IMAGE_HDR_ALIGN - 1) & -IMAGE_HDR_ALIGN); +} + +static void check_output_device(s64 input_size) +{ + if (opt.blkdev_out) { + s64 dest_size; + + if (dev_out) + dest_size = ntfs_device_size_get(dev_out, 1); + else + dest_size = device_size_get(fd_out); + if (dest_size < input_size) + err_exit("Output device is too small (%lld) to fit the " + "NTFS image (%lld).\n", + (long long)dest_size, (long long)input_size); + + check_if_mounted(opt.output, 0); + } else + set_filesize(input_size); +} + +static void ignore_bad_clusters(ntfs_walk_clusters_ctx *image) +{ + ntfs_inode *ni; + ntfs_attr *na; + runlist *rl; + s64 nr_bad_clusters = 0; + static le16 Bad[4] = { + const_cpu_to_le16('$'), const_cpu_to_le16('B'), + const_cpu_to_le16('a'), const_cpu_to_le16('d') + } ; + + if (!(ni = ntfs_inode_open(vol, FILE_BadClus))) + perr_exit("ntfs_open_inode"); + + na = ntfs_attr_open(ni, AT_DATA, Bad, 4); + if (!na) + perr_exit("ntfs_attr_open"); + if (ntfs_attr_map_whole_runlist(na)) + perr_exit("ntfs_attr_map_whole_runlist"); + + for (rl = na->rl; rl->length; rl++) { + s64 lcn = rl->lcn; + + if (lcn == LCN_HOLE || lcn < 0) + continue; + + for (; lcn < rl->lcn + rl->length; lcn++, nr_bad_clusters++) { + if (ntfs_bit_get_and_set(lcn_bitmap.bm, lcn, 0)) + image->inuse--; + } + } + if (nr_bad_clusters) + Printf("WARNING: The disk has %lld or more bad sectors" + " (hardware faults).\n", (long long)nr_bad_clusters); + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) + perr_exit("ntfs_inode_close failed for $BadClus"); +} + +static void check_dest_free_space(u64 src_bytes) +{ +#ifndef HAVE_WINDOWS_H + u64 dest_bytes; + struct statvfs stvfs; + struct stat st; + + if (opt.metadata || opt.blkdev_out || opt.std_out) + return; + /* + * TODO: save_image needs a bit more space than src_bytes + * due to the free space encoding overhead. + */ + if (fstatvfs(fd_out, &stvfs) == -1) { + Printf("WARNING: Unknown free space on the destination: %s\n", + strerror(errno)); + return; + } + + /* If file is a FIFO then there is no point in checking the size. */ + if (!fstat(fd_out, &st)) { + if (S_ISFIFO(st.st_mode)) + return; + } else + Printf("WARNING: fstat failed: %s\n", strerror(errno)); + + dest_bytes = (u64)stvfs.f_frsize * stvfs.f_bfree; + if (!dest_bytes) + dest_bytes = (u64)stvfs.f_bsize * stvfs.f_bfree; + + if (dest_bytes < src_bytes) + err_exit("Destination doesn't have enough free space: " + "%llu MB < %llu MB\n", + (unsigned long long)rounded_up_division(dest_bytes, NTFS_MBYTE), + (unsigned long long)rounded_up_division(src_bytes, NTFS_MBYTE)); +#endif +} + +int main(int argc, char **argv) +{ + ntfs_walk_clusters_ctx image; + s64 device_size; /* input device size in bytes */ + s64 ntfs_size; + unsigned int wiped_total = 0; + + /* make sure the layout of header is not affected by alignments */ + if (offsetof(struct image_hdr, offset_to_image_data) + != IMAGE_OFFSET_OFFSET) { + fprintf(stderr,"ntfsclone is not compiled properly. " + "Please fix\n"); + exit(1); + } + /* print to stderr, stdout can be an NTFS image ... */ + fprintf(stderr, "%s v%s (libntfs-3g)\n", EXEC_NAME, VERSION); + msg_out = stderr; + + parse_options(argc, argv); + + utils_set_locale(); + + if (opt.restore_image) { + device_size = open_image(); + ntfs_size = sle64_to_cpu(image_hdr.nr_clusters) * + le32_to_cpu(image_hdr.cluster_size); + } else { + device_size = open_volume(); + ntfs_size = vol->nr_clusters * vol->cluster_size; + } + // FIXME: This needs to be the cluster size... + ntfs_size += 512; /* add backup boot sector */ + full_device_size = device_size; + + if (opt.std_out) { + if ((fd_out = fileno(stdout)) == -1) + perr_exit("fileno for stdout failed"); + stream_out = stdout; +#ifdef HAVE_WINDOWS_H + if (setmode(fileno(stdout),O_BINARY) == -1) + perr_exit("setting binary stdout failed"); +#endif + } else { + /* device_size_get() might need to read() */ + int flags = O_RDWR | O_BINARY; + + fd_out = 0; + if (!opt.blkdev_out) { + flags |= O_CREAT | O_TRUNC; + if (!opt.overwrite) + flags |= O_EXCL; + } + + if (opt.save_image || opt.metadata_image) { + stream_out = fopen(opt.output,BINWMODE); + if (!stream_out) + perr_exit("Opening file '%s' failed", + opt.output); + fd_out = fileno(stream_out); + } else { +#ifdef HAVE_WINDOWS_H + if (!opt.no_action) { + dev_out = ntfs_device_alloc(opt.output, 0, + &ntfs_device_default_io_ops, NULL); + if (!dev_out + || (dev_out->d_ops->open)(dev_out, flags)) + perr_exit("Opening volume '%s' failed", + opt.output); + } +#else + if (!opt.no_action + && ((fd_out = open(opt.output, flags, + S_IRUSR | S_IWUSR)) == -1)) + perr_exit("Opening file '%s' failed", + opt.output); +#endif + } + + if (!opt.save_image && !opt.metadata_image && !opt.no_action) + check_output_device(ntfs_size); + } + + if (opt.restore_image) { + print_image_info(); + restore_image(); + if (!opt.no_action) + fsync_clone(fd_out); + exit(0); + } + + setup_lcn_bitmap(); + memset(&image, 0, sizeof(image)); + backup_clusters.image = ℑ + + walk_clusters(vol, &backup_clusters); + image.more_use = compare_bitmaps(&lcn_bitmap, + opt.metadata && !opt.metadata_image); + print_disk_usage("", vol->cluster_size, vol->nr_clusters, image.inuse); + + check_dest_free_space(vol->cluster_size * image.inuse); + + ignore_bad_clusters(&image); + + if (opt.save_image) + initialise_image_hdr(device_size, image.inuse); + + if ((opt.std_out && !opt.metadata_image) || !opt.metadata) { + s64 nr_clusters_to_save = image.inuse; + if (opt.std_out && !opt.save_image) + nr_clusters_to_save = vol->nr_clusters; + nr_clusters_to_save++; /* account for the backup boot sector */ + + clone_ntfs(nr_clusters_to_save, image.more_use); + fsync_clone(fd_out); + if (opt.save_image) + fclose(stream_out); + ntfs_umount(vol,FALSE); + free(lcn_bitmap.bm); + + if (opt.adjust_sector_size) { + BOOL dev_opened = TRUE; + int sector_size = 0; + NTFS_BOOT_SECTOR bs; + s64 nbytes = 0; + u16 ntfs_sector_size = 0; + u32 ntfs_cluster_size = 0; + u32 new_sectors_per_cluster = 0; + u64 new_hidden_sectors = 0; + s64 new_number_of_sectors = 0; + + memset(&bs, 0, sizeof(bs)); + + if (!dev_out) { + const int flags = O_RDWR | O_BINARY; + + dev_out = ntfs_device_alloc(opt.output, 0, + &ntfs_device_default_io_ops, NULL); + if (!dev_out || + (dev_out->d_ops->open)(dev_out, flags)) + { + fprintf(stderr, "Opening volume '%s' " + "failed: %s", + opt.output, strerror(errno)); + exit(1); + } + } + + sector_size = ntfs_device_sector_size_get( + /* struct ntfs_device *dev */ + dev_out); + if (sector_size < 0) { + fprintf(stderr, "Error getting sector " + "size of output device: %s\n", + strerror(errno)); + exit(1); + } + + nbytes = dev_out->d_ops->pread( + /* struct ntfs_device *dev */ + dev_out, + /* void *buf */ + &bs, + /* s64 count */ + sizeof(bs), + /* s64 offset */ + 0); + if (nbytes != sizeof(bs)) { + fprintf(stderr, "Error while reading " + "boot sector of NTFS volume: " + "%s\n", + strerror(errno)); + exit(1); + } + + ntfs_sector_size = + /* Note: Misaligned? */ + le16_to_cpu(bs.bpb.bytes_per_sector); + ntfs_cluster_size = + ((u32) ntfs_sector_size) * + ((bs.bpb.sectors_per_cluster > 128) ? + 1 << (256 - bs.bpb.sectors_per_cluster) + : bs.bpb.sectors_per_cluster); + new_sectors_per_cluster = + ntfs_cluster_size / sector_size; + new_hidden_sectors = + (((u64) le32_to_cpu(bs.bpb. + hidden_sectors)) * + ntfs_sector_size) / sector_size; + new_number_of_sectors = + (sle64_to_cpu(bs. + number_of_sectors) * + ntfs_sector_size) / sector_size; + + if ((unsigned) sector_size == ntfs_sector_size); + else if (ntfs_cluster_size < sector_size) { + fprintf(stderr, "Error: No sector size " + "adjustment possible because " + "the cluster size (%lu bytes) " + "is smaller than the sector " + "size of the new device (%d " + "bytes). The NTFS volume needs " + "to be reformatted to be " + "usable on this device.\n", + (unsigned long) + ntfs_cluster_size, + sector_size); + exit(1); + } + else if (new_hidden_sectors > 0xFFFFFFFFUL && + !opt.force) + { + fprintf(stderr, "Error: No sector size " + "adjustment possible because " + "the new hidden sectors value " + "would overflow its type (%llu " + "sectors).\n", + (unsigned long long) + new_hidden_sectors); + exit(1); + } + else if (new_number_of_sectors < 0) { + fprintf(stderr, "Error: No sector size " + "adjustment possible because " + "the new total number of " + "sectors is negative (%lld " + "sectors).\n", + (long long) new_number_of_sectors); + exit(1); + } + + if ((unsigned) sector_size == ntfs_sector_size) { + Printf("No sector size adjustment " + "needed. NTFS sector size " + "already matches device sector " + "size (%d bytes).\n", + sector_size); + } + else { + if (new_sectors_per_cluster <= 128) { + bs.bpb.sectors_per_cluster = + (u8) new_sectors_per_cluster; + } + else { + bs.bpb.sectors_per_cluster = + 257 - + ffs(new_sectors_per_cluster); + } + + bs.bpb.bytes_per_sector = + cpu_to_le16(sector_size); + bs.bpb.hidden_sectors = + cpu_to_le32((new_hidden_sectors > + UINT64_MAX) ? 0U : + (u32) new_hidden_sectors); + bs.number_of_sectors = + cpu_to_sle64(new_number_of_sectors); + + nbytes = dev_out->d_ops->pwrite( + /* struct ntfs_device *dev */ + dev_out, + /* void *buf */ + &bs, + /* s64 count */ + sizeof(bs), + /* s64 offset */ + 0); + if (nbytes != sizeof(bs)) { + fprintf(stderr, "Error while " + "writing boot sector " + "of NTFS volume: %s\n", + strerror(errno)); + exit(1); + } + } + + if (dev_opened) { + (dev_out->d_ops->close)(dev_out); + ntfs_device_free(dev_out); + dev_out = NULL; + } + } + + exit(0); + } + + wipe = 1; + if (opt.metadata_image) { + initialise_image_hdr(device_size, image.inuse); + write_image_hdr(); + } else { + if (dev_out) { + (dev_out->d_ops->close)(dev_out); + dev_out = NULL; + } else + fsync_clone(fd_out); /* sync copy before mounting */ + opt.volume = opt.output; + /* 'force' again mount for dirty volumes (e.g. after resize). + FIXME: use mount flags to avoid potential side-effects in future */ + opt.force++; + ntfs_umount(vol,FALSE); + mount_volume(0 /*NTFS_MNT_NOATIME*/); + } + + free(lcn_bitmap.bm); + setup_lcn_bitmap(); + memset(&image, 0, sizeof(image)); + backup_clusters.image = ℑ + + walk_clusters(vol, &backup_clusters); + + Printf("Num of MFT records = %10lld\n", + (long long)vol->mft_na->initialized_size >> + vol->mft_record_size_bits); + Printf("Num of used MFT records = %10u\n", nr_used_mft_records); + + Printf("Wiped unused MFT data = %10u\n", wiped_unused_mft_data); + Printf("Wiped deleted MFT data = %10u\n", wiped_unused_mft); + Printf("Wiped resident user data = %10u\n", wiped_resident_data); + Printf("Wiped timestamp data = %10u\n", wiped_timestamp_data); + + wiped_total += wiped_unused_mft_data; + wiped_total += wiped_unused_mft; + wiped_total += wiped_resident_data; + wiped_total += wiped_timestamp_data; + Printf("Wiped totally = %10u\n", wiped_total); + + if (opt.metadata_image) + fclose(stream_out); + else + fsync_clone(fd_out); + ntfs_umount(vol,FALSE); + free(lcn_bitmap.bm); + return (0); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscluster.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscluster.8.in new file mode 100644 index 0000000..458a1cc --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscluster.8.in @@ -0,0 +1,124 @@ +.\" Copyright (c) 2003\-2005 Richard Russon. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSCLUSTER 8 "November 2005" "ntfs-3g @VERSION@" +.SH NAME +ntfscluster \- identify files in a specified region of an NTFS volume. +.SH SYNOPSIS +.B ntfscluster +[\fIoptions\fR] \fIdevice\fR +.SH DESCRIPTION +.B ntfscluster +has three modes of operation: +.IR info , +.I sector +and +.IR cluster . +.SS Info +.PP +The default mode, +.I info +is currently not implemented. It will display general information about the +NTFS volume when it is working. +.SS Sector +.PP +The +.I sector +mode will display a list of files that have data in the specified range of +sectors. +.SS Cluster +The +.I cluster +mode will display a list of files that have data in the specified range of +clusters. When the cluster size is one sector, this will be equivalent to the +.I sector +mode of operation. +.SH OPTIONS +Below is a summary of all the options that +.B ntfscluster +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-c\fR, \fB\-\-cluster\fR RANGE +Any files whose data is in this range of clusters will be displayed. +.TP +\fB\-F\fR, \fB\-\-filename\fR NAME +Show information about this file. +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not working with a mounted +volume. Use this option with caution. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-I\fR, \fB\-\-inode\fR NUM +Show information about this inode. +.TP +\fB\-i\fR, \fB\-\-info\fR +This option is not yet implemented. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Reduce the amount of output to a minimum. Naturally, it doesn't make sense to +combine this option with \fB\-\-verbose\fR +.TP +\fB\-s\fR, \fB\-\-sector\fR RANGE +Any files whose data is in this range of sectors will be displayed. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Increase the amount of output that +.B ntfscluster +prints. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license for +.BR ntfscluster . +.SH EXAMPLES +Get some information about the volume /dev/hda1. +.RS +.sp +.B ntfscluster /dev/hda1 +.sp +.RE +Look for files in the first 500 clusters of /dev/hda1. +.RS +.sp +.B ntfscluster \-c 0\-500 /dev/hda1 +.sp +.RE +.SH BUGS +The +.I info +mode isn't implemented yet. +.B ntfscluster +is quite limited, but it has no known bugs. If you find a bug please send an +email describing the problem to the development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfscluster +was written by Richard Russon, with contributions from Anton Altaparmakov. +It was ported to ntfs-3g by Erik Larsson. +.SH AVAILABILITY +.B ntfscluster +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfsinfo (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscluster.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscluster.c new file mode 100644 index 0000000..1f17163 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscluster.c @@ -0,0 +1,580 @@ +/** + * ntfscluster - Part of the Linux-NTFS project. + * + * Copyright (c) 2002-2003 Richard Russon + * Copyright (c) 2005 Anton Altaparmakov + * Copyright (c) 2005-2006 Szabolcs Szakacsits + * + * This utility will locate the owner of any given sector or cluster. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif + +#include "ntfscluster.h" +#include "types.h" +#include "attrib.h" +#include "utils.h" +#include "volume.h" +#include "debug.h" +#include "dir.h" +#include "cluster.h" +/* #include "version.h" */ +#include "logging.h" + +static const char *EXEC_NAME = "ntfscluster"; +static struct options opts; + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + ntfs_log_info("\n%s v%s (libntfs-3g) - Find the owner of any given sector or " + "cluster.\n\n", EXEC_NAME, VERSION); + ntfs_log_info("Copyright (c) 2002-2003 Richard Russon\n"); + ntfs_log_info("Copyright (c) 2005 Anton Altaparmakov\n"); + ntfs_log_info("Copyright (c) 2005-2006 Szabolcs Szakacsits\n"); + ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + ntfs_log_info("\nUsage: %s [options] device\n" + " -i, --info Print information about the volume (default)\n" + "\n" + " -c, --cluster RANGE Look for objects in this range of clusters\n" + " -s, --sector RANGE Look for objects in this range of sectors\n" + " -I, --inode NUM Show information about this inode\n" + " -F, --filename NAME Show information about this file\n" + /* " -l, --last Find the last file on the volume\n" */ + "\n" + " -f, --force Use less caution\n" + " -q, --quiet Less output\n" + " -v, --verbose More output\n" + " -V, --version Version information\n" + " -h, --help Print this help\n\n", + EXEC_NAME); + ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char **argv) +{ + static const char *sopt = "-c:F:fh?I:ilqs:vV"; + static const struct option lopt[] = { + { "cluster", required_argument, NULL, 'c' }, + { "filename", required_argument, NULL, 'F' }, + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "info", no_argument, NULL, 'i' }, + { "inode", required_argument, NULL, 'I' }, + { "last", no_argument, NULL, 'l' }, + { "quiet", no_argument, NULL, 'q' }, + { "sector", required_argument, NULL, 's' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + + int c = -1; + int err = 0; + int ver = 0; + int help = 0; + int levels = 0; + char *end = NULL; + + opterr = 0; /* We'll handle the errors, thank you. */ + + opts.action = act_none; + opts.range_begin = -1; + opts.range_end = -1; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opts.device) { + opts.device = argv[optind-1]; + } else { + opts.device = NULL; + err++; + } + break; + + case 'c': + if ((opts.action == act_none) && + (utils_parse_range(optarg, &opts.range_begin, &opts.range_end, FALSE))) + opts.action = act_cluster; + else + opts.action = act_error; + break; + case 'F': + if (opts.action == act_none) { + opts.action = act_file; + opts.filename = optarg; + } else { + opts.action = act_error; + } + break; + case 'f': + opts.force++; + break; + case 'h': + help++; + break; + case 'I': + if (opts.action == act_none) { + opts.action = act_inode; + opts.inode = strtol(optarg, &end, 0); + if (end && *end) + err++; + } else { + opts.action = act_error; + } + break; + case 'i': + if (opts.action == act_none) + opts.action = act_info; + else + opts.action = act_error; + break; + case 'l': + if (opts.action == act_none) + opts.action = act_last; + else + opts.action = act_error; + break; + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 's': + if ((opts.action == act_none) && + (utils_parse_range(optarg, &opts.range_begin, &opts.range_end, FALSE))) + opts.action = act_sector; + else + opts.action = act_error; + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + ver++; + break; + case '?': + if (strncmp (argv[optind-1], "--log-", 6) == 0) { + if (!ntfs_log_parse_option (argv[optind-1])) + err++; + break; + } + /* fall through */ + default: + if ((optopt == 'c') || (optopt == 's')) + ntfs_log_error("Option '%s' requires an argument.\n", argv[optind-1]); + else + ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); + err++; + break; + } + } + + /* Make sure we're in sync with the log levels */ + levels = ntfs_log_get_levels(); + if (levels & NTFS_LOG_LEVEL_VERBOSE) + opts.verbose++; + if (!(levels & NTFS_LOG_LEVEL_QUIET)) + opts.quiet++; + + if (help || ver) { + opts.quiet = 0; + } else { + if (opts.action == act_none) + opts.action = act_info; + if (opts.action == act_info) + opts.quiet = 0; + + if (opts.device == NULL) { + if (argc > 1) + ntfs_log_error("You must specify exactly one device.\n"); + err++; + } + + if (opts.quiet && opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose at the same time.\n"); + err++; + } + + if (opts.action == act_error) { + ntfs_log_error("You may only specify one action: --info, --cluster, --sector or --last.\n"); + err++; + } else if (opts.range_begin > opts.range_end) { + ntfs_log_error("The range must be in ascending order.\n"); + err++; + } + } + + if (ver) + version(); + if (help || err) + usage(); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver ? 0 : -1)); +} + + +/** + * info + */ +static int info(ntfs_volume *vol) +{ + u64 a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u; + int cb, sb, cps; + u64 uc = 0, mc = 0, fc = 0; + + struct mft_search_ctx *m_ctx; + ntfs_attr_search_ctx *a_ctx; + runlist_element *rl; + ATTR_RECORD *rec; + int z; + int inuse = 0; + + m_ctx = mft_get_search_ctx(vol); + m_ctx->flags_search = FEMR_IN_USE | FEMR_METADATA | FEMR_BASE_RECORD | FEMR_NOT_BASE_RECORD; + while (mft_next_record(m_ctx) == 0) { + + if (!(m_ctx->flags_match & FEMR_IN_USE)) + continue; + + inuse++; + + a_ctx = ntfs_attr_get_search_ctx(m_ctx->inode, NULL); + + while ((rec = find_attribute(AT_UNUSED, a_ctx))) { + + if (!rec->non_resident) + continue; + + rl = ntfs_mapping_pairs_decompress(vol, rec, NULL); + + for (z = 0; rl[z].length > 0; z++) + { + if (rl[z].lcn >= 0) { + if (m_ctx->flags_match & FEMR_METADATA) + mc += rl[z].length; + else + uc += rl[z].length; + } + + } + + free(rl); + } + + ntfs_attr_put_search_ctx(a_ctx); + } + mft_put_search_ctx(m_ctx); + + cb = vol->cluster_size_bits; + sb = vol->sector_size_bits; + cps = cb - sb; + + fc = vol->nr_clusters-mc-uc; + fc <<= cb; + mc <<= cb; + uc <<= cb; + + a = vol->sector_size; + b = vol->cluster_size; + c = 1 << cps; + d = vol->nr_clusters << cb; + e = vol->nr_clusters; + f = vol->nr_clusters >> cps; + g = vol->mft_na->initialized_size >> vol->mft_record_size_bits; + h = inuse; + i = h * 100 / g; + j = fc; + k = fc >> sb; + l = fc >> cb; + m = fc * 100 / b / e; + n = uc; + o = uc >> sb; + p = uc >> cb; + q = uc * 100 / b / e; + r = mc; + s = mc >> sb; + t = mc >> cb; + u = mc * 100 / b / e; + + ntfs_log_info("bytes per sector : %llu\n", (unsigned long long)a); + ntfs_log_info("bytes per cluster : %llu\n", (unsigned long long)b); + ntfs_log_info("sectors per cluster : %llu\n", (unsigned long long)c); + ntfs_log_info("bytes per volume : %llu\n", (unsigned long long)d); + ntfs_log_info("sectors per volume : %llu\n", (unsigned long long)e); + ntfs_log_info("clusters per volume : %llu\n", (unsigned long long)f); + ntfs_log_info("initialized mft records : %llu\n", (unsigned long long)g); + ntfs_log_info("mft records in use : %llu\n", (unsigned long long)h); + ntfs_log_info("mft records percentage : %llu\n", (unsigned long long)i); + ntfs_log_info("bytes of free space : %llu\n", (unsigned long long)j); + ntfs_log_info("sectors of free space : %llu\n", (unsigned long long)k); + ntfs_log_info("clusters of free space : %llu\n", (unsigned long long)l); + ntfs_log_info("percentage free space : %llu\n", (unsigned long long)m); + ntfs_log_info("bytes of user data : %llu\n", (unsigned long long)n); + ntfs_log_info("sectors of user data : %llu\n", (unsigned long long)o); + ntfs_log_info("clusters of user data : %llu\n", (unsigned long long)p); + ntfs_log_info("percentage user data : %llu\n", (unsigned long long)q); + ntfs_log_info("bytes of metadata : %llu\n", (unsigned long long)r); + ntfs_log_info("sectors of metadata : %llu\n", (unsigned long long)s); + ntfs_log_info("clusters of metadata : %llu\n", (unsigned long long)t); + ntfs_log_info("percentage metadata : %llu\n", (unsigned long long)u); + + return 0; +} + +/** + * dump_file + */ +static int dump_file(ntfs_volume *vol, ntfs_inode *ino) +{ + char buffer[1024]; + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *rec; + int i; + runlist *runs; + + utils_inode_get_name(ino, buffer, sizeof(buffer)); + + ntfs_log_info("Dump: %s\n", buffer); + + ctx = ntfs_attr_get_search_ctx(ino, NULL); + + while ((rec = find_attribute(AT_UNUSED, ctx))) { + ntfs_log_info(" 0x%02x - ", (int)le32_to_cpu(rec->type)); + if (rec->non_resident) { + ntfs_log_info("non-resident\n"); + runs = ntfs_mapping_pairs_decompress(vol, rec, NULL); + if (runs) { + ntfs_log_info(" VCN LCN Length\n"); + for (i = 0; runs[i].length > 0; i++) { + ntfs_log_info(" %8lld %8lld %8lld\n", + (long long)runs[i].vcn, + (long long)runs[i].lcn, + (long long) + runs[i].length); + } + free(runs); + } + } else { + ntfs_log_info("resident\n"); + } + } + + ntfs_attr_put_search_ctx(ctx); + return 0; +} + +/** + * print_match + */ +static int print_match(ntfs_inode *ino, ATTR_RECORD *attr, + runlist_element *run, void *data __attribute__((unused))) +{ + char *buffer; + + if (!ino || !attr || !run) + return 1; + + buffer = malloc(MAX_PATH); + if (!buffer) { + ntfs_log_error("!buffer\n"); + return 1; + } + + utils_inode_get_name(ino, buffer, MAX_PATH); + ntfs_log_info("Inode %llu %s", (unsigned long long)ino->mft_no, buffer); + + utils_attr_get_name(ino->vol, attr, buffer, MAX_PATH); + ntfs_log_info("/%s\n", buffer); + + free(buffer); + return 0; +} + +/** + * find_last + */ +static int find_last(ntfs_inode *ino, ATTR_RECORD *attr, runlist_element *run, + void *data) +{ + struct match *m; + + if (!ino || !attr || !run || !data) + return 1; + + m = data; + + if ((run->lcn + run->length) > m->lcn) { + m->inum = ino->mft_no; + m->lcn = run->lcn + run->length; + } + + return 0; +} + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char *argv[]) +{ + ntfs_volume *vol; + ntfs_inode *ino = NULL; + struct match m; + int res; + int result = 1; +#ifdef HAVE_WINDOWS_H + char *unix_name; +#endif + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + res = parse_options(argc, argv); + if (res >= 0) + return (res); + + utils_set_locale(); + + vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY | + (opts.force ? NTFS_MNT_RECOVER : 0)); + if (!vol) + return 1; + + switch (opts.action) { + case act_sector: + if (opts.range_begin == opts.range_end) + ntfs_log_quiet("Searching for sector %llu\n", + (unsigned long long)opts.range_begin); + else + ntfs_log_quiet("Searching for sector range %llu-%llu\n", (unsigned long long)opts.range_begin, (unsigned long long)opts.range_end); + /* Convert to clusters */ + opts.range_begin >>= (vol->cluster_size_bits - vol->sector_size_bits); + opts.range_end >>= (vol->cluster_size_bits - vol->sector_size_bits); + result = cluster_find(vol, opts.range_begin, opts.range_end, (cluster_cb*)&print_match, NULL); + break; + case act_cluster: + if (opts.range_begin == opts.range_end) + ntfs_log_quiet("Searching for cluster %llu\n", + (unsigned long long)opts.range_begin); + else + ntfs_log_quiet("Searching for cluster range %llu-%llu\n", (unsigned long long)opts.range_begin, (unsigned long long)opts.range_end); + result = cluster_find(vol, opts.range_begin, opts.range_end, (cluster_cb*)&print_match, NULL); + break; + case act_file: +#ifdef HAVE_WINDOWS_H + unix_name = ntfs_utils_unix_path(opts.filename); + ino = 0; + if (unix_name) { + ino = ntfs_pathname_to_inode(vol, NULL, + unix_name); + free(unix_name); + } +#else + ino = ntfs_pathname_to_inode(vol, NULL, opts.filename); +#endif + if (ino) + result = dump_file(vol, ino); + break; + case act_inode: + ino = ntfs_inode_open(vol, opts.inode); + if (ino) { + result = dump_file(vol, ino); + ntfs_inode_close(ino); + } else { + ntfs_log_error("Cannot open inode %llu\n", + (unsigned long long)opts.inode); + } + break; + case act_last: + memset(&m, 0, sizeof(m)); + m.lcn = -1; + result = cluster_find(vol, 0, LONG_MAX, (cluster_cb*)&find_last, &m); + if (m.lcn >= 0) { + ino = ntfs_inode_open(vol, m.inum); + if (ino) { + result = dump_file(vol, ino); + ntfs_inode_close(ino); + } else { + ntfs_log_error("Cannot open inode %llu\n", + (unsigned long long) + opts.inode); + } + result = 0; + } else { + result = 1; + } + break; + case act_info: + default: + result = info(vol); + break; + } + + ntfs_umount(vol, FALSE); + return result; +} + + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscluster.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscluster.h new file mode 100644 index 0000000..5a3d6b3 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscluster.h @@ -0,0 +1,63 @@ +/* + * ntfscluster - Part of the Linux-NTFS project. + * + * Copyright (c) 2002-2003 Richard Russon + * + * This utility will locate the owner of any given sector or cluster. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFSCLUSTER_H_ +#define _NTFSCLUSTER_H_ + +#include "types.h" +#include "layout.h" + +enum action { + act_none, + act_info, + act_cluster, + act_sector, + act_inode, + act_file, + act_last, + act_error, +}; + +struct options { + char *device; /* Device/File to work with */ + enum action action; /* What to do */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + int force; /* Override common sense */ + char *filename; /* File to examine */ + u64 inode; /* Inode to examine */ + s64 range_begin; /* Look for objects in this range */ + s64 range_end; +}; + +struct match { + u64 inum; /* Inode number */ + LCN lcn; /* Last cluster in use */ + ATTR_TYPES type; /* Attribute type */ + ntfschar *name; /* Attribute name */ + int name_len; /* Length of attribute name */ +}; + +#endif /* _NTFSCLUSTER_H_ */ + + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscmp.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscmp.8.in new file mode 100644 index 0000000..7196b43 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscmp.8.in @@ -0,0 +1,77 @@ +.\" Copyright (c) 2005\-2006 Szabolcs Szakacsits. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSCMP 8 "April 2006" "ntfs-3g @VERSION@" +.SH NAME +ntfscmp \- compare two NTFS filesystems and tell the differences +.SH SYNOPSIS +.B ntfscmp +[\fIOPTIONS\fR] +.I DEVICE1 +.I DEVICE2 +.br +.SH DESCRIPTION +The +.B ntfscmp +program makes a comparison between two NTFS filesystems from all aspects and +reports all variances it finds. +The filesystems can be on block devices or images files. Ntfscmp can be used +for volume verification however its primary purpose was to be an efficient +development tool, used to quickly locate, identify and check the correctness +of the metadata changes made to NTFS. + +If one is interested only in the NTFS metadata changes then it could be useful +to compare the metadata images created by +using the --metadata option of +.BR ntfsclone (8) +to eliminate the usually uninteresting timestamp changes. + +The terse output of +.B ntfscmp +is intentional because the provided information is enough in each case +to determine the exact differences. This can be achieved, for instance, +if one compares the verbose outputs of +.BR ntfsinfo (8) +for each reported inodes by the +.BR diff (1) +utility. +.SH OPTIONS +Below is a summary of the options that +.B ntfscmp +accepts. +.TP +\fB\-P\fR, \fB\-\-no\-progress\-bar\fR +Don't show progress bars. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +More informational output. +.TP +\fB\-h\fR, \fB\-\-help\fR +Display help and exit. +.SH EXIT CODES +The exit code is 0 on success, non\-zero otherwise. +.SH KNOWN ISSUES +No problem is known. If you would find otherwise then please send +your report to the development team: +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHOR +.B ntfscmp +was written by Szabolcs Szakacsits. +It was ported to ntfs-3g by Erik Larsson. +.SH AVAILABILITY +.B ntfscmp +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfsinfo (8), +.BR ntfscat (8), +.BR diff (1), +.BR ntfsclone (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscmp.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscmp.c new file mode 100644 index 0000000..cabc9c0 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscmp.c @@ -0,0 +1,1012 @@ +/** + * ntfscmp - Part of the Linux-NTFS project. + * + * Copyright (c) 2005-2006 Szabolcs Szakacsits + * Copyright (c) 2005 Anton Altaparmakov + * Copyright (c) 2007 Yura Pakhuchiy + * + * This utility compare two NTFS volumes. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "mst.h" +#include "support.h" +#include "utils.h" +#include "misc.h" +/* #include "version.h" */ + +static const char *EXEC_NAME = "ntfscmp"; + +static const char *invalid_ntfs_msg = +"Apparently device '%s' doesn't have a valid NTFS.\n" +"Maybe you selected the wrong partition? Or the whole disk instead of a\n" +"partition (e.g. /dev/hda, not /dev/hda1)?\n"; + +static const char *corrupt_volume_msg = +"Apparently you have a corrupted NTFS. Please run the filesystem checker\n" +"on Windows by invoking chkdsk /f. Don't forget the /f (force) parameter,\n" +"it's important! You probably also need to reboot Windows to take effect.\n"; + +static const char *hibernated_volume_msg = +"Apparently the NTFS partition is hibernated. Windows must be resumed and\n" +"turned off properly\n"; + + +static struct { + int debug; + int show_progress; + int verbose; + char *vol1; + char *vol2; +} opt; + + +#define NTFS_PROGBAR 0x0001 +#define NTFS_PROGBAR_SUPPRESS 0x0002 + +struct progress_bar { + u64 start; + u64 stop; + int resolution; + int flags; + float unit; +}; + +/* WARNING: don't modify the text, external tools grep for it */ +#define ERR_PREFIX "ERROR" +#define PERR_PREFIX ERR_PREFIX "(%d): " +#define NERR_PREFIX ERR_PREFIX ": " + +__attribute__((format(printf, 2, 3))) +static void perr_printf(int newline, const char *fmt, ...) +{ + va_list ap; + int eo = errno; + + fprintf(stdout, PERR_PREFIX, eo); + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + fprintf(stdout, ": %s", strerror(eo)); + if (newline) + fprintf(stdout, "\n"); + fflush(stdout); + fflush(stderr); +} + +#define perr_print(...) perr_printf(0, __VA_ARGS__) +#define perr_println(...) perr_printf(1, __VA_ARGS__) + +__attribute__((format(printf, 1, 2))) +static void err_printf(const char *fmt, ...) +{ + va_list ap; + + fprintf(stdout, NERR_PREFIX); + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + fflush(stdout); + fflush(stderr); +} + +/** + * err_exit + * + * Print and error message and exit the program. + */ +__attribute__((noreturn)) +__attribute__((format(printf, 1, 2))) +static int err_exit(const char *fmt, ...) +{ + va_list ap; + + fprintf(stdout, NERR_PREFIX); + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + fflush(stdout); + fflush(stderr); + exit(1); +} + +/** + * perr_exit + * + * Print and error message and exit the program + */ +__attribute__((noreturn)) +__attribute__((format(printf, 1, 2))) +static int perr_exit(const char *fmt, ...) +{ + va_list ap; + int eo = errno; + + fprintf(stdout, PERR_PREFIX, eo); + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + printf(": %s\n", strerror(eo)); + fflush(stdout); + fflush(stderr); + exit(1); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +__attribute__((noreturn)) +static void usage(void) +{ + + printf("\nUsage: %s [OPTIONS] DEVICE1 DEVICE2\n" + " Compare two NTFS volumes and tell the differences.\n" + "\n" + " -P, --no-progress-bar Don't show progress bar\n" + " -v, --verbose More output\n" + " -h, --help Display this help\n" +#ifdef DEBUG + " -d, --debug Show debug information\n" +#endif + "\n", EXEC_NAME); + printf("%s%s", ntfs_bugs, ntfs_home); + exit(1); +} + + +static void parse_options(int argc, char **argv) +{ + static const char *sopt = "-dhPv"; + static const struct option lopt[] = { +#ifdef DEBUG + { "debug", no_argument, NULL, 'd' }, +#endif + { "help", no_argument, NULL, 'h' }, + { "no-progress-bar", no_argument, NULL, 'P' }, + { "verbose", no_argument, NULL, 'v' }, + { NULL, 0, NULL, 0 } + }; + + int c; + + memset(&opt, 0, sizeof(opt)); + opt.show_progress = 1; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opt.vol1) { + opt.vol1 = argv[optind - 1]; + } else if (!opt.vol2) { + opt.vol2 = argv[optind - 1]; + } else { + err_printf("Too many arguments!\n"); + usage(); + } + break; +#ifdef DEBUG + case 'd': + opt.debug++; + break; +#endif + case 'h': + case '?': + usage(); + case 'P': + opt.show_progress = 0; + break; + case 'v': + opt.verbose++; + break; + default: + err_printf("Unknown option '%s'.\n", argv[optind - 1]); + usage(); + break; + } + } + + if (opt.vol1 == NULL || opt.vol2 == NULL) { + err_printf("You must specify exactly 2 volumes.\n"); + usage(); + } + + /* Redirect stderr to stdout, note fflush()es are essential! */ + fflush(stdout); + fflush(stderr); + if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1) { + perror("Failed to redirect stderr to stdout"); + exit(1); + } + fflush(stdout); + fflush(stderr); + +#ifdef DEBUG + if (!opt.debug) + if (!freopen("/dev/null", "w", stderr)) + perr_exit("Failed to redirect stderr to /dev/null"); +#endif +} + +static ntfs_attr_search_ctx *attr_get_search_ctx(ntfs_inode *ni) +{ + ntfs_attr_search_ctx *ret; + + if ((ret = ntfs_attr_get_search_ctx(ni, NULL)) == NULL) + perr_println("ntfs_attr_get_search_ctx"); + + return ret; +} + +static void progress_init(struct progress_bar *p, u64 start, u64 stop, int flags) +{ + p->start = start; + p->stop = stop; + p->unit = 100.0 / (stop - start); + p->resolution = 100; + p->flags = flags; +} + +static void progress_update(struct progress_bar *p, u64 current) +{ + float percent; + + if (!(p->flags & NTFS_PROGBAR)) + return; + if (p->flags & NTFS_PROGBAR_SUPPRESS) + return; + + /* WARNING: don't modify the texts, external tools grep for them */ + percent = p->unit * current; + if (current != p->stop) { + if ((current - p->start) % p->resolution) + return; + printf("%6.2f percent completed\r", percent); + } else + printf("100.00 percent completed\n"); + fflush(stdout); +} + +static u64 inumber(ntfs_inode *ni) +{ + if (ni->nr_extents >= 0) + return ni->mft_no; + + return ni->base_ni->mft_no; +} + +static int inode_close(ntfs_inode *ni) +{ + if (ni == NULL) + return 0; + + if (ntfs_inode_close(ni)) { + perr_println("ntfs_inode_close: inode %llu", + (unsigned long long)inumber(ni)); + return -1; + } + return 0; +} + +static inline s64 get_nr_mft_records(ntfs_volume *vol) +{ + return vol->mft_na->initialized_size >> vol->mft_record_size_bits; +} + +#define NTFSCMP_OK 0 +#define NTFSCMP_INODE_OPEN_ERROR 1 +#define NTFSCMP_INODE_OPEN_IO_ERROR 2 +#define NTFSCMP_INODE_OPEN_ENOENT_ERROR 3 +#define NTFSCMP_EXTENSION_RECORD 4 +#define NTFSCMP_INODE_CLOSE_ERROR 5 + +static const char *ntfscmp_errs[] = { + "OK", + "INODE_OPEN_ERROR", + "INODE_OPEN_IO_ERROR", + "INODE_OPEN_ENOENT_ERROR", + "EXTENSION_RECORD", + "INODE_CLOSE_ERROR", + "" +}; + + +static const char *err2string(int err) +{ + return ntfscmp_errs[err]; +} + +static const char *pret2str(void *p) +{ + if (p == NULL) + return "FAILED"; + return "OK"; +} + +static int inode_open(ntfs_volume *vol, MFT_REF mref, ntfs_inode **ni) +{ + *ni = ntfs_inode_open(vol, mref); + if (*ni == NULL) { + if (errno == EIO) + return NTFSCMP_INODE_OPEN_IO_ERROR; + if (errno == ENOENT) + return NTFSCMP_INODE_OPEN_ENOENT_ERROR; + + perr_println("Reading inode %lld failed", (long long)mref); + return NTFSCMP_INODE_OPEN_ERROR; + } + + if ((*ni)->mrec->base_mft_record) { + + if (inode_close(*ni) != 0) + return NTFSCMP_INODE_CLOSE_ERROR; + + return NTFSCMP_EXTENSION_RECORD; + } + + return NTFSCMP_OK; +} + +static ntfs_inode *base_inode(ntfs_attr_search_ctx *ctx) +{ + if (ctx->base_ntfs_ino) + return ctx->base_ntfs_ino; + + return ctx->ntfs_ino; +} + +static void print_inode(u64 inum) +{ + printf("Inode %llu ", (unsigned long long)inum); +} + +static void print_inode_ni(ntfs_inode *ni) +{ + print_inode(inumber(ni)); +} + +static void print_attribute_type(ATTR_TYPES atype) +{ + printf("attribute 0x%x", le32_to_cpu(atype)); +} + +static void print_attribute_name(char *name) +{ + if (name) + printf(":%s", name); +} + +#define GET_ATTR_NAME(a) \ + ((ntfschar *)(((u8 *)(a)) + le16_to_cpu((a)->name_offset))), \ + ((a)->name_length) + +static void free_name(char **name) +{ + if (*name) { + free(*name); + *name = NULL; + } +} + +static char *get_attr_name(u64 mft_no, + ATTR_TYPES atype, + const ntfschar *uname, + const int uname_len) +{ + char *name = NULL; + int name_len; + + if (atype == AT_END) + return NULL; + + name_len = ntfs_ucstombs(uname, uname_len, &name, 0); + if (name_len < 0) { + perr_print("ntfs_ucstombs"); + print_inode(mft_no); + print_attribute_type(atype); + puts(""); + exit(1); + + } else if (name_len > 0) + return name; + + free_name(&name); + return NULL; +} + +static char *get_attr_name_na(ntfs_attr *na) +{ + return get_attr_name(inumber(na->ni), na->type, na->name, na->name_len); +} + +static char *get_attr_name_ctx(ntfs_attr_search_ctx *ctx) +{ + u64 mft_no = inumber(ctx->ntfs_ino); + ATTR_TYPES atype = ctx->attr->type; + + return get_attr_name(mft_no, atype, GET_ATTR_NAME(ctx->attr)); +} + +static void print_attribute(ATTR_TYPES atype, char *name) +{ + print_attribute_type(atype); + print_attribute_name(name); + printf(" "); +} + +static void print_na(ntfs_attr *na) +{ + char *name = get_attr_name_na(na); + print_inode_ni(na->ni); + print_attribute(na->type, name); + free_name(&name); +} + +static void print_attribute_ctx(ntfs_attr_search_ctx *ctx) +{ + char *name = get_attr_name_ctx(ctx); + print_attribute(ctx->attr->type, name); + free_name(&name); +} + +static void print_ctx(ntfs_attr_search_ctx *ctx) +{ + char *name = get_attr_name_ctx(ctx); + print_inode_ni(base_inode(ctx)); + print_attribute(ctx->attr->type, name); + free_name(&name); +} + +static void print_differ(ntfs_attr *na) +{ + print_na(na); + printf("content: DIFFER\n"); +} + +static int cmp_buffer(u8 *buf1, u8 *buf2, long long int size, ntfs_attr *na) +{ + if (memcmp(buf1, buf2, size)) { + print_differ(na); + return -1; + } + return 0; +} + +struct cmp_ia { + INDEX_ALLOCATION *ia; + INDEX_ALLOCATION *tmp_ia; + u8 *bitmap; + u8 *byte; + s64 bm_size; +}; + +static int setup_cmp_ia(ntfs_attr *na, struct cmp_ia *cia) +{ + cia->bitmap = ntfs_attr_readall(na->ni, AT_BITMAP, na->name, + na->name_len, &cia->bm_size); + if (!cia->bitmap) { + perr_println("Failed to readall BITMAP"); + return -1; + } + cia->byte = cia->bitmap; + + cia->tmp_ia = cia->ia = ntfs_malloc(na->data_size); + if (!cia->tmp_ia) + goto free_bm; + + if (ntfs_attr_pread(na, 0, na->data_size, cia->ia) != na->data_size) { + perr_println("Failed to pread INDEX_ALLOCATION"); + goto free_ia; + } + + return 0; +free_ia: + free(cia->ia); +free_bm: + free(cia->bitmap); + return -1; +} + +static void cmp_index_allocation(ntfs_attr *na1, ntfs_attr *na2) +{ + struct cmp_ia cia1, cia2; + int bit, ret1, ret2; + u32 ib_size; + + if (setup_cmp_ia(na1, &cia1)) + return; + if (setup_cmp_ia(na2, &cia2)) + return; + /* + * FIXME: ia can be the same even if the bitmap sizes are different. + */ + if (cia1.bm_size != cia2.bm_size) + goto out; + + if (cmp_buffer(cia1.bitmap, cia2.bitmap, cia1.bm_size, na1)) + goto out; + + if (cmp_buffer((u8 *)cia1.ia, (u8 *)cia2.ia, 0x18, na1)) + goto out; + + ib_size = le32_to_cpu(cia1.ia->index.allocated_size) + 0x18; + + bit = 0; + while ((u8 *)cia1.tmp_ia < (u8 *)cia1.ia + na1->data_size) { + if (*cia1.byte & (1 << bit)) { + ret1 = ntfs_mst_post_read_fixup((NTFS_RECORD *) + cia1.tmp_ia, ib_size); + ret2 = ntfs_mst_post_read_fixup((NTFS_RECORD *) + cia2.tmp_ia, ib_size); + if (ret1 != ret2) { + print_differ(na1); + goto out; + } + + if (ret1 == -1) + continue; + + if (cmp_buffer(((u8 *)cia1.tmp_ia) + 0x18, + ((u8 *)cia2.tmp_ia) + 0x18, + le32_to_cpu(cia1.ia-> + index.index_length), na1)) + goto out; + } + + cia1.tmp_ia = (INDEX_ALLOCATION *)((u8 *)cia1.tmp_ia + ib_size); + cia2.tmp_ia = (INDEX_ALLOCATION *)((u8 *)cia2.tmp_ia + ib_size); + + bit++; + if (bit > 7) { + bit = 0; + cia1.byte++; + } + } +out: + free(cia1.ia); + free(cia2.ia); + free(cia1.bitmap); + free(cia2.bitmap); + return; +} + +static void cmp_attribute_data(ntfs_attr *na1, ntfs_attr *na2) +{ + s64 pos; + s64 count1 = 0, count2; + u8 buf1[NTFS_BUF_SIZE]; + u8 buf2[NTFS_BUF_SIZE]; + + for (pos = 0; pos <= na1->data_size; pos += count1) { + + count1 = ntfs_attr_pread(na1, pos, NTFS_BUF_SIZE, buf1); + count2 = ntfs_attr_pread(na2, pos, NTFS_BUF_SIZE, buf2); + + if (count1 != count2) { + print_na(na1); + printf("abrupt length: %lld != %lld ", + (long long)na1->data_size, + (long long)na2->data_size); + printf("(count: %lld != %lld)", + (long long)count1, (long long)count2); + puts(""); + return; + } + + if (count1 == -1) { + err_printf("%s read error: ", __FUNCTION__); + print_na(na1); + printf("len = %lld, pos = %lld\n", + (long long)na1->data_size, (long long)pos); + exit(1); + } + + if (count1 == 0) { + + if (pos + count1 == na1->data_size) + return; /* we are ready */ + + err_printf("%s read error before EOF: ", __FUNCTION__); + print_na(na1); + printf("%lld != %lld\n", (long long)pos + count1, + (long long)na1->data_size); + exit(1); + } + + if (cmp_buffer(buf1, buf2, count1, na1)) + return; + } + + err_printf("%s read overrun: ", __FUNCTION__); + print_na(na1); + err_printf("(len = %lld, pos = %lld, count = %lld)\n", + (long long)na1->data_size, (long long)pos, (long long)count1); + exit(1); +} + +static int cmp_attribute_header(ATTR_RECORD *a1, ATTR_RECORD *a2) +{ + u32 header_size = offsetof(ATTR_RECORD, resident_end); + + if (a1->non_resident != a2->non_resident) + return 1; + + if (a1->non_resident) { + /* + * FIXME: includes paddings which are not handled by ntfsinfo! + */ + header_size = le32_to_cpu(a1->length); + } + + return memcmp(a1, a2, header_size); +} + +static void cmp_attribute(ntfs_attr_search_ctx *ctx1, + ntfs_attr_search_ctx *ctx2) +{ + ATTR_RECORD *a1 = ctx1->attr; + ATTR_RECORD *a2 = ctx2->attr; + ntfs_attr *na1, *na2; + + if (cmp_attribute_header(a1, a2)) { + print_ctx(ctx1); + printf("header: DIFFER\n"); + } + + na1 = ntfs_attr_open(base_inode(ctx1), a1->type, GET_ATTR_NAME(a1)); + na2 = ntfs_attr_open(base_inode(ctx2), a2->type, GET_ATTR_NAME(a2)); + + if ((!na1 && na2) || (na1 && !na2)) { + print_ctx(ctx1); + printf("open: %s != %s\n", pret2str(na1), pret2str(na2)); + goto close_attribs; + } + + if (na1 == NULL) + goto close_attribs; + + if (na1->data_size != na2->data_size) { + print_na(na1); + printf("length: %lld != %lld\n", + (long long)na1->data_size, (long long)na2->data_size); + goto close_attribs; + } + + if (ntfs_inode_badclus_bad(inumber(ctx1->ntfs_ino), ctx1->attr) == 1) { + /* + * If difference exists then it's already reported at the + * attribute header since the mapping pairs must differ. + */ + goto close_attribs; + } + + if (na1->type == AT_INDEX_ALLOCATION) + cmp_index_allocation(na1, na2); + else + cmp_attribute_data(na1, na2); + +close_attribs: + ntfs_attr_close(na1); + ntfs_attr_close(na2); +} + +static void vprint_attribute(ATTR_TYPES atype, char *name) +{ + if (!opt.verbose) + return; + + printf("0x%x", le32_to_cpu(atype)); + if (name) + printf(":%s", name); + printf(" "); +} + +static void print_attributes(ntfs_inode *ni, + ATTR_TYPES atype1, + ATTR_TYPES atype2, + char *name1, + char *name2) +{ + if (!opt.verbose) + return; + + printf("Walking inode %llu attributes: ", + (unsigned long long)inumber(ni)); + vprint_attribute(atype1, name1); + vprint_attribute(atype2, name2); + printf("\n"); +} + +static int new_name(ntfs_attr_search_ctx *ctx, char *prev_name) +{ + int ret = 0; + char *name = get_attr_name_ctx(ctx); + + if (prev_name && name) { + if (strcmp(prev_name, name) != 0) + ret = 1; + } else if (prev_name || name) + ret = 1; + + free_name(&name); + return ret; + +} + +static int new_attribute(ntfs_attr_search_ctx *ctx, + ATTR_TYPES prev_atype, + char *prev_name) +{ + if (!prev_atype && !prev_name) + return 1; + + if (!ctx->attr->non_resident) + return 1; + + if (prev_atype != ctx->attr->type) + return 1; + + if (new_name(ctx, prev_name)) + return 1; + + if (opt.verbose) { + print_inode(base_inode(ctx)->mft_no); + print_attribute_ctx(ctx); + printf("record %llu lowest_vcn %lld: SKIPPED\n", + (unsigned long long)ctx->ntfs_ino->mft_no, + (long long)sle64_to_cpu(ctx->attr->lowest_vcn)); + } + + return 0; +} + +static void set_prev(char **prev_name, ATTR_TYPES *prev_atype, + char *name, ATTR_TYPES atype) +{ + free_name(prev_name); + if (name) { + *prev_name = strdup(name); + if (!*prev_name) + perr_exit("strdup error"); + } + + *prev_atype = atype; +} + +static void set_cmp_attr(ntfs_attr_search_ctx *ctx, ATTR_TYPES *atype, char **name) +{ + *atype = ctx->attr->type; + + free_name(name); + *name = get_attr_name_ctx(ctx); +} + +static int next_attr(ntfs_attr_search_ctx *ctx, ATTR_TYPES *atype, char **name, + int *err) +{ + int ret; + + ret = ntfs_attrs_walk(ctx); + *err = errno; + if (ret) { + *atype = AT_END; + free_name(name); + } else + set_cmp_attr(ctx, atype, name); + + return ret; +} + +static int cmp_attributes(ntfs_inode *ni1, ntfs_inode *ni2) +{ + int ret = -1; + int old_ret1, ret1 = 0, ret2 = 0; + int errno1 = 0, errno2 = 0; + char *prev_name = NULL, *name1 = NULL, *name2 = NULL; + ATTR_TYPES old_atype1, prev_atype = const_cpu_to_le32(0), atype1, atype2; + ntfs_attr_search_ctx *ctx1, *ctx2; + + if (!(ctx1 = attr_get_search_ctx(ni1))) + return -1; + if (!(ctx2 = attr_get_search_ctx(ni2))) + goto out; + + set_cmp_attr(ctx1, &atype1, &name1); + set_cmp_attr(ctx2, &atype2, &name2); + + while (1) { + + old_atype1 = atype1; + old_ret1 = ret1; + if (!ret1 && (le32_to_cpu(atype1) <= le32_to_cpu(atype2) || + ret2)) + ret1 = next_attr(ctx1, &atype1, &name1, &errno1); + if (!ret2 && (le32_to_cpu(old_atype1) >= le32_to_cpu(atype2) || + old_ret1)) + ret2 = next_attr(ctx2, &atype2, &name2, &errno2); + + print_attributes(ni1, atype1, atype2, name1, name2); + + if (ret1 && ret2) { + if (errno1 != errno2) { + print_inode_ni(ni1); + printf("attribute walk (errno): %d != %d\n", + errno1, errno2); + } + break; + } + + if (ret2 || le32_to_cpu(atype1) < le32_to_cpu(atype2)) { + if (new_attribute(ctx1, prev_atype, prev_name)) { + print_ctx(ctx1); + printf("presence: EXISTS != MISSING\n"); + set_prev(&prev_name, &prev_atype, name1, + atype1); + } + + } else if (ret1 || le32_to_cpu(atype1) > le32_to_cpu(atype2)) { + if (new_attribute(ctx2, prev_atype, prev_name)) { + print_ctx(ctx2); + printf("presence: MISSING != EXISTS \n"); + set_prev(&prev_name, &prev_atype, name2, atype2); + } + + } else /* atype1 == atype2 */ { + if (new_attribute(ctx1, prev_atype, prev_name)) { + cmp_attribute(ctx1, ctx2); + set_prev(&prev_name, &prev_atype, name1, atype1); + } + } + } + + free_name(&prev_name); + ret = 0; + ntfs_attr_put_search_ctx(ctx2); +out: + ntfs_attr_put_search_ctx(ctx1); + return ret; +} + +static int cmp_inodes(ntfs_volume *vol1, ntfs_volume *vol2) +{ + u64 inode; + int ret1, ret2; + ntfs_inode *ni1, *ni2; + struct progress_bar progress; + int pb_flags = 0; /* progress bar flags */ + u64 nr_mft_records, nr_mft_records2; + + if (opt.show_progress) + pb_flags |= NTFS_PROGBAR; + + nr_mft_records = get_nr_mft_records(vol1); + nr_mft_records2 = get_nr_mft_records(vol2); + + if (nr_mft_records != nr_mft_records2) { + + printf("Number of mft records: %lld != %lld\n", + (long long)nr_mft_records, (long long)nr_mft_records2); + + if (nr_mft_records > nr_mft_records2) + nr_mft_records = nr_mft_records2; + } + + progress_init(&progress, 0, nr_mft_records - 1, pb_flags); + progress_update(&progress, 0); + + for (inode = 0; inode < nr_mft_records; inode++) { + + ret1 = inode_open(vol1, (MFT_REF)inode, &ni1); + ret2 = inode_open(vol2, (MFT_REF)inode, &ni2); + + if (ret1 != ret2) { + print_inode(inode); + printf("open: %s != %s\n", + err2string(ret1), err2string(ret2)); + goto close_inodes; + } + + if (ret1 != NTFSCMP_OK) + goto close_inodes; + + if (cmp_attributes(ni1, ni2) != 0) { + inode_close(ni1); + inode_close(ni2); + return -1; + } +close_inodes: + if (inode_close(ni1) != 0) + return -1; + if (inode_close(ni2) != 0) + return -1; + + progress_update(&progress, inode); + } + return 0; +} + +static ntfs_volume *mount_volume(const char *volume) +{ + unsigned long mntflag; + ntfs_volume *vol = NULL; + + if (ntfs_check_if_mounted(volume, &mntflag)) { + perr_println("Failed to check '%s' mount state", volume); + printf("Probably /etc/mtab is missing. It's too risky to " + "continue. You might try\nan another Linux distro.\n"); + exit(1); + } + if (mntflag & NTFS_MF_MOUNTED) { + if (!(mntflag & NTFS_MF_READONLY)) + err_exit("Device '%s' is mounted read-write. " + "You must 'umount' it first.\n", volume); + } + + vol = ntfs_mount(volume, NTFS_MNT_RDONLY); + if (vol == NULL) { + + int err = errno; + + perr_println("Opening '%s' as NTFS failed", volume); + if (err == EINVAL) + printf(invalid_ntfs_msg, volume); + else if (err == EIO) + puts(corrupt_volume_msg); + else if (err == EPERM) + puts(hibernated_volume_msg); + exit(1); + } + + return vol; +} + +int main(int argc, char **argv) +{ + ntfs_volume *vol1; + ntfs_volume *vol2; + + printf("%s v%s (libntfs-3g)\n", EXEC_NAME, VERSION); + + parse_options(argc, argv); + + utils_set_locale(); + + vol1 = mount_volume(opt.vol1); + vol2 = mount_volume(opt.vol2); + + if (cmp_inodes(vol1, vol2) != 0) + exit(1); + + ntfs_umount(vol1, FALSE); + ntfs_umount(vol2, FALSE); + + return (0); +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscp.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscp.8.in new file mode 100644 index 0000000..ba1777a --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscp.8.in @@ -0,0 +1,119 @@ +.\" Copyright (c) 2004\-2007 Yura Pakhuchiy. +.\" Copyright (c) 2005 Richard Russon. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSCP 8 "September 2007" "ntfs-3g @VERSION@" +.SH NAME +ntfscp \- copy file to an NTFS volume. +.SH SYNOPSIS +\fBntfscp\fR [\fIoptions\fR] \fIdevice source_file destination\fR +.SH DESCRIPTION +\fBntfscp\fR will copy file to an NTFS volume. \fIdestination\fR can be either +file or directory. In case if \fIdestination\fR is directory specified by name +then \fIsource_file\fR is copied into this directory, in case if +\fIdestination\fR is directory and specified by inode number then unnamed data +attribute is created for this inode and \fIsource_file\fR is copied into it +(WARNING: it's unusual to have unnamed data streams in the directories, think +twice before specifying directory by inode number). +.SH OPTIONS +Below is a summary of all the options that +.B ntfscp +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-a\fR, \fB\-\-attribute\fR NUM +Write to this attribute. +.TP +\fB\-i\fR, \fB\-\-inode\fR +Treat +.I destination +as inode number. +.TP +\fB\-m\fR, \fB\-\-min-fragments\fR +Minimize fragmentation when allocating space to the attribute. This is +mostly useful when creating big files. +.TP +\fB\-N\fR, \fB\-\-attr\-name\fR NAME +Write to attribute with this name. +.TP +\fB\-n\fR, \fB\-\-no\-action\fR +Use this option to make a test run before doing the real copy operation. +Volume will be opened read\-only and no write will be done. +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not working with a mounted +volume. Use this option with caution. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Suppress some debug/warning/error messages. +.TP +\fB\-t\fR, \fB\-\-timestamp\fR +Copy the modification time of source_file to destination. This is +not compatible with \fB\-\-attr\-name\fR and \fB\-\-attribute\fR. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license +.BR ntfscp . +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Display more debug/warning/error messages. +.SH DATA STREAMS +All data on NTFS is stored in streams, which can have names. A file can have +more than one data streams, but exactly one must have no name. The size of a +file is the size of its unnamed data stream. Usually when you don't specify +stream name you are access to unnamed data stream. If you want access to named +data stream you need to add ":stream_name" to the filename. For example: by +opening "some.mp3:artist" you will open stream "artist" in "some.mp3". But +windows usually prevent you from accessing to named data streams, so you need +to use some program like FAR or utils from cygwin to access named data streams. +.SH EXAMPLES +Copy new_boot.ini from /home/user as boot.ini to the root of an /dev/hda1 NTFS +volume: +.RS +.sp +.B ntfscp /dev/hda1 /home/user/new_boot.ini boot.ini +.sp +.RE +Copy myfile to C:\\some\\path\\myfile:stream (assume that /dev/hda1 letter in +windows is C): +.RS +.sp +.B ntfscp \-N stream /dev/hda1 myfile /some/path +.sp +.RE +.SH BUGS +There are no known problems with \fBntfscp\fR. If you find a bug please send an +email describing the problem to the development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +\fBntfscp\fR was written by Yura Pakhuchiy, with contributions from Anton +Altaparmakov and Hil Liao. +It was ported to ntfs-3g by Erik Larsson. +.SH DEDICATION +With love to Marina Sapego. +.SH AVAILABILITY +.B ntfscp +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g +.hy +.SH SEE ALSO +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscp.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscp.c new file mode 100644 index 0000000..19303b6 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfscp.c @@ -0,0 +1,1179 @@ +/** + * ntfscp - Part of the Linux-NTFS project. + * + * Copyright (c) 2004-2007 Yura Pakhuchiy + * Copyright (c) 2005 Anton Altaparmakov + * Copyright (c) 2006 Hil Liao + * Copyright (c) 2014-2019 Jean-Pierre Andre + * + * This utility will copy file to an NTFS volume. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#include +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_LIBGEN_H +#include +#endif + +#include "types.h" +#include "attrib.h" +#include "utils.h" +#include "volume.h" +#include "dir.h" +#include "bitmap.h" +#include "debug.h" +/* #include "version.h" */ +#include "logging.h" +#include "ntfstime.h" +#include "misc.h" + +struct options { + char *device; /* Device/File to work with */ + char *src_file; /* Source file */ + char *dest_file; /* Destination file */ + char *attr_name; /* Write to attribute with this name. */ + int force; /* Override common sense */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + int minfragments; /* Do minimal fragmentation */ + int timestamp; /* Copy the modification time */ + int noaction; /* Do not write to disk */ + ATTR_TYPES attribute; /* Write to this attribute. */ + int inode; /* Treat dest_file as inode number. */ +}; + +struct ALLOC_CONTEXT { + ntfs_volume *vol; + ntfs_attr *na; + runlist_element *rl; + unsigned char *buf; + s64 gathered_clusters; + s64 wanted_clusters; + s64 new_size; + s64 lcn; + int rl_allocated; + int rl_count; +} ; + +enum STEP { STEP_ERR, STEP_ZERO, STEP_ONE } ; + +static const char *EXEC_NAME = "ntfscp"; +static struct options opts; +static volatile sig_atomic_t caught_terminate = 0; + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + ntfs_log_info("\n%s v%s (libntfs-3g) - Copy file to an NTFS " + "volume.\n\n", EXEC_NAME, VERSION); + ntfs_log_info("Copyright (c) 2004-2007 Yura Pakhuchiy\n"); + ntfs_log_info("Copyright (c) 2005 Anton Altaparmakov\n"); + ntfs_log_info("Copyright (c) 2006 Hil Liao\n"); + ntfs_log_info("Copyright (c) 2014 Jean-Pierre Andre\n"); + ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + ntfs_log_info("\nUsage: %s [options] device src_file dest_file\n\n" + " -a, --attribute NUM Write to this attribute\n" + " -i, --inode Treat dest_file as inode number\n" + " -f, --force Use less caution\n" + " -h, --help Print this help\n" + " -m, --min_fragments Do minimal fragmentation\n" + " -N, --attr-name NAME Write to attribute with this name\n" + " -n, --no-action Do not write to disk\n" + " -q, --quiet Less output\n" + " -t, --timestamp Copy the modification time\n" + " -V, --version Version information\n" + " -v, --verbose More output\n\n", + EXEC_NAME); + ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char **argv) +{ + static const char *sopt = "-a:ifh?mN:no:qtVv"; + static const struct option lopt[] = { + { "attribute", required_argument, NULL, 'a' }, + { "inode", no_argument, NULL, 'i' }, + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "min-fragments", no_argument, NULL, 'm' }, + { "attr-name", required_argument, NULL, 'N' }, + { "no-action", no_argument, NULL, 'n' }, + { "quiet", no_argument, NULL, 'q' }, + { "timestamp", no_argument, NULL, 't' }, + { "version", no_argument, NULL, 'V' }, + { "verbose", no_argument, NULL, 'v' }, + { NULL, 0, NULL, 0 } + }; + + char *s; + int c = -1; + int err = 0; + int ver = 0; + int help = 0; + int levels = 0; + s64 attr; + + opts.device = NULL; + opts.src_file = NULL; + opts.dest_file = NULL; + opts.attr_name = NULL; + opts.inode = 0; + opts.attribute = AT_DATA; + opts.timestamp = 0; + + opterr = 0; /* We'll handle the errors, thank you. */ + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opts.device) { + opts.device = argv[optind - 1]; + } else if (!opts.src_file) { + opts.src_file = argv[optind - 1]; + } else if (!opts.dest_file) { + opts.dest_file = argv[optind - 1]; + } else { + ntfs_log_error("You must specify exactly two " + "files.\n"); + err++; + } + break; + case 'a': + if (opts.attribute != AT_DATA) { + ntfs_log_error("You can specify only one " + "attribute.\n"); + err++; + break; + } + + attr = strtol(optarg, &s, 0); + if (*s) { + ntfs_log_error("Couldn't parse attribute.\n"); + err++; + } else + opts.attribute = (ATTR_TYPES)cpu_to_le32(attr); + break; + case 'i': + opts.inode++; + break; + case 'f': + opts.force++; + break; + case 'h': + help++; + break; + case 'm': + opts.minfragments++; + break; + case 'N': + if (opts.attr_name) { + ntfs_log_error("You can specify only one " + "attribute name.\n"); + err++; + } else + opts.attr_name = argv[optind - 1]; + break; + case 'n': + opts.noaction++; + break; + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 't': + opts.timestamp++; + break; + case 'V': + ver++; + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case '?': + if (strncmp(argv[optind - 1], "--log-", 6) == 0) { + if (!ntfs_log_parse_option(argv[optind - 1])) + err++; + break; + } + /* fall through */ + default: + ntfs_log_error("Unknown option '%s'.\n", + argv[optind - 1]); + err++; + break; + } + } + + /* Make sure we're in sync with the log levels */ + levels = ntfs_log_get_levels(); + if (levels & NTFS_LOG_LEVEL_VERBOSE) + opts.verbose++; + if (!(levels & NTFS_LOG_LEVEL_QUIET)) + opts.quiet++; + + if (help || ver) { + opts.quiet = 0; + } else { + if (!opts.device) { + ntfs_log_error("You must specify a device.\n"); + err++; + } else if (!opts.src_file) { + ntfs_log_error("You must specify a source file.\n"); + err++; + } else if (!opts.dest_file) { + ntfs_log_error("You must specify a destination " + "file.\n"); + err++; + } + + if (opts.quiet && opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose " + "at the same time.\n"); + err++; + } + if (opts.timestamp + && (opts.attr_name || (opts.attribute != AT_DATA))) { + ntfs_log_error("Setting --timestamp is only possible" + " with unname data attribute.\n"); + err++; + } + } + + if (ver) + version(); + if (help || err) + usage(); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver ? 0 : -1)); +} + +/** + * signal_handler - Handle SIGINT and SIGTERM: abort write, sync and exit. + */ +static void signal_handler(int arg __attribute__((unused))) +{ + caught_terminate++; +} + +/* + * Search for the next '0' in a bitmap chunk + * + * Returns the position of next '0' + * or -1 if there are no more '0's + */ + +static int next_zero(struct ALLOC_CONTEXT *alctx, s32 bufpos, s32 count) +{ + s32 index; + unsigned int q,b; + + index = -1; + while ((index < 0) && (bufpos < count)) { + q = alctx->buf[bufpos >> 3]; + if (q == 255) + bufpos = (bufpos | 7) + 1; + else { + b = bufpos & 7; + while ((b < 8) + && ((1 << b) & q)) + b++; + if (b < 8) { + index = (bufpos & -8) | b; + } else { + bufpos = (bufpos | 7) + 1; + } + } + } + return (index); +} + +/* + * Search for the next '1' in a bitmap chunk + * + * Returns the position of next '1' + * or -1 if there are no more '1's + */ + +static int next_one(struct ALLOC_CONTEXT *alctx, s32 bufpos, s32 count) +{ + s32 index; + unsigned int q,b; + + index = -1; + while ((index < 0) && (bufpos < count)) { + q = alctx->buf[bufpos >> 3]; + if (q == 0) + bufpos = (bufpos | 7) + 1; + else { + b = bufpos & 7; + while ((b < 8) + && !((1 << b) & q)) + b++; + if (b < 8) { + index = (bufpos & -8) | b; + } else { + bufpos = (bufpos | 7) + 1; + } + } + } + return (index); +} + +/* + * Allocate a bigger runlist when needed + * + * The allocation is done by multiple of 4096 entries to avoid + * frequent reallocations. + * + * Returns 0 if successful + * -1 otherwise, with errno set accordingly + */ + +static int run_alloc(struct ALLOC_CONTEXT *alctx, s32 count) +{ + runlist_element *prl; + int err; + + err = 0; + if (count > alctx->rl_allocated) { + prl = (runlist_element*)ntfs_malloc( + (alctx->rl_allocated + 4096)*sizeof(runlist_element)); + if (prl) { + if (alctx->rl) { + memcpy(prl, alctx->rl, alctx->rl_allocated + *sizeof(runlist_element)); + free(alctx->rl); + } + alctx->rl = prl; + alctx->rl_allocated += 4096; + } else + err = -1; + } + return (err); +} + +/* + * Merge a new run into the current optimal runlist + * + * The new run is inserted only if it leads to improving the runlist. + * Runs in the current list are dropped when inserting the new one + * make them unneeded. + * The current runlist is sorted by run sizes, and there is no + * terminator. + * + * Returns 0 if successful + * -1 otherwise, with errno set accordingly + */ + +static int merge_run(struct ALLOC_CONTEXT *alctx, s64 lcn, s32 count) +{ + s64 excess; + BOOL replace; + int k; + int drop; + int err; + + err = 0; + if (alctx->rl_count) { + excess = alctx->gathered_clusters + count + - alctx->wanted_clusters; + if (alctx->rl_count > 1) + /* replace if we can reduce the number of runs */ + replace = excess > (alctx->rl[0].length + + alctx->rl[1].length); + else + /* replace if we can shorten a single run */ + replace = (excess > alctx->rl[0].length) + && (count < alctx->rl[0].length); + } else + replace = FALSE; + if (replace) { + /* Using this run, we can now drop smaller runs */ + drop = 0; + excess = alctx->gathered_clusters + count + - alctx->wanted_clusters; + /* Compute how many clusters we can drop */ + while ((drop < alctx->rl_count) + && (alctx->rl[drop].length <= excess)) { + excess -= alctx->rl[drop].length; + drop++; + } + k = 0; + while (((k + drop) < alctx->rl_count) + && (alctx->rl[k + drop].length < count)) { + alctx->rl[k] = alctx->rl[k + drop]; + k++; + } + alctx->rl[k].length = count; + alctx->rl[k].lcn = lcn; + if (drop > 1) { + while ((k + drop) < alctx->rl_count) { + alctx->rl[k + 1] = alctx->rl[k + drop]; + k++; + } + } + alctx->rl_count -= (drop - 1); + alctx->gathered_clusters = alctx->wanted_clusters + excess; + } else { + if (alctx->gathered_clusters < alctx->wanted_clusters) { + /* We had not gathered enough clusters */ + if (!run_alloc(alctx, alctx->rl_count + 1)) { + k = alctx->rl_count - 1; + while ((k >= 0) + && (alctx->rl[k].length > count)) { + alctx->rl[k+1] = alctx->rl[k]; + k--; + } + alctx->rl[k+1].length = count; + alctx->rl[k+1].lcn = lcn; + alctx->rl_count++; + alctx->gathered_clusters += count; + } + } + } + return (err); +} + +/* + * Examine a buffer from the global bitmap + * in order to locate free runs of clusters + * + * Returns STEP_ZERO or STEP_ONE depending on whether the last + * bit examined was in a search for '0' or '1'. This must be + * put as argument to next examination. + * Returns STEP_ERR if there was an error. + */ + +static enum STEP examine_buf(struct ALLOC_CONTEXT *alctx, s64 pos, s64 br, + enum STEP step) +{ + s32 count; + s64 offbuf; /* first bit available in buf */ + s32 bufpos; /* bit index in buf */ + s32 index; + + bufpos = pos & ((alctx->vol->cluster_size << 3) - 1); + offbuf = pos - bufpos; + while (bufpos < (br << 3)) { + if (step == STEP_ZERO) { + /* find first zero */ + index = next_zero(alctx, bufpos, br << 3); + if (index >= 0) { + alctx->lcn = offbuf + index; + step = STEP_ONE; + bufpos = index; + } else { + bufpos = br << 3; + } + } else { + /* find first one */ + index = next_one(alctx, bufpos, br << 3); + if (index >= 0) { + count = offbuf + index - alctx->lcn; + step = STEP_ZERO; + bufpos = index; + if (merge_run(alctx, alctx->lcn, count)) { + step = STEP_ERR; + bufpos = br << 3; + } + } else { + bufpos = br << 3; + } + } + } + return (step); +} + +/* + * Sort the final runlist by lcn's and insert a terminator + * + * Returns 0 if successful + * -1 otherwise, with errno set accordingly + */ + +static int sort_runlist(struct ALLOC_CONTEXT *alctx) +{ + LCN lcn; + VCN vcn; + s64 length; + BOOL sorted; + int err; + int k; + + err = 0; + /* This sorting can be much improved... */ + do { + sorted = TRUE; + for (k=0; (k+1)rl_count; k++) { + if (alctx->rl[k+1].lcn < alctx->rl[k].lcn) { + length = alctx->rl[k].length; + lcn = alctx->rl[k].lcn; + alctx->rl[k] = alctx->rl[k+1]; + alctx->rl[k+1].length = length; + alctx->rl[k+1].lcn = lcn; + sorted = FALSE; + } + } + } while (!sorted); + /* compute the vcns */ + vcn = 0; + for (k=0; krl_count; k++) { + alctx->rl[k].vcn = vcn; + vcn += alctx->rl[k].length; + } + /* Shorten the last run if we got too much */ + if (vcn > alctx->wanted_clusters) { + k = alctx->rl_count - 1; + alctx->rl[k].length -= vcn - alctx->wanted_clusters; + vcn = alctx->wanted_clusters; + } + /* Append terminator */ + if (run_alloc(alctx, alctx->rl_count + 1)) + err = -1; + else { + k = alctx->rl_count++; + alctx->rl[k].vcn = vcn; + alctx->rl[k].length = 0; + alctx->rl[k].lcn = LCN_ENOENT; + } + return (err); +} + +/* + * Update the sizes of an attribute + * + * Returns 0 if successful + * -1 otherwise, with errno set accordingly + */ + +static int set_sizes(struct ALLOC_CONTEXT *alctx, ntfs_attr_search_ctx *ctx) +{ + ntfs_attr *na; + ntfs_inode *ni; + ATTR_RECORD *attr; + + na = alctx->na; + /* Compute the sizes */ + na->data_size = alctx->new_size; + na->initialized_size = 0; + na->allocated_size = alctx->wanted_clusters + << alctx->vol->cluster_size_bits; + /* Feed the sizes into the attribute */ + attr = ctx->attr; + attr->non_resident = 1; + attr->data_size = cpu_to_sle64(na->data_size); + attr->initialized_size = cpu_to_sle64(na->initialized_size); + attr->allocated_size = cpu_to_sle64(na->allocated_size); + if (na->data_flags & ATTR_IS_SPARSE) + attr->compressed_size = cpu_to_sle64(na->compressed_size); + /* Copy the unnamed data attribute sizes to inode */ + if ((opts.attribute == AT_DATA) && !na->name_len) { + ni = na->ni; + ni->data_size = na->data_size; + if (na->data_flags & ATTR_IS_SPARSE) { + ni->allocated_size = na->compressed_size; + ni->flags |= FILE_ATTR_SPARSE_FILE; + } else + ni->allocated_size = na->allocated_size; + } + return (0); +} + +/* + * Assign a runlist to an attribute and store + * + * Returns 0 if successful + * -1 otherwise, with errno set accordingly + */ + +static int assign_runlist(struct ALLOC_CONTEXT *alctx) +{ + ntfs_attr *na; + ntfs_attr_search_ctx *ctx; + int k; + int err; + + err = 0; + na = alctx->na; + if (na->rl) + free(na->rl); + na->rl = alctx->rl; + /* Allocate the clusters */ + for (k=0; ((k + 1) < alctx->rl_count) && !err; k++) { + if (ntfs_bitmap_set_run(alctx->vol->lcnbmp_na, + alctx->rl[k].lcn, alctx->rl[k].length)) { + err = -1; + } + } + na->allocated_size = alctx->wanted_clusters + << alctx->vol->cluster_size_bits; + NAttrSetNonResident(na); + NAttrSetFullyMapped(na); + if (err || ntfs_attr_update_mapping_pairs(na, 0)) { + err = -1; + } else { + ctx = ntfs_attr_get_search_ctx(alctx->na->ni, NULL); + if (ctx) { + if (ntfs_attr_lookup(opts.attribute, na->name, + na->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + err = -1; + } else { + if (set_sizes(alctx, ctx)) + err = -1; + } + } else + err = -1; + ntfs_attr_put_search_ctx(ctx); + } + return (err); +} + +/* + * Find the runs which minimize fragmentation + * + * Only the first and second data zones are examined, the MFT zone + * is preserved. + * + * Returns 0 if successful + * -1 otherwise, with errno set accordingly + */ + +static int find_best_runs(struct ALLOC_CONTEXT *alctx) +{ + ntfs_volume *vol; + s64 pos; /* bit index in bitmap */ + s64 br; /* byte count in buf */ + int err; + enum STEP step; + + err = 0; + vol = alctx->vol; + /* examine the first data zone */ + pos = vol->mft_zone_end; + br = vol->cluster_size; + step = STEP_ZERO; + while ((step != STEP_ERR) + && (br == vol->cluster_size) + && (pos < vol->nr_clusters)) { + br = ntfs_attr_pread(vol->lcnbmp_na, + (pos >> 3) & -vol->cluster_size, + vol->cluster_size, alctx->buf); + if (br > 0) { + step = examine_buf(alctx, pos, br, step); + pos = (pos | ((vol->cluster_size << 3) - 1)) + 1; + } + } + /* examine the second data zone */ + pos = 0; + br = vol->cluster_size; + step = STEP_ZERO; + while ((step != STEP_ERR) + && (br == vol->cluster_size) + && (pos < vol->mft_zone_start)) { + br = ntfs_attr_pread(vol->lcnbmp_na, + (pos >> 3) & -vol->cluster_size, + vol->cluster_size, alctx->buf); + if (br > 0) { + step = examine_buf(alctx, pos, br, step); + pos = (pos | ((vol->cluster_size << 3) - 1)) + 1; + } + } + if (alctx->gathered_clusters < alctx->wanted_clusters) { + errno = ENOSPC; + ntfs_log_error("Error : not enough space on device\n"); + err = -1; + } else { + if ((step == STEP_ERR) || sort_runlist(alctx)) + err = -1; + } + return (err); +} + +/* + * Preallocate clusters with minimal fragmentation + * + * Returns 0 if successful + * -1 otherwise, with errno set accordingly + */ + +static int preallocate(ntfs_attr *na, s64 new_size) +{ + struct ALLOC_CONTEXT *alctx; + ntfs_volume *vol; + int err; + + err = 0; + vol = na->ni->vol; + alctx = (struct ALLOC_CONTEXT*)ntfs_malloc(sizeof(struct ALLOC_CONTEXT)); + if (alctx) { + alctx->buf = (unsigned char*)ntfs_malloc(vol->cluster_size); + if (alctx->buf) { + alctx->na = na; + alctx->vol = vol; + alctx->rl_count = 0; + alctx->rl_allocated = 0; + alctx->rl = (runlist_element*)NULL; + alctx->new_size = new_size; + alctx->wanted_clusters = (new_size + + vol->cluster_size - 1) + >> vol->cluster_size_bits; + alctx->gathered_clusters = 0; + if (find_best_runs(alctx)) + err = -1; + if (!err && !opts.noaction) { + if (assign_runlist(alctx)) + err = -1; + } else + free(alctx->rl); + free(alctx->buf); + } else + err = -1; + free(alctx); + } else + err = -1; + return (err); +} + +/** + * Create a regular file under the given directory inode + * + * It is a wrapper function to ntfs_create(...) + * + * Return: the created file inode + */ +static ntfs_inode *ntfs_new_file(ntfs_inode *dir_ni, + const char *filename) +{ + ntfschar *ufilename; + /* inode to the file that is being created */ + ntfs_inode *ni; + int ufilename_len; + + /* ntfs_mbstoucs(...) will allocate memory for ufilename if it's NULL */ + ufilename = NULL; + ufilename_len = ntfs_mbstoucs(filename, &ufilename); + if (ufilename_len == -1) { + ntfs_log_perror("ERROR: Failed to convert '%s' to unicode", + filename); + return NULL; + } + ni = ntfs_create(dir_ni, const_cpu_to_le32(0), ufilename, ufilename_len, S_IFREG); + free(ufilename); + return ni; +} + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char *argv[]) +{ + FILE *in; + struct stat st; + ntfs_volume *vol; + ntfs_inode *out; + ntfs_attr *na; + int flags = 0; + int res; + int result = 1; + s64 new_size; + u64 offset; + char *buf; + s64 br, bw; + ntfschar *attr_name; + int attr_name_len = 0; +#ifdef HAVE_WINDOWS_H + char *unix_name; +#endif + + ntfs_log_set_handler(ntfs_log_handler_stderr); + + res = parse_options(argc, argv); + if (res >= 0) + return (res); + + utils_set_locale(); + + /* Set SIGINT handler. */ + if (signal(SIGINT, signal_handler) == SIG_ERR) { + ntfs_log_perror("Failed to set SIGINT handler"); + return 1; + } + /* Set SIGTERM handler. */ + if (signal(SIGTERM, signal_handler) == SIG_ERR) { + ntfs_log_perror("Failed to set SIGTERM handler"); + return 1; + } + + if (opts.noaction) + flags = NTFS_MNT_RDONLY; + if (opts.force) + flags |= NTFS_MNT_RECOVER; + + vol = utils_mount_volume(opts.device, flags); + if (!vol) { + ntfs_log_perror("ERROR: couldn't mount volume"); + return 1; + } + + if ((vol->flags & VOLUME_IS_DIRTY) && !opts.force) + goto umount; + + NVolSetCompression(vol); /* allow compression */ + if (ntfs_volume_get_free_space(vol)) { + ntfs_log_perror("ERROR: couldn't get free space"); + goto umount; + } + + { + struct stat fst; + if (stat(opts.src_file, &fst) == -1) { + ntfs_log_perror("ERROR: Couldn't stat source file"); + goto umount; + } + new_size = fst.st_size; + } + ntfs_log_verbose("New file size: %lld\n", (long long)new_size); + + in = fopen(opts.src_file, "r"); + if (!in) { + ntfs_log_perror("ERROR: Couldn't open source file"); + goto umount; + } + + if (opts.inode) { + s64 inode_num; + char *s; + + inode_num = strtoll(opts.dest_file, &s, 0); + if (*s) { + ntfs_log_error("ERROR: Couldn't parse inode number.\n"); + goto close_src; + } + out = ntfs_inode_open(vol, inode_num); + } else { +#ifdef HAVE_WINDOWS_H + unix_name = ntfs_utils_unix_path(opts.dest_file); + if (unix_name) { + out = ntfs_pathname_to_inode(vol, NULL, unix_name); + } else + out = (ntfs_inode*)NULL; +#else + out = ntfs_pathname_to_inode(vol, NULL, opts.dest_file); +#endif + } + if (!out) { + /* Copy the file if the dest_file's parent dir can be opened. */ + char *parent_dirname; + char *filename; + ntfs_inode *dir_ni; + ntfs_inode *ni; + char *dirname_last_whack; + +#ifdef HAVE_WINDOWS_H + filename = basename(unix_name); + parent_dirname = strdup(unix_name); +#else + filename = basename(opts.dest_file); + parent_dirname = strdup(opts.dest_file); +#endif + if (!parent_dirname) { + ntfs_log_perror("strdup() failed"); + goto close_src; + } + dirname_last_whack = strrchr(parent_dirname, '/'); + if (dirname_last_whack) { + if (dirname_last_whack == parent_dirname) + dirname_last_whack[1] = 0; + else + *dirname_last_whack = 0; + dir_ni = ntfs_pathname_to_inode(vol, NULL, + parent_dirname); + } else { + ntfs_log_verbose("Target path does not contain '/'. " + "Using root directory as parent.\n"); + dir_ni = ntfs_inode_open(vol, FILE_root); + } + if (dir_ni) { + if (!(dir_ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)) { + /* Remove the last '/' for estetic reasons. */ + dirname_last_whack[0] = 0; + ntfs_log_error("The file '%s' already exists " + "and is not a directory. " + "Aborting.\n", parent_dirname); + free(parent_dirname); + ntfs_inode_close(dir_ni); + goto close_src; + } + ntfs_log_verbose("Creating a new file '%s' under '%s'" + "\n", filename, parent_dirname); + ni = ntfs_new_file(dir_ni, filename); + ntfs_inode_close(dir_ni); + if (!ni) { + ntfs_log_perror("Failed to create '%s' under " + "'%s'", filename, + parent_dirname); + free(parent_dirname); + goto close_src; + } + out = ni; + } else { + ntfs_log_perror("ERROR: Couldn't open '%s'", + parent_dirname); + free(parent_dirname); + goto close_src; + } + free(parent_dirname); + } + /* The destination is a directory. */ + if ((out->mrec->flags & MFT_RECORD_IS_DIRECTORY) && !opts.inode) { + char *filename; + char *overwrite_filename; + int overwrite_filename_len; + ntfs_inode *ni; + ntfs_inode *dir_ni; + int filename_len; + int dest_dirname_len; + + filename = basename(opts.src_file); + dir_ni = out; + filename_len = strlen(filename); + dest_dirname_len = strlen(opts.dest_file); + overwrite_filename_len = filename_len+dest_dirname_len + 2; + overwrite_filename = malloc(overwrite_filename_len); + if (!overwrite_filename) { + ntfs_log_perror("ERROR: Failed to allocate %i bytes " + "memory for the overwrite filename", + overwrite_filename_len); + ntfs_inode_close(out); + goto close_src; + } +#ifdef HAVE_WINDOWS_H + strcpy(overwrite_filename, unix_name); +#else + strcpy(overwrite_filename, opts.dest_file); +#endif + if (overwrite_filename[dest_dirname_len - 1] != '/') { + strcat(overwrite_filename, "/"); + } + strcat(overwrite_filename, filename); + ni = ntfs_pathname_to_inode(vol, dir_ni, overwrite_filename); + /* Does a file with the same name exist in the dest dir? */ + if (ni) { + ntfs_log_verbose("Destination path has a file with " + "the same name\nOverwriting the file " + "'%s'\n", overwrite_filename); + ntfs_inode_close(out); + out = ni; + } else { + ntfs_log_verbose("Creating a new file '%s' under " + "'%s'\n", filename, opts.dest_file); + ni = ntfs_new_file(dir_ni, filename); + ntfs_inode_close(dir_ni); + if (!ni) { + ntfs_log_perror("ERROR: Failed to create the " + "destination file under '%s'", + opts.dest_file); + free(overwrite_filename); + goto close_src; + } + out = ni; + } + free(overwrite_filename); + } + + attr_name = ntfs_str2ucs(opts.attr_name, &attr_name_len); + if (!attr_name) { + ntfs_log_perror("ERROR: Failed to parse attribute name '%s'", + opts.attr_name); + goto close_dst; + } + + na = ntfs_attr_open(out, opts.attribute, attr_name, attr_name_len); + if (!na) { + if (errno != ENOENT) { + ntfs_log_perror("ERROR: Couldn't open attribute"); + goto close_dst; + } + /* Requested attribute isn't present, add it. */ + if (ntfs_attr_add(out, opts.attribute, attr_name, + attr_name_len, NULL, 0)) { + ntfs_log_perror("ERROR: Couldn't add attribute"); + goto close_dst; + } + na = ntfs_attr_open(out, opts.attribute, attr_name, + attr_name_len); + if (!na) { + ntfs_log_perror("ERROR: Couldn't open just added " + "attribute"); + goto close_dst; + } + } + + ntfs_log_verbose("Old file size: %lld\n", (long long)na->data_size); + if (opts.minfragments && NAttrCompressed(na)) { + ntfs_log_info("Warning : Cannot avoid fragmentation" + " of a compressed attribute\n"); + opts.minfragments = 0; + } + if (na->data_size && opts.minfragments) { + if (ntfs_attr_truncate(na, 0)) { + ntfs_log_perror( + "ERROR: Couldn't truncate existing attribute"); + goto close_attr; + } + } + if (na->data_size != new_size) { + if (opts.minfragments) { + /* + * Do a standard truncate() to check whether the + * attribute has to be made non-resident. + * If still resident, preallocation is not needed. + */ + if (ntfs_attr_truncate(na, new_size)) { + ntfs_log_perror( + "ERROR: Couldn't resize attribute"); + goto close_attr; + } + if (NAttrNonResident(na) + && preallocate(na, new_size)) { + ntfs_log_perror( + "ERROR: Couldn't preallocate attribute"); + goto close_attr; + } + } else { + if (ntfs_attr_truncate_solid(na, new_size)) { + ntfs_log_perror( + "ERROR: Couldn't resize attribute"); + goto close_attr; + } + } + } + + buf = malloc(NTFS_BUF_SIZE); + if (!buf) { + ntfs_log_perror("ERROR: malloc failed"); + goto close_attr; + } + + ntfs_log_verbose("Starting write.\n"); + offset = 0; + while (!feof(in)) { + if (caught_terminate) { + ntfs_log_error("SIGTERM or SIGINT received. " + "Aborting write.\n"); + break; + } + br = fread(buf, 1, NTFS_BUF_SIZE, in); + if (!br) { + if (!feof(in)) ntfs_log_perror("ERROR: fread failed"); + break; + } + bw = ntfs_attr_pwrite(na, offset, br, buf); + if (bw != br) { + ntfs_log_perror("ERROR: ntfs_attr_pwrite failed"); + break; + } + offset += bw; + } + if ((na->data_flags & ATTR_COMPRESSION_MASK) + && ntfs_attr_pclose(na)) + ntfs_log_perror("ERROR: ntfs_attr_pclose failed"); + ntfs_log_verbose("Syncing.\n"); + result = 0; + free(buf); +close_attr: + ntfs_attr_close(na); + if (opts.timestamp) { + if (!fstat(fileno(in),&st)) { + s64 change_time = st.st_mtime*10000000LL + + NTFS_TIME_OFFSET; + out->last_data_change_time = cpu_to_sle64(change_time); + ntfs_inode_update_times(out, 0); + } else { + ntfs_log_error("Failed to get the time stamp.\n"); + } + } +close_dst: + while (ntfs_inode_close(out) && !opts.noaction) { + if (errno != EBUSY) { + ntfs_log_error("Sync failed. Run chkdsk.\n"); + break; + } + ntfs_log_error("Device busy. Will retry sync in 3 seconds.\n"); + sleep(3); + } +close_src: + fclose(in); +umount: + ntfs_umount(vol, FALSE); + ntfs_log_verbose("Done.\n"); + return result; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsdecrypt.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsdecrypt.8.in new file mode 100644 index 0000000..0ac7f33 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsdecrypt.8.in @@ -0,0 +1,129 @@ +.\" Copyright (c) 2014 Jean-Pierre Andre +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSDECRYPT 8 "June 2014" "ntfs-3g @VERSION@" +.SH NAME +ntfsdecrypt \- decrypt or update NTFS files encrypted according to EFS +.SH SYNOPSIS +\fBntfsdecrypt\fR [\fIoptions\fR] -k \fIkey.pfx \fIdevice file\fR +.SH DESCRIPTION +.B ntfsdecrypt +decrypts a file from an unmounted device and print the decrypted data +on the standard output. +It can also update an encrypted file with the encryption key unchanged. +.PP +The NTFS file encryption (known as EFS) uses a two-level encryption : +first, the file contents is encrypted with a random symmetric key, then +this symmetric key is encrypted with the public keys of each of the users +allowed to decrypt the file (RSA public key encryptions). +.P +Three symmetric encryption modes are currently implemented in ntfsdecrypt : +DESX (a DES variant), 3DES (triple DES) and AES_256 (an AES variant). +.P +All the encrypted symmetric keys are stored along with the file in a +special extended attribute named "$LOGGED_UTILITY_STREAM". +Usually, at least two users are allowed to read the file : its owner and +the recovery manager who is able to decrypt all the files in a company. +When backing up an encrypted file, it is important to also backup the +corresponding $LOGGED_UTILITY_STREAM, otherwise the file cannot be +decrypted, even by the recovery manager. Also note that encrypted files +are slightly bigger than apparent, and the option "efs_raw" has +to be used when backing up encrypted files with ntfs-3g. +.P +When ntfsdecrypt is used to update a file, the keys and the +$LOGGED_UTILITY_STREAM are kept unchanged, so a single key file has to +be designated. +.P +Note : the EFS encryption is only available in professional versions +of Windows; +.SH OPTIONS +Below is a summary of all the options that +.B ntfsdecrypt +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-i\fR, \fB\-\-inode\fR NUM +Display or update the contents of a file designated through its inode number +instead of its name. +.TP +\fB\-e\fR, \fB\-\-encrypt\fR +Update an existing encrypted file and get the new contents from the +standard input. The full public and private key file has to be designated, +as the symmetric key is kept unchanged, so the private key is needed to +extract it. +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not using a mounted volume. +Use this option with caution. +.TP +\fB\-k\fR, \fB\-\-keyfile\-name\fR key.pfx +Define the file which contains the public and private keys in PKCS#12 format. +This file obviously contains the keys of one of the users allowed to decrypt +or update the file. It has to be extracted from Windows in PKCS#12 format +(its usual suffix is .p12 or .pfx), and it is protected by a passphrase +which has to be typed in for the keys to be extracted. This can be the +key file of any user allowed to read the file, including the one of the +recovery manager. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Suppress some debug/warning/error messages. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license of +.BR ntfsdecrypt . +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Display more debug/warning/error messages. +.SH EXAMPLES +Display the contents of the file hamlet.doc in the directory Documents of +the root of the NTFS file system on the device /dev/sda1 +.RS +.sp +.B ntfsdecrypt -k foo.key /dev/sda1 Documents/hamlet.doc +.sp +.RE +Update the file hamlet.doc +.RS +.sp +.B ntfsdecrypt -k foo.key /dev/sda1 Documents/hamlet.doc < new.doc +.sp +.RE +.SH BUGS +There are no known problems with +.BR ntfsdecrypt . +If you find a bug please send an email describing the problem to the +development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfsdecrypt +was written by Yuval Fledel, Anton Altaparmakov and Yura Pakhuchiy. +It was ported to ntfs-3g by Erik Larsson and upgraded by Jean-Pierre Andre. +.SH AVAILABILITY +.B ntfsdecrypt +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +Read \fBntfs-3g\fR(8) for details on option efs_raw, +.RE +.BR ntfscat (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsdecrypt.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsdecrypt.c new file mode 100644 index 0000000..3fc3431 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsdecrypt.c @@ -0,0 +1,1630 @@ +/** + * ntfsdecrypt - Decrypt ntfs encrypted files. Part of the Linux-NTFS project. + * + * Copyright (c) 2005 Yuval Fledel + * Copyright (c) 2005-2007 Anton Altaparmakov + * Copyright (c) 2007 Yura Pakhuchiy + * Copyright (c) 2014-2015 Jean-Pierre Andre + * + * This utility will decrypt files and print the decrypted data on the standard + * output. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#include +#include + +#include "types.h" +#include "attrib.h" +#include "utils.h" +#include "volume.h" +#include "debug.h" +#include "dir.h" +#include "layout.h" +/* #include "version.h" */ +#include "misc.h" + +typedef gcry_sexp_t ntfs_rsa_private_key; + +#define NTFS_SHA1_THUMBPRINT_SIZE 0x14 + +#define NTFS_CRED_TYPE_CERT_THUMBPRINT const_cpu_to_le32(3) + +#define NTFS_EFS_CERT_PURPOSE_OID_DDF "1.3.6.1.4.1.311.10.3.4" /* decryption */ +#define NTFS_EFS_CERT_PURPOSE_OID_DRF "1.3.6.1.4.1.311.10.3.4.1" /* recovery */ + +typedef enum { + DF_TYPE_UNKNOWN, + DF_TYPE_DDF, /* decryption */ + DF_TYPE_DRF, /* recovery */ +} NTFS_DF_TYPES; + +/** + * enum NTFS_CRYPTO_ALGORITHMS - List of crypto algorithms used by EFS (32 bit) + * + * To choose which one is used in Windows, create or set the REG_DWORD registry + * key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\EFS\ + * AlgorithmID to the value of your chosen crypto algorithm, e.g. to use DesX, + * set AlgorithmID to 0x6604. + * + * Note that the Windows versions I have tried so far (all are high crypto + * enabled) ignore the AlgorithmID value if it is not one of CALG_3DES, + * CALG_DESX, or CALG_AES_256, i.e. you cannot select CALG_DES at all using + * this registry key. It would be interesting to check out encryption on one + * of the "crippled" crypto Windows versions... + */ +typedef enum { + CALG_DES = const_cpu_to_le32(0x6601), + /* If not one of the below three, fall back to standard Des. */ + CALG_3DES = const_cpu_to_le32(0x6603), + CALG_DESX = const_cpu_to_le32(0x6604), + CALG_AES_256 = const_cpu_to_le32(0x6610), +} NTFS_CRYPTO_ALGORITHMS; + +typedef struct { + u64 in_whitening, out_whitening; + u8 des_key[8]; + u64 prev_blk; +} ntfs_desx_ctx; + +/** + * struct ntfs_fek - Decrypted, in-memory file encryption key. + */ + +typedef struct { + gcry_cipher_hd_t gcry_cipher_hd; + le32 alg_id; + u8 *key_data; + gcry_cipher_hd_t *des_gcry_cipher_hd_ptr; + ntfs_desx_ctx desx_ctx; +} ntfs_fek; + +struct options { + char *keyfile; /* .pfx file containing the user's private key. */ + char *device; /* Device/File to work with */ + char *file; /* File to display */ + s64 inode; /* Inode to work with */ + ATTR_TYPES attr; /* Attribute type to display */ + int force; /* Override common sense */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + int encrypt; /* Encrypt */ +}; + +static const char *EXEC_NAME = "ntfsdecrypt"; +static struct options opts; + +static ntfschar EFS[5] = { + const_cpu_to_le16('$'), const_cpu_to_le16('E'), const_cpu_to_le16('F'), + const_cpu_to_le16('S'), const_cpu_to_le16('\0') +}; + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + ntfs_log_info("\n%s v%s (libntfs-3g) - Decrypt files and print on the " + "standard output.\n\n", EXEC_NAME, VERSION); + ntfs_log_info("Copyright (c) 2005 Yuval Fledel\n"); + ntfs_log_info("Copyright (c) 2005 Anton Altaparmakov\n"); + ntfs_log_info("Copyright (c) 2014-2015 Jean-Pierre Andre\n"); + ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + ntfs_log_info("\nUsage: %s [options] -k name.pfx device [file]\n\n" + " -i, --inode num Display this inode\n\n" + " -k --keyfile name.pfx Use file name as the user's private key file.\n" + " -e --encrypt Update an encrypted file\n" + " -f --force Use less caution\n" + " -h --help Print this help\n" + " -q --quiet Less output\n" + " -V --version Version information\n" + " -v --verbose More output\n\n", + EXEC_NAME); + ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char **argv) +{ + static const char *sopt = "-fh?ei:k:qVv"; + static const struct option lopt[] = { + {"encrypt", no_argument, NULL, 'e'}, + {"force", no_argument, NULL, 'f'}, + {"help", no_argument, NULL, 'h'}, + {"inode", required_argument, NULL, 'i'}, + {"keyfile", required_argument, NULL, 'k'}, + {"quiet", no_argument, NULL, 'q'}, + {"version", no_argument, NULL, 'V'}, + {"verbose", no_argument, NULL, 'v'}, + {NULL, 0, NULL, 0} + }; + + int c = -1; + int err = 0; + int ver = 0; + int help = 0; + + opterr = 0; /* We'll handle the errors, thank you. */ + + opts.inode = -1; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opts.device) + opts.device = argv[optind - 1]; + else if (!opts.file) + opts.file = argv[optind - 1]; + else { + ntfs_log_error("You must specify exactly one " + "file.\n"); + err++; + } + break; + case 'e': + opts.encrypt++; + break; + case 'f': + opts.force++; + break; + case 'h': + help++; + break; + case 'k': + if (!opts.keyfile) + opts.keyfile = argv[optind - 1]; + else { + ntfs_log_error("You must specify exactly one " + "key file.\n"); + err++; + } + break; + case 'i': + if (opts.inode != -1) + ntfs_log_error("You must specify exactly one " + "inode.\n"); + else if (utils_parse_size(optarg, &opts.inode, FALSE)) + break; + else + ntfs_log_error("Couldn't parse inode number.\n"); + err++; + break; + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 'V': + ver++; + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case '?': + default: + ntfs_log_error("Unknown option '%s'.\n", + argv[optind - 1]); + err++; + break; + } + } + + if (help || ver) { + opts.quiet = 0; + ntfs_log_set_levels(NTFS_LOG_LEVEL_QUIET); + } else { + if (!opts.keyfile) { + ntfs_log_error("You must specify a key file.\n"); + err++; + } else if (opts.device == NULL) { + ntfs_log_error("You must specify a device.\n"); + err++; + } else if (opts.file == NULL && opts.inode == -1) { + ntfs_log_error("You must specify a file or inode with " + "the -i option.\n"); + err++; + } else if (opts.file != NULL && opts.inode != -1) { + ntfs_log_error("You can't specify both a file and " + "inode.\n"); + err++; + } + if (opts.quiet && opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose " + "at the same time.\n"); + err++; + } + } + + if (ver) + version(); + if (help || err) + usage(); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver ? 0 : -1)); +} + +/** + * ntfs_pkcs12_load_pfxfile + */ +static int ntfs_pkcs12_load_pfxfile(const char *keyfile, u8 **pfx, + unsigned *pfx_size) +{ + int f, to_read, total, attempts, br; + struct stat key_stat; + + if (!keyfile || !pfx || !pfx_size) { + ntfs_log_error("You have to specify the key file, a pointer " + "to hold the key file contents, and a pointer " + "to hold the size of the key file contents.\n"); + return -1; + } + f = open(keyfile, O_RDONLY); + if (f == -1) { + ntfs_log_perror("Failed to open key file"); + return -1; + } + if (fstat(f, &key_stat) == -1) { + ntfs_log_perror("Failed to stat key file"); + goto file_out; + } + if (!S_ISREG(key_stat.st_mode)) { + ntfs_log_error("Key file is not a regular file, cannot read " + "it.\n"); + goto file_out; + } + if (!key_stat.st_size) { + ntfs_log_error("Key file has zero size.\n"); + goto file_out; + } + *pfx = malloc(key_stat.st_size + 1); + if (!*pfx) { + ntfs_log_perror("Failed to allocate buffer for key file " + "contents"); + goto file_out; + } + to_read = key_stat.st_size; + total = attempts = 0; + do { + br = read(f, *pfx + total, to_read); + if (br == -1) { + ntfs_log_perror("Failed to read from key file"); + goto free_out; + } + if (!br) + attempts++; + to_read -= br; + total += br; + } while (to_read > 0 && attempts < 3); + close(f); + /* Make sure it is zero terminated. */ + (*pfx)[key_stat.st_size] = 0; + *pfx_size = key_stat.st_size; + return 0; +free_out: + free(*pfx); +file_out: + close(f); + return -1; +} + +/** + * ntfs_crypto_init + */ +static int ntfs_crypto_init(void) +{ + int err; + + /* Initialize gcrypt library. Note: Must come before GNU TLS init. */ + if (gcry_control(GCRYCTL_DISABLE_SECMEM, 0) != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to initialize the gcrypt library.\n"); + return -1; + } + /* Initialize GNU TLS library. Note: Must come after libgcrypt init. */ + err = gnutls_global_init(); + if (err < 0) { + ntfs_log_error("Failed to initialize GNU TLS library: %s\n", + gnutls_strerror(err)); + return -1; + } + return 0; +} + +/** + * ntfs_crypto_deinit + */ +static void ntfs_crypto_deinit(void) +{ + gnutls_global_deinit(); +} + +/** + * ntfs_rsa_private_key_import_from_gnutls + */ +static ntfs_rsa_private_key ntfs_rsa_private_key_import_from_gnutls( + gnutls_x509_privkey_t priv_key) +{ + int i, j; + size_t tmp_size; + gnutls_datum_t rd[6]; + gcry_mpi_t rm[6]; + gcry_sexp_t rsa_key; + + /* Extract the RSA parameters from the GNU TLS private key. */ + if (gnutls_x509_privkey_export_rsa_raw(priv_key, &rd[0], &rd[1], + &rd[2], &rd[3], &rd[4], &rd[5])) { + ntfs_log_error("Failed to export rsa parameters. (Is the " + "key an RSA private key?)\n"); + return NULL; + } + /* Convert each RSA parameter to mpi format. */ + for (i = 0; i < 6; i++) { + if (gcry_mpi_scan(&rm[i], GCRYMPI_FMT_USG, rd[i].data, + rd[i].size, &tmp_size) != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to convert RSA parameter %i " + "to mpi format (size %d)\n", i, + rd[i].size); + rsa_key = NULL; + break; + } + } + /* Release the no longer needed datum values. */ + for (j = 0; j < 6; j++) { + if (rd[j].data && rd[j].size) + gnutls_free(rd[j].data); + } + /* + * Build the gcrypt private key, note libgcrypt uses p and q inversed + * to what gnutls uses. + */ + if (i == 6 && gcry_sexp_build(&rsa_key, NULL, + "(private-key(rsa(n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))", + rm[0], rm[1], rm[2], rm[4], rm[3], rm[5]) != + GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to build RSA private key s-exp.\n"); + rsa_key = NULL; + } + /* Release the no longer needed mpi values. */ + for (j = 0; j < i; j++) + gcry_mpi_release(rm[j]); + return (ntfs_rsa_private_key)rsa_key; +} + +/** + * ntfs_rsa_private_key_release + */ +static void ntfs_rsa_private_key_release(ntfs_rsa_private_key rsa_key) +{ + gcry_sexp_release((gcry_sexp_t)rsa_key); +} + +/** + * ntfs_pkcs12_extract_rsa_key + */ +static ntfs_rsa_private_key ntfs_pkcs12_extract_rsa_key(u8 *pfx, int pfx_size, + char *password, char *thumbprint, int thumbprint_size, + NTFS_DF_TYPES *df_type) +{ + int err, bag_index, flags; + gnutls_datum_t dpfx, dkey; + gnutls_pkcs12_t pkcs12 = NULL; + gnutls_pkcs12_bag_t bag = NULL; + gnutls_x509_privkey_t pkey = NULL; + gnutls_x509_crt_t crt = NULL; + ntfs_rsa_private_key rsa_key = NULL; + char purpose_oid[100]; + size_t purpose_oid_size = sizeof(purpose_oid); + int oid_index; + size_t tp_size = thumbprint_size; + BOOL have_thumbprint = FALSE; + + *df_type = DF_TYPE_UNKNOWN; + /* Create a pkcs12 structure. */ + err = gnutls_pkcs12_init(&pkcs12); + if (err) { + ntfs_log_error("Failed to initialize PKCS#12 structure: %s\n", + gnutls_strerror(err)); + return NULL; + } + /* Convert the PFX file (DER format) to native pkcs12 format. */ + dpfx.data = pfx; + dpfx.size = pfx_size; + err = gnutls_pkcs12_import(pkcs12, &dpfx, GNUTLS_X509_FMT_DER, 0); + if (err) { + ntfs_log_error("Failed to convert the PFX file from DER to " + "native PKCS#12 format: %s\n", + gnutls_strerror(err)); + goto err; + } + /* + * Verify that the password is correct and that the key file has not + * been tampered with. Note if the password has zero length and the + * verification fails, retry with password set to NULL. This is needed + * to get passwordless .pfx files generated with Windows XP SP1 (and + * probably earlier versions of Windows) to work. + */ +retry_verify: + err = gnutls_pkcs12_verify_mac(pkcs12, password); + if (err) { + if (err == GNUTLS_E_MAC_VERIFY_FAILED && + password && !strlen(password)) { + password = NULL; + goto retry_verify; + } + ntfs_log_error("Failed to verify the MAC: %s Is the " + "password correct?\n", gnutls_strerror(err)); + goto err; + } + for (bag_index = 0; ; bag_index++) { + err = gnutls_pkcs12_bag_init(&bag); + if (err) { + ntfs_log_error("Failed to initialize PKCS#12 Bag " + "structure: %s\n", + gnutls_strerror(err)); + goto err; + } + err = gnutls_pkcs12_get_bag(pkcs12, bag_index, bag); + if (err) { + if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { + err = 0; + break; + } + ntfs_log_error("Failed to obtain Bag from PKCS#12 " + "structure: %s\n", + gnutls_strerror(err)); + goto err; + } +check_again: + err = gnutls_pkcs12_bag_get_count(bag); + if (err < 0) { + ntfs_log_error("Failed to obtain Bag count: %s\n", + gnutls_strerror(err)); + goto err; + } + err = gnutls_pkcs12_bag_get_type(bag, 0); + if (err < 0) { + ntfs_log_error("Failed to determine Bag type: %s\n", + gnutls_strerror(err)); + goto err; + } + flags = 0; + switch (err) { + case GNUTLS_BAG_PKCS8_KEY: + flags = GNUTLS_PKCS_PLAIN; + /* FALLTHRU */ + case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY: + err = gnutls_pkcs12_bag_get_data(bag, 0, &dkey); + if (err < 0) { + ntfs_log_error("Failed to obtain Bag data: " + "%s\n", gnutls_strerror(err)); + goto err; + } + err = gnutls_x509_privkey_init(&pkey); + if (err) { + ntfs_log_error("Failed to initialized " + "private key structure: %s\n", + gnutls_strerror(err)); + goto err; + } + /* Decrypt the private key into GNU TLS format. */ + err = gnutls_x509_privkey_import_pkcs8(pkey, &dkey, + GNUTLS_X509_FMT_DER, password, flags); + if (err) { + ntfs_log_error("Failed to convert private " + "key from DER to GNU TLS " + "format: %s\n", + gnutls_strerror(err)); + goto err; + } +#if 0 + /* + * Export the key again, but unencrypted, and output it + * to stderr. Note the output has an RSA header so to + * compare to openssl pkcs12 -nodes -in myfile.pfx + * output need to ignore the part of the key between + * the first "MII..." up to the second "MII...". The + * actual RSA private key begins at the second "MII..." + * and in my testing at least was identical to openssl + * output and was also identical both on big and little + * endian so gnutls should be endianness safe. + */ + char *buf = malloc(8192); + size_t bufsize = 8192; + err = gnutls_x509_privkey_export_pkcs8(pkey, + GNUTLS_X509_FMT_PEM, "", GNUTLS_PKCS_PLAIN, buf, + &bufsize); + if (err) { + ntfs_log_error("eek1\n"); + exit(1); + } + ntfs_log_error("%s\n", buf); + free(buf); +#endif + /* Convert the private key to our internal format. */ + rsa_key = ntfs_rsa_private_key_import_from_gnutls(pkey); + if (!rsa_key) + goto err; + break; + case GNUTLS_BAG_ENCRYPTED: + err = gnutls_pkcs12_bag_decrypt(bag, password); + if (err) { + ntfs_log_error("Failed to decrypt Bag: %s\n", + gnutls_strerror(err)); + goto err; + } + goto check_again; + case GNUTLS_BAG_CERTIFICATE: + err = gnutls_pkcs12_bag_get_data(bag, 0, &dkey); + if (err < 0) { + ntfs_log_error("Failed to obtain Bag data: " + "%s\n", gnutls_strerror(err)); + goto err; + } + err = gnutls_x509_crt_init(&crt); + if (err) { + ntfs_log_error("Failed to initialize " + "certificate structure: %s\n", + gnutls_strerror(err)); + goto err; + } + err = gnutls_x509_crt_import(crt, &dkey, + GNUTLS_X509_FMT_DER); + if (err) { + ntfs_log_error("Failed to convert certificate " + "from DER to GNU TLS format: " + "%s\n", gnutls_strerror(err)); + goto err; + } + oid_index = 0; + /* + * Search in the key purposes for an EFS + * encryption purpose or an EFS recovery + * purpose, and use the first one found. + */ + do { + purpose_oid_size = sizeof(purpose_oid); + err = gnutls_x509_crt_get_key_purpose_oid(crt, + oid_index, + purpose_oid, &purpose_oid_size, NULL); + if (!err) { + purpose_oid[purpose_oid_size - 1] + = '\0'; + if (!strcmp(purpose_oid, + NTFS_EFS_CERT_PURPOSE_OID_DRF)) + *df_type = DF_TYPE_DRF; + else if (!strcmp(purpose_oid, + NTFS_EFS_CERT_PURPOSE_OID_DDF)) + *df_type = DF_TYPE_DDF; + else + oid_index++; + } + } while (!err && (*df_type == DF_TYPE_UNKNOWN)); + if (*df_type == DF_TYPE_UNKNOWN) { + /* End of list reached ? */ + if (err + == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) + ntfs_log_error("Key does not have an " + "EFS purpose OID\n"); + else + ntfs_log_error("Failed to get a key " + "purpose OID : %s ", + gnutls_strerror(err)); + goto err; + } + /* Return the thumbprint to the caller. */ + err = gnutls_x509_crt_get_fingerprint(crt, + GNUTLS_DIG_SHA1, thumbprint, &tp_size); + if (err) { + ntfs_log_error("Failed to get thumbprint: " + "%s\n", gnutls_strerror(err)); + goto err; + } + if (tp_size != NTFS_SHA1_THUMBPRINT_SIZE) { + ntfs_log_error("Invalid thumbprint size %zd. " + "Should be %d.\n", tp_size, + thumbprint_size); + err = EINVAL; + goto err; + } + have_thumbprint = TRUE; + gnutls_x509_crt_deinit(crt); + crt = NULL; + break; + default: + /* We do not care about other types. */ + break; + } + gnutls_pkcs12_bag_deinit(bag); + } +err: + if (rsa_key && (err || *df_type == DF_TYPE_UNKNOWN || + !have_thumbprint)) { + if (!err) + ntfs_log_error("Key type or thumbprint not found, " + "aborting.\n"); + ntfs_rsa_private_key_release(rsa_key); + rsa_key = NULL; + } + if (crt) + gnutls_x509_crt_deinit(crt); + if (pkey) + gnutls_x509_privkey_deinit(pkey); + if (bag) + gnutls_pkcs12_bag_deinit(bag); + if (pkcs12) + gnutls_pkcs12_deinit(pkcs12); + return rsa_key; +} + +/** + * ntfs_buffer_reverse - + * + * This is a utility function for reversing the order of a buffer in place. + * Users of this function should be very careful not to sweep byte order + * problems under the rug. + */ +static inline void ntfs_buffer_reverse(u8 *buf, unsigned buf_size) +{ + unsigned i; + u8 t; + + for (i = 0; i < buf_size / 2; i++) { + t = buf[i]; + buf[i] = buf[buf_size - i - 1]; + buf[buf_size - i - 1] = t; + } +} + +#ifndef HAVE_STRNLEN +/** + * strnlen - strnlen is a gnu extension so emulate it if not present + */ +static size_t strnlen(const char *s, size_t maxlen) +{ + const char *p, *end; + + /* Look for a '\0' character. */ + for (p = s, end = s + maxlen; p < end && *p; p++) + ; + return p - s; +} +#endif /* ! HAVE_STRNLEN */ + +/** + * ntfs_raw_fek_decrypt - + * + * Note: decrypting into the input buffer. + */ +static unsigned ntfs_raw_fek_decrypt(u8 *fek, u32 fek_size, + ntfs_rsa_private_key rsa_key) +{ + gcry_mpi_t fek_mpi; + gcry_sexp_t fek_sexp, fek_sexp2; + gcry_error_t err; + size_t size, padding; + + /* Reverse the raw FEK. */ + ntfs_buffer_reverse(fek, fek_size); + /* Convert the FEK to internal MPI format. */ + err = gcry_mpi_scan(&fek_mpi, GCRYMPI_FMT_USG, fek, fek_size, NULL); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to convert file encryption key to " + "internal MPI format: %s\n", + gcry_strerror(err)); + return 0; + } + /* Create an internal S-expression from the FEK. */ + err = gcry_sexp_build(&fek_sexp, NULL, + "(enc-val (flags) (rsa (a %m)))", fek_mpi); + gcry_mpi_release(fek_mpi); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to create internal S-expression of " + "the file encryption key: %s\n", + gcry_strerror(err)); + return 0; + } + /* Decrypt the FEK. */ + err = gcry_pk_decrypt(&fek_sexp2, fek_sexp, (gcry_sexp_t)rsa_key); + gcry_sexp_release(fek_sexp); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to decrypt the file encryption key: " + "%s\n", gcry_strerror(err)); + return 0; + } + /* Extract the actual FEK from the decrypted raw S-expression. */ + fek_sexp = gcry_sexp_find_token(fek_sexp2, "value", 0); + gcry_sexp_release(fek_sexp2); + if (!fek_sexp) { + ntfs_log_error("Failed to find the decrypted file encryption " + "key in the internal S-expression.\n"); + return 0; + } + /* Convert the decrypted FEK S-expression into MPI format. */ + fek_mpi = gcry_sexp_nth_mpi(fek_sexp, 1, GCRYMPI_FMT_USG); + gcry_sexp_release(fek_sexp); + if (!fek_mpi) { + ntfs_log_error("Failed to convert the decrypted file " + "encryption key S-expression to internal MPI " + "format.\n"); + return 0; + } + /* Convert the decrypted FEK from MPI format to binary data. */ + err = gcry_mpi_print(GCRYMPI_FMT_USG, fek, fek_size, &size, fek_mpi); + gcry_mpi_release(fek_mpi); + if (err != GPG_ERR_NO_ERROR || !size) { + ntfs_log_error("Failed to convert decrypted file encryption " + "key from internal MPI format to binary data: " + "%s\n", gcry_strerror(err)); + return 0; + } + /* + * Finally, remove the PKCS#1 padding and return the size of the + * decrypted FEK. + */ + padding = strnlen((char *)fek, size) + 1; + if (padding > size) { + ntfs_log_error("Failed to remove PKCS#1 padding from " + "decrypted file encryption key.\n"); + return 0; + } + size -= padding; + memmove(fek, fek + padding, size); + return size; +} + +/** + * ntfs_desx_key_expand - expand a 128-bit desx key to the needed 192-bit key + * @src: source buffer containing 128-bit key + * + * Expands the on-disk 128-bit desx key to the needed des key, the in-, and the + * out-whitening keys required to perform desx {de,en}cryption. + */ +static gcry_error_t ntfs_desx_key_expand(const u8 *src, u32 *des_key, + u64 *out_whitening, u64 *in_whitening) +{ + static const u8 *salt1 = (const u8*)"Dan Simon "; + static const u8 *salt2 = (const u8*)"Scott Field"; + static const int salt_len = 12; + gcry_md_hd_t hd1, hd2; + u32 *md; + gcry_error_t err; + + err = gcry_md_open(&hd1, GCRY_MD_MD5, 0); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to open MD5 digest.\n"); + return err; + } + /* Hash the on-disk key. */ + gcry_md_write(hd1, src, 128 / 8); + /* Copy the current hash for efficiency. */ + err = gcry_md_copy(&hd2, hd1); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to copy MD5 digest object.\n"); + goto out; + } + /* Hash with the first salt and store the result. */ + gcry_md_write(hd1, salt1, salt_len); + md = (u32*)gcry_md_read(hd1, 0); + des_key[0] = md[0] ^ md[1]; + des_key[1] = md[2] ^ md[3]; + /* Hash with the second salt and store the result. */ + gcry_md_write(hd2, salt2, salt_len); + md = (u32*)gcry_md_read(hd2, 0); + *out_whitening = *(u64*)md; + *in_whitening = *(u64*)(md + 2); + gcry_md_close(hd2); +out: + gcry_md_close(hd1); + return err; +} + +/** + * ntfs_desx_decrypt + */ +static gcry_error_t ntfs_desx_decrypt(ntfs_fek *fek, u8 *outbuf, + const u8 *inbuf) +{ + gcry_error_t err; + u64 curr_blk; + ntfs_desx_ctx *ctx = &fek->desx_ctx; + + curr_blk = *(const u64*)inbuf; + *(u64*)outbuf = curr_blk ^ ctx->out_whitening; + err = gcry_cipher_encrypt(fek->gcry_cipher_hd, outbuf, 8, NULL, 0); + if (err != GPG_ERR_NO_ERROR) + ntfs_log_error("Des decryption failed (error 0x%x).\n", err); + *(u64*)outbuf ^= ctx->in_whitening ^ ctx->prev_blk; + ctx->prev_blk = curr_blk; + return (err); +} + +/** + * ntfs_desx_encrypt + */ +static gcry_error_t ntfs_desx_encrypt(ntfs_fek *fek, u8 *outbuf, + const u8 *inbuf) +{ + gcry_error_t err; + ntfs_desx_ctx *ctx = &fek->desx_ctx; + + *(u64*)outbuf = *(const u64*)inbuf ^ ctx->in_whitening ^ ctx->prev_blk; + err = gcry_cipher_decrypt(fek->gcry_cipher_hd, outbuf, 8, NULL, 0); + if (err != GPG_ERR_NO_ERROR) + ntfs_log_error("Des decryption failed (error 0x%x).\n", err); + *(u64*)outbuf ^= ctx->out_whitening; + ctx->prev_blk = *(u64*)outbuf; + return (err); +} + +//#define DO_CRYPTO_TESTS 1 + +#ifdef DO_CRYPTO_TESTS + +/* Do not remove this test code from this file! AIA */ +/** + * ntfs_desx_key_expand_test + */ +static BOOL ntfs_desx_key_expand_test(void) +{ + const u8 known_desx_on_disk_key[16] = { + 0xa1, 0xf9, 0xe0, 0xb2, 0x53, 0x23, 0x9e, 0x8f, + 0x0f, 0x91, 0x45, 0xd9, 0x8e, 0x20, 0xec, 0x30 + }; + const u8 known_des_key[8] = { + 0x27, 0xd1, 0x93, 0x09, 0xcb, 0x78, 0x93, 0x1f, + }; + const u8 known_out_whitening[8] = { + 0xed, 0xda, 0x4c, 0x47, 0x60, 0x49, 0xdb, 0x8d, + }; + const u8 known_in_whitening[8] = { + 0x75, 0xf6, 0xa0, 0x1a, 0xc0, 0xca, 0x28, 0x1e + }; + u64 test_out_whitening, test_in_whitening; + union { + u64 u64; + u32 u32[2]; + } test_des_key; + gcry_error_t err; + BOOL res; + + err = ntfs_desx_key_expand(known_desx_on_disk_key, test_des_key.u32, + &test_out_whitening, &test_in_whitening); + if (err != GPG_ERR_NO_ERROR) + res = FALSE; + else + res = test_des_key.u64 == *(u64*)known_des_key && + test_out_whitening == + *(u64*)known_out_whitening && + test_in_whitening == + *(u64*)known_in_whitening; + ntfs_log_error("Testing whether ntfs_desx_key_expand() works: %s\n", + res ? "SUCCESS" : "FAILED"); + return res; +} + +/** + * ntfs_des_test + */ +static BOOL ntfs_des_test(void) +{ + const u8 known_des_key[8] = { + 0x27, 0xd1, 0x93, 0x09, 0xcb, 0x78, 0x93, 0x1f + }; + const u8 known_des_encrypted_data[8] = { + 0xdc, 0xf7, 0x68, 0x2a, 0xaf, 0x48, 0x53, 0x0f + }; + const u8 known_decrypted_data[8] = { + 0xd8, 0xd9, 0x15, 0x23, 0x5b, 0x88, 0x0e, 0x09 + }; + u8 test_decrypted_data[8]; + int res; + gcry_error_t err; + gcry_cipher_hd_t gcry_cipher_hd; + + err = gcry_cipher_open(&gcry_cipher_hd, GCRY_CIPHER_DES, + GCRY_CIPHER_MODE_ECB, 0); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to open des cipher (error 0x%x).\n", + err); + return FALSE; + } + err = gcry_cipher_setkey(gcry_cipher_hd, known_des_key, + sizeof(known_des_key)); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to set des key (error 0x%x.\n", err); + gcry_cipher_close(gcry_cipher_hd); + return FALSE; + } + /* + * Apply DES decryption (ntfs actually uses encryption when decrypting). + */ + err = gcry_cipher_encrypt(gcry_cipher_hd, test_decrypted_data, + sizeof(test_decrypted_data), known_des_encrypted_data, + sizeof(known_des_encrypted_data)); + gcry_cipher_close(gcry_cipher_hd); + if (err) { + ntfs_log_error("Failed to des decrypt test data (error " + "0x%x).\n", err); + return FALSE; + } + res = !memcmp(test_decrypted_data, known_decrypted_data, + sizeof(known_decrypted_data)); + ntfs_log_error("Testing whether des decryption works: %s\n", + res ? "SUCCESS" : "FAILED"); + return res; +} + +#else /* !defined(DO_CRYPTO_TESTS) */ + +/** + * ntfs_desx_key_expand_test + */ +static inline BOOL ntfs_desx_key_expand_test(void) +{ + return TRUE; +} + +/** + * ntfs_des_test + */ +static inline BOOL ntfs_des_test(void) +{ + return TRUE; +} + +#endif /* !defined(DO_CRYPTO_TESTS) */ + +/** + * ntfs_fek_import_from_raw + */ +static ntfs_fek *ntfs_fek_import_from_raw(u8 *fek_buf, unsigned fek_size) +{ + ntfs_fek *fek; + u32 key_size, wanted_key_size, gcry_algo; + int gcry_mode; + gcry_error_t err; + ntfs_desx_ctx *ctx; + + key_size = le32_to_cpup((le32*) fek_buf); + ntfs_log_debug("key_size 0x%x\n", key_size); + if (key_size + 16 > fek_size) { + ntfs_log_debug("Invalid FEK. It was probably decrypted with " + "the incorrect RSA key."); + errno = EINVAL; + return NULL; + } + fek = malloc(((((sizeof(*fek) + 7) & ~7) + key_size + 7) & ~7) + + sizeof(gcry_cipher_hd_t)); + if (!fek) { + errno = ENOMEM; + return NULL; + } + ctx = &fek->desx_ctx; + fek->alg_id = *(le32*)(fek_buf + 8); + //ntfs_log_debug("alg_id 0x%x\n", le32_to_cpu(fek->alg_id)); + fek->key_data = (u8*)fek + ((sizeof(*fek) + 7) & ~7); + memcpy(fek->key_data, fek_buf + 16, key_size); + fek->des_gcry_cipher_hd_ptr = NULL; + *(gcry_cipher_hd_t***)(fek->key_data + ((key_size + 7) & ~7)) = + &fek->des_gcry_cipher_hd_ptr; + switch (fek->alg_id) { + case CALG_DESX: + wanted_key_size = 16; + gcry_algo = GCRY_CIPHER_DES; + gcry_mode = GCRY_CIPHER_MODE_ECB; + break; + case CALG_3DES: + wanted_key_size = 24; + gcry_algo = GCRY_CIPHER_3DES; + gcry_mode = GCRY_CIPHER_MODE_CBC; + break; + case CALG_AES_256: + wanted_key_size = 32; + gcry_algo = GCRY_CIPHER_AES256; + gcry_mode = GCRY_CIPHER_MODE_CBC; + break; + default: + wanted_key_size = 8; + gcry_algo = GCRY_CIPHER_DES; + gcry_mode = GCRY_CIPHER_MODE_CBC; + if (fek->alg_id == CALG_DES) + ntfs_log_error("DES is not supported at present\n"); + else + ntfs_log_error("Unknown crypto algorithm 0x%x\n", + le32_to_cpu(fek->alg_id)); + ntfs_log_error(". Please email %s and say that you saw this " + "message. We will then try to implement " + "support for this algorithm.\n", NTFS_DEV_LIST); + err = EOPNOTSUPP; + goto out; + } + if (key_size != wanted_key_size) { + ntfs_log_error("%s key of %u bytes but needed size is %u " + "bytes, assuming corrupt or incorrect key. " + "Aborting.\n", + gcry_cipher_algo_name(gcry_algo), + (unsigned)key_size, (unsigned)wanted_key_size); + err = EIO; + goto out; + } + err = gcry_cipher_open(&fek->gcry_cipher_hd, gcry_algo, + gcry_mode, 0); + + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("gcry_cipher_open() failed: %s\n", + gcry_strerror(err)); + err = EINVAL; + goto out; + } + if (fek->alg_id == CALG_DESX) { + err = ntfs_desx_key_expand(fek->key_data, (u32*)ctx->des_key, + &ctx->out_whitening, &ctx->in_whitening); + if (err == GPG_ERR_NO_ERROR) + err = gcry_cipher_setkey(fek->gcry_cipher_hd, + ctx->des_key, 8); + } else { + err = gcry_cipher_setkey(fek->gcry_cipher_hd, fek->key_data, + key_size); + } + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("gcry_cipher_setkey() failed: %s\n", + gcry_strerror(err)); + gcry_cipher_close(fek->gcry_cipher_hd); + err = EINVAL; + goto out; + } + return fek; +out: + free(fek); + errno = err; + return NULL; +} + +/** + * ntfs_fek_release + */ +static void ntfs_fek_release(ntfs_fek *fek) +{ + if (fek->des_gcry_cipher_hd_ptr) + gcry_cipher_close(*fek->des_gcry_cipher_hd_ptr); + gcry_cipher_close(fek->gcry_cipher_hd); + free(fek); +} + +/** + * ntfs_df_array_fek_get + */ +static ntfs_fek *ntfs_df_array_fek_get(EFS_DF_ARRAY_HEADER *df_array, + ntfs_rsa_private_key rsa_key, char *thumbprint, + int thumbprint_size) +{ + EFS_DF_HEADER *df_header; + EFS_DF_CREDENTIAL_HEADER *df_cred; + EFS_DF_CERT_THUMBPRINT_HEADER *df_cert; + u8 *fek_buf; + ntfs_fek *fek; + u32 df_count, fek_size; + unsigned i; + + df_count = le32_to_cpu(df_array->df_count); + if (!df_count) + ntfs_log_error("There are no elements in the DF array.\n"); + df_header = (EFS_DF_HEADER*)(df_array + 1); + for (i = 0; i < df_count; i++, df_header = (EFS_DF_HEADER*)( + (u8*)df_header + le32_to_cpu(df_header->df_length))) { + df_cred = (EFS_DF_CREDENTIAL_HEADER*)((u8*)df_header + + le32_to_cpu(df_header->cred_header_offset)); + if (df_cred->type != NTFS_CRED_TYPE_CERT_THUMBPRINT) { + ntfs_log_debug("Credential type is not certificate " + "thumbprint, skipping DF entry.\n"); + continue; + } + df_cert = (EFS_DF_CERT_THUMBPRINT_HEADER*)((u8*)df_cred + + le32_to_cpu( + df_cred->cert_thumbprint_header_offset)); + if ((int)le32_to_cpu(df_cert->thumbprint_size) + != thumbprint_size) { + ntfs_log_error("Thumbprint size %d is not valid " + "(should be %d), skipping this DF " + "entry.\n", + le32_to_cpu(df_cert->thumbprint_size), + thumbprint_size); + continue; + } + if (memcmp((u8*)df_cert + + le32_to_cpu(df_cert->thumbprint_offset), + thumbprint, thumbprint_size)) { + ntfs_log_debug("Thumbprints do not match, skipping " + "this DF entry.\n"); + continue; + } + /* + * The thumbprints match so this is probably the DF entry + * matching the RSA key. Try to decrypt the FEK with it. + */ + fek_size = le32_to_cpu(df_header->fek_size); + fek_buf = (u8*)df_header + le32_to_cpu(df_header->fek_offset); + /* Decrypt the FEK. Note: This is done in place. */ + fek_size = ntfs_raw_fek_decrypt(fek_buf, fek_size, rsa_key); + if (fek_size) { + /* Convert the FEK to our internal format. */ + fek = ntfs_fek_import_from_raw(fek_buf, fek_size); + if (fek) + return fek; + ntfs_log_error("Failed to convert the decrypted file " + "encryption key to internal format.\n"); + } else + ntfs_log_error("Failed to decrypt the file " + "encryption key.\n"); + } + return NULL; +} + +/** + * ntfs_inode_fek_get - + */ +static ntfs_fek *ntfs_inode_fek_get(ntfs_inode *inode, + ntfs_rsa_private_key rsa_key, char *thumbprint, + int thumbprint_size, NTFS_DF_TYPES df_type) +{ + EFS_ATTR_HEADER *efs; + EFS_DF_ARRAY_HEADER *df_array = NULL; + ntfs_fek *fek = NULL; + + /* Obtain the $EFS contents. */ + efs = ntfs_attr_readall(inode, AT_LOGGED_UTILITY_STREAM, EFS, 4, NULL); + if (!efs) { + ntfs_log_perror("Failed to read $EFS attribute"); + return NULL; + } + /* + * Depending on whether the key is a normal key or a data recovery key, + * iterate through the DDF or DRF array, respectively. + */ + if (df_type == DF_TYPE_DDF) { + if (efs->offset_to_ddf_array) + df_array = (EFS_DF_ARRAY_HEADER*)((u8*)efs + + le32_to_cpu(efs->offset_to_ddf_array)); + else + ntfs_log_error("There are no entries in the DDF " + "array.\n"); + } else if (df_type == DF_TYPE_DRF) { + if (efs->offset_to_drf_array) + df_array = (EFS_DF_ARRAY_HEADER*)((u8*)efs + + le32_to_cpu(efs->offset_to_drf_array)); + else + ntfs_log_error("There are no entries in the DRF " + "array.\n"); + } else + ntfs_log_error("Invalid DF type.\n"); + if (df_array) + fek = ntfs_df_array_fek_get(df_array, rsa_key, thumbprint, + thumbprint_size); + free(efs); + return fek; +} + +/** + * ntfs_fek_decrypt_sector + */ +static int ntfs_fek_decrypt_sector(ntfs_fek *fek, u8 *data, const u64 offset) +{ + gcry_error_t err; + + err = gcry_cipher_reset(fek->gcry_cipher_hd); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to reset cipher: %s\n", + gcry_strerror(err)); + return -1; + } + /* + * Note: You may wonder why we are not calling gcry_cipher_setiv() here + * instead of doing it by hand after the decryption. The answer is + * that gcry_cipher_setiv() wants an iv of length 8 bytes but we give + * it a length of 16 for AES256 so it does not like it. + */ + if (fek->alg_id == CALG_DESX) { + int k; + + fek->desx_ctx.prev_blk = 0; + for (k=0; (k < 512) && (err == GPG_ERR_NO_ERROR); k+=8) { + err = ntfs_desx_decrypt(fek, &data[k], &data[k]); + } + } else + err = gcry_cipher_decrypt(fek->gcry_cipher_hd, data, 512, NULL, 0); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Decryption failed: %s\n", gcry_strerror(err)); + return -1; + } + /* Apply the IV. */ + if (fek->alg_id == CALG_AES_256) { + ((le64*)data)[0] ^= cpu_to_le64(0x5816657be9161312ULL + offset); + ((le64*)data)[1] ^= cpu_to_le64(0x1989adbe44918961ULL + offset); + } else { + /* All other algos (Des, 3Des, DesX) use the same IV. */ + ((le64*)data)[0] ^= cpu_to_le64(0x169119629891ad13ULL + offset); + } + return 512; +} + +/** + * ntfs_fek_encrypt_sector + */ +static int ntfs_fek_encrypt_sector(ntfs_fek *fek, u8 *data, const u64 offset) +{ + gcry_error_t err; + + err = gcry_cipher_reset(fek->gcry_cipher_hd); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Failed to reset cipher: %s\n", + gcry_strerror(err)); + return -1; + } + /* + * Note: You may wonder why we are not calling gcry_cipher_setiv() here + * instead of doing it by hand after the decryption. The answer is + * that gcry_cipher_setiv() wants an iv of length 8 bytes but we give + * it a length of 16 for AES256 so it does not like it. + */ + /* Apply the IV. */ + if (fek->alg_id == CALG_AES_256) { + ((le64*)data)[0] ^= cpu_to_le64(0x5816657be9161312ULL + offset); + ((le64*)data)[1] ^= cpu_to_le64(0x1989adbe44918961ULL + offset); + } else { + /* All other algos (Des, 3Des, DesX) use the same IV. */ + ((le64*)data)[0] ^= cpu_to_le64(0x169119629891ad13ULL + offset); + } + if (fek->alg_id == CALG_DESX) { + int k; + + fek->desx_ctx.prev_blk = 0; + for (k=0; (k < 512) && (err == GPG_ERR_NO_ERROR); k+=8) { + err = ntfs_desx_encrypt(fek, &data[k], &data[k]); + } + } else + err = gcry_cipher_encrypt(fek->gcry_cipher_hd, data, 512, NULL, 0); + if (err != GPG_ERR_NO_ERROR) { + ntfs_log_error("Encryption failed: %s\n", gcry_strerror(err)); + return -1; + } + return 512; +} + +/** + * ntfs_cat_decrypt - Decrypt the contents of an encrypted file to stdout. + * @inode: An encrypted file's inode structure, as obtained by + * ntfs_inode_open(). + * @fek: A file encryption key. As obtained by ntfs_inode_fek_get(). + */ +static int ntfs_cat_decrypt(ntfs_inode *inode, ntfs_fek *fek) +{ + int bufsize = 512; + unsigned char *buffer; + ntfs_attr *attr; + s64 bytes_read, written, offset, total; + s64 old_data_size, old_initialized_size; + int i; + + buffer = malloc(bufsize); + if (!buffer) + return 1; + attr = ntfs_attr_open(inode, AT_DATA, NULL, 0); + if (!attr) { + ntfs_log_error("Cannot cat a directory.\n"); + free(buffer); + return 1; + } + total = attr->data_size; + + // hack: make sure attr will not be commited to disk if you use this. + // clear the encrypted bit, otherwise the library won't allow reading. + NAttrClearEncrypted(attr); + // extend the size, we may need to read past the end of the stream. + old_data_size = attr->data_size; + old_initialized_size = attr->initialized_size; + attr->data_size = attr->initialized_size = attr->allocated_size; + + offset = 0; + while (total > 0) { + bytes_read = ntfs_attr_pread(attr, offset, 512, buffer); + if (bytes_read == -1) { + ntfs_log_perror("ERROR: Couldn't read file"); + break; + } + if (!bytes_read) + break; + if ((i = ntfs_fek_decrypt_sector(fek, buffer, offset)) < + bytes_read) { + ntfs_log_perror("ERROR: Couldn't decrypt all data!"); + ntfs_log_error("%u/%lld/%lld/%lld\n", i, + (long long)bytes_read, (long long)offset, + (long long)total); + break; + } + if (bytes_read > total) + bytes_read = total; + written = fwrite(buffer, 1, bytes_read, stdout); + if (written != bytes_read) { + ntfs_log_perror("ERROR: Couldn't output all data!"); + break; + } + offset += bytes_read; + total -= bytes_read; + } + attr->data_size = old_data_size; + attr->initialized_size = old_initialized_size; + NAttrSetEncrypted(attr); + ntfs_attr_close(attr); + free(buffer); + return 0; +} + +/** + * ntfs_feed_encrypt - Encrypt the contents of stdin to an encrypted file + * @inode: An encrypted file's inode structure, as obtained by + * ntfs_inode_open(). + * @fek: A file encryption key. As obtained by ntfs_inode_fek_get(). + */ +static int ntfs_feed_encrypt(ntfs_inode *inode, ntfs_fek *fek) +{ + const int bufsize = 512; + unsigned char *buffer; + ntfs_attr *attr; + s64 bytes_read, written, offset, total; + unsigned char *b; + long val; + int count; + int i; + + buffer = (unsigned char*)malloc(bufsize); + if (!buffer) + return 1; + attr = ntfs_attr_open(inode, AT_DATA, NULL, 0); + if (!attr) { + ntfs_log_error("Cannot feed into a directory.\n"); + goto rejected; + } + total = 0; + + if (!(attr->data_flags & ATTR_IS_ENCRYPTED)) { + ntfs_log_error("The data stream was not encrypted\n"); + goto rejected; + } + inode->vol->efs_raw = TRUE; + + if (ntfs_attr_truncate(attr, 0)) { + ntfs_log_error("Failed to truncate the data stream\n"); + goto rejected; + } + offset = 0; + do { + bytes_read = fread(buffer, 1, bufsize, stdin); + if (bytes_read <= 0) { + if (bytes_read < 0) + ntfs_log_perror("ERROR: Couldn't read data"); + } else { + if (bytes_read < bufsize) { + /* Fill with random data */ + srandom((unsigned int)(sle64_to_cpu( + inode->last_data_change_time) + /100000000)); + count = bufsize - bytes_read; + b = &buffer[bytes_read]; + do { + val = random(); + switch (count) { + default : + *b++ = val; + val >>= 8; + /* FALLTHRU */ + case 3 : + *b++ = val; + val >>= 8; + /* FALLTHRU */ + case 2 : + *b++ = val; + val >>= 8; + /* FALLTHRU */ + case 1 : + *b++ = val; + val >>= 8; + } + count -= 4; + } while (count > 0); + } + if ((i = ntfs_fek_encrypt_sector(fek, buffer, offset)) + < bufsize) { + ntfs_log_perror("ERROR: Couldn't encrypt all data!"); + ntfs_log_error("%u/%lld/%lld/%lld\n", i, + (long long)bytes_read, (long long)offset, + (long long)total); + break; + } + written = ntfs_attr_pwrite(attr, offset, bufsize, buffer); + if (written != bufsize) { + ntfs_log_perror("ERROR: Couldn't output all data!"); + break; + } + offset += bufsize; + total += bytes_read; + } + } while (bytes_read == bufsize); + ntfs_attr_truncate(attr, total); + inode->last_data_change_time = ntfs_current_time(); + NAttrSetEncrypted(attr); + ntfs_attr_close(attr); + free(buffer); + return 0; +rejected : + free(buffer); + return (-1); +} + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char *argv[]) +{ + u8 *pfx_buf; + char *password; + ntfs_rsa_private_key rsa_key; + ntfs_volume *vol; + ntfs_inode *inode; + ntfs_fek *fek; + unsigned pfx_size; + int res; + NTFS_DF_TYPES df_type; + char thumbprint[NTFS_SHA1_THUMBPRINT_SIZE]; + + ntfs_log_set_handler(ntfs_log_handler_stderr); + + res = parse_options(argc, argv); + if (res >= 0) + return (res); + utils_set_locale(); + + /* Initialize crypto in ntfs. */ + if (ntfs_crypto_init()) { + ntfs_log_error("Failed to initialize crypto. Aborting.\n"); + return 1; + } + /* Load the PKCS#12 (.pfx) file containing the user's private key. */ + if (ntfs_pkcs12_load_pfxfile(opts.keyfile, &pfx_buf, &pfx_size)) { + ntfs_log_error("Failed to load key file. Aborting.\n"); + ntfs_crypto_deinit(); + return 1; + } + /* Ask the user for their password. */ + password = getpass("Enter the password with which the private key was " + "encrypted: "); + if (!password) { + ntfs_log_perror("Failed to obtain user password"); + free(pfx_buf); + ntfs_crypto_deinit(); + return 1; + } + /* Obtain the user's private RSA key from the key file. */ + rsa_key = ntfs_pkcs12_extract_rsa_key(pfx_buf, pfx_size, password, + thumbprint, sizeof(thumbprint), &df_type); + /* Destroy the password. */ + memset(password, 0, strlen(password)); + /* No longer need the pfx file contents. */ + free(pfx_buf); + if (!rsa_key) { + ntfs_log_error("Failed to extract the private RSA key.\n"); + ntfs_crypto_deinit(); + return 1; + } + /* Mount the ntfs volume. */ + vol = utils_mount_volume(opts.device, + (opts.encrypt ? 0 : NTFS_MNT_RDONLY) | + (opts.force ? NTFS_MNT_RECOVER : 0)); + if (!vol) { + ntfs_log_error("Failed to mount ntfs volume. Aborting.\n"); + ntfs_rsa_private_key_release(rsa_key); + ntfs_crypto_deinit(); + return 1; + } + /* Open the encrypted ntfs file. */ + if (opts.inode != -1) + inode = ntfs_inode_open(vol, opts.inode); + else + inode = ntfs_pathname_to_inode(vol, NULL, opts.file); + if (!inode) { + ntfs_log_error("Failed to open encrypted file. Aborting.\n"); + ntfs_umount(vol, FALSE); + ntfs_rsa_private_key_release(rsa_key); + ntfs_crypto_deinit(); + return 1; + } + /* Obtain the file encryption key of the encrypted file. */ + fek = ntfs_inode_fek_get(inode, rsa_key, thumbprint, + sizeof(thumbprint), df_type); + ntfs_rsa_private_key_release(rsa_key); + if (fek) { + if (opts.encrypt) + res = ntfs_feed_encrypt(inode, fek); + else + res = ntfs_cat_decrypt(inode, fek); + ntfs_fek_release(fek); + } else { + ntfs_log_error("Failed to obtain file encryption key. " + "Aborting.\n"); + res = 1; + } + ntfs_inode_close(inode); + ntfs_umount(vol, FALSE); + ntfs_crypto_deinit(); + return res; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsdump_logfile.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsdump_logfile.c new file mode 100644 index 0000000..0916de3 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsdump_logfile.c @@ -0,0 +1,781 @@ +/** + * ntfsdump_logfile - Part of the Linux-NTFS project. + * + * Copyright (c) 2000-2005 Anton Altaparmakov + * + * This utility will interpret the contents of the journal ($LogFile) of an + * NTFS partition and display the results on stdout. Errors will be output to + * stderr. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS source + * in the file COPYING); if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +/* TODO: + * - Remove the need for clipping at 64MiB. + * - Add normal command line switchs (use getopt_long()). + * - For a volume: allow dumping only uncommitted records. + * - For a file: get an optional command line parameter for the last SN. + * - Sanity checks. + */ + +#include "config.h" + +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif + +#include "types.h" +#include "endians.h" +#include "volume.h" +#include "inode.h" +#include "attrib.h" +#include "layout.h" +#include "logfile.h" +#include "mst.h" +#include "utils.h" +/* #include "version.h" */ +#include "logging.h" + +typedef struct { + BOOL is_volume; + const char *filename; + s64 data_size; + union { + struct { + ntfs_volume *vol; + ntfs_inode *ni; + ntfs_attr *na; + }; + struct { + int fd; + }; + }; +} logfile_file; + +/** + * logfile_close + */ +static int logfile_close(logfile_file *logfile) +{ + if (logfile->is_volume) { + if (logfile->na) + ntfs_attr_close(logfile->na); + if (logfile->ni && ntfs_inode_close(logfile->ni)) + ntfs_log_perror("Warning: Failed to close $LogFile " + "(inode %i)", FILE_LogFile); + if (ntfs_umount(logfile->vol, 0)) + ntfs_log_perror("Warning: Failed to umount %s", + logfile->filename); + } else { + if (close(logfile->fd)) + ntfs_log_perror("Warning: Failed to close file %s", + logfile->filename); + } + return 0; +} + +/** + * device_err_exit - put an error message, cleanup and exit. + * @vol: volume to unmount. + * @ni: Inode to free. + * @na: Attribute to close. + * + * Use when you wish to exit and collate all the cleanups together. + * if you don't have some parameter to pass, just pass NULL. + */ +__attribute__((noreturn)) +__attribute__((format(printf, 4, 5))) +static void device_err_exit(ntfs_volume *vol, ntfs_inode *ni, + ntfs_attr *na, const char *fmt, ...) +{ + va_list ap; + + if (na) + ntfs_attr_close(na); + if (ni && ntfs_inode_close(ni)) + ntfs_log_perror("Warning: Failed to close $LogFile (inode %i)", + FILE_LogFile); + if (ntfs_umount(vol, 0)) + ntfs_log_perror("Warning: Failed to umount"); + + fprintf(stderr, "ERROR: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + ntfs_log_error("Aborting...\n"); + exit(1); +} + +/** + * log_err_exit - + */ +__attribute__((noreturn)) +__attribute__((format(printf, 2, 3))) +static void log_err_exit(u8 *buf, const char *fmt, ...) +{ + va_list ap; + + free(buf); + + fprintf(stderr, "ERROR: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + ntfs_log_error("Aborting...\n"); + exit(1); +} + +/** + * usage - + */ +__attribute__((noreturn)) +static void usage(const char *exec_name) +{ + ntfs_log_error("%s v%s (libntfs-3g) - Interpret and display information " + "about the journal\n($LogFile) of an NTFS volume.\n" + "Copyright (c) 2000-2005 Anton Altaparmakov.\n" + "%s is free software, released under the GNU General " + "Public License\nand you are welcome to redistribute " + "it under certain conditions.\n%s comes with " + "ABSOLUTELY NO WARRANTY; for details read the GNU\n" + "General Public License to be found in the file " + "COPYING in the main Linux-NTFS\ndistribution " + "directory.\nUsage: %s device\n e.g. %s /dev/hda6\n" + "Alternative usage: %s -f file\n e.g. %s -f " + "MyCopyOfTheLogFile\n", exec_name, VERSION, + exec_name, exec_name, + exec_name, exec_name, exec_name, exec_name); + exit(1); +} + +/** + * logfile_open + */ +static int logfile_open(BOOL is_volume, const char *filename, + logfile_file *logfile) +{ + if (is_volume) { + ntfs_volume *vol; + ntfs_inode *ni; + ntfs_attr *na; + + /* Porting note: NTFS_MNT_FORENSIC is not needed when we mount + * the volume in read-only mode. No changes will be made to the + * logfile or anything else when we are in read only-mode. */ + vol = ntfs_mount(filename, NTFS_MNT_RDONLY); + if (!vol) + log_err_exit(NULL, "Failed to mount %s: %s\n", + filename, strerror(errno)); + ntfs_log_info("Mounted NTFS volume %s (NTFS v%i.%i) on device %s.\n", + vol->vol_name ? vol->vol_name : "", + vol->major_ver, vol->minor_ver, filename); + if (ntfs_version_is_supported(vol)) + device_err_exit(vol, NULL, NULL, + "Unsupported NTFS version.\n"); + ni = ntfs_inode_open(vol, FILE_LogFile); + if (!ni) + device_err_exit(vol, NULL, NULL, "Failed to " + "open $LogFile (inode %i): %s\n", + FILE_LogFile, strerror(errno)); + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) + device_err_exit(vol, ni, NULL, "Failed to open " + "$LogFile/$DATA (attribute 0x%x):" + " %s\n", (unsigned int) + le32_to_cpu(AT_DATA), strerror(errno)); + if (!na->data_size) + device_err_exit(vol, ni, na, "$LogFile has zero " + "length. Run chkdsk /f to correct " + "this.\n"); + logfile->data_size = na->data_size; + logfile->vol = vol; + logfile->ni = ni; + logfile->na = na; + } else { + struct stat sbuf; + int fd; + + fd = open(filename, O_RDONLY); + if (fd == -1) { + if (errno == ENOENT) + log_err_exit(NULL, "The file %s does not " + "exist. Did you specify it " + "correctly?\n", filename); + log_err_exit(NULL, "Failed to open file %s: %s\n", + filename, strerror(errno)); + } + + if (fstat(fd, &sbuf) == -1) { + log_err_exit(NULL, "Error getting information about " + "%s: %s\n", filename, strerror(errno)); + } + + logfile->data_size = sbuf.st_size; + logfile->fd = fd; + } + + logfile->is_volume = is_volume; + logfile->filename = filename; + + return 0; +} + +/** + * logfile_read + */ +static int logfile_pread(logfile_file *logfile, int ofs, int count, u8 *buf) +{ + int br; + + if (logfile->is_volume) { + br = (int)ntfs_attr_pread(logfile->na, ofs, count, buf); + } else { + if (lseek(logfile->fd, ofs, SEEK_SET)==-1) { + ntfs_log_error("Could not seek to offset %u\n", ofs); + return 0; + } + br = read(logfile->fd, buf, count); + } + if (br != count) { + ntfs_log_error("Only %d out of %d bytes read starting at %d\n", + br, count, ofs); + } + return br; +} + +/** + * restart_header_sanity() + */ +static void restart_header_sanity(RESTART_PAGE_HEADER *rstr, u8 *buf) +{ + unsigned int usa_end_ofs, page_size; + + /* Only CHKD records are allowed to have chkdsk_lsn set. */ + if (!ntfs_is_chkd_record(rstr->magic) && + sle64_to_cpu(rstr->chkdsk_lsn)) + log_err_exit(buf, "$LogFile is corrupt: Restart page header " + "magic is not CHKD but a chkdsk LSN is " + "specified. Cannot handle this yet.\n"); + /* Both system and log page size must be >= 512 and a power of 2. */ + page_size = le32_to_cpu(rstr->log_page_size); + if (page_size < 512 || page_size & (page_size - 1)) + log_err_exit(buf, "$LogFile is corrupt: Restart page header " + "specifies invalid log page size. Cannot " + "handle this yet.\n"); + if (page_size != le32_to_cpu(rstr->system_page_size)) { + page_size = le32_to_cpu(rstr->system_page_size); + if (page_size < 512 || page_size & (page_size - 1)) + log_err_exit(buf, "$LogFile is corrupt: Restart page " + "header specifies invalid system page " + "size. Cannot handle this yet.\n"); + } + /* Abort if the version number is not 1.1. */ + if (sle16_to_cpu(rstr->major_ver) != 1 || + sle16_to_cpu(rstr->minor_ver) != 1) + log_err_exit(buf, "Unknown $LogFile version %i.%i. Only know " + "how to handle version 1.1.\n", + sle16_to_cpu(rstr->major_ver), + sle16_to_cpu(rstr->minor_ver)); + /* Verify the location and size of the update sequence array. */ + usa_end_ofs = le16_to_cpu(rstr->usa_ofs) + + le16_to_cpu(rstr->usa_count) * sizeof(u16); + if (page_size / NTFS_BLOCK_SIZE + 1 != le16_to_cpu(rstr->usa_count)) + log_err_exit(buf, "Restart page header in $LogFile is " + "corrupt: Update sequence array size is " + "wrong. Cannot handle this yet.\n"); + if (le16_to_cpu(rstr->usa_ofs) < offsetof(RESTART_PAGE_HEADER, usn)) + log_err_exit(buf, "Restart page header in $LogFile is " + "corrupt: Update sequence array overlaps " + "restart page header. Cannot handle this " + "yet.\n"); + if (usa_end_ofs > NTFS_BLOCK_SIZE - sizeof(u16)) + log_err_exit(buf, "Restart page header in $LogFile is " + "corrupt: Update sequence array overlaps or " + "is behind first protected sequence number. " + "Cannot handle this yet.\n"); + if (usa_end_ofs > le16_to_cpu(rstr->restart_area_offset)) + log_err_exit(buf, "Restart page header in $LogFile is " + "corrupt: Update sequence array overlaps or " + "is behind restart area. Cannot handle this " + "yet.\n"); + /* Finally, verify the offset of the restart area. */ + if (le16_to_cpu(rstr->restart_area_offset) & 7) + log_err_exit(buf, "Restart page header in $LogFile is " + "corrupt: Restart area offset is not aligned " + "to 8-byte boundary. Cannot handle this " + "yet.\n"); +} + +/** + * dump_restart_areas_header + */ +static void dump_restart_areas_header(RESTART_PAGE_HEADER *rstr) +{ + ntfs_log_info("\nRestart page header:\n"); + ntfs_log_info("magic = %s\n", ntfs_is_rstr_record(rstr->magic) ? "RSTR" : + "CHKD"); + ntfs_log_info("usa_ofs = %u (0x%x)\n", le16_to_cpu(rstr->usa_ofs), + le16_to_cpu(rstr->usa_ofs)); + ntfs_log_info("usa_count = %u (0x%x)\n", le16_to_cpu(rstr->usa_count), + le16_to_cpu(rstr->usa_count)); + ntfs_log_info("chkdsk_lsn = %lli (0x%llx)\n", + (long long)sle64_to_cpu(rstr->chkdsk_lsn), + (unsigned long long)sle64_to_cpu(rstr->chkdsk_lsn)); + ntfs_log_info("system_page_size = %u (0x%x)\n", + (unsigned int)le32_to_cpu(rstr->system_page_size), + (unsigned int)le32_to_cpu(rstr->system_page_size)); + ntfs_log_info("log_page_size = %u (0x%x)\n", + (unsigned int)le32_to_cpu(rstr->log_page_size), + (unsigned int)le32_to_cpu(rstr->log_page_size)); + ntfs_log_info("restart_offset = %u (0x%x)\n", + le16_to_cpu(rstr->restart_area_offset), + le16_to_cpu(rstr->restart_area_offset)); +} + +/** + * dump_restart_areas_area + */ +static void dump_restart_areas_area(RESTART_PAGE_HEADER *rstr) +{ + LOG_CLIENT_RECORD *lcr; + RESTART_AREA *ra; + int client; + + ra = (RESTART_AREA*)((u8*)rstr + + le16_to_cpu(rstr->restart_area_offset)); + ntfs_log_info("current_lsn = %lli (0x%llx)\n", + (long long)sle64_to_cpu(ra->current_lsn), + (unsigned long long)sle64_to_cpu(ra->current_lsn)); + ntfs_log_info("log_clients = %u (0x%x)\n", le16_to_cpu(ra->log_clients), + le16_to_cpu(ra->log_clients)); + ntfs_log_info("client_free_list = %i (0x%x)\n", + (s16)le16_to_cpu(ra->client_free_list), + le16_to_cpu(ra->client_free_list)); + ntfs_log_info("client_in_use_list = %i (0x%x)\n", + (s16)le16_to_cpu(ra->client_in_use_list), + le16_to_cpu(ra->client_in_use_list)); + ntfs_log_info("flags = 0x%.4x\n", le16_to_cpu(ra->flags)); + ntfs_log_info("seq_number_bits = %u (0x%x)\n", + (unsigned int)le32_to_cpu(ra->seq_number_bits), + (unsigned int)le32_to_cpu(ra->seq_number_bits)); + ntfs_log_info("restart_area_length = %u (0x%x)\n", + le16_to_cpu(ra->restart_area_length), + le16_to_cpu(ra->restart_area_length)); + ntfs_log_info("client_array_offset = %u (0x%x)\n", + le16_to_cpu(ra->client_array_offset), + le16_to_cpu(ra->client_array_offset)); + ntfs_log_info("file_size = %lli (0x%llx)\n", + (long long)sle64_to_cpu(ra->file_size), + (unsigned long long)sle64_to_cpu(ra->file_size)); + ntfs_log_info("last_lsn_data_length = %u (0x%x)\n", + (unsigned int)le32_to_cpu(ra->last_lsn_data_length), + (unsigned int)le32_to_cpu(ra->last_lsn_data_length)); + ntfs_log_info("log_record_header_length = %u (0x%x)\n", + le16_to_cpu(ra->log_record_header_length), + le16_to_cpu(ra->log_record_header_length)); + ntfs_log_info("log_page_data_offset = %u (0x%x)\n", + le16_to_cpu(ra->log_page_data_offset), + le16_to_cpu(ra->log_page_data_offset)); + ntfs_log_info("restart_log_open_count = %u (0x%x)\n", + (unsigned)le32_to_cpu(ra->restart_log_open_count), + (unsigned)le32_to_cpu(ra->restart_log_open_count)); + lcr = (LOG_CLIENT_RECORD*)((u8*)ra + + le16_to_cpu(ra->client_array_offset)); + for (client = 0; client < le16_to_cpu(ra->log_clients); client++) { + char *client_name; + + ntfs_log_info("\nLog client record number %i:\n", client + 1); + ntfs_log_info("oldest_lsn = %lli (0x%llx)\n", + (long long)sle64_to_cpu(lcr->oldest_lsn), + (unsigned long long) + sle64_to_cpu(lcr->oldest_lsn)); + ntfs_log_info("client_restart_lsn = %lli (0x%llx)\n", (long long) + sle64_to_cpu(lcr->client_restart_lsn), + (unsigned long long) + sle64_to_cpu(lcr->client_restart_lsn)); + ntfs_log_info("prev_client = %i (0x%x)\n", + (s16)le16_to_cpu(lcr->prev_client), + le16_to_cpu(lcr->prev_client)); + ntfs_log_info("next_client = %i (0x%x)\n", + (s16)le16_to_cpu(lcr->next_client), + le16_to_cpu(lcr->next_client)); + ntfs_log_info("seq_number = %u (0x%x)\n", le16_to_cpu(lcr->seq_number), + le16_to_cpu(lcr->seq_number)); + ntfs_log_info("client_name_length = %u (0x%x)\n", + (unsigned int)le32_to_cpu(lcr->client_name_length) / 2, + (unsigned int)le32_to_cpu(lcr->client_name_length) / 2); + if (le32_to_cpu(lcr->client_name_length)) { + client_name = NULL; + if (ntfs_ucstombs(lcr->client_name, + le32_to_cpu(lcr->client_name_length) / + 2, &client_name, 0) < 0) { + ntfs_log_perror("Failed to convert log client name"); + client_name = strdup(""); + } + } else + client_name = strdup(""); + ntfs_log_info("client_name = %s\n", client_name); + free(client_name); + /* + * Log client records are fixed size so we can simply use the + * C increment operator to get to the next one. + */ + lcr++; + } +} + +/** + * dump_restart_areas() + */ +static void *dump_restart_areas(RESTART_PAGE_HEADER *rstr, u8 *buf, + unsigned int page_size) +{ + int pass = 1; + +rstr_pass_loc: + if (ntfs_is_chkd_record(rstr->magic)) + log_err_exit(buf, "The %s restart page header in $LogFile has " + "been modified by chkdsk. Do not know how to " + "handle this yet. Reboot into Windows to fix " + "this.\n", (u8*)rstr == buf ? "first" : + "second"); + if (ntfs_mst_post_read_fixup((NTFS_RECORD*)rstr, page_size) || + ntfs_is_baad_record(rstr->magic)) + log_err_exit(buf, "$LogFile incomplete multi sector transfer " + "detected in restart page header. Cannot " + "handle this yet.\n"); + if (pass == 1) + ntfs_log_info("$LogFile version %i.%i.\n", + sle16_to_cpu(rstr->major_ver), + sle16_to_cpu(rstr->minor_ver)); + else /* if (pass == 2) */ { + RESTART_AREA *ra; + + /* + * rstr is now the second restart page so we declare rstr1 + * as the first restart page as this one has been verified in + * the first pass so we can use all its members safely. + */ + RESTART_PAGE_HEADER *rstr1 = (RESTART_PAGE_HEADER*)buf; + + /* Exclude the usa from the comparison. */ + ra = (RESTART_AREA*)((u8*)rstr1 + + le16_to_cpu(rstr1->restart_area_offset)); + if (!memcmp(rstr1, rstr, le16_to_cpu(rstr1->usa_ofs)) && + !memcmp((u8*)rstr1 + le16_to_cpu( + rstr1->restart_area_offset), (u8*)rstr + + le16_to_cpu(rstr->restart_area_offset), + le16_to_cpu(ra->restart_area_length))) { + puts("\nSkipping analysis of second restart page " + "because it fully matches the first " + "one."); + goto skip_rstr_pass; + } + /* + * The $LogFile versions specified in each of the two restart + * page headers must match. + */ + if (rstr1->major_ver != rstr->major_ver || + rstr1->minor_ver != rstr->minor_ver) + log_err_exit(buf, "Second restart area specifies " + "different $LogFile version to first " + "restart area. Cannot handle this " + "yet.\n"); + } + /* The restart page header is in rstr and it is mst deprotected. */ + ntfs_log_info("\n%s restart page:\n", pass == 1 ? "1st" : "2nd"); + dump_restart_areas_header(rstr); + + ntfs_log_info("\nRestart area:\n"); + dump_restart_areas_area(rstr); + +skip_rstr_pass: + if (pass == 1) { + rstr = (RESTART_PAGE_HEADER*)((u8*)rstr + page_size); + ++pass; + goto rstr_pass_loc; + } + + return rstr; +} + +/** + * dump_log_records() + */ +static void dump_log_record(LOG_RECORD *lr) +{ + unsigned int i; + ntfs_log_info("this lsn = 0x%llx\n", + (unsigned long long)sle64_to_cpu(lr->this_lsn)); + ntfs_log_info("client previous lsn = 0x%llx\n", (unsigned long long) + sle64_to_cpu(lr->client_previous_lsn)); + ntfs_log_info("client undo next lsn = 0x%llx\n", (unsigned long long) + sle64_to_cpu(lr->client_undo_next_lsn)); + ntfs_log_info("client data length = 0x%x\n", + (unsigned int)le32_to_cpu(lr->client_data_length)); + ntfs_log_info("client_id.seq_number = 0x%x\n", + le16_to_cpu(lr->client_id.seq_number)); + ntfs_log_info("client_id.client_index = 0x%x\n", + le16_to_cpu(lr->client_id.client_index)); + ntfs_log_info("record type = 0x%x\n", + (unsigned int)le32_to_cpu(lr->record_type)); + ntfs_log_info("transaction_id = 0x%x\n", + (unsigned int)le32_to_cpu(lr->transaction_id)); + ntfs_log_info("flags = 0x%x:", le16_to_cpu(lr->log_record_flags)); + if (!lr->log_record_flags) + ntfs_log_info(" NONE\n"); + else { + int _b = 0; + + if (lr->log_record_flags & LOG_RECORD_MULTI_PAGE) { + ntfs_log_info(" LOG_RECORD_MULTI_PAGE"); + _b = 1; + } + if (lr->log_record_flags & ~LOG_RECORD_MULTI_PAGE) { + if (_b) + ntfs_log_info(" |"); + ntfs_log_info(" Unknown flags"); + } + ntfs_log_info("\n"); + } + ntfs_log_info("redo_operation = 0x%x\n", le16_to_cpu(lr->redo_operation)); + ntfs_log_info("undo_operation = 0x%x\n", le16_to_cpu(lr->undo_operation)); + ntfs_log_info("redo_offset = 0x%x\n", le16_to_cpu(lr->redo_offset)); + ntfs_log_info("redo_length = 0x%x\n", le16_to_cpu(lr->redo_length)); + ntfs_log_info("undo_offset = 0x%x\n", le16_to_cpu(lr->undo_offset)); + ntfs_log_info("undo_length = 0x%x\n", le16_to_cpu(lr->undo_length)); + ntfs_log_info("target_attribute = 0x%x\n", le16_to_cpu(lr->target_attribute)); + ntfs_log_info("lcns_to_follow = 0x%x\n", le16_to_cpu(lr->lcns_to_follow)); + ntfs_log_info("record_offset = 0x%x\n", le16_to_cpu(lr->record_offset)); + ntfs_log_info("attribute_offset = 0x%x\n", le16_to_cpu(lr->attribute_offset)); + ntfs_log_info("target_vcn = 0x%llx\n", + (unsigned long long)sle64_to_cpu(lr->target_vcn)); + if (le16_to_cpu(lr->lcns_to_follow) > 0) + ntfs_log_info("Array of lcns:\n"); + for (i = 0; i < le16_to_cpu(lr->lcns_to_follow); i++) + ntfs_log_info("lcn_list[%u].lcn = 0x%llx\n", i, + (unsigned long long)sle64_to_cpu(lr->lcn_list[i])); +} + +/** + * dump_log_records() + */ +static void dump_log_records(RECORD_PAGE_HEADER *rcrd, u8 *buf, + int buf_size, unsigned int page_size) +{ + LOG_RECORD *lr; + int pass = 0; + int client; + + /* Reuse pass for log area. */ +rcrd_pass_loc: + rcrd = (RECORD_PAGE_HEADER*)((u8*)rcrd + page_size); + if ((u8*)rcrd + page_size > buf + buf_size) + return; + ntfs_log_info("\nLog record page number %i", pass); + if (!ntfs_is_rcrd_record(rcrd->magic) && + !ntfs_is_chkd_record(rcrd->magic)) { + unsigned int i; + for (i = 0; i < page_size; i++) + if (((u8*)rcrd)[i] != (u8)-1) + break; + if (i < page_size) + puts(" is corrupt (magic is not RCRD or CHKD)."); + else + puts(" is empty."); + pass++; + goto rcrd_pass_loc; + } else + puts(":"); + /* Dump log record page */ + ntfs_log_info("magic = %s\n", ntfs_is_rcrd_record(rcrd->magic) ? "RCRD" : + "CHKD"); +// TODO: I am here... (AIA) + ntfs_log_info("copy.last_lsn/file_offset = 0x%llx\n", (unsigned long long) + sle64_to_cpu(rcrd->copy.last_lsn)); + ntfs_log_info("flags = 0x%x\n", (unsigned int)le32_to_cpu(rcrd->flags)); + ntfs_log_info("page count = %i\n", le16_to_cpu(rcrd->page_count)); + ntfs_log_info("page position = %i\n", le16_to_cpu(rcrd->page_position)); + ntfs_log_info("header.next_record_offset = 0x%llx\n", (unsigned long long) + le16_to_cpu(rcrd->next_record_offset)); + ntfs_log_info("header.last_end_lsn = 0x%llx\n", (unsigned long long) + sle64_to_cpu(rcrd->last_end_lsn)); + /* + * Where does the 0x40 come from? Is it just usa_offset + + * usa_client * 2 + 7 & ~7 or is it derived from somewhere? + */ + lr = (LOG_RECORD*)((u8*)rcrd + 0x40); + client = 0; + do { + ntfs_log_info("\nLog record %i:\n", client); + dump_log_record(lr); + client++; + lr = (LOG_RECORD*)((u8*)lr + 0x70); + } while (((u8*)lr + 0x70 <= (u8*)rcrd + + le16_to_cpu(rcrd->next_record_offset))); + + pass++; + goto rcrd_pass_loc; +} + +/** + * main - + */ +int main(int argc, char **argv) +{ + RESTART_PAGE_HEADER *rstr; + RECORD_PAGE_HEADER *rcrd; + unsigned int page_size; + int buf_size, br, err; + logfile_file logfile; + u8 *buf; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + ntfs_log_info("\n"); + if (argc < 2 || argc > 3) + /* print usage and exit */ + usage(argv[0]); + /* + * If one argument, it is a device containing an NTFS volume which we + * need to mount and read the $LogFile from so we can dump its + * contents. + * + * If two arguments the first one must be "-f" and the second one is + * the path and name of the $LogFile (or copy thereof) which we need to + * read and dump the contents of. + */ + + if (argc == 2) { + logfile_open(TRUE, argv[1], &logfile); + } else /* if (argc == 3) */ { + if (strncmp(argv[1], "-f", strlen("-f"))) + usage(argv[0]); + + logfile_open(FALSE, argv[2], &logfile); + } + + buf_size = 64 * 1024 * 1024; + + if (logfile.data_size <= buf_size) + buf_size = logfile.data_size; + else + ntfs_log_error("Warning: $LogFile is too big. " + "Only analysing the first 64MiB.\n"); + + /* For simplicity we read all of $LogFile/$DATA into memory. */ + buf = malloc(buf_size); + if (!buf) { + ntfs_log_perror("Failed to allocate buffer for file data"); + logfile_close(&logfile); + exit(1); + } + + br = logfile_pread(&logfile, 0, buf_size, buf); + err = errno; + logfile_close(&logfile); + if (br != buf_size) { + log_err_exit(buf, "Failed to read $LogFile/$DATA: %s\n", + br < 0 ? strerror(err) : "Partial read."); + } + + /* + * We now have the entirety of the journal ($LogFile/$DATA or argv[2]) + * in the memory buffer buf and this has a size of buf_size. Note we + * apply a size capping at 64MiB, so if the journal is any bigger we + * only have the first 64MiB. This should not be a problem as I have + * never seen such a large $LogFile. Usually it is only a few MiB in + * size. + */ + rstr = (RESTART_PAGE_HEADER*)buf; + + /* Check for presence of restart area signature. */ + if (!ntfs_is_rstr_record(rstr->magic) && + !ntfs_is_chkd_record(rstr->magic)) { + s8 *pos = (s8*)buf; + s8 *end = pos + buf_size; + while (pos < end && *pos == -1) + pos++; + if (pos != end) + log_err_exit(buf, "$LogFile contents are corrupt " + "(magic RSTR is missing). Cannot " + "handle this yet.\n"); + /* All bytes are -1. */ + free(buf); + puts("$LogFile is not initialized."); + return 0; + } + + /* + * First, verify the restart page header for consistency. + */ + restart_header_sanity(rstr, buf); + page_size = le32_to_cpu(rstr->log_page_size); + + /* + * Second, verify the restart area itself. + */ + // TODO: Implement this. + ntfs_log_error("Warning: Sanity checking of restart area not implemented " + "yet.\n"); + /* + * Third and last, verify the array of log client records. + */ + // TODO: Implement this. + ntfs_log_error("Warning: Sanity checking of array of log client records not " + "implemented yet.\n"); + + /* + * Dump the restart headers & areas. + */ + rcrd = (RECORD_PAGE_HEADER*)dump_restart_areas(rstr, buf, page_size); + ntfs_log_info("\n\nFinished with restart pages. " + "Beginning with log pages.\n"); + + /* + * Dump the log areas. + */ + dump_log_records(rcrd, buf, buf_size, page_size); + + free(buf); + return 0; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfallocate.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfallocate.8.in new file mode 100644 index 0000000..35dc224 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfallocate.8.in @@ -0,0 +1,137 @@ +.\" Copyright (c) 2014 Jean-Pierre Andre +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSFALLOCATE 8 "June 2014" "ntfs-3g @VERSION@" +.SH NAME +ntfsfallocate \- preallocate space to a file on an NTFS volume +.SH SYNOPSIS +\fBntfsfallocate\fR [\fIoptions\fR] -l \fIlength\fR \fIdevice\fR \fIfile\fR \fI[attr-type\fR [\fIattr-name\fR]] +.SH DESCRIPTION +.B ntfsfallocate +preallocates space for any attribute of a file or directory, thus reserving +space before actual contents is written. This is similar to fallocate(1). +.SH OPTIONS +Below is a summary of all the options that +.B ntfsfallocate +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not using a mounted volume. +Use this option with caution. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-l\fR, \fB\-\-length\fR LENGTH +This is a mandatory option to specify the number of bytes to preallocate. +It will be rounded up to a multiple of the cluster size. +A suffix of K, M, G, T, P or E may be appended to +mean a multiplicative factor of a power of 1000. Similarly a suffix of +Ki, Mi, Gi, Ti, Pi or Ei may be appended to mean a multiplicative factor +of a power of 1024. +.TP +\fB\-n\fR, \fB\-\-no-size-change\fR +Do not change the apparent size of the file. The space allocated beyond +the apparent size is not zeroed, but subsequent writing beyond the apparent +end of file will force zeroing the inner allocated space as it cannot be +considered as a hole any more, and this may take significant time. +.TP +\fB\-N\fR, \fB\-\-no-action\fR +Simulate the allocation without actually write to device. +.TP +\fB\-o\fR, \fB\-\-offset\fR OFFSET +Specify the offset in the file where preallocation starts. By default, +the preallocation is counted from the beginning of the file. Space already +allocated in the area defined by offset and length is preserved. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Suppress some debug/warning/error messages. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Display more debug/warning/error messages. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license of +.BR ntfsfallocate . +.TP +\fBattr-type\fR +Define a particular attribute type to be preallocated (advanced use only). +By default, the unnamed $DATA attribute (the contents of a plain file) will +be preallocated. The attribute has to be specified by a number in decimal +or hexadecimal : +.TS +box; +lB lB lB +l l l. +Hex Decimal Name +0x10 16 "$STANDARD_INFORMATION" +0x20 32 "$ATTRIBUTE_LIST" +0x30 48 "$FILE_NAME" +0x40 64 "$OBJECT_ID" +0x50 80 "$SECURITY_DESCRIPTOR" +0x60 96 "$VOLUME_NAME" +0x70 112 "$VOLUME_INFORMATION" +0x80 128 "$DATA" +0x90 144 "$INDEX_ROOT" +0xA0 160 "$INDEX_ALLOCATION" +0xB0 176 "$BITMAP" +0xC0 192 "$REPARSE_POINT" +0xD0 208 "$EA_INFORMATION" +0xE0 224 "$EA" +0xF0 240 "$PROPERTY_SET" +0x100 256 "$LOGGED_UTILITY_STREAM" +.TE +.sp +.TP +\fBattr-name\fR +Define the name of the particular attribute type to be preallocated +(advanced use only). +.SH EXAMPLES +Preallocate 100MB to the file database.db located in the Data directory +which is at the root of an NTFS file system. +.RS +.sp +.B ntfsfallocate -l 100M /dev/sda1 Data/database.db +.sp +.RE +.SH BUGS +There are no known problems with +.BR ntfsfallocate , +however it can lead to configurations not supported by Windows +and Windows may crash (BSOD) when writing to preallocated clusters +which were not written to earlier. Files with preallocated space should +be fully be written to before they are updated by Windows. +.P +If you find a bug in \fBntfsfallocate\fR proper, please send an email +describing the problem to the development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfsfallocate +was written by Jean-Pierre Andre. +.SH AVAILABILITY +.B ntfsfallocate +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfs-3g (8), +.BR ntfstruncate (8), +.BR ntfsprogs (8), +.BR fallocate (1) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfallocate.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfallocate.c new file mode 100644 index 0000000..d857922 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfallocate.c @@ -0,0 +1,905 @@ +/** + * ntfsfallocate + * + * Copyright (c) 2013-2020 Jean-Pierre Andre + * + * This utility will allocate clusters to a specified attribute belonging + * to a specified file or directory, to a specified length. + * + * WARNING : this can lead to configurations not supported by Windows + * and Windows may crash (BSOD) when writing to preallocated clusters + * which were not written to. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS source + * in the file COPYING); if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#else +extern char *optarg; +extern int optind; +#endif +#ifdef HAVE_LIMITS_H +#include +#endif + +#ifndef LLONG_MAX +#define LLONG_MAX 9223372036854775807LL +#endif + +#include "types.h" +#include "attrib.h" +#include "inode.h" +#include "layout.h" +#include "volume.h" +#include "logging.h" +#include "runlist.h" +#include "dir.h" +#include "bitmap.h" +#include "lcnalloc.h" +#include "utils.h" +#include "misc.h" + +const char *EXEC_NAME = "ntfsfallocate"; + +char *dev_name; +const char *file_name; +le32 attr_type; +ntfschar *attr_name = NULL; +u32 attr_name_len; +s64 opt_alloc_offs; +s64 opt_alloc_len; + +ATTR_DEF *attr_defs; + +static struct { + /* -h, print usage and exit. */ + int no_action; /* do not write to device, only display + what would be done. */ + int no_size_change; /* -n, do not change the apparent size */ + int quiet; /* -q, quiet execution. */ + int verbose; /* -v, verbose execution, given twice, really + verbose execution (debug mode). */ + int force; /* -f, force allocation. */ + /* -V, print version and exit. */ +} opts; + +static const struct option lopt[] = { + { "offset", required_argument, NULL, 'o' }, + { "length", required_argument, NULL, 'l' }, + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "no-action", no_argument, NULL, 'N' }, + { "no-size-change", no_argument, NULL, 'n' }, + { "quiet", no_argument, NULL, 'q' }, + { "version", no_argument, NULL, 'V' }, + { "verbose", no_argument, NULL, 'v' }, + { NULL, 0, NULL, 0 } +}; + +/** + * err_exit - error output and terminate; ignores quiet (-q) + * + * DO NOT USE when allocations are not in initial state + */ +__attribute__((noreturn)) +__attribute__((format(printf, 2, 3))) +static void err_exit(ntfs_volume *vol, const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "ERROR: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "Aborting...\n"); + if (vol && ntfs_umount(vol, 0)) + fprintf(stderr, "Warning: Could not umount %s\n", dev_name); + exit(1); +} + +/** + * copyright - print copyright statements + */ +static void copyright(void) +{ + fprintf(stderr, "Copyright (c) 2013-2014 Jean-Pierre Andre\n" + "Allocate clusters to a specified attribute of " + "a specified file.\n"); +} + +/** + * license - print license statement + */ +static void license(void) +{ + fprintf(stderr, "%s", ntfs_gpl); +} + +/** + * usage - print a list of the parameters to the program + */ +__attribute__((noreturn)) +static void usage(int ret) +{ + copyright(); + fprintf(stderr, "Usage: %s [options] -l length device file [attr-type " + "[attr-name]]\n" + " If attr-type is not specified, 0x80 (i.e. $DATA) " + "is assumed.\n" + " If attr-name is not specified, an unnamed " + "attribute is assumed.\n" + " -f Force execution despite errors\n" + " -n Do not change the apparent size of file\n" + " -l length Allocate length bytes\n" + " -o offset Start allocating at offset\n" + " -v Verbose execution\n" + " -vv Very verbose execution\n" + " -V Display version information\n" + " -h Display this help\n", EXEC_NAME); + fprintf(stderr, "%s%s", ntfs_bugs, ntfs_home); + exit(ret); +} + +/** + * err_exit - error output, display usage and exit + */ +__attribute__((noreturn)) +__attribute__((format(printf, 1, 2))) +static void err_usage(const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "ERROR: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "\n"); + usage(1); +} + +/* + * Get a value option with a possible multiple suffix + */ + +static s64 option_value(const char *arg) +{ + s64 ll; + char *s; + s64 fact; + int count; + BOOL err; + + err = FALSE; + ll = strtoll(arg, &s, 0); + if ((ll >= LLONG_MAX) && (errno == ERANGE)) + err_exit((ntfs_volume*)NULL, "Too big value : %s\n",arg); + if (*s) { + count = 0; + switch (*s++) { + case 'E' : count++; + /* FALLTHRU */ + case 'P' : count++; + /* FALLTHRU */ + case 'T' : count++; + /* FALLTHRU */ + case 'G' : count++; + /* FALLTHRU */ + case 'M' : count++; + /* FALLTHRU */ + case 'K' : count++; + switch (*s++) { + case 'i' : + fact = 1024; + if (*s++ != 'B') + err = TRUE; + break; + case 'B' : + fact = 1000; + break; + case '\0' : + fact = 1024; + s--; + break; + default : + err = TRUE; + fact = 1; + break; + } + if (*s) + err = TRUE; + break; + default : + err = TRUE; + break; + } + if (err) + err_exit((ntfs_volume*)NULL, + "Invalid suffix in : %s\n",arg); + else + while (count-- > 0) { + if (ll > LLONG_MAX/1024) + err_exit((ntfs_volume*)NULL, + "Too big value : %s\n",arg); + ll *= fact; + } + } + return (ll); +} + + +/** + * parse_options + */ +static void parse_options(int argc, char *argv[]) +{ + long long ll; + char *s, *s2; + int c; + + opt_alloc_len = 0; + opt_alloc_offs = 0; + if (argc && *argv) + EXEC_NAME = *argv; + fprintf(stderr, "%s v%s (libntfs-3g)\n", EXEC_NAME, VERSION); + while ((c = getopt_long(argc, argv, "fh?no:qvVl:", lopt, NULL)) != EOF) { + switch (c) { + case 'f': + opts.force = 1; + break; + case 'n': + opts.no_size_change = 1; + break; + case 'N': /* Not proposed as a short option */ + opts.no_action = 1; + break; + case 'q': + opts.quiet = 1; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + /* Version number already printed */ + license(); + exit(0); + case 'l': + ll = option_value(argv[optind - 1]); + if ((ll <= 0) + || (ll >= LLONG_MAX && errno == ERANGE)) + err_usage("Invalid length : %s\n", + argv[optind - 1]); + opt_alloc_len = ll; + break; + case 'o': + ll = option_value(argv[optind - 1]); + if ((ll < 0) + || (ll >= LLONG_MAX && errno == ERANGE)) + err_usage("Invalid offset : %s\n", + argv[optind - 1]); + opt_alloc_offs = ll; + break; + case 'h': + usage(0); + case '?': + default: + usage(1); + } + } + if (!opt_alloc_len) { + err_usage("Missing allocation length\n"); + } + + ntfs_log_verbose("length = %lli = 0x%llx\n", + (long long)opt_alloc_len, (long long)opt_alloc_len); + ntfs_log_verbose("offset = %lli = 0x%llx\n", + (long long)opt_alloc_offs, (long long)opt_alloc_offs); + + if (optind == argc) + usage(1); + + if (opts.verbose > 1) + ntfs_log_set_levels(NTFS_LOG_LEVEL_DEBUG | NTFS_LOG_LEVEL_TRACE | + NTFS_LOG_LEVEL_VERBOSE | NTFS_LOG_LEVEL_QUIET); + + /* Get the device. */ + dev_name = argv[optind++]; + ntfs_log_verbose("device name = %s\n", dev_name); + + if (optind == argc) + usage(1); + + /* Get the file name. */ + file_name = argv[optind++]; + ntfs_log_verbose("file name = \"%s\"\n", file_name); + + /* Get the attribute type, if specified. */ + if (optind == argc) { + attr_type = AT_DATA; + attr_name = AT_UNNAMED; + attr_name_len = 0; + } else { + unsigned long ul; + + s = argv[optind++]; + ul = strtoul(s, &s2, 0); + if (*s2 || !ul || (ul >= ULONG_MAX && errno == ERANGE)) + err_usage("Invalid attribute type %s: %s\n", s, + strerror(errno)); + attr_type = cpu_to_le32(ul); + + /* Get the attribute name, if specified. */ + if (optind != argc) { + s = argv[optind++]; + /* Convert the string to little endian Unicode. */ + attr_name_len = ntfs_mbstoucs(s, &attr_name); + if ((int)attr_name_len < 0) + err_usage("Invalid attribute name " + "\"%s\": %s\n", + s, strerror(errno)); + + /* Keep hold of the original string. */ + s2 = s; + + s = argv[optind++]; + if (optind != argc) + usage(1); + } else { + attr_name = AT_UNNAMED; + attr_name_len = 0; + } + } + ntfs_log_verbose("attribute type = 0x%lx\n", + (unsigned long)le32_to_cpu(attr_type)); + if (attr_name == AT_UNNAMED) + ntfs_log_verbose("attribute name = \"\" (UNNAMED)\n"); + else + ntfs_log_verbose("attribute name = \"%s\" (length %u " + "Unicode characters)\n", s2, + (unsigned int)attr_name_len); +} + +/* + * Save the initial runlist, to be restored on error + */ + +static runlist_element *ntfs_save_rl(runlist_element *rl) +{ + runlist_element *save; + int n; + + n = 0; + save = (runlist_element*)NULL; + if (rl) { + while (rl[n].length) + n++; + save = (runlist_element*)malloc((n + 1)*sizeof(runlist_element)); + if (save) { + memcpy(save, rl, (n + 1)*sizeof(runlist_element)); + } + } + return (save); +} + +/* + * Free the common part of two runs + */ + +static void free_common(ntfs_volume *vol, runlist_element *brl, s64 blth, + runlist_element *grl, s64 glth) +{ + VCN begin_common; + VCN end_common; + + begin_common = max(grl->vcn, brl->vcn); + end_common = min(grl->vcn + glth, brl->vcn + blth); + if (end_common > begin_common) { + if (ntfs_bitmap_clear_run(vol->lcnbmp_na, + brl->lcn + begin_common - brl->vcn, + end_common - begin_common)) + ntfs_log_error("Failed to free %lld clusters " + "from 0x%llx\n", + (long long)end_common - begin_common, + (long long)(brl->lcn + begin_common + - brl->vcn)); + } +} + +/* + * Restore the cluster allocations to initial state + * + * If a new error occurs, only output a message + */ + +static void ntfs_restore_rl(ntfs_attr *na, runlist_element *oldrl) +{ + runlist_element *brl; /* Pointer to bad runlist */ + runlist_element *grl; /* Pointer to good runlist */ + ntfs_volume *vol; + + vol = na->ni->vol; + /* Examine allocated entries from the bad runlist */ + for (brl=na->rl; brl->length; brl++) { + if (brl->lcn != LCN_HOLE) { +// TODO improve by examining both list in parallel + /* Find the holes in the good runlist which overlap */ + for (grl=oldrl; grl->length + && (grl->vcn<=(brl->vcn+brl->length)); grl++) { + if (grl->lcn == LCN_HOLE) { + free_common(vol, brl, brl->length, grl, + grl->length); + } + } + /* Free allocations beyond the end of good runlist */ + if (grl && !grl->length + && ((brl->vcn + brl->length) > grl->vcn)) { + free_common(vol, brl, brl->length, grl, + brl->vcn + brl->length - grl->vcn); + } + } + } + free(na->rl); + na->rl = oldrl; + if (ntfs_attr_update_mapping_pairs(na, 0)) { + ntfs_log_error("Failed to restore the original runlist\n"); + } +} + +/* + * Zero newly allocated runs up to initialized_size + */ + +static int ntfs_inner_zero(ntfs_attr *na, runlist_element *rl) +{ + ntfs_volume *vol; + char *buf; + runlist_element *zrl; + s64 cofs; + s64 pos; + s64 zeroed; + int err; + + err = 0; + vol = na->ni->vol; + buf = (char*)malloc(vol->cluster_size); + if (buf) { + memset(buf, 0, vol->cluster_size); + zrl = rl; + pos = zrl->vcn << vol->cluster_size_bits; + while (zrl->length + && !err + && (pos < na->initialized_size)) { + for (cofs=0; cofslength && !err; cofs++) { + zeroed = ntfs_pwrite(vol->dev, + (rl->lcn + cofs) + << vol->cluster_size_bits, + vol->cluster_size, buf); + if (zeroed != vol->cluster_size) { + ntfs_log_error("Failed to zero at " + "offset %lld\n", + (long long)pos); + errno = EIO; + err = -1; + } + pos += vol->cluster_size; + } + zrl++; + pos = zrl->vcn << vol->cluster_size_bits; + } + free(buf); + } else { + ntfs_log_error("Failed to allocate memory\n"); + errno = ENOSPC; + err = -1; + } + return (err); +} + +/* + * Merge newly allocated runs into runlist + */ + +static int ntfs_merge_allocation(ntfs_attr *na, runlist_element *rl, + s64 size) +{ + ntfs_volume *vol; + int err; + + err = 0; + vol = na->ni->vol; + /* Newly allocated clusters before initialized size need be zeroed */ + if ((rl->vcn << vol->cluster_size_bits) < na->initialized_size) { + err = ntfs_inner_zero(na, rl); + } + if (!err) { + if (na->data_flags & ATTR_IS_SPARSE) { + na->compressed_size += size; + if (na->compressed_size >= na->allocated_size) { + na->data_flags &= ~ATTR_IS_SPARSE; + if (na->compressed_size > na->allocated_size) { + ntfs_log_error("File size error : " + "apparent %lld, " + "compressed %lld > " + "allocated %lld", + (long long)na->data_size, + (long long)na->compressed_size, + (long long)na->allocated_size); + errno = EIO; + err = -1; + } + } + } + } + if (!err) { + rl = ntfs_runlists_merge(na->rl, rl); + if (!rl) { + ntfs_log_error("Failed to merge the new allocation\n"); + err = -1; + } else { + na->rl = rl; + /* Update the runlist */ + if (ntfs_attr_update_mapping_pairs(na, 0)) { + ntfs_log_error( + "Failed to update the runlist\n"); + err = -1; + } + } + } + return (err); +} + +static int ntfs_inner_allocation(ntfs_attr *na, s64 alloc_offs, s64 alloc_len) +{ + ntfs_volume *vol; + runlist_element *rl; + runlist_element *prl; + runlist_element *rlc; + VCN from_vcn; + VCN end_vcn; + LCN lcn_seek_from; + VCN from_hole; + VCN end_hole; + s64 need; + int err; + BOOL done; + + err = 0; + vol = na->ni->vol; + /* Find holes which overlap the requested allocation */ + from_vcn = alloc_offs >> vol->cluster_size_bits; + end_vcn = (alloc_offs + alloc_len + vol->cluster_size - 1) + >> vol->cluster_size_bits; + do { + done = FALSE; + rl = na->rl; + while (rl->length + && ((rl->lcn >= 0) + || ((rl->vcn + rl->length) <= from_vcn) + || (rl->vcn >= end_vcn))) + rl++; + if (!rl->length) + done = TRUE; + else { + from_hole = max(from_vcn, rl->vcn); + end_hole = min(end_vcn, rl->vcn + rl->length); + need = end_hole - from_hole; + lcn_seek_from = -1; + if (rl->vcn) { + /* Avoid fragmentation when possible */ + prl = rl; + if ((--prl)->lcn >= 0) { + lcn_seek_from = prl->lcn + + from_hole - prl->vcn; + } + } + if (need <= 0) { + ntfs_log_error("Wrong hole size %lld\n", + (long long)need); + errno = EIO; + err = -1; + } else { + rlc = ntfs_cluster_alloc(vol, from_hole, need, + lcn_seek_from, DATA_ZONE); + if (!rlc) + err = -1; + else + err = ntfs_merge_allocation(na, rlc, + need << vol->cluster_size_bits); + } + } + } while (!err && !done); + return (err); +} + +static int ntfs_full_allocation(ntfs_attr *na, ntfs_attr_search_ctx *ctx, + s64 alloc_offs, s64 alloc_len) +{ + ATTR_RECORD *attr; + ntfs_inode *ni; + s64 initialized_size; + s64 data_size; + int err; + + err = 0; + initialized_size = na->initialized_size; + data_size = na->data_size; + + if (na->allocated_size <= alloc_offs) { + /* + * Request is fully beyond what was already allocated : + * only need to expand the attribute + */ + err = ntfs_attr_truncate(na, alloc_offs); + if (!err) + err = ntfs_attr_truncate_solid(na, + alloc_offs + alloc_len); + } else { + /* + * Request overlaps what was already allocated : + * We may have to fill existing holes, and force zeroes + * into clusters which are visible. + */ + if ((alloc_offs + alloc_len) > na->allocated_size) + err = ntfs_attr_truncate(na, alloc_offs + alloc_len); + if (!err) + err = ntfs_inner_allocation(na, alloc_offs, alloc_len); + } + /* Set the sizes, even after an error, to keep consistency */ + na->initialized_size = initialized_size; + /* Restore the original apparent size if requested or error */ + if (err || opts.no_size_change + || ((alloc_offs + alloc_len) < data_size)) + na->data_size = data_size; + else { + /* + * "man 1 fallocate" does not define the new apparent size + * when size change is allowed (no --keep-size). + * Assuming the same as no FALLOC_FL_KEEP_SIZE in fallocate(2) : + * "the file size will be changed if offset + len is greater + * than the file size" +// TODO check the behavior of another file system + */ + na->data_size = alloc_offs + alloc_len; + } + + if (!err) { + /* Find the attribute, which may have been relocated for allocations */ + if (ntfs_attr_lookup(attr_type, attr_name, attr_name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + err = -1; + ntfs_log_error("Failed to locate the attribute\n"); + } else { + /* Feed the sizes into the attribute */ + attr = ctx->attr; + attr->data_size = cpu_to_sle64(na->data_size); + attr->initialized_size + = cpu_to_sle64(na->initialized_size); + attr->allocated_size + = cpu_to_sle64(na->allocated_size); + if (na->data_flags & ATTR_IS_SPARSE) + attr->compressed_size + = cpu_to_sle64(na->compressed_size); + /* Copy the unnamed data attribute sizes to inode */ + if ((attr_type == AT_DATA) && !attr_name_len) { + ni = na->ni; + ni->data_size = na->data_size; + if (na->data_flags & ATTR_IS_SPARSE) { + ni->allocated_size + = na->compressed_size; + ni->flags |= FILE_ATTR_SPARSE_FILE; + } else + ni->allocated_size + = na->allocated_size; + } + } + } + return (err); +} + + +/* + * Do the actual allocations + */ + +static int ntfs_fallocate(ntfs_inode *ni, s64 alloc_offs, s64 alloc_len) +{ + s64 allocated_size; + s64 data_size; + ntfs_attr_search_ctx *ctx; + ntfs_attr *na; + runlist_element *oldrl; + const char *errmess; + int save_errno; + int err; + + err = 0; + /* Open the specified attribute. */ + na = ntfs_attr_open(ni, attr_type, attr_name, attr_name_len); + if (!na) { + ntfs_log_perror("Failed to open attribute 0x%lx: ", + (unsigned long)le32_to_cpu(attr_type)); + err = -1; + } else { + errmess = (const char*)NULL; + if (na->data_flags & ATTR_IS_COMPRESSED) { + errmess= "Cannot fallocate a compressed file"; + } + + /* Locate the attribute record, needed for updating sizes */ + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) { + errmess = "Failed to allocate a search context"; + } + if (errmess) { + ntfs_log_error("%s\n",errmess); + err = -1; + } else { + /* Get and save the initial allocations */ + allocated_size = na->allocated_size; + data_size = ni->data_size; + if (na->rl) + err = ntfs_attr_map_whole_runlist(na); + if (!err) { + if (na->rl) + oldrl = ntfs_save_rl(na->rl); + else + oldrl = (runlist_element*)NULL; + if (!na->rl || oldrl) { + err = ntfs_full_allocation(na, ctx, + alloc_offs, alloc_len); + if (err) { + save_errno = errno; + ni->allocated_size + = allocated_size; + ni->data_size = data_size; + ntfs_restore_rl(na, oldrl); + errno = save_errno; + } else { + free(oldrl); + /* Mark file name dirty, to update the sizes in directories */ + NInoFileNameSetDirty(ni); + NInoSetDirty(ni); + } + } else + err = -1; + } + ntfs_attr_put_search_ctx(ctx); + } + /* Close the attribute. */ + ntfs_attr_close(na); + } + return (err); +} + +/** + * main + */ +int main(int argc, char **argv) +{ + unsigned long mnt_flags, ul; + int err; + ntfs_inode *ni; + ntfs_volume *vol; +#ifdef HAVE_WINDOWS_H + char *unix_name; +#endif + + vol = (ntfs_volume*)NULL; + ntfs_log_set_handler(ntfs_log_handler_outerr); + + /* Initialize opts to zero / required values. */ + memset(&opts, 0, sizeof(opts)); + + /* Parse command line options. */ + parse_options(argc, argv); + + utils_set_locale(); + + /* Make sure the file system is not mounted. */ + if (ntfs_check_if_mounted(dev_name, &mnt_flags)) + ntfs_log_perror("Failed to determine whether %s is mounted", + dev_name); + else if (mnt_flags & NTFS_MF_MOUNTED) { + ntfs_log_error("%s is mounted.\n", dev_name); + if (!opts.force) + err_exit((ntfs_volume*)NULL, "Refusing to run!\n"); + fprintf(stderr, "ntfsfallocate forced anyway. Hope /etc/mtab " + "is incorrect.\n"); + } + + /* Mount the device. */ + if (opts.no_action) { + ntfs_log_quiet("Running in READ-ONLY mode!\n"); + ul = NTFS_MNT_RDONLY; + } else + if (opts.force) + ul = NTFS_MNT_RECOVER; + else + ul = 0; + vol = ntfs_mount(dev_name, ul); + if (!vol) + err_exit(vol, "Failed to mount %s: %s\n", dev_name, + strerror(errno)); + + if ((vol->flags & VOLUME_IS_DIRTY) && !opts.force) + err_exit(vol, "Volume is dirty, please run chkdsk.\n"); + + if (ntfs_volume_get_free_space(vol)) + err_exit(vol, "Failed to get free clusters %s: %s\n", + dev_name, strerror(errno)); + + /* Open the specified inode. */ +#ifdef HAVE_WINDOWS_H + unix_name = ntfs_utils_unix_path(file_name); + if (unix_name) { + ni = ntfs_pathname_to_inode(vol, NULL, unix_name); + free(unix_name); + } else + ni = (ntfs_inode*)NULL; +#else + ni = ntfs_pathname_to_inode(vol, NULL, file_name); +#endif + if (!ni) + err_exit(vol, "Failed to open file \"%s\": %s\n", file_name, + strerror(errno)); + if (!opts.no_action) + err = ntfs_fallocate(ni, opt_alloc_offs, opt_alloc_len); + + /* Close the inode. */ + if (ntfs_inode_close(ni)) { + err = -1; + err_exit(vol, "Failed to close inode \"%s\" : %s\n", file_name, + strerror(errno)); + } + + /* Unmount the volume. */ + err = ntfs_umount(vol, 0); + vol = (ntfs_volume*)NULL; + if (err) + ntfs_log_perror("Warning: Failed to umount %s", dev_name); + + /* Free the attribute name if it exists. */ + if (attr_name_len) + ntfs_ucsfree(attr_name); + + ntfs_log_quiet("ntfsfallocate completed successfully. Have a nice day.\n"); + return 0; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfix.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfix.8.in new file mode 100644 index 0000000..a9657c3 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfix.8.in @@ -0,0 +1,82 @@ +.\" Copyright (c) 2005-2006 Szabolcs Szakacsits. +.\" Copyright (c) 2005 Richard Russon. +.\" Copyright (c) 2011-2013 Jean-Pierre Andre +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSFIX 8 "January 2013" "ntfs-3g @VERSION@" +.SH NAME +ntfsfix \- fix common errors and force Windows to check NTFS +.SH SYNOPSIS +.B ntfsfix +[\fIoptions\fR] \fIdevice\fR +.SH DESCRIPTION +.B ntfsfix +is a utility that fixes some common NTFS problems. +.B ntfsfix +is +.B NOT +a Linux version of chkdsk. It only repairs some fundamental NTFS +inconsistencies, resets the NTFS journal file and schedules an NTFS consistency +check for the first boot into Windows. +.sp +You may run +.B ntfsfix +on an NTFS volume if you think it was damaged by Windows or some other way +and it cannot be mounted. +.SH OPTIONS +Below is a summary of all the options that +.B ntfsfix +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-b\fR, \fB\-\-clear\-bad\-sectors\fR +Clear the list of bad sectors. This is useful after cloning an old disk +with bad sectors to a new disk. +.TP +\fB\-d\fR, \fB\-\-clear\-dirty\fR +Clear the volume dirty flag if the volume can be fixed and mounted. +If the option is not present or the volume cannot be fixed, the dirty +volume flag is set to request a volume checking at next mount. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-n\fR, \fB\-\-no\-action\fR +Do not write anything, just show what would have been done. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license +.SH BUGS +There are no known problems with +.BR ntfsfix . +If you find a bug please send an email describing the problem to the +development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfsfix +was written by Anton Altaparmakov, with contributions from Szabolcs Szakacsits. +It was ported to ntfs-3g by Erik Larsson and Jean-Pierre Andre. +.SH AVAILABILITY +.B ntfsfix +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR mkntfs (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfix.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfix.c new file mode 100644 index 0000000..b214068 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsfix.c @@ -0,0 +1,1715 @@ +/** + * ntfsfix - Part of the Linux-NTFS project. + * + * Copyright (c) 2000-2006 Anton Altaparmakov + * Copyright (c) 2002-2006 Szabolcs Szakacsits + * Copyright (c) 2007 Yura Pakhuchiy + * Copyright (c) 2011-2015 Jean-Pierre Andre + * + * This utility fixes some common NTFS problems, resets the NTFS journal file + * and schedules an NTFS consistency check for the first boot into Windows. + * + * Anton Altaparmakov + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS source + * in the file COPYING); if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * WARNING: This program might not work on architectures which do not allow + * unaligned access. For those, the program would need to start using + * get/put_unaligned macros (#include ), but not doing it yet, + * since NTFS really mostly applies to ia32 only, which does allow unaligned + * accesses. We might not actually have a problem though, since the structs are + * defined as being packed so that might be enough for gcc to insert the + * correct code. + * + * If anyone using a non-little endian and/or an aligned access only CPU tries + * this program please let me know whether it works or not! + * + * Anton Altaparmakov + */ + +#include "config.h" + +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif + +#include "types.h" +#include "attrib.h" +#include "volume.h" +#include "bootsect.h" +#include "mft.h" +#include "device.h" +#include "logfile.h" +#include "runlist.h" +#include "mst.h" +#include "utils.h" +/* #include "version.h" */ +#include "logging.h" +#include "misc.h" + +#ifdef NO_NTFS_DEVICE_DEFAULT_IO_OPS +# error "No default device io operations! Cannot build ntfsfix. \ +You need to run ./configure without the --disable-default-device-io-ops \ +switch if you want to be able to build the NTFS utilities." +#endif + +static const char *EXEC_NAME = "ntfsfix"; +static const char OK[] = "OK\n"; +static const char FAILED[] = "FAILED\n"; +static const char FOUND[] = "FOUND\n"; + +#define DEFAULT_SECTOR_SIZE 512 + +static struct { + char *volume; + BOOL no_action; + BOOL clear_bad_sectors; + BOOL clear_dirty; +} opt; + +/* + * Definitions for fixing the self-located MFT bug + */ + +#define SELFLOC_LIMIT 16 + +struct MFT_SELF_LOCATED { + ntfs_volume *vol; + MFT_RECORD *mft0; + MFT_RECORD *mft1; + MFT_RECORD *mft2; + ATTR_LIST_ENTRY *attrlist; + ATTR_LIST_ENTRY *attrlist_to_ref1; + MFT_REF mft_ref0; + MFT_REF mft_ref1; + LCN attrlist_lcn; + BOOL attrlist_resident; +} ; + +/** + * usage + */ +__attribute__((noreturn)) +static void usage(int ret) +{ + ntfs_log_info("%s v%s (libntfs-3g)\n" + "\n" + "Usage: %s [options] device\n" + " Attempt to fix an NTFS partition.\n" + "\n" + " -b, --clear-bad-sectors Clear the bad sector list\n" + " -d, --clear-dirty Clear the volume dirty flag\n" + " -h, --help Display this help\n" + " -n, --no-action Do not write anything\n" + " -V, --version Display version information\n" + "\n" + "For example: %s /dev/hda6\n\n", + EXEC_NAME, VERSION, EXEC_NAME, + EXEC_NAME); + ntfs_log_info("%s%s", ntfs_bugs, ntfs_home); + exit(ret); +} + +/** + * version + */ +__attribute__((noreturn)) +static void version(void) +{ + ntfs_log_info("%s v%s\n\n" + "Attempt to fix an NTFS partition.\n\n" + "Copyright (c) 2000-2006 Anton Altaparmakov\n" + "Copyright (c) 2002-2006 Szabolcs Szakacsits\n" + "Copyright (c) 2007 Yura Pakhuchiy\n" + "Copyright (c) 2011-2015 Jean-Pierre Andre\n\n", + EXEC_NAME, VERSION); + ntfs_log_info("%s\n%s%s", ntfs_gpl, ntfs_bugs, ntfs_home); + exit(0); +} + +/** + * parse_options + */ +static void parse_options(int argc, char **argv) +{ + int c; + static const char *sopt = "-bdhnV"; + static const struct option lopt[] = { + { "help", no_argument, NULL, 'h' }, + { "no-action", no_argument, NULL, 'n' }, + { "clear-bad-sectors", no_argument, NULL, 'b' }, + { "clear-dirty", no_argument, NULL, 'd' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + + memset(&opt, 0, sizeof(opt)); + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opt.volume) + opt.volume = argv[optind - 1]; + else { + ntfs_log_info("ERROR: Too many arguments.\n"); + usage(1); + } + break; + case 'b': + opt.clear_bad_sectors = TRUE; + break; + case 'd': + opt.clear_dirty = TRUE; + break; + case 'n': + opt.no_action = TRUE; + break; + case 'h': + usage(0); + case '?': + usage(1); + /* fall through */ + case 'V': + version(); + default: + ntfs_log_info("ERROR: Unknown option '%s'.\n", argv[optind - 1]); + usage(1); + } + } + + if (opt.volume == NULL) { + ntfs_log_info("ERROR: You must specify a device.\n"); + usage(1); + } +} + +/** + * OLD_ntfs_volume_set_flags + */ +static int OLD_ntfs_volume_set_flags(ntfs_volume *vol, const le16 flags) +{ + MFT_RECORD *m = NULL; + ATTR_RECORD *a; + VOLUME_INFORMATION *c; + ntfs_attr_search_ctx *ctx; + int ret = -1; /* failure */ + + if (!vol) { + errno = EINVAL; + return -1; + } + if (ntfs_file_record_read(vol, FILE_Volume, &m, NULL)) { + ntfs_log_perror("Failed to read $Volume"); + return -1; + } + /* Sanity check */ + if (!(m->flags & MFT_RECORD_IN_USE)) { + ntfs_log_error("$Volume has been deleted. Cannot handle this " + "yet. Run chkdsk to fix this.\n"); + errno = EIO; + goto err_exit; + } + /* Get a pointer to the volume information attribute. */ + ctx = ntfs_attr_get_search_ctx(NULL, m); + if (!ctx) { + ntfs_log_debug("Failed to allocate attribute search " + "context.\n"); + goto err_exit; + } + if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, AT_UNNAMED, 0, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + ntfs_log_error("Attribute $VOLUME_INFORMATION was not found in " + "$Volume!\n"); + goto err_out; + } + a = ctx->attr; + /* Sanity check. */ + if (a->non_resident) { + ntfs_log_error("Attribute $VOLUME_INFORMATION must be resident " + "(and it isn't)!\n"); + errno = EIO; + goto err_out; + } + /* Get a pointer to the value of the attribute. */ + c = (VOLUME_INFORMATION*)(le16_to_cpu(a->value_offset) + (char*)a); + /* Sanity checks. */ + if ((char*)c + le32_to_cpu(a->value_length) > + (char*)m + le32_to_cpu(m->bytes_in_use) || + le16_to_cpu(a->value_offset) + + le32_to_cpu(a->value_length) > le32_to_cpu(a->length)) { + ntfs_log_error("Attribute $VOLUME_INFORMATION in $Volume is " + "corrupt!\n"); + errno = EIO; + goto err_out; + } + /* Set the volume flags. */ + vol->flags = c->flags = flags; + if (ntfs_mft_record_write(vol, FILE_Volume, m)) { + ntfs_log_perror("Error writing $Volume"); + goto err_out; + } + ret = 0; /* success */ +err_out: + ntfs_attr_put_search_ctx(ctx); +err_exit: + free(m); + return ret; +} + +/** + * set_dirty_flag + */ +static int set_dirty_flag(ntfs_volume *vol) +{ + le16 flags; + + /* Porting note: We test for the current state of VOLUME_IS_DIRTY. This + * should actually be more appropriate than testing for NVolWasDirty. */ + if (vol->flags & VOLUME_IS_DIRTY) + return 0; + ntfs_log_info("Setting required flags on partition... "); + /* + * Set chkdsk flag, i.e. mark the partition dirty so chkdsk will run + * and fix it for us. + */ + flags = vol->flags | VOLUME_IS_DIRTY; + if (!opt.no_action && OLD_ntfs_volume_set_flags(vol, flags)) { + ntfs_log_info(FAILED); + ntfs_log_error("Error setting volume flags.\n"); + return -1; + } + vol->flags = flags; + + /* Porting note: libntfs-3g does not have the 'WasDirty' flag/property, + * and never touches the 'dirty' bit except when explicitly told to do + * so. Since we just wrote the VOLUME_IS_DIRTY bit to disk, and + * vol->flags is up-to-date, we can just ignore the NVolSetWasDirty + * statement. */ + /* NVolSetWasDirty(vol); */ + + ntfs_log_info(OK); + return 0; +} + +/** + * empty_journal + */ +static int empty_journal(ntfs_volume *vol) +{ + if (NVolLogFileEmpty(vol)) + return 0; + ntfs_log_info("Going to empty the journal ($LogFile)... "); + if (ntfs_logfile_reset(vol)) { + ntfs_log_info(FAILED); + ntfs_log_perror("Failed to reset $LogFile"); + return -1; + } + ntfs_log_info(OK); + return 0; +} + +/* + * Clear the sparse flag of an attribute + */ + +static int clear_sparse(ntfs_attr *na, const char *name) +{ + ntfs_attr_search_ctx *ctx; + int res; + + res = -1; + ctx = ntfs_attr_get_search_ctx(na->ni, NULL); + if (ctx) { + if (!ntfs_attr_lookup(na->type, na->name, na->name_len, + CASE_SENSITIVE, 0, NULL, 0, ctx)) { + na->data_flags &= ~ATTR_IS_SPARSE; + ctx->attr->data_size = cpu_to_sle64(na->data_size); + ctx->attr->initialized_size + = cpu_to_sle64(na->initialized_size); + ctx->attr->flags = na->data_flags; + ctx->attr->compression_unit = 0; + ntfs_inode_mark_dirty(ctx->ntfs_ino); + NInoFileNameSetDirty(na->ni); + res = 0; + } else + ntfs_log_perror("Could not locate attribute for %s", + name); + ntfs_attr_put_search_ctx(ctx); + } else + ntfs_log_perror("Could not get a search context for %s", + name); + return (res); +} + +/** + * Clear the bad cluster marks (option) + */ +static int clear_badclus(ntfs_volume *vol) +{ + static ntfschar badstream[] = { + const_cpu_to_le16('$'), const_cpu_to_le16('B'), + const_cpu_to_le16('a'), const_cpu_to_le16('d') + } ; + ntfs_inode *ni; + ntfs_attr *na; + BOOL ok; + + ok = FALSE; + ntfs_log_info("Going to un-mark the bad clusters ($BadClus)... "); + ni = ntfs_inode_open(vol, FILE_BadClus); + if (ni) { + na = ntfs_attr_open(ni, AT_DATA, badstream, 4); + /* + * chkdsk does not adjust the data size when + * moving clusters to $BadClus, so we have to + * check the runlist. + */ + if (na && !ntfs_attr_map_whole_runlist(na)) { + if (na->rl + && na->rl[0].length && na->rl[1].length) { + /* + * Truncate the stream to free all its clusters, + * (which requires setting the data size according + * to allocation), then reallocate a sparse stream + * to full size of volume and reset the data size. + * Note : the sparse flags should not be set. + */ + na->data_size = na->allocated_size; + na->initialized_size = na->allocated_size; + if (!ntfs_attr_truncate(na,0) + && !ntfs_attr_truncate(na,vol->nr_clusters + << vol->cluster_size_bits)) { + na->initialized_size = 0; + NInoFileNameSetDirty(ni); + ok = TRUE; + } else { + ntfs_log_perror("Failed to un-mark the bad clusters"); + } + } else { + ntfs_log_info("No bad clusters..."); + ok = TRUE; + } + /* + * The sparse flags are not set after an initial + * formatting, so do the same. + */ + if (ok) { + ni->flags &= ~FILE_ATTR_SPARSE_FILE; + ok = !clear_sparse(na, "$BadClus::$Bad"); + } + ntfs_attr_close(na); + } else { + ntfs_log_perror("Failed to open $BadClus::$Bad"); + } + ntfs_inode_close(ni); + } else { + ntfs_log_perror("Failed to open inode FILE_BadClus"); + } + if (ok) + ntfs_log_info(OK); + return (ok ? 0 : -1); +} + +/** + * fix_mftmirr + */ +static int fix_mftmirr(ntfs_volume *vol) +{ + s64 l, br; + unsigned char *m, *m2; + int i, ret = -1; /* failure */ + BOOL done; + + ntfs_log_info("\nProcessing $MFT and $MFTMirr...\n"); + + /* Load data from $MFT and $MFTMirr and compare the contents. */ + m = (u8*)malloc(vol->mftmirr_size << vol->mft_record_size_bits); + if (!m) { + ntfs_log_perror("Failed to allocate memory"); + return -1; + } + m2 = (u8*)malloc(vol->mftmirr_size << vol->mft_record_size_bits); + if (!m2) { + ntfs_log_perror("Failed to allocate memory"); + free(m); + return -1; + } + + ntfs_log_info("Reading $MFT... "); + l = ntfs_attr_mst_pread(vol->mft_na, 0, vol->mftmirr_size, + vol->mft_record_size, m); + if (l != vol->mftmirr_size) { + ntfs_log_info(FAILED); + if (l != -1) + errno = EIO; + ntfs_log_perror("Failed to read $MFT"); + goto error_exit; + } + ntfs_log_info(OK); + + ntfs_log_info("Reading $MFTMirr... "); + l = ntfs_attr_mst_pread(vol->mftmirr_na, 0, vol->mftmirr_size, + vol->mft_record_size, m2); + if (l != vol->mftmirr_size) { + ntfs_log_info(FAILED); + if (l != -1) + errno = EIO; + ntfs_log_perror("Failed to read $MFTMirr"); + goto error_exit; + } + ntfs_log_info(OK); + + /* + * FIXME: Need to actually check the $MFTMirr for being real. Otherwise + * we might corrupt the partition if someone is experimenting with + * software RAID and the $MFTMirr is not actually in the position we + * expect it to be... )-: + * FIXME: We should emit a warning it $MFTMirr is damaged and ask + * user whether to recreate it from $MFT or whether to abort. - The + * warning needs to include the danger of software RAID arrays. + * Maybe we should go as far as to detect whether we are running on a + * MD disk and if yes then bomb out right at the start of the program? + */ + + ntfs_log_info("Comparing $MFTMirr to $MFT... "); + done = FALSE; + /* + * Since 2017, Windows 10 does not mirror to full $MFTMirr when + * using big clusters, and some records may be found different. + * Nevertheless chkdsk.exe mirrors it fully, so we do similarly. + */ + for (i = 0; i < vol->mftmirr_size; ++i) { + MFT_RECORD *mrec, *mrec2; + const char *ESTR[12] = { "$MFT", "$MFTMirr", "$LogFile", + "$Volume", "$AttrDef", "root directory", "$Bitmap", + "$Boot", "$BadClus", "$Secure", "$UpCase", "$Extend" }; + const char *s; + BOOL use_mirr; + + if (i < 12) + s = ESTR[i]; + else if (i < 16) + s = "system file"; + else + s = "mft record"; + + use_mirr = FALSE; + mrec = (MFT_RECORD*)(m + i * vol->mft_record_size); + if (mrec->flags & MFT_RECORD_IN_USE) { + if (ntfs_is_baad_record(mrec->magic)) { + ntfs_log_info(FAILED); + ntfs_log_error("$MFT error: Incomplete multi " + "sector transfer detected in " + "%s.\nCannot handle this yet. " + ")-:\n", s); + goto error_exit; + } + if (!ntfs_is_mft_record(mrec->magic)) { + ntfs_log_info(FAILED); + ntfs_log_error("$MFT error: Invalid mft " + "record for %s.\nCannot " + "handle this yet. )-:\n", s); + goto error_exit; + } + } + mrec2 = (MFT_RECORD*)(m2 + i * vol->mft_record_size); + if (mrec2->flags & MFT_RECORD_IN_USE) { + if (ntfs_is_baad_record(mrec2->magic)) { + ntfs_log_info(FAILED); + ntfs_log_error("$MFTMirr error: Incomplete " + "multi sector transfer " + "detected in %s.\n", s); + goto error_exit; + } + if (!ntfs_is_mft_record(mrec2->magic)) { + ntfs_log_info(FAILED); + ntfs_log_error("$MFTMirr error: Invalid mft " + "record for %s.\n", s); + goto error_exit; + } + /* $MFT is corrupt but $MFTMirr is ok, use $MFTMirr. */ + if (!(mrec->flags & MFT_RECORD_IN_USE) && + !ntfs_is_mft_record(mrec->magic)) + use_mirr = TRUE; + } + if (memcmp(mrec, mrec2, ntfs_mft_record_get_data_size(mrec))) { + if (!done) { + done = TRUE; + ntfs_log_info(FAILED); + } + ntfs_log_info("Correcting differences in $MFT%s " + "record %d...", use_mirr ? "" : "Mirr", + i); + br = ntfs_mft_record_write(vol, i, + use_mirr ? mrec2 : mrec); + if (br) { + ntfs_log_info(FAILED); + ntfs_log_perror("Error correcting $MFT%s", + use_mirr ? "" : "Mirr"); + goto error_exit; + } + ntfs_log_info(OK); + } + } + if (!done) + ntfs_log_info(OK); + ntfs_log_info("Processing of $MFT and $MFTMirr completed " + "successfully.\n"); + ret = 0; +error_exit: + free(m); + free(m2); + return ret; +} + +/* + * Rewrite the $UpCase file as default + * + * Returns 0 if could be written + */ + +static int rewrite_upcase(ntfs_volume *vol, ntfs_attr *na) +{ + s64 l; + int res; + + /* writing the $UpCase may require bitmap updates */ + res = -1; + vol->lcnbmp_ni = ntfs_inode_open(vol, FILE_Bitmap); + if (!vol->lcnbmp_ni) { + ntfs_log_perror("Failed to open bitmap inode"); + } else { + vol->lcnbmp_na = ntfs_attr_open(vol->lcnbmp_ni, AT_DATA, + AT_UNNAMED, 0); + if (!vol->lcnbmp_na) { + ntfs_log_perror("Failed to open bitmap data attribute"); + } else { + /* minimal consistency check on the bitmap */ + if (((vol->lcnbmp_na->data_size << 3) + < vol->nr_clusters) + || ((vol->lcnbmp_na->data_size << 3) + >= (vol->nr_clusters << 1)) + || (vol->lcnbmp_na->data_size + > vol->lcnbmp_na->allocated_size)) { + ntfs_log_error("Corrupt cluster map size %lld" + " (allocated %lld minimum %lld)\n", + (long long)vol->lcnbmp_na->data_size, + (long long)vol->lcnbmp_na->allocated_size, + (long long)(vol->nr_clusters + 7) >> 3); + } else { + ntfs_log_info("Rewriting $UpCase file\n"); + l = ntfs_attr_pwrite(na, 0, vol->upcase_len*2, + vol->upcase); + if (l != vol->upcase_len*2) { + ntfs_log_error("Failed to rewrite $UpCase\n"); + } else { + ntfs_log_info("$UpCase has been set to default\n"); + res = 0; + } + } + ntfs_attr_close(vol->lcnbmp_na); + vol->lcnbmp_na = (ntfs_attr*)NULL; + } + ntfs_inode_close(vol->lcnbmp_ni); + vol->lcnbmp_ni = (ntfs_inode*)NULL; + } + return (res); +} + +/* + * Fix the $UpCase file + * + * Returns 0 if the table is valid or has been fixed + */ + +static int fix_upcase(ntfs_volume *vol) +{ + ntfs_inode *ni; + ntfs_attr *na; + ntfschar *upcase; + s64 l; + u32 upcase_len; + u32 k; + int res; + + res = -1; + ni = (ntfs_inode*)NULL; + na = (ntfs_attr*)NULL; + /* Now load the upcase table from $UpCase. */ + ntfs_log_debug("Loading $UpCase...\n"); + ni = ntfs_inode_open(vol, FILE_UpCase); + if (!ni) { + ntfs_log_perror("Failed to open inode FILE_UpCase"); + goto error_exit; + } + /* Get an ntfs attribute for $UpCase/$DATA. */ + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + ntfs_log_perror("Failed to open ntfs attribute"); + goto error_exit; + } + /* + * Note: Normally, the upcase table has a length equal to 65536 + * 2-byte Unicode characters but allow for different cases, so no + * checks done. Just check we don't overflow 32-bits worth of Unicode + * characters. + */ + if (na->data_size & ~0x1ffffffffULL) { + ntfs_log_error("Error: Upcase table is too big (max 32-bit " + "allowed).\n"); + errno = EINVAL; + goto error_exit; + } + upcase_len = na->data_size >> 1; + upcase = (ntfschar*)ntfs_malloc(na->data_size); + if (!upcase) + goto error_exit; + /* Read in the $DATA attribute value into the buffer. */ + l = ntfs_attr_pread(na, 0, na->data_size, upcase); + if (l != na->data_size) { + ntfs_log_error("Failed to read $UpCase, unexpected length " + "(%lld != %lld).\n", (long long)l, + (long long)na->data_size); + errno = EIO; + goto error_exit; + } + /* Consistency check of $UpCase, restricted to plain ASCII chars */ + k = 0x20; + while ((k < upcase_len) + && (k < 0x7f) + && (le16_to_cpu(upcase[k]) + == ((k < 'a') || (k > 'z') ? k : k + 'A' - 'a'))) + k++; + if (k < 0x7f) { + ntfs_log_error("Corrupted file $UpCase\n"); + if (!opt.no_action) { + /* rewrite the $UpCase file from default */ + res = rewrite_upcase(vol, na); + /* free the bad upcase record */ + if (!res) + free(upcase); + } else { + /* keep the default upcase but return an error */ + free(upcase); + } + } else { + /* accept the upcase table read from $UpCase */ + free(vol->upcase); + vol->upcase = upcase; + vol->upcase_len = upcase_len; + res = 0; + } +error_exit : + /* Done with the $UpCase mft record. */ + if (na) + ntfs_attr_close(na); + if (ni && ntfs_inode_close(ni)) { + ntfs_log_perror("Failed to close $UpCase"); + } + return (res); +} + +/* + * Rewrite the boot sector + * + * Returns 0 if successful + */ + +static int rewrite_boot(struct ntfs_device *dev, char *full_bs, + s32 sector_size) +{ + s64 bw; + int res; + + res = -1; + ntfs_log_info("Rewriting the bootsector\n"); + bw = ntfs_pwrite(dev, 0, sector_size, full_bs); + if (bw == sector_size) + res = 0; + else { + if (bw != -1) + errno = EINVAL; + if (!bw) + ntfs_log_error("Failed to rewrite the bootsector (size=0)\n"); + else + ntfs_log_perror("Error rewriting the bootsector"); + } + return (res); +} + +/* + * Locate an unnamed attribute in an MFT record + * + * Returns NULL if not found (with no error message) + */ + +static ATTR_RECORD *find_unnamed_attr(MFT_RECORD *mrec, ATTR_TYPES type) +{ + ATTR_RECORD *a; + u32 offset; + s32 space; + + /* fetch the requested attribute */ + offset = le16_to_cpu(mrec->attrs_offset); + space = le32_to_cpu(mrec->bytes_in_use) - offset; + a = (ATTR_RECORD*)((char*)mrec + offset); + while ((space >= (s32)offsetof(ATTR_RECORD, resident_end)) + && (a->type != AT_END) + && (le32_to_cpu(a->length) <= (u32)space) + && !(le32_to_cpu(a->length) & 7) + && ((a->type != type) || a->name_length)) { + offset += le32_to_cpu(a->length); + space -= le32_to_cpu(a->length); + a = (ATTR_RECORD*)((char*)mrec + offset); + } + if ((offset >= le32_to_cpu(mrec->bytes_in_use)) + || (a->type != type) + || a->name_length) + a = (ATTR_RECORD*)NULL; + return (a); +} + +/* + * First condition for having a self-located MFT : + * only 16 MFT records are defined in MFT record 0 + * + * Only low-level library functions can be used. + * + * Returns TRUE if the condition is met. + */ + +static BOOL short_mft_selfloc_condition(struct MFT_SELF_LOCATED *selfloc) +{ + BOOL ok; + ntfs_volume *vol; + MFT_RECORD *mft0; + ATTR_RECORD *a; + runlist_element *rl; + u16 seqn; + + ok = FALSE; + vol = selfloc->vol; + mft0 = selfloc->mft0; + if ((ntfs_pread(vol->dev, + vol->mft_lcn << vol->cluster_size_bits, + vol->mft_record_size, mft0) + == vol->mft_record_size) + && !ntfs_mst_post_read_fixup((NTFS_RECORD*)mft0, + vol->mft_record_size) + && !ntfs_mft_record_check(vol, 0, mft0)) { + a = find_unnamed_attr(mft0,AT_DATA); + if (a + && a->non_resident + && (((sle64_to_cpu(a->highest_vcn) + 1) + << vol->cluster_size_bits) + == (SELFLOC_LIMIT*vol->mft_record_size))) { + rl = ntfs_mapping_pairs_decompress(vol, a, NULL); + if (rl) { + /* + * The first error condition is having only + * 16 entries mapped in the first MFT record. + */ + if ((rl[0].lcn >= 0) + && ((rl[0].length << vol->cluster_size_bits) + == SELFLOC_LIMIT*vol->mft_record_size) + && (rl[1].vcn == rl[0].length) + && (rl[1].lcn == LCN_RL_NOT_MAPPED)) { + ok = TRUE; + seqn = le16_to_cpu( + mft0->sequence_number); + selfloc->mft_ref0 + = ((MFT_REF)seqn) << 48; + } + free(rl); + } + } + } + return (ok); +} + +/* + * Second condition for having a self-located MFT : + * The 16th MFT record is defined in MFT record >= 16 + * + * Only low-level library functions can be used. + * + * Returns TRUE if the condition is met. + */ + +static BOOL attrlist_selfloc_condition(struct MFT_SELF_LOCATED *selfloc) +{ + ntfs_volume *vol; + ATTR_RECORD *a; + ATTR_LIST_ENTRY *attrlist; + ATTR_LIST_ENTRY *al; + runlist_element *rl; + VCN vcn; + leVCN levcn; + u32 length; + int ok; + + ok = FALSE; + length = 0; + vol = selfloc->vol; + a = find_unnamed_attr(selfloc->mft0,AT_ATTRIBUTE_LIST); + if (a) { + selfloc->attrlist_resident = !a->non_resident; + selfloc->attrlist_lcn = 0; + if (a->non_resident) { + attrlist = selfloc->attrlist; + rl = ntfs_mapping_pairs_decompress(vol, a, NULL); + if (rl + && (rl->lcn >= 0) + && (sle64_to_cpu(a->data_size) < vol->cluster_size) + && (ntfs_pread(vol->dev, + rl->lcn << vol->cluster_size_bits, + vol->cluster_size, attrlist) == vol->cluster_size)) { + selfloc->attrlist_lcn = rl->lcn; + al = attrlist; + length = sle64_to_cpu(a->data_size); + } + } else { + al = (ATTR_LIST_ENTRY*) + ((char*)a + le16_to_cpu(a->value_offset)); + length = le32_to_cpu(a->value_length); + } + if (length) { + /* search for a data attribute defining entry 16 */ + vcn = (SELFLOC_LIMIT*vol->mft_record_size) + >> vol->cluster_size_bits; + levcn = cpu_to_sle64(vcn); + while ((length > 0) + && al->length + && ((al->type != AT_DATA) + || ((leVCN)al->lowest_vcn != levcn))) { + length -= le16_to_cpu(al->length); + al = (ATTR_LIST_ENTRY*) + ((char*)al + le16_to_cpu(al->length)); + } + if ((length > 0) + && al->length + && (al->type == AT_DATA) + && !al->name_length + && ((leVCN)al->lowest_vcn == levcn) + && (MREF_LE(al->mft_reference) >= SELFLOC_LIMIT)) { + selfloc->mft_ref1 + = le64_to_cpu(al->mft_reference); + selfloc->attrlist_to_ref1 = al; + ok = TRUE; + } + } + } + return (ok); +} + +/* + * Third condition for having a self-located MFT : + * The location of the second part of the MFT is defined in itself + * + * To locate the second part, we have to assume the first and the + * second part of the MFT data are contiguous. + * + * Only low-level library functions can be used. + * + * Returns TRUE if the condition is met. + */ + +static BOOL self_mapped_selfloc_condition(struct MFT_SELF_LOCATED *selfloc) +{ + BOOL ok; + s64 inum; + u64 offs; + VCN lowest_vcn; + MFT_RECORD *mft1; + ATTR_RECORD *a; + ntfs_volume *vol; + runlist_element *rl; + + ok = FALSE; + vol = selfloc->vol; + mft1 = selfloc->mft1; + inum = MREF(selfloc->mft_ref1); + offs = (vol->mft_lcn << vol->cluster_size_bits) + + (inum << vol->mft_record_size_bits); + if ((ntfs_pread(vol->dev, offs, vol->mft_record_size, + mft1) == vol->mft_record_size) + && !ntfs_mst_post_read_fixup((NTFS_RECORD*)mft1, + vol->mft_record_size) + && !ntfs_mft_record_check(vol, inum, mft1)) { + + lowest_vcn = (SELFLOC_LIMIT*vol->mft_record_size) + >> vol->cluster_size_bits; + a = find_unnamed_attr(mft1,AT_DATA); + if (a + && (mft1->flags & MFT_RECORD_IN_USE) + && ((VCN)sle64_to_cpu(a->lowest_vcn) == lowest_vcn) + && (le64_to_cpu(mft1->base_mft_record) + == selfloc->mft_ref0) + && ((u16)MSEQNO(selfloc->mft_ref1) + == le16_to_cpu(mft1->sequence_number))) { + rl = ntfs_mapping_pairs_decompress(vol, a, NULL); + if ((rl[0].lcn == LCN_RL_NOT_MAPPED) + && !rl[0].vcn + && (rl[0].length == lowest_vcn) + && (rl[1].vcn == lowest_vcn) + && ((u64)(rl[1].lcn << vol->cluster_size_bits) + <= offs) + && ((u64)((rl[1].lcn + rl[1].length) + << vol->cluster_size_bits) > offs)) { + ok = TRUE; + } + } + } + return (ok); +} + +/* + * Fourth condition, to be able to fix a self-located MFT : + * The MFT record 15 must be available. + * + * The MFT record 15 is expected to be marked in use, we assume + * it is available if it has no parent, no name and no attr list. + * + * Only low-level library functions can be used. + * + * Returns TRUE if the condition is met. + */ + +static BOOL spare_record_selfloc_condition(struct MFT_SELF_LOCATED *selfloc) +{ + BOOL ok; + s64 inum; + u64 offs; + MFT_RECORD *mft2; + ntfs_volume *vol; + + ok = FALSE; + vol = selfloc->vol; + mft2 = selfloc->mft2; + inum = SELFLOC_LIMIT - 1; + offs = (vol->mft_lcn << vol->cluster_size_bits) + + (inum << vol->mft_record_size_bits); + if ((ntfs_pread(vol->dev, offs, vol->mft_record_size, + mft2) == vol->mft_record_size) + && !ntfs_mst_post_read_fixup((NTFS_RECORD*)mft2, + vol->mft_record_size) + && !ntfs_mft_record_check(vol, inum, mft2)) { + if (!mft2->base_mft_record + && (mft2->flags & MFT_RECORD_IN_USE) + && !find_unnamed_attr(mft2,AT_ATTRIBUTE_LIST) + && !find_unnamed_attr(mft2,AT_FILE_NAME)) { + ok = TRUE; + } + } + return (ok); +} + +/* + * Fix a self-located MFT by swapping two MFT records + * + * Only low-level library functions can be used. + * + * Returns 0 if the MFT corruption could be fixed. + */ +static int fix_selfloc_conditions(struct MFT_SELF_LOCATED *selfloc) +{ + MFT_RECORD *mft1; + MFT_RECORD *mft2; + ATTR_RECORD *a; + ATTR_LIST_ENTRY *al; + ntfs_volume *vol; + s64 offs; + s64 offsm; + s64 offs1; + s64 offs2; + s64 inum; + u16 usa_ofs; + int res; + + res = 0; + /* + * In MFT1, we must fix : + * - the self-reference, if present, + * - its own sequence number, must be 15 + * - the sizes of the data attribute. + */ + vol = selfloc->vol; + mft1 = selfloc->mft1; + mft2 = selfloc->mft2; + usa_ofs = le16_to_cpu(mft1->usa_ofs); + if (usa_ofs >= 48) + mft1->mft_record_number = const_cpu_to_le32(SELFLOC_LIMIT - 1); + mft1->sequence_number = const_cpu_to_le16(SELFLOC_LIMIT - 1); + a = find_unnamed_attr(mft1,AT_DATA); + if (a) { + a->allocated_size = const_cpu_to_sle64(0); + a->data_size = const_cpu_to_sle64(0); + a->initialized_size = const_cpu_to_sle64(0); + } else + res = -1; /* bug : it has been found earlier */ + + /* + * In MFT2, we must fix : + * - the self-reference, if present + */ + usa_ofs = le16_to_cpu(mft2->usa_ofs); + if (usa_ofs >= 48) + mft2->mft_record_number = cpu_to_le32(MREF(selfloc->mft_ref1)); + + /* + * In the attribute list, we must fix : + * - the reference to MFT1 + */ + al = selfloc->attrlist_to_ref1; + al->mft_reference = MK_LE_MREF(SELFLOC_LIMIT - 1, SELFLOC_LIMIT - 1); + + /* + * All fixes done, we can write all if allowed + */ + if (!res && !opt.no_action) { + inum = SELFLOC_LIMIT - 1; + offs2 = (vol->mft_lcn << vol->cluster_size_bits) + + (inum << vol->mft_record_size_bits); + inum = MREF(selfloc->mft_ref1); + offs1 = (vol->mft_lcn << vol->cluster_size_bits) + + (inum << vol->mft_record_size_bits); + + /* rewrite the attribute list */ + if (selfloc->attrlist_resident) { + /* write mft0 and mftmirr if it is resident */ + offs = vol->mft_lcn << vol->cluster_size_bits; + offsm = vol->mftmirr_lcn << vol->cluster_size_bits; + if (ntfs_mst_pre_write_fixup( + (NTFS_RECORD*)selfloc->mft0, + vol->mft_record_size) + || (ntfs_pwrite(vol->dev, offs, vol->mft_record_size, + selfloc->mft0) != vol->mft_record_size) + || (ntfs_pwrite(vol->dev, offsm, vol->mft_record_size, + selfloc->mft0) != vol->mft_record_size)) + res = -1; + } else { + /* write a full cluster if non resident */ + offs = selfloc->attrlist_lcn << vol->cluster_size_bits; + if (ntfs_pwrite(vol->dev, offs, vol->cluster_size, + selfloc->attrlist) != vol->cluster_size) + res = -1; + } + /* replace MFT2 by MFT1 and replace MFT1 by MFT2 */ + if (!res + && (ntfs_mst_pre_write_fixup((NTFS_RECORD*)selfloc->mft1, + vol->mft_record_size) + || ntfs_mst_pre_write_fixup((NTFS_RECORD*)selfloc->mft2, + vol->mft_record_size) + || (ntfs_pwrite(vol->dev, offs2, vol->mft_record_size, + mft1) != vol->mft_record_size) + || (ntfs_pwrite(vol->dev, offs1, vol->mft_record_size, + mft2) != vol->mft_record_size))) + res = -1; + } + return (res); +} + +/* + * Detect and fix a Windows XP bug, leading to a corrupt MFT + * + * Windows cannot boot anymore, so chkdsk cannot be started, which + * is a good point, because chkdsk would have deleted all the files. + * Older ntfs-3g fell into an endless recursion (recent versions + * refuse to mount). + * + * This situation is very rare, but it was fun to fix it. + * + * The corrupted condition is : + * - MFT entry 0 has only the runlist for MFT entries 0-15 + * - The attribute list for MFT shows the second part + * in an MFT record beyond 15 + * Of course, this record has to be read in order to know where it is. + * + * Sample case, met in 2011 (Windows XP) : + * MFT record 0 has : stdinfo, nonres attrlist, the first + * part of MFT data (entries 0-15), and bitmap + * MFT record 16 has the name + * MFT record 17 has the third part of MFT data (16-117731) + * MFT record 18 has the second part of MFT data (117732-170908) + * + * Assuming the second part of the MFT is contiguous to the first + * part, we can find it, and fix the condition by relocating it + * and swapping it with MFT record 15. + * This record number 15 appears to be hardcoded into Windows NTFS. + * + * Only low-level library functions can be used. + * + * Returns 0 if the conditions for the error was met and + * this error could be fixed, + * -1 if the condition was not met or some error + * which could not be fixed was encountered. + */ + +static int fix_self_located_mft(ntfs_volume *vol) +{ + struct MFT_SELF_LOCATED selfloc; + BOOL res; + + ntfs_log_info("Checking for self-located MFT segment... "); + res = -1; + selfloc.vol = vol; + selfloc.mft0 = (MFT_RECORD*)malloc(vol->mft_record_size); + selfloc.mft1 = (MFT_RECORD*)malloc(vol->mft_record_size); + selfloc.mft2 = (MFT_RECORD*)malloc(vol->mft_record_size); + selfloc.attrlist = (ATTR_LIST_ENTRY*)malloc(vol->cluster_size); + if (selfloc.mft0 && selfloc.mft1 && selfloc.mft2 + && selfloc.attrlist) { + if (short_mft_selfloc_condition(&selfloc) + && attrlist_selfloc_condition(&selfloc) + && self_mapped_selfloc_condition(&selfloc) + && spare_record_selfloc_condition(&selfloc)) { + ntfs_log_info(FOUND); + ntfs_log_info("Fixing the self-located MFT segment... "); + res = fix_selfloc_conditions(&selfloc); + ntfs_log_info(res ? FAILED : OK); + } else { + ntfs_log_info(OK); + res = -1; + } + free(selfloc.mft0); + free(selfloc.mft1); + free(selfloc.mft2); + free(selfloc.attrlist); + } + return (res); +} + +/* + * Try an alternate boot sector and fix the real one + * + * Only after successful checks is the boot sector rewritten. + * + * The alternate boot sector is not rewritten, either because it + * was found correct, or because we truncated the file system + * and the last actual sector might be part of some file. + * + * Returns 0 if successful + */ + +static int try_fix_boot(ntfs_volume *vol, char *full_bs, + s64 read_sector, s64 fix_sectors, s32 sector_size) +{ + s64 br; + int res; + s64 got_sectors; + le16 sector_size_le; + NTFS_BOOT_SECTOR *bs; + + res = -1; + br = ntfs_pread(vol->dev, read_sector*sector_size, + sector_size, full_bs); + if (br != sector_size) { + if (br != -1) + errno = EINVAL; + if (!br) + ntfs_log_error("Failed to read alternate bootsector (size=0)\n"); + else + ntfs_log_perror("Error reading alternate bootsector"); + } else { + bs = (NTFS_BOOT_SECTOR*)full_bs; + got_sectors = sle64_to_cpu(bs->number_of_sectors); + bs->number_of_sectors = cpu_to_sle64(fix_sectors); + /* alignment problem on Sparc, even doing memcpy() */ + sector_size_le = cpu_to_le16(sector_size); + if (!memcmp(§or_size_le, &bs->bpb.bytes_per_sector,2) + && ntfs_boot_sector_is_ntfs(bs) + && !ntfs_boot_sector_parse(vol, bs)) { + ntfs_log_info("The alternate bootsector is usable\n"); + if (fix_sectors != got_sectors) + ntfs_log_info("Set sector count to %lld instead of %lld\n", + (long long)fix_sectors, + (long long)got_sectors); + /* fix the normal boot sector */ + if (!opt.no_action) { + res = rewrite_boot(vol->dev, full_bs, + sector_size); + } else + res = 0; + } + if (!res && !opt.no_action) + ntfs_log_info("The boot sector has been rewritten\n"); + } + return (res); +} + +/* + * Try the alternate boot sector if the normal one is bad + * + * Actually : + * - first try the last sector of the partition (expected location) + * - then try the last sector as shown in the main boot sector, + * (could be meaningful for an undersized partition) + * - finally try truncating the file system actual size of partition + * (could be meaningful for an oversized partition) + * + * if successful, rewrite the normal boot sector accordingly + * + * Returns 0 if successful + */ + +static int try_alternate_boot(ntfs_volume *vol, char *full_bs, + s32 sector_size, s64 shown_sectors) +{ + s64 actual_sectors; + int res; + + res = -1; + ntfs_log_info("Trying the alternate boot sector\n"); + + /* + * We do not rely on the sector size defined in the + * boot sector, supposed to be corrupt, so we try to get + * the actual sector size and defaulting to 512 if failed + * to get. This value is only used to guess the alternate + * boot sector location and it is checked against the + * value found in the sector itself. It should not damage + * anything if wrong. + * + * Note : the real last sector is not accounted for here. + */ + actual_sectors = ntfs_device_size_get(vol->dev,sector_size) - 1; + + /* first try the actual last sector */ + if ((actual_sectors > 0) + && !try_fix_boot(vol, full_bs, actual_sectors, + actual_sectors, sector_size)) + res = 0; + + /* then try the shown last sector, if less than actual */ + if (res + && (shown_sectors > 0) + && (shown_sectors < actual_sectors) + && !try_fix_boot(vol, full_bs, shown_sectors, + shown_sectors, sector_size)) + res = 0; + + /* then try reducing the number of sectors to actual value */ + if (res + && (shown_sectors > actual_sectors) + && !try_fix_boot(vol, full_bs, 0, actual_sectors, sector_size)) + res = 0; + + return (res); +} + +/* + * Check and fix the alternate boot sector + * + * The alternate boot sector is usually in the last sector of a + * partition, which should not be used by the file system + * (the sector count in the boot sector should be less than + * the total sector count in the partition). + * + * chkdsk never changes the count in the boot sector. + * - If this is less than the total count, chkdsk place the + * alternate boot sector into the sector, + * - if the count is the same as the total count, chkdsk place + * the alternate boot sector into the middle sector (half + * the total count rounded upwards) + * - if the count is greater than the total count, chkdsk + * declares the file system as raw, and refuses to fix anything. + * + * Here, we check and fix the alternate boot sector, only in the + * first situation where the file system does not overflow on the + * last sector. + * + * Note : when shrinking a partition, ntfsresize cannot determine + * the future size of the partition. As a consequence the number of + * sectors in the boot sectors may be less than the possible size. + * + * Returns 0 if successful + */ + +static int check_alternate_boot(ntfs_volume *vol) +{ + s64 got_sectors; + s64 actual_sectors; + s64 last_sector_off; + char *full_bs; + char *alt_bs; + NTFS_BOOT_SECTOR *bs; + s64 br; + s64 bw; + int res; + + res = -1; + full_bs = (char*)malloc(vol->sector_size); + alt_bs = (char*)malloc(vol->sector_size); + if (!full_bs || !alt_bs) { + ntfs_log_info("Error : failed to allocate memory\n"); + goto error_exit; + } + /* Now read both bootsectors. */ + br = ntfs_pread(vol->dev, 0, vol->sector_size, full_bs); + if (br == vol->sector_size) { + bs = (NTFS_BOOT_SECTOR*)full_bs; + got_sectors = sle64_to_cpu(bs->number_of_sectors); + actual_sectors = ntfs_device_size_get(vol->dev, + vol->sector_size); + if (actual_sectors > got_sectors) { + last_sector_off = (actual_sectors - 1) + << vol->sector_size_bits; + ntfs_log_info("Checking the alternate boot sector... "); + br = ntfs_pread(vol->dev, last_sector_off, + vol->sector_size, alt_bs); + } else { + ntfs_log_info("Checking file system overflow... "); + br = -1; + } + /* accept getting no byte, needed for short image files */ + if (br >= 0) { + if ((br != vol->sector_size) + || memcmp(full_bs, alt_bs, vol->sector_size)) { + if (opt.no_action) { + ntfs_log_info("BAD\n"); + } else { + bw = ntfs_pwrite(vol->dev, + last_sector_off, + vol->sector_size, full_bs); + if (bw == vol->sector_size) { + ntfs_log_info("FIXED\n"); + res = 0; + } else { + ntfs_log_info(FAILED); + } + } + } else { + ntfs_log_info(OK); + res = 0; + } + } else { + ntfs_log_info(FAILED); + } + } else { + ntfs_log_info("Error : could not read the boot sector again\n"); + } + free(full_bs); + free(alt_bs); + +error_exit : + return (res); +} + +/* + * Try to fix problems which may arise in the start up sequence + * + * This is a replay of the normal start up sequence with fixes when + * some problem arise. + * + * Returns 0 if there was an error and a fix is available + */ + +static int fix_startup(struct ntfs_device *dev, unsigned long flags) +{ + s64 br; + ntfs_volume *vol; + BOOL dev_open; + s64 shown_sectors; + char *full_bs; + NTFS_BOOT_SECTOR *bs; + s32 sector_size; + int res; + int eo; + + errno = 0; + res = -1; + dev_open = FALSE; + full_bs = (char*)NULL; + if (!dev || !dev->d_ops || !dev->d_name) { + errno = EINVAL; + ntfs_log_perror("%s: dev = %p", __FUNCTION__, dev); + vol = (ntfs_volume*)NULL; + goto error_exit; + } + + /* Allocate the volume structure. */ + vol = ntfs_volume_alloc(); + if (!vol) + goto error_exit; + + /* Create the default upcase table. */ + vol->upcase_len = ntfs_upcase_build_default(&vol->upcase); + if (!vol->upcase_len || !vol->upcase) + goto error_exit; + + /* Default with no locase table and case sensitive file names */ + vol->locase = (ntfschar*)NULL; + NVolSetCaseSensitive(vol); + + /* by default, all files are shown and not marked hidden */ + NVolSetShowSysFiles(vol); + NVolSetShowHidFiles(vol); + NVolClearHideDotFiles(vol); + if (flags & NTFS_MNT_RDONLY) + NVolSetReadOnly(vol); + + /* ...->open needs bracketing to compile with glibc 2.7 */ + if ((dev->d_ops->open)(dev, NVolReadOnly(vol) ? O_RDONLY: O_RDWR)) { + ntfs_log_perror("Error opening '%s'", dev->d_name); + goto error_exit; + } + dev_open = TRUE; + /* Attach the device to the volume. */ + vol->dev = dev; + + sector_size = ntfs_device_sector_size_get(dev); + if (sector_size <= 0) + sector_size = DEFAULT_SECTOR_SIZE; + full_bs = (char*)malloc(sector_size); + if (!full_bs) + goto error_exit; + /* Now read the bootsector. */ + br = ntfs_pread(dev, 0, sector_size, full_bs); + if (br != sector_size) { + if (br != -1) + errno = EINVAL; + if (!br) + ntfs_log_error("Failed to read bootsector (size=0)\n"); + else + ntfs_log_perror("Error reading bootsector"); + goto error_exit; + } + bs = (NTFS_BOOT_SECTOR*)full_bs; + if (!ntfs_boot_sector_is_ntfs(bs) + /* get the bootsector data, only fails when inconsistent */ + || (ntfs_boot_sector_parse(vol, bs) < 0)) { + shown_sectors = sle64_to_cpu(bs->number_of_sectors); + /* boot sector is wrong, try the alternate boot sector */ + if (try_alternate_boot(vol, full_bs, sector_size, + shown_sectors)) { + errno = EINVAL; + goto error_exit; + } + res = 0; + } else { + res = fix_self_located_mft(vol); + } +error_exit: + if (res) { + switch (errno) { + case ENOMEM : + ntfs_log_error("Failed to allocate memory\n"); + break; + case EINVAL : + ntfs_log_error("Unrecoverable error\n"); + break; + default : + break; + } + } + eo = errno; + free(full_bs); + if (vol) { + free(vol->upcase); + free(vol); + } + if (dev_open) { + (dev->d_ops->close)(dev); + } + errno = eo; + return (res); +} + +/** + * fix_mount + */ +static int fix_mount(void) +{ + int ret = 0; /* default success */ + ntfs_volume *vol; + struct ntfs_device *dev; + unsigned long flags; + + ntfs_log_info("Attempting to correct errors... "); + + dev = ntfs_device_alloc(opt.volume, 0, &ntfs_device_default_io_ops, + NULL); + if (!dev) { + ntfs_log_info(FAILED); + ntfs_log_perror("Failed to allocate device"); + return -1; + } + flags = (opt.no_action ? NTFS_MNT_RDONLY : 0); + vol = ntfs_volume_startup(dev, flags); + if (!vol) { + ntfs_log_info(FAILED); + ntfs_log_perror("Failed to startup volume"); + + /* Try fixing the bootsector and MFT, then redo the startup */ + if (!fix_startup(dev, flags)) { + if (opt.no_action) + ntfs_log_info("The startup data can be fixed, " + "but no change was requested\n"); + else + vol = ntfs_volume_startup(dev, flags); + } + if (!vol) { + ntfs_log_error("Volume is corrupt. You should run chkdsk.\n"); + ntfs_device_free(dev); + return -1; + } + if (opt.no_action) + ret = -1; /* error present and not fixed */ + } + /* if option -n proceed despite errors, to display them all */ + if ((!ret || opt.no_action) && (fix_mftmirr(vol) < 0)) + ret = -1; + if ((!ret || opt.no_action) && (fix_upcase(vol) < 0)) + ret = -1; + if ((!ret || opt.no_action) && (set_dirty_flag(vol) < 0)) + ret = -1; + if ((!ret || opt.no_action) && (empty_journal(vol) < 0)) + ret = -1; + /* + * ntfs_umount() will invoke ntfs_device_free() for us. + * Ignore the returned error resulting from partial mounting. + */ + ntfs_umount(vol, 1); + return ret; +} + +/** + * main + */ +int main(int argc, char **argv) +{ + ntfs_volume *vol; + unsigned long mnt_flags; + unsigned long flags; + int ret = 1; /* failure */ + BOOL force = FALSE; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + parse_options(argc, argv); + + if (!ntfs_check_if_mounted(opt.volume, &mnt_flags)) { + if ((mnt_flags & NTFS_MF_MOUNTED) && + !(mnt_flags & NTFS_MF_READONLY) && !force) { + ntfs_log_error("Refusing to operate on read-write " + "mounted device %s.\n", opt.volume); + exit(1); + } + } else + ntfs_log_perror("Failed to determine whether %s is mounted", + opt.volume); + /* Attempt a full mount first. */ + flags = (opt.no_action ? NTFS_MNT_RDONLY : 0); + ntfs_log_info("Mounting volume... "); + vol = ntfs_mount(opt.volume, flags); + if (vol) { + ntfs_log_info(OK); + ntfs_log_info("Processing of $MFT and $MFTMirr completed " + "successfully.\n"); + } else { + ntfs_log_info(FAILED); + if (fix_mount() < 0) { + if (opt.no_action) + ntfs_log_info("No change made\n"); + exit(1); + } + vol = ntfs_mount(opt.volume, 0); + if (!vol) { + ntfs_log_perror("Remount failed"); + exit(1); + } + } + if (check_alternate_boot(vol)) { + ntfs_log_error("Error: Failed to fix the alternate boot sector\n"); + exit(1); + } + /* So the unmount does not clear it again. */ + + /* Porting note: The WasDirty flag was set here to prevent ntfs_unmount + * from clearing the dirty bit (which might have been set in + * fix_mount()). So the intention is to leave the dirty bit set. + * + * libntfs-3g does not automatically set or clear dirty flags on + * mount/unmount, this means that the assumption that the dirty flag is + * now set does not hold. So we need to set it if not already set. + * + * However clear the flag if requested to do so, at this stage + * mounting was successful. + */ + if (opt.clear_dirty) + vol->flags &= ~VOLUME_IS_DIRTY; + else + vol->flags |= VOLUME_IS_DIRTY; + if (!opt.no_action && ntfs_volume_write_flags(vol, vol->flags)) { + ntfs_log_error("Error: Failed to set volume dirty flag (%d " + "(%s))!\n", errno, strerror(errno)); + } + + /* Check NTFS version is ok for us (in $Volume) */ + ntfs_log_info("NTFS volume version is %i.%i.\n", vol->major_ver, + vol->minor_ver); + if (ntfs_version_is_supported(vol)) { + ntfs_log_error("Error: Unknown NTFS version.\n"); + goto error_exit; + } + if (opt.clear_bad_sectors && !opt.no_action) { + if (clear_badclus(vol)) { + ntfs_log_error("Error: Failed to un-mark bad sectors.\n"); + goto error_exit; + } + } + if (vol->major_ver >= 3) { + /* + * FIXME: If on NTFS 3.0+, check for presence of the usn + * journal and stamp it if present. + */ + } + /* FIXME: We should be marking the quota out of date, too. */ + /* That's all for now! */ + ntfs_log_info("NTFS partition %s was processed successfully.\n", + vol->dev->d_name); + /* Set return code to 0. */ + ret = 0; +error_exit: + if (ntfs_umount(vol, 1)) { + ntfs_log_info("Failed to unmount partition\n"); + ret = 1; + } + if (ret) + exit(ret); + return ret; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsinfo.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsinfo.8.in new file mode 100644 index 0000000..03efc65 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsinfo.8.in @@ -0,0 +1,89 @@ +.\" Copyright (c) 2002\-2004 Anton Altaparmakov. +.\" Copyright (c) 2005 Richard Russon. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSINFO 8 "April 2006" "ntfs-3g @VERSION@" +.SH NAME +ntfsinfo \- dump a file's attributes +.SH SYNOPSIS +.B ntfsinfo +[\fIoptions\fR] \fIdevice\fR +.SH DESCRIPTION +.B ntfsinfo +will dump the attributes of inode +.I inode\-number +or the file +.I path\-filename +and/or information about the mft ( +.I \-m +option). +Run ntfsinfo without arguments for a full list of options. +.SH OPTIONS +Below is a summary of all the options that +.B ntfsinfo +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-F\fR, \fB\-\-file\fR FILE +Show information about this file +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not overwriting an existing +file. Use this option with caution. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-i\fR, \fB\-\-inode\fR NUM +Show information about this inode. +.TP +\fB\-m\fR, \fB\-\-mft\fR +Show information about the volume. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Produce less output. +.TP +\fB\-t\fR, \fB\-\-notime\fR +Do not display timestamps in the output. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Increase the amount of output that +.B ntfsinfo +prints. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license. +.SH BUGS +There are no known problems with +.BR ntfsinfo . +If you find a bug please send an email describing the problem to the +development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfsinfo +was written by Matthew J. Fanto, Anton Altaparmakov, Richard Russon, Szabolcs +Szakacsits, Yuval Fledel, Yura Pakhuchiy and Cristian Klein. +It was ported to ntfs-3g by Erik Larsson and Jean-Pierre Andre. +.SH AVAILABILITY +.B ntfsinfo +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsinfo.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsinfo.c new file mode 100644 index 0000000..960a348 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsinfo.c @@ -0,0 +1,2666 @@ +/** + * ntfsinfo - Part of the Linux-NTFS project. + * + * Copyright (c) 2002-2004 Matthew J. Fanto + * Copyright (c) 2002-2006 Anton Altaparmakov + * Copyright (c) 2002-2005 Richard Russon + * Copyright (c) 2003-2006 Szabolcs Szakacsits + * Copyright (c) 2004-2005 Yuval Fledel + * Copyright (c) 2004-2007 Yura Pakhuchiy + * Copyright (c) 2005 Cristian Klein + * Copyright (c) 2011-2022 Jean-Pierre Andre + * + * This utility will dump a file's attributes. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +/* + * TODO LIST: + * - Better error checking. (focus on ntfs_dump_volume) + * - Comment things better. + * - More things at verbose mode. + * - Dump ACLs when security_id exists (NTFS 3+ only). + * - Clean ups. + * - Internationalization. + * - Add more Indexed Attr Types. + * - Make formatting look more like www.flatcap.org/ntfs/info + * + * Still not dumping certain attributes. Need to find the best + * way to output some of these attributes. + * + * Still need to do: + * $REPARSE_POINT/$SYMBOLIC_LINK + * $LOGGED_UTILITY_STREAM + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif + +#include "types.h" +#include "mft.h" +#include "attrib.h" +#include "layout.h" +#include "inode.h" +#include "index.h" +#include "utils.h" +#include "security.h" +#include "mst.h" +#include "dir.h" +#include "logfile.h" +#include "ntfstime.h" +/* #include "version.h" */ +#include "support.h" +#include "misc.h" + +static const char *EXEC_NAME = "ntfsinfo"; + +static struct options { + const char *device; /* Device/File to work with */ + const char *filename; /* Resolve this filename to mft number */ + s64 inode; /* Info for this inode */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + int force; /* Override common sense */ + int notime; /* Don't report timestamps at all */ + int mft; /* Dump information about the volume as well */ +} opts; + +struct RUNCOUNT { + unsigned long runs; + unsigned long fragments; +} ; + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + printf("\n%s v%s (libntfs-3g) - Display information about an NTFS " + "Volume.\n\n", EXEC_NAME, VERSION); + printf("Copyright (c)\n"); + printf(" 2002-2004 Matthew J. Fanto\n"); + printf(" 2002-2006 Anton Altaparmakov\n"); + printf(" 2002-2005 Richard Russon\n"); + printf(" 2003-2006 Szabolcs Szakacsits\n"); + printf(" 2003 Leonard Norrgård\n"); + printf(" 2004-2005 Yuval Fledel\n"); + printf(" 2004-2007 Yura Pakhuchiy\n"); + printf(" 2011-2018 Jean-Pierre Andre\n"); + printf("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + printf("\nUsage: %s [options] device\n" + " -i, --inode NUM Display information about this inode\n" + " -F, --file FILE Display information about this file (absolute path)\n" + " -m, --mft Dump information about the volume\n" + " -t, --notime Don't report timestamps\n" + "\n" + " -f, --force Use less caution\n" + " -q, --quiet Less output\n" + " -v, --verbose More output\n" + " -V, --version Display version information\n" + " -h, --help Display this help\n" + "\n", + EXEC_NAME); + printf("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char *argv[]) +{ + static const char *sopt = "-:dfhi:F:mqtTvV"; + static const struct option lopt[] = { + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "inode", required_argument, NULL, 'i' }, + { "file", required_argument, NULL, 'F' }, + { "quiet", no_argument, NULL, 'q' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { "notime", no_argument, NULL, 'T' }, + { "mft", no_argument, NULL, 'm' }, + { NULL, 0, NULL, 0 } + }; + + int c = -1; + int err = 0; + int ver = 0; + int help = 0; + int levels = 0; + + opterr = 0; /* We'll handle the errors, thank you. */ + + opts.inode = -1; + opts.filename = NULL; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: + if (!opts.device) + opts.device = optarg; + else + err++; + break; + case 'i': + if ((opts.inode != -1) || + (!utils_parse_size(optarg, &opts.inode, FALSE))) { + err++; + } + break; + case 'F': + if (opts.filename == NULL) { + /* The inode can not be resolved here, + store the filename */ + opts.filename = argv[optind-1]; + } else { + /* "-F" can't appear more than once */ + err++; + } + break; + case 'f': + opts.force++; + break; + case 'h': + help++; + break; + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 't': + opts.notime++; + break; + case 'T': + /* 'T' is deprecated, notify */ + ntfs_log_error("Option 'T' is deprecated, it was " + "replaced by 't'.\n"); + err++; + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + ver++; + break; + case 'm': + opts.mft++; + break; + case '?': + if (optopt=='?') { + help++; + continue; + } + if (ntfs_log_parse_option(argv[optind-1])) + continue; + ntfs_log_error("Unknown option '%s'.\n", + argv[optind-1]); + err++; + break; + case ':': + ntfs_log_error("Option '%s' requires an " + "argument.\n", argv[optind-1]); + err++; + break; + default: + ntfs_log_error("Unhandled option case: %d.\n", c); + err++; + break; + } + } + + /* Make sure we're in sync with the log levels */ + levels = ntfs_log_get_levels(); + if (levels & NTFS_LOG_LEVEL_VERBOSE) + opts.verbose++; + if (!(levels & NTFS_LOG_LEVEL_QUIET)) + opts.quiet++; + + if (help || ver) { + opts.quiet = 0; + } else { + if (opts.device == NULL) { + if (argc > 1) + ntfs_log_error("You must specify exactly one " + "device.\n"); + err++; + } + + if (opts.inode == -1 && !opts.filename && !opts.mft) { + if (argc > 1) + ntfs_log_error("You must specify an inode to " + "learn about.\n"); + err++; + } + + if (opts.quiet && opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose " + "at the same time.\n"); + err++; + } + + if ((opts.inode != -1) && (opts.filename != NULL)) { + if (argc > 1) + ntfs_log_error("You may not specify --inode " + "and --file together.\n"); + err++; + } + + } + + if (ver) + version(); + if (help || err) + usage(); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver ? 0 : -1)); +} + + +/* *************** utility functions ******************** */ +/** + * ntfsinfo_time_to_str() - + * @sle_ntfs_clock: on disk time format in 100ns units since 1st jan 1601 + * in little-endian format + * + * Return char* in a format 'Thu Jan 1 00:00:00 1970'. + * No need to free the returned memory. + * + * Example of usage: + * char *time_str = ntfsinfo_time_to_str( + * sle64_to_cpu(standard_attr->creation_time)); + * printf("\tFile Creation Time:\t %s", time_str); + */ +static char *ntfsinfo_time_to_str(const sle64 sle_ntfs_clock) +{ + /* JPA display timestamps in UTC */ + static const char *months[] + = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } ; + static const char *wdays[] + = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" } ; + static char str[50]; + long long stamp; + u32 days; + u32 seconds; + unsigned int year; + unsigned int wday; + int mon; + int cnt; + + stamp = sle64_to_cpu(sle_ntfs_clock); + days = (stamp/(86400*10000000LL)) & 0x7ffff; + seconds = ((stamp/10000000LL)%86400) & 0x1ffff; + wday = (days + 1)%7; + year = 1601; + /* periods of 400 years */ + cnt = days/146097; + days -= 146097*cnt; + year += 400*cnt; + /* periods of 100 years */ + cnt = (3*days + 3)/109573; + days -= 36524*cnt; + year += 100*cnt; + /* periods of 4 years */ + cnt = days/1461; + days -= 1461*cnt; + year += 4*cnt; + /* periods of a single year */ + cnt = (3*days + 3)/1096; + days -= 365*cnt; + year += cnt; + + if ((!(year % 100) ? (year % 400) : (year % 4)) + && (days > 58)) days++; + if (days > 59) { + mon = (5*days + 161)/153; + days -= (153*mon - 162)/5; + } else { + mon = days/31 + 1; + days -= 31*(mon - 1) - 1; + } + snprintf(str, sizeof(str), "%3s %3s %2u %02u:%02u:%02u %4u UTC\n", + wdays[wday], + months[mon-1],(unsigned int)days, + (unsigned int)(seconds/3600), + (unsigned int)(seconds/60%60), + (unsigned int)(seconds%60), + (unsigned int)year); + return (str); +} + +/** + * ntfs_attr_get_name() + * @attr: a valid attribute record + * + * return multi-byte string containing the attribute name if exist. the user + * is then responsible of freeing that memory. + * null if no name exists (attr->name_length==0). no memory allocated. + * null if cannot convert to multi-byte string. errno would contain the + * error id. no memory allocated in that case + */ +static char *ntfs_attr_get_name_mbs(ATTR_RECORD *attr) +{ + ntfschar *ucs_attr_name; + char *mbs_attr_name = NULL; + int mbs_attr_name_size; + + /* Get name in unicode. */ + ucs_attr_name = ntfs_attr_get_name(attr); + /* Convert unicode to printable format. */ + mbs_attr_name_size = ntfs_ucstombs(ucs_attr_name, attr->name_length, + &mbs_attr_name, 0); + if (mbs_attr_name_size > 0) + return mbs_attr_name; + else + return NULL; +} + +static const char *reparse_type_name(le32 tag) +{ + const char *name; + le32 seltag; + + seltag = tag & IO_REPARSE_PLUGIN_SELECT; + switch (seltag) { + case IO_REPARSE_TAG_MOUNT_POINT : + name = " (mount point)"; + break; + case IO_REPARSE_TAG_SYMLINK : + name = " (symlink)"; + break; + case IO_REPARSE_TAG_WOF : + name = " (Wof compressed)"; + break; + case IO_REPARSE_TAG_DEDUP : + name = " (deduplicated)"; + break; + case IO_REPARSE_TAG_WCI : + name = " (Windows container)"; + break; + case IO_REPARSE_TAG_CLOUD : + name = " (Cloud)"; + break; + case IO_REPARSE_TAG_NFS : + name = " (NFS symlink)"; + break; + case IO_REPARSE_TAG_LX_SYMLINK : + name = " (Linux symlink)"; + break; + case IO_REPARSE_TAG_LX_FIFO : + name = " (Linux fifo)"; + break; + case IO_REPARSE_TAG_LX_CHR : + name = " (Linux character device)"; + break; + case IO_REPARSE_TAG_LX_BLK : + name = " (Linux block device)"; + break; + case IO_REPARSE_TAG_AF_UNIX : + name = " (Unix socket)"; + break; + case IO_REPARSE_TAG_APPEXECLINK : + name = " (Exec link)"; + break; + default : + name = ""; + break; + } + return (name); +} + +/* *************** functions for dumping global info ******************** */ + +/* + * Show $LogFile details + */ + +static void ntfs_dump_logfile(ntfs_inode *ni) +{ + ntfs_attr *na; + RESTART_PAGE_HEADER *rp = NULL; + RESTART_AREA *ra; + const char *state; + + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + fprintf(stderr, "Failed to open $FILE_LogFile/$DATA"); + goto out; + } + + if (ntfs_check_logfile(na, &rp)) { + printf("File_Logfile Information\n"); + if (rp) { + ra = (RESTART_AREA*)((char*)rp + + le16_to_cpu(rp->restart_area_offset)); + if (ntfs_is_rstr_record(rp->magic)) + state = "valid"; + else if (ntfs_is_chkd_record(rp->magic)) + state = "checked"; + else if (ntfs_is_empty_record(rp->magic)) + state = "erased"; + else + state = "unknown"; + printf("\tLog state: %s\n", state); + printf("\tSystem page size: %d\n", + (int)le32_to_cpu(rp->system_page_size)); + printf("\tLog page size: %d\n", + (int)le32_to_cpu(rp->log_page_size)); + printf("\tLog version: %d.%d\n", + (int)le16_to_cpu(rp->major_ver), + (int)le16_to_cpu(rp->minor_ver)); + printf("\tLog clients: %d\n", + (int)le16_to_cpu(ra->log_clients)); + printf("\tFree client: %d\n", + (int)le16_to_cpu(ra->client_free_list)); + printf("\tCurrent client: %d\n", + (int)le16_to_cpu(ra->client_in_use_list)); + printf("\tRestart flags: 0x%x\n", + (int)le16_to_cpu(ra->flags)); + if (ntfs_is_logfile_clean(na, rp)) + state = "CLEAN"; + else + state = "DIRTY"; + printf("\tRestart state: %s\n", state); + free(rp); + } else { + printf("\tNo restart page\n"); + } + } + ntfs_attr_close(na); +out : ; +} + +/** + * ntfs_dump_volume - dump information about the volume + */ +static void ntfs_dump_volume(ntfs_volume *vol) +{ + ntfs_inode *log; + + printf("Volume Information \n"); + printf("\tName of device: %s\n", vol->dev->d_name); + printf("\tDevice state: %lu\n", vol->dev->d_state); + printf("\tVolume Name: %s\n", vol->vol_name); + printf("\tVolume State: %lu\n", vol->state); + printf("\tVolume Flags: 0x%04x", (int)le16_to_cpu(vol->flags)); + if (vol->flags & VOLUME_IS_DIRTY) + printf(" DIRTY"); + if (vol->flags & VOLUME_MODIFIED_BY_CHKDSK) + printf(" MODIFIED_BY_CHKDSK"); + printf("\n"); + printf("\tVolume Version: %u.%u\n", vol->major_ver, vol->minor_ver); + printf("\tSector Size: %hu\n", vol->sector_size); + printf("\tCluster Size: %u\n", (unsigned int)vol->cluster_size); + printf("\tIndex Block Size: %u\n", (unsigned int)vol->indx_record_size); + printf("\tVolume Size in Clusters: %lld\n", + (long long)vol->nr_clusters); + + printf("MFT Information \n"); + printf("\tMFT Record Size: %u\n", (unsigned int)vol->mft_record_size); + printf("\tMFT Zone Multiplier: %u\n", vol->mft_zone_multiplier); + printf("\tMFT Data Position: %lld\n", (long long)vol->mft_data_pos); + printf("\tMFT Zone Start: %lld\n", (long long)vol->mft_zone_start); + printf("\tMFT Zone End: %lld\n", (long long)vol->mft_zone_end); + printf("\tMFT Zone Position: %lld\n", (long long)vol->mft_zone_pos); + printf("\tCurrent Position in First Data Zone: %lld\n", + (long long)vol->data1_zone_pos); + printf("\tCurrent Position in Second Data Zone: %lld\n", + (long long)vol->data2_zone_pos); + printf("\tAllocated clusters %lld (%2.1lf%%)\n", + (long long)vol->mft_na->allocated_size + >> vol->cluster_size_bits, + 100.0*(vol->mft_na->allocated_size + >> vol->cluster_size_bits) + / vol->nr_clusters); + printf("\tLCN of Data Attribute for FILE_MFT: %lld\n", + (long long)vol->mft_lcn); + printf("\tFILE_MFTMirr Size: %d\n", vol->mftmirr_size); + printf("\tLCN of Data Attribute for File_MFTMirr: %lld\n", + (long long)vol->mftmirr_lcn); + printf("\tSize of Attribute Definition Table: %d\n", + (int)vol->attrdef_len); + printf("\tNumber of Attached Extent Inodes: %d\n", + (int)vol->mft_ni->nr_extents); + + printf("FILE_Bitmap Information \n"); + printf("\tFILE_Bitmap MFT Record Number: %llu\n", + (unsigned long long)vol->lcnbmp_ni->mft_no); + printf("\tState of FILE_Bitmap Inode: %lu\n", vol->lcnbmp_ni->state); + printf("\tLength of Attribute List: %u\n", + (unsigned int)vol->lcnbmp_ni->attr_list_size); + /* JPA printf("\tAttribute List: %s\n", vol->lcnbmp_ni->attr_list); */ + printf("\tNumber of Attached Extent Inodes: %d\n", + (int)vol->lcnbmp_ni->nr_extents); + /* FIXME: need to add code for the union if nr_extens != 0, but + i dont know if it will ever != 0 with FILE_Bitmap */ + + printf("FILE_Bitmap Data Attribute Information\n"); + printf("\tDecompressed Runlist: not done yet\n"); + printf("\tBase Inode: %llu\n", + (unsigned long long)vol->lcnbmp_na->ni->mft_no); + printf("\tAttribute Types: not done yet\n"); + //printf("\tAttribute Name: %s\n", vol->lcnbmp_na->name); + printf("\tAttribute Name Length: %u\n", + (unsigned int)vol->lcnbmp_na->name_len); + printf("\tAttribute State: %lu\n", vol->lcnbmp_na->state); + printf("\tAttribute Allocated Size: %lld\n", + (long long)vol->lcnbmp_na->allocated_size); + printf("\tAttribute Data Size: %lld\n", + (long long)vol->lcnbmp_na->data_size); + printf("\tAttribute Initialized Size: %lld\n", + (long long)vol->lcnbmp_na->initialized_size); + printf("\tAttribute Compressed Size: %lld\n", + (long long)vol->lcnbmp_na->compressed_size); + printf("\tCompression Block Size: %u\n", + (unsigned int)vol->lcnbmp_na->compression_block_size); + printf("\tCompression Block Size Bits: %u\n", + vol->lcnbmp_na->compression_block_size_bits); + printf("\tCompression Block Clusters: %u\n", + vol->lcnbmp_na->compression_block_clusters); + if (!ntfs_volume_get_free_space(vol)) + printf("\tFree Clusters: %lld (%2.1lf%%)\n", + (long long)vol->free_clusters, + 100.0*vol->free_clusters + /(double)vol->nr_clusters); + log = ntfs_inode_open(vol, FILE_LogFile); + if (!log) { + fprintf(stderr, "Failed to open inode FILE_LogFile"); + } else { + ntfs_dump_logfile(log); + ntfs_inode_close(log); + } + //TODO: Still need to add a few more attributes +} + +/** + * ntfs_dump_flags - Dump flags for STANDARD_INFORMATION and FILE_NAME. + * @type: dump flags for this attribute type + * @flags: flags for dumping + */ +static void ntfs_dump_flags(const char *indent, ATTR_TYPES type, le32 flags) +{ + const le32 original_flags = flags; + + printf("%sFile attributes:\t", indent); + if (flags & FILE_ATTR_READONLY) { + printf(" READONLY"); + flags &= ~FILE_ATTR_READONLY; + } + if (flags & FILE_ATTR_HIDDEN) { + printf(" HIDDEN"); + flags &= ~FILE_ATTR_HIDDEN; + } + if (flags & FILE_ATTR_SYSTEM) { + printf(" SYSTEM"); + flags &= ~FILE_ATTR_SYSTEM; + } + if (flags & FILE_ATTR_DIRECTORY) { + printf(" DIRECTORY"); + flags &= ~FILE_ATTR_DIRECTORY; + } + if (flags & FILE_ATTR_ARCHIVE) { + printf(" ARCHIVE"); + flags &= ~FILE_ATTR_ARCHIVE; + } + if (flags & FILE_ATTR_DEVICE) { + printf(" DEVICE"); + flags &= ~FILE_ATTR_DEVICE; + } + if (flags & FILE_ATTR_NORMAL) { + printf(" NORMAL"); + flags &= ~FILE_ATTR_NORMAL; + } + if (flags & FILE_ATTR_TEMPORARY) { + printf(" TEMPORARY"); + flags &= ~FILE_ATTR_TEMPORARY; + } + if (flags & FILE_ATTR_SPARSE_FILE) { + printf(" SPARSE_FILE"); + flags &= ~FILE_ATTR_SPARSE_FILE; + } + if (flags & FILE_ATTR_REPARSE_POINT) { + printf(" REPARSE_POINT"); + flags &= ~FILE_ATTR_REPARSE_POINT; + } + if (flags & FILE_ATTR_COMPRESSED) { + printf(" COMPRESSED"); + flags &= ~FILE_ATTR_COMPRESSED; + } + if (flags & FILE_ATTR_OFFLINE) { + printf(" OFFLINE"); + flags &= ~FILE_ATTR_OFFLINE; + } + if (flags & FILE_ATTR_NOT_CONTENT_INDEXED) { + printf(" NOT_CONTENT_INDEXED"); + flags &= ~FILE_ATTR_NOT_CONTENT_INDEXED; + } + if (flags & FILE_ATTR_ENCRYPTED) { + printf(" ENCRYPTED"); + flags &= ~FILE_ATTR_ENCRYPTED; + } + /* We know that FILE_ATTR_I30_INDEX_PRESENT only exists on $FILE_NAME, + and in case we are wrong, let it appear as UNKNOWN */ + if (type == AT_FILE_NAME) { + if (flags & FILE_ATTR_I30_INDEX_PRESENT) { + printf(" I30_INDEX"); + flags &= ~FILE_ATTR_I30_INDEX_PRESENT; + } + } + if (flags & FILE_ATTR_VIEW_INDEX_PRESENT) { + printf(" VIEW_INDEX"); + flags &= ~FILE_ATTR_VIEW_INDEX_PRESENT; + } + if (flags & FILE_ATTRIBUTE_RECALL_ON_OPEN) { + printf(" RECALL_ON_OPEN"); + flags &= ~FILE_ATTRIBUTE_RECALL_ON_OPEN; + } + if (flags) + printf(" UNKNOWN: 0x%08x", (unsigned int)le32_to_cpu(flags)); + /* Print all the flags in hex. */ + printf(" (0x%08x)\n", (unsigned)le32_to_cpu(original_flags)); +} + +/** + * ntfs_dump_namespace + */ +static void ntfs_dump_namespace(const char *indent, u8 file_name_type) +{ + const char *mbs_file_type; + + /* name space */ + switch (file_name_type) { + case FILE_NAME_POSIX: + mbs_file_type = "POSIX"; + break; + case FILE_NAME_WIN32: + mbs_file_type = "Win32"; + break; + case FILE_NAME_DOS: + mbs_file_type = "DOS"; + break; + case FILE_NAME_WIN32_AND_DOS: + mbs_file_type = "Win32 & DOS"; + break; + default: + mbs_file_type = "(unknown)"; + } + printf("%sNamespace:\t\t %s\n", indent, mbs_file_type); +} + +/* *************** functions for dumping attributes ******************** */ +/** + * ntfs_dump_standard_information + */ +static void ntfs_dump_attr_standard_information(ATTR_RECORD *attr) +{ + STANDARD_INFORMATION *standard_attr = NULL; + u32 value_length; + + standard_attr = (STANDARD_INFORMATION*)((char *)attr + + le16_to_cpu(attr->value_offset)); + + /* time conversion stuff */ + if (!opts.notime) { + char *ntfs_time_str = NULL; + + ntfs_time_str = ntfsinfo_time_to_str(standard_attr->creation_time); + printf("\tFile Creation Time:\t %s",ntfs_time_str); + + ntfs_time_str = ntfsinfo_time_to_str( + standard_attr->last_data_change_time); + printf("\tFile Altered Time:\t %s",ntfs_time_str); + + ntfs_time_str = ntfsinfo_time_to_str( + standard_attr->last_mft_change_time); + printf("\tMFT Changed Time:\t %s",ntfs_time_str); + + ntfs_time_str = ntfsinfo_time_to_str(standard_attr->last_access_time); + printf("\tLast Accessed Time:\t %s",ntfs_time_str); + } + ntfs_dump_flags("\t", attr->type, standard_attr->file_attributes); + + value_length = le32_to_cpu(attr->value_length); + if (value_length == 48) { + /* Only 12 reserved bytes here */ + } else if (value_length == 72) { + printf("\tMaximum versions:\t %u \n", (unsigned int) + le32_to_cpu(standard_attr->maximum_versions)); + printf("\tVersion number:\t\t %u \n", (unsigned int) + le32_to_cpu(standard_attr->version_number)); + printf("\tClass ID:\t\t %u \n", + (unsigned int)le32_to_cpu(standard_attr->class_id)); + printf("\tUser ID:\t\t %u (0x%x)\n", + (unsigned int)le32_to_cpu(standard_attr->owner_id), + (unsigned int)le32_to_cpu(standard_attr->owner_id)); + printf("\tSecurity ID:\t\t %u (0x%x)\n", + (unsigned int)le32_to_cpu(standard_attr->security_id), + (unsigned int)le32_to_cpu(standard_attr->security_id)); + printf("\tQuota charged:\t\t %llu (0x%llx)\n", + (unsigned long long) + le64_to_cpu(standard_attr->quota_charged), + (unsigned long long) + le64_to_cpu(standard_attr->quota_charged)); + printf("\tUpdate Sequence Number:\t %llu (0x%llx)\n", + (unsigned long long) + le64_to_cpu(standard_attr->usn), + (unsigned long long) + le64_to_cpu(standard_attr->usn)); + } else { + printf("\tSize of STANDARD_INFORMATION is %u (0x%x). It " + "should be either 72 or 48, something is " + "wrong...\n", (unsigned int)value_length, + (unsigned)value_length); + } +} + +static void ntfs_dump_bytes(u8 *buf, int start, int stop) +{ + int i; + + for (i = start; i < stop; i++) { + printf("%02x ", buf[i]); + } +} + +/** + * ntfs_dump_attr_list() + */ +static void ntfs_dump_attr_list(ATTR_RECORD *attr, ntfs_volume *vol) +{ + ATTR_LIST_ENTRY *entry; + u8 *value; + s64 l; + + if (!opts.verbose) + return; + + l = ntfs_get_attribute_value_length(attr); + if (!l) { + ntfs_log_perror("ntfs_get_attribute_value_length failed"); + return; + } + value = ntfs_malloc(l); + if (!value) + return; + + l = ntfs_get_attribute_value(vol, attr, value); + if (!l) { + ntfs_log_perror("ntfs_get_attribute_value failed"); + free(value); + return; + } + printf("\tDumping attribute list:"); + entry = (ATTR_LIST_ENTRY *) value; + for (;(u8 *)entry < (u8 *) value + l; entry = (ATTR_LIST_ENTRY *) + ((u8 *) entry + le16_to_cpu(entry->length))) { + printf("\n"); + printf("\t\tAttribute type:\t0x%x\n", + (unsigned int)le32_to_cpu(entry->type)); + printf("\t\tRecord length:\t%u (0x%x)\n", + (unsigned)le16_to_cpu(entry->length), + (unsigned)le16_to_cpu(entry->length)); + printf("\t\tName length:\t%u (0x%x)\n", + (unsigned)entry->name_length, + (unsigned)entry->name_length); + printf("\t\tName offset:\t%u (0x%x)\n", + (unsigned)entry->name_offset, + (unsigned)entry->name_offset); + printf("\t\tStarting VCN:\t%lld (0x%llx)\n", + (long long)sle64_to_cpu(entry->lowest_vcn), + (unsigned long long) + sle64_to_cpu(entry->lowest_vcn)); + printf("\t\tMFT reference:\t%lld (0x%llx)\n", + (unsigned long long) + MREF_LE(entry->mft_reference), + (unsigned long long) + MREF_LE(entry->mft_reference)); + printf("\t\tInstance:\t%u (0x%x)\n", + (unsigned)le16_to_cpu(entry->instance), + (unsigned)le16_to_cpu(entry->instance)); + printf("\t\tName:\t\t"); + if (entry->name_length) { + char *name = NULL; + int name_size; + + name_size = ntfs_ucstombs(entry->name, + entry->name_length, &name, 0); + + if (name_size > 0) { + printf("%s\n", name); + free(name); + } else + ntfs_log_perror("ntfs_ucstombs failed"); + } else + printf("unnamed\n"); + printf("\t\tPadding:\t"); + ntfs_dump_bytes((u8 *)entry, entry->name_offset + + sizeof(ntfschar) * entry->name_length, + le16_to_cpu(entry->length)); + printf("\n"); + } + free(value); + printf("\tEnd of attribute list reached.\n"); +} + +/** + * ntfs_dump_filename() + */ +static void ntfs_dump_filename(const char *indent, + FILE_NAME_ATTR *file_name_attr) +{ + le32 tag; + + printf("%sParent directory:\t %lld (0x%llx)\n", indent, + (long long)MREF_LE(file_name_attr->parent_directory), + (long long)MREF_LE(file_name_attr->parent_directory)); + /* time stuff */ + if (!opts.notime) { + char *ntfs_time_str; + + ntfs_time_str = ntfsinfo_time_to_str( + file_name_attr->creation_time); + printf("%sFile Creation Time:\t %s", indent, ntfs_time_str); + + ntfs_time_str = ntfsinfo_time_to_str( + file_name_attr->last_data_change_time); + printf("%sFile Altered Time:\t %s", indent, ntfs_time_str); + + ntfs_time_str = ntfsinfo_time_to_str( + file_name_attr->last_mft_change_time); + printf("%sMFT Changed Time:\t %s", indent, ntfs_time_str); + + ntfs_time_str = ntfsinfo_time_to_str( + file_name_attr->last_access_time); + printf("%sLast Accessed Time:\t %s", indent, ntfs_time_str); + } + /* other basic stuff about the file */ + printf("%sAllocated Size:\t\t %lld (0x%llx)\n", indent, (long long) + sle64_to_cpu(file_name_attr->allocated_size), + (unsigned long long) + sle64_to_cpu(file_name_attr->allocated_size)); + printf("%sData Size:\t\t %lld (0x%llx)\n", indent, + (long long)sle64_to_cpu(file_name_attr->data_size), + (unsigned long long) + sle64_to_cpu(file_name_attr->data_size)); + printf("%sFilename Length:\t %d (0x%x)\n", indent, + (unsigned)file_name_attr->file_name_length, + (unsigned)file_name_attr->file_name_length); + ntfs_dump_flags(indent, AT_FILE_NAME, file_name_attr->file_attributes); + if (file_name_attr->file_attributes & FILE_ATTR_REPARSE_POINT && + file_name_attr->reparse_point_tag) { + tag = file_name_attr->reparse_point_tag; + printf("%sReparse point tag:\t 0x%08lx%s\n", indent, + (long)le32_to_cpu(tag), + reparse_type_name(tag)); + } else if (file_name_attr->reparse_point_tag) { + printf("%sEA Length:\t\t %d (0x%x)\n", indent, (unsigned) + le16_to_cpu(file_name_attr->packed_ea_size), + (unsigned) + le16_to_cpu(file_name_attr->packed_ea_size)); + if (file_name_attr->reserved) + printf("%sReserved:\t\t %d (0x%x)\n", indent, + (unsigned) + le16_to_cpu(file_name_attr->reserved), + (unsigned) + le16_to_cpu(file_name_attr->reserved)); + } + /* The filename. */ + ntfs_dump_namespace(indent, file_name_attr->file_name_type); + if (file_name_attr->file_name_length > 0) { + /* but first we need to convert the little endian unicode string + into a printable format */ + char *mbs_file_name = NULL; + int mbs_file_name_size; + + mbs_file_name_size = ntfs_ucstombs(file_name_attr->file_name, + file_name_attr->file_name_length,&mbs_file_name,0); + + if (mbs_file_name_size>0) { + printf("%sFilename:\t\t '%s'\n", indent, mbs_file_name); + free(mbs_file_name); + } else { + /* an error occurred, errno holds the reason - notify the user */ + ntfs_log_perror("ntfsinfo error: could not parse file name"); + } + } else { + printf("%sFile Name:\t\t unnamed?!?\n", indent); + } +} + +/** + * ntfs_dump_attr_file_name() + */ +static void ntfs_dump_attr_file_name(ATTR_RECORD *attr) +{ + ntfs_dump_filename("\t", (FILE_NAME_ATTR*)((u8*)attr + + le16_to_cpu(attr->value_offset))); +} + +/** + * ntfs_dump_object_id + * + * dump the $OBJECT_ID attribute - not present on all systems + */ +static void ntfs_dump_attr_object_id(ATTR_RECORD *attr,ntfs_volume *vol) +{ + OBJECT_ID_ATTR *obj_id_attr = NULL; + + obj_id_attr = (OBJECT_ID_ATTR *)((u8*)attr + + le16_to_cpu(attr->value_offset)); + + if (vol->major_ver >= 3.0) { + u32 value_length; + char printable_GUID[37]; + + value_length = le32_to_cpu(attr->value_length); + + /* Object ID is mandatory. */ + ntfs_guid_to_mbs(&obj_id_attr->object_id, printable_GUID); + printf("\tObject ID:\t\t %s\n", printable_GUID); + + /* Dump Birth Volume ID. */ + if ((value_length > sizeof(GUID)) && !ntfs_guid_is_zero( + &obj_id_attr->birth_volume_id)) { + ntfs_guid_to_mbs(&obj_id_attr->birth_volume_id, + printable_GUID); + printf("\tBirth Volume ID:\t\t %s\n", printable_GUID); + } else + printf("\tBirth Volume ID:\t missing\n"); + + /* Dumping Birth Object ID */ + if ((value_length > sizeof(GUID)) && !ntfs_guid_is_zero( + &obj_id_attr->birth_object_id)) { + ntfs_guid_to_mbs(&obj_id_attr->birth_object_id, + printable_GUID); + printf("\tBirth Object ID:\t\t %s\n", printable_GUID); + } else + printf("\tBirth Object ID:\t missing\n"); + + /* Dumping Domain_id - reserved for now */ + if ((value_length > sizeof(GUID)) && !ntfs_guid_is_zero( + &obj_id_attr->domain_id)) { + ntfs_guid_to_mbs(&obj_id_attr->domain_id, + printable_GUID); + printf("\tDomain ID:\t\t\t %s\n", printable_GUID); + } else + printf("\tDomain ID:\t\t missing\n"); + } else + printf("\t$OBJECT_ID not present. Only NTFS versions > 3.0\n" + "\thave $OBJECT_ID. Your version of NTFS is %d.\n", + vol->major_ver); +} + +/** + * ntfs_dump_acl + * + * given an acl, print it in a beautiful & lovely way. + */ +static void ntfs_dump_acl(const char *prefix, ACL *acl) +{ + unsigned int i; + u16 ace_count; + ACCESS_ALLOWED_ACE *ace; + + printf("%sRevision\t %u\n", prefix, acl->revision); + + /* + * Do not recalculate le16_to_cpu every iteration (minor speedup on + * big-endian machines. + */ + ace_count = le16_to_cpu(acl->ace_count); + + /* initialize 'ace' to the first ace (if any) */ + ace = (ACCESS_ALLOWED_ACE *)((char *)acl + 8); + + /* iterate through ACE's */ + for (i = 1; i <= ace_count; i++) { + const char *ace_type; + char *sid; + + /* set ace_type. */ + switch (ace->type) { + case ACCESS_ALLOWED_ACE_TYPE: + ace_type = "allow"; + break; + case ACCESS_DENIED_ACE_TYPE: + ace_type = "deny"; + break; + case SYSTEM_AUDIT_ACE_TYPE: + ace_type = "audit"; + break; + default: + ace_type = "unknown"; + break; + } + + printf("%sACE:\t\t type:%s flags:0x%x access:0x%x\n", prefix, + ace_type, (unsigned int)ace->flags, + (unsigned int)le32_to_cpu(ace->mask)); + /* get a SID string */ + sid = ntfs_sid_to_mbs(&ace->sid, NULL, 0); + printf("%s\t\t SID: %s\n", prefix, sid); + free(sid); + + /* proceed to next ACE */ + ace = (ACCESS_ALLOWED_ACE *)(((char *)ace) + + le16_to_cpu(ace->size)); + } +} + + +static void ntfs_dump_security_descriptor(SECURITY_DESCRIPTOR_ATTR *sec_desc, + const char *indent) +{ + char *sid; + + printf("%s\tRevision:\t\t %u\n", indent, sec_desc->revision); + + /* TODO: parse the flags */ + printf("%s\tControl:\t\t 0x%04x\n", indent, + le16_to_cpu(sec_desc->control)); + + if (~sec_desc->control & SE_SELF_RELATIVE) { + SECURITY_DESCRIPTOR *sd = (SECURITY_DESCRIPTOR *)sec_desc; + + printf("%s\tOwner SID pointer:\t %p\n", indent, sd->owner); + printf("%s\tGroup SID pointer:\t %p\n", indent, sd->group); + printf("%s\tSACL pointer:\t\t %p\n", indent, sd->sacl); + printf("%s\tDACL pointer:\t\t %p\n", indent, sd->dacl); + + return; + } + + if (sec_desc->owner) { + sid = ntfs_sid_to_mbs((SID *)((char *)sec_desc + + le32_to_cpu(sec_desc->owner)), NULL, 0); + printf("%s\tOwner SID:\t\t %s\n", indent, sid); + free(sid); + } else + printf("%s\tOwner SID:\t\t missing\n", indent); + + if (sec_desc->group) { + sid = ntfs_sid_to_mbs((SID *)((char *)sec_desc + + le32_to_cpu(sec_desc->group)), NULL, 0); + printf("%s\tGroup SID:\t\t %s\n", indent, sid); + free(sid); + } else + printf("%s\tGroup SID:\t\t missing\n", indent); + + printf("%s\tSystem ACL:\t\t ", indent); + if (sec_desc->control & SE_SACL_PRESENT) { + if (sec_desc->control & SE_SACL_DEFAULTED) { + printf("defaulted"); + } + printf("\n"); + ntfs_dump_acl(indent ? "\t\t\t" : "\t\t", + (ACL *)((char *)sec_desc + + le32_to_cpu(sec_desc->sacl))); + } else { + printf("missing\n"); + } + + printf("%s\tDiscretionary ACL:\t ", indent); + if (sec_desc->control & SE_DACL_PRESENT) { + if (sec_desc->control & SE_SACL_DEFAULTED) { + printf("defaulted"); + } + printf("\n"); + ntfs_dump_acl(indent ? "\t\t\t" : "\t\t", + (ACL *)((char *)sec_desc + + le32_to_cpu(sec_desc->dacl))); + } else { + printf("missing\n"); + } +} + +/** + * ntfs_dump_security_descriptor() + * + * dump the security information about the file + */ +static void ntfs_dump_attr_security_descriptor(ATTR_RECORD *attr, ntfs_volume *vol) +{ + SECURITY_DESCRIPTOR_ATTR *sec_desc_attr; + + if (attr->non_resident) { + /* FIXME: We don't handle fragmented mapping pairs case. */ + runlist *rl = ntfs_mapping_pairs_decompress(vol, attr, NULL); + if (rl) { + s64 data_size, bytes_read; + + data_size = sle64_to_cpu(attr->data_size); + sec_desc_attr = ntfs_malloc(data_size); + if (!sec_desc_attr) { + free(rl); + return; + } + bytes_read = ntfs_rl_pread(vol, rl, 0, + data_size, sec_desc_attr); + if (bytes_read != data_size) { + ntfs_log_error("ntfsinfo error: could not " + "read security descriptor\n"); + free(rl); + free(sec_desc_attr); + return; + } + free(rl); + } else { + ntfs_log_error("ntfsinfo error: could not " + "decompress runlist\n"); + return; + } + } else { + sec_desc_attr = (SECURITY_DESCRIPTOR_ATTR *)((u8*)attr + + le16_to_cpu(attr->value_offset)); + } + + ntfs_dump_security_descriptor(sec_desc_attr, ""); + + if (attr->non_resident) + free(sec_desc_attr); +} + +/** + * ntfs_dump_volume_name() + * + * dump the name of the volume the inode belongs to + */ +static void ntfs_dump_attr_volume_name(ATTR_RECORD *attr) +{ + ntfschar *ucs_vol_name = NULL; + + if (le32_to_cpu(attr->value_length) > 0) { + char *mbs_vol_name = NULL; + int mbs_vol_name_size; + /* calculate volume name position */ + ucs_vol_name = (ntfschar*)((u8*)attr + + le16_to_cpu(attr->value_offset)); + /* convert the name to current locale multibyte sequence */ + mbs_vol_name_size = ntfs_ucstombs(ucs_vol_name, + le32_to_cpu(attr->value_length) / + sizeof(ntfschar), &mbs_vol_name, 0); + + if (mbs_vol_name_size>0) { + /* output the converted name. */ + printf("\tVolume Name:\t\t '%s'\n", mbs_vol_name); + free(mbs_vol_name); + } else + ntfs_log_perror("ntfsinfo error: could not parse " + "volume name"); + } else + printf("\tVolume Name:\t\t unnamed\n"); +} + +/** + * ntfs_dump_volume_information() + * + * dump the information for the volume the inode belongs to + * + */ +static void ntfs_dump_attr_volume_information(ATTR_RECORD *attr) +{ + VOLUME_INFORMATION *vol_information = NULL; + + vol_information = (VOLUME_INFORMATION*)((char *)attr+ + le16_to_cpu(attr->value_offset)); + + printf("\tVolume Version:\t\t %d.%d\n", vol_information->major_ver, + vol_information->minor_ver); + printf("\tVolume Flags:\t\t "); + if (vol_information->flags & VOLUME_IS_DIRTY) + printf("DIRTY "); + if (vol_information->flags & VOLUME_RESIZE_LOG_FILE) + printf("RESIZE_LOG "); + if (vol_information->flags & VOLUME_UPGRADE_ON_MOUNT) + printf("UPG_ON_MOUNT "); + if (vol_information->flags & VOLUME_MOUNTED_ON_NT4) + printf("MOUNTED_NT4 "); + if (vol_information->flags & VOLUME_DELETE_USN_UNDERWAY) + printf("DEL_USN "); + if (vol_information->flags & VOLUME_REPAIR_OBJECT_ID) + printf("REPAIR_OBJID "); + if (vol_information->flags & VOLUME_CHKDSK_UNDERWAY) + printf("CHKDSK_UNDERWAY "); + if (vol_information->flags & VOLUME_MODIFIED_BY_CHKDSK) + printf("MOD_BY_CHKDSK "); + if (vol_information->flags & VOLUME_FLAGS_MASK) { + printf("(0x%04x)\n", + (unsigned)le16_to_cpu(vol_information->flags)); + } else + printf("none set (0x0000)\n"); + if (vol_information->flags & (~VOLUME_FLAGS_MASK)) + printf("\t\t\t\t Unknown Flags: 0x%04x\n", + le16_to_cpu(vol_information->flags & + (~VOLUME_FLAGS_MASK))); +} + +static ntfschar NTFS_DATA_SDS[5] = { const_cpu_to_le16('$'), + const_cpu_to_le16('S'), const_cpu_to_le16('D'), + const_cpu_to_le16('S'), const_cpu_to_le16('\0') }; + +static void ntfs_dump_sds_entry(SECURITY_DESCRIPTOR_HEADER *sds) +{ + SECURITY_DESCRIPTOR_RELATIVE *sd; + + ntfs_log_verbose("\n"); + ntfs_log_verbose("\t\tHash:\t\t\t 0x%08x\n", + (unsigned)le32_to_cpu(sds->hash)); + ntfs_log_verbose("\t\tSecurity id:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(sds->security_id), + (unsigned)le32_to_cpu(sds->security_id)); + ntfs_log_verbose("\t\tOffset:\t\t\t %llu (0x%llx)\n", + (unsigned long long)le64_to_cpu(sds->offset), + (unsigned long long)le64_to_cpu(sds->offset)); + ntfs_log_verbose("\t\tLength:\t\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(sds->length), + (unsigned)le32_to_cpu(sds->length)); + + sd = (SECURITY_DESCRIPTOR_RELATIVE *)((char *)sds + + sizeof(SECURITY_DESCRIPTOR_HEADER)); + + ntfs_dump_security_descriptor(sd, "\t"); +} + +static void ntfs_dump_sds_records(ntfs_attr *na, ntfs_index_context *xsii) +{ + SII_INDEX_DATA sii_data; + SECURITY_DESCRIPTOR_HEADER *sd; + INDEX_ENTRY *entry; + s64 offs; + u32 size; + u32 max_size; + + max_size = 0; + sd = (SECURITY_DESCRIPTOR_HEADER*)NULL; + entry = xsii->entry; + + if (entry) { + do { + /* Copy to get the offset properly aligned */ + memcpy(&sii_data, ((char*)&entry->key.sii) + + sizeof(entry->key.sii), sizeof(sii_data)); + offs = le64_to_cpu(sii_data.offset); + size = le32_to_cpu(sii_data.length); + if (size > max_size) { + sd = (SECURITY_DESCRIPTOR_HEADER*) + realloc(sd, size); + max_size = size; + } + if (sd) { + if (ntfs_attr_pread(na, offs, size, sd) + == size) { + ntfs_dump_sds_entry(sd); + } else { + ntfs_log_perror( + "Failed to read a $SDS entry"); + } + } else + ntfs_log_error( + "Failed to allocate a $SDS entry\n"); + entry = ntfs_index_next(entry, xsii); + } while (entry && sd); + } else { + ntfs_log_perror("Could not get any $SII entry"); + } + free(sd); +} + + +static void ntfs_dump_sds(ATTR_RECORD *attr, ntfs_inode *ni) +{ + ntfs_index_context *xsii; + ntfschar *name; + int name_len; + le32 keyid; + ntfs_attr *na; + u64 inode; + + inode = ni->mft_no; + if (ni->nr_extents < 0) + inode = ni->base_ni->mft_no; + if (FILE_Secure != inode) + return; + + name_len = attr->name_length; + if (!name_len) + return; + + name = (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset)); + if (!ntfs_names_are_equal(NTFS_DATA_SDS, sizeof(NTFS_DATA_SDS) / 2 - 1, + name, name_len, CASE_SENSITIVE, NULL, 0)) + return; + + /* Open the $SDS data attribute */ + na = ntfs_attr_open(ni, AT_DATA, name, name_len); + if (!na) { + ntfs_log_perror("Failed to open the $SDS data attribute"); + return; + } + + /* Use the $SII index to locate the records ordered by ids */ + xsii = ntfs_index_ctx_get(ni, NTFS_INDEX_SII, 4); + if (!xsii) { + ntfs_log_perror("Failed to allocate the $SII index"); + } else { + /* Search for the first entry */ + keyid = const_cpu_to_le32(0); + if (!ntfs_index_lookup((char*)&keyid, + sizeof(SII_INDEX_KEY), xsii)) { + ntfs_log_perror("Failed to open the $SII index"); + } else { + ntfs_dump_sds_records(na, xsii); + } + ntfs_index_ctx_put(xsii); + } + ntfs_attr_close(na); +} + +static const char *get_attribute_type_name(le32 type) +{ + switch (type) { + case AT_UNUSED: return "$UNUSED"; + case AT_STANDARD_INFORMATION: return "$STANDARD_INFORMATION"; + case AT_ATTRIBUTE_LIST: return "$ATTRIBUTE_LIST"; + case AT_FILE_NAME: return "$FILE_NAME"; + case AT_OBJECT_ID: return "$OBJECT_ID"; + case AT_SECURITY_DESCRIPTOR: return "$SECURITY_DESCRIPTOR"; + case AT_VOLUME_NAME: return "$VOLUME_NAME"; + case AT_VOLUME_INFORMATION: return "$VOLUME_INFORMATION"; + case AT_DATA: return "$DATA"; + case AT_INDEX_ROOT: return "$INDEX_ROOT"; + case AT_INDEX_ALLOCATION: return "$INDEX_ALLOCATION"; + case AT_BITMAP: return "$BITMAP"; + case AT_REPARSE_POINT: return "$REPARSE_POINT"; + case AT_EA_INFORMATION: return "$EA_INFORMATION"; + case AT_EA: return "$EA"; + case AT_PROPERTY_SET: return "$PROPERTY_SET"; + case AT_LOGGED_UTILITY_STREAM: return "$LOGGED_UTILITY_STREAM"; + case AT_END: return "$END"; + } + + return "$UNKNOWN"; +} + +static const char * ntfs_dump_lcn(LCN lcn) +{ + switch (lcn) { + case LCN_HOLE: + return "\t"; + case LCN_RL_NOT_MAPPED: + return ""; + case LCN_ENOENT: + return "\t"; + case LCN_EINVAL: + return "\t"; + case LCN_EIO: + return "\t"; + default: + ntfs_log_error("Invalid LCN value %llx passed to " + "ntfs_dump_lcn().\n", (long long)lcn); + return "???\t"; + } +} + +static void ntfs_dump_attribute_header(ntfs_attr_search_ctx *ctx, + ntfs_volume *vol, struct RUNCOUNT *runcount) +{ + ATTR_RECORD *a = ctx->attr; + + printf("Dumping attribute %s (0x%x) from mft record %lld (0x%llx)\n", + get_attribute_type_name(a->type), + (unsigned)le32_to_cpu(a->type), + (unsigned long long)ctx->ntfs_ino->mft_no, + (unsigned long long)ctx->ntfs_ino->mft_no); + + ntfs_log_verbose("\tAttribute length:\t %u (0x%x)\n", + (unsigned)le32_to_cpu(a->length), + (unsigned)le32_to_cpu(a->length)); + printf("\tResident: \t\t %s\n", a->non_resident ? "No" : "Yes"); + ntfs_log_verbose("\tName length:\t\t %u (0x%x)\n", + (unsigned)a->name_length, (unsigned)a->name_length); + ntfs_log_verbose("\tName offset:\t\t %u (0x%x)\n", + (unsigned)le16_to_cpu(a->name_offset), + (unsigned)le16_to_cpu(a->name_offset)); + + /* Dump the attribute (stream) name */ + if (a->name_length) { + char *attribute_name = NULL; + + attribute_name = ntfs_attr_get_name_mbs(a); + if (attribute_name) { + printf("\tAttribute name:\t\t '%s'\n", attribute_name); + free(attribute_name); + } else + ntfs_log_perror("Error: couldn't parse attribute name"); + } + + /* TODO: parse the flags */ + printf("\tAttribute flags:\t 0x%04x\n", + (unsigned)le16_to_cpu(a->flags)); + printf("\tAttribute instance:\t %u (0x%x)\n", + (unsigned)le16_to_cpu(a->instance), + (unsigned)le16_to_cpu(a->instance)); + + /* Resident attribute */ + if (!a->non_resident) { + printf("\tData size:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(a->value_length), + (unsigned)le32_to_cpu(a->value_length)); + ntfs_log_verbose("\tData offset:\t\t %u (0x%x)\n", + (unsigned)le16_to_cpu(a->value_offset), + (unsigned)le16_to_cpu(a->value_offset)); + /* TODO: parse the flags */ + printf("\tResident flags:\t\t 0x%02x\n", + (unsigned)a->resident_flags); + ntfs_log_verbose("\tReservedR:\t\t %d (0x%x)\n", + (unsigned)a->reservedR, (unsigned)a->reservedR); + return; + } + + /* Non-resident attribute */ + ntfs_log_verbose("\tLowest VCN\t\t %lld (0x%llx)\n", + (long long)sle64_to_cpu(a->lowest_vcn), + (unsigned long long)sle64_to_cpu(a->lowest_vcn)); + ntfs_log_verbose("\tHighest VCN:\t\t %lld (0x%llx)\n", + (long long)sle64_to_cpu(a->highest_vcn), + (unsigned long long)sle64_to_cpu(a->highest_vcn)); + ntfs_log_verbose("\tMapping pairs offset:\t %u (0x%x)\n", + (unsigned)le16_to_cpu(a->mapping_pairs_offset), + (unsigned)le16_to_cpu(a->mapping_pairs_offset)); + printf("\tCompression unit:\t %u (0x%x)\n", + (unsigned)a->compression_unit, + (unsigned)a->compression_unit); + /* TODO: dump the 5 reserved bytes here in verbose mode */ + + if (!a->lowest_vcn) { + printf("\tData size:\t\t %llu (0x%llx)\n", + (long long)sle64_to_cpu(a->data_size), + (unsigned long long)sle64_to_cpu(a->data_size)); + printf("\tAllocated size:\t\t %llu (0x%llx)\n", + (long long)sle64_to_cpu(a->allocated_size), + (unsigned long long) + sle64_to_cpu(a->allocated_size)); + printf("\tInitialized size:\t %llu (0x%llx)\n", + (long long)sle64_to_cpu(a->initialized_size), + (unsigned long long) + sle64_to_cpu(a->initialized_size)); + if (a->compression_unit || a->flags & ATTR_IS_COMPRESSED || + a->flags & ATTR_IS_SPARSE) + printf("\tCompressed size:\t %llu (0x%llx)\n", + (signed long long) + sle64_to_cpu(a->compressed_size), + (signed long long) + sle64_to_cpu(a->compressed_size)); + } + + if (opts.verbose) { + runlist *rl; + + rl = ntfs_mapping_pairs_decompress(vol, a, NULL); + if (rl) { + runlist *rlc = rl; + LCN next_lcn; + + next_lcn = LCN_HOLE; + // TODO: Switch this to properly aligned hex... + printf("\tRunlist:\tVCN\t\tLCN\t\tLength\n"); + runcount->fragments++; + while (rlc->length) { + runcount->runs++; + if (rlc->lcn >= 0) { + printf("\t\t\t0x%llx\t\t0x%llx\t\t" + "0x%llx\n", + (long long)rlc->vcn, + (long long)rlc->lcn, + (long long)rlc->length); + if ((next_lcn >= 0) + && (rlc->lcn != next_lcn)) + runcount->fragments++; + next_lcn = rlc->lcn + rlc->length; + } else + printf("\t\t\t0x%llx\t\t%s\t" + "0x%llx\n", + (long long)rlc->vcn, + ntfs_dump_lcn(rlc->lcn), + (long long)rlc->length); + rlc++; + } + free(rl); + } else + ntfs_log_error("Error: couldn't decompress runlist\n"); + } +} + +/** + * ntfs_dump_data_attr() + * + * dump some info about the data attribute if it's metadata + */ +static void ntfs_dump_attr_data(ATTR_RECORD *attr, ntfs_inode *ni) +{ + if (opts.verbose) { + switch (ni->mft_no) { + case FILE_Secure : + ntfs_dump_sds(attr, ni); + break; + case FILE_LogFile : + ntfs_dump_logfile(ni); + break; + default : + break; + } + } +} + +typedef enum { + INDEX_ATTR_UNKNOWN, + INDEX_ATTR_DIRECTORY_I30, + INDEX_ATTR_SECURE_SII, + INDEX_ATTR_SECURE_SDH, + INDEX_ATTR_OBJID_O, + INDEX_ATTR_REPARSE_R, + INDEX_ATTR_QUOTA_O, + INDEX_ATTR_QUOTA_Q, +} INDEX_ATTR_TYPE; + +static void ntfs_dump_index_key(INDEX_ENTRY *entry, INDEX_ATTR_TYPE type) +{ + char *sid; + char printable_GUID[37]; + le32 tag; + + switch (type) { + case INDEX_ATTR_SECURE_SII: + ntfs_log_verbose("\t\tKey security id:\t %u (0x%x)\n", + (unsigned) + le32_to_cpu(entry->key.sii.security_id), + (unsigned) + le32_to_cpu(entry->key.sii.security_id)); + break; + case INDEX_ATTR_SECURE_SDH: + ntfs_log_verbose("\t\tKey hash:\t\t 0x%08x\n", + (unsigned)le32_to_cpu(entry->key.sdh.hash)); + ntfs_log_verbose("\t\tKey security id:\t %u (0x%x)\n", + (unsigned) + le32_to_cpu(entry->key.sdh.security_id), + (unsigned) + le32_to_cpu(entry->key.sdh.security_id)); + break; + case INDEX_ATTR_OBJID_O: + ntfs_guid_to_mbs(&entry->key.object_id, printable_GUID); + ntfs_log_verbose("\t\tKey GUID:\t\t %s\n", printable_GUID); + break; + case INDEX_ATTR_REPARSE_R: + tag = entry->key.reparse.reparse_tag; + ntfs_log_verbose("\t\tKey reparse tag:\t 0x%08lx%s\n", + (long)le32_to_cpu(tag), + reparse_type_name(tag)); + ntfs_log_verbose("\t\tKey file id:\t\t %llu (0x%llx)\n", + (unsigned long long) + MREF_LE(entry->key.reparse.file_id), + (unsigned long long) + le64_to_cpu(entry->key.reparse.file_id)); + break; + case INDEX_ATTR_QUOTA_O: + sid = ntfs_sid_to_mbs(&entry->key.sid, NULL, 0); + ntfs_log_verbose("\t\tKey SID:\t\t %s\n", sid); + free(sid); + break; + case INDEX_ATTR_QUOTA_Q: + ntfs_log_verbose("\t\tKey owner id:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(entry->key.owner_id), + (unsigned)le32_to_cpu(entry->key.owner_id)); + break; + default: + ntfs_log_verbose("\t\tIndex attr type is UNKNOWN: \t 0x%08x\n", + (unsigned)type); + break; + } +} + +typedef union { + SII_INDEX_DATA sii; /* $SII index data in $Secure */ + SDH_INDEX_DATA sdh; /* $SDH index data in $Secure */ + QUOTA_O_INDEX_DATA quota_o; /* $O index data in $Quota */ + QUOTA_CONTROL_ENTRY quota_q; /* $Q index data in $Quota */ +} __attribute__((__packed__)) INDEX_ENTRY_DATA; + +static void ntfs_dump_index_data(INDEX_ENTRY *entry, INDEX_ATTR_TYPE type) +{ + INDEX_ENTRY_DATA *data; + + data = (INDEX_ENTRY_DATA *)((u8 *)entry + + le16_to_cpu(entry->data_offset)); + + switch (type) { + case INDEX_ATTR_SECURE_SII: + ntfs_log_verbose("\t\tHash:\t\t\t 0x%08x\n", + (unsigned)le32_to_cpu(data->sii.hash)); + ntfs_log_verbose("\t\tSecurity id:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(data->sii.security_id), + (unsigned)le32_to_cpu(data->sii.security_id)); + ntfs_log_verbose("\t\tOffset in $SDS:\t\t %llu (0x%llx)\n", + (unsigned long long) + le64_to_cpu(data->sii.offset), + (unsigned long long) + le64_to_cpu(data->sii.offset)); + ntfs_log_verbose("\t\tLength in $SDS:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(data->sii.length), + (unsigned)le32_to_cpu(data->sii.length)); + break; + case INDEX_ATTR_SECURE_SDH: + ntfs_log_verbose("\t\tHash:\t\t\t 0x%08x\n", + (unsigned)le32_to_cpu(data->sdh.hash)); + ntfs_log_verbose("\t\tSecurity id:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(data->sdh.security_id), + (unsigned)le32_to_cpu(data->sdh.security_id)); + ntfs_log_verbose("\t\tOffset in $SDS:\t\t %llu (0x%llx)\n", + (unsigned long long) + le64_to_cpu(data->sdh.offset), + (unsigned long long) + le64_to_cpu(data->sdh.offset)); + ntfs_log_verbose("\t\tLength in $SDS:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(data->sdh.length), + (unsigned)le32_to_cpu(data->sdh.length)); + ntfs_log_verbose("\t\tUnknown (padding):\t 0x%08x\n", + (unsigned)le32_to_cpu(data->sdh.reserved_II)); + break; + case INDEX_ATTR_OBJID_O: { + OBJ_ID_INDEX_DATA *object_id_data; + char printable_GUID[37]; + + object_id_data = (OBJ_ID_INDEX_DATA*)((u8*)entry + + le16_to_cpu(entry->data_offset)); + ntfs_log_verbose("\t\tMFT Number:\t\t 0x%llx\n", + (unsigned long long) + MREF_LE(object_id_data->mft_reference)); + ntfs_log_verbose("\t\tMFT Sequence Number:\t 0x%x\n", + (unsigned) + MSEQNO_LE(object_id_data->mft_reference)); + ntfs_guid_to_mbs(&object_id_data->birth_volume_id, + printable_GUID); + ntfs_log_verbose("\t\tBirth volume id GUID:\t %s\n", + printable_GUID); + ntfs_guid_to_mbs(&object_id_data->birth_object_id, + printable_GUID); + ntfs_log_verbose("\t\tBirth object id GUID:\t %s\n", + printable_GUID); + ntfs_guid_to_mbs(&object_id_data->domain_id, printable_GUID); + ntfs_log_verbose("\t\tDomain id GUID:\t\t %s\n", + printable_GUID); + } + break; + case INDEX_ATTR_REPARSE_R: + /* TODO */ + break; + case INDEX_ATTR_QUOTA_O: + ntfs_log_verbose("\t\tOwner id:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(data->quota_o.owner_id), + (unsigned)le32_to_cpu(data->quota_o.owner_id)); + ntfs_log_verbose("\t\tUnknown:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(data->quota_o.unknown), + (unsigned)le32_to_cpu(data->quota_o.unknown)); + break; + case INDEX_ATTR_QUOTA_Q: + ntfs_log_verbose("\t\tVersion:\t\t %u\n", + (unsigned)le32_to_cpu(data->quota_q.version)); + ntfs_log_verbose("\t\tQuota flags:\t\t 0x%08x\n", + (unsigned)le32_to_cpu(data->quota_q.flags)); + ntfs_log_verbose("\t\tBytes used:\t\t %llu (0x%llx)\n", + (unsigned long long) + le64_to_cpu(data->quota_q.bytes_used), + (unsigned long long) + le64_to_cpu(data->quota_q.bytes_used)); + ntfs_log_verbose("\t\tLast changed:\t\t %s", + ntfsinfo_time_to_str( + data->quota_q.change_time)); + ntfs_log_verbose("\t\tThreshold:\t\t %lld (0x%llx)\n", + (unsigned long long) + sle64_to_cpu(data->quota_q.threshold), + (unsigned long long) + sle64_to_cpu(data->quota_q.threshold)); + ntfs_log_verbose("\t\tLimit:\t\t\t %lld (0x%llx)\n", + (unsigned long long) + sle64_to_cpu(data->quota_q.limit), + (unsigned long long) + sle64_to_cpu(data->quota_q.limit)); + ntfs_log_verbose("\t\tExceeded time:\t\t %lld (0x%llx)\n", + (unsigned long long) + sle64_to_cpu(data->quota_q.exceeded_time), + (unsigned long long) + sle64_to_cpu(data->quota_q.exceeded_time)); + if (le16_to_cpu(entry->data_length) > 48) { + char *sid; + sid = ntfs_sid_to_mbs(&data->quota_q.sid, NULL, 0); + ntfs_log_verbose("\t\tOwner SID:\t\t %s\n", sid); + free(sid); + } + break; + default: + ntfs_log_verbose("\t\tIndex attr type is UNKNOWN: \t 0x%08x\n", + (unsigned)type); + break; + } +} + +/** + * ntfs_dump_index_entries() + * + * dump sequence of index_entries and return number of entries dumped. + */ +static int ntfs_dump_index_entries(INDEX_ENTRY *entry, INDEX_ATTR_TYPE type) +{ + int numb_entries = 1; + while (1) { + if (!opts.verbose) { + if (entry->ie_flags & INDEX_ENTRY_END) + break; + entry = (INDEX_ENTRY *)((u8 *)entry + + le16_to_cpu(entry->length)); + numb_entries++; + continue; + } + ntfs_log_verbose("\t\tEntry length:\t\t %u (0x%x)\n", + (unsigned)le16_to_cpu(entry->length), + (unsigned)le16_to_cpu(entry->length)); + ntfs_log_verbose("\t\tKey length:\t\t %u (0x%x)\n", + (unsigned)le16_to_cpu(entry->key_length), + (unsigned)le16_to_cpu(entry->key_length)); + ntfs_log_verbose("\t\tIndex entry flags:\t 0x%02x\n", + (unsigned)le16_to_cpu(entry->ie_flags)); + + if (entry->ie_flags & INDEX_ENTRY_NODE) + ntfs_log_verbose("\t\tSubnode VCN:\t\t %lld (0x%llx)\n", + (long long)ntfs_ie_get_vcn(entry), + (long long)ntfs_ie_get_vcn(entry)); + if (entry->ie_flags & INDEX_ENTRY_END) + break; + + switch (type) { + case INDEX_ATTR_DIRECTORY_I30: + ntfs_log_verbose("\t\tFILE record number:\t %llu " + "(0x%llx)\n", (unsigned long long) + MREF_LE(entry->indexed_file), + (unsigned long long) + MREF_LE(entry->indexed_file)); + ntfs_dump_filename("\t\t", &entry->key.file_name); + break; + default: + ntfs_log_verbose("\t\tData offset:\t\t %u (0x%x)\n", + (unsigned) + le16_to_cpu(entry->data_offset), + (unsigned) + le16_to_cpu(entry->data_offset)); + ntfs_log_verbose("\t\tData length:\t\t %u (0x%x)\n", + (unsigned) + le16_to_cpu(entry->data_length), + (unsigned) + le16_to_cpu(entry->data_length)); + ntfs_dump_index_key(entry, type); + ntfs_log_verbose("\t\tKey Data:\n"); + ntfs_dump_index_data(entry, type); + break; + } + if (!entry->length) { + ntfs_log_verbose("\tWARNING: Corrupt index entry, " + "skipping the remainder of this index " + "block.\n"); + break; + } + entry = (INDEX_ENTRY*)((u8*)entry + le16_to_cpu(entry->length)); + numb_entries++; + ntfs_log_verbose("\n"); + } + ntfs_log_verbose("\tEnd of index block reached\n"); + return numb_entries; +} + +#define COMPARE_INDEX_NAMES(attr, name) \ + ntfs_names_are_equal((name), sizeof(name) / 2 - 1, \ + (ntfschar*)((char*)(attr) + le16_to_cpu((attr)->name_offset)), \ + (attr)->name_length, CASE_SENSITIVE, NULL, 0) + +static INDEX_ATTR_TYPE get_index_attr_type(ntfs_inode *ni, ATTR_RECORD *attr, + INDEX_ROOT *index_root) +{ + char file_name[64]; + + if (!attr->name_length) + return INDEX_ATTR_UNKNOWN; + + if (index_root->type) { + if (index_root->type == AT_FILE_NAME) + return INDEX_ATTR_DIRECTORY_I30; + else + /* weird, this should be illegal */ + ntfs_log_error("Unknown index attribute type: 0x%0X\n", + le32_to_cpu(index_root->type)); + return INDEX_ATTR_UNKNOWN; + } + + if (utils_is_metadata(ni) <= 0) + return INDEX_ATTR_UNKNOWN; + if (utils_inode_get_name(ni, file_name, sizeof(file_name)) <= 0) + return INDEX_ATTR_UNKNOWN; + + if (COMPARE_INDEX_NAMES(attr, NTFS_INDEX_SDH)) + return INDEX_ATTR_SECURE_SDH; + else if (COMPARE_INDEX_NAMES(attr, NTFS_INDEX_SII)) + return INDEX_ATTR_SECURE_SII; + else if (COMPARE_INDEX_NAMES(attr, NTFS_INDEX_SII)) + return INDEX_ATTR_SECURE_SII; + else if (COMPARE_INDEX_NAMES(attr, NTFS_INDEX_Q)) + return INDEX_ATTR_QUOTA_Q; + else if (COMPARE_INDEX_NAMES(attr, NTFS_INDEX_R)) + return INDEX_ATTR_REPARSE_R; + else if (COMPARE_INDEX_NAMES(attr, NTFS_INDEX_O)) { + if (!strcmp(file_name, "/$Extend/$Quota")) + return INDEX_ATTR_QUOTA_O; + else if (!strcmp(file_name, "/$Extend/$ObjId")) + return INDEX_ATTR_OBJID_O; + } + + return INDEX_ATTR_UNKNOWN; +} + +static void ntfs_dump_index_attr_type(INDEX_ATTR_TYPE type) +{ + if (type == INDEX_ATTR_DIRECTORY_I30) + printf("DIRECTORY_I30"); + else if (type == INDEX_ATTR_SECURE_SDH) + printf("SECURE_SDH"); + else if (type == INDEX_ATTR_SECURE_SII) + printf("SECURE_SII"); + else if (type == INDEX_ATTR_OBJID_O) + printf("OBJID_O"); + else if (type == INDEX_ATTR_QUOTA_O) + printf("QUOTA_O"); + else if (type == INDEX_ATTR_QUOTA_Q) + printf("QUOTA_Q"); + else if (type == INDEX_ATTR_REPARSE_R) + printf("REPARSE_R"); + else + printf("UNKNOWN"); + printf("\n"); +} + +static void ntfs_dump_index_header(const char *indent, INDEX_HEADER *idx) +{ + printf("%sEntries Offset:\t\t %u (0x%x)\n", indent, + (unsigned)le32_to_cpu(idx->entries_offset), + (unsigned)le32_to_cpu(idx->entries_offset)); + printf("%sIndex Size:\t\t %u (0x%x)\n", indent, + (unsigned)le32_to_cpu(idx->index_length), + (unsigned)le32_to_cpu(idx->index_length)); + printf("%sAllocated Size:\t\t %u (0x%x)\n", indent, + (unsigned)le32_to_cpu(idx->allocated_size), + (unsigned)le32_to_cpu(idx->allocated_size)); + printf("%sIndex header flags:\t 0x%02x\n", indent, idx->ih_flags); + + /* FIXME: there are 3 reserved bytes here */ +} + +/** + * ntfs_dump_attr_index_root() + * + * dump the index_root attribute + */ +static void ntfs_dump_attr_index_root(ATTR_RECORD *attr, ntfs_inode *ni) +{ + INDEX_ATTR_TYPE type; + INDEX_ROOT *index_root = NULL; + INDEX_ENTRY *entry; + + index_root = (INDEX_ROOT*)((u8*)attr + le16_to_cpu(attr->value_offset)); + + /* attr_type dumping */ + type = get_index_attr_type(ni, attr, index_root); + printf("\tIndexed Attr Type:\t "); + ntfs_dump_index_attr_type(type); + + /* collation rule dumping */ + printf("\tCollation Rule:\t\t %u (0x%x)\n", + (unsigned)le32_to_cpu(index_root->collation_rule), + (unsigned)le32_to_cpu(index_root->collation_rule)); +/* COLLATION_BINARY, COLLATION_FILE_NAME, COLLATION_UNICODE_STRING, + COLLATION_NTOFS_ULONG, COLLATION_NTOFS_SID, + COLLATION_NTOFS_SECURITY_HASH, COLLATION_NTOFS_ULONGS */ + + printf("\tIndex Block Size:\t %u (0x%x)\n", + (unsigned)le32_to_cpu(index_root->index_block_size), + (unsigned)le32_to_cpu(index_root->index_block_size)); + if (le32_to_cpu(index_root->index_block_size) < ni->vol->cluster_size) + printf("\t512-byte Units Per Block:\t %u (0x%x)\n", + (unsigned)index_root->clusters_per_index_block, + (unsigned)index_root->clusters_per_index_block); + else + printf("\tClusters Per Block:\t %u (0x%x)\n", + (unsigned)index_root->clusters_per_index_block, + (unsigned)index_root->clusters_per_index_block); + + ntfs_dump_index_header("\t", &index_root->index); + + entry = (INDEX_ENTRY*)((u8*)index_root + + le32_to_cpu(index_root->index.entries_offset) + 0x10); + ntfs_log_verbose("\tDumping index root:\n"); + printf("\tIndex entries total:\t %d\n", + ntfs_dump_index_entries(entry, type)); +} + +static void ntfs_dump_usa_lsn(const char *indent, MFT_RECORD *mrec) +{ + printf("%sUpd. Seq. Array Off.:\t %u (0x%x)\n", indent, + (unsigned)le16_to_cpu(mrec->usa_ofs), + (unsigned)le16_to_cpu(mrec->usa_ofs)); + printf("%sUpd. Seq. Array Count:\t %u (0x%x)\n", indent, + (unsigned)le16_to_cpu(mrec->usa_count), + (unsigned)le16_to_cpu(mrec->usa_count)); + printf("%sUpd. Seq. Number:\t %u (0x%x)\n", indent, + (unsigned)le16_to_cpup((le16*)((u8*)mrec + + le16_to_cpu(mrec->usa_ofs))), + (unsigned)le16_to_cpup((le16*)((u8*)mrec + + le16_to_cpu(mrec->usa_ofs)))); + printf("%sLogFile Seq. Number:\t 0x%llx\n", indent, + (unsigned long long)sle64_to_cpu(mrec->lsn)); +} + + +static s32 ntfs_dump_index_block(INDEX_BLOCK *ib, INDEX_ATTR_TYPE type, + u32 ib_size) +{ + INDEX_ENTRY *entry; + + if (ntfs_mst_post_read_fixup((NTFS_RECORD*)ib, ib_size)) { + ntfs_log_perror("Damaged INDX record"); + return -1; + } + ntfs_log_verbose("\tDumping index block:\n"); + if (opts.verbose) + ntfs_dump_usa_lsn("\t\t", (MFT_RECORD*)ib); + + ntfs_log_verbose("\t\tNode VCN:\t\t %lld (0x%llx)\n", + (unsigned long long)sle64_to_cpu(ib->index_block_vcn), + (unsigned long long)sle64_to_cpu(ib->index_block_vcn)); + + entry = (INDEX_ENTRY*)((u8*)ib + + le32_to_cpu(ib->index.entries_offset) + 0x18); + + if (opts.verbose) { + ntfs_dump_index_header("\t\t", &ib->index); + printf("\n"); + } + + return ntfs_dump_index_entries(entry, type); +} + +/** + * ntfs_dump_attr_index_allocation() + * + * dump context of the index_allocation attribute + */ +static void ntfs_dump_attr_index_allocation(ATTR_RECORD *attr, ntfs_inode *ni) +{ + INDEX_ALLOCATION *allocation; + INDEX_ROOT *ir; + INDEX_ATTR_TYPE type; + ntfs_attr *na; + s64 offset; + u32 index_block_size; + int total_entries = 0; + int total_indx_blocks = 0; + u8 *bitmap, *byte; + int bit; + ntfschar *name; + u32 name_len; + + ir = ntfs_index_root_get(ni, attr); + if (!ir) { + ntfs_log_perror("Failed to read $INDEX_ROOT attribute"); + return; + } + + type = get_index_attr_type(ni, attr, ir); + + name = (ntfschar *)((u8 *)attr + le16_to_cpu(attr->name_offset)); + name_len = attr->name_length; + + byte = bitmap = ntfs_attr_readall(ni, AT_BITMAP, name, name_len, NULL); + if (!byte) { + ntfs_log_perror("Failed to read $BITMAP attribute"); + goto out_index_root; + } + + index_block_size = le32_to_cpu(ir->index_block_size); + allocation = (INDEX_ALLOCATION*)ntfs_malloc(index_block_size); + if (!allocation) { + ntfs_log_perror("Failed to allocate $INDEX_ALLOCATION buffer"); + goto out_bitmap; + } + na = ntfs_attr_open(ni, AT_INDEX_ALLOCATION, name, name_len); + if (!na) { + ntfs_log_perror("Failed to open $INDEX_ALLOCATION"); + goto out_alloc; + } + + offset = 0; + bit = 0; + while (offset < na->data_size) { + if (*byte & (1 << bit)) { + int entries; + + if (ntfs_attr_pread(na, offset, index_block_size, + allocation) != index_block_size) { + ntfs_log_perror("ntfs_attr_pread failed"); + goto out_na; + } + + entries = ntfs_dump_index_block(allocation, type, + index_block_size); + if (entries != -1) { + total_entries += entries; + total_indx_blocks++; + ntfs_log_verbose("\tIndex entries:\t\t %d\n", + entries); + } + } + bit++; + if (bit > 7) { + bit = 0; + byte++; + } + offset += index_block_size; + } + printf("\tIndex entries total:\t %d\n", total_entries); + printf("\tINDX blocks total:\t %d\n", total_indx_blocks); + +out_na : + ntfs_attr_close(na); +out_alloc : + free(allocation); +out_bitmap: + free(bitmap); +out_index_root: + free(ir); +} + +/** + * ntfs_dump_attr_bitmap() + * + * dump the bitmap attribute + */ +static void ntfs_dump_attr_bitmap(ATTR_RECORD *attr __attribute__((unused))) +{ + /* TODO */ +} + +/** + * ntfs_dump_attr_reparse_point() + * + * of ntfs 3.x dumps the reparse_point attribute + */ +static void ntfs_dump_attr_reparse_point(ATTR_RECORD *attr + __attribute__((unused)), ntfs_inode *inode) +{ + REPARSE_POINT *reparse; + le32 tag; + const char *name; + u8 *pvalue; + s64 size; + unsigned int length; + unsigned int cnt; + + if (attr->non_resident) { + reparse = ntfs_attr_readall(inode, AT_REPARSE_POINT, + (ntfschar*)NULL, 0, &size); + } else { + reparse = (REPARSE_POINT*)((u8*)attr + + le16_to_cpu(attr->value_offset)); + } + if (reparse) { + tag = reparse->reparse_tag; + name = reparse_type_name(tag); + printf("\tReparse tag:\t\t 0x%08lx%s\n", + (long)le32_to_cpu(tag),name); + length = le16_to_cpu(reparse->reparse_data_length); + printf("\tData length:\t\t %u (0x%x)\n", + (unsigned int)length,(unsigned int)length); + cnt = length; + pvalue = reparse->reparse_data; + printf("\tData:\t\t\t"); + printf(cnt ? " 0x" : "(NONE)"); + if (cnt > 32) + cnt = 32; + while (cnt-- > 0) + printf("%02x",*pvalue++); + if (length > 32) + printf("...\n"); + else + printf("\n"); + if (attr->non_resident) + free(reparse); + } else { + ntfs_log_perror("Failed to get the reparse data"); + } +} + +/** + * ntfs_dump_attr_ea_information() + * + * dump the ea_information attribute + */ +static void ntfs_dump_attr_ea_information(ATTR_RECORD *attr) +{ + EA_INFORMATION *ea_info; + + ea_info = (EA_INFORMATION*)((u8*)attr + + le16_to_cpu(attr->value_offset)); + printf("\tPacked EA length:\t %u (0x%x)\n", + (unsigned)le16_to_cpu(ea_info->ea_length), + (unsigned)le16_to_cpu(ea_info->ea_length)); + printf("\tNEED_EA count:\t\t %u (0x%x)\n", + (unsigned)le16_to_cpu(ea_info->need_ea_count), + (unsigned)le16_to_cpu(ea_info->need_ea_count)); + printf("\tUnpacked EA length:\t %u (0x%x)\n", + (unsigned)le32_to_cpu(ea_info->ea_query_length), + (unsigned)le32_to_cpu(ea_info->ea_query_length)); +} + +/** + * ntfs_dump_attr_ea() + * + * dump the ea attribute + */ +static void ntfs_dump_attr_ea(ATTR_RECORD *attr, ntfs_volume *vol) +{ + const EA_ATTR *ea; + const u8 *pvalue; + u8 *buf = NULL; + const le32 *pval; + int offset; + int cnt; + s64 data_size; + + if (attr->non_resident) { + runlist *rl; + + data_size = sle64_to_cpu(attr->data_size); + if (!opts.verbose) + return; + /* FIXME: We don't handle fragmented mapping pairs case. */ + rl = ntfs_mapping_pairs_decompress(vol, attr, NULL); + if (rl) { + s64 bytes_read; + + buf = ntfs_malloc(data_size); + if (!buf) { + free(rl); + return; + } + bytes_read = ntfs_rl_pread(vol, rl, 0, data_size, buf); + if (bytes_read != data_size) { + ntfs_log_perror("ntfs_rl_pread failed"); + free(buf); + free(rl); + return; + } + free(rl); + ea = (EA_ATTR*)buf; + } else { + ntfs_log_perror("ntfs_mapping_pairs_decompress failed"); + return; + } + } else { + data_size = le32_to_cpu(attr->value_length); + if (!opts.verbose) + return; + ea = (EA_ATTR*)((u8*)attr + le16_to_cpu(attr->value_offset)); + } + offset = 0; + while (1) { + printf("\n\tEA flags:\t\t "); + if (ea->flags) { + if (ea->flags == NEED_EA) + printf("NEED_EA\n"); + else + printf("Unknown (0x%02x)\n", + (unsigned)ea->flags); + } else + printf("NONE\n"); + printf("\tName length:\t %d (0x%x)\n", + (unsigned)ea->name_length, + (unsigned)ea->name_length); + printf("\tValue length:\t %d (0x%x)\n", + (unsigned)le16_to_cpu(ea->value_length), + (unsigned)le16_to_cpu(ea->value_length)); + /* Name expected to be null terminated ? */ + printf("\tName:\t\t '%s'\n", ea->name); + printf("\tValue:\t\t "); + if (ea->name_length == 11 && + !strncmp((const char*)"SETFILEBITS", + (const char*)ea->name, 11)) { + pval = (const le32*)(ea->value + ea->name_length + 1); + printf("0%lo\n", (unsigned long)le32_to_cpu(*pval)); + } else { + /* No alignment for value */ + pvalue = ea->value + ea->name_length + 1; + /* Hex show a maximum of 32 bytes */ + cnt = le16_to_cpu(ea->value_length); + printf(cnt ? "0x" : "(NONE)"); + if (cnt > 32) + cnt = 32; + while (cnt-- > 0) + printf("%02x",*pvalue++); + if (le16_to_cpu(ea->value_length) > 32) + printf("...\n"); + else + printf("\n"); + } + if (ea->next_entry_offset) { + offset += le32_to_cpu(ea->next_entry_offset); + ea = (const EA_ATTR*)((const u8*)ea + + le32_to_cpu(ea->next_entry_offset)); + } else + break; + if (offset >= data_size) + break; + } + free(buf); +} + +/** + * ntfs_dump_attr_property_set() + * + * dump the property_set attribute + */ +static void ntfs_dump_attr_property_set(ATTR_RECORD *attr __attribute__((unused))) +{ + /* TODO */ +} + +static void ntfs_hex_dump(void *buf,unsigned int length); + +/** + * ntfs_dump_attr_logged_utility_stream() + * + * dump the property_set attribute + */ +static void ntfs_dump_attr_logged_utility_stream(ATTR_RECORD *attr, + ntfs_inode *ni) +{ + char *buf; + s64 size; + + if (!opts.verbose) + return; + buf = ntfs_attr_readall(ni, AT_LOGGED_UTILITY_STREAM, + ntfs_attr_get_name(attr), attr->name_length, &size); + if (buf) + ntfs_hex_dump(buf, size); + free(buf); + /* TODO */ +} + +/** + * ntfs_hex_dump + */ +static void ntfs_hex_dump(void *buf,unsigned int length) +{ + unsigned int i=0; + while (i126)) { + c = '.'; + } + printf("%c",c); + } + + /* end line */ + printf("\n"); + i=j; + } +} + +/** + * ntfs_dump_attr_unknown + */ +static void ntfs_dump_attr_unknown(ATTR_RECORD *attr) +{ + printf("===== Please report this unknown attribute type to %s =====\n", + NTFS_DEV_LIST); + + if (!attr->non_resident) { + /* hex dump */ + printf("\tDumping some of the attribute data:\n"); + ntfs_hex_dump((u8*)attr + le16_to_cpu(attr->value_offset), + (le32_to_cpu(attr->value_length) > 128) ? + 128 : le32_to_cpu(attr->value_length)); + } +} + +/** + * ntfs_dump_inode_general_info + */ +static void ntfs_dump_inode_general_info(ntfs_inode *inode) +{ + MFT_RECORD *mrec = inode->mrec; + le16 inode_flags = mrec->flags; + + printf("Dumping Inode %llu (0x%llx)\n", + (long long)inode->mft_no, + (unsigned long long)inode->mft_no); + + ntfs_dump_usa_lsn("", mrec); + printf("MFT Record Seq. Numb.:\t %u (0x%x)\n", + (unsigned)le16_to_cpu(mrec->sequence_number), + (unsigned)le16_to_cpu(mrec->sequence_number)); + printf("Number of Hard Links:\t %u (0x%x)\n", + (unsigned)le16_to_cpu(mrec->link_count), + (unsigned)le16_to_cpu(mrec->link_count)); + printf("Attribute Offset:\t %u (0x%x)\n", + (unsigned)le16_to_cpu(mrec->attrs_offset), + (unsigned)le16_to_cpu(mrec->attrs_offset)); + + printf("MFT Record Flags:\t "); + if (inode_flags) { + if (MFT_RECORD_IN_USE & inode_flags) { + printf("IN_USE "); + inode_flags &= ~MFT_RECORD_IN_USE; + } + if (MFT_RECORD_IS_DIRECTORY & inode_flags) { + printf("DIRECTORY "); + inode_flags &= ~MFT_RECORD_IS_DIRECTORY; + } + /* The meaning of IS_4 is illusive but not its existence. */ + if (MFT_RECORD_IS_4 & inode_flags) { + printf("IS_4 "); + inode_flags &= ~MFT_RECORD_IS_4; + } + if (MFT_RECORD_IS_VIEW_INDEX & inode_flags) { + printf("VIEW_INDEX "); + inode_flags &= ~MFT_RECORD_IS_VIEW_INDEX; + } + if (inode_flags) + printf("UNKNOWN: 0x%04x", (unsigned)le16_to_cpu( + inode_flags)); + } else { + printf("none"); + } + printf("\n"); + + printf("Bytes Used:\t\t %u (0x%x) bytes\n", + (unsigned)le32_to_cpu(mrec->bytes_in_use), + (unsigned)le32_to_cpu(mrec->bytes_in_use)); + printf("Bytes Allocated:\t %u (0x%x) bytes\n", + (unsigned)le32_to_cpu(mrec->bytes_allocated), + (unsigned)le32_to_cpu(mrec->bytes_allocated)); + + if (mrec->base_mft_record) { + printf("Base MFT Record:\t %llu (0x%llx)\n", + (unsigned long long) + MREF_LE(mrec->base_mft_record), + (unsigned long long) + MREF_LE(mrec->base_mft_record)); + } + printf("Next Attribute Instance: %u (0x%x)\n", + (unsigned)le16_to_cpu(mrec->next_attr_instance), + (unsigned)le16_to_cpu(mrec->next_attr_instance)); + + printf("MFT Padding:\t"); + ntfs_dump_bytes((u8 *)mrec, le16_to_cpu(mrec->usa_ofs) + + 2 * le16_to_cpu(mrec->usa_count), + le16_to_cpu(mrec->attrs_offset)); + printf("\n"); +} + +/** + * ntfs_get_file_attributes + */ +static void ntfs_dump_file_attributes(ntfs_inode *inode) +{ + struct RUNCOUNT runcount; + ntfs_attr_search_ctx *ctx = NULL; + + runcount.runs = 0; + runcount.fragments = 0; + /* then start enumerating attributes + see ntfs_attr_lookup documentation for detailed explanation */ + ctx = ntfs_attr_get_search_ctx(inode, NULL); + while (!ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, + 0, NULL, 0, ctx)) { + if (ctx->attr->type == AT_END || ctx->attr->type == AT_UNUSED) { + printf("Weird: %s attribute type was found, please " + "report this.\n", + get_attribute_type_name( + ctx->attr->type)); + continue; + } + + ntfs_dump_attribute_header(ctx, inode->vol, &runcount); + + switch (ctx->attr->type) { + case AT_STANDARD_INFORMATION: + ntfs_dump_attr_standard_information(ctx->attr); + break; + case AT_ATTRIBUTE_LIST: + ntfs_dump_attr_list(ctx->attr, inode->vol); + break; + case AT_FILE_NAME: + ntfs_dump_attr_file_name(ctx->attr); + break; + case AT_OBJECT_ID: + ntfs_dump_attr_object_id(ctx->attr, inode->vol); + break; + case AT_SECURITY_DESCRIPTOR: + ntfs_dump_attr_security_descriptor(ctx->attr, + inode->vol); + break; + case AT_VOLUME_NAME: + ntfs_dump_attr_volume_name(ctx->attr); + break; + case AT_VOLUME_INFORMATION: + ntfs_dump_attr_volume_information(ctx->attr); + break; + case AT_DATA: + ntfs_dump_attr_data(ctx->attr, inode); + break; + case AT_INDEX_ROOT: + ntfs_dump_attr_index_root(ctx->attr, inode); + break; + case AT_INDEX_ALLOCATION: + ntfs_dump_attr_index_allocation(ctx->attr, inode); + break; + case AT_BITMAP: + ntfs_dump_attr_bitmap(ctx->attr); + break; + case AT_REPARSE_POINT: + ntfs_dump_attr_reparse_point(ctx->attr, inode); + break; + case AT_EA_INFORMATION: + ntfs_dump_attr_ea_information(ctx->attr); + break; + case AT_EA: + ntfs_dump_attr_ea(ctx->attr, inode->vol); + break; + case AT_PROPERTY_SET: + ntfs_dump_attr_property_set(ctx->attr); + break; + case AT_LOGGED_UTILITY_STREAM: + ntfs_dump_attr_logged_utility_stream(ctx->attr, inode); + break; + default: + ntfs_dump_attr_unknown(ctx->attr); + } + } + + /* if we exited the loop before we're done - notify the user */ + if (errno != ENOENT) { + ntfs_log_perror("ntfsinfo error: stopped before finished " + "enumerating attributes"); + } else { + printf("End of inode reached\n"); + if (opts.verbose) { + printf("Total runs: %lu (fragments: %lu)\n", + runcount.runs, runcount.fragments); + } + } + + /* close all data-structures we used */ + ntfs_attr_put_search_ctx(ctx); + ntfs_inode_close(inode); +} + +/** + * main() - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char **argv) +{ + ntfs_volume *vol; + int res; + + setlinebuf(stdout); + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + res = parse_options(argc, argv); + if (res > 0) + printf("Failed to parse command line options\n"); + if (res >= 0) + exit(res); + + utils_set_locale(); + + vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY | + (opts.force ? NTFS_MNT_RECOVER : 0)); + if (!vol) { + printf("Failed to open '%s'.\n", opts.device); + exit(1); + } + + /* + * if opts.mft is not 0, then we will print out information about + * the volume, such as the sector size and whatnot. + */ + if (opts.mft) + ntfs_dump_volume(vol); + + if ((opts.inode != -1) || opts.filename) { + ntfs_inode *inode; + /* obtain the inode */ + if (opts.filename) { +#ifdef HAVE_WINDOWS_H + char *unix_name; + + unix_name = ntfs_utils_unix_path(opts.filename); + if (unix_name) { + inode = ntfs_pathname_to_inode(vol, NULL, + unix_name); + free(unix_name); + } else + inode = (ntfs_inode*)NULL; +#else + inode = ntfs_pathname_to_inode(vol, NULL, + opts.filename); +#endif + } else { + inode = ntfs_inode_open(vol, MK_MREF(opts.inode, 0)); + } + + /* dump the inode information */ + if (inode) { + /* general info about the inode's mft record */ + ntfs_dump_inode_general_info(inode); + /* dump attributes */ + ntfs_dump_file_attributes(inode); + } else { + /* can't open inode */ + /* + * note: when the specified inode does not exist, either + * EIO or or ESPIPE is returned, we should notify better + * in those cases + */ + ntfs_log_perror("Error loading node"); + } + } + + ntfs_umount(vol, FALSE); + return 0; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfslabel.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfslabel.8.in new file mode 100644 index 0000000..a949366 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfslabel.8.in @@ -0,0 +1,119 @@ +.\" Copyright (c) 2002\-2004 Anton Altaparmakov. +.\" Copyright (c) 2005 Richard Russon. +.\" Copyright (c) 2012 Jean-Pierre Andre. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSLABEL 8 "January 2012" "ntfs-3g @VERSION@" +.SH NAME +ntfslabel \- display/change the label on an ntfs file system +.SH SYNOPSIS +.B ntfslabel +[\fIoptions\fR] \fIdevice \fR[\fInew\-label\fR] +.SH DESCRIPTION +.B ntfslabel +will display or change the file system label on the ntfs file system located on +.IR device . +It can also change the serial number of the +.IR device . +.PP +If the optional argument +.I new\-label +is not present, and no option is present, +.B ntfslabel +will simply display the current file system label. +.PP +If the optional argument +.I new\-label +is present, then +.B ntfslabel +will set the file system label to be +.IR new\-label . +NTFS file system labels can be at most 128 Unicode characters long; if +.I new\-label +is longer than 128 Unicode characters, +.B ntfslabel +will truncate it and print a warning message. +.PP +It is also possible to set the file system label using the +.B \-L +option of +.BR mkntfs (8) +during creation of the file system. +.SH OPTIONS +Below is a summary of all the options that +.B ntfslabel +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not working with a mounted +volume. Use this option with caution. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-\-new\-serial\fR[\fI=ssssssssssssssss\fR], or +.TP +\fB\-\-new\-half\-serial\fR[\fI=ssssssss\fR] +Set a new serial number to the device, either the argument value, or a random +one if no argument is given. The serial number is a 64 bit number, +represented as a sixteen-digit hexadecimal number, used to identify the +device during the mounting process. As a consequence, two devices with the +same serial number cannot be mounted at the same time on the same computer. +This is not the volume UUID used by Windows to locate files which have been +moved to another volume. + +The option \-\-new\-half\-serial only changes the upper part of the serial +number, keeping the lower part which is used by Windows unchanged. +In this case the optional argument is an eight-digit hexadecimal number. + +.TP +\fB\-n\fR, \fB\-\-no\-action\fR +Don't actually write to disk. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Reduce the amount of output to a minimum. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Increase the amount of output that +.B ntfslabel +prints. The label and the serial number are displayed. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license for +.BR ntfslabel . +.SH BUGS +There are no known problems with +.BR ntfslabel . +If you find a bug please send an email describing the problem to the +development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfslabel +was written by Matthew J. Fanto, with contributions from Anton Altaparmakov and +Richard Russon. +It was ported to ntfs-3g by Erik Larsson. +.SH AVAILABILITY +.B ntfslabel +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR mkntfs (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfslabel.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfslabel.c new file mode 100644 index 0000000..9ddf46f --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfslabel.c @@ -0,0 +1,464 @@ +/** + * ntfslabel - Part of the Linux-NTFS project. + * + * Copyright (c) 2002 Matthew J. Fanto + * Copyright (c) 2002-2005 Anton Altaparmakov + * Copyright (c) 2002-2003 Richard Russon + * Copyright (c) 2012-2014 Jean-Pierre Andre + * + * This utility will display/change the label on an NTFS partition. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_LOCALE_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif + +#include "debug.h" +#include "mft.h" +#include "utils.h" +/* #include "version.h" */ +#include "logging.h" +#include "misc.h" + +static const char *EXEC_NAME = "ntfslabel"; + +static struct options { + char *device; /* Device/File to work with */ + char *label; /* Set the label to this */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + int force; /* Override common sense */ + int new_serial; /* Change the serial number */ + unsigned long long serial; /* Forced serial number value */ + int noaction; /* Do not write to disk */ +} opts; + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + ntfs_log_info("\n%s v%s (libntfs-3g) - Display, or set, the label for an " + "NTFS Volume.\n\n", EXEC_NAME, VERSION); + ntfs_log_info("Copyright (c)\n"); + ntfs_log_info(" 2002 Matthew J. Fanto\n"); + ntfs_log_info(" 2002-2005 Anton Altaparmakov\n"); + ntfs_log_info(" 2002-2003 Richard Russon\n"); + ntfs_log_info(" 2012-2014 Jean-Pierre Andre\n"); + ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + ntfs_log_info("\nUsage: %s [options] device [label]\n" + " -n, --no-action Do not write to disk\n" + " -f, --force Use less caution\n" + " --new-serial Set a new serial number\n" + " --new-half-serial Set a partial new serial number\n" + " -q, --quiet Less output\n" + " -v, --verbose More output\n" + " -V, --version Display version information\n" + " -h, --help Display this help\n\n", + EXEC_NAME); + ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char *argv[]) +{ + static const char *sopt = "-fh?IinqvV"; + static const struct option lopt[] = { + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "new-serial", optional_argument, NULL, 'I' }, + { "new-half-serial", optional_argument, NULL, 'i' }, + { "no-action", no_argument, NULL, 'n' }, + { "quiet", no_argument, NULL, 'q' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 }, + }; + + int c = -1; + int err = 0; + int ver = 0; + int help = 0; + int levels = 0; + char *endserial; + + opterr = 0; /* We'll handle the errors, thank you. */ + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!err && !opts.device) + opts.device = argv[optind-1]; + else if (!err && !opts.label) + opts.label = argv[optind-1]; + else + err++; + break; + case 'f': + opts.force++; + break; + case 'h': + help++; + break; + case 'I' : /* not proposed as a short option letter */ + if (optarg) { + opts.serial = strtoull(optarg, &endserial, 16); + if (*endserial) + ntfs_log_error("Bad hexadecimal serial number.\n"); + } + opts.new_serial |= 2; + break; + case 'i' : /* not proposed as a short option letter */ + if (optarg) { + opts.serial = strtoull(optarg, &endserial, 16) + << 32; + if (*endserial) + ntfs_log_error("Bad hexadecimal serial number.\n"); + } + opts.new_serial |= 1; + break; + case 'n': + opts.noaction++; + break; + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + ver++; + break; + case '?': + if (strncmp (argv[optind-1], "--log-", 6) == 0) { + if (!ntfs_log_parse_option (argv[optind-1])) + err++; + break; + } + /* fall through */ + default: + ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); + err++; + break; + } + } + + /* Make sure we're in sync with the log levels */ + levels = ntfs_log_get_levels(); + if (levels & NTFS_LOG_LEVEL_VERBOSE) + opts.verbose++; + if (!(levels & NTFS_LOG_LEVEL_QUIET)) + opts.quiet++; + + if (help || ver) { + opts.quiet = 0; + } else { + if (opts.device == NULL) { + if (argc > 1) + ntfs_log_error("You must specify a device.\n"); + err++; + } + + if (opts.quiet && opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose at " + "the same time.\n"); + err++; + } + } + + if (ver) + version(); + if (help || err) + usage(); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver ? 0 : -1)); +} + +static int change_serial(ntfs_volume *vol, u64 sector, le64 serial_number, + NTFS_BOOT_SECTOR *bs, NTFS_BOOT_SECTOR *oldbs) +{ + int res; + le64 mask; + BOOL same; + + res = -1; + if ((ntfs_pread(vol->dev, sector << vol->sector_size_bits, + vol->sector_size, bs) == vol->sector_size)) { + same = TRUE; + if (!sector) + /* save the real bootsector */ + memcpy(oldbs, bs, vol->sector_size); + else + /* backup bootsector must be similar */ + same = !memcmp(oldbs, bs, vol->sector_size); + if (same) { + if (opts.new_serial & 2) + bs->volume_serial_number = serial_number; + else { + mask = const_cpu_to_le64(~0x0ffffffffULL); + bs->volume_serial_number + = (serial_number & mask) + | (bs->volume_serial_number & ~mask); + } + if (opts.noaction + || (ntfs_pwrite(vol->dev, + sector << vol->sector_size_bits, + vol->sector_size, bs) == vol->sector_size)) { + res = 0; + } + } else { + ntfs_log_info("* Warning : the backup boot sector" + " does not match (leaving unchanged)\n"); + res = 0; + } + } + return (res); +} + +static int set_new_serial(ntfs_volume *vol) +{ + NTFS_BOOT_SECTOR *bs; /* full boot sectors */ + NTFS_BOOT_SECTOR *oldbs; /* full original boot sector */ + le64 serial_number; + u64 number_of_sectors; + u64 sn; + int res; + + res = -1; + bs = (NTFS_BOOT_SECTOR*)ntfs_malloc(vol->sector_size); + oldbs = (NTFS_BOOT_SECTOR*)ntfs_malloc(vol->sector_size); + if (bs && oldbs) { + if (opts.serial) + serial_number = cpu_to_le64(opts.serial); + else { + /* different values for parallel processes */ + srandom(time((time_t*)NULL) ^ (getpid() << 16)); + sn = ((u64)random() << 32) + | ((u64)random() & 0xffffffff); + serial_number = cpu_to_le64(sn); + } + if (!change_serial(vol, 0, serial_number, bs, oldbs)) { + number_of_sectors = ntfs_device_size_get(vol->dev, + vol->sector_size); + if (!change_serial(vol, number_of_sectors - 1, + serial_number, bs, oldbs)) { + ntfs_log_info("New serial number : %016llx\n", + (long long)le64_to_cpu( + bs->volume_serial_number)); + res = 0; + } + } + free(bs); + free(oldbs); + } + if (res) + ntfs_log_info("Error setting a new serial number\n"); + return (res); +} + +static int print_serial(ntfs_volume *vol) +{ + NTFS_BOOT_SECTOR *bs; /* full boot sectors */ + int res; + + res = -1; + bs = (NTFS_BOOT_SECTOR*)ntfs_malloc(vol->sector_size); + if (bs + && (ntfs_pread(vol->dev, 0, + vol->sector_size, bs) == vol->sector_size)) { + ntfs_log_info("Serial number : %016llx\n", + (long long)le64_to_cpu(bs->volume_serial_number)); + res = 0; + free(bs); + } + if (res) + ntfs_log_info("Error getting the serial number\n"); + return (res); +} + +/** + * print_label - display the current label of a mounted ntfs partition. + * @dev: device to read the label from + * @mnt_flags: mount flags of the device or 0 if not mounted + * @mnt_point: mount point of the device or NULL + * + * Print the label of the device @dev. + */ +static int print_label(ntfs_volume *vol, unsigned long mnt_flags) +{ + int result = 0; + //XXX significant? + if ((mnt_flags & (NTFS_MF_MOUNTED | NTFS_MF_READONLY)) == + NTFS_MF_MOUNTED) { + ntfs_log_error("%s is mounted read-write, results may be " + "unreliable.\n", opts.device); + result = 1; + } + + if (opts.verbose) + ntfs_log_info("Volume label : %s\n", vol->vol_name); + else + ntfs_log_info("%s\n", vol->vol_name); + return result; +} + +/** + * change_label - change the current label on a device + * @dev: device to change the label on + * @mnt_flags: mount flags of the device or 0 if not mounted + * @mnt_point: mount point of the device or NULL + * @label: the new label + * + * Change the label on the device @dev to @label. + */ +static int change_label(ntfs_volume *vol, char *label) +{ + ntfschar *new_label = NULL; + int label_len; + int result = 0; + + label_len = ntfs_mbstoucs(label, &new_label); + if (label_len == -1) { + ntfs_log_perror("Unable to convert label string to Unicode"); + return 1; + } + else if (label_len*sizeof(ntfschar) > 0x100) { + ntfs_log_warning("New label is too long. Maximum %u characters " + "allowed. Truncating %u excess characters.\n", + (unsigned)(0x100 / sizeof(ntfschar)), + (unsigned)(label_len - + (0x100 / sizeof(ntfschar)))); + label_len = 0x100 / sizeof(ntfschar); + new_label[label_len] = const_cpu_to_le16(0); + } + + if(!opts.noaction) + result = ntfs_volume_rename(vol, new_label, label_len) ? 1 : 0; + + free(new_label); + return result; +} + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char **argv) +{ + unsigned long mnt_flags = 0; + int result = 0; + ntfs_volume *vol; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + result = parse_options(argc, argv); + if (result >= 0) + return (result); + + result = 0; + utils_set_locale(); + + if ((opts.label || opts.new_serial) + && !opts.noaction + && !opts.force + && !ntfs_check_if_mounted(opts.device, &mnt_flags) + && (mnt_flags & NTFS_MF_MOUNTED)) { + ntfs_log_error("Cannot make changes to a mounted device\n"); + result = 1; + goto abort; + } + + if (!opts.label && !opts.new_serial) + opts.noaction++; + + vol = utils_mount_volume(opts.device, + (opts.noaction ? NTFS_MNT_RDONLY : 0) | + (opts.force ? NTFS_MNT_RECOVER : 0)); + if (!vol) + return 1; + + if (opts.new_serial) { + result = set_new_serial(vol); + if (result) + goto unmount; + } else { + if (opts.verbose) + result = print_serial(vol); + } + if (opts.label) + result = change_label(vol, opts.label); + else + result = print_label(vol, mnt_flags); + +unmount : + ntfs_umount(vol, FALSE); +abort : + /* "result" may be a negative reply of a library function */ + return (result ? 1 : 0); +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsls.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsls.8.in new file mode 100644 index 0000000..874bc04 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsls.8.in @@ -0,0 +1,172 @@ +.\" Copyright (c) 2003 Anton Altaparmakov. +.\" Copyright (c) 2005 Richard Russon. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSLS 8 "November 2005" "ntfs-3g @VERSION@" +.SH NAME +ntfsls \- list directory contents on an NTFS filesystem +.SH SYNOPSIS +.B ntfsls +[\fIoptions\fR] \fIdevice\fR +.sp +.B ntfsls +[ +.B \-a +| +.B \-\-all +] +[ +.B \-F +| +.B \-\-classify +] +[ +.B \-f +| +.B \-\-force +] +[ +.B \-h +| +.B \-\-help +] +[ +.B \-i +| +.B \-\-inode +] +[ +.B \-l +| +.B \-\-long +] +[ +.B \-p +| +.B \-\-path +.I PATH +] +[ +.B \-q +| +.B \-\-quiet +] +[ +.B \-s +| +.B \-\-system +] +[ +.B \-V +| +.B \-\-version +] +[ +.B \-v +| +.B \-\-verbose +] +[ +.B \-x +| +.B \-\-dos +] +.I device +.SH DESCRIPTION +.B ntfsls +is used to list information about the files specified by the +.I PATH +option (the root directory by default). +.I DEVICE +is the special file corresponding to the device (e.g +.IR /dev/hdXX ) +or an NTFS image file. +.SH OPTIONS +Below is a summary of all the options that +.B ntfsls +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-a\fR, \fB\-\-all\fR +Display all files. If this option is not specified file names in the POSIX +namespace will not be displayed. +.TP +\fB\-F\fR, \fB\-\-classify\fR +Append indicator (one of */=@|) to entries. +.TP +\fB\-f\fR, \fB\-\-force\fR +Force execution. For example necessary to run on an NTFS partition stored in +a normal file. +.TP +\fB\-h\fR, \fB\-\-help\fR +Print the usage information of +.B ntfsls +and exit. +.TP +\fB\-i\fR, \fB\-\-inode\fR +Print inode number of each file. This is the MFT reference number in NTFS +terminology. +.TP +\fB\-l\fR, \fB\-\-long\fR +Use a long listing format. +.TP +\fB\-p\fR, \fB\-\-path\fR PATH +The directory whose contents to list or the file (including the path) about +which to display information. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Suppress some debug/warning/error messages. +.TP +\fB\-R\fR, \fB\-\-recursive\fR +Show the contents of all directories beneath the specified directory. +.TP +\fB\-s\fR, \fB\-\-system\fR +Unless this options is specified, all files beginning with a dollar sign +character will not be listed as these files are usually system files. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Display more debug/warning/error messages. +.TP +\fB\-V\fR, \fB\-\-version\fR +Print the version number of +.B ntfsls +and exit. +.TP +\fB\-x\fR, \fB\-\-dos\fR +Display short file names, i.e. files in the DOS namespace, instead of long +file names, i.e. files in the WIN32 namespace. +.SH BUGS +There are no known problems with +.BR ntfsls . +If you find a bug please send an email describing the problem to the +development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +This version of +.B ntfsls +was written by Lode Leroy, Anton Altaparmakov, Richard Russon, Carmelo Kintana +and Giang Nguyen. +It was ported to ntfs-3g by Erik Larsson. +.SH AVAILABILITY +.B ntfsls +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsls.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsls.c new file mode 100644 index 0000000..909e7bb --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsls.c @@ -0,0 +1,717 @@ +/** + * ntfsls - Part of the Linux-NTFS project. + * + * Copyright (c) 2003 Lode Leroy + * Copyright (c) 2003-2005 Anton Altaparmakov + * Copyright (c) 2003 Richard Russon + * Copyright (c) 2004 Carmelo Kintana + * Copyright (c) 2004 Giang Nguyen + * + * This utility will list a directory's files. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif + +#include "types.h" +#include "mft.h" +#include "attrib.h" +#include "layout.h" +#include "inode.h" +#include "utils.h" +#include "dir.h" +#include "list.h" +#include "ntfstime.h" +/* #include "version.h" */ +#include "logging.h" + +static const char *EXEC_NAME = "ntfsls"; + +/** + * To hold sub-directory information for recursive listing. + * @depth: the level of this dir relative to opts.path + */ +struct dir { + struct ntfs_list_head list; + ntfs_inode *ni; + char name[MAX_PATH]; + int depth; +}; + +/** + * path_component - to store path component strings + * + * @name: string pointer + * + * NOTE: @name is not directly allocated memory. It simply points to the + * character array name in struct dir. + */ +struct path_component { + struct ntfs_list_head list; + const char *name; +}; + +/* The list of sub-dirs is like a "horizontal" tree. The root of + * the tree is opts.path, but it is not part of the list because + * that's not necessary. The rules of the list are (in order of + * precedence): + * 1. directories immediately follow their parent. + * 2. siblings are next to one another. + * + * For example, if: + * 1. opts.path is / + * 2. / has 2 sub-dirs: dir1 and dir2 + * 3. dir1 has 2 sub-dirs: dir11 and dir12 + * 4. dir2 has 0 sub-dirs + * then the list will be: + * dummy head -> dir1 -> dir11 -> dir12 -> dir2 + * + * dir_list_insert_pos keeps track of where to insert a sub-dir + * into the list. + */ +static struct ntfs_list_head *dir_list_insert_pos = NULL; + +/* The global depth relative to opts.path. + * ie: opts.path has depth 0, a sub-dir of opts.path has depth 1 + */ +static int depth = 0; + +static struct options { + char *device; /* Device/File to work with */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + int force; /* Override common sense */ + int all; + int system; + int dos; + int lng; + int inode; + int classify; + int recursive; + const char *path; +} opts; + +typedef struct { + ntfs_volume *vol; +} ntfsls_dirent; + +static int list_dir_entry(ntfsls_dirent * dirent, const ntfschar * name, + const int name_len, const int name_type, + const s64 pos, const MFT_REF mref, + const unsigned dt_type); + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + printf("\n%s v%s (libntfs-3g) - Display information about an NTFS " + "Volume.\n\n", EXEC_NAME, VERSION); + printf("Copyright (c) 2003 Lode Leroy\n"); + printf("Copyright (c) 2003-2005 Anton Altaparmakov\n"); + printf("Copyright (c) 2003 Richard Russon\n"); + printf("Copyright (c) 2004 Carmelo Kintana\n"); + printf("Copyright (c) 2004 Giang Nguyen\n"); + printf("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + printf("\nUsage: %s [options] device\n" + "\n" + " -a, --all Display all files\n" + " -F, --classify Display classification\n" + " -f, --force Use less caution\n" + " -h, --help Display this help\n" + " -i, --inode Display inode numbers\n" + " -l, --long Display long info\n" + " -p, --path PATH Directory whose contents to list\n" + " -q, --quiet Less output\n" + " -R, --recursive Recursively list subdirectories\n" + " -s, --system Display system files\n" + " -V, --version Display version information\n" + " -v, --verbose More output\n" + " -x, --dos Use short (DOS 8.3) names\n" + "\n", + EXEC_NAME); + + printf("NOTE: If neither -a nor -s is specified, the program defaults to -a.\n\n"); + + printf("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char *argv[]) +{ + static const char *sopt = "-aFfh?ilp:qRsVvx"; + static const struct option lopt[] = { + { "all", no_argument, NULL, 'a' }, + { "classify", no_argument, NULL, 'F' }, + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "inode", no_argument, NULL, 'i' }, + { "long", no_argument, NULL, 'l' }, + { "path", required_argument, NULL, 'p' }, + { "recursive", no_argument, NULL, 'R' }, + { "quiet", no_argument, NULL, 'q' }, + { "system", no_argument, NULL, 's' }, + { "version", no_argument, NULL, 'V' }, + { "verbose", no_argument, NULL, 'v' }, + { "dos", no_argument, NULL, 'x' }, + { NULL, 0, NULL, 0 }, + }; + + int c = -1; + int err = 0; + int ver = 0; + int help = 0; + int levels = 0; + + opterr = 0; /* We'll handle the errors, thank you. */ + + memset(&opts, 0, sizeof(opts)); + opts.device = NULL; + opts.path = "/"; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: + if (!opts.device) + opts.device = optarg; + else + err++; + break; + case 'p': + opts.path = optarg; + break; + case 'f': + opts.force++; + break; + case 'h': + case '?': + if (strncmp (argv[optind-1], "--log-", 6) == 0) { + if (!ntfs_log_parse_option (argv[optind-1])) + err++; + break; + } + help++; + break; + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + ver++; + break; + case 'x': + opts.dos = 1; + break; + case 'l': + opts.lng++; + break; + case 'i': + opts.inode++; + break; + case 'F': + opts.classify++; + break; + case 'a': + opts.all++; + break; + case 's': + opts.system++; + break; + case 'R': + opts.recursive++; + break; + default: + ntfs_log_error("Unknown option '%s'.\n", argv[optind - 1]); + err++; + break; + } + } + + /* Make sure we're in sync with the log levels */ + levels = ntfs_log_get_levels(); + if (levels & NTFS_LOG_LEVEL_VERBOSE) + opts.verbose++; + if (!(levels & NTFS_LOG_LEVEL_QUIET)) + opts.quiet++; + + /* defaults to -a if -s is not specified */ + if (!opts.system) + opts.all++; + + if (help || ver) + opts.quiet = 0; + else { + if (opts.device == NULL) { + if (argc > 1) + ntfs_log_error("You must specify exactly one " + "device.\n"); + err++; + } + + if (opts.quiet && opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose at the " + "same time.\n"); + err++; + } + } + + if (ver) + version(); + if (help || err) + usage(); + + return (!err && !help && !ver); +} + +/** + * free_dir - free one dir + * @tofree: the dir to free + * + * Close the inode and then free the dir + */ +static void free_dir(struct dir *tofree) +{ + if (tofree) { + if (tofree->ni) { + ntfs_inode_close(tofree->ni); + tofree->ni = NULL; + } + free(tofree); + } +} + +/** + * free_dirs - walk the list of dir's and free each of them + * @dir_list: the ntfs_list_head of any entry in the list + * + * Iterate over @dir_list, calling free_dir on each entry + */ +static void free_dirs(struct ntfs_list_head *dir_list) +{ + struct dir *tofree = NULL; + struct ntfs_list_head *walker = NULL; + + if (dir_list) { + ntfs_list_for_each(walker, dir_list) { + free_dir(tofree); + tofree = ntfs_list_entry(walker, struct dir, list); + } + + free_dir(tofree); + } +} + +/** + * readdir_recursive - list a directory and sub-directories encountered + * @ni: ntfs inode of the directory to list + * @pos: current position in directory + * @dirent: context for filldir callback supplied by the caller + * + * For each directory, print its path relative to opts.path. List a directory, + * then list each of its sub-directories. + * + * Returns 0 on success or -1 on error. + * + * NOTE: Assumes recursive option. Currently no limit on the depths of + * recursion. + */ +static int readdir_recursive(ntfs_inode * ni, s64 * pos, ntfsls_dirent * dirent) +{ + /* list of dirs to "ls" recursively */ + static struct dir dirs = { + .list = NTFS_LIST_HEAD_INIT(dirs.list), + .ni = NULL, + .name = {0}, + .depth = 0 + }; + + static struct path_component paths = { + .list = NTFS_LIST_HEAD_INIT(paths.list), + .name = NULL + }; + + static struct path_component base_comp; + + struct dir *subdir = NULL; + struct dir *tofree = NULL; + struct path_component comp; + struct path_component *tempcomp = NULL; + struct ntfs_list_head *dir_walker = NULL; + struct ntfs_list_head *comp_walker = NULL; + s64 pos2 = 0; + int ni_depth = depth; + int result = 0; + + if (ntfs_list_empty(&dirs.list)) { + base_comp.name = opts.path; + ntfs_list_add(&base_comp.list, &paths.list); + dir_list_insert_pos = &dirs.list; + printf("%s:\n", opts.path); + } + + depth++; + + result = ntfs_readdir(ni, pos, dirent, (ntfs_filldir_t) list_dir_entry); + + if (result == 0) { + ntfs_list_add_tail(&comp.list, &paths.list); + + /* for each of ni's sub-dirs: list in this iteration, then + free at the top of the next iteration or outside of loop */ + ntfs_list_for_each(dir_walker, &dirs.list) { + if (tofree) { + free_dir(tofree); + tofree = NULL; + } + subdir = ntfs_list_entry(dir_walker, struct dir, list); + + /* subdir is not a subdir of ni */ + if (subdir->depth != ni_depth + 1) + break; + + pos2 = 0; + dir_list_insert_pos = &dirs.list; + if (!subdir->ni) { + subdir->ni = + ntfs_pathname_to_inode(ni->vol, ni, + subdir->name); + + if (!subdir->ni) { + ntfs_log_error + ("ntfsls::readdir_recursive(): cannot get inode from pathname.\n"); + result = -1; + break; + } + } + puts(""); + + comp.name = subdir->name; + + /* print relative path header */ + ntfs_list_for_each(comp_walker, &paths.list) { + tempcomp = + ntfs_list_entry(comp_walker, + struct path_component, list); + printf("%s", tempcomp->name); + if (tempcomp != &comp + && *tempcomp->name != PATH_SEP + && (!opts.classify + || tempcomp == &base_comp)) + putchar(PATH_SEP); + } + puts(":"); + + result = readdir_recursive(subdir->ni, &pos2, dirent); + + if (result) + break; + + tofree = subdir; + ntfs_list_del(dir_walker); + } + + ntfs_list_del(&comp.list); + } + + if (tofree) + free_dir(tofree); + + /* if at the outer-most readdir_recursive, then clean up */ + if (ni_depth == 0) { + free_dirs(&dirs.list); + } + + depth--; + + return result; +} + +/** + * list_dir_entry + * + * FIXME: Should we print errors as we go along? (AIA) + */ +static int list_dir_entry(ntfsls_dirent * dirent, const ntfschar * name, + const int name_len, const int name_type, + const s64 pos __attribute__((unused)), + const MFT_REF mref, const unsigned dt_type) +{ + char *filename = NULL; + int result = 0; + + struct dir *dir = NULL; + + filename = calloc(1, MAX_PATH); + if (!filename) + return -1; + + if (ntfs_ucstombs(name, name_len, &filename, MAX_PATH) < 0) { + ntfs_log_error("Cannot represent filename in current locale.\n"); + goto free; + } + + result = 0; // These are successful + if ((MREF(mref) < FILE_first_user) && (!opts.system)) + goto free; + if (name_type == FILE_NAME_POSIX && !opts.all) + goto free; + if (((name_type & FILE_NAME_WIN32_AND_DOS) == FILE_NAME_WIN32) && + opts.dos) + goto free; + if (((name_type & FILE_NAME_WIN32_AND_DOS) == FILE_NAME_DOS) && + !opts.dos) + goto free; + if (dt_type == NTFS_DT_DIR && opts.classify) + sprintf(filename + strlen(filename), "/"); + + if (dt_type == NTFS_DT_DIR && opts.recursive + && strcmp(filename, ".") && strcmp(filename, "./") + && strcmp(filename, "..") && strcmp(filename, "../")) + { + dir = (struct dir *)calloc(1, sizeof(struct dir)); + + if (!dir) { + ntfs_log_error("Failed to allocate for subdir.\n"); + result = -1; + goto free; + } + + strcpy(dir->name, filename); + dir->ni = NULL; + dir->depth = depth; + } + + if (!opts.lng) { + if (!opts.inode) + printf("%s\n", filename); + else + printf("%7llu %s\n", (unsigned long long)MREF(mref), + filename); + result = 0; + } else { + s64 filesize = 0; + ntfs_inode *ni; + ntfs_attr_search_ctx *ctx = NULL; + FILE_NAME_ATTR *file_name_attr; + ATTR_RECORD *attr; + struct timespec change_time; + char t_buf[26]; + + result = -1; // Everything else is bad + + ni = ntfs_inode_open(dirent->vol, mref); + if (!ni) + goto release; + + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + goto release; + + if (ntfs_attr_lookup(AT_FILE_NAME, AT_UNNAMED, 0, 0, 0, NULL, + 0, ctx)) + goto release; + attr = ctx->attr; + + file_name_attr = (FILE_NAME_ATTR *)((char *)attr + + le16_to_cpu(attr->value_offset)); + if (!file_name_attr) + goto release; + + change_time = ntfs2timespec(file_name_attr->last_data_change_time); + strcpy(t_buf, ctime(&change_time.tv_sec)); + memmove(t_buf+16, t_buf+19, 5); + t_buf[21] = '\0'; + + if (dt_type != NTFS_DT_DIR) { + if (!ntfs_attr_lookup(AT_DATA, AT_UNNAMED, 0, 0, 0, + NULL, 0, ctx)) + filesize = ntfs_get_attribute_value_length( + ctx->attr); + } + + if (opts.inode) + printf("%7llu %8lld %s %s\n", + (unsigned long long)MREF(mref), + (long long)filesize, t_buf + 4, + filename); + else + printf("%8lld %s %s\n", (long long)filesize, t_buf + 4, + filename); + + if (dir) { + dir->ni = ni; + ni = NULL; /* so release does not close inode */ + } + + result = 0; +release: + /* Release attribute search context and close the inode. */ + if (ctx) + ntfs_attr_put_search_ctx(ctx); + if (ni) + ntfs_inode_close(ni); + } + + if (dir) { + if (result == 0) { + ntfs_list_add(&dir->list, dir_list_insert_pos); + dir_list_insert_pos = &dir->list; + } else { + free(dir); + dir = NULL; + } + } + +free: + free(filename); + return result; +} + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, parsing mount options failed + * 2 Error, mount attempt failed + * 3 Error, failed to open root directory + * 4 Error, failed to open directory in search path + */ +int main(int argc, char **argv) +{ + s64 pos; + ntfs_volume *vol; + ntfs_inode *ni; + ntfsls_dirent dirent; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + if (!parse_options(argc, argv)) { + // FIXME: Print error... (AIA) + return 1; + } + + utils_set_locale(); + + vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY | + (opts.force ? NTFS_MNT_RECOVER : 0)); + if (!vol) { + // FIXME: Print error... (AIA) + return 2; + } + + ni = ntfs_pathname_to_inode(vol, NULL, opts.path); + if (!ni) { + // FIXME: Print error... (AIA) + ntfs_umount(vol, FALSE); + return 3; + } + + /* + * We now are at the final path component. If it is a file just + * list it. If it is a directory, list its contents. + */ + pos = 0; + memset(&dirent, 0, sizeof(dirent)); + dirent.vol = vol; + if (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) { + if (opts.recursive) + readdir_recursive(ni, &pos, &dirent); + else + ntfs_readdir(ni, &pos, &dirent, + (ntfs_filldir_t) list_dir_entry); + // FIXME: error checking... (AIA) + } else { + ATTR_RECORD *rec; + FILE_NAME_ATTR *attr; + ntfs_attr_search_ctx *ctx; + int space = 4; + ntfschar *name = NULL; + int name_len = 0;; + + ctx = ntfs_attr_get_search_ctx(ni, NULL); + if (!ctx) + return -1; + + while ((rec = find_attribute(AT_FILE_NAME, ctx))) { + /* We know this will always be resident. */ + attr = (FILE_NAME_ATTR *) ((char *) rec + le16_to_cpu(rec->value_offset)); + + if (attr->file_name_type < space) { + name = attr->file_name; + name_len = attr->file_name_length; + space = attr->file_name_type; + } + } + + list_dir_entry(&dirent, name, name_len, space, pos, ni->mft_no, + NTFS_DT_REG); + // FIXME: error checking... (AIA) + + ntfs_attr_put_search_ctx(ctx); + } + + /* Finished with the inode; release it. */ + ntfs_inode_close(ni); + + ntfs_umount(vol, FALSE); + return 0; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsmftalloc.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsmftalloc.c new file mode 100644 index 0000000..96f7f79 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsmftalloc.c @@ -0,0 +1,368 @@ +/** + * ntfsmftalloc - Part of the Linux-NTFS project. + * + * Copyright (c) 2002-2005 Anton Altaparmakov + * + * This utility will allocate and initialize an mft record. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS source + * in the file COPYING); if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_UNISTD_H +# include +#endif +#ifdef HAVE_STDLIB_H +# include +#endif +#ifdef HAVE_STDIO_H +# include +#endif +#ifdef HAVE_STDARG_H +# include +#endif +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_ERRNO_H +# include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_GETOPT_H +# include +#else + extern int optind; +#endif +#ifdef HAVE_LIMITS_H +#include +#endif +#ifndef LLONG_MAX +# define LLONG_MAX 9223372036854775807LL +#endif + +#include "types.h" +#include "attrib.h" +#include "inode.h" +#include "layout.h" +#include "volume.h" +#include "mft.h" +#include "utils.h" +/* #include "version.h" */ +#include "logging.h" + +static const char *EXEC_NAME = "ntfsmftalloc"; + +/* Need these global so ntfsmftalloc_exit can access them. */ +static BOOL success = FALSE; + +static char *dev_name; + +static ntfs_volume *vol; +static ntfs_inode *ni = NULL; +static ntfs_inode *base_ni = NULL; +static s64 base_mft_no = -1; + +static struct { + /* -h, print usage and exit. */ + int no_action; /* -n, do not write to device, only display + what would be done. */ + int quiet; /* -q, quiet execution. */ + int verbose; /* -v, verbose execution, given twice, really + verbose execution (debug mode). */ + int force; /* -f, force allocation. */ + /* -V, print version and exit. */ +} opts; + +/** + * err_exit - error output and terminate; ignores quiet (-q) + */ +__attribute__((noreturn)) +__attribute__((format(printf, 1, 2))) +static void err_exit(const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "ERROR: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "Aborting...\n"); + exit(1); +} + +/** + * copyright - print copyright statements + */ +static void copyright(void) +{ + ntfs_log_info("Copyright (c) 2004-2005 Anton Altaparmakov\n" + "Allocate and initialize a base or an extent mft " + "record. If a base mft record\nis not specified, a " + "base mft record is allocated and initialized. " + "Otherwise,\nan extent mft record is allocated and " + "initialized to point to the specified\nbase mft " + "record.\n"); +} + +/** + * license - print license statement + */ +static void license(void) +{ + ntfs_log_info("%s", ntfs_gpl); +} + +/** + * usage - print a list of the parameters to the program + */ +__attribute__((noreturn)) +static void usage(void) +{ + copyright(); + ntfs_log_info("Usage: %s [options] device [base-mft-record]\n" + " -n Do not write to disk\n" + " -f Force execution despite errors\n" + " -q Quiet execution\n" + " -v Verbose execution\n" + " -vv Very verbose execution\n" + " -V Display version information\n" + " -l Display licensing information\n" + " -h Display this help\n", EXEC_NAME); + ntfs_log_info("%s%s", ntfs_bugs, ntfs_home); + exit(1); +} + +/** + * parse_options + */ +static void parse_options(int argc, char *argv[]) +{ + long long ll; + char *s; + int c; + + if (argc && *argv) + EXEC_NAME = *argv; + ntfs_log_info("%s v%s (libntfs-3g)\n", EXEC_NAME, VERSION); + while ((c = getopt(argc, argv, "fh?nqvVl")) != EOF) { + switch (c) { + case 'f': + opts.force = 1; + break; + case 'n': + opts.no_action = 1; + break; + case 'q': + opts.quiet = 1; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + /* Version number already printed, so just exit. */ + exit(0); + case 'l': + copyright(); + license(); + exit(0); + case 'h': + case '?': + default: + usage(); + } + } + + if (opts.verbose > 1) + ntfs_log_set_levels(NTFS_LOG_LEVEL_DEBUG | NTFS_LOG_LEVEL_TRACE | + NTFS_LOG_LEVEL_VERBOSE | NTFS_LOG_LEVEL_QUIET); + + if (optind == argc) + usage(); + /* Get the device. */ + dev_name = argv[optind++]; + ntfs_log_verbose("device name = %s\n", dev_name); + if (optind != argc) { + /* Get the base mft record number. */ + ll = strtoll(argv[optind++], &s, 0); + if (*s || !ll || (ll >= LLONG_MAX && errno == ERANGE)) + err_exit("Invalid base mft record number: %s\n", + argv[optind - 1]); + base_mft_no = ll; + ntfs_log_verbose("base mft record number = 0x%llx\n", (long long)ll); + } + if (optind != argc) + usage(); +} + +/** + * dump_mft_record + */ +static void dump_mft_record(MFT_RECORD *m) +{ + ATTR_RECORD *a; + unsigned int u; + MFT_REF r; + + ntfs_log_info("-- Beginning dump of mft record. --\n"); + u = le32_to_cpu(m->magic); + ntfs_log_info("Mft record signature (magic) = %c%c%c%c\n", u & 0xff, + u >> 8 & 0xff, u >> 16 & 0xff, u >> 24 & 0xff); + u = le16_to_cpu(m->usa_ofs); + ntfs_log_info("Update sequence array offset = %u (0x%x)\n", u, u); + ntfs_log_info("Update sequence array size = %u\n", le16_to_cpu(m->usa_count)); + ntfs_log_info("$LogFile sequence number (lsn) = %llu\n", + (unsigned long long)sle64_to_cpu(m->lsn)); + ntfs_log_info("Sequence number = %u\n", le16_to_cpu(m->sequence_number)); + ntfs_log_info("Reference (hard link) count = %u\n", + le16_to_cpu(m->link_count)); + u = le16_to_cpu(m->attrs_offset); + ntfs_log_info("First attribute offset = %u (0x%x)\n", u, u); + ntfs_log_info("Flags = %u: ", le16_to_cpu(m->flags)); + if (m->flags & MFT_RECORD_IN_USE) + ntfs_log_info("MFT_RECORD_IN_USE"); + else + ntfs_log_info("MFT_RECORD_NOT_IN_USE"); + if (m->flags & MFT_RECORD_IS_DIRECTORY) + ntfs_log_info(" | MFT_RECORD_IS_DIRECTORY"); + ntfs_log_info("\n"); + u = le32_to_cpu(m->bytes_in_use); + ntfs_log_info("Bytes in use = %u (0x%x)\n", u, u); + u = le32_to_cpu(m->bytes_allocated); + ntfs_log_info("Bytes allocated = %u (0x%x)\n", u, u); + r = le64_to_cpu(m->base_mft_record); + ntfs_log_info("Base mft record reference:\n\tMft record number = %llu\n\t" + "Sequence number = %u\n", + (unsigned long long)MREF(r), MSEQNO(r)); + ntfs_log_info("Next attribute instance = %u\n", + le16_to_cpu(m->next_attr_instance)); + a = (ATTR_RECORD*)((char*)m + le16_to_cpu(m->attrs_offset)); + ntfs_log_info("-- Beginning dump of attributes within mft record. --\n"); + while ((char*)a < (char*)m + le32_to_cpu(m->bytes_in_use)) { + if (a->type == AT_END) + break; + a = (ATTR_RECORD*)((char*)a + le32_to_cpu(a->length)); + }; + ntfs_log_info("-- End of attributes. --\n"); +} + +/** + * ntfsmftalloc_exit + */ +static void ntfsmftalloc_exit(void) +{ + if (success) + return; + /* If there is a base inode, close that instead of the extent inode. */ + if (base_ni) + ni = base_ni; + /* Close the inode. */ + if (ni && ntfs_inode_close(ni)) { + ntfs_log_perror("Warning: Failed to close inode 0x%llx", + (long long)ni->mft_no); + } + /* Unmount the volume. */ + if (ntfs_umount(vol, 0) == -1) + ntfs_log_perror("Warning: Could not umount %s", dev_name); +} + +/** + * main + */ +int main(int argc, char **argv) +{ + unsigned long mnt_flags, ul; + int err; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + /* Initialize opts to zero / required values. */ + memset(&opts, 0, sizeof(opts)); + /* Parse command line options. */ + parse_options(argc, argv); + utils_set_locale(); + /* Make sure the file system is not mounted. */ + if (ntfs_check_if_mounted(dev_name, &mnt_flags)) + ntfs_log_error("Failed to determine whether %s is mounted: %s\n", + dev_name, strerror(errno)); + else if (mnt_flags & NTFS_MF_MOUNTED) { + ntfs_log_error("%s is mounted.\n", dev_name); + if (!opts.force) + err_exit("Refusing to run!\n"); + ntfs_log_error("ntfsmftalloc forced anyway. Hope /etc/mtab " + "is incorrect.\n"); + } + /* Mount the device. */ + if (opts.no_action) { + ntfs_log_quiet("Running in READ-ONLY mode!\n"); + ul = NTFS_MNT_RDONLY; + } else + ul = 0; + vol = ntfs_mount(dev_name, ul); + if (!vol) + err_exit("Failed to mount %s: %s\n", dev_name, strerror(errno)); + /* Register our exit function which will unlock and close the device. */ + err = atexit(&ntfsmftalloc_exit); + if (err == -1) { + ntfs_log_error("Could not set up exit() function because atexit() " + "failed: %s Aborting...\n", strerror(errno)); + ntfsmftalloc_exit(); + exit(1); + } + if (base_mft_no != -1) { + base_ni = ntfs_inode_open(vol, base_mft_no); + if (!base_ni) + err_exit("Failed to open base inode 0x%llx: %s\n", + (long long)base_mft_no, + strerror(errno)); + } + /* Open the specified inode. */ + ni = ntfs_mft_record_alloc(vol, base_ni); + if (!ni) + err_exit("Failed to allocate mft record: %s\n", + strerror(errno)); + ntfs_log_info("Allocated %s mft record 0x%llx", base_ni ? "extent" : "base", + (long long)ni->mft_no); + if (base_ni) + ntfs_log_info(" with base mft record 0x%llx", + (long long)base_mft_no); + ntfs_log_info(".\n"); + if (!opts.quiet && opts.verbose > 1) { + ntfs_log_verbose("Dumping allocated mft record 0x%llx:\n", + (long long)ni->mft_no); + dump_mft_record(ni->mrec); + } + /* Close the (base) inode. */ + if (base_ni) + ni = base_ni; + err = ntfs_inode_close(ni); + if (err) + err_exit("Failed to close inode 0x%llx: %s\n", + (long long)ni->mft_no, strerror(errno)); + /* Unmount the volume. */ + err = ntfs_umount(vol, 0); + /* Disable our ntfsmftalloc_exit() handler. */ + success = TRUE; + if (err == -1) + ntfs_log_perror("Warning: Failed to umount %s", dev_name); + else + ntfs_log_quiet("ntfsmftalloc completed successfully.\n"); + return 0; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsmove.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsmove.c new file mode 100644 index 0000000..3cf695a --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsmove.c @@ -0,0 +1,926 @@ +/** + * ntfsmove - Part of the Linux-NTFS project. + * + * Copyright (c) 2003 Richard Russon + * Copyright (c) 2003-2005 Anton Altaparmakov + * + * This utility will move files on an NTFS volume. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif + +#include "types.h" +#include "attrib.h" +#include "utils.h" +#include "volume.h" +#include "debug.h" +#include "dir.h" +#include "bitmap.h" +#include "ntfsmove.h" +/* #include "version.h" */ +#include "logging.h" + +static const char *EXEC_NAME = "ntfsmove"; +static struct options opts; + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + ntfs_log_info("\n%s v%s (libntfs-3g) - Move files and directories on an " + "NTFS volume.\n\n", EXEC_NAME, VERSION); + ntfs_log_info("Copyright (c) 2003 Richard Russon\n"); + ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + ntfs_log_info("\nUsage: %s [options] device file\n" + "\n" + " -S --start Move to the start of the volume\n" + " -B --best Move to the best place on the volume\n" + " -E --end Move to the end of the volume\n" + " -C num --cluster num Move to this cluster offset\n" + "\n" + " -D --no-dirty Do not mark volume dirty (require chkdsk)\n" + " -n --no-action Do not write to disk\n" + " -f --force Use less caution\n" + " -h --help Print this help\n" + " -q --quiet Less output\n" + " -V --version Version information\n" + " -v --verbose More output\n\n", + EXEC_NAME); + ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char **argv) +{ + static const char *sopt = "-BC:DEfh?nqSVv"; + static const struct option lopt[] = { + { "best", no_argument, NULL, 'B' }, + { "cluster", required_argument, NULL, 'C' }, + { "end", no_argument, NULL, 'E' }, + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "no-action", no_argument, NULL, 'n' }, + { "no-dirty", no_argument, NULL, 'D' }, + { "quiet", no_argument, NULL, 'q' }, + { "start", no_argument, NULL, 'S' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + + int c = -1; + int err = 0; + int ver = 0; + int help = 0; + int levels = 0; + char *end = NULL; + + opterr = 0; /* We'll handle the errors, thank you. */ + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opts.device) { + opts.device = argv[optind-1]; + } else if (!opts.file) { + opts.file = argv[optind-1]; + } else { + opts.device = NULL; + opts.file = NULL; + err++; + } + break; + case 'B': + if (opts.location == 0) + opts.location = NTFS_MOVE_LOC_BEST; + else + opts.location = -1; + break; + case 'C': + if (opts.location == 0) { + opts.location = strtoll(optarg, &end, 0); + if (end && *end) + err++; + } else { + opts.location = -1; + } + break; + case 'D': + opts.nodirty++; + break; + case 'E': + if (opts.location == 0) + opts.location = NTFS_MOVE_LOC_END; + else + opts.location = -1; + break; + case 'f': + opts.force++; + break; + case 'h': + case '?': + if (strncmp (argv[optind-1], "--log-", 6) == 0) { + if (!ntfs_log_parse_option (argv[optind-1])) + err++; + break; + } + help++; + break; + case 'n': + opts.noaction++; + break; + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 'S': + if (opts.location == 0) + opts.location = NTFS_MOVE_LOC_START; + else + opts.location = -1; + break; + case 'V': + ver++; + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + default: + ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); + err++; + break; + } + } + + /* Make sure we're in sync with the log levels */ + levels = ntfs_log_get_levels(); + if (levels & NTFS_LOG_LEVEL_VERBOSE) + opts.verbose++; + if (!(levels & NTFS_LOG_LEVEL_QUIET)) + opts.quiet++; + + if (help || ver) { + opts.quiet = 0; + } else { + if ((opts.device == NULL) || + (opts.file == NULL)) { + if (argc > 1) + ntfs_log_error("You must specify one device and one file.\n"); + err++; + } + + if (opts.quiet && opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose at the " + "same time.\n"); + err++; + } + + if (opts.location == -1) { + ntfs_log_error("You may only specify one location option: " + "--start, --best, --end or --cluster\n"); + err++; + } else if (opts.location == 0) { + opts.location = NTFS_MOVE_LOC_BEST; + } + } + + if (ver) + version(); + if (help || err) + usage(); + + return (!err && !help && !ver); +} + +#if 0 + +/** + * ntfs_debug_runlist_dump2 - Dump a runlist. + */ +static int ntfs_debug_runlist_dump2(const runlist *rl, int abbr, char *prefix) +{ + //int abbr = 3; /* abbreviate long lists */ + int len = 0; + int i; + int res = 0; + u64 total = 0; + const char *lcn_str[5] = { "HOLE", "NOTMAP", "ENOENT", "EINVAL", "XXXX" }; + + if (!rl) { + ntfs_log_info(" Run list not present.\n"); + return 0; + } + + if (!prefix) + prefix = ""; + + if (abbr) + for (len = 0; rl[len].length; len++) ; + + ntfs_log_info("%s VCN LCN len\n", prefix); + for (i = 0; rl->length; i++, rl++) { + LCN lcn = rl->lcn; + + total += rl->length; + if (abbr) + if (len > 20) { + if ((i == abbr) && (len > (abbr*2))) + ntfs_log_info("%s ... ... ...\n", prefix); + if ((i > (abbr-1)) && (i < (len - (abbr-1)))) + continue; + } + + if (rl->vcn < -1) + res = -1; + + if (lcn < (LCN)0) { + int j = -lcn - 1; + + if ((j < 0) || (j > 4)) { + j = 4; + res = -1; + } + ntfs_log_info("%s%8lld %8s %8lld\n", prefix, + rl->vcn, lcn_str[j], rl->length); + } else + ntfs_log_info("%s%8lld %8lld %8lld\n", prefix, + rl->vcn, rl->lcn, rl->length); + } + ntfs_log_info("%s --------\n", prefix); + ntfs_log_info("%s %8lld\n", prefix, total); + ntfs_log_info("\n"); + return res; +} + +#endif /* if 0 */ + +/** + * resize_nonres_attr + */ +static int resize_nonres_attr(MFT_RECORD *m, ATTR_RECORD *a, const u32 new_size) +{ + int this_attr; + int next_attr; + int tail_size; + int file_size; + int old_size; + u8 *ptr; + + old_size = le32_to_cpu(a->length); + file_size = le32_to_cpu(m->bytes_in_use); + this_attr = p2n(a)-p2n(m); + next_attr = this_attr + le32_to_cpu(a->length); + tail_size = file_size - next_attr; + ptr = (u8*) m; + + /* + ntfs_log_info("old_size = %d\n", old_size); + ntfs_log_info("new_size = %d\n", new_size); + ntfs_log_info("file_size = %d\n", file_size); + ntfs_log_info("this_attr = %d\n", this_attr); + ntfs_log_info("next_attr = %d\n", next_attr); + ntfs_log_info("tail_size = %d\n", tail_size); + */ + + memmove(ptr + this_attr + new_size, ptr + next_attr, tail_size); + + a->length = cpu_to_le32(new_size); + m->bytes_in_use = cpu_to_le32(le32_to_cpu(m->bytes_in_use) + (new_size - old_size)); + + return 0; +} + +/** + * calc_attr_length + */ +static int calc_attr_length(ATTR_RECORD *rec, int runlength) +{ + int size; + + if (!rec) + return -1; + if (!rec->non_resident) + return -1; + + size = le16_to_cpu(rec->mapping_pairs_offset) + runlength + 7; + size &= 0xFFF8; + return size; +} + +#if 0 + +/** + * dump_runs + */ +static void dump_runs(u8 *buffer, int len) +{ + int i; + ntfs_log_info("RUN: \e[01;31m"); + + for (i = 0; i < len; i++) { + ntfs_log_info(" %02x", buffer[i]); + } + ntfs_log_info("\e[0m\n"); +} + +#endif /* if 0 */ + +/** + * find_unused + */ +static runlist * find_unused(ntfs_volume *vol, s64 size, u64 loc + __attribute__((unused)), int flags __attribute__((unused))) +{ + const int bufsize = 8192; + u8 *buffer; + int clus; + int i; + int curr = 0; + int count = 0; + s64 start = 0; + int bit = 0; + runlist *res = NULL; + + //ntfs_log_info("find_unused\n"); + buffer = malloc(bufsize); + if (!buffer) { + ntfs_log_info("!buffer\n"); + return NULL; + } + + //ntfs_log_info("looking for space for %lld clusters\n", size); + + clus = vol->lcnbmp_na->allocated_size / bufsize; + //ntfs_log_info("clus = %d\n", clus); + + for (i = 0; i < clus; i++) { + int bytes_read, j; + + bytes_read = ntfs_attr_pread(vol->lcnbmp_na, i*bufsize, + bufsize, buffer); + if (bytes_read != bufsize) { + ntfs_log_info("!read\n"); + return NULL; + } + for (j = 0; j < bufsize*8; j++) { + bit = !!test_bit(j & 7, buffer[j>>3]); + if (curr == bit) { + count++; + if ((!bit) && (count >= size)) { + //res = calloc(2, sizeof(*res)); + res = calloc(1, 4096); + if (res) { + res[0].vcn = 0; + res[0].lcn = start; + res[0].length = size; + res[1].lcn = LCN_ENOENT; + } + goto done; + } + } else { + //ntfs_log_info("%d * %d\n", curr, count); + curr = bit; + count = 1; + start = i*bufsize*8 + j; + } + } + } +done: + //ntfs_log_info("%d * %d\n", curr, count); + + free(buffer); + + if (res) { + for (i = 0; i < size; i++) { + if (utils_cluster_in_use(vol, res->lcn + i)) { + ntfs_log_info("ERROR cluster %lld in use\n", + (long long)res->lcn + i); + } + } + } else { + ntfs_log_info("failed\n"); + } + + return res; +} + +/** + * dont_move + * + * Don't let the user move: + * ANY metadata + * Any fragmented MFT records + * The boot file 'ntldr' + */ +static int dont_move(ntfs_inode *ino) +{ + static const ntfschar ntldr[6] = { + const_cpu_to_le16('n'), const_cpu_to_le16('t'), const_cpu_to_le16('l'), + const_cpu_to_le16('d'), const_cpu_to_le16('r'), const_cpu_to_le16('\0') + }; + + ATTR_RECORD *rec; + FILE_NAME_ATTR *name; + + if (utils_is_metadata(ino)) { + ntfs_log_error("metadata\n"); + return 1; + } + + rec = find_first_attribute(AT_ATTRIBUTE_LIST, ino->mrec); + if (rec) { + ntfs_log_error("attribute list\n"); + return 1; + } + + rec = find_first_attribute(AT_FILE_NAME, ino->mrec); + if (!rec) { + ntfs_log_error("extend inode\n"); + return 1; + } + + name = (FILE_NAME_ATTR*) ((u8*)rec + le16_to_cpu(rec->value_offset)); + if (ntfs_names_are_equal(ntldr, 5, name->file_name, name->file_name_length, + IGNORE_CASE, ino->vol->upcase, ino->vol->upcase_len)) { + ntfs_log_error("ntldr\n"); + return 1; + } + + return 0; +} + + +/** + * bitmap_alloc + */ +static int bitmap_alloc(ntfs_volume *vol, runlist_element *rl) +{ + int res; + + if (!rl) + return -1; + + res = ntfs_bitmap_set_run(vol->lcnbmp_na, rl->lcn, rl->length); + if (res < 0) { + ntfs_log_error("bitmap alloc returns %d\n", res); + } + + return res; +} + +/** + * bitmap_free + */ +static int bitmap_free(ntfs_volume *vol, runlist_element *rl) +{ + int res; + + if (!rl) + return -1; + + res = ntfs_bitmap_clear_run(vol->lcnbmp_na, rl->lcn, rl->length); + if (res < 0) { + ntfs_log_error("bitmap free returns %d\n", res); + } + + return res; +} + +/** + * data_copy + */ +static int data_copy(ntfs_volume *vol, runlist_element *from, runlist_element *to) +{ + int i; + u8 *buffer; + s64 res = 0; + + if (!vol || !from || !to) + return -1; + if ((from->length != to->length) || (from->lcn < 0) || (to->lcn < 0)) + return -1; + + //ntfs_log_info("data_copy: from 0x%llx to 0x%llx\n", from->lcn, to->lcn); + buffer = malloc(vol->cluster_size); + if (!buffer) { + ntfs_log_info("!buffer\n"); + return -1; + } + + for (i = 0; i < from->length; i++) { + //ntfs_log_info("read cluster at %8lld\n", from->lcn+i); + res = ntfs_pread(vol->dev, (from->lcn+i) * vol->cluster_size, + vol->cluster_size, buffer); + if (res != vol->cluster_size) { + ntfs_log_error("!read\n"); + res = -1; + break; + } + + //ntfs_log_info("write cluster to %8lld\n", to->lcn+i); + res = ntfs_pwrite(vol->dev, (to->lcn+i) * vol->cluster_size, + vol->cluster_size, buffer); + if (res != vol->cluster_size) { + ntfs_log_error("!write %lld\n", (long long)res); + res = -1; + break; + } + } + + free(buffer); + return res; +} + +/** + * move_runlist + * + * validate: + * runlists are the same size + * from in use + * to not in use + * allocate new space + * copy data + * deallocate old space + */ +static s64 move_runlist(ntfs_volume *vol, runlist_element *from, + runlist_element *to) +{ + int i; + + if (!vol || !from || !to) + return -1; + if (from->length != to->length) { + ntfs_log_error("diffsizes\n"); + return -1; + } + + if ((from->lcn < 0) || (to->lcn < 0)) { + ntfs_log_error("invalid runs\n"); + return -1; + } + + for (i = 0; i < from->length; i++) { + if (!utils_cluster_in_use(vol, from->lcn+i)) { + ntfs_log_error("from not in use\n"); + return -1; + } + } + + for (i = 0; i < to->length; i++) { + if (utils_cluster_in_use(vol, to->lcn+i)) { + ntfs_log_error("to is in use\n"); + return -1; + } + } + + if (bitmap_alloc(vol, to) < 0) { + ntfs_log_error("cannot bitmap_alloc\n"); + return -1; + } + + if (data_copy(vol, from, to) < 0) { + ntfs_log_error("cannot data_copy\n"); + return -1; + } + + if (bitmap_free(vol, from) < 0) { + ntfs_log_error("cannot bitmap_free\n"); + return -1; + } + + return 0; +} + + +/**original + * move_datarun + * > 0 Bytes moved / size to be moved + * = 0 Nothing to do + * < 0 Error + */ + +// get size of runlist +// find somewhere to put data +// backup original runlist +// move the data + +// got to get the runlist out of this function +// requires a mrec arg, not an ino (ino->mrec will do for now) +// check size of new runlist before allocating / moving +// replace one datarun with another (by hand) +static s64 move_datarun(ntfs_volume *vol, ntfs_inode *ino, ATTR_RECORD *rec, + runlist_element *run, u64 loc, int flags) +{ + runlist *from; + runlist *to; + int need_from; + int need_to; + int i; + s64 res = -1; + + // find empty space + to = find_unused(vol, run->length, loc, flags); + if (!to) { + ntfs_log_error("!to\n"); + return -1; + } + + to->vcn = run->vcn; + + // copy original runlist + from = ntfs_mapping_pairs_decompress(vol, rec, NULL); + if (!from) { + ntfs_log_info("!from\n"); + return -1; + } + + ntfs_log_info("move %lld,%lld,%lld to %lld,%lld,%lld\n", + (long long)run->vcn, (long long)run->lcn, (long long)run->length, + (long long)to->vcn, (long long)to->lcn, (long long)to->length); + + need_from = ntfs_get_size_for_mapping_pairs(vol, from, 0, INT_MAX); + ntfs_log_info("orig data run = %d bytes\n", need_from); + + //ntfs_debug_runlist_dump2(from, 5, "\t"); + + for (i = 0; to[i].length > 0; i++) { + if (from[i].vcn == run->vcn) { + from[i].lcn = to->lcn; + break; + } + } + + //ntfs_debug_runlist_dump2(from, 5, "\t"); + + need_to = ntfs_get_size_for_mapping_pairs(vol, from, 0, INT_MAX); + ntfs_log_info("new data run = %d bytes\n", need_to); + + need_from = calc_attr_length(rec, need_from); + need_to = calc_attr_length(rec, need_to); + + ntfs_log_info("Before %d, after %d\n", need_from, need_to); + + if (need_from != need_to) { + if (resize_nonres_attr(ino->mrec, rec, need_to) < 0) { + ntfs_log_info("!resize\n"); + return -1; + } + } + + res = move_runlist(vol, run, to); + if (res < 0) { + ntfs_log_error("!move_runlist\n"); + return -1; + } + + // wipe orig runs + memset(((u8*)rec) + le16_to_cpu(rec->mapping_pairs_offset), 0, need_to - le16_to_cpu(rec->mapping_pairs_offset)); + + // update data runs + ntfs_mapping_pairs_build(vol, ((u8*)rec) + le16_to_cpu(rec->mapping_pairs_offset), + need_to, from, 0, NULL); + + // commit + ntfs_inode_mark_dirty(ino); + + if (ntfs_inode_sync(ino) < 0) { + ntfs_log_info("!sync\n"); + return -1; + } + + free(from); + free(to); + return res; +} + +/** + * move_attribute - + * + * > 0 Bytes moved / size to be moved + * = 0 Nothing to do + * < 0 Error + */ +static s64 move_attribute(ntfs_volume *vol, ntfs_inode *ino, ATTR_RECORD *rec, + u64 loc, int flags) +{ + int i; + s64 res; + s64 count = 0; + runlist *runs; + + // NTFS_MOVE_LOC_BEST : assess how much space this attribute will need, + // find that space and pass the location to our children. + // Anything else we pass directly to move_datarun. + + runs = ntfs_mapping_pairs_decompress(vol, rec, NULL); + if (!runs) { + ntfs_log_error("!runs\n"); + return -1; + } + + //ntfs_debug_runlist_dump2(runs, 5, "\t"); + + //ntfs_log_info(" VCN LCN Length\n"); + for (i = 0; runs[i].length > 0; i++) { + if (runs[i].lcn == LCN_RL_NOT_MAPPED) { + continue; + } + + res = move_datarun(vol, ino, rec, runs+i, loc, flags); + //ntfs_log_info(" %8lld %8lld %8lld\n", runs[i].vcn, runs[i].lcn, runs[i].length); + if (res < 0) { + ntfs_log_error("!move_datarun\n"); + count = res; + break; + } + count += res; + } + + return count; +} + +/** + * move_file - + * + * > 0 Bytes moved / size to be moved + * = 0 Nothing to do + * < 0 Error + */ +static s64 move_file(ntfs_volume *vol, ntfs_inode *ino, u64 loc, int flags) +{ + char *buffer; + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *rec; + s64 res; + s64 count = 0; + + buffer = malloc(MAX_PATH); + if (!buffer) { + ntfs_log_error("Out of memory\n"); + return -1; + } + + utils_inode_get_name(ino, buffer, MAX_PATH); + + if (dont_move(ino)) { + ntfs_log_error("can't move\n"); + return -1; + } + + ntfs_log_info("Moving %s\n", buffer); + + // NTFS_MOVE_LOC_BEST : assess how much space all the attributes will need, + // find that space and pass the location to our children. + // Anything else we pass directly to move_attribute. + + ctx = ntfs_attr_get_search_ctx(ino, NULL); + + while ((rec = find_attribute(AT_UNUSED, ctx))) { + utils_attr_get_name(vol, rec, buffer, MAX_PATH); + ntfs_log_info("\tAttribute 0x%02x %s is ", le32_to_cpu(rec->type), buffer); + + if (rec->non_resident) { + ntfs_log_info("non-resident. Moving it.\n"); + + res = move_attribute(vol, ino, rec, loc, flags); + if (res < 0) { + count = res; + break; + } + count += res; + } else { + ntfs_log_info("resident.\n\t\tSkipping it.\n"); + } + } + + ntfs_attr_put_search_ctx(ctx); + free(buffer); + return count; +} + + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char *argv[]) +{ + ntfs_volume *vol; + ntfs_inode *inode; + int flags = 0; + int result = 1; + s64 count; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + if (!parse_options(argc, argv)) + return 1; + + utils_set_locale(); + + if (opts.noaction) + flags |= NTFS_MNT_RDONLY; + if (opts.force) + flags |= NTFS_MNT_RECOVER; + + vol = utils_mount_volume(opts.device, flags); + if (!vol) { + ntfs_log_info("!vol\n"); + return 1; + } + + inode = ntfs_pathname_to_inode(vol, NULL, opts.file); + if (!inode) { + ntfs_log_info("!inode\n"); + return 1; + } + + count = move_file(vol, inode, opts.location, 0); + if ((count > 0) && (!opts.nodirty)) { + + /* Porting note: libntfs-3g does not automatically set or clear + * dirty flags on mount/unmount. It always preserves them until + * they are explicitly changed with ntfs_volume_write_flags. + * This means that the dirty flag is possibly not set, but + * should be set. So we explicitly set it with a call to + * ntfs_volume_write_flags. */ + if(!(vol->flags & VOLUME_IS_DIRTY) && ntfs_volume_write_flags( + vol, vol->flags | VOLUME_IS_DIRTY)) { + ntfs_log_error("Error: Failed to set volume dirty " + "flag (%d (%s))!\n", errno, strerror(errno)); + } + + ntfs_log_info("Relocated %lld bytes\n", (long long)count); + } + if (count >= 0) + result = 0; + + if (result) + ntfs_log_info("failed\n"); + else + ntfs_log_info("success\n"); + + ntfs_inode_close(inode); + ntfs_umount(vol, FALSE); + return result; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsmove.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsmove.h new file mode 100644 index 0000000..ffc1519 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsmove.h @@ -0,0 +1,46 @@ +/* + * ntfsmove - Part of the Linux-NTFS project. + * + * Copyright (c) 2003 Richard Russon + * + * This utility will move files on an NTFS volume. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFSMOVE_H_ +#define _NTFSMOVE_H_ + +#include "types.h" + /* Move files to */ +#define NTFS_MOVE_LOC_START -1000 /* the first available space */ +#define NTFS_MOVE_LOC_BEST -1001 /* place big enough for entire file */ +#define NTFS_MOVE_LOC_END -1002 /* the last available space */ + +struct options { + char *device; /* Device/File to work with */ + char *file; /* File to display */ + s64 location; /* Where to place the file */ + int force; /* Override common sense */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + int noaction; /* Do not write to disk */ + int nodirty; /* Do not mark volume dirty */ +}; + +#endif /* _NTFSMOVE_H_ */ + + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsprogs.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsprogs.8.in new file mode 100644 index 0000000..c21b839 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsprogs.8.in @@ -0,0 +1,81 @@ +.\" Copyright (c) 2002\-2005 Richard Russon. +.\" Copyright (c) 2002\-2003 Anton Altaparmakov. +.\" Copyright (c) 2005\-2006 Szabolcs Szakacsits. +.\" Copyright (c) 2005\-2007 Yura Pakhuchiy. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSPROGS 8 "September 2007" "ntfs-3g @VERSION@" +.SH NAME +ntfsprogs \- tools for doing neat things with NTFS +.SH OVERVIEW +.B ntfsprogs +is a suite of NTFS utilities based around a shared library. The tools are +available for free and come with full source code. +.SH TOOLS +.PP +.BR mkntfs (8) +\- Create an NTFS filesystem. +.PP +.BR ntfscat (8) +\- Dump a file's content to the standard output. +.PP +.BR ntfsclone (8) +\- Efficiently clone, backup, restore or rescue NTFS. +.PP +.BR ntfscluster (8) +\- Locate the files which use the given sectors or clusters. +.PP +.BR ntfscmp (8) +\- Compare two NTFS filesystems and tell the differences. +.PP +.BR ntfscp (8) +\- Copy a file to an NTFS volume. +.PP +.BR ntfsfallocate (8) +\- Preallocate space to a file on an NTFS volume +.PP +.BR ntfsfix (8) +\- Check and fix some common errors, clear the LogFile and make Windows +perform a thorough check next time it boots. +.PP +.BR ntfsinfo (8) +\- Show information about NTFS or one of the files or directories within it. +.PP +.BR ntfslabel (8) +\- Show, or set, an NTFS filesystem's volume label. +.PP +.BR ntfsls (8) +\- List information about files in a directory residing on an NTFS. +.PP +.BR ntfsresize (8) +\- Resize NTFS without losing data. +.PP +.BR ntfsrecover (8) +\- Recover updates committed by Windows on an NTFS volume. +.PP +.BR ntfstruncate (8) +\- Truncate a file on an NTFS volume. +.PP +.BR ntfsundelete (8) +\- Recover deleted files from NTFS. +.PP +.BR ntfswipe (8) +\- Overwrite unused space on an NTFS volume. +.SH AUTHORS +.PP +The tools were written by Anton Altaparmakov, Carmelo Kintana, Cristian Klein, +Erik Sornes, Giang Nguyen, Holger Ohmacht, Lode Leroy, Matthew J. Fanto, Per +Olofsson, Richard Russon, Szabolcs Szakacsits, Yura Pakhuchiy and Yuval Fledel. +.SH AVAILABILITY +The +.B ntfsprogs +are part of the +.B ntfs-3g +package which can be downloaded from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfs\-3g (8) + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsrecover.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsrecover.8.in new file mode 100644 index 0000000..e4eca85 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsrecover.8.in @@ -0,0 +1,172 @@ +.\" Copyright (c) 2015 Jean-Pierre Andre +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSRECOVER 8 "September 2015" "ntfs-3g @VERSION@" +.SH NAME +ntfsrecover \- Recover updates committed by Windows on an NTFS volume +.SH SYNOPSIS +\fBntfsrecover\fR [\fIoptions\fR] \fIdevice\fR +.SH DESCRIPTION +.B ntfsrecover +applies to the metadata the updates which were requested on Windows but could +not be completed because they were interrupted by some event such as a power +failure, a hardware crash, a software crash or the device being unplugged. +Doing so, the file system is restored to a consistent state, however updates +to user data may still be lost. + +Updating the file system generally requires updating several records which +should all be made for the file system to be kept consistent. For instance, +creating a new file requires reserving an inode number (set a bit in a bit +map), creating a file record (store the file name and file attributes), and +registering the file in a directory (locate the file from some path). When an +unfortunate event occurs, and one of these updates could be done but not all +of them, the file system is left inconsistent. + +A group of updates which have all to be done to preserve consistency is +called a transaction, and the end of updates within a transaction is called +the commitment of the transaction. + +To protect from unfortunate events, Windows first logs in a special file all +the metadata update requests without applying any, until the commitment is +known. If the event occurs before the commitment, no update has been made and +the file system is consistent. If the event occurs after the update, the log +file can be analyzed later and the transactions which were committed can be +executed again, thus restoring the integrity of the file system. + +.B ntfsrecover +similarly examines the log file and applies the updates within committed +transactions which could not be done by Windows. + +Currently, ntfs-3g does not log updates, so +.B ntfsrecover +cannot be used to restore consistency after an unfortunate event occurred +while the file system was updated by Linux. + +.SH OPTIONS +Below is a summary of all the options that +.B ntfsrecover +accepts. The normal usage is to use no option at all, as most of these +options are oriented towards developers needs. + +Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-bv +is equivalent to +.BR "\-b \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-b\fR, \fB\-\-backward\fR +Examine the actions described in the logfile backward from the latest one to +the earliest one without applying any update. This may encompass records +generated during several sessions, and when Windows is restarted, it often +does not restart writing where it ended the previous session, so this leads +to errors and bad sequencing when examining the full log file. +.TP +\fB\-c\fR, \fB\-\-clusters\fR \fBCLUSTER-RANGE\fR +Restrict the output generated when using options -b -f -u -p +to the actions operating on a cluster within the given cluster range. +CLUSTER-RANGE is defined by the first and last cluster numbers separated +by a hyphen, for instance 100-109 or 0x3e8-0x3ff. A single number means +restricting to a single cluster. The first four log blocks have a special +role and they are always shown. +.TP +\fB\-f\fR, \fB\-\-forward\fR \fBNUM\fR +Examine the actions described in the logfile forward from the first one to +the last one without applying any update. As the log file is reused +circularly, the first one is generally not the earliest. Moreover when +Windows is restarted, it often does not restart writing where it ended the +previous sessions, and this leads to errors when examining a log file +generated during several sessions. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show some help information. +.TP +\fB\-k\fR, \fB\-\-kill\-fast\-restart\fR +When Windows has been interrupted with fast restart mode activated, +part of pending changes are kept in the Windows cache and only the same +Windows version can recover them. This option can be used to apply the +changes recorded in the log file and drop the ones in the Windows cache. +This is dangerous and may cause loss of data. +.TP +\fB\-n\fR, \fB\-\-no-action\fR +Do not apply any modification, useful when using the options -p, -s or -u. +.TP +\fB\-p\fR, \fB\-\-play\fR \fBCOUNT\fR +Undo COUNT transaction sets and redo a single one, a transaction set being +all transactions between two consecutive checkpoints. This is useful for +replaying some transaction in the past. As a few actions are not undoable, +this is not always possible. +.TP +\fB\-r\fR, \fB\-\-range\fR \fBBLOCK-RANGE\fR +Examine the actions described in the logfile forward restricted to the +requested log file block range without applying any update. The first four +log blocks have a special role and they are always examined. +.TP +\fB\-s\fR, \fB\-\-sync\fR +Sync the file system by applying the committed actions which have not +been synced previously. This is the default option, used when none of +the options -n, -f, -r, -p and -u are present. + +The option -s can be repeated to request applying the committed actions +mentioned in the obsolete restart page. This is useful for testing the +situations where the latest restart page cannot be read though it can +actually be read. +.TP +\fB\-t\fR, \fB\-\-transactions\fR \fBCOUNT\fR +Display the transaction parameters when examining the log file with one +of the options --forward, --backward or --range. +.TP +\fB\-u\fR, \fB\-\-undo\fR \fBCOUNT\fR +Undo COUNT transaction sets, thus resetting the file system to some +checkpoint in the past, a transaction set being all transactions between +two consecutive checkpoints. As a few actions are not undoable, this is +not always possible. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Display more debug/warning/error messages. This option may be used twice +to display even more information. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license of +.BR ntfsrecover . +.SH EXAMPLES +Sync an NTFS volume on /dev/sda1. +.RS +.sp +.B ntfsrecover -s /dev/sda1 +.sp +.RE +Display all actions which updated a cluster in range 100 to 119 : +.RS +.sp +.B ntfsrecover --verbose --backward --clusters=100-119 /dev/sda1 +.sp +.RE +.SH BUGS +If you find a bug please send an email describing the problem to the +development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfsrecover +was written by Jean-Pierre Andre +.SH AVAILABILITY +.B ntfsrecover +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfs-3g (8), +.BR ntfsfix (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsrecover.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsrecover.c new file mode 100644 index 0000000..1936649 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsrecover.c @@ -0,0 +1,4435 @@ +/* + * Process log data from an NTFS partition + * + * Copyright (c) 2012-2017 Jean-Pierre Andre + * + * This program examines the Windows log file of an ntfs partition + * and plays the committed transactions in order to restore the + * integrity of metadata. + * + * It can also display the contents of the log file in human-readable + * text, either from a full partition or from the log file itself. + * + * + * History + * + * Sep 2012 + * - displayed textual logfile contents forward + * + * Nov 2014 + * - decoded multi-page log records + * - displayed textual logfile contents backward + * + * Nov 2015 + * - made a general cleaning and redesigned as an ntfsprogs + * - applied committed actions from logfile + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#define BASEBLKS 4 /* number of special blocks (always shown) */ +#define BASEBLKS2 34 /* number of special blocks when version >= 2.0 */ +#define RSTBLKS 2 /* number of restart blocks */ +#define BUFFERCNT 64 /* number of block buffers - a power of 2 */ +#define NTFSBLKLTH 512 /* usa block size */ +#define SHOWATTRS 20 /* max attrs shown in a dump */ +#define SHOWLISTS 10 /* max lcn or lsn shown in a list */ +#define BLOCKBITS 9 /* This is only used to read the restart page */ +#define MAXEXCEPTION 10 /* Max number of exceptions (option -x) */ +#define MINRECSIZE 48 /* Minimal log record size */ +#define MAXRECSIZE 65536 /* Maximal log record size (seen > 56000) */ + +#include "config.h" + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_MALLOC_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif + +#include "types.h" +#include "endians.h" +#include "support.h" +#include "layout.h" +#include "param.h" +#include "ntfstime.h" +#include "device_io.h" +#include "device.h" +#include "logging.h" +#include "runlist.h" +#include "mft.h" +#include "inode.h" +#include "attrib.h" +#include "bitmap.h" +#include "index.h" +#include "volume.h" +#include "unistr.h" +#include "mst.h" +#include "logfile.h" +#include "ntfsrecover.h" +#include "utils.h" +#include "misc.h" + +typedef struct { + ntfs_volume *vol; + FILE *file; + struct ACTION_RECORD *firstaction; + struct ACTION_RECORD *lastaction; +} CONTEXT; + +typedef enum { T_OK, T_ERR, T_DONE } TRISTATE; + +RESTART_PAGE_HEADER log_header; +RESTART_AREA restart; +LOG_CLIENT_RECORD client; +u32 clustersz = 0; +int clusterbits; +u32 blocksz; +int blockbits; +int log_major; +u16 bytespersect; +u64 mftlcn; +u32 mftrecsz; +int mftrecbits; +u32 mftcnt; /* number of entries */ +ntfs_inode *log_ni; +ntfs_attr *log_na; +u64 logfilelcn; +u32 logfilesz; /* bytes */ +u64 redos_met; +u64 committed_lsn; +u64 synced_lsn; +u64 latest_lsn; +u64 restart_lsn; +u64 offset_mask; /* block number in an lsn */ +unsigned long firstblk; /* first block to dump (option -r) */ +unsigned long lastblk; /* last block to dump (option -r) */ +u64 firstlcn; /* first block to dump (option -c) */ +u64 lastlcn; /* last block to dump (option -c) */ +BOOL optb; /* show the log backward */ +BOOL optc; /* restrict to cluster range */ +BOOL optd; /* device argument present*/ +BOOL opth; /* show help */ +BOOL opti; /* show invalid (stale) records */ +BOOL optf; /* show full log */ +BOOL optk; /* kill fast restart */ +BOOL optn; /* do not apply modifications */ +BOOL optp; /* count of transaction sets to play */ +BOOL optr; /* show a range of blocks */ +int opts; /* sync the file system */ +BOOL optt; /* show transactions */ +BOOL optu; /* count of transaction sets to undo */ +int optv; /* verbose */ +int optV; /* version */ +int optx[MAXEXCEPTION + 1]; +struct ATTR **attrtable; +unsigned int actionnum; +unsigned int attrcount; +unsigned int playcount; +unsigned int playedactions; // change the name +unsigned int redocount; +unsigned int undocount; +struct BUFFER *buffer_table[BASEBLKS + BUFFERCNT]; +unsigned int redirect[BASEBLKS2]; + +static const le16 SDS[4] = { + const_cpu_to_le16('$'), const_cpu_to_le16('S'), + const_cpu_to_le16('D'), const_cpu_to_le16('S') +} ; + +static const le16 I30[4] = { + const_cpu_to_le16('$'), const_cpu_to_le16('I'), + const_cpu_to_le16('3'), const_cpu_to_le16('0') +} ; + +/* + * Byte address of a log block + */ + +static s64 loclogblk(CONTEXT *ctx, unsigned int blk) +{ + s64 loc; + LCN lcn; + + if (ctx->vol) { + lcn = ntfs_attr_vcn_to_lcn(log_na, + ((s64)blk << blockbits) >> clusterbits); + loc = lcn << clusterbits; + } else { + if (((s64)blk << blockbits) >= logfilesz) + loc = -1; + else + loc = (logfilelcn << clusterbits) + + ((s64)blk << blockbits); + } + return (loc); +} + +/* + * Deprotect a block + * Only to be used for log buffers + * + * Returns 0 if block was found correct + */ + +static int replaceusa(struct BUFFER *buffer, unsigned int lth) +{ + char *buf; + RECORD_PAGE_HEADER *record; + unsigned int j; + BOOL err; + unsigned int used; + unsigned int xusa, nusa; + + err = FALSE; + /* Restart blocks have no protection */ + if (buffer->num >= RSTBLKS) { + /* Do not check beyond used sectors */ + record = &buffer->block.record; + used = blocksz; + xusa = le16_to_cpu(record->usa_ofs); + nusa = le16_to_cpu(record->usa_count); + if (xusa && nusa + && ((xusa + 1) < lth) + && ((nusa - 1)*NTFSBLKLTH == lth)) { + buf = buffer->block.data; + for (j=1; (j> 1; + if (key < attrtable[mid]->key) + high = mid; + else + if (key > attrtable[mid]->key) + low = mid; + else { + low = mid; + high = mid + 1; + } + } + } + if ((low < attrcount) && (attrtable[low]->key == key)) { + pa = attrtable[low]; + if (pa->namelen < lth) { + pa = (struct ATTR*)realloc(pa, + sizeof(struct ATTR) + lth); + attrtable[low] = pa; + } + } else { + mid = low + 1; + if (!low && attrcount && (attrtable[0]->key > key)) + mid = 0; + pa = (struct ATTR*)malloc(sizeof(struct ATTR) + lth); + if (pa) { + if (attrcount++) { + old = attrtable; + attrtable = (struct ATTR**)realloc(attrtable, + attrcount*sizeof(struct ATTR*)); + if (attrtable) { + high = attrcount; + while (--high > mid) + attrtable[high] + = attrtable[high - 1]; + attrtable[mid] = pa; + } else + attrtable = old; + } else { + attrtable = (struct ATTR**) + malloc(sizeof(struct ATTR*)); + attrtable[0] = pa; + } + pa->key = key; + pa->namelen = 0; + pa->type = const_cpu_to_le32(0); + pa->inode = 0; + } + } + return (pa); +} + +/* + * Read blocks in a circular buffer + * + * returns NULL if block cannot be read or it is found bad + * otherwise returns the full unprotected block data + */ + +static const struct BUFFER *read_buffer(CONTEXT *ctx, unsigned int num) +{ + struct BUFFER *buffer; + BOOL got; + int k; + unsigned int rnum; + + /* + * The first four blocks are stored apart, to make + * sure pages 2 and 3 and the page which is logically + * before them can be accessed at the same time. + * (Only two blocks are stored apart if version >= 2.0) + * Also, block 0 is smaller because it has to be read + * before the block size is known. + * Note : the last block is supposed to have an odd + * number, and cannot be overwritten by block 4 (or 34 + * if version >= 2.0) which follows logically. + */ + if ((num < RSTBLKS) + || ((log_major < 2) && (num < BASEBLKS))) + buffer = buffer_table[num + BUFFERCNT]; + else + buffer = buffer_table[num & (BUFFERCNT - 1)]; + if (buffer && (buffer->size < blocksz)) { + free(buffer); + buffer = (struct BUFFER*)NULL; + } + if (!buffer) { + buffer = (struct BUFFER*) + malloc(sizeof(struct BUFFER) + blocksz); + buffer->size = blocksz; + buffer->rnum = num + 1; /* forced to being read */ + buffer->safe = FALSE; + if (num < BASEBLKS) + buffer_table[num + BUFFERCNT] = buffer; + else + buffer_table[num & (BUFFERCNT - 1)] = buffer; + } + rnum = num; + if (log_major >= 2) { + for (k=RSTBLKS; krnum != rnum)) { + buffer->num = num; + buffer->rnum = rnum; + if (ctx->vol) + got = (ntfs_attr_pread(log_na,(u64)rnum << blockbits, + blocksz, buffer->block.data) == blocksz); + else + got = !fseek(ctx->file, loclogblk(ctx, rnum), 0) + && (fread(buffer->block.data, blocksz, + 1, ctx->file) == 1); + if (got) { + char *data = buffer->block.data; + buffer->headsz = sizeof(RECORD_PAGE_HEADER) + + ((2*getle16(data,6) - 1) | 7) + 1; + buffer->safe = !replaceusa(buffer, blocksz); + } else { + buffer->safe = FALSE; + fprintf(stderr,"** Could not read block %d\n", rnum); + } + } + return (buffer && buffer->safe ? buffer : (const struct BUFFER*)NULL); +} + +void hexdump(const char *buf, unsigned int lth) +{ + unsigned int i,j,k; + + for (i=0; i 0x20) && (buf[j] < 0x7f)) + printf("%c",buf[j]); + else + printf("."); + printf("\n"); + } +} + +/* + * Display a date + */ + +static void showdate(const char *text, le64 lestamp) +{ + time_t utime; + struct tm *ptm; + s64 stamp; + const char *months[] + = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } ; + + stamp = le64_to_cpu(lestamp); + if ((stamp < ((2147000000 + 134774*86400LL)*10000000LL)) + && (stamp > ((-2147000000 + 134774*86400LL)*10000000LL))) { + /* date within traditional Unix limits */ + utime = stamp/10000000 - 134774*86400LL; + ptm = gmtime(&utime); + printf("%s %02d %3s %4d %2d:%02d:%02d UTC\n", + text, + ptm->tm_mday,months[ptm->tm_mon],ptm->tm_year+1900, + ptm->tm_hour,ptm->tm_min,ptm->tm_sec); + } else { + u32 days; + unsigned int year; + int mon; + int cnt; + + days = stamp/(86400*10000000LL); + year = 1601; + /* periods of 400 years */ + cnt = days/146097; + days -= 146097*cnt; + year += 400*cnt; + /* periods of 100 years */ + cnt = (3*days + 3)/109573; + days -= 36524*cnt; + year += 100*cnt; + /* periods of 4 years */ + cnt = days/1461; + days -= 1461L*cnt; + year += 4*cnt; + /* periods of a single year */ + cnt = (3*days + 3)/1096; + days -= 365*cnt; + year += cnt; + + if ((!(year % 100) ? (year % 400) : (year % 4)) + && (days > 58)) days++; + if (days > 59) { + mon = (5*days + 161)/153; + days -= (153*mon - 162)/5; + } else { + mon = days/31 + 1; + days -= 31*(mon - 1) - 1; + } +if (mon > 12) +{ +printf("** Bad day stamp %lld days %lu mon %d year %u\n", +(long long)stamp,(unsigned long)days,mon,year); +} + printf("%s %02u %3s %4u\n",text, + (unsigned int)days,months[mon-1],(unsigned int)year); + } +} + +void showname(const char *prefix, const char *name, int cnt) +{ + const le16 *n; + int i; + int c; + + printf("%s",prefix); + n = (const le16*)name; + for (i=0; (i> 6) + 0xc0, + (c & 63) + 0x80); + else + printf("%c%c%c", + (c >> 12) + 0xe0, + ((c >> 6) & 63) + 0x80, + (c & 63) + 0x80); + } + printf("\n"); +} + +static const char *commitment(u64 lsn) +{ + const char *commit; + s64 diff; + + /* Computations assume lsn could wraparound, they probably never do */ + diff = lsn - synced_lsn; + if (diff <= 0) + commit = "synced"; + else { + diff = lsn - committed_lsn; + if (diff <= 0) + commit = "committed"; + else { + /* may find lsn from older session */ + diff = lsn - latest_lsn; + if (diff <= 0) + commit = "*uncommitted*"; + else + commit = "*stale*"; + } + } + return (commit); +} + +const char *actionname(int op) +{ + static char buffer[24]; + const char *p; + + switch (op) { + case Noop : + p = "Noop"; + break; + case CompensationlogRecord : + p = "CompensationlogRecord"; + break; + case InitializeFileRecordSegment : + p = "InitializeFileRecordSegment"; + break; + case DeallocateFileRecordSegment : + p = "DeallocateFileRecordSegment"; + break; + case WriteEndofFileRecordSegment : + p = "WriteEndofFileRecordSegment"; + break; + case CreateAttribute : + p = "CreateAttribute"; + break; + case DeleteAttribute : + p = "DeleteAttribute"; + break; + case UpdateResidentValue : + p = "UpdateResidentValue"; + break; + case UpdateNonResidentValue : + p = "UpdateNonResidentValue"; + break; + case UpdateMappingPairs : + p = "UpdateMappingPairs"; + break; + case DeleteDirtyClusters : + p = "DeleteDirtyClusters"; + break; + case SetNewAttributeSizes : + p = "SetNewAttributeSizes"; + break; + case AddIndexEntryRoot : + p = "AddIndexEntryRoot"; + break; + case DeleteIndexEntryRoot : + p = "DeleteIndexEntryRoot"; + break; + case AddIndexEntryAllocation : + p = "AddIndexEntryAllocation"; + break; + case DeleteIndexEntryAllocation : + p = "DeleteIndexEntryAllocation"; + break; + case WriteEndOfIndexBuffer : + p = "WriteEndOfIndexBuffer"; + break; + case SetIndexEntryVcnRoot : + p = "SetIndexEntryVcnRoot"; + break; + case SetIndexEntryVcnAllocation : + p = "SetIndexEntryVcnAllocation"; + break; + case UpdateFileNameRoot : + p = "UpdateFileNameRoot"; + break; + case UpdateFileNameAllocation : + p = "UpdateFileNameAllocation"; + break; + case SetBitsInNonResidentBitMap : + p = "SetBitsInNonResidentBitMap"; + break; + case ClearBitsInNonResidentBitMap : + p = "ClearBitsInNonResidentBitMap"; + break; + case HotFix : + p = "HotFix"; + break; + case EndTopLevelAction : + p = "EndTopLevelAction"; + break; + case PrepareTransaction : + p = "PrepareTransaction"; + break; + case CommitTransaction : + p = "CommitTransaction"; + break; + case ForgetTransaction : + p = "ForgetTransaction"; + break; + case OpenNonResidentAttribute : + p = "OpenNonResidentAttribute"; + break; + case OpenAttributeTableDump : + p = "OpenAttributeTableDump"; + break; + case AttributeNamesDump : + p = "AttributeNamesDump"; + break; + case DirtyPageTableDump : + p = "DirtyPageTableDump"; + break; + case TransactionTableDump : + p = "TransactionTableDump"; + break; + case UpdateRecordDataRoot : + p = "UpdateRecordDataRoot"; + break; + case UpdateRecordDataAllocation : + p = "UpdateRecordDataAllocation"; + break; + case Win10Action35 : + p = "Win10Action35"; + break; + case Win10Action36 : + p = "Win10Action36"; + break; + case Win10Action37 : + p = "Win10Action37"; + break; + default : + sprintf(buffer,"*Unknown-Action-%d*",op); + p = buffer; + break; + } + return (p); +} + +static const char *attrname(unsigned int key) +{ + static char name[256]; + const char *p; + struct ATTR *pa; + unsigned int i; + + if ((key <= 65535) && !(key & 3)) { + pa = getattrentry(key,0); + if (pa) { + if (!pa->namelen) + p = "Unnamed"; + else { + p = name; + /* Assume ascii for now */ + for (i=0; 2*inamelen; i++) + name[i] = le16_to_cpu(pa->name[i]); + name[i] = 0; + } + } else + p = "Undefined"; + } else + p = "Invalid"; + return (p); +} + +int fixnamelen(const char *name, int len) +{ + int i; + + i = 0; + while ((i < len) && (name[i] || name[i + 1])) + i += 2; + return (i); +} + +const char *mftattrname(ATTR_TYPES attr) +{ + static char badattr[24]; + const char *p; + + switch (attr) { + case AT_STANDARD_INFORMATION : + p = "Standard-Information"; + break; + case AT_ATTRIBUTE_LIST : + p = "Attribute-List"; + break; + case AT_FILE_NAME : + p = "Name"; + break; + case AT_OBJECT_ID : + p = "Volume-Version"; + break; + case AT_SECURITY_DESCRIPTOR : + p = "Security-Descriptor"; + break; + case AT_VOLUME_NAME : + p = "Volume-Name"; + break; + case AT_VOLUME_INFORMATION : + p = "Volume-Information"; + break; + case AT_DATA : + p = "Data"; + break; + case AT_INDEX_ROOT : + p = "Index-Root"; + break; + case AT_INDEX_ALLOCATION : + p = "Index-Allocation"; + break; + case AT_BITMAP : + p = "Bitmap"; + break; + case AT_REPARSE_POINT : + p = "Reparse-Point"; + break; + case AT_EA_INFORMATION : + p = "EA-Information"; + break; + case AT_EA : + p = "EA"; + break; + case AT_PROPERTY_SET : + p = "Property-Set"; + break; + case AT_LOGGED_UTILITY_STREAM : + p = "Logged-Utility-Stream"; + break; + case AT_END : + p = "End"; + break; + default : + sprintf(badattr,"*0x%x-Unknown*",attr); + p = badattr; + break; + } + return (p); +} + +static void showattribute(const char *prefix, const struct ATTR *pa) +{ + if (pa) { + if (pa->type) { + printf("%sattr 0x%x : inode %lld type %s", + prefix, pa->key, (long long)pa->inode, + mftattrname(pa->type)); + if (pa->namelen) + showname(" name ",(const char*)pa->name, + pa->namelen/2); + else + printf("\n"); + } else { + if (pa->namelen) { + printf("%sattr 0x%x : type Unknown", + prefix, pa->key); + showname(" name ",(const char*)pa->name, + pa->namelen/2); + } else + printf("%s(definition of attr 0x%x not met)\n", + prefix, pa->key); + } + } +} + +/* + * Determine if an action acts on the MFT + */ + +static BOOL acts_on_mft(int op) +{ + BOOL onmft; + + /* A few actions may have to be added to the list */ + switch (op) { + case InitializeFileRecordSegment : + case DeallocateFileRecordSegment : + case CreateAttribute : + case DeleteAttribute : + case UpdateResidentValue : + case UpdateMappingPairs : + case SetNewAttributeSizes : + case AddIndexEntryRoot : + case DeleteIndexEntryRoot : + case UpdateFileNameRoot : + case WriteEndofFileRecordSegment : + case Win10Action37 : + onmft = TRUE; + break; + default : + onmft = FALSE; + break; + } + return (onmft); +} + +u32 get_undo_offset(const LOG_RECORD *logr) +{ + u32 offset; + + if (logr->lcns_to_follow) + offset = 0x30 + le16_to_cpu(logr->undo_offset); + else + offset = 0x28 + le16_to_cpu(logr->undo_offset); + return (offset); +} + +u32 get_redo_offset(const LOG_RECORD *logr) +{ + u32 offset; + + if (logr->lcns_to_follow) + offset = 0x30 + le16_to_cpu(logr->redo_offset); + else + offset = 0x28 + le16_to_cpu(logr->redo_offset); + return (offset); +} + +u32 get_extra_offset(const LOG_RECORD *logr) +{ + u32 uoffset; + u32 roffset; + + roffset = get_redo_offset(logr) + + le16_to_cpu(logr->redo_length); + uoffset = get_undo_offset(logr) + + le16_to_cpu(logr->undo_length); + return ((((uoffset > roffset ? uoffset : roffset) - 1) | 7) + 1); +} + +static BOOL likelyop(const LOG_RECORD *logr) +{ + BOOL likely; + + switch (logr->record_type) { + case LOG_STANDARD : /* standard record */ + /* Operations in range 0..LastAction-1, can be both null */ + likely = ((unsigned int)le16_to_cpu(logr->redo_operation) + < LastAction) + && ((unsigned int)le16_to_cpu(logr->undo_operation) + < LastAction) + /* Offsets aligned to 8 bytes */ + && !(le16_to_cpu(logr->redo_offset) & 7) + && !(le16_to_cpu(logr->undo_offset) & 7) + /* transaction id must not be null */ + && logr->transaction_id + /* client data length aligned to 8 bytes */ + && !(le32_to_cpu(logr->client_data_length) & 7) + /* client data length less than 64K (131K ?) */ + && (le32_to_cpu(logr->client_data_length) < MAXRECSIZE) + /* if there is redo data, offset must be >= 0x28 */ + && (!le16_to_cpu(logr->redo_length) + || ((unsigned int)le16_to_cpu(logr->redo_offset) >= 0x28)) + /* if there is undo data, offset must be >= 0x28 */ + && (!le16_to_cpu(logr->undo_length) + || ((unsigned int)le16_to_cpu(logr->undo_offset) >= 0x28)); + /* undo data and redo data should be contiguous when both present */ + if (likely && logr->redo_length && logr->undo_length) { + /* undo and redo data may be the same when both present and same size */ + if (logr->undo_offset == logr->redo_offset) { + if (logr->redo_length != logr->undo_length) + likely = FALSE; + } else { + if (le16_to_cpu(logr->redo_offset) + < le16_to_cpu(logr->undo_offset)) { + /* undo expected just after redo */ + if ((((le16_to_cpu(logr->redo_offset) + + le16_to_cpu(logr->redo_length) + - 1) | 7) + 1) + != le16_to_cpu(logr->undo_offset)) + likely = FALSE; + } else { + /* redo expected just after undo */ + if ((((le16_to_cpu(logr->undo_offset) + + le16_to_cpu(logr->undo_length) + - 1) | 7) + 1) + != le16_to_cpu(logr->redo_offset)) + likely = FALSE; + } + } + } + break; + case LOG_CHECKPOINT : /* check-point */ + /* + * undo and redo operations are null + * or CompensationlogRecord with no data + */ + likely = (!logr->redo_operation + || ((logr->redo_operation == const_cpu_to_le16(1)) + && !logr->redo_length)) + && (!logr->undo_operation + || ((logr->undo_operation == const_cpu_to_le16(1)) + && !logr->undo_length)) + /* transaction id must be null */ + && !logr->transaction_id + /* client_data_length is 0x68 or 0x70 (Vista and subsequent) */ + && ((le32_to_cpu(logr->client_data_length) == 0x68) + || (le32_to_cpu(logr->client_data_length) == 0x70)); + break; + default : + likely = FALSE; + break; + } + return (likely); +} + +/* + * Search for a likely record in a block + * + * Must not be used when syncing. + * + * Returns 0 when not found + */ + +static u16 searchlikely(const struct BUFFER *buf) +{ + const LOG_RECORD *logr; + const char *data; + u16 k; + + if (opts) + printf("** Error : searchlikely() used for syncing\n"); + data = buf->block.data; + k = buf->headsz; + logr = (const LOG_RECORD*)&data[k]; + if (!likelyop(logr)) { + do { + k += 8; + logr = (const LOG_RECORD*)&data[k]; + } while ((k <= (blocksz - LOG_RECORD_HEAD_SZ)) + && !likelyop(logr)); + if (k > (blocksz - LOG_RECORD_HEAD_SZ)) + k = 0; + } + return (k); +} + +/* + * From a previous block, determine the location of first record + * + * The previous block must have the beginning of an overlapping + * record, and the current block must have the beginning of next + * record (which can overlap on next blocks). + * The argument "skipped" is the number of blocks in-between. + * + * Note : the overlapping record from previous block does not reach + * the current block when it ends near the end of the last skipped block. + * + * Returns 0 if some bad condition is found + * Returns near blocksz when there is no beginning of record in + * the current block + */ + +static u16 firstrecord(int skipped, const struct BUFFER *buf, + const struct BUFFER *prevbuf) +{ + const RECORD_PAGE_HEADER *rph; + const RECORD_PAGE_HEADER *prevrph; + const LOG_RECORD *logr; + const char *data; + const char *prevdata; + u16 k; + u16 blkheadsz; + s32 size; + + rph = &buf->block.record; + data = buf->block.data; + if (prevbuf) { + prevrph = &prevbuf->block.record; + prevdata = prevbuf->block.data; + blkheadsz = prevbuf->headsz; + /* From previous page, determine where the current one starts */ + k = le16_to_cpu(prevrph->next_record_offset); + /* a null value means there is no full record in next block */ + if (!k) + k = blkheadsz; + } else + k = 0; + /* Minimal size is apparently 48 : offset of redo_operation */ + if (k && ((blocksz - k) >= LOG_RECORD_HEAD_SZ)) { + logr = (const LOG_RECORD*)&prevdata[k]; + if (!logr->client_data_length) { + /* + * Sometimes the end of record is free space. + * This apparently means reaching the end of + * a previous session, and must be considered + * as an error. + * We however tolerate this, unless syncing + * is requested. + */ + printf("* Reaching free space at end of block %d\n", + (int)prevbuf->num); + /* As a consequence, there cannot be skipped blocks */ + if (skipped) { + printf("*** Inconsistency : blocks skipped after free space\n"); + k = 0; /* error returned */ + } + if (opts) + k = 0; + else { + k = searchlikely(buf); + printf("* Skipping over free space\n"); + } + } else { + size = le32_to_cpu(logr->client_data_length) + + LOG_RECORD_HEAD_SZ; + if ((size < MINRECSIZE) || (size > MAXRECSIZE)) { + printf("** Bad record size %ld in block %ld" + " offset 0x%x\n", + (long)size,(long)prevbuf->num,(int)k); + k = blkheadsz; + } else { + if ((int)(blocksz - k) >= size) + printf("*** Inconsistency : the final" + " record does not overlap\n"); + k += size - (blocksz - blkheadsz)*(skipped + 1); + } + if ((k <= blkheadsz) + && (k > (blkheadsz - LOG_RECORD_HEAD_SZ))) { + /* There were not enough space in the last skipped block */ + k = blkheadsz; + } else { + if (optv + && ((blocksz - k) < LOG_RECORD_HEAD_SZ)) { + /* Not an error : just no space */ + printf("No minimal record space\n"); + } + if (optv >= 2) + printf("Overlapping record from block %d," + " starting at offset 0x%x\n", + (int)prevbuf->num,(int)k); + } + } + } else { + k = buf->headsz; + if (optv >= 2) { + if (prevbuf) + printf("No minimal record from block %d," + " starting at offset 0x%x\n", + (int)prevbuf->num, (int)k); + else + printf("No block before %d," + " starting at offset 0x%x\n", + (int)buf->num, (int)k); + } + } + /* + * In a wraparound situation, there is frequently no + * match... because there were no wraparound. + * Return an error if syncing is requested, otherwise + * try to find a starting record. + */ + if (k && prevbuf && (prevbuf->num > buf->num)) { + logr = (const LOG_RECORD*)&data[k]; + /* Accept reaching the end with no record beginning */ + if ((k != le16_to_cpu(rph->next_record_offset)) + && !likelyop(logr)) { + if (opts) { + k = 0; + printf("** Could not wraparound\n"); + } else { + k = searchlikely(buf); + printf("* Skipping over bad wraparound\n"); + } + } + } + return (k); +} + +/* + * Find the block which defines the first record in current one + * + * Either the wanted block has the beginning of a record overlapping + * on current one, or it ends in such as there is no space for an + * overlapping one. + * + * Returns 0 if the previous block cannot be determined. + */ + +static const struct BUFFER *findprevious(CONTEXT *ctx, const struct BUFFER *buf) +{ + const struct BUFFER *prevbuf; + const struct BUFFER *savebuf; + const RECORD_PAGE_HEADER *rph; + int skipped; + int prevblk; + BOOL prevmiddle; + BOOL error; + u16 endoff; + + error = FALSE; + prevblk = buf->num; + savebuf = (struct BUFFER*)NULL; + skipped = 0; + do { + prevmiddle = FALSE; + if (prevblk > (log_major < 2 ? BASEBLKS : BASEBLKS2)) + prevblk--; + else + if (prevblk == (log_major < 2 ? BASEBLKS : BASEBLKS2)) + prevblk = (logfilesz >> blockbits) - 1; + else { + rph = &buf->block.record; + if (log_major < 2) + prevblk = (sle64_to_cpu( + rph->copy.file_offset) + >> blockbits) - 1; + else + prevblk = (sle64_to_cpu( + rph->copy.last_lsn) + & offset_mask) + >> (blockbits - 3); + /* + * If an initial block leads to block 4, it + * can mean the last block or no previous + * block at all. Using the last block is safer, + * its lsn will indicate whether it is stale. + */ + if (prevblk + < (log_major < 2 ? BASEBLKS : BASEBLKS2)) + prevblk = (logfilesz >> blockbits) - 1; + } + /* No previous block if the log only consists of block 2 or 3 */ + if (prevblk < BASEBLKS) { + prevbuf = (struct BUFFER*)NULL; + error = TRUE; /* not a real error */ + } else { + prevbuf = read_buffer(ctx, prevblk); + if (prevbuf) { + rph = &prevbuf->block.record; + prevmiddle = !(rph->flags + & const_cpu_to_le32(1)) + || !rph->next_record_offset; + if (prevmiddle) { + savebuf = prevbuf; + skipped++; + } + } else { + error = TRUE; + printf("** Could not read block %d\n", + (int)prevblk); + } + } + } while (prevmiddle && !error); + + if (!prevmiddle && !error && skipped) { + /* No luck if there is not enough space in this record */ + rph = &prevbuf->block.record; + endoff = le16_to_cpu(rph->next_record_offset); + if (endoff > (blocksz - LOG_RECORD_HEAD_SZ)) { + prevbuf = savebuf; + } + } + return (error ? (struct BUFFER*)NULL : prevbuf); +} + +void copy_attribute(struct ATTR *pa, const char *buf, int length) +{ + const ATTR_NEW *panew; + ATTR_OLD old_aligned; + + if (pa) { + switch (length) { + case sizeof(ATTR_NEW) : + panew = (const ATTR_NEW*)buf; + pa->type = panew->type; + pa->lsn = sle64_to_cpu(panew->lsn); + pa->inode = MREF(le64_to_cpu(panew->inode)); + break; + case sizeof(ATTR_OLD) : + /* Badly aligned, first realign */ + memcpy(&old_aligned,buf,sizeof(old_aligned)); + pa->type = old_aligned.type; + pa->lsn = sle64_to_cpu(old_aligned.lsn); + pa->inode = MREF(le64_to_cpu(old_aligned.inode)); + break; + default : + printf("** Unexpected attribute format, length %d\n", + length); + } + } +} + +static int refresh_attributes(const struct ACTION_RECORD *firstaction) +{ + const struct ACTION_RECORD *action; + const LOG_RECORD *logr; + struct ATTR *pa; + const char *buf; + u32 extra; + u32 length; + u32 len; + u32 key; + u32 x; + u32 i; + u32 step; + u32 used; + + for (action=firstaction; action; action=action->next) { + logr = &action->record; + buf = ((const char*)logr) + get_redo_offset(logr); + length = le16_to_cpu(logr->redo_length); + switch (le16_to_cpu(action->record.redo_operation)) { + case OpenNonResidentAttribute : + extra = get_extra_offset(logr) + - get_redo_offset(logr); + if (logr->undo_length) { + len = le32_to_cpu(logr->client_data_length) + + LOG_RECORD_HEAD_SZ + - get_extra_offset(logr); + /* this gives a length aligned modulo 8 */ + len = fixnamelen(&buf[extra], len); + } else + len = 0; + pa = getattrentry(le16_to_cpu(logr->target_attribute), + len); + if (pa) { + copy_attribute(pa, buf, length); + pa->namelen = len; + if (len) { + memcpy(pa->name,&buf[extra],len); + } + } + break; + case OpenAttributeTableDump : + i = 24; + step = getle16(buf, 8); + used = getle16(buf, 12); + /* + * Changed from Win10, formerly we got step = 44. + * The record layout has also changed + */ + for (x=0; (x 510) { + printf("** Error : bad" + " attribute name" + " length %d\n", + len); + key = 0; + } + if (key) { /* Apparently, may have to stop before reaching the end */ + pa = getattrentry(key,len); + if (pa) { + pa->namelen = len; + memcpy(pa->name, + &buf[i+4],len); + } + i += len + 6; + x++; + } + } while (key && (i < length)); + } + break; + default : + break; + } + } + return (0); +} + +/* + * Display a fixup + */ + +static void fixup(CONTEXT *ctx, const LOG_RECORD *logr, const char *buf, + BOOL redo) +{ + struct ATTR *pa; + int action; + int attr; + int offs; + s32 length; + int extra; + s32 i; + int p; + s32 base; + u16 firstpos; /* position of first mft attribute */ + le32 v; + ATTR_TYPES mftattr; + le64 w; + le64 inode; + le64 size; + int lth; + int len; + + attr = le16_to_cpu(logr->target_attribute); + offs = le16_to_cpu(logr->attribute_offset); + if (redo) { + action = le16_to_cpu(logr->redo_operation); + length = le16_to_cpu(logr->redo_length); + } else { + action = le16_to_cpu(logr->undo_operation); + length = le16_to_cpu(logr->undo_length); + } + if (redo) + printf("redo fixup %dR %s attr 0x%x offs 0x%x\n", + actionnum, actionname(action), attr, offs); + else + printf("undo fixup %dU %s attr 0x%x offs 0x%x\n", + actionnum, actionname(action), attr, offs); + switch (action) { + case InitializeFileRecordSegment : /* 2 */ + /* + * When this is a redo (with a NoOp undo), the + * full MFT record is logged. + * When this is an undo (with DeallocateFileRecordSegment redo), + * only the header of the MFT record is logged. + */ + if (!ctx->vol && !mftrecsz && (length > 8)) { + /* mftrecsz can be determined from usa_count */ + mftrecsz = (getle16(buf,6) - 1)*512; + mftrecbits = 1; + while ((u32)(1 << mftrecbits) < mftrecsz) + mftrecbits++; + } + printf(" new base MFT record, attr 0x%x (%s)\n",attr,attrname(attr)); + printf(" inode %lld\n", + (((long long)sle64_to_cpu(logr->target_vcn) + << clusterbits) + + (le16_to_cpu(logr->cluster_index) << 9)) + >> mftrecbits); + if (length >= 18) + printf(" seq number 0x%04x\n",(int)getle16(buf, 16)); + if (length >= 20) + printf(" link count %d\n",(int)getle16(buf, 18)); + if (length >= 24) { + u16 flags; + + flags = getle16(buf, 22); + printf(" flags 0x%x",(int)flags); + switch (flags & 3) { + case 1 : + printf(" (file in use)\n"); + break; + case 3 : + printf(" (directory in use)\n"); + break; + default : + printf(" (not in use)\n"); + break; + } + } + base = getle16(buf, 4) + ((getle16(buf, 6)*2 - 1) | 7) + 1; + while (base < length) { + mftattr = feedle32(buf, base); + printf(" attrib 0x%lx (%s) at offset 0x%x\n", + (long)le32_to_cpu(mftattr), + mftattrname(mftattr), (int)base); + if (mftattr == AT_FILE_NAME) { + showname(" name ",&buf[base + 90], + buf[base + 88] & 255); + inode = feedle64(buf, base + 24); + printf(" parent dir inode %lld\n", + (long long)MREF(le64_to_cpu(inode))); + } + lth = getle32(buf, base + 4); + if ((lth <= 0) || (lth & 7)) + base = length; + else + base += lth; + } + break; + case DeallocateFileRecordSegment : /* 3 */ + printf(" free base MFT record, attr 0x%x (%s)\n", + attr,attrname(attr)); + printf(" inode %lld\n", + (((long long)sle64_to_cpu(logr->target_vcn) << clusterbits) + + (le16_to_cpu(logr->cluster_index) << 9)) >> mftrecbits); + break; + case CreateAttribute : /* 5 */ + pa = getattrentry(attr,0); + base = 24; + /* Assume the beginning of the attribute is always present */ + switch (getle32(buf,0)) { + case 0x30 : + printf(" create file name, attr 0x%x\n",attr); + if (pa) + showattribute(" ",pa); + showname(" file ", + &buf[base + 66],buf[base + 64] & 255); + if (base >= -8) + showdate(" created ",feedle64(buf,base + 8)); + if (base >= -16) + showdate(" modified ",feedle64(buf,base + 16)); + if (base >= -24) + showdate(" changed ",feedle64(buf,base + 24)); + if (base >= -32) + showdate(" read ",feedle64(buf,base + 32)); + size = feedle64(buf,base + 40); + printf(" allocated size %lld\n", + (long long)le64_to_cpu(size)); + size = feedle64(buf,base + 48); + printf(" real size %lld\n", + (long long)le64_to_cpu(size)); + v = feedle32(buf,base + 56); + printf(" DOS flags 0x%lx\n", + (long)le32_to_cpu(v)); + break; + case 0x80 : + printf(" create a data stream, attr 0x%x\n",attr); + break; + case 0xc0 : + printf(" create reparse data\n"); + if (pa) + showattribute(" ",pa); + printf(" tag 0x%lx\n",(long)getle32(buf, base)); + showname(" print name ", + &buf[base + 20 + getle16(buf, base + 12)], + getle16(buf, base + 14)/2); + break; + } + break; + case UpdateResidentValue : /* 7 */ + /* + * The record offset designates the mft attribute offset, + * offs and length define a right-justified window in this + * attribute. + * At this stage, we do not know which kind of mft + * attribute this is about, we assume this is standard + * information when it is the first attribute in the + * record. + */ + base = 0x18 - offs; /* p 8 */ + pa = getattrentry(attr,0); + firstpos = 0x30 + (((mftrecsz/512 + 1)*2 - 1 ) | 7) + 1; + if (pa + && !pa->inode + && (pa->type == const_cpu_to_le32(0x80)) + && !(offs & 3) + && (le16_to_cpu(logr->record_offset) == firstpos)) { + printf(" set standard information, attr 0x%x\n",attr); + showattribute(" ",pa); + if ((base >= 0) && ((base + 8) <= length)) + showdate(" created ", + feedle64(buf,base)); + if (((base + 8) >= 0) && ((base + 16) <= length)) + showdate(" modified ", + feedle64(buf,base + 8)); + if (((base + 16) >= 0) && ((base + 24) <= length)) + showdate(" changed ", + feedle64(buf,base + 16)); + if (((base + 24) >= 0) && ((base + 32) <= length)) + showdate(" read ", + feedle64(buf,base + 24)); + if (((base + 32) >= 0) && ((base + 36) <= length)) { + v = feedle32(buf, base + 32); + printf(" DOS flags 0x%lx\n", + (long)le32_to_cpu(v)); + } + if (((base + 52) >= 0) && ((base + 56) <= length)) { + v = feedle32(buf, base + 52); + printf(" security id 0x%lx\n", + (long)le32_to_cpu(v)); + } + if (((base + 64) >= 0) && ((base + 72) <= length)) { + /* + * This is badly aligned for Sparc when + * stamps not present and base == 52 + */ + memcpy(&w, &buf[base + 64], 8); + printf(" journal idx 0x%llx\n", + (long long)le64_to_cpu(w)); + } + } else { + printf(" set an MFT attribute at offset 0x%x, attr 0x%x\n", + (int)offs, attr); + if (pa) + showattribute(" ",pa); + } + break; + case UpdateNonResidentValue : /* 8 */ + printf(" set attr 0x%x (%s)\n",attr,attrname(attr)); + pa = getattrentry(attr,0); + if (pa) + showattribute(" ",pa); + base = 0; /* ? */ +// Should not be decoded, unless attr is of identified type (I30, ...) + if (pa && (pa->namelen == 8) && !memcmp(pa->name, SDS, 8)) { + if (length >= 4) + printf(" security hash 0x%lx\n", + (long)getle32(buf, 0)); + if (length >= 8) + printf(" security id 0x%lx\n", + (long)getle32(buf, 4)); + if (length >= 20) + printf(" entry size %ld\n", + (long)getle32(buf, 16)); + } + if (pa && (pa->namelen == 8) && !memcmp(pa->name, I30, 8)) { + if (!memcmp(buf, "INDX", 4)) + base = 64; /* full record */ + else + base = 0; /* entries */ + inode = feedle64(buf, base); + printf(" inode %lld\n", + (long long)MREF(le64_to_cpu(inode))); + inode = feedle64(buf, base + 16); + printf(" parent inode %lld\n", + (long long)MREF(le64_to_cpu(inode))); + showname(" file ",&buf[base + 82], + buf[base + 80] & 255); + showdate(" date ",feedle64(buf, base + 32)); + } + break; + case UpdateMappingPairs : /* 9 */ + printf(" update runlist in attr 0x%x (%s)\n",attr, + attrname(attr)); + /* argument is a compressed runlist (or part of it ?) */ + /* stop when finding 00 */ + break; + case SetNewAttributeSizes : /* 11 */ + printf(" set sizes in attr 0x%x (%s)\n",attr,attrname(attr)); + base = 0; /* left justified ? */ + size = feedle64(buf,0); + printf(" allocated size %lld\n",(long long)le64_to_cpu(size)); + size = feedle64(buf,8); + printf(" real size %lld\n",(long long)le64_to_cpu(size)); + size = feedle64(buf,16); + printf(" initialized size %lld\n",(long long)le64_to_cpu(size)); + break; + case AddIndexEntryRoot : /* 12 */ + case AddIndexEntryAllocation : /* 14 */ + /* + * The record offset designates the mft attribute offset, + * offs and length define a left-justified window in this + * attribute. + */ + if (action == AddIndexEntryRoot) + printf(" add resident index entry, attr 0x%x\n",attr); + else + printf(" add nonres index entry, attr 0x%x\n",attr); + pa = getattrentry(attr,0); + if (pa) + showattribute(" ",pa); + base = 0; + p = getle16(buf, base + 8); + /* index types may be discriminated by inode in base+0 */ + switch (p) { /* size of index entry */ + case 32 : /* $R entry */ + memcpy(&inode, &buf[base + 20], 8); /* bad align */ + printf(" $R reparse index\n"); + printf(" reparsed inode 0x%016llx\n", + (long long)le64_to_cpu(inode)); + printf(" reparse tag 0x%lx\n", + (long)getle32(buf, 16)); + break; + case 40 : /* $SII entry */ + printf(" $SII security id index\n"); + printf(" security id 0x%lx\n", + (long)getle32(buf, 16)); + printf(" security hash 0x%lx\n", + (long)getle32(buf, 20)); + break; + case 48 : /* $SDH entry */ + printf(" $SDH security id index\n"); + printf(" security id 0x%lx\n", + (long)getle32(buf, 20)); + printf(" security hash 0x%lx\n", + (long)getle32(buf, 16)); + break; + default : + /* directory index are at least 84 bytes long, ntfsdoc p 98 */ + /* have everything needed to create the index */ + lth = buf[base + 80] & 255; + /* consistency of file name length */ + if (getle16(buf,10) == (u32)(2*lth + 66)) { + printf(" directory index\n"); + inode = feedle64(buf,16); + printf(" parent dir inode %lld\n", + (long long)MREF(le64_to_cpu(inode))); + if (feedle32(buf,72) + & const_cpu_to_le32(0x10000000)) + showname(" file (dir) ", + &buf[base + 82], + buf[base + 80] & 255); + else + showname(" file ", + &buf[base + 82], + buf[base + 80] & 255); + inode = feedle64(buf,0); + printf(" file inode %lld\n", + (long long)MREF(le64_to_cpu(inode))); + size = feedle64(buf,64); + printf(" file size %lld\n", + (long long)le64_to_cpu(size)); + showdate(" created ", + feedle64(buf,base + 24)); + showdate(" modified ", + feedle64(buf,base + 32)); + showdate(" changed ", + feedle64(buf,base + 40)); + showdate(" read ", + feedle64(buf,base + 48)); + } else + printf(" unknown index type\n"); + break; + } + break; + case SetIndexEntryVcnRoot : /* 17 */ + printf(" set vcn of non-resident index root, attr 0x%x\n", + attr); + pa = getattrentry(attr,0); + if (pa) + showattribute(" ",pa); + printf(" vcn %lld\n", (long long)getle64(buf,0)); + break; + case UpdateFileNameRoot : /* 19 */ + /* + * Update an entry in a resident directory index. + * The record offset designates the mft attribute offset, + * offs and length define a right-justified window in this + * attribute. + */ + printf(" set directory resident entry, attr 0x%x\n",attr); + base = length - 0x50; + pa = getattrentry(attr,0); + if (pa) + showattribute(" ",pa); + if (pa + && !pa->inode + && (pa->type == const_cpu_to_le32(0x80)) + && !(offs & 3)) { + if (base >= -24) + showdate(" created ",feedle64(buf, + base + 24)); + if (base >= -32) + showdate(" modified ",feedle64(buf, + base + 32)); + if (base >= -40) + showdate(" changed ",feedle64(buf, + base + 40)); + if (base >= -48) + showdate(" read ",feedle64(buf, + base + 48)); + if (base >= -56) { + size = feedle64(buf,base + 56); + printf(" allocated size %lld\n", + (long long)le64_to_cpu(size)); + } + if (base >= -64) { + size = feedle64(buf,base + 64); + printf(" real size %lld\n", + (long long)le64_to_cpu(size)); + } + if (base > -72) { + v = feedle32(buf,base + 72); + printf(" DOS flags 0x%lx\n", + (long)le32_to_cpu(v)); + } + } else { + /* Usually caused by attr not yet defined */ + if (pa && pa->type) + printf("** Unexpected index parameters\n"); + } + break; + case UpdateFileNameAllocation : /* 20 */ + /* update entry in directory index */ + /* only dates, sizes and attrib */ + base = length - 64; /* p 12 */ + printf(" set directory nonres entry, attr 0x%x\n",attr); + pa = getattrentry(attr,0); + if (pa) + showattribute(" ",pa); + if (base >= -8) + showdate(" created ",feedle64(buf, base + 8)); + if (base >= -16) + showdate(" modified ",feedle64(buf, base + 16)); + if (base >= -24) + showdate(" changed ",feedle64(buf, base + 24)); + if (base >= -32) + showdate(" read ",*(const le64*)&buf[base + 32]); + if (base >= -40) { + size = feedle64(buf, base + 40); + printf(" allocated size %lld\n", + (long long)le64_to_cpu(size)); + } + if (base >= -48) { + size = feedle64(buf, base + 48); + printf(" real size %lld\n", + (long long)le64_to_cpu(size)); + } + if (base >= -56) { + v = feedle32(buf, base + 56); + printf(" DOS flags 0x%lx\n",(long)le32_to_cpu(v)); + } + break; + case SetBitsInNonResidentBitMap : /* 21 */ + case ClearBitsInNonResidentBitMap : /* 22 */ + if (action == SetBitsInNonResidentBitMap) + printf(" SetBitsInNonResidentBitMap, attr 0x%x\n", + attr); + else + printf(" ClearBitsInNonResidentBitMap, attr 0x%x\n", + attr); + pa = getattrentry(attr,0); + if (pa) + showattribute(" ",pa); + v = feedle32(buf, 0); + printf(" first bit %ld\n",(long)le32_to_cpu(v)); + v = feedle32(buf, 4); + printf(" bit count %ld\n",(long)le32_to_cpu(v)); + break; + case OpenNonResidentAttribute : /* 28 */ + printf(" OpenNonResidentAttribute, attr 0x%x\n",attr); + extra = get_extra_offset(logr) + - (redo ? get_redo_offset(logr) + : get_undo_offset(logr)); + if (logr->undo_length) { + len = le32_to_cpu(logr->client_data_length) + + LOG_RECORD_HEAD_SZ + - get_extra_offset(logr); + /* this gives a length aligned modulo 8 */ + len = fixnamelen(&buf[extra], len); + } else + len = 0; + pa = getattrentry(attr,len); + if (pa && redo) { + /* + * If this is a redo, collect the attribute data. + * This should only be done when walking forward. + */ + copy_attribute(pa, buf, length); + pa->namelen = len; + if (len) + memcpy(pa->name,&buf[extra],len); + printf(" MFT attribute 0x%lx (%s)\n", + (long)le32_to_cpu(pa->type), + mftattrname(pa->type)); + printf(" lsn 0x%016llx\n", + (long long)pa->lsn); + printf(" inode %lld\n", + (long long)pa->inode); + } + if (logr->undo_length) + showname(" extra : attr name ", &buf[extra], len/2); + if (!redo && length) { + printf(" * undo attr not shown\n"); + } + break; + case OpenAttributeTableDump : /* 29 */ + printf(" OpenAttributeTableDump, attr 0x%x (%s)\n", + attr,attrname(attr)); + i = 24; + if (i < length) { + int x; + int more; + int step; + int used; + + step = getle16(buf, 8); + used = getle16(buf, 12); + /* + * Changed from Win10, formerly we got step = 44. + * The record layout has also changed + */ + if ((step != sizeof(ATTR_OLD)) + && (step != sizeof(ATTR_NEW))) { + printf(" ** Unexpected step %d\n",step); + } + more = 0; + for (x=0; (xinode, + mftattrname(pa->type)); + if (pa->namelen) + showname(" name ", + (char*)pa->name, + pa->namelen/2); + else + printf("\n"); + } else + more++; + } + } + if (more) + printf(" (%d more attrs not shown)\n",more); + } + break; + case AttributeNamesDump : /* 30 */ + printf(" AttributeNamesDump, attr 0x%x (%s)\n", + attr,attrname(attr)); + i = 8; + if (i < length) { + unsigned int l; + unsigned int key; + int x; + int more; + + more = 0; + x = 0; + do { + l = le16_to_cpu(*(const le16*)&buf[i+2]); + key = le16_to_cpu(*(const le16*)&buf[i]); + if (l > 510) { + printf("** Error : bad attribute name" + " length %d\n",l); + key = 0; + } + /* Apparently, may have to stop before reaching the end */ + if (key) { + pa = getattrentry(key,l); + if (pa) { + pa->namelen = l; + memcpy(pa->name,&buf[i+4],l); + } + if (x < SHOWATTRS) { + printf(" attr 0x%x is",key); + showname(" ",&buf[i+4],l/2); + } else + more++; + i += l + 6; + x++; + } + } while (key && (i < length)); + if (more) + printf(" (%d more attrs not shown)\n",more); + } + break; + default : + break; + } +} + +static void detaillogr(CONTEXT *ctx, const LOG_RECORD *logr) +{ + u64 lcn; + u64 baselcn; + unsigned int i; + unsigned int off; + unsigned int undo; + unsigned int redo; + unsigned int extra; + unsigned int end; + unsigned int listsize; + BOOL onmft; + + switch (logr->record_type) { + case LOG_STANDARD : + onmft = logr->cluster_index + || acts_on_mft(le16_to_cpu(logr->redo_operation)) + || acts_on_mft(le16_to_cpu(logr->undo_operation)); + printf("redo_operation %04x %s\n", + (int)le16_to_cpu(logr->redo_operation), + actionname(le16_to_cpu(logr->redo_operation))); + printf("undo_operation %04x %s\n", + (int)le16_to_cpu(logr->undo_operation), + actionname(le16_to_cpu(logr->undo_operation))); + printf("redo_offset %04x\n", + (int)le16_to_cpu(logr->redo_offset)); + printf("redo_length %04x\n", + (int)le16_to_cpu(logr->redo_length)); + printf("undo_offset %04x\n", + (int)le16_to_cpu(logr->undo_offset)); + printf("undo_length %04x\n", + (int)le16_to_cpu(logr->undo_length)); + printf("target_attribute %04x\n", + (int)le16_to_cpu(logr->target_attribute)); + printf("lcns_to_follow %04x\n", + (int)le16_to_cpu(logr->lcns_to_follow)); + printf("record_offset %04x\n", + (int)le16_to_cpu(logr->record_offset)); + printf("attribute_offset %04x\n", + (int)le16_to_cpu(logr->attribute_offset)); + printf("cluster_index %04x\n", + (int)le16_to_cpu(logr->cluster_index)); + printf("attribute_flags %04x\n", + (int)le16_to_cpu(logr->attribute_flags)); + if (mftrecbits && onmft) + printf("target_vcn %016llx (inode %lld)\n", + (long long)sle64_to_cpu(logr->target_vcn), + (((long long)sle64_to_cpu(logr->target_vcn) + << clusterbits) + + (le16_to_cpu(logr->cluster_index) << 9)) + >> mftrecbits); + else + printf("target_vcn %016llx\n", + (long long)sle64_to_cpu(logr->target_vcn)); + /* Compute a base for the current run of mft */ + baselcn = sle64_to_cpu(logr->lcn_list[0]) + - sle64_to_cpu(logr->target_vcn); + for (i=0; ilcns_to_follow) + && (ilcn_list[i]); + printf(" (%d offs 0x%x) lcn %016llx",i, + (int)(8*i + sizeof(LOG_RECORD) - 8), + (long long)lcn); + lcn &= 0xffffffffffffULL; + if (mftrecsz && onmft) { + if (clustersz > mftrecsz) + printf(" (MFT records for inodes" + " %lld-%lld)\n", + (long long)((lcn - baselcn) + *clustersz/mftrecsz), + (long long)((lcn + 1 - baselcn) + *clustersz/mftrecsz - 1)); + else + printf(" (MFT record for inode %lld)\n", + (long long)((lcn - baselcn) + *clustersz/mftrecsz)); + printf(" assuming record for inode %lld\n", + (long long)((lcn - baselcn) + *clustersz/mftrecsz + + (le16_to_cpu(logr->cluster_index) + >> 1))); + } else + printf("\n"); + } + /* + * redo_offset and undo_offset are considered unsafe + * (actually they are safe when you know the logic) + * 2) redo : redo (defined by redo_offset) + * 3) undo : undo (defined by undo_offset) + * 4) extra : unknown data (end of undo to data_length) + */ + end = le32_to_cpu(logr->client_data_length) + LOG_RECORD_HEAD_SZ; + if (logr->redo_length && logr->undo_length) + { + /* both undo and redo are present */ + if (le16_to_cpu(logr->undo_offset) <= + le16_to_cpu(logr->redo_offset)) + { + undo = sizeof(LOG_RECORD) - 8 + + 8*le16_to_cpu(logr->lcns_to_follow); + if (logr->redo_offset == logr->undo_offset) + redo = undo; + else + redo = undo + ((le16_to_cpu(logr->undo_length) - 1) | 7) + 1; + extra = redo + ((le16_to_cpu(logr->redo_length) - 1) | 7) + 1; + } + else + { + redo = sizeof(LOG_RECORD) - 8 + + 8*le16_to_cpu(logr->lcns_to_follow); + undo = redo + ((le16_to_cpu(logr->redo_length) - 1) | 7) + 1; + extra = undo + ((le16_to_cpu(logr->undo_length) - 1) | 7) + 1; + } + } + else + if (logr->redo_length) + { + /* redo and not undo */ + redo = undo = sizeof(LOG_RECORD) - 8 + + 8*le16_to_cpu(logr->lcns_to_follow); + extra = redo + ((le16_to_cpu(logr->redo_length) - 1) | 7) + 1; + } + else + { + /* optional undo and not redo */ + redo = undo = sizeof(LOG_RECORD) - 8 + + 8*le16_to_cpu(logr->lcns_to_follow); + extra = undo + ((le16_to_cpu(logr->undo_length) - 1) | 7) + 1; + } + + printf("redo 0x%x (%u) undo 0x%x (%u) extra 0x%x (%d)\n", + redo,(int)(((le16_to_cpu(logr->redo_length) - 1) | 7) + 1), + undo,(int)(((le16_to_cpu(logr->undo_length) - 1) | 7) + 1), + extra,(int)(end > extra ? end - extra : 0)); + + if (logr->redo_length && (get_redo_offset(logr) != redo)) + printf("** Unexpected redo offset 0x%x %u (%u)\n", + get_redo_offset(logr),(int)redo, + (int)le16_to_cpu(logr->lcns_to_follow)); + if (logr->undo_length && (get_undo_offset(logr) != undo)) + printf("** Unexpected undo offset 0x%x %u (%u)\n", + get_undo_offset(logr),(int)undo, + (int)le16_to_cpu(logr->lcns_to_follow)); + if (get_extra_offset(logr) != extra) + printf("** Unexpected extra offset 0x%x %u (%u)\n", + get_extra_offset(logr),(int)extra, + (int)le16_to_cpu(logr->lcns_to_follow)); + + if (extra <= end) + { + /* show redo data */ + if (logr->redo_length) + { + if (logr->lcns_to_follow) + { + off = le16_to_cpu(logr->record_offset) + + le16_to_cpu(logr->attribute_offset); + printf("redo data (new data) cluster 0x%llx pos 0x%x :\n", + (long long)sle64_to_cpu(logr->lcn_list[off + >> clusterbits]), + (int)(off & (clustersz - 1))); + } + else + printf("redo data (new data) at offs 0x%x :\n",redo); + if ((u32)(redo + le16_to_cpu(logr->redo_length)) + <= end) + { + hexdump((const char*)logr + + redo,le16_to_cpu(logr->redo_length)); + fixup(ctx, logr, (const char*)logr + redo, TRUE); + } + else printf("redo data overflowing from record\n"); + } + else + { + printf("no redo data (new data)\n"); + fixup(ctx, logr, (const char*)logr + redo, TRUE); + } + + /* show undo data */ + if (logr->undo_length) + { + if (logr->lcns_to_follow) + { + off = le16_to_cpu(logr->record_offset) + + le16_to_cpu(logr->attribute_offset); + printf("undo data (old data) cluster 0x%llx pos 0x%x :\n", + (long long)sle64_to_cpu(logr->lcn_list[off + >> clusterbits]), + (int)(off & (clustersz - 1))); + } + else printf("undo data (old data) at offs 0x%x :\n",undo); + if ((u32)(undo + le16_to_cpu(logr->undo_length)) <= end) + { + if ((undo + le16_to_cpu(logr->undo_length)) < 2*blocksz) + { + hexdump((const char*)logr + + undo,le16_to_cpu(logr->undo_length)); + fixup(ctx, logr, (const char*)logr + undo, FALSE); + } + else printf("undo data overflowing from two blocks\n"); + } + else printf("undo data overflowing from record\n"); + } + else + { + printf("no undo data (old data)\n"); + fixup(ctx, logr, (const char*)logr + undo, FALSE); + } + + /* show extra data, if any */ + if (extra != end) + { + if (end > blocksz) + printf("invalid extra data size\n"); + else + { + printf("extra data at offs 0x%x\n",extra); + hexdump((const char*)logr + extra, + end - extra); + } + } + } + else + { + /* sometimes the designated data overflows */ + if (logr->redo_length + && ((u32)(redo + le16_to_cpu(logr->redo_length)) > end)) + printf("* redo data overflows from record\n"); + if (logr->undo_length + && ((u32)(undo + le16_to_cpu(logr->undo_length)) > end)) + printf("* undo data overflows from record\n"); + } + break; + case LOG_CHECKPOINT : + printf("---> checkpoint record\n"); + printf("redo_operation %04x %s\n", + (int)le16_to_cpu(logr->redo_operation), + actionname(le16_to_cpu(logr->redo_operation))); + printf("undo_operation %04x %s\n", + (int)le16_to_cpu(logr->undo_operation), + actionname(le16_to_cpu(logr->undo_operation))); + printf("redo_offset %04x\n", + (int)le16_to_cpu(logr->redo_offset)); + printf("redo_length %04x\n", + (int)le16_to_cpu(logr->redo_length)); + printf("transaction_lsn %016llx\n", + (long long)sle64_to_cpu(logr->transaction_lsn)); + printf("attributes_lsn %016llx\n", + (long long)sle64_to_cpu(logr->attributes_lsn)); + printf("names_lsn %016llx\n", + (long long)sle64_to_cpu(logr->names_lsn)); + printf("dirty_pages_lsn %016llx\n", + (long long)sle64_to_cpu(logr->dirty_pages_lsn)); + listsize = le32_to_cpu(logr->client_data_length) + + LOG_RECORD_HEAD_SZ + - offsetof(LOG_RECORD, unknown_list); + if (listsize > 8*SHOWLISTS) + listsize = 8*SHOWLISTS; + for (i=0; 8*iunknown_list[i])); + break; + default : + printf("** Unknown action type\n"); + if (le32_to_cpu(logr->client_data_length) < blocksz) { + printf("client_data for record type %ld\n", + (long)le32_to_cpu(logr->record_type)); + hexdump((const char*)&logr->redo_operation, + le32_to_cpu(logr->client_data_length)); + } else + printf("** Bad client data\n"); + break; + } +} + +BOOL within_lcn_range(const LOG_RECORD *logr) +{ + u64 lcn; + unsigned int i; + BOOL within; + + within = FALSE; + switch (logr->record_type) { + case LOG_STANDARD : + for (i=0; ilcns_to_follow); i++) { + lcn = MREF(sle64_to_cpu(logr->lcn_list[i])); + if ((lcn >= firstlcn) && (lcn <= lastlcn)) + within = TRUE; + } + break; + default : + break; + } + return (within); +} + +static void showlogr(CONTEXT *ctx, int k, const LOG_RECORD *logr) +{ + s32 diff; + + if (optv && (!optc || within_lcn_range(logr))) { + diff = sle64_to_cpu(logr->this_lsn) - synced_lsn; + printf("this_lsn %016llx (synced%s%ld) %s\n", + (long long)sle64_to_cpu(logr->this_lsn), + (diff < 0 ? "" : "+"),(long)diff, + commitment(diff + synced_lsn)); + printf("client_previous_lsn %016llx\n", + (long long)sle64_to_cpu(logr->client_previous_lsn)); + printf("client_undo_next_lsn %016llx\n", + (long long)sle64_to_cpu(logr->client_undo_next_lsn)); + printf("client_data_length %08lx\n", + (long)le32_to_cpu(logr->client_data_length)); + printf("seq_number %d\n", + (int)le16_to_cpu(logr->client_id.seq_number)); + printf("client_index %d\n", + (int)le16_to_cpu(logr->client_id.client_index)); + printf("record_type %08lx\n", + (long)le32_to_cpu(logr->record_type)); + printf("transaction_id %08lx\n", + (long)le32_to_cpu(logr->transaction_id)); + printf("log_record_flags %04x\n", + (int)le16_to_cpu(logr->log_record_flags)); + printf("reserved1 %04x %04x %04x\n", + (int)le16_to_cpu(logr->reserved_or_alignment[0]), + (int)le16_to_cpu(logr->reserved_or_alignment[1]), + (int)le16_to_cpu(logr->reserved_or_alignment[2])); + detaillogr(ctx, logr); + } + if (optt) { + const char *state; + + if (logr->record_type == LOG_CHECKPOINT) + state = "--checkpoint--"; + else + state = commitment(sle64_to_cpu(logr->this_lsn)); + printf(" at %04x %016llx %s (%ld) %s\n",k, + (long long)sle64_to_cpu(logr->this_lsn), + state, + (long)(sle64_to_cpu(logr->this_lsn) - synced_lsn), + actionname(le16_to_cpu(logr->redo_operation))); + if (logr->client_previous_lsn || logr->client_undo_next_lsn) { + if (logr->client_previous_lsn + == logr->client_undo_next_lsn) { + printf(" " + " previous and undo %016llx\n", + (long long)sle64_to_cpu( + logr->client_previous_lsn)); + } else { + printf(" " + " previous %016llx", + (long long)sle64_to_cpu( + logr->client_previous_lsn)); + + if (logr->client_undo_next_lsn) + printf(" undo %016llx\n", + (long long)sle64_to_cpu( + logr->client_undo_next_lsn)); + else + printf("\n"); + } + } + } +} + +/* + * Mark transactions which should be redone + */ + +static void mark_transactions(struct ACTION_RECORD *lastaction) +{ + struct ACTION_RECORD *action; + const LOG_RECORD *logr; + le32 id; + int actives; + BOOL more; + BOOL committed; + + actives = 0; + do { + more = FALSE; + id = const_cpu_to_le32(0); + for (action=lastaction; action; action=action->prev) { + logr = &action->record; + if ((logr->redo_operation + == const_cpu_to_le16(ForgetTransaction)) + && !(action->flags & ACTION_TO_REDO) + && !id) { + id = logr->transaction_id; + action->flags |= ACTION_TO_REDO; + if (optv) + printf("Marking transaction 0x%x\n", + (int)le32_to_cpu(id)); + } + committed = ((s64)(sle64_to_cpu(logr->this_lsn) + - committed_lsn)) <= 0; + if (!logr->transaction_id + && committed) + action->flags |= ACTION_TO_REDO; + if (id + && (logr->transaction_id == id) + && committed) { + action->flags |= ACTION_TO_REDO; + more = TRUE; + } + } + if (more) + actives++; + } while (more); + /* + * Show unmarked (aborted) actions + */ + if (optv) { + for (action=lastaction; action; action=action->prev) { + logr = &action->record; + if (logr->transaction_id + && !(action->flags & ACTION_TO_REDO)) + printf("** Action %d was aborted\n", + (int)action->num); + } + } + if (optv && (actives > 1)) + printf("%d active transactions in set\n",actives); +} + +/* + * Enqueue an action and play the queued actions on end of set + */ + +static TRISTATE enqueue_action(CONTEXT *ctx, const LOG_RECORD *logr, + int size, int num) +{ + struct ACTION_RECORD *action; + TRISTATE state; + int err; + + err = 1; + state = T_ERR; + /* enqueue record */ + action = (struct ACTION_RECORD*) + malloc(size + offsetof(struct ACTION_RECORD, record)); + if (action) { + memcpy(&action->record, logr, size); + action->num = num; + action->flags = 0; + /* enqueue ahead of list, firstaction is the oldest one */ + action->prev = (struct ACTION_RECORD*)NULL; + action->next = ctx->firstaction; + if (ctx->firstaction) + ctx->firstaction->prev = action; + else + ctx->lastaction = action; + ctx->firstaction = action; + err = 0; + state = T_OK; + if ((optp || optu) + && (logr->record_type == LOG_CHECKPOINT)) { + /* if chkp process queue, and increment count */ + playedactions++; + if (playedactions <= playcount) { + if (optv) + printf("* Refreshing attributes\n"); + err = refresh_attributes(ctx->firstaction); + if (optv) + printf("* Undoing transaction set %d" + " (actions %d->%d)\n", + (int)playedactions, + (int)ctx->lastaction->num, + (int)ctx->firstaction->num); + err = play_undos(ctx->vol, ctx->lastaction); + if (err) + printf("* Undoing transaction" + " set failed\n"); + } + if (!err && optp && (playedactions == playcount)) { + if (optv) + printf("* Redoing transaction set %d" + " (actions %d->%d)\n", + (int)playedactions, + (int)ctx->firstaction->num, + (int)ctx->lastaction->num); + mark_transactions(ctx->lastaction); + err = play_redos(ctx->vol, ctx->firstaction); + if (err) + printf("* Redoing transaction" + " set failed\n"); + } + if (err) + state = T_ERR; + else + if (playedactions == playcount) + state = T_DONE; + /* free queue */ + while (ctx->firstaction) { + action = ctx->firstaction->next; + free(ctx->firstaction); + ctx->firstaction = action; + } + ctx->lastaction = (struct ACTION_RECORD*)NULL; + } + if (opts + && ((s64)(sle64_to_cpu(logr->this_lsn) - synced_lsn) <= 0)) { + if (optv) + printf("* Refreshing attributes\n"); +// should refresh backward ? + err = refresh_attributes(ctx->firstaction); + mark_transactions(ctx->lastaction); + if (!err) { + if (optv) + printf("* Syncing actions %d->%d\n", + (int)ctx->firstaction->num, + (int)ctx->lastaction->num); + err = play_redos(ctx->vol, ctx->firstaction); + } + if (err) { + printf("* Syncing actions failed\n"); + state = T_ERR; + } else + state = T_DONE; + } + } + return (state); +} + + +static void showheadrcrd(u32 blk, const RECORD_PAGE_HEADER *rph) +{ + s32 diff; + + if (optv) { + printf("magic %08lx\n", + (long)le32_to_cpu(rph->magic)); + printf("usa_ofs %04x\n", + (int)le16_to_cpu(rph->usa_ofs)); + printf("usa_count %04x\n", + (int)le16_to_cpu(rph->usa_count)); + if (blk < 4) + printf("file_offset %016llx\n", + (long long)sle64_to_cpu(rph->copy.file_offset)); + else { + diff = sle64_to_cpu(rph->copy.last_lsn) - synced_lsn; + printf("last_lsn %016llx" + " (synced%s%ld)\n", + (long long)sle64_to_cpu(rph->copy.last_lsn), + (diff < 0 ? "" : "+"),(long)diff); + } + printf("flags %08lx\n", + (long)le32_to_cpu(rph->flags)); + printf("page_count %d\n", + (int)le16_to_cpu(rph->page_count)); + printf("page_position %d\n", + (int)le16_to_cpu(rph->page_position)); + printf("next_record_offset %04x\n", + (int)le16_to_cpu(rph->next_record_offset)); + printf("reserved4 %04x %04x %04x\n", + (int)le16_to_cpu(rph->reserved[0]), + (int)le16_to_cpu(rph->reserved[1]), + (int)le16_to_cpu(rph->reserved[2])); + diff = sle64_to_cpu(rph->last_end_lsn) - synced_lsn; + printf("last_end_lsn %016llx (synced%s%ld)\n", + (long long)sle64_to_cpu(rph->last_end_lsn), + (diff < 0 ? "" : "+"),(long)diff); + printf("usn %04x\n", + (int)getle16(rph,le16_to_cpu(rph->usa_ofs))); + printf("\n"); + } else { + if (optt) { + const char *state; + + state = commitment(sle64_to_cpu(rph->copy.last_lsn)); + diff = sle64_to_cpu(rph->copy.last_lsn) - synced_lsn; + printf(" last %016llx (synced%s%ld) %s\n", + (long long)sle64_to_cpu(rph->copy.last_lsn), + (diff < 0 ? "" : "+"),(long)diff, state); + state = commitment(sle64_to_cpu(rph->last_end_lsn)); + diff = sle64_to_cpu(rph->last_end_lsn) - synced_lsn; + printf(" last_end %016llx (synced%s%ld) %s\n", + (long long)sle64_to_cpu(rph->last_end_lsn), + (diff < 0 ? "" : "+"),(long)diff, state); + } + } +} + +/* + * Analyze and display an action overlapping log blocks + * + * Returns the position of first action in next block. If this is + * greater than a block size (for actions overlapping more than + * two blocks), then some blocks have to be skipped. + * + * Returns 0 in case of error + */ + +static u16 overlapshow(CONTEXT *ctx, u16 k, u32 blk, const struct BUFFER *buf, + const struct BUFFER *nextbuf) +{ + const LOG_RECORD *logr; + const char *data; + const char *nextdata; + char *fullrec; + u32 size; + u32 nextspace; + u32 space; + BOOL likely; + u16 blkheadsz; + + data = buf->block.data; + logr = (const LOG_RECORD*)&data[k]; + size = le32_to_cpu(logr->client_data_length) + LOG_RECORD_HEAD_SZ; + blkheadsz = buf->headsz; + if (nextbuf && (blk >= BASEBLKS)) { + nextdata = nextbuf->block.data; + space = blocksz - k; + nextspace = blocksz - blkheadsz; + if ((space >= LOG_RECORD_HEAD_SZ) + && (size > space)) { + fullrec = (char*)malloc(size); + if (size <= (space + nextspace)) { + /* Overlap on two blocks */ + memcpy(fullrec,&data[k],space); + memcpy(&fullrec[space], + nextdata + blkheadsz, + size - space); + likely = likelyop((LOG_RECORD*)fullrec); + actionnum++; + if (optv) { + printf("\nOverlapping record %u at 0x%x" + " size %d (next at 0x%x)\n", + (int)actionnum,(int)k, + (int)size, (int)(k + size)); + printf("Overlap marked for block %ld" + " space %d likely %d\n", + (long)blk,(int)space,likely); + } + if (likely) + showlogr(ctx, k, + (LOG_RECORD*)fullrec); + else + printf("** Skipping unlikely" + " overlapping record\n"); + k += size - blocksz + blkheadsz; + } else { + const struct BUFFER *midbuf; + int skip; + u32 next; + u32 pos; + int i; + + /* + * The maximum size of of log record is 131104 + * (when both offset and length are 65528 for + * redo or undo). + * So up to 33 log blocks (useful size 4032) + * could be needed. However never both undo and + * redo have been found big, and 17 should be + * the real maximum. + */ + if (optv) + printf("More than two blocks required" + " (size %lu)\n",(long)size); + memcpy(fullrec,&data[k],space); + + skip = (size - space - 1)/nextspace; + pos = space; + likely = TRUE; + for (i=1; (i<=skip) && likely; i++) { + midbuf = read_buffer(ctx, blk + i); + if (midbuf) { + memcpy(&fullrec[pos], + &midbuf->block + .data[blkheadsz], + nextspace); + pos += nextspace; + } else + likely = FALSE; + } + if (pos >= size) { + printf("** Error : bad big overlap" + " pos %d size %d\n", + (int)pos,(int)size); + likely = FALSE; + } + midbuf = read_buffer(ctx, blk + skip + 1); + if (midbuf) + memcpy(&fullrec[pos], + &midbuf->block.data[blkheadsz], + size - pos); + else + likely = FALSE; + if (!likelyop((LOG_RECORD*)fullrec)) + likely = FALSE; + actionnum++; + if (optv) { + printf("\nBig overlapping record %u at " + "0x%x size %u (next at 0x%x)\n", + (int)actionnum,(int)k,(int)size, + (int)(k + size)); + printf("Overlap marked for block %ld" + " space %d likely %d\n", + (long)blk,(int)space,likely); + } + if (likely) + showlogr(ctx, k, + (LOG_RECORD*)fullrec); + else + printf("** Skipping unlikely" + " overlapping record\n"); + /* next and skip are only for displaying */ + next = (size - space) % nextspace + + blkheadsz; + if ((blocksz - next) < LOG_RECORD_HEAD_SZ) + next = blkheadsz; + if (next == blkheadsz) + skip++; + if (optv) + printf("Next record expected in" + " block %lu index 0x%x\n", + (long)(blk + skip + 1),next); + /* Quick check, with no consequences */ + if (firstrecord(skip,buf,buf) != next) + printf("** Error next != firstrecord" + " after block %d\n",blk); + k += size - blocksz + blkheadsz; + } + if (!likely) + k = 0; + else + if (!k) + printf("* Bad return from overlap()\n"); + free(fullrec); + } else { + /* No conditions for overlap, usually a new session */ + printf("* No block found overlapping on block %d\n", + (int)blk); + k = 0; + } + } else { + /* blocks 2, 3 and the last one have no next block */ + k = 0; + } + return (k); +} + +/* + * Analyze and forward display the actions in a log block + * + * Returns the position of first action in next block. If this is + * greater than a block size, then some blocks have to be skipped. + * + * Returns 0 in case of error + */ + +static u16 forward_rcrd(CONTEXT *ctx, u32 blk, u16 pos, + const struct BUFFER *buf, const struct BUFFER *nextbuf) +{ + const RECORD_PAGE_HEADER *rph; + const LOG_RECORD *logr; + const char *data; + u16 k; + u16 endoff; + BOOL stop; + + rph = &buf->block.record; + if (rph && (rph->magic == magic_RCRD)) { + data = buf->block.data; + showheadrcrd(blk, rph); + k = buf->headsz; + if ((k < pos) && (pos < blocksz)) { + k = ((pos - 1) | 7) + 1; + } +// TODO check bad start > blocksz - 48 + logr = (const LOG_RECORD*)&data[k]; + stop = FALSE; + if (!likelyop(logr)) { + if (optv) + printf("* Bad start 0x%x for block %d\n", + (int)pos,(int)blk); + k = searchlikely(buf); + if ((k + sizeof(LOG_RECORD)) > blocksz) { + printf("No likely full record in block %lu\n", + (unsigned long)blk); + /* there can be a partial one */ + k = le16_to_cpu(rph->next_record_offset); + if ((k < (u16)sizeof(RECORD_PAGE_HEADER)) + || ((blocksz - k) < LOG_RECORD_HEAD_SZ)) + stop = TRUE; + } else { + if (optv) + printf("First record computed at" + " offset 0x%x\n", (int)k); + } + } + while (!stop) { + s32 size; + + logr = (const LOG_RECORD*)&data[k]; + size = le32_to_cpu(logr->client_data_length) + + LOG_RECORD_HEAD_SZ; + if ((size < MINRECSIZE) + || (size > MAXRECSIZE) + || (size & 7)) { + printf("** Bad record size %ld in block %ld" + " offset 0x%x\n", + (long)size, (long)buf->num, (int)k); + showlogr(ctx, k, logr); + k = 0; + stop = TRUE; + } else { + endoff = le16_to_cpu(rph->next_record_offset); + if (((u32)(k + size) <= blocksz) + && ((u32)(k + size) <= endoff)) { + actionnum++; + if (optv) { + printf("\n* log action %u at" + " 0x%x size %d (next" + " at 0x%x)\n", + actionnum,k,size, + k + size); + } + showlogr(ctx, k, logr); + if (!logr->client_data_length) { + printf("** Bad" + " client_data_length\n"); + stop = TRUE; + } + k += size; + if ((blocksz - k) + < LOG_RECORD_HEAD_SZ) { + k = nextbuf->headsz; + stop = TRUE; + } + } else { + k = overlapshow(ctx, k, blk, + buf, nextbuf); + stop = TRUE; + } + } + } + } else { + printf("** Not a RCRD record, MAGIC 0x%08lx\n", + (long)le32_to_cpu(rph->magic)); + k = 0; + } + return (k); +} + +/* + * Display a restart page + */ + +static void showrest(const RESTART_PAGE_HEADER *rest) +{ + const RESTART_AREA *resa; + const LOG_CLIENT_RECORD *rcli; + const char *data; + + data = (const char*)rest; + if ((rest->magic == magic_RSTR) + || (rest->magic == magic_CHKD)) { + if (optv) { + printf("magic %08lx\n", + (long)le32_to_cpu(rest->magic)); + printf("usa_ofs %04x\n", + (int)le16_to_cpu(rest->usa_ofs)); + printf("usa_count %04x\n", + (int)le16_to_cpu(rest->usa_count)); + printf("chkdsk_lsn %016llx\n", + (long long)sle64_to_cpu(rest->chkdsk_lsn)); + printf("system_page_size %08lx\n", + (long)le32_to_cpu(rest->system_page_size)); + printf("log_page_size %08lx\n", + (long)le32_to_cpu(rest->log_page_size)); + printf("restart_area_offset %04x\n", + (int)le16_to_cpu(rest->restart_area_offset)); + printf("minor_vers %d\n", + (int)sle16_to_cpu(rest->minor_ver)); + printf("major_vers %d\n", + (int)sle16_to_cpu(rest->major_ver)); + printf("usn %04x\n", + (int)le16_to_cpu(rest->usn)); + printf("\n"); + } else { + if (optt) + printf(" chkdsk %016llx\n", + (long long)sle64_to_cpu(rest->chkdsk_lsn)); + } + resa = (const RESTART_AREA*) + &data[le16_to_cpu(rest->restart_area_offset)]; + if (optv) { + printf("current_lsn %016llx\n", + (long long)sle64_to_cpu(resa->current_lsn)); + printf("log_clients %04x\n", + (int)le16_to_cpu(resa->log_clients)); + printf("client_free_list %04x\n", + (int)le16_to_cpu(resa->client_free_list)); + printf("client_in_use_list %04x\n", + (int)le16_to_cpu(resa->client_in_use_list)); + printf("flags %04x\n", + (int)le16_to_cpu(resa->flags)); + printf("seq_number_bits %08lx\n", + (long)le32_to_cpu(resa->seq_number_bits)); + printf("restart_area_length %04x\n", + (int)le16_to_cpu(resa->restart_area_length)); + printf("client_array_offset %04x\n", + (int)le16_to_cpu(resa->client_array_offset)); + printf("file_size %016llx\n", + (long long)sle64_to_cpu(resa->file_size)); + printf("last_lsn_data_len %08lx\n", + (long)le32_to_cpu(resa->last_lsn_data_length)); + printf("record_length %04x\n", + (int)le16_to_cpu(resa->log_record_header_length)); + printf("log_page_data_offs %04x\n", + (int)le16_to_cpu(resa->log_page_data_offset)); + printf("restart_log_open_count %08lx\n", + (long)le32_to_cpu(resa->restart_log_open_count)); + printf("\n"); + } else { + if (optt) + printf(" latest %016llx\n", + (long long)sle64_to_cpu(resa->current_lsn)); + } + + rcli = (const LOG_CLIENT_RECORD*) + &data[le16_to_cpu(rest->restart_area_offset) + + le16_to_cpu(resa->client_array_offset)]; + if (optv) { + printf("oldest_lsn %016llx\n", + (long long)sle64_to_cpu(rcli->oldest_lsn)); + printf("client_restart_lsn %016llx\n", + (long long)sle64_to_cpu(rcli->client_restart_lsn)); + printf("prev_client %04x\n", + (int)le16_to_cpu(rcli->prev_client)); + printf("next_client %04x\n", + (int)le16_to_cpu(rcli->next_client)); + printf("seq_number %04x\n", + (int)le16_to_cpu(rcli->seq_number)); + printf("client_name_length %08x\n", + (int)le32_to_cpu(rcli->client_name_length)); + showname("client_name ", + (const char*)rcli->client_name, + le32_to_cpu(rcli->client_name_length) >> 1); + } else { + if (optt) { + printf(" synced %016llx\n", + (long long)sle64_to_cpu( + rcli->oldest_lsn)); + printf(" committed %016llx\n", + (long long)sle64_to_cpu( + rcli->client_restart_lsn)); + } + } + } else + printf("Not a RSTR or CHKD record, MAGIC 0x%08lx\n", + (long)le32_to_cpu(rest->magic)); +} + +static BOOL dorest(CONTEXT *ctx, unsigned long blk, + const RESTART_PAGE_HEADER *rph, BOOL initial) +{ + const RESTART_AREA *resa; + const LOG_CLIENT_RECORD *rcli; + const char *data; + s64 diff; + int offs; + int size; + BOOL change; + BOOL dirty; + + data = (const char*)rph; + offs = le16_to_cpu(rph->restart_area_offset); + resa = (const RESTART_AREA*)&data[offs]; + rcli = (const LOG_CLIENT_RECORD*)&data[offs + + le16_to_cpu(resa->client_array_offset)]; + if (initial) { + /* Information from block initially found best */ + latest_lsn = sle64_to_cpu(resa->current_lsn); + committed_lsn = sle64_to_cpu(rcli->client_restart_lsn); + synced_lsn = sle64_to_cpu(rcli->oldest_lsn); + memcpy(&log_header, rph, + sizeof(RESTART_PAGE_HEADER)); + offs = le16_to_cpu(log_header.restart_area_offset); + memcpy(&restart, &data[offs], + sizeof(RESTART_AREA)); + offs += le16_to_cpu(restart.client_array_offset); + memcpy(&client, &data[offs], + sizeof(LOG_CLIENT_RECORD)); + dirty = !(resa->flags & RESTART_VOLUME_IS_CLEAN); + if (optv || optt) + printf("* Using initial restart page," + " syncing from 0x%llx, %s\n", + (long long)synced_lsn, + (dirty ? "dirty" : "clean")); + /* Get the block page size */ + blocksz = le32_to_cpu(rph->log_page_size); + if (optv) + printf("* Block size %ld bytes\n", (long)blocksz); + blockbits = 1; + while ((u32)(1 << blockbits) < blocksz) + blockbits++; + } else { + size = offs + le16_to_cpu(resa->restart_area_length); + if (optv) { + if (optv >= 2) + hexdump(data,size); + printf("* RSTR in block %ld 0x%lx (addr 0x%llx)\n", + (long)blk,(long)blk, + (long long)loclogblk(ctx, blk)); + } else { + if (optt) + printf("restart %ld\n",(long)blk); + } + showrest(rph); + /* Information from an older restart block if requested */ + dirty = !(restart.flags & RESTART_VOLUME_IS_CLEAN); + diff = sle64_to_cpu(rcli->client_restart_lsn) - committed_lsn; + if (ctx->vol) { + change = (opts > 1) && (diff < 0); + } else { + change = (opts > 1 ? diff < 0 : diff > 0); + } + if (change) { + committed_lsn = sle64_to_cpu(rcli->client_restart_lsn); + synced_lsn = sle64_to_cpu(rcli->oldest_lsn); + latest_lsn = sle64_to_cpu(resa->current_lsn); + memcpy(&log_header, rph, + sizeof(RESTART_PAGE_HEADER)); + offs = le16_to_cpu(log_header.restart_area_offset); + memcpy(&restart, &data[offs], + sizeof(RESTART_AREA)); + offs += le16_to_cpu(restart.client_array_offset); + memcpy(&client, &data[offs], + sizeof(LOG_CLIENT_RECORD)); + dirty = !(resa->flags & RESTART_VOLUME_IS_CLEAN); + if (optv || optt) + printf("* Using %s restart page," + " syncing from 0x%llx, %s\n", + (diff < 0 ? "older" : "newer"), + (long long)synced_lsn, + (dirty ? "dirty" : "clean")); + } + } + restart_lsn = synced_lsn; + offset_mask = ((u64)1 << (64 - le32_to_cpu(restart.seq_number_bits))) + - (1 << (blockbits - 3)); + return (dirty); +} + +/* + * Read and process the first restart block + * + * In full mode, both restart page are silently analyzed by the + * library and the most recent readable one is used to define the + * sync parameters. + * + * Returns the first restart buffer + * or NULL if the restart block is not valid + */ + + +static const struct BUFFER *read_restart(CONTEXT *ctx) +{ + const struct BUFFER *buf; + BOOL bad; + int blk; + int major, minor; + + bad = FALSE; + for (blk=0; blkvol) { + RESTART_PAGE_HEADER *rph; + + rph = (RESTART_PAGE_HEADER*)NULL; + /* Full mode : use the restart page selected by the library */ + if (ntfs_check_logfile(log_na, &rph)) { + /* rph is left unchanged for a wiped out log file */ + if (rph) { + dorest(ctx, 0, rph, TRUE); + free(rph); + buf = read_buffer(ctx,0); + } else { + buf = (const struct BUFFER*)NULL; + printf("** The log file has been wiped out\n"); + } + } else { + buf = (const struct BUFFER*)NULL; + printf("** Could not get any restart page\n"); + } + } else { + /* Reduced mode : rely on first restart page */ + blockbits = BLOCKBITS; /* Until the correct value is read */ + blocksz = 1L << blockbits; + buf = read_buffer(ctx,0); + } + if (buf) { + NTFS_RECORD_TYPES magic; + + magic = buf->block.restart.magic; + switch (magic) { + case magic_RSTR : + break; + case magic_CHKD : + printf("** The log file has been obsoleted by chkdsk\n"); + bad = TRUE; + break; + case magic_empty : + printf("** The log file has been wiped out\n"); + bad = TRUE; + break; + default : + printf("** Invalid restart block\n"); + bad = TRUE; + break; + } + if (!bad && !ctx->vol) + dorest(ctx, 0, &buf->block.restart, TRUE); + major = sle16_to_cpu(buf->block.restart.major_ver); + minor = sle16_to_cpu(buf->block.restart.minor_ver); + if ((major == 2) && (minor == 0)) { + if (!optk) { + printf("** Fast restart mode detected," + " data could be lost\n"); + printf(" Use option --kill-fast-restart" + " to bypass\n"); + bad = TRUE; + } + } else + if ((major != 1) || (minor != 1)) { + printf("** Unsupported $LogFile version %d.%d\n", + major, minor); + bad = TRUE; + } + log_major = major; + if (bad) { + buf = (const struct BUFFER*)NULL; + } + } + return (buf); +} + +/* + * Mark the logfile as synced + */ + +static int reset_logfile(CONTEXT *ctx __attribute__((unused))) +{ + char *buffer; + int off; + int err; + + err = 1; + buffer = (char*)malloc(blocksz); + if (buffer) { + memset(buffer, 0, blocksz); + restart.client_in_use_list = LOGFILE_NO_CLIENT; + restart.flags |= RESTART_VOLUME_IS_CLEAN; + client.oldest_lsn = cpu_to_sle64(restart_lsn); + /* Set $LogFile version to 1.1 so that volume can be mounted */ + log_header.major_ver = const_cpu_to_sle16(1); + log_header.minor_ver = const_cpu_to_sle16(1); + memcpy(buffer, &log_header, + sizeof(RESTART_PAGE_HEADER)); + off = le16_to_cpu(log_header.restart_area_offset); + memcpy(&buffer[off], &restart, + sizeof(RESTART_AREA)); + off += le16_to_cpu(restart.client_array_offset); + memcpy(&buffer[off], &client, + sizeof(LOG_CLIENT_RECORD)); + if (!ntfs_mst_pre_write_fixup((NTFS_RECORD*)buffer, blocksz) + && (ntfs_attr_pwrite(log_na, 0, + blocksz, buffer) == blocksz) + && (ntfs_attr_pwrite(log_na, (u64)1 << blockbits, + blocksz, buffer) == blocksz)) + err = 0; + free(buffer); + } + return (err); +} + +/* + * Determine the most recent valid record block + */ + +static const struct BUFFER *best_start(const struct BUFFER *buf, + const struct BUFFER *altbuf) +{ + const struct BUFFER *best; + const RECORD_PAGE_HEADER *head; + const RECORD_PAGE_HEADER *althead; + s64 diff; + + if (!buf || !altbuf) + best = (buf ? buf : altbuf); + else { + head = &buf->block.record; + althead = &altbuf->block.record; + /* determine most recent, caring for wraparounds */ + diff = sle64_to_cpu(althead->last_end_lsn) + - sle64_to_cpu(head->last_end_lsn); + if (diff > 0) + best = altbuf; + else + best = buf; + } + if (best && (best->block.record.magic != magic_RCRD)) + best = (const struct BUFFER*)NULL; + return (best); +} + +/* + * Interpret the boot data + * + * Probably not needed any more, use ctx->vol + */ + +static BOOL getboot(const char *buf) +{ + u64 sectors; + u64 clusters; + u16 sectpercluster; + BOOL ok; + + ok = TRUE; + /* Beware : bad alignment */ + bytespersect = (buf[11] & 255) + ((buf[12] & 255) << 8); + sectpercluster = buf[13] & 255; + clustersz = bytespersect * (u32)sectpercluster; + clusterbits = 1; + while ((u32)(1 << clusterbits) < clustersz) + clusterbits++; + sectors = getle64(buf, 0x28); + clusters = sectors/sectpercluster; + mftlcn = getle64(buf, 0x30); + if (buf[0x40] & 0x80) + mftrecsz = 1 << (16 - (buf[0x40] & 15)); + else + mftrecsz = (buf[0x40] & 127)*clustersz; + mftrecbits = 1; + while ((u32)(1 << mftrecbits) < mftrecsz) + mftrecbits++; + if (optv) { + if ((long long)sectors*bytespersect > 10000000000LL) + printf("Capacity %lld bytes (%lld GB)\n", + (long long)sectors*bytespersect, + (long long)sectors*bytespersect/1000000000); + else + printf("Capacity %lld bytes (%lld MB)\n", + (long long)sectors*bytespersect, + (long long)sectors*bytespersect/1000000); + printf("sectors %lld (0x%llx), sector size %d\n", + (long long)sectors,(long long)sectors, + (int)bytespersect); + printf("clusters %lld (0x%llx), cluster size %d (%d bits)\n", + (long long)clusters,(long long)clusters, + (int)clustersz,(int)clusterbits); + printf("MFT at cluster %lld (0x%llx), entry size %lu\n", + (long long)mftlcn,(long long)mftlcn, + (unsigned long)mftrecsz); + if (mftrecsz > clustersz) + printf("%ld clusters per MFT entry\n", + (long)(mftrecsz/clustersz)); + else + printf("%ld MFT entries per cluster\n", + (long)(clustersz/mftrecsz)); + } + return (ok); +} + +static int locatelogfile(CONTEXT *ctx) +{ + int err; + + err = 1; + log_ni = ntfs_inode_open(ctx->vol, FILE_LogFile); + if (log_ni) { + log_na = ntfs_attr_open(log_ni, AT_DATA, AT_UNNAMED, 0); + if (log_na) { + logfilesz = log_na->data_size; + err = 0; + } + } + return (err); +} + +/* + * Analyze a $LogFile copy + * + * A $LogFile cannot be played. It can be however be analyzed in + * stand-alone mode. + * The location of the $MFT will have to be determined elsewhere. + */ + +static BOOL getlogfiledata(CONTEXT *ctx, const char *boot) +{ + const RESTART_PAGE_HEADER *rph; + const RESTART_AREA *rest; + BOOL ok; + u32 off; + s64 size; + u32 system_page_size; + u32 log_page_size; + + ok = FALSE; + fseek(ctx->file,0L,2); + size = ftell(ctx->file); + rph = (const RESTART_PAGE_HEADER*)boot; + off = le16_to_cpu(rph->restart_area_offset); + /* + * If the system or log page sizes are smaller than the ntfs block size + * or either is not a power of 2 we cannot handle this log file. + */ + system_page_size = le32_to_cpu(rph->system_page_size); + log_page_size = le32_to_cpu(rph->log_page_size); + if (system_page_size < NTFS_BLOCK_SIZE || + log_page_size < NTFS_BLOCK_SIZE || + system_page_size & (system_page_size - 1) || + log_page_size & (log_page_size - 1)) { + printf("** Unsupported page size.\n"); + goto out; + } + if (off & 7 || off > system_page_size) { + printf("** Inconsistent restart area offset.\n"); + goto out; + } + rest = (const RESTART_AREA*)&boot[off]; + + /* estimate cluster size from log file size (unreliable) */ + switch (le32_to_cpu(rest->seq_number_bits)) { + case 45 : clustersz = 512; break; + case 43 : clustersz = 1024; break; /* can be 1024 or 2048 */ + case 40 : + default : clustersz = 4096; break; + } + + clusterbits = 1; + while ((u32)(1 << clusterbits) < clustersz) + clusterbits++; + printf("* Assuming cluster size %ld\n",(long)clustersz); + logfilelcn = 0; + logfilesz = size; + if (optv) + printf("Log file size %lld bytes, cluster size %ld\n", + (long long)size, (long)clustersz); + /* Have to wait an InitializeFileRecordSegment to get these values */ + mftrecsz = 0; + mftrecbits = 0; + ok = TRUE; +out: + return (ok); +} + +/* + * Get basic volume data + * + * Locate the MFT and Logfile + * Not supposed to read the first log block... + */ + +static BOOL getvolumedata(CONTEXT *ctx, char *boot) +{ + const RESTART_AREA *rest; + BOOL ok; + + ok = FALSE; + rest = (const RESTART_AREA*)NULL; + if (ctx->vol) { + getboot(boot); + mftlcn = ctx->vol->mft_lcn; + mftcnt = ctx->vol->mft_na->data_size/mftrecsz; + if (!locatelogfile(ctx)) + ok = TRUE; + else { + fprintf(stderr,"** Could not read the log file\n"); + } + } else { + if (ctx->file + && (!memcmp(boot,"RSTR",4) || !memcmp(boot,"CHKD",4))) { + printf("* Assuming a log file copy\n"); + ok = getlogfiledata(ctx, boot); + if (!ok) + goto out; + } else + fprintf(stderr,"** Not an NTFS image or log file\n"); + } +// TODO get rest ?, meaningful ? + if (ok && rest) { + if (rest->client_in_use_list + || !(rest->flags & const_cpu_to_le16(2))) + printf("Volume was not unmounted safely\n"); + else + printf("Volume was unmounted safely\n"); + if (le16_to_cpu(rest->client_in_use_list) > 1) + printf("** multiple clients not implemented\n"); + } +out: + return (ok); +} + +/* + * Open the volume (or the log file) and gets its parameters + * + * Returns TRUE if successful + */ + +static BOOL open_volume(CONTEXT *ctx, const char *device_name) +{ + union { + char buf[1024]; + /* alignment may be needed in getboot() */ + long long force_align; + } boot; + BOOL ok; + int got; + + ok =FALSE; + /* + * First check the boot sector, to avoid library errors + * when trying to mount a log file. + * If the device cannot be fopened or fread, then it is + * unlikely to be a file. + */ + ctx->vol = (ntfs_volume*)NULL; + ctx->file = fopen(device_name, "rb"); + if (ctx->file) { + got = fread(boot.buf,1,1024,ctx->file); + if ((got == 1024) + && (!memcmp(boot.buf, "RSTR", 4) + || !memcmp(boot.buf, "CHKD", 4))) { + /* This appears to be a log file */ + ctx->vol = (ntfs_volume*)NULL; + ok = getvolumedata(ctx, boot.buf); + if (!ok) { + fclose(ctx->file); + goto out; + } + } else { + fclose(ctx->file); + } + } + if (!ok) { + /* Not a log file, assume an ntfs device, mount it */ + ctx->file = (FILE*)NULL; + ctx->vol = ntfs_mount(device_name, + ((optk || optp || optu || opts) && !optn + ? NTFS_MNT_FORENSIC : NTFS_MNT_RDONLY)); + if (ctx->vol) { + ok = getvolumedata(ctx, boot.buf); + if (!ok) + ntfs_umount(ctx->vol, TRUE); + } + } +out: + return (ok); +} + +static u16 dorcrd(CONTEXT *ctx, u32 blk, u16 pos, const struct BUFFER *buf, + const struct BUFFER *nextbuf) +{ + if (optv) { + if (optv >= 2) + hexdump(buf->block.data,blocksz); + printf("* RCRD in block %ld 0x%lx (addr 0x%llx)" + " from pos 0x%x\n", + (long)blk,(long)blk, + (long long)loclogblk(ctx, blk),(int)pos); + } else { + if (optt) + printf("block %ld\n",(long)blk); + } + return (forward_rcrd(ctx, blk, pos, buf, nextbuf)); +} + +/* + * Concatenate and process a record overlapping on several blocks + */ + +static TRISTATE backoverlap(CONTEXT *ctx, int blk, + const char *data, const char *nextdata, int k) +{ + const LOG_RECORD *logr; + char *fullrec; + s32 size; + int space; + int nextspace; + TRISTATE state; + u16 blkheadsz; + + logr = (const LOG_RECORD*)&data[k]; + state = T_ERR; + size = le32_to_cpu(logr->client_data_length) + LOG_RECORD_HEAD_SZ; + space = blocksz - k; + blkheadsz = sizeof(RECORD_PAGE_HEADER) + + ((2*getle16(data,6) - 1) | 7) + 1; + nextspace = blocksz - blkheadsz; + if ((space >= LOG_RECORD_HEAD_SZ) + && (size > space) + && (size < MAXRECSIZE)) { + fullrec = (char*)malloc(size); + memcpy(fullrec,&data[k],space); + if (size <= (space + nextspace)) + memcpy(&fullrec[space], nextdata + blkheadsz, + size - space); + else { + const struct BUFFER *morebuf; + const char *moredata; + int total; + int more; + unsigned int mblk; + + if (optv) + printf("* big record, size %d\n",size); + total = space; + mblk = blk + 1; + while (total < size) { + if (mblk >= (logfilesz >> blockbits)) + mblk = (log_major < 2 ? BASEBLKS + : BASEBLKS2); + more = size - total; + if (more > nextspace) + more = nextspace; + morebuf = read_buffer(ctx, mblk); + if (morebuf) { + moredata = morebuf->block.data; + memcpy(&fullrec[total], + moredata + blkheadsz, more); + } + total += more; + mblk++; + } + } + + state = (likelyop((LOG_RECORD*)fullrec) ? T_OK : T_ERR); + actionnum++; + if (optv) { + printf("\nOverlapping backward action %d at 0x%x" + " size %d (next at 0x%x)\n", + (int)actionnum,(int)k, + (int)size,(int)(k + size)); + printf("Overlap marked for block %ld space %d" + " likely %d\n", + (long)blk,(int)space,(state == T_OK)); + } + if (state == T_OK) { + showlogr(ctx, k, (LOG_RECORD*)fullrec); + if (optp || optu || opts) + state = enqueue_action(ctx, + (LOG_RECORD*)fullrec, + size, actionnum); + } else { + /* Try to go on unless playing actions */ + if (optb && (state == T_ERR)) + state = T_OK; + } + free(fullrec); + } else { + /* Error conditions */ + if ((size < MINRECSIZE) || (size > MAXRECSIZE)) { + printf("** Invalid record size %ld" + " in block %ld\n", + (long)size,(long)blk); + } else + printf("** Inconsistency : the final" + " record in block %ld" + " does not overlap\n", + (long)blk); + /* Do not abort, unless playing actions */ + state = (optb ? T_OK : T_ERR); + } + return (state); +} + +static TRISTATE backward_rcrd(CONTEXT *ctx, u32 blk, int skipped, + const struct BUFFER *buf, const struct BUFFER *prevbuf, + const struct BUFFER *nextbuf) +{ + u16 poslist[75]; /* 4096/sizeof(LOG_RECORD) */ + const RECORD_PAGE_HEADER *rph; + const RECORD_PAGE_HEADER *prevrph; + const LOG_RECORD *logr; + const char *data; + const char *nextdata; + BOOL stop; + TRISTATE state; + s32 size; + int cnt; + u16 k; + u16 endoff; + int j; + + state = T_ERR; + rph = &buf->block.record; + prevrph = (RECORD_PAGE_HEADER*)NULL; + if (prevbuf) + prevrph = &prevbuf->block.record; + data = buf->block.data; + if (rph && (rph->magic == magic_RCRD) + && (!prevrph || (prevrph->magic == magic_RCRD))) { + if (optv) { + if (optv >= 2) + hexdump(data,blocksz); + if (buf->rnum != blk) + printf("* RCRD for block %ld 0x%lx" + " in block %ld (addr 0x%llx)\n", + (long)blk,(long)blk,(long)buf->rnum, + (long long)loclogblk(ctx, blk)); + else + printf("* RCRD in block %ld 0x%lx (addr 0x%llx)\n", + (long)blk,(long)blk, + (long long)loclogblk(ctx, blk)); + } else { + if (optt) + printf("block %ld\n",(long)blk); + } + showheadrcrd(blk, rph); + if (!prevbuf) + k = buf->headsz; + else + k = firstrecord(skipped, buf, prevbuf); + logr = (const LOG_RECORD*)&data[k]; + cnt = 0; + /* check whether there is at least one beginning of record */ + endoff = le16_to_cpu(rph->next_record_offset); + if (k && ((k < endoff) || !endoff)) { + logr = (const LOG_RECORD*)&data[k]; + if (likelyop(logr)) { + stop = FALSE; + state = T_OK; + if (optv) + printf("First record checked" + " at offset 0x%x\n", (int)k); + } else { + printf("** Bad first record at offset 0x%x\n", + (int)k); + if (optv) + showlogr(ctx, k,logr); + k = searchlikely(buf); + stop = !k; + if (stop) { + printf("** Could not recover," + " stopping at block %d\n", + (int)blk); + state = T_ERR; + } else { + /* Try to go on, unless running */ + if (optb) + state = T_OK; + } + } + while (!stop) { + logr = (const LOG_RECORD*)&data[k]; + size = le32_to_cpu(logr->client_data_length) + + LOG_RECORD_HEAD_SZ; + if ((size < MINRECSIZE) + || (size > MAXRECSIZE) + || (size & 7)) { + printf("** Bad size %ld in block %ld" + " offset 0x%x, stopping\n", + (long)size,(long)blk,(int)k); + stop = TRUE; + } else { + if (((u32)(k + size) <= blocksz) + && ((u32)(k + size) <= endoff)) { + poslist[cnt++] = k; + if (!logr->client_data_length) + stop = TRUE; + k += size; + if ((u32)(k + + LOG_RECORD_HEAD_SZ) + > blocksz) + stop = TRUE; + } else { + stop = TRUE; + } + } + } + } else { + stop = TRUE; + state = (k ? T_OK : T_ERR); + } + /* Now examine an overlapping record */ + if (k + && ((k == endoff) || !endoff) + && ((u32)(k + LOG_RECORD_HEAD_SZ) <= blocksz)) { + if (nextbuf && (blk >= BASEBLKS)) { + nextdata = nextbuf->block.data; + state = backoverlap(ctx, blk, + data, nextdata, k); + } + } + for (j=cnt-1; (j>=0) && (state==T_OK); j--) { + k = poslist[j]; + logr = (const LOG_RECORD*)&data[k]; + size = le32_to_cpu(logr->client_data_length) + + LOG_RECORD_HEAD_SZ; + actionnum++; + if (optv && (!optc || within_lcn_range(logr))) { + printf("\n* log backward action %u at 0x%x" + " size %d (next at 0x%x)\n", + actionnum, k, size, k + size); + } + if ((optv | optt) + && (!nextbuf && (j == (cnt - 1)))) { + printf("* This is the latest record\n"); + if (logr->this_lsn == restart.current_lsn) + printf(" its lsn matches the global" + " restart lsn\n"); + if (logr->this_lsn == client.client_restart_lsn) + printf(" its lsn matches the client" + " restart lsn\n"); + if (logr->client_data_length + == restart.last_lsn_data_length) + printf(" its length matches the" + " last record length\n"); + } + showlogr(ctx, k, logr); + if (optp || optu || opts) + state = enqueue_action(ctx, logr, size, actionnum); + } + } + return (state); +} + +static int walkback(CONTEXT *ctx, const struct BUFFER *buf, u32 blk, + const struct BUFFER *prevbuf, u32 prevblk) +{ + const struct BUFFER *nextbuf; + NTFS_RECORD_TYPES magic; + u32 stopblk; + TRISTATE state; + + if (optv) { + if ((log_major >= 2) && (buf->rnum != blk)) + printf("\n* block %d for block %d at 0x%llx\n", + (int)buf->rnum,(int)blk, + (long long)loclogblk(ctx, buf->rnum)); + else + printf("\n* block %d at 0x%llx\n",(int)blk, + (long long)loclogblk(ctx, blk)); + } + ctx->firstaction = (struct ACTION_RECORD*)NULL; + ctx->lastaction = (struct ACTION_RECORD*)NULL; + nextbuf = (const struct BUFFER*)NULL; + stopblk = prevblk + 2; // wraparound ! + state = backward_rcrd(ctx, blk, 0, buf, + prevbuf, (struct BUFFER*)NULL); + while ((state == T_OK) + && !((blk > stopblk) && (prevblk <= stopblk)) + && (!(optp || optu) || (playedactions < playcount))) { + int skipped; + + nextbuf = buf; + buf = prevbuf; + blk = prevblk; + skipped = 0; + prevbuf = findprevious(ctx, buf); + if (prevbuf) { + prevblk = prevbuf->num; + if (prevblk < blk) + skipped = blk - prevblk - 1; + else + skipped = blk - prevblk - 1 + + (logfilesz >> blockbits) + - (log_major < 2 ? BASEBLKS + : BASEBLKS2); + magic = prevbuf->block.record.magic; + switch (magic) { + case magic_RCRD : + break; + case magic_CHKD : + printf("** Unexpected block type CHKD\n"); + break; + case magic_RSTR : + printf("** Unexpected block type RSTR\n"); + break; + default : + printf("** Invalid block %d\n",(int)prevblk); + break; + } + if (optv) { + if (skipped) + printf("\n* block %ld at 0x%llx (block" + " %ld used as previous one)\n", + (long)blk, + (long long)loclogblk(ctx, blk), + (long)prevblk); + else + if ((log_major >= 2) + && (buf->rnum != blk)) + printf("\n* block %ld for block %ld at 0x%llx\n", + (long)buf->rnum, + (long)blk, + (long long)loclogblk( + ctx,buf->rnum)); + else + printf("\n* block %ld at 0x%llx\n", + (long)blk, + (long long)loclogblk( + ctx, blk)); + } + state = backward_rcrd(ctx, blk, skipped, + buf, prevbuf, nextbuf); + } else { + fprintf(stderr,"** Could not read block %lu\n", + (long)prevblk); + state = T_ERR; + } + } + if ((blk > stopblk) && (prevblk <= stopblk)) + printf("* Earliest block reached\n"); + if ((optp || optu) && (playedactions >= playcount)) + printf("* Transaction set count reached\n"); + if (opts) + printf("* %s %s after playing %u actions\n", + (optn ? "Sync simulation" : "Syncing"), + (state == T_ERR ? "failed" : "successful"), + redocount); + /* free queue */ + while (ctx->firstaction) { + struct ACTION_RECORD *action; + + action = ctx->firstaction->next; + free(ctx->firstaction); + ctx->firstaction = action; + } + ctx->lastaction = (struct ACTION_RECORD*)NULL; + return (state == T_ERR ? 1 : 0); +} + +/* + * Find the latest log block + * + * Usually, the latest block is either block 2 or 3 which act as + * temporary block before being copied to target location. + * However under some unknown condition the block are written + * immediately to target location, and we have to scan for the + * latest one. + * Currently this is not checked for logfile version 2.x which + * use a different layout of temporary blocks. + */ + +static const struct BUFFER *find_latest_block(CONTEXT *ctx, u32 baseblk, + const struct BUFFER *basebuf) +{ + le64 offset; + leLSN prevlsn; + leLSN curlsn; + u32 curblk; + u32 prevblk; + const struct BUFFER *prevbuf; + const struct BUFFER *curbuf; + + offset = basebuf->block.record.copy.file_offset; + curbuf = (const struct BUFFER*)NULL; + curlsn = const_cpu_to_le64(0); + prevblk = 0; + curblk = baseblk; + do { + if (curblk < BASEBLKS) { + prevbuf = basebuf; + prevlsn = basebuf->block.record.last_end_lsn; + prevblk = baseblk; + curblk = le64_to_cpu(offset) >> blockbits; + } else { + if (optv) + printf("block %d is more recent than block %d\n", + (int)curblk, (int)prevblk); + prevbuf = curbuf; + prevlsn = curlsn; + prevblk = curblk; + curblk++; + if (curblk >= (logfilesz >> blockbits)) + curblk = (log_major < 2 ? BASEBLKS : BASEBLKS2); + } + curbuf = read_buffer(ctx, curblk); + if (curbuf && (curbuf->block.record.magic == magic_RCRD)) { + curlsn = curbuf->block.record.copy.last_lsn; + } + } while (curbuf + && (curbuf->block.record.magic == magic_RCRD) + && (le64_to_cpu(curlsn) > le64_to_cpu(prevlsn))); + if (optv) + printf("Block %d is the latest one\n",(int)prevblk); + return (prevbuf); +} + +/* + * Determine the sequencing of blocks (when version >= 2.0) + * + * Blocks 2..17 and 18..33 are temporary blocks being filled until + * they are copied to their target locations, so there are three + * possible location for recent blocks. + * + * Returns the latest target block number + */ + +static int block_sequence(CONTEXT *ctx) +{ + const struct BUFFER *buf; + int blk; + int k; + int target_blk; + int latest_blk; + s64 final_lsn; + s64 last_lsn; + s64 last_lsn12; + s64 last_lsn1, last_lsn2; + + final_lsn = 0; + for (blk=RSTBLKS; 2*blk<(RSTBLKS+BASEBLKS2); blk++) { + /* First temporary block */ + last_lsn1 = 0; + buf = read_buffer(ctx, blk); + if (buf && (buf->block.record.magic == magic_RCRD)) { + last_lsn1 = le64_to_cpu( + buf->block.record.copy.last_lsn); + if (!final_lsn + || ((s64)(last_lsn1 - final_lsn) > 0)) + final_lsn = last_lsn1; + } + /* Second temporary block */ + buf = read_buffer(ctx, blk + (BASEBLKS2 - RSTBLKS)/2); + last_lsn2 = 0; + if (buf && (buf->block.record.magic == magic_RCRD)) { + last_lsn2 = le64_to_cpu( + buf->block.record.copy.last_lsn); + if (!final_lsn + || ((s64)(last_lsn2 - final_lsn) > 0)) + final_lsn = last_lsn2; + } + /* the latest last_lsn defines the target block */ + last_lsn12 = 0; + latest_blk = 0; + if (last_lsn1 || last_lsn2) { + if (!last_lsn2 + || ((s64)(last_lsn1 - last_lsn2) > 0)) { + last_lsn12 = last_lsn1; + latest_blk = blk; + } + if (!last_lsn1 + || ((s64)(last_lsn1 - last_lsn2) <= 0)) { + last_lsn12 = last_lsn2; + latest_blk = blk + (BASEBLKS2 - RSTBLKS)/2; + } + } + last_lsn = 0; + target_blk = 0; + if (last_lsn12) { + target_blk = (last_lsn12 & offset_mask) + >> (blockbits - 3); + buf = read_buffer(ctx, target_blk); + if (buf && (buf->block.record.magic == magic_RCRD)) { + last_lsn = le64_to_cpu( + buf->block.record.copy.last_lsn); + if (!final_lsn + || ((s64)(last_lsn - final_lsn) > 0)) + final_lsn = last_lsn; + } + } + /* redirect to the latest block */ + if (latest_blk + && (!last_lsn || ((s64)(last_lsn - last_lsn12) < 0))) + redirect[latest_blk] = target_blk; + } + if (optv) { + printf("\n Blocks redirected :\n"); + for (k=RSTBLKS; k> (blockbits - 3); + if (optv > 1) + printf("final lsn %llx in blk %d\n",(long long)final_lsn,blk); + return (blk); +} + +static int walk(CONTEXT *ctx) +{ + const struct BUFFER *buf; + const struct BUFFER *nextbuf; + const struct BUFFER *prevbuf; + const struct BUFFER *startbuf; + const NTFS_RECORD *record; + const RECORD_PAGE_HEADER *rph; + NTFS_RECORD_TYPES magic; + u32 blk; + u32 nextblk; + u32 prevblk; + u32 finalblk; + int err; + u16 blkheadsz; + u16 pos; + BOOL dirty; + BOOL done; + + buf = (struct BUFFER*)NULL; + nextbuf = (struct BUFFER*)NULL; + if (optb || optp || optu || opts) { + prevbuf = (struct BUFFER*)NULL; + } + done = FALSE; + dirty = TRUE; + finalblk = 0; + err = 0; + blk = 0; + pos = 0; + /* read and process the first restart block */ + buf = read_restart(ctx); + if (buf) { + if (optv) + printf("\n* block %d at 0x%llx\n",(int)blk, + (long long)loclogblk(ctx, blk)); + } else { + done = TRUE; + err = 1; + } + + nextblk = blk + 1; + while (!done) { + /* next block is needed to process the current one */ + if ((nextblk >= (logfilesz >> blockbits)) && (optr || optf)) + nextbuf = read_buffer(ctx, + (log_major < 2 ? BASEBLKS : BASEBLKS2)); + else + nextbuf = read_buffer(ctx,nextblk); + if (nextbuf) { + record = (const NTFS_RECORD*)&nextbuf->block.data; + blkheadsz = nextbuf->headsz; + magic = record->magic; + switch (magic) { + case magic_CHKD : + case magic_RSTR : + case magic_RCRD : + break; + default : + printf("** Invalid block\n"); + err = 1; + break; + } + magic = buf->block.record.magic; + switch (magic) { + case magic_CHKD : + case magic_RSTR : + dirty = dorest(ctx, blk, &buf->block.restart, + FALSE); + break; + case magic_RCRD : + if (blk < BASEBLKS) + pos = buf->headsz; + pos = dorcrd(ctx, blk, pos, buf, nextbuf); + while (pos >= blocksz) { + if (optv > 1) + printf("Skipping block %d" + " pos 0x%x\n", + (int)nextblk,(int)pos); + pos -= (blocksz - blkheadsz); + nextblk++; + } + if ((blocksz - pos) < LOG_RECORD_HEAD_SZ) { + pos = 0; + nextblk++; + } + if (nextblk != (blk + 1)) { + nextbuf = read_buffer(ctx,nextblk); + } + break; + default : + if (!~magic) { + if (optv) + printf(" empty block\n"); + } + break; + } + } else { + fprintf(stderr,"* Could not read block %d\n",nextblk); + if (ctx->vol) { + /* In full mode, ignore errors on restart blocks */ + if (blk >= RSTBLKS) { + done = TRUE; + err = 1; + } + } else { + done = TRUE; + err = 1; + } + } + blk = nextblk; + nextblk++; + + if (!optr && (log_major >= 2) && (nextblk == RSTBLKS)) { + finalblk = block_sequence(ctx); + if (!finalblk) { + done = TRUE; + err = 1; + } + } + + if (optr) { /* Only selected range */ + u32 endblk; + + endblk = (log_major < 2 ? BASEBLKS : RSTBLKS); + if ((nextblk == endblk) && (nextblk < firstblk)) + nextblk = firstblk; + if ((blk >= endblk) && (blk > lastblk)) + done = TRUE; + } else + if (optf) { /* Full log, forward */ + if (blk*blocksz >= logfilesz) + done = TRUE; + } else + if (optb || optp || optu || opts + || (log_major >= 2)) { + /* Restart blocks only (2 blocks) */ + if (blk >= RSTBLKS) + done = TRUE; + } else { /* Base blocks only (4 blocks) */ + if (blk >= BASEBLKS) + done = TRUE; + } + if (!done) { + buf = nextbuf; + if (blk >= RSTBLKS && blk < BASEBLKS) { + /* The latest buf may be more recent + than restart */ + rph = &buf->block.record; + if ((s64)(sle64_to_cpu(rph->last_end_lsn) + - committed_lsn) > 0) { + committed_lsn = + sle64_to_cpu(rph->last_end_lsn); + if (optv) + printf("* Restart page was " + "obsolete, updated " + "committed lsn\n"); + } + } + if (optv) + printf("\n* block %d at 0x%llx\n",(int)blk, + (long long)loclogblk(ctx, blk)); + } + } + if (optv && opts && !dirty) + printf("* Volume is clean, nothing to do\n"); + if (log_major >= 2) + blk = finalblk; + if (!err + && (optb || optp || optu || (opts && dirty))) { + playedactions = 0; + ctx->firstaction = (struct ACTION_RECORD*)NULL; + ctx->lastaction = (struct ACTION_RECORD*)NULL; + if (log_major < 2) { + buf = nextbuf; + nextbuf = read_buffer(ctx, blk+1); + startbuf = best_start(buf,nextbuf); + if (startbuf && (startbuf == nextbuf)) { + /* nextbuf is better, show blk */ + if (optv && buf) { + printf("* Ignored block %d at 0x%llx\n", + (int)blk, + (long long)loclogblk(ctx, blk)); + if (optv >= 2) + hexdump(buf->block.data, + blocksz); + showheadrcrd(blk, &buf->block.record); + } + blk++; + buf = nextbuf; + } else { + /* buf is better, show blk + 1 */ + if (optv && nextbuf) { + printf("* Ignored block %d at 0x%llx\n", + (int)(blk + 1), + (long long)loclogblk(ctx, + blk + 1)); + if (optv >= 2) + hexdump(nextbuf->block.data, + blocksz); + showheadrcrd(blk + 1, + &nextbuf->block.record); + } + } + if (startbuf && opts) { + buf = startbuf = find_latest_block(ctx, + blk, startbuf); + latest_lsn = le64_to_cpu( + buf->block.record.last_end_lsn); + } + } else { + buf = startbuf = read_buffer(ctx, blk); + nextbuf = (const struct BUFFER*)NULL; + } + if (startbuf) { + /* The latest buf may be more recent than restart */ + rph = &buf->block.record; + if ((s64)(sle64_to_cpu(rph->last_end_lsn) + - committed_lsn) > 0) { + committed_lsn = sle64_to_cpu(rph->last_end_lsn); + if (optv) + printf("* Restart page was obsolete\n"); + } + nextbuf = (const struct BUFFER*)NULL; + prevbuf = findprevious(ctx, buf); + if (prevbuf) { + prevblk = prevbuf->num; + magic = prevbuf->block.record.magic; + switch (magic) { + case magic_RCRD : + break; + case magic_CHKD : + printf("** Unexpected block type CHKD\n"); + err = 1; + break; + case magic_RSTR : + err = 1; + printf("** Unexpected block type RSTR\n"); + break; + default : + err = 1; + printf("** Invalid block\n"); + break; + } + } else + prevblk = BASEBLKS; + if (!err) + err = walkback(ctx, buf, blk, + prevbuf, prevblk); + } else { + fprintf(stderr,"** No valid start block, aborting\n"); + err = 1; + } + } + return (err); +} + +BOOL exception(int num) +{ + int i; + + i = 0; + while ((i < 10) && optx[i] && (optx[i] != num)) + i++; + return (optx[i] == num); +} + +static void version(void) +{ + printf("\n%s v%s (libntfs-3g) - Recover updates committed by Windows" + " on an NTFS Volume.\n\n", "ntfsrecover", VERSION); + printf("Copyright (c) 2012-2017 Jean-Pierre Andre\n"); + printf("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +static void usage(void) +{ + fprintf(stderr,"Usage : for recovering the updates committed by Windows :\n"); + fprintf(stderr," ntfsrecover partition\n"); + fprintf(stderr," (e.g. ntfsrecover /dev/sda1)\n"); + fprintf(stderr,"Advanced : ntfsrecover [-b] [-c first-last] [-i] [-f] [-n] [-p count]\n"); + fprintf(stderr," [-r first-last] [-t] [-u count] [-v] partition\n"); + fprintf(stderr," -b : show the full log backward\n"); + fprintf(stderr," -c : restrict to the actions related to cluster range\n"); + fprintf(stderr," -i : show invalid (stale) records\n"); + fprintf(stderr," -f : show the full log forward\n"); + fprintf(stderr," -h : show this help information\n"); + fprintf(stderr," -k : kill fast restart data\n"); + fprintf(stderr," -n : do not apply any modification\n"); + fprintf(stderr," -p : undo the latest count transaction sets and play one\n"); + fprintf(stderr," -r : show a range of log blocks forward\n"); + fprintf(stderr," -s : sync the committed changes (default)\n"); + fprintf(stderr," -t : show transactions\n"); + fprintf(stderr," -u : undo the latest count transaction sets\n"); + fprintf(stderr," -v : show more information (-vv yet more)\n"); + fprintf(stderr," -V : show version and exit\n"); +} + +/* + * Process command options + */ + +static BOOL getoptions(int argc, char *argv[]) +{ + int c; + int xcount; + u32 xval; + char *endptr; + BOOL err; + static const char *sopt = "-bc:hifknp:r:stu:vVx:"; + static const struct option lopt[] = { + { "backward", no_argument, NULL, 'b' }, + { "clusters", required_argument, NULL, 'c' }, + { "forward", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "kill-fast-restart", no_argument, NULL, 'k' }, + { "no-action", no_argument, NULL, 'n' }, + { "play", required_argument, NULL, 'p' }, + { "range", required_argument, NULL, 'r' }, + { "sync", no_argument, NULL, 's' }, + { "transactions", no_argument, NULL, 't' }, + { "undo", required_argument, NULL, 'u' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { "exceptions", required_argument, NULL, 'x' }, + { NULL, 0, NULL, 0 } + }; + + err = FALSE; + optb = FALSE; + optc = FALSE; + optd = FALSE; + optf = FALSE; + opth = FALSE; + opti = FALSE; + optk = FALSE; + optn = FALSE; + optp = FALSE; + optr = FALSE; + opts = 0; + optt = FALSE; + optu = FALSE; + optv = 0; + optV = FALSE; + optx[0] = 0; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (optind == argc) + optd = TRUE; + else { + fprintf(stderr, "Device must be the" + " last argument.\n"); + err = TRUE; + } + break; + case 'b': + optb = TRUE; + break; + case 'c': + firstlcn = strtoull(optarg, &endptr, 0); + lastlcn = firstlcn; + if (*endptr == '-') + lastlcn = strtoull(++endptr, &endptr, 0); + if (*endptr || (lastlcn < firstlcn)) { + fprintf(stderr,"Bad cluster range\n"); + err = TRUE; + } else + optc = TRUE; + break; + case 'f': + optf = TRUE; + break; + case '?': + case 'h': + opth = TRUE; + break; + case 'k': + optk = TRUE; + break; + case 'n': + optn = TRUE; + break; + case 'p': + playcount = strtoull(optarg, &endptr, 0); + if (*endptr) { + fprintf(stderr,"Bad play count\n"); + err = TRUE; + } else + optp = TRUE; + break; + case 'r' : + firstblk = strtoull(optarg, &endptr, 0); + lastblk = firstblk; + if (*endptr == '-') + lastblk = strtoull(++endptr, &endptr, 0); + if (*endptr || (lastblk < firstblk)) { + fprintf(stderr,"Bad log block range\n"); + err = TRUE; + } else + optr = TRUE; + break; + case 's': + opts++; + break; + case 't': + optt = TRUE; + break; + case 'u': + playcount = strtoull(optarg, &endptr, 0); + if (*endptr) { + fprintf(stderr,"Bad undo count\n"); + err = TRUE; + } else + optu = TRUE; + break; + case 'v': + optv++; + break; + case 'V': + optV = TRUE; + break; + case 'x': + /* + * Undocumented : actions to execute, though + * they should be skipped under normal rules. + */ + xcount = 0; + xval = strtoull(optarg, &endptr, 0); + while ((*endptr == ',') + && (xcount < (MAXEXCEPTION - 1))) { + optx[xcount++] = xval; + xval = strtoull(++endptr, &endptr, 0); + } + if (*endptr || (xcount >= MAXEXCEPTION)) { + fprintf(stderr,"Bad exception list\n"); + err = TRUE; + } else { + optx[xcount++] = xval; + optx[xcount] = 0; + } + break; + default: + fprintf(stderr,"Unknown option '%s'.\n", + argv[optind - 1]); + err = TRUE; + } + } + + if (!optd && !optV && !opth) { + fprintf(stderr,"Device argument is missing\n"); + err = TRUE; + } + if (!(optb || optf || optp || optr || opts || optt || optu || optV)) + opts = 1; + if (optb && (optf || optr || opts)) { + fprintf(stderr,"Options -f, -r and -s are incompatible with -b\n"); + err = TRUE; + } + if (optf && (optp || opts || optu)) { + fprintf(stderr,"Options -p, -s and -u are incompatible with -f\n"); + err = TRUE; + } + if (optp && (optr || opts || optt || optu)) { + fprintf(stderr,"Options -r, -s, -t and -u are incompatible with -p\n"); + err = TRUE; + } + if (optr && (opts || optu)) { + fprintf(stderr,"Options -s and -u are incompatible with -r\n"); + err = TRUE; + } + if (opts && (optt || optu)) { + fprintf(stderr,"Options -t and -u are incompatible with -s\n"); + err = TRUE; + } + + if (opth || err) + usage(); + else + if (optV) + version(); + return (!err); +} + +/* + * Quick checks on the layout of needed structs + */ + +static BOOL checkstructs(void) +{ + BOOL ok; + + ok = TRUE; + if (sizeof(RECORD_PAGE_HEADER) != 40) { + fprintf(stderr, + "* error : bad sizeof(RECORD_PAGE_HEADER) %d\n", + (int)sizeof(RECORD_PAGE_HEADER)); + ok = FALSE; + } + if (sizeof(LOG_RECORD) != 88) { + fprintf(stderr, + "* error : bad sizeof(LOG_RECORD) %d\n", + (int)sizeof(LOG_RECORD)); + ok = FALSE; + } + if (sizeof(RESTART_PAGE_HEADER) != 32) { + fprintf(stderr, + "* error : bad sizeof(RESTART_PAGE_HEADER) %d\n", + (int)sizeof(RESTART_PAGE_HEADER)); + ok = FALSE; + } + if (sizeof(RESTART_AREA) != 48) { + fprintf(stderr, + "* error : bad sizeof(RESTART_AREA) %d\n", + (int)sizeof(RESTART_AREA)); + ok = FALSE; + } + if (sizeof(ATTR_OLD) != 44) { + fprintf(stderr, + "* error : bad sizeof(ATTR_OLD) %d\n", + (int)sizeof(ATTR_OLD)); + ok = FALSE; + } + if (sizeof(ATTR_NEW) != 40) { + fprintf(stderr, + "* error : bad sizeof(ATTR_NEW) %d\n", + (int)sizeof(ATTR_NEW)); + ok = FALSE; + } + if (LastAction != 38) { + fprintf(stderr, + "* error : bad action list, %d actions\n", + (int)LastAction); + ok = FALSE; + } + return (ok); +} + +int main(int argc, char *argv[]) +{ + CONTEXT ctx; + unsigned int i; + int err; + + err = 1; + if (checkstructs() + && getoptions(argc,argv)) { + if (optV || opth) { + err = 0; + } else { + redocount = 0; + undocount = 0; + actionnum = 0; + attrcount = 0; + redos_met = 0; + attrtable = (struct ATTR**)NULL; + for (i=0; i<(BUFFERCNT + BASEBLKS); i++) + buffer_table[i] = (struct BUFFER*)NULL; + ntfs_log_set_handler(ntfs_log_handler_outerr); + if (open_volume(&ctx, argv[argc - 1])) { + if (!ctx.vol + && (opts || optp || optu)) { + printf("Options -s, -p and -u" + " require a full device\n"); + err = 1; + } else { + err = walk(&ctx); + if (ctx.vol) { + if ((optp || optu || opts) + && !err + && !optn) { + reset_logfile(&ctx); + } + ntfs_attr_close(log_na); + ntfs_inode_close(log_ni); + ntfs_umount(ctx.vol, TRUE); + } else + fclose(ctx.file); + } + } else + fprintf(stderr,"Could not open %s\n", + argv[argc - 1]); + for (i=0; i<(BUFFERCNT + BASEBLKS); i++) + free(buffer_table[i]); + for (i=0; i +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif + +#include "param.h" +#include "debug.h" +#include "types.h" +#include "support.h" +#include "endians.h" +#include "bootsect.h" +#include "device.h" +#include "attrib.h" +#include "volume.h" +#include "mft.h" +#include "bitmap.h" +#include "inode.h" +#include "runlist.h" +#include "utils.h" +/* #include "version.h" */ +#include "misc.h" + +#define BAN_NEW_TEXT 1 /* Respect the ban on new messages */ +#define CLEAN_EXIT 0 /* traditionnally volume is not closed, there must be a reason */ + +static const char *EXEC_NAME = "ntfsresize"; + +static const char *resize_warning_msg = +"WARNING: Every sanity check passed and only the dangerous operations left.\n" +"Make sure that important data has been backed up! Power outage or computer\n" +"crash may result major data loss!\n"; + +static const char *resize_important_msg = +"You can go on to shrink the device for example with Linux fdisk.\n" +"IMPORTANT: When recreating the partition, make sure that you\n" +" 1) create it at the same disk sector (use sector as the unit!)\n" +" 2) create it with the same partition type (usually 7, HPFS/NTFS)\n" +" 3) do not make it smaller than the new NTFS filesystem size\n" +" 4) set the bootable flag for the partition if it existed before\n" +"Otherwise you won't be able to access NTFS or can't boot from the disk!\n" +"If you make a mistake and don't have a partition table backup then you\n" +"can recover the partition table by TestDisk or Parted's rescue mode.\n"; + +static const char *invalid_ntfs_msg = +"The device '%s' doesn't have a valid NTFS.\n" +"Maybe you selected the wrong partition? Or the whole disk instead of a\n" +"partition (e.g. /dev/hda, not /dev/hda1)? This error might also occur\n" +"if the disk was incorrectly repartitioned (see the ntfsresize FAQ).\n"; + +static const char *corrupt_volume_msg = +"NTFS is inconsistent. Run chkdsk /f on Windows then reboot it TWICE!\n" +"The usage of the /f parameter is very IMPORTANT! No modification was\n" +"and will be made to NTFS by this software until it gets repaired.\n"; + +static const char *hibernated_volume_msg = +"The NTFS partition is hibernated. Windows must be resumed and turned off\n" +"properly, so resizing could be done safely.\n"; + +static const char *unclean_journal_msg = +"The NTFS journal file is unclean. Please shutdown Windows properly before\n" +"using this software! Note, if you have run chkdsk previously then boot\n" +"Windows again which will automatically initialize the journal correctly.\n"; + +static const char *opened_volume_msg = +"This software has detected that the NTFS volume is already opened by another\n" +"software thus it refuses to progress to preserve data consistency.\n"; + +static const char *bad_sectors_warning_msg = +"****************************************************************************\n" +"* WARNING: The disk has bad sector. This means physical damage on the disk *\n" +"* surface caused by deterioration, manufacturing faults or other reason. *\n" +"* The reliability of the disk may stay stable or degrade fast. We suggest *\n" +"* making a full backup urgently by running 'ntfsclone --rescue ...' then *\n" +"* run 'chkdsk /f /r' on Windows and rebooot it TWICE! Then you can resize *\n" +"* NTFS safely by additionally using the --bad-sectors option of ntfsresize.*\n" +"****************************************************************************\n"; + +static const char *many_bad_sectors_msg = +"***************************************************************************\n" +"* WARNING: The disk has many bad sectors. This means physical damage *\n" +"* on the disk surface caused by deterioration, manufacturing faults or *\n" +"* other reason. We suggest to get a replacement disk as soon as possible. *\n" +"***************************************************************************\n"; + +enum mirror_source { MIRR_OLD, MIRR_NEWMFT, MIRR_MFT }; + +static struct { + int verbose; + int debug; + int ro_flag; + int force; + int info; + int infombonly; + int expand; + int reliable_size; + int show_progress; + int badsectors; + int check; + s64 bytes; + char *volume; +} opt; + +struct bitmap { + s64 size; + u8 *bm; +}; + +#define NTFS_PROGBAR 0x0001 +#define NTFS_PROGBAR_SUPPRESS 0x0002 + +struct progress_bar { + u64 start; + u64 stop; + int resolution; + int flags; + float unit; +}; + +struct llcn_t { + s64 lcn; /* last used LCN for a "special" file/attr type */ + s64 inode; /* inode using it */ +}; + +#define NTFSCK_PROGBAR 0x0001 + + /* runlists which have to be processed later */ +struct DELAYED { + struct DELAYED *next; + ATTR_TYPES type; + MFT_REF mref; + VCN lowest_vcn; + int name_len; + ntfschar *attr_name; + runlist_element *rl; + runlist *head_rl; +} ; + +typedef struct { + ntfs_inode *ni; /* inode being processed */ + ntfs_attr_search_ctx *ctx; /* inode attribute being processed */ + s64 inuse; /* num of clusters in use */ + int multi_ref; /* num of clusters referenced many times */ + int outsider; /* num of clusters outside the volume */ + int show_outsider; /* controls showing the above information */ + int flags; + struct bitmap lcn_bitmap; +} ntfsck_t; + +typedef struct { + ntfs_volume *vol; + ntfs_inode *ni; /* inode being processed */ + s64 new_volume_size; /* in clusters; 0 = --info w/o --size */ + MFT_REF mref; /* mft reference */ + MFT_RECORD *mrec; /* mft record */ + ntfs_attr_search_ctx *ctx; /* inode attribute being processed */ + u64 relocations; /* num of clusters to relocate */ + s64 inuse; /* num of clusters in use */ + runlist mftmir_rl; /* $MFTMirr AT_DATA's new position */ + s64 mftmir_old; /* $MFTMirr AT_DATA's old LCN */ + int dirty_inode; /* some inode data got relocated */ + int shrink; /* shrink = 1, enlarge = 0 */ + s64 badclusters; /* num of physically dead clusters */ + VCN mft_highest_vcn; /* used for relocating the $MFT */ + runlist_element *new_mft_start; /* new first run for $MFT:$DATA */ + struct DELAYED *delayed_runlists; /* runlists to process later */ + struct progress_bar progress; + struct bitmap lcn_bitmap; + /* Temporary statistics until all case is supported */ + struct llcn_t last_mft; + struct llcn_t last_mftmir; + struct llcn_t last_multi_mft; + struct llcn_t last_sparse; + struct llcn_t last_compressed; + struct llcn_t last_lcn; + s64 last_unsupp; /* last unsupported cluster */ + enum mirror_source mirr_from; +} ntfs_resize_t; + +/* FIXME: This, lcn_bitmap and pos from find_free_cluster() will make a cluster + allocation related structure, attached to ntfs_resize_t */ +static s64 max_free_cluster_range = 0; + +#define NTFS_MBYTE (1000 * 1000) + +/* WARNING: don't modify the text, external tools grep for it */ +#define ERR_PREFIX "ERROR" +#define PERR_PREFIX ERR_PREFIX "(%d): " +#define NERR_PREFIX ERR_PREFIX ": " + +#define DIRTY_NONE (0) +#define DIRTY_INODE (1) +#define DIRTY_ATTRIB (2) + +static s64 rounded_up_division(s64 numer, s64 denom) +{ + return (numer + (denom - 1)) / denom; +} + +/** + * perr_printf + * + * Print an error message. + */ +__attribute__((format(printf, 1, 2))) +static void perr_printf(const char *fmt, ...) +{ + va_list ap; + int eo = errno; + + fprintf(stdout, PERR_PREFIX, eo); + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + fprintf(stdout, ": %s\n", strerror(eo)); + fflush(stdout); + fflush(stderr); +} + +__attribute__((format(printf, 1, 2))) +static void err_printf(const char *fmt, ...) +{ + va_list ap; + + fprintf(stdout, NERR_PREFIX); + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + fflush(stdout); + fflush(stderr); +} + +/** + * err_exit + * + * Print and error message and exit the program. + */ +__attribute__((noreturn)) +__attribute__((format(printf, 1, 2))) +static void err_exit(const char *fmt, ...) +{ + va_list ap; + + fprintf(stdout, NERR_PREFIX); + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + fflush(stdout); + fflush(stderr); + exit(1); +} + +/** + * perr_exit + * + * Print and error message and exit the program + */ +__attribute__((noreturn)) +__attribute__((format(printf, 1, 2))) +static void perr_exit(const char *fmt, ...) +{ + va_list ap; + int eo = errno; + + fprintf(stdout, PERR_PREFIX, eo); + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + printf(": %s\n", strerror(eo)); + fflush(stdout); + fflush(stderr); + exit(1); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +__attribute__((noreturn)) +static void usage(int ret) +{ + + printf("\nUsage: %s [OPTIONS] DEVICE\n" + " Resize an NTFS volume non-destructively, safely move any data if needed.\n" + "\n" + " -c, --check Check to ensure that the device is ready for resize\n" + " -i, --info Estimate the smallest shrunken size or the smallest\n" + " expansion size\n" + " -m, --info-mb-only Estimate the smallest shrunken size possible,\n" + " output size in MB only\n" + " -s, --size SIZE Resize volume to SIZE[k|M|G] bytes\n" + " -x, --expand Expand to full partition\n" + "\n" + " -n, --no-action Do not write to disk\n" + " -b, --bad-sectors Support disks having bad sectors\n" + " -f, --force Force to progress\n" + " -P, --no-progress-bar Don't show progress bar\n" + " -v, --verbose More output\n" + " -V, --version Display version information\n" + " -h, --help Display this help\n" +#ifdef DEBUG + " -d, --debug Show debug information\n" +#endif + "\n" + " The options -i and -x are exclusive of option -s, and -m is exclusive\n" + " of option -x. If options -i, -m, -s and -x are are all omitted\n" + " then the NTFS volume will be enlarged to the DEVICE size.\n" + "\n", EXEC_NAME); + printf("%s%s", ntfs_bugs, ntfs_home); + printf("Ntfsresize FAQ: http://linux-ntfs.sourceforge.net/info/ntfsresize.html\n"); + exit(ret); +} + +/** + * proceed_question + * + * Force the user to confirm an action before performing it. + * Copy-paste from e2fsprogs + */ +static void proceed_question(void) +{ + char buf[256]; + const char *short_yes = "yY"; + + fflush(stdout); + fflush(stderr); + printf("Are you sure you want to proceed (y/[n])? "); + buf[0] = 0; + if (fgets(buf, sizeof(buf), stdin) + && !strchr(short_yes, buf[0])) { + printf("OK quitting. NO CHANGES have been made to your " + "NTFS volume.\n"); + exit(1); + } +} + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + printf("\nResize an NTFS Volume, without data loss.\n\n"); + printf("Copyright (c) 2002-2006 Szabolcs Szakacsits\n"); + printf("Copyright (c) 2002-2005 Anton Altaparmakov\n"); + printf("Copyright (c) 2002-2003 Richard Russon\n"); + printf("Copyright (c) 2007 Yura Pakhuchiy\n"); + printf("Copyright (c) 2011-2018 Jean-Pierre Andre\n"); + printf("\n%s\n%s%s", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * get_new_volume_size + * + * Convert a user-supplied string into a size. Without any suffix the number + * will be assumed to be in bytes. If the number has a suffix of k, M or G it + * will be scaled up by 1000, 1000000, or 1000000000. + */ +static s64 get_new_volume_size(char *s) +{ + s64 size; + char *suffix; + int prefix_kind = 1000; + + size = strtoll(s, &suffix, 10); + if (size <= 0 || errno == ERANGE) + err_exit("Illegal new volume size\n"); + + if (!*suffix) { + opt.reliable_size = 1; + return size; + } + + if (strlen(suffix) == 2 && suffix[1] == 'i') + prefix_kind = 1024; + else if (strlen(suffix) > 1) + usage(1); + + /* We follow the SI prefixes: + http://physics.nist.gov/cuu/Units/prefixes.html + http://physics.nist.gov/cuu/Units/binary.html + Disk partitioning tools use prefixes as, + k M G + fdisk 2.11x- 2^10 2^20 10^3*2^20 + fdisk 2.11y+ 10^3 10^6 10^9 + cfdisk 10^3 10^6 10^9 + sfdisk 2^10 2^20 + parted 2^10 2^20 (may change) + fdisk (DOS) 2^10 2^20 + */ + /* FIXME: check for overflow */ + switch (*suffix) { + case 'G': + size *= prefix_kind; + /* FALLTHRU */ + case 'M': + size *= prefix_kind; + /* FALLTHRU */ + case 'k': + size *= prefix_kind; + break; + default: + usage(1); + } + + return size; +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char **argv) +{ + static const char *sopt = "-bcdfhimnPs:vVx"; + static const struct option lopt[] = { + { "bad-sectors",no_argument, NULL, 'b' }, + { "check", no_argument, NULL, 'c' }, +#ifdef DEBUG + { "debug", no_argument, NULL, 'd' }, +#endif + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "info", no_argument, NULL, 'i' }, + { "info-mb-only", no_argument, NULL, 'm' }, + { "no-action", no_argument, NULL, 'n' }, + { "no-progress-bar", no_argument, NULL, 'P' }, + { "size", required_argument, NULL, 's' }, + { "expand", no_argument, NULL, 'x' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + + int c; + int err = 0; + int ver = 0; + int help = 0; + + memset(&opt, 0, sizeof(opt)); + opt.show_progress = 1; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!err && !opt.volume) + opt.volume = argv[optind-1]; + else + err++; + break; + case 'b': + opt.badsectors++; + break; + case 'c': + opt.check++; + break; + case 'd': + opt.debug++; + break; + case 'f': + opt.force++; + break; + case 'h': + help++; + break; + case 'i': + opt.info++; + break; + case 'm': + opt.infombonly++; + break; + case 'n': + opt.ro_flag = NTFS_MNT_RDONLY; + break; + case 'P': + opt.show_progress = 0; + break; + case 's': + if (!err && (opt.bytes == 0)) + opt.bytes = get_new_volume_size(optarg); + else + err++; + break; + case 'v': + opt.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + ver++; + break; + case 'x': + opt.expand++; + break; + case '?': + default: + if (optopt == 's') { + printf("Option '%s' requires an argument.\n", argv[optind-1]); + } else { + printf("Unknown option '%s'.\n", argv[optind-1]); + } + err++; + break; + } + } + + if (!help && !ver) { + if (opt.volume == NULL) { + if (argc > 1) + printf("You must specify exactly one device.\n"); + err++; + } + if (opt.info || opt.infombonly) { + opt.ro_flag = NTFS_MNT_RDONLY; + } + if (opt.bytes + && (opt.expand || opt.info || opt.infombonly)) { + printf(NERR_PREFIX "Options --info(-mb-only) and --expand " + "cannot be used with --size.\n"); + usage(1); + } + if (opt.expand && opt.infombonly) { + printf(NERR_PREFIX "Options --info-mb-only " + "cannot be used with --expand.\n"); + usage(1); + } + } + + /* Redirect stderr to stdout, note fflush()es are essential! */ + fflush(stdout); + fflush(stderr); + if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1) + perr_exit("Failed to redirect stderr to stdout"); + fflush(stdout); + fflush(stderr); + +#ifdef DEBUG + if (!opt.debug) + if (!freopen("/dev/null", "w", stderr)) + perr_exit("Failed to redirect stderr to /dev/null"); +#endif + + if (ver) + version(); + if (help || err) + usage(err > 0); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver ? 0 : -1)); +} + +static void print_advise(ntfs_volume *vol, s64 supp_lcn) +{ + s64 old_b, new_b, freed_b, old_mb, new_mb, freed_mb; + + old_b = vol->nr_clusters * vol->cluster_size; + old_mb = rounded_up_division(old_b, NTFS_MBYTE); + + /* Take the next supported cluster (free or relocatable) + plus reserve a cluster for the backup boot sector */ + supp_lcn += 2; + + if (supp_lcn > vol->nr_clusters) { + err_printf("Very rare fragmentation type detected. " + "Sorry, it's not supported yet.\n" + "Try to defragment your NTFS, perhaps it helps.\n"); + exit(1); + } + + new_b = supp_lcn * vol->cluster_size; + new_mb = rounded_up_division(new_b, NTFS_MBYTE); + freed_b = (vol->nr_clusters - supp_lcn + 1) * vol->cluster_size; + freed_mb = freed_b / NTFS_MBYTE; + + /* WARNING: don't modify the text, external tools grep for it */ + if (!opt.infombonly) + printf("You might resize at %lld bytes ", (long long)new_b); + if ((new_mb * NTFS_MBYTE) < old_b) { + if (!opt.infombonly) + printf("or %lld MB ", (long long)new_mb); + else + printf("Minsize (in MB): %lld\n", (long long)new_mb); + } + + if (!opt.infombonly) { + printf("(freeing "); + if (freed_mb && (old_mb - new_mb)) + printf("%lld MB", (long long)(old_mb - new_mb)); + else + printf("%lld bytes", (long long)freed_b); + printf(").\n"); + + printf("Please make a test run using both the -n and -s " + "options before real resizing!\n"); + } +} + +static void rl_set(runlist *rl, VCN vcn, LCN lcn, s64 len) +{ + rl->vcn = vcn; + rl->lcn = lcn; + rl->length = len; +} + +static int rl_items(runlist *rl) +{ + int i = 0; + + while (rl[i++].length) + ; + + return i; +} + +static void dump_run(runlist_element *r) +{ + ntfs_log_verbose(" %8lld %8lld (0x%08llx) %lld\n", (long long)r->vcn, + (long long)r->lcn, (long long)r->lcn, + (long long)r->length); +} + +static void dump_runlist(runlist *rl) +{ + while (rl->length) + dump_run(rl++); +} + +/** + * nr_clusters_to_bitmap_byte_size + * + * Take the number of clusters in the volume and calculate the size of $Bitmap. + * The size must be always a multiple of 8 bytes. + */ +static s64 nr_clusters_to_bitmap_byte_size(s64 nr_clusters) +{ + s64 bm_bsize; + + bm_bsize = rounded_up_division(nr_clusters, 8); + bm_bsize = (bm_bsize + 7) & ~7; + + return bm_bsize; +} + +static void collect_resize_constraints(ntfs_resize_t *resize, runlist *rl) +{ + s64 inode, last_lcn; + ATTR_FLAGS flags; + ATTR_TYPES atype; + struct llcn_t *llcn = NULL; + int ret, supported = 0; + + last_lcn = rl->lcn + (rl->length - 1); + + inode = resize->ni->mft_no; + flags = resize->ctx->attr->flags; + atype = resize->ctx->attr->type; + + if ((ret = ntfs_inode_badclus_bad(inode, resize->ctx->attr)) != 0) { + if (ret == -1) + perr_exit("Bad sector list check failed"); + return; + } + + if (inode == FILE_Bitmap) { + llcn = &resize->last_lcn; + if (atype == AT_DATA && NInoAttrList(resize->ni)) + err_exit("Highly fragmented $Bitmap isn't supported yet."); + + supported = 1; + + } else if (NInoAttrList(resize->ni)) { + llcn = &resize->last_multi_mft; + + if (inode != FILE_MFTMirr) + supported = 1; + + } else if (flags & ATTR_IS_SPARSE) { + llcn = &resize->last_sparse; + supported = 1; + + } else if (flags & ATTR_IS_COMPRESSED) { + llcn = &resize->last_compressed; + supported = 1; + + } else if (inode == FILE_MFTMirr) { + llcn = &resize->last_mftmir; + supported = 1; + + /* Fragmented $MFTMirr DATA attribute isn't supported yet */ + if (atype == AT_DATA) + if (rl[1].length != 0 || rl->vcn) + supported = 0; + } else { + llcn = &resize->last_lcn; + supported = 1; + } + + if (llcn->lcn < last_lcn) { + llcn->lcn = last_lcn; + llcn->inode = inode; + } + + if (supported) + return; + + if (resize->last_unsupp < last_lcn) + resize->last_unsupp = last_lcn; +} + + +static void collect_relocation_info(ntfs_resize_t *resize, runlist *rl) +{ + s64 lcn, lcn_length, start, len, inode; + s64 new_vol_size; /* (last LCN on the volume) + 1 */ + + lcn = rl->lcn; + lcn_length = rl->length; + inode = resize->ni->mft_no; + new_vol_size = resize->new_volume_size; + + if (lcn + lcn_length <= new_vol_size) + return; + + if (inode == FILE_Bitmap && resize->ctx->attr->type == AT_DATA) + return; + + start = lcn; + len = lcn_length; + + if (lcn < new_vol_size) { + start = new_vol_size; + len = lcn_length - (new_vol_size - lcn); + + if ((!opt.info && !opt.infombonly) && (inode == FILE_MFTMirr)) { + err_printf("$MFTMirr can't be split up yet. Please try " + "a different size.\n"); + print_advise(resize->vol, lcn + lcn_length - 1); + exit(1); + } + } + + resize->relocations += len; + + if ((!opt.info && !opt.infombonly) || !resize->new_volume_size) + return; + + printf("Relocation needed for inode %8lld attr 0x%x LCN 0x%08llx " + "length %6lld\n", (long long)inode, + (unsigned int)le32_to_cpu(resize->ctx->attr->type), + (unsigned long long)start, (long long)len); +} + +/** + * build_lcn_usage_bitmap + * + * lcn_bitmap has one bit for each cluster on the disk. Initially, lcn_bitmap + * has no bits set. As each attribute record is read the bits in lcn_bitmap are + * checked to ensure that no other file already references that cluster. + * + * This serves as a rudimentary "chkdsk" operation. + */ +static void build_lcn_usage_bitmap(ntfs_volume *vol, ntfsck_t *fsck) +{ + s64 inode; + ATTR_RECORD *a; + runlist *rl; + int i, j; + struct bitmap *lcn_bitmap = &fsck->lcn_bitmap; + + a = fsck->ctx->attr; + inode = fsck->ni->mft_no; + + if (!a->non_resident) + return; + + if (!(rl = ntfs_mapping_pairs_decompress(vol, a, NULL))) { + int err = errno; + perr_printf("ntfs_decompress_mapping_pairs"); + if (err == EIO) + printf("%s", corrupt_volume_msg); + exit(1); + } + + + for (i = 0; rl[i].length; i++) { + s64 lcn = rl[i].lcn; + s64 lcn_length = rl[i].length; + + /* CHECKME: LCN_RL_NOT_MAPPED check isn't needed */ + if (lcn == LCN_HOLE || lcn == LCN_RL_NOT_MAPPED) + continue; + + /* FIXME: ntfs_mapping_pairs_decompress should return error */ + if (lcn < 0 || lcn_length <= 0) + err_exit("Corrupt runlist in inode %lld attr %x LCN " + "%llx length %llx\n", (long long)inode, + (unsigned int)le32_to_cpu(a->type), + (long long)lcn, (long long)lcn_length); + + for (j = 0; j < lcn_length; j++) { + u64 k = (u64)lcn + j; + + if (k >= (u64)vol->nr_clusters) { + long long outsiders = lcn_length - j; + + fsck->outsider += outsiders; + + if (++fsck->show_outsider <= 10 || opt.verbose) + printf("Outside of the volume reference" + " for inode %lld at %lld:%lld\n", + (long long)inode, (long long)k, + (long long)outsiders); + + break; + } + + if (ntfs_bit_get_and_set(lcn_bitmap->bm, k, 1)) { + if (++fsck->multi_ref <= 10 || opt.verbose) + printf("Cluster %lld is referenced " + "multiple times!\n", + (long long)k); + continue; + } + } + fsck->inuse += lcn_length; + } + free(rl); +} + + +static ntfs_attr_search_ctx *attr_get_search_ctx(ntfs_inode *ni, MFT_RECORD *mrec) +{ + ntfs_attr_search_ctx *ret; + + if ((ret = ntfs_attr_get_search_ctx(ni, mrec)) == NULL) + perr_printf("ntfs_attr_get_search_ctx"); + + return ret; +} + +/** + * walk_attributes + * + * For a given MFT Record, iterate through all its attributes. Any non-resident + * data runs will be marked in lcn_bitmap. + */ +static int walk_attributes(ntfs_volume *vol, ntfsck_t *fsck) +{ + if (!(fsck->ctx = attr_get_search_ctx(fsck->ni, NULL))) + return -1; + + while (!ntfs_attrs_walk(fsck->ctx)) { + if (fsck->ctx->attr->type == AT_END) + break; + build_lcn_usage_bitmap(vol, fsck); + } + + ntfs_attr_put_search_ctx(fsck->ctx); + return 0; +} + +/** + * compare_bitmaps + * + * Compare two bitmaps. In this case, $Bitmap as read from the disk and + * lcn_bitmap which we built from the MFT Records. + */ +static void compare_bitmaps(ntfs_volume *vol, struct bitmap *a) +{ + s64 i, pos, count; + int mismatch = 0; + int backup_boot = 0; + u8 bm[NTFS_BUF_SIZE]; + + if (!opt.infombonly) + printf("Accounting clusters ...\n"); + + pos = 0; + while (1) { + count = ntfs_attr_pread(vol->lcnbmp_na, pos, NTFS_BUF_SIZE, bm); + if (count == -1) + perr_exit("Couldn't get $Bitmap $DATA"); + + if (count == 0) { + if (a->size > pos) + err_exit("$Bitmap size is smaller than expected" + " (%lld != %lld)\n", + (long long)a->size, (long long)pos); + break; + } + + for (i = 0; i < count; i++, pos++) { + s64 cl; /* current cluster */ + + if (a->size <= pos) + goto done; + + if (a->bm[pos] == bm[i]) + continue; + + for (cl = pos * 8; cl < (pos + 1) * 8; cl++) { + char bit; + + bit = ntfs_bit_get(a->bm, cl); + if (bit == ntfs_bit_get(bm, i * 8 + cl % 8)) + continue; + + if (!mismatch && !bit && !backup_boot && + cl == vol->nr_clusters / 2) { + /* FIXME: call also boot sector check */ + backup_boot = 1; + printf("Found backup boot sector in " + "the middle of the volume.\n"); + continue; + } + + if (++mismatch > 10 && !opt.verbose) + continue; + + printf("Cluster accounting failed at %lld " + "(0x%llx): %s cluster in " + "$Bitmap\n", (long long)cl, + (unsigned long long)cl, + bit ? "missing" : "extra"); + } + } + } +done: + if (mismatch) { + printf("Filesystem check failed! Totally %d cluster " + "accounting mismatches.\n", mismatch); + err_printf("%s", corrupt_volume_msg); + exit(1); + } +} + +/** + * progress_init + * + * Create and scale our progress bar. + */ +static void progress_init(struct progress_bar *p, u64 start, u64 stop, int flags) +{ + p->start = start; + p->stop = stop; + p->unit = 100.0 / (stop - start); + p->resolution = 100; + p->flags = flags; +} + +/** + * progress_update + * + * Update the progress bar and tell the user. + */ +static void progress_update(struct progress_bar *p, u64 current) +{ + float percent; + + if (!(p->flags & NTFS_PROGBAR)) + return; + if (p->flags & NTFS_PROGBAR_SUPPRESS) + return; + + /* WARNING: don't modify the texts, external tools grep for them */ + percent = p->unit * current; + if (current != p->stop) { + if ((current - p->start) % p->resolution) + return; + printf("%6.2f percent completed\r", percent); + } else + printf("100.00 percent completed\n"); + fflush(stdout); +} + +static int inode_close(ntfs_inode *ni) +{ + if (ntfs_inode_close(ni)) { + perr_printf("ntfs_inode_close for inode %llu", + (unsigned long long)ni->mft_no); + return -1; + } + return 0; +} + +/** + * walk_inodes + * + * Read each record in the MFT, skipping the unused ones, and build up a bitmap + * from all the non-resident attributes. + */ +static int build_allocation_bitmap(ntfs_volume *vol, ntfsck_t *fsck) +{ + s64 nr_mft_records, inode = 0; + ntfs_inode *ni; + struct progress_bar progress; + int pb_flags = 0; /* progress bar flags */ + + /* WARNING: don't modify the text, external tools grep for it */ + if (!opt.infombonly) + printf("Checking filesystem consistency ...\n"); + + if (fsck->flags & NTFSCK_PROGBAR) + pb_flags |= NTFS_PROGBAR; + + nr_mft_records = vol->mft_na->initialized_size >> + vol->mft_record_size_bits; + + progress_init(&progress, inode, nr_mft_records - 1, pb_flags); + + for (; inode < nr_mft_records; inode++) { + if (!opt.infombonly) + progress_update(&progress, inode); + + if ((ni = ntfs_inode_open(vol, (MFT_REF)inode)) == NULL) { + /* FIXME: continue only if it make sense, e.g. + MFT record not in use based on $MFT bitmap */ + if (errno == EIO || errno == ENOENT) + continue; + perr_printf("Reading inode %lld failed", + (long long)inode); + return -1; + } + + if (ni->mrec->base_mft_record) + goto close_inode; + + fsck->ni = ni; + if (walk_attributes(vol, fsck) != 0) { + inode_close(ni); + return -1; + } +close_inode: + if (inode_close(ni) != 0) + return -1; + } + return 0; +} + +static void build_resize_constraints(ntfs_resize_t *resize) +{ + s64 i; + runlist *rl; + + if (!resize->ctx->attr->non_resident) + return; + + if (!(rl = ntfs_mapping_pairs_decompress(resize->vol, + resize->ctx->attr, NULL))) + perr_exit("ntfs_decompress_mapping_pairs"); + + for (i = 0; rl[i].length; i++) { + /* CHECKME: LCN_RL_NOT_MAPPED check isn't needed */ + if (rl[i].lcn == LCN_HOLE || rl[i].lcn == LCN_RL_NOT_MAPPED) + continue; + + collect_resize_constraints(resize, rl + i); + if (resize->shrink) + collect_relocation_info(resize, rl + i); + } + free(rl); +} + +static void resize_constraints_by_attributes(ntfs_resize_t *resize) +{ + if (!(resize->ctx = attr_get_search_ctx(resize->ni, NULL))) + exit(1); + + while (!ntfs_attrs_walk(resize->ctx)) { + if (resize->ctx->attr->type == AT_END) + break; + build_resize_constraints(resize); + } + + ntfs_attr_put_search_ctx(resize->ctx); +} + +static void set_resize_constraints(ntfs_resize_t *resize) +{ + s64 nr_mft_records, inode; + ntfs_inode *ni; + + if (!opt.infombonly) + printf("Collecting resizing constraints ...\n"); + + nr_mft_records = resize->vol->mft_na->initialized_size >> + resize->vol->mft_record_size_bits; + + for (inode = 0; inode < nr_mft_records; inode++) { + + ni = ntfs_inode_open(resize->vol, (MFT_REF)inode); + if (ni == NULL) { + if (errno == EIO || errno == ENOENT) + continue; + perr_exit("Reading inode %lld failed", + (long long)inode); + } + + if (ni->mrec->base_mft_record) + goto close_inode; + + resize->ni = ni; + resize_constraints_by_attributes(resize); +close_inode: + if (inode_close(ni) != 0) + exit(1); + } +} + +static void rl_fixup(runlist **rl) +{ + runlist *tmp = *rl; + + if (tmp->lcn == LCN_RL_NOT_MAPPED) { + s64 unmapped_len = tmp->length; + + ntfs_log_verbose("Skip unmapped run at the beginning ...\n"); + + if (!tmp->length) + err_exit("Empty unmapped runlist! Please report!\n"); + (*rl)++; + for (tmp = *rl; tmp->length; tmp++) + tmp->vcn -= unmapped_len; + } + + for (tmp = *rl; tmp->length; tmp++) { + if (tmp->lcn == LCN_RL_NOT_MAPPED) { + ntfs_log_verbose("Skip unmapped run at the end ...\n"); + + if (tmp[1].length) + err_exit("Unmapped runlist in the middle! " + "Please report!\n"); + tmp->lcn = LCN_ENOENT; + tmp->length = 0; + } + } +} + +/* + * Plug a replacement (partial) runlist into full runlist + * + * Returns 0 if successful + * -1 if failed + */ + +static int replace_runlist(ntfs_attr *na, const runlist_element *reprl, + VCN lowest_vcn) +{ + const runlist_element *prep; + const runlist_element *pold; + runlist_element *pnew; + runlist_element *newrl; + VCN nextvcn; + s32 oldcnt, newcnt; + s32 newsize; + int r; + + r = -1; /* default return */ + /* allocate a new runlist able to hold both */ + oldcnt = 0; + while (na->rl[oldcnt].length) + oldcnt++; + newcnt = 0; + while (reprl[newcnt].length) + newcnt++; + newsize = ((oldcnt + newcnt)*sizeof(runlist_element) + 4095) & -4096; + newrl = (runlist_element*)malloc(newsize); + if (newrl) { + /* copy old runs until reaching replaced ones */ + pnew = newrl; + pold = na->rl; + while (pold->length + && ((pold->vcn + pold->length) + <= (reprl[0].vcn + lowest_vcn))) { + *pnew = *pold; + pnew++; + pold++; + } + /* split a possible old run partially overlapped */ + if (pold->length + && (pold->vcn < (reprl[0].vcn + lowest_vcn))) { + pnew->vcn = pold->vcn; + pnew->lcn = pold->lcn; + pnew->length = reprl[0].vcn + lowest_vcn - pold->vcn; + pnew++; + } + /* copy new runs */ + prep = reprl; + nextvcn = prep->vcn + lowest_vcn; + while (prep->length) { + pnew->vcn = prep->vcn + lowest_vcn; + pnew->lcn = prep->lcn; + pnew->length = prep->length; + nextvcn = pnew->vcn + pnew->length; + pnew++; + prep++; + } + /* locate the first fully replaced old run */ + while (pold->length + && ((pold->vcn + pold->length) <= nextvcn)) { + pold++; + } + /* split a possible old run partially overlapped */ + if (pold->length + && (pold->vcn < nextvcn)) { + pnew->vcn = nextvcn; + pnew->lcn = pold->lcn + nextvcn - pold->vcn; + pnew->length = pold->length - nextvcn + pold->vcn; + pnew++; + } + /* copy old runs beyond replaced ones */ + while (pold->length) { + *pnew = *pold; + pnew++; + pold++; + } + /* the terminator is same as the old one */ + *pnew = *pold; + /* deallocate the old runlist and replace */ + free(na->rl); + na->rl = newrl; + r = 0; + } + return (r); +} + +/* + * Expand the new runlist in new extent(s) + * + * This implies allocating inode extents and, generally, creating + * an attribute list and allocating clusters for the list, and + * shuffle the existing attributes accordingly. + * + * Sometimes the runlist being reallocated is within an extent, + * so we have a partial runlist to plug into an existing one + * whose other parts have already been processed or will have + * to be processed later, and we must not interfere with the + * processing of these parts. + * + * This cannot be done on the runlist part stored in a single + * extent, it has to be done globally for the file. + * + * We use the standard library functions, so we must wait until + * the new global bitmap and the new MFT bitmap are saved to + * disk and usable for the allocation of a new extent and creation + * of an attribute list. + * + * Aborts if something goes wrong. There should be no data damage, + * because the old runlist is still in use and the bootsector has + * not been updated yet, so the initial clusters can be accessed. + */ + +static void expand_attribute_runlist(ntfs_volume *vol, struct DELAYED *delayed) +{ + ntfs_inode *ni; + ntfs_attr *na; + ATTR_TYPES type; + MFT_REF mref; + runlist_element *rl; + + /* open the inode */ + mref = delayed->mref; +#ifndef BAN_NEW_TEXT + ntfs_log_verbose("Processing a delayed update for inode %lld\n", + (long long)mref); +#endif + type = delayed->type; + rl = delayed->rl; + + /* The MFT inode is permanently open, do not reopen or close */ + if (mref == FILE_MFT) + ni = vol->mft_ni; + else + ni = ntfs_inode_open(vol,mref); + if (ni) { + if (mref == FILE_MFT) + na = (type == AT_DATA ? vol->mft_na : vol->mftbmp_na); + else + na = ntfs_attr_open(ni, type, + delayed->attr_name, delayed->name_len); + if (na) { + /* + * The runlist is first updated in memory, and + * the updated one is used for updating on device + */ + if (!ntfs_attr_map_whole_runlist(na)) { + if (replace_runlist(na,rl,delayed->lowest_vcn) + || ntfs_attr_update_mapping_pairs(na,0)) + perr_exit("Could not update runlist " + "for attribute 0x%lx in inode %lld", + (long)le32_to_cpu(type),(long long)mref); + } else + perr_exit("Could not map attribute 0x%lx in inode %lld", + (long)le32_to_cpu(type),(long long)mref); + if (mref != FILE_MFT) + ntfs_attr_close(na); + } else + perr_exit("Could not open attribute 0x%lx in inode %lld", + (long)le32_to_cpu(type),(long long)mref); + ntfs_inode_mark_dirty(ni); + if ((mref != FILE_MFT) && ntfs_inode_close(ni)) + perr_exit("Failed to close inode %lld through the library", + (long long)mref); + } else + perr_exit("Could not open inode %lld through the library", + (long long)mref); +} + +/* + * Reload the MFT before merging delayed updates of runlist + * + * The delayed updates of runlists are those which imply updating + * the runlists which overflow from their original MFT record. + * Such updates must be done in the new location of the MFT and + * the allocations must be recorded in the new location of the + * MFT bitmap. + * The MFT data and MFT bitmap may themselves have delayed parts + * of their runlists, and at this stage, their runlists may have + * been partially updated on disk, and partially to be updated. + * Their in-memory runlists still point at the old location, they + * are obsolete, and we have to read the partially updated runlist + * from the device before merging the delayed updates. + * + * Returns 0 if successful + * -1 otherwise + */ + +static int reload_mft(ntfs_resize_t *resize) +{ + ntfs_inode *ni; + ntfs_attr *na; + int r; + int xi; + + r = 0; + /* get the base inode */ + ni = resize->vol->mft_ni; + if (!ntfs_file_record_read(resize->vol, FILE_MFT, &ni->mrec, NULL)) { + for (xi=0; !r && xivol->mft_ni->nr_extents; xi++) { + r = ntfs_file_record_read(resize->vol, + ni->extent_nis[xi]->mft_no, + &ni->extent_nis[xi]->mrec, NULL); + } + + if (!r) { + /* reopen the MFT bitmap, and swap vol->mftbmp_na */ + na = ntfs_attr_open(resize->vol->mft_ni, + AT_BITMAP, NULL, 0); + if (na && !ntfs_attr_map_whole_runlist(na)) { + ntfs_attr_close(resize->vol->mftbmp_na); + resize->vol->mftbmp_na = na; + } else + r = -1; + } + + if (!r) { + /* reopen the MFT data, and swap vol->mft_na */ + na = ntfs_attr_open(resize->vol->mft_ni, + AT_DATA, NULL, 0); + if (na && !ntfs_attr_map_whole_runlist(na)) { + ntfs_attr_close(resize->vol->mft_na); + resize->vol->mft_na = na; + } else + r = -1; + } + } else + r = -1; + return (r); +} + +/* + * Re-record the MFT extents in MFT bitmap + * + * When both MFT data and MFT bitmap have delayed runlists, MFT data + * is updated first, and the extents may be recorded at old location. + */ + +static int record_mft_in_bitmap(ntfs_resize_t *resize) +{ + ntfs_inode *ni; + int r; + int xi; + + r = 0; + /* get the base inode */ + ni = resize->vol->mft_ni; + for (xi=0; !r && xivol->mft_ni->nr_extents; xi++) { + r = ntfs_bitmap_set_run(resize->vol->mftbmp_na, + ni->extent_nis[xi]->mft_no, 1); + } + return (r); +} + +/* + * Process delayed runlist updates + */ + +static void delayed_updates(ntfs_resize_t *resize) +{ + struct DELAYED *delayed; + struct DELAYED *delayed_mft_data; + int nr_extents; + + if (ntfs_volume_get_free_space(resize->vol)) + err_exit("Failed to determine free space\n"); + + delayed_mft_data = (struct DELAYED*)NULL; + if (resize->delayed_runlists && reload_mft(resize)) + err_exit("Failed to reload the MFT for delayed updates\n"); + + /* + * Important : updates to MFT must come first, so that + * the new location of MFT is used for adding needed extents. + * Now, there are runlists in the MFT bitmap and MFT data. + * Extents to MFT bitmap have to be stored in the new MFT + * data, and extents to MFT data have to be recorded in + * the MFT bitmap. + * So we update MFT data first, and we record the MFT + * extents again in the MFT bitmap if they were recorded + * in the old location. + * + * However, if we are operating in "no action" mode, the + * MFT records to update are not written to their new location + * and the MFT data runlist has to be updated last in order + * to have the entries read from their old location. + * In this situation the MFT bitmap is never written to + * disk, so the same extents are reallocated repeatedly, + * which is not what would be done in a real resizing. + */ + + if (opt.ro_flag + && resize->delayed_runlists + && (resize->delayed_runlists->mref == FILE_MFT) + && (resize->delayed_runlists->type == AT_DATA)) { + /* Update the MFT data runlist later */ + delayed_mft_data = resize->delayed_runlists; + resize->delayed_runlists = resize->delayed_runlists->next; + } + + while (resize->delayed_runlists) { + delayed = resize->delayed_runlists; + expand_attribute_runlist(resize->vol, delayed); + if (delayed->mref == FILE_MFT) { + if (delayed->type == AT_BITMAP) + record_mft_in_bitmap(resize); + if (delayed->type == AT_DATA) + resize->mirr_from = MIRR_MFT; + } + resize->delayed_runlists = resize->delayed_runlists->next; + if (delayed->attr_name) + free(delayed->attr_name); + free(delayed->head_rl); + free(delayed); + } + if (opt.ro_flag && delayed_mft_data) { + /* in "no action" mode, check updating the MFT runlist now */ + expand_attribute_runlist(resize->vol, delayed_mft_data); + resize->mirr_from = MIRR_MFT; + if (delayed_mft_data->attr_name) + free(delayed_mft_data->attr_name); + free(delayed_mft_data->head_rl); + free(delayed_mft_data); + } + /* Beware of MFT fragmentation when the target size is too small */ + nr_extents = resize->vol->mft_ni->nr_extents; + if (nr_extents > 2) { + printf("WARNING: The MFT is now severely fragmented" + " (%d extents)\n", nr_extents); + } +} + +/* + * Queue a runlist replacement for later update + * + * Store the attribute identification relative to base inode + */ + +static void replace_later(ntfs_resize_t *resize, runlist *rl, runlist *head_rl) +{ + struct DELAYED *delayed; + struct DELAYED *previous; + ATTR_RECORD *a; + MFT_REF mref; + leMFT_REF lemref; + int name_len; + ntfschar *attr_name; + + /* save the attribute parameters, to be able to find it later */ + a = resize->ctx->attr; + name_len = a->name_length; + attr_name = (ntfschar*)NULL; + if (name_len) { + attr_name = (ntfschar*)ntfs_malloc(name_len*sizeof(ntfschar)); + if (attr_name) + memcpy(attr_name,(u8*)a + le16_to_cpu(a->name_offset), + name_len*sizeof(ntfschar)); + } + delayed = (struct DELAYED*)ntfs_malloc(sizeof(struct DELAYED)); + if (delayed && (attr_name || !name_len)) { + lemref = resize->ctx->mrec->base_mft_record; + if (lemref) + mref = le64_to_cpu(lemref); + else + mref = resize->mref; + delayed->mref = MREF(mref); + delayed->type = a->type; + delayed->attr_name = attr_name; + delayed->name_len = name_len; + delayed->lowest_vcn = sle64_to_cpu(a->lowest_vcn); + delayed->rl = rl; + delayed->head_rl = head_rl; + /* Queue ahead of list if this is MFT or head is not MFT */ + if ((delayed->mref == FILE_MFT) + || !resize->delayed_runlists + || (resize->delayed_runlists->mref != FILE_MFT)) { + delayed->next = resize->delayed_runlists; + resize->delayed_runlists = delayed; + } else { + /* Queue after all MFTs is this is not MFT */ + previous = resize->delayed_runlists; + while (previous->next + && (previous->next->mref == FILE_MFT)) + previous = previous->next; + delayed->next = previous->next; + previous->next = delayed; + } + } else + perr_exit("Could not store delayed update data"); +} + +/* + * Replace the runlist in an attribute + * + * This sometimes requires expanding the runlist into another extent, + * which has to be done globally on the attribute. Is so, the action + * is put in a delay queue, and the caller must not free the runlist. + * + * Returns 0 if the replacement could be done + * 1 when it has been put in the delay queue. + */ + +static int replace_attribute_runlist(ntfs_resize_t *resize, runlist *rl) +{ + int mp_size, l; + int must_delay; + void *mp; + runlist *head_rl; + ntfs_volume *vol; + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *a; + + vol = resize->vol; + ctx = resize->ctx; + a = ctx->attr; + head_rl = rl; + rl_fixup(&rl); + + if ((mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, 0, INT_MAX)) == -1) + perr_exit("ntfs_get_size_for_mapping_pairs"); + + if (a->name_length) { + u16 name_offs = le16_to_cpu(a->name_offset); + u16 mp_offs = le16_to_cpu(a->mapping_pairs_offset); + + if (name_offs >= mp_offs) + err_exit("Attribute name is after mapping pairs! " + "Please report!\n"); + } + + /* CHECKME: don't trust mapping_pairs is always the last item in the + attribute, instead check for the real size/space */ + l = (int)le32_to_cpu(a->length) - le16_to_cpu(a->mapping_pairs_offset); + must_delay = 0; + if (mp_size > l) { + s32 remains_size; + char *next_attr; + + ntfs_log_verbose("Enlarging attribute header ...\n"); + + mp_size = (mp_size + 7) & ~7; + + ntfs_log_verbose("Old mp size : %d\n", l); + ntfs_log_verbose("New mp size : %d\n", mp_size); + ntfs_log_verbose("Bytes in use : %u\n", (unsigned int) + le32_to_cpu(ctx->mrec->bytes_in_use)); + + next_attr = (char *)a + le32_to_cpu(a->length); + l = mp_size - l; + + ntfs_log_verbose("Bytes in use new : %u\n", l + (unsigned int) + le32_to_cpu(ctx->mrec->bytes_in_use)); + ntfs_log_verbose("Bytes allocated : %u\n", (unsigned int) + le32_to_cpu(ctx->mrec->bytes_allocated)); + + remains_size = le32_to_cpu(ctx->mrec->bytes_in_use); + remains_size -= (next_attr - (char *)ctx->mrec); + + ntfs_log_verbose("increase : %d\n", l); + ntfs_log_verbose("shift : %lld\n", + (long long)remains_size); + if (le32_to_cpu(ctx->mrec->bytes_in_use) + l > + le32_to_cpu(ctx->mrec->bytes_allocated)) { +#ifndef BAN_NEW_TEXT + ntfs_log_verbose("Queuing expansion for later processing\n"); +#endif + must_delay = 1; + replace_later(resize,rl,head_rl); + } else { + memmove(next_attr + l, next_attr, remains_size); + ctx->mrec->bytes_in_use = cpu_to_le32(l + + le32_to_cpu(ctx->mrec->bytes_in_use)); + a->length = cpu_to_le32(le32_to_cpu(a->length) + l); + } + } + + if (!must_delay) { + mp = ntfs_calloc(mp_size); + if (!mp) + perr_exit("ntfsc_calloc couldn't get memory"); + + if (ntfs_mapping_pairs_build(vol, (u8*)mp, mp_size, rl, 0, NULL)) + perr_exit("ntfs_mapping_pairs_build"); + + memmove((u8*)a + le16_to_cpu(a->mapping_pairs_offset), mp, mp_size); + + free(mp); + } + return (must_delay); +} + +static void set_bitmap_range(struct bitmap *bm, s64 pos, s64 length, u8 bit) +{ + while (length--) + ntfs_bit_set(bm->bm, pos++, bit); +} + +static void set_bitmap_clusters(struct bitmap *bm, runlist *rl, u8 bit) +{ + for (; rl->length; rl++) + set_bitmap_range(bm, rl->lcn, rl->length, bit); +} + +static void release_bitmap_clusters(struct bitmap *bm, runlist *rl) +{ + max_free_cluster_range = 0; + set_bitmap_clusters(bm, rl, 0); +} + +static void set_max_free_zone(s64 length, s64 end, runlist_element *rle) +{ + if (length > rle->length) { + rle->lcn = end - length; + rle->length = length; + } +} + +static int find_free_cluster(struct bitmap *bm, + runlist_element *rle, + s64 nr_vol_clusters, + int hint) +{ + /* FIXME: get rid of this 'static' variable */ + static s64 pos = 0; + s64 i, items = rle->length; + s64 free_zone = 0; + + if (pos >= nr_vol_clusters) + pos = 0; + if (!max_free_cluster_range) + max_free_cluster_range = nr_vol_clusters; + rle->lcn = rle->length = 0; + if (hint) + pos = nr_vol_clusters / 2; + i = pos; + + do { + if (!ntfs_bit_get(bm->bm, i)) { + if (++free_zone == items) { + set_max_free_zone(free_zone, i + 1, rle); + break; + } + } else { + set_max_free_zone(free_zone, i, rle); + free_zone = 0; + } + if (++i == nr_vol_clusters) { + set_max_free_zone(free_zone, i, rle); + i = free_zone = 0; + } + if (rle->length == max_free_cluster_range) + break; + } while (i != pos); + + if (i) + set_max_free_zone(free_zone, i, rle); + + if (!rle->lcn) { + errno = ENOSPC; + return -1; + } + if (rle->length < items && rle->length < max_free_cluster_range) { + max_free_cluster_range = rle->length; + ntfs_log_verbose("Max free range: %7lld \n", + (long long)max_free_cluster_range); + } + pos = rle->lcn + items; + if (pos == nr_vol_clusters) + pos = 0; + + set_bitmap_range(bm, rle->lcn, rle->length, 1); + return 0; +} + +static runlist *alloc_cluster(struct bitmap *bm, + s64 items, + s64 nr_vol_clusters, + int hint) +{ + runlist_element rle; + runlist *rl = NULL; + int rl_size, runs = 0; + s64 vcn = 0; + + if (items <= 0) { + errno = EINVAL; + return NULL; + } + + while (items > 0) { + + if (runs) + hint = 0; + rle.length = items; + if (find_free_cluster(bm, &rle, nr_vol_clusters, hint) == -1) + return NULL; + + rl_size = (runs + 2) * sizeof(runlist_element); + if (!(rl = (runlist *)realloc(rl, rl_size))) + return NULL; + + rl_set(rl + runs, vcn, rle.lcn, rle.length); + + vcn += rle.length; + items -= rle.length; + runs++; + } + + rl_set(rl + runs, vcn, -1LL, 0LL); + + if (runs > 1) { + ntfs_log_verbose("Multi-run allocation: \n"); + dump_runlist(rl); + } + return rl; +} + +static int read_all(struct ntfs_device *dev, void *buf, int count) +{ + int i; + + while (count > 0) { + + i = count; + if (!NDevReadOnly(dev)) + i = dev->d_ops->read(dev, buf, count); + + if (i < 0) { + if (errno != EAGAIN && errno != EINTR) + return -1; + } else if (i > 0) { + count -= i; + buf = i + (char *)buf; + } else + err_exit("Unexpected end of file!\n"); + } + return 0; +} + +static int write_all(struct ntfs_device *dev, void *buf, int count) +{ + int i; + + while (count > 0) { + + i = count; + if (!NDevReadOnly(dev)) + i = dev->d_ops->write(dev, buf, count); + + if (i < 0) { + if (errno != EAGAIN && errno != EINTR) + return -1; + } else { + count -= i; + buf = i + (char *)buf; + } + } + return 0; +} + +/** + * write_mft_record + * + * Write an MFT Record back to the disk. If the read-only command line option + * was given, this function will do nothing. + */ +static int write_mft_record(ntfs_volume *v, const MFT_REF mref, MFT_RECORD *buf) +{ + if (ntfs_mft_record_write(v, mref, buf)) + perr_exit("ntfs_mft_record_write"); + +// if (v->dev->d_ops->sync(v->dev) == -1) +// perr_exit("Failed to sync device"); + + return 0; +} + +static void lseek_to_cluster(ntfs_volume *vol, s64 lcn) +{ + off_t pos; + + pos = (off_t)(lcn * vol->cluster_size); + + if (vol->dev->d_ops->seek(vol->dev, pos, SEEK_SET) == (off_t)-1) + perr_exit("seek failed to position %lld", (long long)lcn); +} + +static void copy_clusters(ntfs_resize_t *resize, s64 dest, s64 src, s64 len) +{ + s64 i; + char *buff; + ntfs_volume *vol = resize->vol; + + buff = (char*)ntfs_malloc(vol->cluster_size); + if (!buff) + perr_exit("ntfs_malloc"); + + for (i = 0; i < len; i++) { + + lseek_to_cluster(vol, src + i); + + if (read_all(vol->dev, buff, vol->cluster_size) == -1) { + perr_printf("Failed to read from the disk"); + if (errno == EIO) + printf("%s", bad_sectors_warning_msg); + exit(1); + } + + lseek_to_cluster(vol, dest + i); + + if (write_all(vol->dev, buff, vol->cluster_size) == -1) { + perr_printf("Failed to write to the disk"); + if (errno == EIO) + printf("%s", bad_sectors_warning_msg); + exit(1); + } + + resize->relocations++; + progress_update(&resize->progress, resize->relocations); + } + free(buff); +} + +static void relocate_clusters(ntfs_resize_t *r, runlist *dest_rl, s64 src_lcn) +{ + /* collect_shrink_constraints() ensured $MFTMir DATA is one run */ + if (r->mref == FILE_MFTMirr && r->ctx->attr->type == AT_DATA) { + if (!r->mftmir_old) { + r->mftmir_rl.lcn = dest_rl->lcn; + r->mftmir_rl.length = dest_rl->length; + r->mftmir_old = src_lcn; + } else + err_exit("Multi-run $MFTMirr. Please report!\n"); + } + + for (; dest_rl->length; src_lcn += dest_rl->length, dest_rl++) + copy_clusters(r, dest_rl->lcn, src_lcn, dest_rl->length); +} + +static void rl_split_run(runlist **rl, int run, s64 pos) +{ + runlist *rl_new, *rle_new, *rle; + int items, new_size, size_head, size_tail; + s64 len_head, len_tail; + + items = rl_items(*rl); + new_size = (items + 1) * sizeof(runlist_element); + size_head = run * sizeof(runlist_element); + size_tail = (items - run - 1) * sizeof(runlist_element); + + rl_new = ntfs_malloc(new_size); + if (!rl_new) + perr_exit("ntfs_malloc"); + + rle_new = rl_new + run; + rle = *rl + run; + + memmove(rl_new, *rl, size_head); + memmove(rle_new + 2, rle + 1, size_tail); + + len_tail = rle->length - (pos - rle->lcn); + len_head = rle->length - len_tail; + + rl_set(rle_new, rle->vcn, rle->lcn, len_head); + rl_set(rle_new + 1, rle->vcn + len_head, rle->lcn + len_head, len_tail); + + ntfs_log_verbose("Splitting run at cluster %lld:\n", (long long)pos); + dump_run(rle); dump_run(rle_new); dump_run(rle_new + 1); + + free(*rl); + *rl = rl_new; +} + +static void rl_insert_at_run(runlist **rl, int run, runlist *ins) +{ + int items, ins_items; + int new_size, size_tail; + runlist *rle; + s64 vcn; + + items = rl_items(*rl); + ins_items = rl_items(ins) - 1; + new_size = ((items - 1) + ins_items) * sizeof(runlist_element); + size_tail = (items - run - 1) * sizeof(runlist_element); + + if (!(*rl = (runlist *)realloc(*rl, new_size))) + perr_exit("realloc"); + + rle = *rl + run; + + memmove(rle + ins_items, rle + 1, size_tail); + + for (vcn = rle->vcn; ins->length; rle++, vcn += ins->length, ins++) { + rl_set(rle, vcn, ins->lcn, ins->length); +// dump_run(rle); + } + + return; + + /* FIXME: fast path if ins_items = 1 */ +// (*rl + run)->lcn = ins->lcn; +} + +static void relocate_run(ntfs_resize_t *resize, runlist **rl, int run) +{ + s64 lcn, lcn_length; + s64 new_vol_size; /* (last LCN on the volume) + 1 */ + runlist *relocate_rl; /* relocate runlist to relocate_rl */ + int hint; + + lcn = (*rl + run)->lcn; + lcn_length = (*rl + run)->length; + new_vol_size = resize->new_volume_size; + + if (lcn + lcn_length <= new_vol_size) + return; + + if (lcn < new_vol_size) { + rl_split_run(rl, run, new_vol_size); + return; + } + + hint = (resize->mref == FILE_MFTMirr) ? 1 : 0; + if ((resize->mref == FILE_MFT) + && (resize->ctx->attr->type == AT_DATA) + && !run + && resize->new_mft_start) { + relocate_rl = resize->new_mft_start; + } else + if (!(relocate_rl = alloc_cluster(&resize->lcn_bitmap, + lcn_length, new_vol_size, hint))) + perr_exit("Cluster allocation failed for %llu:%lld", + (unsigned long long)resize->mref, + (long long)lcn_length); + + /* FIXME: check $MFTMirr DATA isn't multi-run (or support it) */ + ntfs_log_verbose("Relocate record %7llu:0x%x:%08lld:0x%08llx:0x%08llx " + "--> 0x%08llx\n", (unsigned long long)resize->mref, + (unsigned int)le32_to_cpu(resize->ctx->attr->type), + (long long)lcn_length, + (unsigned long long)(*rl + run)->vcn, + (unsigned long long)lcn, + (unsigned long long)relocate_rl->lcn); + + relocate_clusters(resize, relocate_rl, lcn); + rl_insert_at_run(rl, run, relocate_rl); + + /* We don't release old clusters in the bitmap, that area isn't + used by the allocator and will be truncated later on */ + + /* Do not free the relocated MFT start */ + if ((resize->mref != FILE_MFT) + || (resize->ctx->attr->type != AT_DATA) + || run + || !resize->new_mft_start) + free(relocate_rl); + + resize->dirty_inode = DIRTY_ATTRIB; +} + +static void relocate_attribute(ntfs_resize_t *resize) +{ + ATTR_RECORD *a; + runlist *rl; + int i; + + a = resize->ctx->attr; + + if (!a->non_resident) + return; + + if (!(rl = ntfs_mapping_pairs_decompress(resize->vol, a, NULL))) + perr_exit("ntfs_decompress_mapping_pairs"); + + for (i = 0; rl[i].length; i++) { + s64 lcn = rl[i].lcn; + s64 lcn_length = rl[i].length; + + if (lcn == LCN_HOLE || lcn == LCN_RL_NOT_MAPPED) + continue; + + /* FIXME: ntfs_mapping_pairs_decompress should return error */ + if (lcn < 0 || lcn_length <= 0) + err_exit("Corrupt runlist in MTF %llu attr %x LCN " + "%llx length %llx\n", + (unsigned long long)resize->mref, + (unsigned int)le32_to_cpu(a->type), + (long long)lcn, (long long)lcn_length); + + relocate_run(resize, &rl, i); + } + + if (resize->dirty_inode == DIRTY_ATTRIB) { + if (!replace_attribute_runlist(resize, rl)) + free(rl); + resize->dirty_inode = DIRTY_INODE; + } else + free(rl); +} + +static int is_mftdata(ntfs_resize_t *resize) +{ + /* + * We must update the MFT own DATA record at the end of the second + * step, because the old MFT must be kept available for processing + * the other files. + */ + + if (resize->ctx->attr->type != AT_DATA) + return 0; + + if (resize->mref == 0) + return 1; + + if (MREF_LE(resize->mrec->base_mft_record) == 0 && + MSEQNO_LE(resize->mrec->base_mft_record) != 0) + return 1; + + return 0; +} + +static int handle_mftdata(ntfs_resize_t *resize, int do_mftdata) +{ + ATTR_RECORD *attr = resize->ctx->attr; + VCN highest_vcn, lowest_vcn; + + if (do_mftdata) { + + if (!is_mftdata(resize)) + return 0; + + highest_vcn = sle64_to_cpu(attr->highest_vcn); + lowest_vcn = sle64_to_cpu(attr->lowest_vcn); + + if (resize->mft_highest_vcn != highest_vcn) + return 0; + + if (lowest_vcn == 0) + resize->mft_highest_vcn = lowest_vcn; + else + resize->mft_highest_vcn = lowest_vcn - 1; + + } else if (is_mftdata(resize)) { + + highest_vcn = sle64_to_cpu(attr->highest_vcn); + + if (resize->mft_highest_vcn < highest_vcn) + resize->mft_highest_vcn = highest_vcn; + + return 0; + } + + return 1; +} + +static void relocate_attributes(ntfs_resize_t *resize, int do_mftdata) +{ + int ret; + leMFT_REF lemref; + MFT_REF base_mref; + + if (!(resize->ctx = attr_get_search_ctx(NULL, resize->mrec))) + exit(1); + + lemref = resize->mrec->base_mft_record; + if (lemref) + base_mref = MREF(le64_to_cpu(lemref)); + else + base_mref = resize->mref; + while (!ntfs_attrs_walk(resize->ctx)) { + if (resize->ctx->attr->type == AT_END) + break; + + if (handle_mftdata(resize, do_mftdata) == 0) + continue; + + ret = ntfs_inode_badclus_bad(resize->mref, resize->ctx->attr); + if (ret == -1) + perr_exit("Bad sector list check failed"); + else if (ret == 1) + continue; + + if (resize->mref == FILE_Bitmap && + resize->ctx->attr->type == AT_DATA) + continue; + + /* Do not relocate bad clusters */ + if ((base_mref == FILE_BadClus) + && (resize->ctx->attr->type == AT_DATA)) + continue; + + relocate_attribute(resize); + } + + ntfs_attr_put_search_ctx(resize->ctx); +} + +static void relocate_inode(ntfs_resize_t *resize, MFT_REF mref, int do_mftdata) +{ + ntfs_volume *vol = resize->vol; + + if (ntfs_file_record_read(vol, mref, &resize->mrec, NULL)) { + /* FIXME: continue only if it make sense, e.g. + MFT record not in use based on $MFT bitmap */ + if (errno == EIO || errno == ENOENT) + return; + perr_exit("ntfs_file_record_record"); + } + + if (!(resize->mrec->flags & MFT_RECORD_IN_USE)) + return; + + resize->mref = mref; + resize->dirty_inode = DIRTY_NONE; + + relocate_attributes(resize, do_mftdata); + +// if (vol->dev->d_ops->sync(vol->dev) == -1) +// perr_exit("Failed to sync device"); + /* relocate MFT during second step, even if not dirty */ + if ((mref == FILE_MFT) && do_mftdata && resize->new_mft_start) { + s64 pos; + + /* write the MFT own record at its new location */ + pos = (resize->new_mft_start->lcn + << vol->cluster_size_bits) + + (FILE_MFT << vol->mft_record_size_bits); + if (!opt.ro_flag + && (ntfs_mst_pwrite(vol->dev, pos, 1, + vol->mft_record_size, resize->mrec) != 1)) + perr_exit("Couldn't update MFT own record"); + } else { + if ((resize->dirty_inode == DIRTY_INODE) + && write_mft_record(vol, mref, resize->mrec)) { + perr_exit("Couldn't update record %llu", + (unsigned long long)mref); + } + } +} + +static void relocate_inodes(ntfs_resize_t *resize) +{ + s64 nr_mft_records; + MFT_REF mref; + VCN highest_vcn; + s64 length; + + printf("Relocating needed data ...\n"); + + progress_init(&resize->progress, 0, resize->relocations, resize->progress.flags); + resize->relocations = 0; + + resize->mrec = ntfs_malloc(resize->vol->mft_record_size); + if (!resize->mrec) + perr_exit("ntfs_malloc failed"); + + nr_mft_records = resize->vol->mft_na->initialized_size >> + resize->vol->mft_record_size_bits; + + /* + * If we need to relocate the first run of the MFT DATA, + * do it now, to have a better chance of getting at least + * 16 records in the first chunk. This is mandatory to be + * later able to read an MFT extent in record 15. + * Should this fail, we can stop with no damage, the volume + * is still in its initial state. + */ + if (!resize->vol->mft_na->rl) + err_exit("Internal error : no runlist for $MFT\n"); + + if ((resize->vol->mft_na->rl->lcn + resize->vol->mft_na->rl->length) + >= resize->new_volume_size) { + /* + * The length of the first run is normally found in + * mft_na. However in some rare circumstance, this is + * merged with the first run of an extent of MFT, + * which implies there is a single run in the base record. + * So we have to make sure not to overflow from the + * runs present in the base extent. + */ + length = resize->vol->mft_na->rl->length; + if (ntfs_file_record_read(resize->vol, FILE_MFT, + &resize->mrec, NULL) + || !(resize->ctx = attr_get_search_ctx(NULL, + resize->mrec))) { + err_exit("Could not read the base record of MFT\n"); + } + while (!ntfs_attrs_walk(resize->ctx) + && (resize->ctx->attr->type != AT_DATA)) { } + if (resize->ctx->attr->type == AT_DATA) { + sle64 high_le; + + high_le = resize->ctx->attr->highest_vcn; + if (sle64_to_cpu(high_le) < length) + length = sle64_to_cpu(high_le) + 1; + } else { + err_exit("Could not find the DATA of MFT\n"); + } + ntfs_attr_put_search_ctx(resize->ctx); + resize->new_mft_start = alloc_cluster(&resize->lcn_bitmap, + length, resize->new_volume_size, 0); + if (!resize->new_mft_start + || (((resize->new_mft_start->length + << resize->vol->cluster_size_bits) + >> resize->vol->mft_record_size_bits) < 16)) { + err_exit("Could not allocate 16 records in" + " the first MFT chunk\n"); + } + resize->mirr_from = MIRR_NEWMFT; + } + + for (mref = 0; mref < (MFT_REF)nr_mft_records; mref++) + relocate_inode(resize, mref, 0); + + while (1) { + highest_vcn = resize->mft_highest_vcn; + mref = nr_mft_records; + do { + relocate_inode(resize, --mref, 1); + if (resize->mft_highest_vcn == 0) + goto done; + } while (mref); + + if (highest_vcn == resize->mft_highest_vcn) + err_exit("Sanity check failed! Highest_vcn = %lld. " + "Please report!\n", (long long)highest_vcn); + } +done: + free(resize->mrec); +} + +static void print_hint(ntfs_volume *vol, const char *s, struct llcn_t llcn) +{ + s64 runs_b, runs_mb; + + if (llcn.lcn == 0) + return; + + runs_b = llcn.lcn * vol->cluster_size; + runs_mb = rounded_up_division(runs_b, NTFS_MBYTE); + printf("%-19s: %9lld MB %8lld\n", s, (long long)runs_mb, + (long long)llcn.inode); +} + +/** + * advise_on_resize + * + * The metadata file $Bitmap has one bit for each cluster on disk. This has + * already been read into lcn_bitmap. By looking for the last used cluster on + * the disk, we can work out by how much we can shrink the volume. + */ +static void advise_on_resize(ntfs_resize_t *resize) +{ + ntfs_volume *vol = resize->vol; + + if (opt.verbose) { + printf("Estimating smallest shrunken size supported ...\n"); + printf("File feature Last used at By inode\n"); + print_hint(vol, "$MFT", resize->last_mft); + print_hint(vol, "Multi-Record", resize->last_multi_mft); + print_hint(vol, "$MFTMirr", resize->last_mftmir); + print_hint(vol, "Compressed", resize->last_compressed); + print_hint(vol, "Sparse", resize->last_sparse); + print_hint(vol, "Ordinary", resize->last_lcn); + } + + print_advise(vol, resize->last_unsupp); +} + +/** + * bitmap_file_data_fixup + * + * $Bitmap can overlap the end of the volume. Any bits in this region + * must be set. This region also encompasses the backup boot sector. + */ +static void bitmap_file_data_fixup(s64 cluster, struct bitmap *bm) +{ + for (; cluster < bm->size << 3; cluster++) + ntfs_bit_set(bm->bm, (u64)cluster, 1); +} + +/* + * Open the attribute $BadClust:$Bad and get its runlist + */ + +static ntfs_attr *open_badclust_bad_attr(ntfs_attr_search_ctx *ctx) +{ + ntfs_inode *base_ni; + ntfs_attr *na; + static ntfschar Bad[4] = { + const_cpu_to_le16('$'), const_cpu_to_le16('B'), + const_cpu_to_le16('a'), const_cpu_to_le16('d') + } ; + + base_ni = ctx->base_ntfs_ino; + if (!base_ni) + base_ni = ctx->ntfs_ino; + + na = ntfs_attr_open(base_ni, AT_DATA, Bad, 4); + if (!na) { + err_printf("Could not access the bad sector list\n"); + } else { + if (ntfs_attr_map_whole_runlist(na) || !na->rl) { + err_printf("Could not decode the bad sector list\n"); + ntfs_attr_close(na); + ntfs_inode_close(base_ni); + na = (ntfs_attr*)NULL; + } + } + return (na); +} + +/** + * truncate_badclust_bad_attr + * + * The metadata file $BadClus needs to be shrunk. + * + * FIXME: this function should go away and instead using a generalized + * "truncate_bitmap_data_attr()" + */ +static void truncate_badclust_bad_attr(ntfs_resize_t *resize) +{ + ntfs_inode *base_ni; + ntfs_attr *na; + ntfs_attr_search_ctx *ctx; + s64 nr_clusters = resize->new_volume_size; + ntfs_volume *vol = resize->vol; + + na = open_badclust_bad_attr(resize->ctx); + if (!na) { + err_printf("Could not access the bad sector list\n"); + exit(1); + } + base_ni = na->ni; + if (ntfs_attr_truncate(na,nr_clusters << vol->cluster_size_bits)) { + err_printf("Could not adjust the bad sector list\n"); + exit(1); + } + /* Clear the sparse flags, even if there are bad clusters */ + na->ni->flags &= ~FILE_ATTR_SPARSE_FILE; + na->data_flags &= ~ATTR_IS_SPARSE; + ctx = resize->ctx; + ctx->attr->data_size = cpu_to_sle64(na->data_size); + ctx->attr->initialized_size = cpu_to_sle64(na->initialized_size); + ctx->attr->flags = na->data_flags; + ctx->attr->compression_unit = 0; + ntfs_inode_mark_dirty(ctx->ntfs_ino); + NInoFileNameSetDirty(na->ni); + NInoFileNameSetDirty(na->ni); + + ntfs_attr_close(na); + ntfs_inode_mark_dirty(base_ni); +} + +/** + * realloc_bitmap_data_attr + * + * Reallocate the metadata file $Bitmap. It must be large enough for one bit + * per cluster of the shrunken volume. Also it must be a of 8 bytes in size. + */ +static void realloc_bitmap_data_attr(ntfs_resize_t *resize, + runlist **rl, + s64 nr_bm_clusters) +{ + s64 i; + ntfs_volume *vol = resize->vol; + ATTR_RECORD *a = resize->ctx->attr; + s64 new_size = resize->new_volume_size; + struct bitmap *bm = &resize->lcn_bitmap; + + if (!(*rl = ntfs_mapping_pairs_decompress(vol, a, NULL))) + perr_exit("ntfs_mapping_pairs_decompress"); + + release_bitmap_clusters(bm, *rl); + free(*rl); + + for (i = vol->nr_clusters; i < new_size; i++) + ntfs_bit_set(bm->bm, i, 0); + + if (!(*rl = alloc_cluster(bm, nr_bm_clusters, new_size, 0))) + perr_exit("Couldn't allocate $Bitmap clusters"); +} + +static void realloc_lcn_bitmap(ntfs_resize_t *resize, s64 bm_bsize) +{ + u8 *tmp; + + if (!(tmp = realloc(resize->lcn_bitmap.bm, bm_bsize))) + perr_exit("realloc"); + + resize->lcn_bitmap.bm = tmp; + resize->lcn_bitmap.size = bm_bsize; + bitmap_file_data_fixup(resize->new_volume_size, &resize->lcn_bitmap); +} + +/** + * truncate_bitmap_data_attr + */ +static void truncate_bitmap_data_attr(ntfs_resize_t *resize) +{ + ATTR_RECORD *a; + runlist *rl; + ntfs_attr *lcnbmp_na; + s64 bm_bsize, size; + s64 nr_bm_clusters; + int truncated; + ntfs_volume *vol = resize->vol; + + a = resize->ctx->attr; + if (!a->non_resident) + /* FIXME: handle resident attribute value */ + err_exit("Resident attribute in $Bitmap isn't supported!\n"); + + bm_bsize = nr_clusters_to_bitmap_byte_size(resize->new_volume_size); + nr_bm_clusters = rounded_up_division(bm_bsize, vol->cluster_size); + + if (resize->shrink) { + realloc_bitmap_data_attr(resize, &rl, nr_bm_clusters); + realloc_lcn_bitmap(resize, bm_bsize); + } else { + realloc_lcn_bitmap(resize, bm_bsize); + realloc_bitmap_data_attr(resize, &rl, nr_bm_clusters); + } + /* + * Delayed relocations may require cluster allocations + * through the library, to hold added attribute lists, + * be sure they will be within the new limits. + */ + lcnbmp_na = resize->vol->lcnbmp_na; + lcnbmp_na->data_size = bm_bsize; + lcnbmp_na->initialized_size = bm_bsize; + lcnbmp_na->allocated_size = nr_bm_clusters << vol->cluster_size_bits; + vol->lcnbmp_ni->data_size = bm_bsize; + vol->lcnbmp_ni->allocated_size = lcnbmp_na->allocated_size; + a->highest_vcn = cpu_to_sle64(nr_bm_clusters - 1LL); + a->allocated_size = cpu_to_sle64(nr_bm_clusters * vol->cluster_size); + a->data_size = cpu_to_sle64(bm_bsize); + a->initialized_size = cpu_to_sle64(bm_bsize); + + truncated = !replace_attribute_runlist(resize, rl); + + /* + * FIXME: update allocated/data sizes and timestamps in $FILE_NAME + * attribute too, for now chkdsk will do this for us. + */ + + size = ntfs_rl_pwrite(vol, rl, 0, 0, bm_bsize, resize->lcn_bitmap.bm); + if (bm_bsize != size) { + if (size == -1) + perr_exit("Couldn't write $Bitmap"); + err_exit("Couldn't write full $Bitmap file (%lld from %lld)\n", + (long long)size, (long long)bm_bsize); + } + + if (truncated) { + /* switch to the new bitmap runlist */ + free(lcnbmp_na->rl); + lcnbmp_na->rl = rl; + } +} + +/** + * lookup_data_attr + * + * Find the $DATA attribute (with or without a name) for the given MFT reference + * (inode number). + */ +static void lookup_data_attr(ntfs_volume *vol, + MFT_REF mref, + const char *aname, + ntfs_attr_search_ctx **ctx) +{ + ntfs_inode *ni; + ntfschar *ustr; + int len = 0; + + if (!(ni = ntfs_inode_open(vol, mref))) + perr_exit("ntfs_open_inode"); + + if (!(*ctx = attr_get_search_ctx(ni, NULL))) + exit(1); + + if ((ustr = ntfs_str2ucs(aname, &len)) == NULL) { + perr_printf("Couldn't convert '%s' to Unicode", aname); + exit(1); + } + + if (ntfs_attr_lookup(AT_DATA, ustr, len, CASE_SENSITIVE, + 0, NULL, 0, *ctx)) + perr_exit("ntfs_lookup_attr"); + + ntfs_ucsfree(ustr); +} + +static void close_inode_and_context(ntfs_attr_search_ctx *ctx) +{ + ntfs_inode *ni; + + ni = ctx->base_ntfs_ino; + if (!ni) + ni = ctx->ntfs_ino; + ntfs_attr_put_search_ctx(ctx); + if (ni) + ntfs_inode_close(ni); +} + +static int check_bad_sectors(ntfs_volume *vol) +{ + ntfs_attr_search_ctx *ctx; + ntfs_attr *na; + runlist *rl; + s64 i, badclusters = 0; + + ntfs_log_verbose("Checking for bad sectors ...\n"); + + lookup_data_attr(vol, FILE_BadClus, "$Bad", &ctx); + + na = open_badclust_bad_attr(ctx); + if (!na) { + err_printf("Could not access the bad sector list\n"); + exit(1); + } + rl = na->rl; + for (i = 0; rl[i].length; i++) { + /* CHECKME: LCN_RL_NOT_MAPPED check isn't needed */ + if (rl[i].lcn == LCN_HOLE || rl[i].lcn == LCN_RL_NOT_MAPPED) + continue; + + badclusters += rl[i].length; + ntfs_log_verbose("Bad cluster: %#8llx - %#llx (%lld)\n", + (long long)rl[i].lcn, + (long long)rl[i].lcn + rl[i].length - 1, + (long long)rl[i].length); + } + + if (badclusters) { + printf("%sThis software has detected that the disk has at least" + " %lld bad sector%s.\n", + !opt.badsectors ? NERR_PREFIX : "WARNING: ", + (long long)badclusters, badclusters - 1 ? "s" : ""); + if (!opt.badsectors) { + printf("%s", bad_sectors_warning_msg); + exit(1); + } else + printf("WARNING: Bad sectors can cause reliability " + "problems and massive data loss!!!\n"); + } + + ntfs_attr_close(na); +#if CLEAN_EXIT + close_inode_and_context(ctx); +#else + ntfs_attr_put_search_ctx(ctx); +#endif + + return badclusters; +} + +/** + * truncate_badclust_file + * + * Shrink the $BadClus file to match the new volume size. + */ +static void truncate_badclust_file(ntfs_resize_t *resize) +{ + printf("Updating $BadClust file ...\n"); + + lookup_data_attr(resize->vol, FILE_BadClus, "$Bad", &resize->ctx); + /* FIXME: sanity_check_attr(ctx->attr); */ + resize->mref = FILE_BadClus; + truncate_badclust_bad_attr(resize); + + close_inode_and_context(resize->ctx); +} + +/** + * truncate_bitmap_file + * + * Shrink the $Bitmap file to match the new volume size. + */ +static void truncate_bitmap_file(ntfs_resize_t *resize) +{ + ntfs_volume *vol = resize->vol; + + printf("Updating $Bitmap file ...\n"); + + lookup_data_attr(resize->vol, FILE_Bitmap, NULL, &resize->ctx); + resize->mref = FILE_Bitmap; + truncate_bitmap_data_attr(resize); + + if (resize->new_mft_start) { + s64 pos; + + /* write the MFT record at its new location */ + pos = (resize->new_mft_start->lcn << vol->cluster_size_bits) + + (FILE_Bitmap << vol->mft_record_size_bits); + if (!opt.ro_flag + && (ntfs_mst_pwrite(vol->dev, pos, 1, + vol->mft_record_size, resize->ctx->mrec) != 1)) + perr_exit("Couldn't update $Bitmap at new location"); + } else { + if (write_mft_record(vol, resize->ctx->ntfs_ino->mft_no, + resize->ctx->mrec)) + perr_exit("Couldn't update $Bitmap"); + } + + /* If successful, update cache and sync $Bitmap */ + memcpy(vol->lcnbmp_ni->mrec,resize->ctx->mrec,vol->mft_record_size); + ntfs_inode_mark_dirty(vol->lcnbmp_ni); + NInoFileNameSetDirty(vol->lcnbmp_ni); + ntfs_inode_sync(vol->lcnbmp_ni); + +#if CLEAN_EXIT + close_inode_and_context(resize->ctx); +#else + ntfs_attr_put_search_ctx(resize->ctx); +#endif +} + +/** + * setup_lcn_bitmap + * + * Allocate a block of memory with one bit for each cluster of the disk. + * All the bits are set to 0, except those representing the region beyond the + * end of the disk. + */ +static int setup_lcn_bitmap(struct bitmap *bm, s64 nr_clusters) +{ + /* Determine lcn bitmap byte size and allocate it. */ + bm->size = rounded_up_division(nr_clusters, 8); + + bm->bm = ntfs_calloc(bm->size); + if (!bm->bm) + return -1; + + bitmap_file_data_fixup(nr_clusters, bm); + return 0; +} + +/** + * update_bootsector + * + * FIXME: should be done using ntfs_* functions + */ +static void update_bootsector(ntfs_resize_t *r) +{ + NTFS_BOOT_SECTOR *bs; + ntfs_volume *vol = r->vol; + s64 bs_size = vol->sector_size; + + printf("Updating Boot record ...\n"); + + bs = (NTFS_BOOT_SECTOR*)ntfs_malloc(vol->sector_size); + if (!bs) + perr_exit("ntfs_malloc"); + + if (vol->dev->d_ops->seek(vol->dev, 0, SEEK_SET) == (off_t)-1) + perr_exit("lseek"); + + if (vol->dev->d_ops->read(vol->dev, bs, bs_size) == -1) + perr_exit("read() error"); + + if (bs->bpb.sectors_per_cluster > 128) + bs->number_of_sectors = cpu_to_sle64(r->new_volume_size + << (256 - bs->bpb.sectors_per_cluster)); + else + bs->number_of_sectors = cpu_to_sle64(r->new_volume_size * + bs->bpb.sectors_per_cluster); + + if (r->mftmir_old || (r->mirr_from == MIRR_MFT)) { + r->progress.flags |= NTFS_PROGBAR_SUPPRESS; + /* Be sure the MFTMirr holds the updated MFT runlist */ + switch (r->mirr_from) { + case MIRR_MFT : + /* The late updates of MFT have not been synced */ + ntfs_inode_sync(vol->mft_ni); + copy_clusters(r, r->mftmir_rl.lcn, + vol->mft_na->rl->lcn, r->mftmir_rl.length); + break; + case MIRR_NEWMFT : + copy_clusters(r, r->mftmir_rl.lcn, + r->new_mft_start->lcn, r->mftmir_rl.length); + break; + default : + copy_clusters(r, r->mftmir_rl.lcn, r->mftmir_old, + r->mftmir_rl.length); + break; + } + if (r->mftmir_old) + bs->mftmirr_lcn = cpu_to_sle64(r->mftmir_rl.lcn); + r->progress.flags &= ~NTFS_PROGBAR_SUPPRESS; + } + /* Set the start of the relocated MFT */ + if (r->new_mft_start) { + bs->mft_lcn = cpu_to_sle64(r->new_mft_start->lcn); + /* no more need for the new MFT start */ + free(r->new_mft_start); + r->new_mft_start = (runlist_element*)NULL; + } + + if (vol->dev->d_ops->seek(vol->dev, 0, SEEK_SET) == (off_t)-1) + perr_exit("lseek"); + + if (!opt.ro_flag) + if (vol->dev->d_ops->write(vol->dev, bs, bs_size) == -1) + perr_exit("write() error"); + /* + * Set the backup boot sector, if the target size is + * either not defined or is defined with no multiplier + * suffix and is a multiple of the sector size. + * With these conditions we can be confident enough that + * the partition size is already defined or it will be + * later defined with the same exact value. + */ + if (!opt.ro_flag && opt.reliable_size + && !(opt.bytes % vol->sector_size)) { + if (vol->dev->d_ops->seek(vol->dev, opt.bytes + - vol->sector_size, SEEK_SET) == (off_t)-1) + perr_exit("lseek"); + if (vol->dev->d_ops->write(vol->dev, bs, bs_size) == -1) + perr_exit("write() error"); + } + free(bs); +} + +/** + * vol_size + */ +static s64 vol_size(ntfs_volume *v, s64 nr_clusters) +{ + /* add one sector_size for the backup boot sector */ + return nr_clusters * v->cluster_size + v->sector_size; +} + +/** + * print_vol_size + * + * Print the volume size in bytes and decimal megabytes. + */ +static void print_vol_size(const char *str, s64 bytes) +{ + printf("%s: %lld bytes (%lld MB)\n", str, (long long)bytes, + (long long)rounded_up_division(bytes, NTFS_MBYTE)); +} + +/** + * print_disk_usage + * + * Display the amount of disk space in use. + */ +static void print_disk_usage(ntfs_volume *vol, s64 nr_used_clusters) +{ + s64 total, used; + + total = vol->nr_clusters * vol->cluster_size; + used = nr_used_clusters * vol->cluster_size; + + /* WARNING: don't modify the text, external tools grep for it */ + if (!opt.infombonly) { + printf("Space in use : %lld MB (%.1f%%)\n", + (long long)rounded_up_division(used, NTFS_MBYTE), + 100.0 * ((float)used / total)); + } +} + +static void print_num_of_relocations(ntfs_resize_t *resize) +{ + s64 relocations = resize->relocations * resize->vol->cluster_size; + + printf("Needed relocations : %lld (%lld MB)\n", + (long long)resize->relocations, (long long) + rounded_up_division(relocations, NTFS_MBYTE)); +} + +static ntfs_volume *check_volume(void) +{ + ntfs_volume *myvol = NULL; + + /* + * Pass NTFS_MNT_FORENSIC so that the mount process does not modify the + * volume at all. We will do the logfile emptying and dirty setting + * later if needed. + */ + if (!(myvol = ntfs_mount(opt.volume, opt.ro_flag | NTFS_MNT_FORENSIC))) + { + int err = errno; + + perr_printf("Opening '%s' as NTFS failed", opt.volume); + switch (err) { + case EINVAL : + printf(invalid_ntfs_msg, opt.volume); + break; + case EIO : + printf("%s", corrupt_volume_msg); + break; + case EPERM : + printf("%s", hibernated_volume_msg); + break; + case EOPNOTSUPP : + printf("%s", unclean_journal_msg); + break; + case EBUSY : + printf("%s", opened_volume_msg); + break; + default : + break; + } + exit(1); + } + return myvol; +} + + +/** + * mount_volume + * + * First perform some checks to determine if the volume is already mounted, or + * is dirty (Windows wasn't shutdown properly). If everything is OK, then mount + * the volume (load the metadata into memory). + */ +static ntfs_volume *mount_volume(void) +{ + unsigned long mntflag; + ntfs_volume *vol = NULL; + + if (ntfs_check_if_mounted(opt.volume, &mntflag)) { + perr_printf("Failed to check '%s' mount state", opt.volume); + printf("Probably /etc/mtab is missing. It's too risky to " + "continue. You might try\nan another Linux distro.\n"); + exit(1); + } + if (mntflag & NTFS_MF_MOUNTED) { + if (!(mntflag & NTFS_MF_READONLY)) + err_exit("Device '%s' is mounted read-write. " + "You must 'umount' it first.\n", opt.volume); + if (!opt.ro_flag) + err_exit("Device '%s' is mounted. " + "You must 'umount' it first.\n", opt.volume); + } + vol = check_volume(); + + if (vol->flags & VOLUME_IS_DIRTY) + if (opt.force-- <= 0) + err_exit("Volume is scheduled for check.\nRun chkdsk /f" + " and please try again, or see option -f.\n"); + + if (NTFS_MAX_CLUSTER_SIZE < vol->cluster_size) + err_exit("Cluster size %u is too large!\n", + (unsigned int)vol->cluster_size); + + if (ntfs_volume_get_free_space(vol)) + err_exit("Failed to update the free space\n"); + + if (!opt.infombonly) { + printf("Device name : %s\n", opt.volume); + printf("NTFS volume version: %d.%d\n", + vol->major_ver, vol->minor_ver); + } + if (ntfs_version_is_supported(vol)) + perr_exit("Unknown NTFS version"); + + if (!opt.infombonly) { + printf("Cluster size : %u bytes\n", + (unsigned int)vol->cluster_size); + print_vol_size("Current volume size", + vol_size(vol, vol->nr_clusters)); + } + + return vol; +} + +/** + * prepare_volume_fixup + * + * Set the volume's dirty flag and wipe the filesystem journal. When Windows + * boots it will automatically run chkdsk to check for any problems. If the + * read-only command line option was given, this function will do nothing. + */ +static void prepare_volume_fixup(ntfs_volume *vol) +{ + printf("Schedule chkdsk for NTFS consistency check at Windows boot " + "time ...\n"); + vol->flags |= VOLUME_IS_DIRTY; + if (ntfs_volume_write_flags(vol, vol->flags)) + perr_exit("Failed to set the volume dirty"); + + /* Porting note: This flag does not exist in libntfs-3g. The dirty flag + * is never modified by libntfs-3g on unmount and we set it above. We + * can safely comment out this statement. */ + /* NVolSetWasDirty(vol); */ + + if (vol->dev->d_ops->sync(vol->dev) == -1) + perr_exit("Failed to sync device"); + printf("Resetting $LogFile ... (this might take a while)\n"); + if (ntfs_logfile_reset(vol)) + perr_exit("Failed to reset $LogFile"); + if (vol->dev->d_ops->sync(vol->dev) == -1) + perr_exit("Failed to sync device"); +} + +static void set_disk_usage_constraint(ntfs_resize_t *resize) +{ + /* last lcn for a filled up volume (no empty space) */ + s64 last = resize->inuse - 1; + + if (resize->last_unsupp < last) + resize->last_unsupp = last; +} + +static void check_resize_constraints(ntfs_resize_t *resize) +{ + s64 new_size = resize->new_volume_size; + + /* FIXME: resize.shrink true also if only -i is used */ + if (!resize->shrink) + return; + + if (resize->inuse == resize->vol->nr_clusters) + err_exit("Volume is full. To shrink it, " + "delete unused files.\n"); + + if (opt.info || opt.infombonly) + return; + + /* FIXME: reserve some extra space so Windows can boot ... */ + if (new_size < resize->inuse) + err_exit("New size can't be less than the space already" + " occupied by data.\nYou either need to delete unused" + " files or see the -i option.\n"); + + if (new_size <= resize->last_unsupp) + err_exit("The fragmentation type, you have, isn't " + "supported yet. Rerun ntfsresize\nwith " + "the -i option to estimate the smallest " + "shrunken volume size supported.\n"); + + print_num_of_relocations(resize); +} + +static void check_cluster_allocation(ntfs_volume *vol, ntfsck_t *fsck) +{ + memset(fsck, 0, sizeof(ntfsck_t)); + + if (opt.show_progress) + fsck->flags |= NTFSCK_PROGBAR; + + if (setup_lcn_bitmap(&fsck->lcn_bitmap, vol->nr_clusters) != 0) + perr_exit("Failed to setup allocation bitmap"); + if (build_allocation_bitmap(vol, fsck) != 0) + exit(1); + if (fsck->outsider || fsck->multi_ref) { + err_printf("Filesystem check failed!\n"); + if (fsck->outsider) + err_printf("%d clusters are referenced outside " + "of the volume.\n", fsck->outsider); + if (fsck->multi_ref) + err_printf("%d clusters are referenced multiple" + " times.\n", fsck->multi_ref); + printf("%s", corrupt_volume_msg); + exit(1); + } + + compare_bitmaps(vol, &fsck->lcn_bitmap); +} + +/* + * Following are functions to expand an NTFS file system + * to the beginning of a partition. The old metadata can be + * located according to the backup bootsector, provided it can + * still be found at the end of the partition. + * + * The data itself is kept in place, and this is only possible + * if the expanded size is a multiple of cluster size, and big + * enough to hold the new $Boot, $Bitmap and $MFT + * + * The volume cannot be mounted because the layout of data does + * not match the volume parameters. The alignments of MFT entries + * and index blocks may be different in the new volume and the old + * one. The "ntfs_volume" structure is only partially usable, + * "ntfs_inode" and "search_context" cannot be used until the + * metadata has been moved and the volume is opened. + * + * Currently, no part of this new code is called from old code, + * and the only change in old code is the processing of options. + * Deduplication of code should be done later when the code is + * proved safe. + * + */ + +typedef struct EXPAND { + ntfs_volume *vol; + u64 original_sectors; + u64 new_sectors; + u64 bitmap_allocated; + u64 bitmap_size; + u64 boot_size; + u64 mft_size; + LCN mft_lcn; + s64 byte_increment; + s64 sector_increment; + s64 cluster_increment; + u8 *bitmap; + u8 *mft_bitmap; + char *bootsector; + MFT_RECORD *mrec; + struct progress_bar *progress; + struct DELAYED *delayed_runlists; /* runlists to process later */ +} expand_t; + +/* + * Locate an attribute in an MFT record + * + * Returns NULL if not found (with no error message) + */ + +static ATTR_RECORD *find_attr(MFT_RECORD *mrec, ATTR_TYPES type, + ntfschar *name, int namelen) +{ + ATTR_RECORD *a; + u32 offset; + ntfschar *attrname; + + /* fetch the requested attribute */ + offset = le16_to_cpu(mrec->attrs_offset); + a = (ATTR_RECORD*)((char*)mrec + offset); + attrname = (ntfschar*)((char*)a + le16_to_cpu(a->name_offset)); + while ((a->type != AT_END) + && ((a->type != type) + || (a->name_length != namelen) + || (namelen && memcmp(attrname,name,2*namelen))) + && (offset < le32_to_cpu(mrec->bytes_in_use))) { + offset += le32_to_cpu(a->length); + a = (ATTR_RECORD*)((char*)mrec + offset); + if (namelen) + attrname = (ntfschar*)((char*)a + + le16_to_cpu(a->name_offset)); + } + if ((a->type != type) + || (a->name_length != namelen) + || (namelen && memcmp(attrname,name,2*namelen))) + a = (ATTR_RECORD*)NULL; + return (a); +} + +/* + * Read an MFT record and find an unnamed attribute + * + * Returns NULL if fails to read or attribute is not found + */ + +static ATTR_RECORD *get_unnamed_attr(expand_t *expand, ATTR_TYPES type, + s64 inum) +{ + ntfs_volume *vol; + ATTR_RECORD *a; + MFT_RECORD *mrec; + s64 pos; + BOOL found; + int got; + + found = FALSE; + a = (ATTR_RECORD*)NULL; + mrec = expand->mrec; + vol = expand->vol; + pos = (vol->mft_lcn << vol->cluster_size_bits) + + (inum << vol->mft_record_size_bits) + + expand->byte_increment; + got = ntfs_mst_pread(vol->dev, pos, 1, vol->mft_record_size, mrec); + if ((got == 1) && (mrec->flags & MFT_RECORD_IN_USE)) { + a = find_attr(expand->mrec, type, NULL, 0); + found = a && (a->type == type) && !a->name_length; + } + /* not finding the attribute list is not an error */ + if (!found && (type != AT_ATTRIBUTE_LIST)) { + err_printf("Could not find attribute 0x%lx in inode %lld\n", + (long)le32_to_cpu(type), (long long)inum); + a = (ATTR_RECORD*)NULL; + } + return (a); +} + +/* + * Read an MFT record and find an unnamed attribute + * + * Returns NULL if fails + */ + +static ATTR_RECORD *read_and_get_attr(expand_t *expand, ATTR_TYPES type, + s64 inum, ntfschar *name, int namelen) +{ + ntfs_volume *vol; + ATTR_RECORD *a; + MFT_RECORD *mrec; + s64 pos; + int got; + + a = (ATTR_RECORD*)NULL; + mrec = expand->mrec; + vol = expand->vol; + pos = (vol->mft_lcn << vol->cluster_size_bits) + + (inum << vol->mft_record_size_bits) + + expand->byte_increment; + got = ntfs_mst_pread(vol->dev, pos, 1, vol->mft_record_size, mrec); + if ((got == 1) && (mrec->flags & MFT_RECORD_IN_USE)) { + a = find_attr(expand->mrec, type, name, namelen); + } + if (!a) { + err_printf("Could not find attribute 0x%lx in inode %lld\n", + (long)le32_to_cpu(type), (long long)inum); + } + return (a); +} + +/* + * Get the size allocated to the unnamed data of some inode + * + * Returns zero if fails. + */ + +static s64 get_data_size(expand_t *expand, s64 inum) +{ + ATTR_RECORD *a; + s64 size; + + size = 0; + /* get the size of unnamed $DATA */ + a = get_unnamed_attr(expand, AT_DATA, inum); + if (a && a->non_resident) + size = sle64_to_cpu(a->allocated_size); + if (!size) { + err_printf("Bad record %lld, could not get its size\n", + (long long)inum); + } + return (size); +} + +/* + * Get the MFT bitmap + * + * Returns NULL if fails. + */ + +static u8 *get_mft_bitmap(expand_t *expand) +{ + ATTR_RECORD *a; + ntfs_volume *vol; + runlist_element *rl; + runlist_element *prl; + u32 bitmap_size; + BOOL ok; + + expand->mft_bitmap = (u8*)NULL; + vol = expand->vol; + /* get the runlist of unnamed bitmap */ + a = get_unnamed_attr(expand, AT_BITMAP, FILE_MFT); + ok = TRUE; + bitmap_size = sle64_to_cpu(a->allocated_size); + if (a + && a->non_resident + && ((bitmap_size << (vol->mft_record_size_bits + 3)) + >= expand->mft_size)) { +// rl in extent not implemented + rl = ntfs_mapping_pairs_decompress(expand->vol, a, + (runlist_element*)NULL); + expand->mft_bitmap = (u8*)ntfs_calloc(bitmap_size); + if (rl && expand->mft_bitmap) { + for (prl=rl; prl->length && ok; prl++) { + lseek_to_cluster(vol, + prl->lcn + expand->cluster_increment); + ok = !read_all(vol->dev, expand->mft_bitmap + + (prl->vcn << vol->cluster_size_bits), + prl->length << vol->cluster_size_bits); + } + if (!ok) { + err_printf("Could not read the MFT bitmap\n"); + free(expand->mft_bitmap); + expand->mft_bitmap = (u8*)NULL; + } + free(rl); + } else { + err_printf("Could not get the MFT bitmap\n"); + } + } else + err_printf("Invalid MFT bitmap\n"); + return (expand->mft_bitmap); +} + +/* + * Check for bad sectors + * + * Deduplication to be done when proved safe + */ + +static int check_expand_bad_sectors(expand_t *expand, ATTR_RECORD *a) +{ + runlist *rl; + int res; + s64 i, badclusters = 0; + + res = 0; + ntfs_log_verbose("Checking for bad sectors ...\n"); + + if (find_attr(expand->mrec, AT_ATTRIBUTE_LIST, NULL, 0)) { + err_printf("Hopelessly many bad sectors have been detected!\n"); + err_printf("%s", many_bad_sectors_msg); + res = -1; + } else { + + /* + * FIXME: The below would be partial for non-base records in the + * not yet supported multi-record case. Alternatively use audited + * ntfs_attr_truncate after an umount & mount. + */ + rl = ntfs_mapping_pairs_decompress(expand->vol, a, NULL); + if (!rl) { + perr_printf("Decompressing $BadClust:" + "$Bad mapping pairs failed"); + res = -1; + } else { + for (i = 0; rl[i].length; i++) { + /* CHECKME: LCN_RL_NOT_MAPPED check isn't needed */ + if (rl[i].lcn == LCN_HOLE + || rl[i].lcn == LCN_RL_NOT_MAPPED) + continue; + + badclusters += rl[i].length; + ntfs_log_verbose("Bad cluster: %#8llx - %#llx" + " (%lld)\n", + (long long)rl[i].lcn, + (long long)rl[i].lcn + + rl[i].length - 1, + (long long)rl[i].length); + } + + if (badclusters) { + err_printf("%sThis software has detected that" + " the disk has at least" + " %lld bad sector%s.\n", + !opt.badsectors ? NERR_PREFIX + : "WARNING: ", + (long long)badclusters, + badclusters - 1 ? "s" : ""); + if (!opt.badsectors) { + err_printf("%s", bad_sectors_warning_msg); + res = -1; + } else + err_printf("WARNING: Bad sectors can cause" + " reliability problems" + " and massive data loss!!!\n"); + } + free(rl); + } + } + return (res); +} + +/* + * Check miscellaneous expansion constraints + */ + +static int check_expand_constraints(expand_t *expand) +{ + static ntfschar bad[] = { + const_cpu_to_le16('$'), const_cpu_to_le16('B'), + const_cpu_to_le16('a'), const_cpu_to_le16('d') + } ; + ATTR_RECORD *a; + runlist_element *rl; + VOLUME_INFORMATION *volinfo; + VOLUME_FLAGS flags; + int res; + + if (opt.verbose) + ntfs_log_verbose("Checking for expansion constraints...\n"); + res = 0; + /* extents for $MFT are not supported */ + if (get_unnamed_attr(expand, AT_ATTRIBUTE_LIST, FILE_MFT)) { + err_printf("The $MFT is too much fragmented\n"); + res = -1; + } + /* fragmented $MFTMirr is not supported */ + a = get_unnamed_attr(expand, AT_DATA, FILE_MFTMirr); + if (a) { + rl = ntfs_mapping_pairs_decompress(expand->vol, a, NULL); + if (!rl || !rl[0].length || rl[1].length) { + err_printf("$MFTMirr is bad or fragmented\n"); + res = -1; + } + free(rl); + } + /* fragmented $Boot is not supported */ + a = get_unnamed_attr(expand, AT_DATA, FILE_Boot); + if (a) { + rl = ntfs_mapping_pairs_decompress(expand->vol, a, NULL); + if (!rl || !rl[0].length || rl[1].length) { + err_printf("$Boot is bad or fragmented\n"); + res = -1; + } + free(rl); + } + /* Volume should not be marked dirty */ + a = get_unnamed_attr(expand, AT_VOLUME_INFORMATION, FILE_Volume); + if (a) { + volinfo = (VOLUME_INFORMATION*) + (le16_to_cpu(a->value_offset) + (char*)a); + flags = volinfo->flags; + if ((flags & VOLUME_IS_DIRTY) && (opt.force-- <= 0)) { + err_printf("Volume is scheduled for check.\nRun chkdsk /f" + " and please try again, or see option -f.\n"); + res = -1; + } + } else { + err_printf("Could not get Volume flags\n"); + res = -1; + } + + /* There should not be too many bad clusters */ + a = read_and_get_attr(expand, AT_DATA, FILE_BadClus, bad, 4); + if (!a || !a->non_resident) { + err_printf("Resident attribute in $BadClust! Please report to " + "%s\n", NTFS_DEV_LIST); + res = -1; + } else + if (check_expand_bad_sectors(expand,a)) + res = -1; + return (res); +} + +/* + * Compute the new sizes and check whether the NTFS file + * system can be expanded + * + * The partition has to have been expanded, + * the extra space must be able to hold the $MFT, $Boot, and $Bitmap + * the extra space must be a multiple of cluster size + * + * Returns TRUE if the partition can be expanded, + * FALSE if it canno be expanded or option --info was set + */ + +static BOOL can_expand(expand_t *expand, ntfs_volume *vol) +{ + s64 old_sector_count; + s64 sectors_needed; + s64 clusters; + s64 minimum_size; + s64 got; + s64 advice; + s64 bitmap_bits; + BOOL ok; + + ok = TRUE; + old_sector_count = vol->nr_clusters + << (vol->cluster_size_bits - vol->sector_size_bits); + /* do not include the space lost near the end */ + expand->cluster_increment = (expand->new_sectors + >> (vol->cluster_size_bits - vol->sector_size_bits)) + - vol->nr_clusters; + expand->byte_increment = expand->cluster_increment + << vol->cluster_size_bits; + expand->sector_increment = expand->byte_increment + >> vol->sector_size_bits; + printf("Sectors allocated to volume : old %lld current %lld difference %lld\n", + (long long)old_sector_count, + (long long)(old_sector_count + expand->sector_increment), + (long long)expand->sector_increment); + printf("Clusters allocated to volume : old %lld current %lld difference %lld\n", + (long long)vol->nr_clusters, + (long long)(vol->nr_clusters + + expand->cluster_increment), + (long long)expand->cluster_increment); + /* the new size must be bigger */ + if ((expand->sector_increment < 0) + || (!expand->sector_increment && !opt.info)) { + err_printf("Cannot expand volume : the partition has not been expanded\n"); + ok = FALSE; + } + /* the old bootsector must match the backup */ + got = ntfs_pread(expand->vol->dev, expand->byte_increment, + vol->sector_size, expand->mrec); + if ((got != vol->sector_size) + || memcmp(expand->bootsector,expand->mrec,vol->sector_size)) { + err_printf("The backup bootsector does not match the old bootsector\n"); + ok = FALSE; + } + if (ok) { + /* read the first MFT record, to get the MFT size */ + expand->mft_size = get_data_size(expand, FILE_MFT); + /* read the 6th MFT record, to get the $Boot size */ + expand->boot_size = get_data_size(expand, FILE_Boot); + if (!expand->mft_size || !expand->boot_size) { + ok = FALSE; + } else { + /* + * The bitmap is one bit per full cluster, + * accounting for the backup bootsector. + * When evaluating the minimal size, the bitmap + * size must be adapted to the minimal size : + * bits = clusters + ceil(clusters/clustersize) + */ + if (opt.info) { + clusters = (((expand->original_sectors + 1) + << vol->sector_size_bits) + + expand->mft_size + + expand->boot_size) + >> vol->cluster_size_bits; + bitmap_bits = ((clusters + 1) + << vol->cluster_size_bits) + / (vol->cluster_size + 1); + } else { + bitmap_bits = (expand->new_sectors + 1) + >> (vol->cluster_size_bits + - vol->sector_size_bits); + } + /* byte size must be a multiple of 8 */ + expand->bitmap_size = ((bitmap_bits + 63) >> 3) & -8; + expand->bitmap_allocated = ((expand->bitmap_size - 1) + | (vol->cluster_size - 1)) + 1; + expand->mft_lcn = (expand->boot_size + + expand->bitmap_allocated) + >> vol->cluster_size_bits; + /* + * Check whether $Boot, $Bitmap and $MFT can fit + * into the expanded space. + */ + sectors_needed = (expand->boot_size + expand->mft_size + + expand->bitmap_allocated) + >> vol->sector_size_bits; + if (!opt.info + && (sectors_needed >= expand->sector_increment)) { + err_printf("The expanded space cannot hold the new metadata\n"); + err_printf(" expanded space %lld sectors\n", + (long long)expand->sector_increment); + err_printf(" needed space %lld sectors\n", + (long long)sectors_needed); + ok = FALSE; + } + } + } + if (ok) { + advice = expand->byte_increment; + /* the increment must be an integral number of clusters */ + if (expand->byte_increment & (vol->cluster_size - 1)) { + err_printf("Cannot expand volume without copying the data :\n"); + err_printf("There are %d sectors in a cluster,\n", + (int)(vol->cluster_size/vol->sector_size)); + err_printf(" and the sector difference is not a multiple of %d\n", + (int)(vol->cluster_size/vol->sector_size)); + advice = expand->byte_increment & ~vol->cluster_size; + ok = FALSE; + } + if (!ok) + err_printf("You should increase the beginning of partition by %d sectors\n", + (int)((expand->byte_increment - advice) + >> vol->sector_size_bits)); + } + if (ok) + ok = !check_expand_constraints(expand); + if (ok && opt.info) { + minimum_size = (expand->original_sectors + << vol->sector_size_bits) + + expand->boot_size + + expand->mft_size + + expand->bitmap_allocated; + + printf("You must expand the partition to at least %lld bytes,\n", + (long long)(minimum_size + vol->sector_size)); + printf("and you may add a multiple of %ld bytes to this size.\n", + (long)vol->cluster_size); + printf("The minimum NTFS volume size is %lld bytes\n", + (long long)minimum_size); + ok = FALSE; + } + return (ok); +} + +static int set_bitmap(expand_t *expand, runlist_element *rl) +{ + int res; + s64 lcn; + s64 lcn_end; + BOOL reallocated; + + res = -1; + reallocated = FALSE; + if ((rl->lcn >= 0) + && (rl->length > 0) + && ((rl->lcn + rl->length) + <= (expand->vol->nr_clusters + expand->cluster_increment))) { + lcn = rl->lcn; + lcn_end = lcn + rl->length; + while ((lcn & 7) && (lcn < lcn_end)) { + if (expand->bitmap[lcn >> 3] & 1 << (lcn & 7)) + reallocated = TRUE; + expand->bitmap[lcn >> 3] |= 1 << (lcn & 7); + lcn++; + } + while ((lcn_end - lcn) >= 8) { + if (expand->bitmap[lcn >> 3]) + reallocated = TRUE; + expand->bitmap[lcn >> 3] = 255; + lcn += 8; + } + while (lcn < lcn_end) { + if (expand->bitmap[lcn >> 3] & 1 << (lcn & 7)) + reallocated = TRUE; + expand->bitmap[lcn >> 3] |= 1 << (lcn & 7); + lcn++; + } + if (reallocated) + err_printf("Reallocated cluster found in run" + " lcn 0x%llx length %lld\n", + (long long)rl->lcn,(long long)rl->length); + else + res = 0; + } else { + err_printf("Bad run : lcn 0x%llx length %lld\n", + (long long)rl->lcn,(long long)rl->length); + } + return (res); +} + +/* + * Write the backup bootsector + * + * When this has been done, the resizing cannot be done again + */ + +static int write_bootsector(expand_t *expand) +{ + ntfs_volume *vol; + s64 bw; + int res; + + res = -1; + vol = expand->vol; + if (opt.verbose) + ntfs_log_verbose("Rewriting the backup bootsector\n"); + if (opt.ro_flag) + bw = vol->sector_size; + else + bw = ntfs_pwrite(vol->dev, + expand->new_sectors*vol->sector_size, + vol->sector_size, expand->bootsector); + if (bw == vol->sector_size) + res = 0; + else { + if (bw != -1) + errno = EINVAL; + if (!bw) + err_printf("Failed to rewrite the bootsector (size=0)\n"); + else + err_printf("Error rewriting the bootsector"); + } + return (res); +} + +/* + * Write the new main bitmap + */ + +static int write_bitmap(expand_t *expand) +{ + ntfs_volume *vol; + s64 bw; + u64 cluster; + int res; + + res = -1; + vol = expand->vol; + cluster = vol->nr_clusters + expand->cluster_increment; + while (cluster < (expand->bitmap_size << 3)) { + expand->bitmap[cluster >> 3] |= 1 << (cluster & 7); + cluster++; + } + if (opt.verbose) + ntfs_log_verbose("Writing the new bitmap...\n"); + /* write the full allocation (to avoid having to read) */ + if (opt.ro_flag) + bw = expand->bitmap_allocated; + else + bw = ntfs_pwrite(vol->dev, expand->boot_size, + expand->bitmap_allocated, expand->bitmap); + if (bw == (s64)expand->bitmap_allocated) + res = 0; + else { + if (bw != -1) + errno = EINVAL; + if (!bw) + err_printf("Failed to write the bitmap (size=0)\n"); + else + err_printf("Error rewriting the bitmap"); + } + return (res); +} + +/* + * Copy the $MFT to $MFTMirr + * + * The $MFTMirr is not relocated as it should be kept away from $MFT. + * Apart from the backup bootsector, this is the only part which is + * overwritten. This has no effect on being able to redo the resizing + * if something goes wrong, as the $MFTMirr is never read. However + * this is done near the end of the resizing. + */ + +static int copy_mftmirr(expand_t *expand) +{ + ntfs_volume *vol; + s64 pos; + s64 inum; + int res; + u16 usa_ofs; + le16 *pusn; + u16 usn; + + if (opt.verbose) + ntfs_log_verbose("Copying $MFT to $MFTMirr...\n"); + vol = expand->vol; + res = 0; + for (inum=FILE_MFT; !res && (inum<=FILE_Volume); inum++) { + /* read the new $MFT */ + pos = (expand->mft_lcn << vol->cluster_size_bits) + + (inum << vol->mft_record_size_bits); + if (ntfs_mst_pread(vol->dev, pos, 1, vol->mft_record_size, + expand->mrec) == 1) { + /* overwrite the old $MFTMirr */ + pos = (vol->mftmirr_lcn << vol->cluster_size_bits) + + (inum << vol->mft_record_size_bits) + + expand->byte_increment; + usa_ofs = le16_to_cpu(expand->mrec->usa_ofs); + pusn = (le16*)((u8*)expand->mrec + usa_ofs); + usn = le16_to_cpu(*pusn) - 1; + if (!usn || (usn == 0xffff)) + usn = -2; + *pusn = cpu_to_le16(usn); + if (!opt.ro_flag + && (ntfs_mst_pwrite(vol->dev, pos, 1, + vol->mft_record_size, expand->mrec) != 1)) { + err_printf("Failed to overwrite the old $MFTMirr\n"); + res = -1; + } + } else { + err_printf("Failed to write the new $MFT\n"); + res = -1; + } + } + return (res); +} + +/* + * Copy the $Boot, including the bootsector + * + * When the bootsector has been copied, repair tools are able to + * fix things, but this is dangerous if the other metadata do + * not point to actual user data. So this must be done near the end + * of resizing. + */ + +static int copy_boot(expand_t *expand) +{ + NTFS_BOOT_SECTOR *bs; + char *buf; + ntfs_volume *vol; + s64 mftmirr_lcn; + s64 written; + u32 boot_cnt; + u32 hidden_sectors; + le32 hidden_sectors_le; + int res; + + if (opt.verbose) + ntfs_log_verbose("Copying $Boot...\n"); + vol = expand->vol; + res = 0; + buf = (char*)ntfs_malloc(vol->cluster_size); + if (buf) { + /* set the new volume parameters in the bootsector */ + bs = (NTFS_BOOT_SECTOR*)expand->bootsector; + bs->number_of_sectors = cpu_to_sle64(expand->new_sectors); + bs->mft_lcn = cpu_to_sle64(expand->mft_lcn); + mftmirr_lcn = vol->mftmirr_lcn + expand->cluster_increment; + bs->mftmirr_lcn = cpu_to_sle64(mftmirr_lcn); + /* the hidden sectors are needed to boot into windows */ + memcpy(&hidden_sectors_le,&bs->bpb.hidden_sectors,4); + /* alignment messed up on the Sparc */ + if (hidden_sectors_le) { + hidden_sectors = le32_to_cpu(hidden_sectors_le); + if (hidden_sectors >= expand->sector_increment) + hidden_sectors -= expand->sector_increment; + else + hidden_sectors = 0; + hidden_sectors_le = cpu_to_le32(hidden_sectors); + memcpy(&bs->bpb.hidden_sectors,&hidden_sectors_le,4); + } + written = 0; + boot_cnt = expand->boot_size >> vol->cluster_size_bits; + while (!res && (written < boot_cnt)) { + lseek_to_cluster(vol, expand->cluster_increment + written); + if (!read_all(vol->dev, buf, vol->cluster_size)) { + if (!written) + memcpy(buf, expand->bootsector, vol->sector_size); + lseek_to_cluster(vol, written); + if (!opt.ro_flag + && write_all(vol->dev, buf, vol->cluster_size)) { + err_printf("Failed to write the new $Boot\n"); + res = -1; + } else + written++; + } else { + err_printf("Failed to read the old $Boot\n"); + res = -1; + } + } + free(buf); + } else { + err_printf("Failed to allocate buffer\n"); + res = -1; + } + return (res); +} + +/* + * Process delayed runlist updates + * + * This is derived from delayed_updates() and they should + * both be merged when the new code is considered safe. + */ + +static void delayed_expand(ntfs_volume *vol, struct DELAYED *delayed, + struct progress_bar *progress) +{ + unsigned long count; + struct DELAYED *current; + int step = 100; + + if (delayed) { + if (opt.verbose) + ntfs_log_verbose("Delayed updating of overflowing runlists...\n"); + count = 0; + /* count by steps because of inappropriate resolution */ + for (current=delayed; current; current=current->next) + count += step; + progress_init(progress, 0, count, + (opt.show_progress ? NTFS_PROGBAR : 0)); + current = delayed; + count = 0; + while (current) { + delayed = current; + if (!opt.ro_flag) + expand_attribute_runlist(vol, delayed); + count += step; + progress_update(progress, count); + current = current->next; + if (delayed->attr_name) + free(delayed->attr_name); + free(delayed->head_rl); + free(delayed); + } + } +} + +/* + * Expand the sizes in indexes for inodes which were expanded + * + * Only the new $Bitmap sizes are identified as needed to be + * adjusted in index. The $BadClus is only expanded in an + * alternate data stream, whose sizes are not present in the index. + * + * This is modifying the initial data, and can only be done when + * the volume has been reopened after expanding. + */ + +static int expand_index_sizes(expand_t *expand) +{ + ntfs_inode *ni; + int res; + + res = -1; + ni = ntfs_inode_open(expand->vol, FILE_Bitmap); + if (ni) { + NInoSetDirty(ni); + NInoFileNameSetDirty(ni); + ntfs_inode_close(ni); + res = 0; + } + return (res); +} + +/* + * Update a runlist into an attribute + * + * This is derived from replace_attribute_runlist() and they should + * both be merged when the new code is considered safe. + */ + +static int update_runlist(expand_t *expand, s64 inum, + ATTR_RECORD *a, runlist_element *rl) +{ + ntfs_resize_t resize; + ntfs_attr_search_ctx ctx; + ntfs_volume *vol; + MFT_RECORD *mrec; + runlist *head_rl; + int mp_size; + int l; + int must_delay; + void *mp; + + vol = expand->vol; + mrec = expand->mrec; + head_rl = rl; + rl_fixup(&rl); + if ((mp_size = ntfs_get_size_for_mapping_pairs(vol, rl, + 0, INT_MAX)) == -1) + perr_exit("ntfs_get_size_for_mapping_pairs"); + + if (a->name_length) { + u16 name_offs = le16_to_cpu(a->name_offset); + u16 mp_offs = le16_to_cpu(a->mapping_pairs_offset); + + if (name_offs >= mp_offs) + err_exit("Attribute name is after mapping pairs! " + "Please report!\n"); + } + + /* CHECKME: don't trust mapping_pairs is always the last item in the + attribute, instead check for the real size/space */ + l = (int)le32_to_cpu(a->length) - le16_to_cpu(a->mapping_pairs_offset); + must_delay = 0; + if (mp_size > l) { + s32 remains_size; + char *next_attr; + + ntfs_log_verbose("Enlarging attribute header ...\n"); + + mp_size = (mp_size + 7) & ~7; + + ntfs_log_verbose("Old mp size : %d\n", l); + ntfs_log_verbose("New mp size : %d\n", mp_size); + ntfs_log_verbose("Bytes in use : %u\n", (unsigned int) + le32_to_cpu(mrec->bytes_in_use)); + + next_attr = (char *)a + le32_to_cpu(a->length); + l = mp_size - l; + + ntfs_log_verbose("Bytes in use new : %u\n", l + (unsigned int) + le32_to_cpu(mrec->bytes_in_use)); + ntfs_log_verbose("Bytes allocated : %u\n", (unsigned int) + le32_to_cpu(mrec->bytes_allocated)); + + remains_size = le32_to_cpu(mrec->bytes_in_use); + remains_size -= (next_attr - (char *)mrec); + + ntfs_log_verbose("increase : %d\n", l); + ntfs_log_verbose("shift : %lld\n", + (long long)remains_size); + if (le32_to_cpu(mrec->bytes_in_use) + l > + le32_to_cpu(mrec->bytes_allocated)) { + ntfs_log_verbose("Queuing expansion for later processing\n"); + /* hack for reusing unmodified old code ! */ + resize.ctx = &ctx; + ctx.attr = a; + ctx.mrec = mrec; + resize.mref = inum; + resize.delayed_runlists = expand->delayed_runlists; + resize.mirr_from = MIRR_OLD; + must_delay = 1; + replace_later(&resize,rl,head_rl); + expand->delayed_runlists = resize.delayed_runlists; + } else { + memmove(next_attr + l, next_attr, remains_size); + mrec->bytes_in_use = cpu_to_le32(l + + le32_to_cpu(mrec->bytes_in_use)); + a->length = cpu_to_le32(le32_to_cpu(a->length) + l); + } + } + + if (!must_delay) { + mp = ntfs_calloc(mp_size); + if (!mp) + perr_exit("ntfsc_calloc couldn't get memory"); + + if (ntfs_mapping_pairs_build(vol, (u8*)mp, mp_size, rl, 0, NULL)) + perr_exit("ntfs_mapping_pairs_build"); + + memmove((u8*)a + le16_to_cpu(a->mapping_pairs_offset), mp, mp_size); + + free(mp); + } + return (must_delay); +} + +/* + * Create a minimal valid MFT record + */ + +static int minimal_record(expand_t *expand, MFT_RECORD *mrec) +{ + int usa_count; + u32 bytes_in_use; + + memset(mrec,0,expand->vol->mft_record_size); + mrec->magic = magic_FILE; + mrec->usa_ofs = const_cpu_to_le16(sizeof(MFT_RECORD)); + usa_count = expand->vol->mft_record_size / NTFS_BLOCK_SIZE + 1; + mrec->usa_count = cpu_to_le16(usa_count); + bytes_in_use = (sizeof(MFT_RECORD) + 2*usa_count + 7) & -8; + memset(((char*)mrec) + bytes_in_use, 255, 4); /* AT_END */ + bytes_in_use += 8; + mrec->bytes_in_use = cpu_to_le32(bytes_in_use); + mrec->bytes_allocated = cpu_to_le32(expand->vol->mft_record_size); + return (0); +} + +/* + * Rebase all runlists of an MFT record + * + * Iterate through all its attributes and offset the non resident ones + */ + +static int rebase_runlists(expand_t *expand, s64 inum) +{ + MFT_RECORD *mrec; + ATTR_RECORD *a; + runlist_element *rl; + runlist_element *prl; + u32 offset; + int res; + + res = 0; + mrec = expand->mrec; + offset = le16_to_cpu(mrec->attrs_offset); + a = (ATTR_RECORD*)((char*)mrec + offset); + while (!res && (a->type != AT_END) + && (offset < le32_to_cpu(mrec->bytes_in_use))) { + if (a->non_resident) { + rl = ntfs_mapping_pairs_decompress(expand->vol, a, + (runlist_element*)NULL); + if (rl) { + for (prl=rl; prl->length; prl++) + if (prl->lcn >= 0) { + prl->lcn += expand->cluster_increment; + if (set_bitmap(expand,prl)) + res = -1; + } + if (update_runlist(expand,inum,a,rl)) { + ntfs_log_verbose("Runlist updating has to be delayed\n"); + } else + free(rl); + } else { + err_printf("Could not get a runlist of inode %lld\n", + (long long)inum); + res = -1; + } + } + offset += le32_to_cpu(a->length); + a = (ATTR_RECORD*)((char*)mrec + offset); + } + return (res); +} + +/* + * Rebase the runlists present in records with relocated $DATA + * + * The returned runlist is the old rebased runlist for $DATA, + * which is generally different from the new computed runlist. + */ + +static runlist_element *rebase_runlists_meta(expand_t *expand, s64 inum) +{ + MFT_RECORD *mrec; + ATTR_RECORD *a; + ntfs_volume *vol; + runlist_element *rl; + runlist_element *old_rl; + runlist_element *prl; + runlist_element new_rl[2]; + s64 data_size; + s64 allocated_size; + s64 lcn; + u64 lth; + u32 offset; + BOOL keeprl; + int res; + + res = 0; + old_rl = (runlist_element*)NULL; + vol = expand->vol; + mrec = expand->mrec; + switch (inum) { + case FILE_Boot : + lcn = 0; + lth = expand->boot_size >> vol->cluster_size_bits; + data_size = expand->boot_size; + break; + case FILE_Bitmap : + lcn = expand->boot_size >> vol->cluster_size_bits; + lth = expand->bitmap_allocated >> vol->cluster_size_bits; + data_size = expand->bitmap_size; + break; + case FILE_MFT : + lcn = (expand->boot_size + expand->bitmap_allocated) + >> vol->cluster_size_bits; + lth = expand->mft_size >> vol->cluster_size_bits; + data_size = expand->mft_size; + break; + case FILE_BadClus : + lcn = 0; /* not used */ + lth = vol->nr_clusters + expand->cluster_increment; + data_size = lth << vol->cluster_size_bits; + break; + default : + lcn = lth = data_size = 0; + res = -1; + } + allocated_size = lth << vol->cluster_size_bits; + offset = le16_to_cpu(mrec->attrs_offset); + a = (ATTR_RECORD*)((char*)mrec + offset); + while (!res && (a->type != AT_END) + && (offset < le32_to_cpu(mrec->bytes_in_use))) { + if (a->non_resident) { + keeprl = FALSE; + rl = ntfs_mapping_pairs_decompress(vol, a, + (runlist_element*)NULL); + if (rl) { + /* rebase the old runlist */ + for (prl=rl; prl->length; prl++) + if (prl->lcn >= 0) { + prl->lcn += expand->cluster_increment; + if ((a->type != AT_DATA) + && set_bitmap(expand,prl)) + res = -1; + } + /* relocated unnamed data (not $BadClus) */ + if ((a->type == AT_DATA) + && !a->name_length + && (inum != FILE_BadClus)) { + old_rl = rl; + rl = new_rl; + keeprl = TRUE; + rl[0].vcn = 0; + rl[0].lcn = lcn; + rl[0].length = lth; + rl[1].vcn = lth; + rl[1].lcn = LCN_ENOENT; + rl[1].length = 0; + if (set_bitmap(expand,rl)) + res = -1; + a->data_size = cpu_to_sle64(data_size); + a->initialized_size = a->data_size; + a->allocated_size + = cpu_to_sle64(allocated_size); + a->highest_vcn = cpu_to_sle64(lth - 1); + } + /* expand the named data for $BadClus */ + if ((a->type == AT_DATA) + && a->name_length + && (inum == FILE_BadClus)) { + old_rl = rl; + keeprl = TRUE; + prl = rl; + if (prl->length) { + while (prl[1].length) + prl++; + prl->length = lth - prl->vcn; + prl[1].vcn = lth; + } else + prl->vcn = lth; + a->data_size = cpu_to_sle64(data_size); + /* do not change the initialized size */ + a->allocated_size + = cpu_to_sle64(allocated_size); + a->highest_vcn = cpu_to_sle64(lth - 1); + } + if (!res && update_runlist(expand,inum,a,rl)) + res = -1; + if (!keeprl) + free(rl); + } else { + err_printf("Could not get the data runlist of inode %lld\n", + (long long)inum); + res = -1; + } + } + offset += le32_to_cpu(a->length); + a = (ATTR_RECORD*)((char*)mrec + offset); + } + if (res && old_rl) { + free(old_rl); + old_rl = (runlist_element*)NULL; + } + return (old_rl); +} + +/* + * Rebase all runlists in an MFT record + * + * Read from the old $MFT, rebase the runlists, + * and write to the new $MFT + */ + +static int rebase_inode(expand_t *expand, const runlist_element *prl, + s64 inum, s64 jnum) +{ + MFT_RECORD *mrec; + runlist_element *rl; + ntfs_volume *vol; + s64 pos; + int res; + + res = 0; + vol = expand->vol; + mrec = expand->mrec; + if (expand->mft_bitmap[inum >> 3] & (1 << (inum & 7))) { + pos = (prl->lcn << vol->cluster_size_bits) + + ((inum - jnum) << vol->mft_record_size_bits); + if ((ntfs_mst_pread(vol->dev, pos, 1, + vol->mft_record_size, mrec) == 1) + && (mrec->flags & MFT_RECORD_IN_USE)) { + switch (inum) { + case FILE_Bitmap : + case FILE_Boot : + case FILE_BadClus : + rl = rebase_runlists_meta(expand, inum); + if (rl) + free(rl); + else + res = -1; + break; + default : + res = rebase_runlists(expand, inum); + break; + } + } else { + err_printf("Could not read the $MFT entry %lld\n", + (long long)inum); + res = -1; + } + } else { + /* + * Replace unused records (possibly uninitialized) + * by minimal valid records, not marked in use + */ + res = minimal_record(expand,mrec); + } + if (!res) { + pos = (expand->mft_lcn << vol->cluster_size_bits) + + (inum << vol->mft_record_size_bits); + if (opt.verbose) + ntfs_log_verbose("Rebasing inode %lld cluster 0x%llx\n", + (long long)inum, + (long long)(pos >> vol->cluster_size_bits)); + if (!opt.ro_flag + && (ntfs_mst_pwrite(vol->dev, pos, 1, + vol->mft_record_size, mrec) != 1)) { + err_printf("Could not write the $MFT entry %lld\n", + (long long)inum); + res = -1; + } + } + return (res); +} + +/* + * Rebase all runlists + * + * First get the $MFT and define its location in the expanded space, + * then rebase the other inodes and write them to the new $MFT + */ + +static int rebase_all_inodes(expand_t *expand) +{ + ntfs_volume *vol; + MFT_RECORD *mrec; + s64 inum; + s64 jnum; + s64 inodecnt; + s64 pos; + s64 got; + int res; + runlist_element *mft_rl; + runlist_element *prl; + + res = 0; + mft_rl = (runlist_element*)NULL; + vol = expand->vol; + mrec = expand->mrec; + inum = 0; + pos = (vol->mft_lcn + expand->cluster_increment) + << vol->cluster_size_bits; + got = ntfs_mst_pread(vol->dev, pos, 1, + vol->mft_record_size, mrec); + if ((got == 1) && (mrec->flags & MFT_RECORD_IN_USE)) { + pos = expand->mft_lcn << vol->cluster_size_bits; + if (opt.verbose) + ntfs_log_verbose("Rebasing inode %lld cluster 0x%llx\n", + (long long)inum, + (long long)(pos >> vol->cluster_size_bits)); + mft_rl = rebase_runlists_meta(expand, FILE_MFT); + if (!mft_rl + || (!opt.ro_flag + && (ntfs_mst_pwrite(vol->dev, pos, 1, + vol->mft_record_size, mrec) != 1))) + res = -1; + else { + for (prl=mft_rl; prl->length; prl++) { } + inodecnt = (prl->vcn << vol->cluster_size_bits) + >> vol->mft_record_size_bits; + progress_init(expand->progress, 0, inodecnt, + (opt.show_progress ? NTFS_PROGBAR : 0)); + prl = mft_rl; + jnum = 0; + do { + inum++; + while (prl->length + && ((inum << vol->mft_record_size_bits) + >= ((prl->vcn + prl->length) + << vol->cluster_size_bits))) { + prl++; + jnum = inum; + } + progress_update(expand->progress, inum); + if (prl->length) { + res = rebase_inode(expand, + prl,inum,jnum); + } + } while (!res && prl->length); + free(mft_rl); + } + } else { + err_printf("Could not read the old $MFT\n"); + res = -1; + } + return (res); +} + + + +/* + * Get the old volume parameters from the backup bootsector + * + */ + +static ntfs_volume *get_volume_data(expand_t *expand, struct ntfs_device *dev, + s32 sector_size) +{ + s64 br; + ntfs_volume *vol; + le16 sector_size_le; + NTFS_BOOT_SECTOR *bs; + BOOL ok; + + ok = FALSE; + vol = (ntfs_volume*)ntfs_malloc(sizeof(ntfs_volume)); + expand->bootsector = (char*)ntfs_malloc(sector_size); + if (vol && expand->bootsector) { + expand->vol = vol; + vol->dev = dev; + br = ntfs_pread(dev, expand->new_sectors*sector_size, + sector_size, expand->bootsector); + if (br != sector_size) { + if (br != -1) + errno = EINVAL; + if (!br) + err_printf("Failed to read the backup bootsector (size=0)\n"); + else + err_printf("Error reading the backup bootsector"); + } else { + bs = (NTFS_BOOT_SECTOR*)expand->bootsector; + /* alignment problem on Sparc, even doing memcpy() */ + sector_size_le = cpu_to_le16(sector_size); + if (!memcmp(§or_size_le, + &bs->bpb.bytes_per_sector,2) + && ntfs_boot_sector_is_ntfs(bs) + && !ntfs_boot_sector_parse(vol, bs)) { + expand->original_sectors + = sle64_to_cpu(bs->number_of_sectors); + expand->mrec = (MFT_RECORD*) + ntfs_malloc(vol->mft_record_size); + if (expand->mrec + && can_expand(expand,vol)) { + ntfs_log_verbose("Resizing is possible\n"); + ok = TRUE; + } + } else + err_printf("Could not get the old volume parameters " + "from the backup bootsector\n"); + } + if (!ok) { + free(vol); + free(expand->bootsector); + } + } + return (ok ? vol : (ntfs_volume*)NULL); +} + +static int really_expand(expand_t *expand) +{ + ntfs_volume *vol; + struct ntfs_device *dev; + int res; + + res = -1; + + expand->bitmap = (u8*)ntfs_calloc(expand->bitmap_allocated); + if (expand->bitmap + && get_mft_bitmap(expand)) { + printf("\n*** WARNING ***\n\n"); + printf("Expanding a volume is an experimental new feature\n"); + if (!opt.ro_flag) + printf("A first check with option -n is recommended\n"); + printf("\nShould something go wrong during the actual" + " resizing (power outage, etc.),\n"); + printf("just restart the procedure, but DO NOT TRY to repair" + " with chkdsk or similar,\n"); + printf("until the resizing is over," + " you would LOSE YOUR DATA !\n"); + printf("\nYou have been warned !\n\n"); + if (!opt.ro_flag && (opt.force-- <= 0)) + proceed_question(); + if (!rebase_all_inodes(expand) + && !write_bitmap(expand) + && !copy_mftmirr(expand) + && !copy_boot(expand)) { + free(expand->vol); + expand->vol = (ntfs_volume*)NULL; + free(expand->mft_bitmap); + expand->mft_bitmap = (u8*)NULL; + if (!opt.ro_flag) { + /* the volume must be dirty, do not check */ + opt.force++; + vol = mount_volume(); + if (vol) { + dev = vol->dev; + ntfs_log_verbose("Remounting the updated volume\n"); + expand->vol = vol; + ntfs_log_verbose("Delayed runlist updatings\n"); + delayed_expand(vol, expand->delayed_runlists, + expand->progress); + expand->delayed_runlists + = (struct DELAYED*)NULL; + expand_index_sizes(expand); + /* rewriting the backup bootsector, no return ticket now ! */ + res = write_bootsector(expand); + if (dev->d_ops->sync(dev) == -1) { + printf("Could not sync\n"); + res = -1; + } + ntfs_umount(vol,0); + if (!res) + printf("\nResizing completed successfully\n"); + } + } else { + ntfs_log_verbose("Delayed runlist updatings\n"); + delayed_expand(expand->vol, + expand->delayed_runlists, + expand->progress); + expand->delayed_runlists + = (struct DELAYED*)NULL; + printf("\nAll checks have been completed successfully\n"); + printf("Cannot check further in no-action mode\n"); + } + free(expand->bootsector); + free(expand->mrec); + } + free(expand->bitmap); + } else { + err_printf("Failed to allocate memory\n"); + } + return (res); +} + +/* + * Expand a volume to beginning of partition + * + * We rely on the backup bootsector to determine the original + * volume size and metadata. + */ + +static int expand_to_beginning(void) +{ + expand_t expand; + struct progress_bar progress; + int ret; + ntfs_volume *vol; + struct ntfs_device *dev; + int sector_size; + s64 new_sectors; + + ret = -1; + dev = ntfs_device_alloc(opt.volume, 0, &ntfs_device_default_io_ops, + NULL); + if (dev) { + if (!(*dev->d_ops->open)(dev, + (opt.ro_flag ? O_RDONLY : O_RDWR))) { + sector_size = ntfs_device_sector_size_get(dev); + if (sector_size <= 0) { + sector_size = 512; + new_sectors = ntfs_device_size_get(dev, + sector_size); + if (!new_sectors) { + sector_size = 4096; + new_sectors = ntfs_device_size_get(dev, + sector_size); + } + } else + new_sectors = ntfs_device_size_get(dev, + sector_size); + if (new_sectors) { + new_sectors--; /* last sector not counted */ + expand.new_sectors = new_sectors; + expand.progress = &progress; + expand.delayed_runlists = (struct DELAYED*)NULL; + vol = get_volume_data(&expand,dev,sector_size); + if (vol) { + expand.vol = vol; + ret = really_expand(&expand); + } + } + (*dev->d_ops->close)(dev); + } else { + err_exit("Couldn't open volume '%s'!\n", opt.volume); + } + ntfs_device_free(dev); + } + return (ret); +} + + +int main(int argc, char **argv) +{ + ntfsck_t fsck; + ntfs_resize_t resize; + s64 new_size = 0; /* in clusters; 0 = --info w/o --size */ + s64 device_size; /* in bytes */ + ntfs_volume *vol = NULL; + int res; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + printf("%s v%s (libntfs-3g)\n", EXEC_NAME, VERSION); + + res = parse_options(argc, argv); + if (res >= 0) + return (res); + + utils_set_locale(); + + /* + * If we're just checking the device, we'll do it first, + * and exit out, no matter what we find. + */ + if (opt.check) { + vol = check_volume(); +#if CLEAN_EXIT + if (vol) + ntfs_umount(vol,0); +#endif + exit(0); + } else { + if (opt.expand) { + /* + * If we are to expand to beginning of partition, do + * not try to mount : when merging two partitions, + * the beginning of the partition would contain an + * old filesystem which is not the one to expand. + */ + if (expand_to_beginning() && !opt.info) + exit(1); + return (0); + } + } + + if (!(vol = mount_volume())) + err_exit("Couldn't open volume '%s'!\n", opt.volume); + + device_size = ntfs_device_size_get(vol->dev, vol->sector_size); + device_size *= vol->sector_size; + if (device_size <= 0) + err_exit("Couldn't get device size (%lld)!\n", + (long long)device_size); + + if (!opt.infombonly) + print_vol_size("Current device size", device_size); + + if (device_size < vol->nr_clusters * vol->cluster_size) + err_exit("Current NTFS volume size is bigger than the device " + "size!\nCorrupt partition table or incorrect device " + "partitioning?\n"); + + if (!opt.bytes && !opt.info && !opt.infombonly) { + opt.bytes = device_size; + opt.reliable_size = 1; + } + + /* Backup boot sector at the end of device isn't counted in NTFS + volume size thus we have to reserve space for it. */ + if (opt.bytes > vol->sector_size) + new_size = (opt.bytes - vol->sector_size) / vol->cluster_size; + else + new_size = 0; + + if (!opt.info && !opt.infombonly) { + print_vol_size("New volume size ", vol_size(vol, new_size)); + if (device_size < opt.bytes) + err_exit("New size can't be bigger than the device size" + ".\nIf you want to enlarge NTFS then first " + "enlarge the device size by e.g. fdisk.\n"); + } + + if (!opt.info && !opt.infombonly && (new_size == vol->nr_clusters || + (opt.bytes == device_size && + new_size == vol->nr_clusters - 1))) { + printf("Nothing to do: NTFS volume size is already OK.\n"); + exit(0); + } + + memset(&resize, 0, sizeof(resize)); + resize.vol = vol; + resize.new_volume_size = new_size; + /* This is also true if --info was used w/o --size (new_size = 0) */ + if (new_size < vol->nr_clusters) + resize.shrink = 1; + if (opt.show_progress) + resize.progress.flags |= NTFS_PROGBAR; + /* + * Checking and __reporting__ of bad sectors must be done before cluster + * allocation check because chkdsk doesn't fix $Bitmap's w/ bad sectors + * thus users would (were) quite confused why chkdsk doesn't work. + */ + resize.badclusters = check_bad_sectors(vol); + + NVolSetNoFixupWarn(vol); + check_cluster_allocation(vol, &fsck); + + print_disk_usage(vol, fsck.inuse); + + resize.inuse = fsck.inuse; + resize.lcn_bitmap = fsck.lcn_bitmap; + resize.mirr_from = MIRR_OLD; + + set_resize_constraints(&resize); + set_disk_usage_constraint(&resize); + check_resize_constraints(&resize); + + if (opt.info || opt.infombonly) { + advise_on_resize(&resize); + exit(0); + } + + if (opt.force-- <= 0 && !opt.ro_flag) { + printf("%s", resize_warning_msg); + proceed_question(); + } + + /* FIXME: performance - relocate logfile here if it's needed */ + prepare_volume_fixup(vol); + + if (resize.relocations) + relocate_inodes(&resize); + + truncate_badclust_file(&resize); + truncate_bitmap_file(&resize); + delayed_updates(&resize); + update_bootsector(&resize); + + /* We don't create backup boot sector because we don't know where the + partition will be split. The scheduled chkdsk will fix it */ + + if (opt.ro_flag) { + printf("The read-only test run ended successfully.\n"); + exit(0); + } + + /* WARNING: don't modify the texts, external tools grep for them */ + printf("Syncing device ...\n"); + if (vol->dev->d_ops->sync(vol->dev) == -1) + perr_exit("fsync"); + + printf("Successfully resized NTFS on device '%s'.\n", vol->dev->d_name); + if (resize.shrink) + printf("%s", resize_important_msg); + if (resize.lcn_bitmap.bm) + free(resize.lcn_bitmap.bm); + if (vol) + ntfs_umount(vol,0); + return 0; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfssecaudit.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfssecaudit.8.in new file mode 100644 index 0000000..955d6c0 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfssecaudit.8.in @@ -0,0 +1,184 @@ +.\" Copyright (c) 2007-2016 Jean-Pierre André. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSSECAUDIT 8 "February 2010" "ntfssecaudit 1.5.0" +.SH NAME +ntfssecaudit \- NTFS Security Data Auditing +.SH SYNOPSIS +.B ntfssecaudit +\fB[\fIoptions\fP\fB]\fR +.I args +.PP +Where \fIoptions\fP is a combination of : +.RS +-a full auditing of security data (Linux only) +.RE +.RS +-b backup ACLs +.RE +.RS +-e setting extra backed-up parameters (in conjunction with -s) +.RE +.RS +-h displaying hexadecimal security descriptors saved in a file +.RE +.RS +-r recursing in a directory +.RE +.RS +-s setting backed-up ACLs +.RE +.RS +-u getting a user mapping proposal +.RE +.RS +-v verbose (very verbose if set twice) +.RE +.PP +and args define the parameters and the set of files acted upon. +.PP +Typing secaudit with no args will display a summary of available options. +.SH DESCRIPTION +\fBntfssecaudit\fR +displays the ownership and permissions of a set of files on an NTFS +file system, and checks their consistency. It can be started in terminal +mode only (no graphical user interface is available.) +.PP +When a \fIvolume\fR is required, it has to be unmounted, and the command +has to be issued as \fBroot\fP. The \fIvolume\fR can be either a block +device (i.e. a disk partition) or an image file. +.PP +When acting on a directory or volume, the command may produce a lot +of information. It is therefore advisable to redirect the output to +a file or pipe it to a text editor for examination. +.SH OPTIONS +Below are the valid combinations of options and arguments that +\fBntfssecaudit\fR accepts. All the indicated arguments are +mandatory and must be unique (if wildcards are used, they must +resolve to a single name.) +.TP +\fB-h\fP \fIfile\fP +Displays in an human readable form the hexadecimal security descriptors +saved in \fIfile\fP. This can be used to turn a verbose output into a very +verbose output. +.TP +\fB-a[rv]\fP \fIvolume\fP +Audits the volume : all the global security data on \fIvolume\fP are scanned +and errors are displayed. If option \fB-r\fP is present, all files and +directories are also scanned and their relations to global security data +are checked. This can produce a lot of data. + +This option is not effective on volumes formatted for old NTFS versions (pre +NTFS 3.0). Such volumes have no global security data. + +When errors are signalled, it is advisable to repair the volume with an +appropriate tool (such as \fBchkdsk\fP on Windows.) +.TP +\fB[-v]\fP \fIvolume\fP \fIfile\fP +Displays the security parameters of \fIfile\fP : its interpreted Linux mode +(rwx flags in octal) and Posix ACL[1], its security key if any, and its +security descriptor if verbose output. +.TP +\fB-r[v]\fP \fIvolume\fP \fIdirectory\fP +displays the security parameters of all files and subdirectories in +\fIdirectory\fP : their interpreted Linux mode (rwx flags in octal) and Posix +ACL[1], their security key if any, and their security descriptor if +verbose output. +.TP +.B -b[v] \fIvolume\fP \fI[directory]\fP +Recursively extracts to standard output the NTFS ACLs of files in \fIvolume\fP +and \fIdirectory\fP. +.TP +\fB-s[ev]\fP \fIvolume\fP \fI[backup-file]\fP +Sets the NTFS ACLS as indicated in \fIbackup-file\fP or standard input. The +input data must have been created on Linux. With option \fB-e\fP, also sets +extra parameters (currently Windows attrib). +.TP +\fIvolume\fP \fIperms\fP \fIfile\fP +Sets the security parameters of file to perms. Perms is the Linux +requested mode (rwx flags, expressed in octal form as in chmod) or +a Posix ACL[1] (expressed like in setfacl -m). This sets a new ACL +which is effective for Linux and Windows. +.TP +\fB-r[v]\fP \fIvolume\fP \fIperms\fP \fIdirectory\fP +Sets the security parameters of all files and subdirectories in +\fIdirectory\fP to \fIperms\fP. Perms is the Linux requested mode (rwx flags, +expressed in octal form as in \fBchmod\fP), or a Posix ACL[1] (expressed like +in \fBsetfacl -m\fP.) This sets new ACLs which are effective for Linux and +Windows. +.TP +\fB[-v]\fP \fImounted-file\fP +Displays the security parameters of \fImounted-file\fP : its interpreted +Linux mode (rwx flags in octal) and Posix ACL[1], its security key if any, +and its security descriptor if verbose output. This is a special case which +acts on a mounted file (or directory) and does not require being root. The +Posix ACL interpretation can only be displayed if the full path to +\fImounted-file\fP from the root of the global file tree is provided. +.TP +\fB-u[v]\fP \fImounted-file\fP +Displays a proposed contents for a user mapping file, based on the +ownership parameters set by Windows on \fImounted-file\fP, assuming +this file was created on Windows by the user who should be mapped to the +current Linux user. The displayed information has to be copied to the +file \fB.NTFS-3G/UserMapping\fP where \fB.NTFS-3G\fP is a hidden +subdirectory of the root of the partition for which the mapping is to +be defined. This will cause the ownership of files created on that +partition to be the same as the original \fImounted-file\fP. +.SH NOTE +[1] provided the POSIX ACL option was selected at compile time. A Posix ACL +specification looks like "\fB[d:]{ugmo}:[id]:[perms],...\fP" where id is a +numeric user or group id, and perms an octal digit or a set from the letters +r, w and x. +.RS +Example : "\fBu::7,g::5,o:0,u:510:rwx,g:500:5,d:u:510:7\fP" +.SH EXAMPLES +Audit the global security data on /dev/sda1 +.RS +.sp +.B ntfssecaudit -ar /dev/sda1 +.sp +.RE +Display the ownership and permissions parameters for files in directory +/audio/music on device /dev/sda5, excluding sub-directories : +.RS +.sp +.B ntfssecaudit /dev/sda5 /audio/music +.sp +.RE +Set all files in directory /audio/music on device /dev/sda5 as writeable +by owner and read-only for everybody : +.RS +.sp +.B ntfssecaudit -r /dev/sda5 644 /audio/music +.sp +.RE +.SH EXIT CODES +.B ntfssecaudit +exits with a value of 0 when no error was detected, and with a value +of 1 when an error was detected. +.SH KNOWN ISSUES +Please see +.RS +.sp +https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ/ +.sp +.RE +for common questions and known issues. +If you would find a new one in the latest release of +the software then please send an email describing it +in detail. You can contact the +development team on the ntfs\-3g\-devel@lists.sf.net +address. +.SH AUTHORS +.B ntfssecaudit +has been developed by Jean-Pierre André. +.SH THANKS +Several people made heroic efforts, often over five or more +years which resulted the ntfs-3g driver. Most importantly they are +Anton Altaparmakov, Richard Russon, Szabolcs Szakacsits, Yura Pakhuchiy, +Yuval Fledel, and the author of the groundbreaking FUSE filesystem development +framework, Miklos Szeredi. +.SH SEE ALSO +.BR ntfsprogs (8), +.BR attr (5), +.BR getfattr (1) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfssecaudit.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfssecaudit.c new file mode 100644 index 0000000..9484a60 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfssecaudit.c @@ -0,0 +1,6458 @@ +/* + * Display and audit security attributes in an NTFS volume + * + * Copyright (c) 2007-2016 Jean-Pierre Andre + * + * Options : + * -a auditing security data + * -b backing up NTFS ACLs + * -e set extra backed-up parameters (in conjunction with -s) + * -h displaying hexadecimal security descriptors within a file + * -r recursing in a directory + * -s setting backed-up NTFS ACLs + * -u getting a user mapping proposal + * -v verbose (very verbose if set twice) + * also, if compile-time option is set + * -t run internal tests (with no access to storage) + * + * On Linux (being root, with volume not mounted) : + * ntfssecaudit -h [file] + * display the security descriptors found in file + * ntfssecaudit -a[rv] volume + * audit the volume + * ntfssecaudit [-v] volume file + * display the security parameters of file + * ntfssecaudit -r[v] volume directory + * display the security parameters of files in directory + * ntfssecaudit -b[v] volume [directory] + * backup the security parameters of files in directory + * ntfssecaudit -s[ve] volume [backupfile] + * set the security parameters as indicated in backup + * with -e set extra parameters (Windows attrib) + * ntfssecaudit volume perms file + * set the security parameters of file to perms (mode or acl) + * ntfssecaudit -r[v] volume perms directory + * set the security parameters of files in directory to perms + * special cases, do not require being root : + * ntfssecaudit [-v] mounted-file + * display the security parameters of mounted file + * ntfssecaudit -u[v] mounted-file + * display a user mapping proposal + * + * + * On Windows (the volume being part of file name) + * ntfssecaudit -h [file] + * display the security descriptors found in file + * ntfssecaudit -a[rv] volume + * audit the volume + * ntfssecaudit [-v] file + * display the security parameters of file + * ntfssecaudit -r[v] directory + * display the security parameters of files in directory + * ntfssecaudit -b[v] directory + * backup the security parameters of files in directory + * ntfssecaudit -s[v] volume [backupfile] + * set the security parameters as indicated in backup + * with -e set extra parameters (Windows attrib) + * ntfssecaudit perms file + * set the security parameters of file to perms (mode or acl) + * ntfssecaudit -r[v] perms directory + * set the security parameters of files in directory to perms + */ + +/* History + * + * Nov 2007 + * - first version, by concatenating miscellaneous utilities + * + * Jan 2008, version 1.0.1 + * - fixed mode displaying + * - added a global severe errors count + * + * Feb 2008, version 1.0.2 + * - implemented conversions for big-endian machines + * + * Mar 2008, version 1.0.3 + * - avoided consistency checks on $Secure when there is no such file + * + * Mar 2008, version 1.0.4 + * - changed ordering of ACE's + * - changed representation for special flags + * - defaulted to stdin for option -h + * - added self tests (compile time option) + * - fixed errors specific to big-endian computers + * + * Apr 2008, version 1.1.0 + * - developped Posix ACLs to NTFS ACLs conversions + * - developped NTFS ACLs backup and restore + * + * Apr 2008, version 1.1.1 + * - fixed an error specific to big-endian computers + * - checked hash value and fixed error report in restore + * - improved display in showhex() and restore() + * + * Apr 2008, version 1.1.2 + * - improved and fixed Posix ACLs to NTFS ACLs conversions + * + * Apr 2008, version 1.1.3 + * - reenabled recursion for setting a new mode or ACL + * - processed Unicode file names and displayed them as UTF-8 + * - allocated dynamically memory for file names when recursing + * + * May 2008, version 1.1.4 + * - all Unicode/UTF-8 strings checked and processed + * + * Jul 2008, version 1.1.5 + * - made Windows owner consistent with Linux owner when changing mode + * - allowed owner change on Windows when it does not match Linux owner + * - skipped currently unused code + * + * Aug 2008, version 1.2.0 + * - processed \.NTFS-3G\UserMapping on Windows + * - made use of user mappings through the security API or direct access + * - fixed a bug in restore + * - fixed UTF-8 conversions + * + * Sep 2008, version 1.3.0 + * - split the code to have part of it shared with ntfs-3g library + * - fixed testing for end of SDS block + * - added samples checking in selftest for easier debugging + * + * Oct 2008, version 1.3.1 + * - fixed outputting long long data when testing on a Palm organizer + * + * Dec 2008, version 1.3.2 + * - fixed restoring ACLs + * - added optional logging of ACL hashes to facilitate restore checks + * - fixed collecting SACLs + * - fixed setting special control flags + * - fixed clearing existing SACLs (Linux only) and DACLs + * - changed the sequencing of items when quering a security descriptor + * - avoided recursing on junctions and symlinks on Windows + * + * Jan 2009, version 1.3.3 + * - save/restore Windows attributes (code from Faisal) + * + * Mar 2009, version 1.3.4 + * - enabled displaying attributes of a mounted file over Linux + * + * Apr 2009, version 1.3.5 + * - fixed initialisation of stand-alone user mapping + * - fixed POSIXACL redefinition when included in the ntfs-3g package + * - fixed displaying of options + * - fixed a dependency on the shared library version used + * + * May 2009, version 1.3.6 + * - added new samples for self testing + * + * Jun 2009, version 1.3.7 + * - fixed displaying owner and group of a mounted file over Linux + * + * Jul 2009, version 1.3.8 + * - fixed again displaying owner and group of a mounted file over Linux + * - cleaned some code to avoid warnings + * + * Nov 2009, version 1.3.9 + * - allowed security descriptors up to 64K + * + * Nov 2009, version 1.3.10 + * - applied patches for MacOSX from Erik Larsson + * + * Nov 2009, version 1.3.11 + * - replace by (provided by glibc) + * + * Dec 2009, version 1.3.12 + * - worked around "const" possibly redefined in config.h + * + * Dec 2009, version 1.3.13 + * - fixed the return code of dorestore() + * + * Dec 2009, version 1.3.14 + * - adapted to opensolaris + * + * Jan 2010, version 1.3.15 + * - more adaptations to opensolaris + * - removed the fix for return code of dorestore() + * + * Jan 2010, version 1.3.16 + * - repeated the fix for return code of dorestore() + * + * Mar 2010, version 1.3.17 + * - adapted to new default user mapping + * - fixed #ifdef'd code for selftest + * + * May 2010, version 1.3.18 + * - redefined early error logging + * + * Mar 2011, version 1.3.19 + * - fixed interface to ntfs_initialize_file_security() + * + * Apr 2011, version 1.3.20 + * - fixed false memory leak detection + * + * Jun 2011, version 1.3.21 + * - cleaned a few unneeded variables + * + * Nov 2011, version 1.3.22 + * - added a distinctive prefix to owner and group SID + * - fixed a false memory leak detection + * + * Jun 2012, version 1.3.23 + * - added support for SACL (nickgarvey) + * + * Jul 2012, version 1.3.24 + * - added self-tests for authenticated users + * - added display of ace-inherited flag + * - made runnable on OpenIndiana + * + * Aug 2012, version 1.4.0 + * - added an option for user mapping proposal + * + * Sep 2013, version 1.4.1 + * - silenced an aliasing warning by gcc >= 4.8 + * + * May 2014, version 1.4.2 + * - decoded GENERIC_ALL permissions + * - decoded more "well-known" and generic SIDs + * - showed Windows ownership in verbose situations + * - fixed apparent const violations + * + * Dec 2014, version 1.4.3 + * - fixed displaying "UserMapping" as a file name + * + * Mar 2015, version 1.4.5 + * - adapted to new NTFS ACLs when owner is same as group + * + * May 2015, version 1.4.6 + * - made to load shared library based on generic name + * + * Mar 2016, Version 1.5.0 + * - reorganized to rely on libntfs-3g even on Windows + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * General parameters which may have to be adapted to needs + */ + +#define AUDT_VERSION "1.5.0" + +#define SELFTESTS 0 +#define NOREVBOM 0 /* still unclear what this should be */ + +/* + * External declarations + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif /* HAVE_STDIO_H */ +#ifdef HAVE_STDLIB_H +#include +#endif /* HAVE_STDLIB_H */ +#ifdef HAVE_STRING_H +#include +#endif /* HAVE_STRING_H */ +#ifdef HAVE_UNISTD_H +#include +#endif /* HAVE_UNISTD_H */ +#ifdef HAVE_TIME_H +#include +#endif /* HAVE_TIME_H */ +#ifdef HAVE_GETOPT_H +#include +#endif /* HAVE_GETOPT_H */ +#ifdef HAVE_ERRNO_H +#include +#endif /* HAVE_ERRNO_H */ +#ifdef HAVE_FCNTL_H +#include +#endif /* HAVE_FCNTL_H */ +#ifdef HAVE_SYS_TYPES_H +#include +#endif /* HAVE_SYS_TYPES_H */ +#ifdef HAVE_SYS_STAT_H +#include +#endif /* HAVE_SYS_STAT_H */ +#ifdef HAVE_SETXATTR +#include +#else /* HAVE_SETXATTR */ +#warning "The extended attribute package is not available" +#endif /* HAVE_SETXATTR */ + +#include "types.h" +#include "endians.h" +#include "support.h" +#include "layout.h" +#include "param.h" +#include "ntfstime.h" +#include "device_io.h" +#include "device.h" +#include "logging.h" +#include "runlist.h" +#include "mft.h" +#include "inode.h" +#include "attrib.h" +#include "bitmap.h" +#include "index.h" +#include "volume.h" +#include "unistr.h" +#include "mst.h" +#include "security.h" +#include "acls.h" +#include "realpath.h" +#include "utils.h" +#include "misc.h" + +struct CALLBACK; + +typedef int (*dircallback)(void *context, const ntfschar *ntfsname, + const int length, const int type, const s64 pos, + const MFT_REF mft_ref, const unsigned int dt_type); + +#if POSIXACLS + +static BOOL same_posix(struct POSIX_SECURITY *pxdesc1, + struct POSIX_SECURITY *pxdesc2); + +#endif /* POSIXACLS */ + +#ifndef HAVE_SYSLOG_H +void ntfs_log_early_error(const char *format, ...) + __attribute__((format(printf, 1, 2))); +#endif /* HAVE_SYSLOG_H */ + +#define ACCOUNTSIZE 256 /* maximum size of an account name */ +#define MAXFILENAME 4096 +#define MAXATTRSZ 65536 /* Max sec attr size (16448 met for WinXP) */ +#define MAXLINE 80 /* maximum processed size of a line */ +#define BUFSZ 1024 /* buffer size to read mapping file */ +#define LINESZ 120 /* maximum useful size of a mapping line */ + +typedef enum { RECSHOW, RECSET, RECSETPOSIX } RECURSE; +typedef enum { MAPNONE, MAPEXTERN, MAPLOCAL, MAPDUMMY } MAPTYPE; +typedef enum { CMD_AUDIT, CMD_BACKUP, CMD_HEX, CMD_HELP, CMD_SET, + CMD_TEST, CMD_USERMAP, CMD_VERSION, CMD_NONE } CMDS; + + +#define MAXSECURID 262144 +#define SECBLKSZ 8 +#define MAPDIR ".NTFS-3G" +#define MAPFILE "UserMapping" + +#ifdef HAVE_WINDOWS_H +#define DIRSEP "\\" +#else +#define DIRSEP "/" +#endif + + /* standard owner (and administrator) rights */ + +#define OWNER_RIGHTS (DELETE | READ_CONTROL | WRITE_DAC | WRITE_OWNER \ + | SYNCHRONIZE \ + | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES \ + | FILE_READ_EA | FILE_WRITE_EA) + + /* standard world rights */ + +#define WORLD_RIGHTS (READ_CONTROL | FILE_READ_ATTRIBUTES | FILE_READ_EA \ + | SYNCHRONIZE) + + /* inheritance flags for files and directories */ + +#define FILE_INHERITANCE NO_PROPAGATE_INHERIT_ACE +#define DIR_INHERITANCE (OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE) + +/* + * To identify NTFS ACL meaning Posix ACL granted to root + * we use rights always granted to anybody, so they have no impact + * either on Windows or on Linux. + */ + +#define ROOT_OWNER_UNMARK SYNCHRONIZE /* ACL granted to root as owner */ +#define ROOT_GROUP_UNMARK FILE_READ_EA /* ACL granted to root as group */ + +#define INSDS1 1 +#define INSDS2 2 +#define INSII 4 +#define INSDH 8 + +struct SII { /* this is an image of an $SII index entry */ + le16 offs; + le16 size; + le32 fill1; + le16 indexsz; + le16 indexksz; + le16 flags; + le16 fill2; + le32 keysecurid; + + /* did not find official description for the following */ + le32 hash; + le32 securid; + le32 dataoffsl; /* documented as badly aligned */ + le32 dataoffsh; + le32 datasize; +} ; + +struct SDH { /* this is an image of an $SDH index entry */ + le16 offs; + le16 size; + le32 fill1; + le16 indexsz; + le16 indexksz; + le16 flags; + le16 fill2; + le32 keyhash; + le32 keysecurid; + + /* did not find official description for the following */ + le32 hash; + le32 securid; + le32 dataoffsl; + le32 dataoffsh; + le32 datasize; + le32 fill3; + } ; + +#ifdef HAVE_WINDOWS_H +/* + * Including leads to numerous conflicts with layout.h + * so define a few needed Windows calls unrelated to ntfs-3g + */ +BOOL WINAPI LookupAccountSidA(const char*, void*, char*, u32*, + char*, u32*, s32*); +u32 WINAPI GetFileAttributesA(const char*); +#endif /* HAVE_WINDOWS_H */ + +#define INVALID_FILE_ATTRIBUTES (-1)/* error from ntfs_get_file_attributes() */ + +/* + * Structures for collecting directory contents + */ + +struct LINK { + struct LINK *next; + char name[1]; +} ; + +struct CALLBACK { + struct LINK *head; + const char *dir; +} ; + +static int callback(void *context, const ntfschar *ntfsname, + const int length, const int type, const s64 pos, + const MFT_REF mft_ref, const unsigned int dt_type); + +struct SECURITY_DATA { + u64 offset; + char *attr; + u32 hash; + u32 length; + unsigned int filecount:16; + unsigned int mode:12; + unsigned int flags:4; +} ; + +/* + * Global constants + */ + +#define BANNER "ntfssecaudit " AUDT_VERSION " : NTFS security data auditing" + +#ifdef SELFTESTS + +/* + * Dummy mapping file (self tests only) + */ + +#define DUMMYAUTH "S-1-5-21-3141592653-589793238-462843383-" +char dummymapping[] = "500::" DUMMYAUTH "1000\n" + "501::" DUMMYAUTH "1001\n" + "502::" DUMMYAUTH "1002\n" + "503::" DUMMYAUTH "1003\n" + "516::" DUMMYAUTH "1016\n" + ":500:" DUMMYAUTH "513\r\n" + ":511:S-1-5-21-1607551490-981732888-1819828000-513\n" + ":516:" DUMMYAUTH "1012\r\n" + "::" DUMMYAUTH "10000\n"; + +/* + * SID for authenticated user (S-1-5-11) + */ + +static const char authsidbytes[] = { + 1, /* revision */ + 1, /* auth count */ + 0, 0, 0, 0, 0, 5, /* base */ + 11, 0, 0, 0 /* 1st level */ +}; + +static const SID *authsid = (const SID*)authsidbytes; + +/* + * SID for local users (S-1-5-32-545) + */ + +static const char localsidbytes[] = { + 1, /* revision */ + 2, /* auth count */ + 0, 0, 0, 0, 0, 5, /* base */ + 32, 0, 0, 0, /* 1st level */ + 33, 2, 0, 0 /* 2nd level */ +}; + +static const SID *localsid = (const SID*)localsidbytes; + +/* + * SID for system (S-1-5-18) + */ + +static const char systemsidbytes[] = { + 1, /* revision */ + 1, /* auth count */ + 0, 0, 0, 0, 0, 5, /* base */ + 18, 0, 0, 0 /* 1st level */ + }; + +static const SID *systemsid = (const SID*)systemsidbytes; + +#endif /* SELFTESTS */ + +/* + * Global variables + */ + +BOOL opt_e; /* restore extra (currently windows attribs) */ +BOOL opt_r; /* recursively apply to subdirectories */ +BOOL opt_u; /* user mapping proposal */ +int opt_v; /* verbose or very verbose*/ +CMDS cmd; /* command to process */ +struct SECURITY_DATA *securdata[(MAXSECURID + (1 << SECBLKSZ) - 1)/(1 << SECBLKSZ)]; +unsigned int errors; /* number of severe errors */ +unsigned int warnings; /* number of non-severe errors */ + +struct SECURITY_CONTEXT context; +MAPTYPE mappingtype; +struct SECURITY_API *ntfs_context = (struct SECURITY_API*)NULL; + +/* + * Open and close the security API (obsolete) + */ + +static BOOL open_security_api(void) +{ + return (TRUE); +} + +static BOOL close_security_api(void) +{ + return (0); +} + +/* + * Open and close a volume + * Assumes a single volume is opened + */ + +static BOOL open_volume(const char *volume, unsigned long flags) +{ + BOOL ok; + + ok = FALSE; + if (!ntfs_context) { + ntfs_context = ntfs_initialize_file_security(volume, flags); + if (ntfs_context) { + if (*(u32*)ntfs_context != MAGIC_API) { + fprintf(stderr,"Versions of ntfs-3g and ntfssecaudit" + " are not compatible\n"); + } else { + fprintf(stderr,"\"%s\" opened %s\n",volume, + (flags & NTFS_MNT_RDONLY + ? "read-only" : "read-write")); + mappingtype = MAPEXTERN; + ok = TRUE; + } + } else { + fprintf(stderr,"Could not open \"%s\"\n",volume); +#ifdef HAVE_WINDOWS_H + switch (errno) { + case EACCES : + fprintf(stderr,"You need Administrator rights to open \"%s\"\n", + volume); + break; + case EBUSY : + fprintf(stderr,"Looks like \"%s\" is mounted,\n",volume); + fprintf(stderr,"close all applications using it\n"); + break; + default : + fprintf(stderr,"Close all applications using %s\n", volume); + fprintf(stderr,"to make sure it is not mounted\n"); + } +#else + fprintf(stderr,"Make sure \"%s\" is not mounted\n",volume); +#endif + } + } else + fprintf(stderr,"A volume is already open\n"); + return (ok); +} + +static BOOL close_volume(const char *volume) +{ + BOOL r; + + r = ntfs_leave_file_security(ntfs_context); + if (r) + fprintf(stderr,"\"%s\" closed\n",volume); + else + fprintf(stderr,"Could not close \"%s\"\n",volume); + ntfs_context = (struct SECURITY_API*)NULL; + return (r); +} + +#ifdef HAVE_WINDOWS_H + +/* + * Make a path suitable for feeding to libntfs-3g + * + * Use '/' as a directory separator and remove /../ and /./ + */ + +static int cleanpath(char *path) +{ + int err; + char *p; + char *s, *d; + + err = 0; + for (p=path; *p; p++) + if (*p == '\\') + *p = '/'; + do { + s = (char*)NULL; + p = strstr(path, "/./"); + if (p) { + s = p + 3; + d = p + 1; + } else { + p = strstr(path, "/../"); + if (p) { + d = p; + while ((d != path) && (*--d != '/')) + d--; + if ((d != p) && (*d == '/')) { + s = p + 3; + } else + err = 1; + } + } + if (s) { + while (*s) + *d++ = *s++; + *d = 0; + } + } while (s && !err); + return (err); +} + +/* + * Build a path with Unix-type separators + * + * The path from the ntfs root is required for libntfs-3g calls + */ + +static char *unixname(const char *name) +{ + char *uname; + + uname = (char*)malloc(strlen(name) + 1); + if (uname) { + strcpy(uname, name); + if (cleanpath(uname)) { + fprintf(stderr,"Bad path %s\n",name); + free(uname); + uname = (char*)NULL; + } + } + return (uname); +} + +#endif /* HAVE_WINDOWS_H */ + +/* + * Extract small or big endian data from an array of bytes + */ + +static unsigned int get2l(const char *attr, int p) +{ + int i; + unsigned int v; + + v = 0; + for (i=0; i<2; i++) + v += (attr[p+i] & 255) << (8*i); + return (v); +} + +static unsigned long get4l(const char *attr, int p) +{ + int i; + unsigned long v; + + v = 0; + for (i=0; i<4; i++) + v += ((long)(attr[p+i] & 255)) << (8*i); + return (v); +} + +static u64 get6h(const char *attr, int p) +{ + int i; + u64 v; + + v = 0; + for (i=0; i<6; i++) + v = (v << 8) + (attr[p+i] & 255); + return (v); +} + +static u64 get8l(const char *attr, int p) +{ + int i; + u64 v; + + v = 0; + for (i=0; i<8; i++) + v += ((long long)(attr[p+i] & 255)) << (8*i); + return (v); +} + +/* + * Set small or big endian data into an array of bytes + */ + +static void set2l(char *p, unsigned int v) +{ + int i; + + for (i=0; i<2; i++) + p[i] = ((v >> 8*i) & 255); +} + +static void set4l(char *p, unsigned long v) +{ + int i; + + for (i=0; i<4; i++) + p[i] = ((v >> 8*i) & 255); +} + + +/* + * hexadecimal dump of an array of bytes + */ + +static void hexdump(const char *attr, int size, int level) +{ + int i,j; + + for (i=0; i> 29) & 7); + return (h); +} + +/* + * Evaluate the size of UTS-8 conversion of a UTF-16LE text + * trailing '\0' not accounted for + * Returns 0 for invalid input + */ + +static unsigned int utf8size(const ntfschar *utf16, int length) +{ + int i; + int count = 0; + BOOL surrog; + BOOL fail; + + surrog = FALSE; + fail = FALSE; + for (i = 0; i < length && utf16[i] && !fail; i++) { + unsigned short c = le16_to_cpu(utf16[i]); + if (surrog) { + if ((c >= 0xdc00) && (c < 0xe000)) { + surrog = FALSE; + count += 4; + } else + fail = TRUE; + } else + if (c < 0x80) + count++; + else if (c < 0x800) + count += 2; + else if (c < 0xd800) + count += 3; + else if (c < 0xdc00) + surrog = TRUE; +#if NOREVBOM + else if ((c >= 0xe000) && (c < 0xfffe)) +#else + else if (c >= 0xe000) +#endif + count += 3; + else + fail = TRUE; + } + if (surrog) + fail = TRUE; + + return (fail ? 0 : count); +} + +/* + * Convert a UTF-16LE text to UTF-8 + * Note : wcstombs() not used because on Linux it fails for characters + * not present in current locale + * Returns size or zero for invalid input + */ + +static unsigned int makeutf8(char *utf8, const ntfschar *utf16, int length) +{ + int size; + + size = ntfs_ucstombs(utf16, length, &utf8, MAXFILENAME); + return (size < 0 ? 0 : size); +} + +/* + * Print a file name + * on Windows it prints UTF-16LE names as UTF-8 + */ + +static void printname(FILE *file, const char *name) +{ +#ifdef HAVE_WINDOWS_H + char *wname; + char *p; + + wname = (char*)malloc(strlen(name) + 1); + if (wname) { + strcpy(wname, name); + for (p=wname; *p; p++) + if (*p == '/') + *p = '\\'; + fprintf(file,"%s", wname); + free(wname); + } +#else /* HAVE_WINDOWS_H */ + fprintf(file,"%s",name); +#endif /* HAVE_WINDOWS_H */ +} + +/* + * Print the last error code + */ + +static void printerror(FILE *file) +{ + if (errno) + fprintf(file,"Error code %d : %s\n",errno,strerror(errno)); + switch (errno) { + case EACCES : + fprintf(file,"You probably need Administrator rights\n"); + break; + case EBUSY : + fprintf(file,"You probably try to write to a mounted device\n"); + break; + default : + break; + } +} + +#ifndef HAVE_SYSLOG_H + +/* + * Redefine early error messages in stand-alone situations + */ + +static void ntfs_log_early_error(const char *format, ...) +{ + va_list args; + + va_start(args, format); + vfprintf(stderr,format,args); + va_end(args); +} + +#endif /* HAVE_SYSLOG_H */ + +/* + * Guess whether a security attribute is intended for a directory + * based on the presence of inheritable ACE + * (not 100% reliable) + */ + +static BOOL guess_dir(const char *attr) +{ + int off; + int isdir; + int cnt; + int i; + int x; + + isdir = 0; + off = get4l(attr,16); + if (off) { + cnt = get2l(attr,off+4); + x = 8; + for (i=0; i> 16) & 127); + if (rights & 0x10000) + printf("%*cDelete\n",-level-8,marker); + if (rights & 0x20000) + printf("%*cRead control\n",-level-8,marker); + if (rights & 0x40000) + printf("%*cWrite DAC\n",-level-8,marker); + if (rights & 0x80000) + printf("%*cWrite owner\n",-level-8,marker); + if (rights & 0x100000) + printf("%*cSynchronize\n",-level-8,marker); + if (rights & 0x800000) + printf("%*cCan access security ACL\n",-level-4,marker); + if (rights & 0x10000000) + printf("%*cGeneric all\n",-level-4,marker); + if (rights & 0x20000000) + printf("%*cGeneric execute\n",-level-4,marker); + if (rights & 0x40000000) + printf("%*cGeneric write\n",-level-4,marker); + if (rights & 0x80000000) + printf("%*cGeneric read\n",-level-4,marker); + + printf("%*cSID at 0x%x\n",-level,marker,off+8); + showsid(attr,off+8,"",level+4); + printf("%*cSummary :",-level,marker); + if (attr[off] == 0) + printf(" grant"); + if (attr[off] == 1) + printf(" deny"); + if (rights & le32_to_cpu(FILE_GREAD | FILE_GWRITE | FILE_GEXEC)) { + printf(" "); + if (rights & le32_to_cpu(FILE_GREAD)) + printf("r"); + if (rights & le32_to_cpu(FILE_GWRITE)) + printf("w"); + if (rights & le32_to_cpu(FILE_GEXEC)) + printf("x"); + } else + printf(" none"); + if (flags & 11) + printf(" inherited"); + if (!(flags & 8)) { + int sz; + + printf(" applied"); + sz = attr[off+9]*4 + 8; + if (!memcmp(&attr[off+8],&attr[get4l(attr,4)],sz)) + printf(" to owner"); + if (!memcmp(&attr[off+8],&attr[get4l(attr,8)],sz)) + printf(" to group"); + } + printf("\n"); + +} + +static void showacl(const char *attr, int off, int isdir, int level) +{ + int i; + int cnt; + int size; + int x; + char marker; + + if (cmd == CMD_BACKUP) + marker = '#'; + else + marker = ' '; + size = get2l(attr,off+2); + printf("%*crevision %d\n",-level,marker,attr[off]); + printf("%*cACL size %d\n",-level,marker,size); + cnt = get2l(attr,off+4); + printf("%*cACE cnt %d\n",-level,marker,cnt); + x = 8; + for (i=0; (iacccnt; + defcnt = pxdesc->defcnt; + firstdef = pxdesc->firstdef; + acl = &pxdesc->acl; + printf("Posix descriptor :\n"); + printf(" acccnt %d\n",acccnt); + printf(" defcnt %d\n",defcnt); + printf(" firstdef %d\n",firstdef); + printf(" mode : 0%03o\n",(int)pxdesc->mode); + printf(" tagsset : 0x%02x\n",(int)pxdesc->tagsset); + printf("Posix ACL :\n"); + printf(" version %d\n",(int)acl->version); + printf(" flags 0x%02x\n",(int)acl->flags); + for (k=0; k<(acccnt + defcnt); k++) { + if (k < acccnt) + l = k; + else + l = firstdef + k - acccnt; + pxace = &acl->ace[l]; + tag = pxace->tag; + perms = pxace->perms; + if (tag == POSIX_ACL_SPECIAL) { + txperm[0] = (perms & S_ISVTX ? 's' : '-'); + txperm[1] = (perms & S_ISUID ? 'u' : '-'); + txperm[2] = (perms & S_ISGID ? 'g' : '-'); + } else { + txperm[0] = (perms & 4 ? 'r' : '-'); + txperm[1] = (perms & 2 ? 'w' : '-'); + txperm[2] = (perms & 1 ? 'x' : '-'); + } + txperm[3] = 0; + if (k >= acccnt) + txtype = "default"; + else + txtype = "access "; + switch (tag) { + case POSIX_ACL_USER : + txtag = "USER "; + break; + case POSIX_ACL_USER_OBJ : + txtag = "USR-O"; + break; + case POSIX_ACL_GROUP : + txtag = "GROUP"; + break; + case POSIX_ACL_GROUP_OBJ : + txtag = "GRP-O"; + break; + case POSIX_ACL_MASK : + txtag = "MASK "; + break; + case POSIX_ACL_OTHER : + txtag = "OTHER"; + break; + case POSIX_ACL_SPECIAL : + txtag = "SPECL"; + break; + default : + txtag = "UNKWN"; + break; + } + id = pxace->id; + printf("ace %d : %s %s %4ld perms 0%03o %s\n", + l,txtype,txtag,(long)id, + perms,txperm); + } + } else + printf("** NULL ACL\n"); +} + +#endif /* POSIXACLS */ + +/* + * Relay to get uid as defined during mounting + */ + +static uid_t relay_find_user(const struct MAPPING *mapping __attribute__((unused)), + const SID *usid) +{ + int uid; + + uid = ntfs_get_user(ntfs_context, usid); + return (uid < 0 ? 0 : uid); +} + +/* + * Relay to get gid as defined during mounting + */ + +static gid_t relay_find_group(const struct MAPPING *mapping __attribute__((unused)), + const SID *gsid) +{ + int gid; + + gid = ntfs_get_group(ntfs_context, gsid); + return (gid < 0 ? 0 : gid); +} + +#if POSIXACLS + +static struct POSIX_SECURITY *linux_permissions_posix(const char *attr, BOOL isdir) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; +#if OWNERFROMACL + const SID *osid; +#endif /* OWNERFROMACL */ + const SID *usid; + const SID *gsid; + struct POSIX_SECURITY *posix_desc; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + gsid = (const SID*)&attr[le32_to_cpu(phead->group)]; +#if OWNERFROMACL + osid = (const SID*)&attr[le32_to_cpu(phead->owner)]; + usid = ntfs_acl_owner((const char*)attr); +#ifdef SELFTESTS + if ((cmd != CMD_TEST) && !ntfs_same_sid(usid,osid)) + printf("== Linux owner is different from Windows owner\n"); +#else /* SELFTESTS */ + if (!ntfs_same_sid(usid,osid)) + printf("== Linux owner is different from Windows owner\n"); +#endif /* SELFTESTS */ +#else /* OWNERFROMACL */ + usid = (const SID*)&attr[le32_to_cpu(phead->owner)]; +#endif /* OWNERFROMACL */ + if (mappingtype == MAPEXTERN) + posix_desc = ntfs_build_permissions_posix( + ntfs_context->security.mapping, + (const char*)attr, usid, gsid, isdir); + else + posix_desc = ntfs_build_permissions_posix(context.mapping, + (const char*)attr, usid, gsid, isdir); + if (!posix_desc) { + printf("** Failed to build a Posix descriptor\n"); + errors++; + } + return (posix_desc); +} + +#endif /* POSIXACLS */ + +static int linux_permissions(const char *attr, BOOL isdir) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; +#if OWNERFROMACL + const SID *osid; +#endif /* OWNERFROMACL */ + const SID *usid; + const SID *gsid; + int perm; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + gsid = (const SID*)&attr[le32_to_cpu(phead->group)]; +#if OWNERFROMACL + osid = (const SID*)&attr[le32_to_cpu(phead->owner)]; + usid = ntfs_acl_owner((const char*)attr); +#ifdef SELFTESTS + if ((cmd != CMD_TEST) && !ntfs_same_sid(usid,osid)) + printf("== Linux owner is different from Windows owner\n"); +#else /* SELFTESTS */ + if (!ntfs_same_sid(usid,osid)) + printf("== Linux owner is different from Windows owner\n"); +#endif /* SELFTESTS */ +#else /* OWNERFROMACL */ + usid = (const SID*)&attr[le32_to_cpu(phead->owner)]; +#endif /* OWNERFROMACL */ + perm = ntfs_build_permissions((const char*)attr, usid, gsid, isdir); + if (perm < 0) { + printf("** Failed to build permissions\n"); + errors++; + } + return (perm); +} + +static uid_t linux_owner(const char *attr) +{ + const SID *usid; + uid_t uid; + +#if OWNERFROMACL + usid = ntfs_acl_owner((const char*)attr); +#else /* OWNERFROMACL */ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + usid = (const SID*)&attr[le32_to_cpu(phead->owner)]; +#endif /* OWNERFROMACL */ +#ifdef HAVE_WINDOWS_H + uid = ntfs_find_user(context.mapping[MAPUSERS],usid); +#else /* defined(HAVE_WINDOWS_H) */ + if (mappingtype == MAPEXTERN) + uid = relay_find_user(context.mapping[MAPUSERS],usid); + else + uid = ntfs_find_user(context.mapping[MAPUSERS],usid); +#endif /* defined(HAVE_WINDOWS_H) */ + return (uid); +} + +static gid_t linux_group(const char *attr) +{ + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const SID *gsid; + gid_t gid; + + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + gsid = (const SID*)&attr[le32_to_cpu(phead->group)]; +#ifdef HAVE_WINDOWS_H + gid = ntfs_find_group(context.mapping[MAPGROUPS],gsid); +#else /* defined(HAVE_WINDOWS_H) */ + if (mappingtype == MAPEXTERN) + gid = relay_find_group(context.mapping[MAPGROUPS],gsid); + else + gid = ntfs_find_group(context.mapping[MAPGROUPS],gsid); +#endif /* defined(HAVE_WINDOWS_H) */ + return (gid); +} + +static void newblock(s32 key) +{ + struct SECURITY_DATA *psecurdata; + int i; + + if ((key > 0) && (key < MAXSECURID) && !securdata[key >> SECBLKSZ]) { + securdata[key >> SECBLKSZ] = + (struct SECURITY_DATA*)malloc((1 << SECBLKSZ)*sizeof(struct SECURITY_DATA)); + if (securdata[key >> SECBLKSZ]) + for (i=0; i<(1 << SECBLKSZ); i++) { + psecurdata = &securdata[key >> SECBLKSZ][i]; + psecurdata->filecount = 0; + psecurdata->mode = 0; + psecurdata->flags = 0; + psecurdata->attr = (char*)NULL; + } + } +} + +static void freeblocks(void) +{ + int i,j; + struct SECURITY_DATA *psecurdata; + + for (i=0; i<(MAXSECURID + (1 << SECBLKSZ) - 1)/(1 << SECBLKSZ); i++) + if (securdata[i]) { + for (j=0; j<(1 << SECBLKSZ); j++) { + psecurdata = &securdata[i][j]; + if (psecurdata->attr) + free(psecurdata->attr); + } + free(securdata[i]); + } +} + +/* + * Basic read from a user mapping file (Win32) + */ + +static int basicread(void *fileid, char *buf, size_t size, + off_t pos __attribute__((unused))) +{ + return (read(*(int*)fileid, buf, size)); +} + +#ifdef SELFTESTS + +/* + * Read a dummy mapping file for tests + */ + +static int dummyread(void *fileid __attribute__((unused)), + char *buf, size_t size, off_t pos) +{ + size_t sz; + + if (pos >= (off_t)(sizeof(dummymapping) - 1)) + sz = 0; + else + if ((size + pos) >= (sizeof(dummymapping) - 1)) + sz = sizeof(dummymapping) - 1 - pos; + else + sz = size; + if (sz > 0) + memcpy(buf,&dummymapping[pos],sz); + return (sz); +} + +#endif /* SELFTESTS */ + +/* + * Apply default single user mapping + * returns zero if successful + */ + +static int do_default_mapping(struct MAPPING *mapping[], + const SID *usid) +{ + struct MAPPING *usermapping; + struct MAPPING *groupmapping; + SID *sid; + int sidsz; + int res; + + res = -1; + sidsz = ntfs_sid_size(usid); + sid = (SID*)ntfs_malloc(sidsz); + if (sid) { + memcpy(sid,usid,sidsz); + usermapping = (struct MAPPING*)ntfs_malloc(sizeof(struct MAPPING)); + if (usermapping) { + groupmapping = (struct MAPPING*)ntfs_malloc(sizeof(struct MAPPING)); + if (groupmapping) { + usermapping->sid = sid; + usermapping->xid = 0; + usermapping->next = (struct MAPPING*)NULL; + groupmapping->sid = sid; + groupmapping->xid = 0; + groupmapping->next = (struct MAPPING*)NULL; + mapping[MAPUSERS] = usermapping; + mapping[MAPGROUPS] = groupmapping; + res = 0; + } + } + } + return (res); +} + +/* + * Build the user mapping + * - according to a mapping file if defined (or default present), + * - or try default single user mapping if possible + * + * The mapping is specific to a mounted device + * No locking done, mounting assumed non multithreaded + * + * returns zero if mapping is successful + * (failure should not be interpreted as an error) + */ + +static int local_build_mapping(struct MAPPING *mapping[], const char *usermap_path) +{ +#ifdef HAVE_WINDOWS_H + char mapfile[sizeof(MAPDIR) + sizeof(MAPFILE) + 6]; + char currpath[MAXFILENAME]; +#else /* HAVE_WINDOWS_H */ + char *mapfile; + char *p; +#endif /* HAVE_WINDOWS_H */ + int fd; + struct MAPLIST *item; + struct MAPLIST *firstitem = (struct MAPLIST*)NULL; + struct MAPPING *usermapping; + struct MAPPING *groupmapping; + static struct { + u8 revision; + u8 levels; + be16 highbase; + be32 lowbase; + le32 level1; + le32 level2; + le32 level3; + le32 level4; + le32 level5; + } defmap = { + 1, 5, const_cpu_to_be16(0), const_cpu_to_be32(5), + const_cpu_to_le32(21), + const_cpu_to_le32(DEFSECAUTH1), const_cpu_to_le32(DEFSECAUTH2), + const_cpu_to_le32(DEFSECAUTH3), const_cpu_to_le32(DEFSECBASE) + } ; + + /* be sure not to map anything until done */ + mapping[MAPUSERS] = (struct MAPPING*)NULL; + mapping[MAPGROUPS] = (struct MAPPING*)NULL; + + if (usermap_path) { +#ifdef HAVE_WINDOWS_H +/* TODO : check whether the device can store acls */ + strcpy(mapfile,"x:\\" MAPDIR "\\" MAPFILE); + if (((const le16*)usermap_path)[1] == ':') + mapfile[0] = usermap_path[0]; + else { + getcwd(currpath,MAXFILENAME); + mapfile[0] = currpath[0]; + } + fd = open(mapfile,O_RDONLY); +#else /* HAVE_WINDOWS_H */ + fd = 0; + mapfile = (char*)malloc(MAXFILENAME); + if (mapfile) { + /* build a full path to locate the mapping file */ +/* + if ((usermap_path[0] != '/') + && getcwd(mapfile,MAXFILENAME)) { + strcat(mapfile,"/"); + strcat(mapfile,usermap_path); + } else + strcpy(mapfile,usermap_path); +*/ + p = ntfs_realpath(usermap_path, mapfile); + if (p) + p = strrchr(mapfile,'/'); + if (p) + do { + strcpy(p,"/" MAPDIR "/" MAPFILE); + fd = open(mapfile,O_RDONLY); + if (fd <= 0) { + *p = 0; + p = strrchr(mapfile,'/'); + if (p == mapfile) + p = (char*)NULL; + } + } while ((fd <= 0) && p); + free(mapfile); + if (!p) { + printf("** Could not find the user mapping file\n"); + if (usermap_path[0] != '/') + printf(" Retry with full path of file\n"); + errors++; + } + } +#endif /* HAVE_WINDOWS_H */ + if (fd > 0) { + firstitem = ntfs_read_mapping(basicread, (void*)&fd); + close(fd); + } + } else { +#ifdef SELFTESTS + firstitem = ntfs_read_mapping(dummyread, (void*)NULL); +#endif /* SELFTESTS */ + } + + if (firstitem) { + usermapping = ntfs_do_user_mapping(firstitem); + groupmapping = ntfs_do_group_mapping(firstitem); + if (usermapping && groupmapping) { + mapping[MAPUSERS] = usermapping; + mapping[MAPGROUPS] = groupmapping; + } else + ntfs_log_error("There were no valid user or no valid group\n"); + /* now we can free the memory copy of input text */ + /* and rely on internal representation */ + while (firstitem) { + item = firstitem->next; + free(firstitem); + firstitem = item; + } + } else { + do_default_mapping(mapping,(const SID*)&defmap); + } + if (mapping[MAPUSERS]) + mappingtype = MAPLOCAL; + return (!mapping[MAPUSERS]); +} + +/* + * Get an hexadecimal number (source with MSB first) + */ + +static u32 getmsbhex(const char *text) +{ + u32 v; + int b; + BOOL ok; + + v = 0; + ok = TRUE; + do { + b = *text++; + if ((b >= '0') && (b <= '9')) + v = (v << 4) + b - '0'; + else + if ((b >= 'a') && (b <= 'f')) + v = (v << 4) + b - 'a' + 10; + else + if ((b >= 'A') && (b <= 'F')) + v = (v << 4) + b - 'A' + 10; + else ok = FALSE; + } while (ok); + return (v); +} + + +/* + * Get an hexadecimal number (source with LSB first) + * An odd number of digits might yield a strange result + */ + +static u32 getlsbhex(const char *text) +{ + u32 v; + int b; + BOOL ok; + int pos; + + v = 0; + ok = TRUE; + pos = 0; + do { + b = *text++; + if ((b >= '0') && (b <= '9')) + v |= (u32)(b - '0') << (pos ^ 4); + else + if ((b >= 'a') && (b <= 'f')) + v |= (u32)(b - 'a' + 10) << (pos ^ 4); + else + if ((b >= 'A') && (b <= 'F')) + v |= (u32)(b - 'A' + 10) << (pos ^ 4); + else ok = FALSE; + pos += 4; + } while (ok); + return (v); +} + + +/* + * Check whether a line looks like an hex dump + */ + +static BOOL ishexdump(const char *line, int first, int lth) +{ + BOOL ok; + int i; + int b; + + ok = (first >= 0) && (lth >= (first + 16)); + for (i=0; ((first+i)= '0') && (b <= '9')) + || ((b >= 'a') && (b <= 'f')) + || ((b >= 'A') && (b <= 'F')); + } + return (ok); +} + + +/* + * Display security descriptors from a file + * This is typically to convert a verbose output to a very verbose one + */ + +static void showhex(FILE *fd) +{ + static char attr[MAXATTRSZ]; + char line[MAXLINE+1]; +#if POSIXACLS + struct POSIX_SECURITY *pxdesc; +#endif /* POSIXACLS */ + int lth; + int first; + unsigned int pos; + u32 v; + int c; + int isdir; + int mode; + unsigned int off; + int i; + le32 *pattr; + BOOL acceptable; + BOOL isdump; + BOOL done; + + pos = 0; + off = 0; + done = FALSE; + do { + /* input a (partial) line without displaying */ + lth = 0; + first = -1; + do { + c = getc(fd); + if ((c != ' ') && (first < 0)) + first = lth; + if (c == EOF) + done = TRUE; + else + if (c != '\r') + line[lth++] = c; + } while (!done && (c != '\n') && (lth < MAXLINE)); + /* check whether this looks like an hexadecimal dump */ + isdump = ishexdump(line, first, lth); + if (isdump) off = getmsbhex(&line[first]); + /* line is not an hexadecimal dump */ + /* display what we have in store if acceptable */ + acceptable = ((!isdump || !off) + && (pos >= 20)) + && (pos > get4l(attr,4)) + && (pos > get4l(attr,8)) + && (pos > get4l(attr,12)) + && (pos > get4l(attr,16)) + && (pos >= ntfs_attr_size(attr)); + if (acceptable) { + printf(" Computed hash : 0x%08lx\n", + (unsigned long)hash((le32*)attr, + ntfs_attr_size(attr))); + isdir = guess_dir(attr); + printf(" Estimated type : %s\n", + (isdir ? "directory" : "file")); + if (!ntfs_valid_descr((char*)attr,pos)) { + printf("** Bad descriptor," + " trying to display anyway\n"); + errors++; + } + showheader(attr,4); + showusid(attr,4); + showgsid(attr,4); + showdacl(attr,isdir,4); + showsacl(attr,isdir,4); + showownership(attr); + mode = linux_permissions(attr,isdir); + printf("Interpreted Unix mode 0%03o\n",mode); +#if POSIXACLS + /* + * Posix display not possible when user + * mapping is not available (option -h) + */ + if (mappingtype != MAPNONE) { + pxdesc = linux_permissions_posix(attr,isdir); + if (pxdesc) { + showposix(pxdesc); + free(pxdesc); + } + } +#endif /* POSIXACLS */ + pos = 0; + } + if (isdump && !off) + pos = off; + /* line looks like an hexadecimal dump */ + /* decode it into attribute */ + if (isdump && (off == pos)) { + for (i=first+8; i 0) && (key < MAXSECURID)) { + if (!securdata[key >> SECBLKSZ]) + newblock(key); + if (securdata[key >> SECBLKSZ]) { + psecurdata = &securdata[key >> SECBLKSZ] + [key & ((1 << SECBLKSZ) - 1)]; + } + } + + /* If we have a usable attrib value. Try applying */ + badattrib = FALSE; + if (opt_e && (attrib != INVALID_FILE_ATTRIBUTES)) { + badattrib = !ntfs_set_file_attributes(ntfs_context, fullname, attrib); + if (badattrib) { + printf("** Could not set Windows attrib of "); + printname(stdout,fullname); + printf(" to 0x%x\n", attrib); + printerror(stdout); + warnings++; + } + } + + if (withattr) { + if (psecurdata) { + newattr = (char*)malloc(ntfs_attr_size(attr)); + if (newattr) { + memcpy(newattr,attr,ntfs_attr_size(attr)); + psecurdata->attr = newattr; + } + } + curattr = attr; + } else + /* + * No explicit attr in backup, use attr defined + * previously for the same id + */ + if (psecurdata) + curattr = psecurdata->attr; + + + if (curattr) { + selection = OWNER_SECURITY_INFORMATION + | GROUP_SECURITY_INFORMATION + | DACL_SECURITY_INFORMATION + | SACL_SECURITY_INFORMATION; + bad = !ntfs_set_file_security(ntfs_context,fullname, + selection, (const char*)curattr); + if (bad) { + printf("** Could not set the ACL of "); + printname(stdout,fullname); + printf("\n"); + printerror(stdout); + err = TRUE; + } else + if (opt_v) { + if (opt_e && !badattrib) + printf("ACL and attrib have been applied to "); + else + printf("ACL has been applied to "); + printname(stdout,fullname); + printf("\n"); + + } + } else { + printf("** There was no valid ACL for "); + printname(stdout,fullname); + printf("\n"); + err = TRUE; + } + return (!err); +} + +/* + * Restore security descriptors from a file + */ + +static BOOL restore(FILE *fd) +{ + static char attr[MAXATTRSZ]; + char line[MAXFILENAME+25]; + char fullname[MAXFILENAME+25]; + SECURITY_DESCRIPTOR_RELATIVE *phead; + int lth; + int first; + unsigned int pos; + int c; + int isdir; + int mode; + s32 key; + BOOL isdump; + unsigned int off; + u32 v; + u32 oldhash; + int i; + int count; + int attrib; + le32 *pattr; + BOOL withattr; + BOOL done; + + pos = 0; + off = 0; + done = FALSE; + withattr = FALSE; + oldhash = 0; + key = 0; + errors = 0; + count = 0; + fullname[0] = 0; + attrib = INVALID_FILE_ATTRIBUTES; + do { + /* input a (partial) line without processing */ + lth = 0; + first = -1; + do { + c = getc(fd); + if ((c != ' ') && (first < 0)) + first = lth; + if (c == EOF) + done = TRUE; + else + if (c != '\r') + line[lth++] = c; + } while (!done && (c != '\n') && (lth < (MAXFILENAME + 24))); + /* check whether this looks like an hexadecimal dump */ + isdump = ishexdump(line, first, lth); + if (isdump) off = getmsbhex(&line[first]); + /* line is not an hexadecimal dump */ + /* apply what we have in store, only if valid */ + if ((!isdump || !off) && pos && ntfs_valid_descr((char*)attr,pos)) { + withattr = TRUE; + if (opt_v >= 2) { + printf(" Computed hash : 0x%08lx\n", + (unsigned long)hash((le32*)attr, + ntfs_attr_size(attr))); + isdir = guess_dir(attr); + printf(" Estimated type : %s\n",(isdir ? "directory" : "file")); + showheader(attr,4); + showusid(attr,4); + showgsid(attr,4); + showdacl(attr,isdir,4); + showsacl(attr,isdir,4); + mode = linux_permissions(attr,isdir); + showownership(attr); + printf("Interpreted Unix mode 0%03o\n",mode); + } + pos = 0; + } + if (isdump && !off) + pos = off; + /* line looks like an hexadecimal dump */ + /* decode it into attribute */ + if (isdump && (off == pos)) { + for (i=first+8; i 0) + && ((line[lth-1] == '\n') || (line[lth-1] == '\r'))) + line[--lth] = 0; + if (!strncmp(line,"Computed hash : 0x",18)) + oldhash = getmsbhex(&line[18]); + if (!strncmp(line,"Security key : 0x",17)) + key = getmsbhex(&line[17]); + if (!strncmp(line,"Windows attrib : 0x",19)) + attrib = getmsbhex(&line[19]); + if (done + || !strncmp(line,"File ",5) + || !strncmp(line,"Directory ",10)) { + /* + * New file or directory (or end of file) : + * apply attribute just collected + * or apply attribute defined from current key + */ + + if (withattr + && oldhash + && (hash((const le32*)attr,ntfs_attr_size(attr)) != oldhash)) { + printf("** ACL rejected, its hash is not as expected\n"); + errors++; + } else + if (fullname[0]) { + phead = (SECURITY_DESCRIPTOR_RELATIVE*)attr; + /* set the request for auto-inheritance */ + if (phead->control & SE_DACL_AUTO_INHERITED) + phead->control |= SE_DACL_AUTO_INHERIT_REQ; + if (!applyattr(fullname,attr,withattr, + attrib,key)) + errors++; + else + count++; + } + /* save current file or directory name */ + withattr = FALSE; + key = 0; + oldhash = 0; + attrib = INVALID_FILE_ATTRIBUTES; + if (!done) { + if (!strncmp(line,"File ",5)) + strcpy(fullname,&line[5]); + else + strcpy(fullname,&line[10]); +#ifdef HAVE_WINDOWS_H + cleanpath(fullname); +#endif /* HAVE_WINDOWS_H */ + } + } + } while (!done); + printf("%d ACLs have been applied\n",count); + return (FALSE); +} + +static BOOL dorestore(const char *volume, FILE *fd) +{ + BOOL err; + + err = FALSE; + if (!getuid()) { + if (open_security_api()) { + if (open_volume(volume,NTFS_MNT_NONE)) { + if (restore(fd)) err = TRUE; + close_volume(volume); + } else { + fprintf(stderr,"Could not open volume %s\n",volume); + printerror(stderr); + err = TRUE; + } + close_security_api(); + } else { + fprintf(stderr,"Could not open security API\n"); + printerror(stderr); + err = TRUE; + } + } else { + fprintf(stderr,"Restore is only possible as root\n"); + err = TRUE; + } + return (err); +} + +#if POSIXACLS + +/* + * Merge Posix ACL rights into an u32 (self test only) + * + * Result format : -----rwxrwxrwxrwxrwx---rwxrwxrwx + * U1 U2 G1 G2 M o g w + * + * Only two users (U1, U2) and two groups (G1, G2) taken into account + */ +static u32 merge_rights(const struct POSIX_SECURITY *pxdesc, BOOL def) +{ + const struct POSIX_ACE *pxace; + int i; + int users; + int groups; + int first; + int last; + u32 rights; + + rights = 0; + users = 0; + groups = 0; + if (def) { + first = pxdesc->firstdef; + last = pxdesc->firstdef + pxdesc->defcnt - 1; + } else { + first = 0; + last = pxdesc->acccnt - 1; + } + pxace = pxdesc->acl.ace; + for (i=first; i<=last; i++) { + switch (pxace[i].tag) { + case POSIX_ACL_USER_OBJ : + rights |= (pxace[i].perms & 7) << 6; + break; + case POSIX_ACL_USER : + if (users < 2) + rights |= ((u32)pxace[i].perms & 7) << (24 - 3*users); + users++; + break; + case POSIX_ACL_GROUP_OBJ : + rights |= (pxace[i].perms & 7) << 3; + break; + case POSIX_ACL_GROUP : + if (groups < 2) + rights |= ((u32)pxace[i].perms & 7) << (18 - 3*groups); + groups++; + break; + case POSIX_ACL_MASK : + rights |= ((u32)pxace[i].perms & 7) << 12; + break; + case POSIX_ACL_OTHER : + rights |= (pxace[i].perms & 7); + break; + default : + break; + } + } + return (rights); +} + +static BOOL same_posix(struct POSIX_SECURITY *pxdesc1, + struct POSIX_SECURITY *pxdesc2) +{ + BOOL same; + int i; + + same = pxdesc1 + && pxdesc2 + && (pxdesc1->mode == pxdesc2->mode) + && (pxdesc1->acccnt == pxdesc2->acccnt) + && (pxdesc1->defcnt == pxdesc2->defcnt) + && (pxdesc1->firstdef == pxdesc2->firstdef) + && (pxdesc1->tagsset == pxdesc2->tagsset) + && (pxdesc1->acl.version == pxdesc2->acl.version) + && (pxdesc1->acl.flags == pxdesc2->acl.flags); + i = 0; + while (same && (i < pxdesc1->acccnt)) { + same = (pxdesc1->acl.ace[i].tag == pxdesc2->acl.ace[i].tag) + && (pxdesc1->acl.ace[i].perms == pxdesc2->acl.ace[i].perms) + && (pxdesc1->acl.ace[i].id == pxdesc2->acl.ace[i].id); + i++; + } + i = pxdesc1->firstdef; + while (same && (i < pxdesc1->firstdef + pxdesc1->defcnt)) { + same = (pxdesc1->acl.ace[i].tag == pxdesc2->acl.ace[i].tag) + && (pxdesc1->acl.ace[i].perms == pxdesc2->acl.ace[i].perms) + && (pxdesc1->acl.ace[i].id == pxdesc2->acl.ace[i].id); + i++; + } + return (same); +} + +#endif /* POSIXACLS */ + +#if POSIXACLS & SELFTESTS + +static void tryposix(struct POSIX_SECURITY *pxdesc) +{ + le32 owner_sid[] = /* S-1-5-21-3141592653-589793238-462843383-1016 */ + { + cpu_to_le32(0x501), cpu_to_le32(0x05000000), cpu_to_le32(21), + cpu_to_le32(DEFSECAUTH1), cpu_to_le32(DEFSECAUTH2), + cpu_to_le32(DEFSECAUTH3), cpu_to_le32(1016) + } ; + le32 group_sid[] = /* S-1-5-21-3141592653-589793238-462843383-513 */ + { + cpu_to_le32(0x501), cpu_to_le32(0x05000000), cpu_to_le32(21), + cpu_to_le32(DEFSECAUTH1), cpu_to_le32(DEFSECAUTH2), + cpu_to_le32(DEFSECAUTH3), cpu_to_le32(513) + } ; + + char *attr; + BOOL isdir; + mode_t mode; + struct POSIX_SECURITY *newpxdesc; + struct POSIX_SECURITY *oldpxdesc; + static char *oldattr = (char*)NULL; + + isdir = FALSE; + if (oldattr) { + oldpxdesc = linux_permissions_posix(oldattr, isdir); + newpxdesc = ntfs_merge_descr_posix(pxdesc, oldpxdesc); + if (!newpxdesc) + newpxdesc = pxdesc; + free(oldpxdesc); + if (opt_v) { + printf("merged descriptors :\n"); + showposix(newpxdesc); + } + } else + newpxdesc = pxdesc; + attr = ntfs_build_descr_posix(context.mapping,newpxdesc, + isdir,(SID*)owner_sid,(SID*)group_sid); + if (attr && ntfs_valid_descr(attr, ntfs_attr_size(attr))) { + if (opt_v) + hexdump(attr,ntfs_attr_size(attr),8); + if (opt_v >= 2) { + showheader(attr,4); + showusid(attr,4); + showgsid(attr,4); + showdacl(attr,isdir,4); + showsacl(attr,isdir,4); + mode = linux_permissions(attr,isdir); + printf("Interpreted Unix mode 0%03o\n",mode); + printf("Interpreted back Posix descriptor :\n"); + newpxdesc = linux_permissions_posix(attr,isdir); + showposix(newpxdesc); + free(newpxdesc); + } + if (oldattr) free(oldattr); + oldattr = attr; + } +} + +#endif /* POSIXACLS & SELFTESTS */ + +#ifdef SELFTESTS + +/* + * Build a dummy security descriptor + * returns descriptor in allocated memory, must free() after use + */ + +static char *build_dummy_descr(BOOL isdir __attribute__((unused)), + const SID *usid, const SID *gsid, + int cnt, + /* seq of int allow, SID *sid, int flags, u32 mask */ + ...) +{ + char *attr; + int attrsz; + SECURITY_DESCRIPTOR_RELATIVE *pnhead; + ACL *pacl; + ACCESS_ALLOWED_ACE *pace; + va_list ap; + const SID *sid; + u32 umask; + le32 mask; + int flags; + BOOL allow; + int pos; + int usidsz; + int gsidsz; + int sidsz; + int aclsz; + int i; + + if (usid) + usidsz = ntfs_sid_size(usid); + else + usidsz = 0; + if (gsid) + gsidsz = ntfs_sid_size(gsid); + else + gsidsz = 0; + + + /* allocate enough space for the new security attribute */ + attrsz = sizeof(SECURITY_DESCRIPTOR_RELATIVE) /* header */ + + usidsz + gsidsz /* usid and gsid */ + + sizeof(ACL) /* acl header */ + + cnt*40; + + attr = (char*)ntfs_malloc(attrsz); + if (attr) { + /* build the main header part */ + pnhead = (SECURITY_DESCRIPTOR_RELATIVE*) attr; + pnhead->revision = SECURITY_DESCRIPTOR_REVISION; + pnhead->alignment = 0; + /* + * The flag SE_DACL_PROTECTED prevents the ACL + * to be changed in an inheritance after creation + */ + pnhead->control = SE_DACL_PRESENT | SE_DACL_PROTECTED + | SE_SELF_RELATIVE; + /* + * Windows prefers ACL first, do the same to + * get the same hash value and avoid duplication + */ + /* build the ACL header */ + pos = sizeof(SECURITY_DESCRIPTOR_RELATIVE); + pacl = (ACL*)&attr[pos]; + pacl->revision = ACL_REVISION; + pacl->alignment1 = 0; + pacl->size = cpu_to_le16(0); /* fixed later */ + pacl->ace_count = cpu_to_le16(cnt); + pacl->alignment2 = cpu_to_le16(0); + + /* enter the ACEs */ + + pos += sizeof(ACL); + aclsz = sizeof(ACL); + va_start(ap,cnt); + for (i=0; itype = (allow ? ACCESS_ALLOWED_ACE_TYPE : ACCESS_DENIED_ACE_TYPE); + pace->flags = flags; + pace->size = cpu_to_le16(sidsz + 8); + pace->mask = mask; + memcpy(&pace->sid,sid,sidsz); + aclsz += sidsz + 8; + pos += sidsz + 8; + } + va_end(ap); + + /* append usid and gsid if defined */ + /* positions of ACL, USID and GSID into header */ + pnhead->owner = cpu_to_le32(0); + pnhead->group = cpu_to_le32(0); + if (usid) { + memcpy(&attr[pos], usid, usidsz); + pnhead->owner = cpu_to_le32(pos); + } + if (gsid) { + memcpy(&attr[pos + usidsz], gsid, gsidsz); + pnhead->group = cpu_to_le32(pos + usidsz); + } + /* positions of DACL and SACL into header */ + pnhead->sacl = cpu_to_le32(0); + if (cnt) { + pacl->size = cpu_to_le16(aclsz); + pnhead->dacl = + cpu_to_le32(sizeof(SECURITY_DESCRIPTOR_RELATIVE)); + } else + pnhead->dacl = cpu_to_le32(0); + if (!ntfs_valid_descr(attr,pos+usidsz+gsidsz)) { + printf("** Bad sample descriptor\n"); + free(attr); + attr = (char*)NULL; + errors++; + } + } else + errno = ENOMEM; + return (attr); +} + +/* + * Check a few samples with special conditions + */ + +static void check_samples(void) +{ + char *descr = (char*)NULL; + BOOL isdir = FALSE; + mode_t perms; + mode_t expect = 0; + int cnt; + u32 expectacc; + u32 expectdef; +#if POSIXACLS + u32 accrights; + u32 defrights; + mode_t mixmode; + struct POSIX_SECURITY *pxdesc; + struct POSIX_SECURITY *pxsample; + const char *txsample; +#endif /* POSIXACLS */ + le32 owner1[] = /* S-1-5-21-1833069642-4243175381-1340018762-1003 */ + { + cpu_to_le32(0x501), cpu_to_le32(0x05000000), cpu_to_le32(21), + cpu_to_le32(1833069642), cpu_to_le32(4243175381U), + cpu_to_le32(1340018762), cpu_to_le32(1003) + } ; + le32 group1[] = /* S-1-5-21-1833069642-4243175381-1340018762-513 */ + { + cpu_to_le32(0x501), cpu_to_le32(0x05000000), cpu_to_le32(21), + cpu_to_le32(1833069642), cpu_to_le32(4243175381U), + cpu_to_le32(1340018762), cpu_to_le32(513) + } ; + le32 group2[] = /* S-1-5-21-1607551490-981732888-1819828000-513 */ + { + cpu_to_le32(0x501), cpu_to_le32(0x05000000), cpu_to_le32(21), + cpu_to_le32(1607551490), cpu_to_le32(981732888), + cpu_to_le32(1819828000), cpu_to_le32(513) + } ; + le32 owner3[] = /* S-1-5-21-3141592653-589793238-462843383-1016 */ + { + cpu_to_le32(0x501), cpu_to_le32(0x05000000), cpu_to_le32(21), + cpu_to_le32(DEFSECAUTH1), cpu_to_le32(DEFSECAUTH2), + cpu_to_le32(DEFSECAUTH3), cpu_to_le32(1016) + } ; + le32 group3[] = /* S-1-5-21-3141592653-589793238-462843383-513 */ + { + cpu_to_le32(0x501), cpu_to_le32(0x05000000), cpu_to_le32(21), + cpu_to_le32(DEFSECAUTH1), cpu_to_le32(DEFSECAUTH2), + cpu_to_le32(DEFSECAUTH3), cpu_to_le32(513) + } ; + +#if POSIXACLS + struct { + struct POSIX_SECURITY head; + struct POSIX_ACE ace[4]; + } sampletry1 = + { + { 0645, 4, 0, 4, 0x35, 0, + { POSIX_VERSION, 0, 0 } + }, + { + { 1, 6, -1 }, + { 4, 5, -1 }, + { 16, 4, -1 }, + { 32, 5, -1 } + } + } ; + + struct { + struct POSIX_SECURITY head; + struct POSIX_ACE ace[6]; + } sampletry3 = + { + { 0100, 6, 0, 6, 0x3f, 0, + { POSIX_VERSION, 0, 0 } + }, + { + { 1, 1, -1 }, + { 2, 3, 1000 }, + { 4, 1, -1 }, + { 8, 3, 1002 }, + { 16, 0, -1 }, + { 32, 0, -1 } + } + } ; + + struct { + struct POSIX_SECURITY head; + struct POSIX_ACE ace[8]; + } sampletry4 = + { + { 0140, 8, 0, 8, 0x3f, 0, + { POSIX_VERSION, 0, 0 } + }, + { + { 1, 1, -1 }, + { 2, 3, 516 }, + { 2, 6, 1000 }, + { 4, 1, -1 }, + { 8, 6, 500 }, + { 8, 3, 1002 }, + { 16, 4, -1 }, + { 32, 0, -1 } + } + } ; + + struct { + struct POSIX_SECURITY head; + struct POSIX_ACE ace[6]; + } sampletry5 = + { + { 0454, 6, 0, 6, 0x3f, 0, + { POSIX_VERSION, 0, 0 } + }, + { + { 1, 4, -1 }, + { 2, 5, 516 }, + { 4, 4, -1 }, + { 8, 6, 500 }, + { 16, 5, -1 }, + { 32, 4, -1 } + } + } ; + + struct { + struct POSIX_SECURITY head; + struct POSIX_ACE ace[8]; + } sampletry6 = + { + { 0332, 8, 0, 8, 0x3f, 0, + { POSIX_VERSION, 0, 0 } + }, + { + { 1, 3, -1 }, + { 2, 1, 0 }, + { 2, 2, 1000 }, + { 4, 6, -1 }, + { 8, 4, 0 }, + { 8, 5, 1002 }, + { 16, 3, -1 }, + { 32, 2, -1 } + } + } ; + + struct { + struct POSIX_SECURITY head; + struct POSIX_ACE ace[4]; + } sampletry8 = + { + { 0677, 4, 0, 4, 0x35, 0, + { POSIX_VERSION, 0, 0 } + }, + { + { 1, 6, -1 }, + { 4, 7, -1 }, + { 16, 7, -1 }, + { 32, 7, -1 } + } + } ; + +#endif /* POSIXACLS */ + + +#if POSIXACLS + for (cnt=1; cnt<=8; cnt++) { + switch (cnt) { + case 1 : + pxsample = &sampletry1.head; + txsample = "sampletry1-a"; + isdir = FALSE; + descr = ntfs_build_descr_posix(context.mapping,&sampletry1.head, + isdir, (const SID*)owner3, (const SID*)group3); + break; + case 2 : + pxsample = &sampletry1.head; + txsample = "sampletry1-b"; + isdir = FALSE; + descr = ntfs_build_descr_posix(context.mapping,&sampletry1.head, + isdir, (const SID*)adminsid, (const SID*)group3); + break; + case 3 : + isdir = FALSE; + pxsample = &sampletry3.head; + txsample = "sampletry3"; + descr = ntfs_build_descr_posix(context.mapping,pxsample, + isdir, (const SID*)group3, (const SID*)group3); + break; + case 4 : + isdir = FALSE; + pxsample = &sampletry4.head; + txsample = "sampletry4"; + descr = ntfs_build_descr_posix(context.mapping,pxsample, + isdir, (const SID*)owner3, (const SID*)group3); + break; + case 5 : + isdir = FALSE; + pxsample = &sampletry5.head; + txsample = "sampletry5"; + descr = ntfs_build_descr_posix(context.mapping,pxsample, + isdir, (const SID*)owner3, (const SID*)group3); + break; + case 6 : + isdir = FALSE; + pxsample = &sampletry6.head; + txsample = "sampletry6-a"; + descr = ntfs_build_descr_posix(context.mapping,pxsample, + isdir, (const SID*)owner3, (const SID*)group3); + break; + case 7 : + isdir = FALSE; + pxsample = &sampletry6.head; + txsample = "sampletry6-b"; + descr = ntfs_build_descr_posix(context.mapping,pxsample, + isdir, (const SID*)adminsid, (const SID*)adminsid); + break; + case 8 : + pxsample = &sampletry8.head; + txsample = "sampletry8"; + isdir = FALSE; + descr = ntfs_build_descr_posix(context.mapping,&sampletry8.head, + isdir, (const SID*)owner3, (const SID*)group3); + break; + default : + pxsample = (struct POSIX_SECURITY*)NULL; + txsample = (const char*)NULL; + } + /* check we get original back */ + if (descr) + pxdesc = linux_permissions_posix(descr, isdir); + else + pxdesc = (struct POSIX_SECURITY*)NULL; + if (!descr || !pxdesc || !same_posix(pxsample,pxdesc)) { + printf("** Error in %s (errno %d)\n",txsample,errno); + showposix(pxsample); + if (descr) + showall(descr,0); + if (pxdesc) + showposix(pxdesc); + errors++; + } + free(descr); + free(pxdesc); + } + +#endif /* POSIXACLS */ + + + /* + * Check a few samples built by Windows, + * which cannot be generated by Linux + */ + + for (cnt=1; cnt<=10; cnt++) { + switch(cnt) { + case 1 : /* hp/tmp */ + isdir = TRUE; + descr = build_dummy_descr(isdir, + (const SID*)owner1, (const SID*)group1, + 1, + (int)TRUE, worldsid, (int)0x3, (u32)0x1f01ff); + expect = expectacc = 0777; + expectdef = 0; + break; + case 2 : /* swsetup */ + isdir = TRUE; + descr = build_dummy_descr(isdir, adminsid, (const SID*)group2, + 2, + (int)TRUE, worldsid, (int)0x0, (u32)0x1f01ff, + (int)TRUE, worldsid, (int)0xb, (u32)0x1f01ff); + expectacc = expect = 0777; + expectdef = 0777; + break; + case 3 : /* Dr Watson */ + isdir = TRUE; + descr = build_dummy_descr(isdir, (const SID*)owner3, (const SID*)group3, + 0); + expectacc = expect = 0700; + expectdef = 0; + break; + case 4 : + isdir = FALSE; + descr = build_dummy_descr(isdir, + (const SID*)owner3, (const SID*)group3, + 4, + (int)TRUE, (const SID*)owner3, 0, + le32_to_cpu(FILE_READ_DATA | OWNER_RIGHTS), + (int)TRUE, (const SID*)group3, 0, + le32_to_cpu(FILE_WRITE_DATA), + (int)TRUE, (const SID*)group2, 0, + le32_to_cpu(FILE_WRITE_DATA | FILE_READ_DATA), + (int)TRUE, (const SID*)worldsid, 0, + le32_to_cpu(FILE_EXECUTE)); + expect = 0731; + expectacc = 07070731; + expectdef = 0; + break; + case 5 : /* Vista/JP */ + isdir = TRUE; + descr = build_dummy_descr(isdir, systemsid, systemsid, + 6, + (int)TRUE, owner1, (int)0x0, (u32)0x1f01ff, + (int)TRUE, systemsid, (int)0x0, (u32)0x1f01ff, + (int)TRUE, adminsid, (int)0x0, (u32)0x1f01ff, + (int)TRUE, owner1, (int)0xb, (u32)0x10000000, + (int)TRUE, systemsid, (int)0xb, (u32)0x10000000, + (int)TRUE, adminsid, (int)0xb, (u32)0x10000000); + expectacc = expect = 0700; + expectdef = 0700; + break; + case 6 : /* Vista/JP2 */ + isdir = TRUE; + descr = build_dummy_descr(isdir, systemsid, systemsid, + 7, + (int)TRUE, owner1, (int)0x0, (u32)0x1f01ff, + (int)TRUE, systemsid, (int)0x0, (u32)0x1f01ff, + (int)TRUE, adminsid, (int)0x0, (u32)0x1f01ff, + (int)TRUE, owner1, (int)0xb, (u32)0x1f01ff, + (int)TRUE, systemsid, (int)0xb, (u32)0x1f01ff, + (int)TRUE, adminsid, (int)0xb, (u32)0x1f01ff, + (int)TRUE, owner3, (int)0x3, (u32)0x1200a9); + expectacc = 0500070700; + expectdef = expect = 0700; + break; + case 7 : /* WinXP/JP */ + isdir = TRUE; + descr = build_dummy_descr(isdir, adminsid, systemsid, + 6, + (int)TRUE, owner1, (int)0x0, (u32)0x1f01ff, + (int)TRUE, systemsid, (int)0x0, (u32)0x1f01ff, + (int)TRUE, adminsid, (int)0x0, (u32)0x1f01ff, + (int)TRUE, owner1, (int)0xb, (u32)0x10000000, + (int)TRUE, systemsid, (int)0xb, (u32)0x10000000, + (int)TRUE, adminsid, (int)0xb, (u32)0x10000000); + expectacc = expect = 0700; + expectdef = 0700; + break; + case 8 : /* WinXP/JP2 */ + isdir = TRUE; + descr = build_dummy_descr(isdir, adminsid, systemsid, + 6, + (int)TRUE, owner1, (int)0x0, (u32)0x1f01ff, + (int)TRUE, systemsid, (int)0x0, (u32)0x1f01ff, + (int)TRUE, adminsid, (int)0x0, (u32)0x1f01ff, + (int)TRUE, owner1, (int)0xb, (u32)0x10000000, + (int)TRUE, systemsid, (int)0xb, (u32)0x10000000, + (int)TRUE, adminsid, (int)0xb, (u32)0x10000000); + expectacc = expect = 0700; + expectdef = 0700; + break; + case 9 : /* Win8/bin */ + isdir = TRUE; + descr = build_dummy_descr(isdir, + (const SID*)owner3, (const SID*)owner3, + 6, + (int)TRUE, authsid, (int)0x3, (u32)0x1f01ff, + (int)TRUE, adminsid, (int)0x13, (u32)0x1f01ff, + (int)TRUE, systemsid, (int)0x13, (u32)0x1f01ff, + (int)TRUE, localsid, (int)0x13, (u32)0x1200a9, + (int)TRUE, authsid, (int)0x10, (u32)0x1301bf, + (int)TRUE, authsid, (int)0x1b, (u32)0xe0010000); + expectacc = expect = 0777; + expectdef = 0777; + break; + case 10 : /* Win8/bin/linem.exe */ + isdir = FALSE; + descr = build_dummy_descr(isdir, + (const SID*)owner3, (const SID*)owner3, + 4, + (int)TRUE, authsid, (int)0x10, (u32)0x1f01ff, + (int)TRUE, adminsid, (int)0x10, (u32)0x1f01ff, + (int)TRUE, systemsid, (int)0x10, (u32)0x1ff, + (int)TRUE, localsid, (int)0x10, (u32)0x1200a9); + expectacc = expect = 0777; + expectdef = 0; + break; + default : + expectacc = expectdef = 0; + break; + } + if (descr) { + perms = linux_permissions(descr, isdir); + if (perms != expect) { + printf("** Error in sample %d, perms 0%03o expected 0%03o\n", + cnt,perms,expect); + showall(descr,0); + errors++; + } else { +#if POSIXACLS + pxdesc = linux_permissions_posix(descr, isdir); + if (pxdesc) { + accrights = merge_rights(pxdesc,FALSE); + defrights = merge_rights(pxdesc,TRUE); + if (!(pxdesc->tagsset & ~(POSIX_ACL_USER_OBJ | POSIX_ACL_GROUP_OBJ | POSIX_ACL_OTHER))) + mixmode = expect; + else + mixmode = (expect & 07707) | ((accrights >> 9) & 070); + if ((pxdesc->mode != mixmode) + || (accrights != expectacc) + || (defrights != expectdef)) { + printf("** Error in sample %d : mode %03o expected 0%03o\n", + cnt,pxdesc->mode,mixmode); + printf(" Posix access rights 0%03lo expected 0%03lo\n", + (long)accrights,(long)expectacc); + printf(" default rights 0%03lo expected 0%03lo\n", + (long)defrights,(long)expectdef); + showall(descr,0); + showposix(pxdesc); + } + free(pxdesc); + } +#endif /* POSIXACLS */ + } + free(descr); + } + } +} + + +/* + * Check whether any basic permission setting is interpreted + * back exactly as set + */ + +static void basictest(int kind, BOOL isdir, const SID *owner, const SID *group) +{ + char *attr; + mode_t perm; + mode_t gotback; + u32 count; + u32 acecount; + u32 globhash; + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const ACL *pacl; + enum { ERRNO, + ERRMA, ERRPA, /* error converting mode or Posix ACL to NTFS */ + ERRAM, ERRAP, /* error converting NTFS to mode or Posix ACL */ + } err; + u32 expectcnt[] = { + 27800, 31896, + 24064, 28160, + 24064, 28160, + 24064, 28160, + 24904, 29000 + } ; + u32 expecthash[] = { + 0x8f80865b, 0x7bc7960, + 0x8fd9ecfe, 0xddd4db0, + 0xa8b07400, 0xa189c20, + 0xc5689a00, 0xb6c09000, + 0xb040e509, 0x4f4db7f7 + } ; +#if POSIXACLS + struct POSIX_SECURITY *pxdesc; + char *pxattr; + u32 pxcount; + u32 pxacecount; + u32 pxglobhash; +#endif /* POSIXACLS */ + + count = 0; + acecount = 0; + globhash = 0; +#if POSIXACLS + pxcount = 0; + pxacecount = 0; + pxglobhash = 0; +#endif /* POSIXACLS */ + for (perm=0; (perm<=07777) && (errors < 10); perm++) { + err = ERRNO; + /* file owned by plain user and group */ + attr = ntfs_build_descr(perm,isdir,owner,(const SID*)group); + if (attr && ntfs_valid_descr(attr, ntfs_attr_size(attr))) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + pacl = (const ACL*)&attr[le32_to_cpu(phead->dacl)]; + acecount += le16_to_cpu(pacl->ace_count); + globhash += hash((const le32*)attr,ntfs_attr_size(attr)); + count++; +#if POSIXACLS + /* + * Build a NTFS ACL from a mode, and + * decode to a Posix ACL, expecting to + * get the original mode back. + */ + pxdesc = linux_permissions_posix(attr, isdir); + if (!pxdesc || (pxdesc->mode != perm)) { + err = ERRAP; + if (pxdesc) + gotback = pxdesc->mode; + else + gotback = 0; + } else { + /* + * Build a NTFS ACL from the Posix ACL, expecting to + * get exactly the same NTFS ACL, then decode to a + * mode, expecting to get the original mode back. + */ + pxattr = ntfs_build_descr_posix(context.mapping, + pxdesc,isdir,owner, + (const SID*)group); + if (pxattr && !memcmp(pxattr,attr, + ntfs_attr_size(attr))) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + pacl = (const ACL*)&attr[le32_to_cpu(phead->dacl)]; + pxacecount += le16_to_cpu(pacl->ace_count); + pxglobhash += hash((const le32*)attr,ntfs_attr_size(attr)); + pxcount++; + gotback = linux_permissions(pxattr, isdir); + if (gotback != perm) + err = ERRAM; + else + free(pxattr); + } else + err = ERRPA; + free(attr); + } + free(pxdesc); +#else /* POSIXACLS */ + gotback = linux_permissions(attr, isdir); + if (gotback != perm) + err = ERRAM; + else + free(attr); +#endif /* POSIXACLS */ + } else + err = ERRMA; + + switch (err) { + case ERRMA : + printf("** no or wrong permission settings " + "for kind %d perm %03o\n",kind,perm); + if (attr && opt_v) + hexdump(attr,ntfs_attr_size(attr),8); + if (attr && (opt_v >= 2)) { + showheader(attr,4); + showusid(attr,4); + showgsid(attr,4); + showdacl(attr,isdir,4); + showsacl(attr,isdir,4); + } + errors++; + break; + case ERRPA : + printf("** no or wrong permission settings from PX " + "for kind %d perm %03o\n",kind,perm); + errors++; + break; +#if POSIXACLS + case ERRAM : + printf("** wrong permission settings, " + "kind %d perm 0%03o, gotback %03o\n", + kind, perm, gotback); + if (opt_v) + hexdump(pxattr,ntfs_attr_size(pxattr),8); + if (opt_v >= 2) { + showheader(pxattr,4); + showusid(pxattr,4); + showgsid(pxattr,4); + showdacl(pxattr,isdir,4); + showsacl(pxattr,isdir,4); + } + errors++; + break; + case ERRAP : + /* continued */ +#else /* POSIXACLS */ + case ERRAM : + case ERRAP : +#endif /* POSIXACLS */ + printf("** wrong permission settings, " + "kind %d perm 0%03o, gotback %03o\n", + kind, perm, gotback); + if (opt_v) + hexdump(attr,ntfs_attr_size(attr),8); + if (opt_v >= 2) { + showheader(attr,4); + showusid(attr,4); + showgsid(attr,4); + showdacl(attr,isdir,4); + showsacl(attr,isdir,4); + } + errors++; + free(attr); + break; + default : + break; + } + } + printf("%lu ACLs built from mode, %lu ACE built, mean count %lu.%02lu\n", + (unsigned long)count,(unsigned long)acecount, + (unsigned long)acecount/count,acecount*100L/count%100L); + if (acecount != expectcnt[kind]) { + printf("** Error : ACE count %lu instead of %lu\n", + (unsigned long)acecount, + (unsigned long)expectcnt[kind]); + errors++; + } + if (globhash != expecthash[kind]) { + printf("** Error : wrong global hash 0x%lx instead of 0x%lx\n", + (unsigned long)globhash, (unsigned long)expecthash[kind]); + errors++; + } +#if POSIXACLS + printf("%lu ACLs built from Posix ACLs, %lu ACE built, mean count %lu.%02lu\n", + (unsigned long)pxcount,(unsigned long)pxacecount, + (unsigned long)pxacecount/pxcount,pxacecount*100L/pxcount%100L); + if (pxacecount != expectcnt[kind]) { + printf("** Error : ACE count %lu instead of %lu\n", + (unsigned long)pxacecount, + (unsigned long)expectcnt[kind]); + errors++; + } + if (pxglobhash != expecthash[kind]) { + printf("** Error : wrong global hash 0x%lx instead of 0x%lx\n", + (unsigned long)pxglobhash, (unsigned long)expecthash[kind]); + errors++; + } +#endif /* POSIXACLS */ +} + +#if POSIXACLS + +/* + * Check whether Posix ACL settings are interpreted + * back exactly as set + */ + +static void posixtest(int kind, BOOL isdir, + const SID *owner, const SID *group) +{ + struct POSIX_SECURITY *pxdesc; + struct { + struct POSIX_SECURITY pxdesc; + struct POSIX_ACE aces[10]; + } desc; + int ownobj; + int grpobj; + int usr; + int grp; + int wrld; + int mask; + int mindes, maxdes; + int minmsk, maxmsk; + char *pxattr; + u32 count; + u32 acecount; + u32 globhash; + const SECURITY_DESCRIPTOR_RELATIVE *phead; + const ACL *pacl; + struct POSIX_SECURITY *gotback; + enum { ERRNO, + ERRMA, ERRPA, /* error converting mode or Posix ACL to NTFS */ + ERRAM, ERRAP, /* error converting NTFS to mode or Posix ACL */ + } ; + u32 expectcnt[] = { + 252720, 273456, + 199584, 220320, + 199584, 220320, + 199584, 220320, + 203904, 224640, + 0, 0, + 0, 0, + 0, 0, + 196452, 217188, + 165888, 186624, + 165888, 186624, + 165888, 186624, + 168480, 189216, + 0, 0, + 0, 0, + 0, 0, + 16368, 18672, + 0, 0, + 13824, 0, + 0, 0, + 14640, 0 + } ; + u32 expecthash[] = { + 0x1808a6cd, 0xd82f7c60, + 0x5ad29e85, 0x518c7620, + 0x188ce270, 0x7e44e590, + 0x48a64800, 0x5bdf0030, + 0x1c64aec6, 0x8b0168fa, + 0, 0, + 0, 0, + 0, 0, + 0x169fb80e, 0x382d9a59, + 0xf9c28164, 0x1855d352, + 0xf9685700, 0x44d16700, + 0x587ebe90, 0xf7c51480, + 0x2cb1b518, 0x52408df6, + 0, 0, + 0, 0, + 0, 0, + 0x905f2e38, 0xd40c22f0, + 0, 0, + 0xdd76da00, 0, + 0, 0, + 0x718e34a0, 0 + }; + + count = 0; + acecount = 0; + globhash = 0; + /* fill headers */ + pxdesc = &desc.pxdesc; + pxdesc->mode = 0; + pxdesc->defcnt = 0; + if (kind & 32) { + pxdesc->acccnt = 4; + pxdesc->firstdef = 4; + pxdesc->tagsset = 0x35; + } else { + pxdesc->acccnt = 6;; + pxdesc->firstdef = 6; + pxdesc->tagsset = 0x3f; + } + pxdesc->acl.version = POSIX_VERSION; + pxdesc->acl.flags = 0; + pxdesc->acl.filler = 0; + /* prefill aces */ + pxdesc->acl.ace[0].tag = POSIX_ACL_USER_OBJ; + pxdesc->acl.ace[0].id = -1; + if (kind & 32) { + pxdesc->acl.ace[1].tag = POSIX_ACL_GROUP_OBJ; + pxdesc->acl.ace[1].id = -1; + pxdesc->acl.ace[2].tag = POSIX_ACL_MASK; + pxdesc->acl.ace[2].id = -1; + pxdesc->acl.ace[3].tag = POSIX_ACL_OTHER; + pxdesc->acl.ace[3].id = -1; + } else { + pxdesc->acl.ace[1].tag = POSIX_ACL_USER; + pxdesc->acl.ace[1].id = (kind & 16 ? 0 : 1000); + pxdesc->acl.ace[2].tag = POSIX_ACL_GROUP_OBJ; + pxdesc->acl.ace[2].id = -1; + pxdesc->acl.ace[3].tag = POSIX_ACL_GROUP; + pxdesc->acl.ace[3].id = (kind & 16 ? 0 : 1002); + pxdesc->acl.ace[4].tag = POSIX_ACL_MASK; + pxdesc->acl.ace[4].id = -1; + pxdesc->acl.ace[5].tag = POSIX_ACL_OTHER; + pxdesc->acl.ace[5].id = -1; + } + + mindes = 3; + maxdes = (kind & 32 ? mindes : 6); + minmsk = 0; + maxmsk = 7; + for (mask=minmsk; mask<=maxmsk; mask++) + for (ownobj=1; ownobj<7; ownobj++) + for (grpobj=1; grpobj<7; grpobj++) + for (wrld=0; wrld<8; wrld++) + for (usr=mindes; usr<=maxdes; usr++) + if (usr != 4) + for (grp=mindes; grp<=maxdes; grp++) + if (grp != 4) { + pxdesc->mode = (ownobj << 6) | (mask << 3) | wrld; + + pxdesc->acl.ace[0].perms = ownobj; + if (kind & 32) { + pxdesc->acl.ace[1].perms = grpobj; + pxdesc->acl.ace[2].perms = mask; + pxdesc->acl.ace[3].perms = wrld; + } else { + pxdesc->acl.ace[1].perms = usr; + pxdesc->acl.ace[2].perms = grpobj; + pxdesc->acl.ace[3].perms = grp; + pxdesc->acl.ace[4].perms = mask; + pxdesc->acl.ace[5].perms = wrld; + } + + gotback = (struct POSIX_SECURITY*)NULL; + pxattr = ntfs_build_descr_posix(context.mapping, + pxdesc,isdir,owner,group); + if (pxattr && ntfs_valid_descr(pxattr, ntfs_attr_size(pxattr))) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)pxattr; + pacl = (const ACL*)&pxattr[le32_to_cpu(phead->dacl)]; + acecount += le16_to_cpu(pacl->ace_count); + globhash += hash((const le32*)pxattr,ntfs_attr_size(pxattr)); + count++; + gotback = linux_permissions_posix(pxattr, isdir); + if (gotback) { + if (ntfs_valid_posix(gotback)) { + if (!same_posix(pxdesc,gotback)) { + printf("Non matching got back Posix ACL\n"); + printf("input ACL\n"); + showposix(pxdesc); + printf("NTFS owner\n"); + showusid(pxattr,4); + printf("NTFS group\n"); + showgsid(pxattr,4); + printf("NTFS DACL\n"); + showdacl(pxattr,isdir,4); + printf("gotback ACL\n"); + showposix(gotback); + errors++; + } + } else { + printf("Got back an invalid Posix ACL\n"); + errors++; + } + free(gotback); + } else { + printf("Could not get Posix ACL back\n"); + errors++; + } + + } else { + printf("NTFS ACL incorrect or not build\n"); + printf("input ACL\n"); + showposix(pxdesc); + printf("NTFS DACL\n"); + if (pxattr) + showdacl(pxattr,isdir,4); + else + printf(" (none)\n"); + if (gotback) { + printf("gotback ACL\n"); + showposix(gotback); + } else + printf("no gotback ACL\n"); + errors++; + } + if (pxattr) + free(pxattr); + } + printf("%lu ACLs built from Posix ACLs, %lu ACE built, mean count %lu.%02lu\n", + (unsigned long)count,(unsigned long)acecount, + (unsigned long)acecount/count,acecount*100L/count%100L); + if (acecount != expectcnt[kind]) { + printf("** Error ! expected ACE count %lu\n", + (unsigned long)expectcnt[kind]); + errors++; + } + if (globhash != expecthash[kind]) { + printf("** Error : wrong global hash 0x%lx instead of 0x%lx\n", + (unsigned long)globhash, (unsigned long)expecthash[kind]); + errors++; + } +} + +#endif /* POSIXACLS */ + +static void selftests(void) +{ + le32 owner_sid[] = /* S-1-5-21-3141592653-589793238-462843383-1016 */ + { + cpu_to_le32(0x501), cpu_to_le32(0x05000000), cpu_to_le32(21), + cpu_to_le32(DEFSECAUTH1), cpu_to_le32(DEFSECAUTH2), + cpu_to_le32(DEFSECAUTH3), cpu_to_le32(1016) + } ; + le32 group_sid[] = /* S-1-5-21-3141592653-589793238-462843383-513 */ + { + cpu_to_le32(0x501), cpu_to_le32(0x05000000), cpu_to_le32(21), + cpu_to_le32(DEFSECAUTH1), cpu_to_le32(DEFSECAUTH2), + cpu_to_le32(DEFSECAUTH3), cpu_to_le32(513) + } ; +#if POSIXACLS + unsigned char kindlist[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 16, 17, 18, 20, 22, 24, 19, 21, 23, 25, + 32, 33, 36, 40 } ; + unsigned int k; +#endif /* POSIXACLS */ + int kind; + const SID *owner; + const SID *group; + BOOL isdir; + +#if POSIXACLS + local_build_mapping(context.mapping, (const char*)NULL); +#endif /* POSIXACLS */ + /* first check samples */ + mappingtype = MAPDUMMY; + check_samples(); + /* + * kind is oring of : + * 1 : directory + * 2 : owner is root + * 4 : group is root + * 8 : group is owner + * 16 : root is designated user/group + * 32 : mask present with no designated user/group + */ + for (kind=0; (kind<10) && (errors<10); kind++) { + isdir = kind & 1; + if (kind & 8) + owner = (const SID*)group_sid; + else + owner = (kind & 2 ? adminsid : (const SID*)owner_sid); + group = (kind & 4 ? adminsid : (const SID*)group_sid); + basictest(kind, isdir, owner, group); + } +#if POSIXACLS + for (k=0; (k= 10) + printf("** too many errors, test aborted\n"); +} +#endif /* SELFTESTS */ + +/* + * Get the security descriptor of a file + */ + +static unsigned int getfull(char *attr, const char *fullname) +{ + static char part[MAXATTRSZ]; + BIGSID ownsid; + int xowner; + int ownersz; + u16 ownerfl; + u32 attrsz; + u32 partsz; + BOOL overflow; + + attrsz = 0; + partsz = 0; + overflow = FALSE; + if (ntfs_get_file_security(ntfs_context,fullname, + OWNER_SECURITY_INFORMATION, + (char*)part,MAXATTRSZ,&partsz)) { + xowner = get4l(part,4); + if (xowner) { + ownerfl = get2l(part,2); + ownersz = ntfs_sid_size((SID*)&part[xowner]); + if (ownersz <= (int)sizeof(BIGSID)) + memcpy(ownsid,&part[xowner],ownersz); + else + overflow = TRUE; + } else { + ownerfl = 0; + ownersz = 0; + } + /* + * SACL : just feed in or clean + */ + if (!ntfs_get_file_security(ntfs_context,fullname, + SACL_SECURITY_INFORMATION, + (char*)attr,MAXATTRSZ,&attrsz)) { + attrsz = 20; + set4l(attr,0); + attr[0] = SECURITY_DESCRIPTOR_REVISION; + set4l(&attr[12],0); + if (opt_v >= 2) + printf(" No SACL\n"); + } + /* + * append DACL and merge its flags + */ + partsz = 0; + set4l(&attr[16],0); + if (ntfs_get_file_security(ntfs_context,fullname, + DACL_SECURITY_INFORMATION, + (char*)part,MAXATTRSZ,&partsz)) { + if ((attrsz + partsz - 20) <= MAXATTRSZ) { + memcpy(&attr[attrsz],&part[20],partsz-20); + set4l(&attr[16],(partsz > 20 ? attrsz : 0)); + set2l(&attr[2],get2l(attr,2) | (get2l(part,2) + & const_le16_to_cpu(SE_DACL_PROTECTED + | SE_DACL_AUTO_INHERITED + | SE_DACL_PRESENT))); + attrsz += partsz - 20; + } else + overflow = TRUE; + } else + if (partsz > MAXATTRSZ) + overflow = TRUE; + else { + if (cmd == CMD_BACKUP) + printf("# No discretionary access control list\n"); + else + printf(" No discretionary access control list\n"); + warnings++; + } + + /* + * append owner and merge its flag + */ + if (xowner && !overflow) { + memcpy(&attr[attrsz],ownsid,ownersz); + set4l(&attr[4],attrsz); + set2l(&attr[2],get2l(attr,2) + | (ownerfl & const_le16_to_cpu(SE_OWNER_DEFAULTED))); + attrsz += ownersz; + } else + set4l(&attr[4],0); + /* + * append group + */ + partsz = 0; + set4l(&attr[8],0); + if (ntfs_get_file_security(ntfs_context,fullname, + GROUP_SECURITY_INFORMATION, + (char*)part,MAXATTRSZ,&partsz)) { + if ((attrsz + partsz - 20) <= MAXATTRSZ) { + memcpy(&attr[attrsz],&part[20],partsz-20); + set4l(&attr[8],(partsz > 20 ? attrsz : 0)); + set2l(&attr[2],get2l(attr,2) | (get2l(part,2) + & const_le16_to_cpu(SE_GROUP_DEFAULTED))); + attrsz += partsz - 20; + } else + overflow = TRUE; + } else + if (partsz > MAXATTRSZ) + overflow = TRUE; + else { + printf("** No group SID\n"); + warnings++; + } + if (overflow) { + printf("** Descriptor was too long (> %d)\n",MAXATTRSZ); + warnings++; + attrsz = 0; + } else + if (!ntfs_valid_descr((char*)attr,attrsz)) { + printf("** Descriptor for %s is not valid\n",fullname); + errors++; + attrsz = 0; + } + + } else { + printf("** Could not get owner of %s\n",fullname); + warnings++; + attrsz = 0; + } + return (attrsz); +} + +/* + * Update a security descriptor + */ + +static BOOL updatefull(const char *name, u32 flags, char *attr) +{ + BOOL err; + +// Why was the error not seen before ? + err = !ntfs_set_file_security(ntfs_context, name, flags, attr); + if (err) { + printf("** Could not change attributes of %s\n",name); + printerror(stdout); + errors++; + } + return (!err); +} + + +#if POSIXACLS + +/* + * Set all the parameters associated to a file + */ + +static BOOL setfull_posix(const char *fullname, const struct POSIX_SECURITY *pxdesc, + BOOL isdir) +{ + static char attr[MAXATTRSZ]; + struct POSIX_SECURITY *oldpxdesc; + struct POSIX_SECURITY *newpxdesc; + const SECURITY_DESCRIPTOR_RELATIVE *phead; + char *newattr; + int err; + unsigned int attrsz; + int newattrsz; + const SID *usid; + const SID *gsid; +#if OWNERFROMACL + const SID *osid; +#endif /* OWNERFROMACL */ + + printf("%s ",(isdir ? "Directory" : "File")); + printname(stdout,fullname); + if (pxdesc->acccnt) + printf("\n"); + else + printf(" mode 0%03o\n",pxdesc->mode); + + err = FALSE; + attrsz = getfull(attr, fullname); + if (attrsz) { + oldpxdesc = linux_permissions_posix(attr, isdir); + if (opt_v >= 2) { + printf("Posix equivalent of old ACL :\n"); + showposix(oldpxdesc); + } + if (oldpxdesc) { + if (!pxdesc->defcnt + && !(pxdesc->tagsset & + (POSIX_ACL_USER | POSIX_ACL_GROUP | POSIX_ACL_MASK))) { + if (!ntfs_merge_mode_posix(oldpxdesc,pxdesc->mode)) + newpxdesc = oldpxdesc; + else { + newpxdesc = (struct POSIX_SECURITY*)NULL; + free(oldpxdesc); + } + } else { + newpxdesc = ntfs_merge_descr_posix(pxdesc, oldpxdesc); + free(oldpxdesc); + } + if (opt_v) { + printf("New Posix ACL :\n"); + showposix(newpxdesc); + } + } else + newpxdesc = (struct POSIX_SECURITY*)NULL; + if (newpxdesc) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + gsid = (const SID*)&attr[le32_to_cpu(phead->group)]; +#if OWNERFROMACL + osid = (const SID*)&attr[le32_to_cpu(phead->owner)]; + usid = ntfs_acl_owner((const char*)attr); + if (!ntfs_same_sid(usid,osid)) + printf("== Windows owner might change\n"); +#else /* OWNERFROMACL */ + usid = (const SID*)&attr[le32_to_cpu(phead->owner)]; +#endif /* OWNERFROMACL */ + if (mappingtype == MAPEXTERN) + newattr = ntfs_build_descr_posix( + ntfs_context->security.mapping, + newpxdesc,isdir,usid,gsid); + else + newattr = ntfs_build_descr_posix( + context.mapping, + newpxdesc,isdir,usid,gsid); + free(newpxdesc); + } else + newattr = (char*)NULL; + if (newattr) { + newattrsz = ntfs_attr_size(newattr); + if (opt_v) { + printf("New NTFS security descriptor\n"); + hexdump(newattr,newattrsz,4); + } + if (opt_v >= 2) { + printf("Expected hash : 0x%08lx\n", + (unsigned long)hash((le32*)newattr,ntfs_attr_size(newattr))); + showheader(newattr,0); + showusid(newattr,0); + showgsid(newattr,0); + showdacl(newattr,isdir,0); + showsacl(newattr,isdir,0); + } + + if (!updatefull(fullname, + DACL_SECURITY_INFORMATION + | GROUP_SECURITY_INFORMATION + | OWNER_SECURITY_INFORMATION, + newattr)) + err = TRUE; +/* +{ +struct POSIX_SECURITY *interp; +printf("Reinterpreted new Posix :\n"); +interp = linux_permissions_posix(newattr,isdir); +showposix(interp); +free(interp); +} +*/ + free(newattr); + } else + err = TRUE; + } else + err = TRUE; + return (!err); +} + +#else /* POSIXACLS */ + +static BOOL setfull(const char *fullname, int mode, BOOL isdir) +{ + static char attr[MAXATTRSZ]; + const SECURITY_DESCRIPTOR_RELATIVE *phead; + char *newattr; + int err; + unsigned int attrsz; + int newattrsz; + const SID *usid; + const SID *gsid; +#if OWNERFROMACL + const SID *osid; +#endif /* OWNERFROMACL */ + + printf("%s ",(isdir ? "Directory" : "File")); + printname(stdout,fullname); + printf(" mode 0%03o\n",mode); + attrsz = getfull(attr, fullname); + err = FALSE; + if (attrsz) { + phead = (const SECURITY_DESCRIPTOR_RELATIVE*)attr; + gsid = (const SID*)&attr[le32_to_cpu(phead->group)]; +#if OWNERFROMACL + osid = (const SID*)&attr[le32_to_cpu(phead->owner)]; + usid = ntfs_acl_owner((const char*)attr); + if (!ntfs_same_sid(usid,osid)) + printf("== Windows owner might change\n"); +#else /* OWNERFROMACL */ + usid = (const SID*)&attr[le32_to_cpu(phead->owner)]; +#endif /* OWNERFROMACL */ + newattr = ntfs_build_descr(mode,isdir,usid,gsid); + if (newattr) { + newattrsz = ntfs_attr_size(newattr); + if (opt_v) { + printf("Security descriptor\n"); + hexdump(newattr,newattrsz,4); + } + if (opt_v >= 2) { + printf("Expected hash : 0x%08lx\n", + (unsigned long)hash((le32*)newattr,ntfs_attr_size(newattr))); + showheader(newattr,0); + showusid(newattr,0); + showgsid(newattr,0); + showdacl(newattr,isdir,0); + showsacl(newattr,isdir,0); + } + + if (!updatefull(fullname, + DACL_SECURITY_INFORMATION + | GROUP_SECURITY_INFORMATION + | OWNER_SECURITY_INFORMATION, + newattr)) + err = TRUE; + free(newattr); + } + + } else + err = TRUE; + return (err); +} + +#endif /* POSIXACLS */ + +static BOOL proposal(const char *name, const char *attr) +{ + char fullname[MAXFILENAME]; + int uoff, goff; + int i; + u64 uauth, gauth; + int ucnt, gcnt; + int uid, gid; + BOOL err; +#ifdef HAVE_WINDOWS_H + char driveletter; +#else /* HAVE_WINDOWS_H */ + struct stat st; + char *p,*q; +#endif /* HAVE_WINDOWS_H */ + + err = FALSE; +#ifdef HAVE_WINDOWS_H + uid = gid = 0; +#else /* HAVE_WINDOWS_H */ + uid = getuid(); + gid = getgid(); +#endif /* HAVE_WINDOWS_H */ + uoff = get4l(attr,4); + uauth = get6h(attr,uoff+2); + ucnt = attr[uoff+1] & 255; + goff = get4l(attr,8); + gauth = get6h(attr,goff+2); + gcnt = attr[goff+1] & 255; + + if ((ucnt == 5) && (gcnt == 5) + && (uauth == 5) && (gauth == 5) + && (get4l(attr,uoff+8) == 21) && (get4l(attr,goff+8) == 21)) { + printf("# User mapping proposal :\n"); + printf("# -------------------- cut here -------------------\n"); + if (uid) + printf("%d::",uid); + else + printf("user::"); + printf("S-%d-%llu",attr[uoff] & 255,(long long)uauth); + for (i=0; i= 'a') && (name[0] <= 'z')) + || ((name[0] >= 'A') && (name[0] <= 'Z'))) + && (name[1] == ':')) + driveletter = name[0]; + else { + if (getcwd(fullname, MAXFILENAME) + && (fullname[1] == ':')) + driveletter = fullname[0]; + } + if (driveletter) { + printf("# Example : %c:\\.NTFS-3G\\UserMapping\n", + driveletter); + } +#else /* HAVE_WINDOWS_H */ + printf("# being a hidden subdirectory of the root of the NTFS file system.\n"); + + /* Get the path to the root of the file system */ +/* + if (name[0] != '/') { + p = getcwd(fullname,MAXFILENAME); + if (p) { + strcat(fullname,"/"); + strcat(fullname,name); + } + } else { + strcpy(fullname,name); + p = fullname; + } +*/ + p = ntfs_realpath(name, fullname); + if (p) { + /* go down the path to inode 5 */ + do { + lstat(fullname,&st); + q = strrchr(p,'/'); + if (q && (st.st_ino != 5)) + *q = 0; + } while (strchr(p,'/') && (st.st_ino != 5)); + } + if (p && (st.st_ino == 5)) { + printf("# Example : "); + printname(stdout,p); + printf("/.NTFS-3G/UserMapping\n"); + } +#endif /* HAVE_WINDOWS_H */ + } else { + printf("** Not possible : "); + printname(stdout,name); + printf(" was not created by a Windows user\n"); + err = TRUE; + } + return (err); +} + +/* + * Display all the parameters associated to a file + */ + +static void showfull(const char *fullname, BOOL isdir) +{ + static char attr[MAXATTRSZ]; + static char part[MAXATTRSZ]; +#if POSIXACLS + struct POSIX_SECURITY *pxdesc; +#endif /* POSIXACLS */ + struct SECURITY_DATA *psecurdata; + char *newattr; + int securindex; + int mode; + int level; + int attrib; + u32 attrsz; + u32 partsz; + uid_t uid; + gid_t gid; + + if (opt_v || (cmd == CMD_BACKUP)) { + printf("%s ",(isdir ? "Directory" : "File")); + printname(stdout, fullname); + printf("\n"); + } + + /* get individual parameters, as when trying to get them */ + /* all, and one (typically SACL) is missing, we get none */ + /* and concatenate them, to be able to compute the checksum */ + + partsz = 0; + securindex = ntfs_get_file_security(ntfs_context,fullname, + OWNER_SECURITY_INFORMATION, + (char*)part,MAXATTRSZ,&partsz); + + attrib = ntfs_get_file_attributes(ntfs_context, fullname); + if (attrib == INVALID_FILE_ATTRIBUTES) { + printf("** Could not get file attrib\n"); + errors++; + } + if ((securindex < 0) + || (securindex >= MAXSECURID) + || ((securindex > 0) + && ((!opt_r && (cmd != CMD_BACKUP)) + || !securdata[securindex >> SECBLKSZ] + || !securdata[securindex >> SECBLKSZ][securindex & ((1 << SECBLKSZ) - 1)].filecount))) + { + if (opt_v || (cmd == CMD_BACKUP)) { + if ((securindex < -1) || (securindex >= MAXSECURID)) + printf("Security key : 0x%x out of range\n",securindex); + else + if (securindex == -1) + printf("Security key : none\n"); + else + printf("Security key : 0x%x\n",securindex); + } else { + printf("%s ",(isdir ? "Directory" : "File")); + printname(stdout, fullname); + if ((securindex < -1) || (securindex >= MAXSECURID)) + printf(" : key 0x%x out of range\n",securindex); + else + if (securindex == -1) + printf(" : no key\n"); + else + printf(" : key 0x%x\n",securindex); + } + + attrsz = getfull(attr, fullname); + if (attrsz) { + psecurdata = (struct SECURITY_DATA*)NULL; + if ((securindex < MAXSECURID) && (securindex > 0)) { + if (!securdata[securindex >> SECBLKSZ]) + newblock(securindex); + if (securdata[securindex >> SECBLKSZ]) + psecurdata = &securdata[securindex >> SECBLKSZ] + [securindex & ((1 << SECBLKSZ) - 1)]; + } + if (((cmd == CMD_AUDIT) || (cmd == CMD_BACKUP)) + && opt_v && psecurdata) { + newattr = (char*)malloc(attrsz); + printf("# %s ",(isdir ? "Directory" : "File")); + printname(stdout, fullname); + printf(" hash 0x%lx\n", + (unsigned long)hash((le32*)attr,attrsz)); + if (newattr) { + memcpy(newattr,attr,attrsz); + psecurdata->attr = newattr; + } + } + if ((opt_v || (cmd == CMD_BACKUP)) + && ((securindex >= MAXSECURID) + || (securindex <= 0) + || !psecurdata + || (!psecurdata->filecount + && !psecurdata->flags))) { + hexdump(attr,attrsz,8); + printf("Computed hash : 0x%08lx\n", + (unsigned long)hash((le32*)attr,attrsz)); + } + if (ntfs_valid_descr((char*)attr,attrsz)) { +#if POSIXACLS + pxdesc = linux_permissions_posix(attr,isdir); + if (pxdesc) + mode = pxdesc->mode; + else + mode = 0; +#else /* POSIXACLS */ + mode = linux_permissions(attr,isdir); +#endif /* POSIXACLS */ + attrib = ntfs_get_file_attributes(ntfs_context,fullname); + if (opt_v >= 2) { + level = (cmd == CMD_BACKUP ? 4 : 0); + showheader(attr,level); + showusid(attr,level); + showgsid(attr,level); + showdacl(attr,isdir,level); + showsacl(attr,isdir,level); + } + if (attrib != INVALID_FILE_ATTRIBUTES) + printf("Windows attrib : 0x%x\n",attrib); + uid = linux_owner(attr); + gid = linux_group(attr); + if (cmd == CMD_BACKUP) { + showownership(attr); + printf("# Interpreted Unix owner %d, group %d, mode 0%03o\n", + (int)uid,(int)gid,mode); + } else { + showownership(attr); + printf("Interpreted Unix owner %d, group %d, mode 0%03o\n", + (int)uid,(int)gid,mode); + } +#if POSIXACLS + if (pxdesc) { + if ((cmd != CMD_BACKUP) + && (pxdesc->defcnt + || (pxdesc->tagsset + & (POSIX_ACL_USER + | POSIX_ACL_GROUP + | POSIX_ACL_MASK)))) + showposix(pxdesc); + free(pxdesc); + } +#endif /* POSIXACLS */ + if ((opt_r || (cmd == CMD_BACKUP)) + && (securindex < MAXSECURID) + && (securindex > 0) && psecurdata) { + psecurdata->filecount++; + psecurdata->mode = mode; + } + } else { + printf("** Descriptor fails sanity check\n"); + errors++; + } + } + } else + if (securindex > 0) { + if (securdata[securindex >> SECBLKSZ]) { + psecurdata = &securdata[securindex >> SECBLKSZ] + [securindex & ((1 << SECBLKSZ) - 1)]; + psecurdata->filecount++; + if ((cmd == CMD_BACKUP) || opt_r) { + if ((cmd != CMD_BACKUP) && !opt_v) { + printf("%s ",(isdir ? "Directory" : "File")); + printname(stdout,fullname); + printf("\n"); + } + printf("Security key : 0x%x mode %03o (already displayed)\n", + securindex,psecurdata->mode); + if (attrib != INVALID_FILE_ATTRIBUTES) + printf("Windows attrib : 0x%x\n",attrib); + } else { + printf("%s ",(isdir ? "Directory" : "File")); + printname(stdout,fullname); + printf(" : key 0x%x\n",securindex); + } + if (((cmd == CMD_AUDIT) || (cmd == CMD_BACKUP)) + && opt_v + && psecurdata + && psecurdata->attr) { + printf("# %s ",(isdir ? "Directory" : "File")); + printname(stdout,fullname); + printf(" hash 0x%lx\n", + (unsigned long)hash((le32*)psecurdata->attr, + ntfs_attr_size(psecurdata->attr))); + } + } + } else { + if (!opt_v && (cmd != CMD_BACKUP)) { + printf("%s ",(isdir ? "Directory" : "File")); + printname(stdout, fullname); + } + printf(" (Failed)\n"); + printf("** Could not get security data of "); + printname(stdout, fullname); + printf(", partsz %d\n", partsz); + printerror(stdout); + errors++; + } +} + +static BOOL recurseshow(const char *path) +{ + struct CALLBACK dircontext; + struct LINK *current; + BOOL isdir; + BOOL err; + + err = FALSE; + dircontext.head = (struct LINK*)NULL; + dircontext.dir = path; + isdir = ntfs_read_directory(ntfs_context, path, + callback, &dircontext); + if (isdir) { + showfull(path,TRUE); + if (opt_v) { + if (cmd == CMD_BACKUP) + printf("#\n#\n"); + else + printf("\n\n"); + } + while (dircontext.head) { + current = dircontext.head; + if (recurseshow(current->name)) err = TRUE; + dircontext.head = dircontext.head->next; + free(current); + } + } else + if (errno == ENOTDIR) { + showfull(path,FALSE); + if (opt_v) { + if (cmd == CMD_BACKUP) + printf("#\n#\n"); + else + printf("\n\n"); + } + } else { + printf("** Could not access %s\n",path); + printerror(stdout); + errors++; + err = TRUE; + } + return (!err); +} + + +static BOOL singleshow(const char *path) +{ + BOOL isdir; + BOOL err; + + err = FALSE; + isdir = ntfs_read_directory(ntfs_context, path, + callback, (struct CALLBACK*)NULL); + if (isdir || (errno == ENOTDIR)) + showfull(path,isdir); + else { + printf("** Could not access %s\n",path); + printerror(stdout); + errors++; + err = TRUE; + } + return (err); +} + +#ifndef HAVE_WINDOWS_H + +#ifdef HAVE_SETXATTR + +static ssize_t ntfs_getxattr(const char *path, const char *name, void *value, size_t size) +{ +#if defined(__APPLE__) || defined(__DARWIN__) + return getxattr(path, name, value, size, 0, 0); +#else /* defined(__APPLE__) || defined(__DARWIN__) */ + return getxattr(path, name, value, size); +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ +} + +/* + * Display all the parameters associated to a mounted file + * + * (Unix only) + */ + +static BOOL showmounted(const char *fullname) +{ + + static char attr[MAXATTRSZ]; + struct stat st; +#if POSIXACLS + struct POSIX_SECURITY *pxdesc; +#endif /* POSIXACLS */ + BOOL mapped; + int attrsz; + int mode; + uid_t uid; + gid_t gid; + u32 attrib; + int level; + BOOL isdir; + BOOL err; + + err = FALSE; + if (!stat(fullname,&st)) { + isdir = S_ISDIR(st.st_mode); + printf("%s ",(isdir ? "Directory" : "File")); + printname(stdout,fullname); + printf("\n"); + + attrsz = ntfs_getxattr(fullname,"system.ntfs_acl",attr,MAXATTRSZ); + if (attrsz > 0) { + if (opt_v) { + hexdump(attr,attrsz,8); + printf("Computed hash : 0x%08lx\n", + (unsigned long)hash((le32*)attr,attrsz)); + } + if (ntfs_getxattr(fullname,"system.ntfs_attrib",&attrib,4) != 4) { + printf("** Could not get file attrib\n"); + errors++; + } else + printf("Windows attrib : 0x%x\n",(int)attrib); + if (ntfs_valid_descr(attr,attrsz)) { + mapped = !local_build_mapping(context.mapping,fullname); +#if POSIXACLS + if (mapped) { + pxdesc = linux_permissions_posix(attr,isdir); + if (pxdesc) + mode = pxdesc->mode; + else + mode = 0; + } else { + pxdesc = (struct POSIX_SECURITY*)NULL; + mode = linux_permissions(attr,isdir); + printf("No user mapping : " + "cannot display the Posix ACL\n"); + } +#else /* POSIXACLS */ + mode = linux_permissions(attr,isdir); +#endif /* POSIXACLS */ + if (opt_v >= 2) { + level = (cmd == CMD_BACKUP ? 4 : 0); + showheader(attr,level); + showusid(attr,level); + showgsid(attr,level); + showdacl(attr,isdir,level); + showsacl(attr,isdir,level); + } + showownership(attr); + if (mapped) { + uid = linux_owner(attr); + gid = linux_group(attr); + printf("Interpreted Unix owner %d, group %d, mode 0%03o\n", + (int)uid,(int)gid,mode); + } else { + printf("Interpreted Unix mode 0%03o (owner and group are unmapped)\n", + mode); + } +#if POSIXACLS + if (pxdesc) { + if ((pxdesc->defcnt + || (pxdesc->tagsset + & (POSIX_ACL_USER + | POSIX_ACL_GROUP + | POSIX_ACL_MASK)))) + showposix(pxdesc); + free(pxdesc); + } + if (mapped) + ntfs_free_mapping(context.mapping); +#endif /* POSIXACLS */ + } else { + printf("Descriptor fails sanity check\n"); + errors++; + } + } else { + printf("** Could not get the NTFS ACL, check whether file is on NTFS\n"); + errors++; + } + } else { + printf("%s not found\n",fullname); + err = TRUE; + } + return (err); +} + +static BOOL processmounted(const char *fullname) +{ + + static char attr[MAXATTRSZ]; + struct stat st; + int attrsz; + BOOL err; + + err = FALSE; + if (cmd != CMD_USERMAP) + err = showmounted(fullname); + else + if (!stat(fullname,&st)) { + attrsz = ntfs_getxattr(fullname,"system.ntfs_acl",attr,MAXATTRSZ); + if (attrsz > 0) { + if (opt_v) { + hexdump(attr,attrsz,8); + printf("Computed hash : 0x%08lx\n", + (unsigned long)hash((le32*)attr,attrsz)); + } + if (ntfs_valid_descr(attr,attrsz)) { + err = proposal(fullname, attr); + } else { + printf("*** Descriptor fails sanity check\n"); + errors++; + } + } else { + printf("** Could not get the NTFS ACL, check whether file is on NTFS\n"); + errors++; + } + } else { + printf("%s not found\n",fullname); + err = TRUE; + } + return (err); +} + +#else /* HAVE_SETXATTR */ + +static BOOL processmounted(const char *fullname __attribute__((unused))) +{ + fprintf(stderr,"Not possible on this configuration,\n"); + fprintf(stderr,"you have to use an unmounted partition\n"); + return (TRUE); +} + +#endif /* HAVE_SETXATTR */ + +#endif /* HAVE_WINDOWS_H */ + +#if POSIXACLS + +static BOOL recurseset_posix(const char *path, const struct POSIX_SECURITY *pxdesc) +{ + struct CALLBACK dircontext; + struct LINK *current; + BOOL isdir; + BOOL err; + + err = FALSE; + dircontext.head = (struct LINK*)NULL; + dircontext.dir = path; + isdir = ntfs_read_directory(ntfs_context, path, + callback, &dircontext); + if (isdir) { + err = !setfull_posix(path,pxdesc,TRUE); + if (err) { + printf("** Failed to update %s\n",path); + printerror(stdout); + errors++; + } else { + if (cmd == CMD_BACKUP) + printf("#\n#\n"); + else + printf("\n\n"); + while (dircontext.head) { + current = dircontext.head; + recurseset_posix(current->name,pxdesc); + dircontext.head = dircontext.head->next; + free(current); + } + } + } else + if (errno == ENOTDIR) { + err = !setfull_posix(path,pxdesc,FALSE); + if (err) { + printf("** Failed to update %s\n",path); + printerror(stdout); + errors++; + } + } else { + printf("** Could not access %s\n",path); + printerror(stdout); + errors++; + err = TRUE; + } + return (!err); +} + +#else /* POSIXACLS */ + +static BOOL recurseset(const char *path, int mode) +{ + struct CALLBACK dircontext; + struct LINK *current; + BOOL isdir; + BOOL err; + + err = FALSE; + dircontext.head = (struct LINK*)NULL; + dircontext.dir = path; + isdir = ntfs_read_directory(ntfs_context, path, + callback, &dircontext); + if (isdir) { + setfull(path,mode,TRUE); + if (cmd == CMD_BACKUP) + printf("#\n#\n"); + else + printf("\n\n"); + while (dircontext.head) { + current = dircontext.head; + recurseset(current->name,mode); + dircontext.head = dircontext.head->next; + free(current); + } + } else + if (errno == ENOTDIR) + setfull(path,mode,FALSE); + else { + printf("** Could not access %s\n",path); + printerror(stdout); + errors++; + err = TRUE; + } + return (!err); +} + +#endif /* POSIXACLS */ + +#if POSIXACLS + +static BOOL singleset_posix(const char *path, const struct POSIX_SECURITY *pxdesc) +{ + BOOL isdir; + BOOL err; + + err = FALSE; + isdir = ntfs_read_directory(ntfs_context, path, + callback, (struct CALLBACK*)NULL); + if (isdir || (errno == ENOTDIR)) { + err = !setfull_posix(path,pxdesc,isdir); + if (err) { + printf("** Failed to update %s\n",path); + printerror(stdout); + errors++; + } + } else { + printf("** Could not access %s\n",path); + printerror(stdout); + errors++; + err = TRUE; + } + return (!err); +} + +#else /* POSIXACLS */ + +static BOOL singleset(const char *path, int mode) +{ + BOOL isdir; + BOOL err; + + err = FALSE; + isdir = ntfs_read_directory(ntfs_context, path, + callback, (struct CALLBACK*)NULL); + if (isdir || (errno == ENOTDIR)) + setfull(path,mode,isdir); + else { + printf("** Could not access %s\n",path); + printerror(stdout); + errors++; + err = TRUE; + } + return (!err); +} + +#endif /* POSIXACLS */ + +static int callback(void *ctx, const ntfschar *ntfsname, + const int length, const int type, + const s64 pos __attribute__((unused)), + const MFT_REF mft_ref __attribute__((unused)), + const unsigned int dt_type __attribute__((unused))) +{ + struct LINK *linkage; + struct CALLBACK *dircontext; + char *name; + int newlth; + int size; + + dircontext = (struct CALLBACK*)ctx; + size = utf8size(ntfsname,length); + if (dircontext + && (type != 2) /* 2 : dos name (8+3) */ + && (size > 0) /* chars convertible to utf8 */ + && ((length > 2) + || (ntfsname[0] != const_cpu_to_le16('.')) + || ((length > 1) + && (ntfsname[1] != const_cpu_to_le16('.'))))) { + linkage = (struct LINK*)malloc(sizeof(struct LINK) + + strlen(dircontext->dir) + + size + 2); + if (linkage) { + /* may find ".fuse_hidden*" files */ + /* recommendation is not to hide them, so that */ + /* the user has a clue to delete them */ + strcpy(linkage->name,dircontext->dir); + if (linkage->name[strlen(linkage->name) - 1] != '/') + strcat(linkage->name,"/"); + name = &linkage->name[strlen(linkage->name)]; + newlth = makeutf8(name,ntfsname,length); + name[newlth] = 0; + linkage->next = dircontext->head; + dircontext->head = linkage; + } + } + return (0); +} + +/* + * Backup security descriptors in a directory tree + */ + +static BOOL backup(const char *volume, const char *root) +{ + BOOL err; + int count; + int i,j; + time_t now; + const char *txtime; + + now = time((time_t*)NULL); + txtime = ctime(&now); + if (!getuid() && open_security_api()) { + if (open_volume(volume,NTFS_MNT_RDONLY)) { + printf("#\n# Recursive ACL collection on %s#\n",txtime); + err = recurseshow(root); + count = 0; + for (i=0; i<(MAXSECURID + (1 << SECBLKSZ) - 1)/(1 << SECBLKSZ); i++) + if (securdata[i]) + for (j=0; j<(1 << SECBLKSZ); j++) + if (securdata[i][j].filecount) { + count++; + } + printf("# %d security keys\n",count); + close_volume(volume); + } else { + fprintf(stderr,"Could not open volume %s\n",volume); + printerror(stdout); + err = TRUE; + } + close_security_api(); + } else { + if (getuid()) + fprintf(stderr,"This is only possible as root\n"); + else + fprintf(stderr,"Could not open security API\n"); + err = TRUE; + } + return (err); +} + +/* + * List security descriptors in a directory tree + */ + +static BOOL listfiles(const char *volume, const char *root) +{ + BOOL err; + int i,j; + int count; + + if (!getuid() && open_security_api()) { + if (open_volume(volume,NTFS_MNT_RDONLY)) { + if (opt_r) { + printf("\nRecursive file check\n"); + err = recurseshow(root); + printf("Summary\n"); + count = 0; + for (i=0; i<(MAXSECURID + (1 << SECBLKSZ) - 1)/(1 << SECBLKSZ); i++) + if (securdata[i]) + for (j=0; j<(1 << SECBLKSZ); j++) + if (securdata[i][j].filecount) { + printf("Key 0x%x : %d files, mode 0%03o\n", + i*(1 << SECBLKSZ)+j,securdata[i][j].filecount, + securdata[i][j].mode); + count++; + } + printf("%d security keys\n",count); + } else + err = singleshow(root); + close_volume(volume); + } else { + err = TRUE; + } + close_security_api(); + } else { + if (getuid()) + fprintf(stderr,"This is only possible as root\n"); + else + fprintf(stderr,"Could not open security API\n"); + err = TRUE; + } + return (err); +} + +#ifdef HAVE_WINDOWS_H + +static BOOL mapproposal(const char *volume, const char *name) +{ + BOOL err; + u32 attrsz; + int securindex; + char attr[256]; /* header (20) and a couple of SIDs (max 68 each) */ + + err = FALSE; + if (!getuid() && open_security_api()) { + if (open_volume(volume,NTFS_MNT_RDONLY)) { + + attrsz = 0; + securindex = ntfs_get_file_security(ntfs_context,name, + OWNER_SECURITY_INFORMATION + | GROUP_SECURITY_INFORMATION, + (char*)attr,MAXATTRSZ,&attrsz); + if (securindex) + err = proposal(name,attr); + else { + fprintf(stderr,"*** Could not get the ACL of "); + printname(stderr, name); + fprintf(stderr,"\n"); + printerror(stderr); + errors++; + } + close_volume(volume); + } else { + fprintf(stderr,"Could not open volume %s\n",volume); + printerror(stdout); + err = TRUE; + } + close_security_api(); + } else { + if (getuid()) + fprintf(stderr,"This is only possible as root\n"); + else + fprintf(stderr,"Could not open security API\n"); + err = TRUE; + } + return (err); +} + +#endif + +/* + * Check whether a SDS entry is valid + */ + +static BOOL valid_sds(const char *attr, unsigned int offset, + unsigned int entrysz, unsigned int size, u32 prevkey, + BOOL second) +{ + BOOL unsane; + u32 comphash; + u32 key; + + unsane = FALSE; + if (!get4l(attr,0) && !get4l(attr,4)) { + printf("Entry at 0x%lx was deleted\n",(long)offset); + } else { + if ((ntfs_attr_size(&attr[20]) + 20) > entrysz) { + printf("** Entry is truncated (expected size %ld)\n", + (long)ntfs_attr_size(&attr[20] + 20)); + unsane = TRUE; + errors++; + } + if ((ntfs_attr_size(&attr[20]) + 20) < entrysz) { + printf("** Extra data appended to entry (expected size %ld)\n", + (long)ntfs_attr_size(&attr[20]) + 20); + warnings++; + } + if (!unsane && !ntfs_valid_descr((const char*)&attr[20],size)) { + printf("** General sanity check has failed\n"); + unsane = TRUE; + errors++; + } + if (!unsane) { + comphash = hash((const le32*)&attr[20],entrysz-20); + if ((u32)get4l(attr,0) == comphash) { + if (opt_v >= 2) + printf("Hash 0x%08lx (correct)\n", + (unsigned long)comphash); + } else { + printf("** hash 0x%08lx (computed : 0x%08lx)\n", + (unsigned long)get4l(attr,0), + (unsigned long)comphash); + unsane = TRUE; + errors++; + } + } + if (!unsane) { + if ((second ? get8l(attr,8) + 0x40000 : get8l(attr,8)) == offset) { + if (opt_v >= 2) + printf("Offset 0x%lx (correct)\n",(long)offset); + } else { + printf("** offset 0x%llx (expected : 0x%llx)\n", + (long long)get8l(attr,8), + (long long)(second ? get8l(attr,8) - 0x40000 : get8l(attr,8))); +// unsane = TRUE; + errors++; + } + } + if (!unsane) { + key = get4l(attr,4); + if (opt_v >= 2) + printf("Key 0x%x\n",(int)key); + if (key) { + if (key <= prevkey) { + printf("** Unordered key 0x%lx after 0x%lx\n", + (long)key,(long)prevkey); + unsane = TRUE; + errors++; + } + } + } + } + return (!unsane); +} + +/* + * Check whether a SDS entry is consistent with other known data + * and store current data for subsequent checks + */ + +static int consist_sds(const char *attr, unsigned int offset, + unsigned int entrysz, BOOL second) +{ + int errcnt; + u32 key; + u32 comphash; + struct SECURITY_DATA *psecurdata; + + errcnt = 0; + key = get4l(attr,4); + if ((key > 0) && (key < MAXSECURID)) { + printf("Valid entry at 0x%lx for key 0x%lx\n", + (long)offset,(long)key); + if (!securdata[key >> SECBLKSZ]) + newblock(key); + if (securdata[key >> SECBLKSZ]) { + psecurdata = &securdata[key >> SECBLKSZ][key & ((1 << SECBLKSZ) - 1)]; + comphash = hash((const le32*)&attr[20],entrysz-20); + if (psecurdata->flags & INSDS1) { + if (psecurdata->hash != comphash) { + printf("** Different hash values : $SDS-1 0x%08lx $SDS-2 0x%08lx\n", + (unsigned long)psecurdata->hash, + (unsigned long)comphash); + errcnt++; + errors++; + } + if (psecurdata->offset != get8l(attr,8)) { + printf("** Different offsets : $SDS-1 0x%llx $SDS-2 0x%llx\n", + (long long)psecurdata->offset,(long long)get8l(attr,8)); + errcnt++; + errors++; + } + if (psecurdata->length != get4l(attr,16)) { + printf("** Different lengths : $SDS-1 0x%lx $SDS-2 0x%lx\n", + (long)psecurdata->length,(long)get4l(attr,16)); + errcnt++; + errors++; + } + } else { + if (second) { + printf("** Entry was not present in $SDS-1\n"); + errcnt++; + errors++; + } + psecurdata->hash = comphash; + psecurdata->offset = get8l(attr,8); + psecurdata->length = get4l(attr,16); + } + psecurdata->flags |= (second ? INSDS2 : INSDS1); + } + } else + if (key || get4l(attr,0)) { + printf("** Security_id 0x%x out of bounds\n",key); + warnings++; + } + return (errcnt); +} + + +/* + * Auditing of $SDS + */ + +static int audit_sds(BOOL second) +{ + static char attr[MAXATTRSZ + 20]; + BOOL isdir; + BOOL done; + BOOL unsane; + u32 prevkey; + int errcnt; + int size; + unsigned int entrysz; + unsigned int entryalsz; + unsigned int offset; + int count; + int deleted; + int mode; + + if (second) + printf("\nAuditing $SDS-2\n"); + else + printf("\nAuditing $SDS-1\n"); + errcnt = 0; + offset = (second ? 0x40000 : 0); + count = 0; + deleted = 0; + done = FALSE; + prevkey = 0; + + /* get size of first record */ + + size = ntfs_read_sds(ntfs_context,(char*)attr,20,offset); + if (size != 20) { + if ((size < 0) && (errno == ENOTSUP)) + printf("** There is no $SDS-%d in this volume\n", + (second ? 2 : 1)); + else { + printf("** Could not open $SDS-%d, size %d\n", + (second ? 2 : 1),size); + errors++; + errcnt++; + } + } else + do { + entrysz = get4l(attr,16); + entryalsz = ((entrysz - 1) | 15) + 1; + if (entryalsz <= (MAXATTRSZ + 20)) { + /* read next header in anticipation, to get its size */ + size = ntfs_read_sds(ntfs_context, + (char*)&attr[20],entryalsz,offset + 20); + if (opt_v) + printf("\nAt offset 0x%lx got %lu bytes\n",(long)offset,(long)size); + } else { + printf("** Security attribute is too long (%ld bytes) - stopping\n", + (long)entryalsz); + errcnt++; + } + if ((entryalsz > (MAXATTRSZ + 20)) || (size < (int)(entrysz - 20))) + done = TRUE; + else { + if (opt_v) { + printf("Entry size %d bytes\n",entrysz); + hexdump(&attr[20],size,8); + } + + unsane = !valid_sds(attr,offset,entrysz, + size,prevkey,second); + if (!unsane) { + if (!get4l(attr,0) && !get4l(attr,4)) + deleted++; + else + count++; + errcnt += consist_sds(attr,offset, + entrysz, second); + if (opt_v >= 2) { + isdir = guess_dir(&attr[20]); + printf("Assuming %s descriptor\n",(isdir ? "directory" : "file")); + showheader(&attr[20],0); + showusid(&attr[20],0); + showgsid(&attr[20],0); + showdacl(&attr[20],isdir,0); + showsacl(&attr[20],isdir,0); + showownership(&attr[20]); + mode = linux_permissions( + &attr[20],isdir); + printf("Interpreted Unix mode 0%03o\n",mode); + } + prevkey = get4l(attr,4); + } + if (!unsane) { + memcpy(attr,&attr[entryalsz],20); + offset += entryalsz; + if (!get4l(attr,16) + || ((((offset - 1) | 0x3ffff) - offset + 1) < 20)) { + if (second) + offset = ((offset - 1) | 0x7ffff) + 0x40001; + else + offset = ((offset - 1) | 0x7ffff) + 1; + if (opt_v) + printf("Trying next SDS-%d block at offset 0x%lx\n", + (second ? 2 : 1), (long)offset); + size = ntfs_read_sds(ntfs_context, + (char*)attr,20,offset); + if (size != 20) { + if (opt_v) + printf("Assuming end of $SDS, got %d bytes\n",size); + done = TRUE; + } + } + } else { + printf("** Sanity check failed - stopping there\n"); + errcnt++; + errors++; + done = TRUE; + } + } + } while (!done); + if (count || deleted || errcnt) { + printf("%d valid and %d deleted entries in $SDS-%d\n", + count,deleted,(second ? 2 : 1)); + printf("%d errors in $SDS-%c\n",errcnt,(second ? '2' : '1')); + } + return (errcnt); +} + +/* + * Check whether a SII entry is sane + */ + +static BOOL valid_sii(const char *entry, u32 prevkey) +{ + BOOL valid; + u32 key; + + valid = TRUE; + key = get4l(entry,16); + if (key <= prevkey) { + printf("** Unordered key 0x%lx after 0x%lx\n", + (long)key,(long)prevkey); + valid = FALSE; + errors++; + } + prevkey = key; + if (get2l(entry,0) != 20) { + printf("** offset %d (instead of 20)\n",(int)get2l(entry,0)); + valid = FALSE; + errors++; + } + if (get2l(entry,2) != 20) { + printf("** size %d (instead of 20)\n",(int)get2l(entry,2)); + valid = FALSE; + errors++; + } + if (get4l(entry,4) != 0) { + printf("** fill1 %d (instead of 0)\n",(int)get4l(entry,4)); + valid = FALSE; + errors++; + } + if (get2l(entry,12) & 1) { + if (get2l(entry,8) != 48) { + printf("** index size %d (instead of 48)\n",(int)get2l(entry,8)); + valid = FALSE; + errors++; + } + } else + if (get2l(entry,8) != 40) { + printf("** index size %d (instead of 40)\n",(int)get2l(entry,8)); + valid = FALSE; + errors++; + } + if (get2l(entry,10) != 4) { + printf("** index key size %d (instead of 4)\n",(int)get2l(entry,10)); + valid = FALSE; + errors++; + } + if ((get2l(entry,12) & ~3) != 0) { + printf("** flags 0x%x (instead of < 4)\n",(int)get2l(entry,12)); + valid = FALSE; + errors++; + } + if (get2l(entry,14) != 0) { + printf("** fill2 %d (instead of 0)\n",(int)get2l(entry,14)); + valid = FALSE; + errors++; + } + if (get4l(entry,24) != key) { + printf("** key 0x%x (instead of 0x%x)\n", + (int)get4l(entry,24),(int)key); + valid = FALSE; + errors++; + } + return (valid); +} + +/* + * Check whether a SII entry is consistent with other known data + */ + +static int consist_sii(const char *entry) +{ + int errcnt; + u32 key; + struct SECURITY_DATA *psecurdata; + + errcnt = 0; + key = get4l(entry,16); + if ((key > 0) && (key < MAXSECURID)) { + printf("Valid entry for key 0x%lx\n",(long)key); + if (!securdata[key >> SECBLKSZ]) + newblock(key); + if (securdata[key >> SECBLKSZ]) { + psecurdata = &securdata[key >> SECBLKSZ][key & ((1 << SECBLKSZ) - 1)]; + psecurdata->flags |= INSII; + if (psecurdata->flags & (INSDS1 | INSDS2)) { + if ((u32)get4l(entry,20) != psecurdata->hash) { + printf("** hash 0x%x (instead of 0x%x)\n", + (unsigned int)get4l(entry,20), + (unsigned int)psecurdata->hash); + errors++; + } + if (get8l(entry,28) != psecurdata->offset) { + printf("** offset 0x%llx (instead of 0x%llx)\n", + (long long)get8l(entry,28), + (long long)psecurdata->offset); + errors++; + } + if (get4l(entry,36) != psecurdata->length) { + printf("** length 0x%lx (instead of %ld)\n", + (long)get4l(entry,36), + (long)psecurdata->length); + errors++; + } + } else { + printf("** Entry was not present in $SDS\n"); + errors++; + psecurdata->hash = get4l(entry,20); + psecurdata->offset = get8l(entry,28); + psecurdata->length = get4l(entry,36); + if (opt_v) { + printf(" hash 0x%x\n",(unsigned int)psecurdata->hash); + printf(" offset 0x%llx\n",(long long)psecurdata->offset); + printf(" length %ld\n",(long)psecurdata->length); + } + errcnt++; + } + } + } else { + printf("** Security_id 0x%x out of bounds\n",key); + warnings++; + } + return (errcnt); +} + + +/* + * Auditing of $SII + */ + +static int audit_sii(void) +{ + char *entry; + int errcnt; + u32 prevkey; + BOOL valid; + BOOL done; + int count; + + printf("\nAuditing $SII\n"); + errcnt = 0; + count = 0; + entry = (char*)NULL; + prevkey = 0; + done = FALSE; + do { + entry = (char*)ntfs_read_sii(ntfs_context,(INDEX_ENTRY*)entry); + if (entry) { + valid = valid_sii(entry,prevkey); + if (valid) { + count++; + errcnt += consist_sii(entry); + prevkey = get4l(entry,16); + } else + errcnt++; + } else + if ((errno == ENOTSUP) && !prevkey) + printf("** There is no $SII in this volume\n"); + } while (entry && !done); + if (count || errcnt) { + printf("%d valid entries in $SII\n",count); + printf("%d errors in $SII\n",errcnt); + } + return (errcnt); +} + +/* + * Check whether a SII entry is sane + */ + +static BOOL valid_sdh(const char *entry, u32 prevkey, u32 prevhash) +{ + BOOL valid; + u32 key; + u32 currhash; + + valid = TRUE; + currhash = get4l(entry,16); + key = get4l(entry,20); + if ((currhash < prevhash) + || ((currhash == prevhash) && (key <= prevkey))) { + printf("** Unordered hash and key 0x%x 0x%x after 0x%x 0x%x\n", + (unsigned int)currhash,(unsigned int)key, + (unsigned int)prevhash,(unsigned int)prevkey); + valid = FALSE; + errors++; + } + if ((opt_v >= 2) && (currhash == prevhash)) + printf("Hash collision (not an error)\n"); + + if (get2l(entry,0) != 24) { + printf("** offset %d (instead of 24)\n",(int)get2l(entry,0)); + valid = FALSE; + errors++; + } + if (get2l(entry,2) != 20) { + printf("** size %d (instead of 20)\n",(int)get2l(entry,2)); + valid = FALSE; + errors++; + } + if (get4l(entry,4) != 0) { + printf("** fill1 %d (instead of 0)\n",(int)get4l(entry,4)); + valid = FALSE; + errors++; + } + if (get2l(entry,12) & 1) { + if (get2l(entry,8) != 56) { + printf("** index size %d (instead of 56)\n",(int)get2l(entry,8)); + valid = FALSE; + errors++; + } + } else + if (get2l(entry,8) != 48) { + printf("** index size %d (instead of 48)\n",(int)get2l(entry,8)); + valid = FALSE; + errors++; + } + if (get2l(entry,10) != 8) { + printf("** index key size %d (instead of 8)\n",(int)get2l(entry,10)); + valid = FALSE; + errors++; + } + if ((get2l(entry,12) & ~3) != 0) { + printf("** flags 0x%x (instead of < 4)\n",(int)get2l(entry,12)); + valid = FALSE; + errors++; + } + if (get2l(entry,14) != 0) { + printf("** fill2 %d (instead of 0)\n",(int)get2l(entry,14)); + valid = FALSE; + errors++; + } + if ((u32)get4l(entry,24) != currhash) { + printf("** hash 0x%x (instead of 0x%x)\n", + (unsigned int)get4l(entry,24),(unsigned int)currhash); + valid = FALSE; + errors++; + } + if (get4l(entry,28) != key) { + printf("** key 0x%x (instead of 0x%x)\n", + (int)get4l(entry,28),(int)key); + valid = FALSE; + errors++; + } + if (get4l(entry,44) + && (get4l(entry,44) != 0x490049)) { + printf("** fill3 0x%lx (instead of 0 or 0x490049)\n", + (long)get4l(entry,44)); + valid = FALSE; + errors++; + } + return (valid); +} + +/* + * Check whether a SDH entry is consistent with other known data + */ + +static int consist_sdh(const char *entry) +{ + int errcnt; + u32 key; + struct SECURITY_DATA *psecurdata; + + errcnt = 0; + key = get4l(entry,20); + if ((key > 0) && (key < MAXSECURID)) { + printf("Valid entry for key 0x%lx\n",(long)key); + if (!securdata[key >> SECBLKSZ]) + newblock(key); + if (securdata[key >> SECBLKSZ]) { + psecurdata = &securdata[key >> SECBLKSZ][key & ((1 << SECBLKSZ) - 1)]; + psecurdata->flags |= INSDH; + if (psecurdata->flags & (INSDS1 | INSDS2 | INSII)) { + if ((u32)get4l(entry,24) != psecurdata->hash) { + printf("** hash 0x%x (instead of 0x%x)\n", + (unsigned int)get4l(entry,24), + (unsigned int)psecurdata->hash); + errors++; + } + if (get8l(entry,32) != psecurdata->offset) { + printf("** offset 0x%llx (instead of 0x%llx)\n", + (long long)get8l(entry,32), + (long long)psecurdata->offset); + errors++; + } + if (get4l(entry,40) != psecurdata->length) { + printf("** length %ld (instead of %ld)\n", + (long)get4l(entry,40), + (long)psecurdata->length); + errors++; + } + } else { + printf("** Entry was not present in $SDS nor in $SII\n"); + errors++; + psecurdata->hash = get4l(entry,24); + psecurdata->offset = get8l(entry,32); + psecurdata->length = get4l(entry,40); + if (opt_v) { + printf(" offset 0x%llx\n",(long long)psecurdata->offset); + printf(" length %ld\n",(long)psecurdata->length); + } + errcnt++; + } + } + } else { + printf("** Security_id 0x%x out of bounds\n",key); + warnings++; + } + return (errcnt); +} + +/* + * Auditing of $SDH + */ + +static int audit_sdh(void) +{ + char *entry; + int errcnt; + int count; + u32 prevkey; + u32 prevhash; + BOOL valid; + BOOL done; + + printf("\nAuditing $SDH\n"); + count = 0; + errcnt = 0; + prevkey = 0; + prevhash = 0; + entry = (char*)NULL; + done = FALSE; + do { + entry = (char*)ntfs_read_sdh(ntfs_context,(INDEX_ENTRY*)entry); + if (entry) { + valid = valid_sdh(entry,prevkey,prevhash); + if (valid) { + count++; + errcnt += consist_sdh(entry); + prevhash = get4l(entry,16); + prevkey = get4l(entry,20); + } else + errcnt++; + } else + if ((errno == ENOTSUP) && !prevkey) + printf("** There is no $SDH in this volume\n"); + } while (entry && !done); + if (count || errcnt) { + printf("%d valid entries in $SDH\n",count); + printf("%d errors in $SDH\n",errcnt); + } + return (errcnt); +} + +/* + * Audit summary + */ + +static void audit_summary(void) +{ + int count; + int flags; + int cnt; + int found; + int i,j; + + count = 0; + found = 0; + if (opt_r) printf("Summary of security key use :\n"); + for (i=0; i<(MAXSECURID + (1 << SECBLKSZ) - 1)/(1 << SECBLKSZ); i++) + if (securdata[i]) + for (j=0; j<(1 << SECBLKSZ); j++) { + flags = securdata[i][j].flags & (INSDS1 + INSDS2 + INSII + INSDH); + if (flags) found++; + if (flags + && (flags != (INSDS1 + INSDS2 + INSII + INSDH))) + { + if (!count && !opt_r) + printf("\n** Keys not present in all files :\n"); + cnt = securdata[i][j].filecount; + if (opt_r) + printf("Key 0x%x used by %d %s, not in", + i*(1 << SECBLKSZ)+j,cnt, + (cnt > 1 ? "files" : "file")); + else + printf("Key 0x%x not in", i*(1 << SECBLKSZ)+j); + if (!(flags & INSDS1)) + printf(" SDS-1"); + if (!(flags & INSDS2)) + printf(" SDS-2"); + if (!(flags & INSII)) + printf(" SII"); + if (!(flags & INSDH)) + printf(" SDH"); + printf("\n"); + count++; + } else { + cnt = securdata[i][j].filecount; + if (opt_r && cnt) + printf("Key 0x%x used by %d %s\n", + i*(1 << SECBLKSZ)+j,cnt, + (cnt > 1 ? "files" : "file")); + } + } + if (found) { + if (count) + printf("%d keys not present in all lists\n",count); + else + printf("All keys are present in all lists\n"); + } +} + +/* + * Auditing + */ + +static BOOL audit(const char *volume) +{ + BOOL err; + + err = FALSE; + if (!getuid() && open_security_api()) { + if (open_volume(volume,NTFS_MNT_RDONLY)) { + if (audit_sds(FALSE)) err = TRUE; + if (audit_sds(TRUE)) err = TRUE; + if (audit_sii()) err = TRUE; + if (audit_sdh()) err = TRUE; + if (opt_r) recurseshow("/"); + + audit_summary(); + close_volume(volume); + } + else { + fprintf(stderr,"Could not open volume %s\n",volume); + printerror(stdout); + err = TRUE; + } + close_security_api(); + } + else { + if (getuid()) + fprintf(stderr,"This is only possible as root\n"); + else fprintf(stderr,"Could not open security API\n"); + err = TRUE; + } + return (err); +} + +#if POSIXACLS + +/* + * Encode a Posix ACL string + * [d:]{ugmo}:uid[:perms],... + */ + +static struct POSIX_SECURITY *encode_posix_acl(const char *str) +{ + int acccnt; + int defcnt; + int i,k,l; + int c; + s32 id; + u16 perms; + u16 apermsset; + u16 dpermsset; + u16 tag; + u16 tagsset; + mode_t mode; + BOOL defacl; + BOOL dmask; + BOOL amask; + const char *p; + struct POSIX_ACL *acl; + struct POSIX_SECURITY *pxdesc; + enum { PXBEGIN, PXTAG, PXTAG1, PXID, PXID1, PXID2, + PXPERM, PXPERM1, PXPERM2, PXOCT, PXNEXT, PXEND, PXERR + } state; + + /* raw evaluation of ACE count */ + p = str; + amask = FALSE; + dmask = FALSE; + if (*p == 'd') { + acccnt = 0; + defcnt = 1; + } else { + if ((*p >= '0') && (*p <= '7')) + acccnt = 0; + else + acccnt = 1; + defcnt = 0; + } + while (*p) + if (*p++ == ',') { + if (*p == 'd') { + defcnt++; + if (p[1] && (p[2] == 'm')) + dmask = TRUE; + } else { + acccnt++; + if (*p == 'm') + amask = TRUE; + } + } + /* account for an implicit mask if none defined */ + if (acccnt && !amask) + acccnt++; + if (defcnt && !dmask) + defcnt++; + pxdesc = (struct POSIX_SECURITY*)malloc(sizeof(struct POSIX_SECURITY) + + (acccnt + defcnt)*sizeof(struct POSIX_ACE)); + if (pxdesc) { + pxdesc->acccnt = acccnt; + pxdesc->firstdef = acccnt; + pxdesc->defcnt = defcnt; + acl = &pxdesc->acl; + p = str; + state = PXBEGIN; + id = 0; + defacl = FALSE; + mode = 0; + apermsset = 0; + dpermsset = 0; + tag = 0; + perms = 0; + k = l = 0; + c = *p++; + while ((state != PXEND) && (state != PXERR)) { + switch (state) { + case PXBEGIN : + if (c == 'd') { + defacl = TRUE; + state = PXTAG1; + break; + } else + if ((c >= '0') && (c <= '7')) { + mode = c - '0'; + state = PXOCT; + break; + } + defacl = FALSE; + /* fall through */ + case PXTAG : + switch (c) { + case 'u' : + tag = POSIX_ACL_USER; + state = PXID; + break; + case 'g' : + tag = POSIX_ACL_GROUP; + state = PXID; + break; + case 'o' : + tag = POSIX_ACL_OTHER; + state = PXID; + break; + case 'm' : + tag = POSIX_ACL_MASK; + state = PXID; + break; + default : + state = PXERR; + break; + } + break; + case PXTAG1 : + if (c == ':') + state = PXTAG; + else + state = PXERR; + break; + case PXID : + if (c == ':') { + if ((tag == POSIX_ACL_OTHER) + || (tag == POSIX_ACL_MASK)) + state = PXPERM; + else + state = PXID1; + } else + state = PXERR; + break; + case PXID1 : + if ((c >= '0') && (c <= '9')) { + id = c - '0'; + state = PXID2; + } else + if (c == ':') { + id = -1; + if (tag == POSIX_ACL_USER) + tag = POSIX_ACL_USER_OBJ; + if (tag == POSIX_ACL_GROUP) + tag = POSIX_ACL_GROUP_OBJ; + state = PXPERM1; + } else + state = PXERR; + break; + case PXID2 : + if ((c >= '0') && (c <= '9')) + id = 10*id + c - '0'; + else + if (c == ':') + state = PXPERM1; + else + state = PXERR; + break; + case PXPERM : + if (c == ':') { + id = -1; + state = PXPERM1; + } else + state = PXERR; + break; + case PXPERM1 : + if ((c >= '0') && (c <= '7')) { + perms = c - '0'; + state = PXNEXT; + break; + } + state = PXPERM2; + perms = 0; + /* fall through */ + case PXPERM2 : + switch (c) { + case 'r' : + perms |= POSIX_PERM_R; + break; + case 'w' : + perms |= POSIX_PERM_W; + break; + case 'x' : + perms |= POSIX_PERM_X; + break; + case ',' : + case '\0' : + if (defacl) { + i = acccnt + l++; + dpermsset |= perms; + } else { + i = k++; + apermsset |= perms; + } + acl->ace[i].tag = tag; + acl->ace[i].perms = perms; + acl->ace[i].id = id; + if (c == '\0') + state = PXEND; + else + state = PXBEGIN; + break; + } + break; + case PXNEXT : + if (!c || (c == ',')) { + if (defacl) { + i = acccnt + l++; + dpermsset |= perms; + } else { + i = k++; + apermsset |= perms; + } + acl->ace[i].tag = tag; + acl->ace[i].perms = perms; + acl->ace[i].id = id; + if (c == '\0') + state = PXEND; + else + state = PXBEGIN; + } else + state = PXERR; + break; + case PXOCT : + if ((c >= '0') && (c <= '7')) + mode = (mode << 3) + c - '0'; + else + if (c == '\0') + state = PXEND; + else + state = PXBEGIN; + break; + default : + break; + } + c = *p++; + } + /* insert default mask if none defined */ + if (acccnt && !amask) { + i = k++; + acl->ace[i].tag = POSIX_ACL_MASK; + acl->ace[i].perms = apermsset; + acl->ace[i].id = -1; + } + if (defcnt && !dmask) { + i = acccnt + l++; + acl->ace[i].tag = POSIX_ACL_MASK; + acl->ace[i].perms = dpermsset; + acl->ace[i].id = -1; + } + /* compute the mode and tagsset */ + tagsset = 0; + for (i=0; iace[i].tag; + switch (acl->ace[i].tag) { + case POSIX_ACL_USER_OBJ : + mode |= acl->ace[i].perms << 6; + break; + case POSIX_ACL_GROUP_OBJ : + /* unless mask seen first */ + if (!(tagsset & POSIX_ACL_MASK)) + mode |= acl->ace[i].perms << 3; + break; + case POSIX_ACL_OTHER : + mode |= acl->ace[i].perms; + break; + case POSIX_ACL_MASK : + /* overrides group */ + mode = (mode & 07707) + | (acl->ace[i].perms << 3); + break; + default : + break; + } + } + pxdesc->mode = mode; + pxdesc->tagsset = tagsset; + pxdesc->acl.version = POSIX_VERSION; + pxdesc->acl.flags = 0; + pxdesc->acl.filler = 0; + if (state != PXERR) + ntfs_sort_posix(pxdesc); +showposix(pxdesc); + if ((state == PXERR) + || (k != acccnt) + || (l != defcnt) + || !ntfs_valid_posix(pxdesc)) { + if (~pxdesc->tagsset + & (POSIX_ACL_USER_OBJ | POSIX_ACL_GROUP_OBJ | POSIX_ACL_OTHER)) + fprintf(stderr,"User, group or other permissions missing\n"); + else + fprintf(stderr,"Bad ACL description\n"); + free(pxdesc); + pxdesc = (struct POSIX_SECURITY*)NULL; + } else + if (opt_v >= 2) { + printf("Interpreted input description :\n"); + showposix(pxdesc); + } + } else + errno = ENOMEM; + return (pxdesc); +} + +#endif /* POSIXACLS */ + +static BOOL setperms(const char *volume, const char *perms, const char *base) +{ + const char *p; + BOOL cmderr; + int i; +#if POSIXACLS + struct POSIX_SECURITY *pxdesc; +#else /* POSIXACLS */ + int mode; +#endif /* POSIXACLS */ + + cmderr = FALSE; + p = perms; +#if POSIXACLS + pxdesc = encode_posix_acl(p); + if (pxdesc) { + if (!getuid() && open_security_api()) { + if (open_volume(volume,NTFS_MNT_NONE)) { + if (opt_r) { + for (i=0; i<(MAXSECURID + (1 << SECBLKSZ) - 1)/(1 << SECBLKSZ); i++) + securdata[i] = (struct SECURITY_DATA*)NULL; + recurseset_posix(base,pxdesc); + } else + singleset_posix(base,pxdesc); + close_volume(volume); + } else { + fprintf(stderr,"Could not open volume %s\n",volume); + printerror(stderr); + cmderr = TRUE; + } + close_security_api(); + } else { + if (getuid()) + fprintf(stderr,"This is only possible as root\n"); + else + fprintf(stderr,"Could not open security API\n"); + cmderr = TRUE; + } + free(pxdesc); + } else + cmderr = TRUE; +#else /* POSIXACLS */ + mode = 0; + while ((*p >= '0') && (*p <= '7')) + mode = (mode << 3) + (*p++) - '0'; + if (*p) { + fprintf(stderr,"New mode should be given in octal\n"); + cmderr = TRUE; + } else { + if (!getuid() && open_security_api()) { + if (open_volume(volume,NTFS_MNT_NONE)) { + if (opt_r) { + for (i=0; i<(MAXSECURID + (1 << SECBLKSZ) - 1)/(1 << SECBLKSZ); i++) + securdata[i] = (struct SECURITY_DATA*)NULL; + recurseset(base,mode); + } else + singleset(base,mode); + close_volume(volume); + } else { + fprintf(stderr,"Could not open volume %s\n",volume); + printerror(stderr); + cmderr = TRUE; + } + close_security_api(); + } else { + if (getuid()) + fprintf(stderr,"This is only possible as root\n"); + else + fprintf(stderr,"Could not open security API\n"); + cmderr = TRUE; + } + } +#endif /* POSIXACLS */ + return (cmderr); +} + +static void usage(void) +{ +#ifdef HAVE_WINDOWS_H + fprintf(stderr,"Usage:\n"); +#ifdef SELFTESTS + fprintf(stderr," ntfssecaudit -t\n"); + fprintf(stderr," run self-tests\n"); +#endif /* SELFTESTS */ + fprintf(stderr," ntfssecaudit -h [file]\n"); + fprintf(stderr," display security descriptors within file\n"); + fprintf(stderr," ntfssecaudit -a[rv] volume\n"); + fprintf(stderr," audit the volume\n"); + fprintf(stderr," ntfssecaudit [-v] file\n"); + fprintf(stderr," display the security parameters of file\n"); + fprintf(stderr," ntfssecaudit -r[v] directory\n"); + fprintf(stderr," display the security parameters of files in directory\n"); + fprintf(stderr," ntfssecaudit -b[v] directory\n"); + fprintf(stderr," backup the security parameters of files in directory\n"); + fprintf(stderr," ntfssecaudit -s[ev] volume [backupfile]\n"); + fprintf(stderr," set the security parameters as indicated in backup file\n"); + fprintf(stderr," with -e also set extra parameters (Windows attrib)\n"); + fprintf(stderr," ntfssecaudit perms file\n"); + fprintf(stderr," set the security parameters of file to perms\n"); + fprintf(stderr," ntfssecaudit -r[v] perms directory\n"); + fprintf(stderr," set the security parameters of files in directory to perms\n"); + fprintf(stderr," ntfssecaudit -u file\n"); + fprintf(stderr," get a user mapping proposal applicable to file\n"); +#if POSIXACLS + fprintf(stderr," Notes: perms can be an octal mode or a Posix ACL description\n"); +#else /* POSIXACLS */ + fprintf(stderr," Notes: perms is an octal mode\n"); +#endif /* POSIXACLS */ + fprintf(stderr," volume is a drive letter and colon (eg. D:)\n"); + fprintf(stderr," -v is for verbose, -vv for very verbose\n"); +#else /* HAVE_WINDOWS_H */ + fprintf(stderr,"Usage:\n"); +#ifdef SELFTESTS + fprintf(stderr," ntfssecaudit -t\n"); + fprintf(stderr," run self-tests\n"); +#endif /* SELFTESTS */ + fprintf(stderr," ntfssecaudit -h [file]\n"); + fprintf(stderr," display security descriptors within file\n"); + fprintf(stderr," ntfssecaudit -a[rv] volume\n"); + fprintf(stderr," audit the volume\n"); + fprintf(stderr," ntfssecaudit [-v] volume file\n"); + fprintf(stderr," display the security parameters of file\n"); + fprintf(stderr," ntfssecaudit -r[v] volume directory\n"); + fprintf(stderr," display the security parameters of files in directory\n"); + fprintf(stderr," ntfssecaudit -b[v] volume directory\n"); + fprintf(stderr," backup the security parameters of files in directory\n"); + fprintf(stderr," ntfssecaudit -s[ev] volume [backupfile]\n"); + fprintf(stderr," set the security parameters as indicated in backup file\n"); + fprintf(stderr," with -e also set extra parameters (Windows attrib)\n"); + fprintf(stderr," ntfssecaudit volume perms file\n"); + fprintf(stderr," set the security parameters of file to perms\n"); + fprintf(stderr," ntfssecaudit -r[v] volume perms directory\n"); + fprintf(stderr," set the security parameters of files in directory to perms\n"); +#ifdef HAVE_SETXATTR + fprintf(stderr," special cases, do not require being root :\n"); + fprintf(stderr," ntfssecaudit -u mounted-file\n"); + fprintf(stderr," get a user mapping proposal applicable to mounted file\n"); + fprintf(stderr," ntfssecaudit [-v] mounted-file\n"); + fprintf(stderr," display the security parameters of a mounted file\n"); +#endif /* HAVE_SETXATTR */ +#if POSIXACLS + fprintf(stderr," Notes: perms can be an octal mode or a Posix ACL description\n"); +#else /* POSIXACLS */ + fprintf(stderr," Notes: perms is an octal mode\n"); +#endif /* POSIXACLS */ +#if defined(__sun) && defined (__SVR4) + fprintf(stderr," volume is a partition designator (eg. /dev/dsk/c5t0d0p1)\n"); +#else /* defined(__sun) && defined (__SVR4) */ + fprintf(stderr," volume is a partition designator (eg. /dev/sdb2)\n"); +#endif /* defined(__sun) && defined (__SVR4) */ + fprintf(stderr," -v is for verbose, -vv for very verbose\n"); +#endif /* HAVE_WINDOWS_H */ +} + +static void version(void) +{ + static const char *EXEC_NAME = "ntfssecaudit"; + +// confusing (see banner) + printf("\n%s v%s (libntfs-3g) - Audit security data on a NTFS " + "Volume.\n\n", EXEC_NAME, VERSION); + printf(" Copyright (c) 2007-2016 Jean-Pierre Andre\n"); + printf("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +#ifdef HAVE_WINDOWS_H + +/* + * Split a Windows file designator into volume and fullpath + */ + +static BOOL splitarg(char **split, const char *arg) +{ + char curdir[MAXFILENAME]; + BOOL err; + BOOL withvol; + BOOL withfullpath; + int lthd; + char *volume; + char *filename; + + err = TRUE; + withvol = arg[0] && (arg[1] == ':'); + if (withvol) + withfullpath = (arg[2] == '/') || (arg[2] == '\\'); + else + withfullpath = (arg[0] == '/') || (arg[0] == '\\'); + lthd = 0; + if (!withvol || !withfullpath) { + if (getcwd(curdir, sizeof(curdir))) + lthd = strlen(curdir); + } + if (withvol && !withfullpath && arg[2] + && ((arg[0] ^ curdir[0]) & 0x3f)) { + fprintf(stderr,"%c: is not the current drive,\n",arg[0]); + fprintf(stderr,"please use the full path\n"); + } else { + if (withvol && !withfullpath && !arg[2] + && ((arg[0] ^ curdir[0]) & 0x3f)) { + curdir[2] = '\\'; + curdir[3] = 0; + lthd = 3; + } + volume = (char*)malloc(4); + if (volume) { + if (withvol) + volume[0] = arg[0]; + else + volume[0] = curdir[0]; + volume[1] = ':'; + volume[2] = 0; + filename = (char*)malloc(strlen(arg) + lthd + 2); + if (filename) { + if (withfullpath) { + if (withvol) + strcpy(filename, &arg[2]); + else + strcpy(filename, arg); + } else { + strcpy(filename, &curdir[2]); + if (curdir[lthd - 1] != '\\') + strcat(filename, "\\"); + if (withvol) + strcat(filename, &arg[2]); + else + strcat(filename, arg); + } + if (!cleanpath(filename)) { + split[0] = volume; + split[1] = filename; + err = FALSE; + } else { + fprintf(stderr,"Bad path %s\n", arg); + } + } else + free(volume); + } + } + return (err); +} + +#endif /* HAVE_WINDOWS_H */ + +/* + * Parse the command-line options + */ + +static int parse_options(int argc, char *argv[]) +{ + static const char *sopt = "-abehHrstuvV"; + static const struct option lopt[] = { + { "audit", no_argument, NULL, 'a' }, + { "backup", no_argument, NULL, 'b' }, + { "extra", no_argument, NULL, 'e' }, + { "help", no_argument, NULL, 'H' }, + { "hexdecode", no_argument, NULL, 'h' }, + { "recurse", no_argument, NULL, 'r' }, + { "set", no_argument, NULL, 's' }, + { "test", no_argument, NULL, 't' }, + { "user-mapping",no_argument, NULL, 'u' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + + int c = -1; + int err = 0; + int ver = 0; + int help = 0; + int xarg = 0; + CMDS prevcmd; + + opterr = 0; /* We'll handle the errors, thank you. */ + + opt_e = FALSE; + opt_r = FALSE; + opt_v = 0; + cmd = CMD_NONE; + prevcmd = CMD_NONE; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: + if (!xarg) + xarg = optind - 1; + break; + case 'a': + prevcmd = cmd; + cmd = CMD_AUDIT; + break; + case 'b': + prevcmd = cmd; + cmd = CMD_BACKUP; + break; + case 'e': + opt_e = TRUE; + break; + case 'h': + prevcmd = cmd; + cmd = CMD_HEX; + break; + case 'H': + help++; + break; + case 'r': + opt_r = TRUE; + break; + case 's': + prevcmd = cmd; + cmd = CMD_SET; + break; +#ifdef SELFTESTS + case 't': + prevcmd = cmd; + cmd = CMD_TEST; + break; +#endif + case 'u': + prevcmd = cmd; + cmd = CMD_USERMAP; + break; + case 'v': + opt_v++; + break; + case 'V': + ver++; + break; + default: + if ((c < 'a') || (c > 'z')) + fprintf(stderr,"Unhandled option case: %d.\n", c); + else + fprintf(stderr,"Invalid option -%c\n",c); + err++; + break; + } + if ((cmd != CMD_NONE) + && (prevcmd != CMD_NONE) + && (prevcmd != cmd)) { + fprintf(stderr,"Incompatible commands\n"); + err++; + } + } + + if (!xarg) + xarg = argc; + + if (help || err) + cmd = CMD_HELP; + else + if (ver) + cmd = CMD_VERSION; + + return (err ? 0 : xarg); +} + +int main(int argc, char *argv[]) +{ + char *split[2]; + char *uname; + FILE *fd; + int xarg; + BOOL cmderr; + BOOL fail; + int i; + + printf("%s\n",BANNER); + cmderr = FALSE; + fail = FALSE; + errors = 0; + warnings = 0; + split[0] = split[1] = (char*)NULL; + uname = (char*)NULL; + xarg = parse_options(argc,argv); + if (xarg) { + for (i=0; i<(MAXSECURID + (1 << SECBLKSZ) - 1)/(1 << SECBLKSZ); i++) + securdata[i] = (struct SECURITY_DATA*)NULL; +#if POSIXACLS + context.mapping[MAPUSERS] = (struct MAPPING*)NULL; + context.mapping[MAPGROUPS] = (struct MAPPING*)NULL; +#endif /* POSIXACLS */ + mappingtype = MAPNONE; + + switch (cmd) { + case CMD_AUDIT : + if (xarg == (argc - 1)) + fail = audit(argv[xarg]); + else + cmderr = TRUE; + break; + case CMD_BACKUP : + switch (argc - xarg) { + case 1 : +#ifdef HAVE_WINDOWS_H + if (!splitarg(split, argv[xarg])) + fail = backup(split[0], split[1]); + else + cmderr = TRUE; +#else + fail = backup(argv[xarg],"/"); +#endif + break; + case 2 : + cmderr = backup(argv[xarg],argv[xarg+1]); + break; + default : + cmderr = TRUE; + break; + } + break; + case CMD_HEX : + switch (argc - xarg) { + case 0 : + showhex(stdin); + break; + case 1 : + fd = fopen(argv[xarg],"rb"); + if (fd) { + showhex(fd); + fclose(fd); + } else { + fprintf(stderr,"Could not open %s\n", + argv[xarg]); + cmderr = TRUE; + } + break; + default : + cmderr = TRUE; + break; + } + break; + case CMD_NONE : + switch (argc - xarg) { + case 1 : +#ifdef HAVE_WINDOWS_H + if (!splitarg(split, argv[xarg])) + fail = listfiles(split[0], split[1]); + else + cmderr = TRUE; +#else + if (opt_r) + cmderr = listfiles(argv[xarg],"/"); + else + cmderr = processmounted(argv[xarg]); +#endif + break; + case 2 : +#ifdef HAVE_WINDOWS_H + if (!splitarg(split, argv[xarg + 1])) + fail = setperms(split[0], + argv[xarg], split[1]); + else + cmderr = TRUE; +#else /* HAVE_WINDOWS_H */ + fail = listfiles(argv[xarg],argv[xarg+1]); +#endif /* HAVE_WINDOWS_H */ + break; + case 3 : +#ifdef HAVE_WINDOWS_H + uname = unixname(argv[xarg+2]); + if (uname) + fail = setperms(argv[xarg], + argv[xarg+1], uname); + else + cmderr = TRUE; +#else /* HAVE_WINDOWS_H */ + cmderr = setperms(argv[xarg], argv[xarg+1], + argv[xarg+2]); +#endif + break; + default : + cmderr = TRUE; + break; + } + break; + case CMD_SET : + switch (argc - xarg) { + case 1 : + cmderr = dorestore(argv[xarg],stdin); + break; + case 2 : + fd = fopen(argv[xarg+1],"rb"); + if (fd) { + if (dorestore(argv[xarg],fd)) + cmderr = TRUE; + fclose(fd); + } else { + fprintf(stderr,"Could not open %s\n", + argv[xarg]); + cmderr = TRUE; + } + break; + default : + cmderr = TRUE; + } + break; +#ifdef SELFTESTS + case CMD_TEST : + if (xarg != argc) + cmderr = TRUE; + else + selftests(); + break; +#endif + case CMD_USERMAP : + switch (argc - xarg) { + case 1 : +#ifdef HAVE_WINDOWS_H + if (!splitarg(split, argv[xarg])) + fail = mapproposal(split[0], + split[1]); + else + cmderr = TRUE; +#else /* HAVE_WINDOWS_H */ + processmounted(argv[xarg]); +#endif /* HAVE_WINDOWS_H */ + break; + case 2 : +#ifdef HAVE_WINDOWS_H + uname = unixname(argv[xarg+1]); + if (uname) + cmderr = mapproposal(argv[xarg], + uname); + else + cmderr = TRUE; +#else /* HAVE_WINDOWS_H */ + cmderr = TRUE; +#endif /* HAVE_WINDOWS_H */ + break; + default : + cmderr = TRUE; + } + break; + case CMD_HELP : + default : + usage(); + break; + case CMD_VERSION : + version(); + break; + } + + if (warnings) + printf("** %u %s signalled\n",warnings, + (warnings > 1 ? "warnings were" + : "warning was")); + if (errors) + printf("** %u %s found\n",errors, + (errors > 1 ? "errors were" : "error was")); + else + if (fail) + printf("Command failed\n"); + else + if (cmderr) + usage(); + else + printf("No errors were found\n"); + if (!isatty(1)) { + fflush(stdout); + if (warnings) + fprintf(stderr,"** %u %s signalled\n",warnings, + (warnings > 1 ? "warnings were" + : "warning was")); + if (errors) + fprintf(stderr,"** %u %s found\n",errors, + (errors > 1 ? "errors were" + : "error was")); + else + fprintf(stderr,"No errors were found\n"); + } + if (split[0]) + free(split[0]); + if (split[1]) + free(split[1]); + if (uname) + free(uname); + freeblocks(); + } else + usage(); + if (cmderr || errors) + exit(1); + return (0); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfstruncate.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfstruncate.8.in new file mode 100644 index 0000000..e69d4b9 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfstruncate.8.in @@ -0,0 +1,124 @@ +.\" Copyright (c) 2014 Jean-Pierre Andre +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSTRUNCATE 8 "June 2014" "ntfs-3g @VERSION@" +.SH NAME +ntfstruncate \- truncate a file on an NTFS volume +.SH SYNOPSIS +\fBntfstruncate\fR [\fIoptions\fR] \fIdevice\fR \fIfile\fR \fI[attr-type\fR [\fIattr-name\fR]] \fInew-length\fR +.SH DESCRIPTION +.B ntfstruncate +truncates (or extends) a specified attribute belonging to a +file or directory, to a specified length. +.SH OPTIONS +Below is a summary of all the options that +.B ntfstruncate +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not using a mounted volume. +Use this option with caution. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-l\fR +Display licensing information. +.TP +\fB\-n\fR, \fB\-\-no-action\fR +Simulate the truncation without actually write to device. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Suppress some debug/warning/error messages. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Display more debug/warning/error messages. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license of +.BR ntfstruncate . +.TP +\fBattr-type\fR +Define a particular attribute type to be truncated (advanced use only). +By default, the unnamed $DATA attribute (the contents of a plain file) will +be truncated. The attribute has to be specified by a number in decimal +or hexadecimal : +.TS +box; +lB lB lB +l l l. +Hex Decimal Name +0x10 16 "$STANDARD_INFORMATION" +0x20 32 "$ATTRIBUTE_LIST" +0x30 48 "$FILE_NAME" +0x40 64 "$OBJECT_ID" +0x50 80 "$SECURITY_DESCRIPTOR" +0x60 96 "$VOLUME_NAME" +0x70 112 "$VOLUME_INFORMATION" +0x80 128 "$DATA" +0x90 144 "$INDEX_ROOT" +0xA0 160 "$INDEX_ALLOCATION" +0xB0 176 "$BITMAP" +0xC0 192 "$REPARSE_POINT" +0xD0 208 "$EA_INFORMATION" +0xE0 224 "$EA" +0xF0 240 "$PROPERTY_SET" +0x100 256 "$LOGGED_UTILITY_STREAM" +.TE +.sp +.TP +\fBattr-name\fR +Define the name of the particular attribute type to be truncated +(advanced use only). +.sp +.TP +\fBnew-length\fR +Specify the target size of the file. +It will be rounded up to a multiple of the cluster size. +A suffix of K, M, G, T, P or E may be appended to +mean a multiplicative factor of a power of 1000. Similarly a suffix of +Ki, Mi, Gi, Ti, Pi or Ei may be appended to mean a multiplicative factor +of a power of 1024. +.SH EXAMPLES +Resize to 100MB the file database.db located in the Data directory +which is at the root of an NTFS file system. +.RS +.sp +.B ntfstruncate /dev/sda1 Data/database.db 100M +.sp +.RE +.SH BUGS +There are no known problems with +.BR ntfstruncate . +If you find a bug, please send an email +describing the problem to the development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfstruncate +was written by Anton Altaparmakov. +.SH AVAILABILITY +.B ntfstruncate +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfs-3g (8), +.BR ntfsfallocate (8), +.BR ntfsprogs (8). diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfstruncate.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfstruncate.c new file mode 100644 index 0000000..e997055 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfstruncate.c @@ -0,0 +1,812 @@ +/** + * ntfstruncate - Part of the Linux-NTFS project. + * + * Copyright (c) 2002-2005 Anton Altaparmakov + * + * This utility will truncate a specified attribute belonging to a + * specified inode, i.e. file or directory, to a specified length. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS source + * in the file COPYING); if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_UNISTD_H +# include +#endif +#ifdef HAVE_STDLIB_H +# include +#endif +#ifdef HAVE_STDIO_H +# include +#endif +#ifdef HAVE_STDARG_H +# include +#endif +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_ERRNO_H +# include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_GETOPT_H +# include +#else + extern char *optarg; + extern int optind; +#endif +#ifdef HAVE_LIMITS_H +#include +#endif +#ifndef LLONG_MAX +# define LLONG_MAX 9223372036854775807LL +#endif + +#include "types.h" +#include "attrib.h" +#include "inode.h" +#include "layout.h" +#include "volume.h" +#include "utils.h" +#include "attrdef.h" +/* #include "version.h" */ +#include "logging.h" + +const char *EXEC_NAME = "ntfstruncate"; + +/* Need these global so ntfstruncate_exit can access them. */ +BOOL success = FALSE; + +char *dev_name; +s64 inode; +ATTR_TYPES attr_type; +ntfschar *attr_name = NULL; +u32 attr_name_len; +s64 new_len; + +ntfs_volume *vol; +ntfs_inode *ni; +ntfs_attr *na = NULL; + +ATTR_DEF *attr_defs; + +struct { + /* -h, print usage and exit. */ + int no_action; /* -n, do not write to device, only display + what would be done. */ + int quiet; /* -q, quiet execution. */ + int verbose; /* -v, verbose execution, given twice, really + verbose execution (debug mode). */ + int force; /* -f, force truncation. */ + /* -V, print version and exit. */ +} opts; + +/** + * err_exit - error output and terminate; ignores quiet (-q) + */ +__attribute__((noreturn)) +__attribute__((format(printf, 1, 2))) +static void err_exit(const char *fmt, ...) +{ + va_list ap; + + fprintf(stderr, "ERROR: "); + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fprintf(stderr, "Aborting...\n"); + exit(1); +} + +/** + * copyright - print copyright statements + */ +static void copyright(void) +{ + fprintf(stderr, "Copyright (c) 2002-2005 Anton Altaparmakov\n" + "Copyright (c) 2003 Richard Russon\n" + "Truncate a specified attribute of a specified " + "inode.\n"); +} + +/** + * license - print license statement + */ +static void license(void) +{ + fprintf(stderr, "%s", ntfs_gpl); +} + +/** + * usage - print a list of the parameters to the program + */ +__attribute__((noreturn)) +static void usage(int ret) +{ + copyright(); + fprintf(stderr, "Usage: %s [options] device inode [attr-type " + "[attr-name]] new-length\n" + " If attr-type is not specified, 0x80 (i.e. $DATA) " + "is assumed.\n" + " If attr-name is not specified, an unnamed " + "attribute is assumed.\n" + " -n Do not write to disk\n" + " -f Force execution despite errors\n" + " -q Quiet execution\n" + " -v Verbose execution\n" + " -vv Very verbose execution\n" + " -V Display version information\n" + " -l Display licensing information\n" + " -h Display this help\n", EXEC_NAME); + fprintf(stderr, "%s%s", ntfs_bugs, ntfs_home); + exit(ret); +} + +/** + * parse_options + */ +static void parse_options(int argc, char *argv[]) +{ + long long ll; + char *s, *s2; + int c; + + if (argc && *argv) + EXEC_NAME = *argv; + fprintf(stderr, "%s v%s (libntfs-3g)\n", EXEC_NAME, VERSION); + while ((c = getopt(argc, argv, "fh?nqvVl")) != EOF) + switch (c) { + case 'f': + opts.force = 1; + break; + case 'n': + opts.no_action = 1; + break; + case 'q': + opts.quiet = 1; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + /* Version number already printed, so just exit. */ + exit(0); + case 'l': + copyright(); + license(); + exit(0); + case 'h': + usage(0); + case '?': + default: + usage(1); + } + if (optind == argc) + usage(1); + + if (opts.verbose > 1) + ntfs_log_set_levels(NTFS_LOG_LEVEL_DEBUG | NTFS_LOG_LEVEL_TRACE | + NTFS_LOG_LEVEL_VERBOSE | NTFS_LOG_LEVEL_QUIET); + + /* Get the device. */ + dev_name = argv[optind++]; + ntfs_log_verbose("device name = %s\n", dev_name); + + if (optind == argc) + usage(1); + + /* Get the inode. */ + ll = strtoll(argv[optind++], &s, 0); + if (*s || !ll || (ll >= LLONG_MAX && errno == ERANGE)) + err_exit("Invalid inode number: %s\n", argv[optind - 1]); + inode = ll; + ntfs_log_verbose("inode = %lli\n", (long long)inode); + + if (optind == argc) + usage(1); + + /* Get the attribute type, if specified. */ + s = argv[optind++]; + if (optind == argc) { + attr_type = AT_DATA; + attr_name = AT_UNNAMED; + attr_name_len = 0; + } else { + unsigned long ul; + + ul = strtoul(s, &s2, 0); + if (*s2 || !ul || (ul >= ULONG_MAX && errno == ERANGE)) + err_exit("Invalid attribute type %s: %s\n", s, + strerror(errno)); + attr_type = cpu_to_le32(ul); + + /* Get the attribute name, if specified. */ + s = argv[optind++]; + if (optind != argc) { + /* Convert the string to little endian Unicode. */ + attr_name_len = ntfs_mbstoucs(s, &attr_name); + if ((int)attr_name_len < 0) + err_exit("Invalid attribute name \"%s\": %s\n", + s, strerror(errno)); + + /* Keep hold of the original string. */ + s2 = s; + + s = argv[optind++]; + if (optind != argc) + usage(1); + } else { + attr_name = AT_UNNAMED; + attr_name_len = 0; + } + } + ntfs_log_verbose("attribute type = 0x%x\n", (unsigned int)le32_to_cpu(attr_type)); + if (attr_name == AT_UNNAMED) + ntfs_log_verbose("attribute name = \"\" (UNNAMED)\n"); + else + ntfs_log_verbose("attribute name = \"%s\" (length %u Unicode " + "characters)\n", s2, + (unsigned int)attr_name_len); + + /* Get the new length. */ + ll = strtoll(s, &s2, 0); + if (*s2 || ll < 0 || (ll >= LLONG_MAX && errno == ERANGE)) + err_exit("Invalid new length: %s\n", s); + new_len = ll; + ntfs_log_verbose("new length = %lli\n", (long long)new_len); +} + +/** + * ucstos - convert unicode-character string to ASCII + * @dest: points to buffer to receive the converted string + * @src: points to string to convert + * @maxlen: size of @dest buffer in bytes + * + * Return the number of characters written to @dest, not including the + * terminating null byte. If a unicode character was encountered which could + * not be converted -1 is returned. + */ +static int ucstos(char *dest, const ntfschar *src, int maxlen) +{ + u16 u; + int i; + + /* Need one byte for null terminator. */ + maxlen--; + for (i = 0; i < maxlen; i++) { + u = le16_to_cpu(src[i]); + if (!u) + break; + if (u & 0xff00) + return -1; + dest[i] = u & 0xff; + } + dest[i] = 0; + return i; +} + +/** + * dump_resident_attr_val + */ +static void dump_resident_attr_val(ATTR_TYPES type, char *val, u32 val_len) +{ + const char *don_t_know = "Don't know what to do with this attribute " + "type yet."; + const char *skip = "Skipping display of $%s attribute value.\n"; + const char *todo = "This is still work in progress."; + char *buf; + int i, j; + VOLUME_FLAGS flags; + u32 u; + + switch (type) { + case AT_STANDARD_INFORMATION: + // TODO + printf("%s\n", todo); + return; + case AT_ATTRIBUTE_LIST: + // TODO + printf("%s\n", todo); + return; + case AT_FILE_NAME: + // TODO + printf("%s\n", todo); + return; + case AT_OBJECT_ID: + // TODO + printf("%s\n", todo); + return; + case AT_SECURITY_DESCRIPTOR: + // TODO + printf("%s\n", todo); + return; + case AT_VOLUME_NAME: + printf("Volume name length = %u\n", (unsigned int)val_len); + if (val_len) { + buf = calloc(1, val_len); + if (!buf) + err_exit("Failed to allocate internal buffer: " + "%s\n", strerror(errno)); + i = ucstos(buf, (ntfschar*)val, val_len); + if (i == -1) + printf("Volume name contains non-displayable " + "Unicode characters.\n"); + printf("Volume name = %s\n", buf); + free(buf); + } + return; + case AT_VOLUME_INFORMATION: +#define VOL_INF(x) ((VOLUME_INFORMATION *)(x)) + printf("NTFS version %i.%i\n", VOL_INF(val)->major_ver, + VOL_INF(val)->minor_ver); + flags = VOL_INF(val)->flags; +#undef VOL_INF + printf("Volume flags = 0x%x: ", le16_to_cpu(flags)); + if (!flags) { + printf("NONE\n"); + return; + } + j = 0; + if (flags & VOLUME_MODIFIED_BY_CHKDSK) { + j = 1; + printf("VOLUME_MODIFIED_BY_CHKDSK"); + } + if (flags & VOLUME_REPAIR_OBJECT_ID) { + if (j) + printf(" | "); + else + j = 0; + printf("VOLUME_REPAIR_OBJECT_ID"); + } + if (flags & VOLUME_DELETE_USN_UNDERWAY) { + if (j) + printf(" | "); + else + j = 0; + printf("VOLUME_DELETE_USN_UNDERWAY"); + } + if (flags & VOLUME_MOUNTED_ON_NT4) { + if (j) + printf(" | "); + else + j = 0; + printf("VOLUME_MOUNTED_ON_NT4"); + } + if (flags & VOLUME_UPGRADE_ON_MOUNT) { + if (j) + printf(" | "); + else + j = 0; + printf("VOLUME_UPGRADE_ON_MOUNT"); + } + if (flags & VOLUME_RESIZE_LOG_FILE) { + if (j) + printf(" | "); + else + j = 0; + printf("VOLUME_RESIZE_LOG_FILE"); + } + if (flags & VOLUME_IS_DIRTY) { + if (j) + printf(" | "); + else + j = 0; + printf("VOLUME_IS_DIRTY"); + } + printf("\n"); + return; + case AT_DATA: + printf(skip, "DATA"); + return; + case AT_INDEX_ROOT: + // TODO + printf("%s\n", todo); + return; + case AT_INDEX_ALLOCATION: + // TODO + printf("%s\n", todo); + return; + case AT_BITMAP: + printf(skip, "BITMAP"); + return; + case AT_REPARSE_POINT: + // TODO + printf("%s\n", todo); + return; + case AT_EA_INFORMATION: + // TODO + printf("%s\n", don_t_know); + return; + case AT_EA: + // TODO + printf("%s\n", don_t_know); + return; + case AT_LOGGED_UTILITY_STREAM: + // TODO + printf("%s\n", don_t_know); + return; + default: + u = le32_to_cpu(type); + printf("Cannot display unknown %s defined attribute type 0x%x" + ".\n", u >= + le32_to_cpu(AT_FIRST_USER_DEFINED_ATTRIBUTE) ? + "user" : "system", (unsigned int)u); + } +} + +/** + * dump_resident_attr + */ +static void dump_resident_attr(ATTR_RECORD *a) +{ + int i; + + i = le32_to_cpu(a->value_length); + printf("Attribute value length = %u (0x%x)\n", i, i); + i = le16_to_cpu(a->value_offset); + printf("Attribute value offset = %u (0x%x)\n", i, i); + i = a->resident_flags; + printf("Resident flags = 0x%x: ", i); + if (!i) + printf("NONE\n"); + else if (i & ~RESIDENT_ATTR_IS_INDEXED) + printf("UNKNOWN FLAG(S)\n"); + else + printf("RESIDENT_ATTR_IS_INDEXED\n"); + dump_resident_attr_val(a->type, (char*)a + le16_to_cpu(a->value_offset), + le32_to_cpu(a->value_length)); +} + +/** + * dump_mapping_pairs_array + */ +static void dump_mapping_pairs_array(char *b __attribute__((unused)), + unsigned int max_len __attribute__((unused))) +{ + // TODO + return; +} + +/** + * dump_non_resident_attr + */ +static void dump_non_resident_attr(ATTR_RECORD *a) +{ + s64 l; + int i; + + l = sle64_to_cpu(a->lowest_vcn); + printf("Lowest VCN = %lli (0x%llx)\n", (long long)l, + (unsigned long long)l); + l = sle64_to_cpu(a->highest_vcn); + printf("Highest VCN = %lli (0x%llx)\n", (long long)l, + (unsigned long long)l); + printf("Mapping pairs array offset = 0x%x\n", + le16_to_cpu(a->mapping_pairs_offset)); + printf("Compression unit = 0x%x: %sCOMPRESSED\n", a->compression_unit, + a->compression_unit ? "" : "NOT "); + if (sle64_to_cpu(a->lowest_vcn)) + printf("Attribute is not the first extent. The following " + "sizes are meaningless:\n"); + l = sle64_to_cpu(a->allocated_size); + printf("Allocated size = %lli (0x%llx)\n", (long long)l, + (unsigned long long)l); + l = sle64_to_cpu(a->data_size); + printf("Data size = %lli (0x%llx)\n", (long long)l, + (unsigned long long)l); + l = sle64_to_cpu(a->initialized_size); + printf("Initialized size = %lli (0x%llx)\n", (long long)l, + (unsigned long long)l); + if (a->flags & ATTR_COMPRESSION_MASK) { + l = sle64_to_cpu(a->compressed_size); + printf("Compressed size = %lli (0x%llx)\n", (long long)l, + (unsigned long long)l); + } + i = le16_to_cpu(a->mapping_pairs_offset); + dump_mapping_pairs_array((char*)a + i, le32_to_cpu(a->length) - i); +} + +/** + * dump_attr_record + */ +static void dump_attr_record(MFT_RECORD *m, ATTR_RECORD *a) +{ + unsigned int u; + char s[0x200]; + int i; + ATTR_FLAGS flags; + + printf("-- Beginning dump of attribute record at offset 0x%x. --\n", + (unsigned)((u8*)a - (u8*)m)); + if (a->type == AT_END) { + printf("Attribute type = 0x%x ($END)\n", + (unsigned int)le32_to_cpu(AT_END)); + u = le32_to_cpu(a->length); + printf("Length of resident part = %u (0x%x)\n", u, u); + return; + } + u = le32_to_cpu(a->type); + for (i = 0; attr_defs[i].type; i++) + if (le32_to_cpu(attr_defs[i].type) >= u) + break; + if (attr_defs[i].type) { +// printf("type = 0x%x\n", le32_to_cpu(attr_defs[i].type)); +// { char *p = (char*)attr_defs[i].name; +// printf("name = %c%c%c%c%c\n", *p, p[1], p[2], p[3], p[4]); +// } + if (ucstos(s, attr_defs[i].name, sizeof(s)) == -1) { + ntfs_log_error("Could not convert Unicode string to single " + "byte string in current locale.\n"); + strncpy(s, "Error converting Unicode string", + sizeof(s)); + } + } else + strncpy(s, "UNKNOWN_TYPE", sizeof(s)); + printf("Attribute type = 0x%x (%s)\n", u, s); + u = le32_to_cpu(a->length); + printf("Length of resident part = %u (0x%x)\n", u, u); + printf("Attribute is %sresident\n", a->non_resident ? "non-" : ""); + printf("Name length = %u unicode characters\n", a->name_length); + printf("Name offset = %u (0x%x)\n", le16_to_cpu(a->name_offset), + le16_to_cpu(a->name_offset)); + flags = a->flags; + if (a->name_length) { + if (ucstos(s, (ntfschar*)((char*)a + + le16_to_cpu(a->name_offset)), + min((int)sizeof(s), + a->name_length + 1)) == -1) { + ntfs_log_error("Could not convert Unicode string to single " + "byte string in current locale.\n"); + strncpy(s, "Error converting Unicode string", + sizeof(s)); + + } + printf("Name = %s\n", s); + } + printf("Attribute flags = 0x%x: ", le16_to_cpu(flags)); + if (!flags) + printf("NONE"); + else { + int first = TRUE; + if (flags & ATTR_COMPRESSION_MASK) { + if (flags & ATTR_IS_COMPRESSED) { + printf("ATTR_IS_COMPRESSED"); + first = FALSE; + } + if ((flags & ATTR_COMPRESSION_MASK) & ~ATTR_IS_COMPRESSED) { + if (!first) + printf(" | "); + else + first = FALSE; + printf("ATTR_UNKNOWN_COMPRESSION"); + } + } + if (flags & ATTR_IS_ENCRYPTED) { + if (!first) + printf(" | "); + else + first = FALSE; + printf("ATTR_IS_ENCRYPTED"); + } + if (flags & ATTR_IS_SPARSE) { + if (!first) + printf(" | "); + else + first = FALSE; + printf("ATTR_IS_SPARSE"); + } + } + printf("\n"); + printf("Attribute instance = %u\n", le16_to_cpu(a->instance)); + if (a->non_resident) { + dump_non_resident_attr(a); + } else { + dump_resident_attr(a); + } +} + +/** + * dump_mft_record + */ +static void dump_mft_record(MFT_RECORD *m) +{ + ATTR_RECORD *a; + unsigned int u; + MFT_REF r; + + printf("-- Beginning dump of mft record. --\n"); + u = le32_to_cpu(m->magic); + printf("Mft record signature (magic) = %c%c%c%c\n", u & 0xff, + u >> 8 & 0xff, u >> 16 & 0xff, u >> 24 & 0xff); + u = le16_to_cpu(m->usa_ofs); + printf("Update sequence array offset = %u (0x%x)\n", u, u); + printf("Update sequence array size = %u\n", le16_to_cpu(m->usa_count)); + printf("$LogFile sequence number (lsn) = %llu\n", + (unsigned long long)sle64_to_cpu(m->lsn)); + printf("Sequence number = %u\n", le16_to_cpu(m->sequence_number)); + printf("Reference (hard link) count = %u\n", + le16_to_cpu(m->link_count)); + u = le16_to_cpu(m->attrs_offset); + printf("First attribute offset = %u (0x%x)\n", u, u); + printf("Flags = %u: ", le16_to_cpu(m->flags)); + if (m->flags & MFT_RECORD_IN_USE) + printf("MFT_RECORD_IN_USE"); + else + printf("MFT_RECORD_NOT_IN_USE"); + if (m->flags & MFT_RECORD_IS_DIRECTORY) + printf(" | MFT_RECORD_IS_DIRECTORY"); + printf("\n"); + u = le32_to_cpu(m->bytes_in_use); + printf("Bytes in use = %u (0x%x)\n", u, u); + u = le32_to_cpu(m->bytes_allocated); + printf("Bytes allocated = %u (0x%x)\n", u, u); + r = le64_to_cpu(m->base_mft_record); + printf("Base mft record reference:\n\tMft record number = %llu\n\t" + "Sequence number = %u\n", + (unsigned long long)MREF(r), MSEQNO(r)); + printf("Next attribute instance = %u\n", + le16_to_cpu(m->next_attr_instance)); + a = (ATTR_RECORD*)((char*)m + le16_to_cpu(m->attrs_offset)); + printf("-- Beginning dump of attributes within mft record. --\n"); + while ((char*)a < (char*)m + le32_to_cpu(m->bytes_in_use)) { + if (a->type == attr_type) + dump_attr_record(m, a); + if (a->type == AT_END) + break; + a = (ATTR_RECORD*)((char*)a + le32_to_cpu(a->length)); + }; + printf("-- End of attributes. --\n"); +} + +/** + * ntfstruncate_exit + */ +static void ntfstruncate_exit(void) +{ + int err; + + if (success) + return; + /* Close the attribute. */ + if (na) + ntfs_attr_close(na); + /* Close the inode. */ + if (ni && ntfs_inode_close(ni)) { + ntfs_log_perror("Warning: Failed to close inode %lli", + (long long)inode); + } + /* Unmount the volume. */ + err = ntfs_umount(vol, 0); + if (err == -1) + ntfs_log_perror("Warning: Could not umount %s", dev_name); + /* Free the attribute name if it exists. */ + ntfs_ucsfree(attr_name); +} + +/** + * main + */ +int main(int argc, char **argv) +{ + unsigned long mnt_flags, ul; + int err; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + /* Initialize opts to zero / required values. */ + memset(&opts, 0, sizeof(opts)); + + /* + * Setup a default $AttrDef. FIXME: Should be reading this from the + * volume itself, at ntfs_mount() time. + */ + attr_defs = (ATTR_DEF*)&attrdef_ntfs3x_array; + + /* Parse command line options. */ + parse_options(argc, argv); + + utils_set_locale(); + + /* Make sure the file system is not mounted. */ + if (ntfs_check_if_mounted(dev_name, &mnt_flags)) + ntfs_log_perror("Failed to determine whether %s is mounted", + dev_name); + else if (mnt_flags & NTFS_MF_MOUNTED) { + ntfs_log_error("%s is mounted.\n", dev_name); + if (!opts.force) + err_exit("Refusing to run!\n"); + fprintf(stderr, "ntfstruncate forced anyway. Hope /etc/mtab " + "is incorrect.\n"); + } + + /* Mount the device. */ + if (opts.no_action) { + ntfs_log_quiet("Running in READ-ONLY mode!\n"); + ul = NTFS_MNT_RDONLY; + } else + ul = 0; + vol = ntfs_mount(dev_name, ul); + if (!vol) + err_exit("Failed to mount %s: %s\n", dev_name, strerror(errno)); + + /* Register our exit function which will unlock and close the device. */ + err = atexit(&ntfstruncate_exit); + if (err == -1) { + ntfs_log_error("Could not set up exit() function because atexit() " + "failed: %s Aborting...\n", strerror(errno)); + ntfstruncate_exit(); + exit(1); + } + + /* Open the specified inode. */ + ni = ntfs_inode_open(vol, inode); + if (!ni) + err_exit("Failed to open inode %lli: %s\n", (long long)inode, + strerror(errno)); + + /* Open the specified attribute. */ + na = ntfs_attr_open(ni, attr_type, attr_name, attr_name_len); + if (!na) + err_exit("Failed to open attribute 0x%x: %s\n", + (unsigned int)le32_to_cpu(attr_type), strerror(errno)); + + if (!opts.quiet && opts.verbose > 1) { + ntfs_log_verbose("Dumping mft record before calling " + "ntfs_attr_truncate():\n"); + dump_mft_record(ni->mrec); + } + + /* Truncate the attribute. */ + err = ntfs_attr_truncate(na, new_len); + if (err) + err_exit("Failed to truncate attribute 0x%x: %s\n", + (unsigned int)le32_to_cpu(attr_type), strerror(errno)); + + if (!opts.quiet && opts.verbose > 1) { + ntfs_log_verbose("Dumping mft record after calling " + "ntfs_attr_truncate():\n"); + dump_mft_record(ni->mrec); + } + + /* Close the attribute. */ + ntfs_attr_close(na); + na = NULL; + + /* Close the inode. */ + err = ntfs_inode_close(ni); + if (err) + err_exit("Failed to close inode %lli: %s\n", (long long)inode, + strerror(errno)); + + /* Unmount the volume. */ + err = ntfs_umount(vol, 0); + if (err == -1) + ntfs_log_perror("Warning: Failed to umount %s", dev_name); + + /* Free the attribute name if it exists. */ + ntfs_ucsfree(attr_name); + + /* Finally, disable our ntfstruncate_exit() handler. */ + success = TRUE; + + ntfs_log_quiet("ntfstruncate completed successfully. Have a nice day.\n"); + return 0; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsundelete.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsundelete.8.in new file mode 100644 index 0000000..d95c4a0 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsundelete.8.in @@ -0,0 +1,324 @@ +.\" Copyright (c) 2002\-2005 Richard Russon. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSUNDELETE 8 "November 2005" "ntfs-3g @VERSION@" +.SH NAME +ntfsundelete \- recover a deleted file from an NTFS volume. +.SH SYNOPSIS +.B ntfsundelete +[\fIoptions\fR] \fIdevice\fR +.SH DESCRIPTION +.B ntfsundelete +has three modes of operation: +.IR scan , +.I undelete +and +.IR copy . +.SS Scan +.PP +The default mode, +.I scan +simply reads an NTFS Volume and looks for files that have been deleted. Then it +will print a list giving the inode number, name and size. +.SS Undelete +.PP +The +.I undelete +mode takes the files either matching the regular expression (option \-m) +or specified by the inode\-expressions and recovers as much of the data +as possible. It saves the result to another location. Partly for +safety, but mostly because NTFS write support isn't finished. +.SS Copy +.PP +This is a wizard's option. It will save a portion of the MFT to a file. This +probably only be useful when debugging +.I ntfsundelete +.SS Notes +.B ntfsundelete +only ever +.B reads +from the NTFS Volume. +.B ntfsundelete +will never change the volume. +.SH CAVEATS +.SS Miracles +.B ntfsundelete +cannot perform the impossible. +.PP +When a file is deleted the MFT Record is marked as not in use and the bitmap +representing the disk usage is updated. If the power isn't turned off +immediately, the free space, where the file used to live, may become +overwritten. Worse, the MFT Record may be reused for another file. If this +happens it is impossible to tell where the file was on disk. +.PP +Even if all the clusters of a file are not in use, there is no guarantee that +they haven't been overwritten by some short\-lived file. +.SS Locale +In NTFS all the filenames are stored as Unicode. They will be converted into +the current locale for display by +.BR ntfsundelete . +The utility has successfully displayed some Chinese pictogram filenames and then +correctly recovered them. +.SS Extended MFT Records +In rare circumstances, a single MFT Record will not be large enough to hold the +metadata describing a file (a file would have to be in hundreds of fragments +for this to happen). In these cases one MFT record may hold the filename, but +another will hold the information about the data. +.B ntfsundelete +will not try and piece together such records. It will simply show unnamed files +with data. +.SS Compressed and Encrypted Files +.B ntfsundelete +cannot recover compressed or encrypted files. When scanning for them, it will +display as being 0% recoverable. +.SS The Recovered File's Size and Date +To recover a file +.B ntfsundelete +has to read the file's metadata. Unfortunately, this isn't always intact. +When a file is deleted, the metadata can be left in an inconsistent state. e.g. +the file size may be zero; the dates of the file may be set to the time it was +deleted, or random. +.br +To be safe +.B ntfsundelete +will pick the largest file size it finds and write that to disk. It will also +try and set the file's date to the last modified date. This date may be the +correct last modified date, or something unexpected. +.SH OPTIONS +Below is a summary of all the options that +.B ntfsundelete +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-b\fR, \fB\-\-byte\fR NUM +If any clusters of the file cannot be recovered, the missing parts will be +filled with this byte. The default is zeros. +.TP +\fB\-C\fR, \fB\-\-case\fR +When scanning an NTFS volume, any filename matching (using the +.B \-\-match +option) is case\-insensitive. This option makes the matching case\-sensitive. +.TP +\fB\-c\fR, \fB\-\-copy\fR RANGE +This wizard's option will write a block of MFT FILE records to a file. The +default file is +.I mft +which will be created in the current directory. This option can be combined +with the +.B \-\-output +and +.B \-\-destination +options. +.TP +\fB\-d\fR, \fB\-\-destination\fR DIR +This option controls where to put the output file of the +.B \-\-undelete +and +.B \-\-copy +options. +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not overwriting an existing +file. Use this option with caution. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-i\fR, \fB\-\-inodes\fR RANGE +Recover the files with these inode numbers. +.I RANGE +can be a single inode number, several numbers separated by commas "," or a +range separated by a dash "\-". +.TP +\fB\-m\fR, \fB\-\-match\fR PATTERN +Filter the output by only looking for matching filenames. The pattern can +include the wildcards '?', match exactly one character or '*', match zero or +more characters. By default the matching is case\-insensitive. To make the +search case sensitive, use the +.B \-\-case +option. +.TP +\fB\-O\fR, \fB\-\-optimistic\fR +Recover parts of the file even if they are currently marked as in use. +.TP +\fB\-o\fR, \fB\-\-output\fR FILE +Use this option to set name of output file that +.B \-\-undelete +or +.B \-\-copy +will create. +.TP +\fB\-P\fR, \fB\-\-parent\fR +Display the parent directory of a deleted file. +.TP +\fB\-p\fR, \fB\-\-percentage\fR NUM +Filter the output of the +.B \-\-scan +option, by only matching files with a certain amount of recoverable content. +.B Please read the caveats section for more details. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Reduce the amount of output to a minimum. Naturally, it doesn't make sense to +combine this option with +.BR \-\-scan . +.TP +\fB\-s\fR, \fB\-\-scan\fR +Search through an NTFS volume and print a list of files that could be recovered. +This is the default action of +.BR ntfsundelete . +This list can be filtered by filename, size, percentage recoverable or last +modification time, using the +.BR \-\-match , +.BR \-\-size , +.B \-\-percent +and +.B \-\-time +options, respectively. +.sp +The output of scan will be: +.sp +.nf +Inode Flags %age Date Time Size Filename + 6038 FN.. 93% 2002\-07\-17 13:42 26629 thesis.doc +.fi +.TS +box; +lB lB +l l. +Flag Description +F/D File/Directory +N/R (Non\-)Resident data stream +C/E Compressed/Encrypted data stream +! Missing attributes +.TE +.sp +.sp +The percentage field shows how much of the file can potentially be recovered. +.TP +\fB\-S\fR, \fB\-\-size\fR RANGE +Filter the output of the +.B \-\-scan +option, by looking for a particular range of file sizes. The range may be +specified as two numbers separated by a '\-'. The sizes may be abbreviated +using the suffixes k, m, g, t, for kilobytes, megabytes, gigabytes and terabytes +respectively. +.TP +\fB\-t\fR, \fB\-\-time\fR SINCE +Filter the output of the +.B \-\-scan +option. Only match files that have been altered since this time. The time must +be given as number using a suffix of d, w, m, y for days, weeks, months or years +ago. +.TP +\fB\-T\fR, \fB\-\-truncate\fR +If +.B ntfsundelete +is confident about the size of a deleted file, then it will restore the file to +exactly that size. The default behaviour is to round up the size to the nearest +cluster (which will be a multiple of 512 bytes). +.TP +\fB\-u\fR, \fB\-\-undelete\fR +Select +.B undelete +mode. You can specify the files to be recovered using by using +.B \-\-match +or +.B \-\-inodes +options. This option can be combined with +.BR \-\-output , +.BR \-\-destination , +and +.BR \-\-byte . +.sp +When the file is recovered it will be given its original name, unless the +.B \-\-output +option is used. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Increase the amount of output that +.B ntfsundelete +prints. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license for +.BR ntfsundelete . +.SH EXAMPLES +Look for deleted files on /dev/hda1. +.RS +.sp +.B ntfsundelete /dev/hda1 +.sp +.RE +Look for deleted documents on /dev/hda1. +.RS +.sp +.B ntfsundelete /dev/hda1 \-s \-m '*.doc' +.sp +.RE +Look for deleted files between 5000 and 6000000 bytes, with at least 90% of the +data recoverable, on /dev/hda1. +.RS +.sp +.B ntfsundelete /dev/hda1 \-S 5k\-6m \-p 90 +.sp +.RE +Look for deleted files altered in the last two days +.RS +.sp +.B ntfsundelete /dev/hda1 \-t 2d +.sp +.RE +Undelete inodes 2, 5 and 100 to 131 of device /dev/sda1 +.RS +.sp +.B ntfsundelete /dev/sda1 \-u \-i 2,5,100\-131 +.sp +.RE +Undelete inode number 3689, call the file 'work.doc', set it to recovered +size and put it in the user's home directory. +.RS +.sp +.B ntfsundelete /dev/hda1 \-u \-T \-i 3689 \-o work.doc \-d ~ +.sp +.RE +Save MFT Records 3689 to 3690 to a file 'debug' +.RS +.sp +.B ntfsundelete /dev/hda1 \-c 3689\-3690 \-o debug +.sp +.RE +.SH BUGS +There are some small limitations to +.BR ntfsundelete , +but currently no known bugs. If you find a bug please send an email describing +the problem to the development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfsundelete +was written by Richard Russon and Holger Ohmacht, with contributions from Anton +Altaparmakov. +It was ported to ntfs-3g by Erik Larsson and Jean-Pierre Andre. +.SH AVAILABILITY +.B ntfsundelete +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfsinfo (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsundelete.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsundelete.c new file mode 100644 index 0000000..caa1a12 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsundelete.c @@ -0,0 +1,2535 @@ +/** + * ntfsundelete - Part of the Linux-NTFS project. + * + * Copyright (c) 2002-2005 Richard Russon + * Copyright (c) 2004-2005 Holger Ohmacht + * Copyright (c) 2005 Anton Altaparmakov + * Copyright (c) 2007 Yura Pakhuchiy + * Copyright (c) 2013-2018 Jean-Pierre Andre + * + * This utility will recover deleted files from an NTFS volume. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_FEATURES_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif +#ifdef HAVE_UTIME_H +#include +#endif +#ifdef HAVE_REGEX_H +#include +#endif + +#if !defined(REG_NOERROR) || (REG_NOERROR != 0) +#define REG_NOERROR 0 +#endif + +#ifndef REG_NOMATCH +#define REG_NOMATCH 1 +#endif + +#include "ntfsundelete.h" +#include "bootsect.h" +#include "mft.h" +#include "attrib.h" +#include "layout.h" +#include "inode.h" +#include "device.h" +#include "utils.h" +#include "debug.h" +#include "ntfstime.h" +/* #include "version.h" */ +#include "logging.h" +#include "misc.h" + +#ifdef HAVE_WINDOWS_H +/* + * Replacements for functions which do not exist on Windows + */ +#define ftruncate(fd, size) ntfs_win32_ftruncate(fd, size) +#endif + +static const char *EXEC_NAME = "ntfsundelete"; +static const char *MFTFILE = "mft"; +static const char *UNNAMED = ""; +static const char *NONE = ""; +static const char *UNKNOWN = "unknown"; +static struct options opts; + +typedef struct +{ + u32 begin; + u32 end; +} range; + +static short with_regex; /* Flag Regular expression available */ +static short avoid_duplicate_printing; /* Flag No duplicate printing of file infos */ +static range *ranges; /* Array containing all Inode-Ranges for undelete */ +static long nr_entries; /* Number of range entries */ + +#ifdef HAVE_WINDOWS_H +/* + * Replacement for strftime() on Windows + * + * strftime() on Windows uses format codes different from those + * defined in C99 sect. 7.23.3.5 + * Use snprintf() instead. + */ +static int win32_strftime(char *buffer, int size, const char *format, + const struct tm *ptm) +{ + int ret; + + if (!strcmp(format, "%F %R")) + ret = snprintf(buffer, size, "%4d-%02d-%02d %02d:%02d", + ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, + ptm->tm_hour, ptm->tm_min); + else + ret = snprintf(buffer, size, "%4d-%02d-%02d", + ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday); + return (ret); +} +#define strftime(buf, sz, fmt, ptm) win32_strftime(buf, sz, fmt, ptm) +#endif + +#ifndef HAVE_REGEX_H + +/* + * Pattern matching routing for systems with no regex. + */ + +typedef struct REGEX { + ntfschar *upcase; + u32 upcase_len; + int flags; + int pattern_len; + ntfschar pattern[1]; +} *regex_t; + +enum { REG_NOSUB = 1, REG_ICASE = 2 }; + +static BOOL patmatch(regex_t *re, const ntfschar *f, int flen, + const ntfschar *p, int plen, BOOL dot) +{ + regex_t pre; + BOOL ok; + BOOL anyextens; + int i; + unsigned int c; + + pre = *re; + if (pre->flags & REG_ICASE) { + while ((flen > 0) && (plen > 0) + && ((*f == *p) + || (*p == const_cpu_to_le16('?')) + || ((c = le16_to_cpu(*f)) < pre->upcase_len + ? pre->upcase[c] : *f) == *p)) { + flen--; + if (*f++ == const_cpu_to_le16('.')) + dot = TRUE; + plen--; + p++; + } + } else { + while ((flen > 0) && (plen > 0) + && ((*f == *p) || (*p == const_cpu_to_le16('?')))) { + flen--; + if (*f++ == const_cpu_to_le16('.')) + dot = TRUE; + plen--; + p++; + } + } + if ((flen <= 0) && (plen <= 0)) + ok = TRUE; + else { + ok = FALSE; + plen--; + if (*p++ == const_cpu_to_le16('*')) { + /* special case "*.*" requires the end or a dot */ + anyextens = FALSE; + if ((plen == 2) + && (p[0] == const_cpu_to_le16('.')) + && (p[1] == const_cpu_to_le16('*')) + && !dot) { + for (i=0; (i 0) && !ok) + if (patmatch(re,f,flen,p,plen,dot)) + ok = TRUE; + else { + flen--; + f++; + } + } + } + return (ok); +} + +static int regcomp(regex_t *re, const char *pattern, int flags) +{ + regex_t pre; + ntfschar *rp; + ntfschar *p; + unsigned int c; + int lth; + int i; + + pre = (regex_t)malloc(sizeof(struct REGEX) + + strlen(pattern)*sizeof(ntfschar)); + *re = pre; + if (pre) { + pre->flags = flags; + pre->upcase_len = 0; + rp = pre->pattern; + lth = ntfs_mbstoucs(pattern, &rp); + pre->pattern_len = lth; + p = pre->pattern; + if (flags & REG_ICASE) { + for (i=0; iupcase_len) + *p = pre->upcase[c]; + p++; + } + } + } + return (*re && (lth > 0) ? 0 : -1); +} + +static int regexec(regex_t *re, const ntfschar *uname, int len, + char *q __attribute__((unused)), int r __attribute__((unused))) +{ + BOOL m; + + m = patmatch(re, uname, len, (*re)->pattern, (*re)->pattern_len, FALSE); + return (m ? REG_NOERROR : REG_NOMATCH); +} + +static void regfree(regex_t *re) +{ + free(*re); +} + +#endif + +/** + * parse_inode_arg - parses the inode expression + * + * Parses the optarg after parameter -u for valid ranges + * + * Return: Number of correct inode specifications or -1 for error + */ +static int parse_inode_arg(void) +{ + int p; + u32 range_begin; + u32 range_end; + u32 range_temp; + u32 inode; + char *opt_arg_ptr; + char *opt_arg_temp; + char *opt_arg_end1; + char *opt_arg_end2; + + /* Check whether optarg is available or not */ + nr_entries = 0; + if (optarg == NULL) + return (0); /* bailout if no optarg */ + + /* init variables */ + p = strlen(optarg); + opt_arg_ptr = optarg; + opt_arg_end1 = optarg; + opt_arg_end2 = &(optarg[p]); + + /* alloc mem for range table */ + ranges = (range *) malloc((p + 1) * sizeof(range)); + if (ranges == NULL) { + ntfs_log_error("ERROR: Couldn't alloc mem for parsing inodes!\n"); + return (-1); + } + + /* loop */ + while ((opt_arg_end1 != opt_arg_end2) && (p > 0)) { + /* Try to get inode */ + inode = strtoul(opt_arg_ptr, &opt_arg_end1, 0); + p--; + + /* invalid char at begin */ + if ((opt_arg_ptr == opt_arg_end1) || (opt_arg_ptr == opt_arg_end2)) { + ntfs_log_error("ERROR: Invalid Number: %s\n", opt_arg_ptr); + return (-1); + } + + /* RANGE - Check for range */ + if (opt_arg_end1[0] == '-') { + /* get range end */ + opt_arg_temp = opt_arg_end1; + opt_arg_end1 = & (opt_arg_temp[1]); + if (opt_arg_temp >= opt_arg_end2) { + ntfs_log_error("ERROR: Missing range end!\n"); + return (-1); + } + range_begin = inode; + + /* get count */ + range_end = strtoul(opt_arg_end1, &opt_arg_temp, 0); + if (opt_arg_temp == opt_arg_end1) { + ntfs_log_error("ERROR: Invalid Number: %s\n", opt_arg_temp); + return (-1); + } + + /* check for correct values */ + if (range_begin > range_end) { + range_temp = range_end; + range_end = range_begin; + range_begin = range_temp; + } + + /* put into struct */ + ranges[nr_entries].begin = range_begin; + ranges[nr_entries].end = range_end; + nr_entries++; + + /* Last check */ + opt_arg_ptr = & (opt_arg_temp[1]); + if (opt_arg_ptr >= opt_arg_end2) + break; + } else if (opt_arg_end1[0] == ',') { + /* SINGLE VALUE, BUT CONTINUING */ + /* put inode into range list */ + ranges[nr_entries].begin = inode; + ranges[nr_entries].end = inode; + nr_entries++; + + /* Next inode */ + opt_arg_ptr = & (opt_arg_end1[1]); + if (opt_arg_ptr >= opt_arg_end2) { + ntfs_log_error("ERROR: Missing new value at end of input!\n"); + return (-1); + } + continue; + } else { /* SINGLE VALUE, END */ + ranges[nr_entries].begin = inode; + ranges[nr_entries].end = inode; + nr_entries++; + } + } + return (nr_entries); +} + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + ntfs_log_info("\n%s v%s (libntfs-3g) - Recover deleted files from an " + "NTFS Volume.\n\n", EXEC_NAME, VERSION); + ntfs_log_info("Copyright (c) 2002-2005 Richard Russon\n" + "Copyright (c) 2004-2005 Holger Ohmacht\n" + "Copyright (c) 2005 Anton Altaparmakov\n" + "Copyright (c) 2007 Yura Pakhuchiy\n" + "Copyright (c) 2013-2018 Jean-Pierre Andre\n"); + ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + ntfs_log_info("\nUsage: %s [options] device\n" + " -s, --scan Scan for files (default)\n" + " -p, --percentage NUM Minimum percentage recoverable\n" + " -m, --match PATTERN Only work on files with matching names\n" + " -C, --case Case sensitive matching\n" + " -S, --size RANGE Match files of this size\n" + " -t, --time SINCE Last referenced since this time\n" + "\n" + " -u, --undelete Undelete mode\n" + " -i, --inodes RANGE Recover these inodes\n" + //" -I, --interactive Interactive mode\n" + " -o, --output FILE Save with this filename\n" + " -O, --optimistic Undelete in-use clusters as well\n" + " -d, --destination DIR Destination directory\n" + " -b, --byte NUM Fill missing parts with this byte\n" + " -T, --truncate Truncate 100%% recoverable file to exact size.\n" + " -P, --parent Show parent directory\n" + "\n" + " -c, --copy RANGE Write a range of MFT records to a file\n" + "\n" + " -f, --force Use less caution\n" + " -q, --quiet Less output\n" + " -v, --verbose More output\n" + " -V, --version Display version information\n" + " -h, --help Display this help\n\n", + EXEC_NAME); + ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * transform - Convert a shell style pattern to a regex + * @pattern: String to be converted + * @regex: Resulting regular expression is put here + * + * This will transform patterns, such as "*.doc" to true regular expressions. + * The function will also place '^' and '$' around the expression to make it + * behave as the user would expect + * + * Before After + * . \. + * * .* + * ? . + * + * Notes: + * The returned string must be freed by the caller. + * If transform fails, @regex will not be changed. + * + * Return: 1, Success, the string was transformed + * 0, An error occurred + */ +static int transform(const char *pattern, char **regex) +{ + char *result; + int length, i; +#ifdef HAVE_REGEX_H + int j; +#endif + + if (!pattern || !regex) + return 0; + + length = strlen(pattern); + if (length < 1) { + ntfs_log_error("Pattern to transform is empty\n"); + return 0; + } + + for (i = 0; pattern[i]; i++) { + if ((pattern[i] == '*') || (pattern[i] == '.')) + length++; + } + + result = malloc(length + 3); + if (!result) { + ntfs_log_error("Couldn't allocate memory in transform()\n"); + return 0; + } + +#ifdef HAVE_REGEX_H + result[0] = '^'; + + for (i = 0, j = 1; pattern[i]; i++, j++) { + if (pattern[i] == '*') { + result[j] = '.'; + j++; + result[j] = '*'; + } else if (pattern[i] == '.') { + result[j] = '\\'; + j++; + result[j] = '.'; + } else if (pattern[i] == '?') { + result[j] = '.'; + } else { + result[j] = pattern[i]; + } + } + + result[j] = '$'; + result[j+1] = 0; + ntfs_log_debug("Pattern '%s' replaced with regex '%s'.\n", pattern, + result); +#else + strcpy(result, pattern); +#endif + + *regex = result; + return 1; +} + +/** + * parse_time - Convert a time abbreviation to seconds + * @string: The string to be converted + * @since: The absolute time referred to + * + * Strings representing times will be converted into a time_t. The numbers will + * be regarded as seconds unless suffixed. + * + * Suffix Description + * [yY] Year + * [mM] Month + * [wW] Week + * [dD] Day + * [sS] Second + * + * Therefore, passing "1W" will return the time_t representing 1 week ago. + * + * Notes: + * Only the first character of the suffix is read. + * If parse_time fails, @since will not be changed + * + * Return: 1 Success + * 0 Error, the string was malformed + */ +static int parse_time(const char *value, time_t *since) +{ + long long result; + time_t now; + char *suffix = NULL; + + if (!value || !since) + return -1; + + ntfs_log_trace("Parsing time '%s' ago.\n", value); + + result = strtoll(value, &suffix, 10); + if (result < 0 || errno == ERANGE) { + ntfs_log_error("Invalid time '%s'.\n", value); + return 0; + } + + if (!suffix) { + ntfs_log_error("Internal error, strtoll didn't return a suffix.\n"); + return 0; + } + + if (strlen(suffix) > 1) { + ntfs_log_error("Invalid time suffix '%s'. Use Y, M, W, D or H.\n", suffix); + return 0; + } + + switch (suffix[0]) { + case 'y': case 'Y': result *= 12; + /* FALLTHRU */ + case 'm': case 'M': result *= 4; + /* FALLTHRU */ + case 'w': case 'W': result *= 7; + /* FALLTHRU */ + case 'd': case 'D': result *= 24; + /* FALLTHRU */ + case 'h': case 'H': result *= 3600; + /* FALLTHRU */ + case 0: + break; + + default: + ntfs_log_error("Invalid time suffix '%s'. Use Y, M, W, D or H.\n", suffix); + return 0; + } + + now = time(NULL); + + ntfs_log_debug("Time now = %lld, Time then = %lld.\n", (long long) now, + (long long) result); + *since = now - result; + return 1; +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char *argv[]) +{ + static const char *sopt = "-b:Cc:d:fh?i:m:o:OPp:sS:t:TuqvV"; + static const struct option lopt[] = { + { "byte", required_argument, NULL, 'b' }, + { "case", no_argument, NULL, 'C' }, + { "copy", required_argument, NULL, 'c' }, + { "destination", required_argument, NULL, 'd' }, + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "inodes", required_argument, NULL, 'i' }, + //{ "interactive", no_argument, NULL, 'I' }, + { "match", required_argument, NULL, 'm' }, + { "optimistic", no_argument, NULL, 'O' }, + { "output", required_argument, NULL, 'o' }, + { "parent", no_argument, NULL, 'P' }, + { "percentage", required_argument, NULL, 'p' }, + { "quiet", no_argument, NULL, 'q' }, + { "scan", no_argument, NULL, 's' }, + { "size", required_argument, NULL, 'S' }, + { "time", required_argument, NULL, 't' }, + { "truncate", no_argument, NULL, 'T' }, + { "undelete", no_argument, NULL, 'u' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + + int c = -1; + char *end = NULL; + int err = 0; + int ver = 0; + int help = 0; + int levels = 0; + + opterr = 0; /* We'll handle the errors, thank you. */ + + opts.mode = MODE_NONE; + opts.uinode = -1; + opts.percent = -1; + opts.fillbyte = -1; + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opts.device) { + opts.device = argv[optind-1]; + } else { + opts.device = NULL; + err++; + } + break; + case 'b': + if (opts.fillbyte == (char)-1) { + end = NULL; + opts.fillbyte = strtol(optarg, &end, 0); + if (end && *end) + err++; + } else { + err++; + } + break; + case 'C': + opts.match_case++; + break; + case 'c': + if (opts.mode == MODE_NONE) { + if (!utils_parse_range(optarg, + &opts.mft_begin, &opts.mft_end, TRUE)) + err++; + opts.mode = MODE_COPY; + } else { + opts.mode = MODE_ERROR; + } + break; + case 'd': + if (!opts.dest) + opts.dest = optarg; + else + err++; + break; + case 'f': + opts.force++; + break; + case 'h': + help++; + break; + case '?': + if (ntfs_log_parse_option (argv[optind-1])) + break; + ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); + err++; + break; + case 'i': + end = NULL; + /* parse inodes */ + if (parse_inode_arg() == -1) + err++; + if (end && *end) + err++; + break; + case 'm': + if (!opts.match) { + if (!transform(optarg, &opts.match)) { + err++; + } else { + /* set regex-flag on true ;) */ + with_regex= 1; + } + } else { + err++; + } + break; + case 'o': + if (!opts.output) { + opts.output = optarg; + } else { + err++; + } + break; + case 'O': + if (!opts.optimistic) { + opts.optimistic++; + } else { + err++; + } + break; + case 'P': + if (!opts.parent) { + opts.parent++; + } else { + err++; + } + break; + case 'p': + if (opts.percent == -1) { + end = NULL; + opts.percent = strtol(optarg, &end, 0); + if (end && ((*end != '%') && (*end != 0))) + err++; + } else { + err++; + } + break; + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 's': + if (opts.mode == MODE_NONE) + opts.mode = MODE_SCAN; + else + opts.mode = MODE_ERROR; + break; + case 'S': + if ((opts.size_begin > 0) || (opts.size_end > 0) || + !utils_parse_range(optarg, &opts.size_begin, + &opts.size_end, TRUE)) { + err++; + } + break; + case 't': + if (opts.since == 0) { + if (!parse_time(optarg, &opts.since)) + err++; + } else { + err++; + } + break; + case 'T': + opts.truncate++; + break; + case 'u': + if (opts.mode == MODE_NONE) { + opts.mode = MODE_UNDELETE; + } else { + opts.mode = MODE_ERROR; + } + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + ver++; + break; + default: + if (((optopt == 'b') || (optopt == 'c') || + (optopt == 'd') || (optopt == 'm') || + (optopt == 'o') || (optopt == 'p') || + (optopt == 'S') || (optopt == 't') || + (optopt == 'u')) && (!optarg)) { + ntfs_log_error("Option '%s' requires an argument.\n", argv[optind-1]); + } else { + ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); + } + err++; + break; + } + } + + /* Make sure we're in sync with the log levels */ + levels = ntfs_log_get_levels(); + if (levels & NTFS_LOG_LEVEL_VERBOSE) + opts.verbose++; + if (!(levels & NTFS_LOG_LEVEL_QUIET)) + opts.quiet++; + + if (help || ver) { + opts.quiet = 0; + } else { + if (opts.device == NULL) { + if (argc > 1) + ntfs_log_error("You must specify exactly one device.\n"); + err++; + } + + if (opts.mode == MODE_NONE) { + opts.mode = MODE_SCAN; + } + + switch (opts.mode) { + case MODE_SCAN: + if (opts.output || opts.dest || opts.truncate || + (opts.fillbyte != (char)-1)) { + ntfs_log_error("Scan can only be used with --percent, " + "--match, --ignore-case, --size and --time.\n"); + err++; + } + if (opts.match_case && !opts.match) { + ntfs_log_error("The --case option doesn't make sense without the --match option\n"); + err++; + } + break; + + case MODE_UNDELETE: + /*if ((opts.percent != -1) || (opts.size_begin > 0) || (opts.size_end > 0)) { + ntfs_log_error("Undelete can only be used with " + "--output, --destination, --byte and --truncate.\n"); + err++; + }*/ + break; + case MODE_COPY: + if ((opts.fillbyte != (char)-1) || opts.truncate || + (opts.percent != -1) || + opts.match || opts.match_case || + (opts.size_begin > 0) || + (opts.size_end > 0)) { + ntfs_log_error("Copy can only be used with --output and --destination.\n"); + err++; + } + break; + default: + ntfs_log_error("You can only select one of Scan, Undelete or Copy.\n"); + err++; + } + + if ((opts.percent < -1) || (opts.percent > 100)) { + ntfs_log_error("Percentage value must be in the range 0 - 100.\n"); + err++; + } + + if (opts.quiet) { + if (opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose at the same time.\n"); + err++; + } else if (opts.mode == MODE_SCAN) { + ntfs_log_error("You may not use --quiet when scanning a volume.\n"); + err++; + } + } + + if (opts.parent && !opts.verbose) { + ntfs_log_error("To use --parent, you must also use --verbose.\n"); + err++; + } + } + + if (opts.fillbyte == (char)-1) + opts.fillbyte = 0; + + if (ver) + version(); + if (help || err) + usage(); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver ? 0 : -1)); +} + +/** + * free_file - Release the resources used by a file object + * @file: The unwanted file object + * + * This will free up the memory used by a file object and iterate through the + * object's children, freeing their resources too. + * + * Return: none + */ +static void free_file(struct ufile *file) +{ + struct ntfs_list_head *item, *tmp; + + if (!file) + return; + + ntfs_list_for_each_safe(item, tmp, &file->name) { + /* List of filenames */ + struct filename *f = ntfs_list_entry(item, struct filename, list); + ntfs_log_debug("freeing filename '%s'", f->name ? f->name : + NONE); + if (f->name) + free(f->name); + if (f->parent_name) { + ntfs_log_debug(" and parent filename '%s'", + f->parent_name); + free(f->parent_name); + } + ntfs_log_debug(".\n"); + free(f); + } + + ntfs_list_for_each_safe(item, tmp, &file->data) { + /* List of data streams */ + struct data *d = ntfs_list_entry(item, struct data, list); + ntfs_log_debug("Freeing data stream '%s'.\n", d->name ? + d->name : UNNAMED); + if (d->name) + free(d->name); + if (d->runlist) + free(d->runlist); + free(d); + } + + free(file->mft); + free(file); +} + +/** + * verify_parent - confirm a record is parent of a file + * @name: a filename of the file + * @rec: the mft record of the possible parent + * + * Check that @rec is the parent of the file represented by @name. + * If @rec is a directory, but it is created after @name, then we + * can't determine whether @rec is really @name's parent. + * + * Return: @rec's filename, either same name space as @name or lowest space. + * NULL if can't determine parenthood or on error. + */ +static FILE_NAME_ATTR* verify_parent(struct filename* name, MFT_RECORD* rec) +{ + ATTR_RECORD *attr30; + FILE_NAME_ATTR *filename_attr = NULL, *lowest_space_name = NULL; + ntfs_attr_search_ctx *ctx; + int found_same_space = 1; + + if (!name || !rec) + return NULL; + + if (!(rec->flags & MFT_RECORD_IS_DIRECTORY)) { + return NULL; + } + + ctx = ntfs_attr_get_search_ctx(NULL, rec); + if (!ctx) { + ntfs_log_error("ERROR: Couldn't create a search context.\n"); + return NULL; + } + + attr30 = find_attribute(AT_FILE_NAME, ctx); + if (!attr30) { + return NULL; + } + + filename_attr = (FILE_NAME_ATTR*)((char*)attr30 + le16_to_cpu(attr30->value_offset)); + /* if name is older than this dir -> can't determine */ + if (ntfs2timespec(filename_attr->creation_time).tv_sec > name->date_c) { + return NULL; + } + + if (filename_attr->file_name_type != name->name_space) { + found_same_space = 0; + lowest_space_name = filename_attr; + + while (!found_same_space && (attr30 = find_attribute(AT_FILE_NAME, ctx))) { + filename_attr = (FILE_NAME_ATTR*)((char*)attr30 + le16_to_cpu(attr30->value_offset)); + + if (filename_attr->file_name_type == name->name_space) { + found_same_space = 1; + } else { + if (filename_attr->file_name_type < lowest_space_name->file_name_type) { + lowest_space_name = filename_attr; + } + } + } + } + + ntfs_attr_put_search_ctx(ctx); + + return (found_same_space ? filename_attr : lowest_space_name); +} + +/** + * get_parent_name - Find the name of a file's parent. + * @name: the filename whose parent's name to find + */ +static void get_parent_name(struct filename* name, ntfs_volume* vol) +{ + ntfs_attr* mft_data; + MFT_RECORD* rec; + FILE_NAME_ATTR* filename_attr; + long long inode_num; + + if (!name || !vol) + return; + + rec = calloc(1, vol->mft_record_size); + if (!rec) { + ntfs_log_error("ERROR: Couldn't allocate memory in " + "get_parent_name()\n"); + return; + } + + mft_data = ntfs_attr_open(vol->mft_ni, AT_DATA, AT_UNNAMED, 0); + if (!mft_data) { + ntfs_log_perror("ERROR: Couldn't open $MFT/$DATA"); + } else { + inode_num = MREF_LE(name->parent_mref); + + if (ntfs_attr_pread(mft_data, vol->mft_record_size * inode_num, + vol->mft_record_size, rec) < 1) { + ntfs_log_error("ERROR: Couldn't read MFT Record %lld" + ".\n", inode_num); + } else if ((filename_attr = verify_parent(name, rec))) { + if (ntfs_ucstombs(filename_attr->file_name, + filename_attr->file_name_length, + &name->parent_name, 0) < 0) { + ntfs_log_debug("ERROR: Couldn't translate " + "filename to current " + "locale.\n"); + name->parent_name = NULL; + } + } + } + + if (mft_data) { + ntfs_attr_close(mft_data); + } + + if (rec) { + free(rec); + } + + return; +} + +/* + * Rescue the last deleted name of a file + * + * Under some conditions, when a name is deleted and the MFT + * record is shifted to reclaim the space, the name is still + * present beyond the end of record. + * + * For this to be possible, the data record has to be small (less + * than 80 bytes), and there must be no other attributes. + * So only the names of plain unfragmented files can be rescued. + * + * Returns NULL when the name cannot be recovered. + */ + +static struct filename *rescue_name(MFT_RECORD *mft, ntfs_attr_search_ctx *ctx) +{ + ATTR_RECORD *rec; + struct filename *name; + int off_name; + int length; + int type; + + name = (struct filename*)NULL; + ntfs_attr_reinit_search_ctx(ctx); + rec = find_attribute(AT_DATA, ctx); + if (rec) { + /* + * If the data attribute replaced the name attribute, + * the name itself is at offset 0x58 from the data attr. + * First be sure this location is within the unused part + * of the MFT record, then make extra checks. + */ + off_name = (long)rec - (long)mft + 0x58; + if ((off_name >= (int)le32_to_cpu(mft->bytes_in_use)) + && ((off_name + 4) + <= (int)le32_to_cpu(mft->bytes_allocated))) { + length = *((char*)mft + off_name); + type = *((char*)mft + off_name + 1); + /* check whether the name is fully allocated */ + if ((type <= 3) + && (length > 0) + && ((off_name + 2*length + 2) + <= (int)le32_to_cpu(mft->bytes_allocated))) { + /* create a (partial) name record */ + name = (struct filename*) + ntfs_calloc(sizeof(*name)); + if (name) { + name->uname = (ntfschar*) + ((char*)mft + off_name + 2); + name->uname_len = length; + name->name_space = type; + if (ntfs_ucstombs(name->uname, length, + &name->name, 0) < 0) { + free(name); + name = (struct filename*)NULL; + } + } + if (name && name->name) + ntfs_log_verbose("Recovered file name %s\n", + name->name); + } + } + } + return (name); +} + + + +/** + * get_filenames - Read an MFT Record's $FILENAME attributes + * @file: The file object to work with + * + * A single file may have more than one filename. This is quite common. + * Windows creates a short DOS name for each long name, e.g. LONGFI~1.XYZ, + * LongFiLeName.xyZ. + * + * The filenames that are found are put in filename objects and added to a + * linked list of filenames in the file object. For convenience, the unicode + * filename is converted into the current locale and stored in the filename + * object. + * + * One of the filenames is picked (the one with the lowest numbered namespace) + * and its locale friendly name is put in pref_name. + * + * Return: n The number of $FILENAME attributes found + * -1 Error + */ +static int get_filenames(struct ufile *file, ntfs_volume* vol) +{ + ATTR_RECORD *rec; + FILE_NAME_ATTR *attr; + ntfs_attr_search_ctx *ctx; + struct filename *name; + int count = 0; + int space = 4; + + if (!file) + return -1; + + ctx = ntfs_attr_get_search_ctx(NULL, file->mft); + if (!ctx) + return -1; + + while ((rec = find_attribute(AT_FILE_NAME, ctx))) { + /* We know this will always be resident. */ + attr = (FILE_NAME_ATTR *)((char *)rec + + le16_to_cpu(rec->value_offset)); + + name = calloc(1, sizeof(*name)); + if (!name) { + ntfs_log_error("ERROR: Couldn't allocate memory in " + "get_filenames().\n"); + count = -1; + break; + } + + name->uname = attr->file_name; + name->uname_len = attr->file_name_length; + name->name_space = attr->file_name_type; + name->size_alloc = sle64_to_cpu(attr->allocated_size); + name->size_data = sle64_to_cpu(attr->data_size); + name->flags = attr->file_attributes; + + name->date_c = ntfs2timespec(attr->creation_time).tv_sec; + name->date_a = ntfs2timespec(attr->last_data_change_time).tv_sec; + name->date_m = ntfs2timespec(attr->last_mft_change_time).tv_sec; + name->date_r = ntfs2timespec(attr->last_access_time).tv_sec; + + if (ntfs_ucstombs(name->uname, name->uname_len, &name->name, + 0) < 0) { + ntfs_log_debug("ERROR: Couldn't translate filename to " + "current locale.\n"); + } + + name->parent_name = NULL; + + if (opts.parent) { + name->parent_mref = attr->parent_directory; + get_parent_name(name, vol); + } + + if (name->name_space < space) { + file->pref_name = name->name; + file->pref_pname = name->parent_name; + space = name->name_space; + } + + file->max_size = max(file->max_size, name->size_alloc); + file->max_size = max(file->max_size, name->size_data); + + ntfs_list_add_tail(&name->list, &file->name); + count++; + } + + if (!count) { + name = rescue_name(file->mft,ctx); + if (name) { + /* a name was recovered, get missing attributes */ + file->pref_name = name->name; + ntfs_attr_reinit_search_ctx(ctx); + rec = find_attribute(AT_STANDARD_INFORMATION, ctx); + if (rec) { + attr = (FILE_NAME_ATTR *)((char *)rec + + le16_to_cpu(rec->value_offset)); + name->flags = attr->file_attributes; + + name->date_c = ntfs2timespec(attr->creation_time).tv_sec; + name->date_a = ntfs2timespec(attr->last_data_change_time).tv_sec; + name->date_m = ntfs2timespec(attr->last_mft_change_time).tv_sec; + name->date_r = ntfs2timespec(attr->last_access_time).tv_sec; + } + rec = find_attribute(AT_DATA, ctx); + if (rec) { + attr = (FILE_NAME_ATTR *)((char *)rec + + le16_to_cpu(rec->value_offset)); + name->size_alloc = sle64_to_cpu(attr->allocated_size); + name->size_data = sle64_to_cpu(attr->data_size); + } + ntfs_list_add_tail(&name->list, &file->name); + count++; + } + } + ntfs_attr_put_search_ctx(ctx); + ntfs_log_debug("File has %d names.\n", count); + return count; +} + +/** + * get_data - Read an MFT Record's $DATA attributes + * @file: The file object to work with + * @vol: An ntfs volume obtained from ntfs_mount + * + * A file may have more than one data stream. All files will have an unnamed + * data stream which contains the file's data. Some Windows applications store + * extra information in a separate stream. + * + * The streams that are found are put in data objects and added to a linked + * list of data streams in the file object. + * + * Return: n The number of $FILENAME attributes found + * -1 Error + */ +static int get_data(struct ufile *file, ntfs_volume *vol) +{ + ATTR_RECORD *rec; + ntfs_attr_search_ctx *ctx; + int count = 0; + struct data *data; + + if (!file) + return -1; + + ctx = ntfs_attr_get_search_ctx(NULL, file->mft); + if (!ctx) + return -1; + + while ((rec = find_attribute(AT_DATA, ctx))) { + data = calloc(1, sizeof(*data)); + if (!data) { + ntfs_log_error("ERROR: Couldn't allocate memory in " + "get_data().\n"); + count = -1; + break; + } + + data->resident = !rec->non_resident; + data->compressed = (rec->flags & ATTR_IS_COMPRESSED) ? 1 : 0; + data->encrypted = (rec->flags & ATTR_IS_ENCRYPTED) ? 1 : 0; + + if (rec->name_length) { + data->uname = (ntfschar *)((char *)rec + + le16_to_cpu(rec->name_offset)); + data->uname_len = rec->name_length; + + if (ntfs_ucstombs(data->uname, data->uname_len, + &data->name, 0) < 0) { + ntfs_log_error("ERROR: Cannot translate name " + "into current locale.\n"); + } + } + + if (data->resident) { + data->size_data = le32_to_cpu(rec->value_length); + data->data = (char*)rec + + le16_to_cpu(rec->value_offset); + } else { + data->size_alloc = sle64_to_cpu(rec->allocated_size); + data->size_data = sle64_to_cpu(rec->data_size); + data->size_init = sle64_to_cpu(rec->initialized_size); + data->size_vcn = sle64_to_cpu(rec->highest_vcn) + 1; + } + + data->runlist = ntfs_mapping_pairs_decompress(vol, rec, NULL); + if (!data->runlist) { + ntfs_log_debug("Couldn't decompress the data runs.\n"); + } + + file->max_size = max(file->max_size, data->size_data); + file->max_size = max(file->max_size, data->size_init); + + ntfs_list_add_tail(&data->list, &file->data); + count++; + } + + ntfs_attr_put_search_ctx(ctx); + ntfs_log_debug("File has %d data streams.\n", count); + return count; +} + +/** + * read_record - Read an MFT record into memory + * @vol: An ntfs volume obtained from ntfs_mount + * @record: The record number to read + * + * Read the specified MFT record and gather as much information about it as + * possible. + * + * Return: Pointer A ufile object containing the results + * NULL Error + */ +static struct ufile * read_record(ntfs_volume *vol, long long record) +{ + ATTR_RECORD *attr10, *attr20, *attr90; + struct ufile *file; + ntfs_attr *mft; + u32 log_levels; + + if (!vol) + return NULL; + + file = calloc(1, sizeof(*file)); + if (!file) { + ntfs_log_error("ERROR: Couldn't allocate memory in read_record()\n"); + return NULL; + } + + NTFS_INIT_LIST_HEAD(&file->name); + NTFS_INIT_LIST_HEAD(&file->data); + file->inode = record; + + file->mft = malloc(vol->mft_record_size); + if (!file->mft) { + ntfs_log_error("ERROR: Couldn't allocate memory in read_record()\n"); + free_file(file); + return NULL; + } + + mft = ntfs_attr_open(vol->mft_ni, AT_DATA, AT_UNNAMED, 0); + if (!mft) { + ntfs_log_perror("ERROR: Couldn't open $MFT/$DATA"); + free_file(file); + return NULL; + } + + if (ntfs_attr_mst_pread(mft, vol->mft_record_size * record, 1, vol->mft_record_size, file->mft) < 1) { + ntfs_log_error("ERROR: Couldn't read MFT Record %lld.\n", record); + ntfs_attr_close(mft); + free_file(file); + return NULL; + } + + ntfs_attr_close(mft); + mft = NULL; + + /* disable errors logging, while examining suspicious records */ + log_levels = ntfs_log_clear_levels(NTFS_LOG_LEVEL_PERROR); + attr10 = find_first_attribute(AT_STANDARD_INFORMATION, file->mft); + attr20 = find_first_attribute(AT_ATTRIBUTE_LIST, file->mft); + attr90 = find_first_attribute(AT_INDEX_ROOT, file->mft); + + ntfs_log_debug("Attributes present: %s %s %s.\n", attr10?"0x10":"", + attr20?"0x20":"", attr90?"0x90":""); + + if (attr10) { + STANDARD_INFORMATION *si; + si = (STANDARD_INFORMATION *) ((char *) attr10 + le16_to_cpu(attr10->value_offset)); + file->date = ntfs2timespec(si->last_data_change_time).tv_sec; + } + + if (attr20 || !attr10) + file->attr_list = 1; + if (attr90) + file->directory = 1; + + if (get_filenames(file, vol) < 0) { + ntfs_log_error("ERROR: Couldn't get filenames.\n"); + } + if (get_data(file, vol) < 0) { + ntfs_log_error("ERROR: Couldn't get data streams.\n"); + } + /* restore errors logging */ + ntfs_log_set_levels(log_levels); + + return file; +} + +/** + * calc_percentage - Calculate how much of the file is recoverable + * @file: The file object to work with + * @vol: An ntfs volume obtained from ntfs_mount + * + * Read through all the $DATA streams and determine if each cluster in each + * stream is still free disk space. This is just measuring the potential for + * recovery. The data may have still been overwritten by a another file which + * was then deleted. + * + * Files with a resident $DATA stream will have a 100% potential. + * + * N.B. If $DATA attribute spans more than one MFT record (i.e. badly + * fragmented) then only the data in this segment will be used for the + * calculation. + * + * N.B. Currently, compressed and encrypted files cannot be recovered, so they + * will return 0%. + * + * Return: n The percentage of the file that _could_ be recovered + * -1 Error + */ +static int calc_percentage(struct ufile *file, ntfs_volume *vol) +{ + runlist_element *rl = NULL; + struct ntfs_list_head *pos; + struct data *data; + long long i, j; + long long start, end; + int clusters_inuse, clusters_free; + int percent = 0; + + if (!file || !vol) + return -1; + + if (file->directory) { + ntfs_log_debug("Found a directory: not recoverable.\n"); + return 0; + } + + if (ntfs_list_empty(&file->data)) { + ntfs_log_verbose("File has no data streams.\n"); + return 0; + } + + ntfs_list_for_each(pos, &file->data) { + data = ntfs_list_entry(pos, struct data, list); + clusters_inuse = 0; + clusters_free = 0; + + if (data->encrypted) { + ntfs_log_verbose("File is encrypted, recovery is " + "impossible.\n"); + continue; + } + + if (data->compressed) { + ntfs_log_verbose("File is compressed, recovery not yet " + "implemented.\n"); + continue; + } + + if (data->resident) { + ntfs_log_verbose("File is resident, therefore " + "recoverable.\n"); + percent = 100; + data->percent = 100; + continue; + } + + rl = data->runlist; + if (!rl) { + ntfs_log_verbose("File has no runlist, hence no data." + "\n"); + continue; + } + + if (rl[0].length <= 0) { + ntfs_log_verbose("File has an empty runlist, hence no " + "data.\n"); + continue; + } + + if (rl[0].lcn == LCN_RL_NOT_MAPPED) { /* extended mft record */ + ntfs_log_verbose("Missing segment at beginning, %lld " + "clusters\n", (long long)rl[0].length); + clusters_inuse += rl[0].length; + rl++; + } + + for (i = 0; rl[i].length > 0; i++) { + if (rl[i].lcn == LCN_RL_NOT_MAPPED) { + ntfs_log_verbose("Missing segment at end, %lld " + "clusters\n", + (long long)rl[i].length); + clusters_inuse += rl[i].length; + continue; + } + + if (rl[i].lcn == LCN_HOLE) { + clusters_free += rl[i].length; + continue; + } + + start = rl[i].lcn; + end = rl[i].lcn + rl[i].length; + + for (j = start; j < end; j++) { + if (utils_cluster_in_use(vol, j)) + clusters_inuse++; + else + clusters_free++; + } + } + + if ((clusters_inuse + clusters_free) == 0) { + ntfs_log_error("ERROR: Unexpected error whilst " + "calculating percentage for inode %lld\n", + file->inode); + continue; + } + + data->percent = (clusters_free * 100) / + (clusters_inuse + clusters_free); + + percent = max(percent, data->percent); + } + + ntfs_log_verbose("File is %d%% recoverable\n", percent); + return percent; +} + +/** + * dump_record - Print everything we know about an MFT record + * @file: The file to work with + * + * Output the contents of the file object. This will print everything that has + * been read from the MFT record, or implied by various means. + * + * Because of the redundant nature of NTFS, there will be some duplication of + * information, though it will have been read from different sources. + * + * N.B. If the filename is missing, or couldn't be converted to the current + * locale, "" will be displayed. + * + * Return: none + */ +static void dump_record(struct ufile *file) +{ + char buffer[20]; + struct ntfs_list_head *item; + int i; + + if (!file) + return; + + ntfs_log_quiet("MFT Record %lld\n", file->inode); + ntfs_log_quiet("Type: %s\n", (file->directory) ? "Directory" : "File"); + strftime(buffer, sizeof(buffer), "%F %R", localtime(&file->date)); + ntfs_log_quiet("Date: %s\n", buffer); + + if (file->attr_list) + ntfs_log_quiet("Metadata may span more than one MFT record\n"); + + ntfs_list_for_each(item, &file->name) { + struct filename *f = + ntfs_list_entry(item, struct filename, list); + + ntfs_log_quiet("Filename: (%d) %s\n", f->name_space, f->name); + ntfs_log_quiet("File Flags: "); + if (f->flags & FILE_ATTR_SYSTEM) + ntfs_log_quiet("System "); + if (f->flags & FILE_ATTR_DIRECTORY) + ntfs_log_quiet("Directory "); + if (f->flags & FILE_ATTR_SPARSE_FILE) + ntfs_log_quiet("Sparse "); + if (f->flags & FILE_ATTR_REPARSE_POINT) + ntfs_log_quiet("Reparse "); + if (f->flags & FILE_ATTR_COMPRESSED) + ntfs_log_quiet("Compressed "); + if (f->flags & FILE_ATTR_ENCRYPTED) + ntfs_log_quiet("Encrypted "); + if (!(f->flags & (FILE_ATTR_SYSTEM | FILE_ATTR_DIRECTORY | + FILE_ATTR_SPARSE_FILE | FILE_ATTR_REPARSE_POINT | + FILE_ATTR_COMPRESSED | FILE_ATTR_ENCRYPTED))) { + ntfs_log_quiet("%s", NONE); + } + + ntfs_log_quiet("\n"); + + if (opts.parent) { + ntfs_log_quiet("Parent: %s\n", f->parent_name ? + f->parent_name : ""); + } + + ntfs_log_quiet("Size alloc: %lld\n", f->size_alloc); + ntfs_log_quiet("Size data: %lld\n", f->size_data); + + strftime(buffer, sizeof(buffer), "%F %R", + localtime(&f->date_c)); + ntfs_log_quiet("Date C: %s\n", buffer); + strftime(buffer, sizeof(buffer), "%F %R", + localtime(&f->date_a)); + ntfs_log_quiet("Date A: %s\n", buffer); + strftime(buffer, sizeof(buffer), "%F %R", + localtime(&f->date_m)); + ntfs_log_quiet("Date M: %s\n", buffer); + strftime(buffer, sizeof(buffer), "%F %R", + localtime(&f->date_r)); + ntfs_log_quiet("Date R: %s\n", buffer); + } + + ntfs_log_quiet("Data Streams:\n"); + ntfs_list_for_each(item, &file->data) { + struct data *d = ntfs_list_entry(item, struct data, list); + ntfs_log_quiet("Name: %s\n", (d->name) ? d->name : UNNAMED); + ntfs_log_quiet("Flags: "); + if (d->resident) ntfs_log_quiet("Resident\n"); + if (d->compressed) ntfs_log_quiet("Compressed\n"); + if (d->encrypted) ntfs_log_quiet("Encrypted\n"); + if (!d->resident && !d->compressed && !d->encrypted) + ntfs_log_quiet("None\n"); + else + ntfs_log_quiet("\n"); + + ntfs_log_quiet("Size alloc: %lld\n", d->size_alloc); + ntfs_log_quiet("Size data: %lld\n", d->size_data); + ntfs_log_quiet("Size init: %lld\n", d->size_init); + ntfs_log_quiet("Size vcn: %lld\n", d->size_vcn); + + ntfs_log_quiet("Data runs:\n"); + if ((!d->runlist) || (d->runlist[0].length <= 0)) { + ntfs_log_quiet(" None\n"); + } else { + for (i = 0; d->runlist[i].length > 0; i++) { + ntfs_log_quiet(" %lld @ %lld\n", + (long long)d->runlist[i].length, + (long long)d->runlist[i].lcn); + } + } + + ntfs_log_quiet("Amount potentially recoverable %d%%\n", + d->percent); + } + + ntfs_log_quiet("________________________________________\n\n"); +} + +/** + * list_record - Print a one line summary of the file + * @file: The file to work with + * + * Print a one line description of a file. + * + * Inode Flags %age Date Time Size Filename + * + * The output will contain the file's inode number (MFT Record), some flags, + * the percentage of the file that is recoverable, the last modification date, + * the size and the filename. + * + * The flags are F/D = File/Directory, N/R = Data is (Non-)Resident, + * C = Compressed, E = Encrypted, ! = Metadata may span multiple records. + * + * N.B. The file size is stored in many forms in several attributes. This + * display the largest it finds. + * + * N.B. If the filename is missing, or couldn't be converted to the current + * locale, "" will be displayed. + * + * Return: none + */ +static void list_record(struct ufile *file) +{ + char buffer[20]; + struct ntfs_list_head *item; + const char *name = NULL; + long long size = 0; + int percent = 0; + + char flagd = '.', flagr = '.', flagc = '.', flagx = '.'; + + strftime(buffer, sizeof(buffer), "%F %R", localtime(&file->date)); + + if (file->attr_list) + flagx = '!'; + + if (file->directory) + flagd = 'D'; + else + flagd = 'F'; + + ntfs_list_for_each(item, &file->data) { + struct data *d = ntfs_list_entry(item, struct data, list); + + if (!d->name) { + if (d->resident) + flagr = 'R'; + else + flagr = 'N'; + if (d->compressed) + flagc = 'C'; + if (d->encrypted) + flagc = 'E'; + + percent = max(percent, d->percent); + } + + size = max(size, d->size_data); + size = max(size, d->size_init); + } + + if (file->pref_name) + name = file->pref_name; + else + name = NONE; + + ntfs_log_quiet("%-8lld %c%c%c%c %3d%% %s %9lld %s\n", + file->inode, flagd, flagr, flagc, flagx, + percent, buffer, size, name); + +} + +/** + * name_match - Does a file have a name matching a regex + * @re: The regular expression object + * @file: The file to be tested + * + * Iterate through the file's $FILENAME attributes and compare them against the + * regular expression, created with regcomp. + * + * Return: 1 There is a matching filename. + * 0 There is no match. + */ +static int name_match(regex_t *re, struct ufile *file) +{ + struct ntfs_list_head *item; + int result; + + if (!re || !file) + return 0; + + ntfs_list_for_each(item, &file->name) { + struct filename *f = + ntfs_list_entry(item, struct filename, list); + + if (!f->name) + continue; +#ifdef HAVE_REGEX_H + result = regexec(re, f->name, 0, NULL, 0); +#else + result = regexec(re, f->uname, f->uname_len, NULL, 0); +#endif + if (result < 0) { + ntfs_log_perror("Couldn't compare filename with regex"); + return 0; + } else if (result == REG_NOERROR) { + ntfs_log_debug("Found a matching filename.\n"); + return 1; + } + } + + ntfs_log_debug("Filename '%s' doesn't match regex.\n", file->pref_name); + return 0; +} + +/** + * write_data - Write out a block of data + * @fd: File descriptor to write to + * @buffer: Data to write + * @bufsize: Amount of data to write + * + * Write a block of data to a file descriptor. + * + * Return: -1 Error, something went wrong + * 0 Success, all the data was written + */ +static unsigned int write_data(int fd, const char *buffer, + unsigned int bufsize) +{ + ssize_t result1, result2; + + if (!buffer) { + errno = EINVAL; + return -1; + } + + result1 = write(fd, buffer, bufsize); + if ((result1 == (ssize_t) bufsize) || (result1 < 0)) + return result1; + + /* Try again with the rest of the buffer */ + buffer += result1; + bufsize -= result1; + + result2 = write(fd, buffer, bufsize); + if (result2 < 0) + return result1; + + return result1 + result2; +} + +/** + * create_pathname - Create a path/file from some components + * @dir: Directory in which to create the file (optional) + * @name: Filename to give the file (optional) + * @stream: Name of the stream (optional) + * @buffer: Store the result here + * @bufsize: Size of buffer + * + * Create a filename from various pieces. The output will be of the form: + * dir/file + * dir/file:stream + * file + * file:stream + * + * All the components are optional. If the name is missing, "unknown" will be + * used. If the directory is missing the file will be created in the current + * directory. If the stream name is present it will be appended to the + * filename, delimited by a colon. + * + * N.B. If the buffer isn't large enough the name will be truncated. + * + * Return: n Length of the allocated name + */ +static int create_pathname(const char *dir, const char *name, + const char *stream, char *buffer, int bufsize) +{ + struct stat st; + int s; + int len; + int suffix; + + if (!name) + name = UNKNOWN; + + if (dir) { +#ifdef HAVE_WINDOWS_H + if (stream) + snprintf(buffer, bufsize, "%s\\%s:%s", dir, name, + stream); + else + snprintf(buffer, bufsize, "%s\\%s", dir, name); +#else + if (stream) + snprintf(buffer, bufsize, "%s/%s:%s", dir, name, + stream); + else + snprintf(buffer, bufsize, "%s/%s", dir, name); +#endif + } else + if (stream) + snprintf(buffer, bufsize, "%s:%s", name, stream); + else + snprintf(buffer, bufsize, "%s", name); + len = strlen(buffer); + suffix = 0; +#ifdef HAVE_WINDOWS_H + s = stat(buffer, &st); +#else + s = lstat(buffer, &st); +#endif + while (!s && (suffix < 999)) { + suffix++; + snprintf(&buffer[len], bufsize - len, ".%d", suffix); +#ifdef HAVE_WINDOWS_H + s = stat(buffer, &st); +#else + s = lstat(buffer, &st); +#endif + } + + return strlen(buffer); +} + +/** + * open_file - Open a file to write to + * @pathname: Path, name and stream of the file to open + * + * Create a file and return the file descriptor. + * + * N.B. If option force is given and existing file will be overwritten. + * + * Return: -1 Error, failed to create the file + * n Success, this is the file descriptor + */ +static int open_file(const char *pathname) +{ + int flags; + + ntfs_log_verbose("Creating file: %s\n", pathname); + + if (opts.force) + flags = O_RDWR | O_CREAT | O_TRUNC; + else + flags = O_RDWR | O_CREAT | O_EXCL; +#ifdef HAVE_WINDOWS_H + flags ^= O_BINARY | O_RDWR | O_WRONLY; +#endif + + return open(pathname, flags, S_IRUSR | S_IWUSR); +} + +/** + * set_date - Set the file's date and time + * @pathname: Path and name of the file to alter + * @date: Date and time to set + * + * Give a file a particular date and time. + * + * Return: 1 Success, set the file's date and time + * 0 Error, failed to change the file's date and time + */ +static int set_date(const char *pathname, time_t date) +{ + struct utimbuf ut; + + if (!pathname) + return 0; + + ut.actime = date; + ut.modtime = date; + if (utime(pathname, &ut)) { + ntfs_log_error("ERROR: Couldn't set the file's date and time\n"); + return 0; + } + return 1; +} + +/** + * undelete_file - Recover a deleted file from an NTFS volume + * @vol: An ntfs volume obtained from ntfs_mount + * @inode: MFT Record number to be recovered + * + * Read an MFT Record and try an recover any data associated with it. Some of + * the clusters may be in use; these will be filled with zeros or the fill byte + * supplied in the options. + * + * Each data stream will be recovered and saved to a file. The file's name will + * be the original filename and it will be written to the current directory. + * Any named data stream will be saved as filename:streamname. + * + * The output file's name and location can be altered by using the command line + * options. + * + * N.B. We cannot tell if someone has overwritten some of the data since the + * file was deleted. + * + * Return: 0 Error, something went wrong + * 1 Success, the data was recovered + */ +static int undelete_file(ntfs_volume *vol, long long inode) +{ + char pathname[256]; + char *buffer = NULL; + unsigned int bufsize; + struct ufile *file; + int i, j; + long long start, end; + runlist_element *rl; + struct ntfs_list_head *item; + int fd = -1; + long long k; + int result = 0; + char *name; + long long cluster_count; /* I'll need this variable (see below). +mabs */ + + if (!vol) + return 0; + + /* try to get record */ + file = read_record(vol, inode); + if (!file || !file->mft) { + ntfs_log_error("Can't read info from mft record %lld.\n", inode); + return 0; + } + + /* if flag was not set, print file informations */ + if (avoid_duplicate_printing == 0) { + if (opts.verbose) { + dump_record(file); + } else { + list_record(file); + //ntfs_log_quiet("\n"); + } + } + + bufsize = vol->cluster_size; + buffer = malloc(bufsize); + if (!buffer) + goto free; + + /* calc_percentage() must be called before dump_record() or + * list_record(). Otherwise, when undeleting, a file will always be + * listed as 0% recoverable even if successfully undeleted. +mabs + */ + if (file->mft->flags & MFT_RECORD_IN_USE) { + ntfs_log_error("Record is in use by the mft\n"); + if (!opts.force) { + free(buffer); + free_file(file); + return 0; + } + ntfs_log_verbose("Forced to continue.\n"); + } + + if (calc_percentage(file, vol) == 0) { + ntfs_log_quiet("File has no recoverable data.\n"); + goto free; + } + + if (ntfs_list_empty(&file->data)) { + ntfs_log_quiet("File has no data. There is nothing to recover.\n"); + goto free; + } + + ntfs_list_for_each(item, &file->data) { + struct data *d = ntfs_list_entry(item, struct data, list); + char defname[sizeof(UNKNOWN) + 25]; + + if (opts.output) + name = opts.output; + else + if (file->pref_name) + name = file->pref_name; + else { + sprintf(defname,"%s%lld",UNKNOWN, + (long long)file->inode); + name = defname; + } + + create_pathname(opts.dest, name, d->name, pathname, sizeof(pathname)); + if (d->resident) { + fd = open_file(pathname); + if (fd < 0) { + ntfs_log_perror("Couldn't create file %s", + pathname); + goto free; + } + + ntfs_log_verbose("File has resident data.\n"); + if (write_data(fd, d->data, d->size_data) < d->size_data) { + ntfs_log_perror("Write failed"); + close(fd); + goto free; + } + + if (close(fd) < 0) { + ntfs_log_perror("Close failed"); + } + fd = -1; + } else { + rl = d->runlist; + if (!rl) { + ntfs_log_verbose("File has no runlist, hence no data.\n"); + continue; + } + + if (rl[0].length <= 0) { + ntfs_log_verbose("File has an empty runlist, hence no data.\n"); + continue; + } + + fd = open_file(pathname); + if (fd < 0) { + ntfs_log_perror("Couldn't create file %s", + pathname); + goto free; + } + + if (rl[0].lcn == LCN_RL_NOT_MAPPED) { /* extended mft record */ + ntfs_log_verbose("Missing segment at beginning, %lld " + "clusters.\n", + (long long)rl[0].length); + memset(buffer, opts.fillbyte, bufsize); + for (k = 0; k < rl[0].length * vol->cluster_size; k += bufsize) { + if (write_data(fd, buffer, bufsize) < bufsize) { + ntfs_log_perror("Write failed"); + close(fd); + goto free; + } + } + } + + cluster_count = 0LL; + for (i = 0; rl[i].length > 0; i++) { + + if (rl[i].lcn == LCN_RL_NOT_MAPPED) { + ntfs_log_verbose("Missing segment at end, " + "%lld clusters.\n", + (long long)rl[i].length); + memset(buffer, opts.fillbyte, bufsize); + for (k = 0; k < rl[i].length * vol->cluster_size; k += bufsize) { + if (write_data(fd, buffer, bufsize) < bufsize) { + ntfs_log_perror("Write failed"); + close(fd); + goto free; + } + cluster_count++; + } + continue; + } + + if (rl[i].lcn == LCN_HOLE) { + ntfs_log_verbose("File has a sparse section.\n"); + memset(buffer, 0, bufsize); + for (k = 0; k < rl[i].length * vol->cluster_size; k += bufsize) { + if (write_data(fd, buffer, bufsize) < bufsize) { + ntfs_log_perror("Write failed"); + close(fd); + goto free; + } + } + continue; + } + + start = rl[i].lcn; + end = rl[i].lcn + rl[i].length; + + for (j = start; j < end; j++) { + if (utils_cluster_in_use(vol, j) && !opts.optimistic) { + memset(buffer, opts.fillbyte, bufsize); + if (write_data(fd, buffer, bufsize) < bufsize) { + ntfs_log_perror("Write failed"); + close(fd); + goto free; + } + } else { + if (ntfs_cluster_read(vol, j, 1, buffer) < 1) { + ntfs_log_perror("Read failed"); + close(fd); + goto free; + } + if (write_data(fd, buffer, bufsize) < bufsize) { + ntfs_log_perror("Write failed"); + close(fd); + goto free; + } + cluster_count++; + } + } + } + ntfs_log_quiet("\n"); + + /* + * The following block of code implements the --truncate option. + * Its semantics are as follows: + * IF opts.truncate is set AND data stream currently being recovered is + * non-resident AND data stream has no holes (100% recoverability) AND + * 0 <= (data->size_alloc - data->size_data) <= vol->cluster_size AND + * cluster_count * vol->cluster_size == data->size_alloc THEN file + * currently being written is truncated to data->size_data bytes before + * it's closed. + * This multiple checks try to ensure that only files with consistent + * values of size/occupied clusters are eligible for truncation. Note + * that resident streams need not be truncated, since the original code + * already recovers their exact length. +mabs + */ + if (opts.truncate) { + if (d->percent == 100 && d->size_alloc >= d->size_data && + (d->size_alloc - d->size_data) <= (long long)vol->cluster_size && + cluster_count * (long long)vol->cluster_size == d->size_alloc) { + if (ftruncate(fd, (off_t)d->size_data)) + ntfs_log_perror("Truncation failed"); + } else ntfs_log_quiet("Truncation not performed because file has an " + "inconsistent $MFT record.\n"); + } + + if (close(fd) < 0) { + ntfs_log_perror("Close failed"); + } + fd = -1; + + } + set_date(pathname, file->date); + if (d->name) + ntfs_log_quiet("Undeleted '%s:%s' successfully to %s.\n", + file->pref_name, d->name, pathname); + else + ntfs_log_quiet("Undeleted '%s' successfully to %s.\n", + file->pref_name, pathname); + } + result = 1; +free: + if (buffer) + free(buffer); + free_file(file); + return result; +} + +/** + * scan_disk - Search an NTFS volume for files that could be undeleted + * @vol: An ntfs volume obtained from ntfs_mount + * + * Read through all the MFT entries looking for deleted files. For each one + * determine how much of the data lies in unused disk space. + * + * The list can be filtered by name, size and date, using command line options. + * + * Return: -1 Error, something went wrong + * n Success, the number of recoverable files + */ +static int scan_disk(ntfs_volume *vol) +{ + s64 nr_mft_records; + const int BUFSIZE = 8192; + char *buffer = NULL; + int results = 0; + ntfs_attr *attr; + long long size; + long long bmpsize; + long long i; + int j, k, b; + int percent; + struct ufile *file; + regex_t re; + + if (!vol) + return -1; + + attr = ntfs_attr_open(vol->mft_ni, AT_BITMAP, AT_UNNAMED, 0); + if (!attr) { + ntfs_log_perror("ERROR: Couldn't open $MFT/$BITMAP"); + return -1; + } + NVolSetNoFixupWarn(vol); + bmpsize = attr->initialized_size; + + buffer = malloc(BUFSIZE); + if (!buffer) { + ntfs_log_error("ERROR: Couldn't allocate memory in scan_disk()\n"); + results = -1; + goto out; + } + + if (opts.match) { + int flags = REG_NOSUB; + + if (!opts.match_case) + flags |= REG_ICASE; + if (regcomp(&re, opts.match, flags)) { + ntfs_log_error("ERROR: Couldn't create a regex.\n"); + goto out; + } +#ifndef HAVE_REGEX_H + re->upcase = vol->upcase; + re->upcase_len = vol->upcase_len; +#endif + } + + nr_mft_records = vol->mft_na->initialized_size >> + vol->mft_record_size_bits; + + ntfs_log_quiet("Inode Flags %%age Date Time Size Filename\n"); + ntfs_log_quiet("-----------------------------------------------------------------------\n"); + for (i = 0; i < bmpsize; i += BUFSIZE) { + long long read_count = min((bmpsize - i), BUFSIZE); + size = ntfs_attr_pread(attr, i, read_count, buffer); + if (size < 0) + break; + + for (j = 0; j < size; j++) { + b = buffer[j]; + for (k = 0; k < 8; k++, b>>=1) { + if (((i+j)*8+k) >= nr_mft_records) + goto done; + if (b & 1) + continue; + file = read_record(vol, (i+j)*8+k); + if (!file) { + ntfs_log_error("Couldn't read MFT Record %lld.\n", + (long long)(i+j)*8+k); + continue; + } + + if ((opts.since > 0) && (file->date <= opts.since)) + goto skip; + if (opts.match && !name_match(&re, file)) + goto skip; + if (opts.size_begin && (opts.size_begin > file->max_size)) + goto skip; + if (opts.size_end && (opts.size_end < file->max_size)) + goto skip; + + percent = calc_percentage(file, vol); + if ((opts.percent == -1) || (percent >= opts.percent)) { + if (opts.verbose) + dump_record(file); + else + list_record(file); + + /* Was -u specified with no inode + so undelete file by regex */ + if (opts.mode == MODE_UNDELETE) { + if (!undelete_file(vol, file->inode)) + ntfs_log_verbose("ERROR: Failed to undelete " + "inode %lli\n!", + file->inode); + ntfs_log_info("\n"); + } + } + if (((opts.percent == -1) && (percent > 0)) || + ((opts.percent > 0) && (percent >= opts.percent))) { + results++; + } +skip: + free_file(file); + } + } + } +done: + ntfs_log_quiet("\nFiles with potentially recoverable content: %d\n", + results); +out: + if (opts.match) + regfree(&re); + free(buffer); + NVolClearNoFixupWarn(vol); + if (attr) + ntfs_attr_close(attr); + return results; +} + +/** + * copy_mft - Write a range of MFT Records to a file + * @vol: An ntfs volume obtained from ntfs_mount + * @mft_begin: First MFT Record to save + * @mft_end: Last MFT Record to save + * + * Read a number of MFT Records and write them to a file. + * + * Return: 0 Success, all the records were written + * 1 Error, something went wrong + */ +static int copy_mft(ntfs_volume *vol, long long mft_begin, long long mft_end) +{ + s64 nr_mft_records; + char pathname[256]; + ntfs_attr *mft; + char *buffer; + const char *name; + long long i; + int result = 1; + int fd; + + if (!vol) + return 1; + + if (mft_end < mft_begin) { + ntfs_log_error("Range to copy is backwards.\n"); + return 1; + } + + buffer = malloc(vol->mft_record_size); + if (!buffer) { + ntfs_log_error("Couldn't allocate memory in copy_mft()\n"); + return 1; + } + + mft = ntfs_attr_open(vol->mft_ni, AT_DATA, AT_UNNAMED, 0); + if (!mft) { + ntfs_log_perror("Couldn't open $MFT/$DATA"); + goto free; + } + + name = opts.output; + if (!name) { + name = MFTFILE; + ntfs_log_debug("No output filename, defaulting to '%s'.\n", + name); + } + + create_pathname(opts.dest, name, NULL, pathname, sizeof(pathname)); + fd = open_file(pathname); + if (fd < 0) { + ntfs_log_perror("Couldn't create output file '%s'", name); + goto attr; + } + + nr_mft_records = vol->mft_na->initialized_size >> + vol->mft_record_size_bits; + + mft_end = min(mft_end, nr_mft_records - 1); + + ntfs_log_debug("MFT records:\n"); + ntfs_log_debug("\tTotal: %8lld\n", (long long)nr_mft_records); + ntfs_log_debug("\tBegin: %8lld\n", mft_begin); + ntfs_log_debug("\tEnd: %8lld\n", mft_end); + + for (i = mft_begin; i <= mft_end; i++) { + if (ntfs_attr_pread(mft, vol->mft_record_size * i, + vol->mft_record_size, buffer) < vol->mft_record_size) { + ntfs_log_perror("Couldn't read MFT Record %lld", i); + goto close; + } + + if (write_data(fd, buffer, vol->mft_record_size) < vol->mft_record_size) { + ntfs_log_perror("Write failed"); + goto close; + } + } + + ntfs_log_verbose("Read %lld MFT Records\n", mft_end - mft_begin + 1); + ntfs_log_quiet("MFT extracted to file %s\n", pathname); + result = 0; +close: + close(fd); +attr: + ntfs_attr_close(mft); +free: + free(buffer); + return result; +} + +/** + * handle_undelete + * + * Handles the undelete + */ +static int handle_undelete(ntfs_volume *vol) +{ + int result = 1; + int i; + unsigned long long inode; + + /* Check whether (an) inode(s) was specified or at least a regex! */ + if (nr_entries == 0) { + if (with_regex == 0) { + ntfs_log_error("ERROR: NO inode(s) AND NO match-regex " + "specified!\n"); + } else { + avoid_duplicate_printing= 1; + result = !scan_disk(vol); + if (result) + ntfs_log_verbose("ERROR: Failed to scan device " + "'%s'.\n", opts.device); + } + } else { + /* Normal undelete by specifying inode(s) */ + ntfs_log_quiet("Inode Flags %%age Date Size Filename\n"); + ntfs_log_quiet("---------------------------------------------------------------\n"); + + /* loop all given inodes */ + for (i = 0; i < nr_entries; i++) { + for (inode = ranges[i].begin; inode <= ranges[i].end; inode ++) { + /* Now undelete file */ + result = !undelete_file(vol, inode); + if (result) + ntfs_log_verbose("ERROR: Failed to " + "undelete inode %lli\n!", inode); + } + } + } + return (result); +} + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char *argv[]) +{ + ntfs_volume *vol; + int result = 1; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + with_regex = 0; + avoid_duplicate_printing = 0; + + result = parse_options(argc, argv); + if (result >= 0) + goto free; + + utils_set_locale(); + + vol = utils_mount_volume(opts.device, NTFS_MNT_RDONLY | + (opts.force ? NTFS_MNT_RECOVER : 0)); + if (!vol) + return 1; + + /* handling of the different modes */ + switch (opts.mode) { + /* Scanning */ + case MODE_SCAN: + result = !scan_disk(vol); + if (result) + ntfs_log_verbose("ERROR: Failed to scan device '%s'.\n", + opts.device); + break; + + /* Undelete-handling */ + case MODE_UNDELETE: + result= handle_undelete(vol); + break; + + /* Handling of copy mft */ + case MODE_COPY: + result = !copy_mft(vol, opts.mft_begin, opts.mft_end); + if (result) + ntfs_log_verbose("ERROR: Failed to read MFT blocks " + "%lld-%lld.\n", (long long)opts.mft_begin, + (long long)min((vol->mft_na->initialized_size >> + vol->mft_record_size_bits) , opts.mft_end)); + break; + default: + ; /* Cannot happen */ + } + + ntfs_umount(vol, FALSE); +free: + if (opts.match) + free(opts.match); + + return result; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsundelete.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsundelete.h new file mode 100644 index 0000000..dd462f2 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsundelete.h @@ -0,0 +1,112 @@ +/* + * ntfsundelete - Part of the Linux-NTFS project. + * + * Copyright (c) 2002 Richard Russon + * Copyright (c) 2007 Yura Pakhuchiy + * + * This utility will recover deleted files from an NTFS volume. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFSUNDELETE_H_ +#define _NTFSUNDELETE_H_ + +#include "types.h" +#include "list.h" +#include "runlist.h" +#include "utils.h" + +enum optmode { + MODE_NONE = 0, + MODE_SCAN, + MODE_UNDELETE, + MODE_COPY, + MODE_ERROR +}; + +struct options { + char *device; /* Device/File to work with */ + enum optmode mode; /* Scan / Undelete / Copy */ + int percent; /* Minimum recoverability */ + int uinode; /* Undelete this inode */ + char *dest; /* Save file to this directory */ + char *output; /* With this filename */ + char *match; /* Pattern for filename matching */ + int match_case; /* Case sensitive matching */ + int truncate; /* Truncate files to exact size. */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + int force; /* Override common sense */ + int optimistic; /* Undelete in-use clusters as well */ + int parent; /* Show parent directory */ + time_t since; /* Since this time */ + s64 size_begin; /* Range for file size */ + s64 size_end; + s64 mft_begin; /* Range for mft copy */ + s64 mft_end; + char fillbyte; /* Use for unrecoverable sections */ +}; + +struct filename { + struct ntfs_list_head list; /* Previous/Next links */ + ntfschar *uname; /* Filename in unicode */ + int uname_len; /* and its length */ + long long size_alloc; /* Allocated size (multiple of cluster size) */ + long long size_data; /* Actual size of data */ + FILE_ATTR_FLAGS flags; + time_t date_c; /* Time created */ + time_t date_a; /* altered */ + time_t date_m; /* mft record changed */ + time_t date_r; /* read */ + char *name; /* Filename in current locale */ + FILE_NAME_TYPE_FLAGS name_space; + leMFT_REF parent_mref; + char *parent_name; +}; + +struct data { + struct ntfs_list_head list; /* Previous/Next links */ + char *name; /* Stream name in current locale */ + ntfschar *uname; /* Unicode stream name */ + int uname_len; /* and its length */ + int resident; /* Stream is resident */ + int compressed; /* Stream is compressed */ + int encrypted; /* Stream is encrypted */ + long long size_alloc; /* Allocated size (multiple of cluster size) */ + long long size_data; /* Actual size of data */ + long long size_init; /* Initialised size, may be less than data size */ + long long size_vcn; /* Highest VCN in the data runs */ + runlist_element *runlist; /* Decoded data runs */ + int percent; /* Amount potentially recoverable */ + void *data; /* If resident, a pointer to the data */ +}; + +struct ufile { + long long inode; /* MFT record number */ + time_t date; /* Last modification date/time */ + struct ntfs_list_head name; /* A list of filenames */ + struct ntfs_list_head data; /* A list of data streams */ + char *pref_name; /* Preferred filename */ + char *pref_pname; /* parent filename */ + long long max_size; /* Largest size we find */ + int attr_list; /* MFT record may be one of many */ + int directory; /* MFT record represents a directory */ + MFT_RECORD *mft; /* Raw MFT record */ +}; + +#endif /* _NTFSUNDELETE_H_ */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsusermap.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsusermap.8.in new file mode 100644 index 0000000..a38e43d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsusermap.8.in @@ -0,0 +1,94 @@ +.\" Copyright (c) 2007-2016 Jean-Pierre André. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSUSERMAP 8 "February 2016" "ntfsusermap 1.2.0" +.SH NAME +ntfsusermap \- NTFS Building a User Mapping File +.SH SYNOPSIS +.B ntfsusermap +\fIwindows-system-device\fP +\fB[\fIother-ntfs-device\fP...\fB]\fR +.PP +Where \fIwindows-system-device\fP is the device containing the Windows system +whose users are to be mapped to current Linux system. +.PP +And \fIother-ntfs-device\fP is another device containing files which are +to be accessed both by the Windows mentioned above and current Linux system. +.PP +the ntfsusermap command must be started as root, and the designated devices +must not be mounted. +.PP +Typing ntfsusermap with no args will display a summary of command +arguments. +.SH DESCRIPTION +\fBntfsusermap\fR +creates the file defining the mapping of Windows accounts to Linux logins for +users who owns files which should be visible from both Windows and +Linux. +.PP +It relies on existing files which were created on Windows, trying +to locate significant files and asking which Linux user or group should +own them. When a Linux owner or group is requested, the reply may be : +.PP +- the uid or gid (numeric or symbolic) of Linux owner or group of the file. +.RS +In that situation, no more file with the same Windows owner will be selected. +.RE +- or no answer, when not able to define the owner or group. +.RS +In that situation another file owned by the same Windows user or group +may be selected later so that a mapping can be defined. +.RE +.PP +The mappings for standard Windows users, such as "Administrator" or +"All Users" are defined implicitly. As a consequence a user mapping should +never be defined as Linux root. +.PP +When there are no more significant files, ntfsusermap create the +mapping file into the file UserMapping in the current directory. This +file has to be moved to the hidden directory .NTFS-3G in the root of +all the NTFS file systems to be shared between Windows and Linux. This +requires the file system to be mounted, but the created file will not +be taken into account if not present at mount time, which means the +file system has to be unmounted and mounted again for the new mapping +file to be taken into account. +.SH OPTIONS +No option is defined for ntfsusermap. +.SH EXAMPLES +Map the users defined on the Windows system present on /dev/sda1 : +.RS +.sp +.B ntfsusermap /dev/sda1 +.sp +.RE +.PP +.SH EXIT CODES +.B ntfsusermap +exits with a value of 0 when no error was detected, and with a value +of 1 when an error was detected. +.SH KNOWN ISSUES +Please see +.RS +.sp +https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ/ +.sp +.RE +for common questions and known issues. +If you would find a new one in the latest release of +the software then please send an email describing it +in detail. You can contact the +development team on the ntfs\-3g\-devel@lists.sf.net +address. +.SH AUTHORS +.B ntfs-3g.secaudit +has been developed by Jean-Pierre André. +.SH THANKS +Several people made heroic efforts, often over five or more +years which resulted the ntfs-3g driver. Most importantly they are +Anton Altaparmakov, Richard Russon, Szabolcs Szakacsits, Yura Pakhuchiy, +Yuval Fledel, and the author of the groundbreaking FUSE filesystem development +framework, Miklos Szeredi. +.SH SEE ALSO +.BR ntfsprogs (8), +.BR attr (5), +.BR getfattr (1) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsusermap.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsusermap.c new file mode 100644 index 0000000..096d04c --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfsusermap.c @@ -0,0 +1,1139 @@ +/* + * Windows to Linux user mapping for ntfs-3g + * + * + * Copyright (c) 2007-2016 Jean-Pierre Andre + * + * A quick'n dirty program scanning owners of files in + * "c:\Documents and Settings" (and "c:\Users") + * and asking user to map them to Linux accounts + * + * History + * + * Sep 2007 + * - first version, limited to Win32 + * + * Oct 2007 + * - ported to Linux (rewritten would be more correct) + * + * Nov 2007 Version 1.0.0 + * - added more defaults + * + * Nov 2007 Version 1.0.1 + * - avoided examining files whose name begin with a '$' + * + * Jan 2008 Version 1.0.2 + * - moved user mapping file to directory .NTFS-3G (hidden for Linux) + * - fixed an error case in Windows version + * + * Nov 2008 Version 1.1.0 + * - fixed recursions for account in Linux version + * - searched owner in c:\Users (standard location for Vista) + * + * May 2009 Version 1.1.1 + * - reordered mapping records to limit usage of same SID for user and group + * - fixed decoding SIDs on 64-bit systems + * - fixed a pointer to dynamic data in mapping tables + * - fixed default mapping on Windows + * - fixed bug for renaming UserMapping on Windows + * + * May 2009 Version 1.1.2 + * - avoided selecting DOS names on Linux + * + * Nov 2009 Version 1.1.3 + * - silenced compiler warnings for unused parameters + * + * Jan 2010 Version 1.1.4 + * - fixed compilation problems for Mac OSX (Erik Larsson) + * + * Apr 2014 Version 1.1.5 + * - displayed the parent directory of selected files + * + * May 2014 Version 1.1.6 + * - fixed a wrong function header + * + * Mar 2016 Version 1.2.0 + * - reorganized to rely on libntfs-3g even on Windows + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * General parameters which may have to be adapted to needs + */ + +#define USERMAPVERSION "1.2.0" +#define MAPDIR ".NTFS-3G" +#define MAPFILE "UserMapping" +#define MAXATTRSZ 2048 +#define MAXSIDSZ 80 +#define MAXNAMESZ 256 +#define OWNERS1 "Documents and Settings" +#define OWNERS2 "Users" + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif + +#include "types.h" +#include "endians.h" +#include "support.h" +#include "layout.h" +#include "param.h" +#include "ntfstime.h" +#include "device_io.h" +#include "device.h" +#include "logging.h" +#include "runlist.h" +#include "mft.h" +#include "inode.h" +#include "attrib.h" +#include "bitmap.h" +#include "index.h" +#include "volume.h" +#include "unistr.h" +#include "mst.h" +#include "security.h" +#include "utils.h" +#include "misc.h" + +#ifdef HAVE_WINDOWS_H +/* + * Including leads to numerous conflicts with layout.h + * so define a few needed Windows calls unrelated to ntfs-3g + */ +BOOL WINAPI LookupAccountNameA(const char*, const char*, void*, + u32*, char*, u32*, s32*); +BOOL WINAPI GetUserNameA(char*, u32*); +#endif + +#ifdef HAVE_WINDOWS_H +#define DIRSEP "\\" +#else +#define DIRSEP "/" +#endif + +#ifdef HAVE_WINDOWS_H +#define BANNER "Generated by ntfsusermap for Windows, v " USERMAPVERSION +#else +#define BANNER "Generated by ntfsusermap for Linux, v " USERMAPVERSION +#endif + +typedef enum { DENIED, AGREED } boolean; + +enum STATES { STATE_USERS, STATE_HOMES, STATE_BASE } ; + +struct CALLBACK { + const char *accname; + const char *dir; + int levels; + enum STATES docset; +} ; + +typedef int (*dircallback)(struct CALLBACK *context, char *ntfsname, + int length, int type, long long pos, unsigned long long mft_ref, + unsigned int dt_type); + + +struct USERMAPPING { + struct USERMAPPING *next; + const char *uidstr; + const char *gidstr; + const char *sidstr; + const unsigned char *sid; + const char *login; + boolean defined; +}; + +struct USERMAPPING *firstmapping; +struct USERMAPPING *lastmapping; + +#ifdef HAVE_WINDOWS_H +char *currentwinname; +char *currentdomain; +unsigned char *currentsid; +#endif + +void *ntfs_handle; +void *ntfs_context = (void*)NULL; + +/* + * Open and close a volume in read-only mode + * assuming a single volume needs to be opened at any time + */ + +static boolean open_volume(const char *volume) +{ + boolean ok; + + ok = DENIED; + if (!ntfs_context) { + ntfs_context = ntfs_initialize_file_security(volume, + NTFS_MNT_RDONLY); + if (ntfs_context) { + fprintf(stderr,"\"%s\" opened\n",volume); + ok = AGREED; + } else { + fprintf(stderr,"Could not open \"%s\"\n",volume); +#ifdef HAVE_WINDOWS_H + if (errno == EACCES) + fprintf(stderr,"Make sure you have" + " Administrator rights\n"); +#else + fprintf(stderr,"Make sure \"%s\" is not mounted\n", + volume); +#endif + } + } else + fprintf(stderr,"A volume is already open\n"); + return (ok); +} + +static boolean close_volume(const char *volume) +{ + boolean r; + + r = ntfs_leave_file_security(ntfs_context) ? AGREED : DENIED; + if (r) + fprintf(stderr,"\"%s\" closed\n",volume); + else + fprintf(stderr,"Could not close \"%s\"\n",volume); + ntfs_context = (void*)NULL; + return (r); +} + +/* + * A poor man's conversion of Unicode to UTF8 + * We are assuming outputs to terminal expect UTF8 + */ + +static void to_utf8(char *dst, const char *src, unsigned int cnt) +{ + unsigned int ch; + unsigned int i; + + for (i=0; i> 6); + *dst++ = 0x80 + (ch & 63); + } else { + *dst++ = 0xe0 + (ch >> 12); + *dst++ = 0x80 + ((ch >> 6) & 63); + *dst++ = 0x80 + (ch & 63); + } + } + *dst = 0; +} + +static int utf8_size(const char *src, unsigned int cnt) +{ + unsigned int ch; + unsigned int i; + int size; + + size = 0; + for (i=0; inext = (struct USERMAPPING *)NULL; + mapping->defined = DENIED; + if (lastmapping) + lastmapping->next = mapping; + else + firstmapping = mapping; + lastmapping = mapping; + } + if (mapping) { + if (p && p[0]) { + idstr = (char *)malloc(strlen(p) + 1); + if (idstr) { + strcpy(idstr, p); + if (type) { + mapping->uidstr = ""; + mapping->gidstr = idstr; + } else { + mapping->uidstr = idstr; + mapping->gidstr = idstr; + } + mapping->defined = AGREED; + } + } + mapping->sidstr = sidstr; + if (accname) { + login = (char*)malloc(strlen(accname) + 1); + if (login) + strcpy(login,accname); + mapping->login = login; + } else + mapping->login = (char*)NULL; + sidsz = 8 + sid[1]*4; + p = (char*)malloc(sidsz); + if (p) { + memcpy(p, sid, sidsz); + } + mapping->sid = (unsigned char*)p; + } +} + +static void domapping(const char *accname, const char *filename, + const char *dir, const unsigned char *sid, int type) +{ + char *sidstr; + struct USERMAPPING *mapping; + + if ((get6h(sid, 2) == 5) && (get4l(sid, 8) == 21)) { + sidstr = decodesid(sid); + mapping = firstmapping; + while (mapping && strcmp(mapping->sidstr, sidstr)) + mapping = mapping->next; + if (mapping + && (mapping->defined + || !accname + || !strcmp(mapping->login, accname))) + free(sidstr); /* decision already known */ + else { + askmapping(accname, filename, dir, sid, type, + mapping, sidstr); + } + } +} + +static void listaclusers(const char *accname, const unsigned char *attr, + int off) +{ + int i; + int cnt; + int x; + + cnt = get2l(attr, off + 4); + x = 8; + for (i = 0; i < cnt; i++) { + domapping(accname, (char *)NULL, (char*)NULL, + &attr[off + x + 8], 2); + x += get2l(attr, off + x + 2); + } +} + +static void account(const char *accname, const char *dir, + const char *name, int type) +{ + unsigned char attr[MAXATTRSZ]; + u32 attrsz; + char *fullname; + + fullname = (char *)malloc(strlen(dir) + strlen(name) + 2); + if (fullname) { + strcpy(fullname, dir); + strcat(fullname, "/"); + strcat(fullname, name); + if (ntfs_get_file_security(ntfs_context, + fullname, OWNER_SECURITY_INFORMATION, + (char*)attr, MAXATTRSZ, &attrsz)) { + domapping(accname, name, dir, &attr[20], 0); + attrsz = 0; + if (ntfs_get_file_security(ntfs_context, + fullname, GROUP_SECURITY_INFORMATION, + (char*)attr, MAXATTRSZ, &attrsz)) + domapping(accname, name, dir, &attr[20], 1); + else + printf(" No group SID\n"); + attrsz = 0; + if (ntfs_get_file_security(ntfs_context, + fullname, DACL_SECURITY_INFORMATION, + (char*)attr, MAXATTRSZ, &attrsz)) { + if (type == 0) + listaclusers(accname, attr, 20); + } else + printf(" No discretionary access control" + " list for %s !\n", dir); + } + free(fullname); + } +} + +/* + * recursive search of file owners and groups in a directory + */ + +static boolean recurse(const char *accname, const char *dir, + int levels, enum STATES docset); + +static int callback(void *ctx, const ntfschar *ntfsname, + const int length, const int type, + const s64 pos __attribute__((unused)), + const MFT_REF mft_ref __attribute__((unused)), + const unsigned int dt_type __attribute__((unused))) +{ + struct CALLBACK *context; + char *fullname; + char *accname; + char *name; + + context = (struct CALLBACK*)ctx; + fullname = (char *)malloc(strlen(context->dir) + + utf8_size((const char*)ntfsname, length) + 2); + if (fullname) { + /* No "\\" when interfacing libntfs-3g */ + if (strcmp(context->dir,"/")) { + strcpy(fullname, context->dir); + strcat(fullname, "/"); + } else + strcpy(fullname,"/"); + /* Unicode to ascii conversion by a lazy man */ + name = &fullname[strlen(fullname)]; + to_utf8(name, (const char*)ntfsname, length); + /* ignore special files and DOS names */ + if ((type != 2) + && strcmp(name,".") + && strcmp(name,"..") + && (name[0] != '$')) { + switch (context->docset) { + case STATE_USERS : + /* + * only "Documents and Settings" + * or "Users" + */ + if (!strcmp(name,OWNERS1) + || !strcmp(name,OWNERS2)) { + recurse((char*)NULL, fullname, 2, + STATE_HOMES); + } + break; + /* + * within "Documents and Settings" + * or "Users" + */ + case STATE_HOMES : + accname = (char*)malloc(strlen(name) + 1); + if (accname) { + strcpy(accname, name); + if (context->levels > 0) + recurse(name, fullname, + context->levels - 1, + STATE_BASE); + } + break; + /* + * not related to "Documents and + * Settings" or "Users" + */ + case STATE_BASE : + account(context->accname, context->dir, + name, 1); + if (context->levels > 0) + recurse(context->accname, fullname, + context->levels - 1, + STATE_BASE); + break; + } + } + free(fullname); + } +/* check expected return value */ + return (0); +} + +static boolean recurse(const char *accname, const char *dir, + int levels, enum STATES docset) +{ + struct CALLBACK context; + boolean err; + + err = DENIED; + context.dir = dir; + context.accname = accname; + context.levels = levels; + context.docset = docset; + ntfs_read_directory(ntfs_context,dir,callback,&context); + return (!err); +} + +/* + * Search directory "Documents and Settings" for user accounts + */ + +static boolean getusers(const char *dir, int levels) +{ + boolean err; + struct CALLBACK context; + + printf("* Search for \"" OWNERS1 "\" and \"" OWNERS2 "\"\n"); + err = DENIED; + context.dir = dir; + context.accname = (const char*)NULL; + context.levels = levels; + context.docset = STATE_USERS; + ntfs_read_directory(ntfs_context,dir,callback,&context); + printf("* Search for other directories %s\n",dir); + context.docset = STATE_BASE; + ntfs_read_directory(ntfs_context,dir,callback,&context); + return (!err); +} + +#ifdef HAVE_WINDOWS_H +/* + * Get the current login name (Win32 only) + */ + +static void loginname(boolean silent) +{ + char *winname; + char *domain; + unsigned char *sid; + u32 namesz; + u32 sidsz; + u32 domainsz; + s32 nametype; + boolean ok; + int r; + + ok = FALSE; + winname = (char*)malloc(MAXNAMESZ); + domain = (char*)malloc(MAXNAMESZ); + sid = (char*)malloc(MAXSIDSZ); + + namesz = MAXNAMESZ; + domainsz = MAXNAMESZ; + sidsz = MAXSIDSZ; + if (winname + && domain + && sid + && GetUserNameA(winname,&namesz)) { + winname[namesz] = '\0'; + if (!silent) + printf("Your current user name is %s\n",winname); + nametype = 1; + r = LookupAccountNameA((char*)NULL,winname,sid,&sidsz, + domain,&domainsz,&nametype); + if (r) { + domain[domainsz] = '\0'; + if (!silent) + printf("Your account domain is %s\n",domain); + ok = AGREED; + } + } + if (ok) { + currentwinname = winname; + currentdomain = domain; + currentsid = sid; + } else { + currentwinname = (char*)NULL; + currentdomain = (char*)NULL; + currentsid = (unsigned char*)NULL; + } +} + +/* + * Minimal output on stdout + */ + +static boolean minimal(unsigned char *sid) +{ + const unsigned char *groupsid; + boolean ok; + + ok = DENIED; + if (sid) { + groupsid = makegroupsid(sid); + printf("# %s\n",BANNER); + printf("# For Windows account \"%s\" in domain \"%s\"\n", + currentwinname, currentdomain); + printf("# Replace \"user\" and \"group\" hereafter by" + " matching Linux login\n"); + printf("user::%s\n",decodesid(sid)); + printf(":group:%s\n",decodesid(groupsid)); + ok = AGREED; + } + return (ok); +} + +#endif + +/* + * Create a user mapping file + * + * From now on, partitions which were opened through ntfs-3g + * are closed, and we use the system drivers to create the file. + * On Windows, we can write on a partition which was analyzed. + */ + +static boolean outputmap(const char *volume, const char *dir) +{ + char buf[256]; + int fn; + char *fullname = NULL; + char *backup; + struct USERMAPPING *mapping; + boolean done; + boolean err; + boolean undecided; +#ifdef HAVE_WINDOWS_H + struct stat st; +#endif + + done = DENIED; + fullname = (char *)malloc(strlen(MAPFILE) + 1 + + strlen(volume) + 1 + + (dir ? strlen(dir) + 1 : 0)); + if (fullname) { +#ifdef HAVE_WINDOWS_H + strcpy(fullname, volume); + if (dir && dir[0]) { + strcat(fullname, DIRSEP); + strcat(fullname,dir); + } + + /* build directory, if not present */ + if (stat(fullname,&st) && (errno == ENOENT)) { + printf("* Creating directory %s\n", fullname); + mkdir(fullname); + } + + strcat(fullname, DIRSEP); + strcat(fullname, MAPFILE); + printf("\n"); +#else + strcpy(fullname, MAPFILE); + printf("\n"); +#endif + + backup = (char*)malloc(strlen(fullname) + 5); + if (backup) { + strcpy(backup,fullname); + strcat(backup,".bak"); +#ifdef HAVE_WINDOWS_H + unlink(backup); +#endif + if (!rename(fullname,backup)) { + printf("* Old mapping file moved to %s\n", + backup); + } + free(backup); + } + + printf("* Creating file %s\n", fullname); + err = DENIED; +#ifdef HAVE_WINDOWS_H + fn = open(fullname,O_CREAT + O_TRUNC + O_WRONLY + O_BINARY, + S_IREAD + S_IWRITE); +#else + fn = open(fullname,O_CREAT + O_TRUNC + O_WRONLY, + S_IREAD + S_IWRITE); +#endif + if (fn > 0) { + sprintf(buf,"# %s\n",BANNER); + if (!write(fn,buf,strlen(buf))) + err = AGREED; + printf("%s",buf); + undecided = DENIED; + /* records for owner only or group only */ + for (mapping = firstmapping; mapping && !err; + mapping = mapping->next) + if (mapping->defined + && (!mapping->uidstr[0] + || !mapping->gidstr[0])) { + sprintf(buf,"%s:%s:%s\n", + mapping->uidstr, + mapping->gidstr, + mapping->sidstr); + if (!write(fn,buf,strlen(buf))) + err = AGREED; + printf("%s",buf); + } else + undecided = AGREED; + /* records for both owner and group */ + for (mapping = firstmapping; mapping && !err; + mapping = mapping->next) + if (mapping->defined + && mapping->uidstr[0] + && mapping->gidstr[0]) { + sprintf(buf,"%s:%s:%s\n", + mapping->uidstr, + mapping->gidstr, + mapping->sidstr); + if (!write(fn,buf,strlen(buf))) + err = AGREED; + printf("%s",buf); + } else + undecided = AGREED; + done = !err; + close(fn); + if (undecided) { + printf("Undecided :\n"); + for (mapping = firstmapping; mapping; + mapping = mapping->next) + if (!mapping->defined) { + printf(" %s\n", + mapping->sidstr); + } + } +#ifndef HAVE_WINDOWS_H + printf("\n* You will have to move the file \"" + MAPFILE "\"\n"); + printf(" to directory \"" MAPDIR "\" after" + " mounting\n"); +#endif + } + } + if (!done) + fprintf(stderr, "* Could not create mapping file \"%s\"\n", + fullname); + + if (fullname) { + free(fullname); + } + + return (done); +} + +static boolean sanitize(void) +{ + char buf[81]; + boolean ok; + int ownercnt; + int groupcnt; + struct USERMAPPING *mapping; + struct USERMAPPING *firstowner; + struct USERMAPPING *genericgroup; + struct USERMAPPING *group; + char *sidstr; + + /* count owners and groups */ + /* and find first user, and a generic group */ + ownercnt = 0; + groupcnt = 0; + firstowner = (struct USERMAPPING*)NULL; + genericgroup = (struct USERMAPPING*)NULL; + for (mapping=firstmapping; mapping; mapping=mapping->next) { + if (mapping->defined && mapping->uidstr[0]) { + if (!ownercnt) + firstowner = mapping; + ownercnt++; + } + if (mapping->defined && mapping->gidstr[0] + && !mapping->uidstr[0]) { + groupcnt++; + } + if (!mapping->defined && isgenericgroup(mapping->sidstr)) { + genericgroup = mapping; + } + } +#ifdef HAVE_WINDOWS_H + /* no user defined, on Windows, suggest a mapping */ + /* based on account currently used */ + if (!ownercnt && currentwinname && currentsid) { + char *owner; + char *p; + + printf("\nYou have defined no file owner,\n"); + printf(" please enter the Linux login which should" + " be mapped\n"); + printf(" to account you are currently using\n"); + printf(" Linux user ? "); + p = fgets(buf, 80, stdin); + if (p && p[0] && (p[strlen(p) - 1] == '\n')) + p[strlen(p) - 1] = '\0'; + if (p && p[0]) { + firstowner = (struct USERMAPPING*)malloc( + sizeof(struct USERMAPPING)); + owner = (char*)malloc(strlen(p) + 1); + if (firstowner && owner) { + strcpy(owner, p); + firstowner->next = firstmapping; + firstowner->uidstr = owner; + firstowner->gidstr = ""; + firstowner->sidstr = decodesid(currentsid); + firstowner->sid = currentsid; + firstmapping = firstowner; + ownercnt++; + /* prefer a generic group with the same + * authorities */ + for (mapping=firstmapping; mapping; + mapping=mapping->next) + if (!mapping->defined + && isgenericgroup(mapping->sidstr) + && !memcmp(firstowner->sidstr, + mapping->sidstr, + strlen(mapping + ->sidstr)-3)) + genericgroup = mapping; + } + } + } +#endif + if (ownercnt) { + /* + * No group was selected, but there were a generic + * group, insist in using it, associated to the + * first user + */ + if (!groupcnt) { + printf("\nYou have defined no group," + " this can cause problems\n"); + printf("Do you accept defining a standard group ?\n"); + if (!fgets(buf,80,stdin) + || ((buf[0] != 'n') + && (buf[0] != 'N'))) { + if (genericgroup) { + genericgroup->uidstr = ""; + genericgroup->gidstr = + firstowner->uidstr; + genericgroup->defined = AGREED; + } else { + group = (struct USERMAPPING*) + malloc(sizeof( + struct USERMAPPING)); + sidstr = decodesid( + makegroupsid(firstowner->sid)); + if (group && sidstr) { + group->uidstr = ""; + group->gidstr = firstowner-> + uidstr; + group->sidstr = sidstr; + group->defined = AGREED; + group->next = firstmapping; + firstmapping = group; + } + } + } + } + ok = AGREED; + } else { + printf("\nYou have defined no user, no mapping can be built\n"); + ok = DENIED; + } + + return (ok); +} + +static boolean checkoptions(int argc, char *argv[] __attribute__((unused)), + boolean silent __attribute__((unused))) +{ + boolean err; +#ifdef HAVE_WINDOWS_H + int xarg; + const char *pvol; + + if (silent) { + err = (argc != 1); + } else { + err = (argc < 2); + for (xarg=1; (xarg= 'A') && (pvol[0] <= 'Z')) + || ((pvol[0] >= 'a') + && (pvol[0] <= 'z'))); + } + } + } + if (err) { + fprintf(stderr, "Usage : ntfsusermap [vol1: [vol2: ...]]\n"); + fprintf(stderr, " \"voln\" are the letters of the partition" + " to share with Linux\n"); + fprintf(stderr, " eg C:\n"); + fprintf(stderr, " the Windows system partition should be" + " named first\n"); + if (silent) { + fprintf(stderr, "When outputting to file, a minimal" + " user mapping proposal\n"); + fprintf(stderr, "is written to the file, and no" + " partition should be mentioned\n"); + } + } +#else + err = (argc < 2); + if (err) { + fprintf(stderr, "Usage : ntfsusermap dev1 [dev2 ...]\n"); + fprintf(stderr, " \"dev.\" are the devices to share" + " with Windows\n"); + fprintf(stderr, " eg /dev/sdb1\n"); + fprintf(stderr, " the devices should not be mounted, and\n"); + fprintf(stderr, " the Windows system partition should" + " be named first\n"); + } else + if (getuid()) { + fprintf(stderr, "\nSorry, only root can start" + " ntfsusermap\n"); + err = AGREED; + } +#endif + return (!err); +} + +static boolean process(int argc, char *argv[]) +{ + boolean ok; + int xarg; + int targ; + + firstmapping = (struct USERMAPPING *)NULL; + lastmapping = (struct USERMAPPING *)NULL; + ok = AGREED; + for (xarg=1; (xarg 2 ? 2 : 1); + if (!outputmap(argv[targ],MAPDIR)) { + printf("Trying to write file on root directory\n"); + if (outputmap(argv[targ],(const char*)NULL)) { + printf("\nNote : you will have to move the" + " file to directory \"%s\" on Linux\n", + MAPDIR); + } else + ok = DENIED; + } else + ok = DENIED; + } else + ok = DENIED; + return (ok); +} + +int main(int argc, char *argv[]) +{ + boolean ok; + boolean silent; + + silent = !isatty(1); + if (!silent) + welcome(); + if (checkoptions(argc, argv, silent)) { +#ifdef HAVE_WINDOWS_H + loginname(silent); + if (silent) + ok = minimal(currentsid); + else + ok = process(argc,argv); +#else + ok = process(argc,argv); +#endif + } else + ok = DENIED; + if (!ok) + exit(1); + return (0); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfswipe.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfswipe.8.in new file mode 100644 index 0000000..adf8fb2 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfswipe.8.in @@ -0,0 +1,134 @@ +.\" Copyright (c) 2014 Jean-Pierre Andre +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFSWIPE 8 "June 2014" "ntfs-3g @VERSION@" +.SH NAME +ntfswipe \- overwrite unused space on an NTFS volume +.SH SYNOPSIS +\fBntfswipe\fR [\fIoptions\fR] \fIdevice\fR +.SH DESCRIPTION +.B ntfswipe +clears all or part of unused space on an NTFS volume by overwriting +with zeroes or random bytes. +.SH OPTIONS +Below is a summary of all the options that +.B ntfswipe +accepts. Nearly all options have two equivalent names. The short name is +preceded by +.B \- +and the long name is preceded by +.BR \-\- . +Any single letter options, that don't take an argument, can be combined into a +single command, e.g. +.B \-fv +is equivalent to +.BR "\-f \-v" . +Long named options can be abbreviated to any unique prefix of their name. +.TP +\fB\-a\fR, \fB\-\-all\fR +Wipe all unused space. This may take significant time. If the option +\-\-unused-fast (or -U) is also present, the faster wiping method is used. +.TP +\fB\-b\fR, \fB\-\-bytes\fR BYTE-LIST +Define the allowed replacement bytes which are drawn randomly to overwrite +the unused space. BYTE-LIST is a comma-separated list of values in +range 0-255 expressed in octal, decimal or hexadecimal base. +.TP +\fB\-c\fR, \fB\-\-count\fR NUM +Define the number of times the unused space is to be overwritten. If both +options \fB\-\-bytes\fR and \fB\-\-count\fR are set, the space is +repeatedly overwritten this number of times by each of the values in the list. +.TP +\fB\-d\fR, \fB\-\-directory\fR +Wipe all the directory indexes, which may contain names of deleted files. +.TP +\fB\-f\fR, \fB\-\-force\fR +This will override some sensible defaults, such as not using a mounted volume. +Use this option with caution. +.TP +\fB\-h\fR, \fB\-\-help\fR +Show a list of options with a brief description of each one. +.TP +\fB\-i\fR, \fB\-\-info\fR +Display details about unused space, without wiping anything. +.TP +\fB\-l\fR, \fB\-\-logfile\fR +Overwrite the logfile (update journal). +.TP +\fB\-m\fR, \fB\-\-mft\fR +Overwrite the unused space in the MFT (main file table, which contains the +file names, and the contents of short files). +.TP +\fB\-n\fR, \fB\-\-no-action\fR +Executes the wiping process without writing to device. +.TP +\fB\-p\fR, \fB\-\-pagefile\fR +Overwrite the Windows swap space. +.TP +\fB\-q\fR, \fB\-\-quiet\fR +Suppress some debug/warning/error messages. +.TP +\fB\-s\fR, \fB\-\-undel\fR +Overwrite the space which had been allocated to a file which has been deleted +recently and is still undeletable. This option is not compatible with +\fB\-\-bytes\fR and the replacement bytes are random ones or taken from a +standard list. +.TP +\fB\-t\fR, \fB\-\-tails\fR +Overwrite the space at the end of files which is unused, but allocated +because the allocations are always done by full clusters. +.TP +\fB\-u\fR, \fB\-\-unused\fR +Overwrite the space which is currently not allocated to any file (but +may have been used in the past). +.TP +\fB\-U\fR, \fB\-\-unused-fast\fR +Overwrite the space which is currently not allocated to any file, trying +not to overwrite the space not written to since the previous wiping. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Display more debug/warning/error messages. This option may be used twice +to display even more messages. +.TP +\fB\-V\fR, \fB\-\-version\fR +Show the version number, copyright and license of +.BR ntfswipe . +.SH EXAMPLES +Wipe out all unused space in an NTFS volume. +.RS +.sp +.B ntfswipe -a /dev/sda1 +.sp +.RE +Wipe out all deleted file names from an NTFS volume. +.RS +.sp +.B ntfswipe -dms /dev/sda1 +.sp +.RE +.SH BUGS +There are no known problems with +.BR ntfswipe . +If you find a bug please send an email describing the problem to the +development team: +.br +.nh +ntfs\-3g\-devel@lists.sf.net +.hy +.SH AUTHORS +.B ntfswipe +was written by Richard Russon, Anton Altaparmakov and Yura Pakhuchiy. +It was ported to ntfs-3g by Erik Larsson. +.SH AVAILABILITY +.B ntfswipe +is part of the +.B ntfs-3g +package and is available from: +.br +.nh +https://github.com/tuxera/ntfs-3g/wiki/ +.hy +.SH SEE ALSO +.BR ntfs-3g (8), +.BR ntfsls (8), +.BR ntfsprogs (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfswipe.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfswipe.c new file mode 100644 index 0000000..ef67101 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfswipe.c @@ -0,0 +1,2297 @@ +/** + * ntfswipe - Part of the Linux-NTFS project. + * + * Copyright (c) 2005 Anton Altaparmakov + * Copyright (c) 2002-2005 Richard Russon + * Copyright (c) 2004 Yura Pakhuchiy + * + * This utility will overwrite unused space on an NTFS volume. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#else +#ifdef HAVE_MALLOC_H +#include +#endif +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif + +#include "ntfswipe.h" +#include "types.h" +#include "volume.h" +#include "utils.h" +#include "debug.h" +#include "dir.h" +#include "mst.h" +/* #include "version.h" */ +#include "logging.h" +#include "list.h" +#include "mft.h" + +static const char *EXEC_NAME = "ntfswipe"; +static struct options opts; +static unsigned long int npasses = 0; + +struct filename { + char *parent_name; + struct ntfs_list_head list; /* Previous/Next links */ + ntfschar *uname; /* Filename in unicode */ + int uname_len; /* and its length */ + /* Allocated size (multiple of cluster size) */ + s64 size_alloc; + s64 size_data; /* Actual size of data */ + long long parent_mref; + FILE_ATTR_FLAGS flags; + time_t date_c; /* Time created */ + time_t date_a; /* altered */ + time_t date_m; /* mft record changed */ + time_t date_r; /* read */ + char *name; /* Filename in current locale */ + FILE_NAME_TYPE_FLAGS name_space; + char padding[7]; /* Unused: padding to 64 bit. */ +}; + +struct data { + struct ntfs_list_head list; /* Previous/Next links */ + char *name; /* Stream name in current locale */ + ntfschar *uname; /* Unicode stream name */ + int uname_len; /* and its length */ + int resident; /* Stream is resident */ + int compressed; /* Stream is compressed */ + int encrypted; /* Stream is encrypted */ + /* Allocated size (multiple of cluster size) */ + s64 size_alloc; + s64 size_data; /* Actual size of data */ + /* Initialised size, may be less than data size */ + s64 size_init; + VCN size_vcn; /* Highest VCN in the data runs */ + runlist_element *runlist; /* Decoded data runs */ + int percent; /* Amount potentially recoverable */ + void *data; /* If resident, a pointer to the data */ + char padding[4]; /* Unused: padding to 64 bit. */ +}; + +struct ufile { + s64 inode; /* MFT record number */ + time_t date; /* Last modification date/time */ + struct ntfs_list_head name; /* A list of filenames */ + struct ntfs_list_head data; /* A list of data streams */ + char *pref_name; /* Preferred filename */ + char *pref_pname; /* parent filename */ + s64 max_size; /* Largest size we find */ + int attr_list; /* MFT record may be one of many */ + int directory; /* MFT record represents a directory */ + MFT_RECORD *mft; /* Raw MFT record */ + char padding[4]; /* Unused: padding to 64 bit. */ +}; + +#define NPAT 22 + +/* Taken from `shred' source */ +static const unsigned int patterns[NPAT] = { + 0x000, 0xFFF, /* 1-bit */ + 0x555, 0xAAA, /* 2-bit */ + 0x249, 0x492, 0x6DB, 0x924, 0xB6D, 0xDB6, /* 3-bit */ + 0x111, 0x222, 0x333, 0x444, 0x666, 0x777, + 0x888, 0x999, 0xBBB, 0xCCC, 0xDDD, 0xEEE /* 4-bit */ +}; + + +/** + * version - Print version information about the program + * + * Print a copyright statement and a brief description of the program. + * + * Return: none + */ +static void version(void) +{ + ntfs_log_info("\n%s v%s (libntfs-3g) - Overwrite the unused space on an NTFS " + "Volume.\n\n", EXEC_NAME, VERSION); + ntfs_log_info("Copyright (c) 2002-2005 Richard Russon\n"); + ntfs_log_info("Copyright (c) 2004 Yura Pakhuchiy\n"); + ntfs_log_info("\n%s\n%s%s\n", ntfs_gpl, ntfs_bugs, ntfs_home); +} + +/** + * usage - Print a list of the parameters to the program + * + * Print a list of the parameters and options for the program. + * + * Return: none + */ +static void usage(void) +{ + ntfs_log_info("\nUsage: %s [options] device\n" + " -i --info Show volume information (default)\n" + "\n" + " -d --directory Wipe directory indexes\n" + " -l --logfile Wipe the logfile (journal)\n" + " -m --mft Wipe mft space\n" + " -p --pagefile Wipe pagefile (swap space)\n" + " -t --tails Wipe file tails\n" + " -u --unused Wipe unused clusters\n" + " -U --unused-fast Wipe unused clusters (fast)\n" + " -s --undel Wipe undelete data\n" + "\n" + " -a --all Wipe all unused space\n" + "\n" + " -c num --count num Number of times to write(default = 1)\n" + " -b list --bytes list List of values to write(default = 0)\n" + "\n" + " -n --no-action Do not write to disk\n" + " -f --force Use less caution\n" + " -q --quiet Less output\n" + " -v --verbose More output\n" + " -V --version Version information\n" + " -h --help Print this help\n\n", + EXEC_NAME); + ntfs_log_info("%s%s\n", ntfs_bugs, ntfs_home); +} + +/** + * parse_list - Read a comma-separated list of numbers + * @list: The comma-separated list of numbers + * @result: Store the parsed list here (must be freed by caller) + * + * Read a comma-separated list of numbers and allocate an array of ints to store + * them in. The numbers can be in decimal, octal or hex. + * + * N.B. The caller must free the memory returned in @result. + * N.B. If the function fails, @result is not changed. + * + * Return: 0 Error, invalid string + * n Success, the count of numbers parsed + */ +static int parse_list(char *list, int **result) +{ + char *ptr; + char *end; + int i; + int count; + int *mem = NULL; + + if (!list || !result) + return 0; + + for (count = 0, ptr = list; ptr; ptr = strchr(ptr+1, ',')) + count++; + + mem = malloc((count+1) * sizeof(int)); + if (!mem) { + ntfs_log_error("Couldn't allocate memory in parse_list().\n"); + return 0; + } + + memset(mem, 0xFF, (count+1) * sizeof(int)); + + for (ptr = list, i = 0; i < count; i++) { + + end = NULL; + mem[i] = strtol(ptr, &end, 0); + + if (!end || (end == ptr) || ((*end != ',') && (*end != 0))) { + ntfs_log_error("Invalid list '%s'\n", list); + free(mem); + return 0; + } + + if ((mem[i] < 0) || (mem[i] > 255)) { + ntfs_log_error("Bytes must be in range 0-255.\n"); + free(mem); + return 0; + } + + ptr = end + 1; + } + + ntfs_log_debug("Parsing list '%s' - ", list); + for (i = 0; i <= count; i++) + ntfs_log_debug("0x%02x ", mem[i]); + ntfs_log_debug("\n"); + + *result = mem; + return count; +} + +/** + * parse_options - Read and validate the programs command line + * + * Read the command line, verify the syntax and parse the options. + * This function is very long, but quite simple. + * + * Return: 1 Success + * 0 Error, one or more problems + */ +static int parse_options(int argc, char *argv[]) +{ + static const char *sopt = "-ab:c:dfh?ilmnpqtuUvVs"; + static struct option lopt[] = { + { "all", no_argument, NULL, 'a' }, + { "bytes", required_argument, NULL, 'b' }, + { "count", required_argument, NULL, 'c' }, + { "directory", no_argument, NULL, 'd' }, + { "force", no_argument, NULL, 'f' }, + { "help", no_argument, NULL, 'h' }, + { "info", no_argument, NULL, 'i' }, + { "logfile", no_argument, NULL, 'l' }, + { "mft", no_argument, NULL, 'm' }, + { "no-action", no_argument, NULL, 'n' }, + //{ "no-wait", no_argument, NULL, 0 }, + { "pagefile", no_argument, NULL, 'p' }, + { "quiet", no_argument, NULL, 'q' }, + { "tails", no_argument, NULL, 't' }, + { "unused", no_argument, NULL, 'u' }, + { "unused-fast",no_argument, NULL, 'U' }, + { "undel", no_argument, NULL, 's' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + + int c = -1; + char *end; + int err = 0; + int ver = 0; + int help = 0; + int levels = 0; + + opterr = 0; /* We'll handle the errors, thank you. */ + + opts.count = 1; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opts.device) { + opts.device = argv[optind-1]; + } else { + opts.device = NULL; + err++; + } + break; + + case 'i': + opts.info++; /* and fall through */ + /* FALLTHRU */ + case 'a': + opts.directory++; + opts.logfile++; + opts.mft++; + opts.pagefile++; + opts.tails++; + opts.unused++; + opts.undel++; + break; + case 'b': + if (!opts.bytes) { + if (!parse_list(optarg, &opts.bytes)) + err++; + } else { + err++; + } + break; + case 'c': + if (opts.count == 1) { + end = NULL; + opts.count = strtol(optarg, &end, 0); + if (end && *end) + err++; + } else { + err++; + } + break; + case 'd': + opts.directory++; + break; + case 'f': + opts.force++; + break; + case 'h': + help++; + break; + case 'l': + opts.logfile++; + break; + case 'm': + opts.mft++; + break; + case 'n': + opts.noaction++; + break; + case 'p': + opts.pagefile++; + break; + case 'q': + opts.quiet++; + ntfs_log_clear_levels(NTFS_LOG_LEVEL_QUIET); + break; + case 's': + opts.undel++; + break; + case 't': + opts.tails++; + break; + case 'u': + opts.unused++; + break; + case 'U': + opts.unused_fast++; + break; + case 'v': + opts.verbose++; + ntfs_log_set_levels(NTFS_LOG_LEVEL_VERBOSE); + break; + case 'V': + ver++; + break; + case '?': + if (strncmp (argv[optind-1], "--log-", 6) == 0) { + if (!ntfs_log_parse_option (argv[optind-1])) + err++; + break; + } + /* fall through */ + default: + if ((optopt == 'b') || (optopt == 'c')) { + ntfs_log_error("Option '%s' requires an argument.\n", argv[optind-1]); + } else { + ntfs_log_error("Unknown option '%s'.\n", argv[optind-1]); + } + err++; + break; + } + } + + if (opts.bytes && opts.undel) { + ntfs_log_error("Options --bytes and --undel are not compatible.\n"); + err++; + } + /* Make sure we're in sync with the log levels */ + levels = ntfs_log_get_levels(); + if (levels & NTFS_LOG_LEVEL_VERBOSE) + opts.verbose++; + if (!(levels & NTFS_LOG_LEVEL_QUIET)) + opts.quiet++; + + if (help || ver) { + opts.quiet = 0; + } else { + if (opts.device == NULL) { + if (argc > 1) + ntfs_log_error("You must specify exactly one device.\n"); + err++; + } + + if (opts.quiet && opts.verbose) { + ntfs_log_error("You may not use --quiet and --verbose at the same time.\n"); + err++; + } + + /* + if (opts.info && (opts.unused || opts.tails || opts.mft || opts.directory)) { + ntfs_log_error("You may not use any other options with --info.\n"); + err++; + } + */ + + if ((opts.count < 1) || (opts.count > 100)) { + ntfs_log_error("The iteration count must be between 1 and 100.\n"); + err++; + } + + /* Create a default list */ + if (!opts.bytes) { + opts.bytes = malloc(2 * sizeof(int)); + if (opts.bytes) { + opts.bytes[0] = 0; + opts.bytes[1] = -1; + } else { + ntfs_log_error("Couldn't allocate memory for byte list.\n"); + err++; + } + } + + if (!opts.directory && !opts.logfile && !opts.mft && + !opts.pagefile && !opts.tails && !opts.unused && + !opts.unused_fast && !opts.undel) { + opts.info = 1; + } + } + + if (ver) + version(); + if (help || err) + usage(); + + /* tri-state 0 : done, 1 : error, -1 : proceed */ + return (err ? 1 : (help || ver ? 0 : -1)); +} + +/** + * wipe_unused - Wipe unused clusters + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * + * Read $Bitmap and wipe any clusters that are marked as not in use. + * + * Return: >0 Success, the attribute was wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_unused(ntfs_volume *vol, int byte, enum action act) +{ + s64 i; + s64 total = 0; + s64 result = 0; + u8 *buffer = NULL; + + if (!vol || (byte < 0)) + return -1; + + if (act != act_info) { + buffer = malloc(vol->cluster_size); + if (!buffer) { + ntfs_log_error("malloc failed\n"); + return -1; + } + memset(buffer, byte, vol->cluster_size); + } + + for (i = 0; i < vol->nr_clusters; i++) { + if (utils_cluster_in_use(vol, i)) { + //ntfs_log_verbose("cluster %lld is in use\n", i); + continue; + } + + if (act == act_wipe) { + //ntfs_log_verbose("cluster %lld is not in use\n", i); + result = ntfs_pwrite(vol->dev, vol->cluster_size * i, vol->cluster_size, buffer); + if (result != vol->cluster_size) { + ntfs_log_error("write failed\n"); + goto free; + } + } + + total += vol->cluster_size; + } + + ntfs_log_quiet("wipe_unused 0x%02x, %lld bytes\n", byte, (long long)total); +free: + free(buffer); + return total; +} + +/** + * wipe_unused_fast - Faster wipe unused clusters + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * + * Read $Bitmap and wipe any clusters that are marked as not in use. + * + * - read/write on a block basis (64 clusters, arbitrary) + * - skip of fully used block + * - skip non-used block already wiped + * + * Return: >0 Success, the attribute was wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_unused_fast(ntfs_volume *vol, int byte, enum action act) +{ + s64 i; + s64 total = 0; + s64 unused = 0; + s64 result; + u8 *buffer; + u8 *big_buffer; + u32 *u32_buffer; + u32 u32_bytes; + unsigned int blksize; + unsigned int j,k; + BOOL wipe_needed; + + if (!vol || (byte < 0)) + return -1; + + big_buffer = (u8*)malloc(vol->cluster_size*64); + if (!big_buffer) { + ntfs_log_error("malloc failed\n"); + return -1; + } + + for (i = 0; i < vol->nr_clusters; i+=64) { + blksize = vol->nr_clusters - i; + if (blksize > 64) + blksize = 64; + /* if all clusters in this block are used, ignore the block */ + result = 0; + for (j = 0; j < blksize; j++) { + if (utils_cluster_in_use(vol, i+j)) + result++; + } + unused += (blksize - result) * vol->cluster_size; + + if (result == blksize) { + continue; + } + /* + * if all unused clusters in this block are already wiped, + * ignore the block + */ + if (ntfs_pread(vol->dev, vol->cluster_size * i, + vol->cluster_size * blksize, big_buffer) + != vol->cluster_size * blksize) { + ntfs_log_error("Read failed at cluster %lld\n", + (long long)i); + goto free; + } + + result = 0; + wipe_needed = FALSE; + u32_bytes = (byte & 255)*0x01010101; + buffer = big_buffer; + for (j = 0; (j < blksize) && !wipe_needed; j++) { + u32_buffer = (u32*)buffer; + if (!utils_cluster_in_use(vol, i+j)) { + for (k = 0; (k < vol->cluster_size) + && (*u32_buffer++ == u32_bytes); k+=4) { + } + if (k < vol->cluster_size) + wipe_needed = TRUE; + } + buffer += vol->cluster_size; + } + + if (!wipe_needed) { + continue; + } + /* else wipe unused clusters in the block */ + buffer = big_buffer; + + for (j = 0; j < blksize; j++) { + if (!utils_cluster_in_use(vol, i+j)) { + memset(buffer, byte, vol->cluster_size); + total += vol->cluster_size; + } + buffer += vol->cluster_size; + } + + if ((act == act_wipe) + && (ntfs_pwrite(vol->dev, vol->cluster_size * i, + vol->cluster_size * blksize, big_buffer) + != vol->cluster_size * blksize)) { + ntfs_log_error("Write failed at cluster %lld\n", + (long long)i); + goto free; + } + } + + ntfs_log_quiet("wipe_unused_fast 0x%02x, %lld bytes" + " already wiped, %lld more bytes wiped\n", + byte, (long long)(unused - total), (long long)total); +free: + free(big_buffer); + return total; +} + +/** + * wipe_compressed_attribute - Wipe compressed $DATA attribute + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * @na: Opened ntfs attribute + * + * Return: >0 Success, the attribute was wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_compressed_attribute(ntfs_volume *vol, int byte, + enum action act, ntfs_attr *na) +{ + unsigned char *buf; + s64 size, offset, ret, wiped = 0; + le16 block_size_le; + u16 block_size; + VCN cur_vcn = 0; + runlist_element *rlc = na->rl; + s64 cu_mask = na->compression_block_clusters - 1; + runlist_element *restart = na->rl; + + while (rlc->length) { + cur_vcn += rlc->length; + if ((cur_vcn & cu_mask) || + (((rlc + 1)->length) && (rlc->lcn != LCN_HOLE))) { + rlc++; + continue; + } + + if (rlc->lcn == LCN_HOLE) { + runlist_element *rlt; + + offset = cur_vcn - rlc->length; + if (offset == (offset & (~cu_mask))) { + restart = rlc + 1; + rlc++; + continue; + } + offset = (offset & (~cu_mask)) + << vol->cluster_size_bits; + rlt = rlc; + while ((rlt - 1)->lcn == LCN_HOLE) rlt--; + while (1) { + ret = ntfs_rl_pread(vol, restart, + offset - (restart->vcn + << vol->cluster_size_bits), + 2, &block_size_le); + block_size = le16_to_cpu(block_size_le); + if (ret != 2) { + ntfs_log_verbose("Internal error\n"); + ntfs_log_error("ntfs_rl_pread failed"); + return -1; + } + if (block_size == 0) { + offset += 2; + break; + } + block_size &= 0x0FFF; + block_size += 3; + offset += block_size; + if (offset >= (((rlt->vcn) << + vol->cluster_size_bits) - 2)) + goto next; + } + size = (rlt->vcn << vol->cluster_size_bits) - offset; + } else { + size = na->allocated_size - na->data_size; + offset = (cur_vcn << vol->cluster_size_bits) - size; + } + + if (size < 0) { + ntfs_log_verbose("Internal error\n"); + ntfs_log_error("bug or damaged fs: we want " + "allocate buffer size %lld bytes", + (long long)size); + return -1; + } + + if ((act == act_info) || (!size)) { + wiped += size; + if (rlc->lcn == LCN_HOLE) + restart = rlc + 1; + rlc++; + continue; + } + + buf = malloc(size); + if (!buf) { + ntfs_log_verbose("Not enough memory\n"); + ntfs_log_error("Not enough memory to allocate " + "%lld bytes", + (long long)size); + return -1; + } + memset(buf, byte, size); + + ret = ntfs_rl_pwrite(vol, restart, + restart->vcn << vol->cluster_size_bits, + offset, size, buf); + free(buf); + if (ret != size) { + ntfs_log_verbose("Internal error\n"); + ntfs_log_error("ntfs_rl_pwrite failed, offset %llu, " + "size %lld, vcn %lld", + (unsigned long long)offset, + (long long)size, (long long)rlc->vcn); + return -1; + } + wiped += ret; +next: + if (rlc->lcn == LCN_HOLE) + restart = rlc + 1; + rlc++; + } + + return wiped; +} + +/** + * wipe_attribute - Wipe not compressed $DATA attribute + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * @na: Opened ntfs attribute + * + * Return: >0 Success, the attribute was wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_attribute(ntfs_volume *vol, int byte, enum action act, + ntfs_attr *na) +{ + unsigned char *buf; + s64 wiped; + s64 size; + u64 offset = na->data_size; + + if (!offset) + return 0; + if (na->data_flags & ATTR_IS_ENCRYPTED) + offset = (((offset - 1) >> 10) + 1) << 10; + size = (vol->cluster_size - offset) % vol->cluster_size; + + if (act == act_info) + return size; + + buf = malloc(size); + if (!buf) { + ntfs_log_verbose("Not enough memory\n"); + ntfs_log_error("Not enough memory to allocate %lld bytes", + (long long)size); + return -1; + } + memset(buf, byte, size); + + wiped = ntfs_rl_pwrite(vol, na->rl, 0, offset, size, buf); + if (wiped == -1) { + ntfs_log_verbose("Internal error\n"); + ntfs_log_error("Couldn't wipe tail"); + } + + free(buf); + return wiped; +} + +/* + * Wipe a data attribute tail + * + * Return: >0 Success, the clusters were wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ + +static s64 wipe_attr_tail(ntfs_inode *ni, ntfschar *name, int namelen, + int byte, enum action act) +{ + ntfs_attr *na; + ntfs_volume *vol = ni->vol; + s64 wiped; + + wiped = -1; + na = ntfs_attr_open(ni, AT_DATA, name, namelen); + if (!na) { + ntfs_log_error("Couldn't open $DATA attribute\n"); + goto close_attr; + } + + if (!NAttrNonResident(na)) { + ntfs_log_verbose("Resident $DATA attribute. Skipping.\n"); + goto close_attr; + } + + if (ntfs_attr_map_whole_runlist(na)) { + ntfs_log_verbose("Internal error\n"); + ntfs_log_error("Can't map runlist (inode %lld)\n", + (long long)ni->mft_no); + goto close_attr; + } + + if (na->data_flags & ATTR_COMPRESSION_MASK) + wiped = wipe_compressed_attribute(vol, byte, act, na); + else + wiped = wipe_attribute(vol, byte, act, na); + + if (wiped == -1) { + ntfs_log_error(" (inode %lld)\n", (long long)ni->mft_no); + } + +close_attr: + ntfs_attr_close(na); + return (wiped); +} + +/** + * wipe_tails - Wipe the file tails in all its data attributes + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * + * Disk space is allocated in clusters. If a file isn't an exact multiple of + * the cluster size, there is some slack space at the end. Wipe this space. + * + * Return: >0 Success, the clusters were wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_tails(ntfs_volume *vol, int byte, enum action act) +{ + s64 total = 0; + s64 nr_mft_records, inode_num; + ntfs_attr_search_ctx *ctx; + ntfs_inode *ni; + ATTR_RECORD *a; + ntfschar *name; + + if (!vol || (byte < 0)) + return -1; + + nr_mft_records = vol->mft_na->initialized_size >> + vol->mft_record_size_bits; + + /* Avoid getting fixup warnings on unitialized inodes */ + NVolSetNoFixupWarn(vol); + + for (inode_num = FILE_first_user; inode_num < nr_mft_records; + inode_num++) { + s64 attr_wiped; + s64 wiped = 0; + + ntfs_log_verbose("Inode %lld - ", (long long)inode_num); + ni = ntfs_inode_open(vol, inode_num); + if (!ni) { + if (opts.verbose) + ntfs_log_verbose("Could not open inode\n"); + else + ntfs_log_verbose("\r"); + continue; + } + + if (ni->mrec->base_mft_record) { + ntfs_log_verbose("Not base mft record. Skipping\n"); + goto close_inode; + } + + ctx = ntfs_attr_get_search_ctx(ni, (MFT_RECORD*)NULL); + if (!ctx) { + ntfs_log_error("Can't get a context, aborting\n"); + ntfs_inode_close(ni); + goto close_abort; + } + while (!ntfs_attr_lookup(AT_DATA, NULL, 0, CASE_SENSITIVE, 0, + NULL, 0, ctx)) { + a = ctx->attr; + + if (!ctx->al_entry || !ctx->al_entry->lowest_vcn) { + name = (ntfschar*)((u8*)a + + le16_to_cpu(a->name_offset)); + attr_wiped = wipe_attr_tail(ni, name, + a->name_length, byte, act); + if (attr_wiped > 0) + wiped += attr_wiped; + } + } + ntfs_attr_put_search_ctx(ctx); + if (wiped) { + ntfs_log_verbose("Wiped %llu bytes\n", + (unsigned long long)wiped); + total += wiped; + } else + ntfs_log_verbose("Nothing to wipe\n"); +close_inode: + ntfs_inode_close(ni); + } +close_abort : + NVolClearNoFixupWarn(vol); + ntfs_log_quiet("wipe_tails 0x%02x, %lld bytes\n", byte, + (long long)total); + return total; +} + +/** + * wipe_mft - Wipe the MFT slack space + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * + * MFT Records are 1024 bytes long, but some of this space isn't used. Wipe any + * unused space at the end of the record and wipe any unused records. + * + * Return: >0 Success, the clusters were wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_mft(ntfs_volume *vol, int byte, enum action act) +{ + // by considering the individual attributes we might be able to + // wipe a few more bytes at the attr's tail. + s64 nr_mft_records, i; + s64 total = 0; + s64 result = 0; + int size = 0; + MFT_RECORD *rec = NULL; + + if (!vol || (byte < 0)) + return -1; + + rec = (MFT_RECORD*)malloc(vol->mft_record_size); + if (!rec) { + ntfs_log_error("malloc failed\n"); + return -1; + } + + nr_mft_records = vol->mft_na->initialized_size >> + vol->mft_record_size_bits; + + for (i = 0; i < nr_mft_records; i++) { + if (utils_mftrec_in_use(vol, i)) { + result = ntfs_attr_mst_pread(vol->mft_na, vol->mft_record_size * i, + 1, vol->mft_record_size, rec); + if (result != 1) { + ntfs_log_error("error attr mst read %lld\n", + (long long)i); + total = -1; // XXX just negate result? + goto free; + } + + // We know that the end marker will only take 4 bytes + size = le32_to_cpu(rec->bytes_in_use) - 4; + + if ((size <= 0) || (size > (int)vol->mft_record_size)) { + ntfs_log_error("Bad mft record %lld\n", + (long long)i); + total = -1; + goto free; + } + if (act == act_info) { + //ntfs_log_info("mft %d\n", size); + total += size; + continue; + } + + memset(((u8*) rec) + size, byte, vol->mft_record_size - size); + } else { + const u16 usa_offset = + (vol->major_ver == 3) ? 0x0030 : 0x002A; + const u32 usa_size = 1 + + (vol->mft_record_size >> NTFS_BLOCK_SIZE_BITS); + const u16 attrs_offset = + ((usa_offset + usa_size) + 7) & ~((u16) 7); + const u32 bytes_in_use = attrs_offset + 8; + + if(usa_size > 0xFFFF || (usa_offset + usa_size) > + (NTFS_BLOCK_SIZE - sizeof(u16))) + { + ntfs_log_error("%d: usa_size out of bounds " + "(%u)\n", __LINE__, usa_size); + total = -1; + goto free; + } + + if (act == act_info) { + total += vol->mft_record_size; + continue; + } + + // Build the record from scratch + memset(rec, 0, vol->mft_record_size); + + // Common values + rec->magic = magic_FILE; + rec->usa_ofs = cpu_to_le16(usa_offset); + rec->usa_count = cpu_to_le16((u16) usa_size); + rec->sequence_number = const_cpu_to_le16(0x0001); + rec->attrs_offset = cpu_to_le16(attrs_offset); + rec->bytes_in_use = cpu_to_le32(bytes_in_use); + rec->bytes_allocated = cpu_to_le32(vol->mft_record_size); + rec->next_attr_instance = const_cpu_to_le16(0x0001); + + // End marker. + *((le32*) (((u8*) rec) + attrs_offset)) = const_cpu_to_le32(0xFFFFFFFF); + } + + result = ntfs_attr_mst_pwrite(vol->mft_na, vol->mft_record_size * i, + 1, vol->mft_record_size, rec); + if (result != 1) { + ntfs_log_error("error attr mst write %lld\n", + (long long)i); + total = -1; + goto free; + } + + if ((vol->mft_record_size * (i+1)) <= vol->mftmirr_na->allocated_size) + { + // We have to reduce the update sequence number, or else... + u16 offset; + le16 *usnp; + offset = le16_to_cpu(rec->usa_ofs); + usnp = (le16*) (((u8*) rec) + offset); + *usnp = cpu_to_le16(le16_to_cpu(*usnp) - 1); + + result = ntfs_attr_mst_pwrite(vol->mftmirr_na, vol->mft_record_size * i, + 1, vol->mft_record_size, rec); + if (result != 1) { + ntfs_log_error("error attr mst write %lld\n", + (long long)i); + total = -1; + goto free; + } + } + + total += vol->mft_record_size; + } + + ntfs_log_quiet("wipe_mft 0x%02x, %lld bytes\n", byte, (long long)total); +free: + free(rec); + return total; +} + +/** + * wipe_index_allocation - Wipe $INDEX_ALLOCATION attribute + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * @naa: Opened ntfs $INDEX_ALLOCATION attribute + * @nab: Opened ntfs $BITMAP attribute + * @indx_record_size: Size of INDX record + * + * Return: >0 Success, the clusters were wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_index_allocation(ntfs_volume *vol, int byte, enum action act + __attribute__((unused)), ntfs_attr *naa, ntfs_attr *nab, + u32 indx_record_size) +{ + s64 total = 0; + s64 wiped = 0; + s64 offset = 0; + s64 obyte = 0; + u64 wipe_offset; + s64 wipe_size; + u8 obit = 0; + u8 mask; + u8 *bitmap; + u8 *buf; + + bitmap = malloc(nab->data_size); + if (!bitmap) { + ntfs_log_verbose("malloc failed\n"); + ntfs_log_error("Couldn't allocate %lld bytes", + (long long)nab->data_size); + return -1; + } + + if (ntfs_attr_pread(nab, 0, nab->data_size, bitmap) + != nab->data_size) { + ntfs_log_verbose("Internal error\n"); + ntfs_log_error("Couldn't read $BITMAP"); + total = -1; + goto free_bitmap; + } + + buf = malloc(indx_record_size); + if (!buf) { + ntfs_log_verbose("malloc failed\n"); + ntfs_log_error("Couldn't allocate %u bytes", + (unsigned int)indx_record_size); + total = -1; + goto free_bitmap; + } + + while (offset < naa->allocated_size) { + mask = 1 << obit; + if (bitmap[obyte] & mask) { + INDEX_ALLOCATION *indx; + + s64 ret = ntfs_rl_pread(vol, naa->rl, + offset, indx_record_size, buf); + if (ret != indx_record_size) { + ntfs_log_verbose("ntfs_rl_pread failed\n"); + ntfs_log_error("Couldn't read INDX record"); + total = -1; + goto free_buf; + } + + indx = (INDEX_ALLOCATION *) buf; + if (ntfs_mst_post_read_fixup((NTFS_RECORD *)buf, + indx_record_size)) + ntfs_log_error("damaged fs: mst_post_read_fixup failed"); + + if ((le32_to_cpu(indx->index.allocated_size) + 0x18) != + indx_record_size) { + ntfs_log_verbose("Internal error\n"); + ntfs_log_error("INDX record should be %u bytes", + (unsigned int)indx_record_size); + total = -1; + goto free_buf; + } + + wipe_offset = le32_to_cpu(indx->index.index_length) + 0x18; + wipe_size = indx_record_size - wipe_offset; + memset(buf + wipe_offset, byte, wipe_size); + if (ntfs_mst_pre_write_fixup((NTFS_RECORD *)indx, + indx_record_size)) + ntfs_log_error("damaged fs: mst_pre_write_protect failed"); + if (opts.verbose > 1) + ntfs_log_verbose("+"); + } else { + wipe_size = indx_record_size; + memset(buf, byte, wipe_size); + if (opts.verbose > 1) + ntfs_log_verbose("x"); + } + + wiped = ntfs_rl_pwrite(vol, naa->rl, 0, offset, indx_record_size, buf); + if (wiped != indx_record_size) { + ntfs_log_verbose("ntfs_rl_pwrite failed\n"); + ntfs_log_error("Couldn't wipe tail of INDX record"); + total = -1; + goto free_buf; + } + total += wipe_size; + + offset += indx_record_size; + obit++; + if (obit > 7) { + obit = 0; + obyte++; + } + } + if ((opts.verbose > 1) && (wiped != -1)) + ntfs_log_verbose("\n\t"); +free_buf: + free(buf); +free_bitmap: + free(bitmap); + return total; +} + +/** + * get_indx_record_size - determine size of INDX record from $INDEX_ROOT + * @nar: Opened ntfs $INDEX_ROOT attribute + * + * Return: >0 Success, return INDX record size + * 0 Error, something went wrong + */ +static u32 get_indx_record_size(ntfs_attr *nar) +{ + u32 indx_record_size; + le32 indx_record_size_le; + + if (ntfs_attr_pread(nar, 8, 4, &indx_record_size_le) != 4) { + ntfs_log_verbose("Couldn't determine size of INDX record\n"); + ntfs_log_error("ntfs_attr_pread failed"); + return 0; + } + + indx_record_size = le32_to_cpu(indx_record_size_le); + if (!indx_record_size) { + ntfs_log_verbose("Internal error\n"); + ntfs_log_error("INDX record should be 0"); + } + return indx_record_size; +} + +/** + * wipe_directory - Wipe the directory indexes + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * + * Directories are kept in sorted B+ Trees. Index blocks may not be full. Wipe + * the unused space at the ends of these blocks. + * + * Return: >0 Success, the clusters were wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_directory(ntfs_volume *vol, int byte, enum action act) +{ + s64 total = 0; + s64 nr_mft_records, inode_num; + ntfs_inode *ni; + ntfs_attr *naa; + ntfs_attr *nab; + ntfs_attr *nar; + + if (!vol || (byte < 0)) + return -1; + + nr_mft_records = vol->mft_na->initialized_size >> + vol->mft_record_size_bits; + + /* Avoid getting fixup warnings on unitialized inodes */ + NVolSetNoFixupWarn(vol); + + for (inode_num = 5; inode_num < nr_mft_records; inode_num++) { + u32 indx_record_size; + s64 wiped; + + ntfs_log_verbose("Inode %lld - ", (long long)inode_num); + ni = ntfs_inode_open(vol, inode_num); + if (!ni) { + if (opts.verbose > 2) + ntfs_log_verbose("Could not open inode\n"); + else + ntfs_log_verbose("\r"); + continue; + } + + if (ni->mrec->base_mft_record) { + if (opts.verbose > 2) + ntfs_log_verbose("Not base mft record. Skipping\n"); + else + ntfs_log_verbose("\r"); + goto close_inode; + } + + naa = ntfs_attr_open(ni, AT_INDEX_ALLOCATION, NTFS_INDEX_I30, 4); + if (!naa) { + if (opts.verbose > 2) + ntfs_log_verbose("Couldn't open $INDEX_ALLOCATION\n"); + else + ntfs_log_verbose("\r"); + goto close_inode; + } + + if (!NAttrNonResident(naa)) { + ntfs_log_verbose("Resident $INDEX_ALLOCATION\n"); + ntfs_log_error("damaged fs: Resident $INDEX_ALLOCATION " + "(inode %lld)\n", (long long)inode_num); + goto close_attr_allocation; + } + + if (ntfs_attr_map_whole_runlist(naa)) { + ntfs_log_verbose("Internal error\n"); + ntfs_log_error("Can't map runlist for $INDEX_ALLOCATION " + "(inode %lld)\n", (long long)inode_num); + goto close_attr_allocation; + } + + nab = ntfs_attr_open(ni, AT_BITMAP, NTFS_INDEX_I30, 4); + if (!nab) { + ntfs_log_verbose("Couldn't open $BITMAP\n"); + ntfs_log_error("damaged fs: $INDEX_ALLOCATION is present, " + "but we can't open $BITMAP with same " + "name (inode %lld)\n", (long long)inode_num); + goto close_attr_allocation; + } + + nar = ntfs_attr_open(ni, AT_INDEX_ROOT, NTFS_INDEX_I30, 4); + if (!nar) { + ntfs_log_verbose("Couldn't open $INDEX_ROOT\n"); + ntfs_log_error("damaged fs: $INDEX_ALLOCATION is present, but " + "we can't open $INDEX_ROOT with same name" + " (inode %lld)\n", (long long)inode_num); + goto close_attr_bitmap; + } + + if (NAttrNonResident(nar)) { + ntfs_log_verbose("Not resident $INDEX_ROOT\n"); + ntfs_log_error("damaged fs: Not resident $INDEX_ROOT " + "(inode %lld)\n", (long long)inode_num); + goto close_attr_root; + } + + indx_record_size = get_indx_record_size(nar); + if (!indx_record_size) { + ntfs_log_error(" (inode %lld)\n", (long long)inode_num); + goto close_attr_root; + } + + wiped = wipe_index_allocation(vol, byte, act, + naa, nab, indx_record_size); + if (wiped == -1) { + ntfs_log_error(" (inode %lld)\n", + (long long)inode_num); + goto close_attr_root; + } + + if (wiped) { + ntfs_log_verbose("Wiped %llu bytes\n", + (unsigned long long)wiped); + total += wiped; + } else + ntfs_log_verbose("Nothing to wipe\n"); +close_attr_root: + ntfs_attr_close(nar); +close_attr_bitmap: + ntfs_attr_close(nab); +close_attr_allocation: + ntfs_attr_close(naa); +close_inode: + ntfs_inode_close(ni); + } + + NVolClearNoFixupWarn(vol); + ntfs_log_quiet("wipe_directory 0x%02x, %lld bytes\n", byte, + (long long)total); + return total; +} + +/** + * wipe_logfile - Wipe the logfile (journal) + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * + * The logfile journals the metadata to give the volume fault-tolerance. If the + * volume is in a consistent state, then this information can be erased. + * + * Return: >0 Success, the clusters were wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_logfile(ntfs_volume *vol, int byte, enum action act + __attribute__((unused))) +{ + const int NTFS_BUF_SIZE2 = 8192; + //FIXME(?): We might need to zero the LSN field of every single mft + //record as well. (But, first try without doing that and see what + //happens, since chkdsk might pickup the pieces and do it for us...) + ntfs_inode *ni; + ntfs_attr *na; + s64 len, pos, count; + char buf[NTFS_BUF_SIZE2]; + int eo; + + /* We can wipe logfile only with 0xff. */ + byte = 0xff; + + if (!vol || (byte < 0)) + return -1; + + //ntfs_log_quiet("wipe_logfile(not implemented) 0x%02x\n", byte); + + if ((ni = ntfs_inode_open(vol, FILE_LogFile)) == NULL) { + ntfs_log_debug("Failed to open inode FILE_LogFile.\n"); + return -1; + } + + if ((na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0)) == NULL) { + ntfs_log_debug("Failed to open $FILE_LogFile/$DATA.\n"); + goto error_exit; + } + + /* The $DATA attribute of the $LogFile has to be non-resident. */ + if (!NAttrNonResident(na)) { + ntfs_log_debug("$LogFile $DATA attribute is resident!?!\n"); + errno = EIO; + goto io_error_exit; + } + + /* Get length of $LogFile contents. */ + len = na->data_size; + if (!len) { + ntfs_log_debug("$LogFile has zero length, no disk write " + "needed.\n"); + return 0; + } + + /* Read $LogFile until its end. We do this as a check for correct + length thus making sure we are decompressing the mapping pairs + array correctly and hence writing below is safe as well. */ + pos = 0; + while ((count = ntfs_attr_pread(na, pos, NTFS_BUF_SIZE2, buf)) > 0) + pos += count; + + if (count == -1 || pos != len) { + ntfs_log_debug("Amount of $LogFile data read does not " + "correspond to expected length!\n"); + if (count != -1) + errno = EIO; + goto io_error_exit; + } + + /* Fill the buffer with @byte's. */ + memset(buf, byte, NTFS_BUF_SIZE2); + + /* Set the $DATA attribute. */ + pos = 0; + while ((count = len - pos) > 0) { + if (count > NTFS_BUF_SIZE2) + count = NTFS_BUF_SIZE2; + + if ((count = ntfs_attr_pwrite(na, pos, count, buf)) <= 0) { + ntfs_log_debug("Failed to set the $LogFile attribute " + "value.\n"); + if (count != -1) + errno = EIO; + goto io_error_exit; + } + + pos += count; + } + + ntfs_attr_close(na); + ntfs_inode_close(ni); + ntfs_log_quiet("wipe_logfile 0x%02x, %lld bytes\n", byte, + (long long)pos); + return pos; + +io_error_exit: + eo = errno; + ntfs_attr_close(na); + errno = eo; +error_exit: + eo = errno; + ntfs_inode_close(ni); + errno = eo; + return -1; +} + +/** + * wipe_pagefile - Wipe the pagefile (swap space) + * @vol: An ntfs volume obtained from ntfs_mount + * @byte: Overwrite with this value + * @act: Wipe, test or info + * + * pagefile.sys is used by Windows as extra virtual memory (swap space). + * Windows recreates the file at bootup, so it can be wiped without harm. + * + * Return: >0 Success, the clusters were wiped + * 0 Nothing to wipe + * -1 Error, something went wrong + */ +static s64 wipe_pagefile(ntfs_volume *vol, int byte, enum action act + __attribute__((unused))) +{ + // wipe completely, chkdsk doesn't do anything, booting writes header + const int NTFS_BUF_SIZE2 = 4096; + ntfs_inode *ni; + ntfs_attr *na; + s64 len, pos, count; + char buf[NTFS_BUF_SIZE2]; + int eo; + + if (!vol || (byte < 0)) + return -1; + + //ntfs_log_quiet("wipe_pagefile(not implemented) 0x%02x\n", byte); + + ni = ntfs_pathname_to_inode(vol, NULL, "pagefile.sys"); + if (!ni) { + ntfs_log_debug("Failed to open inode of pagefile.sys.\n"); + return 0; + } + + if ((na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0)) == NULL) { + ntfs_log_debug("Failed to open pagefile.sys/$DATA.\n"); + goto error_exit; + } + + /* The $DATA attribute of the pagefile.sys has to be non-resident. */ + if (!NAttrNonResident(na)) { + ntfs_log_debug("pagefile.sys $DATA attribute is resident!?!\n"); + errno = EIO; + goto io_error_exit; + } + + /* Get length of pagefile.sys contents. */ + len = na->data_size; + if (!len) { + ntfs_log_debug("pagefile.sys has zero length, no disk write " + "needed.\n"); + return 0; + } + + memset(buf, byte, NTFS_BUF_SIZE2); + + /* Set the $DATA attribute. */ + pos = 0; + while ((count = len - pos) > 0) { + if (count > NTFS_BUF_SIZE2) + count = NTFS_BUF_SIZE2; + + if ((count = ntfs_attr_pwrite(na, pos, count, buf)) <= 0) { + ntfs_log_debug("Failed to set the pagefile.sys " + "attribute value.\n"); + if (count != -1) + errno = EIO; + goto io_error_exit; + } + + pos += count; + } + + ntfs_attr_close(na); + ntfs_inode_close(ni); + ntfs_log_quiet("wipe_pagefile 0x%02x, %lld bytes\n", byte, + (long long)pos); + return pos; + +io_error_exit: + eo = errno; + ntfs_attr_close(na); + errno = eo; +error_exit: + eo = errno; + ntfs_inode_close(ni); + errno = eo; + return -1; +} + +/** + * Part of ntfsprogs. + * Modified: removed logging, signal handling, removed data. + * + * free_file - Release the resources used by a file object + * \param file The unwanted file object + * + * This will free up the memory used by a file object and iterate through the + * object's children, freeing their resources too. + * + * \return none + */ +static void free_file (struct ufile *file) +{ + struct ntfs_list_head *item = NULL, *tmp = NULL; + struct filename *f = NULL; + struct data *d = NULL; + + if (file == NULL) + return; + + ntfs_list_for_each_safe(item, tmp, &(file->name)) { + /* List of filenames */ + + f = ntfs_list_entry(item, struct filename, list); + if (f->name != NULL) + free(f->name); + if (f->parent_name != NULL) { + free(f->parent_name); + } + free(f); + } + + ntfs_list_for_each_safe(item, tmp, &(file->data)) { + /* List of data streams */ + + d = ntfs_list_entry(item, struct data, list); + if (d->name != NULL) + free(d->name); + if (d->runlist != NULL) + free(d->runlist); + free(d); + } + + + free(file->mft); + free(file); +} + +/** + * Fills the given buffer with one of predefined patterns. + * \param pat_no Pass number. + * \param buffer Buffer to be filled. + * \param buflen Length of the buffer. + */ +static void fill_buffer ( + unsigned long int pat_no, + unsigned char * const buffer, + const size_t buflen, + int * const selected ) + /*@requires notnull buffer @*/ /*@sets *buffer @*/ +{ + + size_t i; +#if (!defined HAVE_MEMCPY) && (!defined HAVE_STRING_H) + size_t j; +#endif + unsigned int bits; + + if ((buffer == NULL) || (buflen == 0)) + return; + + /* De-select all patterns once every npasses calls. */ + if (pat_no % npasses == 0) { + for (i = 0; i < NPAT; i++) { + selected[i] = 0; + } + } + pat_no %= npasses; + /* double check for npasses >= NPAT + 3: */ + for (i = 0; i < NPAT; i++) { + if (selected[i] == 0) + break; + } + if (i >= NPAT) { + for (i = 0; i < NPAT; i++) { + selected[i] = 0; + } + } + + /* The first, last and middle passess will be using a random pattern */ + if ((pat_no == 0) || (pat_no == npasses-1) || (pat_no == npasses/2)) { +#if (!defined __STRICT_ANSI__) && (defined HAVE_RANDOM) + bits = (unsigned int)(random() & 0xFFF); +#else + bits = (unsigned int)(rand() & 0xFFF); +#endif + } else { + /* For other passes, one of the fixed patterns is selected. */ + do { +#if (!defined __STRICT_ANSI__) && (defined HAVE_RANDOM) + i = (size_t)random() % NPAT; +#else + i = (size_t)rand() % NPAT; +#endif + } while (selected[i] == 1); + bits = patterns[i]; + selected[i] = 1; + } + + buffer[0] = (unsigned char) bits; + buffer[1] = (unsigned char) bits; + buffer[2] = (unsigned char) bits; + for (i = 3; i < buflen / 2; i *= 2) { +#ifdef HAVE_MEMCPY + memcpy(buffer + i, buffer, i); +#elif defined HAVE_STRING_H + strncpy((char *)(buffer + i), (char *)buffer, i); +#else + for (j = 0; j < i; j++) { + buffer[i+j] = buffer[j]; + } +#endif + } + if (i < buflen) { +#ifdef HAVE_MEMCPY + memcpy(buffer + i, buffer, buflen - i); +#elif defined HAVE_STRING_H + strncpy((char *)(buffer + i), (char *)buffer, buflen - i); +#else + for (j=0; jname)); + NTFS_INIT_LIST_HEAD(&(file->data)); + file->inode = record; + + file->mft = (MFT_RECORD*)malloc(nv->mft_record_size); + if (file->mft == NULL) { + free_file (file); + return -1; + } + + mft = ntfs_attr_open(nv->mft_ni, AT_DATA, AT_UNNAMED, 0); + if (mft == NULL) { + free_file(file); + return -2; + } + + /* Avoid getting fixup warnings on unitialized inodes */ + NVolSetNoFixupWarn(nv); + /* Read the MFT reocrd of the i-node */ + if (ntfs_attr_mst_pread(mft, nv->mft_record_size * record, 1LL, + nv->mft_record_size, file->mft) < 1) { + + NVolClearNoFixupWarn(nv); + ntfs_attr_close(mft); + free_file(file); + return -3; + } + NVolClearNoFixupWarn(nv); + ntfs_attr_close(mft); + mft = NULL; + + ctx = ntfs_attr_get_search_ctx(NULL, file->mft); + if (ctx == NULL) { + free_file(file); + return -4; + } + + /* Wiping file names */ + while (1 == 1) { + + if (ntfs_attr_lookup(AT_FILE_NAME, NULL, 0, CASE_SENSITIVE, + 0LL, NULL, 0, ctx) != 0) { + break; /* None / no more of that type */ + } + if (ctx->attr == NULL) + break; + + /* We know this will always be resident. + Find the offset of the data, including the MFT record. */ + a_offset = ((unsigned char *) ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + + for (pass = 0; pass < npasses; pass++) { + fill_buffer(pass, a_offset, + le32_to_cpu(ctx->attr->value_length), + selected); + + if ( !opts.noaction ) { + if (ntfs_mft_records_write(nv, + MK_MREF(record, 0), 1LL, + ctx->mrec) != 0) { + ret_wfs = -5; + break; + } + /* Flush after each writing, if more than + 1 overwriting needs to be done. Allow I/O + bufferring (efficiency), if just one + pass is needed. */ + if (npasses > 1) { + nv->dev->d_ops->sync(nv->dev); + } + } + + } + + /* Wiping file name length */ + for (pass = 0; pass < npasses; pass++) { + + fill_buffer (pass, (unsigned char *) + &(ctx->attr->value_length), sizeof(u32), + selected); + + if (!opts.noaction) { + if (ntfs_mft_records_write(nv, + MK_MREF(record, 0), + 1LL, ctx->mrec) != 0) { + ret_wfs = -5; + break; + } + + if (npasses > 1) { + nv->dev->d_ops->sync(nv->dev); + } + } + } + ctx->attr->value_length = const_cpu_to_le32(0); + if (!opts.noaction) { + if (ntfs_mft_records_write(nv, MK_MREF(record, 0), + 1LL, ctx->mrec) != 0) { + ret_wfs = -5; + break; + } + } + } + + ntfs_attr_reinit_search_ctx(ctx); + + /* Wiping file data */ + while (1 == 1) { + if (ntfs_attr_lookup(AT_DATA, NULL, 0, CASE_SENSITIVE, 0LL, + NULL, 0, ctx) != 0) { + break; /* None / no more of that type */ + } + if (ctx->attr == NULL) + break; + + if (ctx->attr->non_resident == 0) { + /* attribute is resident (part of MFT record) */ + /* find the offset of the data, including the MFT record */ + a_offset = ((unsigned char *) ctx->attr + + le16_to_cpu(ctx->attr->value_offset)); + + /* Wiping the data itself */ + for (pass = 0; pass < npasses; pass++) { + + fill_buffer (pass, a_offset, + le32_to_cpu(ctx->attr->value_length), + selected); + + if (!opts.noaction) { + if (ntfs_mft_records_write(nv, + MK_MREF(record, 0), + 1LL, ctx->mrec) != 0) { + ret_wfs = -5; + break; + } + + if (npasses > 1) { + nv->dev->d_ops->sync(nv->dev); + } + } + } + + /* Wiping data length */ + for (pass = 0; pass < npasses; pass++) { + + fill_buffer(pass, (unsigned char *) + &(ctx->attr->value_length), + sizeof(u32), selected); + + if (!opts.noaction) { + if (ntfs_mft_records_write(nv, + MK_MREF(record, 0), + 1LL, ctx->mrec) != 0) { + ret_wfs = -5; + break; + } + + if (npasses > 1) { + nv->dev->d_ops->sync(nv->dev); + } + } + } + ctx->attr->value_length = const_cpu_to_le32(0); + if ( !opts.noaction ) { + if (ntfs_mft_records_write(nv, + MK_MREF(record, 0), + 1LL, ctx->mrec) != 0) { + ret_wfs = -5; + break; + } + } + } else { + /* Non-resident here */ + + rl = ntfs_mapping_pairs_decompress(nv, + ctx->attr, NULL); + if (rl == NULL) { + continue; + } + + if (rl[0].length <= 0) { + continue; + } + + for (i = 0; (rl[i].length > 0) && (ret_wfs == 0); i++) { + if (rl[i].lcn == -1) { + continue; + } + for (j = rl[i].lcn; + (j < rl[i].lcn + rl[i].length) + && (ret_wfs == 0); j++) { + + if (utils_cluster_in_use(nv, j) != 0) + continue; + for (pass = 0; + pass < npasses; + pass++) { + + fill_buffer(pass, buf, + (size_t) nv->cluster_size, + selected); + if (!opts.noaction) { + if (ntfs_cluster_write( + nv, j, 1LL, + buf) < 1) { + ret_wfs = -5; + break; + } + + if (npasses > 1) { + nv->dev->d_ops->sync + (nv->dev); + } + } + } + } + } + + /* Wipe the data length here */ + for (pass = 0; pass < npasses; pass++) { + fill_buffer(pass, (unsigned char *) + &(ctx->attr->lowest_vcn), + sizeof(VCN), selected); + fill_buffer(pass, (unsigned char *) + &(ctx->attr->highest_vcn), + sizeof(VCN), selected); + fill_buffer(pass, (unsigned char *) + &(ctx->attr->allocated_size), + sizeof(s64), selected); + fill_buffer(pass, (unsigned char *) + &(ctx->attr->data_size), + sizeof(s64), selected); + fill_buffer(pass, (unsigned char *) + &(ctx->attr->initialized_size), + sizeof(s64), selected); + fill_buffer(pass, (unsigned char *) + &(ctx->attr->compressed_size), + sizeof(s64), selected); + + if ( !opts.noaction ) { + if (ntfs_mft_records_write(nv, + MK_MREF (record, 0), + 1LL, ctx->mrec) != 0) { + ret_wfs = -5; + break; + } + + if (npasses > 1) { + nv->dev->d_ops->sync(nv->dev); + } + } + } + ctx->attr->lowest_vcn = const_cpu_to_sle64(0); + ctx->attr->highest_vcn = const_cpu_to_sle64(0); + ctx->attr->allocated_size = const_cpu_to_sle64(0); + ctx->attr->data_size = const_cpu_to_sle64(0); + ctx->attr->initialized_size = const_cpu_to_sle64(0); + ctx->attr->compressed_size = const_cpu_to_sle64(0); + if (!opts.noaction) { + if (ntfs_mft_records_write(nv, + MK_MREF (record, 0), + 1LL, ctx->mrec) != 0) { + ret_wfs = -5; + break; + } + } + } /* end of resident check */ + } /* end of 'wiping file data' loop */ + + ntfs_attr_put_search_ctx(ctx); + free_file(file); + + return ret_wfs; +} + +/** + * Starts search for deleted inodes and undelete data on the given + * NTFS filesystem. + * \param FS The filesystem. + * \return 0 in case of no errors, other values otherwise. + */ +static int wipe_unrm(ntfs_volume *nv) +{ + int ret_wfs = 0, ret; + ntfs_attr *bitmapattr = NULL; + s64 bmpsize, size, nr_mft_records, i, j, k; + unsigned char b; + unsigned char * buf = NULL; + +#define MYBUF_SIZE 8192 + unsigned char *mybuf; +#define MINIM(x, y) ( ((x)<(y))?(x):(y) ) + + mybuf = (unsigned char *) malloc(MYBUF_SIZE); + if (mybuf == NULL) { + return -1; + } + + buf = (unsigned char *) malloc(nv->cluster_size); + if (buf == NULL) { + free (mybuf); + return -1; + } + + bitmapattr = ntfs_attr_open(nv->mft_ni, AT_BITMAP, AT_UNNAMED, 0); + if (bitmapattr == NULL) { + free (buf); + free (mybuf); + return -2; + } + bmpsize = bitmapattr->initialized_size; + + nr_mft_records = nv->mft_na->initialized_size + >> nv->mft_record_size_bits; + + /* just like ntfsundelete; detects i-node numbers fine */ + for (i = 0; (i < bmpsize) && (ret_wfs==0); i += MYBUF_SIZE) { + + /* read a part of the file bitmap */ + size = ntfs_attr_pread(bitmapattr, i, + MINIM((bmpsize - i), MYBUF_SIZE), mybuf); + if (size < 0) + break; + + /* parse each byte of the just-read part of the bitmap */ + for (j = 0; (j < size) && (ret_wfs==0); j++) { + b = mybuf[j]; + /* parse each bit of the byte Bit 1 means 'in use'. */ + for (k = 0; (k < CHAR_BIT) && (ret_wfs==0); + k++, b>>=1) { + /* (i+j)*8+k is the i-node bit number */ + if (((i+j)*CHAR_BIT+k) >= nr_mft_records) { + goto done; + } + if ((b & 1) != 0) { + /* i-node is in use, skip it */ + continue; + } + /* wiping the i-node here: */ + ret = destroy_record (nv, + (i+j)*CHAR_BIT+k, buf); + if (ret != 0) { + ret_wfs = ret; + } + } + } + } +done: + ntfs_attr_close(bitmapattr); + free(buf); + free(mybuf); + + ntfs_log_quiet("wipe_undelete\n"); + return ret_wfs; +} + + + +/** + * print_summary - Tell the user what we are about to do + * + * List the operations about to be performed. The output will be silenced by + * the --quiet option. + * + * Return: none + */ +static void print_summary(void) +{ + int i; + + if (opts.noaction) + ntfs_log_quiet("%s is in 'no-action' mode, it will NOT write to disk." + "\n\n", EXEC_NAME); + + ntfs_log_quiet("%s is about to wipe:\n", EXEC_NAME); + if (opts.unused) + ntfs_log_quiet("\tunused disk space\n"); + if (opts.unused_fast) + ntfs_log_quiet("\tunused disk space (fast)\n"); + if (opts.tails) + ntfs_log_quiet("\tfile tails\n"); + if (opts.mft) + ntfs_log_quiet("\tunused mft areas\n"); + if (opts.directory) + ntfs_log_quiet("\tunused directory index space\n"); + if (opts.logfile) + ntfs_log_quiet("\tthe logfile (journal)\n"); + if (opts.pagefile) + ntfs_log_quiet("\tthe pagefile (swap space)\n"); + if (opts.undel) + ntfs_log_quiet("\tundelete data\n"); + + ntfs_log_quiet("\n%s will overwrite these areas with: ", EXEC_NAME); + if (opts.bytes) { + for (i = 0; opts.bytes[i] >= 0; i++) + ntfs_log_quiet("0x%02x ", opts.bytes[i]); + } + ntfs_log_quiet("\n"); + if (opts.undel) + ntfs_log_quiet("(however undelete data will be overwritten" + " by random values)\n"); + + if (opts.count > 1) + ntfs_log_quiet("%s will repeat these operations %d times.\n", EXEC_NAME, opts.count); + ntfs_log_quiet("\n"); +} + +/** + * main - Begin here + * + * Start from here. + * + * Return: 0 Success, the program worked + * 1 Error, something went wrong + */ +int main(int argc, char *argv[]) +{ + ntfs_volume *vol; + int result = 1; + int flags = 0; + int res; + int i, j; + enum action act = act_info; + + ntfs_log_set_handler(ntfs_log_handler_outerr); + + res = parse_options(argc, argv); + if (res >= 0) + return (res); + + utils_set_locale(); + + if (!opts.info) + print_summary(); + + if (opts.info || opts.noaction) + flags = NTFS_MNT_RDONLY; + if (opts.force) + flags |= NTFS_MNT_RECOVER; + + vol = utils_mount_volume(opts.device, flags); + if (!vol) + goto free; + + if ((vol->flags & VOLUME_IS_DIRTY) && !opts.force) + goto umount; + + if (opts.info) { + act = act_info; + opts.count = 1; + } else if (opts.noaction) { + act = act_test; + } else { + act = act_wipe; + } + + /* Even if the output it quieted, you still get 5 seconds to abort. */ + if ((act == act_wipe) && !opts.force) { + ntfs_log_quiet("\n%s will begin in 5 seconds, press CTRL-C to abort.\n", EXEC_NAME); + sleep(5); + } + + for (i = 0; opts.bytes[i] >= 0; i++) { + npasses = i+1; + } + if (npasses == 0) { + npasses = opts.count; + } +#ifdef HAVE_TIME_H + srandom(time(NULL)); +#else + /* use a pointer as a pseudorandom value */ + srandom((int)vol + npasses); +#endif + ntfs_log_info("\n"); + for (i = 0; i < opts.count; i++) { + int byte; + s64 total = 0; + s64 wiped = 0; + + for (j = 0; byte = opts.bytes[j], byte >= 0; j++) { + + if (opts.directory) { + wiped = wipe_directory(vol, byte, act); + if (wiped < 0) + goto umount; + else + total += wiped; + } + + if (opts.tails) { + wiped = wipe_tails(vol, byte, act); + if (wiped < 0) + goto umount; + else + total += wiped; + } + + if (opts.logfile) { + wiped = wipe_logfile(vol, byte, act); + if (wiped < 0) + goto umount; + else + total += wiped; + } + + if (opts.mft) { + wiped = wipe_mft(vol, byte, act); + if (wiped < 0) + goto umount; + else + total += wiped; + } + + if (opts.pagefile) { + wiped = wipe_pagefile(vol, byte, act); + if (wiped < 0) + goto umount; + else + total += wiped; + } + + if (opts.unused || opts.unused_fast) { + if (opts.unused_fast) + wiped = wipe_unused_fast(vol, byte, + act); + else + wiped = wipe_unused(vol, byte, act); + if (wiped < 0) + goto umount; + else + total += wiped; + } + + if (opts.undel) { + wiped = wipe_unrm(vol); + if (wiped != 0) + goto umount; + /* + else + total += wiped; + */ + } + + if (act == act_info) + break; + } + + if (opts.noaction || opts.info) + ntfs_log_info("%lld bytes would be wiped" + " (excluding undelete data)\n", + (long long)total); + else + ntfs_log_info("%lld bytes were wiped" + " (excluding undelete data)\n", + (long long)total); + } + result = 0; +umount: + ntfs_umount(vol, FALSE); +free: + if (opts.bytes) + free(opts.bytes); + return result; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfswipe.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfswipe.h new file mode 100644 index 0000000..08620e5 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/ntfswipe.h @@ -0,0 +1,55 @@ +/* + * ntfswipe - Part of the Linux-NTFS project. + * + * Copyright (c) 2002 Richard Russon + * + * This utility will overwrite unused space on an NTFS volume. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFSWIPE_H_ +#define _NTFSWIPE_H_ + +#include "types.h" + +enum action { + act_info, + act_test, + act_wipe, +}; + +struct options { + char *device; /* Device/File to work with */ + int info; /* Show volume info */ + int force; /* Override common sense */ + int quiet; /* Less output */ + int verbose; /* Extra output */ + int noaction; /* Do not write to disk */ + int count; /* Number of iterations */ + int *bytes; /* List of overwrite characters */ + int directory; /* Wipe directory indexes */ + int logfile; /* Wipe the logfile (journal) */ + int mft; /* Wipe mft slack space */ + int pagefile; /* Wipe pagefile (swap space) */ + int tails; /* Wipe file tails */ + int unused; /* Wipe unused clusters */ + int unused_fast; /* Wipe unused clusters (fast) */ + int undel; /* Wipe undelete data */ +}; + +#endif /* _NTFSWIPE_H_ */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/playlog.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/playlog.c new file mode 100644 index 0000000..d5fc90d --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/playlog.c @@ -0,0 +1,4904 @@ +/* + * Redo or undo a list of logged actions + * + * Copyright (c) 2014-2017 Jean-Pierre Andre + * + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_MALLOC_H +#include +#endif +#ifdef HAVE_TIME_H +#include +#endif + +#include "types.h" +#include "endians.h" +#include "support.h" +#include "layout.h" +#include "param.h" +#include "ntfstime.h" +#include "device_io.h" +#include "device.h" +#include "logging.h" +#include "runlist.h" +#include "mft.h" +#include "inode.h" +#include "attrib.h" +#include "bitmap.h" +#include "index.h" +#include "volume.h" +#include "unistr.h" +#include "mst.h" +#include "logfile.h" +#include "ntfsrecover.h" +#include "misc.h" + +struct STORE { + struct STORE *upper; + struct STORE *lower; + LCN lcn; + char data[1]; +} ; + +#define dump hexdump + +struct STORE *cluster_door = (struct STORE*)NULL; + +/* check whether a MFT or INDX record is older than action */ +#define older_record(rec, logr) ((s64)(sle64_to_cpu((rec)->lsn) \ + - sle64_to_cpu((logr)->this_lsn)) < 0) +/* check whether a MFT or INDX record is newer than action */ +#define newer_record(rec, logr) ((s64)(sle64_to_cpu((rec)->lsn) \ + - sle64_to_cpu((logr)->this_lsn)) > 0) + +/* + * A few functions for debugging + */ + +static int matchcount(const char *d, const char *s, int n) +{ + int m; + + m = 0; + while ((--n >= 0) && (*d++ == *s++)) m++; + return (m); +} + +/* +static void locate(const char *s, int n, const char *p, int m) +{ + int i,j; + + for (i=0; i<=(n - m); i++) + if (s[i] == *p) { + j = 1; + while ((j < m) && (s[i + j] == p[j])) + j++; + if (j == m) + printf("=== found at offset 0x%x %d\n",i,i); + } +} +*/ + +static u64 inode_number(const LOG_RECORD *logr) +{ + u64 offset; + + offset = ((u64)sle64_to_cpu(logr->target_vcn) + << clusterbits) + + ((u32)le16_to_cpu(logr->cluster_index) + << NTFS_BLOCK_SIZE_BITS); + return (offset >> mftrecbits); +} + +/* + * Find an in-memory copy of a needed cluster + * + * Optionally, allocate a copy. + */ + +static struct STORE *getclusterentry(LCN lcn, BOOL create) +{ + struct STORE **current; + struct STORE *newone; + + current = &cluster_door; + /* A minimal binary tree should be enough */ + while (*current && (lcn != (*current)->lcn)) { + if (lcn > (*current)->lcn) + current = &(*current)->upper; + else + current = &(*current)->lower; + } + if (create && !*current) { + newone = (struct STORE*)malloc(sizeof(struct STORE) + + clustersz); + if (newone) { + newone->upper = (struct STORE*)NULL; + newone->lower = (struct STORE*)NULL; + newone->lcn = lcn; + *current = newone; + } + } + return (*current); +} + +void freeclusterentry(struct STORE *entry) +{ + if (!entry) { + if (cluster_door) + freeclusterentry(cluster_door); + cluster_door = (struct STORE*)NULL; + } else { + if (optv) + printf("* cluster 0x%llx %s updated\n", + (long long)entry->lcn, + (optn ? "would be" : "was")); + if (entry->upper) + freeclusterentry(entry->upper); + if (entry->lower) + freeclusterentry(entry->lower); + free(entry); + } +} + +/* + * Check whether an attribute type is a valid one + */ + +static BOOL valid_type(ATTR_TYPES type) +{ + BOOL ok; + + switch (type) { + case AT_STANDARD_INFORMATION : + case AT_ATTRIBUTE_LIST : + case AT_FILE_NAME : + case AT_OBJECT_ID : + case AT_SECURITY_DESCRIPTOR : + case AT_VOLUME_NAME : + case AT_VOLUME_INFORMATION : + case AT_DATA : + case AT_INDEX_ROOT : + case AT_INDEX_ALLOCATION : + case AT_BITMAP : + case AT_REPARSE_POINT : + case AT_EA_INFORMATION : + case AT_EA : + case AT_PROPERTY_SET : + case AT_LOGGED_UTILITY_STREAM : + case AT_FIRST_USER_DEFINED_ATTRIBUTE : + case AT_END : + ok = TRUE; + break; + default : + ok = FALSE; + break; + } + return (ok); +} + +/* + * Rough check of sanity of an index list + */ + +static int sanity_indx_list(const char *buffer, u32 k, u32 end) +{ + le64 inode; + int err; + int lth; + BOOL done; + + err = 0; + done = FALSE; + while ((k <= end) && !done && !err) { + lth = getle16(buffer,k+8); + if (optv > 1) + /* Usual indexes can be determined from size */ + switch (lth) { + case 16 : /* final without subnode */ + case 24 : /* final with subnode */ + printf("index to none lth 0x%x" + " flags 0x%x pos 0x%x\n", + (int)lth, + (int)getle16(buffer,k+12),(int)k); + break; + case 32 : /* $R in $Reparse */ + /* Badly aligned */ + memcpy(&inode, &buffer[k + 20], 8); + printf("index to reparse of 0x%016llx lth 0x%x" + " flags 0x%x pos 0x%x\n", + (long long)le64_to_cpu(inode), + (int)lth, + (int)getle16(buffer,k+12),(int)k); + break; + case 40 : /* $SII in $Secure */ + printf("index to securid 0x%lx lth 0x%x" + " flags 0x%x pos 0x%x\n", + (long)getle32(buffer,k + 16), + (int)lth, + (int)getle16(buffer,k+12),(int)k); + break; + case 48 : /* $SDH in $Secure */ + printf("index to securid 0x%lx lth 0x%x" + " flags 0x%x pos 0x%x\n", + (long)getle32(buffer,k + 20), + (int)lth, + (int)getle16(buffer,k+12),(int)k); + break; + default : /* at least 80 */ + printf("index to inode 0x%016llx lth 0x%x" + " flags 0x%x pos 0x%x\n", + (long long)getle64(buffer,k), + (int)lth, + (int)getle16(buffer,k+12),(int)k); + if ((lth < 80) || (lth & 7)) { + printf("** Invalid index record" + " length %d\n",lth); + err = 1; + } + } + done = (feedle16(buffer,k+12) & INDEX_ENTRY_END) || !lth; + if (lth & 7) { + if (optv <= 1) /* Do not repeat the warning */ + printf("** Invalid index record length %d\n", + lth); + err = 1; + } else + k += lth; + } + if (k != end) { + printf("** Bad index record length %ld (computed %ld)\n", + (long)end, (long)k); + err = 1; + } + if (!done) { + printf("** Missing end of index mark\n"); + err = 1; + } + return (err); +} + +/* + * Rough check of sanity of an mft record + */ + +static int sanity_mft(const char *buffer) +{ + const MFT_RECORD *record; + const ATTR_RECORD *attr; + u64 instances; + u32 k; + u32 type; + u32 prevtype; + u16 nextinstance; + u16 instance; + int err; + + err = 0; + record = (const MFT_RECORD*)buffer; + nextinstance = le16_to_cpu(record->next_attr_instance); + instances = 0; + k = le16_to_cpu(record->attrs_offset); + attr = (const ATTR_RECORD*)&buffer[k]; + prevtype = 0; + while ((k < mftrecsz) + && (attr->type != AT_END) + && valid_type(attr->type)) { + type = le32_to_cpu(attr->type); + if (type < prevtype) { + printf("** Bad type ordering 0x%lx after 0x%lx\n", + (long)type, (long)prevtype); + err = 1; + } + instance = le16_to_cpu(attr->instance); + /* Can nextinstance wrap around ? */ + if (instance >= nextinstance) { + printf("** Bad attr instance %d (max %d)\n", + (int)instance, (int)nextinstance - 1); + err = 1; + } + if (instance < 64) { + /* Only check up to 64 */ + if (((u64)1 << instance) & instances) { + printf("** Duplicated attr instance %d\n", + (int)instance); + } + instances |= (u64)1 << instance; + } + if (optv > 1) { + if ((attr->type == AT_FILE_NAME) + && buffer[k + 88]) { + printf("attr %08lx offs 0x%x nres %d", + (long)type, (int)k, + (int)attr->non_resident); + showname(" ",&buffer[k+90], + buffer[k + 88] & 255); + } else + printf("attr %08lx offs 0x%x nres %d\n", + (long)type, (int)k, + (int)attr->non_resident); + } + if ((attr->type == AT_INDEX_ROOT) + && sanity_indx_list(buffer, + k + le16_to_cpu(attr->value_offset) + 32, + k + le32_to_cpu(attr->length))) { + err = 1; + } + k += le32_to_cpu(attr->length); + attr = (const ATTR_RECORD*)&buffer[k]; + prevtype = type; + } + if ((optv > 1) && (attr->type == AT_END)) + printf("attr %08lx offs 0x%x\n", + (long)le32_to_cpu(attr->type), (int)k); + if ((attr->type != AT_END) + || (le32_to_cpu(record->bytes_in_use) != (k + 8)) + || (le32_to_cpu(record->bytes_allocated) < (k + 8))) { + printf("** Bad MFT record length %ld" + " (computed %ld allocated %ld)\n", + (long)le32_to_cpu(record->bytes_in_use), + (long)(k + 8), + (long)le32_to_cpu(record->bytes_allocated)); + err = 1; + } + return (err); +} + +/* + * Rough check of sanity of an index block + */ + +static int sanity_indx(ntfs_volume *vol, const char *buffer) +{ + const INDEX_BLOCK *indx; + u32 k; + int err; + + err = 0; + indx = (const INDEX_BLOCK*)buffer; + k = offsetof(INDEX_BLOCK, index) + + le32_to_cpu(indx->index.entries_offset); + err = sanity_indx_list(buffer, k, + le32_to_cpu(indx->index.index_length) + 24); + if ((le32_to_cpu(indx->index.index_length) + > le32_to_cpu(indx->index.allocated_size)) + || (le32_to_cpu(indx->index.allocated_size) + != (vol->indx_record_size - 24))) { + printf("** Bad index length %ld" + " (usable %ld allocated %ld)\n", + (long)le32_to_cpu(indx->index.index_length), + (long)(vol->indx_record_size - 24), + (long)le32_to_cpu(indx->index.allocated_size)); + err = 1; + } + return (err); +} + + +/* + * Allocate a buffer and read a full set of raw clusters + * + * Do not use for accessing $LogFile. + * With option -n reading is first attempted from the memory store + */ + +static char *read_raw(ntfs_volume *vol, const LOG_RECORD *logr) +{ + char *buffer; + char *target; + struct STORE *store; + LCN lcn; + int count; + int i; + BOOL fail; + + count = le16_to_cpu(logr->lcns_to_follow); + if (!count) { + printf("** Error : no lcn to read from\n"); + buffer = (char*)NULL; + } else + buffer = (char*)malloc(clustersz*count); +// TODO error messages + if (buffer) { + fail = FALSE; + for (i=0; (ilcn_list[i]); + target = buffer + clustersz*i; + if (optn) { + store = getclusterentry(lcn, FALSE); + if (store) { + memcpy(target, store->data, clustersz); + if (optv) + printf("== lcn 0x%llx from store\n", + (long long)lcn); + if ((optv > 1) && optc + && within_lcn_range(logr)) + dump(store->data, clustersz); + } + } + if (!store + && (ntfs_pread(vol->dev, lcn << clusterbits, + clustersz, target) != clustersz)) { + fail = TRUE; + } else { + if (!store) { + if (optv) + printf("== lcn 0x%llx" + " from device\n", + (long long)lcn); + if ((optv > 1) && optc + && within_lcn_range(logr)) + dump(target, clustersz); + } + } + } + if (fail) { + printf("** Could not read cluster 0x%llx\n", + (long long)lcn); + free(buffer); + buffer = (char*)NULL; + } + } + return (buffer); +} + +/* + * Write a full set of raw clusters + * + * Do not use for accessing $LogFile. + * With option -n a copy of the buffer is kept in memory for later use. + */ + +static int write_raw(ntfs_volume *vol, const LOG_RECORD *logr, + char *buffer) +{ + int err; + struct STORE *store; + LCN lcn; + char *source; + int count; + int i; + + err = 0; + count = le16_to_cpu(logr->lcns_to_follow); + if (!count) + printf("** Error : no lcn to write to\n"); + if (optn) { + for (i=0; (ilcn_list[i]); + source = buffer + clustersz*i; + store = getclusterentry(lcn, TRUE); + if (store) { + memcpy(store->data, source, clustersz); + if (optv) + printf("== lcn 0x%llx to store\n", + (long long)lcn); + if ((optv > 1) && optc + && within_lcn_range(logr)) + dump(store->data, clustersz); + } else { + printf("** Could not store cluster 0x%llx\n", + (long long)lcn); + err = 1; + } + } + } else { + for (i=0; (ilcn_list[i]); + if (optv) + printf("== lcn 0x%llx to device\n", + (long long)lcn); + source = buffer + clustersz*i; + if (ntfs_pwrite(vol->dev, lcn << clusterbits, + clustersz, source) != clustersz) { + printf("** Could not write cluster 0x%llx\n", + (long long)lcn); + err = 1; + } + } + } + return (err); +} + +/* + * Write a full set of raw clusters to mft_mirr + */ + +static int write_mirr(ntfs_volume *vol, const LOG_RECORD *logr, + char *buffer) +{ + int err; + LCN lcn; + char *source; + int count; + int i; + + err = 0; + count = le16_to_cpu(logr->lcns_to_follow); + if (!count) + printf("** Error : no lcn to write to\n"); + if (!optn) { + for (i=0; (imftmirr_na, + sle64_to_cpu(logr->target_vcn) + i); + source = buffer + clustersz*i; + if ((lcn < 0) + || (ntfs_pwrite(vol->dev, lcn << clusterbits, + clustersz, source) != clustersz)) { + printf("** Could not write cluster 0x%llx\n", + (long long)lcn); + err = 1; + } + } + } + return (err); +} + +/* + * Allocate a buffer and read a single protected record + */ + +static char *read_protected(ntfs_volume *vol, const LOG_RECORD *logr, + u32 size, BOOL warn) +{ + char *buffer; + char *full; + u32 pos; + LCN lcn; + + /* read full clusters */ + buffer = read_raw(vol, logr); + /* + * if the record is smaller than a cluster, + * make a partial copy and free the full buffer + */ + if (buffer && (size < clustersz)) { + full = buffer; + buffer = (char*)malloc(size); + if (buffer) { + pos = le16_to_cpu(logr->cluster_index) + << NTFS_BLOCK_SIZE_BITS; + memcpy(buffer, full + pos, size); + } + free(full); + } + if (buffer && (ntfs_mst_post_read_fixup_warn( + (NTFS_RECORD*)buffer, size, FALSE) < 0)) { + if (warn) { + lcn = sle64_to_cpu(logr->lcn_list[0]); + printf("** Invalid protected record at 0x%llx" + " index %d\n", + (long long)lcn, + (int)le16_to_cpu(logr->cluster_index)); + } + free(buffer); + buffer = (char*)NULL; + } + return (buffer); +} + +/* + * Protect a single record, write, and deallocate the buffer + * + * With option -n a copy of the buffer is kept in protected form in + * memory for later use. + * As the store only knows about clusters, if the record is smaller + * than a cluster, have to read, merge and write. + */ + +static int write_protected(ntfs_volume *vol, const LOG_RECORD *logr, + char *buffer, u32 size) +{ + MFT_RECORD *record; + INDEX_BLOCK *indx; + char *full; + u32 pos; + BOOL mftmirr; + BOOL checked; + int err; + + err = 0; + mftmirr = FALSE; + checked = FALSE; + if ((size == mftrecsz) && !memcmp(buffer,"FILE",4)) { + record = (MFT_RECORD*)buffer; + if (optv) + printf("update inode %ld lsn 0x%llx" + " (record %s than action 0x%llx)\n", + (long)le32_to_cpu(record->mft_record_number), + (long long)sle64_to_cpu(record->lsn), + ((s64)(sle64_to_cpu(record->lsn) + - sle64_to_cpu(logr->this_lsn)) < 0 ? + "older" : "newer"), + (long long)sle64_to_cpu(logr->this_lsn)); + if (optv > 1) + printf("mft vcn %lld index %d\n", + (long long)sle64_to_cpu(logr->target_vcn), + (int)le16_to_cpu(logr->cluster_index)); + err = sanity_mft(buffer); + /* Should set to some previous lsn for undos */ + if (opts) + record->lsn = logr->this_lsn; + /* Duplicate on mftmirr if not overflowing its size */ + mftmirr = (((u64)sle64_to_cpu(logr->target_vcn) + + le16_to_cpu(logr->lcns_to_follow)) + << clusterbits) + <= (((u64)vol->mftmirr_size) << mftrecbits); + checked = TRUE; + } + if ((size == vol->indx_record_size) && !memcmp(buffer,"INDX",4)) { + indx = (INDEX_BLOCK*)buffer; + if (optv) + printf("update index lsn 0x%llx" + " (index %s than action 0x%llx)\n", + (long long)sle64_to_cpu(indx->lsn), + ((s64)(sle64_to_cpu(indx->lsn) + - sle64_to_cpu(logr->this_lsn)) < 0 ? + "older" : "newer"), + (long long)sle64_to_cpu(logr->this_lsn)); + err = sanity_indx(vol, buffer); + /* Should set to some previous lsn for undos */ + if (opts) + indx->lsn = logr->this_lsn; + checked = TRUE; + } + if (!checked) { + printf("** Error : writing protected record of unknown type\n"); + err = 1; + } + if (!err) { + if (!ntfs_mst_pre_write_fixup((NTFS_RECORD*)buffer, size)) { + /* + * If the record is smaller than a cluster, get a full + * cluster, merge and write. + */ + if (size < clustersz) { + full = read_raw(vol, logr); + if (full) { + pos = le16_to_cpu(logr->cluster_index) + << NTFS_BLOCK_SIZE_BITS; + memcpy(full + pos, buffer, size); + err = write_raw(vol, logr, full); + if (!err && mftmirr && !optn) + err = write_mirr(vol, logr, + full); + free(full); + } else + err = 1; + } else { + /* write full clusters */ + err = write_raw(vol, logr, buffer); + if (!err && mftmirr && !optn) + err = write_mirr(vol, logr, buffer); + } + } else { + printf("** Failed to protect record\n"); + err = 1; + } + } + return (err); +} + +/* + * Resize attribute records + * + * The attribute value is resized to new size, but the attribute + * and MFT record must be kept aligned to 8 bytes. + */ + +static int resize_attribute(MFT_RECORD *entry, ATTR_RECORD *attr, INDEX_ROOT *index, + int rawresize, int resize) +{ + int err; + u32 newlength; + u32 newused; + u32 newvalue; + u32 indexlth; + u32 indexalloc; + + err = 0; + if (attr) { + newvalue = le32_to_cpu(attr->value_length) + rawresize; + attr->value_length = cpu_to_le32(newvalue); + newlength = le32_to_cpu(attr->length) + resize; + attr->length = cpu_to_le32(newlength); + } + if (entry) { + newused = le32_to_cpu(entry->bytes_in_use) + resize; + entry->bytes_in_use = cpu_to_le32(newused); + } + if (index) { + indexlth = le32_to_cpu(index->index.index_length) + resize; + index->index.index_length = cpu_to_le32(indexlth); + indexalloc = le32_to_cpu(index->index.allocated_size) + resize; + index->index.allocated_size = cpu_to_le32(indexalloc); + } + return (err); +} + +/* + * Adjust the next attribute instance + * + * If a newly created attribute matches the next instance, then + * the next instance has to be incremented. + * + * Do the opposite when undoing an attribute creation, but + * do not change the next instance when deleting an attribute + * or undoing the deletion. + */ + +static void adjust_instance(const ATTR_RECORD *attr, MFT_RECORD *entry, int increment) +{ + u16 instance; + + if (increment > 0) { + /* Allocating a new instance ? */ + if (attr->instance == entry->next_attr_instance) { + instance = (le16_to_cpu(entry->next_attr_instance) + + 1) & 0xffff; + entry->next_attr_instance = cpu_to_le16(instance); + } + } + if (increment < 0) { + /* Freeing the latest instance ? */ + instance = (le16_to_cpu(entry->next_attr_instance) + - 1) & 0xffff; + if (attr->instance == cpu_to_le16(instance)) + entry->next_attr_instance = attr->instance; + } +} + +/* + * Adjust the highest vcn according to mapping pairs + * + * The runlist has to be fully recomputed + */ + +static int adjust_high_vcn(ntfs_volume *vol, ATTR_RECORD *attr) +{ + runlist_element *rl; + runlist_element *xrl; + VCN high_vcn; + int err; + + err = 1; + attr->highest_vcn = const_cpu_to_sle64(0); + rl = ntfs_mapping_pairs_decompress(vol, attr, (runlist_element*)NULL); + if (rl) { + xrl = rl; + if (xrl->length) + xrl++; + while ((xrl->length) && (xrl->lcn != LCN_RL_NOT_MAPPED)) + xrl++; + high_vcn = xrl->vcn - 1; + attr->highest_vcn = cpu_to_sle64(high_vcn); + free(rl); + err = 0; + } else { + printf("** Failed to decompress the runlist\n"); + dump((char*)attr,128); + } + return (err); +} + +/* + * Check index match, to be used for undos only + * + * The action UpdateFileNameRoot updates the time stamps and/or the + * sizes, but the lsn is not updated in the index record. + * As a consequence such UpdateFileNameRoot are not always undone + * and the actual record does not fully match the undo data. + * We however accept the match if the parent directory and the name + * match. + * Alternate workaround : do not check the lsn when undoing + * UpdateFileNameRoot + */ + +static BOOL index_match_undo(const char *first, const char *second, int length) +{ + int len; + BOOL match; + + match = !memcmp(first, second, length); + if (!match) { + if (optv) { + printf("The existing index does not match :\n"); + dump(second,length); + } + len = (first[80] & 255)*2 + 2; + match = (feedle64(first, 16) == feedle64(second, 16)) + && !memcmp(first + 80, second + 80, len); + if (match && optv) + printf("However parent dir and name do match\n"); + } + return (match); +} + + +/* + * Generic idempotent change to a resident attribute + */ + +static int change_resident(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, u32 target, u32 length) +{ + LCN lcn; + ATTR_RECORD *attr; + u32 attrend; + int err; + int changed; + + err = 1; + if (action->record.undo_length != action->record.redo_length) + printf("** Error size change in change_resident\n"); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + printf("-> full MFT record :\n"); + dump(buffer,mftrecsz); + } + attrend = le16_to_cpu(action->record.record_offset) + + le32_to_cpu(attr->length); + if ((target + length) > attrend) { + printf("** Error : update overflows from attribute\n"); + } + if (!(length & 7) + && ((target + length) <= attrend) + && (attrend <= mftrecsz) + && !sanity_mft(buffer)) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +static int change_resident_expect(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, const char *expected, + u32 target, u32 length, ATTR_TYPES type) +{ + LCN lcn; + ATTR_RECORD *attr; + int err; + BOOL found; + + err = 1; + if (action->record.undo_length != action->record.redo_length) + printf("** Error size change in change_resident\n"); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + printf("-> full record :\n"); + dump((char*)attr, le32_to_cpu(attr->length)); + } + if ((attr->type == type) + && !(length & 7) + && ((target + length) <= mftrecsz)) { + found = !memcmp(buffer + target, expected, length); + err = 0; + if (found) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "updated" : "unchanged")); + } + } + return (err); +} + +/* + * Generic idempotent change to a an index value + * + */ + +static int change_index_value(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, u32 target, u32 length) +{ + LCN lcn; + u32 count; + u32 xsize; + int changed; + int err; + + err = 1; + count = le16_to_cpu(action->record.lcns_to_follow); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + xsize = vol->indx_record_size; + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((target + length) <= (count << clusterbits)) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, xsize); + } + if (optv > 1) { + printf("-> data record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +/* + * Add one or more resident attributes + */ + +static int add_resident(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, u32 target, + u32 length, u32 oldlength) +{ + LCN lcn; + MFT_RECORD *entry; + int err; + BOOL found; + int resize; + + err = 1; + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + resize = length - oldlength; + if (optv > 1) { + printf("existing data :\n"); + dump(buffer + target,length); + } + if (!(length & 7) + && !(oldlength & 7) + && ((target + length) <= mftrecsz)) { + /* This has to be an idempotent action */ + err = 0; + if (data && length) + found = !memcmp(buffer + target, + data, length); + else { + found = TRUE; + err = 1; + } + if (!found && !err) { + /* Make space to insert the entry */ + memmove(buffer + target + resize, + buffer + target, + mftrecsz - target - resize); + if (data) + memcpy(buffer + target, data, length); + else + memset(buffer + target, 0, length); + resize_attribute(entry, NULL, NULL, + resize, resize); + if (optv > 1) { + printf("new data at same location :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "unchanged" : "expanded")); + } + } + return (err); +} + +/* + * Add one or more non-resident records + */ + +static int delete_non_resident(void /*ntfs_volume *vol, + const struct ACTION_RECORD *action, + const char *data, u32 target, u32 length, u32 oldlength*/) +{ + int err; + + err = 1; + printf("** delete_non_resident() not implemented\n"); + return (err); +} + +/* + * Expand a single resident attribute + */ + +static int expand_resident(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, u32 target, + u32 length, u32 oldlength) +{ + LCN lcn; + ATTR_RECORD *attr; + MFT_RECORD *entry; + int err; + BOOL found; + int resize; + u16 base; + + err = 1; + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("existing data :\n"); + dump(buffer + target,length); + } + base = 24 + 2*attr->name_length; + resize = ((base + length - 1) | 7) + - ((base + oldlength - 1) | 7); + if ((target + length) <= mftrecsz) { + /* This has to be an idempotent action */ +// TODO This test is wrong ! + found = le32_to_cpu(attr->value_length) == length; + if (found && data && length) + found = !memcmp(buffer + target, data, length); + err = 0; + if (!found) { + /* Make space to insert the entry */ + memmove(buffer + target + resize, + buffer + target, + mftrecsz - target - resize); +// TODO what to do if length is not a multiple of 8 ? + if (data) + memcpy(buffer + target, data, length); + else + memset(buffer + target, 0, length); + resize_attribute(entry, attr, NULL, + length - oldlength, resize); + if (optv > 1) { + printf("new data at same location :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "unchanged" : "expanded")); + } + } + return (err); +} + +/* + * Add one or more non-resident records + */ + +static int add_non_resident(void /*ntfs_volume *vol, + const struct ACTION_RECORD *action, + const char *data, u32 target, u32 length, u32 oldlength*/) +{ + int err; + + printf("** add_non_resident() not implemented\n"); + err = 0; + return (err); +} + +/* + * Generic insert a new resident attribute + */ + +static int insert_resident(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, u32 target, + u32 length) +{ + LCN lcn; + ATTR_RECORD *attr; + const ATTR_RECORD *newattr; + MFT_RECORD *entry; + u32 newused; + u16 links; + int err; + BOOL found; + + err = 1; + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + newattr = (const ATTR_RECORD*)data; + if (optv > 1) { + printf("existing record :\n"); + dump(buffer + target,length); + if (le32_to_cpu(attr->type) < le32_to_cpu(newattr->type)) { + printf("** Bad attribute order, full record :\n"); + dump(buffer, mftrecsz); + } + } + /* Types must be in ascending order */ + if (valid_type(attr->type) + && (le32_to_cpu(attr->type) + >= le32_to_cpu(newattr->type)) + && !(length & 7) + && ((target + length) <= mftrecsz)) { + /* This has to be an idempotent action */ + found = !memcmp(buffer + target, data, length); + err = 0; + if (!found) { + /* Make space to insert the entry */ + memmove(buffer + target + length, + buffer + target, + mftrecsz - target - length); + memcpy(buffer + target, data, length); + newused = le32_to_cpu(entry->bytes_in_use) + + length; + entry->bytes_in_use = cpu_to_le32(newused); + if (action->record.redo_operation + == const_cpu_to_le16(CreateAttribute)) { + /* + * For a real create, may have to adjust + * the next attribute instance + */ + adjust_instance(newattr, entry, 1); + } + if (newattr->type == AT_FILE_NAME) { + links = le16_to_cpu(entry->link_count) + 1; + entry->link_count = cpu_to_le16(links); + } + if (optv > 1) { + printf("expanded record (now 0x%x" + " bytes used) :\n", + (int)newused); + dump(buffer + target, 2*length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "unchanged" : "expanded")); + } + } + return (err); +} + +/* + * Generic remove a single resident attribute + */ + +static int remove_resident(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, u32 target, + u32 length) +{ + LCN lcn; + ATTR_RECORD *attr; + MFT_RECORD *entry; + u32 newused; + u16 links; + int err; + BOOL found; + + err = 1; + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("existing record :\n"); + dump(buffer + target,length); + } + if (!(length & 7) + && ((target + length) <= mftrecsz)) { + /* This has to be an idempotent action */ + /* For AT_DATA the value is not always present */ + if (attr->type == AT_DATA) + found = !memcmp(buffer + target, data, + le16_to_cpu(attr->value_offset)); + else + found = !memcmp(buffer + target, data, length); + if (!found && optv) { + printf("data 0x%lx 0x%lx offset %d %ld\n", + (long)le32_to_cpu(attr->type), + (long)le32_to_cpu(AT_DATA), + (int)offsetof(ATTR_RECORD, resident_end), + (long)le16_to_cpu(attr->value_offset)); + printf("The existing record does not match (%d/%d)\n", + (int)matchcount(buffer + target, data, + length),(int)length); + dump(data,length); + printf("full attr :\n"); + dump((const char*)attr,mftrecsz + - le16_to_cpu(action->record.record_offset)); + } + err = 0; + if (found) { + if (attr->type == AT_FILE_NAME) { + links = le16_to_cpu(entry->link_count) - 1; + entry->link_count = cpu_to_le16(links); + } + if (action->record.redo_operation + == const_cpu_to_le16(CreateAttribute)) { + adjust_instance(attr, entry, -1); + } + /* Remove the entry */ + memmove(buffer + target, + buffer + target + length, + mftrecsz - target - length); + newused = le32_to_cpu(entry->bytes_in_use) - length; + entry->bytes_in_use = cpu_to_le32(newused); + if (optv > 1) { + printf("new record at same location" + " (now 0x%x bytes used) :\n", + (int)newused); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "shrinked" : "unchanged")); + } + } + return (err); +} + +/* + * Delete one or more resident attributes + */ + +static int delete_resident(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, u32 target, + u32 length, u32 oldlength) +{ + LCN lcn; + MFT_RECORD *entry; + int err; + BOOL found; + int resize; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + if (optv > 1) { + printf("existing data :\n"); + dump(buffer + target,length); + } + resize = length - oldlength; + if (!(length & 7) + && !(oldlength & 7) + && ((target + oldlength) <= mftrecsz)) { + /* This has to be an idempotent action */ + err = 0; + if (data && length) + found = !memcmp(buffer + target, data, length); + else { + found = FALSE; + err = 1; + } + if (!found && !err) { + /* Remove the entry, if present */ + memmove(buffer + target, + buffer + target - resize, + mftrecsz - target + resize); + resize_attribute(entry, NULL, NULL, + length - oldlength, resize); + if (optv > 1) { + printf("new data at same location :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "unchanged" : "shrinked")); + } + } + return (err); +} + +static int shrink_resident(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, u32 target, + u32 length, u32 oldlength) +{ + LCN lcn; + ATTR_RECORD *attr; + MFT_RECORD *entry; + int err; + BOOL found; + int resize; + u16 base; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("existing data :\n"); + dump(buffer + target,length); + } + base = 24 + 2*attr->name_length; + resize = ((base + length - 1) | 7) + - ((base + oldlength - 1) | 7); + if ((oldlength > length) +// TODO limit to attr length + && ((target + oldlength) <= mftrecsz)) { + /* This has to be an idempotent action */ + if (data && length) + found = !memcmp(buffer + target, data, length); + else +{ +// TODO wrong : need checking against the old data, but in known cases +// redo data is not available either and existing data is not zero. + found = FALSE; +printf("* fake test, assuming not shrinked : value length %ld length %ld oldlength %ld\n",(long)le32_to_cpu(attr->value_length),(long)length,(long)oldlength); +//dump(buffer + target, oldlength); +} + err = 0; + if (!found) { + if (length) { + /* Relocate end of record */ +// TODO restrict to bytes_in_use + memmove(buffer + target + length, + buffer + target + oldlength, + mftrecsz - target - oldlength); + /* Insert new data or zeroes */ + if (data) + memcpy(buffer + target, data, length); + else + memset(buffer + target, 0, length); + } else { + /* Remove the entry, unless targeted size */ + memmove(buffer + target, + buffer + target - resize, + mftrecsz - target + resize); + } + resize_attribute(entry, attr, NULL, + length - oldlength, resize); + if (optv > 1) { + printf("new data at same location :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "unchanged" : "shrinked")); + } + } + return (err); +} + +static int update_index(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer, const char *data, u32 target, u32 length) +{ + LCN lcn; + INDEX_BLOCK *indx; + u32 xsize; + BOOL changed; + int err; + + err = 1; + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + xsize = vol->indx_record_size; + indx = (INDEX_BLOCK*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("-> existing index :\n"); + dump(&buffer[target], length); + } + if ((indx->magic == magic_INDX) + && !(length & 7) + && ((target + length) <= xsize)) { + /* This has to be an idempotent action */ + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + /* Update the entry */ + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new index :\n"); + dump(&buffer[target], length); + } + err = write_protected(vol, &action->record, + buffer, xsize); + } + if (optv > 1) { + printf("-> INDX record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +/* + * Controversial deletion of file names, see undo_delete_file() + */ + +static int delete_names(char *buffer) +{ + MFT_RECORD *record; + ATTR_RECORD *attr; + u32 used; + u32 pos; + int length; + int cnt; + + record = (MFT_RECORD*)buffer; + pos = le16_to_cpu(record->attrs_offset); + used = le32_to_cpu(record->bytes_in_use); + cnt = 0; + do { + attr = (ATTR_RECORD*)&buffer[pos]; + length = le32_to_cpu(attr->length); + if (attr->type == AT_FILE_NAME) { + if (optv) + showname("Controversial deletion of ", + &buffer[pos+90], buffer[pos+88] & 255); + memmove(buffer + pos, buffer + pos + length, + mftrecsz - pos - length); + used -= length; + cnt++; + } else + pos += length; + } while ((pos < used) + && (le32_to_cpu(attr->type) <= le32_to_cpu(AT_FILE_NAME))); + record->bytes_in_use = cpu_to_le32(used); + record->link_count = cpu_to_le16(0); + return (cnt ? 0 : 1); +} + +static int rebuildname(const INDEX_ENTRY *index) +{ + ATTR_RECORD *attr; + int headlth; + int datalth; + + datalth = le16_to_cpu(index->length) + - offsetof(INDEX_ENTRY,key.file_name); + headlth = offsetof(ATTR_RECORD,resident_end); + attr = (ATTR_RECORD*)malloc(headlth + datalth); + if (attr) { + attr->type = AT_FILE_NAME; + attr->length = cpu_to_le32(headlth + datalth); + attr->non_resident = 0; + attr->name_length = 0; + attr->name_offset = const_cpu_to_le16(0); + attr->flags = const_cpu_to_le16(0); + attr->instance = const_cpu_to_le16(0); + attr->value_length = cpu_to_le32( + 2*index->key.file_name.file_name_length + + offsetof(FILE_NAME_ATTR, file_name)); + attr->value_offset = cpu_to_le16(headlth); + attr->resident_flags = RESIDENT_ATTR_IS_INDEXED; + memcpy(attr->resident_end, &index->key.file_name, datalth); + free(attr); + } + return (0); +} + +/* + * Controversial creation of an index allocation attribute + * + * This is useful for turning the clock backward, but cannot + * work properly in the general case and must not be used for + * a real sync. + * The main problem is to synchronize the file names when an + * inode is reused with a different name. + */ + +static int insert_index_allocation(ntfs_volume *vol, char *buffer, u32 offs) +{ + MFT_RECORD *record; + ATTR_RECORD *attr; + u32 used; + u32 pos; + u32 xsize; + u16 instance; + int length; + int addedlength; + int namelength; + int err; + static const unsigned char bitmap[] = + { 1, 0, 0, 0, 0, 0, 0, 0 } ; + + err = 1; + if (opts) { + printf("** Call to unsupported insert_index_allocation()\n"); + } else { + record = (MFT_RECORD*)buffer; + pos = le16_to_cpu(record->attrs_offset); + used = le32_to_cpu(record->bytes_in_use); + attr = (ATTR_RECORD*)&buffer[pos]; + while ((pos < used) + && (le32_to_cpu(attr->type) < le32_to_cpu(AT_INDEX_ROOT))) { + pos += le32_to_cpu(attr->length); + attr = (ATTR_RECORD*)&buffer[pos]; + } + length = le32_to_cpu(attr->length); + addedlength = length - 8 /* index allocation */ + + length - 48; /* bitmap */ + if ((attr->type == AT_INDEX_ROOT) + && ((pos + length) == offs) + && ((used + addedlength) < mftrecsz)) { + /* Make space for the attribute */ + memmove(buffer + offs + addedlength, buffer + offs, + mftrecsz - offs - addedlength); + record->bytes_in_use = cpu_to_le32(used + addedlength); + /* + * Insert an AT_INDEX_ALLOCATION + */ + attr = (ATTR_RECORD*)&buffer[offs]; + attr->type = AT_INDEX_ALLOCATION; + attr->length = cpu_to_le32(length - 8); + attr->non_resident = 1; + namelength = buffer[pos + 9] & 255; + attr->name_length = namelength; + attr->name_offset = const_cpu_to_le16(0x40); + memcpy(buffer + offs + 0x40, buffer + pos + 0x18, + 2*namelength); + attr->flags = const_cpu_to_le16(0); + /* Should we really take a new instance ? */ + attr->instance = record->next_attr_instance; + instance = le16_to_cpu(record->next_attr_instance) + 1; + record->next_attr_instance = cpu_to_le16(instance); + attr->lowest_vcn = const_cpu_to_sle64(0); + attr->highest_vcn = const_cpu_to_sle64(0); + attr->mapping_pairs_offset = cpu_to_le16( + 2*namelength + 0x40); + attr->compression_unit = 0; + xsize = vol->indx_record_size; + attr->allocated_size = cpu_to_sle64(xsize); + attr->data_size = attr->allocated_size; + attr->initialized_size = attr->allocated_size; + /* + * Insert an AT_INDEX_BITMAP + */ + attr = (ATTR_RECORD*)&buffer[offs + length - 8]; + attr->type = AT_BITMAP; + attr->length = cpu_to_le32(length - 48); + attr->non_resident = 0; + namelength = buffer[pos + 9] & 255; + attr->name_length = namelength; + attr->name_offset = const_cpu_to_le16(0x18); + memcpy(buffer + offs + length - 8 + 0x18, + buffer + pos + 0x18, 2*namelength); + attr->flags = const_cpu_to_le16(0); + attr->value_length = const_cpu_to_le32(8); + attr->value_offset = cpu_to_le16(2*namelength + 24); + attr->resident_flags = 0; + memcpy((char*)attr->resident_end + 2*namelength, + bitmap, 8); + /* Should we really take a new instance ? */ + attr->instance = record->next_attr_instance; + instance = le16_to_cpu(record->next_attr_instance) + 1; + record->next_attr_instance = cpu_to_le16(instance); + err = sanity_mft(buffer); + } else { + printf("** index root does not match\n"); + err = 1; + } + } + return (err); +} + +/* + * Check whether a full MFT record is fed by an action + * + * If so, checking the validity of existing record is pointless + */ + +static BOOL check_full_mft(const struct ACTION_RECORD *action, BOOL redoing) +{ + const MFT_RECORD *record; + const ATTR_RECORD *attr; + u32 length; + u32 k; + BOOL ok; + + if (redoing) { + record = (const MFT_RECORD*)((const char*)&action->record + + get_redo_offset(&action->record)); + length = le16_to_cpu(action->record.redo_length); + } else { + record = (const MFT_RECORD*)((const char*)&action->record + + get_undo_offset(&action->record)); + length = le16_to_cpu(action->record.undo_length); + } + /* The length in use must be fed */ + ok = !action->record.record_offset + && !action->record.attribute_offset + && (record->magic == magic_FILE) + && (length <= mftrecsz) + && (length >= (offsetof(MFT_RECORD, bytes_in_use) + + sizeof(record->bytes_in_use))); + if (ok) { + k = le16_to_cpu(record->attrs_offset); + attr = (const ATTR_RECORD*)((const char*)record + k); + while (((k + sizeof(attr->type)) <= length) + && (attr->type != AT_END) + && valid_type(attr->type)) { + k += le32_to_cpu(attr->length); + attr = (const ATTR_RECORD*)((const char*)record + k); + } + /* AT_END must be present */ + ok = ((k + sizeof(attr->type)) <= length) + && (attr->type == AT_END); + } + return (ok); +} + +/* + * Check whether a full index block is fed by the log record + * + * If so, checking the validity of existing record is pointless + */ + +static BOOL check_full_index(const struct ACTION_RECORD *action, BOOL redoing) +{ + const INDEX_BLOCK *indx; + u32 length; + + if (redoing) { + indx = (const INDEX_BLOCK*)((const char*)&action->record + + get_redo_offset(&action->record)); + length = le16_to_cpu(action->record.redo_length); + } else { + indx = (const INDEX_BLOCK*)((const char*)&action->record + + get_undo_offset(&action->record)); + length = le16_to_cpu(action->record.undo_length); + } + /* the index length must be fed, so must be the full index block */ + return (!action->record.record_offset + && !action->record.attribute_offset + && (indx->magic == magic_INDX) + && (length >= (offsetof(INDEX_BLOCK, index.index_length) + 4)) + && (length >= (le32_to_cpu(indx->index.index_length) + 24))); +} + +/* + * Create an index block for undoing its deletion + * + * This is useful for turning the clock backward, but cannot + * work properly in the general case and must not be used for + * a real sync. + */ + +static int create_indx(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer) +{ + INDEX_BLOCK *indx; + INDEX_ENTRY_HEADER *ixhead; + INDEX_ENTRY *ixentry; + VCN vcn; + int err; + + if (opts) { + printf("** Call to unsupported create_indx()\n"); + err = 1; + } else { + err = 0; + indx = (INDEX_BLOCK*)buffer; + indx->magic = magic_INDX; +// TODO compute properly + indx->usa_ofs = const_cpu_to_le16(0x28); + indx->usa_count = const_cpu_to_le16(9); + indx->lsn = action->record.this_lsn; + vcn = sle64_to_cpu(action->record.target_vcn); + /* beware of size change on big-endian cpus */ + indx->index_block_vcn = cpu_to_sle64(vcn); + /* INDEX_HEADER */ + indx->index.entries_offset = const_cpu_to_le32(0x28); + indx->index.index_length = const_cpu_to_le32(0x38); + indx->index.allocated_size = + cpu_to_le32(vol->indx_record_size - 24); + indx->index.ih_flags = 0; + /* INDEX_ENTRY_HEADER */ + ixhead = (INDEX_ENTRY_HEADER*)(buffer + 0x28); + ixhead->length = cpu_to_le16(vol->indx_record_size - 24); + /* terminating INDEX_ENTRY */ + ixentry = (INDEX_ENTRY*)(buffer + 0x40); + ixentry->indexed_file = const_cpu_to_le64(0); + ixentry->length = const_cpu_to_le16(16); + ixentry->key_length = const_cpu_to_le16(0); + ixentry->ie_flags = INDEX_ENTRY_END; + } + return (err); +} + +static int redo_action37(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer) +{ + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + printf("existing data :\n"); + dump(buffer + target,length); + } + if ((target + length) == mftrecsz) { + memset(buffer + target, 0, length); + err = write_protected(vol, &action->record, + buffer, mftrecsz); + if (optv > 1) { + printf("-> MFT record trimmed\n"); + } + } else { + printf("** Bad action-37, inode %lld record :\n", + (long long)inode_number(&action->record)); + printf("target %d length %d sum %d\n", + (int)target,(int)length,(int)(target + length)); + dump(buffer,mftrecsz); + } + err = 0; + return (err); +} + +static int redo_add_index(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer) +{ + LCN lcn; + const char *data; + INDEX_BLOCK *indx; + u32 target; + u32 length; + u32 xsize; + u32 indexlth; + int err; + BOOL found; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + xsize = vol->indx_record_size; + indx = (INDEX_BLOCK*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((indx->magic == magic_INDX) + && !(length & 7) + && ((target + length) <= xsize)) { + /* This has to be an idempotent action */ + found = !memcmp(buffer + target, data, length); + err = 0; + if (!found) { + /* Make space to insert the entry */ + memmove(buffer + target + length, + buffer + target, + xsize - target - length); + memcpy(buffer + target, data, length); + indexlth = le32_to_cpu(indx->index.index_length) + + length; + indx->index.index_length = cpu_to_le32(indexlth); + if (optv > 1) { + printf("-> inserted record :\n"); + dump(&buffer[target], length); + } + err = write_protected(vol, &action->record, + buffer, xsize); + } + if (optv > 1) { + printf("-> INDX record %s\n", + (found ? "unchanged" : "inserted")); + } + } + return (err); +} + +static int redo_add_root_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + ATTR_RECORD *attr; + MFT_RECORD *entry; + INDEX_ROOT *index; + u32 target; + u32 length; + int err; + BOOL found; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + index = (INDEX_ROOT*)(((char*)attr) + + le16_to_cpu(attr->value_offset)); + if (optv > 1) { + printf("existing index :\n"); + dump(buffer + target,length); + } + if ((attr->type == AT_INDEX_ROOT) + && !(length & 7) + && ((target + length) <= mftrecsz)) { + /* This has to be an idempotent action */ + found = !memcmp(buffer + target, data, length); + err = 0; + if (!found) { + /* Make space to insert the entry */ + memmove(buffer + target + length, + buffer + target, + mftrecsz - target - length); + memcpy(buffer + target, data, length); + resize_attribute(entry, attr, index, length, length); + if (optv > 1) { + printf("new index at same location :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "unchanged" : "expanded")); + } + } + return (err); +} + +static int redo_compensate(ntfs_volume *vol __attribute__((unused)), + const struct ACTION_RECORD *action, + char *buffer __attribute__((unused))) +{ + u64 lsn; + s64 diff; + + if (optv > 1) + printf("-> %s()\n",__func__); + lsn = sle64_to_cpu(action->record.this_lsn); + diff = lsn - restart_lsn; + if (diff > 0) + restart_lsn = lsn; + return (0); +} + +static int redo_create_file(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + MFT_RECORD *record; + u32 target; + u32 length; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + record = (MFT_RECORD*)buffer; + if (optv > 1) { + printf("-> existing record :\n"); + dump(buffer,mftrecsz); + } + if ((target + length) <= mftrecsz) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed || !(record->flags & MFT_RECORD_IN_USE)) { + memcpy(buffer + target, data, length); + record->flags |= MFT_RECORD_IN_USE; + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer,mftrecsz); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } else { + err = 1; /* record overflows */ + } + return (err); +} + +static int redo_create_attribute(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); +// Could also be AT_DATA or AT_INDEX_ALLOCATION + if (!action->record.undo_length) + err = insert_resident(vol, action, buffer, data, + target, length); + return (err); +} + +static int redo_delete_attribute(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (!action->record.redo_length) + err = remove_resident(vol, action, buffer, data, + target, length); + return (err); +} + +static int redo_delete_file(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + MFT_RECORD *record; + u32 target; + u32 length; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(buffer,mftrecsz); + } + record = (MFT_RECORD*)buffer; + if ((target + length) <= mftrecsz) { + /* write a void mft entry (needed ?) */ + changed = (length && memcmp(buffer + target, data, length)) + || (record->flags & MFT_RECORD_IN_USE); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + record->flags &= ~MFT_RECORD_IN_USE; + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer,mftrecsz); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +static int redo_delete_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + INDEX_BLOCK *indx; + u32 target; + u32 length; + u32 xsize; + u32 indexlth; + int err; + BOOL found; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + xsize = vol->indx_record_size; + indx = (INDEX_BLOCK*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((indx->magic == magic_INDX) + && !(length & 7) + && ((target + length) <= xsize)) { + /* This has to be an idempotent action */ + found = (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord)) + || !memcmp(buffer + target, data, length); + err = 0; + if (found) { + /* Remove the entry */ + memmove(buffer + target, + buffer + target + length, + xsize - target - length); + indexlth = le32_to_cpu(indx->index.index_length) + - length; + indx->index.index_length = cpu_to_le32(indexlth); + err = write_protected(vol, &action->record, + buffer, xsize); + } + if (optv > 1) { + printf("-> INDX record %s\n", + (found ? "removed" : "unchanged")); + } + } + return (err); +} + +static int redo_delete_root_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + ATTR_RECORD *attr; + MFT_RECORD *entry; + INDEX_ROOT *index; + BOOL found; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + index = (INDEX_ROOT*)(((char*)attr) + + le16_to_cpu(attr->value_offset)); + if (optv > 1) { + printf("existing index :\n"); + dump(buffer + target,length); + } + if ((attr->type == AT_INDEX_ROOT) + && !(length & 7) + && ((target + length) <= mftrecsz)) { + /* This has to be an idempotent action */ + found = (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord)) + || !memcmp(buffer + target, data, length); + err = 0; + /* Only delete if present */ + if (found) { + /* Remove the entry */ + memmove(buffer + target, + buffer + target + length, + mftrecsz - target - length); + resize_attribute(entry, attr, index, -length, -length); + if (optv > 1) { + printf("new index at same location :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "shrinked" : "updated")); + } + } + return (err); +} + +static int redo_force_bits(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const struct BITMAP_ACTION *data; + u32 i; + int err; + int wanted; + u32 firstbit; + u32 count; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = (const struct BITMAP_ACTION*) + (((const char*)&action->record) + + get_redo_offset(&action->record)); + firstbit = le32_to_cpu(data->firstbit); + count = le32_to_cpu(data->count); + if (action->record.redo_operation + == const_cpu_to_le16(SetBitsInNonResidentBitMap)) + wanted = 1; + else + wanted = 0; +// TODO consistency undo_offset == redo_offset, etc. +// firstbit + count < 8*clustersz (multiple clusters possible ?) + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx firstbit %d count %d wanted %d\n", + (long long)lcn,(int)firstbit,(int)count,(int)wanted); + } + for (i=0; irecord, buffer)) { + err = 0; + if (optv > 1) + printf("-> record updated\n"); + } + if (err) + printf("** redo_clearbits failed\n"); + return (err); +} + +static int redo_open_attribute(ntfs_volume *vol __attribute__((unused)), + const struct ACTION_RECORD *action) +{ + const char *data; + struct ATTR *pa; + const ATTR_OLD *attr_old; + const ATTR_NEW *attr_new; + const char *name; + le64 inode; + u32 namelen; + u32 length; + u32 extra; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + extra = get_extra_offset(&action->record); + if (action->record.undo_length) { + name = ((const char*)&action->record) + extra; + namelen = le32_to_cpu(action->record.client_data_length) + + LOG_RECORD_HEAD_SZ - extra; + /* fix namelen which was aligned modulo 8 */ + namelen = fixnamelen(name, namelen); + if (optv > 1) { + printf("-> length %d namelen %d",(int)length, + (int)namelen); + showname(", ", name, namelen/2); + } + } else { + name = ""; + namelen = 0; + } + pa = getattrentry(le16_to_cpu(action->record.target_attribute),0); + if (pa) { + if (optv) { + /* + * If the actions have been displayed, the + * attribute has already been fed. Check + * whether it matches what we have in store. + */ + switch (length) { + case sizeof(ATTR_OLD) : + attr_old = (const ATTR_OLD*)data; + /* Badly aligned */ + memcpy(&inode, &attr_old->inode, 8); + err = (MREF(le64_to_cpu(inode)) != pa->inode) + || (attr_old->type != pa->type); + break; + case sizeof(ATTR_NEW) : + attr_new = (const ATTR_NEW*)data; + err = (MREF(le64_to_cpu(attr_new->inode)) + != pa->inode) + || (attr_new->type != pa->type); + break; + default : err = 1; + } + if (!err) { + err = (namelen != pa->namelen) + || (namelen + && memcmp(name, pa->name, namelen)); + } + if (optv > 1) + printf("-> attribute %s the recorded one\n", + (err ? "does not match" : "matches")); + } else { + copy_attribute(pa, data, length); + pa->namelen = namelen; + if (namelen) + memcpy(pa->name, data, namelen); + err = 0; + } + } else + if (optv) + printf("* Unrecorded attribute\n"); + return (err); +} + +static int redo_sizes(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer) +{ + const char *data; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset) + + offsetof(ATTR_RECORD, allocated_size); + err = change_resident(vol, action, buffer, + data, target, length); + return (err); +} + +static int redo_update_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + /* target is left-justified to creation time */ + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset) + + offsetof(INDEX_ENTRY, key.file_name.creation_time); + err = update_index(vol, action, buffer, data, target, length); + return (err); +} + +static int redo_update_index_value(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + u32 length; + u32 target; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + err = change_index_value(vol, action, buffer, data, target, length); + return (err); +} + +static int redo_update_mapping(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + ATTR_RECORD *attr; + MFT_RECORD *entry; + u32 target; + u32 length; + u32 source; + u32 alen; + u32 newused; + int resize; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + resize = length - le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (!attr->non_resident) { + printf("** Error : update_mapping on resident attr\n"); + } + if (valid_type(attr->type) + && attr->non_resident + && !(resize & 7) + && ((target + length) <= mftrecsz)) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + /* Adjust space for new mapping pairs */ + source = target - resize; + if (resize > 0) { + memmove(buffer + target + length, + buffer + source + length, + mftrecsz - target - length); + } + if (resize < 0) { + memmove(buffer + target + length, + buffer + source + length, + mftrecsz - source - length); + } + memcpy(buffer + target, data, length); + /* Resize the attribute */ + alen = le32_to_cpu(attr->length) + resize; + attr->length = cpu_to_le32(alen); + /* Resize the mft record */ + newused = le32_to_cpu(entry->bytes_in_use) + + resize; + entry->bytes_in_use = cpu_to_le32(newused); + /* Compute the new highest_vcn */ + err = adjust_high_vcn(vol, attr); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + if (!err) { + err = write_protected(vol, + &action->record, + buffer, mftrecsz); + } + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +static int redo_update_resident(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + u32 target; + u32 length; + u32 oldlength; + u32 end; + u32 redo; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + end = le32_to_cpu(action->record.client_data_length) + + LOG_RECORD_HEAD_SZ; + length = le16_to_cpu(action->record.redo_length); + redo = get_redo_offset(&action->record); + if ((redo + length) > end) + data = (char*)NULL; + else + data = ((const char*)&action->record) + redo; + oldlength = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (length == oldlength) { + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x" + " length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((target + length) <= mftrecsz) { + changed = (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord)) + || memcmp(buffer + target, data, length); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + } else { + if (length > oldlength) + err = expand_resident(vol, action, buffer, data, + target, length, oldlength); + else + err = shrink_resident(vol, action, buffer, data, + target, length, oldlength); + } + return (err); +} + +static int redo_update_root_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + const char *expected; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + expected = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + /* the fixup is right-justified to the name length */ + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset) + + offsetof(INDEX_ENTRY, key.file_name.file_name_length) + - length; + if (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord)) + err = change_resident(vol, action, buffer, data, + target, length); + else + err = change_resident_expect(vol, action, buffer, data, + expected, target, length, AT_INDEX_ROOT); + return (err); +} + +static int redo_update_root_vcn(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + const char *expected; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + expected = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + if (length == 8) { + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + /* target is right-justified to end of attribute */ + target += getle16(buffer, target + 8) - length; + err = change_resident_expect(vol, action, buffer, data, + expected, target, length, AT_INDEX_ROOT); + } + return (err); +} + +static int redo_update_value(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + u32 length; + u32 target; + u32 count; + u32 redo; + u32 end; + u32 i; + int changed; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + length = le16_to_cpu(action->record.redo_length); + redo = get_redo_offset(&action->record); + end = le32_to_cpu(action->record.client_data_length) + + LOG_RECORD_HEAD_SZ; + /* sometimes there is no redo data */ + if ((redo + length) > end) + data = (char*)NULL; + else + data = ((const char*)&action->record) + redo; + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + count = le16_to_cpu(action->record.lcns_to_follow); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((target + length) <= (count << clusterbits)) { + if (data) + changed = memcmp(buffer + target, data, length); + else { + for (i=0; (i 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_raw(vol, &action->record, buffer); + } + if (optv > 1) { + printf("-> data record %s\n", + (changed ? "updated" : "unchanged")); + } + } + + return (err); +} + +static int redo_update_vcn(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + if (length == 8) { + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + /* target is right-justified to end of attribute */ + target += getle16(buffer, target + 8) - length; + err = update_index(vol, action, buffer, data, target, length); + } + return (err); +} + +static int redo_write_end(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + u32 target; + u32 length; + u32 oldlength; + u32 end; + u32 redo; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + end = le32_to_cpu(action->record.client_data_length) + + LOG_RECORD_HEAD_SZ; + length = le16_to_cpu(action->record.redo_length); + redo = get_redo_offset(&action->record); + if ((redo + length) > end) + data = (char*)NULL; + else + data = ((const char*)&action->record) + redo; + oldlength = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (length == oldlength) { + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x" + " length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((target + length) <= mftrecsz) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + } else { + if (length > oldlength) + err = add_resident(vol, action, buffer, data, + target, length, oldlength); + else + err = delete_resident(vol, action, buffer, data, + target, length, oldlength); + } + return (err); +} + +static int redo_write_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + INDEX_BLOCK *indx; + u32 target; + u32 length; + u32 xsize; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + /* target is left-justified to creation time */ + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + xsize = vol->indx_record_size; + indx = (INDEX_BLOCK*)buffer; + if (action->record.record_offset) { + printf("** Non-null record_offset in redo_write_index()\n"); + } + if (optv > 1) { + printf("-> existing index :\n"); + dump(&buffer[target], length); + } + if ((indx->magic == magic_INDX) + && !(length & 7) + && ((target + length) <= xsize)) { + /* This has to be an idempotent action */ + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + /* Update the entry */ + memcpy(buffer + target, data, length); + /* If truncating, set the new size */ + indx->index.index_length = + cpu_to_le32(target + length - 0x18); + if (optv > 1) { + printf("-> new index :\n"); + dump(&buffer[target], length); + } + err = write_protected(vol, &action->record, + buffer, xsize); + } + if (optv > 1) { + printf("-> INDX record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +static int undo_action37(ntfs_volume *vol __attribute__((unused)), + const struct ACTION_RECORD *action, + char *buffer __attribute__((unused))) +{ +/* + const char *data; + u32 target; + u32 length; +*/ + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; +/* + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); +*/ + printf("* Ignored action-37, inode %lld record :\n", + (long long)inode_number(&action->record)); + err = 0; + return (err); +} + +static int undo_add_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + INDEX_BLOCK *indx; + u32 target; + u32 length; + u32 xsize; + u32 indexlth; + int err; + BOOL found; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + xsize = vol->indx_record_size; + indx = (INDEX_BLOCK*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((indx->magic == magic_INDX) + && !(length & 7) + && ((target + length) <= xsize)) { + /* This has to be an idempotent action */ + found = index_match_undo(buffer + target, data, length); + err = 0; + if (found) { + /* Remove the entry */ + memmove(buffer + target, + buffer + target + length, + xsize - target - length); + indexlth = le32_to_cpu(indx->index.index_length) + - length; + indx->index.index_length = cpu_to_le32(indexlth); + err = write_protected(vol, &action->record, + buffer, xsize); + } else { + sanity_indx(vol,buffer); + printf("full record :\n"); + dump(buffer,xsize); + } + if (optv > 1) { + printf("-> INDX record %s\n", + (found ? "removed" : "unchanged")); + } + } + return (err); +} + +static int undo_add_root_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + ATTR_RECORD *attr; + MFT_RECORD *entry; + INDEX_ROOT *index; + BOOL found; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + index = (INDEX_ROOT*)(((char*)attr) + + le16_to_cpu(attr->value_offset)); + if (optv > 1) { + printf("existing index :\n"); + dump(buffer + target,length); + } + if ((attr->type == AT_INDEX_ROOT) + && !(length & 7) + && ((target + length) <= mftrecsz)) { + /* This has to be an idempotent action */ + found = index_match_undo(buffer + target, data, length); + err = 0; + if (found && !older_record(entry, &action->record)) { + /* Remove the entry */ + memmove(buffer + target, + buffer + target + length, + mftrecsz - target - length); + resize_attribute(entry, attr, index, -length, -length); + if (optv > 1) { + printf("new index at same location :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "shrinked" : "unchanged")); + } + } + return (err); +} + +static int undo_create_attribute(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (!action->record.undo_length) + err = remove_resident(vol, action, buffer, data, + target, length); + return (err); +} + +static int undo_delete_attribute(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (!action->record.redo_length) + err = insert_resident(vol, action, buffer, data, + target, length); + return (err); +} + +static int undo_delete_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + INDEX_BLOCK *indx; + u32 target; + u32 length; + u32 xsize; + u32 indexlth; + int err; + BOOL found; + +// MERGE with redo_add_root_index() ? + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + xsize = vol->indx_record_size; + indx = (INDEX_BLOCK*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((indx->magic == magic_INDX) + && !(length & 7) + && ((target + length) <= xsize) + && !sanity_indx(vol,buffer)) { + /* This has to be an idempotent action */ + found = !memcmp(buffer + target, data, length); + err = 0; + if (!found) { + /* Make space to insert the entry */ + memmove(buffer + target + length, + buffer + target, + xsize - target - length); + memcpy(buffer + target, data, length); + indexlth = le32_to_cpu(indx->index.index_length) + + length; + indx->index.index_length = cpu_to_le32(indexlth); + if (optv > 1) { + printf("-> inserted record :\n"); + dump(&buffer[target], length); + } + /* rebuildname() has no effect currently, should drop */ + rebuildname((const INDEX_ENTRY*)data); + err = write_protected(vol, &action->record, + buffer, xsize); + } + if (optv > 1) { + printf("-> INDX record %s\n", + (found ? "unchanged" : "inserted")); + } + } + return (err); +} + +static int undo_delete_root_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + ATTR_RECORD *attr; + MFT_RECORD *entry; + INDEX_ROOT *index; + u32 target; + u32 length; + int err; + BOOL found; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + index = (INDEX_ROOT*)(((char*)attr) + + le16_to_cpu(attr->value_offset)); + if (attr->type != AT_INDEX_ROOT) { + printf("** Unexpected attr type 0x%lx\n", + (long)le32_to_cpu(attr->type)); + printf("existing mft\n"); + dump((char*)buffer,512); + printf("existing index\n"); + dump(buffer + target,length); + } + if (optv > 1) { + printf("existing index :\n"); + dump(buffer + target,length); + } + if ((attr->type == AT_INDEX_ROOT) + && !(length & 7) + && ((target + length) <= mftrecsz)) { + /* This has to be an idempotent action */ + found = !memcmp(buffer + target, data, length); + err = 0; + /* Do not insert if present */ + if (!found) { + /* Make space to insert the entry */ + memmove(buffer + target + length, + buffer + target, + mftrecsz - target - length); + memcpy(buffer + target, data, length); + resize_attribute(entry, attr, index, length, length); + if (optv > 1) { + printf("new index :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (found ? "unchanged" : "expanded")); + } + } + return (err); +} + +static int undo_create_file(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + MFT_RECORD *record; + u32 target; + u32 length; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + /* redo initialize, clearing the in_use flag ? */ + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + record = (MFT_RECORD*)buffer; + if (optv > 1) { + printf("-> existing record :\n"); + dump(buffer,mftrecsz); + } + if ((target + length) <= mftrecsz) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed || (record->flags & MFT_RECORD_IN_USE)) { + memcpy(buffer + target, data, length); + record->flags &= ~MFT_RECORD_IN_USE; + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer,mftrecsz); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +static int undo_delete_file(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + MFT_RECORD *record; + u32 target; + u32 length; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(buffer,mftrecsz); + } + record = (MFT_RECORD*)buffer; + if ((target + length) <= mftrecsz) { + changed = memcmp(buffer + target, data, length) + || !(record->flags & MFT_RECORD_IN_USE); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + /* + * Unclear what we should do for recreating a file. + * Only 24 bytes are available, the used length is not known, + * the number of links suggests we should keep the current + * names... If so, when will they be deleted ? + * We will have to make stamp changes in the standard + * information attribute, so better not to delete it. + * Should we create a data or index attribute ? + * Here, we assume we should delete the file names when + * the record now appears to not be in use and there are + * links. + */ + if (record->link_count + && !(record->flags & MFT_RECORD_IN_USE)) + err = delete_names(buffer); + record->flags |= MFT_RECORD_IN_USE; + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer,mftrecsz); + } + if (!err) + err = write_protected(vol, + &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +static int undo_force_bits(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const struct BITMAP_ACTION *data; + u32 i; + int err; + int wanted; + u32 firstbit; + u32 count; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = (const struct BITMAP_ACTION*) + (((const char*)&action->record) + + get_redo_offset(&action->record)); + firstbit = le32_to_cpu(data->firstbit); + count = le32_to_cpu(data->count); + if (action->record.redo_operation + == const_cpu_to_le16(SetBitsInNonResidentBitMap)) + wanted = 0; + else + wanted = 1; +// TODO consistency undo_offset == redo_offset, etc. +// firstbit + count < 8*clustersz (multiple clusters possible ?) + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx firstbit %d count %d wanted %d\n", + (long long)lcn,(int)firstbit,(int)count,(int)wanted); + } + for (i=0; irecord, buffer)) { + err = 0; + if (optv > 1) + printf("-> record updated\n"); + } + if (err) + printf("** redo_clearbits failed\n"); + return (err); +} + +static int undo_open_attribute(ntfs_volume *vol __attribute__((unused)), + const struct ACTION_RECORD *action) +{ + const char *data; + struct ATTR *pa; + const ATTR_OLD *attr_old; + const ATTR_NEW *attr_new; + const char *name; + le64 inode; + u32 namelen; + u32 length; + u32 extra; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.redo_length); + extra = get_extra_offset(&action->record); + if (action->record.undo_length) { + name = ((const char*)&action->record) + extra; + namelen = le32_to_cpu(action->record.client_data_length) + + LOG_RECORD_HEAD_SZ - extra; + /* fix namelen which was aligned modulo 8 */ + namelen = fixnamelen(name, namelen); + if (optv > 1) { + printf("-> length %d namelen %d",(int)length, + (int)namelen); + showname(", ", name, namelen/2); + } + } else { + namelen = 0; + name = ""; + } + pa = getattrentry(le16_to_cpu(action->record.target_attribute),0); +// TODO Only process is attr is not older ? + if (pa) { + /* check whether the redo attr matches what we have in store */ + switch (length) { + case sizeof(ATTR_OLD) : + attr_old = (const ATTR_OLD*)data; + /* Badly aligned */ + memcpy(&inode, &attr_old->inode, 8); + err = (MREF(le64_to_cpu(inode)) != pa->inode) + || (attr_old->type != pa->type); + break; + case sizeof(ATTR_NEW) : + attr_new = (const ATTR_NEW*)data; + err = (MREF(le64_to_cpu(attr_new->inode))!= pa->inode) + || (attr_new->type != pa->type); + break; + default : err = 1; + } + if (!err) { + err = (namelen != pa->namelen) + || (namelen + && memcmp(name, pa->name, namelen)); + } + if (optv > 1) + printf("-> attribute %s the recorded one\n", + (err ? "does not match" : "matches")); + } else + if (optv) + printf("* Unrecorded attribute\n"); +err = 0; + return (err); +} + +static int undo_sizes(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + MFT_RECORD *entry; + ATTR_RECORD *attr; + u32 target; + u32 length; + u32 offs; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset) + + offsetof(ATTR_RECORD, allocated_size); + entry = (MFT_RECORD*)buffer; + if (!(entry->flags & MFT_RECORD_IS_DIRECTORY)) + err = change_resident(vol, action, buffer, + data, target, length); + else { + /* On a directory, may have to build an index allocation */ + offs = le16_to_cpu(action->record.record_offset); + attr = (ATTR_RECORD*)(buffer + offs); + if (attr->type != AT_INDEX_ALLOCATION) { + err = insert_index_allocation(vol, buffer, offs); + if (!err) + err = change_resident(vol, action, buffer, + data, target, length); + } else + err = change_resident(vol, action, buffer, + data, target, length); + } + return (err); +} + +static int undo_update_index(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer) +{ + const char *data; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + /* target is left-justified to creation time */ + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset) + + offsetof(INDEX_ENTRY, key.file_name.creation_time); + err = update_index(vol, action, buffer, data, target, length); + return (err); +} + +static int undo_update_index_value(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + u32 length; + u32 target; + int changed; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((target + length) <= vol->indx_record_size) { + changed = length && memcmp(buffer + target, data, length); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, buffer, + vol->indx_record_size); + } + if (optv > 1) { + printf("-> data record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +static int undo_update_vcn(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer) +{ + const char *data; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + if (length == 8) { + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + /* target is right-justified to end of attribute */ + target += getle16(buffer, target + 8) - length; + err = update_index(vol, action, buffer, data, target, length); + } + return (err); +} + +static int undo_update_mapping(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer) +{ + LCN lcn; + const char *data; + ATTR_RECORD *attr; + MFT_RECORD *entry; + u32 target; + u32 length; + u32 source; + u32 alen; + u32 newused; + int err; + int changed; + int resize; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + resize = length - le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x new length %d resize %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length, (int)resize); + } +// TODO share with redo_update_mapping() + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + entry = (MFT_RECORD*)buffer; + attr = (ATTR_RECORD*)(buffer + + le16_to_cpu(action->record.record_offset)); + if (!attr->non_resident) { + printf("** Error : update_mapping on resident attr\n"); + } + if (valid_type(attr->type) + && attr->non_resident + && !(resize & 7) + && ((target + length) <= mftrecsz)) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + /* Adjust space for new mapping pairs */ + source = target - resize; + if (resize > 0) { + memmove(buffer + target + length, + buffer + source + length, + mftrecsz - target - length); + } + if (resize < 0) { + memmove(buffer + target + length, + buffer + source + length, + mftrecsz - source - length); + } + memcpy(buffer + target, data, length); + /* Resize the attribute */ + alen = le32_to_cpu(attr->length) + resize; + attr->length = cpu_to_le32(alen); + /* Resize the mft record */ + newused = le32_to_cpu(entry->bytes_in_use) + + resize; + entry->bytes_in_use = cpu_to_le32(newused); + /* Compute the new highest_vcn */ + err = adjust_high_vcn(vol, attr); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + if (!err) { + err = write_protected(vol, + &action->record, buffer, + mftrecsz); + } + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + return (err); +} + +static int undo_update_resident(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + u32 target; + u32 length; + u32 oldlength; + u32 end; + u32 undo; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + end = le32_to_cpu(action->record.client_data_length) + + LOG_RECORD_HEAD_SZ; + length = le16_to_cpu(action->record.undo_length); + undo = get_undo_offset(&action->record); + if ((undo + length) > end) + data = (char*)NULL; + else + data = ((const char*)&action->record) + undo; + oldlength = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (length == oldlength) { + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((target + length) <= mftrecsz) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + } else { + if (length > oldlength) + err = expand_resident(vol, action, buffer, data, + target, length, oldlength); + else + err = shrink_resident(vol, action, buffer, data, + target, length, oldlength); + } + return (err); +} + +static int undo_update_root_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + const char *expected; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + expected = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + /* the fixup is right-justified to the name length */ + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset) + + offsetof(INDEX_ENTRY, key.file_name.file_name_length) + - length; + err = change_resident_expect(vol, action, buffer, data, expected, + target, length, AT_INDEX_ROOT); + return (err); +} + +static int undo_update_root_vcn(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + const char *data; + const char *expected; + u32 target; + u32 length; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + expected = ((const char*)&action->record) + + get_redo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + if (length == 8) { + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + /* target is right-justified to end of attribute */ + target += getle16(buffer, target + 8) - length; + err = change_resident_expect(vol, action, buffer, data, + expected, target, length, AT_INDEX_ROOT); + } + return (err); +} + +static int undo_update_value(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + u32 length; + u32 target; + u32 count; + int changed; + int err; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + data = ((const char*)&action->record) + + get_undo_offset(&action->record); + length = le16_to_cpu(action->record.undo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + count = le16_to_cpu(action->record.lcns_to_follow); + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> lcn 0x%llx target 0x%x length %d\n", + (long long)lcn, (int)target, (int)length); + } + if (length) { + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((target + length) <= (count << clusterbits)) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_raw(vol, &action->record, buffer); + } + if (optv > 1) { + printf("-> data record %s\n", + (changed ? "updated" : "unchanged")); + } + } + } else { + /* + * No undo data, we cannot undo, sometimes the redo + * data even overflows from record. + * Just ignore for now. + */ + if (optv) + printf("Cannot undo, there is no undo data\n"); + err = 0; + } + + return (err); +} + +static int undo_write_end(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + u32 target; + u32 length; + u32 oldlength; + u32 end; + u32 undo; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + end = le32_to_cpu(action->record.client_data_length) + + LOG_RECORD_HEAD_SZ; + length = le16_to_cpu(action->record.undo_length); + undo = get_undo_offset(&action->record); + if ((undo + length) > end) + data = (char*)NULL; + else + data = ((const char*)&action->record) + undo; + oldlength = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (length == oldlength) { + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x" + " length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((target + length) <= mftrecsz) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + } else { + if (length > oldlength) + err = add_resident(vol, action, buffer, data, + target, length, oldlength); + else + err = delete_resident(vol, action, buffer, data, + target, length, oldlength); + } + return (err); +} + +static int undo_write_index(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + LCN lcn; + const char *data; + u32 target; + u32 length; + u32 oldlength; + u32 end; + u32 undo; + int err; + int changed; + + if (optv > 1) + printf("-> %s()\n",__func__); + err = 1; + end = le32_to_cpu(action->record.client_data_length) + + LOG_RECORD_HEAD_SZ; + length = le16_to_cpu(action->record.undo_length); + undo = get_undo_offset(&action->record); + if ((undo + length) > end) + data = (char*)NULL; + else + data = ((const char*)&action->record) + undo; + oldlength = le16_to_cpu(action->record.redo_length); + target = le16_to_cpu(action->record.record_offset) + + le16_to_cpu(action->record.attribute_offset); + if (length == oldlength) { + if (optv > 1) { + lcn = sle64_to_cpu(action->record.lcn_list[0]); + printf("-> inode %lld lcn 0x%llx target 0x%x" + " length %d\n", + (long long)inode_number(&action->record), + (long long)lcn, (int)target, (int)length); + } + if (optv > 1) { + printf("-> existing record :\n"); + dump(&buffer[target], length); + } + if ((target + length) <= mftrecsz) { + changed = memcmp(buffer + target, data, length); + err = 0; + if (changed) { + memcpy(buffer + target, data, length); + if (optv > 1) { + printf("-> new record :\n"); + dump(buffer + target, length); + } + err = write_protected(vol, &action->record, + buffer, mftrecsz); + } + if (optv > 1) { + printf("-> MFT record %s\n", + (changed ? "updated" : "unchanged")); + } + } + } else { + if (length > oldlength) + err = add_non_resident(/*vol, action, data, + target, length, oldlength*/); + else + err = delete_non_resident(/*vol, action, data, + target, length, oldlength*/); + } + return (err); +} + +enum ACTION_KIND { ON_NONE, ON_MFT, ON_INDX, ON_RAW } ; + +static enum ACTION_KIND get_action_kind(const struct ACTION_RECORD *action) +{ + struct ATTR *pa; + const char *data; + enum ACTION_KIND kind; + /* + * If we are sure the action was defined by Vista + * or subsequent, just use attribute_flags. + * Unfortunately, only on some cases we can determine + * the action was defined by Win10 (or subsequent). + */ + if (action->record.log_record_flags + & (LOG_RECORD_DELETING | LOG_RECORD_ADDING)) { + if (action->record.attribute_flags & ACTS_ON_INDX) + kind = ON_INDX; + else + if (action->record.attribute_flags & ACTS_ON_MFT) + kind = ON_MFT; + else + kind = ON_RAW; + } else { + /* + * In other cases, we have to rely on the attribute + * definition, but this has defects when undoing. + */ + pa = getattrentry(le16_to_cpu( + action->record.target_attribute),0); + if (!pa || !pa->type) { + /* + * Even when the attribute has not been recorded, + * we can sometimes tell the record does not apply + * to MFT or INDX : such records always have a zero + * record_offset, and if attribute_offset is zero, their + * magic can be checked. If neither condition is true, + * the action cannot apply to MFT or INDX. + * (this is useful for undoing) + */ + data = (const char*)&action->record + + get_redo_offset(&action->record); + if (action->record.record_offset + || (!action->record.attribute_offset + && (le16_to_cpu(action->record.redo_length) + >= 4) + && memcmp(data,"FILE",4) + && memcmp(data,"INDX",4))) { + kind = ON_RAW; + } else { + printf("** Error : attribute 0x%x" + " is not defined\n", + (int)le16_to_cpu( + action->record.target_attribute)); + kind = ON_NONE; + } + } else { + if (pa->type == AT_INDEX_ALLOCATION) + kind = ON_INDX; + else + kind = ON_RAW; + } + } + return (kind); +} + + +/* + * Display the redo actions which were executed + * + * Useful for getting indications on the coverage of a test + */ + +void show_redos(void) +{ + int i; + + if (optv && redos_met) { + printf("Redo actions which were executed :\n"); + for (i=0; i<64; i++) + if ((((u64)1) << i) & redos_met) + printf("%s\n", actionname(i)); + } +} + +static int distribute_redos(ntfs_volume *vol, + const struct ACTION_RECORD *action, char *buffer) +{ + int rop, uop; + int err; + + err = 0; + rop = le16_to_cpu(action->record.redo_operation); + uop = le16_to_cpu(action->record.undo_operation); + switch (rop) { + case AddIndexEntryAllocation : + if (action->record.undo_operation + == const_cpu_to_le16(DeleteIndexEntryAllocation)) + err = redo_add_index(vol, action, buffer); + break; + case AddIndexEntryRoot : + if (action->record.undo_operation + == const_cpu_to_le16(DeleteIndexEntryRoot)) + err = redo_add_root_index(vol, action, buffer); + break; + case ClearBitsInNonResidentBitMap : + if ((action->record.undo_operation + == const_cpu_to_le16(SetBitsInNonResidentBitMap)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_force_bits(vol, action, buffer); + break; + case CompensationlogRecord : + if (action->record.undo_operation + == const_cpu_to_le16(Noop)) + err = redo_compensate(vol, action, buffer); + break; + case CreateAttribute : + if ((action->record.undo_operation + == const_cpu_to_le16(DeleteAttribute)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_create_attribute(vol, action, buffer); + break; + case DeallocateFileRecordSegment : + if ((action->record.undo_operation + == const_cpu_to_le16(InitializeFileRecordSegment)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_delete_file(vol, action, buffer); + break; + case DeleteAttribute : + if ((action->record.undo_operation + == const_cpu_to_le16(CreateAttribute)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_delete_attribute(vol, action, buffer); + break; + case DeleteIndexEntryAllocation : + if ((action->record.undo_operation + == const_cpu_to_le16(AddIndexEntryAllocation)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_delete_index(vol, action, buffer); + break; + case DeleteIndexEntryRoot : + if ((action->record.undo_operation + == const_cpu_to_le16(AddIndexEntryRoot)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_delete_root_index(vol, action, buffer); + break; + case InitializeFileRecordSegment : + if (action->record.undo_operation + == const_cpu_to_le16(Noop)) + err = redo_create_file(vol, action, buffer); + break; + case OpenNonResidentAttribute : + if (action->record.undo_operation + == const_cpu_to_le16(Noop)) + err = redo_open_attribute(vol, action); + break; + case SetBitsInNonResidentBitMap : + if (action->record.undo_operation + == const_cpu_to_le16(ClearBitsInNonResidentBitMap)) + err = redo_force_bits(vol, action, buffer); + break; + case SetIndexEntryVcnAllocation : + if ((action->record.undo_operation + == const_cpu_to_le16(SetIndexEntryVcnAllocation)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_update_vcn(vol, action, buffer); + break; + case SetIndexEntryVcnRoot : + if (action->record.undo_operation + == const_cpu_to_le16(SetIndexEntryVcnRoot)) + err = redo_update_root_vcn(vol, action, buffer); + break; + case SetNewAttributeSizes : + if ((action->record.undo_operation + == const_cpu_to_le16(SetNewAttributeSizes)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_sizes(vol, action, buffer); + break; + case UpdateFileNameAllocation : + if ((action->record.undo_operation + == const_cpu_to_le16(UpdateFileNameAllocation)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_update_index(vol, action, buffer); + break; + case UpdateFileNameRoot : + if ((action->record.undo_operation + == const_cpu_to_le16(UpdateFileNameRoot)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_update_root_index(vol, action, buffer); + break; + case UpdateMappingPairs : + if (action->record.undo_operation + == const_cpu_to_le16(UpdateMappingPairs)) + err = redo_update_mapping(vol, action, buffer); + break; + case UpdateNonResidentValue : + switch (get_action_kind(action)) { + case ON_INDX : + err = redo_update_index_value(vol, action, buffer); + break; + case ON_RAW : + err = redo_update_value(vol, action, buffer); + break; + default : + printf("** Bad attribute type\n"); + err = 1; + } + break; + case UpdateResidentValue : + if ((action->record.undo_operation + == const_cpu_to_le16(UpdateResidentValue)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_update_resident(vol, action, buffer); + break; + case Win10Action37 : + if (action->record.undo_operation + == const_cpu_to_le16(Noop)) + err = redo_action37(vol, action, buffer); + break; + case WriteEndofFileRecordSegment : + if (action->record.undo_operation + == const_cpu_to_le16(WriteEndofFileRecordSegment)) + err = redo_write_end(vol, action, buffer); + break; + case WriteEndOfIndexBuffer : + if ((action->record.undo_operation + == const_cpu_to_le16(WriteEndOfIndexBuffer)) + || (action->record.undo_operation + == const_cpu_to_le16(CompensationlogRecord))) + err = redo_write_index(vol, action, buffer); + break; + case AttributeNamesDump : + case DirtyPageTableDump : + case ForgetTransaction : + case Noop : + case OpenAttributeTableDump : + break; + default : + printf("** Unsupported redo %s\n", actionname(rop)); + err = 1; + break; + } + redos_met |= ((u64)1) << rop; + if (err) + printf("* Redoing action %d %s (%s) failed\n", + action->num,actionname(rop), actionname(uop)); + return (err); +} + +/* + * Redo a single action + * + * The record the action acts on is read and, when it is an MFT or + * INDX one, the need for redoing is checked. + * + * When this is an action which creates a new MFT or INDX record + * and the old one cannot be read (usually because it was not + * initialized), a zeroed buffer is allocated. + */ + +static int play_one_redo(ntfs_volume *vol, const struct ACTION_RECORD *action) +{ + MFT_RECORD *entry; + INDEX_BLOCK *indx; + char *buffer; + s64 this_lsn; + s64 data_lsn; + u32 xsize; + int err; + BOOL warn; + BOOL executed; + enum ACTION_KIND kind; + u16 rop; + u16 uop; + + err = 0; + rop = le16_to_cpu(action->record.redo_operation); + uop = le16_to_cpu(action->record.undo_operation); + this_lsn = sle64_to_cpu(action->record.this_lsn); + if (optv) + printf("Redo action %d %s (%s) 0x%llx\n", + action->num, + actionname(rop), actionname(uop), + (long long)sle64_to_cpu( + action->record.this_lsn)); + buffer = (char*)NULL; + switch (rop) { + /* Actions always acting on MFT */ + case AddIndexEntryRoot : + case CreateAttribute : + case DeallocateFileRecordSegment : + case DeleteAttribute : + case DeleteIndexEntryRoot : + case InitializeFileRecordSegment : + case SetIndexEntryVcnRoot : + case SetNewAttributeSizes : + case UpdateFileNameRoot : + case UpdateMappingPairs : + case UpdateResidentValue : + case Win10Action37 : + case WriteEndofFileRecordSegment : + kind = ON_MFT; + break; + /* Actions always acting on INDX */ + case AddIndexEntryAllocation : + case DeleteIndexEntryAllocation : + case SetIndexEntryVcnAllocation : + case UpdateFileNameAllocation : + case WriteEndOfIndexBuffer : + kind = ON_INDX; + break; + /* Actions never acting on MFT or INDX */ + case ClearBitsInNonResidentBitMap : + case SetBitsInNonResidentBitMap : + kind = ON_RAW; + break; + /* Actions which may act on MFT */ + case Noop : /* on MFT if DeallocateFileRecordSegment */ + kind = ON_NONE; + break; + /* Actions which may act on INDX */ + case UpdateNonResidentValue : + /* Known cases : INDX, $SDS, ATTR_LIST */ + kind = get_action_kind(action); + if (kind == ON_NONE) + err = 1; + break; + case CompensationlogRecord : + case OpenNonResidentAttribute : + /* probably not important */ + kind = ON_NONE; + break; + /* Actions currently ignored */ + case AttributeNamesDump : + case DirtyPageTableDump : + case ForgetTransaction : + case OpenAttributeTableDump : + case TransactionTableDump : + kind = ON_NONE; + break; + /* Actions with no known use case */ + case CommitTransaction : + case DeleteDirtyClusters : + case EndTopLevelAction : + case HotFix : + case PrepareTransaction : + case UpdateRecordDataAllocation : + case UpdateRecordDataRoot : + case Win10Action35 : + case Win10Action36 : + default : + err = 1; + kind = ON_NONE; + break; + } + executed = FALSE; + switch (kind) { + case ON_MFT : +/* + the check below cannot be used on WinXP +if (!(action->record.attribute_flags & ACTS_ON_MFT)) +printf("** %s (action %d) not acting on MFT\n",actionname(rop),(int)action->num); +*/ + /* Check whether data is to be discarded */ + warn = (rop != InitializeFileRecordSegment) + || !check_full_mft(action,TRUE); + buffer = read_protected(vol, &action->record, + mftrecsz, warn); + entry = (MFT_RECORD*)buffer; + if (entry && (entry->magic == magic_FILE)) { + data_lsn = sle64_to_cpu(entry->lsn); + /* + * Beware of records not updated + * during the last session which may + * have a stale lsn (consequence + * of ntfs-3g resetting the log) + */ + executed = ((s64)(data_lsn - this_lsn) >= 0) + && (((s64)(data_lsn - latest_lsn)) <= 0) + && !exception(action->num); + } else { + if (!warn) { + /* Old record not needed */ + if (!buffer) + buffer = (char*)calloc(1, mftrecsz); + if (buffer) + executed = FALSE; + else + err = 1; + } else { + printf("** %s (action %d) not" + " acting on MFT\n", + actionname(rop), + (int)action->num); + err = 1; + } + } + break; + case ON_INDX : +/* + the check below cannot be used on WinXP +if (!(action->record.attribute_flags & ACTS_ON_INDX)) +printf("** %s (action %d) not acting on INDX\n",actionname(rop),(int)action->num); +*/ + xsize = vol->indx_record_size; + /* Check whether data is to be discarded */ + warn = (rop != UpdateNonResidentValue) + || !check_full_index(action,TRUE); + buffer = read_protected(vol, &action->record, + xsize, warn); + indx = (INDEX_BLOCK*)buffer; + if (indx && (indx->magic == magic_INDX)) { + data_lsn = sle64_to_cpu(indx->lsn); + /* + * Beware of records not updated + * during the last session which may + * have a stale lsn (consequence + * of ntfs-3g resetting the log) + */ + executed = ((s64)(data_lsn - this_lsn) >= 0) + && (((s64)(data_lsn - latest_lsn)) <= 0) + && ! exception(action->num); + } else { + if (!warn) { + /* Old record not needed */ + if (!buffer) + buffer = (char*)calloc(1, xsize); + if (buffer) + executed = FALSE; + else + err = 1; + } else { + printf("** %s (action %d) not" + " acting on INDX\n", + actionname(rop), + (int)action->num); + err = 1; + } + } + break; + case ON_RAW : + if (action->record.attribute_flags + & (ACTS_ON_INDX | ACTS_ON_MFT)) { + printf("** Error : action %s on MFT" + " or INDX\n", + actionname(rop)); + err = 1; + } else { + buffer = read_raw(vol, &action->record); + if (!buffer) + err = 1; + } + break; + default : + buffer = (char*)NULL; + break; + } + if (!err && (!executed || !opts)) { + err = distribute_redos(vol, action, buffer); + redocount++; + } else { + if (optv) + printf("Action %d %s (%s) not redone\n", + action->num, + actionname(rop), + actionname(uop)); + } + if (buffer) + free(buffer); + return (err); +} + + +/* + * Play the redo actions from earliest to latest + * + * Currently we can only redo the last undone transaction, + * otherwise the attribute table would be out of phase. + */ + +int play_redos(ntfs_volume *vol, const struct ACTION_RECORD *firstaction) +{ + const struct ACTION_RECORD *action; + int err; + + err = 0; + action = firstaction; + while (action && !err) { + /* Only committed actions should be redone */ + if ((!optc || within_lcn_range(&action->record)) + && (action->flags & ACTION_TO_REDO)) + err = play_one_redo(vol, action); + if (!err) + action = action->next; + } + return (err); +} + +static int distribute_undos(ntfs_volume *vol, const struct ACTION_RECORD *action, + char *buffer) +{ + int rop, uop; + int err; + + err = 0; + rop = le16_to_cpu(action->record.redo_operation); + uop = le16_to_cpu(action->record.undo_operation); + switch (rop) { + case AddIndexEntryAllocation : + if (action->record.undo_operation + == const_cpu_to_le16(DeleteIndexEntryAllocation)) + err = undo_add_index(vol, action, buffer); + break; + case AddIndexEntryRoot : + if (action->record.undo_operation + == const_cpu_to_le16(DeleteIndexEntryRoot)) + err = undo_add_root_index(vol, action, buffer); + break; + case ClearBitsInNonResidentBitMap : + if (action->record.undo_operation + == const_cpu_to_le16(SetBitsInNonResidentBitMap)) + err = undo_force_bits(vol, action, buffer); + break; + case CreateAttribute : + if (action->record.undo_operation + == const_cpu_to_le16(DeleteAttribute)) + err = undo_create_attribute(vol, action, buffer); + break; + case DeallocateFileRecordSegment : + if (action->record.undo_operation + == const_cpu_to_le16(InitializeFileRecordSegment)) + err = undo_delete_file(vol, action, buffer); + break; + case DeleteAttribute : + if (action->record.undo_operation + == const_cpu_to_le16(CreateAttribute)) + err = undo_delete_attribute(vol, action, buffer); + break; + case DeleteIndexEntryAllocation : + if (action->record.undo_operation + == const_cpu_to_le16(AddIndexEntryAllocation)) + err = undo_delete_index(vol, action, buffer); + break; + case DeleteIndexEntryRoot : + if (action->record.undo_operation + == const_cpu_to_le16(AddIndexEntryRoot)) + err = undo_delete_root_index(vol, action, buffer); + break; + case InitializeFileRecordSegment : + if (action->record.undo_operation + == const_cpu_to_le16(Noop)) + err = undo_create_file(vol, action, buffer); + break; + case OpenNonResidentAttribute : + if (action->record.undo_operation + == const_cpu_to_le16(Noop)) + err = undo_open_attribute(vol, action); + break; + case SetBitsInNonResidentBitMap : + if (action->record.undo_operation + == const_cpu_to_le16(ClearBitsInNonResidentBitMap)) + err = undo_force_bits(vol, action, buffer); + break; + case SetIndexEntryVcnAllocation : + if (action->record.undo_operation + == const_cpu_to_le16(SetIndexEntryVcnAllocation)) + err = undo_update_vcn(vol, action, buffer); + break; + case SetIndexEntryVcnRoot : + if (action->record.undo_operation + == const_cpu_to_le16(SetIndexEntryVcnRoot)) + err = undo_update_root_vcn(vol, action, buffer); + break; + case SetNewAttributeSizes : + if (action->record.undo_operation + == const_cpu_to_le16(SetNewAttributeSizes)) + err = undo_sizes(vol, action, buffer); + break; + case UpdateFileNameAllocation : + if (action->record.undo_operation + == const_cpu_to_le16(UpdateFileNameAllocation)) + err = undo_update_index(vol, action, buffer); + break; + case UpdateFileNameRoot : + if (action->record.undo_operation + == const_cpu_to_le16(UpdateFileNameRoot)) + err = undo_update_root_index(vol, action, buffer); + break; + case UpdateMappingPairs : + if (action->record.undo_operation + == const_cpu_to_le16(UpdateMappingPairs)) + err = undo_update_mapping(vol, action, buffer); + break; + case UpdateNonResidentValue : + switch (get_action_kind(action)) { + case ON_INDX : + err = undo_update_index_value(vol, action, buffer); + break; + case ON_RAW : + err = undo_update_value(vol, action, buffer); + break; + default : + printf("** Bad attribute type\n"); + err = 1; + } + break; + case UpdateResidentValue : + if (action->record.undo_operation + == const_cpu_to_le16(UpdateResidentValue)) + err = undo_update_resident(vol, action, buffer); + break; + case Win10Action37 : + if (action->record.undo_operation + == const_cpu_to_le16(Noop)) + err = undo_action37(vol, action, buffer); + break; + case WriteEndofFileRecordSegment : + if (action->record.undo_operation + == const_cpu_to_le16(WriteEndofFileRecordSegment)) + err = undo_write_end(vol, action, buffer); + break; + case WriteEndOfIndexBuffer : + if (action->record.undo_operation + == const_cpu_to_le16(WriteEndOfIndexBuffer)) + err = undo_write_index(vol, action, buffer); + break; + case AttributeNamesDump : + case CompensationlogRecord : + case DirtyPageTableDump : + case ForgetTransaction : + case Noop : + case OpenAttributeTableDump : + break; + default : + printf("** Unsupported undo %s\n", actionname(rop)); + err = 1; + break; + } + if (err) + printf("* Undoing action %d %s (%s) failed\n", + action->num,actionname(rop), actionname(uop)); + return (err); +} + +/* + * Undo a single action + * + * The record the action acts on is read and, when it is an MFT or + * INDX one, the need for undoing is checked. + */ + +static int play_one_undo(ntfs_volume *vol, const struct ACTION_RECORD *action) +{ + MFT_RECORD *entry; + INDEX_BLOCK *indx; + char *buffer; + u32 xsize; + u16 rop; + u16 uop; + int err; + BOOL executed; + enum ACTION_KIND kind; + + err = 0; + rop = le16_to_cpu(action->record.redo_operation); + uop = le16_to_cpu(action->record.undo_operation); + if (optv) + printf("Undo action %d %s (%s) lsn 0x%llx\n", + action->num, + actionname(rop), actionname(uop), + (long long)sle64_to_cpu( + action->record.this_lsn)); + buffer = (char*)NULL; + executed = FALSE; + kind = ON_NONE; + switch (rop) { + /* Actions always acting on MFT */ + case AddIndexEntryRoot : + case CreateAttribute : + case DeallocateFileRecordSegment : + case DeleteAttribute : + case DeleteIndexEntryRoot : + case InitializeFileRecordSegment : + case SetIndexEntryVcnRoot : + case SetNewAttributeSizes : + case UpdateFileNameRoot : + case UpdateMappingPairs : + case UpdateResidentValue : + case Win10Action37 : + case WriteEndofFileRecordSegment : + kind = ON_MFT; + break; + /* Actions always acting on INDX */ + case AddIndexEntryAllocation : + case DeleteIndexEntryAllocation : + case SetIndexEntryVcnAllocation : + case UpdateFileNameAllocation : + case WriteEndOfIndexBuffer : + kind = ON_INDX; + break; + /* Actions never acting on MFT or INDX */ + case ClearBitsInNonResidentBitMap : + case SetBitsInNonResidentBitMap : + kind = ON_RAW; + break; + /* Actions which may act on MFT */ + case Noop : /* on MFT if DeallocateFileRecordSegment */ + break; + /* Actions which may act on INDX */ + case UpdateNonResidentValue : + /* Known cases : INDX, $SDS, ATTR_LIST */ + kind = get_action_kind(action); + if (kind == ON_NONE) + err = 1; + break; + case OpenNonResidentAttribute : + /* probably not important */ + kind = ON_NONE; + break; + /* Actions currently ignored */ + case AttributeNamesDump : + case CommitTransaction : + case CompensationlogRecord : + case DeleteDirtyClusters : + case DirtyPageTableDump : + case EndTopLevelAction : + case ForgetTransaction : + case HotFix : + case OpenAttributeTableDump : + case PrepareTransaction : + case TransactionTableDump : + case UpdateRecordDataAllocation : + case UpdateRecordDataRoot : + case Win10Action35 : + case Win10Action36 : + kind = ON_NONE; + break; + } + switch (kind) { + case ON_MFT : +/* + the check below cannot be used on WinXP +if (!(action->record.attribute_flags & ACTS_ON_MFT)) +printf("** %s (action %d) not acting on MFT\n",actionname(rop),(int)action->num); +*/ + buffer = read_protected(vol, &action->record, mftrecsz, TRUE); + entry = (MFT_RECORD*)buffer; + if (entry) { + if (entry->magic == magic_FILE) { + executed = !older_record(entry, + &action->record); + if (!executed + && exception(action->num)) + executed = TRUE; +if (optv > 1) +printf("record lsn 0x%llx is %s than action %d lsn 0x%llx\n", +(long long)sle64_to_cpu(entry->lsn), +(executed ? "not older" : "older"), +(int)action->num, +(long long)sle64_to_cpu(action->record.this_lsn)); + } else { + printf("** %s (action %d) not acting on MFT\n", + actionname(rop), (int)action->num); + err = 1; + } + } else { + /* + * Could not read the MFT record : + * if this is undoing a record create (from scratch) + * which did not take place, there is nothing to redo, + * otherwise this is an error. + */ + if (check_full_mft(action,TRUE)) + executed = FALSE; + else + err = 1; + } + break; + case ON_INDX : +/* + the check below cannot be used on WinXP +if (!(action->record.attribute_flags & ACTS_ON_INDX)) +printf("** %s (action %d) not acting on INDX\n",actionname(rop),(int)action->num); +*/ + xsize = vol->indx_record_size; + buffer = read_protected(vol, &action->record, xsize, TRUE); + indx = (INDEX_BLOCK*)buffer; + if (indx) { + if (indx->magic == magic_INDX) { + executed = !older_record(indx, + &action->record); + if (!executed + && exception(action->num)) + executed = TRUE; +if (optv > 1) +printf("index lsn 0x%llx is %s than action %d lsn 0x%llx\n", +(long long)sle64_to_cpu(indx->lsn), +(executed ? "not older" : "older"), +(int)action->num, +(long long)sle64_to_cpu(action->record.this_lsn)); + } else { + printf("** %s (action %d) not acting on INDX\n", + actionname(rop), (int)action->num); + err = 1; + } + } else { + /* + * Could not read the INDX record : + * if this is undoing a record create (from scratch) + * which did not take place, there is nothing to redo, + * otherwise this must be an error. + * However, after deleting the last index allocation + * in a block, the block is apparently zeroed + * and cannot be read. In this case we have to + * create an initial index block and apply the undo. + */ + if (check_full_index(action,TRUE)) + executed = FALSE; + else { + err = 1; + if (uop == AddIndexEntryAllocation) { + executed = TRUE; + buffer = (char*)calloc(1, xsize); + if (buffer) + err = create_indx(vol, + action, buffer); + } + } + } + break; + case ON_RAW : + if (action->record.attribute_flags + & (ACTS_ON_INDX | ACTS_ON_MFT)) { + printf("** Error : action %s on MFT or INDX\n", + actionname(rop)); + err = 1; + } else { + buffer = read_raw(vol, &action->record); + if (!buffer) + err = 1; + } + executed = TRUE; + break; + default : + executed = TRUE; + buffer = (char*)NULL; + break; + } + if (!err && executed) { + err = distribute_undos(vol, action, buffer); + undocount++; + } + if (buffer) + free(buffer); + + return (err); +} + +/* + * Play the undo actions from latest to earliest + * + * For structured record, a check is made on the lsn to only + * try to undo the actions which were executed. This implies + * identifying actions on a structured record. + * + * Returns 0 if successful + */ + +int play_undos(ntfs_volume *vol, const struct ACTION_RECORD *lastaction) +{ + const struct ACTION_RECORD *action; + int err; + + err = 0; + action = lastaction; + while (action && !err) { + if (!optc || within_lcn_range(&action->record)) + err = play_one_undo(vol, action); + if (!err) + action = action->prev; + } + return (err); +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/sd.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/sd.c new file mode 100644 index 0000000..a60a828 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/sd.c @@ -0,0 +1,607 @@ +#include "types.h" +#include "layout.h" +#include "sd.h" + +/** + * init_system_file_sd - + * + * NTFS 3.1 - System files security decriptors + * ===================================================== + * + * Create the security descriptor for system file number @sys_file_no and + * return a pointer to the descriptor. + * + * Note the root directory system file (".") is very different and handled by a + * different function. + * + * The sd is returned in *@sd_val and has length *@sd_val_len. + * + * Do NOT free *@sd_val as it is static memory. This also means that you can + * only use *@sd_val until the next call to this function. + */ +void init_system_file_sd(int sys_file_no, u8 **sd_val, int *sd_val_len) +{ + static u8 sd_array[0x68]; + SECURITY_DESCRIPTOR_RELATIVE *sd; + ACL *acl; + ACCESS_ALLOWED_ACE *aa_ace; + SID *sid; + le32 *sub_authorities; + + if (sys_file_no < 0) { + *sd_val = NULL; + *sd_val_len = 0; + return; + } + *sd_val = sd_array; + sd = (SECURITY_DESCRIPTOR_RELATIVE*)&sd_array; + sd->revision = 1; + sd->alignment = 0; + sd->control = SE_SELF_RELATIVE | SE_DACL_PRESENT; + *sd_val_len = 0x64; + sd->owner = const_cpu_to_le32(0x48); + sd->group = const_cpu_to_le32(0x54); + sd->sacl = const_cpu_to_le32(0); + sd->dacl = const_cpu_to_le32(0x14); + /* + * Now at offset 0x14, as specified in the security descriptor, we have + * the DACL. + */ + acl = (ACL*)((char*)sd + le32_to_cpu(sd->dacl)); + acl->revision = 2; + acl->alignment1 = 0; + acl->size = const_cpu_to_le16(0x34); + acl->ace_count = const_cpu_to_le16(2); + acl->alignment2 = const_cpu_to_le16(0); + /* + * Now at offset 0x1c, just after the DACL's ACL, we have the first + * ACE of the DACL. The type of the ACE is access allowed. + */ + aa_ace = (ACCESS_ALLOWED_ACE*)((char*)acl + sizeof(ACL)); + aa_ace->type = ACCESS_ALLOWED_ACE_TYPE; + aa_ace->flags = 0; + aa_ace->size = const_cpu_to_le16(0x14); + switch (sys_file_no) { + case FILE_AttrDef: + case FILE_Boot: + aa_ace->mask = SYNCHRONIZE | STANDARD_RIGHTS_READ | + FILE_READ_ATTRIBUTES | FILE_READ_EA | FILE_READ_DATA; + break; + default: + aa_ace->mask = SYNCHRONIZE | STANDARD_RIGHTS_WRITE | + FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | + FILE_WRITE_EA | FILE_READ_EA | FILE_APPEND_DATA | + FILE_WRITE_DATA | FILE_READ_DATA; + break; + } + aa_ace->sid.revision = 1; + aa_ace->sid.sub_authority_count = 1; + aa_ace->sid.identifier_authority.value[0] = 0; + aa_ace->sid.identifier_authority.value[1] = 0; + aa_ace->sid.identifier_authority.value[2] = 0; + aa_ace->sid.identifier_authority.value[3] = 0; + aa_ace->sid.identifier_authority.value[4] = 0; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + aa_ace->sid.identifier_authority.value[5] = 5; + aa_ace->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_LOCAL_SYSTEM_RID); + /* + * Now at offset 0x30 within security descriptor, just after the first + * ACE of the DACL. All system files, except the root directory, have + * a second ACE. + */ + /* The second ACE of the DACL. Type is access allowed. */ + aa_ace = (ACCESS_ALLOWED_ACE*)((char*)aa_ace + + le16_to_cpu(aa_ace->size)); + aa_ace->type = ACCESS_ALLOWED_ACE_TYPE; + aa_ace->flags = 0; + aa_ace->size = const_cpu_to_le16(0x18); + /* Only $AttrDef and $Boot behave differently to everything else. */ + switch (sys_file_no) { + case FILE_AttrDef: + case FILE_Boot: + aa_ace->mask = SYNCHRONIZE | STANDARD_RIGHTS_READ | + FILE_READ_ATTRIBUTES | FILE_READ_EA | + FILE_READ_DATA; + break; + default: + aa_ace->mask = SYNCHRONIZE | STANDARD_RIGHTS_READ | + FILE_WRITE_ATTRIBUTES | + FILE_READ_ATTRIBUTES | FILE_WRITE_EA | + FILE_READ_EA | FILE_APPEND_DATA | + FILE_WRITE_DATA | FILE_READ_DATA; + break; + } + aa_ace->sid.revision = 1; + aa_ace->sid.sub_authority_count = 2; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + aa_ace->sid.identifier_authority.value[0] = 0; + aa_ace->sid.identifier_authority.value[1] = 0; + aa_ace->sid.identifier_authority.value[2] = 0; + aa_ace->sid.identifier_authority.value[3] = 0; + aa_ace->sid.identifier_authority.value[4] = 0; + aa_ace->sid.identifier_authority.value[5] = 5; + sub_authorities = aa_ace->sid.sub_authority; + *sub_authorities++ = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + *sub_authorities = + const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + /* + * Now at offset 0x48 into the security descriptor, as specified in the + * security descriptor, we now have the owner SID. + */ + sid = (SID*)((char*)sd + le32_to_cpu(sd->owner)); + sid->revision = 1; + sid->sub_authority_count = 1; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + sid->identifier_authority.value[0] = 0; + sid->identifier_authority.value[1] = 0; + sid->identifier_authority.value[2] = 0; + sid->identifier_authority.value[3] = 0; + sid->identifier_authority.value[4] = 0; + sid->identifier_authority.value[5] = 5; + sid->sub_authority[0] = const_cpu_to_le32(SECURITY_LOCAL_SYSTEM_RID); + /* + * Now at offset 0x54 into the security descriptor, as specified in the + * security descriptor, we have the group SID. + */ + sid = (SID*)((char*)sd + le32_to_cpu(sd->group)); + sid->revision = 1; + sid->sub_authority_count = 2; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + sid->identifier_authority.value[0] = 0; + sid->identifier_authority.value[1] = 0; + sid->identifier_authority.value[2] = 0; + sid->identifier_authority.value[3] = 0; + sid->identifier_authority.value[4] = 0; + sid->identifier_authority.value[5] = 5; + sub_authorities = sid->sub_authority; + *sub_authorities++ = const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + *sub_authorities = const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); +} + +/** + * init_root_sd - + * + * Creates the security_descriptor for the root folder on ntfs 3.1 as created + * by Windows Vista (when the format is done from the disk management MMC + * snap-in, note this is different from the format done from the disk + * properties in Windows Explorer). + */ +void init_root_sd(u8 **sd_val, int *sd_val_len) +{ + SECURITY_DESCRIPTOR_RELATIVE *sd; + ACL *acl; + ACCESS_ALLOWED_ACE *ace; + SID *sid; + le32 *sub_authorities; + + static char sd_array[0x102c]; + *sd_val_len = 0x102c; + *sd_val = (u8*)&sd_array; + + //security descriptor relative + sd = (SECURITY_DESCRIPTOR_RELATIVE*)sd_array; + sd->revision = SECURITY_DESCRIPTOR_REVISION; + sd->alignment = 0; + sd->control = SE_SELF_RELATIVE | SE_DACL_PRESENT; + sd->owner = const_cpu_to_le32(0x1014); + sd->group = const_cpu_to_le32(0x1020); + sd->sacl = const_cpu_to_le32(0); + sd->dacl = const_cpu_to_le32(sizeof(SECURITY_DESCRIPTOR_RELATIVE)); + + //acl + acl = (ACL*)((u8*)sd + sizeof(SECURITY_DESCRIPTOR_RELATIVE)); + acl->revision = ACL_REVISION; + acl->alignment1 = 0; + acl->size = const_cpu_to_le16(0x1000); + acl->ace_count = const_cpu_to_le16(0x08); + acl->alignment2 = const_cpu_to_le16(0); + + //ace1 + ace = (ACCESS_ALLOWED_ACE*)((u8*)acl + sizeof(ACL)); + ace->type = ACCESS_ALLOWED_ACE_TYPE; + ace->flags = 0; + ace->size = const_cpu_to_le16(0x18); + ace->mask = STANDARD_RIGHTS_ALL | FILE_WRITE_ATTRIBUTES | + FILE_LIST_DIRECTORY | FILE_WRITE_DATA | + FILE_ADD_SUBDIRECTORY | FILE_READ_EA | FILE_WRITE_EA | + FILE_TRAVERSE | FILE_DELETE_CHILD | + FILE_READ_ATTRIBUTES; + ace->sid.revision = SID_REVISION; + ace->sid.sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + sub_authorities = ace->sid.sub_authority; + *sub_authorities++ = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + *sub_authorities = const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + + //ace2 + ace = (ACCESS_ALLOWED_ACE*)((u8*)ace + le16_to_cpu(ace->size)); + ace->type = ACCESS_ALLOWED_ACE_TYPE; + ace->flags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | + INHERIT_ONLY_ACE; + ace->size = const_cpu_to_le16(0x18); + ace->mask = GENERIC_ALL; + ace->sid.revision = SID_REVISION; + ace->sid.sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + sub_authorities = ace->sid.sub_authority; + *sub_authorities++ = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + *sub_authorities = const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + + //ace3 + ace = (ACCESS_ALLOWED_ACE*)((u8*)ace + le16_to_cpu(ace->size)); + ace->type = ACCESS_ALLOWED_ACE_TYPE; + ace->flags = 0; + ace->size = const_cpu_to_le16(0x14); + ace->mask = STANDARD_RIGHTS_ALL | FILE_WRITE_ATTRIBUTES | + FILE_LIST_DIRECTORY | FILE_WRITE_DATA | + FILE_ADD_SUBDIRECTORY | FILE_READ_EA | FILE_WRITE_EA | + FILE_TRAVERSE | FILE_DELETE_CHILD | + FILE_READ_ATTRIBUTES; + ace->sid.revision = SID_REVISION; + ace->sid.sub_authority_count = 0x01; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + ace->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_LOCAL_SYSTEM_RID); + + //ace4 + ace = (ACCESS_ALLOWED_ACE*)((u8*)ace + le16_to_cpu(ace->size)); + ace->type = ACCESS_ALLOWED_ACE_TYPE; + ace->flags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | + INHERIT_ONLY_ACE; + ace->size = const_cpu_to_le16(0x14); + ace->mask = GENERIC_ALL; + ace->sid.revision = SID_REVISION; + ace->sid.sub_authority_count = 0x01; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + ace->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_LOCAL_SYSTEM_RID); + + //ace5 + ace = (ACCESS_ALLOWED_ACE*)((char*)ace + le16_to_cpu(ace->size)); + ace->type = ACCESS_ALLOWED_ACE_TYPE; + ace->flags = 0; + ace->size = const_cpu_to_le16(0x14); + ace->mask = SYNCHRONIZE | READ_CONTROL | DELETE | + FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | + FILE_TRAVERSE | FILE_WRITE_EA | FILE_READ_EA | + FILE_ADD_SUBDIRECTORY | FILE_ADD_FILE | + FILE_LIST_DIRECTORY; + ace->sid.revision = SID_REVISION; + ace->sid.sub_authority_count = 0x01; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + ace->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_AUTHENTICATED_USER_RID); + + //ace6 + ace = (ACCESS_ALLOWED_ACE*)((u8*)ace + le16_to_cpu(ace->size)); + ace->type = ACCESS_ALLOWED_ACE_TYPE; + ace->flags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | + INHERIT_ONLY_ACE; + ace->size = const_cpu_to_le16(0x14); + ace->mask = GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | DELETE; + ace->sid.revision = SID_REVISION; + ace->sid.sub_authority_count = 0x01; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + ace->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_AUTHENTICATED_USER_RID); + + //ace7 + ace = (ACCESS_ALLOWED_ACE*)((u8*)ace + le16_to_cpu(ace->size)); + ace->type = ACCESS_ALLOWED_ACE_TYPE; + ace->flags = 0; + ace->size = const_cpu_to_le16(0x18); + ace->mask = SYNCHRONIZE | READ_CONTROL | FILE_READ_ATTRIBUTES | + FILE_TRAVERSE | FILE_READ_EA | FILE_LIST_DIRECTORY; + ace->sid.revision = SID_REVISION; + ace->sid.sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + sub_authorities = ace->sid.sub_authority; + *sub_authorities++ = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + *sub_authorities = const_cpu_to_le32(DOMAIN_ALIAS_RID_USERS); + + //ace8 + ace = (ACCESS_ALLOWED_ACE*)((u8*)ace + le16_to_cpu(ace->size)); + ace->type = ACCESS_ALLOWED_ACE_TYPE; + ace->flags = OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE | + INHERIT_ONLY_ACE; + ace->size = const_cpu_to_le16(0x18); + ace->mask = GENERIC_READ | GENERIC_EXECUTE; + ace->sid.revision = SID_REVISION; + ace->sid.sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + sub_authorities = ace->sid.sub_authority; + *sub_authorities++ = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + *sub_authorities = const_cpu_to_le32(DOMAIN_ALIAS_RID_USERS); + + //owner sid + sid = (SID*)((char*)sd + le32_to_cpu(sd->owner)); + sid->revision = 0x01; + sid->sub_authority_count = 0x01; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + sid->identifier_authority.value[0] = 0; + sid->identifier_authority.value[1] = 0; + sid->identifier_authority.value[2] = 0; + sid->identifier_authority.value[3] = 0; + sid->identifier_authority.value[4] = 0; + sid->identifier_authority.value[5] = 5; + sid->sub_authority[0] = const_cpu_to_le32(SECURITY_LOCAL_SYSTEM_RID); + + //group sid + sid = (SID*)((char*)sd + le32_to_cpu(sd->group)); + sid->revision = 0x01; + sid->sub_authority_count = 0x01; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + sid->identifier_authority.value[0] = 0; + sid->identifier_authority.value[1] = 0; + sid->identifier_authority.value[2] = 0; + sid->identifier_authority.value[3] = 0; + sid->identifier_authority.value[4] = 0; + sid->identifier_authority.value[5] = 5; + sid->sub_authority[0] = const_cpu_to_le32(SECURITY_LOCAL_SYSTEM_RID); +} + +/** + * init_secure_sds - + * + * NTFS 3.1 - System files security decriptors + * =========================================== + * Create the security descriptor entries in $SDS data stream like they + * are in a partition, newly formatted with windows 2003 + */ +void init_secure_sds(char *sd_val) +{ + SECURITY_DESCRIPTOR_HEADER *sds; + SECURITY_DESCRIPTOR_RELATIVE *sd; + ACL *acl; + ACCESS_ALLOWED_ACE *ace; + SID *sid; + +/* + * security descriptor #1 + */ + //header + sds = (SECURITY_DESCRIPTOR_HEADER*)((char*)sd_val); + sds->hash = const_cpu_to_le32(0xF80312F0); + sds->security_id = const_cpu_to_le32(0x0100); + sds->offset = const_cpu_to_le64(0x00); + sds->length = const_cpu_to_le32(0x7C); + //security descriptor relative + sd = (SECURITY_DESCRIPTOR_RELATIVE*)((char*)sds + + sizeof(SECURITY_DESCRIPTOR_HEADER)); + sd->revision = 0x01; + sd->alignment = 0x00; + sd->control = SE_SELF_RELATIVE | SE_DACL_PRESENT; + sd->owner = const_cpu_to_le32(0x48); + sd->group = const_cpu_to_le32(0x58); + sd->sacl = const_cpu_to_le32(0x00); + sd->dacl = const_cpu_to_le32(0x14); + + //acl + acl = (ACL*)((char*)sd + sizeof(SECURITY_DESCRIPTOR_RELATIVE)); + acl->revision = 0x02; + acl->alignment1 = 0x00; + acl->size = const_cpu_to_le16(0x34); + acl->ace_count = const_cpu_to_le16(0x02); + acl->alignment2 = const_cpu_to_le16(0x00); + + //ace1 + ace = (ACCESS_ALLOWED_ACE*)((char*)acl + sizeof(ACL)); + ace->type = 0x00; + ace->flags = 0x00; + ace->size = const_cpu_to_le16(0x14); + ace->mask = const_cpu_to_le32(0x120089); + ace->sid.revision = 0x01; + ace->sid.sub_authority_count = 0x01; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + ace->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_LOCAL_SYSTEM_RID); + //ace2 + ace = (ACCESS_ALLOWED_ACE*)((char*)ace + le16_to_cpu(ace->size)); + ace->type = 0x00; + ace->flags = 0x00; + ace->size = const_cpu_to_le16(0x18); + ace->mask = const_cpu_to_le32(0x120089); + ace->sid.revision = 0x01; + ace->sid.sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + ace->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + ace->sid.sub_authority[1] = + const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + + //owner sid + sid = (SID*)((char*)sd + le32_to_cpu(sd->owner)); + sid->revision = 0x01; + sid->sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + sid->identifier_authority.value[0] = 0; + sid->identifier_authority.value[1] = 0; + sid->identifier_authority.value[2] = 0; + sid->identifier_authority.value[3] = 0; + sid->identifier_authority.value[4] = 0; + sid->identifier_authority.value[5] = 5; + sid->sub_authority[0] = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + sid->sub_authority[1] = + const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + //group sid + sid = (SID*)((char*)sd + le32_to_cpu(sd->group)); + sid->revision = 0x01; + sid->sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + sid->identifier_authority.value[0] = 0; + sid->identifier_authority.value[1] = 0; + sid->identifier_authority.value[2] = 0; + sid->identifier_authority.value[3] = 0; + sid->identifier_authority.value[4] = 0; + sid->identifier_authority.value[5] = 5; + sid->sub_authority[0] = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + sid->sub_authority[1] = + const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); +/* + * security descriptor #2 + */ + //header + sds = (SECURITY_DESCRIPTOR_HEADER*)((char*)sd_val + 0x80); + sds->hash = const_cpu_to_le32(0xB32451); + sds->security_id = const_cpu_to_le32(0x0101); + sds->offset = const_cpu_to_le64(0x80); + sds->length = const_cpu_to_le32(0x7C); + + //security descriptor relative + sd = (SECURITY_DESCRIPTOR_RELATIVE*)((char*)sds + + sizeof(SECURITY_DESCRIPTOR_HEADER)); + sd->revision = 0x01; + sd->alignment = 0x00; + sd->control = SE_SELF_RELATIVE | SE_DACL_PRESENT; + sd->owner = const_cpu_to_le32(0x48); + sd->group = const_cpu_to_le32(0x58); + sd->sacl = const_cpu_to_le32(0x00); + sd->dacl = const_cpu_to_le32(0x14); + + //acl + acl = (ACL*)((char*)sd + sizeof(SECURITY_DESCRIPTOR_RELATIVE)); + acl->revision = 0x02; + acl->alignment1 = 0x00; + acl->size = const_cpu_to_le16(0x34); + acl->ace_count = const_cpu_to_le16(0x02); + acl->alignment2 = const_cpu_to_le16(0x00); + + //ace1 + ace = (ACCESS_ALLOWED_ACE*)((char*)acl + sizeof(ACL)); + ace->type = 0x00; + ace->flags = 0x00; + ace->size = const_cpu_to_le16(0x14); + ace->mask = const_cpu_to_le32(0x12019F); + ace->sid.revision = 0x01; + ace->sid.sub_authority_count = 0x01; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + ace->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_LOCAL_SYSTEM_RID); + //ace2 + ace = (ACCESS_ALLOWED_ACE*)((char*)ace + le16_to_cpu(ace->size)); + ace->type = 0x00; + ace->flags = 0x00; + ace->size = const_cpu_to_le16(0x18); + ace->mask = const_cpu_to_le32(0x12019F); + ace->sid.revision = 0x01; + ace->sid.sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + ace->sid.identifier_authority.value[0] = 0; + ace->sid.identifier_authority.value[1] = 0; + ace->sid.identifier_authority.value[2] = 0; + ace->sid.identifier_authority.value[3] = 0; + ace->sid.identifier_authority.value[4] = 0; + ace->sid.identifier_authority.value[5] = 5; + ace->sid.sub_authority[0] = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + ace->sid.sub_authority[1] = + const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + + //owner sid + sid = (SID*)((char*)sd + le32_to_cpu(sd->owner)); + sid->revision = 0x01; + sid->sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + sid->identifier_authority.value[0] = 0; + sid->identifier_authority.value[1] = 0; + sid->identifier_authority.value[2] = 0; + sid->identifier_authority.value[3] = 0; + sid->identifier_authority.value[4] = 0; + sid->identifier_authority.value[5] = 5; + sid->sub_authority[0] = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + sid->sub_authority[1] = + const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + + //group sid + sid = (SID*)((char*)sd + le32_to_cpu(sd->group)); + sid->revision = 0x01; + sid->sub_authority_count = 0x02; + /* SECURITY_NT_SID_AUTHORITY (S-1-5) */ + sid->identifier_authority.value[0] = 0; + sid->identifier_authority.value[1] = 0; + sid->identifier_authority.value[2] = 0; + sid->identifier_authority.value[3] = 0; + sid->identifier_authority.value[4] = 0; + sid->identifier_authority.value[5] = 5; + sid->sub_authority[0] = + const_cpu_to_le32(SECURITY_BUILTIN_DOMAIN_RID); + sid->sub_authority[1] = + const_cpu_to_le32(DOMAIN_ALIAS_RID_ADMINS); + + return; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/sd.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/sd.h new file mode 100644 index 0000000..7ad3e6a --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/sd.h @@ -0,0 +1,11 @@ +#ifndef _NTFS_SD_H_ +#define _NTFS_SD_H_ + +#include "types.h" + +void init_system_file_sd(int sys_file_no, u8 **sd_val, int *sd_val_len); +void init_root_sd(u8 **sd_val, int *sd_val_len); +void init_secure_sds(char *sd_val); + +#endif /* _NTFS_SD_H_ */ + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/utils.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/utils.c new file mode 100644 index 0000000..8f56cac --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/utils.c @@ -0,0 +1,1236 @@ +/** + * utils.c - Part of the Linux-NTFS project. + * + * Copyright (c) 2002-2005 Richard Russon + * Copyright (c) 2003-2006 Anton Altaparmakov + * Copyright (c) 2003 Lode Leroy + * Copyright (c) 2005-2007 Yura Pakhuchiy + * Copyright (c) 2014 Jean-Pierre Andre + * + * A set of shared functions for ntfs utilities + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_STAT_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_LOCALE_H +#include +#endif +#ifdef HAVE_LIBINTL_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_LIMITS_H +#include +#endif +#ifdef HAVE_CTYPE_H +#include +#endif + +#include "utils.h" +#include "types.h" +#include "volume.h" +#include "debug.h" +#include "dir.h" +/* #include "version.h" */ +#include "logging.h" +#include "misc.h" + +const char *ntfs_bugs = "Developers' email address: " NTFS_DEV_LIST "\n"; +const char *ntfs_gpl = "This program is free software, released under the GNU " + "General Public License\nand you are welcome to redistribute it under " + "certain conditions. It comes with\nABSOLUTELY NO WARRANTY; for " + "details read the GNU General Public License to be\nfound in the file " + "\"COPYING\" distributed with this program, or online at:\n" + "http://www.gnu.org/copyleft/gpl.html\n"; + +static const char *invalid_ntfs_msg = +"The device '%s' doesn't have a valid NTFS.\n" +"Maybe you selected the wrong device? Or the whole disk instead of a\n" +"partition (e.g. /dev/hda, not /dev/hda1)? Or the other way around?\n"; + +static const char *corrupt_volume_msg = +"NTFS is inconsistent. Run chkdsk /f on Windows then reboot it TWICE!\n" +"The usage of the /f parameter is very IMPORTANT! No modification was\n" +"made to NTFS by this software.\n"; + +static const char *hibernated_volume_msg = +"The NTFS partition is hibernated. Please resume Windows and turn it \n" +"off properly, so mounting could be done safely.\n"; + +static const char *unclean_journal_msg = +"Access is denied because the NTFS journal file is unclean. Choices are:\n" +" A) Shutdown Windows properly.\n" +" B) Click the 'Safely Remove Hardware' icon in the Windows taskbar\n" +" notification area before disconnecting the device.\n" +" C) Use 'Eject' from Windows Explorer to safely remove the device.\n" +" D) If you ran chkdsk previously then boot Windows again which will\n" +" automatically initialize the journal.\n" +" E) Submit 'force' option (WARNING: This solution it not recommended).\n" +" F) ntfsmount: Mount the volume read-only by using the 'ro' mount option.\n"; + +static const char *opened_volume_msg = +"Access is denied because the NTFS volume is already exclusively opened.\n" +"The volume may be already mounted, or another software may use it which\n" +"could be identified for example by the help of the 'fuser' command.\n"; + +static const char *dirty_volume_msg = +"Volume is scheduled for check.\n" +"Please boot into Windows TWICE, or use the 'force' option.\n" +"NOTE: If you had not scheduled check and last time accessed this volume\n" +"using ntfsmount and shutdown system properly, then init scripts in your\n" +"distribution are broken. Please report to your distribution developers\n" +"(NOT to us!) that init scripts kill ntfsmount or mount.ntfs-fuse during\n" +"shutdown instead of proper umount.\n"; + +static const char *fakeraid_msg = +"You seem to have a SoftRAID/FakeRAID hardware and must use an activated,\n" +"different device under /dev/mapper, (e.g. /dev/mapper/nvidia_eahaabcc1)\n" +"to mount NTFS. Please see the 'dmraid' documentation for help.\n"; + +/** + * utils_set_locale + */ +int utils_set_locale(void) +{ + const char *locale; + + locale = setlocale(LC_ALL, ""); + if (!locale) { + locale = setlocale(LC_ALL, NULL); + ntfs_log_error("Failed to set locale, using default '%s'.\n", + locale); + return 1; + } else { + return 0; + } +} + +/** + * linux-ntfs's ntfs_mbstoucs has different semantics, so we emulate it with + * ntfs-3g's. + */ +int ntfs_mbstoucs_libntfscompat(const char *ins, + ntfschar **outs, int outs_len) +{ + if(!outs) { + errno = EINVAL; + return -1; + } + else if(*outs != NULL) { + /* Note: libntfs's mbstoucs implementation allows the caller to + * specify a preallocated buffer while libntfs-3g's always + * allocates the output buffer. + */ + ntfschar *tmpstr = NULL; + int tmpstr_len; + + tmpstr_len = ntfs_mbstoucs(ins, &tmpstr); + if(tmpstr_len >= 0) { + if((tmpstr_len + 1) > outs_len) { + /* Doing a realloc instead of reusing tmpstr + * because it emulates libntfs's mbstoucs more + * closely. */ + ntfschar *re_outs = realloc(*outs, + sizeof(ntfschar)*(tmpstr_len + 1)); + if(!re_outs) + tmpstr_len = -1; + else + *outs = re_outs; + } + + if(tmpstr_len >= 0) { + /* The extra character is the \0 terminator. */ + memcpy(*outs, tmpstr, + sizeof(ntfschar)*(tmpstr_len + 1)); + } + + free(tmpstr); + } + + return tmpstr_len; + } + else + return ntfs_mbstoucs(ins, outs); +} + +/** + * utils_valid_device - Perform some safety checks on the device, before start + * @name: Full pathname of the device/file to work with + * @force: Continue regardless of problems + * + * Check that the name refers to a device and that is isn't already mounted. + * These checks can be overridden by using the force option. + * + * Return: 1 Success, we can continue + * 0 Error, we cannot use this device + */ +int utils_valid_device(const char *name, int force) +{ + unsigned long mnt_flags = 0; + struct stat st; + +#if defined(HAVE_WINDOWS_H) | defined(__CYGWIN32__) + /* FIXME: This doesn't work for Cygwin, so just return success. */ + return 1; +#endif + if (!name) { + errno = EINVAL; + return 0; + } + + if (stat(name, &st) == -1) { + if (errno == ENOENT) + ntfs_log_error("The device %s doesn't exist\n", name); + else + ntfs_log_perror("Error getting information about %s", + name); + return 0; + } + + /* Make sure the file system is not mounted. */ + if (ntfs_check_if_mounted(name, &mnt_flags)) { + ntfs_log_perror("Failed to determine whether %s is mounted", + name); + if (!force) { + ntfs_log_error("Use the force option to ignore this " + "error.\n"); + return 0; + } + ntfs_log_warning("Forced to continue.\n"); + } else if (mnt_flags & NTFS_MF_MOUNTED) { + if (!force) { + ntfs_log_error("%s", opened_volume_msg); + ntfs_log_error("You can use force option to avoid this " + "check, but this is not recommended\n" + "and may lead to data corruption.\n"); + return 0; + } + ntfs_log_warning("Forced to continue.\n"); + } + + return 1; +} + +/** + * utils_mount_volume - Mount an NTFS volume + */ +ntfs_volume * utils_mount_volume(const char *device, unsigned long flags) +{ + ntfs_volume *vol; + + if (!device) { + errno = EINVAL; + return NULL; + } + + /* Porting notes: + * + * libntfs-3g does not have the 'force' flag in ntfs_mount_flags. + * The 'force' flag in libntfs bypasses two safety checks when mounting + * read/write: + * 1. Do not mount when the VOLUME_IS_DIRTY flag in + * VOLUME_INFORMATION is set. + * 2. Do not mount when the logfile is unclean. + * + * libntfs-3g only has safety check number 2. The dirty flag is simply + * ignored because we are confident that we can handle a dirty volume. + * So we treat NTFS_MNT_RECOVER like NTFS_MNT_FORCE, knowing that the + * first check is always bypassed. + */ + + if (!utils_valid_device(device, flags & NTFS_MNT_RECOVER)) + return NULL; + + vol = ntfs_mount(device, flags); + if (!vol) { + ntfs_log_perror("Failed to mount '%s'", device); + if (errno == EINVAL) + ntfs_log_error(invalid_ntfs_msg, device); + else if (errno == EIO) + ntfs_log_error("%s", corrupt_volume_msg); + else if (errno == EPERM) + ntfs_log_error("%s", hibernated_volume_msg); + else if (errno == EOPNOTSUPP) + ntfs_log_error("%s", unclean_journal_msg); + else if (errno == EBUSY) + ntfs_log_error("%s", opened_volume_msg); + else if (errno == ENXIO) + ntfs_log_error("%s", fakeraid_msg); + return NULL; + } + + /* Porting notes: + * libntfs-3g does not record whether the volume log file was dirty + * before mount, so we can only warn if the VOLUME_IS_DIRTY flag is set + * in VOLUME_INFORMATION. */ + if (vol->flags & VOLUME_IS_DIRTY) { + if (!(flags & NTFS_MNT_RECOVER)) { + ntfs_log_error("%s", dirty_volume_msg); + ntfs_umount(vol, FALSE); + return NULL; + } + ntfs_log_error("WARNING: Dirty volume mount was forced by the " + "'force' mount option.\n"); + } + return vol; +} + +/** + * utils_parse_size - Convert a string representing a size + * @value: String to be parsed + * @size: Parsed size + * @scale: Whether or not to allow a suffix to scale the value + * + * Read a string and convert it to a number. Strings may be suffixed to scale + * them. Any number without a suffix is assumed to be in bytes. + * + * Suffix Description Multiple + * [tT] Terabytes 10^12 + * [gG] Gigabytes 10^9 + * [mM] Megabytes 10^6 + * [kK] Kilobytes 10^3 + * + * Notes: + * Only the first character of the suffix is read. + * The multipliers are decimal thousands, not binary: 1000, not 1024. + * If parse_size fails, @size will not be changed + * + * Return: 1 Success + * 0 Error, the string was malformed + */ +int utils_parse_size(const char *value, s64 *size, BOOL scale) +{ + long long result; + char *suffix = NULL; + + if (!value || !size) { + errno = EINVAL; + return 0; + } + + ntfs_log_debug("Parsing size '%s'.\n", value); + + result = strtoll(value, &suffix, 0); + if (result < 0 || errno == ERANGE) { + ntfs_log_error("Invalid size '%s'.\n", value); + return 0; + } + + if (!suffix) { + ntfs_log_error("Internal error, strtoll didn't return a suffix.\n"); + return 0; + } + + if (scale) { + switch (suffix[0]) { + case 't': case 'T': result *= 1000; + /* FALLTHRU */ + case 'g': case 'G': result *= 1000; + /* FALLTHRU */ + case 'm': case 'M': result *= 1000; + /* FALLTHRU */ + case 'k': case 'K': result *= 1000; + /* FALLTHRU */ + case '-': case 0: + break; + default: + ntfs_log_error("Invalid size suffix '%s'. Use T, G, M, or K.\n", suffix); + return 0; + } + } else { + if ((suffix[0] != '-') && (suffix[0] != 0)) { + ntfs_log_error("Invalid number '%.*s'.\n", (int)(suffix - value + 1), value); + return 0; + } + } + + ntfs_log_debug("Parsed size = %lld.\n", result); + *size = result; + return 1; +} + +/** + * utils_parse_range - Convert a string representing a range of numbers + * @string: The string to be parsed + * @start: The beginning of the range will be stored here + * @finish: The end of the range will be stored here + * + * Read a string of the form n-m. If the lower end is missing, zero will be + * substituted. If the upper end is missing LONG_MAX will be used. If the + * string cannot be parsed correctly, @start and @finish will not be changed. + * + * Return: 1 Success, a valid string was found + * 0 Error, the string was not a valid range + */ +int utils_parse_range(const char *string, s64 *start, s64 *finish, BOOL scale) +{ + s64 a, b; + char *middle; + + if (!string || !start || !finish) { + errno = EINVAL; + return 0; + } + + middle = strchr(string, '-'); + if (string == middle) { + ntfs_log_debug("Range has no beginning, defaulting to 0.\n"); + a = 0; + } else { + if (!utils_parse_size(string, &a, scale)) + return 0; + } + + if (middle) { + if (middle[1] == 0) { + b = LONG_MAX; // XXX ULLONG_MAX + ntfs_log_debug("Range has no end, defaulting to " + "%lld.\n", (long long)b); + } else { + if (!utils_parse_size(middle+1, &b, scale)) + return 0; + } + } else { + b = a; + } + + ntfs_log_debug("Range '%s' = %lld - %lld\n", string, (long long)a, + (long long)b); + + *start = a; + *finish = b; + return 1; +} + +/** + * find_attribute - Find an attribute of the given type + * @type: An attribute type, e.g. AT_FILE_NAME + * @ctx: A search context, created using ntfs_get_attr_search_ctx + * + * Using the search context to keep track, find the first/next occurrence of a + * given attribute type. + * + * N.B. This will return a pointer into @mft. As long as the search context + * has been created without an inode, it won't overflow the buffer. + * + * Return: Pointer Success, an attribute was found + * NULL Error, no matching attributes were found + */ +ATTR_RECORD * find_attribute(const ATTR_TYPES type, ntfs_attr_search_ctx *ctx) +{ + if (!ctx) { + errno = EINVAL; + return NULL; + } + + if (ntfs_attr_lookup(type, NULL, 0, 0, 0, NULL, 0, ctx) != 0) { + ntfs_log_debug("find_attribute didn't find an attribute of type: 0x%02x.\n", le32_to_cpu(type)); + return NULL; /* None / no more of that type */ + } + + ntfs_log_debug("find_attribute found an attribute of type: 0x%02x.\n", le32_to_cpu(type)); + return ctx->attr; +} + +/** + * find_first_attribute - Find the first attribute of a given type + * @type: An attribute type, e.g. AT_FILE_NAME + * @mft: A buffer containing a raw MFT record + * + * Search through a raw MFT record for an attribute of a given type. + * The return value is a pointer into the MFT record that was supplied. + * + * N.B. This will return a pointer into @mft. The pointer won't stray outside + * the buffer, since we created the search context without an inode. + * + * Return: Pointer Success, an attribute was found + * NULL Error, no matching attributes were found + */ +ATTR_RECORD * find_first_attribute(const ATTR_TYPES type, MFT_RECORD *mft) +{ + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *rec; + + if (!mft) { + errno = EINVAL; + return NULL; + } + + ctx = ntfs_attr_get_search_ctx(NULL, mft); + if (!ctx) { + ntfs_log_error("Couldn't create a search context.\n"); + return NULL; + } + + rec = find_attribute(type, ctx); + ntfs_attr_put_search_ctx(ctx); + if (rec) + ntfs_log_debug("find_first_attribute: found attr of type 0x%02x.\n", le32_to_cpu(type)); + else + ntfs_log_debug("find_first_attribute: didn't find attr of type 0x%02x.\n", le32_to_cpu(type)); + return rec; +} + +/** + * utils_inode_get_name + * + * using inode + * get filename + * add name to list + * get parent + * if parent is 5 (/) stop + * get inode of parent + */ +#define max_path 20 +int utils_inode_get_name(ntfs_inode *inode, char *buffer, int bufsize) +{ + // XXX option: names = posix/win32 or dos + // flags: path, filename, or both + + + ntfs_volume *vol; + ntfs_attr_search_ctx *ctx; + ATTR_RECORD *rec; + FILE_NAME_ATTR *attr; + int name_space; + MFT_REF parent = FILE_root; + char *names[max_path + 1];// XXX ntfs_malloc? and make max bigger? + int i, len, offset = 0; + + if (!inode || !buffer) { + errno = EINVAL; + return 0; + } + + vol = inode->vol; + + //ntfs_log_debug("sizeof(char*) = %d, sizeof(names) = %d\n", sizeof(char*), sizeof(names)); + memset(names, 0, sizeof(names)); + + for (i = 0; i < max_path; i++) { + + ctx = ntfs_attr_get_search_ctx(inode, NULL); + if (!ctx) { + ntfs_log_error("Couldn't create a search context.\n"); + return 0; + } + + //ntfs_log_debug("i = %d, inode = %p (%lld)\n", i, inode, inode->mft_no); + + name_space = 4; + while ((rec = find_attribute(AT_FILE_NAME, ctx))) { + /* We know this will always be resident. */ + attr = (FILE_NAME_ATTR *) ((char *) rec + le16_to_cpu(rec->value_offset)); + + if (attr->file_name_type > name_space) { //XXX find the ... + continue; + } + + name_space = attr->file_name_type; + parent = le64_to_cpu(attr->parent_directory); + + if (names[i]) { + free(names[i]); + names[i] = NULL; + } + + if (ntfs_ucstombs(attr->file_name, attr->file_name_length, + &names[i], 0) < 0) { + char *temp; + ntfs_log_error("Couldn't translate filename to current locale.\n"); + temp = ntfs_malloc(30); + if (!temp) + return 0; + snprintf(temp, 30, "", (unsigned + long long)inode->mft_no); + names[i] = temp; + } + + //ntfs_log_debug("names[%d] %s\n", i, names[i]); + //ntfs_log_debug("parent = %lld\n", MREF(parent)); + } + + ntfs_attr_put_search_ctx(ctx); + + if (i > 0) /* Don't close the original inode */ + ntfs_inode_close(inode); + + if (MREF(parent) == FILE_root) { /* The root directory, stop. */ + //ntfs_log_debug("inode 5\n"); + break; + } + + inode = ntfs_inode_open(vol, parent); + if (!inode) { + ntfs_log_error("Couldn't open inode %llu.\n", + (unsigned long long)MREF(parent)); + break; + } + } + + if (i >= max_path) { + /* If we get into an infinite loop, we'll end up here. */ + ntfs_log_error("The directory structure is too deep (over %d) nested directories.\n", max_path); + return 0; + } + + /* Assemble the names in the correct order. */ + for (i = max_path; i >= 0; i--) { + if (!names[i]) + continue; + + len = snprintf(buffer + offset, bufsize - offset, "%c%s", PATH_SEP, names[i]); + if (len >= (bufsize - offset)) { + ntfs_log_error("Pathname was truncated.\n"); + break; + } + + offset += len; + } + + /* Free all the allocated memory */ + for (i = 0; i < max_path; i++) + free(names[i]); + + ntfs_log_debug("Pathname: %s\n", buffer); + + return 1; +} +#undef max_path + +/** + * utils_attr_get_name + */ +int utils_attr_get_name(ntfs_volume *vol, ATTR_RECORD *attr, char *buffer, int bufsize) +{ + int len, namelen; + char *name; + ATTR_DEF *attrdef; + + // flags: attr, name, or both + if (!attr || !buffer) { + errno = EINVAL; + return 0; + } + + attrdef = ntfs_attr_find_in_attrdef(vol, attr->type); + if (attrdef) { + name = NULL; + namelen = ntfs_ucsnlen(attrdef->name, sizeof(attrdef->name)); + if (ntfs_ucstombs(attrdef->name, namelen, &name, 0) < 0) { + ntfs_log_error("Couldn't translate attribute type to " + "current locale.\n"); + // ? + return 0; + } + len = snprintf(buffer, bufsize, "%s", name); + } else { + ntfs_log_error("Unknown attribute type 0x%02x\n", le32_to_cpu(attr->type)); + len = snprintf(buffer, bufsize, ""); + } + + if (len >= bufsize) { + ntfs_log_error("Attribute type was truncated.\n"); + return 0; + } + + if (!attr->name_length) { + return 0; + } + + buffer += len; + bufsize -= len; + + name = NULL; + namelen = attr->name_length; + if (ntfs_ucstombs((ntfschar *)((char *)attr + + le16_to_cpu(attr->name_offset)), + namelen, &name, 0) < 0) { + ntfs_log_error("Couldn't translate attribute name to current " + "locale.\n"); + // ? + len = snprintf(buffer, bufsize, ""); + return 0; + } + + len = snprintf(buffer, bufsize, "(%s)", name); + free(name); + + if (len >= bufsize) { + ntfs_log_error("Attribute name was truncated.\n"); + return 0; + } + + return 0; +} + +/** + * utils_cluster_in_use - Determine if a cluster is in use + * @vol: An ntfs volume obtained from ntfs_mount + * @lcn: The Logical Cluster Number to test + * + * The metadata file $Bitmap has one binary bit representing each cluster on + * disk. The bit will be set for each cluster that is in use. The function + * reads the relevant part of $Bitmap into a buffer and tests the bit. + * + * This function has a static buffer in which it caches a section of $Bitmap. + * If the lcn, being tested, lies outside the range, the buffer will be + * refreshed. @bmplcn stores offset to the first bit (in bits) stored in the + * buffer. + * + * NOTE: Be very carefull with shifts by 3 everywhere in this function. + * + * Return: 1 Cluster is in use + * 0 Cluster is free space + * -1 Error occurred + */ +int utils_cluster_in_use(ntfs_volume *vol, long long lcn) +{ + static unsigned char buffer[512]; + static long long bmplcn = -(sizeof(buffer) << 3); + int byte, bit; + ntfs_attr *attr; + + if (!vol) { + errno = EINVAL; + return -1; + } + + /* Does lcn lie in the section of $Bitmap we already have cached? */ + if ((lcn < bmplcn) + || (lcn >= (long long)(bmplcn + (sizeof(buffer) << 3)))) { + ntfs_log_debug("Bit lies outside cache.\n"); + attr = ntfs_attr_open(vol->lcnbmp_ni, AT_DATA, AT_UNNAMED, 0); + if (!attr) { + ntfs_log_perror("Couldn't open $Bitmap"); + return -1; + } + + /* Mark the buffer as in use, in case the read is shorter. */ + memset(buffer, 0xFF, sizeof(buffer)); + bmplcn = lcn & (~((sizeof(buffer) << 3) - 1)); + + if (ntfs_attr_pread(attr, (bmplcn >> 3), sizeof(buffer), + buffer) < 0) { + ntfs_log_perror("Couldn't read $Bitmap"); + ntfs_attr_close(attr); + return -1; + } + + ntfs_log_debug("Reloaded bitmap buffer.\n"); + ntfs_attr_close(attr); + } + + bit = 1 << (lcn & 7); + byte = (lcn >> 3) & (sizeof(buffer) - 1); + ntfs_log_debug("cluster = %lld, bmplcn = %lld, byte = %d, bit = %d, " + "in use %d\n", lcn, bmplcn, byte, bit, buffer[byte] & + bit); + + return (buffer[byte] & bit); +} + +/** + * utils_mftrec_in_use - Determine if a MFT Record is in use + * @vol: An ntfs volume obtained from ntfs_mount + * @mref: MFT Reference (inode number) + * + * The metadata file $BITMAP has one binary bit representing each record in the + * MFT. The bit will be set for each record that is in use. The function + * reads the relevant part of $BITMAP into a buffer and tests the bit. + * + * This function has a static buffer in which it caches a section of $BITMAP. + * If the mref, being tested, lies outside the range, the buffer will be + * refreshed. + * + * Return: 1 MFT Record is in use + * 0 MFT Record is unused + * -1 Error occurred + */ +int utils_mftrec_in_use(ntfs_volume *vol, MFT_REF mref) +{ + static u8 buffer[512]; + static s64 bmpmref = -(sizeof(buffer) << 3) - 1; /* Which bit of $BITMAP is in the buffer */ + int byte, bit; + + ntfs_log_trace("Entering.\n"); + + if (!vol) { + errno = EINVAL; + return -1; + } + + /* Does mref lie in the section of $Bitmap we already have cached? */ + if (((s64)MREF(mref) < bmpmref) + || ((s64)MREF(mref) >= (s64)(bmpmref + (sizeof(buffer) << 3)))) { + ntfs_log_debug("Bit lies outside cache.\n"); + + /* Mark the buffer as not in use, in case the read is shorter. */ + memset(buffer, 0, sizeof(buffer)); + bmpmref = mref & (~((sizeof(buffer) << 3) - 1)); + + if (ntfs_attr_pread(vol->mftbmp_na, (bmpmref>>3), sizeof(buffer), buffer) < 0) { + ntfs_log_perror("Couldn't read $MFT/$BITMAP"); + return -1; + } + + ntfs_log_debug("Reloaded bitmap buffer.\n"); + } + + bit = 1 << (mref & 7); + byte = (mref >> 3) & (sizeof(buffer) - 1); + ntfs_log_debug("cluster = %lld, bmpmref = %lld, byte = %d, bit = %d, " + "in use %d\n", (long long) mref, (long long) bmpmref, + byte, bit, buffer[byte] & bit); + + return (buffer[byte] & bit); +} + +/** + * __metadata + */ +static int __metadata(ntfs_volume *vol, u64 num) +{ + if (num <= FILE_UpCase) + return 1; + if (!vol) + return -1; + if ((vol->major_ver == 3) && (num == FILE_Extend)) + return 1; + + return 0; +} + +/** + * utils_is_metadata - Determine if an inode represents a metadata file + * @inode: An ntfs inode to be tested + * + * A handful of files in the volume contain filesystem data - metadata. + * They can be identified by their inode number (offset in MFT/$DATA) or by + * their parent. + * + * Return: 1 inode is a metadata file + * 0 inode is not a metadata file + * -1 Error occurred + */ +int utils_is_metadata(ntfs_inode *inode) +{ + ntfs_volume *vol; + ATTR_RECORD *rec; + FILE_NAME_ATTR *attr; + MFT_RECORD *file; + u64 num; + + if (!inode) { + errno = EINVAL; + return -1; + } + + vol = inode->vol; + if (!vol) + return -1; + + num = inode->mft_no; + if (__metadata(vol, num) == 1) + return 1; + + file = inode->mrec; + if (file && (file->base_mft_record != 0)) { + num = MREF_LE(file->base_mft_record); + if (__metadata(vol, num) == 1) + return 1; + } + + rec = find_first_attribute(AT_FILE_NAME, inode->mrec); + if (!rec) + return -1; + + /* We know this will always be resident. */ + attr = (FILE_NAME_ATTR *)((char *)rec + le16_to_cpu(rec->value_offset)); + + num = MREF_LE(attr->parent_directory); + if ((num != FILE_root) && (__metadata(vol, num) == 1)) + return 1; + + return 0; +} + +/** + * utils_dump_mem - Display a block of memory in hex and ascii + * @buf: Buffer to be displayed + * @start: Offset into @buf to start from + * @length: Number of bytes to display + * @flags: Options to change the style of the output + * + * Display a block of memory in a tradition hex-dump manner. + * Optionally the ascii part can be turned off. + * + * The flags, described fully in utils.h, default to 0 (DM_DEFAULTS). + * Examples are: DM_INDENT (indent the output by one tab); DM_RED (colour the + * output); DM_NO_ASCII (only print the hex values). + */ +void utils_dump_mem(void *buf, int start, int length, int flags) +{ + int off, i, s, e, col; + u8 *mem = buf; + + s = start & ~15; // round down + e = (start + length + 15) & ~15; // round up + + for (off = s; off < e; off += 16) { + col = 30; + if (flags & DM_RED) + col += 1; + if (flags & DM_GREEN) + col += 2; + if (flags & DM_BLUE) + col += 4; + if (flags & DM_INDENT) + ntfs_log_debug("\t"); + if (flags & DM_BOLD) + ntfs_log_debug("\e[01m"); + if (flags & (DM_RED | DM_BLUE | DM_GREEN | DM_BOLD)) + ntfs_log_debug("\e[%dm", col); + if (off == s) + ntfs_log_debug("%6.6x ", start); + else + ntfs_log_debug("%6.6x ", off); + + for (i = 0; i < 16; i++) { + if ((i == 8) && (!(flags & DM_NO_DIVIDER))) + ntfs_log_debug(" -"); + if (((off+i) >= start) && ((off+i) < (start+length))) + ntfs_log_debug(" %02X", mem[off+i]); + else + ntfs_log_debug(" "); + } + if (!(flags & DM_NO_ASCII)) { + ntfs_log_debug(" "); + for (i = 0; i < 16; i++) { + if (((off+i) < start) || ((off+i) >= (start+length))) + ntfs_log_debug(" "); + else if (isprint(mem[off + i])) + ntfs_log_debug("%c", mem[off + i]); + else + ntfs_log_debug("."); + } + } + if (flags & (DM_RED | DM_BLUE | DM_GREEN | DM_BOLD)) + ntfs_log_debug("\e[0m"); + ntfs_log_debug("\n"); + } +} + + +/** + * mft_get_search_ctx + */ +struct mft_search_ctx * mft_get_search_ctx(ntfs_volume *vol) +{ + struct mft_search_ctx *ctx; + + if (!vol) { + errno = EINVAL; + return NULL; + } + + ctx = (struct mft_search_ctx*)calloc(1, sizeof *ctx); + + ctx->mft_num = -1; + ctx->vol = vol; + + return ctx; +} + +/** + * mft_put_search_ctx + */ +void mft_put_search_ctx(struct mft_search_ctx *ctx) +{ + if (!ctx) + return; + if (ctx->inode) + ntfs_inode_close(ctx->inode); + free(ctx); +} + +/** + * mft_next_record + */ +int mft_next_record(struct mft_search_ctx *ctx) +{ + s64 nr_mft_records; + ATTR_RECORD *attr10 = NULL; + ATTR_RECORD *attr20 = NULL; + ATTR_RECORD *attr80 = NULL; + ntfs_attr_search_ctx *attr_ctx; + + if (!ctx) { + errno = EINVAL; + return -1; + } + + if (ctx->inode) { + ntfs_inode_close(ctx->inode); + ctx->inode = NULL; + } + + nr_mft_records = ctx->vol->mft_na->initialized_size >> + ctx->vol->mft_record_size_bits; + + for (ctx->mft_num++; (s64)ctx->mft_num < nr_mft_records; ctx->mft_num++) { + int in_use; + + ctx->flags_match = 0; + in_use = utils_mftrec_in_use(ctx->vol, (MFT_REF) ctx->mft_num); + if (in_use == -1) { + ntfs_log_error("Error reading inode %llu. Aborting.\n", + (unsigned long long)ctx->mft_num); + return -1; + } + + if (in_use) { + ctx->flags_match |= FEMR_IN_USE; + + ctx->inode = ntfs_inode_open(ctx->vol, (MFT_REF) ctx->mft_num); + if (ctx->inode == NULL) { + MFT_RECORD *mrec; + int r; + MFT_REF base_inode; + + mrec = (MFT_RECORD*)NULL; + r = ntfs_file_record_read(ctx->vol, + (MFT_REF) ctx->mft_num, &mrec, NULL); + if (r || !mrec || !mrec->base_mft_record) + ntfs_log_error( + "Error reading inode %lld.\n", + (long long)ctx->mft_num); + else { + base_inode = le64_to_cpu( + mrec->base_mft_record); + ntfs_log_error("Inode %lld is an " + "extent of inode %lld.\n", + (long long)ctx->mft_num, + (long long)MREF(base_inode)); + } + free (mrec); + continue; + } + + attr10 = find_first_attribute(AT_STANDARD_INFORMATION, ctx->inode->mrec); + attr20 = find_first_attribute(AT_ATTRIBUTE_LIST, ctx->inode->mrec); + attr80 = find_first_attribute(AT_DATA, ctx->inode->mrec); + + if (attr10) + ctx->flags_match |= FEMR_BASE_RECORD; + else + ctx->flags_match |= FEMR_NOT_BASE_RECORD; + + if (attr20) + ctx->flags_match |= FEMR_BASE_RECORD; + + if (attr80) + ctx->flags_match |= FEMR_FILE; + + if (ctx->flags_search & FEMR_DIR) { + attr_ctx = ntfs_attr_get_search_ctx(ctx->inode, NULL); + if (attr_ctx) { + if (ntfs_attr_lookup(AT_INDEX_ROOT, NTFS_INDEX_I30, 4, 0, 0, NULL, 0, attr_ctx) == 0) + ctx->flags_match |= FEMR_DIR; + + ntfs_attr_put_search_ctx(attr_ctx); + } else { + ntfs_log_error("Couldn't create a search context.\n"); + return -1; + } + } + + switch (utils_is_metadata(ctx->inode)) { + case 1: ctx->flags_match |= FEMR_METADATA; break; + case 0: ctx->flags_match |= FEMR_NOT_METADATA; break; + default: + ctx->flags_match |= FEMR_NOT_METADATA; break; + //ntfs_log_error("Error reading inode %lld.\n", ctx->mft_num); + //return -1; + } + + } else { // !in_use + ntfs_attr *mft; + + ctx->flags_match |= FEMR_NOT_IN_USE; + + ctx->inode = (ntfs_inode*)calloc(1, sizeof(*ctx->inode)); + if (!ctx->inode) { + ntfs_log_error("Out of memory. Aborting.\n"); + return -1; + } + + ctx->inode->mft_no = ctx->mft_num; + ctx->inode->vol = ctx->vol; + ctx->inode->mrec = ntfs_malloc(ctx->vol->mft_record_size); + if (!ctx->inode->mrec) { + free(ctx->inode); // == ntfs_inode_close + return -1; + } + + mft = ntfs_attr_open(ctx->vol->mft_ni, AT_DATA, + AT_UNNAMED, 0); + if (!mft) { + ntfs_log_perror("Couldn't open $MFT/$DATA"); + // free / close + return -1; + } + + if (ntfs_attr_pread(mft, ctx->vol->mft_record_size * ctx->mft_num, ctx->vol->mft_record_size, ctx->inode->mrec) < ctx->vol->mft_record_size) { + ntfs_log_perror("Couldn't read MFT Record %llu", + (unsigned long long) ctx->mft_num); + // free / close + ntfs_attr_close(mft); + return -1; + } + + ntfs_attr_close(mft); + } + + if (ctx->flags_match & ctx->flags_search) { + break; + } + + if (ntfs_inode_close(ctx->inode)) { + ntfs_log_error("Error closing inode %llu.\n", + (unsigned long long)ctx->mft_num); + return -errno; + } + + ctx->inode = NULL; + } + + return (ctx->inode == NULL); +} + +#ifdef HAVE_WINDOWS_H + +/* + * Translate formats for older Windows + * + * Up to Windows XP, msvcrt.dll does not support long long format + * specifications (%lld, %llx, etc). We have to translate them + * to %I64. + */ + +char *ntfs_utils_reformat(char *out, int sz, const char *fmt) +{ + const char *f; + char *p; + int i; + enum { F_INIT, F_PERCENT, F_FIRST } state; + + i = 0; + f = fmt; + p = out; + state = F_INIT; + while (*f && ((i + 3) < sz)) { + switch (state) { + case F_INIT : + if (*f == '%') + state = F_PERCENT; + *p++ = *f++; + i++; + break; + case F_PERCENT : + if (*f == 'l') { + state = F_FIRST; + f++; + } else { + if (((*f < '0') || (*f > '9')) + && (*f != '*') && (*f != '-')) + state = F_INIT; + *p++ = *f++; + i++; + } + break; + case F_FIRST : + if (*f == 'l') { + *p++ = 'I'; + *p++ = '6'; + *p++ = '4'; + f++; + i += 3; + } else { + *p++ = 'l'; + *p++ = *f++; + i += 2; + } + state = F_INIT; + break; + } + } + *p++ = 0; + return (out); +} + +/* + * Translate paths to files submitted from Windows + * + * Translate Windows directory separators to Unix ones + * + * Returns the translated path, to be freed by caller + * NULL if there was an error, with errno set + */ + +char *ntfs_utils_unix_path(const char *in) +{ + char *out; + int i; + + out = strdup(in); + if (out) { + for (i=0; in[i]; i++) + if (in[i] == '\\') + out[i] = '/'; + } else + errno = ENOMEM; + return (out); +} + +#endif diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/utils.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/utils.h new file mode 100644 index 0000000..6335924 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/ntfsprogs/utils.h @@ -0,0 +1,138 @@ +/* + * utils.h - Part of the Linux-NTFS project. + * + * Copyright (c) 2002-2005 Richard Russon + * Copyright (c) 2004 Anton Altaparmakov + * + * A set of shared functions for ntfs utilities + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the Linux-NTFS + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_UTILS_H_ +#define _NTFS_UTILS_H_ + +#include "config.h" + +#include "types.h" +#include "layout.h" +#include "volume.h" + +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_STDARG_H +#include +#endif + +extern const char *ntfs_bugs; +extern const char *ntfs_gpl; + +int utils_set_locale(void); +int utils_parse_size(const char *value, s64 *size, BOOL scale); +int utils_parse_range(const char *string, s64 *start, s64 *finish, BOOL scale); +int utils_inode_get_name(ntfs_inode *inode, char *buffer, int bufsize); +int utils_attr_get_name(ntfs_volume *vol, ATTR_RECORD *attr, char *buffer, int bufsize); +int utils_cluster_in_use(ntfs_volume *vol, long long lcn); +int utils_mftrec_in_use(ntfs_volume *vol, MFT_REF mref); +int utils_is_metadata(ntfs_inode *inode); +void utils_dump_mem(void *buf, int start, int length, int flags); + +ATTR_RECORD * find_attribute(const ATTR_TYPES type, ntfs_attr_search_ctx *ctx); +ATTR_RECORD * find_first_attribute(const ATTR_TYPES type, MFT_RECORD *mft); + +int utils_valid_device(const char *name, int force); +ntfs_volume * utils_mount_volume(const char *device, unsigned long flags); + +/** + * defines... + * if *not in use* then the other flags are ignored? + */ +#define FEMR_IN_USE (1 << 0) +#define FEMR_NOT_IN_USE (1 << 1) +#define FEMR_FILE (1 << 2) // $DATA +#define FEMR_DIR (1 << 3) // $INDEX_ROOT, "$I30" +#define FEMR_METADATA (1 << 4) +#define FEMR_NOT_METADATA (1 << 5) +#define FEMR_BASE_RECORD (1 << 6) +#define FEMR_NOT_BASE_RECORD (1 << 7) +#define FEMR_ALL_RECORDS 0xFF + +/** + * struct mft_search_ctx + */ +struct mft_search_ctx { + int flags_search; + int flags_match; + ntfs_inode *inode; + ntfs_volume *vol; + u64 mft_num; +}; + +struct mft_search_ctx * mft_get_search_ctx(ntfs_volume *vol); +void mft_put_search_ctx(struct mft_search_ctx *ctx); +int mft_next_record(struct mft_search_ctx *ctx); + +// Flags for dump mem +#define DM_DEFAULTS 0 +#define DM_NO_ASCII (1 << 0) +#define DM_NO_DIVIDER (1 << 1) +#define DM_INDENT (1 << 2) +#define DM_RED (1 << 3) +#define DM_GREEN (1 << 4) +#define DM_BLUE (1 << 5) +#define DM_BOLD (1 << 6) + +/* MAX_PATH definition was missing in ntfs-3g's headers. */ +#ifndef MAX_PATH +#define MAX_PATH 1024 +#endif + +#ifdef HAVE_WINDOWS_H +/* + * Macroes to hide the needs to translate formats on older Windows + */ +#define MAX_FMT 1536 +char *ntfs_utils_reformat(char *out, int sz, const char *fmt); +char *ntfs_utils_unix_path(const char *in); +#define ntfs_log_redirect(fn,fi,li,le,d,fmt, args...) \ + do { char _b[MAX_FMT]; ntfs_log_redirect(fn,fi,li,le,d, \ + ntfs_utils_reformat(_b,MAX_FMT,fmt), args); } while (0) +#define printf(fmt, args...) \ + do { char _b[MAX_FMT]; \ + printf(ntfs_utils_reformat(_b,MAX_FMT,fmt), args); } while (0) +#define fprintf(str, fmt, args...) \ + do { char _b[MAX_FMT]; \ + fprintf(str, ntfs_utils_reformat(_b,MAX_FMT,fmt), args); } while (0) +#define vfprintf(file, fmt, args) \ + do { char _b[MAX_FMT]; vfprintf(file, \ + ntfs_utils_reformat(_b,MAX_FMT,fmt), args); } while (0) +#endif + +/** + * linux-ntfs's ntfs_mbstoucs has different semantics, so we emulate it with + * ntfs-3g's. + */ +int ntfs_mbstoucs_libntfscompat(const char *ins, + ntfschar **outs, int outs_len); + +/* This simple utility function was missing from libntfs-3g. */ +static __inline__ ntfschar *ntfs_attr_get_name(ATTR_RECORD *attr) +{ + return (ntfschar*)((u8*)attr + le16_to_cpu(attr->name_offset)); +} + +#endif /* _NTFS_UTILS_H_ */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/Makefile.am b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/Makefile.am new file mode 100644 index 0000000..8d98408 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/Makefile.am @@ -0,0 +1,82 @@ + +EXTRA_DIST = ntfs-3g_common.h + +MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +if FUSE_INTERNAL +FUSE_CFLAGS = -I$(top_srcdir)/include/fuse-lite +FUSE_LIBS = $(top_builddir)/libfuse-lite/libfuse-lite.la +else +FUSE_CFLAGS = $(FUSE_MODULE_CFLAGS) +FUSE_LIBS = $(FUSE_MODULE_LIBS) +endif + +if !DISABLE_PLUGINS +plugindir = $(libdir)/ntfs-3g +PLUGIN_CFLAGS = -DPLUGIN_DIR=\"$(plugindir)\" +endif + +if ENABLE_NTFS_3G + +bin_PROGRAMS = ntfs-3g.probe +rootbin_PROGRAMS = ntfs-3g lowntfs-3g +rootsbin_DATA = #Create directory +man_MANS = ntfs-3g.8 ntfs-3g.probe.8 + +ntfs_3g_LDADD = $(LIBDL) $(FUSE_LIBS) $(top_builddir)/libntfs-3g/libntfs-3g.la +if REALLYSTATIC +ntfs_3g_LDFLAGS = $(AM_LDFLAGS) -all-static +endif +ntfs_3g_CFLAGS = \ + $(AM_CFLAGS) \ + -DFUSE_USE_VERSION=26 \ + $(FUSE_CFLAGS) \ + -I$(top_srcdir)/include/ntfs-3g \ + $(PLUGIN_CFLAGS) +ntfs_3g_SOURCES = ntfs-3g.c ntfs-3g_common.c + +lowntfs_3g_LDADD = $(LIBDL) $(FUSE_LIBS) $(top_builddir)/libntfs-3g/libntfs-3g.la +if REALLYSTATIC +lowntfs_3g_LDFLAGS = $(AM_LDFLAGS) -all-static +endif +lowntfs_3g_CFLAGS = \ + $(AM_CFLAGS) \ + -DFUSE_USE_VERSION=26 \ + $(FUSE_CFLAGS) \ + -I$(top_srcdir)/include/ntfs-3g \ + $(PLUGIN_CFLAGS) +lowntfs_3g_SOURCES = lowntfs-3g.c ntfs-3g_common.c + +ntfs_3g_probe_LDADD = $(top_builddir)/libntfs-3g/libntfs-3g.la +if REALLYSTATIC +ntfs_3g_probe_LDFLAGS = $(AM_LDFLAGS) -all-static +endif +ntfs_3g_probe_CFLAGS = $(AM_CFLAGS) -I$(top_srcdir)/include/ntfs-3g +ntfs_3g_probe_SOURCES = ntfs-3g.probe.c + +drivers : $(FUSE_LIBS) ntfs-3g lowntfs-3g + +install-exec-hook: +if RUN_LDCONFIG + $(LDCONFIG) +endif +if !DISABLE_PLUGINS + $(MKDIR_P) $(DESTDIR)/$(plugindir) +endif + +if ENABLE_MOUNT_HELPER +install-exec-local: install-rootbinPROGRAMS + $(MKDIR_P) "$(DESTDIR)/sbin" + $(LN_S) -f "$(rootbindir)/ntfs-3g" "$(DESTDIR)/sbin/mount.ntfs-3g" + $(LN_S) -f "$(rootbindir)/lowntfs-3g" "$(DESTDIR)/sbin/mount.lowntfs-3g" + +install-data-local: install-man8 + $(LN_S) -f ntfs-3g.8 "$(DESTDIR)$(man8dir)/mount.ntfs-3g.8" + $(LN_S) -f ntfs-3g.8 "$(DESTDIR)$(man8dir)/mount.lowntfs-3g.8" + +uninstall-local: + $(RM) -f "$(DESTDIR)$(man8dir)/mount.ntfs-3g.8" + $(RM) -f "$(DESTDIR)/sbin/mount.ntfs-3g" "$(DESTDIR)/sbin/mount.lowntfs-3g" +endif + +endif # ENABLE_NTFS_3G diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/lowntfs-3g.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/lowntfs-3g.c new file mode 100644 index 0000000..e4ff0bf --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/lowntfs-3g.c @@ -0,0 +1,4874 @@ +/** + * ntfs-3g - Third Generation NTFS Driver + * + * Copyright (c) 2005-2007 Yura Pakhuchiy + * Copyright (c) 2005 Yuval Fledel + * Copyright (c) 2006-2009 Szabolcs Szakacsits + * Copyright (c) 2007-2021 Jean-Pierre Andre + * Copyright (c) 2009 Erik Larsson + * + * This file is originated from the Linux-NTFS project. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#include +#include + +#if !defined(FUSE_VERSION) || (FUSE_VERSION < 26) +#error "***********************************************************" +#error "* *" +#error "* Compilation requires at least FUSE version 2.6.0! *" +#error "* *" +#error "***********************************************************" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_LOCALE_H +#include +#endif +#include +#ifdef HAVE_LIMITS_H +#include +#endif +#include +#include + +#ifdef HAVE_SETXATTR +#include +#endif + +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef MAJOR_IN_MKDEV +#include +#endif +#ifdef MAJOR_IN_SYSMACROS +#include +#endif + +#if defined(__APPLE__) || defined(__DARWIN__) +#include +#elif defined(__sun) && defined (__SVR4) +#include +#endif /* defined(__APPLE__) || defined(__DARWIN__), ... */ + +#ifndef FUSE_CAP_POSIX_ACL /* until defined in */ +#define FUSE_CAP_POSIX_ACL (1 << 18) +#endif /* FUSE_CAP_POSIX_ACL */ + +#include "compat.h" +#include "bitmap.h" +#include "attrib.h" +#include "inode.h" +#include "volume.h" +#include "dir.h" +#include "unistr.h" +#include "layout.h" +#include "index.h" +#include "ntfstime.h" +#include "security.h" +#include "reparse.h" +#include "ea.h" +#include "object_id.h" +#include "efs.h" +#include "logging.h" +#include "xattrs.h" +#include "misc.h" +#include "ioctl.h" +#include "plugin.h" + +#include "ntfs-3g_common.h" + +/* + * The following permission checking modes are governed by + * the LPERMSCONFIG value in param.h + */ + +/* ACLS may be checked by kernel (requires a fuse patch) or here */ +#define KERNELACLS ((LPERMSCONFIG > 6) & (LPERMSCONFIG < 10)) +/* basic permissions may be checked by kernel or here */ +#define KERNELPERMS (((LPERMSCONFIG - 1) % 6) < 3) +/* may want to use fuse/kernel cacheing */ +#define CACHEING (!(LPERMSCONFIG % 3)) + +#if KERNELACLS & !KERNELPERMS +#error "Incompatible options KERNELACLS and KERNELPERMS" +#endif + +#if !CACHEING +#define ATTR_TIMEOUT (ctx->ro ? TIMEOUT_RO : 0.0) +#define ENTRY_TIMEOUT (ctx->ro ? TIMEOUT_RO : 0.0) +#else +#if defined(__sun) && defined (__SVR4) +#define ATTR_TIMEOUT (ctx->ro ? TIMEOUT_RO : 10.0) +#define ENTRY_TIMEOUT (ctx->ro ? TIMEOUT_RO : 10.0) +#else /* defined(__sun) && defined (__SVR4) */ + /* + * FUSE cacheing is only usable with basic permissions + * checked by the kernel with external fuse >= 2.8 + */ +#if !KERNELPERMS +#warning "Fuse cacheing is only usable with basic permissions checked by kernel" +#endif +#if KERNELACLS +#define ATTR_TIMEOUT (ctx->ro ? TIMEOUT_RO : 10.0) +#define ENTRY_TIMEOUT (ctx->ro ? TIMEOUT_RO : 10.0) +#else /* KERNELACLS */ +#define ATTR_TIMEOUT (ctx->ro ? TIMEOUT_RO : \ + (ctx->vol->secure_flags & (1 << SECURITY_DEFAULT) ? 10.0 : 0.0)) +#define ENTRY_TIMEOUT (ctx->ro ? TIMEOUT_RO : \ + (ctx->vol->secure_flags & (1 << SECURITY_DEFAULT) ? 10.0 : 0.0)) +#endif /* KERNELACLS */ +#endif /* defined(__sun) && defined (__SVR4) */ +#endif /* !CACHEING */ +#define GHOSTLTH 40 /* max length of a ghost file name - see ghostformat */ + + /* sometimes the kernel cannot check access */ +#define ntfs_real_allowed_access(scx, ni, type) ntfs_allowed_access(scx, ni, type) +#if POSIXACLS & KERNELPERMS & !KERNELACLS + /* short-circuit if PERMS checked by kernel and ACLs by fs */ +#define ntfs_allowed_access(scx, ni, type) \ + ((scx)->vol->secure_flags & (1 << SECURITY_DEFAULT) \ + ? 1 : ntfs_allowed_access(scx, ni, type)) +#endif + +#define set_archive(ni) (ni)->flags |= FILE_ATTR_ARCHIVE +#define INODE(ino) ((ino) == 1 ? (MFT_REF)FILE_root : (MFT_REF)(ino)) + +/* + * Call a function from a reparse plugin (variable arguments) + * Requires "reparse" and "ops" to have been defined + * + * Returns a non-negative value if successful, + * and a negative error code if something fails. + */ +#define CALL_REPARSE_PLUGIN(ni, op_name, ...) \ + (reparse = (REPARSE_POINT*)NULL, \ + ops = select_reparse_plugin(ctx, ni, &reparse), \ + (!ops ? -errno \ + : (ops->op_name ? \ + ops->op_name(ni, reparse, __VA_ARGS__) \ + : -EOPNOTSUPP))), \ + free(reparse) + +typedef enum { + FSTYPE_NONE, + FSTYPE_UNKNOWN, + FSTYPE_FUSE, + FSTYPE_FUSEBLK +} fuse_fstype; + +typedef struct fill_item { + struct fill_item *next; + size_t bufsize; + size_t off; + char buf[0]; +} ntfs_fuse_fill_item_t; + +typedef struct fill_context { + struct fill_item *first; + struct fill_item *last; +#ifndef DISABLE_PLUGINS + u64 fh; +#endif /* DISABLE_PLUGINS */ + off_t off; + fuse_req_t req; + fuse_ino_t ino; + BOOL filled; +} ntfs_fuse_fill_context_t; + +struct open_file { + struct open_file *next; + struct open_file *previous; + long long ghost; + fuse_ino_t ino; + fuse_ino_t parent; + int state; +#ifndef DISABLE_PLUGINS + struct fuse_file_info fi; +#endif /* DISABLE_PLUGINS */ +} ; + +enum { + CLOSE_GHOST = 1, + CLOSE_COMPRESSED = 2, + CLOSE_ENCRYPTED = 4, + CLOSE_DMTIME = 8, + CLOSE_REPARSE = 16 +}; + +enum RM_TYPES { + RM_LINK, + RM_DIR, + RM_ANY, +} ; + +static struct ntfs_options opts; + +const char *EXEC_NAME = "lowntfs-3g"; + +static ntfs_fuse_context_t *ctx; +static u32 ntfs_sequence; +static const char ghostformat[] = ".ghost-ntfs-3g-%020llu"; + +static const char *usage_msg = +"\n" +"%s %s %s %d - Third Generation NTFS Driver\n" +"\t\tConfiguration type %d, " +#ifdef HAVE_SETXATTR +"XATTRS are on, " +#else +"XATTRS are off, " +#endif +#if POSIXACLS +"POSIX ACLS are on\n" +#else +"POSIX ACLS are off\n" +#endif +"\n" +"Copyright (C) 2005-2007 Yura Pakhuchiy\n" +"Copyright (C) 2006-2009 Szabolcs Szakacsits\n" +"Copyright (C) 2007-2022 Jean-Pierre Andre\n" +"Copyright (C) 2009-2020 Erik Larsson\n" +"\n" +"Usage: %s [-o option[,...]] \n" +"\n" +"Options: ro (read-only mount), windows_names, uid=, gid=,\n" +" umask=, fmask=, dmask=, streams_interface=.\n" +" Please see the details in the manual (type: man ntfs-3g).\n" +"\n" +"Example: lowntfs-3g /dev/sda1 /mnt/windows\n" +"\n" +#ifdef PLUGIN_DIR +"Plugin path: " PLUGIN_DIR "\n\n" +#endif /* PLUGIN_DIR */ +"%s"; + +static const char ntfs_bad_reparse[] = "unsupported reparse tag 0x%08lx"; + /* exact length of target text, without the terminator */ +#define ntfs_bad_reparse_lth (sizeof(ntfs_bad_reparse) + 2) + +#ifdef FUSE_INTERNAL +int drop_privs(void); +int restore_privs(void); +#else +/* + * setuid and setgid root ntfs-3g denies to start with external FUSE, + * therefore the below functions are no-op in such case. + */ +static int drop_privs(void) { return 0; } +#if defined(linux) || defined(__uClinux__) +static int restore_privs(void) { return 0; } +#endif + +static const char *setuid_msg = +"Mount is denied because setuid and setgid root ntfs-3g is insecure with the\n" +"external FUSE library. Either remove the setuid/setgid bit from the binary\n" +"or rebuild NTFS-3G with integrated FUSE support and make it setuid root.\n" +"Please see more information at\n" +"https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ\n"; + +static const char *unpriv_fuseblk_msg = +"Unprivileged user can not mount NTFS block devices using the external FUSE\n" +"library. Either mount the volume as root, or rebuild NTFS-3G with integrated\n" +"FUSE support and make it setuid root. Please see more information at\n" +"https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ\n"; +#endif + + +static void ntfs_fuse_update_times(ntfs_inode *ni, ntfs_time_update_flags mask) +{ + if (ctx->atime == ATIME_DISABLED) + mask &= ~NTFS_UPDATE_ATIME; + else if (ctx->atime == ATIME_RELATIVE && mask == NTFS_UPDATE_ATIME && + (sle64_to_cpu(ni->last_access_time) + >= sle64_to_cpu(ni->last_data_change_time)) && + (sle64_to_cpu(ni->last_access_time) + >= sle64_to_cpu(ni->last_mft_change_time))) + return; + ntfs_inode_update_times(ni, mask); +} + +static s64 ntfs_get_nr_free_mft_records(ntfs_volume *vol) +{ + ntfs_attr *na = vol->mftbmp_na; + s64 nr_free = ntfs_attr_get_free_bits(na); + + if (nr_free >= 0) + nr_free += (na->allocated_size - na->data_size) << 3; + return nr_free; +} + +/* + * Fill a security context as needed by security functions + * returns TRUE if there is a user mapping, + * FALSE if there is none + * This is not an error and the context is filled anyway, + * it is used for implicit Windows-like inheritance + */ + +static BOOL ntfs_fuse_fill_security_context(fuse_req_t req, + struct SECURITY_CONTEXT *scx) +{ + const struct fuse_ctx *fusecontext; + + scx->vol = ctx->vol; + scx->mapping[MAPUSERS] = ctx->security.mapping[MAPUSERS]; + scx->mapping[MAPGROUPS] = ctx->security.mapping[MAPGROUPS]; + scx->pseccache = &ctx->seccache; + if (req) { + fusecontext = fuse_req_ctx(req); + scx->uid = fusecontext->uid; + scx->gid = fusecontext->gid; + scx->tid = fusecontext->pid; +#ifdef FUSE_CAP_DONT_MASK + /* the umask can be processed by the file system */ + scx->umask = fusecontext->umask; +#else + /* the umask if forced by fuse on creation */ + scx->umask = 0; +#endif + + } else { + scx->uid = 0; + scx->gid = 0; + scx->tid = 0; + scx->umask = 0; + } + return (ctx->security.mapping[MAPUSERS] != (struct MAPPING*)NULL); +} + +static u64 ntfs_fuse_inode_lookup(fuse_ino_t parent, const char *name) +{ + u64 ino = (u64)-1; + u64 inum; + ntfs_inode *dir_ni; + + /* Open target directory. */ + dir_ni = ntfs_inode_open(ctx->vol, INODE(parent)); + if (dir_ni) { + /* Lookup file */ + inum = ntfs_inode_lookup_by_mbsname(dir_ni, name); + /* never return inodes 0 and 1 */ + if (MREF(inum) <= 1) { + inum = (u64)-1; + errno = ENOENT; + } + if (ntfs_inode_close(dir_ni) + || (inum == (u64)-1)) + ino = (u64)-1; + else + ino = MREF(inum); + } + return (ino); +} + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + +/* + * Check access to parent directory + * + * file inode is only opened when not fed in and S_ISVTX is requested, + * when already open and S_ISVTX, it *HAS TO* be fed in. + * + * returns 1 if allowed, + * 0 if not allowed or some error occurred (errno tells why) + */ + +static int ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx, + ntfs_inode *dir_ni, fuse_ino_t ino, + ntfs_inode *ni, mode_t accesstype) +{ + int allowed; + ntfs_inode *ni2; + struct stat stbuf; + + allowed = ntfs_allowed_access(scx, dir_ni, accesstype); + /* + * for an not-owned sticky directory, have to + * check whether file itself is owned + */ + if ((accesstype == (S_IWRITE + S_IEXEC + S_ISVTX)) + && (allowed == 2)) { + if (ni) + ni2 = ni; + else + ni2 = ntfs_inode_open(ctx->vol, INODE(ino)); + allowed = 0; + if (ni2) { + allowed = (ntfs_get_owner_mode(scx,ni2,&stbuf) >= 0) + && (stbuf.st_uid == scx->uid); + if (!ni) + ntfs_inode_close(ni2); + } + } + return (allowed); +} + +#endif /* !KERNELPERMS | (POSIXACLS & !KERNELACLS) */ + +/** + * ntfs_fuse_statfs - return information about mounted NTFS volume + * @path: ignored (but fuse requires it) + * @sfs: statfs structure in which to return the information + * + * Return information about the mounted NTFS volume @sb in the statfs structure + * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is + * called). We interpret the values to be correct of the moment in time at + * which we are called. Most values are variable otherwise and this isn't just + * the free values but the totals as well. For example we can increase the + * total number of file nodes if we run out and we can keep doing this until + * there is no more space on the volume left at all. + * + * This code based on ntfs_statfs from ntfs kernel driver. + * + * Returns 0 on success or -errno on error. + */ + +static void ntfs_fuse_statfs(fuse_req_t req, + fuse_ino_t ino __attribute__((unused))) +{ + struct statvfs sfs; + s64 size; + int delta_bits; + ntfs_volume *vol; + + vol = ctx->vol; + if (vol) { + /* + * File system block size. Used to calculate used/free space by df. + * Incorrectly documented as "optimal transfer block size". + */ + sfs.f_bsize = vol->cluster_size; + + /* Fundamental file system block size, used as the unit. */ + sfs.f_frsize = vol->cluster_size; + + /* + * Total number of blocks on file system in units of f_frsize. + * Since inodes are also stored in blocks ($MFT is a file) hence + * this is the number of clusters on the volume. + */ + sfs.f_blocks = vol->nr_clusters; + + /* Free blocks available for all and for non-privileged processes. */ + size = vol->free_clusters; + if (size < 0) + size = 0; + sfs.f_bavail = sfs.f_bfree = size; + + /* Free inodes on the free space */ + delta_bits = vol->cluster_size_bits - vol->mft_record_size_bits; + if (delta_bits >= 0) + size <<= delta_bits; + else + size >>= -delta_bits; + + /* Number of inodes at this point in time. */ + sfs.f_files = (vol->mftbmp_na->allocated_size << 3) + size; + + /* Free inodes available for all and for non-privileged processes. */ + size += vol->free_mft_records; + if (size < 0) + size = 0; + sfs.f_ffree = sfs.f_favail = size; + + /* Maximum length of filenames. */ + sfs.f_namemax = NTFS_MAX_NAME_LEN; + fuse_reply_statfs(req, &sfs); + } else + fuse_reply_err(req, ENODEV); + +} + +static void set_fuse_error(int *err) +{ + if (!*err) + *err = -errno; +} + +#if 0 && (defined(__APPLE__) || defined(__DARWIN__)) /* Unfinished. */ +static int ntfs_macfuse_getxtimes(const char *org_path, + struct timespec *bkuptime, struct timespec *crtime) +{ + int res = 0; + ntfs_inode *ni; + char *path = NULL; + ntfschar *stream_name; + int stream_name_len; + + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + memset(bkuptime, 0, sizeof(struct timespec)); + memset(crtime, 0, sizeof(struct timespec)); + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + goto exit; + } + + /* We have no backup timestamp in NTFS. */ + crtime->tv_sec = ni->creation_time; +exit: + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(path); + if (stream_name_len) + free(stream_name); + return res; +} + +int ntfs_macfuse_setcrtime(const char *path, const struct timespec *tv) +{ + ntfs_inode *ni; + int res = 0; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + if (tv) { + ni->creation_time = tv->tv_sec; + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + } + + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +int ntfs_macfuse_setbkuptime(const char *path, const struct timespec *tv) +{ + ntfs_inode *ni; + int res = 0; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + /* + * Only pretending to set backup time successfully to please the APIs of + * Mac OS X. In reality, NTFS has no backup time. + */ + + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +int ntfs_macfuse_setchgtime(const char *path, const struct timespec *tv) +{ + ntfs_inode *ni; + int res = 0; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + if (tv) { + ni->last_mft_change_time = tv->tv_sec; + ntfs_fuse_update_times(ni, 0); + } + + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + +static void ntfs_init(void *userdata __attribute__((unused)), + struct fuse_conn_info *conn) +{ +#if defined(__APPLE__) || defined(__DARWIN__) + FUSE_ENABLE_XTIMES(conn); +#endif +#ifdef FUSE_CAP_DONT_MASK + /* request umask not to be enforced by fuse */ + conn->want |= FUSE_CAP_DONT_MASK; +#endif /* defined FUSE_CAP_DONT_MASK */ +#if POSIXACLS & KERNELACLS + /* request ACLs to be checked by kernel */ + conn->want |= FUSE_CAP_POSIX_ACL; +#endif /* POSIXACLS & KERNELACLS */ +#ifdef FUSE_CAP_BIG_WRITES + if (ctx->big_writes + && ((ctx->vol->nr_clusters << ctx->vol->cluster_size_bits) + >= SAFE_CAPACITY_FOR_BIG_WRITES)) + conn->want |= FUSE_CAP_BIG_WRITES; +#endif +#ifdef FUSE_CAP_IOCTL_DIR + conn->want |= FUSE_CAP_IOCTL_DIR; +#endif /* defined(FUSE_CAP_IOCTL_DIR) */ +} + +#ifndef DISABLE_PLUGINS + +/* + * Define attributes for a junction or symlink + * (internal plugin) + */ + +static int junction_getstat(ntfs_inode *ni, + const REPARSE_POINT *reparse __attribute__((unused)), + struct stat *stbuf) +{ + char *target; + int res; + + errno = 0; + target = ntfs_make_symlink(ni, ctx->abs_mnt_point); + /* + * If the reparse point is not a valid + * directory junction, and there is no error + * we still display as a symlink + */ + if (target || (errno == EOPNOTSUPP)) { + if (target) + stbuf->st_size = strlen(target); + else + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_blocks = (ni->allocated_size + 511) >> 9; + stbuf->st_mode = S_IFLNK; + free(target); + res = 0; + } else { + res = -errno; + } + return (res); +} + +static int wsl_getstat(ntfs_inode *ni, const REPARSE_POINT *reparse, + struct stat *stbuf) +{ + dev_t rdev; + int res; + + res = ntfs_reparse_check_wsl(ni, reparse); + if (!res) { + switch (reparse->reparse_tag) { + case IO_REPARSE_TAG_AF_UNIX : + stbuf->st_mode = S_IFSOCK; + break; + case IO_REPARSE_TAG_LX_FIFO : + stbuf->st_mode = S_IFIFO; + break; + case IO_REPARSE_TAG_LX_CHR : + stbuf->st_mode = S_IFCHR; + res = ntfs_ea_check_wsldev(ni, &rdev); + stbuf->st_rdev = rdev; + break; + case IO_REPARSE_TAG_LX_BLK : + stbuf->st_mode = S_IFBLK; + res = ntfs_ea_check_wsldev(ni, &rdev); + stbuf->st_rdev = rdev; + break; + default : + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_mode = S_IFLNK; + break; + } + } + /* + * If the reparse point is not a valid wsl special file + * we display as a symlink + */ + if (res) { + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_mode = S_IFLNK; + res = 0; + } + return (res); +} + +/* + * Apply permission masks to st_mode returned by reparse handler + */ + +static void apply_umask(struct stat *stbuf) +{ + switch (stbuf->st_mode & S_IFMT) { + case S_IFREG : + stbuf->st_mode &= ~ctx->fmask; + break; + case S_IFDIR : + stbuf->st_mode &= ~ctx->dmask; + break; + case S_IFLNK : + stbuf->st_mode = (stbuf->st_mode & S_IFMT) | 0777; + break; + default : + break; + } +} + +#endif /* DISABLE_PLUGINS */ + +static int ntfs_fuse_getstat(struct SECURITY_CONTEXT *scx, + ntfs_inode *ni, struct stat *stbuf) +{ + int res = 0; + ntfs_attr *na; + BOOL withusermapping; + + memset(stbuf, 0, sizeof(struct stat)); + withusermapping = (scx->mapping[MAPUSERS] != (struct MAPPING*)NULL); + stbuf->st_nlink = le16_to_cpu(ni->mrec->link_count); + if (ctx->posix_nlink + && !(ni->flags & FILE_ATTR_REPARSE_POINT)) + stbuf->st_nlink = ntfs_dir_link_cnt(ni); + if ((ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + || (ni->flags & FILE_ATTR_REPARSE_POINT)) { + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + res = CALL_REPARSE_PLUGIN(ni, getattr, stbuf); + if (!res) { + apply_umask(stbuf); + } else { + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_blocks = + (ni->allocated_size + 511) >> 9; + stbuf->st_mode = S_IFLNK; + res = 0; + } + goto ok; +#else /* DISABLE_PLUGINS */ + char *target; + + errno = 0; + target = ntfs_make_symlink(ni, ctx->abs_mnt_point); + /* + * If the reparse point is not a valid + * directory junction, and there is no error + * we still display as a symlink + */ + if (target || (errno == EOPNOTSUPP)) { + if (target) + stbuf->st_size = strlen(target); + else + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_blocks = + (ni->allocated_size + 511) >> 9; + stbuf->st_nlink = + le16_to_cpu(ni->mrec->link_count); + stbuf->st_mode = S_IFLNK; + free(target); + } else { + res = -errno; + goto exit; + } +#endif /* DISABLE_PLUGINS */ + } else { + /* Directory. */ + stbuf->st_mode = S_IFDIR | (0777 & ~ctx->dmask); + /* get index size, if not known */ + if (!test_nino_flag(ni, KnownSize)) { + na = ntfs_attr_open(ni, AT_INDEX_ALLOCATION, + NTFS_INDEX_I30, 4); + if (na) { + ni->data_size = na->data_size; + ni->allocated_size = na->allocated_size; + set_nino_flag(ni, KnownSize); + ntfs_attr_close(na); + } + } + stbuf->st_size = ni->data_size; + stbuf->st_blocks = ni->allocated_size >> 9; + if (!ctx->posix_nlink) + stbuf->st_nlink = 1; /* Make find(1) work */ + } + } else { + /* Regular or Interix (INTX) file. */ + stbuf->st_mode = S_IFREG; + stbuf->st_size = ni->data_size; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* + * return data size rounded to next 512 byte boundary for + * encrypted files to include padding required for decryption + * also include 2 bytes for padding info + */ + if (ctx->efs_raw + && (ni->flags & FILE_ATTR_ENCRYPTED) + && ni->data_size) + stbuf->st_size = ((ni->data_size + 511) & ~511) + 2; +#endif /* HAVE_SETXATTR */ + /* + * Temporary fix to make ActiveSync work via Samba 3.0. + * See more on the ntfs-3g-devel list. + */ + stbuf->st_blocks = (ni->allocated_size + 511) >> 9; + if (ni->flags & FILE_ATTR_SYSTEM) { + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + stbuf->st_ino = ni->mft_no; + goto nodata; + } + /* Check whether it's Interix FIFO or socket. */ + if (!(ni->flags & FILE_ATTR_HIDDEN)) { + /* FIFO. */ + if (na->data_size == 0) + stbuf->st_mode = S_IFIFO; + /* Socket link. */ + if (na->data_size == 1) + stbuf->st_mode = S_IFSOCK; + } + /* + * Check whether it's Interix symbolic link, block or + * character device. + */ + if ((u64)na->data_size <= sizeof(INTX_FILE_TYPES) + + sizeof(ntfschar) * PATH_MAX + && (u64)na->data_size > + sizeof(INTX_FILE_TYPES)) { + INTX_FILE *intx_file; + + intx_file = + (INTX_FILE*)ntfs_malloc(na->data_size); + if (!intx_file) { + res = -errno; + ntfs_attr_close(na); + goto exit; + } + if (ntfs_attr_pread(na, 0, na->data_size, + intx_file) != na->data_size) { + res = -errno; + free(intx_file); + ntfs_attr_close(na); + goto exit; + } + if (intx_file->magic == INTX_BLOCK_DEVICE && + na->data_size == (s64)offsetof( + INTX_FILE, device_end)) { + stbuf->st_mode = S_IFBLK; + stbuf->st_rdev = makedev(le64_to_cpu( + intx_file->major), + le64_to_cpu( + intx_file->minor)); + } + if (intx_file->magic == INTX_CHARACTER_DEVICE && + na->data_size == (s64)offsetof( + INTX_FILE, device_end)) { + stbuf->st_mode = S_IFCHR; + stbuf->st_rdev = makedev(le64_to_cpu( + intx_file->major), + le64_to_cpu( + intx_file->minor)); + } + if (intx_file->magic == INTX_SYMBOLIC_LINK) { + char *target = NULL; + int len; + + /* st_size should be set to length of + * symlink target as multibyte string */ + len = ntfs_ucstombs( + intx_file->target, + (na->data_size - + offsetof(INTX_FILE, + target)) / + sizeof(ntfschar), + &target, 0); + if (len < 0) { + res = -errno; + free(intx_file); + ntfs_attr_close(na); + goto exit; + } + free(target); + stbuf->st_mode = S_IFLNK; + stbuf->st_size = len; + } + free(intx_file); + } + ntfs_attr_close(na); + } + stbuf->st_mode |= (0777 & ~ctx->fmask); + } +#ifndef DISABLE_PLUGINS +ok: +#endif /* DISABLE_PLUGINS */ + if (withusermapping) { + if (ntfs_get_owner_mode(scx,ni,stbuf) < 0) + set_fuse_error(&res); + } else { + stbuf->st_uid = ctx->uid; + stbuf->st_gid = ctx->gid; + } + if (S_ISLNK(stbuf->st_mode)) + stbuf->st_mode |= 0777; +nodata : + stbuf->st_ino = ni->mft_no; +#ifdef HAVE_STRUCT_STAT_ST_ATIMESPEC + stbuf->st_atimespec = ntfs2timespec(ni->last_access_time); + stbuf->st_ctimespec = ntfs2timespec(ni->last_mft_change_time); + stbuf->st_mtimespec = ntfs2timespec(ni->last_data_change_time); +#elif defined(HAVE_STRUCT_STAT_ST_ATIM) + stbuf->st_atim = ntfs2timespec(ni->last_access_time); + stbuf->st_ctim = ntfs2timespec(ni->last_mft_change_time); + stbuf->st_mtim = ntfs2timespec(ni->last_data_change_time); +#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC) + { + struct timespec ts; + + ts = ntfs2timespec(ni->last_access_time); + stbuf->st_atime = ts.tv_sec; + stbuf->st_atimensec = ts.tv_nsec; + ts = ntfs2timespec(ni->last_mft_change_time); + stbuf->st_ctime = ts.tv_sec; + stbuf->st_ctimensec = ts.tv_nsec; + ts = ntfs2timespec(ni->last_data_change_time); + stbuf->st_mtime = ts.tv_sec; + stbuf->st_mtimensec = ts.tv_nsec; + } +#else +#warning "No known way to set nanoseconds in struct stat !" + { + struct timespec ts; + + ts = ntfs2timespec(ni->last_access_time); + stbuf->st_atime = ts.tv_sec; + ts = ntfs2timespec(ni->last_mft_change_time); + stbuf->st_ctime = ts.tv_sec; + ts = ntfs2timespec(ni->last_data_change_time); + stbuf->st_mtime = ts.tv_sec; + } +#endif +exit: + return (res); +} + +static void ntfs_fuse_getattr(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi __attribute__((unused))) +{ + int res; + ntfs_inode *ni; + struct stat stbuf; + struct SECURITY_CONTEXT security; + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) + res = -errno; + else { + ntfs_fuse_fill_security_context(req, &security); + res = ntfs_fuse_getstat(&security, ni, &stbuf); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + if (!res) + fuse_reply_attr(req, &stbuf, ATTR_TIMEOUT); + else + fuse_reply_err(req, -res); +} + +static __inline__ BOOL ntfs_fuse_fillstat(struct SECURITY_CONTEXT *scx, + struct fuse_entry_param *pentry, u64 iref) +{ + ntfs_inode *ni; + BOOL ok = FALSE; + + pentry->ino = MREF(iref); + ni = ntfs_inode_open(ctx->vol, pentry->ino); + if (ni) { + if (!ntfs_fuse_getstat(scx, ni, &pentry->attr)) { + pentry->generation = 1; + pentry->attr_timeout = ATTR_TIMEOUT; + pentry->entry_timeout = ENTRY_TIMEOUT; + ok = TRUE; + } + if (ntfs_inode_close(ni)) + ok = FALSE; + } + return (ok); +} + + +static void ntfs_fuse_lookup(fuse_req_t req, fuse_ino_t parent, + const char *name) +{ + struct SECURITY_CONTEXT security; + struct fuse_entry_param entry; + ntfs_inode *dir_ni; + u64 iref; + BOOL ok = FALSE; + + if (strlen(name) < 256) { + dir_ni = ntfs_inode_open(ctx->vol, INODE(parent)); + if (dir_ni) { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * make sure the parent directory is searchable + */ + if (ntfs_fuse_fill_security_context(req, &security) + && !ntfs_allowed_access(&security,dir_ni,S_IEXEC)) { + ntfs_inode_close(dir_ni); + errno = EACCES; + } else { +#else + ntfs_fuse_fill_security_context(req, &security); +#endif + iref = ntfs_inode_lookup_by_mbsname(dir_ni, + name); + /* never return inodes 0 and 1 */ + if (MREF(iref) <= 1) { + iref = (u64)-1; + errno = ENOENT; + } + ok = !ntfs_inode_close(dir_ni) + && (iref != (u64)-1) + && ntfs_fuse_fillstat( + &security,&entry,iref); +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + } +#endif + } + } else + errno = ENAMETOOLONG; + if (!ok) + fuse_reply_err(req, errno); + else + fuse_reply_entry(req, &entry); +} + +#ifndef DISABLE_PLUGINS + +/* + * Get the link defined by a junction or symlink + * (internal plugin) + */ + +static int junction_readlink(ntfs_inode *ni, + const REPARSE_POINT *reparse, char **pbuf) +{ + int res; + le32 tag; + int lth; + + errno = 0; + res = 0; + *pbuf = ntfs_make_symlink(ni, ctx->abs_mnt_point); + if (!*pbuf) { + if (errno == EOPNOTSUPP) { + *pbuf = (char*)ntfs_malloc(ntfs_bad_reparse_lth + 1); + if (*pbuf) { + if (reparse) + tag = reparse->reparse_tag; + else + tag = const_cpu_to_le32(0); + lth = snprintf(*pbuf, ntfs_bad_reparse_lth + 1, + ntfs_bad_reparse, + (long)le32_to_cpu(tag)); + if (lth != ntfs_bad_reparse_lth) { + free(*pbuf); + *pbuf = (char*)NULL; + res = -errno; + } + } else + res = -ENOMEM; + } else + res = -errno; + } + return (res); +} + +#endif /* DISABLE_PLUGINS */ + +static void ntfs_fuse_readlink(fuse_req_t req, fuse_ino_t ino) +{ + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + INTX_FILE *intx_file = NULL; + char *buf = (char*)NULL; + int res = 0; + + /* Get inode. */ + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + res = -errno; + goto exit; + } + /* + * Reparse point : analyze as a junction point + */ + if (ni->flags & FILE_ATTR_REPARSE_POINT) { + REPARSE_POINT *reparse; + le32 tag; + int lth; +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + + res = CALL_REPARSE_PLUGIN(ni, readlink, &buf); + /* plugin missing or reparse tag failing the check */ + if (res && ((errno == ELIBACC) || (errno == EINVAL))) + errno = EOPNOTSUPP; +#else /* DISABLE_PLUGINS */ + errno = 0; + res = 0; + buf = ntfs_make_symlink(ni, ctx->abs_mnt_point); +#endif /* DISABLE_PLUGINS */ + if (!buf && (errno == EOPNOTSUPP)) { + buf = (char*)malloc(ntfs_bad_reparse_lth + 1); + if (buf) { + reparse = ntfs_get_reparse_point(ni); + if (reparse) { + tag = reparse->reparse_tag; + free(reparse); + } else + tag = const_cpu_to_le32(0); + lth = snprintf(buf, ntfs_bad_reparse_lth + 1, + ntfs_bad_reparse, + (long)le32_to_cpu(tag)); + res = 0; + if (lth != ntfs_bad_reparse_lth) { + free(buf); + buf = (char*)NULL; + } + } + } + if (!buf) + res = -errno; + goto exit; + } + /* Sanity checks. */ + if (!(ni->flags & FILE_ATTR_SYSTEM)) { + res = -EINVAL; + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + res = -errno; + goto exit; + } + if ((size_t)na->data_size <= sizeof(INTX_FILE_TYPES)) { + res = -EINVAL; + goto exit; + } + if ((size_t)na->data_size > sizeof(INTX_FILE_TYPES) + + sizeof(ntfschar) * PATH_MAX) { + res = -ENAMETOOLONG; + goto exit; + } + /* Receive file content. */ + intx_file = (INTX_FILE*)ntfs_malloc(na->data_size); + if (!intx_file) { + res = -errno; + goto exit; + } + if (ntfs_attr_pread(na, 0, na->data_size, intx_file) != na->data_size) { + res = -errno; + goto exit; + } + /* Sanity check. */ + if (intx_file->magic != INTX_SYMBOLIC_LINK) { + res = -EINVAL; + goto exit; + } + /* Convert link from unicode to local encoding. */ + if (ntfs_ucstombs(intx_file->target, (na->data_size - + offsetof(INTX_FILE, target)) / sizeof(ntfschar), + &buf, 0) < 0) { + res = -errno; + goto exit; + } +exit: + if (intx_file) + free(intx_file); + if (na) + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_readlink(req, buf); + free(buf); +} + +static int ntfs_fuse_filler(ntfs_fuse_fill_context_t *fill_ctx, + const ntfschar *name, const int name_len, const int name_type, + const s64 pos __attribute__((unused)), const MFT_REF mref, + const unsigned dt_type __attribute__((unused))) +{ + char *filename = NULL; + int ret = 0; + int filenamelen = -1; + size_t sz; + ntfs_fuse_fill_item_t *current; + ntfs_fuse_fill_item_t *newone; + + if (name_type == FILE_NAME_DOS) + return 0; + + if ((filenamelen = ntfs_ucstombs(name, name_len, &filename, 0)) < 0) { + ntfs_log_perror("Filename decoding failed (inode %llu)", + (unsigned long long)MREF(mref)); + return -1; + } + /* never return inodes 0 and 1 */ + if (MREF(mref) > 1) { + struct stat st = { .st_ino = MREF(mref) }; +#ifndef DISABLE_PLUGINS + ntfs_inode *ni; +#endif /* DISABLE_PLUGINS */ + + switch (dt_type) { + case NTFS_DT_DIR : + st.st_mode = S_IFDIR | (0777 & ~ctx->dmask); + break; + case NTFS_DT_LNK : + st.st_mode = S_IFLNK | 0777; + break; + case NTFS_DT_FIFO : + st.st_mode = S_IFIFO; + break; + case NTFS_DT_SOCK : + st.st_mode = S_IFSOCK; + break; + case NTFS_DT_BLK : + st.st_mode = S_IFBLK; + break; + case NTFS_DT_CHR : + st.st_mode = S_IFCHR; + break; + case NTFS_DT_REPARSE : + st.st_mode = S_IFLNK | 0777; /* default */ +#ifndef DISABLE_PLUGINS + /* get emulated type from plugin if available */ + ni = ntfs_inode_open(ctx->vol, mref); + if (ni && (ni->flags & FILE_ATTR_REPARSE_POINT)) { + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + int res; + + res = CALL_REPARSE_PLUGIN(ni, getattr, &st); + if (!res) + apply_umask(&st); + else + st.st_mode = S_IFLNK; + } + if (ni) + ntfs_inode_close(ni); +#endif /* DISABLE_PLUGINS */ + break; + default : /* unexpected types shown as plain files */ + case NTFS_DT_REG : + st.st_mode = S_IFREG | (0777 & ~ctx->fmask); + break; + } + +#if defined(__APPLE__) || defined(__DARWIN__) + /* + * Returning file names larger than MAXNAMLEN (255) bytes + * causes Darwin/Mac OS X to bug out and skip the entry. + */ + if (filenamelen > MAXNAMLEN) { + ntfs_log_debug("Truncating %d byte filename to " + "%d bytes.\n", filenamelen, MAXNAMLEN); + ntfs_log_debug(" before: '%s'\n", filename); + memset(filename + MAXNAMLEN, 0, filenamelen - MAXNAMLEN); + ntfs_log_debug(" after: '%s'\n", filename); + } +#elif defined(__sun) && defined (__SVR4) + /* + * Returning file names larger than MAXNAMELEN (256) bytes + * causes Solaris/Illumos to return an I/O error from the system + * call. + * However we also need space for a terminating NULL, or user + * space tools will bug out since they expect a NULL terminator. + * Effectively the maximum length of a file name is MAXNAMELEN - + * 1 (255). + */ + if (filenamelen > (MAXNAMELEN - 1)) { + ntfs_log_debug("Truncating %d byte filename to %d " + "bytes.\n", filenamelen, MAXNAMELEN - 1); + ntfs_log_debug(" before: '%s'\n", filename); + memset(&filename[MAXNAMELEN - 1], 0, + filenamelen - (MAXNAMELEN - 1)); + ntfs_log_debug(" after: '%s'\n", filename); + } +#endif /* defined(__APPLE__) || defined(__DARWIN__), ... */ + + current = fill_ctx->last; + sz = fuse_add_direntry(fill_ctx->req, + ¤t->buf[current->off], + current->bufsize - current->off, + filename, &st, current->off + fill_ctx->off); + if (!sz || ((current->off + sz) > current->bufsize)) { + newone = (ntfs_fuse_fill_item_t*)ntfs_malloc + (sizeof(ntfs_fuse_fill_item_t) + + current->bufsize); + if (newone) { + newone->off = 0; + newone->bufsize = current->bufsize; + newone->next = (ntfs_fuse_fill_item_t*)NULL; + current->next = newone; + fill_ctx->last = newone; + fill_ctx->off += current->off; + current = newone; + sz = fuse_add_direntry(fill_ctx->req, + current->buf, + current->bufsize - current->off, + filename, &st, fill_ctx->off); + if (!sz) { + errno = EIO; + ntfs_log_error("Could not add a" + " directory entry (inode %lld)\n", + (unsigned long long)MREF(mref)); + } + } else { + sz = 0; + errno = ENOMEM; + } + } + if (sz) { + current->off += sz; + } else { + ret = -1; + } + } + + free(filename); + return ret; +} + +static void ntfs_fuse_opendir(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + int res = 0; + ntfs_inode *ni; + int accesstype; + ntfs_fuse_fill_context_t *fill; + struct SECURITY_CONTEXT security; + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (ni) { + if (ntfs_fuse_fill_security_context(req, &security)) { + if (fi->flags & O_WRONLY) + accesstype = S_IWRITE; + else + if (fi->flags & O_RDWR) + accesstype = S_IWRITE | S_IREAD; + else + accesstype = S_IREAD; + if (!ntfs_allowed_access(&security,ni,accesstype)) + res = -EACCES; + } + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + fi->fh = 0; + res = CALL_REPARSE_PLUGIN(ni, opendir, fi); +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + } + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + if (!res) { + fill = (ntfs_fuse_fill_context_t*) + ntfs_malloc(sizeof(ntfs_fuse_fill_context_t)); + if (!fill) + res = -errno; + else { + fill->first = fill->last + = (ntfs_fuse_fill_item_t*)NULL; + fill->filled = FALSE; + fill->ino = ino; + fill->off = 0; +#ifndef DISABLE_PLUGINS + fill->fh = fi->fh; +#endif /* DISABLE_PLUGINS */ + } + fi->fh = (long)fill; + } + } else + res = -errno; + if (!res) + fuse_reply_open(req, fi); + else + fuse_reply_err(req, -res); +} + + +static void ntfs_fuse_releasedir(fuse_req_t req, + fuse_ino_t ino __attribute__((unused)), + struct fuse_file_info *fi) +{ +#ifndef DISABLE_PLUGINS + struct fuse_file_info ufi; + ntfs_inode *ni; +#endif /* DISABLE_PLUGINS */ + ntfs_fuse_fill_context_t *fill; + ntfs_fuse_fill_item_t *current; + int res; + + res = 0; + fill = (ntfs_fuse_fill_context_t*)(long)fi->fh; + if (fill && (fill->ino == ino)) { + /* make sure to clear results */ + current = fill->first; + while (current) { + current = current->next; + free(fill->first); + fill->first = current; + } +#ifndef DISABLE_PLUGINS + if (fill->fh) { + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (ni) { + if (ni->flags & FILE_ATTR_REPARSE_POINT) { + memcpy(&ufi, fi, sizeof(ufi)); + ufi.fh = fill->fh; + res = CALL_REPARSE_PLUGIN(ni, release, + &ufi); + } + if (ntfs_inode_close(ni) && !res) + res = -errno; + } else + res = -errno; + } +#endif /* DISABLE_PLUGINS */ + fill->ino = 0; + free(fill); + } + fuse_reply_err(req, -res); +} + +static void ntfs_fuse_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, + off_t off __attribute__((unused)), + struct fuse_file_info *fi __attribute__((unused))) +{ +#ifndef DISABLE_PLUGINS + struct fuse_file_info ufi; +#endif /* DISABLE_PLUGINS */ + ntfs_fuse_fill_item_t *first; + ntfs_fuse_fill_item_t *current; + ntfs_fuse_fill_context_t *fill; + ntfs_inode *ni; + s64 pos = 0; + int err = 0; + + fill = (ntfs_fuse_fill_context_t*)(long)fi->fh; + if (fill && (fill->ino == ino)) { + if (fill->filled && !off) { + /* Rewinding : make sure to clear existing results */ + current = fill->first; + while (current) { + current = current->next; + free(fill->first); + fill->first = current; + } + fill->filled = FALSE; + } + if (!fill->filled) { + /* initial call : build the full list */ + current = (ntfs_fuse_fill_item_t*)NULL; + first = (ntfs_fuse_fill_item_t*)ntfs_malloc + (sizeof(ntfs_fuse_fill_item_t) + size); + if (first) { + first->bufsize = size; + first->off = 0; + first->next = (ntfs_fuse_fill_item_t*)NULL; + fill->req = req; + fill->first = first; + fill->last = first; + fill->off = 0; + ni = ntfs_inode_open(ctx->vol,INODE(ino)); + if (!ni) + err = -errno; + else { + if (ni->flags + & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + memcpy(&ufi, fi, sizeof(ufi)); + ufi.fh = fill->fh; + err = CALL_REPARSE_PLUGIN(ni, + readdir, &pos, fill, + (ntfs_filldir_t) + ntfs_fuse_filler, &ufi); +#else /* DISABLE_PLUGINS */ + err = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + } else { + if (ntfs_readdir(ni, &pos, fill, + (ntfs_filldir_t) + ntfs_fuse_filler)) + err = -errno; + } + fill->filled = TRUE; + ntfs_fuse_update_times(ni, + NTFS_UPDATE_ATIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&err); + } + if (!err) { + off_t loc = 0; + /* + * In some circumstances, the queue gets + * reinitialized by releasedir() + opendir(), + * apparently always on end of partial buffer. + * Files may be missing or duplicated. + */ + while (first + && ((loc < off) || !first->off)) { + loc += first->off; + fill->first = first->next; + free(first); + first = fill->first; + } + current = first; + } + } else + err = -errno; + } else { + /* subsequent call : return next non-empty buffer */ + current = fill->first; + while (current && !current->off) { + current = current->next; + free(fill->first); + fill->first = current; + } + } + if (!err) { + if (current) { + fuse_reply_buf(req, current->buf, current->off); + fill->first = current->next; + free(current); + } else { + fuse_reply_buf(req, (char*)NULL, 0); + /* reply sent, now must exit with no error */ + } + } + } else { + errno = EIO; + err = -errno; + ntfs_log_error("Uninitialized fuse_readdir()\n"); + } + if (err) + fuse_reply_err(req, -err); +} + +static void ntfs_fuse_open(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + ntfs_inode *ni; + ntfs_attr *na = NULL; + struct open_file *of; + int state = 0; + int res = 0; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + int accesstype; + struct SECURITY_CONTEXT security; +#endif + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (ni) { + if (!(ni->flags & FILE_ATTR_REPARSE_POINT)) { + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + res = -errno; + goto close; + } + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (ntfs_fuse_fill_security_context(req, &security)) { + if (fi->flags & O_WRONLY) + accesstype = S_IWRITE; + else + if (fi->flags & O_RDWR) + accesstype = S_IWRITE | S_IREAD; + else + accesstype = S_IREAD; + /* check whether requested access is allowed */ + if (!ntfs_allowed_access(&security, + ni,accesstype)) + res = -EACCES; + } +#endif + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + fi->fh = 0; + res = CALL_REPARSE_PLUGIN(ni, open, fi); + if (!res && fi->fh) { + state = CLOSE_REPARSE; + } +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + goto close; + } + if ((res >= 0) + && (fi->flags & (O_WRONLY | O_RDWR))) { + /* mark a future need to compress the last chunk */ + if (na->data_flags & ATTR_COMPRESSION_MASK) + state |= CLOSE_COMPRESSED; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* mark a future need to fixup encrypted inode */ + if (ctx->efs_raw + && !(na->data_flags & ATTR_IS_ENCRYPTED) + && (ni->flags & FILE_ATTR_ENCRYPTED)) + state |= CLOSE_ENCRYPTED; +#endif /* HAVE_SETXATTR */ + /* mark a future need to update the mtime */ + if (ctx->dmtime) + state |= CLOSE_DMTIME; + /* deny opening metadata files for writing */ + if (ino < FILE_first_user) + res = -EPERM; + } + ntfs_attr_close(na); +close: + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } else + res = -errno; + if (res >= 0) { + of = (struct open_file*)malloc(sizeof(struct open_file)); + if (of) { + of->parent = 0; + of->ino = ino; + of->state = state; +#ifndef DISABLE_PLUGINS + memcpy(&of->fi, fi, sizeof(struct fuse_file_info)); +#endif /* DISABLE_PLUGINS */ + of->next = ctx->open_files; + of->previous = (struct open_file*)NULL; + if (ctx->open_files) + ctx->open_files->previous = of; + ctx->open_files = of; + fi->fh = (long)of; + } + } + if (res) + fuse_reply_err(req, -res); + else + fuse_reply_open(req, fi); +} + +static void ntfs_fuse_read(fuse_req_t req, fuse_ino_t ino, size_t size, + off_t offset, + struct fuse_file_info *fi __attribute__((unused))) +{ + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + int res; + char *buf = (char*)NULL; + s64 total = 0; + s64 max_read; + + if (!size) { + res = 0; + goto exit; + } + buf = (char*)ntfs_malloc(size); + if (!buf) { + res = -errno; + goto exit; + } + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + res = -errno; + goto exit; + } + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + struct open_file *of; + + of = (struct open_file*)(long)fi->fh; + res = CALL_REPARSE_PLUGIN(ni, read, buf, size, offset, &of->fi); + if (res >= 0) { + goto stamps; + } +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + res = -errno; + goto exit; + } + max_read = na->data_size; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* limit reads at next 512 byte boundary for encrypted attributes */ + if (ctx->efs_raw + && max_read + && (na->data_flags & ATTR_IS_ENCRYPTED) + && NAttrNonResident(na)) { + max_read = ((na->data_size+511) & ~511) + 2; + } +#endif /* HAVE_SETXATTR */ + if (offset + (off_t)size > max_read) { + if (max_read < offset) + goto ok; + size = max_read - offset; + } + while (size > 0) { + s64 ret = ntfs_attr_pread(na, offset, size, buf + total); + if (ret != (s64)size) + ntfs_log_perror("ntfs_attr_pread error reading inode %lld at " + "offset %lld: %lld <> %lld", (long long)ni->mft_no, + (long long)offset, (long long)size, (long long)ret); + if (ret <= 0 || ret > (s64)size) { + res = (ret < 0) ? -errno : -EIO; + goto exit; + } + size -= ret; + offset += ret; + total += ret; + } +ok: + res = total; +#ifndef DISABLE_PLUGINS +stamps : +#endif /* DISABLE_PLUGINS */ + ntfs_fuse_update_times(ni, NTFS_UPDATE_ATIME); +exit: + if (na) + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_buf(req, buf, res); + free(buf); +} + +static void ntfs_fuse_write(fuse_req_t req, fuse_ino_t ino, const char *buf, + size_t size, off_t offset, + struct fuse_file_info *fi __attribute__((unused))) +{ + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + int res, total = 0; + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + res = -errno; + goto exit; + } + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + struct open_file *of; + + of = (struct open_file*)(long)fi->fh; + res = CALL_REPARSE_PLUGIN(ni, write, buf, size, offset, + &of->fi); + if (res >= 0) { + goto stamps; + } +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + res = -errno; + goto exit; + } + while (size) { + s64 ret = ntfs_attr_pwrite(na, offset, size, buf + total); + if (ret <= 0) { + res = -errno; + goto exit; + } + size -= ret; + offset += ret; + total += ret; + } + res = total; +#ifndef DISABLE_PLUGINS +stamps : +#endif /* DISABLE_PLUGINS */ + if ((res > 0) + && (!ctx->dmtime + || (sle64_to_cpu(ntfs_current_time()) + - sle64_to_cpu(ni->last_data_change_time)) > ctx->dmtime)) + ntfs_fuse_update_times(ni, NTFS_UPDATE_MCTIME); +exit: + if (na) + ntfs_attr_close(na); + if (res > 0) + set_archive(ni); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_write(req, res); +} + +static int ntfs_fuse_chmod(struct SECURITY_CONTEXT *scx, fuse_ino_t ino, + mode_t mode, struct stat *stbuf) +{ + int res = 0; + ntfs_inode *ni; + + /* Unsupported if inherit or no user mapping has been defined */ + if ((!scx->mapping[MAPUSERS] || ctx->inherit) + && !ctx->silent) { + res = -EOPNOTSUPP; + } else { + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) + res = -errno; + else { + /* ignore if Windows inheritance is forced */ + if (scx->mapping[MAPUSERS] && !ctx->inherit) { + if (ntfs_set_mode(scx, ni, mode)) + res = -errno; + else { + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + /* + * Must return updated times, and + * inode has been updated, so hope + * we get no further errors + */ + res = ntfs_fuse_getstat(scx, ni, stbuf); + } + NInoSetDirty(ni); + } else + res = ntfs_fuse_getstat(scx, ni, stbuf); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } + return res; +} + +static int ntfs_fuse_chown(struct SECURITY_CONTEXT *scx, fuse_ino_t ino, + uid_t uid, gid_t gid, struct stat *stbuf) +{ + ntfs_inode *ni; + int res; + + /* Unsupported if inherit or no user mapping has been defined */ + if ((!scx->mapping[MAPUSERS] || ctx->inherit) + && !ctx->silent + && ((uid != ctx->uid) || (gid != ctx->gid))) + res = -EOPNOTSUPP; + else { + res = 0; + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) + res = -errno; + else { + /* ignore if Windows inheritance is forced */ + if (scx->mapping[MAPUSERS] + && !ctx->inherit + && (((int)uid != -1) || ((int)gid != -1))) { + if (ntfs_set_owner(scx, ni, uid, gid)) + res = -errno; + else { + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + /* + * Must return updated times, and + * inode has been updated, so hope + * we get no further errors + */ + res = ntfs_fuse_getstat(scx, ni, stbuf); + } + } else + res = ntfs_fuse_getstat(scx, ni, stbuf); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } + return (res); +} + +static int ntfs_fuse_chownmod(struct SECURITY_CONTEXT *scx, fuse_ino_t ino, + uid_t uid, gid_t gid, mode_t mode, struct stat *stbuf) +{ + ntfs_inode *ni; + int res; + + /* Unsupported if inherit or no user mapping has been defined */ + if ((!scx->mapping[MAPUSERS] || ctx->inherit) + && !ctx->silent + && ((uid != ctx->uid) || (gid != ctx->gid))) + res = -EOPNOTSUPP; + else { + res = 0; + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) + res = -errno; + else { + /* ignore if Windows inheritance is forced */ + if (scx->mapping[MAPUSERS] && !ctx->inherit) { + if (ntfs_set_ownmod(scx, ni, uid, gid, mode)) + res = -errno; + else { + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + /* + * Must return updated times, and + * inode has been updated, so hope + * we get no further errors + */ + res = ntfs_fuse_getstat(scx, ni, stbuf); + } + } else + res = ntfs_fuse_getstat(scx, ni, stbuf); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } + return (res); +} + +static int ntfs_fuse_trunc(struct SECURITY_CONTEXT *scx, fuse_ino_t ino, +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + off_t size, BOOL chkwrite, struct stat *stbuf) +#else + off_t size, BOOL chkwrite __attribute__((unused)), + struct stat *stbuf) +#endif +{ + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + int res; + s64 oldsize; + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) + goto exit; + + /* deny truncating metadata files */ + if (ino < FILE_first_user) { + errno = EPERM; + goto exit; + } + if (!(ni->flags & FILE_ATTR_REPARSE_POINT)) { + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) + goto exit; + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * deny truncation if cannot write to file + * (already checked for ftruncate()) + */ + if (scx->mapping[MAPUSERS] + && chkwrite + && !ntfs_allowed_access(scx, ni, S_IWRITE)) { + errno = EACCES; + goto exit; + } +#endif + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + res = CALL_REPARSE_PLUGIN(ni, truncate, size); + if (!res) { + set_archive(ni); + goto stamps; + } +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + goto exit; + } + /* + * for compressed files, upsizing is done by inserting a final + * zero, which is optimized as creating a hole when possible. + */ + oldsize = na->data_size; + if ((na->data_flags & ATTR_COMPRESSION_MASK) + && (size > na->initialized_size)) { + char zero = 0; + if (ntfs_attr_pwrite(na, size - 1, 1, &zero) <= 0) + goto exit; + } else + if (ntfs_attr_truncate(na, size)) + goto exit; + if (oldsize != size) + set_archive(ni); + +#ifndef DISABLE_PLUGINS +stamps : +#endif /* DISABLE_PLUGINS */ + ntfs_fuse_update_times(ni, NTFS_UPDATE_MCTIME); + res = ntfs_fuse_getstat(scx, ni, stbuf); + errno = (res ? -res : 0); +exit: + res = -errno; + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +#if defined(HAVE_UTIMENSAT) & defined(FUSE_SET_ATTR_ATIME_NOW) + +static int ntfs_fuse_utimens(struct SECURITY_CONTEXT *scx, fuse_ino_t ino, + struct stat *stin, struct stat *stbuf, int to_set) +{ + ntfs_inode *ni; + int res = 0; + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) + return -errno; + + /* no check or update if both UTIME_OMIT */ + if (to_set & (FUSE_SET_ATTR_ATIME + FUSE_SET_ATTR_MTIME)) { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (ntfs_allowed_as_owner(scx, ni) + || ((to_set & FUSE_SET_ATTR_ATIME_NOW) + && (to_set & FUSE_SET_ATTR_MTIME_NOW) + && ntfs_allowed_access(scx, ni, S_IWRITE))) { +#endif + ntfs_time_update_flags mask = NTFS_UPDATE_CTIME; + + if (to_set & FUSE_SET_ATTR_ATIME_NOW) + mask |= NTFS_UPDATE_ATIME; + else + if (to_set & FUSE_SET_ATTR_ATIME) { +#ifdef HAVE_STRUCT_STAT_ST_ATIMESPEC + ni->last_access_time + = timespec2ntfs(stin->st_atimespec); +#elif defined(HAVE_STRUCT_STAT_ST_ATIM) + ni->last_access_time + = timespec2ntfs(stin->st_atim); +#else + ni->last_access_time.tv_sec + = stin->st_atime; +#ifdef HAVE_STRUCT_STAT_ST_ATIMENSEC + ni->last_access_time.tv_nsec + = stin->st_atimensec; +#endif +#endif + } + if (to_set & FUSE_SET_ATTR_MTIME_NOW) + mask |= NTFS_UPDATE_MTIME; + else + if (to_set & FUSE_SET_ATTR_MTIME) { +#ifdef HAVE_STRUCT_STAT_ST_ATIMESPEC + ni->last_data_change_time + = timespec2ntfs(stin->st_mtimespec); +#elif defined(HAVE_STRUCT_STAT_ST_ATIM) + ni->last_data_change_time + = timespec2ntfs(stin->st_mtim); +#else + ni->last_data_change_time.tv_sec + = stin->st_mtime; +#ifdef HAVE_STRUCT_STAT_ST_ATIMENSEC + ni->last_data_change_time.tv_nsec + = stin->st_mtimensec; +#endif +#endif + } + ntfs_inode_update_times(ni, mask); +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + } else + res = -errno; +#endif + } + if (!res) + res = ntfs_fuse_getstat(scx, ni, stbuf); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +#else /* defined(HAVE_UTIMENSAT) & defined(FUSE_SET_ATTR_ATIME_NOW) */ + +static int ntfs_fuse_utime(struct SECURITY_CONTEXT *scx, fuse_ino_t ino, + struct stat *stin, struct stat *stbuf) +{ + ntfs_inode *ni; + int res = 0; + struct timespec actime; + struct timespec modtime; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + BOOL ownerok; + BOOL writeok; +#endif + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) + return -errno; + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + ownerok = ntfs_allowed_as_owner(scx, ni); + if (stin) { + /* + * fuse never calls with a NULL buf and we do not + * know whether the specific condition can be applied + * So we have to accept updating by a non-owner having + * write access. + */ + writeok = !ownerok + && (stin->st_atime == stin->st_mtime) + && ntfs_allowed_access(scx, ni, S_IWRITE); + /* Must be owner */ + if (!ownerok && !writeok) + res = (stin->st_atime == stin->st_mtime + ? -EACCES : -EPERM); + else { + actime.tv_sec = stin->st_atime; + actime.tv_nsec = 0; + modtime.tv_sec = stin->st_mtime; + modtime.tv_nsec = 0; + ni->last_access_time = timespec2ntfs(actime); + ni->last_data_change_time = timespec2ntfs(modtime); + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + } + } else { + /* Must be owner or have write access */ + writeok = !ownerok + && ntfs_allowed_access(scx, ni, S_IWRITE); + if (!ownerok && !writeok) + res = -EACCES; + else + ntfs_inode_update_times(ni, NTFS_UPDATE_AMCTIME); + } +#else + if (stin) { + actime.tv_sec = stin->st_atime; + actime.tv_nsec = 0; + modtime.tv_sec = stin->st_mtime; + modtime.tv_nsec = 0; + ni->last_access_time = timespec2ntfs(actime); + ni->last_data_change_time = timespec2ntfs(modtime); + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + } else + ntfs_inode_update_times(ni, NTFS_UPDATE_AMCTIME); +#endif + + res = ntfs_fuse_getstat(scx, ni, stbuf); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +#endif /* defined(HAVE_UTIMENSAT) & defined(FUSE_SET_ATTR_ATIME_NOW) */ + +static void ntfs_fuse_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, + int to_set, struct fuse_file_info *fi __attribute__((unused))) +{ + struct stat stbuf; + ntfs_inode *ni; + int res; + struct SECURITY_CONTEXT security; + + res = 0; + ntfs_fuse_fill_security_context(req, &security); + /* no flags */ + if (!(to_set + & (FUSE_SET_ATTR_MODE + | FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID + | FUSE_SET_ATTR_SIZE + | FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME))) { + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) + res = -errno; + else { + res = ntfs_fuse_getstat(&security, ni, &stbuf); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } + /* some set of uid/gid/mode */ + if (to_set + & (FUSE_SET_ATTR_MODE + | FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) { + switch (to_set + & (FUSE_SET_ATTR_MODE + | FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) { + case FUSE_SET_ATTR_MODE : + res = ntfs_fuse_chmod(&security, ino, + attr->st_mode & 07777, &stbuf); + break; + case FUSE_SET_ATTR_UID : + res = ntfs_fuse_chown(&security, ino, attr->st_uid, + (gid_t)-1, &stbuf); + break; + case FUSE_SET_ATTR_GID : + res = ntfs_fuse_chown(&security, ino, (uid_t)-1, + attr->st_gid, &stbuf); + break; + case FUSE_SET_ATTR_UID + FUSE_SET_ATTR_GID : + res = ntfs_fuse_chown(&security, ino, attr->st_uid, + attr->st_gid, &stbuf); + break; + case FUSE_SET_ATTR_UID + FUSE_SET_ATTR_MODE: + res = ntfs_fuse_chownmod(&security, ino, attr->st_uid, + (gid_t)-1,attr->st_mode, + &stbuf); + break; + case FUSE_SET_ATTR_GID + FUSE_SET_ATTR_MODE: + res = ntfs_fuse_chownmod(&security, ino, (uid_t)-1, + attr->st_gid,attr->st_mode, + &stbuf); + break; + case FUSE_SET_ATTR_UID + FUSE_SET_ATTR_GID + FUSE_SET_ATTR_MODE: + res = ntfs_fuse_chownmod(&security, ino, attr->st_uid, + attr->st_gid,attr->st_mode, &stbuf); + break; + default : + break; + } + } + /* size */ + if (!res && (to_set & FUSE_SET_ATTR_SIZE)) { + res = ntfs_fuse_trunc(&security, ino, attr->st_size, + !fi, &stbuf); + } + /* some set of atime/mtime */ + if (!res && (to_set & (FUSE_SET_ATTR_ATIME + FUSE_SET_ATTR_MTIME))) { +#if defined(HAVE_UTIMENSAT) & defined(FUSE_SET_ATTR_ATIME_NOW) + res = ntfs_fuse_utimens(&security, ino, attr, &stbuf, to_set); +#else /* defined(HAVE_UTIMENSAT) & defined(FUSE_SET_ATTR_ATIME_NOW) */ + res = ntfs_fuse_utime(&security, ino, attr, &stbuf); +#endif /* defined(HAVE_UTIMENSAT) & defined(FUSE_SET_ATTR_ATIME_NOW) */ + } + if (res) + fuse_reply_err(req, -res); + else + fuse_reply_attr(req, &stbuf, ATTR_TIMEOUT); +} + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + +static void ntfs_fuse_access(fuse_req_t req, fuse_ino_t ino, int mask) +{ + int res = 0; + int mode; + ntfs_inode *ni; + struct SECURITY_CONTEXT security; + + /* JPA return unsupported if no user mapping has been defined */ + if (!ntfs_fuse_fill_security_context(req, &security)) { + if (ctx->silent) + res = 0; + else + res = -EOPNOTSUPP; + } else { + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + res = -errno; + } else { + mode = 0; + if (mask & (X_OK | W_OK | R_OK)) { + if (mask & X_OK) mode += S_IEXEC; + if (mask & W_OK) mode += S_IWRITE; + if (mask & R_OK) mode += S_IREAD; + if (!ntfs_allowed_access(&security, + ni, mode)) + res = -errno; + } + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_err(req, 0); +} + +#endif /* !KERNELPERMS | (POSIXACLS & !KERNELACLS) */ + +static int ntfs_fuse_create(fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t typemode, dev_t dev, + struct fuse_entry_param *e, + const char *target, struct fuse_file_info *fi) +{ + ntfschar *uname = NULL, *utarget = NULL; + ntfs_inode *dir_ni = NULL, *ni; + struct open_file *of; + int state = 0; + le32 securid; + gid_t gid; + mode_t dsetgid; + mode_t type = typemode & ~07777; + mode_t perm; + struct SECURITY_CONTEXT security; + int res = 0, uname_len, utarget_len; + + /* Generate unicode filename. */ + uname_len = ntfs_mbstoucs(name, &uname); + if ((uname_len < 0) + || (ctx->windows_names + && ntfs_forbidden_names(ctx->vol,uname,uname_len,TRUE))) { + res = -errno; + goto exit; + } + /* Deny creating into $Extend */ + if (parent == FILE_Extend) { + res = -EPERM; + goto exit; + } + /* Open parent directory. */ + dir_ni = ntfs_inode_open(ctx->vol, INODE(parent)); + if (!dir_ni) { + res = -errno; + goto exit; + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* make sure parent directory is writeable and executable */ + if (!ntfs_fuse_fill_security_context(req, &security) + || ntfs_allowed_create(&security, + dir_ni, &gid, &dsetgid)) { +#else + ntfs_fuse_fill_security_context(req, &security); + ntfs_allowed_create(&security, dir_ni, &gid, &dsetgid); +#endif + if (S_ISDIR(type)) + perm = (typemode & ~ctx->dmask & 0777) + | (dsetgid & S_ISGID); + else + if ((ctx->special_files == NTFS_FILES_WSL) + && S_ISLNK(type)) + perm = typemode | 0777; + else + perm = typemode & ~ctx->fmask & 0777; + /* + * Try to get a security id available for + * file creation (from inheritance or argument). + * This is not possible for NTFS 1.x, and we will + * have to build a security attribute later. + */ + if (!ctx->security.mapping[MAPUSERS]) + securid = const_cpu_to_le32(0); + else + if (ctx->inherit) + securid = ntfs_inherited_id(&security, + dir_ni, S_ISDIR(type)); + else +#if POSIXACLS + securid = ntfs_alloc_securid(&security, + security.uid, gid, + dir_ni, perm, S_ISDIR(type)); +#else + securid = ntfs_alloc_securid(&security, + security.uid, gid, + perm & ~security.umask, S_ISDIR(type)); +#endif + /* Create object specified in @type. */ + if (dir_ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + reparse = (REPARSE_POINT*)NULL; + ops = select_reparse_plugin(ctx, dir_ni, &reparse); + if (ops && ops->create) { + ni = (*ops->create)(dir_ni, reparse, + securid, uname, uname_len, type); + } else { + ni = (ntfs_inode*)NULL; + errno = EOPNOTSUPP; + } + free(reparse); +#else /* DISABLE_PLUGINS */ + ni = (ntfs_inode*)NULL; + errno = EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + } else { + switch (type) { + case S_IFCHR: + case S_IFBLK: + ni = ntfs_create_device(dir_ni, securid, + uname, uname_len, + type, dev); + break; + case S_IFLNK: + utarget_len = ntfs_mbstoucs(target, + &utarget); + if (utarget_len < 0) { + res = -errno; + goto exit; + } + ni = ntfs_create_symlink(dir_ni, + securid, + uname, uname_len, + utarget, utarget_len); + break; + default: + ni = ntfs_create(dir_ni, securid, uname, + uname_len, type); + break; + } + } + if (ni) { + /* + * set the security attribute if a security id + * could not be allocated (eg NTFS 1.x) + */ + if (ctx->security.mapping[MAPUSERS]) { +#if POSIXACLS + if (!securid + && ntfs_set_inherited_posix(&security, ni, + security.uid, gid, + dir_ni, perm) < 0) + set_fuse_error(&res); +#else + if (!securid + && ntfs_set_owner_mode(&security, ni, + security.uid, gid, + perm & ~security.umask) < 0) + set_fuse_error(&res); +#endif + } + set_archive(ni); + /* mark a need to compress the end of file */ + if (fi && (ni->flags & FILE_ATTR_COMPRESSED)) { + state |= CLOSE_COMPRESSED; + } +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* mark a future need to fixup encrypted inode */ + if (fi + && ctx->efs_raw + && (ni->flags & FILE_ATTR_ENCRYPTED)) + state |= CLOSE_ENCRYPTED; +#endif /* HAVE_SETXATTR */ + if (fi && ctx->dmtime) + state |= CLOSE_DMTIME; + ntfs_inode_update_mbsname(dir_ni, name, ni->mft_no); + NInoSetDirty(ni); + e->ino = ni->mft_no; + e->generation = 1; + e->attr_timeout = ATTR_TIMEOUT; + e->entry_timeout = ENTRY_TIMEOUT; + res = ntfs_fuse_getstat(&security, ni, &e->attr); + /* + * closing ni requires access to dir_ni to + * synchronize the index, avoid double opening. + */ + if (ntfs_inode_close_in_dir(ni, dir_ni)) + set_fuse_error(&res); + ntfs_fuse_update_times(dir_ni, NTFS_UPDATE_MCTIME); + } else + res = -errno; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + } else + res = -errno; +#endif + +exit: + free(uname); + if (ntfs_inode_close(dir_ni)) + set_fuse_error(&res); + if (utarget) + free(utarget); + if ((res >= 0) && fi) { + of = (struct open_file*)malloc(sizeof(struct open_file)); + if (of) { + of->parent = 0; + of->ino = e->ino; + of->state = state; + of->next = ctx->open_files; + of->previous = (struct open_file*)NULL; + if (ctx->open_files) + ctx->open_files->previous = of; + ctx->open_files = of; + fi->fh = (long)of; + } + } + return res; +} + +static void ntfs_fuse_create_file(fuse_req_t req, fuse_ino_t parent, + const char *name, mode_t mode, + struct fuse_file_info *fi) +{ + int res; + struct fuse_entry_param entry; + + res = ntfs_fuse_create(req, parent, name, mode & (S_IFMT | 07777), + 0, &entry, NULL, fi); + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_create(req, &entry, fi); +} + +static void ntfs_fuse_mknod(fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, dev_t rdev) +{ + int res; + struct fuse_entry_param e; + + res = ntfs_fuse_create(req, parent, name, mode & (S_IFMT | 07777), + rdev, &e,NULL,(struct fuse_file_info*)NULL); + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_entry(req, &e); +} + +static void ntfs_fuse_symlink(fuse_req_t req, const char *target, + fuse_ino_t parent, const char *name) +{ + int res; + struct fuse_entry_param entry; + + res = ntfs_fuse_create(req, parent, name, S_IFLNK, 0, + &entry, target, (struct fuse_file_info*)NULL); + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_entry(req, &entry); +} + + +static int ntfs_fuse_newlink(fuse_req_t req __attribute__((unused)), + fuse_ino_t ino, fuse_ino_t newparent, + const char *newname, struct fuse_entry_param *e) +{ + ntfschar *uname = NULL; + ntfs_inode *dir_ni = NULL, *ni; + int res = 0, uname_len; + struct SECURITY_CONTEXT security; + + /* Open file for which create hard link. */ + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + res = -errno; + goto exit; + } + + /* Do not accept linking to a directory (except for renaming) */ + if (e && (ni->mrec->flags & MFT_RECORD_IS_DIRECTORY)) { + errno = EPERM; + res = -errno; + goto exit; + } + /* Generate unicode filename. */ + uname_len = ntfs_mbstoucs(newname, &uname); + if ((uname_len < 0) + || (ctx->windows_names + && ntfs_forbidden_names(ctx->vol,uname,uname_len,TRUE))) { + res = -errno; + goto exit; + } + /* Open parent directory. */ + dir_ni = ntfs_inode_open(ctx->vol, INODE(newparent)); + if (!dir_ni) { + res = -errno; + goto exit; + } + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* make sure the target parent directory is writeable */ + if (ntfs_fuse_fill_security_context(req, &security) + && !ntfs_allowed_access(&security,dir_ni,S_IWRITE + S_IEXEC)) + res = -EACCES; + else +#else + ntfs_fuse_fill_security_context(req, &security); +#endif + { + if (dir_ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + res = CALL_REPARSE_PLUGIN(dir_ni, link, + ni, uname, uname_len); + if (res < 0) + goto exit; +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; + goto exit; +#endif /* DISABLE_PLUGINS */ + } else { + if (ntfs_link(ni, dir_ni, uname, uname_len)) { + res = -errno; + goto exit; + } + } + ntfs_inode_update_mbsname(dir_ni, newname, ni->mft_no); + if (e) { + e->ino = ni->mft_no; + e->generation = 1; + e->attr_timeout = ATTR_TIMEOUT; + e->entry_timeout = ENTRY_TIMEOUT; + res = ntfs_fuse_getstat(&security, ni, &e->attr); + } + set_archive(ni); + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + ntfs_fuse_update_times(dir_ni, NTFS_UPDATE_MCTIME); + } +exit: + /* + * Must close dir_ni first otherwise ntfs_inode_sync_file_name(ni) + * may fail because ni may not be in parent's index on the disk yet. + */ + if (ntfs_inode_close(dir_ni)) + set_fuse_error(&res); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(uname); + return (res); +} + +static void ntfs_fuse_link(fuse_req_t req, fuse_ino_t ino, + fuse_ino_t newparent, const char *newname) +{ + struct fuse_entry_param entry; + int res; + + res = ntfs_fuse_newlink(req, ino, newparent, newname, &entry); + if (res) + fuse_reply_err(req, -res); + else + fuse_reply_entry(req, &entry); +} + +static int ntfs_fuse_rm(fuse_req_t req, fuse_ino_t parent, const char *name, + enum RM_TYPES rm_type __attribute__((unused))) +{ + ntfschar *uname = NULL; + ntfschar *ugname; + ntfs_inode *dir_ni = NULL, *ni = NULL; + int res = 0, uname_len; + int ugname_len; + u64 iref; + fuse_ino_t ino; + struct open_file *of; + char ghostname[GHOSTLTH]; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct SECURITY_CONTEXT security; +#endif +#if defined(__sun) && defined (__SVR4) + int isdir; +#endif /* defined(__sun) && defined (__SVR4) */ + + /* Deny removing from $Extend */ + if (parent == FILE_Extend) { + res = -EPERM; + goto exit; + } + /* Open parent directory. */ + dir_ni = ntfs_inode_open(ctx->vol, INODE(parent)); + if (!dir_ni) { + res = -errno; + goto exit; + } + /* Generate unicode filename. */ + uname_len = ntfs_mbstoucs(name, &uname); + if (uname_len < 0) { + res = -errno; + goto exit; + } + /* Open object for delete. */ + iref = ntfs_inode_lookup_by_mbsname(dir_ni, name); + if (iref == (u64)-1) { + res = -errno; + goto exit; + } + ino = (fuse_ino_t)MREF(iref); + /* deny unlinking metadata files */ + if (ino < FILE_first_user) { + res = -EPERM; + goto exit; + } + + ni = ntfs_inode_open(ctx->vol, ino); + if (!ni) { + res = -errno; + goto exit; + } + +#if defined(__sun) && defined (__SVR4) + /* on Solaris : deny unlinking directories */ + isdir = ni->mrec->flags & MFT_RECORD_IS_DIRECTORY; +#ifndef DISABLE_PLUGINS + /* get emulated type from plugin if available */ + if (ni->flags & FILE_ATTR_REPARSE_POINT) { + struct stat st; + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + /* Avoid double opening of parent directory */ + res = ntfs_inode_close(dir_ni); + if (res) + goto exit; + dir_ni = (ntfs_inode*)NULL; + res = CALL_REPARSE_PLUGIN(ni, getattr, &st); + if (res) + goto exit; + isdir = S_ISDIR(st.st_mode); + dir_ni = ntfs_inode_open(ctx->vol, INODE(parent)); + if (!dir_ni) { + res = -errno; + goto exit; + } + } +#endif /* DISABLE_PLUGINS */ + if (rm_type == (isdir ? RM_LINK : RM_DIR)) { + errno = (isdir ? EISDIR : ENOTDIR); + res = -errno; + goto exit; + } +#endif /* defined(__sun) && defined (__SVR4) */ + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* JPA deny unlinking if directory is not writable and executable */ + if (ntfs_fuse_fill_security_context(req, &security) + && !ntfs_allowed_dir_access(&security, dir_ni, ino, ni, + S_IEXEC + S_IWRITE + S_ISVTX)) { + errno = EACCES; + res = -errno; + goto exit; + } +#endif + /* + * We keep one open_file record per opening, to avoid + * having to check the list of open files when opening + * and closing (which are more frequent than unlinking). + * As a consequence, we may have to create several + * ghosts names for the same file. + * The file may have been opened with a different name + * in a different parent directory. The ghost is + * nevertheless created in the parent directory of the + * name being unlinked, and permissions to do so are the + * same as required for unlinking. + */ + for (of=ctx->open_files; of; of = of->next) { + if ((of->ino == ino) && !(of->state & CLOSE_GHOST)) { + /* file was open, create a ghost in unlink parent */ + ntfs_inode *gni; + u64 gref; + + /* ni has to be closed for linking ghost */ + if (ni) { + if (ntfs_inode_close(ni)) { + res = -errno; + goto exit; + } + ni = (ntfs_inode*)NULL; + } + of->state |= CLOSE_GHOST; + of->parent = parent; + of->ghost = ++ctx->latest_ghost; + sprintf(ghostname,ghostformat,of->ghost); + /* Generate unicode filename. */ + ugname = (ntfschar*)NULL; + ugname_len = ntfs_mbstoucs(ghostname, &ugname); + if (ugname_len < 0) { + res = -errno; + goto exit; + } + /* sweep existing ghost if any, ignoring errors */ + gref = ntfs_inode_lookup_by_mbsname(dir_ni, ghostname); + if (gref != (u64)-1) { + gni = ntfs_inode_open(ctx->vol, MREF(gref)); + ntfs_delete(ctx->vol, (char*)NULL, gni, dir_ni, + ugname, ugname_len); + /* ntfs_delete() always closes gni and dir_ni */ + dir_ni = (ntfs_inode*)NULL; + } else { + if (ntfs_inode_close(dir_ni)) { + res = -errno; + goto out; + } + dir_ni = (ntfs_inode*)NULL; + } + free(ugname); + res = ntfs_fuse_newlink(req, of->ino, parent, ghostname, + (struct fuse_entry_param*)NULL); + if (res) + goto out; + /* now reopen then parent directory */ + dir_ni = ntfs_inode_open(ctx->vol, INODE(parent)); + if (!dir_ni) { + res = -errno; + goto exit; + } + } + } + if (!ni) { + ni = ntfs_inode_open(ctx->vol, ino); + if (!ni) { + res = -errno; + goto exit; + } + } + if (dir_ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + res = CALL_REPARSE_PLUGIN(dir_ni, unlink, (char*)NULL, + ni, uname, uname_len); +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + } else + if (ntfs_delete(ctx->vol, (char*)NULL, ni, dir_ni, + uname, uname_len)) + res = -errno; + /* ntfs_delete() always closes ni and dir_ni */ + ni = dir_ni = NULL; +exit: + if (ntfs_inode_close(ni) && !res) + res = -errno; + if (ntfs_inode_close(dir_ni) && !res) + res = -errno; +out : + free(uname); + return res; +} + +static void ntfs_fuse_unlink(fuse_req_t req, fuse_ino_t parent, + const char *name) +{ + int res; + + res = ntfs_fuse_rm(req, parent, name, RM_LINK); + if (res) + fuse_reply_err(req, -res); + else + fuse_reply_err(req, 0); +} + +static int ntfs_fuse_safe_rename(fuse_req_t req, fuse_ino_t ino, + fuse_ino_t parent, const char *name, fuse_ino_t xino, + fuse_ino_t newparent, const char *newname, + const char *tmp) +{ + int ret; + + ntfs_log_trace("Entering\n"); + + ret = ntfs_fuse_newlink(req, xino, newparent, tmp, + (struct fuse_entry_param*)NULL); + if (ret) + return ret; + + ret = ntfs_fuse_rm(req, newparent, newname, RM_ANY); + if (!ret) { + + ret = ntfs_fuse_newlink(req, ino, newparent, newname, + (struct fuse_entry_param*)NULL); + if (ret) + goto restore; + + ret = ntfs_fuse_rm(req, parent, name, RM_ANY); + if (ret) { + if (ntfs_fuse_rm(req, newparent, newname, RM_ANY)) + goto err; + goto restore; + } + } + + goto cleanup; +restore: + if (ntfs_fuse_newlink(req, xino, newparent, newname, + (struct fuse_entry_param*)NULL)) { +err: + ntfs_log_perror("Rename failed. Existing file '%s' was renamed " + "to '%s'", newname, tmp); + } else { +cleanup: + /* + * Condition for this unlink has already been checked in + * "ntfs_fuse_rename_existing_dest()", so it should never + * fail (unless concurrent access to directories when fuse + * is multithreaded) + */ + if (ntfs_fuse_rm(req, newparent, tmp, RM_ANY) < 0) + ntfs_log_perror("Rename failed. Existing file '%s' still present " + "as '%s'", newname, tmp); + } + return ret; +} + +static int ntfs_fuse_rename_existing_dest(fuse_req_t req, fuse_ino_t ino, + fuse_ino_t parent, const char *name, + fuse_ino_t xino, fuse_ino_t newparent, + const char *newname) +{ + int ret, len; + char *tmp; + const char *ext = ".ntfs-3g-"; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + ntfs_inode *newdir_ni; + struct SECURITY_CONTEXT security; +#endif + + ntfs_log_trace("Entering\n"); + + len = strlen(newname) + strlen(ext) + 10 + 1; /* wc(str(2^32)) + \0 */ + tmp = (char*)ntfs_malloc(len); + if (!tmp) + return -errno; + + ret = snprintf(tmp, len, "%s%s%010d", newname, ext, ++ntfs_sequence); + if (ret != len - 1) { + ntfs_log_error("snprintf failed: %d != %d\n", ret, len - 1); + ret = -EOVERFLOW; + } else { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * Make sure existing dest can be removed. + * This is only needed if parent directory is + * sticky, because in this situation condition + * for unlinking is different from condition for + * linking + */ + newdir_ni = ntfs_inode_open(ctx->vol, INODE(newparent)); + if (newdir_ni) { + if (!ntfs_fuse_fill_security_context(req,&security) + || ntfs_allowed_dir_access(&security, newdir_ni, + xino, (ntfs_inode*)NULL, + S_IEXEC + S_IWRITE + S_ISVTX)) { + if (ntfs_inode_close(newdir_ni)) + ret = -errno; + else + ret = ntfs_fuse_safe_rename(req, ino, + parent, name, xino, + newparent, newname, + tmp); + } else { + ntfs_inode_close(newdir_ni); + ret = -EACCES; + } + } else + ret = -errno; +#else + ret = ntfs_fuse_safe_rename(req, ino, parent, name, + xino, newparent, newname, tmp); +#endif + } + free(tmp); + return ret; +} + +static void ntfs_fuse_rename(fuse_req_t req, fuse_ino_t parent, + const char *name, fuse_ino_t newparent, + const char *newname) +{ + int ret; + fuse_ino_t ino; + fuse_ino_t xino; + ntfs_inode *ni; + + ntfs_log_debug("rename: old: '%s' new: '%s'\n", name, newname); + + /* + * FIXME: Rename should be atomic. + */ + + ino = ntfs_fuse_inode_lookup(parent, name); + if (ino == (fuse_ino_t)-1) { + ret = -errno; + goto out; + } + /* Check whether target is present */ + xino = ntfs_fuse_inode_lookup(newparent, newname); + if (xino != (fuse_ino_t)-1) { + /* + * Target exists : no need to check whether it + * designates the same inode, this has already + * been checked (by fuse ?) + */ + ni = ntfs_inode_open(ctx->vol, INODE(xino)); + if (!ni) + ret = -errno; + else { + ret = ntfs_check_empty_dir(ni); + if (ret < 0) { + ret = -errno; + ntfs_inode_close(ni); + goto out; + } + + if (ntfs_inode_close(ni)) { + set_fuse_error(&ret); + goto out; + } + ret = ntfs_fuse_rename_existing_dest(req, ino, parent, + name, xino, newparent, newname); + } + } else { + /* target does not exist */ + ret = ntfs_fuse_newlink(req, ino, newparent, newname, + (struct fuse_entry_param*)NULL); + if (ret) + goto out; + + ret = ntfs_fuse_rm(req, parent, name, RM_ANY); + if (ret) + ntfs_fuse_rm(req, newparent, newname, RM_ANY); + } +out: + if (ret) + fuse_reply_err(req, -ret); + else + fuse_reply_err(req, 0); +} + +static void ntfs_fuse_release(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + struct open_file *of; + char ghostname[GHOSTLTH]; + int res; + + of = (struct open_file*)(long)fi->fh; + /* Only for marked descriptors there is something to do */ + if (!of + || !(of->state & (CLOSE_COMPRESSED | CLOSE_ENCRYPTED + | CLOSE_DMTIME | CLOSE_REPARSE))) { + res = 0; + goto out; + } + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + res = -errno; + goto exit; + } + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + res = CALL_REPARSE_PLUGIN(ni, release, &of->fi); + if (!res) { + goto stamps; + } +#else /* DISABLE_PLUGINS */ + /* Assume release() was not needed */ + res = 0; +#endif /* DISABLE_PLUGINS */ + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + res = -errno; + goto exit; + } + res = 0; + if (of->state & CLOSE_COMPRESSED) + res = ntfs_attr_pclose(na); +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + if (of->state & CLOSE_ENCRYPTED) + res = ntfs_efs_fixup_attribute(NULL, na); +#endif /* HAVE_SETXATTR */ +#ifndef DISABLE_PLUGINS +stamps : +#endif /* DISABLE_PLUGINS */ + if (of->state & CLOSE_DMTIME) + ntfs_inode_update_times(ni,NTFS_UPDATE_MCTIME); +exit: + if (na) + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); +out: + /* remove the associate ghost file (even if release failed) */ + if (of) { + if (of->state & CLOSE_GHOST) { + sprintf(ghostname,ghostformat,of->ghost); + ntfs_fuse_rm(req, of->parent, ghostname, RM_ANY); + } + /* remove from open files list */ + if (of->next) + of->next->previous = of->previous; + if (of->previous) + of->previous->next = of->next; + else + ctx->open_files = of->next; + free(of); + } + if (res) + fuse_reply_err(req, -res); + else + fuse_reply_err(req, 0); +} + +static void ntfs_fuse_mkdir(fuse_req_t req, fuse_ino_t parent, + const char *name, mode_t mode) +{ + int res; + struct fuse_entry_param entry; + + res = ntfs_fuse_create(req, parent, name, S_IFDIR | (mode & 07777), + 0, &entry, (char*)NULL, (struct fuse_file_info*)NULL); + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_entry(req, &entry); +} + +static void ntfs_fuse_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name) +{ + int res; + + res = ntfs_fuse_rm(req, parent, name, RM_DIR); + if (res) + fuse_reply_err(req, -res); + else + fuse_reply_err(req, 0); +} + +static void ntfs_fuse_fsync(fuse_req_t req, + fuse_ino_t ino __attribute__((unused)), + int type __attribute__((unused)), + struct fuse_file_info *fi __attribute__((unused))) +{ + /* sync the full device */ + if (ntfs_device_sync(ctx->vol->dev)) + fuse_reply_err(req, errno); + else + fuse_reply_err(req, 0); +} + +#if defined(FUSE_INTERNAL) || (FUSE_VERSION >= 28) +static void ntfs_fuse_ioctl(fuse_req_t req __attribute__((unused)), + fuse_ino_t ino __attribute__((unused)), + int cmd, void *arg, + struct fuse_file_info *fi __attribute__((unused)), + unsigned flags, const void *data, + size_t in_bufsz, size_t out_bufsz) +{ + ntfs_inode *ni; + char *buf = (char*)NULL; + int bufsz; + int ret = 0; + + if (flags & FUSE_IOCTL_COMPAT) { + ret = -ENOSYS; + } else { + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + ret = -errno; + goto fail; + } + bufsz = (in_bufsz > out_bufsz ? in_bufsz : out_bufsz); + if (bufsz) { + buf = ntfs_malloc(bufsz); + if (!buf) { + ret = ENOMEM; + goto fail; + } + memcpy(buf, data, in_bufsz); + } + /* + * Linux defines the request argument of ioctl() to be an + * unsigned long, which fuse 2.x forwards as a signed int + * into which the request sometimes does not fit. + * So we must expand the value and make sure it is not + * sign-extended. + */ + ret = ntfs_ioctl(ni, (unsigned int)cmd, arg, flags, buf); + if (ntfs_inode_close (ni)) + set_fuse_error(&ret); + } + if (ret) +fail : + fuse_reply_err(req, -ret); + else + fuse_reply_ioctl(req, 0, buf, out_bufsz); + if (buf) + free(buf); +} +#endif /* defined(FUSE_INTERNAL) || (FUSE_VERSION >= 28) */ + +static void ntfs_fuse_bmap(fuse_req_t req, fuse_ino_t ino, size_t blocksize, + uint64_t vidx) +{ + ntfs_inode *ni; + ntfs_attr *na; + LCN lcn; + uint64_t lidx = 0; + int ret = 0; + int cl_per_bl = ctx->vol->cluster_size / blocksize; + + if (blocksize > ctx->vol->cluster_size) { + ret = -EINVAL; + goto done; + } + + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + ret = -errno; + goto done; + } + + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + ret = -errno; + goto close_inode; + } + + if ((na->data_flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED)) + || !NAttrNonResident(na)) { + ret = -EINVAL; + goto close_attr; + } + + if (ntfs_attr_map_whole_runlist(na)) { + ret = -errno; + goto close_attr; + } + + lcn = ntfs_rl_vcn_to_lcn(na->rl, vidx / cl_per_bl); + lidx = (lcn > 0) ? lcn * cl_per_bl + vidx % cl_per_bl : 0; + +close_attr: + ntfs_attr_close(na); +close_inode: + if (ntfs_inode_close(ni)) + set_fuse_error(&ret); +done : + if (ret < 0) + fuse_reply_err(req, -ret); + else + fuse_reply_bmap(req, lidx); +} + +#ifdef HAVE_SETXATTR + +/* + * Name space identifications and prefixes + */ + +enum { + XATTRNS_NONE, + XATTRNS_USER, + XATTRNS_SYSTEM, + XATTRNS_SECURITY, + XATTRNS_TRUSTED, + XATTRNS_OPEN +} ; + +/* + * Check whether access to internal data as an extended + * attribute in system name space is allowed + * + * Returns pointer to inode if allowed, + * NULL and errno set if not allowed + */ + +static ntfs_inode *ntfs_check_access_xattr(fuse_req_t req, + struct SECURITY_CONTEXT *security, + fuse_ino_t ino, int attr, BOOL setting) +{ + ntfs_inode *dir_ni; + ntfs_inode *ni; + BOOL foracl; + BOOL bad; + mode_t acctype; + + ni = (ntfs_inode*)NULL; + foracl = (attr == XATTR_POSIX_ACC) + || (attr == XATTR_POSIX_DEF); + /* + * When accessing Posix ACL, return unsupported if ACL + * were disabled or no user mapping has been defined, + * or trying to change a Windows-inherited ACL. + * However no error will be returned to getfacl + */ + if (((!ntfs_fuse_fill_security_context(req, security) + || (ctx->secure_flags + & ((1 << SECURITY_DEFAULT) | (1 << SECURITY_RAW)))) + || !(ctx->secure_flags & (1 << SECURITY_ACL)) + || (setting && ctx->inherit)) + && foracl) { + if (ctx->silent && !ctx->security.mapping[MAPUSERS]) + errno = 0; + else + errno = EOPNOTSUPP; + } else { + /* + * parent directory must be executable, and + * for setting a DOS name it must be writeable + */ + if (setting && (attr == XATTR_NTFS_DOS_NAME)) + acctype = S_IEXEC | S_IWRITE; + else + acctype = S_IEXEC; + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + /* basic access was checked previously in a lookup */ + if (ni && (acctype != S_IEXEC)) { + bad = FALSE; + /* do not reopen root */ + if (ni->mft_no == FILE_root) { + /* forbid getting/setting names on root */ + if ((attr == XATTR_NTFS_DOS_NAME) + || !ntfs_real_allowed_access(security, + ni, acctype)) + bad = TRUE; + } else { + dir_ni = ntfs_dir_parent_inode(ni); + if (dir_ni) { + if (!ntfs_real_allowed_access(security, + dir_ni, acctype)) + bad = TRUE; + if (ntfs_inode_close(dir_ni)) + bad = TRUE; + } else + bad = TRUE; + } + if (bad) { + ntfs_inode_close(ni); + ni = (ntfs_inode*)NULL; + } + } + } + return (ni); +} + +/* + * Determine the name space of an extended attribute + */ + +static int xattr_namespace(const char *name) +{ + int namespace; + + if (ctx->streams == NF_STREAMS_INTERFACE_XATTR) { + namespace = XATTRNS_NONE; + if (!strncmp(name, nf_ns_user_prefix, + nf_ns_user_prefix_len) + && (strlen(name) != (size_t)nf_ns_user_prefix_len)) + namespace = XATTRNS_USER; + else if (!strncmp(name, nf_ns_system_prefix, + nf_ns_system_prefix_len) + && (strlen(name) != (size_t)nf_ns_system_prefix_len)) + namespace = XATTRNS_SYSTEM; + else if (!strncmp(name, nf_ns_security_prefix, + nf_ns_security_prefix_len) + && (strlen(name) != (size_t)nf_ns_security_prefix_len)) + namespace = XATTRNS_SECURITY; + else if (!strncmp(name, nf_ns_trusted_prefix, + nf_ns_trusted_prefix_len) + && (strlen(name) != (size_t)nf_ns_trusted_prefix_len)) + namespace = XATTRNS_TRUSTED; + } else + namespace = XATTRNS_OPEN; + return (namespace); +} + +/* + * Fix the prefix of an extended attribute + */ + +static int fix_xattr_prefix(const char *name, int namespace, ntfschar **lename) +{ + int len; + char *prefixed; + + *lename = (ntfschar*)NULL; + switch (namespace) { + case XATTRNS_USER : + /* + * user name space : remove user prefix + */ + len = ntfs_mbstoucs(name + nf_ns_user_prefix_len, lename); + break; + case XATTRNS_SYSTEM : + case XATTRNS_SECURITY : + case XATTRNS_TRUSTED : + /* + * security, trusted and unmapped system name spaces : + * insert ntfs-3g prefix + */ + prefixed = (char*)ntfs_malloc(strlen(xattr_ntfs_3g) + + strlen(name) + 1); + if (prefixed) { + strcpy(prefixed,xattr_ntfs_3g); + strcat(prefixed,name); + len = ntfs_mbstoucs(prefixed, lename); + free(prefixed); + } else + len = -1; + break; + case XATTRNS_OPEN : + /* + * in open name space mode : do no fix prefix + */ + len = ntfs_mbstoucs(name, lename); + break; + default : + len = -1; + } + return (len); +} + +static void ntfs_fuse_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) +{ + ntfs_attr_search_ctx *actx = NULL; + ntfs_inode *ni; + char *list = (char*)NULL; + int ret = 0; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct SECURITY_CONTEXT security; +#endif + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + ntfs_fuse_fill_security_context(req, &security); +#endif + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + ret = -errno; + goto out; + } + /* Return with no result for symlinks, fifo, etc. */ + if (!user_xattrs_allowed(ctx, ni)) + goto exit; + /* otherwise file must be readable */ +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (!ntfs_allowed_access(&security,ni,S_IREAD)) { + ret = -EACCES; + goto exit; + } +#endif + actx = ntfs_attr_get_search_ctx(ni, NULL); + if (!actx) { + ret = -errno; + goto exit; + } + if (size) { + list = (char*)malloc(size); + if (!list) { + ret = -errno; + goto exit; + } + } + + if ((ctx->streams == NF_STREAMS_INTERFACE_XATTR) + || (ctx->streams == NF_STREAMS_INTERFACE_OPENXATTR)) { + ret = ntfs_fuse_listxattr_common(ni, actx, list, size, + ctx->streams == NF_STREAMS_INTERFACE_XATTR); + if (ret < 0) + goto exit; + } + if (errno != ENOENT) + ret = -errno; +exit: + if (actx) + ntfs_attr_put_search_ctx(actx); + if (ntfs_inode_close(ni)) + set_fuse_error(&ret); +out : + if (ret < 0) + fuse_reply_err(req, -ret); + else + if (size) + fuse_reply_buf(req, list, ret); + else + fuse_reply_xattr(req, ret); + free(list); +} + +#if defined(__APPLE__) || defined(__DARWIN__) +static void ntfs_fuse_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, + size_t size, uint32_t position) +#else +static void ntfs_fuse_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, + size_t size) +#endif +{ +#if !(defined(__APPLE__) || defined(__DARWIN__)) + static const unsigned int position = 0U; +#endif + + ntfs_inode *ni; + ntfs_inode *dir_ni; + ntfs_attr *na = NULL; + char *value = (char*)NULL; + ntfschar *lename = (ntfschar*)NULL; + int lename_len; + int res; + s64 rsize; + enum SYSTEMXATTRS attr; + int namespace; + struct SECURITY_CONTEXT security; + +#if defined(__APPLE__) || defined(__DARWIN__) + /* If the attribute is not a resource fork attribute and the position + * parameter is non-zero, we return with EINVAL as requesting position + * is not permitted for non-resource fork attributes. */ + if (position && strcmp(name, XATTR_RESOURCEFORK_NAME)) { + fuse_reply_err(req, EINVAL); + return; + } +#endif + + attr = ntfs_xattr_system_type(name,ctx->vol); + if (attr != XATTR_UNMAPPED) { + /* + * hijack internal data and ACL retrieval, whatever + * mode was selected for xattr (from the user's + * point of view, ACLs are not xattr) + */ + if (size) + value = (char*)ntfs_malloc(size); +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (!size || value) { + ni = ntfs_check_access_xattr(req, &security, ino, + attr, FALSE); + if (ni) { + if (ntfs_allowed_access(&security,ni,S_IREAD)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = ntfs_dir_parent_inode(ni); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_getxattr(&security, + attr, ni, dir_ni, value, size); + if (dir_ni && ntfs_inode_close(dir_ni)) + set_fuse_error(&res); + } else + res = -errno; + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } else + res = -errno; +#else + /* + * Standard access control has been done by fuse/kernel + */ + if (!size || value) { + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (ni) { + /* user mapping not mandatory */ + ntfs_fuse_fill_security_context(req, &security); + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = ntfs_dir_parent_inode(ni); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_getxattr(&security, + attr, ni, dir_ni, value, size); + if (dir_ni && ntfs_inode_close(dir_ni)) + set_fuse_error(&res); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } else + res = -errno; +#endif + } else + res = -errno; + if (res < 0) + fuse_reply_err(req, -res); + else + if (size) + fuse_reply_buf(req, value, res); + else + fuse_reply_xattr(req, res); + free(value); + return; + } + if (ctx->streams == NF_STREAMS_INTERFACE_NONE) { + res = -EOPNOTSUPP; + goto out; + } + namespace = xattr_namespace(name); + if (namespace == XATTRNS_NONE) { + res = -EOPNOTSUPP; + goto out; + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + ntfs_fuse_fill_security_context(req,&security); + /* trusted only readable by root */ + if ((namespace == XATTRNS_TRUSTED) + && security.uid) { + res = -NTFS_NOXATTR_ERRNO; + goto out; + } +#endif + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + res = -errno; + goto out; + } + /* Return with no result for symlinks, fifo, etc. */ + if (!user_xattrs_allowed(ctx, ni)) { + res = -NTFS_NOXATTR_ERRNO; + goto exit; + } + /* otherwise file must be readable */ +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (!ntfs_allowed_access(&security, ni, S_IREAD)) { + res = -errno; + goto exit; + } +#endif + lename_len = fix_xattr_prefix(name, namespace, &lename); + if (lename_len == -1) { + res = -errno; + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, lename, lename_len); + if (!na) { + res = -NTFS_NOXATTR_ERRNO; + goto exit; + } + rsize = na->data_size; + if (ctx->efs_raw + && rsize + && (na->data_flags & ATTR_IS_ENCRYPTED) + && NAttrNonResident(na)) + rsize = ((na->data_size + 511) & ~511) + 2; + rsize -= position; + if (size) { + if (size >= (size_t)rsize) { + value = (char*)ntfs_malloc(rsize); + if (value) + res = ntfs_attr_pread(na, position, rsize, + value); + if (!value || (res != rsize)) + res = -errno; + } else + res = -ERANGE; + } else + res = rsize; +exit: + if (na) + ntfs_attr_close(na); + free(lename); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + +out : + if (res < 0) + fuse_reply_err(req, -res); + else + if (size) + fuse_reply_buf(req, value, res); + else + fuse_reply_xattr(req, res); + free(value); +} + +#if defined(__APPLE__) || defined(__DARWIN__) +static void ntfs_fuse_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, + const char *value, size_t size, int flags, + uint32_t position) +#else +static void ntfs_fuse_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, + const char *value, size_t size, int flags) +#endif +{ +#if !(defined(__APPLE__) || defined(__DARWIN__)) + static const unsigned int position = 0U; +#else + BOOL is_resource_fork; +#endif + + ntfs_inode *ni; + ntfs_inode *dir_ni; + ntfs_attr *na = NULL; + ntfschar *lename = NULL; + int res, lename_len; + size_t total; + s64 part; + enum SYSTEMXATTRS attr; + int namespace; + struct SECURITY_CONTEXT security; + +#if defined(__APPLE__) || defined(__DARWIN__) + /* If the attribute is not a resource fork attribute and the position + * parameter is non-zero, we return with EINVAL as requesting position + * is not permitted for non-resource fork attributes. */ + is_resource_fork = strcmp(name, XATTR_RESOURCEFORK_NAME) ? FALSE : TRUE; + if (position && !is_resource_fork) { + fuse_reply_err(req, EINVAL); + return; + } +#endif + + attr = ntfs_xattr_system_type(name,ctx->vol); + if (attr != XATTR_UNMAPPED) { + /* + * hijack internal data and ACL setting, whatever + * mode was selected for xattr (from the user's + * point of view, ACLs are not xattr) + * Note : ctime updated on successful settings + */ +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + ni = ntfs_check_access_xattr(req,&security,ino,attr,TRUE); + if (ni) { + if (ntfs_allowed_as_owner(&security, ni)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = ntfs_dir_parent_inode(ni); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_setxattr(&security, + attr, ni, dir_ni, value, size, flags); + /* never have to close dir_ni */ + if (res) + res = -errno; + } else + res = -errno; + if (attr != XATTR_NTFS_DOS_NAME) { + if (!res) + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } else + res = -errno; +#else + /* creation of a new name is not controlled by fuse */ + if (attr == XATTR_NTFS_DOS_NAME) + ni = ntfs_check_access_xattr(req, &security, + ino, attr, TRUE); + else + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (ni) { + /* + * user mapping is not mandatory + * if defined, only owner is allowed + */ + if (!ntfs_fuse_fill_security_context(req, &security) + || ntfs_allowed_as_owner(&security, ni)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = ntfs_dir_parent_inode(ni); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_setxattr(&security, + attr, ni, dir_ni, value, size, flags); + /* never have to close dir_ni */ + if (res) + res = -errno; + } else + res = -errno; + if (attr != XATTR_NTFS_DOS_NAME) { + if (!res) + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } else + res = -errno; +#endif +#if CACHEING && !defined(FUSE_INTERNAL) && FUSE_VERSION >= 28 + /* + * Most of system xattr settings cause changes to some + * file attribute (st_mode, st_nlink, st_mtime, etc.), + * so we must invalidate cached data when cacheing is + * in use (not possible with internal fuse or external + * fuse before 2.8) + */ + if ((res >= 0) + && fuse_lowlevel_notify_inval_inode(ctx->fc, ino, -1, 0)) + res = -errno; +#endif + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_err(req, 0); + return; + } + if ((ctx->streams != NF_STREAMS_INTERFACE_XATTR) + && (ctx->streams != NF_STREAMS_INTERFACE_OPENXATTR)) { + res = -EOPNOTSUPP; + goto out; + } + namespace = xattr_namespace(name); + if (namespace == XATTRNS_NONE) { + res = -EOPNOTSUPP; + goto out; + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + ntfs_fuse_fill_security_context(req,&security); + /* security and trusted only settable by root */ + if (((namespace == XATTRNS_SECURITY) + || (namespace == XATTRNS_TRUSTED)) + && security.uid) { + res = -EPERM; + goto out; + } +#endif + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + res = -errno; + goto out; + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + switch (namespace) { + case XATTRNS_SECURITY : + case XATTRNS_TRUSTED : + if (security.uid) { + res = -EPERM; + goto exit; + } + break; + case XATTRNS_SYSTEM : + if (!ntfs_allowed_as_owner(&security, ni)) { + res = -EACCES; + goto exit; + } + break; + default : + /* User xattr not allowed for symlinks, fifo, etc. */ + if (!user_xattrs_allowed(ctx, ni)) { + res = -EPERM; + goto exit; + } + if (!ntfs_allowed_access(&security,ni,S_IWRITE)) { + res = -EACCES; + goto exit; + } + break; + } +#else + /* User xattr not allowed for symlinks, fifo, etc. */ + if ((namespace == XATTRNS_USER) + && !user_xattrs_allowed(ctx, ni)) { + res = -EPERM; + goto exit; + } +#endif + lename_len = fix_xattr_prefix(name, namespace, &lename); + if ((lename_len == -1) + || (ctx->windows_names + && ntfs_forbidden_chars(lename,lename_len,TRUE))) { + res = -errno; + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, lename, lename_len); + if (na && flags == XATTR_CREATE) { + res = -EEXIST; + goto exit; + } + if (!na) { + if (flags == XATTR_REPLACE) { + res = -NTFS_NOXATTR_ERRNO; + goto exit; + } + if (ntfs_attr_add(ni, AT_DATA, lename, lename_len, NULL, 0)) { + res = -errno; + goto exit; + } + if (!(ni->flags & FILE_ATTR_ARCHIVE)) { + set_archive(ni); + NInoFileNameSetDirty(ni); + } + na = ntfs_attr_open(ni, AT_DATA, lename, lename_len); + if (!na) { + res = -errno; + goto exit; + } +#if defined(__APPLE__) || defined(__DARWIN__) + } else if (is_resource_fork) { + /* In macOS, the resource fork is a special case. It doesn't + * ever shrink (it would have to be removed and re-added). */ +#endif + } else { + /* currently compressed streams can only be wiped out */ + if (ntfs_attr_truncate(na, (s64)0 /* size */)) { + res = -errno; + goto exit; + } + } + total = 0; + res = 0; + if (size) { + do { + part = ntfs_attr_pwrite(na, position + total, + size - total, &value[total]); + if (part > 0) + total += part; + } while ((part > 0) && (total < size)); + } + if ((total != size) || ntfs_attr_pclose(na)) + res = -errno; + else { + if (ctx->efs_raw + && (ni->flags & FILE_ATTR_ENCRYPTED)) { + if (ntfs_efs_fixup_attribute(NULL,na)) + res = -errno; + } + } + if (!res) { + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + if (!(ni->flags & FILE_ATTR_ARCHIVE)) { + set_archive(ni); + NInoFileNameSetDirty(ni); + } + } +exit: + if (na) + ntfs_attr_close(na); + free(lename); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); +out : + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_err(req, 0); +} + +static void ntfs_fuse_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name) +{ + ntfs_inode *ni; + ntfs_inode *dir_ni; + ntfschar *lename = NULL; + int res = 0, lename_len; + enum SYSTEMXATTRS attr; + int namespace; + struct SECURITY_CONTEXT security; + + attr = ntfs_xattr_system_type(name,ctx->vol); + if (attr != XATTR_UNMAPPED) { + switch (attr) { + /* + * Removal of NTFS ACL, ATTRIB, EFSINFO or TIMES + * is never allowed + */ + case XATTR_NTFS_ACL : + case XATTR_NTFS_ATTRIB : + case XATTR_NTFS_ATTRIB_BE : + case XATTR_NTFS_EFSINFO : + case XATTR_NTFS_TIMES : + case XATTR_NTFS_TIMES_BE : + case XATTR_NTFS_CRTIME : + case XATTR_NTFS_CRTIME_BE : + res = -EPERM; + break; + default : +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + ni = ntfs_check_access_xattr(req, &security, ino, + attr,TRUE); + if (ni) { + if (ntfs_allowed_as_owner(&security, ni)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = ntfs_dir_parent_inode(ni); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_removexattr(&security, + attr, ni, dir_ni); + if (res) + res = -errno; + /* never have to close dir_ni */ + } else + res = -errno; + if (attr != XATTR_NTFS_DOS_NAME) { + if (!res) + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } else + res = -errno; +#else + /* creation of a new name is not controlled by fuse */ + if (attr == XATTR_NTFS_DOS_NAME) + ni = ntfs_check_access_xattr(req, &security, + ino, attr, TRUE); + else + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (ni) { + /* + * user mapping is not mandatory + * if defined, only owner is allowed + */ + if (!ntfs_fuse_fill_security_context(req, &security) + || ntfs_allowed_as_owner(&security, ni)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = ntfs_dir_parent_inode(ni); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_removexattr(&security, + attr, ni, dir_ni); + /* never have to close dir_ni */ + if (res) + res = -errno; + } else + res = -errno; + if (attr != XATTR_NTFS_DOS_NAME) { + if (!res) + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } else + res = -errno; +#endif +#if CACHEING && !defined(FUSE_INTERNAL) && FUSE_VERSION >= 28 + /* + * Some allowed system xattr removals cause changes to + * some file attribute (st_mode, st_nlink, etc.), + * so we must invalidate cached data when cacheing is + * in use (not possible with internal fuse or external + * fuse before 2.8) + */ + if ((res >= 0) + && fuse_lowlevel_notify_inval_inode(ctx->fc, + ino, -1, 0)) + res = -errno; +#endif + break; + } + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_err(req, 0); + return; + } + if ((ctx->streams != NF_STREAMS_INTERFACE_XATTR) + && (ctx->streams != NF_STREAMS_INTERFACE_OPENXATTR)) { + res = -EOPNOTSUPP; + goto out; + } + namespace = xattr_namespace(name); + if (namespace == XATTRNS_NONE) { + res = -EOPNOTSUPP; + goto out; + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + ntfs_fuse_fill_security_context(req,&security); + /* security and trusted only settable by root */ + if (((namespace == XATTRNS_SECURITY) + || (namespace == XATTRNS_TRUSTED)) + && security.uid) { + res = -EACCES; + goto out; + } +#endif + ni = ntfs_inode_open(ctx->vol, INODE(ino)); + if (!ni) { + res = -errno; + goto out; + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + switch (namespace) { + case XATTRNS_SECURITY : + case XATTRNS_TRUSTED : + if (security.uid) { + res = -EPERM; + goto exit; + } + break; + case XATTRNS_SYSTEM : + if (!ntfs_allowed_as_owner(&security, ni)) { + res = -EACCES; + goto exit; + } + break; + default : + /* User xattr not allowed for symlinks, fifo, etc. */ + if (!user_xattrs_allowed(ctx, ni)) { + res = -EPERM; + goto exit; + } + if (!ntfs_allowed_access(&security,ni,S_IWRITE)) { + res = -EACCES; + goto exit; + } + break; + } +#else + /* User xattr not allowed for symlinks, fifo, etc. */ + if ((namespace == XATTRNS_USER) + && !user_xattrs_allowed(ctx, ni)) { + res = -EPERM; + goto exit; + } +#endif + lename_len = fix_xattr_prefix(name, namespace, &lename); + if (lename_len == -1) { + res = -errno; + goto exit; + } + if (ntfs_attr_remove(ni, AT_DATA, lename, lename_len)) { + if (errno == ENOENT) + errno = NTFS_NOXATTR_ERRNO; + res = -errno; + } + if (!res) { + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + if (!(ni->flags & FILE_ATTR_ARCHIVE)) { + set_archive(ni); + NInoFileNameSetDirty(ni); + } + } +exit: + free(lename); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); +out : + if (res < 0) + fuse_reply_err(req, -res); + else + fuse_reply_err(req, 0); + return; + +} + +#else +#if POSIXACLS +#error "Option inconsistency : POSIXACLS requires SETXATTR" +#endif +#endif /* HAVE_SETXATTR */ + +#ifndef DISABLE_PLUGINS +static void register_internal_reparse_plugins(void) +{ + static const plugin_operations_t ops = { + .getattr = junction_getstat, + .readlink = junction_readlink, + } ; + static const plugin_operations_t wsl_ops = { + .getattr = wsl_getstat, + } ; + register_reparse_plugin(ctx, IO_REPARSE_TAG_MOUNT_POINT, + &ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_SYMLINK, + &ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_LX_SYMLINK, + &ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_AF_UNIX, + &wsl_ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_LX_FIFO, + &wsl_ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_LX_CHR, + &wsl_ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_LX_BLK, + &wsl_ops, (void*)NULL); +} +#endif /* DISABLE_PLUGINS */ + +static void ntfs_close(void) +{ + struct SECURITY_CONTEXT security; + + if (!ctx) + return; + + if (!ctx->vol) + return; + + if (ctx->mounted) { + ntfs_log_info("Unmounting %s (%s)\n", opts.device, + ctx->vol->vol_name); + if (ntfs_fuse_fill_security_context((fuse_req_t)NULL, &security)) { + if (ctx->seccache && ctx->seccache->head.p_reads) { + ntfs_log_info("Permissions cache : %lu writes, " + "%lu reads, %lu.%1lu%% hits\n", + ctx->seccache->head.p_writes, + ctx->seccache->head.p_reads, + 100 * ctx->seccache->head.p_hits + / ctx->seccache->head.p_reads, + 1000 * ctx->seccache->head.p_hits + / ctx->seccache->head.p_reads % 10); + } + } + ntfs_destroy_security_context(&security); + } + + if (ntfs_umount(ctx->vol, FALSE)) + ntfs_log_perror("Failed to close volume %s", opts.device); + + ctx->vol = NULL; +} + +static void ntfs_fuse_destroy2(void *notused __attribute__((unused))) +{ + ntfs_close(); +} + +static struct fuse_lowlevel_ops ntfs_3g_ops = { + .lookup = ntfs_fuse_lookup, + .getattr = ntfs_fuse_getattr, + .readlink = ntfs_fuse_readlink, + .opendir = ntfs_fuse_opendir, + .readdir = ntfs_fuse_readdir, + .releasedir = ntfs_fuse_releasedir, + .open = ntfs_fuse_open, + .release = ntfs_fuse_release, + .read = ntfs_fuse_read, + .write = ntfs_fuse_write, + .setattr = ntfs_fuse_setattr, + .statfs = ntfs_fuse_statfs, + .create = ntfs_fuse_create_file, + .mknod = ntfs_fuse_mknod, + .symlink = ntfs_fuse_symlink, + .link = ntfs_fuse_link, + .unlink = ntfs_fuse_unlink, + .rename = ntfs_fuse_rename, + .mkdir = ntfs_fuse_mkdir, + .rmdir = ntfs_fuse_rmdir, + .fsync = ntfs_fuse_fsync, + .fsyncdir = ntfs_fuse_fsync, + .bmap = ntfs_fuse_bmap, + .destroy = ntfs_fuse_destroy2, +#if defined(FUSE_INTERNAL) || (FUSE_VERSION >= 28) + .ioctl = ntfs_fuse_ioctl, +#endif /* defined(FUSE_INTERNAL) || (FUSE_VERSION >= 28) */ +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + .access = ntfs_fuse_access, +#endif +#ifdef HAVE_SETXATTR + .getxattr = ntfs_fuse_getxattr, + .setxattr = ntfs_fuse_setxattr, + .removexattr = ntfs_fuse_removexattr, + .listxattr = ntfs_fuse_listxattr, +#endif /* HAVE_SETXATTR */ +#if 0 && (defined(__APPLE__) || defined(__DARWIN__)) /* Unfinished. */ + /* MacFUSE extensions. */ + .getxtimes = ntfs_macfuse_getxtimes, + .setcrtime = ntfs_macfuse_setcrtime, + .setbkuptime = ntfs_macfuse_setbkuptime, + .setchgtime = ntfs_macfuse_setchgtime, +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + .init = ntfs_init +}; + +static int ntfs_fuse_init(void) +{ + ctx = (ntfs_fuse_context_t*)ntfs_calloc(sizeof(ntfs_fuse_context_t)); + if (!ctx) + return -1; + + *ctx = (ntfs_fuse_context_t) { + .uid = getuid(), + .gid = getgid(), +#if defined(linux) + .streams = NF_STREAMS_INTERFACE_XATTR, +#else + .streams = NF_STREAMS_INTERFACE_NONE, +#endif + .atime = ATIME_RELATIVE, + .silent = TRUE, + .recover = TRUE + }; + return 0; +} + +static int ntfs_open(const char *device) +{ + unsigned long flags = 0; + ntfs_volume *vol; + + if (!ctx->blkdev) + flags |= NTFS_MNT_EXCLUSIVE; + if (ctx->ro) + flags |= NTFS_MNT_RDONLY; + else + if (!ctx->hiberfile) + flags |= NTFS_MNT_MAY_RDONLY; + if (ctx->recover) + flags |= NTFS_MNT_RECOVER; + if (ctx->hiberfile) + flags |= NTFS_MNT_IGNORE_HIBERFILE; + + ctx->vol = vol = ntfs_mount(device, flags); + if (!vol) { + ntfs_log_perror("Failed to mount '%s'", device); + goto err_out; + } + if (ctx->sync && ctx->vol->dev) + NDevSetSync(ctx->vol->dev); + if (ctx->compression) + NVolSetCompression(ctx->vol); + else + NVolClearCompression(ctx->vol); +#ifdef HAVE_SETXATTR + /* archivers must see hidden files */ + if (ctx->efs_raw) + ctx->hide_hid_files = FALSE; +#endif + if (ntfs_set_shown_files(ctx->vol, ctx->show_sys_files, + !ctx->hide_hid_files, ctx->hide_dot_files)) + goto err_out; + + if (ctx->ignore_case && ntfs_set_ignore_case(vol)) + goto err_out; + + if (ntfs_volume_get_free_space(ctx->vol)) { + ntfs_log_perror("Failed to read NTFS $Bitmap"); + goto err_out; + } + + vol->free_mft_records = ntfs_get_nr_free_mft_records(vol); + if (vol->free_mft_records < 0) { + ntfs_log_perror("Failed to calculate free MFT records"); + goto err_out; + } + + if (ctx->hiberfile && ntfs_volume_check_hiberfile(vol, 0)) { + if (errno != EPERM) + goto err_out; + if (ntfs_fuse_rm((fuse_req_t)NULL,FILE_root,"hiberfil.sys", + RM_LINK)) + goto err_out; + } + + errno = 0; + goto out; +err_out: + if (!errno) /* Make sure to return an error */ + errno = EIO; +out : + return ntfs_volume_error(errno); +} + +static void usage(void) +{ + ntfs_log_info(usage_msg, EXEC_NAME, VERSION, FUSE_TYPE, fuse_version(), + 5 + POSIXACLS*6 - KERNELPERMS*3 + CACHEING, + EXEC_NAME, ntfs_home); +} + +#if defined(linux) || defined(__uClinux__) + +static const char *dev_fuse_msg = +"HINT: You should be root, or make ntfs-3g setuid root, or load the FUSE\n" +" kernel module as root ('modprobe fuse' or 'insmod /fuse.ko'" +" or insmod /fuse.o'). Make also sure that the fuse device" +" exists. It's usually either /dev/fuse or /dev/misc/fuse."; + +static const char *fuse26_kmod_msg = +"WARNING: Deficient Linux kernel detected. Some driver features are\n" +" not available (swap file on NTFS, boot from NTFS by LILO), and\n" +" unmount is not safe unless it's made sure the ntfs-3g process\n" +" naturally terminates after calling 'umount'. If you wish this\n" +" message to disappear then you should upgrade to at least kernel\n" +" version 2.6.20, or request help from your distribution to fix\n" +" the kernel problem. The below web page has more information:\n" +" https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ\n" +"\n"; + +static void mknod_dev_fuse(const char *dev) +{ + struct stat st; + + if (stat(dev, &st) && (errno == ENOENT)) { + mode_t mask = umask(0); + if (mknod(dev, S_IFCHR | 0666, makedev(10, 229))) { + ntfs_log_perror("Failed to create '%s'", dev); + if (errno == EPERM) + ntfs_log_error("%s", dev_fuse_msg); + } + umask(mask); + } +} + +static void create_dev_fuse(void) +{ + mknod_dev_fuse("/dev/fuse"); + +#ifdef __UCLIBC__ + { + struct stat st; + /* The fuse device is under /dev/misc using devfs. */ + if (stat("/dev/misc", &st) && (errno == ENOENT)) { + mode_t mask = umask(0); + mkdir("/dev/misc", 0775); + umask(mask); + } + mknod_dev_fuse("/dev/misc/fuse"); + } +#endif +} + +static fuse_fstype get_fuse_fstype(void) +{ + char buf[256]; + fuse_fstype fstype = FSTYPE_NONE; + + FILE *f = fopen("/proc/filesystems", "r"); + if (!f) { + ntfs_log_perror("Failed to open /proc/filesystems"); + return FSTYPE_UNKNOWN; + } + + while (fgets(buf, sizeof(buf), f)) { + if (strstr(buf, "fuseblk\n")) { + fstype = FSTYPE_FUSEBLK; + break; + } + if (strstr(buf, "fuse\n")) + fstype = FSTYPE_FUSE; + } + + fclose(f); + return fstype; +} + +static fuse_fstype load_fuse_module(void) +{ + int i; + struct stat st; + pid_t pid; + const char *cmd = "/sbin/modprobe"; + char *env = (char*)NULL; + struct timespec req = { 0, 100000000 }; /* 100 msec */ + fuse_fstype fstype; + + if (!stat(cmd, &st) && !geteuid()) { + pid = fork(); + if (!pid) { + execle(cmd, cmd, "fuse", (char*)NULL, &env); + _exit(1); + } else if (pid != -1) + waitpid(pid, NULL, 0); + } + + for (i = 0; i < 10; i++) { + /* + * We sleep first because despite the detection of the loaded + * FUSE kernel module, fuse_mount() can still fail if it's not + * fully functional/initialized. Note, of course this is still + * unreliable but usually helps. + */ + nanosleep(&req, NULL); + fstype = get_fuse_fstype(); + if (fstype != FSTYPE_NONE) + break; + } + return fstype; +} + +#endif + +static struct fuse_chan *try_fuse_mount(char *parsed_options) +{ + struct fuse_chan *fc = NULL; + struct fuse_args margs = FUSE_ARGS_INIT(0, NULL); + + /* The fuse_mount() options get modified, so we always rebuild it */ + if ((fuse_opt_add_arg(&margs, EXEC_NAME) == -1 || + fuse_opt_add_arg(&margs, "-o") == -1 || + fuse_opt_add_arg(&margs, parsed_options) == -1)) { + ntfs_log_error("Failed to set FUSE options.\n"); + goto free_args; + } + + fc = fuse_mount(opts.mnt_point, &margs); +free_args: + fuse_opt_free_args(&margs); + return fc; + +} + +static int set_fuseblk_options(char **parsed_options) +{ + char options[64]; + long pagesize; + u32 blksize = ctx->vol->cluster_size; + + pagesize = sysconf(_SC_PAGESIZE); + if (pagesize < 1) + pagesize = 4096; + + if (blksize > (u32)pagesize) + blksize = pagesize; + + snprintf(options, sizeof(options), ",blkdev,blksize=%u", blksize); + if (ntfs_strappend(parsed_options, options)) + return -1; + return 0; +} + +static struct fuse_session *mount_fuse(char *parsed_options) +{ + struct fuse_session *se = NULL; + struct fuse_args args = FUSE_ARGS_INIT(0, NULL); + + ctx->fc = try_fuse_mount(parsed_options); + if (!ctx->fc) + return NULL; + + if (fuse_opt_add_arg(&args, "") == -1) + goto err; + if (ctx->debug) + if (fuse_opt_add_arg(&args, "-odebug") == -1) + goto err; + + se = fuse_lowlevel_new(&args , &ntfs_3g_ops, sizeof(ntfs_3g_ops), NULL); + if (!se) + goto err; + + + if (fuse_set_signal_handlers(se)) + goto err_destroy; + fuse_session_add_chan(se, ctx->fc); +out: + fuse_opt_free_args(&args); + return se; +err_destroy: + fuse_session_destroy(se); + se = NULL; +err: + fuse_unmount(opts.mnt_point, ctx->fc); + goto out; +} + +static void setup_logging(char *parsed_options) +{ + if (!ctx->no_detach) { + if (daemon(0, ctx->debug)) + ntfs_log_error("Failed to daemonize.\n"); + else if (!ctx->debug) { +#ifndef DEBUG + ntfs_log_set_handler(ntfs_log_handler_syslog); + /* Override default libntfs identify. */ + openlog(EXEC_NAME, LOG_PID, LOG_DAEMON); +#endif + } + } + + ctx->seccache = (struct PERMISSIONS_CACHE*)NULL; + + ntfs_log_info("Version %s %s %d\n", VERSION, FUSE_TYPE, fuse_version()); + if (strcmp(opts.arg_device,opts.device)) + ntfs_log_info("Requested device %s canonicalized as %s\n", + opts.arg_device,opts.device); + ntfs_log_info("Mounted %s (%s, label \"%s\", NTFS %d.%d)\n", + opts.device, (ctx->ro) ? "Read-Only" : "Read-Write", + ctx->vol->vol_name, ctx->vol->major_ver, + ctx->vol->minor_ver); + ntfs_log_info("Cmdline options: %s\n", opts.options ? opts.options : ""); + ntfs_log_info("Mount options: %s\n", parsed_options); +} + +int main(int argc, char *argv[]) +{ + char *parsed_options = NULL; + struct fuse_session *se; +#if !(defined(__sun) && defined (__SVR4)) + fuse_fstype fstype = FSTYPE_UNKNOWN; +#endif + const char *permissions_mode = (const char*)NULL; + const char *failed_secure = (const char*)NULL; +#if defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) + struct XATTRMAPPING *xattr_mapping = (struct XATTRMAPPING*)NULL; +#endif /* defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) */ + struct stat sbuf; + unsigned long existing_mount; + int err, fd; + + /* + * Make sure file descriptors 0, 1 and 2 are open, + * otherwise chaos would ensue. + */ + do { + fd = open("/dev/null", O_RDWR); + if (fd > 2) + close(fd); + } while (fd >= 0 && fd <= 2); + +#ifndef FUSE_INTERNAL + if ((getuid() != geteuid()) || (getgid() != getegid())) { + fprintf(stderr, "%s", setuid_msg); + return NTFS_VOLUME_INSECURE; + } +#endif + if (drop_privs()) + return NTFS_VOLUME_NO_PRIVILEGE; + + ntfs_set_locale(); + ntfs_log_set_handler(ntfs_log_handler_stderr); + + if (ntfs_parse_options(&opts, usage, argc, argv)) { + usage(); + return NTFS_VOLUME_SYNTAX_ERROR; + } + + if (ntfs_fuse_init()) { + err = NTFS_VOLUME_OUT_OF_MEMORY; + goto err2; + } + + parsed_options = parse_mount_options(ctx, &opts, TRUE); + if (!parsed_options) { + err = NTFS_VOLUME_SYNTAX_ERROR; + goto err_out; + } + if (!ntfs_check_if_mounted(opts.device,&existing_mount) + && (existing_mount & NTFS_MF_MOUNTED) + /* accept multiple read-only mounts */ + && (!(existing_mount & NTFS_MF_READONLY) || !ctx->ro)) { + err = NTFS_VOLUME_LOCKED; + goto err_out; + } + + /* need absolute mount point for junctions */ + if (opts.mnt_point[0] == '/') + ctx->abs_mnt_point = strdup(opts.mnt_point); + else { + ctx->abs_mnt_point = (char*)ntfs_malloc(PATH_MAX); + if (ctx->abs_mnt_point) { + if ((strlen(opts.mnt_point) < PATH_MAX) + && getcwd(ctx->abs_mnt_point, + PATH_MAX - strlen(opts.mnt_point) - 1)) { + strcat(ctx->abs_mnt_point, "/"); + strcat(ctx->abs_mnt_point, opts.mnt_point); +#if defined(__sun) && defined (__SVR4) + /* Solaris also wants the absolute mount point */ + opts.mnt_point = ctx->abs_mnt_point; +#endif /* defined(__sun) && defined (__SVR4) */ + } else { + free(ctx->abs_mnt_point); + ctx->abs_mnt_point = (char*)NULL; + } + } + } + if (!ctx->abs_mnt_point) { + err = NTFS_VOLUME_OUT_OF_MEMORY; + goto err_out; + } + + ctx->security.uid = 0; + ctx->security.gid = 0; + if ((opts.mnt_point[0] == '/') + && !stat(opts.mnt_point,&sbuf)) { + /* collect owner of mount point, useful for default mapping */ + ctx->security.uid = sbuf.st_uid; + ctx->security.gid = sbuf.st_gid; + } + +#if defined(linux) || defined(__uClinux__) + fstype = get_fuse_fstype(); + + err = NTFS_VOLUME_NO_PRIVILEGE; + if (restore_privs()) + goto err_out; + + if (fstype == FSTYPE_NONE || fstype == FSTYPE_UNKNOWN) + fstype = load_fuse_module(); + create_dev_fuse(); + + if (drop_privs()) + goto err_out; +#endif + if (stat(opts.device, &sbuf)) { + ntfs_log_perror("Failed to access '%s'", opts.device); + err = NTFS_VOLUME_NO_PRIVILEGE; + goto err_out; + } + +#if !(defined(__sun) && defined (__SVR4)) + /* Always use fuseblk for block devices unless it's surely missing. */ + if (S_ISBLK(sbuf.st_mode) && (fstype != FSTYPE_FUSE)) + ctx->blkdev = TRUE; +#endif + +#ifndef FUSE_INTERNAL + if (getuid() && ctx->blkdev) { + ntfs_log_error("%s", unpriv_fuseblk_msg); + err = NTFS_VOLUME_NO_PRIVILEGE; + goto err2; + } +#endif + err = ntfs_open(opts.device); + if (err) + goto err_out; + + /* Force read-only mount if the device was found read-only */ + if (!ctx->ro && NVolReadOnly(ctx->vol)) { + ctx->rw = FALSE; + ctx->ro = TRUE; + if (ntfs_strinsert(&parsed_options, ",ro")) + goto err_out; + ntfs_log_info("Could not mount read-write, trying read-only\n"); + } else + if (ctx->rw && ntfs_strinsert(&parsed_options, ",rw")) + goto err_out; + /* We must do this after ntfs_open() to be able to set the blksize */ + if (ctx->blkdev && set_fuseblk_options(&parsed_options)) + goto err_out; + + ctx->vol->abs_mnt_point = ctx->abs_mnt_point; + ctx->vol->special_files = ctx->special_files; + ctx->security.vol = ctx->vol; + ctx->vol->secure_flags = ctx->secure_flags; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + ctx->vol->efs_raw = ctx->efs_raw; +#endif /* HAVE_SETXATTR */ + if (!ntfs_build_mapping(&ctx->security,ctx->usermap_path, + (ctx->vol->secure_flags + & ((1 << SECURITY_DEFAULT) | (1 << SECURITY_ACL))) + && !ctx->inherit + && !(ctx->vol->secure_flags & (1 << SECURITY_WANTED)))) { +#if POSIXACLS + /* use basic permissions if requested */ + if (ctx->vol->secure_flags & (1 << SECURITY_DEFAULT)) + permissions_mode = "User mapping built, Posix ACLs not used"; + else { + permissions_mode = "User mapping built, Posix ACLs in use"; +#if KERNELACLS + if (ntfs_strinsert(&parsed_options, + ",default_permissions,acl")) { + err = NTFS_VOLUME_SYNTAX_ERROR; + goto err_out; + } +#endif /* KERNELACLS */ + } +#else /* POSIXACLS */ + if (!(ctx->vol->secure_flags + & ((1 << SECURITY_DEFAULT) | (1 << SECURITY_ACL)))) { + /* + * No explicit option but user mapping found + * force default security + */ +#if KERNELPERMS + ctx->vol->secure_flags |= (1 << SECURITY_DEFAULT); + if (ntfs_strinsert(&parsed_options, ",default_permissions")) { + err = NTFS_VOLUME_SYNTAX_ERROR; + goto err_out; + } +#endif /* KERNELPERMS */ + } + permissions_mode = "User mapping built"; +#endif /* POSIXACLS */ + ctx->dmask = ctx->fmask = 0; + } else { + ctx->security.uid = ctx->uid; + ctx->security.gid = ctx->gid; + /* same ownership/permissions for all files */ + ctx->security.mapping[MAPUSERS] = (struct MAPPING*)NULL; + ctx->security.mapping[MAPGROUPS] = (struct MAPPING*)NULL; + if ((ctx->vol->secure_flags & (1 << SECURITY_WANTED)) + && !(ctx->vol->secure_flags & (1 << SECURITY_DEFAULT))) { + ctx->vol->secure_flags |= (1 << SECURITY_DEFAULT); + if (ntfs_strinsert(&parsed_options, ",default_permissions")) { + err = NTFS_VOLUME_SYNTAX_ERROR; + goto err_out; + } + } + if (ctx->vol->secure_flags & (1 << SECURITY_DEFAULT)) { + ctx->vol->secure_flags |= (1 << SECURITY_RAW); + permissions_mode = "Global ownership and permissions enforced"; + } else { + ctx->vol->secure_flags &= ~(1 << SECURITY_RAW); + permissions_mode = "Ownership and permissions disabled"; + } + } + if (ctx->usermap_path) + free (ctx->usermap_path); + +#if defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) + xattr_mapping = ntfs_xattr_build_mapping(ctx->vol, + ctx->xattrmap_path); + ctx->vol->xattr_mapping = xattr_mapping; + /* + * Errors are logged, do not refuse mounting, it would be + * too difficult to fix the unmountable mapping file. + */ + if (ctx->xattrmap_path) + free(ctx->xattrmap_path); +#endif /* defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) */ + +#ifndef DISABLE_PLUGINS + register_internal_reparse_plugins(); +#endif /* DISABLE_PLUGINS */ + + se = mount_fuse(parsed_options); + if (!se) { + err = NTFS_VOLUME_FUSE_ERROR; + goto err_out; + } + + ctx->mounted = TRUE; + +#if defined(linux) || defined(__uClinux__) + if (S_ISBLK(sbuf.st_mode) && (fstype == FSTYPE_FUSE)) + ntfs_log_info("%s", fuse26_kmod_msg); +#endif + setup_logging(parsed_options); + if (failed_secure) + ntfs_log_info("%s\n",failed_secure); + if (permissions_mode) + ntfs_log_info("%s, configuration type %d\n",permissions_mode, + 5 + POSIXACLS*6 - KERNELPERMS*3 + CACHEING); + + fuse_session_loop(se); + fuse_remove_signal_handlers(se); + + err = 0; + + fuse_unmount(opts.mnt_point, ctx->fc); + fuse_session_destroy(se); +err_out: + ntfs_mount_error(opts.device, opts.mnt_point, err); + if (ctx->abs_mnt_point) + free(ctx->abs_mnt_point); +#if defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) + ntfs_xattr_free_mapping(xattr_mapping); +#endif /* defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) */ +err2: + ntfs_close(); +#ifndef DISABLE_PLUGINS + close_reparse_plugins(ctx); +#endif /* DISABLE_PLUGINS */ + free(ctx); + free(parsed_options); + free(opts.options); + free(opts.device); + return err; +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.8.in new file mode 100644 index 0000000..30b4110 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.8.in @@ -0,0 +1,488 @@ +.\" Copyright (c) 2005-2006 Yura Pakhuchiy. +.\" Copyright (c) 2005 Richard Russon. +.\" Copyright (c) 2006-2009 Szabolcs Szakacsits. +.\" Copyright (c) 2009-2014 Jean-Pierre Andre +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFS-3G 8 "Aug 2021" "ntfs-3g @VERSION@" +.SH NAME +ntfs-3g \- Third Generation Read/Write NTFS Driver +.SH SYNOPSIS +.B ntfs-3g +\fB[-o \fIoption\fP\fB[,...]]\fR +.I volume mount_point +.br +.B mount \-t ntfs-3g +\fB[-o \fIoption\fP\fB[,...]]\fR +.I volume mount_point +.br +.B lowntfs-3g +\fB[-o \fIoption\fP\fB[,...]]\fR +.I volume mount_point +.br +.B mount \-t lowntfs-3g +\fB[-o \fIoption\fP\fB[,...]]\fR +.I volume mount_point +.SH DESCRIPTION +\fBntfs-3g\fR is an NTFS driver, which can create, remove, rename, move +files, directories, hard links, and streams; it can read and write files, +including streams, sparse files and transparently compressed files; it can +handle special files like symbolic links, devices, and FIFOs; moreover it +provides standard management of file ownership and permissions, including +POSIX ACLs. +.PP +It comes in two variants \fBntfs-3g\fR and \fBlowntfs-3g\fR with +a few differences mentioned below in relevant options descriptions. +.PP +The \fIvolume\fR to be mounted can be either a block device or +an image file, either by using the mount command or starting the drive. +.SS Windows hibernation and fast restarting +On computers which can be dual-booted into Windows or Linux, Windows has +to be fully shut down before booting into Linux, otherwise the NTFS file +systems on internal disks may be left in an inconsistent state and changes +made by Linux may be ignored by Windows. +.P +So, Windows may not be left in hibernation when starting Linux, in order +to avoid inconsistencies. Moreover, the fast restart feature available on +recent Windows systems has to be disabled. This can be achieved by issuing +as an Administrator the Windows command which disables both +hibernation and fast restarting : +.RS +.sp +powercfg /h off +.sp +.RE +If either Windows is hibernated or its fast restart is enabled, partitions +on internal disks are forced to be mounted in read-only mode. +.SS Access Handling and Security +By default, files and directories are owned by the effective +user and group of the mounting process, and everybody has +full read, write, execution and directory browsing permissions. +You can also assign permissions to a single user by using the +.B uid +and/or the +.B gid +options together with the +.B umask, +or +.B fmask +and +.B dmask +options. +.PP +Doing so, all Windows users have full access to the files created by +.B ntfs-3g. +.PP +But, by setting the \fBpermissions\fR option, you can benefit from the full +ownership and permissions features as defined by POSIX. Moreover, by defining +a Windows-to-Linux user mapping, the ownerships and permissions are even +applied to Windows users and conversely. +.PP +If +.B ntfs-3g +is set setuid-root then non-root users will +be also able to mount volumes. +.SS Windows Filename Compatibility +NTFS supports several filename namespaces: DOS, Win32 and POSIX. While the +\fBntfs-3g\fR driver handles all of them, it always creates new files in the +POSIX namespace for maximum portability and interoperability reasons. +This means that filenames are case sensitive and all characters are +allowed except '/' and '\\0'. This is perfectly legal on Windows, though +some application may get confused. The option \fBwindows_names\fP may be +used to apply Windows restrictions to new file names. +.SS Alternate Data Streams (ADS) +NTFS stores all data in streams. Every file has exactly one unnamed +data stream and can have many named data streams. The size of a file is the +size of its unnamed data stream. By default, \fBntfs-3g\fR will only read +the unnamed data stream. +.PP +By using the option \fBstreams_interface=windows\fP, with the ntfs-3g driver +(not possible with lowntfs-3g), you will be able to read any named data +streams, simply by specifying the stream name after a colon. +For example: +.RS +.sp +cat some.mp3:artist +.sp +.RE +Named data streams act like normal files, so you can read from them, write to +them and even delete them (using rm). You can list all the named data streams +a file has by getting the \fBntfs.streams.list\fP extended attribute. +.SH OPTIONS +Below is a summary of the options that \fBntfs-3g\fR accepts. +.TP +.B acl +Enable setting Posix ACLs on created files and use them for access control. +This option is only available on specific builds. It is set by default +when a user mapping file is present and the +.B permissions +mount option is not set. +.TP +.B allow_other +This option overrides the security measure restricting file access +to the user mounting the filesystem. This option is only +allowed to root, but this restriction can be overridden by +the \fBuser_allow_other\fP option in the /etc/fuse.conf file. +.TP +.B atime, noatime, relatime +The +.B atime +option updates inode access time for each access. + +The +.B noatime +option disables inode access time updates, which can speed up +file operations and prevent sleeping (notebook) disks spinning +up too often thus saving energy and disk lifetime. + +The +.B relatime +option is very similar to +.B noatime. +It updates inode access times relative to modify or change time. +The access time is only updated if the previous access time was earlier +than the current modify or change time. Unlike +.B noatime +this option doesn't break applications that need to know +if a file has been read since the last time it was modified. +This is the default behaviour. +.TP +.B big_writes +This option prevents fuse from splitting write buffers into 4K chunks, +enabling big write buffers to be transferred from the application in a +single step (up to some system limit, generally 128K bytes). +.TP +.B compression +This option enables creating new transparently compressed files in +directories marked for compression. A directory is marked for compression by +setting the bit 11 (value 0x00000800) in its Windows attribute. In such a +directory, new files are created compressed and new subdirectories are +themselves marked for compression. The option and the flag have no effect +on existing files. Currently this is the default option. +.TP +.B debug +Makes ntfs-3g (or lowntfs-3g) to print a lot of debug output from libntfs-3g +and FUSE. +.TP +.B delay_mtime[= value] +Only update the file modification time and the file change time of a file +when it is closed or when the indicated delay since the previous update has +elapsed. The argument is a number of seconds, with a default value of 60. +This is mainly useful for big files which are kept open for a long +time and written to without changing their size, such as databases or file +system images mounted as loop. +.TP +.BI dmask= value +Set the bitmask of the directory permissions that are not +present. The value is given in octal. The default value is 0 which +means full access to everybody. +.TP +.B efs_raw +This option should only be used in backup or restore situation. +It changes the apparent size of files and the behavior of read and +write operation so that encrypted files can be saved and restored +without being decrypted. The \fBuser.ntfs.efsinfo\fP extended attribute +has also to be saved and restored for the file to be decrypted. +.TP +.BI fmask= value +Set the bitmask of the file permissions that are not present. +The value is given in octal. The default value is 0 which +means full access to everybody. +.TP +.B force +This option is obsolete. It has been superseded by the \fBrecover\fR and +\fBnorecover\fR options. +.TP +.B hide_dot_files +Set the hidden flag in the NTFS attribute for created files and directories +whose first character of the name is a dot. Such files and directories +normally do not appear in directory listings, and when the flag is set +they do not appear in Windows directory displays either. +When a file is renamed or linked with a new name, the hidden flag is +adjusted to the latest name. +.TP +.B hide_hid_files +Hide the hidden files and directories in directory listings, the hidden files +and directories being the ones whose NTFS attribute have the hidden flag set. +The hidden files will not be selected when using wildcards in commands, +but all files and directories remain accessible by full name, for example you +can always display the Windows trash bin directory by : +"ls \-ld '$RECYCLE.BIN'". +.TP +.B ignore_case \fP(only with lowntfs-3g) +Ignore character case when accessing a file (\fBFOO\fR, \fBFoo\fR, \fBfoo\fR, +etc. designate the same file). All files are displayed with lower case in +directory listings. +.TP +.B inherit +When creating a new file, set its initial protections +according to inheritance rules defined in parent directory. These rules +deviate from Posix specifications, but yield a better Windows +compatibility. The \fBpermissions\fR (or **acl**) option or a valid user +mapping file is required for this option to be effective. +.TP +.BI locale= value +This option can be useful when wanting a language specific locale environment. +It is however discouraged as it leads to files with untranslatable characters +to not be visible. +.TP +.BI max_read= value +With this option the maximum size of read operations can be set. +The default is infinite. Note that the size of read requests is +limited anyway by the system (usually to 128kbyte). +.TP +.B no_def_opts +By default ntfs-3g acts as if \fBsilent\fP (ignore permission errors when +permissions are not enabled), +\fBallow_other\fP (allow any user to access files) and \fBnonempty\fP +(allow mounting on non-empty directories) were set, and \fBno_def_opts\fP +cancels these default options. +.TP +.B no_detach +Makes ntfs-3g to not detach from terminal and print some debug output. +.TP +.B nocompression +This option disables creating new transparently compressed files in directories +marked for compression. Existing compressed files can still be read and +updated. +.TP +.B norecover +Do not try to mount a partition which was not unmounted properly by Windows. +.TP +.B permissions +Set standard permissions on created files and use standard access control. +This option is set by default when a user mapping file is present. +.TP +.B posix_nlink +Compute the count of hard links of a file or directory according to +the POSIX specifications. When this option is not set, a count of 1 +is set for directories, and the short name of files is accounted for. +Using the option entails some penalty as the count is not stored and +has to be computed. +.TP +.B recover +Recover and try to mount a partition which was not unmounted properly by +Windows. The Windows logfile is cleared, which may cause inconsistencies. +Currently this is the default option. +.TP +.B remove_hiberfile +When the NTFS volume is hibernated, a read-write mount is denied and +a read-only mount is forced. One needs either to resume Windows and +shutdown it properly, or use this option which will remove the Windows +hibernation file. Please note, this means that the saved Windows +session will be completely lost. Use this option under your own +responsibility. +.TP +.B ro +Mount the filesystem read\-only. Useful if Windows is hibernated or the +NTFS journal file is unclean. +.TP +.B show_sys_files +Show the metafiles in directory listings. Otherwise the default behaviour is +to hide the metafiles, which are special files used to store the NTFS +structure. Please note that even when this option is specified, "$MFT" may +not be visible due to a glibc bug. Furthermore, irrespectively of +\fBshow_sys_files\fP, all files are accessible by name, for example you can +always do +"ls \-l '$UpCase'". +.TP +.B silent +Do nothing, without returning any error, on chmod and chown operations +and on permission checking errors, +when the \fBpermissions\fR option is not set and no user mapping file +is defined. This option is on by default, and when set off (through option +\fBno_def_opts\fR) ownership and permissions parameters have to be set. +.TP +.BI special_files= mode +This option selects a mode for representing a special file to be created +(symbolic link, socket, fifo, character or block device). The \fImode\fP can +be \fBinterix\fR or \fBwsl\fR, and existing files in either mode are +recognized irrespective of the selected mode. Interix is the traditional +mode, used by default, and wsl is interoperable with Windows WSL, but +it is not compatible with Windows versions earlier than Windows 10. +Neither mode are interoperable with Windows. +.TP +.BI streams_interface= mode +This option controls how the user can access Alternate Data Streams (ADS) or in +other words, named data streams. The \fImode\fP can be set to one of \fBnone\fR, +\fBwindows\fR or \fBxattr\fR. If the option is set to \fBnone\fR, the user +will have no access to the named data streams. If it is set to \fBwindows\fR +(not possible with lowntfs-3g), then the user can access them just like in +Windows (eg. cat file:stream). If it's set to \fBxattr\fR, then the named +data streams are mapped to extended attributes and a user can manipulate them +using \fB{get,set}fattr\fR utilities. The default is \fBxattr\fR. +.TP +\fBuid=\fP\fIvalue\fP and \fBgid=\fP\fIvalue\fP +Set the owner and the group of files and directories. The values are numerical. +The defaults are the uid and gid of the current process. +.TP +.BI umask= value +Set the bitmask of the file and directory permissions that are not +present. The value is given in octal. The default value is 0 which +means full access to everybody. +.TP +.BI usermapping= file-name +Use file \fIfile-name\fP as the user mapping file instead of the default +\fB.NTFS-3G/UserMapping\fP. If \fIfile-name\fP defines a full path, the +file must be located on a partition previously mounted. If it defines a +relative path, it is interpreted relative to the root of NTFS partition +being mounted. +.P +.RS +When a user mapping file is defined, the options \fBuid=\fP, \fBgid=\fP, +\fBumask=\fP, \fBfmask=\fP, \fBdmask=\fP and \fBsilent\fP are ignored. +.RE +.TP +.B user_xattr +Same as \fBstreams_interface=\fP\fIxattr\fP. +.TP +.B windows_names +This option prevents files, directories and extended attributes to be +created with a name not allowed by windows, because +.RS +.RS +.sp +- it contains some not allowed character, +.br +- or the last character is a space or a dot, +.br +- or the name is reserved. +.sp +.RE +The forbidden characters are the nine characters " * / : < > ? \\ | and +those whose code is less than 0x20, and +the reserved names are CON, PRN, AUX, NUL, COM1..COM9, LPT1..LPT9, +with no suffix or followed by a dot. +.sp +Existing such files can still be read (and renamed). +.RE +.SH USER MAPPING +NTFS uses specific ids to record the ownership of files instead of +the \fBuid\fP (user id) and \fBgid\fP (group id) used by Linux. As a +consequence a mapping between the ids has to be defined for ownerships +to be recorded into NTFS files and recognized. +.P +By default, this mapping is fetched from the file \fB.NTFS-3G/UserMapping\fP +located in the NTFS partition. The option \fBusermapping=\fP may be used +to define another location. When the option **permissions** is set and +no mapping file is found, a default mapping is used. +.P +Each line in the user mapping file defines a mapping. It is organized +in three fields separated by colons. The first field identifies a \fBuid\fP, +the second field identifies a \fBgid\fP and the third one identifies the +corresponding NTFS id, known as a \fBSID\fP. The \fBuid\fP and the \fBgid\fP +are optional and defining both of them for the same \fBSID\fP is not +recommended. +.P +If no interoperation with Windows is needed, you can use the option +\fBpermissions\fP to define a standard mapping. Alternately, you may define +your own mapping by setting a single default mapping with no uid and gid. In +both cases, files created on Linux will appear to Windows as owned by a +foreign user, and files created on Windows will appear to Linux as owned by +root. Just copy the example below and replace the 9 and 10-digit numbers by +any number not greater than 4294967295. The resulting behavior is the same as +the one with the option \fBpermission\fP set with no ownership option and no +user mapping file available. +.RS +.sp +.B ::S-1-5-21-3141592653-589793238-462643383-10000 +.sp +.RE +If a strong interoperation with Windows is needed, the mapping has to be +defined for each user and group known to both system, and the \fBSID\fPs used +by Windows has to be collected. This will lead to a user mapping file like : +.RS +.sp +.B john::S-1-5-21-3141592653-589793238-462643383-1008 +.B mary::S-1-5-21-3141592653-589793238-462643383-1009 +.B :smith:S-1-5-21-3141592653-589793238-462643383-513 +.B ::S-1-5-21-3141592653-589793238-462643383-10000 +.sp +.RE +.P +The utility \fBntfsusermap\fP may be used to create such a user +mapping file. +.SH EXAMPLES +Mount /dev/sda1 to /mnt/windows: +.RS +.sp +.B ntfs-3g /dev/sda1 /mnt/windows +.RE +or +.RS +.B mount -t ntfs-3g /dev/sda1 /mnt/windows +.sp +.RE +Mount the ntfs data partition /dev/sda3 to /mnt/data with standard Linux +permissions applied : +.RS +.sp +.B ntfs-3g -o permissions /dev/sda3 /mnt/data +.RE +or +.RS +.B mount -t ntfs-3g -o permissions /dev/sda3 /mnt/data +.sp +.RE +Read\-only mount /dev/sda5 to /home/user/mnt and make user with uid 1000 +to be the owner of all files: +.RS +.sp +.B ntfs-3g /dev/sda5 /home/user/mnt \-o ro,uid=1000 +.sp +.RE +/etc/fstab entry for the above (the sixth and last field has to be zero to +avoid a file system check at boot time) : +.RS +.sp +.B /dev/sda5 /home/user/mnt ntfs\-3g ro,uid=1000 0 0 +.sp +.RE +Unmount /mnt/windows: +.RS +.sp +.B umount /mnt/windows +.sp +.RE +.SH EXIT CODES +To facilitate the use of the +.B ntfs-3g +driver in scripts, an exit code is returned to give an indication of the +mountability status of a volume. Value 0 means success, and all other +ones mean an error. The unique error codes are documented in the +.BR ntfs-3g.probe (8) +manual page. +.SH KNOWN ISSUES +Please see +.RS +.sp +https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ +.sp +.RE +for common questions and known issues. +If you would find a new one in the latest release of +the software then please post an ntfs-3g issue describing it in detail +so that the development team can be aware of the issue and take care of it: +.RS +.sp +https://github.com/tuxera/ntfs-3g/issues +.sp +.RE +.SH AUTHORS +.B ntfs-3g +was based on and a major improvement to ntfsmount and libntfs which were +written by Yura Pakhuchiy and the Linux-NTFS team. The improvements were +made, the ntfs-3g project was initiated and currently led by long time +Linux-NTFS team developer Szabolcs Szakacsits (szaka@tuxera.com). +.SH THANKS +Several people made heroic efforts, often over five or more +years which resulted the ntfs-3g driver. Most importantly they are +Anton Altaparmakov, Jean-Pierre André, Erik Larsson, Richard Russon, +Szabolcs Szakacsits, +Yura Pakhuchiy, Yuval Fledel, and the author of the groundbreaking FUSE +filesystem development framework, Miklos Szeredi. +.SH SEE ALSO +.BR ntfs-3g.probe (8), +.BR ntfsprogs (8), +.BR attr (5), +.BR getfattr (1) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.c new file mode 100644 index 0000000..820733e --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.c @@ -0,0 +1,4602 @@ +/** + * ntfs-3g - Third Generation NTFS Driver + * + * Copyright (c) 2005-2007 Yura Pakhuchiy + * Copyright (c) 2005 Yuval Fledel + * Copyright (c) 2006-2009 Szabolcs Szakacsits + * Copyright (c) 2007-2021 Jean-Pierre Andre + * Copyright (c) 2009 Erik Larsson + * + * This file is originated from the Linux-NTFS project. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#include + +#if !defined(FUSE_VERSION) || (FUSE_VERSION < 26) +#error "***********************************************************" +#error "* *" +#error "* Compilation requires at least FUSE version 2.6.0! *" +#error "* *" +#error "***********************************************************" +#endif + +#ifdef HAVE_STDIO_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_LOCALE_H +#include +#endif +#include +#ifdef HAVE_LIMITS_H +#include +#endif +#include +#include + +#ifdef HAVE_SETXATTR +#include +#endif + +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef MAJOR_IN_MKDEV +#include +#endif +#ifdef MAJOR_IN_SYSMACROS +#include +#endif + +#if defined(__APPLE__) || defined(__DARWIN__) +#include +#elif defined(__sun) && defined (__SVR4) +#include +#endif /* defined(__APPLE__) || defined(__DARWIN__), ... */ + +#ifndef FUSE_CAP_POSIX_ACL /* until defined in */ +#define FUSE_CAP_POSIX_ACL (1 << 18) +#endif /* FUSE_CAP_POSIX_ACL */ + +#include "compat.h" +#include "attrib.h" +#include "inode.h" +#include "volume.h" +#include "dir.h" +#include "unistr.h" +#include "layout.h" +#include "index.h" +#include "ntfstime.h" +#include "security.h" +#include "reparse.h" +#include "ea.h" +#include "object_id.h" +#include "efs.h" +#include "logging.h" +#include "xattrs.h" +#include "misc.h" +#include "ioctl.h" +#include "plugin.h" + +#include "ntfs-3g_common.h" + +/* + * The following permission checking modes are governed by + * the HPERMSCONFIG value in param.h + */ + +/* ACLS may be checked by kernel (requires a fuse patch) or here */ +#define KERNELACLS ((HPERMSCONFIG > 6) & (HPERMSCONFIG < 10)) +/* basic permissions may be checked by kernel or here */ +#define KERNELPERMS (((HPERMSCONFIG - 1) % 6) < 3) +/* may want to use fuse/kernel cacheing */ +#define CACHEING (!(HPERMSCONFIG % 3)) + +#if KERNELACLS & !KERNELPERMS +#error Incompatible options KERNELACLS and KERNELPERMS +#endif + + /* sometimes the kernel cannot check access */ +#define ntfs_real_allowed_access(scx, ni, type) ntfs_allowed_access(scx, ni, type) +#if POSIXACLS & KERNELPERMS & !KERNELACLS + /* short-circuit if PERMS checked by kernel and ACLs by fs */ +#define ntfs_allowed_access(scx, ni, type) \ + ((scx)->vol->secure_flags & (1 << SECURITY_DEFAULT) \ + ? 1 : ntfs_allowed_access(scx, ni, type)) +#endif + +#define set_archive(ni) (ni)->flags |= FILE_ATTR_ARCHIVE + +/* + * Call a function from a reparse plugin (variable arguments) + * Requires "reparse" and "ops" to have been defined + * + * Returns a non-negative value if successful, + * and a negative error code if something fails. + */ +#define CALL_REPARSE_PLUGIN(ni, op_name, ...) \ + (reparse = (REPARSE_POINT*)NULL, \ + ops = select_reparse_plugin(ctx, ni, &reparse), \ + (!ops ? -errno \ + : (ops->op_name ? \ + ops->op_name(ni, reparse, __VA_ARGS__) \ + : -EOPNOTSUPP))), \ + free(reparse) + +typedef enum { + FSTYPE_NONE, + FSTYPE_UNKNOWN, + FSTYPE_FUSE, + FSTYPE_FUSEBLK +} fuse_fstype; + +typedef struct { + fuse_fill_dir_t filler; + void *buf; +} ntfs_fuse_fill_context_t; + +enum { + CLOSE_COMPRESSED = 1, + CLOSE_ENCRYPTED = 2, + CLOSE_DMTIME = 4, + CLOSE_REPARSE = 8 +}; + +static struct ntfs_options opts; + +const char *EXEC_NAME = "ntfs-3g"; + +static ntfs_fuse_context_t *ctx; +static u32 ntfs_sequence; + +static const char *usage_msg = +"\n" +"%s %s %s %d - Third Generation NTFS Driver\n" +"\t\tConfiguration type %d, " +#ifdef HAVE_SETXATTR +"XATTRS are on, " +#else +"XATTRS are off, " +#endif +#if POSIXACLS +"POSIX ACLS are on\n" +#else +"POSIX ACLS are off\n" +#endif +"\n" +"Copyright (C) 2005-2007 Yura Pakhuchiy\n" +"Copyright (C) 2006-2009 Szabolcs Szakacsits\n" +"Copyright (C) 2007-2022 Jean-Pierre Andre\n" +"Copyright (C) 2009-2020 Erik Larsson\n" +"\n" +"Usage: %s [-o option[,...]] \n" +"\n" +"Options: ro (read-only mount), windows_names, uid=, gid=,\n" +" umask=, fmask=, dmask=, streams_interface=.\n" +" Please see the details in the manual (type: man ntfs-3g).\n" +"\n" +"Example: ntfs-3g /dev/sda1 /mnt/windows\n" +"\n" +#ifdef PLUGIN_DIR +"Plugin path: " PLUGIN_DIR "\n\n" +#endif /* PLUGIN_DIR */ +"%s"; + +static const char ntfs_bad_reparse[] = "unsupported reparse tag 0x%08lx"; + /* exact length of target text, without the terminator */ +#define ntfs_bad_reparse_lth (sizeof(ntfs_bad_reparse) + 2) + +#ifdef FUSE_INTERNAL +int drop_privs(void); +int restore_privs(void); +#else +/* + * setuid and setgid root ntfs-3g denies to start with external FUSE, + * therefore the below functions are no-op in such case. + */ +static int drop_privs(void) { return 0; } +#if defined(linux) || defined(__uClinux__) +static int restore_privs(void) { return 0; } +#endif + +static const char *setuid_msg = +"Mount is denied because setuid and setgid root ntfs-3g is insecure with the\n" +"external FUSE library. Either remove the setuid/setgid bit from the binary\n" +"or rebuild NTFS-3G with integrated FUSE support and make it setuid root.\n" +"Please see more information at\n" +"https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ\n"; + +static const char *unpriv_fuseblk_msg = +"Unprivileged user can not mount NTFS block devices using the external FUSE\n" +"library. Either mount the volume as root, or rebuild NTFS-3G with integrated\n" +"FUSE support and make it setuid root. Please see more information at\n" +"https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ\n"; +#endif + + +/** + * ntfs_fuse_is_named_data_stream - check path to be to named data stream + * @path: path to check + * + * Returns 1 if path is to named data stream or 0 otherwise. + */ +static int ntfs_fuse_is_named_data_stream(const char *path) +{ + if (strchr(path, ':') && ctx->streams == NF_STREAMS_INTERFACE_WINDOWS) + return 1; + return 0; +} + +static void ntfs_fuse_update_times(ntfs_inode *ni, ntfs_time_update_flags mask) +{ + if (ctx->atime == ATIME_DISABLED) + mask &= ~NTFS_UPDATE_ATIME; + else if (ctx->atime == ATIME_RELATIVE && mask == NTFS_UPDATE_ATIME && + (sle64_to_cpu(ni->last_access_time) + >= sle64_to_cpu(ni->last_data_change_time)) && + (sle64_to_cpu(ni->last_access_time) + >= sle64_to_cpu(ni->last_mft_change_time))) + return; + ntfs_inode_update_times(ni, mask); +} + +static s64 ntfs_get_nr_free_mft_records(ntfs_volume *vol) +{ + ntfs_attr *na = vol->mftbmp_na; + s64 nr_free = ntfs_attr_get_free_bits(na); + + if (nr_free >= 0) + nr_free += (na->allocated_size - na->data_size) << 3; + return nr_free; +} + +/* + * Fill a security context as needed by security functions + * returns TRUE if there is a user mapping, + * FALSE if there is none + * This is not an error and the context is filled anyway, + * it is used for implicit Windows-like inheritance + */ + +static BOOL ntfs_fuse_fill_security_context(struct SECURITY_CONTEXT *scx) +{ + struct fuse_context *fusecontext; + + scx->vol = ctx->vol; + scx->mapping[MAPUSERS] = ctx->security.mapping[MAPUSERS]; + scx->mapping[MAPGROUPS] = ctx->security.mapping[MAPGROUPS]; + scx->pseccache = &ctx->seccache; + fusecontext = fuse_get_context(); + scx->uid = fusecontext->uid; + scx->gid = fusecontext->gid; + scx->tid = fusecontext->pid; +#ifdef FUSE_CAP_DONT_MASK + /* the umask can be processed by the file system */ + scx->umask = fusecontext->umask; +#else + /* the umask if forced by fuse on creation */ + scx->umask = 0; +#endif + + return (ctx->security.mapping[MAPUSERS] != (struct MAPPING*)NULL); +} + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + +/* + * Check access to parent directory + * + * directory and file inodes are only opened when not fed in, + * they *HAVE TO* be fed in when already open, however + * file inode is only useful when S_ISVTX is requested + * + * returns 1 if allowed, + * 0 if not allowed or some error occurred (errno tells why) + */ + +static int ntfs_allowed_dir_access(struct SECURITY_CONTEXT *scx, + const char *path, ntfs_inode *dir_ni, + ntfs_inode *ni, mode_t accesstype) +{ + int allowed; + ntfs_inode *ni2; + ntfs_inode *dir_ni2; + char *dirpath; + char *name; + struct stat stbuf; + +#if POSIXACLS & KERNELPERMS & !KERNELACLS + /* short-circuit if PERMS checked by kernel and ACLs by fs */ + if (scx->vol->secure_flags & (1 << SECURITY_DEFAULT)) + allowed = 1; + else +#endif + { + if (dir_ni) + allowed = ntfs_real_allowed_access(scx, dir_ni, + accesstype); + else { + allowed = 0; + dirpath = strdup(path); + if (dirpath) { + /* the root of file system is seen as a parent of itself */ + /* is that correct ? */ + name = strrchr(dirpath, '/'); + *name = 0; + dir_ni2 = ntfs_pathname_to_inode(scx->vol, + NULL, dirpath); + if (dir_ni2) { + allowed = ntfs_real_allowed_access(scx, + dir_ni2, accesstype); + if (ntfs_inode_close(dir_ni2)) + allowed = 0; + } + free(dirpath); + } + } + /* + * for a not-owned sticky directory, have to + * check whether file itself is owned + */ + if ((accesstype == (S_IWRITE + S_IEXEC + S_ISVTX)) + && (allowed == 2)) { + if (ni) + ni2 = ni; + else + ni2 = ntfs_pathname_to_inode(scx->vol, NULL, + path); + allowed = 0; + if (ni2) { + allowed = (ntfs_get_owner_mode(scx,ni2,&stbuf) + >= 0) + && (stbuf.st_uid == scx->uid); + if (!ni) + ntfs_inode_close(ni2); + } + } + } + return (allowed); +} + +#endif + +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + +/* + * Check access to parent directory + * + * for non-standard cases where access control cannot be checked by kernel + * + * no known situations where S_ISVTX is requested + * + * returns 1 if allowed, + * 0 if not allowed or some error occurred (errno tells why) + */ + +static int ntfs_allowed_real_dir_access(struct SECURITY_CONTEXT *scx, + const char *path, ntfs_inode *dir_ni, + mode_t accesstype) +{ + int allowed; + ntfs_inode *dir_ni2; + char *dirpath; + char *name; + + if (dir_ni) + allowed = ntfs_real_allowed_access(scx, dir_ni, accesstype); + else { + allowed = 0; + dirpath = strdup(path); + if (dirpath) { + /* the root of file system is seen as a parent of itself */ + /* is that correct ? */ + name = strrchr(dirpath, '/'); + *name = 0; + dir_ni2 = ntfs_pathname_to_inode(scx->vol, NULL, + dirpath); + if (dir_ni2) { + allowed = ntfs_real_allowed_access(scx, + dir_ni2, accesstype); + if (ntfs_inode_close(dir_ni2)) + allowed = 0; + } + free(dirpath); + } + } + return (allowed); +} + +static ntfs_inode *get_parent_dir(const char *path) +{ + ntfs_inode *dir_ni; + char *dirpath; + char *p; + + dirpath = strdup(path); + dir_ni = (ntfs_inode*)NULL; + if (dirpath) { + p = strrchr(dirpath,'/'); + if (p) { /* always present, be safe */ + *p = 0; + dir_ni = ntfs_pathname_to_inode(ctx->vol, + NULL, dirpath); + } + free(dirpath); + } else + errno = ENOMEM; + return (dir_ni); +} + + +#endif /* HAVE_SETXATTR */ + +/** + * ntfs_fuse_statfs - return information about mounted NTFS volume + * @path: ignored (but fuse requires it) + * @sfs: statfs structure in which to return the information + * + * Return information about the mounted NTFS volume @sb in the statfs structure + * pointed to by @sfs (this is initialized with zeros before ntfs_statfs is + * called). We interpret the values to be correct of the moment in time at + * which we are called. Most values are variable otherwise and this isn't just + * the free values but the totals as well. For example we can increase the + * total number of file nodes if we run out and we can keep doing this until + * there is no more space on the volume left at all. + * + * This code based on ntfs_statfs from ntfs kernel driver. + * + * Returns 0 on success or -errno on error. + */ +static int ntfs_fuse_statfs(const char *path __attribute__((unused)), + struct statvfs *sfs) +{ + s64 size; + int delta_bits; + ntfs_volume *vol; + + vol = ctx->vol; + if (!vol) + return -ENODEV; + + /* + * File system block size. Used to calculate used/free space by df. + * Incorrectly documented as "optimal transfer block size". + */ + sfs->f_bsize = vol->cluster_size; + + /* Fundamental file system block size, used as the unit. */ + sfs->f_frsize = vol->cluster_size; + + /* + * Total number of blocks on file system in units of f_frsize. + * Since inodes are also stored in blocks ($MFT is a file) hence + * this is the number of clusters on the volume. + */ + sfs->f_blocks = vol->nr_clusters; + + /* Free blocks available for all and for non-privileged processes. */ + size = vol->free_clusters; + if (size < 0) + size = 0; + sfs->f_bavail = sfs->f_bfree = size; + + /* Free inodes on the free space */ + delta_bits = vol->cluster_size_bits - vol->mft_record_size_bits; + if (delta_bits >= 0) + size <<= delta_bits; + else + size >>= -delta_bits; + + /* Number of inodes at this point in time. */ + sfs->f_files = (vol->mftbmp_na->allocated_size << 3) + size; + + /* Free inodes available for all and for non-privileged processes. */ + size += vol->free_mft_records; + if (size < 0) + size = 0; + sfs->f_ffree = sfs->f_favail = size; + + /* Maximum length of filenames. */ + sfs->f_namemax = NTFS_MAX_NAME_LEN; + return 0; +} + +/** + * ntfs_fuse_parse_path - split path to path and stream name. + * @org_path: path to split + * @path: pointer to buffer in which parsed path saved + * @stream_name: pointer to buffer where stream name in unicode saved + * + * This function allocates buffers for @*path and @*stream, user must free them + * after use. + * + * Return values: + * <0 Error occurred, return -errno; + * 0 No stream name, @*stream is not allocated and set to AT_UNNAMED. + * >0 Stream name length in unicode characters. + */ +static int ntfs_fuse_parse_path(const char *org_path, char **path, + ntfschar **stream_name) +{ + char *stream_name_mbs; + int res; + + stream_name_mbs = strdup(org_path); + if (!stream_name_mbs) + return -errno; + if (ctx->streams == NF_STREAMS_INTERFACE_WINDOWS) { + *path = strsep(&stream_name_mbs, ":"); + if (stream_name_mbs) { + *stream_name = NULL; + res = ntfs_mbstoucs(stream_name_mbs, stream_name); + if (res < 0) { + free(*path); + *path = NULL; + return -errno; + } + return res; + } + } else + *path = stream_name_mbs; + *stream_name = AT_UNNAMED; + return 0; +} + +static void set_fuse_error(int *err) +{ + if (!*err) + *err = -errno; +} + +#if defined(__APPLE__) || defined(__DARWIN__) +static int ntfs_macfuse_getxtimes(const char *org_path, + struct timespec *bkuptime, struct timespec *crtime) +{ + int res = 0; + ntfs_inode *ni; + char *path = NULL; + ntfschar *stream_name; + int stream_name_len; + + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + memset(bkuptime, 0, sizeof(struct timespec)); + memset(crtime, 0, sizeof(struct timespec)); + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + goto exit; + } + + /* We have no backup timestamp in NTFS. */ + crtime->tv_sec = sle64_to_cpu(ni->creation_time); +exit: + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(path); + if (stream_name_len) + free(stream_name); + return res; +} + +int ntfs_macfuse_setcrtime(const char *path, const struct timespec *tv) +{ + ntfs_inode *ni; + int res = 0; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + if (tv) { + ni->creation_time = cpu_to_sle64(tv->tv_sec); + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + } + + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +int ntfs_macfuse_setbkuptime(const char *path, const struct timespec *tv) +{ + ntfs_inode *ni; + int res = 0; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + /* + * Only pretending to set backup time successfully to please the APIs of + * Mac OS X. In reality, NTFS has no backup time. + */ + + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +int ntfs_macfuse_setchgtime(const char *path, const struct timespec *tv) +{ + ntfs_inode *ni; + int res = 0; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + if (tv) { + ni->last_mft_change_time = cpu_to_sle64(tv->tv_sec); + ntfs_fuse_update_times(ni, 0); + } + + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + +static void *ntfs_init(struct fuse_conn_info *conn) +{ +#if defined(__APPLE__) || defined(__DARWIN__) + FUSE_ENABLE_XTIMES(conn); +#endif +#ifdef FUSE_CAP_DONT_MASK + /* request umask not to be enforced by fuse */ + conn->want |= FUSE_CAP_DONT_MASK; +#endif /* defined FUSE_CAP_DONT_MASK */ +#if POSIXACLS & KERNELACLS + /* request ACLs to be checked by kernel */ + conn->want |= FUSE_CAP_POSIX_ACL; +#endif /* POSIXACLS & KERNELACLS */ +#ifdef FUSE_CAP_BIG_WRITES + if (ctx->big_writes + && ((ctx->vol->nr_clusters << ctx->vol->cluster_size_bits) + >= SAFE_CAPACITY_FOR_BIG_WRITES)) + conn->want |= FUSE_CAP_BIG_WRITES; +#endif +#ifdef FUSE_CAP_IOCTL_DIR + conn->want |= FUSE_CAP_IOCTL_DIR; +#endif /* defined(FUSE_CAP_IOCTL_DIR) */ + return NULL; +} + +#ifndef DISABLE_PLUGINS + +/* + * Define attributes for a junction or symlink + * (internal plugin) + */ + +static int junction_getattr(ntfs_inode *ni, + const REPARSE_POINT *reparse __attribute__((unused)), + struct stat *stbuf) +{ + char *target; + int res; + + errno = 0; + target = ntfs_make_symlink(ni, ctx->abs_mnt_point); + /* + * If the reparse point is not a valid + * directory junction, and there is no error + * we still display as a symlink + */ + if (target || (errno == EOPNOTSUPP)) { + if (target) + stbuf->st_size = strlen(target); + else + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_blocks = (ni->allocated_size + 511) >> 9; + stbuf->st_mode = S_IFLNK; + free(target); + res = 0; + } else { + res = -errno; + } + return (res); +} + +static int wsl_getattr(ntfs_inode *ni, const REPARSE_POINT *reparse, + struct stat *stbuf) +{ + dev_t rdev; + int res; + + res = ntfs_reparse_check_wsl(ni, reparse); + if (!res) { + switch (reparse->reparse_tag) { + case IO_REPARSE_TAG_AF_UNIX : + stbuf->st_mode = S_IFSOCK; + break; + case IO_REPARSE_TAG_LX_FIFO : + stbuf->st_mode = S_IFIFO; + break; + case IO_REPARSE_TAG_LX_CHR : + stbuf->st_mode = S_IFCHR; + res = ntfs_ea_check_wsldev(ni, &rdev); + stbuf->st_rdev = rdev; + break; + case IO_REPARSE_TAG_LX_BLK : + stbuf->st_mode = S_IFBLK; + res = ntfs_ea_check_wsldev(ni, &rdev); + stbuf->st_rdev = rdev; + break; + default : + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_mode = S_IFLNK; + break; + } + } + /* + * If the reparse point is not a valid wsl special file + * we display as a symlink + */ + if (res) { + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_mode = S_IFLNK; + res = 0; + } + return (res); +} + +/* + * Apply permission masks to st_mode returned by a reparse handler + */ + +static void apply_umask(struct stat *stbuf) +{ + switch (stbuf->st_mode & S_IFMT) { + case S_IFREG : + stbuf->st_mode &= ~ctx->fmask; + break; + case S_IFDIR : + stbuf->st_mode &= ~ctx->dmask; + break; + case S_IFLNK : + stbuf->st_mode = (stbuf->st_mode & S_IFMT) | 0777; + break; + default : + break; + } +} + +#endif /* DISABLE_PLUGINS */ + +static int ntfs_fuse_getattr(const char *org_path, struct stat *stbuf) +{ + int res = 0; + ntfs_inode *ni; + ntfs_attr *na; + char *path = NULL; + ntfschar *stream_name; + int stream_name_len; + BOOL withusermapping; + struct SECURITY_CONTEXT security; + + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + memset(stbuf, 0, sizeof(struct stat)); + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + goto exit; + } + withusermapping = ntfs_fuse_fill_security_context(&security); +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * make sure the parent directory is searchable + */ + if (withusermapping + && !ntfs_allowed_dir_access(&security,path, + (!strcmp(org_path,"/") ? ni : (ntfs_inode*)NULL), + ni, S_IEXEC)) { + res = -EACCES; + goto exit; + } +#endif + stbuf->st_nlink = le16_to_cpu(ni->mrec->link_count); + if (ctx->posix_nlink + && !(ni->flags & FILE_ATTR_REPARSE_POINT)) + stbuf->st_nlink = ntfs_dir_link_cnt(ni); + + if (((ni->mrec->flags & MFT_RECORD_IS_DIRECTORY) + || (ni->flags & FILE_ATTR_REPARSE_POINT)) + && !stream_name_len) { + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + res = CALL_REPARSE_PLUGIN(ni, getattr, stbuf); + if (!res) { + apply_umask(stbuf); + goto ok; + } else { + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_blocks = + (ni->allocated_size + 511) >> 9; + stbuf->st_mode = S_IFLNK; + res = 0; + goto ok; + } + goto exit; +#else /* DISABLE_PLUGINS */ + char *target; + + errno = 0; + target = ntfs_make_symlink(ni, ctx->abs_mnt_point); + /* + * If the reparse point is not a valid + * directory junction, and there is no error + * we still display as a symlink + */ + if (target || (errno == EOPNOTSUPP)) { + if (target) + stbuf->st_size = strlen(target); + else + stbuf->st_size = ntfs_bad_reparse_lth; + stbuf->st_blocks = (ni->allocated_size + 511) >> 9; + stbuf->st_nlink = le16_to_cpu(ni->mrec->link_count); + stbuf->st_mode = S_IFLNK; + free(target); + } else { + res = -errno; + goto exit; + } +#endif /* DISABLE_PLUGINS */ + } else { + /* Directory. */ + stbuf->st_mode = S_IFDIR | (0777 & ~ctx->dmask); + /* get index size, if not known */ + if (!test_nino_flag(ni, KnownSize)) { + na = ntfs_attr_open(ni, AT_INDEX_ALLOCATION, NTFS_INDEX_I30, 4); + if (na) { + ni->data_size = na->data_size; + ni->allocated_size = na->allocated_size; + set_nino_flag(ni, KnownSize); + ntfs_attr_close(na); + } + } + stbuf->st_size = ni->data_size; + stbuf->st_blocks = ni->allocated_size >> 9; + if (!ctx->posix_nlink) + stbuf->st_nlink = 1; /* Make find(1) work */ + } + } else { + /* Regular or Interix (INTX) file. */ + stbuf->st_mode = S_IFREG; + stbuf->st_size = ni->data_size; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* + * return data size rounded to next 512 byte boundary for + * encrypted files to include padding required for decryption + * also include 2 bytes for padding info + */ + if (ctx->efs_raw + && (ni->flags & FILE_ATTR_ENCRYPTED) + && ni->data_size) + stbuf->st_size = ((ni->data_size + 511) & ~511) + 2; +#endif /* HAVE_SETXATTR */ + /* + * Temporary fix to make ActiveSync work via Samba 3.0. + * See more on the ntfs-3g-devel list. + */ + stbuf->st_blocks = (ni->allocated_size + 511) >> 9; + if (ni->flags & FILE_ATTR_SYSTEM || stream_name_len) { + na = ntfs_attr_open(ni, AT_DATA, stream_name, + stream_name_len); + if (!na) { + if (stream_name_len) { + res = -ENOENT; + goto exit; + } else + goto nodata; + } + if (stream_name_len) { + stbuf->st_size = na->data_size; + stbuf->st_blocks = na->allocated_size >> 9; + } + /* Check whether it's Interix FIFO or socket. */ + if (!(ni->flags & FILE_ATTR_HIDDEN) && + !stream_name_len) { + /* FIFO. */ + if (na->data_size == 0) + stbuf->st_mode = S_IFIFO; + /* Socket link. */ + if (na->data_size == 1) + stbuf->st_mode = S_IFSOCK; + } +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* encrypted named stream */ + /* round size up to next 512 byte boundary */ + if (ctx->efs_raw && stream_name_len && + (na->data_flags & ATTR_IS_ENCRYPTED) && + NAttrNonResident(na)) + stbuf->st_size = ((na->data_size+511) & ~511)+2; +#endif /* HAVE_SETXATTR */ + /* + * Check whether it's Interix symbolic link, block or + * character device. + */ + if ((u64)na->data_size <= sizeof(INTX_FILE_TYPES) + + sizeof(ntfschar) * PATH_MAX + && (u64)na->data_size > + sizeof(INTX_FILE_TYPES) + && !stream_name_len) { + + INTX_FILE *intx_file; + + intx_file = ntfs_malloc(na->data_size); + if (!intx_file) { + res = -errno; + ntfs_attr_close(na); + goto exit; + } + if (ntfs_attr_pread(na, 0, na->data_size, + intx_file) != na->data_size) { + res = -errno; + free(intx_file); + ntfs_attr_close(na); + goto exit; + } + if (intx_file->magic == INTX_BLOCK_DEVICE && + na->data_size == offsetof( + INTX_FILE, device_end)) { + stbuf->st_mode = S_IFBLK; + stbuf->st_rdev = makedev(le64_to_cpu( + intx_file->major), + le64_to_cpu( + intx_file->minor)); + } + if (intx_file->magic == INTX_CHARACTER_DEVICE && + na->data_size == offsetof( + INTX_FILE, device_end)) { + stbuf->st_mode = S_IFCHR; + stbuf->st_rdev = makedev(le64_to_cpu( + intx_file->major), + le64_to_cpu( + intx_file->minor)); + } + if (intx_file->magic == INTX_SYMBOLIC_LINK) { + char *target = NULL; + int len; + + /* st_size should be set to length of + * symlink target as multibyte string */ + len = ntfs_ucstombs( + intx_file->target, + (na->data_size - + offsetof(INTX_FILE, + target)) / + sizeof(ntfschar), + &target, 0); + if (len < 0) { + res = -errno; + free(intx_file); + ntfs_attr_close(na); + goto exit; + } + free(target); + stbuf->st_mode = S_IFLNK; + stbuf->st_size = len; + } + free(intx_file); + } + ntfs_attr_close(na); + } + stbuf->st_mode |= (0777 & ~ctx->fmask); + } +#ifndef DISABLE_PLUGINS +ok: +#endif /* DISABLE_PLUGINS */ + if (withusermapping) { + if (ntfs_get_owner_mode(&security,ni,stbuf) < 0) + set_fuse_error(&res); + } else { + stbuf->st_uid = ctx->uid; + stbuf->st_gid = ctx->gid; + } + if (S_ISLNK(stbuf->st_mode)) + stbuf->st_mode |= 0777; +nodata : + stbuf->st_ino = ni->mft_no; +#ifdef HAVE_STRUCT_STAT_ST_ATIMESPEC + stbuf->st_atimespec = ntfs2timespec(ni->last_access_time); + stbuf->st_ctimespec = ntfs2timespec(ni->last_mft_change_time); + stbuf->st_mtimespec = ntfs2timespec(ni->last_data_change_time); +#elif defined(HAVE_STRUCT_STAT_ST_ATIM) + stbuf->st_atim = ntfs2timespec(ni->last_access_time); + stbuf->st_ctim = ntfs2timespec(ni->last_mft_change_time); + stbuf->st_mtim = ntfs2timespec(ni->last_data_change_time); +#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC) + { + struct timespec ts; + + ts = ntfs2timespec(ni->last_access_time); + stbuf->st_atime = ts.tv_sec; + stbuf->st_atimensec = ts.tv_nsec; + ts = ntfs2timespec(ni->last_mft_change_time); + stbuf->st_ctime = ts.tv_sec; + stbuf->st_ctimensec = ts.tv_nsec; + ts = ntfs2timespec(ni->last_data_change_time); + stbuf->st_mtime = ts.tv_sec; + stbuf->st_mtimensec = ts.tv_nsec; + } +#else +#warning "No known way to set nanoseconds in struct stat !" + { + struct timespec ts; + + ts = ntfs2timespec(ni->last_access_time); + stbuf->st_atime = ts.tv_sec; + ts = ntfs2timespec(ni->last_mft_change_time); + stbuf->st_ctime = ts.tv_sec; + ts = ntfs2timespec(ni->last_data_change_time); + stbuf->st_mtime = ts.tv_sec; + } +#endif +exit: + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(path); + if (stream_name_len) + free(stream_name); + return res; +} + +#ifndef DISABLE_PLUGINS + +/* + * Get the link defined by a junction or symlink + * (internal plugin) + */ + +static int junction_readlink(ntfs_inode *ni, + const REPARSE_POINT *reparse __attribute__((unused)), + char **pbuf) +{ + int res; + le32 tag; + int lth; + + errno = 0; + res = 0; + *pbuf = ntfs_make_symlink(ni, ctx->abs_mnt_point); + if (!*pbuf) { + if (errno == EOPNOTSUPP) { + *pbuf = (char*)ntfs_malloc(ntfs_bad_reparse_lth + 1); + if (*pbuf) { + if (reparse) + tag = reparse->reparse_tag; + else + tag = const_cpu_to_le32(0); + lth = snprintf(*pbuf, ntfs_bad_reparse_lth + 1, + ntfs_bad_reparse, + (long)le32_to_cpu(tag)); + if (lth != ntfs_bad_reparse_lth) { + free(*pbuf); + *pbuf = (char*)NULL; + res = -errno; + } + } else + res = -ENOMEM; + } else + res = -errno; + } + return (res); +} + +#endif /* DISABLE_PLUGINS */ + +static int ntfs_fuse_readlink(const char *org_path, char *buf, size_t buf_size) +{ + char *path = NULL; + ntfschar *stream_name; + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + INTX_FILE *intx_file = NULL; + int stream_name_len, res = 0; + REPARSE_POINT *reparse; + le32 tag; + int lth; + + /* Get inode. */ + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + if (stream_name_len > 0) { + res = -EINVAL; + goto exit; + } + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + goto exit; + } + /* + * Reparse point : analyze as a junction point + */ + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + char *gotlink; + const plugin_operations_t *ops; + + gotlink = (char*)NULL; + res = CALL_REPARSE_PLUGIN(ni, readlink, &gotlink); + if (gotlink) { + strncpy(buf, gotlink, buf_size); + free(gotlink); + res = 0; + } else { + errno = EOPNOTSUPP; + res = -EOPNOTSUPP; + } +#else /* DISABLE_PLUGINS */ + char *target; + + errno = 0; + res = 0; + target = ntfs_make_symlink(ni, ctx->abs_mnt_point); + if (target) { + strncpy(buf,target,buf_size); + free(target); + } else + res = -errno; +#endif /* DISABLE_PLUGINS */ + if (res == -EOPNOTSUPP) { + reparse = ntfs_get_reparse_point(ni); + if (reparse) { + tag = reparse->reparse_tag; + free(reparse); + } else + tag = const_cpu_to_le32(0); + lth = snprintf(buf, ntfs_bad_reparse_lth + 1, + ntfs_bad_reparse, + (long)le32_to_cpu(tag)); + res = 0; + if (lth != ntfs_bad_reparse_lth) + res = -errno; + } + goto exit; + } + /* Sanity checks. */ + if (!(ni->flags & FILE_ATTR_SYSTEM)) { + res = -EINVAL; + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + res = -errno; + goto exit; + } + if ((size_t)na->data_size <= sizeof(INTX_FILE_TYPES)) { + res = -EINVAL; + goto exit; + } + if ((size_t)na->data_size > sizeof(INTX_FILE_TYPES) + + sizeof(ntfschar) * PATH_MAX) { + res = -ENAMETOOLONG; + goto exit; + } + /* Receive file content. */ + intx_file = ntfs_malloc(na->data_size); + if (!intx_file) { + res = -errno; + goto exit; + } + if (ntfs_attr_pread(na, 0, na->data_size, intx_file) != na->data_size) { + res = -errno; + goto exit; + } + /* Sanity check. */ + if (intx_file->magic != INTX_SYMBOLIC_LINK) { + res = -EINVAL; + goto exit; + } + /* Convert link from unicode to local encoding. */ + if (ntfs_ucstombs(intx_file->target, (na->data_size - + offsetof(INTX_FILE, target)) / sizeof(ntfschar), + &buf, buf_size) < 0) { + res = -errno; + goto exit; + } +exit: + if (intx_file) + free(intx_file); + if (na) + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(path); + if (stream_name_len) + free(stream_name); + return res; +} + +static int ntfs_fuse_filler(ntfs_fuse_fill_context_t *fill_ctx, + const ntfschar *name, const int name_len, const int name_type, + const s64 pos __attribute__((unused)), const MFT_REF mref, + const unsigned dt_type __attribute__((unused))) +{ + char *filename = NULL; + int ret = 0; + int filenamelen = -1; + + if (name_type == FILE_NAME_DOS) + return 0; + + if ((filenamelen = ntfs_ucstombs(name, name_len, &filename, 0)) < 0) { + ntfs_log_perror("Filename decoding failed (inode %llu)", + (unsigned long long)MREF(mref)); + return -1; + } + + if (ntfs_fuse_is_named_data_stream(filename)) { + ntfs_log_error("Unable to access '%s' (inode %llu) with " + "current named streams access interface.\n", + filename, (unsigned long long)MREF(mref)); + free(filename); + return 0; + } else { + struct stat st = { .st_ino = MREF(mref) }; +#ifndef DISABLE_PLUGINS + ntfs_inode *ni; +#endif /* DISABLE_PLUGINS */ + + switch (dt_type) { + case NTFS_DT_DIR : + st.st_mode = S_IFDIR | (0777 & ~ctx->dmask); + break; + case NTFS_DT_LNK : + st.st_mode = S_IFLNK | 0777; + break; + case NTFS_DT_FIFO : + st.st_mode = S_IFIFO; + break; + case NTFS_DT_SOCK : + st.st_mode = S_IFSOCK; + break; + case NTFS_DT_BLK : + st.st_mode = S_IFBLK; + break; + case NTFS_DT_CHR : + st.st_mode = S_IFCHR; + break; + case NTFS_DT_REPARSE : + st.st_mode = S_IFLNK | 0777; /* default */ +#ifndef DISABLE_PLUGINS + /* get emulated type from plugin if available */ + ni = ntfs_inode_open(ctx->vol, mref); + if (ni && (ni->flags & FILE_ATTR_REPARSE_POINT)) { + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + int res; + + res = CALL_REPARSE_PLUGIN(ni, getattr, &st); + if (!res) + apply_umask(&st); + else + st.st_mode = S_IFLNK; + } + if (ni) + ntfs_inode_close(ni); +#endif /* DISABLE_PLUGINS */ + break; + default : /* unexpected types shown as plain files */ + case NTFS_DT_REG : + st.st_mode = S_IFREG | (0777 & ~ctx->fmask); + break; + } + +#if defined(__APPLE__) || defined(__DARWIN__) + /* + * Returning file names larger than MAXNAMLEN (255) bytes + * causes Darwin/Mac OS X to bug out and skip the entry. + */ + if (filenamelen > MAXNAMLEN) { + ntfs_log_debug("Truncating %d byte filename to " + "%d bytes.\n", filenamelen, MAXNAMLEN); + ntfs_log_debug(" before: '%s'\n", filename); + memset(filename + MAXNAMLEN, 0, filenamelen - MAXNAMLEN); + ntfs_log_debug(" after: '%s'\n", filename); + } +#elif defined(__sun) && defined (__SVR4) + /* + * Returning file names larger than MAXNAMELEN (256) bytes + * causes Solaris/Illumos to return an I/O error from the system + * call. + * However we also need space for a terminating NULL, or user + * space tools will bug out since they expect a NULL terminator. + * Effectively the maximum length of a file name is MAXNAMELEN - + * 1 (255). + */ + if (filenamelen > (MAXNAMELEN - 1)) { + ntfs_log_debug("Truncating %d byte filename to %d " + "bytes.\n", filenamelen, MAXNAMELEN - 1); + ntfs_log_debug(" before: '%s'\n", filename); + memset(&filename[MAXNAMELEN - 1], 0, + filenamelen - (MAXNAMELEN - 1)); + ntfs_log_debug(" after: '%s'\n", filename); + } +#endif /* defined(__APPLE__) || defined(__DARWIN__), ... */ + + ret = fill_ctx->filler(fill_ctx->buf, filename, &st, 0); + } + + free(filename); + return ret; +} + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + +static int ntfs_fuse_opendir(const char *path, + struct fuse_file_info *fi) +{ + int res = 0; + ntfs_inode *ni; + int accesstype; + struct SECURITY_CONTEXT security; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (ni) { + if (ntfs_fuse_fill_security_context(&security)) { + if (fi->flags & O_WRONLY) + accesstype = S_IWRITE; + else + if (fi->flags & O_RDWR) + accesstype = S_IWRITE | S_IREAD; + else + accesstype = S_IREAD; + /* + * directory must be searchable + * and requested access be allowed + */ + if (!strcmp(path,"/") + ? !ntfs_allowed_dir_access(&security, + path, ni, ni, accesstype | S_IEXEC) + : !ntfs_allowed_dir_access(&security, path, + (ntfs_inode*)NULL, ni, S_IEXEC) + || !ntfs_allowed_access(&security, + ni,accesstype)) + res = -EACCES; + } + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + fi->fh = 0; + res = CALL_REPARSE_PLUGIN(ni, opendir, fi); +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + } + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } else + res = -errno; + return res; +} + +#endif + +static int ntfs_fuse_readdir(const char *path, void *buf, + fuse_fill_dir_t filler, off_t offset __attribute__((unused)), + struct fuse_file_info *fi __attribute__((unused))) +{ + ntfs_fuse_fill_context_t fill_ctx; + ntfs_inode *ni; + s64 pos = 0; + int err = 0; + + fill_ctx.filler = filler; + fill_ctx.buf = buf; + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + err = CALL_REPARSE_PLUGIN(ni, readdir, &pos, &fill_ctx, + (ntfs_filldir_t)ntfs_fuse_filler, fi); +#else /* DISABLE_PLUGINS */ + err = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + } else { + if (ntfs_readdir(ni, &pos, &fill_ctx, + (ntfs_filldir_t)ntfs_fuse_filler)) + err = -errno; + } + ntfs_fuse_update_times(ni, NTFS_UPDATE_ATIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&err); + return err; +} + +static int ntfs_fuse_open(const char *org_path, +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct fuse_file_info *fi) +#else + struct fuse_file_info *fi __attribute__((unused))) +#endif +{ + ntfs_inode *ni; + ntfs_attr *na = NULL; + int res = 0; + char *path = NULL; + ntfschar *stream_name; + int stream_name_len; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + int accesstype; + struct SECURITY_CONTEXT security; +#endif + + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (ni) { + if (!(ni->flags & FILE_ATTR_REPARSE_POINT)) { + na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len); + if (!na) { + res = -errno; + goto close; + } + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (ntfs_fuse_fill_security_context(&security)) { + if (fi->flags & O_WRONLY) + accesstype = S_IWRITE; + else + if (fi->flags & O_RDWR) + accesstype = S_IWRITE | S_IREAD; + else + accesstype = S_IREAD; + /* + * directory must be searchable + * and requested access allowed + */ + if (!ntfs_allowed_dir_access(&security, + path,(ntfs_inode*)NULL,ni,S_IEXEC) + || !ntfs_allowed_access(&security, + ni,accesstype)) + res = -EACCES; + } +#endif + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + fi->fh = 0; + res = CALL_REPARSE_PLUGIN(ni, open, fi); +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + goto close; + } + if ((res >= 0) + && (fi->flags & (O_WRONLY | O_RDWR))) { + /* mark a future need to compress the last chunk */ + if (na->data_flags & ATTR_COMPRESSION_MASK) + fi->fh |= CLOSE_COMPRESSED; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* mark a future need to fixup encrypted inode */ + if (ctx->efs_raw + && !(na->data_flags & ATTR_IS_ENCRYPTED) + && (ni->flags & FILE_ATTR_ENCRYPTED)) + fi->fh |= CLOSE_ENCRYPTED; +#endif /* HAVE_SETXATTR */ + /* mark a future need to update the mtime */ + if (ctx->dmtime) + fi->fh |= CLOSE_DMTIME; + /* deny opening metadata files for writing */ + if (ni->mft_no < FILE_first_user) + res = -EPERM; + } + ntfs_attr_close(na); +close: + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } else + res = -errno; + free(path); + if (stream_name_len) + free(stream_name); + return res; +} + +static int ntfs_fuse_read(const char *org_path, char *buf, size_t size, + off_t offset, struct fuse_file_info *fi __attribute__((unused))) +{ + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + char *path = NULL; + ntfschar *stream_name; + int stream_name_len, res; + s64 total = 0; + s64 max_read; + + if (!size) + return 0; + + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + goto exit; + } + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + if (stream_name_len || !fi) { + res = -EINVAL; + goto exit; + } + res = CALL_REPARSE_PLUGIN(ni, read, buf, size, offset, fi); + if (res >= 0) { + goto stamps; + } +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len); + if (!na) { + res = -errno; + goto exit; + } + max_read = na->data_size; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* limit reads at next 512 byte boundary for encrypted attributes */ + if (ctx->efs_raw + && max_read + && (na->data_flags & ATTR_IS_ENCRYPTED) + && NAttrNonResident(na)) { + max_read = ((na->data_size+511) & ~511) + 2; + } +#endif /* HAVE_SETXATTR */ + if (offset + (off_t)size > max_read) { + if (max_read < offset) + goto ok; + size = max_read - offset; + } + while (size > 0) { + s64 ret = ntfs_attr_pread(na, offset, size, buf + total); + if (ret != (s64)size) + ntfs_log_perror("ntfs_attr_pread error reading '%s' at " + "offset %lld: %lld <> %lld", org_path, + (long long)offset, (long long)size, (long long)ret); + if (ret <= 0 || ret > (s64)size) { + res = (ret < 0) ? -errno : -EIO; + goto exit; + } + size -= ret; + offset += ret; + total += ret; + } +ok: + res = total; +#ifndef DISABLE_PLUGINS +stamps: +#endif /* DISABLE_PLUGINS */ + ntfs_fuse_update_times(ni, NTFS_UPDATE_ATIME); +exit: + if (na) + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(path); + if (stream_name_len) + free(stream_name); + return res; +} + +static int ntfs_fuse_write(const char *org_path, const char *buf, size_t size, + off_t offset, struct fuse_file_info *fi __attribute__((unused))) +{ + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + char *path = NULL; + ntfschar *stream_name; + int stream_name_len, res, total = 0; + + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) { + res = stream_name_len; + goto out; + } + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + goto exit; + } + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + if (stream_name_len || !fi) { + res = -EINVAL; + goto exit; + } + res = CALL_REPARSE_PLUGIN(ni, write, buf, size, offset, fi); + if (res >= 0) { + goto stamps; + } +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len); + if (!na) { + res = -errno; + goto exit; + } + while (size) { + s64 ret = ntfs_attr_pwrite(na, offset, size, buf + total); + if (ret <= 0) { + res = -errno; + goto exit; + } + size -= ret; + offset += ret; + total += ret; + } + res = total; +#ifndef DISABLE_PLUGINS +stamps: +#endif /* DISABLE_PLUGINS */ + if ((res > 0) + && (!ctx->dmtime + || (sle64_to_cpu(ntfs_current_time()) + - sle64_to_cpu(ni->last_data_change_time)) > ctx->dmtime)) + ntfs_fuse_update_times(ni, NTFS_UPDATE_MCTIME); +exit: + if (na) + ntfs_attr_close(na); + if (res > 0) + set_archive(ni); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(path); + if (stream_name_len) + free(stream_name); +out: + return res; +} + +static int ntfs_fuse_release(const char *org_path, + struct fuse_file_info *fi) +{ + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + char *path = NULL; + ntfschar *stream_name; + int stream_name_len, res; + + if (!fi) { + res = -EINVAL; + goto out; + } + + /* Only for marked descriptors there is something to do */ + + if (!fi->fh) { + res = 0; + goto out; + } + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) { + res = stream_name_len; + goto out; + } + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + goto exit; + } + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + if (stream_name_len) { + res = -EINVAL; + goto exit; + } + res = CALL_REPARSE_PLUGIN(ni, release, fi); + if (!res) { + goto stamps; + } +#else /* DISABLE_PLUGINS */ + /* Assume release() was not needed */ + res = 0; +#endif /* DISABLE_PLUGINS */ + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len); + if (!na) { + res = -errno; + goto exit; + } + res = 0; + if (fi->fh & CLOSE_COMPRESSED) + res = ntfs_attr_pclose(na); +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + if (fi->fh & CLOSE_ENCRYPTED) + res = ntfs_efs_fixup_attribute(NULL, na); +#endif /* HAVE_SETXATTR */ +#ifndef DISABLE_PLUGINS +stamps: +#endif /* DISABLE_PLUGINS */ + if (fi->fh & CLOSE_DMTIME) + ntfs_inode_update_times(ni,NTFS_UPDATE_MCTIME); +exit: + if (na) + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(path); + if (stream_name_len) + free(stream_name); +out: + return res; +} + +/* + * Common part for truncate() and ftruncate() + */ + +static int ntfs_fuse_trunc(const char *org_path, off_t size, +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + BOOL chkwrite) +#else + BOOL chkwrite __attribute__((unused))) +#endif +{ + ntfs_inode *ni = NULL; + ntfs_attr *na = NULL; + int res; + char *path = NULL; + ntfschar *stream_name; + int stream_name_len; + s64 oldsize; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct SECURITY_CONTEXT security; +#endif + + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + goto exit; + /* deny truncating metadata files */ + if (ni->mft_no < FILE_first_user) { + errno = EPERM; + goto exit; + } + + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + if (stream_name_len) { + res = -EINVAL; + goto exit; + } + res = CALL_REPARSE_PLUGIN(ni, truncate, size); + if (!res) { + set_archive(ni); + goto stamps; + } +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len); + if (!na) + goto exit; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * JPA deny truncation if cannot search in parent directory + * or cannot write to file (already checked for ftruncate()) + */ + if (ntfs_fuse_fill_security_context(&security) + && (!ntfs_allowed_dir_access(&security, path, + (ntfs_inode*)NULL, ni, S_IEXEC) + || (chkwrite + && !ntfs_allowed_access(&security, ni, S_IWRITE)))) { + errno = EACCES; + goto exit; + } +#endif + /* + * For compressed files, upsizing is done by inserting a final + * zero, which is optimized as creating a hole when possible. + */ + oldsize = na->data_size; + if ((na->data_flags & ATTR_COMPRESSION_MASK) + && (size > na->initialized_size)) { + char zero = 0; + if (ntfs_attr_pwrite(na, size - 1, 1, &zero) <= 0) + goto exit; + } else + if (ntfs_attr_truncate(na, size)) + goto exit; + if (oldsize != size) + set_archive(ni); + +#ifndef DISABLE_PLUGINS +stamps: +#endif /* DISABLE_PLUGINS */ + ntfs_fuse_update_times(ni, NTFS_UPDATE_MCTIME); + errno = 0; +exit: + res = -errno; + ntfs_attr_close(na); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(path); + if (stream_name_len) + free(stream_name); + return res; +} + +static int ntfs_fuse_truncate(const char *org_path, off_t size) +{ + return ntfs_fuse_trunc(org_path, size, TRUE); +} + +static int ntfs_fuse_ftruncate(const char *org_path, off_t size, + struct fuse_file_info *fi __attribute__((unused))) +{ + /* + * in ->ftruncate() the file handle is guaranteed + * to have been opened for write. + */ + return (ntfs_fuse_trunc(org_path, size, FALSE)); +} + +static int ntfs_fuse_chmod(const char *path, + mode_t mode) +{ + int res = 0; + ntfs_inode *ni; + struct SECURITY_CONTEXT security; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + + /* + * Return unsupported if no user mapping has been defined + * or enforcing Windows-type inheritance + */ + if (ctx->inherit + || !ntfs_fuse_fill_security_context(&security)) { + if (ctx->silent) + res = 0; + else + res = -EOPNOTSUPP; + } else { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* parent directory must be executable */ + if (ntfs_allowed_dir_access(&security,path, + (ntfs_inode*)NULL,(ntfs_inode*)NULL,S_IEXEC)) { +#endif + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + res = -errno; + else { + if (ntfs_set_mode(&security,ni,mode)) + res = -errno; + else + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + NInoSetDirty(ni); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + } else + res = -errno; +#endif + } + return res; +} + +static int ntfs_fuse_chown(const char *path, uid_t uid, gid_t gid) +{ + ntfs_inode *ni; + int res; + struct SECURITY_CONTEXT security; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + /* + * Return unsupported if no user mapping has been defined + * or enforcing Windows-type inheritance + */ + if (ctx->inherit + || !ntfs_fuse_fill_security_context(&security)) { + if (ctx->silent) + return 0; + if (uid == ctx->uid && gid == ctx->gid) + return 0; + return -EOPNOTSUPP; + } else { + res = 0; + if (((int)uid != -1) || ((int)gid != -1)) { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* parent directory must be executable */ + + if (ntfs_allowed_dir_access(&security,path, + (ntfs_inode*)NULL,(ntfs_inode*)NULL,S_IEXEC)) { +#endif + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + res = -errno; + else { + if (ntfs_set_owner(&security, + ni,uid,gid)) + res = -errno; + else + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + } else + res = -errno; +#endif + } + } + return (res); +} + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + +static int ntfs_fuse_access(const char *path, int type) +{ + int res = 0; + int mode; + ntfs_inode *ni; + struct SECURITY_CONTEXT security; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + + /* JPA return unsupported if no user mapping has been defined */ + if (!ntfs_fuse_fill_security_context(&security)) { + if (ctx->silent) + res = 0; + else + res = -EOPNOTSUPP; + } else { + /* parent directory must be seachable */ + if (ntfs_allowed_dir_access(&security,path,(ntfs_inode*)NULL, + (ntfs_inode*)NULL,S_IEXEC)) { + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + } else { + mode = 0; + if (type & (X_OK | W_OK | R_OK)) { + if (type & X_OK) mode += S_IEXEC; + if (type & W_OK) mode += S_IWRITE; + if (type & R_OK) mode += S_IREAD; + if (!ntfs_allowed_access(&security, + ni, mode)) + res = -errno; + } + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } else + res = -errno; + } + return (res); +} + +#endif + +static int ntfs_fuse_create(const char *org_path, mode_t typemode, dev_t dev, + const char *target, struct fuse_file_info *fi) +{ + char *name; + ntfschar *uname = NULL, *utarget = NULL; + ntfs_inode *dir_ni = NULL, *ni; + char *dir_path; + le32 securid; + char *path = NULL; + gid_t gid; + mode_t dsetgid; + ntfschar *stream_name; + int stream_name_len; + mode_t type = typemode & ~07777; + mode_t perm; + struct SECURITY_CONTEXT security; + int res = 0, uname_len, utarget_len; + + dir_path = strdup(org_path); + if (!dir_path) + return -errno; + /* Generate unicode filename. */ + name = strrchr(dir_path, '/'); + name++; + uname_len = ntfs_mbstoucs(name, &uname); + if ((uname_len < 0) + || (ctx->windows_names + && ntfs_forbidden_names(ctx->vol,uname,uname_len,TRUE))) { + res = -errno; + goto exit; + } + stream_name_len = ntfs_fuse_parse_path(org_path, + &path, &stream_name); + /* stream name validity has been checked previously */ + if (stream_name_len < 0) { + res = stream_name_len; + goto exit; + } + /* Open parent directory. */ + *--name = 0; + dir_ni = ntfs_pathname_to_inode(ctx->vol, NULL, dir_path); + /* Deny creating files in $Extend */ + if (!dir_ni || (dir_ni->mft_no == FILE_Extend)) { + free(path); + res = -errno; + if (dir_ni) + res = -EPERM; + goto exit; + } +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* make sure parent directory is writeable and executable */ + if (!ntfs_fuse_fill_security_context(&security) + || ntfs_allowed_create(&security, + dir_ni, &gid, &dsetgid)) { +#else + ntfs_fuse_fill_security_context(&security); + ntfs_allowed_create(&security, dir_ni, &gid, &dsetgid); +#endif + if (S_ISDIR(type)) + perm = (typemode & ~ctx->dmask & 0777) + | (dsetgid & S_ISGID); + else + if ((ctx->special_files == NTFS_FILES_WSL) + && S_ISLNK(type)) + perm = typemode | 0777; + else + perm = typemode & ~ctx->fmask & 0777; + /* + * Try to get a security id available for + * file creation (from inheritance or argument). + * This is not possible for NTFS 1.x, and we will + * have to build a security attribute later. + */ + if (!ctx->security.mapping[MAPUSERS]) + securid = const_cpu_to_le32(0); + else + if (ctx->inherit) + securid = ntfs_inherited_id(&security, + dir_ni, S_ISDIR(type)); + else +#if POSIXACLS + securid = ntfs_alloc_securid(&security, + security.uid, gid, + dir_ni, perm, S_ISDIR(type)); +#else + securid = ntfs_alloc_securid(&security, + security.uid, gid, + perm & ~security.umask, S_ISDIR(type)); +#endif + /* Create object specified in @type. */ + if (dir_ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + reparse = (REPARSE_POINT*)NULL; + ops = select_reparse_plugin(ctx, dir_ni, &reparse); + if (ops && ops->create) { + ni = (*ops->create)(dir_ni, reparse, + securid, uname, uname_len, type); + } else { + ni = (ntfs_inode*)NULL; + errno = EOPNOTSUPP; + } + free(reparse); +#else /* DISABLE_PLUGINS */ + errno = EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + } else { + switch (type) { + case S_IFCHR: + case S_IFBLK: + ni = ntfs_create_device(dir_ni, securid, + uname, uname_len, type, dev); + break; + case S_IFLNK: + utarget_len = ntfs_mbstoucs(target, + &utarget); + if (utarget_len < 0) { + res = -errno; + goto exit; + } + ni = ntfs_create_symlink(dir_ni, + securid, uname, uname_len, + utarget, utarget_len); + break; + default: + ni = ntfs_create(dir_ni, securid, + uname, uname_len, type); + break; + } + } + if (ni) { + /* + * set the security attribute if a security id + * could not be allocated (eg NTFS 1.x) + */ + if (ctx->security.mapping[MAPUSERS]) { +#if POSIXACLS + if (!securid + && ntfs_set_inherited_posix(&security, ni, + security.uid, gid, + dir_ni, perm) < 0) + set_fuse_error(&res); +#else + if (!securid + && ntfs_set_owner_mode(&security, ni, + security.uid, gid, + perm & ~security.umask) < 0) + set_fuse_error(&res); +#endif + } + set_archive(ni); + /* mark a need to compress the end of file */ + if (fi && (ni->flags & FILE_ATTR_COMPRESSED)) { + fi->fh |= CLOSE_COMPRESSED; + } +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* mark a future need to fixup encrypted inode */ + if (fi + && ctx->efs_raw + && (ni->flags & FILE_ATTR_ENCRYPTED)) + fi->fh |= CLOSE_ENCRYPTED; +#endif /* HAVE_SETXATTR */ + /* mark a need to update the mtime */ + if (fi && ctx->dmtime) + fi->fh |= CLOSE_DMTIME; + NInoSetDirty(ni); + /* + * closing ni requires access to dir_ni to + * synchronize the index, avoid double opening. + */ + if (ntfs_inode_close_in_dir(ni, dir_ni)) + set_fuse_error(&res); + ntfs_fuse_update_times(dir_ni, NTFS_UPDATE_MCTIME); + } else + res = -errno; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + } else + res = -errno; +#endif + free(path); + +exit: + free(uname); + if (ntfs_inode_close(dir_ni)) + set_fuse_error(&res); + if (utarget) + free(utarget); + free(dir_path); + return res; +} + +static int ntfs_fuse_create_stream(const char *path, + ntfschar *stream_name, const int stream_name_len, + struct fuse_file_info *fi) +{ + ntfs_inode *ni; + int res = 0; + + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + if (res == -ENOENT) { + /* + * If such file does not exist, create it and try once + * again to add stream to it. + * Note : no fuse_file_info for creation of main file + */ + res = ntfs_fuse_create(path, S_IFREG, 0, NULL, + (struct fuse_file_info*)NULL); + if (!res) + return ntfs_fuse_create_stream(path, + stream_name, stream_name_len,fi); + else + res = -errno; + } + return res; + } + if (ntfs_attr_add(ni, AT_DATA, stream_name, stream_name_len, NULL, 0)) + res = -errno; + else + set_archive(ni); + + if ((res >= 0) + && fi + && (fi->flags & (O_WRONLY | O_RDWR))) { + /* mark a future need to compress the last block */ + if (ni->flags & FILE_ATTR_COMPRESSED) + fi->fh |= CLOSE_COMPRESSED; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + /* mark a future need to fixup encrypted inode */ + if (ctx->efs_raw + && (ni->flags & FILE_ATTR_ENCRYPTED)) + fi->fh |= CLOSE_ENCRYPTED; +#endif /* HAVE_SETXATTR */ + if (ctx->dmtime) + fi->fh |= CLOSE_DMTIME; + } + + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +static int ntfs_fuse_mknod_common(const char *org_path, mode_t mode, dev_t dev, + struct fuse_file_info *fi) +{ + char *path = NULL; + ntfschar *stream_name; + int stream_name_len; + int res = 0; + + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + if (stream_name_len + && (!S_ISREG(mode) + || (ctx->windows_names + && ntfs_forbidden_names(ctx->vol,stream_name, + stream_name_len, TRUE)))) { + res = -EINVAL; + goto exit; + } + if (!stream_name_len) + res = ntfs_fuse_create(path, mode & (S_IFMT | 07777), dev, + NULL,fi); + else + res = ntfs_fuse_create_stream(path, stream_name, + stream_name_len,fi); +exit: + free(path); + if (stream_name_len) + free(stream_name); + return res; +} + +static int ntfs_fuse_mknod(const char *path, mode_t mode, dev_t dev) +{ + return ntfs_fuse_mknod_common(path, mode, dev, + (struct fuse_file_info*)NULL); +} + +static int ntfs_fuse_create_file(const char *path, mode_t mode, + struct fuse_file_info *fi) +{ + return ntfs_fuse_mknod_common(path, mode, 0, fi); +} + +static int ntfs_fuse_symlink(const char *to, const char *from) +{ + if (ntfs_fuse_is_named_data_stream(from)) + return -EINVAL; /* n/a for named data streams. */ + return ntfs_fuse_create(from, S_IFLNK, 0, to, + (struct fuse_file_info*)NULL); +} + +static int ntfs_fuse_link(const char *old_path, const char *new_path) +{ + char *name; + ntfschar *uname = NULL; + ntfs_inode *dir_ni = NULL, *ni; + char *path; + int res = 0, uname_len; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + BOOL samedir; + struct SECURITY_CONTEXT security; +#endif + + if (ntfs_fuse_is_named_data_stream(old_path)) + return -EINVAL; /* n/a for named data streams. */ + if (ntfs_fuse_is_named_data_stream(new_path)) + return -EINVAL; /* n/a for named data streams. */ + path = strdup(new_path); + if (!path) + return -errno; + /* Open file for which create hard link. */ + ni = ntfs_pathname_to_inode(ctx->vol, NULL, old_path); + if (!ni) { + res = -errno; + goto exit; + } + + /* Generate unicode filename. */ + name = strrchr(path, '/'); + name++; + uname_len = ntfs_mbstoucs(name, &uname); + if ((uname_len < 0) + || (ctx->windows_names + && ntfs_forbidden_names(ctx->vol,uname,uname_len,TRUE))) { + res = -errno; + goto exit; + } + /* Open parent directory. */ + *--name = 0; + dir_ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!dir_ni) { + res = -errno; + goto exit; + } + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + samedir = !strncmp(old_path, path, strlen(path)) + && (old_path[strlen(path)] == '/'); + /* JPA make sure the parent directories are writeable */ + if (ntfs_fuse_fill_security_context(&security) + && ((!samedir && !ntfs_allowed_dir_access(&security,old_path, + (ntfs_inode*)NULL,ni,S_IWRITE + S_IEXEC)) + || !ntfs_allowed_access(&security,dir_ni,S_IWRITE + S_IEXEC))) + res = -EACCES; + else +#endif + { + if (dir_ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + res = CALL_REPARSE_PLUGIN(dir_ni, link, + ni, uname, uname_len); +#else /* DISABLE_PLUGINS */ + errno = EOPNOTSUPP; + res = -errno; +#endif /* DISABLE_PLUGINS */ + if (res) + goto exit; + } else + if (ntfs_link(ni, dir_ni, uname, uname_len)) { + res = -errno; + goto exit; + } + + set_archive(ni); + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + ntfs_fuse_update_times(dir_ni, NTFS_UPDATE_MCTIME); + } +exit: + /* + * Must close dir_ni first otherwise ntfs_inode_sync_file_name(ni) + * may fail because ni may not be in parent's index on the disk yet. + */ + if (ntfs_inode_close(dir_ni)) + set_fuse_error(&res); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(uname); + free(path); + return res; +} + +static int ntfs_fuse_rm(const char *org_path) +{ + char *name; + ntfschar *uname = NULL; + ntfs_inode *dir_ni = NULL, *ni; + char *path; + int res = 0, uname_len; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct SECURITY_CONTEXT security; +#endif + + path = strdup(org_path); + if (!path) + return -errno; + /* Open object for delete. */ + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) { + res = -errno; + goto exit; + } + /* deny unlinking metadata files */ + if (ni->mft_no < FILE_first_user) { + errno = EPERM; + res = -errno; + goto exit; + } + + /* Generate unicode filename. */ + name = strrchr(path, '/'); + name++; + uname_len = ntfs_mbstoucs(name, &uname); + if (uname_len < 0) { + res = -errno; + goto exit; + } + /* Open parent directory. */ + *--name = 0; + dir_ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + /* deny unlinking metadata files from $Extend */ + if (!dir_ni || (dir_ni->mft_no == FILE_Extend)) { + res = -errno; + if (dir_ni) + res = -EPERM; + goto exit; + } + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* JPA deny unlinking if directory is not writable and executable */ + if (!ntfs_fuse_fill_security_context(&security) + || ntfs_allowed_dir_access(&security, org_path, dir_ni, ni, + S_IEXEC + S_IWRITE + S_ISVTX)) { +#endif + if (dir_ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + const plugin_operations_t *ops; + REPARSE_POINT *reparse; + + res = CALL_REPARSE_PLUGIN(dir_ni, unlink, + org_path, ni, uname, uname_len); +#else /* DISABLE_PLUGINS */ + res = -EOPNOTSUPP; +#endif /* DISABLE_PLUGINS */ + } else + if (ntfs_delete(ctx->vol, org_path, ni, dir_ni, + uname, uname_len)) + res = -errno; + /* ntfs_delete() always closes ni and dir_ni */ + ni = dir_ni = NULL; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + } else + res = -EACCES; +#endif +exit: + if (ntfs_inode_close(dir_ni)) + set_fuse_error(&res); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + free(uname); + free(path); + return res; +} + +static int ntfs_fuse_rm_stream(const char *path, ntfschar *stream_name, + const int stream_name_len) +{ + ntfs_inode *ni; + int res = 0; + + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + if (ntfs_attr_remove(ni, AT_DATA, stream_name, stream_name_len)) + res = -errno; + + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +static int ntfs_fuse_unlink(const char *org_path) +{ + char *path = NULL; + ntfschar *stream_name; + int stream_name_len; + int res = 0; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct SECURITY_CONTEXT security; +#endif + + stream_name_len = ntfs_fuse_parse_path(org_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + if (!stream_name_len) + res = ntfs_fuse_rm(path); + else { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * JPA deny unlinking stream if directory is not + * writable and executable (debatable) + */ + if (!ntfs_fuse_fill_security_context(&security) + || ntfs_allowed_dir_access(&security, path, + (ntfs_inode*)NULL, (ntfs_inode*)NULL, + S_IEXEC + S_IWRITE + S_ISVTX)) + res = ntfs_fuse_rm_stream(path, stream_name, + stream_name_len); + else + res = -errno; +#else + res = ntfs_fuse_rm_stream(path, stream_name, stream_name_len); +#endif + } + free(path); + if (stream_name_len) + free(stream_name); + return res; +} + +static int ntfs_fuse_safe_rename(const char *old_path, + const char *new_path, + const char *tmp) +{ + int ret; + + ntfs_log_trace("Entering\n"); + + ret = ntfs_fuse_link(new_path, tmp); + if (ret) + return ret; + + ret = ntfs_fuse_unlink(new_path); + if (!ret) { + + ret = ntfs_fuse_link(old_path, new_path); + if (ret) + goto restore; + + ret = ntfs_fuse_unlink(old_path); + if (ret) { + if (ntfs_fuse_unlink(new_path)) + goto err; + goto restore; + } + } + + goto cleanup; +restore: + if (ntfs_fuse_link(tmp, new_path)) { +err: + ntfs_log_perror("Rename failed. Existing file '%s' was renamed " + "to '%s'", new_path, tmp); + } else { +cleanup: + /* + * Condition for this unlink has already been checked in + * "ntfs_fuse_rename_existing_dest()", so it should never + * fail (unless concurrent access to directories when fuse + * is multithreaded) + */ + if (ntfs_fuse_unlink(tmp) < 0) + ntfs_log_perror("Rename failed. Existing file '%s' still present " + "as '%s'", new_path, tmp); + } + return ret; +} + +static int ntfs_fuse_rename_existing_dest(const char *old_path, const char *new_path) +{ + int ret, len; + char *tmp; + const char *ext = ".ntfs-3g-"; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct SECURITY_CONTEXT security; +#endif + + ntfs_log_trace("Entering\n"); + + len = strlen(new_path) + strlen(ext) + 10 + 1; /* wc(str(2^32)) + \0 */ + tmp = ntfs_malloc(len); + if (!tmp) + return -errno; + + ret = snprintf(tmp, len, "%s%s%010d", new_path, ext, ++ntfs_sequence); + if (ret != len - 1) { + ntfs_log_error("snprintf failed: %d != %d\n", ret, len - 1); + ret = -EOVERFLOW; + } else { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * Make sure existing dest can be removed. + * This is only needed if parent directory is + * sticky, because in this situation condition + * for unlinking is different from condition for + * linking + */ + if (!ntfs_fuse_fill_security_context(&security) + || ntfs_allowed_dir_access(&security, new_path, + (ntfs_inode*)NULL, (ntfs_inode*)NULL, + S_IEXEC + S_IWRITE + S_ISVTX)) + ret = ntfs_fuse_safe_rename(old_path, new_path, tmp); + else + ret = -EACCES; +#else + ret = ntfs_fuse_safe_rename(old_path, new_path, tmp); +#endif + } + free(tmp); + return ret; +} + +static int ntfs_fuse_rename(const char *old_path, const char *new_path) +{ + int ret, stream_name_len; + char *path = NULL; + ntfschar *stream_name; + ntfs_inode *ni; + u64 inum; + BOOL same; + + ntfs_log_debug("rename: old: '%s' new: '%s'\n", old_path, new_path); + + /* + * FIXME: Rename should be atomic. + */ + stream_name_len = ntfs_fuse_parse_path(new_path, &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (ni) { + ret = ntfs_check_empty_dir(ni); + if (ret < 0) { + ret = -errno; + ntfs_inode_close(ni); + goto out; + } + + inum = ni->mft_no; + if (ntfs_inode_close(ni)) { + set_fuse_error(&ret); + goto out; + } + + free(path); + path = (char*)NULL; + if (stream_name_len) + free(stream_name); + + /* silently ignore a rename to same inode */ + stream_name_len = ntfs_fuse_parse_path(old_path, + &path, &stream_name); + if (stream_name_len < 0) + return stream_name_len; + + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (ni) { + same = ni->mft_no == inum; + if (ntfs_inode_close(ni)) + ret = -errno; + else + if (!same) + ret = ntfs_fuse_rename_existing_dest( + old_path, new_path); + } else + ret = -errno; + goto out; + } + + ret = ntfs_fuse_link(old_path, new_path); + if (ret) + goto out; + + ret = ntfs_fuse_unlink(old_path); + if (ret) + ntfs_fuse_unlink(new_path); +out: + free(path); + if (stream_name_len) + free(stream_name); + return ret; +} + +static int ntfs_fuse_mkdir(const char *path, + mode_t mode) +{ + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + return ntfs_fuse_create(path, S_IFDIR | (mode & 07777), 0, NULL, + (struct fuse_file_info*)NULL); +} + +static int ntfs_fuse_rmdir(const char *path) +{ + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ + return ntfs_fuse_rm(path); +} + +#ifdef HAVE_UTIMENSAT + +static int ntfs_fuse_utimens(const char *path, const struct timespec tv[2]) +{ + ntfs_inode *ni; + int res = 0; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct SECURITY_CONTEXT security; +#endif + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* parent directory must be executable */ + if (ntfs_fuse_fill_security_context(&security) + && !ntfs_allowed_dir_access(&security,path, + (ntfs_inode*)NULL,(ntfs_inode*)NULL,S_IEXEC)) { + return (-errno); + } +#endif + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + /* no check or update if both UTIME_OMIT */ + if ((tv[0].tv_nsec != UTIME_OMIT) || (tv[1].tv_nsec != UTIME_OMIT)) { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (ntfs_allowed_as_owner(&security, ni) + || ((tv[0].tv_nsec == UTIME_NOW) + && (tv[1].tv_nsec == UTIME_NOW) + && ntfs_allowed_access(&security, ni, S_IWRITE))) { +#endif + ntfs_time_update_flags mask = NTFS_UPDATE_CTIME; + + if (tv[0].tv_nsec == UTIME_NOW) + mask |= NTFS_UPDATE_ATIME; + else + if (tv[0].tv_nsec != UTIME_OMIT) + ni->last_access_time + = timespec2ntfs(tv[0]); + if (tv[1].tv_nsec == UTIME_NOW) + mask |= NTFS_UPDATE_MTIME; + else + if (tv[1].tv_nsec != UTIME_OMIT) + ni->last_data_change_time + = timespec2ntfs(tv[1]); + ntfs_inode_update_times(ni, mask); +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + } else + res = -errno; +#endif + } + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +#else /* HAVE_UTIMENSAT */ + +static int ntfs_fuse_utime(const char *path, struct utimbuf *buf) +{ + ntfs_inode *ni; + int res = 0; + struct timespec actime; + struct timespec modtime; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + BOOL ownerok; + BOOL writeok; + struct SECURITY_CONTEXT security; +#endif + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; /* n/a for named data streams. */ +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* parent directory must be executable */ + if (ntfs_fuse_fill_security_context(&security) + && !ntfs_allowed_dir_access(&security,path, + (ntfs_inode*)NULL,(ntfs_inode*)NULL,S_IEXEC)) { + return (-errno); + } +#endif + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + ownerok = ntfs_allowed_as_owner(&security, ni); + if (buf) { + /* + * fuse never calls with a NULL buf and we do not + * know whether the specific condition can be applied + * So we have to accept updating by a non-owner having + * write access. + */ + writeok = !ownerok + && (buf->actime == buf->modtime) + && ntfs_allowed_access(&security, ni, S_IWRITE); + /* Must be owner */ + if (!ownerok && !writeok) + res = (buf->actime == buf->modtime ? -EACCES : -EPERM); + else { + actime.tv_sec = buf->actime; + actime.tv_nsec = 0; + modtime.tv_sec = buf->modtime; + modtime.tv_nsec = 0; + ni->last_access_time = timespec2ntfs(actime); + ni->last_data_change_time = timespec2ntfs(modtime); + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + } + } else { + /* Must be owner or have write access */ + writeok = !ownerok + && ntfs_allowed_access(&security, ni, S_IWRITE); + if (!ownerok && !writeok) + res = -EACCES; + else + ntfs_inode_update_times(ni, NTFS_UPDATE_AMCTIME); + } +#else + if (buf) { + actime.tv_sec = buf->actime; + actime.tv_nsec = 0; + modtime.tv_sec = buf->modtime; + modtime.tv_nsec = 0; + ni->last_access_time = timespec2ntfs(actime); + ni->last_data_change_time = timespec2ntfs(modtime); + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + } else + ntfs_inode_update_times(ni, NTFS_UPDATE_AMCTIME); +#endif + + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +#endif /* HAVE_UTIMENSAT */ + +static int ntfs_fuse_fsync(const char *path __attribute__((unused)), + int type __attribute__((unused)), + struct fuse_file_info *fi __attribute__((unused))) +{ + int ret; + + /* sync the full device */ + ret = ntfs_device_sync(ctx->vol->dev); + if (ret) + ret = -errno; + return (ret); +} + +#if defined(FUSE_INTERNAL) || (FUSE_VERSION >= 28) +static int ntfs_fuse_ioctl(const char *path, + int cmd, void *arg, + struct fuse_file_info *fi __attribute__((unused)), + unsigned int flags, void *data) +{ + ntfs_inode *ni; + int ret; + + if (flags & FUSE_IOCTL_COMPAT) + return -ENOSYS; + + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + /* + * Linux defines the request argument of ioctl() to be an + * unsigned long, which fuse 2.x forwards as a signed int into + * which the request sometimes does not fit. + * So we must expand the value and make sure it is not sign-extended. + */ + ret = ntfs_ioctl(ni, (unsigned int)cmd, arg, flags, data); + + if (ntfs_inode_close (ni)) + set_fuse_error(&ret); + return ret; +} +#endif /* defined(FUSE_INTERNAL) || (FUSE_VERSION >= 28) */ + +static int ntfs_fuse_bmap(const char *path, size_t blocksize, uint64_t *idx) +{ + ntfs_inode *ni; + ntfs_attr *na; + LCN lcn; + int ret = 0; + int cl_per_bl = ctx->vol->cluster_size / blocksize; + + if (blocksize > ctx->vol->cluster_size) + return -EINVAL; + + if (ntfs_fuse_is_named_data_stream(path)) + return -EINVAL; + + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + + na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0); + if (!na) { + ret = -errno; + goto close_inode; + } + + if ((na->data_flags & (ATTR_COMPRESSION_MASK | ATTR_IS_ENCRYPTED)) + || !NAttrNonResident(na)) { + ret = -EINVAL; + goto close_attr; + } + + if (ntfs_attr_map_whole_runlist(na)) { + ret = -errno; + goto close_attr; + } + + lcn = ntfs_rl_vcn_to_lcn(na->rl, *idx / cl_per_bl); + *idx = (lcn > 0) ? lcn * cl_per_bl + *idx % cl_per_bl : 0; + +close_attr: + ntfs_attr_close(na); +close_inode: + if (ntfs_inode_close(ni)) + set_fuse_error(&ret); + return ret; +} + +#ifdef HAVE_SETXATTR + +/* + * Name space identifications and prefixes + */ + +enum { + XATTRNS_NONE, + XATTRNS_USER, + XATTRNS_SYSTEM, + XATTRNS_SECURITY, + XATTRNS_TRUSTED, + XATTRNS_OPEN +} ; + +/* + * Check whether access to internal data as an extended + * attribute in system name space is allowed + * + * Returns pointer to inode if allowed, + * NULL and errno set if not allowed + */ + +static ntfs_inode *ntfs_check_access_xattr(struct SECURITY_CONTEXT *security, + const char *path, int attr, BOOL setting) +{ + ntfs_inode *ni; + BOOL foracl; + mode_t acctype; + + ni = (ntfs_inode*)NULL; + if (ntfs_fuse_is_named_data_stream(path)) + errno = EINVAL; /* n/a for named data streams. */ + else { + foracl = (attr == XATTR_POSIX_ACC) + || (attr == XATTR_POSIX_DEF); + /* + * When accessing Posix ACL, return unsupported if ACL + * were disabled or no user mapping has been defined, + * or trying to change a Windows-inherited ACL. + * However no error will be returned to getfacl + */ + if (((!ntfs_fuse_fill_security_context(security) + || (ctx->secure_flags + & ((1 << SECURITY_DEFAULT) | (1 << SECURITY_RAW)))) + || !(ctx->secure_flags & (1 << SECURITY_ACL)) + || (setting && ctx->inherit)) + && foracl) { + if (ctx->silent && !ctx->security.mapping[MAPUSERS]) + errno = 0; + else + errno = EOPNOTSUPP; + } else { + /* + * parent directory must be executable, and + * for setting a DOS name it must be writeable + */ + if (setting && (attr == XATTR_NTFS_DOS_NAME)) + acctype = S_IEXEC | S_IWRITE; + else + acctype = S_IEXEC; + if ((attr == XATTR_NTFS_DOS_NAME) + && !strcmp(path,"/")) + /* forbid getting/setting names on root */ + errno = EPERM; + else + if (ntfs_allowed_real_dir_access(security, path, + (ntfs_inode*)NULL ,acctype)) { + ni = ntfs_pathname_to_inode(ctx->vol, + NULL, path); + } + } + } + return (ni); +} + +/* + * Determine the name space of an extended attribute + */ + +static int xattr_namespace(const char *name) +{ + int namespace; + + if (ctx->streams == NF_STREAMS_INTERFACE_XATTR) { + namespace = XATTRNS_NONE; + if (!strncmp(name, nf_ns_user_prefix, + nf_ns_user_prefix_len) + && (strlen(name) != (size_t)nf_ns_user_prefix_len)) + namespace = XATTRNS_USER; + else if (!strncmp(name, nf_ns_system_prefix, + nf_ns_system_prefix_len) + && (strlen(name) != (size_t)nf_ns_system_prefix_len)) + namespace = XATTRNS_SYSTEM; + else if (!strncmp(name, nf_ns_security_prefix, + nf_ns_security_prefix_len) + && (strlen(name) != (size_t)nf_ns_security_prefix_len)) + namespace = XATTRNS_SECURITY; + else if (!strncmp(name, nf_ns_trusted_prefix, + nf_ns_trusted_prefix_len) + && (strlen(name) != (size_t)nf_ns_trusted_prefix_len)) + namespace = XATTRNS_TRUSTED; + } else + namespace = XATTRNS_OPEN; + return (namespace); +} + +/* + * Fix the prefix of an extended attribute + */ + +static int fix_xattr_prefix(const char *name, int namespace, ntfschar **lename) +{ + int len; + char *prefixed; + + *lename = (ntfschar*)NULL; + switch (namespace) { + case XATTRNS_USER : + /* + * user name space : remove user prefix + */ + len = ntfs_mbstoucs(name + nf_ns_user_prefix_len, lename); + break; + case XATTRNS_SYSTEM : + case XATTRNS_SECURITY : + case XATTRNS_TRUSTED : + /* + * security, trusted and unmapped system name spaces : + * insert ntfs-3g prefix + */ + prefixed = ntfs_malloc(strlen(xattr_ntfs_3g) + + strlen(name) + 1); + if (prefixed) { + strcpy(prefixed,xattr_ntfs_3g); + strcat(prefixed,name); + len = ntfs_mbstoucs(prefixed, lename); + free(prefixed); + } else + len = -1; + break; + case XATTRNS_OPEN : + /* + * in open name space mode : do no fix prefix + */ + len = ntfs_mbstoucs(name, lename); + break; + default : + len = -1; + } + return (len); +} + +static int ntfs_fuse_listxattr(const char *path, char *list, size_t size) +{ + ntfs_attr_search_ctx *actx = NULL; + ntfs_inode *ni; + int ret = 0; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct SECURITY_CONTEXT security; +#endif +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* parent directory must be executable */ + if (ntfs_fuse_fill_security_context(&security) + && !ntfs_allowed_dir_access(&security,path,(ntfs_inode*)NULL, + (ntfs_inode*)NULL,S_IEXEC)) { + return (-errno); + } +#endif + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + /* Return with no result for symlinks, fifo, etc. */ + if (!user_xattrs_allowed(ctx, ni)) + goto exit; + /* otherwise file must be readable */ +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (!ntfs_allowed_access(&security,ni,S_IREAD)) { + ret = -EACCES; + goto exit; + } +#endif + actx = ntfs_attr_get_search_ctx(ni, NULL); + if (!actx) { + ret = -errno; + goto exit; + } + + if ((ctx->streams == NF_STREAMS_INTERFACE_XATTR) + || (ctx->streams == NF_STREAMS_INTERFACE_OPENXATTR)) { + ret = ntfs_fuse_listxattr_common(ni, actx, list, size, + ctx->streams == NF_STREAMS_INTERFACE_XATTR); + if (ret < 0) + goto exit; + } + if (errno != ENOENT) + ret = -errno; +exit: + if (actx) + ntfs_attr_put_search_ctx(actx); + if (ntfs_inode_close(ni)) + set_fuse_error(&ret); + return ret; +} + +static int ntfs_fuse_getxattr_windows(const char *path, const char *name, + char *value, size_t size) +{ + ntfs_attr_search_ctx *actx = NULL; + ntfs_inode *ni; + char *to = value; + int ret = 0; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + struct SECURITY_CONTEXT security; +#endif + + if (strcmp(name, "ntfs.streams.list")) + return -EOPNOTSUPP; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* parent directory must be executable */ + if (ntfs_fuse_fill_security_context(&security) + && !ntfs_allowed_dir_access(&security,path,(ntfs_inode*)NULL, + (ntfs_inode*)NULL,S_IEXEC)) { + return (-errno); + } +#endif + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (!ntfs_allowed_access(&security,ni,S_IREAD)) { + ret = -errno; + goto exit; + } +#endif + actx = ntfs_attr_get_search_ctx(ni, NULL); + if (!actx) { + ret = -errno; + goto exit; + } + while (!ntfs_attr_lookup(AT_DATA, NULL, 0, CASE_SENSITIVE, + 0, NULL, 0, actx)) { + char *tmp_name = NULL; + int tmp_name_len; + + if (!actx->attr->name_length) + continue; + tmp_name_len = ntfs_ucstombs((ntfschar *)((u8*)actx->attr + + le16_to_cpu(actx->attr->name_offset)), + actx->attr->name_length, &tmp_name, 0); + if (tmp_name_len < 0) { + ret = -errno; + goto exit; + } + if (ret) + ret++; /* For space delimiter. */ + ret += tmp_name_len; + if (size) { + if ((size_t)ret <= size) { + /* Don't add space to the beginning of line. */ + if (to != value) { + *to = '\0'; + to++; + } + strncpy(to, tmp_name, tmp_name_len); + to += tmp_name_len; + } else { + free(tmp_name); + ret = -ERANGE; + goto exit; + } + } + free(tmp_name); + } + if (errno != ENOENT) + ret = -errno; +exit: + if (actx) + ntfs_attr_put_search_ctx(actx); + if (ntfs_inode_close(ni)) + set_fuse_error(&ret); + return ret; +} + +#if defined(__APPLE__) || defined(__DARWIN__) +static int ntfs_fuse_getxattr(const char *path, const char *name, + char *value, size_t size, uint32_t position) +#else +static int ntfs_fuse_getxattr(const char *path, const char *name, + char *value, size_t size) +#endif +{ +#if !(defined(__APPLE__) || defined(__DARWIN__)) + static const unsigned int position = 0U; +#endif + + ntfs_inode *ni; + ntfs_inode *dir_ni; + ntfs_attr *na = NULL; + ntfschar *lename = NULL; + int res, lename_len; + s64 rsize; + enum SYSTEMXATTRS attr; + int namespace; + struct SECURITY_CONTEXT security; + +#if defined(__APPLE__) || defined(__DARWIN__) + /* If the attribute is not a resource fork attribute and the position + * parameter is non-zero, we return with EINVAL as requesting position + * is not permitted for non-resource fork attributes. */ + if (position && strcmp(name, XATTR_RESOURCEFORK_NAME)) { + return -EINVAL; + } +#endif + + attr = ntfs_xattr_system_type(name,ctx->vol); + if (attr != XATTR_UNMAPPED) { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * hijack internal data and ACL retrieval, whatever + * mode was selected for xattr (from the user's + * point of view, ACLs are not xattr) + */ + ni = ntfs_check_access_xattr(&security, path, attr, FALSE); + if (ni) { + if (ntfs_allowed_access(&security,ni,S_IREAD)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = get_parent_dir(path); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_getxattr(&security, + attr, ni, dir_ni, value, size); + if (dir_ni && ntfs_inode_close(dir_ni)) + set_fuse_error(&res); + } else { + res = -errno; + } + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } else + res = -errno; +#else + /* + * Only hijack NTFS ACL retrieval if POSIX ACLS + * option is not selected + * Access control is done by fuse + */ + if (ntfs_fuse_is_named_data_stream(path)) + res = -EINVAL; /* n/a for named data streams. */ + else { + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (ni) { + /* user mapping not mandatory */ + ntfs_fuse_fill_security_context(&security); + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = get_parent_dir(path); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_getxattr(&security, + attr, ni, dir_ni, value, size); + if (dir_ni && ntfs_inode_close(dir_ni)) + set_fuse_error(&res); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } else + res = -errno; + } +#endif + return (res); + } + if (ctx->streams == NF_STREAMS_INTERFACE_WINDOWS) + return ntfs_fuse_getxattr_windows(path, name, value, size); + if (ctx->streams == NF_STREAMS_INTERFACE_NONE) + return -EOPNOTSUPP; + namespace = xattr_namespace(name); + if (namespace == XATTRNS_NONE) + return -EOPNOTSUPP; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* parent directory must be executable */ + if (ntfs_fuse_fill_security_context(&security) + && !ntfs_allowed_dir_access(&security,path,(ntfs_inode*)NULL, + (ntfs_inode*)NULL,S_IEXEC)) { + return (-errno); + } + /* trusted only readable by root */ + if ((namespace == XATTRNS_TRUSTED) + && security.uid) + return -NTFS_NOXATTR_ERRNO; +#endif + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; + /* Return with no result for symlinks, fifo, etc. */ + if (!user_xattrs_allowed(ctx, ni)) { + res = -NTFS_NOXATTR_ERRNO; + goto exit; + } + /* otherwise file must be readable */ +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + if (!ntfs_allowed_access(&security, ni, S_IREAD)) { + res = -errno; + goto exit; + } +#endif + lename_len = fix_xattr_prefix(name, namespace, &lename); + if (lename_len == -1) { + res = -errno; + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, lename, lename_len); + if (!na) { + res = -NTFS_NOXATTR_ERRNO; + goto exit; + } + rsize = na->data_size; + if (ctx->efs_raw + && rsize + && (na->data_flags & ATTR_IS_ENCRYPTED) + && NAttrNonResident(na)) + rsize = ((na->data_size + 511) & ~511) + 2; + rsize -= position; + if (size) { + if (size >= (size_t)rsize) { + res = ntfs_attr_pread(na, position, rsize, value); + if (res != rsize) + res = -errno; + } else + res = -ERANGE; + } else + res = rsize; +exit: + if (na) + ntfs_attr_close(na); + free(lename); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +#if defined(__APPLE__) || defined(__DARWIN__) +static int ntfs_fuse_setxattr(const char *path, const char *name, + const char *value, size_t size, int flags, + uint32_t position) +#else +static int ntfs_fuse_setxattr(const char *path, const char *name, + const char *value, size_t size, int flags) +#endif +{ +#if !(defined(__APPLE__) || defined(__DARWIN__)) + static const unsigned int position = 0U; +#else + BOOL is_resource_fork; +#endif + + ntfs_inode *ni; + ntfs_inode *dir_ni; + ntfs_attr *na = NULL; + ntfschar *lename = NULL; + int res, lename_len; + size_t total; + s64 part; + enum SYSTEMXATTRS attr; + int namespace; + struct SECURITY_CONTEXT security; + +#if defined(__APPLE__) || defined(__DARWIN__) + /* If the attribute is not a resource fork attribute and the position + * parameter is non-zero, we return with EINVAL as requesting position + * is not permitted for non-resource fork attributes. */ + is_resource_fork = strcmp(name, XATTR_RESOURCEFORK_NAME) ? FALSE : TRUE; + if (position && !is_resource_fork) { + return -EINVAL; + } +#endif + + attr = ntfs_xattr_system_type(name,ctx->vol); + if (attr != XATTR_UNMAPPED) { +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * hijack internal data and ACL setting, whatever + * mode was selected for xattr (from the user's + * point of view, ACLs are not xattr) + * Note : ctime updated on successful settings + */ + ni = ntfs_check_access_xattr(&security,path,attr,TRUE); + if (ni) { + if (ntfs_allowed_as_owner(&security,ni)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = get_parent_dir(path); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_setxattr(&security, + attr, ni, dir_ni, value, size, flags); + /* never have to close dir_ni */ + if (res) + res = -errno; + } else + res = -errno; + if (attr != XATTR_NTFS_DOS_NAME) { + if (!res) + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } else + res = -errno; +#else + /* + * Only hijack NTFS ACL setting if POSIX ACLS + * option is not selected + * Access control is partially done by fuse + */ + if (ntfs_fuse_is_named_data_stream(path)) + res = -EINVAL; /* n/a for named data streams. */ + else { + /* creation of a new name is not controlled by fuse */ + if (attr == XATTR_NTFS_DOS_NAME) + ni = ntfs_check_access_xattr(&security,path,attr,TRUE); + else + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (ni) { + /* + * user mapping is not mandatory + * if defined, only owner is allowed + */ + if (!ntfs_fuse_fill_security_context(&security) + || ntfs_allowed_as_owner(&security,ni)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = get_parent_dir(path); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_setxattr(&security, + attr, ni, dir_ni, value, + size, flags); + /* never have to close dir_ni */ + if (res) + res = -errno; + } else + res = -errno; + if (attr != XATTR_NTFS_DOS_NAME) { + if (!res) + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } else + res = -errno; + } +#endif + return (res); + } + if ((ctx->streams != NF_STREAMS_INTERFACE_XATTR) + && (ctx->streams != NF_STREAMS_INTERFACE_OPENXATTR)) + return -EOPNOTSUPP; + namespace = xattr_namespace(name); + if (namespace == XATTRNS_NONE) + return -EOPNOTSUPP; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* parent directory must be executable */ + if (ntfs_fuse_fill_security_context(&security) + && !ntfs_allowed_dir_access(&security,path,(ntfs_inode*)NULL, + (ntfs_inode*)NULL,S_IEXEC)) { + return (-errno); + } + /* security and trusted only settable by root */ + if (((namespace == XATTRNS_SECURITY) + || (namespace == XATTRNS_TRUSTED)) + && security.uid) + return -EPERM; +#endif + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + switch (namespace) { + case XATTRNS_SECURITY : + case XATTRNS_TRUSTED : + if (security.uid) { + res = -EPERM; + goto exit; + } + break; + case XATTRNS_SYSTEM : + if (!ntfs_allowed_as_owner(&security,ni)) { + res = -EACCES; + goto exit; + } + break; + default : + /* User xattr not allowed for symlinks, fifo, etc. */ + if (!user_xattrs_allowed(ctx, ni)) { + res = -EPERM; + goto exit; + } + if (!ntfs_allowed_access(&security,ni,S_IWRITE)) { + res = -EACCES; + goto exit; + } + break; + } +#else + /* User xattr not allowed for symlinks, fifo, etc. */ + if ((namespace == XATTRNS_USER) + && !user_xattrs_allowed(ctx, ni)) { + res = -EPERM; + goto exit; + } +#endif + lename_len = fix_xattr_prefix(name, namespace, &lename); + if ((lename_len == -1) + || (ctx->windows_names + && ntfs_forbidden_chars(lename,lename_len,TRUE))) { + res = -errno; + goto exit; + } + na = ntfs_attr_open(ni, AT_DATA, lename, lename_len); + if (na && flags == XATTR_CREATE) { + res = -EEXIST; + goto exit; + } + if (!na) { + if (flags == XATTR_REPLACE) { + res = -NTFS_NOXATTR_ERRNO; + goto exit; + } + if (ntfs_attr_add(ni, AT_DATA, lename, lename_len, NULL, 0)) { + res = -errno; + goto exit; + } + if (!(ni->flags & FILE_ATTR_ARCHIVE)) { + set_archive(ni); + NInoFileNameSetDirty(ni); + } + na = ntfs_attr_open(ni, AT_DATA, lename, lename_len); + if (!na) { + res = -errno; + goto exit; + } +#if defined(__APPLE__) || defined(__DARWIN__) + } else if (is_resource_fork) { + /* In macOS, the resource fork is a special case. It doesn't + * ever shrink (it would have to be removed and re-added). */ +#endif + } else { + /* currently compressed streams can only be wiped out */ + if (ntfs_attr_truncate(na, (s64)0 /* size */)) { + res = -errno; + goto exit; + } + } + total = 0; + res = 0; + if (size) { + do { + part = ntfs_attr_pwrite(na, position + total, + size - total, &value[total]); + if (part > 0) + total += part; + } while ((part > 0) && (total < size)); + } + if ((total != size) || ntfs_attr_pclose(na)) + res = -errno; + else { + if (ctx->efs_raw + && (ni->flags & FILE_ATTR_ENCRYPTED)) { + if (ntfs_efs_fixup_attribute(NULL,na)) + res = -errno; + } + } + if (!res) { + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + if (!(ni->flags & FILE_ATTR_ARCHIVE)) { + set_archive(ni); + NInoFileNameSetDirty(ni); + } + } +exit: + if (na) + ntfs_attr_close(na); + free(lename); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +static int ntfs_fuse_removexattr(const char *path, const char *name) +{ + ntfs_inode *ni; + ntfs_inode *dir_ni; + ntfschar *lename = NULL; + int res = 0, lename_len; + enum SYSTEMXATTRS attr; + int namespace; + struct SECURITY_CONTEXT security; + + attr = ntfs_xattr_system_type(name,ctx->vol); + if (attr != XATTR_UNMAPPED) { + switch (attr) { + /* + * Removal of NTFS ACL, ATTRIB, EFSINFO or TIMES + * is never allowed + */ + case XATTR_NTFS_ACL : + case XATTR_NTFS_ATTRIB : + case XATTR_NTFS_ATTRIB_BE : + case XATTR_NTFS_EFSINFO : + case XATTR_NTFS_TIMES : + case XATTR_NTFS_TIMES_BE : + case XATTR_NTFS_CRTIME : + case XATTR_NTFS_CRTIME_BE : + res = -EPERM; + break; + default : +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* + * hijack internal data and ACL removal, whatever + * mode was selected for xattr (from the user's + * point of view, ACLs are not xattr) + * Note : ctime updated on successful settings + */ + ni = ntfs_check_access_xattr(&security,path,attr,TRUE); + if (ni) { + if (ntfs_allowed_as_owner(&security,ni)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = get_parent_dir(path); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_removexattr(&security, + attr, ni, dir_ni); + /* never have to close dir_ni */ + if (res) + res = -errno; + } else + res = -errno; + if (attr != XATTR_NTFS_DOS_NAME) { + if (!res) + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } else + res = -errno; +#else + /* + * Only hijack NTFS ACL setting if POSIX ACLS + * option is not selected + * Access control is partially done by fuse + */ + /* creation of a new name is not controlled by fuse */ + if (attr == XATTR_NTFS_DOS_NAME) + ni = ntfs_check_access_xattr(&security, + path, attr, TRUE); + else { + if (ntfs_fuse_is_named_data_stream(path)) { + ni = (ntfs_inode*)NULL; + errno = EINVAL; /* n/a for named data streams. */ + } else + ni = ntfs_pathname_to_inode(ctx->vol, + NULL, path); + } + if (ni) { + /* + * user mapping is not mandatory + * if defined, only owner is allowed + */ + if (!ntfs_fuse_fill_security_context(&security) + || ntfs_allowed_as_owner(&security,ni)) { + if (attr == XATTR_NTFS_DOS_NAME) + dir_ni = get_parent_dir(path); + else + dir_ni = (ntfs_inode*)NULL; + res = ntfs_xattr_system_removexattr(&security, + attr, ni, dir_ni); + /* never have to close dir_ni */ + if (res) + res = -errno; + } else + res = -errno; + if (attr != XATTR_NTFS_DOS_NAME) { + if (!res) + ntfs_fuse_update_times(ni, + NTFS_UPDATE_CTIME); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + } + } else + res = -errno; +#endif + break; + } + return (res); + } + if ((ctx->streams != NF_STREAMS_INTERFACE_XATTR) + && (ctx->streams != NF_STREAMS_INTERFACE_OPENXATTR)) + return -EOPNOTSUPP; + namespace = xattr_namespace(name); + if (namespace == XATTRNS_NONE) + return -EOPNOTSUPP; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + /* parent directory must be executable */ + if (ntfs_fuse_fill_security_context(&security) + && !ntfs_allowed_dir_access(&security,path,(ntfs_inode*)NULL, + (ntfs_inode*)NULL,S_IEXEC)) { + return (-errno); + } + /* security and trusted only settable by root */ + if (((namespace == XATTRNS_SECURITY) + || (namespace == XATTRNS_TRUSTED)) + && security.uid) + return -EACCES; +#endif + ni = ntfs_pathname_to_inode(ctx->vol, NULL, path); + if (!ni) + return -errno; +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + switch (namespace) { + case XATTRNS_SECURITY : + case XATTRNS_TRUSTED : + if (security.uid) { + res = -EPERM; + goto exit; + } + break; + case XATTRNS_SYSTEM : + if (!ntfs_allowed_as_owner(&security,ni)) { + res = -EACCES; + goto exit; + } + break; + default : + /* User xattr not allowed for symlinks, fifo, etc. */ + if (!user_xattrs_allowed(ctx, ni)) { + res = -EPERM; + goto exit; + } + if (!ntfs_allowed_access(&security,ni,S_IWRITE)) { + res = -EACCES; + goto exit; + } + break; + } +#else + /* User xattr not allowed for symlinks, fifo, etc. */ + if ((namespace == XATTRNS_USER) + && !user_xattrs_allowed(ctx, ni)) { + res = -EPERM; + goto exit; + } +#endif + lename_len = fix_xattr_prefix(name, namespace, &lename); + if (lename_len == -1) { + res = -errno; + goto exit; + } + if (ntfs_attr_remove(ni, AT_DATA, lename, lename_len)) { + if (errno == ENOENT) + errno = NTFS_NOXATTR_ERRNO; + res = -errno; + } + if (!res) { + ntfs_fuse_update_times(ni, NTFS_UPDATE_CTIME); + if (!(ni->flags & FILE_ATTR_ARCHIVE)) { + set_archive(ni); + NInoFileNameSetDirty(ni); + } + } +exit: + free(lename); + if (ntfs_inode_close(ni)) + set_fuse_error(&res); + return res; +} + +#else +#if POSIXACLS +#error "Option inconsistency : POSIXACLS requires SETXATTR" +#endif +#endif /* HAVE_SETXATTR */ + +#ifndef DISABLE_PLUGINS +static void register_internal_reparse_plugins(void) +{ + static const plugin_operations_t ops = { + .getattr = junction_getattr, + .readlink = junction_readlink, + } ; + static const plugin_operations_t wsl_ops = { + .getattr = wsl_getattr, + } ; + + register_reparse_plugin(ctx, IO_REPARSE_TAG_MOUNT_POINT, + &ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_SYMLINK, + &ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_LX_SYMLINK, + &ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_LX_SYMLINK, + &ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_AF_UNIX, + &wsl_ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_LX_FIFO, + &wsl_ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_LX_CHR, + &wsl_ops, (void*)NULL); + register_reparse_plugin(ctx, IO_REPARSE_TAG_LX_BLK, + &wsl_ops, (void*)NULL); +} +#endif /* DISABLE_PLUGINS */ + +static void ntfs_close(void) +{ + struct SECURITY_CONTEXT security; + + if (!ctx) + return; + + if (!ctx->vol) + return; + + if (ctx->mounted) { + ntfs_log_info("Unmounting %s (%s)\n", opts.device, + ctx->vol->vol_name); + if (ntfs_fuse_fill_security_context(&security)) { + if (ctx->seccache && ctx->seccache->head.p_reads) { + ntfs_log_info("Permissions cache : %lu writes, " + "%lu reads, %lu.%1lu%% hits\n", + ctx->seccache->head.p_writes, + ctx->seccache->head.p_reads, + 100 * ctx->seccache->head.p_hits + / ctx->seccache->head.p_reads, + 1000 * ctx->seccache->head.p_hits + / ctx->seccache->head.p_reads % 10); + } + } + ntfs_destroy_security_context(&security); + } + + if (ntfs_umount(ctx->vol, FALSE)) + ntfs_log_perror("Failed to close volume %s", opts.device); + + ctx->vol = NULL; +} + +static void ntfs_fuse_destroy2(void *unused __attribute__((unused))) +{ + ntfs_close(); +} + +static struct fuse_operations ntfs_3g_ops = { + .getattr = ntfs_fuse_getattr, + .readlink = ntfs_fuse_readlink, + .readdir = ntfs_fuse_readdir, + .open = ntfs_fuse_open, + .release = ntfs_fuse_release, + .read = ntfs_fuse_read, + .write = ntfs_fuse_write, + .truncate = ntfs_fuse_truncate, + .ftruncate = ntfs_fuse_ftruncate, + .statfs = ntfs_fuse_statfs, + .chmod = ntfs_fuse_chmod, + .chown = ntfs_fuse_chown, + .create = ntfs_fuse_create_file, + .mknod = ntfs_fuse_mknod, + .symlink = ntfs_fuse_symlink, + .link = ntfs_fuse_link, + .unlink = ntfs_fuse_unlink, + .rename = ntfs_fuse_rename, + .mkdir = ntfs_fuse_mkdir, + .rmdir = ntfs_fuse_rmdir, +#ifdef HAVE_UTIMENSAT + .utimens = ntfs_fuse_utimens, +#if defined(linux) & !defined(FUSE_INTERNAL) & (FUSE_VERSION < 30) + .flag_utime_omit_ok = 1, +#endif /* defined(linux) & !defined(FUSE_INTERNAL) */ +#else + .utime = ntfs_fuse_utime, +#endif + .fsync = ntfs_fuse_fsync, + .fsyncdir = ntfs_fuse_fsync, + .bmap = ntfs_fuse_bmap, + .destroy = ntfs_fuse_destroy2, +#if defined(FUSE_INTERNAL) || (FUSE_VERSION >= 28) + .ioctl = ntfs_fuse_ioctl, +#endif /* defined(FUSE_INTERNAL) || (FUSE_VERSION >= 28) */ +#if !KERNELPERMS | (POSIXACLS & !KERNELACLS) + .access = ntfs_fuse_access, + .opendir = ntfs_fuse_opendir, + .releasedir = ntfs_fuse_release, +#endif +#ifdef HAVE_SETXATTR + .getxattr = ntfs_fuse_getxattr, + .setxattr = ntfs_fuse_setxattr, + .removexattr = ntfs_fuse_removexattr, + .listxattr = ntfs_fuse_listxattr, +#endif /* HAVE_SETXATTR */ +#if defined(__APPLE__) || defined(__DARWIN__) + /* MacFUSE extensions. */ + .getxtimes = ntfs_macfuse_getxtimes, + .setcrtime = ntfs_macfuse_setcrtime, + .setbkuptime = ntfs_macfuse_setbkuptime, + .setchgtime = ntfs_macfuse_setchgtime, +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + .init = ntfs_init +}; + +static int ntfs_fuse_init(void) +{ + ctx = ntfs_calloc(sizeof(ntfs_fuse_context_t)); + if (!ctx) + return -1; + + *ctx = (ntfs_fuse_context_t) { + .uid = getuid(), + .gid = getgid(), +#if defined(linux) + .streams = NF_STREAMS_INTERFACE_XATTR, +#else + .streams = NF_STREAMS_INTERFACE_NONE, +#endif + .atime = ATIME_RELATIVE, + .silent = TRUE, + .recover = TRUE + }; + return 0; +} + +static int ntfs_open(const char *device) +{ + unsigned long flags = 0; + + if (!ctx->blkdev) + flags |= NTFS_MNT_EXCLUSIVE; + if (ctx->ro) + flags |= NTFS_MNT_RDONLY; + else + if (!ctx->hiberfile) + flags |= NTFS_MNT_MAY_RDONLY; + if (ctx->recover) + flags |= NTFS_MNT_RECOVER; + if (ctx->hiberfile) + flags |= NTFS_MNT_IGNORE_HIBERFILE; + + ctx->vol = ntfs_mount(device, flags); + if (!ctx->vol) { + ntfs_log_perror("Failed to mount '%s'", device); + goto err_out; + } + if (ctx->sync && ctx->vol->dev) + NDevSetSync(ctx->vol->dev); + if (ctx->compression) + NVolSetCompression(ctx->vol); + else + NVolClearCompression(ctx->vol); +#ifdef HAVE_SETXATTR + /* archivers must see hidden files */ + if (ctx->efs_raw) + ctx->hide_hid_files = FALSE; +#endif + if (ntfs_set_shown_files(ctx->vol, ctx->show_sys_files, + !ctx->hide_hid_files, ctx->hide_dot_files)) + goto err_out; + + if (ntfs_volume_get_free_space(ctx->vol)) { + ntfs_log_perror("Failed to read NTFS $Bitmap"); + goto err_out; + } + + ctx->vol->free_mft_records = ntfs_get_nr_free_mft_records(ctx->vol); + if (ctx->vol->free_mft_records < 0) { + ntfs_log_perror("Failed to calculate free MFT records"); + goto err_out; + } + + if (ctx->hiberfile && ntfs_volume_check_hiberfile(ctx->vol, 0)) { + if (errno != EPERM) + goto err_out; + if (ntfs_fuse_rm("/hiberfil.sys")) + goto err_out; + } + + errno = 0; + goto out; +err_out: + if (!errno) + errno = EIO; +out : + return ntfs_volume_error(errno); +} + +static void usage(void) +{ + ntfs_log_info(usage_msg, EXEC_NAME, VERSION, FUSE_TYPE, fuse_version(), + 4 + POSIXACLS*6 - KERNELPERMS*3 + CACHEING, + EXEC_NAME, ntfs_home); +} + +#if defined(linux) || defined(__uClinux__) + +static const char *dev_fuse_msg = +"HINT: You should be root, or make ntfs-3g setuid root, or load the FUSE\n" +" kernel module as root ('modprobe fuse' or 'insmod /fuse.ko'" +" or insmod /fuse.o'). Make also sure that the fuse device" +" exists. It's usually either /dev/fuse or /dev/misc/fuse."; + +static const char *fuse26_kmod_msg = +"WARNING: Deficient Linux kernel detected. Some driver features are\n" +" not available (swap file on NTFS, boot from NTFS by LILO), and\n" +" unmount is not safe unless it's made sure the ntfs-3g process\n" +" naturally terminates after calling 'umount'. If you wish this\n" +" message to disappear then you should upgrade to at least kernel\n" +" version 2.6.20, or request help from your distribution to fix\n" +" the kernel problem. The below web page has more information:\n" +" https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ\n" +"\n"; + +static void mknod_dev_fuse(const char *dev) +{ + struct stat st; + + if (stat(dev, &st) && (errno == ENOENT)) { + mode_t mask = umask(0); + if (mknod(dev, S_IFCHR | 0666, makedev(10, 229))) { + ntfs_log_perror("Failed to create '%s'", dev); + if (errno == EPERM) + ntfs_log_error("%s", dev_fuse_msg); + } + umask(mask); + } +} + +static void create_dev_fuse(void) +{ + mknod_dev_fuse("/dev/fuse"); + +#ifdef __UCLIBC__ + { + struct stat st; + /* The fuse device is under /dev/misc using devfs. */ + if (stat("/dev/misc", &st) && (errno == ENOENT)) { + mode_t mask = umask(0); + mkdir("/dev/misc", 0775); + umask(mask); + } + mknod_dev_fuse("/dev/misc/fuse"); + } +#endif +} + +static fuse_fstype get_fuse_fstype(void) +{ + char buf[256]; + fuse_fstype fstype = FSTYPE_NONE; + + FILE *f = fopen("/proc/filesystems", "r"); + if (!f) { + ntfs_log_perror("Failed to open /proc/filesystems"); + return FSTYPE_UNKNOWN; + } + + while (fgets(buf, sizeof(buf), f)) { + if (strstr(buf, "fuseblk\n")) { + fstype = FSTYPE_FUSEBLK; + break; + } + if (strstr(buf, "fuse\n")) + fstype = FSTYPE_FUSE; + } + + fclose(f); + return fstype; +} + +static fuse_fstype load_fuse_module(void) +{ + int i; + struct stat st; + pid_t pid; + const char *cmd = "/sbin/modprobe"; + char *env = (char*)NULL; + struct timespec req = { 0, 100000000 }; /* 100 msec */ + fuse_fstype fstype; + + if (!stat(cmd, &st) && !geteuid()) { + pid = fork(); + if (!pid) { + execle(cmd, cmd, "fuse", (char*)NULL, &env); + _exit(1); + } else if (pid != -1) + waitpid(pid, NULL, 0); + } + + for (i = 0; i < 10; i++) { + /* + * We sleep first because despite the detection of the loaded + * FUSE kernel module, fuse_mount() can still fail if it's not + * fully functional/initialized. Note, of course this is still + * unreliable but usually helps. + */ + nanosleep(&req, NULL); + fstype = get_fuse_fstype(); + if (fstype != FSTYPE_NONE) + break; + } + return fstype; +} + +#endif + +static struct fuse_chan *try_fuse_mount(char *parsed_options) +{ + struct fuse_chan *fc = NULL; + struct fuse_args margs = FUSE_ARGS_INIT(0, NULL); + + /* The fuse_mount() options get modified, so we always rebuild it */ + if ((fuse_opt_add_arg(&margs, EXEC_NAME) == -1 || + fuse_opt_add_arg(&margs, "-o") == -1 || + fuse_opt_add_arg(&margs, parsed_options) == -1)) { + ntfs_log_error("Failed to set FUSE options.\n"); + goto free_args; + } + + fc = fuse_mount(opts.mnt_point, &margs); +free_args: + fuse_opt_free_args(&margs); + return fc; + +} + +static int set_fuseblk_options(char **parsed_options) +{ + char options[64]; + long pagesize; + u32 blksize = ctx->vol->cluster_size; + + pagesize = sysconf(_SC_PAGESIZE); + if (pagesize < 1) + pagesize = 4096; + + if (blksize > (u32)pagesize) + blksize = pagesize; + + snprintf(options, sizeof(options), ",blkdev,blksize=%u", blksize); + if (ntfs_strappend(parsed_options, options)) + return -1; + return 0; +} + +static struct fuse *mount_fuse(char *parsed_options) +{ + struct fuse *fh = NULL; + struct fuse_args args = FUSE_ARGS_INIT(0, NULL); + + ctx->fc = try_fuse_mount(parsed_options); + if (!ctx->fc) + return NULL; + + if (fuse_opt_add_arg(&args, "") == -1) + goto err; + if (ctx->ro) { + char buf[128]; + int len; + + len = snprintf(buf, sizeof(buf), "-ouse_ino,kernel_cache" + ",attr_timeout=%d,entry_timeout=%d", + (int)TIMEOUT_RO, (int)TIMEOUT_RO); + if ((len < 0) + || (len >= (int)sizeof(buf)) + || (fuse_opt_add_arg(&args, buf) == -1)) + goto err; + } else { +#if !CACHEING + if (fuse_opt_add_arg(&args, "-ouse_ino,kernel_cache" + ",attr_timeout=0") == -1) + goto err; +#else + if (fuse_opt_add_arg(&args, "-ouse_ino,kernel_cache" + ",attr_timeout=1") == -1) + goto err; +#endif + } + if (ctx->debug) + if (fuse_opt_add_arg(&args, "-odebug") == -1) + goto err; + + fh = fuse_new(ctx->fc, &args , &ntfs_3g_ops, sizeof(ntfs_3g_ops), NULL); + if (!fh) + goto err; + + if (fuse_set_signal_handlers(fuse_get_session(fh))) + goto err_destory; +out: + fuse_opt_free_args(&args); + return fh; +err_destory: + fuse_destroy(fh); + fh = NULL; +err: + fuse_unmount(opts.mnt_point, ctx->fc); + goto out; +} + +static void setup_logging(char *parsed_options) +{ + if (!ctx->no_detach) { + if (daemon(0, ctx->debug)) + ntfs_log_error("Failed to daemonize.\n"); + else if (!ctx->debug) { +#ifndef DEBUG + ntfs_log_set_handler(ntfs_log_handler_syslog); + /* Override default libntfs identify. */ + openlog(EXEC_NAME, LOG_PID, LOG_DAEMON); +#endif + } + } + + ctx->seccache = (struct PERMISSIONS_CACHE*)NULL; + + ntfs_log_info("Version %s %s %d\n", VERSION, FUSE_TYPE, fuse_version()); + if (strcmp(opts.arg_device,opts.device)) + ntfs_log_info("Requested device %s canonicalized as %s\n", + opts.arg_device,opts.device); + ntfs_log_info("Mounted %s (%s, label \"%s\", NTFS %d.%d)\n", + opts.device, (ctx->ro) ? "Read-Only" : "Read-Write", + ctx->vol->vol_name, ctx->vol->major_ver, + ctx->vol->minor_ver); + ntfs_log_info("Cmdline options: %s\n", opts.options ? opts.options : ""); + ntfs_log_info("Mount options: %s\n", parsed_options); +} + +int main(int argc, char *argv[]) +{ + char *parsed_options = NULL; + struct fuse *fh; +#if !(defined(__sun) && defined (__SVR4)) + fuse_fstype fstype = FSTYPE_UNKNOWN; +#endif + const char *permissions_mode = (const char*)NULL; + const char *failed_secure = (const char*)NULL; +#if defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) + struct XATTRMAPPING *xattr_mapping = (struct XATTRMAPPING*)NULL; +#endif /* defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) */ + struct stat sbuf; + unsigned long existing_mount; + int err, fd; + + /* + * Make sure file descriptors 0, 1 and 2 are open, + * otherwise chaos would ensue. + */ + do { + fd = open("/dev/null", O_RDWR); + if (fd > 2) + close(fd); + } while (fd >= 0 && fd <= 2); + +#ifndef FUSE_INTERNAL + if ((getuid() != geteuid()) || (getgid() != getegid())) { + fprintf(stderr, "%s", setuid_msg); + return NTFS_VOLUME_INSECURE; + } +#endif + if (drop_privs()) + return NTFS_VOLUME_NO_PRIVILEGE; + + ntfs_set_locale(); + ntfs_log_set_handler(ntfs_log_handler_stderr); + + if (ntfs_parse_options(&opts, usage, argc, argv)) { + usage(); + return NTFS_VOLUME_SYNTAX_ERROR; + } + + if (ntfs_fuse_init()) { + err = NTFS_VOLUME_OUT_OF_MEMORY; + goto err2; + } + + parsed_options = parse_mount_options(ctx, &opts, FALSE); + if (!parsed_options) { + err = NTFS_VOLUME_SYNTAX_ERROR; + goto err_out; + } + if (!ntfs_check_if_mounted(opts.device,&existing_mount) + && (existing_mount & NTFS_MF_MOUNTED) + /* accept multiple read-only mounts */ + && (!(existing_mount & NTFS_MF_READONLY) || !ctx->ro)) { + err = NTFS_VOLUME_LOCKED; + goto err_out; + } + + /* need absolute mount point for junctions */ + if (opts.mnt_point[0] == '/') + ctx->abs_mnt_point = strdup(opts.mnt_point); + else { + ctx->abs_mnt_point = (char*)ntfs_malloc(PATH_MAX); + if (ctx->abs_mnt_point) { + if ((strlen(opts.mnt_point) < PATH_MAX) + && getcwd(ctx->abs_mnt_point, + PATH_MAX - strlen(opts.mnt_point) - 1)) { + strcat(ctx->abs_mnt_point, "/"); + strcat(ctx->abs_mnt_point, opts.mnt_point); +#if defined(__sun) && defined (__SVR4) + /* Solaris also wants the absolute mount point */ + opts.mnt_point = ctx->abs_mnt_point; +#endif /* defined(__sun) && defined (__SVR4) */ + } else { + free(ctx->abs_mnt_point); + ctx->abs_mnt_point = (char*)NULL; + } + } + } + if (!ctx->abs_mnt_point) { + err = NTFS_VOLUME_OUT_OF_MEMORY; + goto err_out; + } + + ctx->security.uid = 0; + ctx->security.gid = 0; + if ((opts.mnt_point[0] == '/') + && !stat(opts.mnt_point,&sbuf)) { + /* collect owner of mount point, useful for default mapping */ + ctx->security.uid = sbuf.st_uid; + ctx->security.gid = sbuf.st_gid; + } + +#if defined(linux) || defined(__uClinux__) + fstype = get_fuse_fstype(); + + err = NTFS_VOLUME_NO_PRIVILEGE; + if (restore_privs()) + goto err_out; + + if (fstype == FSTYPE_NONE || fstype == FSTYPE_UNKNOWN) + fstype = load_fuse_module(); + create_dev_fuse(); + + if (drop_privs()) + goto err_out; +#endif + if (stat(opts.device, &sbuf)) { + ntfs_log_perror("Failed to access '%s'", opts.device); + err = NTFS_VOLUME_NO_PRIVILEGE; + goto err_out; + } + +#if !(defined(__sun) && defined (__SVR4)) + /* Always use fuseblk for block devices unless it's surely missing. */ + if (S_ISBLK(sbuf.st_mode) && (fstype != FSTYPE_FUSE)) + ctx->blkdev = TRUE; +#endif + +#ifndef FUSE_INTERNAL + if (getuid() && ctx->blkdev) { + ntfs_log_error("%s", unpriv_fuseblk_msg); + err = NTFS_VOLUME_NO_PRIVILEGE; + goto err2; + } +#endif + err = ntfs_open(opts.device); + if (err) + goto err_out; + + /* Force read-only mount if the device was found read-only */ + if (!ctx->ro && NVolReadOnly(ctx->vol)) { + ctx->rw = FALSE; + ctx->ro = TRUE; + if (ntfs_strinsert(&parsed_options, ",ro")) + goto err_out; + ntfs_log_info("Could not mount read-write, trying read-only\n"); + } else + if (ctx->rw && ntfs_strinsert(&parsed_options, ",rw")) + goto err_out; + /* We must do this after ntfs_open() to be able to set the blksize */ + if (ctx->blkdev && set_fuseblk_options(&parsed_options)) + goto err_out; + + ctx->vol->abs_mnt_point = ctx->abs_mnt_point; + ctx->security.vol = ctx->vol; + ctx->vol->secure_flags = ctx->secure_flags; + ctx->vol->special_files = ctx->special_files; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + ctx->vol->efs_raw = ctx->efs_raw; +#endif /* HAVE_SETXATTR */ + if (!ntfs_build_mapping(&ctx->security,ctx->usermap_path, + (ctx->vol->secure_flags + & ((1 << SECURITY_DEFAULT) | (1 << SECURITY_ACL))) + && !ctx->inherit + && !(ctx->vol->secure_flags & (1 << SECURITY_WANTED)))) { +#if POSIXACLS + /* use basic permissions if requested */ + if (ctx->vol->secure_flags & (1 << SECURITY_DEFAULT)) + permissions_mode = "User mapping built, Posix ACLs not used"; + else { + permissions_mode = "User mapping built, Posix ACLs in use"; +#if KERNELACLS + if (ntfs_strinsert(&parsed_options, ",default_permissions,acl")) { + err = NTFS_VOLUME_SYNTAX_ERROR; + goto err_out; + } +#endif /* KERNELACLS */ + } +#else /* POSIXACLS */ +#if KERNELPERMS + if (!(ctx->vol->secure_flags + & ((1 << SECURITY_DEFAULT) | (1 << SECURITY_ACL)))) { + /* + * No explicit option but user mapping found + * force default security + */ + ctx->vol->secure_flags |= (1 << SECURITY_DEFAULT); + if (ntfs_strinsert(&parsed_options, ",default_permissions")) { + err = NTFS_VOLUME_SYNTAX_ERROR; + goto err_out; + } + } +#endif /* KERNELPERMS */ + permissions_mode = "User mapping built"; +#endif /* POSIXACLS */ + ctx->dmask = ctx->fmask = 0; + } else { + ctx->security.uid = ctx->uid; + ctx->security.gid = ctx->gid; + /* same ownership/permissions for all files */ + ctx->security.mapping[MAPUSERS] = (struct MAPPING*)NULL; + ctx->security.mapping[MAPGROUPS] = (struct MAPPING*)NULL; + if ((ctx->vol->secure_flags & (1 << SECURITY_WANTED)) + && !(ctx->vol->secure_flags & (1 << SECURITY_DEFAULT))) { + ctx->vol->secure_flags |= (1 << SECURITY_DEFAULT); + if (ntfs_strinsert(&parsed_options, ",default_permissions")) { + err = NTFS_VOLUME_SYNTAX_ERROR; + goto err_out; + } + } + if (ctx->vol->secure_flags & (1 << SECURITY_DEFAULT)) { + ctx->vol->secure_flags |= (1 << SECURITY_RAW); + permissions_mode = "Global ownership and permissions enforced"; + } else { + ctx->vol->secure_flags &= ~(1 << SECURITY_RAW); + permissions_mode = "Ownership and permissions disabled"; + } + } + if (ctx->usermap_path) + free (ctx->usermap_path); + +#if defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) + xattr_mapping = ntfs_xattr_build_mapping(ctx->vol, + ctx->xattrmap_path); + ctx->vol->xattr_mapping = xattr_mapping; + /* + * Errors are logged, do not refuse mounting, it would be + * too difficult to fix the unmountable mapping file. + */ + if (ctx->xattrmap_path) + free(ctx->xattrmap_path); +#endif /* defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) */ + +#ifndef DISABLE_PLUGINS + register_internal_reparse_plugins(); +#endif /* DISABLE_PLUGINS */ + + fh = mount_fuse(parsed_options); + if (!fh) { + err = NTFS_VOLUME_FUSE_ERROR; + goto err_out; + } + + ctx->mounted = TRUE; + +#if defined(linux) || defined(__uClinux__) + if (S_ISBLK(sbuf.st_mode) && (fstype == FSTYPE_FUSE)) + ntfs_log_info("%s", fuse26_kmod_msg); +#endif + setup_logging(parsed_options); + if (failed_secure) + ntfs_log_info("%s\n",failed_secure); + if (permissions_mode) + ntfs_log_info("%s, configuration type %d\n",permissions_mode, + 4 + POSIXACLS*6 - KERNELPERMS*3 + CACHEING); + if ((ctx->vol->secure_flags & (1 << SECURITY_RAW)) + && !ctx->uid && ctx->gid) + ntfs_log_error("Warning : using problematic uid==0 and gid!=0\n"); + + fuse_loop(fh); + + err = 0; + + fuse_unmount(opts.mnt_point, ctx->fc); + fuse_destroy(fh); +err_out: + ntfs_mount_error(opts.device, opts.mnt_point, err); + if (ctx->abs_mnt_point) + free(ctx->abs_mnt_point); +#if defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) + ntfs_xattr_free_mapping(xattr_mapping); +#endif /* defined(HAVE_SETXATTR) && defined(XATTR_MAPPINGS) */ +err2: + ntfs_close(); +#ifndef DISABLE_PLUGINS + close_reparse_plugins(ctx); +#endif /* DISABLE_PLUGINS */ + free(ctx); + free(parsed_options); + free(opts.options); + free(opts.device); + return err; +} diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.probe.8.in b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.probe.8.in new file mode 100644 index 0000000..9b57493 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.probe.8.in @@ -0,0 +1,85 @@ +.\" Copyright (c) 2008 Szabolcs Szakacsits. +.\" This file may be copied under the terms of the GNU Public License. +.\" +.TH NTFS-3G.PROBE 8 "January 2008" "ntfs-3g.probe @VERSION@" +.SH NAME +ntfs-3g.probe \- Probe an NTFS volume mountability +.SH SYNOPSIS +.B ntfs-3g.probe +.I <\-\-readonly|\-\-readwrite> +.I volume +.br +.SH DESCRIPTION +The \fBntfs-3g.probe\fR utility tests a volume if it's NTFS mountable +read-only or read-write, and exits with a status value accordingly. +The \fIvolume\fR can be a block device or image file. +.SH OPTIONS +Below is a summary of the options that \fBntfs-3g.probe\fR accepts. +.TP +.B \-r, \-\-readonly +Test if the volume can be mounted read-only. +.TP +.B \-w, \-\-readwrite +Test if the volume can be mounted read-write. +.TP +.B \-h, \-\-help +Display help and exit. +.SH EXAMPLE +Test if /dev/sda1 can be mounted read-write: +.RS +.sp +.B ntfs-3g.probe --readwrite /dev/sda1 +.sp +.RE +.SH EXIT CODES +The exit codes are as follows: +.IP 0 +Volume is mountable. +.IP 11 +Syntax error, command line parsing failed. +.IP 12 +The volume doesn't have a valid NTFS. +.IP 13 +Inconsistent NTFS, hardware or device driver fault, or unsetup +SoftRAID/FakeRAID hardware. +.IP 14 +The NTFS partition is hibernated. +.IP 15 +The volume was not cleanly unmounted. +.IP 16 +The volume is already exclusively opened and in use by a kernel +driver or software. +.IP 17 +Unsetup SoftRAID/FakeRAID hardware. +.IP 18 +Unknown reason. +.IP 19 +Not enough privilege to mount. +.IP 20 +Out of memory. +.IP 21 +Unclassified FUSE error. +.SH KNOWN ISSUES +Please see +.RS +.sp +https://github.com/tuxera/ntfs-3g/wiki/NTFS-3G-FAQ +.sp +.RE +for common questions and known issues. +If you think you have found an undocumented problem in the latest release of +the software then please post an ntfs-3g issue describing it in detail +so that the development team can be aware of the issue and take care of it: +.RS +.sp +https://github.com/tuxera/ntfs-3g/issues +.sp +.RE +.SH AUTHORS +.B ntfs-3g.probe +was written by Szabolcs Szakacsits. +.SH THANKS +Alon Bar-Lev has integrated the utility into the NTFS-3G build process and +tested it with Erik Larsson before the public release. +.SH SEE ALSO +.BR ntfs-3g (8) diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.probe.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.probe.c new file mode 100644 index 0000000..cb73aee --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g.probe.c @@ -0,0 +1,166 @@ +/** + * ntfs-3g.probe - Probe NTFS volume mountability + * + * Copyright (c) 2007-2009 Szabolcs Szakacsits + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#include + +#include "compat.h" +#include "volume.h" +#include "misc.h" + +typedef enum { + PROBE_UNSET, + PROBE_READONLY, + PROBE_READWRITE +} probe_t; + +static struct options { + probe_t probetype; + char *device; +} opts; + +static const char *EXEC_NAME = "ntfs-3g.probe"; + +static const char *usage_msg = +"\n" +"%s %s - Probe NTFS volume mountability\n" +"\n" +"Copyright (C) 2007 Szabolcs Szakacsits\n" +"\n" +"Usage: %s <--readonly|--readwrite> \n" +"\n" +"Example: ntfs-3g.probe --readwrite /dev/sda1\n" +"\n" +"%s"; + +static int ntfs_open(const char *device) +{ + ntfs_volume *vol; + unsigned long flags = 0; + int ret = NTFS_VOLUME_OK; + + if (opts.probetype == PROBE_READONLY) + flags |= NTFS_MNT_RDONLY; + + vol = ntfs_mount(device, flags); + if (!vol) + ret = ntfs_volume_error(errno); + + if (ret == 0 && ntfs_umount(vol, FALSE) == -1) + ret = ntfs_volume_error(errno); + + return ret; +} + +static void usage(void) +{ + ntfs_log_info(usage_msg, EXEC_NAME, VERSION, EXEC_NAME, ntfs_home); +} + +static int parse_options(int argc, char *argv[]) +{ + int c; + + static const char *sopt = "-hrw"; + static const struct option lopt[] = { + { "readonly", no_argument, NULL, 'r' }, + { "readwrite", no_argument, NULL, 'w' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } + }; + + opterr = 0; /* We handle errors. */ + opts.probetype = PROBE_UNSET; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opts.device) { + opts.device = ntfs_malloc(PATH_MAX + 1); + if (!opts.device) + return -1; + + strncpy(opts.device, optarg, PATH_MAX); + opts.device[PATH_MAX] = 0; + } else { + ntfs_log_error("%s: You must specify exactly " + "one device\n", EXEC_NAME); + return -1; + } + break; + case 'h': + usage(); + exit(0); + case 'r': + opts.probetype = PROBE_READONLY; + break; + case 'w': + opts.probetype = PROBE_READWRITE; + break; + default: + ntfs_log_error("%s: Unknown option '%s'.\n", EXEC_NAME, + argv[optind - 1]); + return -1; + } + } + + if (!opts.device) { + ntfs_log_error("ERROR: %s: Device is missing\n", EXEC_NAME); + return -1; + } + + if (opts.probetype == PROBE_UNSET) { + ntfs_log_error("ERROR: %s: Probe type is missing\n", EXEC_NAME); + return -1; + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + int err; + + ntfs_log_set_handler(ntfs_log_handler_stderr); + + if (parse_options(argc, argv)) { + usage(); + exit(NTFS_VOLUME_SYNTAX_ERROR); + } + + err = ntfs_open(opts.device); + + free(opts.device); + if (err) + exit(err); + return (0); +} + diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g_common.c b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g_common.c new file mode 100644 index 0000000..f0ee145 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g_common.c @@ -0,0 +1,1021 @@ +/** + * ntfs-3g_common.c - Common definitions for ntfs-3g and lowntfs-3g. + * + * Copyright (c) 2010-2021 Jean-Pierre Andre + * Copyright (c) 2010 Erik Larsson + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +#include +#endif + +#ifdef HAVE_DLFCN_H +#include +#endif + +#ifdef HAVE_STRING_H +#include +#endif + +#ifdef HAVE_LIMITS_H +#include +#endif + +#ifdef HAVE_ERRNO_H +#include +#endif + +#include +#include + +#include "compat.h" +#include "inode.h" +#include "dir.h" +#include "security.h" +#include "xattrs.h" +#include "reparse.h" +#include "plugin.h" +#include "ntfs-3g_common.h" +#include "realpath.h" +#include "misc.h" + +const char xattr_ntfs_3g[] = "ntfs-3g."; + +const char nf_ns_user_prefix[] = "user."; +const int nf_ns_user_prefix_len = sizeof(nf_ns_user_prefix) - 1; +const char nf_ns_system_prefix[] = "system."; +const int nf_ns_system_prefix_len = sizeof(nf_ns_system_prefix) - 1; +const char nf_ns_security_prefix[] = "security."; +const int nf_ns_security_prefix_len = sizeof(nf_ns_security_prefix) - 1; +const char nf_ns_trusted_prefix[] = "trusted."; +const int nf_ns_trusted_prefix_len = sizeof(nf_ns_trusted_prefix) - 1; + +static const char nf_ns_alt_xattr_efsinfo[] = "user.ntfs.efsinfo"; + +static const char def_opts[] = "allow_other,nonempty,"; + + /* + * Table of recognized options + * Their order may be significant + * The options invalid in some configuration should still + * be present, so that an error can be returned + */ +const struct DEFOPTION optionlist[] = { + { "ro", OPT_RO, FLGOPT_APPEND | FLGOPT_BOGUS }, + { "noatime", OPT_NOATIME, FLGOPT_BOGUS }, + { "atime", OPT_ATIME, FLGOPT_BOGUS }, + { "relatime", OPT_RELATIME, FLGOPT_BOGUS }, + { "delay_mtime", OPT_DMTIME, FLGOPT_DECIMAL | FLGOPT_OPTIONAL }, + { "rw", OPT_RW, FLGOPT_BOGUS }, + { "fake_rw", OPT_FAKE_RW, FLGOPT_BOGUS }, + { "fsname", OPT_FSNAME, FLGOPT_NOSUPPORT }, + { "no_def_opts", OPT_NO_DEF_OPTS, FLGOPT_BOGUS }, + { "default_permissions", OPT_DEFAULT_PERMISSIONS, FLGOPT_BOGUS }, + { "permissions", OPT_PERMISSIONS, FLGOPT_BOGUS }, + { "acl", OPT_ACL, FLGOPT_BOGUS }, + { "umask", OPT_UMASK, FLGOPT_OCTAL }, + { "fmask", OPT_FMASK, FLGOPT_OCTAL }, + { "dmask", OPT_DMASK, FLGOPT_OCTAL }, + { "uid", OPT_UID, FLGOPT_DECIMAL }, + { "gid", OPT_GID, FLGOPT_DECIMAL }, + { "show_sys_files", OPT_SHOW_SYS_FILES, FLGOPT_BOGUS }, + { "hide_hid_files", OPT_HIDE_HID_FILES, FLGOPT_BOGUS }, + { "hide_dot_files", OPT_HIDE_DOT_FILES, FLGOPT_BOGUS }, + { "ignore_case", OPT_IGNORE_CASE, FLGOPT_BOGUS }, + { "windows_names", OPT_WINDOWS_NAMES, FLGOPT_BOGUS }, + { "compression", OPT_COMPRESSION, FLGOPT_BOGUS }, + { "nocompression", OPT_NOCOMPRESSION, FLGOPT_BOGUS }, + { "silent", OPT_SILENT, FLGOPT_BOGUS }, + { "recover", OPT_RECOVER, FLGOPT_BOGUS }, + { "norecover", OPT_NORECOVER, FLGOPT_BOGUS }, + { "remove_hiberfile", OPT_REMOVE_HIBERFILE, FLGOPT_BOGUS }, + { "sync", OPT_SYNC, FLGOPT_BOGUS | FLGOPT_APPEND }, + { "big_writes", OPT_BIG_WRITES, FLGOPT_BOGUS }, + { "locale", OPT_LOCALE, FLGOPT_STRING }, + { "nfconv", OPT_NFCONV, FLGOPT_BOGUS }, + { "nonfconv", OPT_NONFCONV, FLGOPT_BOGUS }, + { "streams_interface", OPT_STREAMS_INTERFACE, FLGOPT_STRING }, + { "user_xattr", OPT_USER_XATTR, FLGOPT_BOGUS }, + { "noauto", OPT_NOAUTO, FLGOPT_BOGUS }, + { "debug", OPT_DEBUG, FLGOPT_BOGUS }, + { "no_detach", OPT_NO_DETACH, FLGOPT_BOGUS }, + { "remount", OPT_REMOUNT, FLGOPT_BOGUS }, + { "blksize", OPT_BLKSIZE, FLGOPT_STRING }, + { "inherit", OPT_INHERIT, FLGOPT_BOGUS }, + { "addsecurids", OPT_ADDSECURIDS, FLGOPT_BOGUS }, + { "staticgrps", OPT_STATICGRPS, FLGOPT_BOGUS }, + { "usermapping", OPT_USERMAPPING, FLGOPT_STRING }, + { "xattrmapping", OPT_XATTRMAPPING, FLGOPT_STRING }, + { "efs_raw", OPT_EFS_RAW, FLGOPT_BOGUS }, + { "posix_nlink", OPT_POSIX_NLINK, FLGOPT_BOGUS }, + { "special_files", OPT_SPECIAL_FILES, FLGOPT_STRING }, + { "--help", OPT_HELP, FLGOPT_BOGUS }, + { "-h", OPT_HELP, FLGOPT_BOGUS }, + { "--version", OPT_VERSION, FLGOPT_BOGUS }, + { "-V", OPT_VERSION, FLGOPT_BOGUS }, + { (const char*)NULL, 0, 0 } /* end marker */ +} ; + +#define STRAPPEND_MAX_INSIZE 8192 +#define strappend_is_large(x) ((x) > STRAPPEND_MAX_INSIZE) + +int ntfs_strappend(char **dest, const char *append) +{ + char *p; + size_t size_append, size_dest = 0; + + if (!dest) + return -1; + if (!append) + return 0; + + size_append = strlen(append); + if (*dest) + size_dest = strlen(*dest); + + if (strappend_is_large(size_dest) || strappend_is_large(size_append)) { + errno = EOVERFLOW; + ntfs_log_perror("%s: Too large input buffer", EXEC_NAME); + return -1; + } + + p = (char*)realloc(*dest, size_dest + size_append + 1); + if (!p) { + ntfs_log_perror("%s: Memory realloction failed", EXEC_NAME); + return -1; + } + + *dest = p; + strcpy(*dest + size_dest, append); + + return 0; +} + +int ntfs_strappend_escaped(char **dest, const char *append) +{ + int ret = -1; + char *escaped_string = NULL; + +#if FUSE_VERSION >= 27 + if (fuse_version() >= 28) { + const char *ptr = append; + char *dest_ptr = NULL; + size_t append_length = 0; + size_t escaped_length = 0; + + while (*ptr) { + if (*ptr == '\\' || *ptr == ',') { + escaped_length++; + } + + escaped_length++; + append_length++; + ptr++; + } + + if (escaped_length != append_length) { + ptr = append; + + escaped_string = malloc(escaped_length + 1); + if (!escaped_string) { + goto out; + } + + dest_ptr = escaped_string; + + while (*ptr) { + if (*ptr == '\\' || *ptr == ',') { + *(dest_ptr++) = '\\'; + } + + *(dest_ptr++) = *(ptr++); + } + + *dest_ptr = '\0'; + + ntfs_log_debug("Generated escaped string: %s\n", + escaped_string); + } + } +#endif /* FUSE_VERSION >= 27 */ + + ret = ntfs_strappend(dest, escaped_string ? escaped_string : append); +out: + if (escaped_string) { + free(escaped_string); + } + + return ret; +} + +/* + * Insert an option before ",fsname=" + * This is for keeping "fsname" as the last option, because on + * Solaris device names may contain commas. + */ + +int ntfs_strinsert(char **dest, const char *append) +{ + char *p, *q; + size_t size_append, size_dest = 0; + + if (!dest) + return -1; + if (!append) + return 0; + + size_append = strlen(append); + if (*dest) + size_dest = strlen(*dest); + + if (strappend_is_large(size_dest) || strappend_is_large(size_append)) { + errno = EOVERFLOW; + ntfs_log_perror("%s: Too large input buffer", EXEC_NAME); + return -1; + } + + p = (char*)malloc(size_dest + size_append + 1); + if (!p) { + ntfs_log_perror("%s: Memory reallocation failed", EXEC_NAME); + return -1; + } + strcpy(p, *dest); + q = strstr(p, ",fsname="); + if (q) { + strcpy(q, append); + q = strstr(*dest, ",fsname="); + if (q) + strcat(p, q); + free(*dest); + *dest = p; + } else { + free(*dest); + *dest = p; + strcpy(*dest + size_dest, append); + } + return 0; +} + +static int bogus_option_value(char *val, const char *s) +{ + if (val) { + ntfs_log_error("'%s' option shouldn't have value.\n", s); + return -1; + } + return 0; +} + +static int missing_option_value(char *val, const char *s) +{ + if (!val) { + ntfs_log_error("'%s' option should have a value.\n", s); + return -1; + } + return 0; +} + +char *parse_mount_options(ntfs_fuse_context_t *ctx, + const struct ntfs_options *popts, BOOL low_fuse) +{ + char *options, *s, *opt, *val, *ret = NULL; + const char *orig_opts = popts->options; + BOOL no_def_opts = FALSE; + int default_permissions = 0; + int permissions = 0; + int acl = 0; + int want_permissions = 0; + int intarg; + const struct DEFOPTION *poptl; + + ctx->secure_flags = 0; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + ctx->efs_raw = FALSE; +#endif /* HAVE_SETXATTR */ + ctx->compression = DEFAULT_COMPRESSION; + options = strdup(orig_opts ? orig_opts : ""); + if (!options) { + ntfs_log_perror("%s: strdup failed", EXEC_NAME); + return NULL; + } + + s = options; + while (s && *s && (val = strsep(&s, ","))) { + opt = strsep(&val, "="); + poptl = optionlist; + while (poptl->name && strcmp(poptl->name,opt)) + poptl++; + if (poptl->name) { + if ((poptl->flags & FLGOPT_BOGUS) + && bogus_option_value(val, opt)) + goto err_exit; + if ((poptl->flags & FLGOPT_OCTAL) + && (!val + || sscanf(val, "%o", &intarg) != 1)) { + ntfs_log_error("'%s' option needs an octal value\n", + opt); + goto err_exit; + } + if (poptl->flags & FLGOPT_DECIMAL) { + if ((poptl->flags & FLGOPT_OPTIONAL) && !val) + intarg = 0; + else + if (!val + || sscanf(val, "%i", &intarg) != 1) + { + ntfs_log_error("'%s' option " + "needs a decimal value\n", + opt); + goto err_exit; + } + } + if ((poptl->flags & FLGOPT_STRING) + && missing_option_value(val, opt)) + goto err_exit; + + switch (poptl->type) { + case OPT_RO : + case OPT_FAKE_RW : + ctx->ro = TRUE; + break; + case OPT_RW : + ctx->rw = TRUE; + break; + case OPT_NOATIME : + ctx->atime = ATIME_DISABLED; + break; + case OPT_ATIME : + ctx->atime = ATIME_ENABLED; + break; + case OPT_RELATIME : + ctx->atime = ATIME_RELATIVE; + break; + case OPT_DMTIME : + if (!intarg) + intarg = DEFAULT_DMTIME; + ctx->dmtime = intarg*10000000LL; + break; + case OPT_NO_DEF_OPTS : + no_def_opts = TRUE; /* Don't add default options. */ + ctx->silent = FALSE; /* cancel default silent */ + break; + case OPT_DEFAULT_PERMISSIONS : + default_permissions = 1; + break; + case OPT_PERMISSIONS : + permissions = 1; + break; +#if POSIXACLS + case OPT_ACL : + acl = 1; + break; +#endif + case OPT_UMASK : + ctx->dmask = ctx->fmask = intarg; + want_permissions = 1; + break; + case OPT_FMASK : + ctx->fmask = intarg; + want_permissions = 1; + break; + case OPT_DMASK : + ctx->dmask = intarg; + want_permissions = 1; + break; + case OPT_UID : + ctx->uid = intarg; + want_permissions = 1; + break; + case OPT_GID : + ctx->gid = intarg; + want_permissions = 1; + break; + case OPT_SHOW_SYS_FILES : + ctx->show_sys_files = TRUE; + break; + case OPT_HIDE_HID_FILES : + ctx->hide_hid_files = TRUE; + break; + case OPT_HIDE_DOT_FILES : + ctx->hide_dot_files = TRUE; + break; + case OPT_WINDOWS_NAMES : + ctx->windows_names = TRUE; + break; + case OPT_IGNORE_CASE : + if (low_fuse) + ctx->ignore_case = TRUE; + else { + ntfs_log_error("'%s' is an unsupported option.\n", + poptl->name); + goto err_exit; + } + break; + case OPT_COMPRESSION : + ctx->compression = TRUE; + break; + case OPT_NOCOMPRESSION : + ctx->compression = FALSE; + break; + case OPT_SILENT : + ctx->silent = TRUE; + break; + case OPT_RECOVER : + ctx->recover = TRUE; + break; + case OPT_NORECOVER : + ctx->recover = FALSE; + break; + case OPT_REMOVE_HIBERFILE : + ctx->hiberfile = TRUE; + break; + case OPT_SYNC : + ctx->sync = TRUE; + break; +#ifdef FUSE_CAP_BIG_WRITES + case OPT_BIG_WRITES : + ctx->big_writes = TRUE; + break; +#endif + case OPT_LOCALE : + ntfs_set_char_encoding(val); + break; +#if defined(__APPLE__) || defined(__DARWIN__) +#ifdef ENABLE_NFCONV + case OPT_NFCONV : + if (ntfs_macosx_normalize_filenames(1)) { + ntfs_log_error("ntfs_macosx_normalize_filenames(1) failed!\n"); + goto err_exit; + } + break; + case OPT_NONFCONV : + if (ntfs_macosx_normalize_filenames(0)) { + ntfs_log_error("ntfs_macosx_normalize_filenames(0) failed!\n"); + goto err_exit; + } + break; +#endif /* ENABLE_NFCONV */ +#endif /* defined(__APPLE__) || defined(__DARWIN__) */ + case OPT_STREAMS_INTERFACE : + if (!strcmp(val, "none")) + ctx->streams = NF_STREAMS_INTERFACE_NONE; + else if (!strcmp(val, "xattr")) + ctx->streams = NF_STREAMS_INTERFACE_XATTR; + else if (!strcmp(val, "openxattr")) + ctx->streams = NF_STREAMS_INTERFACE_OPENXATTR; + else if (!low_fuse && !strcmp(val, "windows")) + ctx->streams = NF_STREAMS_INTERFACE_WINDOWS; + else { + ntfs_log_error("Invalid named data streams " + "access interface.\n"); + goto err_exit; + } + break; + case OPT_USER_XATTR : +#if defined(__APPLE__) || defined(__DARWIN__) + /* macOS builds use non-namespaced extended + * attributes by default since it matches the + * standard behaviour of macOS filesystems. */ + ctx->streams = NF_STREAMS_INTERFACE_OPENXATTR; +#else + ctx->streams = NF_STREAMS_INTERFACE_XATTR; +#endif + break; + case OPT_NOAUTO : + /* Don't pass noauto option to fuse. */ + break; + case OPT_DEBUG : + ctx->debug = TRUE; + ntfs_log_set_levels(NTFS_LOG_LEVEL_DEBUG); + ntfs_log_set_levels(NTFS_LOG_LEVEL_TRACE); + break; + case OPT_NO_DETACH : + ctx->no_detach = TRUE; + break; + case OPT_REMOUNT : + ntfs_log_error("Remounting is not supported at present." + " You have to umount volume and then " + "mount it once again.\n"); + goto err_exit; + case OPT_BLKSIZE : + ntfs_log_info("WARNING: blksize option is ignored " + "because ntfs-3g must calculate it.\n"); + break; + case OPT_INHERIT : + /* + * do not overwrite inherited permissions + * in create() + */ + ctx->inherit = TRUE; + break; + case OPT_ADDSECURIDS : + /* + * create security ids for files being read + * with an individual security attribute + */ + ctx->secure_flags |= (1 << SECURITY_ADDSECURIDS); + break; + case OPT_STATICGRPS : + /* + * use static definition of groups + * for file access control + */ + ctx->secure_flags |= (1 << SECURITY_STATICGRPS); + break; + case OPT_USERMAPPING : + ctx->usermap_path = strdup(val); + if (!ctx->usermap_path) { + ntfs_log_error("no more memory to store " + "'usermapping' option.\n"); + goto err_exit; + } + break; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ +#ifdef XATTR_MAPPINGS + case OPT_XATTRMAPPING : + ctx->xattrmap_path = strdup(val); + if (!ctx->xattrmap_path) { + ntfs_log_error("no more memory to store " + "'xattrmapping' option.\n"); + goto err_exit; + } + break; +#endif /* XATTR_MAPPINGS */ + case OPT_EFS_RAW : + ctx->efs_raw = TRUE; + break; +#endif /* HAVE_SETXATTR */ + case OPT_POSIX_NLINK : + ctx->posix_nlink = TRUE; + break; + case OPT_SPECIAL_FILES : + if (!strcmp(val, "interix")) + ctx->special_files = NTFS_FILES_INTERIX; + else if (!strcmp(val, "wsl")) + ctx->special_files = NTFS_FILES_WSL; + else { + ntfs_log_error("Invalid special_files" + " mode.\n"); + goto err_exit; + } + break; + case OPT_FSNAME : /* Filesystem name. */ + /* + * We need this to be able to check whether filesystem + * mounted or not. + * (falling through to default) + */ + case OPT_HELP : /* Could lead to unclean condition */ + case OPT_VERSION : /* Could lead to unclean condition */ + default : + ntfs_log_error("'%s' is an unsupported option.\n", + poptl->name); + goto err_exit; + } + if ((poptl->flags & FLGOPT_APPEND) + && (ntfs_strappend(&ret, poptl->name) + || ntfs_strappend(&ret, ","))) + goto err_exit; + } else { /* Probably FUSE option. */ + if (ntfs_strappend(&ret, opt)) + goto err_exit; + if (val) { + if (ntfs_strappend(&ret, "=")) + goto err_exit; + if (ntfs_strappend(&ret, val)) + goto err_exit; + } + if (ntfs_strappend(&ret, ",")) + goto err_exit; + } + } + if (!no_def_opts && ntfs_strappend(&ret, def_opts)) + goto err_exit; + if ((default_permissions || (permissions && !acl)) + && ntfs_strappend(&ret, "default_permissions,")) + goto err_exit; + /* The atime options exclude each other */ + if (ctx->atime == ATIME_RELATIVE && ntfs_strappend(&ret, "relatime,")) + goto err_exit; + else if (ctx->atime == ATIME_ENABLED && ntfs_strappend(&ret, "atime,")) + goto err_exit; + else if (ctx->atime == ATIME_DISABLED && ntfs_strappend(&ret, "noatime,")) + goto err_exit; + + if (ntfs_strappend(&ret, "fsname=")) + goto err_exit; + if (ntfs_strappend_escaped(&ret, popts->device)) { + goto err_exit; + } + if (permissions && !acl) + ctx->secure_flags |= (1 << SECURITY_DEFAULT); + if (acl) + ctx->secure_flags |= (1 << SECURITY_ACL); + if (want_permissions) + ctx->secure_flags |= (1 << SECURITY_WANTED); + if (ctx->ro) { + ctx->secure_flags &= ~(1 << SECURITY_ADDSECURIDS); + ctx->hiberfile = FALSE; + ctx->rw = FALSE; + } +exit: + free(options); + return ret; +err_exit: + free(ret); + ret = NULL; + goto exit; +} + +/** + * parse_options - Read and validate the programs command line + * Read the command line, verify the syntax and parse the options. + * + * Return: 0 success, -1 error. + */ +int ntfs_parse_options(struct ntfs_options *popts, void (*usage)(void), + int argc, char *argv[]) +{ + int c; + + static const char *sopt = "-o:hnsvV"; + static const struct option lopt[] = { + { "options", required_argument, NULL, 'o' }, + { "help", no_argument, NULL, 'h' }, + { "no-mtab", no_argument, NULL, 'n' }, + { "verbose", no_argument, NULL, 'v' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } + }; + + opterr = 0; /* We'll handle the errors, thank you. */ + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!popts->device) { + popts->device = ntfs_malloc(PATH_MAX + 1); + if (!popts->device) + return -1; + + /* Canonicalize device name (mtab, etc) */ + popts->arg_device = optarg; + if (!ntfs_realpath_canonicalize(optarg, + popts->device)) { + ntfs_log_perror("%s: Failed to access " + "volume '%s'", EXEC_NAME, optarg); + free(popts->device); + popts->device = NULL; + return -1; + } + } else if (!popts->mnt_point) { + popts->mnt_point = optarg; + } else { + ntfs_log_error("%s: You must specify exactly one " + "device and exactly one mount " + "point.\n", EXEC_NAME); + return -1; + } + break; + case 'o': + if (popts->options) + if (ntfs_strappend(&popts->options, ",")) + return -1; + if (ntfs_strappend(&popts->options, optarg)) + return -1; + break; + case 'h': + usage(); + exit(9); + case 'n': + /* + * no effect - automount passes it, meaning 'no-mtab' + */ + break; + case 's': + /* + * no effect - automount passes it, meaning sloppy + */ + break; + case 'v': + /* + * We must handle the 'verbose' option even if + * we don't use it because mount(8) passes it. + */ + break; + case 'V': + ntfs_log_info("%s %s %s %d\n", EXEC_NAME, VERSION, + FUSE_TYPE, fuse_version()); + exit(0); + default: + ntfs_log_error("%s: Unknown option '%s'.\n", EXEC_NAME, + argv[optind - 1]); + return -1; + } + } + + if (!popts->device) { + ntfs_log_error("%s: No device is specified.\n", EXEC_NAME); + return -1; + } + if (!popts->mnt_point) { + ntfs_log_error("%s: No mountpoint is specified.\n", EXEC_NAME); + return -1; + } + + return 0; +} + +#ifdef HAVE_SETXATTR + +int ntfs_fuse_listxattr_common(ntfs_inode *ni, ntfs_attr_search_ctx *actx, + char *list, size_t size, BOOL prefixing) +{ + int ret = 0; + char *to = list; +#ifdef XATTR_MAPPINGS + BOOL accepted; + const struct XATTRMAPPING *item; +#endif /* XATTR_MAPPINGS */ + + /* first list the regular user attributes (ADS) */ + while (!ntfs_attr_lookup(AT_DATA, NULL, 0, CASE_SENSITIVE, + 0, NULL, 0, actx)) { + char *tmp_name = NULL; + int tmp_name_len; + + if (!actx->attr->name_length) + continue; + tmp_name_len = ntfs_ucstombs( + (ntfschar *)((u8*)actx->attr + + le16_to_cpu(actx->attr->name_offset)), + actx->attr->name_length, &tmp_name, 0); + if (tmp_name_len < 0) { + ret = -errno; + goto exit; + } + /* + * When using name spaces, do not return + * security, trusted or system attributes + * (filtered elsewhere anyway) + * otherwise insert "user." prefix + */ + if (prefixing) { + if ((strlen(tmp_name) > sizeof(xattr_ntfs_3g)) + && !strncmp(tmp_name,xattr_ntfs_3g, + sizeof(xattr_ntfs_3g)-1)) + tmp_name_len = 0; + else + ret += tmp_name_len + + nf_ns_user_prefix_len + 1; + } else + ret += tmp_name_len + 1; + if (size && tmp_name_len) { + if ((size_t)ret <= size) { + if (prefixing) { + strcpy(to, nf_ns_user_prefix); + to += nf_ns_user_prefix_len; + } + strncpy(to, tmp_name, tmp_name_len); + to += tmp_name_len; + *to = 0; + to++; + } else { + free(tmp_name); + ret = -ERANGE; + goto exit; + } + } + free(tmp_name); + } +#ifdef XATTR_MAPPINGS + /* now append the system attributes mapped to user space */ + for (item=ni->vol->xattr_mapping; item; item=item->next) { + switch (item->xattr) { + case XATTR_NTFS_EFSINFO : + accepted = ni->vol->efs_raw + && (ni->flags & FILE_ATTR_ENCRYPTED); + break; + case XATTR_NTFS_REPARSE_DATA : + accepted = (ni->flags & FILE_ATTR_REPARSE_POINT) + != const_cpu_to_le32(0); + break; +// TODO : we are supposed to only return xattrs which are set +// this is more complex for OBJECT_ID and DOS_NAME + default : accepted = TRUE; + break; + } + if (accepted) { + ret += strlen(item->name) + 1; + if (size) { + if ((size_t)ret <= size) { + strcpy(to, item->name); + to += strlen(item->name); + *to++ = 0; + } else { + ret = -ERANGE; + goto exit; + } + } +#else /* XATTR_MAPPINGS */ + /* List efs info xattr for encrypted files */ + if (ni->vol->efs_raw && (ni->flags & FILE_ATTR_ENCRYPTED)) { + ret += sizeof(nf_ns_alt_xattr_efsinfo); + if ((size_t)ret <= size) { + memcpy(to, nf_ns_alt_xattr_efsinfo, + sizeof(nf_ns_alt_xattr_efsinfo)); + to += sizeof(nf_ns_alt_xattr_efsinfo); +#endif /* XATTR_MAPPINGS */ + } + } +exit : + return (ret); +} + +#endif /* HAVE_SETXATTR */ + +#ifndef DISABLE_PLUGINS + +int register_reparse_plugin(ntfs_fuse_context_t *ctx, le32 tag, + const plugin_operations_t *ops, void *handle) +{ + plugin_list_t *plugin; + int res; + + res = -1; + plugin = (plugin_list_t*)ntfs_malloc(sizeof(plugin_list_t)); + if (plugin) { + plugin->tag = tag; + plugin->ops = ops; + plugin->handle = handle; + plugin->next = ctx->plugins; + ctx->plugins = plugin; + res = 0; + } + return (res); +} + +/* + * Get the reparse operations associated to an inode + * + * The plugin able to process the reparse point is dynamically loaded + * + * When successful, returns the operations vector and the reparse + * data if requested, + * Otherwise returns NULL, with errno set. + */ + +const struct plugin_operations *select_reparse_plugin(ntfs_fuse_context_t *ctx, + ntfs_inode *ni, REPARSE_POINT **reparse_wanted) +{ + const struct plugin_operations *ops; + void *handle; + REPARSE_POINT *reparse; + le32 tag, seltag; + plugin_list_t *plugin; + plugin_init_t pinit; + + ops = (struct plugin_operations*)NULL; + reparse = ntfs_get_reparse_point(ni); + if (reparse) { + tag = reparse->reparse_tag; + seltag = tag & IO_REPARSE_PLUGIN_SELECT; + for (plugin=ctx->plugins; plugin && (plugin->tag != seltag); + plugin = plugin->next) { } + if (plugin) { + ops = plugin->ops; + } else { +#ifdef PLUGIN_DIR + char name[sizeof(PLUGIN_DIR) + 64]; + + snprintf(name,sizeof(name), PLUGIN_DIR + "/ntfs-plugin-%08lx.so", + (long)le32_to_cpu(seltag)); +#else + char name[64]; + + snprintf(name,sizeof(name), "ntfs-plugin-%08lx.so", + (long)le32_to_cpu(seltag)); +#endif + handle = dlopen(name, RTLD_LAZY); + if (handle) { + pinit = (plugin_init_t)dlsym(handle, "init"); + if (pinit) { + /* pinit() should set errno if it fails */ + ops = (*pinit)(tag); + if (ops && register_reparse_plugin(ctx, + seltag, ops, handle)) + ops = (struct plugin_operations*)NULL; + } else + errno = ELIBBAD; + if (!ops) + dlclose(handle); + } else { + errno = ELIBACC; + if (!(ctx->errors_logged & ERR_PLUGIN)) { + ntfs_log_perror( + "Could not load plugin %s", + name); + ntfs_log_error("Hint %s\n",dlerror()); + } + ctx->errors_logged |= ERR_PLUGIN; + } + } + if (ops && reparse_wanted) + *reparse_wanted = reparse; + else + free(reparse); + } + return (ops); +} + +void close_reparse_plugins(ntfs_fuse_context_t *ctx) +{ + while (ctx->plugins) { + plugin_list_t *next; + + next = ctx->plugins->next; + if (ctx->plugins->handle) + dlclose(ctx->plugins->handle); + free(ctx->plugins); + ctx->plugins = next; + } +} + +#endif /* DISABLE_PLUGINS */ + +#ifdef HAVE_SETXATTR + +/* + * Check whether a user xattr is allowed + * + * The inode must be a plain file or a directory. The only allowed + * metadata file is the root directory (useful for MacOSX and hopefully + * does not harm Windows). + */ + +BOOL user_xattrs_allowed(ntfs_fuse_context_t *ctx __attribute__((unused)), + ntfs_inode *ni) +{ + u32 dt_type; + BOOL res; + + /* Quick return for common cases and root */ + if (!(ni->flags & (FILE_ATTR_SYSTEM | FILE_ATTR_REPARSE_POINT)) + || (ni->mft_no == FILE_root)) + res = TRUE; + else { + /* Reparse point depends on kind, see plugin */ + if (ni->flags & FILE_ATTR_REPARSE_POINT) { +#ifndef DISABLE_PLUGINS + struct stat stbuf; + REPARSE_POINT *reparse; + const plugin_operations_t *ops; + + res = FALSE; /* default for error cases */ + ops = select_reparse_plugin(ctx, ni, &reparse); + if (ops) { + if (ops->getattr + && !ops->getattr(ni,reparse,&stbuf)) { + res = S_ISREG(stbuf.st_mode) + || S_ISDIR(stbuf.st_mode); + } + free(reparse); + } +#else /* DISABLE_PLUGINS */ + res = FALSE; /* mountpoints, symlinks, ... */ +#endif /* DISABLE_PLUGINS */ + } else { + /* Metadata */ + if (ni->mft_no < FILE_first_user) + res = FALSE; + else { + /* Interix types */ + dt_type = ntfs_interix_types(ni); + res = (dt_type == NTFS_DT_REG) + || (dt_type == NTFS_DT_DIR); + } + } + } + return (res); +} + +#endif /* HAVE_SETXATTR */ diff --git a/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g_common.h b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g_common.h new file mode 100644 index 0000000..8ead510 --- /dev/null +++ b/code/drivers/fs/ntfs/ntfs3g/third_party/ntfs-3g/src/ntfs-3g_common.h @@ -0,0 +1,223 @@ +/* + * ntfs-3g_common.h - Common declarations for ntfs-3g and lowntfs-3g. + * + * Copyright (c) 2010-2011 Jean-Pierre Andre + * Copyright (c) 2010 Erik Larsson + * + * This program/include file is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program/include file is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _NTFS_3G_COMMON_H +#define _NTFS_3G_COMMON_H + +#include "inode.h" + +struct ntfs_options { + char *mnt_point; /* Mount point */ + char *options; /* Mount options */ + char *device; /* Device to mount */ + char *arg_device; /* Device requested in argv */ +} ; + +typedef enum { + NF_STREAMS_INTERFACE_NONE, /* No access to named data streams. */ + NF_STREAMS_INTERFACE_XATTR, /* Map named data streams to xattrs. */ + NF_STREAMS_INTERFACE_OPENXATTR, /* Same, not limited to "user." */ + NF_STREAMS_INTERFACE_WINDOWS, /* "file:stream" interface. */ +} ntfs_fuse_streams_interface; + +struct DEFOPTION { + const char *name; + int type; + int flags; +} ; + /* Options, order not significant */ +enum { + OPT_RO, + OPT_NOATIME, + OPT_ATIME, + OPT_RELATIME, + OPT_DMTIME, + OPT_RW, + OPT_FAKE_RW, + OPT_FSNAME, + OPT_NO_DEF_OPTS, + OPT_DEFAULT_PERMISSIONS, + OPT_PERMISSIONS, + OPT_ACL, + OPT_UMASK, + OPT_FMASK, + OPT_DMASK, + OPT_UID, + OPT_GID, + OPT_SHOW_SYS_FILES, + OPT_HIDE_HID_FILES, + OPT_HIDE_DOT_FILES, + OPT_IGNORE_CASE, + OPT_WINDOWS_NAMES, + OPT_COMPRESSION, + OPT_NOCOMPRESSION, + OPT_SILENT, + OPT_RECOVER, + OPT_NORECOVER, + OPT_REMOVE_HIBERFILE, + OPT_SYNC, + OPT_BIG_WRITES, + OPT_LOCALE, + OPT_NFCONV, + OPT_NONFCONV, + OPT_STREAMS_INTERFACE, + OPT_USER_XATTR, + OPT_NOAUTO, + OPT_DEBUG, + OPT_NO_DETACH, + OPT_REMOUNT, + OPT_BLKSIZE, + OPT_INHERIT, + OPT_ADDSECURIDS, + OPT_STATICGRPS, + OPT_USERMAPPING, + OPT_XATTRMAPPING, + OPT_EFS_RAW, + OPT_POSIX_NLINK, + OPT_SPECIAL_FILES, + OPT_HELP, + OPT_VERSION, +} ; + + /* Option flags */ +enum { + FLGOPT_BOGUS = 1, + FLGOPT_STRING = 2, + FLGOPT_OCTAL = 4, + FLGOPT_DECIMAL = 8, + FLGOPT_APPEND = 16, + FLGOPT_NOSUPPORT = 32, + FLGOPT_OPTIONAL = 64 +} ; + +typedef enum { + ATIME_ENABLED, + ATIME_DISABLED, + ATIME_RELATIVE +} ntfs_atime_t; + +typedef enum { + ERR_PLUGIN = 1 +} single_log_t; + +#ifndef DISABLE_PLUGINS + +typedef struct plugin_list { + struct plugin_list *next; + void *handle; + const plugin_operations_t *ops; + le32 tag; +} plugin_list_t; + +#endif /* DISABLE_PLUGINS */ + +typedef struct { + ntfs_volume *vol; + unsigned int uid; + unsigned int gid; + unsigned int fmask; + unsigned int dmask; + ntfs_fuse_streams_interface streams; + ntfs_atime_t atime; + s64 dmtime; + BOOL ro; + BOOL rw; + BOOL show_sys_files; + BOOL hide_hid_files; + BOOL hide_dot_files; + BOOL windows_names; + BOOL ignore_case; + BOOL compression; + BOOL acl; + BOOL silent; + BOOL recover; + BOOL hiberfile; + BOOL sync; + BOOL big_writes; + BOOL debug; + BOOL no_detach; + BOOL blkdev; + BOOL mounted; + BOOL posix_nlink; + ntfs_volume_special_files special_files; +#ifdef HAVE_SETXATTR /* extended attributes interface required */ + BOOL efs_raw; +#ifdef XATTR_MAPPINGS + char *xattrmap_path; +#endif /* XATTR_MAPPINGS */ +#endif /* HAVE_SETXATTR */ + struct fuse_chan *fc; + BOOL inherit; + unsigned int secure_flags; + single_log_t errors_logged; + char *usermap_path; + char *abs_mnt_point; +#ifndef DISABLE_PLUGINS + plugin_list_t *plugins; +#endif /* DISABLE_PLUGINS */ + struct PERMISSIONS_CACHE *seccache; + struct SECURITY_CONTEXT security; + struct open_file *open_files; /* only defined in lowntfs-3g */ + u64 latest_ghost; +} ntfs_fuse_context_t; + +extern const char *EXEC_NAME; + +#ifdef FUSE_INTERNAL +#define FUSE_TYPE "integrated FUSE" +#else +#define FUSE_TYPE "external FUSE" +#endif + +extern const char xattr_ntfs_3g[]; + +extern const char nf_ns_user_prefix[]; +extern const int nf_ns_user_prefix_len; +extern const char nf_ns_system_prefix[]; +extern const int nf_ns_system_prefix_len; +extern const char nf_ns_security_prefix[]; +extern const int nf_ns_security_prefix_len; +extern const char nf_ns_trusted_prefix[]; +extern const int nf_ns_trusted_prefix_len; + +int ntfs_strappend(char **dest, const char *append); +int ntfs_strinsert(char **dest, const char *append); +char *parse_mount_options(ntfs_fuse_context_t *ctx, + const struct ntfs_options *popts, BOOL low_fuse); +int ntfs_parse_options(struct ntfs_options *popts, void (*usage)(void), + int argc, char *argv[]); + +int ntfs_fuse_listxattr_common(ntfs_inode *ni, ntfs_attr_search_ctx *actx, + char *list, size_t size, BOOL prefixing); +BOOL user_xattrs_allowed(ntfs_fuse_context_t *ctx, ntfs_inode *ni); + +#ifndef DISABLE_PLUGINS + +void close_reparse_plugins(ntfs_fuse_context_t *ctx); +const struct plugin_operations *select_reparse_plugin(ntfs_fuse_context_t *ctx, + ntfs_inode *ni, REPARSE_POINT **reparse); +int register_reparse_plugin(ntfs_fuse_context_t *ctx, le32 tag, + const plugin_operations_t *ops, void *handle); + +#endif /* DISABLE_PLUGINS */ + +#endif /* _NTFS_3G_COMMON_H */ diff --git a/code/drivers/net/lwip/lwip_sys_arch.c b/code/drivers/net/lwip/lwip_sys_arch.c index 6151d6b..ec7c7f2 100644 --- a/code/drivers/net/lwip/lwip_sys_arch.c +++ b/code/drivers/net/lwip/lwip_sys_arch.c @@ -313,7 +313,6 @@ void sys_mbox_free(sys_mbox_t* mbox) { sys_thread_t sys_thread_new(const char* name, lwip_thread_fn thread, void* arg, int stacksize, int prio) { - (void)name; (void)stacksize; if (!thread) @@ -337,7 +336,7 @@ sys_thread_t sys_thread_new(const char* name, lwip_thread_fn thread, void* arg, // function pointer that lwIP supplied, with exactly the argument it supplied. THREAD* t = 0; - const KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NORMAL(kp); + const KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NAMED_NORMAL(kp, name); KSTATUS st = KeCreateKernelThread((uintptr_t)thread, (uintptr_t)arg, ¶meters, diff --git a/code/drivers/storage/ahci/ahci.c b/code/drivers/storage/ahci/ahci.c index 156009c..bf52a9c 100644 --- a/code/drivers/storage/ahci/ahci.c +++ b/code/drivers/storage/ahci/ahci.c @@ -1071,7 +1071,8 @@ static KSTATUS AhciStartController(AHCI_CONTROLLER* ctl) { st = KeEventCreate(EventManualReset, 0, &drv->workerFinished); if (KSUCCESS(st)) { drv->workerRunning = true; - const KE_THREAD_CREATE_PARAMETERS workerParameters = KE_THREAD_CREATE_NORMAL(PriorityHigh); + const KE_THREAD_CREATE_PARAMETERS workerParameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityHigh, "ahci.disk"); st = KeCreateKernelThread((uintptr_t)AhciWorker, (uintptr_t)drv, &workerParameters, diff --git a/code/drivers/storage/ata/ata.c b/code/drivers/storage/ata/ata.c index af04614..1816e40 100644 --- a/code/drivers/storage/ata/ata.c +++ b/code/drivers/storage/ata/ata.c @@ -54,29 +54,92 @@ static void AtaDelay400ns(ATA_CHANNEL* ch) { (void)AtaAltStatus(ch); } -// Waits for not Busy. -static KSTATUS AtaWaitNotBusy(ATA_CHANNEL* ch) { - for (uint32_t i = 0; i < ATA_POLL_LIMIT; i++) { - if (!(AtaIn8(ch, ATA_REG_STATUS) & ATA_STATUS_BSY)) - return NTLSTATUS_SUCCESS; - KeYield(); - } - return NTLSTATUS_TIMEOUT; +// Returns the monotonic DDK timer in microseconds. +static uint64_t AtaQueryTicks(void) { + return gAtaDdk->Timer->QueryTicks(); } -// Waits for drq. -static KSTATUS AtaWaitDrq(ATA_CHANNEL* ch) { - for (uint32_t i = 0; i < ATA_POLL_LIMIT; i++) { - uint8_t st = AtaIn8(ch, ATA_REG_STATUS); - if (st & ATA_STATUS_ERR) - return NTLSTATUS_DEVICE_ERROR; - if (st & ATA_STATUS_DF) - return NTLSTATUS_DEVICE_ERROR; - if (!(st & ATA_STATUS_BSY) && (st & ATA_STATUS_DRQ)) - return NTLSTATUS_SUCCESS; - KeYield(); +// Delays briefly between ATA status samples without involving the scheduler. +static void AtaPollDelay(void) { + gAtaDdk->Timer->Stall(ATA_POLL_INTERVAL_US); +} + +// Returns whether a status byte represents an unimplemented/floating legacy channel. +static bool AtaStatusIsAbsent(uint8_t status) { + return status == 0x00u || status == 0xFFu; +} + +// Waits until one microsecond deadline expires while polling alternate status. +static KSTATUS AtaWaitStatus(ATA_CHANNEL* ch, uint64_t timeoutUs, bool requireDrq, bool checkErrors, + uint8_t* finalStatusOut) { + uint64_t start = AtaQueryTicks(); + uint64_t deadline = start > UINT64_MAX - timeoutUs ? UINT64_MAX : start + timeoutUs; + uint64_t pollBudget = timeoutUs / ATA_POLL_INTERVAL_US + 2u; + + for (;;) { + uint8_t status = AtaAltStatus(ch); + if (finalStatusOut) *finalStatusOut = status; + if (AtaStatusIsAbsent(status)) return NTLSTATUS_NOT_FOUND; + if (!(status & ATA_STATUS_BSY)) { + if (checkErrors && (status & (ATA_STATUS_ERR | ATA_STATUS_DF))) + return NTLSTATUS_DEVICE_ERROR; + if (!requireDrq || (status & ATA_STATUS_DRQ)) + return NTLSTATUS_SUCCESS; + } + if (AtaQueryTicks() >= deadline || pollBudget-- == 0u) + return NTLSTATUS_TIMEOUT; + AtaPollDelay(); } - return NTLSTATUS_TIMEOUT; +} + +// Waits boundedly for BSY to clear using elapsed hardware time rather than scheduler iterations. +static KSTATUS AtaWaitNotBusy(ATA_CHANNEL* ch) { + return AtaWaitStatus(ch, ATA_POLL_TIMEOUT_US, false, true, (uint8_t*)0); +} + +// Waits boundedly for a data phase while rejecting absent and failed devices early. +static KSTATUS AtaWaitDrq(ATA_CHANNEL* ch) { + return AtaWaitStatus(ch, ATA_POLL_TIMEOUT_US, true, true, (uint8_t*)0); +} + +// Emits the task-file state needed to distinguish selection, signature, and command failures. +static void AtaLogProbeState(const ATA_DEVICE* dev, const char* stage, KSTATUS result) { + if (!dev || !dev->Channel) return; + ATA_CHANNEL* ch = dev->Channel; + DbgPrint("ATA: channel %u %s identify %s result=0x%x status=0x%02x error=0x%02x lba1=0x%02x lba2=0x%02x\n", + ch->Index, + dev->Drive ? "slave" : "master", + stage ? stage : "stage", + result, + AtaAltStatus(ch), + AtaIn8(ch, ATA_REG_ERROR), + AtaIn8(ch, ATA_REG_LBA1), + AtaIn8(ch, ATA_REG_LBA2)); +} + +// Resets one legacy ATA channel before probing so firmware state cannot leak into IDENTIFY. +static KSTATUS AtaResetChannel(ATA_CHANNEL* ch) { + if (!ch) return NTLSTATUS_INVALID_PARAMETER; + + uint8_t before = AtaAltStatus(ch); + DbgPrint("ATA: channel %u reset begin status=0x%02x\n", ch->Index, before); + + AtaDeviceControl(ch, ATA_DEVICE_CONTROL_NIEN | ATA_DEVICE_CONTROL_SRST); + gAtaDdk->Timer->Stall(ATA_RESET_ASSERT_US); + AtaDeviceControl(ch, ATA_DEVICE_CONTROL_NIEN); + gAtaDdk->Timer->Stall(ATA_RESET_SETTLE_US); + + uint8_t finalStatus = AtaAltStatus(ch); + if (AtaStatusIsAbsent(finalStatus)) { + DbgPrint("ATA: channel %u reset found no responding device status=0x%02x\n", + ch->Index, finalStatus); + return NTLSTATUS_NOT_FOUND; + } + + KSTATUS status = AtaWaitStatus(ch, ATA_RESET_TIMEOUT_US, false, false, &finalStatus); + DbgPrint("ATA: channel %u reset complete result=0x%x status=0x%02x\n", + ch->Index, status, finalStatus); + return status; } // Implements the select Drive operation. @@ -86,7 +149,7 @@ static void AtaSelectDrive(ATA_DEVICE* dev) { AtaDelay400ns(ch); } -// Implements the identify operation. +// Implements IDENTIFY with stage diagnostics and finite hardware deadlines. static KSTATUS AtaIdentify(ATA_DEVICE* dev) { ATA_CHANNEL* ch = dev->Channel; uint16_t ident[256]; @@ -95,8 +158,20 @@ static KSTATUS AtaIdentify(ATA_DEVICE* dev) { KSTATUS st = KeWaitSemaphore(ch->Lock, KE_WAIT_INFINITE); if (!KSUCCESS(st)) return st; + DbgPrint("ATA: channel %u %s identify lock acquired\n", + ch->Index, dev->Drive ? "slave" : "master"); AtaSelectDrive(dev); + DbgPrint("ATA: channel %u %s identify selected status=0x%02x\n", + ch->Index, dev->Drive ? "slave" : "master", AtaAltStatus(ch)); + + st = AtaWaitStatus(ch, ATA_POLL_TIMEOUT_US, false, false, (uint8_t*)0); + if (!KSUCCESS(st)) { + AtaLogProbeState(dev, "pre-command wait", st); + KeSignalSemaphore(ch->Lock); + return st; + } + AtaOut8(ch, ATA_REG_SECTOR_COUNT, 0); AtaOut8(ch, ATA_REG_LBA0, 0); AtaOut8(ch, ATA_REG_LBA1, 0); @@ -104,25 +179,33 @@ static KSTATUS AtaIdentify(ATA_DEVICE* dev) { AtaOut8(ch, ATA_REG_COMMAND, ATA_CMD_IDENTIFY_DEVICE); AtaDelay400ns(ch); - uint8_t status = AtaIn8(ch, ATA_REG_STATUS); - if (status == 0) { + uint8_t status = AtaAltStatus(ch); + DbgPrint("ATA: channel %u %s identify command issued status=0x%02x\n", + ch->Index, dev->Drive ? "slave" : "master", status); + if (AtaStatusIsAbsent(status)) { + AtaLogProbeState(dev, "absent after command", NTLSTATUS_NOT_FOUND); KeSignalSemaphore(ch->Lock); return NTLSTATUS_NOT_FOUND; } st = AtaWaitNotBusy(ch); if (!KSUCCESS(st)) { + AtaLogProbeState(dev, "busy timeout/error", st); KeSignalSemaphore(ch->Lock); return st; } - if (AtaIn8(ch, ATA_REG_LBA1) != 0 || AtaIn8(ch, ATA_REG_LBA2) != 0) { + uint8_t lba1 = AtaIn8(ch, ATA_REG_LBA1); + uint8_t lba2 = AtaIn8(ch, ATA_REG_LBA2); + if (lba1 != 0 || lba2 != 0) { + AtaLogProbeState(dev, "non-ATA signature", NTLSTATUS_NOT_SUPPORTED); KeSignalSemaphore(ch->Lock); - return NTLSTATUS_NOT_SUPPORTED; // ATAPI or non-ATA signature. + return NTLSTATUS_NOT_SUPPORTED; } st = AtaWaitDrq(ch); if (!KSUCCESS(st)) { + AtaLogProbeState(dev, "DRQ timeout/error", st); KeSignalSemaphore(ch->Lock); return st; } @@ -130,7 +213,11 @@ static KSTATUS AtaIdentify(ATA_DEVICE* dev) { for (uint32_t i = 0; i < 256; i++) ident[i] = AtaIn16(ch, ATA_REG_DATA); + AtaLogProbeState(dev, "data complete", NTLSTATUS_SUCCESS); KeSignalSemaphore(ch->Lock); + DbgPrint("ATA: channel %u %s identify lock released capabilities=0x%04x command_sets=0x%04x\n", + ch->Index, dev->Drive ? "slave" : "master", + ident[ATA_IDENT_CAPABILITIES], ident[ATA_IDENT_COMMAND_SETS]); if (!(ident[ATA_IDENT_CAPABILITIES] & ATA_IDENT_LBA_SUPPORTED)) return NTLSTATUS_NOT_SUPPORTED; @@ -148,6 +235,9 @@ static KSTATUS AtaIdentify(ATA_DEVICE* dev) { ident[ATA_IDENT_LBA28_SECTORS_LO]; } + DbgPrint("ATA: channel %u %s identify parsed lba48=%u sectors=%llu\n", + ch->Index, dev->Drive ? "slave" : "master", + dev->SupportsLba48 ? 1u : 0u, (unsigned long long)dev->SectorCount); return dev->SectorCount ? NTLSTATUS_SUCCESS : NTLSTATUS_NOT_FOUND; } @@ -323,8 +413,14 @@ static void AtaWorker(uintptr_t arg) { if (!dev) return; - while (true) { - KeWaitSemaphore(dev->IrpQueue.Semaphore, KE_WAIT_INFINITE); + DbgPrint("ATA: channel %u %s worker online\n", + dev->Channel ? dev->Channel->Index : 0u, dev->Drive ? "slave" : "master"); + while (dev->WorkerRunning) { + KSTATUS waitStatus = KeWaitSemaphore(dev->IrpQueue.Semaphore, KE_WAIT_INFINITE); + if (!KSUCCESS(waitStatus)) + continue; + if (!dev->WorkerRunning) + break; IRP* irp = IrpQueueDequeue(&dev->IrpQueue); if (!irp) @@ -357,6 +453,58 @@ static void AtaWorker(uintptr_t arg) { IoReleaseRemoveLock(&dev->RemoveLock, irp); IoCompleteRequest(irp, st, information); } + + DbgPrint("ATA: channel %u %s worker stopped\n", + dev->Channel ? dev->Channel->Index : 0u, dev->Drive ? "slave" : "master"); + if (dev->WorkerFinished) + KeSignalEvent(dev->WorkerFinished); +} + +// Stops one ATA disk worker and releases the worker-owned synchronization references. +static void AtaStopWorker(ATA_DEVICE* dev) { + if (!dev) + return; + + dev->WorkerRunning = false; + if (dev->IrpQueue.Semaphore) + KeSignalSemaphore(dev->IrpQueue.Semaphore); + if (dev->WorkerThread && dev->WorkerFinished) + (void)KeWaitForEvent(dev->WorkerFinished, 0); + + if (dev->WorkerThread) { + ObDereferenceObject((OBJECT*)dev->WorkerThread); + dev->WorkerThread = (THREAD*)0; + } + if (dev->WorkerFinished) { + ObDereferenceObject(&dev->WorkerFinished->Header); + dev->WorkerFinished = (EVENT*)0; + } + if (dev->IrpQueue.Semaphore) { + ObDereferenceObject(&dev->IrpQueue.Semaphore->Header); + dev->IrpQueue.Semaphore = (SEMAPHORE*)0; + } +} + +// Starts one ATA disk worker before the disk is published to synchronous discovery consumers. +static KSTATUS AtaStartWorker(ATA_DEVICE* dev) { + if (!dev || !dev->IrpQueue.Semaphore) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = KeEventCreate(EventManualReset, 0, &dev->WorkerFinished); + if (!KSUCCESS(status)) + return status; + + dev->WorkerRunning = true; + const KE_THREAD_CREATE_PARAMETERS workerParameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityHigh, "ata.disk"); + status = KeCreateKernelThread((uintptr_t)AtaWorker, (uintptr_t)dev, + &workerParameters, &dev->WorkerThread); + if (!KSUCCESS(status)) { + dev->WorkerRunning = false; + AtaStopWorker(dev); + return status; + } + return NTLSTATUS_SUCCESS; } // Dispatches read Write. @@ -461,7 +609,7 @@ static KSTATUS AtaInitChannel(ATA_CONTROLLER* ctl, uint8_t index) { if (!KSUCCESS(st)) return st; - AtaDeviceControl(ch, 0x02u); // nIEN: polled I/O for now. + AtaDeviceControl(ch, ATA_DEVICE_CONTROL_NIEN); // nIEN: polled I/O for now. DbgPrint("ATA: channel %u cmd=0x%04x ctl=0x%04x irq=%u mode=%s\n", index, ch->CommandBase, @@ -531,18 +679,33 @@ static KSTATUS AtaProbeDrive(ATA_CONTROLLER* ctl, ATA_CHANNEL* ch, uint8_t drive return st; IrpQueueInit(&dev->IrpQueue, sem); - dev->Present = true; - st = AtaRegisterDisk(dev); + DbgPrint("ATA: channel %u %s worker start before disk publication\n", + ch->Index, drive ? "slave" : "master"); + st = AtaStartWorker(dev); if (!KSUCCESS(st)) { - dev->Present = false; + DbgPrint("ATA: channel %u %s worker start failed (0x%x)\n", + ch->Index, drive ? "slave" : "master", st); + AtaStopWorker(dev); + IoDestroyRemoveLock(&dev->RemoveLock); return st; } - const KE_THREAD_CREATE_PARAMETERS workerParameters = KE_THREAD_CREATE_NORMAL(PriorityHigh); - st = KeCreateKernelThread((uintptr_t)AtaWorker, (uintptr_t)dev, - &workerParameters, &dev->WorkerThread); - if (!KSUCCESS(st)) - DbgPrint("ATA: drive %u worker thread create failed (0x%x)\n", dev->DiskIndex, st); + dev->Present = true; + DbgPrint("ATA: channel %u %s publishing disk sectors=%llu lba48=%u\n", + ch->Index, drive ? "slave" : "master", + (unsigned long long)dev->SectorCount, dev->SupportsLba48 ? 1u : 0u); + st = AtaRegisterDisk(dev); + if (!KSUCCESS(st)) { + DbgPrint("ATA: channel %u %s disk publication failed (0x%x)\n", + ch->Index, drive ? "slave" : "master", st); + dev->Present = false; + AtaStopWorker(dev); + IoDestroyRemoveLock(&dev->RemoveLock); + return st; + } + + DbgPrint("ATA: channel %u %s probe complete disk=%u\n", + ch->Index, drive ? "slave" : "master", dev->DiskIndex); return NTLSTATUS_SUCCESS; } @@ -598,10 +761,20 @@ static KSTATUS AtaStartController(ATA_CONTROLLER* ctl) { continue; } + st = AtaResetChannel(ch); + if (!KSUCCESS(st)) + DbgPrint("ATA: channel %u reset did not complete cleanly (0x%x); probing drives with bounded waits\n", + chIndex, st); + for (uint8_t drive = 0; drive < ATA_DRIVES_PER_CHANNEL; drive++) { + DbgPrint("ATA: channel %u probing %s\n", chIndex, drive ? "slave" : "master"); st = AtaProbeDrive(ctl, ch, drive); - if (KSUCCESS(st)) + if (KSUCCESS(st)) { ctl->DeviceCount++; + } else if (st != NTLSTATUS_NOT_FOUND) { + DbgPrint("ATA: channel %u %s probe failed (0x%x)\n", + chIndex, drive ? "slave" : "master", st); + } } } @@ -633,7 +806,9 @@ static void AtaRemoveControllerDevices(ATA_CONTROLLER* ctl, IO_DEVICE_CHANGE cha if (!dev->Present) continue; + dev->Present = false; IrpQueueCancelAllWithCallback(&dev->IrpQueue, NTLSTATUS_OBJECT_DEAD, AtaCancelReleaseRemoveLock, dev); + AtaStopWorker(dev); if (dev->DiskIndex != 0xFFu) { if (change == IoDeviceSurpriseRemoved) IoNotifyDiskSurpriseRemoved(dev->DiskIndex); @@ -641,7 +816,6 @@ static void AtaRemoveControllerDevices(ATA_CONTROLLER* ctl, IO_DEVICE_CHANGE cha IoUnregisterDiskDevice(dev->DiskIndex); dev->DiskIndex = 0xFFu; } - dev->Present = false; } } } @@ -701,12 +875,15 @@ KSTATUS AtaDispatchPnp(DEVICE_OBJECT* device, IRP* irp) { device->PnpState = DevicePnpSurpriseRemoved; dev->Present = false; IrpQueueCancelAllWithCallback(&dev->IrpQueue, NTLSTATUS_OBJECT_DEAD, AtaCancelReleaseRemoveLock, dev); + AtaStopWorker(dev); return NTLSTATUS_SUCCESS; case IRP_MN_REMOVE_DEVICE: device->PnpState = DevicePnpDeleted; dev->Present = false; IrpQueueCancelAllWithCallback(&dev->IrpQueue, NTLSTATUS_OBJECT_DEAD, AtaCancelReleaseRemoveLock, dev); + AtaStopWorker(dev); IoReleaseRemoveLockAndWait(&dev->RemoveLock, irp); + IoDestroyRemoveLock(&dev->RemoveLock); IoDeleteDevice(device); dev->DeviceObject = (DEVICE_OBJECT*)0; return NTLSTATUS_SUCCESS; @@ -748,7 +925,7 @@ KSTATUS AtaAddDevice(DRIVER_OBJECT* driver, DEVICE_OBJECT* physicalDeviceObject) // Implements the driver Entry operation. KSTATUS DriverEntry(const NTL_DDK* ddk, NTL_DRIVER_INIT* init) { (void)init; - if (!ddk) + if (!ddk || !ddk->Timer || !ddk->Timer->QueryTicks || !ddk->Timer->Stall) return NTLSTATUS_INVALID_PARAMETER; DbgPrint("ATA: driver loading...\n"); diff --git a/code/drivers/storage/ata/ata.h b/code/drivers/storage/ata/ata.h index 5efcdc7..7634f7f 100644 --- a/code/drivers/storage/ata/ata.h +++ b/code/drivers/storage/ata/ata.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #define ATA_CONTROLLER_MAGIC 0x41544143u // ATAC @@ -47,6 +48,9 @@ #define ATA_REG_ALT_STATUS 0x00u #define ATA_REG_DEVICE_CONTROL 0x00u +#define ATA_DEVICE_CONTROL_NIEN 0x02u +#define ATA_DEVICE_CONTROL_SRST 0x04u + #define ATA_STATUS_ERR 0x01u #define ATA_STATUS_DRQ 0x08u #define ATA_STATUS_DF 0x20u @@ -70,7 +74,11 @@ #define ATA_IDENT_LBA48_SUPPORTED (1u << 10) #define ATA_SECTOR_SIZE 512u -#define ATA_POLL_LIMIT 100000u +#define ATA_POLL_TIMEOUT_US 250000u +#define ATA_RESET_TIMEOUT_US 5000000u +#define ATA_POLL_INTERVAL_US 10u +#define ATA_RESET_ASSERT_US 5u +#define ATA_RESET_SETTLE_US 2000u struct ATA_CONTROLLER; struct ATA_CHANNEL; @@ -92,6 +100,8 @@ typedef struct ATA_DEVICE { SEMAPHORE* IrpSemaphore; IO_REMOVE_LOCK RemoveLock; THREAD* WorkerThread; + EVENT* WorkerFinished; + volatile bool WorkerRunning; } ATA_DEVICE; typedef struct ATA_CHANNEL { diff --git a/code/kernel/hal/hal_main.c b/code/kernel/hal/hal_main.c index 6863ccc..1099498 100644 --- a/code/kernel/hal/hal_main.c +++ b/code/kernel/hal/hal_main.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -112,11 +113,26 @@ static HAL_PROCESSOR_TOPOLOGY gCpuTopology; // ACPI power-off state (filled in by ParseFadt / ScanDsdtForS5). // Do not guess SLP_TYP on real hardware: these values are firmware-specific. -static uint16_t gPm1aCntBlk = 0; -static uint16_t gPm1bCntBlk = 0; -static uint8_t gSlpTypA = 0; -static uint8_t gSlpTypB = 0; -static int gS5Found = 0; +typedef struct ACPI_FIXED_REGISTER { + uint8_t AddressSpaceId; + uint8_t WidthBytes; + uint16_t Reserved; + uint64_t Address; +} ACPI_FIXED_REGISTER; + +static ACPI_FIXED_REGISTER gPm1aCnt; +static ACPI_FIXED_REGISTER gPm1bCnt; +static ACPI_FIXED_REGISTER gSleepControl; +static uint32_t gLegacyPm1aPort = 0; +static uint32_t gLegacyPm1bPort = 0; +static uint32_t gFadtFlags = 0; +static uint32_t gSmiCommandPort = 0; +static uint8_t gAcpiEnableValue = 0; +static uint8_t gSlpTypA = 0; +static uint8_t gSlpTypB = 0; +static int gS5Found = 0; +static int gHwReducedAcpi = 0; +static int gIntelPchPmBase = 0; int debugEnabled = 0; int _fltused; // MSVC floating-point helper anchor. @@ -156,13 +172,21 @@ void* HalPhysicalToVirtual(uint64_t phys) { // FADT (Fixed ACPI Description Table) offsets used here. ACPI 2.0+ // systems may put the real DSDT and PM1 control registers in the extended // X_* fields, so do not rely only on the legacy 32-bit fields. -#define FADT_DSDT_OFFSET 40u -#define FADT_PM1A_CNT_BLK_OFFSET 64u -#define FADT_PM1B_CNT_BLK_OFFSET 68u -#define FADT_X_DSDT_OFFSET 140u -#define FADT_X_PM1A_CNT_BLK_OFFSET 172u -#define FADT_X_PM1B_CNT_BLK_OFFSET 184u +#define FADT_DSDT_OFFSET 40u +#define FADT_SMI_CMD_OFFSET 48u +#define FADT_ACPI_ENABLE_OFFSET 52u +#define FADT_PM1A_CNT_BLK_OFFSET 64u +#define FADT_PM1B_CNT_BLK_OFFSET 68u +#define FADT_PM1_CNT_LEN_OFFSET 89u #define FADT_CENTURY_OFFSET 108u +#define FADT_FLAGS_OFFSET 112u +#define FADT_X_DSDT_OFFSET 140u +#define FADT_X_PM1A_CNT_BLK_OFFSET 172u +#define FADT_X_PM1B_CNT_BLK_OFFSET 184u +#define FADT_SLEEP_CONTROL_OFFSET 244u +#define FADT_FLAG_HW_REDUCED_ACPI (1u << 20) +#define INTEL_PCH_SMI_EN_OFFSET 0x30u +#define INTEL_PCH_SMI_ON_SLP_EN (1u << 4) #pragma pack(push, 1) typedef struct { @@ -190,19 +214,131 @@ static uint64_t AcpiReadU64(const void* table, uint32_t tableLen, uint32_t off) return value; } -// Implements the acpi Read Gas Io Port operation. -static uint16_t AcpiReadGasIoPort(const void* table, uint32_t tableLen, uint32_t off) { +// Decodes one ACPI Generic Address Structure used for fixed power registers. +static int AcpiReadGasRegister(const void* table, uint32_t tableLen, uint32_t off, + ACPI_FIXED_REGISTER* out) { + if (!out || off + sizeof(ACPI_GAS) > tableLen) return 0; + ACPI_GAS gas; - if (off + sizeof(gas) > tableLen) return 0; - memcpy(&gas, (const uint8_t*)table + off, sizeof(gas)); + if (!gas.Address || gas.RegisterBitOffset != 0) return 0; + if (gas.AddressSpaceId != 0x00 && gas.AddressSpaceId != 0x01) return 0; + if (gas.AddressSpaceId == 0x01 && gas.Address > 0xFFFFu) return 0; - // 0x01 == System I/O. HalShutdown currently writes PM1_CNT with outw(), - // so ignore MMIO GAS entries until the HAL grows MMIO register access here. - if (gas.AddressSpaceId != 0x01 || gas.Address == 0 || gas.Address > 0xFFFF) - return 0; + uint8_t widthBytes = 0; + if (gas.AccessSize >= 1 && gas.AccessSize <= 3) + widthBytes = (uint8_t)(1u << (gas.AccessSize - 1u)); + else if (gas.RegisterBitWidth) + widthBytes = (uint8_t)((gas.RegisterBitWidth + 7u) / 8u); + if (widthBytes < 2u) widthBytes = 2u; + if (widthBytes > 4u) widthBytes = 4u; - return (uint16_t)gas.Address; + out->AddressSpaceId = gas.AddressSpaceId; + out->WidthBytes = widthBytes; + out->Address = gas.Address; + return 1; +} + +// Decodes one byte-wide ACPI GAS used by the HW-reduced Sleep Control register. +static int AcpiReadByteGasRegister(const void* table, uint32_t tableLen, uint32_t off, + ACPI_FIXED_REGISTER* out) { + if (!out || off + sizeof(ACPI_GAS) > tableLen) return 0; + + ACPI_GAS gas; + memcpy(&gas, (const uint8_t*)table + off, sizeof(gas)); + if (!gas.Address || gas.RegisterBitOffset != 0 || gas.RegisterBitWidth != 8) return 0; + if (gas.AddressSpaceId != 0x00 && gas.AddressSpaceId != 0x01) return 0; + if (gas.AddressSpaceId == 0x01 && gas.Address > 0xFFFFu) return 0; + + out->AddressSpaceId = gas.AddressSpaceId; + out->WidthBytes = 1u; + out->Address = gas.Address; + return 1; +} + +// Reads one ACPI fixed register while preserving the address-space distinction. +static uint32_t AcpiReadFixedRegister(const ACPI_FIXED_REGISTER* reg) { + if (!reg || !reg->Address) return 0; + + if (reg->AddressSpaceId == 0x01) { + uint16_t port = (uint16_t)reg->Address; + if (reg->WidthBytes <= 1u) return __inbyte(port); + if (reg->WidthBytes == 2u) return __inword(port); + return __indword(port); + } + + volatile void* address = HalPhysicalToVirtual(reg->Address); + if (reg->WidthBytes <= 1u) return *(volatile uint8_t*)address; + if (reg->WidthBytes == 2u) return *(volatile uint16_t*)address; + return *(volatile uint32_t*)address; +} + +// Writes one ACPI fixed register using the width advertised by firmware. +static void AcpiWriteFixedRegister(const ACPI_FIXED_REGISTER* reg, uint32_t value) { + if (!reg || !reg->Address) return; + + if (reg->AddressSpaceId == 0x01) { + uint16_t port = (uint16_t)reg->Address; + if (reg->WidthBytes <= 1u) __outbyte(port, (uint8_t)value); + else if (reg->WidthBytes == 2u) __outword(port, (uint16_t)value); + else __outdword(port, value); + return; + } + + volatile void* address = HalPhysicalToVirtual(reg->Address); + if (reg->WidthBytes <= 1u) *(volatile uint8_t*)address = (uint8_t)value; + else if (reg->WidthBytes == 2u) *(volatile uint16_t*)address = (uint16_t)value; + else *(volatile uint32_t*)address = value; +} + +// Detects whether PM1 control is using the standard Intel PCH PMBASE layout. +static void AcpiDetectIntelPchPmBase(void) { + gIntelPchPmBase = 0; + if (!gApi.Pci.Read32 || gPm1aCnt.AddressSpaceId != 0x01 || + gPm1aCnt.Address > 0xFFFFu || (gPm1aCnt.Address & 0x7Fu) != 0x04u) { + return; + } + + uint32_t id = gApi.Pci.Read32(0, 0, 31, 0, 0x00u); + uint32_t classRevision = gApi.Pci.Read32(0, 0, 31, 0, 0x08u); + if ((id & 0xFFFFu) == 0x8086u && ((classRevision >> 16) & 0xFFFFu) == 0x0601u) + gIntelPchPmBase = 1; +} + +// Disables Intel PCH sleep trapping so SLP_EN reaches the hardware sleep transition. +static void AcpiDisableIntelSleepSmiTrap(void) { + if (!gIntelPchPmBase) return; + + uint64_t pmBase = gPm1aCnt.Address - 4u; + if (pmBase > 0xFFFFu - INTEL_PCH_SMI_EN_OFFSET - 3u) return; + + uint16_t smiEnablePort = (uint16_t)(pmBase + INTEL_PCH_SMI_EN_OFFSET); + uint32_t smiEnable = __indword(smiEnablePort); + if (!(smiEnable & INTEL_PCH_SMI_ON_SLP_EN)) return; + + __outdword(smiEnablePort, smiEnable & ~INTEL_PCH_SMI_ON_SLP_EN); + if (__indword(smiEnablePort) & INTEL_PCH_SMI_ON_SLP_EN) + HalDbgPrint("HAL: Intel PCH refused to clear SMI_ON_SLP_EN.\n"); +} + +// Requests ACPI mode when firmware left fixed power registers in legacy mode. +static KSTATUS AcpiEnableModeForShutdown(void) { + if (!gPm1aCnt.Address) return NTLSTATUS_NOT_SUPPORTED; + if (AcpiReadFixedRegister(&gPm1aCnt) & 0x0001u) return NTLSTATUS_SUCCESS; + if (!gSmiCommandPort || !gAcpiEnableValue || gSmiCommandPort > 0xFFFFu) + return NTLSTATUS_NOT_SUPPORTED; + + __outbyte((uint16_t)gSmiCommandPort, gAcpiEnableValue); + uint64_t startTick = gApi.Timer.QueryTicks ? gApi.Timer.QueryTicks() : 0; + uint64_t deadline = startTick > UINT64_MAX - 3000000ull ? UINT64_MAX : startTick + 3000000ull; + uint32_t fallbackWaits = 0; + for (;;) { + if (AcpiReadFixedRegister(&gPm1aCnt) & 0x0001u) return NTLSTATUS_SUCCESS; + if (gApi.Timer.QueryTicks && gApi.Timer.QueryTicks() >= deadline) return NTLSTATUS_TIMEOUT; + if (++fallbackWaits >= 3000u) return NTLSTATUS_TIMEOUT; + if (gApi.Timer.Stall) gApi.Timer.Stall(1000u); + else _mm_pause(); + } } static int AmlParsePkgLength(const uint8_t* p, const uint8_t* end, @@ -302,7 +438,7 @@ static int AmlReadInteger(const uint8_t** pp, const uint8_t* end, uint8_t* out) // Scan DSDT AML for the _S5_ Name object and extract SLP_TYP values. Real // firmware commonly encodes the name as "\\_S5_" instead of just "_S5_", and // package integers may be ZeroOp/OneOp/Byte/Word/DWord/QWord constants. -static void ScanDsdtForS5(const uint8_t* aml, uint32_t len) { +static int ScanAmlForS5(const uint8_t* aml, uint32_t len, const char* source) { const uint8_t* end = aml + len; for (const uint8_t* cur = aml; cur < end; cur++) { @@ -329,12 +465,12 @@ static void ScanDsdtForS5(const uint8_t* aml, uint32_t len) { gSlpTypB = typB & 0x07; gS5Found = 1; - HalDbgPrint("HAL: _S5_ SLP_TYP_A=0x%02x SLP_TYP_B=0x%02x\n", - gSlpTypA, gSlpTypB); - return; + HalDbgPrint("HAL: _S5_ source=%s SLP_TYP_A=0x%02x SLP_TYP_B=0x%02x\n", + source ? source : "AML", gSlpTypA, gSlpTypB); + return 1; } - HalDbgPrint("HAL: _S5_ not found in DSDT; ACPI shutdown disabled\n"); + return 0; } // Validates checksum. @@ -352,13 +488,50 @@ static void ParseFadt(const ACPISDTHeader* header) { const void* fadt = header; uint32_t len = header->Length; - gPm1aCntBlk = (uint16_t)AcpiReadU32(fadt, len, FADT_PM1A_CNT_BLK_OFFSET); - gPm1bCntBlk = (uint16_t)AcpiReadU32(fadt, len, FADT_PM1B_CNT_BLK_OFFSET); + memset(&gPm1aCnt, 0, sizeof(gPm1aCnt)); + memset(&gPm1bCnt, 0, sizeof(gPm1bCnt)); + memset(&gSleepControl, 0, sizeof(gSleepControl)); + gLegacyPm1aPort = 0; + gLegacyPm1bPort = 0; + gFadtFlags = AcpiReadU32(fadt, len, FADT_FLAGS_OFFSET); + gHwReducedAcpi = (gFadtFlags & FADT_FLAG_HW_REDUCED_ACPI) != 0; - if (!gPm1aCntBlk) - gPm1aCntBlk = AcpiReadGasIoPort(fadt, len, FADT_X_PM1A_CNT_BLK_OFFSET); - if (!gPm1bCntBlk) - gPm1bCntBlk = AcpiReadGasIoPort(fadt, len, FADT_X_PM1B_CNT_BLK_OFFSET); + uint8_t pm1CntLength = FADT_PM1_CNT_LEN_OFFSET < len + ? *((const uint8_t*)fadt + FADT_PM1_CNT_LEN_OFFSET) + : 2u; + if (pm1CntLength < 2u) pm1CntLength = 2u; + if (pm1CntLength > 4u) pm1CntLength = 4u; + + uint32_t legacyPm1a = AcpiReadU32(fadt, len, FADT_PM1A_CNT_BLK_OFFSET); + uint32_t legacyPm1b = AcpiReadU32(fadt, len, FADT_PM1B_CNT_BLK_OFFSET); + gLegacyPm1aPort = legacyPm1a <= 0xFFFFu ? legacyPm1a : 0u; + gLegacyPm1bPort = legacyPm1b <= 0xFFFFu ? legacyPm1b : 0u; + if (legacyPm1a && legacyPm1a <= 0xFFFFu) { + gPm1aCnt.AddressSpaceId = 0x01; + gPm1aCnt.WidthBytes = pm1CntLength; + gPm1aCnt.Address = legacyPm1a; + } + if (legacyPm1b && legacyPm1b <= 0xFFFFu) { + gPm1bCnt.AddressSpaceId = 0x01; + gPm1bCnt.WidthBytes = pm1CntLength; + gPm1bCnt.Address = legacyPm1b; + } + + // ACPI 2.0+ GAS fields supersede the legacy blocks when firmware supplies them. + ACPI_FIXED_REGISTER extended; + memset(&extended, 0, sizeof(extended)); + if (AcpiReadGasRegister(fadt, len, FADT_X_PM1A_CNT_BLK_OFFSET, &extended)) + gPm1aCnt = extended; + memset(&extended, 0, sizeof(extended)); + if (AcpiReadGasRegister(fadt, len, FADT_X_PM1B_CNT_BLK_OFFSET, &extended)) + gPm1bCnt = extended; + if (gHwReducedAcpi) + (void)AcpiReadByteGasRegister(fadt, len, FADT_SLEEP_CONTROL_OFFSET, &gSleepControl); + + gSmiCommandPort = AcpiReadU32(fadt, len, FADT_SMI_CMD_OFFSET); + gAcpiEnableValue = FADT_ACPI_ENABLE_OFFSET < len + ? *((const uint8_t*)fadt + FADT_ACPI_ENABLE_OFFSET) + : 0u; uint64_t dsdtPhys = AcpiReadU64(fadt, len, FADT_X_DSDT_OFFSET); if (!dsdtPhys) @@ -369,8 +542,14 @@ static void ParseFadt(const ACPISDTHeader* header) { centuryRegister = *((const uint8_t*)fadt + FADT_CENTURY_OFFSET); HalRtcSetCenturyRegister(centuryRegister); - HalDbgPrint("HAL: FADT PM1a_CNT=0x%04x PM1b_CNT=0x%04x DSDT=0x%016llx RTC_CENTURY=0x%02x\n", - gPm1aCntBlk, gPm1bCntBlk, dsdtPhys, centuryRegister); + HalDbgPrint("HAL: FADT flags=0x%08x hw_reduced=%u PM1a_CNT=%u:0x%016llx legacy=0x%04x " + "PM1b_CNT=%u:0x%016llx legacy=0x%04x SLEEP_CTL=%u:0x%016llx " + "SMI=0x%08x DSDT=0x%016llx RTC_CENTURY=0x%02x\n", + gFadtFlags, gHwReducedAcpi ? 1u : 0u, + gPm1aCnt.AddressSpaceId, gPm1aCnt.Address, gLegacyPm1aPort, + gPm1bCnt.AddressSpaceId, gPm1bCnt.Address, gLegacyPm1bPort, + gSleepControl.AddressSpaceId, gSleepControl.Address, + gSmiCommandPort, dsdtPhys, centuryRegister); if (dsdtPhys) { const ACPISDTHeader* dsdt = (const ACPISDTHeader*)(dsdtPhys + gPhysBase); @@ -378,7 +557,7 @@ static void ParseFadt(const ACPISDTHeader* header) { // Validates checksum. validate_checksum((void*)dsdt, dsdt->Length)) { const uint8_t* aml = (const uint8_t*)(dsdt + 1); - ScanDsdtForS5(aml, dsdt->Length - (uint32_t)sizeof(ACPISDTHeader)); + (void)ScanAmlForS5(aml, dsdt->Length - (uint32_t)sizeof(ACPISDTHeader), "DSDT"); } else { HalDbgPrint("HAL: DSDT invalid or checksum failed\n"); } @@ -514,28 +693,106 @@ static void WalkAcpi(uintptr_t rsdpPhys) { // HPET base address block is located at offset 44 // (which is 8 bytes past the 36-byte ACPISDTHeader). gHpetPhys = *(uint64_t*)((uint8_t*)header + 44) & ~0xFFFULL; + } else if (memcmp(header->Signature, "SSDT", 4) == 0 && !gS5Found && + header->Length >= sizeof(ACPISDTHeader)) { + const uint8_t* aml = (const uint8_t*)(header + 1); + (void)ScanAmlForS5(aml, header->Length - (uint32_t)sizeof(ACPISDTHeader), "SSDT"); } } + + if (!gS5Found) + HalDbgPrint("HAL: _S5_ not found in DSDT or SSDT AML; ACPI S5 unavailable\n"); } // HalShutdown - ACPI S5 soft power-off. Does not return. static void HalShutdown(void) { - // Refuse further maskable interrupts before entering firmware power state. - (void)HalDisableInterrupts(); - - if (!gS5Found || !gPm1aCntBlk) { - // Refuse to guess. Guessing SLP_TYP is exactly how real hardware gets - // put into S3/S4 sleep instead of S5 soft-off. + if (!gS5Found) { + HalDbgPrint("HAL: shutdown unsupported: firmware _S5_ object was not resolved.\n"); HalStopProcessor(); } - // SLP_EN (bit 13) | SLP_TYPx (bits 12:10). - uint16_t valA = (uint16_t)(((uint16_t)gSlpTypA << 10) | (1 << 13)); - uint16_t valB = (uint16_t)(((uint16_t)gSlpTypB << 10) | (1 << 13)); + if (gHwReducedAcpi) { + if (!gSleepControl.Address) { + HalDbgPrint("HAL: shutdown unsupported: HW-reduced ACPI has no Sleep Control register.\n"); + HalStopProcessor(); + } - __outword(gPm1aCntBlk, valA); - if (gPm1bCntBlk) __outword(gPm1bCntBlk, valB); + uint32_t sleepValue = ((uint32_t)gSlpTypA << 2) | (1u << 5); + (void)HalDisableInterrupts(); + AcpiWriteFixedRegister(&gSleepControl, sleepValue); + } else { + if (!gPm1aCnt.Address) { + HalDbgPrint("HAL: shutdown unsupported: PM1a control register is unavailable.\n"); + HalStopProcessor(); + } + // Some Intel PCH firmware leaves SMI_ON_SLP_EN armed after boot. When set, + // the chipset traps SLP_EN into SMM and explicitly suppresses the sleep transition. + AcpiDisableIntelSleepSmiTrap(); + + // Try the fixed-register S5 request before changing ACPI ownership. A late + // SMI_CMD transition can itself enter firmware SMM on machines that boot with SCI_EN clear. + uint32_t valA = AcpiReadFixedRegister(&gPm1aCnt); + valA &= ~(0x7u << 10); + valA |= ((uint32_t)gSlpTypA << 10) | (1u << 13); + + uint32_t valB = 0u; + if (gPm1bCnt.Address) { + valB = AcpiReadFixedRegister(&gPm1bCnt); + valB &= ~(0x7u << 10); + valB |= ((uint32_t)gSlpTypB << 10) | (1u << 13); + } + + uintptr_t interruptFlags = HalDisableInterrupts(); + AcpiWriteFixedRegister(&gPm1aCnt, valA); + if (gPm1bCnt.Address) + AcpiWriteFixedRegister(&gPm1bCnt, valB); + + if (gApi.Timer.Stall) gApi.Timer.Stall(50000u); + + // If the direct request returned, fall back to the firmware ownership path. + HalRestoreInterrupts(interruptFlags); + (void)AcpiEnableModeForShutdown(); + AcpiDisableIntelSleepSmiTrap(); + + valA = AcpiReadFixedRegister(&gPm1aCnt); + valA &= ~(0x7u << 10); + valA |= ((uint32_t)gSlpTypA << 10) | (1u << 13); + if (gPm1bCnt.Address) { + valB = AcpiReadFixedRegister(&gPm1bCnt); + valB &= ~(0x7u << 10); + valB |= ((uint32_t)gSlpTypB << 10) | (1u << 13); + } + + (void)HalDisableInterrupts(); + AcpiWriteFixedRegister(&gPm1aCnt, valA); + if (gPm1bCnt.Address) + AcpiWriteFixedRegister(&gPm1bCnt, valB); + + if (gApi.Timer.Stall) gApi.Timer.Stall(50000u); + + bool selectedPm1aIsLegacy = gPm1aCnt.AddressSpaceId == 0x01 && + gPm1aCnt.Address == gLegacyPm1aPort; + if (gLegacyPm1aPort && !selectedPm1aIsLegacy) { + uint16_t legacyA = __inword((uint16_t)gLegacyPm1aPort); + legacyA = (uint16_t)((legacyA & ~(0x7u << 10)) | + ((uint16_t)gSlpTypA << 10) | (1u << 13)); + uint16_t legacyB = 0u; + if (gLegacyPm1bPort) { + legacyB = __inword((uint16_t)gLegacyPm1bPort); + legacyB = (uint16_t)((legacyB & ~(0x7u << 10)) | + ((uint16_t)gSlpTypB << 10) | (1u << 13)); + } + + AcpiDisableIntelSleepSmiTrap(); + __outword((uint16_t)gLegacyPm1aPort, legacyA); + if (gLegacyPm1bPort) + __outword((uint16_t)gLegacyPm1bPort, legacyB); + } + } + + if (gApi.Timer.Stall) gApi.Timer.Stall(100000u); + HalDbgPrint("HAL: shutdown failed: firmware returned from S5 request.\n"); HalStopProcessor(); } @@ -616,8 +873,9 @@ HAL_ENTRY KSTATUS HalInitialize(const HAL_INIT_PARAMS* initParams) { HalRtcInitialize(&gApi.Rtc); HalPciInitialize(&gApi.Pci); - gApi.Power.Shutdown = HalShutdown; + gApi.Power.Shutdown = HalShutdown; + AcpiDetectIntelPchPmBase(); gInitialised = 1; // The 8042 can leave output bytes pending while the keyboard/mouse drivers diff --git a/code/kernel/hal/hal_serial.c b/code/kernel/hal/hal_serial.c index 2bd3b6b..1b814e4 100644 --- a/code/kernel/hal/hal_serial.c +++ b/code/kernel/hal/hal_serial.c @@ -6,13 +6,16 @@ #include #include #include +#include #define HAL_COM1_BASE 0x03F8u #define HAL_COM1_DATA_PORT (HAL_COM1_BASE + 0u) #define HAL_COM1_STATUS_PORT (HAL_COM1_BASE + 5u) #define HAL_COM1_SCRATCH_PORT (HAL_COM1_BASE + 7u) #define HAL_COM1_TX_READY 0x20u -#define HAL_SERIAL_TX_SPIN_LIMIT 1000000u +#define HAL_SERIAL_TX_TIMEOUT_US 2000u +#define HAL_SERIAL_TIMER_SPIN_CAP 65536u +#define HAL_SERIAL_EARLY_SPINS 4096u static volatile long gSerialWriteLock; static volatile long gSerialPresent; @@ -30,9 +33,25 @@ static bool HalSerialProbeCom1(void) { return first == 0x5Au && second == 0xA5u; } -// Waits for COM1 transmit readiness without allowing broken or removed hardware to stall the machine forever. +// Waits for COM1 transmit readiness with a real microsecond deadline once the HAL timer is live. static bool HalSerialWaitForTransmit(void) { - for (uint32_t spin = 0; spin < HAL_SERIAL_TX_SPIN_LIMIT; spin++) { + uint64_t start = HalTimerQueryTicks(); + if (start != 0) { + uint64_t deadline = start > UINT64_MAX - HAL_SERIAL_TX_TIMEOUT_US + ? UINT64_MAX + : start + HAL_SERIAL_TX_TIMEOUT_US; + for (uint32_t spin = 0; spin < HAL_SERIAL_TIMER_SPIN_CAP; ++spin) { + if ((__inbyte(HAL_COM1_STATUS_PORT) & HAL_COM1_TX_READY) != 0) + return true; + if (HalTimerQueryTicks() >= deadline) + return false; + _mm_pause(); + } + return false; + } + + // Serial is registered before timer calibration, so keep the early path small and finite. + for (uint32_t spin = 0; spin < HAL_SERIAL_EARLY_SPINS; ++spin) { if ((__inbyte(HAL_COM1_STATUS_PORT) & HAL_COM1_TX_READY) != 0) return true; _mm_pause(); diff --git a/code/kernel/hal/hal_timer.c b/code/kernel/hal/hal_timer.c index c56d66f..1d14186 100644 --- a/code/kernel/hal/hal_timer.c +++ b/code/kernel/hal/hal_timer.c @@ -42,7 +42,7 @@ static void TimerInterrupt(void) { } // Returns ticks. -static uint64_t QueryTicks(void) { +uint64_t HalTimerQueryTicks(void) { // Prefer HPET when present: it is already a monotonically increasing main // counter and avoids races between the LAPIC periodic tick and the current // countdown register. @@ -53,6 +53,9 @@ static uint64_t QueryTicks(void) { return seconds * 1000000ull + (rem * 1000000ull) / gHpetHz; } + // Before LAPIC calibration there is no safe fallback clock to sample. + if (!gLapic || !gApicTicksPerQuantum) return 0; + // Fallback: combine the coarse periodic tick with the current LAPIC // countdown. Read the coarse tick twice so an interrupt between gTicks and // LAPIC_TIMER_CUR cannot make time jump backwards by almost one quantum. @@ -226,7 +229,7 @@ KSTATUS HalTimerInitialize(uintptr_t lapicPhys, uintptr_t hpetPhys, HAL_TIMER* o // Leave the BSP timer masked until the kernel registers its scheduler // callback and IRQ route. APs arm their local timer after per-CPU setup. - out->QueryTicks = QueryTicks; + out->QueryTicks = HalTimerQueryTicks; out->QueryTickFrequency = QueryTickFrequency; out->Stall = HalTimerStall; out->HandleInterrupt = TimerInterrupt; diff --git a/code/kernel/include/kernel/hal/hal_timer.h b/code/kernel/include/kernel/hal/hal_timer.h index f8531fd..b5744c4 100644 --- a/code/kernel/include/kernel/hal/hal_timer.h +++ b/code/kernel/include/kernel/hal/hal_timer.h @@ -7,4 +7,5 @@ #include KSTATUS HalTimerInitialize(uintptr_t lapicPhys, uintptr_t hpetPhys, HAL_TIMER* out); +uint64_t HalTimerQueryTicks(void); void HalTimerStall(uint32_t microseconds); diff --git a/code/kernel/include/kernel/io_file.h b/code/kernel/include/kernel/io_file.h index af53b73..d94255f 100644 --- a/code/kernel/include/kernel/io_file.h +++ b/code/kernel/include/kernel/io_file.h @@ -8,6 +8,7 @@ #include struct PROCESS; +typedef struct SYS_FILE_LOCK_INFO SYS_FILE_LOCK_INFO; // Capability-relative opens rooted at an already-open directory. KSTATUS IoOpenFileAtDirectory(DIR_OBJECT* directory, SYS_STRING_VIEW path, FILE_OBJECT** out); @@ -50,6 +51,15 @@ __declspec(dllexport) KSTATUS IoCancelAsyncFileRequest(IO_ASYNC_FILE_REQUEST_OBJ __declspec(dllexport) KSTATUS IoQueryAsyncFileRequest(IO_ASYNC_FILE_REQUEST_OBJECT* request, IO_STATUS_BLOCK* iosbOut); +__declspec(dllexport) KSTATUS IoQueryFileLock(FILE_OBJECT* file, + struct PROCESS* owner, + uint32_t mode, + uint64_t offset, + uint64_t length, + SYS_FILE_LOCK_INFO* infoOut); +__declspec(dllexport) KSTATUS IoSetFileStatus(FILE_OBJECT* file, + uint32_t mask, + uint32_t flags); __declspec(dllexport) KSTATUS IoLockFile(FILE_OBJECT* file, struct PROCESS* owner, uint32_t mode, diff --git a/code/kernel/include/kernel/io_pipe.h b/code/kernel/include/kernel/io_pipe.h new file mode 100644 index 0000000..cb464a7 --- /dev/null +++ b/code/kernel/include/kernel/io_pipe.h @@ -0,0 +1,34 @@ +// Native anonymous pipe declarations. + +#pragma once + +#include +#include +#include +#include + +struct PROCESS; +typedef struct PROCESS PROCESS; +typedef struct IO_PIPE_ENDPOINT IO_PIPE_ENDPOINT; + +// Creates one shared byte-stream pipe with separate endpoint objects. +KSTATUS IoCreatePipe(IO_PIPE_ENDPOINT** readEndOut, IO_PIPE_ENDPOINT** writeEndOut); + +// Maps the pipe's shared data-plane pages into one process. +KSTATUS IoMapPipe(IO_PIPE_ENDPOINT* endpoint, PROCESS* process, void** sharedStateOut); + +// Refreshes kernel wait objects after a user-mode pipe state transition. +KSTATUS IoNotifyPipe(IO_PIPE_ENDPOINT* endpoint); + +// Queries readiness for poll-style events. +KSTATUS IoPollPipe(IO_PIPE_ENDPOINT* endpoint, uint32_t events, uint32_t* returnedEventsOut); + +// Returns the event used by generic wait syscalls for this endpoint. +EVENT* IoPipeWaitEvent(IO_PIPE_ENDPOINT* endpoint); + +// Publishes and withdraws one kernel waiter around a generic wait operation. +void IoPipeBeginWait(IO_PIPE_ENDPOINT* endpoint); +void IoPipeEndWait(IO_PIPE_ENDPOINT* endpoint); + +// Returns whether this endpoint is the read side of the pipe. +bool IoPipeIsReadEnd(IO_PIPE_ENDPOINT* endpoint); diff --git a/code/kernel/include/kernel/ke_display.h b/code/kernel/include/kernel/ke_display.h index c1b7897..294479b 100644 --- a/code/kernel/include/kernel/ke_display.h +++ b/code/kernel/include/kernel/ke_display.h @@ -17,6 +17,10 @@ #include #include +typedef struct KE_DISPLAY_MANAGER { + OBJECT Header; +} KE_DISPLAY_MANAGER; + typedef struct DISPLAY_CLOCK_OBJECT { OBJECT Header; EVENT* Event; @@ -28,6 +32,7 @@ typedef struct DISPLAY_CLOCK_OBJECT { } DISPLAY_CLOCK_OBJECT; void KeDisplayInitialize(const HAL_API* hal); +KSTATUS KeDisplayReferenceManager(KE_DISPLAY_MANAGER** managerOut); // Completes every console glyph queued before the call. KSTATUS KeDisplayFlushConsoleOutput(void); diff --git a/code/kernel/include/kernel/ke_input.h b/code/kernel/include/kernel/ke_input.h index ba14b33..47c83a9 100644 --- a/code/kernel/include/kernel/ke_input.h +++ b/code/kernel/include/kernel/ke_input.h @@ -11,6 +11,16 @@ #include typedef struct SYS_GLOBAL_INPUT_STATE SYS_GLOBAL_INPUT_STATE; +typedef struct SYS_INPUT_MANAGER_EVENT SYS_INPUT_MANAGER_EVENT; + +typedef struct KE_INPUT_MANAGER_EVENT { + uint32_t Type; + uint64_t Sequence; + union { + INPUT_KEY_EVENT Key; + INPUT_MOUSE_EVENT Mouse; + } Data; +} KE_INPUT_MANAGER_EVENT; typedef struct INPUT_KEY_QUEUE_ENTRY { INPUT_KEY Key; @@ -34,6 +44,12 @@ typedef struct KE_INPUT_CONTEXT { INPUT_MOUSE_EVENT MouseQueue[128]; volatile int MouseHead; volatile int MouseTail; + + KE_INPUT_MANAGER_EVENT ManagerQueue[256]; + volatile int ManagerHead; + volatile int ManagerTail; + volatile long long ManagerSequence; + EVENT* ManagerWaitEvent; volatile int MouseX; volatile int MouseY; volatile int MouseBoundsWidth; @@ -44,6 +60,10 @@ typedef struct KE_INPUT_CONTEXT { EVENT* WaitEvent; } KE_INPUT_CONTEXT; +typedef struct KE_INPUT_MANAGER { + OBJECT Header; +} KE_INPUT_MANAGER; + typedef struct INPUT_QUEUE { OBJECT Header; KE_INPUT_CONTEXT* Context; @@ -51,6 +71,8 @@ typedef struct INPUT_QUEUE { } INPUT_QUEUE; void KeInputInitialize(const HAL_API* hal); +KSTATUS KeInputReferenceManager(KE_INPUT_MANAGER** managerOut); +KSTATUS KeInputManagerReadEvent(KE_INPUT_MANAGER* manager, uint64_t timeoutMs, SYS_INPUT_MANAGER_EVENT* eventOut); __declspec(dllexport) KSTATUS KeInputRegisterDevice(const char* name, uint32_t flags, uint32_t* outDeviceId); __declspec(dllexport) void KeInputSubmitKey(const INPUT_KEY_EVENT* event); diff --git a/code/kernel/include/kernel/ke_surface.h b/code/kernel/include/kernel/ke_surface.h new file mode 100644 index 0000000..6dd8150 --- /dev/null +++ b/code/kernel/include/kernel/ke_surface.h @@ -0,0 +1,39 @@ +// Kernel surface declarations. Surfaces are capability-backed shared pixel storage, not display ownership. + +#pragma once + +#include +#include +#include +#include + +struct PROCESS; +struct EVENT; + +// Creates one surface storage object and returns its initial object reference. +KSTATUS KeSurfaceCreate(uint32_t width, uint32_t height, uint32_t format, uint32_t flags, + OBJECT** surfaceOut); + +// Maps one surface into the calling process's VAD namespace with attenuated page permissions. +KSTATUS KeSurfaceMap(struct PROCESS* process, OBJECT* surface, uint32_t flags, + void** baseOut, uint32_t* pitchBytesOut); + +// Removes one mapping only when the supplied VAD is a surface shared view owned by the process. +KSTATUS KeSurfaceUnmap(struct PROCESS* process, void* base); + +// Snapshots one surface's storage and commit metadata. +KSTATUS KeSurfaceQuery(OBJECT* surface, SYS_SURFACE_DESC* descOut); + +// Announces changed pixels without touching physical scanout. +KSTATUS KeSurfaceCommit(OBJECT* surface, const DISPLAY_RECT* rects, uint32_t rectCount, + uint64_t* generationOut); + +// Presents one surface after a caller has separately proved DisplayManager PRESENT authority. +KSTATUS KeSurfacePresent(OBJECT* surface, const DISPLAY_RECT* rects, uint32_t rectCount, + uint64_t* bytesCopiedOut); + +// Returns the passive event used to wait for commit-generation changes. +struct EVENT* KeSurfaceWaitEvent(OBJECT* surface); + +// Snapshots global surface telemetry. +void KeSurfaceQueryStats(SYS_SURFACE_STATS* statsOut); diff --git a/code/kernel/include/kernel/ke_thread.h b/code/kernel/include/kernel/ke_thread.h index 8b15d04..4271c73 100644 --- a/code/kernel/include/kernel/ke_thread.h +++ b/code/kernel/include/kernel/ke_thread.h @@ -108,6 +108,11 @@ struct THREAD { void* FpuContext; uint8_t FpuStorage[528]; + // Mutable diagnostic description. This is thread-owned state rather than + // process metadata because independent worker threads may carry distinct roles. + SPINLOCK NameLock; + char Name[NTL_THREAD_NAME_MAX]; + // Scheduler accounting. QuantaConsumed is kept in the assembly-stable // portion of THREAD for layout compatibility; the dynamic scheduler uses // these fields for per-dispatch budget and CPU-burst tracking. @@ -188,6 +193,11 @@ struct THREAD { // process memory. Process termination must wait until that ownership has // returned instead of freeing the thread underneath an active driver. volatile long TerminationDeferralCount; + + // Set once terminal thread teardown begins. Interrupts may continue to + // run, but interrupt-return scheduling must not abandon this kernel stack + // before KeFinalizeThreadExit and process-exit publication complete. + volatile long ExitInProgress; }; // Creates and queues a user thread. On success, *out receives a referenced @@ -251,6 +261,7 @@ void KiWakeThreadForUserApc(THREAD* thread); void KeEnterTerminationDeferral(void); void KeLeaveTerminationDeferral(void); int KeThreadTerminationDeferred(const THREAD* thread); +int KeThreadExitInProgress(const THREAD* thread); // Assembly stub - implemented in ke_switch.asm. void KeSwitchContext(THREAD* from, THREAD* to); @@ -258,4 +269,6 @@ __declspec(noreturn) void KeStartInitialThread(THREAD* thread); void KeInitThreadFpuContext(THREAD* thread); void KeSetThreadAffinityMask(THREAD* thread, uint64_t affinityMask); +KSTATUS KeSetThreadName(THREAD* thread, const char* name, size_t length); +size_t KeQueryThreadName(THREAD* thread, char* buffer, size_t capacity); uint64_t KeGetDefaultThreadAffinityMask(void); diff --git a/code/kernel/include/kernel/ke_userenv.h b/code/kernel/include/kernel/ke_userenv.h index 8479f2f..c1854ed 100644 --- a/code/kernel/include/kernel/ke_userenv.h +++ b/code/kernel/include/kernel/ke_userenv.h @@ -20,6 +20,9 @@ #define SYS_STARTUP_CAP_SERVICE_CONTROL 2u #define SYS_STARTUP_CAP_SYSTEM_VOLUME_NAMESPACE 3u #define SYS_STARTUP_CAP_VOLUME_MANAGER 4u +#define SYS_STARTUP_CAP_INPUT_MANAGER 5u +#define SYS_STARTUP_CAP_DISPLAY_MANAGER 6u +#define SYS_STARTUP_CAP_SESSION_CONTROL 7u #define SYS_STARTUP_CAPABILITY_MAX 32u typedef struct SYS_STARTUP_CAPABILITY { diff --git a/code/kernel/include/kernel/mm_virt.h b/code/kernel/include/kernel/mm_virt.h index 737bbec..73a482f 100644 --- a/code/kernel/include/kernel/mm_virt.h +++ b/code/kernel/include/kernel/mm_virt.h @@ -34,6 +34,8 @@ typedef enum MM_VAD_TYPE { #define MM_VAD_ZERO 0x00000004u #define MM_VAD_PREFETCH 0x00000008u #define MM_VAD_SHARED_WRITE 0x00000010u +#define MM_VAD_SURFACE 0x00000020u +#define MM_VAD_PIPE 0x00000040u // Virtual address space constants. #define USER_BASE 0x0000000000001000ULL diff --git a/code/kernel/include/kernel/syscall_nr.h b/code/kernel/include/kernel/syscall_nr.h index 4f97310..bcb500c 100644 --- a/code/kernel/include/kernel/syscall_nr.h +++ b/code/kernel/include/kernel/syscall_nr.h @@ -25,164 +25,182 @@ #define SYS_GET_CURRENT_TID 14 #define SYS_GET_CURRENT_PID 15 #define SYS_SET_THREAD_PRIORITY 16 -#define SYS_YIELD 17 -#define SYS_SLEEP 18 -#define SYS_WAIT_SINGLE 19 -#define SYS_WAIT_MANY 20 -#define SYS_SET_USER_APC_DISPATCHER 21 -#define SYS_APC_RETURN 22 -#define SYS_SET_EXCEPTION_DISPATCHER 23 -#define SYS_GET_EXCEPTION_DISPATCHER 24 -#define SYS_CONTINUE_EXCEPTION 25 -#define SYS_RAISE_EXCEPTION 26 +#define SYS_SET_THREAD_NAME 17 +#define SYS_QUERY_THREAD_NAME 18 +#define SYS_YIELD 19 +#define SYS_SLEEP 20 +#define SYS_WAIT_SINGLE 21 +#define SYS_WAIT_MANY 22 +#define SYS_SET_USER_APC_DISPATCHER 23 +#define SYS_APC_RETURN 24 +#define SYS_SET_EXCEPTION_DISPATCHER 25 +#define SYS_GET_EXCEPTION_DISPATCHER 26 +#define SYS_CONTINUE_EXCEPTION 27 +#define SYS_RAISE_EXCEPTION 28 // Virtual memory, mappings, and handles. -#define SYS_ALLOC_VM 27 -#define SYS_FREE_VM 28 -#define SYS_PROTECT_VM 29 -#define SYS_QUERY_VM 30 -#define SYS_CLOSE_HANDLE 31 -#define SYS_DUPLICATE_HANDLE 32 -#define SYS_DERIVE_HANDLE 33 -#define SYS_REVOKE_HANDLE 34 -#define SYS_DERIVE_SECURITY_CONTEXT 35 -#define SYS_QUERY_SECURITY_CONTEXT 36 -#define SYS_CREATE_FILE_MAPPING 37 -#define SYS_MAP_FILE 38 +#define SYS_ALLOC_VM 29 +#define SYS_FREE_VM 30 +#define SYS_PROTECT_VM 31 +#define SYS_QUERY_VM 32 +#define SYS_CLOSE_HANDLE 33 +#define SYS_DUPLICATE_HANDLE 34 +#define SYS_DERIVE_HANDLE 35 +#define SYS_REVOKE_HANDLE 36 +#define SYS_DERIVE_SECURITY_CONTEXT 37 +#define SYS_QUERY_SECURITY_CONTEXT 38 +#define SYS_CREATE_FILE_MAPPING 39 +#define SYS_MAP_FILE 40 // Synchronization objects. -#define SYS_CREATE_MUTEX 39 -#define SYS_TRY_ACQUIRE_MUTEX 40 -#define SYS_RELEASE_MUTEX 41 -#define SYS_CREATE_SEM 42 -#define SYS_TRY_WAIT_SEM 43 -#define SYS_SIGNAL_SEM 44 -#define SYS_CREATE_EVENT 45 -#define SYS_SIGNAL_EVENT 46 -#define SYS_CLEAR_EVENT 47 +#define SYS_CREATE_MUTEX 41 +#define SYS_TRY_ACQUIRE_MUTEX 42 +#define SYS_RELEASE_MUTEX 43 +#define SYS_CREATE_SEM 44 +#define SYS_TRY_WAIT_SEM 45 +#define SYS_SIGNAL_SEM 46 +#define SYS_CREATE_EVENT 47 +#define SYS_SIGNAL_EVENT 48 +#define SYS_CLEAR_EVENT 49 // Queues and named queues. -#define SYS_CREATE_QUEUE 48 -#define SYS_SEND_QUEUE 49 -#define SYS_RECV_QUEUE 50 -#define SYS_PUBLISH_QUEUE 51 -#define SYS_OPEN_NAMED_QUEUE 52 -#define SYS_OPEN_NAMED_QUEUE_OWNER_PROCESS 53 -#define SYS_UNPUBLISH_QUEUE 54 +#define SYS_CREATE_QUEUE 50 +#define SYS_SEND_QUEUE 51 +#define SYS_RECV_QUEUE 52 +#define SYS_PUBLISH_QUEUE 53 +#define SYS_OPEN_NAMED_QUEUE 54 +#define SYS_OPEN_NAMED_QUEUE_OWNER_PROCESS 55 +#define SYS_UNPUBLISH_QUEUE 56 // LPC-like Ports and well-known system services. -#define SYS_CREATE_PORT 55 -#define SYS_CONNECT_PORT 56 -#define SYS_ACCEPT_PORT 57 -#define SYS_SEND_PORT 58 -#define SYS_RECEIVE_PORT 59 -#define SYS_CALL_PORT 60 -#define SYS_REPLY_PORT 61 -#define SYS_DISCONNECT_PORT 62 -#define SYS_REGISTER_SYSTEM_SERVICE 63 -#define SYS_UNREGISTER_SYSTEM_SERVICE 64 -#define SYS_CONNECT_SYSTEM_SERVICE 65 +#define SYS_CREATE_PORT 57 +#define SYS_CONNECT_PORT 58 +#define SYS_ACCEPT_PORT 59 +#define SYS_SEND_PORT 60 +#define SYS_RECEIVE_PORT 61 +#define SYS_CALL_PORT 62 +#define SYS_REPLY_PORT 63 +#define SYS_DISCONNECT_PORT 64 +#define SYS_REGISTER_SYSTEM_SERVICE 65 +#define SYS_UNREGISTER_SYSTEM_SERVICE 66 +#define SYS_CONNECT_SYSTEM_SERVICE 67 // Mounted-volume manager control plane. -#define SYS_QUERY_VOLUMES 66 -#define SYS_OPEN_VOLUME 67 -#define SYS_WAIT_VOLUME_CHANGE 68 +#define SYS_QUERY_VOLUMES 68 +#define SYS_OPEN_VOLUME 69 +#define SYS_WAIT_VOLUME_CHANGE 70 // Files and directories. -#define SYS_OPEN_FILE_AT 69 -#define SYS_OPEN_DIR_AT 70 -#define SYS_STAT_AT 71 -#define SYS_DELETE_AT 72 -#define SYS_RENAME_AT 73 -#define SYS_CREATE_DIR_AT 74 -#define SYS_REMOVE_DIR_AT 75 -#define SYS_READ_FILE 76 -#define SYS_WRITE_FILE 77 -#define SYS_READ_FILE_ASYNC 78 -#define SYS_WRITE_FILE_ASYNC 79 -#define SYS_CANCEL_IO 80 -#define SYS_SEEK_FILE 81 -#define SYS_GET_FILE_POS 82 -#define SYS_GET_FILE_SIZE 83 -#define SYS_GET_FILE_STAT 84 -#define SYS_TRUNCATE_FILE 85 -#define SYS_FLUSH_FILE 86 -#define SYS_LOCK_FILE 87 -#define SYS_UNLOCK_FILE 88 -#define SYS_READ_DIR 89 +#define SYS_OPEN_FILE_AT 71 +#define SYS_OPEN_DIR_AT 72 +#define SYS_STAT_AT 73 +#define SYS_DELETE_AT 74 +#define SYS_RENAME_AT 75 +#define SYS_CREATE_DIR_AT 76 +#define SYS_REMOVE_DIR_AT 77 +#define SYS_READ_FILE 78 +#define SYS_WRITE_FILE 79 +#define SYS_READ_FILE_ASYNC 80 +#define SYS_WRITE_FILE_ASYNC 81 +#define SYS_CANCEL_IO 82 +#define SYS_SEEK_FILE 83 +#define SYS_GET_FILE_POS 84 +#define SYS_GET_FILE_SIZE 85 +#define SYS_GET_FILE_STAT 86 +#define SYS_TRUNCATE_FILE 87 +#define SYS_FLUSH_FILE 88 +#define SYS_LOCK_FILE 89 +#define SYS_UNLOCK_FILE 90 +#define SYS_QUERY_FILE_LOCK 91 +#define SYS_SET_FILE_STATUS 92 +#define SYS_CREATE_PIPE 93 +#define SYS_NOTIFY_PIPE 94 +#define SYS_READ_DIR 95 // Console, display, and surfaces. -#define SYS_WRITE_CONSOLE 90 -#define SYS_GET_CONSOLE_INFO 91 -#define SYS_SET_CONSOLE_CURSOR 92 -#define SYS_FILL_CONSOLE_CHARACTER 93 -#define SYS_FILL_CONSOLE_ATTRIBUTE 94 -#define SYS_PUT_SCREEN_CHAR 95 -#define SYS_CLEAR_SCREEN 96 -#define SYS_GET_SCREEN_SIZE 97 -#define SYS_QUERY_DISPLAY_INFO 98 -#define SYS_OPEN_DISPLAY_CLOCK 99 -#define SYS_CREATE_SURFACE 100 -#define SYS_MAP_SURFACE 101 -#define SYS_QUERY_SURFACE 102 -#define SYS_PRESENT_SURFACE 103 +#define SYS_WRITE_CONSOLE 96 +#define SYS_GET_CONSOLE_INFO 97 +#define SYS_SET_CONSOLE_CURSOR 98 +#define SYS_FILL_CONSOLE_CHARACTER 99 +#define SYS_FILL_CONSOLE_ATTRIBUTE 100 +#define SYS_PUT_SCREEN_CHAR 101 +#define SYS_CLEAR_SCREEN 102 +#define SYS_GET_SCREEN_SIZE 103 +#define SYS_QUERY_DISPLAY_INFO 104 +#define SYS_QUERY_DISPLAY_MANAGER_INFO 105 +#define SYS_PRESENT_DISPLAY_MANAGER_SURFACE 106 +#define SYS_SET_DISPLAY_MANAGER_CURSOR 107 +#define SYS_WRITE_DISPLAY_MANAGER_CONSOLE 108 +#define SYS_QUERY_DISPLAY_MANAGER_CONSOLE_INFO 109 +#define SYS_SET_DISPLAY_MANAGER_CONSOLE_CURSOR 110 +#define SYS_FILL_DISPLAY_MANAGER_CONSOLE_CHARACTER 111 +#define SYS_FILL_DISPLAY_MANAGER_CONSOLE_ATTRIBUTE 112 +#define SYS_PUT_DISPLAY_MANAGER_CONSOLE_CHARACTER 113 +#define SYS_CLEAR_DISPLAY_MANAGER_CONSOLE 114 +#define SYS_OPEN_DISPLAY_CLOCK 115 +#define SYS_CREATE_SURFACE 116 +#define SYS_MAP_SURFACE 117 +#define SYS_UNMAP_SURFACE 118 +#define SYS_QUERY_SURFACE 119 +#define SYS_COMMIT_SURFACE 120 // Native render audio. -#define SYS_AUDIO_ENUMERATE_ENDPOINTS 104 -#define SYS_AUDIO_OPEN_STREAM 105 -#define SYS_AUDIO_WRITE 106 -#define SYS_AUDIO_ARM_WAIT 107 -#define SYS_AUDIO_QUERY_STREAM 108 -#define SYS_AUDIO_START 109 -#define SYS_AUDIO_PAUSE 110 -#define SYS_AUDIO_RESUME 111 -#define SYS_AUDIO_DRAIN 112 -#define SYS_AUDIO_STOP 113 -#define SYS_AUDIO_RESET 114 -#define SYS_AUDIO_SET_VOLUME 115 -#define SYS_AUDIO_SET_MASTER_VOLUME 116 +#define SYS_AUDIO_ENUMERATE_ENDPOINTS 121 +#define SYS_AUDIO_OPEN_STREAM 122 +#define SYS_AUDIO_WRITE 123 +#define SYS_AUDIO_ARM_WAIT 124 +#define SYS_AUDIO_QUERY_STREAM 125 +#define SYS_AUDIO_START 126 +#define SYS_AUDIO_PAUSE 127 +#define SYS_AUDIO_RESUME 128 +#define SYS_AUDIO_DRAIN 129 +#define SYS_AUDIO_STOP 130 +#define SYS_AUDIO_RESET 131 +#define SYS_AUDIO_SET_VOLUME 132 +#define SYS_AUDIO_SET_MASTER_VOLUME 133 // Input. -#define SYS_POLL_KEYBOARD 117 -#define SYS_IS_KEY_DOWN 118 -#define SYS_READ_KEYBOARD_CHAR 119 -#define SYS_READ_KEY 120 -#define SYS_GET_KEY_MODIFIERS 121 -#define SYS_QUERY_GLOBAL_INPUT_STATE 122 -#define SYS_OPEN_INPUT_QUEUE 123 -#define SYS_READ_INPUT_EVENT 124 -#define SYS_PEEK_INPUT_EVENT 125 -#define SYS_PEEK_MOUSE 126 -#define SYS_READ_MOUSE 127 -#define SYS_GET_MOUSE_POS 128 +#define SYS_READ_INPUT_MANAGER_EVENT 134 +#define SYS_POLL_KEYBOARD 135 +#define SYS_IS_KEY_DOWN 136 +#define SYS_READ_KEYBOARD_CHAR 137 +#define SYS_READ_KEY 138 +#define SYS_GET_KEY_MODIFIERS 139 +#define SYS_QUERY_GLOBAL_INPUT_STATE 140 +#define SYS_OPEN_INPUT_QUEUE 141 +#define SYS_READ_INPUT_EVENT 142 +#define SYS_PEEK_INPUT_EVENT 143 +#define SYS_PEEK_MOUSE 144 +#define SYS_READ_MOUSE 145 +#define SYS_GET_MOUSE_POS 146 // Time, debug, and system information. -#define SYS_CREATE_TIMER 129 -#define SYS_SET_TIMER 130 -#define SYS_CANCEL_TIMER 131 -#define SYS_GET_TICK 132 -#define SYS_GET_TICK_FREQUENCY 133 -#define SYS_GET_SYSTEM_TIME 134 -#define SYS_PRINT_DEBUG 135 -#define SYS_QUERY_TRAP_FRAME 136 -#define SYS_GET_MEMORY_STATUS 137 -#define SYS_QUERY_KERNEL_STATS 138 +#define SYS_CREATE_TIMER 147 +#define SYS_SET_TIMER 148 +#define SYS_CANCEL_TIMER 149 +#define SYS_GET_TICK 150 +#define SYS_GET_TICK_FREQUENCY 151 +#define SYS_GET_SYSTEM_TIME 152 +#define SYS_PRINT_DEBUG 153 +#define SYS_QUERY_TRAP_FRAME 154 +#define SYS_GET_MEMORY_STATUS 155 +#define SYS_QUERY_KERNEL_STATS 156 // Native sockets and polling. -#define SYS_CREATE_SOCKET 139 -#define SYS_SOCKET_IO_ASYNC 140 -#define SYS_BIND_SOCKET 141 -#define SYS_LISTEN_SOCKET 142 -#define SYS_ACCEPT_SOCKET 143 -#define SYS_CONNECT_SOCKET 144 -#define SYS_SEND_SOCKET 145 -#define SYS_RECV_SOCKET 146 -#define SYS_SHUTDOWN_SOCKET 147 -#define SYS_SEND_TO_SOCKET 148 -#define SYS_RECV_FROM_SOCKET 149 -#define SYS_RESOLVE_HOST 150 -#define SYS_SET_SOCKET_OPTION 151 -#define SYS_GET_SOCKET_OPTION 152 -#define SYS_POLL 153 +#define SYS_CREATE_SOCKET 157 +#define SYS_SOCKET_IO_ASYNC 158 +#define SYS_BIND_SOCKET 159 +#define SYS_LISTEN_SOCKET 160 +#define SYS_ACCEPT_SOCKET 161 +#define SYS_CONNECT_SOCKET 162 +#define SYS_SEND_SOCKET 163 +#define SYS_RECV_SOCKET 164 +#define SYS_SHUTDOWN_SOCKET 165 +#define SYS_SEND_TO_SOCKET 166 +#define SYS_RECV_FROM_SOCKET 167 +#define SYS_RESOLVE_HOST 168 +#define SYS_SET_SOCKET_OPTION 169 +#define SYS_GET_SOCKET_OPTION 170 +#define SYS_POLL 171 -#define SYS_MAX 154 +#define SYS_MAX 172 diff --git a/code/kernel/io_file.c b/code/kernel/io_file.c index f97d17a..27d2af1 100644 --- a/code/kernel/io_file.c +++ b/code/kernel/io_file.c @@ -44,6 +44,7 @@ typedef struct IO_FILE_LOCK_ENTRY { LIST_ENTRY Link; IO_FILE_IDENTITY Identity; PROCESS* Owner; + uint32_t OwnerProcessId; uint64_t Offset; uint64_t Length; uint32_t Mode; @@ -102,6 +103,41 @@ static bool KiIoFileLockRangesOverlap(uint64_t aOffset, uint64_t aLength, return aOffset < bEnd && bOffset < aEnd; } +// Converts an exclusive range end back to the public lock-length representation. +static uint64_t KiIoFileLockLengthFromEnd(uint64_t offset, uint64_t end) { + if (end == UINT64_MAX) + return SYS_FILE_LOCK_TO_EOF; + return end - offset; +} + +// Initializes one file-lock entry from an existing owner's identity. +static void KiIoInitializeFileLockEntry(IO_FILE_LOCK_ENTRY* entry, + const IO_FILE_IDENTITY* identity, + PROCESS* owner, + uint32_t mode, + uint64_t offset, + uint64_t length) { + memset(entry, 0, sizeof(*entry)); + LIST_INIT(&entry->Link); + entry->Identity = *identity; + entry->Owner = owner; + entry->OwnerProcessId = owner->ProcessId; + entry->Offset = offset; + entry->Length = length; + entry->Mode = mode; +} + +// Frees every detached file-lock entry linked on a private list. +static void KiIoFreeDetachedFileLocks(LIST_ENTRY* head) { + if (!head) + return; + while (head->Fwd != head) { + LIST_ENTRY* node = ListPopHead(head); + IO_FILE_LOCK_ENTRY* entry = CONTAINING_RECORD(node, IO_FILE_LOCK_ENTRY, Link); + MmFreePool(entry); + } +} + // Tests whether one existing lock conflicts with a requested lock. static bool KiIoFileLockConflicts(const IO_FILE_LOCK_ENTRY* lock, const IO_FILE_IDENTITY* identity, @@ -302,7 +338,8 @@ static KSTATUS KiIoEnsureAsyncFileIoStarted(void) { KSTATUS s = KeSemaphoreCreate(0, 0x7fffffff, &gAsyncFileSemaphore); if (KSUCCESS(s)) { THREAD* worker = (THREAD*)0; - const KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + const KE_THREAD_CREATE_PARAMETERS parameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "io.file.async"); s = KeCreateKernelThread((uintptr_t)KiIoAsyncFileWorker, 0, ¶meters, @@ -686,7 +723,7 @@ __declspec(dllexport) KSTATUS IoReadFileAt(FILE_OBJECT* file, uint64_t offset, size_t remaining = (size_t)(fileSize - offset); if (size > remaining) size = remaining; - if ((file->Flags & IO_FILE_FLAG_NO_CACHE) || size < 512) + if (file->Flags & IO_FILE_FLAG_NO_CACHE) return IoVolumeRead(file->Volume, file->FileCtx, offset, buf, size, bytesRead); size_t total = 0; @@ -881,6 +918,63 @@ __declspec(dllexport) KSTATUS IoQueryFileIdentity(FILE_OBJECT* file, IO_FILE_IDE return IoVolumeQueryIdentity(file->Volume, file->FileCtx, out); } +// Queries the first conflicting advisory lock held by another process. +__declspec(dllexport) KSTATUS IoQueryFileLock(FILE_OBJECT* file, PROCESS* owner, uint32_t mode, + uint64_t offset, uint64_t length, + SYS_FILE_LOCK_INFO* infoOut) { + if (!file || !owner || !infoOut) + return NTLSTATUS_INVALID_PARAMETER; + if (mode != SYS_FILE_LOCK_SHARED && mode != SYS_FILE_LOCK_EXCLUSIVE) + return NTLSTATUS_INVALID_PARAMETER; + if (length == 0) + return NTLSTATUS_INVALID_PARAMETER; + + IO_FILE_IDENTITY identity; + KSTATUS status = IoQueryFileIdentity(file, &identity); + if (!KSUCCESS(status)) + return status; + + KiIoEnsureFileLockState(); + memset(infoOut, 0, sizeof(*infoOut)); + infoOut->Size = sizeof(*infoOut); + infoOut->Mode = SYS_FILE_LOCK_NONE; + + uintptr_t flags = KeAcquireSpinlock(&gFileLockLock); + LIST_ENTRY* head = &gFileLocks; + for (LIST_ENTRY* node = head->Fwd; node != head; node = node->Fwd) { + IO_FILE_LOCK_ENTRY* lock = CONTAINING_RECORD(node, IO_FILE_LOCK_ENTRY, Link); + if (!KiIoFileLockConflicts(lock, &identity, owner, mode, offset, length)) + continue; + infoOut->Mode = lock->Mode; + infoOut->OwnerProcessId = lock->OwnerProcessId; + infoOut->Offset = lock->Offset; + infoOut->Length = lock->Length; + break; + } + KeReleaseSpinlock(&gFileLockLock, flags); + return NTLSTATUS_SUCCESS; +} + +// Changes mutable file-object status flags without changing handle authority. +__declspec(dllexport) KSTATUS IoSetFileStatus(FILE_OBJECT* file, uint32_t mask, uint32_t flags) { + if (!file) + return NTLSTATUS_INVALID_PARAMETER; + if (mask & ~SYS_FILE_STATUS_APPEND) + return NTLSTATUS_INVALID_PARAMETER; + if (flags & ~mask) + return NTLSTATUS_INVALID_PARAMETER; + + uintptr_t lockFlags = KeAcquireSpinlock(&file->Lock); + if (mask & SYS_FILE_STATUS_APPEND) { + if (flags & SYS_FILE_STATUS_APPEND) + file->Flags |= IO_FILE_FLAG_APPEND; + else + file->Flags &= ~IO_FILE_FLAG_APPEND; + } + KeReleaseSpinlock(&file->Lock, lockFlags); + return NTLSTATUS_SUCCESS; +} + // Acquires an advisory process-owned byte-range lock for one file identity. __declspec(dllexport) KSTATUS IoLockFile(FILE_OBJECT* file, PROCESS* owner, uint32_t mode, uint64_t offset, uint64_t length, @@ -905,27 +999,73 @@ __declspec(dllexport) KSTATUS IoLockFile(FILE_OBJECT* file, PROCESS* owner, uint memset(&waiter, 0, sizeof(waiter)); while (true) { - IO_FILE_LOCK_ENTRY* entry = (IO_FILE_LOCK_ENTRY*)MmAllocatePoolTag(sizeof(*entry), MM_POOL_TAG('f', 'l', 'c', 'k')); - if (!entry) { + IO_FILE_LOCK_ENTRY* entry = + (IO_FILE_LOCK_ENTRY*)MmAllocatePoolTag(sizeof(*entry), MM_POOL_TAG('f', 'l', 'c', 'k')); + IO_FILE_LOCK_ENTRY* split = + (IO_FILE_LOCK_ENTRY*)MmAllocatePoolTag(sizeof(*split), MM_POOL_TAG('f', 'l', 'c', 'k')); + if (!entry || !split) { + if (entry) MmFreePool(entry); + if (split) MmFreePool(split); if (waitEvent) ObDereferenceObject(&waitEvent->Header); return NTLSTATUS_NO_MEMORY; } + LIST_ENTRY detached; + LIST_INIT(&detached); + bool changedOwnedRange = false; + bool splitUsed = false; uintptr_t flags = KeAcquireSpinlock(&gFileLockLock); if (!KiIoFileLockHasConflictLocked(&identity, owner, mode, offset, length)) { - entry->Identity = identity; - entry->Owner = owner; - entry->Offset = offset; - entry->Length = length; - entry->Mode = mode; - LIST_INIT(&entry->Link); + uint64_t requestedEnd = KiIoFileLockEnd(offset, length); + LIST_ENTRY* head = &gFileLocks; + LIST_ENTRY* node = head->Fwd; + while (node != head) { + LIST_ENTRY* next = node->Fwd; + IO_FILE_LOCK_ENTRY* lock = CONTAINING_RECORD(node, IO_FILE_LOCK_ENTRY, Link); + if (lock->Owner != owner || !KiIoSameFileIdentity(&lock->Identity, &identity) || + !KiIoFileLockRangesOverlap(lock->Offset, lock->Length, offset, length)) { + node = next; + continue; + } + + uint64_t lockStart = lock->Offset; + uint64_t lockEnd = KiIoFileLockEnd(lock->Offset, lock->Length); + changedOwnedRange = true; + + if (lockStart < offset && requestedEnd < lockEnd) { + lock->Length = KiIoFileLockLengthFromEnd(lockStart, offset); + KiIoInitializeFileLockEntry(split, &identity, owner, lock->Mode, + requestedEnd, + KiIoFileLockLengthFromEnd(requestedEnd, lockEnd)); + ListInsertTail(&gFileLocks, &split->Link); + splitUsed = true; + } else if (lockStart < offset) { + lock->Length = KiIoFileLockLengthFromEnd(lockStart, offset); + } else if (requestedEnd < lockEnd) { + lock->Offset = requestedEnd; + lock->Length = KiIoFileLockLengthFromEnd(requestedEnd, lockEnd); + } else { + ListRemove(&lock->Link); + ListInsertTail(&detached, &lock->Link); + } + node = next; + } + + KiIoInitializeFileLockEntry(entry, &identity, owner, mode, offset, length); ListInsertTail(&gFileLocks, &entry->Link); + if (changedOwnedRange) + KiIoSignalFileLockWaitersLocked(&identity); KeReleaseSpinlock(&gFileLockLock, flags); + + if (!splitUsed) + MmFreePool(split); + KiIoFreeDetachedFileLocks(&detached); if (waitEvent) ObDereferenceObject(&waitEvent->Header); return NTLSTATUS_SUCCESS; } KeReleaseSpinlock(&gFileLockLock, flags); MmFreePool(entry); + MmFreePool(split); if (timeoutMilliseconds == 0) { if (waitEvent) ObDereferenceObject(&waitEvent->Header); @@ -971,7 +1111,7 @@ __declspec(dllexport) KSTATUS IoLockFile(FILE_OBJECT* file, PROCESS* owner, uint } } -// Releases this process's overlapping advisory locks for one file identity. +// Releases this process's requested advisory byte range while preserving outside fragments. __declspec(dllexport) KSTATUS IoUnlockFile(FILE_OBJECT* file, PROCESS* owner, uint64_t offset, uint64_t length) { if (!file || !owner || length == 0) return NTLSTATUS_INVALID_PARAMETER; @@ -981,27 +1121,61 @@ __declspec(dllexport) KSTATUS IoUnlockFile(FILE_OBJECT* file, PROCESS* owner, ui KiIoEnsureFileLockState(); - bool released = false; + IO_FILE_LOCK_ENTRY* split = + (IO_FILE_LOCK_ENTRY*)MmAllocatePoolTag(sizeof(*split), MM_POOL_TAG('f', 'l', 'c', 'k')); + LIST_ENTRY detached; + LIST_INIT(&detached); + bool changed = false; + bool splitUsed = false; + uint64_t requestedEnd = KiIoFileLockEnd(offset, length); + uintptr_t flags = KeAcquireSpinlock(&gFileLockLock); LIST_ENTRY* head = &gFileLocks; LIST_ENTRY* node = head->Fwd; while (node != head) { LIST_ENTRY* next = node->Fwd; IO_FILE_LOCK_ENTRY* lock = CONTAINING_RECORD(node, IO_FILE_LOCK_ENTRY, Link); - if (lock->Owner == owner && KiIoSameFileIdentity(&lock->Identity, &identity) && - KiIoFileLockRangesOverlap(lock->Offset, lock->Length, offset, length)) { - ListRemove(&lock->Link); - MmFreePool(lock); - released = true; + if (lock->Owner != owner || !KiIoSameFileIdentity(&lock->Identity, &identity) || + !KiIoFileLockRangesOverlap(lock->Offset, lock->Length, offset, length)) { + node = next; + continue; } + + uint64_t lockStart = lock->Offset; + uint64_t lockEnd = KiIoFileLockEnd(lock->Offset, lock->Length); + if (lockStart < offset && requestedEnd < lockEnd) { + if (!split) { + KeReleaseSpinlock(&gFileLockLock, flags); + KiIoFreeDetachedFileLocks(&detached); + return NTLSTATUS_NO_MEMORY; + } + lock->Length = KiIoFileLockLengthFromEnd(lockStart, offset); + KiIoInitializeFileLockEntry(split, &identity, owner, lock->Mode, + requestedEnd, + KiIoFileLockLengthFromEnd(requestedEnd, lockEnd)); + ListInsertTail(&gFileLocks, &split->Link); + splitUsed = true; + } else if (lockStart < offset) { + lock->Length = KiIoFileLockLengthFromEnd(lockStart, offset); + } else if (requestedEnd < lockEnd) { + lock->Offset = requestedEnd; + lock->Length = KiIoFileLockLengthFromEnd(requestedEnd, lockEnd); + } else { + ListRemove(&lock->Link); + ListInsertTail(&detached, &lock->Link); + } + changed = true; node = next; } - if (released) + if (changed) KiIoSignalFileLockWaitersLocked(&identity); KeReleaseSpinlock(&gFileLockLock, flags); - return released ? NTLSTATUS_SUCCESS : NTLSTATUS_NOT_FOUND; + if (split && !splitUsed) + MmFreePool(split); + KiIoFreeDetachedFileLocks(&detached); + return changed ? NTLSTATUS_SUCCESS : NTLSTATUS_NOT_FOUND; } // Releases every advisory file lock owned by one exiting process. diff --git a/code/kernel/io_pipe.c b/code/kernel/io_pipe.c new file mode 100644 index 0000000..8525dba --- /dev/null +++ b/code/kernel/io_pipe.c @@ -0,0 +1,321 @@ +// Kernel pipe object lifetime, shared backing, readiness, and wait integration. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define IO_PIPE_STATE_TAG MM_POOL_TAG('p', 'i', 'p', 's') +#define IO_PIPE_FRAME_TAG MM_POOL_TAG('p', 'i', 'p', 'f') + +typedef enum IO_PIPE_DIRECTION { + IoPipeReadEnd = 1, + IoPipeWriteEnd = 2, +} IO_PIPE_DIRECTION; + +typedef struct IO_PIPE_STATE { + volatile long RefCount; + SPINLOCK Lock; + EVENT* ReadEvent; + EVENT* WriteEvent; + PHYSICAL_ADDRESS* Frames; + size_t PageCount; + MM_KERNEL_VIEW* KernelView; + SYS_PIPE_SHARED_STATE* Shared; +} IO_PIPE_STATE; + +struct IO_PIPE_ENDPOINT { + OBJECT Header; + IO_PIPE_STATE* State; + IO_PIPE_DIRECTION Direction; +}; + +// Returns the current buffered-byte count when the shared counters are sane. +static bool KiPipeUsedBytes(const SYS_PIPE_SHARED_STATE* shared, uint64_t* usedOut) { + if (!shared || !usedOut || shared->Magic != SYS_PIPE_SHARED_MAGIC || + shared->Version != SYS_PIPE_SHARED_VERSION || + shared->Capacity != SYS_PIPE_BUFFER_CAPACITY || + shared->HeaderBytes != SYS_PIPE_SHARED_HEADER_BYTES) + return false; + + uint64_t readPosition = shared->ReadPosition; + uint64_t writePosition = shared->WritePosition; + uint64_t used = writePosition - readPosition; + if (used > SYS_PIPE_BUFFER_CAPACITY) + return false; + *usedOut = used; + return true; +} + +// Refreshes level-triggered endpoint events while the state lock is held. +static void KiPipeRefreshEventsLocked(IO_PIPE_STATE* state) { + if (!state || !state->Shared) + return; + + SYS_PIPE_SHARED_STATE* shared = state->Shared; + uint64_t used = 0; + if (!KiPipeUsedBytes(shared, &used)) { + KeSignalEvent(state->ReadEvent); + KeSignalEvent(state->WriteEvent); + return; + } + + if (used != 0 || shared->WriterOpen == 0) + KeSignalEvent(state->ReadEvent); + else + KeClearEvent(state->ReadEvent); + + uint64_t freeBytes = SYS_PIPE_BUFFER_CAPACITY - used; + if (shared->ReaderOpen == 0 || freeBytes >= SYS_PIPE_ATOMIC_WRITE_MAX) + KeSignalEvent(state->WriteEvent); + else + KeClearEvent(state->WriteEvent); +} + +// Releases shared pipe storage after both endpoint objects are gone. +static void KiPipeStateRelease(IO_PIPE_STATE* state) { + if (!state || _InterlockedDecrement(&state->RefCount) != 0) + return; + + if (state->KernelView) { + MmDestroyKernelView(state->KernelView); + state->KernelView = (MM_KERNEL_VIEW*)0; + state->Shared = (SYS_PIPE_SHARED_STATE*)0; + } + if (state->ReadEvent) + ObDereferenceObject(&state->ReadEvent->Header); + if (state->WriteEvent) + ObDereferenceObject(&state->WriteEvent->Header); + if (state->Frames) { + for (size_t i = 0; i < state->PageCount; ++i) + if (state->Frames[i]) + (void)MmPfnRelease(state->Frames[i]); + MmFreePool(state->Frames); + } + MmFreePool(state); +} + +// Destroys one endpoint and publishes final-reader/final-writer state. +static void KiPipeEndpointDestroy(OBJECT* object) { + if (!object) + return; + + IO_PIPE_ENDPOINT* endpoint = CONTAINING_RECORD(object, IO_PIPE_ENDPOINT, Header); + IO_PIPE_STATE* state = endpoint->State; + endpoint->State = (IO_PIPE_STATE*)0; + if (!state) + return; + + uintptr_t flags = KeAcquireSpinlock(&state->Lock); + if (state->Shared) { + if (endpoint->Direction == IoPipeReadEnd) + state->Shared->ReaderOpen = 0; + else + state->Shared->WriterOpen = 0; + _ReadWriteBarrier(); + KiPipeRefreshEventsLocked(state); + } + KeReleaseSpinlock(&state->Lock, flags); + KiPipeStateRelease(state); +} + +// Allocates one endpoint object for existing shared state. +static IO_PIPE_ENDPOINT* KiPipeCreateEndpoint(IO_PIPE_STATE* state, IO_PIPE_DIRECTION direction) { + IO_PIPE_ENDPOINT* endpoint = + (IO_PIPE_ENDPOINT*)ObAllocateObject(ObjectTypePipeEndpoint, sizeof(IO_PIPE_ENDPOINT)); + if (!endpoint) + return (IO_PIPE_ENDPOINT*)0; + endpoint->Header.Destroy = KiPipeEndpointDestroy; + endpoint->State = state; + endpoint->Direction = direction; + return endpoint; +} + +// Allocates the physical pages and stable kernel view for one pipe data plane. +static KSTATUS KiPipeCreateBacking(IO_PIPE_STATE* state) { + size_t payloadSize = sizeof(SYS_PIPE_SHARED_STATE); + size_t pageCount = (payloadSize + PAGE_SIZE - 1u) / PAGE_SIZE; + state->Frames = (PHYSICAL_ADDRESS*)MmAllocatePoolTag(pageCount * sizeof(PHYSICAL_ADDRESS), + IO_PIPE_FRAME_TAG); + if (!state->Frames) + return NTLSTATUS_NO_MEMORY; + memset(state->Frames, 0, pageCount * sizeof(PHYSICAL_ADDRESS)); + state->PageCount = pageCount; + + for (size_t i = 0; i < pageCount; ++i) { + PHYSICAL_ADDRESS frame = MmPfnAllocate(MmPfnUseAnonymous); + if (!frame) + return NTLSTATUS_NO_MEMORY; + state->Frames[i] = frame; + void* page = MmPfnToVirtual(frame); + if (page) + memset(page, 0, PAGE_SIZE); + } + + KSTATUS status = MmCreateKernelViewFromFrames(state->Frames, state->PageCount, + &state->KernelView); + if (!KSUCCESS(status)) + return status; + state->Shared = (SYS_PIPE_SHARED_STATE*)MmKernelViewBase(state->KernelView); + if (!state->Shared) + return NTLSTATUS_FAILURE; + + state->Shared->Magic = SYS_PIPE_SHARED_MAGIC; + state->Shared->Version = SYS_PIPE_SHARED_VERSION; + state->Shared->Capacity = SYS_PIPE_BUFFER_CAPACITY; + state->Shared->HeaderBytes = SYS_PIPE_SHARED_HEADER_BYTES; + state->Shared->ReaderOpen = 1; + state->Shared->WriterOpen = 1; + return NTLSTATUS_SUCCESS; +} + +// Creates one anonymous pipe with kernel-owned endpoints and a user-mapped data plane. +KSTATUS IoCreatePipe(IO_PIPE_ENDPOINT** readEndOut, IO_PIPE_ENDPOINT** writeEndOut) { + if (!readEndOut || !writeEndOut) + return NTLSTATUS_INVALID_PARAMETER; + *readEndOut = (IO_PIPE_ENDPOINT*)0; + *writeEndOut = (IO_PIPE_ENDPOINT*)0; + + IO_PIPE_STATE* state = (IO_PIPE_STATE*)MmAllocatePoolTag(sizeof(*state), IO_PIPE_STATE_TAG); + if (!state) + return NTLSTATUS_NO_MEMORY; + memset(state, 0, sizeof(*state)); + state->RefCount = 2; + state->Lock = (SPINLOCK)SPINLOCK_INIT; + + KSTATUS status = KeEventCreate(EventManualReset, 0, &state->ReadEvent); + if (KSUCCESS(status)) + status = KeEventCreate(EventManualReset, 1, &state->WriteEvent); + if (KSUCCESS(status)) + status = KiPipeCreateBacking(state); + if (!KSUCCESS(status)) { + state->RefCount = 1; + KiPipeStateRelease(state); + return status; + } + + IO_PIPE_ENDPOINT* readEnd = KiPipeCreateEndpoint(state, IoPipeReadEnd); + IO_PIPE_ENDPOINT* writeEnd = KiPipeCreateEndpoint(state, IoPipeWriteEnd); + if (!readEnd || !writeEnd) { + if (readEnd) + ObDereferenceObject(&readEnd->Header); + else + KiPipeStateRelease(state); + if (writeEnd) + ObDereferenceObject(&writeEnd->Header); + else + KiPipeStateRelease(state); + return NTLSTATUS_NO_MEMORY; + } + + *readEndOut = readEnd; + *writeEndOut = writeEnd; + return NTLSTATUS_SUCCESS; +} + +// Maps the shared pipe data plane into one process-owned VAD. +KSTATUS IoMapPipe(IO_PIPE_ENDPOINT* endpoint, PROCESS* process, void** sharedStateOut) { + if (!endpoint || !process || !sharedStateOut || !endpoint->State) + return NTLSTATUS_INVALID_PARAMETER; + *sharedStateOut = (void*)0; + + IO_PIPE_STATE* state = endpoint->State; + KSTATUS status = MmVadCreateSharedViewFromFrames(process, state->Frames, state->PageCount, + sizeof(SYS_PIPE_SHARED_STATE), + PAGE_PRESENT | PAGE_USER | PAGE_WRITE | PAGE_NX, + MM_VAD_PIPE, MmVadShared, sharedStateOut); + if (!KSUCCESS(status)) + return status; + + status = MmVadProtect(process, *sharedStateOut, SYS_PIPE_SHARED_HEADER_BYTES, + PAGE_PRESENT | PAGE_USER | PAGE_NX); + if (!KSUCCESS(status)) { + (void)MmVadDestroy(process, *sharedStateOut); + *sharedStateOut = (void*)0; + } + return status; +} + +// Refreshes readiness after a user-mode ring transition. +KSTATUS IoNotifyPipe(IO_PIPE_ENDPOINT* endpoint) { + if (!endpoint || !endpoint->State) + return NTLSTATUS_INVALID_PARAMETER; + IO_PIPE_STATE* state = endpoint->State; + uintptr_t flags = KeAcquireSpinlock(&state->Lock); + KiPipeRefreshEventsLocked(state); + KeReleaseSpinlock(&state->Lock, flags); + return NTLSTATUS_SUCCESS; +} + +// Queries readiness for poll-style events. +KSTATUS IoPollPipe(IO_PIPE_ENDPOINT* endpoint, uint32_t events, uint32_t* returnedEventsOut) { + if (!endpoint || !returnedEventsOut || !endpoint->State || !endpoint->State->Shared) + return NTLSTATUS_INVALID_PARAMETER; + + SYS_PIPE_SHARED_STATE* shared = endpoint->State->Shared; + uint64_t used = 0; + if (!KiPipeUsedBytes(shared, &used)) + return NTLSTATUS_FAILURE; + + uint32_t returned = 0; + if (endpoint->Direction == IoPipeReadEnd) { + if ((events & SYS_POLL_READ) && (used != 0 || shared->WriterOpen == 0)) + returned |= SYS_POLL_READ; + if (shared->WriterOpen == 0) + returned |= SYS_POLL_HANGUP; + } else { + if (shared->ReaderOpen == 0) { + returned |= SYS_POLL_ERROR | SYS_POLL_HANGUP; + } else if ((events & SYS_POLL_WRITE) && used < SYS_PIPE_BUFFER_CAPACITY) { + returned |= SYS_POLL_WRITE; + } + } + *returnedEventsOut = returned; + return NTLSTATUS_SUCCESS; +} + +// Returns the endpoint event used by generic waits. +EVENT* IoPipeWaitEvent(IO_PIPE_ENDPOINT* endpoint) { + if (!endpoint || !endpoint->State) + return (EVENT*)0; + return endpoint->Direction == IoPipeReadEnd + ? endpoint->State->ReadEvent + : endpoint->State->WriteEvent; +} + +// Publishes one kernel waiter before entering the generic event wait engine. +void IoPipeBeginWait(IO_PIPE_ENDPOINT* endpoint) { + if (!endpoint || !endpoint->State || !endpoint->State->Shared) + return; + SYS_PIPE_SHARED_STATE* shared = endpoint->State->Shared; + if (endpoint->Direction == IoPipeReadEnd) + _InterlockedIncrement(&shared->ReadWaiters); + else + _InterlockedIncrement(&shared->WriteWaiters); + + uintptr_t flags = KeAcquireSpinlock(&endpoint->State->Lock); + KiPipeRefreshEventsLocked(endpoint->State); + KeReleaseSpinlock(&endpoint->State->Lock, flags); +} + +// Withdraws one kernel waiter after the generic event wait completes. +void IoPipeEndWait(IO_PIPE_ENDPOINT* endpoint) { + if (!endpoint || !endpoint->State || !endpoint->State->Shared) + return; + SYS_PIPE_SHARED_STATE* shared = endpoint->State->Shared; + if (endpoint->Direction == IoPipeReadEnd) + _InterlockedDecrement(&shared->ReadWaiters); + else + _InterlockedDecrement(&shared->WriteWaiters); +} + +// Returns whether this endpoint is the read side of the pipe. +bool IoPipeIsReadEnd(IO_PIPE_ENDPOINT* endpoint) { + return endpoint && endpoint->Direction == IoPipeReadEnd; +} diff --git a/code/kernel/ke_apc.c b/code/kernel/ke_apc.c index 85014f2..ef3668e 100644 --- a/code/kernel/ke_apc.c +++ b/code/kernel/ke_apc.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -109,7 +110,7 @@ __declspec(dllexport) bool KeInsertQueueApc(KAPC* apc, return false; THREAD* thread = apc->Thread; - if (thread->State == ThreadDead) + if (thread->State == ThreadDead || KeThreadExitInProgress(thread)) return false; if (_InterlockedCompareExchange(&apc->Inserted, 1, 0) != 0) @@ -192,12 +193,23 @@ void KiDeliverKernelApcs(void) { if (!thread) return; - if (KeGetCurrentIrql() != PASSIVE_LEVEL) + KCPU* cpu = KeGetCurrentCpu(); + if (KeGetCurrentIrql() != PASSIVE_LEVEL || + (__readeflags() & 0x200u) == 0 || + !cpu || cpu->InterruptDepth != 0) return; if (thread->ApcDeliveryActive || thread->KernelApcDisable != 0 || !thread->KernelApcPending) return; + // A thread may already have marked itself waiting while it is still + // executing in the prepare-to-commit window. Delivering an APC there can + // consume the pending APC without completing an alertable wait, after + // which KeCommitBlockThread() would sleep forever. Wait commit owns APC + // interruption while the thread is logically waiting. + if (thread->State == ThreadWaiting) + return; + thread->ApcDeliveryActive = 1; while (true) { diff --git a/code/kernel/ke_audio.c b/code/kernel/ke_audio.c index d9ae652..2b78631 100644 --- a/code/kernel/ke_audio.c +++ b/code/kernel/ke_audio.c @@ -1397,7 +1397,7 @@ static KSTATUS KiAudioRegisterEndpoint(const IO_AUDIO_ENDPOINT_REGISTRATION* reg ObReferenceObject(&endpoint->Header); const KE_THREAD_CREATE_PARAMETERS mixerParameters = - KE_THREAD_CREATE_REALTIME(KE_AUDIO_MIXER_REALTIME_PRIORITY); + KE_THREAD_CREATE_NAMED_REALTIME(KE_AUDIO_MIXER_REALTIME_PRIORITY, "audio.mixer"); status = KeCreateKernelThread((uintptr_t)KiAudioMixerThread, (uintptr_t)endpoint, &mixerParameters, diff --git a/code/kernel/ke_display.c b/code/kernel/ke_display.c index 800b87e..5634868 100644 --- a/code/kernel/ke_display.c +++ b/code/kernel/ke_display.c @@ -65,6 +65,7 @@ typedef struct BOOTFB_CONTEXT { } BOOTFB_CONTEXT; static const HAL_API* gDisplayHal; +static KE_DISPLAY_MANAGER gDisplayManager; static DISPLAY_DEVICE* gDisplayList; static DISPLAY_DEVICE* gPrimaryDisplay; static SPINLOCK gDisplayLock = SPINLOCK_INIT; @@ -1236,8 +1237,17 @@ static void KiDisplayRegisterOutputEndpoint(void) { } } -// Implements the display Initialize operation. +// References the singleton display-manager authority object used for bootstrap delegation. +KSTATUS KeDisplayReferenceManager(KE_DISPLAY_MANAGER** managerOut) { + if (!managerOut) return NTLSTATUS_INVALID_PARAMETER; + *managerOut = &gDisplayManager; + ObReferenceObject(&gDisplayManager.Header); + return NTLSTATUS_SUCCESS; +} + +// Initializes the kernel display manager and bootstrap framebuffer provider. void KeDisplayInitialize(const HAL_API* hal) { + ObInitializeObjectHeader(&gDisplayManager.Header, ObjectTypeDisplayManager); gDisplayHal = hal; gDisplayList = NULL; gPrimaryDisplay = NULL; diff --git a/code/kernel/ke_exception.c b/code/kernel/ke_exception.c index c8dafb6..cfd3ba1 100644 --- a/code/kernel/ke_exception.c +++ b/code/kernel/ke_exception.c @@ -129,8 +129,10 @@ static KSTATUS KiExceptionUnhandledExitStatus(const SYS_EXCEPTION_RECORD* record return NTLSTATUS_PROCESS_PAGE_FAULT; case SYS_EXCEPTION_CODE_DATATYPE_MISALIGNMENT: return NTLSTATUS_PROCESS_ALIGNMENT_FAULT; + case SYS_EXCEPTION_CODE_USER_RAISED: + return NTLSTATUS_PROCESS_USER_EXCEPTION; default: - return (KSTATUS)record->Code; + return NTLSTATUS_PROCESS_UNKNOWN_FAULT; } } @@ -146,7 +148,7 @@ static uint32_t KiExceptionCodeFromVector(uint32_t vector) { case 13: return SYS_EXCEPTION_CODE_PRIVILEGED_INSTRUCTION; case 14: return SYS_EXCEPTION_CODE_ACCESS_VIOLATION; case 17: return SYS_EXCEPTION_CODE_DATATYPE_MISALIGNMENT; - default: return SYS_EXCEPTION_CODE_NTLITE_UNKNOWN; + default: return SYS_EXCEPTION_CODE_UNKNOWN; } } @@ -519,7 +521,8 @@ uint64_t KeRaiseUserException(const SYS_RAISE_EXCEPTION_REQUEST* request, const memset(&record, 0, sizeof(record)); record.Size = sizeof(record); record.Version = SYS_EXCEPTION_ABI_VERSION; - record.Code = request->Code; + record.Code = SYS_EXCEPTION_CODE_USER_RAISED; + record.UserCode = request->UserCode; record.Flags = SYS_EXCEPTION_FLAG_FIRST_CHANCE; if (request->Flags & SYS_RAISE_EXCEPTION_FLAG_NONCONTINUABLE) record.Flags |= SYS_EXCEPTION_FLAG_NONCONTINUABLE; diff --git a/code/kernel/ke_handle.c b/code/kernel/ke_handle.c index 347867c..e321c10 100644 --- a/code/kernel/ke_handle.c +++ b/code/kernel/ke_handle.c @@ -236,7 +236,8 @@ SYS_RIGHTS KeGetValidHandleRights(OBJECT_TYPE type) { SYS_PROCESS_RIGHT_TERMINATE | SYS_PROCESS_RIGHT_INSERT_HANDLE | SYS_PROCESS_RIGHT_BUILD; case ObjectTypeThread: - return management | SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_THREAD_RIGHT_RESUME; + return management | SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_THREAD_RIGHT_RESUME | + SYS_THREAD_RIGHT_SET_INFORMATION; case ObjectTypeMutex: return management | SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_MUTEX_RIGHT_RELEASE; case ObjectTypeSemaphore: @@ -261,10 +262,15 @@ SYS_RIGHTS KeGetValidHandleRights(OBJECT_TYPE type) { SYS_SOCKET_RIGHT_ACCEPT | SYS_SOCKET_RIGHT_SEND | SYS_SOCKET_RIGHT_RECEIVE | SYS_SOCKET_RIGHT_SHUTDOWN | SYS_SOCKET_RIGHT_POLL | SYS_SOCKET_RIGHT_SET_OPTION; + case ObjectTypePipeEndpoint: + return management | SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | + SYS_PIPE_RIGHT_READ | SYS_PIPE_RIGHT_WRITE | SYS_PIPE_RIGHT_POLL; case ObjectTypeFileMapping: return management | SYS_RIGHT_QUERY | SYS_FILE_MAPPING_RIGHT_MAP; case ObjectTypeSurface: - return management | SYS_RIGHT_QUERY | SYS_SURFACE_RIGHT_MAP | SYS_SURFACE_RIGHT_PRESENT; + return management | SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | + SYS_SURFACE_RIGHT_MAP_READ | SYS_SURFACE_RIGHT_MAP_WRITE | + SYS_SURFACE_RIGHT_COMMIT; case ObjectTypeInputQueue: return management | SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_INPUT_QUEUE_RIGHT_READ; case ObjectTypeTimer: @@ -289,6 +295,13 @@ SYS_RIGHTS KeGetValidHandleRights(OBJECT_TYPE type) { return management | SYS_RIGHT_QUERY | SYS_VOLUME_MANAGER_RIGHT_ENUMERATE | SYS_VOLUME_MANAGER_RIGHT_OPEN | SYS_VOLUME_MANAGER_RIGHT_WAIT; + case ObjectTypeInputManager: + return management | SYS_RIGHT_QUERY | SYS_INPUT_MANAGER_RIGHT_READ | + SYS_INPUT_MANAGER_RIGHT_CONFIGURE; + case ObjectTypeDisplayManager: + return management | SYS_RIGHT_QUERY | SYS_DISPLAY_MANAGER_RIGHT_ENUMERATE | + SYS_DISPLAY_MANAGER_RIGHT_MODESET | SYS_DISPLAY_MANAGER_RIGHT_PRESENT | + SYS_DISPLAY_MANAGER_RIGHT_CURSOR | SYS_DISPLAY_MANAGER_RIGHT_CONSOLE; case ObjectTypeDevice: case ObjectTypeDriver: case ObjectTypeUsbDevice: diff --git a/code/kernel/ke_input.c b/code/kernel/ke_input.c index 3ad352e..c4bd0b4 100644 --- a/code/kernel/ke_input.c +++ b/code/kernel/ke_input.c @@ -19,6 +19,8 @@ #define KEY_QUEUE_MASK (KEY_QUEUE_SIZE - 1u) #define MOUSE_QUEUE_SIZE 128u #define MOUSE_QUEUE_MASK (MOUSE_QUEUE_SIZE - 1u) +#define MANAGER_QUEUE_SIZE 256u +#define MANAGER_QUEUE_MASK (MANAGER_QUEUE_SIZE - 1u) #define MAX_INPUT_KEYS 256u #define KEY_REPEAT_DELAY_US 500000ull #define KEY_REPEAT_PERIOD_US 33000ull @@ -30,6 +32,7 @@ typedef struct KEY_CHAR_PAIR { } KEY_CHAR_PAIR; static KE_INPUT_CONTEXT gDefaultInputContext; +static KE_INPUT_MANAGER gInputManager; static KE_ID_ALLOCATOR gInputDeviceIds = KE_ID_ALLOCATOR_INIT(1u); static char KiInputKeyToChar(KE_INPUT_CONTEXT* context, INPUT_KEY key); @@ -64,6 +67,25 @@ static const KEY_CHAR_PAIR gCharMap[] = { { INPUT_KEY_KP_0, '0', '0' }, { INPUT_KEY_KP_PERIOD, '.', '.' }, }; +// Updates the waitable manager event to match queued physical input. +static void KiInputRefreshManagerEvent(KE_INPUT_CONTEXT* context) { + if (!context->ManagerWaitEvent) return; + if (context->ManagerHead != context->ManagerTail) KeSignalEvent(context->ManagerWaitEvent); + else KeClearEvent(context->ManagerWaitEvent); +} + +// Queues one normalized physical event for the delegated input manager. +static void KiInputQueueManagerEvent(KE_INPUT_CONTEXT* context, const KE_INPUT_MANAGER_EVENT* event) { + if (!context || !event) return; + int next = (context->ManagerTail + 1) & (int)MANAGER_QUEUE_MASK; + if (next == context->ManagerHead) return; + KE_INPUT_MANAGER_EVENT queued = *event; + queued.Sequence = (uint64_t)_InterlockedIncrement64(&context->ManagerSequence); + context->ManagerQueue[context->ManagerTail] = queued; + context->ManagerTail = next; + KiInputRefreshManagerEvent(context); +} + // Updates the waitable keyboard event to match queued keyboard input. static void KiInputRefreshKeyboardEvent(KE_INPUT_CONTEXT* context) { if (!context->WaitEvent) return; @@ -208,6 +230,7 @@ static char KiInputKeyToChar(KE_INPUT_CONTEXT* context, INPUT_KEY key) { // Implements the input Initialize operation. void KeInputInitialize(const HAL_API* hal) { KE_INPUT_CONTEXT* context = &gDefaultInputContext; + ObInitializeObjectHeader(&gInputManager.Header, ObjectTypeInputManager); context->Hal = hal; memset((void*)context->KeyDown, 0, sizeof(context->KeyDown)); context->Modifiers = 0; @@ -216,6 +239,8 @@ void KeInputInitialize(const HAL_API* hal) { context->RepeatKey = INPUT_KEY_NONE; context->RepeatNextTick = 0; context->MouseHead = context->MouseTail = 0; + context->ManagerHead = context->ManagerTail = 0; + context->ManagerSequence = 0; context->MouseBoundsWidth = 1920; context->MouseBoundsHeight = 1080; context->MouseWheel = 0; @@ -227,7 +252,13 @@ void KeInputInitialize(const HAL_API* hal) { if (!KSUCCESS(s)) DbgPrint("Input: keyboard wait event create failed st=0x%x\n", s); } + if (!context->ManagerWaitEvent) { + KSTATUS s = KeEventCreate(EventManualReset, 0, &context->ManagerWaitEvent); + if (!KSUCCESS(s)) + DbgPrint("Input: manager wait event create failed st=0x%x\n", s); + } KiInputRefreshKeyboardEvent(context); + KiInputRefreshManagerEvent(context); int w = 1920, h = 1080; DISPLAY_MODE mode; @@ -243,6 +274,48 @@ void KeInputInitialize(const HAL_API* hal) { DbgPrint("Input: kernel input core initialized (%dx%d)\n", w, h); } +// References the singleton input-manager authority object used for bootstrap delegation. +KSTATUS KeInputReferenceManager(KE_INPUT_MANAGER** managerOut) { + if (!managerOut) return NTLSTATUS_INVALID_PARAMETER; + *managerOut = &gInputManager; + ObReferenceObject(&gInputManager.Header); + return NTLSTATUS_SUCCESS; +} + +// Reads one normalized physical event from the singleton manager stream. +KSTATUS KeInputManagerReadEvent(KE_INPUT_MANAGER* manager, uint64_t timeoutMs, SYS_INPUT_MANAGER_EVENT* eventOut) { + KE_INPUT_CONTEXT* context = &gDefaultInputContext; + if (!manager || manager != &gInputManager || !eventOut) + return NTLSTATUS_INVALID_PARAMETER; + + for (;;) { + if (context->ManagerHead != context->ManagerTail) { + KE_INPUT_MANAGER_EVENT event = context->ManagerQueue[context->ManagerHead]; + context->ManagerHead = (context->ManagerHead + 1) & (int)MANAGER_QUEUE_MASK; + KiInputRefreshManagerEvent(context); + + memset(eventOut, 0, sizeof(*eventOut)); + eventOut->Size = sizeof(*eventOut); + eventOut->Type = event.Type; + eventOut->Sequence = event.Sequence; + if (event.Type == SYS_INPUT_MANAGER_EVENT_KEY) + eventOut->Data.Key = event.Data.Key; + else if (event.Type == SYS_INPUT_MANAGER_EVENT_MOUSE) + eventOut->Data.Mouse = event.Data.Mouse; + else + return NTLSTATUS_FAILURE; + return NTLSTATUS_SUCCESS; + } + + if (timeoutMs == 0) return NTLSTATUS_WOULD_BLOCK; + if (!context->ManagerWaitEvent) return NTLSTATUS_FAILURE; + EVENT* waitEvent = context->ManagerWaitEvent; + uint32_t selected = 0; + KSTATUS status = KeWaitForMultipleEvents(&waitEvent, 1, 0, timeoutMs, &selected); + if (!KSUCCESS(status)) return status; + } +} + // Implements the input Register Device operation. __declspec(dllexport) KSTATUS KeInputRegisterDevice(const char* name, uint32_t flags, uint32_t* outDeviceId) { (void)flags; @@ -261,6 +334,12 @@ __declspec(dllexport) void KeInputSubmitKey(const INPUT_KEY_EVENT* event) { INPUT_KEY key = event->Key; if (key == INPUT_KEY_NONE || key >= MAX_INPUT_KEYS) return; + KE_INPUT_MANAGER_EVENT managerEvent; + memset(&managerEvent, 0, sizeof(managerEvent)); + managerEvent.Type = SYS_INPUT_MANAGER_EVENT_KEY; + managerEvent.Data.Key = *event; + KiInputQueueManagerEvent(context, &managerEvent); + bool wasDown = context->KeyDown[key] != 0; bool pressed = event->Pressed; @@ -302,7 +381,6 @@ static bool KiInputCommitMousePosition(KE_INPUT_CONTEXT* context, int nx, int ny context->MouseX = nx; context->MouseY = ny; context->MouseButtons = buttons; - KeDisplaySetCursorPosition(nx, ny); return changed; } @@ -311,6 +389,12 @@ __declspec(dllexport) void KeInputSubmitMouse(const INPUT_MOUSE_EVENT* event) { KE_INPUT_CONTEXT* context = &gDefaultInputContext; if (!event) return; + KE_INPUT_MANAGER_EVENT managerEvent; + memset(&managerEvent, 0, sizeof(managerEvent)); + managerEvent.Type = SYS_INPUT_MANAGER_EVENT_MOUSE; + managerEvent.Data.Mouse = *event; + KiInputQueueManagerEvent(context, &managerEvent); + int oldX = context->MouseX; int oldY = context->MouseY; int nx = oldX + event->dx; @@ -337,6 +421,18 @@ __declspec(dllexport) void KeInputSubmitMouseAbsolute(const INPUT_MOUSE_ABSOLUTE int oldX = context->MouseX; int oldY = context->MouseY; + + INPUT_MOUSE_EVENT managerMouse; + managerMouse.dx = event->x - oldX; + managerMouse.dy = event->y - oldY; + managerMouse.wheel = event->wheel; + managerMouse.buttons = event->buttons; + KE_INPUT_MANAGER_EVENT managerEvent; + memset(&managerEvent, 0, sizeof(managerEvent)); + managerEvent.Type = SYS_INPUT_MANAGER_EVENT_MOUSE; + managerEvent.Data.Mouse = managerMouse; + KiInputQueueManagerEvent(context, &managerEvent); + bool changed = KiInputCommitMousePosition(context, event->x, event->y, event->buttons); if (event->wheel != 0) { context->MouseWheel += event->wheel; @@ -539,7 +635,6 @@ void KeInputMouseSetPosition(int x, int y) { bool changed = (x != context->MouseX) || (y != context->MouseY); context->MouseX = x; context->MouseY = y; - KeDisplaySetCursorPosition(x, y); if (changed) KiInputBumpGeneration(context); } diff --git a/code/kernel/ke_irq.c b/code/kernel/ke_irq.c index ad619c7..b5f8358 100644 --- a/code/kernel/ke_irq.c +++ b/code/kernel/ke_irq.c @@ -134,6 +134,7 @@ void KiExitInterrupt(void) { if (cpu->InterruptDepth == 0 && KeGetCurrentIrql() == PASSIVE_LEVEL) { THREAD* current = cpu->CurrentThread; if (current && !current->IsIdleThread && current->Owner && + !KeThreadExitInProgress(current) && KeIsProcessTerminating(current->Owner) && !KeThreadTerminationDeferred(current)) { KeExitThread(current, current->Owner->ExitCode); @@ -141,7 +142,14 @@ void KiExitInterrupt(void) { } KiDrainDpcQueue(); - if (KeGetCurrentIrql() == PASSIVE_LEVEL) + + // KeExitThread owns a terminal stack/rundown interval. A timer or IPI + // may interrupt it, but switching away here would abandon teardown + // before the process ExitEvent is necessarily published. The explicit + // KeYield at the end of KeExitThread performs the one terminal switch. + current = cpu->CurrentThread; + if (KeGetCurrentIrql() == PASSIVE_LEVEL && + (!current || !KeThreadExitInProgress(current))) KiScheduleFromInterrupt(); } } diff --git a/code/kernel/ke_irql.c b/code/kernel/ke_irql.c index b3fa396..79b9b83 100644 --- a/code/kernel/ke_irql.c +++ b/code/kernel/ke_irql.c @@ -4,6 +4,7 @@ #include #include #include +#include // Implements the irql Name operation. __declspec(dllexport) const char* KeIrqlName(KIRQL irql) { @@ -60,7 +61,8 @@ __declspec(dllexport) void KeLowerIrql(KIRQL oldIrql) { cpu->CurrentIrql = oldIrql; - if (oldIrql == PASSIVE_LEVEL && currentIrql != PASSIVE_LEVEL) + if (oldIrql == PASSIVE_LEVEL && currentIrql != PASSIVE_LEVEL && + (__readeflags() & 0x200u) != 0 && cpu->InterruptDepth == 0) KiDeliverKernelApcs(); } diff --git a/code/kernel/ke_process.c b/code/kernel/ke_process.c index 613517b..52a57ce 100644 --- a/code/kernel/ke_process.c +++ b/code/kernel/ke_process.c @@ -229,6 +229,7 @@ static uint32_t KiWaitObjectTypeToSys(OBJECT_TYPE type) { case ObjectTypeSurface: return SYS_THREAD_WAIT_SURFACE; case ObjectTypeTimer: return SYS_THREAD_WAIT_TIMER; case ObjectTypeDisplayClock: return SYS_THREAD_WAIT_DISPLAY_CLOCK; + case ObjectTypePipeEndpoint: return SYS_THREAD_WAIT_PIPE; default: return SYS_THREAD_WAIT_UNKNOWN; } } @@ -273,6 +274,7 @@ KSTATUS KeCopyProcessThreadInfo(uint32_t pid, struct SYS_THREAD_INFO* infoOut, s : SYS_THREAD_WAIT_NONE; info->WaitStatus = (uint32_t)snapshot.WaitStatus; info->KernelApcDisable = thread->KernelApcDisable; + (void)KeQueryThreadName(thread, info->Name, sizeof(info->Name)); if (thread == current) info->Flags |= SYS_THREAD_FLAG_CURRENT; if (thread->IsIdleThread) info->Flags |= SYS_THREAD_FLAG_IDLE; diff --git a/code/kernel/ke_sched.c b/code/kernel/ke_sched.c index 90175a6..469e745 100644 --- a/code/kernel/ke_sched.c +++ b/code/kernel/ke_sched.c @@ -794,6 +794,9 @@ KSTATUS KiCreateIdleThreadForCpu(KCPU* cpu) { idle->Teb = (void*)0; idle->TebSize = 0; idle->ExitEvent = (EVENT*)0; + idle->NameLock = (SPINLOCK)SPINLOCK_INIT; + memset(idle->Name, 0, sizeof(idle->Name)); + memcpy(idle->Name, "idle", sizeof("idle")); KiInitializeApcState(idle); idle->TrapFrame = (KTRAP_FRAME*)0; LIST_INIT(&idle->ProcessLink); @@ -1071,14 +1074,11 @@ static void KiSchedule(void) { uintptr_t flags = KeAcquireSpinlock(&gSchedLock); KiArmPostSwitchUnlock(cur, flags); - // The currently running thread is not present in a run queue. If it is - // still runnable and nobody else is ready, keep executing it. Otherwise a - // lone CPU-bound thread gets alternated with the idle thread on every timer - // tick, effectively turning a periodic preemption check into a 50% throttle. - if (cur != idle && KiThreadIsKilledLocked(cur)) { - cur->State = ThreadDead; - } - + // The scheduler never converts process termination directly into + // ThreadDead. Terminal state owns rundown/lifetime publication and is + // entered only through KeExitThread at a safe execution boundary. + // If the current thread is still runnable and nobody else is ready, keep + // executing it rather than alternating with idle on every timer tick. if (cur != idle && cur->State == ThreadRunning && !KiAnyRunnableForCpuLocked(cpu)) { if (cur->QuantumRemaining == 0) KiRefillQuantumLocked(cur); @@ -1379,6 +1379,11 @@ static int KiRunLocalSchedulerTickLocked(KCPU* cpu, THREAD* cur, THREAD* idle) { if (cur == idle) { if (KiAnyRunnableForCpuLocked(cpu)) requestReschedule = 1; + } else if (KeThreadExitInProgress(cur)) { + // Terminal teardown is interruptible but not preemptible. Its explicit + // final KeYield performs the permanent switch after completion state is + // published. + requestReschedule = 0; } else if (KiThreadIsKilledLocked(cur)) { requestReschedule = 1; } else { diff --git a/code/kernel/ke_selftest.c b/code/kernel/ke_selftest.c index a81ba64..4120f32 100644 --- a/code/kernel/ke_selftest.c +++ b/code/kernel/ke_selftest.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -28,460 +29,83 @@ #include #include -// Kernel APC self-test. - -typedef struct KI_APC_SELFTEST_STATE { - EVENT* ReadyEvent; - EVENT* GoEvent; - EVENT* DoneEvent; - EVENT* FinishedEvent; - - THREAD* WorkerThread; - - volatile int SelfDelivered; - volatile int DeferredDelivered; - volatile int CrossDelivered; -} KI_APC_SELFTEST_STATE; - -static void KiApcSelfTestKernelRoutine(KAPC* apc, - void** normalRoutine, - void** normalContext, - void** systemArgument1, - void** systemArgument2) { - DbgPrint("APC selftest: kernel routine apc=0x%016llX normal=0x%016llX ctx=0x%016llX arg1=0x%016llX arg2=0x%016llX irql=%u\n", - (uint64_t)apc, - (uint64_t)*normalRoutine, - (uint64_t)*normalContext, - (uint64_t)*systemArgument1, - (uint64_t)*systemArgument2, - KeGetCurrentIrql()); -} - -static void KiApcSelfTestNormalRoutine(void* normalContext, - void* systemArgument1, - void* systemArgument2) { - volatile int* counter = (volatile int*)normalContext; - - DbgPrint("APC selftest: normal routine ctx=0x%016llX arg1=0x%016llX arg2=0x%016llX irql=%u\n", - (uint64_t)normalContext, - (uint64_t)systemArgument1, - (uint64_t)systemArgument2, - KeGetCurrentIrql()); +// Focused APC wait-commit race self-test. User-mode APC return and async I/O +// behavior are covered by executable smoke tests; keep only the scheduler race +// that cannot be constructed through the public ABI. +// Verifies that one APC normal routine executes in interruptible PASSIVE thread context. +static void KiAssertApcNormalContext(const char* name) { + KCPU* cpu = KeGetCurrentCpu(); if (KeGetCurrentIrql() != PASSIVE_LEVEL) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: normal routine ran above PASSIVE_LEVEL\n"); - - (*counter)++; + KePanic(KE_PANIC_CODE_SELFTEST, "%s ran above PASSIVE_LEVEL\n", name); + if ((__readeflags() & 0x200u) == 0) + KePanic(KE_PANIC_CODE_SELFTEST, "%s ran with interrupts disabled\n", name); + if (!cpu || cpu->InterruptDepth != 0) + KePanic(KE_PANIC_CODE_SELFTEST, "%s ran inside interrupt context depth=%u\n", + name, cpu ? cpu->InterruptDepth : 0xFFFFFFFFu); } -static void KiApcSelfTestCrossThreadRoutine(void* normalContext, - void* systemArgument1, - void* systemArgument2) { - KI_APC_SELFTEST_STATE* st = (KI_APC_SELFTEST_STATE*)normalContext; - - DbgPrint("APC selftest: cross-thread APC delivered arg1=0x%016llX arg2=0x%016llX irql=%u\n", - (uint64_t)systemArgument1, - (uint64_t)systemArgument2, - KeGetCurrentIrql()); - - if (KeGetCurrentIrql() != PASSIVE_LEVEL) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: cross-thread APC ran above PASSIVE_LEVEL\n"); - - st->CrossDelivered++; - KeSignalEvent(st->DoneEvent); -} - -// Implements the apc Self Test Worker operation. -static void KiApcSelfTestWorker(void* arg) { - KI_APC_SELFTEST_STATE* st = (KI_APC_SELFTEST_STATE*)arg; - - DbgPrint("APC selftest: worker started, signaling ready\n"); - KeSignalEvent(st->ReadyEvent); - - DbgPrint("APC selftest: worker waiting for go event\n"); - KSTATUS s = KeWaitForEvent(st->GoEvent, 0); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: worker wait failed status=%d\n", s); - - // KeCommitBlockThread(..., alertable) should deliver pending kernel APCs before this - // wait returns. If that hook is broken, DoneEvent will never be signaled. - DbgPrint("APC selftest: worker resumed after go event, cross=%d\n", - st->CrossDelivered); - - if (st->CrossDelivered != 1) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: worker resumed but APC was not delivered\n"); - - KeSignalEvent(st->FinishedEvent); - KeExitThread(KeGetCurrentThread(), 0); -} - -typedef struct KI_APC_ALERTABLE_SELFTEST_STATE { - EVENT* ReadyEvent; - EVENT* WaitEvent; - EVENT* FinishedEvent; - THREAD* WorkerThread; - - volatile int ApcDelivered; - volatile KSTATUS WaitStatus; -} KI_APC_ALERTABLE_SELFTEST_STATE; - -static void KiApcAlertableSelfTestNormalRoutine(void* normalContext, - void* systemArgument1, - void* systemArgument2) { - KI_APC_ALERTABLE_SELFTEST_STATE* st = - (KI_APC_ALERTABLE_SELFTEST_STATE*)normalContext; - - DbgPrint("APC selftest: alertable APC delivered arg1=0x%016llX arg2=0x%016llX irql=%u\n", - (uint64_t)systemArgument1, - (uint64_t)systemArgument2, - KeGetCurrentIrql()); - - if (KeGetCurrentIrql() != PASSIVE_LEVEL) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alertable APC ran above PASSIVE_LEVEL\n"); - - st->ApcDelivered++; -} - -// Implements the apc Alertable Self Test Worker operation. -static void KiApcAlertableSelfTestWorker(void* arg) { - KI_APC_ALERTABLE_SELFTEST_STATE* st = - (KI_APC_ALERTABLE_SELFTEST_STATE*)arg; - - DbgPrint("APC selftest: alertable worker ready, entering alertable wait\n"); - KeSignalEvent(st->ReadyEvent); - - KSTATUS s = KeWaitForEvent(st->WaitEvent, 1); - st->WaitStatus = s; - - DbgPrint("APC selftest: alertable wait returned status=%u delivered=%d\n", - s, st->ApcDelivered); - - if (s != NTLSTATUS_KERNEL_APC) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alertable wait returned wrong status=%u\n", s); - - if (st->ApcDelivered != 1) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alertable APC delivery count wrong=%d\n", - st->ApcDelivered); - - KeSignalEvent(st->FinishedEvent); - KeExitThread(KeGetCurrentThread(), 0); -} - -// Runs kernel Apc Self Test. -static void KiRunKernelApcSelfTest(void) { - DbgPrint("APC selftest: begin\n"); - - if (KeGetCurrentIrql() != PASSIVE_LEVEL) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: called above PASSIVE_LEVEL\n"); - - THREAD* self = KeGetCurrentThread(); - if (!self) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: no current thread\n"); - - // Test 1: queue APC to current thread at PASSIVE_LEVEL. - { - KAPC apc; - volatile int count = 0; - - KeInitializeApc(&apc, - self, - KiApcSelfTestKernelRoutine, - KiApcSelfTestNormalRoutine, - (void*)&count); - - if (!KeInsertQueueApc(&apc, (void*)0x11111111, (void*)0x22222222)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: self queue failed\n"); - - if (count != 1) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: self APC was not delivered immediately, count=%d\n", count); - - DbgPrint("APC selftest: self APC passed\n"); - } - - // Test 2: disable kernel APCs, queue, then re-enable. - { - KAPC apc; - volatile int count = 0; - - KeInitializeApc(&apc, - self, - KiApcSelfTestKernelRoutine, - KiApcSelfTestNormalRoutine, - (void*)&count); - - KeEnterCriticalRegion(); - - if (!KeInsertQueueApc(&apc, (void*)0x33333333, (void*)0x44444444)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: deferred queue failed\n"); - - if (count != 0) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: APC delivered while critical region held, count=%d\n", count); - - KeLeaveCriticalRegion(); - - if (count != 1) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: deferred APC was not delivered after leave, count=%d\n", count); - - DbgPrint("APC selftest: critical-region deferral passed\n"); - } - - // Test 3: queue APC to another blocked kernel thread, then wake it. - { - KI_APC_SELFTEST_STATE st; - KAPC apc; - memset(&st, 0, sizeof(st)); - - KSTATUS s; - s = KeEventCreate(EventAutoReset, 0, &st.ReadyEvent); - if (!KSUCCESS(s)) KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: ReadyEvent create failed %d\n", s); - - s = KeEventCreate(EventAutoReset, 0, &st.GoEvent); - if (!KSUCCESS(s)) KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: GoEvent create failed %d\n", s); - - s = KeEventCreate(EventAutoReset, 0, &st.DoneEvent); - if (!KSUCCESS(s)) KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: DoneEvent create failed %d\n", s); - - s = KeEventCreate(EventAutoReset, 0, &st.FinishedEvent); - if (!KSUCCESS(s)) KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: FinishedEvent create failed %d\n", s); - - const KE_THREAD_CREATE_PARAMETERS workerParameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); - s = KeCreateKernelThread((uintptr_t)KiApcSelfTestWorker, - (uintptr_t)&st, - &workerParameters, - &st.WorkerThread); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: worker create failed %d\n", s); - - s = KeWaitForEvent(st.ReadyEvent, 0); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: ready wait failed %d\n", s); - - DbgPrint("APC selftest: worker ready, queueing cross-thread APC\n"); - - KeInitializeApc(&apc, - st.WorkerThread, - KiApcSelfTestKernelRoutine, - KiApcSelfTestCrossThreadRoutine, - (void*)&st); - - if (!KeInsertQueueApc(&apc, (void*)0x55555555, (void*)0x66666666)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: cross-thread queue failed\n"); - - if (st.CrossDelivered != 0) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: cross-thread APC delivered before target resumed\n"); - - KeSignalEvent(st.GoEvent); - - s = KeWaitForEvent(st.DoneEvent, 0); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: done wait failed %d\n", s); - - if (st.CrossDelivered != 1) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: cross-thread delivery count wrong, count=%d\n", - st.CrossDelivered); - - // The APC signals DoneEvent before the worker returns from the wait. - // Wait for an explicit worker-owned event before dropping stack-backed - // state and event references. THREAD.ExitEvent is not part of this - // kernel-thread contract on all builds. - s = KeWaitForEvent(st.FinishedEvent, 0); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: worker finished wait failed %d\n", s); - - DbgPrint("APC selftest: cross-thread APC passed\n"); - - ObDereferenceObject(&st.ReadyEvent->Header); - ObDereferenceObject(&st.GoEvent->Header); - ObDereferenceObject(&st.DoneEvent->Header); - ObDereferenceObject(&st.FinishedEvent->Header); - ObDereferenceObject(&st.WorkerThread->Header); - } - - // Test 4: queue APC to a thread blocked in an alertable wait. - // The APC itself must wake the waiter; no event signal is sent. - { - KI_APC_ALERTABLE_SELFTEST_STATE st; - KAPC apc; - memset(&st, 0, sizeof(st)); - st.WaitStatus = NTLSTATUS_PENDING; - - KSTATUS s; - s = KeEventCreate(EventAutoReset, 0, &st.ReadyEvent); - if (!KSUCCESS(s)) KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alert ReadyEvent create failed %d\n", s); - - s = KeEventCreate(EventAutoReset, 0, &st.WaitEvent); - if (!KSUCCESS(s)) KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alert WaitEvent create failed %d\n", s); - - s = KeEventCreate(EventAutoReset, 0, &st.FinishedEvent); - if (!KSUCCESS(s)) KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alert FinishedEvent create failed %d\n", s); - - const KE_THREAD_CREATE_PARAMETERS workerParameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); - s = KeCreateKernelThread((uintptr_t)KiApcAlertableSelfTestWorker, - (uintptr_t)&st, - &workerParameters, - &st.WorkerThread); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alertable worker create failed %d\n", s); - - s = KeWaitForEvent(st.ReadyEvent, 0); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alert ready wait failed %d\n", s); - - DbgPrint("APC selftest: queueing APC to alertable waiter\n"); - - KeInitializeApc(&apc, - st.WorkerThread, - KiApcSelfTestKernelRoutine, - KiApcAlertableSelfTestNormalRoutine, - (void*)&st); - - if (!KeInsertQueueApc(&apc, (void*)(unsigned long long)0x77777777, (void*)(unsigned long long)0x88888888)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alertable APC queue failed\n"); - - s = KeWaitForEvent(st.FinishedEvent, 0); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alert finished wait failed %d\n", s); - - if (st.WaitStatus != NTLSTATUS_KERNEL_APC || st.ApcDelivered != 1) - KePanic(KE_PANIC_CODE_SELFTEST, "APC selftest: alertable result bad status=%u delivered=%d\n", - st.WaitStatus, st.ApcDelivered); - - DbgPrint("APC selftest: alertable wait passed\n"); - - ObDereferenceObject(&st.ReadyEvent->Header); - ObDereferenceObject(&st.WaitEvent->Header); - ObDereferenceObject(&st.FinishedEvent->Header); - ObDereferenceObject(&st.WorkerThread->Header); - } - - DbgPrint("APC selftest: PASS\n"); -} - -// IRP + kernel APC self-test. - -typedef struct KI_IRP_APC_SELFTEST_STATE { - EVENT* CompletionEvent; - DEVICE_OBJECT* DiskDevice; - IRP Irp; - KAPC Apc; - uint8_t* Buffer; - - volatile int ApcDelivered; - volatile KSTATUS ApcStatus; - volatile size_t ApcInformation; -} KI_IRP_APC_SELFTEST_STATE; - -static void KiIrpApcSelfTestKernelRoutine(KAPC* apc, - void** normalRoutine, - void** normalContext, - void** systemArgument1, - void** systemArgument2) { - DbgPrint("IRP/APC selftest: kernel routine apc=0x%016llX irp=0x%016llX status=%u irql=%u\n", - (uint64_t)apc, - (uint64_t)*systemArgument1, - (uint32_t)(uintptr_t)*systemArgument2, - KeGetCurrentIrql()); - +// Leaves the APC's normal routine and arguments unchanged. +static void KiApcWaitRaceKernelRoutine(KAPC* apc, + void** normalRoutine, + void** normalContext, + void** systemArgument1, + void** systemArgument2) { + (void)apc; (void)normalRoutine; (void)normalContext; + (void)systemArgument1; + (void)systemArgument2; } -static void KiIrpApcSelfTestNormalRoutine(void* normalContext, - void* systemArgument1, - void* systemArgument2) { - KI_IRP_APC_SELFTEST_STATE* st = (KI_IRP_APC_SELFTEST_STATE*)normalContext; - IRP* irp = (IRP*)systemArgument1; - KSTATUS status = (KSTATUS)(uintptr_t)systemArgument2; - - DbgPrint("IRP/APC selftest: APC delivered irp=0x%016llX status=%u info=%llu irql=%u\n", - (uint64_t)irp, - status, - (uint64_t)(irp ? irp->Information : 0), - KeGetCurrentIrql()); - - if (KeGetCurrentIrql() != PASSIVE_LEVEL) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: normal routine ran above PASSIVE_LEVEL\n"); - - if (irp != &st->Irp) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: APC arg1 was not the submitted IRP\n"); - - st->ApcDelivered++; - st->ApcStatus = status; - st->ApcInformation = irp->Information; +// Records delivery for the prepared-wait race probe. +static void KiApcWaitRaceNormalRoutine(void* normalContext, + void* systemArgument1, + void* systemArgument2) { + (void)systemArgument1; + (void)systemArgument2; + KiAssertApcNormalContext("APC wait-race normal routine"); + (*(volatile int*)normalContext)++; } -// Runs irp Kernel Apc Self Test. -static void KiRunIrpKernelApcSelfTest(const HAL_API* hal) { - DbgPrint("IRP/APC selftest: begin\n"); +// Reproduces the prepare-to-commit APC race deterministically. +static void KiRunApcWaitRaceSelfTest(void) { + DbgPrint("APC wait-race selftest: begin\n"); - (void)hal; - if (!IoDiskDevicePresent(0)) { - DbgPrint("IRP/APC selftest: SKIP, drive 0 not present\n"); - return; - } + THREAD* self = KeGetCurrentThread(); + if (!self || KeGetCurrentIrql() != PASSIVE_LEVEL) + KePanic(KE_PANIC_CODE_SELFTEST, "APC wait-race selftest: invalid execution context\n"); - if (KeGetCurrentIrql() != PASSIVE_LEVEL) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: called above PASSIVE_LEVEL\n"); + EVENT* waitEvent = (EVENT*)0; + KAPC apc; + volatile int delivered = 0; + KSTATUS status = KeEventCreate(EventAutoReset, 0, &waitEvent); + if (!KSUCCESS(status)) + KePanic(KE_PANIC_CODE_SELFTEST, "APC wait-race selftest: event create failed %u\n", status); - KI_IRP_APC_SELFTEST_STATE st; - memset(&st, 0, sizeof(st)); + KeInitializeApc(&apc, self, KiApcWaitRaceKernelRoutine, + KiApcWaitRaceNormalRoutine, (void*)&delivered); - KSTATUS s = KeEventCreate(EventAutoReset, 0, &st.CompletionEvent); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: completion event create failed %u\n", s); + uintptr_t flags = KeAcquireSpinlock(&waitEvent->Lock); + if (!KeInsertQueueApc(&apc, (void*)(uintptr_t)0x99999999u, + (void*)(uintptr_t)0xAAAAAAAAu)) + KePanic(KE_PANIC_CODE_SELFTEST, "APC wait-race selftest: queue failed\n"); + if (delivered != 0) + KePanic(KE_PANIC_CODE_SELFTEST, "APC wait-race selftest: delivered above PASSIVE_LEVEL\n"); - st.Buffer = (uint8_t*)MmAllocatePool(512); - if (!st.Buffer) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: no buffer\n"); - memset(st.Buffer, 0xCC, 512); + KePrepareBlockThread(self, &waitEvent->Header, 1); + KeListInsertTailChecked(&waitEvent->WaitList, &self->SchedulerLink); + KeSetThreadWaitList(self, &waitEvent->WaitList); + KeReleaseSpinlock(&waitEvent->Lock, flags); - KeInitializeApc(&st.Apc, - KeGetCurrentThread(), - KiIrpApcSelfTestKernelRoutine, - KiIrpApcSelfTestNormalRoutine, - (void*)&st); + status = KeCommitBlockThread(self, 1); + if (status != NTLSTATUS_KERNEL_APC || delivered != 1) + KePanic(KE_PANIC_CODE_SELFTEST, + "APC wait-race selftest: bad result status=%u delivered=%d\n", + status, delivered); - st.DiskDevice = IoReferenceDiskDevice(0); - if (!st.DiskDevice) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: drive 0 disappeared before submit\n"); - - IoBuildReadWriteIrp(&st.Irp, IRP_MJ_READ, 0, 512, st.Buffer); - st.Irp.CompletionEvent = st.CompletionEvent; - st.Irp.CompletionApc = &st.Apc; - st.Irp.CompletionFn = (IRP_COMPLETION_FN)0; - st.Irp.CompletionCtx = (void*)0; - - DbgPrint("IRP/APC selftest: submitting drive=0 lba=0 sectors=1\n"); - s = IoSubmitIrp(st.DiskDevice, &st.Irp); - if (s != NTLSTATUS_PENDING) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: IoSubmitIrp returned %u\n", s); - - s = KeWaitForEvent(st.CompletionEvent, 0); - if (!KSUCCESS(s)) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: completion wait failed %u\n", s); - - // The wait return path should have delivered the pending kernel APC. - if (st.ApcDelivered != 1) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: APC was not delivered after I/O wait, count=%d\n", - st.ApcDelivered); - - if (!KSUCCESS(st.Irp.Status) || !KSUCCESS(st.ApcStatus)) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: I/O failed irp=%u apc=%u info=%llu\n", - st.Irp.Status, st.ApcStatus, (uint64_t)st.Irp.Information); - - if (st.Irp.Information != 512 || st.ApcInformation != 512) - KePanic(KE_PANIC_CODE_SELFTEST, "IRP/APC selftest: wrong byte count irp=%llu apc=%llu\n", - (uint64_t)st.Irp.Information, (uint64_t)st.ApcInformation); - - DbgPrint("IRP/APC selftest: first bytes %02X %02X %02X %02X, tail %02X %02X\n", - st.Buffer[0], st.Buffer[1], st.Buffer[2], st.Buffer[3], - st.Buffer[510], st.Buffer[511]); - - MmFreePool(st.Buffer); - if (st.DiskDevice) IoDereferenceDeviceObject(st.DiskDevice); - ObDereferenceObject(&st.CompletionEvent->Header); - - DbgPrint("IRP/APC selftest: PASS\n"); + ObDereferenceObject(&waitEvent->Header); + DbgPrint("APC wait-race selftest: PASS\n"); } // Verifies that the PFN database tracks ownership, references, pins, and contiguous runs. @@ -1510,7 +1134,8 @@ static void KiRunSynchronizationTimeoutSelfTest(void) { "SYNC/TIMEOUT selftest: owner acquire failed status=%u\n", status); THREAD* worker = (THREAD*)0; - const KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_REALTIME(12); + const KE_THREAD_CREATE_PARAMETERS parameters = + KE_THREAD_CREATE_NAMED_REALTIME(12, "selftest.sync.timeout"); status = KeCreateKernelThread((uintptr_t)KiSynchronizationTimeoutWorker, (uintptr_t)&state, ¶meters, @@ -1691,7 +1316,8 @@ static void KiRunRealtimeSchedulerSelfTest(void) { "SCHED selftest: owner acquire failed status=%u\n", status); THREAD* chainWorker = (THREAD*)0; - const KE_THREAD_CREATE_PARAMETERS chainParameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + const KE_THREAD_CREATE_PARAMETERS chainParameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "selftest.sched.chain"); status = KeCreateKernelThread((uintptr_t)KiSchedulerChainWorker, (uintptr_t)&state, &chainParameters, @@ -1713,7 +1339,8 @@ static void KiRunRealtimeSchedulerSelfTest(void) { "SCHED selftest: mutex cycle was not rejected status=%u\n", status); THREAD* realtimeWorker = (THREAD*)0; - const KE_THREAD_CREATE_PARAMETERS realtimeParameters = KE_THREAD_CREATE_REALTIME(16); + const KE_THREAD_CREATE_PARAMETERS realtimeParameters = + KE_THREAD_CREATE_NAMED_REALTIME(16, "selftest.sched.rt"); status = KeCreateKernelThread((uintptr_t)KiSchedulerRealtimeWorker, (uintptr_t)&state, &realtimeParameters, @@ -1895,10 +1522,11 @@ void KiRunEarlyKernelSelfTests(void) { // Runs kernel Self Tests. void KiRunKernelSelfTests(const HAL_API* hal) { + (void)hal; DbgPrint("Kernel selftests: begin\n"); KiRunSynchronizationTimeoutSelfTest(); KiRunRealtimeSchedulerSelfTest(); - KiRunKernelApcSelfTest(); + KiRunApcWaitRaceSelfTest(); KiRunPfnDatabaseSelfTest(); KiRunDmaSelfTest(); KiRunMemoryMappingSelfTest(); @@ -1907,7 +1535,6 @@ void KiRunKernelSelfTests(const HAL_API* hal) { KiRunVadSwapSelfTest(); KiRunStringViewSelfTest(); KiRunVolumeRelativePathSelfTest(); - KiRunIrpKernelApcSelfTest(hal); KiRunAsyncFileIoSelfTest(); KiRunTrapFrameSelfTest(); DbgPrint("Kernel selftests: PASS\n"); diff --git a/code/kernel/ke_smp.c b/code/kernel/ke_smp.c index 1929180..4df76ee 100644 --- a/code/kernel/ke_smp.c +++ b/code/kernel/ke_smp.c @@ -28,8 +28,8 @@ #define AP_TRAMPOLINE_FALLBACK_MIN 0x00010000ULL #define AP_TRAMPOLINE_BOOT_OFF 0x0800u #define AP_STACK_PAGES 8u -#define AP_START_TIMEOUT_SPINS 20000000u -#define AP_SCHED_TIMEOUT_SPINS 20000000u +#define AP_START_TIMEOUT_US 1000000ull +#define AP_SCHED_TIMEOUT_US 1000000ull #define AP_CR4_PAE 0x00000020u #define AP_CR4_OSFXSR 0x00000200u @@ -50,6 +50,18 @@ #define AP_STATE_KERNEL_PARKED 4u #define AP_STATE_FAILED 0xFFFFFFFFu +// Returns the HAL monotonic counter in microseconds for pre-scheduler hardware deadlines. +static uint64_t KiSmpQueryTicks(void) { + const HAL_API* hal = KeGetHal(); + return hal && hal->Timer.QueryTicks ? hal->Timer.QueryTicks() : 0; +} + +// Returns a saturated deadline from the current HAL monotonic counter. +static uint64_t KiSmpDeadlineAfter(uint64_t timeoutUs) { + uint64_t now = KiSmpQueryTicks(); + return now > UINT64_MAX - timeoutUs ? UINT64_MAX : now + timeoutUs; +} + #define AP_STAGE_NONE 0x00000000u #define AP_STAGE_LONG64 0x4C4D3634u // 'LM64' #define AP_STAGE_ENTRY 0x454E5452u // 'ENTR' @@ -748,16 +760,16 @@ KSTATUS KeStartApplicationProcessors(void) { } uint64_t lastHeartbeat = gApBootBlock->Heartbeat; + uint64_t startDeadline = KiSmpDeadlineAfter(AP_START_TIMEOUT_US); int parked = 0; - for (uint32_t spin = 0; spin < AP_START_TIMEOUT_SPINS; spin++) { + for (;;) { if (gApBootBlock->State == AP_STATE_KERNEL_PARKED) { parked = 1; break; } - if ((spin & 0xFFFFu) == 0) { - uint64_t hb = gApBootBlock->Heartbeat; - lastHeartbeat = hb; - } + lastHeartbeat = gApBootBlock->Heartbeat; + if (KiSmpQueryTicks() >= startDeadline) + break; _mm_pause(); } @@ -823,11 +835,14 @@ KSTATUS KeAdmitApplicationProcessorsToScheduler(void) { KeSendProcessorInterrupt(cpu->HardwareId, KE_VECTOR_RESCHEDULE_IPI); int active = 0; - for (uint32_t spin = 0; spin < AP_SCHED_TIMEOUT_SPINS; spin++) { + uint64_t schedulerDeadline = KiSmpDeadlineAfter(AP_SCHED_TIMEOUT_US); + for (;;) { if (_InterlockedCompareExchange(&cpu->State, 0, 0) == KeCpuSchedulerActive) { active = 1; break; } + if (KiSmpQueryTicks() >= schedulerDeadline) + break; _mm_pause(); } @@ -849,7 +864,7 @@ KSTATUS KeAdmitApplicationProcessorsToScheduler(void) { } #define SMP_AP_WORKER_ROUNDS 64u -#define SMP_AP_WORKER_TIMEOUT_SPINS 4000000u +#define SMP_AP_WORKER_TIMEOUT_US 2000000ull #define SMP_AP_TEST_MAGIC 0x524B575041504D53ULL // 'SMPAPWKR' typedef struct SMP_AP_WORK_TEST SMP_AP_WORK_TEST; @@ -933,13 +948,13 @@ static void KiSmpApControlledWorker(void* context) { // Waits for for Worker Counter. static int KiWaitForWorkerCounter(volatile long* counter, long expected, const char* what) { - for (uint32_t spin = 0; spin < SMP_AP_WORKER_TIMEOUT_SPINS; spin++) { + uint64_t deadline = KiSmpDeadlineAfter(SMP_AP_WORKER_TIMEOUT_US); + for (;;) { if (_InterlockedCompareExchange(counter, 0, 0) >= expected) return 1; - if ((spin & 0x3FFu) == 0) - KeYield(); - else - _mm_pause(); + if (KiSmpQueryTicks() >= deadline) + break; + KeYield(); } DbgPrint("SMP scheduler selftest timeout waiting for %s expected=%ld got=%ld\n", what ? what : "counter", expected, _InterlockedCompareExchange(counter, 0, 0)); @@ -981,7 +996,8 @@ KSTATUS KeRunApplicationProcessorSchedulerSelfTests(void) { cpuIndex, st); } - KE_THREAD_CREATE_PARAMETERS workerParameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + KE_THREAD_CREATE_PARAMETERS workerParameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "selftest.smp"); workerParameters.AffinityMask = 1ULL << cpuIndex; st = KeCreateKernelThread((uintptr_t)KiSmpApControlledWorker, (uintptr_t)arg, diff --git a/code/kernel/ke_surface.c b/code/kernel/ke_surface.c new file mode 100644 index 0000000..9363c1c --- /dev/null +++ b/code/kernel/ke_surface.c @@ -0,0 +1,439 @@ +// Kernel surface storage. This file owns shared backing frames, mappings, commit generations, and privileged presentation. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct KE_SURFACE { + OBJECT Header; + uint32_t Width; + uint32_t Height; + uint32_t PitchBytes; + uint32_t Format; + uint32_t Flags; + size_t PayloadSize; + size_t PageCount; + uint8_t StatsRegistered; + uint8_t LastDamageValid; + uint16_t Reserved; + uint32_t Reserved2; + uint64_t MapCount; + uint64_t ContentGeneration; + uint64_t CommitCount; + uint64_t CommitRectCount; + uint64_t CommitClippedRectCount; + uint64_t CommitEmptyClipCount; + DISPLAY_RECT LastDamageBounds; + PHYSICAL_ADDRESS* Frames; + MM_KERNEL_VIEW* KernelView; + void* KernelBase; + EVENT* CommitEvent; + MUTEX* Lock; +} KE_SURFACE; + +static SPINLOCK gSurfaceStatsLock = SPINLOCK_INIT; +static SYS_SURFACE_STATS gSurfaceStats; + +// Multiplies two sizes while rejecting overflow. +static int KiSurfaceMulSize(size_t a, size_t b, size_t* out) { + if (!out || (a != 0 && b > SIZE_MAX / a)) return 0; + *out = a * b; + return 1; +} + +// Aligns one size upward while rejecting wraparound. +static size_t KiSurfaceAlignUp(size_t value, size_t alignment) { + if (!alignment || (alignment & (alignment - 1u))) return 0; + size_t mask = alignment - 1u; + if (value > SIZE_MAX - mask) return 0; + return (value + mask) & ~mask; +} + +// Converts a generic object reference to a surface only when its type is correct. +static KE_SURFACE* KiSurfaceFromObject(OBJECT* object) { + if (!object || object->Type != ObjectTypeSurface) return (KE_SURFACE*)0; + return CONTAINING_RECORD(object, KE_SURFACE, Header); +} + +// Registers a surface in the global telemetry snapshot. +static void KiSurfaceStatsRegister(KE_SURFACE* surface) { + if (!surface || surface->StatsRegistered) return; + uintptr_t irq = KeAcquireSpinlock(&gSurfaceStatsLock); + gSurfaceStats.CreatedSurfaces++; + gSurfaceStats.LiveSurfaces++; + gSurfaceStats.LivePages += surface->PageCount; + gSurfaceStats.LiveBytes += surface->PayloadSize; + surface->StatsRegistered = 1; + KeReleaseSpinlock(&gSurfaceStatsLock, irq); +} + +// Removes a surface from the global telemetry snapshot. +static void KiSurfaceStatsUnregister(KE_SURFACE* surface) { + if (!surface || !surface->StatsRegistered) return; + uintptr_t irq = KeAcquireSpinlock(&gSurfaceStatsLock); + gSurfaceStats.DestroyedSurfaces++; + if (gSurfaceStats.LiveSurfaces) gSurfaceStats.LiveSurfaces--; + gSurfaceStats.LivePages = gSurfaceStats.LivePages >= surface->PageCount + ? gSurfaceStats.LivePages - surface->PageCount : 0; + gSurfaceStats.LiveBytes = gSurfaceStats.LiveBytes >= surface->PayloadSize + ? gSurfaceStats.LiveBytes - surface->PayloadSize : 0; + surface->StatsRegistered = 0; + KeReleaseSpinlock(&gSurfaceStatsLock, irq); +} + +// Records one successful surface mapping operation. +static void KiSurfaceStatsRecordMap(KE_SURFACE* surface) { + uintptr_t irq = KeAcquireSpinlock(&gSurfaceStatsLock); + surface->MapCount++; + gSurfaceStats.MapCalls++; + KeReleaseSpinlock(&gSurfaceStatsLock, irq); +} + +// Records one commit after rectangle validation and clipping. +static void KiSurfaceStatsRecordCommit(uint64_t inputRects, uint64_t clippedRects, uint64_t emptyClips) { + uintptr_t irq = KeAcquireSpinlock(&gSurfaceStatsLock); + gSurfaceStats.CommitCalls++; + gSurfaceStats.CommitRectInputs += inputRects; + gSurfaceStats.CommitClippedRects += clippedRects; + gSurfaceStats.CommitEmptyClips += emptyClips; + KeReleaseSpinlock(&gSurfaceStatsLock, irq); +} + +// Records one presentation performed through DisplayManager authority. +static void KiSurfaceStatsRecordPresent(uint64_t bytesCopied) { + uintptr_t irq = KeAcquireSpinlock(&gSurfaceStatsLock); + gSurfaceStats.PhysicalPresentCalls++; + gSurfaceStats.PhysicalPresentBytesCopied += bytesCopied; + KeReleaseSpinlock(&gSurfaceStatsLock, irq); +} + +// Releases surface storage. User VADs retain their own backing-frame references until unmapped. +static void KiSurfaceDestroy(OBJECT* object) { + KE_SURFACE* surface = KiSurfaceFromObject(object); + if (!surface) return; + + KiSurfaceStatsUnregister(surface); + if (surface->KernelView) { + MmDestroyKernelView(surface->KernelView); + surface->KernelView = (MM_KERNEL_VIEW*)0; + surface->KernelBase = (void*)0; + } + if (surface->CommitEvent) { + ObDereferenceObject(&surface->CommitEvent->Header); + surface->CommitEvent = (EVENT*)0; + } + if (surface->Lock) { + ObDereferenceObject(&surface->Lock->Header); + surface->Lock = (MUTEX*)0; + } + if (surface->Frames) { + for (size_t i = 0; i < surface->PageCount; ++i) + if (surface->Frames[i]) (void)MmPfnRelease(surface->Frames[i]); + MmFreePool(surface->Frames); + surface->Frames = (PHYSICAL_ADDRESS*)0; + } +} + +// Clips one rectangle to the surface extent. +static int KiSurfaceClipRect(const KE_SURFACE* surface, const DISPLAY_RECT* input, DISPLAY_RECT* output) { + if (!surface || !output) return 0; + DISPLAY_RECT r; + if (input) { + r = *input; + } else { + r.X = 0; + r.Y = 0; + r.W = (int32_t)surface->Width; + r.H = (int32_t)surface->Height; + } + if (r.W <= 0 || r.H <= 0) return 0; + + int64_t x0 = r.X; + int64_t y0 = r.Y; + int64_t x1 = x0 + r.W; + int64_t y1 = y0 + r.H; + if (x0 < 0) x0 = 0; + if (y0 < 0) y0 = 0; + if (x1 > surface->Width) x1 = surface->Width; + if (y1 > surface->Height) y1 = surface->Height; + if (x0 >= x1 || y0 >= y1) return 0; + + output->X = (int32_t)x0; + output->Y = (int32_t)y0; + output->W = (int32_t)(x1 - x0); + output->H = (int32_t)(y1 - y0); + return 1; +} + +// Expands one damage bound to include another clipped rectangle. +static void KiSurfaceUnionRect(DISPLAY_RECT* bounds, const DISPLAY_RECT* rect) { + int32_t x0 = bounds->X < rect->X ? bounds->X : rect->X; + int32_t y0 = bounds->Y < rect->Y ? bounds->Y : rect->Y; + int32_t bx1 = bounds->X + bounds->W; + int32_t by1 = bounds->Y + bounds->H; + int32_t rx1 = rect->X + rect->W; + int32_t ry1 = rect->Y + rect->H; + int32_t x1 = bx1 > rx1 ? bx1 : rx1; + int32_t y1 = by1 > ry1 ? by1 : ry1; + bounds->X = x0; + bounds->Y = y0; + bounds->W = x1 - x0; + bounds->H = y1 - y0; +} + +// Clips one kernel-captured rectangle list. A zero count means the whole surface. +static KSTATUS KiSurfaceCaptureRects(KE_SURFACE* surface, const DISPLAY_RECT* rects, uint32_t rectCount, + DISPLAY_RECT* clippedOut, uint32_t* clippedCountOut, + uint64_t* emptyOut) { + if (!surface || !clippedOut || !clippedCountOut || !emptyOut) + return NTLSTATUS_INVALID_PARAMETER; + if (rectCount > SYS_SURFACE_MAX_RECTS || (rectCount && !rects)) + return NTLSTATUS_INVALID_PARAMETER; + + DISPLAY_RECT whole = {0, 0, (int32_t)surface->Width, (int32_t)surface->Height}; + if (!rectCount) { + rects = &whole; + rectCount = 1; + } + + uint32_t clippedCount = 0; + uint64_t empty = 0; + for (uint32_t i = 0; i < rectCount; ++i) { + DISPLAY_RECT clipped; + if (!KiSurfaceClipRect(surface, &rects[i], &clipped)) { + empty++; + continue; + } + clippedOut[clippedCount++] = clipped; + } + *clippedCountOut = clippedCount; + *emptyOut = empty; + return NTLSTATUS_SUCCESS; +} + +// Creates one surface storage object and returns its initial object reference. +KSTATUS KeSurfaceCreate(uint32_t width, uint32_t height, uint32_t format, uint32_t flags, + OBJECT** surfaceOut) { + if (!surfaceOut) return NTLSTATUS_INVALID_PARAMETER; + *surfaceOut = (OBJECT*)0; + if (!width || !height || width > SYS_SURFACE_MAX_DIMENSION || height > SYS_SURFACE_MAX_DIMENSION) + return NTLSTATUS_INVALID_PARAMETER; + if (format != SYS_SURFACE_FORMAT_XRGB8888 || flags != SYS_SURFACE_FLAG_NONE) + return NTLSTATUS_NOT_SUPPORTED; + + size_t rowBytes = 0; + if (!KiSurfaceMulSize((size_t)width, sizeof(uint32_t), &rowBytes)) + return NTLSTATUS_INVALID_PARAMETER; + size_t pitchBytes = KiSurfaceAlignUp(rowBytes, 64u); + if (!pitchBytes || pitchBytes < rowBytes) return NTLSTATUS_INVALID_PARAMETER; + + size_t payloadSize = 0; + if (!KiSurfaceMulSize(pitchBytes, (size_t)height, &payloadSize) || payloadSize > SYS_SURFACE_MAX_BYTES) + return NTLSTATUS_INVALID_PARAMETER; + size_t pageCount = (payloadSize + PAGE_SIZE - 1u) / PAGE_SIZE; + if (!pageCount || pageCount > SIZE_MAX / sizeof(PHYSICAL_ADDRESS)) + return NTLSTATUS_INVALID_PARAMETER; + + KE_SURFACE* surface = (KE_SURFACE*)ObAllocateObject(ObjectTypeSurface, sizeof(KE_SURFACE)); + if (!surface) return NTLSTATUS_NO_MEMORY; + surface->Header.Destroy = KiSurfaceDestroy; + surface->Width = width; + surface->Height = height; + surface->PitchBytes = (uint32_t)pitchBytes; + surface->Format = format; + surface->Flags = flags; + surface->PayloadSize = payloadSize; + surface->PageCount = pageCount; + + surface->Frames = (PHYSICAL_ADDRESS*)MmAllocatePoolTag(pageCount * sizeof(PHYSICAL_ADDRESS), + MM_POOL_TAG('s', 'f', 'r', 'm')); + if (surface->Frames) + memset(surface->Frames, 0, pageCount * sizeof(PHYSICAL_ADDRESS)); + + KSTATUS status = KeMutexCreate(&surface->Lock); + if (KSUCCESS(status)) status = KeEventCreate(EventAutoReset, 0, &surface->CommitEvent); + if (!surface->Frames || !KSUCCESS(status)) { + ObDereferenceObject(&surface->Header); + return !surface->Frames ? NTLSTATUS_NO_MEMORY : status; + } + for (size_t i = 0; i < pageCount; ++i) { + PHYSICAL_ADDRESS frame = MmPfnAllocate(MmPfnUseSurface); + if (!frame) { + ObDereferenceObject(&surface->Header); + return NTLSTATUS_NO_MEMORY; + } + surface->Frames[i] = frame; + void* kva = MmPfnToVirtual(frame); + if (kva) memset(kva, 0, PAGE_SIZE); + } + + status = MmCreateKernelViewFromFrames(surface->Frames, surface->PageCount, &surface->KernelView); + if (!KSUCCESS(status)) { + ObDereferenceObject(&surface->Header); + return status; + } + surface->KernelBase = MmKernelViewBase(surface->KernelView); + KiSurfaceStatsRegister(surface); + *surfaceOut = &surface->Header; + return NTLSTATUS_SUCCESS; +} + +// Maps one surface into the calling process's VAD namespace with attenuated page permissions. +KSTATUS KeSurfaceMap(PROCESS* process, OBJECT* object, uint32_t flags, + void** baseOut, uint32_t* pitchBytesOut) { + KE_SURFACE* surface = KiSurfaceFromObject(object); + if (!process || !surface || !baseOut || !pitchBytesOut || !(flags & SYS_SURFACE_MAP_READ) || + (flags & ~(SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE))) + return NTLSTATUS_INVALID_PARAMETER; + + uint64_t pageFlags = PAGE_PRESENT | PAGE_USER | PAGE_NX; + if (flags & SYS_SURFACE_MAP_WRITE) pageFlags |= PAGE_WRITE; + KSTATUS status = MmVadCreateSharedViewFromFrames(process, surface->Frames, surface->PageCount, + surface->PayloadSize, pageFlags, + MM_VAD_SURFACE, MmVadShared, baseOut); + if (KSUCCESS(status)) { + *pitchBytesOut = surface->PitchBytes; + KiSurfaceStatsRecordMap(surface); + } + return status; +} + +// Removes one mapping only when the supplied VAD is a surface shared view owned by the process. +KSTATUS KeSurfaceUnmap(PROCESS* process, void* base) { + if (!process || !base) return NTLSTATUS_INVALID_PARAMETER; + void* vadBase = (void*)0; + size_t vadSize = 0; + uint64_t pageFlags = 0; + MM_VAD_TYPE type = MmVadGeneric; + uint32_t vadFlags = 0; + KSTATUS status = MmVadQuery(process, base, &vadBase, &vadSize, &pageFlags, &type, &vadFlags); + (void)vadSize; + (void)pageFlags; + if (!KSUCCESS(status)) return status; + if (vadBase != base || type != MmVadShared || !(vadFlags & MM_VAD_SURFACE)) + return NTLSTATUS_INVALID_PARAMETER; + return MmVadDestroy(process, base); +} + +// Snapshots one surface's storage and commit metadata. +KSTATUS KeSurfaceQuery(OBJECT* object, SYS_SURFACE_DESC* descOut) { + KE_SURFACE* surface = KiSurfaceFromObject(object); + if (!surface || !descOut) return NTLSTATUS_INVALID_PARAMETER; + memset(descOut, 0, sizeof(*descOut)); + + KSTATUS status = KeAcquireMutex(surface->Lock, KE_WAIT_INFINITE); + if (!KE_MUTEX_ACQUIRE_SUCCEEDED(status)) return status; + descOut->Width = surface->Width; + descOut->Height = surface->Height; + descOut->PitchBytes = surface->PitchBytes; + descOut->Format = surface->Format; + descOut->PayloadBytes = surface->PayloadSize; + descOut->PageCount = surface->PageCount; + descOut->ContentGeneration = surface->ContentGeneration; + descOut->CommitCount = surface->CommitCount; + descOut->CommitRectCount = surface->CommitRectCount; + descOut->CommitClippedRectCount = surface->CommitClippedRectCount; + descOut->CommitEmptyClipCount = surface->CommitEmptyClipCount; + descOut->LastDamageBounds = surface->LastDamageBounds; + descOut->LastDamageValid = surface->LastDamageValid; + KeReleaseMutex(surface->Lock); + + uintptr_t irq = KeAcquireSpinlock(&gSurfaceStatsLock); + descOut->MapCount = surface->MapCount; + KeReleaseSpinlock(&gSurfaceStatsLock, irq); + return NTLSTATUS_SUCCESS; +} + +// Announces changed pixels without touching physical scanout. +KSTATUS KeSurfaceCommit(OBJECT* object, const DISPLAY_RECT* rects, uint32_t rectCount, + uint64_t* generationOut) { + KE_SURFACE* surface = KiSurfaceFromObject(object); + if (!surface) return NTLSTATUS_INVALID_PARAMETER; + + uint32_t requestedCount = rectCount ? rectCount : 1u; + DISPLAY_RECT clipped[SYS_SURFACE_MAX_RECTS]; + uint32_t clippedCount = 0; + uint64_t emptyCount = 0; + KSTATUS status = KiSurfaceCaptureRects(surface, rects, rectCount, clipped, &clippedCount, &emptyCount); + if (!KSUCCESS(status)) return status; + + status = KeAcquireMutex(surface->Lock, KE_WAIT_INFINITE); + if (!KE_MUTEX_ACQUIRE_SUCCEEDED(status)) return status; + surface->ContentGeneration++; + surface->CommitCount++; + surface->CommitRectCount += requestedCount; + surface->CommitClippedRectCount += clippedCount; + surface->CommitEmptyClipCount += emptyCount; + surface->LastDamageValid = clippedCount ? 1u : 0u; + if (clippedCount) { + surface->LastDamageBounds = clipped[0]; + for (uint32_t i = 1; i < clippedCount; ++i) + KiSurfaceUnionRect(&surface->LastDamageBounds, &clipped[i]); + } else { + memset(&surface->LastDamageBounds, 0, sizeof(surface->LastDamageBounds)); + } + uint64_t generation = surface->ContentGeneration; + KeReleaseMutex(surface->Lock); + + KiSurfaceStatsRecordCommit(requestedCount, clippedCount, emptyCount); + KeSignalEvent(surface->CommitEvent); + if (generationOut) *generationOut = generation; + return NTLSTATUS_SUCCESS; +} + +// Presents one surface after a caller has separately proved DisplayManager PRESENT authority. +KSTATUS KeSurfacePresent(OBJECT* object, const DISPLAY_RECT* rects, uint32_t rectCount, + uint64_t* bytesCopiedOut) { + KE_SURFACE* surface = KiSurfaceFromObject(object); + if (!surface || !bytesCopiedOut) return NTLSTATUS_INVALID_PARAMETER; + + DISPLAY_RECT clipped[SYS_SURFACE_MAX_RECTS]; + uint32_t clippedCount = 0; + uint64_t emptyCount = 0; + KSTATUS status = KiSurfaceCaptureRects(surface, rects, rectCount, clipped, &clippedCount, &emptyCount); + (void)emptyCount; + if (!KSUCCESS(status)) return status; + if (!surface->KernelBase) return NTLSTATUS_DEVICE_ERROR; + + const uint8_t* sourceBase = (const uint8_t*)surface->KernelBase; + uint64_t bytesCopied = 0; + for (uint32_t i = 0; i < clippedCount; ++i) { + const DISPLAY_RECT* rect = &clipped[i]; + const uint32_t* source = (const uint32_t*)(sourceBase + + (size_t)(uint32_t)rect->Y * surface->PitchBytes + + (size_t)(uint32_t)rect->X * sizeof(uint32_t)); + status = KeDisplayPresentPitched(rect->X, rect->Y, rect->W, rect->H, source, surface->PitchBytes); + if (!KSUCCESS(status)) return status; + bytesCopied += (uint64_t)(uint32_t)rect->W * (uint64_t)(uint32_t)rect->H * sizeof(uint32_t); + } + + *bytesCopiedOut = bytesCopied; + KiSurfaceStatsRecordPresent(bytesCopied); + return NTLSTATUS_SUCCESS; +} + +// Returns the passive event used to wait for commit-generation changes. +EVENT* KeSurfaceWaitEvent(OBJECT* object) { + KE_SURFACE* surface = KiSurfaceFromObject(object); + return surface ? surface->CommitEvent : (EVENT*)0; +} + +// Snapshots global surface telemetry. +void KeSurfaceQueryStats(SYS_SURFACE_STATS* statsOut) { + if (!statsOut) return; + uintptr_t irq = KeAcquireSpinlock(&gSurfaceStatsLock); + *statsOut = gSurfaceStats; + statsOut->Size = sizeof(*statsOut); + statsOut->Version = SYS_STATS_VERSION; + KeReleaseSpinlock(&gSurfaceStatsLock, irq); +} diff --git a/code/kernel/ke_syscall.asm b/code/kernel/ke_syscall.asm index f699770..5ae59e1 100644 --- a/code/kernel/ke_syscall.asm +++ b/code/kernel/ke_syscall.asm @@ -7,6 +7,7 @@ EXTERN KeSyscallDispatch:PROC EXTERN KiEnterTrapFrame:PROC EXTERN KiExitTrapFrame:PROC +EXTERN KiDeliverKernelApcs:PROC EXTERN KeFaultDispatchFrame:PROC KTRAP_FRAME_SIZE EQU 240 @@ -112,6 +113,12 @@ KeSyscallEntry PROC call KiEnterTrapFrame add rsp, 32 + ; The interrupt gate protects the ring transition and SWAPGS/trap-frame + ; construction. Once the thread context is fully established, syscalls are + ; ordinary interruptible PASSIVE_LEVEL execution rather than one giant + ; interrupt-disabled critical section. + sti + ; Re-order original user arguments into KeSyscallDispatch(nr,a0..a3). ; Original registers were saved in the trap frame before any C calls. sub rsp, 48 @@ -127,6 +134,14 @@ KeSyscallEntry PROC mov [rbx + TF_Rax], rax + ; Syscall return is a scheduler-safe kernel APC delivery boundary. Keep IF + ; set while normal routines run, then close the tiny machine-exit window + ; before committing the editable IRET frame and restoring user GS. + sub rsp, 32 + call KiDeliverKernelApcs + add rsp, 32 + cli + sub rsp, 32 mov rcx, rbx call KiExitTrapFrame diff --git a/code/kernel/ke_syscall.c b/code/kernel/ke_syscall.c index 2dd5dd5..8ddde35 100644 --- a/code/kernel/ke_syscall.c +++ b/code/kernel/ke_syscall.c @@ -63,6 +63,7 @@ #include #include #include +#include #include #include #include @@ -71,6 +72,7 @@ #include #include #include +#include #include #include #include @@ -131,154 +133,6 @@ static void KiFileMappingDestroy(OBJECT* obj) { } -typedef struct SURFACE_OBJECT { - OBJECT Header; - PROCESS* Owner; - uint32_t Width; - uint32_t Height; - uint32_t PitchBytes; - uint32_t Format; - uint32_t Flags; - size_t PayloadSize; - size_t PageCount; - uint8_t StatsRegistered; - uint64_t MapCount; - uint64_t PresentCount; - uint64_t PresentRectCount; - uint64_t PresentClippedRectCount; - uint64_t PresentEmptyClipCount; - uint64_t PresentRowsCopied; - uint64_t PresentBytesCopied; - PHYSICAL_ADDRESS* Frames; - void* UserBase; - MM_KERNEL_VIEW* KernelView; - void* KernelBase; - MUTEX* Lock; -} SURFACE_OBJECT; - -static SPINLOCK gSurfaceStatsLock = SPINLOCK_INIT; -static SYS_SURFACE_STATS gSurfaceStats; - -// Registers a surface in the global surface telemetry snapshot. -static void KiSurfaceStatsRegister(SURFACE_OBJECT* surface) { - if (!surface || surface->StatsRegistered) - return; - - uintptr_t flags = KeAcquireSpinlock(&gSurfaceStatsLock); - gSurfaceStats.CreatedSurfaces++; - gSurfaceStats.LiveSurfaces++; - gSurfaceStats.LivePages += surface->PageCount; - gSurfaceStats.LiveBytes += surface->PayloadSize; - surface->StatsRegistered = 1; - KeReleaseSpinlock(&gSurfaceStatsLock, flags); -} - -// Removes a surface from the global surface telemetry snapshot. -static void KiSurfaceStatsUnregister(SURFACE_OBJECT* surface) { - if (!surface || !surface->StatsRegistered) - return; - - uintptr_t flags = KeAcquireSpinlock(&gSurfaceStatsLock); - gSurfaceStats.DestroyedSurfaces++; - if (gSurfaceStats.LiveSurfaces) gSurfaceStats.LiveSurfaces--; - if (gSurfaceStats.LivePages >= surface->PageCount) gSurfaceStats.LivePages -= surface->PageCount; - else gSurfaceStats.LivePages = 0; - if (gSurfaceStats.LiveBytes >= surface->PayloadSize) gSurfaceStats.LiveBytes -= surface->PayloadSize; - else gSurfaceStats.LiveBytes = 0; - surface->StatsRegistered = 0; - KeReleaseSpinlock(&gSurfaceStatsLock, flags); -} - -// Records a successful surface map operation. -static void KiSurfaceStatsRecordMap(SURFACE_OBJECT* surface) { - if (!surface) - return; - - uintptr_t flags = KeAcquireSpinlock(&gSurfaceStatsLock); - surface->MapCount++; - gSurfaceStats.MapCalls++; - KeReleaseSpinlock(&gSurfaceStatsLock, flags); -} - -// Records a successful surface present operation. -static void KiSurfaceStatsRecordPresent(SURFACE_OBJECT* surface, uint64_t inputRects, uint64_t clippedRects, - uint64_t emptyClips, uint64_t rowsCopied, uint64_t bytesCopied) { - if (!surface) - return; - - uintptr_t flags = KeAcquireSpinlock(&gSurfaceStatsLock); - surface->PresentCount++; - surface->PresentRectCount += inputRects; - surface->PresentClippedRectCount += clippedRects; - surface->PresentEmptyClipCount += emptyClips; - surface->PresentRowsCopied += rowsCopied; - surface->PresentBytesCopied += bytesCopied; - gSurfaceStats.PresentCalls++; - gSurfaceStats.PresentRectInputs += inputRects; - gSurfaceStats.PresentClippedRects += clippedRects; - gSurfaceStats.PresentEmptyClips += emptyClips; - gSurfaceStats.PresentRowsCopied += rowsCopied; - gSurfaceStats.PresentBytesCopied += bytesCopied; - KeReleaseSpinlock(&gSurfaceStatsLock, flags); -} - -// Copies the global surface telemetry snapshot. -static void KiSurfaceStatsQuery(SYS_SURFACE_STATS* out) { - if (!out) - return; - - uintptr_t flags = KeAcquireSpinlock(&gSurfaceStatsLock); - *out = gSurfaceStats; - out->Size = sizeof(*out); - out->Version = SYS_STATS_VERSION; - KeReleaseSpinlock(&gSurfaceStatsLock, flags); -} - -static void KiSurfaceDestroy(OBJECT* obj) { - SURFACE_OBJECT* surface = CONTAINING_RECORD(obj, SURFACE_OBJECT, Header); - - KiSurfaceStatsUnregister(surface); - - if (surface->Owner && surface->UserBase) { - (void)MmVadDestroy(surface->Owner, surface->UserBase); - surface->UserBase = (void*)0; - } - - if (surface->KernelView) { - MmDestroyKernelView(surface->KernelView); - surface->KernelView = (MM_KERNEL_VIEW*)0; - surface->KernelBase = (void*)0; - } - - if (surface->Lock) { - ObDereferenceObject(&surface->Lock->Header); - surface->Lock = (MUTEX*)0; - } - - if (surface->Frames) { - for (size_t i = 0; i < surface->PageCount; ++i) { - if (surface->Frames[i]) - (void)MmPfnRelease(surface->Frames[i]); - } - MmFreePool(surface->Frames); - surface->Frames = (PHYSICAL_ADDRESS*)0; - } - -} - -// Creates the process mapping used for user drawing. -static KSTATUS KiSurfaceEnsureViewLocked(SURFACE_OBJECT* surface) { - if (!surface || !surface->Owner || !surface->Frames || !surface->PageCount) - return NTLSTATUS_INVALID_PARAMETER; - if (surface->UserBase) - return NTLSTATUS_SUCCESS; - - return MmVadCreateSharedViewFromFrames(surface->Owner, surface->Frames, surface->PageCount, - surface->PayloadSize, - PAGE_PRESENT | PAGE_WRITE | PAGE_USER | PAGE_NX, - MM_VAD_ZERO, MmVadShared, &surface->UserBase); -} - // Implements the syscall Init operation. void KeSyscallInit(const HAL_API* hal) { gHal = hal; @@ -497,7 +351,9 @@ static uint64_t SyscallCreateThread(const SYS_CREATE_THREAD_REQUEST* request) { if (req.Priority >= PriorityCount) return NTLSTATUS_INVALID_PARAMETER; if (req.Flags & ~SYS_CREATE_THREAD_FLAG_SUSPENDED) return NTLSTATUS_INVALID_PARAMETER; - if (!req.ThreadHandleOut && !req.ThreadIdOut) return NTLSTATUS_INVALID_PARAMETER; + // Running threads may be detached: callers that do not need a handle or TID + // can leave both outputs null. A suspended thread still requires a handle so + // there is a capability with which to resume or cancel it. if ((req.Flags & SYS_CREATE_THREAD_FLAG_SUSPENDED) && !req.ThreadHandleOut) return NTLSTATUS_INVALID_PARAMETER; @@ -622,6 +478,9 @@ static KSTATUS KiHandleToWaitEvent(PROCESS* proc, SYS_HANDLE handle, EVENT** eve case ObjectTypeTimer: event = KeTimerWaitEvent(CONTAINING_RECORD(obj, KTIMER, Header)); break; + case ObjectTypeSurface: + event = KeSurfaceWaitEvent(obj); + break; case ObjectTypeDisplayClock: event = KeDisplayClockWaitEvent((DISPLAY_CLOCK_OBJECT*)obj); break; @@ -631,6 +490,9 @@ static KSTATUS KiHandleToWaitEvent(PROCESS* proc, SYS_HANDLE handle, EVENT** eve case ObjectTypePort: event = KePortWaitEvent((PORT*)obj); break; + case ObjectTypePipeEndpoint: + event = IoPipeWaitEvent((IO_PIPE_ENDPOINT*)obj); + break; default: ObDereferenceObject(obj); return NTLSTATUS_NOT_SUPPORTED; @@ -684,8 +546,12 @@ static uint64_t SyscallWaitSingle(SYS_HANDLE handle, uint32_t flags, uint64_t ti s = KiHandleToWaitEvent(proc, handle, &event, &ref); if (!KSUCCESS(s)) return s; + if (ref->Type == ObjectTypePipeEndpoint) + IoPipeBeginWait((IO_PIPE_ENDPOINT*)ref); uint32_t selected = 0; s = KeWaitForMultipleEvents(&event, 1, alertable, timeoutMs, &selected); + if (ref->Type == ObjectTypePipeEndpoint) + IoPipeEndWait((IO_PIPE_ENDPOINT*)ref); ObDereferenceObject(ref); if (s != NTLSTATUS_TIMEOUT && s != NTLSTATUS_NOT_SUPPORTED) KiFinishAlertableUserWait(alertable); @@ -726,12 +592,19 @@ static uint64_t SyscallWaitMany(const SYS_WAIT_MANY_REQUEST* request) { } } + for (uint32_t i = 0; i < req.Count; i++) + if (refs[i]->Type == ObjectTypePipeEndpoint) + IoPipeBeginWait((IO_PIPE_ENDPOINT*)refs[i]); + uint8_t alertable = (req.Flags & SYS_WAIT_FLAG_ALERTABLE) ? 1 : 0; uint32_t selected = 0; s = KeWaitForMultipleEvents(events, req.Count, alertable, req.TimeoutMilliseconds, &selected); - for (uint32_t i = 0; i < req.Count; i++) + for (uint32_t i = 0; i < req.Count; i++) { + if (refs[i]->Type == ObjectTypePipeEndpoint) + IoPipeEndWait((IO_PIPE_ENDPOINT*)refs[i]); ObDereferenceObject(refs[i]); + } if (KSUCCESS(s) && req.SignaledIndexOut) { KSTATUS cs = KiCopyU32ToUser(req.SignaledIndexOut, selected); @@ -954,6 +827,76 @@ static uint64_t SyscallSetThreadPriority(uint32_t priority) { return KiSchedulerSetNormalPriority(t, (THREAD_PRIORITY)priority); } +// Implements SYS_SET_THREAD_NAME. +static uint64_t SyscallSetThreadName(const SYS_SET_THREAD_NAME_REQUEST* userRequest) { + SYS_SET_THREAD_NAME_REQUEST request; + KSTATUS status = KiCopySizedUserRequest(userRequest, &request, sizeof(request)); + if (!KSUCCESS(status)) return status; + + char name[SYS_THREAD_NAME_MAX]; + SYS_STRING_VIEW importedName; + status = KiImportUserTextView(request.Name, name, sizeof(name), &importedName, true); + if (!KSUCCESS(status)) return status; + + THREAD* thread = (THREAD*)0; + OBJECT* referencedObject = (OBJECT*)0; + if (request.ThreadHandle == SYS_HANDLE_INVALID) { + thread = KeGetCurrentThread(); + if (!thread) return NTLSTATUS_FAILURE; + } else { + PROCESS* process = KeGetCurrentProcess(); + if (!process) return NTLSTATUS_FAILURE; + status = KeReferenceHandle(process, request.ThreadHandle, ObjectTypeThread, + SYS_THREAD_RIGHT_SET_INFORMATION, &referencedObject); + if (!KSUCCESS(status)) return status; + thread = CONTAINING_RECORD(referencedObject, THREAD, Header); + } + + status = KeSetThreadName(thread, importedName.Data, importedName.Length); + if (referencedObject) ObDereferenceObject(referencedObject); + return status; +} + +// Implements SYS_QUERY_THREAD_NAME. +static uint64_t SyscallQueryThreadName(SYS_HANDLE threadHandle, + char* buffer, + size_t bufferSize, + size_t* lengthOut) { + if (!lengthOut || bufferSize > SYS_THREAD_NAME_MAX || (bufferSize && !buffer)) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = MmProbeUserWrite(lengthOut, sizeof(*lengthOut)); + if (!KSUCCESS(status)) return status; + if (bufferSize) { + status = MmProbeUserWrite(buffer, bufferSize); + if (!KSUCCESS(status)) return status; + } + + THREAD* thread = (THREAD*)0; + OBJECT* referencedObject = (OBJECT*)0; + if (threadHandle == SYS_HANDLE_INVALID) { + thread = KeGetCurrentThread(); + if (!thread) return NTLSTATUS_FAILURE; + } else { + PROCESS* process = KeGetCurrentProcess(); + if (!process) return NTLSTATUS_FAILURE; + status = KeReferenceHandle(process, threadHandle, ObjectTypeThread, + SYS_RIGHT_QUERY, &referencedObject); + if (!KSUCCESS(status)) return status; + thread = CONTAINING_RECORD(referencedObject, THREAD, Header); + } + + char name[SYS_THREAD_NAME_MAX]; + size_t length = KeQueryThreadName(thread, name, sizeof(name)); + if (referencedObject) ObDereferenceObject(referencedObject); + + status = KiCopySizeToUser(lengthOut, length); + if (!KSUCCESS(status)) return status; + if (bufferSize < length + 1u) + return NTLSTATUS_BUFFER_TOO_SMALL; + return CopyToUser(buffer, name, length + 1u); +} + // SYS_VM_* page-granular user virtual memory. static KSTATUS KiVmProtectToPageFlags(uint32_t protect, uint64_t* flagsOut) { if (!flagsOut) return NTLSTATUS_INVALID_PARAMETER; @@ -1635,7 +1578,7 @@ static uint64_t SyscallOpenFileAt(const SYS_OPEN_FILE_AT_REQUEST* request) { status = KiImportUserTextView(req.Path, pathBuffer, sizeof(pathBuffer), &path, true); if (!KSUCCESS(status)) return status; - bool requireModify = (req.Access & (SYS_FILE_ACCESS_WRITE | SYS_FILE_ACCESS_APPEND | SYS_FILE_ACCESS_LOCK)) != 0 || + bool requireModify = (req.Access & (SYS_FILE_ACCESS_WRITE | SYS_FILE_ACCESS_APPEND)) != 0 || req.Disposition != SYS_FILE_OPEN_EXISTING; KSTATUS handleStatus = NTLSTATUS_SUCCESS; OBJECT* root = KiReferencePathRoot(req.RootHandle, requireModify, &handleStatus); @@ -2331,294 +2274,176 @@ static uint64_t SyscallOpenDisplayClock(const SYS_OPEN_DISPLAY_CLOCK_REQUEST* re return s; } -// Implements SYS_CREATE_SURFACE. -static uint64_t SyscallCreateSurface(const SYS_CREATE_SURFACE_REQUEST* request) { - SYS_CREATE_SURFACE_REQUEST req; - KSTATUS s = KiCopySizedUserRequest(request, &req, sizeof(req)); - if (!KSUCCESS(s)) return s; - - uint32_t width = req.Width; - uint32_t height = req.Height; - uint32_t format = req.Format; - uint32_t flags = req.Flags; - if (!width || !height || !req.HandleOut) +// Copies one optional surface rectangle list from user memory. A zero count means the whole surface. +static KSTATUS KiCaptureUserSurfaceRects(const DISPLAY_RECT* userRects, uint32_t rectCount, + DISPLAY_RECT* rectsOut) { + if (rectCount > SYS_SURFACE_MAX_RECTS || (rectCount && (!userRects || !rectsOut))) return NTLSTATUS_INVALID_PARAMETER; - if (format != SYS_SURFACE_FORMAT_XRGB8888 || flags != SYS_SURFACE_FLAG_NONE) - return NTLSTATUS_NOT_SUPPORTED; - - DISPLAY_MODE mode; - s = KeDisplayGetMode(&mode); - if (!KSUCCESS(s)) - return s; - if (width > mode.Width || height > mode.Height) - return NTLSTATUS_INVALID_PARAMETER; - - size_t rowBytes = 0; - if (!KiMulSize((size_t)width, sizeof(uint32_t), &rowBytes)) - return NTLSTATUS_INVALID_PARAMETER; - size_t pitchBytes = KiAlignUpSize(rowBytes, 64); - if (pitchBytes < rowBytes) - return NTLSTATUS_INVALID_PARAMETER; - - size_t payloadSize = 0; - if (!KiMulSize(pitchBytes, (size_t)height, &payloadSize)) - return NTLSTATUS_INVALID_PARAMETER; - size_t pageCount = (payloadSize + PAGE_SIZE - 1) / PAGE_SIZE; - if (!pageCount || pageCount > (SIZE_MAX / sizeof(PHYSICAL_ADDRESS))) - return NTLSTATUS_INVALID_PARAMETER; - - SURFACE_OBJECT* surface = (SURFACE_OBJECT*)ObAllocateObject(ObjectTypeSurface, sizeof(SURFACE_OBJECT)); - if (!surface) - return NTLSTATUS_NO_MEMORY; - surface->Header.Destroy = KiSurfaceDestroy; - surface->Owner = KeGetCurrentProcess(); - surface->Width = width; - surface->Height = height; - surface->PitchBytes = (uint32_t)pitchBytes; - surface->Format = format; - surface->Flags = flags; - surface->PayloadSize = payloadSize; - surface->PageCount = pageCount; - - surface->Frames = (PHYSICAL_ADDRESS*)MmAllocatePoolTag(pageCount * sizeof(PHYSICAL_ADDRESS), MM_POOL_TAG('s', 'f', 'r', 'm')); - s = KeMutexCreate(&surface->Lock); - if (!surface->Frames || !KSUCCESS(s)) { - ObDereferenceObject(&surface->Header); - return NTLSTATUS_NO_MEMORY; - } - memset(surface->Frames, 0, pageCount * sizeof(PHYSICAL_ADDRESS)); - - for (size_t i = 0; i < pageCount; ++i) { - PHYSICAL_ADDRESS frame = MmPfnAllocate(MmPfnUseSurface); - if (!frame) { - ObDereferenceObject(&surface->Header); - return NTLSTATUS_NO_MEMORY; - } - surface->Frames[i] = frame; - void* kva = MmPfnToVirtual(frame); - if (kva) - memset(kva, 0, PAGE_SIZE); - } - - s = MmCreateKernelViewFromFrames(surface->Frames, surface->PageCount, - &surface->KernelView); - if (!KSUCCESS(s)) { - ObDereferenceObject(&surface->Header); - return s; - } - surface->KernelBase = MmKernelViewBase(surface->KernelView); - - SYS_HANDLE handle = SYS_HANDLE_INVALID; - s = KiAllocHandleForCurrentWithRights(&surface->Header, KeGetValidHandleRights(ObjectTypeSurface), &handle); - if (!KSUCCESS(s)) - return s; - - KiSurfaceStatsRegister(surface); - - s = CopyToUser(req.HandleOut, &handle, sizeof(handle)); - if (!KSUCCESS(s)) { - KeCloseHandle(KeGetCurrentProcess(), handle); - return s; - } - return NTLSTATUS_SUCCESS; + if (!rectCount) return NTLSTATUS_SUCCESS; + return CopyFromUser(rectsOut, userRects, (size_t)rectCount * sizeof(DISPLAY_RECT)); } -// Implements SYS_MAP_SURFACE. -static uint64_t SyscallMapSurface(SYS_HANDLE surfaceHandle, void** baseOut, uint32_t* pitchBytesOut) { - if (!baseOut || !pitchBytesOut) +// Implements SYS_CREATE_SURFACE by creating storage and returning its root capability. +static uint64_t SyscallCreateSurface(const SYS_CREATE_SURFACE_REQUEST* request) { + SYS_CREATE_SURFACE_REQUEST req; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; + if (!req.HandleOut) return NTLSTATUS_INVALID_PARAMETER; + + OBJECT* surface = (OBJECT*)0; + status = KeSurfaceCreate(req.Width, req.Height, req.Format, req.Flags, &surface); + if (!KSUCCESS(status)) return status; + + SYS_HANDLE handle = SYS_HANDLE_INVALID; + status = KiAllocHandleForCurrentWithRights(surface, KeGetValidHandleRights(ObjectTypeSurface), &handle); + if (!KSUCCESS(status)) return status; + status = CopyToUser(req.HandleOut, &handle, sizeof(handle)); + if (!KSUCCESS(status)) + KeCloseHandle(KeGetCurrentProcess(), handle); + return status; +} + +// Implements SYS_MAP_SURFACE. Each process receives its own VAD over shared backing frames. +static uint64_t SyscallMapSurface(SYS_HANDLE surfaceHandle, uint32_t flags, + void** baseOut, uint32_t* pitchBytesOut) { + if (!baseOut || !pitchBytesOut || !(flags & SYS_SURFACE_MAP_READ) || + (flags & ~(SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE))) return NTLSTATUS_INVALID_PARAMETER; + SYS_RIGHTS rights = SYS_SURFACE_RIGHT_MAP_READ; + if (flags & SYS_SURFACE_MAP_WRITE) rights |= SYS_SURFACE_RIGHT_MAP_WRITE; KSTATUS handleStatus = NTLSTATUS_SUCCESS; - SURFACE_OBJECT* surface = (SURFACE_OBJECT*)KiHandleToObject(surfaceHandle, ObjectTypeSurface, - SYS_SURFACE_RIGHT_MAP, &handleStatus); - if (!surface) - return handleStatus; + OBJECT* surface = KiHandleToObject(surfaceHandle, ObjectTypeSurface, rights, &handleStatus); + if (!surface) return handleStatus; - PROCESS* current = KeGetCurrentProcess(); - if (surface->Owner != current) { - ObDereferenceObject(&surface->Header); - return NTLSTATUS_ACCESS_DENIED; - } + void* userBase = (void*)0; + uint32_t pitchBytes = 0; + KSTATUS status = KeSurfaceMap(KeGetCurrentProcess(), surface, flags, &userBase, &pitchBytes); + if (KSUCCESS(status)) status = CopyToUser(pitchBytesOut, &pitchBytes, sizeof(pitchBytes)); + if (KSUCCESS(status)) status = CopyToUser(baseOut, &userBase, sizeof(userBase)); + if (!KSUCCESS(status) && userBase) + (void)KeSurfaceUnmap(KeGetCurrentProcess(), userBase); + ObDereferenceObject(surface); + return status; +} - KSTATUS s = KeAcquireMutex(surface->Lock, KE_WAIT_INFINITE); - if (!KE_MUTEX_ACQUIRE_SUCCEEDED(s)) { - ObDereferenceObject(&surface->Header); - return s; - } - - s = KiSurfaceEnsureViewLocked(surface); - - void* userBase = surface->UserBase; - uint32_t pitchBytes = surface->PitchBytes; - KeReleaseMutex(surface->Lock); - - if (KSUCCESS(s)) - s = CopyToUser(baseOut, &userBase, sizeof(userBase)); - if (KSUCCESS(s)) - s = CopyToUser(pitchBytesOut, &pitchBytes, sizeof(pitchBytes)); - if (KSUCCESS(s)) - KiSurfaceStatsRecordMap(surface); - - ObDereferenceObject(&surface->Header); - return s; +// Implements SYS_UNMAP_SURFACE without accepting arbitrary VADs as surface mappings. +static uint64_t SyscallUnmapSurface(void* base) { + return KeSurfaceUnmap(KeGetCurrentProcess(), base); } // Implements SYS_QUERY_SURFACE. static uint64_t SyscallQuerySurface(SYS_HANDLE surfaceHandle, SYS_SURFACE_DESC* descOut) { - if (!descOut) - return NTLSTATUS_INVALID_PARAMETER; - + if (!descOut) return NTLSTATUS_INVALID_PARAMETER; KSTATUS handleStatus = NTLSTATUS_SUCCESS; - SURFACE_OBJECT* surface = (SURFACE_OBJECT*)KiHandleToObject(surfaceHandle, ObjectTypeSurface, - SYS_RIGHT_QUERY, &handleStatus); - if (!surface) - return handleStatus; - - if (surface->Owner != KeGetCurrentProcess()) { - ObDereferenceObject(&surface->Header); - return NTLSTATUS_ACCESS_DENIED; - } + OBJECT* surface = KiHandleToObject(surfaceHandle, ObjectTypeSurface, SYS_RIGHT_QUERY, &handleStatus); + if (!surface) return handleStatus; SYS_SURFACE_DESC desc; - memset(&desc, 0, sizeof(desc)); - - KSTATUS s = KeAcquireMutex(surface->Lock, KE_WAIT_INFINITE); - if (KE_MUTEX_ACQUIRE_SUCCEEDED(s)) { - desc.Width = surface->Width; - desc.Height = surface->Height; - desc.PitchBytes = surface->PitchBytes; - desc.Format = surface->Format; - desc.Base = surface->UserBase; - desc.PayloadBytes = surface->PayloadSize; - desc.PageCount = surface->PageCount; - KeReleaseMutex(surface->Lock); - - uintptr_t statsFlags = KeAcquireSpinlock(&gSurfaceStatsLock); - desc.MapCount = surface->MapCount; - desc.PresentCount = surface->PresentCount; - desc.PresentRectCount = surface->PresentRectCount; - desc.PresentClippedRectCount = surface->PresentClippedRectCount; - desc.PresentEmptyClipCount = surface->PresentEmptyClipCount; - desc.PresentRowsCopied = surface->PresentRowsCopied; - desc.PresentBytesCopied = surface->PresentBytesCopied; - KeReleaseSpinlock(&gSurfaceStatsLock, statsFlags); - - s = CopyToUser(descOut, &desc, sizeof(desc)); - } - - ObDereferenceObject(&surface->Header); - return s; + KSTATUS status = KeSurfaceQuery(surface, &desc); + ObDereferenceObject(surface); + if (!KSUCCESS(status)) return status; + return CopyToUser(descOut, &desc, sizeof(desc)); } -static int KiClipSurfaceRect(const SURFACE_OBJECT* surface, const DISPLAY_RECT* in, DISPLAY_RECT* out) { - if (!surface || !out) - return 0; - DISPLAY_RECT r; - if (in) { - r = *in; - } else { - r.X = 0; - r.Y = 0; - r.W = (int32_t)surface->Width; - r.H = (int32_t)surface->Height; - } - if (r.W <= 0 || r.H <= 0) - return 0; - - int64_t x0 = r.X; - int64_t y0 = r.Y; - int64_t x1 = x0 + r.W; - int64_t y1 = y0 + r.H; - if (x0 < 0) x0 = 0; - if (y0 < 0) y0 = 0; - if (x1 > surface->Width) x1 = surface->Width; - if (y1 > surface->Height) y1 = surface->Height; - if (x0 >= x1 || y0 >= y1) - return 0; - - out->X = (int32_t)x0; - out->Y = (int32_t)y0; - out->W = (int32_t)(x1 - x0); - out->H = (int32_t)(y1 - y0); - return 1; -} - -// Implements SYS_PRESENT_SURFACE. -static uint64_t SyscallPresentSurface(SYS_HANDLE surfaceHandle, const DISPLAY_RECT* userRects, uint32_t rectCount) { - KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_DISPLAY_ACCESS); - if (!KSUCCESS(privilegeStatus)) return privilegeStatus; - if (rectCount > SYS_SURFACE_MAX_RECTS) - return NTLSTATUS_INVALID_PARAMETER; - - if (rectCount != 0 && !userRects) - return NTLSTATUS_INVALID_PARAMETER; +// Implements SYS_COMMIT_SURFACE. Commit announces new pixels and never touches scanout. +static uint64_t SyscallCommitSurface(SYS_HANDLE surfaceHandle, const DISPLAY_RECT* userRects, + uint32_t rectCount, uint64_t* generationOut) { + DISPLAY_RECT rects[SYS_SURFACE_MAX_RECTS]; + KSTATUS status = KiCaptureUserSurfaceRects(userRects, rectCount, rects); + if (!KSUCCESS(status)) return status; KSTATUS handleStatus = NTLSTATUS_SUCCESS; - SURFACE_OBJECT* surface = (SURFACE_OBJECT*)KiHandleToObject(surfaceHandle, ObjectTypeSurface, - SYS_SURFACE_RIGHT_PRESENT, &handleStatus); - if (!surface) - return handleStatus; - if (surface->Owner != KeGetCurrentProcess()) { - ObDereferenceObject(&surface->Header); - return NTLSTATUS_ACCESS_DENIED; - } + OBJECT* surface = KiHandleToObject(surfaceHandle, ObjectTypeSurface, + SYS_SURFACE_RIGHT_COMMIT, &handleStatus); + if (!surface) return handleStatus; - DISPLAY_RECT rects[SYS_SURFACE_MAX_RECTS]; - if (rectCount == 0) { - rects[0].X = 0; - rects[0].Y = 0; - rects[0].W = (int32_t)surface->Width; - rects[0].H = (int32_t)surface->Height; - rectCount = 1; - } else { - KSTATUS cs = CopyFromUser(rects, userRects, (size_t)rectCount * sizeof(DISPLAY_RECT)); - if (!KSUCCESS(cs)) { - ObDereferenceObject(&surface->Header); - return cs; - } - } - - uint64_t inputRects = (uint64_t)rectCount; - uint64_t clippedRects = 0; - uint64_t emptyClips = 0; - uint64_t rowsCopied = 0; - uint64_t bytesCopied = 0; - - KSTATUS s = surface->KernelBase ? NTLSTATUS_SUCCESS : NTLSTATUS_DEVICE_ERROR; - const uint8_t* sourceBase = (const uint8_t*)surface->KernelBase; - - for (uint32_t i = 0; i < rectCount && KSUCCESS(s); ++i) { - DISPLAY_RECT clipped; - if (!KiClipSurfaceRect(surface, &rects[i], &clipped)) { - emptyClips++; - continue; - } - - const uint32_t* source = (const uint32_t*)(sourceBase + - (size_t)(uint32_t)clipped.Y * surface->PitchBytes + - (size_t)(uint32_t)clipped.X * sizeof(uint32_t)); - s = KeDisplayPresentPitched(clipped.X, clipped.Y, clipped.W, clipped.H, - source, surface->PitchBytes); - if (!KSUCCESS(s)) - break; - - clippedRects++; - rowsCopied += (uint64_t)(uint32_t)clipped.H; - bytesCopied += (uint64_t)(uint32_t)clipped.W * (uint64_t)(uint32_t)clipped.H * sizeof(uint32_t); - } - - if (KSUCCESS(s)) - KiSurfaceStatsRecordPresent(surface, inputRects, clippedRects, emptyClips, rowsCopied, bytesCopied); - - ObDereferenceObject(&surface->Header); - return s; + uint64_t generation = 0; + status = KeSurfaceCommit(surface, rectCount ? rects : (DISPLAY_RECT*)0, rectCount, &generation); + ObDereferenceObject(surface); + if (KSUCCESS(status) && generationOut) + status = CopyToUser(generationOut, &generation, sizeof(generation)); + return status; } -// Implements SYS_CLEAR_SCREEN. +// Clears the legacy text-console display after its caller has already been authorized. +static KSTATUS KiConsoleClear(uint32_t color) { + return KeDisplayClear(color); +} + +// Returns the current console geometry and cursor state after authorization. +static KSTATUS KiConsoleQueryInfo(SYS_CONSOLE_INFO* infoOut) { + if (!infoOut) return NTLSTATUS_INVALID_PARAMETER; + + KE_CONSOLE_INFO ki; + KSTATUS status = KeDisplayGetConsoleInfo(&ki); + if (!KSUCCESS(status)) return status; + + SYS_CONSOLE_INFO out; + memset(&out, 0, sizeof(out)); + out.Columns = ki.Columns; + out.Rows = ki.Rows; + out.CursorX = ki.CursorX; + out.CursorY = ki.CursorY; + out.Attribute = ki.Attribute; + return CopyToUser(infoOut, &out, sizeof(out)); +} + +// Updates the legacy text-console cursor after authorization. +static KSTATUS KiConsoleSetCursor(int x, int y) { + if (x < 0 || y < 0) return NTLSTATUS_INVALID_PARAMETER; + return KeDisplaySetConsoleCursor(x, y); +} + +// Draws one console glyph at explicit pixel coordinates after authorization. +static KSTATUS KiConsolePutCharacter(const SYS_PUT_SCREEN_CHAR_REQUEST* request) { + SYS_PUT_SCREEN_CHAR_REQUEST req; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; + if (req.Reserved[0] || req.Reserved[1] || req.Reserved[2] || req.X < 0 || req.Y < 0) + return NTLSTATUS_INVALID_PARAMETER; + return KeDisplayPutChar(req.Character, req.X, req.Y, req.Foreground, req.Background); +} + +// Fills console characters over one linear range after authorization. +static KSTATUS KiConsoleFillCharacter(const SYS_FILL_CONSOLE_CHARACTER_REQUEST* request) { + SYS_FILL_CONSOLE_CHARACTER_REQUEST req; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; + if (req.Reserved[0] || req.Reserved[1] || req.Reserved[2] || req.X < 0 || req.Y < 0) + return NTLSTATUS_INVALID_PARAMETER; + + size_t written = 0; + status = KeDisplayFillConsoleChars(req.Character, req.X, req.Y, req.Length, &written); + if (req.WrittenOut) { + KSTATUS copyStatus = KiCopySizeToUser(req.WrittenOut, written); + if (KSUCCESS(status) && !KSUCCESS(copyStatus)) status = copyStatus; + } + return status; +} + +// Fills console attributes over one linear range after authorization. +static KSTATUS KiConsoleFillAttribute(const SYS_FILL_CONSOLE_ATTRIBUTE_REQUEST* request) { + SYS_FILL_CONSOLE_ATTRIBUTE_REQUEST req; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; + if (req.Reserved != 0 || req.X < 0 || req.Y < 0) + return NTLSTATUS_INVALID_PARAMETER; + + size_t written = 0; + status = KeDisplayFillConsoleAttrs(req.Attribute, req.X, req.Y, req.Length, &written); + if (req.WrittenOut) { + KSTATUS copyStatus = KiCopySizeToUser(req.WrittenOut, written); + if (KSUCCESS(status) && !KSUCCESS(copyStatus)) status = copyStatus; + } + return status; +} + +// Implements SYS_CLEAR_SCREEN through the legacy ambient console privilege. static uint64_t SyscallClearScreen(uint32_t color) { KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_CONSOLE_ACCESS); if (!KSUCCESS(privilegeStatus)) return privilegeStatus; - return KeDisplayClear(color); + return KiConsoleClear(color); } // Implements SYS_GET_SCREEN_SIZE. @@ -2639,91 +2464,39 @@ static uint64_t SyscallGetScreenSize(int* widthOut, int* heightOut) { return NTLSTATUS_SUCCESS; } -// Implements SYS_PUT_SCREEN_CHAR. +// Implements SYS_PUT_SCREEN_CHAR through the legacy ambient console privilege. static uint64_t SyscallPutScreenChar(const SYS_PUT_SCREEN_CHAR_REQUEST* request) { KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_CONSOLE_ACCESS); if (!KSUCCESS(privilegeStatus)) return privilegeStatus; - SYS_PUT_SCREEN_CHAR_REQUEST req; - KSTATUS s = KiCopySizedUserRequest(request, &req, sizeof(req)); - if (!KSUCCESS(s)) return s; - if (req.Reserved[0] || req.Reserved[1] || req.Reserved[2]) - return NTLSTATUS_INVALID_PARAMETER; - if (req.X < 0 || req.Y < 0) - return NTLSTATUS_INVALID_PARAMETER; - return KeDisplayPutChar(req.Character, req.X, req.Y, - req.Foreground, req.Background); + return KiConsolePutCharacter(request); } -// Returns the current console geometry and cursor state. +// Implements SYS_GET_CONSOLE_INFO through the legacy ambient console privilege. static uint64_t SyscallGetConsoleInfo(SYS_CONSOLE_INFO* infoOut) { KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_CONSOLE_ACCESS); if (!KSUCCESS(privilegeStatus)) return privilegeStatus; - if (!infoOut) return NTLSTATUS_INVALID_PARAMETER; - - KE_CONSOLE_INFO ki; - KSTATUS s = KeDisplayGetConsoleInfo(&ki); - if (!KSUCCESS(s)) return s; - - SYS_CONSOLE_INFO out; - memset(&out, 0, sizeof(out)); - out.Columns = ki.Columns; - out.Rows = ki.Rows; - out.CursorX = ki.CursorX; - out.CursorY = ki.CursorY; - out.Attribute = ki.Attribute; - return CopyToUser(infoOut, &out, sizeof(out)); + return KiConsoleQueryInfo(infoOut); } -// Implements SYS_SET_CONSOLE_CURSOR. +// Implements SYS_SET_CONSOLE_CURSOR through the legacy ambient console privilege. static uint64_t SyscallSetConsoleCursor(int x, int y) { KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_CONSOLE_ACCESS); if (!KSUCCESS(privilegeStatus)) return privilegeStatus; - int col = (int)x; - int row = (int)y; - if (col < 0 || row < 0) return NTLSTATUS_INVALID_PARAMETER; - return KeDisplaySetConsoleCursor(col, row); + return KiConsoleSetCursor(x, y); } -// Implements SYS_FILL_CONSOLE_CHARACTER. +// Implements SYS_FILL_CONSOLE_CHARACTER through the legacy ambient console privilege. static uint64_t SyscallFillConsoleCharacter(const SYS_FILL_CONSOLE_CHARACTER_REQUEST* request) { KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_CONSOLE_ACCESS); if (!KSUCCESS(privilegeStatus)) return privilegeStatus; - SYS_FILL_CONSOLE_CHARACTER_REQUEST req; - KSTATUS s = KiCopySizedUserRequest(request, &req, sizeof(req)); - if (!KSUCCESS(s)) return s; - if (req.Reserved[0] || req.Reserved[1] || req.Reserved[2]) - return NTLSTATUS_INVALID_PARAMETER; - if (req.X < 0 || req.Y < 0) - return NTLSTATUS_INVALID_PARAMETER; - - size_t written = 0; - s = KeDisplayFillConsoleChars(req.Character, req.X, req.Y, req.Length, &written); - if (req.WrittenOut) { - KSTATUS copyStatus = KiCopySizeToUser(req.WrittenOut, written); - if (KSUCCESS(s) && !KSUCCESS(copyStatus)) s = copyStatus; - } - return s; + return KiConsoleFillCharacter(request); } -// Fills console attributes over one linear character range. +// Implements SYS_FILL_CONSOLE_ATTRIBUTE through the legacy ambient console privilege. static uint64_t SyscallFillConsoleAttribute(const SYS_FILL_CONSOLE_ATTRIBUTE_REQUEST* request) { KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_CONSOLE_ACCESS); if (!KSUCCESS(privilegeStatus)) return privilegeStatus; - SYS_FILL_CONSOLE_ATTRIBUTE_REQUEST req; - KSTATUS s = KiCopySizedUserRequest(request, &req, sizeof(req)); - if (!KSUCCESS(s)) return s; - if (req.Reserved != 0) - return NTLSTATUS_INVALID_PARAMETER; - if (req.X < 0 || req.Y < 0) - return NTLSTATUS_INVALID_PARAMETER; - - size_t written = 0; - s = KeDisplayFillConsoleAttrs(req.Attribute, req.X, req.Y, req.Length, &written); - if (req.WrittenOut) { - KSTATUS copyStatus = KiCopySizeToUser(req.WrittenOut, written); - if (KSUCCESS(s) && !KSUCCESS(copyStatus)) s = copyStatus; - } - return s; + return KiConsoleFillAttribute(request); } static KSTATUS KiReferenceInputQueueHandle(SYS_HANDLE handle, INPUT_QUEUE** queueOut) { @@ -2739,6 +2512,26 @@ static KSTATUS KiReferenceInputQueueHandle(SYS_HANDLE handle, INPUT_QUEUE** queu return NTLSTATUS_SUCCESS; } +// Implements the capability-scoped raw input-manager stream used by RSSRV. +static uint64_t SyscallReadInputManagerEvent(const SYS_INPUT_MANAGER_READ_REQUEST* request) { + SYS_INPUT_MANAGER_READ_REQUEST req; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; + if (req.Flags != SYS_INPUT_MANAGER_EVENT_FLAG_NONE || !req.EventOut) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_INPUT_MANAGER* manager = (KE_INPUT_MANAGER*)KiHandleToObject( + req.ManagerHandle, ObjectTypeInputManager, SYS_INPUT_MANAGER_RIGHT_READ, &handleStatus); + if (!manager) return handleStatus; + + SYS_INPUT_MANAGER_EVENT event; + status = KeInputManagerReadEvent(manager, req.TimeoutMilliseconds, &event); + ObDereferenceObject(&manager->Header); + if (!KSUCCESS(status)) return status; + return CopyToUser(req.EventOut, &event, sizeof(event)); +} + // Copies one keyboard input record to user mode. static KSTATUS KiCopyInputRecordToUser(INPUT_KEY_RECORD* userRecord, const INPUT_KEY_RECORD* record) { if (!userRecord || !record) return NTLSTATUS_INVALID_PARAMETER; @@ -3258,7 +3051,8 @@ static uint64_t SyscallStartProcess(const SYS_START_PROCESS_REQUEST* request) { if (KSUCCESS(status) && !KeIsProcessTerminating(child)) { child->ImageBase = req.MainImageBase; child->ImageSize = req.MainImageSize; - KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + KE_THREAD_CREATE_PARAMETERS parameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, child->Name); parameters.Flags = KE_THREAD_CREATE_FLAG_SUSPENDED; status = KeCreateThread(child, req.EntryPoint, 0, ¶meters, &thread); if (KSUCCESS(status)) { @@ -3353,14 +3147,11 @@ static uint64_t SyscallGetProcessExitCode(SYS_HANDLE handle, int32_t* exitCodeOu return KiCopyInt32ToUser(exitCodeOut, exitCode); } -// Implements SYS_WRITE_CONSOLE. -// Writes user console text and completes display delivery before returning. -static uint64_t SyscallWriteConsole(const SYS_WRITE_CONSOLE_REQUEST* request) { - KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_CONSOLE_ACCESS); - if (!KSUCCESS(privilegeStatus)) return privilegeStatus; +// Writes one user text span to the legacy kernel console after authorization. +static KSTATUS KiConsoleWrite(const SYS_WRITE_CONSOLE_REQUEST* request) { SYS_WRITE_CONSOLE_REQUEST req; - KSTATUS s = KiCopySizedUserRequest(request, &req, sizeof(req)); - if (!KSUCCESS(s)) return s; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; if (req.Text.Length == 0) return NTLSTATUS_SUCCESS; if (!req.Text.Data) return NTLSTATUS_INVALID_PARAMETER; if (req.Text.Length > SYSCALL_TEXT_MAX_BYTES) return NTLSTATUS_BUFFER_TOO_SMALL; @@ -3370,14 +3161,21 @@ static uint64_t SyscallWriteConsole(const SYS_WRITE_CONSOLE_REQUEST* request) { while (offset < req.Text.Length) { size_t todo = req.Text.Length - offset; if (todo > sizeof(chunk)) todo = sizeof(chunk); - s = CopyFromUser(chunk, req.Text.Data + offset, todo); - if (!KSUCCESS(s)) return s; + status = CopyFromUser(chunk, req.Text.Data + offset, todo); + if (!KSUCCESS(status)) return status; KePrintConsole(chunk, todo); offset += todo; } return KeDisplayFlushConsoleOutput(); } +// Implements SYS_WRITE_CONSOLE through the legacy ambient console privilege. +static uint64_t SyscallWriteConsole(const SYS_WRITE_CONSOLE_REQUEST* request) { + KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_CONSOLE_ACCESS); + if (!KSUCCESS(privilegeStatus)) return privilegeStatus; + return KiConsoleWrite(request); +} + // Implements SYS_READ_DIR. // Returns NTLSTATUS_NO_MORE_FILES when the directory is exhausted. static uint64_t SyscallReadDir(SYS_HANDLE handle, SYS_DIR_ENTRY* entryOut) { @@ -4460,6 +4258,133 @@ static uint64_t SyscallUnlockFile(const SYS_UNLOCK_FILE_REQUEST* request) { return s; } +// Implements SYS_QUERY_FILE_LOCK. +static uint64_t SyscallQueryFileLock(const SYS_QUERY_FILE_LOCK_REQUEST* request) { + SYS_QUERY_FILE_LOCK_REQUEST req; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; + if (req.Flags != SYS_FILE_LOCK_FLAG_NONE || !req.InfoOut) + return NTLSTATUS_INVALID_PARAMETER; + if (req.Mode != SYS_FILE_LOCK_SHARED && req.Mode != SYS_FILE_LOCK_EXCLUSIVE) + return NTLSTATUS_INVALID_PARAMETER; + if (req.Length == 0) + return NTLSTATUS_INVALID_PARAMETER; + + status = MmProbeUserWrite(req.InfoOut, sizeof(*req.InfoOut)); + if (!KSUCCESS(status)) return status; + + PROCESS* proc = KeGetCurrentProcess(); + if (!proc) return NTLSTATUS_FAILURE; + + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + FILE_OBJECT* file = (FILE_OBJECT*)KiHandleToObject(req.FileHandle, ObjectTypeFile, + SYS_FILE_RIGHT_LOCK, &handleStatus); + if (!file) return handleStatus; + + SYS_FILE_LOCK_INFO info; + status = IoQueryFileLock(file, proc, req.Mode, req.Offset, req.Length, &info); + ObDereferenceObject(&file->Header); + if (!KSUCCESS(status)) return status; + return CopyToUser(req.InfoOut, &info, sizeof(info)); +} + +// Implements SYS_SET_FILE_STATUS. +static uint64_t SyscallSetFileStatus(const SYS_SET_FILE_STATUS_REQUEST* request) { + SYS_SET_FILE_STATUS_REQUEST req; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; + if (req.Mask & ~SYS_FILE_STATUS_APPEND) + return NTLSTATUS_INVALID_PARAMETER; + if (req.Flags & ~req.Mask) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + FILE_OBJECT* file = (FILE_OBJECT*)KiHandleToObject(req.FileHandle, ObjectTypeFile, + SYS_RIGHT_QUERY, &handleStatus); + if (!file) return handleStatus; + status = IoSetFileStatus(file, req.Mask, req.Flags); + ObDereferenceObject(&file->Header); + return status; +} + +// Implements SYS_CREATE_PIPE. The syscall creates endpoint capabilities and one +// process-owned shared data-plane view; ordinary reads and writes stay in user mode. +static uint64_t SyscallCreatePipe(const SYS_CREATE_PIPE_REQUEST* request) { + SYS_CREATE_PIPE_REQUEST req; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; + if (req.Flags != 0 || !req.ReadHandleOut || !req.WriteHandleOut || !req.SharedStateOut) + return NTLSTATUS_INVALID_PARAMETER; + + status = MmProbeUserWrite(req.ReadHandleOut, sizeof(*req.ReadHandleOut)); + if (!KSUCCESS(status)) return status; + status = MmProbeUserWrite(req.WriteHandleOut, sizeof(*req.WriteHandleOut)); + if (!KSUCCESS(status)) return status; + status = MmProbeUserWrite(req.SharedStateOut, sizeof(*req.SharedStateOut)); + if (!KSUCCESS(status)) return status; + + IO_PIPE_ENDPOINT* readEnd = (IO_PIPE_ENDPOINT*)0; + IO_PIPE_ENDPOINT* writeEnd = (IO_PIPE_ENDPOINT*)0; + status = IoCreatePipe(&readEnd, &writeEnd); + if (!KSUCCESS(status)) return status; + + PROCESS* proc = KeGetCurrentProcess(); + void* sharedState = (void*)0; + status = IoMapPipe(readEnd, proc, &sharedState); + if (!KSUCCESS(status)) { + ObDereferenceObject((OBJECT*)readEnd); + ObDereferenceObject((OBJECT*)writeEnd); + return status; + } + + SYS_HANDLE readHandle = SYS_HANDLE_INVALID; + SYS_HANDLE writeHandle = SYS_HANDLE_INVALID; + SYS_RIGHTS readRights = SYS_RIGHT_MANAGE | SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | + SYS_PIPE_RIGHT_READ | SYS_PIPE_RIGHT_POLL; + SYS_RIGHTS writeRights = SYS_RIGHT_MANAGE | SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | + SYS_PIPE_RIGHT_WRITE | SYS_PIPE_RIGHT_POLL; + + status = KiAllocHandleForCurrentWithRights((OBJECT*)readEnd, readRights, &readHandle); + if (!KSUCCESS(status)) { + ObDereferenceObject((OBJECT*)writeEnd); + (void)MmVadDestroy(proc, sharedState); + return status; + } + status = KiAllocHandleForCurrentWithRights((OBJECT*)writeEnd, writeRights, &writeHandle); + if (!KSUCCESS(status)) { + if (proc) KeCloseHandle(proc, readHandle); + (void)MmVadDestroy(proc, sharedState); + return status; + } + + status = KiCopyHandleToUser(req.ReadHandleOut, readHandle); + if (KSUCCESS(status)) status = KiCopyHandleToUser(req.WriteHandleOut, writeHandle); + if (KSUCCESS(status)) status = CopyToUser(req.SharedStateOut, &sharedState, sizeof(sharedState)); + if (!KSUCCESS(status)) { + if (sharedState && proc) + (void)MmVadDestroy(proc, sharedState); + if (proc) { + KeCloseHandle(proc, readHandle); + KeCloseHandle(proc, writeHandle); + } + } + return status; +} + +// Implements SYS_NOTIFY_PIPE. User mode calls this only when a shared waiter +// count says a data-plane transition may need scheduler attention. +static uint64_t SyscallNotifyPipe(SYS_HANDLE pipeHandle) { + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + IO_PIPE_ENDPOINT* endpoint = (IO_PIPE_ENDPOINT*)KiHandleToObject(pipeHandle, + ObjectTypePipeEndpoint, + SYS_RIGHT_WAIT, + &handleStatus); + if (!endpoint) return handleStatus; + KSTATUS status = IoNotifyPipe(endpoint); + ObDereferenceObject((OBJECT*)endpoint); + return status; +} + typedef struct SYS_SOCKET_SEND_TO_REQUEST_K { uint32_t Size; const void* Buffer; @@ -5040,17 +4965,23 @@ static uint64_t SyscallPoll(const SYS_POLL_REQUEST* request) { for (size_t i = 0; i < count; i++) { fds[i].ReturnedEvents = 0; KSTATUS handleStatus = NTLSTATUS_SUCCESS; + uint32_t revents = 0; SOCKET_OBJECT* socket = (SOCKET_OBJECT*)KiHandleToObject( fds[i].Handle, ObjectTypeSocket, SYS_SOCKET_RIGHT_POLL, &handleStatus); - if (!socket) { - fds[i].ReturnedEvents = SYS_POLL_INVALID; - ready++; - continue; + if (socket) { + s = NetSocketPoll(socket, fds[i].Events, &revents); + ObDereferenceObject(&socket->Header); + } else { + IO_PIPE_ENDPOINT* pipe = (IO_PIPE_ENDPOINT*)KiHandleToObject( + fds[i].Handle, ObjectTypePipeEndpoint, SYS_PIPE_RIGHT_POLL, &handleStatus); + if (!pipe) { + fds[i].ReturnedEvents = SYS_POLL_INVALID; + ready++; + continue; + } + s = IoPollPipe(pipe, fds[i].Events, &revents); + ObDereferenceObject((OBJECT*)pipe); } - - uint32_t revents = 0; - s = NetSocketPoll(socket, fds[i].Events, &revents); - ObDereferenceObject(&socket->Header); if (!KSUCCESS(s)) { fds[i].ReturnedEvents = SYS_POLL_ERROR; ready++; @@ -5555,15 +5486,13 @@ static KSTATUS KiReturnKernelStats(void* buffer, size_t bufferSize, return CopyToUser(buffer, src, size); } -// Returns the current display mode and framebuffer metadata. -static uint64_t SyscallQueryDisplayInfo(SYS_DISPLAY_INFO* infoOut) { - KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_DISPLAY_ACCESS); - if (!KSUCCESS(privilegeStatus)) return privilegeStatus; +// Copies the current primary-display state through either native authority path. +static KSTATUS KiCopyDisplayInfoToUser(SYS_DISPLAY_INFO* infoOut) { if (!infoOut) return NTLSTATUS_INVALID_PARAMETER; KE_DISPLAY_INFO ki; - KSTATUS s = KeDisplayQueryInfo(&ki); - if (!KSUCCESS(s)) return s; + KSTATUS status = KeDisplayQueryInfo(&ki); + if (!KSUCCESS(status)) return status; SYS_DISPLAY_INFO out; memset(&out, 0, sizeof(out)); @@ -5574,10 +5503,163 @@ static uint64_t SyscallQueryDisplayInfo(SYS_DISPLAY_INFO* infoOut) { out.Flags = ki.Flags; out.ManagerStats = ki.ManagerStats; out.DeviceStats = ki.DeviceStats; - return CopyToUser(infoOut, &out, sizeof(out)); } +// Returns display metadata through the legacy ambient display authority. +static uint64_t SyscallQueryDisplayInfo(SYS_DISPLAY_INFO* infoOut) { + KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_DISPLAY_ACCESS); + if (!KSUCCESS(privilegeStatus)) return privilegeStatus; + return KiCopyDisplayInfoToUser(infoOut); +} + +// Returns display metadata through an explicitly delegated display-manager capability. +static uint64_t SyscallQueryDisplayManagerInfo(SYS_HANDLE managerHandle, SYS_DISPLAY_INFO* infoOut) { + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = (KE_DISPLAY_MANAGER*)KiHandleToObject( + managerHandle, ObjectTypeDisplayManager, SYS_DISPLAY_MANAGER_RIGHT_ENUMERATE, &handleStatus); + if (!manager) return handleStatus; + KSTATUS status = KiCopyDisplayInfoToUser(infoOut); + ObDereferenceObject(&manager->Header); + return status; +} + +// Presents one surface through explicitly delegated physical-display authority. +static uint64_t SyscallPresentDisplayManagerSurface(SYS_HANDLE managerHandle, SYS_HANDLE surfaceHandle, + const DISPLAY_RECT* userRects, uint32_t rectCount) { + KSTATUS managerStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = (KE_DISPLAY_MANAGER*)KiHandleToObject( + managerHandle, ObjectTypeDisplayManager, SYS_DISPLAY_MANAGER_RIGHT_PRESENT, &managerStatus); + if (!manager) return managerStatus; + + DISPLAY_RECT rects[SYS_SURFACE_MAX_RECTS]; + KSTATUS status = KiCaptureUserSurfaceRects(userRects, rectCount, rects); + if (!KSUCCESS(status)) { + ObDereferenceObject(&manager->Header); + return status; + } + + KSTATUS surfaceStatus = NTLSTATUS_SUCCESS; + OBJECT* surface = KiHandleToObject(surfaceHandle, ObjectTypeSurface, + SYS_SURFACE_RIGHT_MAP_READ, &surfaceStatus); + if (!surface) { + ObDereferenceObject(&manager->Header); + return surfaceStatus; + } + + uint64_t bytesCopied = 0; + status = KeSurfacePresent(surface, rectCount ? rects : (DISPLAY_RECT*)0, rectCount, &bytesCopied); + ObDereferenceObject(surface); + ObDereferenceObject(&manager->Header); + return status; +} + +// Updates the hardware cursor through an explicitly delegated display-manager capability. +static uint64_t SyscallSetDisplayManagerCursor(const SYS_DISPLAY_MANAGER_CURSOR_REQUEST* request) { + SYS_DISPLAY_MANAGER_CURSOR_REQUEST req; + KSTATUS status = KiCopySizedUserRequest(request, &req, sizeof(req)); + if (!KSUCCESS(status)) return status; + if (req.Flags & ~(SYS_DISPLAY_MANAGER_CURSOR_FLAG_SHOW | SYS_DISPLAY_MANAGER_CURSOR_FLAG_HIDE)) + return NTLSTATUS_INVALID_PARAMETER; + if ((req.Flags & SYS_DISPLAY_MANAGER_CURSOR_FLAG_SHOW) && + (req.Flags & SYS_DISPLAY_MANAGER_CURSOR_FLAG_HIDE)) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = (KE_DISPLAY_MANAGER*)KiHandleToObject( + req.ManagerHandle, ObjectTypeDisplayManager, SYS_DISPLAY_MANAGER_RIGHT_CURSOR, &handleStatus); + if (!manager) return handleStatus; + + status = KeDisplaySetCursorPosition(req.X, req.Y); + if (KSUCCESS(status) && (req.Flags & SYS_DISPLAY_MANAGER_CURSOR_FLAG_SHOW)) + status = KeDisplayShowCursor(true); + if (KSUCCESS(status) && (req.Flags & SYS_DISPLAY_MANAGER_CURSOR_FLAG_HIDE)) + status = KeDisplayShowCursor(false); + ObDereferenceObject(&manager->Header); + return status; +} + +// Returns a referenced display manager carrying RSSRV's console authority. +static KE_DISPLAY_MANAGER* KiReferenceDisplayManagerConsole(SYS_HANDLE managerHandle, KSTATUS* statusOut) { + return (KE_DISPLAY_MANAGER*)KiHandleToObject(managerHandle, ObjectTypeDisplayManager, + SYS_DISPLAY_MANAGER_RIGHT_CONSOLE, statusOut); +} + +// Writes text through an explicitly delegated display-manager console capability. +static uint64_t SyscallWriteDisplayManagerConsole(SYS_HANDLE managerHandle, + const SYS_WRITE_CONSOLE_REQUEST* request) { + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = KiReferenceDisplayManagerConsole(managerHandle, &handleStatus); + if (!manager) return handleStatus; + KSTATUS status = KiConsoleWrite(request); + ObDereferenceObject(&manager->Header); + return status; +} + +// Queries console state through an explicitly delegated display-manager capability. +static uint64_t SyscallQueryDisplayManagerConsoleInfo(SYS_HANDLE managerHandle, + SYS_CONSOLE_INFO* infoOut) { + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = KiReferenceDisplayManagerConsole(managerHandle, &handleStatus); + if (!manager) return handleStatus; + KSTATUS status = KiConsoleQueryInfo(infoOut); + ObDereferenceObject(&manager->Header); + return status; +} + +// Updates the console cursor through an explicitly delegated display-manager capability. +static uint64_t SyscallSetDisplayManagerConsoleCursor(SYS_HANDLE managerHandle, int x, int y) { + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = KiReferenceDisplayManagerConsole(managerHandle, &handleStatus); + if (!manager) return handleStatus; + KSTATUS status = KiConsoleSetCursor(x, y); + ObDereferenceObject(&manager->Header); + return status; +} + +// Fills console characters through an explicitly delegated display-manager capability. +static uint64_t SyscallFillDisplayManagerConsoleCharacter( + SYS_HANDLE managerHandle, const SYS_FILL_CONSOLE_CHARACTER_REQUEST* request) { + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = KiReferenceDisplayManagerConsole(managerHandle, &handleStatus); + if (!manager) return handleStatus; + KSTATUS status = KiConsoleFillCharacter(request); + ObDereferenceObject(&manager->Header); + return status; +} + +// Fills console attributes through an explicitly delegated display-manager capability. +static uint64_t SyscallFillDisplayManagerConsoleAttribute( + SYS_HANDLE managerHandle, const SYS_FILL_CONSOLE_ATTRIBUTE_REQUEST* request) { + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = KiReferenceDisplayManagerConsole(managerHandle, &handleStatus); + if (!manager) return handleStatus; + KSTATUS status = KiConsoleFillAttribute(request); + ObDereferenceObject(&manager->Header); + return status; +} + +// Draws one explicit console glyph through delegated display-manager authority. +static uint64_t SyscallPutDisplayManagerConsoleCharacter( + SYS_HANDLE managerHandle, const SYS_PUT_SCREEN_CHAR_REQUEST* request) { + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = KiReferenceDisplayManagerConsole(managerHandle, &handleStatus); + if (!manager) return handleStatus; + KSTATUS status = KiConsolePutCharacter(request); + ObDereferenceObject(&manager->Header); + return status; +} + +// Clears the console target through delegated display-manager authority. +static uint64_t SyscallClearDisplayManagerConsole(SYS_HANDLE managerHandle, uint32_t color) { + KSTATUS handleStatus = NTLSTATUS_SUCCESS; + KE_DISPLAY_MANAGER* manager = KiReferenceDisplayManagerConsole(managerHandle, &handleStatus); + if (!manager) return handleStatus; + KSTATUS status = KiConsoleClear(color); + ObDereferenceObject(&manager->Header); + return status; +} + // Queries one class of kernel diagnostic statistics. static uint64_t SyscallQueryKernelStats(uint32_t classId, void* buffer, size_t bufferSize, size_t* requiredSizeOut) { KSTATUS privilegeStatus = KiRequirePrivilege(SYS_PRIV_KERNEL_DIAGNOSTICS); @@ -5805,7 +5887,7 @@ static uint64_t SyscallQueryKernelStats(uint32_t classId, void* buffer, size_t b case SYS_KERNEL_STATS_SURFACE: { SYS_SURFACE_STATS stats; memset(&stats, 0, sizeof(stats)); - KiSurfaceStatsQuery(&stats); + KeSurfaceQueryStats(&stats); return KiReturnKernelStats(buffer, bufferSize, requiredSizeOut, &stats, sizeof(stats)); } @@ -6465,6 +6547,8 @@ static const SYSCALL_ENTRY gSyscallTable[SYS_MAX] = { [SYS_GET_CURRENT_TID] = SYSCALL_ENTRY0(SyscallGetCurrentTid), [SYS_GET_CURRENT_PID] = SYSCALL_ENTRY0(SyscallGetCurrentPid), [SYS_SET_THREAD_PRIORITY] = SYSCALL_ENTRY1(SyscallSetThreadPriority), + [SYS_SET_THREAD_NAME] = SYSCALL_ENTRY1(SyscallSetThreadName), + [SYS_QUERY_THREAD_NAME] = SYSCALL_ENTRY4(SyscallQueryThreadName), [SYS_YIELD] = SYSCALL_ENTRY0(SyscallYield), [SYS_SLEEP] = SYSCALL_ENTRY1(SyscallSleep), [SYS_WAIT_SINGLE] = SYSCALL_ENTRY3(SyscallWaitSingle), @@ -6549,6 +6633,10 @@ static const SYSCALL_ENTRY gSyscallTable[SYS_MAX] = { [SYS_FLUSH_FILE] = SYSCALL_ENTRY1(SyscallFlushFile), [SYS_LOCK_FILE] = SYSCALL_ENTRY1(SyscallLockFile), [SYS_UNLOCK_FILE] = SYSCALL_ENTRY1(SyscallUnlockFile), + [SYS_QUERY_FILE_LOCK] = SYSCALL_ENTRY1(SyscallQueryFileLock), + [SYS_SET_FILE_STATUS] = SYSCALL_ENTRY1(SyscallSetFileStatus), + [SYS_CREATE_PIPE] = SYSCALL_ENTRY1(SyscallCreatePipe), + [SYS_NOTIFY_PIPE] = SYSCALL_ENTRY1(SyscallNotifyPipe), [SYS_READ_DIR] = SYSCALL_ENTRY2(SyscallReadDir), // Console, display, and surfaces. @@ -6561,11 +6649,22 @@ static const SYSCALL_ENTRY gSyscallTable[SYS_MAX] = { [SYS_CLEAR_SCREEN] = SYSCALL_ENTRY1(SyscallClearScreen), [SYS_GET_SCREEN_SIZE] = SYSCALL_ENTRY2(SyscallGetScreenSize), [SYS_QUERY_DISPLAY_INFO] = SYSCALL_ENTRY1(SyscallQueryDisplayInfo), + [SYS_QUERY_DISPLAY_MANAGER_INFO] = SYSCALL_ENTRY2(SyscallQueryDisplayManagerInfo), + [SYS_PRESENT_DISPLAY_MANAGER_SURFACE] = SYSCALL_ENTRY4(SyscallPresentDisplayManagerSurface), + [SYS_SET_DISPLAY_MANAGER_CURSOR] = SYSCALL_ENTRY1(SyscallSetDisplayManagerCursor), + [SYS_WRITE_DISPLAY_MANAGER_CONSOLE] = SYSCALL_ENTRY2(SyscallWriteDisplayManagerConsole), + [SYS_QUERY_DISPLAY_MANAGER_CONSOLE_INFO] = SYSCALL_ENTRY2(SyscallQueryDisplayManagerConsoleInfo), + [SYS_SET_DISPLAY_MANAGER_CONSOLE_CURSOR] = SYSCALL_ENTRY3(SyscallSetDisplayManagerConsoleCursor), + [SYS_FILL_DISPLAY_MANAGER_CONSOLE_CHARACTER] = SYSCALL_ENTRY2(SyscallFillDisplayManagerConsoleCharacter), + [SYS_FILL_DISPLAY_MANAGER_CONSOLE_ATTRIBUTE] = SYSCALL_ENTRY2(SyscallFillDisplayManagerConsoleAttribute), + [SYS_PUT_DISPLAY_MANAGER_CONSOLE_CHARACTER] = SYSCALL_ENTRY2(SyscallPutDisplayManagerConsoleCharacter), + [SYS_CLEAR_DISPLAY_MANAGER_CONSOLE] = SYSCALL_ENTRY2(SyscallClearDisplayManagerConsole), [SYS_OPEN_DISPLAY_CLOCK] = SYSCALL_ENTRY1(SyscallOpenDisplayClock), [SYS_CREATE_SURFACE] = SYSCALL_ENTRY1(SyscallCreateSurface), - [SYS_MAP_SURFACE] = SYSCALL_ENTRY3(SyscallMapSurface), + [SYS_MAP_SURFACE] = SYSCALL_ENTRY4(SyscallMapSurface), + [SYS_UNMAP_SURFACE] = SYSCALL_ENTRY1(SyscallUnmapSurface), [SYS_QUERY_SURFACE] = SYSCALL_ENTRY2(SyscallQuerySurface), - [SYS_PRESENT_SURFACE] = SYSCALL_ENTRY3(SyscallPresentSurface), + [SYS_COMMIT_SURFACE] = SYSCALL_ENTRY4(SyscallCommitSurface), // Native render audio. [SYS_AUDIO_ENUMERATE_ENDPOINTS] = SYSCALL_ENTRY1(SyscallAudioEnumerateEndpoints), @@ -6583,6 +6682,7 @@ static const SYSCALL_ENTRY gSyscallTable[SYS_MAX] = { [SYS_AUDIO_SET_MASTER_VOLUME] = SYSCALL_ENTRY1(SyscallAudioSetMasterVolume), // Input. + [SYS_READ_INPUT_MANAGER_EVENT] = SYSCALL_ENTRY1(SyscallReadInputManagerEvent), [SYS_POLL_KEYBOARD] = SYSCALL_ENTRY1(SyscallKbPoll), [SYS_IS_KEY_DOWN] = SYSCALL_ENTRY2(SyscallKbIsKeyDown), [SYS_READ_KEYBOARD_CHAR] = SYSCALL_ENTRY1(SyscallKbReadChar), diff --git a/code/kernel/ke_thread.c b/code/kernel/ke_thread.c index ffd9771..5f3ed78 100644 --- a/code/kernel/ke_thread.c +++ b/code/kernel/ke_thread.c @@ -200,6 +200,25 @@ static void KiThreadDestroy(OBJECT* obj) { } } + +// Validates one optional kernel-supplied thread description and returns its length. +static KSTATUS KiValidateThreadName(const char* name, size_t* lengthOut) { + if (!lengthOut) return NTLSTATUS_INVALID_PARAMETER; + if (!name) { + *lengthOut = 0; + return NTLSTATUS_SUCCESS; + } + + size_t length = 0; + while (length < NTL_THREAD_NAME_MAX && name[length]) + length++; + if (length >= NTL_THREAD_NAME_MAX) + return NTLSTATUS_INVALID_PARAMETER; + + *lengthOut = length; + return NTLSTATUS_SUCCESS; +} + __declspec(dllexport) KSTATUS KeCreateThread(PROCESS* owner, uintptr_t entry, uintptr_t arg, const KE_THREAD_CREATE_PARAMETERS* parameters, THREAD** out) { if (!owner || !parameters || !out) return NTLSTATUS_INVALID_PARAMETER; @@ -208,6 +227,10 @@ __declspec(dllexport) KSTATUS KeCreateThread(PROCESS* owner, uintptr_t entry, ui (parameters->Flags & ~KE_THREAD_CREATE_FLAG_SUSPENDED)) return NTLSTATUS_INVALID_PARAMETER; + size_t initialNameLength = 0; + KSTATUS nameStatus = KiValidateThreadName(parameters->Name, &initialNameLength); + if (!KSUCCESS(nameStatus)) return nameStatus; + uint8_t initiallySuspended = (parameters->Flags & KE_THREAD_CREATE_FLAG_SUSPENDED) ? 1u : 0u; @@ -259,6 +282,11 @@ __declspec(dllexport) KSTATUS KeCreateThread(PROCESS* owner, uintptr_t entry, ui thread->UserExceptionFrameUserAddress = 0; memset(thread->UserExceptionExitStatus, 0, sizeof(thread->UserExceptionExitStatus)); thread->TerminationDeferralCount = 0; + thread->ExitInProgress = 0; + thread->NameLock = (SPINLOCK)SPINLOCK_INIT; + memset(thread->Name, 0, sizeof(thread->Name)); + if (initialNameLength) + memcpy(thread->Name, parameters->Name, initialNameLength); void* kstack = MmAllocatePoolTag(KSTACK_SIZE, MM_POOL_TAG('k', 's', 't', 'k')); if (!kstack) { @@ -414,18 +442,42 @@ int KeThreadTerminationDeferred(const THREAD* thread) { 0) > 0; } +// Returns whether terminal teardown owns the current thread stack. +int KeThreadExitInProgress(const THREAD* thread) { + return thread && _InterlockedCompareExchange((volatile long*)&thread->ExitInProgress, + 0, + 0) != 0; +} + // Implements the exit Thread operation. __declspec(dllexport) void KeExitThread(THREAD* thread, int code) { (void)code; if (!thread) return; - KiRundownApcState(thread); - KiMutexRundownThread(thread); + // Terminal teardown must run exactly once and must retain this kernel + // stack until thread/process completion is fully published. Hardware + // interrupts remain enabled, but interrupt-return scheduling observes + // ExitInProgress and leaves the thread on-CPU until the final KeYield(). + if (_InterlockedCompareExchange(&thread->ExitInProgress, 1, 0) != 0) + KePanic(KE_PANIC_CODE_SCHEDULER, + "recursive thread exit: thread=0x%016llX tid=%u\n", + (uint64_t)thread, + thread->ThreadId); + // Reject new APCs as soon as teardown owns the thread. The scheduler must + // not interpret ThreadDead as permission to switch away until the explicit + // terminal yield below. thread->State = ThreadDead; + KiRundownApcState(thread); + KiMutexRundownThread(thread); KeFinalizeThreadExit(thread); KeYield(); + + KePanic(KE_PANIC_CODE_SCHEDULER, + "dead thread resumed after terminal yield: thread=0x%016llX tid=%u\n", + (uint64_t)thread, + thread->ThreadId); } // KeCreateKernelThread @@ -463,6 +515,10 @@ static KSTATUS KiCreateKernelThread(uintptr_t fn, uintptr_t arg, return NTLSTATUS_INVALID_PARAMETER; } + size_t initialNameLength = 0; + KSTATUS nameStatus = KiValidateThreadName(parameters->Name, &initialNameLength); + if (!KSUCCESS(nameStatus)) return nameStatus; + uint8_t initiallySuspended = (parameters->Flags & KE_THREAD_CREATE_FLAG_SUSPENDED) ? 1u : 0u; @@ -514,6 +570,11 @@ static KSTATUS KiCreateKernelThread(uintptr_t fn, uintptr_t arg, thread->UserExceptionFrameUserAddress = 0; memset(thread->UserExceptionExitStatus, 0, sizeof(thread->UserExceptionExitStatus)); thread->TerminationDeferralCount = 0; + thread->ExitInProgress = 0; + thread->NameLock = (SPINLOCK)SPINLOCK_INIT; + memset(thread->Name, 0, sizeof(thread->Name)); + if (initialNameLength) + memcpy(thread->Name, parameters->Name, initialNameLength); thread->UserStackBase = (void*)0; // no user stack thread->Teb = (void*)0; // no TEB; GS_BASE will be 0 in kernel ctx thread->TebSize = 0; @@ -571,6 +632,37 @@ __declspec(dllexport) KSTATUS KeCreateKernelThread(uintptr_t fn, uintptr_t arg, return KiCreateKernelThread(fn, arg, parameters, out); } +// Replaces one thread's diagnostic description. +KSTATUS KeSetThreadName(THREAD* thread, const char* name, size_t length) { + if (!thread || (length && !name) || length >= NTL_THREAD_NAME_MAX) + return NTLSTATUS_INVALID_PARAMETER; + + uintptr_t flags = KeAcquireSpinlock(&thread->NameLock); + if (length) + memcpy(thread->Name, name, length); + thread->Name[length] = '\0'; + KeReleaseSpinlock(&thread->NameLock, flags); + return NTLSTATUS_SUCCESS; +} + +// Queries one stable snapshot of a mutable thread description. +size_t KeQueryThreadName(THREAD* thread, char* buffer, size_t capacity) { + if (!thread) return 0; + + uintptr_t flags = KeAcquireSpinlock(&thread->NameLock); + size_t length = 0; + while (length < NTL_THREAD_NAME_MAX && thread->Name[length]) + length++; + if (buffer && capacity) { + size_t copyLength = length < capacity - 1u ? length : capacity - 1u; + if (copyLength) + memcpy(buffer, thread->Name, copyLength); + buffer[copyLength] = '\0'; + } + KeReleaseSpinlock(&thread->NameLock, flags); + return length; +} + // Prepares block Thread. void KePrepareBlockThread(THREAD* thread, OBJECT* waitObject, uint8_t alertable) { // Called with the synchronization object's spinlock held, so the caller is @@ -640,11 +732,25 @@ KSTATUS KeCommitBlockThread(THREAD* thread, uint8_t alertable) { return status; } + // A kernel APC can be queued before the waiter finishes publishing its + // alertable state. In that race the queueing CPU cannot wake the wait, and + // APC delivery is deliberately deferred while State is ThreadWaiting. + // Consume the prepared wait here before the thread can actually sleep. + if (alertable && thread->KernelApcPending && thread->KernelApcDisable == 0) { + if (KeAbortThreadWait(thread, NTLSTATUS_KERNEL_APC)) { + thread->AlertableWait = 0; + KiDeliverKernelApcs(); + return NTLSTATUS_KERNEL_APC; + } + } + // If a user APC was already pending before the thread fully blocked, // consume the alertable wait immediately instead of going to sleep. if (alertable && thread->UserApcPending && thread->UserApcDispatcher) { - if (KeAbortThreadWait(thread, NTLSTATUS_USER_APC)) + if (KeAbortThreadWait(thread, NTLSTATUS_USER_APC)) { + thread->AlertableWait = 0; return NTLSTATUS_USER_APC; + } } KiSchedulerNoteVoluntaryBlock(thread); diff --git a/code/kernel/ke_work.c b/code/kernel/ke_work.c index 78122ec..2643968 100644 --- a/code/kernel/ke_work.c +++ b/code/kernel/ke_work.c @@ -122,7 +122,8 @@ KSTATUS KeStartWorkQueue(void) { KSTATUS s = KeEventCreate(EventAutoReset, 0, &gWorkQueueEvent); if (KSUCCESS(s)) { THREAD* worker = (THREAD*)0; - const KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + const KE_THREAD_CREATE_PARAMETERS parameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "work.queue"); s = KeCreateKernelThread((uintptr_t)KiWorkQueueWorker, 0, ¶meters, diff --git a/code/kernel/kmain.c b/code/kernel/kmain.c index 0bd0e6f..26866e8 100644 --- a/code/kernel/kmain.c +++ b/code/kernel/kmain.c @@ -433,12 +433,14 @@ static PROCESS* KiLaunchUserProcess(const char* filePath, const char* args) { return 0; } - // Bootstrap init receives read-only system-volume authority, separate namespace-seeding - // authority, and the volume-manager capability used to supervise RSSRV publication. + // The bootstrap service manager receives only the capabilities required to construct + // the initial user-mode resource plane. Its PID is deliberately not part of this ABI. IO_VOLUME* systemVolume = (IO_VOLUME*)0; SYS_HANDLE systemVolumeHandle = SYS_HANDLE_INVALID; SYS_HANDLE namespaceVolumeHandle = SYS_HANDLE_INVALID; SYS_HANDLE volumeManagerHandle = SYS_HANDLE_INVALID; + SYS_HANDLE inputManagerHandle = SYS_HANDLE_INVALID; + SYS_HANDLE displayManagerHandle = SYS_HANDLE_INVALID; s = KeReferenceSystemRootVolume(&systemVolume); if (KSUCCESS(s)) { s = KeCreateHandle(proc, &systemVolume->Header, @@ -464,18 +466,46 @@ static PROCESS* KiLaunchUserProcess(const char* filePath, const char* args) { ObDereferenceObject(&volumeManager->Header); } } + if (KSUCCESS(s)) { + KE_INPUT_MANAGER* inputManager = (KE_INPUT_MANAGER*)0; + s = KeInputReferenceManager(&inputManager); + if (KSUCCESS(s)) { + s = KeCreateHandle(proc, &inputManager->Header, + SYS_RIGHT_DUPLICATE | SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | + SYS_INPUT_MANAGER_RIGHT_READ | SYS_INPUT_MANAGER_RIGHT_CONFIGURE, + &inputManagerHandle); + ObDereferenceObject(&inputManager->Header); + } + } + if (KSUCCESS(s)) { + KE_DISPLAY_MANAGER* displayManager = (KE_DISPLAY_MANAGER*)0; + s = KeDisplayReferenceManager(&displayManager); + if (KSUCCESS(s)) { + // Init must be able to attenuate this manager again when RSSRV publishes + // narrower display capabilities such as /Device/Display0. + s = KeCreateHandle(proc, &displayManager->Header, + SYS_RIGHT_DUPLICATE | SYS_RIGHT_DERIVE | SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | + SYS_DISPLAY_MANAGER_RIGHT_ENUMERATE | SYS_DISPLAY_MANAGER_RIGHT_MODESET | + SYS_DISPLAY_MANAGER_RIGHT_PRESENT | SYS_DISPLAY_MANAGER_RIGHT_CURSOR | + SYS_DISPLAY_MANAGER_RIGHT_CONSOLE, + &displayManagerHandle); + ObDereferenceObject(&displayManager->Header); + } + } if (!KSUCCESS(s)) { DbgPrint("KiLaunchUserProcess: Failed to create bootstrap startup capabilities. Status: %d\n", s); IoCloseFile(f); ObDereferenceObject(&proc->Header); return 0; } - SYS_STARTUP_CAPABILITY startupCapabilities[3] = { + SYS_STARTUP_CAPABILITY startupCapabilities[5] = { { SYS_STARTUP_CAP_SYSTEM_VOLUME, systemVolumeHandle }, { SYS_STARTUP_CAP_SYSTEM_VOLUME_NAMESPACE, namespaceVolumeHandle }, { SYS_STARTUP_CAP_VOLUME_MANAGER, volumeManagerHandle }, + { SYS_STARTUP_CAP_INPUT_MANAGER, inputManagerHandle }, + { SYS_STARTUP_CAP_DISPLAY_MANAGER, displayManagerHandle }, }; - s = KeSetProcessStartupCapabilities(proc, startupCapabilities, 3); + s = KeSetProcessStartupCapabilities(proc, startupCapabilities, 5); if (!KSUCCESS(s)) { DbgPrint("KiLaunchUserProcess: Failed to install startup capabilities. Status: %d\n", s); IoCloseFile(f); @@ -513,7 +543,8 @@ static PROCESS* KiLaunchUserProcess(const char* filePath, const char* args) { uintptr_t entryPoint = LdrGetEntryPoint(imageBase); // Create the main thread last. KeCreateThread queues it before returning. - const KE_THREAD_CREATE_PARAMETERS threadParameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + const KE_THREAD_CREATE_PARAMETERS threadParameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, proc->Name); s = KeCreateThread(proc, (uintptr_t)entryPoint, 0, &threadParameters, &thread); if (!KSUCCESS(s)) { DbgPrint("KiLaunchUserProcess: Failed to create thread. Status: %d\n", s); @@ -549,20 +580,20 @@ void SystemInitThread(void* arg) { if (args->BootConfig.Debug & KE_DEBUG_SELFTEST) KiRunKernelSelfTests(args->Hal); - // Launch User-Mode init.exe. - PROCESS* shellProc = KiLaunchUserProcess("system/init.exe", args->BootConfig.CommandLine); - if (!shellProc) { + // Launch the bootstrap service manager. Its allocated PID is not a system contract. + PROCESS* initProcess = KiLaunchUserProcess("system/init.exe", args->BootConfig.CommandLine); + if (!initProcess) { DbgPrint("SystemInitThread: Failed to start system/init.exe.\n"); KePanic(KE_PANIC_CODE_INIT, "SystemInitThread: Failed to start system/init.exe.\n"); } - KeWaitForEvent(shellProc->ExitEvent, 0); - int exitCode = shellProc->ExitCode; + KeWaitForEvent(initProcess->ExitEvent, 0); + int exitCode = initProcess->ExitCode; if (exitCode != 0) { KePanic(KE_PANIC_CODE_INIT, "SystemInitThread: init.exe exited with code %d.\n", exitCode); } DbgPrint("SystemInitThread: init.exe exited with code %d.\n", exitCode); - ObDereferenceObject(&shellProc->Header); + ObDereferenceObject(&initProcess->Header); if (args->BootConfig.Debug & (KE_DEBUG_CONSOLE | KE_DEBUG_FILE_LOG)) { KSTATUS logStatus = KeFlushDebugLogToSystemRoot("kernel.log"); @@ -805,7 +836,8 @@ extern void KernelMain(BOOT_INFO* bootInfo) { // Spawn the Initialization Thread. No creator reference is needed; the // scheduler owns the live thread lifetime. - const KE_THREAD_CREATE_PARAMETERS initThreadParameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + const KE_THREAD_CREATE_PARAMETERS initThreadParameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "system-init"); KSTATUS st = KeCreateKernelThread((uintptr_t)SystemInitThread, (uintptr_t)initArgs, &initThreadParameters, (THREAD**)0); @@ -813,7 +845,6 @@ extern void KernelMain(BOOT_INFO* bootInfo) { if (!KSUCCESS(st)) { KePanic(KE_PANIC_CODE_INIT, "KernelMain: Failed to create SystemInitThread (status 0x%x)\n", st); } - DbgPrint("KernelMain: Handing over to scheduler...\n"); // The bootstrap CPU becomes the scheduler idle context once interrupts are enabled. diff --git a/code/kernel/mm_file_cache.c b/code/kernel/mm_file_cache.c index 0022114..dc78f8e 100644 --- a/code/kernel/mm_file_cache.c +++ b/code/kernel/mm_file_cache.c @@ -375,7 +375,8 @@ static KSTATUS KiFileCacheEnsureWritebackStarted(void) { KSTATUS s = KeSemaphoreCreate(0, 0x7fffffff, &gWritebackSemaphore); if (KSUCCESS(s)) { THREAD* worker = (THREAD*)0; - const KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + const KE_THREAD_CREATE_PARAMETERS parameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "mm.writeback"); s = KeCreateKernelThread((uintptr_t)KiFileCacheWritebackWorker, 0, ¶meters, diff --git a/code/kernel/net_socket.c b/code/kernel/net_socket.c index 48945ab..143f4f8 100644 --- a/code/kernel/net_socket.c +++ b/code/kernel/net_socket.c @@ -426,7 +426,8 @@ static KSTATUS NetEnsureAsyncSocketWorkerStarted(void) { KSTATUS status = KeSemaphoreCreate(0, 0x7fffffff, &gAsyncSocketSemaphore); if (KSUCCESS(status)) { THREAD* worker = (THREAD*)0; - const KE_THREAD_CREATE_PARAMETERS parameters = KE_THREAD_CREATE_NORMAL(PriorityNormal); + const KE_THREAD_CREATE_PARAMETERS parameters = + KE_THREAD_CREATE_NAMED_NORMAL(PriorityNormal, "net.socket.async"); status = KeCreateKernelThread((uintptr_t)NetAsyncSocketWorker, 0, ¶meters, diff --git a/code/kernel/ob_object.c b/code/kernel/ob_object.c index 758e607..f6b609d 100644 --- a/code/kernel/ob_object.c +++ b/code/kernel/ob_object.c @@ -36,6 +36,9 @@ static uint32_t KiObjectPoolTag(OBJECT_TYPE type) { case ObjectTypePort: return MM_POOL_TAG('p', 'o', 'r', 't'); case ObjectTypeVolume: return MM_POOL_TAG('v', 'o', 'l', 'm'); case ObjectTypeVolumeManager: return MM_POOL_TAG('v', 'm', 'g', 'r'); + case ObjectTypeInputManager: return MM_POOL_TAG('i', 'm', 'g', 'r'); + case ObjectTypeDisplayManager: return MM_POOL_TAG('d', 'm', 'g', 'r'); + case ObjectTypePipeEndpoint: return MM_POOL_TAG('p', 'i', 'p', 'e'); default: return MM_POOL_TAG('o', 'b', 'j', ' '); } } diff --git a/code/public/freestanding/intrin.h b/code/public/freestanding/intrin.h new file mode 100644 index 0000000..d71e1de --- /dev/null +++ b/code/public/freestanding/intrin.h @@ -0,0 +1,21 @@ +// Minimal MSVC intrinsic declarations needed by nt-lite public/user headers. + +#pragma once + +unsigned __int64 __readgsqword(unsigned long offset); +long _InterlockedCompareExchange(long volatile* destination, long exchange, long comparand); +long _InterlockedExchange(long volatile* target, long value); +long _InterlockedIncrement(long volatile* addend); +long _InterlockedDecrement(long volatile* addend); +void _ReadWriteBarrier(void); +void _mm_pause(void); + +#ifdef _MSC_VER +#pragma intrinsic(__readgsqword) +#pragma intrinsic(_InterlockedCompareExchange) +#pragma intrinsic(_InterlockedExchange) +#pragma intrinsic(_InterlockedIncrement) +#pragma intrinsic(_InterlockedDecrement) +#pragma intrinsic(_ReadWriteBarrier) +#pragma intrinsic(_mm_pause) +#endif diff --git a/code/public/freestanding/stdarg.h b/code/public/freestanding/stdarg.h new file mode 100644 index 0000000..385a18f --- /dev/null +++ b/code/public/freestanding/stdarg.h @@ -0,0 +1,21 @@ +// Minimal freestanding variadic argument support for nt-lite target builds. + +#pragma once + +#ifdef _MSC_VER +#define va_list __crt_va_list +#define va_start __crt_va_start +#define va_arg __crt_va_arg +#define va_end __crt_va_end +#define va_copy(destination, source) ((destination) = (source)) +typedef char* __crt_va_list; +#define __crt_va_start(ap, x) ((ap) = (char*)(&(x)) + ((sizeof(x) + 7u) & ~7u)) +#define __crt_va_arg(ap, t) (*(t*)(((ap) += 8u) - 8u)) +#define __crt_va_end(ap) ((ap) = (char*)0) +#else +#define va_list __builtin_va_list +#define va_start(ap, last) __builtin_va_start(ap, last) +#define va_arg(ap, type) __builtin_va_arg(ap, type) +#define va_end(ap) __builtin_va_end(ap) +#define va_copy(destination, source) __builtin_va_copy(destination, source) +#endif diff --git a/code/public/freestanding/stdbool.h b/code/public/freestanding/stdbool.h new file mode 100644 index 0000000..73afdb9 --- /dev/null +++ b/code/public/freestanding/stdbool.h @@ -0,0 +1,11 @@ +// Freestanding Boolean declarations for nt-lite C target builds. + +#pragma once + +#ifndef __cplusplus +#define bool _Bool +#define true 1 +#define false 0 +#endif + +#define __bool_true_false_are_defined 1 diff --git a/code/public/freestanding/stddef.h b/code/public/freestanding/stddef.h new file mode 100644 index 0000000..e432246 --- /dev/null +++ b/code/public/freestanding/stddef.h @@ -0,0 +1,14 @@ +// Freestanding ISO C stddef subset for nt-lite target builds. + +#pragma once + +typedef unsigned __int64 size_t; +typedef __int64 ptrdiff_t; + +#ifndef NULL +#define NULL ((void*)0) +#endif + +#ifndef offsetof +#define offsetof(type, member) ((size_t)&(((type*)0)->member)) +#endif diff --git a/code/public/freestanding/stdint.h b/code/public/freestanding/stdint.h new file mode 100644 index 0000000..48c5338 --- /dev/null +++ b/code/public/freestanding/stdint.h @@ -0,0 +1,51 @@ +// Freestanding fixed-width integer declarations for nt-lite x86-64 targets. + +#pragma once + +typedef signed char int8_t; +typedef unsigned char uint8_t; +typedef short int16_t; +typedef unsigned short uint16_t; +typedef int int32_t; +typedef unsigned int uint32_t; +typedef __int64 int64_t; +typedef unsigned __int64 uint64_t; + +typedef int8_t int_least8_t; +typedef uint8_t uint_least8_t; +typedef int16_t int_least16_t; +typedef uint16_t uint_least16_t; +typedef int32_t int_least32_t; +typedef uint32_t uint_least32_t; +typedef int64_t int_least64_t; +typedef uint64_t uint_least64_t; + +typedef int8_t int_fast8_t; +typedef uint8_t uint_fast8_t; +typedef int16_t int_fast16_t; +typedef uint16_t uint_fast16_t; +typedef int32_t int_fast32_t; +typedef uint32_t uint_fast32_t; +typedef int64_t int_fast64_t; +typedef uint64_t uint_fast64_t; + +typedef __int64 intptr_t; +typedef unsigned __int64 uintptr_t; +typedef __int64 intmax_t; +typedef unsigned __int64 uintmax_t; + +#define INT8_MIN (-127 - 1) +#define INT16_MIN (-32767 - 1) +#define INT32_MIN (-2147483647 - 1) +#define INT64_MIN (-9223372036854775807LL - 1) +#define INT8_MAX 127 +#define INT16_MAX 32767 +#define INT32_MAX 2147483647 +#define INT64_MAX 9223372036854775807LL +#define UINT8_MAX 0xffU +#define UINT16_MAX 0xffffU +#define UINT32_MAX 0xffffffffU +#define UINT64_MAX 0xffffffffffffffffULL +#define INTPTR_MIN INT64_MIN +#define INTPTR_MAX INT64_MAX +#define UINTPTR_MAX UINT64_MAX diff --git a/code/public/freestanding/string.h b/code/public/freestanding/string.h new file mode 100644 index 0000000..3f2c67c --- /dev/null +++ b/code/public/freestanding/string.h @@ -0,0 +1,23 @@ +// Freestanding string and memory declarations backed by nt-lite libc. + +#pragma once + +#include + +char* strcpy(char* destination, const char* source); +char* strncpy(char* destination, const char* source, size_t count); +char* strcat(char* destination, const char* source); +char* strncat(char* destination, const char* source, size_t count); +int strcmp(const char* left, const char* right); +int strncmp(const char* left, const char* right, size_t count); +int _stricmp(const char* left, const char* right); +size_t strlen(const char* string); +size_t strnlen(const char* string, size_t maximumLength); +char* strchr(const char* string, int character); +char* strrchr(const char* string, int character); +char* strstr(const char* haystack, const char* needle); +void* memchr(const void* buffer, int character, size_t count); +void* memset(void* destination, int value, size_t count); +void* memcpy(void* destination, const void* source, size_t count); +void* memmove(void* destination, const void* source, size_t count); +int memcmp(const void* left, const void* right, size_t count); diff --git a/code/public/ntlite/console_service.h b/code/public/ntlite/console_service.h new file mode 100644 index 0000000..b675393 --- /dev/null +++ b/code/public/ntlite/console_service.h @@ -0,0 +1,121 @@ +// Native console service protocol and client helpers. + +#pragma once + +#include +#include +#include +#include +#include + +#define NTL_CONSOLE_PROTOCOL_VERSION 1u +#define NTL_CONSOLE_SERVICE_PATH "/Services/Console" + +#define NTL_CONSOLE_FLAG_NONE 0x00000000u +#define NTL_CONSOLE_WRITE_MAX 512u + +#define NTL_CONSOLE_OP_WRITE 1u +#define NTL_CONSOLE_OP_QUERY_INFO 2u +#define NTL_CONSOLE_OP_SET_CURSOR 3u +#define NTL_CONSOLE_OP_FILL_CHARACTER 4u +#define NTL_CONSOLE_OP_FILL_ATTRIBUTE 5u +#define NTL_CONSOLE_OP_PUT_CHARACTER 6u +#define NTL_CONSOLE_OP_CLEAR 7u +#define NTL_CONSOLE_OP_READ_INPUT 8u +#define NTL_CONSOLE_OP_PEEK_INPUT 9u +#define NTL_CONSOLE_OP_FLUSH_INPUT 10u + +typedef struct NTL_CONSOLE_INFO { + uint32_t Size; + uint32_t Flags; + uint32_t Columns; + uint32_t Rows; + uint32_t CursorX; + uint32_t CursorY; + uint16_t Attribute; + uint16_t Reserved; + uint32_t PixelWidth; + uint32_t PixelHeight; + uint64_t Generation; +} NTL_CONSOLE_INFO; + +typedef struct NTL_CONSOLE_REQUEST { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + uint64_t TimeoutMilliseconds; + union { + struct { + uint32_t Length; + char Data[NTL_CONSOLE_WRITE_MAX]; + } Write; + struct { + int32_t X; + int32_t Y; + } Cursor; + struct { + char Character; + uint8_t Reserved[3]; + int32_t X; + int32_t Y; + uint32_t Length; + } FillCharacter; + struct { + uint16_t Attribute; + uint16_t Reserved; + int32_t X; + int32_t Y; + uint32_t Length; + } FillAttribute; + struct { + char Character; + uint8_t Reserved[3]; + int32_t X; + int32_t Y; + uint32_t Foreground; + uint32_t Background; + } PutCharacter; + struct { + uint32_t Color; + } Clear; + } Data; +} NTL_CONSOLE_REQUEST; + +typedef struct NTL_CONSOLE_REPLY { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + KSTATUS Status; + uint32_t Written; + INPUT_KEY_RECORD Input; + NTL_CONSOLE_INFO Info; +} NTL_CONSOLE_REPLY; + +#if defined(NTL_SYSCALL_STATIC) +#define NTL_CONSOLE_API +#elif defined(NTL_SYSCALL_BUILD) +#define NTL_CONSOLE_API __declspec(dllexport) +#else +#define NTL_CONSOLE_API +#endif + +NTL_CONSOLE_API KSTATUS NtlConsoleConnect(SYS_HANDLE* sessionOut); +NTL_CONSOLE_API KSTATUS NtlConsoleClose(SYS_HANDLE session); +NTL_CONSOLE_API KSTATUS NtlConsoleWrite(SYS_HANDLE session, const void* data, size_t length, size_t* writtenOut); +NTL_CONSOLE_API KSTATUS NtlConsoleQueryInfo(SYS_HANDLE session, NTL_CONSOLE_INFO* infoOut); +NTL_CONSOLE_API KSTATUS NtlConsoleSetCursor(SYS_HANDLE session, int x, int y); +NTL_CONSOLE_API KSTATUS NtlConsoleFillCharacter(SYS_HANDLE session, char character, int x, int y, + size_t length, size_t* writtenOut); +NTL_CONSOLE_API KSTATUS NtlConsoleFillAttribute(SYS_HANDLE session, uint16_t attribute, int x, int y, + size_t length, size_t* writtenOut); +NTL_CONSOLE_API KSTATUS NtlConsolePutCharacter(SYS_HANDLE session, char character, int x, int y, + uint32_t foreground, uint32_t background); +NTL_CONSOLE_API KSTATUS NtlConsoleClear(SYS_HANDLE session, uint32_t color); +NTL_CONSOLE_API KSTATUS NtlConsoleReadInput(SYS_HANDLE session, uint64_t timeoutMilliseconds, + INPUT_KEY_RECORD* recordOut); +NTL_CONSOLE_API KSTATUS NtlConsolePeekInput(SYS_HANDLE session, uint64_t timeoutMilliseconds, + INPUT_KEY_RECORD* recordOut); +NTL_CONSOLE_API KSTATUS NtlConsoleFlushInput(SYS_HANDLE session); +NTL_CONSOLE_API KSTATUS NtlConsoleReadCharacter(SYS_HANDLE session, char* characterOut); diff --git a/code/public/ntlite/ddk/object.h b/code/public/ntlite/ddk/object.h index c6d17dd..f4c8b44 100644 --- a/code/public/ntlite/ddk/object.h +++ b/code/public/ntlite/ddk/object.h @@ -34,6 +34,9 @@ typedef enum OBJECT_TYPE { ObjectTypePort = 24, ObjectTypeVolume = 25, ObjectTypeVolumeManager = 26, + ObjectTypeInputManager = 27, + ObjectTypeDisplayManager = 28, + ObjectTypePipeEndpoint = 29, } OBJECT_TYPE; typedef enum OBJECT_LIFETIME_STATE { diff --git a/code/public/ntlite/ddk/sync.h b/code/public/ntlite/ddk/sync.h index c1d6a6c..b558fe1 100644 --- a/code/public/ntlite/ddk/sync.h +++ b/code/public/ntlite/ddk/sync.h @@ -55,8 +55,12 @@ static __forceinline void KeReleaseSpinlock(SPINLOCK* sl, uintptr_t cookie) { KIRQL oldIrql = KiSpinlockCookieIrql(cookie); _InterlockedExchange(&sl->Locked, 0); - KeLowerIrql(oldIrql); + + // Restore the caller's hardware interrupt state before dropping to an + // IRQL that can dispatch APCs. PASSIVE_LEVEL with IF still clear is not a + // valid APC execution context. KeRestoreInterrupts(flags); + KeLowerIrql(oldIrql); } // Attempts to acquire a spinlock without blocking. @@ -72,8 +76,10 @@ static __forceinline int KeTryAcquireSpinlock(SPINLOCK* sl, uintptr_t* outCookie return 1; } - KeLowerIrql(oldIrql); + // Keep the failed-acquire unwind consistent with normal release: restore + // IF before a PASSIVE transition can dispatch pending APCs. KeRestoreInterrupts(flags); + KeLowerIrql(oldIrql); return 0; } diff --git a/code/public/ntlite/ddk/thread.h b/code/public/ntlite/ddk/thread.h index d0c0228..07f4311 100644 --- a/code/public/ntlite/ddk/thread.h +++ b/code/public/ntlite/ddk/thread.h @@ -4,6 +4,7 @@ #include #include +#include typedef struct THREAD THREAD; @@ -34,21 +35,29 @@ typedef enum KE_SCHEDULING_CLASS { // must retain the returned THREAD reference and call KeResumeThread(). #define KE_THREAD_CREATE_FLAG_SUSPENDED 0x00000001u -// Describes initial scheduling, execution state, and affinity for one new -// thread. Zero affinity selects the kernel default rather than an empty CPU -// set. +// Describes initial scheduling, execution state, affinity, and diagnostics for +// one new thread. Zero affinity selects the kernel default rather than an empty +// CPU set. Name may be NULL; otherwise it must be a NUL-terminated UTF-8 name +// shorter than NTL_THREAD_NAME_MAX bytes. typedef struct KE_THREAD_CREATE_PARAMETERS { KE_SCHEDULING_CLASS SchedulingClass; uint32_t Priority; uint32_t Flags; uint64_t AffinityMask; + const char* Name; } KE_THREAD_CREATE_PARAMETERS; #define KE_THREAD_CREATE_NORMAL(priorityValue) \ - { KeSchedulingNormal, (uint32_t)(priorityValue), 0u, 0ULL } + { KeSchedulingNormal, (uint32_t)(priorityValue), 0u, 0ULL, (const char*)0 } + +#define KE_THREAD_CREATE_NAMED_NORMAL(priorityValue, nameValue) \ + { KeSchedulingNormal, (uint32_t)(priorityValue), 0u, 0ULL, (nameValue) } #define KE_THREAD_CREATE_REALTIME(priorityValue) \ - { KeSchedulingRealtime, (uint32_t)(priorityValue), 0u, 0ULL } + { KeSchedulingRealtime, (uint32_t)(priorityValue), 0u, 0ULL, (const char*)0 } + +#define KE_THREAD_CREATE_NAMED_REALTIME(priorityValue, nameValue) \ + { KeSchedulingRealtime, (uint32_t)(priorityValue), 0u, 0ULL, (nameValue) } // Creates a system-process kernel thread with explicit scheduling parameters. __declspec(dllexport) KSTATUS KeCreateKernelThread(uintptr_t fn, diff --git a/code/public/ntlite/desktop.h b/code/public/ntlite/desktop.h new file mode 100644 index 0000000..bd343a3 --- /dev/null +++ b/code/public/ntlite/desktop.h @@ -0,0 +1,138 @@ +// Native desktop/window protocol and client helpers. DWM owns window policy while RSSRV retains Session 0 routing authority. + +#pragma once + +#include +#include +#include +#include +#include + +#define NTL_DESKTOP_PROTOCOL_VERSION 1u +#define NTL_DESKTOP_SERVICE_PATH "/Sessions/0/Desktop" + +#define NTL_DESKTOP_FLAG_NONE 0x00000000u + +#define NTL_DESKTOP_OP_CREATE_WINDOW 1u +#define NTL_DESKTOP_OP_DESTROY_WINDOW 2u +#define NTL_DESKTOP_OP_SET_BOUNDS 3u +#define NTL_DESKTOP_OP_SET_VISIBLE 4u +#define NTL_DESKTOP_OP_RAISE_WINDOW 5u +#define NTL_DESKTOP_OP_LOWER_WINDOW 6u +#define NTL_DESKTOP_OP_REQUEST_FOCUS 7u +#define NTL_DESKTOP_OP_REQUEST_CAPTURE 8u +#define NTL_DESKTOP_OP_RELEASE_CAPTURE 9u +#define NTL_DESKTOP_OP_SET_POINTER_MODE 10u +#define NTL_DESKTOP_OP_ATTACH_SURFACE 11u +#define NTL_DESKTOP_OP_DETACH_SURFACE 12u +#define NTL_DESKTOP_OP_QUERY_WINDOW 13u + +#define NTL_DESKTOP_EVENT_KEY 1u +#define NTL_DESKTOP_EVENT_MOUSE 2u + +#define NTL_DESKTOP_POINTER_FREE 0u +#define NTL_DESKTOP_POINTER_CONFINED 1u +#define NTL_DESKTOP_POINTER_RELATIVE_LOCKED 2u + +typedef uint64_t NTL_WINDOW_ID; + +// One fixed desktop request. ATTACH_SURFACE carries exactly one surface capability. +typedef struct NTL_DESKTOP_REQUEST { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + NTL_WINDOW_ID WindowId; + DISPLAY_RECT Bounds; + uint32_t Value; + uint32_t Reserved; +} NTL_DESKTOP_REQUEST; + +// One fixed desktop reply. Query fields are meaningful after CREATE_WINDOW or QUERY_WINDOW. +typedef struct NTL_DESKTOP_REPLY { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + KSTATUS Status; + uint32_t Reserved; + NTL_WINDOW_ID WindowId; + DISPLAY_RECT Bounds; + uint32_t Visible; + uint32_t Focused; + uint32_t Captured; + uint32_t HasSurface; + uint32_t SurfaceWidth; + uint32_t SurfaceHeight; +} NTL_DESKTOP_REPLY; + +// One asynchronous input notification delivered to the owning window connection. +typedef struct NTL_DESKTOP_EVENT { + uint32_t Size; + uint32_t Version; + uint32_t Type; + uint32_t Flags; + NTL_WINDOW_ID WindowId; + int32_t PointerX; + int32_t PointerY; + INPUT_KEY_RECORD Key; + INPUT_MOUSE_EVENT Mouse; +} NTL_DESKTOP_EVENT; + +#if defined(NTL_SYSCALL_STATIC) +#define NTL_DESKTOP_API +#elif defined(NTL_SYSCALL_BUILD) +#define NTL_DESKTOP_API __declspec(dllexport) +#else +#define NTL_DESKTOP_API +#endif + +// Connects to the active Session 0 desktop endpoint through RSSRV. +NTL_DESKTOP_API KSTATUS NtlDesktopConnect(SYS_HANDLE* sessionOut); + +// Creates one hidden client-owned top-level window. +NTL_DESKTOP_API KSTATUS NtlDesktopCreateWindow(SYS_HANDLE session, const DISPLAY_RECT* bounds, NTL_WINDOW_ID* windowOut); + +// Destroys one window owned by this connection. +NTL_DESKTOP_API KSTATUS NtlDesktopDestroyWindow(SYS_HANDLE session, NTL_WINDOW_ID window); + +// Changes one window's screen-space bounds. +NTL_DESKTOP_API KSTATUS NtlDesktopSetBounds(SYS_HANDLE session, NTL_WINDOW_ID window, const DISPLAY_RECT* bounds); + +// Shows or hides one window without changing its z-order. +NTL_DESKTOP_API KSTATUS NtlDesktopSetVisible(SYS_HANDLE session, NTL_WINDOW_ID window, uint32_t visible); + +// Moves one window to the top of the z-order. +NTL_DESKTOP_API KSTATUS NtlDesktopRaiseWindow(SYS_HANDLE session, NTL_WINDOW_ID window); + +// Moves one window to the bottom of the z-order. +NTL_DESKTOP_API KSTATUS NtlDesktopLowerWindow(SYS_HANDLE session, NTL_WINDOW_ID window); + +// Requests Session 0 keyboard focus for one visible window. +NTL_DESKTOP_API KSTATUS NtlDesktopRequestFocus(SYS_HANDLE session, NTL_WINDOW_ID window); + +// Requests Session 0 pointer capture for one visible window. +NTL_DESKTOP_API KSTATUS NtlDesktopRequestCapture(SYS_HANDLE session, NTL_WINDOW_ID window); + +// Releases pointer capture only when this window currently owns it. +NTL_DESKTOP_API KSTATUS NtlDesktopReleaseCapture(SYS_HANDLE session, NTL_WINDOW_ID window); + +// Sets visible cursor policy for the focused/captured window. +NTL_DESKTOP_API KSTATUS NtlDesktopSetPointerMode(SYS_HANDLE session, NTL_WINDOW_ID window, uint32_t mode); + +// Attaches a shared surface. DWM receives only QUERY, WAIT, and MAP_READ rights. +NTL_DESKTOP_API KSTATUS NtlDesktopAttachSurface(SYS_HANDLE session, NTL_WINDOW_ID window, SYS_HANDLE surface); + +// Detaches the current shared surface from one window. +NTL_DESKTOP_API KSTATUS NtlDesktopDetachSurface(SYS_HANDLE session, NTL_WINDOW_ID window); + +// Queries one connection-owned window. +NTL_DESKTOP_API KSTATUS NtlDesktopQueryWindow(SYS_HANDLE session, NTL_WINDOW_ID window, NTL_DESKTOP_REPLY* replyOut); + +// Receives one asynchronous key or mouse event from DWM. +NTL_DESKTOP_API KSTATUS NtlDesktopReceiveEvent(SYS_HANDLE session, uint64_t timeoutMilliseconds, NTL_DESKTOP_EVENT* eventOut); + +// Disconnects and closes one desktop session endpoint. +NTL_DESKTOP_API KSTATUS NtlDesktopClose(SYS_HANDLE session); + +#undef NTL_DESKTOP_API diff --git a/code/public/ntlite/display.h b/code/public/ntlite/display.h index 9bc9f66..79b8af2 100644 --- a/code/public/ntlite/display.h +++ b/code/public/ntlite/display.h @@ -35,22 +35,28 @@ typedef struct DISPLAY_RECT { #define SYS_SURFACE_FORMAT_XRGB8888 1u #define SYS_SURFACE_FLAG_NONE 0u #define SYS_SURFACE_MAX_RECTS 64u +#define SYS_SURFACE_MAX_DIMENSION 16384u +#define SYS_SURFACE_MAX_BYTES (256ull * 1024ull * 1024ull) + +#define SYS_SURFACE_MAP_READ 0x00000001u +#define SYS_SURFACE_MAP_WRITE 0x00000002u typedef struct SYS_SURFACE_DESC { uint32_t Width; uint32_t Height; uint32_t PitchBytes; uint32_t Format; - void* Base; uint64_t PayloadBytes; uint64_t PageCount; uint64_t MapCount; - uint64_t PresentCount; - uint64_t PresentRectCount; - uint64_t PresentClippedRectCount; - uint64_t PresentEmptyClipCount; - uint64_t PresentRowsCopied; - uint64_t PresentBytesCopied; + uint64_t ContentGeneration; + uint64_t CommitCount; + uint64_t CommitRectCount; + uint64_t CommitClippedRectCount; + uint64_t CommitEmptyClipCount; + DISPLAY_RECT LastDamageBounds; + uint32_t LastDamageValid; + uint32_t Reserved; } SYS_SURFACE_DESC; typedef struct SYS_SURFACE_STATS { @@ -62,12 +68,12 @@ typedef struct SYS_SURFACE_STATS { uint64_t LivePages; uint64_t LiveBytes; uint64_t MapCalls; - uint64_t PresentCalls; - uint64_t PresentRectInputs; - uint64_t PresentClippedRects; - uint64_t PresentEmptyClips; - uint64_t PresentRowsCopied; - uint64_t PresentBytesCopied; + uint64_t CommitCalls; + uint64_t CommitRectInputs; + uint64_t CommitClippedRects; + uint64_t CommitEmptyClips; + uint64_t PhysicalPresentCalls; + uint64_t PhysicalPresentBytesCopied; } SYS_SURFACE_STATS; typedef struct DISPLAY_STATS { diff --git a/code/public/ntlite/handle.h b/code/public/ntlite/handle.h index c2f69e8..279833a 100644 --- a/code/public/ntlite/handle.h +++ b/code/public/ntlite/handle.h @@ -26,7 +26,8 @@ typedef uint32_t SYS_RIGHTS; #define SYS_PROCESS_RIGHT_BUILD 0x00000400u // Thread-specific rights. -#define SYS_THREAD_RIGHT_RESUME 0x00000100u +#define SYS_THREAD_RIGHT_RESUME 0x00000100u +#define SYS_THREAD_RIGHT_SET_INFORMATION 0x00000200u // Mutex-specific rights. #define SYS_MUTEX_RIGHT_RELEASE 0x00000100u @@ -58,6 +59,17 @@ typedef uint32_t SYS_RIGHTS; #define SYS_VOLUME_MANAGER_RIGHT_OPEN 0x00000200u #define SYS_VOLUME_MANAGER_RIGHT_WAIT 0x00000400u +// Input-manager rights. The bootstrap manager capability is delegated to RSSRV. +#define SYS_INPUT_MANAGER_RIGHT_READ 0x00000100u +#define SYS_INPUT_MANAGER_RIGHT_CONFIGURE 0x00000200u + +// Display-manager rights. The bootstrap manager capability is delegated to RSSRV. +#define SYS_DISPLAY_MANAGER_RIGHT_ENUMERATE 0x00000100u +#define SYS_DISPLAY_MANAGER_RIGHT_MODESET 0x00000200u +#define SYS_DISPLAY_MANAGER_RIGHT_PRESENT 0x00000400u +#define SYS_DISPLAY_MANAGER_RIGHT_CURSOR 0x00000800u +#define SYS_DISPLAY_MANAGER_RIGHT_CONSOLE 0x00001000u + // Directory-specific rights. #define SYS_DIR_RIGHT_ENUMERATE 0x00000100u #define SYS_DIR_RIGHT_OPEN 0x00000200u @@ -77,12 +89,18 @@ typedef uint32_t SYS_RIGHTS; #define SYS_SOCKET_RIGHT_POLL 0x00008000u #define SYS_SOCKET_RIGHT_SET_OPTION 0x00010000u +// Pipe-endpoint rights. +#define SYS_PIPE_RIGHT_READ 0x00000100u +#define SYS_PIPE_RIGHT_WRITE 0x00000200u +#define SYS_PIPE_RIGHT_POLL 0x00000400u + // File-mapping rights. #define SYS_FILE_MAPPING_RIGHT_MAP 0x00000100u // Surface rights. -#define SYS_SURFACE_RIGHT_MAP 0x00000100u -#define SYS_SURFACE_RIGHT_PRESENT 0x00000200u +#define SYS_SURFACE_RIGHT_MAP_READ 0x00000100u +#define SYS_SURFACE_RIGHT_MAP_WRITE 0x00000200u +#define SYS_SURFACE_RIGHT_COMMIT 0x00000400u // Input-queue rights. #define SYS_INPUT_QUEUE_RIGHT_READ 0x00000100u diff --git a/code/public/ntlite/input_service.h b/code/public/ntlite/input_service.h new file mode 100644 index 0000000..afd37eb --- /dev/null +++ b/code/public/ntlite/input_service.h @@ -0,0 +1,92 @@ +// Native logical-input service protocol and client helpers. + +#pragma once + +#include +#include +#include +#include +#include + +#define NTL_INPUT_PROTOCOL_VERSION 1u +#define NTL_INPUT_SERVICE_PATH "/Services/Input" + +#define NTL_INPUT_FLAG_NONE 0x00000000u + +#define NTL_INPUT_OP_READ_KEY 1u +#define NTL_INPUT_OP_PEEK_KEY 2u +#define NTL_INPUT_OP_READ_MOUSE 3u +#define NTL_INPUT_OP_PEEK_MOUSE 4u +#define NTL_INPUT_OP_QUERY_STATE 5u + +#define NTL_INPUT_STATE_FLAG_NONE 0x00000000u +#define NTL_INPUT_KEY_BYTES 32u + +// RSSRV-owned logical input state. This is distinct from the legacy kernel-global input snapshot. +typedef struct NTL_INPUT_STATE { + uint32_t Size; + uint32_t Flags; + uint64_t Generation; + uint32_t Modifiers; + uint8_t KeyDown[NTL_INPUT_KEY_BYTES]; + int32_t MouseX; + int32_t MouseY; + int32_t MouseWheel; + uint32_t MouseButtons; +} NTL_INPUT_STATE; + +// Request header for all logical-input service operations. +typedef struct NTL_INPUT_REQUEST { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + uint64_t TimeoutMilliseconds; +} NTL_INPUT_REQUEST; + +// Fixed reply. Only the field associated with Operation is meaningful on success. +typedef struct NTL_INPUT_REPLY { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + KSTATUS Status; + uint32_t Reserved; + INPUT_KEY_RECORD Key; + INPUT_MOUSE_EVENT Mouse; + NTL_INPUT_STATE State; +} NTL_INPUT_REPLY; + +#if defined(NTL_SYSCALL_STATIC) +#define NTL_INPUT_API +#elif defined(NTL_SYSCALL_BUILD) +#define NTL_INPUT_API __declspec(dllexport) +#else +#define NTL_INPUT_API +#endif + +// Opens one session to RSSRV's logical-input service. +NTL_INPUT_API KSTATUS NtlInputConnect(SYS_HANDLE* sessionOut); +NTL_INPUT_API KSTATUS NtlInputClose(SYS_HANDLE session); + +// Reads or peeks one logical keyboard record. SYS_HANDLE_INVALID selects the process-default session; a zero timeout is nonblocking. +NTL_INPUT_API KSTATUS NtlInputReadEvent(SYS_HANDLE session, uint64_t timeoutMilliseconds, INPUT_KEY_RECORD* recordOut); +NTL_INPUT_API KSTATUS NtlInputPeekEvent(SYS_HANDLE session, uint64_t timeoutMilliseconds, INPUT_KEY_RECORD* recordOut); + +// Reads or peeks one logical mouse event. SYS_HANDLE_INVALID selects the process-default session; a zero timeout is nonblocking. +NTL_INPUT_API KSTATUS NtlInputReadMouse(SYS_HANDLE session, uint64_t timeoutMilliseconds, INPUT_MOUSE_EVENT* eventOut); +NTL_INPUT_API KSTATUS NtlInputPeekMouse(SYS_HANDLE session, uint64_t timeoutMilliseconds, INPUT_MOUSE_EVENT* eventOut); + +// Snapshots RSSRV's logical key/modifier/pointer state. +NTL_INPUT_API KSTATUS NtlInputQueryState(SYS_HANDLE session, NTL_INPUT_STATE* stateOut); + +// Process-local convenience wrappers backed by a lazily connected RSSRV session. +NTL_INPUT_API KSTATUS NtlInputPollKeyboard(INPUT_KEY* keyOut); +NTL_INPUT_API KSTATUS NtlInputIsKeyDown(INPUT_KEY key, bool* downOut); +NTL_INPUT_API KSTATUS NtlInputReadKeyboardChar(char* charOut); +NTL_INPUT_API KSTATUS NtlInputReadKey(INPUT_KEY* keyOut); +NTL_INPUT_API uint32_t NtlInputGetKeyModifiers(void); +NTL_INPUT_API KSTATUS NtlInputQueryDefaultState(NTL_INPUT_STATE* stateOut); +NTL_INPUT_API KSTATUS NtlInputGetMousePos(int* xOut, int* yOut); + +#undef NTL_INPUT_API diff --git a/code/public/ntlite/posix_service.h b/code/public/ntlite/posix_service.h new file mode 100644 index 0000000..1e52c09 --- /dev/null +++ b/code/public/ntlite/posix_service.h @@ -0,0 +1,219 @@ +// Native POSIX personality-server discovery, process identity, and shared-attribute protocol. + +#pragma once + +#include +#include +#include + +#define NTL_POSIX_SERVICE_PATH "/Services/posix" +#define NTL_POSIX_PROTOCOL_VERSION 6u + +#define NTL_POSIX_REQUEST_FLAG_NONE 0u + +#define NTL_POSIX_OP_QUERY 1u +#define NTL_POSIX_OP_BEGIN_TRANSACTION 2u +#define NTL_POSIX_OP_CANCEL_TRANSACTION 3u +#define NTL_POSIX_OP_REGISTER_PROCESS 4u +#define NTL_POSIX_OP_GET_PROCESS_ATTRIBUTES 5u +#define NTL_POSIX_OP_SET_CURRENT_DIRECTORY 6u +#define NTL_POSIX_OP_SET_UID 7u +#define NTL_POSIX_OP_SET_GID 8u +#define NTL_POSIX_OP_SET_PROCESS_GROUP 9u +#define NTL_POSIX_OP_CREATE_SESSION 10u +#define NTL_POSIX_OP_LOOKUP_USER 11u +#define NTL_POSIX_OP_LOOKUP_GROUP 12u +#define NTL_POSIX_OP_CREATE_OPEN_DESCRIPTION 13u +#define NTL_POSIX_OP_RELEASE_OPEN_DESCRIPTION 14u +#define NTL_POSIX_OP_SET_OPEN_STATUS 15u +#define NTL_POSIX_OP_CREATE_PIPE 16u +#define NTL_POSIX_OP_CREATE_FD_INHERITANCE 17u +#define NTL_POSIX_OP_IMPORT_FD_INHERITANCE 18u +#define NTL_POSIX_OP_CANCEL_FD_INHERITANCE 19u +#define NTL_POSIX_OP_ARM_FD_INHERITANCE 20u + +#define NTL_POSIX_TRANSACTION_NONE 0u +#define NTL_POSIX_TRANSACTION_SPAWN 1u +#define NTL_POSIX_TRANSACTION_EXEC 2u + +#define NTL_POSIX_PROCESS_STATE_NONE 0u +#define NTL_POSIX_PROCESS_STATE_RUNNING 1u +#define NTL_POSIX_PROCESS_STATE_EXITED 2u + +#define NTL_POSIX_LOOKUP_NONE 0u +#define NTL_POSIX_LOOKUP_BY_ID 1u +#define NTL_POSIX_LOOKUP_BY_NAME 2u + +#define NTL_POSIX_TRANSACTION_CAPACITY 8u +#define NTL_POSIX_SUPPLEMENTARY_GROUP_MAX 16u +#define NTL_POSIX_IDENTITY_NAME_MAX 63u +#define NTL_POSIX_IDENTITY_HOME_MAX 255u +#define NTL_POSIX_IDENTITY_SHELL_MAX 255u + +// POSIX open-description kinds shared between posix.dll and posixsrv. +#define NTL_POSIX_OPEN_TYPE_FILE 1u +#define NTL_POSIX_OPEN_TYPE_CONSOLE_READ 2u +#define NTL_POSIX_OPEN_TYPE_CONSOLE_WRITE 3u +#define NTL_POSIX_OPEN_TYPE_DEV_NULL 4u +#define NTL_POSIX_OPEN_TYPE_DEV_ZERO 5u +#define NTL_POSIX_OPEN_TYPE_SOCKET 6u +#define NTL_POSIX_OPEN_TYPE_PIPE_READ 7u +#define NTL_POSIX_OPEN_TYPE_PIPE_WRITE 8u + +// Values intentionally match the public POSIX fcntl ABI; posixsrv owns policy but +// does not include personality headers. +#define NTL_POSIX_OPEN_STATUS_RDONLY 0x0000u +#define NTL_POSIX_OPEN_STATUS_WRONLY 0x0001u +#define NTL_POSIX_OPEN_STATUS_RDWR 0x0002u +#define NTL_POSIX_OPEN_STATUS_ACCMODE 0x0003u +#define NTL_POSIX_OPEN_STATUS_APPEND 0x0400u +#define NTL_POSIX_OPEN_STATUS_NONBLOCK 0x0800u +#define NTL_POSIX_OPEN_STATUS_VALID_MASK \ + (NTL_POSIX_OPEN_STATUS_ACCMODE | NTL_POSIX_OPEN_STATUS_APPEND | NTL_POSIX_OPEN_STATUS_NONBLOCK) + +#define NTL_POSIX_SHARED_OPEN_MAGIC 0x31444F50u +#define NTL_POSIX_SHARED_OPEN_VERSION 2u + +#define NTL_POSIX_FD_INHERIT_MAX 64u +#define NTL_POSIX_FD_IMPORT_BATCH_MAX SYS_PORT_MAX_TRANSFER_HANDLES +#define NTL_POSIX_FD_INHERIT_FLAG_NONE 0u +#define NTL_POSIX_FD_DESCRIPTOR_CLOEXEC 0x00000001u +#define NTL_POSIX_FD_HANDLE_NONE UINT32_MAX + +typedef struct NTL_POSIX_FD_MANIFEST_ENTRY { + int32_t Descriptor; + uint32_t DescriptorFlags; + uint64_t OpenDescriptionId; +} NTL_POSIX_FD_MANIFEST_ENTRY; + +typedef struct NTL_POSIX_FD_DESCRIPTION_ENTRY { + uint64_t OpenDescriptionId; + uint64_t SharedOpenDescription; + uint32_t OpenDescriptionType; + uint32_t OpenStatusFlags; + uint32_t HandleIndex; + uint32_t Reserved; +} NTL_POSIX_FD_DESCRIPTION_ENTRY; + +// Hot open-description state lives in posixsrv-owned userspace memory. In the +// single-address-space model clients use this pointer directly; lifetime remains +// authoritative in posixsrv rather than in writable shared fields. +typedef struct NTL_POSIX_SHARED_OPEN_DESCRIPTION { + uint32_t Magic; + uint32_t Version; + uint32_t Type; + uint32_t Reserved; + uint64_t DescriptionId; + volatile long StatusFlags; + uint32_t Reserved2; + // Optional server-owned userspace data-plane pointer. Pipe endpoint + // descriptions use this for SYS_PIPE_SHARED_STATE; other types leave it zero. + uint64_t SharedObjectData; +} NTL_POSIX_SHARED_OPEN_DESCRIPTION; + +// Future server-owned spawn/exec builders place this opaque personality payload +// in the generic userspace process-loader startup data. Zero token/PID/generation +// means an ordinary process is self-registering for the first time. +#define NTL_POSIX_BOOTSTRAP_MAGIC 0x3258504Eu +#define NTL_POSIX_BOOTSTRAP_VERSION 2u + +typedef struct NTL_POSIX_BOOTSTRAP_DATA { + uint32_t Magic; + uint32_t Version; + uint32_t Size; + uint32_t Reserved; + uint64_t Token; + uint32_t PosixPid; + uint32_t Generation; + uint64_t FdInheritanceToken; +} NTL_POSIX_BOOTSTRAP_DATA; + +// Requests remain fixed-size through P4 open-description control. Later spawn/exec +// transactions may layer bounded multi-message payloads over this header. +typedef struct NTL_POSIX_REQUEST { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + uint32_t TransactionId; + uint32_t TransactionKind; + uint64_t BootstrapToken; + uint32_t BootstrapPosixPid; + uint32_t BootstrapGeneration; + + uint32_t TargetPosixPid; + uint32_t UserId; + uint32_t GroupId; + uint32_t ProcessGroupId; + uint32_t LookupKind; + uint32_t OpenDescriptionType; + uint32_t OpenStatusFlags; + uint64_t OpenDescriptionId; + uint32_t NameLength; + uint32_t PathLength; + uint64_t FdInheritanceToken; + uint32_t FdInheritanceIndex; + uint32_t FdInheritanceCount; + uint32_t FdInheritanceFlags; + uint32_t Reserved[2]; + NTL_POSIX_FD_MANIFEST_ENTRY FdManifest[NTL_POSIX_FD_INHERIT_MAX]; + char Name[NTL_POSIX_IDENTITY_NAME_MAX + 1u]; + char Path[SYS_PATH_MAX + 1u]; +} NTL_POSIX_REQUEST; + +// Replies expose stable POSIX identity and the server-owned shared process +// attributes. Lookup operations reuse the identity strings and ID fields. +typedef struct NTL_POSIX_REPLY { + uint32_t Size; + uint32_t Version; + uint32_t Operation; + uint32_t Flags; + KSTATUS Status; + uint32_t TransactionId; + uint32_t ClientProcessId; + uint32_t ClientSecurityId; + uint32_t ActiveTransactions; + uint32_t TransactionCapacity; + uint32_t PosixPid; + uint32_t NativeGeneration; + uint32_t ProcessState; + uint32_t RegisteredProcesses; + uint32_t RegisteredOpenDescriptions; + uint32_t OpenDescriptionType; + uint32_t OpenStatusFlags; + uint64_t OpenDescriptionId; + uint64_t SharedOpenDescription; + + uint32_t OpenDescriptionType2; + uint32_t OpenStatusFlags2; + uint64_t OpenDescriptionId2; + uint64_t SharedOpenDescription2; + + uint64_t FdInheritanceToken; + uint32_t FdInheritanceIndex; + uint32_t FdInheritanceCount; + uint32_t FdDescriptionIndex; + uint32_t FdDescriptionCount; + uint32_t FdDescriptionTotal; + NTL_POSIX_FD_MANIFEST_ENTRY FdManifest[NTL_POSIX_FD_INHERIT_MAX]; + NTL_POSIX_FD_DESCRIPTION_ENTRY FdDescriptions[NTL_POSIX_FD_IMPORT_BATCH_MAX]; + + uint32_t ParentPosixPid; + uint32_t UserId; + uint32_t EffectiveUserId; + uint32_t GroupId; + uint32_t EffectiveGroupId; + uint32_t ProcessGroupId; + uint32_t SessionId; + uint32_t SupplementaryGroupCount; + uint32_t SupplementaryGroups[NTL_POSIX_SUPPLEMENTARY_GROUP_MAX]; + + uint32_t NameLength; + uint32_t HomeLength; + uint32_t ShellLength; + uint32_t CurrentDirectoryLength; + char Name[NTL_POSIX_IDENTITY_NAME_MAX + 1u]; + char Home[NTL_POSIX_IDENTITY_HOME_MAX + 1u]; + char Shell[NTL_POSIX_IDENTITY_SHELL_MAX + 1u]; + char CurrentDirectory[SYS_PATH_MAX + 1u]; +} NTL_POSIX_REPLY; diff --git a/code/public/ntlite/process_loader.h b/code/public/ntlite/process_loader.h index 5b8804d..9aa78e6 100644 --- a/code/public/ntlite/process_loader.h +++ b/code/public/ntlite/process_loader.h @@ -2,6 +2,7 @@ #pragma once +#include #include #include #include @@ -10,6 +11,7 @@ #include #define NTL_PROCESS_LOAD_FLAG_NONE 0x00000000u +#define NTL_PROCESS_STARTUP_DATA_MAX 1024u // Describes one source capability to duplicate into a constructing child. typedef struct NTL_PROCESS_STARTUP_CAPABILITY { @@ -35,6 +37,9 @@ typedef struct NTL_CREATE_PROCESS_AT_REQUEST { void* ResolveContext; const NTL_PROCESS_STARTUP_CAPABILITY* StartupCapabilities; uint32_t StartupCapabilityCount; + // Optional opaque userspace bootstrap data copied into the child's read-only loader block. + const void* StartupData; + size_t StartupDataSize; SYS_HANDLE* ProcessHandleOut; } NTL_CREATE_PROCESS_AT_REQUEST; @@ -54,6 +59,9 @@ typedef struct NTL_CREATE_SYSTEM_PROCESS_REQUEST { void* ResolveContext; const NTL_PROCESS_STARTUP_CAPABILITY* StartupCapabilities; uint32_t StartupCapabilityCount; + // Optional opaque userspace bootstrap data copied into the child's read-only loader block. + const void* StartupData; + size_t StartupDataSize; SYS_HANDLE* ProcessHandleOut; } NTL_CREATE_SYSTEM_PROCESS_REQUEST; @@ -67,5 +75,9 @@ typedef struct NTL_CREATE_SYSTEM_PROCESS_REQUEST { NTL_PROCESS_LOADER_API KSTATUS NtlCreateProcessAt(const NTL_CREATE_PROCESS_AT_REQUEST* request); NTL_PROCESS_LOADER_API KSTATUS NtlCreateSystemProcess(const NTL_CREATE_SYSTEM_PROCESS_REQUEST* request); +// Copies the optional opaque startup data supplied by the userspace process builder. +NTL_PROCESS_LOADER_API KSTATUS NtlGetProcessStartupData(void* buffer, + size_t bufferSize, + size_t* sizeOut); #undef NTL_PROCESS_LOADER_API diff --git a/code/public/ntlite/resource.h b/code/public/ntlite/resource.h index 339d8f6..c283633 100644 --- a/code/public/ntlite/resource.h +++ b/code/public/ntlite/resource.h @@ -7,25 +7,31 @@ #include #include -#define NTL_RESOURCE_PROTOCOL_VERSION 2u +#define NTL_RESOURCE_PROTOCOL_VERSION 3u #define NTL_RESOURCE_PATH_MAX 1024u +#define NTL_RESOURCE_NAME_MAX 255u #define NTL_RESOURCE_FLAG_NONE 0x00000000u #define NTL_RESOURCE_RESOLVE_FLAG_MODIFY 0x00000001u -#define NTL_RESOURCE_OP_LOOKUP 1u -#define NTL_RESOURCE_OP_QUERY 2u -#define NTL_RESOURCE_OP_BIND 3u -#define NTL_RESOURCE_OP_UNBIND 4u -#define NTL_RESOURCE_OP_RESOLVE 5u +#define NTL_RESOURCE_OP_LOOKUP 1u +#define NTL_RESOURCE_OP_QUERY 2u +#define NTL_RESOURCE_OP_BIND 3u +#define NTL_RESOURCE_OP_UNBIND 4u +#define NTL_RESOURCE_OP_RESOLVE 5u +#define NTL_RESOURCE_OP_ENUMERATE 6u +#define NTL_RESOURCE_OP_CREATE_DIRECTORY 7u +#define NTL_RESOURCE_OP_CREATE_SYMBOLIC_LINK 8u -#define NTL_RESOURCE_KIND_INVALID 0u -#define NTL_RESOURCE_KIND_CAPABILITY 1u -#define NTL_RESOURCE_KIND_VOLUME 2u -#define NTL_RESOURCE_KIND_DIRECTORY 3u -#define NTL_RESOURCE_KIND_DEVICE 4u +#define NTL_RESOURCE_KIND_INVALID 0u +#define NTL_RESOURCE_KIND_CAPABILITY 1u +#define NTL_RESOURCE_KIND_VOLUME 2u +#define NTL_RESOURCE_KIND_DIRECTORY 3u +#define NTL_RESOURCE_KIND_DEVICE 4u +#define NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY 5u +#define NTL_RESOURCE_KIND_SYMBOLIC_LINK 6u -// Request header followed immediately by PathLength bytes of canonical native resource path. +// Request header followed by PathLength bytes of canonical native resource path and AuxLength operation-specific bytes. typedef struct NTL_RESOURCE_REQUEST { uint32_t Size; uint32_t Version; @@ -35,10 +41,10 @@ typedef struct NTL_RESOURCE_REQUEST { SYS_RIGHTS ExportRights; uint32_t Kind; uint32_t PathLength; - uint32_t Reserved; + uint32_t AuxLength; } NTL_RESOURCE_REQUEST; -// Every resource request receives one fixed-size reply. Successful lookup additionally transfers one handle. +// Every ordinary resource request receives one fixed-size reply. Enumeration appends NameLength bytes of one child name. typedef struct NTL_RESOURCE_REPLY { uint32_t Size; uint32_t Version; @@ -47,9 +53,21 @@ typedef struct NTL_RESOURCE_REPLY { KSTATUS Status; uint32_t Kind; uint32_t ResolvedLength; - uint32_t Reserved; + uint32_t NameLength; } NTL_RESOURCE_REPLY; +// Stateless lexicographic enumeration cursor. Callers zero-initialize it before the first request. +typedef struct NTL_RESOURCE_ENUM_CURSOR { + uint32_t NameLength; + char Name[NTL_RESOURCE_NAME_MAX + 1u]; +} NTL_RESOURCE_ENUM_CURSOR; + +// One immediate child of an RSSRV namespace directory. +typedef struct NTL_RESOURCE_ENUM_ENTRY { + uint32_t Kind; + char Name[NTL_RESOURCE_NAME_MAX + 1u]; +} NTL_RESOURCE_ENUM_ENTRY; + #if defined(NTL_SYSCALL_STATIC) #define NTL_RESOURCE_API #elif defined(NTL_SYSCALL_BUILD) @@ -63,17 +81,17 @@ NTL_RESOURCE_API KSTATUS NtlResourceConnect(SYS_HANDLE* sessionOut); NTL_RESOURCE_API KSTATUS NtlResourceOpenSystemRoot(SYS_RIGHTS desiredRights, SYS_HANDLE* handleOut); // Connects one session through an explicitly delegated resource-listener capability. NTL_RESOURCE_API KSTATUS NtlResourceConnectListener(SYS_HANDLE listenerHandle, SYS_HANDLE* sessionOut); -// Resolves one canonical global resource name to an attenuated capability. +// Resolves one canonical global resource name to an attenuated capability. Symbolic links are followed. NTL_RESOURCE_API KSTATUS NtlResourceLookup(SYS_HANDLE session, SYS_STRING_VIEW path, SYS_RIGHTS desiredRights, SYS_HANDLE* handleOut); -// Queries resource metadata without transferring the resource capability. +// Queries the exact namespace node without following the final symbolic link. NTL_RESOURCE_API KSTATUS NtlResourceQuery(SYS_HANDLE session, SYS_STRING_VIEW path, uint32_t* kindOut); -// Resolves the longest bound resource prefix of one canonical absolute path. -// ResolvedLength receives the byte length of the matched resource name; the remaining +// Resolves the longest bound resource prefix of one canonical absolute path, following namespace symbolic links. +// ResolvedLength receives the byte length consumed from the caller's original resource path; the remaining // path, after one optional '/', is relative to the returned volume/directory capability. NTL_RESOURCE_API KSTATUS NtlResourceResolve(SYS_HANDLE session, SYS_STRING_VIEW path, @@ -81,6 +99,17 @@ NTL_RESOURCE_API KSTATUS NtlResourceResolve(SYS_HANDLE session, SYS_HANDLE* rootHandleOut, uint32_t* kindOut, uint32_t* resolvedLengthOut); +// Enumerates immediate children in stable bytewise-name order using a caller-owned stateless cursor. +NTL_RESOURCE_API KSTATUS NtlResourceEnumerate(SYS_HANDLE session, + SYS_STRING_VIEW directoryPath, + NTL_RESOURCE_ENUM_CURSOR* cursor, + NTL_RESOURCE_ENUM_ENTRY* entryOut); +// Creates one empty namespace directory through an administrative session. +NTL_RESOURCE_API KSTATUS NtlResourceCreateDirectory(SYS_HANDLE session, SYS_STRING_VIEW path); +// Creates one absolute namespace symbolic link through an administrative session. +NTL_RESOURCE_API KSTATUS NtlResourceCreateSymbolicLink(SYS_HANDLE session, + SYS_STRING_VIEW path, + SYS_STRING_VIEW target); // Binds one capability into an administrative session with an explicit namespace-rights grant and export ceiling. NTL_RESOURCE_API KSTATUS NtlResourceBind(SYS_HANDLE session, SYS_STRING_VIEW path, @@ -88,7 +117,7 @@ NTL_RESOURCE_API KSTATUS NtlResourceBind(SYS_HANDLE session, SYS_HANDLE resourceHandle, SYS_RIGHTS namespaceRights, SYS_RIGHTS exportRights); -// Removes one name from an administrative resource session. Existing looked-up capabilities remain valid. +// Removes one non-root namespace node through an administrative session. Namespace directories must be empty. NTL_RESOURCE_API KSTATUS NtlResourceUnbind(SYS_HANDLE session, SYS_STRING_VIEW path); // Disconnects and closes one resource session endpoint. NTL_RESOURCE_API KSTATUS NtlResourceClose(SYS_HANDLE session); diff --git a/code/public/ntlite/service_control.h b/code/public/ntlite/service_control.h index 70963ef..e37e173 100644 --- a/code/public/ntlite/service_control.h +++ b/code/public/ntlite/service_control.h @@ -5,19 +5,19 @@ #include #include -#define NTL_SERVICE_CONTROL_VERSION 1u +#define NTL_SERVICE_CONTROL_VERSION 2u #define NTL_SERVICE_CONTROL_READY 1u #define NTL_SERVICE_CONTROL_STOP 2u -#define NTL_SERVICE_READY_FLAG_RESOURCE_ADMIN_LISTENER 0x00000001u - // Service-control messages are notifications over a private Port connection. +// READY transfers ExportCount service-defined handles whose meaning is fixed by +// the service type known to the manager. Other operations transfer no handles. typedef struct NTL_SERVICE_CONTROL_MESSAGE { uint32_t Size; uint32_t Version; uint32_t Operation; - uint32_t Flags; + uint32_t ExportCount; KSTATUS Status; uint32_t Reserved[3]; } NTL_SERVICE_CONTROL_MESSAGE; diff --git a/code/public/ntlite/status.h b/code/public/ntlite/status.h index 3e191f0..c51825a 100644 --- a/code/public/ntlite/status.h +++ b/code/public/ntlite/status.h @@ -66,6 +66,17 @@ typedef uint32_t KSTATUS; #define NTLSTATUS_CAPABILITY_REVOKED KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x000017u) #define NTLSTATUS_LIMIT_REACHED KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x000018u) #define NTLSTATUS_PORT_DISCONNECTED KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x000019u) +#define NTLSTATUS_BROKEN_PIPE KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x00001Au) +#define NTLSTATUS_NOT_A_DIRECTORY KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x00001Bu) +#define NTLSTATUS_IS_A_DIRECTORY KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x00001Cu) +#define NTLSTATUS_DIRECTORY_NOT_EMPTY KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x00001Du) +#define NTLSTATUS_CROSS_DEVICE KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x00001Eu) +#define NTLSTATUS_NO_CHILD KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x00001Fu) +#define NTLSTATUS_INVALID_IMAGE_FORMAT KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x000020u) +#define NTLSTATUS_NAME_TOO_LONG KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x000021u) +#define NTLSTATUS_TOO_MANY_LINKS KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x000022u) +#define NTLSTATUS_READ_ONLY_FILESYSTEM KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x000023u) +#define NTLSTATUS_FILE_TOO_LARGE KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_SYSTEM, 0x000024u) // Process termination and fault statuses use the process facility. #define NTLSTATUS_PROCESS_BASE KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_PROCESS, 0x000000u) @@ -76,6 +87,7 @@ typedef uint32_t KSTATUS; #define NTLSTATUS_PROCESS_DIVIDE_BY_ZERO KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_PROCESS, 0x000013u) #define NTLSTATUS_PROCESS_BREAKPOINT KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_PROCESS, 0x000014u) #define NTLSTATUS_PROCESS_ALIGNMENT_FAULT KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_PROCESS, 0x000015u) +#define NTLSTATUS_PROCESS_USER_EXCEPTION KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_PROCESS, 0x000016u) #define NTLSTATUS_PROCESS_UNKNOWN_FAULT KSTATUS_MAKE_ERROR(NTLSTATUS_FACILITY_PROCESS, 0x00001Fu) #define KSUCCESS(status) (((KSTATUS)(status)) == NTLSTATUS_SUCCESS) diff --git a/code/public/ntlite/syscall.h b/code/public/ntlite/syscall.h index faed35b..09c5ae4 100644 --- a/code/public/ntlite/syscall.h +++ b/code/public/ntlite/syscall.h @@ -18,6 +18,7 @@ #include #include #include +#include // Syscall ABI convention: status-returning calls use KSTATUS. Syscalls use at most // four register arguments; larger operations use request structures. Every @@ -38,6 +39,9 @@ #define SYS_STARTUP_CAP_SERVICE_CONTROL 2u #define SYS_STARTUP_CAP_SYSTEM_VOLUME_NAMESPACE 3u #define SYS_STARTUP_CAP_VOLUME_MANAGER 4u +#define SYS_STARTUP_CAP_INPUT_MANAGER 5u +#define SYS_STARTUP_CAP_DISPLAY_MANAGER 6u +#define SYS_STARTUP_CAP_SESSION_CONTROL 7u #define SYS_STARTUP_CAPABILITY_MAX 32u typedef struct SYS_STARTUP_CAPABILITY { @@ -186,7 +190,7 @@ typedef struct SYS_TRAP_FRAME_INFO { // Structured exception delivery ABI. The kernel owns capture, delivery, and // context validation; personalities own VEH, SEH, signal, and unwind policy. -#define SYS_EXCEPTION_ABI_VERSION 1u +#define SYS_EXCEPTION_ABI_VERSION 2u #define SYS_EXCEPTION_MAXIMUM_PARAMETERS 15u #define SYS_EXCEPTION_FLAG_NONE 0x00000000u @@ -198,31 +202,28 @@ typedef struct SYS_TRAP_FRAME_INFO { #define SYS_RAISE_EXCEPTION_FLAG_NONE 0x00000000u #define SYS_RAISE_EXCEPTION_FLAG_NONCONTINUABLE 0x00000001u -// Common exception codes intentionally match the Windows values used by user -// VEH/SEH code. The native ABI remains policy-neutral; the matching numbers -// only avoid needless translation in the Win32 personality. -#define SYS_EXCEPTION_CODE_GUARD_PAGE_VIOLATION 0x80000001u -#define SYS_EXCEPTION_CODE_DATATYPE_MISALIGNMENT 0x80000002u -#define SYS_EXCEPTION_CODE_BREAKPOINT 0x80000003u -#define SYS_EXCEPTION_CODE_SINGLE_STEP 0x80000004u -#define SYS_EXCEPTION_CODE_ACCESS_VIOLATION 0xC0000005u -#define SYS_EXCEPTION_CODE_ILLEGAL_INSTRUCTION 0xC000001Du -#define SYS_EXCEPTION_CODE_NONCONTINUABLE_EXCEPTION 0xC0000025u -#define SYS_EXCEPTION_CODE_INVALID_DISPOSITION 0xC0000026u -#define SYS_EXCEPTION_CODE_ARRAY_BOUNDS_EXCEEDED 0xC000008Cu -#define SYS_EXCEPTION_CODE_FLT_DENORMAL_OPERAND 0xC000008Du -#define SYS_EXCEPTION_CODE_FLT_DIVIDE_BY_ZERO 0xC000008Eu -#define SYS_EXCEPTION_CODE_FLT_INEXACT_RESULT 0xC000008Fu -#define SYS_EXCEPTION_CODE_FLT_INVALID_OPERATION 0xC0000090u -#define SYS_EXCEPTION_CODE_FLT_OVERFLOW 0xC0000091u -#define SYS_EXCEPTION_CODE_FLT_STACK_CHECK 0xC0000092u -#define SYS_EXCEPTION_CODE_FLT_UNDERFLOW 0xC0000093u -#define SYS_EXCEPTION_CODE_INT_DIVIDE_BY_ZERO 0xC0000094u -#define SYS_EXCEPTION_CODE_INT_OVERFLOW 0xC0000095u -#define SYS_EXCEPTION_CODE_PRIVILEGED_INSTRUCTION 0xC0000096u -#define SYS_EXCEPTION_CODE_STACK_OVERFLOW 0xC00000FDu -#define SYS_EXCEPTION_CODE_NTLITE_UNKNOWN 0xE0000001u -#define SYS_EXCEPTION_CODE_NTLITE_USER_RAISED 0xE0000002u +// Native exception codes are an nt-lite ABI. Personality runtimes translate +// them to their own exception or signal namespaces at the user-mode boundary. +#define SYS_EXCEPTION_CODE_UNKNOWN 1u +#define SYS_EXCEPTION_CODE_USER_RAISED 2u +#define SYS_EXCEPTION_CODE_ACCESS_VIOLATION 3u +#define SYS_EXCEPTION_CODE_DATATYPE_MISALIGNMENT 4u +#define SYS_EXCEPTION_CODE_ILLEGAL_INSTRUCTION 5u +#define SYS_EXCEPTION_CODE_PRIVILEGED_INSTRUCTION 6u +#define SYS_EXCEPTION_CODE_BREAKPOINT 7u +#define SYS_EXCEPTION_CODE_SINGLE_STEP 8u +#define SYS_EXCEPTION_CODE_INT_DIVIDE_BY_ZERO 9u +#define SYS_EXCEPTION_CODE_INT_OVERFLOW 10u +#define SYS_EXCEPTION_CODE_ARRAY_BOUNDS_EXCEEDED 11u +#define SYS_EXCEPTION_CODE_FLT_DENORMAL_OPERAND 12u +#define SYS_EXCEPTION_CODE_FLT_DIVIDE_BY_ZERO 13u +#define SYS_EXCEPTION_CODE_FLT_INEXACT_RESULT 14u +#define SYS_EXCEPTION_CODE_FLT_INVALID_OPERATION 15u +#define SYS_EXCEPTION_CODE_FLT_OVERFLOW 16u +#define SYS_EXCEPTION_CODE_FLT_STACK_CHECK 17u +#define SYS_EXCEPTION_CODE_FLT_UNDERFLOW 18u +#define SYS_EXCEPTION_CODE_STACK_OVERFLOW 19u +#define SYS_EXCEPTION_CODE_GUARD_PAGE_VIOLATION 20u #define SYS_EXCEPTION_DISPOSITION_CONTINUE_EXECUTION 0u #define SYS_EXCEPTION_DISPOSITION_CONTINUE_SEARCH 1u @@ -283,7 +284,9 @@ typedef struct SYS_EXCEPTION_RECORD { uint64_t ErrorCode; uint32_t ParameterCount; - uint32_t Reserved; + // Meaningful only when Code is SYS_EXCEPTION_CODE_USER_RAISED. The native + // kernel treats this as an opaque personality/application value. + uint32_t UserCode; uint64_t Parameters[SYS_EXCEPTION_MAXIMUM_PARAMETERS]; } SYS_EXCEPTION_RECORD; @@ -327,7 +330,8 @@ typedef struct SYS_CONTINUE_EXCEPTION_REQUEST { typedef struct SYS_RAISE_EXCEPTION_REQUEST { uint32_t Size; uint32_t Flags; - uint32_t Code; + // Opaque user/personality exception value carried in SYS_EXCEPTION_RECORD.UserCode. + uint32_t UserCode; uint32_t ParameterCount; const uint64_t* Parameters; } SYS_RAISE_EXCEPTION_REQUEST; @@ -496,6 +500,72 @@ typedef struct SYS_UNLOCK_FILE_REQUEST { uint64_t Length; } SYS_UNLOCK_FILE_REQUEST; +#define SYS_FILE_LOCK_NONE 0u + +typedef struct SYS_FILE_LOCK_INFO { + uint32_t Size; + uint32_t Mode; + uint32_t OwnerProcessId; + uint32_t Reserved; + uint64_t Offset; + uint64_t Length; +} SYS_FILE_LOCK_INFO; + +typedef struct SYS_QUERY_FILE_LOCK_REQUEST { + uint32_t Size; + SYS_HANDLE FileHandle; + uint32_t Mode; + uint32_t Flags; + uint64_t Offset; + uint64_t Length; + SYS_FILE_LOCK_INFO* InfoOut; +} SYS_QUERY_FILE_LOCK_REQUEST; + +#define SYS_FILE_STATUS_APPEND 0x00000001u + +typedef struct SYS_SET_FILE_STATUS_REQUEST { + uint32_t Size; + SYS_HANDLE FileHandle; + uint32_t Mask; + uint32_t Flags; +} SYS_SET_FILE_STATUS_REQUEST; + +#define SYS_PIPE_ATOMIC_WRITE_MAX 4096u +#define SYS_PIPE_BUFFER_CAPACITY 16384u +#define SYS_PIPE_SHARED_VERSION 1u +#define SYS_PIPE_SHARED_MAGIC 0x31504950u +#define SYS_PIPE_SHARED_HEADER_BYTES 4096u + +// Shared pipe state. The first page is mapped read-only and contains +// kernel-authoritative lifecycle state. The following pages are writable and +// form the user-mode ring data plane. Positions are monotonic byte counters. +typedef struct SYS_PIPE_SHARED_STATE { + uint32_t Magic; + uint32_t Version; + uint32_t Capacity; + uint32_t HeaderBytes; + volatile long ReaderOpen; + volatile long WriterOpen; + uint8_t HeaderReserved[SYS_PIPE_SHARED_HEADER_BYTES - 24u]; + + volatile uint64_t ReadPosition; + volatile uint64_t WritePosition; + volatile long ReadLock; + volatile long WriteLock; + volatile long ReadWaiters; + volatile long WriteWaiters; + uint8_t RingReserved[16]; + uint8_t Buffer[SYS_PIPE_BUFFER_CAPACITY]; +} SYS_PIPE_SHARED_STATE; + +typedef struct SYS_CREATE_PIPE_REQUEST { + uint32_t Size; + uint32_t Flags; + SYS_HANDLE* ReadHandleOut; + SYS_HANDLE* WriteHandleOut; + SYS_PIPE_SHARED_STATE** SharedStateOut; +} SYS_CREATE_PIPE_REQUEST; + typedef struct SYS_WRITE_CONSOLE_REQUEST { uint32_t Size; SYS_STRING_VIEW Text; @@ -584,6 +654,8 @@ typedef struct SYS_CREATE_THREAD_REQUEST { uintptr_t Argument; uint32_t Priority; uint32_t Flags; + // Both outputs are optional for a running detached thread. Suspended creation + // requires ThreadHandleOut so the caller retains a capability to resume it. SYS_HANDLE* ThreadHandleOut; uint32_t* ThreadIdOut; } SYS_CREATE_THREAD_REQUEST; @@ -666,8 +738,11 @@ KSTATUS SysKillProcess(SYS_HANDLE processHandle, int32_t exitCode); #define SYS_THREAD_WAIT_SURFACE 16u #define SYS_THREAD_WAIT_TIMER 17u #define SYS_THREAD_WAIT_DISPLAY_CLOCK 18u +#define SYS_THREAD_WAIT_PIPE 19u #define SYS_THREAD_WAIT_UNKNOWN 0xffffffffu +#define SYS_THREAD_NAME_MAX NTL_THREAD_NAME_MAX + typedef struct SYS_THREAD_INFO { uint32_t Size; uint32_t ProcessId; @@ -691,8 +766,18 @@ typedef struct SYS_THREAD_INFO { uint32_t WaitObjectType; uint32_t WaitStatus; uint32_t KernelApcDisable; + + char Name[SYS_THREAD_NAME_MAX]; } SYS_THREAD_INFO; +// Sets one thread description. ThreadHandle == SYS_HANDLE_INVALID targets the +// calling thread. Name may be empty to clear the description. +typedef struct SYS_SET_THREAD_NAME_REQUEST { + uint32_t Size; + SYS_HANDLE ThreadHandle; + SYS_STRING_VIEW Name; +} SYS_SET_THREAD_NAME_REQUEST; + typedef struct SYS_PROCESS_INFO { uint32_t Size; uint32_t ProcessId; @@ -734,6 +819,8 @@ KSTATUS SysResumeThread(SYS_HANDLE threadHandle, uint32_t* previousSuspendCountO uint32_t SysGetCurrentTid(void); uint32_t SysGetCurrentPid(void); KSTATUS SysSetThreadPriority(uint32_t priority); +KSTATUS SysSetThreadName(const SYS_SET_THREAD_NAME_REQUEST* request); +KSTATUS SysQueryThreadName(SYS_HANDLE threadHandle, char* buffer, size_t bufferSize, size_t* lengthOut); KSTATUS SysYield(void); #define SYS_TIMER_FLAG_NONE 0u @@ -923,6 +1010,10 @@ KSTATUS SysTruncateFile(SYS_HANDLE handle, uint64_t newSize); KSTATUS SysFlushFile(SYS_HANDLE handle); KSTATUS SysLockFile(const SYS_LOCK_FILE_REQUEST* request); KSTATUS SysUnlockFile(const SYS_UNLOCK_FILE_REQUEST* request); +KSTATUS SysQueryFileLock(const SYS_QUERY_FILE_LOCK_REQUEST* request); +KSTATUS SysSetFileStatus(const SYS_SET_FILE_STATUS_REQUEST* request); +KSTATUS SysCreatePipe(const SYS_CREATE_PIPE_REQUEST* request); +KSTATUS SysNotifyPipe(SYS_HANDLE pipeHandle); KSTATUS SysReadDir(SYS_HANDLE handle, SYS_DIR_ENTRY* entryOut); @@ -983,13 +1074,39 @@ typedef struct SYS_CREATE_SURFACE_REQUEST { } SYS_CREATE_SURFACE_REQUEST; KSTATUS SysCreateSurface(const SYS_CREATE_SURFACE_REQUEST* request); -KSTATUS SysMapSurface(SYS_HANDLE surfaceHandle, void** baseOut, uint32_t* pitchBytesOut); +KSTATUS SysMapSurface(SYS_HANDLE surfaceHandle, uint32_t flags, void** baseOut, uint32_t* pitchBytesOut); +KSTATUS SysUnmapSurface(void* base); KSTATUS SysQuerySurface(SYS_HANDLE surfaceHandle, SYS_SURFACE_DESC* descOut); -KSTATUS SysPresentSurface(SYS_HANDLE surfaceHandle, const DISPLAY_RECT* rects, uint32_t rectCount); +KSTATUS SysCommitSurface(SYS_HANDLE surfaceHandle, const DISPLAY_RECT* rects, uint32_t rectCount, uint64_t* generationOut); KSTATUS SysClearScreen(uint32_t color); KSTATUS SysGetScreenSize(int* widthOut, int* heightOut); KSTATUS SysQueryDisplayInfo(SYS_DISPLAY_INFO* infoOut); +#define SYS_DISPLAY_MANAGER_CURSOR_FLAG_NONE 0x00000000u +#define SYS_DISPLAY_MANAGER_CURSOR_FLAG_SHOW 0x00000001u +#define SYS_DISPLAY_MANAGER_CURSOR_FLAG_HIDE 0x00000002u + +typedef struct SYS_DISPLAY_MANAGER_CURSOR_REQUEST { + uint32_t Size; + SYS_HANDLE ManagerHandle; + uint32_t Flags; + int32_t X; + int32_t Y; +} SYS_DISPLAY_MANAGER_CURSOR_REQUEST; + +// Capability-scoped display operations used by the native resource plane. +KSTATUS SysQueryDisplayManagerInfo(SYS_HANDLE managerHandle, SYS_DISPLAY_INFO* infoOut); +KSTATUS SysSetDisplayManagerCursor(const SYS_DISPLAY_MANAGER_CURSOR_REQUEST* request); +KSTATUS SysPresentDisplayManagerSurface(SYS_HANDLE managerHandle, SYS_HANDLE surfaceHandle, + const DISPLAY_RECT* rects, uint32_t rectCount); +KSTATUS SysWriteDisplayManagerConsole(SYS_HANDLE managerHandle, const SYS_WRITE_CONSOLE_REQUEST* request); +KSTATUS SysQueryDisplayManagerConsoleInfo(SYS_HANDLE managerHandle, SYS_CONSOLE_INFO* infoOut); +KSTATUS SysSetDisplayManagerConsoleCursor(SYS_HANDLE managerHandle, int x, int y); +KSTATUS SysFillDisplayManagerConsoleCharacter(SYS_HANDLE managerHandle, const SYS_FILL_CONSOLE_CHARACTER_REQUEST* request); +KSTATUS SysFillDisplayManagerConsoleAttribute(SYS_HANDLE managerHandle, const SYS_FILL_CONSOLE_ATTRIBUTE_REQUEST* request); +KSTATUS SysPutDisplayManagerConsoleCharacter(SYS_HANDLE managerHandle, const SYS_PUT_SCREEN_CHAR_REQUEST* request); +KSTATUS SysClearDisplayManagerConsole(SYS_HANDLE managerHandle, uint32_t color); + // Display clocks are waitable pacing objects. Providers with frame-event // support signal them once per submitted frame. Other providers use the boot processor // scheduler tick, with PeriodMilliseconds rounded up to one or more ticks. @@ -1006,14 +1123,38 @@ typedef struct SYS_OPEN_DISPLAY_CLOCK_REQUEST { KSTATUS SysOpenDisplayClock(const SYS_OPEN_DISPLAY_CLOCK_REQUEST* request); // Input. +#define SYS_INPUT_MANAGER_EVENT_FLAG_NONE 0x00000000u +#define SYS_INPUT_MANAGER_EVENT_KEY 1u +#define SYS_INPUT_MANAGER_EVENT_MOUSE 2u + +typedef struct SYS_INPUT_MANAGER_EVENT { + uint32_t Size; + uint32_t Type; + uint64_t Sequence; + union { + INPUT_KEY_EVENT Key; + INPUT_MOUSE_EVENT Mouse; + } Data; +} SYS_INPUT_MANAGER_EVENT; + +typedef struct SYS_INPUT_MANAGER_READ_REQUEST { + uint32_t Size; + SYS_HANDLE ManagerHandle; + uint32_t Flags; + uint64_t TimeoutMilliseconds; + SYS_INPUT_MANAGER_EVENT* EventOut; +} SYS_INPUT_MANAGER_READ_REQUEST; + +// Reads the next normalized physical input event through an input-manager capability. +KSTATUS SysReadInputManagerEvent(const SYS_INPUT_MANAGER_READ_REQUEST* request); + #define SYS_INPUT_QUEUE_FLAG_NONE 0x00000000u #define SYS_INPUT_EVENT_FLAG_NONE 0x00000000u #define SYS_GLOBAL_INPUT_STATE_FLAG_NONE 0x00000000u #define SYS_GLOBAL_INPUT_KEY_BYTES 32u -// SysQueryGlobalInputState snapshots the current physical input state. It does -// not consume queued keyboard or mouse events and is intentionally global; a -// later routed input context API should use a context handle instead. +// Legacy kernel-global input snapshot retained for compatibility. Normal interactive +// clients should consume RSSRV logical input through . typedef struct SYS_GLOBAL_INPUT_STATE { uint32_t Size; uint32_t Flags; @@ -1267,7 +1408,7 @@ typedef struct SYS_MEMORY_STATUS { // Public stats payloads start with Size and Version. Size is the byte count // returned for that payload, including any trailing records. Version describes // the field layout for fixed fields in the named payload. -#define SYS_STATS_VERSION 1u +#define SYS_STATS_VERSION 3u typedef struct SYS_EXCEPTION_STATS { diff --git a/code/public/ntlite/thread.h b/code/public/ntlite/thread.h new file mode 100644 index 0000000..253ba7a --- /dev/null +++ b/code/public/ntlite/thread.h @@ -0,0 +1,7 @@ +// Public thread constants shared by native user mode and kernel clients. + +#pragma once + +// Thread descriptions are UTF-8 byte strings stored inline with the thread. +// The limit includes the terminating NUL byte. +#define NTL_THREAD_NAME_MAX 64u diff --git a/code/public/posix/arpa/inet.h b/code/public/posix/arpa/inet.h new file mode 100644 index 0000000..9463f23 --- /dev/null +++ b/code/public/posix/arpa/inet.h @@ -0,0 +1,16 @@ +// Internet address conversion declarations for nt-lite POSIX. + +#pragma once + +#include +#include + +NTL_POSIX_API uint16_t htons(uint16_t value); +NTL_POSIX_API uint16_t ntohs(uint16_t value); +NTL_POSIX_API uint32_t htonl(uint32_t value); +NTL_POSIX_API uint32_t ntohl(uint32_t value); +NTL_POSIX_API in_addr_t inet_addr(const char* text); +NTL_POSIX_API char* inet_ntoa(struct in_addr address); +NTL_POSIX_API int inet_pton(int family, const char* source, void* destination); +NTL_POSIX_API const char* inet_ntop(int family, const void* source, + char* destination, socklen_t size); diff --git a/code/public/posix/dirent.h b/code/public/posix/dirent.h new file mode 100644 index 0000000..8bdd529 --- /dev/null +++ b/code/public/posix/dirent.h @@ -0,0 +1,28 @@ +// POSIX directory-stream declarations for nt-lite. + +#pragma once + +#include +#include +#include + +#define NAME_MAX 259 + +#define DT_UNKNOWN 0 +#define DT_DIR 4 +#define DT_REG 8 + +typedef struct dirent { + char d_name[NAME_MAX + 1]; + unsigned char d_type; +} dirent; + +typedef struct DIR { + uint32_t KHandle; + dirent Current; + bool Valid; +} DIR; + +NTL_POSIX_API DIR* opendir(const char* path); +NTL_POSIX_API struct dirent* readdir(DIR* directory); +NTL_POSIX_API int closedir(DIR* directory); diff --git a/code/public/posix/dlfcn.h b/code/public/posix/dlfcn.h new file mode 100644 index 0000000..5e29a2f --- /dev/null +++ b/code/public/posix/dlfcn.h @@ -0,0 +1,15 @@ +// nt-lite dynamic-loader POSIX extension declarations. + +#pragma once + +#include + +#define RTLD_LAZY 0x0001 +#define RTLD_NOW 0x0002 +#define RTLD_LOCAL 0x0004 +#define RTLD_GLOBAL 0x0008 + +NTL_POSIX_API void* dlopen(const char* filename, int flags); +NTL_POSIX_API void* dlsym(void* handle, const char* symbol); +NTL_POSIX_API int dlclose(void* handle); +NTL_POSIX_API char* dlerror(void); diff --git a/code/public/posix/errno.h b/code/public/posix/errno.h new file mode 100644 index 0000000..0edd297 --- /dev/null +++ b/code/public/posix/errno.h @@ -0,0 +1,50 @@ +// POSIX errno declarations backed by nt-lite per-thread personality state. + +#pragma once + +#include + +// errno values are an nt-lite POSIX personality ABI. POSIX specifies the +// symbolic names, not their numeric representation, so keep this table compact +// and personality-owned instead of inheriting numbers from another Unix ABI. +#define EPERM 1 +#define ENOENT 2 +#define ESRCH 3 +#define EINTR 4 +#define EIO 5 +#define ENOEXEC 6 +#define EBADF 7 +#define ECHILD 8 +#define ENOMEM 9 +#define EACCES 10 +#define EFAULT 11 +#define EEXIST 12 +#define EXDEV 13 +#define ENODEV 14 +#define ENOTDIR 15 +#define EISDIR 16 +#define EINVAL 17 +#define EMFILE 18 +#define EFBIG 19 +#define ENOSPC 20 +#define ESPIPE 21 +#define EROFS 22 +#define EMLINK 23 +#define EPIPE 24 +#define ERANGE 25 +#define EAGAIN 26 +#define EWOULDBLOCK EAGAIN +#define ENAMETOOLONG 27 +#define ENOSYS 28 +#define ENOTEMPTY 29 +#define EDEADLK 30 +#define ENOTSOCK 31 +#define EAFNOSUPPORT 32 +#define ECONNRESET 33 +#define EINPROGRESS 34 +#define ETIMEDOUT 35 +#define ECANCELED 36 + +NTL_POSIX_API int* __ntl_posix_errno_location(void); + +#define errno (*__ntl_posix_errno_location()) diff --git a/code/public/posix/fcntl.h b/code/public/posix/fcntl.h new file mode 100644 index 0000000..0436c01 --- /dev/null +++ b/code/public/posix/fcntl.h @@ -0,0 +1,41 @@ +// POSIX file-control declarations currently implemented by nt-lite. + +#pragma once + +#include +#include + +#define O_RDONLY 0x0000 +#define O_WRONLY 0x0001 +#define O_RDWR 0x0002 +#define O_CREAT 0x0040 +#define O_TRUNC 0x0200 +#define O_APPEND 0x0400 +#define O_NONBLOCK 0x0800 +#define O_ACCMODE 0x0003 + +#define F_DUPFD 0 +#define F_GETFD 1 +#define F_SETFD 2 +#define F_GETFL 3 +#define F_SETFL 4 +#define F_GETLK 5 +#define F_SETLK 6 +#define F_SETLKW 7 + +#define F_RDLCK 0 +#define F_WRLCK 1 +#define F_UNLCK 2 + +#define FD_CLOEXEC 1 + +struct flock { + short l_type; + short l_whence; + off_t l_start; + off_t l_len; + pid_t l_pid; +}; + +NTL_POSIX_API int open(const char* path, int flags, ...); +NTL_POSIX_API int fcntl(int fd, int command, ...); diff --git a/code/public/posix/grp.h b/code/public/posix/grp.h new file mode 100644 index 0000000..4d63769 --- /dev/null +++ b/code/public/posix/grp.h @@ -0,0 +1,15 @@ +// POSIX group-database declarations backed by posixsrv identity policy. + +#pragma once + +#include +#include + +struct group { + char* gr_name; + gid_t gr_gid; + char** gr_mem; +}; + +NTL_POSIX_API struct group* getgrnam(const char* name); +NTL_POSIX_API struct group* getgrgid(gid_t gid); diff --git a/code/public/posix/netdb.h b/code/public/posix/netdb.h new file mode 100644 index 0000000..1503a6f --- /dev/null +++ b/code/public/posix/netdb.h @@ -0,0 +1,34 @@ +// POSIX name-resolution declarations supported by nt-lite. + +#pragma once + +#include +#include + +struct hostent { + char* h_name; + char** h_aliases; + int h_addrtype; + int h_length; + char** h_addr_list; +}; + +#define h_addr h_addr_list[0] + +struct addrinfo { + int ai_flags; + int ai_family; + int ai_socktype; + int ai_protocol; + socklen_t ai_addrlen; + struct sockaddr* ai_addr; + char* ai_canonname; + struct addrinfo* ai_next; +}; + +#define AI_PASSIVE 0x0001 + +NTL_POSIX_API struct hostent* gethostbyname(const char* name); +NTL_POSIX_API int getaddrinfo(const char* node, const char* service, + const struct addrinfo* hints, struct addrinfo** result); +NTL_POSIX_API void freeaddrinfo(struct addrinfo* result); diff --git a/code/public/posix/netinet/in.h b/code/public/posix/netinet/in.h new file mode 100644 index 0000000..fab3f2c --- /dev/null +++ b/code/public/posix/netinet/in.h @@ -0,0 +1,40 @@ +// IPv4 and IPv6 address declarations for the nt-lite POSIX socket layer. + +#pragma once + +#include +#include + +typedef uint16_t in_port_t; +typedef uint32_t in_addr_t; + +#define IPPROTO_ICMP 1 +#define IPPROTO_TCP 6 +#define IPPROTO_UDP 17 +#define IPPROTO_ICMPV6 58 + +#define INADDR_ANY ((in_addr_t)0x00000000u) +#define INADDR_NONE ((in_addr_t)0xFFFFFFFFu) + +struct in_addr { + in_addr_t s_addr; +}; + +struct in6_addr { + uint8_t s6_addr[16]; +}; + +struct sockaddr_in { + sa_family_t sin_family; + in_port_t sin_port; + struct in_addr sin_addr; + char sin_zero[8]; +}; + +struct sockaddr_in6 { + sa_family_t sin6_family; + in_port_t sin6_port; + uint32_t sin6_flowinfo; + struct in6_addr sin6_addr; + uint32_t sin6_scope_id; +}; diff --git a/code/public/posix/ntlite/posix_extensions.h b/code/public/posix/ntlite/posix_extensions.h new file mode 100644 index 0000000..5a7eed3 --- /dev/null +++ b/code/public/posix/ntlite/posix_extensions.h @@ -0,0 +1,10 @@ +// nt-lite-specific extensions kept above the POSIX.1-1990-shaped profile. + +#pragma once + +#include + +NTL_POSIX_API const char* posix_get_command_line(void); +NTL_POSIX_API char* realpath(const char* path, char* resolvedPath); +NTL_POSIX_API void clearscreen(void); +NTL_POSIX_API void clearkeyboard(void); diff --git a/code/public/posix/ntlite_posix_api.h b/code/public/posix/ntlite_posix_api.h new file mode 100644 index 0000000..1cff965 --- /dev/null +++ b/code/public/posix/ntlite_posix_api.h @@ -0,0 +1,9 @@ +// nt-lite POSIX public ABI decoration shared by the POSIX include tree. + +#pragma once + +#ifdef NTL_POSIX_BUILD +#define NTL_POSIX_API __declspec(dllexport) +#else +#define NTL_POSIX_API __declspec(dllimport) +#endif diff --git a/code/public/posix/ntlite_posix_profile.def b/code/public/posix/ntlite_posix_profile.def new file mode 100644 index 0000000..63411d7 --- /dev/null +++ b/code/public/posix/ntlite_posix_profile.def @@ -0,0 +1,150 @@ +// nt-lite POSIX target manifest: name, profile class, implementation state. +// IMPLEMENTED is callable now, PARTIAL has intentionally incomplete semantics, +// PLANNED belongs to the active target, and UNSUPPORTED is deliberately rejected. + +POSIX_API(getpid, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getppid, POSIX1990_DEFERRED_PROCESS_MODEL, PLANNED) +POSIX_API(wait, POSIX1990_DEFERRED_PROCESS_MODEL, PLANNED) +POSIX_API(waitpid, POSIX1990_DEFERRED_PROCESS_MODEL, PARTIAL) +POSIX_API(_exit, POSIX1990_REQUIRED, PLANNED) +POSIX_API(fork, POSIX1990_DEFERRED_PROCESS_MODEL, UNSUPPORTED) +POSIX_API(vfork, POSIX1990_DEFERRED_PROCESS_MODEL, PLANNED) +POSIX_API(execl, POSIX1990_DEFERRED_PROCESS_MODEL, DEFERRED) +POSIX_API(execle, POSIX1990_DEFERRED_PROCESS_MODEL, DEFERRED) +POSIX_API(execlp, POSIX1990_DEFERRED_PROCESS_MODEL, DEFERRED) +POSIX_API(execv, POSIX1990_DEFERRED_PROCESS_MODEL, DEFERRED) +POSIX_API(execve, POSIX1990_DEFERRED_PROCESS_MODEL, PLANNED) +POSIX_API(execvp, POSIX1990_DEFERRED_PROCESS_MODEL, DEFERRED) + +POSIX_API(read, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(write, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(open, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(close, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(lseek, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(pipe, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(dup, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(dup2, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(fcntl, POSIX1990_REQUIRED, IMPLEMENTED) + +POSIX_API(chdir, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getcwd, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(unlink, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(stat, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(fstat, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(opendir, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(readdir, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(closedir, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(rewinddir, POSIX1990_REQUIRED, PLANNED) +POSIX_API(creat, POSIX1990_REQUIRED, PLANNED) +POSIX_API(umask, POSIX1990_REQUIRED, PLANNED) +POSIX_API(link, POSIX1990_REQUIRED, PLANNED) +POSIX_API(mkdir, POSIX1990_REQUIRED, PLANNED) +POSIX_API(mkfifo, POSIX1990_REQUIRED, PLANNED) +POSIX_API(rmdir, POSIX1990_REQUIRED, PLANNED) +POSIX_API(rename, POSIX1990_REQUIRED, PLANNED) +POSIX_API(access, POSIX1990_REQUIRED, PLANNED) +POSIX_API(chmod, POSIX1990_REQUIRED, PLANNED) +POSIX_API(fchmod, POSIX1990_REQUIRED, PLANNED) +POSIX_API(chown, POSIX1990_REQUIRED, PLANNED) +POSIX_API(fchown, POSIX1990_REQUIRED, PLANNED) +POSIX_API(utime, POSIX1990_REQUIRED, PLANNED) +POSIX_API(pathconf, POSIX1990_REQUIRED, PLANNED) +POSIX_API(fpathconf, POSIX1990_REQUIRED, PLANNED) + +POSIX_API(getuid, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(geteuid, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getgid, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getegid, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(setuid, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(setgid, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getgroups, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getlogin, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getpgrp, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(setpgid, POSIX1990_REQUIRED, PARTIAL) +POSIX_API(setsid, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(uname, POSIX1990_REQUIRED, PLANNED) +POSIX_API(time, POSIX1990_REQUIRED, PLANNED) +POSIX_API(times, POSIX1990_REQUIRED, PLANNED) +POSIX_API(getenv, POSIX1990_REQUIRED, PLANNED) +POSIX_API(ctermid, POSIX1990_REQUIRED, PLANNED) +POSIX_API(ttyname, POSIX1990_REQUIRED, PLANNED) +POSIX_API(isatty, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sysconf, POSIX1990_REQUIRED, PLANNED) + +POSIX_API(sigemptyset, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sigfillset, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sigaddset, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sigdelset, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sigismember, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sigaction, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sigprocmask, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sigpending, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sigsuspend, POSIX1990_REQUIRED, PLANNED) +POSIX_API(kill, POSIX1990_REQUIRED, PLANNED) +POSIX_API(alarm, POSIX1990_REQUIRED, PLANNED) +POSIX_API(pause, POSIX1990_REQUIRED, PLANNED) +POSIX_API(sleep, POSIX1990_REQUIRED, PLANNED) +POSIX_API(signal, POSIX1990_REQUIRED, PLANNED) +POSIX_API(raise, POSIX1990_REQUIRED, PLANNED) + +POSIX_API(cfgetispeed, POSIX1990_REQUIRED, PLANNED) +POSIX_API(cfgetospeed, POSIX1990_REQUIRED, PLANNED) +POSIX_API(cfsetispeed, POSIX1990_REQUIRED, PLANNED) +POSIX_API(cfsetospeed, POSIX1990_REQUIRED, PLANNED) +POSIX_API(tcgetattr, POSIX1990_REQUIRED, PLANNED) +POSIX_API(tcsetattr, POSIX1990_REQUIRED, PLANNED) +POSIX_API(tcsendbreak, POSIX1990_REQUIRED, PLANNED) +POSIX_API(tcdrain, POSIX1990_REQUIRED, PLANNED) +POSIX_API(tcflush, POSIX1990_REQUIRED, PLANNED) +POSIX_API(tcflow, POSIX1990_REQUIRED, PLANNED) +POSIX_API(tcgetpgrp, POSIX1990_REQUIRED, PLANNED) +POSIX_API(tcsetpgrp, POSIX1990_REQUIRED, PLANNED) +POSIX_API(getpwnam, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getpwuid, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getgrnam, POSIX1990_REQUIRED, IMPLEMENTED) +POSIX_API(getgrgid, POSIX1990_REQUIRED, IMPLEMENTED) + +POSIX_API(mmap, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(munmap, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(msync, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(dlopen, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(dlsym, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(dlclose, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(dlerror, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(pthread_create, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(pthread_join, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(pthread_exit, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(pthread_mutex_init, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(pthread_mutex_destroy, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(pthread_mutex_lock, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(pthread_mutex_trylock, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(pthread_mutex_unlock, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(posix_spawn, NTL_POSIX_EXTENSION, PARTIAL) +POSIX_API(socket, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(bind, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(listen, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(accept, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(connect, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(send, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(recv, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(sendto, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(recvfrom, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(shutdown, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(setsockopt, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(getsockopt, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(poll, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(htons, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(ntohs, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(htonl, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(ntohl, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(inet_addr, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(inet_ntoa, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(inet_pton, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(inet_ntop, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(gethostbyname, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(getaddrinfo, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(freeaddrinfo, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(posix_get_command_line, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(realpath, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(clearscreen, NTL_POSIX_EXTENSION, IMPLEMENTED) +POSIX_API(clearkeyboard, NTL_POSIX_EXTENSION, IMPLEMENTED) diff --git a/code/public/posix/ntlite_posix_profile.h b/code/public/posix/ntlite_posix_profile.h new file mode 100644 index 0000000..8fa6dc4 --- /dev/null +++ b/code/public/posix/ntlite_posix_profile.h @@ -0,0 +1,13 @@ +// nt-lite POSIX profile identity and deliberate conformance limits. + +#pragma once + +#define NTL_POSIX_PROFILE_VERSION 199009L +#define NTL_POSIX_FORMAL_CONFORMANCE 0 +#define NTL_POSIX_HAS_FORK 0 +#define NTL_POSIX_HAS_VFORK 0 +#define NTL_POSIX_HAS_EXECVE 0 +#define NTL_POSIX_HAS_NATIVE_EXEC_REPLACEMENT 0 +#define NTL_POSIX_HAS_POSIX_SPAWN 1 + +// ntlite_posix_profile.def is the X-macro API target/implementation manifest. diff --git a/code/public/posix/poll.h b/code/public/posix/poll.h new file mode 100644 index 0000000..b29a543 --- /dev/null +++ b/code/public/posix/poll.h @@ -0,0 +1,21 @@ +// POSIX poll declarations supported by nt-lite. + +#pragma once + +#include + +typedef unsigned long nfds_t; + +struct pollfd { + int fd; + short events; + short revents; +}; + +#define POLLIN 0x0001 +#define POLLOUT 0x0002 +#define POLLERR 0x0004 +#define POLLHUP 0x0008 +#define POLLNVAL 0x0010 + +NTL_POSIX_API int poll(struct pollfd* descriptors, nfds_t count, int timeout); diff --git a/code/public/posix/pthread.h b/code/public/posix/pthread.h new file mode 100644 index 0000000..51d80c3 --- /dev/null +++ b/code/public/posix/pthread.h @@ -0,0 +1,28 @@ +// nt-lite pthread extension declarations. + +#pragma once + +#include +#include + +typedef struct pthread_t { + uint32_t Handle; + uint32_t Tid; +} pthread_t; + +typedef struct pthread_mutex_t { + uint32_t Handle; +} pthread_mutex_t; + +typedef void* pthread_attr_t; +typedef void* pthread_mutexattr_t; + +NTL_POSIX_API int pthread_create(pthread_t* thread, const pthread_attr_t* attr, + void* (*routine)(void*), void* argument); +NTL_POSIX_API int pthread_join(pthread_t thread, void** result); +NTL_POSIX_API void pthread_exit(void* result); +NTL_POSIX_API int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr); +NTL_POSIX_API int pthread_mutex_destroy(pthread_mutex_t* mutex); +NTL_POSIX_API int pthread_mutex_lock(pthread_mutex_t* mutex); +NTL_POSIX_API int pthread_mutex_trylock(pthread_mutex_t* mutex); +NTL_POSIX_API int pthread_mutex_unlock(pthread_mutex_t* mutex); diff --git a/code/public/posix/pwd.h b/code/public/posix/pwd.h new file mode 100644 index 0000000..8f70998 --- /dev/null +++ b/code/public/posix/pwd.h @@ -0,0 +1,17 @@ +// POSIX password-database declarations backed by posixsrv identity policy. + +#pragma once + +#include +#include + +struct passwd { + char* pw_name; + uid_t pw_uid; + gid_t pw_gid; + char* pw_dir; + char* pw_shell; +}; + +NTL_POSIX_API struct passwd* getpwnam(const char* name); +NTL_POSIX_API struct passwd* getpwuid(uid_t uid); diff --git a/code/public/posix/signal.h b/code/public/posix/signal.h new file mode 100644 index 0000000..a79a14e --- /dev/null +++ b/code/public/posix/signal.h @@ -0,0 +1,8 @@ +// POSIX signal type declarations reserved for the next runtime phase. + +#pragma once + +#include + +typedef uint64_t sigset_t; +typedef int sig_atomic_t; diff --git a/code/public/posix/spawn.h b/code/public/posix/spawn.h new file mode 100644 index 0000000..28408a7 --- /dev/null +++ b/code/public/posix/spawn.h @@ -0,0 +1,14 @@ +// nt-lite posix_spawn declarations. + +#pragma once + +#include +#include + +typedef void* posix_spawn_file_actions_t; +typedef void* posix_spawnattr_t; + +NTL_POSIX_API int posix_spawn(pid_t* pid, const char* path, + const posix_spawn_file_actions_t* fileActions, + const posix_spawnattr_t* attributes, + char* const argv[], char* const envp[]); diff --git a/code/public/posix/stdlib.h b/code/public/posix/stdlib.h new file mode 100644 index 0000000..33a05d5 --- /dev/null +++ b/code/public/posix/stdlib.h @@ -0,0 +1,12 @@ +// C allocation and process-exit declarations exported by nt-lite posix.dll. + +#pragma once + +#include +#include + +NTL_POSIX_API void* malloc(size_t size); +NTL_POSIX_API void* calloc(size_t count, size_t size); +NTL_POSIX_API void* realloc(void* pointer, size_t size); +NTL_POSIX_API void free(void* pointer); +NTL_POSIX_API void exit(int status); diff --git a/code/public/posix/sys/mman.h b/code/public/posix/sys/mman.h new file mode 100644 index 0000000..e863561 --- /dev/null +++ b/code/public/posix/sys/mman.h @@ -0,0 +1,24 @@ +// nt-lite mmap extension declarations. + +#pragma once + +#include +#include +#include +#include + +#define PROT_NONE 0x0 +#define PROT_READ 0x1 +#define PROT_WRITE 0x2 +#define PROT_EXEC 0x4 +#define MAP_SHARED 0x01 +#define MAP_PRIVATE 0x02 +#define MAP_FAILED ((void*)(intptr_t)-1) +#define MS_ASYNC 0x01 +#define MS_SYNC 0x04 +#define MS_INVALIDATE 0x02 + +NTL_POSIX_API void* mmap(void* address, size_t length, int protection, + int flags, int fd, off_t offset); +NTL_POSIX_API int munmap(void* address, size_t length); +NTL_POSIX_API int msync(void* address, size_t length, int flags); diff --git a/code/public/posix/sys/socket.h b/code/public/posix/sys/socket.h new file mode 100644 index 0000000..a17af30 --- /dev/null +++ b/code/public/posix/sys/socket.h @@ -0,0 +1,50 @@ +// POSIX socket declarations supported by nt-lite. + +#pragma once + +#include +#include +#include +#include + +typedef uint16_t sa_family_t; +typedef uint32_t socklen_t; + +#define AF_UNSPEC 0 +#define AF_INET 2 +#define AF_INET6 10 +#define PF_INET AF_INET +#define PF_INET6 AF_INET6 + +#define SOCK_STREAM 1 +#define SOCK_DGRAM 2 +#define SOCK_RAW 3 + +#define SHUT_RD 0 +#define SHUT_WR 1 +#define SHUT_RDWR 2 + +#define SOL_SOCKET 1 +#define SO_RCVTIMEO 1 +#define SO_SNDTIMEO 2 +#define SO_NONBLOCK 3 + +struct sockaddr { + sa_family_t sa_family; + char sa_data[14]; +}; + +NTL_POSIX_API int socket(int domain, int type, int protocol); +NTL_POSIX_API int bind(int fd, const struct sockaddr* address, socklen_t addressLength); +NTL_POSIX_API int listen(int fd, int backlog); +NTL_POSIX_API int accept(int fd, struct sockaddr* address, socklen_t* addressLength); +NTL_POSIX_API int connect(int fd, const struct sockaddr* address, socklen_t addressLength); +NTL_POSIX_API ssize_t send(int fd, const void* buffer, size_t length, int flags); +NTL_POSIX_API ssize_t recv(int fd, void* buffer, size_t length, int flags); +NTL_POSIX_API ssize_t sendto(int fd, const void* buffer, size_t length, int flags, + const struct sockaddr* destination, socklen_t destinationLength); +NTL_POSIX_API ssize_t recvfrom(int fd, void* buffer, size_t length, int flags, + struct sockaddr* source, socklen_t* sourceLength); +NTL_POSIX_API int shutdown(int fd, int how); +NTL_POSIX_API int setsockopt(int fd, int level, int option, const void* value, socklen_t valueLength); +NTL_POSIX_API int getsockopt(int fd, int level, int option, void* value, socklen_t* valueLength); diff --git a/code/public/posix/sys/stat.h b/code/public/posix/sys/stat.h new file mode 100644 index 0000000..99bd53a --- /dev/null +++ b/code/public/posix/sys/stat.h @@ -0,0 +1,46 @@ +// POSIX file-status declarations for nt-lite. + +#pragma once + +#include +#include +#include + +#define S_IFMT 0170000 +#define S_IFSOCK 0140000 +#define S_IFIFO 0010000 +#define S_IFCHR 0020000 +#define S_IFDIR 0040000 +#define S_IFREG 0100000 +#define S_IRUSR 0000400 +#define S_IWUSR 0000200 +#define S_IXUSR 0000100 +#define S_IRGRP 0000040 +#define S_IWGRP 0000020 +#define S_IXGRP 0000010 +#define S_IROTH 0000004 +#define S_IWOTH 0000002 +#define S_IXOTH 0000001 + +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) +#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) +#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) + +struct stat { + uint64_t st_dev; + uint64_t st_ino; + mode_t st_mode; + uint32_t st_nlink; + uid_t st_uid; + gid_t st_gid; + uint64_t st_rdev; + off_t st_size; + time_t st_atime; + time_t st_mtime; + time_t st_ctime; +}; + +NTL_POSIX_API int stat(const char* path, struct stat* out); +NTL_POSIX_API int fstat(int fd, struct stat* out); diff --git a/code/public/posix/sys/time.h b/code/public/posix/sys/time.h new file mode 100644 index 0000000..294a36f --- /dev/null +++ b/code/public/posix/sys/time.h @@ -0,0 +1,8 @@ +// POSIX time-value declarations used by nt-lite socket options. + +#pragma once + +struct timeval { + long tv_sec; + long tv_usec; +}; diff --git a/code/public/posix/sys/times.h b/code/public/posix/sys/times.h new file mode 100644 index 0000000..9ee43bb --- /dev/null +++ b/code/public/posix/sys/times.h @@ -0,0 +1,12 @@ +// POSIX process-time declarations reserved for the process facade. + +#pragma once + +#include + +struct tms { + clock_t tms_utime; + clock_t tms_stime; + clock_t tms_cutime; + clock_t tms_cstime; +}; diff --git a/code/public/posix/sys/types.h b/code/public/posix/sys/types.h new file mode 100644 index 0000000..c30a7ca --- /dev/null +++ b/code/public/posix/sys/types.h @@ -0,0 +1,15 @@ +// POSIX scalar type declarations for nt-lite. + +#pragma once + +#include +#include + +typedef int32_t pid_t; +typedef int32_t off_t; +typedef uint32_t mode_t; +typedef uint32_t uid_t; +typedef uint32_t gid_t; +typedef int64_t ssize_t; +typedef int64_t time_t; +typedef int64_t clock_t; diff --git a/code/public/posix/sys/utsname.h b/code/public/posix/sys/utsname.h new file mode 100644 index 0000000..e7ad2cd --- /dev/null +++ b/code/public/posix/sys/utsname.h @@ -0,0 +1,13 @@ +// POSIX system-name declarations reserved for the identity facade. + +#pragma once + +#define _UTSNAME_LENGTH 65 + +struct utsname { + char sysname[_UTSNAME_LENGTH]; + char nodename[_UTSNAME_LENGTH]; + char release[_UTSNAME_LENGTH]; + char version[_UTSNAME_LENGTH]; + char machine[_UTSNAME_LENGTH]; +}; diff --git a/code/public/posix/sys/wait.h b/code/public/posix/sys/wait.h new file mode 100644 index 0000000..39ec2af --- /dev/null +++ b/code/public/posix/sys/wait.h @@ -0,0 +1,10 @@ +// POSIX wait declarations for nt-lite spawn associations. + +#pragma once + +#include +#include + +#define WEXITSTATUS(status) (((status) >> 8) & 0xff) + +NTL_POSIX_API pid_t waitpid(pid_t pid, int* status, int options); diff --git a/code/public/posix/termios.h b/code/public/posix/termios.h new file mode 100644 index 0000000..97af895 --- /dev/null +++ b/code/public/posix/termios.h @@ -0,0 +1,19 @@ +// POSIX terminal type declarations reserved for the tty phase. + +#pragma once + +#include + +typedef uint32_t tcflag_t; +typedef uint8_t cc_t; +typedef uint32_t speed_t; + +#define NCCS 20 + +struct termios { + tcflag_t c_iflag; + tcflag_t c_oflag; + tcflag_t c_cflag; + tcflag_t c_lflag; + cc_t c_cc[NCCS]; +}; diff --git a/code/public/posix/unistd.h b/code/public/posix/unistd.h new file mode 100644 index 0000000..165c191 --- /dev/null +++ b/code/public/posix/unistd.h @@ -0,0 +1,39 @@ +// POSIX process, path, and byte-stream declarations for nt-lite. + +#pragma once + +#include +#include +#include +#include + +#define _POSIX_VERSION NTL_POSIX_PROFILE_VERSION + +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 + +#define PIPE_BUF 4096 + +NTL_POSIX_API ssize_t read(int fd, void* buffer, size_t count); +NTL_POSIX_API ssize_t write(int fd, const void* buffer, size_t count); +NTL_POSIX_API int close(int fd); +NTL_POSIX_API int dup(int oldfd); +NTL_POSIX_API int dup2(int oldfd, int newfd); +NTL_POSIX_API int pipe(int pipefd[2]); +NTL_POSIX_API off_t lseek(int fd, off_t offset, int whence); +NTL_POSIX_API int chdir(const char* path); +NTL_POSIX_API char* getcwd(char* buffer, size_t size); +NTL_POSIX_API int unlink(const char* path); +NTL_POSIX_API pid_t getpid(void); +NTL_POSIX_API uid_t getuid(void); +NTL_POSIX_API uid_t geteuid(void); +NTL_POSIX_API gid_t getgid(void); +NTL_POSIX_API gid_t getegid(void); +NTL_POSIX_API int setuid(uid_t uid); +NTL_POSIX_API int setgid(gid_t gid); +NTL_POSIX_API int getgroups(int size, gid_t list[]); +NTL_POSIX_API char* getlogin(void); +NTL_POSIX_API pid_t getpgrp(void); +NTL_POSIX_API int setpgid(pid_t pid, pid_t pgid); +NTL_POSIX_API pid_t setsid(void); diff --git a/code/public/posix/utime.h b/code/public/posix/utime.h new file mode 100644 index 0000000..4d70114 --- /dev/null +++ b/code/public/posix/utime.h @@ -0,0 +1,10 @@ +// POSIX file-time declarations reserved for the filesystem facade. + +#pragma once + +#include + +struct utimbuf { + time_t actime; + time_t modtime; +}; diff --git a/code/services.ini b/code/services.ini new file mode 100644 index 0000000..f7509cd --- /dev/null +++ b/code/services.ini @@ -0,0 +1,37 @@ +; nt-lite user-mode system services. +; +; init.exe loads this mandatory file from the bootstrap system volume. Service +; identifiers are the case-sensitive suffix of [service.]. + +[service.rssrv] +type=resource +image=system/rssrv.exe +start=auto +critical=true +restart=never +ready_timeout_ms=5000 +stop_timeout_ms=5000 +requires= + +; Generic services export one public Port listener in READY. Init publishes it at +; /Services/ through RSSRV and supervises the process with restart=never. + +[service.posix] +type=generic +image=system/posixsrv.exe +start=auto +critical=false +restart=never +ready_timeout_ms=5000 +stop_timeout_ms=5000 +requires=rssrv + +[service.dwm] +type=dwm +image=system/dwm.exe +start=auto +critical=false +restart=never +ready_timeout_ms=5000 +stop_timeout_ms=5000 +requires=rssrv diff --git a/code/user/apps/doom/doom.c b/code/user/apps/doom/doom.c index 806a6e6..c2af0f4 100644 --- a/code/user/apps/doom/doom.c +++ b/code/user/apps/doom/doom.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include "doom_audio.h" @@ -23,8 +25,7 @@ int _fltused; // --------------------------------------------------------------------------- static void doom_print_wrapper(const char* msg) { - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(msg) }; - SysWriteConsole(&req); + if (msg) (void)NtlConsoleWrite(SYS_HANDLE_INVALID, msg, strlen(msg), NULL); } // --------------------------------------------------------------------------- @@ -275,7 +276,7 @@ static const KeyMap g_keymap[] = { static bool g_prev_key[KEYMAP_COUNT]; // Returns whether one key bit is set in a global input snapshot. -static bool ntlite_snapshot_key_down(const SYS_GLOBAL_INPUT_STATE* state, INPUT_KEY key) +static bool ntlite_snapshot_key_down(const NTL_INPUT_STATE* state, INPUT_KEY key) { if (!state || key >= 256u) return false; return (state->KeyDown[key >> 3] & (uint8_t)(1u << (key & 7u))) != 0; @@ -283,12 +284,12 @@ static bool ntlite_snapshot_key_down(const SYS_GLOBAL_INPUT_STATE* state, INPUT_ static void poll_input(void) { - SYS_GLOBAL_INPUT_STATE input; + NTL_INPUT_STATE input; memset(&input, 0, sizeof(input)); input.Size = sizeof(input); - input.Flags = SYS_GLOBAL_INPUT_STATE_FLAG_NONE; + input.Flags = NTL_INPUT_STATE_FLAG_NONE; - if (SysQueryGlobalInputState(&input) != NTLSTATUS_SUCCESS) + if (NtlInputQueryDefaultState(&input) != NTLSTATUS_SUCCESS) return; for (int i = 0; i < (int)KEYMAP_COUNT; i++) { @@ -334,7 +335,8 @@ static void init_surface(void) void* base = 0; uint32_t pitchBytes = 0; - s = SysMapSurface(g_surface_handle, &base, &pitchBytes); + s = SysMapSurface(g_surface_handle, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, + &base, &pitchBytes); if (s != NTLSTATUS_SUCCESS || !base || pitchBytes < DISP_W * sizeof(uint32_t)) { doom_print_wrapper("doom: SysMapSurface failed\n"); SysExitProcess(1); @@ -390,10 +392,9 @@ static void render_frame(const uint8_t* rgba) } DISPLAY_RECT rect = { 0, 0, DISP_W, DISP_H }; - uint32_t code = SysPresentSurface(g_surface_handle, &rect, 1); - if (code != NTLSTATUS_SUCCESS) { + uint32_t code = SysCommitSurface(g_surface_handle, &rect, 1, 0); + if (code != NTLSTATUS_SUCCESS) SysExitProcess(code); - } } static int build_argv_from_command_line(const char* cmdline, char* storage, size_t storageSize, @@ -465,7 +466,7 @@ int main(void) { const char* cmdline = SysGetPeb()->CommandLine.Data; - do { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SYS_SV_LITERAL("doom: starting\n") }; SysWriteConsole(&req); } while (0); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, "doom: starting\n", sizeof("doom: starting\n") - 1u, NULL); // Wire up PureDOOM core mechanics doom_set_print(doom_print_wrapper); @@ -503,12 +504,12 @@ int main(void) doom_print_wrapper("doom: parsed argc <= 0\n"); init_surface(); init_display_clock(); - do { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SYS_SV_LITERAL("doom: starting doom\n") }; SysWriteConsole(&req); } while (0); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, "doom: starting doom\n", sizeof("doom: starting doom\n") - 1u, NULL); doom_init(argc, argv, 0); - do { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SYS_SV_LITERAL("doom: starting audio\n") }; SysWriteConsole(&req); } while (0); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, "doom: starting audio\n", sizeof("doom: starting audio\n") - 1u, NULL); DoomAudioStart(); - do { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SYS_SV_LITERAL("doom: entering game loop\n") }; SysWriteConsole(&req); } while (0); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, "doom: entering game loop\n", sizeof("doom: entering game loop\n") - 1u, NULL); // Core execution loop. Doom advances at TICRATE, so rendering duplicate // frames as fast as possible only amplifies scheduler and input jitter. diff --git a/code/user/apps/doom/doom_audio.c b/code/user/apps/doom/doom_audio.c index 0756a0e..1ad1356 100644 --- a/code/user/apps/doom/doom_audio.c +++ b/code/user/apps/doom/doom_audio.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "doom_audio.h" @@ -213,8 +214,7 @@ static DOOM_AUDIO_STATE g_doomAudio; // Writes one complete diagnostic line to the process console. static void DoomAudioPrint(const char* text) { - SYS_WRITE_CONSOLE_REQUEST request = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(text) }; - (void)SysWriteConsole(&request); + if (text) (void)NtlConsoleWrite(SYS_HANDLE_INVALID, text, strlen(text), NULL); } // Opens the default available native render endpoint. @@ -755,6 +755,7 @@ void DoomAudioStart(void) { } memset(&thread, 0, sizeof(thread)); + thread.Size = sizeof(thread); thread.Entry = (uintptr_t)DoomAudioSynthLoader; thread.Priority = DOOM_AUDIO_SYNTH_LOADER_PRIORITY; thread.ThreadHandleOut = &g_doomAudio.SynthLoaderHandle; diff --git a/code/user/apps/edit/edit.c b/code/user/apps/edit/edit.c index 8aa9542..271b459 100644 --- a/code/user/apps/edit/edit.c +++ b/code/user/apps/edit/edit.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #define GLYPH_W 8 @@ -75,13 +77,14 @@ static void invalidate_screen_cache(void) { // Updates screen size. static void update_screen_size(void) { - int pxW = 0, pxH = 0; int oldCols = gCols; int oldRows = gRows; + NTL_CONSOLE_INFO info; - if (SysGetScreenSize(&pxW, &pxH) == 0 && pxW > 0 && pxH > 0) { - gCols = pxW / GLYPH_W; - gRows = pxH / GLYPH_H; + if (NtlConsoleQueryInfo(SYS_HANDLE_INVALID, &info) == NTLSTATUS_SUCCESS && + info.Columns > 0 && info.Rows > 0) { + gCols = (int)info.Columns; + gRows = (int)info.Rows; if (gCols < 20) gCols = 20; if (gRows < 5) gRows = 5; @@ -110,11 +113,8 @@ static void put_cell(int col, int row, char c, uint32_t fg, uint32_t bg) { cell->bg = bg; } - SYS_PUT_SCREEN_CHAR_REQUEST request = { - sizeof(SYS_PUT_SCREEN_CHAR_REQUEST), c, { 0, 0, 0 }, - col * GLYPH_W, row * GLYPH_H, fg, bg - }; - SysPutScreenChar(&request); + (void)NtlConsolePutCharacter(SYS_HANDLE_INVALID, c, + col * GLYPH_W, row * GLYPH_H, fg, bg); } // Clears row. @@ -506,8 +506,8 @@ static void editor_loop(void) { render(); INPUT_KEY key = INPUT_KEY_NONE; - if (SysReadKey(&key) != NTLSTATUS_SUCCESS) continue; - uint32_t mods = SysGetKeyModifiers(); + if (NtlInputReadKey(&key) != NTLSTATUS_SUCCESS) continue; + uint32_t mods = NtlInputGetKeyModifiers(); bool ctrl = (mods & INPUT_MOD_CTRL) != 0; if (ctrl && key == EDIT_KEY_Q) { @@ -562,18 +562,17 @@ static void editor_loop(void) { // Prints the requested operation. static void print(const char* s) { - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) }; - SysWriteConsole(&req); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, strlen(s), 0); } // Runs the requested operation. int main(void) { parse_filename_from_cmdline(); load_file(); - SysClearScreen(COL_BG); + NtlConsoleClear(SYS_HANDLE_INVALID, COL_BG); invalidate_screen_cache(); editor_loop(); - SysClearScreen(0x012456); + NtlConsoleClear(SYS_HANDLE_INVALID, 0x012456); invalidate_screen_cache(); print("edit: goodbye\n"); SysExitProcess(0); diff --git a/code/user/apps/httpget/httpget.c b/code/user/apps/httpget/httpget.c index 220894c..15d537c 100644 --- a/code/user/apps/httpget/httpget.c +++ b/code/user/apps/httpget/httpget.c @@ -5,16 +5,16 @@ #include #include #include +#include // Prints the requested operation. static void print(const char* s) { - if (s) { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) }; SysWriteConsole(&req); } + if (s) (void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, strlen(s), NULL); } // Prints char. static void print_char(char c) { - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromParts(&c, 1) }; - SysWriteConsole(&req); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, &c, 1, NULL); } // Prints u64. diff --git a/code/user/apps/memtop/memtop.c b/code/user/apps/memtop/memtop.c index 447a72e..d4dbdd2 100644 --- a/code/user/apps/memtop/memtop.c +++ b/code/user/apps/memtop/memtop.c @@ -3,6 +3,7 @@ #include #include #include +#include static int u64_to_string(uint64_t n, char* buf) { int i = 0; @@ -25,12 +26,12 @@ static int u64_to_string(uint64_t n, char* buf) { } int main(void) { - SYS_CONSOLE_INFO console; + NTL_CONSOLE_INFO console; SYS_MEMORY_STATUS mem; char metricsBuf[128]; while (true) { - if (SysGetConsoleInfo(&console) == NTLSTATUS_SUCCESS && + if (NtlConsoleQueryInfo(SYS_HANDLE_INVALID, &console) == NTLSTATUS_SUCCESS && SysGetMemoryStatus(&mem) == NTLSTATUS_SUCCESS) { // If the system isn't reporting columns correctly, default to a safe standard width @@ -60,11 +61,8 @@ int main(void) { // Loop through the string and use the console's dedicated text filling API // This utilizes the console's native font configuration instead of drawing raw screen bits for (size_t i = 0; i < textLen; i++) { - SYS_FILL_CONSOLE_CHARACTER_REQUEST request = { - sizeof(SYS_FILL_CONSOLE_CHARACTER_REQUEST), metricsBuf[i], { 0, 0, 0 }, - targetX + (int)i, targetY, 1, &written - }; - SysFillConsoleCharacter(&request); + (void)NtlConsoleFillCharacter(SYS_HANDLE_INVALID, metricsBuf[i], + targetX + (int)i, targetY, 1, &written); } } } diff --git a/code/user/apps/ping/ping.c b/code/user/apps/ping/ping.c index b77f982..5b47dd1 100644 --- a/code/user/apps/ping/ping.c +++ b/code/user/apps/ping/ping.c @@ -5,6 +5,7 @@ #include #include #include +#include #define PING_DEFAULT_TARGET "10.0.2.2" #define PING_DEFAULT_COUNT 4u @@ -15,13 +16,12 @@ // Prints the requested operation. static void print(const char* s) { - if (s) { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) }; SysWriteConsole(&req); } + if (s) (void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, strlen(s), NULL); } // Prints char. static void print_char(char c) { - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromParts(&c, 1) }; - SysWriteConsole(&req); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, &c, 1, NULL); } // Prints u64. diff --git a/code/user/apps/resolve/resolve.c b/code/user/apps/resolve/resolve.c index b6e6c42..25a4cc2 100644 --- a/code/user/apps/resolve/resolve.c +++ b/code/user/apps/resolve/resolve.c @@ -4,16 +4,16 @@ #include #include #include +#include // Prints the requested operation. static void print(const char* s) { - if (s) { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) }; SysWriteConsole(&req); } + if (s) (void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, strlen(s), NULL); } // Prints char. static void print_char(char c) { - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromParts(&c, 1) }; - SysWriteConsole(&req); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, &c, 1, NULL); } // Prints u64. diff --git a/code/user/apps/shell_native/shell_native.c b/code/user/apps/shell_native/shell_native.c index 0229f93..0f035e0 100644 --- a/code/user/apps/shell_native/shell_native.c +++ b/code/user/apps/shell_native/shell_native.c @@ -5,8 +5,11 @@ #include #include #include +#include +#include #include #include +#include #include #define MAX_CMD 256 @@ -22,7 +25,7 @@ #define SHELL_CURSOR_BACKGROUND 0x00E0E0E0u // Output helpers. -static void print(const char* s) { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) }; SysWriteConsole(&req); } +static void print(const char* s) { (void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, strlen(s), 0); } // Opens file. static uint32_t open_file(const char* path, uint32_t* handleOut) { @@ -228,7 +231,7 @@ static const char* const gShellBuiltinCommands[] = { "dir", "display", "exit", - "fbbench", + "surfbench", "gpu", "help", "kill", @@ -313,13 +316,10 @@ static void shellHistoryAdd(const char* cmd) { static void shellEditorHideVisibleCursor(SHELL_VISIBLE_CURSOR* cursor) { if (!cursor || !cursor->Visible) return; - SYS_PUT_SCREEN_CHAR_REQUEST request = { - sizeof(SYS_PUT_SCREEN_CHAR_REQUEST), cursor->Character, { 0, 0, 0 }, - cursor->Column * SHELL_GLYPH_WIDTH, - cursor->Row * SHELL_GLYPH_HEIGHT, - SHELL_TEXT_FOREGROUND, SHELL_TEXT_BACKGROUND - }; - (void)SysPutScreenChar(&request); + (void)NtlConsolePutCharacter(SYS_HANDLE_INVALID, cursor->Character, + cursor->Column * SHELL_GLYPH_WIDTH, + cursor->Row * SHELL_GLYPH_HEIGHT, + SHELL_TEXT_FOREGROUND, SHELL_TEXT_BACKGROUND); cursor->Visible = false; } @@ -328,19 +328,16 @@ static void shellEditorShowVisibleCursor(SHELL_VISIBLE_CURSOR* cursor, const SHE if (!cursor || !editor) return; shellEditorHideVisibleCursor(cursor); - SYS_CONSOLE_INFO info; - if (SysGetConsoleInfo(&info) != NTLSTATUS_SUCCESS) return; + NTL_CONSOLE_INFO info; + if (NtlConsoleQueryInfo(SYS_HANDLE_INVALID, &info) != NTLSTATUS_SUCCESS) return; char character = editor->Cursor < editor->Length ? editor->Buffer[editor->Cursor] : ' '; - SYS_PUT_SCREEN_CHAR_REQUEST request = { - sizeof(SYS_PUT_SCREEN_CHAR_REQUEST), character, { 0, 0, 0 }, - (int)info.CursorX * SHELL_GLYPH_WIDTH, - (int)info.CursorY * SHELL_GLYPH_HEIGHT, - SHELL_CURSOR_FOREGROUND, SHELL_CURSOR_BACKGROUND - }; - if (SysPutScreenChar(&request) != NTLSTATUS_SUCCESS) + if (NtlConsolePutCharacter(SYS_HANDLE_INVALID, character, + (int)info.CursorX * SHELL_GLYPH_WIDTH, + (int)info.CursorY * SHELL_GLYPH_HEIGHT, + SHELL_CURSOR_FOREGROUND, SHELL_CURSOR_BACKGROUND) != NTLSTATUS_SUCCESS) return; cursor->Column = (int)info.CursorX; @@ -439,16 +436,23 @@ static bool shellTokenHasPathSeparator(const char* token, size_t length) { return false; } -// Returns true when name starts with prefix. +// Returns an ASCII lower-case value for case-insensitive shell name handling. +static char shellAsciiLower(char c) { + if (c >= 'A' && c <= 'Z') + return (char)(c - 'A' + 'a'); + return c; +} + +// Returns true when name starts with prefix without treating ASCII case as significant. static bool shellNameStartsWith(const char* name, const char* prefix, size_t prefixLength) { if (!name || !prefix) return false; for (size_t i = 0; i < prefixLength; i++) { - if (name[i] == '\0' || name[i] != prefix[i]) return false; + if (name[i] == '\0' || shellAsciiLower(name[i]) != shellAsciiLower(prefix[i])) return false; } return true; } -// Extends an in-progress common prefix with one completion candidate. +// Extends an in-progress common prefix case-insensitively while preserving the first candidate's spelling. static void shellUpdateCommonPrefix(char* common, size_t commonSize, const char* name, uint32_t matchCount) { if (!common || commonSize == 0 || !name) return; if (matchCount == 0) { @@ -458,7 +462,7 @@ static void shellUpdateCommonPrefix(char* common, size_t commonSize, const char* } size_t i = 0; - while (common[i] && name[i] && common[i] == name[i]) i++; + while (common[i] && name[i] && shellAsciiLower(common[i]) == shellAsciiLower(name[i])) i++; common[i] = '\0'; } @@ -532,6 +536,42 @@ static bool shellSplitCompletionPath(const char* token, size_t tokenLength, return true; } +// Collects immediate RSSRV namespace children when the completion directory is a namespace directory. +static void shellCollectResourceCompletions(const char* token, size_t tokenLength, + SHELL_COMPLETION_STATE* state, + bool printMatches) { + char dir[MAX_CMD]; + const char* prefix = token; + size_t prefixLength = tokenLength; + if (!shellSplitCompletionPath(token, tokenLength, dir, sizeof(dir), &prefix, &prefixLength)) return; + if (dir[0] != '/') return; + + SYS_HANDLE session = SYS_HANDLE_INVALID; + if (!KSUCCESS(NtlResourceConnect(&session))) return; + uint32_t kind = NTL_RESOURCE_KIND_INVALID; + KSTATUS status = NtlResourceQuery(session, SysSvFromCstr(dir), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) { + (void)NtlResourceClose(session); + return; + } + + NTL_RESOURCE_ENUM_CURSOR cursor; + memset(&cursor, 0, sizeof(cursor)); + for (;;) { + NTL_RESOURCE_ENUM_ENTRY entry; + status = NtlResourceEnumerate(session, SysSvFromCstr(dir), &cursor, &entry); + if (status == NTLSTATUS_NO_MORE_FILES) break; + if (!KSUCCESS(status)) break; + if (!shellNameStartsWith(entry.Name, prefix, prefixLength)) continue; + + bool directory = entry.Kind == NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY || + entry.Kind == NTL_RESOURCE_KIND_SYMBOLIC_LINK; + if (printMatches) shellPrintCompletionCandidate(entry.Name, directory); + else shellRecordCompletion(state, entry.Name, directory, true); + } + (void)NtlResourceClose(session); +} + // Collects filesystem path matches using the existing directory enumeration ABI. static void shellCollectPathCompletions(const char* token, size_t tokenLength, SHELL_COMPLETION_STATE* state, @@ -593,15 +633,18 @@ static void shellCompleteLine(SHELL_EDITOR* editor) { if (commandPosition && !explicitPath) { shellCollectBuiltinCompletions(token, tokenLength, &state, false); } + shellCollectResourceCompletions(token, tokenLength, &state, false); shellCollectPathCompletions(token, tokenLength, &state, false); if (state.MatchCount == 0) return; size_t commonLength = strlen(state.Common); bool expanded = commonLength > namePrefixLength; - if (expanded) { - (void)shellEditorReplaceRange(editor, editor->Cursor, (int)tokenEnd, - state.Common + namePrefixLength); + bool canonicalized = commonLength == namePrefixLength && + memcmp(state.Common, namePrefix, namePrefixLength) != 0; + if (expanded || canonicalized) { + size_t nameStart = tokenStart + (size_t)(namePrefix - token); + (void)shellEditorReplaceRange(editor, (int)nameStart, (int)tokenEnd, state.Common); } if (state.MatchCount == 1) { @@ -624,6 +667,7 @@ static void shellCompleteLine(SHELL_EDITOR* editor) { if (commandPosition && !explicitPath) { shellCollectBuiltinCompletions(token, tokenLength, NULL, true); } + shellCollectResourceCompletions(token, tokenLength, NULL, true); shellCollectPathCompletions(token, tokenLength, NULL, true); shellRedrawPromptAndLine(editor); } @@ -647,13 +691,7 @@ static bool readLine(char* buf, int len) { while (true) { shellEditorShowVisibleCursor(&visibleCursor, &editor); INPUT_KEY_RECORD input; - SYS_INPUT_EVENT_REQUEST inputReq; - inputReq.Size = sizeof(inputReq); - inputReq.QueueHandle = 0; - inputReq.Flags = SYS_INPUT_EVENT_FLAG_NONE; - inputReq.TimeoutMilliseconds = SYS_WAIT_TIMEOUT_INFINITE; - inputReq.RecordOut = &input; - KSTATUS inputStatus = SysReadInputEvent(&inputReq); + KSTATUS inputStatus = NtlInputReadEvent(SYS_HANDLE_INVALID, SYS_WAIT_TIMEOUT_INFINITE, &input); shellEditorHideVisibleCursor(&visibleCursor); if (inputStatus != NTLSTATUS_SUCCESS) { SysYield(); continue; } @@ -755,7 +793,7 @@ static void cmd_help() { " mem - Show memory usage\n" " time - Show system wall-clock time\n" " display - Show display provider and counters\n" - " fbbench - Benchmark Doom-size and full-screen presents\n" + " surfbench - Benchmark Doom-size and full-screen surface commits\n" " stats - Show scheduler/CPU/MM telemetry\n" " ps - List processes\n" " threads - Show one process's thread statuses\n" @@ -1276,13 +1314,13 @@ static void cmd_stats(void) { print(" live="); printUInt64(surface.LiveSurfaces); print("\n"); print(" live memory: pages="); printUInt64(surface.LivePages); print(" bytes="); printUInt64(surface.LiveBytes); print("\n"); - print(" maps/presents: maps="); printUInt64(surface.MapCalls); - print(" presents="); printUInt64(surface.PresentCalls); - print(" input-rects="); printUInt64(surface.PresentRectInputs); print("\n"); - print(" present work: clipped="); printUInt64(surface.PresentClippedRects); - print(" empty="); printUInt64(surface.PresentEmptyClips); - print(" rows="); printUInt64(surface.PresentRowsCopied); - print(" bytes="); printUInt64(surface.PresentBytesCopied); print("\n"); + print(" maps/commits: maps="); printUInt64(surface.MapCalls); + print(" commits="); printUInt64(surface.CommitCalls); + print(" input-rects="); printUInt64(surface.CommitRectInputs); print("\n"); + print(" commit work: clipped="); printUInt64(surface.CommitClippedRects); + print(" empty="); printUInt64(surface.CommitEmptyClips); print("\n"); + print(" physical present: calls="); printUInt64(surface.PhysicalPresentCalls); + print(" bytes="); printUInt64(surface.PhysicalPresentBytesCopied); print("\n"); print("Page tables:\n"); print(" leaf mappings: "); printUInt64(pte.CurrentLeafMappings); print("\n"); @@ -1411,9 +1449,7 @@ typedef struct SHELL_FB_BENCH_RESULT { uint32_t Status; uint64_t ElapsedTicks; uint64_t Frequency; - uint64_t ExpectedBytes; - uint64_t ScanoutBytes; - uint64_t DirectPresents; + uint64_t CommitBytes; } SHELL_FB_BENCH_RESULT; // Computes a decimal fixed-point ratio with one digit after the decimal point. @@ -1441,7 +1477,7 @@ static void shellFillBenchmarkSurface(uint32_t* pixels, } } -// Runs one synchronous surface-present benchmark case. +// Runs one synchronous surface-commit benchmark case. static void shellRunFramebufferBenchmark(uint32_t width, uint32_t height, uint32_t frames, @@ -1471,7 +1507,8 @@ static void shellRunFramebufferBenchmark(uint32_t width, void* base = 0; uint32_t pitchBytes = 0; - status = SysMapSurface(surface, &base, &pitchBytes); + status = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, + &base, &pitchBytes); if (status != NTLSTATUS_SUCCESS || !base || pitchBytes < width * sizeof(uint32_t)) { result->Status = status ? status : NTLSTATUS_FAILURE; SysCloseHandle(surface); @@ -1481,44 +1518,28 @@ static void shellRunFramebufferBenchmark(uint32_t width, shellFillBenchmarkSurface((uint32_t*)base, pitchBytes / sizeof(uint32_t), width, height); DISPLAY_RECT rect = { 0, 0, (int32_t)width, (int32_t)height }; - for (uint32_t i = 0; i < 2; i++) { - status = SysPresentSurface(surface, &rect, 1); - if (status != NTLSTATUS_SUCCESS) - break; - } - - SYS_DISPLAY_INFO before; - SYS_DISPLAY_INFO after; - memset(&before, 0, sizeof(before)); - memset(&after, 0, sizeof(after)); - if (status == NTLSTATUS_SUCCESS) - status = SysQueryDisplayInfo(&before); + for (uint32_t i = 0; i < 2 && status == NTLSTATUS_SUCCESS; ++i) + status = SysCommitSurface(surface, &rect, 1, 0); uint64_t begin = 0; uint64_t end = 0; uint32_t completed = 0; if (status == NTLSTATUS_SUCCESS) { begin = SysGetTick(); - for (; completed < frames; completed++) { - status = SysPresentSurface(surface, &rect, 1); + for (; completed < frames; ++completed) { + status = SysCommitSurface(surface, &rect, 1, 0); if (status != NTLSTATUS_SUCCESS) break; } end = SysGetTick(); - uint32_t queryStatus = SysQueryDisplayInfo(&after); - if (status == NTLSTATUS_SUCCESS && queryStatus != NTLSTATUS_SUCCESS) - status = queryStatus; } result->Frames = completed; result->Status = status; result->ElapsedTicks = end - begin; - result->ExpectedBytes = (uint64_t)width * (uint64_t)height * sizeof(uint32_t) * completed; - if (after.DeviceStats.ScanoutBytes >= before.DeviceStats.ScanoutBytes) - result->ScanoutBytes = after.DeviceStats.ScanoutBytes - before.DeviceStats.ScanoutBytes; - if (after.DeviceStats.DirectPresentCount >= before.DeviceStats.DirectPresentCount) - result->DirectPresents = after.DeviceStats.DirectPresentCount - before.DeviceStats.DirectPresentCount; + result->CommitBytes = (uint64_t)width * (uint64_t)height * sizeof(uint32_t) * completed; + (void)SysUnmapSurface(base); SysCloseHandle(surface); } @@ -1535,32 +1556,30 @@ static void shellPrintFramebufferBenchmark(const char* name, uint64_t elapsedUs = SysTicksToMicroseconds(result->ElapsedTicks, result->Frequency); uint64_t fpsTenths = shellRatioTenths((uint64_t)result->Frames * result->Frequency, result->ElapsedTicks); - uint64_t bytes = result->ScanoutBytes ? result->ScanoutBytes : result->ExpectedBytes; - uint64_t mibTenths = shellRatioTenths(bytes * result->Frequency, + uint64_t mibTenths = shellRatioTenths(result->CommitBytes * result->Frequency, result->ElapsedTicks * 1024u * 1024u); print("frames="); printUInt(result->Frames); print(" time="); printUInt64(elapsedUs / 1000u); print(" ms"); print(" fps="); printUInt64(fpsTenths / 10u); print("."); printUInt64(fpsTenths % 10u); - print(" scanout="); printUInt64(mibTenths / 10u); print("."); printUInt64(mibTenths % 10u); print(" MiB/s"); - print(" direct="); printUInt64(result->DirectPresents); - print(" bytes="); printUInt64(bytes); + print(" payload="); printUInt64(mibTenths / 10u); print("."); printUInt64(mibTenths % 10u); print(" MiB/s"); + print(" bytes="); printUInt64(result->CommitBytes); print("\n"); } -// Benchmarks the synchronous surface-to-scanout path without console traffic. -static void cmd_fbbench(void) { +// Benchmarks surface commit bookkeeping without implying physical presentation. +static void cmd_surfbench(void) { SYS_DISPLAY_INFO info; uint32_t status = SysQueryDisplayInfo(&info); if (status != NTLSTATUS_SUCCESS) { - print("fbbench: display query failed status=0x"); printHex32(status); print("\n"); + print("surfbench: display query failed status=0x"); printHex32(status); print("\n"); return; } uint64_t frequency = 0; status = SysGetTickFrequency(&frequency); if (status != NTLSTATUS_SUCCESS || !frequency) { - print("fbbench: timer query failed status=0x"); printHex32(status); print("\n"); + print("surfbench: timer query failed status=0x"); printHex32(status); print("\n"); return; } @@ -1579,13 +1598,13 @@ static void cmd_fbbench(void) { if (fullFrames < 8u) fullFrames = 8u; if (fullFrames > 64u) fullFrames = 64u; - print("fbbench: running; the framebuffer will be overwritten temporarily\n"); + print("surfbench: commit notifications only; scanout is DWM-owned\n"); SHELL_FB_BENCH_RESULT doom; SHELL_FB_BENCH_RESULT full; shellRunFramebufferBenchmark(doomWidth, doomHeight, doomFrames, frequency, &doom); shellRunFramebufferBenchmark(fullWidth, fullHeight, fullFrames, frequency, &full); - SysClearScreen(0x00012456u); + NtlConsoleClear(SYS_HANDLE_INVALID, 0x00012456u); print("Framebuffer benchmark:\n"); print(" provider: "); print(info.PrimaryName[0] ? info.PrimaryName : "?"); print(" mode="); printUInt(info.Mode.Width); print("x"); printUInt(info.Mode.Height); @@ -1669,6 +1688,7 @@ static const char* threadWaitName(uint32_t waitType) { case SYS_THREAD_WAIT_USB_PIPE: return "usbpipe"; case SYS_THREAD_WAIT_FILE_MAPPING: return "mapping"; case SYS_THREAD_WAIT_SURFACE: return "surface"; + case SYS_THREAD_WAIT_PIPE: return "pipe"; default: return "unknown"; } } @@ -1770,7 +1790,7 @@ static void cmd_threads(const char* args) { print(" (Count: "); printUInt((uint32_t)count); print("):\n"); - print(" TID State Base Eff CPU LastCPU QRem QTick Burst Wait WStat Flags\n"); + print(" TID State Base Eff CPU LastCPU QRem QTick Burst Wait WStat Flags Name\n"); print(" Flags: *=current I=idle K=kernel Q=queued A=alertable k=kernel-apc u=user-apc.\n"); size_t displayCount = count < MAX_THREADS ? count : MAX_THREADS; @@ -1804,6 +1824,8 @@ static void cmd_threads(const char* args) { printUIntWidth(t->WaitStatus, 5); print(" "); printThreadFlags(t->Flags); + print(" "); + print(t->Name[0] ? t->Name : "-"); print("\n"); } @@ -1861,13 +1883,6 @@ static void cmd_write(const char* args) { #define SHELL_DIR_INITIAL_CAPACITY 64u -// Returns an ASCII lower-case value for case-insensitive directory sorting. -static char shellAsciiLower(char c) { - if (c >= 'A' && c <= 'Z') - return (char)(c - 'A' + 'a'); - return c; -} - // Compares two names using case-insensitive order with a stable case tie-breaker. static int shellCompareNames(const char* a, const char* b) { size_t i = 0; @@ -1966,10 +1981,55 @@ static void shellPrintDirEntry(const SYS_DIR_ENTRY* ke) { print("\n"); } +// Prints one RSSRV namespace kind label. +static void shellPrintResourceKind(uint32_t kind) { + if (kind == NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) print(" "); + else if (kind == NTL_RESOURCE_KIND_SYMBOLIC_LINK) print(" "); + else if (kind == NTL_RESOURCE_KIND_VOLUME) print(" "); + else if (kind == NTL_RESOURCE_KIND_DIRECTORY) print(" "); + else if (kind == NTL_RESOURCE_KIND_DEVICE) print(" "); + else print(" "); +} + +// Handles dir for an exact RSSRV namespace directory and returns whether it owned the request. +static bool shellTryResourceDir(const char* target) { + if (!target || target[0] != '/') return false; + SYS_HANDLE session = SYS_HANDLE_INVALID; + if (!KSUCCESS(NtlResourceConnect(&session))) return false; + + uint32_t kind = NTL_RESOURCE_KIND_INVALID; + KSTATUS status = NtlResourceQuery(session, SysSvFromCstr(target), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) { + (void)NtlResourceClose(session); + return false; + } + + NTL_RESOURCE_ENUM_CURSOR cursor; + memset(&cursor, 0, sizeof(cursor)); + for (;;) { + NTL_RESOURCE_ENUM_ENTRY entry; + status = NtlResourceEnumerate(session, SysSvFromCstr(target), &cursor, &entry); + if (status == NTLSTATUS_NO_MORE_FILES) break; + if (!KSUCCESS(status)) { + print("dir: namespace enumeration failed, status=0x"); + printHex32(status); + print("\n"); + break; + } + shellPrintResourceKind(entry.Kind); + print(entry.Name); + if (entry.Kind == NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) print("/"); + print("\n"); + } + (void)NtlResourceClose(session); + return true; +} + // Implements the cmd dir operation. static void cmd_dir(const char* path) { uint32_t hDir; const char* target = (path && *path != '\0') ? path : "."; + if (shellTryResourceDir(target)) return; uint32_t st = open_dir(target, &hDir); if (st != 0) { @@ -2050,7 +2110,7 @@ static bool extractProgramPath(const char* cmdline, char* out, size_t outSize) { // Returns whether one input key is currently held. static bool shellIsKeyDown(INPUT_KEY key) { bool down = false; - return SysIsKeyDown(key, &down) == NTLSTATUS_SUCCESS && down; + return NtlInputIsKeyDown(key, &down) == NTLSTATUS_SUCCESS && down; } // Waits for a foreground process while polling Ctrl-C without consuming child input. @@ -2066,7 +2126,7 @@ static uint32_t waitForegroundProcess(uint32_t hProc) { if (status != NTLSTATUS_TIMEOUT) return status; - uint32_t modifiers = SysGetKeyModifiers(); + uint32_t modifiers = NtlInputGetKeyModifiers(); if (!sentBreak && (modifiers & INPUT_MOD_CTRL) && shellIsKeyDown(INPUT_KEY_C)) { print("^C\n"); @@ -2089,7 +2149,7 @@ static int runCommand(const char* cmd) { if (!cmd || *cmd == '\0') return 0; if (strcmp(cmd, "exit") == 0) return 1; if (strcmp(cmd, "help") == 0) { cmd_help(); return 0; } - if (strcmp(cmd, "clear") == 0) { SysClearScreen(0x012456); return 0; } + if (strcmp(cmd, "clear") == 0) { (void)NtlConsoleClear(SYS_HANDLE_INVALID, 0x012456); return 0; } if (strncmp(cmd, "crash", 5) == 0) { // Check the sub-option trailing "crash " (offset 6). if (strcmp(cmd + 6, "void") == 0) { @@ -2131,7 +2191,7 @@ static int runCommand(const char* cmd) { if (strcmp(cmd, "mem") == 0) { cmd_mem(); return 0; } if (strcmp(cmd, "time") == 0 || strcmp(cmd, "date") == 0) { cmd_time(); return 0; } if (strcmp(cmd, "display") == 0 || strcmp(cmd, "gpu") == 0) { cmd_display(); return 0; } - if (strcmp(cmd, "fbbench") == 0) { cmd_fbbench(); return 0; } + if (strcmp(cmd, "surfbench") == 0) { cmd_surfbench(); return 0; } if (strcmp(cmd, "volume") == 0) { cmd_volume(""); return 0; } if (strncmp(cmd, "volume ", 7) == 0) { cmd_volume(cmd + 7); return 0; } if (strcmp(cmd, "stats") == 0 || strcmp(cmd, "telemetry") == 0) { cmd_stats(); return 0; } @@ -2199,7 +2259,7 @@ static int runCommand(const char* cmd) { print("\n"); SysCloseHandle(hProc); INPUT_KEY drainedKey; - while (SysPollKeyboard(&drainedKey) == NTLSTATUS_SUCCESS) (void)SysReadKey(&drainedKey); + while (NtlInputPollKeyboard(&drainedKey) == NTLSTATUS_SUCCESS) (void)NtlInputReadKey(&drainedKey); } } else { print("shell: launch failed\n"); diff --git a/code/user/apps/shell_posix/shell_posix.c b/code/user/apps/shell_posix/shell_posix.c index 3429c20..a9da3fb 100644 --- a/code/user/apps/shell_posix/shell_posix.c +++ b/code/user/apps/shell_posix/shell_posix.c @@ -4,51 +4,21 @@ #include #include #include -#include +#include +#include +#include +#include +#include +#include +#include #define MAX_CMD 256 -#define NAME_MAX 259 - -typedef struct dirent { char d_name[NAME_MAX + 1]; unsigned char d_type; } dirent; -typedef struct DIR { uint32_t KHandle; dirent Current; bool Valid; } DIR; - -extern int read(int fd, void* buf, size_t count); -extern int write(int fd, const void* buf, size_t count); -extern int open(const char* pathname, int flags, ...); -extern int close(int fd); -extern int chdir(const char* path); -extern DIR* opendir(const char* path); -extern dirent* readdir(DIR* d); -extern int closedir(DIR* d); -extern int unlink(const char* pathname); -extern int posix_spawn(int* pid, const char* path, const void* act, const void* attr, char* const argv[], char* const envp[]); -extern int waitpid(int pid, int* status, int options); -extern void* malloc(size_t size); -extern void* realloc(void* ptr, size_t size); -extern void free(void* ptr); -extern void exit(int status); -extern void clearscreen(void); -extern void clearkeyboard(void); static int hIn = 0; static int hOut = 1; // Prints the requested operation. static void print(const char* s) { write(hOut, s, strlen(s)); } -// Prints u Int. -static void printUInt(uint32_t n) { - char buf[12]; int i = 0; - if (n == 0) { print("0"); return; } - while (n > 0) { buf[i++] = (char)('0' + n % 10); n /= 10; } - while (i > 0) { char c[2] = { buf[--i], '\0' }; print(c); } -} - -// Prints int. -static void printInt(int32_t n) { - if (n < 0) { print("-"); n = -n; } - printUInt((uint32_t)n); -} - // Returns line. static void readLine(char* buf, int len) { int pos = 0; @@ -202,7 +172,7 @@ static void cmd_dir(const char* path) { closedir(d); shellSortDirEntries(entries, count); for (size_t i = 0; i < count; i++) { - print(entries[i].Type == 1 ? " " : " "); + print(entries[i].Type == DT_DIR ? " " : " "); print(entries[i].Name); print("\n"); } @@ -213,7 +183,8 @@ static void cmd_dir(const char* path) { static void cmd_type(const char* filename) { int fd = open(filename, 0); // O_RDONLY if (fd < 0) { print("type: cannot open file\n"); return; } - char buffer[512]; int n; + char buffer[512]; + ssize_t n; while ((n = read(fd, buffer, 511)) > 0) { buffer[n] = '\0'; print(buffer); } close(fd); print("\n"); } @@ -224,7 +195,7 @@ static int runCommand(const char* cmd) { if (strcmp(cmd, "exit") == 0) return 1; if (strcmp(cmd, "help") == 0) { cmd_help(); return 0; } if (strcmp(cmd, "clear") == 0) { clearscreen(); return 0; } - if (strcmp(cmd, "crash") == 0) { *(int*)0 = 42; return 0; } + if (strcmp(cmd, "crash") == 0) { *(volatile int*)0 = 42; return 0; } if (strcmp(cmd, "dir") == 0) { cmd_dir("."); return 0; } if (strncmp(cmd, "dir ", 4) == 0) { cmd_dir(cmd + 4); return 0; } if (strncmp(cmd, "cd ", 3) == 0) { if (chdir(cmd + 3) != 0) print("cd: failed\n"); return 0; } @@ -251,7 +222,7 @@ static int runCommand(const char* cmd) { // Runs the requested operation. int main(void) { - const char* cmdline = SysGetPeb()->CommandLine.Data; + const char* cmdline = posix_get_command_line(); char lineBuf[MAX_CMD]; print("shell: command line: "); diff --git a/code/user/apps/tcpserve/tcpserve.c b/code/user/apps/tcpserve/tcpserve.c index 5d67af7..ef9b311 100644 --- a/code/user/apps/tcpserve/tcpserve.c +++ b/code/user/apps/tcpserve/tcpserve.c @@ -5,16 +5,16 @@ #include #include #include +#include // Prints the requested operation. static void print(const char* s) { - if (s) { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) }; SysWriteConsole(&req); } + if (s) (void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, strlen(s), NULL); } // Prints char. static void print_char(char c) { - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromParts(&c, 1) }; - SysWriteConsole(&req); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, &c, 1, NULL); } // Prints u64. diff --git a/code/user/apps/udp/udp.c b/code/user/apps/udp/udp.c index c750eb3..c6f8b58 100644 --- a/code/user/apps/udp/udp.c +++ b/code/user/apps/udp/udp.c @@ -5,16 +5,16 @@ #include #include #include +#include // Prints the requested operation. static void print(const char* s) { - if (s) { SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) }; SysWriteConsole(&req); } + if (s) (void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, strlen(s), NULL); } // Prints char. static void print_char(char c) { - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromParts(&c, 1) }; - SysWriteConsole(&req); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, &c, 1, NULL); } // Prints u64. diff --git a/code/user/apps/video/video.c b/code/user/apps/video/video.c index 3c9d338..8d14389 100644 --- a/code/user/apps/video/video.c +++ b/code/user/apps/video/video.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "bink.h" @@ -18,14 +19,12 @@ static void print_span(const char* s, size_t len) { if (!s || !len) return; - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromParts(s, len) }; - SysWriteConsole(&req); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, len, NULL); } static void print(const char* s) { if (!s) return; - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(s) }; - SysWriteConsole(&req); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, strlen(s), NULL); } static void print_u64(uint64_t v) { @@ -297,7 +296,8 @@ int main(void) { void* surfaceBase = 0; uint32_t pitchBytes = 0; - status = SysMapSurface(surface, &surfaceBase, &pitchBytes); + status = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, + &surfaceBase, &pitchBytes); if (status != NTLSTATUS_SUCCESS || !surfaceBase || pitchBytes < width * sizeof(uint32_t)) { SysCloseHandle(surface); BinkClose(b); @@ -309,7 +309,7 @@ int main(void) { print("video: decode loop entered\n"); DISPLAY_RECT fullFrame = { 0, 0, (int32_t)width, (int32_t)height }; - int firstFramePresented = 0; + int firstFrameCommitted = 0; while (1) { if (BinkWait(b)) { @@ -329,16 +329,17 @@ int main(void) { BinkCopyToBuffer(b, surfaceBase, pitchBytes, height, 0, 0, BINKSURFACE32); - status = SysPresentSurface(surface, &fullFrame, 1); + status = SysCommitSurface(surface, &fullFrame, 1, 0); if (status != NTLSTATUS_SUCCESS) { + (void)SysUnmapSurface(surfaceBase); SysCloseHandle(surface); BinkClose(b); SysFreeVm(buf); - fail_status("SysPresentSurface", status); + fail_status("SysCommitSurface", status); } - if (!firstFramePresented) { - firstFramePresented = 1; - print("video: first frame presented sndBytes="); + if (!firstFrameCommitted) { + firstFrameCommitted = 1; + print("video: first frame committed sndBytes="); print_u64(b->SndSize); print(" soundon="); print_u64(b->soundon); @@ -359,6 +360,7 @@ int main(void) { print_u64(b->SndSize); print("\n"); print("video: closing surface\n"); + (void)SysUnmapSurface(surfaceBase); SysCloseHandle(surface); print("video: closing Bink\n"); BinkClose(b); diff --git a/code/user/personalities/posix/posix.h b/code/user/personalities/posix/posix.h deleted file mode 100644 index 1d74c3d..0000000 --- a/code/user/personalities/posix/posix.h +++ /dev/null @@ -1,299 +0,0 @@ -// POSIX compatibility support for posix. - -#pragma once -// posix.h - POSIX type and constant definitions for posix.dll -// Covers just what open/read/write/close/lseek/spawn/pthread/chdir need. - -#include -#include -#include - -// errno. -#ifdef errno -#undef errno -#endif -#ifdef EAGAIN -#undef EAGAIN -#endif -#ifdef ENOSYS -#undef ENOSYS -#endif -#ifdef EWOULDBLOCK -#undef EWOULDBLOCK -#endif -#ifdef EINPROGRESS -#undef EINPROGRESS -#endif -#ifdef ETIMEDOUT -#undef ETIMEDOUT -#endif -#ifdef ECONNRESET -#undef ECONNRESET -#endif -#ifdef ENOTSOCK -#undef ENOTSOCK -#endif -extern int errno; // defined in posix_fd.c; single global in v1 - -#define EPERM 1 -#define ENOENT 2 -#define ESRCH 3 -#define EINTR 4 -#define EIO 5 -#define EBADF 9 -#define ENOMEM 12 -#define EACCES 13 -#define EEXIST 17 -#define ENODEV 19 -#define ENOTDIR 20 -#define EINVAL 22 -#define ESPIPE 29 -#define EMFILE 24 -#define ENOSPC 28 -#define ERANGE 34 -#define EAGAIN 35 -#define EWOULDBLOCK EAGAIN -#define ENOSYS 38 -#define ENOTSOCK 88 -#define EINPROGRESS 115 -#define ETIMEDOUT 116 -#define ECONNRESET 104 - -// open() flags. -#define O_RDONLY 0x0000 -#define O_WRONLY 0x0001 -#define O_RDWR 0x0002 -#define O_CREAT 0x0040 -#define O_TRUNC 0x0200 -#define O_APPEND 0x0400 - -// lseek() whence. -#define SEEK_SET 0 -#define SEEK_CUR 1 -#define SEEK_END 2 - -// mmap() protection and mapping flags. -#define PROT_NONE 0x0 -#define PROT_READ 0x1 -#define PROT_WRITE 0x2 -#define PROT_EXEC 0x4 -#define MAP_SHARED 0x01 -#define MAP_PRIVATE 0x02 -#define MAP_FAILED ((void*)(intptr_t)-1) -#define MS_SYNC 0x04 -#define MS_ASYNC 0x01 -#define MS_INVALIDATE 0x02 - -// Dynamic loader flags. -#define RTLD_LAZY 0x0001 -#define RTLD_NOW 0x0002 -#define RTLD_LOCAL 0x0004 -#define RTLD_GLOBAL 0x0008 - -// Scalar types. -typedef int32_t pid_t; -typedef int32_t off_t; -typedef uint32_t mode_t; -typedef int64_t ssize_t; -#define S_IFMT 0170000 -#define S_IFSOCK 0140000 -#define S_IFCHR 0020000 -#define S_IFDIR 0040000 -#define S_IFREG 0100000 -#define S_IRUSR 0000400 -#define S_IWUSR 0000200 -#define S_IXUSR 0000100 -#define S_IRGRP 0000040 -#define S_IWGRP 0000020 -#define S_IXGRP 0000010 -#define S_IROTH 0000004 -#define S_IWOTH 0000002 -#define S_IXOTH 0000001 - -#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) -#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) -#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) -#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) - -struct stat { - uint64_t st_dev; - uint64_t st_ino; - mode_t st_mode; - uint32_t st_nlink; - uint32_t st_uid; - uint32_t st_gid; - uint64_t st_rdev; - off_t st_size; - int64_t st_atime; - int64_t st_mtime; - int64_t st_ctime; -}; - -__declspec(dllexport) int stat(const char* path, struct stat* out); -__declspec(dllexport) int fstat(int fd, struct stat* out); -__declspec(dllexport) char* realpath(const char* path, char* resolved_path); - -__declspec(dllexport) void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset); -__declspec(dllexport) int munmap(void* addr, size_t length); -__declspec(dllexport) int msync(void* addr, size_t length, int flags); - -__declspec(dllexport) void* dlopen(const char* filename, int flags); -__declspec(dllexport) void* dlsym(void* handle, const char* symbol); -__declspec(dllexport) int dlclose(void* handle); -__declspec(dllexport) char* dlerror(void); - -// Sockets. -typedef uint16_t sa_family_t; -typedef uint16_t in_port_t; -typedef uint32_t in_addr_t; -typedef uint32_t socklen_t; - -#define AF_INET 2 -#define AF_INET6 10 -#define AF_UNSPEC 0 -#define PF_INET AF_INET -#define PF_INET6 AF_INET6 -#define SOCK_STREAM 1 -#define SOCK_DGRAM 2 -#define SOCK_RAW 3 -#define IPPROTO_ICMP 1 -#define IPPROTO_TCP 6 -#define IPPROTO_UDP 17 -#define IPPROTO_ICMPV6 58 - -#define SHUT_RD 0 -#define SHUT_WR 1 -#define SHUT_RDWR 2 - -#define SOL_SOCKET 1 -#define SO_RCVTIMEO 1 -#define SO_SNDTIMEO 2 -#define SO_NONBLOCK 3 - -#define INADDR_ANY ((in_addr_t)0x00000000u) -#define INADDR_NONE ((in_addr_t)0xFFFFFFFFu) - -struct in_addr { - in_addr_t s_addr; -}; - -struct in6_addr { - uint8_t s6_addr[16]; -}; - -struct sockaddr { - sa_family_t sa_family; - char sa_data[14]; -}; - -struct sockaddr_in { - sa_family_t sin_family; - in_port_t sin_port; - struct in_addr sin_addr; - char sin_zero[8]; -}; - -struct sockaddr_in6 { - sa_family_t sin6_family; - in_port_t sin6_port; - uint32_t sin6_flowinfo; - struct in6_addr sin6_addr; - uint32_t sin6_scope_id; -}; - -struct hostent { - char* h_name; - char** h_aliases; - int h_addrtype; - int h_length; - char** h_addr_list; -}; -#define h_addr h_addr_list[0] - -struct addrinfo { - int ai_flags; - int ai_family; - int ai_socktype; - int ai_protocol; - socklen_t ai_addrlen; - struct sockaddr* ai_addr; - char* ai_canonname; - struct addrinfo* ai_next; -}; - -#define AI_PASSIVE 0x0001 - -struct pollfd { - int fd; - short events; - short revents; -}; - -#define POLLIN 0x0001 -#define POLLOUT 0x0002 -#define POLLERR 0x0004 -#define POLLHUP 0x0008 -#define POLLNVAL 0x0010 - -typedef unsigned long nfds_t; - -typedef struct timeval { - long tv_sec; - long tv_usec; -} timeval; - -__declspec(dllexport) uint16_t htons(uint16_t x); -__declspec(dllexport) uint16_t ntohs(uint16_t x); -__declspec(dllexport) uint32_t htonl(uint32_t x); -__declspec(dllexport) uint32_t ntohl(uint32_t x); -__declspec(dllexport) in_addr_t inet_addr(const char* text); -__declspec(dllexport) char* inet_ntoa(struct in_addr in); -__declspec(dllexport) int inet_pton(int af, const char* src, void* dst); -__declspec(dllexport) const char* inet_ntop(int af, const void* src, char* dst, socklen_t size); - -__declspec(dllexport) int socket(int domain, int type, int protocol); -__declspec(dllexport) int bind(int fd, const struct sockaddr* addr, socklen_t addrlen); -__declspec(dllexport) int listen(int fd, int backlog); -__declspec(dllexport) int accept(int fd, struct sockaddr* addr, socklen_t* addrlen); -__declspec(dllexport) int connect(int fd, const struct sockaddr* addr, socklen_t addrlen); -__declspec(dllexport) ssize_t send(int fd, const void* buf, size_t len, int flags); -__declspec(dllexport) ssize_t recv(int fd, void* buf, size_t len, int flags); -__declspec(dllexport) ssize_t sendto(int fd, const void* buf, size_t len, int flags, const struct sockaddr* dest, socklen_t destlen); -__declspec(dllexport) ssize_t recvfrom(int fd, void* buf, size_t len, int flags, struct sockaddr* src, socklen_t* srclen); -__declspec(dllexport) int shutdown(int fd, int how); -__declspec(dllexport) int setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen); -__declspec(dllexport) int getsockopt(int fd, int level, int optname, void* optval, socklen_t* optlen); -__declspec(dllexport) int poll(struct pollfd* fds, nfds_t nfds, int timeout); -__declspec(dllexport) struct hostent* gethostbyname(const char* name); -__declspec(dllexport) int getaddrinfo(const char* node, const char* service, const struct addrinfo* hints, struct addrinfo** res); -__declspec(dllexport) void freeaddrinfo(struct addrinfo* res); - -// pthread types. -typedef struct { uint32_t Handle; uint32_t Tid; } pthread_t; -typedef struct { uint32_t Handle; } pthread_mutex_t; -typedef void* pthread_attr_t; // unused in v1 -typedef void* pthread_mutexattr_t; - -// posix_spawn types (file_actions and attrp both ignored in v1). -typedef void* posix_spawn_file_actions_t; -typedef void* posix_spawnattr_t; - -__declspec(dllexport) const char* posix_get_command_line(void); - -// dirent (used by posix_dir.c). -#define NAME_MAX 259 - -typedef struct dirent { - char d_name[NAME_MAX + 1]; - unsigned char d_type; // DT_REG or DT_DIR -} dirent; - -#define DT_UNKNOWN 0 -#define DT_REG 8 -#define DT_DIR 4 - -typedef struct DIR { - uint32_t KHandle; // kernel DIR_OBJECT handle - dirent Current; - bool Valid; -} DIR; diff --git a/code/user/personalities/posix/posix_dir.c b/code/user/personalities/posix/posix_dir.c index bd72e9b..9ffba02 100644 --- a/code/user/personalities/posix/posix_dir.c +++ b/code/user/personalities/posix/posix_dir.c @@ -4,7 +4,7 @@ #include #include #include -#include "posix.h" +#include "posix_internal.h" #include "posix_fd.h" #include "posix_path.h" @@ -12,31 +12,74 @@ typedef SYS_DIR_ENTRY KDIR_ENTRY; #define KATTR_DIR 0x10 -// Implements the chdir operation. +// Publishes the process-local native CWD into the authoritative posixsrv process record. +static KSTATUS PosixPublishCurrentDirectory(const char* nativeDirectory) { + if (!nativeDirectory) return NTLSTATUS_INVALID_PARAMETER; + size_t length = strlen(nativeDirectory); + if (length == 0 || length > SYS_PATH_MAX) return NTLSTATUS_INVALID_PARAMETER; + + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_SET_CURRENT_DIRECTORY; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + request.PathLength = (uint32_t)length; + memcpy(request.Path, nativeDirectory, length + 1u); + + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeCallServer(&request, &reply); + if (!KSUCCESS(status)) return status; + return reply.Status; +} + +// Implements chdir while keeping the local path resolver and posixsrv authority synchronized. __declspec(dllexport) int chdir(const char* path) { - char native[512]; - PosixTranslatePath(path, native, sizeof(native)); - uint32_t s = NtlSetCurrentDirectory(SysSvFromCstr(native)); - if (s != 0) { errno = ENOENT; return -1; } + if (!path) { errno = EINVAL; return -1; } + + char oldNative[SYS_PATH_MAX + 1u]; + size_t oldRequired = 0; + KSTATUS status = NtlGetCurrentDirectory(oldNative, sizeof(oldNative), &oldRequired); + if (!KSUCCESS(status)) { errno = PosixStatusToErrno(status); return -1; } + + char native[SYS_PATH_MAX + 1u]; + const char* translated = PosixTranslatePath(path, native, sizeof(native)); + if (translated != native || native[0] == '\0') { errno = EINVAL; return -1; } + + status = NtlSetCurrentDirectory(SysSvFromCstr(native)); + if (!KSUCCESS(status)) { errno = PosixStatusToErrno(status); return -1; } + + char canonical[SYS_PATH_MAX + 1u]; + size_t required = 0; + status = NtlGetCurrentDirectory(canonical, sizeof(canonical), &required); + if (KSUCCESS(status)) status = PosixPublishCurrentDirectory(canonical); + if (!KSUCCESS(status)) { + (void)NtlSetCurrentDirectory(SysSvFromCstr(oldNative)); + errno = PosixStatusToErrno(status); + return -1; + } return 0; } -// Implements the getcwd operation. +// Implements getcwd from the server-owned POSIX process attribute. __declspec(dllexport) char* getcwd(char* buf, size_t size) { if (!buf || size == 0) { errno = EINVAL; return (char*)0; } - char native[SYS_PATH_MAX + 1u]; - size_t required = 0; - uint32_t status = NtlGetCurrentDirectory(native, sizeof(native), &required); - if (status != NTLSTATUS_SUCCESS) { - errno = ERANGE; + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeGetProcessAttributes(&reply); + if (!KSUCCESS(status)) { + errno = PosixStatusToErrno(status); + return (char*)0; + } + if (reply.CurrentDirectoryLength == 0 || reply.CurrentDirectoryLength > SYS_PATH_MAX || + reply.CurrentDirectory[reply.CurrentDirectoryLength] != '\0') { + errno = EIO; return (char*)0; } - char posix[SYS_PATH_MAX + 1]; - NativeToPosix(native, posix, sizeof(posix)); - - size_t needed = strlen(posix) + 1; + char posix[SYS_PATH_MAX + 1u]; + NativeToPosix(reply.CurrentDirectory, posix, sizeof(posix)); + size_t needed = strlen(posix) + 1u; if (needed > size) { errno = ERANGE; return (char*)0; @@ -180,26 +223,37 @@ __declspec(dllexport) int stat(const char* path, struct stat* out) { __declspec(dllexport) int fstat(int fd, struct stat* out) { if (!out) { errno = EINVAL; return -1; } - uint32_t pid = SysGetCurrentPid(); - FD_ENTRY* e = FdGet(pid, fd); - if (!e) return -1; + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(fd); + if (!description) return -1; - switch (e->Type) { - case FD_CONSOLE_R: - case FD_CONSOLE_W: - case FD_DEVNULL: - case FD_DEVZERO: - return PosixFillSyntheticStat(S_IFCHR, out); - case FD_SOCKET: - return PosixFillSyntheticStat(S_IFSOCK, out); - case FD_FILE: { + int result = -1; + switch (description->Type) { + case PosixFdConsoleRead: + case PosixFdConsoleWrite: + case PosixFdDevNull: + case PosixFdDevZero: + result = PosixFillSyntheticStat(S_IFCHR, out); + break; + case PosixFdSocket: + result = PosixFillSyntheticStat(S_IFSOCK, out); + break; + case PosixFdPipeRead: + case PosixFdPipeWrite: + result = PosixFillSyntheticStat(S_IFIFO, out); + break; + case PosixFdFile: { SYS_FILE_STAT nativeStat; - uint32_t s = SysGetFileStat(e->KHandle, &nativeStat); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } - return PosixFillStat(&nativeStat, out); + uint32_t status = SysGetFileStat(description->KHandle, &nativeStat); + if (status != NTLSTATUS_SUCCESS) + errno = PosixStatusToErrno(status); + else + result = PosixFillStat(&nativeStat, out); + break; } default: errno = EBADF; - return -1; + break; } + PosixFdRelease(description); + return result; } diff --git a/code/user/personalities/posix/posix_dlfcn.c b/code/user/personalities/posix/posix_dlfcn.c index 537e347..48f92c8 100644 --- a/code/user/personalities/posix/posix_dlfcn.c +++ b/code/user/personalities/posix/posix_dlfcn.c @@ -5,25 +5,18 @@ #include #include #include -#include "posix.h" +#include "posix_internal.h" #include "posix_path.h" #include "posix_fd.h" -static char gDlError[160]; -static int gDlErrorPending; - -// Clears the process-local dynamic-loader error string. +// Clears the current thread's dynamic-loader error string. static void DlClearError(void) { - gDlError[0] = '\0'; - gDlErrorPending = 0; + PosixRuntimeClearDlError(); } -// Stores one process-local dynamic-loader error string. +// Stores one current-thread dynamic-loader error string. static void DlSetError(const char* message) { - if (!message) message = "dynamic loader error"; - strncpy(gDlError, message, sizeof(gDlError) - 1); - gDlError[sizeof(gDlError) - 1] = '\0'; - gDlErrorPending = 1; + PosixRuntimeSetDlError(message); } // Converts one native loader status to errno and a dlerror message. @@ -131,7 +124,5 @@ __declspec(dllexport) int dlclose(void* handle) { // Returns and clears the most recent dynamic-loader error string. __declspec(dllexport) char* dlerror(void) { - if (!gDlErrorPending || !gDlError[0]) return 0; - gDlErrorPending = 0; - return gDlError; + return PosixRuntimeTakeDlError(); } diff --git a/code/user/personalities/posix/posix_fd.c b/code/user/personalities/posix/posix_fd.c index 3bf8288..a6a2cf5 100644 --- a/code/user/personalities/posix/posix_fd.c +++ b/code/user/personalities/posix/posix_fd.c @@ -1,77 +1,977 @@ -// POSIX compatibility support for posix fd. +// POSIX descriptor table and shared open-description implementation. +#include +#include +#include +#include #include -#include "posix.h" +#include "posix_internal.h" #include "posix_fd.h" -int errno = 0; - -// Implements the status To Errno operation. +// Maps one native status to the POSIX personality errno space. int PosixStatusToErrno(uint32_t s) { switch (s) { case NTLSTATUS_SUCCESS: return 0; + case NTLSTATUS_TIMEOUT: return ETIMEDOUT; + case NTLSTATUS_PENDING: return EINPROGRESS; + case NTLSTATUS_ALERTED: + case NTLSTATUS_KERNEL_APC: + case NTLSTATUS_USER_APC: return EINTR; + case NTLSTATUS_ABANDONED: return EIO; + + case NTLSTATUS_FAILURE: return EIO; case NTLSTATUS_NOT_SUPPORTED: return ENOSYS; - case NTLSTATUS_TIMEOUT: return EAGAIN; - case NTLSTATUS_WOULD_BLOCK: return EAGAIN; case NTLSTATUS_INVALID_PARAMETER: return EINVAL; case NTLSTATUS_INVALID_HANDLE: return EBADF; case NTLSTATUS_BUFFER_TOO_SMALL: return ERANGE; + case NTLSTATUS_CHECKSUM_MISMATCH: return EIO; case NTLSTATUS_NO_MEMORY: return ENOMEM; case NTLSTATUS_ACCESS_DENIED: return EACCES; case NTLSTATUS_NOT_FOUND: return ENOENT; - case NTLSTATUS_ALREADY_EXISTS: return EEXIST; + case NTLSTATUS_ALREADY_EXISTS: case NTLSTATUS_OBJECT_NAME_COLLISION: return EEXIST; case NTLSTATUS_OBJECT_DEAD: return ECONNRESET; - case NTLSTATUS_QUEUE_FULL: return EAGAIN; - case NTLSTATUS_QUEUE_EMPTY: return EAGAIN; + case NTLSTATUS_QUEUE_FULL: + case NTLSTATUS_QUEUE_EMPTY: + case NTLSTATUS_WOULD_BLOCK: return EAGAIN; case NTLSTATUS_CANCELLED: return ECANCELED; + case NTLSTATUS_DEVICE_ERROR: return EIO; case NTLSTATUS_NO_MORE_FILES: return ENOENT; + case NTLSTATUS_INVALID_IRQL: return EIO; + case NTLSTATUS_DEADLOCK: return EDEADLK; + case NTLSTATUS_DEVICE_REMOVED: return ENODEV; + case NTLSTATUS_ID_EXHAUSTED: + case NTLSTATUS_LIMIT_REACHED: return EAGAIN; + case NTLSTATUS_CAPABILITY_REVOKED: return EBADF; + case NTLSTATUS_PORT_DISCONNECTED: return ECONNRESET; + case NTLSTATUS_BROKEN_PIPE: return EPIPE; + + case NTLSTATUS_NOT_A_DIRECTORY: return ENOTDIR; + case NTLSTATUS_IS_A_DIRECTORY: return EISDIR; + case NTLSTATUS_DIRECTORY_NOT_EMPTY: return ENOTEMPTY; + case NTLSTATUS_CROSS_DEVICE: return EXDEV; + case NTLSTATUS_NO_CHILD: return ECHILD; + case NTLSTATUS_INVALID_IMAGE_FORMAT: return ENOEXEC; + case NTLSTATUS_NAME_TOO_LONG: return ENAMETOOLONG; + case NTLSTATUS_TOO_MANY_LINKS: return EMLINK; + case NTLSTATUS_READ_ONLY_FILESYSTEM: return EROFS; + case NTLSTATUS_FILE_TOO_LARGE: return EFBIG; + + case NTLSTATUS_PROCESS_KILLED: return EINTR; + case NTLSTATUS_PROCESS_PAGE_FAULT: return EFAULT; + case NTLSTATUS_PROCESS_GENERAL_PROTECTION: + case NTLSTATUS_PROCESS_ILLEGAL_OPCODE: + case NTLSTATUS_PROCESS_DIVIDE_BY_ZERO: + case NTLSTATUS_PROCESS_BREAKPOINT: + case NTLSTATUS_PROCESS_ALIGNMENT_FAULT: + case NTLSTATUS_PROCESS_USER_EXCEPTION: + case NTLSTATUS_PROCESS_UNKNOWN_FAULT: return EIO; default: return EIO; } } -static FD_ENTRY gFdTable[FD_TABLE_PIDS][FD_MAX]; - -// Implements the fd Init operation. -__declspec(dllexport) void PosixFdInit(void) { - uint32_t pid = SysGetCurrentPid(); - uint32_t slot = pid % FD_TABLE_PIDS; - - // Clear this process's slice (handles inherited state from a previous - // process that happened to land on the same slot - unlikely but safe). - for (int i = 0; i < FD_MAX; i++) - gFdTable[slot][i] = (FD_ENTRY){ FD_FREE, 0, false }; - - gFdTable[slot][0] = (FD_ENTRY){ FD_CONSOLE_R, 0, true }; - gFdTable[slot][1] = (FD_ENTRY){ FD_CONSOLE_W, 0, true }; - gFdTable[slot][2] = (FD_ENTRY){ FD_CONSOLE_W, 0, true }; +// Locks the process descriptor table. +static int PosixFdLock(POSIX_PROCESS_STATE* process) { + if (!process || process->FdLock == SYS_HANDLE_INVALID) { + errno = EIO; + return -1; + } + uint32_t status = SysWait(process->FdLock, SYS_WAIT_FLAG_NONE); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } + return 0; } -// Implements the fd Alloc operation. -__declspec(dllexport) int FdAlloc(uint32_t pid, FD_TYPE type, uint32_t khandle) { - uint32_t slot = pid % FD_TABLE_PIDS; - for (int fd = 3; fd < FD_MAX; fd++) { - if (!gFdTable[slot][fd].Used) { - gFdTable[slot][fd] = (FD_ENTRY){ type, khandle, true }; - return fd; +// Unlocks the process descriptor table. +static void PosixFdUnlock(POSIX_PROCESS_STATE* process) { + uint32_t status = SysReleaseMutex(process->FdLock); + if (status != NTLSTATUS_SUCCESS) + errno = PosixStatusToErrno(status); +} + +// Returns whether one open-description type requires a retained native capability. +static int PosixOpenDescriptionNeedsHandle(POSIX_FD_TYPE type) { + return type == PosixFdFile || type == PosixFdSocket || + type == PosixFdPipeRead || type == PosixFdPipeWrite; +} + +// Derives the rights retained by posixsrv's master capability for one description. +static SYS_RIGHTS PosixOpenDescriptionMasterRights(POSIX_FD_TYPE type, int statusFlags) { + SYS_RIGHTS rights = SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY; + if (type == PosixFdFile) { + rights |= SYS_FILE_RIGHT_LOCK; + int access = statusFlags & O_ACCMODE; + if (access == O_RDONLY || access == O_RDWR) + rights |= SYS_FILE_RIGHT_READ_DATA | SYS_FILE_RIGHT_MAP_READ; + if (access == O_WRONLY || access == O_RDWR) + rights |= SYS_FILE_RIGHT_WRITE_DATA | SYS_FILE_RIGHT_MAP_WRITE; + } else if (type == PosixFdSocket) { + rights |= SYS_SOCKET_RIGHT_BIND | SYS_SOCKET_RIGHT_CONNECT | SYS_SOCKET_RIGHT_LISTEN | + SYS_SOCKET_RIGHT_ACCEPT | SYS_SOCKET_RIGHT_SEND | SYS_SOCKET_RIGHT_RECEIVE | + SYS_SOCKET_RIGHT_SHUTDOWN | SYS_SOCKET_RIGHT_POLL | + SYS_SOCKET_RIGHT_SET_OPTION; + } else if (type == PosixFdPipeRead) { + rights |= SYS_RIGHT_WAIT | SYS_PIPE_RIGHT_READ | SYS_PIPE_RIGHT_POLL; + } else if (type == PosixFdPipeWrite) { + rights |= SYS_RIGHT_WAIT | SYS_PIPE_RIGHT_WRITE | SYS_PIPE_RIGHT_POLL; + } + return rights; +} + +// Best-effort release of one server-owned open description after local teardown/failure. +void PosixFdReleaseServerDescription(uint64_t descriptionId) { + if (descriptionId == 0) return; + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_RELEASE_OPEN_DESCRIPTION; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + request.OpenDescriptionId = descriptionId; + NTL_POSIX_REPLY reply; + (void)PosixRuntimeCallServer(&request, &reply); +} + +// Captures process-local descriptor numbers/flags while posixsrv pins unique descriptions. +KSTATUS PosixFdCreateInheritanceSnapshot(uint32_t flags, uint64_t* tokenOut) { + if (!tokenOut || (flags & ~POSIX_FD_INHERIT_SKIP_CLOEXEC) != 0) + return NTLSTATUS_INVALID_PARAMETER; + *tokenOut = 0; + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process) return NTLSTATUS_NOT_FOUND; + if (PosixFdLock(process) != 0) return NTLSTATUS_FAILURE; + + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_CREATE_FD_INHERITANCE; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + request.FdInheritanceFlags = NTL_POSIX_FD_INHERIT_FLAG_NONE; + for (int fd = 0; fd < POSIX_FD_MAX; ++fd) { + POSIX_OPEN_DESCRIPTION* description = process->FdTable[fd].Description; + if (!description) continue; + int descriptorFlags = process->FdTable[fd].DescriptorFlags & FD_CLOEXEC; + if ((flags & POSIX_FD_INHERIT_SKIP_CLOEXEC) && (descriptorFlags & FD_CLOEXEC)) + continue; + uint32_t index = request.FdInheritanceCount++; + request.FdManifest[index].Descriptor = fd; + request.FdManifest[index].DescriptorFlags = (uint32_t)descriptorFlags; + request.FdManifest[index].OpenDescriptionId = description->DescriptionId; + } + + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeCallServer(&request, &reply); + PosixFdUnlock(process); + if (!KSUCCESS(status)) return status; + if (!KSUCCESS(reply.Status)) return reply.Status; + if (reply.FdInheritanceToken == 0 || + reply.FdInheritanceCount != request.FdInheritanceCount) { + if (reply.FdInheritanceToken != 0) + PosixFdCancelInheritanceSnapshot(reply.FdInheritanceToken); + return NTLSTATUS_FAILURE; + } + *tokenOut = reply.FdInheritanceToken; + return NTLSTATUS_SUCCESS; +} + +// Arms one snapshot to a concrete native child lifetime before the parent returns from spawn. +KSTATUS PosixFdArmInheritanceSnapshot(uint64_t token, SYS_HANDLE processHandle) { + if (token == 0 || processHandle == SYS_HANDLE_INVALID) return NTLSTATUS_INVALID_PARAMETER; + + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_ARM_FD_INHERITANCE; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + request.FdInheritanceToken = token; + + SYS_PORT_HANDLE_TRANSFER transfer; + memset(&transfer, 0, sizeof(transfer)); + transfer.Handle = processHandle; + transfer.DesiredRights = SYS_RIGHT_QUERY | SYS_RIGHT_WAIT; + transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeCallServerWithHandles(&request, &transfer, 1u, &reply); + if (!KSUCCESS(status)) return status; + return reply.Status; +} + +// Cancels one snapshot when native process construction failed before a child could claim it. +void PosixFdCancelInheritanceSnapshot(uint64_t token) { + if (token == 0) return; + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_CANCEL_FD_INHERITANCE; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + request.FdInheritanceToken = token; + NTL_POSIX_REPLY reply; + (void)PosixRuntimeCallServer(&request, &reply); +} + +// Allocates one local wrapper backed by one posixsrv-owned shared open description. +static POSIX_OPEN_DESCRIPTION* PosixOpenDescriptionCreate(POSIX_FD_TYPE type, + uint32_t khandle, + int statusFlags) { + if ((PosixOpenDescriptionNeedsHandle(type) && khandle == SYS_HANDLE_INVALID) || + (!PosixOpenDescriptionNeedsHandle(type) && khandle != SYS_HANDLE_INVALID)) { + errno = EINVAL; + return (POSIX_OPEN_DESCRIPTION*)0; + } + + POSIX_OPEN_DESCRIPTION* description = + (POSIX_OPEN_DESCRIPTION*)calloc(1, sizeof(*description)); + if (!description) { + errno = ENOMEM; + return (POSIX_OPEN_DESCRIPTION*)0; + } + + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_CREATE_OPEN_DESCRIPTION; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + request.OpenDescriptionType = (uint32_t)type; + request.OpenStatusFlags = (uint32_t)statusFlags; + + SYS_PORT_HANDLE_TRANSFER transfer; + memset(&transfer, 0, sizeof(transfer)); + const SYS_PORT_HANDLE_TRANSFER* transfers = (const SYS_PORT_HANDLE_TRANSFER*)0; + uint32_t transferCount = 0; + if (PosixOpenDescriptionNeedsHandle(type)) { + transfer.Handle = khandle; + transfer.DesiredRights = PosixOpenDescriptionMasterRights(type, statusFlags); + transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + transfers = &transfer; + transferCount = 1; + } + + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeCallServerWithHandles(&request, transfers, transferCount, &reply); + if (!KSUCCESS(status) || !KSUCCESS(reply.Status)) { + errno = PosixStatusToErrno(KSUCCESS(status) ? reply.Status : status); + free(description); + return (POSIX_OPEN_DESCRIPTION*)0; + } + + NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)(uintptr_t)reply.SharedOpenDescription; + if (!shared || reply.OpenDescriptionId == 0 || reply.OpenDescriptionType != (uint32_t)type || + shared->Magic != NTL_POSIX_SHARED_OPEN_MAGIC || + shared->Version != NTL_POSIX_SHARED_OPEN_VERSION || shared->Reserved != 0 || + shared->Reserved2 != 0 || shared->SharedObjectData != 0 || + shared->DescriptionId != reply.OpenDescriptionId || + shared->Type != (uint32_t)type) { + PosixFdReleaseServerDescription(reply.OpenDescriptionId); + errno = EIO; + free(description); + return (POSIX_OPEN_DESCRIPTION*)0; + } + + description->RefCount = 1; + description->Type = type; + description->KHandle = khandle; + description->DescriptionId = reply.OpenDescriptionId; + description->Shared = shared; + return description; +} + +// Adds one reference to an open description. +static void PosixFdReference(POSIX_OPEN_DESCRIPTION* description) { + if (description) + _InterlockedIncrement(&description->RefCount); +} + +// Returns one atomic snapshot of server-owned shared open-description status flags. +int PosixFdGetStatusFlags(const POSIX_OPEN_DESCRIPTION* description) { + if (!description || !description->Shared) + return 0; + return (int)_InterlockedCompareExchange( + (volatile long*)&description->Shared->StatusFlags, 0, 0); +} + +// Applies mutable status flags through posixsrv so native backing and shared state stay serialized. +int PosixFdSetStatusFlags(POSIX_OPEN_DESCRIPTION* description, int statusFlags) { + if (!description || !description->Shared || description->DescriptionId == 0) { + errno = EBADF; + return -1; + } + + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_SET_OPEN_STATUS; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + request.OpenDescriptionId = description->DescriptionId; + request.OpenStatusFlags = (uint32_t)statusFlags; + + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeCallServer(&request, &reply); + if (!KSUCCESS(status) || !KSUCCESS(reply.Status)) { + errno = PosixStatusToErrno(KSUCCESS(status) ? reply.Status : status); + return -1; + } + if (reply.OpenDescriptionId != description->DescriptionId || + reply.OpenDescriptionType != (uint32_t)description->Type) { + errno = EIO; + return -1; + } + return 0; +} + +// Releases one stable local wrapper and its server holder at the final reference. +void PosixFdRelease(POSIX_OPEN_DESCRIPTION* description) { + if (!description) + return; + if (_InterlockedDecrement(&description->RefCount) != 0) + return; + + if (PosixOpenDescriptionNeedsHandle(description->Type) && + description->KHandle != SYS_HANDLE_INVALID) + (void)SysCloseHandle(description->KHandle); + PosixFdReleaseServerDescription(description->DescriptionId); + free(description); +} + +// Installs a description at the lowest free descriptor at or above minFd. +static int PosixFdInstall(POSIX_PROCESS_STATE* process, + POSIX_OPEN_DESCRIPTION* description, + int minFd, + int descriptorFlags) { + if (!process || !description || minFd < 0 || minFd >= POSIX_FD_MAX) { + errno = EINVAL; + return -1; + } + if (PosixFdLock(process) != 0) + return -1; + + int fd = -1; + for (int i = minFd; i < POSIX_FD_MAX; i++) { + if (!process->FdTable[i].Description) { + PosixFdReference(description); + process->FdTable[i].Description = description; + process->FdTable[i].DescriptorFlags = descriptorFlags & FD_CLOEXEC; + fd = i; + break; } } - errno = EMFILE; - return -1; + PosixFdUnlock(process); + if (fd < 0) + errno = EMFILE; + return fd; } -// Implements the fd Get operation. -__declspec(dllexport) FD_ENTRY* FdGet(uint32_t pid, int fd) { - if (fd < 0 || fd >= FD_MAX) { errno = EBADF; return (FD_ENTRY*)0; } - uint32_t slot = pid % FD_TABLE_PIDS; - FD_ENTRY* e = &gFdTable[slot][fd]; - if (!e->Used) { errno = EBADF; return (FD_ENTRY*)0; } - return e; +// Creates and installs one new open description. +int PosixFdCreate(POSIX_FD_TYPE type, uint32_t khandle, int statusFlags) { + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process) { + errno = EIO; + return -1; + } + + POSIX_OPEN_DESCRIPTION* description = + PosixOpenDescriptionCreate(type, khandle, statusFlags); + if (!description) + return -1; + + int fd = PosixFdInstall(process, description, 3, 0); + PosixFdRelease(description); + return fd; } -// Implements the fd Free operation. -__declspec(dllexport) void FdFree(uint32_t pid, int fd) { - if (fd < 0 || fd >= FD_MAX) return; - uint32_t slot = pid % FD_TABLE_PIDS; - gFdTable[slot][fd] = (FD_ENTRY){ FD_FREE, 0, false }; +// Adopts one server-owned description into a process-local wrapper. +static POSIX_OPEN_DESCRIPTION* PosixOpenDescriptionImport(POSIX_FD_TYPE type, + uint32_t khandle, + uint64_t descriptionId, + NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared) { + int needsHandle = PosixOpenDescriptionNeedsHandle(type); + int isPipe = type == PosixFdPipeRead || type == PosixFdPipeWrite; + if (descriptionId == 0 || !shared || + (needsHandle && khandle == SYS_HANDLE_INVALID) || + (!needsHandle && khandle != SYS_HANDLE_INVALID) || + shared->Magic != NTL_POSIX_SHARED_OPEN_MAGIC || + shared->Version != NTL_POSIX_SHARED_OPEN_VERSION || shared->Reserved != 0 || + shared->Reserved2 != 0 || shared->DescriptionId != descriptionId || + shared->Type != (uint32_t)type || + (isPipe && shared->SharedObjectData == 0) || + (!isPipe && shared->SharedObjectData != 0)) { + if (needsHandle && khandle != SYS_HANDLE_INVALID) (void)SysCloseHandle(khandle); + PosixFdReleaseServerDescription(descriptionId); + errno = EIO; + return (POSIX_OPEN_DESCRIPTION*)0; + } + POSIX_OPEN_DESCRIPTION* description = + (POSIX_OPEN_DESCRIPTION*)calloc(1, sizeof(*description)); + if (!description) { + if (needsHandle) (void)SysCloseHandle(khandle); + PosixFdReleaseServerDescription(descriptionId); + errno = ENOMEM; + return (POSIX_OPEN_DESCRIPTION*)0; + } + description->RefCount = 1; + description->Type = type; + description->KHandle = khandle; + description->DescriptionId = descriptionId; + description->Shared = shared; + return description; +} + +// Installs one process-local wrapper for a server-created shared open description. +int PosixFdImport(POSIX_FD_TYPE type, uint32_t khandle, uint64_t descriptionId, + NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared, int descriptorFlags) { + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process) { errno = EIO; return -1; } + POSIX_OPEN_DESCRIPTION* description = + PosixOpenDescriptionImport(type, khandle, descriptionId, shared); + if (!description) return -1; + int fd = PosixFdInstall(process, description, 3, descriptorFlags); + PosixFdRelease(description); + return fd; +} + +// Acquires a stable reference to the open description backing fd. +POSIX_OPEN_DESCRIPTION* PosixFdAcquire(int fd) { + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process || fd < 0 || fd >= POSIX_FD_MAX) { + errno = EBADF; + return (POSIX_OPEN_DESCRIPTION*)0; + } + if (PosixFdLock(process) != 0) + return (POSIX_OPEN_DESCRIPTION*)0; + + POSIX_OPEN_DESCRIPTION* description = process->FdTable[fd].Description; + if (description) + PosixFdReference(description); + PosixFdUnlock(process); + + if (!description) + errno = EBADF; + return description; +} + +// Removes one descriptor and releases its table-owned reference. +int PosixFdClose(int fd) { + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process || fd < 0 || fd >= POSIX_FD_MAX) { + errno = EBADF; + return -1; + } + if (PosixFdLock(process) != 0) + return -1; + + POSIX_OPEN_DESCRIPTION* description = process->FdTable[fd].Description; + if (!description) { + PosixFdUnlock(process); + errno = EBADF; + return -1; + } + process->FdTable[fd].Description = (POSIX_OPEN_DESCRIPTION*)0; + process->FdTable[fd].DescriptorFlags = 0; + PosixFdUnlock(process); + PosixFdRelease(description); + return 0; +} + +// Installs one fixed standard descriptor during process initialization. +static uint32_t PosixFdInstallStandard(POSIX_PROCESS_STATE* process, + int fd, + POSIX_FD_TYPE type, + int statusFlags) { + POSIX_OPEN_DESCRIPTION* description = + PosixOpenDescriptionCreate(type, SYS_HANDLE_INVALID, statusFlags); + if (!description) + return NTLSTATUS_NO_MEMORY; + + PosixFdReference(description); + process->FdTable[fd].Description = description; + process->FdTable[fd].DescriptorFlags = 0; + PosixFdRelease(description); + return NTLSTATUS_SUCCESS; +} + +// Reconstructs one exact descriptor table from a server-owned inheritance snapshot. +static uint32_t PosixFdImportInheritance(POSIX_PROCESS_STATE* process, uint64_t token) { + if (!process || token == 0) return NTLSTATUS_INVALID_PARAMETER; + + NTL_POSIX_FD_MANIFEST_ENTRY manifest[NTL_POSIX_FD_INHERIT_MAX]; + memset(manifest, 0, sizeof(manifest)); + uint32_t manifestCount = UINT32_MAX; + POSIX_OPEN_DESCRIPTION* descriptions[NTL_POSIX_FD_INHERIT_MAX]; + uint64_t descriptionIds[NTL_POSIX_FD_INHERIT_MAX]; + memset(descriptions, 0, sizeof(descriptions)); + memset(descriptionIds, 0, sizeof(descriptionIds)); + uint32_t descriptionCount = 0; + uint32_t descriptionIndex = 0; + uint32_t descriptionTotal = UINT32_MAX; + uint32_t result = NTLSTATUS_SUCCESS; + + for (;;) { + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_IMPORT_FD_INHERITANCE; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + request.FdInheritanceToken = token; + request.FdInheritanceIndex = descriptionIndex; + + SYS_HANDLE handles[NTL_POSIX_FD_IMPORT_BATCH_MAX]; + for (uint32_t i = 0; i < NTL_POSIX_FD_IMPORT_BATCH_MAX; ++i) + handles[i] = SYS_HANDLE_INVALID; + uint32_t handleCount = 0; + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeCallServerExchangeVariable( + &request, handles, NTL_POSIX_FD_IMPORT_BATCH_MAX, &handleCount, &reply); + if (!KSUCCESS(status) || !KSUCCESS(reply.Status)) { + result = KSUCCESS(status) ? reply.Status : status; + for (uint32_t i = 0; i < handleCount; ++i) + if (handles[i] != SYS_HANDLE_INVALID) (void)SysCloseHandle(handles[i]); + break; + } + if (reply.FdInheritanceToken != token || reply.FdInheritanceIndex != descriptionIndex || + reply.FdInheritanceCount > NTL_POSIX_FD_INHERIT_MAX || + reply.FdDescriptionIndex != descriptionIndex || + reply.FdDescriptionCount > NTL_POSIX_FD_IMPORT_BATCH_MAX || + reply.FdDescriptionTotal > NTL_POSIX_FD_INHERIT_MAX || + descriptionIndex + reply.FdDescriptionCount > reply.FdDescriptionTotal) { + result = NTLSTATUS_FAILURE; + for (uint32_t i = 0; i < handleCount; ++i) + if (handles[i] != SYS_HANDLE_INVALID) (void)SysCloseHandle(handles[i]); + break; + } + + if (manifestCount == UINT32_MAX) { + manifestCount = reply.FdInheritanceCount; + memcpy(manifest, reply.FdManifest, sizeof(manifest)); + descriptionTotal = reply.FdDescriptionTotal; + } else if (manifestCount != reply.FdInheritanceCount || + descriptionTotal != reply.FdDescriptionTotal || + memcmp(manifest, reply.FdManifest, sizeof(manifest)) != 0) { + result = NTLSTATUS_FAILURE; + for (uint32_t i = 0; i < handleCount; ++i) + if (handles[i] != SYS_HANDLE_INVALID) (void)SysCloseHandle(handles[i]); + break; + } + + uint8_t handleUsed[NTL_POSIX_FD_IMPORT_BATCH_MAX]; + memset(handleUsed, 0, sizeof(handleUsed)); + for (uint32_t i = 0; i < reply.FdDescriptionCount; ++i) { + NTL_POSIX_FD_DESCRIPTION_ENTRY* entry = &reply.FdDescriptions[i]; + if (entry->Reserved != 0 || entry->OpenDescriptionId == 0 || + entry->OpenDescriptionType < NTL_POSIX_OPEN_TYPE_FILE || + entry->OpenDescriptionType > NTL_POSIX_OPEN_TYPE_PIPE_WRITE || + descriptionCount >= NTL_POSIX_FD_INHERIT_MAX) { + result = NTLSTATUS_FAILURE; + break; + } + for (uint32_t j = 0; j < descriptionCount; ++j) { + if (descriptionIds[j] == entry->OpenDescriptionId) { result = NTLSTATUS_FAILURE; break; } + } + if (!KSUCCESS(result)) break; + POSIX_FD_TYPE type = (POSIX_FD_TYPE)entry->OpenDescriptionType; + int needsHandle = PosixOpenDescriptionNeedsHandle(type); + SYS_HANDLE handle = SYS_HANDLE_INVALID; + if (needsHandle) { + if (entry->HandleIndex >= handleCount || handleUsed[entry->HandleIndex]) { + result = NTLSTATUS_FAILURE; + break; + } + handleUsed[entry->HandleIndex] = 1u; + handle = handles[entry->HandleIndex]; + handles[entry->HandleIndex] = SYS_HANDLE_INVALID; + } else if (entry->HandleIndex != NTL_POSIX_FD_HANDLE_NONE) { + result = NTLSTATUS_FAILURE; + break; + } + POSIX_OPEN_DESCRIPTION* description = PosixOpenDescriptionImport( + type, handle, entry->OpenDescriptionId, + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)(uintptr_t)entry->SharedOpenDescription); + if (!description) { result = NTLSTATUS_FAILURE; break; } + descriptions[descriptionCount] = description; + descriptionIds[descriptionCount] = entry->OpenDescriptionId; + ++descriptionCount; + } + for (uint32_t i = 0; i < handleCount; ++i) { + if (handles[i] != SYS_HANDLE_INVALID) { + (void)SysCloseHandle(handles[i]); + if (KSUCCESS(result)) result = NTLSTATUS_FAILURE; + } + } + if (!KSUCCESS(result)) break; + + descriptionIndex += reply.FdDescriptionCount; + if (descriptionIndex == descriptionTotal) break; + if (reply.FdDescriptionCount == 0) { result = NTLSTATUS_FAILURE; break; } + } + + if (KSUCCESS(result)) { + if (manifestCount == UINT32_MAX || descriptionTotal == UINT32_MAX || + descriptionCount != descriptionTotal) + result = NTLSTATUS_FAILURE; + } + + if (KSUCCESS(result)) { + uint8_t fdUsed[POSIX_FD_MAX]; + memset(fdUsed, 0, sizeof(fdUsed)); + for (uint32_t i = 0; i < manifestCount; ++i) { + NTL_POSIX_FD_MANIFEST_ENTRY* entry = &manifest[i]; + if (entry->Descriptor < 0 || entry->Descriptor >= POSIX_FD_MAX || + fdUsed[entry->Descriptor] || + (entry->DescriptorFlags & ~NTL_POSIX_FD_DESCRIPTOR_CLOEXEC) != 0) { + result = NTLSTATUS_FAILURE; + break; + } + POSIX_OPEN_DESCRIPTION* description = (POSIX_OPEN_DESCRIPTION*)0; + for (uint32_t j = 0; j < descriptionCount; ++j) { + if (descriptionIds[j] == entry->OpenDescriptionId) { + description = descriptions[j]; + break; + } + } + if (!description) { result = NTLSTATUS_FAILURE; break; } + fdUsed[entry->Descriptor] = 1u; + PosixFdReference(description); + process->FdTable[entry->Descriptor].Description = description; + process->FdTable[entry->Descriptor].DescriptorFlags = + (int)(entry->DescriptorFlags & NTL_POSIX_FD_DESCRIPTOR_CLOEXEC); + } + } + + for (uint32_t i = 0; i < descriptionCount; ++i) + PosixFdRelease(descriptions[i]); + return result; +} + +// Initializes the process descriptor table and standard descriptors. +uint32_t PosixFdInitializeProcess(POSIX_PROCESS_STATE* process, uint64_t inheritanceToken) { + if (!process) + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t lock = SYS_HANDLE_INVALID; + uint32_t status = SysCreateMutex(&lock); + if (status != NTLSTATUS_SUCCESS) + return status; + process->FdLock = lock; + memset(process->FdTable, 0, sizeof(process->FdTable)); + + if (inheritanceToken != 0) { + status = PosixFdImportInheritance(process, inheritanceToken); + if (status != NTLSTATUS_SUCCESS) PosixFdDestroyProcess(process); + return status; + } + + status = PosixFdInstallStandard(process, 0, PosixFdConsoleRead, O_RDONLY); + if (status == NTLSTATUS_SUCCESS) + status = PosixFdInstallStandard(process, 1, PosixFdConsoleWrite, O_WRONLY); + if (status == NTLSTATUS_SUCCESS) + status = PosixFdInstallStandard(process, 2, PosixFdConsoleWrite, O_WRONLY); + if (status != NTLSTATUS_SUCCESS) + PosixFdDestroyProcess(process); + return status; +} + +// Releases descriptor-table resources during failed process startup. +void PosixFdDestroyProcess(POSIX_PROCESS_STATE* process) { + if (!process) + return; + + for (int fd = 0; fd < POSIX_FD_MAX; fd++) { + POSIX_OPEN_DESCRIPTION* description = process->FdTable[fd].Description; + process->FdTable[fd].Description = (POSIX_OPEN_DESCRIPTION*)0; + process->FdTable[fd].DescriptorFlags = 0; + PosixFdRelease(description); + } + if (process->FdLock != SYS_HANDLE_INVALID) { + SysCloseHandle(process->FdLock); + process->FdLock = SYS_HANDLE_INVALID; + } +} + +// Duplicates oldfd into the lowest free descriptor at or above minFd. +static int PosixFdDuplicateFrom(int oldfd, int minFd) { + if (minFd < 0 || minFd >= POSIX_FD_MAX) { + errno = EINVAL; + return -1; + } + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(oldfd); + if (!description) + return -1; + int result = PosixFdInstall(PosixRuntimeProcessState(), description, minFd, 0); + PosixFdRelease(description); + return result; +} + +// Duplicates one descriptor to the lowest available descriptor. +NTL_POSIX_API int dup(int oldfd) { + return PosixFdDuplicateFrom(oldfd, 0); +} + +// Duplicates oldfd onto newfd, atomically replacing newfd when necessary. +NTL_POSIX_API int dup2(int oldfd, int newfd) { + if (newfd < 0 || newfd >= POSIX_FD_MAX) { + errno = EBADF; + return -1; + } + + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(oldfd); + if (!description) + return -1; + if (oldfd == newfd) { + PosixFdRelease(description); + return newfd; + } + + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (PosixFdLock(process) != 0) { + PosixFdRelease(description); + return -1; + } + + POSIX_OPEN_DESCRIPTION* replaced = process->FdTable[newfd].Description; + PosixFdReference(description); + process->FdTable[newfd].Description = description; + process->FdTable[newfd].DescriptorFlags = 0; + PosixFdUnlock(process); + + PosixFdRelease(replaced); + PosixFdRelease(description); + return newfd; +} + +// Resolves one flock range to the native absolute byte-range form. +static int PosixFlockRange(POSIX_OPEN_DESCRIPTION* description, + const struct flock* lock, + uint64_t* offsetOut, + uint64_t* lengthOut) { + if (!description || !lock || !offsetOut || !lengthOut) { + errno = EINVAL; + return -1; + } + if (description->Type != PosixFdFile) { + errno = EBADF; + return -1; + } + + int64_t base = 0; + uint32_t status = NTLSTATUS_SUCCESS; + if (lock->l_whence == SEEK_CUR) { + uint64_t current = 0; + status = SysGetFilePos(description->KHandle, ¤t); + if (status == NTLSTATUS_SUCCESS) { + if (current > 0x7FFFFFFFULL) status = NTLSTATUS_INVALID_PARAMETER; + else base = (int64_t)current; + } + } else if (lock->l_whence == SEEK_END) { + uint64_t size = 0; + status = SysGetFileSize(description->KHandle, &size); + if (status == NTLSTATUS_SUCCESS) { + if (size > 0x7FFFFFFFULL) status = NTLSTATUS_INVALID_PARAMETER; + else base = (int64_t)size; + } + } else if (lock->l_whence != SEEK_SET) { + status = NTLSTATUS_INVALID_PARAMETER; + } + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } + + int64_t start = base + (int64_t)lock->l_start; + int64_t length = (int64_t)lock->l_len; + if (length < 0) { + start += length; + length = -length; + } + if (start < 0 || start > 0x7FFFFFFFLL) { + errno = EINVAL; + return -1; + } + + *offsetOut = (uint64_t)start; + *lengthOut = length == 0 ? SYS_FILE_LOCK_TO_EOF : (uint64_t)length; + return 0; +} + +// Implements F_GETLK/F_SETLK/F_SETLKW on the native process-owned lock table. +static int PosixFdControlLock(POSIX_OPEN_DESCRIPTION* description, int command, struct flock* lock) { + if (!description || !lock) { + errno = EINVAL; + return -1; + } + if (description->Type != PosixFdFile) { + errno = EBADF; + return -1; + } + + uint64_t offset = 0; + uint64_t length = 0; + if (PosixFlockRange(description, lock, &offset, &length) != 0) + return -1; + + if (command == F_GETLK) { + if (lock->l_type != F_RDLCK && lock->l_type != F_WRLCK) { + errno = EINVAL; + return -1; + } + SYS_FILE_LOCK_INFO info; + memset(&info, 0, sizeof(info)); + SYS_QUERY_FILE_LOCK_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.FileHandle = description->KHandle; + request.Mode = lock->l_type == F_RDLCK ? SYS_FILE_LOCK_SHARED : SYS_FILE_LOCK_EXCLUSIVE; + request.Flags = SYS_FILE_LOCK_FLAG_NONE; + request.Offset = offset; + request.Length = length; + request.InfoOut = &info; + uint32_t status = SysQueryFileLock(&request); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } + if (info.Mode == SYS_FILE_LOCK_NONE) { + lock->l_type = F_UNLCK; + lock->l_pid = 0; + return 0; + } + lock->l_type = info.Mode == SYS_FILE_LOCK_SHARED ? F_RDLCK : F_WRLCK; + lock->l_whence = SEEK_SET; + lock->l_start = (off_t)info.Offset; + lock->l_len = info.Length == SYS_FILE_LOCK_TO_EOF ? 0 : (off_t)info.Length; + lock->l_pid = (pid_t)info.OwnerProcessId; + return 0; + } + + if (lock->l_type == F_UNLCK) { + SYS_UNLOCK_FILE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.FileHandle = description->KHandle; + request.Flags = SYS_FILE_LOCK_FLAG_NONE; + request.Offset = offset; + request.Length = length; + uint32_t status = SysUnlockFile(&request); + if (status == NTLSTATUS_SUCCESS || status == NTLSTATUS_NOT_FOUND) + return 0; + errno = PosixStatusToErrno(status); + return -1; + } + + int statusFlags = PosixFdGetStatusFlags(description); + uint32_t mode = 0; + if (lock->l_type == F_RDLCK) { + if ((statusFlags & O_ACCMODE) == O_WRONLY) { + errno = EBADF; + return -1; + } + mode = SYS_FILE_LOCK_SHARED; + } else if (lock->l_type == F_WRLCK) { + if ((statusFlags & O_ACCMODE) == O_RDONLY) { + errno = EBADF; + return -1; + } + mode = SYS_FILE_LOCK_EXCLUSIVE; + } else { + errno = EINVAL; + return -1; + } + + SYS_LOCK_FILE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.FileHandle = description->KHandle; + request.Mode = mode; + request.Flags = SYS_FILE_LOCK_FLAG_NONE; + request.Offset = offset; + request.Length = length; + request.TimeoutMilliseconds = command == F_SETLKW ? SYS_WAIT_TIMEOUT_INFINITE : 0; + uint32_t status = SysLockFile(&request); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } + return 0; +} + +// Implements descriptor and open-description file-control operations. +NTL_POSIX_API int fcntl(int fd, int command, ...) { + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process || fd < 0 || fd >= POSIX_FD_MAX) { + errno = EBADF; + return -1; + } + + va_list args; + va_start(args, command); + + if (command == F_DUPFD) { + int minFd = va_arg(args, int); + va_end(args); + return PosixFdDuplicateFrom(fd, minFd); + } + + if (command == F_GETFL) { + va_end(args); + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(fd); + if (!description) + return -1; + int flags = PosixFdGetStatusFlags(description); + PosixFdRelease(description); + return flags; + } + + if (command == F_SETFL) { + int flags = va_arg(args, int); + va_end(args); + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(fd); + if (!description) + return -1; + int result = PosixFdSetStatusFlags(description, flags); + PosixFdRelease(description); + return result; + } + + if (command == F_GETLK || command == F_SETLK || command == F_SETLKW) { + struct flock* lock = va_arg(args, struct flock*); + va_end(args); + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(fd); + if (!description) + return -1; + int result = PosixFdControlLock(description, command, lock); + PosixFdRelease(description); + return result; + } + + if (PosixFdLock(process) != 0) { + va_end(args); + return -1; + } + if (!process->FdTable[fd].Description) { + PosixFdUnlock(process); + va_end(args); + errno = EBADF; + return -1; + } + + int result = -1; + if (command == F_GETFD) { + result = process->FdTable[fd].DescriptorFlags; + } else if (command == F_SETFD) { + int flags = va_arg(args, int); + process->FdTable[fd].DescriptorFlags = flags & FD_CLOEXEC; + result = 0; + } else { + errno = EINVAL; + } + PosixFdUnlock(process); + va_end(args); + return result; } diff --git a/code/user/personalities/posix/posix_fd.h b/code/user/personalities/posix/posix_fd.h index 661b0e2..0f90f2b 100644 --- a/code/user/personalities/posix/posix_fd.h +++ b/code/user/personalities/posix/posix_fd.h @@ -1,39 +1,81 @@ -// POSIX compatibility support for posix fd. +// POSIX descriptor/open-description declarations internal to posix.dll. #pragma once -// posix_fd.h - file descriptor table (internal to posix.dll). + #include #include +#include -typedef enum { - FD_FREE, - FD_FILE, - FD_CONSOLE_R, // fd 0 - reads from keyboard through SysReadKeyboardChar - FD_CONSOLE_W, // fd 1, 2 - writes via SysWriteConsole - FD_DEVNULL, // /dev/null - FD_DEVZERO, // /dev/zero - FD_SOCKET, // kernel socket handle -} FD_TYPE; +#define POSIX_FD_MAX 64 +#define POSIX_FD_INHERIT_SKIP_CLOEXEC 0x00000001u -typedef struct { - FD_TYPE Type; - uint32_t KHandle; // kernel handle; 0 for console/devnull/devzero - bool Used; -} FD_ENTRY; +// Open-description kinds owned by the POSIX personality. +typedef enum POSIX_FD_TYPE { + PosixFdFile = NTL_POSIX_OPEN_TYPE_FILE, + PosixFdConsoleRead = NTL_POSIX_OPEN_TYPE_CONSOLE_READ, + PosixFdConsoleWrite = NTL_POSIX_OPEN_TYPE_CONSOLE_WRITE, + PosixFdDevNull = NTL_POSIX_OPEN_TYPE_DEV_NULL, + PosixFdDevZero = NTL_POSIX_OPEN_TYPE_DEV_ZERO, + PosixFdSocket = NTL_POSIX_OPEN_TYPE_SOCKET, + PosixFdPipeRead = NTL_POSIX_OPEN_TYPE_PIPE_READ, + PosixFdPipeWrite = NTL_POSIX_OPEN_TYPE_PIPE_WRITE, +} POSIX_FD_TYPE; -#define FD_MAX 64 -#define FD_TABLE_PIDS 32 // circular, indexed by pid % FD_TABLE_PIDS +// One refcounted open description shared by one or more descriptors. +typedef struct POSIX_OPEN_DESCRIPTION { + volatile long RefCount; + POSIX_FD_TYPE Type; + uint32_t KHandle; + uint64_t DescriptionId; + NTL_POSIX_SHARED_OPEN_DESCRIPTION* Shared; +} POSIX_OPEN_DESCRIPTION; -// Called from PosixStart before main(); sets up fds 0/1/2 for this process. -__declspec(dllexport) void PosixFdInit(void); +// One process-local descriptor entry with descriptor-local flags. +typedef struct POSIX_FD { + POSIX_OPEN_DESCRIPTION* Description; + int DescriptorFlags; +} POSIX_FD; -// Allocate the lowest free fd >= 3. Returns -1 and sets errno=EMFILE on failure. -__declspec(dllexport) int FdAlloc(uint32_t pid, FD_TYPE type, uint32_t khandle); +struct POSIX_PROCESS_STATE; -// Look up an fd. Returns NULL and sets errno=EBADF if invalid. -__declspec(dllexport) FD_ENTRY* FdGet(uint32_t pid, int fd); +// Initializes the process descriptor table from inherited state or fresh standard descriptors. +uint32_t PosixFdInitializeProcess(struct POSIX_PROCESS_STATE* process, uint64_t inheritanceToken); -// Release an fd slot (does NOT close the kernel handle). -__declspec(dllexport) void FdFree(uint32_t pid, int fd); +// Captures the current descriptor table in one server-owned inheritance snapshot. +KSTATUS PosixFdCreateInheritanceSnapshot(uint32_t flags, uint64_t* tokenOut); -int PosixStatusToErrno(uint32_t s); +// Arms one inheritance snapshot to the lifetime of the native child process. +KSTATUS PosixFdArmInheritanceSnapshot(uint64_t token, SYS_HANDLE processHandle); + +// Cancels one unclaimed inheritance snapshot owned by the current process. +void PosixFdCancelInheritanceSnapshot(uint64_t token); + +// Releases descriptor-table resources during failed process startup. +void PosixFdDestroyProcess(struct POSIX_PROCESS_STATE* process); + +// Creates and installs one new open description at the lowest free descriptor >= 3. +int PosixFdCreate(POSIX_FD_TYPE type, uint32_t khandle, int statusFlags); + +// Installs one process-local wrapper for a server-created shared open description. +int PosixFdImport(POSIX_FD_TYPE type, uint32_t khandle, uint64_t descriptionId, + NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared, int descriptorFlags); + +// Releases one server holder when import/construction fails before a local wrapper exists. +void PosixFdReleaseServerDescription(uint64_t descriptionId); + +// Acquires a stable reference to an open description backing fd. +POSIX_OPEN_DESCRIPTION* PosixFdAcquire(int fd); + +// Returns one atomic snapshot of shared open-description status flags. +int PosixFdGetStatusFlags(const POSIX_OPEN_DESCRIPTION* description); + +// Applies F_SETFL mutable status through the server-owned open-description authority. +int PosixFdSetStatusFlags(POSIX_OPEN_DESCRIPTION* description, int statusFlags); + +// Releases one stable open-description reference. +void PosixFdRelease(POSIX_OPEN_DESCRIPTION* description); + +// Removes one descriptor and releases its table-owned open-description reference. +int PosixFdClose(int fd); + +int PosixStatusToErrno(uint32_t status); diff --git a/code/user/personalities/posix/posix_internal.h b/code/user/personalities/posix/posix_internal.h new file mode 100644 index 0000000..e41b03e --- /dev/null +++ b/code/user/personalities/posix/posix_internal.h @@ -0,0 +1,134 @@ +// POSIX personality internal declarations and process/thread runtime state. + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "posix_fd.h" + +#define POSIX_PROCESS_STATE_MAGIC 0x50505243u +#define POSIX_THREAD_STATE_MAGIC 0x50544852u +#define POSIX_DLERROR_MAX 160u +#define POSIX_SPAWN_TABLE_SIZE 32u +#define POSIX_PTHREAD_TABLE_SIZE 32u + +typedef struct POSIX_SPAWN_ENTRY { + pid_t Pid; + SYS_HANDLE Handle; + uint64_t FdInheritanceToken; + bool Used; +} POSIX_SPAWN_ENTRY; + +typedef struct POSIX_PTHREAD_SLOT { + uint32_t Tid; + void* RetVal; + bool Used; +} POSIX_PTHREAD_SLOT; + +typedef struct POSIX_LOGICAL_PROCESS_STATE { + uint32_t PosixPid; + uint32_t Generation; +} POSIX_LOGICAL_PROCESS_STATE; + +typedef struct POSIX_PROCESS_STATE { + uint32_t Magic; + SYS_PEB* Peb; + SYS_HANDLE ServerEndpoint; + POSIX_LOGICAL_PROCESS_STATE DefaultIdentity; + SYS_HANDLE FdLock; + POSIX_FD FdTable[POSIX_FD_MAX]; + POSIX_SPAWN_ENTRY SpawnTable[POSIX_SPAWN_TABLE_SIZE]; + POSIX_PTHREAD_SLOT PthreadTable[POSIX_PTHREAD_TABLE_SIZE]; +} POSIX_PROCESS_STATE; + +typedef struct POSIX_THREAD_STATE { + uint32_t Magic; + int ErrnoValue; + uint8_t DlErrorPending; + uint8_t Reserved[3]; + char DlError[POSIX_DLERROR_MAX]; + POSIX_PROCESS_STATE* NativeProcess; + POSIX_LOGICAL_PROCESS_STATE* LogicalProcess; + struct passwd PasswordResult; + char PasswordName[NTL_POSIX_IDENTITY_NAME_MAX + 1u]; + char PasswordHome[NTL_POSIX_IDENTITY_HOME_MAX + 1u]; + char PasswordShell[NTL_POSIX_IDENTITY_SHELL_MAX + 1u]; + struct group GroupResult; + char GroupName[NTL_POSIX_IDENTITY_NAME_MAX + 1u]; + char* GroupMembers[1]; + char LoginName[NTL_POSIX_IDENTITY_NAME_MAX + 1u]; +} POSIX_THREAD_STATE; + +// Initializes process-local POSIX state and attaches the initial thread. +uint32_t PosixRuntimeInitializeProcess(void); + +// Returns the POSIX state for the current process after startup. +POSIX_PROCESS_STATE* PosixRuntimeProcessState(void); + +// Returns the current thread state, attaching lazily when necessary. +POSIX_THREAD_STATE* PosixRuntimeThreadState(void); + +// Attaches POSIX state to the current native TEB. +uint32_t PosixRuntimeAttachThread(void); + +// Releases POSIX state owned by the current native TEB. +void PosixRuntimeDetachThread(void); + +// Clears the current thread's dynamic-loader error state. +void PosixRuntimeClearDlError(void); + +// Stores one dynamic-loader error in current-thread state. +void PosixRuntimeSetDlError(const char* message); + +// Returns and consumes the current thread's dynamic-loader error. +char* PosixRuntimeTakeDlError(void); + +// Calls posixsrv over the registered process endpoint and validates one fixed-size reply. +KSTATUS PosixRuntimeCallServer(const NTL_POSIX_REQUEST* request, NTL_POSIX_REPLY* replyOut); + +// Calls posixsrv while transferring a bounded set of native capabilities. +KSTATUS PosixRuntimeCallServerWithHandles(const NTL_POSIX_REQUEST* request, + const SYS_PORT_HANDLE_TRANSFER* handles, + uint32_t handleCount, + NTL_POSIX_REPLY* replyOut); + +// Calls posixsrv with independent request/reply capability transfer. +KSTATUS PosixRuntimeCallServerExchange(const NTL_POSIX_REQUEST* request, + const SYS_PORT_HANDLE_TRANSFER* requestHandles, + uint32_t requestHandleCount, + SYS_HANDLE* replyHandlesOut, + uint32_t replyHandleCapacity, + uint32_t expectedReplyHandleCount, + NTL_POSIX_REPLY* replyOut); + +// Calls posixsrv and accepts a variable number of reply capabilities up to the supplied capacity. +KSTATUS PosixRuntimeCallServerExchangeVariable(const NTL_POSIX_REQUEST* request, + SYS_HANDLE* replyHandlesOut, + uint32_t replyHandleCapacity, + uint32_t* replyHandleCountOut, + NTL_POSIX_REPLY* replyOut); + +// Queries the server-owned shared process attributes for the current logical process. +KSTATUS PosixRuntimeGetProcessAttributes(NTL_POSIX_REPLY* replyOut); diff --git a/code/user/personalities/posix/posix_io.c b/code/user/personalities/posix/posix_io.c index f4f4466..16c024e 100644 --- a/code/user/personalities/posix/posix_io.c +++ b/code/user/personalities/posix/posix_io.c @@ -1,53 +1,223 @@ -// POSIX compatibility support for posix io. +// POSIX compatibility support for byte-stream file I/O. #include #include +#include #include +#include #include -#include "posix.h" +#include "posix_internal.h" #include "posix_fd.h" #include "posix_path.h" -// Implements the clearscreen operation. -__declspec(dllexport) void clearscreen(void) { - SysClearScreen(0x012456); +// Returns whether one open description permits reading. +static bool PosixDescriptionReadable(const POSIX_OPEN_DESCRIPTION* description) { + return description && (PosixFdGetStatusFlags(description) & O_ACCMODE) != O_WRONLY; } -// Implements the clearkeyboard operation. -__declspec(dllexport) void clearkeyboard(void) { - INPUT_KEY key; - while (SysPollKeyboard(&key) == NTLSTATUS_SUCCESS) { - char ignored; - (void)SysReadKeyboardChar(&ignored); +// Returns whether one open description permits writing. +static bool PosixDescriptionWritable(const POSIX_OPEN_DESCRIPTION* description) { + return description && (PosixFdGetStatusFlags(description) & O_ACCMODE) != O_RDONLY; +} + +// Returns the server-owned shared ring behind one pipe open description. +static SYS_PIPE_SHARED_STATE* PosixPipeShared(POSIX_OPEN_DESCRIPTION* description) { + if (!description || !description->Shared || description->Shared->SharedObjectData == 0) { + errno = EIO; + return (SYS_PIPE_SHARED_STATE*)0; + } + SYS_PIPE_SHARED_STATE* shared = + (SYS_PIPE_SHARED_STATE*)(uintptr_t)description->Shared->SharedObjectData; + if (shared->Magic != SYS_PIPE_SHARED_MAGIC || shared->Version != SYS_PIPE_SHARED_VERSION || + shared->Capacity != SYS_PIPE_BUFFER_CAPACITY || + shared->HeaderBytes != SYS_PIPE_SHARED_HEADER_BYTES) { + errno = EIO; + return (SYS_PIPE_SHARED_STATE*)0; + } + return shared; +} + +// Acquires one shared pipe-side serialization lock without entering the kernel. +static void PosixPipeLock(volatile long* lock) { + while (_InterlockedCompareExchange(lock, 1, 0) != 0) + _mm_pause(); + _ReadWriteBarrier(); +} + +// Releases one shared pipe-side serialization lock. +static void PosixPipeUnlock(volatile long* lock) { + _ReadWriteBarrier(); + _InterlockedExchange(lock, 0); +} + +// Returns buffered bytes, rejecting corrupted shared counters. +static int PosixPipeUsed(SYS_PIPE_SHARED_STATE* shared, uint64_t* usedOut) { + uint64_t readPosition = shared->ReadPosition; + _ReadWriteBarrier(); + uint64_t writePosition = shared->WritePosition; + uint64_t used = writePosition - readPosition; + if (used > SYS_PIPE_BUFFER_CAPACITY) { + errno = EIO; + return -1; + } + *usedOut = used; + return 0; +} + +// Copies bytes out of the shared circular pipe buffer. +static void PosixPipeCopyOut(SYS_PIPE_SHARED_STATE* shared, uint64_t readPosition, + void* buffer, size_t length) { + size_t offset = (size_t)(readPosition % SYS_PIPE_BUFFER_CAPACITY); + size_t first = SYS_PIPE_BUFFER_CAPACITY - offset; + if (first > length) first = length; + memcpy(buffer, shared->Buffer + offset, first); + if (first < length) + memcpy((uint8_t*)buffer + first, shared->Buffer, length - first); +} + +// Copies bytes into the shared circular pipe buffer. +static void PosixPipeCopyIn(SYS_PIPE_SHARED_STATE* shared, uint64_t writePosition, + const void* buffer, size_t length) { + size_t offset = (size_t)(writePosition % SYS_PIPE_BUFFER_CAPACITY); + size_t first = SYS_PIPE_BUFFER_CAPACITY - offset; + if (first > length) first = length; + memcpy(shared->Buffer + offset, buffer, first); + if (first < length) + memcpy(shared->Buffer, (const uint8_t*)buffer + first, length - first); +} + +// Reads one pipe entirely through the shared user mapping except when blocking. +static ssize_t PosixPipeRead(POSIX_OPEN_DESCRIPTION* description, void* buffer, size_t length) { + SYS_PIPE_SHARED_STATE* shared = PosixPipeShared(description); + if (!shared) return -1; + + for (;;) { + PosixPipeLock(&shared->ReadLock); + uint64_t used = 0; + if (PosixPipeUsed(shared, &used) != 0) { + PosixPipeUnlock(&shared->ReadLock); + return -1; + } + if (used != 0) { + size_t transfer = length < used ? length : (size_t)used; + uint64_t readPosition = shared->ReadPosition; + PosixPipeCopyOut(shared, readPosition, buffer, transfer); + _ReadWriteBarrier(); + shared->ReadPosition = readPosition + transfer; + _ReadWriteBarrier(); + long writeWaiters = shared->WriteWaiters; + PosixPipeUnlock(&shared->ReadLock); + if (writeWaiters != 0) + (void)SysNotifyPipe(description->KHandle); + return (ssize_t)transfer; + } + if (shared->WriterOpen == 0) { + PosixPipeUnlock(&shared->ReadLock); + return 0; + } + if (PosixFdGetStatusFlags(description) & O_NONBLOCK) { + PosixPipeUnlock(&shared->ReadLock); + errno = EAGAIN; + return -1; + } + PosixPipeUnlock(&shared->ReadLock); + + uint32_t status = SysWait(description->KHandle, SYS_WAIT_FLAG_NONE); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } } } -// Opens the requested operation. +// Writes one pipe entirely through the shared user mapping except when blocking/waking. +static ssize_t PosixPipeWrite(POSIX_OPEN_DESCRIPTION* description, const void* buffer, size_t length) { + SYS_PIPE_SHARED_STATE* shared = PosixPipeShared(description); + if (!shared) return -1; + + for (;;) { + PosixPipeLock(&shared->WriteLock); + if (shared->ReaderOpen == 0) { + PosixPipeUnlock(&shared->WriteLock); + errno = EPIPE; + return -1; + } + + uint64_t used = 0; + if (PosixPipeUsed(shared, &used) != 0) { + PosixPipeUnlock(&shared->WriteLock); + return -1; + } + size_t available = SYS_PIPE_BUFFER_CAPACITY - (size_t)used; + size_t transfer = 0; + if (length <= SYS_PIPE_ATOMIC_WRITE_MAX) { + if (available >= length) + transfer = length; + } else if (available != 0) { + transfer = length < available ? length : available; + } + + if (transfer != 0) { + uint64_t writePosition = shared->WritePosition; + PosixPipeCopyIn(shared, writePosition, buffer, transfer); + _ReadWriteBarrier(); + shared->WritePosition = writePosition + transfer; + _ReadWriteBarrier(); + long readWaiters = shared->ReadWaiters; + PosixPipeUnlock(&shared->WriteLock); + if (readWaiters != 0) + (void)SysNotifyPipe(description->KHandle); + return (ssize_t)transfer; + } + + if (PosixFdGetStatusFlags(description) & O_NONBLOCK) { + PosixPipeUnlock(&shared->WriteLock); + errno = EAGAIN; + return -1; + } + PosixPipeUnlock(&shared->WriteLock); + + uint32_t status = SysWait(description->KHandle, SYS_WAIT_FLAG_NONE); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } + } +} + +// Clears the native console display. +__declspec(dllexport) void clearscreen(void) { + (void)NtlConsoleClear(SYS_HANDLE_INVALID, 0x012456); +} + +// Flushes pending native console keyboard input. +__declspec(dllexport) void clearkeyboard(void) { + (void)NtlConsoleFlushInput(SYS_HANDLE_INVALID); +} + +// Opens one native file or synthetic POSIX device and returns a process-local descriptor. __declspec(dllexport) int open(const char* path, int flags, ...) { + if (!path || (flags & O_ACCMODE) == O_ACCMODE) { + errno = EINVAL; + return -1; + } + char native[512]; const char* p = PosixTranslatePath(path, native, sizeof(native)); - uint32_t pid = SysGetCurrentPid(); - FD_TYPE type = FD_FILE; + int statusFlags = flags & (O_ACCMODE | O_APPEND | O_NONBLOCK); + if (strcmp(p, SYNTH_DEVNULL) == 0) + return PosixFdCreate(PosixFdDevNull, SYS_HANDLE_INVALID, statusFlags); + if (strcmp(p, SYNTH_DEVZERO) == 0) + return PosixFdCreate(PosixFdDevZero, SYS_HANDLE_INVALID, statusFlags); - // Synthetic devices. - if (strcmp(p, SYNTH_DEVNULL) == 0) { - int fd = FdAlloc(pid, FD_DEVNULL, 0); - return fd; - } - - if (strcmp(p, SYNTH_DEVZERO) == 0) { - int fd = FdAlloc(pid, FD_DEVZERO, 0); - return fd; - } - - uint32_t kh = 0; - uint32_t access = SYS_FILE_ACCESS_QUERY; + uint32_t kh = SYS_HANDLE_INVALID; + uint32_t access = SYS_FILE_ACCESS_QUERY | SYS_FILE_ACCESS_LOCK; uint32_t disposition = SYS_FILE_OPEN_EXISTING; - if ((flags & O_RDWR) == O_RDWR) + if ((flags & O_ACCMODE) == O_RDWR) access |= SYS_FILE_ACCESS_READ | SYS_FILE_ACCESS_WRITE; - else if (flags & O_WRONLY) + else if ((flags & O_ACCMODE) == O_WRONLY) access |= SYS_FILE_ACCESS_WRITE; else access |= SYS_FILE_ACCESS_READ; @@ -62,166 +232,323 @@ __declspec(dllexport) int open(const char* path, int flags, ...) { else if (flags & O_TRUNC) disposition = SYS_FILE_TRUNCATE_EXISTING; - uint32_t s = NtlOpenFile(SysSvFromCstr(p), access, disposition, - SYS_FILE_FLAG_NONE, &kh); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } - - int fd = FdAlloc(pid, type, kh); - if (fd < 0) { - SysCloseHandle(kh); // FdAlloc already set errno + uint32_t status = NtlOpenFile(SysSvFromCstr(p), access, disposition, + SYS_FILE_FLAG_NONE, &kh); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); return -1; } + + int fd = PosixFdCreate(PosixFdFile, kh, statusFlags); + if (fd < 0) + SysCloseHandle(kh); return fd; } -// Returns the requested value. +// Reads bytes from one descriptor through its stable open-description reference. __declspec(dllexport) ssize_t read(int fd, void* buf, size_t count) { - if (count == 0) return 0; - uint32_t pid = SysGetCurrentPid(); - FD_ENTRY* e = FdGet(pid, fd); - if (!e) return -1; + if (count == 0) + return 0; + if (!buf) { + errno = EINVAL; + return -1; + } - switch (e->Type) { - case FD_CONSOLE_R: { - // Read up to count chars from keyboard, blocking per char. + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(fd); + if (!description) + return -1; + if (!PosixDescriptionReadable(description)) { + PosixFdRelease(description); + errno = EBADF; + return -1; + } + + ssize_t result = -1; + switch (description->Type) { + case PosixFdConsoleRead: { char* dst = (char*)buf; + result = (ssize_t)count; for (size_t i = 0; i < count; i++) { - uint32_t s = SysReadKeyboardChar(&dst[i]); - if (s != NTLSTATUS_SUCCESS) { errno = PosixStatusToErrno(s); return -1; } - if (dst[i] == '\n' || dst[i] == '\r') return (ssize_t)(i + 1); + uint32_t status = NtlConsoleReadCharacter(SYS_HANDLE_INVALID, &dst[i]); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + result = -1; + break; + } + if (dst[i] == '\n' || dst[i] == '\r') { + result = (ssize_t)(i + 1u); + break; + } } - return (ssize_t)count; + break; } - case FD_DEVNULL: - return 0; - case FD_DEVZERO: + case PosixFdDevNull: + result = 0; + break; + case PosixFdDevZero: memset(buf, 0, count); - return (ssize_t)count; - case FD_FILE: { + result = (ssize_t)count; + break; + case PosixFdFile: { size_t got = 0; - SYS_FILE_IO_REQUEST req = SysMakeFileIoRequest(e->KHandle, buf, count, &got); - uint32_t s = SysReadFile(&req); - if (s != 0 && got == 0) { errno = PosixStatusToErrno(s); return -1; } - return (ssize_t)got; + SYS_FILE_IO_REQUEST request = + SysMakeFileIoRequest(description->KHandle, buf, count, &got); + uint32_t status = SysReadFile(&request); + if (status != NTLSTATUS_SUCCESS && got == 0) + errno = PosixStatusToErrno(status); + else + result = (ssize_t)got; + break; } - case FD_SOCKET: { + case PosixFdPipeRead: + result = PosixPipeRead(description, buf, count); + break; + case PosixFdSocket: { size_t got = 0; SYS_SOCKET_RECV_REQUEST request = { - sizeof(SYS_SOCKET_RECV_REQUEST), e->KHandle, 0, + sizeof(SYS_SOCKET_RECV_REQUEST), description->KHandle, 0, buf, count, &got }; - uint32_t s = SysRecvSocket(&request); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } - return (ssize_t)got; + uint32_t status = SysRecvSocket(&request); + if (status != NTLSTATUS_SUCCESS) + errno = PosixStatusToErrno(status); + else + result = (ssize_t)got; + break; } default: errno = EBADF; - return -1; + break; } + + PosixFdRelease(description); + return result; } -// Updates the requested value. +// Writes bytes through one descriptor's shared open description. __declspec(dllexport) ssize_t write(int fd, const void* buf, size_t count) { - if (count == 0) return 0; - uint32_t pid = SysGetCurrentPid(); - FD_ENTRY* e = FdGet(pid, fd); - if (!e) return -1; + if (count == 0) + return 0; + if (!buf) { + errno = EINVAL; + return -1; + } - switch (e->Type) { - case FD_CONSOLE_W: { - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromParts((const char*)buf, count) }; - SysWriteConsole(&req); - return (ssize_t)count; - } - case FD_DEVNULL: - case FD_DEVZERO: - return (ssize_t)count; - case FD_FILE: { + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(fd); + if (!description) + return -1; + if (!PosixDescriptionWritable(description)) { + PosixFdRelease(description); + errno = EBADF; + return -1; + } + + ssize_t result = -1; + switch (description->Type) { + case PosixFdConsoleWrite: { size_t written = 0; - SYS_FILE_IO_REQUEST req = SysMakeFileIoRequest(e->KHandle, buf, count, &written); - uint32_t s = SysWriteFile(&req); - if (s != 0 && written == 0) { errno = PosixStatusToErrno(s); return -1; } - return (ssize_t)written; + uint32_t status = NtlConsoleWrite(SYS_HANDLE_INVALID, buf, count, &written); + if (status != NTLSTATUS_SUCCESS && written == 0) + errno = PosixStatusToErrno(status); + else + result = (ssize_t)written; + break; } - case FD_SOCKET: { + case PosixFdDevNull: + case PosixFdDevZero: + result = (ssize_t)count; + break; + case PosixFdFile: { + size_t written = 0; + SYS_FILE_IO_REQUEST request = + SysMakeFileIoRequest(description->KHandle, (void*)buf, count, &written); + uint32_t status = SysWriteFile(&request); + if (status != NTLSTATUS_SUCCESS && written == 0) + errno = PosixStatusToErrno(status); + else + result = (ssize_t)written; + break; + } + case PosixFdPipeWrite: + result = PosixPipeWrite(description, buf, count); + break; + case PosixFdSocket: { size_t sent = 0; SYS_SOCKET_SEND_REQUEST request = { - sizeof(SYS_SOCKET_SEND_REQUEST), e->KHandle, 0, + sizeof(SYS_SOCKET_SEND_REQUEST), description->KHandle, 0, buf, count, &sent }; - uint32_t s = SysSendSocket(&request); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } - return (ssize_t)sent; + uint32_t status = SysSendSocket(&request); + if (status != NTLSTATUS_SUCCESS) + errno = PosixStatusToErrno(status); + else + result = (ssize_t)sent; + break; } default: errno = EBADF; - return -1; + break; } + + PosixFdRelease(description); + return result; } -// Releases the requested object. +// Closes one descriptor without invalidating concurrent open-description users. __declspec(dllexport) int close(int fd) { - uint32_t pid = SysGetCurrentPid(); - FD_ENTRY* e = FdGet(pid, fd); - if (!e) return -1; - if (e->Type == FD_FILE || e->Type == FD_SOCKET) - SysCloseHandle(e->KHandle); - FdFree(pid, fd); + return PosixFdClose(fd); +} + +// Creates one anonymous pipe whose backing and master endpoint capabilities are owned by posixsrv. +__declspec(dllexport) int pipe(int pipefd[2]) { + if (!pipefd) { + errno = EINVAL; + return -1; + } + + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_CREATE_PIPE; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + + SYS_HANDLE handles[2] = { SYS_HANDLE_INVALID, SYS_HANDLE_INVALID }; + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeCallServerExchange( + &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, handles, 2u, 2u, &reply); + if (!KSUCCESS(status) || !KSUCCESS(reply.Status)) { + for (uint32_t i = 0; i < 2u; ++i) { + if (handles[i] != SYS_HANDLE_INVALID) + (void)SysCloseHandle(handles[i]); + } + errno = PosixStatusToErrno(KSUCCESS(status) ? reply.Status : status); + return -1; + } + + NTL_POSIX_SHARED_OPEN_DESCRIPTION* readShared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)(uintptr_t)reply.SharedOpenDescription; + NTL_POSIX_SHARED_OPEN_DESCRIPTION* writeShared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)(uintptr_t)reply.SharedOpenDescription2; + SYS_PIPE_SHARED_STATE* pipeShared = readShared ? + (SYS_PIPE_SHARED_STATE*)(uintptr_t)readShared->SharedObjectData : + (SYS_PIPE_SHARED_STATE*)0; + int valid = handles[0] != SYS_HANDLE_INVALID && handles[1] != SYS_HANDLE_INVALID && + reply.OpenDescriptionId != 0 && reply.OpenDescriptionId2 != 0 && + reply.OpenDescriptionId != reply.OpenDescriptionId2 && + reply.OpenDescriptionType == NTL_POSIX_OPEN_TYPE_PIPE_READ && + reply.OpenDescriptionType2 == NTL_POSIX_OPEN_TYPE_PIPE_WRITE && + reply.OpenStatusFlags == NTL_POSIX_OPEN_STATUS_RDONLY && + reply.OpenStatusFlags2 == NTL_POSIX_OPEN_STATUS_WRONLY && + readShared && writeShared && + readShared->Magic == NTL_POSIX_SHARED_OPEN_MAGIC && + writeShared->Magic == NTL_POSIX_SHARED_OPEN_MAGIC && + readShared->Version == NTL_POSIX_SHARED_OPEN_VERSION && + writeShared->Version == NTL_POSIX_SHARED_OPEN_VERSION && + readShared->Type == NTL_POSIX_OPEN_TYPE_PIPE_READ && + writeShared->Type == NTL_POSIX_OPEN_TYPE_PIPE_WRITE && + readShared->DescriptionId == reply.OpenDescriptionId && + writeShared->DescriptionId == reply.OpenDescriptionId2 && + readShared->Reserved == 0 && writeShared->Reserved == 0 && + readShared->Reserved2 == 0 && writeShared->Reserved2 == 0 && + readShared->SharedObjectData != 0 && + readShared->SharedObjectData == writeShared->SharedObjectData && + pipeShared && pipeShared->Magic == SYS_PIPE_SHARED_MAGIC && + pipeShared->Version == SYS_PIPE_SHARED_VERSION && + pipeShared->Capacity == SYS_PIPE_BUFFER_CAPACITY && + pipeShared->HeaderBytes == SYS_PIPE_SHARED_HEADER_BYTES; + if (!valid) { + (void)SysCloseHandle(handles[0]); + (void)SysCloseHandle(handles[1]); + PosixFdReleaseServerDescription(reply.OpenDescriptionId); + PosixFdReleaseServerDescription(reply.OpenDescriptionId2); + errno = EIO; + return -1; + } + + int readFd = PosixFdImport(PosixFdPipeRead, handles[0], reply.OpenDescriptionId, + readShared, 0); + handles[0] = SYS_HANDLE_INVALID; + if (readFd < 0) { + (void)SysCloseHandle(handles[1]); + PosixFdReleaseServerDescription(reply.OpenDescriptionId2); + return -1; + } + + int writeFd = PosixFdImport(PosixFdPipeWrite, handles[1], reply.OpenDescriptionId2, + writeShared, 0); + handles[1] = SYS_HANDLE_INVALID; + if (writeFd < 0) { + (void)PosixFdClose(readFd); + return -1; + } + + pipefd[0] = readFd; + pipefd[1] = writeFd; return 0; } -// Implements the unlink operation. +// Deletes one translated native path. __declspec(dllexport) int unlink(const char* path) { if (!path) { errno = EINVAL; return -1; } char native[512]; const char* p = PosixTranslatePath(path, native, sizeof(native)); - if (strcmp(p, SYNTH_DEVNULL) == 0 || strcmp(p, SYNTH_DEVZERO) == 0) { errno = EINVAL; return -1; } - uint32_t s = NtlDeleteFile(SysSvFromCstr(p)); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + uint32_t status = NtlDeleteFile(SysSvFromCstr(p)); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return 0; } -// Implements the lseek operation. +// Repositions the shared file offset owned by an open description. __declspec(dllexport) off_t lseek(int fd, off_t offset, int whence) { - uint32_t pid = SysGetCurrentPid(); - FD_ENTRY* e = FdGet(pid, fd); - if (!e || e->Type != FD_FILE) { errno = (e ? ESPIPE : EBADF); return -1; } - - int64_t target = 0; - switch (whence) { - case SEEK_SET: - target = (int64_t)offset; - break; - case SEEK_CUR: { - uint64_t cur = 0; - uint32_t s = SysGetFilePos(e->KHandle, &cur); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } - if (cur > 0x7FFFFFFFULL) { errno = EINVAL; return -1; } - target = (int64_t)cur + (int64_t)offset; - break; - } - case SEEK_END: { - uint64_t size = 0; - uint32_t s = SysGetFileSize(e->KHandle, &size); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } - if (size > 0x7FFFFFFFULL) { errno = EINVAL; return -1; } - target = (int64_t)size + (int64_t)offset; - break; - } - default: - errno = EINVAL; - return -1; + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(fd); + if (!description) + return -1; + if (description->Type != PosixFdFile) { + PosixFdRelease(description); + errno = ESPIPE; + return -1; } - if (target < 0 || target > 0x7FFFFFFFLL) { errno = EINVAL; return -1; } + int64_t target = 0; + uint32_t status = NTLSTATUS_SUCCESS; + if (whence == SEEK_SET) { + target = (int64_t)offset; + } else if (whence == SEEK_CUR) { + uint64_t current = 0; + status = SysGetFilePos(description->KHandle, ¤t); + if (status == NTLSTATUS_SUCCESS && current <= 0x7FFFFFFFULL) + target = (int64_t)current + (int64_t)offset; + else if (status == NTLSTATUS_SUCCESS) + status = NTLSTATUS_INVALID_PARAMETER; + } else if (whence == SEEK_END) { + uint64_t size = 0; + status = SysGetFileSize(description->KHandle, &size); + if (status == NTLSTATUS_SUCCESS && size <= 0x7FFFFFFFULL) + target = (int64_t)size + (int64_t)offset; + else if (status == NTLSTATUS_SUCCESS) + status = NTLSTATUS_INVALID_PARAMETER; + } else { + status = NTLSTATUS_INVALID_PARAMETER; + } - uint32_t s = SysSeekFile(e->KHandle, (uint64_t)target); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + if (status == NTLSTATUS_SUCCESS && (target < 0 || target > 0x7FFFFFFFLL)) + status = NTLSTATUS_INVALID_PARAMETER; + if (status == NTLSTATUS_SUCCESS) + status = SysSeekFile(description->KHandle, (uint64_t)target); + + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return (off_t)target; } diff --git a/code/user/personalities/posix/posix_mman.c b/code/user/personalities/posix/posix_mman.c index 1fafff4..973be7f 100644 --- a/code/user/personalities/posix/posix_mman.c +++ b/code/user/personalities/posix/posix_mman.c @@ -3,7 +3,7 @@ #include #include #include -#include "posix.h" +#include "posix_internal.h" #include "posix_fd.h" static uint32_t PosixProtToSysMapAccess(int prot, int flags) { @@ -31,16 +31,17 @@ __declspec(dllexport) void* mmap(void* addr, size_t length, int prot, return MAP_FAILED; } - uint32_t pid = SysGetCurrentPid(); - FD_ENTRY* e = FdGet(pid, fd); - if (!e) return MAP_FAILED; - if (e->Type != FD_FILE) { + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(fd); + if (!description) return MAP_FAILED; + if (description->Type != PosixFdFile) { + PosixFdRelease(description); errno = EBADF; return MAP_FAILED; } uint64_t maxSize = (uint64_t)offset + (uint64_t)length; if (maxSize < (uint64_t)offset) { + PosixFdRelease(description); errno = EINVAL; return MAP_FAILED; } @@ -51,10 +52,11 @@ __declspec(dllexport) void* mmap(void* addr, size_t length, int prot, uint32_t mapping = 0; SYS_CREATE_FILE_MAPPING_REQUEST createMapping = { - sizeof(SYS_CREATE_FILE_MAPPING_REQUEST), e->KHandle, protect, + sizeof(SYS_CREATE_FILE_MAPPING_REQUEST), description->KHandle, protect, SYS_MAP_FLAG_NONE, maxSize, &mapping }; uint32_t s = SysCreateFileMapping(&createMapping); + PosixFdRelease(description); if (s != NTLSTATUS_SUCCESS) { errno = PosixStatusToErrno(s); return MAP_FAILED; diff --git a/code/user/personalities/posix/posix_proc.c b/code/user/personalities/posix/posix_proc.c index 75d7e17..ee56003 100644 --- a/code/user/personalities/posix/posix_proc.c +++ b/code/user/personalities/posix/posix_proc.c @@ -5,15 +5,13 @@ #include #include #include -#include "posix.h" +#include "posix_internal.h" #include "posix_path.h" -static SYS_PEB* gProcessPeb = NULL; - -// Implements the peb operation. +// Returns the native PEB retained by the process-local POSIX runtime state. static SYS_PEB* PosixPeb(void) { - if (!gProcessPeb) gProcessPeb = SysGetPeb(); - return gProcessPeb; + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + return process ? process->Peb : (SYS_PEB*)0; } // Implements the posix get command line operation. @@ -23,40 +21,49 @@ __declspec(dllexport) const char* posix_get_command_line(void) { return peb->CommandLine.Data; } -// pid -> proc handle table (user-mode side, for waitpid). -#define PROC_TABLE_SIZE 32 -typedef struct { - pid_t pid; - uint32_t handle; - bool used; -} ProcEntry; +// Records one spawn association in process-local POSIX state. +static void ProcTableInsert(pid_t pid, SYS_HANDLE handle, uint64_t inheritanceToken) { + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process) { + SysCloseHandle(handle); + return; + } -static ProcEntry gProcTable[PROC_TABLE_SIZE]; - -// Implements the proc Table Insert operation. -static void ProcTableInsert(pid_t pid, uint32_t handle) { - for (int i = 0; i < PROC_TABLE_SIZE; i++) { - if (!gProcTable[i].used) { - gProcTable[i] = (ProcEntry){ pid, handle, true }; + for (uint32_t i = 0; i < POSIX_SPAWN_TABLE_SIZE; i++) { + if (!process->SpawnTable[i].Used) { + process->SpawnTable[i] = (POSIX_SPAWN_ENTRY){ pid, handle, inheritanceToken, true }; return; } } SysCloseHandle(handle); + if (inheritanceToken != 0) PosixFdCancelInheritanceSnapshot(inheritanceToken); } -// Implements the proc Table Find operation. -static uint32_t ProcTableFind(pid_t pid) { - for (int i = 0; i < PROC_TABLE_SIZE; i++) - if (gProcTable[i].used && gProcTable[i].pid == pid) - return gProcTable[i].handle; - return 0; +// Finds a spawned process handle retained by the current POSIX process. +static SYS_HANDLE ProcTableFind(pid_t pid) { + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process) return SYS_HANDLE_INVALID; + + for (uint32_t i = 0; i < POSIX_SPAWN_TABLE_SIZE; i++) { + if (process->SpawnTable[i].Used && process->SpawnTable[i].Pid == pid) + return process->SpawnTable[i].Handle; + } + return SYS_HANDLE_INVALID; } -// Implements the proc Table Remove operation. +// Removes one spawn association from process-local POSIX state. static void ProcTableRemove(pid_t pid) { - for (int i = 0; i < PROC_TABLE_SIZE; i++) - if (gProcTable[i].used && gProcTable[i].pid == pid) - gProcTable[i].used = false; + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process) return; + + for (uint32_t i = 0; i < POSIX_SPAWN_TABLE_SIZE; i++) { + if (process->SpawnTable[i].Used && process->SpawnTable[i].Pid == pid) { + uint64_t inheritanceToken = process->SpawnTable[i].FdInheritanceToken; + memset(&process->SpawnTable[i], 0, sizeof(process->SpawnTable[i])); + if (inheritanceToken != 0) PosixFdCancelInheritanceSnapshot(inheritanceToken); + return; + } + } } // Build a flat Windows-style cmdline from a NULL-terminated argv array. @@ -82,7 +89,10 @@ static void BuildCmdline(char* const argv[], char* out, size_t outSize) { // POSIX API. __declspec(dllexport) pid_t getpid(void) { - return (pid_t)SysGetCurrentPid(); + POSIX_THREAD_STATE* thread = PosixRuntimeThreadState(); + if (!thread || !thread->LogicalProcess || thread->LogicalProcess->PosixPid == 0) + return (pid_t)-1; + return (pid_t)thread->LogicalProcess->PosixPid; } __declspec(dllexport) int posix_spawn(pid_t* pidOut, @@ -96,12 +106,12 @@ __declspec(dllexport) int posix_spawn(pid_t* pidOut, char native[512]; uint32_t pathStatus = PosixProcessPath(path, native, sizeof(native)); - if (pathStatus != NTLSTATUS_SUCCESS) { errno = ENOENT; return -1; } + if (pathStatus != NTLSTATUS_SUCCESS) { errno = PosixStatusToErrno(pathStatus); return -1; } SYS_HANDLE imageFile = SYS_HANDLE_INVALID; pathStatus = NtlOpenFile(SysSvFromCstr(native), SYS_FILE_ACCESS_READ | SYS_FILE_ACCESS_QUERY, SYS_FILE_OPEN_EXISTING, SYS_FILE_FLAG_NONE, &imageFile); - if (pathStatus != NTLSTATUS_SUCCESS) { errno = ENOENT; return -1; } + if (pathStatus != NTLSTATUS_SUCCESS) { errno = PosixStatusToErrno(pathStatus); return -1; } char cmdline[512]; BuildCmdline(argv, cmdline, sizeof(cmdline)); @@ -114,6 +124,22 @@ __declspec(dllexport) int posix_spawn(pid_t* pidOut, cmdline[len] = '\0'; } + uint64_t inheritanceToken = 0; + uint32_t snapshotStatus = + PosixFdCreateInheritanceSnapshot(POSIX_FD_INHERIT_SKIP_CLOEXEC, &inheritanceToken); + if (snapshotStatus != NTLSTATUS_SUCCESS) { + (void)SysCloseHandle(imageFile); + errno = PosixStatusToErrno(snapshotStatus); + return -1; + } + + NTL_POSIX_BOOTSTRAP_DATA bootstrap; + memset(&bootstrap, 0, sizeof(bootstrap)); + bootstrap.Magic = NTL_POSIX_BOOTSTRAP_MAGIC; + bootstrap.Version = NTL_POSIX_BOOTSTRAP_VERSION; + bootstrap.Size = sizeof(bootstrap); + bootstrap.FdInheritanceToken = inheritanceToken; + uint32_t procHandle = 0; NTL_CREATE_SYSTEM_PROCESS_REQUEST req; memset(&req, 0, sizeof(req)); @@ -124,14 +150,29 @@ __declspec(dllexport) int posix_spawn(pid_t* pidOut, req.CommandLine = SysSvFromCstr(cmdline); req.SecurityContext = SYS_SECURITY_CONTEXT_CURRENT; req.ResolveModuleName = PosixResolveModuleName; + req.StartupData = &bootstrap; + req.StartupDataSize = sizeof(bootstrap); req.ProcessHandleOut = &procHandle; uint32_t s = NtlCreateSystemProcess(&req); if (imageFile != SYS_HANDLE_INVALID) (void)SysCloseHandle(imageFile); - if (s != 0) { errno = ENOENT; return -1; } + if (s != NTLSTATUS_SUCCESS) { + PosixFdCancelInheritanceSnapshot(inheritanceToken); + errno = PosixStatusToErrno(s); + return -1; + } + + KSTATUS armStatus = PosixFdArmInheritanceSnapshot(inheritanceToken, procHandle); + if (!KSUCCESS(armStatus) && armStatus != NTLSTATUS_NOT_FOUND) { + (void)SysKillProcess(procHandle, (int32_t)NTLSTATUS_PROCESS_KILLED); + (void)SysCloseHandle(procHandle); + PosixFdCancelInheritanceSnapshot(inheritanceToken); + errno = PosixStatusToErrno(armStatus); + return -1; + } uint32_t kpid = 0; SysGetProcessPid(procHandle, &kpid); - ProcTableInsert((pid_t)kpid, procHandle); + ProcTableInsert((pid_t)kpid, procHandle, inheritanceToken); if (pidOut) *pidOut = (pid_t)kpid; return 0; } @@ -140,8 +181,8 @@ __declspec(dllexport) int posix_spawn(pid_t* pidOut, __declspec(dllexport) pid_t waitpid(pid_t pid, int* status, int options) { (void)options; - uint32_t handle = ProcTableFind(pid); - if (!handle) { errno = ESRCH; return -1; } + SYS_HANDLE handle = ProcTableFind(pid); + if (handle == SYS_HANDLE_INVALID) { errno = ESRCH; return -1; } SysWait(handle, SYS_WAIT_FLAG_NONE); @@ -157,4 +198,259 @@ __declspec(dllexport) pid_t waitpid(pid_t pid, int* status, int options) { // Implements the exit operation. __declspec(dllexport) void exit(int status) { SysExitProcess(status); + while (true) {} +} + +// Initializes one ordinary shared-attribute request. +static void PosixProcessRequest(NTL_POSIX_REQUEST* request, uint32_t operation) { + memset(request, 0, sizeof(*request)); + request->Size = sizeof(*request); + request->Version = NTL_POSIX_PROTOCOL_VERSION; + request->Operation = operation; + request->Flags = NTL_POSIX_REQUEST_FLAG_NONE; +} + +// Executes one shared-attribute request and converts transport/server failure to errno. +static int PosixProcessCall(NTL_POSIX_REQUEST* request, NTL_POSIX_REPLY* reply) { + KSTATUS status = PosixRuntimeCallServer(request, reply); + if (!KSUCCESS(status)) { + errno = PosixStatusToErrno(status); + return -1; + } + if (!KSUCCESS(reply->Status)) { + errno = PosixStatusToErrno(reply->Status); + return -1; + } + return 0; +} + +// Executes one operation whose permission failures are POSIX EPERM rather than EACCES. +static int PosixProcessCallPermission(NTL_POSIX_REQUEST* request, NTL_POSIX_REPLY* reply) { + KSTATUS status = PosixRuntimeCallServer(request, reply); + if (!KSUCCESS(status)) { + errno = PosixStatusToErrno(status); + return -1; + } + if (!KSUCCESS(reply->Status)) { + errno = reply->Status == NTLSTATUS_ACCESS_DENIED ? EPERM : PosixStatusToErrno(reply->Status); + return -1; + } + return 0; +} + +// Returns the real POSIX user id owned by posixsrv. +__declspec(dllexport) uid_t getuid(void) { + NTL_POSIX_REPLY reply; + if (!KSUCCESS(PosixRuntimeGetProcessAttributes(&reply))) return (uid_t)-1; + return (uid_t)reply.UserId; +} + +// Returns the effective POSIX user id owned by posixsrv. +__declspec(dllexport) uid_t geteuid(void) { + NTL_POSIX_REPLY reply; + if (!KSUCCESS(PosixRuntimeGetProcessAttributes(&reply))) return (uid_t)-1; + return (uid_t)reply.EffectiveUserId; +} + +// Returns the real POSIX group id owned by posixsrv. +__declspec(dllexport) gid_t getgid(void) { + NTL_POSIX_REPLY reply; + if (!KSUCCESS(PosixRuntimeGetProcessAttributes(&reply))) return (gid_t)-1; + return (gid_t)reply.GroupId; +} + +// Returns the effective POSIX group id owned by posixsrv. +__declspec(dllexport) gid_t getegid(void) { + NTL_POSIX_REPLY reply; + if (!KSUCCESS(PosixRuntimeGetProcessAttributes(&reply))) return (gid_t)-1; + return (gid_t)reply.EffectiveGroupId; +} + +// Changes synthetic user credentials; uid zero is the only credential-changing authority. +__declspec(dllexport) int setuid(uid_t uid) { + NTL_POSIX_REQUEST request; + NTL_POSIX_REPLY reply; + PosixProcessRequest(&request, NTL_POSIX_OP_SET_UID); + request.UserId = (uint32_t)uid; + return PosixProcessCallPermission(&request, &reply); +} + +// Changes synthetic group credentials; effective uid zero is the authority. +__declspec(dllexport) int setgid(gid_t gid) { + NTL_POSIX_REQUEST request; + NTL_POSIX_REPLY reply; + PosixProcessRequest(&request, NTL_POSIX_OP_SET_GID); + request.GroupId = (uint32_t)gid; + return PosixProcessCallPermission(&request, &reply); +} + +// Returns the supplementary group list attached to the current POSIX process. +__declspec(dllexport) int getgroups(int size, gid_t list[]) { + if (size < 0 || (size != 0 && !list)) { + errno = EINVAL; + return -1; + } + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeGetProcessAttributes(&reply); + if (!KSUCCESS(status)) { + errno = PosixStatusToErrno(status); + return -1; + } + if (reply.SupplementaryGroupCount > NTL_POSIX_SUPPLEMENTARY_GROUP_MAX) { + errno = EIO; + return -1; + } + if (size == 0) return (int)reply.SupplementaryGroupCount; + if ((uint32_t)size < reply.SupplementaryGroupCount) { + errno = EINVAL; + return -1; + } + for (uint32_t i = 0; i < reply.SupplementaryGroupCount; ++i) + list[i] = (gid_t)reply.SupplementaryGroups[i]; + return (int)reply.SupplementaryGroupCount; +} + +// Returns the login identity fixed when the POSIX process record is created. +__declspec(dllexport) char* getlogin(void) { + POSIX_THREAD_STATE* thread = PosixRuntimeThreadState(); + if (!thread) return (char*)0; + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeGetProcessAttributes(&reply); + if (!KSUCCESS(status)) { + errno = PosixStatusToErrno(status); + return (char*)0; + } + if (reply.NameLength == 0 || reply.NameLength > NTL_POSIX_IDENTITY_NAME_MAX || + reply.Name[reply.NameLength] != '\0') { + errno = ENOENT; + return (char*)0; + } + memcpy(thread->LoginName, reply.Name, reply.NameLength + 1u); + return thread->LoginName; +} + +// Returns the current POSIX process-group id. +__declspec(dllexport) pid_t getpgrp(void) { + NTL_POSIX_REPLY reply; + KSTATUS status = PosixRuntimeGetProcessAttributes(&reply); + if (!KSUCCESS(status)) { + errno = PosixStatusToErrno(status); + return (pid_t)-1; + } + return (pid_t)reply.ProcessGroupId; +} + +// Changes process-group membership under posixsrv's parent/session invariants. +__declspec(dllexport) int setpgid(pid_t pid, pid_t pgid) { + if (pid < 0 || pgid < 0) { + errno = EINVAL; + return -1; + } + NTL_POSIX_REQUEST request; + NTL_POSIX_REPLY reply; + PosixProcessRequest(&request, NTL_POSIX_OP_SET_PROCESS_GROUP); + request.TargetPosixPid = (uint32_t)pid; + request.ProcessGroupId = (uint32_t)pgid; + return PosixProcessCallPermission(&request, &reply); +} + +// Creates a new POSIX session and process group when the caller is not a group leader. +__declspec(dllexport) pid_t setsid(void) { + NTL_POSIX_REQUEST request; + NTL_POSIX_REPLY reply; + PosixProcessRequest(&request, NTL_POSIX_OP_CREATE_SESSION); + if (PosixProcessCallPermission(&request, &reply) != 0) return (pid_t)-1; + return (pid_t)reply.SessionId; +} + +// Performs one passwd lookup and builds a thread-local POSIX result view. +static struct passwd* PosixLookupUser(uint32_t lookupKind, uid_t uid, const char* name) { + POSIX_THREAD_STATE* thread = PosixRuntimeThreadState(); + if (!thread) return (struct passwd*)0; + NTL_POSIX_REQUEST request; + NTL_POSIX_REPLY reply; + PosixProcessRequest(&request, NTL_POSIX_OP_LOOKUP_USER); + request.LookupKind = lookupKind; + request.UserId = (uint32_t)uid; + if (lookupKind == NTL_POSIX_LOOKUP_BY_NAME) { + if (!name) { errno = EINVAL; return (struct passwd*)0; } + size_t length = strlen(name); + if (length == 0 || length > NTL_POSIX_IDENTITY_NAME_MAX) { + errno = ENOENT; + return (struct passwd*)0; + } + memcpy(request.Name, name, length + 1u); + request.NameLength = (uint32_t)length; + } + if (PosixProcessCall(&request, &reply) != 0) return (struct passwd*)0; + if (reply.NameLength == 0 || reply.NameLength > NTL_POSIX_IDENTITY_NAME_MAX || + reply.HomeLength > NTL_POSIX_IDENTITY_HOME_MAX || + reply.ShellLength > NTL_POSIX_IDENTITY_SHELL_MAX || + reply.Name[reply.NameLength] != '\0' || reply.Home[reply.HomeLength] != '\0' || + reply.Shell[reply.ShellLength] != '\0') { + errno = EIO; + return (struct passwd*)0; + } + memcpy(thread->PasswordName, reply.Name, reply.NameLength + 1u); + memcpy(thread->PasswordHome, reply.Home, reply.HomeLength + 1u); + memcpy(thread->PasswordShell, reply.Shell, reply.ShellLength + 1u); + thread->PasswordResult.pw_name = thread->PasswordName; + thread->PasswordResult.pw_uid = (uid_t)reply.UserId; + thread->PasswordResult.pw_gid = (gid_t)reply.GroupId; + thread->PasswordResult.pw_dir = thread->PasswordHome; + thread->PasswordResult.pw_shell = thread->PasswordShell; + return &thread->PasswordResult; +} + +// Looks up one synthesized passwd entry by name. +__declspec(dllexport) struct passwd* getpwnam(const char* name) { + return PosixLookupUser(NTL_POSIX_LOOKUP_BY_NAME, 0, name); +} + +// Looks up one synthesized passwd entry by uid. +__declspec(dllexport) struct passwd* getpwuid(uid_t uid) { + return PosixLookupUser(NTL_POSIX_LOOKUP_BY_ID, uid, (const char*)0); +} + +// Performs one group lookup and builds a thread-local POSIX result view. +static struct group* PosixLookupGroup(uint32_t lookupKind, gid_t gid, const char* name) { + POSIX_THREAD_STATE* thread = PosixRuntimeThreadState(); + if (!thread) return (struct group*)0; + NTL_POSIX_REQUEST request; + NTL_POSIX_REPLY reply; + PosixProcessRequest(&request, NTL_POSIX_OP_LOOKUP_GROUP); + request.LookupKind = lookupKind; + request.GroupId = (uint32_t)gid; + if (lookupKind == NTL_POSIX_LOOKUP_BY_NAME) { + if (!name) { errno = EINVAL; return (struct group*)0; } + size_t length = strlen(name); + if (length == 0 || length > NTL_POSIX_IDENTITY_NAME_MAX) { + errno = ENOENT; + return (struct group*)0; + } + memcpy(request.Name, name, length + 1u); + request.NameLength = (uint32_t)length; + } + if (PosixProcessCall(&request, &reply) != 0) return (struct group*)0; + if (reply.NameLength == 0 || reply.NameLength > NTL_POSIX_IDENTITY_NAME_MAX || + reply.Name[reply.NameLength] != '\0') { + errno = EIO; + return (struct group*)0; + } + memcpy(thread->GroupName, reply.Name, reply.NameLength + 1u); + thread->GroupMembers[0] = (char*)0; + thread->GroupResult.gr_name = thread->GroupName; + thread->GroupResult.gr_gid = (gid_t)reply.GroupId; + thread->GroupResult.gr_mem = thread->GroupMembers; + return &thread->GroupResult; +} + +// Looks up one synthesized group entry by name. +__declspec(dllexport) struct group* getgrnam(const char* name) { + return PosixLookupGroup(NTL_POSIX_LOOKUP_BY_NAME, 0, name); +} + +// Looks up one synthesized group entry by gid. +__declspec(dllexport) struct group* getgrgid(gid_t gid) { + return PosixLookupGroup(NTL_POSIX_LOOKUP_BY_ID, gid, (const char*)0); } diff --git a/code/user/personalities/posix/posix_runtime.c b/code/user/personalities/posix/posix_runtime.c new file mode 100644 index 0000000..2af94b4 --- /dev/null +++ b/code/user/personalities/posix/posix_runtime.c @@ -0,0 +1,438 @@ +// POSIX personality process/thread runtime state backed by the native TEB extension slot. + +#include +#include +#include +#include +#include +#include +#include +#include "posix_internal.h" +#include "posix_fd.h" + +static POSIX_PROCESS_STATE* gPosixProcessState; +static int gEmergencyErrno; + +// Resolves the published POSIX service and opens one private client endpoint. +static KSTATUS PosixRuntimeConnectServer(SYS_HANDLE* endpointOut) { + if (!endpointOut) return NTLSTATUS_INVALID_PARAMETER; + *endpointOut = SYS_HANDLE_INVALID; + + SYS_HANDLE resource = SYS_HANDLE_INVALID; + KSTATUS status = NtlResourceConnect(&resource); + if (!KSUCCESS(status)) return status; + + SYS_HANDLE listener = SYS_HANDLE_INVALID; + status = NtlResourceLookup(resource, SYS_SV_LITERAL(NTL_POSIX_SERVICE_PATH), + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT, &listener); + (void)NtlResourceClose(resource); + if (!KSUCCESS(status)) return status; + + SYS_CONNECT_PORT_REQUEST connect; + memset(&connect, 0, sizeof(connect)); + connect.Size = sizeof(connect); + connect.ListenerHandle = listener; + connect.Flags = SYS_PORT_CONNECT_FLAG_NONE; + connect.EndpointHandleOut = endpointOut; + status = SysConnectPort(&connect); + (void)SysCloseHandle(listener); + return status; +} + +// Calls one fixed-size posixsrv request over an explicitly supplied endpoint. +static KSTATUS PosixRuntimeCallEndpoint(SYS_HANDLE endpoint, + const NTL_POSIX_REQUEST* request, + const SYS_PORT_HANDLE_TRANSFER* requestHandles, + uint32_t requestHandleCount, + SYS_HANDLE* replyHandlesOut, + uint32_t replyHandleCapacity, + uint32_t expectedReplyHandleCount, + uint32_t* actualReplyHandleCountOut, + NTL_POSIX_REPLY* replyOut) { + if (endpoint == SYS_HANDLE_INVALID || !request || !replyOut || + (expectedReplyHandleCount != UINT32_MAX && expectedReplyHandleCount > replyHandleCapacity) || + (replyHandleCapacity != 0 && !replyHandlesOut)) + return NTLSTATUS_INVALID_PARAMETER; + memset(replyOut, 0, sizeof(*replyOut)); + if (replyHandlesOut) { + for (uint32_t i = 0; i < replyHandleCapacity; ++i) + replyHandlesOut[i] = SYS_HANDLE_INVALID; + } + + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + + SYS_CALL_PORT_REQUEST call; + memset(&call, 0, sizeof(call)); + call.Size = sizeof(call); + call.EndpointHandle = endpoint; + call.Flags = SYS_PORT_FLAG_NONE; + call.RequestPayload = request; + call.RequestLength = sizeof(*request); + call.RequestHandles = requestHandles; + call.RequestHandleCount = requestHandleCount; + call.ReplyPayload = replyOut; + call.ReplyCapacity = sizeof(*replyOut); + call.ReplyHandlesOut = replyHandlesOut; + call.ReplyHandleCapacity = replyHandleCapacity; + call.ReplyInfoOut = &info; + call.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysCallPort(&call); + if (!KSUCCESS(status)) return status; + + uint32_t expectedHandles = KSUCCESS(replyOut->Status) ? expectedReplyHandleCount : 0u; + int handlesValid = expectedReplyHandleCount == UINT32_MAX ? + info.HandleCount <= replyHandleCapacity : info.HandleCount == expectedHandles; + if (info.Type != SYS_PORT_MESSAGE_REPLY || !handlesValid || + info.PayloadLength != sizeof(*replyOut) || replyOut->Size != sizeof(*replyOut) || + replyOut->Version != NTL_POSIX_PROTOCOL_VERSION || + replyOut->Operation != request->Operation || + replyOut->Flags != NTL_POSIX_REQUEST_FLAG_NONE) { + if (replyHandlesOut) { + uint32_t closeCount = info.HandleCount < replyHandleCapacity ? + info.HandleCount : replyHandleCapacity; + for (uint32_t i = 0; i < closeCount; ++i) { + if (replyHandlesOut[i] != SYS_HANDLE_INVALID) + (void)SysCloseHandle(replyHandlesOut[i]); + replyHandlesOut[i] = SYS_HANDLE_INVALID; + } + } + return NTLSTATUS_FAILURE; + } + if (actualReplyHandleCountOut) *actualReplyHandleCountOut = info.HandleCount; + return NTLSTATUS_SUCCESS; +} + +// Calls posixsrv while optionally transferring native capabilities. +KSTATUS PosixRuntimeCallServerWithHandles(const NTL_POSIX_REQUEST* request, + const SYS_PORT_HANDLE_TRANSFER* handles, + uint32_t handleCount, + NTL_POSIX_REPLY* replyOut) { + POSIX_PROCESS_STATE* process = gPosixProcessState; + if (!process || process->Magic != POSIX_PROCESS_STATE_MAGIC) + return NTLSTATUS_NOT_FOUND; + return PosixRuntimeCallEndpoint(process->ServerEndpoint, request, handles, handleCount, + (SYS_HANDLE*)0, 0, 0, (uint32_t*)0, replyOut); +} + +// Calls posixsrv with independent request/reply capability transfer. +KSTATUS PosixRuntimeCallServerExchange(const NTL_POSIX_REQUEST* request, + const SYS_PORT_HANDLE_TRANSFER* requestHandles, + uint32_t requestHandleCount, + SYS_HANDLE* replyHandlesOut, + uint32_t replyHandleCapacity, + uint32_t expectedReplyHandleCount, + NTL_POSIX_REPLY* replyOut) { + POSIX_PROCESS_STATE* process = gPosixProcessState; + if (!process || process->Magic != POSIX_PROCESS_STATE_MAGIC) + return NTLSTATUS_NOT_FOUND; + return PosixRuntimeCallEndpoint(process->ServerEndpoint, request, + requestHandles, requestHandleCount, + replyHandlesOut, replyHandleCapacity, + expectedReplyHandleCount, (uint32_t*)0, replyOut); +} + +KSTATUS PosixRuntimeCallServerExchangeVariable(const NTL_POSIX_REQUEST* request, + SYS_HANDLE* replyHandlesOut, + uint32_t replyHandleCapacity, + uint32_t* replyHandleCountOut, + NTL_POSIX_REPLY* replyOut) { + POSIX_PROCESS_STATE* process = gPosixProcessState; + if (!process || process->Magic != POSIX_PROCESS_STATE_MAGIC || !replyHandleCountOut) + return NTLSTATUS_INVALID_PARAMETER; + *replyHandleCountOut = 0; + return PosixRuntimeCallEndpoint(process->ServerEndpoint, request, + (const SYS_PORT_HANDLE_TRANSFER*)0, 0, + replyHandlesOut, replyHandleCapacity, UINT32_MAX, + replyHandleCountOut, replyOut); +} + +// Calls posixsrv over the registered process endpoint and validates one reply. +KSTATUS PosixRuntimeCallServer(const NTL_POSIX_REQUEST* request, NTL_POSIX_REPLY* replyOut) { + return PosixRuntimeCallServerWithHandles(request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, + replyOut); +} + +// Queries the current server-owned POSIX process attributes. +KSTATUS PosixRuntimeGetProcessAttributes(NTL_POSIX_REPLY* replyOut) { + if (!replyOut) return NTLSTATUS_INVALID_PARAMETER; + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_GET_PROCESS_ATTRIBUTES; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + KSTATUS status = PosixRuntimeCallServer(&request, replyOut); + if (!KSUCCESS(status)) return status; + return replyOut->Status; +} + +// Reads and validates optional POSIX bootstrap identity from generic userspace loader data. +static KSTATUS PosixRuntimeReadBootstrap(NTL_POSIX_BOOTSTRAP_DATA* bootstrapOut) { + if (!bootstrapOut) return NTLSTATUS_INVALID_PARAMETER; + memset(bootstrapOut, 0, sizeof(*bootstrapOut)); + + size_t size = 0; + KSTATUS status = NtlGetProcessStartupData(bootstrapOut, sizeof(*bootstrapOut), &size); + if (status == NTLSTATUS_NOT_FOUND) + return NTLSTATUS_SUCCESS; + if (!KSUCCESS(status)) return status; + if (size != sizeof(*bootstrapOut) || bootstrapOut->Magic != NTL_POSIX_BOOTSTRAP_MAGIC || + bootstrapOut->Version != NTL_POSIX_BOOTSTRAP_VERSION || + bootstrapOut->Size != sizeof(*bootstrapOut) || bootstrapOut->Reserved != 0) + return NTLSTATUS_INVALID_PARAMETER; + return NTLSTATUS_SUCCESS; +} + +// Registers the current native process with posixsrv and caches its stable logical identity. +static KSTATUS PosixRuntimeRegisterProcess(POSIX_PROCESS_STATE* process, + const NTL_POSIX_BOOTSTRAP_DATA* bootstrap) { + if (!process || !bootstrap) return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = NTLSTATUS_SUCCESS; + SYS_HANDLE endpoint = SYS_HANDLE_INVALID; + status = PosixRuntimeConnectServer(&endpoint); + if (!KSUCCESS(status)) return status; + + SYS_HANDLE selfProcess = SYS_HANDLE_INVALID; + status = SysOpenProcess(SysGetCurrentPid(), + SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_RIGHT_TRANSFER, + &selfProcess); + if (!KSUCCESS(status)) { + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + return status; + } + + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = NTL_POSIX_OP_REGISTER_PROCESS; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + request.TransactionKind = NTL_POSIX_TRANSACTION_NONE; + request.BootstrapToken = bootstrap->Token; + request.BootstrapPosixPid = bootstrap->PosixPid; + request.BootstrapGeneration = bootstrap->Generation; + + size_t currentDirectorySize = 0; + status = NtlGetCurrentDirectory(request.Path, sizeof(request.Path), ¤tDirectorySize); + if (!KSUCCESS(status) || currentDirectorySize == 0 || currentDirectorySize > sizeof(request.Path)) { + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + (void)SysCloseHandle(selfProcess); + return KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + } + request.PathLength = (uint32_t)strlen(request.Path); + + SYS_PORT_HANDLE_TRANSFER transfer; + memset(&transfer, 0, sizeof(transfer)); + transfer.Handle = selfProcess; + transfer.DesiredRights = SYS_RIGHT_QUERY | SYS_RIGHT_WAIT; + transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + + NTL_POSIX_REPLY reply; + status = PosixRuntimeCallEndpoint(endpoint, &request, &transfer, 1u, + (SYS_HANDLE*)0, 0, 0, (uint32_t*)0, &reply); + (void)SysCloseHandle(selfProcess); + if (!KSUCCESS(status)) { + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + return status; + } + + if (!KSUCCESS(reply.Status)) { + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + return reply.Status; + } + if (reply.ClientProcessId != SysGetCurrentPid() || reply.PosixPid == 0 || + reply.PosixPid > 0x7FFFFFFFu || reply.NativeGeneration == 0 || + reply.ProcessState != NTL_POSIX_PROCESS_STATE_RUNNING) { + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + return NTLSTATUS_FAILURE; + } + + process->ServerEndpoint = endpoint; + process->DefaultIdentity.PosixPid = reply.PosixPid; + process->DefaultIdentity.Generation = reply.NativeGeneration; + return NTLSTATUS_SUCCESS; +} + +// Initializes process-local POSIX state, registers stable identity, and attaches the initial thread. +uint32_t PosixRuntimeInitializeProcess(void) { + if (gPosixProcessState) + return PosixRuntimeAttachThread(); + + SYS_PEB* peb = SysGetPeb(); + if (!peb || peb->Magic != SYS_PEB_MAGIC || peb->Version != SYS_ENV_VERSION) + return NTLSTATUS_INVALID_PARAMETER; + + POSIX_PROCESS_STATE* process = (POSIX_PROCESS_STATE*)calloc(1, sizeof(*process)); + if (!process) + return NTLSTATUS_NO_MEMORY; + + process->Magic = POSIX_PROCESS_STATE_MAGIC; + process->Peb = peb; + process->ServerEndpoint = SYS_HANDLE_INVALID; + process->FdLock = SYS_HANDLE_INVALID; + gPosixProcessState = process; + + NTL_POSIX_BOOTSTRAP_DATA bootstrap; + uint32_t status = PosixRuntimeReadBootstrap(&bootstrap); + if (status != NTLSTATUS_SUCCESS) { + gPosixProcessState = (POSIX_PROCESS_STATE*)0; + free(process); + return status; + } + + status = PosixRuntimeRegisterProcess(process, &bootstrap); + if (status != NTLSTATUS_SUCCESS) { + gPosixProcessState = (POSIX_PROCESS_STATE*)0; + free(process); + return status; + } + + status = PosixFdInitializeProcess(process, bootstrap.FdInheritanceToken); + if (status != NTLSTATUS_SUCCESS) { + (void)SysDisconnectPort(process->ServerEndpoint); + (void)SysCloseHandle(process->ServerEndpoint); + process->ServerEndpoint = SYS_HANDLE_INVALID; + gPosixProcessState = (POSIX_PROCESS_STATE*)0; + free(process); + return status; + } + + status = PosixRuntimeAttachThread(); + if (status != NTLSTATUS_SUCCESS) { + PosixFdDestroyProcess(process); + (void)SysDisconnectPort(process->ServerEndpoint); + (void)SysCloseHandle(process->ServerEndpoint); + process->ServerEndpoint = SYS_HANDLE_INVALID; + gPosixProcessState = (POSIX_PROCESS_STATE*)0; + free(process); + return status; + } + + return NTLSTATUS_SUCCESS; +} + +// Returns the POSIX state for the current native process after startup. +POSIX_PROCESS_STATE* PosixRuntimeProcessState(void) { + POSIX_PROCESS_STATE* process = gPosixProcessState; + if (!process || process->Magic != POSIX_PROCESS_STATE_MAGIC) + return (POSIX_PROCESS_STATE*)0; + return process; +} + +// Attaches the native thread to the process's default logical POSIX identity. +uint32_t PosixRuntimeAttachThread(void) { + SYS_TEB* teb = SysGetTeb(); + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!teb || teb->Magic != SYS_TEB_MAGIC || !process) + return NTLSTATUS_INVALID_PARAMETER; + + if (teb->SubsystemData) { + POSIX_THREAD_STATE* existing = (POSIX_THREAD_STATE*)teb->SubsystemData; + return existing->Magic == POSIX_THREAD_STATE_MAGIC && + existing->NativeProcess == process && existing->LogicalProcess + ? NTLSTATUS_SUCCESS + : NTLSTATUS_ACCESS_DENIED; + } + + POSIX_THREAD_STATE* thread = (POSIX_THREAD_STATE*)calloc(1, sizeof(*thread)); + if (!thread) + return NTLSTATUS_NO_MEMORY; + + thread->Magic = POSIX_THREAD_STATE_MAGIC; + thread->NativeProcess = process; + thread->LogicalProcess = &process->DefaultIdentity; + teb->SubsystemData = thread; + return NTLSTATUS_SUCCESS; +} + +// Returns the current thread state, attaching lazily when necessary. +POSIX_THREAD_STATE* PosixRuntimeThreadState(void) { + if (!PosixRuntimeProcessState()) { + if (PosixRuntimeInitializeProcess() != NTLSTATUS_SUCCESS) + return (POSIX_THREAD_STATE*)0; + } + + SYS_TEB* teb = SysGetTeb(); + if (!teb || teb->Magic != SYS_TEB_MAGIC) + return (POSIX_THREAD_STATE*)0; + + if (!teb->SubsystemData && PosixRuntimeAttachThread() != NTLSTATUS_SUCCESS) + return (POSIX_THREAD_STATE*)0; + + POSIX_THREAD_STATE* thread = (POSIX_THREAD_STATE*)teb->SubsystemData; + if (!thread || thread->Magic != POSIX_THREAD_STATE_MAGIC || + thread->NativeProcess != PosixRuntimeProcessState() || !thread->LogicalProcess) + return (POSIX_THREAD_STATE*)0; + return thread; +} + +// Releases POSIX state owned by the current native TEB. +void PosixRuntimeDetachThread(void) { + SYS_TEB* teb = SysGetTeb(); + if (!teb || teb->Magic != SYS_TEB_MAGIC || !teb->SubsystemData) + return; + + POSIX_THREAD_STATE* thread = (POSIX_THREAD_STATE*)teb->SubsystemData; + if (thread->Magic != POSIX_THREAD_STATE_MAGIC || + thread->NativeProcess != PosixRuntimeProcessState()) + return; + + teb->SubsystemData = (void*)0; + thread->Magic = 0; + free(thread); +} + +// Returns the lvalue backing the current thread's errno value. +NTL_POSIX_API int* __ntl_posix_errno_location(void) { + POSIX_THREAD_STATE* thread = PosixRuntimeThreadState(); + if (!thread) + return &gEmergencyErrno; + return &thread->ErrnoValue; +} + +// Clears the current thread's dynamic-loader error state. +void PosixRuntimeClearDlError(void) { + POSIX_THREAD_STATE* thread = PosixRuntimeThreadState(); + if (!thread) + return; + thread->DlError[0] = '\0'; + thread->DlErrorPending = 0; +} + +// Stores one dynamic-loader error in current-thread state. +void PosixRuntimeSetDlError(const char* message) { + POSIX_THREAD_STATE* thread = PosixRuntimeThreadState(); + if (!thread) + return; + if (!message) + message = "dynamic loader error"; + strncpy(thread->DlError, message, sizeof(thread->DlError) - 1u); + thread->DlError[sizeof(thread->DlError) - 1u] = '\0'; + thread->DlErrorPending = 1; +} + +// Returns and consumes the current thread's dynamic-loader error. +char* PosixRuntimeTakeDlError(void) { + POSIX_THREAD_STATE* thread = PosixRuntimeThreadState(); + if (!thread || !thread->DlErrorPending || !thread->DlError[0]) + return (char*)0; + thread->DlErrorPending = 0; + return thread->DlError; +} + +// Initializes all POSIX process state required before application main. +NTL_POSIX_API uint32_t __ntl_posix_process_start(void) { + uint32_t status = PosixRuntimeInitializeProcess(); + if (status != NTLSTATUS_SUCCESS) + return status; + + return NTLSTATUS_SUCCESS; +} diff --git a/code/user/personalities/posix/posix_socket.c b/code/user/personalities/posix/posix_socket.c index c8d1a08..c8ecef8 100644 --- a/code/user/personalities/posix/posix_socket.c +++ b/code/user/personalities/posix/posix_socket.c @@ -5,7 +5,7 @@ #include #include #include -#include "posix.h" +#include "posix_internal.h" #include "posix_fd.h" // Implements the bswap16 operation. @@ -284,61 +284,84 @@ static void SysToSockAddr(const SYS_SOCKET_ADDRESS* in, struct sockaddr* addr, s } } -// Implements the socket Fd Get operation. -static FD_ENTRY* SocketFdGet(int fd) { - FD_ENTRY* e = FdGet(SysGetCurrentPid(), fd); - if (!e) return (FD_ENTRY*)0; - if (e->Type != FD_SOCKET) { errno = ENOTSOCK; return (FD_ENTRY*)0; } - return e; +// Acquires one socket open-description reference. +static POSIX_OPEN_DESCRIPTION* SocketFdAcquire(int fd) { + POSIX_OPEN_DESCRIPTION* description = PosixFdAcquire(fd); + if (!description) + return (POSIX_OPEN_DESCRIPTION*)0; + if (description->Type != PosixFdSocket) { + PosixFdRelease(description); + errno = ENOTSOCK; + return (POSIX_OPEN_DESCRIPTION*)0; + } + return description; } -// Implements the socket operation. +// Creates a socket and installs it as a process-local descriptor. __declspec(dllexport) int socket(int domain, int type, int protocol) { - uint32_t kh = 0; - uint32_t s = SysCreateSocket((uint32_t)domain, (uint32_t)type, (uint32_t)protocol, &kh); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } - - int fd = FdAlloc(SysGetCurrentPid(), FD_SOCKET, kh); - if (fd < 0) { - SysCloseHandle(kh); + uint32_t kh = SYS_HANDLE_INVALID; + uint32_t status = SysCreateSocket((uint32_t)domain, (uint32_t)type, (uint32_t)protocol, &kh); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); return -1; } + + int fd = PosixFdCreate(PosixFdSocket, kh, O_RDWR); + if (fd < 0) + SysCloseHandle(kh); return fd; } -// Implements the bind operation. +// Binds one socket descriptor to a local address. __declspec(dllexport) int bind(int fd, const struct sockaddr* addr, socklen_t addrlen) { - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; - SYS_SOCKET_ADDRESS a; - if (SockAddrToSys(addr, addrlen, &a) != 0) return -1; - uint32_t s = SysBindSocket(e->KHandle, &a, sizeof(a)); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; + SYS_SOCKET_ADDRESS address; + if (SockAddrToSys(addr, addrlen, &address) != 0) { + PosixFdRelease(description); + return -1; + } + uint32_t status = SysBindSocket(description->KHandle, &address, sizeof(address)); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return 0; } -// Implements the listen operation. +// Places one socket into listening state. __declspec(dllexport) int listen(int fd, int backlog) { - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; - uint32_t s = SysListenSocket(e->KHandle, backlog < 0 ? 0u : (uint32_t)backlog); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; + uint32_t status = SysListenSocket(description->KHandle, backlog < 0 ? 0u : (uint32_t)backlog); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return 0; } -// Implements the accept operation. +// Accepts one socket and installs the accepted endpoint as a new descriptor. __declspec(dllexport) int accept(int fd, struct sockaddr* addr, socklen_t* addrlen) { - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; SYS_SOCKET_ADDRESS remote; size_t remoteSize = sizeof(remote); - uint32_t kh = 0; - uint32_t s = SysAcceptSocket(e->KHandle, addr ? &remote : (SYS_SOCKET_ADDRESS*)0, - addr ? &remoteSize : (size_t*)0, &kh); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + uint32_t kh = SYS_HANDLE_INVALID; + uint32_t status = SysAcceptSocket(description->KHandle, + addr ? &remote : (SYS_SOCKET_ADDRESS*)0, + addr ? &remoteSize : (size_t*)0, + &kh); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } - int outfd = FdAlloc(SysGetCurrentPid(), FD_SOCKET, kh); + int outfd = PosixFdCreate(PosixFdSocket, kh, O_RDWR); if (outfd < 0) { SysCloseHandle(kh); return -1; @@ -347,113 +370,145 @@ __declspec(dllexport) int accept(int fd, struct sockaddr* addr, socklen_t* addrl return outfd; } -// Implements the connect operation. +// Connects one socket descriptor to a remote address. __declspec(dllexport) int connect(int fd, const struct sockaddr* addr, socklen_t addrlen) { - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; - SYS_SOCKET_ADDRESS a; - if (SockAddrToSys(addr, addrlen, &a) != 0) return -1; - uint32_t s = SysConnectSocket(e->KHandle, &a, sizeof(a)); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; + SYS_SOCKET_ADDRESS address; + if (SockAddrToSys(addr, addrlen, &address) != 0) { + PosixFdRelease(description); + return -1; + } + uint32_t status = SysConnectSocket(description->KHandle, &address, sizeof(address)); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return 0; } -// Implements the send operation. +// Sends bytes through one socket descriptor. __declspec(dllexport) ssize_t send(int fd, const void* buf, size_t len, int flags) { - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; size_t sent = 0; SYS_SOCKET_SEND_REQUEST request = { - sizeof(SYS_SOCKET_SEND_REQUEST), e->KHandle, (uint32_t)flags, + sizeof(SYS_SOCKET_SEND_REQUEST), description->KHandle, (uint32_t)flags, buf, len, &sent }; - uint32_t s = SysSendSocket(&request); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + uint32_t status = SysSendSocket(&request); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return (ssize_t)sent; } -// Implements the recv operation. +// Receives bytes through one socket descriptor. __declspec(dllexport) ssize_t recv(int fd, void* buf, size_t len, int flags) { - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; size_t got = 0; SYS_SOCKET_RECV_REQUEST request = { - sizeof(SYS_SOCKET_RECV_REQUEST), e->KHandle, (uint32_t)flags, + sizeof(SYS_SOCKET_RECV_REQUEST), description->KHandle, (uint32_t)flags, buf, len, &got }; - uint32_t s = SysRecvSocket(&request); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + uint32_t status = SysRecvSocket(&request); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return (ssize_t)got; } +// Sends one datagram to a remote address. __declspec(dllexport) ssize_t sendto(int fd, const void* buf, size_t len, int flags, const struct sockaddr* dest, socklen_t destlen) { - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; SYS_SOCKET_ADDRESS remote; - if (SockAddrToSys(dest, destlen, &remote) != 0) return -1; + if (SockAddrToSys(dest, destlen, &remote) != 0) { + PosixFdRelease(description); + return -1; + } size_t sent = 0; - SYS_SOCKET_SEND_TO_REQUEST req; - memset(&req, 0, sizeof(req)); - req.Size = sizeof(req); - req.Buffer = buf; - req.Length = len; - req.Flags = (uint32_t)flags; - req.RemoteAddress = &remote; - req.RemoteAddressSize = sizeof(remote); - req.SentOut = &sent; + SYS_SOCKET_SEND_TO_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Buffer = buf; + request.Length = len; + request.Flags = (uint32_t)flags; + request.RemoteAddress = &remote; + request.RemoteAddressSize = sizeof(remote); + request.SentOut = &sent; - uint32_t s = SysSendToSocket(e->KHandle, &req); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + uint32_t status = SysSendToSocket(description->KHandle, &request); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return (ssize_t)sent; } +// Receives one datagram and optionally reports its source address. __declspec(dllexport) ssize_t recvfrom(int fd, void* buf, size_t len, int flags, struct sockaddr* src, socklen_t* srclen) { - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; SYS_SOCKET_ADDRESS remote; size_t remoteSize = sizeof(remote); size_t got = 0; - SYS_SOCKET_RECV_FROM_REQUEST req; - memset(&req, 0, sizeof(req)); - req.Size = sizeof(req); - req.Buffer = buf; - req.Length = len; - req.Flags = (uint32_t)flags; - req.RemoteAddressOut = src ? &remote : (SYS_SOCKET_ADDRESS*)0; - req.RemoteAddressSizeInOut = src ? &remoteSize : (size_t*)0; - req.ReceivedOut = &got; + SYS_SOCKET_RECV_FROM_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Buffer = buf; + request.Length = len; + request.Flags = (uint32_t)flags; + request.RemoteAddressOut = src ? &remote : (SYS_SOCKET_ADDRESS*)0; + request.RemoteAddressSizeInOut = src ? &remoteSize : (size_t*)0; + request.ReceivedOut = &got; - uint32_t s = SysRecvFromSocket(e->KHandle, &req); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + uint32_t status = SysRecvFromSocket(description->KHandle, &request); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } if (src) SysToSockAddr(&remote, src, srclen); return (ssize_t)got; } -// Implements the shutdown operation. +// Shuts down one or both directions of a socket. __declspec(dllexport) int shutdown(int fd, int how) { - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; - uint32_t s = SysShutdownSocket(e->KHandle, (uint32_t)how); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; + uint32_t status = SysShutdownSocket(description->KHandle, (uint32_t)how); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return 0; } -// Implements the timeval To Ms operation. +// Converts one timeval into a bounded millisecond count. static uint32_t TimevalToMs(const struct timeval* tv) { if (!tv) return 0; uint64_t ms = (uint64_t)tv->tv_sec * 1000u + (uint64_t)(tv->tv_usec / 1000); return ms > 0xFFFFFFFFu ? 0xFFFFFFFFu : (uint32_t)ms; } -// Implements the setsockopt operation. +// Sets one supported socket option. __declspec(dllexport) int setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen) { if (level != SOL_SOCKET || !optval) { errno = EINVAL; return -1; } - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; uint32_t option = 0; uint32_t value = 0; @@ -461,50 +516,80 @@ __declspec(dllexport) int setsockopt(int fd, int level, int optname, const void* option = (optname == SO_RCVTIMEO) ? SYS_SOCKET_OPT_RECV_TIMEOUT_MS : SYS_SOCKET_OPT_SEND_TIMEOUT_MS; if (optlen == sizeof(struct timeval)) value = TimevalToMs((const struct timeval*)optval); else if (optlen == sizeof(uint32_t)) memcpy(&value, optval, sizeof(value)); - else { errno = EINVAL; return -1; } + else { + PosixFdRelease(description); + errno = EINVAL; + return -1; + } } else if (optname == SO_NONBLOCK) { option = SYS_SOCKET_OPT_NONBLOCKING; - if (optlen < sizeof(int)) { errno = EINVAL; return -1; } + if (optlen < sizeof(int)) { + PosixFdRelease(description); + errno = EINVAL; + return -1; + } value = (*(const int*)optval) ? 1u : 0u; } else { + PosixFdRelease(description); errno = ENOSYS; return -1; } - SYS_SOCKET_OPTION_REQUEST req; - memset(&req, 0, sizeof(req)); - req.Size = sizeof(req); - req.Option = option; - req.Value = &value; - req.ValueSize = sizeof(value); - uint32_t s = SysSetSocketOption(e->KHandle, &req); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + if (optname == SO_NONBLOCK) { + int flags = PosixFdGetStatusFlags(description); + if (value) flags |= O_NONBLOCK; + else flags &= ~O_NONBLOCK; + int result = PosixFdSetStatusFlags(description, flags); + PosixFdRelease(description); + return result; + } + + SYS_SOCKET_OPTION_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Option = option; + request.Value = &value; + request.ValueSize = sizeof(value); + uint32_t status = SysSetSocketOption(description->KHandle, &request); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return 0; } -// Implements the getsockopt operation. +// Queries one supported socket option. __declspec(dllexport) int getsockopt(int fd, int level, int optname, void* optval, socklen_t* optlen) { if (level != SOL_SOCKET || !optval || !optlen) { errno = EINVAL; return -1; } - FD_ENTRY* e = SocketFdGet(fd); - if (!e) return -1; + POSIX_OPEN_DESCRIPTION* description = SocketFdAcquire(fd); + if (!description) return -1; uint32_t option = 0; if (optname == SO_RCVTIMEO) option = SYS_SOCKET_OPT_RECV_TIMEOUT_MS; else if (optname == SO_SNDTIMEO) option = SYS_SOCKET_OPT_SEND_TIMEOUT_MS; else if (optname == SO_NONBLOCK) option = SYS_SOCKET_OPT_NONBLOCKING; - else { errno = ENOSYS; return -1; } + else { + PosixFdRelease(description); + errno = ENOSYS; + return -1; + } uint32_t value = 0; size_t actual = sizeof(value); - SYS_SOCKET_OPTION_REQUEST req; - memset(&req, 0, sizeof(req)); - req.Size = sizeof(req); - req.Option = option; - req.Value = &value; - req.ValueSize = sizeof(value); - req.ActualSizeOut = &actual; - uint32_t s = SysGetSocketOption(e->KHandle, &req); - if (s != 0) { errno = PosixStatusToErrno(s); return -1; } + SYS_SOCKET_OPTION_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Option = option; + request.Value = &value; + request.ValueSize = sizeof(value); + request.ActualSizeOut = &actual; + uint32_t status = SysGetSocketOption(description->KHandle, &request); + PosixFdRelease(description); + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } if ((optname == SO_RCVTIMEO || optname == SO_SNDTIMEO) && *optlen >= sizeof(struct timeval)) { struct timeval* tv = (struct timeval*)optval; @@ -521,24 +606,35 @@ __declspec(dllexport) int getsockopt(int fd, int level, int optname, void* optva return 0; } -// Polls the requested operation. +// Polls socket and pipe descriptors while holding stable open-description references. __declspec(dllexport) int poll(struct pollfd* fds, nfds_t nfds, int timeout) { - if (!fds || nfds == 0 || nfds > 64) { errno = EINVAL; return -1; } + if (!fds || nfds == 0 || nfds > POSIX_FD_MAX) { errno = EINVAL; return -1; } - SYS_POLLFD* kfds = (SYS_POLLFD*)malloc(sizeof(SYS_POLLFD) * nfds); - if (!kfds) { errno = ENOMEM; return -1; } + SYS_POLLFD* kfds = (SYS_POLLFD*)calloc(nfds, sizeof(SYS_POLLFD)); + POSIX_OPEN_DESCRIPTION** descriptions = + (POSIX_OPEN_DESCRIPTION**)calloc(nfds, sizeof(POSIX_OPEN_DESCRIPTION*)); + if (!kfds || !descriptions) { + free(kfds); + free(descriptions); + errno = ENOMEM; + return -1; + } for (nfds_t i = 0; i < nfds; i++) { - kfds[i].Handle = 0; - kfds[i].Events = 0; - kfds[i].ReturnedEvents = 0; - fds[i].revents = 0; - FD_ENTRY* e = FdGet(SysGetCurrentPid(), fds[i].fd); - if (!e || e->Type != FD_SOCKET) { + descriptions[i] = PosixFdAcquire(fds[i].fd); + if (!descriptions[i] || + (descriptions[i]->Type != PosixFdSocket && + descriptions[i]->Type != PosixFdPipeRead && + descriptions[i]->Type != PosixFdPipeWrite)) { + if (descriptions[i]) { + PosixFdRelease(descriptions[i]); + descriptions[i] = (POSIX_OPEN_DESCRIPTION*)0; + } + fds[i].revents = POLLNVAL; continue; } - kfds[i].Handle = e->KHandle; + kfds[i].Handle = descriptions[i]->KHandle; if (fds[i].events & POLLIN) kfds[i].Events |= SYS_POLL_READ; if (fds[i].events & POLLOUT) kfds[i].Events |= SYS_POLL_WRITE; if (fds[i].events & POLLERR) kfds[i].Events |= SYS_POLL_ERROR; @@ -547,33 +643,38 @@ __declspec(dllexport) int poll(struct pollfd* fds, nfds_t nfds, int timeout) { size_t ready = 0; uint32_t timeoutMs = timeout < 0 ? SYS_POLL_TIMEOUT_INFINITE : (uint32_t)timeout; - SYS_POLL_REQUEST req; - memset(&req, 0, sizeof(req)); - req.Size = sizeof(req); - req.TimeoutMs = timeoutMs; - req.Fds = kfds; - req.Count = nfds; - req.ReadyOut = &ready; - KSTATUS s = SysPoll(&req); - if (s != 0) { - free(kfds); - errno = PosixStatusToErrno(s); - return -1; - } + SYS_POLL_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.TimeoutMs = timeoutMs; + request.Fds = kfds; + request.Count = nfds; + request.ReadyOut = &ready; + KSTATUS status = SysPoll(&request); int out = 0; - for (nfds_t i = 0; i < nfds; i++) { - short re = 0; - if (kfds[i].ReturnedEvents & SYS_POLL_READ) re |= POLLIN; - if (kfds[i].ReturnedEvents & SYS_POLL_WRITE) re |= POLLOUT; - if (kfds[i].ReturnedEvents & SYS_POLL_ERROR) re |= POLLERR; - if (kfds[i].ReturnedEvents & SYS_POLL_HANGUP) re |= POLLHUP; - if (kfds[i].ReturnedEvents & SYS_POLL_INVALID) re |= POLLNVAL; - fds[i].revents = re; - if (re) out++; + if (status == NTLSTATUS_SUCCESS) { + for (nfds_t i = 0; i < nfds; i++) { + short revents = fds[i].revents; + if (kfds[i].ReturnedEvents & SYS_POLL_READ) revents |= POLLIN; + if (kfds[i].ReturnedEvents & SYS_POLL_WRITE) revents |= POLLOUT; + if (kfds[i].ReturnedEvents & SYS_POLL_ERROR) revents |= POLLERR; + if (kfds[i].ReturnedEvents & SYS_POLL_HANGUP) revents |= POLLHUP; + if (kfds[i].ReturnedEvents & SYS_POLL_INVALID) revents |= POLLNVAL; + fds[i].revents = revents; + if (revents) out++; + } } + for (nfds_t i = 0; i < nfds; i++) + PosixFdRelease(descriptions[i]); + free(descriptions); free(kfds); + + if (status != NTLSTATUS_SUCCESS) { + errno = PosixStatusToErrno(status); + return -1; + } return out; } diff --git a/code/user/personalities/posix/posix_thread.c b/code/user/personalities/posix/posix_thread.c index 2e49d5c..f37c4d4 100644 --- a/code/user/personalities/posix/posix_thread.c +++ b/code/user/personalities/posix/posix_thread.c @@ -3,46 +3,38 @@ #include #include #include -#include "posix.h" +#include "posix_internal.h" -// Thread return-value table (PID-keyed like the fd table). -#define PTHREAD_TABLE_PIDS 32 -#define PTHREAD_TABLE_SIZE 32 +// Allocates a join-result slot in process-local POSIX state. +static POSIX_PTHREAD_SLOT* PthreadSlotAlloc(uint32_t tid) { + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process) return (POSIX_PTHREAD_SLOT*)0; -typedef struct { - uint32_t Tid; - void* RetVal; - bool Used; -} PTHREAD_SLOT; - -static PTHREAD_SLOT gPthreadTable[PTHREAD_TABLE_PIDS][PTHREAD_TABLE_SIZE]; - -// Implements the pthread Slot Alloc operation. -static PTHREAD_SLOT* PthreadSlotAlloc(uint32_t pid, uint32_t tid) { - PTHREAD_SLOT* table = gPthreadTable[pid % PTHREAD_TABLE_PIDS]; - for (int i = 0; i < PTHREAD_TABLE_SIZE; i++) { - if (!table[i].Used) { - table[i] = (PTHREAD_SLOT){ tid, (void*)0, true }; - return &table[i]; + for (uint32_t i = 0; i < POSIX_PTHREAD_TABLE_SIZE; i++) { + if (!process->PthreadTable[i].Used) { + process->PthreadTable[i] = (POSIX_PTHREAD_SLOT){ tid, (void*)0, true }; + return &process->PthreadTable[i]; } } - return (PTHREAD_SLOT*)0; + return (POSIX_PTHREAD_SLOT*)0; } -// Implements the pthread Slot Find operation. -static PTHREAD_SLOT* PthreadSlotFind(uint32_t pid, uint32_t tid) { - PTHREAD_SLOT* table = gPthreadTable[pid % PTHREAD_TABLE_PIDS]; - for (int i = 0; i < PTHREAD_TABLE_SIZE; i++) - if (table[i].Used && table[i].Tid == tid) - return &table[i]; - return (PTHREAD_SLOT*)0; +// Finds a join-result slot in process-local POSIX state. +static POSIX_PTHREAD_SLOT* PthreadSlotFind(uint32_t tid) { + POSIX_PROCESS_STATE* process = PosixRuntimeProcessState(); + if (!process) return (POSIX_PTHREAD_SLOT*)0; + + for (uint32_t i = 0; i < POSIX_PTHREAD_TABLE_SIZE; i++) { + if (process->PthreadTable[i].Used && process->PthreadTable[i].Tid == tid) + return &process->PthreadTable[i]; + } + return (POSIX_PTHREAD_SLOT*)0; } // Trampoline - this is what actually runs as the new thread. typedef struct { void* (*Fn)(void*); - void* Arg; - uint32_t Pid; // owner pid, for the retval table + void* Arg; } PTHREAD_TRAMP; // Implements the pthread Trampoline operation. @@ -50,14 +42,18 @@ static void PthreadTrampoline(uintptr_t raw) { PTHREAD_TRAMP* tr = (PTHREAD_TRAMP*)raw; void* (*fn)(void*) = tr->Fn; void* arg = tr->Arg; - uint32_t pid = tr->Pid; uint32_t tid = SysGetCurrentTid(); + if (PosixRuntimeAttachThread() != NTLSTATUS_SUCCESS) { + free(tr); + SysExitThread(ENOMEM); + } + // Create the join slot from inside the new thread. KeCreateThread makes the // thread runnable before the creator regains control, so the old design // raced on tr->Tid and could observe zero/stale memory. - PTHREAD_SLOT* slot = PthreadSlotFind(pid, tid); - if (!slot) slot = PthreadSlotAlloc(pid, tid); + POSIX_PTHREAD_SLOT* slot = PthreadSlotFind(tid); + if (!slot) slot = PthreadSlotAlloc(tid); free(tr); @@ -66,6 +62,7 @@ static void PthreadTrampoline(uintptr_t raw) { // Store retval for pthread_join. if (slot) slot->RetVal = retval; + PosixRuntimeDetachThread(); SysExitThread(0); } @@ -75,13 +72,10 @@ __declspec(dllexport) int pthread_create(pthread_t* t, const pthread_attr_t* att void* (*fn)(void*), void* arg) { (void)attr; - uint32_t pid = SysGetCurrentPid(); - PTHREAD_TRAMP* tr = (PTHREAD_TRAMP*)malloc(sizeof(PTHREAD_TRAMP)); if (!tr) return ENOMEM; tr->Fn = fn; tr->Arg = arg; - tr->Pid = pid; uint32_t tid = 0; uint32_t handle = 0; @@ -111,11 +105,10 @@ __declspec(dllexport) int pthread_create(pthread_t* t, const pthread_attr_t* att // Implements the pthread join operation. __declspec(dllexport) int pthread_join(pthread_t t, void** retval) { - uint32_t pid = SysGetCurrentPid(); uint32_t s = SysWait(t.Handle, SYS_WAIT_FLAG_NONE); if (s != 0) return ESRCH; - PTHREAD_SLOT* slot = PthreadSlotFind(pid, t.Tid); + POSIX_PTHREAD_SLOT* slot = PthreadSlotFind(t.Tid); if (retval) *retval = slot ? slot->RetVal : (void*)0; if (slot) slot->Used = false; SysCloseHandle(t.Handle); @@ -124,10 +117,10 @@ __declspec(dllexport) int pthread_join(pthread_t t, void** retval) { // Implements the pthread exit operation. __declspec(dllexport) void pthread_exit(void* retval) { - uint32_t pid = SysGetCurrentPid(); uint32_t tid = SysGetCurrentTid(); - PTHREAD_SLOT* slot = PthreadSlotFind(pid, tid); + POSIX_PTHREAD_SLOT* slot = PthreadSlotFind(tid); if (slot) slot->RetVal = retval; + PosixRuntimeDetachThread(); SysExitThread(0); while (1); // unreachable } diff --git a/code/user/personalities/win32/kernel32.c b/code/user/personalities/win32/kernel32.c index ef6002d..8a6555b 100644 --- a/code/user/personalities/win32/kernel32.c +++ b/code/user/personalities/win32/kernel32.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include #include @@ -338,8 +340,7 @@ static uint32_t K32CreateProcessNative(const char* path, uint32_t imageFile, uin // Implements the k32 Write Console Span operation. static void K32WriteConsoleSpan(const char* data, size_t length) { - SYS_WRITE_CONSOLE_REQUEST req = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromParts(data, length) }; - SysWriteConsole(&req); + (void)NtlConsoleWrite(SYS_HANDLE_INVALID, data, length, 0); } // Implements the k32 Debug Print A operation. @@ -745,7 +746,7 @@ __declspec(dllexport) BOOL FlushViewOfFile(const void* lpBaseAddress, size_t dwN // Returns whether one input key is currently held for Win32 adapters. static bool Win32IsInputKeyDown(INPUT_KEY key) { bool down = false; - return SysIsKeyDown(key, &down) == NTLSTATUS_SUCCESS && down; + return NtlInputIsKeyDown(key, &down) == NTLSTATUS_SUCCESS && down; } // GetAsyncKeyState implementation over the canonical input key namespace. @@ -811,18 +812,14 @@ __declspec(dllexport) SHORT GetAsyncKeyState(int vKey) { // FlushConsoleInputBuffer: clear the keyboard input buffer. __declspec(dllexport) BOOL FlushConsoleInputBuffer(HANDLE hConsoleInput) { (void)hConsoleInput; - // Drain the keyboard buffer by reading until empty. - INPUT_KEY key; - while (SysPollKeyboard(&key) == NTLSTATUS_SUCCESS) { - char ignored; - (void)SysReadKeyboardChar(&ignored); - } + (void)NtlConsoleFlushInput(SYS_HANDLE_INVALID); return TRUE; } // !Made-up! clear console api. __declspec(dllexport) BOOL ClearConsoleScreenBuffer(HANDLE hConsole) { - SysClearScreen(0x012456); + (void)hConsole; + (void)NtlConsoleClear(SYS_HANDLE_INVALID, 0x012456); return TRUE; } @@ -1948,7 +1945,7 @@ __declspec(dllexport) HANDLE GetStdHandle(DWORD nStdHandle) { } // ReadConsoleA: blocking read of up to nNumberOfCharsToRead characters. -// Each character is read individually via SysReadKeyboardChar so the caller can +// Each character is read individually via NtlInputReadKeyboardChar so the caller can // do its own line-editing (echo, backspace, etc.) one char at a time. __declspec(dllexport) BOOL ReadConsoleA(HANDLE hConsoleInput, void* lpBuffer, @@ -1963,7 +1960,7 @@ __declspec(dllexport) BOOL ReadConsoleA(HANDLE hConsoleInput, while (i < nNumberOfCharsToRead) { char c = 0; - if (SysReadKeyboardChar(&c) != NTLSTATUS_SUCCESS) continue; + if (NtlConsoleReadCharacter(SYS_HANDLE_INVALID, &c) != NTLSTATUS_SUCCESS) continue; // Ignore unmapped keys (Shift, Ctrl, etc.). if (c == 0) continue; @@ -2013,8 +2010,8 @@ __declspec(dllexport) BOOL GetConsoleScreenBufferInfo(HANDLE hConsoleOutput, return FALSE; } - SYS_CONSOLE_INFO info; - uint32_t s = SysGetConsoleInfo(&info); + NTL_CONSOLE_INFO info; + uint32_t s = NtlConsoleQueryInfo(SYS_HANDLE_INVALID, &info); if (s != NTLSTATUS_SUCCESS) { K32SetConsoleLastError(s); return FALSE; @@ -2045,7 +2042,7 @@ __declspec(dllexport) BOOL SetConsoleCursorPosition(HANDLE hConsoleOutput, return FALSE; } - uint32_t s = SysSetConsoleCursor((int)dwCursorPosition.X, (int)dwCursorPosition.Y); + uint32_t s = NtlConsoleSetCursor(SYS_HANDLE_INVALID, (int)dwCursorPosition.X, (int)dwCursorPosition.Y); if (s != NTLSTATUS_SUCCESS) { K32SetConsoleLastError(s); return FALSE; @@ -2071,11 +2068,9 @@ __declspec(dllexport) BOOL FillConsoleOutputCharacterA(HANDLE hConsoleOutput, return TRUE; size_t written = 0; - SYS_FILL_CONSOLE_CHARACTER_REQUEST request = { - sizeof(SYS_FILL_CONSOLE_CHARACTER_REQUEST), cCharacter, { 0, 0, 0 }, - (int)dwWriteCoord.X, (int)dwWriteCoord.Y, (size_t)nLength, &written - }; - uint32_t s = SysFillConsoleCharacter(&request); + uint32_t s = NtlConsoleFillCharacter(SYS_HANDLE_INVALID, cCharacter, + (int)dwWriteCoord.X, (int)dwWriteCoord.Y, + (size_t)nLength, &written); if (lpNumberOfCharsWritten) *lpNumberOfCharsWritten = (DWORD)written; if (s != NTLSTATUS_SUCCESS) { K32SetConsoleLastError(s); @@ -2102,11 +2097,9 @@ __declspec(dllexport) BOOL FillConsoleOutputAttribute(HANDLE hConsoleOutput, return TRUE; size_t written = 0; - SYS_FILL_CONSOLE_ATTRIBUTE_REQUEST request = { - sizeof(SYS_FILL_CONSOLE_ATTRIBUTE_REQUEST), wAttribute, 0, - (int)dwWriteCoord.X, (int)dwWriteCoord.Y, (size_t)nLength, &written - }; - uint32_t s = SysFillConsoleAttribute(&request); + uint32_t s = NtlConsoleFillAttribute(SYS_HANDLE_INVALID, wAttribute, + (int)dwWriteCoord.X, (int)dwWriteCoord.Y, + (size_t)nLength, &written); if (lpNumberOfAttrsWritten) *lpNumberOfAttrsWritten = (DWORD)written; if (s != NTLSTATUS_SUCCESS) { K32SetConsoleLastError(s); @@ -3120,7 +3113,7 @@ __declspec(dllexport) DWORD GetFileType(HANDLE hFile) { #define EXCEPTION_CONTINUE_SEARCH 0 #define EXCEPTION_EXECUTE_HANDLER 1 #define EXCEPTION_NONCONTINUABLE 0x1UL -#define EXCEPTION_MAXIMUM_PARAMETERS SYS_EXCEPTION_MAXIMUM_PARAMETERS +#define EXCEPTION_MAXIMUM_PARAMETERS 15u #define WIN_VECTORED_MAX_HANDLERS 16u #define WIN_SEH_MAX_FRAMES 16u @@ -3239,10 +3232,61 @@ static uint32_t* WinpVectoredTailSlot(uint32_t isContinue) { return isContinue ? &gWinVectoredContinueTail : &gWinVectoredExceptionTail; } +// Windows exception values remain a Win32 personality ABI and are translated +// explicitly from the native nt-lite exception classes. +#define WIN_EXCEPTION_GUARD_PAGE_VIOLATION 0x80000001u +#define WIN_EXCEPTION_DATATYPE_MISALIGNMENT 0x80000002u +#define WIN_EXCEPTION_BREAKPOINT 0x80000003u +#define WIN_EXCEPTION_SINGLE_STEP 0x80000004u +#define WIN_EXCEPTION_ACCESS_VIOLATION 0xC0000005u +#define WIN_EXCEPTION_ILLEGAL_INSTRUCTION 0xC000001Du +#define WIN_EXCEPTION_ARRAY_BOUNDS_EXCEEDED 0xC000008Cu +#define WIN_EXCEPTION_FLT_DENORMAL_OPERAND 0xC000008Du +#define WIN_EXCEPTION_FLT_DIVIDE_BY_ZERO 0xC000008Eu +#define WIN_EXCEPTION_FLT_INEXACT_RESULT 0xC000008Fu +#define WIN_EXCEPTION_FLT_INVALID_OPERATION 0xC0000090u +#define WIN_EXCEPTION_FLT_OVERFLOW 0xC0000091u +#define WIN_EXCEPTION_FLT_STACK_CHECK 0xC0000092u +#define WIN_EXCEPTION_FLT_UNDERFLOW 0xC0000093u +#define WIN_EXCEPTION_INT_DIVIDE_BY_ZERO 0xC0000094u +#define WIN_EXCEPTION_INT_OVERFLOW 0xC0000095u +#define WIN_EXCEPTION_PRIVILEGED_INSTRUCTION 0xC0000096u +#define WIN_EXCEPTION_STACK_OVERFLOW 0xC00000FDu +#define WIN_EXCEPTION_NTLITE_UNKNOWN 0xE04E544Cu + +// Maps one native exception class to the Win32 exception namespace. +static DWORD WinpExceptionCodeFromNative(const SYS_EXCEPTION_RECORD* source) { + if (!source) return WIN_EXCEPTION_NTLITE_UNKNOWN; + if (source->Code == SYS_EXCEPTION_CODE_USER_RAISED) + return source->UserCode; + + switch (source->Code) { + case SYS_EXCEPTION_CODE_GUARD_PAGE_VIOLATION: return WIN_EXCEPTION_GUARD_PAGE_VIOLATION; + case SYS_EXCEPTION_CODE_DATATYPE_MISALIGNMENT: return WIN_EXCEPTION_DATATYPE_MISALIGNMENT; + case SYS_EXCEPTION_CODE_BREAKPOINT: return WIN_EXCEPTION_BREAKPOINT; + case SYS_EXCEPTION_CODE_SINGLE_STEP: return WIN_EXCEPTION_SINGLE_STEP; + case SYS_EXCEPTION_CODE_ACCESS_VIOLATION: return WIN_EXCEPTION_ACCESS_VIOLATION; + case SYS_EXCEPTION_CODE_ILLEGAL_INSTRUCTION: return WIN_EXCEPTION_ILLEGAL_INSTRUCTION; + case SYS_EXCEPTION_CODE_ARRAY_BOUNDS_EXCEEDED: return WIN_EXCEPTION_ARRAY_BOUNDS_EXCEEDED; + case SYS_EXCEPTION_CODE_FLT_DENORMAL_OPERAND: return WIN_EXCEPTION_FLT_DENORMAL_OPERAND; + case SYS_EXCEPTION_CODE_FLT_DIVIDE_BY_ZERO: return WIN_EXCEPTION_FLT_DIVIDE_BY_ZERO; + case SYS_EXCEPTION_CODE_FLT_INEXACT_RESULT: return WIN_EXCEPTION_FLT_INEXACT_RESULT; + case SYS_EXCEPTION_CODE_FLT_INVALID_OPERATION: return WIN_EXCEPTION_FLT_INVALID_OPERATION; + case SYS_EXCEPTION_CODE_FLT_OVERFLOW: return WIN_EXCEPTION_FLT_OVERFLOW; + case SYS_EXCEPTION_CODE_FLT_STACK_CHECK: return WIN_EXCEPTION_FLT_STACK_CHECK; + case SYS_EXCEPTION_CODE_FLT_UNDERFLOW: return WIN_EXCEPTION_FLT_UNDERFLOW; + case SYS_EXCEPTION_CODE_INT_DIVIDE_BY_ZERO: return WIN_EXCEPTION_INT_DIVIDE_BY_ZERO; + case SYS_EXCEPTION_CODE_INT_OVERFLOW: return WIN_EXCEPTION_INT_OVERFLOW; + case SYS_EXCEPTION_CODE_PRIVILEGED_INSTRUCTION: return WIN_EXCEPTION_PRIVILEGED_INSTRUCTION; + case SYS_EXCEPTION_CODE_STACK_OVERFLOW: return WIN_EXCEPTION_STACK_OVERFLOW; + default: return WIN_EXCEPTION_NTLITE_UNKNOWN; + } +} + // Copies native exception state into the Win32-facing exception record. static void WinpBuildExceptionRecord(const SYS_EXCEPTION_RECORD* source, EXCEPTION_RECORD* target) { memset(target, 0, sizeof(*target)); - target->ExceptionCode = source->Code; + target->ExceptionCode = WinpExceptionCodeFromNative(source); if (source->Flags & SYS_EXCEPTION_FLAG_NONCONTINUABLE) target->ExceptionFlags |= EXCEPTION_NONCONTINUABLE; target->ExceptionAddress = (void*)(uintptr_t)source->ExceptionAddress; @@ -3603,7 +3647,7 @@ __declspec(dllexport) void RaiseException(DWORD dwExceptionCode, DWORD dwExcepti SYS_RAISE_EXCEPTION_REQUEST request; memset(&request, 0, sizeof(request)); request.Size = sizeof(request); - request.Code = dwExceptionCode; + request.UserCode = dwExceptionCode; request.Flags = (dwExceptionFlags & EXCEPTION_NONCONTINUABLE) ? SYS_RAISE_EXCEPTION_FLAG_NONCONTINUABLE : SYS_RAISE_EXCEPTION_FLAG_NONE; diff --git a/code/user/runtime/posix_start.c b/code/user/runtime/posix_start.c index 5f3a196..a69d16b 100644 --- a/code/user/runtime/posix_start.c +++ b/code/user/runtime/posix_start.c @@ -1,12 +1,12 @@ // POSIX process startup. This file owns process-local POSIX personality initialization before main runs. #include +#include #include #include extern int main(void); -extern void exit(int status); -extern void PosixFdInit(void); +__declspec(dllimport) uint32_t __ntl_posix_process_start(void); // Initializes POSIX process state and exits with main's return value. void PosixStart(void) { @@ -15,7 +15,11 @@ void PosixStart(void) { while (true) SysExitProcess((int32_t)status); } - PosixFdInit(); + status = __ntl_posix_process_start(); + if (status != NTLSTATUS_SUCCESS) { + while (true) SysExitProcess((int32_t)status); + } + int exitCode = main(); exit(exitCode); while (true) {} diff --git a/code/user/syscall/console_service.c b/code/user/syscall/console_service.c new file mode 100644 index 0000000..fa4d1ca --- /dev/null +++ b/code/user/syscall/console_service.c @@ -0,0 +1,284 @@ +// Native console client. RSSRV owns the logical console; this file speaks its Port protocol. + +#include +#include +#include +#include +#include +#include + +static volatile long gDefaultConsoleSession; +static KSTATUS NtlConsoleGetDefaultSession(SYS_HANDLE* sessionOut); + +// Connects to RSSRV's native console service. +KSTATUS NtlConsoleConnect(SYS_HANDLE* sessionOut) { + if (!sessionOut) return NTLSTATUS_INVALID_PARAMETER; + *sessionOut = SYS_HANDLE_INVALID; + + SYS_HANDLE resourceSession = SYS_HANDLE_INVALID; + KSTATUS status = NtlResourceConnect(&resourceSession); + if (!KSUCCESS(status)) return status; + + SYS_HANDLE listener = SYS_HANDLE_INVALID; + status = NtlResourceLookup(resourceSession, SYS_SV_LITERAL(NTL_CONSOLE_SERVICE_PATH), + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT, &listener); + (void)NtlResourceClose(resourceSession); + if (!KSUCCESS(status)) return status; + + status = NtlResourceConnectListener(listener, sessionOut); + (void)SysCloseHandle(listener); + return status; +} + +// Disconnects one console service session. +KSTATUS NtlConsoleClose(SYS_HANDLE session) { + if (session == SYS_HANDLE_INVALID) return NTLSTATUS_INVALID_PARAMETER; + (void)SysDisconnectPort(session); + return SysCloseHandle(session); +} + +// Returns one process-local cached console session. +static KSTATUS NtlConsoleGetDefaultSession(SYS_HANDLE* sessionOut) { + if (!sessionOut) return NTLSTATUS_INVALID_PARAMETER; + long current = gDefaultConsoleSession; + if (current != 0) { + *sessionOut = (SYS_HANDLE)(uint32_t)current; + return NTLSTATUS_SUCCESS; + } + + SYS_HANDLE created = SYS_HANDLE_INVALID; + KSTATUS status = NtlConsoleConnect(&created); + if (!KSUCCESS(status)) return status; + + long previous = _InterlockedCompareExchange(&gDefaultConsoleSession, (long)created, 0); + if (previous != 0) { + (void)NtlConsoleClose(created); + created = (SYS_HANDLE)(uint32_t)previous; + } + *sessionOut = created; + return NTLSTATUS_SUCCESS; +} + +// Executes one fixed console request and validates the reply envelope. +static KSTATUS NtlConsoleCall(SYS_HANDLE session, + const NTL_CONSOLE_REQUEST* request, + NTL_CONSOLE_REPLY* replyOut) { + if (!request || !replyOut) return NTLSTATUS_INVALID_PARAMETER; + if (session == SYS_HANDLE_INVALID) { + KSTATUS status = NtlConsoleGetDefaultSession(&session); + if (!KSUCCESS(status)) return status; + } + + NTL_CONSOLE_REPLY reply; + memset(&reply, 0, sizeof(reply)); + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + + SYS_CALL_PORT_REQUEST call; + memset(&call, 0, sizeof(call)); + call.Size = sizeof(call); + call.EndpointHandle = session; + call.Flags = SYS_PORT_FLAG_NONE; + call.RequestPayload = request; + call.RequestLength = sizeof(*request); + call.ReplyPayload = &reply; + call.ReplyCapacity = sizeof(reply); + call.ReplyInfoOut = &info; + call.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysCallPort(&call); + if (!KSUCCESS(status)) return status; + + if (info.Type != SYS_PORT_MESSAGE_REPLY || info.PayloadLength != sizeof(reply) || info.HandleCount != 0 || + reply.Size != sizeof(reply) || reply.Version != NTL_CONSOLE_PROTOCOL_VERSION || + reply.Operation != request->Operation || reply.Flags != NTL_CONSOLE_FLAG_NONE) + return NTLSTATUS_FAILURE; + *replyOut = reply; + return reply.Status; +} + +// Writes arbitrary bytes to the console in bounded protocol chunks. +KSTATUS NtlConsoleWrite(SYS_HANDLE session, const void* data, size_t length, size_t* writtenOut) { + if (writtenOut) *writtenOut = 0; + if (length && !data) return NTLSTATUS_INVALID_PARAMETER; + const char* bytes = (const char*)data; + size_t total = 0; + while (total < length) { + size_t chunk = length - total; + if (chunk > NTL_CONSOLE_WRITE_MAX) chunk = NTL_CONSOLE_WRITE_MAX; + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_CONSOLE_PROTOCOL_VERSION; + request.Operation = NTL_CONSOLE_OP_WRITE; + request.Flags = NTL_CONSOLE_FLAG_NONE; + request.Data.Write.Length = (uint32_t)chunk; + memcpy(request.Data.Write.Data, bytes + total, chunk); + NTL_CONSOLE_REPLY reply; + memset(&reply, 0, sizeof(reply)); + KSTATUS status = NtlConsoleCall(session, &request, &reply); + total += reply.Written; + if (!KSUCCESS(status) || reply.Written != chunk) { + if (writtenOut) *writtenOut = total; + return KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + } + } + if (writtenOut) *writtenOut = total; + return NTLSTATUS_SUCCESS; +} + +// Queries the logical console's current dimensions and cursor state. +KSTATUS NtlConsoleQueryInfo(SYS_HANDLE session, NTL_CONSOLE_INFO* infoOut) { + if (!infoOut) return NTLSTATUS_INVALID_PARAMETER; + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_CONSOLE_PROTOCOL_VERSION; + request.Operation = NTL_CONSOLE_OP_QUERY_INFO; + NTL_CONSOLE_REPLY reply; + KSTATUS status = NtlConsoleCall(session, &request, &reply); + if (KSUCCESS(status)) *infoOut = reply.Info; + return status; +} + +// Executes one console operation whose reply has only status and optional written count. +static KSTATUS NtlConsoleSimpleCall(SYS_HANDLE session, NTL_CONSOLE_REQUEST* request, + uint32_t* writtenOut) { + NTL_CONSOLE_REPLY reply; + memset(&reply, 0, sizeof(reply)); + KSTATUS status = NtlConsoleCall(session, request, &reply); + if (writtenOut) *writtenOut = reply.Written; + return status; +} + +// Updates the logical console cursor. +KSTATUS NtlConsoleSetCursor(SYS_HANDLE session, int x, int y) { + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_CONSOLE_PROTOCOL_VERSION; + request.Operation = NTL_CONSOLE_OP_SET_CURSOR; + request.Data.Cursor.X = x; + request.Data.Cursor.Y = y; + return NtlConsoleSimpleCall(session, &request, 0); +} + +// Fills console characters over one linear cell range. +KSTATUS NtlConsoleFillCharacter(SYS_HANDLE session, char character, int x, int y, + size_t length, size_t* writtenOut) { + if (writtenOut) *writtenOut = 0; + if (length > UINT32_MAX) return NTLSTATUS_INVALID_PARAMETER; + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_CONSOLE_PROTOCOL_VERSION; + request.Operation = NTL_CONSOLE_OP_FILL_CHARACTER; + request.Data.FillCharacter.Character = character; + request.Data.FillCharacter.X = x; + request.Data.FillCharacter.Y = y; + request.Data.FillCharacter.Length = (uint32_t)length; + uint32_t written = 0; + KSTATUS status = NtlConsoleSimpleCall(session, &request, &written); + if (writtenOut) *writtenOut = written; + return status; +} + +// Fills console attributes over one linear cell range. +KSTATUS NtlConsoleFillAttribute(SYS_HANDLE session, uint16_t attribute, int x, int y, + size_t length, size_t* writtenOut) { + if (writtenOut) *writtenOut = 0; + if (length > UINT32_MAX) return NTLSTATUS_INVALID_PARAMETER; + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_CONSOLE_PROTOCOL_VERSION; + request.Operation = NTL_CONSOLE_OP_FILL_ATTRIBUTE; + request.Data.FillAttribute.Attribute = attribute; + request.Data.FillAttribute.X = x; + request.Data.FillAttribute.Y = y; + request.Data.FillAttribute.Length = (uint32_t)length; + uint32_t written = 0; + KSTATUS status = NtlConsoleSimpleCall(session, &request, &written); + if (writtenOut) *writtenOut = written; + return status; +} + +// Draws one glyph at explicit pixel coordinates without moving console state. +KSTATUS NtlConsolePutCharacter(SYS_HANDLE session, char character, int x, int y, + uint32_t foreground, uint32_t background) { + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_CONSOLE_PROTOCOL_VERSION; + request.Operation = NTL_CONSOLE_OP_PUT_CHARACTER; + request.Data.PutCharacter.Character = character; + request.Data.PutCharacter.X = x; + request.Data.PutCharacter.Y = y; + request.Data.PutCharacter.Foreground = foreground; + request.Data.PutCharacter.Background = background; + return NtlConsoleSimpleCall(session, &request, 0); +} + +// Clears the console's current display target. +KSTATUS NtlConsoleClear(SYS_HANDLE session, uint32_t color) { + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_CONSOLE_PROTOCOL_VERSION; + request.Operation = NTL_CONSOLE_OP_CLEAR; + request.Data.Clear.Color = color; + return NtlConsoleSimpleCall(session, &request, 0); +} + +// Executes one console input operation and returns its key record. +static KSTATUS NtlConsoleInputCall(SYS_HANDLE session, uint32_t operation, + uint64_t timeoutMilliseconds, INPUT_KEY_RECORD* recordOut) { + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_CONSOLE_PROTOCOL_VERSION; + request.Operation = operation; + request.TimeoutMilliseconds = timeoutMilliseconds; + NTL_CONSOLE_REPLY reply; + KSTATUS status = NtlConsoleCall(session, &request, &reply); + if (KSUCCESS(status) && recordOut) *recordOut = reply.Input; + return status; +} + +// Reads one logical console input record. +KSTATUS NtlConsoleReadInput(SYS_HANDLE session, uint64_t timeoutMilliseconds, + INPUT_KEY_RECORD* recordOut) { + if (!recordOut) return NTLSTATUS_INVALID_PARAMETER; + return NtlConsoleInputCall(session, NTL_CONSOLE_OP_READ_INPUT, timeoutMilliseconds, recordOut); +} + +// Peeks one logical console input record. +KSTATUS NtlConsolePeekInput(SYS_HANDLE session, uint64_t timeoutMilliseconds, + INPUT_KEY_RECORD* recordOut) { + if (!recordOut) return NTLSTATUS_INVALID_PARAMETER; + return NtlConsoleInputCall(session, NTL_CONSOLE_OP_PEEK_INPUT, timeoutMilliseconds, recordOut); +} + +// Discards every queued logical console input record. +KSTATUS NtlConsoleFlushInput(SYS_HANDLE session) { + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_CONSOLE_PROTOCOL_VERSION; + request.Operation = NTL_CONSOLE_OP_FLUSH_INPUT; + return NtlConsoleSimpleCall(session, &request, 0); +} + +// Reads until one console key record has a character representation. +KSTATUS NtlConsoleReadCharacter(SYS_HANDLE session, char* characterOut) { + if (!characterOut) return NTLSTATUS_INVALID_PARAMETER; + for (;;) { + INPUT_KEY_RECORD record; + KSTATUS status = NtlConsoleReadInput(session, SYS_PORT_TIMEOUT_INFINITE, &record); + if (!KSUCCESS(status)) return status; + if (record.Char != 0) { + *characterOut = record.Char; + return NTLSTATUS_SUCCESS; + } + } +} diff --git a/code/user/syscall/desktop.c b/code/user/syscall/desktop.c new file mode 100644 index 0000000..d5ea5af --- /dev/null +++ b/code/user/syscall/desktop.c @@ -0,0 +1,234 @@ +// Native desktop client helpers. The protocol stays capability-based and does not expose DWM process identity. + +#include +#include +#include +#include +#include + +// Performs one fixed desktop request and validates the fixed reply envelope. +static KSTATUS NtlDesktopCall(SYS_HANDLE session, + NTL_DESKTOP_REQUEST* request, + const SYS_PORT_HANDLE_TRANSFER* transfer, + uint32_t transferCount, + NTL_DESKTOP_REPLY* replyOut) { + if (session == SYS_HANDLE_INVALID || !request || !replyOut || transferCount > 1u || + (transferCount && !transfer)) + return NTLSTATUS_INVALID_PARAMETER; + + memset(replyOut, 0, sizeof(*replyOut)); + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + + SYS_CALL_PORT_REQUEST call; + memset(&call, 0, sizeof(call)); + call.Size = sizeof(call); + call.EndpointHandle = session; + call.Flags = SYS_PORT_FLAG_NONE; + call.RequestPayload = request; + call.RequestLength = sizeof(*request); + call.RequestHandles = transfer; + call.RequestHandleCount = transferCount; + call.ReplyPayload = replyOut; + call.ReplyCapacity = sizeof(*replyOut); + call.ReplyInfoOut = &info; + call.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysCallPort(&call); + if (!KSUCCESS(status)) return status; + if (info.Type != SYS_PORT_MESSAGE_REPLY || info.HandleCount != 0 || + info.PayloadLength != sizeof(*replyOut) || replyOut->Size != sizeof(*replyOut) || + replyOut->Version != NTL_DESKTOP_PROTOCOL_VERSION || + replyOut->Operation != request->Operation || replyOut->Flags != NTL_DESKTOP_FLAG_NONE) + return NTLSTATUS_FAILURE; + return replyOut->Status; +} + +// Initializes one request envelope. +static void NtlDesktopInitRequest(NTL_DESKTOP_REQUEST* request, uint32_t operation, NTL_WINDOW_ID window) { + memset(request, 0, sizeof(*request)); + request->Size = sizeof(*request); + request->Version = NTL_DESKTOP_PROTOCOL_VERSION; + request->Operation = operation; + request->Flags = NTL_DESKTOP_FLAG_NONE; + request->WindowId = window; +} + +// Connects to the active Session 0 desktop endpoint through RSSRV. +KSTATUS NtlDesktopConnect(SYS_HANDLE* sessionOut) { + if (!sessionOut) return NTLSTATUS_INVALID_PARAMETER; + *sessionOut = SYS_HANDLE_INVALID; + + SYS_HANDLE resource = SYS_HANDLE_INVALID; + SYS_HANDLE listener = SYS_HANDLE_INVALID; + KSTATUS status = NtlResourceConnect(&resource); + if (!KSUCCESS(status)) return status; + status = NtlResourceLookup(resource, SYS_SV_LITERAL(NTL_DESKTOP_SERVICE_PATH), + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT, &listener); + (void)NtlResourceClose(resource); + if (!KSUCCESS(status)) return status; + + SYS_CONNECT_PORT_REQUEST connect; + memset(&connect, 0, sizeof(connect)); + connect.Size = sizeof(connect); + connect.ListenerHandle = listener; + connect.Flags = SYS_PORT_CONNECT_FLAG_NONE; + connect.EndpointHandleOut = sessionOut; + status = SysConnectPort(&connect); + (void)SysCloseHandle(listener); + return status; +} + +// Creates one hidden client-owned top-level window. +KSTATUS NtlDesktopCreateWindow(SYS_HANDLE session, const DISPLAY_RECT* bounds, NTL_WINDOW_ID* windowOut) { + if (!bounds || !windowOut) return NTLSTATUS_INVALID_PARAMETER; + *windowOut = 0; + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_CREATE_WINDOW, 0); + request.Bounds = *bounds; + NTL_DESKTOP_REPLY reply; + KSTATUS status = NtlDesktopCall(session, &request, 0, 0, &reply); + if (KSUCCESS(status)) *windowOut = reply.WindowId; + return status; +} + +// Destroys one window owned by this connection. +KSTATUS NtlDesktopDestroyWindow(SYS_HANDLE session, NTL_WINDOW_ID window) { + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_DESTROY_WINDOW, window); + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Changes one window's screen-space bounds. +KSTATUS NtlDesktopSetBounds(SYS_HANDLE session, NTL_WINDOW_ID window, const DISPLAY_RECT* bounds) { + if (!bounds) return NTLSTATUS_INVALID_PARAMETER; + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_SET_BOUNDS, window); + request.Bounds = *bounds; + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Shows or hides one window without changing its z-order. +KSTATUS NtlDesktopSetVisible(SYS_HANDLE session, NTL_WINDOW_ID window, uint32_t visible) { + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_SET_VISIBLE, window); + request.Value = visible ? 1u : 0u; + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Moves one window to the top of the z-order. +KSTATUS NtlDesktopRaiseWindow(SYS_HANDLE session, NTL_WINDOW_ID window) { + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_RAISE_WINDOW, window); + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Moves one window to the bottom of the z-order. +KSTATUS NtlDesktopLowerWindow(SYS_HANDLE session, NTL_WINDOW_ID window) { + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_LOWER_WINDOW, window); + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Requests Session 0 keyboard focus for one visible window. +KSTATUS NtlDesktopRequestFocus(SYS_HANDLE session, NTL_WINDOW_ID window) { + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_REQUEST_FOCUS, window); + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Requests Session 0 pointer capture for one visible window. +KSTATUS NtlDesktopRequestCapture(SYS_HANDLE session, NTL_WINDOW_ID window) { + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_REQUEST_CAPTURE, window); + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Releases pointer capture only when this window currently owns it. +KSTATUS NtlDesktopReleaseCapture(SYS_HANDLE session, NTL_WINDOW_ID window) { + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_RELEASE_CAPTURE, window); + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Sets visible cursor policy for the focused/captured window. +KSTATUS NtlDesktopSetPointerMode(SYS_HANDLE session, NTL_WINDOW_ID window, uint32_t mode) { + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_SET_POINTER_MODE, window); + request.Value = mode; + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Attaches a shared surface. DWM receives only QUERY, WAIT, and MAP_READ rights. +KSTATUS NtlDesktopAttachSurface(SYS_HANDLE session, NTL_WINDOW_ID window, SYS_HANDLE surface) { + if (surface == SYS_HANDLE_INVALID) return NTLSTATUS_INVALID_PARAMETER; + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_ATTACH_SURFACE, window); + SYS_PORT_HANDLE_TRANSFER transfer; + transfer.Handle = surface; + transfer.DesiredRights = SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_SURFACE_RIGHT_MAP_READ; + transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, &transfer, 1u, &reply); +} + +// Detaches the current shared surface from one window. +KSTATUS NtlDesktopDetachSurface(SYS_HANDLE session, NTL_WINDOW_ID window) { + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_DETACH_SURFACE, window); + NTL_DESKTOP_REPLY reply; + return NtlDesktopCall(session, &request, 0, 0, &reply); +} + +// Queries one connection-owned window. +KSTATUS NtlDesktopQueryWindow(SYS_HANDLE session, NTL_WINDOW_ID window, NTL_DESKTOP_REPLY* replyOut) { + if (!replyOut) return NTLSTATUS_INVALID_PARAMETER; + NTL_DESKTOP_REQUEST request; + NtlDesktopInitRequest(&request, NTL_DESKTOP_OP_QUERY_WINDOW, window); + return NtlDesktopCall(session, &request, 0, 0, replyOut); +} + +// Receives one asynchronous key or mouse event from DWM. +KSTATUS NtlDesktopReceiveEvent(SYS_HANDLE session, uint64_t timeoutMilliseconds, NTL_DESKTOP_EVENT* eventOut) { + if (session == SYS_HANDLE_INVALID || !eventOut) return NTLSTATUS_INVALID_PARAMETER; + memset(eventOut, 0, sizeof(*eventOut)); + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = session; + receive.Flags = SYS_PORT_FLAG_NONE; + receive.Payload = eventOut; + receive.PayloadCapacity = sizeof(*eventOut); + receive.InfoOut = &info; + receive.TimeoutMilliseconds = timeoutMilliseconds; + KSTATUS status = SysReceivePort(&receive); + if (!KSUCCESS(status)) return status; + if (info.Type != SYS_PORT_MESSAGE_NOTIFICATION || info.HandleCount != 0 || + info.PayloadLength != sizeof(*eventOut) || eventOut->Size != sizeof(*eventOut) || + eventOut->Version != NTL_DESKTOP_PROTOCOL_VERSION || eventOut->Flags != NTL_DESKTOP_FLAG_NONE || + (eventOut->Type != NTL_DESKTOP_EVENT_KEY && eventOut->Type != NTL_DESKTOP_EVENT_MOUSE)) + return NTLSTATUS_FAILURE; + return NTLSTATUS_SUCCESS; +} + +// Disconnects and closes one desktop session endpoint. +KSTATUS NtlDesktopClose(SYS_HANDLE session) { + if (session == SYS_HANDLE_INVALID) return NTLSTATUS_INVALID_HANDLE; + KSTATUS disconnect = SysDisconnectPort(session); + KSTATUS close = SysCloseHandle(session); + if (!KSUCCESS(disconnect) && disconnect != NTLSTATUS_PORT_DISCONNECTED) + return disconnect; + return close; +} diff --git a/code/user/syscall/input_service.c b/code/user/syscall/input_service.c new file mode 100644 index 0000000..93000a6 --- /dev/null +++ b/code/user/syscall/input_service.c @@ -0,0 +1,250 @@ +// Native logical-input client. RSSRV owns keyboard interpretation and pointer policy; this file speaks its Port protocol. + +#include +#include +#include +#include +#include +#include + +static volatile long gDefaultInputSession; +static KSTATUS NtlInputGetDefaultSession(SYS_HANDLE* sessionOut); + +// Connects to the RSSRV listener published at /Services/Input. +KSTATUS NtlInputConnect(SYS_HANDLE* sessionOut) { + if (!sessionOut) return NTLSTATUS_INVALID_PARAMETER; + *sessionOut = SYS_HANDLE_INVALID; + + SYS_HANDLE resourceSession = SYS_HANDLE_INVALID; + KSTATUS status = NtlResourceConnect(&resourceSession); + if (!KSUCCESS(status)) return status; + + SYS_HANDLE listener = SYS_HANDLE_INVALID; + status = NtlResourceLookup(resourceSession, + SysSvFromCstr(NTL_INPUT_SERVICE_PATH), + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT, + &listener); + (void)NtlResourceClose(resourceSession); + if (!KSUCCESS(status)) return status; + + status = NtlResourceConnectListener(listener, sessionOut); + (void)SysCloseHandle(listener); + return status; +} + +// Disconnects one logical-input session. +KSTATUS NtlInputClose(SYS_HANDLE session) { + if (session == SYS_HANDLE_INVALID) return NTLSTATUS_INVALID_PARAMETER; + (void)SysDisconnectPort(session); + return SysCloseHandle(session); +} + +// Executes one fixed input-service request and validates its reply envelope. +static KSTATUS NtlInputCall(SYS_HANDLE session, + uint32_t operation, + uint64_t timeoutMilliseconds, + NTL_INPUT_REPLY* replyOut) { + if (session == SYS_HANDLE_INVALID || !replyOut) + return NTLSTATUS_INVALID_PARAMETER; + + NTL_INPUT_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_INPUT_PROTOCOL_VERSION; + request.Operation = operation; + request.Flags = NTL_INPUT_FLAG_NONE; + request.TimeoutMilliseconds = timeoutMilliseconds; + + NTL_INPUT_REPLY reply; + memset(&reply, 0, sizeof(reply)); + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + + SYS_CALL_PORT_REQUEST call; + memset(&call, 0, sizeof(call)); + call.Size = sizeof(call); + call.EndpointHandle = session; + call.Flags = SYS_PORT_FLAG_NONE; + call.RequestPayload = &request; + call.RequestLength = sizeof(request); + call.ReplyPayload = &reply; + call.ReplyCapacity = sizeof(reply); + call.ReplyInfoOut = &info; + call.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysCallPort(&call); + if (!KSUCCESS(status)) return status; + + if (info.Type != SYS_PORT_MESSAGE_REPLY || info.PayloadLength != sizeof(reply) || info.HandleCount != 0 || + reply.Size != sizeof(reply) || reply.Version != NTL_INPUT_PROTOCOL_VERSION || + reply.Operation != operation || reply.Flags != NTL_INPUT_FLAG_NONE) + return NTLSTATUS_FAILURE; + + *replyOut = reply; + return reply.Status; +} + +// Reads one logical keyboard event. +KSTATUS NtlInputReadEvent(SYS_HANDLE session, uint64_t timeoutMilliseconds, INPUT_KEY_RECORD* recordOut) { + if (!recordOut) return NTLSTATUS_INVALID_PARAMETER; + if (session == SYS_HANDLE_INVALID) { + KSTATUS connectStatus = NtlInputGetDefaultSession(&session); + if (!KSUCCESS(connectStatus)) return connectStatus; + } + NTL_INPUT_REPLY reply; + KSTATUS status = NtlInputCall(session, NTL_INPUT_OP_READ_KEY, timeoutMilliseconds, &reply); + if (KSUCCESS(status)) *recordOut = reply.Key; + return status; +} + +// Peeks one logical keyboard event. +KSTATUS NtlInputPeekEvent(SYS_HANDLE session, uint64_t timeoutMilliseconds, INPUT_KEY_RECORD* recordOut) { + if (!recordOut) return NTLSTATUS_INVALID_PARAMETER; + if (session == SYS_HANDLE_INVALID) { + KSTATUS connectStatus = NtlInputGetDefaultSession(&session); + if (!KSUCCESS(connectStatus)) return connectStatus; + } + NTL_INPUT_REPLY reply; + KSTATUS status = NtlInputCall(session, NTL_INPUT_OP_PEEK_KEY, timeoutMilliseconds, &reply); + if (KSUCCESS(status)) *recordOut = reply.Key; + return status; +} + +// Reads one logical mouse event. +KSTATUS NtlInputReadMouse(SYS_HANDLE session, uint64_t timeoutMilliseconds, INPUT_MOUSE_EVENT* eventOut) { + if (!eventOut) return NTLSTATUS_INVALID_PARAMETER; + if (session == SYS_HANDLE_INVALID) { + KSTATUS connectStatus = NtlInputGetDefaultSession(&session); + if (!KSUCCESS(connectStatus)) return connectStatus; + } + NTL_INPUT_REPLY reply; + KSTATUS status = NtlInputCall(session, NTL_INPUT_OP_READ_MOUSE, timeoutMilliseconds, &reply); + if (KSUCCESS(status)) *eventOut = reply.Mouse; + return status; +} + +// Peeks one logical mouse event. +KSTATUS NtlInputPeekMouse(SYS_HANDLE session, uint64_t timeoutMilliseconds, INPUT_MOUSE_EVENT* eventOut) { + if (!eventOut) return NTLSTATUS_INVALID_PARAMETER; + if (session == SYS_HANDLE_INVALID) { + KSTATUS connectStatus = NtlInputGetDefaultSession(&session); + if (!KSUCCESS(connectStatus)) return connectStatus; + } + NTL_INPUT_REPLY reply; + KSTATUS status = NtlInputCall(session, NTL_INPUT_OP_PEEK_MOUSE, timeoutMilliseconds, &reply); + if (KSUCCESS(status)) *eventOut = reply.Mouse; + return status; +} + +// Queries one RSSRV-owned logical input snapshot. +KSTATUS NtlInputQueryState(SYS_HANDLE session, NTL_INPUT_STATE* stateOut) { + if (!stateOut) return NTLSTATUS_INVALID_PARAMETER; + if (session == SYS_HANDLE_INVALID) { + KSTATUS connectStatus = NtlInputGetDefaultSession(&session); + if (!KSUCCESS(connectStatus)) return connectStatus; + } + NTL_INPUT_REPLY reply; + KSTATUS status = NtlInputCall(session, NTL_INPUT_OP_QUERY_STATE, 0, &reply); + if (KSUCCESS(status)) *stateOut = reply.State; + return status; +} + +// Returns one process-local cached input session, connecting it on first use. +static KSTATUS NtlInputGetDefaultSession(SYS_HANDLE* sessionOut) { + if (!sessionOut) return NTLSTATUS_INVALID_PARAMETER; + long current = gDefaultInputSession; + if (current != 0) { + *sessionOut = (SYS_HANDLE)(uint32_t)current; + return NTLSTATUS_SUCCESS; + } + + SYS_HANDLE created = SYS_HANDLE_INVALID; + KSTATUS status = NtlInputConnect(&created); + if (!KSUCCESS(status)) return status; + + long previous = _InterlockedCompareExchange(&gDefaultInputSession, (long)created, 0); + if (previous != 0) { + (void)NtlInputClose(created); + created = (SYS_HANDLE)(uint32_t)previous; + } + *sessionOut = created; + return NTLSTATUS_SUCCESS; +} + +// Peeks the next logical key without consuming it. +KSTATUS NtlInputPollKeyboard(INPUT_KEY* keyOut) { + if (!keyOut) return NTLSTATUS_INVALID_PARAMETER; + SYS_HANDLE session = SYS_HANDLE_INVALID; + KSTATUS status = NtlInputGetDefaultSession(&session); + if (!KSUCCESS(status)) return status; + INPUT_KEY_RECORD record; + status = NtlInputPeekEvent(session, 0, &record); + if (KSUCCESS(status)) *keyOut = record.Key; + return status; +} + +// Queries one logical key's current down state. +KSTATUS NtlInputIsKeyDown(INPUT_KEY key, bool* downOut) { + if (!downOut || key == INPUT_KEY_NONE || key >= NTL_INPUT_KEY_BYTES * 8u) + return NTLSTATUS_INVALID_PARAMETER; + NTL_INPUT_STATE state; + KSTATUS status = NtlInputQueryDefaultState(&state); + if (!KSUCCESS(status)) return status; + *downOut = (state.KeyDown[key >> 3] & (uint8_t)(1u << (key & 7u))) != 0; + return NTLSTATUS_SUCCESS; +} + +// Consumes logical key records until one has a character representation. +KSTATUS NtlInputReadKeyboardChar(char* charOut) { + if (!charOut) return NTLSTATUS_INVALID_PARAMETER; + SYS_HANDLE session = SYS_HANDLE_INVALID; + KSTATUS status = NtlInputGetDefaultSession(&session); + if (!KSUCCESS(status)) return status; + for (;;) { + INPUT_KEY_RECORD record; + status = NtlInputReadEvent(session, SYS_PORT_TIMEOUT_INFINITE, &record); + if (!KSUCCESS(status)) return status; + if (record.Char != 0) { + *charOut = record.Char; + return NTLSTATUS_SUCCESS; + } + } +} + +// Reads one logical key. +KSTATUS NtlInputReadKey(INPUT_KEY* keyOut) { + if (!keyOut) return NTLSTATUS_INVALID_PARAMETER; + SYS_HANDLE session = SYS_HANDLE_INVALID; + KSTATUS status = NtlInputGetDefaultSession(&session); + if (!KSUCCESS(status)) return status; + INPUT_KEY_RECORD record; + status = NtlInputReadEvent(session, SYS_PORT_TIMEOUT_INFINITE, &record); + if (KSUCCESS(status)) *keyOut = record.Key; + return status; +} + +// Returns the current logical modifier mask, or zero if the service cannot be queried. +uint32_t NtlInputGetKeyModifiers(void) { + NTL_INPUT_STATE state; + return KSUCCESS(NtlInputQueryDefaultState(&state)) ? state.Modifiers : 0u; +} + +// Queries the process-default logical input session. +KSTATUS NtlInputQueryDefaultState(NTL_INPUT_STATE* stateOut) { + if (!stateOut) return NTLSTATUS_INVALID_PARAMETER; + SYS_HANDLE session = SYS_HANDLE_INVALID; + KSTATUS status = NtlInputGetDefaultSession(&session); + if (!KSUCCESS(status)) return status; + return NtlInputQueryState(session, stateOut); +} + +// Returns the current RSSRV-owned logical pointer position. +KSTATUS NtlInputGetMousePos(int* xOut, int* yOut) { + if (!xOut || !yOut) return NTLSTATUS_INVALID_PARAMETER; + NTL_INPUT_STATE state; + KSTATUS status = NtlInputQueryDefaultState(&state); + if (!KSUCCESS(status)) return status; + *xOut = state.MouseX; + *yOut = state.MouseY; + return NTLSTATUS_SUCCESS; +} diff --git a/code/user/syscall/module_loader.c b/code/user/syscall/module_loader.c index e7bfe5e..7402c31 100644 --- a/code/user/syscall/module_loader.c +++ b/code/user/syscall/module_loader.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -12,7 +13,7 @@ #define NTL_MODULE_CONTEXT_MAGIC 0x4D4F444Cu #define NTL_MODULE_PAGE_SIZE 4096u #define NTL_LOADER_DATA_MAGIC 0x30444C4Eu -#define NTL_LOADER_DATA_VERSION 2u +#define NTL_LOADER_DATA_VERSION 3u typedef struct NTL_LOADER_IMAGE_RECORD { uintptr_t Base; @@ -28,7 +29,8 @@ typedef struct NTL_LOADER_DATA { uint32_t ImageCount; uint32_t CurrentDirectoryOffset; uint32_t CurrentDirectoryLength; - uint32_t Reserved[2]; + uint32_t StartupDataOffset; + uint32_t StartupDataLength; NTL_LOADER_IMAGE_RECORD Images[]; } NTL_LOADER_DATA; @@ -418,7 +420,12 @@ KSTATUS NtlpCreateModuleContext(SYS_HANDLE processHandle, data->CurrentDirectoryOffset > data->Size || data->CurrentDirectoryLength > data->Size - data->CurrentDirectoryOffset || data->CurrentDirectoryLength == data->Size - data->CurrentDirectoryOffset || - ((const char*)data)[data->CurrentDirectoryOffset + data->CurrentDirectoryLength] != '\0') { + ((const char*)data)[data->CurrentDirectoryOffset + data->CurrentDirectoryLength] != '\0' || + (data->StartupDataLength == 0 && data->StartupDataOffset != 0) || + (data->StartupDataLength != 0 && + (data->StartupDataOffset < data->CurrentDirectoryOffset + data->CurrentDirectoryLength + 1u || + data->StartupDataOffset > data->Size || + data->StartupDataLength > data->Size - data->StartupDataOffset))) { NtlpFree(context); return NTLSTATUS_FAILURE; } @@ -491,12 +498,15 @@ KSTATUS NtlpLoadMainImage(NTL_MODULE_CONTEXT* context, // Serializes startup dependency metadata into a read-only child-owned VAD. KSTATUS NtlpBuildLoaderData(NTL_MODULE_CONTEXT* context, SYS_STRING_VIEW currentDirectory, + const void* startupData, + size_t startupDataSize, void** loaderDataOut) { if (!context || context->Magic != NTL_MODULE_CONTEXT_MAGIC || !loaderDataOut || context->ProcessHandle == SYS_HANDLE_INVALID || !SysSvIsValid(currentDirectory) || currentDirectory.Length == 0 || currentDirectory.Length > SYS_PATH_MAX || currentDirectory.Data[0] != '/' || SysSvContainsByte(currentDirectory, '\\') || - SysSvContainsByte(currentDirectory, '\0')) + SysSvContainsByte(currentDirectory, '\0') || startupDataSize > NTL_PROCESS_STARTUP_DATA_MAX || + (startupDataSize && !startupData)) return NTLSTATUS_INVALID_PARAMETER; *loaderDataOut = 0; @@ -508,6 +518,12 @@ KSTATUS NtlpBuildLoaderData(NTL_MODULE_CONTEXT* context, return NTLSTATUS_NO_MEMORY; size_t currentDirectoryOffset = bytes; bytes += currentDirectory.Length + 1u; + size_t startupDataOffset = 0; + if (startupDataSize) { + if (startupDataSize > SIZE_MAX - bytes) return NTLSTATUS_NO_MEMORY; + startupDataOffset = bytes; + bytes += startupDataSize; + } if (bytes > UINT32_MAX) return NTLSTATUS_NO_MEMORY; if (bytes > SIZE_MAX - (NTL_MODULE_PAGE_SIZE - 1u)) return NTLSTATUS_NO_MEMORY; size_t pages = (bytes + NTL_MODULE_PAGE_SIZE - 1u) / NTL_MODULE_PAGE_SIZE; @@ -532,6 +548,8 @@ KSTATUS NtlpBuildLoaderData(NTL_MODULE_CONTEXT* context, data->ImageCount = context->Count; data->CurrentDirectoryOffset = (uint32_t)currentDirectoryOffset; data->CurrentDirectoryLength = (uint32_t)currentDirectory.Length; + data->StartupDataOffset = startupDataSize ? (uint32_t)startupDataOffset : 0u; + data->StartupDataLength = (uint32_t)startupDataSize; uint32_t i = 0; for (NTL_MODULE_ENTRY* entry = context->Head; entry; entry = entry->Next) { NTL_LOADER_IMAGE_RECORD* image = &data->Images[i++]; @@ -543,6 +561,8 @@ KSTATUS NtlpBuildLoaderData(NTL_MODULE_CONTEXT* context, char* cwd = (char*)data + currentDirectoryOffset; memcpy(cwd, currentDirectory.Data, currentDirectory.Length); cwd[currentDirectory.Length] = '\0'; + if (startupDataSize) + memcpy((char*)data + startupDataOffset, startupData, startupDataSize); status = SysProtectProcessVm(context->ProcessHandle, base, pages, SYS_VM_READ); if (!KSUCCESS(status)) return status; @@ -550,6 +570,48 @@ KSTATUS NtlpBuildLoaderData(NTL_MODULE_CONTEXT* context, return NTLSTATUS_SUCCESS; } +// Copies the opaque userspace bootstrap payload from the current process loader block. +KSTATUS NtlGetProcessStartupData(void* buffer, size_t bufferSize, size_t* sizeOut) { + if (!sizeOut || (bufferSize && !buffer)) return NTLSTATUS_INVALID_PARAMETER; + *sizeOut = 0; + + SYS_PEB* peb = SysGetPeb(); + if (!peb || peb->Magic != SYS_PEB_MAGIC || peb->Version != SYS_ENV_VERSION || !peb->LoaderData) + return NTLSTATUS_NOT_FOUND; + + SYS_VM_REGION_INFO region; + memset(®ion, 0, sizeof(region)); + KSTATUS status = SysQueryVm((void*)peb->LoaderData, ®ion); + if (!KSUCCESS(status) || !region.Base || !region.PageCount || + region.PageCount > SIZE_MAX / NTL_MODULE_PAGE_SIZE) + return NTLSTATUS_FAILURE; + + size_t regionBytes = region.PageCount * NTL_MODULE_PAGE_SIZE; + uintptr_t regionBase = (uintptr_t)region.Base; + uintptr_t dataBase = (uintptr_t)peb->LoaderData; + if (dataBase < regionBase || dataBase - regionBase > regionBytes || + regionBytes - (dataBase - regionBase) < sizeof(NTL_LOADER_DATA)) + return NTLSTATUS_FAILURE; + + const NTL_LOADER_DATA* data = (const NTL_LOADER_DATA*)peb->LoaderData; + if (data->Magic != NTL_LOADER_DATA_MAGIC || data->Version != NTL_LOADER_DATA_VERSION || + data->Size < sizeof(*data) || data->Size > regionBytes - (dataBase - regionBase) || + data->StartupDataLength > NTL_PROCESS_STARTUP_DATA_MAX || + (data->StartupDataLength == 0 && data->StartupDataOffset != 0) || + (data->StartupDataLength != 0 && + (data->StartupDataOffset < sizeof(*data) || data->StartupDataOffset > data->Size || + data->StartupDataLength > data->Size - data->StartupDataOffset))) + return NTLSTATUS_FAILURE; + if (data->StartupDataLength == 0) + return NTLSTATUS_NOT_FOUND; + + *sizeOut = data->StartupDataLength; + if (bufferSize < data->StartupDataLength) + return NTLSTATUS_BUFFER_TOO_SMALL; + memcpy(buffer, (const char*)data + data->StartupDataOffset, data->StartupDataLength); + return NTLSTATUS_SUCCESS; +} + // Returns the canonical inherited resource CWD stored in this process's loader data. KSTATUS NtlpGetLoaderCurrentDirectory(SYS_STRING_VIEW* currentDirectoryOut) { if (!currentDirectoryOut) return NTLSTATUS_INVALID_PARAMETER; diff --git a/code/user/syscall/module_loader_internal.h b/code/user/syscall/module_loader_internal.h index fd930f0..126945a 100644 --- a/code/user/syscall/module_loader_internal.h +++ b/code/user/syscall/module_loader_internal.h @@ -25,6 +25,8 @@ KSTATUS NtlpLoadMainImage(NTL_MODULE_CONTEXT* context, // Builds the opaque read-only loader-data block consumed later by userspace base services. KSTATUS NtlpBuildLoaderData(NTL_MODULE_CONTEXT* context, SYS_STRING_VIEW currentDirectory, + const void* startupData, + size_t startupDataSize, void** loaderDataOut); // Returns the inherited canonical resource CWD stored in the current process loader data. KSTATUS NtlpGetLoaderCurrentDirectory(SYS_STRING_VIEW* currentDirectoryOut); diff --git a/code/user/syscall/process_loader.c b/code/user/syscall/process_loader.c index 5733e3d..db7e1e7 100644 --- a/code/user/syscall/process_loader.c +++ b/code/user/syscall/process_loader.c @@ -125,7 +125,8 @@ static KSTATUS NtlCreateProcessFile(const NTL_CREATE_PROCESS_AT_REQUEST* request } void* loaderData = 0; - status = NtlpBuildLoaderData(modules, currentDirectory, &loaderData); + status = NtlpBuildLoaderData(modules, currentDirectory, request->StartupData, + request->StartupDataSize, &loaderData); if (!KSUCCESS(status)) { NtlpDestroyModuleContext(modules); NtlDiscardChild(processHandle); @@ -164,6 +165,8 @@ KSTATUS NtlCreateProcessAt(const NTL_CREATE_PROCESS_AT_REQUEST* request) { !SysSvIsValid(request->ModuleDirectory) || request->StartupCapabilityCount > SYS_STARTUP_CAPABILITY_MAX || (request->StartupCapabilityCount && !request->StartupCapabilities) || + request->StartupDataSize > NTL_PROCESS_STARTUP_DATA_MAX || + (request->StartupDataSize && !request->StartupData) || !request->ProcessHandleOut) return NTLSTATUS_INVALID_PARAMETER; @@ -191,7 +194,8 @@ KSTATUS NtlCreateProcessAt(const NTL_CREATE_PROCESS_AT_REQUEST* request) { KSTATUS NtlCreateSystemProcess(const NTL_CREATE_SYSTEM_PROCESS_REQUEST* request) { if (!request || request->Size != sizeof(*request) || request->Flags != NTL_PROCESS_LOAD_FLAG_NONE || !request->Path.Data || - request->Path.Length == 0 || !request->ProcessHandleOut) + request->Path.Length == 0 || request->StartupDataSize > NTL_PROCESS_STARTUP_DATA_MAX || + (request->StartupDataSize && !request->StartupData) || !request->ProcessHandleOut) return NTLSTATUS_INVALID_PARAMETER; *request->ProcessHandleOut = SYS_HANDLE_INVALID; @@ -215,6 +219,8 @@ KSTATUS NtlCreateSystemProcess(const NTL_CREATE_SYSTEM_PROCESS_REQUEST* request) create.ResolveContext = request->ResolveContext; create.StartupCapabilities = request->StartupCapabilities; create.StartupCapabilityCount = request->StartupCapabilityCount; + create.StartupData = request->StartupData; + create.StartupDataSize = request->StartupDataSize; create.ProcessHandleOut = request->ProcessHandleOut; if (request->ImageFile != SYS_HANDLE_INVALID) { status = NtlCreateProcessFile(&create, request->ImageFile); diff --git a/code/user/syscall/resource.c b/code/user/syscall/resource.c index 4d32613..6c8e3ca 100644 --- a/code/user/syscall/resource.c +++ b/code/user/syscall/resource.c @@ -5,10 +5,12 @@ #include #include -// Validates the canonical global resource-path syntax understood by rssrv. +// Validates canonical global resource-path syntax. The namespace root is represented by exactly "/". static KSTATUS NtlValidateResourcePath(SYS_STRING_VIEW path) { - if (!path.Data || path.Length < 2 || path.Length > NTL_RESOURCE_PATH_MAX || path.Data[0] != '/') + if (!path.Data || path.Length == 0 || path.Length > NTL_RESOURCE_PATH_MAX || path.Data[0] != '/') return NTLSTATUS_INVALID_PARAMETER; + if (path.Length == 1) + return NTLSTATUS_SUCCESS; if (path.Data[path.Length - 1] == '/') return NTLSTATUS_INVALID_PARAMETER; @@ -23,7 +25,7 @@ static KSTATUS NtlValidateResourcePath(SYS_STRING_VIEW path) { } size_t componentLength = i - componentStart; - if (componentLength == 0 || + if (componentLength == 0 || componentLength > NTL_RESOURCE_NAME_MAX || (componentLength == 1 && path.Data[componentStart] == '.') || (componentLength == 2 && path.Data[componentStart] == '.' && path.Data[componentStart + 1] == '.')) return NTLSTATUS_INVALID_PARAMETER; @@ -32,20 +34,21 @@ static KSTATUS NtlValidateResourcePath(SYS_STRING_VIEW path) { return NTLSTATUS_SUCCESS; } -// Builds one inline request header and path payload. +// Builds one inline request header with a path and optional operation-specific payload. static KSTATUS NtlBuildResourceRequest(uint8_t* buffer, size_t bufferSize, uint32_t operation, SYS_STRING_VIEW path, + SYS_STRING_VIEW aux, SYS_RIGHTS rights, SYS_RIGHTS exportRights, uint32_t kind, uint32_t* lengthOut) { KSTATUS status = NtlValidateResourcePath(path); - if (!KSUCCESS(status) || !buffer || !lengthOut) + if (!KSUCCESS(status) || !buffer || !lengthOut || !SysSvIsValid(aux)) return KSUCCESS(status) ? NTLSTATUS_INVALID_PARAMETER : status; - size_t total = sizeof(NTL_RESOURCE_REQUEST) + path.Length; - if (total > bufferSize || total > SYS_PORT_MAX_INLINE_BYTES) + size_t total = sizeof(NTL_RESOURCE_REQUEST) + path.Length + aux.Length; + if (total > bufferSize || total > SYS_PORT_MAX_INLINE_BYTES || aux.Length > UINT32_MAX) return NTLSTATUS_BUFFER_TOO_SMALL; NTL_RESOURCE_REQUEST header; @@ -58,8 +61,11 @@ static KSTATUS NtlBuildResourceRequest(uint8_t* buffer, header.ExportRights = exportRights; header.Kind = kind; header.PathLength = (uint32_t)path.Length; + header.AuxLength = (uint32_t)aux.Length; memcpy(buffer, &header, sizeof(header)); memcpy(buffer + sizeof(header), path.Data, path.Length); + if (aux.Length) + memcpy(buffer + sizeof(header) + path.Length, aux.Data, aux.Length); *lengthOut = (uint32_t)total; return NTLSTATUS_SUCCESS; } @@ -102,7 +108,7 @@ static KSTATUS NtlCallResource(SYS_HANDLE session, return status; if (info.Type != SYS_PORT_MESSAGE_REPLY || info.PayloadLength != sizeof(reply) || - reply.Size != sizeof(reply) || reply.Version != NTL_RESOURCE_PROTOCOL_VERSION) { + reply.Size != sizeof(reply) || reply.Version != NTL_RESOURCE_PROTOCOL_VERSION || reply.NameLength != 0) { if (replyHandle != SYS_HANDLE_INVALID) (void)SysCloseHandle(replyHandle); return NTLSTATUS_FAILURE; } @@ -164,7 +170,8 @@ KSTATUS NtlResourceLookup(SYS_HANDLE session, uint8_t payload[sizeof(NTL_RESOURCE_REQUEST) + NTL_RESOURCE_PATH_MAX]; uint32_t payloadLength = 0; KSTATUS status = NtlBuildResourceRequest(payload, sizeof(payload), NTL_RESOURCE_OP_LOOKUP, - path, desiredRights, 0, NTL_RESOURCE_KIND_INVALID, + path, SysSvFromParts((const char*)0, 0), + desiredRights, 0, NTL_RESOURCE_KIND_INVALID, &payloadLength); if (!KSUCCESS(status)) return status; @@ -186,14 +193,15 @@ KSTATUS NtlResourceLookup(SYS_HANDLE session, return NTLSTATUS_SUCCESS; } -// Queries one bound resource kind without transferring authority. +// Queries one exact namespace node kind without transferring authority. KSTATUS NtlResourceQuery(SYS_HANDLE session, SYS_STRING_VIEW path, uint32_t* kindOut) { if (!kindOut) return NTLSTATUS_INVALID_PARAMETER; *kindOut = NTL_RESOURCE_KIND_INVALID; uint8_t payload[sizeof(NTL_RESOURCE_REQUEST) + NTL_RESOURCE_PATH_MAX]; uint32_t payloadLength = 0; KSTATUS status = NtlBuildResourceRequest(payload, sizeof(payload), NTL_RESOURCE_OP_QUERY, - path, 0, 0, NTL_RESOURCE_KIND_INVALID, &payloadLength); + path, SysSvFromParts((const char*)0, 0), + 0, 0, NTL_RESOURCE_KIND_INVALID, &payloadLength); if (!KSUCCESS(status)) return status; NTL_RESOURCE_REPLY reply; @@ -213,7 +221,7 @@ KSTATUS NtlResourceResolve(SYS_HANDLE session, SYS_HANDLE* rootHandleOut, uint32_t* kindOut, uint32_t* resolvedLengthOut) { - if (!rootHandleOut || !kindOut || !resolvedLengthOut || + if (!rootHandleOut || !kindOut || !resolvedLengthOut || path.Length < 2 || (flags & ~NTL_RESOURCE_RESOLVE_FLAG_MODIFY) != 0) return NTLSTATUS_INVALID_PARAMETER; *rootHandleOut = SYS_HANDLE_INVALID; @@ -223,7 +231,8 @@ KSTATUS NtlResourceResolve(SYS_HANDLE session, uint8_t payload[sizeof(NTL_RESOURCE_REQUEST) + NTL_RESOURCE_PATH_MAX]; uint32_t payloadLength = 0; KSTATUS status = NtlBuildResourceRequest(payload, sizeof(payload), NTL_RESOURCE_OP_RESOLVE, - path, 0, 0, NTL_RESOURCE_KIND_INVALID, &payloadLength); + path, SysSvFromParts((const char*)0, 0), + 0, 0, NTL_RESOURCE_KIND_INVALID, &payloadLength); if (!KSUCCESS(status)) return status; ((NTL_RESOURCE_REQUEST*)payload)->Flags = flags; @@ -253,6 +262,104 @@ KSTATUS NtlResourceResolve(SYS_HANDLE session, return NTLSTATUS_SUCCESS; } +// Enumerates one immediate namespace child after the cursor's last returned bytewise name. +KSTATUS NtlResourceEnumerate(SYS_HANDLE session, + SYS_STRING_VIEW directoryPath, + NTL_RESOURCE_ENUM_CURSOR* cursor, + NTL_RESOURCE_ENUM_ENTRY* entryOut) { + if (session == SYS_HANDLE_INVALID || !cursor || !entryOut || + cursor->NameLength > NTL_RESOURCE_NAME_MAX || cursor->Name[cursor->NameLength] != '\0') + return NTLSTATUS_INVALID_PARAMETER; + memset(entryOut, 0, sizeof(*entryOut)); + + SYS_STRING_VIEW after = SysSvFromParts(cursor->Name, cursor->NameLength); + uint8_t requestPayload[sizeof(NTL_RESOURCE_REQUEST) + NTL_RESOURCE_PATH_MAX + NTL_RESOURCE_NAME_MAX]; + uint32_t requestLength = 0; + KSTATUS status = NtlBuildResourceRequest(requestPayload, sizeof(requestPayload), NTL_RESOURCE_OP_ENUMERATE, + directoryPath, after, 0, 0, NTL_RESOURCE_KIND_INVALID, + &requestLength); + if (!KSUCCESS(status)) return status; + + uint8_t replyPayload[sizeof(NTL_RESOURCE_REPLY) + NTL_RESOURCE_NAME_MAX]; + memset(replyPayload, 0, sizeof(replyPayload)); + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + + SYS_CALL_PORT_REQUEST call; + memset(&call, 0, sizeof(call)); + call.Size = sizeof(call); + call.EndpointHandle = session; + call.Flags = SYS_PORT_FLAG_NONE; + call.RequestPayload = requestPayload; + call.RequestLength = requestLength; + call.ReplyPayload = replyPayload; + call.ReplyCapacity = sizeof(replyPayload); + call.ReplyInfoOut = &info; + call.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + status = SysCallPort(&call); + if (!KSUCCESS(status)) return status; + if (info.Type != SYS_PORT_MESSAGE_REPLY || info.HandleCount != 0 || info.PayloadLength < sizeof(NTL_RESOURCE_REPLY)) + return NTLSTATUS_FAILURE; + + NTL_RESOURCE_REPLY reply; + memcpy(&reply, replyPayload, sizeof(reply)); + if (reply.Size != sizeof(reply) || reply.Version != NTL_RESOURCE_PROTOCOL_VERSION || + reply.Operation != NTL_RESOURCE_OP_ENUMERATE || reply.Flags != NTL_RESOURCE_FLAG_NONE || + reply.ResolvedLength != 0 || reply.NameLength > NTL_RESOURCE_NAME_MAX || + info.PayloadLength != sizeof(reply) + reply.NameLength) + return NTLSTATUS_FAILURE; + if (!KSUCCESS(reply.Status)) { + if (reply.NameLength != 0) + return NTLSTATUS_FAILURE; + return reply.Status; + } + if (reply.NameLength == 0 || reply.Kind == NTL_RESOURCE_KIND_INVALID) + return NTLSTATUS_FAILURE; + + memcpy(entryOut->Name, replyPayload + sizeof(reply), reply.NameLength); + entryOut->Name[reply.NameLength] = '\0'; + entryOut->Kind = reply.Kind; + memcpy(cursor->Name, entryOut->Name, reply.NameLength + 1u); + cursor->NameLength = reply.NameLength; + return NTLSTATUS_SUCCESS; +} + +// Creates one namespace directory through an administrative resource session. +KSTATUS NtlResourceCreateDirectory(SYS_HANDLE session, SYS_STRING_VIEW path) { + uint8_t payload[sizeof(NTL_RESOURCE_REQUEST) + NTL_RESOURCE_PATH_MAX]; + uint32_t payloadLength = 0; + KSTATUS status = NtlBuildResourceRequest(payload, sizeof(payload), NTL_RESOURCE_OP_CREATE_DIRECTORY, + path, SysSvFromParts((const char*)0, 0), + 0, 0, NTL_RESOURCE_KIND_INVALID, &payloadLength); + if (!KSUCCESS(status)) return status; + + NTL_RESOURCE_REPLY reply; + status = NtlCallResource(session, payload, payloadLength, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, + &reply, (SYS_HANDLE*)0); + if (!KSUCCESS(status)) return status; + if (reply.Operation != NTL_RESOURCE_OP_CREATE_DIRECTORY) return NTLSTATUS_FAILURE; + return reply.Status; +} + +// Creates one absolute namespace symbolic link through an administrative resource session. +KSTATUS NtlResourceCreateSymbolicLink(SYS_HANDLE session, SYS_STRING_VIEW path, SYS_STRING_VIEW target) { + KSTATUS status = NtlValidateResourcePath(target); + if (!KSUCCESS(status)) return status; + uint8_t payload[sizeof(NTL_RESOURCE_REQUEST) + NTL_RESOURCE_PATH_MAX * 2u]; + uint32_t payloadLength = 0; + status = NtlBuildResourceRequest(payload, sizeof(payload), NTL_RESOURCE_OP_CREATE_SYMBOLIC_LINK, + path, target, 0, 0, NTL_RESOURCE_KIND_INVALID, &payloadLength); + if (!KSUCCESS(status)) return status; + + NTL_RESOURCE_REPLY reply; + status = NtlCallResource(session, payload, payloadLength, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, + &reply, (SYS_HANDLE*)0); + if (!KSUCCESS(status)) return status; + if (reply.Operation != NTL_RESOURCE_OP_CREATE_SYMBOLIC_LINK) return NTLSTATUS_FAILURE; + return reply.Status; +} + // Binds an explicitly attenuated capability into an administrative resource session. KSTATUS NtlResourceBind(SYS_HANDLE session, SYS_STRING_VIEW path, @@ -260,14 +367,16 @@ KSTATUS NtlResourceBind(SYS_HANDLE session, SYS_HANDLE resourceHandle, SYS_RIGHTS namespaceRights, SYS_RIGHTS exportRights) { - if (kind == NTL_RESOURCE_KIND_INVALID || resourceHandle == SYS_HANDLE_INVALID || + if (kind == NTL_RESOURCE_KIND_INVALID || kind == NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY || + kind == NTL_RESOURCE_KIND_SYMBOLIC_LINK || resourceHandle == SYS_HANDLE_INVALID || !(namespaceRights & SYS_RIGHT_TRANSFER) || !exportRights || (exportRights & ~namespaceRights) != 0) return NTLSTATUS_INVALID_PARAMETER; uint8_t payload[sizeof(NTL_RESOURCE_REQUEST) + NTL_RESOURCE_PATH_MAX]; uint32_t payloadLength = 0; KSTATUS status = NtlBuildResourceRequest(payload, sizeof(payload), NTL_RESOURCE_OP_BIND, - path, namespaceRights, exportRights, kind, &payloadLength); + path, SysSvFromParts((const char*)0, 0), + namespaceRights, exportRights, kind, &payloadLength); if (!KSUCCESS(status)) return status; SYS_PORT_HANDLE_TRANSFER transfer; @@ -281,12 +390,13 @@ KSTATUS NtlResourceBind(SYS_HANDLE session, return reply.Status; } -// Removes one resource name through an administrative session. +// Removes one non-root namespace node through an administrative session. KSTATUS NtlResourceUnbind(SYS_HANDLE session, SYS_STRING_VIEW path) { uint8_t payload[sizeof(NTL_RESOURCE_REQUEST) + NTL_RESOURCE_PATH_MAX]; uint32_t payloadLength = 0; KSTATUS status = NtlBuildResourceRequest(payload, sizeof(payload), NTL_RESOURCE_OP_UNBIND, - path, 0, 0, NTL_RESOURCE_KIND_INVALID, &payloadLength); + path, SysSvFromParts((const char*)0, 0), + 0, 0, NTL_RESOURCE_KIND_INVALID, &payloadLength); if (!KSUCCESS(status)) return status; NTL_RESOURCE_REPLY reply; diff --git a/code/user/syscall/syscall_nr.inc b/code/user/syscall/syscall_nr.inc index 54dc21d..12b96cf 100644 --- a/code/user/syscall/syscall_nr.inc +++ b/code/user/syscall/syscall_nr.inc @@ -18,164 +18,182 @@ SYS_GET_THREAD_LIST EQU 13 SYS_GET_CURRENT_TID EQU 14 SYS_GET_CURRENT_PID EQU 15 SYS_SET_THREAD_PRIORITY EQU 16 -SYS_YIELD EQU 17 -SYS_SLEEP EQU 18 -SYS_WAIT_SINGLE EQU 19 -SYS_WAIT_MANY EQU 20 -SYS_SET_USER_APC_DISPATCHER EQU 21 -SYS_APC_RETURN EQU 22 -SYS_SET_EXCEPTION_DISPATCHER EQU 23 -SYS_GET_EXCEPTION_DISPATCHER EQU 24 -SYS_CONTINUE_EXCEPTION EQU 25 -SYS_RAISE_EXCEPTION EQU 26 +SYS_SET_THREAD_NAME EQU 17 +SYS_QUERY_THREAD_NAME EQU 18 +SYS_YIELD EQU 19 +SYS_SLEEP EQU 20 +SYS_WAIT_SINGLE EQU 21 +SYS_WAIT_MANY EQU 22 +SYS_SET_USER_APC_DISPATCHER EQU 23 +SYS_APC_RETURN EQU 24 +SYS_SET_EXCEPTION_DISPATCHER EQU 25 +SYS_GET_EXCEPTION_DISPATCHER EQU 26 +SYS_CONTINUE_EXCEPTION EQU 27 +SYS_RAISE_EXCEPTION EQU 28 ; Virtual memory, mappings, and handles. -SYS_ALLOC_VM EQU 27 -SYS_FREE_VM EQU 28 -SYS_PROTECT_VM EQU 29 -SYS_QUERY_VM EQU 30 -SYS_CLOSE_HANDLE EQU 31 -SYS_DUPLICATE_HANDLE EQU 32 -SYS_DERIVE_HANDLE EQU 33 -SYS_REVOKE_HANDLE EQU 34 -SYS_DERIVE_SECURITY_CONTEXT EQU 35 -SYS_QUERY_SECURITY_CONTEXT EQU 36 -SYS_CREATE_FILE_MAPPING EQU 37 -SYS_MAP_FILE EQU 38 +SYS_ALLOC_VM EQU 29 +SYS_FREE_VM EQU 30 +SYS_PROTECT_VM EQU 31 +SYS_QUERY_VM EQU 32 +SYS_CLOSE_HANDLE EQU 33 +SYS_DUPLICATE_HANDLE EQU 34 +SYS_DERIVE_HANDLE EQU 35 +SYS_REVOKE_HANDLE EQU 36 +SYS_DERIVE_SECURITY_CONTEXT EQU 37 +SYS_QUERY_SECURITY_CONTEXT EQU 38 +SYS_CREATE_FILE_MAPPING EQU 39 +SYS_MAP_FILE EQU 40 ; Synchronization objects. -SYS_CREATE_MUTEX EQU 39 -SYS_TRY_ACQUIRE_MUTEX EQU 40 -SYS_RELEASE_MUTEX EQU 41 -SYS_CREATE_SEM EQU 42 -SYS_TRY_WAIT_SEM EQU 43 -SYS_SIGNAL_SEM EQU 44 -SYS_CREATE_EVENT EQU 45 -SYS_SIGNAL_EVENT EQU 46 -SYS_CLEAR_EVENT EQU 47 +SYS_CREATE_MUTEX EQU 41 +SYS_TRY_ACQUIRE_MUTEX EQU 42 +SYS_RELEASE_MUTEX EQU 43 +SYS_CREATE_SEM EQU 44 +SYS_TRY_WAIT_SEM EQU 45 +SYS_SIGNAL_SEM EQU 46 +SYS_CREATE_EVENT EQU 47 +SYS_SIGNAL_EVENT EQU 48 +SYS_CLEAR_EVENT EQU 49 ; Queues and named queues. -SYS_CREATE_QUEUE EQU 48 -SYS_SEND_QUEUE EQU 49 -SYS_RECV_QUEUE EQU 50 -SYS_PUBLISH_QUEUE EQU 51 -SYS_OPEN_NAMED_QUEUE EQU 52 -SYS_OPEN_NAMED_QUEUE_OWNER_PROCESS EQU 53 -SYS_UNPUBLISH_QUEUE EQU 54 +SYS_CREATE_QUEUE EQU 50 +SYS_SEND_QUEUE EQU 51 +SYS_RECV_QUEUE EQU 52 +SYS_PUBLISH_QUEUE EQU 53 +SYS_OPEN_NAMED_QUEUE EQU 54 +SYS_OPEN_NAMED_QUEUE_OWNER_PROCESS EQU 55 +SYS_UNPUBLISH_QUEUE EQU 56 ; LPC-like Ports and well-known system services. -SYS_CREATE_PORT EQU 55 -SYS_CONNECT_PORT EQU 56 -SYS_ACCEPT_PORT EQU 57 -SYS_SEND_PORT EQU 58 -SYS_RECEIVE_PORT EQU 59 -SYS_CALL_PORT EQU 60 -SYS_REPLY_PORT EQU 61 -SYS_DISCONNECT_PORT EQU 62 -SYS_REGISTER_SYSTEM_SERVICE EQU 63 -SYS_UNREGISTER_SYSTEM_SERVICE EQU 64 -SYS_CONNECT_SYSTEM_SERVICE EQU 65 +SYS_CREATE_PORT EQU 57 +SYS_CONNECT_PORT EQU 58 +SYS_ACCEPT_PORT EQU 59 +SYS_SEND_PORT EQU 60 +SYS_RECEIVE_PORT EQU 61 +SYS_CALL_PORT EQU 62 +SYS_REPLY_PORT EQU 63 +SYS_DISCONNECT_PORT EQU 64 +SYS_REGISTER_SYSTEM_SERVICE EQU 65 +SYS_UNREGISTER_SYSTEM_SERVICE EQU 66 +SYS_CONNECT_SYSTEM_SERVICE EQU 67 ; Mounted-volume manager control plane. -SYS_QUERY_VOLUMES EQU 66 -SYS_OPEN_VOLUME EQU 67 -SYS_WAIT_VOLUME_CHANGE EQU 68 +SYS_QUERY_VOLUMES EQU 68 +SYS_OPEN_VOLUME EQU 69 +SYS_WAIT_VOLUME_CHANGE EQU 70 ; Files and directories. -SYS_OPEN_FILE_AT EQU 69 -SYS_OPEN_DIR_AT EQU 70 -SYS_STAT_AT EQU 71 -SYS_DELETE_AT EQU 72 -SYS_RENAME_AT EQU 73 -SYS_CREATE_DIR_AT EQU 74 -SYS_REMOVE_DIR_AT EQU 75 -SYS_READ_FILE EQU 76 -SYS_WRITE_FILE EQU 77 -SYS_READ_FILE_ASYNC EQU 78 -SYS_WRITE_FILE_ASYNC EQU 79 -SYS_CANCEL_IO EQU 80 -SYS_SEEK_FILE EQU 81 -SYS_GET_FILE_POS EQU 82 -SYS_GET_FILE_SIZE EQU 83 -SYS_GET_FILE_STAT EQU 84 -SYS_TRUNCATE_FILE EQU 85 -SYS_FLUSH_FILE EQU 86 -SYS_LOCK_FILE EQU 87 -SYS_UNLOCK_FILE EQU 88 -SYS_READ_DIR EQU 89 +SYS_OPEN_FILE_AT EQU 71 +SYS_OPEN_DIR_AT EQU 72 +SYS_STAT_AT EQU 73 +SYS_DELETE_AT EQU 74 +SYS_RENAME_AT EQU 75 +SYS_CREATE_DIR_AT EQU 76 +SYS_REMOVE_DIR_AT EQU 77 +SYS_READ_FILE EQU 78 +SYS_WRITE_FILE EQU 79 +SYS_READ_FILE_ASYNC EQU 80 +SYS_WRITE_FILE_ASYNC EQU 81 +SYS_CANCEL_IO EQU 82 +SYS_SEEK_FILE EQU 83 +SYS_GET_FILE_POS EQU 84 +SYS_GET_FILE_SIZE EQU 85 +SYS_GET_FILE_STAT EQU 86 +SYS_TRUNCATE_FILE EQU 87 +SYS_FLUSH_FILE EQU 88 +SYS_LOCK_FILE EQU 89 +SYS_UNLOCK_FILE EQU 90 +SYS_QUERY_FILE_LOCK EQU 91 +SYS_SET_FILE_STATUS EQU 92 +SYS_CREATE_PIPE EQU 93 +SYS_NOTIFY_PIPE EQU 94 +SYS_READ_DIR EQU 95 ; Console, display, and surfaces. -SYS_WRITE_CONSOLE EQU 90 -SYS_GET_CONSOLE_INFO EQU 91 -SYS_SET_CONSOLE_CURSOR EQU 92 -SYS_FILL_CONSOLE_CHARACTER EQU 93 -SYS_FILL_CONSOLE_ATTRIBUTE EQU 94 -SYS_PUT_SCREEN_CHAR EQU 95 -SYS_CLEAR_SCREEN EQU 96 -SYS_GET_SCREEN_SIZE EQU 97 -SYS_QUERY_DISPLAY_INFO EQU 98 -SYS_OPEN_DISPLAY_CLOCK EQU 99 -SYS_CREATE_SURFACE EQU 100 -SYS_MAP_SURFACE EQU 101 -SYS_QUERY_SURFACE EQU 102 -SYS_PRESENT_SURFACE EQU 103 +SYS_WRITE_CONSOLE EQU 96 +SYS_GET_CONSOLE_INFO EQU 97 +SYS_SET_CONSOLE_CURSOR EQU 98 +SYS_FILL_CONSOLE_CHARACTER EQU 99 +SYS_FILL_CONSOLE_ATTRIBUTE EQU 100 +SYS_PUT_SCREEN_CHAR EQU 101 +SYS_CLEAR_SCREEN EQU 102 +SYS_GET_SCREEN_SIZE EQU 103 +SYS_QUERY_DISPLAY_INFO EQU 104 +SYS_QUERY_DISPLAY_MANAGER_INFO EQU 105 +SYS_PRESENT_DISPLAY_MANAGER_SURFACE EQU 106 +SYS_SET_DISPLAY_MANAGER_CURSOR EQU 107 +SYS_WRITE_DISPLAY_MANAGER_CONSOLE EQU 108 +SYS_QUERY_DISPLAY_MANAGER_CONSOLE_INFO EQU 109 +SYS_SET_DISPLAY_MANAGER_CONSOLE_CURSOR EQU 110 +SYS_FILL_DISPLAY_MANAGER_CONSOLE_CHARACTER EQU 111 +SYS_FILL_DISPLAY_MANAGER_CONSOLE_ATTRIBUTE EQU 112 +SYS_PUT_DISPLAY_MANAGER_CONSOLE_CHARACTER EQU 113 +SYS_CLEAR_DISPLAY_MANAGER_CONSOLE EQU 114 +SYS_OPEN_DISPLAY_CLOCK EQU 115 +SYS_CREATE_SURFACE EQU 116 +SYS_MAP_SURFACE EQU 117 +SYS_UNMAP_SURFACE EQU 118 +SYS_QUERY_SURFACE EQU 119 +SYS_COMMIT_SURFACE EQU 120 ; Native render audio. -SYS_AUDIO_ENUMERATE_ENDPOINTS EQU 104 -SYS_AUDIO_OPEN_STREAM EQU 105 -SYS_AUDIO_WRITE EQU 106 -SYS_AUDIO_ARM_WAIT EQU 107 -SYS_AUDIO_QUERY_STREAM EQU 108 -SYS_AUDIO_START EQU 109 -SYS_AUDIO_PAUSE EQU 110 -SYS_AUDIO_RESUME EQU 111 -SYS_AUDIO_DRAIN EQU 112 -SYS_AUDIO_STOP EQU 113 -SYS_AUDIO_RESET EQU 114 -SYS_AUDIO_SET_VOLUME EQU 115 -SYS_AUDIO_SET_MASTER_VOLUME EQU 116 +SYS_AUDIO_ENUMERATE_ENDPOINTS EQU 121 +SYS_AUDIO_OPEN_STREAM EQU 122 +SYS_AUDIO_WRITE EQU 123 +SYS_AUDIO_ARM_WAIT EQU 124 +SYS_AUDIO_QUERY_STREAM EQU 125 +SYS_AUDIO_START EQU 126 +SYS_AUDIO_PAUSE EQU 127 +SYS_AUDIO_RESUME EQU 128 +SYS_AUDIO_DRAIN EQU 129 +SYS_AUDIO_STOP EQU 130 +SYS_AUDIO_RESET EQU 131 +SYS_AUDIO_SET_VOLUME EQU 132 +SYS_AUDIO_SET_MASTER_VOLUME EQU 133 ; Input. -SYS_POLL_KEYBOARD EQU 117 -SYS_IS_KEY_DOWN EQU 118 -SYS_READ_KEYBOARD_CHAR EQU 119 -SYS_READ_KEY EQU 120 -SYS_GET_KEY_MODIFIERS EQU 121 -SYS_QUERY_GLOBAL_INPUT_STATE EQU 122 -SYS_OPEN_INPUT_QUEUE EQU 123 -SYS_READ_INPUT_EVENT EQU 124 -SYS_PEEK_INPUT_EVENT EQU 125 -SYS_PEEK_MOUSE EQU 126 -SYS_READ_MOUSE EQU 127 -SYS_GET_MOUSE_POS EQU 128 +SYS_READ_INPUT_MANAGER_EVENT EQU 134 +SYS_POLL_KEYBOARD EQU 135 +SYS_IS_KEY_DOWN EQU 136 +SYS_READ_KEYBOARD_CHAR EQU 137 +SYS_READ_KEY EQU 138 +SYS_GET_KEY_MODIFIERS EQU 139 +SYS_QUERY_GLOBAL_INPUT_STATE EQU 140 +SYS_OPEN_INPUT_QUEUE EQU 141 +SYS_READ_INPUT_EVENT EQU 142 +SYS_PEEK_INPUT_EVENT EQU 143 +SYS_PEEK_MOUSE EQU 144 +SYS_READ_MOUSE EQU 145 +SYS_GET_MOUSE_POS EQU 146 ; Time, debug, and system information. -SYS_CREATE_TIMER EQU 129 -SYS_SET_TIMER EQU 130 -SYS_CANCEL_TIMER EQU 131 -SYS_GET_TICK EQU 132 -SYS_GET_TICK_FREQUENCY EQU 133 -SYS_GET_SYSTEM_TIME EQU 134 -SYS_PRINT_DEBUG EQU 135 -SYS_QUERY_TRAP_FRAME EQU 136 -SYS_GET_MEMORY_STATUS EQU 137 -SYS_QUERY_KERNEL_STATS EQU 138 +SYS_CREATE_TIMER EQU 147 +SYS_SET_TIMER EQU 148 +SYS_CANCEL_TIMER EQU 149 +SYS_GET_TICK EQU 150 +SYS_GET_TICK_FREQUENCY EQU 151 +SYS_GET_SYSTEM_TIME EQU 152 +SYS_PRINT_DEBUG EQU 153 +SYS_QUERY_TRAP_FRAME EQU 154 +SYS_GET_MEMORY_STATUS EQU 155 +SYS_QUERY_KERNEL_STATS EQU 156 ; Native sockets and polling. -SYS_CREATE_SOCKET EQU 139 -SYS_SOCKET_IO_ASYNC EQU 140 -SYS_BIND_SOCKET EQU 141 -SYS_LISTEN_SOCKET EQU 142 -SYS_ACCEPT_SOCKET EQU 143 -SYS_CONNECT_SOCKET EQU 144 -SYS_SEND_SOCKET EQU 145 -SYS_RECV_SOCKET EQU 146 -SYS_SHUTDOWN_SOCKET EQU 147 -SYS_SEND_TO_SOCKET EQU 148 -SYS_RECV_FROM_SOCKET EQU 149 -SYS_RESOLVE_HOST EQU 150 -SYS_SET_SOCKET_OPTION EQU 151 -SYS_GET_SOCKET_OPTION EQU 152 -SYS_POLL EQU 153 +SYS_CREATE_SOCKET EQU 157 +SYS_SOCKET_IO_ASYNC EQU 158 +SYS_BIND_SOCKET EQU 159 +SYS_LISTEN_SOCKET EQU 160 +SYS_ACCEPT_SOCKET EQU 161 +SYS_CONNECT_SOCKET EQU 162 +SYS_SEND_SOCKET EQU 163 +SYS_RECV_SOCKET EQU 164 +SYS_SHUTDOWN_SOCKET EQU 165 +SYS_SEND_TO_SOCKET EQU 166 +SYS_RECV_FROM_SOCKET EQU 167 +SYS_RESOLVE_HOST EQU 168 +SYS_SET_SOCKET_OPTION EQU 169 +SYS_GET_SOCKET_OPTION EQU 170 +SYS_POLL EQU 171 -SYS_MAX EQU 154 +SYS_MAX EQU 172 diff --git a/code/user/syscall/syscall_stubs.asm b/code/user/syscall/syscall_stubs.asm index 6763cd9..f407b1f 100644 --- a/code/user/syscall/syscall_stubs.asm +++ b/code/user/syscall/syscall_stubs.asm @@ -59,6 +59,8 @@ SYSCALL_STUB ResumeThread, SYS_RESUME_THREAD SYSCALL_STUB GetCurrentTid, SYS_GET_CURRENT_TID SYSCALL_STUB GetCurrentPid, SYS_GET_CURRENT_PID SYSCALL_STUB SetThreadPriority, SYS_SET_THREAD_PRIORITY +SYSCALL_STUB SetThreadName, SYS_SET_THREAD_NAME +SYSCALL_STUB QueryThreadName, SYS_QUERY_THREAD_NAME SYSCALL_STUB Yield, SYS_YIELD SYSCALL_STUB Sleep, SYS_SLEEP SYSCALL_STUB SetUserApcDispatcher, SYS_SET_USER_APC_DISPATCHER @@ -187,6 +189,10 @@ SYSCALL_STUB TruncateFile, SYS_TRUNCATE_FILE SYSCALL_STUB FlushFile, SYS_FLUSH_FILE SYSCALL_STUB LockFile, SYS_LOCK_FILE SYSCALL_STUB UnlockFile, SYS_UNLOCK_FILE +SYSCALL_STUB QueryFileLock, SYS_QUERY_FILE_LOCK +SYSCALL_STUB SetFileStatus, SYS_SET_FILE_STATUS +SYSCALL_STUB CreatePipe, SYS_CREATE_PIPE +SYSCALL_STUB NotifyPipe, SYS_NOTIFY_PIPE SYSCALL_STUB ReadDir, SYS_READ_DIR ; --------------------------------------------------------------------------- @@ -200,11 +206,22 @@ SYSCALL_STUB FillConsoleAttribute, SYS_FILL_CONSOLE_ATTRIBUTE SYSCALL_STUB PutScreenChar, SYS_PUT_SCREEN_CHAR SYSCALL_STUB CreateSurface, SYS_CREATE_SURFACE SYSCALL_STUB MapSurface, SYS_MAP_SURFACE +SYSCALL_STUB UnmapSurface, SYS_UNMAP_SURFACE SYSCALL_STUB QuerySurface, SYS_QUERY_SURFACE -SYSCALL_STUB PresentSurface, SYS_PRESENT_SURFACE +SYSCALL_STUB CommitSurface, SYS_COMMIT_SURFACE SYSCALL_STUB ClearScreen, SYS_CLEAR_SCREEN SYSCALL_STUB GetScreenSize, SYS_GET_SCREEN_SIZE SYSCALL_STUB QueryDisplayInfo, SYS_QUERY_DISPLAY_INFO +SYSCALL_STUB QueryDisplayManagerInfo, SYS_QUERY_DISPLAY_MANAGER_INFO +SYSCALL_STUB PresentDisplayManagerSurface, SYS_PRESENT_DISPLAY_MANAGER_SURFACE +SYSCALL_STUB SetDisplayManagerCursor, SYS_SET_DISPLAY_MANAGER_CURSOR +SYSCALL_STUB WriteDisplayManagerConsole, SYS_WRITE_DISPLAY_MANAGER_CONSOLE +SYSCALL_STUB QueryDisplayManagerConsoleInfo, SYS_QUERY_DISPLAY_MANAGER_CONSOLE_INFO +SYSCALL_STUB SetDisplayManagerConsoleCursor, SYS_SET_DISPLAY_MANAGER_CONSOLE_CURSOR +SYSCALL_STUB FillDisplayManagerConsoleCharacter, SYS_FILL_DISPLAY_MANAGER_CONSOLE_CHARACTER +SYSCALL_STUB FillDisplayManagerConsoleAttribute, SYS_FILL_DISPLAY_MANAGER_CONSOLE_ATTRIBUTE +SYSCALL_STUB PutDisplayManagerConsoleCharacter, SYS_PUT_DISPLAY_MANAGER_CONSOLE_CHARACTER +SYSCALL_STUB ClearDisplayManagerConsole, SYS_CLEAR_DISPLAY_MANAGER_CONSOLE SYSCALL_STUB OpenDisplayClock, SYS_OPEN_DISPLAY_CLOCK ; --------------------------------------------------------------------------- @@ -227,6 +244,7 @@ SYSCALL_STUB AudioSetMasterVolume, SYS_AUDIO_SET_MASTER_VOLUME ; --------------------------------------------------------------------------- ; Input ; --------------------------------------------------------------------------- +SYSCALL_STUB ReadInputManagerEvent, SYS_READ_INPUT_MANAGER_EVENT SYSCALL_STUB PollKeyboard, SYS_POLL_KEYBOARD SYSCALL_STUB IsKeyDown, SYS_IS_KEY_DOWN SYSCALL_STUB ReadKeyboardChar, SYS_READ_KEYBOARD_CHAR diff --git a/code/user/system/dwm/dwm.c b/code/user/system/dwm/dwm.c new file mode 100644 index 0000000..d7e7200 --- /dev/null +++ b/code/user/system/dwm/dwm.c @@ -0,0 +1,1340 @@ +// Native Session 0 desktop/window manager. DWM owns window policy, composition, and visible cursor presentation. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DWM_EVENT_AUTO_RESET 0u +#define DWM_DESKTOP_BACKLOG 16u +#define DWM_MAX_WINDOWS 1024u +#define DWM_MAX_CLIENT_WINDOWS 128u +#define DWM_DESKTOP_COLOR 0x00202020u +#define DWM_WINDOW_COLOR 0x00303030u +#define DWM_BORDER_COLOR 0x00606060u +#define DWM_FOCUS_BORDER_COLOR 0x00D0D0D0u + +typedef struct DWM_CLIENT DWM_CLIENT; +typedef struct DWM_WINDOW DWM_WINDOW; +typedef struct DWM_SURFACE_BINDING DWM_SURFACE_BINDING; + +struct DWM_CLIENT { + SYS_HANDLE Endpoint; + uint32_t ProcessId; + uint32_t WindowCount; + DWM_CLIENT* Next; +}; + +struct DWM_SURFACE_BINDING { + DWM_WINDOW* Window; + SYS_HANDLE Surface; + void* Base; + SYS_SURFACE_DESC Desc; + SYS_HANDLE StopEvent; + SYS_HANDLE Thread; +}; + +struct DWM_WINDOW { + NTL_WINDOW_ID Id; + DWM_CLIENT* Owner; + DISPLAY_RECT Bounds; + uint32_t Visible; + uint32_t PointerMode; + uint32_t Destroying; + DWM_SURFACE_BINDING* Surface; + DWM_WINDOW* Lower; + DWM_WINDOW* Higher; +}; + +typedef struct DWM_STATE { + volatile uint32_t Running; + SYS_HANDLE Mutex; + SYS_HANDLE DirtyEvent; + SYS_HANDLE DisplayManager; + SYS_HANDLE SessionEndpoint; + SYS_HANDLE DesktopListener; + SYS_HANDLE CompositionSurface; + void* CompositionBase; + uint32_t CompositionPitch; + int32_t Width; + int32_t Height; + uint64_t NextWindowId; + uint32_t WindowCount; + DWM_CLIENT* Clients; + DWM_WINDOW* Bottom; + DWM_WINDOW* Top; + DWM_WINDOW* Focused; + DWM_WINDOW* PointerWindow; + DWM_WINDOW* Captured; + DISPLAY_RECT Dirty; + uint32_t DirtyValid; + int32_t PointerX; + int32_t PointerY; + uint32_t PointerButtons; + uint32_t CursorVisible; +} DWM_STATE; + +typedef struct DWM_CLIENT_THREAD_CONTEXT { + DWM_CLIENT* Client; +} DWM_CLIENT_THREAD_CONTEXT; + +static DWM_STATE gDwm; + +// Terminates DWM without relying on a compiler runtime return path. +static void DwmExit(int code) { + for (;;) SysExitProcess(code); +} + +// Sets a best-effort diagnostic name for the current DWM thread. +static void DwmSetCurrentThreadName(const char* name) { + SYS_SET_THREAD_NAME_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.ThreadHandle = SYS_HANDLE_INVALID; + request.Name = SysSvFromCstr(name); + (void)SysSetThreadName(&request); +} + +// Acquires the DWM policy/composition mutex. +static KSTATUS DwmLock(void) { + if (gDwm.Mutex == SYS_HANDLE_INVALID) return NTLSTATUS_INVALID_PARAMETER; + return SysWait(gDwm.Mutex, SYS_WAIT_FLAG_NONE); +} + +// Releases the DWM policy/composition mutex. +static void DwmUnlock(void) { + if (gDwm.Mutex != SYS_HANDLE_INVALID) (void)SysReleaseMutex(gDwm.Mutex); +} + +// Clips one screen rectangle without relying on signed 32-bit addition. +static bool DwmClipScreenRect(const DISPLAY_RECT* input, DISPLAY_RECT* output) { + if (!input || !output || input->W <= 0 || input->H <= 0) return false; + int64_t x0 = input->X; + int64_t y0 = input->Y; + int64_t x1 = x0 + input->W; + int64_t y1 = y0 + input->H; + if (x0 < 0) x0 = 0; + if (y0 < 0) y0 = 0; + if (x1 > gDwm.Width) x1 = gDwm.Width; + if (y1 > gDwm.Height) y1 = gDwm.Height; + if (x0 >= x1 || y0 >= y1) return false; + output->X = (int32_t)x0; + output->Y = (int32_t)y0; + output->W = (int32_t)(x1 - x0); + output->H = (int32_t)(y1 - y0); + return true; +} + +// Intersects two rectangles using widened arithmetic. +static bool DwmIntersectRect(const DISPLAY_RECT* a, const DISPLAY_RECT* b, DISPLAY_RECT* output) { + if (!a || !b || !output || a->W <= 0 || a->H <= 0 || b->W <= 0 || b->H <= 0) return false; + int64_t ax1 = (int64_t)a->X + a->W; + int64_t ay1 = (int64_t)a->Y + a->H; + int64_t bx1 = (int64_t)b->X + b->W; + int64_t by1 = (int64_t)b->Y + b->H; + int64_t x0 = a->X > b->X ? a->X : b->X; + int64_t y0 = a->Y > b->Y ? a->Y : b->Y; + int64_t x1 = ax1 < bx1 ? ax1 : bx1; + int64_t y1 = ay1 < by1 ? ay1 : by1; + if (x0 >= x1 || y0 >= y1) return false; + output->X = (int32_t)x0; + output->Y = (int32_t)y0; + output->W = (int32_t)(x1 - x0); + output->H = (int32_t)(y1 - y0); + return true; +} + +// Adds one screen-space rectangle to the coarse dirty union while the DWM mutex is held. +static void DwmDirtyLocked(const DISPLAY_RECT* rect) { + DISPLAY_RECT clipped; + if (!DwmClipScreenRect(rect, &clipped)) return; + if (!gDwm.DirtyValid) { + gDwm.Dirty = clipped; + gDwm.DirtyValid = 1u; + } else { + int32_t x0 = gDwm.Dirty.X < clipped.X ? gDwm.Dirty.X : clipped.X; + int32_t y0 = gDwm.Dirty.Y < clipped.Y ? gDwm.Dirty.Y : clipped.Y; + int64_t aX1 = (int64_t)gDwm.Dirty.X + gDwm.Dirty.W; + int64_t aY1 = (int64_t)gDwm.Dirty.Y + gDwm.Dirty.H; + int64_t bX1 = (int64_t)clipped.X + clipped.W; + int64_t bY1 = (int64_t)clipped.Y + clipped.H; + int32_t x1 = (int32_t)(aX1 > bX1 ? aX1 : bX1); + int32_t y1 = (int32_t)(aY1 > bY1 ? aY1 : bY1); + gDwm.Dirty.X = x0; + gDwm.Dirty.Y = y0; + gDwm.Dirty.W = x1 - x0; + gDwm.Dirty.H = y1 - y0; + } + (void)SysSignalEvent(gDwm.DirtyEvent); +} + +// Finds one connection-owned window while the DWM mutex is held. +static DWM_WINDOW* DwmFindOwnedWindowLocked(DWM_CLIENT* client, NTL_WINDOW_ID id) { + if (!client || id == 0) return (DWM_WINDOW*)0; + for (DWM_WINDOW* window = gDwm.Bottom; window; window = window->Higher) + if (window->Id == id && window->Owner == client && !window->Destroying) return window; + return (DWM_WINDOW*)0; +} + +// Returns the topmost visible window containing one screen-space point. +static DWM_WINDOW* DwmHitTestLocked(int32_t x, int32_t y) { + for (DWM_WINDOW* window = gDwm.Top; window; window = window->Lower) { + if (!window->Visible || window->Destroying) continue; + int64_t right = (int64_t)window->Bounds.X + window->Bounds.W; + int64_t bottom = (int64_t)window->Bounds.Y + window->Bounds.H; + if ((int64_t)x >= window->Bounds.X && (int64_t)x < right && + (int64_t)y >= window->Bounds.Y && (int64_t)y < bottom) + return window; + } + return (DWM_WINDOW*)0; +} + +// Removes one window from the z-order while the DWM mutex is held. +static void DwmUnlinkWindowLocked(DWM_WINDOW* window) { + if (!window) return; + if (window->Lower) window->Lower->Higher = window->Higher; + else if (gDwm.Bottom == window) gDwm.Bottom = window->Higher; + if (window->Higher) window->Higher->Lower = window->Lower; + else if (gDwm.Top == window) gDwm.Top = window->Lower; + window->Lower = (DWM_WINDOW*)0; + window->Higher = (DWM_WINDOW*)0; +} + +// Inserts one window at the top of the z-order while the DWM mutex is held. +static void DwmLinkTopLocked(DWM_WINDOW* window) { + if (!window) return; + window->Lower = gDwm.Top; + window->Higher = (DWM_WINDOW*)0; + if (gDwm.Top) gDwm.Top->Higher = window; + else gDwm.Bottom = window; + gDwm.Top = window; +} + +// Inserts one window at the bottom of the z-order while the DWM mutex is held. +static void DwmLinkBottomLocked(DWM_WINDOW* window) { + if (!window) return; + window->Lower = (DWM_WINDOW*)0; + window->Higher = gDwm.Bottom; + if (gDwm.Bottom) gDwm.Bottom->Lower = window; + else gDwm.Top = window; + gDwm.Bottom = window; +} + +// Sends one private focus/capture transition request to RSSRV. +static KSTATUS DwmSetSessionAuthority(uint32_t operation, bool enabled) { + if (gDwm.SessionEndpoint == SYS_HANDLE_INVALID) return NTLSTATUS_INVALID_HANDLE; + NTL_SESSION_DWM_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_SESSION_DWM_PROTOCOL_VERSION; + request.Operation = operation; + request.Flags = NTL_SESSION_DWM_FLAG_NONE; + request.Enabled = enabled ? 1u : 0u; + + NTL_SESSION_DWM_REPLY reply; + memset(&reply, 0, sizeof(reply)); + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + SYS_CALL_PORT_REQUEST call; + memset(&call, 0, sizeof(call)); + call.Size = sizeof(call); + call.EndpointHandle = gDwm.SessionEndpoint; + call.Flags = SYS_PORT_FLAG_NONE; + call.RequestPayload = &request; + call.RequestLength = sizeof(request); + call.ReplyPayload = &reply; + call.ReplyCapacity = sizeof(reply); + call.ReplyInfoOut = &info; + call.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysCallPort(&call); + if (!KSUCCESS(status)) return status; + if (info.Type != SYS_PORT_MESSAGE_REPLY || info.HandleCount != 0 || info.PayloadLength != sizeof(reply) || + reply.Size != sizeof(reply) || reply.Version != NTL_SESSION_DWM_PROTOCOL_VERSION || + reply.Operation != operation || reply.Flags != NTL_SESSION_DWM_FLAG_NONE) + return NTLSTATUS_FAILURE; + return reply.Status; +} + +// Changes the internal focused window only after RSSRV accepts desktop keyboard focus. +static KSTATUS DwmFocusWindowLocked(DWM_WINDOW* window) { + if (!window || !window->Visible || window->Destroying) return NTLSTATUS_INVALID_PARAMETER; + if (gDwm.Focused == window) return NTLSTATUS_SUCCESS; + KSTATUS status = DwmSetSessionAuthority(NTL_SESSION_DWM_OP_SET_KEYBOARD_FOCUS, true); + if (!KSUCCESS(status)) return status; + DWM_WINDOW* previous = gDwm.Focused; + gDwm.Focused = window; + if (previous) DwmDirtyLocked(&previous->Bounds); + DwmDirtyLocked(&window->Bounds); + return NTLSTATUS_SUCCESS; +} + +// Releases RSSRV keyboard focus only when DWM currently owns it through this focused window. +static void DwmReleaseFocusLocked(DWM_WINDOW* window) { + if (!window || gDwm.Focused != window) return; + (void)DwmSetSessionAuthority(NTL_SESSION_DWM_OP_SET_KEYBOARD_FOCUS, false); + gDwm.Focused = (DWM_WINDOW*)0; + DwmDirtyLocked(&window->Bounds); +} + +// Updates the desktop pointer-focus grant to match DWM hit testing. +static void DwmSetPointerWindowLocked(DWM_WINDOW* window) { + if (gDwm.Captured) return; + if (gDwm.PointerWindow == window) return; + bool enable = window != (DWM_WINDOW*)0; + if (!KSUCCESS(DwmSetSessionAuthority(NTL_SESSION_DWM_OP_SET_POINTER_FOCUS, enable))) return; + gDwm.PointerWindow = window; +} + +// Acquires RSSRV pointer capture for one DWM window. +static KSTATUS DwmCaptureWindowLocked(DWM_WINDOW* window) { + if (!window || !window->Visible || window->Destroying) return NTLSTATUS_INVALID_PARAMETER; + if (gDwm.Captured == window) return NTLSTATUS_SUCCESS; + KSTATUS status = DwmSetSessionAuthority(NTL_SESSION_DWM_OP_SET_POINTER_CAPTURE, true); + if (!KSUCCESS(status)) return status; + gDwm.Captured = window; + return NTLSTATUS_SUCCESS; +} + +// Releases RSSRV pointer capture only when the supplied window owns it. +static KSTATUS DwmReleaseCaptureLocked(DWM_WINDOW* window) { + if (!window || gDwm.Captured != window) return NTLSTATUS_ACCESS_DENIED; + KSTATUS status = DwmSetSessionAuthority(NTL_SESSION_DWM_OP_SET_POINTER_CAPTURE, false); + if (!KSUCCESS(status)) return status; + gDwm.Captured = (DWM_WINDOW*)0; + DWM_WINDOW* hit = DwmHitTestLocked(gDwm.PointerX, gDwm.PointerY); + DwmSetPointerWindowLocked(hit); + return NTLSTATUS_SUCCESS; +} + +// Resolves the currently authorized visible cursor policy while the DWM mutex is held. +static void DwmResolveCursorLocked(int32_t* xOut, int32_t* yOut, bool* visibleOut) { + int32_t x = gDwm.PointerX; + int32_t y = gDwm.PointerY; + DWM_WINDOW* policyWindow = gDwm.Captured ? gDwm.Captured : gDwm.Focused; + uint32_t mode = policyWindow ? policyWindow->PointerMode : NTL_DESKTOP_POINTER_FREE; + + if (policyWindow && mode == NTL_DESKTOP_POINTER_RELATIVE_LOCKED) { + x = policyWindow->Bounds.X + policyWindow->Bounds.W / 2; + y = policyWindow->Bounds.Y + policyWindow->Bounds.H / 2; + } else if (policyWindow && mode == NTL_DESKTOP_POINTER_CONFINED) { + int64_t right = (int64_t)policyWindow->Bounds.X + policyWindow->Bounds.W; + int64_t bottom = (int64_t)policyWindow->Bounds.Y + policyWindow->Bounds.H; + if (x < policyWindow->Bounds.X) x = policyWindow->Bounds.X; + if (y < policyWindow->Bounds.Y) y = policyWindow->Bounds.Y; + if ((int64_t)x >= right) x = (int32_t)(right - 1); + if ((int64_t)y >= bottom) y = (int32_t)(bottom - 1); + } + + if (x < 0) x = 0; + if (y < 0) y = 0; + if (x >= gDwm.Width) x = gDwm.Width - 1; + if (y >= gDwm.Height) y = gDwm.Height - 1; + *xOut = x; + *yOut = y; + *visibleOut = mode != NTL_DESKTOP_POINTER_RELATIVE_LOCKED; +} + +// Applies the currently resolved visible cursor state through DWM's DisplayManager capability. +static void DwmApplyCursorLocked(void) { + int32_t x = 0; + int32_t y = 0; + bool visible = true; + DwmResolveCursorLocked(&x, &y, &visible); + + SYS_DISPLAY_MANAGER_CURSOR_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.ManagerHandle = gDwm.DisplayManager; + request.Flags = SYS_DISPLAY_MANAGER_CURSOR_FLAG_NONE; + if ((visible ? 1u : 0u) != gDwm.CursorVisible) + request.Flags = visible ? SYS_DISPLAY_MANAGER_CURSOR_FLAG_SHOW : SYS_DISPLAY_MANAGER_CURSOR_FLAG_HIDE; + request.X = x; + request.Y = y; + if (KSUCCESS(SysSetDisplayManagerCursor(&request))) gDwm.CursorVisible = visible ? 1u : 0u; +} + +// Sends one nonblocking desktop input event while the DWM mutex keeps client/window lifetime stable. +static void DwmSendClientEventLocked(DWM_WINDOW* window, + uint32_t type, + const NTL_SESSION_DWM_EVENT* source) { + if (!window || !window->Owner || window->Owner->Endpoint == SYS_HANDLE_INVALID || !source) return; + NTL_DESKTOP_EVENT event; + memset(&event, 0, sizeof(event)); + event.Size = sizeof(event); + event.Version = NTL_DESKTOP_PROTOCOL_VERSION; + event.Type = type; + event.Flags = NTL_DESKTOP_FLAG_NONE; + event.WindowId = window->Id; + event.PointerX = source->PointerX - window->Bounds.X; + event.PointerY = source->PointerY - window->Bounds.Y; + event.Key = source->Key; + event.Mouse = source->Mouse; + + SYS_SEND_PORT_REQUEST send; + memset(&send, 0, sizeof(send)); + send.Size = sizeof(send); + send.EndpointHandle = window->Owner->Endpoint; + send.Flags = SYS_PORT_FLAG_NONE; + send.Payload = &event; + send.PayloadLength = sizeof(event); + send.TimeoutMilliseconds = 0; + (void)SysSendPort(&send); +} + +// Observes logical pointer motion for hit testing, click-to-focus, z-order, and visible cursor policy. +static void DwmObserveMouseLocked(const NTL_SESSION_DWM_EVENT* event) { + if (!event) return; + uint32_t previousButtons = gDwm.PointerButtons; + gDwm.PointerX = event->PointerX; + gDwm.PointerY = event->PointerY; + gDwm.PointerButtons = event->Mouse.buttons; + + DWM_WINDOW* hit = gDwm.Captured ? gDwm.Captured : DwmHitTestLocked(gDwm.PointerX, gDwm.PointerY); + if (!gDwm.Captured) DwmSetPointerWindowLocked(hit); + + uint32_t pressed = event->Mouse.buttons & ~previousButtons; + if (pressed && hit && hit->Visible) { + if (hit != gDwm.Top) { + DwmDirtyLocked(&hit->Bounds); + DwmUnlinkWindowLocked(hit); + DwmLinkTopLocked(hit); + DwmDirtyLocked(&hit->Bounds); + } + (void)DwmFocusWindowLocked(hit); + } + DwmApplyCursorLocked(); +} + +// Consumes RSSRV observer/delivery notifications and translates desktop-routed input to client windows. +static void DwmSessionInputThread(uintptr_t argument) { + (void)argument; + DwmSetCurrentThreadName("dwm.session.input"); + + while (gDwm.Running) { + NTL_SESSION_DWM_EVENT event; + SYS_PORT_MESSAGE_INFO info; + memset(&event, 0, sizeof(event)); + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = gDwm.SessionEndpoint; + receive.Flags = SYS_PORT_FLAG_NONE; + receive.Payload = &event; + receive.PayloadCapacity = sizeof(event); + receive.InfoOut = &info; + receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysReceivePort(&receive); + if (status == NTLSTATUS_PORT_DISCONNECTED || !KSUCCESS(status)) break; + if (info.Type != SYS_PORT_MESSAGE_NOTIFICATION || info.HandleCount != 0 || + info.PayloadLength != sizeof(event) || event.Size != sizeof(event) || + event.Version != NTL_SESSION_DWM_PROTOCOL_VERSION || event.Flags != NTL_SESSION_DWM_FLAG_NONE) + continue; + + if (!KSUCCESS(DwmLock())) continue; + if (event.Type == NTL_SESSION_DWM_EVENT_OBSERVE_MOUSE) { + DwmObserveMouseLocked(&event); + } else if (event.Type == NTL_SESSION_DWM_EVENT_DELIVER_KEY) { + if (gDwm.Focused) DwmSendClientEventLocked(gDwm.Focused, NTL_DESKTOP_EVENT_KEY, &event); + } else if (event.Type == NTL_SESSION_DWM_EVENT_DELIVER_MOUSE) { + DWM_WINDOW* target = gDwm.Captured ? gDwm.Captured : gDwm.PointerWindow; + if (target) DwmSendClientEventLocked(target, NTL_DESKTOP_EVENT_MOUSE, &event); + } + DwmUnlock(); + } + SysExitThread(0); +} + +// Fills one composition rectangle with one XRGB color. +static void DwmFillCompositionRect(const DISPLAY_RECT* rect, uint32_t color) { + DISPLAY_RECT clipped; + if (!DwmClipScreenRect(rect, &clipped)) return; + uint8_t* base = (uint8_t*)gDwm.CompositionBase; + for (int32_t y = 0; y < clipped.H; ++y) { + uint32_t* row = (uint32_t*)(base + (size_t)(clipped.Y + y) * gDwm.CompositionPitch) + clipped.X; + for (int32_t x = 0; x < clipped.W; ++x) row[x] = color; + } +} + +// Draws the dirty pieces of one one-pixel window border. +static void DwmDrawWindowBorder(const DWM_WINDOW* window, const DISPLAY_RECT* dirty, uint32_t color) { + if (!window || !dirty || window->Bounds.W <= 0 || window->Bounds.H <= 0) return; + DISPLAY_RECT edges[4] = { + { window->Bounds.X, window->Bounds.Y, window->Bounds.W, 1 }, + { window->Bounds.X, window->Bounds.Y + window->Bounds.H - 1, window->Bounds.W, 1 }, + { window->Bounds.X, window->Bounds.Y, 1, window->Bounds.H }, + { window->Bounds.X + window->Bounds.W - 1, window->Bounds.Y, 1, window->Bounds.H } + }; + for (uint32_t i = 0; i < 4u; ++i) { + DISPLAY_RECT clipped; + if (DwmIntersectRect(&edges[i], dirty, &clipped)) DwmFillCompositionRect(&clipped, color); + } +} + +// Copies the visible portion of one client surface into the full-screen composition surface. +static void DwmCopyWindowSurface(const DWM_WINDOW* window, const DISPLAY_RECT* dirty) { + if (!window || !dirty || !window->Surface || !window->Surface->Base) return; + DISPLAY_RECT overlap; + if (!DwmIntersectRect(&window->Bounds, dirty, &overlap)) return; + + int32_t sx = overlap.X - window->Bounds.X; + int32_t sy = overlap.Y - window->Bounds.Y; + int32_t width = overlap.W; + int32_t height = overlap.H; + if (sx < 0 || sy < 0) return; + if ((uint32_t)sx >= window->Surface->Desc.Width || (uint32_t)sy >= window->Surface->Desc.Height) return; + if ((uint64_t)(uint32_t)sx + (uint32_t)width > window->Surface->Desc.Width) + width = (int32_t)(window->Surface->Desc.Width - (uint32_t)sx); + if ((uint64_t)(uint32_t)sy + (uint32_t)height > window->Surface->Desc.Height) + height = (int32_t)(window->Surface->Desc.Height - (uint32_t)sy); + if (width <= 0 || height <= 0) return; + + uint8_t* destinationBase = (uint8_t*)gDwm.CompositionBase; + const uint8_t* sourceBase = (const uint8_t*)window->Surface->Base; + for (int32_t y = 0; y < height; ++y) { + uint32_t* destination = (uint32_t*)(destinationBase + + (size_t)(overlap.Y + y) * gDwm.CompositionPitch) + overlap.X; + const uint32_t* source = (const uint32_t*)(sourceBase + + (size_t)(sy + y) * window->Surface->Desc.PitchBytes) + sx; + memcpy(destination, source, (size_t)width * sizeof(uint32_t)); + } +} + +// Rebuilds one dirty screen-space rectangle from bottom to top while the DWM mutex is held. +static void DwmComposeLocked(const DISPLAY_RECT* dirty) { + if (!dirty) return; + DwmFillCompositionRect(dirty, DWM_DESKTOP_COLOR); + for (DWM_WINDOW* window = gDwm.Bottom; window; window = window->Higher) { + if (!window->Visible || window->Destroying) continue; + DISPLAY_RECT overlap; + if (!DwmIntersectRect(&window->Bounds, dirty, &overlap)) continue; + DwmFillCompositionRect(&overlap, DWM_WINDOW_COLOR); + DwmCopyWindowSurface(window, dirty); + DwmDrawWindowBorder(window, dirty, window == gDwm.Focused ? DWM_FOCUS_BORDER_COLOR : DWM_BORDER_COLOR); + } +} + +// Rebuilds and physically presents dirty regions from DWM's private composition surface. +static void DwmCompositorThread(uintptr_t argument) { + (void)argument; + DwmSetCurrentThreadName("dwm.compositor"); + while (gDwm.Running) { + KSTATUS status = SysWait(gDwm.DirtyEvent, SYS_WAIT_FLAG_NONE); + if (!KSUCCESS(status) || !gDwm.Running) break; + + DISPLAY_RECT dirty; + memset(&dirty, 0, sizeof(dirty)); + if (!KSUCCESS(DwmLock())) continue; + if (!gDwm.DirtyValid) { + DwmUnlock(); + continue; + } + dirty = gDwm.Dirty; + gDwm.DirtyValid = 0; + memset(&gDwm.Dirty, 0, sizeof(gDwm.Dirty)); + DwmComposeLocked(&dirty); + DwmUnlock(); + + (void)SysCommitSurface(gDwm.CompositionSurface, &dirty, 1u, 0); + (void)SysPresentDisplayManagerSurface(gDwm.DisplayManager, gDwm.CompositionSurface, &dirty, 1u); + } + SysExitThread(0); +} + +// Waits for client commits and converts them into coarse window-sized compositor damage. +static void DwmSurfaceWatcherThread(uintptr_t argument) { + DWM_SURFACE_BINDING* binding = (DWM_SURFACE_BINDING*)argument; + DwmSetCurrentThreadName("dwm.surface.watch"); + if (!binding) SysExitThread(1); + + SYS_HANDLE waits[2] = { binding->Surface, binding->StopEvent }; + for (;;) { + uint32_t signaled = 0; + SYS_WAIT_MANY_REQUEST wait; + memset(&wait, 0, sizeof(wait)); + wait.Size = sizeof(wait); + wait.Flags = SYS_WAIT_FLAG_NONE; + wait.Handles = waits; + wait.Count = 2u; + wait.SignaledIndexOut = &signaled; + wait.TimeoutMilliseconds = SYS_WAIT_TIMEOUT_INFINITE; + KSTATUS status = SysWaitMany(&wait); + if (!KSUCCESS(status) || signaled == 1u) break; + if (signaled != 0u) continue; + + if (!KSUCCESS(DwmLock())) continue; + if (binding->Window && !binding->Window->Destroying && binding->Window->Surface == binding) + DwmDirtyLocked(&binding->Window->Bounds); + DwmUnlock(); + } + SysExitThread(0); +} + +// Tears down one detached surface binding after it is no longer reachable from a window. +static void DwmStopSurfaceBinding(DWM_SURFACE_BINDING* binding) { + if (!binding) return; + if (binding->StopEvent != SYS_HANDLE_INVALID) (void)SysSignalEvent(binding->StopEvent); + if (binding->Thread != SYS_HANDLE_INVALID) { + (void)SysWait(binding->Thread, SYS_WAIT_FLAG_NONE); + (void)SysCloseHandle(binding->Thread); + } + if (binding->Base) (void)SysUnmapSurface(binding->Base); + if (binding->StopEvent != SYS_HANDLE_INVALID) (void)SysCloseHandle(binding->StopEvent); + if (binding->Surface != SYS_HANDLE_INVALID) (void)SysCloseHandle(binding->Surface); + free(binding); +} + +// Creates a read-only DWM mapping around one transferred client surface capability. +static KSTATUS DwmCreateSurfaceBinding(DWM_WINDOW* window, + SYS_HANDLE surface, + DWM_SURFACE_BINDING** bindingOut) { + if (!window || surface == SYS_HANDLE_INVALID || !bindingOut) return NTLSTATUS_INVALID_PARAMETER; + *bindingOut = (DWM_SURFACE_BINDING*)0; + + DWM_SURFACE_BINDING* binding = (DWM_SURFACE_BINDING*)malloc(sizeof(*binding)); + if (!binding) { + (void)SysCloseHandle(surface); + return NTLSTATUS_NO_MEMORY; + } + memset(binding, 0, sizeof(*binding)); + binding->Window = window; + binding->Surface = surface; + binding->StopEvent = SYS_HANDLE_INVALID; + binding->Thread = SYS_HANDLE_INVALID; + + KSTATUS status = SysQuerySurface(surface, &binding->Desc); + if (!KSUCCESS(status) || binding->Desc.Format != SYS_SURFACE_FORMAT_XRGB8888 || + binding->Desc.Width == 0 || binding->Desc.Height == 0) { + DwmStopSurfaceBinding(binding); + return KSUCCESS(status) ? NTLSTATUS_NOT_SUPPORTED : status; + } + uint32_t pitch = 0; + status = SysMapSurface(surface, SYS_SURFACE_MAP_READ, &binding->Base, &pitch); + if (!KSUCCESS(status)) { + DwmStopSurfaceBinding(binding); + return status; + } + if (pitch != binding->Desc.PitchBytes) { + DwmStopSurfaceBinding(binding); + return NTLSTATUS_FAILURE; + } + status = SysCreateEvent(DWM_EVENT_AUTO_RESET, 0u, &binding->StopEvent); + if (!KSUCCESS(status)) { + DwmStopSurfaceBinding(binding); + return status; + } + *bindingOut = binding; + return NTLSTATUS_SUCCESS; +} + +// Starts one prepared surface watcher after the binding has been installed into its window. +static KSTATUS DwmStartSurfaceBinding(DWM_SURFACE_BINDING* binding) { + if (!binding || !binding->Window) return NTLSTATUS_INVALID_PARAMETER; + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)DwmSurfaceWatcherThread; + create.Argument = (uintptr_t)binding; + create.Priority = 1u; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + create.ThreadHandleOut = &binding->Thread; + return SysCreateThread(&create); +} + +// Validates one top-level window rectangle without requiring it to remain fully on-screen. +static KSTATUS DwmValidateWindowBounds(const DISPLAY_RECT* bounds) { + if (!bounds || bounds->W <= 0 || bounds->H <= 0 || + (uint32_t)bounds->W > SYS_SURFACE_MAX_DIMENSION || (uint32_t)bounds->H > SYS_SURFACE_MAX_DIMENSION) + return NTLSTATUS_INVALID_PARAMETER; + return NTLSTATUS_SUCCESS; +} + +// Copies current window state into one desktop reply while the DWM mutex is held. +static void DwmFillWindowReplyLocked(const DWM_WINDOW* window, NTL_DESKTOP_REPLY* reply) { + if (!window || !reply) return; + reply->WindowId = window->Id; + reply->Bounds = window->Bounds; + reply->Visible = window->Visible; + reply->Focused = gDwm.Focused == window ? 1u : 0u; + reply->Captured = gDwm.Captured == window ? 1u : 0u; + reply->HasSurface = window->Surface ? 1u : 0u; + if (window->Surface) { + reply->SurfaceWidth = window->Surface->Desc.Width; + reply->SurfaceHeight = window->Surface->Desc.Height; + } +} + +// Destroys one connection-owned window and returns its detached binding for blocking teardown outside the mutex. +static DWM_SURFACE_BINDING* DwmDestroyWindowLocked(DWM_WINDOW* window) { + if (!window || window->Destroying) return (DWM_SURFACE_BINDING*)0; + window->Destroying = 1u; + DwmDirtyLocked(&window->Bounds); + if (gDwm.Focused == window) DwmReleaseFocusLocked(window); + if (gDwm.Captured == window) (void)DwmReleaseCaptureLocked(window); + if (gDwm.PointerWindow == window) { + gDwm.PointerWindow = (DWM_WINDOW*)0; + (void)DwmSetSessionAuthority(NTL_SESSION_DWM_OP_SET_POINTER_FOCUS, false); + } + DwmUnlinkWindowLocked(window); + if (window->Owner && window->Owner->WindowCount) window->Owner->WindowCount--; + if (gDwm.WindowCount) gDwm.WindowCount--; + DWM_SURFACE_BINDING* binding = window->Surface; + window->Surface = (DWM_SURFACE_BINDING*)0; + if (!gDwm.Captured) { + DWM_WINDOW* hit = DwmHitTestLocked(gDwm.PointerX, gDwm.PointerY); + DwmSetPointerWindowLocked(hit); + } + return binding; +} + +// Handles one validated desktop request and owns any transferred surface handle supplied with it. +static KSTATUS DwmHandleDesktopRequest(DWM_CLIENT* client, + const NTL_DESKTOP_REQUEST* request, + SYS_HANDLE transferredHandle, + NTL_DESKTOP_REPLY* reply) { + if (!client || !request || !reply) { + if (transferredHandle != SYS_HANDLE_INVALID) (void)SysCloseHandle(transferredHandle); + return NTLSTATUS_INVALID_PARAMETER; + } + + KSTATUS status = NTLSTATUS_SUCCESS; + DWM_SURFACE_BINDING* detached = (DWM_SURFACE_BINDING*)0; + DWM_WINDOW* freeWindow = (DWM_WINDOW*)0; + + if (request->Operation == NTL_DESKTOP_OP_ATTACH_SURFACE) { + DWM_WINDOW* window = (DWM_WINDOW*)0; + if (!KSUCCESS(DwmLock())) { + (void)SysCloseHandle(transferredHandle); + return NTLSTATUS_FAILURE; + } + window = DwmFindOwnedWindowLocked(client, request->WindowId); + DwmUnlock(); + if (!window) { + (void)SysCloseHandle(transferredHandle); + return NTLSTATUS_NOT_FOUND; + } + + DWM_SURFACE_BINDING* binding = (DWM_SURFACE_BINDING*)0; + status = DwmCreateSurfaceBinding(window, transferredHandle, &binding); + if (!KSUCCESS(status)) return status; + + status = DwmLock(); + if (!KSUCCESS(status)) { + DwmStopSurfaceBinding(binding); + return status; + } + if (window->Destroying || window->Owner != client) { + DwmUnlock(); + DwmStopSurfaceBinding(binding); + return NTLSTATUS_OBJECT_DEAD; + } + detached = window->Surface; + window->Surface = binding; + DwmDirtyLocked(&window->Bounds); + DwmFillWindowReplyLocked(window, reply); + DwmUnlock(); + + status = DwmStartSurfaceBinding(binding); + if (!KSUCCESS(status)) { + if (KSUCCESS(DwmLock())) { + if (window->Surface == binding) window->Surface = (DWM_SURFACE_BINDING*)0; + DwmDirtyLocked(&window->Bounds); + DwmUnlock(); + } + DwmStopSurfaceBinding(binding); + } + if (detached) DwmStopSurfaceBinding(detached); + return status; + } + + if (transferredHandle != SYS_HANDLE_INVALID) { + (void)SysCloseHandle(transferredHandle); + return NTLSTATUS_INVALID_PARAMETER; + } + + status = DwmLock(); + if (!KSUCCESS(status)) return status; + DWM_WINDOW* window = request->WindowId ? DwmFindOwnedWindowLocked(client, request->WindowId) : (DWM_WINDOW*)0; + + switch (request->Operation) { + case NTL_DESKTOP_OP_CREATE_WINDOW: { + status = DwmValidateWindowBounds(&request->Bounds); + if (!KSUCCESS(status)) break; + if (gDwm.WindowCount >= DWM_MAX_WINDOWS || client->WindowCount >= DWM_MAX_CLIENT_WINDOWS) { + status = NTLSTATUS_LIMIT_REACHED; + break; + } + window = (DWM_WINDOW*)malloc(sizeof(*window)); + if (!window) { + status = NTLSTATUS_NO_MEMORY; + break; + } + memset(window, 0, sizeof(*window)); + window->Owner = client; + window->Bounds = request->Bounds; + window->PointerMode = NTL_DESKTOP_POINTER_FREE; + window->Id = gDwm.NextWindowId++; + if (window->Id == 0) window->Id = gDwm.NextWindowId++; + DwmLinkTopLocked(window); + gDwm.WindowCount++; + client->WindowCount++; + DwmFillWindowReplyLocked(window, reply); + break; + } + case NTL_DESKTOP_OP_DESTROY_WINDOW: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + detached = DwmDestroyWindowLocked(window); + freeWindow = window; + break; + case NTL_DESKTOP_OP_SET_BOUNDS: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + status = DwmValidateWindowBounds(&request->Bounds); + if (!KSUCCESS(status)) break; + DwmDirtyLocked(&window->Bounds); + window->Bounds = request->Bounds; + DwmDirtyLocked(&window->Bounds); + if (!gDwm.Captured) DwmSetPointerWindowLocked(DwmHitTestLocked(gDwm.PointerX, gDwm.PointerY)); + DwmApplyCursorLocked(); + DwmFillWindowReplyLocked(window, reply); + break; + case NTL_DESKTOP_OP_SET_VISIBLE: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + if (request->Value > 1u) { + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + if (window->Visible != request->Value) { + DwmDirtyLocked(&window->Bounds); + window->Visible = request->Value; + if (!window->Visible) { + DwmReleaseFocusLocked(window); + if (gDwm.Captured == window) (void)DwmReleaseCaptureLocked(window); + if (gDwm.PointerWindow == window) { + gDwm.PointerWindow = (DWM_WINDOW*)0; + (void)DwmSetSessionAuthority(NTL_SESSION_DWM_OP_SET_POINTER_FOCUS, false); + } + } + if (!gDwm.Captured) DwmSetPointerWindowLocked(DwmHitTestLocked(gDwm.PointerX, gDwm.PointerY)); + DwmApplyCursorLocked(); + } + DwmFillWindowReplyLocked(window, reply); + break; + case NTL_DESKTOP_OP_RAISE_WINDOW: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + if (window != gDwm.Top) { + DwmDirtyLocked(&window->Bounds); + DwmUnlinkWindowLocked(window); + DwmLinkTopLocked(window); + DwmDirtyLocked(&window->Bounds); + } + DwmFillWindowReplyLocked(window, reply); + break; + case NTL_DESKTOP_OP_LOWER_WINDOW: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + if (window != gDwm.Bottom) { + DwmDirtyLocked(&window->Bounds); + DwmUnlinkWindowLocked(window); + DwmLinkBottomLocked(window); + DwmDirtyLocked(&window->Bounds); + } + DwmFillWindowReplyLocked(window, reply); + break; + case NTL_DESKTOP_OP_REQUEST_FOCUS: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + status = DwmFocusWindowLocked(window); + if (KSUCCESS(status)) DwmFillWindowReplyLocked(window, reply); + DwmApplyCursorLocked(); + break; + case NTL_DESKTOP_OP_REQUEST_CAPTURE: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + status = DwmCaptureWindowLocked(window); + if (KSUCCESS(status)) DwmFillWindowReplyLocked(window, reply); + DwmApplyCursorLocked(); + break; + case NTL_DESKTOP_OP_RELEASE_CAPTURE: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + status = DwmReleaseCaptureLocked(window); + if (KSUCCESS(status)) DwmFillWindowReplyLocked(window, reply); + DwmApplyCursorLocked(); + break; + case NTL_DESKTOP_OP_SET_POINTER_MODE: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + if (request->Value > NTL_DESKTOP_POINTER_RELATIVE_LOCKED) { + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + if (gDwm.Focused != window && gDwm.Captured != window) { + status = NTLSTATUS_ACCESS_DENIED; + break; + } + window->PointerMode = request->Value; + DwmApplyCursorLocked(); + DwmFillWindowReplyLocked(window, reply); + break; + case NTL_DESKTOP_OP_DETACH_SURFACE: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + detached = window->Surface; + window->Surface = (DWM_SURFACE_BINDING*)0; + DwmDirtyLocked(&window->Bounds); + DwmFillWindowReplyLocked(window, reply); + break; + case NTL_DESKTOP_OP_QUERY_WINDOW: + if (!window) { + status = NTLSTATUS_NOT_FOUND; + break; + } + DwmFillWindowReplyLocked(window, reply); + break; + default: + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + + DwmUnlock(); + if (detached) DwmStopSurfaceBinding(detached); + if (freeWindow) free(freeWindow); + return status; +} + +// Sends one fixed desktop reply for a client request. +static void DwmReplyDesktop(SYS_HANDLE endpoint, + uint32_t messageId, + const NTL_DESKTOP_REQUEST* request, + KSTATUS status, + NTL_DESKTOP_REPLY* reply) { + if (!reply) return; + reply->Size = sizeof(*reply); + reply->Version = NTL_DESKTOP_PROTOCOL_VERSION; + reply->Operation = request ? request->Operation : 0u; + reply->Flags = NTL_DESKTOP_FLAG_NONE; + reply->Status = status; + + SYS_REPLY_PORT_REQUEST send; + memset(&send, 0, sizeof(send)); + send.Size = sizeof(send); + send.EndpointHandle = endpoint; + send.MessageId = messageId; + send.Flags = SYS_PORT_FLAG_NONE; + send.Payload = reply; + send.PayloadLength = sizeof(*reply); + (void)SysReplyPort(&send); +} + +// Destroys every window owned by a disconnected client before releasing the client record. +static void DwmDestroyClientWindows(DWM_CLIENT* client) { + if (!client) return; + for (;;) { + DWM_WINDOW* window = (DWM_WINDOW*)0; + DWM_SURFACE_BINDING* binding = (DWM_SURFACE_BINDING*)0; + if (!KSUCCESS(DwmLock())) break; + for (DWM_WINDOW* candidate = gDwm.Bottom; candidate; candidate = candidate->Higher) { + if (candidate->Owner == client && !candidate->Destroying) { + window = candidate; + break; + } + } + if (window) binding = DwmDestroyWindowLocked(window); + DwmUnlock(); + if (!window) break; + if (binding) DwmStopSurfaceBinding(binding); + free(window); + } +} + +// Removes one client from the DWM client list while the DWM mutex is held. +static void DwmRemoveClientLocked(DWM_CLIENT* client) { + if (!client) return; + DWM_CLIENT** link = &gDwm.Clients; + while (*link) { + if (*link == client) { + *link = client->Next; + client->Next = (DWM_CLIENT*)0; + return; + } + link = &(*link)->Next; + } +} + +// Serves one desktop client connection and enforces per-connection window ownership. +static void DwmClientThread(uintptr_t argument) { + DWM_CLIENT_THREAD_CONTEXT* context = (DWM_CLIENT_THREAD_CONTEXT*)argument; + DWM_CLIENT* client = context ? context->Client : (DWM_CLIENT*)0; + free(context); + DwmSetCurrentThreadName("dwm.client"); + if (!client) SysExitThread(1); + + while (gDwm.Running) { + NTL_DESKTOP_REQUEST request; + SYS_HANDLE handles[1] = { SYS_HANDLE_INVALID }; + SYS_PORT_MESSAGE_INFO info; + memset(&request, 0, sizeof(request)); + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = client->Endpoint; + receive.Flags = SYS_PORT_FLAG_NONE; + receive.Payload = &request; + receive.PayloadCapacity = sizeof(request); + receive.HandlesOut = handles; + receive.HandleCapacity = 1u; + receive.InfoOut = &info; + receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysReceivePort(&receive); + if (status == NTLSTATUS_PORT_DISCONNECTED || !KSUCCESS(status)) break; + + bool attach = request.Operation == NTL_DESKTOP_OP_ATTACH_SURFACE; + bool envelopeValid = info.Type == SYS_PORT_MESSAGE_REQUEST && info.PayloadLength == sizeof(request) && + request.Size == sizeof(request) && request.Version == NTL_DESKTOP_PROTOCOL_VERSION && + request.Flags == NTL_DESKTOP_FLAG_NONE && request.Reserved == 0 && + info.HandleCount == (attach ? 1u : 0u); + NTL_DESKTOP_REPLY reply; + memset(&reply, 0, sizeof(reply)); + if (!envelopeValid) { + if (info.HandleCount && handles[0] != SYS_HANDLE_INVALID) (void)SysCloseHandle(handles[0]); + DwmReplyDesktop(client->Endpoint, info.MessageId, &request, NTLSTATUS_INVALID_PARAMETER, &reply); + continue; + } + + SYS_HANDLE transferred = attach ? handles[0] : SYS_HANDLE_INVALID; + status = DwmHandleDesktopRequest(client, &request, transferred, &reply); + DwmReplyDesktop(client->Endpoint, info.MessageId, &request, status, &reply); + } + + DwmDestroyClientWindows(client); + if (KSUCCESS(DwmLock())) { + DwmRemoveClientLocked(client); + DwmUnlock(); + } + (void)SysDisconnectPort(client->Endpoint); + (void)SysCloseHandle(client->Endpoint); + free(client); + SysExitThread(0); +} + +// Accepts public desktop connections and gives each connection one policy worker. +static void DwmDesktopListenerThread(uintptr_t argument) { + (void)argument; + DwmSetCurrentThreadName("dwm.listen"); + while (gDwm.Running) { + SYS_HANDLE endpoint = SYS_HANDLE_INVALID; + SYS_PORT_CONNECTION_INFO connection; + memset(&connection, 0, sizeof(connection)); + connection.Size = sizeof(connection); + SYS_ACCEPT_PORT_REQUEST accept; + memset(&accept, 0, sizeof(accept)); + accept.Size = sizeof(accept); + accept.ListenerHandle = gDwm.DesktopListener; + accept.Flags = SYS_PORT_ACCEPT_FLAG_NONE; + accept.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + accept.EndpointHandleOut = &endpoint; + accept.ConnectionInfoOut = &connection; + KSTATUS status = SysAcceptPort(&accept); + if (!KSUCCESS(status)) break; + + DWM_CLIENT* client = (DWM_CLIENT*)malloc(sizeof(*client)); + DWM_CLIENT_THREAD_CONTEXT* context = (DWM_CLIENT_THREAD_CONTEXT*)malloc(sizeof(*context)); + if (!client || !context) { + free(client); + free(context); + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + continue; + } + memset(client, 0, sizeof(*client)); + client->Endpoint = endpoint; + client->ProcessId = connection.ClientProcessId; + context->Client = client; + + status = DwmLock(); + if (!KSUCCESS(status)) { + free(context); + free(client); + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + continue; + } + client->Next = gDwm.Clients; + gDwm.Clients = client; + DwmUnlock(); + + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)DwmClientThread; + create.Argument = (uintptr_t)context; + create.Priority = 1u; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + status = SysCreateThread(&create); + if (!KSUCCESS(status)) { + if (KSUCCESS(DwmLock())) { + DwmRemoveClientLocked(client); + DwmUnlock(); + } + free(context); + free(client); + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + } + } + SysExitThread(0); +} + +// Accepts init's private service-control connection. +static KSTATUS DwmAcceptControl(SYS_HANDLE listener, SYS_HANDLE* endpointOut) { + if (listener == SYS_HANDLE_INVALID || !endpointOut) return NTLSTATUS_INVALID_PARAMETER; + SYS_ACCEPT_PORT_REQUEST accept; + memset(&accept, 0, sizeof(accept)); + accept.Size = sizeof(accept); + accept.ListenerHandle = listener; + accept.Flags = SYS_PORT_ACCEPT_FLAG_NONE; + accept.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + accept.EndpointHandleOut = endpointOut; + return SysAcceptPort(&accept); +} + +// Connects to RSSRV's init-delegated private Session 0 control listener. +static KSTATUS DwmConnectSession(SYS_HANDLE listener, SYS_HANDLE* endpointOut) { + if (listener == SYS_HANDLE_INVALID || !endpointOut) return NTLSTATUS_INVALID_PARAMETER; + SYS_CONNECT_PORT_REQUEST connect; + memset(&connect, 0, sizeof(connect)); + connect.Size = sizeof(connect); + connect.ListenerHandle = listener; + connect.Flags = SYS_PORT_CONNECT_FLAG_NONE; + connect.EndpointHandleOut = endpointOut; + return SysConnectPort(&connect); +} + +// Reports DWM readiness and delegates its public desktop listener to init for namespace publication. +static KSTATUS DwmSendReady(SYS_HANDLE endpoint, SYS_HANDLE desktopListener) { + if (endpoint == SYS_HANDLE_INVALID || desktopListener == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + NTL_SERVICE_CONTROL_MESSAGE message; + memset(&message, 0, sizeof(message)); + message.Size = sizeof(message); + message.Version = NTL_SERVICE_CONTROL_VERSION; + message.Operation = NTL_SERVICE_CONTROL_READY; + message.ExportCount = 1u; + message.Status = NTLSTATUS_SUCCESS; + + SYS_PORT_HANDLE_TRANSFER transfer; + transfer.Handle = desktopListener; + transfer.DesiredRights = SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT; + transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + + SYS_SEND_PORT_REQUEST send; + memset(&send, 0, sizeof(send)); + send.Size = sizeof(send); + send.EndpointHandle = endpoint; + send.Flags = SYS_PORT_FLAG_NONE; + send.Payload = &message; + send.PayloadLength = sizeof(message); + send.Handles = &transfer; + send.HandleCount = 1u; + send.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + return SysSendPort(&send); +} + +// Waits until init requests service stop or disconnects the private control port. +static void DwmControlLoop(SYS_HANDLE endpoint) { + for (;;) { + NTL_SERVICE_CONTROL_MESSAGE message; + SYS_PORT_MESSAGE_INFO info; + memset(&message, 0, sizeof(message)); + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = endpoint; + receive.Payload = &message; + receive.PayloadCapacity = sizeof(message); + receive.InfoOut = &info; + receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysReceivePort(&receive); + if (status == NTLSTATUS_PORT_DISCONNECTED || !KSUCCESS(status)) return; + if (info.Type != SYS_PORT_MESSAGE_NOTIFICATION || info.HandleCount != 0 || + info.PayloadLength != sizeof(message) || message.Size != sizeof(message) || + message.Version != NTL_SERVICE_CONTROL_VERSION || message.ExportCount != 0 || + message.Reserved[0] != 0 || message.Reserved[1] != 0 || message.Reserved[2] != 0) + continue; + if (message.Operation == NTL_SERVICE_CONTROL_STOP) return; + } +} + +// Creates DWM's private full-screen composition surface and CPU mapping. +static KSTATUS DwmCreateCompositionSurface(void) { + SYS_CREATE_SURFACE_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Width = (uint32_t)gDwm.Width; + create.Height = (uint32_t)gDwm.Height; + create.Format = SYS_SURFACE_FORMAT_XRGB8888; + create.Flags = SYS_SURFACE_FLAG_NONE; + create.HandleOut = &gDwm.CompositionSurface; + KSTATUS status = SysCreateSurface(&create); + if (!KSUCCESS(status)) return status; + return SysMapSurface(gDwm.CompositionSurface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, + &gDwm.CompositionBase, &gDwm.CompositionPitch); +} + +// Creates the public desktop listener owned by this DWM instance. +static KSTATUS DwmCreateDesktopListener(void) { + SYS_CREATE_PORT_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Flags = SYS_PORT_FLAG_NONE; + create.Backlog = DWM_DESKTOP_BACKLOG; + create.HandleOut = &gDwm.DesktopListener; + return SysCreatePort(&create); +} + +// Starts one retained DWM worker and returns its waitable thread capability. +static KSTATUS DwmStartThread(uintptr_t entry, const char* name, SYS_HANDLE* threadOut) { + (void)name; + if (!entry || !threadOut) return NTLSTATUS_INVALID_PARAMETER; + *threadOut = SYS_HANDLE_INVALID; + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = entry; + create.Priority = 1u; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + create.ThreadHandleOut = threadOut; + return SysCreateThread(&create); +} + +// Starts the Session 0 DWM service with no namespace-admin or modeset authority. +void _start(void) { + KSTATUS status = NtlInitializeExceptionRuntime(); + if (!KSUCCESS(status)) DwmExit(1); + DwmSetCurrentThreadName("dwm.main"); + + SYS_SECURITY_CONTEXT_INFO security; + memset(&security, 0, sizeof(security)); + security.Size = sizeof(security); + status = SysQuerySecurityContext(SYS_SECURITY_CONTEXT_CURRENT, &security); + if (!KSUCCESS(status) || security.EffectivePrivileges != 0 || security.DelegablePrivileges != 0 || + security.ServiceClaims != 0 || security.DelegableServiceClaims != 0) + DwmExit(2); + + SYS_HANDLE controlListener = SysGetStartupCapability(SYS_STARTUP_CAP_SERVICE_CONTROL); + SYS_HANDLE displayManager = SysGetStartupCapability(SYS_STARTUP_CAP_DISPLAY_MANAGER); + SYS_HANDLE sessionListener = SysGetStartupCapability(SYS_STARTUP_CAP_SESSION_CONTROL); + if (controlListener == SYS_HANDLE_INVALID || displayManager == SYS_HANDLE_INVALID || + sessionListener == SYS_HANDLE_INVALID) + DwmExit(3); + + memset(&gDwm, 0, sizeof(gDwm)); + gDwm.Mutex = SYS_HANDLE_INVALID; + gDwm.DirtyEvent = SYS_HANDLE_INVALID; + gDwm.DisplayManager = displayManager; + gDwm.SessionEndpoint = SYS_HANDLE_INVALID; + gDwm.DesktopListener = SYS_HANDLE_INVALID; + gDwm.CompositionSurface = SYS_HANDLE_INVALID; + gDwm.NextWindowId = 1u; + gDwm.CursorVisible = 1u; + + SYS_DISPLAY_INFO displayInfo; + memset(&displayInfo, 0, sizeof(displayInfo)); + status = SysQueryDisplayManagerInfo(displayManager, &displayInfo); + if (!KSUCCESS(status) || displayInfo.Mode.Width == 0 || displayInfo.Mode.Height == 0 || + displayInfo.Mode.Format != DisplayPixelFormatX8R8G8B8) + DwmExit(4); + gDwm.Width = (int32_t)displayInfo.Mode.Width; + gDwm.Height = (int32_t)displayInfo.Mode.Height; + gDwm.PointerX = gDwm.Width / 2; + gDwm.PointerY = gDwm.Height / 2; + + status = SysCreateMutex(&gDwm.Mutex); + if (!KSUCCESS(status)) DwmExit(5); + status = SysCreateEvent(DWM_EVENT_AUTO_RESET, 0u, &gDwm.DirtyEvent); + if (!KSUCCESS(status)) DwmExit(5); + status = DwmCreateCompositionSurface(); + if (!KSUCCESS(status)) DwmExit(5); + status = DwmCreateDesktopListener(); + if (!KSUCCESS(status)) DwmExit(5); + status = DwmConnectSession(sessionListener, &gDwm.SessionEndpoint); + (void)SysCloseHandle(sessionListener); + if (!KSUCCESS(status)) DwmExit(5); + + SYS_HANDLE controlEndpoint = SYS_HANDLE_INVALID; + status = DwmAcceptControl(controlListener, &controlEndpoint); + (void)SysCloseHandle(controlListener); + if (!KSUCCESS(status)) DwmExit(6); + + gDwm.Running = 1u; + SYS_HANDLE compositorThread = SYS_HANDLE_INVALID; + SYS_HANDLE inputThread = SYS_HANDLE_INVALID; + SYS_HANDLE listenerThread = SYS_HANDLE_INVALID; + status = DwmStartThread((uintptr_t)DwmCompositorThread, "dwm.compositor", &compositorThread); + if (!KSUCCESS(status)) DwmExit(7); + status = DwmStartThread((uintptr_t)DwmSessionInputThread, "dwm.session.input", &inputThread); + if (!KSUCCESS(status)) DwmExit(7); + status = DwmStartThread((uintptr_t)DwmDesktopListenerThread, "dwm.listen", &listenerThread); + if (!KSUCCESS(status)) DwmExit(7); + + if (KSUCCESS(DwmLock())) { + DISPLAY_RECT whole = { 0, 0, gDwm.Width, gDwm.Height }; + DwmDirtyLocked(&whole); + DwmApplyCursorLocked(); + DwmUnlock(); + } + + status = DwmSendReady(controlEndpoint, gDwm.DesktopListener); + if (!KSUCCESS(status)) DwmExit(8); + + DwmControlLoop(controlEndpoint); + gDwm.Running = 0u; + (void)SysSignalEvent(gDwm.DirtyEvent); + (void)SysDisconnectPort(gDwm.SessionEndpoint); + (void)SysDisconnectPort(controlEndpoint); + (void)SysCloseHandle(gDwm.DesktopListener); + (void)SysWait(compositorThread, SYS_WAIT_FLAG_NONE); + (void)SysWait(inputThread, SYS_WAIT_FLAG_NONE); + (void)SysCloseHandle(compositorThread); + (void)SysCloseHandle(inputThread); + (void)SysCloseHandle(listenerThread); + DwmExit(0); +} diff --git a/code/user/system/init/init.c b/code/user/system/init/init.c index 53ee50e..3a6cb02 100644 --- a/code/user/system/init/init.c +++ b/code/user/system/init/init.c @@ -5,21 +5,40 @@ #include #include #include +#include +#include +#include #include #include #include #include #include -#define INIT_SERVICE_READY_TIMEOUT_MS 5000u -#define INIT_SERVICE_STOP_TIMEOUT_MS 5000u +#include "service_config.h" -typedef struct INIT_RESOURCE_SERVICE { +typedef enum INIT_SERVICE_STATE { + InitServiceStopped = 0, + InitServiceStarting, + InitServiceReady, + InitServiceStopping, + InitServiceFailed, +} INIT_SERVICE_STATE; + +typedef struct INIT_SERVICE { + const INIT_SERVICE_DESCRIPTOR* Descriptor; SYS_HANDLE Process; SYS_HANDLE ControlEndpoint; + INIT_SERVICE_STATE State; +} INIT_SERVICE; + +typedef struct INIT_RESOURCE_SERVICE { + INIT_SERVICE Service; SYS_HANDLE AdminListener; + SYS_HANDLE SessionControlListener; } INIT_RESOURCE_SERVICE; +static INIT_SERVICE_CONFIG gServiceConfig; + // Writes one string through the privileged kernel debug channel. static void debug_print(const char* text) { SYS_PRINT_DEBUG_REQUEST request = { sizeof(request), SysSvFromCstr(text) }; @@ -35,11 +54,59 @@ static void debug_status(KSTATUS status) { debug_print(buffer); } +// Writes one unsigned decimal value without pulling in a formatting runtime. +static void debug_u32(uint32_t value) { + char buffer[11]; + uint32_t length = 0; + do { + buffer[length++] = (char)('0' + value % 10u); + value /= 10u; + } while (value != 0u); + for (uint32_t i = 0; i < length / 2u; ++i) { + char temporary = buffer[i]; + buffer[i] = buffer[length - 1u - i]; + buffer[length - 1u - i] = temporary; + } + buffer[length] = '\0'; + debug_print(buffer); +} + +// Writes one signed decimal value without pulling in a formatting runtime. +static void debug_i32(int32_t value) { + if (value < 0) debug_print("-"); + uint32_t magnitude = value < 0 ? (uint32_t)(-(int64_t)value) : (uint32_t)value; + debug_u32(magnitude); +} + +// Reports a managed service that terminated before completing its READY contract. +static void debug_early_service_exit(const INIT_SERVICE* service) { + if (!service || !service->Descriptor || service->Process == SYS_HANDLE_INVALID) return; + KSTATUS waitStatus = SysWaitSingle(service->Process, SYS_WAIT_FLAG_NONE, 0); + if (waitStatus != NTLSTATUS_SUCCESS) return; + + int32_t exitCode = -1; + if (SysGetProcessExitCode(service->Process, &exitCode) != NTLSTATUS_SUCCESS) return; + debug_print("[init] service "); + debug_print(service->Descriptor->Id); + debug_print(" exited during startup with code "); + debug_i32(exitCode); + debug_print("\n"); +} + // Exits init without relying on a compiler runtime return path. static void exit_now(int code) { for (;;) SysExitProcess(code); } +// Initializes one managed-service runtime record in the stopped state. +static void initialize_service(INIT_SERVICE* service, const INIT_SERVICE_DESCRIPTOR* descriptor) { + memset(service, 0, sizeof(*service)); + service->Descriptor = descriptor; + service->Process = SYS_HANDLE_INVALID; + service->ControlEndpoint = SYS_HANDLE_INVALID; + service->State = InitServiceStopped; +} + // Derives the least-privileged security context required by RSSRV. static KSTATUS derive_resource_service_context(SYS_HANDLE* contextOut) { if (!contextOut) return NTLSTATUS_INVALID_PARAMETER; @@ -57,6 +124,23 @@ static KSTATUS derive_resource_service_context(SYS_HANDLE* contextOut) { return SysDeriveSecurityContext(&request); } +// Derives the unprivileged security context used by replaceable user-mode services. +static KSTATUS derive_unprivileged_service_context(SYS_HANDLE* contextOut) { + if (!contextOut) return NTLSTATUS_INVALID_PARAMETER; + *contextOut = SYS_HANDLE_INVALID; + SYS_DERIVE_SECURITY_CONTEXT_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.SourceContext = SYS_SECURITY_CONTEXT_CURRENT; + request.EffectivePrivileges = 0; + request.DelegablePrivileges = 0; + request.ServiceClaims = 0; + request.DelegableServiceClaims = 0; + request.Flags = SYS_SECURITY_CONTEXT_FLAG_NONE; + request.ContextHandleOut = contextOut; + return SysDeriveSecurityContext(&request); +} + // Creates one private Port listener used only between init and a managed service. static KSTATUS create_control_listener(SYS_HANDLE* listenerOut) { SYS_CREATE_PORT_REQUEST request; @@ -79,174 +163,72 @@ static KSTATUS connect_control_listener(SYS_HANDLE listener, SYS_HANDLE* endpoin return SysConnectPort(&request); } -// Waits for RSSRV readiness and receives its private administrative namespace listener. -static KSTATUS wait_resource_service_ready(SYS_HANDLE controlEndpoint, SYS_HANDLE* adminListenerOut) { - if (!adminListenerOut) return NTLSTATUS_INVALID_PARAMETER; - *adminListenerOut = SYS_HANDLE_INVALID; - - NTL_SERVICE_CONTROL_MESSAGE message; - SYS_PORT_MESSAGE_INFO info; - SYS_HANDLE adminListener = SYS_HANDLE_INVALID; - memset(&message, 0, sizeof(message)); - memset(&info, 0, sizeof(info)); - info.Size = sizeof(info); - - SYS_RECEIVE_PORT_REQUEST receive; - memset(&receive, 0, sizeof(receive)); - receive.Size = sizeof(receive); - receive.EndpointHandle = controlEndpoint; - receive.Flags = SYS_PORT_FLAG_NONE; - receive.Payload = &message; - receive.PayloadCapacity = sizeof(message); - receive.HandlesOut = &adminListener; - receive.HandleCapacity = 1; - receive.InfoOut = &info; - receive.TimeoutMilliseconds = INIT_SERVICE_READY_TIMEOUT_MS; - KSTATUS status = SysReceivePort(&receive); - if (!KSUCCESS(status)) return status; - if (info.Type != SYS_PORT_MESSAGE_NOTIFICATION || info.PayloadLength != sizeof(message) || - info.HandleCount != 1 || adminListener == SYS_HANDLE_INVALID || - message.Size != sizeof(message) || message.Version != NTL_SERVICE_CONTROL_VERSION || - message.Operation != NTL_SERVICE_CONTROL_READY || - message.Flags != NTL_SERVICE_READY_FLAG_RESOURCE_ADMIN_LISTENER || - !KSUCCESS(message.Status)) { - if (adminListener != SYS_HANDLE_INVALID) (void)SysCloseHandle(adminListener); - return NTLSTATUS_FAILURE; - } - - *adminListenerOut = adminListener; - return NTLSTATUS_SUCCESS; -} - -// Verifies private namespace administration and public /SystemRoot lookup before declaring RSSRV usable. -static KSTATUS verify_resource_service(SYS_HANDLE adminListener, SYS_HANDLE namespaceVolume) { - SYS_HANDLE admin = SYS_HANDLE_INVALID; - KSTATUS status = NtlResourceConnectListener(adminListener, &admin); - if (!KSUCCESS(status)) return status; - - uint32_t kind = NTL_RESOURCE_KIND_INVALID; - status = NtlResourceQuery(admin, SYS_SV_LITERAL("/SystemRoot"), &kind); - if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_VOLUME) { - (void)NtlResourceClose(admin); - return KSUCCESS(status) ? NTLSTATUS_FAILURE : status; - } - - // Exercise capability-based administration without leaving a bootstrap-only namespace entry behind. - status = NtlResourceBind(admin, SYS_SV_LITERAL("/Init/BootstrapProbe"), - NTL_RESOURCE_KIND_VOLUME, namespaceVolume, - SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN, - SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN); - if (!KSUCCESS(status)) { - (void)NtlResourceClose(admin); - return status; - } - - SYS_HANDLE publicSession = SYS_HANDLE_INVALID; - status = NtlResourceConnect(&publicSession); - if (!KSUCCESS(status)) { - (void)NtlResourceUnbind(admin, SYS_SV_LITERAL("/Init/BootstrapProbe")); - (void)NtlResourceClose(admin); - return status; - } - - SYS_HANDLE volume = SYS_HANDLE_INVALID; - status = NtlResourceLookup(publicSession, SYS_SV_LITERAL("/SystemRoot"), - SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN, &volume); - if (KSUCCESS(status)) (void)SysCloseHandle(volume); - if (!KSUCCESS(status)) { - (void)NtlResourceClose(publicSession); - (void)NtlResourceUnbind(admin, SYS_SV_LITERAL("/Init/BootstrapProbe")); - (void)NtlResourceClose(admin); - return status; - } - - kind = NTL_RESOURCE_KIND_INVALID; - status = NtlResourceQuery(publicSession, SYS_SV_LITERAL("/Init/BootstrapProbe"), &kind); - if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_VOLUME) { - (void)NtlResourceClose(publicSession); - (void)NtlResourceUnbind(admin, SYS_SV_LITERAL("/Init/BootstrapProbe")); - (void)NtlResourceClose(admin); - return KSUCCESS(status) ? NTLSTATUS_FAILURE : status; - } - - status = NtlResourceUnbind(admin, SYS_SV_LITERAL("/Init/BootstrapProbe")); - if (KSUCCESS(status)) { - kind = NTL_RESOURCE_KIND_INVALID; - KSTATUS queryStatus = NtlResourceQuery(publicSession, SYS_SV_LITERAL("/Init/BootstrapProbe"), &kind); - if (queryStatus != NTLSTATUS_NOT_FOUND) status = NTLSTATUS_FAILURE; - } - - (void)NtlResourceClose(publicSession); - (void)NtlResourceClose(admin); - return status; -} - -// Tears down retained parent-side service handles without sending a stop message. -static void close_resource_service(INIT_RESOURCE_SERVICE* service) { +// Closes the generic parent-side handles retained for one managed service. +static void close_service_handles(INIT_SERVICE* service) { if (!service) return; if (service->ControlEndpoint != SYS_HANDLE_INVALID) { (void)SysDisconnectPort(service->ControlEndpoint); (void)SysCloseHandle(service->ControlEndpoint); } - if (service->AdminListener != SYS_HANDLE_INVALID) - (void)SysCloseHandle(service->AdminListener); if (service->Process != SYS_HANDLE_INVALID) (void)SysCloseHandle(service->Process); service->Process = SYS_HANDLE_INVALID; service->ControlEndpoint = SYS_HANDLE_INVALID; - service->AdminListener = SYS_HANDLE_INVALID; } -// Starts one restricted, import-free RSSRV instance through the userspace process loader. -static KSTATUS start_resource_service(SYS_HANDLE systemVolume, - SYS_HANDLE namespaceVolume, - SYS_HANDLE volumeManager, - SYS_HANDLE resourceContext, - INIT_RESOURCE_SERVICE* serviceOut) { - if (!serviceOut) return NTLSTATUS_INVALID_PARAMETER; - memset(serviceOut, 0, sizeof(*serviceOut)); +// Starts one managed service process and establishes its private control connection. +static KSTATUS start_service_process(SYS_HANDLE systemVolume, + SYS_HANDLE securityContext, + const NTL_PROCESS_STARTUP_CAPABILITY* startupCapabilities, + uint32_t startupCapabilityCount, + INIT_SERVICE* service) { + if (!service || !service->Descriptor || service->Descriptor->ImagePath[0] == '\0' || + startupCapabilityCount > SYS_STARTUP_CAPABILITY_MAX - 1u || + (startupCapabilityCount && !startupCapabilities)) + return NTLSTATUS_INVALID_PARAMETER; + if (service->Process != SYS_HANDLE_INVALID || service->ControlEndpoint != SYS_HANDLE_INVALID || + (service->State != InitServiceStopped && service->State != InitServiceFailed)) + return NTLSTATUS_FAILURE; + + service->State = InitServiceStarting; SYS_HANDLE controlListener = SYS_HANDLE_INVALID; KSTATUS status = create_control_listener(&controlListener); - if (!KSUCCESS(status)) return status; + if (!KSUCCESS(status)) { + service->State = InitServiceFailed; + return status; + } - NTL_PROCESS_STARTUP_CAPABILITY startup[3]; + NTL_PROCESS_STARTUP_CAPABILITY startup[SYS_STARTUP_CAPABILITY_MAX]; memset(startup, 0, sizeof(startup)); - startup[0].Tag = SYS_STARTUP_CAP_SYSTEM_VOLUME_NAMESPACE; - startup[0].SourceHandle = namespaceVolume; - startup[0].DesiredRights = SYS_RIGHT_QUERY | SYS_RIGHT_TRANSFER | - SYS_VOLUME_RIGHT_OPEN | SYS_VOLUME_RIGHT_MODIFY; - startup[0].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; - startup[1].Tag = SYS_STARTUP_CAP_SERVICE_CONTROL; - startup[1].SourceHandle = controlListener; - startup[1].DesiredRights = SYS_RIGHT_QUERY | SYS_PORT_RIGHT_ACCEPT; - startup[1].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; - startup[2].Tag = SYS_STARTUP_CAP_VOLUME_MANAGER; - startup[2].SourceHandle = volumeManager; - startup[2].DesiredRights = SYS_RIGHT_QUERY | - SYS_VOLUME_MANAGER_RIGHT_ENUMERATE | SYS_VOLUME_MANAGER_RIGHT_OPEN | - SYS_VOLUME_MANAGER_RIGHT_WAIT; - startup[2].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; + for (uint32_t i = 0; i < startupCapabilityCount; ++i) + startup[i] = startupCapabilities[i]; + startup[startupCapabilityCount].Tag = SYS_STARTUP_CAP_SERVICE_CONTROL; + startup[startupCapabilityCount].SourceHandle = controlListener; + startup[startupCapabilityCount].DesiredRights = SYS_RIGHT_QUERY | SYS_PORT_RIGHT_ACCEPT; + startup[startupCapabilityCount].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; SYS_HANDLE process = SYS_HANDLE_INVALID; + SYS_STRING_VIEW imagePath = SysSvFromCstr(service->Descriptor->ImagePath); NTL_CREATE_PROCESS_AT_REQUEST create; memset(&create, 0, sizeof(create)); create.Size = sizeof(create); create.Flags = NTL_PROCESS_LOAD_FLAG_NONE; create.RootHandle = systemVolume; - create.Path = SYS_SV_LITERAL("system/rssrv.exe"); - create.CommandLine = SYS_SV_LITERAL("system/rssrv.exe"); - create.SecurityContext = resourceContext; + create.Path = imagePath; + create.CommandLine = imagePath; + create.SecurityContext = securityContext; create.ModuleRootHandle = systemVolume; create.ModuleDirectory = SYS_SV_LITERAL("system"); create.ResolveModuleName = NtlResolveModuleNameIdentity; create.ResolveContext = (void*)0; create.StartupCapabilities = startup; - create.StartupCapabilityCount = 3; + create.StartupCapabilityCount = startupCapabilityCount + 1u; create.ProcessHandleOut = &process; status = NtlCreateProcessAt(&create); if (!KSUCCESS(status)) { (void)SysCloseHandle(controlListener); + service->State = InitServiceFailed; return status; } @@ -256,37 +238,552 @@ static KSTATUS start_resource_service(SYS_HANDLE systemVolume, if (!KSUCCESS(status)) { (void)SysKillProcess(process, 1); (void)SysCloseHandle(process); + service->State = InitServiceFailed; return status; } + service->Process = process; + service->ControlEndpoint = controlEndpoint; + return NTLSTATUS_SUCCESS; +} + +// Waits for one service READY notification and receives its service-defined export handles. +static KSTATUS wait_service_ready(INIT_SERVICE* service, + SYS_HANDLE* exportsOut, + uint32_t exportCapacity, + uint32_t* exportCountOut) { + if (!service || !service->Descriptor || !exportCountOut || + exportCapacity > SYS_PORT_MAX_TRANSFER_HANDLES || (exportCapacity && !exportsOut)) + return NTLSTATUS_INVALID_PARAMETER; + + for (uint32_t i = 0; i < exportCapacity; ++i) exportsOut[i] = SYS_HANDLE_INVALID; + *exportCountOut = 0; + + NTL_SERVICE_CONTROL_MESSAGE message; + SYS_PORT_MESSAGE_INFO info; + memset(&message, 0, sizeof(message)); + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = service->ControlEndpoint; + receive.Flags = SYS_PORT_FLAG_NONE; + receive.Payload = &message; + receive.PayloadCapacity = sizeof(message); + receive.HandlesOut = exportsOut; + receive.HandleCapacity = exportCapacity; + receive.InfoOut = &info; + receive.TimeoutMilliseconds = service->Descriptor->ReadyTimeoutMilliseconds; + KSTATUS status = SysReceivePort(&receive); + if (!KSUCCESS(status)) return status; + + bool valid = info.Type == SYS_PORT_MESSAGE_NOTIFICATION && + info.PayloadLength == sizeof(message) && + message.Size == sizeof(message) && + message.Version == NTL_SERVICE_CONTROL_VERSION && + message.Operation == NTL_SERVICE_CONTROL_READY && + message.ExportCount == info.HandleCount && + message.ExportCount <= exportCapacity && + message.Reserved[0] == 0 && message.Reserved[1] == 0 && message.Reserved[2] == 0; + if (!valid || !KSUCCESS(message.Status)) { + for (uint32_t i = 0; i < info.HandleCount && i < exportCapacity; ++i) { + if (exportsOut[i] != SYS_HANDLE_INVALID) { + (void)SysCloseHandle(exportsOut[i]); + exportsOut[i] = SYS_HANDLE_INVALID; + } + } + return valid ? message.Status : NTLSTATUS_FAILURE; + } + + *exportCountOut = info.HandleCount; + return NTLSTATUS_SUCCESS; +} + +// Waits for RSSRV readiness and receives init-only namespace and DWM-session bootstrap exports. +static KSTATUS wait_resource_service_ready(INIT_SERVICE* service, + SYS_HANDLE* adminListenerOut, + SYS_HANDLE* sessionControlListenerOut) { + if (!adminListenerOut || !sessionControlListenerOut) return NTLSTATUS_INVALID_PARAMETER; + *adminListenerOut = SYS_HANDLE_INVALID; + *sessionControlListenerOut = SYS_HANDLE_INVALID; + + SYS_HANDLE exports[2] = { SYS_HANDLE_INVALID, SYS_HANDLE_INVALID }; + uint32_t exportCount = 0; + KSTATUS status = wait_service_ready(service, exports, 2u, &exportCount); + if (!KSUCCESS(status)) return status; + if (exportCount != 2u || exports[0] == SYS_HANDLE_INVALID || exports[1] == SYS_HANDLE_INVALID) { + for (uint32_t i = 0; i < 2u; ++i) + if (exports[i] != SYS_HANDLE_INVALID) (void)SysCloseHandle(exports[i]); + return NTLSTATUS_FAILURE; + } + + *adminListenerOut = exports[0]; + *sessionControlListenerOut = exports[1]; + return NTLSTATUS_SUCCESS; +} + +// Verifies namespace structure, administration, symbolic links, enumeration, and public capability lookup. +static KSTATUS verify_resource_service(SYS_HANDLE adminListener, SYS_HANDLE namespaceVolume) { + SYS_HANDLE admin = SYS_HANDLE_INVALID; + SYS_HANDLE publicSession = SYS_HANDLE_INVALID; + SYS_HANDLE volume = SYS_HANDLE_INVALID; + SYS_HANDLE displayDevice = SYS_HANDLE_INVALID; + SYS_HANDLE consoleAlias = SYS_HANDLE_INVALID; + uint32_t initDirectoryCreated = 0; + uint32_t aliasCreated = 0; + uint32_t probeBound = 0; + NTL_RESOURCE_ENUM_CURSOR cursor; + uint32_t sawAlias = 0; + uint32_t sawProbe = 0; + memset(&cursor, 0, sizeof(cursor)); + KSTATUS status = NtlResourceConnectListener(adminListener, &admin); + if (!KSUCCESS(status)) return status; + + uint32_t kind = NTL_RESOURCE_KIND_INVALID; + status = NtlResourceQuery(admin, SYS_SV_LITERAL("/"), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + status = NtlResourceQuery(admin, SYS_SV_LITERAL("/SystemRoot"), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_SYMBOLIC_LINK) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + status = NtlResourceQuery(admin, SYS_SV_LITERAL("/Device/Display0"), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_DEVICE) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + status = NtlResourceLookup(admin, SYS_SV_LITERAL("/Device/Display0"), + SYS_RIGHT_QUERY | SYS_DISPLAY_MANAGER_RIGHT_ENUMERATE, &displayDevice); + if (!KSUCCESS(status)) goto cleanup; + SYS_DISPLAY_INFO displayInfo; + memset(&displayInfo, 0, sizeof(displayInfo)); + status = SysQueryDisplayManagerInfo(displayDevice, &displayInfo); + (void)SysCloseHandle(displayDevice); + displayDevice = SYS_HANDLE_INVALID; + if (!KSUCCESS(status) || displayInfo.Mode.Width == 0 || displayInfo.Mode.Height == 0) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + + status = NtlResourceQuery(admin, SYS_SV_LITERAL("/Sessions/0"), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + status = NtlResourceQuery(admin, SYS_SV_LITERAL("/Sessions/0/Desktop"), &kind); + if (status != NTLSTATUS_NOT_FOUND) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + status = NtlResourceQuery(admin, SYS_SV_LITERAL("/Sessions/0/Console"), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_CAPABILITY) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + status = NtlResourceQuery(admin, SYS_SV_LITERAL("/Console"), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_SYMBOLIC_LINK) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + status = NtlResourceLookup(admin, SYS_SV_LITERAL("/Console"), + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT, &consoleAlias); + if (!KSUCCESS(status)) goto cleanup; + (void)SysCloseHandle(consoleAlias); + consoleAlias = SYS_HANDLE_INVALID; + status = NtlResourceQuery(admin, SYS_SV_LITERAL(NTL_INPUT_SERVICE_PATH), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_CAPABILITY) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + SYS_HANDLE inputSession = SYS_HANDLE_INVALID; + status = NtlInputConnect(&inputSession); + if (!KSUCCESS(status)) goto cleanup; + NTL_INPUT_STATE inputState; + status = NtlInputQueryState(inputSession, &inputState); + (void)NtlInputClose(inputSession); + if (!KSUCCESS(status) || inputState.Size != sizeof(inputState) || + inputState.Flags != NTL_INPUT_STATE_FLAG_NONE) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + + status = NtlResourceQuery(admin, SYS_SV_LITERAL(NTL_CONSOLE_SERVICE_PATH), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_CAPABILITY) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + SYS_HANDLE consoleSession = SYS_HANDLE_INVALID; + status = NtlConsoleConnect(&consoleSession); + if (!KSUCCESS(status)) goto cleanup; + NTL_CONSOLE_INFO consoleInfo; + memset(&consoleInfo, 0, sizeof(consoleInfo)); + status = NtlConsoleQueryInfo(consoleSession, &consoleInfo); + (void)NtlConsoleClose(consoleSession); + if (!KSUCCESS(status) || consoleInfo.Size != sizeof(consoleInfo) || + consoleInfo.Flags != NTL_CONSOLE_FLAG_NONE || consoleInfo.Columns == 0 || + consoleInfo.Rows == 0 || consoleInfo.PixelWidth == 0 || consoleInfo.PixelHeight == 0) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + + // Exercise explicit parent creation rather than allowing bind to manufacture namespace directories. + status = NtlResourceCreateDirectory(admin, SYS_SV_LITERAL("/Init")); + if (!KSUCCESS(status)) goto cleanup; + initDirectoryCreated = 1; + + // Exercise a symbolic-link chain: /Init/SystemRoot -> /SystemRoot -> /Device/HarddiskVolumeN. + status = NtlResourceCreateSymbolicLink(admin, SYS_SV_LITERAL("/Init/SystemRoot"), + SYS_SV_LITERAL("/SystemRoot")); + if (!KSUCCESS(status)) goto cleanup; + aliasCreated = 1; + + status = NtlResourceBind(admin, SYS_SV_LITERAL("/Init/BootstrapProbe"), + NTL_RESOURCE_KIND_VOLUME, namespaceVolume, + SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN, + SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN); + if (!KSUCCESS(status)) goto cleanup; + probeBound = 1; + + status = NtlResourceConnect(&publicSession); + if (!KSUCCESS(status)) goto cleanup; + + status = NtlResourceLookup(publicSession, SYS_SV_LITERAL("/SystemRoot"), + SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN, &volume); + if (!KSUCCESS(status)) goto cleanup; + (void)SysCloseHandle(volume); + volume = SYS_HANDLE_INVALID; + + status = NtlResourceLookup(publicSession, SYS_SV_LITERAL("/Init/SystemRoot"), + SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN, &volume); + if (!KSUCCESS(status)) goto cleanup; + (void)SysCloseHandle(volume); + volume = SYS_HANDLE_INVALID; + + kind = NTL_RESOURCE_KIND_INVALID; + status = NtlResourceQuery(publicSession, SYS_SV_LITERAL("/Init/BootstrapProbe"), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_VOLUME) { + status = KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + goto cleanup; + } + + for (;;) { + NTL_RESOURCE_ENUM_ENTRY entry; + status = NtlResourceEnumerate(publicSession, SYS_SV_LITERAL("/Init"), &cursor, &entry); + if (status == NTLSTATUS_NO_MORE_FILES) { + status = NTLSTATUS_SUCCESS; + break; + } + if (!KSUCCESS(status)) goto cleanup; + if (strcmp(entry.Name, "SystemRoot") == 0 && entry.Kind == NTL_RESOURCE_KIND_SYMBOLIC_LINK) + sawAlias = 1; + if (strcmp(entry.Name, "BootstrapProbe") == 0 && entry.Kind == NTL_RESOURCE_KIND_VOLUME) + sawProbe = 1; + } + if (!sawAlias || !sawProbe) { + status = NTLSTATUS_FAILURE; + goto cleanup; + } + + status = NtlResourceUnbind(admin, SYS_SV_LITERAL("/Init/BootstrapProbe")); + if (!KSUCCESS(status)) goto cleanup; + probeBound = 0; + kind = NTL_RESOURCE_KIND_INVALID; + if (NtlResourceQuery(publicSession, SYS_SV_LITERAL("/Init/BootstrapProbe"), &kind) != NTLSTATUS_NOT_FOUND) { + status = NTLSTATUS_FAILURE; + goto cleanup; + } + +cleanup: + if (consoleAlias != SYS_HANDLE_INVALID) (void)SysCloseHandle(consoleAlias); + if (displayDevice != SYS_HANDLE_INVALID) (void)SysCloseHandle(displayDevice); + if (volume != SYS_HANDLE_INVALID) (void)SysCloseHandle(volume); + if (publicSession != SYS_HANDLE_INVALID) (void)NtlResourceClose(publicSession); + if (probeBound) (void)NtlResourceUnbind(admin, SYS_SV_LITERAL("/Init/BootstrapProbe")); + if (aliasCreated) { + KSTATUS cleanupStatus = NtlResourceUnbind(admin, SYS_SV_LITERAL("/Init/SystemRoot")); + if (KSUCCESS(status) && !KSUCCESS(cleanupStatus)) status = cleanupStatus; + } + if (initDirectoryCreated) { + KSTATUS cleanupStatus = NtlResourceUnbind(admin, SYS_SV_LITERAL("/Init")); + if (KSUCCESS(status) && !KSUCCESS(cleanupStatus)) status = cleanupStatus; + } + (void)NtlResourceClose(admin); + return status; +} + +// Tears down retained RSSRV-specific and generic handles without sending a stop message. +static void close_resource_service(INIT_RESOURCE_SERVICE* resourceService) { + if (!resourceService) return; + if (resourceService->AdminListener != SYS_HANDLE_INVALID) + (void)SysCloseHandle(resourceService->AdminListener); + if (resourceService->SessionControlListener != SYS_HANDLE_INVALID) + (void)SysCloseHandle(resourceService->SessionControlListener); + resourceService->AdminListener = SYS_HANDLE_INVALID; + resourceService->SessionControlListener = SYS_HANDLE_INVALID; + close_service_handles(&resourceService->Service); +} + +// Initializes the RSSRV-specific extension of one managed-service runtime. +static void initialize_resource_service(INIT_RESOURCE_SERVICE* resourceService, + const INIT_SERVICE_DESCRIPTOR* descriptor) { + memset(resourceService, 0, sizeof(*resourceService)); + initialize_service(&resourceService->Service, descriptor); + resourceService->AdminListener = SYS_HANDLE_INVALID; + resourceService->SessionControlListener = SYS_HANDLE_INVALID; +} + +// Starts one restricted RSSRV instance through the generic managed-service process path. +static KSTATUS start_resource_service(SYS_HANDLE systemVolume, + SYS_HANDLE namespaceVolume, + SYS_HANDLE volumeManager, + SYS_HANDLE inputManager, + SYS_HANDLE displayManager, + SYS_HANDLE resourceContext, + INIT_RESOURCE_SERVICE* resourceService) { + if (!resourceService || !resourceService->Service.Descriptor || + resourceService->Service.Descriptor->Type != InitServiceTypeResource || + resourceService->AdminListener != SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + NTL_PROCESS_STARTUP_CAPABILITY startup[4]; + memset(startup, 0, sizeof(startup)); + startup[0].Tag = SYS_STARTUP_CAP_SYSTEM_VOLUME_NAMESPACE; + startup[0].SourceHandle = namespaceVolume; + startup[0].DesiredRights = SYS_RIGHT_QUERY | SYS_RIGHT_TRANSFER | + SYS_VOLUME_RIGHT_OPEN | SYS_VOLUME_RIGHT_MODIFY; + startup[0].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; + startup[1].Tag = SYS_STARTUP_CAP_VOLUME_MANAGER; + startup[1].SourceHandle = volumeManager; + startup[1].DesiredRights = SYS_RIGHT_QUERY | + SYS_VOLUME_MANAGER_RIGHT_ENUMERATE | SYS_VOLUME_MANAGER_RIGHT_OPEN | + SYS_VOLUME_MANAGER_RIGHT_WAIT; + startup[1].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; + startup[2].Tag = SYS_STARTUP_CAP_INPUT_MANAGER; + startup[2].SourceHandle = inputManager; + startup[2].DesiredRights = SYS_RIGHT_QUERY | SYS_INPUT_MANAGER_RIGHT_READ; + startup[2].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; + startup[3].Tag = SYS_STARTUP_CAP_DISPLAY_MANAGER; + startup[3].SourceHandle = displayManager; + startup[3].DesiredRights = SYS_RIGHT_DERIVE | SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_DISPLAY_MANAGER_RIGHT_ENUMERATE | + SYS_DISPLAY_MANAGER_RIGHT_MODESET | SYS_DISPLAY_MANAGER_RIGHT_CONSOLE; + startup[3].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; + + KSTATUS status = start_service_process(systemVolume, resourceContext, startup, 4, + &resourceService->Service); + if (!KSUCCESS(status)) return status; + SYS_HANDLE adminListener = SYS_HANDLE_INVALID; - status = wait_resource_service_ready(controlEndpoint, &adminListener); + SYS_HANDLE sessionControlListener = SYS_HANDLE_INVALID; + status = wait_resource_service_ready(&resourceService->Service, &adminListener, &sessionControlListener); if (KSUCCESS(status)) status = verify_resource_service(adminListener, namespaceVolume); if (!KSUCCESS(status)) { if (adminListener != SYS_HANDLE_INVALID) (void)SysCloseHandle(adminListener); - (void)SysDisconnectPort(controlEndpoint); - (void)SysCloseHandle(controlEndpoint); - (void)SysKillProcess(process, 1); - (void)SysCloseHandle(process); + if (sessionControlListener != SYS_HANDLE_INVALID) (void)SysCloseHandle(sessionControlListener); + debug_early_service_exit(&resourceService->Service); + (void)SysKillProcess(resourceService->Service.Process, 1); + (void)SysWait(resourceService->Service.Process, SYS_WAIT_FLAG_NONE); + close_service_handles(&resourceService->Service); + resourceService->Service.State = InitServiceFailed; return status; } - serviceOut->Process = process; - serviceOut->ControlEndpoint = controlEndpoint; - serviceOut->AdminListener = adminListener; + resourceService->AdminListener = adminListener; + resourceService->SessionControlListener = sessionControlListener; + resourceService->Service.State = InitServiceReady; return NTLSTATUS_SUCCESS; } -// Requests graceful RSSRV shutdown, falling back to process termination if it stops responding. -static void stop_resource_service(INIT_RESOURCE_SERVICE* service) { +// Publishes DWM's desktop listener at the stable Session 0 desktop resource path. +static KSTATUS publish_dwm_desktop(SYS_HANDLE adminListener, SYS_HANDLE desktopListener) { + if (adminListener == SYS_HANDLE_INVALID || desktopListener == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + SYS_HANDLE admin = SYS_HANDLE_INVALID; + KSTATUS status = NtlResourceConnectListener(adminListener, &admin); + if (!KSUCCESS(status)) return status; + status = NtlResourceBind(admin, SYS_SV_LITERAL(NTL_DESKTOP_SERVICE_PATH), + NTL_RESOURCE_KIND_CAPABILITY, desktopListener, + SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT, + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT); + (void)NtlResourceClose(admin); + return status; +} + +// Removes DWM's public desktop endpoint while retaining Session 0 itself inside RSSRV. +static void unpublish_dwm_desktop(SYS_HANDLE adminListener) { + if (adminListener == SYS_HANDLE_INVALID) return; + SYS_HANDLE admin = SYS_HANDLE_INVALID; + if (!KSUCCESS(NtlResourceConnectListener(adminListener, &admin))) return; + (void)NtlResourceUnbind(admin, SYS_SV_LITERAL(NTL_DESKTOP_SERVICE_PATH)); + (void)NtlResourceClose(admin); +} + +// Starts one DWM instance with physical presentation, cursor policy, and attenuated Session 0 authority. +static KSTATUS start_dwm_service(SYS_HANDLE systemVolume, + SYS_HANDLE displayManager, + SYS_HANDLE securityContext, + const INIT_RESOURCE_SERVICE* resourceService, + INIT_SERVICE* service) { + if (!resourceService || !service || !service->Descriptor || service->Descriptor->Type != InitServiceTypeDwm || + displayManager == SYS_HANDLE_INVALID || securityContext == SYS_HANDLE_INVALID || + resourceService->AdminListener == SYS_HANDLE_INVALID || + resourceService->SessionControlListener == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + NTL_PROCESS_STARTUP_CAPABILITY startup[2]; + memset(startup, 0, sizeof(startup)); + startup[0].Tag = SYS_STARTUP_CAP_DISPLAY_MANAGER; + startup[0].SourceHandle = displayManager; + startup[0].DesiredRights = SYS_RIGHT_QUERY | SYS_DISPLAY_MANAGER_RIGHT_ENUMERATE | + SYS_DISPLAY_MANAGER_RIGHT_PRESENT | SYS_DISPLAY_MANAGER_RIGHT_CURSOR; + startup[0].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; + startup[1].Tag = SYS_STARTUP_CAP_SESSION_CONTROL; + startup[1].SourceHandle = resourceService->SessionControlListener; + startup[1].DesiredRights = SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT; + startup[1].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; + + KSTATUS status = start_service_process(systemVolume, securityContext, startup, 2u, service); + if (!KSUCCESS(status)) { + debug_print("[init] DWM spawn/delegation failed: "); + debug_status(status); + debug_print("\n"); + return status; + } + + SYS_HANDLE exports[1] = { SYS_HANDLE_INVALID }; + uint32_t exportCount = 0; + status = wait_service_ready(service, exports, 1u, &exportCount); + if (!KSUCCESS(status)) { + debug_print("[init] DWM READY wait failed: "); + debug_status(status); + debug_print("\n"); + } else if (exportCount != 1u || exports[0] == SYS_HANDLE_INVALID) { + debug_print("[init] DWM READY contract invalid\n"); + status = NTLSTATUS_FAILURE; + } else { + status = publish_dwm_desktop(resourceService->AdminListener, exports[0]); + if (!KSUCCESS(status)) { + debug_print("[init] DWM desktop publication failed: "); + debug_status(status); + debug_print("\n"); + } + } + if (exports[0] != SYS_HANDLE_INVALID) (void)SysCloseHandle(exports[0]); + + if (!KSUCCESS(status)) { + debug_early_service_exit(service); + (void)SysKillProcess(service->Process, 1); + (void)SysWait(service->Process, SYS_WAIT_FLAG_NONE); + close_service_handles(service); + service->State = InitServiceFailed; + return status; + } + + service->State = InitServiceReady; + return NTLSTATUS_SUCCESS; +} + +// Builds the canonical RSSRV publication path for one generic service identifier. +static KSTATUS build_generic_service_path(const INIT_SERVICE_DESCRIPTOR* descriptor, + char* pathOut, + size_t pathCapacity) { + static const char prefix[] = "/Services/"; + if (!descriptor || !pathOut || pathCapacity == 0) return NTLSTATUS_INVALID_PARAMETER; + size_t prefixLength = sizeof(prefix) - 1u; + size_t idLength = strlen(descriptor->Id); + if (idLength == 0 || prefixLength + idLength + 1u > pathCapacity) + return NTLSTATUS_NAME_TOO_LONG; + memcpy(pathOut, prefix, prefixLength); + memcpy(pathOut + prefixLength, descriptor->Id, idLength + 1u); + return NTLSTATUS_SUCCESS; +} + +// Publishes one generic service listener through RSSRV rather than the kernel service registry. +static KSTATUS publish_generic_service(SYS_HANDLE adminListener, + const INIT_SERVICE_DESCRIPTOR* descriptor, + SYS_HANDLE publicListener) { + if (adminListener == SYS_HANDLE_INVALID || !descriptor || publicListener == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + char path[64]; + KSTATUS status = build_generic_service_path(descriptor, path, sizeof(path)); + if (!KSUCCESS(status)) return status; + + SYS_HANDLE admin = SYS_HANDLE_INVALID; + status = NtlResourceConnectListener(adminListener, &admin); + if (!KSUCCESS(status)) return status; + status = NtlResourceBind(admin, SysSvFromCstr(path), NTL_RESOURCE_KIND_CAPABILITY, + publicListener, + SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT, + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT); + (void)NtlResourceClose(admin); + return status; +} + +// Removes one generic service publication before its process is stopped or forgotten. +static void unpublish_generic_service(SYS_HANDLE adminListener, + const INIT_SERVICE_DESCRIPTOR* descriptor) { + if (adminListener == SYS_HANDLE_INVALID || !descriptor) return; + char path[64]; + if (!KSUCCESS(build_generic_service_path(descriptor, path, sizeof(path)))) return; + SYS_HANDLE admin = SYS_HANDLE_INVALID; + if (!KSUCCESS(NtlResourceConnectListener(adminListener, &admin))) return; + (void)NtlResourceUnbind(admin, SysSvFromCstr(path)); + (void)NtlResourceClose(admin); +} + +// Starts one generic service, validates its READY listener export, and publishes it through RSSRV. +static KSTATUS start_generic_service(SYS_HANDLE systemVolume, + SYS_HANDLE securityContext, + const INIT_RESOURCE_SERVICE* resourceService, + INIT_SERVICE* service) { + if (!resourceService || !service || !service->Descriptor || + service->Descriptor->Type != InitServiceTypeGeneric || + resourceService->AdminListener == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = start_service_process(systemVolume, securityContext, (const NTL_PROCESS_STARTUP_CAPABILITY*)0, + 0u, service); + if (!KSUCCESS(status)) return status; + + SYS_HANDLE exports[1] = { SYS_HANDLE_INVALID }; + uint32_t exportCount = 0; + status = wait_service_ready(service, exports, 1u, &exportCount); + if (KSUCCESS(status) && (exportCount != 1u || exports[0] == SYS_HANDLE_INVALID)) + status = NTLSTATUS_FAILURE; + if (KSUCCESS(status)) + status = publish_generic_service(resourceService->AdminListener, service->Descriptor, exports[0]); + if (exports[0] != SYS_HANDLE_INVALID) (void)SysCloseHandle(exports[0]); + + if (!KSUCCESS(status)) { + debug_early_service_exit(service); + (void)SysKillProcess(service->Process, 1); + (void)SysWait(service->Process, SYS_WAIT_FLAG_NONE); + close_service_handles(service); + service->State = InitServiceFailed; + return status; + } + + service->State = InitServiceReady; + return NTLSTATUS_SUCCESS; +} + +// Requests graceful shutdown of one managed service, falling back to process termination. +static void stop_service(INIT_SERVICE* service) { if (!service || service->Process == SYS_HANDLE_INVALID) return; + service->State = InitServiceStopping; + NTL_SERVICE_CONTROL_MESSAGE message; memset(&message, 0, sizeof(message)); message.Size = sizeof(message); message.Version = NTL_SERVICE_CONTROL_VERSION; message.Operation = NTL_SERVICE_CONTROL_STOP; + message.ExportCount = 0; SYS_SEND_PORT_REQUEST send; memset(&send, 0, sizeof(send)); @@ -295,18 +792,57 @@ static void stop_resource_service(INIT_RESOURCE_SERVICE* service) { send.Flags = SYS_PORT_FLAG_NONE; send.Payload = &message; send.PayloadLength = sizeof(message); - send.TimeoutMilliseconds = INIT_SERVICE_STOP_TIMEOUT_MS; + send.TimeoutMilliseconds = service->Descriptor->StopTimeoutMilliseconds; (void)SysSendPort(&send); - KSTATUS status = SysWaitSingle(service->Process, SYS_WAIT_FLAG_NONE, INIT_SERVICE_STOP_TIMEOUT_MS); + KSTATUS status = SysWaitSingle(service->Process, SYS_WAIT_FLAG_NONE, + service->Descriptor->StopTimeoutMilliseconds); if (!KSUCCESS(status)) { (void)SysKillProcess(service->Process, 1); (void)SysWait(service->Process, SYS_WAIT_FLAG_NONE); } - close_resource_service(service); + close_service_handles(service); + service->State = InitServiceStopped; } -// Starts the configured native session child directly beneath the system-volume capability. +// Unpublishes and stops DWM so new desktop connections cannot race service teardown. +static void stop_dwm_service(INIT_RESOURCE_SERVICE* resourceService, INIT_SERVICE* service) { + if (!resourceService || !service || service->State != InitServiceReady) return; + unpublish_dwm_desktop(resourceService->AdminListener); + stop_service(service); +} + +// Unpublishes and stops one generic service before RSSRV namespace teardown. +static void stop_generic_service(const INIT_RESOURCE_SERVICE* resourceService, INIT_SERVICE* service) { + if (!resourceService || !service || service->State != InitServiceReady) return; + unpublish_generic_service(resourceService->AdminListener, service->Descriptor); + stop_service(service); +} + +// Stops ready generic services in reverse dependency order. +static void stop_generic_services(const INIT_RESOURCE_SERVICE* resourceService, + INIT_SERVICE* services) { + if (!resourceService || !services) return; + for (uint32_t order = gServiceConfig.StartupCount; order > 0; --order) { + uint16_t index = gServiceConfig.StartupOrder[order - 1u]; + if (gServiceConfig.Services[index].Type == InitServiceTypeGeneric) + stop_generic_service(resourceService, &services[index]); + } +} + +// Requests graceful RSSRV shutdown and releases its private administrative export. +static void stop_resource_service(INIT_RESOURCE_SERVICE* resourceService) { + if (!resourceService) return; + stop_service(&resourceService->Service); + if (resourceService->AdminListener != SYS_HANDLE_INVALID) + (void)SysCloseHandle(resourceService->AdminListener); + if (resourceService->SessionControlListener != SYS_HANDLE_INVALID) + (void)SysCloseHandle(resourceService->SessionControlListener); + resourceService->AdminListener = SYS_HANDLE_INVALID; + resourceService->SessionControlListener = SYS_HANDLE_INVALID; +} + +// Starts the configured native session process from the system-volume capability. static KSTATUS start_configured_child(SYS_HANDLE systemVolume, SYS_STRING_VIEW path, SYS_HANDLE* processOut) { NTL_CREATE_PROCESS_AT_REQUEST create; memset(&create, 0, sizeof(create)); @@ -323,17 +859,45 @@ static KSTATUS start_configured_child(SYS_HANDLE systemVolume, SYS_STRING_VIEW p return NtlCreateProcessAt(&create); } -// Initializes init state and supervises RSSRV plus the configured session process. +// Best-effort diagnostic name for init's current thread. +static void init_set_current_thread_name(const char* name) { + SYS_SET_THREAD_NAME_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.ThreadHandle = SYS_HANDLE_INVALID; + request.Name = SysSvFromCstr(name); + (void)SysSetThreadName(&request); +} + +// Initializes init state and supervises configured services plus the native session process. void _start(void) { KSTATUS status = NtlInitializeExceptionRuntime(); if (!KSUCCESS(status)) exit_now((int)status); + init_set_current_thread_name("init.main"); SYS_HANDLE systemVolume = SysGetStartupCapability(SYS_STARTUP_CAP_SYSTEM_VOLUME); SYS_HANDLE namespaceVolume = SysGetStartupCapability(SYS_STARTUP_CAP_SYSTEM_VOLUME_NAMESPACE); SYS_HANDLE volumeManager = SysGetStartupCapability(SYS_STARTUP_CAP_VOLUME_MANAGER); + SYS_HANDLE inputManager = SysGetStartupCapability(SYS_STARTUP_CAP_INPUT_MANAGER); + SYS_HANDLE displayManager = SysGetStartupCapability(SYS_STARTUP_CAP_DISPLAY_MANAGER); if (systemVolume == SYS_HANDLE_INVALID || namespaceVolume == SYS_HANDLE_INVALID || - volumeManager == SYS_HANDLE_INVALID) { - debug_print("[init] FATAL: bootstrap storage capability missing\n"); + volumeManager == SYS_HANDLE_INVALID || inputManager == SYS_HANDLE_INVALID || + displayManager == SYS_HANDLE_INVALID) { + debug_print("[init] FATAL: bootstrap resource-plane capability missing\n"); + exit_now(1); + } + + uint32_t configErrorLine = 0; + status = InitLoadServiceConfig(systemVolume, &gServiceConfig, &configErrorLine); + if (!KSUCCESS(status)) { + debug_print("[init] FATAL: invalid or unreadable services.ini"); + if (configErrorLine != 0u) { + debug_print(" at line "); + debug_u32(configErrorLine); + } + debug_print(": "); + debug_status(status); + debug_print("\n"); exit_now(1); } @@ -346,76 +910,239 @@ void _start(void) { exit_now(1); } + SYS_HANDLE unprivilegedServiceContext = SYS_HANDLE_INVALID; + status = derive_unprivileged_service_context(&unprivilegedServiceContext); + if (!KSUCCESS(status)) { + debug_print("[init] FATAL: cannot derive unprivileged service context: "); + debug_status(status); + debug_print("\n"); + (void)SysCloseHandle(resourceContext); + exit_now(1); + } + SYS_STRING_VIEW childPath = SysGetPeb()->CommandLine; if (!childPath.Data || childPath.Length == 0) { debug_print("[init] FATAL: no configured session process\n"); + if (unprivilegedServiceContext != SYS_HANDLE_INVALID) (void)SysCloseHandle(unprivilegedServiceContext); + (void)SysCloseHandle(resourceContext); + exit_now(1); + } + + INIT_RESOURCE_SERVICE resourceService; + initialize_resource_service(&resourceService, + &gServiceConfig.Services[gServiceConfig.ResourceServiceIndex]); + + INIT_SERVICE dwmService; + memset(&dwmService, 0, sizeof(dwmService)); + if (gServiceConfig.DwmServiceIndex != UINT16_MAX) + initialize_service(&dwmService, &gServiceConfig.Services[gServiceConfig.DwmServiceIndex]); + else { + dwmService.Process = SYS_HANDLE_INVALID; + dwmService.ControlEndpoint = SYS_HANDLE_INVALID; + dwmService.State = InitServiceStopped; + } + + INIT_SERVICE genericServices[INIT_SERVICE_MAX_COUNT]; + memset(genericServices, 0, sizeof(genericServices)); + uint32_t automaticGenericCount = 0; + for (uint32_t i = 0; i < gServiceConfig.ServiceCount; ++i) { + if (gServiceConfig.Services[i].Type != InitServiceTypeGeneric) continue; + initialize_service(&genericServices[i], &gServiceConfig.Services[i]); + if (gServiceConfig.Services[i].StartPolicy == InitServiceStartAuto) ++automaticGenericCount; + } + if (automaticGenericCount + 3u > SYS_WAIT_MANY_MAX_HANDLES) { + debug_print("[init] FATAL: too many automatic generic services for supervision wait set\n"); + (void)SysCloseHandle(unprivilegedServiceContext); + (void)SysCloseHandle(resourceContext); + exit_now(1); + } + + for (uint32_t order = 0; order < gServiceConfig.StartupCount; ++order) { + uint16_t index = gServiceConfig.StartupOrder[order]; + const INIT_SERVICE_DESCRIPTOR* descriptor = &gServiceConfig.Services[index]; + if (descriptor->Type == InitServiceTypeResource) { + status = start_resource_service(systemVolume, namespaceVolume, volumeManager, inputManager, + displayManager, resourceContext, &resourceService); + if (!KSUCCESS(status)) { + debug_print("[init] FATAL: RSSRV startup failed: "); + debug_status(status); + debug_print("\n"); + if (unprivilegedServiceContext != SYS_HANDLE_INVALID) (void)SysCloseHandle(unprivilegedServiceContext); + (void)SysCloseHandle(resourceContext); + exit_now(1); + } + debug_print("[init] RSSRV ready\n"); + } else if (descriptor->Type == InitServiceTypeDwm) { + status = start_dwm_service(systemVolume, displayManager, unprivilegedServiceContext, &resourceService, &dwmService); + if (!KSUCCESS(status)) { + debug_print("[init] noncritical DWM startup failed: "); + debug_status(status); + debug_print("\n"); + } else { + debug_print("[init] DWM ready\n"); + } + } else if (descriptor->Type == InitServiceTypeGeneric) { + status = start_generic_service(systemVolume, unprivilegedServiceContext, &resourceService, + &genericServices[index]); + if (!KSUCCESS(status)) { + debug_print("[init] noncritical generic service startup failed: "); + debug_print(descriptor->Id); + debug_print(" status="); + debug_status(status); + debug_print("\n"); + } else { + debug_print("[init] generic service ready: "); + debug_print(descriptor->Id); + debug_print("\n"); + } + } + } + + if (resourceService.Service.State != InitServiceReady) { + debug_print("[init] FATAL: configured resource service was not started\n"); + if (dwmService.State == InitServiceReady) stop_dwm_service(&resourceService, &dwmService); + stop_generic_services(&resourceService, genericServices); + if (unprivilegedServiceContext != SYS_HANDLE_INVALID) (void)SysCloseHandle(unprivilegedServiceContext); + (void)SysCloseHandle(resourceContext); exit_now(1); } for (;;) { - INIT_RESOURCE_SERVICE resourceService; - status = start_resource_service(systemVolume, namespaceVolume, volumeManager, resourceContext, &resourceService); + SYS_HANDLE child = SYS_HANDLE_INVALID; + status = start_configured_child(systemVolume, childPath, &child); if (!KSUCCESS(status)) { - debug_print("[init] FATAL: RSSRV startup failed: "); + debug_print("[init] FATAL: failed to start configured child: "); debug_status(status); debug_print("\n"); + if (dwmService.State == InitServiceReady) stop_dwm_service(&resourceService, &dwmService); + stop_generic_services(&resourceService, genericServices); + stop_resource_service(&resourceService); + if (unprivilegedServiceContext != SYS_HANDLE_INVALID) (void)SysCloseHandle(unprivilegedServiceContext); + (void)SysCloseHandle(resourceContext); exit_now(1); } - debug_print("[init] RSSRV ready\n"); + debug_print("[init] session process started\n"); + bool restartChild = false; for (;;) { - SYS_HANDLE child = SYS_HANDLE_INVALID; - status = start_configured_child(systemVolume, childPath, &child); - if (!KSUCCESS(status)) { - debug_print("[init] FATAL: failed to start configured child: "); - debug_status(status); - debug_print("\n"); - stop_resource_service(&resourceService); - exit_now(1); + SYS_HANDLE waits[SYS_WAIT_MANY_MAX_HANDLES]; + uint32_t kinds[SYS_WAIT_MANY_MAX_HANDLES]; + uint32_t waitCount = 0; + waits[waitCount] = resourceService.Service.Process; + kinds[waitCount++] = 0u; + if (dwmService.State == InitServiceReady && dwmService.Process != SYS_HANDLE_INVALID) { + waits[waitCount] = dwmService.Process; + kinds[waitCount++] = 1u; + } + waits[waitCount] = child; + kinds[waitCount++] = 2u; + for (uint32_t i = 0; i < gServiceConfig.ServiceCount; ++i) { + if (gServiceConfig.Services[i].Type != InitServiceTypeGeneric || + genericServices[i].State != InitServiceReady || + genericServices[i].Process == SYS_HANDLE_INVALID) + continue; + waits[waitCount] = genericServices[i].Process; + kinds[waitCount++] = 3u + i; } - debug_print("[init] session process started\n"); - SYS_HANDLE waits[2] = { resourceService.Process, child }; uint32_t signaled = 0; SYS_WAIT_MANY_REQUEST wait; memset(&wait, 0, sizeof(wait)); wait.Size = sizeof(wait); wait.Flags = SYS_WAIT_FLAG_NONE; wait.Handles = waits; - wait.Count = 2; + wait.Count = waitCount; wait.SignaledIndexOut = &signaled; wait.TimeoutMilliseconds = SYS_WAIT_TIMEOUT_INFINITE; status = SysWaitMany(&wait); if (!KSUCCESS(status)) { (void)SysKillProcess(child, 1); + (void)SysWait(child, SYS_WAIT_FLAG_NONE); (void)SysCloseHandle(child); + if (dwmService.State == InitServiceReady) stop_dwm_service(&resourceService, &dwmService); + stop_generic_services(&resourceService, genericServices); stop_resource_service(&resourceService); + if (unprivilegedServiceContext != SYS_HANDLE_INVALID) (void)SysCloseHandle(unprivilegedServiceContext); + (void)SysCloseHandle(resourceContext); debug_print("[init] FATAL: service wait failed\n"); exit_now(1); } - if (signaled == 0) { + if (kinds[signaled] == 0u) { int32_t serviceExit = -1; - (void)SysGetProcessExitCode(resourceService.Process, &serviceExit); - debug_print("[init] RSSRV exited unexpectedly; restarting service plane\n"); + (void)SysGetProcessExitCode(resourceService.Service.Process, &serviceExit); + resourceService.Service.State = InitServiceFailed; + debug_print("[init] FATAL: critical RSSRV exited unexpectedly: "); + debug_i32(serviceExit); + debug_print("\n"); (void)SysKillProcess(child, 1); (void)SysWait(child, SYS_WAIT_FLAG_NONE); (void)SysCloseHandle(child); + if (dwmService.State == InitServiceReady) stop_dwm_service(&resourceService, &dwmService); + for (uint32_t i = 0; i < gServiceConfig.ServiceCount; ++i) { + if (gServiceConfig.Services[i].Type == InitServiceTypeGeneric && + genericServices[i].Process != SYS_HANDLE_INVALID) { + (void)SysKillProcess(genericServices[i].Process, 1); + (void)SysWait(genericServices[i].Process, SYS_WAIT_FLAG_NONE); + close_service_handles(&genericServices[i]); + genericServices[i].State = InitServiceFailed; + } + } close_resource_service(&resourceService); - break; + if (unprivilegedServiceContext != SYS_HANDLE_INVALID) (void)SysCloseHandle(unprivilegedServiceContext); + (void)SysCloseHandle(resourceContext); + exit_now(1); + } + + if (kinds[signaled] == 1u) { + int32_t serviceExit = -1; + (void)SysGetProcessExitCode(dwmService.Process, &serviceExit); + debug_print("[init] noncritical DWM exited unexpectedly: "); + debug_i32(serviceExit); + debug_print("\n"); + unpublish_dwm_desktop(resourceService.AdminListener); + close_service_handles(&dwmService); + dwmService.State = InitServiceFailed; + continue; + } + + if (kinds[signaled] >= 3u) { + uint32_t index = kinds[signaled] - 3u; + INIT_SERVICE* generic = &genericServices[index]; + int32_t serviceExit = -1; + (void)SysGetProcessExitCode(generic->Process, &serviceExit); + debug_print("[init] noncritical generic service exited unexpectedly: "); + debug_print(generic->Descriptor->Id); + debug_print(" code="); + debug_i32(serviceExit); + debug_print("\n"); + unpublish_generic_service(resourceService.AdminListener, generic->Descriptor); + close_service_handles(generic); + generic->State = InitServiceFailed; + continue; } int32_t exitCode = -1; status = SysGetProcessExitCode(child, &exitCode); (void)SysCloseHandle(child); + child = SYS_HANDLE_INVALID; if (!KSUCCESS(status)) exitCode = 1; if (exitCode == 0) { + if (dwmService.State == InitServiceReady) stop_dwm_service(&resourceService, &dwmService); + stop_generic_services(&resourceService, genericServices); stop_resource_service(&resourceService); debug_print("[init] done\n"); + if (unprivilegedServiceContext != SYS_HANDLE_INVALID) (void)SysCloseHandle(unprivilegedServiceContext); (void)SysCloseHandle(resourceContext); exit_now(0); } debug_print("[init] session process crashed; restarting\n"); + restartChild = true; + break; } + + if (!restartChild) break; } + + exit_now(1); } diff --git a/code/user/system/init/service_config.c b/code/user/system/init/service_config.c new file mode 100644 index 0000000..03eaeda --- /dev/null +++ b/code/user/system/init/service_config.c @@ -0,0 +1,476 @@ +// Init service-configuration parser. Malformed or ambiguous service policy is rejected at boot. + +#include "service_config.h" + +#include +#include +#include + +#define INIT_SERVICE_KEY_TYPE 0x00000001u +#define INIT_SERVICE_KEY_IMAGE 0x00000002u +#define INIT_SERVICE_KEY_START 0x00000004u +#define INIT_SERVICE_KEY_CRITICAL 0x00000008u +#define INIT_SERVICE_KEY_RESTART 0x00000010u +#define INIT_SERVICE_KEY_READY_TIMEOUT 0x00000020u +#define INIT_SERVICE_KEY_STOP_TIMEOUT 0x00000040u +#define INIT_SERVICE_KEY_REQUIRES 0x00000080u +#define INIT_SERVICE_REQUIRED_KEYS 0x000000FFu + +typedef struct INIT_PARSED_SERVICE { + INIT_SERVICE_DESCRIPTOR Descriptor; + char DependencyIds[INIT_SERVICE_DEPENDENCY_MAX][INIT_SERVICE_ID_MAX + 1u]; + uint32_t DependencyCount; + uint32_t PresentKeys; + uint32_t SectionLine; +} INIT_PARSED_SERVICE; + +static char gServicesIniBuffer[INIT_SERVICES_MAX_FILE_BYTES + 1u]; +static INIT_PARSED_SERVICE gParsedServices[INIT_SERVICE_MAX_COUNT]; + +// Returns whether one byte is horizontal configuration whitespace. +static bool is_space(char c) { + return c == ' ' || c == '\t' || c == '\r'; +} + +// Trims leading horizontal whitespace in place by returning the first content byte. +static char* trim_left(char* text) { + while (*text && is_space(*text)) ++text; + return text; +} + +// Trims trailing horizontal whitespace from one mutable string. +static void trim_right(char* text) { + char* end = text + strlen(text); + while (end > text && is_space(end[-1])) *--end = '\0'; +} + +// Returns whether one service identifier byte belongs to the deliberately small grammar. +static bool is_service_id_char(char c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || c == '_' || c == '-' || c == '.'; +} + +// Validates a case-sensitive service identifier used by sections and dependencies. +static bool validate_service_id(const char* id) { + size_t length = id ? strlen(id) : 0; + if (length == 0 || length > INIT_SERVICE_ID_MAX) return false; + for (size_t i = 0; i < length; ++i) + if (!is_service_id_char(id[i])) return false; + return true; +} + +// Validates the capability-relative native image path accepted by the process loader. +static bool validate_image_path(const char* path) { + size_t length = path ? strlen(path) : 0; + if (length == 0 || length >= SYS_PATH_MAX || path[0] == '/' || path[length - 1u] == '/') + return false; + + const char* component = path; + for (size_t i = 0; i <= length; ++i) { + char c = path[i]; + if (c == '\\' || c == ':') return false; + if (c != '/' && c != '\0') continue; + + size_t componentLength = (size_t)(&path[i] - component); + if (componentLength == 0 || + (componentLength == 1 && component[0] == '.') || + (componentLength == 2 && component[0] == '.' && component[1] == '.')) + return false; + component = &path[i + 1u]; + } + return true; +} + +// Copies one bounded configuration string into fixed init-owned storage. +static bool copy_bounded(char* destination, size_t capacity, const char* source) { + size_t length = source ? strlen(source) : 0; + if (!destination || capacity == 0 || length >= capacity) return false; + memcpy(destination, source, length + 1u); + return true; +} + +// Parses one strict lower-case boolean. +static bool parse_bool(const char* text, bool* valueOut) { + if (!text || !valueOut) return false; + if (strcmp(text, "true") == 0) { + *valueOut = true; + return true; + } + if (strcmp(text, "false") == 0) { + *valueOut = false; + return true; + } + return false; +} + +// Parses one bounded unsigned decimal integer without signs or suffixes. +static bool parse_u32(const char* text, uint32_t minimum, uint32_t maximum, uint32_t* valueOut) { + if (!text || !*text || !valueOut) return false; + uint64_t value = 0; + for (const char* p = text; *p; ++p) { + if (*p < '0' || *p > '9') return false; + value = value * 10u + (uint32_t)(*p - '0'); + if (value > maximum) return false; + } + if (value < minimum) return false; + *valueOut = (uint32_t)value; + return true; +} + +// Looks up a parsed service by its case-sensitive identifier. +static int32_t find_parsed_service(const INIT_PARSED_SERVICE* services, + uint32_t serviceCount, + const char* id) { + for (uint32_t i = 0; i < serviceCount; ++i) + if (strcmp(services[i].Descriptor.Id, id) == 0) return (int32_t)i; + return -1; +} + +// Parses the comma-separated hard dependency list for one service. +static bool parse_requires(char* value, INIT_PARSED_SERVICE* service) { + service->DependencyCount = 0; + if (!*value) return true; + + char* cursor = value; + for (;;) { + char* comma = strchr(cursor, ','); + if (comma) *comma = '\0'; + char* id = trim_left(cursor); + trim_right(id); + if (!validate_service_id(id) || service->DependencyCount >= INIT_SERVICE_DEPENDENCY_MAX) + return false; + + for (uint32_t i = 0; i < service->DependencyCount; ++i) + if (strcmp(service->DependencyIds[i], id) == 0) return false; + if (!copy_bounded(service->DependencyIds[service->DependencyCount], + sizeof(service->DependencyIds[0]), id)) + return false; + ++service->DependencyCount; + + if (!comma) break; + cursor = comma + 1; + if (!*cursor) return false; + } + return true; +} + +// Applies one known key to the current service and rejects duplicates or invalid values. +static bool parse_service_key(INIT_PARSED_SERVICE* service, char* key, char* value) { + uint32_t bit = 0; + + if (strcmp(key, "type") == 0) { + bit = INIT_SERVICE_KEY_TYPE; + if (strcmp(value, "resource") == 0) service->Descriptor.Type = InitServiceTypeResource; + else if (strcmp(value, "dwm") == 0) service->Descriptor.Type = InitServiceTypeDwm; + else if (strcmp(value, "generic") == 0) service->Descriptor.Type = InitServiceTypeGeneric; + else return false; + } else if (strcmp(key, "image") == 0) { + bit = INIT_SERVICE_KEY_IMAGE; + if (!validate_image_path(value) || + !copy_bounded(service->Descriptor.ImagePath, sizeof(service->Descriptor.ImagePath), value)) + return false; + } else if (strcmp(key, "start") == 0) { + bit = INIT_SERVICE_KEY_START; + if (strcmp(value, "auto") == 0) service->Descriptor.StartPolicy = InitServiceStartAuto; + else if (strcmp(value, "disabled") == 0) service->Descriptor.StartPolicy = InitServiceStartDisabled; + else return false; + } else if (strcmp(key, "critical") == 0) { + bit = INIT_SERVICE_KEY_CRITICAL; + if (!parse_bool(value, &service->Descriptor.Critical)) return false; + } else if (strcmp(key, "restart") == 0) { + bit = INIT_SERVICE_KEY_RESTART; + if (strcmp(value, "never") == 0) service->Descriptor.RestartPolicy = InitServiceRestartNever; + else if (strcmp(value, "on-failure") == 0) service->Descriptor.RestartPolicy = InitServiceRestartOnFailure; + else return false; + } else if (strcmp(key, "ready_timeout_ms") == 0) { + bit = INIT_SERVICE_KEY_READY_TIMEOUT; + if (!parse_u32(value, 1u, 600000u, &service->Descriptor.ReadyTimeoutMilliseconds)) return false; + } else if (strcmp(key, "stop_timeout_ms") == 0) { + bit = INIT_SERVICE_KEY_STOP_TIMEOUT; + if (!parse_u32(value, 1u, 600000u, &service->Descriptor.StopTimeoutMilliseconds)) return false; + } else if (strcmp(key, "requires") == 0) { + bit = INIT_SERVICE_KEY_REQUIRES; + if (!parse_requires(value, service)) return false; + } else { + return false; + } + + if (service->PresentKeys & bit) return false; + service->PresentKeys |= bit; + return true; +} + +// Parses the strict services.ini grammar into temporary fixed storage. +static KSTATUS parse_services_ini(char* buffer, uint32_t* serviceCountOut, uint32_t* errorLineOut) { + uint32_t serviceCount = 0; + INIT_PARSED_SERVICE* current = NULL; + uint32_t lineNumber = 0; + char* line = buffer; + + while (*line) { + ++lineNumber; + char* end = strchr(line, '\n'); + if (!end) end = line + strlen(line); + char saved = *end; + *end = '\0'; + + char* text = trim_left(line); + trim_right(text); + bool valid = true; + + if (!*text || *text == ';' || *text == '#') { + // Blank and whole-line comments are the only ignored line forms. + } else if (*text == '[') { + size_t length = strlen(text); + if (length < 11u || text[length - 1u] != ']' || strchr(text + 1, ']') != &text[length - 1u]) { + valid = false; + } else { + text[length - 1u] = '\0'; + const char prefix[] = "service."; + if (strncmp(text + 1, prefix, sizeof(prefix) - 1u) != 0) { + valid = false; + } else { + char* id = text + 1u + sizeof(prefix) - 1u; + if (!validate_service_id(id) || serviceCount >= INIT_SERVICE_MAX_COUNT || + find_parsed_service(gParsedServices, serviceCount, id) >= 0) { + valid = false; + } else { + current = &gParsedServices[serviceCount++]; + memset(current, 0, sizeof(*current)); + current->SectionLine = lineNumber; + if (!copy_bounded(current->Descriptor.Id, sizeof(current->Descriptor.Id), id)) + valid = false; + } + } + } + } else { + if (!current) { + valid = false; + } else { + char* equals = strchr(text, '='); + if (!equals || strchr(equals + 1, '=')) { + valid = false; + } else { + *equals = '\0'; + char* key = text; + char* value = trim_left(equals + 1); + trim_right(key); + trim_right(value); + if (!*key || !parse_service_key(current, key, value)) valid = false; + } + } + } + + *end = saved; + if (!valid) { + if (errorLineOut) *errorLineOut = lineNumber; + return NTLSTATUS_INVALID_PARAMETER; + } + line = saved ? end + 1u : end; + } + + if (serviceCount == 0) return NTLSTATUS_INVALID_PARAMETER; + *serviceCountOut = serviceCount; + return NTLSTATUS_SUCCESS; +} + +// Resolves dependency identifiers, validates invariants, and computes dependency-first order. +static KSTATUS validate_services(uint32_t serviceCount, + INIT_SERVICE_CONFIG* configOut, + uint32_t* errorLineOut) { + uint32_t resourceCount = 0; + uint16_t resourceIndex = 0; + uint32_t dwmCount = 0; + uint16_t dwmIndex = UINT16_MAX; + + memset(configOut, 0, sizeof(*configOut)); + configOut->ResourceServiceIndex = UINT16_MAX; + configOut->DwmServiceIndex = UINT16_MAX; + + for (uint32_t i = 0; i < serviceCount; ++i) { + INIT_PARSED_SERVICE* parsed = &gParsedServices[i]; + if (parsed->PresentKeys != INIT_SERVICE_REQUIRED_KEYS) { + if (errorLineOut) *errorLineOut = parsed->SectionLine; + return NTLSTATUS_INVALID_PARAMETER; + } + + INIT_SERVICE_DESCRIPTOR* descriptor = &configOut->Services[i]; + *descriptor = parsed->Descriptor; + descriptor->DependencyCount = parsed->DependencyCount; + for (uint32_t d = 0; d < parsed->DependencyCount; ++d) { + int32_t dependency = find_parsed_service(gParsedServices, serviceCount, parsed->DependencyIds[d]); + if (dependency < 0 || (uint32_t)dependency == i) { + if (errorLineOut) *errorLineOut = parsed->SectionLine; + return NTLSTATUS_INVALID_PARAMETER; + } + descriptor->Dependencies[d] = (uint16_t)dependency; + } + + if (descriptor->Type == InitServiceTypeResource) { + ++resourceCount; + resourceIndex = (uint16_t)i; + } else if (descriptor->Type == InitServiceTypeDwm) { + ++dwmCount; + dwmIndex = (uint16_t)i; + } + } + + if (resourceCount != 1u || dwmCount > 1u) return NTLSTATUS_INVALID_PARAMETER; + + INIT_SERVICE_DESCRIPTOR* resource = &configOut->Services[resourceIndex]; + if (resource->StartPolicy != InitServiceStartAuto || !resource->Critical || + resource->RestartPolicy != InitServiceRestartNever || resource->DependencyCount != 0u) { + if (errorLineOut) *errorLineOut = gParsedServices[resourceIndex].SectionLine; + return NTLSTATUS_INVALID_PARAMETER; + } + + if (dwmCount == 1u) { + INIT_SERVICE_DESCRIPTOR* dwm = &configOut->Services[dwmIndex]; + bool requiresResource = false; + for (uint32_t d = 0; d < dwm->DependencyCount; ++d) + if (dwm->Dependencies[d] == resourceIndex) requiresResource = true; + if (dwm->StartPolicy == InitServiceStartAuto && + (dwm->Critical || dwm->RestartPolicy != InitServiceRestartNever || !requiresResource)) { + if (errorLineOut) *errorLineOut = gParsedServices[dwmIndex].SectionLine; + return NTLSTATUS_INVALID_PARAMETER; + } + } + + // The generic adapter publishes READY listeners through RSSRV, so automatic generic services require it. + for (uint32_t i = 0; i < serviceCount; ++i) { + INIT_SERVICE_DESCRIPTOR* generic = &configOut->Services[i]; + if (generic->Type != InitServiceTypeGeneric || generic->StartPolicy != InitServiceStartAuto) continue; + bool requiresResource = false; + for (uint32_t d = 0; d < generic->DependencyCount; ++d) + if (generic->Dependencies[d] == resourceIndex) requiresResource = true; + if (generic->Critical || generic->RestartPolicy != InitServiceRestartNever || !requiresResource) { + if (errorLineOut) *errorLineOut = gParsedServices[i].SectionLine; + return NTLSTATUS_INVALID_PARAMETER; + } + } + + for (uint32_t i = 0; i < serviceCount; ++i) { + INIT_SERVICE_DESCRIPTOR* descriptor = &configOut->Services[i]; + if (descriptor->StartPolicy != InitServiceStartAuto) continue; + for (uint32_t d = 0; d < descriptor->DependencyCount; ++d) { + INIT_SERVICE_DESCRIPTOR* dependency = &configOut->Services[descriptor->Dependencies[d]]; + if (dependency->StartPolicy == InitServiceStartDisabled) { + if (errorLineOut) *errorLineOut = gParsedServices[i].SectionLine; + return NTLSTATUS_INVALID_PARAMETER; + } + } + } + + uint32_t indegree[INIT_SERVICE_MAX_COUNT] = {0}; + bool emitted[INIT_SERVICE_MAX_COUNT] = {0}; + for (uint32_t i = 0; i < serviceCount; ++i) + indegree[i] = configOut->Services[i].DependencyCount; + + uint32_t emittedCount = 0; + while (emittedCount < serviceCount) { + int32_t next = -1; + for (uint32_t i = 0; i < serviceCount; ++i) { + if (!emitted[i] && indegree[i] == 0u) { + next = (int32_t)i; + break; + } + } + if (next < 0) { + if (errorLineOut) { + for (uint32_t i = 0; i < serviceCount; ++i) { + if (!emitted[i]) { + *errorLineOut = gParsedServices[i].SectionLine; + break; + } + } + } + return NTLSTATUS_DEADLOCK; + } + + emitted[next] = true; + if (configOut->Services[next].StartPolicy == InitServiceStartAuto) + configOut->StartupOrder[configOut->StartupCount++] = (uint16_t)next; + ++emittedCount; + + for (uint32_t i = 0; i < serviceCount; ++i) { + if (emitted[i]) continue; + INIT_SERVICE_DESCRIPTOR* descriptor = &configOut->Services[i]; + for (uint32_t d = 0; d < descriptor->DependencyCount; ++d) { + if (descriptor->Dependencies[d] == (uint16_t)next) { + --indegree[i]; + break; + } + } + } + } + + configOut->ServiceCount = serviceCount; + configOut->ResourceServiceIndex = resourceIndex; + configOut->DwmServiceIndex = dwmIndex; + return NTLSTATUS_SUCCESS; +} + +// Reads the mandatory services.ini file from the bootstrap system volume. +static KSTATUS read_services_ini(SYS_HANDLE systemVolume, size_t* sizeOut) { + SYS_HANDLE file = SYS_HANDLE_INVALID; + SYS_OPEN_FILE_AT_REQUEST open = SysMakeOpenFileAtRequest(systemVolume, + SYS_SV_LITERAL(INIT_SERVICES_PATH), + SYS_FILE_ACCESS_READ | SYS_FILE_ACCESS_QUERY, + SYS_FILE_OPEN_EXISTING, + SYS_FILE_FLAG_NONE, + &file); + KSTATUS status = SysOpenFileAt(&open); + if (!KSUCCESS(status)) return status; + + uint64_t fileSize = 0; + status = SysGetFileSize(file, &fileSize); + if (!KSUCCESS(status)) { + (void)SysCloseHandle(file); + return status; + } + if (fileSize == 0u || fileSize > INIT_SERVICES_MAX_FILE_BYTES) { + (void)SysCloseHandle(file); + return fileSize > INIT_SERVICES_MAX_FILE_BYTES ? NTLSTATUS_LIMIT_REACHED : NTLSTATUS_INVALID_PARAMETER; + } + + size_t total = 0; + while (total < (size_t)fileSize) { + size_t transferred = 0; + SYS_FILE_IO_REQUEST read = SysMakeFileIoRequest(file, + gServicesIniBuffer + total, + (size_t)fileSize - total, + &transferred); + status = SysReadFile(&read); + if (!KSUCCESS(status) || transferred == 0u) { + (void)SysCloseHandle(file); + return KSUCCESS(status) ? NTLSTATUS_FAILURE : status; + } + total += transferred; + } + (void)SysCloseHandle(file); + + for (size_t i = 0; i < total; ++i) + if (gServicesIniBuffer[i] == '\0') return NTLSTATUS_INVALID_PARAMETER; + gServicesIniBuffer[total] = '\0'; + *sizeOut = total; + return NTLSTATUS_SUCCESS; +} + +// Loads, strictly parses, and validates services.ini beneath the bootstrap system volume. +KSTATUS InitLoadServiceConfig(SYS_HANDLE systemVolume, + INIT_SERVICE_CONFIG* configOut, + uint32_t* errorLineOut) { + if (systemVolume == SYS_HANDLE_INVALID || !configOut) return NTLSTATUS_INVALID_PARAMETER; + if (errorLineOut) *errorLineOut = 0; + + memset(gParsedServices, 0, sizeof(gParsedServices)); + size_t size = 0; + KSTATUS status = read_services_ini(systemVolume, &size); + if (!KSUCCESS(status)) return status; + (void)size; + + uint32_t serviceCount = 0; + status = parse_services_ini(gServicesIniBuffer, &serviceCount, errorLineOut); + if (!KSUCCESS(status)) return status; + return validate_services(serviceCount, configOut, errorLineOut); +} diff --git a/code/user/system/init/service_config.h b/code/user/system/init/service_config.h new file mode 100644 index 0000000..5fd86dc --- /dev/null +++ b/code/user/system/init/service_config.h @@ -0,0 +1,56 @@ +// Init service-configuration declarations. services.ini is strict policy owned by init. + +#pragma once + +#include +#include +#include + +#define INIT_SERVICES_PATH "system/services.ini" +#define INIT_SERVICES_MAX_FILE_BYTES (64u * 1024u) +#define INIT_SERVICE_MAX_COUNT 32u +#define INIT_SERVICE_ID_MAX 31u +#define INIT_SERVICE_DEPENDENCY_MAX 8u + +typedef enum INIT_SERVICE_TYPE { + InitServiceTypeResource = 0, + InitServiceTypeDwm, + InitServiceTypeGeneric, +} INIT_SERVICE_TYPE; + +typedef enum INIT_SERVICE_START_POLICY { + InitServiceStartAuto = 0, + InitServiceStartDisabled, +} INIT_SERVICE_START_POLICY; + +typedef enum INIT_SERVICE_RESTART_POLICY { + InitServiceRestartNever = 0, + InitServiceRestartOnFailure, +} INIT_SERVICE_RESTART_POLICY; + +typedef struct INIT_SERVICE_DESCRIPTOR { + char Id[INIT_SERVICE_ID_MAX + 1u]; + char ImagePath[SYS_PATH_MAX]; + INIT_SERVICE_TYPE Type; + INIT_SERVICE_START_POLICY StartPolicy; + bool Critical; + INIT_SERVICE_RESTART_POLICY RestartPolicy; + uint32_t ReadyTimeoutMilliseconds; + uint32_t StopTimeoutMilliseconds; + uint32_t DependencyCount; + uint16_t Dependencies[INIT_SERVICE_DEPENDENCY_MAX]; +} INIT_SERVICE_DESCRIPTOR; + +typedef struct INIT_SERVICE_CONFIG { + INIT_SERVICE_DESCRIPTOR Services[INIT_SERVICE_MAX_COUNT]; + uint16_t StartupOrder[INIT_SERVICE_MAX_COUNT]; + uint32_t ServiceCount; + uint32_t StartupCount; + uint16_t ResourceServiceIndex; + uint16_t DwmServiceIndex; +} INIT_SERVICE_CONFIG; + +// Loads, strictly parses, and validates services.ini beneath the bootstrap system volume. +KSTATUS InitLoadServiceConfig(SYS_HANDLE systemVolume, + INIT_SERVICE_CONFIG* configOut, + uint32_t* errorLineOut); diff --git a/code/user/system/posixsrv/posixsrv.c b/code/user/system/posixsrv/posixsrv.c new file mode 100644 index 0000000..446af75 --- /dev/null +++ b/code/user/system/posixsrv/posixsrv.c @@ -0,0 +1,2153 @@ +// POSIX personality-server bootstrap, stable process identity, shared attributes, and transaction plumbing. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define POSIXSRV_FIRST_PID 0x00010000u +#define POSIXSRV_LAST_PID 0x7FFFFFFFu + +typedef struct POSIXSRV_TRANSACTION { + uint32_t Id; + uint32_t Kind; +} POSIXSRV_TRANSACTION; + +typedef struct POSIXSRV_OPEN_DESCRIPTION POSIXSRV_OPEN_DESCRIPTION; +typedef struct POSIXSRV_PROCESS_RECORD POSIXSRV_PROCESS_RECORD; +typedef struct POSIXSRV_FD_INHERITANCE POSIXSRV_FD_INHERITANCE; + +typedef struct POSIXSRV_PIPE_BACKING { + SYS_PIPE_SHARED_STATE* Shared; + uint32_t DescriptionCount; +} POSIXSRV_PIPE_BACKING; + +typedef struct POSIXSRV_OPEN_HOLDER { + struct POSIXSRV_OPEN_HOLDER* Next; + POSIXSRV_OPEN_DESCRIPTION* Description; +} POSIXSRV_OPEN_HOLDER; + +typedef struct POSIXSRV_FD_INHERITANCE_ENTRY { + NTL_POSIX_FD_MANIFEST_ENTRY Manifest; + POSIXSRV_OPEN_DESCRIPTION* Description; +} POSIXSRV_FD_INHERITANCE_ENTRY; + +typedef struct POSIXSRV_FD_INHERITANCE_MONITOR { + uint64_t Token; + uint32_t NativeProcessId; + SYS_HANDLE ProcessHandle; +} POSIXSRV_FD_INHERITANCE_MONITOR; + +struct POSIXSRV_FD_INHERITANCE { + POSIXSRV_FD_INHERITANCE* Next; + uint64_t Token; + POSIXSRV_PROCESS_RECORD* SourceProcess; + POSIXSRV_PROCESS_RECORD* ClaimProcess; + uint32_t SecurityId; + uint32_t Armed; + uint32_t TargetNativeProcessId; + uint32_t EntryCount; + uint32_t DescriptionCount; + POSIXSRV_FD_INHERITANCE_ENTRY Entries[NTL_POSIX_FD_INHERIT_MAX]; + POSIXSRV_OPEN_DESCRIPTION* Descriptions[NTL_POSIX_FD_INHERIT_MAX]; +}; + +struct POSIXSRV_OPEN_DESCRIPTION { + POSIXSRV_OPEN_DESCRIPTION* Next; + uint64_t DescriptionId; + uint32_t Type; + uint32_t HolderCount; + uint32_t InheritanceRefCount; + SYS_HANDLE MasterHandle; + NTL_POSIX_SHARED_OPEN_DESCRIPTION* Shared; + POSIXSRV_PIPE_BACKING* PipeBacking; +}; + +struct POSIXSRV_PROCESS_RECORD { + struct POSIXSRV_PROCESS_RECORD* Next; + uint32_t PosixPid; + uint32_t ParentPosixPid; + uint32_t NativeProcessId; + uint32_t SecurityId; + uint32_t Generation; + uint32_t State; + uint32_t ConnectionCount; + uint32_t MonitorActive; + uint32_t UserId; + uint32_t EffectiveUserId; + uint32_t GroupId; + uint32_t EffectiveGroupId; + uint32_t ProcessGroupId; + uint32_t SessionId; + uint32_t SupplementaryGroupCount; + uint32_t SupplementaryGroups[NTL_POSIX_SUPPLEMENTARY_GROUP_MAX]; + char LoginName[NTL_POSIX_IDENTITY_NAME_MAX + 1u]; + char CurrentDirectory[SYS_PATH_MAX + 1u]; + int32_t ExitCode; + SYS_HANDLE ProcessHandle; + POSIXSRV_OPEN_HOLDER* OpenDescriptions; +}; + +typedef struct POSIXSRV_USER_RECORD { + const char* Name; + uint32_t UserId; + uint32_t GroupId; + const char* Home; + const char* Shell; +} POSIXSRV_USER_RECORD; + +typedef struct POSIXSRV_GROUP_RECORD { + const char* Name; + uint32_t GroupId; +} POSIXSRV_GROUP_RECORD; + +typedef struct POSIXSRV_CLIENT { + SYS_HANDLE Endpoint; + SYS_PORT_CONNECTION_INFO Connection; + POSIXSRV_PROCESS_RECORD* Process; + uint32_t NextTransactionId; + uint32_t ActiveTransactions; + POSIXSRV_TRANSACTION Transactions[NTL_POSIX_TRANSACTION_CAPACITY]; +} POSIXSRV_CLIENT; + +static SYS_HANDLE gProcessRegistryMutex = SYS_HANDLE_INVALID; +static POSIXSRV_PROCESS_RECORD* gProcessRegistry; +static POSIXSRV_OPEN_DESCRIPTION* gOpenDescriptionRegistry; +static POSIXSRV_FD_INHERITANCE* gFdInheritanceRegistry; +static uint32_t gNextPosixPid = POSIXSRV_FIRST_PID; +static uint64_t gNextOpenDescriptionId = 1u; +static uint64_t gNextFdInheritanceToken = 1u; +static uint32_t gRegisteredProcesses; +static uint32_t gRegisteredOpenDescriptions; + +static const POSIXSRV_USER_RECORD gUsers[] = { + { "root", 0u, 0u, "/", "/system/shell_posix.exe" }, + { "user", 1000u, 1000u, "/", "/system/shell_posix.exe" }, +}; + +static const POSIXSRV_GROUP_RECORD gGroups[] = { + { "root", 0u }, + { "users", 1000u }, +}; + +// Terminates the server without depending on a compiler runtime return path. +static void PosixsrvExit(int code) { + for (;;) SysExitProcess(code); +} + +// Gives one server thread a best-effort diagnostic name. +static void PosixsrvSetCurrentThreadName(const char* name) { + SYS_SET_THREAD_NAME_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.ThreadHandle = SYS_HANDLE_INVALID; + request.Name = SysSvFromCstr(name); + (void)SysSetThreadName(&request); +} + +// Acquires the process-registry mutex. +static KSTATUS PosixsrvLockRegistry(void) { + if (gProcessRegistryMutex == SYS_HANDLE_INVALID) return NTLSTATUS_FAILURE; + return SysWait(gProcessRegistryMutex, SYS_WAIT_FLAG_NONE); +} + +// Releases the process-registry mutex. +static void PosixsrvUnlockRegistry(void) { + if (gProcessRegistryMutex != SYS_HANDLE_INVALID) + (void)SysReleaseMutex(gProcessRegistryMutex); +} + +// Finds one live or retained record by kernel-stamped native identity. +static POSIXSRV_PROCESS_RECORD* PosixsrvFindProcessLocked(uint32_t nativePid, uint32_t securityId) { + for (POSIXSRV_PROCESS_RECORD* record = gProcessRegistry; record; record = record->Next) { + if (record->NativeProcessId == nativePid && record->SecurityId == securityId) + return record; + } + return (POSIXSRV_PROCESS_RECORD*)0; +} + +// Finds one process by stable POSIX identity while the registry mutex is held. +static POSIXSRV_PROCESS_RECORD* PosixsrvFindPosixProcessLocked(uint32_t posixPid) { + for (POSIXSRV_PROCESS_RECORD* record = gProcessRegistry; record; record = record->Next) { + if (record->PosixPid == posixPid) + return record; + } + return (POSIXSRV_PROCESS_RECORD*)0; +} + +// Returns whether a process group currently exists in one POSIX session. +static int PosixsrvProcessGroupExistsLocked(uint32_t sessionId, uint32_t processGroupId) { + if (processGroupId == 0) return 0; + for (POSIXSRV_PROCESS_RECORD* record = gProcessRegistry; record; record = record->Next) { + if (record->State == NTL_POSIX_PROCESS_STATE_RUNNING && record->SessionId == sessionId && + record->ProcessGroupId == processGroupId) + return 1; + } + return 0; +} + +// Returns whether one protocol open-description type requires a retained native handle. +static int PosixsrvOpenDescriptionNeedsHandle(uint32_t type) { + return type == NTL_POSIX_OPEN_TYPE_FILE || type == NTL_POSIX_OPEN_TYPE_SOCKET || + type == NTL_POSIX_OPEN_TYPE_PIPE_READ || type == NTL_POSIX_OPEN_TYPE_PIPE_WRITE; +} + +// Returns whether one protocol open-description type is known. +static int PosixsrvOpenDescriptionTypeValid(uint32_t type) { + return type >= NTL_POSIX_OPEN_TYPE_FILE && type <= NTL_POSIX_OPEN_TYPE_PIPE_WRITE; +} + +// Finds one process holder for a shared open description while the registry lock is held. +static POSIXSRV_OPEN_HOLDER* PosixsrvFindOpenHolderLocked(POSIXSRV_PROCESS_RECORD* process, + uint64_t descriptionId) { + if (!process) return (POSIXSRV_OPEN_HOLDER*)0; + for (POSIXSRV_OPEN_HOLDER* holder = process->OpenDescriptions; holder; holder = holder->Next) { + if (holder->Description && holder->Description->DescriptionId == descriptionId) + return holder; + } + return (POSIXSRV_OPEN_HOLDER*)0; +} + +// Removes and destroys one globally unheld open description while the registry lock is held. +static void PosixsrvDestroyOpenDescriptionLocked(POSIXSRV_OPEN_DESCRIPTION* description) { + if (!description || description->HolderCount != 0 || description->InheritanceRefCount != 0) return; + POSIXSRV_OPEN_DESCRIPTION** link = &gOpenDescriptionRegistry; + while (*link && *link != description) link = &(*link)->Next; + if (*link != description) return; + *link = description->Next; + description->Next = (POSIXSRV_OPEN_DESCRIPTION*)0; + if (gRegisteredOpenDescriptions != 0) --gRegisteredOpenDescriptions; + if (description->MasterHandle != SYS_HANDLE_INVALID) + (void)SysCloseHandle(description->MasterHandle); + if (description->PipeBacking) { + POSIXSRV_PIPE_BACKING* backing = description->PipeBacking; + description->PipeBacking = (POSIXSRV_PIPE_BACKING*)0; + if (backing->DescriptionCount != 0) + --backing->DescriptionCount; + if (backing->DescriptionCount == 0) { + if (backing->Shared) + (void)SysFreeVm(backing->Shared); + free(backing); + } + } + free(description->Shared); + free(description); +} + +// Releases one process holder and destroys the description when this was its final holder. +static KSTATUS PosixsrvReleaseOpenDescriptionLocked(POSIXSRV_PROCESS_RECORD* process, + uint64_t descriptionId) { + if (!process || descriptionId == 0) return NTLSTATUS_INVALID_PARAMETER; + POSIXSRV_OPEN_HOLDER** link = &process->OpenDescriptions; + while (*link && (!(*link)->Description || + (*link)->Description->DescriptionId != descriptionId)) + link = &(*link)->Next; + if (!*link) return NTLSTATUS_NOT_FOUND; + + POSIXSRV_OPEN_HOLDER* holder = *link; + POSIXSRV_OPEN_DESCRIPTION* description = holder->Description; + *link = holder->Next; + free(holder); + if (description && description->HolderCount != 0) + --description->HolderCount; + PosixsrvDestroyOpenDescriptionLocked(description); + return NTLSTATUS_SUCCESS; +} + +// Releases every open-description holder owned by one terminating POSIX process. +static void PosixsrvReleaseAllOpenDescriptionsLocked(POSIXSRV_PROCESS_RECORD* process) { + if (!process) return; + while (process->OpenDescriptions) { + POSIXSRV_OPEN_HOLDER* holder = process->OpenDescriptions; + POSIXSRV_OPEN_DESCRIPTION* description = holder->Description; + process->OpenDescriptions = holder->Next; + free(holder); + if (description && description->HolderCount != 0) + --description->HolderCount; + PosixsrvDestroyOpenDescriptionLocked(description); + } +} + +// Finds one pending descriptor-inheritance snapshot while the registry lock is held. +static POSIXSRV_FD_INHERITANCE* PosixsrvFindFdInheritanceLocked(uint64_t token) { + if (token == 0) return (POSIXSRV_FD_INHERITANCE*)0; + for (POSIXSRV_FD_INHERITANCE* snapshot = gFdInheritanceRegistry; snapshot; + snapshot = snapshot->Next) { + if (snapshot->Token == token) return snapshot; + } + return (POSIXSRV_FD_INHERITANCE*)0; +} + +// Drops one snapshot's temporary description references and removes it from the registry. +static void PosixsrvDestroyFdInheritanceLocked(POSIXSRV_FD_INHERITANCE* snapshot) { + if (!snapshot) return; + POSIXSRV_FD_INHERITANCE** link = &gFdInheritanceRegistry; + while (*link && *link != snapshot) link = &(*link)->Next; + if (*link != snapshot) return; + *link = snapshot->Next; + snapshot->Next = (POSIXSRV_FD_INHERITANCE*)0; + for (uint32_t i = 0; i < snapshot->DescriptionCount; ++i) { + POSIXSRV_OPEN_DESCRIPTION* description = snapshot->Descriptions[i]; + if (description && description->InheritanceRefCount != 0) + --description->InheritanceRefCount; + PosixsrvDestroyOpenDescriptionLocked(description); + } + free(snapshot); +} + +// Cleans snapshots owned or claimed by one native process generation on genuine exit. +static void PosixsrvReleaseFdInheritanceForProcessLocked(POSIXSRV_PROCESS_RECORD* process) { + if (!process) return; + POSIXSRV_FD_INHERITANCE* snapshot = gFdInheritanceRegistry; + while (snapshot) { + POSIXSRV_FD_INHERITANCE* next = snapshot->Next; + if (snapshot->ClaimProcess == process || + (snapshot->SourceProcess == process && !snapshot->ClaimProcess && !snapshot->Armed)) { + PosixsrvDestroyFdInheritanceLocked(snapshot); + } else if (snapshot->SourceProcess == process) { + snapshot->SourceProcess = (POSIXSRV_PROCESS_RECORD*)0; + } + snapshot = next; + } +} + +// Reclaims one armed snapshot if its designated native child dies before import completes. +static void PosixsrvFdInheritanceMonitorThread(uintptr_t argument) { + POSIXSRV_FD_INHERITANCE_MONITOR* monitor = + (POSIXSRV_FD_INHERITANCE_MONITOR*)argument; + if (!monitor) { + SysExitThread(1); + return; + } + PosixsrvSetCurrentThreadName("posixsrv.fd-inherit"); + + KSTATUS waitStatus = SysWait(monitor->ProcessHandle, SYS_WAIT_FLAG_NONE); + if (KSUCCESS(PosixsrvLockRegistry())) { + POSIXSRV_FD_INHERITANCE* snapshot = PosixsrvFindFdInheritanceLocked(monitor->Token); + if (snapshot && snapshot->Armed && + snapshot->TargetNativeProcessId == monitor->NativeProcessId) + PosixsrvDestroyFdInheritanceLocked(snapshot); + PosixsrvUnlockRegistry(); + } + + (void)SysCloseHandle(monitor->ProcessHandle); + free(monitor); + SysExitThread(KSUCCESS(waitStatus) ? 0 : 1); +} + +// Pins one inheritance snapshot to a concrete native child until import or child exit. +static KSTATUS PosixsrvArmFdInheritance(POSIXSRV_CLIENT* client, uint64_t token, + SYS_HANDLE* targetProcessHandle) { + if (!client || !client->Process || token == 0 || !targetProcessHandle || + *targetProcessHandle == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t nativePid = 0; + KSTATUS status = SysGetProcessPid(*targetProcessHandle, &nativePid); + if (!KSUCCESS(status)) return status; + if (nativePid == 0 || nativePid == client->Process->NativeProcessId) + return NTLSTATUS_INVALID_PARAMETER; + + POSIXSRV_FD_INHERITANCE_MONITOR* monitor = + (POSIXSRV_FD_INHERITANCE_MONITOR*)calloc(1, sizeof(*monitor)); + if (!monitor) return NTLSTATUS_NO_MEMORY; + monitor->Token = token; + monitor->NativeProcessId = nativePid; + monitor->ProcessHandle = *targetProcessHandle; + + status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) { + free(monitor); + return status; + } + + POSIXSRV_FD_INHERITANCE* snapshot = PosixsrvFindFdInheritanceLocked(token); + if (!snapshot) { + PosixsrvUnlockRegistry(); + free(monitor); + return NTLSTATUS_NOT_FOUND; + } + if (snapshot->SecurityId != client->Process->SecurityId || + snapshot->SourceProcess != client->Process) { + PosixsrvUnlockRegistry(); + free(monitor); + return NTLSTATUS_ACCESS_DENIED; + } + if (snapshot->ClaimProcess) { + status = snapshot->ClaimProcess->NativeProcessId == nativePid + ? NTLSTATUS_SUCCESS + : NTLSTATUS_ACCESS_DENIED; + PosixsrvUnlockRegistry(); + free(monitor); + return status; + } + if (snapshot->Armed) { + status = snapshot->TargetNativeProcessId == nativePid + ? NTLSTATUS_SUCCESS + : NTLSTATUS_ACCESS_DENIED; + PosixsrvUnlockRegistry(); + free(monitor); + return status; + } + + snapshot->Armed = 1u; + snapshot->TargetNativeProcessId = nativePid; + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)PosixsrvFdInheritanceMonitorThread; + create.Argument = (uintptr_t)monitor; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + status = SysCreateThread(&create); + if (!KSUCCESS(status)) { + snapshot->Armed = 0; + snapshot->TargetNativeProcessId = 0; + PosixsrvUnlockRegistry(); + free(monitor); + return status; + } + + *targetProcessHandle = SYS_HANDLE_INVALID; + PosixsrvUnlockRegistry(); + return NTLSTATUS_SUCCESS; +} + +// Returns the process-local rights needed by one imported native capability. +static SYS_RIGHTS PosixsrvOpenDescriptionClientRights(POSIXSRV_OPEN_DESCRIPTION* description) { + if (!description || !description->Shared) return 0; + uint32_t statusFlags = (uint32_t)_InterlockedCompareExchange( + &description->Shared->StatusFlags, 0, 0); + SYS_RIGHTS rights = SYS_RIGHT_QUERY; + if (description->Type == NTL_POSIX_OPEN_TYPE_FILE) { + rights |= SYS_FILE_RIGHT_LOCK; + uint32_t access = statusFlags & NTL_POSIX_OPEN_STATUS_ACCMODE; + if (access == NTL_POSIX_OPEN_STATUS_RDONLY || access == NTL_POSIX_OPEN_STATUS_RDWR) + rights |= SYS_FILE_RIGHT_READ_DATA | SYS_FILE_RIGHT_MAP_READ; + if (access == NTL_POSIX_OPEN_STATUS_WRONLY || access == NTL_POSIX_OPEN_STATUS_RDWR) + rights |= SYS_FILE_RIGHT_WRITE_DATA | SYS_FILE_RIGHT_MAP_WRITE; + } else if (description->Type == NTL_POSIX_OPEN_TYPE_SOCKET) { + rights |= SYS_SOCKET_RIGHT_BIND | SYS_SOCKET_RIGHT_CONNECT | SYS_SOCKET_RIGHT_LISTEN | + SYS_SOCKET_RIGHT_ACCEPT | SYS_SOCKET_RIGHT_SEND | SYS_SOCKET_RIGHT_RECEIVE | + SYS_SOCKET_RIGHT_SHUTDOWN | SYS_SOCKET_RIGHT_POLL | + SYS_SOCKET_RIGHT_SET_OPTION; + } else if (description->Type == NTL_POSIX_OPEN_TYPE_PIPE_READ) { + rights |= SYS_RIGHT_WAIT | SYS_PIPE_RIGHT_READ | SYS_PIPE_RIGHT_POLL; + } else if (description->Type == NTL_POSIX_OPEN_TYPE_PIPE_WRITE) { + rights |= SYS_RIGHT_WAIT | SYS_PIPE_RIGHT_WRITE | SYS_PIPE_RIGHT_POLL; + } + return rights; +} + +// Creates a one-shot server snapshot of the caller's process-local FD table manifest. +static KSTATUS PosixsrvCreateFdInheritance(POSIXSRV_CLIENT* client, + const NTL_POSIX_REQUEST* request, + uint64_t* tokenOut) { + if (!client || !client->Process || !request || !tokenOut) + return NTLSTATUS_INVALID_PARAMETER; + *tokenOut = 0; + if (request->FdInheritanceFlags != NTL_POSIX_FD_INHERIT_FLAG_NONE || + request->FdInheritanceIndex != 0 || request->FdInheritanceToken != 0 || + request->FdInheritanceCount > NTL_POSIX_FD_INHERIT_MAX) + return NTLSTATUS_INVALID_PARAMETER; + + POSIXSRV_FD_INHERITANCE* snapshot = + (POSIXSRV_FD_INHERITANCE*)calloc(1, sizeof(*snapshot)); + if (!snapshot) return NTLSTATUS_NO_MEMORY; + + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) { free(snapshot); return status; } + if (client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING) { + PosixsrvUnlockRegistry(); free(snapshot); return NTLSTATUS_OBJECT_DEAD; + } + if (gNextFdInheritanceToken == 0) { + PosixsrvUnlockRegistry(); free(snapshot); return NTLSTATUS_ID_EXHAUSTED; + } + + snapshot->Token = gNextFdInheritanceToken++; + snapshot->SourceProcess = client->Process; + snapshot->SecurityId = client->Process->SecurityId; + snapshot->EntryCount = request->FdInheritanceCount; + + for (uint32_t i = 0; i < request->FdInheritanceCount; ++i) { + const NTL_POSIX_FD_MANIFEST_ENTRY* manifest = &request->FdManifest[i]; + if (manifest->Descriptor < 0 || manifest->Descriptor >= (int32_t)NTL_POSIX_FD_INHERIT_MAX || + (manifest->DescriptorFlags & ~NTL_POSIX_FD_DESCRIPTOR_CLOEXEC) != 0 || + manifest->OpenDescriptionId == 0) { + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + for (uint32_t j = 0; j < i; ++j) { + if (snapshot->Entries[j].Manifest.Descriptor == manifest->Descriptor) { + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + } + if (!KSUCCESS(status)) break; + POSIXSRV_OPEN_HOLDER* holder = + PosixsrvFindOpenHolderLocked(client->Process, manifest->OpenDescriptionId); + if (!holder || !holder->Description) { status = NTLSTATUS_NOT_FOUND; break; } + snapshot->Entries[i].Manifest = *manifest; + snapshot->Entries[i].Description = holder->Description; + uint32_t found = 0; + for (uint32_t j = 0; j < snapshot->DescriptionCount; ++j) { + if (snapshot->Descriptions[j] == holder->Description) { found = 1; break; } + } + if (!found) snapshot->Descriptions[snapshot->DescriptionCount++] = holder->Description; + } + for (uint32_t i = request->FdInheritanceCount; i < NTL_POSIX_FD_INHERIT_MAX; ++i) { + if (request->FdManifest[i].Descriptor != 0 || request->FdManifest[i].DescriptorFlags != 0 || + request->FdManifest[i].OpenDescriptionId != 0) { + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + } + if (!KSUCCESS(status)) { + PosixsrvUnlockRegistry(); + free(snapshot); + return status; + } + + for (uint32_t i = 0; i < snapshot->DescriptionCount; ++i) + ++snapshot->Descriptions[i]->InheritanceRefCount; + snapshot->Next = gFdInheritanceRegistry; + gFdInheritanceRegistry = snapshot; + *tokenOut = snapshot->Token; + PosixsrvUnlockRegistry(); + return NTLSTATUS_SUCCESS; +} + +// Cancels one unclaimed snapshot owned by the caller. +static KSTATUS PosixsrvCancelFdInheritance(POSIXSRV_CLIENT* client, uint64_t token) { + if (!client || !client->Process || token == 0) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + POSIXSRV_FD_INHERITANCE* snapshot = PosixsrvFindFdInheritanceLocked(token); + if (!snapshot) status = NTLSTATUS_NOT_FOUND; + else if (snapshot->SourceProcess != client->Process || snapshot->ClaimProcess) + status = NTLSTATUS_ACCESS_DENIED; + else { PosixsrvDestroyFdInheritanceLocked(snapshot); status = NTLSTATUS_SUCCESS; } + PosixsrvUnlockRegistry(); + return status; +} + +// Adds one process holder for every unique description before the child imports wrappers. +static KSTATUS PosixsrvClaimFdInheritanceLocked(POSIXSRV_FD_INHERITANCE* snapshot, + POSIXSRV_PROCESS_RECORD* process) { + if (!snapshot || !process) return NTLSTATUS_INVALID_PARAMETER; + if (snapshot->Armed && snapshot->TargetNativeProcessId != process->NativeProcessId) + return NTLSTATUS_ACCESS_DENIED; + if (snapshot->ClaimProcess) + return snapshot->ClaimProcess == process ? NTLSTATUS_SUCCESS : NTLSTATUS_ACCESS_DENIED; + if (process->OpenDescriptions) + return NTLSTATUS_ACCESS_DENIED; + POSIXSRV_OPEN_DESCRIPTION* added[NTL_POSIX_FD_INHERIT_MAX]; + uint32_t addedCount = 0; + for (uint32_t i = 0; i < snapshot->DescriptionCount; ++i) { + POSIXSRV_OPEN_DESCRIPTION* description = snapshot->Descriptions[i]; + if (PosixsrvFindOpenHolderLocked(process, description->DescriptionId)) continue; + POSIXSRV_OPEN_HOLDER* holder = (POSIXSRV_OPEN_HOLDER*)calloc(1, sizeof(*holder)); + if (!holder) { + for (uint32_t j = 0; j < addedCount; ++j) + (void)PosixsrvReleaseOpenDescriptionLocked(process, added[j]->DescriptionId); + return NTLSTATUS_NO_MEMORY; + } + holder->Description = description; + holder->Next = process->OpenDescriptions; + process->OpenDescriptions = holder; + ++description->HolderCount; + added[addedCount++] = description; + } + snapshot->ClaimProcess = process; + return NTLSTATUS_SUCCESS; +} + +// Releases one holder owned by the calling POSIX process. +static KSTATUS PosixsrvReleaseOpenDescription(POSIXSRV_CLIENT* client, uint64_t descriptionId) { + if (!client || !client->Process || descriptionId == 0) + return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + if (client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING) + status = NTLSTATUS_OBJECT_DEAD; + else + status = PosixsrvReleaseOpenDescriptionLocked(client->Process, descriptionId); + PosixsrvUnlockRegistry(); + return status; +} + +// Creates one server-owned shared open description and one holder for the calling process. +static KSTATUS PosixsrvCreateOpenDescription(POSIXSRV_CLIENT* client, + const NTL_POSIX_REQUEST* request, + SYS_HANDLE* handles, + uint32_t handleCount, + uint64_t* descriptionIdOut, + NTL_POSIX_SHARED_OPEN_DESCRIPTION** sharedOut) { + if (!client || !client->Process || !request || !descriptionIdOut || !sharedOut) + return NTLSTATUS_INVALID_PARAMETER; + *descriptionIdOut = 0; + *sharedOut = (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)0; + + uint32_t type = request->OpenDescriptionType; + uint32_t statusFlags = request->OpenStatusFlags; + if (!PosixsrvOpenDescriptionTypeValid(type) || + (statusFlags & ~NTL_POSIX_OPEN_STATUS_VALID_MASK) != 0 || + (statusFlags & NTL_POSIX_OPEN_STATUS_ACCMODE) == NTL_POSIX_OPEN_STATUS_ACCMODE) + return NTLSTATUS_INVALID_PARAMETER; + uint32_t expectedHandles = PosixsrvOpenDescriptionNeedsHandle(type) ? 1u : 0u; + if (handleCount != expectedHandles || (expectedHandles && !handles)) + return NTLSTATUS_INVALID_PARAMETER; + + POSIXSRV_OPEN_DESCRIPTION* description = + (POSIXSRV_OPEN_DESCRIPTION*)calloc(1, sizeof(*description)); + POSIXSRV_OPEN_HOLDER* holder = (POSIXSRV_OPEN_HOLDER*)calloc(1, sizeof(*holder)); + NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)calloc(1, sizeof(*shared)); + if (!description || !holder || !shared) { + free(description); + free(holder); + free(shared); + return NTLSTATUS_NO_MEMORY; + } + + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) { + free(description); + free(holder); + free(shared); + return status; + } + if (client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING) { + PosixsrvUnlockRegistry(); + free(description); + free(holder); + free(shared); + return NTLSTATUS_OBJECT_DEAD; + } + if (gNextOpenDescriptionId == 0) { + PosixsrvUnlockRegistry(); + free(description); + free(holder); + free(shared); + return NTLSTATUS_ID_EXHAUSTED; + } + + uint64_t id = gNextOpenDescriptionId++; + description->DescriptionId = id; + description->Type = type; + description->HolderCount = 1u; + description->MasterHandle = expectedHandles ? handles[0] : SYS_HANDLE_INVALID; + description->Shared = shared; + + shared->Magic = NTL_POSIX_SHARED_OPEN_MAGIC; + shared->Version = NTL_POSIX_SHARED_OPEN_VERSION; + shared->Type = type; + shared->DescriptionId = id; + shared->StatusFlags = (long)statusFlags; + + holder->Description = description; + holder->Next = client->Process->OpenDescriptions; + client->Process->OpenDescriptions = holder; + description->Next = gOpenDescriptionRegistry; + gOpenDescriptionRegistry = description; + ++gRegisteredOpenDescriptions; + if (expectedHandles) handles[0] = SYS_HANDLE_INVALID; + + *descriptionIdOut = id; + *sharedOut = shared; + PosixsrvUnlockRegistry(); + return NTLSTATUS_SUCCESS; +} + +// Creates one server-owned pipe backing plus read/write shared open descriptions. +static KSTATUS PosixsrvCreatePipe(POSIXSRV_CLIENT* client, + uint64_t* readIdOut, + uint64_t* writeIdOut) { + if (!client || !client->Process || !readIdOut || !writeIdOut) + return NTLSTATUS_INVALID_PARAMETER; + *readIdOut = 0; + *writeIdOut = 0; + + SYS_HANDLE readHandle = SYS_HANDLE_INVALID; + SYS_HANDLE writeHandle = SYS_HANDLE_INVALID; + SYS_PIPE_SHARED_STATE* pipeShared = (SYS_PIPE_SHARED_STATE*)0; + SYS_CREATE_PIPE_REQUEST pipeRequest; + memset(&pipeRequest, 0, sizeof(pipeRequest)); + pipeRequest.Size = sizeof(pipeRequest); + pipeRequest.ReadHandleOut = &readHandle; + pipeRequest.WriteHandleOut = &writeHandle; + pipeRequest.SharedStateOut = &pipeShared; + KSTATUS status = SysCreatePipe(&pipeRequest); + if (!KSUCCESS(status)) return status; + + if (!pipeShared || pipeShared->Magic != SYS_PIPE_SHARED_MAGIC || + pipeShared->Version != SYS_PIPE_SHARED_VERSION || + pipeShared->Capacity != SYS_PIPE_BUFFER_CAPACITY || + pipeShared->HeaderBytes != SYS_PIPE_SHARED_HEADER_BYTES) { + (void)SysCloseHandle(readHandle); + (void)SysCloseHandle(writeHandle); + if (pipeShared) (void)SysFreeVm(pipeShared); + return NTLSTATUS_FAILURE; + } + + POSIXSRV_PIPE_BACKING* backing = + (POSIXSRV_PIPE_BACKING*)calloc(1, sizeof(*backing)); + POSIXSRV_OPEN_DESCRIPTION* readDescription = + (POSIXSRV_OPEN_DESCRIPTION*)calloc(1, sizeof(*readDescription)); + POSIXSRV_OPEN_DESCRIPTION* writeDescription = + (POSIXSRV_OPEN_DESCRIPTION*)calloc(1, sizeof(*writeDescription)); + POSIXSRV_OPEN_HOLDER* readHolder = + (POSIXSRV_OPEN_HOLDER*)calloc(1, sizeof(*readHolder)); + POSIXSRV_OPEN_HOLDER* writeHolder = + (POSIXSRV_OPEN_HOLDER*)calloc(1, sizeof(*writeHolder)); + NTL_POSIX_SHARED_OPEN_DESCRIPTION* readShared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)calloc(1, sizeof(*readShared)); + NTL_POSIX_SHARED_OPEN_DESCRIPTION* writeShared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)calloc(1, sizeof(*writeShared)); + if (!backing || !readDescription || !writeDescription || !readHolder || !writeHolder || + !readShared || !writeShared) { + free(backing); + free(readDescription); + free(writeDescription); + free(readHolder); + free(writeHolder); + free(readShared); + free(writeShared); + (void)SysCloseHandle(readHandle); + (void)SysCloseHandle(writeHandle); + (void)SysFreeVm(pipeShared); + return NTLSTATUS_NO_MEMORY; + } + + status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) { + free(backing); + free(readDescription); + free(writeDescription); + free(readHolder); + free(writeHolder); + free(readShared); + free(writeShared); + (void)SysCloseHandle(readHandle); + (void)SysCloseHandle(writeHandle); + (void)SysFreeVm(pipeShared); + return status; + } + if (client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING) { + PosixsrvUnlockRegistry(); + free(backing); + free(readDescription); + free(writeDescription); + free(readHolder); + free(writeHolder); + free(readShared); + free(writeShared); + (void)SysCloseHandle(readHandle); + (void)SysCloseHandle(writeHandle); + (void)SysFreeVm(pipeShared); + return NTLSTATUS_OBJECT_DEAD; + } + if (gNextOpenDescriptionId == 0 || gNextOpenDescriptionId == UINT64_MAX) { + PosixsrvUnlockRegistry(); + free(backing); + free(readDescription); + free(writeDescription); + free(readHolder); + free(writeHolder); + free(readShared); + free(writeShared); + (void)SysCloseHandle(readHandle); + (void)SysCloseHandle(writeHandle); + (void)SysFreeVm(pipeShared); + return NTLSTATUS_ID_EXHAUSTED; + } + + uint64_t readId = gNextOpenDescriptionId++; + uint64_t writeId = gNextOpenDescriptionId++; + backing->Shared = pipeShared; + backing->DescriptionCount = 2u; + + readDescription->DescriptionId = readId; + readDescription->Type = NTL_POSIX_OPEN_TYPE_PIPE_READ; + readDescription->HolderCount = 1u; + readDescription->MasterHandle = readHandle; + readDescription->Shared = readShared; + readDescription->PipeBacking = backing; + readShared->Magic = NTL_POSIX_SHARED_OPEN_MAGIC; + readShared->Version = NTL_POSIX_SHARED_OPEN_VERSION; + readShared->Type = NTL_POSIX_OPEN_TYPE_PIPE_READ; + readShared->DescriptionId = readId; + readShared->StatusFlags = NTL_POSIX_OPEN_STATUS_RDONLY; + readShared->SharedObjectData = (uint64_t)(uintptr_t)pipeShared; + + writeDescription->DescriptionId = writeId; + writeDescription->Type = NTL_POSIX_OPEN_TYPE_PIPE_WRITE; + writeDescription->HolderCount = 1u; + writeDescription->MasterHandle = writeHandle; + writeDescription->Shared = writeShared; + writeDescription->PipeBacking = backing; + writeShared->Magic = NTL_POSIX_SHARED_OPEN_MAGIC; + writeShared->Version = NTL_POSIX_SHARED_OPEN_VERSION; + writeShared->Type = NTL_POSIX_OPEN_TYPE_PIPE_WRITE; + writeShared->DescriptionId = writeId; + writeShared->StatusFlags = NTL_POSIX_OPEN_STATUS_WRONLY; + writeShared->SharedObjectData = (uint64_t)(uintptr_t)pipeShared; + + readHolder->Description = readDescription; + writeHolder->Description = writeDescription; + writeHolder->Next = client->Process->OpenDescriptions; + readHolder->Next = writeHolder; + client->Process->OpenDescriptions = readHolder; + + writeDescription->Next = gOpenDescriptionRegistry; + readDescription->Next = writeDescription; + gOpenDescriptionRegistry = readDescription; + gRegisteredOpenDescriptions += 2u; + + *readIdOut = readId; + *writeIdOut = writeId; + PosixsrvUnlockRegistry(); + return NTLSTATUS_SUCCESS; +} + +// Applies F_SETFL mutable state through the master native capability and publishes the result. +static KSTATUS PosixsrvSetOpenStatus(POSIXSRV_CLIENT* client, + uint64_t descriptionId, + uint32_t requestedFlags, + uint32_t* statusFlagsOut, + uint32_t* typeOut) { + if (!client || !client->Process || descriptionId == 0 || !statusFlagsOut || !typeOut) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + POSIXSRV_OPEN_HOLDER* holder = + PosixsrvFindOpenHolderLocked(client->Process, descriptionId); + if (!holder || !holder->Description || !holder->Description->Shared) { + PosixsrvUnlockRegistry(); + return NTLSTATUS_NOT_FOUND; + } + + POSIXSRV_OPEN_DESCRIPTION* description = holder->Description; + uint32_t current = (uint32_t)_InterlockedCompareExchange( + &description->Shared->StatusFlags, 0, 0); + uint32_t updated = (current & ~(NTL_POSIX_OPEN_STATUS_APPEND | + NTL_POSIX_OPEN_STATUS_NONBLOCK)) | + (requestedFlags & (NTL_POSIX_OPEN_STATUS_APPEND | + NTL_POSIX_OPEN_STATUS_NONBLOCK)); + + status = NTLSTATUS_SUCCESS; + if (description->Type == NTL_POSIX_OPEN_TYPE_FILE) { + SYS_SET_FILE_STATUS_REQUEST fileRequest; + memset(&fileRequest, 0, sizeof(fileRequest)); + fileRequest.Size = sizeof(fileRequest); + fileRequest.FileHandle = description->MasterHandle; + fileRequest.Mask = SYS_FILE_STATUS_APPEND; + fileRequest.Flags = (updated & NTL_POSIX_OPEN_STATUS_APPEND) ? + SYS_FILE_STATUS_APPEND : 0; + status = SysSetFileStatus(&fileRequest); + } else if (description->Type == NTL_POSIX_OPEN_TYPE_SOCKET) { + uint32_t nonblocking = (updated & NTL_POSIX_OPEN_STATUS_NONBLOCK) ? 1u : 0u; + SYS_SOCKET_OPTION_REQUEST socketRequest; + memset(&socketRequest, 0, sizeof(socketRequest)); + socketRequest.Size = sizeof(socketRequest); + socketRequest.Option = SYS_SOCKET_OPT_NONBLOCKING; + socketRequest.Value = &nonblocking; + socketRequest.ValueSize = sizeof(nonblocking); + status = SysSetSocketOption(description->MasterHandle, &socketRequest); + } + + if (KSUCCESS(status)) + _InterlockedExchange(&description->Shared->StatusFlags, (long)updated); + *statusFlagsOut = KSUCCESS(status) ? updated : current; + *typeOut = description->Type; + PosixsrvUnlockRegistry(); + return status; +} + +// Finds one synthesized passwd record by uid or exact name. +static const POSIXSRV_USER_RECORD* PosixsrvFindUser(uint32_t lookupKind, + uint32_t userId, + const char* name) { + for (uint32_t i = 0; i < (uint32_t)(sizeof(gUsers) / sizeof(gUsers[0])); ++i) { + if ((lookupKind == NTL_POSIX_LOOKUP_BY_ID && gUsers[i].UserId == userId) || + (lookupKind == NTL_POSIX_LOOKUP_BY_NAME && name && strcmp(gUsers[i].Name, name) == 0)) + return &gUsers[i]; + } + return (const POSIXSRV_USER_RECORD*)0; +} + +// Finds one synthesized group record by gid or exact name. +static const POSIXSRV_GROUP_RECORD* PosixsrvFindGroup(uint32_t lookupKind, + uint32_t groupId, + const char* name) { + for (uint32_t i = 0; i < (uint32_t)(sizeof(gGroups) / sizeof(gGroups[0])); ++i) { + if ((lookupKind == NTL_POSIX_LOOKUP_BY_ID && gGroups[i].GroupId == groupId) || + (lookupKind == NTL_POSIX_LOOKUP_BY_NAME && name && strcmp(gGroups[i].Name, name) == 0)) + return &gGroups[i]; + } + return (const POSIXSRV_GROUP_RECORD*)0; +} + +// Validates one length-delimited canonical native current-directory string. +static int PosixsrvValidateCurrentDirectory(const NTL_POSIX_REQUEST* request) { + if (!request || request->PathLength == 0 || request->PathLength > SYS_PATH_MAX) + return 0; + if (request->Path[0] != '/' || request->Path[request->PathLength] != '\0') + return 0; + return strlen(request->Path) == request->PathLength; +} + +// Validates one optional length-delimited identity lookup name. +static int PosixsrvValidateLookupName(const NTL_POSIX_REQUEST* request) { + if (!request || request->NameLength == 0 || request->NameLength > NTL_POSIX_IDENTITY_NAME_MAX) + return 0; + if (request->Name[request->NameLength] != '\0') + return 0; + return strlen(request->Name) == request->NameLength; +} + +// Removes one process record from the registry while the registry mutex is held. +static void PosixsrvUnlinkProcessLocked(POSIXSRV_PROCESS_RECORD* record) { + if (!record) return; + POSIXSRV_PROCESS_RECORD** link = &gProcessRegistry; + while (*link && *link != record) link = &(*link)->Next; + if (*link == record) { + *link = record->Next; + record->Next = (POSIXSRV_PROCESS_RECORD*)0; + if (gRegisteredProcesses != 0) --gRegisteredProcesses; + } +} + +// Releases one detached process record and its retained native capability. +static void PosixsrvFreeProcessRecord(POSIXSRV_PROCESS_RECORD* record) { + if (!record) return; + if (record->ProcessHandle != SYS_HANDLE_INVALID) + (void)SysCloseHandle(record->ProcessHandle); + free(record); +} + +// Allocates one stable positive POSIX PID from the server-owned namespace. +static KSTATUS PosixsrvAllocatePidLocked(uint32_t* pidOut) { + if (!pidOut) return NTLSTATUS_INVALID_PARAMETER; + *pidOut = 0; + if (gNextPosixPid < POSIXSRV_FIRST_PID || gNextPosixPid > POSIXSRV_LAST_PID) + return NTLSTATUS_ID_EXHAUSTED; + *pidOut = gNextPosixPid++; + return NTLSTATUS_SUCCESS; +} + +// Verifies that the service runs without ambient privilege or service-registration claims. +static KSTATUS PosixsrvValidateSecurity(void) { + SYS_SECURITY_CONTEXT_INFO security; + memset(&security, 0, sizeof(security)); + security.Size = sizeof(security); + KSTATUS status = SysQuerySecurityContext(SYS_SECURITY_CONTEXT_CURRENT, &security); + if (!KSUCCESS(status)) return status; + if (security.EffectivePrivileges != 0 || security.DelegablePrivileges != 0 || + security.ServiceClaims != 0 || security.DelegableServiceClaims != 0) + return NTLSTATUS_ACCESS_DENIED; + return NTLSTATUS_SUCCESS; +} + +// Accepts init's private service-control connection. +static KSTATUS PosixsrvAcceptControl(SYS_HANDLE listener, SYS_HANDLE* endpointOut) { + if (listener == SYS_HANDLE_INVALID || !endpointOut) return NTLSTATUS_INVALID_PARAMETER; + SYS_ACCEPT_PORT_REQUEST accept; + memset(&accept, 0, sizeof(accept)); + accept.Size = sizeof(accept); + accept.ListenerHandle = listener; + accept.Flags = SYS_PORT_ACCEPT_FLAG_NONE; + accept.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + accept.EndpointHandleOut = endpointOut; + return SysAcceptPort(&accept); +} + +// Creates the public client listener exported through the generic service READY contract. +static KSTATUS PosixsrvCreateListener(SYS_HANDLE* listenerOut) { + if (!listenerOut) return NTLSTATUS_INVALID_PARAMETER; + *listenerOut = SYS_HANDLE_INVALID; + SYS_CREATE_PORT_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Flags = SYS_PORT_FLAG_NONE; + create.Backlog = SYS_PORT_DEFAULT_BACKLOG; + create.HandleOut = listenerOut; + return SysCreatePort(&create); +} + +// Copies one bounded NUL-terminated string into a protocol field and returns its byte length. +static uint32_t PosixsrvCopyText(char* destination, uint32_t capacity, const char* source) { + if (!destination || capacity == 0) return 0; + if (!source) source = ""; + size_t length = strlen(source); + if (length >= capacity) length = capacity - 1u; + if (length) memcpy(destination, source, length); + destination[length] = '\0'; + return (uint32_t)length; +} + +// Copies the current process identity and shared attributes into one reply. +static void PosixsrvSnapshotReplyIdentity(POSIXSRV_CLIENT* client, NTL_POSIX_REPLY* reply) { + if (!client || !reply || !KSUCCESS(PosixsrvLockRegistry())) return; + if (client->Process) { + POSIXSRV_PROCESS_RECORD* record = client->Process; + reply->PosixPid = record->PosixPid; + reply->NativeGeneration = record->Generation; + reply->ProcessState = record->State; + reply->ParentPosixPid = record->ParentPosixPid; + reply->UserId = record->UserId; + reply->EffectiveUserId = record->EffectiveUserId; + reply->GroupId = record->GroupId; + reply->EffectiveGroupId = record->EffectiveGroupId; + reply->ProcessGroupId = record->ProcessGroupId; + reply->SessionId = record->SessionId; + reply->SupplementaryGroupCount = record->SupplementaryGroupCount; + if (reply->SupplementaryGroupCount > NTL_POSIX_SUPPLEMENTARY_GROUP_MAX) + reply->SupplementaryGroupCount = NTL_POSIX_SUPPLEMENTARY_GROUP_MAX; + for (uint32_t i = 0; i < reply->SupplementaryGroupCount; ++i) + reply->SupplementaryGroups[i] = record->SupplementaryGroups[i]; + reply->NameLength = PosixsrvCopyText(reply->Name, sizeof(reply->Name), record->LoginName); + reply->CurrentDirectoryLength = PosixsrvCopyText(reply->CurrentDirectory, + sizeof(reply->CurrentDirectory), + record->CurrentDirectory); + } + reply->RegisteredProcesses = gRegisteredProcesses; + reply->RegisteredOpenDescriptions = gRegisteredOpenDescriptions; + PosixsrvUnlockRegistry(); +} + +// Initializes the common fields of one protocol reply. +static void PosixsrvInitializeReply(POSIXSRV_CLIENT* client, + NTL_POSIX_REPLY* reply, + uint32_t operation, + KSTATUS status, + uint32_t transactionId) { + memset(reply, 0, sizeof(*reply)); + reply->Size = sizeof(*reply); + reply->Version = NTL_POSIX_PROTOCOL_VERSION; + reply->Operation = operation; + reply->Flags = NTL_POSIX_REQUEST_FLAG_NONE; + reply->Status = status; + reply->TransactionId = transactionId; + reply->ClientProcessId = client->Connection.ClientProcessId; + reply->ClientSecurityId = client->Connection.ClientSecurityId; + reply->ActiveTransactions = client->ActiveTransactions; + reply->TransactionCapacity = NTL_POSIX_TRANSACTION_CAPACITY; + PosixsrvSnapshotReplyIdentity(client, reply); +} + +// Sends one already-prepared fixed-size protocol reply with no capability transfer. +static KSTATUS PosixsrvSendReply(POSIXSRV_CLIENT* client, + uint32_t messageId, + const NTL_POSIX_REPLY* reply) { + if (!client || !reply) return NTLSTATUS_INVALID_PARAMETER; + SYS_REPLY_PORT_REQUEST out; + memset(&out, 0, sizeof(out)); + out.Size = sizeof(out); + out.EndpointHandle = client->Endpoint; + out.MessageId = messageId; + out.Flags = SYS_PORT_FLAG_NONE; + out.Payload = reply; + out.PayloadLength = sizeof(*reply); + return SysReplyPort(&out); +} + +// Sends one prepared reply with a bounded set of transferred capabilities. +static KSTATUS PosixsrvSendReplyWithHandles(POSIXSRV_CLIENT* client, + uint32_t messageId, + const NTL_POSIX_REPLY* reply, + const SYS_PORT_HANDLE_TRANSFER* handles, + uint32_t handleCount) { + if (!client || !reply || handleCount > SYS_PORT_MAX_TRANSFER_HANDLES || + (handleCount != 0 && !handles)) + return NTLSTATUS_INVALID_PARAMETER; + SYS_REPLY_PORT_REQUEST out; + memset(&out, 0, sizeof(out)); + out.Size = sizeof(out); + out.EndpointHandle = client->Endpoint; + out.MessageId = messageId; + out.Flags = SYS_PORT_FLAG_NONE; + out.Payload = reply; + out.PayloadLength = sizeof(*reply); + out.Handles = handles; + out.HandleCount = handleCount; + return SysReplyPort(&out); +} + +// Sends one ordinary process/transaction reply. +static KSTATUS PosixsrvReply(POSIXSRV_CLIENT* client, + uint32_t messageId, + uint32_t operation, + KSTATUS status, + uint32_t transactionId) { + if (!client) return NTLSTATUS_INVALID_PARAMETER; + NTL_POSIX_REPLY reply; + PosixsrvInitializeReply(client, &reply, operation, status, transactionId); + return PosixsrvSendReply(client, messageId, &reply); +} + +// Sends one open-description result while preserving current-process metadata. +static KSTATUS PosixsrvReplyOpenDescription(POSIXSRV_CLIENT* client, + uint32_t messageId, + uint32_t operation, + KSTATUS status, + uint64_t descriptionId, + uint32_t type, + uint32_t statusFlags, + NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared) { + NTL_POSIX_REPLY reply; + PosixsrvInitializeReply(client, &reply, operation, status, 0); + if (KSUCCESS(status)) { + reply.OpenDescriptionId = descriptionId; + reply.OpenDescriptionType = type; + reply.OpenStatusFlags = statusFlags; + reply.SharedOpenDescription = (uint64_t)(uintptr_t)shared; + } + return PosixsrvSendReply(client, messageId, &reply); +} + +// Sends one server-created pipe result with endpoint capabilities in read/write order. +static KSTATUS PosixsrvReplyPipe(POSIXSRV_CLIENT* client, + uint32_t messageId, + KSTATUS status, + uint64_t readId, + uint64_t writeId) { + NTL_POSIX_REPLY reply; + PosixsrvInitializeReply(client, &reply, NTL_POSIX_OP_CREATE_PIPE, status, 0); + if (!KSUCCESS(status)) + return PosixsrvSendReply(client, messageId, &reply); + + status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + POSIXSRV_OPEN_HOLDER* readHolder = + PosixsrvFindOpenHolderLocked(client->Process, readId); + POSIXSRV_OPEN_HOLDER* writeHolder = + PosixsrvFindOpenHolderLocked(client->Process, writeId); + POSIXSRV_OPEN_DESCRIPTION* readDescription = + readHolder ? readHolder->Description : (POSIXSRV_OPEN_DESCRIPTION*)0; + POSIXSRV_OPEN_DESCRIPTION* writeDescription = + writeHolder ? writeHolder->Description : (POSIXSRV_OPEN_DESCRIPTION*)0; + if (!readDescription || !writeDescription || !readDescription->Shared || + !writeDescription->Shared || + readDescription->Type != NTL_POSIX_OPEN_TYPE_PIPE_READ || + writeDescription->Type != NTL_POSIX_OPEN_TYPE_PIPE_WRITE || + readDescription->MasterHandle == SYS_HANDLE_INVALID || + writeDescription->MasterHandle == SYS_HANDLE_INVALID) { + PosixsrvUnlockRegistry(); + return PosixsrvReply(client, messageId, NTL_POSIX_OP_CREATE_PIPE, + NTLSTATUS_OBJECT_DEAD, 0); + } + + reply.OpenDescriptionId = readDescription->DescriptionId; + reply.OpenDescriptionType = readDescription->Type; + reply.OpenStatusFlags = (uint32_t)_InterlockedCompareExchange( + &readDescription->Shared->StatusFlags, 0, 0); + reply.SharedOpenDescription = (uint64_t)(uintptr_t)readDescription->Shared; + reply.OpenDescriptionId2 = writeDescription->DescriptionId; + reply.OpenDescriptionType2 = writeDescription->Type; + reply.OpenStatusFlags2 = (uint32_t)_InterlockedCompareExchange( + &writeDescription->Shared->StatusFlags, 0, 0); + reply.SharedOpenDescription2 = (uint64_t)(uintptr_t)writeDescription->Shared; + + SYS_PORT_HANDLE_TRANSFER transfers[2]; + memset(transfers, 0, sizeof(transfers)); + transfers[0].Handle = readDescription->MasterHandle; + transfers[0].DesiredRights = SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | + SYS_PIPE_RIGHT_READ | SYS_PIPE_RIGHT_POLL; + transfers[0].Flags = SYS_PORT_TRANSFER_FLAG_NONE; + transfers[1].Handle = writeDescription->MasterHandle; + transfers[1].DesiredRights = SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | + SYS_PIPE_RIGHT_WRITE | SYS_PIPE_RIGHT_POLL; + transfers[1].Flags = SYS_PORT_TRANSFER_FLAG_NONE; + KSTATUS replyStatus = PosixsrvSendReplyWithHandles(client, messageId, &reply, transfers, 2u); + PosixsrvUnlockRegistry(); + return replyStatus; +} + +// Sends one bounded inherited-description batch and its process-local native aliases. +static KSTATUS PosixsrvReplyFdInheritance(POSIXSRV_CLIENT* client, + uint32_t messageId, + uint64_t token, + uint32_t descriptionIndex) { + if (!client || !client->Process || token == 0) return NTLSTATUS_INVALID_PARAMETER; + + NTL_POSIX_REPLY reply; + PosixsrvInitializeReply(client, &reply, NTL_POSIX_OP_IMPORT_FD_INHERITANCE, + NTLSTATUS_SUCCESS, 0); + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + + POSIXSRV_FD_INHERITANCE* snapshot = PosixsrvFindFdInheritanceLocked(token); + if (!snapshot || snapshot->SecurityId != client->Process->SecurityId || + client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING) { + PosixsrvUnlockRegistry(); + return PosixsrvReply(client, messageId, NTL_POSIX_OP_IMPORT_FD_INHERITANCE, + snapshot ? NTLSTATUS_ACCESS_DENIED : NTLSTATUS_NOT_FOUND, 0); + } + if ((snapshot->DescriptionCount != 0 && descriptionIndex >= snapshot->DescriptionCount) || + (snapshot->DescriptionCount == 0 && descriptionIndex != 0)) { + PosixsrvUnlockRegistry(); + return PosixsrvReply(client, messageId, NTL_POSIX_OP_IMPORT_FD_INHERITANCE, + NTLSTATUS_INVALID_PARAMETER, 0); + } + + status = PosixsrvClaimFdInheritanceLocked(snapshot, client->Process); + if (!KSUCCESS(status)) { + PosixsrvUnlockRegistry(); + return PosixsrvReply(client, messageId, NTL_POSIX_OP_IMPORT_FD_INHERITANCE, status, 0); + } + + uint32_t count = snapshot->DescriptionCount - descriptionIndex; + if (count > NTL_POSIX_FD_IMPORT_BATCH_MAX) count = NTL_POSIX_FD_IMPORT_BATCH_MAX; + reply.FdInheritanceToken = token; + reply.FdInheritanceIndex = descriptionIndex; + reply.FdInheritanceCount = snapshot->EntryCount; + reply.FdDescriptionIndex = descriptionIndex; + reply.FdDescriptionCount = count; + reply.FdDescriptionTotal = snapshot->DescriptionCount; + for (uint32_t i = 0; i < snapshot->EntryCount; ++i) + reply.FdManifest[i] = snapshot->Entries[i].Manifest; + + SYS_PORT_HANDLE_TRANSFER transfers[NTL_POSIX_FD_IMPORT_BATCH_MAX]; + memset(transfers, 0, sizeof(transfers)); + uint32_t handleCount = 0; + for (uint32_t i = 0; i < count; ++i) { + POSIXSRV_OPEN_DESCRIPTION* description = snapshot->Descriptions[descriptionIndex + i]; + if (!description || !description->Shared) { status = NTLSTATUS_OBJECT_DEAD; break; } + NTL_POSIX_FD_DESCRIPTION_ENTRY* out = &reply.FdDescriptions[i]; + out->OpenDescriptionId = description->DescriptionId; + out->SharedOpenDescription = (uint64_t)(uintptr_t)description->Shared; + out->OpenDescriptionType = description->Type; + out->OpenStatusFlags = (uint32_t)_InterlockedCompareExchange( + &description->Shared->StatusFlags, 0, 0); + out->HandleIndex = NTL_POSIX_FD_HANDLE_NONE; + if (PosixsrvOpenDescriptionNeedsHandle(description->Type)) { + if (description->MasterHandle == SYS_HANDLE_INVALID || handleCount >= NTL_POSIX_FD_IMPORT_BATCH_MAX) { + status = NTLSTATUS_OBJECT_DEAD; + break; + } + out->HandleIndex = handleCount; + transfers[handleCount].Handle = description->MasterHandle; + transfers[handleCount].DesiredRights = PosixsrvOpenDescriptionClientRights(description); + transfers[handleCount].Flags = SYS_PORT_TRANSFER_FLAG_NONE; + ++handleCount; + } + } + + if (!KSUCCESS(status)) { + PosixsrvUnlockRegistry(); + return PosixsrvReply(client, messageId, NTL_POSIX_OP_IMPORT_FD_INHERITANCE, status, 0); + } + + KSTATUS replyStatus = PosixsrvSendReplyWithHandles(client, messageId, &reply, + transfers, handleCount); + if (KSUCCESS(replyStatus) && descriptionIndex + count == snapshot->DescriptionCount) + PosixsrvDestroyFdInheritanceLocked(snapshot); + PosixsrvUnlockRegistry(); + return replyStatus; +} + +// Sends one passwd-database lookup result while preserving current-process metadata. +static KSTATUS PosixsrvReplyUser(POSIXSRV_CLIENT* client, + uint32_t messageId, + uint32_t operation, + KSTATUS status, + const POSIXSRV_USER_RECORD* user) { + NTL_POSIX_REPLY reply; + PosixsrvInitializeReply(client, &reply, operation, status, 0); + if (KSUCCESS(status) && user) { + reply.UserId = user->UserId; + reply.GroupId = user->GroupId; + reply.NameLength = PosixsrvCopyText(reply.Name, sizeof(reply.Name), user->Name); + reply.HomeLength = PosixsrvCopyText(reply.Home, sizeof(reply.Home), user->Home); + reply.ShellLength = PosixsrvCopyText(reply.Shell, sizeof(reply.Shell), user->Shell); + } + return PosixsrvSendReply(client, messageId, &reply); +} + +// Sends one group-database lookup result while preserving current-process metadata. +static KSTATUS PosixsrvReplyGroup(POSIXSRV_CLIENT* client, + uint32_t messageId, + uint32_t operation, + KSTATUS status, + const POSIXSRV_GROUP_RECORD* group) { + NTL_POSIX_REPLY reply; + PosixsrvInitializeReply(client, &reply, operation, status, 0); + if (KSUCCESS(status) && group) { + reply.GroupId = group->GroupId; + reply.NameLength = PosixsrvCopyText(reply.Name, sizeof(reply.Name), group->Name); + } + return PosixsrvSendReply(client, messageId, &reply); +} + +// Waits for one native process generation and records genuine process termination. +static void PosixsrvProcessMonitorThread(uintptr_t argument) { + POSIXSRV_PROCESS_RECORD* record = (POSIXSRV_PROCESS_RECORD*)argument; + if (!record) { + SysExitThread(1); + return; + } + PosixsrvSetCurrentThreadName("posixsrv.process"); + + KSTATUS waitStatus = SysWait(record->ProcessHandle, SYS_WAIT_FLAG_NONE); + int32_t exitCode = (int32_t)NTLSTATUS_PROCESS_UNKNOWN_FAULT; + if (KSUCCESS(waitStatus)) + (void)SysGetProcessExitCode(record->ProcessHandle, &exitCode); + + POSIXSRV_PROCESS_RECORD* freeRecord = (POSIXSRV_PROCESS_RECORD*)0; + SYS_HANDLE closeHandle = SYS_HANDLE_INVALID; + if (KSUCCESS(PosixsrvLockRegistry())) { + if (KSUCCESS(waitStatus) && record->State == NTL_POSIX_PROCESS_STATE_RUNNING) { + PosixsrvReleaseFdInheritanceForProcessLocked(record); + PosixsrvReleaseAllOpenDescriptionsLocked(record); + record->State = NTL_POSIX_PROCESS_STATE_EXITED; + record->ExitCode = exitCode; + closeHandle = record->ProcessHandle; + record->ProcessHandle = SYS_HANDLE_INVALID; + } + record->MonitorActive = 0; + if (record->State == NTL_POSIX_PROCESS_STATE_EXITED && record->ConnectionCount == 0) { + PosixsrvUnlinkProcessLocked(record); + freeRecord = record; + } + PosixsrvUnlockRegistry(); + } + + if (closeHandle != SYS_HANDLE_INVALID) (void)SysCloseHandle(closeHandle); + if (freeRecord) PosixsrvFreeProcessRecord(freeRecord); + SysExitThread(KSUCCESS(waitStatus) ? 0 : 1); +} + +// Registers the connection's native process and assigns or recovers its stable POSIX identity. +static KSTATUS PosixsrvRegisterProcess(POSIXSRV_CLIENT* client, + const NTL_POSIX_REQUEST* request, + SYS_HANDLE* transferredProcessHandle) { + if (!client || !request || !transferredProcessHandle || + *transferredProcessHandle == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + // Reserved bootstrap identities become active when server-owned process construction lands. + if (request->BootstrapToken != 0 || request->BootstrapPosixPid != 0 || + request->BootstrapGeneration != 0) + return NTLSTATUS_NOT_SUPPORTED; + if (!PosixsrvValidateCurrentDirectory(request) || request->NameLength != 0 || + request->TargetPosixPid != 0 || request->UserId != 0 || request->GroupId != 0 || + request->ProcessGroupId != 0 || request->LookupKind != NTL_POSIX_LOOKUP_NONE || + request->OpenDescriptionType != 0 || request->OpenStatusFlags != 0 || + request->OpenDescriptionId != 0) + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t nativePid = 0; + KSTATUS status = SysGetProcessPid(*transferredProcessHandle, &nativePid); + if (!KSUCCESS(status)) return status; + if (nativePid != client->Connection.ClientProcessId) + return NTLSTATUS_ACCESS_DENIED; + + POSIXSRV_PROCESS_RECORD* candidate = + (POSIXSRV_PROCESS_RECORD*)calloc(1, sizeof(*candidate)); + if (!candidate) return NTLSTATUS_NO_MEMORY; + candidate->ProcessHandle = SYS_HANDLE_INVALID; + + status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) { + free(candidate); + return status; + } + + if (client->Process) { + PosixsrvUnlockRegistry(); + free(candidate); + return NTLSTATUS_SUCCESS; + } + + POSIXSRV_PROCESS_RECORD* existing = + PosixsrvFindProcessLocked(nativePid, client->Connection.ClientSecurityId); + if (existing && existing->State == NTL_POSIX_PROCESS_STATE_RUNNING) { + existing->ConnectionCount++; + client->Process = existing; + PosixsrvUnlockRegistry(); + free(candidate); + return NTLSTATUS_SUCCESS; + } + + uint32_t posixPid = 0; + status = PosixsrvAllocatePidLocked(&posixPid); + if (!KSUCCESS(status)) { + PosixsrvUnlockRegistry(); + free(candidate); + return status; + } + + candidate->PosixPid = posixPid; + candidate->ParentPosixPid = 0; + candidate->NativeProcessId = nativePid; + candidate->SecurityId = client->Connection.ClientSecurityId; + candidate->Generation = 1u; + candidate->State = NTL_POSIX_PROCESS_STATE_RUNNING; + candidate->ConnectionCount = 1u; + candidate->MonitorActive = 1u; + candidate->UserId = 0; + candidate->EffectiveUserId = 0; + candidate->GroupId = 0; + candidate->EffectiveGroupId = 0; + candidate->ProcessGroupId = posixPid; + candidate->SessionId = posixPid; + candidate->SupplementaryGroupCount = 0; + (void)PosixsrvCopyText(candidate->LoginName, sizeof(candidate->LoginName), "root"); + (void)PosixsrvCopyText(candidate->CurrentDirectory, sizeof(candidate->CurrentDirectory), request->Path); + candidate->ProcessHandle = *transferredProcessHandle; + candidate->Next = gProcessRegistry; + gProcessRegistry = candidate; + ++gRegisteredProcesses; + client->Process = candidate; + + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)PosixsrvProcessMonitorThread; + create.Argument = (uintptr_t)candidate; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + status = SysCreateThread(&create); + if (!KSUCCESS(status)) { + client->Process = (POSIXSRV_PROCESS_RECORD*)0; + PosixsrvUnlinkProcessLocked(candidate); + candidate->ProcessHandle = SYS_HANDLE_INVALID; + PosixsrvUnlockRegistry(); + free(candidate); + return status; + } + + *transferredProcessHandle = SYS_HANDLE_INVALID; + PosixsrvUnlockRegistry(); + return NTLSTATUS_SUCCESS; +} + +// Detaches one Port connection without treating transport loss as process termination. +static void PosixsrvDetachClientProcess(POSIXSRV_CLIENT* client) { + if (!client || !client->Process) return; + + POSIXSRV_PROCESS_RECORD* freeRecord = (POSIXSRV_PROCESS_RECORD*)0; + if (!KSUCCESS(PosixsrvLockRegistry())) return; + POSIXSRV_PROCESS_RECORD* record = client->Process; + client->Process = (POSIXSRV_PROCESS_RECORD*)0; + if (record->ConnectionCount != 0) --record->ConnectionCount; + if (record->State == NTL_POSIX_PROCESS_STATE_EXITED && record->ConnectionCount == 0 && + !record->MonitorActive) { + PosixsrvUnlinkProcessLocked(record); + freeRecord = record; + } + PosixsrvUnlockRegistry(); + if (freeRecord) PosixsrvFreeProcessRecord(freeRecord); +} + +// Replaces the current process's authoritative POSIX current directory. +static KSTATUS PosixsrvSetCurrentDirectory(POSIXSRV_CLIENT* client, + const NTL_POSIX_REQUEST* request) { + if (!client || !request || !client->Process) + return NTLSTATUS_INVALID_PARAMETER; + if (!PosixsrvValidateCurrentDirectory(request)) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + if (client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING) { + PosixsrvUnlockRegistry(); + return NTLSTATUS_OBJECT_DEAD; + } + (void)PosixsrvCopyText(client->Process->CurrentDirectory, + sizeof(client->Process->CurrentDirectory), request->Path); + PosixsrvUnlockRegistry(); + return NTLSTATUS_SUCCESS; +} + +// Applies setuid semantics to synthetic POSIX credentials. +static KSTATUS PosixsrvSetUid(POSIXSRV_CLIENT* client, uint32_t userId) { + if (!client || !client->Process) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + POSIXSRV_PROCESS_RECORD* process = client->Process; + if (process->State != NTL_POSIX_PROCESS_STATE_RUNNING) { + status = NTLSTATUS_OBJECT_DEAD; + } else if (process->EffectiveUserId == 0) { + process->UserId = userId; + process->EffectiveUserId = userId; + } else if (userId != process->UserId) { + status = NTLSTATUS_ACCESS_DENIED; + } + PosixsrvUnlockRegistry(); + return status; +} + +// Applies setgid semantics to synthetic POSIX credentials. +static KSTATUS PosixsrvSetGid(POSIXSRV_CLIENT* client, uint32_t groupId) { + if (!client || !client->Process) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + POSIXSRV_PROCESS_RECORD* process = client->Process; + if (process->State != NTL_POSIX_PROCESS_STATE_RUNNING) { + status = NTLSTATUS_OBJECT_DEAD; + } else if (process->EffectiveUserId == 0) { + process->GroupId = groupId; + process->EffectiveGroupId = groupId; + } else if (groupId != process->GroupId) { + status = NTLSTATUS_ACCESS_DENIED; + } + PosixsrvUnlockRegistry(); + return status; +} + +// Changes one process group using the subset whose parent relationship is already authoritative. +static KSTATUS PosixsrvSetProcessGroup(POSIXSRV_CLIENT* client, + uint32_t targetPid, + uint32_t processGroupId) { + if (!client || !client->Process) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + + POSIXSRV_PROCESS_RECORD* caller = client->Process; + if (targetPid == 0) targetPid = caller->PosixPid; + POSIXSRV_PROCESS_RECORD* target = PosixsrvFindPosixProcessLocked(targetPid); + if (!target || target->State != NTL_POSIX_PROCESS_STATE_RUNNING) { + status = NTLSTATUS_NOT_FOUND; + goto done; + } + if (target != caller && target->ParentPosixPid != caller->PosixPid) { + status = NTLSTATUS_ACCESS_DENIED; + goto done; + } + if (target->SessionId != caller->SessionId || target->SessionId == target->PosixPid) { + status = NTLSTATUS_ACCESS_DENIED; + goto done; + } + + if (processGroupId == 0) processGroupId = target->PosixPid; + if (processGroupId != target->PosixPid && + !PosixsrvProcessGroupExistsLocked(target->SessionId, processGroupId)) { + status = NTLSTATUS_ACCESS_DENIED; + goto done; + } + target->ProcessGroupId = processGroupId; + +done: + PosixsrvUnlockRegistry(); + return status; +} + +// Creates a new session for the caller when it is not currently a process-group leader. +static KSTATUS PosixsrvCreateSession(POSIXSRV_CLIENT* client) { + if (!client || !client->Process) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = PosixsrvLockRegistry(); + if (!KSUCCESS(status)) return status; + POSIXSRV_PROCESS_RECORD* process = client->Process; + if (process->State != NTL_POSIX_PROCESS_STATE_RUNNING) { + status = NTLSTATUS_OBJECT_DEAD; + } else if (PosixsrvProcessGroupExistsLocked(process->SessionId, process->PosixPid)) { + status = NTLSTATUS_ACCESS_DENIED; + } else { + process->SessionId = process->PosixPid; + process->ProcessGroupId = process->PosixPid; + } + PosixsrvUnlockRegistry(); + return status; +} + +// Allocates one bounded connection-local transaction identifier for future spawn/exec work. +static KSTATUS PosixsrvBeginTransaction(POSIXSRV_CLIENT* client, + uint32_t kind, + uint32_t* transactionIdOut) { + if (!client || !transactionIdOut) return NTLSTATUS_INVALID_PARAMETER; + *transactionIdOut = 0; + if (kind != NTL_POSIX_TRANSACTION_SPAWN && kind != NTL_POSIX_TRANSACTION_EXEC) + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t slot = NTL_POSIX_TRANSACTION_CAPACITY; + for (uint32_t i = 0; i < NTL_POSIX_TRANSACTION_CAPACITY; ++i) { + if (client->Transactions[i].Id == 0) { + slot = i; + break; + } + } + if (slot == NTL_POSIX_TRANSACTION_CAPACITY) return NTLSTATUS_LIMIT_REACHED; + + uint32_t id = ++client->NextTransactionId; + if (id == 0) id = ++client->NextTransactionId; + client->Transactions[slot].Id = id; + client->Transactions[slot].Kind = kind; + ++client->ActiveTransactions; + *transactionIdOut = id; + return NTLSTATUS_SUCCESS; +} + +// Cancels one transaction owned by the current connection. +static KSTATUS PosixsrvCancelTransaction(POSIXSRV_CLIENT* client, uint32_t transactionId) { + if (!client || transactionId == 0) return NTLSTATUS_INVALID_PARAMETER; + for (uint32_t i = 0; i < NTL_POSIX_TRANSACTION_CAPACITY; ++i) { + if (client->Transactions[i].Id != transactionId) continue; + memset(&client->Transactions[i], 0, sizeof(client->Transactions[i])); + if (client->ActiveTransactions != 0) --client->ActiveTransactions; + return NTLSTATUS_SUCCESS; + } + return NTLSTATUS_NOT_FOUND; +} + +// Returns whether operation-specific bootstrap fields are all unused. +static int PosixsrvHasNoBootstrap(const NTL_POSIX_REQUEST* request) { + return request && request->BootstrapToken == 0 && request->BootstrapPosixPid == 0 && + request->BootstrapGeneration == 0; +} + +// Returns whether all P3 shared-attribute payload fields are unused. +static int PosixsrvHasNoAttributePayload(const NTL_POSIX_REQUEST* request) { + return request && request->TargetPosixPid == 0 && request->UserId == 0 && + request->GroupId == 0 && request->ProcessGroupId == 0 && + request->LookupKind == NTL_POSIX_LOOKUP_NONE && + request->OpenDescriptionType == 0 && request->OpenStatusFlags == 0 && + request->OpenDescriptionId == 0 && request->NameLength == 0 && + request->PathLength == 0 && request->Name[0] == '\0' && request->Path[0] == '\0'; +} + +// Returns whether one request carries no descriptor-inheritance payload. +static int PosixsrvHasNoFdInheritancePayload(const NTL_POSIX_REQUEST* request) { + if (!request || request->FdInheritanceToken != 0 || request->FdInheritanceIndex != 0 || + request->FdInheritanceCount != 0 || request->FdInheritanceFlags != 0) + return 0; + for (uint32_t i = 0; i < NTL_POSIX_FD_INHERIT_MAX; ++i) { + if (request->FdManifest[i].Descriptor != 0 || + request->FdManifest[i].DescriptorFlags != 0 || + request->FdManifest[i].OpenDescriptionId != 0) + return 0; + } + return 1; +} + +// Returns whether one request carries no transaction state. +static int PosixsrvHasNoTransaction(const NTL_POSIX_REQUEST* request) { + return request && request->TransactionId == 0 && + request->TransactionKind == NTL_POSIX_TRANSACTION_NONE; +} + +// Dispatches one validated bootstrap/process request and consumes transferred handles explicitly. +static KSTATUS PosixsrvDispatch(POSIXSRV_CLIENT* client, + const NTL_POSIX_REQUEST* request, + const SYS_PORT_MESSAGE_INFO* info, + SYS_HANDLE* handles) { + if (!client || !request || !info) return NTLSTATUS_INVALID_PARAMETER; + + uint32_t operation = request->Operation; + if (info->SenderProcessId != client->Connection.ClientProcessId || + info->SenderSecurityId != client->Connection.ClientSecurityId) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_ACCESS_DENIED, 0); + + if (request->Size != sizeof(*request) || request->Flags != NTL_POSIX_REQUEST_FLAG_NONE || + request->Reserved[0] != 0 || request->Reserved[1] != 0) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + if (request->Version != NTL_POSIX_PROTOCOL_VERSION) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_NOT_SUPPORTED, 0); + + if (operation == NTL_POSIX_OP_REGISTER_PROCESS) { + if (!PosixsrvHasNoTransaction(request) || !PosixsrvHasNoFdInheritancePayload(request) || + info->HandleCount != 1 || !handles) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + KSTATUS status = PosixsrvRegisterProcess(client, request, &handles[0]); + return PosixsrvReply(client, info->MessageId, operation, status, 0); + } + + if (!PosixsrvHasNoBootstrap(request)) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + + if (operation == NTL_POSIX_OP_CREATE_FD_INHERITANCE) { + if (!client->Process || client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING || + info->HandleCount != 0 || !PosixsrvHasNoTransaction(request) || + request->TargetPosixPid != 0 || request->UserId != 0 || request->GroupId != 0 || + request->ProcessGroupId != 0 || request->LookupKind != NTL_POSIX_LOOKUP_NONE || + request->OpenDescriptionType != 0 || request->OpenStatusFlags != 0 || + request->OpenDescriptionId != 0 || request->NameLength != 0 || request->PathLength != 0 || + request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + uint64_t token = 0; + KSTATUS status = PosixsrvCreateFdInheritance(client, request, &token); + NTL_POSIX_REPLY reply; + PosixsrvInitializeReply(client, &reply, operation, status, 0); + if (KSUCCESS(status)) { + reply.FdInheritanceToken = token; + reply.FdInheritanceCount = request->FdInheritanceCount; + } + KSTATUS replyStatus = PosixsrvSendReply(client, info->MessageId, &reply); + if (!KSUCCESS(replyStatus) && KSUCCESS(status)) + (void)PosixsrvCancelFdInheritance(client, token); + return replyStatus; + } + + if (operation == NTL_POSIX_OP_IMPORT_FD_INHERITANCE) { + if (!client->Process || client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING || + info->HandleCount != 0 || !PosixsrvHasNoTransaction(request) || + request->FdInheritanceToken == 0 || request->FdInheritanceCount != 0 || + request->FdInheritanceFlags != 0 || request->TargetPosixPid != 0 || + request->UserId != 0 || request->GroupId != 0 || request->ProcessGroupId != 0 || + request->LookupKind != NTL_POSIX_LOOKUP_NONE || request->OpenDescriptionType != 0 || + request->OpenStatusFlags != 0 || request->OpenDescriptionId != 0 || + request->NameLength != 0 || request->PathLength != 0 || request->Name[0] != '\0' || + request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + for (uint32_t i = 0; i < NTL_POSIX_FD_INHERIT_MAX; ++i) { + if (request->FdManifest[i].Descriptor != 0 || + request->FdManifest[i].DescriptorFlags != 0 || + request->FdManifest[i].OpenDescriptionId != 0) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + } + return PosixsrvReplyFdInheritance(client, info->MessageId, + request->FdInheritanceToken, + request->FdInheritanceIndex); + } + + if (operation == NTL_POSIX_OP_CANCEL_FD_INHERITANCE) { + if (!client->Process || client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING || + info->HandleCount != 0 || !PosixsrvHasNoTransaction(request) || + request->FdInheritanceToken == 0 || request->FdInheritanceIndex != 0 || + request->FdInheritanceCount != 0 || request->FdInheritanceFlags != 0 || + request->TargetPosixPid != 0 || request->UserId != 0 || request->GroupId != 0 || + request->ProcessGroupId != 0 || request->LookupKind != NTL_POSIX_LOOKUP_NONE || + request->OpenDescriptionType != 0 || request->OpenStatusFlags != 0 || + request->OpenDescriptionId != 0 || request->NameLength != 0 || request->PathLength != 0 || + request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + for (uint32_t i = 0; i < NTL_POSIX_FD_INHERIT_MAX; ++i) { + if (request->FdManifest[i].Descriptor != 0 || + request->FdManifest[i].DescriptorFlags != 0 || + request->FdManifest[i].OpenDescriptionId != 0) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + } + KSTATUS status = PosixsrvCancelFdInheritance(client, request->FdInheritanceToken); + return PosixsrvReply(client, info->MessageId, operation, status, 0); + } + + if (operation == NTL_POSIX_OP_ARM_FD_INHERITANCE) { + if (!client->Process || client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING || + info->HandleCount != 1 || !handles || !PosixsrvHasNoTransaction(request) || + request->FdInheritanceToken == 0 || request->FdInheritanceIndex != 0 || + request->FdInheritanceCount != 0 || request->FdInheritanceFlags != 0 || + request->TargetPosixPid != 0 || request->UserId != 0 || request->GroupId != 0 || + request->ProcessGroupId != 0 || request->LookupKind != NTL_POSIX_LOOKUP_NONE || + request->OpenDescriptionType != 0 || request->OpenStatusFlags != 0 || + request->OpenDescriptionId != 0 || request->NameLength != 0 || request->PathLength != 0 || + request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + for (uint32_t i = 0; i < NTL_POSIX_FD_INHERIT_MAX; ++i) { + if (request->FdManifest[i].Descriptor != 0 || + request->FdManifest[i].DescriptorFlags != 0 || + request->FdManifest[i].OpenDescriptionId != 0) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + } + KSTATUS status = PosixsrvArmFdInheritance(client, request->FdInheritanceToken, &handles[0]); + return PosixsrvReply(client, info->MessageId, operation, status, 0); + } + + if (!PosixsrvHasNoFdInheritancePayload(request)) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + + if (operation == NTL_POSIX_OP_CREATE_OPEN_DESCRIPTION) { + if (!client->Process || client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING || + !PosixsrvHasNoTransaction(request) || request->TargetPosixPid != 0 || + request->UserId != 0 || request->GroupId != 0 || request->ProcessGroupId != 0 || + request->LookupKind != NTL_POSIX_LOOKUP_NONE || request->OpenDescriptionId != 0 || + request->NameLength != 0 || request->PathLength != 0 || + request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + uint64_t descriptionId = 0; + NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared = (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)0; + KSTATUS status = PosixsrvCreateOpenDescription(client, request, handles, info->HandleCount, + &descriptionId, &shared); + KSTATUS replyStatus = PosixsrvReplyOpenDescription( + client, info->MessageId, operation, status, descriptionId, + request->OpenDescriptionType, request->OpenStatusFlags, shared); + if (!KSUCCESS(replyStatus) && KSUCCESS(status) && descriptionId != 0) + (void)PosixsrvReleaseOpenDescription(client, descriptionId); + return replyStatus; + } + + if (operation == NTL_POSIX_OP_CREATE_PIPE) { + if (!client->Process || client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING || + info->HandleCount != 0 || !PosixsrvHasNoTransaction(request) || + !PosixsrvHasNoAttributePayload(request)) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + + uint64_t readId = 0; + uint64_t writeId = 0; + KSTATUS status = PosixsrvCreatePipe(client, &readId, &writeId); + KSTATUS replyStatus = PosixsrvReplyPipe(client, info->MessageId, status, + readId, writeId); + if (!KSUCCESS(replyStatus) && KSUCCESS(status)) { + if (readId != 0) (void)PosixsrvReleaseOpenDescription(client, readId); + if (writeId != 0) (void)PosixsrvReleaseOpenDescription(client, writeId); + } + return replyStatus; + } + + if (info->HandleCount != 0) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + if (operation != NTL_POSIX_OP_RELEASE_OPEN_DESCRIPTION && + operation != NTL_POSIX_OP_SET_OPEN_STATUS && + (request->OpenDescriptionType != 0 || request->OpenStatusFlags != 0 || + request->OpenDescriptionId != 0)) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + + if (operation == NTL_POSIX_OP_QUERY) { + if (!PosixsrvHasNoTransaction(request) || !PosixsrvHasNoAttributePayload(request)) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_SUCCESS, 0); + } + + if (operation == NTL_POSIX_OP_BEGIN_TRANSACTION) { + if (request->TransactionId != 0 || request->TargetPosixPid != 0 || request->UserId != 0 || + request->GroupId != 0 || request->ProcessGroupId != 0 || + request->LookupKind != NTL_POSIX_LOOKUP_NONE || request->NameLength != 0 || + request->PathLength != 0 || request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + uint32_t transactionId = 0; + KSTATUS status = PosixsrvBeginTransaction(client, request->TransactionKind, &transactionId); + return PosixsrvReply(client, info->MessageId, operation, status, transactionId); + } + + if (operation == NTL_POSIX_OP_CANCEL_TRANSACTION) { + if (request->TransactionKind != NTL_POSIX_TRANSACTION_NONE || + request->TargetPosixPid != 0 || request->UserId != 0 || request->GroupId != 0 || + request->ProcessGroupId != 0 || request->LookupKind != NTL_POSIX_LOOKUP_NONE || + request->NameLength != 0 || request->PathLength != 0 || + request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + KSTATUS status = PosixsrvCancelTransaction(client, request->TransactionId); + return PosixsrvReply(client, info->MessageId, operation, status, + KSUCCESS(status) ? request->TransactionId : 0); + } + + if (!client->Process || client->Process->State != NTL_POSIX_PROCESS_STATE_RUNNING) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_NOT_FOUND, 0); + if (!PosixsrvHasNoTransaction(request)) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + + if (operation == NTL_POSIX_OP_RELEASE_OPEN_DESCRIPTION) { + if (request->OpenDescriptionId == 0 || request->OpenDescriptionType != 0 || + request->OpenStatusFlags != 0 || request->TargetPosixPid != 0 || + request->UserId != 0 || request->GroupId != 0 || request->ProcessGroupId != 0 || + request->LookupKind != NTL_POSIX_LOOKUP_NONE || request->NameLength != 0 || + request->PathLength != 0 || request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + KSTATUS status = PosixsrvReleaseOpenDescription(client, request->OpenDescriptionId); + return PosixsrvReply(client, info->MessageId, operation, status, 0); + } + + if (operation == NTL_POSIX_OP_SET_OPEN_STATUS) { + if (request->OpenDescriptionId == 0 || request->OpenDescriptionType != 0 || + request->TargetPosixPid != 0 || request->UserId != 0 || request->GroupId != 0 || + request->ProcessGroupId != 0 || request->LookupKind != NTL_POSIX_LOOKUP_NONE || + request->NameLength != 0 || request->PathLength != 0 || + request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + uint32_t statusFlags = 0; + uint32_t type = 0; + KSTATUS status = PosixsrvSetOpenStatus(client, request->OpenDescriptionId, + request->OpenStatusFlags, &statusFlags, &type); + return PosixsrvReplyOpenDescription(client, info->MessageId, operation, status, + request->OpenDescriptionId, type, statusFlags, + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)0); + } + + if (operation == NTL_POSIX_OP_GET_PROCESS_ATTRIBUTES) { + if (!PosixsrvHasNoAttributePayload(request)) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_SUCCESS, 0); + } + + if (operation == NTL_POSIX_OP_SET_CURRENT_DIRECTORY) { + if (request->TargetPosixPid != 0 || request->UserId != 0 || request->GroupId != 0 || + request->ProcessGroupId != 0 || request->LookupKind != NTL_POSIX_LOOKUP_NONE || + request->NameLength != 0 || request->Name[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + KSTATUS status = PosixsrvSetCurrentDirectory(client, request); + return PosixsrvReply(client, info->MessageId, operation, status, 0); + } + + if (operation == NTL_POSIX_OP_SET_UID) { + if (request->TargetPosixPid != 0 || request->GroupId != 0 || request->ProcessGroupId != 0 || + request->LookupKind != NTL_POSIX_LOOKUP_NONE || request->NameLength != 0 || + request->PathLength != 0 || request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + KSTATUS status = PosixsrvSetUid(client, request->UserId); + return PosixsrvReply(client, info->MessageId, operation, status, 0); + } + + if (operation == NTL_POSIX_OP_SET_GID) { + if (request->TargetPosixPid != 0 || request->UserId != 0 || request->ProcessGroupId != 0 || + request->LookupKind != NTL_POSIX_LOOKUP_NONE || request->NameLength != 0 || + request->PathLength != 0 || request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + KSTATUS status = PosixsrvSetGid(client, request->GroupId); + return PosixsrvReply(client, info->MessageId, operation, status, 0); + } + + if (operation == NTL_POSIX_OP_SET_PROCESS_GROUP) { + if (request->UserId != 0 || request->GroupId != 0 || + request->LookupKind != NTL_POSIX_LOOKUP_NONE || request->NameLength != 0 || + request->PathLength != 0 || request->Name[0] != '\0' || request->Path[0] != '\0') + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + KSTATUS status = PosixsrvSetProcessGroup(client, request->TargetPosixPid, + request->ProcessGroupId); + return PosixsrvReply(client, info->MessageId, operation, status, 0); + } + + if (operation == NTL_POSIX_OP_CREATE_SESSION) { + if (!PosixsrvHasNoAttributePayload(request)) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + KSTATUS status = PosixsrvCreateSession(client); + return PosixsrvReply(client, info->MessageId, operation, status, 0); + } + + if (operation == NTL_POSIX_OP_LOOKUP_USER) { + if (request->TargetPosixPid != 0 || request->ProcessGroupId != 0 || request->GroupId != 0 || + request->PathLength != 0 || request->Path[0] != '\0' || + (request->LookupKind != NTL_POSIX_LOOKUP_BY_ID && + request->LookupKind != NTL_POSIX_LOOKUP_BY_NAME) || + (request->LookupKind == NTL_POSIX_LOOKUP_BY_ID && + (request->NameLength != 0 || request->Name[0] != '\0')) || + (request->LookupKind == NTL_POSIX_LOOKUP_BY_NAME && !PosixsrvValidateLookupName(request))) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + const POSIXSRV_USER_RECORD* user = + PosixsrvFindUser(request->LookupKind, request->UserId, request->Name); + return PosixsrvReplyUser(client, info->MessageId, operation, + user ? NTLSTATUS_SUCCESS : NTLSTATUS_NOT_FOUND, user); + } + + if (operation == NTL_POSIX_OP_LOOKUP_GROUP) { + if (request->TargetPosixPid != 0 || request->ProcessGroupId != 0 || request->UserId != 0 || + request->PathLength != 0 || request->Path[0] != '\0' || + (request->LookupKind != NTL_POSIX_LOOKUP_BY_ID && + request->LookupKind != NTL_POSIX_LOOKUP_BY_NAME) || + (request->LookupKind == NTL_POSIX_LOOKUP_BY_ID && + (request->NameLength != 0 || request->Name[0] != '\0')) || + (request->LookupKind == NTL_POSIX_LOOKUP_BY_NAME && !PosixsrvValidateLookupName(request))) + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_INVALID_PARAMETER, 0); + const POSIXSRV_GROUP_RECORD* group = + PosixsrvFindGroup(request->LookupKind, request->GroupId, request->Name); + return PosixsrvReplyGroup(client, info->MessageId, operation, + group ? NTLSTATUS_SUCCESS : NTLSTATUS_NOT_FOUND, group); + } + + return PosixsrvReply(client, info->MessageId, operation, NTLSTATUS_NOT_SUPPORTED, 0); +} + +// Closes every unconsumed capability delivered with one client request. +static void PosixsrvCloseReceivedHandles(SYS_HANDLE* handles, uint32_t count) { + if (!handles) return; + for (uint32_t i = 0; i < count; ++i) { + if (handles[i] != SYS_HANDLE_INVALID) + (void)SysCloseHandle(handles[i]); + } +} + +// Serves one client until disconnect and drops connection-owned transactions on exit. +static void PosixsrvClientThread(uintptr_t argument) { + POSIXSRV_CLIENT* client = (POSIXSRV_CLIENT*)argument; + if (!client) { + SysExitThread(1); + return; + } + PosixsrvSetCurrentThreadName("posixsrv.client"); + + for (;;) { + NTL_POSIX_REQUEST request; + SYS_PORT_MESSAGE_INFO info; + SYS_HANDLE handles[SYS_PORT_MAX_TRANSFER_HANDLES]; + memset(&request, 0, sizeof(request)); + memset(&info, 0, sizeof(info)); + for (uint32_t i = 0; i < SYS_PORT_MAX_TRANSFER_HANDLES; ++i) + handles[i] = SYS_HANDLE_INVALID; + info.Size = sizeof(info); + + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = client->Endpoint; + receive.Flags = SYS_PORT_FLAG_NONE; + receive.Payload = &request; + receive.PayloadCapacity = sizeof(request); + receive.HandlesOut = handles; + receive.HandleCapacity = SYS_PORT_MAX_TRANSFER_HANDLES; + receive.InfoOut = &info; + receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysReceivePort(&receive); + if (status == NTLSTATUS_PORT_DISCONNECTED || !KSUCCESS(status)) break; + + if (info.Type != SYS_PORT_MESSAGE_REQUEST || info.PayloadLength != sizeof(request)) { + if (info.Type == SYS_PORT_MESSAGE_REQUEST) + (void)PosixsrvReply(client, info.MessageId, request.Operation, + NTLSTATUS_INVALID_PARAMETER, 0); + PosixsrvCloseReceivedHandles(handles, info.HandleCount); + continue; + } + status = PosixsrvDispatch(client, &request, &info, handles); + PosixsrvCloseReceivedHandles(handles, info.HandleCount); + if (!KSUCCESS(status)) break; + } + + PosixsrvDetachClientProcess(client); + (void)SysDisconnectPort(client->Endpoint); + (void)SysCloseHandle(client->Endpoint); + free(client); + SysExitThread(0); +} + +// Accepts public connections and gives each kernel-stamped client identity its own worker. +static void PosixsrvListenerThread(uintptr_t argument) { + SYS_HANDLE listener = (SYS_HANDLE)argument; + PosixsrvSetCurrentThreadName("posixsrv.listen"); + for (;;) { + POSIXSRV_CLIENT* client = (POSIXSRV_CLIENT*)calloc(1, sizeof(*client)); + if (!client) { + SysExitThread(1); + return; + } + client->Endpoint = SYS_HANDLE_INVALID; + client->Connection.Size = sizeof(client->Connection); + + SYS_ACCEPT_PORT_REQUEST accept; + memset(&accept, 0, sizeof(accept)); + accept.Size = sizeof(accept); + accept.ListenerHandle = listener; + accept.Flags = SYS_PORT_ACCEPT_FLAG_NONE; + accept.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + accept.EndpointHandleOut = &client->Endpoint; + accept.ConnectionInfoOut = &client->Connection; + KSTATUS status = SysAcceptPort(&accept); + if (!KSUCCESS(status)) { + free(client); + break; + } + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)PosixsrvClientThread; + create.Argument = (uintptr_t)client; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + status = SysCreateThread(&create); + if (!KSUCCESS(status)) { + (void)SysDisconnectPort(client->Endpoint); + (void)SysCloseHandle(client->Endpoint); + free(client); + } + } + SysExitThread(0); +} + +// Reports READY and transfers the public listener for init-owned RSSRV namespace publication. +static KSTATUS PosixsrvSendReady(SYS_HANDLE controlEndpoint, SYS_HANDLE publicListener) { + if (controlEndpoint == SYS_HANDLE_INVALID || publicListener == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + NTL_SERVICE_CONTROL_MESSAGE message; + memset(&message, 0, sizeof(message)); + message.Size = sizeof(message); + message.Version = NTL_SERVICE_CONTROL_VERSION; + message.Operation = NTL_SERVICE_CONTROL_READY; + message.ExportCount = 1u; + message.Status = NTLSTATUS_SUCCESS; + + SYS_PORT_HANDLE_TRANSFER transfer; + memset(&transfer, 0, sizeof(transfer)); + transfer.Handle = publicListener; + transfer.DesiredRights = SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT; + transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + + SYS_SEND_PORT_REQUEST send; + memset(&send, 0, sizeof(send)); + send.Size = sizeof(send); + send.EndpointHandle = controlEndpoint; + send.Flags = SYS_PORT_FLAG_NONE; + send.Payload = &message; + send.PayloadLength = sizeof(message); + send.Handles = &transfer; + send.HandleCount = 1u; + send.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + return SysSendPort(&send); +} + +// Waits for init to stop the service or close the private control connection. +static void PosixsrvControlLoop(SYS_HANDLE endpoint) { + for (;;) { + NTL_SERVICE_CONTROL_MESSAGE message; + SYS_PORT_MESSAGE_INFO info; + memset(&message, 0, sizeof(message)); + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = endpoint; + receive.Payload = &message; + receive.PayloadCapacity = sizeof(message); + receive.InfoOut = &info; + receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysReceivePort(&receive); + if (status == NTLSTATUS_PORT_DISCONNECTED || !KSUCCESS(status)) return; + if (info.Type != SYS_PORT_MESSAGE_NOTIFICATION || info.HandleCount != 0 || + info.PayloadLength != sizeof(message) || message.Size != sizeof(message) || + message.Version != NTL_SERVICE_CONTROL_VERSION || message.ExportCount != 0 || + message.Reserved[0] != 0 || message.Reserved[1] != 0 || message.Reserved[2] != 0) + continue; + if (message.Operation == NTL_SERVICE_CONTROL_STOP) return; + } +} + +// Starts the POSIX policy service while keeping stable POSIX process identity out of the kernel. +void _start(void) { + KSTATUS status = NtlInitializeExceptionRuntime(); + if (!KSUCCESS(status)) PosixsrvExit(1); + PosixsrvSetCurrentThreadName("posixsrv.main"); + + status = PosixsrvValidateSecurity(); + if (!KSUCCESS(status)) PosixsrvExit(2); + + status = SysCreateMutex(&gProcessRegistryMutex); + if (!KSUCCESS(status)) PosixsrvExit(3); + + SYS_HANDLE controlListener = SysGetStartupCapability(SYS_STARTUP_CAP_SERVICE_CONTROL); + if (controlListener == SYS_HANDLE_INVALID) PosixsrvExit(4); + + SYS_HANDLE publicListener = SYS_HANDLE_INVALID; + status = PosixsrvCreateListener(&publicListener); + if (!KSUCCESS(status)) PosixsrvExit(5); + + SYS_HANDLE controlEndpoint = SYS_HANDLE_INVALID; + status = PosixsrvAcceptControl(controlListener, &controlEndpoint); + (void)SysCloseHandle(controlListener); + if (!KSUCCESS(status)) PosixsrvExit(6); + + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)PosixsrvListenerThread; + create.Argument = (uintptr_t)publicListener; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + status = SysCreateThread(&create); + if (!KSUCCESS(status)) PosixsrvExit(7); + + status = PosixsrvSendReady(controlEndpoint, publicListener); + if (!KSUCCESS(status)) PosixsrvExit(8); + + PosixsrvControlLoop(controlEndpoint); + (void)SysDisconnectPort(controlEndpoint); + (void)SysCloseHandle(controlEndpoint); + (void)SysCloseHandle(publicListener); + (void)SysCloseHandle(gProcessRegistryMutex); + gProcessRegistryMutex = SYS_HANDLE_INVALID; + PosixsrvExit(0); +} diff --git a/code/user/system/rssrv/console.c b/code/user/system/rssrv/console.c new file mode 100644 index 0000000..a088c6c --- /dev/null +++ b/code/user/system/rssrv/console.c @@ -0,0 +1,433 @@ +// RSSRV native console. One logical console owns text output policy and consumes RSSRV logical input. + +#include "rssrv_internal.h" +#include +#include + +#define RSSRV_EVENT_MANUAL_RESET 1u +#define RSSRV_CONSOLE_KEY_QUEUE_MASK (RSSRV_CONSOLE_KEY_QUEUE_SIZE - 1u) + +typedef struct RSSRV_CONSOLE_SESSION { + RSSRV_CONSOLE_STATE* Console; + SYS_HANDLE Endpoint; +} RSSRV_CONSOLE_SESSION; + +// Acquires the console mutation lock. +static KSTATUS RssrvConsoleLock(RSSRV_CONSOLE_STATE* console) { + if (!console || console->Mutex == SYS_HANDLE_INVALID) return NTLSTATUS_INVALID_PARAMETER; + return SysWait(console->Mutex, SYS_WAIT_FLAG_NONE); +} + +// Releases the console mutation lock. +static void RssrvConsoleUnlock(RSSRV_CONSOLE_STATE* console) { + if (console && console->Mutex != SYS_HANDLE_INVALID) (void)SysReleaseMutex(console->Mutex); +} + + +// Refreshes the console-input availability event while the console mutex is held. +static void RssrvConsoleRefreshInputEventLocked(RSSRV_CONSOLE_STATE* console) { + if (console->KeyHead != console->KeyTail) (void)SysSignalEvent(console->KeyEvent); + else (void)SysClearEvent(console->KeyEvent); +} + +// Receives one focused logical key record from Session 0. +static void RssrvConsoleRouteKey(void* context, const INPUT_KEY_RECORD* record) { + RSSRV_CONSOLE_STATE* console = (RSSRV_CONSOLE_STATE*)context; + if (!console || !record || !KSUCCESS(RssrvConsoleLock(console))) return; + uint32_t next = (console->KeyTail + 1u) & RSSRV_CONSOLE_KEY_QUEUE_MASK; + if (next != console->KeyHead) { + console->KeyQueue[console->KeyTail] = *record; + console->KeyTail = next; + RssrvConsoleRefreshInputEventLocked(console); + } + RssrvConsoleUnlock(console); +} + +// Receives pointer routing for the console target. The text console does not consume pointer events yet. +static void RssrvConsoleRouteMouse(void* context, const INPUT_MOUSE_EVENT* event, int32_t x, int32_t y) { + (void)context; + (void)event; + (void)x; + (void)y; +} + +// Reads or peeks one keyboard record routed specifically to this console. +static KSTATUS RssrvConsoleReadInput(RSSRV_CONSOLE_STATE* console, + int peek, + uint64_t timeoutMilliseconds, + INPUT_KEY_RECORD* recordOut) { + if (!console || !recordOut) return NTLSTATUS_INVALID_PARAMETER; + for (;;) { + KSTATUS status = RssrvConsoleLock(console); + if (!KSUCCESS(status)) return status; + if (console->KeyHead != console->KeyTail) { + *recordOut = console->KeyQueue[console->KeyHead]; + if (!peek) { + console->KeyHead = (console->KeyHead + 1u) & RSSRV_CONSOLE_KEY_QUEUE_MASK; + RssrvConsoleRefreshInputEventLocked(console); + } + RssrvConsoleUnlock(console); + return NTLSTATUS_SUCCESS; + } + RssrvConsoleUnlock(console); + if (timeoutMilliseconds == 0) return NTLSTATUS_WOULD_BLOCK; + status = SysWaitSingle(console->KeyEvent, SYS_WAIT_FLAG_NONE, timeoutMilliseconds); + if (!KSUCCESS(status)) return status; + } +} + +// Discards all keyboard records already routed to this console. +static KSTATUS RssrvConsoleFlushInput(RSSRV_CONSOLE_STATE* console, uint32_t* discardedOut) { + if (!console) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvConsoleLock(console); + if (!KSUCCESS(status)) return status; + uint32_t discarded = (console->KeyTail - console->KeyHead) & RSSRV_CONSOLE_KEY_QUEUE_MASK; + console->KeyHead = console->KeyTail; + RssrvConsoleRefreshInputEventLocked(console); + RssrvConsoleUnlock(console); + if (discardedOut) *discardedOut = discarded; + return NTLSTATUS_SUCCESS; +} + +// Records one successful visible console mutation while holding the console lock. +static void RssrvConsoleChangedLocked(RSSRV_CONSOLE_STATE* console) { + console->Generation++; + if (console->Generation == 0) console->Generation = 1; +} + +// Writes one console chunk in serialized order. +static KSTATUS RssrvConsoleWrite(RSSRV_CONSOLE_STATE* console, + const char* data, + uint32_t length, + uint32_t* writtenOut) { + if (writtenOut) *writtenOut = 0; + if (!console || length > NTL_CONSOLE_WRITE_MAX || (length && !data)) + return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvConsoleLock(console); + if (!KSUCCESS(status)) return status; + status = RssrvDisplayWriteConsole(console->Display, data, length); + if (KSUCCESS(status) && length) RssrvConsoleChangedLocked(console); + RssrvConsoleUnlock(console); + if (KSUCCESS(status) && writtenOut) *writtenOut = length; + return status; +} + +// Queries dimensions from the backing display and snapshots RSSRV's console generation. +static KSTATUS RssrvConsoleQueryInfo(RSSRV_CONSOLE_STATE* console, NTL_CONSOLE_INFO* infoOut) { + if (!console || !infoOut) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvConsoleLock(console); + if (!KSUCCESS(status)) return status; + + SYS_CONSOLE_INFO consoleInfo; + SYS_DISPLAY_INFO displayInfo; + memset(&consoleInfo, 0, sizeof(consoleInfo)); + memset(&displayInfo, 0, sizeof(displayInfo)); + status = RssrvDisplayQueryConsoleInfo(console->Display, &consoleInfo); + if (KSUCCESS(status)) status = RssrvDisplayQueryInfo(console->Display, &displayInfo); + if (KSUCCESS(status)) { + memset(infoOut, 0, sizeof(*infoOut)); + infoOut->Size = sizeof(*infoOut); + infoOut->Flags = NTL_CONSOLE_FLAG_NONE; + infoOut->Columns = consoleInfo.Columns; + infoOut->Rows = consoleInfo.Rows; + infoOut->CursorX = consoleInfo.CursorX; + infoOut->CursorY = consoleInfo.CursorY; + infoOut->Attribute = consoleInfo.Attribute; + infoOut->PixelWidth = displayInfo.Mode.Width; + infoOut->PixelHeight = displayInfo.Mode.Height; + infoOut->Generation = console->Generation; + } + RssrvConsoleUnlock(console); + return status; +} + +// Sends one fixed console reply. +static KSTATUS RssrvConsoleReply(SYS_HANDLE endpoint, + uint32_t messageId, + uint32_t operation, + KSTATUS status, + uint32_t written, + const INPUT_KEY_RECORD* input, + const NTL_CONSOLE_INFO* consoleInfo) { + NTL_CONSOLE_REPLY reply; + memset(&reply, 0, sizeof(reply)); + reply.Size = sizeof(reply); + reply.Version = NTL_CONSOLE_PROTOCOL_VERSION; + reply.Operation = operation; + reply.Flags = NTL_CONSOLE_FLAG_NONE; + reply.Status = status; + reply.Written = written; + if (input) reply.Input = *input; + if (consoleInfo) reply.Info = *consoleInfo; + + SYS_REPLY_PORT_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.EndpointHandle = endpoint; + request.MessageId = messageId; + request.Flags = SYS_PORT_FLAG_NONE; + request.Payload = &reply; + request.PayloadLength = sizeof(reply); + return SysReplyPort(&request); +} + +// Handles one mutation request that must be serialized through the console lock. +static KSTATUS RssrvConsoleMutate(RSSRV_CONSOLE_STATE* console, + const NTL_CONSOLE_REQUEST* request, + uint32_t* writtenOut) { + if (writtenOut) *writtenOut = 0; + KSTATUS status = RssrvConsoleLock(console); + if (!KSUCCESS(status)) return status; + + size_t written = 0; + switch (request->Operation) { + case NTL_CONSOLE_OP_SET_CURSOR: + status = RssrvDisplaySetConsoleCursor(console->Display, + request->Data.Cursor.X, + request->Data.Cursor.Y); + break; + case NTL_CONSOLE_OP_FILL_CHARACTER: + if (request->Data.FillCharacter.Reserved[0] || request->Data.FillCharacter.Reserved[1] || + request->Data.FillCharacter.Reserved[2]) { + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + status = RssrvDisplayFillConsoleCharacter(console->Display, + request->Data.FillCharacter.Character, + request->Data.FillCharacter.X, + request->Data.FillCharacter.Y, + request->Data.FillCharacter.Length, + &written); + break; + case NTL_CONSOLE_OP_FILL_ATTRIBUTE: + if (request->Data.FillAttribute.Reserved != 0) { + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + status = RssrvDisplayFillConsoleAttribute(console->Display, + request->Data.FillAttribute.Attribute, + request->Data.FillAttribute.X, + request->Data.FillAttribute.Y, + request->Data.FillAttribute.Length, + &written); + break; + case NTL_CONSOLE_OP_PUT_CHARACTER: + if (request->Data.PutCharacter.Reserved[0] || request->Data.PutCharacter.Reserved[1] || + request->Data.PutCharacter.Reserved[2]) { + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + status = RssrvDisplayPutConsoleCharacter(console->Display, + request->Data.PutCharacter.Character, + request->Data.PutCharacter.X, + request->Data.PutCharacter.Y, + request->Data.PutCharacter.Foreground, + request->Data.PutCharacter.Background); + break; + case NTL_CONSOLE_OP_CLEAR: + status = RssrvDisplayClearConsole(console->Display, request->Data.Clear.Color); + break; + default: + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + if (KSUCCESS(status)) RssrvConsoleChangedLocked(console); + RssrvConsoleUnlock(console); + if (writtenOut) *writtenOut = written > UINT32_MAX ? UINT32_MAX : (uint32_t)written; + return status; +} + +// Serves one connected logical-console client. +static void RssrvConsoleSessionThread(uintptr_t argument) { + RSSRV_CONSOLE_SESSION* session = (RSSRV_CONSOLE_SESSION*)argument; + if (!session || !session->Console) { + if (session) free(session); + SysExitThread(1); + } + RSSRV_CONSOLE_STATE* console = session->Console; + SYS_HANDLE endpoint = session->Endpoint; + free(session); + RssrvSetCurrentThreadName("rssrv.console.session"); + + for (;;) { + NTL_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = endpoint; + receive.Flags = SYS_PORT_FLAG_NONE; + receive.Payload = &request; + receive.PayloadCapacity = sizeof(request); + receive.InfoOut = &info; + receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysReceivePort(&receive); + if (status == NTLSTATUS_PORT_DISCONNECTED) break; + if (!KSUCCESS(status)) break; + + if (info.Type != SYS_PORT_MESSAGE_REQUEST || info.PayloadLength != sizeof(request) || + info.HandleCount != 0 || request.Size != sizeof(request) || + request.Version != NTL_CONSOLE_PROTOCOL_VERSION || request.Flags != NTL_CONSOLE_FLAG_NONE) { + (void)RssrvConsoleReply(endpoint, info.MessageId, request.Operation, + NTLSTATUS_INVALID_PARAMETER, 0, 0, 0); + continue; + } + + uint32_t written = 0; + INPUT_KEY_RECORD input; + NTL_CONSOLE_INFO consoleInfo; + memset(&input, 0, sizeof(input)); + memset(&consoleInfo, 0, sizeof(consoleInfo)); + switch (request.Operation) { + case NTL_CONSOLE_OP_WRITE: + if (request.Data.Write.Length > NTL_CONSOLE_WRITE_MAX) status = NTLSTATUS_INVALID_PARAMETER; + else status = RssrvConsoleWrite(console, request.Data.Write.Data, + request.Data.Write.Length, &written); + (void)RssrvConsoleReply(endpoint, info.MessageId, request.Operation, + status, written, 0, 0); + break; + case NTL_CONSOLE_OP_QUERY_INFO: + status = RssrvConsoleQueryInfo(console, &consoleInfo); + (void)RssrvConsoleReply(endpoint, info.MessageId, request.Operation, + status, 0, 0, KSUCCESS(status) ? &consoleInfo : 0); + break; + case NTL_CONSOLE_OP_SET_CURSOR: + case NTL_CONSOLE_OP_FILL_CHARACTER: + case NTL_CONSOLE_OP_FILL_ATTRIBUTE: + case NTL_CONSOLE_OP_PUT_CHARACTER: + case NTL_CONSOLE_OP_CLEAR: + status = RssrvConsoleMutate(console, &request, &written); + (void)RssrvConsoleReply(endpoint, info.MessageId, request.Operation, + status, written, 0, 0); + break; + case NTL_CONSOLE_OP_READ_INPUT: + status = RssrvConsoleReadInput(console, 0, + request.TimeoutMilliseconds, &input); + (void)RssrvConsoleReply(endpoint, info.MessageId, request.Operation, + status, 0, KSUCCESS(status) ? &input : 0, 0); + break; + case NTL_CONSOLE_OP_PEEK_INPUT: + status = RssrvConsoleReadInput(console, 1, + request.TimeoutMilliseconds, &input); + (void)RssrvConsoleReply(endpoint, info.MessageId, request.Operation, + status, 0, KSUCCESS(status) ? &input : 0, 0); + break; + case NTL_CONSOLE_OP_FLUSH_INPUT: + status = RssrvConsoleFlushInput(console, &written); + (void)RssrvConsoleReply(endpoint, info.MessageId, request.Operation, + status, written, 0, 0); + break; + default: + (void)RssrvConsoleReply(endpoint, info.MessageId, request.Operation, + NTLSTATUS_INVALID_PARAMETER, 0, 0, 0); + break; + } + } + + (void)SysCloseHandle(endpoint); + SysExitThread(0); +} + +// Accepts console sessions and gives each connection one worker thread. +static void RssrvConsoleListenerThread(uintptr_t argument) { + RSSRV_CONSOLE_STATE* console = (RSSRV_CONSOLE_STATE*)argument; + RssrvSetCurrentThreadName("rssrv.console.listen"); + if (!console) SysExitThread(1); + + for (;;) { + SYS_HANDLE endpoint = SYS_HANDLE_INVALID; + SYS_ACCEPT_PORT_REQUEST accept; + memset(&accept, 0, sizeof(accept)); + accept.Size = sizeof(accept); + accept.ListenerHandle = console->Listener; + accept.Flags = SYS_PORT_ACCEPT_FLAG_NONE; + accept.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + accept.EndpointHandleOut = &endpoint; + KSTATUS status = SysAcceptPort(&accept); + if (!KSUCCESS(status)) RssrvExit(16); + + RSSRV_CONSOLE_SESSION* session = (RSSRV_CONSOLE_SESSION*)malloc(sizeof(*session)); + if (!session) { + (void)SysCloseHandle(endpoint); + continue; + } + session->Console = console; + session->Endpoint = endpoint; + + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)RssrvConsoleSessionThread; + create.Argument = (uintptr_t)session; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + KSTATUS createStatus = SysCreateThread(&create); + if (!KSUCCESS(createStatus)) { + free(session); + (void)SysCloseHandle(endpoint); + } + } +} + +// Initializes Session 0's native console object and registers its focused-input target. +KSTATUS RssrvConsoleInitialize(RSSRV_CONSOLE_STATE* console, + RSSRV_NAMESPACE* namespaceState, + RSSRV_DISPLAY_STATE* display, + RSSRV_SESSION_STATE* session) { + if (!console || !namespaceState || !display || !session) + return NTLSTATUS_INVALID_PARAMETER; + memset(console, 0, sizeof(*console)); + console->Mutex = SYS_HANDLE_INVALID; + console->KeyEvent = SYS_HANDLE_INVALID; + console->Listener = SYS_HANDLE_INVALID; + console->Namespace = namespaceState; + console->Display = display; + console->Session = session; + console->Generation = 1; + + KSTATUS status = SysCreateMutex(&console->Mutex); + if (!KSUCCESS(status)) return status; + status = SysCreateEvent(RSSRV_EVENT_MANUAL_RESET, 0, &console->KeyEvent); + if (!KSUCCESS(status)) return status; + status = RssrvSessionRegisterInputTarget(session, &console->InputTarget, + RSSRV_INPUT_TARGET_KIND_CONSOLE, console, + RssrvConsoleRouteKey, RssrvConsoleRouteMouse); + if (!KSUCCESS(status)) return status; + return RssrvSessionActivateInputTarget(session, &console->InputTarget); +} + +// Publishes /Services/Console and starts its client listener. +KSTATUS RssrvConsoleStart(RSSRV_CONSOLE_STATE* console) { + if (!console || !console->Namespace || !console->Display || !console->Session || + console->Listener != SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + SYS_CREATE_PORT_REQUEST createPort; + memset(&createPort, 0, sizeof(createPort)); + createPort.Size = sizeof(createPort); + createPort.Flags = SYS_PORT_FLAG_NONE; + createPort.Backlog = RSSRV_LISTENER_BACKLOG; + createPort.HandleOut = &console->Listener; + KSTATUS status = SysCreatePort(&createPort); + if (!KSUCCESS(status)) return status; + + status = RssrvNamespaceBindOwned(console->Namespace, + NTL_CONSOLE_SERVICE_PATH, + sizeof(NTL_CONSOLE_SERVICE_PATH) - 1u, + NTL_RESOURCE_KIND_CAPABILITY, + console->Listener, + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT); + if (!KSUCCESS(status)) return status; + + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)RssrvConsoleListenerThread; + create.Argument = (uintptr_t)console; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + return SysCreateThread(&create); +} diff --git a/code/user/system/rssrv/display.c b/code/user/system/rssrv/display.c new file mode 100644 index 0000000..6168a5f --- /dev/null +++ b/code/user/system/rssrv/display.c @@ -0,0 +1,126 @@ +// RSSRV display authority. This component owns session display metadata, modeset, and transitional console authority. + +#include "rssrv_internal.h" +#include + +// Initializes RSSRV display authority from the attenuated bootstrap manager capability. +KSTATUS RssrvDisplayInitialize(RSSRV_DISPLAY_STATE* display, RSSRV_NAMESPACE* namespaceState, SYS_HANDLE manager) { + if (!display || !namespaceState || manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + memset(display, 0, sizeof(*display)); + display->Manager = manager; + display->Namespace = namespaceState; + return NTLSTATUS_SUCCESS; +} + +// Publishes read-only display metadata while keeping mode and transitional console authority private to RSSRV. +KSTATUS RssrvDisplayPublishDevice(RSSRV_DISPLAY_STATE* display) { + if (!display || !display->Namespace || display->Manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + SYS_HANDLE deviceHandle = SYS_HANDLE_INVALID; + SYS_DERIVE_HANDLE_REQUEST derive; + memset(&derive, 0, sizeof(derive)); + derive.Size = sizeof(derive); + derive.SourceHandle = display->Manager; + derive.DesiredRights = SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_DISPLAY_MANAGER_RIGHT_ENUMERATE; + derive.DerivedHandleOut = &deviceHandle; + KSTATUS status = SysDeriveHandle(&derive); + if (!KSUCCESS(status)) return status; + + status = RssrvNamespaceBindOwned(display->Namespace, + "/Device/Display0", + sizeof("/Device/Display0") - 1u, + NTL_RESOURCE_KIND_DEVICE, + deviceHandle, + SYS_RIGHT_QUERY | SYS_DISPLAY_MANAGER_RIGHT_ENUMERATE); + if (!KSUCCESS(status)) (void)SysCloseHandle(deviceHandle); + return status; +} + +// Queries the primary display through RSSRV's delegated manager capability. +KSTATUS RssrvDisplayQueryInfo(RSSRV_DISPLAY_STATE* display, SYS_DISPLAY_INFO* infoOut) { + if (!display || display->Manager == SYS_HANDLE_INVALID || !infoOut) + return NTLSTATUS_INVALID_PARAMETER; + return SysQueryDisplayManagerInfo(display->Manager, infoOut); +} + +// Writes text through RSSRV's delegated legacy-console bridge. +KSTATUS RssrvDisplayWriteConsole(RSSRV_DISPLAY_STATE* display, const char* data, size_t length) { + if (!display || display->Manager == SYS_HANDLE_INVALID || (length && !data)) + return NTLSTATUS_INVALID_PARAMETER; + SYS_WRITE_CONSOLE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Text = SysSvFromParts(data, length); + return SysWriteDisplayManagerConsole(display->Manager, &request); +} + +// Queries the kernel-backed console state during the RSSRV migration period. +KSTATUS RssrvDisplayQueryConsoleInfo(RSSRV_DISPLAY_STATE* display, SYS_CONSOLE_INFO* infoOut) { + if (!display || display->Manager == SYS_HANDLE_INVALID || !infoOut) + return NTLSTATUS_INVALID_PARAMETER; + return SysQueryDisplayManagerConsoleInfo(display->Manager, infoOut); +} + +// Updates the kernel-backed console cursor through delegated authority. +KSTATUS RssrvDisplaySetConsoleCursor(RSSRV_DISPLAY_STATE* display, int x, int y) { + if (!display || display->Manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + return SysSetDisplayManagerConsoleCursor(display->Manager, x, y); +} + +// Fills kernel-backed console characters through delegated authority. +KSTATUS RssrvDisplayFillConsoleCharacter(RSSRV_DISPLAY_STATE* display, char character, + int x, int y, size_t length, size_t* writtenOut) { + if (!display || display->Manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + SYS_FILL_CONSOLE_CHARACTER_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Character = character; + request.X = x; + request.Y = y; + request.Length = length; + request.WrittenOut = writtenOut; + return SysFillDisplayManagerConsoleCharacter(display->Manager, &request); +} + +// Fills kernel-backed console attributes through delegated authority. +KSTATUS RssrvDisplayFillConsoleAttribute(RSSRV_DISPLAY_STATE* display, uint16_t attribute, + int x, int y, size_t length, size_t* writtenOut) { + if (!display || display->Manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + SYS_FILL_CONSOLE_ATTRIBUTE_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Attribute = attribute; + request.X = x; + request.Y = y; + request.Length = length; + request.WrittenOut = writtenOut; + return SysFillDisplayManagerConsoleAttribute(display->Manager, &request); +} + +// Draws one explicit glyph through delegated console authority. +KSTATUS RssrvDisplayPutConsoleCharacter(RSSRV_DISPLAY_STATE* display, char character, + int x, int y, uint32_t foreground, uint32_t background) { + if (!display || display->Manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + SYS_PUT_SCREEN_CHAR_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Character = character; + request.X = x; + request.Y = y; + request.Foreground = foreground; + request.Background = background; + return SysPutDisplayManagerConsoleCharacter(display->Manager, &request); +} + +// Clears the current console target through delegated console authority. +KSTATUS RssrvDisplayClearConsole(RSSRV_DISPLAY_STATE* display, uint32_t color) { + if (!display || display->Manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + return SysClearDisplayManagerConsole(display->Manager, color); +} diff --git a/code/user/system/rssrv/input.c b/code/user/system/rssrv/input.c new file mode 100644 index 0000000..e26357b --- /dev/null +++ b/code/user/system/rssrv/input.c @@ -0,0 +1,564 @@ +// RSSRV logical input. The kernel reports normalized physical events; RSSRV owns interpretation, pointer state, and routing. + +#include "rssrv_internal.h" +#include +#include +#include + +#define RSSRV_EVENT_MANUAL_RESET 1u +#define RSSRV_INPUT_KEY_QUEUE_MASK (RSSRV_INPUT_KEY_QUEUE_SIZE - 1u) +#define RSSRV_INPUT_MOUSE_QUEUE_MASK (RSSRV_INPUT_MOUSE_QUEUE_SIZE - 1u) + +typedef struct RSSRV_INPUT_SESSION { + RSSRV_INPUT_STATE* Input; + SYS_HANDLE Endpoint; +} RSSRV_INPUT_SESSION; + +typedef struct RSSRV_KEY_CHAR_PAIR { + INPUT_KEY Key; + char Normal; + char Shifted; +} RSSRV_KEY_CHAR_PAIR; + +static const RSSRV_KEY_CHAR_PAIR gInputCharMap[] = { + { INPUT_KEY_1, '1', '!' }, { INPUT_KEY_2, '2', '@' }, + { INPUT_KEY_3, '3', '#' }, { INPUT_KEY_4, '4', '$' }, + { INPUT_KEY_5, '5', '%' }, { INPUT_KEY_6, '6', '^' }, + { INPUT_KEY_7, '7', '&' }, { INPUT_KEY_8, '8', '*' }, + { INPUT_KEY_9, '9', '(' }, { INPUT_KEY_0, '0', ')' }, + { INPUT_KEY_MINUS, '-', '_' }, { INPUT_KEY_EQUAL, '=', '+' }, + { INPUT_KEY_LEFT_BRACKET, '[', '{' }, { INPUT_KEY_RIGHT_BRACKET, ']', '}' }, + { INPUT_KEY_BACKSLASH, '\\', '|' }, { INPUT_KEY_SEMICOLON, ';', ':' }, + { INPUT_KEY_APOSTROPHE, '\'', '"' }, { INPUT_KEY_GRAVE, '`', '~' }, + { INPUT_KEY_COMMA, ',', '<' }, { INPUT_KEY_PERIOD, '.', '>' }, + { INPUT_KEY_SLASH, '/', '?' }, + { INPUT_KEY_SPACE, ' ', ' ' }, { INPUT_KEY_TAB, '\t', '\t' }, + { INPUT_KEY_ENTER, '\n', '\n' }, { INPUT_KEY_BACKSPACE, '\b', '\b' }, + { INPUT_KEY_KP_DIVIDE, '/', '/' }, { INPUT_KEY_KP_MULTIPLY, '*', '*' }, + { INPUT_KEY_KP_MINUS, '-', '-' }, { INPUT_KEY_KP_PLUS, '+', '+' }, + { INPUT_KEY_KP_ENTER, '\n', '\n' }, { INPUT_KEY_KP_1, '1', '1' }, + { INPUT_KEY_KP_2, '2', '2' }, { INPUT_KEY_KP_3, '3', '3' }, + { INPUT_KEY_KP_4, '4', '4' }, { INPUT_KEY_KP_5, '5', '5' }, + { INPUT_KEY_KP_6, '6', '6' }, { INPUT_KEY_KP_7, '7', '7' }, + { INPUT_KEY_KP_8, '8', '8' }, { INPUT_KEY_KP_9, '9', '9' }, + { INPUT_KEY_KP_0, '0', '0' }, { INPUT_KEY_KP_PERIOD, '.', '.' }, +}; + +// Acquires the input-state mutex. +static KSTATUS RssrvInputLock(RSSRV_INPUT_STATE* input) { + return input ? SysWait(input->Mutex, SYS_WAIT_FLAG_NONE) : NTLSTATUS_INVALID_PARAMETER; +} + +// Releases the input-state mutex. +static void RssrvInputUnlock(RSSRV_INPUT_STATE* input) { + if (input) (void)SysReleaseMutex(input->Mutex); +} + +// Tests one logical key bit while the input mutex is held. +static int RssrvInputKeyDownLocked(const RSSRV_INPUT_STATE* input, INPUT_KEY key) { + return key < NTL_INPUT_KEY_BYTES * 8u && + (input->KeyDown[key >> 3] & (uint8_t)(1u << (key & 7u))) != 0; +} + +// Updates one logical key bit while the input mutex is held. +static void RssrvInputSetKeyDownLocked(RSSRV_INPUT_STATE* input, INPUT_KEY key, int down) { + uint8_t mask = (uint8_t)(1u << (key & 7u)); + if (down) input->KeyDown[key >> 3] |= mask; + else input->KeyDown[key >> 3] &= (uint8_t)~mask; +} + +// Returns whether duplicate physical make events should generate repeated logical key records. +static int RssrvInputKeyIsRepeatable(INPUT_KEY key) { + switch (key) { + case INPUT_KEY_NONE: + case INPUT_KEY_LEFT_SHIFT: + case INPUT_KEY_RIGHT_SHIFT: + case INPUT_KEY_LEFT_CTRL: + case INPUT_KEY_RIGHT_CTRL: + case INPUT_KEY_LEFT_ALT: + case INPUT_KEY_RIGHT_ALT: + case INPUT_KEY_LEFT_GUI: + case INPUT_KEY_RIGHT_GUI: + case INPUT_KEY_CAPS_LOCK: + case INPUT_KEY_NUM_LOCK: + return 0; + default: + return 1; + } +} + +// Applies modifier and lock-key state for one first make or final break. +static void RssrvInputUpdateModifierLocked(RSSRV_INPUT_STATE* input, INPUT_KEY key, int pressed) { + uint32_t bit = 0; + switch (key) { + case INPUT_KEY_LEFT_SHIFT: bit = INPUT_MOD_LSHIFT; break; + case INPUT_KEY_RIGHT_SHIFT: bit = INPUT_MOD_RSHIFT; break; + case INPUT_KEY_LEFT_CTRL: bit = INPUT_MOD_LCTRL; break; + case INPUT_KEY_RIGHT_CTRL: bit = INPUT_MOD_RCTRL; break; + case INPUT_KEY_LEFT_ALT: bit = INPUT_MOD_LALT; break; + case INPUT_KEY_RIGHT_ALT: bit = INPUT_MOD_RALT; break; + case INPUT_KEY_LEFT_GUI: bit = INPUT_MOD_LGUI; break; + case INPUT_KEY_RIGHT_GUI: bit = INPUT_MOD_RGUI; break; + default: break; + } + if (bit) { + if (pressed) input->Modifiers |= bit; + else input->Modifiers &= ~bit; + } + if (pressed && key == INPUT_KEY_CAPS_LOCK) input->Modifiers ^= INPUT_MOD_CAPS_LOCK; + if (pressed && key == INPUT_KEY_NUM_LOCK) input->Modifiers ^= INPUT_MOD_NUM_LOCK; +} + +// Maps one key through RSSRV's initial US keyboard layout. +static char RssrvInputKeyToCharLocked(const RSSRV_INPUT_STATE* input, INPUT_KEY key) { + if (key >= INPUT_KEY_A && key <= INPUT_KEY_Z) { + int shifted = (input->Modifiers & INPUT_MOD_SHIFT) != 0; + int caps = (input->Modifiers & INPUT_MOD_CAPS_LOCK) != 0; + char c = (char)('a' + (key - INPUT_KEY_A)); + if (shifted ^ caps) c = (char)(c - 'a' + 'A'); + return c; + } + int shifted = (input->Modifiers & INPUT_MOD_SHIFT) != 0; + for (size_t i = 0; i < sizeof(gInputCharMap) / sizeof(gInputCharMap[0]); ++i) { + if (gInputCharMap[i].Key == key) + return shifted ? gInputCharMap[i].Shifted : gInputCharMap[i].Normal; + } + return 0; +} + +// Refreshes the keyboard availability event while the input mutex is held. +static void RssrvInputRefreshKeyEventLocked(RSSRV_INPUT_STATE* input) { + if (input->KeyHead != input->KeyTail) (void)SysSignalEvent(input->KeyEvent); + else (void)SysClearEvent(input->KeyEvent); +} + +// Refreshes the mouse availability event while the input mutex is held. +static void RssrvInputRefreshMouseEventLocked(RSSRV_INPUT_STATE* input) { + if (input->MouseHead != input->MouseTail) (void)SysSignalEvent(input->MouseEvent); + else (void)SysClearEvent(input->MouseEvent); +} + +// Builds one logical key record and mirrors it into the raw logical-input queue. +static void RssrvInputQueueKeyLocked(RSSRV_INPUT_STATE* input, INPUT_KEY key, INPUT_KEY_RECORD* recordOut) { + INPUT_KEY_RECORD record; + memset(&record, 0, sizeof(record)); + record.Key = key; + record.Char = RssrvInputKeyToCharLocked(input, key); + record.Modifiers = input->Modifiers; + if (recordOut) *recordOut = record; + + uint32_t next = (input->KeyTail + 1u) & RSSRV_INPUT_KEY_QUEUE_MASK; + if (next == input->KeyHead) return; + input->KeyQueue[input->KeyTail] = record; + input->KeyTail = next; + RssrvInputRefreshKeyEventLocked(input); +} + +// Applies one normalized physical keyboard make/break event and routes logical presses after releasing the input lock. +static void RssrvInputApplyKey(RSSRV_INPUT_STATE* input, const INPUT_KEY_EVENT* event) { + if (!input || !event || event->Key == INPUT_KEY_NONE || event->Key >= NTL_INPUT_KEY_BYTES * 8u) + return; + if (!KSUCCESS(RssrvInputLock(input))) return; + + INPUT_KEY_RECORD routedRecord; + memset(&routedRecord, 0, sizeof(routedRecord)); + int routeRecord = 0; + INPUT_KEY key = event->Key; + int wasDown = RssrvInputKeyDownLocked(input, key); + if (event->Pressed) { + if (!wasDown) { + RssrvInputSetKeyDownLocked(input, key, 1); + RssrvInputUpdateModifierLocked(input, key, 1); + input->Generation++; + RssrvInputQueueKeyLocked(input, key, &routedRecord); + routeRecord = 1; + } else if (RssrvInputKeyIsRepeatable(key)) { + // Hardware typematic make events remain useful until RSSRV grows its own repeat timer. + RssrvInputQueueKeyLocked(input, key, &routedRecord); + routeRecord = 1; + } + } else if (wasDown) { + RssrvInputSetKeyDownLocked(input, key, 0); + RssrvInputUpdateModifierLocked(input, key, 0); + input->Generation++; + } + RssrvInputUnlock(input); + + if (routeRecord) { + RssrvSessionObserveKey(input->Session, &routedRecord); + RssrvSessionRouteKey(input->Session, &routedRecord); + } +} + +// Applies one normalized relative mouse event and lets Session 0 own cursor and target routing policy. +static void RssrvInputApplyMouse(RSSRV_INPUT_STATE* input, const INPUT_MOUSE_EVENT* event) { + if (!input || !event) return; + int x = 0; + int y = 0; + int changed = 0; + if (!KSUCCESS(RssrvInputLock(input))) return; + + int nx = input->MouseX + event->dx; + int ny = input->MouseY + event->dy; + int width = input->MouseWidth > 0 ? input->MouseWidth : 1; + int height = input->MouseHeight > 0 ? input->MouseHeight : 1; + if (nx < 0) nx = 0; + if (ny < 0) ny = 0; + if (nx >= width) nx = width - 1; + if (ny >= height) ny = height - 1; + + changed = nx != input->MouseX || ny != input->MouseY || + event->wheel != 0 || event->buttons != input->MouseButtons; + input->MouseX = nx; + input->MouseY = ny; + input->MouseWheel += event->wheel; + input->MouseButtons = event->buttons; + if (changed) input->Generation++; + + uint32_t next = (input->MouseTail + 1u) & RSSRV_INPUT_MOUSE_QUEUE_MASK; + if (next != input->MouseHead) { + input->MouseQueue[input->MouseTail] = *event; + input->MouseTail = next; + RssrvInputRefreshMouseEventLocked(input); + } + x = input->MouseX; + y = input->MouseY; + RssrvInputUnlock(input); + + if (changed) { + RssrvSessionObserveMouse(input->Session, event, x, y); + RssrvSessionRouteMouse(input->Session, event, x, y); + } +} + +// Consumes the kernel's normalized physical input stream for the lifetime of RSSRV. +static void RssrvInputRawThread(uintptr_t argument) { + RSSRV_INPUT_STATE* input = (RSSRV_INPUT_STATE*)argument; + RssrvSetCurrentThreadName("rssrv.input.raw"); + if (!input) SysExitThread(1); + + for (;;) { + SYS_INPUT_MANAGER_EVENT event; + memset(&event, 0, sizeof(event)); + SYS_INPUT_MANAGER_READ_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.ManagerHandle = input->Manager; + request.Flags = SYS_INPUT_MANAGER_EVENT_FLAG_NONE; + request.TimeoutMilliseconds = SYS_WAIT_TIMEOUT_INFINITE; + request.EventOut = &event; + KSTATUS status = SysReadInputManagerEvent(&request); + if (!KSUCCESS(status)) RssrvExit(14); + + if (event.Type == SYS_INPUT_MANAGER_EVENT_KEY) + RssrvInputApplyKey(input, &event.Data.Key); + else if (event.Type == SYS_INPUT_MANAGER_EVENT_MOUSE) + RssrvInputApplyMouse(input, &event.Data.Mouse); + } +} + +// Reads or peeks one logical keyboard record, blocking on the queue event when requested. +KSTATUS RssrvInputReadKeyRecord(RSSRV_INPUT_STATE* input, + int peek, + uint64_t timeoutMilliseconds, + INPUT_KEY_RECORD* recordOut) { + if (!input || !recordOut) return NTLSTATUS_INVALID_PARAMETER; + for (;;) { + KSTATUS status = RssrvInputLock(input); + if (!KSUCCESS(status)) return status; + if (input->KeyHead != input->KeyTail) { + *recordOut = input->KeyQueue[input->KeyHead]; + if (!peek) { + input->KeyHead = (input->KeyHead + 1u) & RSSRV_INPUT_KEY_QUEUE_MASK; + RssrvInputRefreshKeyEventLocked(input); + } + RssrvInputUnlock(input); + return NTLSTATUS_SUCCESS; + } + RssrvInputUnlock(input); + if (timeoutMilliseconds == 0) return NTLSTATUS_WOULD_BLOCK; + status = SysWaitSingle(input->KeyEvent, SYS_WAIT_FLAG_NONE, timeoutMilliseconds); + if (!KSUCCESS(status)) return status; + } +} + +// Discards all queued logical keyboard records while preserving current key state. +KSTATUS RssrvInputFlushKeys(RSSRV_INPUT_STATE* input, uint32_t* discardedOut) { + if (!input) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvInputLock(input); + if (!KSUCCESS(status)) return status; + uint32_t discarded = (input->KeyTail - input->KeyHead) & RSSRV_INPUT_KEY_QUEUE_MASK; + input->KeyHead = input->KeyTail; + RssrvInputRefreshKeyEventLocked(input); + RssrvInputUnlock(input); + if (discardedOut) *discardedOut = discarded; + return NTLSTATUS_SUCCESS; +} + +// Reads or peeks one logical mouse record, blocking on the queue event when requested. +static KSTATUS RssrvInputGetMouse(RSSRV_INPUT_STATE* input, + int peek, + uint64_t timeoutMilliseconds, + INPUT_MOUSE_EVENT* eventOut) { + if (!input || !eventOut) return NTLSTATUS_INVALID_PARAMETER; + for (;;) { + KSTATUS status = RssrvInputLock(input); + if (!KSUCCESS(status)) return status; + if (input->MouseHead != input->MouseTail) { + *eventOut = input->MouseQueue[input->MouseHead]; + if (!peek) { + input->MouseHead = (input->MouseHead + 1u) & RSSRV_INPUT_MOUSE_QUEUE_MASK; + RssrvInputRefreshMouseEventLocked(input); + } + RssrvInputUnlock(input); + return NTLSTATUS_SUCCESS; + } + RssrvInputUnlock(input); + if (timeoutMilliseconds == 0) return NTLSTATUS_WOULD_BLOCK; + status = SysWaitSingle(input->MouseEvent, SYS_WAIT_FLAG_NONE, timeoutMilliseconds); + if (!KSUCCESS(status)) return status; + } +} + +// Snapshots RSSRV's logical state while holding its state mutex. +static KSTATUS RssrvInputQueryState(RSSRV_INPUT_STATE* input, NTL_INPUT_STATE* stateOut) { + if (!input || !stateOut) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvInputLock(input); + if (!KSUCCESS(status)) return status; + memset(stateOut, 0, sizeof(*stateOut)); + stateOut->Size = sizeof(*stateOut); + stateOut->Flags = NTL_INPUT_STATE_FLAG_NONE; + stateOut->Generation = input->Generation; + stateOut->Modifiers = input->Modifiers; + memcpy(stateOut->KeyDown, input->KeyDown, sizeof(stateOut->KeyDown)); + stateOut->MouseX = input->MouseX; + stateOut->MouseY = input->MouseY; + stateOut->MouseWheel = input->MouseWheel; + stateOut->MouseButtons = input->MouseButtons; + RssrvInputUnlock(input); + return NTLSTATUS_SUCCESS; +} + +// Sends one fixed logical-input reply. +static KSTATUS RssrvInputReply(SYS_HANDLE endpoint, + uint32_t messageId, + uint32_t operation, + KSTATUS status, + const INPUT_KEY_RECORD* key, + const INPUT_MOUSE_EVENT* mouse, + const NTL_INPUT_STATE* state) { + NTL_INPUT_REPLY reply; + memset(&reply, 0, sizeof(reply)); + reply.Size = sizeof(reply); + reply.Version = NTL_INPUT_PROTOCOL_VERSION; + reply.Operation = operation; + reply.Flags = NTL_INPUT_FLAG_NONE; + reply.Status = status; + if (key) reply.Key = *key; + if (mouse) reply.Mouse = *mouse; + if (state) reply.State = *state; + + SYS_REPLY_PORT_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.EndpointHandle = endpoint; + request.MessageId = messageId; + request.Flags = SYS_PORT_FLAG_NONE; + request.Payload = &reply; + request.PayloadLength = sizeof(reply); + return SysReplyPort(&request); +} + +// Serves one connected logical-input client. +static void RssrvInputSessionThread(uintptr_t argument) { + RSSRV_INPUT_SESSION* session = (RSSRV_INPUT_SESSION*)argument; + if (!session || !session->Input) { + if (session) free(session); + SysExitThread(1); + } + RSSRV_INPUT_STATE* input = session->Input; + SYS_HANDLE endpoint = session->Endpoint; + free(session); + RssrvSetCurrentThreadName("rssrv.input.session"); + + for (;;) { + NTL_INPUT_REQUEST request; + memset(&request, 0, sizeof(request)); + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = endpoint; + receive.Flags = SYS_PORT_FLAG_NONE; + receive.Payload = &request; + receive.PayloadCapacity = sizeof(request); + receive.InfoOut = &info; + receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysReceivePort(&receive); + if (status == NTLSTATUS_PORT_DISCONNECTED) break; + if (!KSUCCESS(status)) break; + + if (info.Type != SYS_PORT_MESSAGE_REQUEST || info.PayloadLength != sizeof(request) || info.HandleCount != 0 || + request.Size != sizeof(request) || request.Version != NTL_INPUT_PROTOCOL_VERSION || + request.Flags != NTL_INPUT_FLAG_NONE) { + (void)RssrvInputReply(endpoint, info.MessageId, request.Operation, + NTLSTATUS_INVALID_PARAMETER, 0, 0, 0); + continue; + } + + INPUT_KEY_RECORD key; + INPUT_MOUSE_EVENT mouse; + NTL_INPUT_STATE state; + memset(&key, 0, sizeof(key)); + memset(&mouse, 0, sizeof(mouse)); + memset(&state, 0, sizeof(state)); + switch (request.Operation) { + case NTL_INPUT_OP_READ_KEY: + status = RssrvInputReadKeyRecord(input, 0, request.TimeoutMilliseconds, &key); + (void)RssrvInputReply(endpoint, info.MessageId, request.Operation, status, + KSUCCESS(status) ? &key : 0, 0, 0); + break; + case NTL_INPUT_OP_PEEK_KEY: + status = RssrvInputReadKeyRecord(input, 1, request.TimeoutMilliseconds, &key); + (void)RssrvInputReply(endpoint, info.MessageId, request.Operation, status, + KSUCCESS(status) ? &key : 0, 0, 0); + break; + case NTL_INPUT_OP_READ_MOUSE: + status = RssrvInputGetMouse(input, 0, request.TimeoutMilliseconds, &mouse); + (void)RssrvInputReply(endpoint, info.MessageId, request.Operation, status, + 0, KSUCCESS(status) ? &mouse : 0, 0); + break; + case NTL_INPUT_OP_PEEK_MOUSE: + status = RssrvInputGetMouse(input, 1, request.TimeoutMilliseconds, &mouse); + (void)RssrvInputReply(endpoint, info.MessageId, request.Operation, status, + 0, KSUCCESS(status) ? &mouse : 0, 0); + break; + case NTL_INPUT_OP_QUERY_STATE: + status = RssrvInputQueryState(input, &state); + (void)RssrvInputReply(endpoint, info.MessageId, request.Operation, status, + 0, 0, KSUCCESS(status) ? &state : 0); + break; + default: + (void)RssrvInputReply(endpoint, info.MessageId, request.Operation, + NTLSTATUS_INVALID_PARAMETER, 0, 0, 0); + break; + } + } + + (void)SysCloseHandle(endpoint); + SysExitThread(0); +} + +// Accepts logical-input sessions and gives each connection one worker thread. +static void RssrvInputListenerThread(uintptr_t argument) { + RSSRV_INPUT_STATE* input = (RSSRV_INPUT_STATE*)argument; + RssrvSetCurrentThreadName("rssrv.input.listen"); + if (!input) SysExitThread(1); + + for (;;) { + SYS_HANDLE endpoint = SYS_HANDLE_INVALID; + SYS_ACCEPT_PORT_REQUEST accept; + memset(&accept, 0, sizeof(accept)); + accept.Size = sizeof(accept); + accept.ListenerHandle = input->Listener; + accept.Flags = SYS_PORT_ACCEPT_FLAG_NONE; + accept.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + accept.EndpointHandleOut = &endpoint; + KSTATUS status = SysAcceptPort(&accept); + if (!KSUCCESS(status)) RssrvExit(15); + + RSSRV_INPUT_SESSION* session = (RSSRV_INPUT_SESSION*)malloc(sizeof(*session)); + if (!session) { + (void)SysCloseHandle(endpoint); + continue; + } + session->Input = input; + session->Endpoint = endpoint; + + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)RssrvInputSessionThread; + create.Argument = (uintptr_t)session; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + KSTATUS createStatus = SysCreateThread(&create); + if (!KSUCCESS(createStatus)) { + free(session); + (void)SysCloseHandle(endpoint); + } + } +} + +// Initializes RSSRV logical input and its initial display-sized pointer state. +KSTATUS RssrvInputInitialize(RSSRV_INPUT_STATE* input, + RSSRV_NAMESPACE* namespaceState, + RSSRV_SESSION_STATE* session, + SYS_HANDLE manager) { + if (!input || !namespaceState || !session || manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + memset(input, 0, sizeof(*input)); + input->Manager = manager; + input->Namespace = namespaceState; + input->Session = session; + input->Mutex = SYS_HANDLE_INVALID; + input->KeyEvent = SYS_HANDLE_INVALID; + input->MouseEvent = SYS_HANDLE_INVALID; + input->Listener = SYS_HANDLE_INVALID; + + KSTATUS status = SysCreateMutex(&input->Mutex); + if (!KSUCCESS(status)) return status; + status = SysCreateEvent(RSSRV_EVENT_MANUAL_RESET, 0, &input->KeyEvent); + if (!KSUCCESS(status)) return status; + status = SysCreateEvent(RSSRV_EVENT_MANUAL_RESET, 0, &input->MouseEvent); + if (!KSUCCESS(status)) return status; + + status = RssrvSessionQueryDisplayBounds(session, &input->MouseWidth, &input->MouseHeight); + if (!KSUCCESS(status)) return status; + input->MouseX = input->MouseWidth / 2; + input->MouseY = input->MouseHeight / 2; + return NTLSTATUS_SUCCESS; +} + +// Publishes /Services/Input and starts the raw-input and client-listener workers. +KSTATUS RssrvInputStart(RSSRV_INPUT_STATE* input) { + if (!input || !input->Namespace || input->Listener != SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + SYS_CREATE_PORT_REQUEST createPort; + memset(&createPort, 0, sizeof(createPort)); + createPort.Size = sizeof(createPort); + createPort.Flags = SYS_PORT_FLAG_NONE; + createPort.Backlog = RSSRV_LISTENER_BACKLOG; + createPort.HandleOut = &input->Listener; + KSTATUS status = SysCreatePort(&createPort); + if (!KSUCCESS(status)) return status; + + status = RssrvNamespaceBindOwned(input->Namespace, + NTL_INPUT_SERVICE_PATH, + sizeof(NTL_INPUT_SERVICE_PATH) - 1u, + NTL_RESOURCE_KIND_CAPABILITY, + input->Listener, + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT); + if (!KSUCCESS(status)) return status; + + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)RssrvInputRawThread; + create.Argument = (uintptr_t)input; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + status = SysCreateThread(&create); + if (!KSUCCESS(status)) return status; + + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)RssrvInputListenerThread; + create.Argument = (uintptr_t)input; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + return SysCreateThread(&create); +} diff --git a/code/user/system/rssrv/namespace.c b/code/user/system/rssrv/namespace.c new file mode 100644 index 0000000..d5e8668 --- /dev/null +++ b/code/user/system/rssrv/namespace.c @@ -0,0 +1,558 @@ +// RSSRV native namespace tree, symbolic-link traversal, bindings, and enumeration. + +#include "rssrv_internal.h" +#include +#include +#include + +typedef enum RSSRV_NODE_TYPE { + RssrvNodeDirectory = 1, + RssrvNodeBinding = 2, + RssrvNodeSymbolicLink = 3, +} RSSRV_NODE_TYPE; + +// One hierarchical namespace node. Name storage follows the fixed header in the same allocation. +struct RSSRV_NODE { + RSSRV_NODE* Parent; + RSSRV_NODE* FirstChild; + RSSRV_NODE* NextSibling; + RSSRV_NODE_TYPE Type; + uint32_t NameLength; + union { + struct { + SYS_HANDLE Handle; + uint32_t Kind; + SYS_RIGHTS ExportRights; + } Binding; + struct { + char* Target; + uint32_t TargetLength; + } Link; + } u; + char Name[1]; +}; + +// Acquires the namespace mutex. +static KSTATUS RssrvNamespaceLock(RSSRV_NAMESPACE* namespaceState) { + if (!namespaceState || namespaceState->Mutex == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + return SysWait(namespaceState->Mutex, SYS_WAIT_FLAG_NONE); +} + +// Releases the namespace mutex. +static void RssrvNamespaceUnlock(RSSRV_NAMESPACE* namespaceState) { + if (namespaceState && namespaceState->Mutex != SYS_HANDLE_INVALID) + (void)SysReleaseMutex(namespaceState->Mutex); +} + +// Allocates one namespace node with inline component-name storage. +static RSSRV_NODE* RssrvAllocateNode(RSSRV_NODE_TYPE type, const char* name, uint32_t nameLength) { + if (nameLength > NTL_RESOURCE_NAME_MAX || (nameLength && !name)) + return (RSSRV_NODE*)0; + size_t bytes = offsetof(RSSRV_NODE, Name) + (size_t)nameLength + 1u; + RSSRV_NODE* node = (RSSRV_NODE*)malloc(bytes); + if (!node) return (RSSRV_NODE*)0; + memset(node, 0, bytes); + node->Type = type; + node->NameLength = nameLength; + if (nameLength) memcpy(node->Name, name, nameLength); + node->Name[nameLength] = '\0'; + return node; +} + +// Returns the public resource kind exposed for one exact namespace node. +static uint32_t RssrvNodeKind(const RSSRV_NODE* node) { + if (!node) return NTL_RESOURCE_KIND_INVALID; + if (node->Type == RssrvNodeDirectory) return NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY; + if (node->Type == RssrvNodeSymbolicLink) return NTL_RESOURCE_KIND_SYMBOLIC_LINK; + if (node->Type == RssrvNodeBinding) return node->u.Binding.Kind; + return NTL_RESOURCE_KIND_INVALID; +} + +// Finds one immediate child while the namespace mutex is held. +static RSSRV_NODE* RssrvFindChildLocked(RSSRV_NODE* parent, const char* name, uint32_t nameLength) { + if (!parent || parent->Type != RssrvNodeDirectory || !name || !nameLength) + return (RSSRV_NODE*)0; + for (RSSRV_NODE* child = parent->FirstChild; child; child = child->NextSibling) { + if (child->NameLength == nameLength && memcmp(child->Name, name, nameLength) == 0) + return child; + } + return (RSSRV_NODE*)0; +} + +// Links one unowned node beneath a namespace directory while the namespace mutex is held. +static KSTATUS RssrvInsertChildLocked(RSSRV_NODE* parent, RSSRV_NODE* node) { + if (!parent || parent->Type != RssrvNodeDirectory || !node || !node->NameLength) + return NTLSTATUS_INVALID_PARAMETER; + if (RssrvFindChildLocked(parent, node->Name, node->NameLength)) + return NTLSTATUS_OBJECT_NAME_COLLISION; + node->Parent = parent; + node->NextSibling = parent->FirstChild; + parent->FirstChild = node; + return NTLSTATUS_SUCCESS; +} + +// Walks an exact namespace path without following symbolic links. +static RSSRV_NODE* RssrvFindExactLocked(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length) { + if (!namespaceState || !namespaceState->Root || !path || length == 0 || path[0] != '/') + return (RSSRV_NODE*)0; + if (length == 1) + return namespaceState->Root; + + RSSRV_NODE* current = namespaceState->Root; + uint32_t componentStart = 1; + for (uint32_t i = 1; i <= length; ++i) { + if (i != length && path[i] != '/') + continue; + uint32_t componentLength = i - componentStart; + current = RssrvFindChildLocked(current, path + componentStart, componentLength); + if (!current) + return (RSSRV_NODE*)0; + if (i != length && current->Type != RssrvNodeDirectory) + return (RSSRV_NODE*)0; + componentStart = i + 1; + } + return current; +} + +// Splits one non-root path into an existing namespace parent and final component. +static KSTATUS RssrvFindParentLocked(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + RSSRV_NODE** parentOut, + const char** nameOut, + uint32_t* nameLengthOut) { + if (!namespaceState || !path || length < 2 || !parentOut || !nameOut || !nameLengthOut) + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t slash = length; + while (slash > 1 && path[slash - 1u] != '/') slash--; + uint32_t nameStart = slash; + uint32_t nameLength = length - nameStart; + uint32_t parentLength = nameStart == 1u ? 1u : nameStart - 1u; + RSSRV_NODE* parent = RssrvFindExactLocked(namespaceState, path, parentLength); + if (!parent) return NTLSTATUS_NOT_FOUND; + if (parent->Type != RssrvNodeDirectory) return NTLSTATUS_NOT_SUPPORTED; + + *parentOut = parent; + *nameOut = path + nameStart; + *nameLengthOut = nameLength; + return NTLSTATUS_SUCCESS; +} + +// Frees one detached namespace node and the payload it owns. +static void RssrvFreeDetachedNode(RSSRV_NODE* node) { + if (!node) return; + if (node->Type == RssrvNodeBinding && node->u.Binding.Handle != SYS_HANDLE_INVALID) + (void)SysCloseHandle(node->u.Binding.Handle); + if (node->Type == RssrvNodeSymbolicLink && node->u.Link.Target) + free(node->u.Link.Target); + free(node); +} + +// Resolves a symbolic-link target to one exact namespace node while bounding recursive links. +static KSTATUS RssrvResolveExactNodeLocked(RSSRV_NAMESPACE* namespaceState, + RSSRV_NODE* node, + uint32_t depth, + RSSRV_NODE** nodeOut) { + if (!namespaceState || !nodeOut) return NTLSTATUS_INVALID_PARAMETER; + *nodeOut = (RSSRV_NODE*)0; + while (node && node->Type == RssrvNodeSymbolicLink) { + if (depth++ >= RSSRV_SYMBOLIC_LINK_MAX_DEPTH) + return NTLSTATUS_LIMIT_REACHED; + node = RssrvFindExactLocked(namespaceState, node->u.Link.Target, node->u.Link.TargetLength); + if (!node) + return NTLSTATUS_NOT_FOUND; + } + if (!node) return NTLSTATUS_NOT_FOUND; + *nodeOut = node; + return NTLSTATUS_SUCCESS; +} + +// Resolves the first bound capability reached by the caller's path, following namespace links. +static KSTATUS RssrvResolveBindingLocked(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + RSSRV_NODE** bindingOut, + uint32_t* consumedOut) { + if (!namespaceState || !path || length < 2 || !bindingOut || !consumedOut) + return NTLSTATUS_INVALID_PARAMETER; + *bindingOut = (RSSRV_NODE*)0; + *consumedOut = 0; + + RSSRV_NODE* current = namespaceState->Root; + uint32_t componentStart = 1; + for (uint32_t i = 1; i <= length; ++i) { + if (i != length && path[i] != '/') + continue; + uint32_t componentLength = i - componentStart; + RSSRV_NODE* next = RssrvFindChildLocked(current, path + componentStart, componentLength); + if (!next) return NTLSTATUS_NOT_FOUND; + uint32_t consumed = i; + RSSRV_NODE* resolved = (RSSRV_NODE*)0; + KSTATUS status = RssrvResolveExactNodeLocked(namespaceState, next, 0, &resolved); + if (!KSUCCESS(status)) return status; + next = resolved; + if (next->Type == RssrvNodeBinding) { + *bindingOut = next; + *consumedOut = consumed; + return NTLSTATUS_SUCCESS; + } + if (next->Type != RssrvNodeDirectory) + return NTLSTATUS_NOT_SUPPORTED; + current = next; + componentStart = i + 1u; + } + return NTLSTATUS_NOT_SUPPORTED; +} + +// Returns the lexicographically next immediate child after one optional cursor name. +static RSSRV_NODE* RssrvEnumerateNextLocked(RSSRV_NODE* directory, + const char* after, + uint32_t afterLength) { + if (!directory || directory->Type != RssrvNodeDirectory) + return (RSSRV_NODE*)0; + RSSRV_NODE* best = (RSSRV_NODE*)0; + for (RSSRV_NODE* child = directory->FirstChild; child; child = child->NextSibling) { + int afterCompare = 1; + if (afterLength) { + uint32_t common = child->NameLength < afterLength ? child->NameLength : afterLength; + int cmp = memcmp(child->Name, after, common); + if (cmp == 0) { + if (child->NameLength < afterLength) cmp = -1; + else if (child->NameLength > afterLength) cmp = 1; + } + afterCompare = cmp; + } + if (afterLength && afterCompare <= 0) + continue; + if (!best) { + best = child; + continue; + } + uint32_t common = child->NameLength < best->NameLength ? child->NameLength : best->NameLength; + int cmp = memcmp(child->Name, best->Name, common); + if (cmp < 0 || (cmp == 0 && child->NameLength < best->NameLength)) + best = child; + } + return best; +} + +// Initializes the native namespace root and its synchronization object. +KSTATUS RssrvNamespaceInitialize(RSSRV_NAMESPACE* namespaceState) { + if (!namespaceState) return NTLSTATUS_INVALID_PARAMETER; + memset(namespaceState, 0, sizeof(*namespaceState)); + namespaceState->Mutex = SYS_HANDLE_INVALID; + + KSTATUS status = SysCreateMutex(&namespaceState->Mutex); + if (!KSUCCESS(status)) return status; + namespaceState->Root = RssrvAllocateNode(RssrvNodeDirectory, (const char*)0, 0); + if (!namespaceState->Root) { + (void)SysCloseHandle(namespaceState->Mutex); + namespaceState->Mutex = SYS_HANDLE_INVALID; + return NTLSTATUS_NO_MEMORY; + } + return NTLSTATUS_SUCCESS; +} + +// Validates one canonical absolute native resource path, including the root path "/". +KSTATUS RssrvNamespaceValidatePath(const char* path, uint32_t length) { + if (!path || length == 0 || length > NTL_RESOURCE_PATH_MAX || path[0] != '/') + return NTLSTATUS_INVALID_PARAMETER; + if (length == 1) + return NTLSTATUS_SUCCESS; + if (path[length - 1] == '/') + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t componentStart = 1; + for (uint32_t i = 1; i <= length; ++i) { + if (i < length) { + char c = path[i]; + if (c == '\\' || c == '\0') + return NTLSTATUS_INVALID_PARAMETER; + if (c != '/') + continue; + } + uint32_t componentLength = i - componentStart; + if (componentLength == 0 || componentLength > NTL_RESOURCE_NAME_MAX || + (componentLength == 1 && path[componentStart] == '.') || + (componentLength == 2 && path[componentStart] == '.' && path[componentStart + 1] == '.')) + return NTLSTATUS_INVALID_PARAMETER; + componentStart = i + 1; + } + return NTLSTATUS_SUCCESS; +} + +// Creates one exact namespace directory. Its parent must already exist. +KSTATUS RssrvNamespaceCreateDirectory(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length) { + KSTATUS status = RssrvNamespaceValidatePath(path, length); + if (!KSUCCESS(status)) return status; + if (!namespaceState || length == 1) return NTLSTATUS_OBJECT_NAME_COLLISION; + + RSSRV_NODE* node = (RSSRV_NODE*)0; + status = RssrvNamespaceLock(namespaceState); + if (!KSUCCESS(status)) return status; + + RSSRV_NODE* parent = (RSSRV_NODE*)0; + const char* name = (const char*)0; + uint32_t nameLength = 0; + status = RssrvFindParentLocked(namespaceState, path, length, &parent, &name, &nameLength); + if (KSUCCESS(status)) { + node = RssrvAllocateNode(RssrvNodeDirectory, name, nameLength); + if (!node) status = NTLSTATUS_NO_MEMORY; + } + if (KSUCCESS(status)) + status = RssrvInsertChildLocked(parent, node); + RssrvNamespaceUnlock(namespaceState); + if (!KSUCCESS(status) && node) free(node); + return status; +} + +// Creates one absolute namespace symbolic link. Dangling targets are permitted. +KSTATUS RssrvNamespaceCreateSymbolicLink(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + const char* target, + uint32_t targetLength) { + KSTATUS status = RssrvNamespaceValidatePath(path, length); + if (!KSUCCESS(status)) return status; + status = RssrvNamespaceValidatePath(target, targetLength); + if (!KSUCCESS(status)) return status; + if (!namespaceState || length == 1) return NTLSTATUS_ACCESS_DENIED; + + RSSRV_NODE* node = (RSSRV_NODE*)0; + char* targetCopy = (char*)malloc((size_t)targetLength + 1u); + if (!targetCopy) return NTLSTATUS_NO_MEMORY; + memcpy(targetCopy, target, targetLength); + targetCopy[targetLength] = '\0'; + + status = RssrvNamespaceLock(namespaceState); + if (!KSUCCESS(status)) { + free(targetCopy); + return status; + } + RSSRV_NODE* parent = (RSSRV_NODE*)0; + const char* name = (const char*)0; + uint32_t nameLength = 0; + status = RssrvFindParentLocked(namespaceState, path, length, &parent, &name, &nameLength); + if (KSUCCESS(status)) { + node = RssrvAllocateNode(RssrvNodeSymbolicLink, name, nameLength); + if (!node) status = NTLSTATUS_NO_MEMORY; + } + if (KSUCCESS(status)) { + node->u.Link.Target = targetCopy; + node->u.Link.TargetLength = targetLength; + status = RssrvInsertChildLocked(parent, node); + } + RssrvNamespaceUnlock(namespaceState); + if (!KSUCCESS(status)) { + if (node) free(node); + free(targetCopy); + } + return status; +} + +// Takes ownership of one handle and binds it to a new exact resource name. +KSTATUS RssrvNamespaceBindOwned(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + uint32_t kind, + SYS_HANDLE handle, + SYS_RIGHTS exportRights) { + if (!namespaceState || kind == NTL_RESOURCE_KIND_INVALID || + kind == NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY || kind == NTL_RESOURCE_KIND_SYMBOLIC_LINK || + handle == SYS_HANDLE_INVALID || !exportRights) + return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvNamespaceValidatePath(path, length); + if (!KSUCCESS(status)) return status; + if (length == 1) return NTLSTATUS_ACCESS_DENIED; + + RSSRV_NODE* node = (RSSRV_NODE*)0; + status = RssrvNamespaceLock(namespaceState); + if (!KSUCCESS(status)) return status; + RSSRV_NODE* parent = (RSSRV_NODE*)0; + const char* name = (const char*)0; + uint32_t nameLength = 0; + status = RssrvFindParentLocked(namespaceState, path, length, &parent, &name, &nameLength); + if (KSUCCESS(status)) { + node = RssrvAllocateNode(RssrvNodeBinding, name, nameLength); + if (!node) status = NTLSTATUS_NO_MEMORY; + } + if (KSUCCESS(status)) { + node->u.Binding.Handle = handle; + node->u.Binding.Kind = kind; + node->u.Binding.ExportRights = exportRights; + status = RssrvInsertChildLocked(parent, node); + } + RssrvNamespaceUnlock(namespaceState); + if (!KSUCCESS(status) && node) free(node); + return status; +} + +// Removes one exact namespace node. Namespace directories must be empty. +KSTATUS RssrvNamespaceUnbind(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length) { + KSTATUS status = RssrvNamespaceValidatePath(path, length); + if (!KSUCCESS(status)) return status; + if (!namespaceState || length == 1) return NTLSTATUS_ACCESS_DENIED; + status = RssrvNamespaceLock(namespaceState); + if (!KSUCCESS(status)) return status; + + RSSRV_NODE* node = RssrvFindExactLocked(namespaceState, path, length); + if (!node) { + RssrvNamespaceUnlock(namespaceState); + return NTLSTATUS_NOT_FOUND; + } + if (node->Type == RssrvNodeDirectory && node->FirstChild) { + RssrvNamespaceUnlock(namespaceState); + return NTLSTATUS_ACCESS_DENIED; + } + + RSSRV_NODE* parent = node->Parent; + RSSRV_NODE** link = &parent->FirstChild; + while (*link && *link != node) link = &(*link)->NextSibling; + if (*link != node) { + RssrvNamespaceUnlock(namespaceState); + return NTLSTATUS_FAILURE; + } + *link = node->NextSibling; + node->Parent = (RSSRV_NODE*)0; + node->NextSibling = (RSSRV_NODE*)0; + RssrvNamespaceUnlock(namespaceState); + RssrvFreeDetachedNode(node); + return NTLSTATUS_SUCCESS; +} + +// Queries one exact namespace node without following the final symbolic link. +KSTATUS RssrvNamespaceQuery(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + uint32_t* kindOut) { + if (!namespaceState || !kindOut) return NTLSTATUS_INVALID_PARAMETER; + *kindOut = NTL_RESOURCE_KIND_INVALID; + KSTATUS status = RssrvNamespaceLock(namespaceState); + if (!KSUCCESS(status)) return status; + RSSRV_NODE* node = RssrvFindExactLocked(namespaceState, path, length); + if (node) *kindOut = RssrvNodeKind(node); + RssrvNamespaceUnlock(namespaceState); + return node ? NTLSTATUS_SUCCESS : NTLSTATUS_NOT_FOUND; +} + +// Returns the lexicographically next immediate child after one optional cursor name. +KSTATUS RssrvNamespaceEnumerate(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + const char* after, + uint32_t afterLength, + RSSRV_NAMESPACE_ENTRY* entryOut) { + if (!namespaceState || !entryOut || (afterLength && !after) || afterLength > NTL_RESOURCE_NAME_MAX) + return NTLSTATUS_INVALID_PARAMETER; + memset(entryOut, 0, sizeof(*entryOut)); + entryOut->Kind = NTL_RESOURCE_KIND_INVALID; + + KSTATUS status = RssrvNamespaceLock(namespaceState); + if (!KSUCCESS(status)) return status; + RSSRV_NODE* directory = RssrvFindExactLocked(namespaceState, path, length); + if (!directory) { + RssrvNamespaceUnlock(namespaceState); + return NTLSTATUS_NOT_FOUND; + } + if (directory->Type != RssrvNodeDirectory) { + entryOut->Kind = RssrvNodeKind(directory); + RssrvNamespaceUnlock(namespaceState); + return NTLSTATUS_NOT_SUPPORTED; + } + + RSSRV_NODE* child = RssrvEnumerateNextLocked(directory, after, afterLength); + if (!child) { + RssrvNamespaceUnlock(namespaceState); + return NTLSTATUS_NO_MORE_FILES; + } + entryOut->Kind = RssrvNodeKind(child); + entryOut->NameLength = child->NameLength; + memcpy(entryOut->Name, child->Name, child->NameLength); + entryOut->Name[child->NameLength] = '\0'; + RssrvNamespaceUnlock(namespaceState); + return NTLSTATUS_SUCCESS; +} + +// Opens a locked view of one exact bound object, following a final namespace symbolic link. +KSTATUS RssrvNamespaceBeginLookup(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + RSSRV_NAMESPACE_BINDING_VIEW* viewOut, + uint32_t* kindOut) { + if (!namespaceState || !viewOut || !kindOut) return NTLSTATUS_INVALID_PARAMETER; + memset(viewOut, 0, sizeof(*viewOut)); + viewOut->Handle = SYS_HANDLE_INVALID; + *kindOut = NTL_RESOURCE_KIND_INVALID; + + KSTATUS status = RssrvNamespaceLock(namespaceState); + if (!KSUCCESS(status)) return status; + RSSRV_NODE* node = RssrvFindExactLocked(namespaceState, path, length); + if (!node) { + RssrvNamespaceUnlock(namespaceState); + return NTLSTATUS_NOT_FOUND; + } + RSSRV_NODE* resolved = (RSSRV_NODE*)0; + status = RssrvResolveExactNodeLocked(namespaceState, node, 0, &resolved); + if (!KSUCCESS(status)) { + RssrvNamespaceUnlock(namespaceState); + return status; + } + node = resolved; + *kindOut = RssrvNodeKind(node); + if (node->Type != RssrvNodeBinding) { + RssrvNamespaceUnlock(namespaceState); + return NTLSTATUS_NOT_SUPPORTED; + } + + viewOut->Namespace = namespaceState; + viewOut->Handle = node->u.Binding.Handle; + viewOut->Kind = node->u.Binding.Kind; + viewOut->ExportRights = node->u.Binding.ExportRights; + viewOut->Locked = 1u; + return NTLSTATUS_SUCCESS; +} + +// Opens a locked view of the first bound object reached while traversing one resource path. +KSTATUS RssrvNamespaceBeginResolve(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + RSSRV_NAMESPACE_BINDING_VIEW* viewOut) { + if (!namespaceState || !viewOut) return NTLSTATUS_INVALID_PARAMETER; + memset(viewOut, 0, sizeof(*viewOut)); + viewOut->Handle = SYS_HANDLE_INVALID; + + KSTATUS status = RssrvNamespaceLock(namespaceState); + if (!KSUCCESS(status)) return status; + RSSRV_NODE* node = (RSSRV_NODE*)0; + uint32_t resolvedLength = 0; + status = RssrvResolveBindingLocked(namespaceState, path, length, &node, &resolvedLength); + if (!KSUCCESS(status)) { + RssrvNamespaceUnlock(namespaceState); + return status; + } + + viewOut->Namespace = namespaceState; + viewOut->Handle = node->u.Binding.Handle; + viewOut->Kind = node->u.Binding.Kind; + viewOut->ExportRights = node->u.Binding.ExportRights; + viewOut->ResolvedLength = resolvedLength; + viewOut->Locked = 1u; + return NTLSTATUS_SUCCESS; +} + +// Releases the namespace lock held by a successful binding view. +void RssrvNamespaceEndBinding(RSSRV_NAMESPACE_BINDING_VIEW* view) { + if (!view || !view->Locked || !view->Namespace) return; + RssrvNamespaceUnlock(view->Namespace); + view->Locked = 0; + view->Namespace = (RSSRV_NAMESPACE*)0; + view->Handle = SYS_HANDLE_INVALID; +} diff --git a/code/user/system/rssrv/resource_service.c b/code/user/system/rssrv/resource_service.c new file mode 100644 index 0000000..f4a72c3 --- /dev/null +++ b/code/user/system/rssrv/resource_service.c @@ -0,0 +1,560 @@ +// RSSRV resource protocol listeners, sessions, request validation, and capability transfer. + +#include "rssrv_internal.h" +#include +#include + +typedef struct RSSRV_SESSION { + RSSRV_RESOURCE_SERVICE* Service; + SYS_HANDLE Endpoint; + uint32_t Administrative; +} RSSRV_SESSION; + +// Replies with one fixed resource-protocol reply and no handle transfer. +static KSTATUS RssrvReplyStatus(SYS_HANDLE endpoint, + uint32_t messageId, + uint32_t operation, + KSTATUS status, + uint32_t kind) { + NTL_RESOURCE_REPLY reply; + memset(&reply, 0, sizeof(reply)); + reply.Size = sizeof(reply); + reply.Version = NTL_RESOURCE_PROTOCOL_VERSION; + reply.Operation = operation; + reply.Flags = NTL_RESOURCE_FLAG_NONE; + reply.Status = status; + reply.Kind = kind; + + SYS_REPLY_PORT_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.EndpointHandle = endpoint; + request.MessageId = messageId; + request.Flags = SYS_PORT_FLAG_NONE; + request.Payload = &reply; + request.PayloadLength = sizeof(reply); + return SysReplyPort(&request); +} + +// Handles one exact capability lookup while retaining the namespace lock through transfer. +static KSTATUS RssrvHandleLookup(RSSRV_RESOURCE_SERVICE* service, + SYS_HANDLE endpoint, + uint32_t messageId, + const NTL_RESOURCE_REQUEST* request, + const char* path) { + if (!request->Rights) + return RssrvReplyStatus(endpoint, messageId, request->Operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + + RSSRV_NAMESPACE_BINDING_VIEW view; + uint32_t kind = NTL_RESOURCE_KIND_INVALID; + KSTATUS status = RssrvNamespaceBeginLookup(service->Namespace, + path, + request->PathLength, + &view, + &kind); + if (!KSUCCESS(status)) + return RssrvReplyStatus(endpoint, messageId, request->Operation, status, kind); + + kind = view.Kind; + if ((request->Rights & ~view.ExportRights) != 0) { + RssrvNamespaceEndBinding(&view); + return RssrvReplyStatus(endpoint, messageId, request->Operation, + NTLSTATUS_ACCESS_DENIED, kind); + } + + NTL_RESOURCE_REPLY reply; + memset(&reply, 0, sizeof(reply)); + reply.Size = sizeof(reply); + reply.Version = NTL_RESOURCE_PROTOCOL_VERSION; + reply.Operation = NTL_RESOURCE_OP_LOOKUP; + reply.Status = NTLSTATUS_SUCCESS; + reply.Kind = kind; + + SYS_PORT_HANDLE_TRANSFER transfer; + transfer.Handle = view.Handle; + transfer.DesiredRights = request->Rights; + transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + + SYS_REPLY_PORT_REQUEST out; + memset(&out, 0, sizeof(out)); + out.Size = sizeof(out); + out.EndpointHandle = endpoint; + out.MessageId = messageId; + out.Flags = SYS_PORT_FLAG_NONE; + out.Payload = &reply; + out.PayloadLength = sizeof(reply); + out.Handles = &transfer; + out.HandleCount = 1; + status = SysReplyPort(&out); + RssrvNamespaceEndBinding(&view); + + // A transfer failure leaves the synchronous call pending, so complete it with the precise status. + if (!KSUCCESS(status)) + return RssrvReplyStatus(endpoint, messageId, request->Operation, status, kind); + return NTLSTATUS_SUCCESS; +} + +// Resolves the first bound namespace object and transfers its traversal authority. +static KSTATUS RssrvHandleResolve(RSSRV_RESOURCE_SERVICE* service, + SYS_HANDLE endpoint, + uint32_t messageId, + const NTL_RESOURCE_REQUEST* request, + const char* path) { + if ((request->Flags & ~NTL_RESOURCE_RESOLVE_FLAG_MODIFY) != 0) + return RssrvReplyStatus(endpoint, messageId, request->Operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + + RSSRV_NAMESPACE_BINDING_VIEW view; + KSTATUS status = RssrvNamespaceBeginResolve(service->Namespace, + path, + request->PathLength, + &view); + if (!KSUCCESS(status)) + return RssrvReplyStatus(endpoint, messageId, request->Operation, + status, NTL_RESOURCE_KIND_INVALID); + + SYS_RIGHTS desiredRights = 0; + if (view.Kind == NTL_RESOURCE_KIND_VOLUME) { + desiredRights = SYS_VOLUME_RIGHT_OPEN; + if (request->Flags & NTL_RESOURCE_RESOLVE_FLAG_MODIFY) + desiredRights |= SYS_VOLUME_RIGHT_MODIFY; + } else if (view.Kind == NTL_RESOURCE_KIND_DIRECTORY) { + desiredRights = SYS_DIR_RIGHT_OPEN; + if (request->Flags & NTL_RESOURCE_RESOLVE_FLAG_MODIFY) + desiredRights |= SYS_DIR_RIGHT_MODIFY; + } else { + uint32_t kind = view.Kind; + RssrvNamespaceEndBinding(&view); + return RssrvReplyStatus(endpoint, messageId, request->Operation, + NTLSTATUS_NOT_SUPPORTED, kind); + } + + uint32_t kind = view.Kind; + if ((desiredRights & ~view.ExportRights) != 0) { + RssrvNamespaceEndBinding(&view); + return RssrvReplyStatus(endpoint, messageId, request->Operation, + NTLSTATUS_ACCESS_DENIED, kind); + } + + NTL_RESOURCE_REPLY reply; + memset(&reply, 0, sizeof(reply)); + reply.Size = sizeof(reply); + reply.Version = NTL_RESOURCE_PROTOCOL_VERSION; + reply.Operation = NTL_RESOURCE_OP_RESOLVE; + reply.Flags = request->Flags; + reply.Status = NTLSTATUS_SUCCESS; + reply.Kind = kind; + reply.ResolvedLength = view.ResolvedLength; + + SYS_PORT_HANDLE_TRANSFER transfer; + transfer.Handle = view.Handle; + transfer.DesiredRights = desiredRights; + transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + + SYS_REPLY_PORT_REQUEST out; + memset(&out, 0, sizeof(out)); + out.Size = sizeof(out); + out.EndpointHandle = endpoint; + out.MessageId = messageId; + out.Flags = SYS_PORT_FLAG_NONE; + out.Payload = &reply; + out.PayloadLength = sizeof(reply); + out.Handles = &transfer; + out.HandleCount = 1; + status = SysReplyPort(&out); + RssrvNamespaceEndBinding(&view); + if (!KSUCCESS(status)) + return RssrvReplyStatus(endpoint, messageId, request->Operation, status, kind); + return NTLSTATUS_SUCCESS; +} + +// Handles one exact metadata query without following the final symbolic link. +static KSTATUS RssrvHandleQuery(RSSRV_RESOURCE_SERVICE* service, + SYS_HANDLE endpoint, + uint32_t messageId, + const NTL_RESOURCE_REQUEST* request, + const char* path) { + uint32_t kind = NTL_RESOURCE_KIND_INVALID; + KSTATUS status = RssrvNamespaceQuery(service->Namespace, path, request->PathLength, &kind); + return RssrvReplyStatus(endpoint, messageId, request->Operation, status, kind); +} + +// Returns one immediate namespace child after the supplied bytewise cursor name. +static KSTATUS RssrvHandleEnumerate(RSSRV_RESOURCE_SERVICE* service, + SYS_HANDLE endpoint, + uint32_t messageId, + const NTL_RESOURCE_REQUEST* request, + const char* path, + const char* after) { + RSSRV_NAMESPACE_ENTRY entry; + KSTATUS status = RssrvNamespaceEnumerate(service->Namespace, + path, + request->PathLength, + after, + request->AuxLength, + &entry); + if (!KSUCCESS(status)) + return RssrvReplyStatus(endpoint, messageId, request->Operation, status, entry.Kind); + + uint8_t payload[sizeof(NTL_RESOURCE_REPLY) + NTL_RESOURCE_NAME_MAX]; + NTL_RESOURCE_REPLY reply; + memset(&reply, 0, sizeof(reply)); + reply.Size = sizeof(reply); + reply.Version = NTL_RESOURCE_PROTOCOL_VERSION; + reply.Operation = NTL_RESOURCE_OP_ENUMERATE; + reply.Status = NTLSTATUS_SUCCESS; + reply.Kind = entry.Kind; + reply.NameLength = entry.NameLength; + memcpy(payload, &reply, sizeof(reply)); + memcpy(payload + sizeof(reply), entry.Name, entry.NameLength); + uint32_t payloadLength = (uint32_t)sizeof(reply) + entry.NameLength; + + SYS_REPLY_PORT_REQUEST out; + memset(&out, 0, sizeof(out)); + out.Size = sizeof(out); + out.EndpointHandle = endpoint; + out.MessageId = messageId; + out.Flags = SYS_PORT_FLAG_NONE; + out.Payload = payload; + out.PayloadLength = payloadLength; + return SysReplyPort(&out); +} + +// Handles one administrative bind. On success the namespace takes ownership of receivedHandle. +static KSTATUS RssrvHandleBind(RSSRV_RESOURCE_SERVICE* service, + SYS_HANDLE endpoint, + uint32_t messageId, + const NTL_RESOURCE_REQUEST* request, + const char* path, + uint32_t administrative, + SYS_HANDLE receivedHandle, + uint32_t* consumedOut) { + if (!consumedOut) return NTLSTATUS_INVALID_PARAMETER; + *consumedOut = 0; + if (!administrative || RssrvVolumesOwnPath(path, request->PathLength)) + return RssrvReplyStatus(endpoint, messageId, request->Operation, + NTLSTATUS_ACCESS_DENIED, NTL_RESOURCE_KIND_INVALID); + KSTATUS bindStatus = RssrvNamespaceBindOwned(service->Namespace, + path, + request->PathLength, + request->Kind, + receivedHandle, + request->ExportRights); + if (KSUCCESS(bindStatus)) *consumedOut = 1; + KSTATUS replyStatus = RssrvReplyStatus(endpoint, messageId, request->Operation, bindStatus, + KSUCCESS(bindStatus) ? request->Kind : NTL_RESOURCE_KIND_INVALID); + return KSUCCESS(replyStatus) ? bindStatus : replyStatus; +} + +// Dispatches one received resource request and returns whether a received handle was consumed. +static uint32_t RssrvDispatchRequest(RSSRV_RESOURCE_SERVICE* service, + SYS_HANDLE endpoint, + uint32_t administrative, + const uint8_t* payload, + uint32_t payloadLength, + const SYS_PORT_MESSAGE_INFO* info, + SYS_HANDLE receivedHandle) { + uint32_t operation = 0; + if (!service || payloadLength < sizeof(NTL_RESOURCE_REQUEST)) { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + return 0; + } + + NTL_RESOURCE_REQUEST request; + memcpy(&request, payload, sizeof(request)); + operation = request.Operation; + size_t expectedLength = sizeof(request) + (size_t)request.PathLength + (size_t)request.AuxLength; + if (request.Size != sizeof(request) || request.Version != NTL_RESOURCE_PROTOCOL_VERSION || + request.PathLength == 0 || expectedLength != payloadLength || expectedLength > SYS_PORT_MAX_INLINE_BYTES) { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + return 0; + } + const char* path = (const char*)(payload + sizeof(request)); + const char* aux = path + request.PathLength; + KSTATUS status = RssrvNamespaceValidatePath(path, request.PathLength); + if (!KSUCCESS(status)) { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, status, NTL_RESOURCE_KIND_INVALID); + return 0; + } + + switch (operation) { + case NTL_RESOURCE_OP_LOOKUP: + if (request.Flags != NTL_RESOURCE_FLAG_NONE || request.AuxLength != 0 || info->HandleCount != 0 || + request.ExportRights != 0 || request.Kind != NTL_RESOURCE_KIND_INVALID) { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + return 0; + } + (void)RssrvHandleLookup(service, endpoint, info->MessageId, &request, path); + return 0; + case NTL_RESOURCE_OP_QUERY: + if (request.Flags != NTL_RESOURCE_FLAG_NONE || request.AuxLength != 0 || info->HandleCount != 0 || + request.Rights != 0 || request.ExportRights != 0 || request.Kind != NTL_RESOURCE_KIND_INVALID) { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + return 0; + } + (void)RssrvHandleQuery(service, endpoint, info->MessageId, &request, path); + return 0; + case NTL_RESOURCE_OP_RESOLVE: + if (request.AuxLength != 0 || info->HandleCount != 0 || request.Rights != 0 || + request.ExportRights != 0 || request.Kind != NTL_RESOURCE_KIND_INVALID || + (request.Flags & ~NTL_RESOURCE_RESOLVE_FLAG_MODIFY) != 0) { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + return 0; + } + (void)RssrvHandleResolve(service, endpoint, info->MessageId, &request, path); + return 0; + case NTL_RESOURCE_OP_ENUMERATE: + if (request.Flags != NTL_RESOURCE_FLAG_NONE || info->HandleCount != 0 || request.Rights != 0 || + request.ExportRights != 0 || request.Kind != NTL_RESOURCE_KIND_INVALID || + request.AuxLength > NTL_RESOURCE_NAME_MAX) { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + return 0; + } + for (uint32_t i = 0; i < request.AuxLength; ++i) { + if (aux[i] == '/' || aux[i] == '\\' || aux[i] == '\0') { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + return 0; + } + } + (void)RssrvHandleEnumerate(service, endpoint, info->MessageId, &request, path, aux); + return 0; + case NTL_RESOURCE_OP_CREATE_DIRECTORY: + if (!administrative || request.Flags != NTL_RESOURCE_FLAG_NONE || request.AuxLength != 0 || + info->HandleCount != 0 || request.Rights != 0 || request.ExportRights != 0 || + request.Kind != NTL_RESOURCE_KIND_INVALID || RssrvVolumesOwnPath(path, request.PathLength)) { + status = administrative ? NTLSTATUS_INVALID_PARAMETER : NTLSTATUS_ACCESS_DENIED; + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, status, NTL_RESOURCE_KIND_INVALID); + return 0; + } + status = RssrvNamespaceCreateDirectory(service->Namespace, path, request.PathLength); + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, status, + KSUCCESS(status) ? NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY : NTL_RESOURCE_KIND_INVALID); + return 0; + case NTL_RESOURCE_OP_CREATE_SYMBOLIC_LINK: + if (!administrative || request.Flags != NTL_RESOURCE_FLAG_NONE || request.AuxLength == 0 || + info->HandleCount != 0 || request.Rights != 0 || request.ExportRights != 0 || + request.Kind != NTL_RESOURCE_KIND_INVALID || RssrvVolumesOwnPath(path, request.PathLength)) { + status = administrative ? NTLSTATUS_INVALID_PARAMETER : NTLSTATUS_ACCESS_DENIED; + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, status, NTL_RESOURCE_KIND_INVALID); + return 0; + } + status = RssrvNamespaceValidatePath(aux, request.AuxLength); + if (KSUCCESS(status)) + status = RssrvNamespaceCreateSymbolicLink(service->Namespace, + path, + request.PathLength, + aux, + request.AuxLength); + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, status, + KSUCCESS(status) ? NTL_RESOURCE_KIND_SYMBOLIC_LINK : NTL_RESOURCE_KIND_INVALID); + return 0; + case NTL_RESOURCE_OP_BIND: + if (request.Flags != NTL_RESOURCE_FLAG_NONE || request.AuxLength != 0 || info->HandleCount != 1 || + !(request.Rights & SYS_RIGHT_TRANSFER) || request.ExportRights == 0 || + (request.ExportRights & ~request.Rights) != 0 || request.Kind == NTL_RESOURCE_KIND_INVALID || + request.Kind == NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY || request.Kind == NTL_RESOURCE_KIND_SYMBOLIC_LINK || + receivedHandle == SYS_HANDLE_INVALID) { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + return 0; + } + { + uint32_t consumed = 0; + (void)RssrvHandleBind(service, endpoint, info->MessageId, &request, path, administrative, + receivedHandle, &consumed); + return consumed; + } + case NTL_RESOURCE_OP_UNBIND: + if (request.Flags != NTL_RESOURCE_FLAG_NONE || request.AuxLength != 0 || info->HandleCount != 0 || + request.Rights != 0 || request.ExportRights != 0 || request.Kind != NTL_RESOURCE_KIND_INVALID) { + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); + return 0; + } + status = administrative && !RssrvVolumesOwnPath(path, request.PathLength) + ? RssrvNamespaceUnbind(service->Namespace, path, request.PathLength) + : NTLSTATUS_ACCESS_DENIED; + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, status, NTL_RESOURCE_KIND_INVALID); + return 0; + default: + (void)RssrvReplyStatus(endpoint, info->MessageId, operation, + NTLSTATUS_NOT_SUPPORTED, NTL_RESOURCE_KIND_INVALID); + return 0; + } +} + +// Serves one connected resource session until the peer disconnects. +static void RssrvSessionThread(uintptr_t argument) { + RSSRV_SESSION* session = (RSSRV_SESSION*)argument; + if (!session || !session->Service) { + if (session) free(session); + SysExitThread(1); + return; + } + RSSRV_RESOURCE_SERVICE* service = session->Service; + SYS_HANDLE endpoint = session->Endpoint; + uint32_t administrative = session->Administrative; + RssrvSetCurrentThreadName(administrative ? "rssrv.session.admin" : "rssrv.session.public"); + free(session); + + uint8_t payload[SYS_PORT_MAX_INLINE_BYTES]; + SYS_HANDLE handles[SYS_PORT_MAX_TRANSFER_HANDLES]; + for (;;) { + memset(handles, 0, sizeof(handles)); + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = endpoint; + receive.Flags = SYS_PORT_FLAG_NONE; + receive.Payload = payload; + receive.PayloadCapacity = sizeof(payload); + receive.HandlesOut = handles; + receive.HandleCapacity = SYS_PORT_MAX_TRANSFER_HANDLES; + receive.InfoOut = &info; + receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysReceivePort(&receive); + if (status == NTLSTATUS_PORT_DISCONNECTED) + break; + if (!KSUCCESS(status)) + break; + + uint32_t consumed = 0; + if (info.Type == SYS_PORT_MESSAGE_REQUEST) + consumed = RssrvDispatchRequest(service, + endpoint, + administrative, + payload, + info.PayloadLength, + &info, + info.HandleCount == 1 ? handles[0] : SYS_HANDLE_INVALID); + for (uint32_t i = consumed ? 1u : 0u; i < info.HandleCount; ++i) { + if (handles[i] != SYS_HANDLE_INVALID) (void)SysCloseHandle(handles[i]); + } + } + + (void)SysCloseHandle(endpoint); + SysExitThread(0); +} + +// Accepts sessions from one listener and gives every connection its own worker thread. +static void RssrvListenerThread(uintptr_t argument) { + RSSRV_LISTENER_WORKER* worker = (RSSRV_LISTENER_WORKER*)argument; + if (!worker || !worker->Service) { + SysExitThread(1); + return; + } + RssrvSetCurrentThreadName(worker->Administrative ? "rssrv.listen.admin" : "rssrv.listen.public"); + for (;;) { + SYS_HANDLE endpoint = SYS_HANDLE_INVALID; + SYS_ACCEPT_PORT_REQUEST accept; + memset(&accept, 0, sizeof(accept)); + accept.Size = sizeof(accept); + accept.ListenerHandle = worker->Listener; + accept.Flags = SYS_PORT_ACCEPT_FLAG_NONE; + accept.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + accept.EndpointHandleOut = &endpoint; + KSTATUS status = SysAcceptPort(&accept); + if (!KSUCCESS(status)) + break; + + RSSRV_SESSION* session = (RSSRV_SESSION*)malloc(sizeof(*session)); + if (!session) { + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + continue; + } + session->Service = worker->Service; + session->Endpoint = endpoint; + session->Administrative = worker->Administrative; + + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)RssrvSessionThread; + create.Argument = (uintptr_t)session; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + status = SysCreateThread(&create); + if (!KSUCCESS(status)) { + free(session); + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + } + } + SysExitThread(0); +} + +// Creates one listener using the service's bounded default backlog. +static KSTATUS RssrvCreateListener(SYS_HANDLE* listenerOut) { + SYS_CREATE_PORT_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Flags = SYS_PORT_FLAG_NONE; + create.Backlog = RSSRV_LISTENER_BACKLOG; + create.HandleOut = listenerOut; + return SysCreatePort(&create); +} + +// Starts one resource listener worker thread. +static KSTATUS RssrvStartListenerWorker(RSSRV_RESOURCE_SERVICE* service, + RSSRV_LISTENER_WORKER* worker, + SYS_HANDLE listener, + uint32_t administrative) { + if (!service || !worker || listener == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + worker->Service = service; + worker->Listener = listener; + worker->Administrative = administrative; + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)RssrvListenerThread; + create.Argument = (uintptr_t)worker; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + return SysCreateThread(&create); +} + +// Initializes the public/admin resource protocol component around existing namespace state. +KSTATUS RssrvResourceInitialize(RSSRV_RESOURCE_SERVICE* service, + RSSRV_NAMESPACE* namespaceState) { + if (!service || !namespaceState) + return NTLSTATUS_INVALID_PARAMETER; + memset(service, 0, sizeof(*service)); + service->Namespace = namespaceState; + service->PublicListener = SYS_HANDLE_INVALID; + service->AdminListener = SYS_HANDLE_INVALID; + service->PublicWorker.Listener = SYS_HANDLE_INVALID; + service->AdminWorker.Listener = SYS_HANDLE_INVALID; + return NTLSTATUS_SUCCESS; +} + +// Creates the resource listeners and starts their accept workers. +KSTATUS RssrvResourceStart(RSSRV_RESOURCE_SERVICE* service) { + if (!service || !service->Namespace) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = RssrvCreateListener(&service->PublicListener); + if (!KSUCCESS(status)) return status; + status = RssrvCreateListener(&service->AdminListener); + if (!KSUCCESS(status)) { + (void)SysCloseHandle(service->PublicListener); + service->PublicListener = SYS_HANDLE_INVALID; + return status; + } + status = RssrvStartListenerWorker(service, &service->PublicWorker, service->PublicListener, 0); + if (!KSUCCESS(status)) return status; + status = RssrvStartListenerWorker(service, &service->AdminWorker, service->AdminListener, 1); + return status; +} diff --git a/code/user/system/rssrv/rssrv.c b/code/user/system/rssrv/rssrv.c index ff598ff..7087aee 100644 --- a/code/user/system/rssrv/rssrv.c +++ b/code/user/system/rssrv/rssrv.c @@ -1,817 +1,28 @@ -// Native resource namespace service. RSSRV owns global names and transfers capabilities; it is not a file data path. +// Native resource service bootstrap and lifetime. RSSRV is one critical server with internally owned components. +#include "rssrv_internal.h" #include -#include #include #include #include #include -#include -#include #include -#define RSSRV_LISTENER_BACKLOG 16u +static RSSRV_STATE gRssrv; -// One namespace binding. Path storage follows the fixed header in the same allocation. -typedef struct RSSRV_ENTRY { - struct RSSRV_ENTRY* Next; - SYS_HANDLE Handle; - uint32_t Kind; - SYS_RIGHTS ExportRights; - uint32_t PathLength; - char Path[1]; -} RSSRV_ENTRY; - -typedef struct RSSRV_SESSION { - SYS_HANDLE Endpoint; - uint32_t Administrative; -} RSSRV_SESSION; - -typedef struct RSSRV_LISTENER_WORKER { - SYS_HANDLE Listener; - uint32_t Administrative; -} RSSRV_LISTENER_WORKER; - -typedef struct RSSRV_VOLUME_PUBLICATION { - struct RSSRV_VOLUME_PUBLICATION* Next; - uint32_t InstanceId; - uint32_t NamespaceId; - uint32_t PathLength; - char Path[48]; -} RSSRV_VOLUME_PUBLICATION; - -static SYS_HANDLE gNamespaceMutex = SYS_HANDLE_INVALID; -static RSSRV_ENTRY* gNamespace = (RSSRV_ENTRY*)0; -static SYS_HANDLE gPublicListener = SYS_HANDLE_INVALID; -static SYS_HANDLE gAdminListener = SYS_HANDLE_INVALID; -static RSSRV_LISTENER_WORKER gPublicWorker; -static RSSRV_LISTENER_WORKER gAdminWorker; -static RSSRV_VOLUME_PUBLICATION* gPublishedVolumes = (RSSRV_VOLUME_PUBLICATION*)0; -static uint32_t gNextHarddiskVolumeId = 1u; - -// Exits without depending on a compiler runtime return path. -static void RssrvExit(int code) { - for (;;) SysExitProcess(code); -} - -// Acquires the namespace mutex. -static KSTATUS RssrvLockNamespace(void) { - return SysWait(gNamespaceMutex, SYS_WAIT_FLAG_NONE); -} - -// Releases the namespace mutex. -static void RssrvUnlockNamespace(void) { - (void)SysReleaseMutex(gNamespaceMutex); -} - -// Validates one canonical absolute native resource path. -static KSTATUS RssrvValidatePath(const char* path, uint32_t length) { - if (!path || length < 2 || length > NTL_RESOURCE_PATH_MAX || path[0] != '/' || path[length - 1] == '/') - return NTLSTATUS_INVALID_PARAMETER; - - uint32_t componentStart = 1; - for (uint32_t i = 1; i <= length; ++i) { - if (i < length) { - char c = path[i]; - if (c == '\\' || c == '\0') - return NTLSTATUS_INVALID_PARAMETER; - if (c != '/') - continue; - } - uint32_t componentLength = i - componentStart; - if (componentLength == 0 || - (componentLength == 1 && path[componentStart] == '.') || - (componentLength == 2 && path[componentStart] == '.' && path[componentStart + 1] == '.')) - return NTLSTATUS_INVALID_PARAMETER; - componentStart = i + 1; - } - return NTLSTATUS_SUCCESS; -} - -// Returns whether a path belongs to the RSSRV-owned automatic mounted-volume naming family. -static int RssrvIsManagedVolumePath(const char* path, uint32_t length) { - static const char prefix[] = "/Device/HarddiskVolume"; - uint32_t prefixLength = (uint32_t)(sizeof(prefix) - 1u); - return path && length >= prefixLength && memcmp(path, prefix, prefixLength) == 0; -} - -// Finds one exact binding while the namespace mutex is held. -static RSSRV_ENTRY* RssrvFindLocked(const char* path, uint32_t length, RSSRV_ENTRY*** linkOut) { - RSSRV_ENTRY** link = &gNamespace; - while (*link) { - RSSRV_ENTRY* entry = *link; - if (entry->PathLength == length && memcmp(entry->Path, path, length) == 0) { - if (linkOut) *linkOut = link; - return entry; - } - link = &entry->Next; - } - if (linkOut) *linkOut = link; - return (RSSRV_ENTRY*)0; -} - -// Finds the longest namespace binding that is an exact component prefix of path. -static RSSRV_ENTRY* RssrvFindPrefixLocked(const char* path, uint32_t length) { - RSSRV_ENTRY* best = (RSSRV_ENTRY*)0; - for (RSSRV_ENTRY* entry = gNamespace; entry; entry = entry->Next) { - if (entry->PathLength > length) - continue; - if (memcmp(entry->Path, path, entry->PathLength) != 0) - continue; - if (entry->PathLength != length && path[entry->PathLength] != '/') - continue; - if (!best || entry->PathLength > best->PathLength) - best = entry; - } - return best; -} - -// Takes ownership of one handle and binds it to a new exact resource name. -static KSTATUS RssrvBindOwned(const char* path, - uint32_t length, - uint32_t kind, - SYS_HANDLE handle, - SYS_RIGHTS exportRights) { - if (kind == NTL_RESOURCE_KIND_INVALID || handle == SYS_HANDLE_INVALID || !exportRights) - return NTLSTATUS_INVALID_PARAMETER; - KSTATUS status = RssrvValidatePath(path, length); - if (!KSUCCESS(status)) return status; - - size_t bytes = offsetof(RSSRV_ENTRY, Path) + (size_t)length + 1u; - RSSRV_ENTRY* entry = (RSSRV_ENTRY*)malloc(bytes); - if (!entry) return NTLSTATUS_NO_MEMORY; - memset(entry, 0, bytes); - entry->Handle = handle; - entry->Kind = kind; - entry->ExportRights = exportRights; - entry->PathLength = length; - memcpy(entry->Path, path, length); - entry->Path[length] = '\0'; - - status = RssrvLockNamespace(); - if (!KSUCCESS(status)) { - free(entry); - return status; - } - if (RssrvFindLocked(path, length, (RSSRV_ENTRY***)0)) { - RssrvUnlockNamespace(); - free(entry); - return NTLSTATUS_OBJECT_NAME_COLLISION; - } - entry->Next = gNamespace; - gNamespace = entry; - RssrvUnlockNamespace(); - return NTLSTATUS_SUCCESS; -} - -// Removes one binding and closes the namespace's retained handle. -static KSTATUS RssrvUnbind(const char* path, uint32_t length) { - KSTATUS status = RssrvValidatePath(path, length); - if (!KSUCCESS(status)) return status; - status = RssrvLockNamespace(); - if (!KSUCCESS(status)) return status; - - RSSRV_ENTRY** link = (RSSRV_ENTRY**)0; - RSSRV_ENTRY* entry = RssrvFindLocked(path, length, &link); - if (!entry) { - RssrvUnlockNamespace(); - return NTLSTATUS_NOT_FOUND; - } - *link = entry->Next; - RssrvUnlockNamespace(); - (void)SysCloseHandle(entry->Handle); - free(entry); - return NTLSTATUS_SUCCESS; -} - -// Finds one published mounted volume by kernel instance identity. -static RSSRV_VOLUME_PUBLICATION* RssrvFindPublishedVolume(uint32_t instanceId) { - for (RSSRV_VOLUME_PUBLICATION* publication = gPublishedVolumes; publication; publication = publication->Next) { - if (publication->InstanceId == instanceId) - return publication; - } - return (RSSRV_VOLUME_PUBLICATION*)0; -} - -// Returns whether one kernel volume instance is present in the current manager snapshot. -static int RssrvSnapshotContainsVolume(const SYS_VOLUME_INFO* volumes, uint32_t count, uint32_t instanceId) { - for (uint32_t i = 0; i < count; ++i) { - if (volumes[i].InstanceId == instanceId) - return 1; - } - return 0; -} - -// Builds one RSSRV-local /Device/HarddiskVolumeN name without introducing a formatting-runtime dependency. -static KSTATUS RssrvBuildHarddiskVolumePath(uint32_t namespaceId, char* path, uint32_t capacity, uint32_t* lengthOut) { - static const char prefix[] = "/Device/HarddiskVolume"; - if (!namespaceId || !path || !lengthOut) - return NTLSTATUS_INVALID_PARAMETER; - - uint32_t prefixLength = (uint32_t)(sizeof(prefix) - 1u); - char digits[10]; - uint32_t digitCount = 0; - uint32_t value = namespaceId; - do { - digits[digitCount++] = (char)('0' + (value % 10u)); - value /= 10u; - } while (value && digitCount < (uint32_t)sizeof(digits)); - - if (prefixLength + digitCount + 1u > capacity) - return NTLSTATUS_BUFFER_TOO_SMALL; - memcpy(path, prefix, prefixLength); - for (uint32_t i = 0; i < digitCount; ++i) - path[prefixLength + i] = digits[digitCount - 1u - i]; - path[prefixLength + digitCount] = '\0'; - *lengthOut = prefixLength + digitCount; - return NTLSTATUS_SUCCESS; -} - -// Takes a stable manager snapshot, growing the userspace buffer if the mounted-volume count changes. -static KSTATUS RssrvQueryVolumes(SYS_HANDLE manager, - SYS_VOLUME_INFO** volumesOut, - uint32_t* countOut, - uint64_t* generationOut) { - if (!volumesOut || !countOut || !generationOut) - return NTLSTATUS_INVALID_PARAMETER; - *volumesOut = (SYS_VOLUME_INFO*)0; - *countOut = 0; - *generationOut = 0; - - SYS_VOLUME_INFO* volumes = (SYS_VOLUME_INFO*)0; - uint32_t capacity = 0; - for (;;) { - uint32_t count = 0; - uint64_t generation = 0; - SYS_QUERY_VOLUMES_REQUEST query; - memset(&query, 0, sizeof(query)); - query.Size = sizeof(query); - query.ManagerHandle = manager; - query.Flags = SYS_VOLUME_QUERY_FLAG_NONE; - query.Capacity = capacity; - query.Volumes = volumes; - query.CountOut = &count; - query.GenerationOut = &generation; - KSTATUS status = SysQueryVolumes(&query); - if (status == NTLSTATUS_BUFFER_TOO_SMALL) { - if (volumes) free(volumes); - volumes = (SYS_VOLUME_INFO*)0; - capacity = count; - if (capacity) { - volumes = (SYS_VOLUME_INFO*)malloc((size_t)capacity * sizeof(*volumes)); - if (!volumes) - return NTLSTATUS_NO_MEMORY; - memset(volumes, 0, (size_t)capacity * sizeof(*volumes)); - } - continue; - } - if (!KSUCCESS(status)) { - if (volumes) free(volumes); - return status; - } - for (uint32_t i = 0; i < count; ++i) { - if (volumes[i].Size != sizeof(SYS_VOLUME_INFO) || - volumes[i].Version != SYS_VOLUME_ENUM_VERSION || !volumes[i].InstanceId || - volumes[i].Flags != 0) { - if (volumes) free(volumes); - return NTLSTATUS_INVALID_PARAMETER; - } - } - *volumesOut = volumes; - *countOut = count; - *generationOut = generation; - return NTLSTATUS_SUCCESS; - } -} - -// Reconciles the RSSRV object namespace with the kernel's mounted-volume snapshot. -static KSTATUS RssrvSyncVolumes(SYS_HANDLE manager, uint64_t* generationOut) { - if (!generationOut) - return NTLSTATUS_INVALID_PARAMETER; - - SYS_VOLUME_INFO* volumes = (SYS_VOLUME_INFO*)0; - uint32_t count = 0; - uint64_t generation = 0; - KSTATUS status = RssrvQueryVolumes(manager, &volumes, &count, &generation); - if (!KSUCCESS(status)) - return status; - - RSSRV_VOLUME_PUBLICATION** link = &gPublishedVolumes; - while (*link) { - RSSRV_VOLUME_PUBLICATION* publication = *link; - if (RssrvSnapshotContainsVolume(volumes, count, publication->InstanceId)) { - link = &publication->Next; - continue; - } - status = RssrvUnbind(publication->Path, publication->PathLength); - if (!KSUCCESS(status) && status != NTLSTATUS_NOT_FOUND) { - free(volumes); - return status; - } - *link = publication->Next; - free(publication); - } - - for (uint32_t i = 0; i < count; ++i) { - if (RssrvFindPublishedVolume(volumes[i].InstanceId)) - continue; - - SYS_HANDLE volume = SYS_HANDLE_INVALID; - status = SysOpenVolume(manager, volumes[i].InstanceId, - SYS_RIGHT_QUERY | SYS_RIGHT_TRANSFER | - SYS_VOLUME_RIGHT_OPEN | SYS_VOLUME_RIGHT_MODIFY, - &volume); - if (status == NTLSTATUS_NOT_FOUND || status == NTLSTATUS_OBJECT_DEAD) - continue; - if (!KSUCCESS(status)) { - free(volumes); - return status; - } - - RSSRV_VOLUME_PUBLICATION* publication = - (RSSRV_VOLUME_PUBLICATION*)malloc(sizeof(*publication)); - if (!publication) { - (void)SysCloseHandle(volume); - free(volumes); - return NTLSTATUS_NO_MEMORY; - } - memset(publication, 0, sizeof(*publication)); - publication->InstanceId = volumes[i].InstanceId; - publication->NamespaceId = gNextHarddiskVolumeId; - status = RssrvBuildHarddiskVolumePath(publication->NamespaceId, - publication->Path, - (uint32_t)sizeof(publication->Path), - &publication->PathLength); - if (!KSUCCESS(status)) { - free(publication); - (void)SysCloseHandle(volume); - free(volumes); - return status; - } - - status = RssrvBindOwned(publication->Path, publication->PathLength, - NTL_RESOURCE_KIND_VOLUME, volume, - SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN | SYS_VOLUME_RIGHT_MODIFY); - if (!KSUCCESS(status)) { - free(publication); - (void)SysCloseHandle(volume); - free(volumes); - return status; - } - publication->Next = gPublishedVolumes; - gPublishedVolumes = publication; - gNextHarddiskVolumeId++; - if (!gNextHarddiskVolumeId) { - free(volumes); - return NTLSTATUS_LIMIT_REACHED; - } - } - - if (volumes) free(volumes); - *generationOut = generation; - return NTLSTATUS_SUCCESS; -} - -// Keeps automatic /Device/HarddiskVolumeN bindings synchronized with mounted IO_VOLUME instances. -static void RssrvVolumeWorker(uintptr_t argument) { - SYS_HANDLE manager = (SYS_HANDLE)argument; - for (;;) { - uint64_t generation = 0; - KSTATUS status = RssrvSyncVolumes(manager, &generation); - if (!KSUCCESS(status)) - RssrvExit(12); - status = SysWaitVolumeChange(manager, generation, SYS_WAIT_TIMEOUT_INFINITE); - if (!KSUCCESS(status)) - RssrvExit(12); - } -} - -// Starts the mounted-volume namespace publication worker. -static KSTATUS RssrvStartVolumeWorker(SYS_HANDLE manager) { - uint32_t threadId = 0; - SYS_CREATE_THREAD_REQUEST create; - memset(&create, 0, sizeof(create)); - create.Size = sizeof(create); - create.Entry = (uintptr_t)RssrvVolumeWorker; - create.Argument = (uintptr_t)manager; - create.Priority = 1; - create.Flags = SYS_CREATE_THREAD_FLAG_NONE; - create.ThreadIdOut = &threadId; - return SysCreateThread(&create); -} - -// Replies with one fixed resource-protocol reply and no handle transfer. -static KSTATUS RssrvReplyStatus(SYS_HANDLE endpoint, - uint32_t messageId, - uint32_t operation, - KSTATUS status, - uint32_t kind) { - NTL_RESOURCE_REPLY reply; - memset(&reply, 0, sizeof(reply)); - reply.Size = sizeof(reply); - reply.Version = NTL_RESOURCE_PROTOCOL_VERSION; - reply.Operation = operation; - reply.Flags = NTL_RESOURCE_FLAG_NONE; - reply.Status = status; - reply.Kind = kind; - - SYS_REPLY_PORT_REQUEST request; +// Sets a best-effort diagnostic name for the current RSSRV thread. +void RssrvSetCurrentThreadName(const char* name) { + SYS_SET_THREAD_NAME_REQUEST request; memset(&request, 0, sizeof(request)); request.Size = sizeof(request); - request.EndpointHandle = endpoint; - request.MessageId = messageId; - request.Flags = SYS_PORT_FLAG_NONE; - request.Payload = &reply; - request.PayloadLength = sizeof(reply); - return SysReplyPort(&request); + request.ThreadHandle = SYS_HANDLE_INVALID; + request.Name = SysSvFromCstr(name); + (void)SysSetThreadName(&request); } -// Handles one lookup while retaining the namespace mutex through capability transfer. -static KSTATUS RssrvHandleLookup(SYS_HANDLE endpoint, - uint32_t messageId, - const NTL_RESOURCE_REQUEST* request, - const char* path) { - if (!request->Rights) - return RssrvReplyStatus(endpoint, messageId, request->Operation, - NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); - - KSTATUS status = RssrvLockNamespace(); - if (!KSUCCESS(status)) - return RssrvReplyStatus(endpoint, messageId, request->Operation, status, NTL_RESOURCE_KIND_INVALID); - RSSRV_ENTRY* entry = RssrvFindLocked(path, request->PathLength, (RSSRV_ENTRY***)0); - if (!entry) { - RssrvUnlockNamespace(); - return RssrvReplyStatus(endpoint, messageId, request->Operation, - NTLSTATUS_NOT_FOUND, NTL_RESOURCE_KIND_INVALID); - } - - uint32_t kind = entry->Kind; - NTL_RESOURCE_REPLY reply; - memset(&reply, 0, sizeof(reply)); - reply.Size = sizeof(reply); - reply.Version = NTL_RESOURCE_PROTOCOL_VERSION; - reply.Operation = NTL_RESOURCE_OP_LOOKUP; - reply.Status = NTLSTATUS_SUCCESS; - reply.Kind = kind; - - SYS_PORT_HANDLE_TRANSFER transfer; - transfer.Handle = entry->Handle; - if ((request->Rights & ~entry->ExportRights) != 0) { - RssrvUnlockNamespace(); - return RssrvReplyStatus(endpoint, messageId, request->Operation, - NTLSTATUS_ACCESS_DENIED, kind); - } - - transfer.DesiredRights = request->Rights; - transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; - - SYS_REPLY_PORT_REQUEST out; - memset(&out, 0, sizeof(out)); - out.Size = sizeof(out); - out.EndpointHandle = endpoint; - out.MessageId = messageId; - out.Flags = SYS_PORT_FLAG_NONE; - out.Payload = &reply; - out.PayloadLength = sizeof(reply); - out.Handles = &transfer; - out.HandleCount = 1; - status = SysReplyPort(&out); - RssrvUnlockNamespace(); - - // A rights mismatch leaves the synchronous call pending, so return the precise error without a handle. - if (!KSUCCESS(status)) - return RssrvReplyStatus(endpoint, messageId, request->Operation, status, kind); - return NTLSTATUS_SUCCESS; -} - -// Resolves the longest bound resource prefix and transfers traversal authority. -static KSTATUS RssrvHandleResolve(SYS_HANDLE endpoint, - uint32_t messageId, - const NTL_RESOURCE_REQUEST* request, - const char* path) { - if ((request->Flags & ~NTL_RESOURCE_RESOLVE_FLAG_MODIFY) != 0) - return RssrvReplyStatus(endpoint, messageId, request->Operation, - NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); - - KSTATUS status = RssrvLockNamespace(); - if (!KSUCCESS(status)) - return RssrvReplyStatus(endpoint, messageId, request->Operation, status, NTL_RESOURCE_KIND_INVALID); - - RSSRV_ENTRY* entry = RssrvFindPrefixLocked(path, request->PathLength); - if (!entry) { - RssrvUnlockNamespace(); - return RssrvReplyStatus(endpoint, messageId, request->Operation, - NTLSTATUS_NOT_FOUND, NTL_RESOURCE_KIND_INVALID); - } - - SYS_RIGHTS desiredRights = 0; - if (entry->Kind == NTL_RESOURCE_KIND_VOLUME) { - desiredRights = SYS_VOLUME_RIGHT_OPEN; - if (request->Flags & NTL_RESOURCE_RESOLVE_FLAG_MODIFY) - desiredRights |= SYS_VOLUME_RIGHT_MODIFY; - } else if (entry->Kind == NTL_RESOURCE_KIND_DIRECTORY) { - desiredRights = SYS_DIR_RIGHT_OPEN; - if (request->Flags & NTL_RESOURCE_RESOLVE_FLAG_MODIFY) - desiredRights |= SYS_DIR_RIGHT_MODIFY; - } else { - uint32_t kind = entry->Kind; - RssrvUnlockNamespace(); - return RssrvReplyStatus(endpoint, messageId, request->Operation, - NTLSTATUS_NOT_SUPPORTED, kind); - } - - if ((desiredRights & ~entry->ExportRights) != 0) { - uint32_t kind = entry->Kind; - RssrvUnlockNamespace(); - return RssrvReplyStatus(endpoint, messageId, request->Operation, - NTLSTATUS_ACCESS_DENIED, kind); - } - - uint32_t kind = entry->Kind; - NTL_RESOURCE_REPLY reply; - memset(&reply, 0, sizeof(reply)); - reply.Size = sizeof(reply); - reply.Version = NTL_RESOURCE_PROTOCOL_VERSION; - reply.Operation = NTL_RESOURCE_OP_RESOLVE; - reply.Flags = request->Flags; - reply.Status = NTLSTATUS_SUCCESS; - reply.Kind = kind; - reply.ResolvedLength = entry->PathLength; - - SYS_PORT_HANDLE_TRANSFER transfer; - transfer.Handle = entry->Handle; - transfer.DesiredRights = desiredRights; - transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; - - SYS_REPLY_PORT_REQUEST out; - memset(&out, 0, sizeof(out)); - out.Size = sizeof(out); - out.EndpointHandle = endpoint; - out.MessageId = messageId; - out.Flags = SYS_PORT_FLAG_NONE; - out.Payload = &reply; - out.PayloadLength = sizeof(reply); - out.Handles = &transfer; - out.HandleCount = 1; - status = SysReplyPort(&out); - RssrvUnlockNamespace(); - if (!KSUCCESS(status)) - return RssrvReplyStatus(endpoint, messageId, request->Operation, status, kind); - return NTLSTATUS_SUCCESS; -} - -// Handles one metadata query. -static KSTATUS RssrvHandleQuery(SYS_HANDLE endpoint, - uint32_t messageId, - const NTL_RESOURCE_REQUEST* request, - const char* path) { - KSTATUS status = RssrvLockNamespace(); - if (!KSUCCESS(status)) - return RssrvReplyStatus(endpoint, messageId, request->Operation, status, NTL_RESOURCE_KIND_INVALID); - RSSRV_ENTRY* entry = RssrvFindLocked(path, request->PathLength, (RSSRV_ENTRY***)0); - uint32_t found = entry != (RSSRV_ENTRY*)0; - uint32_t kind = found ? entry->Kind : NTL_RESOURCE_KIND_INVALID; - RssrvUnlockNamespace(); - return RssrvReplyStatus(endpoint, messageId, request->Operation, - found ? NTLSTATUS_SUCCESS : NTLSTATUS_NOT_FOUND, kind); -} - -// Handles one administrative bind. On success the namespace takes ownership of receivedHandle. -static KSTATUS RssrvHandleBind(SYS_HANDLE endpoint, - uint32_t messageId, - const NTL_RESOURCE_REQUEST* request, - const char* path, - uint32_t administrative, - SYS_HANDLE receivedHandle, - uint32_t* consumedOut) { - if (!consumedOut) return NTLSTATUS_INVALID_PARAMETER; - *consumedOut = 0; - if (!administrative || RssrvIsManagedVolumePath(path, request->PathLength)) - return RssrvReplyStatus(endpoint, messageId, request->Operation, - NTLSTATUS_ACCESS_DENIED, NTL_RESOURCE_KIND_INVALID); - KSTATUS bindStatus = RssrvBindOwned(path, request->PathLength, request->Kind, receivedHandle, request->ExportRights); - if (KSUCCESS(bindStatus)) *consumedOut = 1; - KSTATUS replyStatus = RssrvReplyStatus(endpoint, messageId, request->Operation, bindStatus, - KSUCCESS(bindStatus) ? request->Kind : NTL_RESOURCE_KIND_INVALID); - return KSUCCESS(replyStatus) ? bindStatus : replyStatus; -} - -// Dispatches one received resource request and returns whether a received handle was consumed. -static uint32_t RssrvDispatchRequest(SYS_HANDLE endpoint, - uint32_t administrative, - const uint8_t* payload, - uint32_t payloadLength, - const SYS_PORT_MESSAGE_INFO* info, - SYS_HANDLE receivedHandle) { - uint32_t operation = 0; - if (payloadLength < sizeof(NTL_RESOURCE_REQUEST)) { - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, - NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); - return 0; - } - - NTL_RESOURCE_REQUEST request; - memcpy(&request, payload, sizeof(request)); - operation = request.Operation; - if (request.Size != sizeof(request) || request.Version != NTL_RESOURCE_PROTOCOL_VERSION || - request.PathLength == 0 || payloadLength != sizeof(request) + request.PathLength) { - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, - NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); - return 0; - } - const char* path = (const char*)(payload + sizeof(request)); - KSTATUS status = RssrvValidatePath(path, request.PathLength); - if (!KSUCCESS(status)) { - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, status, NTL_RESOURCE_KIND_INVALID); - return 0; - } - - switch (operation) { - case NTL_RESOURCE_OP_LOOKUP: - if (request.Flags != NTL_RESOURCE_FLAG_NONE || info->HandleCount != 0 || - request.ExportRights != 0 || request.Kind != NTL_RESOURCE_KIND_INVALID) { - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, - NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); - return 0; - } - (void)RssrvHandleLookup(endpoint, info->MessageId, &request, path); - return 0; - case NTL_RESOURCE_OP_QUERY: - if (request.Flags != NTL_RESOURCE_FLAG_NONE || info->HandleCount != 0 || request.Rights != 0 || - request.ExportRights != 0 || request.Kind != NTL_RESOURCE_KIND_INVALID) { - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, - NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); - return 0; - } - (void)RssrvHandleQuery(endpoint, info->MessageId, &request, path); - return 0; - case NTL_RESOURCE_OP_RESOLVE: - if (info->HandleCount != 0 || request.Rights != 0 || request.ExportRights != 0 || - request.Kind != NTL_RESOURCE_KIND_INVALID || - (request.Flags & ~NTL_RESOURCE_RESOLVE_FLAG_MODIFY) != 0) { - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, - NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); - return 0; - } - (void)RssrvHandleResolve(endpoint, info->MessageId, &request, path); - return 0; - case NTL_RESOURCE_OP_BIND: - if (request.Flags != NTL_RESOURCE_FLAG_NONE || info->HandleCount != 1 || - !(request.Rights & SYS_RIGHT_TRANSFER) || - request.ExportRights == 0 || (request.ExportRights & ~request.Rights) != 0 || - request.Kind == NTL_RESOURCE_KIND_INVALID || receivedHandle == SYS_HANDLE_INVALID) { - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, - NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); - return 0; - } - { - uint32_t consumed = 0; - (void)RssrvHandleBind(endpoint, info->MessageId, &request, path, administrative, - receivedHandle, &consumed); - return consumed; - } - case NTL_RESOURCE_OP_UNBIND: - if (request.Flags != NTL_RESOURCE_FLAG_NONE || info->HandleCount != 0 || request.Rights != 0 || - request.ExportRights != 0 || request.Kind != NTL_RESOURCE_KIND_INVALID) { - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, - NTLSTATUS_INVALID_PARAMETER, NTL_RESOURCE_KIND_INVALID); - return 0; - } - status = administrative && !RssrvIsManagedVolumePath(path, request.PathLength) - ? RssrvUnbind(path, request.PathLength) - : NTLSTATUS_ACCESS_DENIED; - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, status, NTL_RESOURCE_KIND_INVALID); - return 0; - default: - (void)RssrvReplyStatus(endpoint, info->MessageId, operation, - NTLSTATUS_NOT_SUPPORTED, NTL_RESOURCE_KIND_INVALID); - return 0; - } -} - -// Serves one connected resource session until the peer disconnects. -static void RssrvSessionThread(uintptr_t argument) { - RSSRV_SESSION* session = (RSSRV_SESSION*)argument; - if (!session) { - SysExitThread(1); - return; - } - SYS_HANDLE endpoint = session->Endpoint; - uint32_t administrative = session->Administrative; - free(session); - - uint8_t payload[SYS_PORT_MAX_INLINE_BYTES]; - SYS_HANDLE handles[SYS_PORT_MAX_TRANSFER_HANDLES]; - for (;;) { - memset(handles, 0, sizeof(handles)); - SYS_PORT_MESSAGE_INFO info; - memset(&info, 0, sizeof(info)); - info.Size = sizeof(info); - SYS_RECEIVE_PORT_REQUEST receive; - memset(&receive, 0, sizeof(receive)); - receive.Size = sizeof(receive); - receive.EndpointHandle = endpoint; - receive.Flags = SYS_PORT_FLAG_NONE; - receive.Payload = payload; - receive.PayloadCapacity = sizeof(payload); - receive.HandlesOut = handles; - receive.HandleCapacity = SYS_PORT_MAX_TRANSFER_HANDLES; - receive.InfoOut = &info; - receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; - KSTATUS status = SysReceivePort(&receive); - if (status == NTLSTATUS_PORT_DISCONNECTED) - break; - if (!KSUCCESS(status)) - break; - - uint32_t consumed = 0; - if (info.Type == SYS_PORT_MESSAGE_REQUEST) - consumed = RssrvDispatchRequest(endpoint, administrative, payload, info.PayloadLength, - &info, info.HandleCount == 1 ? handles[0] : SYS_HANDLE_INVALID); - for (uint32_t i = consumed ? 1u : 0u; i < info.HandleCount; ++i) { - if (handles[i] != SYS_HANDLE_INVALID) (void)SysCloseHandle(handles[i]); - } - } - - (void)SysCloseHandle(endpoint); - SysExitThread(0); -} - -// Accepts sessions from one listener and gives every connection its own worker thread. -static void RssrvListenerThread(uintptr_t argument) { - RSSRV_LISTENER_WORKER* worker = (RSSRV_LISTENER_WORKER*)argument; - if (!worker) { - SysExitThread(1); - return; - } - for (;;) { - SYS_HANDLE endpoint = SYS_HANDLE_INVALID; - SYS_ACCEPT_PORT_REQUEST accept; - memset(&accept, 0, sizeof(accept)); - accept.Size = sizeof(accept); - accept.ListenerHandle = worker->Listener; - accept.Flags = SYS_PORT_ACCEPT_FLAG_NONE; - accept.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; - accept.EndpointHandleOut = &endpoint; - KSTATUS status = SysAcceptPort(&accept); - if (!KSUCCESS(status)) - break; - - RSSRV_SESSION* session = (RSSRV_SESSION*)malloc(sizeof(*session)); - if (!session) { - (void)SysDisconnectPort(endpoint); - (void)SysCloseHandle(endpoint); - continue; - } - session->Endpoint = endpoint; - session->Administrative = worker->Administrative; - - uint32_t threadId = 0; - SYS_CREATE_THREAD_REQUEST create; - memset(&create, 0, sizeof(create)); - create.Size = sizeof(create); - create.Entry = (uintptr_t)RssrvSessionThread; - create.Argument = (uintptr_t)session; - create.Priority = 1; - create.Flags = SYS_CREATE_THREAD_FLAG_NONE; - create.ThreadIdOut = &threadId; - status = SysCreateThread(&create); - if (!KSUCCESS(status)) { - free(session); - (void)SysDisconnectPort(endpoint); - (void)SysCloseHandle(endpoint); - } - } - SysExitThread(0); -} - -// Starts one resource listener worker thread. -static KSTATUS RssrvStartListenerWorker(RSSRV_LISTENER_WORKER* worker, - SYS_HANDLE listener, - uint32_t administrative) { - worker->Listener = listener; - worker->Administrative = administrative; - uint32_t threadId = 0; - SYS_CREATE_THREAD_REQUEST create; - memset(&create, 0, sizeof(create)); - create.Size = sizeof(create); - create.Entry = (uintptr_t)RssrvListenerThread; - create.Argument = (uintptr_t)worker; - create.Priority = 1; - create.Flags = SYS_CREATE_THREAD_FLAG_NONE; - create.ThreadIdOut = &threadId; - return SysCreateThread(&create); -} - -// Creates one listener using the service's bounded default backlog. -static KSTATUS RssrvCreateListener(SYS_HANDLE* listenerOut) { - SYS_CREATE_PORT_REQUEST create; - memset(&create, 0, sizeof(create)); - create.Size = sizeof(create); - create.Flags = SYS_PORT_FLAG_NONE; - create.Backlog = RSSRV_LISTENER_BACKLOG; - create.HandleOut = listenerOut; - return SysCreatePort(&create); +// Terminates RSSRV without depending on a compiler runtime return path. +void RssrvExit(int code) { + for (;;) SysExitProcess(code); } // Accepts the one private service-manager control connection. @@ -826,20 +37,31 @@ static KSTATUS RssrvAcceptControl(SYS_HANDLE controlListener, SYS_HANDLE* endpoi return SysAcceptPort(&accept); } -// Sends service readiness and delegates the private administrative resource listener to init. -static KSTATUS RssrvSendReady(SYS_HANDLE controlEndpoint) { +// Sends service readiness and delegates init-only namespace and DWM-session bootstrap capabilities. +static KSTATUS RssrvSendReady(SYS_HANDLE controlEndpoint, + const RSSRV_RESOURCE_SERVICE* resource, + const RSSRV_SESSION_STATE* session) { + if (!resource || !session || resource->AdminListener == SYS_HANDLE_INVALID || + session->DwmListener == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + NTL_SERVICE_CONTROL_MESSAGE message; memset(&message, 0, sizeof(message)); message.Size = sizeof(message); message.Version = NTL_SERVICE_CONTROL_VERSION; message.Operation = NTL_SERVICE_CONTROL_READY; - message.Flags = NTL_SERVICE_READY_FLAG_RESOURCE_ADMIN_LISTENER; + message.ExportCount = 2u; message.Status = NTLSTATUS_SUCCESS; - SYS_PORT_HANDLE_TRANSFER transfer; - transfer.Handle = gAdminListener; - transfer.DesiredRights = SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT; - transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + SYS_PORT_HANDLE_TRANSFER transfers[2]; + memset(transfers, 0, sizeof(transfers)); + transfers[0].Handle = resource->AdminListener; + transfers[0].DesiredRights = SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT; + transfers[0].Flags = SYS_PORT_TRANSFER_FLAG_NONE; + transfers[1].Handle = session->DwmListener; + // Init must transfer this private listener once more into DWM. DWM itself receives only connect/query rights. + transfers[1].DesiredRights = SYS_RIGHT_DUPLICATE | SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT; + transfers[1].Flags = SYS_PORT_TRANSFER_FLAG_NONE; SYS_SEND_PORT_REQUEST send; memset(&send, 0, sizeof(send)); @@ -848,8 +70,8 @@ static KSTATUS RssrvSendReady(SYS_HANDLE controlEndpoint) { send.Flags = SYS_PORT_FLAG_NONE; send.Payload = &message; send.PayloadLength = sizeof(message); - send.Handles = &transfer; - send.HandleCount = 1; + send.Handles = transfers; + send.HandleCount = 2u; send.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; return SysSendPort(&send); } @@ -877,17 +99,56 @@ static void RssrvControlLoop(SYS_HANDLE controlEndpoint) { return; if (info.Type != SYS_PORT_MESSAGE_NOTIFICATION || info.HandleCount != 0 || info.PayloadLength != sizeof(message) || message.Size != sizeof(message) || - message.Version != NTL_SERVICE_CONTROL_VERSION) + message.Version != NTL_SERVICE_CONTROL_VERSION || message.ExportCount != 0 || + message.Reserved[0] != 0 || message.Reserved[1] != 0 || message.Reserved[2] != 0) continue; if (message.Operation == NTL_SERVICE_CONTROL_STOP) return; } } +// Queries the mounted-volume identity carried by one volume or directory root capability. +static KSTATUS RssrvQueryRootVolumeId(SYS_HANDLE root, uint32_t* volumeIdOut) { + if (root == SYS_HANDLE_INVALID || !volumeIdOut) + return NTLSTATUS_INVALID_PARAMETER; + *volumeIdOut = 0; + SYS_FILE_STAT stat; + memset(&stat, 0, sizeof(stat)); + SYS_STAT_AT_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.RootHandle = root; + request.Path = SYS_SV_NULL; + request.StatOut = &stat; + KSTATUS status = SysStatAt(&request); + if (!KSUCCESS(status)) return status; + if (stat.Size != sizeof(stat) || stat.VolumeId == 0 || stat.VolumeId > UINT32_MAX) + return NTLSTATUS_FAILURE; + *volumeIdOut = (uint32_t)stat.VolumeId; + return NTLSTATUS_SUCCESS; +} + +// Initializes the fixed native namespace roots before any dynamic publication starts. +static KSTATUS RssrvInitializeNamespaceRoots(RSSRV_NAMESPACE* namespaceState) { + static const char* roots[] = { + "/Device", + "/Sessions", + "/Services", + }; + for (uint32_t i = 0; i < (uint32_t)(sizeof(roots) / sizeof(roots[0])); ++i) { + KSTATUS status = RssrvNamespaceCreateDirectory(namespaceState, + roots[i], + (uint32_t)strlen(roots[i])); + if (!KSUCCESS(status)) return status; + } + return NTLSTATUS_SUCCESS; +} + // Starts RSSRV and owns the global native resource namespace until init requests shutdown. void _start(void) { KSTATUS status = NtlInitializeExceptionRuntime(); if (!KSUCCESS(status)) RssrvExit(1); + RssrvSetCurrentThreadName("rssrv.main"); SYS_SECURITY_CONTEXT_INFO security; memset(&security, 0, sizeof(security)); @@ -901,46 +162,83 @@ void _start(void) { SYS_HANDLE systemVolume = SysGetStartupCapability(SYS_STARTUP_CAP_SYSTEM_VOLUME_NAMESPACE); SYS_HANDLE controlListener = SysGetStartupCapability(SYS_STARTUP_CAP_SERVICE_CONTROL); SYS_HANDLE volumeManager = SysGetStartupCapability(SYS_STARTUP_CAP_VOLUME_MANAGER); + SYS_HANDLE inputManager = SysGetStartupCapability(SYS_STARTUP_CAP_INPUT_MANAGER); + SYS_HANDLE displayManager = SysGetStartupCapability(SYS_STARTUP_CAP_DISPLAY_MANAGER); if (systemVolume == SYS_HANDLE_INVALID || controlListener == SYS_HANDLE_INVALID || - volumeManager == SYS_HANDLE_INVALID) + volumeManager == SYS_HANDLE_INVALID || inputManager == SYS_HANDLE_INVALID || + displayManager == SYS_HANDLE_INVALID) RssrvExit(3); - status = SysCreateMutex(&gNamespaceMutex); + uint32_t systemVolumeId = 0; + status = RssrvQueryRootVolumeId(systemVolume, &systemVolumeId); if (!KSUCCESS(status)) RssrvExit(3); - // RSSRV takes ownership of its startup system-volume handle as the first namespace binding. - static const char systemRoot[] = "/SystemRoot"; - status = RssrvBindOwned(systemRoot, sizeof(systemRoot) - 1, NTL_RESOURCE_KIND_VOLUME, systemVolume, - SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN | SYS_VOLUME_RIGHT_MODIFY); + memset(&gRssrv, 0, sizeof(gRssrv)); + status = RssrvNamespaceInitialize(&gRssrv.Namespace); + if (!KSUCCESS(status)) RssrvExit(3); + status = RssrvInitializeNamespaceRoots(&gRssrv.Namespace); if (!KSUCCESS(status)) RssrvExit(4); + status = RssrvVolumesInitialize(&gRssrv.Volumes, &gRssrv.Namespace, volumeManager); + if (!KSUCCESS(status)) RssrvExit(4); + status = RssrvDisplayInitialize(&gRssrv.Display, &gRssrv.Namespace, displayManager); + if (!KSUCCESS(status)) RssrvExit(4); + status = RssrvDisplayPublishDevice(&gRssrv.Display); + if (!KSUCCESS(status)) RssrvExit(4); + status = RssrvSessionInitialize(&gRssrv.Session, &gRssrv.Namespace, &gRssrv.Display); + if (!KSUCCESS(status)) RssrvExit(4); + status = RssrvInputInitialize(&gRssrv.Input, &gRssrv.Namespace, &gRssrv.Session, inputManager); + if (!KSUCCESS(status)) RssrvExit(4); + status = RssrvConsoleInitialize(&gRssrv.Console, &gRssrv.Namespace, + &gRssrv.Display, &gRssrv.Session); + if (!KSUCCESS(status)) RssrvExit(4); + uint64_t volumeGeneration = 0; + status = RssrvVolumesSynchronize(&gRssrv.Volumes, &volumeGeneration); + if (!KSUCCESS(status)) RssrvExit(4); + + char systemVolumePath[RSSRV_VOLUME_PATH_CAPACITY]; + uint32_t systemVolumePathLength = 0; + status = RssrvVolumesQueryPublishedPath(&gRssrv.Volumes, + systemVolumeId, + systemVolumePath, + (uint32_t)sizeof(systemVolumePath), + &systemVolumePathLength); + if (!KSUCCESS(status)) RssrvExit(4); + + // /SystemRoot is a real namespace symbolic link to the independently published system volume. + status = RssrvNamespaceCreateSymbolicLink(&gRssrv.Namespace, + "/SystemRoot", + sizeof("/SystemRoot") - 1u, + systemVolumePath, + systemVolumePathLength); + if (!KSUCCESS(status)) RssrvExit(4); + (void)SysCloseHandle(systemVolume); + SYS_HANDLE controlEndpoint = SYS_HANDLE_INVALID; status = RssrvAcceptControl(controlListener, &controlEndpoint); (void)SysCloseHandle(controlListener); if (!KSUCCESS(status)) RssrvExit(5); - status = RssrvCreateListener(&gPublicListener); + status = RssrvResourceInitialize(&gRssrv.Resource, &gRssrv.Namespace); if (!KSUCCESS(status)) RssrvExit(6); - status = RssrvCreateListener(&gAdminListener); + status = RssrvResourceStart(&gRssrv.Resource); if (!KSUCCESS(status)) RssrvExit(7); - status = RssrvStartListenerWorker(&gPublicWorker, gPublicListener, 0); + status = RssrvInputStart(&gRssrv.Input); if (!KSUCCESS(status)) RssrvExit(8); - status = RssrvStartListenerWorker(&gAdminWorker, gAdminListener, 1); + status = RssrvConsoleStart(&gRssrv.Console); if (!KSUCCESS(status)) RssrvExit(9); - - uint64_t volumeGeneration = 0; - status = RssrvSyncVolumes(volumeManager, &volumeGeneration); + status = RssrvSessionStart(&gRssrv.Session, &gRssrv.Console); if (!KSUCCESS(status)) RssrvExit(10); - status = RssrvStartVolumeWorker(volumeManager); + status = RssrvVolumesStartWorker(&gRssrv.Volumes); if (!KSUCCESS(status)) RssrvExit(11); - status = SysRegisterSystemService(SYS_SERVICE_RESOURCE, gPublicListener); + status = SysRegisterSystemService(SYS_SERVICE_RESOURCE, gRssrv.Resource.PublicListener); if (!KSUCCESS(status)) RssrvExit(12); - status = RssrvSendReady(controlEndpoint); + status = RssrvSendReady(controlEndpoint, &gRssrv.Resource, &gRssrv.Session); if (!KSUCCESS(status)) RssrvExit(13); RssrvControlLoop(controlEndpoint); - (void)SysUnregisterSystemService(SYS_SERVICE_RESOURCE, gPublicListener); + (void)SysUnregisterSystemService(SYS_SERVICE_RESOURCE, gRssrv.Resource.PublicListener); (void)SysDisconnectPort(controlEndpoint); (void)SysCloseHandle(controlEndpoint); RssrvExit(0); diff --git a/code/user/system/rssrv/rssrv_internal.h b/code/user/system/rssrv/rssrv_internal.h new file mode 100644 index 0000000..a947dee --- /dev/null +++ b/code/user/system/rssrv/rssrv_internal.h @@ -0,0 +1,304 @@ +// Internal RSSRV component model. One process owns the native resource plane, while each component owns its state. + +#pragma once + +#include +#include +#include +#include +#include + +#define RSSRV_LISTENER_BACKLOG 16u +#define RSSRV_SYMBOLIC_LINK_MAX_DEPTH 16u +#define RSSRV_VOLUME_PATH_CAPACITY 48u +#define RSSRV_INPUT_KEY_QUEUE_SIZE 256u +#define RSSRV_INPUT_MOUSE_QUEUE_SIZE 128u +#define RSSRV_CONSOLE_KEY_QUEUE_SIZE 256u + +typedef struct RSSRV_NODE RSSRV_NODE; +typedef struct RSSRV_VOLUME_PUBLICATION RSSRV_VOLUME_PUBLICATION; +typedef struct RSSRV_RESOURCE_SERVICE RSSRV_RESOURCE_SERVICE; +typedef struct RSSRV_SESSION_STATE RSSRV_SESSION_STATE; +typedef struct RSSRV_INPUT_TARGET RSSRV_INPUT_TARGET; + +typedef void (*RSSRV_INPUT_TARGET_KEY_ROUTINE)(void* context, const INPUT_KEY_RECORD* record); +typedef void (*RSSRV_INPUT_TARGET_MOUSE_ROUTINE)(void* context, const INPUT_MOUSE_EVENT* event, int32_t x, int32_t y); + +#define RSSRV_INPUT_TARGET_KIND_NONE 0u +#define RSSRV_INPUT_TARGET_KIND_CONSOLE 1u +#define RSSRV_INPUT_TARGET_KIND_DESKTOP 2u + +struct RSSRV_INPUT_TARGET { + uint64_t Id; + uint32_t Kind; + uint32_t Flags; + RSSRV_SESSION_STATE* Session; + void* Context; + RSSRV_INPUT_TARGET_KEY_ROUTINE KeyRoutine; + RSSRV_INPUT_TARGET_MOUSE_ROUTINE MouseRoutine; +}; + +typedef struct RSSRV_NAMESPACE { + SYS_HANDLE Mutex; + RSSRV_NODE* Root; +} RSSRV_NAMESPACE; + +typedef struct RSSRV_NAMESPACE_BINDING_VIEW { + RSSRV_NAMESPACE* Namespace; + SYS_HANDLE Handle; + uint32_t Kind; + SYS_RIGHTS ExportRights; + uint32_t ResolvedLength; + uint32_t Locked; +} RSSRV_NAMESPACE_BINDING_VIEW; + +typedef struct RSSRV_NAMESPACE_ENTRY { + uint32_t Kind; + uint32_t NameLength; + char Name[NTL_RESOURCE_NAME_MAX + 1u]; +} RSSRV_NAMESPACE_ENTRY; + +typedef struct RSSRV_DISPLAY_STATE { + SYS_HANDLE Manager; + RSSRV_NAMESPACE* Namespace; +} RSSRV_DISPLAY_STATE; + +struct RSSRV_SESSION_STATE { + SYS_HANDLE Mutex; + RSSRV_NAMESPACE* Namespace; + RSSRV_DISPLAY_STATE* Display; + uint32_t Id; + uint32_t Reserved; + uint64_t Generation; + uint64_t NextTargetId; + int32_t DisplayWidth; + int32_t DisplayHeight; + RSSRV_INPUT_TARGET* ActiveTarget; + RSSRV_INPUT_TARGET* KeyboardFocus; + RSSRV_INPUT_TARGET* PointerFocus; + RSSRV_INPUT_TARGET* PointerCapture; + SYS_HANDLE DwmListener; + SYS_HANDLE DwmEndpoint; + RSSRV_INPUT_TARGET DesktopTarget; +}; + +typedef struct RSSRV_INPUT_STATE { + SYS_HANDLE Manager; + SYS_HANDLE Mutex; + SYS_HANDLE KeyEvent; + SYS_HANDLE MouseEvent; + SYS_HANDLE Listener; + RSSRV_NAMESPACE* Namespace; + RSSRV_SESSION_STATE* Session; + + uint8_t KeyDown[NTL_INPUT_KEY_BYTES]; + uint32_t Modifiers; + uint64_t Generation; + INPUT_KEY_RECORD KeyQueue[RSSRV_INPUT_KEY_QUEUE_SIZE]; + uint32_t KeyHead; + uint32_t KeyTail; + + INPUT_MOUSE_EVENT MouseQueue[RSSRV_INPUT_MOUSE_QUEUE_SIZE]; + uint32_t MouseHead; + uint32_t MouseTail; + int32_t MouseX; + int32_t MouseY; + int32_t MouseWheel; + uint32_t MouseButtons; + int32_t MouseWidth; + int32_t MouseHeight; +} RSSRV_INPUT_STATE; + +typedef struct RSSRV_CONSOLE_STATE { + SYS_HANDLE Mutex; + SYS_HANDLE KeyEvent; + SYS_HANDLE Listener; + RSSRV_NAMESPACE* Namespace; + RSSRV_DISPLAY_STATE* Display; + RSSRV_SESSION_STATE* Session; + RSSRV_INPUT_TARGET InputTarget; + INPUT_KEY_RECORD KeyQueue[RSSRV_CONSOLE_KEY_QUEUE_SIZE]; + uint32_t KeyHead; + uint32_t KeyTail; + uint64_t Generation; +} RSSRV_CONSOLE_STATE; + +typedef struct RSSRV_VOLUME_STATE { + RSSRV_NAMESPACE* Namespace; + SYS_HANDLE Manager; + RSSRV_VOLUME_PUBLICATION* Published; + uint32_t NextHarddiskVolumeId; +} RSSRV_VOLUME_STATE; + +typedef struct RSSRV_LISTENER_WORKER { + RSSRV_RESOURCE_SERVICE* Service; + SYS_HANDLE Listener; + uint32_t Administrative; +} RSSRV_LISTENER_WORKER; + +struct RSSRV_RESOURCE_SERVICE { + RSSRV_NAMESPACE* Namespace; + SYS_HANDLE PublicListener; + SYS_HANDLE AdminListener; + RSSRV_LISTENER_WORKER PublicWorker; + RSSRV_LISTENER_WORKER AdminWorker; +}; + +typedef struct RSSRV_STATE { + RSSRV_NAMESPACE Namespace; + RSSRV_VOLUME_STATE Volumes; + RSSRV_DISPLAY_STATE Display; + RSSRV_SESSION_STATE Session; + RSSRV_INPUT_STATE Input; + RSSRV_CONSOLE_STATE Console; + RSSRV_RESOURCE_SERVICE Resource; +} RSSRV_STATE; + +// Sets a best-effort diagnostic name for the current RSSRV thread. +void RssrvSetCurrentThreadName(const char* name); + +// Terminates RSSRV without depending on a compiler runtime return path. +void RssrvExit(int code); + +// Initializes the native namespace root and its synchronization object. +KSTATUS RssrvNamespaceInitialize(RSSRV_NAMESPACE* namespaceState); + +// Validates one canonical absolute native resource path, including the root path "/". +KSTATUS RssrvNamespaceValidatePath(const char* path, uint32_t length); + +// Creates one exact namespace directory. Its parent must already exist. +KSTATUS RssrvNamespaceCreateDirectory(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length); + +// Creates one absolute namespace symbolic link. Dangling targets are permitted. +KSTATUS RssrvNamespaceCreateSymbolicLink(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + const char* target, + uint32_t targetLength); + +// Takes ownership of one handle and binds it to a new exact resource name. +KSTATUS RssrvNamespaceBindOwned(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + uint32_t kind, + SYS_HANDLE handle, + SYS_RIGHTS exportRights); + +// Removes one exact namespace node. Namespace directories must be empty. +KSTATUS RssrvNamespaceUnbind(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length); + +// Queries one exact namespace node without following the final symbolic link. +KSTATUS RssrvNamespaceQuery(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + uint32_t* kindOut); + +// Returns the lexicographically next immediate child after one optional cursor name. +KSTATUS RssrvNamespaceEnumerate(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + const char* after, + uint32_t afterLength, + RSSRV_NAMESPACE_ENTRY* entryOut); + +// Opens a locked view of one exact bound object, following a final namespace symbolic link. +KSTATUS RssrvNamespaceBeginLookup(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + RSSRV_NAMESPACE_BINDING_VIEW* viewOut, + uint32_t* kindOut); + +// Opens a locked view of the first bound object reached while traversing one resource path. +KSTATUS RssrvNamespaceBeginResolve(RSSRV_NAMESPACE* namespaceState, + const char* path, + uint32_t length, + RSSRV_NAMESPACE_BINDING_VIEW* viewOut); + +// Releases the namespace lock held by a successful binding view. +void RssrvNamespaceEndBinding(RSSRV_NAMESPACE_BINDING_VIEW* view); + +// Initializes display authority around the kernel display-manager capability. +KSTATUS RssrvDisplayInitialize(RSSRV_DISPLAY_STATE* display, RSSRV_NAMESPACE* namespaceState, SYS_HANDLE manager); +// Publishes a query-only display device capability while retaining physical display authority inside RSSRV. +KSTATUS RssrvDisplayPublishDevice(RSSRV_DISPLAY_STATE* display); +KSTATUS RssrvDisplayQueryInfo(RSSRV_DISPLAY_STATE* display, SYS_DISPLAY_INFO* infoOut); +KSTATUS RssrvDisplayWriteConsole(RSSRV_DISPLAY_STATE* display, const char* data, size_t length); +KSTATUS RssrvDisplayQueryConsoleInfo(RSSRV_DISPLAY_STATE* display, SYS_CONSOLE_INFO* infoOut); +KSTATUS RssrvDisplaySetConsoleCursor(RSSRV_DISPLAY_STATE* display, int x, int y); +KSTATUS RssrvDisplayFillConsoleCharacter(RSSRV_DISPLAY_STATE* display, char character, + int x, int y, size_t length, size_t* writtenOut); +KSTATUS RssrvDisplayFillConsoleAttribute(RSSRV_DISPLAY_STATE* display, uint16_t attribute, + int x, int y, size_t length, size_t* writtenOut); +KSTATUS RssrvDisplayPutConsoleCharacter(RSSRV_DISPLAY_STATE* display, char character, + int x, int y, uint32_t foreground, uint32_t background); +KSTATUS RssrvDisplayClearConsole(RSSRV_DISPLAY_STATE* display, uint32_t color); + +// Initializes Session 0, input routing, and display ownership. +KSTATUS RssrvSessionInitialize(RSSRV_SESSION_STATE* session, + RSSRV_NAMESPACE* namespaceState, + RSSRV_DISPLAY_STATE* display); +KSTATUS RssrvSessionRegisterInputTarget(RSSRV_SESSION_STATE* session, + RSSRV_INPUT_TARGET* target, + uint32_t kind, + void* context, + RSSRV_INPUT_TARGET_KEY_ROUTINE keyRoutine, + RSSRV_INPUT_TARGET_MOUSE_ROUTINE mouseRoutine); +KSTATUS RssrvSessionActivateInputTarget(RSSRV_SESSION_STATE* session, RSSRV_INPUT_TARGET* target); +KSTATUS RssrvSessionSetKeyboardFocus(RSSRV_SESSION_STATE* session, RSSRV_INPUT_TARGET* target); +KSTATUS RssrvSessionSetPointerFocus(RSSRV_SESSION_STATE* session, RSSRV_INPUT_TARGET* target); +KSTATUS RssrvSessionSetPointerCapture(RSSRV_SESSION_STATE* session, RSSRV_INPUT_TARGET* target); +KSTATUS RssrvSessionQueryDisplayBounds(RSSRV_SESSION_STATE* session, int32_t* widthOut, int32_t* heightOut); +void RssrvSessionObserveKey(RSSRV_SESSION_STATE* session, const INPUT_KEY_RECORD* record); +void RssrvSessionObserveMouse(RSSRV_SESSION_STATE* session, const INPUT_MOUSE_EVENT* event, int32_t x, int32_t y); +void RssrvSessionRouteKey(RSSRV_SESSION_STATE* session, const INPUT_KEY_RECORD* record); +void RssrvSessionRouteMouse(RSSRV_SESSION_STATE* session, const INPUT_MOUSE_EVENT* event, int32_t x, int32_t y); +KSTATUS RssrvSessionStart(RSSRV_SESSION_STATE* session, RSSRV_CONSOLE_STATE* console); + +// Initializes and starts RSSRV-owned logical input over the kernel manager stream. +KSTATUS RssrvInputInitialize(RSSRV_INPUT_STATE* input, + RSSRV_NAMESPACE* namespaceState, + RSSRV_SESSION_STATE* session, + SYS_HANDLE manager); +KSTATUS RssrvInputStart(RSSRV_INPUT_STATE* input); +KSTATUS RssrvInputReadKeyRecord(RSSRV_INPUT_STATE* input, int peek, + uint64_t timeoutMilliseconds, INPUT_KEY_RECORD* recordOut); +KSTATUS RssrvInputFlushKeys(RSSRV_INPUT_STATE* input, uint32_t* discardedOut); + +// Initializes and starts the one native logical console owned by RSSRV. +KSTATUS RssrvConsoleInitialize(RSSRV_CONSOLE_STATE* console, + RSSRV_NAMESPACE* namespaceState, + RSSRV_DISPLAY_STATE* display, + RSSRV_SESSION_STATE* session); +KSTATUS RssrvConsoleStart(RSSRV_CONSOLE_STATE* console); + +// Initializes mounted-volume publication state against one volume-manager capability. +KSTATUS RssrvVolumesInitialize(RSSRV_VOLUME_STATE* volumes, + RSSRV_NAMESPACE* namespaceState, + SYS_HANDLE manager); + +// Reconciles RSSRV volume bindings with the kernel's mounted-volume snapshot. +KSTATUS RssrvVolumesSynchronize(RSSRV_VOLUME_STATE* volumes, uint64_t* generationOut); + +// Copies the namespace path assigned to one mounted kernel volume instance. +KSTATUS RssrvVolumesQueryPublishedPath(RSSRV_VOLUME_STATE* volumes, + uint32_t instanceId, + char* pathOut, + uint32_t capacity, + uint32_t* lengthOut); + +// Starts the mounted-volume namespace publication worker. +KSTATUS RssrvVolumesStartWorker(RSSRV_VOLUME_STATE* volumes); + +// Returns whether a path belongs to the RSSRV-owned automatic mounted-volume naming family. +int RssrvVolumesOwnPath(const char* path, uint32_t length); + +// Initializes the public/admin resource protocol component around existing namespace state. +KSTATUS RssrvResourceInitialize(RSSRV_RESOURCE_SERVICE* service, + RSSRV_NAMESPACE* namespaceState); + +// Creates the resource listeners and starts their accept workers. +KSTATUS RssrvResourceStart(RSSRV_RESOURCE_SERVICE* service); diff --git a/code/user/system/rssrv/session.c b/code/user/system/rssrv/session.c new file mode 100644 index 0000000..10cfac1 --- /dev/null +++ b/code/user/system/rssrv/session.c @@ -0,0 +1,464 @@ +// RSSRV interactive-session policy. Session 0 owns display routing, activation, focus, pointer capture, and DWM authority. + +#include "rssrv_internal.h" +#include +#include +#include + +#define RSSRV_SESSION_ZERO_PATH "/Sessions/0" +#define RSSRV_SESSION_ZERO_CONSOLE_PATH "/Sessions/0/Console" +#define RSSRV_CURRENT_CONSOLE_PATH "/Console" +#define RSSRV_DWM_LISTENER_BACKLOG 1u + +// Acquires the session-state mutex. +static KSTATUS RssrvSessionLock(RSSRV_SESSION_STATE* session) { + if (!session || session->Mutex == SYS_HANDLE_INVALID) return NTLSTATUS_INVALID_PARAMETER; + return SysWait(session->Mutex, SYS_WAIT_FLAG_NONE); +} + +// Releases the session-state mutex. +static void RssrvSessionUnlock(RSSRV_SESSION_STATE* session) { + if (session && session->Mutex != SYS_HANDLE_INVALID) (void)SysReleaseMutex(session->Mutex); +} + +// Advances the session generation while preserving zero as an uninitialized value. +static void RssrvSessionChangedLocked(RSSRV_SESSION_STATE* session) { + session->Generation++; + if (session->Generation == 0) session->Generation = 1; +} + +// Sends one best-effort DWM input notification while holding the session mutex. +static void RssrvSessionSendDwmEventLocked(RSSRV_SESSION_STATE* session, + uint32_t type, + const INPUT_KEY_RECORD* key, + const INPUT_MOUSE_EVENT* mouse, + int32_t x, + int32_t y) { + if (!session || session->DwmEndpoint == SYS_HANDLE_INVALID) return; + + NTL_SESSION_DWM_EVENT event; + memset(&event, 0, sizeof(event)); + event.Size = sizeof(event); + event.Version = NTL_SESSION_DWM_PROTOCOL_VERSION; + event.Type = type; + event.Flags = NTL_SESSION_DWM_FLAG_NONE; + event.PointerX = x; + event.PointerY = y; + if (key) event.Key = *key; + if (mouse) event.Mouse = *mouse; + + SYS_SEND_PORT_REQUEST send; + memset(&send, 0, sizeof(send)); + send.Size = sizeof(send); + send.EndpointHandle = session->DwmEndpoint; + send.Flags = SYS_PORT_FLAG_NONE; + send.Payload = &event; + send.PayloadLength = sizeof(event); + send.TimeoutMilliseconds = 0; + (void)SysSendPort(&send); +} + +// Routes one focused keyboard record into the connected DWM desktop target. +static void RssrvSessionDwmRouteKey(void* context, const INPUT_KEY_RECORD* record) { + RSSRV_SESSION_STATE* session = (RSSRV_SESSION_STATE*)context; + if (!session || !record) return; + if (!KSUCCESS(RssrvSessionLock(session))) return; + RssrvSessionSendDwmEventLocked(session, NTL_SESSION_DWM_EVENT_DELIVER_KEY, record, 0, 0, 0); + RssrvSessionUnlock(session); +} + +// Routes one focused/captured mouse record into the connected DWM desktop target. +static void RssrvSessionDwmRouteMouse(void* context, + const INPUT_MOUSE_EVENT* event, + int32_t x, + int32_t y) { + RSSRV_SESSION_STATE* session = (RSSRV_SESSION_STATE*)context; + if (!session || !event) return; + if (!KSUCCESS(RssrvSessionLock(session))) return; + RssrvSessionSendDwmEventLocked(session, NTL_SESSION_DWM_EVENT_DELIVER_MOUSE, 0, event, x, y); + RssrvSessionUnlock(session); +} + +// Replies to one private DWM session-control request. +static void RssrvSessionReplyDwm(SYS_HANDLE endpoint, + uint32_t messageId, + uint32_t operation, + KSTATUS status) { + NTL_SESSION_DWM_REPLY reply; + memset(&reply, 0, sizeof(reply)); + reply.Size = sizeof(reply); + reply.Version = NTL_SESSION_DWM_PROTOCOL_VERSION; + reply.Operation = operation; + reply.Flags = NTL_SESSION_DWM_FLAG_NONE; + reply.Status = status; + + SYS_REPLY_PORT_REQUEST send; + memset(&send, 0, sizeof(send)); + send.Size = sizeof(send); + send.EndpointHandle = endpoint; + send.MessageId = messageId; + send.Flags = SYS_PORT_FLAG_NONE; + send.Payload = &reply; + send.PayloadLength = sizeof(reply); + (void)SysReplyPort(&send); +} + +// Applies one DWM focus/capture request while preserving RSSRV as the session authority owner. +static KSTATUS RssrvSessionApplyDwmRequest(RSSRV_SESSION_STATE* session, + const NTL_SESSION_DWM_REQUEST* request) { + if (!session || !request || request->Enabled > 1u) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvSessionLock(session); + if (!KSUCCESS(status)) return status; + + RSSRV_INPUT_TARGET* desktop = &session->DesktopTarget; + RSSRV_INPUT_TARGET* fallback = session->ActiveTarget == desktop ? (RSSRV_INPUT_TARGET*)0 : session->ActiveTarget; + switch (request->Operation) { + case NTL_SESSION_DWM_OP_SET_KEYBOARD_FOCUS: + if (request->Enabled) { + if (session->KeyboardFocus != desktop) { + session->KeyboardFocus = desktop; + RssrvSessionChangedLocked(session); + } + } else if (session->KeyboardFocus == desktop) { + session->KeyboardFocus = fallback; + RssrvSessionChangedLocked(session); + } + break; + case NTL_SESSION_DWM_OP_SET_POINTER_FOCUS: + if (request->Enabled) { + if (session->PointerFocus != desktop) { + session->PointerFocus = desktop; + RssrvSessionChangedLocked(session); + } + } else if (session->PointerFocus == desktop) { + session->PointerFocus = fallback; + RssrvSessionChangedLocked(session); + } + break; + case NTL_SESSION_DWM_OP_SET_POINTER_CAPTURE: + if (request->Enabled) { + if (session->PointerCapture != desktop) { + session->PointerCapture = desktop; + RssrvSessionChangedLocked(session); + } + } else if (session->PointerCapture == desktop) { + session->PointerCapture = (RSSRV_INPUT_TARGET*)0; + RssrvSessionChangedLocked(session); + } + break; + default: + status = NTLSTATUS_INVALID_PARAMETER; + break; + } + + RssrvSessionUnlock(session); + return status; +} + +// Restores non-DWM routing after one DWM control connection disappears. +static void RssrvSessionDisconnectDwm(RSSRV_SESSION_STATE* session, SYS_HANDLE endpoint) { + if (!session) return; + if (!KSUCCESS(RssrvSessionLock(session))) return; + if (session->DwmEndpoint == endpoint) { + RSSRV_INPUT_TARGET* desktop = &session->DesktopTarget; + RSSRV_INPUT_TARGET* fallback = session->ActiveTarget == desktop ? (RSSRV_INPUT_TARGET*)0 : session->ActiveTarget; + if (session->KeyboardFocus == desktop) session->KeyboardFocus = fallback; + if (session->PointerFocus == desktop) session->PointerFocus = fallback; + if (session->PointerCapture == desktop) session->PointerCapture = (RSSRV_INPUT_TARGET*)0; + session->DwmEndpoint = SYS_HANDLE_INVALID; + RssrvSessionChangedLocked(session); + } + RssrvSessionUnlock(session); +} + +// Accepts replaceable DWM connections and serves the private Session 0 authority protocol. +static void RssrvSessionDwmListenerThread(uintptr_t argument) { + RSSRV_SESSION_STATE* session = (RSSRV_SESSION_STATE*)argument; + RssrvSetCurrentThreadName("rssrv.session.dwm"); + if (!session) SysExitThread(1); + + for (;;) { + SYS_HANDLE endpoint = SYS_HANDLE_INVALID; + SYS_ACCEPT_PORT_REQUEST accept; + memset(&accept, 0, sizeof(accept)); + accept.Size = sizeof(accept); + accept.ListenerHandle = session->DwmListener; + accept.Flags = SYS_PORT_ACCEPT_FLAG_NONE; + accept.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + accept.EndpointHandleOut = &endpoint; + KSTATUS status = SysAcceptPort(&accept); + if (!KSUCCESS(status)) RssrvExit(16); + + status = RssrvSessionLock(session); + if (!KSUCCESS(status)) { + (void)SysCloseHandle(endpoint); + continue; + } + if (session->DwmEndpoint != SYS_HANDLE_INVALID) { + RssrvSessionUnlock(session); + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + continue; + } + session->DwmEndpoint = endpoint; + RssrvSessionChangedLocked(session); + RssrvSessionUnlock(session); + + for (;;) { + NTL_SESSION_DWM_REQUEST request; + SYS_PORT_MESSAGE_INFO info; + memset(&request, 0, sizeof(request)); + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + SYS_RECEIVE_PORT_REQUEST receive; + memset(&receive, 0, sizeof(receive)); + receive.Size = sizeof(receive); + receive.EndpointHandle = endpoint; + receive.Flags = SYS_PORT_FLAG_NONE; + receive.Payload = &request; + receive.PayloadCapacity = sizeof(request); + receive.InfoOut = &info; + receive.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + status = SysReceivePort(&receive); + if (status == NTLSTATUS_PORT_DISCONNECTED || !KSUCCESS(status)) break; + + if (info.Type != SYS_PORT_MESSAGE_REQUEST || info.HandleCount != 0 || + info.PayloadLength != sizeof(request) || request.Size != sizeof(request) || + request.Version != NTL_SESSION_DWM_PROTOCOL_VERSION || + request.Flags != NTL_SESSION_DWM_FLAG_NONE || request.Reserved[0] != 0 || + request.Reserved[1] != 0 || request.Reserved[2] != 0) { + RssrvSessionReplyDwm(endpoint, info.MessageId, request.Operation, NTLSTATUS_INVALID_PARAMETER); + continue; + } + + status = RssrvSessionApplyDwmRequest(session, &request); + RssrvSessionReplyDwm(endpoint, info.MessageId, request.Operation, status); + } + + RssrvSessionDisconnectDwm(session, endpoint); + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + } +} + +// Initializes Session 0 and creates the private listener that init may delegate only to DWM. +KSTATUS RssrvSessionInitialize(RSSRV_SESSION_STATE* session, + RSSRV_NAMESPACE* namespaceState, + RSSRV_DISPLAY_STATE* display) { + if (!session || !namespaceState || !display) return NTLSTATUS_INVALID_PARAMETER; + memset(session, 0, sizeof(*session)); + session->Mutex = SYS_HANDLE_INVALID; + session->DwmListener = SYS_HANDLE_INVALID; + session->DwmEndpoint = SYS_HANDLE_INVALID; + session->Namespace = namespaceState; + session->Display = display; + session->Id = 0; + session->Generation = 1; + session->NextTargetId = 1; + + SYS_DISPLAY_INFO info; + memset(&info, 0, sizeof(info)); + KSTATUS status = RssrvDisplayQueryInfo(display, &info); + if (!KSUCCESS(status)) return status; + session->DisplayWidth = info.Mode.Width ? (int32_t)info.Mode.Width : 1; + session->DisplayHeight = info.Mode.Height ? (int32_t)info.Mode.Height : 1; + + status = SysCreateMutex(&session->Mutex); + if (!KSUCCESS(status)) return status; + status = RssrvNamespaceCreateDirectory(namespaceState, + RSSRV_SESSION_ZERO_PATH, + sizeof(RSSRV_SESSION_ZERO_PATH) - 1u); + if (!KSUCCESS(status)) return status; + + SYS_CREATE_PORT_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Flags = SYS_PORT_FLAG_NONE; + create.Backlog = RSSRV_DWM_LISTENER_BACKLOG; + create.HandleOut = &session->DwmListener; + return SysCreatePort(&create); +} + +// Registers one stable in-process input target with Session 0. +KSTATUS RssrvSessionRegisterInputTarget(RSSRV_SESSION_STATE* session, + RSSRV_INPUT_TARGET* target, + uint32_t kind, + void* context, + RSSRV_INPUT_TARGET_KEY_ROUTINE keyRoutine, + RSSRV_INPUT_TARGET_MOUSE_ROUTINE mouseRoutine) { + if (!session || !target || kind == RSSRV_INPUT_TARGET_KIND_NONE || !context) + return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvSessionLock(session); + if (!KSUCCESS(status)) return status; + if (target->Id != 0) { + RssrvSessionUnlock(session); + return NTLSTATUS_OBJECT_NAME_COLLISION; + } + memset(target, 0, sizeof(*target)); + target->Id = session->NextTargetId++; + if (target->Id == 0) target->Id = session->NextTargetId++; + target->Kind = kind; + target->Session = session; + target->Context = context; + target->KeyRoutine = keyRoutine; + target->MouseRoutine = mouseRoutine; + RssrvSessionChangedLocked(session); + RssrvSessionUnlock(session); + return NTLSTATUS_SUCCESS; +} + +// Activates one registered target and gives it ordinary keyboard and pointer focus. +KSTATUS RssrvSessionActivateInputTarget(RSSRV_SESSION_STATE* session, RSSRV_INPUT_TARGET* target) { + if (!session || !target || target->Id == 0 || target->Session != session) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvSessionLock(session); + if (!KSUCCESS(status)) return status; + session->ActiveTarget = target; + session->KeyboardFocus = target; + session->PointerFocus = target; + RssrvSessionChangedLocked(session); + RssrvSessionUnlock(session); + return NTLSTATUS_SUCCESS; +} + +// Sets keyboard focus without changing activation or pointer routing. +KSTATUS RssrvSessionSetKeyboardFocus(RSSRV_SESSION_STATE* session, RSSRV_INPUT_TARGET* target) { + if (!session || (target && (target->Id == 0 || target->Session != session))) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvSessionLock(session); + if (!KSUCCESS(status)) return status; + if (session->KeyboardFocus != target) { + session->KeyboardFocus = target; + RssrvSessionChangedLocked(session); + } + RssrvSessionUnlock(session); + return NTLSTATUS_SUCCESS; +} + +// Sets ordinary pointer focus without disturbing an active pointer capture. +KSTATUS RssrvSessionSetPointerFocus(RSSRV_SESSION_STATE* session, RSSRV_INPUT_TARGET* target) { + if (!session || (target && (target->Id == 0 || target->Session != session))) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvSessionLock(session); + if (!KSUCCESS(status)) return status; + if (session->PointerFocus != target) { + session->PointerFocus = target; + RssrvSessionChangedLocked(session); + } + RssrvSessionUnlock(session); + return NTLSTATUS_SUCCESS; +} + +// Sets or releases pointer capture independently of ordinary pointer focus. +KSTATUS RssrvSessionSetPointerCapture(RSSRV_SESSION_STATE* session, RSSRV_INPUT_TARGET* target) { + if (!session || (target && (target->Id == 0 || target->Session != session))) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvSessionLock(session); + if (!KSUCCESS(status)) return status; + if (session->PointerCapture != target) { + session->PointerCapture = target; + RssrvSessionChangedLocked(session); + } + RssrvSessionUnlock(session); + return NTLSTATUS_SUCCESS; +} + +// Returns the active session display bounds used to clamp the logical pointer. +KSTATUS RssrvSessionQueryDisplayBounds(RSSRV_SESSION_STATE* session, int32_t* widthOut, int32_t* heightOut) { + if (!session || !widthOut || !heightOut) return NTLSTATUS_INVALID_PARAMETER; + KSTATUS status = RssrvSessionLock(session); + if (!KSUCCESS(status)) return status; + *widthOut = session->DisplayWidth; + *heightOut = session->DisplayHeight; + RssrvSessionUnlock(session); + return NTLSTATUS_SUCCESS; +} + +// Gives DWM a non-consuming observation of one logical key before authoritative session routing occurs. +void RssrvSessionObserveKey(RSSRV_SESSION_STATE* session, const INPUT_KEY_RECORD* record) { + if (!session || !record) return; + if (!KSUCCESS(RssrvSessionLock(session))) return; + RssrvSessionSendDwmEventLocked(session, NTL_SESSION_DWM_EVENT_OBSERVE_KEY, record, 0, 0, 0); + RssrvSessionUnlock(session); +} + +// Gives DWM a non-consuming observation of one logical mouse event for hit testing and cursor policy. +void RssrvSessionObserveMouse(RSSRV_SESSION_STATE* session, + const INPUT_MOUSE_EVENT* event, + int32_t x, + int32_t y) { + if (!session || !event) return; + if (!KSUCCESS(RssrvSessionLock(session))) return; + RssrvSessionSendDwmEventLocked(session, NTL_SESSION_DWM_EVENT_OBSERVE_MOUSE, 0, event, x, y); + RssrvSessionUnlock(session); +} + +// Routes one logical keyboard record to the current keyboard-focus target. +void RssrvSessionRouteKey(RSSRV_SESSION_STATE* session, const INPUT_KEY_RECORD* record) { + if (!session || !record) return; + RSSRV_INPUT_TARGET* target = (RSSRV_INPUT_TARGET*)0; + if (KSUCCESS(RssrvSessionLock(session))) { + target = session->KeyboardFocus; + RssrvSessionUnlock(session); + } + if (target && target->KeyRoutine) target->KeyRoutine(target->Context, record); +} + +// Routes one logical mouse event through capture or pointer focus; DWM owns visible cursor policy. +void RssrvSessionRouteMouse(RSSRV_SESSION_STATE* session, + const INPUT_MOUSE_EVENT* event, + int32_t x, + int32_t y) { + if (!session || !event) return; + RSSRV_INPUT_TARGET* target = (RSSRV_INPUT_TARGET*)0; + if (KSUCCESS(RssrvSessionLock(session))) { + target = session->PointerCapture ? session->PointerCapture : session->PointerFocus; + RssrvSessionUnlock(session); + } + if (target && target->MouseRoutine) target->MouseRoutine(target->Context, event, x, y); +} + +// Publishes Session 0's console alias and starts the private DWM authority listener. +KSTATUS RssrvSessionStart(RSSRV_SESSION_STATE* session, RSSRV_CONSOLE_STATE* console) { + if (!session || !console || console->Listener == SYS_HANDLE_INVALID || !session->ActiveTarget || + session->DwmListener == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + + KSTATUS status = RssrvSessionRegisterInputTarget(session, &session->DesktopTarget, + RSSRV_INPUT_TARGET_KIND_DESKTOP, session, + RssrvSessionDwmRouteKey, RssrvSessionDwmRouteMouse); + if (!KSUCCESS(status)) return status; + + SYS_HANDLE sessionConsole = SYS_HANDLE_INVALID; + SYS_DERIVE_HANDLE_REQUEST derive; + memset(&derive, 0, sizeof(derive)); + derive.Size = sizeof(derive); + derive.SourceHandle = console->Listener; + derive.DesiredRights = SYS_RIGHT_TRANSFER | SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT; + derive.DerivedHandleOut = &sessionConsole; + status = SysDeriveHandle(&derive); + if (!KSUCCESS(status)) return status; + + status = RssrvNamespaceBindOwned(session->Namespace, + RSSRV_SESSION_ZERO_CONSOLE_PATH, + sizeof(RSSRV_SESSION_ZERO_CONSOLE_PATH) - 1u, + NTL_RESOURCE_KIND_CAPABILITY, + sessionConsole, + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT); + if (!KSUCCESS(status)) { + (void)SysCloseHandle(sessionConsole); + return status; + } + + status = RssrvNamespaceCreateSymbolicLink(session->Namespace, + RSSRV_CURRENT_CONSOLE_PATH, + sizeof(RSSRV_CURRENT_CONSOLE_PATH) - 1u, + RSSRV_SESSION_ZERO_CONSOLE_PATH, + sizeof(RSSRV_SESSION_ZERO_CONSOLE_PATH) - 1u); + if (!KSUCCESS(status)) return status; + + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)RssrvSessionDwmListenerThread; + create.Argument = (uintptr_t)session; + create.Priority = 1u; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + return SysCreateThread(&create); +} diff --git a/code/user/system/rssrv/volume.c b/code/user/system/rssrv/volume.c new file mode 100644 index 0000000..c27f1f0 --- /dev/null +++ b/code/user/system/rssrv/volume.c @@ -0,0 +1,279 @@ +// RSSRV mounted-volume publication and /Device/HarddiskVolumeN synchronization. + +#include "rssrv_internal.h" +#include +#include + +struct RSSRV_VOLUME_PUBLICATION { + RSSRV_VOLUME_PUBLICATION* Next; + uint32_t InstanceId; + uint32_t NamespaceId; + uint32_t PathLength; + char Path[RSSRV_VOLUME_PATH_CAPACITY]; +}; + +// Finds one published mounted volume by kernel instance identity. +static RSSRV_VOLUME_PUBLICATION* RssrvFindPublishedVolume(RSSRV_VOLUME_STATE* volumes, uint32_t instanceId) { + if (!volumes) return (RSSRV_VOLUME_PUBLICATION*)0; + for (RSSRV_VOLUME_PUBLICATION* publication = volumes->Published; publication; publication = publication->Next) { + if (publication->InstanceId == instanceId) + return publication; + } + return (RSSRV_VOLUME_PUBLICATION*)0; +} + +// Returns whether one kernel volume instance is present in the current manager snapshot. +static int RssrvSnapshotContainsVolume(const SYS_VOLUME_INFO* snapshot, uint32_t count, uint32_t instanceId) { + for (uint32_t i = 0; i < count; ++i) { + if (snapshot[i].InstanceId == instanceId) + return 1; + } + return 0; +} + +// Builds one RSSRV-local /Device/HarddiskVolumeN name without introducing a formatting-runtime dependency. +static KSTATUS RssrvBuildHarddiskVolumePath(uint32_t namespaceId, + char* path, + uint32_t capacity, + uint32_t* lengthOut) { + static const char prefix[] = "/Device/HarddiskVolume"; + if (!namespaceId || !path || !lengthOut) + return NTLSTATUS_INVALID_PARAMETER; + + uint32_t prefixLength = (uint32_t)(sizeof(prefix) - 1u); + char digits[10]; + uint32_t digitCount = 0; + uint32_t value = namespaceId; + do { + digits[digitCount++] = (char)('0' + (value % 10u)); + value /= 10u; + } while (value && digitCount < (uint32_t)sizeof(digits)); + + if (prefixLength + digitCount + 1u > capacity) + return NTLSTATUS_BUFFER_TOO_SMALL; + memcpy(path, prefix, prefixLength); + for (uint32_t i = 0; i < digitCount; ++i) + path[prefixLength + i] = digits[digitCount - 1u - i]; + path[prefixLength + digitCount] = '\0'; + *lengthOut = prefixLength + digitCount; + return NTLSTATUS_SUCCESS; +} + +// Takes a stable manager snapshot, growing the userspace buffer if the mounted-volume count changes. +static KSTATUS RssrvQueryVolumes(SYS_HANDLE manager, + SYS_VOLUME_INFO** volumesOut, + uint32_t* countOut, + uint64_t* generationOut) { + if (!volumesOut || !countOut || !generationOut) + return NTLSTATUS_INVALID_PARAMETER; + *volumesOut = (SYS_VOLUME_INFO*)0; + *countOut = 0; + *generationOut = 0; + + SYS_VOLUME_INFO* snapshot = (SYS_VOLUME_INFO*)0; + uint32_t capacity = 0; + for (;;) { + uint32_t count = 0; + uint64_t generation = 0; + SYS_QUERY_VOLUMES_REQUEST query; + memset(&query, 0, sizeof(query)); + query.Size = sizeof(query); + query.ManagerHandle = manager; + query.Flags = SYS_VOLUME_QUERY_FLAG_NONE; + query.Capacity = capacity; + query.Volumes = snapshot; + query.CountOut = &count; + query.GenerationOut = &generation; + KSTATUS status = SysQueryVolumes(&query); + if (status == NTLSTATUS_BUFFER_TOO_SMALL) { + if (snapshot) free(snapshot); + snapshot = (SYS_VOLUME_INFO*)0; + capacity = count; + if (capacity) { + snapshot = (SYS_VOLUME_INFO*)malloc((size_t)capacity * sizeof(*snapshot)); + if (!snapshot) + return NTLSTATUS_NO_MEMORY; + memset(snapshot, 0, (size_t)capacity * sizeof(*snapshot)); + } + continue; + } + if (!KSUCCESS(status)) { + if (snapshot) free(snapshot); + return status; + } + for (uint32_t i = 0; i < count; ++i) { + if (snapshot[i].Size != sizeof(SYS_VOLUME_INFO) || + snapshot[i].Version != SYS_VOLUME_ENUM_VERSION || !snapshot[i].InstanceId || + snapshot[i].Flags != 0) { + if (snapshot) free(snapshot); + return NTLSTATUS_INVALID_PARAMETER; + } + } + *volumesOut = snapshot; + *countOut = count; + *generationOut = generation; + return NTLSTATUS_SUCCESS; + } +} + +// Keeps automatic /Device/HarddiskVolumeN bindings synchronized with mounted IO_VOLUME instances. +static void RssrvVolumeWorker(uintptr_t argument) { + RSSRV_VOLUME_STATE* volumes = (RSSRV_VOLUME_STATE*)argument; + if (!volumes) { + SysExitThread(1); + return; + } + RssrvSetCurrentThreadName("rssrv.volume"); + for (;;) { + uint64_t generation = 0; + KSTATUS status = RssrvVolumesSynchronize(volumes, &generation); + if (!KSUCCESS(status)) + RssrvExit(12); + status = SysWaitVolumeChange(volumes->Manager, generation, SYS_WAIT_TIMEOUT_INFINITE); + if (!KSUCCESS(status)) + RssrvExit(12); + } +} + +// Initializes mounted-volume publication state against one volume-manager capability. +KSTATUS RssrvVolumesInitialize(RSSRV_VOLUME_STATE* volumes, + RSSRV_NAMESPACE* namespaceState, + SYS_HANDLE manager) { + if (!volumes || !namespaceState || manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + memset(volumes, 0, sizeof(*volumes)); + volumes->Namespace = namespaceState; + volumes->Manager = manager; + volumes->NextHarddiskVolumeId = 1u; + return NTLSTATUS_SUCCESS; +} + +// Reconciles RSSRV volume bindings with the kernel's mounted-volume snapshot. +KSTATUS RssrvVolumesSynchronize(RSSRV_VOLUME_STATE* volumes, uint64_t* generationOut) { + if (!volumes || !volumes->Namespace || volumes->Manager == SYS_HANDLE_INVALID || !generationOut) + return NTLSTATUS_INVALID_PARAMETER; + + SYS_VOLUME_INFO* snapshot = (SYS_VOLUME_INFO*)0; + uint32_t count = 0; + uint64_t generation = 0; + KSTATUS status = RssrvQueryVolumes(volumes->Manager, &snapshot, &count, &generation); + if (!KSUCCESS(status)) + return status; + + RSSRV_VOLUME_PUBLICATION** link = &volumes->Published; + while (*link) { + RSSRV_VOLUME_PUBLICATION* publication = *link; + if (RssrvSnapshotContainsVolume(snapshot, count, publication->InstanceId)) { + link = &publication->Next; + continue; + } + status = RssrvNamespaceUnbind(volumes->Namespace, publication->Path, publication->PathLength); + if (!KSUCCESS(status) && status != NTLSTATUS_NOT_FOUND) { + free(snapshot); + return status; + } + *link = publication->Next; + free(publication); + } + + for (uint32_t i = 0; i < count; ++i) { + if (RssrvFindPublishedVolume(volumes, snapshot[i].InstanceId)) + continue; + + SYS_HANDLE volume = SYS_HANDLE_INVALID; + status = SysOpenVolume(volumes->Manager, snapshot[i].InstanceId, + SYS_RIGHT_QUERY | SYS_RIGHT_TRANSFER | + SYS_VOLUME_RIGHT_OPEN | SYS_VOLUME_RIGHT_MODIFY, + &volume); + if (status == NTLSTATUS_NOT_FOUND || status == NTLSTATUS_OBJECT_DEAD) + continue; + if (!KSUCCESS(status)) { + free(snapshot); + return status; + } + + RSSRV_VOLUME_PUBLICATION* publication = + (RSSRV_VOLUME_PUBLICATION*)malloc(sizeof(*publication)); + if (!publication) { + (void)SysCloseHandle(volume); + free(snapshot); + return NTLSTATUS_NO_MEMORY; + } + memset(publication, 0, sizeof(*publication)); + publication->InstanceId = snapshot[i].InstanceId; + publication->NamespaceId = volumes->NextHarddiskVolumeId; + status = RssrvBuildHarddiskVolumePath(publication->NamespaceId, + publication->Path, + (uint32_t)sizeof(publication->Path), + &publication->PathLength); + if (!KSUCCESS(status)) { + free(publication); + (void)SysCloseHandle(volume); + free(snapshot); + return status; + } + + status = RssrvNamespaceBindOwned(volumes->Namespace, + publication->Path, + publication->PathLength, + NTL_RESOURCE_KIND_VOLUME, + volume, + SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN | SYS_VOLUME_RIGHT_MODIFY); + if (!KSUCCESS(status)) { + free(publication); + (void)SysCloseHandle(volume); + free(snapshot); + return status; + } + publication->Next = volumes->Published; + volumes->Published = publication; + volumes->NextHarddiskVolumeId++; + if (!volumes->NextHarddiskVolumeId) { + free(snapshot); + return NTLSTATUS_LIMIT_REACHED; + } + } + + if (snapshot) free(snapshot); + *generationOut = generation; + return NTLSTATUS_SUCCESS; +} + +// Copies the namespace path assigned to one mounted kernel volume instance. +KSTATUS RssrvVolumesQueryPublishedPath(RSSRV_VOLUME_STATE* volumes, + uint32_t instanceId, + char* pathOut, + uint32_t capacity, + uint32_t* lengthOut) { + if (!volumes || !instanceId || !pathOut || !capacity || !lengthOut) + return NTLSTATUS_INVALID_PARAMETER; + *lengthOut = 0; + RSSRV_VOLUME_PUBLICATION* publication = RssrvFindPublishedVolume(volumes, instanceId); + if (!publication) return NTLSTATUS_NOT_FOUND; + if (publication->PathLength + 1u > capacity) + return NTLSTATUS_BUFFER_TOO_SMALL; + memcpy(pathOut, publication->Path, publication->PathLength + 1u); + *lengthOut = publication->PathLength; + return NTLSTATUS_SUCCESS; +} + +// Starts the mounted-volume namespace publication worker. +KSTATUS RssrvVolumesStartWorker(RSSRV_VOLUME_STATE* volumes) { + if (!volumes || volumes->Manager == SYS_HANDLE_INVALID) + return NTLSTATUS_INVALID_PARAMETER; + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)RssrvVolumeWorker; + create.Argument = (uintptr_t)volumes; + create.Priority = 1; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + return SysCreateThread(&create); +} + +// Returns whether a path belongs to the RSSRV-owned automatic mounted-volume naming family. +int RssrvVolumesOwnPath(const char* path, uint32_t length) { + static const char prefix[] = "/Device/HarddiskVolume"; + uint32_t prefixLength = (uint32_t)(sizeof(prefix) - 1u); + return path && length >= prefixLength && memcmp(path, prefix, prefixLength) == 0; +} diff --git a/code/user/tests/console_service_smoke/console_service_smoke.c b/code/user/tests/console_service_smoke/console_service_smoke.c new file mode 100644 index 0000000..d2905a3 --- /dev/null +++ b/code/user/tests/console_service_smoke/console_service_smoke.c @@ -0,0 +1,101 @@ +// Console-service smoke test. Verifies RSSRV publication, query, write, and cursor operations. + +#include +#include +#include +#include +#include + +static void exit_test(int code) { + for (;;) SysExitProcess(code); +} + +// Writes one diagnostic string through the transitional native console. +static void print(const char* text) { + SYS_WRITE_CONSOLE_REQUEST request = { sizeof(request), SysSvFromCstr(text) }; + (void)SysWriteConsole(&request); +} + +// Writes one status value in hexadecimal without a formatting runtime. +static void print_status(KSTATUS status) { + static const char digits[] = "0123456789ABCDEF"; + char buffer[11] = "0x00000000"; + for (uint32_t i = 0; i < 8u; ++i) + buffer[2u + i] = digits[(status >> ((7u - i) * 4u)) & 0xFu]; + print(buffer); +} + +// Reports one failed console-service operation with its native status. +static void fail_status(const char* operation, KSTATUS status, int code) { + print("console_service_smoke: "); + print(operation); + print(" failed status="); + print_status(status); + print("\n"); + exit_test(code); +} + +// Reports one failed console-service invariant. +static void fail(const char* message, int code) { + print("console_service_smoke: "); + print(message); + print("\n"); + exit_test(code); +} + +// Runs the console-service smoke contract. +int main(void) { + SYS_HANDLE resources = SYS_HANDLE_INVALID; + KSTATUS status = NtlResourceConnect(&resources); + if (!KSUCCESS(status)) fail_status("connect resource service", status, 1); + + uint32_t kind = NTL_RESOURCE_KIND_INVALID; + status = NtlResourceQuery(resources, SYS_SV_LITERAL("/Sessions/0"), &kind); + if (!KSUCCESS(status)) fail_status("query /Sessions/0", status, 2); + if (kind != NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) fail("/Sessions/0 has wrong resource kind", 2); + // DWM is noncritical, so the console contract must not depend on the desktop endpoint being published. + status = NtlResourceQuery(resources, SYS_SV_LITERAL("/Sessions/0/Console"), &kind); + if (!KSUCCESS(status)) fail_status("query /Sessions/0/Console", status, 3); + if (kind != NTL_RESOURCE_KIND_CAPABILITY) fail("/Sessions/0/Console has wrong resource kind", 3); + status = NtlResourceQuery(resources, SYS_SV_LITERAL("/Console"), &kind); + if (!KSUCCESS(status)) fail_status("query /Console", status, 4); + if (kind != NTL_RESOURCE_KIND_SYMBOLIC_LINK) fail("/Console has wrong resource kind", 4); + SYS_HANDLE alias = SYS_HANDLE_INVALID; + status = NtlResourceLookup(resources, SYS_SV_LITERAL("/Console"), + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT, &alias); + if (!KSUCCESS(status)) fail_status("lookup /Console", status, 5); + if (alias == SYS_HANDLE_INVALID) fail("lookup /Console returned invalid handle", 5); + (void)SysCloseHandle(alias); + (void)NtlResourceClose(resources); + + SYS_HANDLE session = SYS_HANDLE_INVALID; + status = NtlConsoleConnect(&session); + if (!KSUCCESS(status)) fail_status("connect console", status, 6); + + NTL_CONSOLE_INFO before; + memset(&before, 0, sizeof(before)); + status = NtlConsoleQueryInfo(session, &before); + if (!KSUCCESS(status)) fail_status("query console info", status, 7); + if (before.Size != sizeof(before) || before.Flags != NTL_CONSOLE_FLAG_NONE || + before.Columns == 0 || before.Rows == 0 || before.PixelWidth == 0 || before.PixelHeight == 0) + fail("initial console info is invalid", 7); + + static const char message[] = "[console_service_smoke] PASS\n"; + size_t written = 0; + status = NtlConsoleWrite(session, message, sizeof(message) - 1u, &written); + if (!KSUCCESS(status)) fail_status("write console", status, 8); + if (written != sizeof(message) - 1u) fail("console write returned short byte count", 8); + + NTL_CONSOLE_INFO after; + memset(&after, 0, sizeof(after)); + status = NtlConsoleQueryInfo(session, &after); + if (!KSUCCESS(status)) fail_status("query console info after write", status, 9); + if (after.Generation <= before.Generation) fail("console generation did not advance", 9); + + status = NtlConsoleSetCursor(session, (int)after.CursorX, (int)after.CursorY); + if (!KSUCCESS(status)) fail_status("set console cursor", status, 10); + + (void)NtlConsoleClose(session); + exit_test(0); + return 0; +} diff --git a/code/user/tests/desktop_smoke/desktop_smoke.c b/code/user/tests/desktop_smoke/desktop_smoke.c new file mode 100644 index 0000000..970ccfc --- /dev/null +++ b/code/user/tests/desktop_smoke/desktop_smoke.c @@ -0,0 +1,138 @@ +// Desktop protocol smoke test. Exercises window ownership, shared surfaces, z-order, focus, capture, and pointer policy. + +#include +#include +#include +#include +#include + +#define TEST_WIDTH 160u +#define TEST_HEIGHT 100u + +// Exits without depending on a compiler runtime return path. +static void exit_now(int code) { + while (true) SysExitProcess(code); +} + +// Writes one diagnostic string through the transitional native console. +static void print(const char* text) { + SYS_WRITE_CONSOLE_REQUEST request = { sizeof(request), SysSvFromCstr(text) }; + (void)SysWriteConsole(&request); +} + +// Writes one status value in hexadecimal without a formatting runtime. +static void print_status(KSTATUS status) { + static const char digits[] = "0123456789ABCDEF"; + char buffer[11] = "0x00000000"; + for (uint32_t i = 0; i < 8u; ++i) + buffer[2u + i] = digits[(status >> ((7u - i) * 4u)) & 0xFu]; + print(buffer); +} + +// Reports one failed desktop-smoke operation with its native status. +static void fail_status(const char* operation, KSTATUS status, int code) { + print("desktop_smoke: "); + print(operation); + print(" failed status="); + print_status(status); + print("\n"); + exit_now(code); +} + +// Reports one failed desktop-smoke invariant. +static void fail(const char* message, int code) { + print("desktop_smoke: "); + print(message); + print("\n"); + exit_now(code); +} + +// Fills one mapped XRGB surface with a deterministic test pattern. +static void fill_surface(void* base, uint32_t pitch) { + uint8_t* bytes = (uint8_t*)base; + for (uint32_t y = 0; y < TEST_HEIGHT; ++y) { + uint32_t* row = (uint32_t*)(bytes + (size_t)y * pitch); + for (uint32_t x = 0; x < TEST_WIDTH; ++x) + row[x] = 0x00202080u + ((x & 0x3Fu) << 16) + ((y & 0x3Fu) << 8); + } +} + +// Exercises the first final-shaped DWM client contract. +int main(void) { + SYS_HANDLE desktop = SYS_HANDLE_INVALID; + KSTATUS status = NtlDesktopConnect(&desktop); + if (!KSUCCESS(status)) fail_status("connect /Sessions/0/Desktop", status, 1); + + SYS_HANDLE surface = SYS_HANDLE_INVALID; + SYS_CREATE_SURFACE_REQUEST createSurface; + memset(&createSurface, 0, sizeof(createSurface)); + createSurface.Size = sizeof(createSurface); + createSurface.Width = TEST_WIDTH; + createSurface.Height = TEST_HEIGHT; + createSurface.Format = SYS_SURFACE_FORMAT_XRGB8888; + createSurface.Flags = SYS_SURFACE_FLAG_NONE; + createSurface.HandleOut = &surface; + status = SysCreateSurface(&createSurface); + if (!KSUCCESS(status)) fail_status("create surface", status, 2); + + void* base = (void*)0; + uint32_t pitch = 0; + status = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, &base, &pitch); + if (!KSUCCESS(status)) fail_status("map surface", status, 3); + if (!base || pitch < TEST_WIDTH * sizeof(uint32_t)) fail("invalid surface mapping", 3); + fill_surface(base, pitch); + + DISPLAY_RECT bounds = { 40, 40, (int32_t)TEST_WIDTH, (int32_t)TEST_HEIGHT }; + NTL_WINDOW_ID window = 0; + status = NtlDesktopCreateWindow(desktop, &bounds, &window); + if (!KSUCCESS(status)) fail_status("create window", status, 4); + if (window == 0) fail("create window returned zero ID", 4); + status = NtlDesktopAttachSurface(desktop, window, surface); + if (!KSUCCESS(status)) fail_status("attach surface", status, 5); + status = NtlDesktopSetVisible(desktop, window, 1u); + if (!KSUCCESS(status)) fail_status("show window", status, 6); + + DISPLAY_RECT damage = { 0, 0, (int32_t)TEST_WIDTH, (int32_t)TEST_HEIGHT }; + status = SysCommitSurface(surface, &damage, 1u, 0); + if (!KSUCCESS(status)) fail_status("commit surface", status, 7); + + NTL_DESKTOP_REPLY query; + memset(&query, 0, sizeof(query)); + status = NtlDesktopQueryWindow(desktop, window, &query); + if (!KSUCCESS(status)) fail_status("query window", status, 8); + if (query.WindowId != window || !query.Visible || !query.HasSurface || + query.SurfaceWidth != TEST_WIDTH || query.SurfaceHeight != TEST_HEIGHT) + fail("query window returned inconsistent state", 8); + + status = NtlDesktopRequestFocus(desktop, window); + if (!KSUCCESS(status)) fail_status("request focus", status, 9); + status = NtlDesktopSetPointerMode(desktop, window, NTL_DESKTOP_POINTER_CONFINED); + if (!KSUCCESS(status)) fail_status("set confined pointer mode", status, 10); + status = NtlDesktopRequestCapture(desktop, window); + if (!KSUCCESS(status)) fail_status("request capture", status, 11); + status = NtlDesktopSetPointerMode(desktop, window, NTL_DESKTOP_POINTER_RELATIVE_LOCKED); + if (!KSUCCESS(status)) fail_status("set relative-locked pointer mode", status, 12); + status = NtlDesktopReleaseCapture(desktop, window); + if (!KSUCCESS(status)) fail_status("release capture", status, 13); + status = NtlDesktopSetPointerMode(desktop, window, NTL_DESKTOP_POINTER_FREE); + if (!KSUCCESS(status)) fail_status("set free pointer mode", status, 14); + + bounds.X += 24; + bounds.Y += 18; + status = NtlDesktopSetBounds(desktop, window, &bounds); + if (!KSUCCESS(status)) fail_status("set bounds", status, 15); + status = NtlDesktopLowerWindow(desktop, window); + if (!KSUCCESS(status)) fail_status("lower window", status, 16); + status = NtlDesktopRaiseWindow(desktop, window); + if (!KSUCCESS(status)) fail_status("raise window", status, 17); + status = NtlDesktopDetachSurface(desktop, window); + if (!KSUCCESS(status)) fail_status("detach surface", status, 18); + status = NtlDesktopDestroyWindow(desktop, window); + if (!KSUCCESS(status)) fail_status("destroy window", status, 19); + + (void)SysUnmapSurface(base); + (void)SysCloseHandle(surface); + (void)NtlDesktopClose(desktop); + print("desktop_smoke: PASS\n"); + return 0; +} diff --git a/code/user/tests/exception_smoke/exception_smoke.c b/code/user/tests/exception_smoke/exception_smoke.c index ac04455..936df46 100644 --- a/code/user/tests/exception_smoke/exception_smoke.c +++ b/code/user/tests/exception_smoke/exception_smoke.c @@ -196,7 +196,8 @@ static int noncontinuable_handler(SYS_EXCEPTION_RECORD* record, void* userContext) { (void)context; (void)userContext; - if (!record || record->Code != SMOKE_NONCONTINUABLE_CODE) + if (!record || record->Code != SYS_EXCEPTION_CODE_USER_RAISED || + record->UserCode != SMOKE_NONCONTINUABLE_CODE) exit_now(40); if ((record->Flags & SYS_EXCEPTION_FLAG_NONCONTINUABLE) == 0) exit_now(41); @@ -213,7 +214,7 @@ static void child_noncontinuable(void) { memset(&request, 0, sizeof(request)); request.Size = sizeof(request); request.Flags = SYS_RAISE_EXCEPTION_FLAG_NONCONTINUABLE; - request.Code = SMOKE_NONCONTINUABLE_CODE; + request.UserCode = SMOKE_NONCONTINUABLE_CODE; uint32_t status = SysRaiseException(&request); exit_now((int32_t)status); diff --git a/code/user/tests/file_lock_smoke/file_lock_smoke.c b/code/user/tests/file_lock_smoke/file_lock_smoke.c index 35d8785..27cae6d 100644 --- a/code/user/tests/file_lock_smoke/file_lock_smoke.c +++ b/code/user/tests/file_lock_smoke/file_lock_smoke.c @@ -129,6 +129,21 @@ static uint32_t unlock_range(uint32_t file, uint64_t offset, uint64_t length) { return SysUnlockFile(&req); } +// Queries the first conflicting lock through the public request ABI. +static uint32_t query_range(uint32_t file, uint32_t mode, uint64_t offset, uint64_t length, + SYS_FILE_LOCK_INFO* infoOut) { + SYS_QUERY_FILE_LOCK_REQUEST req = { + sizeof(req), + file, + mode, + SYS_FILE_LOCK_FLAG_NONE, + offset, + length, + infoOut, + }; + return SysQueryFileLock(&req); +} + // Splits the child command line into mode and path. static int parse_child_command(SYS_STRING_VIEW view, char* mode, size_t modeCap, char* path, size_t pathCap) { if (!view.Data || view.Length == 0) @@ -159,6 +174,7 @@ static int is_child_mode(const char* mode) { str_eq(mode, "excl-wait") || str_eq(mode, "shared-ok") || str_eq(mode, "excl-nonoverlap") || + str_eq(mode, "query-exclusive") || str_eq(mode, "hold-exit"); } @@ -210,6 +226,17 @@ static void run_child(SYS_STRING_VIEW commandLine) { exit_now(s == NTLSTATUS_SUCCESS ? 0 : 8); } + if (str_eq(mode, "query-exclusive")) { + SYS_FILE_LOCK_INFO info; + memset(&info, 0, sizeof(info)); + s = query_range(file, SYS_FILE_LOCK_EXCLUSIVE, LOCK_OFFSET, LOCK_LENGTH, &info); + SysCloseHandle(file); + exit_now(s == NTLSTATUS_SUCCESS && + info.Mode == SYS_FILE_LOCK_EXCLUSIVE && + info.Offset == LOCK_OFFSET && info.Length == LOCK_LENGTH && + info.OwnerProcessId != 0 ? 0 : 11); + } + if (str_eq(mode, "hold-exit")) { s = lock_range(file, SYS_FILE_LOCK_EXCLUSIVE, LOCK_OFFSET, LOCK_LENGTH, 0); SysCloseHandle(file); @@ -329,6 +356,7 @@ int main(void) { s = lock_range(file, SYS_FILE_LOCK_EXCLUSIVE, LOCK_OFFSET, LOCK_LENGTH, 0); if (s != NTLSTATUS_SUCCESS) fail_status("exclusive parent lock", s); run_child_expect_success("excl-blocked", path); + run_child_expect_success("query-exclusive", path); run_child_expect_success("excl-timeout", path); run_child_expect_success("excl-nonoverlap", path); s = unlock_range(file, LOCK_OFFSET, LOCK_LENGTH); diff --git a/code/user/tests/input_service_smoke/input_service_smoke.c b/code/user/tests/input_service_smoke/input_service_smoke.c new file mode 100644 index 0000000..7183143 --- /dev/null +++ b/code/user/tests/input_service_smoke/input_service_smoke.c @@ -0,0 +1,83 @@ +// Logical input-service smoke test. It verifies RSSRV publication and nonblocking protocol behavior without requiring user input. + +#include +#include +#include +#include +#include + +static int gFailures; + +// Writes one diagnostic line fragment. +static void print(const char* text) { + SYS_WRITE_CONSOLE_REQUEST request = { sizeof(request), SysSvFromCstr(text) }; + (void)SysWriteConsole(&request); +} + +// Records one failed expectation. +static void fail(const char* text) { + print("input_service_smoke: FAIL: "); + print(text); + print("\n"); + gFailures++; +} + +// Checks one status against an allowed nonblocking result pair. +static void expect_try_status(const char* what, KSTATUS status) { + if (status != NTLSTATUS_SUCCESS && status != NTLSTATUS_WOULD_BLOCK) + fail(what); +} + +// Exercises RSSRV's logical-input listener and snapshot contract. +int main(void) { + print("input_service_smoke: start\n"); + + SYS_HANDLE resource = SYS_HANDLE_INVALID; + KSTATUS status = NtlResourceConnect(&resource); + if (!KSUCCESS(status)) { + fail("resource connect"); + return 1; + } + uint32_t kind = NTL_RESOURCE_KIND_INVALID; + status = NtlResourceQuery(resource, SYS_SV_LITERAL(NTL_INPUT_SERVICE_PATH), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_CAPABILITY) + fail("/Services/Input publication"); + (void)NtlResourceClose(resource); + + if (NtlInputConnect((SYS_HANDLE*)0) != NTLSTATUS_INVALID_PARAMETER) + fail("connect null"); + + SYS_HANDLE input = SYS_HANDLE_INVALID; + status = NtlInputConnect(&input); + if (!KSUCCESS(status) || input == SYS_HANDLE_INVALID) { + fail("input connect"); + return 1; + } + + NTL_INPUT_STATE state; + memset(&state, 0, sizeof(state)); + status = NtlInputQueryState(input, &state); + if (!KSUCCESS(status) || state.Size != sizeof(state) || state.Flags != NTL_INPUT_STATE_FLAG_NONE) + fail("query state"); + if (state.MouseX < 0 || state.MouseY < 0) + fail("pointer bounds"); + + INPUT_KEY_RECORD key; + status = NtlInputPeekEvent(input, 0, &key); + expect_try_status("peek key", status); + + INPUT_MOUSE_EVENT mouse; + status = NtlInputPeekMouse(input, 0, &mouse); + expect_try_status("peek mouse", status); + + NTL_INPUT_STATE defaultState; + memset(&defaultState, 0, sizeof(defaultState)); + status = NtlInputQueryDefaultState(&defaultState); + if (!KSUCCESS(status) || defaultState.Size != sizeof(defaultState)) + fail("default query state"); + + (void)NtlInputClose(input); + if (gFailures) return 1; + print("input_service_smoke: PASS\n"); + return 0; +} diff --git a/code/user/tests/native_raise_exception_smoke/native_raise_exception_smoke.c b/code/user/tests/native_raise_exception_smoke/native_raise_exception_smoke.c index 182a407..81d4f0a 100644 --- a/code/user/tests/native_raise_exception_smoke/native_raise_exception_smoke.c +++ b/code/user/tests/native_raise_exception_smoke/native_raise_exception_smoke.c @@ -85,8 +85,10 @@ static int smoke_handler(SYS_EXCEPTION_RECORD* record, SYS_USER_CONTEXT* context fail("handler context mismatch"); if (!record || !context) fail("handler received null records"); - if (record->Code != SMOKE_EXCEPTION_CODE) - fail("handler received wrong exception code"); + if (record->Code != SYS_EXCEPTION_CODE_USER_RAISED) + fail("handler received wrong native exception class"); + if (record->UserCode != SMOKE_EXCEPTION_CODE) + fail("handler received wrong user exception code"); if ((record->Flags & SYS_EXCEPTION_FLAG_FIRST_CHANCE) == 0) fail("handler missing first-chance flag"); if (record->Flags & SYS_EXCEPTION_FLAG_NONCONTINUABLE) @@ -117,7 +119,7 @@ int main(void) { SYS_RAISE_EXCEPTION_REQUEST request; request.Size = sizeof(request); request.Flags = SYS_RAISE_EXCEPTION_FLAG_NONE; - request.Code = SMOKE_EXCEPTION_CODE; + request.UserCode = SMOKE_EXCEPTION_CODE; request.ParameterCount = 2; request.Parameters = args; @@ -134,8 +136,8 @@ int main(void) { after.UserRaised < before.UserRaised + 1 || after.Continued < before.Continued + 1) fail("exception counters did not advance"); - if (after.LastCode != SMOKE_EXCEPTION_CODE) - fail("last exception code was not updated"); + if (after.LastCode != SYS_EXCEPTION_CODE_USER_RAISED) + fail("last native exception class was not updated"); print("native_raise_exception_smoke: PASS\n"); exit_now(0); diff --git a/code/user/tests/pcache_multithread/pcache_multithread.c b/code/user/tests/pcache_multithread/pcache_multithread.c index b37f953..d0a3671 100644 --- a/code/user/tests/pcache_multithread/pcache_multithread.c +++ b/code/user/tests/pcache_multithread/pcache_multithread.c @@ -110,6 +110,10 @@ static void thread_main(uintptr_t arg) { THREAD_CTX* ctx = (THREAD_CTX*)arg; if (!ctx) SysExitThread(1); + print("pcache_multithread: worker start "); + print_u64(ctx->Index); + print("\n"); + for (uint32_t iter = 0; iter < ITERATIONS; ++iter) { void* base = 0; SYS_MAP_FILE_REQUEST mapFile = { @@ -122,11 +126,23 @@ static void thread_main(uintptr_t arg) { SysExitThread(1); } + if (iter == 0) { + print("pcache_multithread: worker mapped "); + print_u64(ctx->Index); + print("\n"); + } + volatile uint8_t* touch = (volatile uint8_t*)base; for (size_t off = 0; off < TEST_SIZE; off += 4096) (void)touch[off]; (void)touch[TEST_SIZE - 1]; + if (iter == 0) { + print("pcache_multithread: worker faulted "); + print_u64(ctx->Index); + print("\n"); + } + s = validate_mapping((const uint8_t*)base); uint32_t fs = SysFreeVm(base); if (s != NTLSTATUS_SUCCESS) { @@ -137,9 +153,17 @@ static void thread_main(uintptr_t arg) { ctx->Status = fs; SysExitThread(1); } + if (iter == 0) { + print("pcache_multithread: worker unmapped "); + print_u64(ctx->Index); + print("\n"); + } ctx->IterationsDone++; } + print("pcache_multithread: worker done "); + print_u64(ctx->Index); + print("\n"); ctx->Status = NTLSTATUS_SUCCESS; SysExitThread(0); } @@ -152,12 +176,14 @@ int main(void) { const char* path = 0; uint32_t s = create_temp(&file, &path); if (s != NTLSTATUS_SUCCESS) fail_status("create temp", s); + print("pcache_multithread: temp created\n"); size_t written = 0; SYS_FILE_IO_REQUEST writeReq = SysMakeFileIoRequest(file, gPattern, sizeof(gPattern), &written); s = SysWriteFile(&writeReq); if (s != NTLSTATUS_SUCCESS) fail_status("write pattern", s); if (written != sizeof(gPattern)) fail("short write"); + print("pcache_multithread: pattern written\n"); uint32_t mapping = 0; SYS_CREATE_FILE_MAPPING_REQUEST createMapping = { @@ -166,10 +192,12 @@ int main(void) { }; s = SysCreateFileMapping(&createMapping); if (s != NTLSTATUS_SUCCESS) fail_status("create mapping", s); + print("pcache_multithread: mapping created\n"); SYS_FILE_CACHE_STATS before; SYS_FILE_CACHE_STATS after; if (query_cache(&before) != NTLSTATUS_SUCCESS) fail("initial stats query failed"); + print("pcache_multithread: stats queried\n"); uint32_t handles[THREAD_COUNT]; uint32_t tids[THREAD_COUNT]; @@ -192,10 +220,17 @@ int main(void) { s = SysCreateThread(&req); if (s != NTLSTATUS_SUCCESS) fail_status("create thread", s); } + print("pcache_multithread: threads created\n"); for (uint32_t i = 0; i < THREAD_COUNT; ++i) { + print("pcache_multithread: waiting worker "); + print_u64(i); + print("\n"); s = SysWait(handles[i], SYS_WAIT_FLAG_NONE); if (s != NTLSTATUS_SUCCESS) fail_status("wait thread", s); + print("pcache_multithread: joined worker "); + print_u64(i); + print("\n"); SysCloseHandle(handles[i]); } diff --git a/code/user/tests/posix_fd_smoke/posix_fd_smoke.c b/code/user/tests/posix_fd_smoke/posix_fd_smoke.c new file mode 100644 index 0000000..9428d9b --- /dev/null +++ b/code/user/tests/posix_fd_smoke/posix_fd_smoke.c @@ -0,0 +1,262 @@ +// User-mode smoke test for POSIX descriptor and open-description semantics. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Writes one NUL-terminated diagnostic to stdout. +static void print(const char* text) { + (void)write(1, text, strlen(text)); +} + +// Terminates the smoke test with one diagnostic. +static void fail(const char* message) { + print("posix_fd_smoke: "); + print(message); + print("\n"); + exit(1); +} + +#define INHERIT_FILE_FD 10 +#define INHERIT_PIPE_A_READ 11 +#define INHERIT_PIPE_A_WRITE 12 +#define INHERIT_CLOEXEC_FD 13 +#define INHERIT_PIPE_B_READ 14 +#define INHERIT_PIPE_B_WRITE 15 + +// Returns whether this invocation is the descriptor-inheritance child. +static int is_inheritance_child(void) { + const char* command = posix_get_command_line(); + return command && strstr(command, "--inherit-child") != NULL; +} + +// Runs the receiving side of the cross-process descriptor inheritance contract. +static int run_inheritance_child(void) { + char bytes[3] = {0, 0, 0}; + if (read(INHERIT_FILE_FD, bytes, 2) != 2 || strcmp(bytes, "cd") != 0) + fail("inherited shared file offset failed in child"); + + errno = 0; + if (fcntl(INHERIT_CLOEXEC_FD, F_GETFD) != -1 || errno != EBADF) + fail("FD_CLOEXEC descriptor survived spawn inheritance"); + if ((fcntl(INHERIT_PIPE_A_READ, F_GETFL) & O_NONBLOCK) == 0) + fail("inherited pipe status flags were not shared"); + if (fcntl(INHERIT_FILE_FD, F_SETFL, O_APPEND) != 0) + fail("child F_SETFL failed"); + + if (write(INHERIT_PIPE_A_WRITE, "R", 1) != 1) + fail("child ready signal failed"); + char byte = 0; + if (read(INHERIT_PIPE_B_READ, &byte, 1) != 1 || byte != 'x') + fail("cross-process pipe read failed"); + if (close(INHERIT_PIPE_B_READ) != 0) + fail("child read endpoint close failed"); + if (write(INHERIT_PIPE_A_WRITE, "C", 1) != 1) + fail("child close signal failed"); + if (close(INHERIT_PIPE_A_WRITE) != 0) + fail("child write endpoint close failed"); + return 0; +} + +// Moves one descriptor to an exact number and closes the original alias. +static void move_fd(int source, int target) { + if (source < 0 || dup2(source, target) != target) + fail("inheritance dup2 setup failed"); + if (source != target && close(source) != 0) + fail("inheritance source close failed"); +} + +// Waits for and consumes one byte from the inherited control pipe. +static char wait_control_byte(void) { + struct pollfd descriptor; + descriptor.fd = INHERIT_PIPE_A_READ; + descriptor.events = POLLIN | POLLHUP; + descriptor.revents = 0; + if (poll(&descriptor, 1, 5000) <= 0) + fail("inheritance control poll timed out"); + char value = 0; + if (read(INHERIT_PIPE_A_READ, &value, 1) != 1) + fail("inheritance control read failed"); + return value; +} + +// Proves open-description sharing across two distinct native processes. +static void run_cross_process_inheritance(void) { + const char* path = "posix_fd_inherit.tmp"; + (void)unlink(path); + int file = open(path, O_CREAT | O_TRUNC | O_RDWR); + if (file < 0 || write(file, "abcdef", 6) != 6 || lseek(file, 0, SEEK_SET) != 0) + fail("inheritance file setup failed"); + char prefix[3] = {0, 0, 0}; + if (read(file, prefix, 2) != 2 || strcmp(prefix, "ab") != 0) + fail("inheritance initial offset setup failed"); + move_fd(file, INHERIT_FILE_FD); + + if (dup2(INHERIT_FILE_FD, INHERIT_CLOEXEC_FD) != INHERIT_CLOEXEC_FD || + fcntl(INHERIT_CLOEXEC_FD, F_SETFD, FD_CLOEXEC) != 0) + fail("inheritance CLOEXEC setup failed"); + + int pipeA[2]; + int pipeB[2]; + if (pipe(pipeA) != 0 || pipe(pipeB) != 0) + fail("inheritance pipe setup failed"); + move_fd(pipeA[0], INHERIT_PIPE_A_READ); + move_fd(pipeA[1], INHERIT_PIPE_A_WRITE); + move_fd(pipeB[0], INHERIT_PIPE_B_READ); + move_fd(pipeB[1], INHERIT_PIPE_B_WRITE); + if (fcntl(INHERIT_PIPE_A_READ, F_SETFL, O_NONBLOCK) != 0) + fail("inheritance nonblock setup failed"); + + char self[512]; + const char* command = posix_get_command_line(); + size_t length = 0; + while (command && command[length] && command[length] != ' ' && command[length] != '\t' && + length + 1 < sizeof(self)) { + self[length] = command[length]; + ++length; + } + self[length] = '\0'; + if (length == 0 || (command[length] && command[length] != ' ' && command[length] != '\t')) + fail("could not resolve self path"); + + char* childArgv[] = { self, "--inherit-child", NULL }; + pid_t child = 0; + if (posix_spawn(&child, self, NULL, NULL, childArgv, NULL) != 0) + fail("inheritance child spawn failed"); + + if (close(INHERIT_PIPE_A_WRITE) != 0 || close(INHERIT_PIPE_B_READ) != 0) + fail("parent endpoint close failed"); + if (wait_control_byte() != 'R') + fail("unexpected child ready signal"); + + if ((fcntl(INHERIT_FILE_FD, F_GETFL) & O_APPEND) == 0) + fail("child F_SETFL was not visible in parent"); + if (fcntl(INHERIT_CLOEXEC_FD, F_GETFD) != FD_CLOEXEC) + fail("parent descriptor-local CLOEXEC changed"); + char suffix[3] = {0, 0, 0}; + if (read(INHERIT_FILE_FD, suffix, 2) != 2 || strcmp(suffix, "ef") != 0) + fail("child did not advance shared native file offset"); + + if (write(INHERIT_PIPE_B_WRITE, "x", 1) != 1) + fail("write while inherited reader alive failed"); + if (wait_control_byte() != 'C') + fail("unexpected child close signal"); + errno = 0; + if (write(INHERIT_PIPE_B_WRITE, "y", 1) != -1 || errno != EPIPE) + fail("final cross-process reader close did not produce EPIPE"); + + int childStatus = 0; + if (waitpid(child, &childStatus, 0) != child || childStatus != 0) + fail("inheritance child failed"); + char eof = 0; + if (read(INHERIT_PIPE_A_READ, &eof, 1) != 0) + fail("final cross-process writer close did not produce EOF"); + + if (close(INHERIT_FILE_FD) != 0 || close(INHERIT_CLOEXEC_FD) != 0 || + close(INHERIT_PIPE_A_READ) != 0 || close(INHERIT_PIPE_B_WRITE) != 0) + fail("inheritance cleanup close failed"); + if (unlink(path) != 0) + fail("inheritance cleanup unlink failed"); +} + +// Runs descriptor duplication and shared-open-description tests. +int main(void) { + if (is_inheritance_child()) + return run_inheritance_child(); + + const char* path = "posix_fd_smoke.tmp"; + (void)unlink(path); + + int fd = open(path, O_CREAT | O_TRUNC | O_RDWR); + if (fd < 0) + fail("open failed"); + if (write(fd, "abcdef", 6) != 6) + fail("write failed"); + if (lseek(fd, 0, SEEK_SET) != 0) + fail("initial seek failed"); + + int duplicate = dup(fd); + if (duplicate < 0 || duplicate == fd) + fail("dup failed"); + + char bytes[3] = {0, 0, 0}; + if (read(duplicate, bytes, 2) != 2 || strcmp(bytes, "ab") != 0) + fail("duplicate read failed"); + memset(bytes, 0, sizeof(bytes)); + if (read(fd, bytes, 2) != 2 || strcmp(bytes, "cd") != 0) + fail("shared file offset failed"); + + if (fcntl(duplicate, F_GETFD) != 0) + fail("new duplicate inherited descriptor flags"); + if (fcntl(duplicate, F_SETFD, FD_CLOEXEC) != 0) + fail("F_SETFD failed"); + if (fcntl(duplicate, F_GETFD) != FD_CLOEXEC) + fail("F_GETFD failed"); + if (fcntl(fd, F_GETFD) != 0) + fail("descriptor flags were shared"); + + int duplicate20 = dup2(fd, 20); + if (duplicate20 != 20) + fail("dup2 failed"); + if ((fcntl(duplicate20, F_GETFL) & O_ACCMODE) != O_RDWR) + fail("F_GETFL lost access mode"); + + int duplicate30 = fcntl(fd, F_DUPFD, 30); + if (duplicate30 < 30) + fail("F_DUPFD failed"); + + if (close(fd) != 0) + fail("close original failed"); + memset(bytes, 0, sizeof(bytes)); + if (read(duplicate, bytes, 2) != 2 || strcmp(bytes, "ef") != 0) + fail("duplicate did not retain open description"); + + if (fcntl(duplicate, F_SETFL, O_APPEND | O_NONBLOCK) != 0) + fail("F_SETFL failed"); + int sharedFlags = fcntl(duplicate20, F_GETFL); + if ((sharedFlags & (O_APPEND | O_NONBLOCK)) != (O_APPEND | O_NONBLOCK)) + fail("F_SETFL status was not shared"); + if (lseek(duplicate, 0, SEEK_SET) != 0) + fail("append setup seek failed"); + if (write(duplicate20, "Z", 1) != 1) + fail("append write failed"); + if (lseek(duplicate, 6, SEEK_SET) != 6) + fail("append verify seek failed"); + char appended = 0; + if (read(duplicate, &appended, 1) != 1 || appended != 'Z') + fail("O_APPEND did not append"); + if (fcntl(duplicate, F_SETFL, 0) != 0 || + (fcntl(duplicate30, F_GETFL) & (O_APPEND | O_NONBLOCK)) != 0) + fail("F_SETFL clear was not shared"); + + struct flock lock; + memset(&lock, 0, sizeof(lock)); + lock.l_type = F_WRLCK; + lock.l_whence = SEEK_SET; + lock.l_start = 0; + lock.l_len = 4; + if (fcntl(duplicate, F_SETLK, &lock) != 0) + fail("F_SETLK failed"); + struct flock query = lock; + if (fcntl(duplicate20, F_GETLK, &query) != 0 || query.l_type != F_UNLCK) + fail("F_GETLK reported this process's own lock"); + lock.l_type = F_UNLCK; + if (fcntl(duplicate30, F_SETLK, &lock) != 0) + fail("fcntl unlock failed"); + + if (close(duplicate) != 0 || close(duplicate20) != 0 || close(duplicate30) != 0) + fail("duplicate close failed"); + if (unlink(path) != 0) + fail("unlink failed"); + + run_cross_process_inheritance(); + + print("posix_fd_smoke: PASS\n"); + return 0; +} diff --git a/code/user/tests/posix_module_smoke/posix_module_smoke.c b/code/user/tests/posix_module_smoke/posix_module_smoke.c index 00904bc..8c15708 100644 --- a/code/user/tests/posix_module_smoke/posix_module_smoke.c +++ b/code/user/tests/posix_module_smoke/posix_module_smoke.c @@ -4,18 +4,9 @@ #include #include #include - -#define RTLD_NOW 0x0002 -#define RTLD_LOCAL 0x0004 - -typedef int64_t ssize_t; - -extern ssize_t write(int fd, const void* buf, size_t count); -extern void exit(int status); -extern void* dlopen(const char* filename, int flags); -extern void* dlsym(void* handle, const char* symbol); -extern int dlclose(void* handle); -extern char* dlerror(void); +#include +#include +#include // Writes one NUL-terminated string to stdout. static void print(const char* s) { diff --git a/code/user/tests/posix_pipe_smoke/posix_pipe_smoke.c b/code/user/tests/posix_pipe_smoke/posix_pipe_smoke.c new file mode 100644 index 0000000..95fc101 --- /dev/null +++ b/code/user/tests/posix_pipe_smoke/posix_pipe_smoke.c @@ -0,0 +1,121 @@ +// User-mode smoke test for POSIX pipe, nonblocking, and poll semantics. + +#include +#include +#include +#include +#include +#include +#include + +// Writes one NUL-terminated diagnostic to stdout. +static void print(const char* text) { + (void)write(1, text, strlen(text)); +} + +// Terminates the smoke test with one diagnostic. +static void fail(const char* message) { + print("posix_pipe_smoke: "); + print(message); + print("\n"); + exit(1); +} + +// Requires one descriptor to expose the expected immediate poll bits. +static short poll_now(int fd, short events) { + struct pollfd descriptor; + descriptor.fd = fd; + descriptor.events = events; + descriptor.revents = 0; + int result = poll(&descriptor, 1, 0); + if (result < 0) + fail("poll failed"); + return descriptor.revents; +} + +// Runs pipe lifetime, readiness, EOF, broken-pipe, and atomic-write checks. +int main(void) { + int descriptors[2]; + if (pipe(descriptors) != 0) + fail("pipe failed"); + + struct stat st; + if (fstat(descriptors[0], &st) != 0 || !S_ISFIFO(st.st_mode)) + fail("read endpoint is not FIFO"); + if (fstat(descriptors[1], &st) != 0 || !S_ISFIFO(st.st_mode)) + fail("write endpoint is not FIFO"); + + if (poll_now(descriptors[0], POLLIN) & POLLIN) + fail("empty pipe reported readable"); + if (!(poll_now(descriptors[1], POLLOUT) & POLLOUT)) + fail("empty pipe write endpoint not writable"); + + if (write(descriptors[1], "abc", 3) != 3) + fail("basic write failed"); + if (!(poll_now(descriptors[0], POLLIN) & POLLIN)) + fail("pipe data did not become readable"); + + char buffer[4] = {0, 0, 0, 0}; + if (read(descriptors[0], buffer, 3) != 3 || strcmp(buffer, "abc") != 0) + fail("basic read failed"); + + if (fcntl(descriptors[0], F_SETFL, O_NONBLOCK) != 0) + fail("read F_SETFL O_NONBLOCK failed"); + errno = 0; + if (read(descriptors[0], buffer, 1) != -1 || errno != EAGAIN) + fail("empty nonblocking read did not return EAGAIN"); + + int writerAlias = dup(descriptors[1]); + if (writerAlias < 0) + fail("writer dup failed"); + if (close(descriptors[1]) != 0) + fail("close original writer failed"); + if (poll_now(descriptors[0], POLLIN | POLLHUP) & POLLHUP) + fail("HUP published before final writer closed"); + if (close(writerAlias) != 0) + fail("close final writer failed"); + + short eofEvents = poll_now(descriptors[0], POLLIN | POLLHUP); + if ((eofEvents & (POLLIN | POLLHUP)) != (POLLIN | POLLHUP)) + fail("EOF readiness transition missing"); + if (read(descriptors[0], buffer, 1) != 0) + fail("final writer close did not produce EOF"); + if (close(descriptors[0]) != 0) + fail("close read endpoint failed"); + + if (pipe(descriptors) != 0) + fail("second pipe failed"); + if (close(descriptors[0]) != 0) + fail("close second read endpoint failed"); + errno = 0; + if (write(descriptors[1], "x", 1) != -1 || errno != EPIPE) + fail("write without readers did not return EPIPE"); + if (close(descriptors[1]) != 0) + fail("close broken writer failed"); + + if (pipe(descriptors) != 0) + fail("atomic pipe failed"); + if (fcntl(descriptors[1], F_SETFL, O_NONBLOCK) != 0) + fail("write F_SETFL O_NONBLOCK failed"); + char atomic[PIPE_BUF]; + memset(atomic, 'A', sizeof(atomic)); + for (;;) { + errno = 0; + ssize_t written = write(descriptors[1], atomic, sizeof(atomic)); + if (written == (ssize_t)sizeof(atomic)) + continue; + if (written == -1 && errno == EAGAIN) + break; + fail("PIPE_BUF write was partially committed"); + } + if (poll_now(descriptors[1], POLLOUT) & POLLOUT) + fail("full pipe still reported writable"); + if (!(poll_now(descriptors[0], POLLIN) & POLLIN)) + fail("full pipe read endpoint not readable"); + + if (close(descriptors[0]) != 0 || close(descriptors[1]) != 0) + fail("atomic pipe close failed"); + + print("posix_pipe_smoke: PASS\n"); + return 0; +} diff --git a/code/user/tests/posix_runtime_smoke/posix_runtime_smoke.c b/code/user/tests/posix_runtime_smoke/posix_runtime_smoke.c new file mode 100644 index 0000000..7512e35 --- /dev/null +++ b/code/user/tests/posix_runtime_smoke/posix_runtime_smoke.c @@ -0,0 +1,165 @@ +// User-mode smoke test for POSIX process/thread runtime state and public headers. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static volatile int gWorkerErrno; +static volatile uintptr_t gWorkerErrnoAddress; +static volatile int gWorkerDlErrorOk; +static volatile pid_t gWorkerPid; + +// Writes one NUL-terminated string to stdout. +static void print(const char* text) { + (void)write(1, text, strlen(text)); +} + +// Terminates the smoke test with one diagnostic. +static void fail(const char* message) { + print("posix_runtime_smoke: "); + print(message); + print("\n"); + exit(1); +} + +// Mutates errno and dlerror in a second POSIX thread. +static void* worker(void* argument) { + (void)argument; + + if (errno != 0) + return (void*)1; + + gWorkerErrnoAddress = (uintptr_t)__ntl_posix_errno_location(); + gWorkerPid = getpid(); + errno = 222; + + if (dlsym((void*)0, "worker_missing_symbol") != (void*)0) + return (void*)2; + char* error = dlerror(); + if (!error || !error[0] || dlerror() != (char*)0) + return (void*)3; + + gWorkerErrno = errno; + gWorkerDlErrorOk = 1; + return (void*)0x1234; +} + +// Runs the POSIX runtime foundation smoke test. +int main(void) { + print("posix_runtime_smoke: start\n"); + + if (_POSIX_VERSION != 199009L) + fail("wrong POSIX profile version"); + + uintptr_t mainErrnoAddress = (uintptr_t)__ntl_posix_errno_location(); + if (!mainErrnoAddress) + fail("main errno address is null"); + + pid_t stablePid = getpid(); + if (stablePid <= 0) + fail("stable POSIX pid was invalid"); + if ((uint32_t)stablePid == SysGetCurrentPid()) + fail("getpid leaked native pid"); + + errno = 77; + if (dlsym((void*)0, "main_missing_symbol") != (void*)0) + fail("main dlsym unexpectedly succeeded"); + if (!dlerror()) + fail("main dlerror was not set"); + + // Re-arm one main-thread loader error so the worker can prove isolation. + if (dlsym((void*)0, "main_missing_symbol_2") != (void*)0) + fail("second main dlsym unexpectedly succeeded"); + errno = 77; + + pthread_t thread; + int createStatus = pthread_create(&thread, (const pthread_attr_t*)0, worker, (void*)0); + if (createStatus != 0) + fail("pthread_create failed"); + + void* result = (void*)0; + int joinStatus = pthread_join(thread, &result); + if (joinStatus != 0) + fail("pthread_join failed"); + if (result != (void*)0x1234) + fail("worker returned wrong value"); + + if (gWorkerErrnoAddress == 0 || gWorkerErrnoAddress == mainErrnoAddress) + fail("errno storage was shared between threads"); + if (gWorkerPid != stablePid) + fail("worker did not inherit logical POSIX process identity"); + if (gWorkerErrno != EINVAL || !gWorkerDlErrorOk) + fail("worker thread-local state was wrong"); + if (errno != 77) + fail("worker changed main errno"); + + char* mainError = dlerror(); + if (!mainError || !mainError[0]) + fail("worker consumed main dlerror"); + if (dlerror() != (char*)0) + fail("main dlerror did not clear"); + + if (getuid() != 0 || geteuid() != 0 || getgid() != 0 || getegid() != 0) + fail("initial synthetic credentials were wrong"); + if (getgroups(0, (gid_t*)0) != 0) + fail("initial supplementary group set was wrong"); + char* login = getlogin(); + if (!login || strcmp(login, "root") != 0) + fail("initial login identity was wrong"); + + struct passwd* root = getpwnam("root"); + if (!root || root->pw_uid != 0 || root->pw_gid != 0 || strcmp(root->pw_name, "root") != 0) + fail("root passwd lookup failed"); + struct passwd* user = getpwuid(1000); + if (!user || user->pw_uid != 1000 || user->pw_gid != 1000 || strcmp(user->pw_name, "user") != 0) + fail("user passwd lookup failed"); + struct group* users = getgrnam("users"); + if (!users || users->gr_gid != 1000 || strcmp(users->gr_name, "users") != 0 || + !users->gr_mem || users->gr_mem[0] != (char*)0) + fail("group lookup failed"); + if (getgrgid(0) == (struct group*)0) + fail("group id lookup failed"); + + if (getpgrp() != stablePid) + fail("initial process group was wrong"); + errno = 0; + if (setsid() != (pid_t)-1 || errno != EPERM) + fail("session leader unexpectedly created another session"); + errno = 0; + if (setpgid(0, 0) != -1 || errno != EPERM) + fail("session leader unexpectedly changed process group"); + + char originalCwd[256]; + if (!getcwd(originalCwd, sizeof(originalCwd))) + fail("getcwd before chdir failed"); + if (chdir("/") != 0) + fail("chdir root failed"); + char rootCwd[16]; + if (!getcwd(rootCwd, sizeof(rootCwd)) || strcmp(rootCwd, "/") != 0) + fail("chdir root did not update cwd"); + if (chdir(originalCwd) != 0) + fail("cwd restore failed"); + + if (setgid(1000) != 0 || getgid() != 1000 || getegid() != 1000) + fail("root setgid failed"); + if (setuid(1000) != 0 || getuid() != 1000 || geteuid() != 1000) + fail("root setuid failed"); + errno = 0; + if (setgid(0) != -1 || errno != EPERM) + fail("dropped uid retained setgid authority"); + errno = 0; + if (setuid(0) != -1 || errno != EPERM) + fail("dropped uid regained root"); + if (!getlogin() || strcmp(getlogin(), "root") != 0) + fail("login identity changed with effective uid"); + + print("posix_runtime_smoke: PASS\n"); + return 0; +} diff --git a/code/user/tests/posix_service_smoke/posix_service_smoke.c b/code/user/tests/posix_service_smoke/posix_service_smoke.c new file mode 100644 index 0000000..5cd7272 --- /dev/null +++ b/code/user/tests/posix_service_smoke/posix_service_smoke.c @@ -0,0 +1,514 @@ +// Native smoke test for posixsrv discovery, stable PID registration, and bounded transactions. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CHILD_ARGUMENT "--pid-child" + +// Writes one test diagnostic through the native console. +static void print(const char* text) { + SYS_WRITE_CONSOLE_REQUEST request = { sizeof(request), SysSvFromCstr(text) }; + (void)SysWriteConsole(&request); +} + +// Terminates the smoke test with one visible failure reason. +static void fail(const char* text) { + print("posix_service_smoke: "); + print(text); + print("\n"); + for (;;) SysExitProcess(1); +} + +// Returns whether the current command line contains one ASCII token. +static int command_contains(const char* token) { + SYS_PEB* peb = SysGetPeb(); + if (!peb || peb->Magic != SYS_PEB_MAGIC || peb->Version != SYS_ENV_VERSION || !token) + return 0; + size_t tokenLength = strlen(token); + if (tokenLength == 0 || tokenLength > peb->CommandLine.Length) return 0; + for (size_t i = 0; i + tokenLength <= peb->CommandLine.Length; ++i) { + if (memcmp(peb->CommandLine.Data + i, token, tokenLength) == 0) + return 1; + } + return 0; +} + +// Resolves /Services/posix through RSSRV and connects one independent client endpoint. +static KSTATUS connect_posixsrv(SYS_HANDLE* endpointOut) { + if (!endpointOut) return NTLSTATUS_INVALID_PARAMETER; + *endpointOut = SYS_HANDLE_INVALID; + + SYS_HANDLE resource = SYS_HANDLE_INVALID; + KSTATUS status = NtlResourceConnect(&resource); + if (!KSUCCESS(status)) return status; + + SYS_HANDLE listener = SYS_HANDLE_INVALID; + status = NtlResourceLookup(resource, SYS_SV_LITERAL(NTL_POSIX_SERVICE_PATH), + SYS_RIGHT_QUERY | SYS_PORT_RIGHT_CONNECT, &listener); + (void)NtlResourceClose(resource); + if (!KSUCCESS(status)) return status; + + SYS_CONNECT_PORT_REQUEST connect; + memset(&connect, 0, sizeof(connect)); + connect.Size = sizeof(connect); + connect.ListenerHandle = listener; + connect.Flags = SYS_PORT_CONNECT_FLAG_NONE; + connect.EndpointHandleOut = endpointOut; + status = SysConnectPort(&connect); + (void)SysCloseHandle(listener); + return status; +} + +// Performs one fixed-size request with optional capability transfer and validates reply framing. +static KSTATUS call_posixsrv_exchange(SYS_HANDLE endpoint, + const NTL_POSIX_REQUEST* request, + const SYS_PORT_HANDLE_TRANSFER* requestHandles, + uint32_t requestHandleCount, + SYS_HANDLE* replyHandlesOut, + uint32_t replyHandleCapacity, + uint32_t expectedReplyHandleCount, + NTL_POSIX_REPLY* replyOut) { + if (endpoint == SYS_HANDLE_INVALID || !request || !replyOut || + expectedReplyHandleCount > replyHandleCapacity || + (replyHandleCapacity != 0 && !replyHandlesOut)) + return NTLSTATUS_INVALID_PARAMETER; + memset(replyOut, 0, sizeof(*replyOut)); + if (replyHandlesOut) { + for (uint32_t i = 0; i < replyHandleCapacity; ++i) + replyHandlesOut[i] = SYS_HANDLE_INVALID; + } + + SYS_PORT_MESSAGE_INFO info; + memset(&info, 0, sizeof(info)); + info.Size = sizeof(info); + + SYS_CALL_PORT_REQUEST call; + memset(&call, 0, sizeof(call)); + call.Size = sizeof(call); + call.EndpointHandle = endpoint; + call.Flags = SYS_PORT_FLAG_NONE; + call.RequestPayload = request; + call.RequestLength = sizeof(*request); + call.RequestHandles = requestHandles; + call.RequestHandleCount = requestHandleCount; + call.ReplyPayload = replyOut; + call.ReplyCapacity = sizeof(*replyOut); + call.ReplyHandlesOut = replyHandlesOut; + call.ReplyHandleCapacity = replyHandleCapacity; + call.ReplyInfoOut = &info; + call.TimeoutMilliseconds = SYS_PORT_TIMEOUT_INFINITE; + KSTATUS status = SysCallPort(&call); + if (!KSUCCESS(status)) return status; + uint32_t expectedHandles = KSUCCESS(replyOut->Status) ? expectedReplyHandleCount : 0u; + if (info.Type != SYS_PORT_MESSAGE_REPLY || info.HandleCount != expectedHandles || + info.PayloadLength != sizeof(*replyOut) || replyOut->Size != sizeof(*replyOut) || + replyOut->Version != NTL_POSIX_PROTOCOL_VERSION || + replyOut->Operation != request->Operation || + replyOut->Flags != NTL_POSIX_REQUEST_FLAG_NONE) { + if (replyHandlesOut) { + uint32_t closeCount = info.HandleCount < replyHandleCapacity ? + info.HandleCount : replyHandleCapacity; + for (uint32_t i = 0; i < closeCount; ++i) { + if (replyHandlesOut[i] != SYS_HANDLE_INVALID) + (void)SysCloseHandle(replyHandlesOut[i]); + replyHandlesOut[i] = SYS_HANDLE_INVALID; + } + } + return NTLSTATUS_FAILURE; + } + return NTLSTATUS_SUCCESS; +} + +// Performs one request that expects no capabilities in the reply. +static KSTATUS call_posixsrv(SYS_HANDLE endpoint, + const NTL_POSIX_REQUEST* request, + const SYS_PORT_HANDLE_TRANSFER* handles, + uint32_t handleCount, + NTL_POSIX_REPLY* replyOut) { + return call_posixsrv_exchange(endpoint, request, handles, handleCount, + (SYS_HANDLE*)0, 0, 0, replyOut); +} + +// Constructs the common fixed-size request header used by the service protocol. +static NTL_POSIX_REQUEST make_request(uint32_t operation) { + NTL_POSIX_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.Version = NTL_POSIX_PROTOCOL_VERSION; + request.Operation = operation; + request.Flags = NTL_POSIX_REQUEST_FLAG_NONE; + return request; +} + +// Registers the current native process by transferring a restricted self process capability. +static KSTATUS register_current_process(SYS_HANDLE endpoint, NTL_POSIX_REPLY* replyOut) { + SYS_HANDLE self = SYS_HANDLE_INVALID; + KSTATUS status = SysOpenProcess(SysGetCurrentPid(), + SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_RIGHT_TRANSFER, + &self); + if (!KSUCCESS(status)) return status; + + SYS_PORT_HANDLE_TRANSFER transfer; + memset(&transfer, 0, sizeof(transfer)); + transfer.Handle = self; + transfer.DesiredRights = SYS_RIGHT_QUERY | SYS_RIGHT_WAIT; + transfer.Flags = SYS_PORT_TRANSFER_FLAG_NONE; + + NTL_POSIX_REQUEST request = make_request(NTL_POSIX_OP_REGISTER_PROCESS); + size_t cwdRequired = 0; + status = NtlGetCurrentDirectory(request.Path, sizeof(request.Path), &cwdRequired); + if (!KSUCCESS(status)) { + (void)SysCloseHandle(self); + return status; + } + request.PathLength = (uint32_t)strlen(request.Path); + status = call_posixsrv(endpoint, &request, &transfer, 1u, replyOut); + (void)SysCloseHandle(self); + return status; +} + +// Creates one handle-less server-owned description and returns its stable identity. +static uint64_t create_synthetic_description(SYS_HANDLE endpoint, + uint32_t type, + uint32_t statusFlags, + NTL_POSIX_REPLY* replyOut) { + NTL_POSIX_REQUEST request = make_request(NTL_POSIX_OP_CREATE_OPEN_DESCRIPTION); + request.OpenDescriptionType = type; + request.OpenStatusFlags = statusFlags; + if (!KSUCCESS(call_posixsrv(endpoint, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, + replyOut)) || !KSUCCESS(replyOut->Status) || + replyOut->OpenDescriptionId == 0 || replyOut->SharedOpenDescription == 0) + fail("shared description creation failed"); + return replyOut->OpenDescriptionId; +} + +// Releases one server-owned description held by the current registered process. +static KSTATUS release_description(SYS_HANDLE endpoint, uint64_t descriptionId, + NTL_POSIX_REPLY* replyOut) { + NTL_POSIX_REQUEST request = make_request(NTL_POSIX_OP_RELEASE_OPEN_DESCRIPTION); + request.OpenDescriptionId = descriptionId; + KSTATUS status = call_posixsrv(endpoint, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, + replyOut); + if (!KSUCCESS(status)) return status; + return replyOut->Status; +} + +// Waits for the process monitor to retire one process and all of its shared holders. +static void wait_registry_counts(SYS_HANDLE endpoint, + uint32_t expectedProcesses, + uint32_t expectedDescriptions) { + for (uint32_t attempt = 0; attempt < 4096u; ++attempt) { + NTL_POSIX_REQUEST request = make_request(NTL_POSIX_OP_QUERY); + NTL_POSIX_REPLY reply; + if (!KSUCCESS(call_posixsrv(endpoint, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &reply)) || + !KSUCCESS(reply.Status)) + fail("registry query failed"); + if (reply.RegisteredProcesses == expectedProcesses && + reply.RegisteredOpenDescriptions == expectedDescriptions) + return; + (void)SysYield(); + } + fail("exited process registry state was not retired"); +} + +// Runs a second native process so the server must allocate a distinct stable POSIX PID. +static uint32_t spawn_child_posix_pid(uint32_t* nativePidOut) { + if (nativePidOut) *nativePidOut = 0; + SYS_PEB* peb = SysGetPeb(); + if (!peb || peb->Magic != SYS_PEB_MAGIC || peb->Version != SYS_ENV_VERSION || + !peb->ImagePath.Data || peb->ImagePath.Length == 0 || peb->ImagePath.Length > SYS_PATH_MAX) + fail("invalid self image path"); + + char path[SYS_PATH_MAX + 1u]; + memcpy(path, peb->ImagePath.Data, peb->ImagePath.Length); + path[peb->ImagePath.Length] = '\0'; + + char command[SYS_PATH_MAX + 32u]; + size_t pathLength = strlen(path); + size_t argumentLength = strlen(CHILD_ARGUMENT); + if (pathLength + 1u + argumentLength + 1u > sizeof(command)) + fail("child command line too long"); + memcpy(command, path, pathLength); + command[pathLength] = ' '; + memcpy(command + pathLength + 1u, CHILD_ARGUMENT, argumentLength + 1u); + + SYS_HANDLE process = SYS_HANDLE_INVALID; + NTL_CREATE_SYSTEM_PROCESS_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Flags = NTL_PROCESS_LOAD_FLAG_NONE; + create.ImageFile = SYS_HANDLE_INVALID; + create.Path = SysSvFromCstr(path); + create.CommandLine = SysSvFromCstr(command); + create.SecurityContext = SYS_SECURITY_CONTEXT_CURRENT; + create.ResolveModuleName = NtlResolveSystemModuleName; + NTL_POSIX_BOOTSTRAP_DATA bootstrap; + memset(&bootstrap, 0, sizeof(bootstrap)); + bootstrap.Magic = NTL_POSIX_BOOTSTRAP_MAGIC; + bootstrap.Version = NTL_POSIX_BOOTSTRAP_VERSION; + bootstrap.Size = sizeof(bootstrap); + create.StartupData = &bootstrap; + create.StartupDataSize = sizeof(bootstrap); + create.ProcessHandleOut = &process; + KSTATUS status = NtlCreateSystemProcess(&create); + if (!KSUCCESS(status)) fail("child process creation failed"); + + uint32_t nativePid = 0; + if (!KSUCCESS(SysGetProcessPid(process, &nativePid))) + fail("child native pid query failed"); + if (nativePidOut) *nativePidOut = nativePid; + + if (!KSUCCESS(SysWait(process, SYS_WAIT_FLAG_NONE))) + fail("child process wait failed"); + int32_t exitCode = 0; + if (!KSUCCESS(SysGetProcessExitCode(process, &exitCode))) + fail("child exit query failed"); + (void)SysCloseHandle(process); + if (exitCode <= 0) fail("child returned invalid POSIX pid"); + return (uint32_t)exitCode; +} + +// Registers and returns the stable PID when running as the self-spawned child. +static void run_child_mode(void) { + NTL_POSIX_BOOTSTRAP_DATA bootstrap; + size_t bootstrapSize = 0; + if (!KSUCCESS(NtlGetProcessStartupData(&bootstrap, sizeof(bootstrap), &bootstrapSize)) || + bootstrapSize != sizeof(bootstrap) || bootstrap.Magic != NTL_POSIX_BOOTSTRAP_MAGIC || + bootstrap.Version != NTL_POSIX_BOOTSTRAP_VERSION || bootstrap.Size != sizeof(bootstrap) || + bootstrap.Reserved != 0 || bootstrap.Token != 0 || bootstrap.PosixPid != 0 || + bootstrap.Generation != 0 || bootstrap.FdInheritanceToken != 0) + for (;;) SysExitProcess(4); + + SYS_HANDLE endpoint = SYS_HANDLE_INVALID; + if (!KSUCCESS(connect_posixsrv(&endpoint))) + for (;;) SysExitProcess(2); + NTL_POSIX_REPLY reply; + if (!KSUCCESS(register_current_process(endpoint, &reply)) || !KSUCCESS(reply.Status) || + reply.PosixPid == 0 || reply.PosixPid > 0x7FFFFFFFu || + reply.ProcessState != NTL_POSIX_PROCESS_STATE_RUNNING) + for (;;) SysExitProcess(3); + uint32_t pid = reply.PosixPid; + NTL_POSIX_REPLY descriptionReply; + uint64_t leakedDescription = create_synthetic_description( + endpoint, NTL_POSIX_OPEN_TYPE_DEV_NULL, NTL_POSIX_OPEN_STATUS_WRONLY, + &descriptionReply); + NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)(uintptr_t)descriptionReply.SharedOpenDescription; + if (leakedDescription == 0 || !shared || shared->DescriptionId != leakedDescription || + shared->Magic != NTL_POSIX_SHARED_OPEN_MAGIC || + shared->Version != NTL_POSIX_SHARED_OPEN_VERSION) + for (;;) SysExitProcess(5); + // Intentionally leak the holder; the server's native-process monitor must reclaim it. + (void)SysDisconnectPort(endpoint); + (void)SysCloseHandle(endpoint); + for (;;) SysExitProcess((int32_t)pid); +} + +// Exercises service discovery, stable identity, protocol versions, and transaction boundaries. +int main(void) { + if (command_contains(CHILD_ARGUMENT)) run_child_mode(); + + print("posix_service_smoke: start\n"); + + SYS_HANDLE first = SYS_HANDLE_INVALID; + if (!KSUCCESS(connect_posixsrv(&first))) fail("first connect failed"); + + SYS_SECURITY_CONTEXT_INFO security; + memset(&security, 0, sizeof(security)); + security.Size = sizeof(security); + if (!KSUCCESS(SysQuerySecurityContext(SYS_SECURITY_CONTEXT_CURRENT, &security))) + fail("security query failed"); + + NTL_POSIX_REQUEST request = make_request(NTL_POSIX_OP_QUERY); + NTL_POSIX_REPLY reply; + if (!KSUCCESS(call_posixsrv(first, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &reply)) || + !KSUCCESS(reply.Status)) + fail("query failed"); + if (reply.ClientProcessId != SysGetCurrentPid() || reply.ClientSecurityId != security.SecurityId || + reply.ActiveTransactions != 0 || reply.TransactionCapacity != NTL_POSIX_TRANSACTION_CAPACITY || + reply.PosixPid != 0) + fail("pre-registration identity mismatch"); + + if (!KSUCCESS(register_current_process(first, &reply)) || !KSUCCESS(reply.Status)) + fail("process registration failed"); + uint32_t stablePid = reply.PosixPid; + if (stablePid == 0 || stablePid == SysGetCurrentPid() || reply.NativeGeneration != 1u || + reply.ProcessState != NTL_POSIX_PROCESS_STATE_RUNNING) + fail("stable process identity mismatch"); + if (reply.UserId != 0 || reply.EffectiveUserId != 0 || reply.GroupId != 0 || + reply.EffectiveGroupId != 0 || reply.ProcessGroupId != stablePid || + reply.SessionId != stablePid || reply.SupplementaryGroupCount != 0 || + reply.NameLength != 4u || strcmp(reply.Name, "root") != 0) + fail("initial shared process attributes mismatch"); + char registeredCwd[SYS_PATH_MAX + 1u]; + size_t registeredCwdRequired = 0; + if (!KSUCCESS(NtlGetCurrentDirectory(registeredCwd, sizeof(registeredCwd), ®isteredCwdRequired)) || + reply.CurrentDirectoryLength != strlen(registeredCwd) || + strcmp(reply.CurrentDirectory, registeredCwd) != 0) + fail("registered cwd mismatch"); + + uint32_t baselineDescriptions = reply.RegisteredOpenDescriptions; + NTL_POSIX_REPLY descriptionReply; + uint64_t descriptionId = create_synthetic_description( + first, NTL_POSIX_OPEN_TYPE_DEV_NULL, NTL_POSIX_OPEN_STATUS_WRONLY, + &descriptionReply); + if (descriptionReply.RegisteredOpenDescriptions != baselineDescriptions + 1u || + descriptionReply.OpenDescriptionType != NTL_POSIX_OPEN_TYPE_DEV_NULL || + descriptionReply.OpenStatusFlags != NTL_POSIX_OPEN_STATUS_WRONLY) + fail("shared description registry mismatch"); + NTL_POSIX_SHARED_OPEN_DESCRIPTION* shared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)(uintptr_t)descriptionReply.SharedOpenDescription; + if (!shared || shared->Magic != NTL_POSIX_SHARED_OPEN_MAGIC || + shared->Version != NTL_POSIX_SHARED_OPEN_VERSION || + shared->Type != NTL_POSIX_OPEN_TYPE_DEV_NULL || + shared->DescriptionId != descriptionId || + (uint32_t)shared->StatusFlags != NTL_POSIX_OPEN_STATUS_WRONLY) + fail("shared description pointer mismatch"); + + request = make_request(NTL_POSIX_OP_SET_OPEN_STATUS); + request.OpenDescriptionId = descriptionId; + request.OpenStatusFlags = NTL_POSIX_OPEN_STATUS_WRONLY | NTL_POSIX_OPEN_STATUS_NONBLOCK; + if (!KSUCCESS(call_posixsrv(first, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &descriptionReply)) || + !KSUCCESS(descriptionReply.Status) || descriptionReply.OpenDescriptionId != descriptionId || + descriptionReply.OpenStatusFlags != request.OpenStatusFlags || + (uint32_t)shared->StatusFlags != request.OpenStatusFlags) + fail("server-serialized status update failed"); + + if (!KSUCCESS(release_description(first, descriptionId, &descriptionReply)) || + descriptionReply.RegisteredOpenDescriptions != baselineDescriptions) + fail("shared description release failed"); + if (release_description(first, descriptionId, &descriptionReply) != NTLSTATUS_NOT_FOUND) + fail("duplicate shared description release was accepted"); + + request = make_request(NTL_POSIX_OP_CREATE_PIPE); + SYS_HANDLE pipeHandles[2] = { SYS_HANDLE_INVALID, SYS_HANDLE_INVALID }; + if (!KSUCCESS(call_posixsrv_exchange(first, &request, + (const SYS_PORT_HANDLE_TRANSFER*)0, 0, + pipeHandles, 2u, 2u, &descriptionReply)) || + !KSUCCESS(descriptionReply.Status) || + descriptionReply.RegisteredOpenDescriptions != baselineDescriptions + 2u || + descriptionReply.OpenDescriptionType != NTL_POSIX_OPEN_TYPE_PIPE_READ || + descriptionReply.OpenDescriptionType2 != NTL_POSIX_OPEN_TYPE_PIPE_WRITE || + descriptionReply.OpenStatusFlags != NTL_POSIX_OPEN_STATUS_RDONLY || + descriptionReply.OpenStatusFlags2 != NTL_POSIX_OPEN_STATUS_WRONLY || + descriptionReply.OpenDescriptionId == 0 || descriptionReply.OpenDescriptionId2 == 0 || + descriptionReply.OpenDescriptionId == descriptionReply.OpenDescriptionId2 || + pipeHandles[0] == SYS_HANDLE_INVALID || pipeHandles[1] == SYS_HANDLE_INVALID) + fail("server-owned pipe creation failed"); + + NTL_POSIX_SHARED_OPEN_DESCRIPTION* pipeReadShared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)(uintptr_t)descriptionReply.SharedOpenDescription; + NTL_POSIX_SHARED_OPEN_DESCRIPTION* pipeWriteShared = + (NTL_POSIX_SHARED_OPEN_DESCRIPTION*)(uintptr_t)descriptionReply.SharedOpenDescription2; + SYS_PIPE_SHARED_STATE* pipeShared = pipeReadShared ? + (SYS_PIPE_SHARED_STATE*)(uintptr_t)pipeReadShared->SharedObjectData : + (SYS_PIPE_SHARED_STATE*)0; + if (!pipeReadShared || !pipeWriteShared || !pipeShared || + pipeReadShared->Magic != NTL_POSIX_SHARED_OPEN_MAGIC || + pipeWriteShared->Magic != NTL_POSIX_SHARED_OPEN_MAGIC || + pipeReadShared->Version != NTL_POSIX_SHARED_OPEN_VERSION || + pipeWriteShared->Version != NTL_POSIX_SHARED_OPEN_VERSION || + pipeReadShared->DescriptionId != descriptionReply.OpenDescriptionId || + pipeWriteShared->DescriptionId != descriptionReply.OpenDescriptionId2 || + pipeReadShared->SharedObjectData != pipeWriteShared->SharedObjectData || + pipeShared->Magic != SYS_PIPE_SHARED_MAGIC || pipeShared->Version != SYS_PIPE_SHARED_VERSION || + pipeShared->Capacity != SYS_PIPE_BUFFER_CAPACITY || + pipeShared->HeaderBytes != SYS_PIPE_SHARED_HEADER_BYTES || + pipeShared->ReaderOpen == 0 || pipeShared->WriterOpen == 0) + fail("server-owned pipe shared state mismatch"); + + if (!KSUCCESS(release_description(first, descriptionReply.OpenDescriptionId, &reply)) || + reply.RegisteredOpenDescriptions != baselineDescriptions + 1u || + pipeShared->ReaderOpen == 0) + fail("pipe read master release invalidated live alias"); + if (!KSUCCESS(SysCloseHandle(pipeHandles[0])) || pipeShared->ReaderOpen != 0) + fail("pipe read alias close did not publish reader shutdown"); + pipeHandles[0] = SYS_HANDLE_INVALID; + + if (!KSUCCESS(SysCloseHandle(pipeHandles[1]))) + fail("pipe write alias close failed"); + pipeHandles[1] = SYS_HANDLE_INVALID; + if (!KSUCCESS(release_description(first, descriptionReply.OpenDescriptionId2, &reply)) || + reply.RegisteredOpenDescriptions != baselineDescriptions) + fail("server-owned pipe description release failed"); + + request = make_request(NTL_POSIX_OP_LOOKUP_USER); + request.LookupKind = NTL_POSIX_LOOKUP_BY_NAME; + memcpy(request.Name, "user", 5u); + request.NameLength = 4u; + if (!KSUCCESS(call_posixsrv(first, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &reply)) || + !KSUCCESS(reply.Status) || reply.UserId != 1000u || reply.GroupId != 1000u || + strcmp(reply.Name, "user") != 0) + fail("identity database lookup failed"); + + uint32_t childNativePid = 0; + uint32_t childPosixPid = spawn_child_posix_pid(&childNativePid); + if (childPosixPid == stablePid || childPosixPid == childNativePid) + fail("child POSIX identity was not independent"); + wait_registry_counts(first, 1u, baselineDescriptions); + + request = make_request(NTL_POSIX_OP_BEGIN_TRANSACTION); + request.TransactionKind = NTL_POSIX_TRANSACTION_SPAWN; + if (!KSUCCESS(call_posixsrv(first, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &reply)) || + !KSUCCESS(reply.Status) || reply.TransactionId == 0 || reply.ActiveTransactions != 1) + fail("transaction begin failed"); + uint32_t transactionId = reply.TransactionId; + + SYS_HANDLE second = SYS_HANDLE_INVALID; + if (!KSUCCESS(connect_posixsrv(&second))) fail("second connect failed"); + if (!KSUCCESS(register_current_process(second, &reply)) || !KSUCCESS(reply.Status) || + reply.PosixPid != stablePid || reply.NativeGeneration != 1u) + fail("same-process reconnect changed POSIX identity"); + + request = make_request(NTL_POSIX_OP_QUERY); + if (!KSUCCESS(call_posixsrv(second, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &reply)) || + !KSUCCESS(reply.Status) || reply.ActiveTransactions != 0 || reply.PosixPid != stablePid) + fail("connection transaction isolation failed"); + + request = make_request(NTL_POSIX_OP_QUERY); + request.Version = NTL_POSIX_PROTOCOL_VERSION + 1u; + if (!KSUCCESS(call_posixsrv(second, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &reply)) || + reply.Status != NTLSTATUS_NOT_SUPPORTED) + fail("version rejection failed"); + + request = make_request(NTL_POSIX_OP_CANCEL_TRANSACTION); + request.TransactionId = transactionId; + if (!KSUCCESS(call_posixsrv(first, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &reply)) || + !KSUCCESS(reply.Status) || reply.TransactionId != transactionId || reply.ActiveTransactions != 0) + fail("transaction cancel failed"); + + request = make_request(NTL_POSIX_OP_CANCEL_TRANSACTION); + request.TransactionId = transactionId; + if (!KSUCCESS(call_posixsrv(first, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &reply)) || + reply.Status != NTLSTATUS_NOT_FOUND) + fail("unknown transaction rejection failed"); + + request = make_request(NTL_POSIX_OP_BEGIN_TRANSACTION); + request.TransactionKind = NTL_POSIX_TRANSACTION_EXEC; + if (!KSUCCESS(call_posixsrv(first, &request, (const SYS_PORT_HANDLE_TRANSFER*)0, 0, &reply)) || + !KSUCCESS(reply.Status)) + fail("exec transaction begin failed"); + + (void)SysDisconnectPort(first); + (void)SysCloseHandle(first); + (void)SysDisconnectPort(second); + (void)SysCloseHandle(second); + + SYS_HANDLE third = SYS_HANDLE_INVALID; + if (!KSUCCESS(connect_posixsrv(&third))) fail("reconnect failed"); + if (!KSUCCESS(register_current_process(third, &reply)) || !KSUCCESS(reply.Status) || + reply.PosixPid != stablePid || reply.NativeGeneration != 1u || reply.ActiveTransactions != 0) + fail("live process lost identity after disconnect"); + (void)SysDisconnectPort(third); + (void)SysCloseHandle(third); + + print("posix_service_smoke: PASS\n"); + return 0; +} diff --git a/code/user/tests/resource_service_smoke/resource_service_smoke.c b/code/user/tests/resource_service_smoke/resource_service_smoke.c index c3d8170..a13094f 100644 --- a/code/user/tests/resource_service_smoke/resource_service_smoke.c +++ b/code/user/tests/resource_service_smoke/resource_service_smoke.c @@ -12,29 +12,6 @@ static void print(const char* text) { } -// Builds one /Device/HarddiskVolumeN resource path. -static SYS_STRING_VIEW make_volume_path(uint32_t index, char* buffer, size_t capacity) { - static const char prefix[] = "/Device/HarddiskVolume"; - size_t prefixLength = sizeof(prefix) - 1u; - if (!index || !buffer || capacity <= prefixLength) - return SysSvFromParts((const char*)0, 0); - - char digits[10]; - size_t digitCount = 0; - do { - digits[digitCount++] = (char)('0' + (index % 10u)); - index /= 10u; - } while (index && digitCount < sizeof(digits)); - if (prefixLength + digitCount + 1u > capacity) - return SysSvFromParts((const char*)0, 0); - - memcpy(buffer, prefix, prefixLength); - for (size_t i = 0; i < digitCount; ++i) - buffer[prefixLength + i] = digits[digitCount - 1u - i]; - buffer[prefixLength + digitCount] = '\0'; - return SysSvFromParts(buffer, prefixLength + digitCount); -} - static int fail(const char* what, KSTATUS status) { (void)status; print("resource_service_smoke: FAIL "); @@ -49,25 +26,61 @@ int main(void) { if (!KSUCCESS(status)) return fail("connect", status); uint32_t kind = NTL_RESOURCE_KIND_INVALID; + status = NtlResourceQuery(session, SYS_SV_LITERAL("/"), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) + return fail("query namespace root", status); + status = NtlResourceQuery(session, SYS_SV_LITERAL("/Device"), &kind); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) + return fail("query /Device", status); status = NtlResourceQuery(session, SYS_SV_LITERAL("/SystemRoot"), &kind); - if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_VOLUME) - return fail("query /SystemRoot", status); + if (!KSUCCESS(status) || kind != NTL_RESOURCE_KIND_SYMBOLIC_LINK) + return fail("query /SystemRoot link", status); + + // Root enumeration exposes namespace directories and symbolic links without transferring authority. + NTL_RESOURCE_ENUM_CURSOR rootCursor; + memset(&rootCursor, 0, sizeof(rootCursor)); + uint32_t sawDevice = 0; + uint32_t sawSessions = 0; + uint32_t sawServices = 0; + uint32_t sawSystemRoot = 0; + for (;;) { + NTL_RESOURCE_ENUM_ENTRY entry; + status = NtlResourceEnumerate(session, SYS_SV_LITERAL("/"), &rootCursor, &entry); + if (status == NTLSTATUS_NO_MORE_FILES) break; + if (!KSUCCESS(status)) return fail("enumerate namespace root", status); + if (strcmp(entry.Name, "Device") == 0 && entry.Kind == NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) sawDevice = 1; + if (strcmp(entry.Name, "Sessions") == 0 && entry.Kind == NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) sawSessions = 1; + if (strcmp(entry.Name, "Services") == 0 && entry.Kind == NTL_RESOURCE_KIND_NAMESPACE_DIRECTORY) sawServices = 1; + if (strcmp(entry.Name, "SystemRoot") == 0 && entry.Kind == NTL_RESOURCE_KIND_SYMBOLIC_LINK) sawSystemRoot = 1; + } + if (!sawDevice || !sawSessions || !sawServices || !sawSystemRoot) + return fail("root enumeration contents", NTLSTATUS_NOT_FOUND); // RSSRV assigns its own monotonic /Device/HarddiskVolumeN names to mounted IO_VOLUME instances. uint32_t publishedVolumes = 0; uint32_t systemVolumePublished = 0; - for (uint32_t index = 1; index <= 32; ++index) { + NTL_RESOURCE_ENUM_CURSOR deviceCursor; + memset(&deviceCursor, 0, sizeof(deviceCursor)); + for (;;) { + NTL_RESOURCE_ENUM_ENTRY entry; + status = NtlResourceEnumerate(session, SYS_SV_LITERAL("/Device"), &deviceCursor, &entry); + if (status == NTLSTATUS_NO_MORE_FILES) break; + if (!KSUCCESS(status)) return fail("enumerate /Device", status); + if (entry.Kind != NTL_RESOURCE_KIND_VOLUME || strncmp(entry.Name, "HarddiskVolume", 14) != 0) + continue; + char volumePathBuffer[48]; - SYS_STRING_VIEW volumePath = make_volume_path(index, volumePathBuffer, sizeof(volumePathBuffer)); - if (!volumePath.Data) - return fail("build dynamic volume path", NTLSTATUS_FAILURE); + size_t nameLength = strlen(entry.Name); + if (sizeof("/Device/") - 1u + nameLength + 1u > sizeof(volumePathBuffer)) + return fail("dynamic volume name too long", NTLSTATUS_FAILURE); + memcpy(volumePathBuffer, "/Device/", sizeof("/Device/") - 1u); + memcpy(volumePathBuffer + sizeof("/Device/") - 1u, entry.Name, nameLength + 1u); + SYS_STRING_VIEW volumePath = SysSvFromCstr(volumePathBuffer); SYS_HANDLE published = SYS_HANDLE_INVALID; status = NtlResourceLookup(session, volumePath, SYS_RIGHT_QUERY | SYS_VOLUME_RIGHT_OPEN, &published); - if (status == NTLSTATUS_NOT_FOUND) - continue; if (!KSUCCESS(status)) - return fail("lookup dynamic volume", status); + return fail("lookup enumerated volume", status); publishedVolumes++; SYS_HANDLE probe = SYS_HANDLE_INVALID; diff --git a/code/user/tests/surface_cross_process_smoke/surface_cross_process_smoke.c b/code/user/tests/surface_cross_process_smoke/surface_cross_process_smoke.c new file mode 100644 index 0000000..76ac228 --- /dev/null +++ b/code/user/tests/surface_cross_process_smoke/surface_cross_process_smoke.c @@ -0,0 +1,161 @@ +// Cross-process surface smoke test. It proves transferred read-only mapping, shared backing, and commit wakeup. + +#include +#include +#include +#include +#include +#include + +#define TEST_SURFACE_TAG 0x53555246u +#define TEST_READY_TAG 0x53555245u +#define TEST_WIDTH 32u +#define TEST_HEIGHT 16u +#define TEST_PIXEL_X 7u +#define TEST_PIXEL_Y 5u +#define TEST_VALUE_A 0x00123456u +#define TEST_VALUE_B 0x00ABCDEFu + +// Terminates the test without depending on a compiler runtime return path. +static void exit_now(int code) { + for (;;) SysExitProcess(code); +} + +// Writes one diagnostic string through the native console path. +static void print(const char* text) { + SYS_WRITE_CONSOLE_REQUEST request = { sizeof(request), SysSvFromCstr(text) }; + (void)SysWriteConsole(&request); +} + +// Returns whether this process was launched as the verifier child. +static bool is_child(void) { + SYS_PEB* peb = SysGetPeb(); + static const char marker[] = "surface-cross-process-child"; + return peb && peb->CommandLine.Length == sizeof(marker) - 1u && + memcmp(peb->CommandLine.Data, marker, sizeof(marker) - 1u) == 0; +} + +// Reads one pixel from a pitch-based XRGB8888 mapping. +static uint32_t read_pixel(const void* base, uint32_t pitch) { + const uint8_t* row = (const uint8_t*)base + (size_t)TEST_PIXEL_Y * pitch; + return ((const uint32_t*)row)[TEST_PIXEL_X]; +} + +// Writes one pixel to a pitch-based XRGB8888 mapping. +static void write_pixel(void* base, uint32_t pitch, uint32_t value) { + uint8_t* row = (uint8_t*)base + (size_t)TEST_PIXEL_Y * pitch; + ((uint32_t*)row)[TEST_PIXEL_X] = value; +} + +// Runs the child side using only the attenuated capabilities supplied at process construction. +static void run_child(void) { + SYS_HANDLE surface = SysGetStartupCapability(TEST_SURFACE_TAG); + SYS_HANDLE ready = SysGetStartupCapability(TEST_READY_TAG); + if (surface == SYS_HANDLE_INVALID || ready == SYS_HANDLE_INVALID) exit_now(10); + + void* invalidBase = (void*)0; + uint32_t invalidPitch = 0; + KSTATUS status = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, + &invalidBase, &invalidPitch); + if (status != NTLSTATUS_ACCESS_DENIED) exit_now(11); + + void* base = (void*)0; + uint32_t pitch = 0; + status = SysMapSurface(surface, SYS_SURFACE_MAP_READ, &base, &pitch); + if (!KSUCCESS(status) || !base || pitch < TEST_WIDTH * sizeof(uint32_t)) exit_now(12); + if (read_pixel(base, pitch) != TEST_VALUE_A) exit_now(13); + + status = SysSignalEvent(ready); + if (!KSUCCESS(status)) exit_now(14); + status = SysWait(surface, SYS_WAIT_FLAG_NONE); + if (!KSUCCESS(status)) exit_now(15); + if (read_pixel(base, pitch) != TEST_VALUE_B) exit_now(16); + + SYS_SURFACE_DESC desc; + memset(&desc, 0, sizeof(desc)); + status = SysQuerySurface(surface, &desc); + if (!KSUCCESS(status) || desc.ContentGeneration != 1u || desc.CommitCount != 1u) exit_now(17); + + status = SysUnmapSurface(base); + if (!KSUCCESS(status)) exit_now(18); + exit_now(0); +} + +// Creates the producer surface, transfers an attenuated capability, and verifies child observation after commit. +int main(void) { + if (is_child()) run_child(); + + SYS_PEB* peb = SysGetPeb(); + if (!peb || peb->Magic != SYS_PEB_MAGIC || peb->Version != SYS_ENV_VERSION) exit_now(1); + + SYS_HANDLE surface = SYS_HANDLE_INVALID; + SYS_CREATE_SURFACE_REQUEST createSurface; + memset(&createSurface, 0, sizeof(createSurface)); + createSurface.Size = sizeof(createSurface); + createSurface.Width = TEST_WIDTH; + createSurface.Height = TEST_HEIGHT; + createSurface.Format = SYS_SURFACE_FORMAT_XRGB8888; + createSurface.Flags = SYS_SURFACE_FLAG_NONE; + createSurface.HandleOut = &surface; + KSTATUS status = SysCreateSurface(&createSurface); + if (!KSUCCESS(status)) exit_now(2); + + void* base = (void*)0; + uint32_t pitch = 0; + status = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, &base, &pitch); + if (!KSUCCESS(status)) exit_now(3); + write_pixel(base, pitch, TEST_VALUE_A); + + SYS_HANDLE ready = SYS_HANDLE_INVALID; + status = SysCreateEvent(0u, 0u, &ready); + if (!KSUCCESS(status)) exit_now(4); + + NTL_PROCESS_STARTUP_CAPABILITY startup[2]; + memset(startup, 0, sizeof(startup)); + startup[0].Tag = TEST_SURFACE_TAG; + startup[0].SourceHandle = surface; + startup[0].DesiredRights = SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_SURFACE_RIGHT_MAP_READ; + startup[0].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; + startup[1].Tag = TEST_READY_TAG; + startup[1].SourceHandle = ready; + startup[1].DesiredRights = SYS_RIGHT_QUERY | SYS_EVENT_RIGHT_SIGNAL; + startup[1].DuplicateFlags = SYS_DUPLICATE_HANDLE_FLAG_NONE; + + SYS_HANDLE child = SYS_HANDLE_INVALID; + NTL_CREATE_SYSTEM_PROCESS_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Flags = NTL_PROCESS_LOAD_FLAG_NONE; + create.ImageFile = SYS_HANDLE_INVALID; + create.Path = peb->ImagePath; + create.CommandLine = SYS_SV_LITERAL("surface-cross-process-child"); + create.SecurityContext = SYS_SECURITY_CONTEXT_CURRENT; + create.ResolveModuleName = NtlResolveSystemModuleName; + create.StartupCapabilities = startup; + create.StartupCapabilityCount = 2u; + create.ProcessHandleOut = &child; + status = NtlCreateSystemProcess(&create); + if (!KSUCCESS(status)) exit_now(5); + + status = SysWait(ready, SYS_WAIT_FLAG_NONE); + if (!KSUCCESS(status)) exit_now(6); + write_pixel(base, pitch, TEST_VALUE_B); + + DISPLAY_RECT damage = { (int32_t)TEST_PIXEL_X, (int32_t)TEST_PIXEL_Y, 1, 1 }; + uint64_t generation = 0; + status = SysCommitSurface(surface, &damage, 1u, &generation); + if (!KSUCCESS(status) || generation != 1u) exit_now(7); + + status = SysWait(child, SYS_WAIT_FLAG_NONE); + if (!KSUCCESS(status)) exit_now(8); + int32_t childExit = -1; + status = SysGetProcessExitCode(child, &childExit); + if (!KSUCCESS(status) || childExit != 0) exit_now(9); + + (void)SysCloseHandle(child); + (void)SysCloseHandle(ready); + (void)SysUnmapSurface(base); + (void)SysCloseHandle(surface); + print("surface_cross_process_smoke: PASS\n"); + return 0; +} diff --git a/code/user/tests/surface_invalid_rect/surface_invalid_rect.c b/code/user/tests/surface_invalid_rect/surface_invalid_rect.c index f034115..91d3085 100644 --- a/code/user/tests/surface_invalid_rect/surface_invalid_rect.c +++ b/code/user/tests/surface_invalid_rect/surface_invalid_rect.c @@ -89,26 +89,29 @@ int main(void) { void* base = 0; uint32_t pitch = 0; - s = SysMapSurface(surface, &base, &pitch); + s = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, &base, &pitch); if (s != NTLSTATUS_SUCCESS) fail_status("map surface", s); if (!base || pitch < SURFACE_W * sizeof(uint32_t)) fail("invalid mapping"); - s = SysPresentSurface(surface, 0, 1); - expect_status("null rect array with nonzero count", s, NTLSTATUS_INVALID_PARAMETER); + s = SysCommitSurface(surface, 0, 1, 0); + expect_status("commit null rect array with nonzero count", s, NTLSTATUS_INVALID_PARAMETER); + DISPLAY_RECT rects[SYS_SURFACE_MAX_RECTS + 1]; memset(rects, 0, sizeof(rects)); - s = SysPresentSurface(surface, rects, SYS_SURFACE_MAX_RECTS + 1); - expect_status("too many rects", s, NTLSTATUS_INVALID_PARAMETER); + s = SysCommitSurface(surface, rects, SYS_SURFACE_MAX_RECTS + 1, 0); + expect_status("commit too many rects", s, NTLSTATUS_INVALID_PARAMETER); DISPLAY_RECT clipped = { -8, -8, 24, 24 }; - s = SysPresentSurface(surface, &clipped, 1); - if (s != NTLSTATUS_SUCCESS) fail_status("present clipped rect", s); + s = SysCommitSurface(surface, &clipped, 1, 0); + if (s != NTLSTATUS_SUCCESS) fail_status("commit clipped rect", s); DISPLAY_RECT empty = { 10000, 10000, 16, 16 }; - s = SysPresentSurface(surface, &empty, 1); - if (s != NTLSTATUS_SUCCESS) fail_status("present empty clipped rect", s); + s = SysCommitSurface(surface, &empty, 1, 0); + if (s != NTLSTATUS_SUCCESS) fail_status("commit empty clipped rect", s); + s = SysUnmapSurface(base); + if (s != NTLSTATUS_SUCCESS) fail_status("unmap surface", s); SysCloseHandle(surface); print("surface_invalid_rect: PASS\n"); exit_now(0); diff --git a/code/user/tests/surface_multithread_present/surface_multithread_present.c b/code/user/tests/surface_multithread_commit/surface_multithread_commit.c similarity index 83% rename from code/user/tests/surface_multithread_present/surface_multithread_present.c rename to code/user/tests/surface_multithread_commit/surface_multithread_commit.c index 08d7593..8a4b389 100644 --- a/code/user/tests/surface_multithread_present/surface_multithread_present.c +++ b/code/user/tests/surface_multithread_commit/surface_multithread_commit.c @@ -1,5 +1,5 @@ // Native surface multithreaded present test. -// Multiple user threads present one process-private surface to catch mapping and scratch-buffer races. +// Multiple user threads commit and legacy-present one shared surface to catch synchronization regressions. #include #include @@ -47,14 +47,14 @@ static void exit_now(int code) { } static void fail(const char* msg) { - print("surface_multithread_present: "); + print("surface_multithread_commit: "); print(msg); print("\n"); exit_now(1); } static void fail_status(const char* what, uint32_t status) { - print("surface_multithread_present: "); + print("surface_multithread_commit: "); print(what); print(" failed status="); print_u64(status); @@ -79,7 +79,7 @@ static void worker_main(uintptr_t arg) { } DISPLAY_RECT rect = { 0, (int32_t)y0, (int32_t)SURFACE_W, (int32_t)h }; - uint32_t s = SysPresentSurface(st->Surface, &rect, 1); + uint32_t s = SysCommitSurface(st->Surface, &rect, 1, 0); if (s != NTLSTATUS_SUCCESS) { st->Status = s; SysExitThread(1); @@ -93,7 +93,7 @@ static void worker_main(uintptr_t arg) { } int main(void) { - print("surface_multithread_present: start\n"); + print("surface_multithread_commit: start\n"); uint32_t surface = 0; SYS_CREATE_SURFACE_REQUEST req; @@ -109,7 +109,7 @@ int main(void) { void* base = 0; uint32_t pitch = 0; - s = SysMapSurface(surface, &base, &pitch); + s = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, &base, &pitch); if (s != NTLSTATUS_SUCCESS) fail_status("map surface", s); if (!base || pitch < SURFACE_W * sizeof(uint32_t)) fail("invalid mapping"); @@ -145,7 +145,7 @@ int main(void) { for (uint32_t i = 0; i < THREAD_COUNT; ++i) { if (gWorkers[i].Status != NTLSTATUS_SUCCESS) { - print("surface_multithread_present: worker "); + print("surface_multithread_commit: worker "); print_u64(i); print(" status="); print_u64(gWorkers[i].Status); @@ -157,8 +157,17 @@ int main(void) { if (gWorkers[i].IterationsDone != ITERATIONS) fail("worker iteration count mismatch"); } + SYS_SURFACE_DESC desc; + memset(&desc, 0, sizeof(desc)); + s = SysQuerySurface(surface, &desc); + if (s != NTLSTATUS_SUCCESS) fail_status("query surface", s); + if (desc.CommitCount != (uint64_t)THREAD_COUNT * ITERATIONS) + fail("commit count mismatch"); + + s = SysUnmapSurface(base); + if (s != NTLSTATUS_SUCCESS) fail_status("unmap surface", s); SysCloseHandle(surface); - print("surface_multithread_present: PASS\n"); + print("surface_multithread_commit: PASS\n"); exit_now(0); return 0; } diff --git a/code/user/tests/surface_smoke/surface_smoke.c b/code/user/tests/surface_smoke/surface_smoke.c index b68f1bb..7b23232 100644 --- a/code/user/tests/surface_smoke/surface_smoke.c +++ b/code/user/tests/surface_smoke/surface_smoke.c @@ -1,5 +1,5 @@ // Native surface smoke test. -// Verifies that process-private surface handles can be created, mapped, queried, presented, and closed. +// Verifies shared surface storage, attenuated mappings, commit notification, and cleanup. #include #include @@ -67,17 +67,34 @@ int main(void) { void* base = 0; uint32_t pitch = 0; - s = SysMapSurface(surface, &base, &pitch); + s = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE, &base, &pitch); if (s != NTLSTATUS_SUCCESS) fail_status("map surface", s); if (!base || pitch < SURFACE_W * sizeof(uint32_t)) fail("invalid mapping"); + SYS_HANDLE readHandle = SYS_HANDLE_INVALID; + SYS_DERIVE_HANDLE_REQUEST derive = { + sizeof(derive), + surface, + SYS_RIGHT_QUERY | SYS_RIGHT_WAIT | SYS_SURFACE_RIGHT_MAP_READ, + &readHandle, + }; + s = SysDeriveHandle(&derive); + if (s != NTLSTATUS_SUCCESS) fail_status("derive read-only surface", s); + + void* readBase = 0; + uint32_t readPitch = 0; + s = SysMapSurface(readHandle, SYS_SURFACE_MAP_READ, &readBase, &readPitch); + if (s != NTLSTATUS_SUCCESS) fail_status("map read-only surface", s); + if (!readBase || readPitch != pitch || readBase == base) fail("invalid read-only mapping"); + SYS_SURFACE_DESC desc; memset(&desc, 0, sizeof(desc)); s = SysQuerySurface(surface, &desc); if (s != NTLSTATUS_SUCCESS) fail_status("query surface", s); if (desc.Width != SURFACE_W || desc.Height != SURFACE_H) fail("query dimensions mismatch"); if (desc.PitchBytes != pitch || desc.Format != SYS_SURFACE_FORMAT_XRGB8888) fail("query metadata mismatch"); - if (desc.Base != base) fail("query base mismatch"); + if (desc.MapCount != 2) fail("surface map count mismatch"); + if (desc.ContentGeneration != 0 || desc.CommitCount != 0) fail("fresh surface generation mismatch"); uint8_t* rows = (uint8_t*)base; for (uint32_t y = 0; y < SURFACE_H; ++y) { @@ -86,13 +103,35 @@ int main(void) { row[x] = 0x00202020u + ((x & 0xffu) << 16) + ((y & 0xffu) << 8); } + const uint32_t* readPixel = (const uint32_t*)((const uint8_t*)readBase + (size_t)17u * readPitch) + 23u; + uint32_t expectedPixel = 0x00202020u + (23u << 16) + (17u << 8); + if (*readPixel != expectedPixel) fail("shared read-only mapping does not see written pixels"); + DISPLAY_RECT rect = { 0, 0, (int32_t)SURFACE_W, (int32_t)SURFACE_H }; - s = SysPresentSurface(surface, &rect, 1); - if (s != NTLSTATUS_SUCCESS) fail_status("present surface", s); + uint64_t generation = 0; + s = SysCommitSurface(surface, &rect, 1, &generation); + if (s != NTLSTATUS_SUCCESS) fail_status("commit surface", s); + if (generation != 1) fail("commit generation mismatch"); - s = SysPresentSurface(surface, 0, 0); - if (s != NTLSTATUS_SUCCESS) fail_status("present full surface", s); + s = SysWaitSingle(surface, 0, 0); + if (s != NTLSTATUS_SUCCESS) fail_status("wait committed surface", s); + s = SysWaitSingle(surface, 0, 0); + if (s != NTLSTATUS_TIMEOUT) fail_status("surface commit event did not auto-reset", s); + memset(&desc, 0, sizeof(desc)); + s = SysQuerySurface(surface, &desc); + if (s != NTLSTATUS_SUCCESS) fail_status("query committed surface", s); + if (desc.ContentGeneration != 1 || desc.CommitCount != 1 || !desc.LastDamageValid) + fail("committed surface metadata mismatch"); + if (desc.LastDamageBounds.X != 0 || desc.LastDamageBounds.Y != 0 || + desc.LastDamageBounds.W != SURFACE_W || desc.LastDamageBounds.H != SURFACE_H) + fail("commit damage bounds mismatch"); + + s = SysUnmapSurface(readBase); + if (s != NTLSTATUS_SUCCESS) fail_status("unmap read-only surface", s); + s = SysUnmapSurface(base); + if (s != NTLSTATUS_SUCCESS) fail_status("unmap surface", s); + SysCloseHandle(readHandle); SysCloseHandle(surface); print("surface_smoke: PASS\n"); exit_now(0); diff --git a/code/user/tests/thread_name_smoke/thread_name_smoke.c b/code/user/tests/thread_name_smoke/thread_name_smoke.c new file mode 100644 index 0000000..aa086a0 --- /dev/null +++ b/code/user/tests/thread_name_smoke/thread_name_smoke.c @@ -0,0 +1,149 @@ +// Native thread-description smoke test. + +#include +#include +#include +#include +#include + +#define TEST_NAME_MAIN "thread-name-main" +#define TEST_NAME_WORKER "thread-name-worker" +#define TEST_MAX_THREADS 16u +#define TEST_EVENT_AUTO_RESET 0u + +// Writes one NUL-terminated string to the console. +static void print(const char* text) { + SYS_WRITE_CONSOLE_REQUEST request = { sizeof(SYS_WRITE_CONSOLE_REQUEST), SysSvFromCstr(text) }; + (void)SysWriteConsole(&request); +} + +// Terminates the test immediately. +static void exit_now(int32_t code) { + for (;;) SysExitProcess(code); +} + +// Fails the test with one diagnostic. +static void fail(const char* text) { + print("thread_name_smoke: "); + print(text); + print("\n"); + exit_now(1); +} + +// Sets a description on the supplied native thread handle. +static KSTATUS set_name(SYS_HANDLE thread, const char* name) { + SYS_SET_THREAD_NAME_REQUEST request; + memset(&request, 0, sizeof(request)); + request.Size = sizeof(request); + request.ThreadHandle = thread; + request.Name = SysSvFromCstr(name); + return SysSetThreadName(&request); +} + +// A suspended worker only needs to prove its named handle remains runnable. +static void worker(uintptr_t argument) { + (void)argument; + SysExitThread(0); +} + +// A detached worker proves running threads do not need a returned handle or TID. +static void detached_worker(uintptr_t argument) { + (void)SysSignalEvent((SYS_HANDLE)argument); + SysExitThread(0); +} + +// Runs the thread-description ABI smoke test. +int main(void) { + print("thread_name_smoke: start\n"); + + if (set_name(SYS_HANDLE_INVALID, TEST_NAME_MAIN) != NTLSTATUS_SUCCESS) + fail("set current name failed"); + + char name[SYS_THREAD_NAME_MAX]; + size_t length = 0; + KSTATUS status = SysQueryThreadName(SYS_HANDLE_INVALID, name, sizeof(name), &length); + if (status != NTLSTATUS_SUCCESS || length != sizeof(TEST_NAME_MAIN) - 1u || + strcmp(name, TEST_NAME_MAIN) != 0) + fail("query current name mismatch"); + + size_t required = 0; + status = SysQueryThreadName(SYS_HANDLE_INVALID, (char*)0, 0, &required); + if (status != NTLSTATUS_BUFFER_TOO_SMALL || required != sizeof(TEST_NAME_MAIN) - 1u) + fail("length-only query contract mismatch"); + + SYS_HANDLE thread = SYS_HANDLE_INVALID; + uint32_t tid = 0; + SYS_CREATE_THREAD_REQUEST create; + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)worker; + create.Priority = 1u; + create.Flags = SYS_CREATE_THREAD_FLAG_SUSPENDED; + create.ThreadHandleOut = &thread; + create.ThreadIdOut = &tid; + status = SysCreateThread(&create); + if (status != NTLSTATUS_SUCCESS || thread == SYS_HANDLE_INVALID || tid == 0) + fail("create suspended worker failed"); + + status = set_name(thread, TEST_NAME_WORKER); + if (status != NTLSTATUS_SUCCESS) + fail("set worker name failed"); + + memset(name, 0, sizeof(name)); + length = 0; + status = SysQueryThreadName(thread, name, sizeof(name), &length); + if (status != NTLSTATUS_SUCCESS || length != sizeof(TEST_NAME_WORKER) - 1u || + strcmp(name, TEST_NAME_WORKER) != 0) + fail("query worker name mismatch"); + + SYS_THREAD_INFO threads[TEST_MAX_THREADS]; + size_t threadCount = 0; + status = SysGetThreadList(SysGetCurrentPid(), threads, TEST_MAX_THREADS, &threadCount); + if (status != NTLSTATUS_SUCCESS) + fail("thread enumeration failed"); + + int foundWorker = 0; + size_t visible = threadCount < TEST_MAX_THREADS ? threadCount : TEST_MAX_THREADS; + for (size_t i = 0; i < visible; ++i) { + if (threads[i].ThreadId == tid) { + if (strcmp(threads[i].Name, TEST_NAME_WORKER) != 0) + fail("thread-list name mismatch"); + foundWorker = 1; + break; + } + } + if (!foundWorker) + fail("worker missing from thread list"); + + uint32_t previousSuspendCount = 0; + status = SysResumeThread(thread, &previousSuspendCount); + if (status != NTLSTATUS_SUCCESS || previousSuspendCount != 1u) + fail("resume worker failed"); + status = SysWait(thread, SYS_WAIT_FLAG_NONE); + if (status != NTLSTATUS_SUCCESS) + fail("wait worker failed"); + (void)SysCloseHandle(thread); + + SYS_HANDLE detachedReady = SYS_HANDLE_INVALID; + status = SysCreateEvent(TEST_EVENT_AUTO_RESET, 0, &detachedReady); + if (status != NTLSTATUS_SUCCESS) + fail("create detached-ready event failed"); + + memset(&create, 0, sizeof(create)); + create.Size = sizeof(create); + create.Entry = (uintptr_t)detached_worker; + create.Argument = (uintptr_t)detachedReady; + create.Priority = 1u; + create.Flags = SYS_CREATE_THREAD_FLAG_NONE; + status = SysCreateThread(&create); + if (status != NTLSTATUS_SUCCESS) + fail("create detached worker failed"); + status = SysWait(detachedReady, SYS_WAIT_FLAG_NONE); + if (status != NTLSTATUS_SUCCESS) + fail("detached worker did not run"); + (void)SysCloseHandle(detachedReady); + + print("thread_name_smoke: PASS\n"); + exit_now(0); + return 0; +} diff --git a/docs.md b/docs.md index 6cca3fe..6ff5d14 100644 --- a/docs.md +++ b/docs.md @@ -1,4 +1,4 @@ -# nt-lite development notes +# nt-lite instruction notes for LLMs (development notes) nt-lite is a clean-design hobby kernel. It is not a stable compatibility platform. Prefer the right design over preserving old APIs, shims, or compatibility aliases. @@ -98,6 +98,7 @@ Breaking changes are allowed. When making one: - 0x%016llX over %p, it looks prettier and is standard. - never add Ex, it makes me mad, never, rearchitect instead - i do put a focus on overengineering if it means it will be cleaner and futureproof -- ASK QUESTIONS! never assume you know the right answer, if you do, you are probably wrong +- **ASK QUESTIONS!** i can't stress this enough. never assume you know the right answer. if you do, you are probably wrong. +- **DISCUSS!** if it comes to designing something, anything, discuss it with me first. often times i can suggest something simpler, faster and elegant. - shift the syscall number table every time you add a syscall, do not append to the end - do not worry if a patch breaks something, it's better to break, document and fix rather than work around it. diff --git a/license b/license index 6558b19..549577c 100644 --- a/license +++ b/license @@ -1,7 +1,29 @@ -MIT No Attribution -Copyright + WTFPL+ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE PLUS + Version 1, August 2025 -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. +Copyright (C) 2026 Maksymilian Wilczyński -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +Everyone is permitted to copy and distribute verbatim or modified +copies of this license document, and changing it is allowed as long +as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE PLUS + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO! + + DISCLAIMER +This software is provided "AS IS", without warranty of any kind, +express or implied, including but not limited to the warranties of +merchantability, fitness for a particular purpose and noninfringement. +In no event shall the authors be liable for any claim, damages or +other liability, whether in an action of contract, tort or otherwise, +arising from, out of or in connection with the software or the use or +other dealings in the software. + +So in short: + DO WHATEVER YOU WANT, + BUT IF IT BREAKS SOMETHING, + DON’T COME CRYING — OR SUING — TO ME. diff --git a/readme.md b/readme.md index cbf6b82..c57fae9 100644 --- a/readme.md +++ b/readme.md @@ -108,11 +108,12 @@ drivers/ hda.sys lwip.sys ntfs.sys + ext4.sys pci.sys ps2.sys rtl816x.sys rtl8139.sys - serial.sys + cereal.sys usb.sys vmwaresvga.sys apps/