ReactOS  0.4.15-dev-449-g64abd9f
btrfslib.c
Go to the documentation of this file.
1 /* Copyright (c) Mark Harmstone 2016-17
2  *
3  * This file is part of WinBtrfs.
4  *
5  * WinBtrfs is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public Licence as published by
7  * the Free Software Foundation, either version 3 of the Licence, or
8  * (at your option) any later version.
9  *
10  * WinBtrfs is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public Licence for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public Licence
16  * along with WinBtrfs. If not, see <http://www.gnu.org/licenses/>. */
17 
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <time.h>
21 #include <ntstatus.h>
22 #define WIN32_NO_STATUS
23 #include <windef.h>
24 #include <winbase.h>
25 #ifndef __REACTOS__
26 #include <winternl.h>
27 #include <devioctl.h>
28 #include <ntdddisk.h>
29 #else
30 #include <ndk/iofuncs.h>
31 #include <ndk/obfuncs.h>
32 #include <ndk/rtlfuncs.h>
33 #endif
34 #include <ntddscsi.h>
35 #include <ntddstor.h>
36 #include <ata.h>
37 #include <mountmgr.h>
38 #ifdef __REACTOS__
39 #include <winnls.h>
40 #include <stdbool.h>
41 #include "btrfs.h"
42 #include "btrfsioctl.h"
43 #include "crc32c.h"
44 #include "xxhash.h"
45 #else
46 #include <stringapiset.h>
47 #include <stdbool.h>
48 #include "../btrfs.h"
49 #include "../btrfsioctl.h"
50 #include "../crc32c.h"
51 #include "../xxhash.h"
52 
53 #if defined(_X86_) || defined(_AMD64_)
54 #ifndef _MSC_VER
55 #include <cpuid.h>
56 #else
57 #include <intrin.h>
58 #endif
59 #endif
60 #endif // __REACTOS__
61 
62 #define SHA256_HASH_SIZE 32
63 void calc_sha256(uint8_t* hash, const void* input, size_t len);
64 
65 #define BLAKE2_HASH_SIZE 32
66 void blake2b(void *out, size_t outlen, const void* in, size_t inlen);
67 
68 #ifndef __REACTOS__
69 #define FSCTL_LOCK_VOLUME CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 6, METHOD_BUFFERED, FILE_ANY_ACCESS)
70 #define FSCTL_UNLOCK_VOLUME CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 7, METHOD_BUFFERED, FILE_ANY_ACCESS)
71 #define FSCTL_DISMOUNT_VOLUME CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 8, METHOD_BUFFERED, FILE_ANY_ACCESS)
72 
73 #ifndef _MSC_VER // not in mingw yet
74 #define DEVICE_DSM_FLAG_TRIM_NOT_FS_ALLOCATED 0x80000000
75 #endif
76 
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
81 
84 
87 #ifdef __cplusplus
88 }
89 #endif
90 #endif // __REACTOS__
91 
92 // These are undocumented, and what comes from format.exe
93 typedef struct {
94  void* table;
95  void* unk1;
97 } DSTRING;
98 
99 typedef struct {
100  void* table;
102 
103 #define FORMAT_FLAG_QUICK_FORMAT 0x00000001
104 #define FORMAT_FLAG_UNKNOWN1 0x00000002
105 #define FORMAT_FLAG_DISMOUNT_FIRST 0x00000004
106 #define FORMAT_FLAG_UNKNOWN2 0x00000040
107 #define FORMAT_FLAG_LARGE_RECORDS 0x00000100
108 #define FORMAT_FLAG_INTEGRITY_DISABLE 0x00000100
109 
110 typedef struct {
115 } options;
116 
117 #ifndef __REACTOS__
119  ListHead->Flink = ListHead->Blink = ListHead;
120 }
121 
123  PLIST_ENTRY Blink;
124 
125  Blink = ListHead->Blink;
126  Entry->Flink = ListHead;
127  Entry->Blink = Blink;
128  Blink->Flink = Entry;
129  ListHead->Blink = Entry;
130 }
131 #endif
132 
133 #ifdef __REACTOS__
135 #endif
136 
137 typedef struct {
140  void* data;
142 } btrfs_item;
143 
144 typedef struct {
150 } btrfs_chunk;
151 
152 typedef struct {
158 } btrfs_root;
159 
160 typedef struct {
163 } btrfs_dev;
164 
165 #define keycmp(key1, key2)\
166  ((key1.obj_id < key2.obj_id) ? -1 :\
167  ((key1.obj_id > key2.obj_id) ? 1 :\
168  ((key1.obj_type < key2.obj_type) ? -1 :\
169  ((key1.obj_type > key2.obj_type) ? 1 :\
170  ((key1.offset < key2.offset) ? -1 :\
171  ((key1.offset > key2.offset) ? 1 :\
172  0))))))
173 
178 
179 // the following definitions come from fmifs.h in ReactOS
180 
181 typedef struct {
182  ULONG Lines;
183  PCHAR Output;
185 
186 typedef enum {
212 
213 typedef enum {
232 
233 typedef BOOLEAN (NTAPI* PFMIFSCALLBACK)(CALLBACKCOMMAND Command, ULONG SubAction, PVOID ActionInfo);
234 
235 #ifndef __REACTOS__
237 #else
239 #endif
240  BOOLEAN ScanDrive, PFMIFSCALLBACK Callback) {
241  // STUB
242 
243  if (Callback) {
245 
246  TextOut.Lines = 1;
247  TextOut.Output = "stub, not implemented";
248 
249  Callback(OUTPUT, 0, &TextOut);
250  }
251 
252  return STATUS_SUCCESS;
253 }
254 
255 static btrfs_root* add_root(LIST_ENTRY* roots, uint64_t id) {
256  btrfs_root* root;
257 
258 #ifdef __REACTOS__
259  root = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(btrfs_root));
260 #else
261  root = malloc(sizeof(btrfs_root));
262 #endif
263 
264  root->id = id;
265 #ifndef __REACTOS__
266  RtlZeroMemory(&root->header, sizeof(tree_header));
267 #endif
268  InitializeListHead(&root->items);
269  InsertTailList(roots, &root->list_entry);
270 
271  return root;
272 }
273 
274 static void free_roots(LIST_ENTRY* roots) {
275  LIST_ENTRY* le;
276 
277  le = roots->Flink;
278  while (le != roots) {
279  LIST_ENTRY *le2 = le->Flink, *le3;
281 
282  le3 = r->items.Flink;
283  while (le3 != &r->items) {
284  LIST_ENTRY* le4 = le3->Flink;
286 
287  if (item->data)
288 #ifdef __REACTOS__
289  RtlFreeHeap(RtlGetProcessHeap(), 0, item->data);
290 
291  RtlFreeHeap(RtlGetProcessHeap(), 0, item);
292 #else
293  free(item->data);
294 
295  free(item);
296 #endif
297 
298  le3 = le4;
299  }
300 
301 #ifdef __REACTOS__
302  RtlFreeHeap(RtlGetProcessHeap(), 0, r);
303 #else
304  free(r);
305 #endif
306 
307  le = le2;
308  }
309 }
310 
311 static void free_chunks(LIST_ENTRY* chunks) {
312  LIST_ENTRY* le;
313 
314  le = chunks->Flink;
315  while (le != chunks) {
316  LIST_ENTRY *le2 = le->Flink;
318 
319 #ifndef __REACTOS__
320  free(c->chunk_item);
321  free(c);
322 #else
323  RtlFreeHeap(RtlGetProcessHeap(), 0, c->chunk_item);
324  RtlFreeHeap(RtlGetProcessHeap(), 0, c);
325 #endif
326 
327  le = le2;
328  }
329 }
330 
331 static void add_item(btrfs_root* r, uint64_t obj_id, uint8_t obj_type, uint64_t offset, void* data, uint16_t size) {
332  LIST_ENTRY* le;
333  btrfs_item* item;
334 
335 #ifndef __REACTOS__
336  item = malloc(sizeof(btrfs_item));
337 #else
338  item = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(btrfs_item));
339 #endif
340 
341  item->key.obj_id = obj_id;
342  item->key.obj_type = obj_type;
343  item->key.offset = offset;
344  item->size = size;
345 
346  if (size == 0)
347  item->data = NULL;
348  else {
349 #ifndef __REACTOS__
350  item->data = malloc(size);
351 #else
352  item->data = RtlAllocateHeap(RtlGetProcessHeap(), 0, size);
353 #endif
354  memcpy(item->data, data, size);
355  }
356 
357  le = r->items.Flink;
358  while (le != &r->items) {
360 
361  if (keycmp(item->key, i2->key) != 1) {
362  InsertTailList(le, &item->list_entry);
363  return;
364  }
365 
366  le = le->Flink;
367  }
368 
369  InsertTailList(&r->items, &item->list_entry);
370 }
371 
373  uint64_t off;
374  DEV_EXTENT de;
375 
376  off = dev->last_alloc;
377  dev->last_alloc += size;
378 
379  dev->dev_item.bytes_used += size;
380 
382  de.objid = 0x100;
383  de.address = offset;
384  de.length = size;
385  de.chunktree_uuid = *chunkuuid;
386 
387  add_item(dev_root, dev->dev_item.dev_id, TYPE_DEV_EXTENT, off, &de, sizeof(DEV_EXTENT));
388 
389  return off;
390 }
391 
392 static btrfs_chunk* add_chunk(LIST_ENTRY* chunks, uint64_t flags, btrfs_root* chunk_root, btrfs_dev* dev, btrfs_root* dev_root, BTRFS_UUID* chunkuuid, uint32_t sector_size) {
393  uint64_t off, size;
394  uint16_t stripes, i;
395  btrfs_chunk* c;
396  LIST_ENTRY* le;
397  CHUNK_ITEM_STRIPE* cis;
398 
399  off = 0xc00000;
400  le = chunks->Flink;
401  while (le != chunks) {
403 
404  if (c->offset + c->chunk_item->size > off)
405  off = c->offset + c->chunk_item->size;
406 
407  le = le->Flink;
408  }
409 
410  if (flags & BLOCK_FLAG_METADATA) {
411  if (dev->dev_item.num_bytes > 0xC80000000) // 50 GB
412  size = 0x40000000; // 1 GB
413  else
414  size = 0x10000000; // 256 MB
415  } else if (flags & BLOCK_FLAG_SYSTEM)
416  size = 0x800000;
417 
418  size = min(size, dev->dev_item.num_bytes / 10); // cap at 10%
419  size &= ~(sector_size - 1);
420 
421  stripes = flags & BLOCK_FLAG_DUPLICATE ? 2 : 1;
422 
423  if (dev->dev_item.num_bytes - dev->dev_item.bytes_used < stripes * size) // not enough space
424  return NULL;
425 
426 #ifndef __REACTOS__
427  c = malloc(sizeof(btrfs_chunk));
428 #else
429  c = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(btrfs_chunk));
430 #endif
431  c->offset = off;
432  c->lastoff = off;
433  c->used = 0;
434 
435 #ifndef __REACTOS__
436  c->chunk_item = malloc(sizeof(CHUNK_ITEM) + (stripes * sizeof(CHUNK_ITEM_STRIPE)));
437 #else
438  c->chunk_item = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(CHUNK_ITEM) + (stripes * sizeof(CHUNK_ITEM_STRIPE)));
439 #endif
440 
441  c->chunk_item->size = size;
442  c->chunk_item->root_id = BTRFS_ROOT_EXTENT;
443  c->chunk_item->stripe_length = max(sector_size, 0x10000);
444  c->chunk_item->type = flags;
445  c->chunk_item->opt_io_alignment = max(sector_size, 0x10000);
446  c->chunk_item->opt_io_width = max(sector_size, 0x10000);
447  c->chunk_item->sector_size = sector_size;
448  c->chunk_item->num_stripes = stripes;
449  c->chunk_item->sub_stripes = 0;
450 
451  cis = (CHUNK_ITEM_STRIPE*)&c->chunk_item[1];
452 
453  for (i = 0; i < stripes; i++) {
454  cis[i].dev_id = dev->dev_item.dev_id;
455  cis[i].offset = find_chunk_offset(size, c->offset, dev, dev_root, chunkuuid);
456  cis[i].dev_uuid = dev->dev_item.device_uuid;
457  }
458 
459  add_item(chunk_root, 0x100, TYPE_CHUNK_ITEM, c->offset, c->chunk_item, sizeof(CHUNK_ITEM) + (stripes * sizeof(CHUNK_ITEM_STRIPE)));
460 
461  InsertTailList(chunks, &c->list_entry);
462 
463  return c;
464 }
465 
467  CHUNK_ITEM_STRIPE* cis = (CHUNK_ITEM_STRIPE*)&c->chunk_item[1];
468  uint64_t stripe = (address - c->offset) / c->chunk_item->stripe_length;
469  uint16_t i, j;
470 
471  for (i = 0; i < c->chunk_item->num_stripes; i++) {
472  j = 0;
473  while (superblock_addrs[j] != 0) {
474  if (superblock_addrs[j] >= cis[i].offset) {
475  uint64_t stripe2 = (superblock_addrs[j] - cis[i].offset) / c->chunk_item->stripe_length;
476 
477  if (stripe2 == stripe)
478  return true;
479  }
480  j++;
481  }
482  }
483 
484  return false;
485 }
486 
488  uint64_t addr;
489 
490  addr = c->lastoff;
491 
492  while (superblock_collision(c, addr)) {
493  addr = addr - ((addr - c->offset) % c->chunk_item->stripe_length) + c->chunk_item->stripe_length;
494 
495  if (addr >= c->offset + c->chunk_item->size) // chunk has been exhausted
496  return 0;
497  }
498 
499  return addr;
500 }
501 
502 typedef struct {
507 
508 typedef struct {
514 
515 static void assign_addresses(LIST_ENTRY* roots, btrfs_chunk* sys_chunk, btrfs_chunk* metadata_chunk, uint32_t node_size,
516  btrfs_root* root_root, btrfs_root* extent_root, bool skinny) {
517  LIST_ENTRY* le;
518 
519  le = roots->Flink;
520  while (le != roots) {
522  btrfs_chunk* c = r->id == BTRFS_ROOT_CHUNK ? sys_chunk : metadata_chunk;
523 
524  r->header.address = get_next_address(c);
525  r->c = c;
526  c->lastoff = r->header.address + node_size;
527  c->used += node_size;
528 
529  if (skinny) {
531 
532  eim.ei.refcount = 1;
533  eim.ei.generation = 1;
536  eim.tbr.offset = r->id;
537 
538  add_item(extent_root, r->header.address, TYPE_METADATA_ITEM, 0, &eim, sizeof(EXTENT_ITEM_METADATA));
539  } else {
541  KEY firstitem;
542 
543  if (r->items.Flink == &r->items) {
544  firstitem.obj_id = 0;
545  firstitem.obj_type = 0;
546  firstitem.offset = 0;
547  } else {
548  btrfs_item* bi = CONTAINING_RECORD(r->items.Flink, btrfs_item, list_entry);
549 
550  firstitem = bi->key;
551  }
552 
553  eim2.ei.refcount = 1;
554  eim2.ei.generation = 1;
556  eim2.ei2.firstitem = firstitem;
557  eim2.ei2.level = 0;
558  eim2.type = TYPE_TREE_BLOCK_REF;
559  eim2.tbr.offset = r->id;
560 
561  add_item(extent_root, r->header.address, TYPE_EXTENT_ITEM, node_size, &eim2, sizeof(EXTENT_ITEM_METADATA2));
562  }
563 
564  if (r->id != BTRFS_ROOT_ROOT && r->id != BTRFS_ROOT_CHUNK) {
565  ROOT_ITEM ri;
566 
567  memset(&ri, 0, sizeof(ROOT_ITEM));
568 
569  ri.inode.generation = 1;
570  ri.inode.st_size = 3;
571  ri.inode.st_blocks = node_size;
572  ri.inode.st_nlink = 1;
573  ri.inode.st_mode = 040755;
574  ri.generation = 1;
575  ri.objid = r->id == 5 || r->id >= 0x100 ? SUBVOL_ROOT_INODE : 0;
576  ri.block_number = r->header.address;
577  ri.bytes_used = node_size;
578  ri.num_references = 1;
579  ri.generation2 = ri.generation;
580 
581  add_item(root_root, r->id, TYPE_ROOT_ITEM, 0, &ri, sizeof(ROOT_ITEM));
582  }
583 
584  le = le->Flink;
585  }
586 }
587 
590  uint16_t i;
593  CHUNK_ITEM_STRIPE* cis;
594 
595  cis = (CHUNK_ITEM_STRIPE*)&c->chunk_item[1];
596 
597  for (i = 0; i < c->chunk_item->num_stripes; i++) {
598  off.QuadPart = cis[i].offset + address - c->offset;
599 
601  if (!NT_SUCCESS(Status))
602  return Status;
603  }
604 
605  return STATUS_SUCCESS;
606 }
607 
608 static void calc_tree_checksum(tree_header* th, uint32_t node_size) {
609  switch (def_csum_type) {
610  case CSUM_TYPE_CRC32C:
611  *(uint32_t*)th = ~calc_crc32c(0xffffffff, (uint8_t*)&th->fs_uuid, node_size - sizeof(th->csum));
612  break;
613 
614  case CSUM_TYPE_XXHASH:
615  *(uint64_t*)th = XXH64((uint8_t*)&th->fs_uuid, node_size - sizeof(th->csum), 0);
616  break;
617 
618  case CSUM_TYPE_SHA256:
619  calc_sha256((uint8_t*)th, &th->fs_uuid, node_size - sizeof(th->csum));
620  break;
621 
622  case CSUM_TYPE_BLAKE2:
623  blake2b((uint8_t*)th, BLAKE2_HASH_SIZE, &th->fs_uuid, node_size - sizeof(th->csum));
624  break;
625  }
626 }
627 
628 static NTSTATUS write_roots(HANDLE h, LIST_ENTRY* roots, uint32_t node_size, BTRFS_UUID* fsuuid, BTRFS_UUID* chunkuuid) {
629  LIST_ENTRY *le, *le2;
631  uint8_t* tree;
632 
633 #ifndef __REACTOS__
634  tree = malloc(node_size);
635 #else
636  tree = RtlAllocateHeap(RtlGetProcessHeap(), 0, node_size);
637 #endif
638 
639  le = roots->Flink;
640  while (le != roots) {
642  uint8_t* dp;
643  leaf_node* ln;
644 
645  memset(tree, 0, node_size);
646 
647  r->header.num_items = 0;
648  r->header.fs_uuid = *fsuuid;
650  r->header.chunk_tree_uuid = *chunkuuid;
651  r->header.generation = 1;
652  r->header.tree_id = r->id;
653 
654  ln = (leaf_node*)(tree + sizeof(tree_header));
655 
656  dp = tree + node_size;
657 
658  le2 = r->items.Flink;
659  while (le2 != &r->items) {
661 
662  ln->key = item->key;
663  ln->size = item->size;
664 
665  if (item->size > 0) {
666  dp -= item->size;
667  memcpy(dp, item->data, item->size);
668 
669  ln->offset = (uint32_t)(dp - tree - sizeof(tree_header));
670  } else
671  ln->offset = 0;
672 
673  ln = &ln[1];
674 
675  r->header.num_items++;
676 
677  le2 = le2->Flink;
678  }
679 
680  memcpy(tree, &r->header, sizeof(tree_header));
681 
682  calc_tree_checksum((tree_header*)tree, node_size);
683 
684  Status = write_data(h, r->header.address, r->c, tree, node_size);
685  if (!NT_SUCCESS(Status)) {
686 #ifndef __REACTOS__
687  free(tree);
688 #else
689  RtlFreeHeap(RtlGetProcessHeap(), 0, tree);
690 #endif
691  return Status;
692  }
693 
694  le = le->Flink;
695  }
696 
697 #ifndef __REACTOS__
698  free(tree);
699 #else
700  RtlFreeHeap(RtlGetProcessHeap(), 0, tree);
701 #endif
702 
703  return STATUS_SUCCESS;
704 }
705 
706 #ifndef __REACTOS__
707 static void get_uuid(BTRFS_UUID* uuid) {
708 #else
709 static void get_uuid(BTRFS_UUID* uuid, ULONG* seed) {
710 #endif
711  uint8_t i;
712 
713  for (i = 0; i < 16; i+=2) {
714 #ifndef __REACTOS__
715  ULONG r = rand();
716 #else
717  ULONG r = RtlRandom(seed);
718 #endif
719 
720  uuid->uuid[i] = (r & 0xff00) >> 8;
721  uuid->uuid[i+1] = r & 0xff;
722  }
723 }
724 
725 #ifndef __REACTOS__
727 #else
728 static void init_device(btrfs_dev* dev, uint64_t id, uint64_t size, BTRFS_UUID* fsuuid, uint32_t sector_size, ULONG* seed) {
729 #endif
730  dev->dev_item.dev_id = id;
731  dev->dev_item.num_bytes = size;
732  dev->dev_item.bytes_used = 0;
733  dev->dev_item.optimal_io_align = sector_size;
734  dev->dev_item.optimal_io_width = sector_size;
735  dev->dev_item.minimal_io_size = sector_size;
736  dev->dev_item.type = 0;
737  dev->dev_item.generation = 0;
738  dev->dev_item.start_offset = 0;
739  dev->dev_item.dev_group = 0;
740  dev->dev_item.seek_speed = 0;
741  dev->dev_item.bandwidth = 0;
742 #ifndef __REACTOS__
743  get_uuid(&dev->dev_item.device_uuid);
744 #else
745  get_uuid(&dev->dev_item.device_uuid, seed);
746 #endif
747  dev->dev_item.fs_uuid = *fsuuid;
748 
749  dev->last_alloc = 0x100000; // skip first megabyte
750 }
751 
753  switch (def_csum_type) {
754  case CSUM_TYPE_CRC32C:
755  *(uint32_t*)sb = ~calc_crc32c(0xffffffff, (uint8_t*)&sb->uuid, (ULONG)sizeof(superblock) - sizeof(sb->checksum));
756  break;
757 
758  case CSUM_TYPE_XXHASH:
759  *(uint64_t*)sb = XXH64(&sb->uuid, sizeof(superblock) - sizeof(sb->checksum), 0);
760  break;
761 
762  case CSUM_TYPE_SHA256:
763  calc_sha256((uint8_t*)sb, &sb->uuid, sizeof(superblock) - sizeof(sb->checksum));
764  break;
765 
766  case CSUM_TYPE_BLAKE2:
767  blake2b((uint8_t*)sb, BLAKE2_HASH_SIZE, &sb->uuid, sizeof(superblock) - sizeof(sb->checksum));
768  break;
769  }
770 }
771 
772 static NTSTATUS write_superblocks(HANDLE h, btrfs_dev* dev, btrfs_root* chunk_root, btrfs_root* root_root, btrfs_root* extent_root,
773  btrfs_chunk* sys_chunk, uint32_t node_size, BTRFS_UUID* fsuuid, uint32_t sector_size, PUNICODE_STRING label, uint64_t incompat_flags) {
776  ULONG sblen;
777  int i;
778  superblock* sb;
779  KEY* key;
780  uint64_t bytes_used;
781  LIST_ENTRY* le;
782 
783  sblen = sizeof(*sb);
784  if (sblen & (sector_size - 1))
785  sblen = (sblen & sector_size) + sector_size;
786 
787  bytes_used = 0;
788 
789  le = extent_root->items.Flink;
790  while (le != &extent_root->items) {
792 
793  if (item->key.obj_type == TYPE_EXTENT_ITEM)
794  bytes_used += item->key.offset;
795  else if (item->key.obj_type == TYPE_METADATA_ITEM)
796  bytes_used += node_size;
797 
798  le = le->Flink;
799  }
800 
801 #ifndef __REACTOS__
802  sb = malloc(sblen);
803  memset(sb, 0, sblen);
804 #else
805  sb = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, sblen);
806 #endif
807 
808  sb->uuid = *fsuuid;
809  sb->flags = 1;
810  sb->magic = BTRFS_MAGIC;
811  sb->generation = 1;
812  sb->root_tree_addr = root_root->header.address;
813  sb->chunk_tree_addr = chunk_root->header.address;
814  sb->total_bytes = dev->dev_item.num_bytes;
815  sb->bytes_used = bytes_used;
817  sb->num_devices = 1;
819  sb->node_size = node_size;
820  sb->leaf_size = node_size;
822  sb->n = sizeof(KEY) + sizeof(CHUNK_ITEM) + (sys_chunk->chunk_item->num_stripes * sizeof(CHUNK_ITEM_STRIPE));
824  sb->incompat_flags = incompat_flags;
826  memcpy(&sb->dev_item, &dev->dev_item, sizeof(DEV_ITEM));
827 
828  if (label->Length > 0) {
829 #ifdef __REACTOS__
830  ANSI_STRING as;
831  unsigned int i;
832 
833  for (i = 0; i < label->Length / sizeof(WCHAR); i++) {
834 #else
835  ULONG utf8len;
836 
837  for (unsigned int i = 0; i < label->Length / sizeof(WCHAR); i++) {
838 #endif
839  if (label->Buffer[i] == '/' || label->Buffer[i] == '\\') {
840 #ifndef __REACTOS__
841  free(sb);
842 #else
843  RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
844 #endif
846  }
847  }
848 
849 #ifndef __REACTOS__
850  utf8len = WideCharToMultiByte(CP_UTF8, 0, label->Buffer, label->Length / sizeof(WCHAR), NULL, 0, NULL, NULL);
851 
852  if (utf8len == 0 || utf8len > MAX_LABEL_SIZE) {
853  free(sb);
855  }
856 
857  if (WideCharToMultiByte(CP_UTF8, 0, label->Buffer, label->Length / sizeof(WCHAR), sb->label, utf8len, NULL, NULL) == 0) {
858  free(sb);
860  }
861 #else
862  as.Buffer = sb->label;
863  as.Length = 0;
865 
867  {
868  RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
870  }
871 #endif
872  }
873  sb->cache_generation = 0xffffffffffffffff;
874 
875  key = (KEY*)sb->sys_chunk_array;
876  key->obj_id = 0x100;
877  key->obj_type = TYPE_CHUNK_ITEM;
878  key->offset = sys_chunk->offset;
879  memcpy(&key[1], sys_chunk->chunk_item, sizeof(CHUNK_ITEM) + (sys_chunk->chunk_item->num_stripes * sizeof(CHUNK_ITEM_STRIPE)));
880 
881  i = 0;
882  while (superblock_addrs[i] != 0) {
884 
885  if (superblock_addrs[i] > dev->dev_item.num_bytes)
886  break;
887 
889 
891 
892  off.QuadPart = superblock_addrs[i];
893 
894  Status = NtWriteFile(h, NULL, NULL, NULL, &iosb, sb, sblen, &off, NULL);
895  if (!NT_SUCCESS(Status)) {
896 #ifndef __REACTOS__
897  free(sb);
898 #else
899  RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
900 #endif
901  return Status;
902  }
903 
904  i++;
905  }
906 
907 #ifndef __REACTOS__
908  free(sb);
909 #else
910  RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
911 #endif
912 
913  return STATUS_SUCCESS;
914 }
915 
916 static __inline void win_time_to_unix(LARGE_INTEGER t, BTRFS_TIME* out) {
917  ULONGLONG l = t.QuadPart - 116444736000000000;
918 
919  out->seconds = l / 10000000;
920  out->nanoseconds = (l % 10000000) * 100;
921 }
922 
923 #ifdef __REACTOS__
924 VOID
925 WINAPI
927 {
928  LARGE_INTEGER SystemTime;
929 
930  do
931  {
932  SystemTime.HighPart = SharedUserData->SystemTime.High1Time;
933  SystemTime.LowPart = SharedUserData->SystemTime.LowPart;
934  }
935  while (SystemTime.HighPart != SharedUserData->SystemTime.High2Time);
936 
937  lpFileTime->dwLowDateTime = SystemTime.LowPart;
938  lpFileTime->dwHighDateTime = SystemTime.HighPart;
939 }
940 #endif
941 
943  uint16_t name_len = (uint16_t)strlen(name);
944 #ifndef __REACTOS__
945  INODE_REF* ir = malloc(offsetof(INODE_REF, name[0]) + name_len);
946 #else
947  INODE_REF* ir = RtlAllocateHeap(RtlGetProcessHeap(), 0, offsetof(INODE_REF, name[0]) + name_len);
948 #endif
949 
950  ir->index = 0;
951  ir->n = name_len;
952  memcpy(ir->name, name, name_len);
953 
955 
956 #ifndef __REACTOS__
957  free(ir);
958 #else
959  RtlFreeHeap(RtlGetProcessHeap(), 0, ir);
960 #endif
961 }
962 
963 static void init_fs_tree(btrfs_root* r, uint32_t node_size) {
964  INODE_ITEM ii;
967 
968  memset(&ii, 0, sizeof(INODE_ITEM));
969 
970  ii.generation = 1;
971  ii.st_blocks = node_size;
972  ii.st_nlink = 1;
973  ii.st_mode = 040755;
974 
976  time.LowPart = filetime.dwLowDateTime;
977  time.HighPart = filetime.dwHighDateTime;
978 
980  ii.st_ctime = ii.st_mtime = ii.st_atime;
981 
983 
985 }
986 
987 static void add_block_group_items(LIST_ENTRY* chunks, btrfs_root* extent_root) {
988  LIST_ENTRY* le;
989 
990  le = chunks->Flink;
991  while (le != chunks) {
993  BLOCK_GROUP_ITEM bgi;
994 
995  bgi.used = c->used;
996  bgi.chunk_tree = 0x100;
997  bgi.flags = c->chunk_item->type;
998  add_item(extent_root, c->offset, TYPE_BLOCK_GROUP_ITEM, c->chunk_item->size, &bgi, sizeof(BLOCK_GROUP_ITEM));
999 
1000  le = le->Flink;
1001  }
1002 }
1003 
1005  NTSTATUS Status;
1008  uint8_t* mb;
1009 
1010 #ifndef __REACTOS__
1011  mb = malloc(0x100000);
1012  memset(mb, 0, 0x100000);
1013 #else
1014  mb = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, 0x100000);
1015 #endif
1016 
1017  zero.QuadPart = 0;
1018 
1019  Status = NtWriteFile(h, NULL, NULL, NULL, &iosb, mb, 0x100000, &zero, NULL);
1020 
1021 #ifndef __REACTOS__
1022  free(mb);
1023 #else
1024  RtlFreeHeap(RtlGetProcessHeap(), 0, mb);
1025 #endif
1026 
1027  return Status;
1028 }
1029 
1030 static bool is_ssd(HANDLE h) {
1031  ULONG aptelen;
1032  ATA_PASS_THROUGH_EX* apte;
1034  NTSTATUS Status;
1035  IDENTIFY_DEVICE_DATA* idd;
1036 
1037  aptelen = sizeof(ATA_PASS_THROUGH_EX) + 512;
1038 #ifndef __REACTOS__
1039  apte = malloc(aptelen);
1040 
1041  RtlZeroMemory(apte, aptelen);
1042 #else
1043  apte = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, aptelen);
1044 #endif
1045 
1046  apte->Length = sizeof(ATA_PASS_THROUGH_EX);
1047  apte->AtaFlags = ATA_FLAGS_DATA_IN;
1048  apte->DataTransferLength = aptelen - sizeof(ATA_PASS_THROUGH_EX);
1049  apte->TimeOutValue = 3;
1050  apte->DataBufferOffset = apte->Length;
1052 
1053  Status = NtDeviceIoControlFile(h, NULL, NULL, NULL, &iosb, IOCTL_ATA_PASS_THROUGH, apte, aptelen, apte, aptelen);
1054 
1055  if (NT_SUCCESS(Status)) {
1056  idd = (IDENTIFY_DEVICE_DATA*)((uint8_t*)apte + sizeof(ATA_PASS_THROUGH_EX));
1057 
1058  if (idd->NominalMediaRotationRate == 1) {
1059 #ifndef __REACTOS__
1060  free(apte);
1061 #else
1062  RtlFreeHeap(RtlGetProcessHeap(), 0, apte);
1063 #endif
1064  return true;
1065  }
1066  }
1067 
1068 #ifndef __REACTOS__
1069  free(apte);
1070 #else
1071  RtlFreeHeap(RtlGetProcessHeap(), 0, apte);
1072 #endif
1073 
1074  return false;
1075 }
1076 
1078  uint64_t key_offset, uint64_t transid, uint8_t type, const char* name) {
1079  uint16_t name_len = (uint16_t)strlen(name);
1080 #ifndef __REACTOS__
1081  DIR_ITEM* di = malloc(offsetof(DIR_ITEM, name[0]) + name_len);
1082 #else
1083  DIR_ITEM* di = RtlAllocateHeap(RtlGetProcessHeap(), 0, offsetof(DIR_ITEM, name[0]) + name_len);
1084 #endif
1085 
1086  di->key.obj_id = key_objid;
1087  di->key.obj_type = key_type;
1088  di->key.offset = key_offset;
1089  di->transid = transid;
1090  di->m = 0;
1091  di->n = name_len;
1092  di->type = type;
1093  memcpy(di->name, name, name_len);
1094 
1095  add_item(root, inode, TYPE_DIR_ITEM, hash, di, (uint16_t)(offsetof(DIR_ITEM, name[0]) + di->m + di->n));
1096 
1097 #ifndef __REACTOS__
1098  free(di);
1099 #else
1100  RtlFreeHeap(RtlGetProcessHeap(), 0, di);
1101 #endif
1102 }
1103 
1104 static void set_default_subvol(btrfs_root* root_root, uint32_t node_size) {
1105  INODE_ITEM ii;
1108 
1109  static const char default_subvol[] = "default";
1110  static const uint32_t default_hash = 0x8dbfc2d2;
1111 
1112  add_inode_ref(root_root, BTRFS_ROOT_FSTREE, BTRFS_ROOT_TREEDIR, 0, default_subvol);
1113 
1114  memset(&ii, 0, sizeof(INODE_ITEM));
1115 
1116  ii.generation = 1;
1117  ii.st_blocks = node_size;
1118  ii.st_nlink = 1;
1119  ii.st_mode = 040755;
1120 
1122  time.LowPart = filetime.dwLowDateTime;
1123  time.HighPart = filetime.dwHighDateTime;
1124 
1126  ii.st_ctime = ii.st_mtime = ii.otime = ii.st_atime;
1127 
1128  add_item(root_root, BTRFS_ROOT_TREEDIR, TYPE_INODE_ITEM, 0, &ii, sizeof(INODE_ITEM));
1129 
1130  add_inode_ref(root_root, BTRFS_ROOT_TREEDIR, BTRFS_ROOT_TREEDIR, 0, "..");
1131 
1133  0xffffffffffffffff, 0, BTRFS_TYPE_DIRECTORY, default_subvol);
1134 }
1135 
1137  NTSTATUS Status;
1138  LIST_ENTRY roots, chunks;
1139  btrfs_root *root_root, *chunk_root, *extent_root, *dev_root, *fs_root, *reloc_root;
1140  btrfs_chunk *sys_chunk, *metadata_chunk;
1141  btrfs_dev dev;
1142  BTRFS_UUID fsuuid, chunkuuid;
1143  bool ssd;
1144  uint64_t metadata_flags;
1145 #ifdef __REACTOS__
1146  ULONG seed;
1147 #endif
1148 
1149 #ifndef __REACTOS__
1150  srand((unsigned int)time(0));
1151  get_uuid(&fsuuid);
1152  get_uuid(&chunkuuid);
1153 #else
1154  seed = NtGetTickCount();
1155  get_uuid(&fsuuid, &seed);
1156  get_uuid(&chunkuuid, &seed);
1157 #endif
1158 
1159  InitializeListHead(&roots);
1160  InitializeListHead(&chunks);
1161 
1162  root_root = add_root(&roots, BTRFS_ROOT_ROOT);
1163  chunk_root = add_root(&roots, BTRFS_ROOT_CHUNK);
1164  extent_root = add_root(&roots, BTRFS_ROOT_EXTENT);
1165  dev_root = add_root(&roots, BTRFS_ROOT_DEVTREE);
1166  add_root(&roots, BTRFS_ROOT_CHECKSUM);
1167  fs_root = add_root(&roots, BTRFS_ROOT_FSTREE);
1169 
1170 #ifndef __REACTOS__
1171  init_device(&dev, 1, size, &fsuuid, sector_size);
1172 #else
1173  init_device(&dev, 1, size, &fsuuid, sector_size, &seed);
1174 #endif
1175 
1176  ssd = is_ssd(h);
1177 
1178  sys_chunk = add_chunk(&chunks, BLOCK_FLAG_SYSTEM | (ssd ? 0 : BLOCK_FLAG_DUPLICATE), chunk_root, &dev, dev_root, &chunkuuid, sector_size);
1179  if (!sys_chunk)
1180  return STATUS_INTERNAL_ERROR;
1181 
1182  metadata_flags = BLOCK_FLAG_METADATA;
1183 
1184  if (!ssd && !(incompat_flags & BTRFS_INCOMPAT_FLAGS_MIXED_GROUPS))
1185  metadata_flags |= BLOCK_FLAG_DUPLICATE;
1186 
1187  if (incompat_flags & BTRFS_INCOMPAT_FLAGS_MIXED_GROUPS)
1188  metadata_flags |= BLOCK_FLAG_DATA;
1189 
1190  metadata_chunk = add_chunk(&chunks, metadata_flags, chunk_root, &dev, dev_root, &chunkuuid, sector_size);
1191  if (!metadata_chunk)
1192  return STATUS_INTERNAL_ERROR;
1193 
1194  add_item(chunk_root, 1, TYPE_DEV_ITEM, dev.dev_item.dev_id, &dev.dev_item, sizeof(DEV_ITEM));
1195 
1196  set_default_subvol(root_root, node_size);
1197 
1198  init_fs_tree(fs_root, node_size);
1199  init_fs_tree(reloc_root, node_size);
1200 
1201  assign_addresses(&roots, sys_chunk, metadata_chunk, node_size, root_root, extent_root, incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA);
1202 
1203  add_block_group_items(&chunks, extent_root);
1204 
1205  Status = write_roots(h, &roots, node_size, &fsuuid, &chunkuuid);
1206  if (!NT_SUCCESS(Status))
1207  return Status;
1208 
1210  if (!NT_SUCCESS(Status))
1211  return Status;
1212 
1213  Status = write_superblocks(h, &dev, chunk_root, root_root, extent_root, sys_chunk, node_size, &fsuuid, sector_size, label, incompat_flags);
1214  if (!NT_SUCCESS(Status))
1215  return Status;
1216 
1217  free_roots(&roots);
1218  free_chunks(&chunks);
1219 
1220  return STATUS_SUCCESS;
1221 }
1222 
1223 static bool look_for_device(btrfs_filesystem* bfs, BTRFS_UUID* devuuid) {
1224  uint32_t i;
1226 
1227  for (i = 0; i < bfs->num_devices; i++) {
1228  if (i == 0)
1229  dev = &bfs->device;
1230  else
1232 
1233  if (RtlCompareMemory(&dev->uuid, devuuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID))
1234  return true;
1235  }
1236 
1237  return false;
1238 }
1239 
1241  switch (sb->csum_type) {
1242  case CSUM_TYPE_CRC32C: {
1243  uint32_t crc32 = ~calc_crc32c(0xffffffff, (uint8_t*)&sb->uuid, (ULONG)sizeof(superblock) - sizeof(sb->checksum));
1244 
1245  return crc32 == *(uint32_t*)sb;
1246  }
1247 
1248  case CSUM_TYPE_XXHASH: {
1249  uint64_t hash = XXH64(&sb->uuid, sizeof(superblock) - sizeof(sb->checksum), 0);
1250 
1251  return hash == *(uint64_t*)sb;
1252  }
1253 
1254  case CSUM_TYPE_SHA256: {
1256 
1257  calc_sha256(hash, &sb->uuid, sizeof(superblock) - sizeof(sb->checksum));
1258 
1259  return !memcmp(hash, sb, SHA256_HASH_SIZE);
1260  }
1261 
1262  case CSUM_TYPE_BLAKE2: {
1264 
1265  blake2b(hash, sizeof(hash), &sb->uuid, sizeof(superblock) - sizeof(sb->checksum));
1266 
1267  return !memcmp(hash, sb, BLAKE2_HASH_SIZE);
1268  }
1269 
1270  default:
1271  return false;
1272  }
1273 }
1274 
1276  NTSTATUS Status;
1277  superblock* sb;
1278  ULONG sblen;
1281  BTRFS_UUID fsuuid, devuuid;
1284  HANDLE h2;
1285  btrfs_filesystem *bfs = NULL, *bfs2;
1286  ULONG bfssize;
1287  bool ret = false;
1288 
1289  static WCHAR btrfs[] = L"\\Btrfs";
1290 
1291  sblen = sizeof(*sb);
1292  if (sblen & (sector_size - 1))
1293  sblen = (sblen & sector_size) + sector_size;
1294 
1295 #ifndef __REACTOS__
1296  sb = malloc(sblen);
1297 #else
1298  sb = RtlAllocateHeap(RtlGetProcessHeap(), 0, sblen);
1299 #endif
1300 
1301  off.QuadPart = superblock_addrs[0];
1302 
1303  Status = NtReadFile(h, NULL, NULL, NULL, &iosb, sb, sblen, &off, NULL);
1304  if (!NT_SUCCESS(Status)) {
1305 #ifndef __REACTOS__
1306  free(sb);
1307 #else
1308  RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
1309 #endif
1310  return false;
1311  }
1312 
1313  if (sb->magic != BTRFS_MAGIC) {
1314 #ifndef __REACTOS__
1315  free(sb);
1316 #else
1317  RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
1318 #endif
1319  return false;
1320  }
1321 
1322  if (!check_superblock_checksum(sb)) {
1323 #ifndef __REACTOS__
1324  free(sb);
1325 #else
1326  RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
1327 #endif
1328  return false;
1329  }
1330 
1331  fsuuid = sb->uuid;
1332  devuuid = sb->dev_item.device_uuid;
1333 
1334 #ifndef __REACTOS__
1335  free(sb);
1336 #else
1337  RtlFreeHeap(RtlGetProcessHeap(), 0, sb);
1338 #endif
1339 
1340  us.Length = us.MaximumLength = (USHORT)(wcslen(btrfs) * sizeof(WCHAR));
1341  us.Buffer = btrfs;
1342 
1344 
1347  if (!NT_SUCCESS(Status)) // not a problem, it usually just means the driver isn't loaded
1348  return false;
1349 
1350  bfssize = 0;
1351 
1352  do {
1353  bfssize += 1024;
1354 
1355 #ifndef __REACTOS__
1356  if (bfs) free(bfs);
1357  bfs = malloc(bfssize);
1358 #else
1359  if (bfs) RtlFreeHeap(RtlGetProcessHeap(), 0, bfs);
1360  bfs = RtlAllocateHeap(RtlGetProcessHeap(), 0, bfssize);
1361 #endif
1362 
1365  NtClose(h2);
1366  return false;
1367  }
1368  } while (Status == STATUS_BUFFER_OVERFLOW);
1369 
1370  if (!NT_SUCCESS(Status))
1371  goto end;
1372 
1373  if (bfs->num_devices != 0) {
1374  bfs2 = bfs;
1375  while (true) {
1376  if (RtlCompareMemory(&bfs2->uuid, &fsuuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
1377  if (bfs2->num_devices == 1)
1378  ret = false;
1379  else
1380  ret = look_for_device(bfs2, &devuuid);
1381 
1382  goto end;
1383  }
1384 
1385  if (bfs2->next_entry == 0)
1386  break;
1387  else
1388  bfs2 = (btrfs_filesystem*)((uint8_t*)bfs2 + bfs2->next_entry);
1389  }
1390  }
1391 
1392 end:
1393  NtClose(h2);
1394 
1395  if (bfs)
1396 #ifndef __REACTOS__
1397  free(bfs);
1398 #else
1399  RtlFreeHeap(RtlGetProcessHeap(), 0, bfs);
1400 #endif
1401 
1402  return ret;
1403 }
1404 
1405 static void do_full_trim(HANDLE h) {
1408 
1410 
1411  dmdsa.Size = sizeof(DEVICE_MANAGE_DATA_SET_ATTRIBUTES);
1412  dmdsa.Action = DeviceDsmAction_Trim;
1414  dmdsa.ParameterBlockOffset = 0;
1415  dmdsa.ParameterBlockLength = 0;
1416  dmdsa.DataSetRangesOffset = 0;
1417  dmdsa.DataSetRangesLength = 0;
1418 
1420 }
1421 
1422 static bool is_power_of_two(ULONG i) {
1423  return ((i != 0) && !(i & (i - 1)));
1424 }
1425 
1426 #if !defined(__REACTOS__) && (defined(_X86_) || defined(_AMD64_))
1427 static void check_cpu() {
1428  unsigned int cpuInfo[4];
1429  bool have_sse42;
1430 
1431 #ifndef _MSC_VER
1432  __get_cpuid(1, &cpuInfo[0], &cpuInfo[1], &cpuInfo[2], &cpuInfo[3]);
1433  have_sse42 = cpuInfo[2] & bit_SSE4_2;
1434 #else
1435  __cpuid(cpuInfo, 1);
1436  have_sse42 = cpuInfo[2] & (1 << 20);
1437 #endif
1438 
1439  if (have_sse42)
1440  calc_crc32c = calc_crc32c_hw;
1441 }
1442 #endif
1443 
1444 #ifndef __REACTOS__
1446 #else
1448 #endif // __REACTOS__
1450 {
1451  NTSTATUS Status;
1452  HANDLE h, btrfsh;
1456  DISK_GEOMETRY dg;
1457  uint32_t sector_size, node_size;
1458  UNICODE_STRING btrfsus;
1459 #ifndef __REACTOS__
1460  HANDLE token;
1462  LUID luid;
1463 #endif
1464  uint64_t incompat_flags;
1465  UNICODE_STRING empty_label;
1466 
1467  static WCHAR btrfs[] = L"\\Btrfs";
1468 
1469 #ifndef __REACTOS__
1472 
1473  if (!LookupPrivilegeValueW(NULL, L"SeManageVolumePrivilege", &luid)) {
1474  CloseHandle(token);
1476  }
1477 
1478  tp.PrivilegeCount = 1;
1479  tp.Privileges[0].Luid = luid;
1480  tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1481 
1482  if (!AdjustTokenPrivileges(token, false, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) {
1483  CloseHandle(token);
1485  }
1486 
1487  CloseHandle(token);
1488 
1489 #if defined(_X86_) || defined(_AMD64_)
1490  check_cpu();
1491 #endif
1492 #endif
1493 
1496  return STATUS_INVALID_PARAMETER;
1497 
1499 
1502 
1503  if (!NT_SUCCESS(Status))
1504  return Status;
1505 
1507  if (!NT_SUCCESS(Status)) {
1508  NtClose(h);
1509  return Status;
1510  }
1511 
1512  // MSDN tells us to use IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, but there are
1513  // some instances where it fails and IOCTL_DISK_GET_DRIVE_GEOMETRY succeeds -
1514  // such as with spanned volumes.
1516  if (!NT_SUCCESS(Status)) {
1517  NtClose(h);
1518  return Status;
1519  }
1520 
1521  if (def_sector_size == 0) {
1523 
1524  if (sector_size == 0x200 || sector_size == 0)
1525  sector_size = 0x1000;
1526  } else {
1528  NtClose(h);
1529  return STATUS_INVALID_PARAMETER;
1530  }
1531 
1533  }
1534 
1535  if (def_node_size == 0)
1536  node_size = 0x4000;
1537  else {
1539  NtClose(h);
1540  return STATUS_INVALID_PARAMETER;
1541  }
1542 
1543  node_size = def_node_size;
1544  }
1545 
1546  if (Callback) {
1547  ULONG pc = 0;
1548  Callback(PROGRESS, 0, (PVOID)&pc);
1549  }
1550 
1552 
1555  goto end;
1556  }
1557 
1558  do_full_trim(h);
1559 
1560  incompat_flags = def_incompat_flags;
1562 
1563  if (!Label) {
1564  empty_label.Buffer = NULL;
1565  empty_label.Length = empty_label.MaximumLength = 0;
1566  Label = &empty_label;
1567  }
1568 
1569  Status = write_btrfs(h, gli.Length.QuadPart, Label, sector_size, node_size, incompat_flags);
1570 
1572 
1573 end:
1575 
1576  NtClose(h);
1577 
1578  if (NT_SUCCESS(Status)) {
1579  btrfsus.Buffer = btrfs;
1580  btrfsus.Length = btrfsus.MaximumLength = (USHORT)(wcslen(btrfs) * sizeof(WCHAR));
1581 
1582  InitializeObjectAttributes(&attr, &btrfsus, 0, NULL, NULL);
1583 
1586 
1587  if (NT_SUCCESS(Status)) {
1588  MOUNTDEV_NAME* mdn;
1589  ULONG mdnsize;
1590 
1591  mdnsize = (ULONG)(offsetof(MOUNTDEV_NAME, Name[0]) + DriveRoot->Length);
1592 #ifndef __REACTOS__
1593  mdn = malloc(mdnsize);
1594 #else
1595  mdn = RtlAllocateHeap(RtlGetProcessHeap(), 0, mdnsize);
1596 #endif
1597 
1598  mdn->NameLength = DriveRoot->Length;
1599  memcpy(mdn->Name, DriveRoot->Buffer, DriveRoot->Length);
1600 
1601  NtDeviceIoControlFile(btrfsh, NULL, NULL, NULL, &iosb, IOCTL_BTRFS_PROBE_VOLUME, mdn, mdnsize, NULL, 0);
1602 
1603 #ifndef __REACTOS__
1604  free(mdn);
1605 #else
1606  RtlFreeHeap(RtlGetProcessHeap(), 0, mdn);
1607 #endif
1608 
1609  NtClose(btrfsh);
1610  }
1611 
1613  }
1614 
1615  if (Callback) {
1616  bool success = NT_SUCCESS(Status);
1617  Callback(DONE, 0, (PVOID)&success);
1618  }
1619 
1620  return Status;
1621 }
1622 
1624  UNICODE_STRING DriveRoot, Label;
1625  NTSTATUS Status;
1626 
1627  if (!root || !root->string)
1628  return false;
1629 
1630  DriveRoot.Length = DriveRoot.MaximumLength = (USHORT)(wcslen(root->string) * sizeof(WCHAR));
1631  DriveRoot.Buffer = root->string;
1632 
1633  if (opts && opts->label && opts->label->string) {
1634  Label.Length = Label.MaximumLength = (USHORT)(wcslen(opts->label->string) * sizeof(WCHAR));
1635  Label.Buffer = opts->label->string;
1636  } else {
1637  Label.Length = Label.MaximumLength = 0;
1638  Label.Buffer = NULL;
1639  }
1640 
1641 #ifndef __REACTOS__
1642  Status = FormatEx2(&DriveRoot, FMIFS_HARDDISK, &Label, opts && opts->flags & FORMAT_FLAG_QUICK_FORMAT, 0, NULL);
1643 #else
1644  Status = BtrfsFormatEx(&DriveRoot, FMIFS_HARDDISK, &Label, opts && opts->flags & FORMAT_FLAG_QUICK_FORMAT, 0, NULL);
1645 #endif
1646 
1647  return NT_SUCCESS(Status);
1648 }
1649 
1651  if (sector != 0)
1653 
1654  if (node != 0)
1655  def_node_size = node;
1656 }
1657 
1658 void __stdcall SetIncompatFlags(uint64_t incompat_flags) {
1659  def_incompat_flags = incompat_flags;
1660 }
1661 
1662 void __stdcall SetCsumType(uint16_t csum_type) {
1663  def_csum_type = csum_type;
1664 }
1665 
1667  // STUB - undocumented
1668 
1669  return true;
1670 }
1671 
1672 #ifndef __REACTOS__
1675  module = (HMODULE)hModule;
1676 
1677  return true;
1678 }
1679 #endif
void * table
Definition: btrfslib.c:94
signed char * PCHAR
Definition: retypes.h:7
uint8_t sys_chunk_array[SYS_CHUNK_ARRAY_SIZE]
Definition: btrfs.h:250
#define FILE_GENERIC_READ
Definition: nt_native.h:653
NTSYSAPI ULONG NTAPI RtlRandom(_Inout_ PULONG Seed)
uint64_t obj_id
Definition: btrfs.h:137
static PIO_STATUS_BLOCK iosb
Definition: file.c:98
void __stdcall SetCsumType(uint16_t csum_type)
Definition: btrfslib.c:1662
#define TYPE_DEV_ITEM
Definition: btrfs.h:43
uint32_t sector
Definition: isohybrid.c:61
_In_opt_ HANDLE _In_opt_ PIO_APC_ROUTINE _In_opt_ PVOID ApcContext
Definition: iofuncs.h:719
VOID WINAPI GetSystemTimeAsFileTime(OUT PFILETIME lpFileTime)
Definition: time.c:128
#define STATUS_PRIVILEGE_NOT_HELD
Definition: DriverTester.h:9
#define max(a, b)
Definition: svc.c:63
Definition: tftpd.h:59
uint64_t lastoff
Definition: btrfslib.c:147
struct TEXTOUTPUT * PTEXTOUTPUT
static NTSTATUS write_superblocks(HANDLE h, btrfs_dev *dev, btrfs_root *chunk_root, btrfs_root *root_root, btrfs_root *extent_root, btrfs_chunk *sys_chunk, uint32_t node_size, BTRFS_UUID *fsuuid, uint32_t sector_size, PUNICODE_STRING label, uint64_t incompat_flags)
Definition: btrfslib.c:772
#define CloseHandle
Definition: compat.h:407
VOID(* PIO_APC_ROUTINE)(IN PVOID ApcContext, IN PIO_STATUS_BLOCK IoStatusBlock, IN ULONG Reserved)
Definition: nt_native.h:877
uint8_t obj_type
Definition: btrfs.h:138
BOOL WINAPI LookupPrivilegeValueW(LPCWSTR lpSystemName, LPCWSTR lpPrivilegeName, PLUID lpLuid)
Definition: misc.c:782
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
static NTSTATUS write_roots(HANDLE h, LIST_ENTRY *roots, uint32_t node_size, BTRFS_UUID *fsuuid, BTRFS_UUID *chunkuuid)
Definition: btrfslib.c:628
uint64_t bytes_used
Definition: btrfs.h:228
BTRFS_TIME otime
Definition: btrfs.h:296
#define WideCharToMultiByte
Definition: compat.h:101
EXTENT_ITEM2 ei2
Definition: btrfslib.c:510
NTSTATUS NTAPI NtReadFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
struct _Entry Entry
Definition: kefuncs.h:627
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
static uint64_t find_chunk_offset(uint64_t size, uint64_t offset, btrfs_dev *dev, btrfs_root *dev_root, BTRFS_UUID *chunkuuid)
Definition: btrfslib.c:372
static bool look_for_device(btrfs_filesystem *bfs, BTRFS_UUID *devuuid)
Definition: btrfslib.c:1223
USHORT MaximumLength
Definition: env_spec_w32.h:370
static void set_default_subvol(btrfs_root *root_root, uint32_t node_size)
Definition: btrfslib.c:1104
BOOL Verbose
Definition: chkdsk.c:72
DEV_ITEM dev_item
Definition: btrfslib.c:161
uint64_t last_alloc
Definition: btrfslib.c:162
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
LIST_ENTRY list_entry
Definition: btrfs_drv.h:482
uint64_t root_tree_addr
Definition: btrfs.h:223
uint64_t bytes_used
Definition: btrfs.h:305
uint32 __fastcall crc32(IN uint8 *s, IN uint32 len)
Definition: udf_info.cpp:4290
uint64_t dev_id
Definition: btrfs.h:340
static bool is_power_of_two(ULONG i)
Definition: btrfslib.c:1422
uint64_t magic
Definition: btrfs.h:221
char label[MAX_LABEL_SIZE]
Definition: btrfs.h:245
superblock * sb
Definition: btrfs.c:4220
static bool is_mounted_multi_device(HANDLE h, uint32_t sector_size)
Definition: btrfslib.c:1275
static const BYTE us[]
Definition: encode.c:689
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
BOOL __stdcall FormatEx(DSTRING *root, STREAM_MESSAGE *message, options *opts, uint32_t unk1)
Definition: btrfslib.c:1623
#define CSUM_TYPE_BLAKE2
Definition: btrfs.h:128
uint64_t flags
Definition: btrfs.h:414
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
struct _LIST_ENTRY * Blink
Definition: typedefs.h:121
#define IOCTL_BTRFS_QUERY_FILESYSTEMS
Definition: btrfsioctl.h:21
#define SUBVOL_ROOT_INODE
Definition: propsheet.cpp:42
void * table
Definition: btrfslib.c:100
struct _tree tree
char * reloc_root
Definition: mkisofs.c:177
#define BTRFS_INCOMPAT_FLAGS_EXTENDED_IREF
Definition: btrfs.h:114
#define free
Definition: debug_ros.c:5
struct _root root
static NTSTATUS write_btrfs(HANDLE h, uint64_t size, PUNICODE_STRING label, uint32_t sector_size, uint32_t node_size, uint64_t incompat_flags)
Definition: btrfslib.c:1136
#define TYPE_CHUNK_ITEM
Definition: btrfs.h:44
LONG NTSTATUS
Definition: precomp.h:26
static void add_inode_ref(btrfs_root *r, uint64_t inode, uint64_t parent, uint64_t index, const char *name)
Definition: btrfslib.c:942
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
#define FORMAT_FLAG_QUICK_FORMAT
Definition: btrfslib.c:103
GLintptr offset
Definition: glext.h:5920
NTSTATUS NTAPI NtWriteFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, PVOID Buffer, ULONG Length, PLARGE_INTEGER ByteOffset, PULONG Key)
#define TYPE_TREE_BLOCK_REF
Definition: btrfs.h:33
void __cdecl srand(_In_ unsigned int _Seed)
uint64_t length
Definition: btrfs.h:460
ULONG_PTR DataBufferOffset
Definition: ntddscsi.h:139
struct _DEVICE_MANAGE_DATA_SET_ATTRIBUTES DEVICE_MANAGE_DATA_SET_ATTRIBUTES
GLdouble GLdouble t
Definition: gl.h:2047
#define ATA_FLAGS_DATA_IN
Definition: uata_ctl.h:222
_Must_inspect_result_ _In_ PFILE_OBJECT _In_ ULONG _In_ ULONG _In_ ULONG OutputBufferLength
Definition: fltkernel.h:1374
Definition: write.c:111
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
void __stdcall SetIncompatFlags(uint64_t incompat_flags)
Definition: btrfslib.c:1658
#define IDE_COMMAND_IDENTIFY
Definition: atapi.h:118
uint64_t block_number
Definition: btrfs.h:303
#define BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA
Definition: btrfs.h:116
#define NTSYSCALLAPI
Definition: ntbasedef.h:204
#define DEVICE_DSM_FLAG_TRIM_NOT_FS_ALLOCATED
Definition: btrfslib.c:74
#define DLL_PROCESS_ATTACH
Definition: compat.h:120
ULONG BytesPerSector
Definition: ntdddisk.h:381
uint8_t csum[32]
Definition: btrfs.h:147
GLuint GLuint end
Definition: gl.h:1545
#define CSUM_TYPE_CRC32C
Definition: btrfs.h:125
NTSYSAPI NTSTATUS NTAPI NtDeviceIoControlFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG DeviceIoControlCode, IN PVOID InBuffer OPTIONAL, IN ULONG InBufferLength, OUT PVOID OutBuffer OPTIONAL, IN ULONG OutBufferLength)
EXTENT_ITEM ei
Definition: btrfslib.c:509
__u16 time
Definition: mkdosfs.c:366
#define STATUS_INVALID_VOLUME_LABEL
Definition: udferr_usr.h:156
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
unsigned short int uint16_t
Definition: acefiex.h:54
#define uint16_t
Definition: nsiface.idl:60
TREE_BLOCK_REF tbr
Definition: btrfslib.c:512
#define TYPE_METADATA_ITEM
Definition: btrfs.h:32
LIST_ENTRY list_entry
Definition: btrfslib.c:141
CALLBACKCOMMAND
Definition: fmifs.h:66
uint16_t csum_type
Definition: btrfs.h:240
uint64_t used
Definition: btrfs.h:412
BTRFS_TIME st_ctime
Definition: btrfs.h:294
DSTRING * label
Definition: btrfslib.c:114
#define HEADER_FLAG_MIXED_BACKREF
Definition: btrfs.h:144
static void add_block_group_items(LIST_ENTRY *chunks, btrfs_root *extent_root)
Definition: btrfslib.c:987
uint64_t offset
Definition: btrfs.h:139
#define NtGetTickCount
Definition: rtlp.h:146
#define BTRFS_ROOT_DATA_RELOC
Definition: btrfs.h:59
static int dev
Definition: mkdosfs.c:536
CHAR InputBuffer[80]
Definition: conmgr.c:33
#define FILE_SHARE_READ
Definition: compat.h:125
uint64_t chunk_tree_addr
Definition: btrfs.h:224
static btrfs_root * add_root(LIST_ENTRY *roots, uint64_t id)
Definition: btrfslib.c:255
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
DEVICE_DATA_MANAGEMENT_SET_ACTION Action
Definition: ntddstor.h:578
uint16_t unk1
Definition: btrfslib.c:111
uint64_t generation2
Definition: btrfs.h:312
Definition: fs.h:78
#define STATUS_INTERNAL_ERROR
Definition: ntstatus.h:451
DWORD dwReason
Definition: misc.cpp:154
uint16_t size
Definition: btrfslib.c:139
struct node node
uint64_t offset
Definition: btrfs.h:341
static DWORD unk1
Definition: cursoricon.c:1638
static bool check_superblock_checksum(superblock *sb)
Definition: btrfslib.c:1240
HANDLE FileHandle
Definition: stats.c:38
#define FSCTL_UNLOCK_VOLUME
Definition: btrfslib.c:70
uint8_t checksum[32]
Definition: btrfs.h:217
NTSTATUS NTAPI BtrfsFormatEx(IN PUNICODE_STRING DriveRoot, IN FMIFS_MEDIA_FLAG MediaFlag, IN PUNICODE_STRING Label, IN BOOLEAN QuickFormat, IN ULONG ClusterSize, IN PFMIFSCALLBACK Callback)
btrfs_chunk * c
Definition: btrfslib.c:155
#define SE_PRIVILEGE_ENABLED
Definition: setypes.h:63
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
uint64_t address
Definition: btrfs.h:149
#define FILE_SYNCHRONOUS_IO_ALERT
Definition: from_kernel.h:30
uint16_t m
Definition: btrfs.h:268
uint32_t num_devices
Definition: btrfsioctl.h:190
static NTSTATUS NTAPI FormatEx2(PUNICODE_STRING DriveRoot, FMIFS_MEDIA_FLAG MediaFlag, PUNICODE_STRING Label, BOOLEAN QuickFormat, ULONG ClusterSize, PFMIFSCALLBACK Callback)
Definition: btrfslib.c:1445
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
uint32_t stripe_size
Definition: btrfs.h:234
uint64_t refcount
Definition: btrfs.h:380
#define CP_UTF8
Definition: nls.h:20
uint64_t id
Definition: btrfs_drv.h:467
unsigned int BOOL
Definition: ntddk_ex.h:94
USHORT NominalMediaRotationRate
Definition: ata.h:243
uint64_t generation
Definition: btrfs.h:301
_Check_return_ int __cdecl rand(void)
Definition: rand.c:10
uint16_t def_csum_type
Definition: btrfslib.c:177
uint64_t num_devices
Definition: btrfs.h:230
uint32_t st_nlink
Definition: btrfs.h:285
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat token
Definition: glfuncs.h:210
FORCEINLINE VOID InsertTailList(PLIST_ENTRY ListHead, PLIST_ENTRY Entry)
Definition: btrfslib.c:122
#define BTRFS_INCOMPAT_FLAGS_MIXED_GROUPS
Definition: btrfs.h:110
void blake2b(void *out, size_t outlen, const void *in, size_t inlen)
Definition: blake2b-ref.c:237
HMODULE module
Definition: btrfslib.c:174
WCHAR * string
Definition: btrfslib.c:96
#define TYPE_DEV_EXTENT
Definition: btrfs.h:42
#define DeviceDsmAction_Trim
Definition: ntddstor.h:211
char name[1]
Definition: btrfs.h:365
KEY key
Definition: btrfs.h:266
uint32_t num_references
Definition: btrfs.h:308
uint32_t offset
Definition: btrfs.h:160
char name[1]
Definition: btrfs.h:271
#define BLOCK_FLAG_SYSTEM
Definition: shellext.h:76
#define BTRFS_TYPE_DIRECTORY
Definition: shellext.h:86
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define offsetof(TYPE, MEMBER)
#define TYPE_EXTENT_ITEM
Definition: btrfs.h:31
struct btrfs_disk_key key
Definition: btrfs.h:110
#define TYPE_INODE_REF
Definition: btrfs.h:20
#define FORCEINLINE
Definition: ntbasedef.h:221
DEV_ITEM dev_item
Definition: btrfs.h:244
uint8_t type
Definition: btrfs.h:270
uint32_t size
Definition: btrfs.h:161
Definition: bufpool.h:45
GLuint index
Definition: glext.h:6031
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToAnsiString(PANSI_STRING DestinationString, PUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
_Must_inspect_result_ __drv_aliasesMem _In_ PDEVICE_OBJECT _In_opt_ PVOID _In_ ULONG _Out_opt_ PVOID OutputBuffer
Definition: iofuncs.h:713
uint16_t n
Definition: btrfs.h:364
tree_header header
Definition: btrfslib.c:154
BOOL QuickFormat
Definition: format.c:66
bool have_sse42
Definition: recv.cpp:44
BTRFS_TIME st_mtime
Definition: btrfs.h:295
uint64_t offset
Definition: btrfs.h:401
int options
Definition: main.c:106
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
NTSTATUS NTAPI BtrfsChkdskEx(IN PUNICODE_STRING DriveRoot, IN BOOLEAN FixErrors, IN BOOLEAN Verbose, IN BOOLEAN CheckOnlyIfDirty, IN BOOLEAN ScanDrive, IN PFMIFSCALLBACK Callback)
#define IOCTL_DISK_GET_LENGTH_INFO
Definition: imports.h:192
static uint64_t get_next_address(btrfs_chunk *c)
Definition: btrfslib.c:487
#define keycmp(key1, key2)
Definition: btrfslib.c:165
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
_In_ uint64_t _In_ uint64_t _In_ uint64_t _In_opt_ traverse_ptr * tp
Definition: btrfs.c:2930
uint64_t sb_phys_addr
Definition: btrfs.h:219
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
r l[0]
Definition: byte_order.h:167
#define BTRFS_ROOT_ROOT
Definition: btrfs.h:50
BTRFS_UUID device_uuid
Definition: btrfs.h:183
void calc_sha256(uint8_t *hash, const void *input, size_t len)
Definition: sha256.c:126
#define BTRFS_INCOMPAT_FLAGS_MIXED_BACKREF
Definition: btrfs.h:108
uint64_t flags
Definition: btrfs.h:220
USHORT MaximumLength
Definition: env_spec_w32.h:377
struct _LIST_ENTRY * Flink
Definition: typedefs.h:120
NTSYSAPI NTSTATUS NTAPI NtOpenFile(OUT PHANDLE phFile, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN ULONG ShareMode, IN ULONG OpenMode)
Definition: file.c:3951
uint64_t st_size
Definition: btrfs.h:282
static void init_device(btrfs_dev *dev, uint64_t id, uint64_t size, BTRFS_UUID *fsuuid, uint32_t sector_size)
Definition: btrfslib.c:726
GLsizeiptr size
Definition: glext.h:5919
uint64_t objid
Definition: btrfs.h:302
static void calc_superblock_checksum(superblock *sb)
Definition: btrfslib.c:752
#define TOKEN_QUERY
Definition: setypes.h:874
r parent
Definition: btrfs.c:2944
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define BTRFS_ROOT_TREEDIR
Definition: btrfs.h:55
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR Name[1]
Definition: imports.h:144
FORCEINLINE VOID InitializeListHead(PLIST_ENTRY ListHead)
Definition: btrfslib.c:118
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, void *lpReserved)
Definition: btrfslib.c:1673
#define IOCTL_STORAGE_MANAGE_DATA_SET_ATTRIBUTES
Definition: ntddstor.h:170
struct Command Command
uint64_t ULONGLONG
Definition: typedefs.h:66
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1138
uint64_t root_dir_objectid
Definition: btrfs.h:229
ULONG DataTransferLength
Definition: ntddscsi.h:136
DWORD ClusterSize
Definition: format.c:67
#define WINAPI
Definition: msvc.h:6
void * unk1
Definition: btrfslib.c:95
const GLubyte * c
Definition: glext.h:8905
static void do_full_trim(HANDLE h)
Definition: btrfslib.c:1405
KEY firstitem
Definition: btrfs.h:386
static FILE * out
Definition: regtests2xml.c:44
#define for
Definition: utility.h:88
uint64_t objid
Definition: btrfs.h:458
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint address
Definition: glext.h:9393
static bool is_ssd(HANDLE h)
Definition: btrfslib.c:1030
#define success(from, fromstr, to, tostr)
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
#define __stdcall
Definition: typedefs.h:25
Definition: cookie.c:201
NTSTATUS WINAPI ChkdskEx(PUNICODE_STRING DriveRoot, BOOLEAN FixErrors, BOOLEAN Verbose, BOOLEAN CheckOnlyIfDirty, BOOLEAN ScanDrive, PFMIFSCALLBACK Callback)
Definition: btrfslib.c:236
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define CSUM_TYPE_XXHASH
Definition: btrfs.h:126
BTRFS_UUID dev_uuid
Definition: btrfs.h:342
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
TREE_BLOCK_REF tbr
Definition: btrfslib.c:505
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
GLbitfield flags
Definition: glext.h:7161
_In_opt_ HANDLE _In_opt_ PIO_APC_ROUTINE ApcRoutine
Definition: iofuncs.h:719
uint64_t used
Definition: btrfslib.c:148
uint64_t generation
Definition: btrfs.h:222
#define FILE_READ_ATTRIBUTES
Definition: nt_native.h:647
FMIFS_MEDIA_FLAG
Definition: btrfslib.c:186
ULONG def_node_size
Definition: btrfslib.c:175
BTRFS_TIME st_atime
Definition: btrfs.h:293
#define IOCTL_BTRFS_PROBE_VOLUME
Definition: btrfsioctl.h:28
#define SharedUserData
EXTENT_ITEM ei
Definition: btrfslib.c:503
int ret
static double zero
Definition: j0_y0.c:96
GLenum const GLvoid * addr
Definition: glext.h:9621
tree_header header
Definition: btrfs_drv.h:442
__u8 attr
Definition: mkdosfs.c:359
static const WCHAR L[]
Definition: oid.c:1250
#define BTRFS_INCOMPAT_FLAGS_BIG_METADATA
Definition: btrfs.h:113
#define BLOCK_FLAG_METADATA
Definition: shellext.h:77
#define BTRFS_ROOT_FSTREE
Definition: btrfs.h:54
Definition: arc.h:85
void * data
Definition: btrfslib.c:140
Definition: btrfslib.c:225
HKEY key
Definition: reg.c:42
uint64_t id
Definition: btrfslib.c:153
#define TYPE_INODE_ITEM
Definition: btrfs.h:19
uint8_t level
Definition: btrfs.h:387
ULONG LowPart
Definition: typedefs.h:105
#define TYPE_BLOCK_GROUP_ITEM
Definition: btrfs.h:38
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
crc_func calc_crc32c
Definition: crc32c.c:23
static bool superblock_collision(btrfs_chunk *c, uint64_t address)
Definition: btrfslib.c:466
#define BLAKE2_HASH_SIZE
Definition: btrfslib.c:65
uint64_t cache_generation
Definition: btrfs.h:246
static void add_item(btrfs_root *r, uint64_t obj_id, uint8_t obj_type, uint64_t offset, void *data, uint16_t size)
Definition: btrfslib.c:331
Definition: typedefs.h:118
uint32_t n
Definition: btrfs.h:235
static void init_fs_tree(btrfs_root *r, uint32_t node_size)
Definition: btrfslib.c:963
#define SYNCHRONIZE
Definition: nt_native.h:61
ULONG def_sector_size
Definition: btrfslib.c:175
uint64_t generation
Definition: btrfs.h:280
BYTE uint8_t
Definition: msvideo1.c:66
uint8_t label[11]
Definition: fsck.fat.h:65
LARGE_INTEGER Length
Definition: winioctl.h:423
_Must_inspect_result_ _In_ PFILE_OBJECT _In_ ULONG _In_ ULONG InputBufferLength
Definition: fltkernel.h:1372
uint64_t st_blocks
Definition: btrfs.h:283
static __inline void win_time_to_unix(LARGE_INTEGER t, BTRFS_TIME *out)
Definition: btrfslib.c:916
uint64_t generation
Definition: btrfs.h:381
__u8 sector_size[2]
Definition: mkdosfs.c:361
XXH_PUBLIC_API unsigned long long XXH64(const void *input, size_t len, unsigned long long seed)
Definition: xxhash.c:555
BOOL WINAPI AdjustTokenPrivileges(HANDLE TokenHandle, BOOL DisableAllPrivileges, PTOKEN_PRIVILEGES NewState, DWORD BufferLength, PTOKEN_PRIVILEGES PreviousState, PDWORD ReturnLength)
Definition: security.c:376
static void calc_tree_checksum(tree_header *th, uint32_t node_size)
Definition: btrfslib.c:608
static void add_dir_item(btrfs_root *root, uint64_t inode, uint32_t hash, uint64_t key_objid, uint8_t key_type, uint64_t key_offset, uint64_t transid, uint8_t type, const char *name)
Definition: btrfslib.c:1077
struct _ATA_PASS_THROUGH_EX ATA_PASS_THROUGH_EX
Status
Definition: gdiplustypes.h:24
#define CSUM_TYPE_SHA256
Definition: btrfs.h:127
#define FILE_GENERIC_WRITE
Definition: nt_native.h:660
HANDLE HMODULE
Definition: typedefs.h:76
static void free_roots(LIST_ENTRY *roots)
Definition: btrfslib.c:274
Definition: btrfs.h:136
#define FSCTL_DISMOUNT_VOLUME
Definition: btrfslib.c:71
uint64_t offset
Definition: btrfslib.c:145
LIST_ENTRY list_entry
Definition: btrfslib.c:149
INODE_ITEM inode
Definition: btrfs.h:300
uint32_t flags
Definition: btrfslib.c:113
#define MAX_LABEL_SIZE
Definition: btrfs.h:16
PPC_QUAL void __cpuid(int CPUInfo[], const int InfoType)
Definition: intrin_ppc.h:682
UINT64 uint64_t
Definition: types.h:77
BTRFS_UUID fs_uuid
Definition: btrfs.h:148
static ATOM item
Definition: dde.c:856
GLenum GLenum GLenum input
Definition: glext.h:9031
#define BTRFS_ROOT_CHECKSUM
Definition: btrfs.h:56
#define STATUS_BUFFER_OVERFLOW
Definition: shellext.h:66
GLuint in
Definition: glext.h:9616
#define KEY
Definition: profile.c:30
#define EXTENT_ITEM_TREE_BLOCK
Definition: btrfs.h:376
unsigned short USHORT
Definition: pedump.c:61
uint64_t total_bytes
Definition: btrfs.h:227
KEY key
Definition: btrfs.h:159
uint64_t index
Definition: btrfs.h:363
UCHAR CurrentTaskFile[8]
Definition: ntddscsi.h:141
BTRFS_UUID uuid
Definition: btrfs.h:218
#define TYPE_ROOT_ITEM
Definition: btrfs.h:28
BOOLEAN(NTAPI * PFMIFSCALLBACK)(CALLBACKCOMMAND Command, ULONG SubAction, PVOID ActionInfo)
Definition: btrfslib.c:233
uint32_t node_size
Definition: btrfs.h:232
NTSYSCALLAPI NTSTATUS NTAPI NtFsControlFile(HANDLE FileHandle, HANDLE Event, PIO_APC_ROUTINE ApcRoutine, PVOID ApcContext, PIO_STATUS_BLOCK IoStatusBlock, ULONG FsControlCode, PVOID InputBuffer, ULONG InputBufferLength, PVOID OutputBuffer, ULONG OutputBufferLength)
uint64_t chunk_tree
Definition: btrfs.h:413
#define TYPE_DIR_ITEM
Definition: btrfs.h:24
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
unsigned int * PULONG
Definition: retypes.h:1
#define min(a, b)
Definition: monoChain.cc:55
Definition: list.h:27
static btrfs_chunk * add_chunk(LIST_ENTRY *chunks, uint64_t flags, btrfs_root *chunk_root, btrfs_dev *dev, btrfs_root *dev_root, BTRFS_UUID *chunkuuid, uint32_t sector_size)
Definition: btrfslib.c:392
BOOL WINAPI OpenProcessToken(HANDLE ProcessHandle, DWORD DesiredAccess, PHANDLE TokenHandle)
Definition: security.c:296
USHORT NameLength
Definition: imports.h:143
#define IOCTL_ATA_PASS_THROUGH
Definition: ntddscsi.h:40
uint64_t dev_id
#define DEVICE_DSM_FLAG_ENTIRE_DATA_SET_RANGE
Definition: ntddstor.h:214
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
static NTSTATUS write_data(HANDLE h, uint64_t address, btrfs_chunk *c, void *data, ULONG size)
Definition: btrfslib.c:588
UINT32 uint32_t
Definition: types.h:75
uint16_t unk2
Definition: btrfslib.c:112
LIST_ENTRY items
Definition: btrfslib.c:156
BOOL FixErrors
Definition: chkdsk.c:69
_Must_inspect_result_ _In_ PFILE_OBJECT _In_ ULONG FsControlCode
Definition: fltkernel.h:1369
#define BLOCK_FLAG_DATA
Definition: shellext.h:75
uint64_t def_incompat_flags
Definition: btrfslib.c:176
#define BTRFS_ROOT_DEVTREE
Definition: btrfs.h:53
Definition: name.c:38
static const uint64_t superblock_addrs[]
Definition: btrfs.h:13
#define BOOLEAN
Definition: pedump.c:73
#define OUT
Definition: typedefs.h:40
LIST_ENTRY list_entry
Definition: btrfslib.c:157
BOOL __stdcall GetFilesystemInformation(uint32_t unk1, uint32_t unk2, void *unk3)
Definition: btrfslib.c:1666
CHUNK_ITEM * chunk_item
Definition: btrfslib.c:146
#define FSCTL_LOCK_VOLUME
Definition: btrfslib.c:69
#define c
Definition: ke_i.h:80
static NTSTATUS clear_first_megabyte(HANDLE h)
Definition: btrfslib.c:1004
Definition: msctf.idl:510
unsigned int ULONG
Definition: retypes.h:1
GLenum GLuint id
Definition: glext.h:5579
#define TextOut
Definition: wingdi.h:4482
static void free_chunks(LIST_ENTRY *chunks)
Definition: btrfslib.c:311
PWCHAR Label
Definition: format.c:70
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
#define BTRFS_ROOT_CHUNK
Definition: btrfs.h:52
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define HEADER_FLAG_WRITTEN
Definition: btrfs.h:142
#define malloc
Definition: debug_ros.c:4
#define BLOCK_FLAG_DUPLICATE
Definition: shellext.h:80
uint64_t transid
Definition: btrfs.h:267
uint64_t flags
Definition: btrfs.h:382
#define BTRFS_ROOT_EXTENT
Definition: btrfs.h:51
uint32_t sector_size
Definition: btrfs.h:231
uint64_t chunktree
Definition: btrfs.h:457
BTRFS_UUID chunktree_uuid
Definition: btrfs.h:461
uint32_t leaf_size
Definition: btrfs.h:233
#define uint32_t
Definition: nsiface.idl:61
uint64_t chunk_root_generation
Definition: btrfs.h:236
static void get_uuid(BTRFS_UUID *uuid)
Definition: btrfslib.c:707
uint16_t n
Definition: btrfs.h:269
return STATUS_SUCCESS
Definition: btrfs.c:3014
Definition: _hash_fun.h:40
#define memset(x, y, z)
Definition: compat.h:39
#define TOKEN_ADJUST_PRIVILEGES
Definition: setypes.h:876
LPFNPSPCALLBACK Callback
Definition: desk.c:112
static void assign_addresses(LIST_ENTRY *roots, btrfs_chunk *sys_chunk, btrfs_chunk *metadata_chunk, uint32_t node_size, btrfs_root *root_root, btrfs_root *extent_root, bool skinny)
Definition: btrfslib.c:515
void __stdcall SetSizes(ULONG sector, ULONG node)
Definition: btrfslib.c:1650
CALLBACKCOMMAND
Definition: btrfslib.c:213
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
base of all file and directory entries
Definition: entries.h:82
btrfs_filesystem_device device
Definition: btrfsioctl.h:191
#define IOCTL_DISK_GET_DRIVE_GEOMETRY
Definition: cdrw_usr.h:169
#define APIENTRY
Definition: api.h:79
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
uint64_t address
Definition: btrfs.h:459
#define SHA256_HASH_SIZE
Definition: btrfslib.c:62
_In_ uint16_t _Out_ ULONG * atts
Definition: btrfs_drv.h:1109
LONGLONG QuadPart
Definition: typedefs.h:113
IN PDCB IN PCCB IN VBO IN OUT PULONG OUT PDIRENT OUT PBCB OUT PVBO ByteOffset
Definition: fatprocs.h:725
HMODULE hModule
Definition: animate.c:44
Definition: path.c:41
FMIFS_MEDIA_FLAG
Definition: fmifs.h:37
#define BTRFS_MAGIC
Definition: btrfs.h:42
Definition: dlist.c:348
uint32_t st_mode
Definition: btrfs.h:288
off
Definition: i386-dis.c:3909
uint64_t incompat_flags
Definition: btrfs.h:239