ReactOS  0.4.14-dev-52-g6116262
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 
218 
219  switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
221  Status = query_filesystems(map_user_buffer(Irp, NormalPagePriority), IrpSp->Parameters.FileSystemControl.OutputBufferLength);
222  break;
223 
225  Status = probe_volume(Irp->AssociatedIrp.SystemBuffer, IrpSp->Parameters.FileSystemControl.InputBufferLength, Irp->RequestorMode);
226  break;
227 
228  default:
229  TRACE("unhandled ioctl %x\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
231  break;
232  }
233 
234  return Status;
235 }
236 
239 NTSTATUS __stdcall drv_device_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
243  bool top_level;
244 
246 
247  top_level = is_top_level(Irp);
248 
249  Irp->IoStatus.Information = 0;
250 
251  if (Vcb) {
252  if (Vcb->type == VCB_TYPE_CONTROL) {
254  goto end;
255  } else if (Vcb->type == VCB_TYPE_VOLUME) {
257  goto end;
258  } else if (Vcb->type != VCB_TYPE_FS) {
260  goto end;
261  }
262  } else {
264  goto end;
265  }
266 
267  if (!IrpSp->FileObject || IrpSp->FileObject->FsContext != Vcb->volume_fcb) {
269  goto end;
270  }
271 
272  switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
275  goto end;
276 
279  goto end;
280 
281  default:
282  TRACE("unhandled control code %x\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
283  break;
284  }
285 
287 
288  Status = IoCallDriver(Vcb->Vpb->RealDevice, Irp);
289 
290  goto end2;
291 
292 end:
293  Irp->IoStatus.Status = Status;
294 
295  if (Status != STATUS_PENDING)
297 
298 end2:
299  TRACE("returning %08x\n", Status);
300 
301  if (top_level)
303 
305 
306  return Status;
307 }
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 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 IOCTL_DISK_IS_WRITABLE
Definition: cdrw_usr.h:172
_Dispatch_type_(IRP_MJ_DEVICE_CONTROL)
Definition: devctrl.c:237
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:657
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:272
_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:189
void disk_arrival(PDRIVER_OBJECT DriverObject, PUNICODE_STRING devpath)
Definition: search.c:261
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:4134
#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:215
#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:659
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:2841
#define Vcb
Definition: cdprocs.h:1425
static __inline void * map_user_buffer(PIRP Irp, ULONG priority)
Definition: btrfs_drv.h:941
uint32_t next_entry
Definition: btrfsioctl.h:187
#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
#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:658
_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:797
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:61
volume_arrival(drvobj, &pnp_name)
_In_ PIO_STACK_LOCATION IrpSp
Definition: create.c:4157
Definition: services.c:325
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:4164
uint8_t uuid[16]
Definition: btrfsioctl.h:188
#define msg(x)
Definition: auth_time.c:54
Definition: name.c:36
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:2966
btrfs_filesystem_device device
Definition: btrfsioctl.h:190
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:575
static NTSTATUS mountdev_query_stable_guid(device_extension *Vcb, PIRP Irp)
Definition: devctrl.c:27