* Sync with trunk HEAD (r75820).
[reactos.git] / reactos / 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 static NTSTATUS drv_close(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
468 NTSTATUS Status;
469 PIO_STACK_LOCATION IrpSp;
470 device_extension* Vcb = DeviceObject->DeviceExtension;
471 BOOL top_level;
472
473 FsRtlEnterFileSystem();
474
475 TRACE("close\n");
476
477 top_level = is_top_level(Irp);
478
479 if (DeviceObject == master_devobj) {
480 TRACE("Closing file system\n");
481 Status = STATUS_SUCCESS;
482 goto end;
483 } else if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
484 Status = vol_close(DeviceObject, Irp);
485 goto end;
486 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
487 Status = STATUS_INVALID_PARAMETER;
488 goto end;
489 }
490
491 IrpSp = IoGetCurrentIrpStackLocation(Irp);
492
493 // FIXME - unmount if called for volume
494 // FIXME - call FsRtlNotifyUninitializeSync(&Vcb->NotifySync) if unmounting
495
496 Status = close_file(IrpSp->FileObject, Irp);
497
498 end:
499 Irp->IoStatus.Status = Status;
500 Irp->IoStatus.Information = 0;
501
502 IoCompleteRequest( Irp, IO_DISK_INCREMENT );
503
504 if (top_level)
505 IoSetTopLevelIrp(NULL);
506
507 TRACE("returning %08x\n", Status);
508
509 FsRtlExitFileSystem();
510
511 return Status;
512 }
513
514 _Dispatch_type_(IRP_MJ_FLUSH_BUFFERS)
515 _Function_class_(DRIVER_DISPATCH)
516 static NTSTATUS drv_flush_buffers(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
517 NTSTATUS Status;
518 PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
519 PFILE_OBJECT FileObject = IrpSp->FileObject;
520 fcb* fcb = FileObject->FsContext;
521 device_extension* Vcb = DeviceObject->DeviceExtension;
522 BOOL top_level;
523
524 FsRtlEnterFileSystem();
525
526 TRACE("flush buffers\n");
527
528 top_level = is_top_level(Irp);
529
530 if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
531 Status = vol_flush_buffers(DeviceObject, Irp);
532 goto end;
533 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
534 Status = STATUS_INVALID_PARAMETER;
535 goto end;
536 }
537
538 if (!fcb) {
539 ERR("fcb was NULL\n");
540 Status = STATUS_INVALID_PARAMETER;
541 goto end;
542 }
543
544 if (fcb == Vcb->volume_fcb) {
545 Status = STATUS_INVALID_PARAMETER;
546 goto end;
547 }
548
549 Irp->IoStatus.Information = 0;
550
551 fcb->Header.IsFastIoPossible = fast_io_possible(fcb);
552
553 Status = STATUS_SUCCESS;
554 Irp->IoStatus.Status = Status;
555
556 if (fcb->type != BTRFS_TYPE_DIRECTORY) {
557 CcFlushCache(&fcb->nonpaged->segment_object, NULL, 0, &Irp->IoStatus);
558
559 if (fcb->Header.PagingIoResource) {
560 ExAcquireResourceExclusiveLite(fcb->Header.PagingIoResource, TRUE);
561 ExReleaseResourceLite(fcb->Header.PagingIoResource);
562 }
563
564 Status = Irp->IoStatus.Status;
565 }
566
567 end:
568 IoCompleteRequest(Irp, IO_NO_INCREMENT);
569
570 TRACE("returning %08x\n", Status);
571
572 if (top_level)
573 IoSetTopLevelIrp(NULL);
574
575 FsRtlExitFileSystem();
576
577 return Status;
578 }
579
580 static void calculate_total_space(_In_ device_extension* Vcb, _Out_ UINT64* totalsize, _Out_ UINT64* freespace) {
581 UINT64 nfactor, dfactor, sectors_used;
582
583 if (Vcb->data_flags & BLOCK_FLAG_DUPLICATE || Vcb->data_flags & BLOCK_FLAG_RAID1 || Vcb->data_flags & BLOCK_FLAG_RAID10) {
584 nfactor = 1;
585 dfactor = 2;
586 } else if (Vcb->data_flags & BLOCK_FLAG_RAID5) {
587 nfactor = Vcb->superblock.num_devices - 1;
588 dfactor = Vcb->superblock.num_devices;
589 } else if (Vcb->data_flags & BLOCK_FLAG_RAID6) {
590 nfactor = Vcb->superblock.num_devices - 2;
591 dfactor = Vcb->superblock.num_devices;
592 } else {
593 nfactor = 1;
594 dfactor = 1;
595 }
596
597 sectors_used = Vcb->superblock.bytes_used / Vcb->superblock.sector_size;
598
599 *totalsize = (Vcb->superblock.total_bytes / Vcb->superblock.sector_size) * nfactor / dfactor;
600 *freespace = sectors_used > *totalsize ? 0 : (*totalsize - sectors_used);
601 }
602
603 #ifndef __REACTOS__
604 // This function exists because we have to lie about our FS type in certain situations.
605 // MPR!MprGetConnection queries the FS type, and compares it to a whitelist. If it doesn't match,
606 // it will return ERROR_NO_NET_OR_BAD_PATH, which prevents UAC from working.
607 // The command mklink refuses to create hard links on anything other than NTFS, so we have to
608 // blacklist cmd.exe too.
609
610 static BOOL lie_about_fs_type() {
611 NTSTATUS Status;
612 PROCESS_BASIC_INFORMATION pbi;
613 PPEB peb;
614 LIST_ENTRY* le;
615 ULONG retlen;
616
617 static WCHAR mpr[] = L"MPR.DLL";
618 static WCHAR cmd[] = L"CMD.EXE";
619 static WCHAR fsutil[] = L"FSUTIL.EXE";
620 UNICODE_STRING mprus, cmdus, fsutilus;
621
622 mprus.Buffer = mpr;
623 mprus.Length = mprus.MaximumLength = (USHORT)(wcslen(mpr) * sizeof(WCHAR));
624 cmdus.Buffer = cmd;
625 cmdus.Length = cmdus.MaximumLength = (USHORT)(wcslen(cmd) * sizeof(WCHAR));
626 fsutilus.Buffer = fsutil;
627 fsutilus.Length = fsutilus.MaximumLength = (USHORT)(wcslen(fsutil) * sizeof(WCHAR));
628
629 if (!PsGetCurrentProcess())
630 return FALSE;
631
632 Status = ZwQueryInformationProcess(NtCurrentProcess(), ProcessBasicInformation, &pbi, sizeof(pbi), &retlen);
633
634 if (!NT_SUCCESS(Status)) {
635 ERR("ZwQueryInformationProcess returned %08x\n", Status);
636 return FALSE;
637 }
638
639 if (!pbi.PebBaseAddress)
640 return FALSE;
641
642 peb = pbi.PebBaseAddress;
643
644 if (!peb->Ldr)
645 return FALSE;
646
647 le = peb->Ldr->InMemoryOrderModuleList.Flink;
648 while (le != &peb->Ldr->InMemoryOrderModuleList) {
649 LDR_DATA_TABLE_ENTRY* entry = CONTAINING_RECORD(le, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
650 BOOL blacklist = FALSE;
651
652 if (entry->FullDllName.Length >= mprus.Length) {
653 UNICODE_STRING name;
654
655 name.Buffer = &entry->FullDllName.Buffer[(entry->FullDllName.Length - mprus.Length) / sizeof(WCHAR)];
656 name.Length = name.MaximumLength = mprus.Length;
657
658 blacklist = FsRtlAreNamesEqual(&name, &mprus, TRUE, NULL);
659 }
660
661 if (!blacklist && entry->FullDllName.Length >= cmdus.Length) {
662 UNICODE_STRING name;
663
664 name.Buffer = &entry->FullDllName.Buffer[(entry->FullDllName.Length - cmdus.Length) / sizeof(WCHAR)];
665 name.Length = name.MaximumLength = cmdus.Length;
666
667 blacklist = FsRtlAreNamesEqual(&name, &cmdus, TRUE, NULL);
668 }
669
670 if (!blacklist && entry->FullDllName.Length >= fsutilus.Length) {
671 UNICODE_STRING name;
672
673 name.Buffer = &entry->FullDllName.Buffer[(entry->FullDllName.Length - fsutilus.Length) / sizeof(WCHAR)];
674 name.Length = name.MaximumLength = fsutilus.Length;
675
676 blacklist = FsRtlAreNamesEqual(&name, &fsutilus, TRUE, NULL);
677 }
678
679 if (blacklist) {
680 void** frames;
681 ULONG i, num_frames;
682
683 frames = ExAllocatePoolWithTag(PagedPool, 256 * sizeof(void*), ALLOC_TAG);
684 if (!frames) {
685 ERR("out of memory\n");
686 return FALSE;
687 }
688
689 num_frames = RtlWalkFrameChain(frames, 256, 1);
690
691 for (i = 0; i < num_frames; i++) {
692 // entry->Reserved3[1] appears to be the image size
693 if (frames[i] >= entry->DllBase && (ULONG_PTR)frames[i] <= (ULONG_PTR)entry->DllBase + (ULONG_PTR)entry->Reserved3[1]) {
694 ExFreePool(frames);
695 return TRUE;
696 }
697 }
698
699 ExFreePool(frames);
700 }
701
702 le = le->Flink;
703 }
704
705 return FALSE;
706 }
707 #endif
708
709 _Dispatch_type_(IRP_MJ_QUERY_VOLUME_INFORMATION)
710 _Function_class_(DRIVER_DISPATCH)
711 static NTSTATUS drv_query_volume_information(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
712 PIO_STACK_LOCATION IrpSp;
713 NTSTATUS Status;
714 ULONG BytesCopied = 0;
715 device_extension* Vcb = DeviceObject->DeviceExtension;
716 BOOL top_level;
717
718 FsRtlEnterFileSystem();
719
720 TRACE("query volume information\n");
721 top_level = is_top_level(Irp);
722
723 if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
724 Status = vol_query_volume_information(DeviceObject, Irp);
725 goto end;
726 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
727 Status = STATUS_INVALID_PARAMETER;
728 goto end;
729 }
730
731 IrpSp = IoGetCurrentIrpStackLocation(Irp);
732
733 Status = STATUS_NOT_IMPLEMENTED;
734
735 switch (IrpSp->Parameters.QueryVolume.FsInformationClass) {
736 case FileFsAttributeInformation:
737 {
738 FILE_FS_ATTRIBUTE_INFORMATION* data = Irp->AssociatedIrp.SystemBuffer;
739 BOOL overflow = FALSE;
740 #ifndef __REACTOS__
741 WCHAR* fs_name = (Irp->RequestorMode == UserMode && lie_about_fs_type()) ? L"NTFS" : L"Btrfs";
742 ULONG fs_name_len = (ULONG)wcslen(fs_name) * sizeof(WCHAR);
743 #else
744 WCHAR* fs_name = L"Btrfs";
745 ULONG fs_name_len = 5 * sizeof(WCHAR);
746 #endif
747 ULONG orig_fs_name_len = fs_name_len;
748
749 TRACE("FileFsAttributeInformation\n");
750
751 if (IrpSp->Parameters.QueryVolume.Length < sizeof(FILE_FS_ATTRIBUTE_INFORMATION) - sizeof(WCHAR) + fs_name_len) {
752 if (IrpSp->Parameters.QueryVolume.Length > sizeof(FILE_FS_ATTRIBUTE_INFORMATION) - sizeof(WCHAR))
753 fs_name_len = IrpSp->Parameters.QueryVolume.Length - sizeof(FILE_FS_ATTRIBUTE_INFORMATION) + sizeof(WCHAR);
754 else
755 fs_name_len = 0;
756
757 overflow = TRUE;
758 }
759
760 data->FileSystemAttributes = FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH |
761 FILE_UNICODE_ON_DISK | FILE_NAMED_STREAMS | FILE_SUPPORTS_HARD_LINKS | FILE_PERSISTENT_ACLS |
762 FILE_SUPPORTS_REPARSE_POINTS | FILE_SUPPORTS_SPARSE_FILES | FILE_SUPPORTS_OBJECT_IDS |
763 FILE_SUPPORTS_OPEN_BY_FILE_ID | FILE_SUPPORTS_EXTENDED_ATTRIBUTES | FILE_SUPPORTS_BLOCK_REFCOUNTING;
764 if (Vcb->readonly)
765 data->FileSystemAttributes |= FILE_READ_ONLY_VOLUME;
766
767 // should also be FILE_FILE_COMPRESSION when supported
768 data->MaximumComponentNameLength = 255; // FIXME - check
769 data->FileSystemNameLength = orig_fs_name_len;
770 RtlCopyMemory(data->FileSystemName, fs_name, fs_name_len);
771
772 BytesCopied = sizeof(FILE_FS_ATTRIBUTE_INFORMATION) - sizeof(WCHAR) + fs_name_len;
773 Status = overflow ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
774 break;
775 }
776
777 case FileFsDeviceInformation:
778 {
779 FILE_FS_DEVICE_INFORMATION* ffdi = Irp->AssociatedIrp.SystemBuffer;
780
781 TRACE("FileFsDeviceInformation\n");
782
783 ffdi->DeviceType = FILE_DEVICE_DISK;
784
785 ExAcquireResourceSharedLite(&Vcb->tree_lock, TRUE);
786 ffdi->Characteristics = Vcb->Vpb->RealDevice->Characteristics;
787 ExReleaseResourceLite(&Vcb->tree_lock);
788
789 if (Vcb->readonly)
790 ffdi->Characteristics |= FILE_READ_ONLY_DEVICE;
791 else
792 ffdi->Characteristics &= ~FILE_READ_ONLY_DEVICE;
793
794 BytesCopied = sizeof(FILE_FS_DEVICE_INFORMATION);
795 Status = STATUS_SUCCESS;
796
797 break;
798 }
799
800 case FileFsFullSizeInformation:
801 {
802 FILE_FS_FULL_SIZE_INFORMATION* ffsi = Irp->AssociatedIrp.SystemBuffer;
803
804 TRACE("FileFsFullSizeInformation\n");
805
806 calculate_total_space(Vcb, (UINT64*)&ffsi->TotalAllocationUnits.QuadPart, (UINT64*)&ffsi->ActualAvailableAllocationUnits.QuadPart);
807 ffsi->CallerAvailableAllocationUnits.QuadPart = ffsi->ActualAvailableAllocationUnits.QuadPart;
808 ffsi->SectorsPerAllocationUnit = 1;
809 ffsi->BytesPerSector = Vcb->superblock.sector_size;
810
811 BytesCopied = sizeof(FILE_FS_FULL_SIZE_INFORMATION);
812 Status = STATUS_SUCCESS;
813
814 break;
815 }
816
817 case FileFsObjectIdInformation:
818 {
819 FILE_FS_OBJECTID_INFORMATION* ffoi = Irp->AssociatedIrp.SystemBuffer;
820
821 TRACE("FileFsObjectIdInformation\n");
822
823 RtlCopyMemory(ffoi->ObjectId, &Vcb->superblock.uuid.uuid[0], sizeof(UCHAR) * 16);
824 RtlZeroMemory(ffoi->ExtendedInfo, sizeof(ffoi->ExtendedInfo));
825
826 BytesCopied = sizeof(FILE_FS_OBJECTID_INFORMATION);
827 Status = STATUS_SUCCESS;
828
829 break;
830 }
831
832 case FileFsSizeInformation:
833 {
834 FILE_FS_SIZE_INFORMATION* ffsi = Irp->AssociatedIrp.SystemBuffer;
835
836 TRACE("FileFsSizeInformation\n");
837
838 calculate_total_space(Vcb, (UINT64*)&ffsi->TotalAllocationUnits.QuadPart, (UINT64*)&ffsi->AvailableAllocationUnits.QuadPart);
839 ffsi->SectorsPerAllocationUnit = 1;
840 ffsi->BytesPerSector = Vcb->superblock.sector_size;
841
842 BytesCopied = sizeof(FILE_FS_SIZE_INFORMATION);
843 Status = STATUS_SUCCESS;
844
845 break;
846 }
847
848 case FileFsVolumeInformation:
849 {
850 FILE_FS_VOLUME_INFORMATION* data = Irp->AssociatedIrp.SystemBuffer;
851 FILE_FS_VOLUME_INFORMATION ffvi;
852 BOOL overflow = FALSE;
853 ULONG label_len, orig_label_len;
854
855 TRACE("FileFsVolumeInformation\n");
856 TRACE("max length = %u\n", IrpSp->Parameters.QueryVolume.Length);
857
858 ExAcquireResourceSharedLite(&Vcb->tree_lock, TRUE);
859
860 Status = RtlUTF8ToUnicodeN(NULL, 0, &label_len, Vcb->superblock.label, (ULONG)strlen(Vcb->superblock.label));
861 if (!NT_SUCCESS(Status)) {
862 ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
863 ExReleaseResourceLite(&Vcb->tree_lock);
864 break;
865 }
866
867 orig_label_len = label_len;
868
869 if (IrpSp->Parameters.QueryVolume.Length < sizeof(FILE_FS_VOLUME_INFORMATION) - sizeof(WCHAR) + label_len) {
870 if (IrpSp->Parameters.QueryVolume.Length > sizeof(FILE_FS_VOLUME_INFORMATION) - sizeof(WCHAR))
871 label_len = IrpSp->Parameters.QueryVolume.Length - sizeof(FILE_FS_VOLUME_INFORMATION) + sizeof(WCHAR);
872 else
873 label_len = 0;
874
875 overflow = TRUE;
876 }
877
878 TRACE("label_len = %u\n", label_len);
879
880 ffvi.VolumeCreationTime.QuadPart = 0; // FIXME
881 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];
882 ffvi.VolumeLabelLength = orig_label_len;
883 ffvi.SupportsObjects = FALSE;
884
885 RtlCopyMemory(data, &ffvi, min(sizeof(FILE_FS_VOLUME_INFORMATION) - sizeof(WCHAR), IrpSp->Parameters.QueryVolume.Length));
886
887 if (label_len > 0) {
888 ULONG bytecount;
889
890 Status = RtlUTF8ToUnicodeN(&data->VolumeLabel[0], label_len, &bytecount, Vcb->superblock.label, (ULONG)strlen(Vcb->superblock.label));
891 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL) {
892 ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
893 ExReleaseResourceLite(&Vcb->tree_lock);
894 break;
895 }
896
897 TRACE("label = %.*S\n", label_len / sizeof(WCHAR), data->VolumeLabel);
898 }
899
900 ExReleaseResourceLite(&Vcb->tree_lock);
901
902 BytesCopied = sizeof(FILE_FS_VOLUME_INFORMATION) - sizeof(WCHAR) + label_len;
903 Status = overflow ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
904 break;
905 }
906
907 #ifndef __REACTOS__
908 #ifdef _MSC_VER // not in mingw yet
909 case FileFsSectorSizeInformation:
910 {
911 FILE_FS_SECTOR_SIZE_INFORMATION* data = Irp->AssociatedIrp.SystemBuffer;
912
913 data->LogicalBytesPerSector = Vcb->superblock.sector_size;
914 data->PhysicalBytesPerSectorForAtomicity = Vcb->superblock.sector_size;
915 data->PhysicalBytesPerSectorForPerformance = Vcb->superblock.sector_size;
916 data->FileSystemEffectivePhysicalBytesPerSectorForAtomicity = Vcb->superblock.sector_size;
917 data->ByteOffsetForSectorAlignment = 0;
918 data->ByteOffsetForPartitionAlignment = 0;
919
920 data->Flags = SSINFO_FLAGS_ALIGNED_DEVICE | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE;
921
922 if (Vcb->trim && !Vcb->options.no_trim)
923 data->Flags |= SSINFO_FLAGS_TRIM_ENABLED;
924
925 BytesCopied = sizeof(FILE_FS_SECTOR_SIZE_INFORMATION);
926
927 break;
928 }
929 #endif
930 #endif /* __REACTOS__ */
931
932 default:
933 Status = STATUS_INVALID_PARAMETER;
934 WARN("unknown FsInformationClass %u\n", IrpSp->Parameters.QueryVolume.FsInformationClass);
935 break;
936 }
937
938 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_OVERFLOW)
939 Irp->IoStatus.Information = 0;
940 else
941 Irp->IoStatus.Information = BytesCopied;
942
943 end:
944 Irp->IoStatus.Status = Status;
945
946 IoCompleteRequest( Irp, IO_DISK_INCREMENT );
947
948 if (top_level)
949 IoSetTopLevelIrp(NULL);
950
951 TRACE("query volume information returning %08x\n", Status);
952
953 FsRtlExitFileSystem();
954
955 return Status;
956 }
957
958 _Function_class_(IO_COMPLETION_ROUTINE)
959 #ifdef __REACTOS__
960 static NTSTATUS NTAPI read_completion(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp, _In_ PVOID conptr) {
961 #else
962 static NTSTATUS read_completion(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp, _In_ PVOID conptr) {
963 #endif
964 read_context* context = conptr;
965
966 UNUSED(DeviceObject);
967
968 context->iosb = Irp->IoStatus;
969 KeSetEvent(&context->Event, 0, FALSE);
970
971 return STATUS_MORE_PROCESSING_REQUIRED;
972 }
973
974 NTSTATUS create_root(_In_ _Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, _In_ UINT64 id,
975 _Out_ root** rootptr, _In_ BOOL no_tree, _In_ UINT64 offset, _In_opt_ PIRP Irp) {
976 NTSTATUS Status;
977 root* r;
978 tree* t = NULL;
979 ROOT_ITEM* ri;
980 traverse_ptr tp;
981
982 r = ExAllocatePoolWithTag(PagedPool, sizeof(root), ALLOC_TAG);
983 if (!r) {
984 ERR("out of memory\n");
985 return STATUS_INSUFFICIENT_RESOURCES;
986 }
987
988 r->nonpaged = ExAllocatePoolWithTag(NonPagedPool, sizeof(root_nonpaged), ALLOC_TAG);
989 if (!r->nonpaged) {
990 ERR("out of memory\n");
991 ExFreePool(r);
992 return STATUS_INSUFFICIENT_RESOURCES;
993 }
994
995 if (!no_tree) {
996 t = ExAllocatePoolWithTag(PagedPool, sizeof(tree), ALLOC_TAG);
997 if (!t) {
998 ERR("out of memory\n");
999 ExFreePool(r->nonpaged);
1000 ExFreePool(r);
1001 return STATUS_INSUFFICIENT_RESOURCES;
1002 }
1003
1004 t->is_unique = TRUE;
1005 t->uniqueness_determined = TRUE;
1006 t->buf = NULL;
1007 }
1008
1009 ri = ExAllocatePoolWithTag(PagedPool, sizeof(ROOT_ITEM), ALLOC_TAG);
1010 if (!ri) {
1011 ERR("out of memory\n");
1012
1013 if (t)
1014 ExFreePool(t);
1015
1016 ExFreePool(r->nonpaged);
1017 ExFreePool(r);
1018 return STATUS_INSUFFICIENT_RESOURCES;
1019 }
1020
1021 r->id = id;
1022 r->treeholder.address = 0;
1023 r->treeholder.generation = Vcb->superblock.generation;
1024 r->treeholder.tree = t;
1025 r->lastinode = 0;
1026 r->dirty = FALSE;
1027 r->received = FALSE;
1028 r->reserved = NULL;
1029 r->parent = 0;
1030 r->send_ops = 0;
1031 RtlZeroMemory(&r->root_item, sizeof(ROOT_ITEM));
1032 r->root_item.num_references = 1;
1033 InitializeListHead(&r->fcbs);
1034
1035 RtlCopyMemory(ri, &r->root_item, sizeof(ROOT_ITEM));
1036
1037 // We ask here for a traverse_ptr to the item we're inserting, so we can
1038 // copy some of the tree's variables
1039
1040 Status = insert_tree_item(Vcb, Vcb->root_root, id, TYPE_ROOT_ITEM, offset, ri, sizeof(ROOT_ITEM), &tp, Irp);
1041 if (!NT_SUCCESS(Status)) {
1042 ERR("insert_tree_item returned %08x\n", Status);
1043 ExFreePool(ri);
1044
1045 if (t)
1046 ExFreePool(t);
1047
1048 ExFreePool(r->nonpaged);
1049 ExFreePool(r);
1050 return Status;
1051 }
1052
1053 ExInitializeResourceLite(&r->nonpaged->load_tree_lock);
1054
1055 InsertTailList(&Vcb->roots, &r->list_entry);
1056
1057 if (!no_tree) {
1058 RtlZeroMemory(&t->header, sizeof(tree_header));
1059 t->header.fs_uuid = tp.tree->header.fs_uuid;
1060 t->header.address = 0;
1061 t->header.flags = HEADER_FLAG_MIXED_BACKREF | 1; // 1 == "written"? Why does the Linux driver record this?
1062 t->header.chunk_tree_uuid = tp.tree->header.chunk_tree_uuid;
1063 t->header.generation = Vcb->superblock.generation;
1064 t->header.tree_id = id;
1065 t->header.num_items = 0;
1066 t->header.level = 0;
1067
1068 t->has_address = FALSE;
1069 t->size = 0;
1070 t->Vcb = Vcb;
1071 t->parent = NULL;
1072 t->paritem = NULL;
1073 t->root = r;
1074
1075 InitializeListHead(&t->itemlist);
1076
1077 t->new_address = 0;
1078 t->has_new_address = FALSE;
1079 t->updated_extents = FALSE;
1080
1081 InsertTailList(&Vcb->trees, &t->list_entry);
1082 t->list_entry_hash.Flink = NULL;
1083
1084 t->write = TRUE;
1085 Vcb->need_write = TRUE;
1086 }
1087
1088 *rootptr = r;
1089
1090 return STATUS_SUCCESS;
1091 }
1092
1093 static NTSTATUS set_label(_In_ device_extension* Vcb, _In_ FILE_FS_LABEL_INFORMATION* ffli) {
1094 ULONG utf8len;
1095 NTSTATUS Status;
1096 ULONG vollen, i;
1097
1098 TRACE("label = %.*S\n", ffli->VolumeLabelLength / sizeof(WCHAR), ffli->VolumeLabel);
1099
1100 vollen = ffli->VolumeLabelLength;
1101
1102 for (i = 0; i < ffli->VolumeLabelLength / sizeof(WCHAR); i++) {
1103 if (ffli->VolumeLabel[i] == 0) {
1104 vollen = i * sizeof(WCHAR);
1105 break;
1106 } else if (ffli->VolumeLabel[i] == '/' || ffli->VolumeLabel[i] == '\\') {
1107 Status = STATUS_INVALID_VOLUME_LABEL;
1108 goto end;
1109 }
1110 }
1111
1112 if (vollen == 0) {
1113 utf8len = 0;
1114 } else {
1115 Status = RtlUnicodeToUTF8N(NULL, 0, &utf8len, ffli->VolumeLabel, vollen);
1116 if (!NT_SUCCESS(Status))
1117 goto end;
1118
1119 if (utf8len > MAX_LABEL_SIZE) {
1120 Status = STATUS_INVALID_VOLUME_LABEL;
1121 goto end;
1122 }
1123 }
1124
1125 ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
1126
1127 if (utf8len > 0) {
1128 Status = RtlUnicodeToUTF8N((PCHAR)&Vcb->superblock.label, MAX_LABEL_SIZE, &utf8len, ffli->VolumeLabel, vollen);
1129 if (!NT_SUCCESS(Status))
1130 goto release;
1131 } else
1132 Status = STATUS_SUCCESS;
1133
1134 if (utf8len < MAX_LABEL_SIZE)
1135 RtlZeroMemory(Vcb->superblock.label + utf8len, MAX_LABEL_SIZE - utf8len);
1136
1137 Vcb->need_write = TRUE;
1138
1139 release:
1140 ExReleaseResourceLite(&Vcb->tree_lock);
1141
1142 end:
1143 TRACE("returning %08x\n", Status);
1144
1145 return Status;
1146 }
1147
1148 _Dispatch_type_(IRP_MJ_SET_VOLUME_INFORMATION)
1149 _Function_class_(DRIVER_DISPATCH)
1150 static NTSTATUS drv_set_volume_information(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
1151 PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
1152 device_extension* Vcb = DeviceObject->DeviceExtension;
1153 NTSTATUS Status;
1154 BOOL top_level;
1155
1156 FsRtlEnterFileSystem();
1157
1158 TRACE("set volume information\n");
1159
1160 top_level = is_top_level(Irp);
1161
1162 if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
1163 Status = vol_set_volume_information(DeviceObject, Irp);
1164 goto end;
1165 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
1166 Status = STATUS_INVALID_PARAMETER;
1167 goto end;
1168 }
1169
1170 Status = STATUS_NOT_IMPLEMENTED;
1171
1172 if (Vcb->readonly) {
1173 Status = STATUS_MEDIA_WRITE_PROTECTED;
1174 goto end;
1175 }
1176
1177 if (Vcb->removing || Vcb->locked) {
1178 Status = STATUS_ACCESS_DENIED;
1179 goto end;
1180 }
1181
1182 switch (IrpSp->Parameters.SetVolume.FsInformationClass) {
1183 case FileFsControlInformation:
1184 FIXME("STUB: FileFsControlInformation\n");
1185 break;
1186
1187 case FileFsLabelInformation:
1188 TRACE("FileFsLabelInformation\n");
1189
1190 Status = set_label(Vcb, Irp->AssociatedIrp.SystemBuffer);
1191 break;
1192
1193 case FileFsObjectIdInformation:
1194 FIXME("STUB: FileFsObjectIdInformation\n");
1195 break;
1196
1197 default:
1198 WARN("Unrecognized FsInformationClass 0x%x\n", IrpSp->Parameters.SetVolume.FsInformationClass);
1199 break;
1200 }
1201
1202 end:
1203 Irp->IoStatus.Status = Status;
1204 Irp->IoStatus.Information = 0;
1205
1206 TRACE("returning %08x\n", Status);
1207
1208 IoCompleteRequest( Irp, IO_NO_INCREMENT );
1209
1210 if (top_level)
1211 IoSetTopLevelIrp(NULL);
1212
1213 FsRtlExitFileSystem();
1214
1215 return Status;
1216 }
1217
1218 static WCHAR* file_desc_fcb(_In_ fcb* fcb) {
1219 char s[60];
1220 NTSTATUS Status;
1221 UNICODE_STRING us;
1222 ANSI_STRING as;
1223
1224 if (fcb->debug_desc)
1225 return fcb->debug_desc;
1226
1227 if (fcb == fcb->Vcb->volume_fcb)
1228 return L"volume FCB";
1229
1230 fcb->debug_desc = ExAllocatePoolWithTag(PagedPool, 60 * sizeof(WCHAR), ALLOC_TAG);
1231 if (!fcb->debug_desc)
1232 return L"(memory error)";
1233
1234 // I know this is pretty hackish...
1235 // GCC doesn't like %llx in sprintf, and MSVC won't let us use swprintf
1236 // without the CRT, which breaks drivers.
1237
1238 sprintf(s, "subvol %x, inode %x", (UINT32)fcb->subvol->id, (UINT32)fcb->inode);
1239
1240 as.Buffer = s;
1241 as.Length = as.MaximumLength = (USHORT)strlen(s);
1242
1243 us.Buffer = fcb->debug_desc;
1244 us.MaximumLength = 60 * sizeof(WCHAR);
1245 us.Length = 0;
1246
1247 Status = RtlAnsiStringToUnicodeString(&us, &as, FALSE);
1248 if (!NT_SUCCESS(Status))
1249 return L"(RtlAnsiStringToUnicodeString error)";
1250
1251 us.Buffer[us.Length / sizeof(WCHAR)] = 0;
1252
1253 return fcb->debug_desc;
1254 }
1255
1256 WCHAR* file_desc_fileref(_In_ file_ref* fileref) {
1257 NTSTATUS Status;
1258 UNICODE_STRING fn;
1259 ULONG reqlen;
1260
1261 if (fileref->debug_desc)
1262 return fileref->debug_desc;
1263
1264 fn.Length = fn.MaximumLength = 0;
1265 Status = fileref_get_filename(fileref, &fn, NULL, &reqlen);
1266 if (Status != STATUS_BUFFER_OVERFLOW)
1267 return L"ERROR";
1268
1269 if (reqlen > 0xffff - sizeof(WCHAR))
1270 return L"(too long)";
1271
1272 fileref->debug_desc = ExAllocatePoolWithTag(PagedPool, reqlen + sizeof(WCHAR), ALLOC_TAG);
1273 if (!fileref->debug_desc)
1274 return L"(memory error)";
1275
1276 fn.Buffer = fileref->debug_desc;
1277 fn.Length = 0;
1278 fn.MaximumLength = (USHORT)(reqlen + sizeof(WCHAR));
1279
1280 Status = fileref_get_filename(fileref, &fn, NULL, &reqlen);
1281 if (!NT_SUCCESS(Status)) {
1282 ExFreePool(fileref->debug_desc);
1283 fileref->debug_desc = NULL;
1284 return L"ERROR";
1285 }
1286
1287 fileref->debug_desc[fn.Length / sizeof(WCHAR)] = 0;
1288
1289 return fileref->debug_desc;
1290 }
1291
1292 _Ret_z_
1293 WCHAR* file_desc(_In_ PFILE_OBJECT FileObject) {
1294 fcb* fcb = FileObject->FsContext;
1295 ccb* ccb = FileObject->FsContext2;
1296 file_ref* fileref = ccb ? ccb->fileref : NULL;
1297
1298 if (fileref)
1299 return file_desc_fileref(fileref);
1300 else
1301 return file_desc_fcb(fcb);
1302 }
1303
1304 void send_notification_fileref(_In_ file_ref* fileref, _In_ ULONG filter_match, _In_ ULONG action, _In_opt_ PUNICODE_STRING stream) {
1305 UNICODE_STRING fn;
1306 NTSTATUS Status;
1307 ULONG reqlen;
1308 USHORT name_offset;
1309 fcb* fcb = fileref->fcb;
1310
1311 fn.Length = fn.MaximumLength = 0;
1312 Status = fileref_get_filename(fileref, &fn, NULL, &reqlen);
1313 if (Status != STATUS_BUFFER_OVERFLOW) {
1314 ERR("fileref_get_filename returned %08x\n", Status);
1315 return;
1316 }
1317
1318 if (reqlen > 0xffff) {
1319 WARN("reqlen was too long for FsRtlNotifyFilterReportChange\n");
1320 return;
1321 }
1322
1323 fn.Buffer = ExAllocatePoolWithTag(PagedPool, reqlen, ALLOC_TAG);
1324 if (!fn.Buffer) {
1325 ERR("out of memory\n");
1326 return;
1327 }
1328
1329 fn.MaximumLength = (USHORT)reqlen;
1330 fn.Length = 0;
1331
1332 Status = fileref_get_filename(fileref, &fn, &name_offset, &reqlen);
1333 if (!NT_SUCCESS(Status)) {
1334 ERR("fileref_get_filename returned %08x\n", Status);
1335 ExFreePool(fn.Buffer);
1336 return;
1337 }
1338
1339 FsRtlNotifyFilterReportChange(fcb->Vcb->NotifySync, &fcb->Vcb->DirNotifyList, (PSTRING)&fn, name_offset,
1340 (PSTRING)stream, NULL, filter_match, action, NULL, NULL);
1341 ExFreePool(fn.Buffer);
1342 }
1343
1344 void send_notification_fcb(_In_ file_ref* fileref, _In_ ULONG filter_match, _In_ ULONG action, _In_opt_ PUNICODE_STRING stream) {
1345 fcb* fcb = fileref->fcb;
1346 LIST_ENTRY* le;
1347 NTSTATUS Status;
1348
1349 // no point looking for hardlinks if st_nlink == 1
1350 if (fileref->fcb->inode_item.st_nlink == 1) {
1351 send_notification_fileref(fileref, filter_match, action, stream);
1352 return;
1353 }
1354
1355 ExAcquireResourceExclusiveLite(&fcb->Vcb->fcb_lock, TRUE);
1356
1357 le = fcb->hardlinks.Flink;
1358 while (le != &fcb->hardlinks) {
1359 hardlink* hl = CONTAINING_RECORD(le, hardlink, list_entry);
1360 file_ref* parfr;
1361
1362 Status = open_fileref_by_inode(fcb->Vcb, fcb->subvol, hl->parent, &parfr, NULL);
1363
1364 if (!NT_SUCCESS(Status))
1365 ERR("open_fileref_by_inode returned %08x\n", Status);
1366 else if (!parfr->deleted) {
1367 UNICODE_STRING fn;
1368 ULONG pathlen;
1369
1370 fn.Length = fn.MaximumLength = 0;
1371 Status = fileref_get_filename(parfr, &fn, NULL, &pathlen);
1372 if (Status != STATUS_BUFFER_OVERFLOW) {
1373 ERR("fileref_get_filename returned %08x\n", Status);
1374 free_fileref(fcb->Vcb, parfr);
1375 break;
1376 }
1377
1378 if (parfr != fcb->Vcb->root_fileref)
1379 pathlen += sizeof(WCHAR);
1380
1381 if (pathlen + hl->name.Length > 0xffff) {
1382 WARN("pathlen + hl->name.Length was too long for FsRtlNotifyFilterReportChange\n");
1383 free_fileref(fcb->Vcb, parfr);
1384 break;
1385 }
1386
1387 fn.MaximumLength = (USHORT)(pathlen + hl->name.Length);
1388 fn.Buffer = ExAllocatePoolWithTag(PagedPool, fn.MaximumLength, ALLOC_TAG);
1389 if (!fn.Buffer) {
1390 ERR("out of memory\n");
1391 free_fileref(fcb->Vcb, parfr);
1392 break;
1393 }
1394
1395 Status = fileref_get_filename(parfr, &fn, NULL, NULL);
1396 if (!NT_SUCCESS(Status)) {
1397 ERR("fileref_get_filename returned %08x\n", Status);
1398 free_fileref(fcb->Vcb, parfr);
1399 ExFreePool(fn.Buffer);
1400 break;
1401 }
1402
1403 if (parfr != fcb->Vcb->root_fileref) {
1404 fn.Buffer[(pathlen / sizeof(WCHAR)) - 1] = '\\';
1405 fn.Length += sizeof(WCHAR);
1406 }
1407
1408 RtlCopyMemory(&fn.Buffer[pathlen / sizeof(WCHAR)], hl->name.Buffer, hl->name.Length);
1409 fn.Length += hl->name.Length;
1410
1411 FsRtlNotifyFilterReportChange(fcb->Vcb->NotifySync, &fcb->Vcb->DirNotifyList, (PSTRING)&fn, (USHORT)pathlen,
1412 (PSTRING)stream, NULL, filter_match, action, NULL, NULL);
1413
1414 ExFreePool(fn.Buffer);
1415
1416 free_fileref(fcb->Vcb, parfr);
1417 }
1418
1419 le = le->Flink;
1420 }
1421
1422 ExReleaseResourceLite(&fcb->Vcb->fcb_lock);
1423 }
1424
1425 void mark_fcb_dirty(_In_ fcb* fcb) {
1426 if (!fcb->dirty) {
1427 #ifdef DEBUG_FCB_REFCOUNTS
1428 LONG rc;
1429 #endif
1430 fcb->dirty = TRUE;
1431
1432 #ifdef DEBUG_FCB_REFCOUNTS
1433 rc = InterlockedIncrement(&fcb->refcount);
1434 WARN("fcb %p: refcount now %i\n", fcb, rc);
1435 #else
1436 InterlockedIncrement(&fcb->refcount);
1437 #endif
1438
1439 ExAcquireResourceExclusiveLite(&fcb->Vcb->dirty_fcbs_lock, TRUE);
1440 InsertTailList(&fcb->Vcb->dirty_fcbs, &fcb->list_entry_dirty);
1441 ExReleaseResourceLite(&fcb->Vcb->dirty_fcbs_lock);
1442 }
1443
1444 fcb->Vcb->need_write = TRUE;
1445 }
1446
1447 void mark_fileref_dirty(_In_ file_ref* fileref) {
1448 if (!fileref->dirty) {
1449 fileref->dirty = TRUE;
1450 increase_fileref_refcount(fileref);
1451
1452 ExAcquireResourceExclusiveLite(&fileref->fcb->Vcb->dirty_filerefs_lock, TRUE);
1453 InsertTailList(&fileref->fcb->Vcb->dirty_filerefs, &fileref->list_entry_dirty);
1454 ExReleaseResourceLite(&fileref->fcb->Vcb->dirty_filerefs_lock);
1455 }
1456
1457 fileref->fcb->Vcb->need_write = TRUE;
1458 }
1459
1460 #ifdef DEBUG_FCB_REFCOUNTS
1461 void _free_fcb(_Requires_exclusive_lock_held_(_Curr_->fcb_lock) _In_ device_extension* Vcb, _Inout_ fcb* fcb, _In_ const char* func) {
1462 #else
1463 void free_fcb(_Requires_exclusive_lock_held_(_Curr_->fcb_lock) _In_ device_extension* Vcb, _Inout_ fcb* fcb) {
1464 #endif
1465 LONG rc;
1466
1467 rc = InterlockedDecrement(&fcb->refcount);
1468
1469 #ifdef DEBUG_FCB_REFCOUNTS
1470 #ifdef DEBUG_LONG_MESSAGES
1471 ERR("fcb %p: refcount now %i (subvol %llx, inode %llx)\n", fcb, rc, fcb->subvol ? fcb->subvol->id : 0, fcb->inode);
1472 #else
1473 ERR("fcb %p: refcount now %i (subvol %llx, inode %llx)\n", fcb, rc, fcb->subvol ? fcb->subvol->id : 0, fcb->inode);
1474 #endif
1475 #endif
1476
1477 if (rc > 0)
1478 return;
1479
1480 if (fcb->list_entry.Flink)
1481 RemoveEntryList(&fcb->list_entry);
1482
1483 if (fcb->list_entry_all.Flink)
1484 RemoveEntryList(&fcb->list_entry_all);
1485
1486 ExDeleteResourceLite(&fcb->nonpaged->resource);
1487 ExDeleteResourceLite(&fcb->nonpaged->paging_resource);
1488 ExDeleteResourceLite(&fcb->nonpaged->dir_children_lock);
1489
1490 ExFreeToNPagedLookasideList(&Vcb->fcb_np_lookaside, fcb->nonpaged);
1491
1492 if (fcb->sd)
1493 ExFreePool(fcb->sd);
1494
1495 if (fcb->adsxattr.Buffer)
1496 ExFreePool(fcb->adsxattr.Buffer);
1497
1498 if (fcb->reparse_xattr.Buffer)
1499 ExFreePool(fcb->reparse_xattr.Buffer);
1500
1501 if (fcb->ea_xattr.Buffer)
1502 ExFreePool(fcb->ea_xattr.Buffer);
1503
1504 if (fcb->adsdata.Buffer)
1505 ExFreePool(fcb->adsdata.Buffer);
1506
1507 if (fcb->debug_desc)
1508 ExFreePool(fcb->debug_desc);
1509
1510 while (!IsListEmpty(&fcb->extents)) {
1511 LIST_ENTRY* le = RemoveHeadList(&fcb->extents);
1512 extent* ext = CONTAINING_RECORD(le, extent, list_entry);
1513
1514 if (ext->csum)
1515 ExFreePool(ext->csum);
1516
1517 ExFreePool(ext);
1518 }
1519
1520 while (!IsListEmpty(&fcb->hardlinks)) {
1521 LIST_ENTRY* le = RemoveHeadList(&fcb->hardlinks);
1522 hardlink* hl = CONTAINING_RECORD(le, hardlink, list_entry);
1523
1524 if (hl->name.Buffer)
1525 ExFreePool(hl->name.Buffer);
1526
1527 if (hl->utf8.Buffer)
1528 ExFreePool(hl->utf8.Buffer);
1529
1530 ExFreePool(hl);
1531 }
1532
1533 while (!IsListEmpty(&fcb->xattrs)) {
1534 xattr* xa = CONTAINING_RECORD(RemoveHeadList(&fcb->xattrs), xattr, list_entry);
1535
1536 ExFreePool(xa);
1537 }
1538
1539 while (!IsListEmpty(&fcb->dir_children_index)) {
1540 LIST_ENTRY* le = RemoveHeadList(&fcb->dir_children_index);
1541 dir_child* dc = CONTAINING_RECORD(le, dir_child, list_entry_index);
1542
1543 ExFreePool(dc->utf8.Buffer);
1544 ExFreePool(dc->name.Buffer);
1545 ExFreePool(dc->name_uc.Buffer);
1546 ExFreePool(dc);
1547 }
1548
1549 if (fcb->hash_ptrs)
1550 ExFreePool(fcb->hash_ptrs);
1551
1552 if (fcb->hash_ptrs_uc)
1553 ExFreePool(fcb->hash_ptrs_uc);
1554
1555 FsRtlUninitializeFileLock(&fcb->lock);
1556
1557 if (fcb->pool_type == NonPagedPool)
1558 ExFreePool(fcb);
1559 else
1560 ExFreeToPagedLookasideList(&Vcb->fcb_lookaside, fcb);
1561
1562 #ifdef DEBUG_FCB_REFCOUNTS
1563 #ifdef DEBUG_LONG_MESSAGES
1564 _debug_message(func, file, line, "freeing fcb %p\n", fcb);
1565 #else
1566 _debug_message(func, "freeing fcb %p\n", fcb);
1567 #endif
1568 #endif
1569 }
1570
1571 void free_fileref(_Requires_exclusive_lock_held_(_Curr_->fcb_lock) _In_ device_extension* Vcb, _Inout_ file_ref* fr) {
1572 LONG rc;
1573
1574 rc = InterlockedDecrement(&fr->refcount);
1575
1576 #ifdef DEBUG_FCB_REFCOUNTS
1577 ERR("fileref %p: refcount now %i\n", fr, rc);
1578 #endif
1579
1580 #ifdef _DEBUG
1581 if (rc < 0) {
1582 ERR("fileref %p: refcount now %i\n", fr, rc);
1583 int3;
1584 }
1585 #endif
1586
1587 if (rc > 0)
1588 return;
1589
1590 if (fr->parent)
1591 ExAcquireResourceExclusiveLite(&fr->parent->nonpaged->children_lock, TRUE);
1592
1593 // FIXME - do we need a file_ref lock?
1594
1595 // FIXME - do delete if needed
1596
1597 if (fr->debug_desc)
1598 ExFreePool(fr->debug_desc);
1599
1600 ExDeleteResourceLite(&fr->nonpaged->children_lock);
1601 ExDeleteResourceLite(&fr->nonpaged->fileref_lock);
1602
1603 ExFreeToNPagedLookasideList(&Vcb->fileref_np_lookaside, fr->nonpaged);
1604
1605 // FIXME - throw error if children not empty
1606
1607 if (fr->fcb->fileref == fr)
1608 fr->fcb->fileref = NULL;
1609
1610 if (fr->dc) {
1611 if (fr->fcb->ads)
1612 fr->dc->size = fr->fcb->adsdata.Length;
1613
1614 fr->dc->fileref = NULL;
1615 }
1616
1617 if (fr->list_entry.Flink)
1618 RemoveEntryList(&fr->list_entry);
1619
1620 if (fr->parent) {
1621 ExReleaseResourceLite(&fr->parent->nonpaged->children_lock);
1622 free_fileref(Vcb, fr->parent);
1623 }
1624
1625 free_fcb(Vcb, fr->fcb);
1626
1627 ExFreeToPagedLookasideList(&Vcb->fileref_lookaside, fr);
1628 }
1629
1630 static NTSTATUS close_file(_In_ PFILE_OBJECT FileObject, _In_ PIRP Irp) {
1631 fcb* fcb;
1632 ccb* ccb;
1633 file_ref* fileref = NULL;
1634 LONG open_files;
1635 device_extension* Vcb;
1636
1637 UNUSED(Irp);
1638
1639 TRACE("FileObject = %p\n", FileObject);
1640
1641 fcb = FileObject->FsContext;
1642 if (!fcb) {
1643 TRACE("FCB was NULL, returning success\n");
1644 return STATUS_SUCCESS;
1645 }
1646
1647 open_files = InterlockedDecrement(&fcb->Vcb->open_files);
1648
1649 ccb = FileObject->FsContext2;
1650
1651 TRACE("close called for %S (fcb == %p)\n", file_desc(FileObject), fcb);
1652
1653 // FIXME - make sure notification gets sent if file is being deleted
1654
1655 if (ccb) {
1656 if (ccb->query_string.Buffer)
1657 RtlFreeUnicodeString(&ccb->query_string);
1658
1659 if (ccb->filename.Buffer)
1660 ExFreePool(ccb->filename.Buffer);
1661
1662 // FIXME - use refcounts for fileref
1663 fileref = ccb->fileref;
1664
1665 if (fcb->Vcb->running_sends > 0) {
1666 BOOL send_cancelled = FALSE;
1667
1668 ExAcquireResourceExclusiveLite(&fcb->Vcb->send_load_lock, TRUE);
1669
1670 if (ccb->send) {
1671 ccb->send->cancelling = TRUE;
1672 send_cancelled = TRUE;
1673 KeSetEvent(&ccb->send->cleared_event, 0, FALSE);
1674 }
1675
1676 ExReleaseResourceLite(&fcb->Vcb->send_load_lock);
1677
1678 if (send_cancelled) {
1679 while (ccb->send) {
1680 ExAcquireResourceExclusiveLite(&fcb->Vcb->send_load_lock, TRUE);
1681 ExReleaseResourceLite(&fcb->Vcb->send_load_lock);
1682 }
1683 }
1684 }
1685
1686 ExFreePool(ccb);
1687 }
1688
1689 CcUninitializeCacheMap(FileObject, NULL, NULL);
1690
1691 if (open_files == 0 && fcb->Vcb->removing) {
1692 uninit(fcb->Vcb, FALSE);
1693 return STATUS_SUCCESS;
1694 }
1695
1696 if (!(fcb->Vcb->Vpb->Flags & VPB_MOUNTED))
1697 return STATUS_SUCCESS;
1698
1699 Vcb = fcb->Vcb;
1700
1701 ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
1702
1703 if (fileref)
1704 free_fileref(fcb->Vcb, fileref);
1705 else
1706 free_fcb(Vcb, fcb);
1707
1708 ExReleaseResourceLite(&Vcb->fcb_lock);
1709
1710 return STATUS_SUCCESS;
1711 }
1712
1713 void uninit(_In_ device_extension* Vcb, _In_ BOOL flush) {
1714 UINT64 i;
1715 NTSTATUS Status;
1716 LIST_ENTRY* le;
1717 LARGE_INTEGER time;
1718
1719 if (!Vcb->removing) {
1720 ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
1721 Vcb->removing = TRUE;
1722 ExReleaseResourceLite(&Vcb->tree_lock);
1723 }
1724
1725 RemoveEntryList(&Vcb->list_entry);
1726
1727 if (Vcb->balance.thread) {
1728 Vcb->balance.paused = FALSE;
1729 Vcb->balance.stopping = TRUE;
1730 KeSetEvent(&Vcb->balance.event, 0, FALSE);
1731 KeWaitForSingleObject(&Vcb->balance.finished, Executive, KernelMode, FALSE, NULL);
1732 }
1733
1734 if (Vcb->scrub.thread) {
1735 Vcb->scrub.paused = FALSE;
1736 Vcb->scrub.stopping = TRUE;
1737 KeSetEvent(&Vcb->scrub.event, 0, FALSE);
1738 KeWaitForSingleObject(&Vcb->scrub.finished, Executive, KernelMode, FALSE, NULL);
1739 }
1740
1741 if (Vcb->running_sends != 0) {
1742 BOOL send_cancelled = FALSE;
1743
1744 ExAcquireResourceExclusiveLite(&Vcb->send_load_lock, TRUE);
1745
1746 le = Vcb->send_ops.Flink;
1747 while (le != &Vcb->send_ops) {
1748 send_info* send = CONTAINING_RECORD(le, send_info, list_entry);
1749
1750 if (!send->cancelling) {
1751 send->cancelling = TRUE;
1752 send_cancelled = TRUE;
1753 send->ccb = NULL;
1754 KeSetEvent(&send->cleared_event, 0, FALSE);
1755 }
1756
1757 le = le->Flink;
1758 }
1759
1760 ExReleaseResourceLite(&Vcb->send_load_lock);
1761
1762 if (send_cancelled) {
1763 while (Vcb->running_sends != 0) {
1764 ExAcquireResourceExclusiveLite(&Vcb->send_load_lock, TRUE);
1765 ExReleaseResourceLite(&Vcb->send_load_lock);
1766 }
1767 }
1768 }
1769
1770 Status = registry_mark_volume_unmounted(&Vcb->superblock.uuid);
1771 if (!NT_SUCCESS(Status) && Status != STATUS_TOO_LATE)
1772 WARN("registry_mark_volume_unmounted returned %08x\n", Status);
1773
1774 if (flush) {
1775 ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
1776
1777 if (Vcb->need_write && !Vcb->readonly) {
1778 Status = do_write(Vcb, NULL);
1779 if (!NT_SUCCESS(Status))
1780 ERR("do_write returned %08x\n", Status);
1781 }
1782
1783 free_trees(Vcb);
1784
1785 ExReleaseResourceLite(&Vcb->tree_lock);
1786 }
1787
1788 for (i = 0; i < Vcb->calcthreads.num_threads; i++) {
1789 Vcb->calcthreads.threads[i].quit = TRUE;
1790 }
1791
1792 KeSetEvent(&Vcb->calcthreads.event, 0, FALSE);
1793
1794 for (i = 0; i < Vcb->calcthreads.num_threads; i++) {
1795 KeWaitForSingleObject(&Vcb->calcthreads.threads[i].finished, Executive, KernelMode, FALSE, NULL);
1796
1797 ZwClose(Vcb->calcthreads.threads[i].handle);
1798 }
1799
1800 ExDeleteResourceLite(&Vcb->calcthreads.lock);
1801 ExFreePool(Vcb->calcthreads.threads);
1802
1803 time.QuadPart = 0;
1804 KeSetTimer(&Vcb->flush_thread_timer, time, NULL); // trigger the timer early
1805 KeWaitForSingleObject(&Vcb->flush_thread_finished, Executive, KernelMode, FALSE, NULL);
1806
1807 ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
1808 free_fcb(Vcb, Vcb->volume_fcb);
1809 free_fcb(Vcb, Vcb->dummy_fcb);
1810 ExReleaseResourceLite(&Vcb->fcb_lock);
1811
1812 if (Vcb->root_file)
1813 ObDereferenceObject(Vcb->root_file);
1814
1815 le = Vcb->chunks.Flink;
1816 while (le != &Vcb->chunks) {
1817 chunk* c = CONTAINING_RECORD(le, chunk, list_entry);
1818
1819 if (c->cache) {
1820 ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
1821 free_fcb(Vcb, c->cache);
1822 ExReleaseResourceLite(&Vcb->fcb_lock);
1823 c->cache = NULL;
1824 }
1825
1826 le = le->Flink;
1827 }
1828
1829 while (!IsListEmpty(&Vcb->roots)) {
1830 root* r = CONTAINING_RECORD(RemoveHeadList(&Vcb->roots), root, list_entry);
1831
1832 ExDeleteResourceLite(&r->nonpaged->load_tree_lock);
1833 ExFreePool(r->nonpaged);
1834 ExFreePool(r);
1835 }
1836
1837 while (!IsListEmpty(&Vcb->chunks)) {
1838 chunk* c = CONTAINING_RECORD(RemoveHeadList(&Vcb->chunks), chunk, list_entry);
1839
1840 while (!IsListEmpty(&c->space)) {
1841 LIST_ENTRY* le2 = RemoveHeadList(&c->space);
1842 space* s = CONTAINING_RECORD(le2, space, list_entry);
1843
1844 ExFreePool(s);
1845 }
1846
1847 while (!IsListEmpty(&c->deleting)) {
1848 LIST_ENTRY* le2 = RemoveHeadList(&c->deleting);
1849 space* s = CONTAINING_RECORD(le2, space, list_entry);
1850
1851 ExFreePool(s);
1852 }
1853
1854 if (c->devices)
1855 ExFreePool(c->devices);
1856
1857 if (c->cache) {
1858 ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
1859 free_fcb(Vcb, c->cache);
1860 ExReleaseResourceLite(&Vcb->fcb_lock);
1861 }
1862
1863 ExDeleteResourceLite(&c->range_locks_lock);
1864 ExDeleteResourceLite(&c->partial_stripes_lock);
1865 ExDeleteResourceLite(&c->lock);
1866 ExDeleteResourceLite(&c->changed_extents_lock);
1867
1868 ExFreePool(c->chunk_item);
1869 ExFreePool(c);
1870 }
1871
1872 // FIXME - free any open fcbs?
1873
1874 while (!IsListEmpty(&Vcb->devices)) {
1875 device* dev = CONTAINING_RECORD(RemoveHeadList(&Vcb->devices), device, list_entry);
1876
1877 while (!IsListEmpty(&dev->space)) {
1878 LIST_ENTRY* le2 = RemoveHeadList(&dev->space);
1879 space* s = CONTAINING_RECORD(le2, space, list_entry);
1880
1881 ExFreePool(s);
1882 }
1883
1884 ExFreePool(dev);
1885 }
1886
1887 ExAcquireResourceExclusiveLite(&Vcb->scrub.stats_lock, TRUE);
1888 while (!IsListEmpty(&Vcb->scrub.errors)) {
1889 scrub_error* err = CONTAINING_RECORD(RemoveHeadList(&Vcb->scrub.errors), scrub_error, list_entry);
1890
1891 ExFreePool(err);
1892 }
1893 ExReleaseResourceLite(&Vcb->scrub.stats_lock);
1894
1895 ExDeleteResourceLite(&Vcb->fcb_lock);
1896 ExDeleteResourceLite(&Vcb->load_lock);
1897 ExDeleteResourceLite(&Vcb->tree_lock);
1898 ExDeleteResourceLite(&Vcb->chunk_lock);
1899 ExDeleteResourceLite(&Vcb->dirty_fcbs_lock);
1900 ExDeleteResourceLite(&Vcb->dirty_filerefs_lock);
1901 ExDeleteResourceLite(&Vcb->dirty_subvols_lock);
1902 ExDeleteResourceLite(&Vcb->scrub.stats_lock);
1903 ExDeleteResourceLite(&Vcb->send_load_lock);
1904
1905 ExDeletePagedLookasideList(&Vcb->tree_data_lookaside);
1906 ExDeletePagedLookasideList(&Vcb->traverse_ptr_lookaside);
1907 ExDeletePagedLookasideList(&Vcb->batch_item_lookaside);
1908 ExDeletePagedLookasideList(&Vcb->fileref_lookaside);
1909 ExDeletePagedLookasideList(&Vcb->fcb_lookaside);
1910 ExDeletePagedLookasideList(&Vcb->name_bit_lookaside);
1911 ExDeleteNPagedLookasideList(&Vcb->range_lock_lookaside);
1912 ExDeleteNPagedLookasideList(&Vcb->fileref_np_lookaside);
1913 ExDeleteNPagedLookasideList(&Vcb->fcb_np_lookaside);
1914
1915 ZwClose(Vcb->flush_thread_handle);
1916 }
1917
1918 NTSTATUS delete_fileref(_In_ file_ref* fileref, _In_ PFILE_OBJECT FileObject, _In_opt_ PIRP Irp, _In_ LIST_ENTRY* rollback) {
1919 LARGE_INTEGER newlength, time;
1920 BTRFS_TIME now;
1921 NTSTATUS Status;
1922 ULONG utf8len = 0;
1923
1924 KeQuerySystemTime(&time);
1925 win_time_to_unix(time, &now);
1926
1927 ExAcquireResourceExclusiveLite(fileref->fcb->Header.Resource, TRUE);
1928
1929 if (fileref->deleted) {
1930 ExReleaseResourceLite(fileref->fcb->Header.Resource);
1931 return STATUS_SUCCESS;
1932 }
1933
1934 if (fileref->fcb->subvol->send_ops > 0) {
1935 ExReleaseResourceLite(fileref->fcb->Header.Resource);
1936 return STATUS_ACCESS_DENIED;
1937 }
1938
1939 fileref->deleted = TRUE;
1940 mark_fileref_dirty(fileref);
1941
1942 // delete INODE_ITEM (0x1)
1943
1944 TRACE("nlink = %u\n", fileref->fcb->inode_item.st_nlink);
1945
1946 if (!fileref->fcb->ads) {
1947 if (fileref->parent->fcb->subvol == fileref->fcb->subvol) {
1948 LIST_ENTRY* le;
1949
1950 mark_fcb_dirty(fileref->fcb);
1951
1952 fileref->fcb->inode_item_changed = TRUE;
1953
1954 if (fileref->fcb->inode_item.st_nlink > 1) {
1955 fileref->fcb->inode_item.st_nlink--;
1956 fileref->fcb->inode_item.transid = fileref->fcb->Vcb->superblock.generation;
1957 fileref->fcb->inode_item.sequence++;
1958 fileref->fcb->inode_item.st_ctime = now;
1959 } else {
1960 // excise extents
1961
1962 if (fileref->fcb->type != BTRFS_TYPE_DIRECTORY && fileref->fcb->inode_item.st_size > 0) {
1963 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);
1964 if (!NT_SUCCESS(Status)) {
1965 ERR("excise_extents returned %08x\n", Status);
1966 ExReleaseResourceLite(fileref->fcb->Header.Resource);
1967 return Status;
1968 }
1969 }
1970
1971 fileref->fcb->Header.AllocationSize.QuadPart = 0;
1972 fileref->fcb->Header.FileSize.QuadPart = 0;
1973 fileref->fcb->Header.ValidDataLength.QuadPart = 0;
1974
1975 if (FileObject) {
1976 CC_FILE_SIZES ccfs;
1977
1978 ccfs.AllocationSize = fileref->fcb->Header.AllocationSize;
1979 ccfs.FileSize = fileref->fcb->Header.FileSize;
1980 ccfs.ValidDataLength = fileref->fcb->Header.ValidDataLength;
1981
1982 Status = STATUS_SUCCESS;
1983
1984 _SEH2_TRY {
1985 CcSetFileSizes(FileObject, &ccfs);
1986 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
1987 Status = _SEH2_GetExceptionCode();
1988 } _SEH2_END;
1989
1990 if (!NT_SUCCESS(Status)) {
1991 ERR("CcSetFileSizes threw exception %08x\n", Status);
1992 ExReleaseResourceLite(fileref->fcb->Header.Resource);
1993 return Status;
1994 }
1995 }
1996
1997 fileref->fcb->deleted = TRUE;
1998 }
1999
2000 if (fileref->dc) {
2001 le = fileref->fcb->hardlinks.Flink;
2002 while (le != &fileref->fcb->hardlinks) {
2003 hardlink* hl = CONTAINING_RECORD(le, hardlink, list_entry);
2004
2005 if (hl->parent == fileref->parent->fcb->inode && hl->index == fileref->dc->index) {
2006 RemoveEntryList(&hl->list_entry);
2007
2008 if (hl->name.Buffer)
2009 ExFreePool(hl->name.Buffer);
2010
2011 if (hl->utf8.Buffer)
2012 ExFreePool(hl->utf8.Buffer);
2013
2014 ExFreePool(hl);
2015 break;
2016 }
2017
2018 le = le->Flink;
2019 }
2020 }
2021 } else if (fileref->fcb->subvol->parent == fileref->parent->fcb->subvol->id) { // valid subvolume
2022 if (fileref->fcb->subvol->root_item.num_references > 1) {
2023 fileref->fcb->subvol->root_item.num_references--;
2024
2025 mark_fcb_dirty(fileref->fcb); // so ROOT_ITEM gets updated
2026 } else {
2027 // FIXME - we need a lock here
2028
2029 RemoveEntryList(&fileref->fcb->subvol->list_entry);
2030
2031 InsertTailList(&fileref->fcb->Vcb->drop_roots, &fileref->fcb->subvol->list_entry);
2032 }
2033 }
2034 } else {
2035 fileref->fcb->deleted = TRUE;
2036 mark_fcb_dirty(fileref->fcb);
2037 }
2038
2039 // remove dir_child from parent
2040
2041 if (fileref->dc) {
2042 TRACE("delete file %.*S\n", fileref->dc->name.Length / sizeof(WCHAR), fileref->dc->name.Buffer);
2043
2044 ExAcquireResourceExclusiveLite(&fileref->parent->fcb->nonpaged->dir_children_lock, TRUE);
2045 RemoveEntryList(&fileref->dc->list_entry_index);
2046
2047 if (!fileref->fcb->ads)
2048 remove_dir_child_from_hash_lists(fileref->parent->fcb, fileref->dc);
2049
2050 ExReleaseResourceLite(&fileref->parent->fcb->nonpaged->dir_children_lock);
2051
2052 if (!fileref->oldutf8.Buffer)
2053 fileref->oldutf8 = fileref->dc->utf8;
2054 else
2055 ExFreePool(fileref->dc->utf8.Buffer);
2056
2057 utf8len = fileref->dc->utf8.Length;
2058
2059 fileref->oldindex = fileref->dc->index;
2060
2061 ExFreePool(fileref->dc->name.Buffer);
2062 ExFreePool(fileref->dc->name_uc.Buffer);
2063 ExFreePool(fileref->dc);
2064
2065 fileref->dc = NULL;
2066 }
2067
2068 // update INODE_ITEM of parent
2069
2070 ExAcquireResourceExclusiveLite(fileref->parent->fcb->Header.Resource, TRUE);
2071
2072 fileref->parent->fcb->inode_item.transid = fileref->fcb->Vcb->superblock.generation;
2073 fileref->parent->fcb->inode_item.sequence++;
2074 fileref->parent->fcb->inode_item.st_ctime = now;
2075
2076 if (!fileref->fcb->ads) {
2077 TRACE("fileref->parent->fcb->inode_item.st_size (inode %llx) was %llx\n", fileref->parent->fcb->inode, fileref->parent->fcb->inode_item.st_size);
2078 fileref->parent->fcb->inode_item.st_size -= utf8len * 2;
2079 TRACE("fileref->parent->fcb->inode_item.st_size (inode %llx) now %llx\n", fileref->parent->fcb->inode, fileref->parent->fcb->inode_item.st_size);
2080 fileref->parent->fcb->inode_item.st_mtime = now;
2081 }
2082
2083 fileref->parent->fcb->inode_item_changed = TRUE;
2084 ExReleaseResourceLite(fileref->parent->fcb->Header.Resource);
2085
2086 if (!fileref->fcb->ads && fileref->parent->dc)
2087 send_notification_fcb(fileref->parent, FILE_NOTIFY_CHANGE_LAST_WRITE, FILE_ACTION_MODIFIED, NULL);
2088
2089 mark_fcb_dirty(fileref->parent->fcb);
2090
2091 fileref->fcb->subvol->root_item.ctransid = fileref->fcb->Vcb->superblock.generation;
2092 fileref->fcb->subvol->root_item.ctime = now;
2093
2094 newlength.QuadPart = 0;
2095
2096 if (FileObject && !CcUninitializeCacheMap(FileObject, &newlength, NULL))
2097 TRACE("CcUninitializeCacheMap failed\n");
2098
2099 ExReleaseResourceLite(fileref->fcb->Header.Resource);
2100
2101 return STATUS_SUCCESS;
2102 }
2103
2104 _Dispatch_type_(IRP_MJ_CLEANUP)
2105 _Function_class_(DRIVER_DISPATCH)
2106 static NTSTATUS drv_cleanup(_In_ PDEVICE_OBJECT DeviceObject, _In_ PIRP Irp) {
2107 NTSTATUS Status;
2108 PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
2109 PFILE_OBJECT FileObject = IrpSp->FileObject;
2110 device_extension* Vcb = DeviceObject->DeviceExtension;
2111 fcb* fcb = FileObject->FsContext;
2112 BOOL top_level;
2113
2114 FsRtlEnterFileSystem();
2115
2116 TRACE("cleanup\n");
2117
2118 top_level = is_top_level(Irp);
2119
2120 if (Vcb && Vcb->type == VCB_TYPE_VOLUME) {
2121 Status = vol_cleanup(DeviceObject, Irp);
2122 goto exit;
2123 } else if (DeviceObject == master_devobj) {
2124 TRACE("closing file system\n");
2125 Status = STATUS_SUCCESS;
2126 goto exit;
2127 } else if (!Vcb || Vcb->type != VCB_TYPE_FS) {
2128 Status = STATUS_INVALID_PARAMETER;
2129 goto exit;
2130 }
2131
2132 if (FileObject->Flags & FO_CLEANUP_COMPLETE) {
2133 TRACE("FileObject %p already cleaned up\n", FileObject);
2134 Status = STATUS_SUCCESS;
2135 goto exit;
2136 }
2137
2138 if (!fcb) {
2139 ERR("fcb was NULL\n");
2140 Status = STATUS_INVALID_PARAMETER;
2141 goto exit;
2142 }
2143
2144 // We have to use the pointer to Vcb stored in the fcb, as we can receive cleanup
2145 // messages belonging to other devices.
2146
2147 if (FileObject && FileObject->FsContext) {
2148 LONG oc;
2149 ccb* ccb;
2150 file_ref* fileref;
2151 BOOL locked = TRUE;
2152
2153 ccb = FileObject->FsContext2;
2154 fileref = ccb ? ccb->fileref : NULL;
2155
2156 TRACE("cleanup called for FileObject %p\n", FileObject);
2157 TRACE("fileref %p (%S), refcount = %u, open_count = %u\n", fileref, file_desc(FileObject), fileref ? fileref->refcount : 0, fileref ? fileref->open_count : 0);
2158
2159 ExAcquireResourceSharedLite(&fcb->Vcb->tree_lock, TRUE);
2160
2161 ExAcquireResourceExclusiveLite(fcb->Header.Resource, TRUE);
2162
2163 IoRemoveShareAccess(FileObject, &fcb->share_access);
2164
2165 if (ccb)
2166 FsRtlNotifyCleanup(fcb->Vcb->NotifySync, &fcb->Vcb->DirNotifyList, ccb);
2167
2168 if (fileref) {
2169 oc = InterlockedDecrement(&fileref->open_count);
2170 #ifdef DEBUG_FCB_REFCOUNTS
2171 ERR("fileref %p: open_count now %i\n", fileref, oc);
2172 #endif
2173 }
2174
2175 if (ccb && ccb->options & FILE_DELETE_ON_CLOSE && fileref)
2176 fileref->delete_on_close = TRUE;
2177
2178 if (fileref && fileref->delete_on_close && fcb->type == BTRFS_TYPE_DIRECTORY && fcb->inode_item.st_size > 0 && fcb != fcb->Vcb->dummy_fcb)
2179 fileref->delete_on_close = FALSE;
2180
2181 if (fcb->Vcb->locked && fcb->Vcb->locked_fileobj == FileObject) {
2182 TRACE("unlocking volume\n");
2183 do_unlock_volume(fcb->Vcb);
2184 FsRtlNotifyVolumeEvent(FileObject, FSRTL_VOLUME_UNLOCK);
2185 }
2186
2187 if (ccb && ccb->reserving) {
2188 fcb->subvol->reserved = NULL;
2189 ccb->reserving = FALSE;
2190 // FIXME - flush all of subvol's fcbs
2191 }
2192
2193 if (fileref && oc == 0) {
2194 if (!fcb->Vcb->removing) {
2195 if (fileref && fileref->delete_on_close && fileref != fcb->Vcb->root_fileref && fcb != fcb->Vcb->volume_fcb) {
2196 LIST_ENTRY rollback;
2197
2198 InitializeListHead(&rollback);
2199
2200 if (!fileref->fcb->ads || fileref->dc) {
2201 if (fileref->fcb->ads) {
2202 send_notification_fileref(fileref->parent, fcb->type == BTRFS_TYPE_DIRECTORY ? FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME,
2203 FILE_ACTION_REMOVED, &fileref->dc->name);
2204 } else
2205 send_notification_fileref(fileref, fcb->type == BTRFS_TYPE_DIRECTORY ? FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME, FILE_ACTION_REMOVED, NULL);
2206 }
2207
2208 ExReleaseResourceLite(fcb->Header.Resource);
2209 locked = FALSE;
2210
2211 // fcb_lock needs to be acquired before fcb->Header.Resource
2212 ExAcquireResourceExclusiveLite(&fcb->Vcb->fcb_lock, TRUE);
2213
2214 Status = delete_fileref(fileref, FileObject, Irp, &rollback);
2215 if (!NT_SUCCESS(Status)) {
2216 ERR("delete_fileref returned %08x\n", Status);
2217 do_rollback(fcb->Vcb, &rollback);
2218 ExReleaseResourceLite(&fcb->Vcb->fcb_lock);
2219 ExReleaseResourceLite(&fcb->Vcb->tree_lock);
2220 goto exit;
2221 }
2222
2223 ExReleaseResourceLite(&fcb->Vcb->fcb_lock);
2224
2225 locked = FALSE;
2226
2227 clear_rollback(&rollback);
2228 } else if (FileObject->Flags & FO_CACHE_SUPPORTED && fcb->nonpaged->segment_object.DataSectionObject) {
2229 IO_STATUS_BLOCK iosb;
2230 CcFlushCache(FileObject->SectionObjectPointer, NULL, 0, &iosb);
2231
2232 if (!NT_SUCCESS(iosb.Status)) {
2233 ERR("CcFlushCache returned %08x\n", iosb.Status);
2234 }
2235
2236 if (!ExIsResourceAcquiredSharedLite(fcb->Header.PagingIoResource)) {
2237 ExAcquireResourceExclusiveLite(fcb->Header.PagingIoResource, TRUE);
2238 ExReleaseResourceLite(fcb->Header.PagingIoResource);
2239 }
2240
2241 CcPurgeCacheSection(&fcb->nonpaged->segment_object, NULL, 0, FALSE);
2242
2243 TRACE("flushed cache on close (FileObject = %p, fcb = %p, AllocationSize = %llx, FileSize = %llx, ValidDataLength = %llx)\n",
2244 FileObject, fcb, fcb->Header.AllocationSize.QuadPart, fcb->Header.FileSize.QuadPart, fcb->Header.ValidDataLength.QuadPart);
2245 }
2246 }
2247
2248 if (fcb->Vcb && fcb != fcb->Vcb->volume_fcb)
2249 CcUninitializeCacheMap(FileObject, NULL, NULL);
2250 }
2251
2252 if (locked)
2253 ExReleaseResourceLite(fcb->Header.Resource);
2254
2255 ExReleaseResourceLite(&fcb->Vcb->tree_lock);
2256
2257 FileObject->Flags |= FO_CLEANUP_COMPLETE;
2258 }
2259
2260 Status = STATUS_SUCCESS;
2261
2262 exit:
2263 TRACE("returning %08x\n", Status);
2264
2265 Irp->IoStatus.Status = Status;
2266 Irp->IoStatus.Information = 0;
2267
2268 IoCompleteRequest(Irp, IO_NO_INCREMENT);
2269
2270 if (top_level)
2271 IoSetTopLevelIrp(NULL);
2272
2273 FsRtlExitFileSystem();
2274
2275 return Status;
2276 }
2277
2278 _Success_(return)
2279 BOOL get_file_attributes_from_xattr(_In_reads_bytes_(len) char* val, _In_ UINT16 len, _Out_ ULONG* atts) {
2280 if (len > 2 && val[0] == '0' && val[1] == 'x') {
2281 int i;
2282 ULONG dosnum = 0;
2283
2284 for (i = 2; i < len; i++) {
2285 dosnum *= 0x10;
2286
2287 if (val[i] >= '0' && val[i] <= '9')
2288 dosnum |= val[i] - '0';
2289 else if (val[i] >= 'a' && val[i] <= 'f')
2290 dosnum |= val[i] + 10 - 'a';
2291 else if (val[i] >= 'A' && val[i] <= 'F')
2292 dosnum |= val[i] + 10 - 'a';
2293 }
2294
2295 TRACE("DOSATTRIB: %08x\n", dosnum);
2296
2297 *atts = dosnum;
2298
2299 return TRUE;
2300 }
2301
2302 return FALSE;
2303 }
2304
2305 ULONG get_file_attributes(_In_ _Requires_lock_held_(_Curr_->tree_lock) device_extension* Vcb, _In_ root* r, _In_ UINT64 inode,
2306 _In_ UINT8 type, _In_ BOOL dotfile, _In_ BOOL ignore_xa, _In_opt_ PIRP Irp) {
2307 ULONG att;
2308 char* eaval;
2309 UINT16 ealen;
2310
2311 if (!ignore_xa && get_xattr(Vcb, r, inode, EA_DOSATTRIB, EA_DOSATTRIB_HASH, (UINT8**)&eaval, &ealen, Irp)) {
2312 ULONG dosnum = 0;
2313
2314 if (get_file_attributes_from_xattr(eaval, ealen, &dosnum)) {
2315 ExFreePool(eaval);
2316
2317 if (type == BTRFS_TYPE_DIRECTORY)
2318 dosnum |= FILE_ATTRIBUTE_DIRECTORY;
2319 else if (type == BTRFS_TYPE_SYMLINK)
2320 dosnum |= FILE_ATTRIBUTE_REPARSE_POINT;
2321
2322 if (type != BTRFS_TYPE_DIRECTORY)
2323 dosnum &= ~FILE_ATTRIBUTE_DIRECTORY;
2324
2325 if (inode == SUBVOL_ROOT_INODE) {
2326 if (r->root_item.flags & BTRFS_SUBVOL_READONLY)
2327 dosnum |= FILE_ATTRIBUTE_READONLY;
2328 else
2329 dosnum &= ~FILE_ATTRIBUTE_READONLY;
2330 }
2331
2332 return dosnum;
2333 }
2334
2335 ExFreePool(eaval);
2336 }
2337
2338 switch (type) {
2339 case BTRFS_TYPE_DIRECTORY:
2340 att = FILE_ATTRIBUTE_DIRECTORY;
2341 break;
2342
2343 case BTRFS_TYPE_SYMLINK:
2344 att = FILE_ATTRIBUTE_REPARSE_POINT;
2345 break;
2346
2347 default:
2348 att = 0;
2349 break;
2350 }
2351
2352 if (dotfile) {
2353 att |= FILE_ATTRIBUTE_HIDDEN;
2354 }
2355
2356 att |= FILE_ATTRIBUTE_ARCHIVE;
2357
2358 if (inode == SUBVOL_ROOT_INODE) {
2359 if (r->root_item.flags & BTRFS_SUBVOL_READONLY)
2360 att |= FILE_ATTRIBUTE_READONLY;
2361 else
2362 att &= ~FILE_ATTRIBUTE_READONLY;
2363 }
2364
2365 // FIXME - get READONLY from ii->st_mode
2366 // FIXME - return SYSTEM for block/char devices?
2367
2368 if (att == 0)
2369 att = FILE_ATTRIBUTE_NORMAL;
2370
2371 return att;
2372 }
2373
2374 NTSTATUS sync_read_phys(_In_ PDEVICE_OBJECT DeviceObject, _In_ UINT64 StartingOffset, _In_ ULONG Length,
2375 _Out_writes_bytes_(Length) PUCHAR Buffer, _In_ BOOL override) {
2376 IO_STATUS_BLOCK IoStatus;
2377 LARGE_INTEGER Offset;
2378 PIRP Irp;
2379 PIO_STACK_LOCATION IrpSp;
2380 NTSTATUS Status;
2381 read_context context;
2382
2383 num_reads++;
2384
2385 RtlZeroMemory(&context, sizeof(read_context));
2386 KeInitializeEvent(&context.Event, NotificationEvent, FALSE);
2387
2388 Offset.QuadPart = (LONGLONG)StartingOffset;
2389
2390 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
2391
2392 if (!Irp) {
2393 ERR("IoAllocateIrp failed\n");
2394 return STATUS_INSUFFICIENT_RESOURCES;
2395 }
2396
2397 Irp->Flags |= IRP_NOCACHE;
2398 IrpSp = IoGetNextIrpStackLocation(Irp);
2399 IrpSp->MajorFunction = IRP_MJ_READ;
2400
2401 if (override)
2402 IrpSp->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
2403
2404 if (DeviceObject->Flags & DO_BUFFERED_IO) {
2405 Irp->AssociatedIrp.SystemBuffer = ExAllocatePoolWithTag(NonPagedPool, Length, ALLOC_TAG);
2406 if (!Irp->AssociatedIrp.SystemBuffer) {
2407 ERR("out of memory\n");
2408 Status = STATUS_INSUFFICIENT_RESOURCES;
2409 goto exit;
2410 }
2411
2412 Irp->Flags |= IRP_BUFFERED_IO | IRP_DEALLOCATE_BUFFER | IRP_INPUT_OPERATION;
2413
2414 Irp->UserBuffer = Buffer;
2415 } else if (DeviceObject->Flags & DO_DIRECT_IO) {
2416 Irp->MdlAddress = IoAllocateMdl(Buffer, Length, FALSE, FALSE, NULL);
2417 if (!Irp->MdlAddress) {
2418 ERR("IoAllocateMdl failed\n");
2419 Status = STATUS_INSUFFICIENT_RESOURCES;
2420 goto exit;
2421 }
2422
2423 Status = STATUS_SUCCESS;
2424
2425 _SEH2_TRY {
2426 MmProbeAndLockPages(Irp->MdlAddress, KernelMode, IoWriteAccess);
2427 } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
2428 Status = _SEH2_GetExceptionCode();
2429 } _SEH2_END;
2430
2431 if (!NT_SUCCESS(Status)) {
2432 ERR("MmProbeAndLockPages threw exception %08x\n", Status);
2433 IoFreeMdl(Irp->MdlAddress);
2434 goto exit;
2435 }
2436 } else
2437 Irp->UserBuffer = Buffer;
2438
2439 IrpSp->Parameters.Read.Length = Length;
2440 IrpSp->Parameters.Read.ByteOffset = Offset;
2441
2442 Irp->UserIosb = &IoStatus;
2443
2444 Irp->UserEvent = &context.Event;
2445
2446 IoSetCompletionRoutine(Irp, read_completion, &context, TRUE, TRUE, TRUE);
2447
2448 Status = IoCallDriver(DeviceObject, Irp);
2449
2450 if (Status == STATUS_PENDING) {
2451 KeWaitForSingleObject(&context.Event, Executive, KernelMode, FALSE, NULL);
2452 Status = context.iosb.Status;
2453 }
2454
2455 if (DeviceObject->Flags & DO_DIRECT_IO) {
2456 MmUnlockPages(Irp->MdlAddress);
2457 IoFreeMdl(Irp->MdlAddress);
2458 }
2459
2460 exit:
2461 IoFreeIrp(Irp);
2462
2463 return Status;
2464 }
2465
2466 static NTSTATUS read_superblock(_In_ device_extension* Vcb, _In_ PDEVICE_OBJECT device, _In_ UINT64 length) {
2467 NTSTATUS Status;
2468 superblock* sb;
2469 ULONG i, to_read;
2470 UINT8 valid_superblocks;
2471
2472 to_read = device->SectorSize == 0 ? sizeof(superblock) : (ULONG)sector_align(sizeof(superblock), device->SectorSize);
2473
2474 sb = ExAllocatePoolWithTag(NonPagedPool, to_read, ALLOC_TAG);
2475 if (!sb) {
2476 ERR("out of memory\n");
2477 return STATUS_INSUFFICIENT_RESOURCES;
2478 }
2479
2480 if (superblock_addrs[0] + to_read > length) {
2481 WARN("device was too short to have any superblock\n");
2482 ExFreePool(sb);
2483 return STATUS_UNRECOGNIZED_VOLUME;
2484 }
2485
2486 i = 0;
2487 valid_superblocks = 0;
2488
2489 while (superblock_addrs[i] > 0) {
2490 UINT32 crc32;
2491
2492 if (i > 0 && superblock_addrs[i] + to_read > length)
2493 break;
2494
2495 Status = sync_read_phys(device, superblock_addrs[i], to_read, (PUCHAR)sb, FALSE);
2496 if (!NT_SUCCESS(Status)) {
2497 ERR("Failed to read superblock %u: %08x\n", i, Status);
2498 ExFreePool(sb);
2499 return Status;
2500 }
2501
2502 if (sb->magic != BTRFS_MAGIC) {
2503 if (i == 0) {
2504 TRACE("not a BTRFS volume\n");
2505 ExFreePool(sb);
2506 return STATUS_UNRECOGNIZED_VOLUME;
2507 }
2508 } else {
2509 TRACE("got superblock %u!\n", i);
2510
2511 crc32 = ~calc_crc32c(0xffffffff, (UINT8*)&sb->uuid, (ULONG)sizeof(superblock) - sizeof(sb->checksum));
2512
2513 if (crc32 != *((UINT32*)sb->checksum))
2514 WARN("crc32 was %08x, expected %08x\n", crc32, *((UINT32*)sb->checksum));
2515 else if (sb->sector_size == 0)
2516 WARN("superblock sector size was 0\n");
2517 else if (sb->node_size < sizeof(tree_header) + sizeof(internal_node) || sb->node_size > 0x10000)
2518 WARN("invalid node size %x\n", sb->node_size);
2519 else if ((sb->node_size % sb->sector_size) != 0)
2520 WARN("node size %x was not a multiple of sector_size %x\n", sb->node_size, sb->sector_size);
2521 else if (valid_superblocks == 0 || sb->generation > Vcb->superblock.generation) {
2522 RtlCopyMemory(&Vcb->superblock, sb, sizeof(superblock));
2523 valid_superblocks++;
2524 }
2525 }
2526
2527 i++;
2528 }
2529
2530 ExFreePool(sb);
2531
2532 if (valid_superblocks == 0) {
2533 ERR("could not find any valid superblocks\n");
2534 return STATUS_INTERNAL_ERROR;
2535 }
2536
2537 TRACE("label is %s\n", Vcb->superblock.label);
2538
2539 return STATUS_SUCCESS;
2540 }
2541
2542 NTSTATUS dev_ioctl(_In_ PDEVICE_OBJECT DeviceObject, _In_ ULONG ControlCode, _In_reads_bytes_opt_(InputBufferSize) PVOID InputBuffer, _In_ ULONG InputBufferSize,
2543 _Out_writes_bytes_opt_(OutputBufferSize) PVOID OutputBuffer, _In_ ULONG OutputBufferSize, _In_ BOOLEAN Override, _Out_opt_ IO_STATUS_BLOCK* iosb) {
2544 PIRP Irp;
2545 KEVENT Event;
2546 NTSTATUS Status;
2547 PIO_STACK_LOCATION IrpSp;
2548 IO_STATUS_BLOCK IoStatus;
2549
2550 KeInitializeEvent(&Event, NotificationEvent, FALSE);
2551
2552 Irp = IoBuildDeviceIoControlRequest(ControlCode,
2553 DeviceObject,
2554 InputBuffer,
2555 InputBufferSize,
2556 OutputBuffer,
2557 OutputBufferSize,
2558 FALSE,
2559 &Event,
2560 &IoStatus);
2561
2562 if (!Irp) return STATUS_INSUFFICIENT_RESOURCES;
2563
2564 if (Override) {
2565 IrpSp = IoGetNextIrpStackLocation(Irp);
2566 IrpSp->Flags |= SL_OVERRIDE_VERIFY_VOLUME;
2567 }
2568
2569 Status = IoCallDriver(DeviceObject, Irp);
2570
2571 if (Status == STATUS_PENDING) {
2572 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
2573 Status = IoStatus.Status;
2574 }
2575
2576 if (iosb)
2577 *iosb = IoStatus;
2578
2579 return Status;
2580 }
2581
2582 _Requires_exclusive_lock_held_(Vcb->tree_lock)
2583 static NTSTATUS add_root(_Inout_ device_extension* Vcb, _In_ UINT64 id, _In_ UINT64 addr,
2584 _In_ UINT64 generation, _In_opt_ traverse_ptr* tp) {
2585 root* r = ExAllocatePoolWithTag(PagedPool, sizeof(root), ALLOC_TAG);
2586 if (!r) {
2587 ERR("out of memory\n");
2588 return STATUS_INSUFFICIENT_RESOURCES;
2589 }
2590
2591 r->id = id;
2592 r->dirty = FALSE;
2593 r->received = FALSE;
2594 r->reserved = NULL;
2595 r->treeholder.address = addr;
2596 r->treeholder.tree = NULL;
2597 r->treeholder.generation = generation;
2598 r->parent = 0;
2599 r->send_ops = 0;
2600 InitializeListHead(&r->fcbs);
2601
2602 r->nonpaged = ExAllocatePoolWithTag(NonPagedPool, sizeof(root_nonpaged), ALLOC_TAG);
2603 if (!r->nonpaged) {
2604 ERR("out of memory\n");
2605 ExFreePool(r);
2606 return STATUS_INSUFFICIENT_RESOURCES;
2607 }
2608
2609 ExInitializeResourceLite(&r->nonpaged->load_tree_lock);
2610
2611 r->lastinode = 0;
2612
2613 if (tp) {
2614 RtlCopyMemory(&r->root_item, tp->item->data, min(sizeof(ROOT_ITEM), tp->item->size));
2615 if (tp->item->size < sizeof(ROOT_ITEM))
2616 RtlZeroMemory(((UINT8*)&r->root_item) + tp->item->size, sizeof(ROOT_ITEM) - tp->item->size);
2617 } else
2618 RtlZeroMemory(&r->root_item, sizeof(ROOT_ITEM));
2619
2620 if (!Vcb->readonly && (r->id == BTRFS_ROOT_ROOT || r->id == BTRFS_ROOT_FSTREE || (r->id >= 0x100 && !(r->id & 0xf000000000000000)))) { // FS tree root
2621 // FIXME - don't call this if subvol is readonly (though we will have to if we ever toggle this flag)
2622 get_last_inode(Vcb, r, NULL);
2623
2624 if (r->id == BTRFS_ROOT_ROOT && r->lastinode < 0x100)
2625 r->lastinode = 0x100;
2626 }
2627
2628 InsertTailList(&Vcb->roots, &r->list_entry);
2629
2630 switch (r->id) {
2631 case BTRFS_ROOT_ROOT:
2632 Vcb->root_root = r;
2633 break;
2634
2635 case BTRFS_ROOT_EXTENT:
2636 Vcb->extent_root = r;
2637 break;
2638
2639 case BTRFS_ROOT_CHUNK:
2640 Vcb->chunk_root = r;
2641 break;
2642
2643 case BTRFS_ROOT_DEVTREE:
2644 Vcb->dev_root = r;
2645 break;
2646
2647 case BTRFS_ROOT_CHECKSUM:
2648 Vcb->checksum_root = r;
2649 break;
2650
2651 case BTRFS_ROOT_UUID:
2652 Vcb->uuid_root = r;
2653 break;
2654
2655 case BTRFS_ROOT_FREE_SPACE:
2656 Vcb->space_root = r;
2657 break;
2658
2659 case BTRFS_ROOT_DATA_RELOC:
2660 Vcb->data_reloc_root = r;
2661 break;
2662 }
2663
2664 return STATUS_SUCCESS;
2665 }
2666
2667 static NTSTATUS look_for_roots(_Requires_exclusive_lock_held_(_Curr_->tree_lock) _In_ device_extension* Vcb, _In_opt_ PIRP Irp) {
2668 traverse_ptr tp, next_tp;
2669 KEY searchkey;
2670 BOOL b;
2671 NTSTATUS Status;
2672
2673 searchkey.obj_id = 0;
2674 searchkey.obj_type = 0;
2675 searchkey.offset = 0;
2676
2677 Status = find_item(Vcb, Vcb->root_root, &tp, &searchkey, FALSE, Irp);
2678 if (!NT_SUCCESS(Status)) {
2679 ERR("error - find_item returned %08x\n", Status);
2680 return Status;
2681 }
2682
2683 do {
2684 TRACE("(%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
2685
2686 if (tp.item->key.obj_type == TYPE_ROOT_ITEM) {
2687 ROOT_ITEM* ri = (ROOT_ITEM*)tp.item->data;
2688
2689 if (tp.item->size < offsetof(ROOT_ITEM, byte_limit)) {
2690 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));
2691 } else {
2692 TRACE("root %llx - address %llx\n", tp.item->key.obj_id, ri->block_number);
2693
2694 Status = add_root(Vcb, tp.item->key.obj_id, ri->block_number, ri->generation, &tp);
2695 if (!NT_SUCCESS(Status)) {
2696 ERR("add_root returned %08x\n", Status);
2697 return Status;
2698 }
2699 }
2700 } else if (tp.item->key.obj_type == TYPE_ROOT_BACKREF && !IsListEmpty(&Vcb->roots)) {
2701 root* lastroot = CONTAINING_RECORD(Vcb->roots.Blink, root, list_entry);
2702
2703 if (lastroot->id == tp.item->key.obj_id)
2704 lastroot->parent = tp.item->key.offset;
2705 }
2706
2707 b = find_next_item(Vcb, &tp, &next_tp, FALSE, Irp);
2708
2709 if (b)
2710 tp = next_tp;
2711 } while (b);
2712
2713 if (!Vcb->readonly && !Vcb->data_reloc_root) {
2714 root* reloc_root;
2715 INODE_ITEM* ii;
2716 UINT16 irlen;
2717 INODE_REF* ir;
2718 LARGE_INTEGER time;
2719 BTRFS_TIME now;
2720
2721 WARN("data reloc root doesn't exist, creating it\n");
2722
2723 Status = create_root(Vcb, BTRFS_ROOT_DATA_RELOC, &reloc_root, FALSE, 0, Irp);
2724
2725 if (!NT_SUCCESS(Status)) {
2726 ERR("create_root returned %08x\n", Status);
2727 return Status;
2728 }
2729
2730 reloc_root->root_item.inode.generation = 1;
2731 reloc_root->root_item.inode.st_size = 3;
2732 reloc_root->root_item.inode.st_blocks = Vcb->superblock.node_size;
2733 reloc_root->root_item.inode.st_nlink = 1;
2734 reloc_root->root_item.inode.st_mode = 040755;
2735 reloc_root->root_item.inode.flags = 0xffffffff80000000;
2736 reloc_root->root_item.objid = SUBVOL_ROOT_INODE;
2737 reloc_root->root_item.bytes_used = Vcb->superblock.node_size;
2738
2739 ii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
2740 if (!ii) {
2741 ERR("out of memory\n");
2742 return STATUS_INSUFFICIENT_RESOURCES;
2743 }
2744
2745 KeQuerySystemTime(&time);
2746 win_time_to_unix(time, &now);
2747
2748 RtlZeroMemory(ii, sizeof(INODE_ITEM));
2749 ii->generation = Vcb->superblock.generation;
2750 ii->st_blocks = Vcb->superblock.node_size;
2751 ii->st_nlink = 1;
2752 ii->st_mode = 040755;
2753 ii->st_atime = now;
2754 ii->st_ctime = now;
2755 ii->st_mtime = now;
2756
2757 Status = insert_tree_item(Vcb, reloc_root, SUBVOL_ROOT_INODE, TYPE_INODE_ITEM, 0, ii, sizeof(INODE_ITEM), NULL, Irp);
2758 if (!NT_SUCCESS(Status)) {
2759 ERR("insert_tree_item returned %08x\n", Status);
2760 ExFreePool(ii);
2761 return Status;
2762 }
2763
2764 irlen = (UINT16)offsetof(INODE_REF, name[0]) + 2;
2765 ir = ExAllocatePoolWithTag(PagedPool, irlen, ALLOC_TAG);
2766 if (!ir) {
2767 ERR("out of memory\n");
2768 return STATUS_INSUFFICIENT_RESOURCES;
2769 }
2770
2771 ir->index = 0;
2772 ir->n = 2;
2773 ir->name[0] = '.';
2774 ir->name[1] = '.';
2775
2776 Status = insert_tree_item(Vcb, reloc_root, SUBVOL_ROOT_INODE, TYPE_INODE_REF, SUBVOL_ROOT_INODE, ir, irlen, NULL, Irp);
2777 if (!NT_SUCCESS(Status)) {
2778 ERR("insert_tree_item returned %08x\n", Status);
2779 ExFreePool(ir);
2780 return Status;
2781 }
2782
2783 Vcb->data_reloc_root = reloc_root;
2784 Vcb->need_write = TRUE;
2785 }
2786
2787 return STATUS_SUCCESS;
2788 }
2789
2790 static NTSTATUS find_disk_holes(_In_ _Requires_lock_held_(_Curr_->tree_lock) device_extension* Vcb, _In_ device* dev, _In_opt_ PIRP Irp) {
2791 KEY searchkey;
2792 traverse_ptr tp, next_tp;
2793 BOOL b;
2794 UINT64 lastaddr;
2795 NTSTATUS Status;
2796
2797 InitializeListHead(&dev->space);
2798
2799 searchkey.obj_id = 0;
2800 searchkey.obj_type = TYPE_DEV_STATS;
2801 searchkey.offset = dev->devitem.dev_id;
2802
2803 Status = find_item(Vcb, Vcb->dev_root, &tp, &searchkey, FALSE, Irp);
2804 if (NT_SUCCESS(Status) && !keycmp(tp.item->key, searchkey))
2805 RtlCopyMemory(dev->stats, tp.item->data, min(sizeof(UINT64) * 5, tp.item->size));
2806
2807 searchkey.obj_id = dev->devitem.dev_id;
2808 searchkey.obj_type = TYPE_DEV_EXTENT;
2809 searchkey.offset = 0;
2810
2811 Status = find_item(Vcb, Vcb->dev_root, &tp, &searchkey, FALSE, Irp);
2812 if (!NT_SUCCESS(Status)) {
2813 ERR("error - find_item returned %08x\n", Status);
2814 return Status;
2815 }
2816
2817 lastaddr = 0;
2818
2819 do {
2820 if (tp.item->key.obj_id == dev->devitem.dev_id && tp.item->key.obj_type == TYPE_DEV_EXTENT) {
2821 if (tp.item->size >= sizeof(DEV_EXTENT)) {
2822 DEV_EXTENT* de = (DEV_EXTENT*)tp.item->data;
2823
2824 if (tp.item->key.offset > lastaddr) {
2825 Status = add_space_entry(&dev->space, NULL, lastaddr, tp.item->key.offset - lastaddr);
2826 if (!NT_SUCCESS(Status)) {
2827 ERR("add_space_entry returned %08x\n", Status);
2828 return Status;
2829 }
2830 }
2831
2832 lastaddr = tp.item->key.offset + de->length;
2833 } else {
2834 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));
2835 }
2836 }
2837
2838 b = find_next_item(Vcb, &tp, &next_tp, FALSE, Irp);
2839
2840 if (b) {
2841 tp = next_tp;
2842 if (tp.item->key.obj_id > searchkey.obj_id || tp.item->key.obj_type > searchkey.obj_type)
2843 break;
2844 }
2845 } while (b);
2846
2847 if (lastaddr < dev->devitem.num_bytes) {
2848 Status = add_space_entry(&dev->space, NULL, lastaddr, dev->devitem.num_bytes - lastaddr);
2849 if (!NT_SUCCESS(Status)) {
2850 ERR("add_space_entry returned %08x\n", Status);
2851 return Status;
2852 }
2853 }
2854
2855 // The Linux driver doesn't like to allocate chunks within the first megabyte of a device.
2856
2857 space_list_subtract2(&dev->space, NULL, 0, 0x100000, NULL, NULL);
2858
2859 return STATUS_SUCCESS;
2860 }
2861
2862 static void add_device_to_list(_In_ device_extension* Vcb, _In_ device* dev) {
2863 LIST_ENTRY* le;
2864
2865 le = Vcb->devices.Flink;
2866
2867 while (le != &Vcb->devices) {
2868 device* dev2 = CONTAINING_RECORD(le, device, list_entry);
2869
2870 if (dev2->devitem.dev_id > dev->devitem.dev_id) {
2871 InsertHeadList(le->Blink, &dev->list_entry);
2872 return;
2873 }
2874
2875 le = le->Flink;
2876 }
2877
2878 InsertTailList(&Vcb->devices, &dev->list_entry);
2879 }
2880
2881 _Ret_maybenull_
2882 device* find_device_from_uuid(_In_ device_extension* Vcb, _In_ BTRFS_UUID* uuid) {
2883 volume_device_extension* vde;
2884 pdo_device_extension* pdode;
2885 LIST_ENTRY* le;
2886
2887 le = Vcb->devices.Flink;
2888 while (le != &Vcb->devices) {
2889 device* dev = CONTAINING_RECORD(le, device, list_entry);
2890
2891 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,
2892 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],
2893 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]);
2894
2895 if (RtlCompareMemory(&dev->devitem.device_uuid, uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
2896 TRACE("returning device %llx\n", dev->devitem.dev_id);
2897 return dev;
2898 }
2899
2900 le = le->Flink;
2901 }
2902
2903 vde = Vcb->vde;
2904
2905 if (!vde)
2906 goto end;
2907
2908 pdode = vde->pdode;
2909
2910 ExAcquireResourceSharedLite(&pdode->child_lock, TRUE);
2911
2912 if (Vcb->devices_loaded < Vcb->superblock.num_devices) {
2913 le = pdode->children.Flink;
2914
2915 while (le != &pdode->children) {
2916 volume_child* vc = CONTAINING_RECORD(le, volume_child, list_entry);
2917
2918 if (RtlCompareMemory(uuid, &vc->uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
2919 device* dev;
2920
2921 dev = ExAllocatePoolWithTag(NonPagedPool, sizeof(device), ALLOC_TAG);
2922 if (!dev) {
2923 ExReleaseResourceLite(&pdode->child_lock);
2924 ERR("out of memory\n");
2925 return NULL;
2926 }
2927
2928 RtlZeroMemory(dev, sizeof(device));
2929 dev->devobj = vc->devobj;
2930 dev->devitem.device_uuid = *uuid;
2931 dev->devitem.dev_id = vc->devid;
2932 dev->devitem.num_bytes = vc->size;
2933 dev->seeding = vc->seeding;
2934 dev->readonly = dev->seeding;
2935 dev->reloc = FALSE;
2936 dev->removable = FALSE;
2937 dev->disk_num = vc->disk_num;
2938 dev->part_num = vc->part_num;
2939 dev->num_trim_entries = 0;
2940 InitializeListHead(&dev->trim_list);
2941
2942 add_device_to_list(Vcb, dev);
2943 Vcb->devices_loaded++;
2944
2945 ExReleaseResourceLite(&pdode->child_lock);
2946
2947 return dev;
2948 }
2949
2950 le = le->Flink;
2951 }
2952 }
2953
2954 ExReleaseResourceLite(&pdode->child_lock);
2955
2956 end:
2957 WARN("could not find device with uuid %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
2958 uuid->uuid[0], uuid->uuid[1], uuid->uuid[2], uuid->uuid[3], uuid->uuid[4], uuid->uuid[5], uuid->uuid[6], uuid->uuid[7],
2959 uuid->uuid[8], uuid->uuid[9], uuid->uuid[10], uuid->uuid[11], uuid->uuid[12], uuid->uuid[13], uuid->uuid[14], uuid->uuid[15]);
2960
2961 return NULL;
2962 }
2963
2964 static BOOL is_device_removable(_In_ PDEVICE_OBJECT devobj) {
2965 NTSTATUS Status;
2966 STORAGE_HOTPLUG_INFO shi;
2967
2968 Status = dev_ioctl(devobj, IOCTL_STORAGE_GET_HOTPLUG_INFO, NULL, 0, &shi, sizeof(STORAGE_HOTPLUG_INFO), TRUE, NULL);
2969
2970 if (!NT_SUCCESS(Status)) {
2971 ERR("dev_ioctl returned %08x\n", Status);
2972 return FALSE;
2973 }
2974
2975 return shi.MediaRemovable != 0 ? TRUE : FALSE;
2976 }
2977
2978 static ULONG get_device_change_count(_In_ PDEVICE_OBJECT devobj) {
2979 NTSTATUS Status;
2980 ULONG cc;
2981 IO_STATUS_BLOCK iosb;
2982
2983 Status = dev_ioctl(devobj, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, &cc, sizeof(ULONG), TRUE, &iosb);
2984
2985 if (!NT_SUCCESS(Status)) {
2986 ERR("dev_ioctl returned %08x\n", Status);
2987 return 0;
2988 }
2989
2990 if (iosb.Information < sizeof(ULONG)) {
2991 ERR("iosb.Information was too short\n");
2992 return 0;
2993 }
2994
2995 return cc;
2996 }
2997
2998 void init_device(_In_ device_extension* Vcb, _Inout_ device* dev, _In_ BOOL get_nums) {
2999 NTSTATUS Status;
3000 ULONG aptelen;
3001 ATA_PASS_THROUGH_EX* apte;
3002 STORAGE_PROPERTY_QUERY spq;
3003 DEVICE_TRIM_DESCRIPTOR dtd;
3004
3005 dev->removable = is_device_removable(dev->devobj);
3006 dev->change_count = dev->removable ? get_device_change_count(dev->devobj) : 0;
3007
3008 if (get_nums) {
3009 STORAGE_DEVICE_NUMBER sdn;
3010
3011 Status = dev_ioctl(dev->devobj, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0,
3012 &sdn, sizeof(STORAGE_DEVICE_NUMBER), TRUE, NULL);
3013
3014 if (!NT_SUCCESS(Status)) {
3015 WARN("IOCTL_STORAGE_GET_DEVICE_NUMBER returned %08x\n", Status);
3016 dev->disk_num = 0xffffffff;
3017 dev->part_num = 0xffffffff;
3018 } else {
3019 dev->disk_num = sdn.DeviceNumber;
3020 dev->part_num = sdn.PartitionNumber;
3021 }
3022 }
3023
3024 dev->trim = FALSE;
3025 dev->readonly = dev->seeding;
3026 dev->reloc = FALSE;
3027 dev->num_trim_entries = 0;
3028 dev->stats_changed = FALSE;
3029 InitializeListHead(&dev->trim_list);
3030
3031 if (!dev->readonly) {
3032 Status = dev_ioctl(dev->devobj, IOCTL_DISK_IS_WRITABLE, NULL, 0,
3033 NULL, 0, TRUE, NULL);
3034 if (Status == STATUS_MEDIA_WRITE_PROTECTED)
3035 dev->readonly = TRUE;
3036 }
3037
3038 aptelen = sizeof(ATA_PASS_THROUGH_EX) + 512;
3039 apte = ExAllocatePoolWithTag(NonPagedPool, aptelen, ALLOC_TAG);
3040 if (!apte) {
3041 ERR("out of memory\n");
3042 return;
3043 }
3044
3045 RtlZeroMemory(apte, aptelen);
3046
3047 apte->Length = sizeof(ATA_PASS_THROUGH_EX);
3048 apte->AtaFlags = ATA_FLAGS_DATA_IN;
3049 apte->DataTransferLength = aptelen - sizeof(ATA_PASS_THROUGH_EX);
3050 apte->TimeOutValue = 3;
3051 apte->DataBufferOffset = apte->Length;
3052 apte->CurrentTaskFile[6] = IDE_COMMAND_IDENTIFY;
3053
3054 Status = dev_ioctl(dev->devobj, IOCTL_ATA_PASS_THROUGH, apte, aptelen,
3055 apte, aptelen, TRUE, NULL);
3056
3057 if (!NT_SUCCESS(Status))
3058 TRACE("IOCTL_ATA_PASS_THROUGH returned %08x for IDENTIFY DEVICE\n", Status);
3059 else {
3060 IDENTIFY_DEVICE_DATA* idd = (IDENTIFY_DEVICE_DATA*)((UINT8*)apte + sizeof(ATA_PASS_THROUGH_EX));
3061
3062 if (idd->CommandSetSupport.FlushCache) {
3063 dev->can_flush = TRUE;
3064 TRACE("FLUSH CACHE supported\n");
3065 } else
3066 TRACE("FLUSH CACHE not supported\n");
3067 }
3068
3069 ExFreePool(apte);
3070
3071 spq.PropertyId = StorageDeviceTrimProperty;
3072 spq.QueryType = PropertyStandardQuery;
3073 spq.AdditionalParameters[0] = 0;
3074
3075 Status = dev_ioctl(dev->devobj, IOCTL_STORAGE_QUERY_PROPERTY, &spq, sizeof(STORAGE_PROPERTY_QUERY),
3076 &dtd, sizeof(DEVICE_TRIM_DESCRIPTOR), TRUE, NULL);
3077
3078 if (NT_SUCCESS(Status)) {
3079 if (dtd.TrimEnabled) {
3080 dev->trim = TRUE;
3081 Vcb->trim = TRUE;
3082 TRACE("TRIM supported\n");
3083 } else
3084 TRACE("TRIM not supported\n");
3085 }
3086
3087 RtlZeroMemory(dev->stats, sizeof(UINT64) * 5);
3088 }
3089
3090 static NTSTATUS load_chunk_root(_In_ _Requires_lock_held_(_Curr_->tree_lock) device_extension* Vcb, _In_opt_ PIRP Irp) {
3091 traverse_ptr tp, next_tp;
3092 KEY searchkey;
3093 BOOL b;
3094 chunk* c;
3095 NTSTATUS Status;
3096
3097 searchkey.obj_id = 0;
3098 searchkey.obj_type = 0;
3099 searchkey.offset = 0;
3100
3101 Vcb->data_flags = 0;
3102 Vcb->metadata_flags = 0;
3103 Vcb->system_flags = 0;
3104
3105 Status = find_item(Vcb, Vcb->chunk_root, &tp, &searchkey, FALSE, Irp);
3106 if (!NT_SUCCESS(Status)) {
3107 ERR("error - find_item returned %08x\n", Status);
3108 return Status;
3109 }
3110
3111 do {
3112 TRACE("(%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
3113
3114 if (tp.item->key.obj_id == 1 && tp.item->key.obj_type == TYPE_DEV_ITEM) {
3115 if (tp.item->size < sizeof(DEV_ITEM)) {
3116 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_ITEM));
3117 } else {
3118 DEV_ITEM* di = (DEV_ITEM*)tp.item->data;
3119 LIST_ENTRY* le;
3120 BOOL done = FALSE;
3121
3122 le = Vcb->devices.Flink;
3123 while (le != &Vcb->devices) {
3124 device* dev = CONTAINING_RECORD(le, device, list_entry);
3125
3126 if (dev->devobj && RtlCompareMemory(&dev->devitem.device_uuid, &di->device_uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
3127 RtlCopyMemory(&dev->devitem, tp.item->data, min(tp.item->size, sizeof(DEV_ITEM)));
3128
3129 if (le != Vcb->devices.Flink)
3130 init_device(Vcb, dev, TRUE);
3131
3132 done = TRUE;
3133 break;
3134 }
3135
3136 le = le->Flink;
3137 }
3138
3139 if (!done && Vcb->vde) {
3140 volume_device_extension* vde = Vcb->vde;
3141 pdo_device_extension* pdode = vde->pdode;
3142
3143 ExAcquireResourceSharedLite(&pdode->child_lock, TRUE);
3144
3145 if (Vcb->devices_loaded < Vcb->superblock.num_devices) {
3146 le = pdode->children.Flink;
3147
3148 while (le != &pdode->children) {
3149 volume_child* vc = CONTAINING_RECORD(le, volume_child, list_entry);
3150
3151 if (RtlCompareMemory(&di->device_uuid, &vc->uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
3152 device* dev;
3153
3154 dev = ExAllocatePoolWithTag(NonPagedPool, sizeof(device), ALLOC_TAG);
3155 if (!dev) {
3156 ExReleaseResourceLite(&pdode->child_lock);
3157 ERR("out of memory\n");
3158 return STATUS_INSUFFICIENT_RESOURCES;
3159 }
3160
3161 RtlZeroMemory(dev, sizeof(device));
3162
3163 dev->devobj = vc->devobj;
3164 RtlCopyMemory(&dev->devitem, di, min(tp.item->size, sizeof(DEV_ITEM)));
3165 dev->seeding = vc->seeding;
3166 init_device(Vcb, dev, FALSE);
3167
3168 if (dev->devitem.num_bytes > vc->size) {
3169 WARN("device %llx: DEV_ITEM says %llx bytes, but Windows only reports %llx\n", tp.item->key.offset,
3170 dev->devitem.num_bytes, vc->size);
3171
3172 dev->devitem.num_bytes = vc->size;
3173 }
3174
3175 dev->disk_num = vc->disk_num;
3176 dev->part_num = vc->part_num;
3177 add_device_to_list(Vcb, dev);
3178 Vcb->devices_loaded++;
3179
3180 done = TRUE;
3181 break;
3182 }
3183
3184 le = le->Flink;
3185 }
3186
3187 if (!done) {
3188 if (!Vcb->options.allow_degraded) {
3189 ERR("volume not found: device %llx, uuid %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n", tp.item->key.offset,
3190 di->device_uuid.uuid[0], di->device_uuid.uuid[1], di->device_uuid.uuid[2], di->device_uuid.uuid[3], di->device_uuid.uuid[4], di->device_uuid.uuid[5], di->device_uuid.uuid[6], di->device_uuid.uuid[7],
3191 di->device_uuid.uuid[8], di->device_uuid.uuid[9], di->device_uuid.uuid[10], di->device_uuid.uuid[11], di->device_uuid.uuid[12], di->device_uuid.uuid[13], di->device_uuid.uuid[14], di->device_uuid.uuid[15]);
3192 } else {
3193 device* dev;
3194
3195 dev = ExAllocatePoolWithTag(NonPagedPool, sizeof(device), ALLOC_TAG);
3196 if (!dev) {
3197 ExReleaseResourceLite(&pdode->child_lock);
3198 ERR("out of memory\n");
3199 return STATUS_INSUFFICIENT_RESOURCES;
3200 }
3201
3202 RtlZeroMemory(dev, sizeof(device));
3203
3204 // Missing device, so we keep dev->devobj as NULL
3205 RtlCopyMemory(&dev->devitem, di, min(tp.item->size, sizeof(DEV_ITEM)));
3206 InitializeListHead(&dev->trim_list);
3207
3208 add_device_to_list(Vcb, dev);
3209 Vcb->devices_loaded++;
3210 }
3211 }
3212 } else
3213 ERR("unexpected device %llx found\n", tp.item->key.offset);
3214
3215 ExReleaseResourceLite(&pdode->child_lock);
3216 }
3217 }
3218 } else if (tp.item->key.obj_type == TYPE_CHUNK_ITEM) {
3219 if (tp.item->size < sizeof(CHUNK_ITEM)) {
3220 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(CHUNK_ITEM));
3221 } else {
3222 c = ExAllocatePoolWithTag(NonPagedPool, sizeof(chunk), ALLOC_TAG);
3223
3224 if (!c) {
3225 ERR("out of memory\n");
3226 return STATUS_INSUFFICIENT_RESOURCES;
3227 }
3228
3229 c->size = tp.item->size;
3230 c->offset = tp.item->key.offset;
3231 c->used = c->oldused = 0;
3232 c->cache = c->old_cache = NULL;
3233 c->created = FALSE;
3234 c->readonly = FALSE;
3235 c->reloc = FALSE;
3236 c->cache_loaded = FALSE;
3237 c->changed = FALSE;
3238 c->space_changed = FALSE;
3239 c->balance_num = 0;
3240
3241 c->chunk_item = ExAllocatePoolWithTag(NonPagedPool, tp.item->size, ALLOC_TAG);
3242
3243 if (!c->chunk_item) {
3244 ERR("out of memory\n");
3245 ExFreePool(c);
3246 return STATUS_INSUFFICIENT_RESOURCES;
3247 }
3248
3249 RtlCopyMemory(c->chunk_item, tp.item->data, tp.item->size);
3250
3251 if (c->chunk_item->type & BLOCK_FLAG_DATA && c->chunk_item->type > Vcb->data_flags)
3252 Vcb->data_flags = c->chunk_item->type;
3253
3254 if (c->chunk_item->type & BLOCK_FLAG_METADATA && c->chunk_item->type > Vcb->metadata_flags)
3255 Vcb->metadata_flags = c->chunk_item->type;
3256
3257 if (c->chunk_item->type & BLOCK_FLAG_SYSTEM && c->chunk_item->type > Vcb->system_flags)
3258 Vcb->system_flags = c->chunk_item->type;
3259
3260 if (c->chunk_item->type & BLOCK_FLAG_RAID10) {
3261 if (c->chunk_item->sub_stripes == 0 || c->chunk_item->sub_stripes > c->chunk_item->num_stripes) {
3262 ERR("chunk %llx: invalid stripes (num_stripes %u, sub_stripes %u)\n", c->offset, c->chunk_item->num_stripes, c->chunk_item->sub_stripes);
3263 ExFreePool(c->chunk_item);
3264 ExFreePool(c);
3265 return STATUS_INTERNAL_ERROR;
3266 }
3267 }
3268
3269 if (c->chunk_item->num_stripes > 0) {
3270 CHUNK_ITEM_STRIPE* cis = (CHUNK_ITEM_STRIPE*)&c->chunk_item[1];
3271 UINT16 i;
3272
3273 c->devices = ExAllocatePoolWithTag(NonPagedPool, sizeof(device*) * c->chunk_item->num_stripes, ALLOC_TAG);
3274
3275 if (!c->devices) {
3276 ERR("out of memory\n");
3277 ExFreePool(c->chunk_item);
3278 ExFreePool(c);
3279 return STATUS_INSUFFICIENT_RESOURCES;
3280 }
3281
3282 for (i = 0; i < c->chunk_item->num_stripes; i++) {
3283 c->devices[i] = find_device_from_uuid(Vcb, &cis[i].dev_uuid);
3284 TRACE("device %llu = %p\n", i, c->devices[i]);
3285
3286 if (!c->devices[i]) {
3287 ERR("missing device\n");
3288 ExFreePool(c->chunk_item);
3289 ExFreePool(c);
3290 return STATUS_INTERNAL_ERROR;
3291 }
3292
3293 if (c->devices[i]->readonly)
3294 c->readonly = TRUE;
3295 }
3296 } else {
3297 ERR("chunk %llx: number of stripes is 0\n", c->offset);
3298 ExFreePool(c->chunk_item);
3299 ExFreePool(c);
3300 return STATUS_INTERNAL_ERROR;
3301 }
3302
3303 ExInitializeResourceLite(&c->lock);
3304 ExInitializeResourceLite(&c->changed_extents_lock);
3305
3306 InitializeListHead(&c->space);
3307 InitializeListHead(&c->space_size);
3308 InitializeListHead(&c->deleting);
3309 InitializeListHead(&c->changed_extents);
3310
3311 InitializeListHead(&c->range_locks);
3312 ExInitializeResourceLite(&c->range_locks_lock);
3313 KeInitializeEvent(&c->range_locks_event, NotificationEvent, FALSE);
3314
3315 InitializeListHead(&c->partial_stripes);
3316 ExInitializeResourceLite(&c->partial_stripes_lock);
3317
3318 c->last_alloc_set = FALSE;
3319
3320 c->last_stripe = 0;
3321
3322 InsertTailList(&Vcb->chunks, &c->list_entry);
3323
3324 c->list_entry_balance.Flink = NULL;
3325 }
3326 }
3327
3328 b = find_next_item(Vcb, &tp, &next_tp, FALSE, Irp);
3329
3330 if (b)
3331 tp = next_tp;
3332 } while (b);
3333
3334 Vcb->log_to_phys_loaded = TRUE;
3335
3336 if (Vcb->data_flags == 0)
3337 Vcb->data_flags = BLOCK_FLAG_DATA | (Vcb->superblock.num_devices > 1 ? BLOCK_FLAG_RAID0 : 0);
3338
3339 if (Vcb->metadata_flags == 0)
3340 Vcb->metadata_flags = BLOCK_FLAG_METADATA | (Vcb->superblock.num_devices > 1 ? BLOCK_FLAG_RAID1 : BLOCK_FLAG_DUPLICATE);
3341
3342 if (Vcb->system_flags == 0)
3343 Vcb->system_flags = BLOCK_FLAG_SYSTEM | (Vcb->superblock.num_devices > 1 ? BLOCK_FLAG_RAID1 : BLOCK_FLAG_DUPLICATE);
3344
3345 if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_MIXED_GROUPS) {
3346 Vcb->metadata_flags |= BLOCK_FLAG_DATA;
3347 Vcb->data_flags = Vcb->metadata_flags;
3348 }
3349
3350 return STATUS_SUCCESS;
3351 }
3352
3353 void protect_superblocks(_Inout_ chunk* c) {
3354 UINT16 i = 0, j;
3355 UINT64 off_start, off_end;
3356
3357 // The Linux driver also protects all the space before the first superblock.
3358 // I realize this confuses physical and logical addresses, but this is what btrfs-progs does -
3359 // evidently Linux assumes the chunk at 0 is always SINGLE.
3360 if (c->offset < superblock_addrs[0])
3361 space_list_subtract(c, FALSE, c->offset, superblock_addrs[0] - c->offset, NULL);
3362
3363 while (superblock_addrs[i] != 0) {
3364