[BTRFS] Fix booting with runtime checks
[reactos.git] / drivers / filesystems / btrfs / btrfs.c
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 #ifdef _DEBUG
19 #define DEBUG
20 #endif
21
22 #include "btrfs_drv.h"
23 #ifndef __REACTOS__
24 #ifndef _MSC_VER
25 #include <cpuid.h>
26 #else
27 #include <intrin.h>
28 #endif
29 #endif
30 #include <ntddscsi.h>
31 #include "btrfs.h"
32 #include <ata.h>
33
34 #ifndef _MSC_VER
35 #include <initguid.h>
36 #include <ntddstor.h>
37 #undef INITGUID
38 #endif
39
40 #include <ntdddisk.h>
41 #include <ntddvol.h>
42
43 #ifdef _MSC_VER
44 #include <initguid.h>
45 #include <ntddstor.h>
46 #undef INITGUID
47 #endif
48
49 #define INCOMPAT_SUPPORTED (BTRFS_INCOMPAT_FLAGS_MIXED_BACKREF | BTRFS_INCOMPAT_FLAGS_DEFAULT_SUBVOL | BTRFS_INCOMPAT_FLAGS_MIXED_GROUPS | \
50 BTRFS_INCOMPAT_FLAGS_COMPRESS_LZO | BTRFS_INCOMPAT_FLAGS_BIG_METADATA | BTRFS_INCOMPAT_FLAGS_RAID56 | \
51 BTRFS_INCOMPAT_FLAGS_EXTENDED_IREF | BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA | BTRFS_INCOMPAT_FLAGS_NO_HOLES)
52 #define COMPAT_RO_SUPPORTED (BTRFS_COMPAT_RO_FLAGS_FREE_SPACE_CACHE | BTRFS_COMPAT_RO_FLAGS_FREE_SPACE_CACHE_VALID)
53
54 static WCHAR device_name[] = {'\\','B','t','r','f','s',0};
55 static WCHAR dosdevice_name[] = {'\\','D','o','s','D','e','v','i','c','e','s','\\','B','t','r','f','s',0};
56
57 DEFINE_GUID(BtrfsBusInterface, 0x4d414874, 0x6865, 0x6761, 0x6d, 0x65, 0x83, 0x69, 0x17, 0x9a, 0x7d, 0x1d);
58
59 PDRIVER_OBJECT drvobj;
60 PDEVICE_OBJECT master_devobj;
61 #ifndef __REACTOS__
62 BOOL have_sse42 = FALSE, have_sse2 = FALSE;
63 #endif
64 UINT64 num_reads = 0;
65 LIST_ENTRY uid_map_list, gid_map_list;
66 LIST_ENTRY VcbList;
67 ERESOURCE global_loading_lock;
68 UINT32 debug_log_level = 0;
69 UINT32 mount_compress = 0;
70 UINT32 mount_compress_force = 0;
71 UINT32 mount_compress_type = 0;
72 UINT32 mount_zlib_level = 3;
73 UINT32 mount_flush_interval = 30;
74 UINT32 mount_max_inline = 2048;
75 UINT32 mount_skip_balance = 0;
76 UINT32 mount_no_barrier = 0;
77 UINT32 mount_no_trim = 0;
78 UINT32 mount_clear_cache = 0;
79 UINT32 mount_allow_degraded = 0;
80 UINT32 mount_readonly = 0;
81 UINT32 no_pnp = 0;
82 BOOL log_started = FALSE;
83 UNICODE_STRING log_device, log_file, registry_path;
84 tPsUpdateDiskCounters fPsUpdateDiskCounters;
85 tCcCopyReadEx fCcCopyReadEx;
86 tCcCopyWriteEx fCcCopyWriteEx;
87 tCcSetAdditionalCacheAttributesEx fCcSetAdditionalCacheAttributesEx;
88 tFsRtlUpdateDiskCounters fFsRtlUpdateDiskCounters;
89 BOOL diskacc = FALSE;
90 void *notification_entry = NULL, *notification_entry2 = NULL, *notification_entry3 = NULL;
91 ERESOURCE pdo_list_lock, mapping_lock;
92 LIST_ENTRY pdo_list;
93 BOOL finished_probing = FALSE;
94 HANDLE degraded_wait_handle = NULL, mountmgr_thread_handle = NULL;
95 BOOL degraded_wait = TRUE;
96 KEVENT mountmgr_thread_event;
97 BOOL shutting_down = FALSE;
98
99 #ifdef _DEBUG
100 PFILE_OBJECT comfo = NULL;
101 PDEVICE_OBJECT comdo = NULL;
102 HANDLE log_handle = NULL;
103 ERESOURCE log_lock;
104 HANDLE serial_thread_handle = NULL;
105
106 static void init_serial(BOOL first_time);
107 #endif
108
109 static NTSTATUS close_file(_In_ PFILE_OBJECT FileObject, _In_ PIRP Irp);
110
111 typedef struct {
112 KEVENT Event;
113 IO_STATUS_BLOCK iosb;
114 } read_context;
115
116 #ifdef _DEBUG
117 _Function_class_(IO_COMPLETION_ROUTINE)
118 static NTSTATUS dbg_completion(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp, _In_ PVOID conptr) {
119 read_context* context = conptr;
120
121 UNUSED(DeviceObject);
122
123 context->iosb = Irp->IoStatus;
124 KeSetEvent(&context->Event, 0, FALSE);
125
126 return STATUS_MORE_PROCESSING_REQUIRED;
127 }
128
129 #ifdef DEBUG_LONG_MESSAGES
130 void _debug_message(_In_ const char* func, _In_ const char* file, _In_ unsigned int line, _In_ char* s, ...) {
131 #else
132 void _debug_message(_In_ const char* func, _In_ char* s, ...) {
133 #endif
134 LARGE_INTEGER offset;
135 PIO_STACK_LOCATION IrpSp;
136 NTSTATUS Status;
137 PIRP Irp;
138 va_list ap;
139 char *buf2, *buf;
140 read_context context;
141 UINT32 length;
142
143 buf2 = ExAllocatePoolWithTag(NonPagedPool, 1024, ALLOC_TAG);
144
145 if (!buf2) {
146 DbgPrint("Couldn't allocate buffer in debug_message\n");
147 return;
148 }
149
150 #ifdef DEBUG_LONG_MESSAGES
151 sprintf(buf2, "%p:%s:%s:%u:", PsGetCurrentThread(), func, file, line);
152 #else
153 sprintf(buf2, "%p:%s:", PsGetCurrentThread(), func);
154 #endif
155 buf = &buf2[strlen(buf2)];
156
157 va_start(ap, s);
158 vsprintf(buf, s, ap);
159
160 ExAcquireResourceSharedLite(&log_lock, TRUE);
161
162 if (!log_started || (log_device.Length == 0 && log_file.Length == 0)) {
163 DbgPrint(buf2);
164 } else if (log_device.Length > 0) {
165 if (!comdo) {
166 DbgPrint("comdo is NULL :-(\n");
167 DbgPrint(buf2);
168 goto exit2;
169 }
170
171 length = (UINT32)strlen(buf2);
172
173 offset.u.LowPart = 0;
174 offset.u.HighPart = 0;
175
176 RtlZeroMemory(&context, sizeof(read_context));
177
178 KeInitializeEvent(&context.Event, NotificationEvent, FALSE);
179
180 Irp = IoAllocateIrp(comdo->StackSize, FALSE);
181
182 if (!Irp) {
183 DbgPrint("IoAllocateIrp failed\n");
184 goto exit2;
185 }
186
187 IrpSp = IoGetNextIrpStackLocation(Irp);
188 IrpSp->MajorFunction = IRP_MJ_WRITE;
189
190 if (comdo->Flags & DO_BUFFERED_IO) {
191 Irp->AssociatedIrp.SystemBuffer = buf2;
192
193 Irp->Flags = IRP_BUFFERED_IO;
194 } else if (comdo->Flags & DO_DIRECT_IO) {
195 Irp->MdlAddress = IoAllocateMdl(buf2, length, FALSE, FALSE, NULL);
196 if (!Irp->MdlAddress) {
197 DbgPrint("IoAllocateMdl failed\n");
198 goto exit;
199 }
200
201 MmBuildMdlForNonPagedPool(Irp->MdlAddress);
202 } else {
203 Irp->UserBuffer = buf2;
204 }
205
206 IrpSp->Parameters.Write.Length = length;
207 IrpSp->Parameters.Write.ByteOffset = offset;
208
209 Irp->UserIosb = &context.iosb;
210
211 Irp->UserEvent = &context.Event;
212
213 IoSetCompletionRoutine(Irp, dbg_completion, &context, TRUE, TRUE, TRUE);
214
215 Status = IoCallDriver(comdo, Irp);
216
217 if (Status == STATUS_PENDING) {
218 KeWaitForSingleObject(&context.Event, Executive, KernelMode, FALSE, NULL);
219 Status = context.iosb.Status;
220 }
221
222 if (comdo->Flags & DO_DIRECT_IO)
223 IoFreeMdl(Irp->MdlAddress);
224
225 if (!NT_SUCCESS(Status)) {
226 DbgPrint("failed to write to COM1 - error %08x\n", Status);
227 goto exit;
228 }
229
230 exit:
231 IoFreeIrp(Irp);
232 } else if (log_handle != NULL) {
233 IO_STATUS_BLOCK iosb;
234
235 length = (UINT32)strlen(buf2);
236
237 Status = ZwWriteFile(log_handle, NULL, NULL, NULL, &iosb, buf2, length, NULL, NULL);
238
239 if (!NT_SUCCESS(Status)) {
240 DbgPrint("failed to write to file - error %08x\n", Status);
241 }
242 }
243
244 exit2:
245 ExReleaseResourceLite(&log_lock);
246
247 va_end(ap);
248
249 if (buf2)
250 ExFreePool(buf2);
251 }
252 #endif
253
254 BOOL is_top_level(_In_ PIRP Irp) {
255 if (!IoGetTopLevelIrp()) {
256 IoSetTopLevelIrp(Irp);
257 return TRUE;
258 }
259
260 return FALSE;
261 }
262
263 _Function_class_(DRIVER_UNLOAD)
264 #ifdef __REACTOS__
265 static void NTAPI DriverUnload(_In_ PDRIVER_OBJECT DriverObject) {
266 #else
267 static void DriverUnload(_In_ PDRIVER_OBJECT DriverObject) {
268 #endif
269 UNICODE_STRING dosdevice_nameW;
270
271 ERR("DriverUnload\n");
272
273 free_cache();
274
275 IoUnregisterFileSystem(DriverObject->DeviceObject);
276
277 if (notification_entry2)
278 #ifdef __REACTOS__
279 IoUnregisterPlugPlayNotification(notification_entry2);
280 #else
281 IoUnregisterPlugPlayNotificationEx(notification_entry2);
282 #endif
283
284 if (notification_entry3)
285 #ifdef __REACTOS__
286 IoUnregisterPlugPlayNotification(notification_entry3);
287 #else
288 IoUnregisterPlugPlayNotificationEx(notification_entry3);
289 #endif
290
291 if (notification_entry)
292 #ifdef __REACTOS__
293 IoUnregisterPlugPlayNotification(notification_entry);
294 #else
295 IoUnregisterPlugPlayNotificationEx(notification_entry);
296 #endif
297
298 dosdevice_nameW.Buffer = dosdevice_name;
299 dosdevice_nameW.Length = dosdevice_nameW.MaximumLength = (USHORT)wcslen(dosdevice_name) * sizeof(WCHAR);
300
301 IoDeleteSymbolicLink(&dosdevice_nameW);
302 IoDeleteDevice(DriverObject->DeviceObject);
303
304 while (!IsListEmpty(&uid_map_list)) {
305 LIST_ENTRY* le = RemoveHeadList(&uid_map_list);
306 uid_map* um = CONTAINING_RECORD(le, uid_map, listentry);
307
308 ExFreePool(um->sid);
309
310 ExFreePool(um);
311 }
312
313 while (!IsListEmpty(&gid_map_list)) {
314 gid_map* gm = CONTAINING_RECORD(RemoveHeadList(&gid_map_list), gid_map, listentry);
315
316 ExFreePool(gm->sid);
317 ExFreePool(gm);
318 }
319
320 // FIXME - free volumes and their devpaths
321
322 #ifdef _DEBUG
323 if (comfo)
324 ObDereferenceObject(comfo);
325
326 if (log_handle)
327 ZwClose(log_handle);
328 #endif
329
330 ExDeleteResourceLite(&global_loading_lock);
331 ExDeleteResourceLite(&pdo_list_lock);
332
333 if (log_device.Buffer)
334 ExFreePool(log_device.Buffer);
335
336 if (log_file.Buffer)
337 ExFreePool(log_file.Buffer);
338
339 if (registry_path.Buffer)
340 ExFreePool(registry_path.Buffer);
341
342 #ifdef _DEBUG
343 ExDeleteResourceLite(&log_lock);
344 #endif
345 ExDeleteResourceLite(&mapping_lock);
346 }
347
348 static BOOL get_last_inode(_In_ _Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, _In_ root* r, _In_opt_ PIRP Irp) {
349 KEY searchkey;
350 traverse_ptr tp, prev_tp;
351 NTSTATUS Status;
352
353 // get last entry
354 searchkey.obj_id = 0xffffffffffffffff;
355 searchkey.obj_type = 0xff;
356 searchkey.offset = 0xffffffffffffffff;
357
358 Status = find_item(Vcb, r, &tp, &searchkey, FALSE, Irp);
359 if (!NT_SUCCESS(Status)) {
360 ERR("error - find_item returned %08x\n", Status);
361 return FALSE;
362 }
363
364 if (tp.item->key.obj_type == TYPE_INODE_ITEM || (tp.item->key.obj_type == TYPE_ROOT_ITEM && !(tp.item->key.obj_id & 0x8000000000000000))) {
365 r->lastinode = tp.item->key.obj_id;
366 TRACE("last inode for tree %llx is %llx\n", r->id, r->lastinode);
367 return TRUE;
368 }
369
370 while (find_prev_item(Vcb, &tp, &prev_tp, Irp)) {
371 tp = prev_tp;
372
373 TRACE("moving on to %llx,%x,%llx\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
374
375 if (tp.item->key.obj_type == TYPE_INODE_ITEM || (tp.item->key.obj_type == TYPE_ROOT_ITEM && !(tp.item->key.obj_id & 0x8000000000000000))) {
376 r->lastinode = tp.item->key.obj_id;
377 TRACE("last inode for tree %llx is %llx\n", r->id, r->lastinode);
378 return TRUE;
379 }
380 }
381
382 r->lastinode = SUBVOL_ROOT_INODE;
383
384 WARN("no INODE_ITEMs in tree %llx\n", r->id);
385
386 return TRUE;
387 }
388
389 _Success_(return)
390 static BOOL extract_xattr(_In_reads_bytes_(size) void* item, _In_ USHORT size, _In_z_ char* name, _Out_ UINT8** data, _Out_ UINT16* datalen) {
391 DIR_ITEM* xa = (DIR_ITEM*)item;
392 USHORT xasize;
393
394 while (TRUE) {
395 if (size < sizeof(DIR_ITEM) || size < (sizeof(DIR_ITEM) - 1 + xa->m + xa->n)) {
396 WARN("DIR_ITEM is truncated\n");
397 return FALSE;
398 }
399
400 if (xa->n == strlen(name) && RtlCompareMemory(name, xa->name, xa->n) == xa->n) {
401 TRACE("found xattr %s\n", name);
402
403 *datalen = xa->m;
404
405 if (xa->m > 0) {
406 *data = ExAllocatePoolWithTag(PagedPool, xa->m, ALLOC_TAG);
407 if (!*data) {
408 ERR("out of memory\n");
409 return FALSE;
410 }
411
412 RtlCopyMemory(*data, &xa->name[xa->n], xa->m);
413 } else
414 *data = NULL;
415
416 return TRUE;
417 }
418
419 xasize = sizeof(DIR_ITEM) - 1 + xa->m + xa->n;
420
421 if (size > xasize) {
422 size -= xasize;
423 xa = (DIR_ITEM*)&xa->name[xa->m + xa->n];
424 } else
425 break;
426 }
427
428 TRACE("xattr %s not found\n", name);
429
430 return FALSE;
431 }
432
433 _Success_(return)
434 BOOL get_xattr(_In_ _Requires_lock_held_(_Curr_->tree_lock) device_extension* Vcb, _In_ root* subvol, _In_ UINT64 inode, _In_z_ char* name, _In_ UINT32 crc32,
435 _Out_ UINT8** data, _Out_ UINT16* datalen, _In_opt_ PIRP Irp) {
436 KEY searchkey;
437 traverse_ptr tp;
438 NTSTATUS Status;
439
440 TRACE("(%p, %llx, %llx, %s, %08x, %p, %p)\n", Vcb, subvol->id, inode, name, crc32, data, datalen);
441
442 searchkey.obj_id = inode;
443 searchkey.obj_type = TYPE_XATTR_ITEM;
444 searchkey.offset = crc32;
445
446 Status = find_item(Vcb, subvol, &tp, &searchkey, FALSE, Irp);
447 if (!NT_SUCCESS(Status)) {
448 ERR("error - find_item returned %08x\n", Status);
449 return FALSE;
450 }
451
452 if (keycmp(tp.item->key, searchkey)) {
453 TRACE("could not find item (%llx,%x,%llx)\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
454 return FALSE;
455 }
456
457 if (tp.item->size < sizeof(DIR_ITEM)) {
458 ERR("(%llx,%x,%llx) was %u bytes, expected at least %u\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(DIR_ITEM));
459 return FALSE;
460 }
461
462 return extract_xattr(tp.item->data, tp.item->size, name, data, datalen);
463 }
464
465 _Dispatch_type_(IRP_MJ_CLOSE)
466 _Function_class_(DRIVER_DISPATCH)
467 #ifdef __REACTOS__
468 static NTSTATUS NTAPI drv_close(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
469 #else
470 static NTSTATUS drv_close(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
471 #endif
472 NTSTATUS Status;
473 PIO_STACK_LOCATION IrpSp;
474 device_extension* Vcb = DeviceObject->DeviceExtension;
475 BOOL top_level;
476
477 FsRtlEnterFileSystem();
478
479 TRACE("close\n");
480
481 top_level = is_top_level(Irp);
482
483 if (DeviceObject == master_devobj) {
484 TRACE("Closing file system\n");
485 Status = STATUS_SUCCESS;
486 goto end;
487 } else if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
488 Status = vol_close(DeviceObject, Irp);
489 goto end;
490 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
491 Status = STATUS_INVALID_PARAMETER;
492 goto end;
493 }
494
495 IrpSp = IoGetCurrentIrpStackLocation(Irp);
496
497 // FIXME - unmount if called for volume
498 // FIXME - call FsRtlNotifyUninitializeSync(&Vcb->NotifySync) if unmounting
499
500 Status = close_file(IrpSp->FileObject, Irp);
501
502 end:
503 Irp->IoStatus.Status = Status;
504 Irp->IoStatus.Information = 0;
505
506 IoCompleteRequest( Irp, IO_DISK_INCREMENT );
507
508 if (top_level)
509 IoSetTopLevelIrp(NULL);
510
511 TRACE("returning %08x\n", Status);
512
513 FsRtlExitFileSystem();
514
515 return Status;
516 }
517
518 _Dispatch_type_(IRP_MJ_FLUSH_BUFFERS)
519 _Function_class_(DRIVER_DISPATCH)
520 #ifdef __REACTOS__
521 static NTSTATUS NTAPI drv_flush_buffers(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
522 #else
523 static NTSTATUS drv_flush_buffers(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
524 #endif
525 NTSTATUS Status;
526 PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
527 PFILE_OBJECT FileObject = IrpSp->FileObject;
528 fcb* fcb = FileObject->FsContext;
529 device_extension* Vcb = DeviceObject->DeviceExtension;
530 BOOL top_level;
531
532 FsRtlEnterFileSystem();
533
534 TRACE("flush buffers\n");
535
536 top_level = is_top_level(Irp);
537
538 if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
539 Status = vol_flush_buffers(DeviceObject, Irp);
540 goto end;
541 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
542 Status = STATUS_INVALID_PARAMETER;
543 goto end;
544 }
545
546 if (!fcb) {
547 ERR("fcb was NULL\n");
548 Status = STATUS_INVALID_PARAMETER;
549 goto end;
550 }
551
552 if (fcb == Vcb->volume_fcb) {
553 Status = STATUS_INVALID_PARAMETER;
554 goto end;
555 }
556
557 Irp->IoStatus.Information = 0;
558
559 fcb->Header.IsFastIoPossible = fast_io_possible(fcb);
560
561 Status = STATUS_SUCCESS;
562 Irp->IoStatus.Status = Status;
563
564 if (fcb->type != BTRFS_TYPE_DIRECTORY) {
565 CcFlushCache(&fcb->nonpaged->segment_object, NULL, 0, &Irp->IoStatus);
566
567 if (fcb->Header.PagingIoResource) {
568 ExAcquireResourceExclusiveLite(fcb->Header.PagingIoResource, TRUE);
569 ExReleaseResourceLite(fcb->Header.PagingIoResource);
570 }
571
572 Status = Irp->IoStatus.Status;
573 }
574
575 end:
576 IoCompleteRequest(Irp, IO_NO_INCREMENT);
577
578 TRACE("returning %08x\n", Status);
579
580 if (top_level)
581 IoSetTopLevelIrp(NULL);
582
583 FsRtlExitFileSystem();
584
585 return Status;
586 }
587
588 static void calculate_total_space(_In_ device_extension* Vcb, _Out_ UINT64* totalsize, _Out_ UINT64* freespace) {
589 UINT64 nfactor, dfactor, sectors_used;
590
591 if (Vcb->data_flags & BLOCK_FLAG_DUPLICATE || Vcb->data_flags & BLOCK_FLAG_RAID1 || Vcb->data_flags & BLOCK_FLAG_RAID10) {
592 nfactor = 1;
593 dfactor = 2;
594 } else if (Vcb->data_flags & BLOCK_FLAG_RAID5) {
595 nfactor = Vcb->superblock.num_devices - 1;
596 dfactor = Vcb->superblock.num_devices;
597 } else if (Vcb->data_flags & BLOCK_FLAG_RAID6) {
598 nfactor = Vcb->superblock.num_devices - 2;
599 dfactor = Vcb->superblock.num_devices;
600 } else {
601 nfactor = 1;
602 dfactor = 1;
603 }
604
605 sectors_used = Vcb->superblock.bytes_used / Vcb->superblock.sector_size;
606
607 *totalsize = (Vcb->superblock.total_bytes / Vcb->superblock.sector_size) * nfactor / dfactor;
608 *freespace = sectors_used > *totalsize ? 0 : (*totalsize - sectors_used);
609 }
610
611 #ifndef __REACTOS__
612 // This function exists because we have to lie about our FS type in certain situations.
613 // MPR!MprGetConnection queries the FS type, and compares it to a whitelist. If it doesn't match,
614 // it will return ERROR_NO_NET_OR_BAD_PATH, which prevents UAC from working.
615 // The command mklink refuses to create hard links on anything other than NTFS, so we have to
616 // blacklist cmd.exe too.
617
618 static BOOL lie_about_fs_type() {
619 NTSTATUS Status;
620 PROCESS_BASIC_INFORMATION pbi;
621 PPEB peb;
622 LIST_ENTRY* le;
623 ULONG retlen;
624
625 static WCHAR mpr[] = L"MPR.DLL";
626 static WCHAR cmd[] = L"CMD.EXE";
627 static WCHAR fsutil[] = L"FSUTIL.EXE";
628 UNICODE_STRING mprus, cmdus, fsutilus;
629
630 mprus.Buffer = mpr;
631 mprus.Length = mprus.MaximumLength = (USHORT)(wcslen(mpr) * sizeof(WCHAR));
632 cmdus.Buffer = cmd;
633 cmdus.Length = cmdus.MaximumLength = (USHORT)(wcslen(cmd) * sizeof(WCHAR));
634 fsutilus.Buffer = fsutil;
635 fsutilus.Length = fsutilus.MaximumLength = (USHORT)(wcslen(fsutil) * sizeof(WCHAR));
636
637 if (!PsGetCurrentProcess())
638 return FALSE;
639
640 Status = ZwQueryInformationProcess(NtCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), &retlen);
641
642 if (!NT_SUCCESS(Status)) {
643 ERR("ZwQueryInformationProcess returned %08x\n", Status);
644 return FALSE;
645 }
646
647 if (!pbi.PebBaseAddress)
648 return FALSE;
649
650 peb = pbi.PebBaseAddress;
651
652 if (!peb->Ldr)
653 return FALSE;
654
655 le = peb->Ldr->InMemoryOrderModuleList.Flink;
656 while (le != &peb->Ldr->InMemoryOrderModuleList) {
657 LDR_DATA_TABLE_ENTRY* entry = CONTAINING_RECORD(le, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
658 BOOL blacklist = FALSE;
659
660 if (entry->FullDllName.Length >= mprus.Length) {
661 UNICODE_STRING name;
662
663 name.Buffer = &entry->FullDllName.Buffer[(entry->FullDllName.Length - mprus.Length) / sizeof(WCHAR)];
664 name.Length = name.MaximumLength = mprus.Length;
665
666 blacklist = FsRtlAreNamesEqual(&name, &mprus, TRUE, NULL);
667 }
668
669 if (!blacklist && entry->FullDllName.Length >= cmdus.Length) {
670 UNICODE_STRING name;
671
672 name.Buffer = &entry->FullDllName.Buffer[(entry->FullDllName.Length - cmdus.Length) / sizeof(WCHAR)];
673 name.Length = name.MaximumLength = cmdus.Length;
674
675 blacklist = FsRtlAreNamesEqual(&name, &cmdus, TRUE, NULL);
676 }
677
678 if (!blacklist && entry->FullDllName.Length >= fsutilus.Length) {
679 UNICODE_STRING name;
680
681 name.Buffer = &entry->FullDllName.Buffer[(entry->FullDllName.Length - fsutilus.Length) / sizeof(WCHAR)];
682 name.Length = name.MaximumLength = fsutilus.Length;
683
684 blacklist = FsRtlAreNamesEqual(&name, &fsutilus, TRUE, NULL);
685 }
686
687 if (blacklist) {
688 void** frames;
689 ULONG i, num_frames;
690
691 frames = ExAllocatePoolWithTag(PagedPool, 256 * sizeof(void*), ALLOC_TAG);
692 if (!frames) {
693 ERR("out of memory\n");
694 return FALSE;
695 }
696
697 num_frames = RtlWalkFrameChain(frames, 256, 1);
698
699 for (i = 0; i < num_frames; i++) {
700 // entry->Reserved3[1] appears to be the image size
701 if (frames[i] >= entry->DllBase && (ULONG_PTR)frames[i] <= (ULONG_PTR)entry->DllBase + (ULONG_PTR)entry->Reserved3[1]) {
702 ExFreePool(frames);
703 return TRUE;
704 }
705 }
706
707 ExFreePool(frames);
708 }
709
710 le = le->Flink;
711 }
712
713 return FALSE;
714 }
715 #endif
716
717 _Dispatch_type_(IRP_MJ_QUERY_VOLUME_INFORMATION)
718 _Function_class_(DRIVER_DISPATCH)
719 #ifdef __REACTOS__
720 static NTSTATUS NTAPI drv_query_volume_information(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
721 #else
722 static NTSTATUS drv_query_volume_information(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
723 #endif
724 PIO_STACK_LOCATION IrpSp;
725 NTSTATUS Status;
726 ULONG BytesCopied = 0;
727 device_extension* Vcb = DeviceObject->DeviceExtension;
728 BOOL top_level;
729
730 FsRtlEnterFileSystem();
731
732 TRACE("query volume information\n");
733 top_level = is_top_level(Irp);
734
735 if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
736 Status = vol_query_volume_information(DeviceObject, Irp);
737 goto end;
738 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
739 Status = STATUS_INVALID_PARAMETER;
740 goto end;
741 }
742
743 IrpSp = IoGetCurrentIrpStackLocation(Irp);
744
745 Status = STATUS_NOT_IMPLEMENTED;
746
747 switch (IrpSp->Parameters.QueryVolume.FsInformationClass) {
748 case FileFsAttributeInformation:
749 {
750 FILE_FS_ATTRIBUTE_INFORMATION* data = Irp->AssociatedIrp.SystemBuffer;
751 BOOL overflow = FALSE;
752 #ifndef __REACTOS__
753 WCHAR* fs_name = (Irp->RequestorMode == UserMode && lie_about_fs_type()) ? L"NTFS" : L"Btrfs";
754 ULONG fs_name_len = (ULONG)wcslen(fs_name) * sizeof(WCHAR);
755 #else
756 WCHAR* fs_name = L"Btrfs";
757 ULONG fs_name_len = 5 * sizeof(WCHAR);
758 #endif
759 ULONG orig_fs_name_len = fs_name_len;
760
761 TRACE("FileFsAttributeInformation\n");
762
763 if (IrpSp->Parameters.QueryVolume.Length < sizeof(FILE_FS_ATTRIBUTE_INFORMATION) - sizeof(WCHAR) + fs_name_len) {
764 if (IrpSp->Parameters.QueryVolume.Length > sizeof(FILE_FS_ATTRIBUTE_INFORMATION) - sizeof(WCHAR))
765 fs_name_len = IrpSp->Parameters.QueryVolume.Length - sizeof(FILE_FS_ATTRIBUTE_INFORMATION) + sizeof(WCHAR);
766 else
767 fs_name_len = 0;
768
769 overflow = TRUE;
770 }
771
772 data->FileSystemAttributes = FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH |
773 FILE_UNICODE_ON_DISK | FILE_NAMED_STREAMS | FILE_SUPPORTS_HARD_LINKS | FILE_PERSISTENT_ACLS |
774 FILE_SUPPORTS_REPARSE_POINTS | FILE_SUPPORTS_SPARSE_FILES | FILE_SUPPORTS_OBJECT_IDS |
775 FILE_SUPPORTS_OPEN_BY_FILE_ID | FILE_SUPPORTS_EXTENDED_ATTRIBUTES | FILE_SUPPORTS_BLOCK_REFCOUNTING;
776 if (Vcb->readonly)
777 data->FileSystemAttributes |= FILE_READ_ONLY_VOLUME;
778
779 // should also be FILE_FILE_COMPRESSION when supported
780 data->MaximumComponentNameLength = 255; // FIXME - check
781 data->FileSystemNameLength = orig_fs_name_len;
782 RtlCopyMemory(data->FileSystemName, fs_name, fs_name_len);
783
784 BytesCopied = sizeof(FILE_FS_ATTRIBUTE_INFORMATION) - sizeof(WCHAR) + fs_name_len;
785 Status = overflow ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
786 break;
787 }
788
789 case FileFsDeviceInformation:
790 {
791 FILE_FS_DEVICE_INFORMATION* ffdi = Irp->AssociatedIrp.SystemBuffer;
792
793 TRACE("FileFsDeviceInformation\n");
794
795 ffdi->DeviceType = FILE_DEVICE_DISK;
796
797 ExAcquireResourceSharedLite(&Vcb->tree_lock, TRUE);
798 ffdi->Characteristics = Vcb->Vpb->RealDevice->Characteristics;
799 ExReleaseResourceLite(&Vcb->tree_lock);
800
801 if (Vcb->readonly)
802 ffdi->Characteristics |= FILE_READ_ONLY_DEVICE;
803 else
804 ffdi->Characteristics &= ~FILE_READ_ONLY_DEVICE;
805
806 BytesCopied = sizeof(FILE_FS_DEVICE_INFORMATION);
807 Status = STATUS_SUCCESS;
808
809 break;
810 }
811
812 case FileFsFullSizeInformation:
813 {
814 FILE_FS_FULL_SIZE_INFORMATION* ffsi = Irp->AssociatedIrp.SystemBuffer;
815
816 TRACE("FileFsFullSizeInformation\n");
817
818 calculate_total_space(Vcb, (UINT64*)&ffsi->TotalAllocationUnits.QuadPart, (UINT64*)&ffsi->ActualAvailableAllocationUnits.QuadPart);
819 ffsi->CallerAvailableAllocationUnits.QuadPart = ffsi->ActualAvailableAllocationUnits.QuadPart;
820 ffsi->SectorsPerAllocationUnit = 1;
821 ffsi->BytesPerSector = Vcb->superblock.sector_size;
822
823 BytesCopied = sizeof(FILE_FS_FULL_SIZE_INFORMATION);
824 Status = STATUS_SUCCESS;
825
826 break;
827 }
828
829 case FileFsObjectIdInformation:
830 {
831 FILE_FS_OBJECTID_INFORMATION* ffoi = Irp->AssociatedIrp.SystemBuffer;
832
833 TRACE("FileFsObjectIdInformation\n");
834
835 RtlCopyMemory(ffoi->ObjectId, &Vcb->superblock.uuid.uuid[0], sizeof(UCHAR) * 16);
836 RtlZeroMemory(ffoi->ExtendedInfo, sizeof(ffoi->ExtendedInfo));
837
838 BytesCopied = sizeof(FILE_FS_OBJECTID_INFORMATION);
839 Status = STATUS_SUCCESS;
840
841 break;
842 }
843
844 case FileFsSizeInformation:
845 {
846 FILE_FS_SIZE_INFORMATION* ffsi = Irp->AssociatedIrp.SystemBuffer;
847
848 TRACE("FileFsSizeInformation\n");
849
850 calculate_total_space(Vcb, (UINT64*)&ffsi->TotalAllocationUnits.QuadPart, (UINT64*)&ffsi->AvailableAllocationUnits.QuadPart);
851 ffsi->SectorsPerAllocationUnit = 1;
852 ffsi->BytesPerSector = Vcb->superblock.sector_size;
853
854 BytesCopied = sizeof(FILE_FS_SIZE_INFORMATION);
855 Status = STATUS_SUCCESS;
856
857 break;
858 }
859
860 case FileFsVolumeInformation:
861 {
862 FILE_FS_VOLUME_INFORMATION* data = Irp->AssociatedIrp.SystemBuffer;
863 FILE_FS_VOLUME_INFORMATION ffvi;
864 BOOL overflow = FALSE;
865 ULONG label_len, orig_label_len;
866
867 TRACE("FileFsVolumeInformation\n");
868 TRACE("max length = %u\n", IrpSp->Parameters.QueryVolume.Length);
869
870 ExAcquireResourceSharedLite(&Vcb->tree_lock, TRUE);
871
872 Status = RtlUTF8ToUnicodeN(NULL, 0, &label_len, Vcb->superblock.label, (ULONG)strlen(Vcb->superblock.label));
873 if (!NT_SUCCESS(Status)) {
874 ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
875 ExReleaseResourceLite(&Vcb->tree_lock);
876 break;
877 }
878
879 orig_label_len = label_len;
880
881 if (IrpSp->Parameters.QueryVolume.Length < sizeof(FILE_FS_VOLUME_INFORMATION) - sizeof(WCHAR) + label_len) {
882 if (IrpSp->Parameters.QueryVolume.Length > sizeof(FILE_FS_VOLUME_INFORMATION) - sizeof(WCHAR))
883 label_len = IrpSp->Parameters.QueryVolume.Length - sizeof(FILE_FS_VOLUME_INFORMATION) + sizeof(WCHAR);
884 else
885 label_len = 0;
886
887 overflow = TRUE;
888 }
889
890 TRACE("label_len = %u\n", label_len);
891
892 ffvi.VolumeCreationTime.QuadPart = 0; // FIXME
893 ffvi.VolumeSerialNumber = Vcb->superblock.uuid.uuid[12] << 24 | Vcb->superblock.uuid.uuid[13] << 16 | Vcb->superblock.uuid.uuid[14] << 8 | Vcb->superblock.uuid.uuid[15];
894 ffvi.VolumeLabelLength = orig_label_len;
895 ffvi.SupportsObjects = FALSE;
896
897 RtlCopyMemory(data, &ffvi, min(sizeof(FILE_FS_VOLUME_INFORMATION) - sizeof(WCHAR), IrpSp->Parameters.QueryVolume.Length));
898
899 if (label_len > 0) {
900 ULONG bytecount;
901
902 Status = RtlUTF8ToUnicodeN(&data->VolumeLabel[0], label_len, &bytecount, Vcb->superblock.label, (ULONG)strlen(Vcb->superblock.label));
903 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL) {
904 ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
905 ExReleaseResourceLite(&Vcb->tree_lock);
906 break;
907 }
908
909 TRACE("label = %.*S\n", label_len / sizeof(WCHAR), data->VolumeLabel);
910 }
911
912 ExReleaseResourceLite(&Vcb->tree_lock);
913
914 BytesCopied = sizeof(FILE_FS_VOLUME_INFORMATION) - sizeof(WCHAR) + label_len;
915 Status = overflow ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
916 break;
917 }
918
919 #ifndef __REACTOS__
920 #ifdef _MSC_VER // not in mingw yet
921 case FileFsSectorSizeInformation:
922 {
923 FILE_FS_SECTOR_SIZE_INFORMATION* data = Irp->AssociatedIrp.SystemBuffer;
924
925 data->LogicalBytesPerSector = Vcb->superblock.sector_size;
926 data->PhysicalBytesPerSectorForAtomicity = Vcb->superblock.sector_size;
927 data->PhysicalBytesPerSectorForPerformance = Vcb->superblock.sector_size;
928 data->FileSystemEffectivePhysicalBytesPerSectorForAtomicity = Vcb->superblock.sector_size;
929 data->ByteOffsetForSectorAlignment = 0;
930 data->ByteOffsetForPartitionAlignment = 0;
931
932 data->Flags = SSINFO_FLAGS_ALIGNED_DEVICE | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE;
933
934 if (Vcb->trim && !Vcb->options.no_trim)
935 data->Flags |= SSINFO_FLAGS_TRIM_ENABLED;
936
937 BytesCopied = sizeof(FILE_FS_SECTOR_SIZE_INFORMATION);
938
939 break;
940 }
941 #endif
942 #endif /* __REACTOS__ */
943
944 default:
945 Status = STATUS_INVALID_PARAMETER;
946 WARN("unknown FsInformationClass %u\n", IrpSp->Parameters.QueryVolume.FsInformationClass);
947 break;
948 }
949
950 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_OVERFLOW)
951 Irp->IoStatus.Information = 0;
952 else
953 Irp->IoStatus.Information = BytesCopied;
954
955 end:
956 Irp->IoStatus.Status = Status;
957
958 IoCompleteRequest( Irp, IO_DISK_INCREMENT );
959
960 if (top_level)
961 IoSetTopLevelIrp(NULL);
962
963 TRACE("query volume information returning %08x\n", Status);
964
965 FsRtlExitFileSystem();
966
967 return Status;
968 }
969
970 _Function_class_(IO_COMPLETION_ROUTINE)
971 #ifdef __REACTOS__
972 static NTSTATUS NTAPI read_completion(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp, _In_ PVOID conptr) {
973 #else
974 static NTSTATUS read_completion(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp, _In_ PVOID conptr) {
975 #endif
976 read_context* context = conptr;
977
978 UNUSED(DeviceObject);
979
980 context->iosb = Irp->IoStatus;
981 KeSetEvent(&context->Event, 0, FALSE);
982
983 return STATUS_MORE_PROCESSING_REQUIRED;
984 }
985
986 NTSTATUS create_root(_In_ _Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, _In_ UINT64 id,
987 _Out_ root** rootptr, _In_ BOOL no_tree, _In_ UINT64 offset, _In_opt_ PIRP Irp) {
988 NTSTATUS Status;
989 root* r;
990 tree* t = NULL;
991 ROOT_ITEM* ri;
992 traverse_ptr tp;
993
994 r = ExAllocatePoolWithTag(PagedPool, sizeof(root), ALLOC_TAG);
995 if (!r) {
996 ERR("out of memory\n");
997 return STATUS_INSUFFICIENT_RESOURCES;
998 }
999
1000 r->nonpaged = ExAllocatePoolWithTag(NonPagedPool, sizeof(root_nonpaged), ALLOC_TAG);
1001 if (!r->nonpaged) {
1002 ERR("out of memory\n");
1003 ExFreePool(r);
1004 return STATUS_INSUFFICIENT_RESOURCES;
1005 }
1006
1007 if (!no_tree) {
1008 t = ExAllocatePoolWithTag(PagedPool, sizeof(tree), ALLOC_TAG);
1009 if (!t) {
1010 ERR("out of memory\n");
1011 ExFreePool(r->nonpaged);
1012 ExFreePool(r);
1013 return STATUS_INSUFFICIENT_RESOURCES;
1014 }
1015
1016 t->is_unique = TRUE;
1017 t->uniqueness_determined = TRUE;
1018 t->buf = NULL;
1019 }
1020
1021 ri = ExAllocatePoolWithTag(PagedPool, sizeof(ROOT_ITEM), ALLOC_TAG);
1022 if (!ri) {
1023 ERR("out of memory\n");
1024
1025 if (t)
1026 ExFreePool(t);
1027
1028 ExFreePool(r->nonpaged);
1029 ExFreePool(r);
1030 return STATUS_INSUFFICIENT_RESOURCES;
1031 }
1032
1033 r->id = id;
1034 r->treeholder.address = 0;
1035 r->treeholder.generation = Vcb->superblock.generation;
1036 r->treeholder.tree = t;
1037 r->lastinode = 0;
1038 r->dirty = FALSE;
1039 r->received = FALSE;
1040 r->reserved = NULL;
1041 r->parent = 0;
1042 r->send_ops = 0;
1043 RtlZeroMemory(&r->root_item, sizeof(ROOT_ITEM));
1044 r->root_item.num_references = 1;
1045 InitializeListHead(&r->fcbs);
1046
1047 RtlCopyMemory(ri, &r->root_item, sizeof(ROOT_ITEM));
1048
1049 // We ask here for a traverse_ptr to the item we're inserting, so we can
1050 // copy some of the tree's variables
1051
1052 Status = insert_tree_item(Vcb, Vcb->root_root, id, TYPE_ROOT_ITEM, offset, ri, sizeof(ROOT_ITEM), &tp, Irp);
1053 if (!NT_SUCCESS(Status)) {
1054 ERR("insert_tree_item returned %08x\n", Status);
1055 ExFreePool(ri);
1056
1057 if (t)
1058 ExFreePool(t);
1059
1060 ExFreePool(r->nonpaged);
1061 ExFreePool(r);
1062 return Status;
1063 }
1064
1065 ExInitializeResourceLite(&r->nonpaged->load_tree_lock);
1066
1067 InsertTailList(&Vcb->roots, &r->list_entry);
1068
1069 if (!no_tree) {
1070 RtlZeroMemory(&t->header, sizeof(tree_header));
1071 t->header.fs_uuid = tp.tree->header.fs_uuid;
1072 t->header.address = 0;
1073 t->header.flags = HEADER_FLAG_MIXED_BACKREF | 1; // 1 == "written"? Why does the Linux driver record this?
1074 t->header.chunk_tree_uuid = tp.tree->header.chunk_tree_uuid;
1075 t->header.generation = Vcb->superblock.generation;
1076 t->header.tree_id = id;
1077 t->header.num_items = 0;
1078 t->header.level = 0;
1079
1080 t->has_address = FALSE;
1081 t->size = 0;
1082 t->Vcb = Vcb;
1083 t->parent = NULL;
1084 t->paritem = NULL;
1085 t->root = r;
1086
1087 InitializeListHead(&t->itemlist);
1088
1089 t->new_address = 0;
1090 t->has_new_address = FALSE;
1091 t->updated_extents = FALSE;
1092
1093 InsertTailList(&Vcb->trees, &t->list_entry);
1094 t->list_entry_hash.Flink = NULL;
1095
1096 t->write = TRUE;
1097 Vcb->need_write = TRUE;
1098 }
1099
1100 *rootptr = r;
1101
1102 return STATUS_SUCCESS;
1103 }
1104
1105 static NTSTATUS set_label(_In_ device_extension* Vcb, _In_ FILE_FS_LABEL_INFORMATION* ffli) {
1106 ULONG utf8len;
1107 NTSTATUS Status;
1108 ULONG vollen, i;
1109
1110 TRACE("label = %.*S\n", ffli->VolumeLabelLength / sizeof(WCHAR), ffli->VolumeLabel);
1111
1112 vollen = ffli->VolumeLabelLength;
1113
1114 for (i = 0; i < ffli->VolumeLabelLength / sizeof(WCHAR); i++) {
1115 if (ffli->VolumeLabel[i] == 0) {
1116 vollen = i * sizeof(WCHAR);
1117 break;
1118 } else if (ffli->VolumeLabel[i] == '/' || ffli->VolumeLabel[i] == '\\') {
1119 Status = STATUS_INVALID_VOLUME_LABEL;
1120 goto end;
1121 }
1122 }
1123
1124 if (vollen == 0) {
1125 utf8len = 0;
1126 } else {
1127 Status = RtlUnicodeToUTF8N(NULL, 0, &utf8len, ffli->VolumeLabel, vollen);
1128 if (!NT_SUCCESS(Status))
1129 goto end;
1130
1131 if (utf8len > MAX_LABEL_SIZE) {
1132 Status = STATUS_INVALID_VOLUME_LABEL;
1133 goto end;
1134 }
1135 }
1136
1137 ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
1138
1139 if (utf8len > 0) {
1140 Status = RtlUnicodeToUTF8N((PCHAR)&Vcb->superblock.label, MAX_LABEL_SIZE, &utf8len, ffli->VolumeLabel, vollen);
1141 if (!NT_SUCCESS(Status))
1142 goto release;
1143 } else
1144 Status = STATUS_SUCCESS;
1145
1146 if (utf8len < MAX_LABEL_SIZE)
1147 RtlZeroMemory(Vcb->superblock.label + utf8len, MAX_LABEL_SIZE - utf8len);
1148
1149 Vcb->need_write = TRUE;
1150
1151 release:
1152 ExReleaseResourceLite(&Vcb->tree_lock);
1153
1154 end:
1155 TRACE("returning %08x\n", Status);
1156
1157 return Status;
1158 }
1159
1160 _Dispatch_type_(IRP_MJ_SET_VOLUME_INFORMATION)
1161 _Function_class_(DRIVER_DISPATCH)
1162 #ifdef __REACTOS__
1163 static NTSTATUS NTAPI drv_set_volume_information(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
1164 #else
1165 static NTSTATUS drv_set_volume_information(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
1166 #endif
1167 PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
1168 device_extension* Vcb = DeviceObject->DeviceExtension;
1169 NTSTATUS Status;
1170 BOOL top_level;
1171
1172 FsRtlEnterFileSystem();
1173
1174 TRACE("set volume information\n");
1175
1176 top_level = is_top_level(Irp);
1177
1178 if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
1179 Status = vol_set_volume_information(DeviceObject, Irp);
1180 goto end;
1181 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
1182 Status = STATUS_INVALID_PARAMETER;
1183 goto end;
1184 }
1185
1186 Status = STATUS_NOT_IMPLEMENTED;
1187
1188 if (Vcb->readonly) {
1189 Status = STATUS_MEDIA_WRITE_PROTECTED;
1190 goto end;
1191 }
1192
1193 if (Vcb->removing || Vcb->locked) {
1194 Status = STATUS_ACCESS_DENIED;
1195 goto end;
1196 }
1197
1198 switch (IrpSp->Parameters.SetVolume.FsInformationClass) {
1199 case FileFsControlInformation:
1200 FIXME("STUB: FileFsControlInformation\n");
1201 break;
1202
1203 case FileFsLabelInformation:
1204 TRACE("FileFsLabelInformation\n");
1205
1206 Status = set_label(Vcb, Irp->AssociatedIrp.SystemBuffer);
1207 break;
1208
1209 case FileFsObjectIdInformation:
1210 FIXME("STUB: FileFsObjectIdInformation\n");
1211 break;
1212
1213 default:
1214 WARN("Unrecognized FsInformationClass 0x%x\n", IrpSp->Parameters.SetVolume.FsInformationClass);
1215 break;
1216 }
1217
1218 end:
1219 Irp->IoStatus.Status = Status;
1220 Irp->IoStatus.Information = 0;
1221
1222 TRACE("returning %08x\n", Status);
1223
1224 IoCompleteRequest( Irp, IO_NO_INCREMENT );
1225
1226 if (top_level)
1227 IoSetTopLevelIrp(NULL);
1228
1229 FsRtlExitFileSystem();
1230
1231 return Status;
1232 }
1233
1234 static WCHAR* file_desc_fcb(_In_ fcb* fcb) {
1235 char s[60];
1236 NTSTATUS Status;
1237 UNICODE_STRING us;
1238 ANSI_STRING as;
1239
1240 if (fcb->debug_desc)
1241 return fcb->debug_desc;
1242
1243 if (fcb == fcb->Vcb->volume_fcb)
1244 return L"volume FCB";
1245
1246 fcb->debug_desc = ExAllocatePoolWithTag(PagedPool, 60 * sizeof(WCHAR), ALLOC_TAG);
1247 if (!fcb->debug_desc)
1248 return L"(memory error)";
1249
1250 // I know this is pretty hackish...
1251 // GCC doesn't like %llx in sprintf, and MSVC won't let us use swprintf
1252 // without the CRT, which breaks drivers.
1253
1254 sprintf(s, "subvol %x, inode %x", (UINT32)fcb->subvol->id, (UINT32)fcb->inode);
1255
1256 as.Buffer = s;
1257 as.Length = as.MaximumLength = (USHORT)strlen(s);
1258
1259 us.Buffer = fcb->debug_desc;
1260 us.MaximumLength = 60 * sizeof(WCHAR);
1261 us.Length = 0;
1262
1263 Status = RtlAnsiStringToUnicodeString(&us, &as, FALSE);
1264 if (!NT_SUCCESS(Status))
1265 return L"(RtlAnsiStringToUnicodeString error)";
1266
1267 us.Buffer[us.Length / sizeof(WCHAR)] = 0;
1268
1269 return fcb->debug_desc;
1270 }
1271
1272 WCHAR* file_desc_fileref(_In_ file_ref* fileref) {
1273 NTSTATUS Status;
1274 UNICODE_STRING fn;
1275 ULONG reqlen;
1276
1277 if (fileref->debug_desc)
1278 return fileref->debug_desc;
1279
1280 fn.Length = fn.MaximumLength = 0;
1281 Status = fileref_get_filename(fileref, &fn, NULL, &reqlen);
1282 if (Status != STATUS_BUFFER_OVERFLOW)
1283 return L"ERROR";
1284
1285 if (reqlen > 0xffff - sizeof(WCHAR))
1286 return L"(too long)";
1287
1288 fileref->debug_desc = ExAllocatePoolWithTag(PagedPool, reqlen + sizeof(WCHAR), ALLOC_TAG);
1289 if (!fileref->debug_desc)
1290 return L"(memory error)";
1291
1292 fn.Buffer = fileref->debug_desc;
1293 fn.Length = 0;
1294 fn.MaximumLength = (USHORT)(reqlen + sizeof(WCHAR));
1295
1296 Status = fileref_get_filename(fileref, &fn, NULL, &reqlen);
1297 if (!NT_SUCCESS(Status)) {
1298 ExFreePool(fileref->debug_desc);
1299 fileref->debug_desc = NULL;
1300 return L"ERROR";
1301 }
1302
1303 fileref->debug_desc[fn.Length / sizeof(WCHAR)] = 0;
1304
1305 return fileref->debug_desc;
1306 }
1307
1308 _Ret_z_
1309 WCHAR* file_desc(_In_ PFILE_OBJECT FileObject) {
1310 fcb* fcb = FileObject->FsContext;
1311 ccb* ccb = FileObject->FsContext2;
1312 file_ref* fileref = ccb ? ccb->fileref : NULL;
1313
1314 if (fileref)
1315 return file_desc_fileref(fileref);
1316 else
1317 return file_desc_fcb(fcb);
1318 }
1319
1320 void send_notification_fileref(_In_ file_ref* fileref, _In_ ULONG filter_match, _In_ ULONG action, _In_opt_ PUNICODE_STRING stream) {
1321 UNICODE_STRING fn;
1322 NTSTATUS Status;
1323 ULONG reqlen;
1324 USHORT name_offset;
1325 fcb* fcb = fileref->fcb;
1326
1327 fn.Length = fn.MaximumLength = 0;
1328 Status = fileref_get_filename(fileref, &fn, NULL, &reqlen);
1329 if (Status != STATUS_BUFFER_OVERFLOW) {
1330 ERR("fileref_get_filename returned %08x\n", Status);
1331 return;
1332 }
1333
1334 if (reqlen > 0xffff) {
1335 WARN("reqlen was too long for FsRtlNotifyFilterReportChange\n");
1336 return;
1337 }
1338
1339 fn.Buffer = ExAllocatePoolWithTag(PagedPool, reqlen, ALLOC_TAG);
1340 if (!fn.Buffer) {
1341 ERR("out of memory\n");
1342 return;
1343 }
1344
1345 fn.MaximumLength = (USHORT)reqlen;
1346 fn.Length = 0;
1347
1348 Status = fileref_get_filename(fileref, &fn, &name_offset, &reqlen);
1349 if (!NT_SUCCESS(Status)) {
1350 ERR("fileref_get_filename returned %08x\n", Status);
1351 ExFreePool(fn.Buffer);
1352 return;
1353 }
1354
1355 FsRtlNotifyFilterReportChange(fcb->Vcb->NotifySync, &fcb->Vcb->DirNotifyList, (PSTRING)&fn, name_offset,
1356 (PSTRING)stream, NULL, filter_match, action, NULL, NULL);
1357 ExFreePool(fn.Buffer);
1358 }
1359
1360 void send_notification_fcb(_In_ file_ref* fileref, _In_ ULONG filter_match, _In_ ULONG action, _In_opt_ PUNICODE_STRING stream) {
1361 fcb* fcb = fileref->fcb;
1362 LIST_ENTRY* le;
1363 NTSTATUS Status;
1364
1365 // no point looking for hardlinks if st_nlink == 1
1366 if (fileref->fcb->inode_item.st_nlink == 1) {
1367 send_notification_fileref(fileref, filter_match, action, stream);
1368 return;
1369 }
1370
1371 acquire_fcb_lock_exclusive(fcb->Vcb);
1372
1373 le = fcb->hardlinks.Flink;
1374 while (le != &fcb->hardlinks) {
1375 hardlink* hl = CONTAINING_RECORD(le, hardlink, list_entry);
1376 file_ref* parfr;
1377
1378 Status = open_fileref_by_inode(fcb->Vcb, fcb->subvol, hl->parent, &parfr, NULL);
1379
1380 if (!NT_SUCCESS(Status))
1381 ERR("open_fileref_by_inode returned %08x\n", Status);
1382 else if (!parfr->deleted) {
1383 UNICODE_STRING fn;
1384 ULONG pathlen;
1385
1386 fn.Length = fn.MaximumLength = 0;
1387 Status = fileref_get_filename(parfr, &fn, NULL, &pathlen);
1388 if (Status != STATUS_BUFFER_OVERFLOW) {
1389 ERR("fileref_get_filename returned %08x\n", Status);
1390 free_fileref(fcb->Vcb, parfr);
1391 break;
1392 }
1393
1394 if (parfr != fcb->Vcb->root_fileref)
1395 pathlen += sizeof(WCHAR);
1396
1397 if (pathlen + hl->name.Length > 0xffff) {
1398 WARN("pathlen + hl->name.Length was too long for FsRtlNotifyFilterReportChange\n");
1399 free_fileref(fcb->Vcb, parfr);
1400 break;
1401 }
1402
1403 fn.MaximumLength = (USHORT)(pathlen + hl->name.Length);
1404 fn.Buffer = ExAllocatePoolWithTag(PagedPool, fn.MaximumLength, ALLOC_TAG);
1405 if (!fn.Buffer) {
1406 ERR("out of memory\n");
1407 free_fileref(fcb->Vcb, parfr);
1408 break;
1409 }
1410
1411 Status = fileref_get_filename(parfr, &fn, NULL, NULL);
1412 if (!NT_SUCCESS(Status)) {
1413 ERR("fileref_get_filename returned %08x\n", Status);
1414 free_fileref(fcb->Vcb, parfr);
1415 ExFreePool(fn.Buffer);
1416 break;
1417 }
1418
1419 if (parfr != fcb->Vcb->root_fileref) {
1420 fn.Buffer[(pathlen / sizeof(WCHAR)) - 1] = '\\';
1421 fn.Length += sizeof(WCHAR);
1422 }
1423
1424 RtlCopyMemory(&fn.Buffer[pathlen / sizeof(WCHAR)], hl->name.Buffer, hl->name.Length);
1425 fn.Length += hl->name.Length;
1426
1427 FsRtlNotifyFilterReportChange(fcb->Vcb->NotifySync, &fcb->Vcb->DirNotifyList, (PSTRING)&fn, (USHORT)pathlen,
1428 (PSTRING)stream, NULL, filter_match, action, NULL, NULL);
1429
1430 ExFreePool(fn.Buffer);
1431
1432 free_fileref(fcb->Vcb, parfr);
1433 }
1434
1435 le = le->Flink;
1436 }
1437
1438 release_fcb_lock(fcb->Vcb);
1439 }
1440
1441 void mark_fcb_dirty(_In_ fcb* fcb) {
1442 if (!fcb->dirty) {
1443 #ifdef DEBUG_FCB_REFCOUNTS
1444 LONG rc;
1445 #endif
1446 fcb->dirty = TRUE;
1447
1448 #ifdef DEBUG_FCB_REFCOUNTS
1449 rc = InterlockedIncrement(&fcb->refcount);
1450 WARN("fcb %p: refcount now %i\n", fcb, rc);
1451 #else
1452 InterlockedIncrement(&fcb->refcount);
1453 #endif
1454
1455 ExAcquireResourceExclusiveLite(&fcb->Vcb->dirty_fcbs_lock, TRUE);
1456 InsertTailList(&fcb->Vcb->dirty_fcbs, &fcb->list_entry_dirty);
1457 ExReleaseResourceLite(&fcb->Vcb->dirty_fcbs_lock);
1458 }
1459
1460 fcb->Vcb->need_write = TRUE;
1461 }
1462
1463 void mark_fileref_dirty(_In_ file_ref* fileref) {
1464 if (!fileref->dirty) {
1465 fileref->dirty = TRUE;
1466 increase_fileref_refcount(fileref);
1467
1468 ExAcquireResourceExclusiveLite(&fileref->fcb->Vcb->dirty_filerefs_lock, TRUE);
1469 InsertTailList(&fileref->fcb->Vcb->dirty_filerefs, &fileref->list_entry_dirty);
1470 ExReleaseResourceLite(&fileref->fcb->Vcb->dirty_filerefs_lock);
1471 }
1472
1473 fileref->fcb->Vcb->need_write = TRUE;
1474 }
1475
1476 #ifdef DEBUG_FCB_REFCOUNTS
1477 void _free_fcb(_Requires_exclusive_lock_held_(_Curr_->fcb_lock) _In_ device_extension* Vcb, _Inout_ fcb* fcb, _In_ const char* func) {
1478 #else
1479 void free_fcb(_Requires_exclusive_lock_held_(_Curr_->fcb_lock) _In_ device_extension* Vcb, _Inout_ fcb* fcb) {
1480 #endif
1481 LONG rc;
1482
1483 rc = InterlockedDecrement(&fcb->refcount);
1484
1485 #ifdef DEBUG_FCB_REFCOUNTS
1486 #ifdef DEBUG_LONG_MESSAGES
1487 ERR("fcb %p: refcount now %i (subvol %llx, inode %llx)\n", fcb, rc, fcb->subvol ? fcb->subvol->id : 0, fcb->inode);
1488 #else
1489 ERR("fcb %p: refcount now %i (subvol %llx, inode %llx)\n", fcb, rc, fcb->subvol ? fcb->subvol->id : 0, fcb->inode);
1490 #endif
1491 #endif
1492
1493 if (rc > 0)
1494 return;
1495
1496 if (fcb->list_entry.Flink)
1497 RemoveEntryList(&fcb->list_entry);
1498
1499 if (fcb->list_entry_all.Flink)
1500 RemoveEntryList(&fcb->list_entry_all);
1501
1502 ExDeleteResourceLite(&fcb->nonpaged->resource);
1503 ExDeleteResourceLite(&fcb->nonpaged->paging_resource);
1504 ExDeleteResourceLite(&fcb->nonpaged->dir_children_lock);
1505
1506 ExFreeToNPagedLookasideList(&Vcb->fcb_np_lookaside, fcb->nonpaged);
1507
1508 if (fcb->sd)
1509 ExFreePool(fcb->sd);
1510
1511 if (fcb->adsxattr.Buffer)
1512 ExFreePool(fcb->adsxattr.Buffer);
1513
1514 if (fcb->reparse_xattr.Buffer)
1515 ExFreePool(fcb->reparse_xattr.Buffer);
1516
1517 if (fcb->ea_xattr.Buffer)
1518 ExFreePool(fcb->ea_xattr.Buffer);
1519
1520 if (fcb->adsdata.Buffer)
1521 ExFreePool(fcb->adsdata.Buffer);
1522
1523 if (fcb->debug_desc)
1524 ExFreePool(fcb->debug_desc);
1525
1526 while (!IsListEmpty(&fcb->extents)) {
1527 LIST_ENTRY* le = RemoveHeadList(&fcb->extents);
1528 extent* ext = CONTAINING_RECORD(le, extent, list_entry);
1529
1530 if (ext->csum)
1531 ExFreePool(ext->csum);
1532
1533 ExFreePool(ext);
1534 }
1535
1536 while (!IsListEmpty(&fcb->hardlinks)) {
1537 LIST_ENTRY* le = RemoveHeadList(&fcb->hardlinks);
1538 hardlink* hl = CONTAINING_RECORD(le, hardlink, list_entry);
1539
1540 if (hl->name.Buffer)
1541 ExFreePool(hl->name.Buffer);
1542
1543 if (hl->utf8.Buffer)
1544 ExFreePool(hl->utf8.Buffer);
1545
1546 ExFreePool(hl);
1547 }
1548
1549 while (!IsListEmpty(&fcb->xattrs)) {
1550 xattr* xa = CONTAINING_RECORD(RemoveHeadList(&fcb->xattrs), xattr, list_entry);
1551
1552 ExFreePool(xa);
1553 }
1554
1555 while (!IsListEmpty(&fcb->dir_children_index)) {
1556 LIST_ENTRY* le = RemoveHeadList(&fcb->dir_children_index);
1557 dir_child* dc = CONTAINING_RECORD(le, dir_child, list_entry_index);
1558
1559 ExFreePool(dc->utf8.Buffer);
1560 ExFreePool(dc->name.Buffer);
1561 ExFreePool(dc->name_uc.Buffer);
1562 ExFreePool(dc);
1563 }
1564
1565 if (fcb->hash_ptrs)
1566 ExFreePool(fcb->hash_ptrs);
1567
1568 if (fcb->hash_ptrs_uc)
1569 ExFreePool(fcb->hash_ptrs_uc);
1570
1571 FsRtlUninitializeFileLock(&fcb->lock);
1572
1573 if (fcb->pool_type == NonPagedPool)
1574 ExFreePool(fcb);
1575 else
1576 ExFreeToPagedLookasideList(&Vcb->fcb_lookaside, fcb);
1577
1578 #ifdef DEBUG_FCB_REFCOUNTS
1579 #ifdef DEBUG_LONG_MESSAGES
1580 _debug_message(func, file, line, "freeing fcb %p\n", fcb);
1581 #else
1582 _debug_message(func, "freeing fcb %p\n", fcb);
1583 #endif
1584 #endif
1585 }
1586
1587 void free_fileref(_Requires_exclusive_lock_held_(_Curr_->fcb_lock) _In_ device_extension* Vcb, _Inout_ file_ref* fr) {
1588 LONG rc;
1589
1590 rc = InterlockedDecrement(&fr->refcount);
1591
1592 #ifdef DEBUG_FCB_REFCOUNTS
1593 ERR("fileref %p: refcount now %i\n", fr, rc);
1594 #endif
1595
1596 #ifdef _DEBUG
1597 if (rc < 0) {
1598 ERR("fileref %p: refcount now %i\n", fr, rc);
1599 int3;
1600 }
1601 #endif
1602
1603 if (rc > 0)
1604 return;
1605
1606 if (fr->parent)
1607 ExAcquireResourceExclusiveLite(&fr->parent->nonpaged->children_lock, TRUE);
1608
1609 // FIXME - do we need a file_ref lock?
1610
1611 // FIXME - do delete if needed
1612
1613 if (fr->debug_desc)
1614 ExFreePool(fr->debug_desc);
1615
1616 ExDeleteResourceLite(&fr->nonpaged->children_lock);
1617 ExDeleteResourceLite(&fr->nonpaged->fileref_lock);
1618
1619 ExFreeToNPagedLookasideList(&Vcb->fileref_np_lookaside, fr->nonpaged);
1620
1621 // FIXME - throw error if children not empty
1622
1623 if (fr->fcb->fileref == fr)
1624 fr->fcb->fileref = NULL;
1625
1626 if (fr->dc) {
1627 if (fr->fcb->ads)
1628 fr->dc->size = fr->fcb->adsdata.Length;
1629
1630 fr->dc->fileref = NULL;
1631 }
1632
1633 if (fr->list_entry.Flink)
1634 RemoveEntryList(&fr->list_entry);
1635
1636 if (fr->parent) {
1637 ExReleaseResourceLite(&fr->parent->nonpaged->children_lock);
1638 free_fileref(Vcb, fr->parent);
1639 }
1640
1641 free_fcb(Vcb, fr->fcb);
1642
1643 ExFreeToPagedLookasideList(&Vcb->fileref_lookaside, fr);
1644 }
1645
1646 static NTSTATUS close_file(_In_ PFILE_OBJECT FileObject, _In_ PIRP Irp) {
1647 fcb* fcb;
1648 ccb* ccb;
1649 file_ref* fileref = NULL;
1650 LONG open_files;
1651 device_extension* Vcb;
1652
1653 UNUSED(Irp);
1654
1655 TRACE("FileObject = %p\n", FileObject);
1656
1657 fcb = FileObject->FsContext;
1658 if (!fcb) {
1659 TRACE("FCB was NULL, returning success\n");
1660 return STATUS_SUCCESS;
1661 }
1662
1663 open_files = InterlockedDecrement(&fcb->Vcb->open_files);
1664
1665 ccb = FileObject->FsContext2;
1666
1667 TRACE("close called for %S (fcb == %p)\n", file_desc(FileObject), fcb);
1668
1669 // FIXME - make sure notification gets sent if file is being deleted
1670
1671 if (ccb) {
1672 if (ccb->query_string.Buffer)
1673 RtlFreeUnicodeString(&ccb->query_string);
1674
1675 if (ccb->filename.Buffer)
1676 ExFreePool(ccb->filename.Buffer);
1677
1678 // FIXME - use refcounts for fileref
1679 fileref = ccb->fileref;
1680
1681 if (fcb->Vcb->running_sends > 0) {
1682 BOOL send_cancelled = FALSE;
1683
1684 ExAcquireResourceExclusiveLite(&fcb->Vcb->send_load_lock, TRUE);
1685
1686 if (ccb->send) {
1687 ccb->send->cancelling = TRUE;
1688 send_cancelled = TRUE;
1689 KeSetEvent(&ccb->send->cleared_event, 0, FALSE);
1690 }
1691
1692 ExReleaseResourceLite(&fcb->Vcb->send_load_lock);
1693
1694 if (send_cancelled) {
1695 while (ccb->send) {
1696 ExAcquireResourceExclusiveLite(&fcb->Vcb->send_load_lock, TRUE);
1697 ExReleaseResourceLite(&fcb->Vcb->send_load_lock);
1698 }
1699 }
1700 }
1701
1702 ExFreePool(ccb);
1703 }
1704
1705 CcUninitializeCacheMap(FileObject, NULL, NULL);
1706
1707 if (open_files == 0 && fcb->Vcb->removing) {
1708 uninit(fcb->Vcb, FALSE);
1709 return STATUS_SUCCESS;
1710 }
1711
1712 if (!(fcb->Vcb->Vpb->Flags & VPB_MOUNTED))
1713 return STATUS_SUCCESS;
1714
1715 Vcb = fcb->Vcb;
1716
1717 acquire_fcb_lock_exclusive(Vcb);
1718
1719 if (fileref)
1720 free_fileref(fcb->Vcb, fileref);
1721 else
1722 free_fcb(Vcb, fcb);
1723
1724 release_fcb_lock(Vcb);
1725
1726 return STATUS_SUCCESS;
1727 }
1728
1729 void uninit(_In_ device_extension* Vcb, _In_ BOOL flush) {
1730 UINT64 i;
1731 NTSTATUS Status;
1732 LIST_ENTRY* le;
1733 LARGE_INTEGER time;
1734
1735 if (!Vcb->removing) {
1736 ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
1737 Vcb->removing = TRUE;
1738 ExReleaseResourceLite(&Vcb->tree_lock);
1739 }
1740
1741 RemoveEntryList(&Vcb->list_entry);
1742
1743 if (Vcb->balance.thread) {
1744 Vcb->balance.paused = FALSE;
1745 Vcb->balance.stopping = TRUE;
1746 KeSetEvent(&Vcb->balance.event, 0, FALSE);
1747 KeWaitForSingleObject(&Vcb->balance.finished, Executive, KernelMode, FALSE, NULL);
1748 }
1749
1750 if (Vcb->scrub.thread) {
1751 Vcb->scrub.paused = FALSE;
1752 Vcb->scrub.stopping = TRUE;
1753 KeSetEvent(&Vcb->scrub.event, 0, FALSE);
1754 KeWaitForSingleObject(&Vcb->scrub.finished, Executive, KernelMode, FALSE, NULL);
1755 }
1756
1757 if (Vcb->running_sends != 0) {
1758 BOOL send_cancelled = FALSE;
1759
1760 ExAcquireResourceExclusiveLite(&Vcb->send_load_lock, TRUE);
1761
1762 le = Vcb->send_ops.Flink;
1763 while (le != &Vcb->send_ops) {
1764 send_info* send = CONTAINING_RECORD(le, send_info, list_entry);
1765
1766 if (!send->cancelling) {
1767 send->cancelling = TRUE;
1768 send_cancelled = TRUE;
1769 send->ccb = NULL;
1770 KeSetEvent(&send->cleared_event, 0, FALSE);
1771 }
1772
1773 le = le->Flink;
1774 }
1775
1776 ExReleaseResourceLite(&Vcb->send_load_lock);
1777
1778 if (send_cancelled) {
1779 while (Vcb->running_sends != 0) {
1780 ExAcquireResourceExclusiveLite(&Vcb->send_load_lock, TRUE);
1781 ExReleaseResourceLite(&Vcb->send_load_lock);
1782 }
1783 }
1784 }
1785
1786 Status = registry_mark_volume_unmounted(&Vcb->superblock.uuid);
1787 if (!NT_SUCCESS(Status) && Status != STATUS_TOO_LATE)
1788 WARN("registry_mark_volume_unmounted returned %08x\n", Status);
1789
1790 if (flush) {
1791 ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
1792
1793 if (Vcb->need_write && !Vcb->readonly) {
1794 Status = do_write(Vcb, NULL);
1795 if (!NT_SUCCESS(Status))
1796 ERR("do_write returned %08x\n", Status);
1797 }
1798
1799 free_trees(Vcb);
1800
1801 ExReleaseResourceLite(&Vcb->tree_lock);
1802 }
1803
1804 for (i = 0; i < Vcb->calcthreads.num_threads; i++) {
1805 Vcb->calcthreads.threads[i].quit = TRUE;
1806 }
1807
1808 KeSetEvent(&Vcb->calcthreads.event, 0, FALSE);
1809
1810 for (i = 0; i < Vcb->calcthreads.num_threads; i++) {
1811 KeWaitForSingleObject(&Vcb->calcthreads.threads[i].finished, Executive, KernelMode, FALSE, NULL);
1812
1813 ZwClose(Vcb->calcthreads.threads[i].handle);
1814 }
1815
1816 ExDeleteResourceLite(&Vcb->calcthreads.lock);
1817 ExFreePool(Vcb->calcthreads.threads);
1818
1819 time.QuadPart = 0;
1820 KeSetTimer(&Vcb->flush_thread_timer, time, NULL); // trigger the timer early
1821 KeWaitForSingleObject(&Vcb->flush_thread_finished, Executive, KernelMode, FALSE, NULL);
1822
1823 acquire_fcb_lock_exclusive(Vcb);
1824 free_fcb(Vcb, Vcb->volume_fcb);
1825 free_fcb(Vcb, Vcb->dummy_fcb);
1826 release_fcb_lock(Vcb);
1827
1828 if (Vcb->root_file)
1829 ObDereferenceObject(Vcb->root_file);
1830
1831 le = Vcb->chunks.Flink;
1832 while (le != &Vcb->chunks) {
1833 chunk* c = CONTAINING_RECORD(le, chunk, list_entry);
1834
1835 if (c->cache) {
1836 acquire_fcb_lock_exclusive(Vcb);
1837 free_fcb(Vcb, c->cache);
1838 release_fcb_lock(Vcb);
1839 c->cache = NULL;
1840 }
1841
1842 le = le->Flink;
1843 }
1844
1845 while (!IsListEmpty(&Vcb->roots)) {
1846 root* r = CONTAINING_RECORD(RemoveHeadList(&Vcb->roots), root, list_entry);
1847
1848 ExDeleteResourceLite(&r->nonpaged->load_tree_lock);
1849 ExFreePool(r->nonpaged);
1850 ExFreePool(r);
1851 }
1852
1853 while (!IsListEmpty(&Vcb->chunks)) {
1854 chunk* c = CONTAINING_RECORD(RemoveHeadList(&Vcb->chunks), chunk, list_entry);
1855
1856 while (!IsListEmpty(&c->space)) {
1857 LIST_ENTRY* le2 = RemoveHeadList(&c->space);
1858 space* s = CONTAINING_RECORD(le2, space, list_entry);
1859
1860 ExFreePool(s);
1861 }
1862
1863 while (!IsListEmpty(&c->deleting)) {
1864 LIST_ENTRY* le2 = RemoveHeadList(&c->deleting);
1865 space* s = CONTAINING_RECORD(le2, space, list_entry);
1866
1867 ExFreePool(s);
1868 }
1869
1870 if (c->devices)
1871 ExFreePool(c->devices);
1872
1873 if (c->cache) {
1874 acquire_fcb_lock_exclusive(Vcb);
1875 free_fcb(Vcb, c->cache);
1876 release_fcb_lock(Vcb);
1877 }
1878
1879 ExDeleteResourceLite(&c->range_locks_lock);
1880 ExDeleteResourceLite(&c->partial_stripes_lock);
1881 ExDeleteResourceLite(&c->lock);
1882 ExDeleteResourceLite(&c->changed_extents_lock);
1883
1884 ExFreePool(c->chunk_item);
1885 ExFreePool(c);
1886 }
1887
1888 // FIXME - free any open fcbs?
1889
1890 while (!IsListEmpty(&Vcb->devices)) {
1891 device* dev = CONTAINING_RECORD(RemoveHeadList(&Vcb->devices), device, list_entry);
1892
1893 while (!IsListEmpty(&dev->space)) {
1894 LIST_ENTRY* le2 = RemoveHeadList(&dev->space);
1895 space* s = CONTAINING_RECORD(le2, space, list_entry);
1896
1897 ExFreePool(s);
1898 }
1899
1900 ExFreePool(dev);
1901 }
1902
1903 ExAcquireResourceExclusiveLite(&Vcb->scrub.stats_lock, TRUE);
1904 while (!IsListEmpty(&Vcb->scrub.errors)) {
1905 scrub_error* err = CONTAINING_RECORD(RemoveHeadList(&Vcb->scrub.errors), scrub_error, list_entry);
1906
1907 ExFreePool(err);
1908 }
1909 ExReleaseResourceLite(&Vcb->scrub.stats_lock);
1910
1911 ExDeleteResourceLite(&Vcb->fcb_lock);
1912 ExDeleteResourceLite(&Vcb->load_lock);
1913 ExDeleteResourceLite(&Vcb->tree_lock);
1914 ExDeleteResourceLite(&Vcb->chunk_lock);
1915 ExDeleteResourceLite(&Vcb->dirty_fcbs_lock);
1916 ExDeleteResourceLite(&Vcb->dirty_filerefs_lock);
1917 ExDeleteResourceLite(&Vcb->dirty_subvols_lock);
1918 ExDeleteResourceLite(&Vcb->scrub.stats_lock);
1919 ExDeleteResourceLite(&Vcb->send_load_lock);
1920
1921 ExDeletePagedLookasideList(&Vcb->tree_data_lookaside);
1922 ExDeletePagedLookasideList(&Vcb->traverse_ptr_lookaside);
1923 ExDeletePagedLookasideList(&Vcb->batch_item_lookaside);
1924 ExDeletePagedLookasideList(&Vcb->fileref_lookaside);
1925 ExDeletePagedLookasideList(&Vcb->fcb_lookaside);
1926 ExDeletePagedLookasideList(&Vcb->name_bit_lookaside);
1927 ExDeleteNPagedLookasideList(&Vcb->range_lock_lookaside);
1928 ExDeleteNPagedLookasideList(&Vcb->fileref_np_lookaside);
1929 ExDeleteNPagedLookasideList(&Vcb->fcb_np_lookaside);
1930
1931 ZwClose(Vcb->flush_thread_handle);
1932 }
1933
1934 NTSTATUS delete_fileref(_In_ file_ref* fileref, _In_opt_ PFILE_OBJECT FileObject, _In_opt_ PIRP Irp, _In_ LIST_ENTRY* rollback) {
1935 LARGE_INTEGER newlength, time;
1936 BTRFS_TIME now;
1937 NTSTATUS Status;
1938 ULONG utf8len = 0;
1939
1940 KeQuerySystemTime(&time);
1941 win_time_to_unix(time, &now);
1942
1943 ExAcquireResourceExclusiveLite(fileref->fcb->Header.Resource, TRUE);
1944
1945 if (fileref->deleted) {
1946 ExReleaseResourceLite(fileref->fcb->Header.Resource);
1947 return STATUS_SUCCESS;
1948 }
1949
1950 if (fileref->fcb->subvol->send_ops > 0) {
1951 ExReleaseResourceLite(fileref->fcb->Header.Resource);
1952 return STATUS_ACCESS_DENIED;
1953 }
1954
1955 fileref->deleted = TRUE;
1956 mark_fileref_dirty(fileref);
1957
1958 // delete INODE_ITEM (0x1)
1959
1960 TRACE("nlink = %u\n", fileref->fcb->inode_item.st_nlink);
1961
1962 if (!fileref->fcb->ads) {
1963 if (fileref->parent->fcb->subvol == fileref->fcb->subvol) {
1964 LIST_ENTRY* le;
1965
1966 mark_fcb_dirty(fileref->fcb);
1967
1968 fileref->fcb->inode_item_changed = TRUE;
1969
1970 if (fileref->fcb->inode_item.st_nlink > 1) {
1971 fileref->fcb->inode_item.st_nlink--;
1972 fileref->fcb->inode_item.transid = fileref->fcb->Vcb->superblock.generation;
1973 fileref->fcb->inode_item.sequence++;
1974 fileref->fcb->inode_item.st_ctime = now;
1975 } else {
1976 // excise extents
1977
1978 if (fileref->fcb->type != BTRFS_TYPE_DIRECTORY && fileref->fcb->inode_item.st_size > 0) {
1979 Status = excise_extents(fileref->fcb->Vcb, fileref->fcb, 0, sector_align(fileref->fcb->inode_item.st_size, fileref->fcb->Vcb->superblock.sector_size), Irp, rollback);
1980 if (!NT_SUCCESS(Status)) {
1981 ERR("excise_extents returned %08x\n", Status);
1982 ExReleaseResourceLite(fileref->fcb->Header.Resource);
1983 return Status;
1984 }
1985 }
1986
1987 fileref->fcb->Header.AllocationSize.QuadPart = 0;
1988 fileref->fcb->Header.FileSize.QuadPart = 0;
1989 fileref->fcb->Header.ValidDataLength.QuadPart = 0;
1990
1991 if (FileObject) {
1992 CC_FILE_SIZES ccfs;
1993
1994 ccfs.AllocationSize = fileref->fcb->Header.AllocationSize;
1995 ccfs.FileSize = fileref->fcb->Header.FileSize;
1996 ccfs.ValidDataLength = fileref->fcb->Header.ValidDataLength;
1997
1998 Status = STATUS_SUCCESS;
1999
2000 _SEH2_TRY {
2001 CcSetFileSizes(FileObject, &ccfs);
2002 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
2003 Status = _SEH2_GetExceptionCode();
2004 } _SEH2_END;
2005
2006 if (!NT_SUCCESS(Status)) {
2007 ERR("CcSetFileSizes threw exception %08x\n", Status);
2008 ExReleaseResourceLite(fileref->fcb->Header.Resource);
2009 return Status;
2010 }
2011 }
2012
2013 fileref->fcb->deleted = TRUE;
2014
2015 le = fileref->children.Flink;
2016 while (le != &fileref->children) {
2017 file_ref* fr2 = CONTAINING_RECORD(le, file_ref, list_entry);
2018
2019 if (fr2->fcb->ads) {
2020 fr2->fcb->deleted = TRUE;
2021 mark_fcb_dirty(fr2->fcb);
2022 }
2023
2024 le = le->Flink;
2025 }
2026 }
2027
2028 if (fileref->dc) {
2029 le = fileref->fcb->hardlinks.Flink;
2030 while (le != &fileref->fcb->hardlinks) {
2031 hardlink* hl = CONTAINING_RECORD(le, hardlink, list_entry);
2032
2033 if (hl->parent == fileref->parent->fcb->inode && hl->index == fileref->dc->index) {
2034 RemoveEntryList(&hl->list_entry);
2035
2036 if (hl->name.Buffer)
2037 ExFreePool(hl->name.Buffer);
2038
2039 if (hl->utf8.Buffer)
2040 ExFreePool(hl->utf8.Buffer);
2041
2042 ExFreePool(hl);
2043 break;
2044 }
2045
2046 le = le->Flink;
2047 }
2048 }
2049 } else if (fileref->fcb->subvol->parent == fileref->parent->fcb->subvol->id) { // valid subvolume
2050 if (fileref->fcb->subvol->root_item.num_references > 1) {
2051 fileref->fcb->subvol->root_item.num_references--;
2052
2053 mark_fcb_dirty(fileref->fcb); // so ROOT_ITEM gets updated
2054 } else {
2055 LIST_ENTRY* le;
2056
2057 // FIXME - we need a lock here
2058
2059 RemoveEntryList(&fileref->fcb->subvol->list_entry);
2060
2061 InsertTailList(&fileref->fcb->Vcb->drop_roots, &fileref->fcb->subvol->list_entry);
2062
2063 le = fileref->children.Flink;
2064 while (le != &fileref->children) {
2065 file_ref* fr2 = CONTAINING_RECORD(le, file_ref, list_entry);
2066
2067 if (fr2->fcb->ads) {
2068 fr2->fcb->deleted = TRUE;
2069 mark_fcb_dirty(fr2->fcb);
2070 }
2071
2072 le = le->Flink;
2073 }
2074 }
2075 }
2076 } else {
2077 fileref->fcb->deleted = TRUE;
2078 mark_fcb_dirty(fileref->fcb);
2079 }
2080
2081 // remove dir_child from parent
2082
2083 if (fileref->dc) {
2084 TRACE("delete file %.*S\n", fileref->dc->name.Length / sizeof(WCHAR), fileref->dc->name.Buffer);
2085
2086 ExAcquireResourceExclusiveLite(&fileref->parent->fcb->nonpaged->dir_children_lock, TRUE);
2087 RemoveEntryList(&fileref->dc->list_entry_index);
2088
2089 if (!fileref->fcb->ads)
2090 remove_dir_child_from_hash_lists(fileref->parent->fcb, fileref->dc);
2091
2092 ExReleaseResourceLite(&fileref->parent->fcb->nonpaged->dir_children_lock);
2093
2094 if (!fileref->oldutf8.Buffer)
2095 fileref->oldutf8 = fileref->dc->utf8;
2096 else
2097 ExFreePool(fileref->dc->utf8.Buffer);
2098
2099 utf8len = fileref->dc->utf8.Length;
2100
2101 fileref->oldindex = fileref->dc->index;
2102
2103 ExFreePool(fileref->dc->name.Buffer);
2104 ExFreePool(fileref->dc->name_uc.Buffer);
2105 ExFreePool(fileref->dc);
2106
2107 fileref->dc = NULL;
2108 }
2109
2110 // update INODE_ITEM of parent
2111
2112 ExAcquireResourceExclusiveLite(fileref->parent->fcb->Header.Resource, TRUE);
2113
2114 fileref->parent->fcb->inode_item.transid = fileref->fcb->Vcb->superblock.generation;
2115 fileref->parent->fcb->inode_item.sequence++;
2116 fileref->parent->fcb->inode_item.st_ctime = now;
2117
2118 if (!fileref->fcb->ads) {
2119 TRACE("fileref->parent->fcb->inode_item.st_size (inode %llx) was %llx\n", fileref->parent->fcb->inode, fileref->parent->fcb->inode_item.st_size);
2120 fileref->parent->fcb->inode_item.st_size -= utf8len * 2;
2121 TRACE("fileref->parent->fcb->inode_item.st_size (inode %llx) now %llx\n", fileref->parent->fcb->inode, fileref->parent->fcb->inode_item.st_size);
2122 fileref->parent->fcb->inode_item.st_mtime = now;
2123 }
2124
2125 fileref->parent->fcb->inode_item_changed = TRUE;
2126 ExReleaseResourceLite(fileref->parent->fcb->Header.Resource);
2127
2128 if (!fileref->fcb->ads && fileref->parent->dc)
2129 send_notification_fcb(fileref->parent, FILE_NOTIFY_CHANGE_LAST_WRITE, FILE_ACTION_MODIFIED, NULL);
2130
2131 mark_fcb_dirty(fileref->parent->fcb);
2132
2133 fileref->fcb->subvol->root_item.ctransid = fileref->fcb->Vcb->superblock.generation;
2134 fileref->fcb->subvol->root_item.ctime = now;
2135
2136 newlength.QuadPart = 0;
2137
2138 if (FileObject && !CcUninitializeCacheMap(FileObject, &newlength, NULL))
2139 TRACE("CcUninitializeCacheMap failed\n");
2140
2141 ExReleaseResourceLite(fileref->fcb->Header.Resource);
2142
2143 return STATUS_SUCCESS;
2144 }
2145
2146 _Dispatch_type_(IRP_MJ_CLEANUP)
2147 _Function_class_(DRIVER_DISPATCH)
2148 #ifdef __REACTOS__
2149 static NTSTATUS NTAPI drv_cleanup(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
2150 #else
2151 static NTSTATUS drv_cleanup(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
2152 #endif
2153 NTSTATUS Status;
2154 PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
2155 PFILE_OBJECT FileObject = IrpSp->FileObject;
2156 device_extension* Vcb = DeviceObject->DeviceExtension;
2157 fcb* fcb = FileObject->FsContext;
2158 BOOL top_level;
2159
2160 FsRtlEnterFileSystem();
2161
2162 TRACE("cleanup\n");
2163
2164 top_level = is_top_level(Irp);
2165
2166 if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
2167 Status = vol_cleanup(DeviceObject, Irp);
2168 goto exit;
2169 } else if (DeviceObject == master_devobj) {
2170 TRACE("closing file system\n");
2171 Status = STATUS_SUCCESS;
2172 goto exit;
2173 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
2174 Status = STATUS_INVALID_PARAMETER;
2175 goto exit;
2176 }
2177
2178 if (FileObject->Flags & FO_CLEANUP_COMPLETE) {
2179 TRACE("FileObject %p already cleaned up\n", FileObject);
2180 Status = STATUS_SUCCESS;
2181 goto exit;
2182 }
2183
2184 if (!fcb) {
2185 ERR("fcb was NULL\n");
2186 Status = STATUS_INVALID_PARAMETER;
2187 goto exit;
2188 }
2189
2190 // We have to use the pointer to Vcb stored in the fcb, as we can receive cleanup
2191 // messages belonging to other devices.
2192
2193 if (FileObject && FileObject->FsContext) {
2194 LONG oc;
2195 ccb* ccb;
2196 file_ref* fileref;
2197 BOOL locked = TRUE;
2198
2199 ccb = FileObject->FsContext2;
2200 fileref = ccb ? ccb->fileref : NULL;
2201
2202 TRACE("cleanup called for FileObject %p\n", FileObject);
2203 TRACE("fileref %p (%S), refcount = %u, open_count = %u\n", fileref, file_desc(FileObject), fileref ? fileref->refcount : 0, fileref ? fileref->open_count : 0);
2204
2205 ExAcquireResourceSharedLite(&fcb->Vcb->tree_lock, TRUE);
2206
2207 ExAcquireResourceExclusiveLite(fcb->Header.Resource, TRUE);
2208
2209 IoRemoveShareAccess(FileObject, &fcb->share_access);
2210
2211 if (ccb)
2212 FsRtlNotifyCleanup(fcb->Vcb->NotifySync, &fcb->Vcb->DirNotifyList, ccb);
2213
2214 if (fileref) {
2215 oc = InterlockedDecrement(&fileref->open_count);
2216 #ifdef DEBUG_FCB_REFCOUNTS
2217 ERR("fileref %p: open_count now %i\n", fileref, oc);
2218 #endif
2219 }
2220
2221 if (ccb && ccb->options & FILE_DELETE_ON_CLOSE && fileref)
2222 fileref->delete_on_close = TRUE;
2223
2224 if (fileref && fileref->delete_on_close && fcb->type == BTRFS_TYPE_DIRECTORY && fcb->inode_item.st_size > 0 && fcb != fcb->Vcb->dummy_fcb)
2225 fileref->delete_on_close = FALSE;
2226
2227 if (fcb->Vcb->locked && fcb->Vcb->locked_fileobj == FileObject) {
2228 TRACE("unlocking volume\n");
2229 do_unlock_volume(fcb->Vcb);
2230 FsRtlNotifyVolumeEvent(FileObject, FSRTL_VOLUME_UNLOCK);
2231 }
2232
2233 if (ccb && ccb->reserving) {
2234 fcb->subvol->reserved = NULL;
2235 ccb->reserving = FALSE;
2236 // FIXME - flush all of subvol's fcbs
2237 }
2238
2239 if (fileref && oc == 0) {
2240 if (!fcb->Vcb->removing) {
2241 if (fileref && fileref->delete_on_close && fileref != fcb->Vcb->root_fileref && fcb != fcb->Vcb->volume_fcb) {
2242 LIST_ENTRY rollback;
2243
2244 InitializeListHead(&rollback);
2245
2246 if (!fileref->fcb->ads || fileref->dc) {
2247 if (fileref->fcb->ads) {
2248 send_notification_fileref(fileref->parent, fcb->type == BTRFS_TYPE_DIRECTORY ? FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME,
2249 FILE_ACTION_REMOVED, &fileref->dc->name);
2250 } else
2251 send_notification_fileref(fileref, fcb->type == BTRFS_TYPE_DIRECTORY ? FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME, FILE_ACTION_REMOVED, NULL);
2252 }
2253
2254 ExReleaseResourceLite(fcb->Header.Resource);
2255 locked = FALSE;
2256
2257 // fcb_lock needs to be acquired before fcb->Header.Resource
2258 acquire_fcb_lock_exclusive(fcb->Vcb);
2259
2260 Status = delete_fileref(fileref, FileObject, Irp, &rollback);
2261 if (!NT_SUCCESS(Status)) {
2262 ERR("delete_fileref returned %08x\n", Status);
2263 do_rollback(fcb->Vcb, &rollback);
2264 release_fcb_lock(fcb->Vcb);
2265 ExReleaseResourceLite(&fcb->Vcb->tree_lock);
2266 goto exit;
2267 }
2268
2269 release_fcb_lock(fcb->Vcb);
2270
2271 locked = FALSE;
2272
2273 clear_rollback(&rollback);
2274 } else if (FileObject->Flags & FO_CACHE_SUPPORTED && fcb->nonpaged->segment_object.DataSectionObject) {
2275 IO_STATUS_BLOCK iosb;
2276 CcFlushCache(FileObject->SectionObjectPointer, NULL, 0, &iosb);
2277
2278 if (!NT_SUCCESS(iosb.Status)) {
2279 ERR("CcFlushCache returned %08x\n", iosb.Status);
2280 }
2281
2282 if (!ExIsResourceAcquiredSharedLite(fcb->Header.PagingIoResource)) {
2283 ExAcquireResourceExclusiveLite(fcb->Header.PagingIoResource, TRUE);
2284 ExReleaseResourceLite(fcb->Header.PagingIoResource);
2285 }
2286
2287 CcPurgeCacheSection(&fcb->nonpaged->segment_object, NULL, 0, FALSE);
2288
2289 TRACE("flushed cache on close (FileObject = %p, fcb = %p, AllocationSize = %llx, FileSize = %llx, ValidDataLength = %llx)\n",
2290 FileObject, fcb, fcb->Header.AllocationSize.QuadPart, fcb->Header.FileSize.QuadPart, fcb->Header.ValidDataLength.QuadPart);
2291 }
2292 }
2293
2294 if (fcb->Vcb && fcb != fcb->Vcb->volume_fcb)
2295 CcUninitializeCacheMap(FileObject, NULL, NULL);
2296 }
2297
2298 if (locked)
2299 ExReleaseResourceLite(fcb->Header.Resource);
2300
2301 ExReleaseResourceLite(&fcb->Vcb->tree_lock);
2302
2303 FileObject->Flags |= FO_CLEANUP_COMPLETE;
2304 }
2305
2306 Status = STATUS_SUCCESS;
2307
2308 exit:
2309 TRACE("returning %08x\n", Status);
2310
2311 Irp->IoStatus.Status = Status;
2312 Irp->IoStatus.Information = 0;
2313
2314 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2315
2316 if (top_level)
2317 IoSetTopLevelIrp(NULL);
2318
2319 FsRtlExitFileSystem();
2320
2321 return Status;
2322 }
2323
2324 _Success_(return)
2325 BOOL get_file_attributes_from_xattr(_In_reads_bytes_(len) char* val, _In_ UINT16 len, _Out_ ULONG* atts) {
2326 if (len > 2 && val[0] == '0' && val[1] == 'x') {
2327 int i;
2328 ULONG dosnum = 0;
2329
2330 for (i = 2; i < len; i++) {
2331 dosnum *= 0x10;
2332
2333 if (val[i] >= '0' && val[i] <= '9')
2334 dosnum |= val[i] - '0';
2335 else if (val[i] >= 'a' && val[i] <= 'f')
2336 dosnum |= val[i] + 10 - 'a';
2337 else if (val[i] >= 'A' && val[i] <= 'F')
2338 dosnum |= val[i] + 10 - 'a';
2339 }
2340
2341 TRACE("DOSATTRIB: %08x\n", dosnum);
2342
2343 *atts = dosnum;
2344
2345 return TRUE;
2346 }
2347
2348 return FALSE;
2349 }
2350
2351 ULONG get_file_attributes(_In_ _Requires_lock_held_(_Curr_->tree_lock) device_extension* Vcb, _In_ root* r, _In_ UINT64 inode,
2352 _In_ UINT8 type, _In_ BOOL dotfile, _In_ BOOL ignore_xa, _In_opt_ PIRP Irp) {
2353 ULONG att;
2354 char* eaval;
2355 UINT16 ealen;
2356
2357 if (!ignore_xa && get_xattr(Vcb, r, inode, EA_DOSATTRIB, EA_DOSATTRIB_HASH, (UINT8**)&eaval, &ealen, Irp)) {
2358 ULONG dosnum = 0;
2359
2360 if (get_file_attributes_from_xattr(eaval, ealen, &dosnum)) {
2361 ExFreePool(eaval);
2362
2363 if (type == BTRFS_TYPE_DIRECTORY)
2364 dosnum |= FILE_ATTRIBUTE_DIRECTORY;
2365 else if (type == BTRFS_TYPE_SYMLINK)
2366 dosnum |= FILE_ATTRIBUTE_REPARSE_POINT;
2367
2368 if (type != BTRFS_TYPE_DIRECTORY)
2369 dosnum &= ~FILE_ATTRIBUTE_DIRECTORY;
2370
2371 if (inode == SUBVOL_ROOT_INODE) {
2372 if (r->root_item.flags & BTRFS_SUBVOL_READONLY)
2373 dosnum |= FILE_ATTRIBUTE_READONLY;
2374 else
2375 dosnum &= ~FILE_ATTRIBUTE_READONLY;
2376 }
2377
2378 return dosnum;
2379 }
2380
2381 ExFreePool(eaval);
2382 }
2383
2384 switch (type) {
2385 case BTRFS_TYPE_DIRECTORY:
2386 att = FILE_ATTRIBUTE_DIRECTORY;
2387 break;
2388
2389 case BTRFS_TYPE_SYMLINK:
2390 att = FILE_ATTRIBUTE_REPARSE_POINT;
2391 break;
2392
2393 default:
2394 att = 0;
2395 break;
2396 }
2397
2398 if (dotfile) {
2399 att |= FILE_ATTRIBUTE_HIDDEN;
2400 }
2401
2402 att |= FILE_ATTRIBUTE_ARCHIVE;
2403
2404 if (inode == SUBVOL_ROOT_INODE) {
2405 if (r->root_item.flags & BTRFS_SUBVOL_READONLY)
2406 att |= FILE_ATTRIBUTE_READONLY;
2407 else
2408 att &= ~FILE_ATTRIBUTE_READONLY;
2409 }
2410
2411 // FIXME - get READONLY from ii->st_mode
2412 // FIXME - return SYSTEM for block/char devices?
2413
2414 if (att == 0)
2415 att = FILE_ATTRIBUTE_NORMAL;
2416
2417 return att;
2418 }
2419
2420 NTSTATUS sync_read_phys(_In_ PDEVICE_OBJECT DeviceObject, _In_ UINT64 StartingOffset, _In_ ULONG Length,
2421 _Out_writes_bytes_(Length) PUCHAR Buffer, _In_ BOOL override) {
2422 IO_STATUS_BLOCK IoStatus;
2423 LARGE_INTEGER Offset;
2424 PIRP Irp;
2425 PIO_STACK_LOCATION IrpSp;
2426 NTSTATUS Status;
2427 read_context context;
2428
2429 num_reads++;
2430
2431 RtlZeroMemory(&context, sizeof(read_context));
2432 KeInitializeEvent(&context.Event, NotificationEvent, FALSE);
2433
2434 Offset.QuadPart = (LONGLONG)StartingOffset;
2435
2436 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
2437
2438 if (!Irp) {
2439 ERR("IoAllocateIrp failed\n");
2440 return STATUS_INSUFFICIENT_RESOURCES;
2441 }
2442
2443 Irp->Flags |= IRP_NOCACHE;
2444 IrpSp = IoGetNextIrpStackLocation(Irp);
2445 IrpSp->MajorFunction = IRP_MJ_READ;
2446
2447 if (override)
2448 IrpSp->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
2449
2450 if (DeviceObject->Flags & DO_BUFFERED_IO) {
2451 Irp->AssociatedIrp.SystemBuffer = ExAllocatePoolWithTag(NonPagedPool, Length, ALLOC_TAG);
2452 if (!Irp->AssociatedIrp.SystemBuffer) {
2453 ERR("out of memory\n");
2454 Status = STATUS_INSUFFICIENT_RESOURCES;
2455 goto exit;
2456 }
2457
2458 Irp->Flags |= IRP_BUFFERED_IO | IRP_DEALLOCATE_BUFFER | IRP_INPUT_OPERATION;
2459
2460 Irp->UserBuffer = Buffer;
2461 } else if (DeviceObject->Flags & DO_DIRECT_IO) {
2462 Irp->MdlAddress = IoAllocateMdl(Buffer, Length, FALSE, FALSE, NULL);
2463 if (!Irp->MdlAddress) {
2464 ERR("IoAllocateMdl failed\n");
2465 Status = STATUS_INSUFFICIENT_RESOURCES;
2466 goto exit;
2467 }
2468
2469 Status = STATUS_SUCCESS;
2470
2471 _SEH2_TRY {
2472 MmProbeAndLockPages(Irp->MdlAddress, KernelMode, IoWriteAccess);
2473 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
2474 Status = _SEH2_GetExceptionCode();
2475 } _SEH2_END;
2476
2477 if (!NT_SUCCESS(Status)) {
2478 ERR("MmProbeAndLockPages threw exception %08x\n", Status);
2479 IoFreeMdl(Irp->MdlAddress);
2480 goto exit;
2481 }
2482 } else
2483 Irp->UserBuffer = Buffer;
2484
2485 IrpSp->Parameters.Read.Length = Length;
2486 IrpSp->Parameters.Read.ByteOffset = Offset;
2487
2488 Irp->UserIosb = &IoStatus;
2489
2490 Irp->UserEvent = &context.Event;
2491
2492 IoSetCompletionRoutine(Irp, read_completion, &context, TRUE, TRUE, TRUE);
2493
2494 Status = IoCallDriver(DeviceObject, Irp);
2495
2496 if (Status == STATUS_PENDING) {
2497 KeWaitForSingleObject(&context.Event, Executive, KernelMode, FALSE, NULL);
2498 Status = context.iosb.Status;
2499 }
2500
2501 if (DeviceObject->Flags & DO_DIRECT_IO) {
2502 MmUnlockPages(Irp->MdlAddress);
2503 IoFreeMdl(Irp->MdlAddress);
2504 }
2505
2506 exit:
2507 IoFreeIrp(Irp);
2508
2509 return Status;
2510 }
2511
2512 static NTSTATUS read_superblock(_In_ device_extension* Vcb, _In_ PDEVICE_OBJECT device, _In_ UINT64 length) {
2513 NTSTATUS Status;
2514 superblock* sb;
2515 ULONG i, to_read;
2516 UINT8 valid_superblocks;
2517
2518 to_read = device->SectorSize == 0 ? sizeof(superblock) : (ULONG)sector_align(sizeof(superblock), device->SectorSize);
2519
2520 sb = ExAllocatePoolWithTag(NonPagedPool, to_read, ALLOC_TAG);
2521 if (!sb) {
2522 ERR("out of memory\n");
2523 return STATUS_INSUFFICIENT_RESOURCES;
2524 }
2525
2526 if (superblock_addrs[0] + to_read > length) {
2527 WARN("device was too short to have any superblock\n");
2528 ExFreePool(sb);
2529 return STATUS_UNRECOGNIZED_VOLUME;
2530 }
2531
2532 i = 0;
2533 valid_superblocks = 0;
2534
2535 while (superblock_addrs[i] > 0) {
2536 UINT32 crc32;
2537
2538 if (i > 0 && superblock_addrs[i] + to_read > length)
2539 break;
2540
2541 Status = sync_read_phys(device, superblock_addrs[i], to_read, (PUCHAR)sb, FALSE);
2542 if (!NT_SUCCESS(Status)) {
2543 ERR("Failed to read superblock %u: %08x\n", i, Status);
2544 ExFreePool(sb);
2545 return Status;
2546 }
2547
2548 if (sb->magic != BTRFS_MAGIC) {
2549 if (i == 0) {
2550 TRACE("not a BTRFS volume\n");
2551 ExFreePool(sb);
2552 return STATUS_UNRECOGNIZED_VOLUME;
2553 }
2554 } else {
2555 TRACE("got superblock %u!\n", i);
2556
2557 crc32 = ~calc_crc32c(0xffffffff, (UINT8*)&sb->uuid, (ULONG)sizeof(superblock) - sizeof(sb->checksum));
2558
2559 if (crc32 != *((UINT32*)sb->checksum))
2560 WARN("crc32 was %08x, expected %08x\n", crc32, *((UINT32*)sb->checksum));
2561 else if (sb->sector_size == 0)
2562 WARN("superblock sector size was 0\n");
2563 else if (sb->node_size < sizeof(tree_header) + sizeof(internal_node) || sb->node_size > 0x10000)
2564 WARN("invalid node size %x\n", sb->node_size);
2565 else if ((sb->node_size % sb->sector_size) != 0)
2566 WARN("node size %x was not a multiple of sector_size %x\n", sb->node_size, sb->sector_size);
2567 else if (valid_superblocks == 0 || sb->generation > Vcb->superblock.generation) {
2568 RtlCopyMemory(&Vcb->superblock, sb, sizeof(superblock));
2569 valid_superblocks++;
2570 }
2571 }
2572
2573 i++;
2574 }
2575
2576 ExFreePool(sb);
2577
2578 if (valid_superblocks == 0) {
2579 ERR("could not find any valid superblocks\n");
2580 return STATUS_INTERNAL_ERROR;
2581 }
2582
2583 TRACE("label is %s\n", Vcb->superblock.label);
2584
2585 return STATUS_SUCCESS;
2586 }
2587
2588 NTSTATUS dev_ioctl(_In_ PDEVICE_OBJECT DeviceObject, _In_ ULONG ControlCode, _In_reads_bytes_opt_(InputBufferSize) PVOID InputBuffer, _In_ ULONG InputBufferSize,
2589 _Out_writes_bytes_opt_(OutputBufferSize) PVOID OutputBuffer, _In_ ULONG OutputBufferSize, _In_ BOOLEAN Override, _Out_opt_ IO_STATUS_BLOCK* iosb) {
2590 PIRP Irp;
2591 KEVENT Event;
2592 NTSTATUS Status;
2593 PIO_STACK_LOCATION IrpSp;
2594 IO_STATUS_BLOCK IoStatus;
2595
2596 KeInitializeEvent(&Event, NotificationEvent, FALSE);
2597
2598 Irp = IoBuildDeviceIoControlRequest(ControlCode,
2599 DeviceObject,
2600 InputBuffer,
2601 InputBufferSize,
2602 OutputBuffer,
2603 OutputBufferSize,
2604 FALSE,
2605 &Event,
2606 &IoStatus);
2607
2608 if (!Irp) return STATUS_INSUFFICIENT_RESOURCES;
2609
2610 if (Override) {
2611 IrpSp = IoGetNextIrpStackLocation(Irp);
2612 IrpSp->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
2613 }
2614
2615 Status = IoCallDriver(DeviceObject, Irp);
2616
2617 if (Status == STATUS_PENDING) {
2618 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
2619 Status = IoStatus.Status;
2620 }
2621
2622 if (iosb)
2623 *iosb = IoStatus;
2624
2625 return Status;
2626 }
2627
2628 _Requires_exclusive_lock_held_(Vcb->tree_lock)
2629 static NTSTATUS add_root(_Inout_ device_extension* Vcb, _In_ UINT64 id, _In_ UINT64 addr,
2630 _In_ UINT64 generation, _In_opt_ traverse_ptr* tp) {
2631 root* r = ExAllocatePoolWithTag(PagedPool, sizeof(root), ALLOC_TAG);
2632 if (!r) {
2633 ERR("out of memory\n");
2634 return STATUS_INSUFFICIENT_RESOURCES;
2635 }
2636
2637 r->id = id;
2638 r->dirty = FALSE;
2639 r->received = FALSE;
2640 r->reserved = NULL;
2641 r->treeholder.address = addr;
2642 r->treeholder.tree = NULL;
2643 r->treeholder.generation = generation;
2644 r->parent = 0;
2645 r->send_ops = 0;
2646 InitializeListHead(&r->fcbs);
2647
2648 r->nonpaged = ExAllocatePoolWithTag(NonPagedPool, sizeof(root_nonpaged), ALLOC_TAG);
2649 if (!r->nonpaged) {
2650 ERR("out of memory\n");
2651 ExFreePool(r);
2652 return STATUS_INSUFFICIENT_RESOURCES;
2653 }
2654
2655 ExInitializeResourceLite(&r->nonpaged->load_tree_lock);
2656
2657 r->lastinode = 0;
2658
2659 if (tp) {
2660 RtlCopyMemory(&r->root_item, tp->item->data, min(sizeof(ROOT_ITEM), tp->item->size));
2661 if (tp->item->size < sizeof(ROOT_ITEM))
2662 RtlZeroMemory(((UINT8*)&r->root_item) + tp->item->size, sizeof(ROOT_ITEM) - tp->item->size);
2663 } else
2664 RtlZeroMemory(&r->root_item, sizeof(ROOT_ITEM));
2665
2666 if (!Vcb->readonly && (r->id == BTRFS_ROOT_ROOT || r->id == BTRFS_ROOT_FSTREE || (r->id >= 0x100 && !(r->id & 0xf000000000000000)))) { // FS tree root
2667 // FIXME - don't call this if subvol is readonly (though we will have to if we ever toggle this flag)
2668 get_last_inode(Vcb, r, NULL);
2669
2670 if (r->id == BTRFS_ROOT_ROOT && r->lastinode < 0x100)
2671 r->lastinode = 0x100;
2672 }
2673
2674 InsertTailList(&Vcb->roots, &r->list_entry);
2675
2676 switch (r->id) {
2677 case BTRFS_ROOT_ROOT:
2678 Vcb->root_root = r;
2679 break;
2680
2681 case BTRFS_ROOT_EXTENT:
2682 Vcb->extent_root = r;
2683 break;
2684
2685 case BTRFS_ROOT_CHUNK:
2686 Vcb->chunk_root = r;
2687 break;
2688
2689 case BTRFS_ROOT_DEVTREE:
2690 Vcb->dev_root = r;
2691 break;
2692
2693 case BTRFS_ROOT_CHECKSUM:
2694 Vcb->checksum_root = r;
2695 break;
2696
2697 case BTRFS_ROOT_UUID:
2698 Vcb->uuid_root = r;
2699 break;
2700
2701 case BTRFS_ROOT_FREE_SPACE:
2702 Vcb->space_root = r;
2703 break;
2704
2705 case BTRFS_ROOT_DATA_RELOC:
2706 Vcb->data_reloc_root = r;
2707 break;
2708 }
2709
2710 return STATUS_SUCCESS;
2711 }
2712
2713 static NTSTATUS look_for_roots(_Requires_exclusive_lock_held_(_Curr_->tree_lock) _In_ device_extension* Vcb, _In_opt_ PIRP Irp) {
2714 traverse_ptr tp, next_tp;
2715 KEY searchkey;
2716 BOOL b;
2717 NTSTATUS Status;
2718
2719 searchkey.obj_id = 0;
2720 searchkey.obj_type = 0;
2721 searchkey.offset = 0;
2722
2723 Status = find_item(Vcb, Vcb->root_root, &tp, &searchkey, FALSE, Irp);
2724 if (!NT_SUCCESS(Status)) {
2725 ERR("error - find_item returned %08x\n", Status);
2726 return Status;
2727 }
2728
2729 do {
2730 TRACE("(%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
2731
2732 if (tp.item->key.obj_type == TYPE_ROOT_ITEM) {
2733 ROOT_ITEM* ri = (ROOT_ITEM*)tp.item->data;
2734
2735 if (tp.item->size < offsetof(ROOT_ITEM, byte_limit)) {
2736 ERR("(%llx,%x,%llx) was %u bytes, expected at least %u\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, offsetof(ROOT_ITEM, byte_limit));
2737 } else {
2738 TRACE("root %llx - address %llx\n", tp.item->key.obj_id, ri->block_number);
2739
2740 Status = add_root(Vcb, tp.item->key.obj_id, ri->block_number, ri->generation, &tp);
2741 if (!NT_SUCCESS(Status)) {
2742 ERR("add_root returned %08x\n", Status);
2743 return Status;
2744 }
2745 }
2746 } else if (tp.item->key.obj_type == TYPE_ROOT_BACKREF && !IsListEmpty(&Vcb->roots)) {
2747 root* lastroot = CONTAINING_RECORD(Vcb->roots.Blink, root, list_entry);
2748
2749 if (lastroot->id == tp.item->key.obj_id)
2750 lastroot->parent = tp.item->key.offset;
2751 }
2752
2753 b = find_next_item(Vcb, &tp, &next_tp, FALSE, Irp);
2754
2755 if (b)
2756 tp = next_tp;
2757 } while (b);
2758
2759 if (!Vcb->readonly && !Vcb->data_reloc_root) {
2760 root* reloc_root;
2761 INODE_ITEM* ii;
2762 UINT16 irlen;
2763 INODE_REF* ir;
2764 LARGE_INTEGER time;
2765 BTRFS_TIME now;
2766
2767 WARN("data reloc root doesn't exist, creating it\n");
2768
2769 Status = create_root(Vcb, BTRFS_ROOT_DATA_RELOC, &reloc_root, FALSE, 0, Irp);
2770
2771 if (!NT_SUCCESS(Status)) {
2772 ERR("create_root returned %08x\n", Status);
2773 return Status;
2774 }
2775
2776 reloc_root->root_item.inode.generation = 1;
2777 reloc_root->root_item.inode.st_size = 3;
2778 reloc_root->root_item.inode.st_blocks = Vcb->superblock.node_size;
2779 reloc_root->root_item.inode.st_nlink = 1;
2780 reloc_root->root_item.inode.st_mode = 040755;
2781 reloc_root->root_item.inode.flags = 0xffffffff80000000;
2782 reloc_root->root_item.objid = SUBVOL_ROOT_INODE;
2783 reloc_root->root_item.bytes_used = Vcb->superblock.node_size;
2784
2785 ii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
2786 if (!ii) {
2787 ERR("out of memory\n");
2788 return STATUS_INSUFFICIENT_RESOURCES;
2789 }
2790
2791 KeQuerySystemTime(&time);
2792 win_time_to_unix(time, &now);
2793
2794 RtlZeroMemory(ii, sizeof(INODE_ITEM));
2795 ii->generation = Vcb->superblock.generation;
2796 ii->st_blocks = Vcb->superblock.node_size;
2797 ii->st_nlink = 1;
2798 ii->st_mode = 040755;
2799 ii->st_atime = now;
2800 ii->st_ctime = now;
2801 ii->st_mtime = now;
2802
2803 Status = insert_tree_item(Vcb, reloc_root, SUBVOL_ROOT_INODE, TYPE_INODE_ITEM, 0, ii, sizeof(INODE_ITEM), NULL, Irp);
2804 if (!NT_SUCCESS(Status)) {
2805 ERR("insert_tree_item returned %08x\n", Status);
2806 ExFreePool(ii);
2807 return Status;
2808 }
2809
2810 irlen = (UINT16)offsetof(INODE_REF, name[0]) + 2;
2811 ir = ExAllocatePoolWithTag(PagedPool, irlen, ALLOC_TAG);
2812 if (!ir) {
2813 ERR("out of memory\n");
2814 return STATUS_INSUFFICIENT_RESOURCES;
2815 }
2816
2817 ir->index = 0;
2818 ir->n = 2;
2819 ir->name[0] = '.';
2820 ir->name[1] = '.';
2821
2822 Status = insert_tree_item(Vcb, reloc_root, SUBVOL_ROOT_INODE, TYPE_INODE_REF, SUBVOL_ROOT_INODE, ir, irlen, NULL, Irp);
2823 if (!NT_SUCCESS(Status)) {
2824 ERR("insert_tree_item returned %08x\n", Status);
2825 ExFreePool(ir);
2826 return Status;
2827 }
2828
2829 Vcb->data_reloc_root = reloc_root;
2830 Vcb->need_write = TRUE;
2831 }
2832
2833 return STATUS_SUCCESS;
2834 }
2835
2836 static NTSTATUS find_disk_holes(_In_ _Requires_lock_held_(_Curr_->tree_lock) device_extension* Vcb, _In_ device* dev, _In_opt_ PIRP Irp) {
2837 KEY searchkey;
2838 traverse_ptr tp, next_tp;
2839 BOOL b;
2840 UINT64 lastaddr;
2841 NTSTATUS Status;
2842
2843 InitializeListHead(&dev->space);
2844
2845 searchkey.obj_id = 0;
2846 searchkey.obj_type = TYPE_DEV_STATS;
2847 searchkey.offset = dev->devitem.dev_id;
2848
2849 Status = find_item(Vcb, Vcb->dev_root, &tp, &searchkey, FALSE, Irp);
2850 if (NT_SUCCESS(Status) && !keycmp(tp.item->key, searchkey))
2851 RtlCopyMemory(dev->stats, tp.item->data, min(sizeof(UINT64) * 5, tp.item->size));
2852
2853 searchkey.obj_id = dev->devitem.dev_id;
2854 searchkey.obj_type = TYPE_DEV_EXTENT;
2855 searchkey.offset = 0;
2856
2857 Status = find_item(Vcb, Vcb->dev_root, &tp, &searchkey, FALSE, Irp);
2858 if (!NT_SUCCESS(Status)) {
2859 ERR("error - find_item returned %08x\n", Status);
2860 return Status;
2861 }
2862
2863 lastaddr = 0;
2864
2865 do {
2866 if (tp.item->key.obj_id == dev->devitem.dev_id && tp.item->key.obj_type == TYPE_DEV_EXTENT) {
2867 if (tp.item->size >= sizeof(DEV_EXTENT)) {
2868 DEV_EXTENT* de = (DEV_EXTENT*)tp.item->data;
2869
2870 if (tp.item->key.offset > lastaddr) {
2871 Status = add_space_entry(&dev->space, NULL, lastaddr, tp.item->key.offset - lastaddr);
2872 if (!NT_SUCCESS(Status)) {
2873 ERR("add_space_entry returned %08x\n", Status);
2874 return Status;
2875 }
2876 }
2877
2878 lastaddr = tp.item->key.offset + de->length;
2879 } else {
2880 ERR("(%llx,%x,%llx) was %u bytes, expected %u\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(DEV_EXTENT));
2881 }
2882 }
2883
2884 b = find_next_item(Vcb, &tp, &next_tp, FALSE, Irp);
2885
2886 if (b) {
2887 tp = next_tp;
2888 if (tp.item->key.obj_id > searchkey.obj_id || tp.item->key.obj_type > searchkey.obj_type)
2889 break;
2890 }
2891 } while (b);
2892
2893 if (lastaddr < dev->devitem.num_bytes) {
2894 Status = add_space_entry(&dev->space, NULL, lastaddr, dev->devitem.num_bytes - lastaddr);
2895 if (!NT_SUCCESS(Status)) {
2896 ERR("add_space_entry returned %08x\n", Status);
2897 return Status;
2898 }
2899 }
2900
2901 // The Linux driver doesn't like to allocate chunks within the first megabyte of a device.
2902
2903 space_list_subtract2(&dev->space, NULL, 0, 0x100000, NULL, NULL);
2904
2905 return STATUS_SUCCESS;
2906 }
2907
2908 static void add_device_to_list(_In_ device_extension* Vcb, _In_ device* dev) {
2909 LIST_ENTRY* le;
2910
2911 le = Vcb->devices.Flink;
2912
2913 while (le != &Vcb->devices) {
2914 device* dev2 = CONTAINING_RECORD(le, device, list_entry);
2915
2916 if (dev2->devitem.dev_id > dev->devitem.dev_id) {
2917 InsertHeadList(le->Blink, &dev->list_entry);
2918 return;
2919 }
2920
2921 le = le->Flink;
2922 }
2923
2924 InsertTailList(&Vcb->devices, &dev->list_entry);
2925 }
2926
2927 _Ret_maybenull_
2928 device* find_device_from_uuid(_In_ device_extension* Vcb, _In_ BTRFS_UUID* uuid) {
2929 volume_device_extension* vde;
2930 pdo_device_extension* pdode;
2931 LIST_ENTRY* le;
2932
2933 le = Vcb->devices.Flink;
2934 while (le != &Vcb->devices) {
2935 device* dev = CONTAINING_RECORD(le, device, list_entry);
2936
2937 TRACE("device %llx, uuid %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n", dev->devitem.dev_id,
2938 dev->devitem.device_uuid.uuid[0], dev->devitem.device_uuid.uuid[1], dev->devitem.device_uuid.uuid[2], dev->devitem.device_uuid.uuid[3], dev->devitem.device_uuid.uuid[4], dev->devitem.device_uuid.uuid[5], dev->devitem.device_uuid.uuid[6], dev->devitem.device_uuid.uuid[7],
2939 dev->devitem.device_uuid.uuid[8], dev->devitem.device_uuid.uuid[9], dev->devitem.device_uuid.uuid[10], dev->devitem.device_uuid.uuid[11], dev->devitem.device_uuid.uuid[12], dev->devitem.device_uuid.uuid[13], dev->devitem.device_uuid.uuid[14], dev->devitem.device_uuid.uuid[15]);
2940
2941 if (RtlCompareMemory(&dev->devitem.device_uuid, uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
2942 TRACE("returning device %llx\n", dev->devitem.dev_id);
2943 return dev;
2944 }
2945
2946 le = le->Flink;
2947 }
2948
2949 vde = Vcb->vde;
2950
2951 if (!vde)
2952 goto end;
2953
2954 pdode = vde->pdode;
2955
2956 ExAcquireResourceSharedLite(&pdode->child_lock, TRUE);
2957
2958 if (Vcb->devices_loaded < Vcb->superblock.num_devices) {
2959 le = pdode->children.Flink;
2960
2961 while (le != &pdode->children) {
2962 volume_child* vc = CONTAINING_RECORD(le, volume_child, list_entry);
2963
2964 if (RtlCompareMemory(uuid, &vc->uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
2965 device* dev;
2966
2967 dev = ExAllocatePoolWithTag(NonPagedPool, sizeof(device), ALLOC_TAG);
2968 if (!dev) {
2969 ExReleaseResourceLite(&pdode->child_lock);
2970 ERR("out of memory\n");
2971 return NULL;
2972 }
2973
2974 RtlZeroMemory(dev, sizeof(device));
2975 dev->devobj = vc->devobj;
2976 dev->devitem.device_uuid = *uuid;
2977 dev->devitem.dev_id = vc->devid;
2978 dev->devitem.num_bytes = vc->size;
2979 dev->seeding = vc->seeding;
2980 dev->readonly = dev->seeding;
2981 dev->reloc = FALSE;
2982 dev->removable = FALSE;
2983 dev->disk_num = vc->disk_num;
2984 dev->part_num = vc->part_num;
2985 dev->num_trim_entries = 0;
2986 InitializeListHead(&dev->trim_list);
2987
2988 add_device_to_list(Vcb, dev);
2989 Vcb->devices_loaded++;
2990
2991 ExReleaseResourceLite(&pdode->child_lock);
2992
2993 return dev;
2994 }
2995
2996 le = le->Flink;
2997 }
2998 }
2999
3000 ExReleaseResourceLite(&pdode->child_lock);
3001
3002 end:
3003 WARN("could not find device with uuid %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
3004 uuid->uuid[0], uuid->uuid[1], uuid->uuid[2], uuid->uuid[3], uuid->uuid[4], uuid->uuid[5], uuid->uuid[6], uuid->uuid[7],
3005 uuid->uuid[8], uuid->uuid[9], uuid->uuid[10], uuid->uuid[11], uuid->uuid[12], uuid->uuid[13], uuid->uuid[14], uuid->uuid[15]);
3006
3007 return NULL;
3008 }
3009
3010 static BOOL is_device_removable(_In_ PDEVICE_OBJECT devobj) {
3011 NTSTATUS Status;
3012 STORAGE_HOTPLUG_INFO shi;
3013
3014 Status = dev_ioctl(devobj, IOCTL_STORAGE_GET_HOTPLUG_INFO, NULL, 0, &shi, sizeof(STORAGE_HOTPLUG_INFO), TRUE, NULL);
3015
3016 if (!NT_SUCCESS(Status)) {
3017 ERR("dev_ioctl returned %08x\n", Status);
3018 return FALSE;
3019 }
3020
3021 return shi.MediaRemovable != 0 ? TRUE : FALSE;
3022 }
3023
3024 static ULONG get_device_change_count(_In_ PDEVICE_OBJECT devobj) {
3025 NTSTATUS Status;
3026 ULONG cc;
3027 IO_STATUS_BLOCK iosb;
3028
3029 Status = dev_ioctl(devobj, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, &cc, sizeof(ULONG), TRUE, &iosb);
3030
3031 if (!NT_SUCCESS(Status)) {
3032 ERR("dev_ioctl returned %08x\n", Status);
3033 return 0;
3034 }
3035
3036 if (iosb.Information < sizeof(ULONG)) {
3037 ERR("iosb.Information was too short\n");
3038 return 0;
3039 }
3040
3041 return cc;
3042 }
3043
3044 void init_device(_In_ device_extension* Vcb, _Inout_ device* dev, _In_ BOOL get_nums) {
3045 NTSTATUS Status;
3046 ULONG aptelen;
3047 ATA_PASS_THROUGH_EX* apte;
3048 STORAGE_PROPERTY_QUERY spq;
3049 DEVICE_TRIM_DESCRIPTOR dtd;
3050
3051 dev->removable = is_device_removable(dev->devobj);
3052 dev->change_count = dev->removable ? get_device_change_count(dev->devobj) : 0;
3053
3054 if (get_nums) {
3055 STORAGE_DEVICE_NUMBER sdn;
3056
3057 Status = dev_ioctl(dev->devobj, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0,
3058 &sdn, sizeof(STORAGE_DEVICE_NUMBER), TRUE, NULL);