ReactOS  0.4.14-dev-604-gcfdd483
devctrl.c
Go to the documentation of this file.
1 /* Copyright (c) Mark Harmstone 2016-17
2  *
3  * This file is part of WinBtrfs.
4  *
5  * WinBtrfs is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public Licence as published by
7  * the Free Software Foundation, either version 3 of the Licence, or
8  * (at your option) any later version.
9  *
10  * WinBtrfs is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public Licence for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public Licence
16  * along with WinBtrfs. If not, see <http://www.gnu.org/licenses/>. */
17 
18 #include "btrfs_drv.h"
19 #include <ntdddisk.h>
20 #include <mountdev.h>
21 #include <diskguid.h>
22 
23 extern PDRIVER_OBJECT drvobj;
24 extern LIST_ENTRY VcbList;
26 
28  MOUNTDEV_STABLE_GUID* msg = Irp->UserBuffer;
30 
31  TRACE("IOCTL_MOUNTDEV_QUERY_STABLE_GUID\n");
32 
33  if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_STABLE_GUID))
35 
36  RtlCopyMemory(&msg->StableGuid, &Vcb->superblock.uuid, sizeof(GUID));
37 
38  Irp->IoStatus.Information = sizeof(MOUNTDEV_STABLE_GUID);
39 
40  return STATUS_SUCCESS;
41 }
42 
44  TRACE("IOCTL_DISK_IS_WRITABLE\n");
45 
46  return Vcb->readonly ? STATUS_MEDIA_WRITE_PROTECTED : STATUS_SUCCESS;
47 }
48 
51  LIST_ENTRY *le, *le2;
52  btrfs_filesystem* bfs = NULL;
53  ULONG itemsize;
54 
56 
57  if (IsListEmpty(&VcbList)) {
58  if (length < sizeof(btrfs_filesystem)) {
60  goto end;
61  } else {
64  goto end;
65  }
66  }
67 
68  le = VcbList.Flink;
69 
70  while (le != &VcbList) {
73 
74  if (bfs) {
75  bfs->next_entry = itemsize;
76  bfs = (btrfs_filesystem*)((uint8_t*)bfs + itemsize);
77  } else
78  bfs = data;
79 
82  goto end;
83  }
84 
85  itemsize = offsetof(btrfs_filesystem, device);
87 
88  bfs->next_entry = 0;
89  RtlCopyMemory(&bfs->uuid, &Vcb->superblock.uuid, sizeof(BTRFS_UUID));
90 
91  ExAcquireResourceSharedLite(&Vcb->tree_lock, true);
92 
93  bfs->num_devices = (uint32_t)Vcb->superblock.num_devices;
94 
95  bfd = NULL;
96 
97  le2 = Vcb->devices.Flink;
98  while (le2 != &Vcb->devices) {
100  MOUNTDEV_NAME mdn;
101 
102  if (bfd)
104  else
105  bfd = &bfs->device;
106 
108  ExReleaseResourceLite(&Vcb->tree_lock);
110  goto end;
111  }
112 
113  itemsize += (ULONG)offsetof(btrfs_filesystem_device, name[0]);
115 
116  RtlCopyMemory(&bfd->uuid, &dev->devitem.device_uuid, sizeof(BTRFS_UUID));
117 
118  if (dev->devobj) {
119  Status = dev_ioctl(dev->devobj, IOCTL_MOUNTDEV_QUERY_DEVICE_NAME, NULL, 0, &mdn, sizeof(MOUNTDEV_NAME), true, NULL);
121  ExReleaseResourceLite(&Vcb->tree_lock);
122  ERR("IOCTL_MOUNTDEV_QUERY_DEVICE_NAME returned %08x\n", Status);
123  goto end;
124  }
125 
126  if (mdn.NameLength > length) {
127  ExReleaseResourceLite(&Vcb->tree_lock);
129  goto end;
130  }
131 
132  Status = dev_ioctl(dev->devobj, IOCTL_MOUNTDEV_QUERY_DEVICE_NAME, NULL, 0, &bfd->name_length, (ULONG)offsetof(MOUNTDEV_NAME, Name[0]) + mdn.NameLength, true, NULL);
134  ExReleaseResourceLite(&Vcb->tree_lock);
135  ERR("IOCTL_MOUNTDEV_QUERY_DEVICE_NAME returned %08x\n", Status);
136  goto end;
137  }
138 
139  itemsize += bfd->name_length;
140  length -= bfd->name_length;
141  } else {
142  bfd->missing = true;
143  bfd->name_length = 0;
144  }
145 
146  le2 = le2->Flink;
147  }
148 
149  ExReleaseResourceLite(&Vcb->tree_lock);
150 
151  le = le->Flink;
152  }
153 
155 
156 end:
158 
159  return Status;
160 }
161 
162 static NTSTATUS probe_volume(void* data, ULONG length, KPROCESSOR_MODE processor_mode) {
168  const GUID* guid;
169 
170  if (length < sizeof(MOUNTDEV_NAME))
172 
173  if (length < offsetof(MOUNTDEV_NAME, Name[0]) + mdn->NameLength)
175 
176  TRACE("%.*S\n", mdn->NameLength / sizeof(WCHAR), mdn->Name);
177 
178  if (!SeSinglePrivilegeCheck(RtlConvertLongToLuid(SE_MANAGE_VOLUME_PRIVILEGE), processor_mode))
180 
181  path.Buffer = mdn->Name;
182  path.Length = path.MaximumLength = mdn->NameLength;
183 
185  if (!NT_SUCCESS(Status)) {
186  ERR("IoGetDeviceObjectPointer returned %08x\n", Status);
187  return Status;
188  }
189 
191  if (!NT_SUCCESS(Status)) {
192  ERR("get_device_pnp_name returned %08x\n", Status);
194  return Status;
195  }
196 
197  if (RtlCompareMemory(guid, &GUID_DEVINTERFACE_DISK, sizeof(GUID)) == sizeof(GUID)) {
199  if (!NT_SUCCESS(Status))
200  WARN("IOCTL_DISK_UPDATE_PROPERTIES returned %08x\n", Status);
201  }
202 
204 
206 
207  if (RtlCompareMemory(guid, &GUID_DEVINTERFACE_DISK, sizeof(GUID)) == sizeof(GUID))
209  else
211 
212  return STATUS_SUCCESS;
213 }
214 
216  if (!SeSinglePrivilegeCheck(RtlConvertLongToLuid(SE_LOAD_DRIVER_PRIVILEGE), Irp->RequestorMode)) {
217  ERR("insufficient privileges\n");
219  }
220 
221  do_shutdown(Irp);
222 
223  return STATUS_SUCCESS;
224 }
225 
229 
230  switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
232  Status = query_filesystems(map_user_buffer(Irp, NormalPagePriority), IrpSp->Parameters.FileSystemControl.OutputBufferLength);
233  break;
234 
236  Status = probe_volume(Irp->AssociatedIrp.SystemBuffer, IrpSp->Parameters.FileSystemControl.InputBufferLength, Irp->RequestorMode);
237  break;
238 
239  case IOCTL_BTRFS_UNLOAD:
241  break;
242 
243  default:
244  TRACE("unhandled ioctl %x\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
246  break;
247  }
248 
249  return Status;
250 }
251 
254 NTSTATUS __stdcall drv_device_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
258  bool top_level;
259 
261 
262  top_level = is_top_level(Irp);
263 
264  Irp->IoStatus.Information = 0;
265 
266  if (Vcb) {
267  if (Vcb->type == VCB_TYPE_CONTROL) {
269  goto end;
270  } else if (Vcb->type == VCB_TYPE_VOLUME) {
272  goto end;
273  } else if (Vcb->type != VCB_TYPE_FS) {
275  goto end;
276  }
277  } else {
279  goto end;
280  }
281 
282  if (!IrpSp->FileObject || IrpSp->FileObject->FsContext != Vcb->volume_fcb) {
284  goto end;
285  }
286 
287  switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
290  goto end;
291 
294  goto end;
295 
296  default:
297  TRACE("unhandled control code %x\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
298  break;
299  }
300 
302 
303  Status = IoCallDriver(Vcb->Vpb->RealDevice, Irp);
304 
305  goto end2;
306 
307 end:
308  Irp->IoStatus.Status = Status;
309 
310  if (Status != STATUS_PENDING)
312 
313 end2:
314  TRACE("returning %08x\n", Status);
315 
316  if (top_level)
318 
320 
321  return Status;
322 }
ERESOURCE global_loading_lock
Definition: btrfs.c:74
#define IN
Definition: typedefs.h:38
#define STATUS_PRIVILEGE_NOT_HELD
Definition: DriverTester.h:9
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
#define FsRtlEnterFileSystem
#define IOCTL_BTRFS_UNLOAD
Definition: btrfsioctl.h:39
#define FsRtlExitFileSystem
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:225
_In_ PIRP Irp
Definition: csq.h:116
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
GLsizei const GLchar ** path
Definition: glext.h:7234
#define IOCTL_BTRFS_QUERY_FILESYSTEMS
Definition: btrfsioctl.h:21
void * bfd
Definition: i386-dis.c:28
#define WARN(fmt,...)
Definition: debug.h:111
LONG NTSTATUS
Definition: precomp.h:26
#define SE_LOAD_DRIVER_PRIVILEGE
Definition: security.c:664
#define IOCTL_DISK_IS_WRITABLE
Definition: cdrw_usr.h:172
_Dispatch_type_(IRP_MJ_DEVICE_CONTROL)
Definition: devctrl.c:252
BOOLEAN NTAPI SeSinglePrivilegeCheck(IN LUID PrivilegeValue, IN KPROCESSOR_MODE PreviousMode)
Definition: priv.c:524
NTSTATUS NTAPI IoGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName, IN ACCESS_MASK DesiredAccess, OUT PFILE_OBJECT *FileObject, OUT PDEVICE_OBJECT *DeviceObject)
Definition: device.c:1435
#define VCB_TYPE_FS
Definition: btrfs_drv.h:664
VOID NTAPI ObDereferenceObject(IN PVOID Object)
Definition: obref.c:375
GLuint GLuint end
Definition: gl.h:1545
bool is_top_level(_In_ PIRP Irp)
Definition: btrfs.c:276
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
struct _MOUNTDEV_STABLE_GUID MOUNTDEV_STABLE_GUID
#define IOCTL_MOUNTDEV_QUERY_DEVICE_NAME
Definition: imports.h:93
while(1)
Definition: macro.lex.yy.c:740
const GUID * guid
uint32_t num_devices
Definition: btrfsioctl.h:190
void disk_arrival(PDRIVER_OBJECT DriverObject, PUNICODE_STRING devpath)
Definition: search.c:263
Definition: devices.h:37
PVOID DeviceExtension
Definition: env_spec_w32.h:418
smooth NULL
Definition: ftsmooth.c:416
#define offsetof(TYPE, MEMBER)
#define IoCompleteRequest
Definition: irp.c:1240
#define STATUS_MEDIA_WRITE_PROTECTED
Definition: udferr_usr.h:161
_Inout_ PFILE_OBJECT FileObject
Definition: cdprocs.h:593
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
#define IOCTL_MOUNTDEV_QUERY_STABLE_GUID
Definition: imports.h:255
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
NTSTATUS get_device_pnp_name(_In_ PDEVICE_OBJECT DeviceObject, _Out_ PUNICODE_STRING pnp_name, _Out_ const GUID **guid)
Definition: btrfs.c:4109
#define TRACE(s)
Definition: solgame.cpp:4
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR Name[1]
Definition: imports.h:144
static NTSTATUS control_ioctl(PIRP Irp)
Definition: devctrl.c:226
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
#define VCB_TYPE_VOLUME
Definition: btrfs_drv.h:666
NTSTATUS dev_ioctl(_In_ PDEVICE_OBJECT DeviceObject, _In_ ULONG ControlCode, _In_reads_bytes_opt_(InputBufferSize) PVOID InputBuffer, _In_ ULONG InputBufferSize, _Out_writes_bytes_opt_(OutputBufferSize) PVOID OutputBuffer, _In_ ULONG OutputBufferSize, _In_ bool Override, _Out_opt_ IO_STATUS_BLOCK *iosb)
Definition: btrfs.c:2813
#define Vcb
Definition: cdprocs.h:1425
static __inline void * map_user_buffer(PIRP Irp, ULONG priority)
Definition: btrfs_drv.h:952
uint32_t next_entry
Definition: btrfsioctl.h:188
#define __stdcall
Definition: typedefs.h:25
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1817
* PFILE_OBJECT
Definition: iotypes.h:1955
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
LIST_ENTRY VcbList
Definition: btrfs.c:73
void volume_arrival(PDRIVER_OBJECT DriverObject, PUNICODE_STRING devpath)
Definition: search.c:497
#define FILE_READ_ATTRIBUTES
Definition: nt_native.h:647
#define IOCTL_BTRFS_PROBE_VOLUME
Definition: btrfsioctl.h:28
VOID NTAPI IoSetTopLevelIrp(IN PIRP Irp)
Definition: irp.c:2000
Definition: typedefs.h:117
DRIVER_DISPATCH(nfs41_FsdDispatch)
BYTE uint8_t
Definition: msvideo1.c:66
#define IOCTL_DISK_UPDATE_PROPERTIES
Definition: winioctl.h:69
#define VCB_TYPE_CONTROL
Definition: btrfs_drv.h:665
_Function_class_(IRP_MJ_DEVICE_CONTROL)
Definition: devctrl.c:48
Status
Definition: gdiplustypes.h:24
#define ERR(fmt,...)
Definition: debug.h:109
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetCurrentIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2745
PFILE_OBJECT FileObject
Definition: iotypes.h:2813
#define SE_MANAGE_VOLUME_PRIVILEGE
Definition: security.c:682
NTSTATUS vol_device_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: volume.c:820
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
_In_ PIO_STACK_LOCATION IrpSp
Definition: create.c:4157
Definition: list.h:27
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
USHORT NameLength
Definition: imports.h:143
#define IoSkipCurrentIrpStackLocation(Irp)
Definition: ntifs_ex.h:421
BOOLEAN NTAPI ExAcquireResourceSharedLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:885
static NTSTATUS is_writable(device_extension *Vcb)
Definition: devctrl.c:43
UNICODE_STRING pnp_name
Definition: btrfs.c:4139
uint8_t uuid[16]
Definition: btrfsioctl.h:189
#define msg(x)
Definition: auth_time.c:54
static NTSTATUS ioctl_unload(PIRP Irp)
Definition: devctrl.c:215
Definition: name.c:38
ULONG ERESOURCE
Definition: env_spec_w32.h:594
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:566
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
PDRIVER_OBJECT drvobj
Definition: btrfs.c:66
static NTSTATUS query_filesystems(void *data, ULONG length)
Definition: devctrl.c:49
struct _NAMED_PIPE_CREATE_PARAMETERS * Parameters
Definition: iotypes.h:2772
#define uint32_t
Definition: nsiface.idl:61
return STATUS_SUCCESS
Definition: btrfs.c:2938
void do_shutdown(PIRP Irp)
Definition: btrfs.c:5212
btrfs_filesystem_device device
Definition: btrfsioctl.h:191
static NTSTATUS probe_volume(void *data, ULONG length, KPROCESSOR_MODE processor_mode)
Definition: devctrl.c:162
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
#define IRP_MJ_DEVICE_CONTROL
Definition: rdpdr.c:52
void volume_removal(PDRIVER_OBJECT DriverObject, PUNICODE_STRING devpath)
Definition: search.c:591
static NTSTATUS mountdev_query_stable_guid(device_extension *Vcb, PIRP Irp)
Definition: devctrl.c:27