Skip to content

Commit

Permalink
mount code, sort of clean
Browse files Browse the repository at this point in the history
Signed-off-by: Jorgen Lundman <lundman@lundman.net>
  • Loading branch information
lundman committed Jul 22, 2024
1 parent b0f716a commit bde195d
Show file tree
Hide file tree
Showing 14 changed files with 2,020 additions and 456 deletions.
3 changes: 1 addition & 2 deletions contrib/windows/OpenZFS/OpenZFS/OpenZFS.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@
<DebuggerFlavor>DbgengKernelDebugger</DebuggerFlavor>
<Inf2CatUseLocalTime>true</Inf2CatUseLocalTime>
<DisableProductionSignDebugWarnings>false</DisableProductionSignDebugWarnings>
<TimeStampServer>
</TimeStampServer>
<TimeStampServer>http://timestamp.digicert.com</TimeStampServer>
<IncludePath>$(IncludePath);$(KMDF_INC_PATH)$(KMDF_VER_PATH);../../../../include</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand Down
16 changes: 13 additions & 3 deletions include/os/windows/spl/sys/mount.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ struct vfsstatfs {
};

typedef enum _FSD_IDENTIFIER_TYPE {
MOUNT_TYPE_DGL = ':DGL', // Dokan Global
MOUNT_TYPE_DGL = ':DGL', // Global
MOUNT_TYPE_BUS = ':BUS', // Bus Control
MOUNT_TYPE_DCB = ':DCB', // Disk Control Block
MOUNT_TYPE_VCB = ':VCB', // Volume Control Block
MOUNT_TYPE_FCB = ':FCB', // File Control Block
Expand All @@ -96,15 +97,24 @@ struct mount
ULONG size;
void *fsprivate;
void *parent_device; // Only set so vcd can find dcb
PDEVICE_OBJECT deviceObject;
PDEVICE_OBJECT diskDeviceObject;
uuid_t rawuuid;
PDEVICE_OBJECT PhysicalDeviceObject; // From AddDevices
PDEVICE_OBJECT LowerDeviceObject; // Attaching PDO in AddDevices
PDEVICE_OBJECT FunctionalDeviceObject; // Created in AddDevices
PDEVICE_OBJECT VolumeDeviceObject;
PDEVICE_OBJECT AttachedDevice;
UNICODE_STRING bus_name;
UNICODE_STRING device_name;
UNICODE_STRING symlink_name;
UNICODE_STRING arc_name;
UNICODE_STRING fs_name;
UNICODE_STRING name;
UNICODE_STRING uuid;
UNICODE_STRING mountpoint;
UNICODE_STRING deviceInterfaceName;
UNICODE_STRING fsInterfaceName;
UNICODE_STRING volumeInterfaceName;
PFILE_OBJECT root_file;
boolean_t justDriveLetter;
uint64_t volume_opens;
PVPB vpb;
Expand Down
1 change: 1 addition & 0 deletions include/os/windows/zfs/sys/zfs_context_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ typedef struct spa_iokit spa_iokit_t;
extern boolean_t ml_set_interrupts_enabled(boolean_t);
extern PDRIVER_OBJECT WIN_DriverObject;


/*
* Ok this is pretty gross - until we can get rid of it from lua -
* it works as long as it doesn't parse strings
Expand Down
9 changes: 9 additions & 0 deletions include/os/windows/zfs/sys/zfs_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ typedef struct zfs_ccb zfs_ccb_t;

extern uint64_t zfs_module_busy;

#define DIR_LINKS(zp) (S_ISDIR((zp)->z_mode) ? (zp)->z_links - 1 : (zp)->z_links)

extern CACHE_MANAGER_CALLBACKS CacheManagerCallbacks;


Expand Down Expand Up @@ -143,6 +145,8 @@ extern size_t get_reparse_point_impl(znode_t *zp, char *buffer, size_t outlen);
extern void fastio_init(FAST_IO_DISPATCH **fast);
extern NTSTATUS pnp_query_di(PDEVICE_OBJECT DeviceObject, PIRP Irp,
PIO_STACK_LOCATION IrpSp);
extern NTSTATUS pnp_query_bus_information(PDEVICE_OBJECT DeviceObject, PIRP Irp,
PIO_STACK_LOCATION IrpSp);
extern NTSTATUS pnp_device_usage_notification(PDEVICE_OBJECT DeviceObject,
PIRP Irp, PIO_STACK_LOCATION IrpSp);

Expand All @@ -167,6 +171,8 @@ extern void acl_trivial_access_masks(mode_t mode, boolean_t isdir,
extern void zfs_save_ntsecurity(struct vnode *vp);
void zfs_load_ntsecurity(struct vnode *vp);
struct vnode *zfs_parent(struct vnode *);
extern PVOID MapUserBuffer(IN OUT PIRP Irp);


/* IRP_MJ_SET_INFORMATION helpers */
extern NTSTATUS set_file_basic_information(PDEVICE_OBJECT, PIRP,
Expand Down Expand Up @@ -234,6 +240,7 @@ extern NTSTATUS file_hard_link_information(PDEVICE_OBJECT, PIRP,

/* IRP_MJ_DEVICE_CONTROL helpers */
extern NTSTATUS QueryCapabilities(PDEVICE_OBJECT, PIRP, PIO_STACK_LOCATION);
extern NTSTATUS QueryDeviceRelations(PDEVICE_OBJECT, PIRP, PIO_STACK_LOCATION);
extern NTSTATUS ioctl_query_device_name(PDEVICE_OBJECT, PIRP,
PIO_STACK_LOCATION);
extern NTSTATUS ioctl_disk_get_drive_geometry(PDEVICE_OBJECT, PIRP,
Expand Down Expand Up @@ -266,6 +273,8 @@ extern NTSTATUS fsctl_set_zero_data(PDEVICE_OBJECT DeviceObject, PIRP Irp,
PIO_STACK_LOCATION IrpSp);
extern NTSTATUS ioctl_get_gpt_attributes(PDEVICE_OBJECT DeviceObject, PIRP Irp,
PIO_STACK_LOCATION IrpSp);
extern NTSTATUS volume_read(PDEVICE_OBJECT DeviceObject, PIRP Irp,
PIO_STACK_LOCATION IrpSp);


#endif
13 changes: 10 additions & 3 deletions module/os/windows/OpenZFS.inf
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,22 @@ OpenZFS.DllFiles = 11 ;%windir%\system32

[Standard.NTamd64]
%VolumeName% = OpenZFS_Install, OpenZFSVolume
;%DriverName% = OpenZFS_Install, ROOT\OpenZFS
%ControllerName% = OpenZFS_Install, ROOT\OpenZFS

[Standard.NTx86]
%VolumeName% = OpenZFS_Install, OpenZFSVolume
;%DriverName% = OpenZFS_Install, ROOT\OpenZFS
%ControllerName% = OpenZFS_Install, ROOT\OpenZFS

[OpenZFS_Install]
OptionDesc = %ServiceDescription%
CopyFiles = OpenZFS.DriverFiles
AddReg=Device_AddReg

[Device_AddReg]
HKR,,DeviceType,0x00010001,1

;;,OpenZFS.DllFiles
;;RegisterDlls = shellopenzfs

Expand All @@ -71,7 +78,7 @@ DelService = %ServiceName%,0x200 ;Ensure service is stopped before deleting
[OpenZFS.Service]
DisplayName = %ServiceName%
Description = %ServiceDescription%
ServiceBinary = %12%\OpenZFS.sys ;%windir%\system32\drivers\
ServiceBinary = %12%\%DriverName%.sys ;%windir%\system32\drivers\
ServiceType = 1
StartType = 1 ;SERVICE_SYSTEM_START
ErrorControl = 1
Expand All @@ -85,8 +92,8 @@ LoadOrderGroup = "File System"
;

[OpenZFS.DriverFiles]
OpenZFS.sys
OpenZFS.man
%DriverName%.sys
%DriverName%.man

[OpenZFS.DllFiles]
;;zpool.exe
Expand Down
118 changes: 117 additions & 1 deletion module/os/windows/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
#include <sys/kstat.h>

#include <ntddk.h>
#include <ntddstor.h>
#include <storport.h>

#include <sys/taskq.h>
#include <sys/wzvol.h>
#include <sys/mount.h>
#include <sys/driver_extension.h>

#include "Trace.h"

Expand Down Expand Up @@ -65,13 +68,103 @@ extern void sysctl_os_fini(void);
PDRIVER_OBJECT WIN_DriverObject = NULL;
PDRIVER_UNLOAD STOR_DriverUnload = NULL;
PDRIVER_DISPATCH STOR_MajorFunction[IRP_MJ_MAXIMUM_FUNCTION + 1];

wzvolDriverInfo STOR_wzvolDriverInfo;

extern _Function_class_(IO_WORKITEM_ROUTINE)
void __stdcall
sysctl_os_registry_change(DEVICE_OBJECT *DeviceObject,
PVOID Parameter);
void
mount_add_device(PDEVICE_OBJECT DriverObject,
PDEVICE_OBJECT PhysicalDeviceObject, PDEVICE_OBJECT AddDeviceObject);

NTSTATUS
OpenZFS_AddDevice(
IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT PhysicalDeviceObject
)
{
NTSTATUS status;

if (PhysicalDeviceObject == NULL) {
DbgBreakPoint();
return (STATUS_NO_MORE_ENTRIES);
}

OpenZFS_Driver_Extension *DriverExtension =
(OpenZFS_Driver_Extension *)IoGetDriverObjectExtension(DriverObject, DriverObject);

xprintf("%s called: PhysicalDeviceObject %p\n", __func__, PhysicalDeviceObject);
DbgBreakPoint();

if (DriverExtension == NULL) {
dprintf("No DriverExtension? Help!\n");
return (STATUS_INVALID_PARAMETER);
}

if (PhysicalDeviceObject->DeviceType == FILE_DEVICE_CONTROLLER) {
DriverExtension->PhysicalDeviceObject = PhysicalDeviceObject;
return (STATUS_SUCCESS);
}

// This isnt needed, we set it in DriverLoad
if (DriverExtension->PhysicalDeviceObject == NULL) {
// Called when driver installs at load time.
DriverExtension->PhysicalDeviceObject = PhysicalDeviceObject;
#if 0 // chatgpt version
// I feel we don't really need this one?
status = IoCreateDevice(
DriverObject,
sizeof(mount_t),
NULL,
FILE_DEVICE_BUS_EXTENDER,
FILE_AUTOGENERATED_DEVICE_NAME | FILE_DEVICE_SECURE_OPEN,
FALSE,
&DriverExtension->FunctionalDeviceObject // busfdo
);

if (NT_SUCCESS(status)) {
mount_t *zmo = DriverExtension->FunctionalDeviceObject->DeviceExtension;
zmo->size = sizeof(mount_t);
zmo->type = MOUNT_TYPE_BUS;
DriverExtension->FunctionalDeviceObject->Flags |= DO_POWER_PAGABLE;
DriverExtension->FunctionalDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
// DriverExtension->ChildDeviceObject = DriverExtension->FunctionalDeviceObject;
zmo->PhysicalDeviceObject = PhysicalDeviceObject;

DriverExtension->LowerDeviceObject =
IoAttachDeviceToDeviceStack(DriverExtension->FunctionalDeviceObject,
PhysicalDeviceObject);

zmo->LowerDeviceObject = DriverExtension->LowerDeviceObject;
RtlInitUnicodeString(&zmo->bus_name, L"OpenZFS");

status = IoRegisterDeviceInterface(DriverExtension->FunctionalDeviceObject,
&BtrfsBusInterface, NULL, &zmo->bus_name);

status = IoSetDeviceInterfaceState(&zmo->bus_name, TRUE);

}
#else // btrfs


#endif
return (STATUS_SUCCESS);
}

// DbgBreakPoint();

// OK, we created a new mount
if (DriverExtension->AddDeviceObject) {
PDEVICE_OBJECT AddDeviceObject = DriverExtension->AddDeviceObject;
DriverExtension->AddDeviceObject = NULL; // Yeah, we need better sync here

mount_add_device(DriverObject, PhysicalDeviceObject, AddDeviceObject);

}

return (STATUS_SUCCESS);
}

void
OpenZFS_Fini(PDRIVER_OBJECT DriverObject)
Expand Down Expand Up @@ -114,9 +207,25 @@ DriverEntry(_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING pRegistryPath)
{
NTSTATUS status;
OpenZFS_Driver_Extension *DriverExtension;

KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL,
"OpenZFS: DriverEntry\n"));

LARGE_INTEGER start, end, frequency;
LONGLONG elapsedTime;
frequency = KeQueryPerformanceCounter(NULL);
start = KeQueryPerformanceCounter(NULL);


status = IoAllocateDriverObjectExtension(
DriverObject,
DriverObject, // Identifying the extension
sizeof(OpenZFS_Driver_Extension), // Size of your custom structure
(PVOID*)&DriverExtension
);


ZFSWppInit(DriverObject, pRegistryPath);

// Setup global so zfs_ioctl.c can setup devnode
Expand Down Expand Up @@ -160,6 +269,7 @@ DriverEntry(_In_ PDRIVER_OBJECT DriverObject,

/* Now set the Driver Callbacks to dispatcher and start ZFS */
WIN_DriverObject->DriverUnload = OpenZFS_Fini;
WIN_DriverObject->DriverExtension->AddDevice = OpenZFS_AddDevice;

/* Start ZFS itself */
zfs_kmod_init();
Expand All @@ -172,5 +282,11 @@ DriverEntry(_In_ PDRIVER_OBJECT DriverObject,

KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL,
"OpenZFS: Started\n"));

end = KeQueryPerformanceCounter(NULL);
elapsedTime = ((end.QuadPart - start.QuadPart) * 1000000) / frequency.QuadPart;
xprintf("DriverEntry execution time: %lld microseconds\n", elapsedTime);

return (STATUS_SUCCESS);
}

10 changes: 5 additions & 5 deletions module/os/windows/zfs/sysctl_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ sysctl_os_process(PUNICODE_STRING pRegistryPath, ztunable_t *zt)
{
HANDLE regfd = NULL;

dprintf(
"tunable: '%s/%s' type %d at %p\n",
zt->zt_prefix, zt->zt_name,
zt->zt_type,
zt->zt_ptr);
// dprintf(
// "tunable: '%s/%s' type %d at %p\n",
// zt->zt_prefix, zt->zt_name,
// zt->zt_type,
// zt->zt_ptr);

/*
* tunable: 'zfs_prefetch_disable' type 0 at FFFFF80731A1B770
Expand Down
Loading

0 comments on commit bde195d

Please sign in to comment.