+/*
+ * Synchronously execute a filesystem control method. This is a wrapper around
+ * NtFsControlFile() that handles STATUS_PENDING. Note that SYNCHRONIZE
+ * permission is, in general, required on the handle.
+ */
+NTSTATUS
+winnt_fsctl(HANDLE h, u32 code, const void *in, u32 in_size,
+ void *out, u32 out_size_avail, u32 *actual_out_size_ret)
+{
+ IO_STATUS_BLOCK iosb;
+ NTSTATUS status;
+
+ status = (*func_NtFsControlFile)(h, NULL, NULL, NULL, &iosb, code,
+ (void *)in, in_size,
+ out, out_size_avail);
+ if (status == STATUS_PENDING) {
+ /* Beware: this case is often encountered with remote
+ * filesystems, but rarely with local filesystems. */
+
+ status = (*func_NtWaitForSingleObject)(h, FALSE, NULL);
+ if (NT_SUCCESS(status)) {
+ status = iosb.Status;
+ } else {
+ /* We shouldn't be issuing ioctls on a handle to which
+ * we don't have SYNCHRONIZE access. Otherwise we have
+ * no way to wait for them to complete. */
+ wimlib_assert(status != STATUS_ACCESS_DENIED);
+ }
+ }
+
+ if (NT_SUCCESS(status) && actual_out_size_ret != NULL)
+ *actual_out_size_ret = (u32)iosb.Information;
+
+ return status;
+}
+