[BTRFS]
authorPierre Schweitzer <pierre@reactos.org>
Sat, 29 Oct 2016 17:05:10 +0000 (17:05 +0000)
committerPierre Schweitzer <pierre@reactos.org>
Sat, 29 Oct 2016 17:05:10 +0000 (17:05 +0000)
Sync btrfs to 0.7.

CORE-12223

svn path=/trunk/; revision=73062

25 files changed:
reactos/drivers/filesystems/btrfs/CMakeLists.txt
reactos/drivers/filesystems/btrfs/btrfs.c
reactos/drivers/filesystems/btrfs/btrfs.h
reactos/drivers/filesystems/btrfs/btrfs.rc
reactos/drivers/filesystems/btrfs/btrfs_drv.h
reactos/drivers/filesystems/btrfs/compress.c
reactos/drivers/filesystems/btrfs/crc32c.c
reactos/drivers/filesystems/btrfs/create.c
reactos/drivers/filesystems/btrfs/devctrl.c [new file with mode: 0644]
reactos/drivers/filesystems/btrfs/dirctrl.c
reactos/drivers/filesystems/btrfs/extent-tree.c
reactos/drivers/filesystems/btrfs/fileinfo.c
reactos/drivers/filesystems/btrfs/flushthread.c
reactos/drivers/filesystems/btrfs/free-space.c
reactos/drivers/filesystems/btrfs/fsctl.c
reactos/drivers/filesystems/btrfs/galois.c [new file with mode: 0644]
reactos/drivers/filesystems/btrfs/pnp.c
reactos/drivers/filesystems/btrfs/read.c
reactos/drivers/filesystems/btrfs/registry.c
reactos/drivers/filesystems/btrfs/reparse.c
reactos/drivers/filesystems/btrfs/search.c
reactos/drivers/filesystems/btrfs/security.c
reactos/drivers/filesystems/btrfs/treefuncs.c
reactos/drivers/filesystems/btrfs/worker-thread.c
reactos/drivers/filesystems/btrfs/write.c

index 730aa1f..118429e 100644 (file)
@@ -9,6 +9,7 @@ list(APPEND SOURCE
     compress.c
     crc32c.c
     create.c
+    devctrl.c
     dirctrl.c
     extent-tree.c
     fastio.c
@@ -16,6 +17,7 @@ list(APPEND SOURCE
     flushthread.c
     free-space.c
     fsctl.c
+    galois.c
     pnp.c
     read.c
     registry.c
index 2426362..6611438 100644 (file)
 #include <intrin.h>
 #endif
 #endif
+#include <ntddscsi.h>
 #include "btrfs.h"
 #ifndef __REACTOS__
 #include <winioctl.h>
 #else
 #include <rtlfuncs.h>
 #endif
-#include <mountdev.h>
+#include <ata.h>
 
 #define INCOMPAT_SUPPORTED (BTRFS_INCOMPAT_FLAGS_MIXED_BACKREF | BTRFS_INCOMPAT_FLAGS_DEFAULT_SUBVOL | BTRFS_INCOMPAT_FLAGS_MIXED_GROUPS | \
-                            BTRFS_INCOMPAT_FLAGS_COMPRESS_LZO | BTRFS_INCOMPAT_FLAGS_BIG_METADATA | BTRFS_INCOMPAT_FLAGS_EXTENDED_IREF | \
-                            BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA | BTRFS_INCOMPAT_FLAGS_NO_HOLES)
+                            BTRFS_INCOMPAT_FLAGS_COMPRESS_LZO | BTRFS_INCOMPAT_FLAGS_BIG_METADATA | BTRFS_INCOMPAT_FLAGS_RAID56 | \
+                            BTRFS_INCOMPAT_FLAGS_EXTENDED_IREF | BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA | BTRFS_INCOMPAT_FLAGS_NO_HOLES)
 #define COMPAT_RO_SUPPORTED 0
 
 static WCHAR device_name[] = {'\\','B','t','r','f','s',0};
@@ -46,7 +47,7 @@ static WCHAR dosdevice_name[] = {'\\','D','o','s','D','e','v','i','c','e','s','\
 PDRIVER_OBJECT drvobj;
 PDEVICE_OBJECT devobj;
 #ifndef __REACTOS__
-BOOL have_sse42 = FALSE;
+BOOL have_sse42 = FALSE, have_sse2 = FALSE;
 #endif
 UINT64 num_reads = 0;
 LIST_ENTRY uid_map_list;
@@ -60,6 +61,8 @@ UINT32 mount_compress_type = 0;
 UINT32 mount_zlib_level = 3;
 UINT32 mount_flush_interval = 30;
 UINT32 mount_max_inline = 2048;
+UINT32 mount_raid5_recalculation = 1;
+UINT32 mount_raid6_recalculation = 1;
 BOOL log_started = FALSE;
 UNICODE_STRING log_device, log_file, registry_path;
 
@@ -243,28 +246,6 @@ UINT64 sector_align( UINT64 NumberToBeAligned, UINT64 Alignment )
     return NumberToBeAligned;
 }
 
-int keycmp(const KEY* key1, const KEY* key2) {
-    if (key1->obj_id < key2->obj_id) {
-        return -1;
-    } else if (key1->obj_id > key2->obj_id) {
-        return 1;
-    }
-    
-    if (key1->obj_type < key2->obj_type) {
-        return -1;
-    } else if (key1->obj_type > key2->obj_type) {
-        return 1;
-    }
-    
-    if (key1->offset < key2->offset) {
-        return -1;
-    } else if (key1->offset > key2->offset) {
-        return 1;
-    }
-    
-    return 0;
-}
-
 BOOL is_top_level(PIRP Irp) {
     if (!IoGetTopLevelIrp()) {
         IoSetTopLevelIrp(Irp);
@@ -320,7 +301,7 @@ static void STDCALL DriverUnload(PDRIVER_OBJECT DriverObject) {
         ExFreePool(registry_path.Buffer);
 }
 
-BOOL STDCALL get_last_inode(device_extension* Vcb, root* r, PIRP Irp) {
+static BOOL STDCALL get_last_inode(device_extension* Vcb, root* r, PIRP Irp) {
     KEY searchkey;
     traverse_ptr tp, prev_tp;
     NTSTATUS Status;
@@ -336,6 +317,12 @@ BOOL STDCALL get_last_inode(device_extension* Vcb, root* r, PIRP Irp) {
         return FALSE;
     }
     
+    if (tp.item->key.obj_type == TYPE_INODE_ITEM || (tp.item->key.obj_type == TYPE_ROOT_ITEM && !(tp.item->key.obj_id & 0x8000000000000000))) {
+        r->lastinode = tp.item->key.obj_id;
+        TRACE("last inode for tree %llx is %llx\n", r->id, r->lastinode);
+        return TRUE;
+    }
+    
     while (find_prev_item(Vcb, &tp, &prev_tp, FALSE, Irp)) {
         tp = prev_tp;
         
@@ -374,7 +361,7 @@ BOOL STDCALL get_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char*
         return FALSE;
     }
     
-    if (keycmp(&tp.item->key, &searchkey)) {
+    if (keycmp(tp.item->key, searchkey)) {
         TRACE("could not find item (%llx,%x,%llx)\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
         return FALSE;
     }
@@ -426,53 +413,6 @@ BOOL STDCALL get_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char*
     return FALSE;
 }
 
-NTSTATUS add_dir_item(device_extension* Vcb, root* subvol, UINT64 inode, UINT32 crc32, DIR_ITEM* di, ULONG disize, PIRP Irp, LIST_ENTRY* rollback) {
-    KEY searchkey;
-    traverse_ptr tp;
-    UINT8* di2;
-    NTSTATUS Status;
-    
-    searchkey.obj_id = inode;
-    searchkey.obj_type = TYPE_DIR_ITEM;
-    searchkey.offset = crc32;
-    
-    Status = find_item(Vcb, subvol, &tp, &searchkey, FALSE, Irp);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        return Status;
-    }
-    
-    if (!keycmp(&tp.item->key, &searchkey)) {
-        ULONG maxlen = Vcb->superblock.node_size - sizeof(tree_header) - sizeof(leaf_node);
-        
-        if (tp.item->size + disize > maxlen) {
-            WARN("DIR_ITEM was longer than maxlen (%u + %u > %u)\n", tp.item->size, disize, maxlen);
-            return STATUS_INTERNAL_ERROR;
-        }
-        
-        di2 = ExAllocatePoolWithTag(PagedPool, tp.item->size + disize, ALLOC_TAG);
-        if (!di2) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
-        
-        if (tp.item->size > 0)
-            RtlCopyMemory(di2, tp.item->data, tp.item->size);
-        
-        RtlCopyMemory(di2 + tp.item->size, di, disize);
-        
-        delete_tree_item(Vcb, &tp, rollback);
-        
-        insert_tree_item(Vcb, subvol, inode, TYPE_DIR_ITEM, crc32, di2, tp.item->size + disize, NULL, Irp, rollback);
-        
-        ExFreePool(di);
-    } else {
-        insert_tree_item(Vcb, subvol, inode, TYPE_DIR_ITEM, crc32, di, disize, NULL, Irp, rollback);
-    }
-    
-    return STATUS_SUCCESS;
-}
-
 static NTSTATUS STDCALL drv_close(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
     NTSTATUS Status;
     PIO_STACK_LOCATION IrpSp;
@@ -514,73 +454,6 @@ exit:
     return Status;
 }
 
-static NTSTATUS STDCALL drv_query_ea(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
-    NTSTATUS Status;
-    BOOL top_level;
-    device_extension* Vcb = DeviceObject->DeviceExtension;
-
-    FsRtlEnterFileSystem();
-
-    top_level = is_top_level(Irp);
-    
-    if (Vcb && Vcb->type == VCB_TYPE_PARTITION0) {
-        Status = part0_passthrough(DeviceObject, Irp);
-        goto exit;
-    }
-    
-    FIXME("STUB: query ea\n");
-    Status = STATUS_NOT_IMPLEMENTED;
-    
-    Irp->IoStatus.Status = Status;
-    Irp->IoStatus.Information = 0;
-
-    IoCompleteRequest( Irp, IO_NO_INCREMENT );
-
-exit:
-    if (top_level) 
-        IoSetTopLevelIrp(NULL);
-    
-    FsRtlExitFileSystem();
-
-    return Status;
-}
-
-static NTSTATUS STDCALL drv_set_ea(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
-    NTSTATUS Status;
-    device_extension* Vcb = DeviceObject->DeviceExtension;
-    BOOL top_level;
-
-    FsRtlEnterFileSystem();
-
-    top_level = is_top_level(Irp);
-    
-    if (Vcb && Vcb->type == VCB_TYPE_PARTITION0) {
-        Status = part0_passthrough(DeviceObject, Irp);
-        goto exit;
-    }
-    
-    FIXME("STUB: set ea\n");
-    Status = STATUS_NOT_IMPLEMENTED;
-    
-    if (Vcb->readonly)
-        Status = STATUS_MEDIA_WRITE_PROTECTED;
-    
-    // FIXME - return STATUS_ACCESS_DENIED if subvol readonly
-    
-    Irp->IoStatus.Status = Status;
-    Irp->IoStatus.Information = 0;
-
-    IoCompleteRequest( Irp, IO_NO_INCREMENT );
-    
-exit:
-    if (top_level) 
-        IoSetTopLevelIrp(NULL);
-    
-    FsRtlExitFileSystem();
-
-    return Status;
-}
-
 static NTSTATUS STDCALL drv_flush_buffers(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
     NTSTATUS Status;
     PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation( Irp );
@@ -627,15 +500,27 @@ exit:
 }
 
 static void calculate_total_space(device_extension* Vcb, LONGLONG* totalsize, LONGLONG* freespace) {
-    UINT8 factor;
+    UINT16 nfactor, dfactor;
+    UINT64 sectors_used;
+    
+    if (Vcb->data_flags & BLOCK_FLAG_DUPLICATE || Vcb->data_flags & BLOCK_FLAG_RAID1 || Vcb->data_flags & BLOCK_FLAG_RAID10) {
+        nfactor = 1;
+        dfactor = 2;
+    } else if (Vcb->data_flags & BLOCK_FLAG_RAID5) {
+        nfactor = Vcb->superblock.num_devices - 1;
+        dfactor = Vcb->superblock.num_devices;
+    } else if (Vcb->data_flags & BLOCK_FLAG_RAID6) {
+        nfactor = Vcb->superblock.num_devices - 2;
+        dfactor = Vcb->superblock.num_devices;
+    } else {
+        nfactor = 1;
+        dfactor = 1;
+    }
     
-    if (Vcb->data_flags & BLOCK_FLAG_DUPLICATE || Vcb->data_flags & BLOCK_FLAG_RAID1 || Vcb->data_flags & BLOCK_FLAG_RAID10)
-        factor = 2;
-    else
-        factor = 1;
+    sectors_used = Vcb->superblock.bytes_used / Vcb->superblock.sector_size;
     
-    *totalsize = (Vcb->superblock.total_bytes / Vcb->superblock.sector_size) factor;
-    *freespace = ((Vcb->superblock.total_bytes - Vcb->superblock.bytes_used) / Vcb->superblock.sector_size) / factor;
+    *totalsize = (Vcb->superblock.total_bytes / Vcb->superblock.sector_size) * nfactor / dfactor;
+    *freespace = sectors_used > *totalsize ? 0 : (*totalsize - sectors_used);
 }
 
 static NTSTATUS STDCALL drv_query_volume_information(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
@@ -692,7 +577,8 @@ static NTSTATUS STDCALL drv_query_volume_information(IN PDEVICE_OBJECT DeviceObj
             
             data->FileSystemAttributes = FILE_CASE_PRESERVED_NAMES | FILE_CASE_SENSITIVE_SEARCH |
                                          FILE_UNICODE_ON_DISK | FILE_NAMED_STREAMS | FILE_SUPPORTS_HARD_LINKS | FILE_PERSISTENT_ACLS |
-                                         FILE_SUPPORTS_REPARSE_POINTS | FILE_SUPPORTS_SPARSE_FILES | FILE_SUPPORTS_OBJECT_IDS;
+                                         FILE_SUPPORTS_REPARSE_POINTS | FILE_SUPPORTS_SPARSE_FILES | FILE_SUPPORTS_OBJECT_IDS |
+                                         FILE_SUPPORTS_OPEN_BY_FILE_ID | FILE_SUPPORTS_EXTENDED_ATTRIBUTES;
             if (Vcb->readonly)
                 data->FileSystemAttributes |= FILE_READ_ONLY_VOLUME;
                                          
@@ -830,6 +716,29 @@ static NTSTATUS STDCALL drv_query_volume_information(IN PDEVICE_OBJECT DeviceObj
             Status = overflow ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
             break;
         }
+        
+#ifdef _MSC_VER // not in mingw yet
+        case FileFsSectorSizeInformation:
+        {
+            FILE_FS_SECTOR_SIZE_INFORMATION* data = Irp->AssociatedIrp.SystemBuffer;
+            
+            data->LogicalBytesPerSector = Vcb->superblock.sector_size;
+            data->PhysicalBytesPerSectorForAtomicity = Vcb->superblock.sector_size;
+            data->PhysicalBytesPerSectorForPerformance = Vcb->superblock.sector_size;
+            data->FileSystemEffectivePhysicalBytesPerSectorForAtomicity = Vcb->superblock.sector_size;
+            data->ByteOffsetForSectorAlignment = 0;
+            data->ByteOffsetForPartitionAlignment = 0;
+            
+            data->Flags = SSINFO_FLAGS_ALIGNED_DEVICE | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE;
+            
+            if (Vcb->trim)
+                data->Flags |= SSINFO_FLAGS_TRIM_ENABLED;
+            
+            BytesCopied = sizeof(FILE_FS_SECTOR_SIZE_INFORMATION);
+  
+            break;
+        }
+#endif
 
         default:
             Status = STATUS_INVALID_PARAMETER;
@@ -902,7 +811,7 @@ static NTSTATUS STDCALL read_completion(PDEVICE_OBJECT DeviceObject, PIRP Irp, P
 //         return;
 //     }
 //     
-//     while (TRUE/*keycmp(&tp.item->key, &endkey) < 1*/) {
+//     while (TRUE/*keycmp(tp.item->key, endkey) < 1*/) {
 //         tp.item->ignore = TRUE;
 //         add_to_tree_cache(tc, tp.tree);
 //         
@@ -1035,6 +944,7 @@ NTSTATUS create_root(device_extension* Vcb, UINT64 id, root** rootptr, BOOL no_t
     
         t->new_address = 0;
         t->has_new_address = FALSE;
+        t->updated_extents = FALSE;
         t->flags = tp.tree->flags;
         
         InsertTailList(&Vcb->trees, &t->list_entry);
@@ -1179,10 +1089,103 @@ NTSTATUS create_root(device_extension* Vcb, UINT64 id, root** rootptr, BOOL no_t
 //     int3;
 // }
 
+#if 0
+void STDCALL tree_test(void* context) {
+    device_extension* Vcb = context;
+    NTSTATUS Status;
+    UINT64 id;
+    LARGE_INTEGER due_time, time;
+    KTIMER timer;
+    root* r;
+    LIST_ENTRY rollback;
+    ULONG seed;
+    
+    InitializeListHead(&rollback);
+    
+    KeInitializeTimer(&timer);
+    
+    id = InterlockedIncrement64(&Vcb->root_root->lastinode);
+    Status = create_root(Vcb, id, &r, FALSE, 0, NULL, &rollback);
+    if (!NT_SUCCESS(Status)) {
+        ERR("create_root returned %08x\n");
+        return;
+    }
+    
+    clear_rollback(Vcb, &rollback);
+    
+    due_time.QuadPart = (UINT64)1 * -10000000;
+    
+    KeQueryPerformanceCounter(&time);
+    seed = time.LowPart;
+    
+    while (TRUE) {
+        UINT32 i;
+        
+        FsRtlEnterFileSystem();
+        
+        ExAcquireResourceExclusiveLite(&Vcb->tree_lock, TRUE);
+        
+        for (i = 0; i < 100; i++) {
+            void* data;
+            ULONG datalen;
+            UINT64 objid, offset;
+            
+            objid = RtlRandomEx(&seed);
+            objid <<= 32;
+            objid |= RtlRandomEx(&seed);
+            
+            offset = RtlRandomEx(&seed);
+            offset <<= 32;
+            offset |= RtlRandomEx(&seed);
+            
+            datalen = 30;
+            data = ExAllocatePoolWithTag(PagedPool, datalen, ALLOC_TAG);
+            
+            if (!insert_tree_item(Vcb, r, objid, 0xfd, offset, data, datalen, NULL, NULL, &rollback)) {
+                ERR("insert_tree_item failed\n");
+            }
+        }
+        
+        for (i = 0; i < 25; i++) {
+            KEY searchkey;
+            traverse_ptr tp;
+            
+            searchkey.obj_id = RtlRandomEx(&seed);
+            searchkey.obj_id <<= 32;
+            searchkey.obj_id |= RtlRandomEx(&seed);
+            
+            searchkey.obj_type = 0xfd;
+            
+            searchkey.offset = RtlRandomEx(&seed);
+            searchkey.offset <<= 32;
+            searchkey.offset |= RtlRandomEx(&seed);
+            
+            Status = find_item(Vcb, r, &tp, &searchkey, FALSE, NULL);
+            if (!NT_SUCCESS(Status)) {
+                ERR("error - find_item returned %08x\n", Status);
+            } else {
+                delete_tree_item(Vcb, &tp, &rollback);
+            }
+        }
+        
+        clear_rollback(Vcb, &rollback);
+        
+        ExReleaseResourceLite(&Vcb->tree_lock);
+        
+        FsRtlExitFileSystem();
+        
+        KeSetTimer(&timer, due_time, NULL);
+        
+        KeWaitForSingleObject(&timer, Executive, KernelMode, FALSE, NULL);
+    }
+}
+#endif
+
 static NTSTATUS STDCALL set_label(device_extension* Vcb, FILE_FS_LABEL_INFORMATION* ffli) {
     ULONG utf8len;
     NTSTATUS Status;
     USHORT vollen, i;
+//     HANDLE h;
     
     TRACE("label = %.*S\n", ffli->VolumeLabelLength / sizeof(WCHAR), ffli->VolumeLabel);
     
@@ -1232,6 +1235,8 @@ static NTSTATUS STDCALL set_label(device_extension* Vcb, FILE_FS_LABEL_INFORMATI
     
     Vcb->need_write = TRUE;
     
+//     PsCreateSystemThread(&h, 0, NULL, NULL, NULL, tree_test, Vcb);
+    
 release:  
     ExReleaseResourceLite(&Vcb->tree_lock);
 
@@ -1320,7 +1325,7 @@ NTSTATUS delete_dir_item(device_extension* Vcb, root* subvol, UINT64 parinode, U
         return Status;
     }
     
-    if (!keycmp(&searchkey, &tp.item->key)) {
+    if (!keycmp(searchkey, tp.item->key)) {
         if (tp.item->size < sizeof(DIR_ITEM)) {
             WARN("(%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(DIR_ITEM));
         } else {
@@ -1391,7 +1396,7 @@ NTSTATUS delete_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UIN
         return Status;
     }
     
-    if (!keycmp(&searchkey, &tp.item->key)) {
+    if (!keycmp(searchkey, tp.item->key)) {
         if (tp.item->size < sizeof(INODE_REF)) {
             WARN("(%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(INODE_REF));
         } else {
@@ -1476,7 +1481,7 @@ NTSTATUS delete_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UIN
         return Status;
     }
     
-    if (!keycmp(&searchkey, &tp.item->key)) {
+    if (!keycmp(searchkey, tp.item->key)) {
         if (tp.item->size < sizeof(INODE_EXTREF)) {
             WARN("(%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(INODE_EXTREF));
         } else {
@@ -1816,6 +1821,9 @@ void _free_fcb(fcb* fcb, const char* func, const char* file, unsigned int line)
     if (fcb->reparse_xattr.Buffer)
         ExFreePool(fcb->reparse_xattr.Buffer);
     
+    if (fcb->ea_xattr.Buffer)
+        ExFreePool(fcb->ea_xattr.Buffer);
+    
     if (fcb->adsdata.Buffer)
         ExFreePool(fcb->adsdata.Buffer);
     
@@ -1938,12 +1946,19 @@ static NTSTATUS STDCALL close_file(device_extension* Vcb, PFILE_OBJECT FileObjec
     fcb* fcb;
     ccb* ccb;
     file_ref* fileref = NULL;
+    LONG open_files;
     
     TRACE("FileObject = %p\n", FileObject);
     
+    open_files = InterlockedDecrement(&Vcb->open_files);
+    
     fcb = FileObject->FsContext;
     if (!fcb) {
         TRACE("FCB was NULL, returning success\n");
+        
+        if (open_files == 0 && Vcb->removing)
+            uninit(Vcb, FALSE);
+        
         return STATUS_SUCCESS;
     }
     
@@ -1968,6 +1983,11 @@ static NTSTATUS STDCALL close_file(device_extension* Vcb, PFILE_OBJECT FileObjec
     
     CcUninitializeCacheMap(FileObject, NULL, NULL);
     
+    if (open_files == 0 && Vcb->removing) {
+        uninit(Vcb, FALSE);
+        return STATUS_SUCCESS;
+    }
+    
     if (!(Vcb->Vpb->Flags & VPB_MOUNTED))
         return STATUS_SUCCESS;
     
@@ -2009,22 +2029,11 @@ void STDCALL uninit(device_extension* Vcb, BOOL flush) {
         
         free_trees(Vcb);
         
-        clear_rollback(&rollback);
+        clear_rollback(Vcb, &rollback);
 
         ExReleaseResourceLite(&Vcb->tree_lock);
     }
     
-    for (i = 0; i < Vcb->threads.num_threads; i++) {
-        Vcb->threads.threads[i].quit = TRUE;
-        KeSetEvent(&Vcb->threads.threads[i].event, 0, FALSE);
-        
-        KeWaitForSingleObject(&Vcb->threads.threads[i].finished, Executive, KernelMode, FALSE, NULL);
-        
-        ZwClose(Vcb->threads.threads[i].handle);
-    }
-    
-    ExFreePool(Vcb->threads.threads);
-    
     time.QuadPart = 0;
     KeSetTimer(&Vcb->flush_thread_timer, time, NULL); // trigger the timer early
     KeWaitForSingleObject(&Vcb->flush_thread_finished, Executive, KernelMode, FALSE, NULL);
@@ -2114,6 +2123,12 @@ void STDCALL uninit(device_extension* Vcb, BOOL flush) {
     ExDeleteResourceLite(&Vcb->checksum_lock);
     ExDeleteResourceLite(&Vcb->chunk_lock);
     
+    ExDeletePagedLookasideList(&Vcb->tree_data_lookaside);
+    ExDeletePagedLookasideList(&Vcb->traverse_ptr_lookaside);
+    ExDeletePagedLookasideList(&Vcb->rollback_item_lookaside);
+    ExDeletePagedLookasideList(&Vcb->batch_item_lookaside);
+    ExDeleteNPagedLookasideList(&Vcb->range_lock_lookaside);
+    
     ZwClose(Vcb->flush_thread_handle);
 }
 
@@ -2150,6 +2165,7 @@ NTSTATUS delete_fileref(file_ref* fileref, PFILE_OBJECT FileObject, PIRP Irp, LI
                 fileref->fcb->inode_item.transid = fileref->fcb->Vcb->superblock.generation;
                 fileref->fcb->inode_item.sequence++;
                 fileref->fcb->inode_item.st_ctime = now;
+                fileref->fcb->inode_item_changed = TRUE;
             } else {
                 fileref->fcb->deleted = TRUE;
             
@@ -2229,6 +2245,7 @@ NTSTATUS delete_fileref(file_ref* fileref, PFILE_OBJECT FileObject, PIRP Irp, LI
     fileref->parent->fcb->inode_item.st_mtime = now;
     ExReleaseResourceLite(fileref->parent->fcb->Header.Resource);
 
+    fileref->parent->fcb->inode_item_changed = TRUE;
     mark_fcb_dirty(fileref->parent->fcb);
     
     send_notification_fcb(fileref->parent, FILE_NOTIFY_CHANGE_LAST_WRITE, FILE_ACTION_MODIFIED);
@@ -2236,9 +2253,6 @@ NTSTATUS delete_fileref(file_ref* fileref, PFILE_OBJECT FileObject, PIRP Irp, LI
     fileref->fcb->subvol->root_item.ctransid = fileref->fcb->Vcb->superblock.generation;
     fileref->fcb->subvol->root_item.ctime = now;
     
-    if (FileObject && FileObject->Flags & FO_CACHE_SUPPORTED && fileref->fcb->nonpaged->segment_object.DataSectionObject)
-        CcPurgeCacheSection(&fileref->fcb->nonpaged->segment_object, NULL, 0, FALSE);
-    
     newlength.QuadPart = 0;
     
     if (FileObject && !CcUninitializeCacheMap(FileObject, &newlength, NULL))
@@ -2284,16 +2298,18 @@ static NTSTATUS STDCALL drv_cleanup(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
         fileref = ccb ? ccb->fileref : NULL;
         
         TRACE("cleanup called for FileObject %p\n", FileObject);
-        TRACE("fcb %p (%S), refcount = %u, open_count = %u\n", fcb, file_desc(FileObject), fcb->refcount, fcb->open_count);
+        TRACE("fileref %p (%S), refcount = %u, open_count = %u\n", fileref, file_desc(FileObject), fileref ? fileref->refcount : 0, fileref ? fileref->open_count : 0);
         
         IoRemoveShareAccess(FileObject, &fcb->share_access);
         
         FsRtlNotifyCleanup(Vcb->NotifySync, &Vcb->DirNotifyList, ccb);    
         
-        oc = InterlockedDecrement(&fcb->open_count);
+        if (fileref) {
+            oc = InterlockedDecrement(&fileref->open_count);
 #ifdef DEBUG_FCB_REFCOUNTS
-        ERR("fcb %p: open_count now %i\n", fcb, oc);
+            ERR("fileref %p: open_count now %i\n", fileref, oc);
 #endif
+        }
         
         if (ccb && ccb->options & FILE_DELETE_ON_CLOSE && fileref)
             fileref->delete_on_close = TRUE;
@@ -2307,7 +2323,7 @@ static NTSTATUS STDCALL drv_cleanup(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
             FsRtlNotifyVolumeEvent(FileObject, FSRTL_VOLUME_UNLOCK);
         }
         
-        if (oc == 0) {
+        if (fileref && oc == 0) {
             if (!Vcb->removing) {
                 LIST_ENTRY rollback;
         
@@ -2318,16 +2334,21 @@ static NTSTATUS STDCALL drv_cleanup(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
                     
                     ExAcquireResourceSharedLite(&fcb->Vcb->tree_lock, TRUE);
                     
+                    ExAcquireResourceExclusiveLite(&fcb->Vcb->fcb_lock, TRUE);
+                    
                     Status = delete_fileref(fileref, FileObject, Irp, &rollback);
                     if (!NT_SUCCESS(Status)) {
                         ERR("delete_fileref returned %08x\n", Status);
                         do_rollback(Vcb, &rollback);
+                        ExReleaseResourceLite(&fcb->Vcb->fcb_lock);
                         ExReleaseResourceLite(&fcb->Vcb->tree_lock);
                         goto exit;
                     }
                     
+                    ExReleaseResourceLite(&fcb->Vcb->fcb_lock);
+                    
                     ExReleaseResourceLite(&fcb->Vcb->tree_lock);
-                    clear_rollback(&rollback);
+                    clear_rollback(Vcb, &rollback);
                 } else if (FileObject->Flags & FO_CACHE_SUPPORTED && fcb->nonpaged->segment_object.DataSectionObject) {
                     IO_STATUS_BLOCK iosb;
                     CcFlushCache(FileObject->SectionObjectPointer, NULL, 0, &iosb);
@@ -2543,7 +2564,7 @@ static NTSTATUS STDCALL read_superblock(device_extension* Vcb, PDEVICE_OBJECT de
     NTSTATUS Status;
     superblock* sb;
     unsigned int i, to_read;
-    UINT32 crc32;
+    UINT8 valid_superblocks;
     
     to_read = sector_align(sizeof(superblock), device->SectorSize);
     
@@ -2554,8 +2575,11 @@ static NTSTATUS STDCALL read_superblock(device_extension* Vcb, PDEVICE_OBJECT de
     }
     
     i = 0;
+    valid_superblocks = 0;
     
     while (superblock_addrs[i] > 0) {
+        UINT32 crc32;
+        
         if (i > 0 && superblock_addrs[i] + sizeof(superblock) > length)
             break;
         
@@ -2566,27 +2590,28 @@ static NTSTATUS STDCALL read_superblock(device_extension* Vcb, PDEVICE_OBJECT de
             return Status;
         }
         
-        // FIXME - check checksum before accepting?
-        
         TRACE("got superblock %u!\n", i);
-
-        if (i == 0 || sb->generation > Vcb->superblock.generation)
+        
+        crc32 = ~calc_crc32c(0xffffffff, (UINT8*)&sb->uuid, (ULONG)sizeof(superblock) - sizeof(sb->checksum));
+        
+        if (crc32 != *((UINT32*)sb->checksum))
+            WARN("crc32 was %08x, expected %08x\n", crc32, *((UINT32*)sb->checksum));
+        else if (valid_superblocks == 0 || sb->generation > Vcb->superblock.generation) {
             RtlCopyMemory(&Vcb->superblock, sb, sizeof(superblock));
+            valid_superblocks++;
+        }
         
         i++;
     }
     
     ExFreePool(sb);
     
-    crc32 = calc_crc32c(0xffffffff, (UINT8*)&Vcb->superblock.uuid, (ULONG)sizeof(superblock) - sizeof(Vcb->superblock.checksum));
-    crc32 = ~crc32;
-    TRACE("crc32 was %08x, expected %08x\n", crc32, *((UINT32*)Vcb->superblock.checksum));
-    
-    if (crc32 != *((UINT32*)Vcb->superblock.checksum))
-        return STATUS_INTERNAL_ERROR; // FIXME - correct error?
+    if (valid_superblocks == 0) {
+        ERR("could not find any valid superblocks\n");
+        return STATUS_INTERNAL_ERROR;
+    }
     
     TRACE("label is %s\n", Vcb->superblock.label);
-//     utf8_to_utf16(Vcb->superblock.label, Vcb->label, MAX_LABEL_SIZE * sizeof(WCHAR));
     
     return STATUS_SUCCESS;
 }
@@ -2662,6 +2687,14 @@ static NTSTATUS STDCALL add_root(device_extension* Vcb, UINT64 id, UINT64 addr,
             RtlZeroMemory(((UINT8*)&r->root_item) + tp->item->size, sizeof(ROOT_ITEM) - tp->item->size);
     }
     
+    if (!Vcb->readonly && (r->id == BTRFS_ROOT_ROOT || r->id == BTRFS_ROOT_FSTREE || (r->id >= 0x100 && !(r->id & 0xf000000000000000)))) { // FS tree root
+        // FIXME - don't call this if subvol is readonly (though we will have to if we ever toggle this flag)
+        get_last_inode(Vcb, r, NULL);
+        
+        if (r->id == BTRFS_ROOT_ROOT && r->lastinode < 0x100)
+            r->lastinode = 0x100;
+    }
+    
     InsertTailList(&Vcb->roots, &r->list_entry);
     
     switch (r->id) {
@@ -2688,6 +2721,9 @@ static NTSTATUS STDCALL add_root(device_extension* Vcb, UINT64 id, UINT64 addr,
         case BTRFS_ROOT_UUID:
             Vcb->uuid_root = r;
             break;
+            
+        case BTRFS_ROOT_DATA_RELOC:
+            Vcb->data_reloc_root = r;
     }
     
     return STATUS_SUCCESS;
@@ -2734,6 +2770,79 @@ static NTSTATUS STDCALL look_for_roots(device_extension* Vcb, PIRP Irp) {
             tp = next_tp;
     } while (b);
     
+    if (!Vcb->readonly && !Vcb->data_reloc_root) {
+        root* reloc_root;
+        INODE_ITEM* ii;
+        ULONG irlen;
+        INODE_REF* ir;
+        LARGE_INTEGER time;
+        BTRFS_TIME now;
+        LIST_ENTRY rollback;
+        
+        InitializeListHead(&rollback);
+        
+        WARN("data reloc root doesn't exist, creating it\n");
+        
+        Status = create_root(Vcb, BTRFS_ROOT_DATA_RELOC, &reloc_root, FALSE, 0, Irp, &rollback);
+        
+        if (!NT_SUCCESS(Status)) {
+            ERR("create_root returned %08x\n", Status);
+            do_rollback(Vcb, &rollback);
+            goto end;
+        }
+        
+        reloc_root->root_item.inode.generation = 1;
+        reloc_root->root_item.inode.st_size = 3;
+        reloc_root->root_item.inode.st_blocks = Vcb->superblock.node_size;
+        reloc_root->root_item.inode.st_nlink = 1;
+        reloc_root->root_item.inode.st_mode = 040755;
+        reloc_root->root_item.inode.flags = 0xffffffff80000000;
+        reloc_root->root_item.objid = SUBVOL_ROOT_INODE;
+        reloc_root->root_item.bytes_used = Vcb->superblock.node_size;
+        
+        ii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
+        if (!ii) {
+            ERR("out of memory\n");
+            do_rollback(Vcb, &rollback);
+            goto end;
+        }
+        
+        KeQuerySystemTime(&time);
+        win_time_to_unix(time, &now);
+        
+        RtlZeroMemory(ii, sizeof(INODE_ITEM));
+        ii->generation = Vcb->superblock.generation;
+        ii->st_blocks = Vcb->superblock.node_size;
+        ii->st_nlink = 1;
+        ii->st_mode = 040755;
+        ii->st_atime = now;
+        ii->st_ctime = now;
+        ii->st_mtime = now;
+        
+        insert_tree_item(Vcb, reloc_root, SUBVOL_ROOT_INODE, TYPE_INODE_ITEM, 0, ii, sizeof(INODE_ITEM), NULL, Irp, &rollback);
+
+        irlen = offsetof(INODE_REF, name[0]) + 2;
+        ir = ExAllocatePoolWithTag(PagedPool, irlen, ALLOC_TAG);
+        if (!ir) {
+            ERR("out of memory\n");
+            do_rollback(Vcb, &rollback);
+            goto end;
+        }
+        
+        ir->index = 0;
+        ir->n = 2;
+        ir->name[0] = '.';
+        ir->name[1] = '.';
+        
+        insert_tree_item(Vcb, reloc_root, SUBVOL_ROOT_INODE, TYPE_INODE_REF, SUBVOL_ROOT_INODE, ir, irlen, NULL, Irp, &rollback);
+        
+        clear_rollback(Vcb, &rollback);
+        
+        Vcb->data_reloc_root = reloc_root;
+        Vcb->need_write = TRUE;
+    }
+    
+end:
     return STATUS_SUCCESS;
 }
 
@@ -2794,6 +2903,10 @@ static NTSTATUS find_disk_holes(device_extension* Vcb, device* dev, PIRP Irp) {
         }
     }
     
+    // The Linux driver doesn't like to allocate chunks within the first megabyte of a device.
+    
+    space_list_subtract2(Vcb, &dev->space, NULL, 0, 0x100000, NULL);
+    
     return STATUS_SUCCESS;
 }
 
@@ -2837,6 +2950,9 @@ device* find_device_from_uuid(device_extension* Vcb, BTRFS_UUID* uuid) {
                 
                 Vcb->devices[Vcb->devices_loaded].devobj = DeviceObject;
                 Vcb->devices[Vcb->devices_loaded].devitem.device_uuid = *uuid;
+                Vcb->devices[Vcb->devices_loaded].seeding = v->seeding;
+                Vcb->devices[Vcb->devices_loaded].readonly = Vcb->devices[Vcb->devices_loaded].seeding;
+                Vcb->devices[Vcb->devices_loaded].removable = FALSE;
                 Vcb->devices_loaded++;
                 
                 return &Vcb->devices[Vcb->devices_loaded - 1];
@@ -2890,6 +3006,9 @@ static ULONG get_device_change_count(PDEVICE_OBJECT devobj) {
 static void init_device(device_extension* Vcb, device* dev, BOOL get_length) {
     NTSTATUS Status;
     GET_LENGTH_INFORMATION gli;
+    ULONG aptelen;
+    ATA_PASS_THROUGH_EX* apte;
+    IDENTIFY_DEVICE_DATA* idd;
     
     dev->removable = is_device_removable(dev->devobj);
     dev->change_count = dev->removable ? get_device_change_count(dev->devobj) : 0;
@@ -2903,6 +3022,59 @@ static void init_device(device_extension* Vcb, device* dev, BOOL get_length) {
         
         dev->length = gli.Length.QuadPart;
     }
+    
+    dev->ssd = FALSE;
+    dev->trim = FALSE;
+    dev->readonly = dev->seeding;
+    
+    if (!dev->readonly) {
+        Status = dev_ioctl(dev->devobj, IOCTL_DISK_IS_WRITABLE, NULL, 0,
+                        NULL, 0, TRUE, NULL);
+        if (Status == STATUS_MEDIA_WRITE_PROTECTED)
+            dev->readonly = TRUE;
+    }
+
+    aptelen = sizeof(ATA_PASS_THROUGH_EX) + 512;
+    apte = ExAllocatePoolWithTag(NonPagedPool, aptelen, ALLOC_TAG);
+    if (!apte) {
+        ERR("out of memory\n");
+        return;
+    }
+    
+    RtlZeroMemory(apte, aptelen);
+    
+    apte->Length = sizeof(ATA_PASS_THROUGH_EX);
+    apte->AtaFlags = ATA_FLAGS_DATA_IN;
+    apte->DataTransferLength = aptelen - sizeof(ATA_PASS_THROUGH_EX);
+    apte->TimeOutValue = 3;
+    apte->DataBufferOffset = apte->Length;
+    apte->CurrentTaskFile[6] = 0xec; // IDENTIFY DEVICE
+    
+    Status = dev_ioctl(dev->devobj, IOCTL_ATA_PASS_THROUGH, apte, aptelen,
+                       apte, aptelen, TRUE, NULL);
+    
+    if (!NT_SUCCESS(Status)) {
+        ERR("error calling ATA IDENTIFY DEVICE: %08x\n", Status);
+    } else {
+        idd = (IDENTIFY_DEVICE_DATA*)((UINT8*)apte + sizeof(ATA_PASS_THROUGH_EX));
+        
+        if (idd->NominalMediaRotationRate == 1) {
+            dev->ssd = TRUE;
+            TRACE("device identified as SSD\n");
+        } else if (idd->NominalMediaRotationRate == 0)
+            TRACE("no rotational speed returned, assuming not SSD\n");
+        else
+            TRACE("rotational speed of %u RPM\n", idd->NominalMediaRotationRate);
+        
+        if (idd->DataSetManagementFeature.SupportsTrim) {
+            dev->trim = TRUE;
+            Vcb->trim = TRUE;
+            TRACE("TRIM supported\n");
+        } else
+            TRACE("TRIM not supported\n");
+    }
+    
+    ExFreePool(apte);
 }
 
 static NTSTATUS STDCALL load_chunk_root(device_extension* Vcb, PIRP Irp) {
@@ -2954,9 +3126,7 @@ static NTSTATUS STDCALL load_chunk_root(device_extension* Vcb, PIRP Irp) {
                         while (le != &volumes) {
                             volume* v = CONTAINING_RECORD(le, volume, list_entry);
             
-                            if (RtlCompareMemory(&Vcb->superblock.uuid, &v->fsuuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID) &&
-                                RtlCompareMemory(&di->device_uuid, &v->devuuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)
-                            ) {
+                            if (RtlCompareMemory(&di->device_uuid, &v->devuuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
                                 PFILE_OBJECT FileObject;
                                 PDEVICE_OBJECT DeviceObject;
                                 
@@ -2974,6 +3144,9 @@ static NTSTATUS STDCALL load_chunk_root(device_extension* Vcb, PIRP Irp) {
                                 Vcb->devices[Vcb->devices_loaded].devobj = DeviceObject;
                                 RtlCopyMemory(&Vcb->devices[Vcb->devices_loaded].devitem, di, min(tp.item->size, sizeof(DEV_ITEM)));
                                 init_device(Vcb, &Vcb->devices[i], FALSE);
+
+                                Vcb->devices[i].seeding = v->seeding;
+
                                 Vcb->devices[i].length = v->length;
                                 Vcb->devices_loaded++;
 
@@ -3009,6 +3182,7 @@ static NTSTATUS STDCALL load_chunk_root(device_extension* Vcb, PIRP Irp) {
                 c->used = c->oldused = 0;
                 c->cache = NULL;
                 c->created = FALSE;
+                c->readonly = FALSE;
                 
                 c->chunk_item = ExAllocatePoolWithTag(NonPagedPool, tp.item->size, ALLOC_TAG);
                 
@@ -3030,14 +3204,24 @@ static NTSTATUS STDCALL load_chunk_root(device_extension* Vcb, PIRP Irp) {
                     
                     if (!c->devices) {
                         ERR("out of memory\n");
-                        ExFreePool(c);
                         ExFreePool(c->chunk_item);
+                        ExFreePool(c);
                         return STATUS_INSUFFICIENT_RESOURCES;
                     }
                     
                     for (i = 0; i < c->chunk_item->num_stripes; i++) {
                         c->devices[i] = find_device_from_uuid(Vcb, &cis[i].dev_uuid);
                         TRACE("device %llu = %p\n", i, c->devices[i]);
+                        
+                        if (!c->devices[i]) {
+                            ERR("missing device\n");
+                            ExFreePool(c->chunk_item);
+                            ExFreePool(c);
+                            return STATUS_INTERNAL_ERROR;
+                        }
+                            
+                        if (c->devices[i]->readonly)
+                            c->readonly = TRUE;
                     }
                 } else
                     c->devices = NULL;
@@ -3049,6 +3233,10 @@ static NTSTATUS STDCALL load_chunk_root(device_extension* Vcb, PIRP Irp) {
                 InitializeListHead(&c->space_size);
                 InitializeListHead(&c->deleting);
                 InitializeListHead(&c->changed_extents);
+                
+                InitializeListHead(&c->range_locks);
+                KeInitializeSpinLock(&c->range_locks_spinlock);
+                KeInitializeEvent(&c->range_locks_event, NotificationEvent, FALSE);
 
                 InsertTailList(&Vcb->chunks, &c->list_entry);
                 
@@ -3109,6 +3297,42 @@ void protect_superblocks(device_extension* Vcb, chunk* c) {
                     TRACE("startoff = %llx, superblock = %llx\n", startoff + cis[j].offset, superblock_addrs[i]);
 #endif
                     
+                    space_list_subtract(Vcb, c, FALSE, c->offset + off_start, off_end - off_start, NULL);
+                }
+            }
+        } else if (ci->type & BLOCK_FLAG_RAID5) {
+            for (j = 0; j < ci->num_stripes; j++) {
+                UINT64 stripe_size = ci->size / (ci->num_stripes - 1);
+                
+                if (cis[j].offset + stripe_size > superblock_addrs[i] && cis[j].offset <= superblock_addrs[i] + sizeof(superblock)) {
+                    TRACE("cut out superblock in chunk %llx\n", c->offset);
+                    
+                    off_start = superblock_addrs[i] - cis[j].offset;
+                    off_start -= off_start % (ci->stripe_length * (ci->num_stripes - 1));
+                    off_start *= ci->num_stripes - 1;
+
+                    off_end = off_start + (ci->stripe_length * (ci->num_stripes - 1));
+                    
+                    TRACE("cutting out %llx, size %llx\n", c->offset + off_start, off_end - off_start);
+
+                    space_list_subtract(Vcb, c, FALSE, c->offset + off_start, off_end - off_start, NULL);
+                }
+            }
+        } else if (ci->type & BLOCK_FLAG_RAID6) {
+            for (j = 0; j < ci->num_stripes; j++) {
+                UINT64 stripe_size = ci->size / (ci->num_stripes - 2);
+                
+                if (cis[j].offset + stripe_size > superblock_addrs[i] && cis[j].offset <= superblock_addrs[i] + sizeof(superblock)) {
+                    TRACE("cut out superblock in chunk %llx\n", c->offset);
+                    
+                    off_start = superblock_addrs[i] - cis[j].offset;
+                    off_start -= off_start % (ci->stripe_length * (ci->num_stripes - 2));
+                    off_start *= ci->num_stripes - 2;
+
+                    off_end = off_start + (ci->stripe_length * (ci->num_stripes - 2));
+                    
+                    TRACE("cutting out %llx, size %llx\n", c->offset + off_start, off_end - off_start);
+
                     space_list_subtract(Vcb, c, FALSE, c->offset + off_start, off_end - off_start, NULL);
                 }
             }
@@ -3156,7 +3380,7 @@ static NTSTATUS STDCALL find_chunk_usage(device_extension* Vcb, PIRP Irp) {
             return Status;
         }
         
-        if (!keycmp(&searchkey, &tp.item->key)) {
+        if (!keycmp(searchkey, tp.item->key)) {
             if (tp.item->size >= sizeof(BLOCK_GROUP_ITEM)) {
                 bgi = (BLOCK_GROUP_ITEM*)tp.item->data;
                 
@@ -3168,13 +3392,17 @@ static NTSTATUS STDCALL find_chunk_usage(device_extension* Vcb, PIRP Irp) {
                     Vcb->extent_root->id, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(BLOCK_GROUP_ITEM));
             }
         }
-        
+            
 //         if (addr >= c->offset && (addr - c->offset) < c->chunk_item->size && c->chunk_item->num_stripes > 0) {
 //             cis = (CHUNK_ITEM_STRIPE*)&c->chunk_item[1];
 // 
 //             return (addr - c->offset) + cis->offset;
 //         }
-        
+
+        // It doesn't make a great deal of sense to load the free space cache of a
+        // readonly seeding chunk, as we'll never write to it. But btrfs check will
+        // complain if we don't write a valid cache, so we have to do it anyway...
+            
         // FIXME - make sure we free occasionally after doing one of these, or we
         // might use up a lot of memory with a big disk.
         
@@ -3182,7 +3410,7 @@ static NTSTATUS STDCALL find_chunk_usage(device_extension* Vcb, PIRP Irp) {
         if (!NT_SUCCESS(Status)) {
             ERR("load_free_space_cache returned %08x\n", Status);
             return Status;
-        }        
+        }
         
         protect_superblocks(Vcb, c);
 
@@ -3324,7 +3552,7 @@ static root* find_default_subvol(device_extension* Vcb, PIRP Irp) {
             goto end;
         }
         
-        if (keycmp(&tp.item->key, &searchkey)) {
+        if (keycmp(tp.item->key, searchkey)) {
             ERR("could not find (%llx,%x,%llx) in root tree\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
             goto end;
         }
@@ -3378,78 +3606,6 @@ end:
     return NULL;
 }
 
-static NTSTATUS create_worker_threads(PDEVICE_OBJECT DeviceObject) {
-    device_extension* Vcb = DeviceObject->DeviceExtension;
-    ULONG i;
-    NTSTATUS Status;
-    
-    Vcb->threads.num_threads = max(3, KeQueryActiveProcessorCount(NULL));
-    
-    Vcb->threads.threads = ExAllocatePoolWithTag(NonPagedPool, sizeof(drv_thread) * Vcb->threads.num_threads, ALLOC_TAG);
-    if (!Vcb->threads.threads) {
-        ERR("out of memory\n");
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-    
-    RtlZeroMemory(Vcb->threads.threads, sizeof(drv_thread) * Vcb->threads.num_threads);
-    
-    for (i = 0; i < Vcb->threads.num_threads; i++) {
-        Vcb->threads.threads[i].DeviceObject = DeviceObject;
-        KeInitializeEvent(&Vcb->threads.threads[i].event, SynchronizationEvent, FALSE);
-        KeInitializeEvent(&Vcb->threads.threads[i].finished, NotificationEvent, FALSE);
-        InitializeListHead(&Vcb->threads.threads[i].jobs);
-        KeInitializeSpinLock(&Vcb->threads.threads[i].spin_lock);
-        
-        Status = PsCreateSystemThread(&Vcb->threads.threads[i].handle, 0, NULL, NULL, NULL, worker_thread, &Vcb->threads.threads[i]);
-        if (!NT_SUCCESS(Status)) {
-            ULONG j;
-            
-            ERR("PsCreateSystemThread returned %08x\n", Status);
-            
-            for (j = 0; j < i; j++) {
-                Vcb->threads.threads[i].quit = TRUE;
-                KeSetEvent(&Vcb->threads.threads[i].event, 0, FALSE);
-            }
-            
-            return Status;
-        }
-    }
-    
-    Vcb->threads.pending_jobs = 0;
-    
-    return STATUS_SUCCESS;
-}
-
-BOOL add_thread_job(device_extension* Vcb, PIRP Irp) {
-    ULONG threadnum;
-    thread_job* tj;
-    
-    threadnum = InterlockedIncrement(&Vcb->threads.next_thread) % Vcb->threads.num_threads;
-    
-    if (Vcb->threads.pending_jobs >= Vcb->threads.num_threads)
-        return FALSE;
-    
-    if (Vcb->threads.threads[threadnum].quit)
-        return FALSE;
-    
-    tj = ExAllocatePoolWithTag(NonPagedPool, sizeof(thread_job), ALLOC_TAG);
-    if (!tj) {
-        Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
-        Irp->IoStatus.Information = 0;
-        IoCompleteRequest(Irp, IO_NO_INCREMENT);
-        return FALSE;
-    }
-    
-    tj->Irp = Irp;
-    
-    InterlockedIncrement(&Vcb->threads.pending_jobs);
-    
-    ExInterlockedInsertTailList(&Vcb->threads.threads[threadnum].jobs, &tj->list_entry, &Vcb->threads.threads[threadnum].spin_lock);
-    KeSetEvent(&Vcb->threads.threads[threadnum].event, 0, FALSE);
-    
-    return TRUE;
-}
-
 static BOOL raid_generations_okay(device_extension* Vcb) {
     UINT64 i;
     
@@ -3484,11 +3640,12 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     device_extension* Vcb = NULL;
     GET_LENGTH_INFORMATION gli;
     UINT64 i;
-    LIST_ENTRY* le;
+    LIST_ENTRY *le, batchlist;
     KEY searchkey;
     traverse_ptr tp;
     fcb* root_fcb = NULL;
     ccb* root_ccb = NULL;
+    BOOL init_lookaside = FALSE;
     
     TRACE("mount_vol called\n");
     
@@ -3635,6 +3792,9 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     
     Vcb->devices[0].devobj = DeviceToMount;
     RtlCopyMemory(&Vcb->devices[0].devitem, &Vcb->superblock.dev_item, sizeof(DEV_ITEM));
+    
+    Vcb->devices[0].seeding = Vcb->superblock.flags & BTRFS_SUPERBLOCK_FLAGS_SEEDING ? TRUE : FALSE;
+    
     init_device(Vcb, &Vcb->devices[0], FALSE);
     Vcb->devices[0].length = gli.Length.QuadPart;
     
@@ -3643,6 +3803,9 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     
     Vcb->devices_loaded = 1;
     
+    if (DeviceToMount->Flags & DO_SYSTEM_BOOT_PARTITION)
+        Vcb->disallow_dismount = TRUE;
+    
     TRACE("DeviceToMount = %p\n", DeviceToMount);
     TRACE("Stack->Parameters.MountVolume.Vpb = %p\n", Stack->Parameters.MountVolume.Vpb);
 
@@ -3675,17 +3838,22 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     InitializeListHead(&Vcb->all_fcbs);
     InitializeListHead(&Vcb->dirty_fcbs);
     InitializeListHead(&Vcb->dirty_filerefs);
-    InitializeListHead(&Vcb->shared_extents);
     InitializeListHead(&Vcb->sector_checksums);
     
     KeInitializeSpinLock(&Vcb->dirty_fcbs_lock);
     KeInitializeSpinLock(&Vcb->dirty_filerefs_lock);
-    KeInitializeSpinLock(&Vcb->shared_extents_lock);
     
     InitializeListHead(&Vcb->DirNotifyList);
 
     FsRtlNotifyInitializeSync(&Vcb->NotifySync);
     
+    ExInitializePagedLookasideList(&Vcb->tree_data_lookaside, NULL, NULL, 0, sizeof(tree_data), ALLOC_TAG, 0);
+    ExInitializePagedLookasideList(&Vcb->traverse_ptr_lookaside, NULL, NULL, 0, sizeof(traverse_ptr), ALLOC_TAG, 0);
+    ExInitializePagedLookasideList(&Vcb->rollback_item_lookaside, NULL, NULL, 0, sizeof(rollback_item), ALLOC_TAG, 0);
+    ExInitializePagedLookasideList(&Vcb->batch_item_lookaside, NULL, NULL, 0, sizeof(batch_item), ALLOC_TAG, 0);
+    ExInitializeNPagedLookasideList(&Vcb->range_lock_lookaside, NULL, NULL, 0, sizeof(range_lock), ALLOC_TAG, 0);
+    init_lookaside = TRUE;
+    
     Status = load_chunk_root(Vcb, Irp);
     if (!NT_SUCCESS(Status)) {
         ERR("load_chunk_root returned %08x\n", Status);
@@ -3702,6 +3870,23 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
             goto exit;
         }
         
+        if (Vcb->devices[0].readonly && !Vcb->readonly) {
+            Vcb->readonly = TRUE;
+            
+            for (i = 0; i < Vcb->superblock.num_devices; i++) {
+                if (Vcb->devices[i].readonly && !Vcb->devices[i].seeding)
+                    break;
+                
+                if (!Vcb->devices[i].readonly) {
+                    Vcb->readonly = FALSE;
+                    break;
+                }
+            }
+            
+            if (Vcb->readonly)
+                WARN("setting volume to readonly\n");
+        }
+        
         if (!raid_generations_okay(Vcb)) {
             ERR("could not mount as generation mismatch\n");
             
@@ -3710,6 +3895,11 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
             Status = STATUS_INTERNAL_ERROR;
             goto exit;
         }
+    } else {
+        if (Vcb->devices[0].readonly) {
+            WARN("setting volume to readonly as device is readonly\n");
+            Vcb->readonly = TRUE;
+        }
     }
     
     add_root(Vcb, BTRFS_ROOT_ROOT, Vcb->superblock.root_tree_addr, NULL);
@@ -3734,16 +3924,21 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         }
     }
     
+    InitializeListHead(&batchlist);
+    
     // We've already increased the generation by one
     if (!Vcb->readonly && Vcb->superblock.generation - 1 != Vcb->superblock.cache_generation) {
         WARN("generation was %llx, free-space cache generation was %llx; clearing cache...\n", Vcb->superblock.generation - 1, Vcb->superblock.cache_generation);
-        Status = clear_free_space_cache(Vcb, Irp);
+        Status = clear_free_space_cache(Vcb, &batchlist, Irp);
         if (!NT_SUCCESS(Status)) {
             ERR("clear_free_space_cache returned %08x\n", Status);
+            clear_batch_list(Vcb, &batchlist);
             goto exit;
         }
     }
     
+    commit_batch_list(Vcb, &batchlist, Irp, NULL);
+    
     Vcb->volume_fcb = create_fcb(NonPagedPool);
     if (!Vcb->volume_fcb) {
         ERR("out of memory\n");
@@ -3868,12 +4063,6 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         goto exit;
     }
     
-    Status = create_worker_threads(NewDeviceObject);
-    if (!NT_SUCCESS(Status)) {
-        ERR("create_worker_threads returned %08x\n", Status);
-        goto exit;
-    }
-    
     Status = registry_mark_volume_mounted(&Vcb->superblock.uuid);
     if (!NT_SUCCESS(Status))
         WARN("registry_mark_volume_mounted returned %08x\n", Status);
@@ -3887,6 +4076,14 @@ exit:
 
     if (!NT_SUCCESS(Status)) {
         if (Vcb) {
+            if (init_lookaside) {
+                ExDeletePagedLookasideList(&Vcb->tree_data_lookaside);
+                ExDeletePagedLookasideList(&Vcb->traverse_ptr_lookaside);
+                ExDeletePagedLookasideList(&Vcb->rollback_item_lookaside);
+                ExDeletePagedLookasideList(&Vcb->batch_item_lookaside);
+                ExDeleteNPagedLookasideList(&Vcb->range_lock_lookaside);
+            }
+                
             if (Vcb->root_file)
                 ObDereferenceObject(Vcb->root_file);
             else if (Vcb->root_fileref)
@@ -4054,8 +4251,11 @@ static NTSTATUS STDCALL drv_file_system_control(IN PDEVICE_OBJECT DeviceObject,
             Status = verify_volume(DeviceObject);
             
             if (!NT_SUCCESS(Status) && Vcb->Vpb->Flags & VPB_MOUNTED) {
-                uninit(Vcb, FALSE);
-//                 Vcb->Vpb->Flags &= ~VPB_MOUNTED;
+                if (Vcb->open_files > 0) {
+                    Vcb->removing = TRUE;
+//                     Vcb->Vpb->Flags &= ~VPB_MOUNTED;
+                } else
+                    uninit(Vcb, FALSE);
             }
             
             break;
@@ -4119,151 +4319,6 @@ NTSTATUS part0_passthrough(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
     return Status;
 }
 
-static NTSTATUS part0_device_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
-    NTSTATUS Status;
-    part0_device_extension* p0de = DeviceObject->DeviceExtension;
-    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
-    
-    TRACE("control code = %x\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
-    
-    switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
-        case IOCTL_MOUNTDEV_QUERY_UNIQUE_ID:
-        {
-            MOUNTDEV_UNIQUE_ID* mduid;
-
-            if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_UNIQUE_ID)) {
-                Status = STATUS_BUFFER_TOO_SMALL;
-                Irp->IoStatus.Status = Status;
-                Irp->IoStatus.Information = sizeof(MOUNTDEV_UNIQUE_ID);
-                IoCompleteRequest(Irp, IO_NO_INCREMENT);
-                return Status;
-            }
-
-            mduid = Irp->AssociatedIrp.SystemBuffer;
-            mduid->UniqueIdLength = sizeof(BTRFS_UUID);
-
-            if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_UNIQUE_ID) - 1 + mduid->UniqueIdLength) {
-                Status = STATUS_BUFFER_OVERFLOW;
-                Irp->IoStatus.Status = Status;
-                Irp->IoStatus.Information = sizeof(MOUNTDEV_UNIQUE_ID);
-                IoCompleteRequest(Irp, IO_NO_INCREMENT);
-                return Status;
-            }
-
-            RtlCopyMemory(mduid->UniqueId, &p0de->uuid, sizeof(BTRFS_UUID));
-
-            Status = STATUS_SUCCESS;
-            Irp->IoStatus.Status = Status;
-            Irp->IoStatus.Information = sizeof(MOUNTDEV_UNIQUE_ID) - 1 + mduid->UniqueIdLength;
-            IoCompleteRequest(Irp, IO_NO_INCREMENT);
-            
-            return Status;
-        }
-        
-        case IOCTL_MOUNTDEV_QUERY_DEVICE_NAME:
-        {
-            PMOUNTDEV_NAME name;
-
-            if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_NAME)) {
-                Status = STATUS_BUFFER_TOO_SMALL;
-                Irp->IoStatus.Status = Status;
-                Irp->IoStatus.Information = sizeof(MOUNTDEV_NAME);
-                IoCompleteRequest(Irp, IO_NO_INCREMENT);
-                return Status;
-            }
-
-            name = Irp->AssociatedIrp.SystemBuffer;
-            name->NameLength = p0de->name.Length;
-
-            if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_NAME) - 1 + name->NameLength) {
-                Status = STATUS_BUFFER_OVERFLOW;
-                Irp->IoStatus.Status = Status;
-                Irp->IoStatus.Information = sizeof(MOUNTDEV_NAME);
-                IoCompleteRequest(Irp, IO_NO_INCREMENT);
-                return Status;
-            }
-            
-            RtlCopyMemory(name->Name, p0de->name.Buffer, p0de->name.Length);
-
-            Status = STATUS_SUCCESS;
-            Irp->IoStatus.Status = Status;
-            Irp->IoStatus.Information = sizeof(MOUNTDEV_NAME) - 1 + name->NameLength;
-            IoCompleteRequest(Irp, IO_NO_INCREMENT);
-            
-            return Status;
-        }
-    }
-    
-    IoSkipCurrentIrpStackLocation(Irp);
-    
-    Status = IoCallDriver(p0de->devobj, Irp);
-    
-    TRACE("returning %08x\n", Status);
-    
-    return Status;
-}
-
-static NTSTATUS STDCALL drv_device_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
-    NTSTATUS Status;
-    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
-    PFILE_OBJECT FileObject = IrpSp->FileObject;
-    device_extension* Vcb = DeviceObject->DeviceExtension;
-    fcb* fcb;
-    BOOL top_level;
-
-    FsRtlEnterFileSystem();
-
-    top_level = is_top_level(Irp);
-    
-    Irp->IoStatus.Information = 0;
-    
-    if (Vcb && Vcb->type == VCB_TYPE_PARTITION0) {
-        Status = part0_device_control(DeviceObject, Irp);
-        goto end2;
-    }
-    
-    TRACE("control code = %x\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
-    
-    if (!FileObject) {
-        ERR("FileObject was NULL\n");
-        Status = STATUS_INVALID_PARAMETER;
-        goto end;
-    }
-    
-    fcb = FileObject->FsContext;
-    
-    if (!fcb) {
-        ERR("FCB was NULL\n");
-        Status = STATUS_INVALID_PARAMETER;
-        goto end;
-    }
-    
-    if (fcb != Vcb->volume_fcb) {
-        Status = STATUS_NOT_IMPLEMENTED;
-        goto end;
-    }
-    
-    IoSkipCurrentIrpStackLocation(Irp);
-    
-    Status = IoCallDriver(Vcb->devices[0].devobj, Irp);
-    
-    goto end2;
-    
-end:
-    Irp->IoStatus.Status = Status;
-
-    if (Status != STATUS_PENDING)
-        IoCompleteRequest(Irp, IO_NO_INCREMENT);
-    
-end2:
-    if (top_level) 
-        IoSetTopLevelIrp(NULL);
-    
-    FsRtlExitFileSystem();
-
-    return Status;
-}
-
 static NTSTATUS STDCALL drv_shutdown(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
     NTSTATUS Status;
     BOOL top_level;
@@ -4325,6 +4380,79 @@ BOOL is_file_name_valid(PUNICODE_STRING us) {
     return TRUE;
 }
 
+void chunk_lock_range(device_extension* Vcb, chunk* c, UINT64 start, UINT64 length) {
+    LIST_ENTRY* le;
+    BOOL locked;
+    range_lock* rl;
+    
+    rl = ExAllocateFromNPagedLookasideList(&Vcb->range_lock_lookaside);
+    if (!rl) {
+        ERR("out of memory\n");
+        return;
+    }
+    
+    rl->start = start;
+    rl->length = length;
+    rl->thread = PsGetCurrentThread();
+    
+    while (TRUE) {
+        KIRQL irql;
+        
+        locked = FALSE;
+        
+        KeAcquireSpinLock(&c->range_locks_spinlock, &irql);
+        
+        le = c->range_locks.Flink;
+        while (le != &c->range_locks) {
+            range_lock* rl2 = CONTAINING_RECORD(le, range_lock, list_entry);
+            
+            if (rl2->start < start + length && rl2->start + rl2->length > start && rl2->thread != PsGetCurrentThread()) {
+                locked = TRUE;
+                break;
+            }
+            
+            le = le->Flink;
+        }
+        
+        if (!locked) {
+            InsertTailList(&c->range_locks, &rl->list_entry);
+            
+            KeReleaseSpinLock(&c->range_locks_spinlock, irql);
+            return;
+        }
+        
+        KeClearEvent(&c->range_locks_event);
+        
+        KeReleaseSpinLock(&c->range_locks_spinlock, irql);
+        
+        KeWaitForSingleObject(&c->range_locks_event, UserRequest, KernelMode, FALSE, NULL);
+    }
+}
+
+void chunk_unlock_range(device_extension* Vcb, chunk* c, UINT64 start, UINT64 length) {
+    KIRQL irql;
+    LIST_ENTRY* le;
+    
+    KeAcquireSpinLock(&c->range_locks_spinlock, &irql);
+    
+    le = c->range_locks.Flink;
+    while (le != &c->range_locks) {
+        range_lock* rl = CONTAINING_RECORD(le, range_lock, list_entry);
+        
+        if (rl->start == start && rl->length == length) {
+            RemoveEntryList(&rl->list_entry);
+            ExFreeToNPagedLookasideList(&Vcb->range_lock_lookaside, rl);
+            break;
+        }
+        
+        le = le->Flink;
+    }
+    
+    KeSetEvent(&c->range_locks_event, 0, FALSE);
+    
+    KeReleaseSpinLock(&c->range_locks_spinlock, irql);
+}
+
 #ifdef _DEBUG
 static void STDCALL init_serial() {
     NTSTATUS Status;
@@ -4342,15 +4470,22 @@ static void STDCALL check_cpu() {
 #ifndef _MSC_VER
     __get_cpuid(1, &cpuInfo[0], &cpuInfo[1], &cpuInfo[2], &cpuInfo[3]);
     have_sse42 = cpuInfo[2] & bit_SSE4_2;
+    have_sse2 = cpuInfo[3] & bit_SSE2;
 #else
    __cpuid(cpuInfo, 1);
    have_sse42 = cpuInfo[2] & (1 << 20);
+   have_sse2 = cpuInfo[3] & (1 << 26);
 #endif
 
     if (have_sse42)
         TRACE("SSE4.2 is supported\n");
     else
         TRACE("SSE4.2 not supported\n");
+    
+    if (have_sse2)
+        TRACE("SSE2 is supported\n");
+    else
+        TRACE("SSE2 is not supported\n");
 }
 #endif
 
index b3a2398..c5184cd 100644 (file)
@@ -46,6 +46,7 @@ static const UINT64 superblock_addrs[] = { 0x10000, 0x4000000, 0x4000000000, 0x4
 #define BTRFS_ROOT_FSTREE       5
 #define BTRFS_ROOT_CHECKSUM     7
 #define BTRFS_ROOT_UUID         9
+#define BTRFS_ROOT_DATA_RELOC   0xFFFFFFFFFFFFFFF7
 
 #define BTRFS_COMPRESSION_NONE  0
 #define BTRFS_COMPRESSION_ZLIB  1
@@ -100,6 +101,8 @@ static const UINT64 superblock_addrs[] = { 0x10000, 0x4000000, 0x4000000000, 0x4
 #define BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA    0x0100
 #define BTRFS_INCOMPAT_FLAGS_NO_HOLES           0x0200
 
+#define BTRFS_SUPERBLOCK_FLAGS_SEEDING   0x100000000
+
 #pragma pack(push, 1)
 
 typedef struct {
@@ -112,8 +115,9 @@ typedef struct {
     UINT64 offset;
 } KEY;
 
-#define HEADER_FLAG_MIXED_BACKREF   0x100000000000000
+#define HEADER_FLAG_WRITTEN         0x000000000000001
 #define HEADER_FLAG_SHARED_BACKREF  0x000000000000002
+#define HEADER_FLAG_MIXED_BACKREF   0x100000000000000
 
 typedef struct {
     UINT8 csum[32];
index 4d7d89e..467f2e8 100644 (file)
@@ -70,12 +70,12 @@ BEGIN
         BLOCK "080904b0"
         BEGIN
             VALUE "FileDescription", "WinBtrfs"
-            VALUE "FileVersion", "0.5"
+            VALUE "FileVersion", "0.7"
             VALUE "InternalName", "btrfs"
             VALUE "LegalCopyright", "Copyright (c) Mark Harmstone 2016"
             VALUE "OriginalFilename", "btrfs.sys"
             VALUE "ProductName", "WinBtrfs"
-            VALUE "ProductVersion", "0.6"
+            VALUE "ProductVersion", "0.7"
         END
     END
     BLOCK "VarFileInfo"
index 1b01d92..22ae32e 100644 (file)
 #include <stdio.h>
 #include <stdarg.h>
 #include <stddef.h>
+#include <emmintrin.h>
 #include "btrfs.h"
 
 #ifdef _DEBUG
 // #define DEBUG_FCB_REFCOUNTS
 // #define DEBUG_LONG_MESSAGES
+// #define DEBUG_FLUSH_TIMES
+// #define DEBUG_STATS
 #define DEBUG_PARANOID
 #endif
 
 #define EA_REPARSE "system.reparse"
 #define EA_REPARSE_HASH 0x786f6167
 
+#define EA_EA "user.EA"
+#define EA_EA_HASH 0x8270dd43
+
 #define MAX_EXTENT_SIZE 0x8000000 // 128 MB
 #define COMPRESSED_EXTENT_SIZE 0x20000 // 128 KB
 
 #define READ_AHEAD_GRANULARITY COMPRESSED_EXTENT_SIZE // really ought to be a multiple of COMPRESSED_EXTENT_SIZE
 
+#define IO_REPARSE_TAG_LXSS_SYMLINK 0xa000001d // undocumented?
+
 #ifdef _MSC_VER
 #define try __try
 #define except __except
@@ -98,6 +106,7 @@ typedef struct {
     UNICODE_STRING devpath;
     UINT64 length;
     UINT64 gen1, gen2;
+    BOOL seeding;
     BOOL processed;
     LIST_ENTRY list_entry;
 } volume;
@@ -147,7 +156,6 @@ typedef struct _fcb {
     FSRTL_ADVANCED_FCB_HEADER Header;
     struct _fcb_nonpaged* nonpaged;
     LONG refcount;
-    LONG open_count;
     struct _device_extension* Vcb;
     struct _root* subvol;
     UINT64 inode;
@@ -163,8 +171,11 @@ typedef struct _fcb {
     LIST_ENTRY extents;
     UINT64 last_dir_index;
     ANSI_STRING reparse_xattr;
+    ANSI_STRING ea_xattr;
+    ULONG ealen;
     LIST_ENTRY hardlinks;
     struct _file_ref* fileref;
+    BOOL inode_item_changed;
     
     BOOL index_loaded;
     LIST_ENTRY index_list;
@@ -174,6 +185,7 @@ typedef struct _fcb {
     BOOL atts_changed, atts_deleted;
     BOOL extents_changed;
     BOOL reparse_xattr_changed;
+    BOOL ea_changed;
     BOOL created;
     
     BOOL ads;
@@ -208,6 +220,7 @@ typedef struct _file_ref {
     file_ref_nonpaged* nonpaged;
     LIST_ENTRY children;
     LONG refcount;
+    LONG open_count;
     struct _file_ref* parent;
     WCHAR* debug_desc;
     
@@ -234,6 +247,12 @@ typedef struct _ccb {
     ACCESS_MASK access;
     file_ref* fileref;
     UNICODE_STRING filename;
+    ULONG ea_index;
+    BOOL case_sensitive;
+    BOOL user_set_creation_time;
+    BOOL user_set_access_time;
+    BOOL user_set_write_time;
+    BOOL user_set_change_time;
 } ccb;
 
 // typedef struct _log_to_phys {
@@ -301,6 +320,7 @@ typedef struct _tree {
     LIST_ENTRY list_entry;
     UINT64 new_address;
     BOOL has_new_address;
+    BOOL updated_extents;
     UINT64 flags;
     BOOL write;
 } tree;
@@ -312,15 +332,37 @@ typedef struct {
 
 typedef struct _root {
     UINT64 id;
+    LONGLONG lastinode; // signed so we can use InterlockedIncrement64
     tree_holder treeholder;
     root_nonpaged* nonpaged;
-    UINT64 lastinode;
     ROOT_ITEM root_item;
     UNICODE_STRING path;
     LIST_ENTRY fcbs;
     LIST_ENTRY list_entry;
 } root;
 
+enum batch_operation {
+    Batch_Insert,
+    Batch_SetXattr,
+    Batch_DirItem,
+    Batch_InodeRef,
+    Batch_InodeExtRef,
+};
+
+typedef struct {
+    KEY key;
+    void* data;
+    UINT16 datalen;
+    enum batch_operation operation;
+    LIST_ENTRY list_entry;
+} batch_item;
+
+typedef struct {
+    root* r;
+    LIST_ENTRY items;
+    LIST_ENTRY list_entry;
+} batch_root;
+
 typedef struct {
     tree* tree;
     tree_data* item;
@@ -342,11 +384,22 @@ typedef struct {
     PDEVICE_OBJECT devobj;
     DEV_ITEM devitem;
     BOOL removable;
+    BOOL seeding;
+    BOOL readonly;
+    BOOL ssd;
+    BOOL trim;
     ULONG change_count;
     UINT64 length;
     LIST_ENTRY space;
 } device;
 
+typedef struct {
+    UINT64 start;
+    UINT64 length;
+    PETHREAD thread;
+    LIST_ENTRY list_entry;
+} range_lock;
+
 typedef struct {
     CHUNK_ITEM* chunk_item;
     UINT32 size;
@@ -359,9 +412,13 @@ typedef struct {
     LIST_ENTRY space_size;
     LIST_ENTRY deleting;
     LIST_ENTRY changed_extents;
+    LIST_ENTRY range_locks;
+    KSPIN_LOCK range_locks_spinlock;
+    KEVENT range_locks_event;
     ERESOURCE lock;
     ERESOURCE changed_extents_lock;
     BOOL created;
+    BOOL readonly;
     
     LIST_ENTRY list_entry;
     LIST_ENTRY list_entry_changed;
@@ -374,30 +431,23 @@ typedef struct {
     UINT64 count;
     UINT64 old_count;
     BOOL no_csum;
+    BOOL superseded;
     LIST_ENTRY refs;
     LIST_ENTRY old_refs;
     LIST_ENTRY list_entry;
 } changed_extent;
 
 typedef struct {
-    EXTENT_DATA_REF edr;
+    UINT8 type;
+    
+    union {
+        EXTENT_DATA_REF edr;
+        SHARED_DATA_REF sdr;
+    };
+    
     LIST_ENTRY list_entry;
 } changed_extent_ref;
 
-typedef struct {
-    UINT64 address;
-    UINT64 size;
-    EXTENT_DATA_REF edr;
-    LIST_ENTRY list_entry;
-} shared_data_entry;
-
-typedef struct {
-    UINT64 address;
-    UINT64 parent;
-    LIST_ENTRY entries;
-    LIST_ENTRY list_entry;
-} shared_data;
-
 typedef struct {
     KEY key;
     void* data;
@@ -405,27 +455,6 @@ typedef struct {
     LIST_ENTRY list_entry;
 } sys_chunk;
 
-typedef struct {
-    PIRP Irp;
-    LIST_ENTRY list_entry;
-} thread_job;
-
-typedef struct {
-    PDEVICE_OBJECT DeviceObject;
-    HANDLE handle;
-    KEVENT event, finished;
-    BOOL quit;
-    LIST_ENTRY jobs;
-    KSPIN_LOCK spin_lock;
-} drv_thread;
-
-typedef struct {
-    ULONG num_threads;
-    LONG next_thread;
-    drv_thread* threads;
-    LONG pending_jobs;
-} drv_threads;
-
 typedef struct {
     BOOL ignore;
     BOOL compress;
@@ -436,16 +465,31 @@ typedef struct {
     UINT32 flush_interval;
     UINT32 max_inline;
     UINT64 subvol_id;
+    UINT32 raid5_recalculation;
+    UINT32 raid6_recalculation;
 } mount_options;
 
 #define VCB_TYPE_VOLUME     1
 #define VCB_TYPE_PARTITION0 2
 
+#ifdef DEBUG_STATS
+typedef struct {
+    UINT64 num_reads;
+    UINT64 data_read;
+    UINT64 read_total_time;
+    UINT64 read_csum_time;
+    UINT64 read_disk_time;
+} debug_stats;
+#endif
+
 typedef struct _device_extension {
     UINT32 type;
     mount_options options;
     PVPB Vpb;
     device* devices;
+#ifdef DEBUG_STATS
+    debug_stats stats;
+#endif
     UINT64 devices_loaded;
 //     DISK_GEOMETRY geometry;
     superblock superblock;
@@ -453,9 +497,12 @@ typedef struct _device_extension {
     BOOL readonly;
     BOOL removing;
     BOOL locked;
+    BOOL disallow_dismount;
+    BOOL trim;
     PFILE_OBJECT locked_fileobj;
     fcb* volume_fcb;
     file_ref* root_fileref;
+    LONG open_files;
     ERESOURCE DirResource;
     KSPIN_LOCK FcbListLock;
     ERESOURCE fcb_lock;
@@ -478,6 +525,7 @@ typedef struct _device_extension {
     root* checksum_root;
     root* dev_root;
     root* uuid_root;
+    root* data_reloc_root;
     BOOL log_to_phys_loaded;
     LIST_ENTRY sys_chunks;
     LIST_ENTRY chunks;
@@ -491,13 +539,15 @@ typedef struct _device_extension {
     ERESOURCE checksum_lock;
     ERESOURCE chunk_lock;
     LIST_ENTRY sector_checksums;
-    LIST_ENTRY shared_extents;
-    KSPIN_LOCK shared_extents_lock;
     HANDLE flush_thread_handle;
     KTIMER flush_thread_timer;
     KEVENT flush_thread_finished;
-    drv_threads threads;
     PFILE_OBJECT root_file;
+    PAGED_LOOKASIDE_LIST tree_data_lookaside;
+    PAGED_LOOKASIDE_LIST traverse_ptr_lookaside;
+    PAGED_LOOKASIDE_LIST rollback_item_lookaside;
+    PAGED_LOOKASIDE_LIST batch_item_lookaside;
+    NPAGED_LOOKASIDE_LIST range_lock_lookaside;
     LIST_ENTRY list_entry;
 } device_extension;
 
@@ -607,16 +657,35 @@ static __inline void get_raid0_offset(UINT64 off, UINT64 stripe_length, UINT16 n
     *stripeoff = initoff + startoff - (*stripe * stripe_length);
 }
 
+/* We only have 64 bits for a file ID, which isn't technically enough to be
+ * unique on Btrfs. We fudge it by having three bytes for the subvol and
+ * five for the inode, which should be good enough.
+ * Inodes are also 64 bits on Linux, but the Linux driver seems to get round
+ * this by tricking it into thinking subvols are separate volumes. */
+#ifdef __REACTOS__
+static __inline UINT64 make_file_id(root* r, UINT64 inode) {
+#else
+static UINT64 __inline make_file_id(root* r, UINT64 inode) {
+#endif
+    return (r->id << 40) | (inode & 0xffffffffff);
+}
+
+#define keycmp(key1, key2)\
+    ((key1.obj_id < key2.obj_id) ? -1 :\
+    ((key1.obj_id > key2.obj_id) ? 1 :\
+    ((key1.obj_type < key2.obj_type) ? -1 :\
+    ((key1.obj_type > key2.obj_type) ? 1 :\
+    ((key1.offset < key2.offset) ? -1 :\
+    ((key1.offset > key2.offset) ? 1 :\
+    0))))))
+
 // in btrfs.c
 device* find_device_from_uuid(device_extension* Vcb, BTRFS_UUID* uuid);
 UINT64 sector_align( UINT64 NumberToBeAligned, UINT64 Alignment );
-int keycmp(const KEY* key1, const KEY* key2);
 ULONG STDCALL get_file_attributes(device_extension* Vcb, INODE_ITEM* ii, root* r, UINT64 inode, UINT8 type, BOOL dotfile, BOOL ignore_xa, PIRP Irp);
 BOOL STDCALL get_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char* name, UINT32 crc32, UINT8** data, UINT16* datalen, PIRP Irp);
 void _free_fcb(fcb* fcb, const char* func, const char* file, unsigned int line);
 void _free_fileref(file_ref* fr, const char* func, const char* file, unsigned int line);
-BOOL STDCALL get_last_inode(device_extension* Vcb, root* r, PIRP Irp);
-NTSTATUS add_dir_item(device_extension* Vcb, root* subvol, UINT64 inode, UINT32 crc32, DIR_ITEM* di, ULONG disize, PIRP Irp, LIST_ENTRY* rollback);
 NTSTATUS delete_dir_item(device_extension* Vcb, root* subvol, UINT64 parinode, UINT32 crc32, PANSI_STRING utf8, PIRP Irp, LIST_ENTRY* rollback);
 NTSTATUS delete_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UINT64 parinode, PANSI_STRING utf8, PIRP Irp, LIST_ENTRY* rollback);
 fcb* create_fcb(POOL_TYPE pool_type);
@@ -637,6 +706,8 @@ NTSTATUS part0_passthrough(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
 void mark_fcb_dirty(fcb* fcb);
 void mark_fileref_dirty(file_ref* fileref);
 NTSTATUS delete_fileref(file_ref* fileref, PFILE_OBJECT FileObject, PIRP Irp, LIST_ENTRY* rollback);
+void chunk_lock_range(device_extension* Vcb, chunk* c, UINT64 start, UINT64 length);
+void chunk_unlock_range(device_extension* Vcb, chunk* c, UINT64 start, UINT64 length);
 
 #ifdef _MSC_VER
 #define funcname __FUNCTION__
@@ -648,12 +719,16 @@ NTSTATUS delete_fileref(file_ref* fileref, PFILE_OBJECT FileObject, PIRP Irp, LI
 #define free_fcb(fcb) _free_fcb(fcb, funcname, __FILE__, __LINE__)
 #define free_fileref(fileref) _free_fileref(fileref, funcname, __FILE__, __LINE__)
 
+extern BOOL have_sse2;
+
 extern UINT32 mount_compress;
 extern UINT32 mount_compress_force;
 extern UINT32 mount_compress_type;
 extern UINT32 mount_zlib_level;
 extern UINT32 mount_flush_interval;
 extern UINT32 mount_max_inline;
+extern UINT32 mount_raid5_recalculation;
+extern UINT32 mount_raid6_recalculation;
 
 #ifdef _DEBUG
 
@@ -732,6 +807,12 @@ enum rollback_type {
     ROLLBACK_SUBTRACT_SPACE
 };
 
+typedef struct {
+    enum rollback_type type;
+    void* ptr;
+    LIST_ENTRY list_entry;
+} rollback_item;
+
 // in treefuncs.c
 NTSTATUS STDCALL _find_item(device_extension* Vcb, root* r, traverse_ptr* tp, const KEY* searchkey, BOOL ignore, PIRP Irp, const char* func, const char* file, unsigned int line);
 BOOL STDCALL _find_next_item(device_extension* Vcb, const traverse_ptr* tp, traverse_ptr* next_tp, BOOL ignore, PIRP Irp, const char* func, const char* file, unsigned int line);
@@ -743,10 +824,12 @@ tree* STDCALL _free_tree(tree* t, const char* func, const char* file, unsigned i
 NTSTATUS STDCALL _load_tree(device_extension* Vcb, UINT64 addr, root* r, tree** pt, tree* parent, PIRP Irp, const char* func, const char* file, unsigned int line);
 NTSTATUS STDCALL _do_load_tree(device_extension* Vcb, tree_holder* th, root* r, tree* t, tree_data* td, BOOL* loaded, PIRP Irp,
                                const char* func, const char* file, unsigned int line);
-void clear_rollback(LIST_ENTRY* rollback);
+void clear_rollback(device_extension* Vcb, LIST_ENTRY* rollback);
 void do_rollback(device_extension* Vcb, LIST_ENTRY* rollback);
 void free_trees_root(device_extension* Vcb, root* r);
-void add_rollback(LIST_ENTRY* rollback, enum rollback_type type, void* ptr);
+void add_rollback(device_extension* Vcb, LIST_ENTRY* rollback, enum rollback_type type, void* ptr);
+void commit_batch_list(device_extension* Vcb, LIST_ENTRY* batchlist, PIRP Irp, LIST_ENTRY* rollback);
+void clear_batch_list(device_extension* Vcb, LIST_ENTRY* batchlist);
 
 #define find_item(Vcb, r, tp, searchkey, ignore, Irp) _find_item(Vcb, r, tp, searchkey, ignore, Irp, funcname, __FILE__, __LINE__)
 #define find_next_item(Vcb, tp, next_tp, ignore, Irp) _find_next_item(Vcb, tp, next_tp, ignore, Irp, funcname, __FILE__, __LINE__)
@@ -764,7 +847,6 @@ void STDCALL free_cache();
 extern CACHE_MANAGER_CALLBACKS* cache_callbacks;
 
 // in write.c
-NTSTATUS STDCALL do_write(device_extension* Vcb, PIRP Irp, LIST_ENTRY* rollback);
 NTSTATUS write_file(device_extension* Vcb, PIRP Irp, BOOL wait, BOOL deferred_write);
 NTSTATUS write_file2(device_extension* Vcb, PIRP Irp, LARGE_INTEGER offset, void* buf, ULONG* length, BOOL paging_io, BOOL no_cache,
                      BOOL wait, BOOL deferred_write, LIST_ENTRY* rollback);
@@ -777,16 +859,14 @@ chunk* alloc_chunk(device_extension* Vcb, UINT64 flags);
 NTSTATUS STDCALL write_data(device_extension* Vcb, UINT64 address, void* data, BOOL need_free, UINT32 length, write_data_context* wtc, PIRP Irp, chunk* c);
 NTSTATUS STDCALL write_data_complete(device_extension* Vcb, UINT64 address, void* data, UINT32 length, PIRP Irp, chunk* c);
 void free_write_data_stripes(write_data_context* wtc);
-NTSTATUS get_tree_new_address(device_extension* Vcb, tree* t, PIRP Irp, LIST_ENTRY* rollback);
 NTSTATUS STDCALL drv_write(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
-void flush_fcb(fcb* fcb, BOOL cache, PIRP Irp, LIST_ENTRY* rollback);
 BOOL insert_extent_chunk(device_extension* Vcb, fcb* fcb, chunk* c, UINT64 start_data, UINT64 length, BOOL prealloc, void* data, LIST_ENTRY* changed_sector_list,
                          PIRP Irp, LIST_ENTRY* rollback, UINT8 compression, UINT64 decoded_size);
 NTSTATUS insert_extent(device_extension* Vcb, fcb* fcb, UINT64 start_data, UINT64 length, void* data, LIST_ENTRY* changed_sector_list, PIRP Irp, LIST_ENTRY* rollback);
-NTSTATUS update_changed_extent_ref(device_extension* Vcb, chunk* c, UINT64 address, UINT64 size, UINT64 root, UINT64 objid, UINT64 offset,
-                                   signed long long count, BOOL no_csum, UINT64 new_size, PIRP Irp);
 NTSTATUS do_write_file(fcb* fcb, UINT64 start_data, UINT64 end_data, void* data, LIST_ENTRY* changed_sector_list, PIRP Irp, LIST_ENTRY* rollback);
 NTSTATUS write_compressed(fcb* fcb, UINT64 start_data, UINT64 end_data, void* data, LIST_ENTRY* changed_sector_list, PIRP Irp, LIST_ENTRY* rollback);
+BOOL find_address_in_chunk(device_extension* Vcb, chunk* c, UINT64 length, UINT64* address);
+void get_raid56_lock_range(chunk* c, UINT64 address, UINT64 length, UINT64* lockaddr, UINT64* locklen);
 
 // in dirctrl.c
 NTSTATUS STDCALL drv_directory_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
@@ -805,23 +885,25 @@ NTSTATUS fcb_get_new_sd(fcb* fcb, file_ref* parfileref, ACCESS_STATE* as);
 // in fileinfo.c
 NTSTATUS STDCALL drv_set_information(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
 NTSTATUS STDCALL drv_query_information(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
-NTSTATUS add_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UINT64 parinode, UINT64 index, PANSI_STRING utf8, PIRP Irp, LIST_ENTRY* rollback);
 BOOL has_open_children(file_ref* fileref);
 NTSTATUS STDCALL stream_set_end_of_file_information(device_extension* Vcb, UINT64 end, fcb* fcb, file_ref* fileref, PFILE_OBJECT FileObject, BOOL advance_only, LIST_ENTRY* rollback);
 NTSTATUS fileref_get_filename(file_ref* fileref, PUNICODE_STRING fn, USHORT* name_offset);
 NTSTATUS open_fileref_by_inode(device_extension* Vcb, root* subvol, UINT64 inode, file_ref** pfr, PIRP Irp);
+NTSTATUS STDCALL drv_query_ea(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
+NTSTATUS STDCALL drv_set_ea(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
 
 // in reparse.c
-NTSTATUS get_reparse_point(PDEVICE_OBJECT DeviceObject, PFILE_OBJECT FileObject, void* buffer, DWORD buflen, DWORD* retlen);
+NTSTATUS get_reparse_point(PDEVICE_OBJECT DeviceObject, PFILE_OBJECT FileObject, void* buffer, DWORD buflen, ULONG_PTR* retlen);
 NTSTATUS set_reparse_point(PDEVICE_OBJECT DeviceObject, PIRP Irp);
 NTSTATUS delete_reparse_point(PDEVICE_OBJECT DeviceObject, PIRP Irp);
 
 // in create.c
 NTSTATUS STDCALL drv_create(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
 NTSTATUS STDCALL find_file_in_dir(device_extension* Vcb, PUNICODE_STRING filename, file_ref* fr,
-                                  root** subvol, UINT64* inode, UINT8* type, UINT64* index, PANSI_STRING utf8, PIRP Irp);
-NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnus, file_ref* related, BOOL parent, USHORT* unparsed, ULONG* fn_offset, PIRP Irp);
-NTSTATUS open_fcb(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type, PANSI_STRING utf8, fcb* parent, fcb** pfcb, PIRP Irp);
+                                  root** subvol, UINT64* inode, UINT8* type, UINT64* index, PANSI_STRING utf8, BOOL case_sensitive, PIRP Irp);
+NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnus, file_ref* related, BOOL parent, USHORT* unparsed, ULONG* fn_offset,
+                      POOL_TYPE pooltype, BOOL case_sensitive, PIRP Irp);
+NTSTATUS open_fcb(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type, PANSI_STRING utf8, fcb* parent, fcb** pfcb, POOL_TYPE pooltype, PIRP Irp);
 NTSTATUS open_fcb_stream(device_extension* Vcb, root* subvol, UINT64 inode, ANSI_STRING* xattr, UINT32 streamhash, fcb* parent, fcb** pfcb, PIRP Irp);
 void insert_fileref_child(file_ref* parent, file_ref* child, BOOL do_lock);
 NTSTATUS fcb_get_last_dir_index(fcb* fcb, UINT64* index, PIRP Irp);
@@ -833,10 +915,15 @@ void do_unlock_volume(device_extension* Vcb);
 
 // in flushthread.c
 void STDCALL flush_thread(void* context);
+NTSTATUS STDCALL do_write(device_extension* Vcb, PIRP Irp, LIST_ENTRY* rollback);
+NTSTATUS get_tree_new_address(device_extension* Vcb, tree* t, PIRP Irp, LIST_ENTRY* rollback);
+void flush_fcb(fcb* fcb, BOOL cache, LIST_ENTRY* batchlist, PIRP Irp, LIST_ENTRY* rollback);
+NTSTATUS STDCALL write_data_phys(PDEVICE_OBJECT device, UINT64 address, void* data, UINT32 length);
+BOOL is_tree_unique(device_extension* Vcb, tree* t, PIRP Irp);
 
 // in read.c
 NTSTATUS STDCALL drv_read(PDEVICE_OBJECT DeviceObject, PIRP Irp);
-NTSTATUS STDCALL read_data(device_extension* Vcb, UINT64 addr, UINT32 length, UINT32* csum, BOOL is_tree, UINT8* buf, chunk** pc, PIRP Irp);
+NTSTATUS STDCALL read_data(device_extension* Vcb, UINT64 addr, UINT32 length, UINT32* csum, BOOL is_tree, UINT8* buf, chunk* c, chunk** pc, PIRP Irp);
 NTSTATUS STDCALL read_file(fcb* fcb, UINT8* data, UINT64 start, UINT64 length, ULONG* pbr, PIRP Irp);
 NTSTATUS do_read(PIRP Irp, BOOL wait, ULONG* bytes_read);
 
@@ -845,31 +932,40 @@ NTSTATUS STDCALL drv_pnp(PDEVICE_OBJECT DeviceObject, PIRP Irp);
 
 // in free-space.c
 NTSTATUS load_free_space_cache(device_extension* Vcb, chunk* c, PIRP Irp);
-NTSTATUS clear_free_space_cache(device_extension* Vcb, PIRP Irp);
+NTSTATUS clear_free_space_cache(device_extension* Vcb, LIST_ENTRY* batchlist, PIRP Irp);
 NTSTATUS allocate_cache(device_extension* Vcb, BOOL* changed, PIRP Irp, LIST_ENTRY* rollback);
 NTSTATUS update_chunk_caches(device_extension* Vcb, PIRP Irp, LIST_ENTRY* rollback);
 NTSTATUS add_space_entry(LIST_ENTRY* list, LIST_ENTRY* list_size, UINT64 offset, UINT64 size);
 void _space_list_add(device_extension* Vcb, chunk* c, BOOL deleting, UINT64 address, UINT64 length, LIST_ENTRY* rollback, const char* func);
-void _space_list_add2(LIST_ENTRY* list, LIST_ENTRY* list_size, UINT64 address, UINT64 length, chunk* c, LIST_ENTRY* rollback, const char* func);
+void _space_list_add2(device_extension* Vcb, LIST_ENTRY* list, LIST_ENTRY* list_size, UINT64 address, UINT64 length, chunk* c, LIST_ENTRY* rollback, const char* func);
 void _space_list_subtract(device_extension* Vcb, chunk* c, BOOL deleting, UINT64 address, UINT64 length, LIST_ENTRY* rollback, const char* func);
-void _space_list_subtract2(LIST_ENTRY* list, LIST_ENTRY* list_size, UINT64 address, UINT64 length, chunk* c, LIST_ENTRY* rollback, const char* func);
+void _space_list_subtract2(device_extension* Vcb, LIST_ENTRY* list, LIST_ENTRY* list_size, UINT64 address, UINT64 length, chunk* c, LIST_ENTRY* rollback, const char* func);
 
 #define space_list_add(Vcb, c, deleting, address, length, rollback) _space_list_add(Vcb, c, deleting, address, length, rollback, funcname)
-#define space_list_add2(list, list_size, address, length, rollback) _space_list_add2(list, list_size, address, length, NULL, rollback, funcname)
+#define space_list_add2(Vcb, list, list_size, address, length, rollback) _space_list_add2(Vcb, list, list_size, address, length, NULL, rollback, funcname)
 #define space_list_subtract(Vcb, c, deleting, address, length, rollback) _space_list_subtract(Vcb, c, deleting, address, length, rollback, funcname)
-#define space_list_subtract2(list, list_size, address, length, rollback) _space_list_subtract2(list, list_size, address, length, NULL, rollback, funcname)
+#define space_list_subtract2(Vcb, list, list_size, address, length, rollback) _space_list_subtract2(Vcb, list, list_size, address, length, NULL, rollback, funcname)
 
 // in extent-tree.c
 NTSTATUS increase_extent_refcount_data(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 root, UINT64 inode, UINT64 offset, UINT32 refcount, PIRP Irp, LIST_ENTRY* rollback);
 NTSTATUS decrease_extent_refcount_data(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 root, UINT64 inode, UINT64 offset, UINT32 refcount, PIRP Irp, LIST_ENTRY* rollback);
-NTSTATUS decrease_extent_refcount_shared_data(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 treeaddr, UINT64 parent, PIRP Irp, LIST_ENTRY* rollback);
-NTSTATUS decrease_extent_refcount_old(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 treeaddr, PIRP Irp, LIST_ENTRY* rollback);
+NTSTATUS decrease_extent_refcount_tree(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 root, UINT8 level, PIRP Irp, LIST_ENTRY* rollback);
 void decrease_chunk_usage(chunk* c, UINT64 delta);
-NTSTATUS convert_old_data_extent(device_extension* Vcb, UINT64 address, UINT64 size, PIRP Irp, LIST_ENTRY* rollback);
-UINT64 find_extent_data_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 root, UINT64 objid, UINT64 offset, PIRP Irp);
+// NTSTATUS convert_old_data_extent(device_extension* Vcb, UINT64 address, UINT64 size, PIRP Irp, LIST_ENTRY* rollback);
+UINT64 get_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, PIRP Irp);
+BOOL is_extent_unique(device_extension* Vcb, UINT64 address, UINT64 size, PIRP Irp);
+NTSTATUS increase_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT8 type, void* data, KEY* firstitem, UINT8 level, PIRP Irp, LIST_ENTRY* rollback);
+UINT64 get_extent_flags(device_extension* Vcb, UINT64 address, PIRP Irp);
+void update_extent_flags(device_extension* Vcb, UINT64 address, UINT64 flags, PIRP Irp);
+NTSTATUS update_changed_extent_ref(device_extension* Vcb, chunk* c, UINT64 address, UINT64 size, UINT64 root, UINT64 objid, UINT64 offset,
+                                   signed long long count, BOOL no_csum, BOOL superseded, PIRP Irp);
+void add_changed_extent_ref(chunk* c, UINT64 address, UINT64 size, UINT64 root, UINT64 objid, UINT64 offset, UINT32 count, BOOL no_csum);
+UINT64 find_extent_shared_tree_refcount(device_extension* Vcb, UINT64 address, UINT64 parent, PIRP Irp);
+UINT64 find_extent_shared_data_refcount(device_extension* Vcb, UINT64 address, UINT64 parent, PIRP Irp);
+NTSTATUS decrease_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT8 type, void* data, KEY* firstitem,
+                                  UINT8 level, UINT64 parent, PIRP Irp, LIST_ENTRY* rollback);
 
 // in worker-thread.c
-void STDCALL worker_thread(void* context);
 void do_read_job(PIRP Irp);
 void do_write_job(device_extension* Vcb, PIRP Irp);
 
@@ -883,6 +979,16 @@ NTSTATUS registry_load_volume_options(device_extension* Vcb);
 NTSTATUS decompress(UINT8 type, UINT8* inbuf, UINT64 inlen, UINT8* outbuf, UINT64 outlen);
 NTSTATUS write_compressed_bit(fcb* fcb, UINT64 start_data, UINT64 end_data, void* data, BOOL* compressed, LIST_ENTRY* changed_sector_list, PIRP Irp, LIST_ENTRY* rollback);
 
+// in galois.c
+void galois_double(UINT8* data, UINT32 len);
+void galois_divpower(UINT8* data, UINT8 div, UINT32 readlen);
+UINT8 gpow2(UINT8 e);
+UINT8 gmul(UINT8 a, UINT8 b);
+UINT8 gdiv(UINT8 a, UINT8 b);
+
+// in devctrl.c
+NTSTATUS STDCALL drv_device_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
+
 #define fast_io_possible(fcb) (!FsRtlAreThereCurrentFileLocks(&fcb->lock) && !fcb->Vcb->readonly ? FastIoIsPossible : FastIoIsQuestionable)
 
 static __inline void print_open_trees(device_extension* Vcb) {
@@ -897,17 +1003,6 @@ static __inline void print_open_trees(device_extension* Vcb) {
     }
 }
 
-static __inline void InsertAfter(LIST_ENTRY* head, LIST_ENTRY* item, LIST_ENTRY* before) {
-    item->Flink = before->Flink;
-    before->Flink = item;
-    item->Blink = before;
-
-    if (item->Flink != head)
-        item->Flink->Blink = item;
-    else
-        head->Blink = item;
-}
-
 static __inline BOOL write_fcb_compressed(fcb* fcb) {
     // make sure we don't accidentally write the cache inodes or pagefile compressed
     if (fcb->subvol->id == BTRFS_ROOT_ROOT || fcb->Header.Flags2 & FSRTL_FLAG2_IS_PAGING_FILE)
@@ -925,6 +1020,34 @@ static __inline BOOL write_fcb_compressed(fcb* fcb) {
     return FALSE;
 }
 
+static __inline void do_xor(UINT8* buf1, UINT8* buf2, UINT32 len) {
+    UINT32 j;
+#ifndef __REACTOS__
+    __m128i x1, x2;
+#endif
+    
+#ifndef __REACTOS__
+    if (have_sse2 && ((uintptr_t)buf1 & 0xf) == 0 && ((uintptr_t)buf2 & 0xf) == 0) {
+        while (len >= 16) {
+            x1 = _mm_load_si128((__m128i*)buf1);
+            x2 = _mm_load_si128((__m128i*)buf2);
+            x1 = _mm_xor_si128(x1, x2);
+            _mm_store_si128((__m128i*)buf1, x1);
+            
+            buf1 += 16;
+            buf2 += 16;
+            len -= 16;
+        }
+    }
+#endif
+    
+    for (j = 0; j < len; j++) {
+        *buf1 ^= *buf2;
+        buf1++;
+        buf2++;
+    }
+}
+
 #ifdef DEBUG_FCB_REFCOUNTS
 #ifdef DEBUG_LONG_MESSAGES
 #define increase_fileref_refcount(fileref) {\
@@ -1009,6 +1132,18 @@ static __inline BOOL write_fcb_compressed(fcb* fcb) {
 #define S_IXOTH (S_IXGRP >> 3)
 #endif
 
+// LXSS programs can be distinguished by the fact they have a NULL PEB.
+#ifdef _AMD64_
+    static __inline BOOL called_from_lxss() {
+        UINT8* proc = (UINT8*)PsGetCurrentProcess();
+        ULONG_PTR* peb = (ULONG_PTR*)&proc[0x3f8];
+        
+        return !*peb;
+    }
+#else
+#define called_from_lxss() FALSE
+#endif
+
 #if defined(__REACTOS__) && (NTDDI_VERSION < NTDDI_WIN7)
 NTSTATUS WINAPI RtlUnicodeToUTF8N(CHAR *utf8_dest, ULONG utf8_bytes_max,
                                   ULONG *utf8_bytes_written,
index 7b7a00a..67ad41b 100755 (executable)
@@ -444,21 +444,22 @@ static NTSTATUS zlib_write_compressed_bit(fcb* fcb, UINT64 start_data, UINT64 en
     while (le != &fcb->Vcb->chunks) {
         c = CONTAINING_RECORD(le, chunk, list_entry);
         
-        ExAcquireResourceExclusiveLite(&c->lock, TRUE);
-        
-        if (c->chunk_item->type == fcb->Vcb->data_flags && (c->chunk_item->size - c->used) >= comp_length) {
-            if (insert_extent_chunk(fcb->Vcb, fcb, c, start_data, comp_length, FALSE, comp_data, changed_sector_list, Irp, rollback, compression, end_data - start_data)) {
-                ExReleaseResourceLite(&c->lock);
-                ExReleaseResourceLite(&fcb->Vcb->chunk_lock);
-                
-                if (compression != BTRFS_COMPRESSION_NONE)
-                    ExFreePool(comp_data);
-                
-                return STATUS_SUCCESS;
+        if (!c->readonly) {
+            ExAcquireResourceExclusiveLite(&c->lock, TRUE);
+            
+            if (c->chunk_item->type == fcb->Vcb->data_flags && (c->chunk_item->size - c->used) >= comp_length) {
+                if (insert_extent_chunk(fcb->Vcb, fcb, c, start_data, comp_length, FALSE, comp_data, changed_sector_list, Irp, rollback, compression, end_data - start_data)) {
+                    ExReleaseResourceLite(&fcb->Vcb->chunk_lock);
+                    
+                    if (compression != BTRFS_COMPRESSION_NONE)
+                        ExFreePool(comp_data);
+                    
+                    return STATUS_SUCCESS;
+                }
             }
+            
+            ExReleaseResourceLite(&c->lock);
         }
-        
-        ExReleaseResourceLite(&c->lock);
 
         le = le->Flink;
     }
@@ -474,8 +475,6 @@ static NTSTATUS zlib_write_compressed_bit(fcb* fcb, UINT64 start_data, UINT64 en
         
         if (c->chunk_item->type == fcb->Vcb->data_flags && (c->chunk_item->size - c->used) >= comp_length) {
             if (insert_extent_chunk(fcb->Vcb, fcb, c, start_data, comp_length, FALSE, comp_data, changed_sector_list, Irp, rollback, compression, end_data - start_data)) {
-                ExReleaseResourceLite(&c->lock);
-                
                 if (compression != BTRFS_COMPRESSION_NONE)
                     ExFreePool(comp_data);
                 
@@ -830,21 +829,22 @@ static NTSTATUS lzo_write_compressed_bit(fcb* fcb, UINT64 start_data, UINT64 end
     while (le != &fcb->Vcb->chunks) {
         c = CONTAINING_RECORD(le, chunk, list_entry);
         
-        ExAcquireResourceExclusiveLite(&c->lock, TRUE);
-        
-        if (c->chunk_item->type == fcb->Vcb->data_flags && (c->chunk_item->size - c->used) >= comp_length) {
-            if (insert_extent_chunk(fcb->Vcb, fcb, c, start_data, comp_length, FALSE, comp_data, changed_sector_list, Irp, rollback, compression, end_data - start_data)) {
-                ExReleaseResourceLite(&c->lock);
-                ExReleaseResourceLite(&fcb->Vcb->chunk_lock);
-                
-                if (compression != BTRFS_COMPRESSION_NONE)
-                    ExFreePool(comp_data);
-                
-                return STATUS_SUCCESS;
+        if (!c->readonly) {
+            ExAcquireResourceExclusiveLite(&c->lock, TRUE);
+            
+            if (c->chunk_item->type == fcb->Vcb->data_flags && (c->chunk_item->size - c->used) >= comp_length) {
+                if (insert_extent_chunk(fcb->Vcb, fcb, c, start_data, comp_length, FALSE, comp_data, changed_sector_list, Irp, rollback, compression, end_data - start_data)) {
+                    ExReleaseResourceLite(&fcb->Vcb->chunk_lock);
+                    
+                    if (compression != BTRFS_COMPRESSION_NONE)
+                        ExFreePool(comp_data);
+                    
+                    return STATUS_SUCCESS;
+                }
             }
+            
+            ExReleaseResourceLite(&c->lock);
         }
-        
-        ExReleaseResourceLite(&c->lock);
 
         le = le->Flink;
     }
@@ -860,8 +860,6 @@ static NTSTATUS lzo_write_compressed_bit(fcb* fcb, UINT64 start_data, UINT64 end
         
         if (c->chunk_item->type == fcb->Vcb->data_flags && (c->chunk_item->size - c->used) >= comp_length) {
             if (insert_extent_chunk(fcb->Vcb, fcb, c, start_data, comp_length, FALSE, comp_data, changed_sector_list, Irp, rollback, compression, end_data - start_data)) {
-                ExReleaseResourceLite(&c->lock);
-                
                 if (compression != BTRFS_COMPRESSION_NONE)
                     ExFreePool(comp_data);
                 
index 791c459..7390d66 100644 (file)
@@ -75,7 +75,7 @@ static UINT32 crc32c_hw(const void *input, int len, UINT32 crc) {
         crc = _mm_crc32_u8(crc, *buf);
     }
 
-#ifdef __x86_64__
+#ifdef _AMD64_
     CALC_CRC(_mm_crc32_u64, crc, UINT64, buf, len);
 #endif
     CALC_CRC(_mm_crc32_u32, crc, UINT32, buf, len);
index 5576833..f389dfb 100644 (file)
@@ -25,6 +25,8 @@
 
 extern PDEVICE_OBJECT devobj;
 
+static WCHAR datastring[] = L"::$DATA";
+
 static NTSTATUS find_file_dir_index(device_extension* Vcb, root* r, UINT64 inode, UINT64 parinode, PANSI_STRING utf8, UINT64* pindex, PIRP Irp) {
     KEY searchkey;
     traverse_ptr tp;
@@ -41,7 +43,7 @@ static NTSTATUS find_file_dir_index(device_extension* Vcb, root* r, UINT64 inode
         return Status;
     }
     
-    if (!keycmp(&tp.item->key, &searchkey)) {
+    if (!keycmp(tp.item->key, searchkey)) {
         INODE_REF* ir;
         ULONG len;
         
@@ -98,7 +100,7 @@ static NTSTATUS find_file_dir_index_extref(device_extension* Vcb, root* r, UINT6
         return Status;
     }
     
-    if (!keycmp(&tp.item->key, &searchkey)) {
+    if (!keycmp(tp.item->key, searchkey)) {
         INODE_EXTREF* ier;
         ULONG len;
         
@@ -155,7 +157,7 @@ static NTSTATUS find_subvol_dir_index(device_extension* Vcb, root* r, UINT64 sub
         return Status;
     }
     
-    if (keycmp(&tp.item->key, &searchkey)) {
+    if (keycmp(tp.item->key, searchkey)) {
         ERR("couldn't find (%llx,%x,%llx) in root tree\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
         return STATUS_INTERNAL_ERROR;
     }
@@ -197,7 +199,7 @@ static NTSTATUS load_index_list(fcb* fcb, PIRP Irp) {
         return Status;
     }
 
-    if (keycmp(&tp.item->key, &searchkey) == -1) {
+    if (keycmp(tp.item->key, searchkey) == -1) {
         if (find_next_item(fcb->Vcb, &tp, &next_tp, FALSE, Irp)) {
             tp = next_tp;
             
@@ -337,7 +339,8 @@ end:
             ExFreePool(ie);
         }
     } else
-        mark_fcb_dirty(fcb);
+        mark_fcb_dirty(fcb); // It's not necessarily dirty, but this is an easy way of making sure
+                             // the list remains in memory until the next flush.
     
     return Status;
 }
@@ -473,7 +476,8 @@ end:
 }
 
 static NTSTATUS STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING filename, UINT32 crc32, file_ref* fr,
-                                                    root** subvol, UINT64* inode, UINT8* type, UINT64* pindex, PANSI_STRING utf8, PIRP Irp) {
+                                                    root** subvol, UINT64* inode, UINT8* type, UINT64* pindex, PANSI_STRING utf8,
+                                                    BOOL case_sensitive, PIRP Irp) {
     DIR_ITEM* di;
     KEY searchkey;
     traverse_ptr tp;
@@ -495,7 +499,7 @@ static NTSTATUS STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNIC
     
     TRACE("found item %llx,%x,%llx\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
     
-    if (!keycmp(&searchkey, &tp.item->key)) {
+    if (!keycmp(searchkey, tp.item->key)) {
         UINT32 size = tp.item->size;
         
         // found by hash
@@ -538,7 +542,7 @@ static NTSTATUS STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNIC
                         us.Buffer = utf16;
                         us.Length = us.MaximumLength = (USHORT)stringlen;
                         
-                        if (FsRtlAreNamesEqual(filename, &us, TRUE, NULL)) {
+                        if (FsRtlAreNamesEqual(filename, &us, !case_sensitive, NULL)) {
                             UINT64 index;
                             
                             if (di->key.obj_type == TYPE_ROOT_ITEM) {
@@ -627,7 +631,7 @@ static NTSTATUS STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNIC
                                     file_ref* fr2 = CONTAINING_RECORD(le, file_ref, list_entry);
                                     
                                     if (fr2->index == index) {
-                                        if (fr2->deleted || !FsRtlAreNamesEqual(&fr2->filepart, filename, TRUE, NULL)) {
+                                        if (fr2->deleted || !FsRtlAreNamesEqual(&fr2->filepart, filename, !case_sensitive, NULL)) {
                                             goto byindex;
                                         }
                                         break;
@@ -656,6 +660,9 @@ static NTSTATUS STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNIC
     }
     
 byindex:
+    if (case_sensitive)
+        return STATUS_OBJECT_NAME_NOT_FOUND;
+    
     Status = find_file_in_dir_index(fr, filename, subvol, inode, type, pindex, utf8, Irp);
     if (!NT_SUCCESS(Status) && Status != STATUS_OBJECT_NAME_NOT_FOUND) {
         ERR("find_file_in_dir_index returned %08x\n", Status);
@@ -747,7 +754,8 @@ file_ref* create_fileref() {
 }
 
 NTSTATUS STDCALL find_file_in_dir(device_extension* Vcb, PUNICODE_STRING filename, file_ref* fr,
-                                  root** subvol, UINT64* inode, UINT8* type, UINT64* index, PANSI_STRING utf8, PIRP Irp) {
+                                  root** subvol, UINT64* inode, UINT8* type, UINT64* index, PANSI_STRING utf8,
+                                  BOOL case_sensitive, PIRP Irp) {
     char* fn;
     UINT32 crc32;
     ULONG utf8len;
@@ -777,7 +785,7 @@ NTSTATUS STDCALL find_file_in_dir(device_extension* Vcb, PUNICODE_STRING filenam
     crc32 = calc_crc32c(0xfffffffe, (UINT8*)fn, (ULONG)utf8len);
     TRACE("crc32c(%.*s) = %08x\n", utf8len, fn, crc32);
     
-    return find_file_in_dir_with_crc32(Vcb, filename, crc32, fr, subvol, inode, type, index, utf8, Irp);
+    return find_file_in_dir_with_crc32(Vcb, filename, crc32, fr, subvol, inode, type, index, utf8, case_sensitive, Irp);
 }
 
 static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream, PUNICODE_STRING newstreamname, UINT32* hash, PANSI_STRING xattr, PIRP Irp) {
@@ -824,6 +832,11 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
     crc32 = calc_crc32c(0xfffffffe, (UINT8*)utf8, utf8len);
     TRACE("crc32 = %08x\n", crc32);
     
+    if ((crc32 == EA_DOSATTRIB_HASH && utf8len == strlen(EA_DOSATTRIB) && RtlCompareMemory(utf8, EA_DOSATTRIB, utf8len) == utf8len) || 
+        (crc32 == EA_EA_HASH && utf8len == strlen(EA_EA) && RtlCompareMemory(utf8, EA_EA, utf8len) == utf8len)) {
+        return FALSE;
+    }
+    
     searchkey.obj_id = fcb->inode;
     searchkey.obj_type = TYPE_XATTR_ITEM;
     searchkey.offset = crc32;
@@ -834,7 +847,7 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
         goto end;
     }
     
-    if (!keycmp(&tp.item->key, &searchkey)) {
+    if (!keycmp(tp.item->key, searchkey)) {
         if (tp.item->size < sizeof(DIR_ITEM)) {
             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));
         } else {
@@ -1091,7 +1104,7 @@ static NTSTATUS split_path(PUNICODE_STRING path, UNICODE_STRING** parts, ULONG*
 // }
 // #endif
 
-static file_ref* search_fileref_children(file_ref* dir, PUNICODE_STRING name) {
+static file_ref* search_fileref_children(file_ref* dir, PUNICODE_STRING name, BOOL case_sensitive) {
     LIST_ENTRY* le;
     file_ref *c, *deleted = NULL;
     NTSTATUS Status;
@@ -1099,13 +1112,39 @@ static file_ref* search_fileref_children(file_ref* dir, PUNICODE_STRING name) {
 #ifdef DEBUG_FCB_REFCOUNTS
     ULONG rc;
 #endif
+    
+    if (case_sensitive) {
+        le = dir->children.Flink;
+        while (le != &dir->children) {
+            c = CONTAINING_RECORD(le, file_ref, list_entry);
+            
+            if (c->refcount > 0 && c->filepart.Length == name->Length &&
+                RtlCompareMemory(c->filepart.Buffer, name->Buffer, name->Length) == name->Length) {
+                if (c->deleted) {
+                    deleted = c;
+                } else {
+#ifdef DEBUG_FCB_REFCOUNTS
+                    rc = InterlockedIncrement(&c->refcount);
+                    WARN("fileref %p: refcount now %i (%S)\n", c, rc, file_desc_fileref(c));
+#else
+                    InterlockedIncrement(&c->refcount);
+#endif
+                    return c;
+                }
+            }
+            
+            le = le->Flink;
+        }
+        
+        goto end;
+    }
 
     Status = RtlUpcaseUnicodeString(&ucus, name, TRUE);
     if (!NT_SUCCESS(Status)) {
         ERR("RtlUpcaseUnicodeString returned %08x\n", Status);
         return NULL;
     }
-    
+        
     le = dir->children.Flink;
     while (le != &dir->children) {
         c = CONTAINING_RECORD(le, file_ref, list_entry);
@@ -1130,56 +1169,23 @@ static file_ref* search_fileref_children(file_ref* dir, PUNICODE_STRING name) {
         le = le->Flink;
     }
     
+    ExFreePool(ucus.Buffer);
+    
+end:
     if (deleted)
         increase_fileref_refcount(deleted);
     
-    ExFreePool(ucus.Buffer);
-    
     return deleted;
 }
 
-static UINT64 get_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, PIRP Irp) {
-    KEY searchkey;
-    traverse_ptr tp;
-    NTSTATUS Status;
-    EXTENT_ITEM* ei;
-    
-    searchkey.obj_id = address;
-    searchkey.obj_type = TYPE_EXTENT_ITEM;
-    searchkey.offset = size;
-    
-    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        return 0;
-    }
-    
-    if (keycmp(&searchkey, &tp.item->key)) {
-        ERR("couldn't find (%llx,%x,%llx) in extent tree\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
-        return 0;
-    }
-    
-    if (tp.item->size == sizeof(EXTENT_ITEM_V0)) {
-        EXTENT_ITEM_V0* eiv0 = (EXTENT_ITEM_V0*)tp.item->data;
-        
-        return eiv0->refcount;
-    } else if (tp.item->size < sizeof(EXTENT_ITEM)) {
-        ERR("(%llx,%x,%llx) was %x bytes, expected at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type,
-                                                                       tp.item->key.offset, tp.item->size, sizeof(EXTENT_DATA));
-        return 0;
-    }
-    
-    ei = (EXTENT_ITEM*)tp.item->data;
-    
-    return ei->refcount;
-}
-
-NTSTATUS open_fcb(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type, PANSI_STRING utf8, fcb* parent, fcb** pfcb, PIRP Irp) {
+NTSTATUS open_fcb(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type, PANSI_STRING utf8, fcb* parent, fcb** pfcb, POOL_TYPE pooltype, PIRP Irp) {
     KEY searchkey;
     traverse_ptr tp;
     NTSTATUS Status;
     fcb* fcb;
     BOOL b;
+    UINT8* eadata;
+    UINT16 ealen;
     
     if (!IsListEmpty(&subvol->fcbs)) {
         LIST_ENTRY* le = subvol->fcbs.Flink;
@@ -1204,7 +1210,7 @@ NTSTATUS open_fcb(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type,
         }
     }
     
-    fcb = create_fcb(PagedPool);
+    fcb = create_fcb(pooltype);
     if (!fcb) {
         ERR("out of memory\n");
         return STATUS_INSUFFICIENT_RESOURCES;
@@ -1257,13 +1263,50 @@ NTSTATUS open_fcb(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type,
     
     fcb_get_sd(fcb, parent, Irp);
     
-    if (fcb->type == BTRFS_TYPE_DIRECTORY) {
+    if (fcb->type == BTRFS_TYPE_DIRECTORY && fcb->atts & FILE_ATTRIBUTE_REPARSE_POINT) {
         UINT8* xattrdata;
         UINT16 xattrlen;
         
         if (get_xattr(Vcb, subvol, inode, EA_REPARSE, EA_REPARSE_HASH, &xattrdata, &xattrlen, Irp)) {
             fcb->reparse_xattr.Buffer = (char*)xattrdata;
             fcb->reparse_xattr.Length = fcb->reparse_xattr.MaximumLength = xattrlen;
+        } else {
+            fcb->atts &= ~FILE_ATTRIBUTE_REPARSE_POINT;
+            
+            if (!Vcb->readonly && !(subvol->root_item.flags & BTRFS_SUBVOL_READONLY)) {
+                fcb->atts_changed = TRUE;
+                mark_fcb_dirty(fcb);
+            }
+        }
+    }
+    
+    fcb->ealen = 0;
+    
+    if (get_xattr(Vcb, subvol, inode, EA_EA, EA_EA_HASH, &eadata, &ealen, Irp)) {
+        ULONG offset;
+        
+        Status = IoCheckEaBufferValidity((FILE_FULL_EA_INFORMATION*)eadata, ealen, &offset);
+        
+        if (!NT_SUCCESS(Status)) {
+            WARN("IoCheckEaBufferValidity returned %08x (error at offset %u)\n", Status, offset);
+            ExFreePool(eadata);
+        } else {
+            FILE_FULL_EA_INFORMATION* eainfo;
+            fcb->ea_xattr.Buffer = (char*)eadata;
+            fcb->ea_xattr.Length = fcb->ea_xattr.MaximumLength = ealen;
+            
+            fcb->ealen = 4;
+            
+            // calculate ealen
+            eainfo = (FILE_FULL_EA_INFORMATION*)eadata;
+            do {
+                fcb->ealen += 5 + eainfo->EaNameLength + eainfo->EaValueLength;
+                
+                if (eainfo->NextEntryOffset == 0)
+                    break;
+                
+                eainfo = (FILE_FULL_EA_INFORMATION*)(((UINT8*)eainfo) + eainfo->NextEntryOffset);
+            } while (TRUE);
         }
     }
     
@@ -1320,18 +1363,18 @@ NTSTATUS open_fcb(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type,
                     if (ed2->address == 0 && ed2->size == 0) // sparse
                         goto nextitem;
                     
-                    if (ed2->size != 0)
-                        unique = get_extent_refcount(fcb->Vcb, ed2->address, ed2->size, Irp) == 1;
+                    if (ed2->size != 0 && is_tree_unique(Vcb, tp.tree, Irp))
+                        unique = is_extent_unique(Vcb, ed2->address, ed2->size, Irp);
                 }
                 
-                ext = ExAllocatePoolWithTag(PagedPool, sizeof(extent), ALLOC_TAG);
+                ext = ExAllocatePoolWithTag(pooltype, sizeof(extent), ALLOC_TAG);
                 if (!ext) {
                     ERR("out of memory\n");
                     free_fcb(fcb);
                     return STATUS_INSUFFICIENT_RESOURCES;
                 }
                 
-                ext->data = ExAllocatePoolWithTag(PagedPool, tp.item->size, ALLOC_TAG);
+                ext->data = ExAllocatePoolWithTag(pooltype, tp.item->size, ALLOC_TAG);
                 if (!ext->data) {
                     ERR("out of memory\n");
                     ExFreePool(ext);
@@ -1396,7 +1439,7 @@ nextitem:
                     hardlink* hl;
                     ULONG stringlen;
                     
-                    hl = ExAllocatePoolWithTag(PagedPool, sizeof(hardlink), ALLOC_TAG);
+                    hl = ExAllocatePoolWithTag(pooltype, sizeof(hardlink), ALLOC_TAG);
                     if (!hl) {
                         ERR("out of memory\n");
                         free_fcb(fcb);
@@ -1409,7 +1452,7 @@ nextitem:
                     hl->utf8.Length = hl->utf8.MaximumLength = ir->n;
                     
                     if (hl->utf8.Length > 0) {
-                        hl->utf8.Buffer = ExAllocatePoolWithTag(PagedPool, hl->utf8.MaximumLength, ALLOC_TAG);
+                        hl->utf8.Buffer = ExAllocatePoolWithTag(pooltype, hl->utf8.MaximumLength, ALLOC_TAG);
                         RtlCopyMemory(hl->utf8.Buffer, ir->name, ir->n);
                     }
                     
@@ -1426,7 +1469,7 @@ nextitem:
                     if (stringlen == 0)
                         hl->name.Buffer = NULL;
                     else {
-                        hl->name.Buffer = ExAllocatePoolWithTag(PagedPool, hl->name.MaximumLength, ALLOC_TAG);
+                        hl->name.Buffer = ExAllocatePoolWithTag(pooltype, hl->name.MaximumLength, ALLOC_TAG);
                         
                         if (!hl->name.Buffer) {
                             ERR("out of memory\n");
@@ -1461,7 +1504,7 @@ nextitem:
                     hardlink* hl;
                     ULONG stringlen;
                     
-                    hl = ExAllocatePoolWithTag(PagedPool, sizeof(hardlink), ALLOC_TAG);
+                    hl = ExAllocatePoolWithTag(pooltype, sizeof(hardlink), ALLOC_TAG);
                     if (!hl) {
                         ERR("out of memory\n");
                         free_fcb(fcb);
@@ -1474,7 +1517,7 @@ nextitem:
                     hl->utf8.Length = hl->utf8.MaximumLength = ier->n;
                     
                     if (hl->utf8.Length > 0) {
-                        hl->utf8.Buffer = ExAllocatePoolWithTag(PagedPool, hl->utf8.MaximumLength, ALLOC_TAG);
+                        hl->utf8.Buffer = ExAllocatePoolWithTag(pooltype, hl->utf8.MaximumLength, ALLOC_TAG);
                         RtlCopyMemory(hl->utf8.Buffer, ier->name, ier->n);
                     }
                     
@@ -1491,7 +1534,7 @@ nextitem:
                     if (stringlen == 0)
                         hl->name.Buffer = NULL;
                     else {
-                        hl->name.Buffer = ExAllocatePoolWithTag(PagedPool, hl->name.MaximumLength, ALLOC_TAG);
+                        hl->name.Buffer = ExAllocatePoolWithTag(pooltype, hl->name.MaximumLength, ALLOC_TAG);
                         
                         if (!hl->name.Buffer) {
                             ERR("out of memory\n");
@@ -1599,7 +1642,7 @@ NTSTATUS open_fcb_stream(device_extension* Vcb, root* subvol, UINT64 inode, ANSI
         return Status;
     }
     
-    if (keycmp(&tp.item->key, &searchkey)) {
+    if (keycmp(tp.item->key, searchkey)) {
         ERR("error - could not find key for xattr\n");
         free_fcb(fcb);
         return STATUS_INTERNAL_ERROR;
@@ -1664,7 +1707,8 @@ void insert_fileref_child(file_ref* parent, file_ref* child, BOOL do_lock) {
         ExReleaseResourceLite(&parent->nonpaged->children_lock);
 }
 
-NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnus, file_ref* related, BOOL parent, USHORT* unparsed, ULONG* fn_offset, PIRP Irp) {
+NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnus, file_ref* related, BOOL parent, USHORT* unparsed, ULONG* fn_offset,
+                      POOL_TYPE pooltype, BOOL case_sensitive, PIRP Irp) {
     UNICODE_STRING fnus2;
     file_ref *dir, *sf, *sf2;
     ULONG i, num_parts;
@@ -1707,7 +1751,7 @@ NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnu
         }
         
         if (fnus2.Length == sizeof(WCHAR)) {
-            if (Vcb->root_fileref->fcb->open_count == 0) { // don't allow root to be opened on unmounted FS
+            if (Vcb->root_fileref->open_count == 0) { // don't allow root to be opened on unmounted FS
                 ULONG cc;
                 IO_STATUS_BLOCK iosb;
                 
@@ -1737,6 +1781,9 @@ NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnu
     
     if (fnus->Length == 0) {
         num_parts = 0;
+    } else if (fnus->Length == wcslen(datastring) * sizeof(WCHAR) &&
+               RtlCompareMemory(fnus->Buffer, datastring, wcslen(datastring) * sizeof(WCHAR)) == wcslen(datastring) * sizeof(WCHAR)) {
+        num_parts = 0;
     } else {
         Status = split_path(&fnus2, &parts, &num_parts, &has_stream);
         if (!NT_SUCCESS(Status)) {
@@ -1763,12 +1810,10 @@ NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnu
         goto end2;
     }
     
-    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-    
     for (i = 0; i < num_parts; i++) {
         BOOL lastpart = (i == num_parts-1) || (i == num_parts-2 && has_stream);
         
-        sf2 = search_fileref_children(sf, &parts[i]);
+        sf2 = search_fileref_children(sf, &parts[i], case_sensitive);
         
         if (sf2 && sf2->fcb->type != BTRFS_TYPE_DIRECTORY && !lastpart) {
             WARN("passed path including file as subdirectory\n");
@@ -1778,6 +1823,13 @@ NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnu
             goto end;
         }
         
+        if (sf2 && sf2->deleted) {
+            TRACE("element in path has been deleted\n");
+            free_fileref(sf2);
+            Status = lastpart ? STATUS_OBJECT_NAME_NOT_FOUND : STATUS_OBJECT_PATH_NOT_FOUND;
+            goto end;
+        }
+        
         if (!sf2) {
             if (has_stream && i == num_parts - 1) {
                 UNICODE_STRING streamname;
@@ -1858,7 +1910,7 @@ NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnu
                 UINT8 type;
                 ANSI_STRING utf8;
                 
-                Status = find_file_in_dir(Vcb, &parts[i], sf, &subvol, &inode, &type, &index, &utf8, Irp);
+                Status = find_file_in_dir(Vcb, &parts[i], sf, &subvol, &inode, &type, &index, &utf8, case_sensitive, Irp);
                 if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {
                     TRACE("could not find %.*S\n", parts[i].Length / sizeof(WCHAR), parts[i].Buffer);
 
@@ -1871,7 +1923,7 @@ NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnu
                     fcb* fcb;
                     ULONG strlen;
                     
-                    Status = open_fcb(Vcb, subvol, inode, type, &utf8, sf->fcb, &fcb, Irp);
+                    Status = open_fcb(Vcb, subvol, inode, type, &utf8, sf->fcb, &fcb, pooltype, Irp);
                     if (!NT_SUCCESS(Status)) {
                         ERR("open_fcb returned %08x\n", Status);
                         goto end;
@@ -1965,7 +2017,6 @@ NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnu
     
 end:
     free_fileref(sf);
-    ExReleaseResourceLite(&Vcb->fcb_lock);
     
 end2:
     if (parts)
@@ -2021,7 +2072,8 @@ end:
     return Status;
 }
 
-static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_STRING fpus, file_ref* parfileref, ULONG options, file_ref** pfr, LIST_ENTRY* rollback) {
+static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_STRING fpus, file_ref* parfileref, ULONG options,
+                                     FILE_FULL_EA_INFORMATION* ea, ULONG ealen, file_ref** pfr, LIST_ENTRY* rollback) {
     NTSTATUS Status;
     fcb* fcb;
     ULONG utf8len;
@@ -2078,12 +2130,10 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     parfileref->fcb->inode_item.st_mtime = now;
     ExReleaseResourceLite(parfileref->fcb->Header.Resource);
     
+    parfileref->fcb->inode_item_changed = TRUE;
     mark_fcb_dirty(parfileref->fcb);
     
-    if (parfileref->fcb->subvol->lastinode == 0)
-        get_last_inode(Vcb, parfileref->fcb->subvol, Irp);
-    
-    inode = parfileref->fcb->subvol->lastinode + 1;
+    inode = InterlockedIncrement64(&parfileref->fcb->subvol->lastinode);
     
     type = options & FILE_DIRECTORY_FILE ? BTRFS_TYPE_DIRECTORY : BTRFS_TYPE_FILE;
     
@@ -2108,8 +2158,6 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     if (IrpSp->Parameters.Create.FileAttributes == FILE_ATTRIBUTE_NORMAL)
         IrpSp->Parameters.Create.FileAttributes = defda;
     
-    parfileref->fcb->subvol->lastinode++;
-    
     fcb = create_fcb(pool_type);
     if (!fcb) {
         ERR("out of memory\n");
@@ -2119,8 +2167,10 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
 
     fcb->Vcb = Vcb;
 
-    if (IrpSp->Flags & SL_OPEN_PAGING_FILE)
+    if (IrpSp->Flags & SL_OPEN_PAGING_FILE) {
         fcb->Header.Flags2 |= FSRTL_FLAG2_IS_PAGING_FILE;
+        Vcb->disallow_dismount = TRUE;
+    }
 
     fcb->inode_item.generation = Vcb->superblock.generation;
     fcb->inode_item.transid = Vcb->superblock.generation;
@@ -2161,6 +2211,8 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
             fcb->inode_item.flags |= BTRFS_INODE_COMPRESS;
     }
     
+    fcb->inode_item_changed = TRUE;
+    
     fcb->Header.IsFastIoPossible = fast_io_possible(fcb);
     fcb->Header.AllocationSize.QuadPart = 0;
     fcb->Header.FileSize.QuadPart = 0;
@@ -2183,24 +2235,52 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     
     if (!NT_SUCCESS(Status)) {
         ERR("fcb_get_new_sd returned %08x\n", Status);
-        
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
-        
         return Status;
     }
     
     fcb->sd_dirty = TRUE;
     
+    if (ea && ealen > 0) {
+        FILE_FULL_EA_INFORMATION* eainfo;
+        
+        fcb->ealen = 4;
+        
+        // capitalize EA names
+        eainfo = ea;
+        do {
+            STRING s;
+            
+            s.Length = s.MaximumLength = eainfo->EaNameLength;
+            s.Buffer = eainfo->EaName;
+            
+            RtlUpperString(&s, &s);
+            
+            fcb->ealen += 5 + eainfo->EaNameLength + eainfo->EaValueLength;
+            
+            if (eainfo->NextEntryOffset == 0)
+                break;
+            
+            eainfo = (FILE_FULL_EA_INFORMATION*)(((UINT8*)eainfo) + eainfo->NextEntryOffset);
+        } while (TRUE);
+        
+        fcb->ea_xattr.Buffer = ExAllocatePoolWithTag(pool_type, ealen, ALLOC_TAG);
+        if (!fcb->ea_xattr.Buffer) {
+            ERR("out of memory\n");
+            free_fcb(fcb);
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+        
+        fcb->ea_xattr.Length = fcb->ea_xattr.MaximumLength = ealen;
+        RtlCopyMemory(fcb->ea_xattr.Buffer, ea, ealen);
+        
+        fcb->ea_changed = TRUE;
+    }
+    
     hl = ExAllocatePoolWithTag(pool_type, sizeof(hardlink), ALLOC_TAG);
     if (!hl) {
         ERR("out of memory\n");
-        
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
-        
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
@@ -2213,11 +2293,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     if (!hl->utf8.Buffer) {
         ERR("out of memory\n");
         ExFreePool(hl);
-        
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
-        
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     RtlCopyMemory(hl->utf8.Buffer, utf8, utf8len);
@@ -2229,11 +2305,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
         ERR("out of memory\n");
         ExFreePool(hl->utf8.Buffer);
         ExFreePool(hl);
-        
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
-        
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
@@ -2244,11 +2316,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     fileref = create_fileref();
     if (!fileref) {
         ERR("out of memory\n");
-        
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
-        
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
@@ -2267,11 +2335,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
         
         if (!fileref->filepart.Buffer) {
             ERR("out of memory\n");
-            
-            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
             free_fcb(fcb);
-            ExReleaseResource(&Vcb->fcb_lock);
-            
             return STATUS_INSUFFICIENT_RESOURCES;
         }
         
@@ -2281,9 +2345,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     Status = RtlUpcaseUnicodeString(&fileref->filepart_uc, &fileref->filepart, TRUE);
     if (!NT_SUCCESS(Status)) {
         ERR("RtlUpcaseUnicodeString returned %08x\n", Status);
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fileref(fileref);
-        ExReleaseResource(&Vcb->fcb_lock);
         return Status;
     }
         
@@ -2292,9 +2354,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
         
         if (!NT_SUCCESS(Status)) {
             ERR("extend_file returned %08x\n", Status);
-            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
             free_fileref(fileref);
-            ExReleaseResource(&Vcb->fcb_lock);
             return Status;
         }
     }
@@ -2328,10 +2388,12 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
 }
 
 static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_ref** pparfileref, PUNICODE_STRING fpus, PUNICODE_STRING stream,
-                              PIRP Irp, ULONG options, POOL_TYPE pool_type, LIST_ENTRY* rollback) {
+                              PIRP Irp, ULONG options, POOL_TYPE pool_type, BOOL case_sensitive, LIST_ENTRY* rollback) {
     file_ref *fileref, *newpar, *parfileref;
     fcb* fcb;
     static char xapref[] = "user.";
+    static WCHAR DOSATTRIB[] = L"DOSATTRIB";
+    static WCHAR EA[] = L"EA";
     ULONG xapreflen = strlen(xapref), overhead;
     LARGE_INTEGER time;
     BTRFS_TIME now;
@@ -2348,9 +2410,7 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     
     parfileref = *pparfileref;
     
-    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-    Status = open_fileref(Vcb, &newpar, fpus, parfileref, FALSE, NULL, NULL, Irp);
-    ExReleaseResource(&Vcb->fcb_lock);
+    Status = open_fileref(Vcb, &newpar, fpus, parfileref, FALSE, NULL, NULL, PagedPool, case_sensitive, Irp);
     
     if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {
         UNICODE_STRING fpus2;
@@ -2368,7 +2428,7 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
         
         RtlCopyMemory(fpus2.Buffer, fpus->Buffer, fpus2.Length);
         
-        Status = file_create2(Irp, Vcb, &fpus2, parfileref, options, &newpar, rollback);
+        Status = file_create2(Irp, Vcb, &fpus2, parfileref, options, NULL, 0, &newpar, rollback);
     
         if (!NT_SUCCESS(Status)) {
             ERR("file_create2 returned %08x\n", Status);
@@ -2383,15 +2443,13 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
         return Status;
     }
     
-    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
     free_fileref(parfileref);
-    ExReleaseResource(&Vcb->fcb_lock);
     
     parfileref = newpar;
     *pparfileref = parfileref;
     
-    if (parfileref->fcb->type != BTRFS_TYPE_FILE && parfileref->fcb->type != BTRFS_TYPE_SYMLINK) {
-        WARN("parent not file or symlink\n");
+    if (parfileref->fcb->type != BTRFS_TYPE_FILE && parfileref->fcb->type != BTRFS_TYPE_SYMLINK && parfileref->fcb->type != BTRFS_TYPE_DIRECTORY) {
+        WARN("parent not file, directory, or symlink\n");
         return STATUS_INVALID_PARAMETER;
     }
     
@@ -2399,6 +2457,11 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
         WARN("tried to create directory as stream\n");
         return STATUS_INVALID_PARAMETER;
     }
+    
+    if ((stream->Length == wcslen(DOSATTRIB) * sizeof(WCHAR) && RtlCompareMemory(stream->Buffer, DOSATTRIB, stream->Length) == stream->Length) || 
+        (stream->Length == wcslen(EA) * sizeof(WCHAR) && RtlCompareMemory(stream->Buffer, EA, stream->Length) == stream->Length)) {
+        return STATUS_OBJECT_NAME_INVALID;
+    }
         
     fcb = create_fcb(pool_type);
     if (!fcb) {
@@ -2428,9 +2491,7 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     Status = RtlUnicodeToUTF8N(NULL, 0, &utf8len, stream->Buffer, stream->Length);
     if (!NT_SUCCESS(Status)) {
         ERR("RtlUnicodeToUTF8N 1 returned %08x\n", Status);
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
         return Status;
     }
     
@@ -2439,9 +2500,7 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     fcb->adsxattr.Buffer = ExAllocatePoolWithTag(pool_type, fcb->adsxattr.MaximumLength, ALLOC_TAG);
     if (!fcb->adsxattr.Buffer) {
         ERR("out of memory\n");
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
@@ -2450,9 +2509,7 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     Status = RtlUnicodeToUTF8N(&fcb->adsxattr.Buffer[xapreflen], utf8len, &utf8len, stream->Buffer, stream->Length);
     if (!NT_SUCCESS(Status)) {
         ERR("RtlUnicodeToUTF8N 2 returned %08x\n", Status);
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
         return Status;
     }
     
@@ -2470,13 +2527,11 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     Status = find_item(Vcb, parfileref->fcb->subvol, &tp, &searchkey, FALSE, Irp);
     if (!NT_SUCCESS(Status)) {
         ERR("find_item returned %08x\n", Status);
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
         return Status;
     }
     
-    if (!keycmp(&tp.item->key, &searchkey))
+    if (!keycmp(tp.item->key, searchkey))
         overhead = tp.item->size;
     else
         overhead = 0;
@@ -2485,9 +2540,7 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     
     if (utf8len + xapreflen + overhead > fcb->adsmaxlen) {
         WARN("not enough room for new DIR_ITEM (%u + %u > %u)", utf8len + xapreflen, overhead, fcb->adsmaxlen);
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
         return STATUS_DISK_FULL;
     } else
         fcb->adsmaxlen -= overhead + utf8len + xapreflen;
@@ -2495,9 +2548,7 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     fileref = create_fileref();
     if (!fileref) {
         ERR("out of memory\n");
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fcb(fcb);
-        ExReleaseResource(&Vcb->fcb_lock);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
@@ -2507,9 +2558,7 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     fileref->filepart.Buffer = ExAllocatePoolWithTag(pool_type, fileref->filepart.MaximumLength, ALLOC_TAG);
     if (!fileref->filepart.Buffer) {
         ERR("out of memory\n");
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fileref(fileref);
-        ExReleaseResource(&Vcb->fcb_lock);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
@@ -2518,19 +2567,15 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     Status = RtlUpcaseUnicodeString(&fileref->filepart_uc, &fileref->filepart, TRUE);
     if (!NT_SUCCESS(Status)) {
         ERR("RtlUpcaseUnicodeString returned %08x\n", Status);
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fileref(fileref);
-        ExReleaseResource(&Vcb->fcb_lock);
         return Status;
     }
     
     mark_fcb_dirty(fcb);
     mark_fileref_dirty(fileref);
     
-    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
     InsertTailList(&fcb->subvol->fcbs, &fcb->list_entry);
     InsertTailList(&Vcb->all_fcbs, &fcb->list_entry_all);
-    ExReleaseResource(&Vcb->fcb_lock);
     
     KeQuerySystemTime(&time);
     win_time_to_unix(time, &now);
@@ -2538,6 +2583,7 @@ static NTSTATUS create_stream(device_extension* Vcb, file_ref** pfileref, file_r
     parfileref->fcb->inode_item.transid = Vcb->superblock.generation;
     parfileref->fcb->inode_item.sequence++;
     parfileref->fcb->inode_item.st_ctime = now;
+    parfileref->fcb->inode_item_changed = TRUE;
     
     mark_fcb_dirty(parfileref->fcb);
     
@@ -2587,9 +2633,7 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
     } else
         related = NULL;
     
-    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-    Status = open_fileref(Vcb, &parfileref, &FileObject->FileName, related, TRUE, NULL, NULL, Irp);
-    ExReleaseResource(&Vcb->fcb_lock);
+    Status = open_fileref(Vcb, &parfileref, &FileObject->FileName, related, TRUE, NULL, NULL, pool_type, IrpSp->Flags & SL_CASE_SENSITIVE, Irp);
     
     if (!NT_SUCCESS(Status))
         goto end;
@@ -2659,7 +2703,7 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
     }
     
     if (stream.Length > 0) {
-        Status = create_stream(Vcb, &fileref, &parfileref, &fpus, &stream, Irp, options, pool_type, rollback);
+        Status = create_stream(Vcb, &fileref, &parfileref, &fpus, &stream, Irp, options, pool_type, IrpSp->Flags & SL_CASE_SENSITIVE, rollback);
         if (!NT_SUCCESS(Status)) {
             ERR("create_stream returned %08x\n", Status);
             goto end;
@@ -2670,7 +2714,18 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
             goto end;
         }
         
-        Status = file_create2(Irp, Vcb, &fpus, parfileref, options, &fileref, rollback);
+        if (Irp->AssociatedIrp.SystemBuffer && IrpSp->Parameters.Create.EaLength > 0) {
+            ULONG offset;
+            
+            Status = IoCheckEaBufferValidity(Irp->AssociatedIrp.SystemBuffer, IrpSp->Parameters.Create.EaLength, &offset);
+            if (!NT_SUCCESS(Status)) {
+                ERR("IoCheckEaBufferValidity returned %08x (error at offset %u)\n", Status, offset);
+                goto end;
+            }
+        }
+        
+        Status = file_create2(Irp, Vcb, &fpus, parfileref, options, Irp->AssociatedIrp.SystemBuffer, IrpSp->Parameters.Create.EaLength,
+                              &fileref, rollback);
         
         if (!NT_SUCCESS(Status)) {
             ERR("file_create2 returned %08x\n", Status);
@@ -2687,9 +2742,7 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
     if (!ccb) {
         ERR("out of memory\n");
         Status = STATUS_INSUFFICIENT_RESOURCES;
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fileref(fileref);
-        ExReleaseResource(&Vcb->fcb_lock);
         goto end;
     }
     
@@ -2706,13 +2759,15 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
     ccb->has_wildcard = FALSE;
     ccb->specific_file = FALSE;
     ccb->access = access_state->OriginalDesiredAccess;
+    ccb->case_sensitive = IrpSp->Flags & SL_CASE_SENSITIVE;
     
 #ifdef DEBUG_FCB_REFCOUNTS
-    oc = InterlockedIncrement(&fileref->fcb->open_count);
-    ERR("fcb %p: open_count now %i\n", fileref->fcb, oc);
+    oc = InterlockedIncrement(&fileref->open_count);
+    ERR("fileref %p: open_count now %i\n", fileref, oc);
 #else
-    InterlockedIncrement(&fileref->fcb->open_count);
+    InterlockedIncrement(&fileref->open_count);
 #endif
+    InterlockedIncrement(&Vcb->open_files);
     
     FileObject->FsContext2 = ccb;
     
@@ -2756,11 +2811,8 @@ end:
         ExFreePool(fpus.Buffer);
     
 end2:
-    if (parfileref) {
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+    if (parfileref)
         free_fileref(parfileref);
-        ExReleaseResource(&Vcb->fcb_lock);
-    }
     
     return Status;
 }
@@ -3019,6 +3071,8 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
     USHORT unparsed;
     ULONG fn_offset = 0;
     file_ref *related, *fileref;
+    POOL_TYPE pool_type = Stack->Flags & SL_OPEN_PAGING_FILE ? NonPagedPool : PagedPool;
+    ACCESS_MASK granted_access;
 #ifdef DEBUG_FCB_REFCOUNTS
     LONG oc;
 #endif
@@ -3031,7 +3085,7 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
     if (options & FILE_DIRECTORY_FILE && RequestedDisposition == FILE_SUPERSEDE) {
         WARN("error - supersede requested with FILE_DIRECTORY_FILE\n");
         Status = STATUS_INVALID_PARAMETER;
-        goto exit;
+        goto exit2;
     }
 
     FileObject = Stack->FileObject;
@@ -3081,14 +3135,16 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
     
     if (Vcb->readonly && (RequestedDisposition == FILE_SUPERSEDE || RequestedDisposition == FILE_CREATE || RequestedDisposition == FILE_OVERWRITE)) {
         Status = STATUS_MEDIA_WRITE_PROTECTED;
-        goto exit;
+        goto exit2;
     }
     
     if (Vcb->readonly && Stack->Parameters.Create.SecurityContext->DesiredAccess &
         (FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | DELETE | WRITE_OWNER | WRITE_DAC)) {
         Status = STATUS_MEDIA_WRITE_PROTECTED;
-        goto exit;
+        goto exit2;
     }
+    
+    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
 
     if (options & FILE_OPEN_BY_FILE_ID) {
         if (FileObject->FileName.Length == sizeof(UINT64) && related && RequestedDisposition == FILE_OPEN) {
@@ -3104,9 +3160,7 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
                 increase_fileref_refcount(fileref);
                 Status = STATUS_SUCCESS;
             } else {
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 Status = open_fileref_by_inode(Vcb, related->fcb->subvol, inode, &fileref, Irp);
-                ExReleaseResource(&Vcb->fcb_lock);
             }
         } else {
             WARN("FILE_OPEN_BY_FILE_ID only supported for inodes\n");
@@ -3119,9 +3173,8 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
             goto exit;
         }
         
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-        Status = open_fileref(Vcb, &fileref, &FileObject->FileName, related, Stack->Flags & SL_OPEN_TARGET_DIRECTORY, &unparsed, &fn_offset, Irp);
-        ExReleaseResource(&Vcb->fcb_lock);
+        Status = open_fileref(Vcb, &fileref, &FileObject->FileName, related, Stack->Flags & SL_OPEN_TARGET_DIRECTORY, &unparsed, &fn_offset,
+                              pool_type, Stack->Flags & SL_CASE_SENSITIVE, Irp);
     }
     
     if (Status == STATUS_REPARSE) {
@@ -3134,9 +3187,7 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
         if (!NT_SUCCESS(Status)) {
             ERR("get_reparse_block returned %08x\n", Status);
             
-            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
             free_fileref(fileref);
-            ExReleaseResourceLite(&Vcb->fcb_lock);
             goto exit;
         }
         
@@ -3147,16 +3198,12 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
         
         Irp->Tail.Overlay.AuxiliaryBuffer = (void*)data;
         
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fileref(fileref);
-        ExReleaseResourceLite(&Vcb->fcb_lock);
         goto exit;
     }
     
     if (NT_SUCCESS(Status) && fileref->deleted) {
-        ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
         free_fileref(fileref);
-        ExReleaseResourceLite(&Vcb->fcb_lock);
         
         Status = STATUS_OBJECT_NAME_NOT_FOUND;
     }
@@ -3165,9 +3212,7 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
         if (RequestedDisposition == FILE_CREATE) {
             TRACE("file %S already exists, returning STATUS_OBJECT_NAME_COLLISION\n", file_desc_fileref(fileref));
             Status = STATUS_OBJECT_NAME_COLLISION;
-            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
             free_fileref(fileref);
-            ExReleaseResource(&Vcb->fcb_lock);
             goto exit;
         }
     } else if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {
@@ -3189,27 +3234,35 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
         if (RequestedDisposition == FILE_SUPERSEDE || RequestedDisposition == FILE_OVERWRITE || RequestedDisposition == FILE_OVERWRITE_IF) {
             if (fileref->fcb->type == BTRFS_TYPE_DIRECTORY || fileref->fcb->subvol->root_item.flags & BTRFS_SUBVOL_READONLY) {
                 Status = STATUS_ACCESS_DENIED;
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResource(&Vcb->fcb_lock);
                 goto exit;
             }
             
             if (Vcb->readonly) {
                 Status = STATUS_MEDIA_WRITE_PROTECTED;
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResource(&Vcb->fcb_lock);
                 goto exit;
             }
         }
         
-        if (fileref->fcb->subvol->root_item.flags & BTRFS_SUBVOL_READONLY && Stack->Parameters.Create.SecurityContext->DesiredAccess &
+        SeLockSubjectContext(&Stack->Parameters.Create.SecurityContext->AccessState->SubjectSecurityContext);
+        
+        if (!SeAccessCheck(fileref->fcb->ads ? fileref->parent->fcb->sd : fileref->fcb->sd,
+                           &Stack->Parameters.Create.SecurityContext->AccessState->SubjectSecurityContext,
+                           FALSE, Stack->Parameters.Create.SecurityContext->DesiredAccess, 0, NULL,
+                           IoGetFileObjectGenericMapping(), Stack->Flags & SL_FORCE_ACCESS_CHECK ? UserMode : Irp->RequestorMode,
+                           &granted_access, &Status)) {
+            SeUnlockSubjectContext(&Stack->Parameters.Create.SecurityContext->AccessState->SubjectSecurityContext);
+            WARN("SeAccessCheck failed, returning %08x\n", Status);
+            goto exit;
+        }
+        
+        SeUnlockSubjectContext(&Stack->Parameters.Create.SecurityContext->AccessState->SubjectSecurityContext);
+        
+        if (fileref->fcb->subvol->root_item.flags & BTRFS_SUBVOL_READONLY && granted_access &
             (FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA | FILE_WRITE_ATTRIBUTES | DELETE | WRITE_OWNER | WRITE_DAC)) {
             Status = STATUS_ACCESS_DENIED;
-            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
             free_fileref(fileref);
-            ExReleaseResource(&Vcb->fcb_lock);
             goto exit;
         }
         
@@ -3221,14 +3274,12 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
                 WARN("could not open as deletion pending\n");
                 Status = STATUS_DELETE_PENDING;
                 
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResourceLite(&Vcb->fcb_lock);
                 goto exit;
             }
             sf = sf->parent;
         }
-        
+
         if (fileref->fcb->atts & FILE_ATTRIBUTE_READONLY) {
             ACCESS_MASK allowed = DELETE | READ_CONTROL | WRITE_OWNER | WRITE_DAC |
                                     SYNCHRONIZE | ACCESS_SYSTEM_SECURITY | FILE_READ_DATA |
@@ -3239,11 +3290,9 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
             if (fileref->fcb->type == BTRFS_TYPE_DIRECTORY)
                 allowed |= FILE_ADD_SUBDIRECTORY | FILE_ADD_FILE | FILE_DELETE_CHILD;
             
-            if (Stack->Parameters.Create.SecurityContext->DesiredAccess & ~allowed) {
+            if (granted_access & ~allowed) {
                 Status = STATUS_ACCESS_DENIED;
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResource(&Vcb->fcb_lock);
                 goto exit;
             }
         }
@@ -3251,9 +3300,7 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
         if (options & FILE_DELETE_ON_CLOSE && (fileref == Vcb->root_fileref || Vcb->readonly ||
             fileref->fcb->subvol->root_item.flags & BTRFS_SUBVOL_READONLY || fileref->fcb->atts & FILE_ATTRIBUTE_READONLY)) {
             Status = STATUS_CANNOT_DELETE;
-            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
             free_fileref(fileref);
-            ExReleaseResource(&Vcb->fcb_lock);
             goto exit;
         }
         
@@ -3270,10 +3317,7 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
             Status = get_reparse_block(fileref->fcb, (UINT8**)&data);
             if (!NT_SUCCESS(Status)) {
                 ERR("get_reparse_block returned %08x\n", Status);
-                
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResourceLite(&Vcb->fcb_lock);
                 goto exit;
             }
             
@@ -3285,56 +3329,41 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
             
             Irp->Tail.Overlay.AuxiliaryBuffer = (void*)data;
             
-            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
             free_fileref(fileref);
-            ExReleaseResourceLite(&Vcb->fcb_lock);
             goto exit;
         }
         
-        if (fileref->fcb->type == BTRFS_TYPE_DIRECTORY) {
+        if (fileref->fcb->type == BTRFS_TYPE_DIRECTORY && !fileref->fcb->ads) {
             if (options & FILE_NON_DIRECTORY_FILE && !(fileref->fcb->atts & FILE_ATTRIBUTE_REPARSE_POINT)) {
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResourceLite(&Vcb->fcb_lock);
-                
                 Status = STATUS_FILE_IS_A_DIRECTORY;
                 goto exit;
             }
         } else if (options & FILE_DIRECTORY_FILE) {
             TRACE("returning STATUS_NOT_A_DIRECTORY (type = %u, %S)\n", fileref->fcb->type, file_desc_fileref(fileref));
-            
-            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
             free_fileref(fileref);
-            ExReleaseResourceLite(&Vcb->fcb_lock);
-            
             Status = STATUS_NOT_A_DIRECTORY;
             goto exit;
         }
         
-        if (fileref->fcb->open_count > 0) {
-            Status = IoCheckShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
-                                        Stack->Parameters.Create.ShareAccess, FileObject, &fileref->fcb->share_access, TRUE);
+        if (fileref->open_count > 0) {
+            Status = IoCheckShareAccess(granted_access, Stack->Parameters.Create.ShareAccess, FileObject, &fileref->fcb->share_access, TRUE);
             
             if (!NT_SUCCESS(Status)) {
                 WARN("IoCheckShareAccess failed, returning %08x\n", Status);
                 
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResourceLite(&Vcb->fcb_lock);
                 goto exit;
             }
         } else {
-            IoSetShareAccess(Stack->Parameters.Create.SecurityContext->DesiredAccess,
-                             Stack->Parameters.Create.ShareAccess, FileObject, &fileref->fcb->share_access);
+            IoSetShareAccess(granted_access, Stack->Parameters.Create.ShareAccess, FileObject, &fileref->fcb->share_access);
         }
 
-        if (Stack->Parameters.Create.SecurityContext->DesiredAccess & FILE_WRITE_DATA || options & FILE_DELETE_ON_CLOSE) {
+        if (granted_access & FILE_WRITE_DATA || options & FILE_DELETE_ON_CLOSE) {
             if (!MmFlushImageSection(&fileref->fcb->nonpaged->segment_object, MmFlushForWrite)) {
                 Status = (options & FILE_DELETE_ON_CLOSE) ? STATUS_CANNOT_DELETE : STATUS_SHARING_VIOLATION;
                 
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResourceLite(&Vcb->fcb_lock);
                 goto exit;
             }
         }
@@ -3347,9 +3376,7 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
             if ((RequestedDisposition == FILE_OVERWRITE || RequestedDisposition == FILE_OVERWRITE_IF) && fileref->fcb->atts & FILE_ATTRIBUTE_READONLY) {
                 WARN("cannot overwrite readonly file\n");
                 Status = STATUS_ACCESS_DENIED;
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResource(&Vcb->fcb_lock);
                 goto exit;
             }
     
@@ -3365,9 +3392,7 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
             Status = truncate_file(fileref->fcb, 0, Irp, rollback);
             if (!NT_SUCCESS(Status)) {
                 ERR("truncate_file returned %08x\n", Status);
-                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                 free_fileref(fileref);
-                ExReleaseResource(&Vcb->fcb_lock);
                 goto exit;
             }
             
@@ -3376,13 +3401,67 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
                 
                 if (!NT_SUCCESS(Status)) {
                     ERR("extend_file returned %08x\n", Status);
-                    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
                     free_fileref(fileref);
-                    ExReleaseResource(&Vcb->fcb_lock);
                     goto exit;
                 }
             }
             
+            if (Irp->AssociatedIrp.SystemBuffer && Stack->Parameters.Create.EaLength > 0) {
+                ULONG offset;
+                FILE_FULL_EA_INFORMATION* eainfo;
+                
+                Status = IoCheckEaBufferValidity(Irp->AssociatedIrp.SystemBuffer, Stack->Parameters.Create.EaLength, &offset);
+                if (!NT_SUCCESS(Status)) {
+                    ERR("IoCheckEaBufferValidity returned %08x (error at offset %u)\n", Status, offset);
+                    free_fileref(fileref);
+                    goto exit;
+                }
+                
+                fileref->fcb->ealen = 4;
+                
+                // capitalize EA name
+                eainfo = Irp->AssociatedIrp.SystemBuffer;
+                do {
+                    STRING s;
+                    
+                    s.Length = s.MaximumLength = eainfo->EaNameLength;
+                    s.Buffer = eainfo->EaName;
+                    
+                    RtlUpperString(&s, &s);
+                    
+                    fileref->fcb->ealen += 5 + eainfo->EaNameLength + eainfo->EaValueLength;
+                    
+                    if (eainfo->NextEntryOffset == 0)
+                        break;
+                    
+                    eainfo = (FILE_FULL_EA_INFORMATION*)(((UINT8*)eainfo) + eainfo->NextEntryOffset);
+                } while (TRUE);
+                
+                if (fileref->fcb->ea_xattr.Buffer)
+                    ExFreePool(fileref->fcb->ea_xattr.Buffer);
+                
+                fileref->fcb->ea_xattr.Buffer = ExAllocatePoolWithTag(pool_type, Stack->Parameters.Create.EaLength, ALLOC_TAG);
+                if (!fileref->fcb->ea_xattr.Buffer) {
+                    ERR("out of memory\n");
+                    Status = STATUS_INSUFFICIENT_RESOURCES;
+                    
+                    free_fileref(fileref);
+                    goto exit;
+                }
+                
+                fileref->fcb->ea_xattr.Length = fileref->fcb->ea_xattr.MaximumLength = Stack->Parameters.Create.EaLength;
+                RtlCopyMemory(fileref->fcb->ea_xattr.Buffer, Irp->AssociatedIrp.SystemBuffer, Stack->Parameters.Create.EaLength);
+            } else {
+                if (fileref->fcb->ea_xattr.Length > 0) {
+                    ExFreePool(fileref->fcb->ea_xattr.Buffer);
+                    fileref->fcb->ea_xattr.Buffer = NULL;
+                    fileref->fcb->ea_xattr.Length = fileref->fcb->ea_xattr.MaximumLength = 0;
+                    
+                    fileref->fcb->ea_changed = TRUE;
+                    fileref->fcb->ealen = 0;
+                }
+            }
+            
             filter = FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE;
             
             mark_fcb_dirty(fileref->fcb);
@@ -3410,11 +3489,30 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
             fileref->fcb->inode_item.sequence++;
             fileref->fcb->inode_item.st_ctime = now;
             fileref->fcb->inode_item.st_mtime = now;
+            fileref->fcb->inode_item_changed = TRUE;
 
             // FIXME - truncate streams
             // FIXME - do we need to alter parent directory's times?
             
             send_notification_fcb(fileref, filter, FILE_ACTION_MODIFIED);
+        } else {
+            if (options & FILE_NO_EA_KNOWLEDGE && fileref->fcb->ea_xattr.Length > 0) {
+                FILE_FULL_EA_INFORMATION* ffei = (FILE_FULL_EA_INFORMATION*)fileref->fcb->ea_xattr.Buffer;
+                
+                do {
+                    if (ffei->Flags & FILE_NEED_EA) {
+                        WARN("returning STATUS_ACCESS_DENIED as no EA knowledge\n");
+                        free_fileref(fileref);
+                        Status = STATUS_ACCESS_DENIED;
+                        goto exit;
+                    }
+                    
+                    if (ffei->NextEntryOffset == 0)
+                        break;
+                    
+                    ffei = (FILE_FULL_EA_INFORMATION*)(((UINT8*)ffei) + ffei->NextEntryOffset);
+                } while (TRUE);
+            }
         }
     
         FileObject->FsContext = fileref->fcb;
@@ -3422,11 +3520,7 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
         ccb = ExAllocatePoolWithTag(NonPagedPool, sizeof(*ccb), ALLOC_TAG);
         if (!ccb) {
             ERR("out of memory\n");
-            
-            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
             free_fileref(fileref);
-            ExReleaseResourceLite(&Vcb->fcb_lock);
-
             Status = STATUS_INSUFFICIENT_RESOURCES;
             goto exit;
         }
@@ -3441,7 +3535,8 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
         RtlInitUnicodeString(&ccb->query_string, NULL);
         ccb->has_wildcard = FALSE;
         ccb->specific_file = FALSE;
-        ccb->access = Stack->Parameters.Create.SecurityContext->DesiredAccess;
+        ccb->access = granted_access;
+        ccb->case_sensitive = Stack->Flags & SL_CASE_SENSITIVE;
         
         ccb->fileref = fileref;
         
@@ -3472,12 +3567,45 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
             }
         }
         
+        // Make sure paging files don't have any extents marked as being prealloc,
+        // as this would mean we'd have to lock exclusively when writing.
+        if (Stack->Flags & SL_OPEN_PAGING_FILE) {
+            LIST_ENTRY* le;
+            BOOL changed = FALSE;
+            
+            ExAcquireResourceExclusiveLite(fileref->fcb->Header.Resource, TRUE);
+            
+            le = fileref->fcb->extents.Flink;
+            
+            while (le != &fileref->fcb->extents) {
+                extent* ext = CONTAINING_RECORD(le, extent, list_entry);
+                
+                if (ext->data->type == EXTENT_TYPE_PREALLOC) {
+                    ext->data->type = EXTENT_TYPE_REGULAR;
+                    changed = TRUE;
+                }
+                
+                le = le->Flink;
+            }
+            
+            ExReleaseResourceLite(fileref->fcb->Header.Resource);
+            
+            if (changed) {
+                fileref->fcb->extents_changed = TRUE;
+                mark_fcb_dirty(fileref->fcb);
+            }
+            
+            fileref->fcb->Header.Flags2 |= FSRTL_FLAG2_IS_PAGING_FILE;
+            Vcb->disallow_dismount = TRUE;
+        }
+        
 #ifdef DEBUG_FCB_REFCOUNTS
-        oc = InterlockedIncrement(&fileref->fcb->open_count);
-        ERR("fcb %p: open_count now %i\n", fileref->fcb, oc);
+        oc = InterlockedIncrement(&fileref->open_count);
+        ERR("fileref %p: open_count now %i\n", fileref, oc);
 #else
-        InterlockedIncrement(&fileref->fcb->open_count);
+        InterlockedIncrement(&fileref->open_count);
 #endif
+        InterlockedIncrement(&Vcb->open_files);
     } else {
         Status = file_create(Irp, DeviceObject->DeviceExtension, FileObject, &FileObject->FileName, RequestedDisposition, options, rollback);
         Irp->IoStatus.Information = NT_SUCCESS(Status) ? FILE_CREATED : 0;
@@ -3487,6 +3615,9 @@ static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_EN
         FileObject->Flags |= FO_CACHE_SUPPORTED;
     
 exit:
+    ExReleaseResourceLite(&Vcb->fcb_lock);
+    
+exit2:
     if (NT_SUCCESS(Status)) {
         if (!FileObject->Vpb)
             FileObject->Vpb = DeviceObject->Vpb;
@@ -3501,7 +3632,7 @@ exit:
 NTSTATUS verify_vcb(device_extension* Vcb, PIRP Irp) {
     UINT64 i;
     
-    for (i = 0; i < Vcb->superblock.num_devices; i++) {
+    for (i = 0; i < Vcb->devices_loaded; i++) {
         if (Vcb->devices[i].removable) {
             NTSTATUS Status;
             ULONG cc;
@@ -3570,8 +3701,6 @@ NTSTATUS STDCALL drv_create(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
         goto exit;
     }
     
-    Vcb = DeviceObject->DeviceExtension;
-    
     Status = verify_vcb(Vcb, Irp);
     if (!NT_SUCCESS(Status)) {
         ERR("verify_vcb returned %08x\n", Status);
@@ -3646,15 +3775,17 @@ NTSTATUS STDCALL drv_create(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
             Status = STATUS_NOT_A_DIRECTORY;
             goto exit;
         }
+        
+        if (Vcb->removing) {
+            Status = STATUS_ACCESS_DENIED;
+            goto exit;
+        }
 
 #ifdef DEBUG_FCB_REFCOUNTS
         rc = InterlockedIncrement(&Vcb->volume_fcb->refcount);
-        oc = InterlockedIncrement(&Vcb->volume_fcb->open_count);
         WARN("fcb %p: refcount now %i (volume)\n", Vcb->volume_fcb, rc);
-        WARN("fcb %p: open_count now %i (volume)\n", Vcb->volume_fcb, oc);
 #else
         InterlockedIncrement(&Vcb->volume_fcb->refcount);
-        InterlockedIncrement(&Vcb->volume_fcb->open_count);
 #endif
         IrpSp->FileObject->FsContext = Vcb->volume_fcb;
         
@@ -3662,6 +3793,8 @@ NTSTATUS STDCALL drv_create(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
 
         if (!IrpSp->FileObject->Vpb)
             IrpSp->FileObject->Vpb = DeviceObject->Vpb;
+        
+        InterlockedIncrement(&Vcb->open_files);
 
         Irp->IoStatus.Information = FILE_OPENED;
         Status = STATUS_SUCCESS;
@@ -3688,7 +3821,7 @@ NTSTATUS STDCALL drv_create(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
         if (!NT_SUCCESS(Status))
             do_rollback(Vcb, &rollback);
         else
-            clear_rollback(&rollback);
+            clear_rollback(Vcb, &rollback);
         
         if (!skip_lock)
             ExReleaseResourceLite(&Vcb->tree_lock);
diff --git a/reactos/drivers/filesystems/btrfs/devctrl.c b/reactos/drivers/filesystems/btrfs/devctrl.c
new file mode 100644 (file)
index 0000000..11422e4
--- /dev/null
@@ -0,0 +1,210 @@
+/* Copyright (c) Mark Harmstone 2016
+ * 
+ * This file is part of WinBtrfs.
+ * 
+ * WinBtrfs is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public Licence as published by
+ * the Free Software Foundation, either version 3 of the Licence, or
+ * (at your option) any later version.
+ * 
+ * WinBtrfs is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public Licence for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public Licence
+ * along with WinBtrfs.  If not, see <http://www.gnu.org/licenses/>. */
+
+#include "btrfs_drv.h"
+#ifndef __REACTOS__
+#include <winioctl.h>
+#endif
+#include <mountdev.h>
+#include <initguid.h>
+#include <diskguid.h>
+
+static NTSTATUS part0_device_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
+    NTSTATUS Status;
+    part0_device_extension* p0de = DeviceObject->DeviceExtension;
+    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
+    
+    TRACE("control code = %x\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
+    
+    switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
+        case IOCTL_MOUNTDEV_QUERY_UNIQUE_ID:
+        {
+            MOUNTDEV_UNIQUE_ID* mduid;
+
+            if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_UNIQUE_ID)) {
+                Status = STATUS_BUFFER_TOO_SMALL;
+                Irp->IoStatus.Status = Status;
+                Irp->IoStatus.Information = sizeof(MOUNTDEV_UNIQUE_ID);
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return Status;
+            }
+
+            mduid = Irp->AssociatedIrp.SystemBuffer;
+            mduid->UniqueIdLength = sizeof(BTRFS_UUID);
+
+            if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_UNIQUE_ID) - 1 + mduid->UniqueIdLength) {
+                Status = STATUS_BUFFER_OVERFLOW;
+                Irp->IoStatus.Status = Status;
+                Irp->IoStatus.Information = sizeof(MOUNTDEV_UNIQUE_ID);
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return Status;
+            }
+
+            RtlCopyMemory(mduid->UniqueId, &p0de->uuid, sizeof(BTRFS_UUID));
+
+            Status = STATUS_SUCCESS;
+            Irp->IoStatus.Status = Status;
+            Irp->IoStatus.Information = sizeof(MOUNTDEV_UNIQUE_ID) - 1 + mduid->UniqueIdLength;
+            IoCompleteRequest(Irp, IO_NO_INCREMENT);
+            
+            return Status;
+        }
+        
+        case IOCTL_MOUNTDEV_QUERY_DEVICE_NAME:
+        {
+            PMOUNTDEV_NAME name;
+
+            if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_NAME)) {
+                Status = STATUS_BUFFER_TOO_SMALL;
+                Irp->IoStatus.Status = Status;
+                Irp->IoStatus.Information = sizeof(MOUNTDEV_NAME);
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return Status;
+            }
+
+            name = Irp->AssociatedIrp.SystemBuffer;
+            name->NameLength = p0de->name.Length;
+
+            if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_NAME) - 1 + name->NameLength) {
+                Status = STATUS_BUFFER_OVERFLOW;
+                Irp->IoStatus.Status = Status;
+                Irp->IoStatus.Information = sizeof(MOUNTDEV_NAME);
+                IoCompleteRequest(Irp, IO_NO_INCREMENT);
+                return Status;
+            }
+            
+            RtlCopyMemory(name->Name, p0de->name.Buffer, p0de->name.Length);
+
+            Status = STATUS_SUCCESS;
+            Irp->IoStatus.Status = Status;
+            Irp->IoStatus.Information = sizeof(MOUNTDEV_NAME) - 1 + name->NameLength;
+            IoCompleteRequest(Irp, IO_NO_INCREMENT);
+            
+            return Status;
+        }
+    }
+    
+    IoSkipCurrentIrpStackLocation(Irp);
+    
+    Status = IoCallDriver(p0de->devobj, Irp);
+    
+    TRACE("returning %08x\n", Status);
+    
+    return Status;
+}
+
+static NTSTATUS mountdev_query_stable_guid(device_extension* Vcb, PIRP Irp) {
+    MOUNTDEV_STABLE_GUID* msg = Irp->UserBuffer;
+    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
+    
+    TRACE("IOCTL_MOUNTDEV_QUERY_STABLE_GUID\n");
+    
+    if (IrpSp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(MOUNTDEV_STABLE_GUID))
+        return STATUS_INVALID_PARAMETER;
+
+    RtlCopyMemory(&msg->StableGuid, &Vcb->superblock.uuid, sizeof(GUID));
+    
+    Irp->IoStatus.Information = sizeof(MOUNTDEV_STABLE_GUID);
+    
+    return STATUS_SUCCESS;
+}
+
+static NTSTATUS get_partition_info_ex(device_extension* Vcb, PIRP Irp) {
+    NTSTATUS Status;
+    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
+    PARTITION_INFORMATION_EX* piex;
+    
+    TRACE("IOCTL_DISK_GET_PARTITION_INFO_EX\n");
+    
+    Status = dev_ioctl(Vcb->devices[0].devobj, IOCTL_DISK_GET_PARTITION_INFO_EX, NULL, 0,
+                       Irp->UserBuffer, IrpSp->Parameters.DeviceIoControl.OutputBufferLength, TRUE, &Irp->IoStatus);
+    if (!NT_SUCCESS(Status))
+        return Status;
+    
+    piex = (PARTITION_INFORMATION_EX*)Irp->UserBuffer;
+    
+    if (piex->PartitionStyle == PARTITION_STYLE_MBR) {
+        piex->Mbr.PartitionType = PARTITION_IFS;
+        piex->Mbr.RecognizedPartition = TRUE;
+    } else if (piex->PartitionStyle == PARTITION_STYLE_GPT) {
+        piex->Gpt.PartitionType = PARTITION_BASIC_DATA_GUID;
+    }
+    
+    return STATUS_SUCCESS;
+}
+
+static NTSTATUS is_writable(device_extension* Vcb, PIRP Irp) {
+    TRACE("IOCTL_DISK_IS_WRITABLE\n");
+    
+    return Vcb->readonly ? STATUS_MEDIA_WRITE_PROTECTED : STATUS_SUCCESS;
+}
+
+NTSTATUS STDCALL drv_device_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
+    NTSTATUS Status;
+    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
+    device_extension* Vcb = DeviceObject->DeviceExtension;
+    BOOL top_level;
+
+    FsRtlEnterFileSystem();
+
+    top_level = is_top_level(Irp);
+    
+    Irp->IoStatus.Information = 0;
+    
+    if (Vcb && Vcb->type == VCB_TYPE_PARTITION0) {
+        Status = part0_device_control(DeviceObject, Irp);
+        goto end2;
+    }
+    
+    switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
+        case IOCTL_MOUNTDEV_QUERY_STABLE_GUID:
+            Status = mountdev_query_stable_guid(Vcb, Irp);
+            goto end;
+            
+        case IOCTL_DISK_GET_PARTITION_INFO_EX:
+            Status = get_partition_info_ex(Vcb, Irp);
+            goto end;
+            
+        case IOCTL_DISK_IS_WRITABLE:
+            Status = is_writable(Vcb, Irp);
+            goto end;
+            
+        default:
+            TRACE("unhandled control code %x\n", IrpSp->Parameters.DeviceIoControl.IoControlCode);
+            break;
+    }
+    
+    IoSkipCurrentIrpStackLocation(Irp);
+    
+    Status = IoCallDriver(Vcb->devices[0].devobj, Irp);
+    
+    goto end2;
+    
+end:
+    Irp->IoStatus.Status = Status;
+
+    if (Status != STATUS_PENDING)
+        IoCompleteRequest(Irp, IO_NO_INCREMENT);
+    
+end2:
+    if (top_level) 
+        IoSetTopLevelIrp(NULL);
+    
+    FsRtlExitFileSystem();
+
+    return Status;
+}
index 5cc54e9..b70e68f 100644 (file)
@@ -37,10 +37,12 @@ ULONG STDCALL get_reparse_tag(device_extension* Vcb, root* subvol, UINT64 inode,
     ULONG tag = 0, br;
     NTSTATUS Status;
     
-    // FIXME - will this slow things down?
-    
-    if (type == BTRFS_TYPE_SYMLINK)
-        return IO_REPARSE_TAG_SYMLINK;
+    if (type == BTRFS_TYPE_SYMLINK) {
+        if (called_from_lxss())
+            return IO_REPARSE_TAG_LXSS_SYMLINK;
+        else
+            return IO_REPARSE_TAG_SYMLINK;
+    }
     
     if (type != BTRFS_TYPE_FILE && type != BTRFS_TYPE_DIRECTORY)
         return 0;
@@ -49,7 +51,7 @@ ULONG STDCALL get_reparse_tag(device_extension* Vcb, root* subvol, UINT64 inode,
         return 0;
     
     ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-    Status = open_fcb(Vcb, subvol, inode, type, NULL, NULL, &fcb, Irp);
+    Status = open_fcb(Vcb, subvol, inode, type, NULL, NULL, &fcb, PagedPool, Irp);
     if (!NT_SUCCESS(Status)) {
         ERR("open_fcb returned %08x\n", Status);
         ExReleaseResourceLite(&Vcb->fcb_lock);
@@ -85,6 +87,43 @@ end:
     return tag;
 }
 
+static ULONG get_ea_len(device_extension* Vcb, root* subvol, UINT64 inode, PIRP Irp) {
+    UINT8* eadata;
+    UINT16 len;
+    
+    if (get_xattr(Vcb, subvol, inode, EA_EA, EA_EA_HASH, &eadata, &len, Irp)) {
+        ULONG offset;
+        NTSTATUS Status;
+        
+        Status = IoCheckEaBufferValidity((FILE_FULL_EA_INFORMATION*)eadata, len, &offset);
+        
+        if (!NT_SUCCESS(Status)) {
+            WARN("IoCheckEaBufferValidity returned %08x (error at offset %u)\n", Status, offset);
+            ExFreePool(eadata);
+            return 0;
+        } else {
+            FILE_FULL_EA_INFORMATION* eainfo;
+            ULONG ealen;
+            
+            ealen = 4;
+            eainfo = (FILE_FULL_EA_INFORMATION*)eadata;
+            do {
+                ealen += 5 + eainfo->EaNameLength + eainfo->EaValueLength;
+                
+                if (eainfo->NextEntryOffset == 0)
+                    break;
+                
+                eainfo = (FILE_FULL_EA_INFORMATION*)(((UINT8*)eainfo) + eainfo->NextEntryOffset);
+            } while (TRUE);
+            
+            ExFreePool(eadata);
+            
+            return ealen;
+        }
+    } else
+        return 0;
+}
+
 static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, LONG* len, PIRP Irp, dir_entry* de, root* r) {
     PIO_STACK_LOCATION IrpSp;
     UINT32 needed;
@@ -92,7 +131,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
     INODE_ITEM ii;
     NTSTATUS Status;
     ULONG stringlen;
-    ULONG atts;
+    ULONG atts, ealen;
     
     IrpSp = IoGetCurrentIrpStackLocation(Irp);
     
@@ -139,6 +178,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
                         if (fcb2->inode == inode && !fcb2->ads) {
                             ii = fcb2->inode_item;
                             atts = fcb2->atts;
+                            ealen = fcb2->ealen;
                             found = TRUE;
                             break;
                         }
@@ -175,12 +215,20 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
                     if (IrpSp->Parameters.QueryDirectory.FileInformationClass == FileBothDirectoryInformation ||
                         IrpSp->Parameters.QueryDirectory.FileInformationClass == FileDirectoryInformation ||
                         IrpSp->Parameters.QueryDirectory.FileInformationClass == FileFullDirectoryInformation ||
-                        IrpSp->Parameters.QueryDirectory.FileInformationClass == FileIdBothDirectoryInformation) {
+                        IrpSp->Parameters.QueryDirectory.FileInformationClass == FileIdBothDirectoryInformation || 
+                        IrpSp->Parameters.QueryDirectory.FileInformationClass == FileIdFullDirectoryInformation) {
                         
                         BOOL dotfile = de->namelen > 1 && de->name[0] == '.';
 
                         atts = get_file_attributes(fcb->Vcb, &ii, r, inode, de->type, dotfile, FALSE, Irp);
                     }
+                    
+                    if (IrpSp->Parameters.QueryDirectory.FileInformationClass == FileBothDirectoryInformation || 
+                        IrpSp->Parameters.QueryDirectory.FileInformationClass == FileFullDirectoryInformation || 
+                        IrpSp->Parameters.QueryDirectory.FileInformationClass == FileIdBothDirectoryInformation || 
+                        IrpSp->Parameters.QueryDirectory.FileInformationClass == FileIdFullDirectoryInformation) {
+                        ealen = get_ea_len(fcb->Vcb, r, inode, Irp);
+                    }
                 }
                 
                 break;
@@ -191,6 +239,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
                 r = fcb->subvol;
                 inode = fcb->inode;
                 atts = fcb->atts;
+                ealen = fcb->ealen;
                 break;
                 
             case DirEntryType_Parent:
@@ -199,6 +248,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
                     r = fileref->parent->fcb->subvol;
                     inode = fileref->parent->fcb->inode;
                     atts = fileref->parent->fcb->atts;
+                    ealen = fileref->parent->fcb->ealen;
                 } else {
                     ERR("no fileref\n");
                     return STATUS_INTERNAL_ERROR;
@@ -212,6 +262,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
         IrpSp->Parameters.QueryDirectory.FileInformationClass == FileDirectoryInformation ||
         IrpSp->Parameters.QueryDirectory.FileInformationClass == FileFullDirectoryInformation ||
         IrpSp->Parameters.QueryDirectory.FileInformationClass == FileIdBothDirectoryInformation ||
+        IrpSp->Parameters.QueryDirectory.FileInformationClass == FileIdFullDirectoryInformation ||
         IrpSp->Parameters.QueryDirectory.FileInformationClass == FileNamesInformation) {
         
         Status = RtlUTF8ToUnicodeN(NULL, 0, &stringlen, de->name, de->namelen);
@@ -245,7 +296,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
             fbdi->AllocationSize.QuadPart = de->type == BTRFS_TYPE_SYMLINK ? 0 : ii.st_blocks;
             fbdi->FileAttributes = atts;
             fbdi->FileNameLength = stringlen;
-            fbdi->EaSize = get_reparse_tag(fcb->Vcb, r, inode, de->type, atts, Irp);
+            fbdi->EaSize = atts & FILE_ATTRIBUTE_REPARSE_POINT ? get_reparse_tag(fcb->Vcb, r, inode, de->type, atts, Irp) : ealen;
             fbdi->ShortNameLength = 0;
 //             fibdi->ShortName[12];
             
@@ -320,7 +371,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
             ffdi->AllocationSize.QuadPart = de->type == BTRFS_TYPE_SYMLINK ? 0 : ii.st_blocks;
             ffdi->FileAttributes = atts;
             ffdi->FileNameLength = stringlen;
-            ffdi->EaSize = get_reparse_tag(fcb->Vcb, r, inode, de->type, atts, Irp);
+            ffdi->EaSize = atts & FILE_ATTRIBUTE_REPARSE_POINT ? get_reparse_tag(fcb->Vcb, r, inode, de->type, atts, Irp) : ealen;
             
             Status = RtlUTF8ToUnicodeN(ffdi->FileName, stringlen, &stringlen, de->name, de->namelen);
 
@@ -360,10 +411,10 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
             fibdi->AllocationSize.QuadPart = de->type == BTRFS_TYPE_SYMLINK ? 0 : ii.st_blocks;
             fibdi->FileAttributes = atts;
             fibdi->FileNameLength = stringlen;
-            fibdi->EaSize = get_reparse_tag(fcb->Vcb, r, inode, de->type, atts, Irp);
+            fibdi->EaSize = atts & FILE_ATTRIBUTE_REPARSE_POINT ? get_reparse_tag(fcb->Vcb, r, inode, de->type, atts, Irp) : ealen;
             fibdi->ShortNameLength = 0;
 //             fibdi->ShortName[12];
-            fibdi->FileId.QuadPart = inode;
+            fibdi->FileId.QuadPart = make_file_id(r, inode);
             
             Status = RtlUTF8ToUnicodeN(fibdi->FileName, stringlen, &stringlen, de->name, de->namelen);
 
@@ -378,8 +429,45 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, file_ref* fileref, void* buf, L
         }
 
         case FileIdFullDirectoryInformation:
-            FIXME("STUB: FileIdFullDirectoryInformation\n");
-            break;
+        {
+            FILE_ID_FULL_DIR_INFORMATION* fifdi = buf;
+            
+            TRACE("FileIdFullDirectoryInformation\n");
+            
+            needed = sizeof(FILE_ID_FULL_DIR_INFORMATION) - sizeof(WCHAR) + stringlen;
+            
+            if (needed > *len) {
+                TRACE("buffer overflow - %u > %u\n", needed, *len);
+                return STATUS_BUFFER_OVERFLOW;
+            }
+            
+//             if (!buf)
+//                 return STATUS_INVALID_POINTER;
+            
+            fifdi->NextEntryOffset = 0;
+            fifdi->FileIndex = 0;
+            fifdi->CreationTime.QuadPart = unix_time_to_win(&ii.otime);
+            fifdi->LastAccessTime.QuadPart = unix_time_to_win(&ii.st_atime);
+            fifdi->LastWriteTime.QuadPart = unix_time_to_win(&ii.st_mtime);
+            fifdi->ChangeTime.QuadPart = unix_time_to_win(&ii.st_ctime);
+            fifdi->EndOfFile.QuadPart = de->type == BTRFS_TYPE_SYMLINK ? 0 : ii.st_size;
+            fifdi->AllocationSize.QuadPart = de->type == BTRFS_TYPE_SYMLINK ? 0 : ii.st_blocks;
+            fifdi->FileAttributes = atts;
+            fifdi->FileNameLength = stringlen;
+            fifdi->EaSize = atts & FILE_ATTRIBUTE_REPARSE_POINT ? get_reparse_tag(fcb->Vcb, r, inode, de->type, atts, Irp) : ealen;
+            fifdi->FileId.QuadPart = make_file_id(r, inode);
+            
+            Status = RtlUTF8ToUnicodeN(fifdi->FileName, stringlen, &stringlen, de->name, de->namelen);
+
+            if (!NT_SUCCESS(Status)) {
+                ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
+                return Status;
+            }
+            
+            *len -= needed;
+            
+            return STATUS_SUCCESS;
+        }
 
         case FileNamesInformation:
         {
@@ -528,12 +616,12 @@ static NTSTATUS STDCALL next_dir_entry(file_ref* fileref, UINT64* offset, dir_en
             goto end;
         }
         
-        if (keycmp(&tp.item->key, &searchkey) == -1) {
+        if (keycmp(tp.item->key, searchkey) == -1) {
             if (find_next_item(fileref->fcb->Vcb, &tp, &next_tp, FALSE, Irp))
                 tp = next_tp;
         }
         
-        if (keycmp(&tp.item->key, &searchkey) != -1 && tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
+        if (keycmp(tp.item->key, searchkey) != -1 && tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
             do {
                 if (fr) {
                     if (fr->index <= tp.item->key.offset && !fr->deleted) {
@@ -673,7 +761,7 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         return STATUS_INVALID_PARAMETER;
     }
     
-    if (!(ccb->access & FILE_LIST_DIRECTORY)) {
+    if (Irp->RequestorMode == UserMode && !(ccb->access & FILE_LIST_DIRECTORY)) {
         WARN("insufficient privileges\n");
         return STATUS_ACCESS_DENIED;
     }
@@ -724,7 +812,7 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         
         if (IrpSp->Parameters.QueryDirectory.FileName->Buffer[0] != '*') {
             specific_file = TRUE;
-            if (FsRtlDoesNameContainWildCards(IrpSp->Parameters.QueryDirectory.FileName)) {
+            if (!ccb->case_sensitive || FsRtlDoesNameContainWildCards(IrpSp->Parameters.QueryDirectory.FileName)) {
                 has_wildcard = TRUE;
                 specific_file = FALSE;
             }
@@ -796,10 +884,14 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         UNICODE_STRING us;
         LIST_ENTRY* le;
         
-        Status = RtlUpcaseUnicodeString(&us, &ccb->query_string, TRUE);
-        if (!NT_SUCCESS(Status)) {
-            ERR("RtlUpcaseUnicodeString returned %08x\n", Status);
-            goto end;
+        us.Buffer = NULL;
+        
+        if (!ccb->case_sensitive) {
+            Status = RtlUpcaseUnicodeString(&us, &ccb->query_string, TRUE);
+            if (!NT_SUCCESS(Status)) {
+                ERR("RtlUpcaseUnicodeString returned %08x\n", Status);
+                goto end;
+            }
         }
         
         ExAcquireResourceSharedLite(&fileref->nonpaged->children_lock, TRUE);
@@ -807,11 +899,17 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         le = fileref->children.Flink;
         while (le != &fileref->children) {
             file_ref* fr2 = CONTAINING_RECORD(le, file_ref, list_entry);
-                
-            if (!fr2->deleted && fr2->filepart_uc.Length == us.Length &&
-                RtlCompareMemory(fr2->filepart_uc.Buffer, us.Buffer, us.Length) == us.Length) {
-                found = TRUE;
             
+            if (!fr2->deleted) {
+                if (!ccb->case_sensitive && fr2->filepart_uc.Length == us.Length &&
+                    RtlCompareMemory(fr2->filepart_uc.Buffer, us.Buffer, us.Length) == us.Length)
+                    found = TRUE;
+                else if (ccb->case_sensitive && fr2->filepart.Length == ccb->query_string.Length &&
+                    RtlCompareMemory(fr2->filepart.Buffer, ccb->query_string.Buffer, ccb->query_string.Length) == ccb->query_string.Length)
+                    found = TRUE;
+            }
+                
+            if (found) {
                 if (fr2->fcb->subvol == fcb->subvol) {
                     de.key.obj_id = fr2->fcb->inode;
                     de.key.obj_type = TYPE_INODE_ITEM;
@@ -847,7 +945,7 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
             ExFreePool(us.Buffer);
         
         if (!found) {
-            Status = find_file_in_dir(fcb->Vcb, &ccb->query_string, fileref, &found_subvol, &found_inode, &found_type, &found_index, &utf8, Irp);
+            Status = find_file_in_dir(fcb->Vcb, &ccb->query_string, fileref, &found_subvol, &found_inode, &found_type, &found_index, &utf8, FALSE, Irp);
             
             if (!NT_SUCCESS(Status)) {
                 Status = STATUS_NO_SUCH_FILE;
@@ -898,7 +996,7 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         di_uni_fn.Length = di_uni_fn.MaximumLength = stringlen;
         di_uni_fn.Buffer = uni_fn;
         
-        while (!FsRtlIsNameInExpression(&ccb->query_string, &di_uni_fn, TRUE, NULL)) {
+        while (!FsRtlIsNameInExpression(&ccb->query_string, &di_uni_fn, !ccb->case_sensitive, NULL)) {
             if (de.name_alloc)
                 ExFreePool(de.name);
             
@@ -961,6 +1059,7 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
                 case FileDirectoryInformation:
                 case FileIdBothDirectoryInformation:
                 case FileFullDirectoryInformation:
+                case FileIdFullDirectoryInformation:
                     length -= length % 8;
                     break;
                     
@@ -1011,7 +1110,7 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
                         di_uni_fn.Buffer = uni_fn;
                     }
                     
-                    if (!has_wildcard || FsRtlIsNameInExpression(&ccb->query_string, &di_uni_fn, TRUE, NULL)) {
+                    if (!has_wildcard || FsRtlIsNameInExpression(&ccb->query_string, &di_uni_fn, !ccb->case_sensitive, NULL)) {
                         curitem = (UINT8*)buf + IrpSp->Parameters.QueryDirectory.Length - length;
                         count++;
                         
@@ -1086,7 +1185,7 @@ static NTSTATUS STDCALL notify_change_directory(device_extension* Vcb, PIRP Irp)
         return STATUS_INVALID_PARAMETER;
     }
     
-    if (!(ccb->access & FILE_LIST_DIRECTORY)) {
+    if (Irp->RequestorMode == UserMode && !(ccb->access & FILE_LIST_DIRECTORY)) {
         WARN("insufficient privileges\n");
         return STATUS_ACCESS_DENIED;
     }
index 992b33d..8df4e57 100644 (file)
 
 #include "btrfs_drv.h"
 
+typedef struct {
+    UINT8 type;
+    
+    union {
+        EXTENT_DATA_REF edr;
+        SHARED_DATA_REF sdr;
+        TREE_BLOCK_REF tbr;
+        SHARED_BLOCK_REF sbr;
+    };
+    
+    UINT64 hash;
+    LIST_ENTRY list_entry;
+} extent_ref;
+
 static __inline ULONG get_extent_data_len(UINT8 type) {
     switch (type) {
         case TYPE_TREE_BLOCK_REF:
@@ -28,7 +42,8 @@ static __inline ULONG get_extent_data_len(UINT8 type) {
         case TYPE_EXTENT_REF_V0:
             return sizeof(EXTENT_REF_V0);
             
-        // FIXME - TYPE_SHARED_BLOCK_REF
+        case TYPE_SHARED_BLOCK_REF:
+            return sizeof(SHARED_BLOCK_REF);
             
         case TYPE_SHARED_DATA_REF:
             return sizeof(SHARED_DATA_REF);
@@ -55,7 +70,8 @@ static __inline UINT64 get_extent_data_refcount(UINT8 type, void* data) {
             return erv0->count;
         }
         
-        // FIXME - TYPE_SHARED_BLOCK_REF
+        case TYPE_SHARED_BLOCK_REF:
+            return 1;
         
         case TYPE_SHARED_DATA_REF:
         {
@@ -85,160 +101,477 @@ static __inline UINT64 get_extent_data_ref_hash(EXTENT_DATA_REF* edr) {
 static UINT64 get_extent_hash(UINT8 type, void* data) {
     if (type == TYPE_EXTENT_DATA_REF) {
         return get_extent_data_ref_hash((EXTENT_DATA_REF*)data);
+    } else if (type == TYPE_SHARED_BLOCK_REF) {
+        SHARED_BLOCK_REF* sbr = (SHARED_BLOCK_REF*)data;
+        return sbr->offset;
+    } else if (type == TYPE_SHARED_DATA_REF) {
+        SHARED_DATA_REF* sdr = (SHARED_DATA_REF*)data;
+        return sdr->offset;
+    } else if (type == TYPE_TREE_BLOCK_REF) {
+        TREE_BLOCK_REF* tbr = (TREE_BLOCK_REF*)data;
+        return tbr->offset;
     } else {
         ERR("unhandled extent type %x\n", type);
         return 0;
     }
 }
 
-static NTSTATUS increase_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT8 type, void* data, KEY* firstitem, UINT8 level, PIRP Irp, LIST_ENTRY* rollback) {
-    NTSTATUS Status;
-    KEY searchkey;
-    traverse_ptr tp;
-    ULONG datalen = get_extent_data_len(type), len, max_extent_item_size;
-    EXTENT_ITEM* ei;
-    UINT8* ptr;
-    UINT64 inline_rc, offset;
-    UINT8* data2;
-    EXTENT_ITEM* newei;
+static void free_extent_refs(LIST_ENTRY* extent_refs) {
+    while (!IsListEmpty(extent_refs)) {
+        LIST_ENTRY* le = RemoveHeadList(extent_refs);
+        extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+        
+        ExFreePool(er);
+    }
+}
+
+static NTSTATUS add_shared_data_extent_ref(LIST_ENTRY* extent_refs, UINT64 parent, UINT32 count) {
+    extent_ref* er2;
+    LIST_ENTRY* le;
     
-    // FIXME - handle A9s
+    if (!IsListEmpty(extent_refs)) {
+        le = extent_refs->Flink;
+        
+        while (le != extent_refs) {
+            extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+            
+            if (er->type == TYPE_SHARED_DATA_REF && er->sdr.offset == parent) {
+                er->sdr.count += count;
+                return STATUS_SUCCESS;
+            }
+            
+            le = le->Flink;
+        }
+    }
     
-    if (datalen == 0) {
-        ERR("unrecognized extent type %x\n", type);
-        return STATUS_INTERNAL_ERROR;
+    er2 = ExAllocatePoolWithTag(PagedPool, sizeof(extent_ref), ALLOC_TAG);
+    if (!er2) {
+        ERR("out of memory\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
     }
     
-    searchkey.obj_id = address;
-    searchkey.obj_type = TYPE_EXTENT_ITEM;
-    searchkey.offset = 0xffffffffffffffff;
+    er2->type = TYPE_SHARED_DATA_REF;
+    er2->sdr.offset = parent;
+    er2->sdr.count = count;
     
-    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        return Status;
-    }
+    InsertTailList(extent_refs, &er2->list_entry);
     
-    // If entry doesn't exist yet, create new inline extent item
+    return STATUS_SUCCESS;
+}
+
+static NTSTATUS add_shared_block_extent_ref(LIST_ENTRY* extent_refs, UINT64 parent) {
+    extent_ref* er2;
+    LIST_ENTRY* le;
     
-    if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
-        ULONG eisize;
-        EXTENT_ITEM* ei;
-        BOOL is_tree = type == TYPE_TREE_BLOCK_REF;
-        UINT8* ptr;
-        
-        eisize = sizeof(EXTENT_ITEM);
-        if (is_tree) eisize += sizeof(EXTENT_ITEM2);
-        eisize += sizeof(UINT8);
-        eisize += datalen;
-        
-        ei = ExAllocatePoolWithTag(PagedPool, eisize, ALLOC_TAG);
-        if (!ei) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
-        
-        ei->refcount = get_extent_data_refcount(type, data);
-        ei->generation = Vcb->superblock.generation;
-        ei->flags = is_tree ? EXTENT_ITEM_TREE_BLOCK : EXTENT_ITEM_DATA;
-        ptr = (UINT8*)&ei[1];
+    if (!IsListEmpty(extent_refs)) {
+        le = extent_refs->Flink;
         
-        if (is_tree) {
-            EXTENT_ITEM2* ei2 = (EXTENT_ITEM2*)ptr;
-            ei2->firstitem = *firstitem;
-            ei2->level = level;
-            ptr = (UINT8*)&ei2[1];
+        while (le != extent_refs) {
+            extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+            
+            if (er->type == TYPE_SHARED_BLOCK_REF && er->sbr.offset == parent)
+                return STATUS_SUCCESS;
+            
+            le = le->Flink;
         }
+    }
+    
+    er2 = ExAllocatePoolWithTag(PagedPool, sizeof(extent_ref), ALLOC_TAG);
+    if (!er2) {
+        ERR("out of memory\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    er2->type = TYPE_SHARED_BLOCK_REF;
+    er2->sbr.offset = parent;
+    
+    InsertTailList(extent_refs, &er2->list_entry);
+    
+    return STATUS_SUCCESS;
+}
+
+static NTSTATUS add_tree_block_extent_ref(LIST_ENTRY* extent_refs, UINT64 root) {
+    extent_ref* er2;
+    LIST_ENTRY* le;
+    
+    if (!IsListEmpty(extent_refs)) {
+        le = extent_refs->Flink;
         
-        *ptr = type;
-        RtlCopyMemory(ptr + 1, data, datalen);
-        
-        if (!insert_tree_item(Vcb, Vcb->extent_root, address, TYPE_EXTENT_ITEM, size, ei, eisize, NULL, Irp, rollback)) {
-            ERR("insert_tree_item failed\n");
-            return STATUS_INTERNAL_ERROR;
+        while (le != extent_refs) {
+            extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+            
+            if (er->type == TYPE_TREE_BLOCK_REF && er->tbr.offset == root)
+                return STATUS_SUCCESS;
+            
+            le = le->Flink;
         }
-        
-        // FIXME - add to space list?
+    }
+    
+    er2 = ExAllocatePoolWithTag(PagedPool, sizeof(extent_ref), ALLOC_TAG);
+    if (!er2) {
+        ERR("out of memory\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    er2->type = TYPE_TREE_BLOCK_REF;
+    er2->tbr.offset = root;
+    
+    InsertTailList(extent_refs, &er2->list_entry);
+    
+    return STATUS_SUCCESS;
+}
 
+static NTSTATUS construct_extent_item(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 flags, LIST_ENTRY* extent_refs,
+                                      KEY* firstitem, UINT8 level, PIRP Irp, LIST_ENTRY* rollback) {
+    LIST_ENTRY *le, *next_le;
+    UINT64 refcount;
+    ULONG inline_len;
+    BOOL all_inline = TRUE;
+    extent_ref* first_noninline;
+    EXTENT_ITEM* ei;
+    UINT8* siptr;
+    
+    // FIXME - write skinny extents if is tree and incompat flag set
+    
+    if (IsListEmpty(extent_refs)) {
+        WARN("no extent refs found\n");
         return STATUS_SUCCESS;
-    } else if (tp.item->key.offset != size) {
-        ERR("extent %llx exists, but with size %llx rather than %llx expected\n", tp.item->key.obj_id, tp.item->key.offset, size);
-        return STATUS_INTERNAL_ERROR;
     }
+    
+    refcount = 0;
+    inline_len = sizeof(EXTENT_ITEM);
+    
+    if (flags & EXTENT_ITEM_TREE_BLOCK)
+        inline_len += sizeof(EXTENT_ITEM2);
+    
+    le = extent_refs->Flink;
+    while (le != extent_refs) {
+        extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+        UINT64 rc;
         
-    if (tp.item->size == sizeof(EXTENT_ITEM_V0)) {
-        EXTENT_ITEM_V0* eiv0 = (EXTENT_ITEM_V0*)tp.item->data;
-        
-        TRACE("converting old-style extent at (%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
-        
-        ei = ExAllocatePoolWithTag(PagedPool, sizeof(EXTENT_ITEM), ALLOC_TAG);
-        
-        if (!ei) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
-        
-        ei->refcount = eiv0->refcount;
-        ei->generation = Vcb->superblock.generation;
-        ei->flags = EXTENT_ITEM_DATA;
-        
-        delete_tree_item(Vcb, &tp, rollback);
+        next_le = le->Flink;
         
-        if (!insert_tree_item(Vcb, Vcb->extent_root, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, ei, sizeof(EXTENT_ITEM), NULL, Irp, rollback)) {
-            ERR("insert_tree_item failed\n");
-            ExFreePool(ei);
-            return STATUS_INTERNAL_ERROR;
-        }
+        rc = get_extent_data_refcount(er->type, &er->edr);
         
-        Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
-        if (!NT_SUCCESS(Status)) {
-            ERR("error - find_item returned %08x\n", Status);
-            return Status;
+        if (rc == 0) {
+            RemoveEntryList(&er->list_entry);
+            
+            ExFreePool(er);
+        } else {
+            ULONG extlen = get_extent_data_len(er->type);
+            
+            refcount += rc;
+            
+            er->hash = get_extent_hash(er->type, &er->edr);
+            
+            if (all_inline) {
+                if (inline_len + 1 + extlen > Vcb->superblock.node_size / 4) {
+                    all_inline = FALSE;
+                    first_noninline = er;
+                } else
+                    inline_len += extlen + 1;
+            }
         }
-    }
         
-    if (tp.item->size < sizeof(EXTENT_ITEM)) {
-        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(EXTENT_ITEM));
-        return STATUS_INTERNAL_ERROR;
+        le = next_le;
     }
     
-    ei = (EXTENT_ITEM*)tp.item->data;
+    ei = ExAllocatePoolWithTag(PagedPool, inline_len, ALLOC_TAG);
+    if (!ei) {
+        ERR("out of memory\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
     
-    len = tp.item->size - sizeof(EXTENT_ITEM);
-    ptr = (UINT8*)&ei[1];
+    ei->refcount = refcount;
+    ei->generation = Vcb->superblock.generation;
+    ei->flags = flags;
     
-    if (ei->flags & EXTENT_ITEM_TREE_BLOCK) {
-        if (tp.item->size < sizeof(EXTENT_ITEM) + sizeof(EXTENT_ITEM2)) {
-            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(EXTENT_ITEM) + sizeof(EXTENT_ITEM2));
-            return STATUS_INTERNAL_ERROR;
+    if (flags & EXTENT_ITEM_TREE_BLOCK) {
+        EXTENT_ITEM2* ei2 = (EXTENT_ITEM2*)&ei[1];
+        
+        if (firstitem) {
+            ei2->firstitem.obj_id = firstitem->obj_id;
+            ei2->firstitem.obj_type = firstitem->obj_type;
+            ei2->firstitem.offset = firstitem->offset;
+        } else {
+            ei2->firstitem.obj_id = 0;
+            ei2->firstitem.obj_type = 0;
+            ei2->firstitem.offset = 0;
         }
         
-        len -= sizeof(EXTENT_ITEM2);
-        ptr += sizeof(EXTENT_ITEM2);
-    }
-    
-    inline_rc = 0;
+        ei2->level = level;
+        
+        siptr = (UINT8*)&ei2[1];
+    } else
+        siptr = (UINT8*)&ei[1];
     
-    // Loop through existing inline extent entries
+    // Do we need to sort the inline extent refs? The Linux driver doesn't seem to bother.
     
-    while (len > 0) {
-        UINT8 secttype = *ptr;
-        ULONG sectlen = get_extent_data_len(secttype);
-        UINT64 sectcount = get_extent_data_refcount(secttype, ptr + sizeof(UINT8));
+    le = extent_refs->Flink;
+    while (le != extent_refs) {
+        extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+        ULONG extlen = get_extent_data_len(er->type);
         
-        len--;
+        if (!all_inline && er == first_noninline)
+            break;
         
-        if (sectlen > len) {
-            ERR("(%llx,%x,%llx): %x bytes left, expecting at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, len, sectlen);
-            return STATUS_INTERNAL_ERROR;
-        }
-
-        if (sectlen == 0) {
-            ERR("(%llx,%x,%llx): unrecognized extent type %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, secttype);
-            return STATUS_INTERNAL_ERROR;
-        }
+        *siptr = er->type;
+        siptr++;
         
-        // If inline extent already present, increase refcount and return
+        if (extlen > 0) {
+            RtlCopyMemory(siptr, &er->edr, extlen);
+            siptr += extlen;
+        }
+         
+        le = le->Flink;
+    }
+    
+    if (!insert_tree_item(Vcb, Vcb->extent_root, address, TYPE_EXTENT_ITEM, size, ei, inline_len, NULL, Irp, rollback)) {
+        ERR("error - failed to insert item\n");
+        ExFreePool(ei);
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    if (!all_inline) {
+        le = &first_noninline->list_entry;
+        
+        while (le != extent_refs) {
+            extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+            ULONG len = get_extent_data_len(er->type);
+            UINT8* data;
+            
+            if (len > 0) {
+                data = ExAllocatePoolWithTag(PagedPool, len, ALLOC_TAG);
+                
+                if (!data) {
+                    ERR("out of memory\n");
+                    return STATUS_INSUFFICIENT_RESOURCES;
+                }
+                
+                RtlCopyMemory(data, &er->edr, len);
+            } else
+                data = NULL;
+            
+            if (!insert_tree_item(Vcb, Vcb->extent_root, address, er->type, er->hash, data, len, NULL, Irp, rollback)) {
+                ERR("error - failed to insert item\n");
+                return STATUS_INTERNAL_ERROR;
+            }
+            
+            le = le->Flink;
+        }
+    }
+    
+    return STATUS_SUCCESS;
+}
+
+static NTSTATUS convert_old_extent(device_extension* Vcb, UINT64 address, BOOL tree, KEY* firstitem, UINT8 level, PIRP Irp, LIST_ENTRY* rollback) {
+    NTSTATUS Status;
+    KEY searchkey;
+    traverse_ptr tp, next_tp;
+    LIST_ENTRY extent_refs;
+    UINT64 size;
+    
+    InitializeListHead(&extent_refs);
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = TYPE_EXTENT_ITEM;
+    searchkey.offset = 0xffffffffffffffff;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+    if (!NT_SUCCESS(Status)) {
+        ERR("find_item returned %08x\n", Status);
+        return Status;
+    }
+    
+    if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
+        ERR("old-style extent %llx not found\n", address);
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    size = tp.item->key.offset;
+    
+    delete_tree_item(Vcb, &tp, rollback);
+    
+    while (find_next_item(Vcb, &tp, &next_tp, FALSE, Irp)) {
+        tp = next_tp;
+        
+        if (tp.item->key.obj_id == address && tp.item->key.obj_type == TYPE_EXTENT_REF_V0 && tp.item->size >= sizeof(EXTENT_REF_V0)) {
+            EXTENT_REF_V0* erv0 = (EXTENT_REF_V0*)tp.item->data;
+            
+            if (tree) {
+                if (tp.item->key.offset == tp.item->key.obj_id) { // top of the tree
+                    Status = add_tree_block_extent_ref(&extent_refs, erv0->root);
+                    if (!NT_SUCCESS(Status)) {
+                        ERR("add_tree_block_extent_ref returned %08x\n", Status);
+                        goto end;
+                    }
+                } else {
+                    Status = add_shared_block_extent_ref(&extent_refs, tp.item->key.offset);
+                    if (!NT_SUCCESS(Status)) {
+                        ERR("add_shared_block_extent_ref returned %08x\n", Status);
+                        goto end;
+                    }
+                }
+            } else {
+                Status = add_shared_data_extent_ref(&extent_refs, tp.item->key.offset, erv0->count);
+                if (!NT_SUCCESS(Status)) {
+                    ERR("add_shared_data_extent_ref returned %08x\n", Status);
+                    goto end;
+                }
+            }
+            
+            delete_tree_item(Vcb, &tp, rollback);
+        }
+
+        if (tp.item->key.obj_id > address || tp.item->key.obj_type > TYPE_EXTENT_REF_V0)
+            break;
+    }
+
+    Status = construct_extent_item(Vcb, address, size, tree ? (EXTENT_ITEM_TREE_BLOCK | EXTENT_ITEM_SHARED_BACKREFS) : EXTENT_ITEM_DATA,
+                                   &extent_refs, firstitem, level, Irp, rollback);
+    if (!NT_SUCCESS(Status))
+        ERR("construct_extent_item returned %08x\n", Status);
+
+end:
+    free_extent_refs(&extent_refs);
+    
+    return Status;
+}
+
+NTSTATUS increase_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT8 type, void* data, KEY* firstitem, UINT8 level, PIRP Irp, LIST_ENTRY* rollback) {
+    NTSTATUS Status;
+    KEY searchkey;
+    traverse_ptr tp;
+    ULONG datalen = get_extent_data_len(type), len, max_extent_item_size;
+    EXTENT_ITEM* ei;
+    UINT8* ptr;
+    UINT64 inline_rc, offset;
+    UINT8* data2;
+    EXTENT_ITEM* newei;
+    BOOL skinny;
+    BOOL is_tree = type == TYPE_TREE_BLOCK_REF || type == TYPE_SHARED_BLOCK_REF;
+    
+    if (datalen == 0) {
+        ERR("unrecognized extent type %x\n", type);
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA ? TYPE_METADATA_ITEM : TYPE_EXTENT_ITEM;
+    searchkey.offset = 0xffffffffffffffff;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return Status;
+    }
+    
+    // If entry doesn't exist yet, create new inline extent item
+    
+    if (tp.item->key.obj_id != searchkey.obj_id || (tp.item->key.obj_type != TYPE_EXTENT_ITEM && tp.item->key.obj_type != TYPE_METADATA_ITEM)) {
+        ULONG eisize;
+        EXTENT_ITEM* ei;
+        UINT8* ptr;
+        
+        eisize = sizeof(EXTENT_ITEM);
+        if (is_tree) eisize += sizeof(EXTENT_ITEM2);
+        eisize += sizeof(UINT8);
+        eisize += datalen;
+        
+        ei = ExAllocatePoolWithTag(PagedPool, eisize, ALLOC_TAG);
+        if (!ei) {
+            ERR("out of memory\n");
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+        
+        ei->refcount = get_extent_data_refcount(type, data);
+        ei->generation = Vcb->superblock.generation;
+        ei->flags = is_tree ? EXTENT_ITEM_TREE_BLOCK : EXTENT_ITEM_DATA;
+        ptr = (UINT8*)&ei[1];
+        
+        if (is_tree && !(Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA)) {
+            EXTENT_ITEM2* ei2 = (EXTENT_ITEM2*)ptr;
+            ei2->firstitem = *firstitem;
+            ei2->level = level;
+            ptr = (UINT8*)&ei2[1];
+        }
+        
+        *ptr = type;
+        RtlCopyMemory(ptr + 1, data, datalen);
+        
+        if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA && is_tree) {
+            if (!insert_tree_item(Vcb, Vcb->extent_root, address, TYPE_METADATA_ITEM, level, ei, eisize, NULL, Irp, rollback)) {
+                ERR("insert_tree_item failed\n");
+                return STATUS_INTERNAL_ERROR;
+            }
+        } else {
+            if (!insert_tree_item(Vcb, Vcb->extent_root, address, TYPE_EXTENT_ITEM, size, ei, eisize, NULL, Irp, rollback)) {
+                ERR("insert_tree_item failed\n");
+                return STATUS_INTERNAL_ERROR;
+            }
+        }
+
+        return STATUS_SUCCESS;
+    } else if (tp.item->key.obj_id == address && tp.item->key.obj_type == TYPE_EXTENT_ITEM && tp.item->key.offset != size) {
+        ERR("extent %llx exists, but with size %llx rather than %llx expected\n", tp.item->key.obj_id, tp.item->key.offset, size);
+        return STATUS_INTERNAL_ERROR;
+    }
+
+    skinny = tp.item->key.obj_type == TYPE_METADATA_ITEM;
+
+    if (tp.item->size == sizeof(EXTENT_ITEM_V0) && !skinny) {
+        Status = convert_old_extent(Vcb, address, is_tree, firstitem, level, Irp, rollback);
+        
+        if (!NT_SUCCESS(Status)) {
+            ERR("convert_old_extent returned %08x\n", Status);
+            return Status;
+        }
+
+        return increase_extent_refcount(Vcb, address, size, type, data, firstitem, level, Irp, rollback);
+    }
+        
+    if (tp.item->size < sizeof(EXTENT_ITEM)) {
+        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(EXTENT_ITEM));
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    ei = (EXTENT_ITEM*)tp.item->data;
+    
+    len = tp.item->size - sizeof(EXTENT_ITEM);
+    ptr = (UINT8*)&ei[1];
+    
+    if (ei->flags & EXTENT_ITEM_TREE_BLOCK && !skinny) {
+        if (tp.item->size < sizeof(EXTENT_ITEM) + sizeof(EXTENT_ITEM2)) {
+            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(EXTENT_ITEM) + sizeof(EXTENT_ITEM2));
+            return STATUS_INTERNAL_ERROR;
+        }
+        
+        len -= sizeof(EXTENT_ITEM2);
+        ptr += sizeof(EXTENT_ITEM2);
+    }
+    
+    inline_rc = 0;
+    
+    // Loop through existing inline extent entries
+    
+    while (len > 0) {
+        UINT8 secttype = *ptr;
+        ULONG sectlen = get_extent_data_len(secttype);
+        UINT64 sectcount = get_extent_data_refcount(secttype, ptr + sizeof(UINT8));
+        
+        len--;
+        
+        if (sectlen > len) {
+            ERR("(%llx,%x,%llx): %x bytes left, expecting at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, len, sectlen);
+            return STATUS_INTERNAL_ERROR;
+        }
+
+        if (sectlen == 0) {
+            ERR("(%llx,%x,%llx): unrecognized extent type %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, secttype);
+            return STATUS_INTERNAL_ERROR;
+        }
+        
+        // If inline extent already present, increase refcount and return
         
         if (secttype == type) {
             if (type == TYPE_EXTENT_DATA_REF) {
@@ -273,8 +606,50 @@ static NTSTATUS increase_extent_refcount(device_extension* Vcb, UINT64 address,
                     return STATUS_SUCCESS;
                 }
             } else if (type == TYPE_TREE_BLOCK_REF) {
-                ERR("trying to increase refcount of tree extent\n");
-                return STATUS_INTERNAL_ERROR;
+                TREE_BLOCK_REF* secttbr = (TREE_BLOCK_REF*)(ptr + sizeof(UINT8));
+                TREE_BLOCK_REF* tbr = (TREE_BLOCK_REF*)data;
+                
+                if (secttbr->offset == tbr->offset) {
+                    TRACE("trying to increase refcount of non-shared tree extent\n");
+                    return STATUS_SUCCESS;
+                }
+            } else if (type == TYPE_SHARED_BLOCK_REF) {
+                SHARED_BLOCK_REF* sectsbr = (SHARED_BLOCK_REF*)(ptr + sizeof(UINT8));
+                SHARED_BLOCK_REF* sbr = (SHARED_BLOCK_REF*)data;
+                
+                if (sectsbr->offset == sbr->offset)
+                    return STATUS_SUCCESS;
+            } else if (type == TYPE_SHARED_DATA_REF) {
+                SHARED_DATA_REF* sectsdr = (SHARED_DATA_REF*)(ptr + sizeof(UINT8));
+                SHARED_DATA_REF* sdr = (SHARED_DATA_REF*)data;
+                
+                if (sectsdr->offset == sdr->offset) {
+                    UINT32 rc = get_extent_data_refcount(type, data);
+                    SHARED_DATA_REF* sectsdr2;
+                    
+                    newei = ExAllocatePoolWithTag(PagedPool, tp.item->size, ALLOC_TAG);
+                    if (!newei) {
+                        ERR("out of memory\n");
+                        return STATUS_INSUFFICIENT_RESOURCES;
+                    }
+                    
+                    RtlCopyMemory(newei, tp.item->data, tp.item->size);
+                    
+                    newei->generation = Vcb->superblock.generation;
+                    newei->refcount += rc;
+                    
+                    sectsdr2 = (SHARED_DATA_REF*)((UINT8*)newei + ((UINT8*)sectsdr - tp.item->data));
+                    sectsdr2->count += rc;
+                    
+                    delete_tree_item(Vcb, &tp, rollback);
+                    
+                    if (!insert_tree_item(Vcb, Vcb->extent_root, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, newei, tp.item->size, NULL, Irp, rollback)) {
+                        ERR("insert_tree_item failed\n");
+                        return STATUS_INTERNAL_ERROR;
+                    }
+                    
+                    return STATUS_SUCCESS;
+                }
             } else {
                 ERR("unhandled extent type %x\n", type);
                 return STATUS_INTERNAL_ERROR;
@@ -296,7 +671,7 @@ static NTSTATUS increase_extent_refcount(device_extension* Vcb, UINT64 address,
         len = tp.item->size - sizeof(EXTENT_ITEM);
         ptr = (UINT8*)&ei[1];
         
-        if (ei->flags & EXTENT_ITEM_TREE_BLOCK) {
+        if (ei->flags & EXTENT_ITEM_TREE_BLOCK && !skinny) {
             len -= sizeof(EXTENT_ITEM2);
             ptr += sizeof(EXTENT_ITEM2);
         }
@@ -358,7 +733,7 @@ static NTSTATUS increase_extent_refcount(device_extension* Vcb, UINT64 address,
             return Status;
         }
         
-        if (!keycmp(&tp.item->key, &searchkey)) {
+        if (!keycmp(tp.item->key, searchkey)) {
             if (tp.item->size < datalen) {
                 ERR("(%llx,%x,%llx) was %x bytes, expecting %x\n", tp2.item->key.obj_id, tp2.item->key.obj_type, tp2.item->key.offset, tp.item->size, datalen);
                 return STATUS_INTERNAL_ERROR;
@@ -372,8 +747,14 @@ static NTSTATUS increase_extent_refcount(device_extension* Vcb, UINT64 address,
                 
                 edr->count += get_extent_data_refcount(type, data);
             } else if (type == TYPE_TREE_BLOCK_REF) {
-                ERR("trying to increase refcount of tree extent\n");
-                return STATUS_INTERNAL_ERROR;
+                TRACE("trying to increase refcount of non-shared tree extent\n");
+                return STATUS_SUCCESS;
+            } else if (type == TYPE_SHARED_BLOCK_REF)
+                return STATUS_SUCCESS;
+            else if (type == TYPE_SHARED_DATA_REF) {
+                SHARED_DATA_REF* sdr = (SHARED_DATA_REF*)data2;
+                
+                sdr->count += get_extent_data_refcount(type, data);
             } else {
                 ERR("unhandled extent type %x\n", type);
                 return STATUS_INTERNAL_ERROR;
@@ -446,8 +827,8 @@ void decrease_chunk_usage(chunk* c, UINT64 delta) {
     TRACE("decreasing size of chunk %llx by %llx\n", c->offset, delta);
 }
 
-static NTSTATUS decrease_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT8 type, void* data, KEY* firstitem,
-                                         UINT8 level, UINT64 parent, PIRP Irp, LIST_ENTRY* rollback) {
+NTSTATUS decrease_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT8 type, void* data, KEY* firstitem,
+                                  UINT8 level, UINT64 parent, PIRP Irp, LIST_ENTRY* rollback) {
     KEY searchkey;
     NTSTATUS Status;
     traverse_ptr tp, tp2;
@@ -457,57 +838,53 @@ static NTSTATUS decrease_extent_refcount(device_extension* Vcb, UINT64 address,
     UINT8* ptr;
     UINT32 rc = data ? get_extent_data_refcount(type, data) : 1;
     ULONG datalen = get_extent_data_len(type);
+    BOOL is_tree = (type == TYPE_TREE_BLOCK_REF || type == TYPE_SHARED_BLOCK_REF), skinny = FALSE;
     
-    // FIXME - handle trees
-    
-    searchkey.obj_id = address;
-    searchkey.obj_type = TYPE_EXTENT_ITEM;
-    searchkey.offset = 0xffffffffffffffff;
-    
-    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        return Status;
+    if (is_tree && Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA) {
+        searchkey.obj_id = address;
+        searchkey.obj_type = TYPE_METADATA_ITEM;
+        searchkey.offset = 0xffffffffffffffff;
+        
+        Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+        if (!NT_SUCCESS(Status)) {
+            ERR("error - find_item returned %08x\n", Status);
+            return Status;
+        }
+        
+        if (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type)
+            skinny = TRUE;
     }
     
-    if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
-        ERR("could not find EXTENT_ITEM for address %llx\n", address);
-        return STATUS_INTERNAL_ERROR;
-    }
-    
-    if (tp.item->key.offset != size) {
-        ERR("extent %llx had length %llx, not %llx as expected\n", address, tp.item->key.offset, size);
-        return STATUS_INTERNAL_ERROR;
-    }
-    
-    if (tp.item->size == sizeof(EXTENT_ITEM_V0)) {
-        EXTENT_ITEM_V0* eiv0 = (EXTENT_ITEM_V0*)tp.item->data;
-        
-        TRACE("converting old-style extent at (%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
-        
-        ei = ExAllocatePoolWithTag(PagedPool, sizeof(EXTENT_ITEM), ALLOC_TAG);
+    if (!skinny) {
+        searchkey.obj_id = address;
+        searchkey.obj_type = TYPE_EXTENT_ITEM;
+        searchkey.offset = 0xffffffffffffffff;
         
-        if (!ei) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
+        Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+        if (!NT_SUCCESS(Status)) {
+            ERR("error - find_item returned %08x\n", Status);
+            return Status;
         }
         
-        ei->refcount = eiv0->refcount;
-        ei->generation = Vcb->superblock.generation;
-        ei->flags = EXTENT_ITEM_DATA;
-        
-        delete_tree_item(Vcb, &tp, rollback);
+        if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
+            ERR("could not find EXTENT_ITEM for address %llx\n", address);
+            return STATUS_INTERNAL_ERROR;
+        }
         
-        if (!insert_tree_item(Vcb, Vcb->extent_root, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, ei, sizeof(EXTENT_ITEM), &tp, Irp, rollback)) {
-            ERR("insert_tree_item failed\n");
-            ExFreePool(ei);
+        if (tp.item->key.offset != size) {
+            ERR("extent %llx had length %llx, not %llx as expected\n", address, tp.item->key.offset, size);
             return STATUS_INTERNAL_ERROR;
         }
         
-        Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
-        if (!NT_SUCCESS(Status)) {
-            ERR("error - find_item returned %08x\n", Status);
-            return Status;
+        if (tp.item->size == sizeof(EXTENT_ITEM_V0)) {
+            Status = convert_old_extent(Vcb, address, is_tree, firstitem, level, Irp, rollback);
+            
+            if (!NT_SUCCESS(Status)) {
+                ERR("convert_old_extent returned %08x\n", Status);
+                return Status;
+            }
+
+            return decrease_extent_refcount(Vcb, address, size, type, data, firstitem, level, parent, Irp, rollback);
         }
     }
     
@@ -521,7 +898,7 @@ static NTSTATUS decrease_extent_refcount(device_extension* Vcb, UINT64 address,
     len = tp.item->size - sizeof(EXTENT_ITEM);
     ptr = (UINT8*)&ei[1];
     
-    if (ei->flags & EXTENT_ITEM_TREE_BLOCK) {
+    if (ei->flags & EXTENT_ITEM_TREE_BLOCK && !skinny) {
         if (tp.item->size < sizeof(EXTENT_ITEM) + sizeof(EXTENT_ITEM2)) {
             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(EXTENT_ITEM) + sizeof(EXTENT_ITEM2));
             return STATUS_INTERNAL_ERROR;
@@ -648,6 +1025,80 @@ static NTSTATUS decrease_extent_refcount(device_extension* Vcb, UINT64 address,
                         return STATUS_INTERNAL_ERROR;
                     }
                     
+                    return STATUS_SUCCESS;
+                }
+            } else if (type == TYPE_TREE_BLOCK_REF) {
+                TREE_BLOCK_REF* secttbr = (TREE_BLOCK_REF*)(ptr + sizeof(UINT8));
+                TREE_BLOCK_REF* tbr = (TREE_BLOCK_REF*)data;
+                ULONG neweilen;
+                EXTENT_ITEM* newei;
+                
+                if (secttbr->offset == tbr->offset) {
+                    if (ei->refcount == 1) {
+                        delete_tree_item(Vcb, &tp, rollback);
+                        return STATUS_SUCCESS;
+                    }
+
+                    neweilen = tp.item->size - sizeof(UINT8) - sectlen;
+                    
+                    newei = ExAllocatePoolWithTag(PagedPool, neweilen, ALLOC_TAG);
+                    if (!newei) {
+                        ERR("out of memory\n");
+                        return STATUS_INSUFFICIENT_RESOURCES;
+                    }
+                    
+                    RtlCopyMemory(newei, ei, ptr - tp.item->data);
+                    
+                    if (len > sectlen)
+                        RtlCopyMemory((UINT8*)newei + (ptr - tp.item->data), ptr + sectlen + sizeof(UINT8), len - sectlen);
+                    
+                    newei->generation = Vcb->superblock.generation;
+                    newei->refcount--;
+                    
+                    delete_tree_item(Vcb, &tp, rollback);
+                    
+                    if (!insert_tree_item(Vcb, Vcb->extent_root, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, newei, neweilen, NULL, Irp, rollback)) {
+                        ERR("insert_tree_item failed\n");
+                        return STATUS_INTERNAL_ERROR;
+                    }
+                    
+                    return STATUS_SUCCESS;
+                }
+            } else if (type == TYPE_SHARED_BLOCK_REF) {
+                SHARED_BLOCK_REF* sectsbr = (SHARED_BLOCK_REF*)(ptr + sizeof(UINT8));
+                SHARED_BLOCK_REF* sbr = (SHARED_BLOCK_REF*)data;
+                ULONG neweilen;
+                EXTENT_ITEM* newei;
+                
+                if (sectsbr->offset == sbr->offset) {
+                    if (ei->refcount == 1) {
+                        delete_tree_item(Vcb, &tp, rollback);
+                        return STATUS_SUCCESS;
+                    }
+                    
+                    neweilen = tp.item->size - sizeof(UINT8) - sectlen;
+                    
+                    newei = ExAllocatePoolWithTag(PagedPool, neweilen, ALLOC_TAG);
+                    if (!newei) {
+                        ERR("out of memory\n");
+                        return STATUS_INSUFFICIENT_RESOURCES;
+                    }
+                    
+                    RtlCopyMemory(newei, ei, ptr - tp.item->data);
+                    
+                    if (len > sectlen)
+                        RtlCopyMemory((UINT8*)newei + (ptr - tp.item->data), ptr + sectlen + sizeof(UINT8), len - sectlen);
+                    
+                    newei->generation = Vcb->superblock.generation;
+                    newei->refcount--;
+                    
+                    delete_tree_item(Vcb, &tp, rollback);
+                    
+                    if (!insert_tree_item(Vcb, Vcb->extent_root, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, newei, neweilen, NULL, Irp, rollback)) {
+                        ERR("insert_tree_item failed\n");
+                        return STATUS_INTERNAL_ERROR;
+                    }
+                    
                     return STATUS_SUCCESS;
                 }
             } else {
@@ -676,7 +1127,7 @@ static NTSTATUS decrease_extent_refcount(device_extension* Vcb, UINT64 address,
         return Status;
     }
     
-    if (keycmp(&tp2.item->key, &searchkey)) {
+    if (keycmp(tp2.item->key, searchkey)) {
         ERR("(%llx,%x,%llx) not found\n", tp2.item->key.obj_id, tp2.item->key.obj_type, tp2.item->key.offset);
         return STATUS_INTERNAL_ERROR;
     }
@@ -780,6 +1231,80 @@ static NTSTATUS decrease_extent_refcount(device_extension* Vcb, UINT64 address,
                 return STATUS_INTERNAL_ERROR;
             }
             
+            return STATUS_SUCCESS;
+        } else {
+            ERR("error - collision?\n");
+            return STATUS_INTERNAL_ERROR;
+        }
+    } else if (type == TYPE_SHARED_BLOCK_REF) {
+        SHARED_BLOCK_REF* sectsbr = (SHARED_BLOCK_REF*)tp2.item->data;
+        SHARED_BLOCK_REF* sbr = (SHARED_BLOCK_REF*)data;
+        EXTENT_ITEM* newei;
+        
+        if (sectsbr->offset == sbr->offset) {
+            if (ei->refcount == 1) {
+                delete_tree_item(Vcb, &tp, rollback);
+                delete_tree_item(Vcb, &tp2, rollback);
+                return STATUS_SUCCESS;
+            }
+            
+            delete_tree_item(Vcb, &tp2, rollback);
+            
+            newei = ExAllocatePoolWithTag(PagedPool, tp.item->size, ALLOC_TAG);
+            if (!newei) {
+                ERR("out of memory\n");
+                return STATUS_INSUFFICIENT_RESOURCES;
+            }
+            
+            RtlCopyMemory(newei, tp.item->data, tp.item->size);
+
+            newei->generation = Vcb->superblock.generation;
+            newei->refcount -= rc;
+            
+            delete_tree_item(Vcb, &tp, rollback);
+            
+            if (!insert_tree_item(Vcb, Vcb->extent_root, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, newei, tp.item->size, NULL, Irp, rollback)) {
+                ERR("insert_tree_item failed\n");
+                return STATUS_INTERNAL_ERROR;
+            }
+            
+            return STATUS_SUCCESS;
+        } else {
+            ERR("error - collision?\n");
+            return STATUS_INTERNAL_ERROR;
+        }
+    } else if (type == TYPE_TREE_BLOCK_REF) {
+        TREE_BLOCK_REF* secttbr = (TREE_BLOCK_REF*)tp2.item->data;
+        TREE_BLOCK_REF* tbr = (TREE_BLOCK_REF*)data;
+        EXTENT_ITEM* newei;
+        
+        if (secttbr->offset == tbr->offset) {
+            if (ei->refcount == 1) {
+                delete_tree_item(Vcb, &tp, rollback);
+                delete_tree_item(Vcb, &tp2, rollback);
+                return STATUS_SUCCESS;
+            }
+            
+            delete_tree_item(Vcb, &tp2, rollback);
+            
+            newei = ExAllocatePoolWithTag(PagedPool, tp.item->size, ALLOC_TAG);
+            if (!newei) {
+                ERR("out of memory\n");
+                return STATUS_INSUFFICIENT_RESOURCES;
+            }
+            
+            RtlCopyMemory(newei, tp.item->data, tp.item->size);
+
+            newei->generation = Vcb->superblock.generation;
+            newei->refcount -= rc;
+            
+            delete_tree_item(Vcb, &tp, rollback);
+            
+            if (!insert_tree_item(Vcb, Vcb->extent_root, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, newei, tp.item->size, NULL, Irp, rollback)) {
+                ERR("insert_tree_item failed\n");
+                return STATUS_INTERNAL_ERROR;
+            }
+            
             return STATUS_SUCCESS;
         } else {
             ERR("error - collision?\n");
@@ -834,336 +1359,575 @@ NTSTATUS decrease_extent_refcount_data(device_extension* Vcb, UINT64 address, UI
     return decrease_extent_refcount(Vcb, address, size, TYPE_EXTENT_DATA_REF, &edr, NULL, 0, 0, Irp, rollback);
 }
 
-NTSTATUS decrease_extent_refcount_shared_data(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 treeaddr, UINT64 parent, PIRP Irp, LIST_ENTRY* rollback) {
-    SHARED_DATA_REF sdr;
-
-    sdr.offset = treeaddr;
-    sdr.count = 1;
+NTSTATUS decrease_extent_refcount_tree(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 root,
+                                       UINT8 level, PIRP Irp, LIST_ENTRY* rollback) {
+    TREE_BLOCK_REF tbr;
     
-    return decrease_extent_refcount(Vcb, address, size, TYPE_SHARED_DATA_REF, &sdr, NULL, 0, parent, Irp, rollback);
-}
-
-NTSTATUS decrease_extent_refcount_old(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 treeaddr, PIRP Irp, LIST_ENTRY* rollback) {
-    return decrease_extent_refcount(Vcb, address, size, TYPE_EXTENT_REF_V0, NULL, NULL, 0, treeaddr, Irp, rollback);
-}
-
-typedef struct {
-    UINT8 type;
-    void* data;
-    BOOL allocated;
-    UINT64 hash;
-    LIST_ENTRY list_entry;
-} extent_ref;
-
-static void free_extent_refs(LIST_ENTRY* extent_refs) {
-    while (!IsListEmpty(extent_refs)) {
-        LIST_ENTRY* le = RemoveHeadList(extent_refs);
-        extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
-        
-        if (er->allocated)
-            ExFreePool(er->data);
-        
-        ExFreePool(er);
-    }
+    tbr.offset = root;
+    
+    return decrease_extent_refcount(Vcb, address, size, TYPE_TREE_BLOCK_REF, &tbr, NULL/*FIXME*/, level, 0, Irp, rollback);
 }
 
-static NTSTATUS add_data_extent_ref(LIST_ENTRY* extent_refs, UINT64 tree_id, UINT64 obj_id, UINT64 offset) {
-    extent_ref* er2;
+static UINT64 find_extent_data_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 root, UINT64 objid, UINT64 offset, PIRP Irp) {
+    NTSTATUS Status;
+    KEY searchkey;
+    traverse_ptr tp;
     EXTENT_DATA_REF* edr;
-    LIST_ENTRY* le;
     
-    if (!IsListEmpty(extent_refs)) {
-        le = extent_refs->Flink;
+    searchkey.obj_id = address;
+    searchkey.obj_type = TYPE_EXTENT_ITEM;
+    searchkey.offset = 0xffffffffffffffff;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return 0;
+    }
+    
+    if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
+        TRACE("could not find address %llx in extent tree\n", address);
+        return 0;
+    }
+    
+    if (tp.item->key.offset != size) {
+        ERR("extent %llx had size %llx, not %llx as expected\n", address, tp.item->key.offset, size);
+        return 0;
+    }
+    
+    if (tp.item->size >= sizeof(EXTENT_ITEM)) {
+        EXTENT_ITEM* ei = (EXTENT_ITEM*)tp.item->data;
+        UINT32 len = tp.item->size - sizeof(EXTENT_ITEM);
+        UINT8* ptr = (UINT8*)&ei[1];
         
-        while (le != extent_refs) {
-            extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+        while (len > 0) {
+            UINT8 secttype = *ptr;
+            ULONG sectlen = get_extent_data_len(secttype);
+            UINT64 sectcount = get_extent_data_refcount(secttype, ptr + sizeof(UINT8));
+            
+            len--;
+            
+            if (sectlen > len) {
+                ERR("(%llx,%x,%llx): %x bytes left, expecting at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, len, sectlen);
+                return 0;
+            }
+
+            if (sectlen == 0) {
+                ERR("(%llx,%x,%llx): unrecognized extent type %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, secttype);
+                return 0;
+            }
             
-            if (er->type == TYPE_EXTENT_DATA_REF) {
-                edr = (EXTENT_DATA_REF*)er->data;
+            if (secttype == TYPE_EXTENT_DATA_REF) {
+                EXTENT_DATA_REF* sectedr = (EXTENT_DATA_REF*)(ptr + sizeof(UINT8));
                 
-                if (edr->root == tree_id && edr->objid == obj_id && edr->offset == offset) {
-                    edr->count++;
-                    return STATUS_SUCCESS;
-                }
+                if (sectedr->root == root && sectedr->objid == objid && sectedr->offset == offset)
+                    return sectcount;
             }
             
-            le = le->Flink;
+            len -= sectlen;
+            ptr += sizeof(UINT8) + sectlen;
         }
     }
     
-    er2 = ExAllocatePoolWithTag(PagedPool, sizeof(extent_ref), ALLOC_TAG);
-    if (!er2) {
-        ERR("out of memory\n");
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
+    searchkey.obj_id = address;
+    searchkey.obj_type = TYPE_EXTENT_DATA_REF;
+    searchkey.offset = get_extent_data_ref_hash2(root, objid, offset);
     
-    edr = ExAllocatePoolWithTag(PagedPool, sizeof(EXTENT_DATA_REF), ALLOC_TAG);
-    if (!edr) {
-        ERR("out of memory\n");
-        ExFreePool(er2);
-        return STATUS_INSUFFICIENT_RESOURCES;
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return 0;
     }
     
-    edr->root = tree_id;
-    edr->objid = obj_id;
-    edr->offset = offset;
-    edr->count = 1; // FIXME - not necessarily
-    
-    er2->type = TYPE_EXTENT_DATA_REF;
-    er2->data = edr;
-    er2->allocated = TRUE;
-    
-    InsertTailList(extent_refs, &er2->list_entry);
+    if (!keycmp(searchkey, tp.item->key)) {    
+        if (tp.item->size < sizeof(EXTENT_DATA_REF))
+            ERR("(%llx,%x,%llx) has size %u, not %u as expected\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(EXTENT_DATA_REF));
+        else {    
+            edr = (EXTENT_DATA_REF*)tp.item->data;
+            
+            return edr->count;
+        }
+    }
     
-    return STATUS_SUCCESS;
+    return 0;
 }
 
-static NTSTATUS construct_extent_item(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 flags, LIST_ENTRY* extent_refs, PIRP Irp, LIST_ENTRY* rollback) {
-    LIST_ENTRY *le, *next_le;
-    UINT64 refcount;
-    ULONG inline_len;
-    BOOL all_inline = TRUE;
-    extent_ref* first_noninline;
+UINT64 get_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, PIRP Irp) {
+    KEY searchkey;
+    traverse_ptr tp;
+    NTSTATUS Status;
     EXTENT_ITEM* ei;
-    UINT8* siptr;
     
-    if (IsListEmpty(extent_refs)) {
-        WARN("no extent refs found\n");
-        return STATUS_SUCCESS;
+    searchkey.obj_id = address;
+    searchkey.obj_type = Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA ? TYPE_METADATA_ITEM : TYPE_EXTENT_ITEM;
+    searchkey.offset = 0xffffffffffffffff;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return 0;
     }
     
-    refcount = 0;
-    inline_len = sizeof(EXTENT_ITEM);
+    if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA && tp.item->key.obj_id == address &&
+        tp.item->key.obj_type == TYPE_METADATA_ITEM && tp.item->size >= sizeof(EXTENT_ITEM)) {
+        ei = (EXTENT_ITEM*)tp.item->data;
     
-    le = extent_refs->Flink;
-    while (le != extent_refs) {
-        extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
-        UINT64 rc;
-        
-        next_le = le->Flink;
-        
-        rc = get_extent_data_refcount(er->type, er->data);
-        
-        if (rc == 0) {
-            if (er->allocated)
-                ExFreePool(er->data);
-            
-            RemoveEntryList(&er->list_entry);
-            
-            ExFreePool(er);
-        } else {
-            ULONG extlen = get_extent_data_len(er->type);
-            
-            refcount += rc;
-            
-            if (er->type == TYPE_EXTENT_DATA_REF)
-                er->hash = get_extent_data_ref_hash(er->data);
-            else
-                er->hash = 0;
-            
-            if (all_inline) {
-                if (inline_len + 1 + extlen > Vcb->superblock.node_size / 4) {
-                    all_inline = FALSE;
-                    first_noninline = er;
-                } else
-                    inline_len += extlen + 1;
-            }
-        }
+        return ei->refcount;
+    }
+    
+    if (tp.item->key.obj_id != address || tp.item->key.obj_type != TYPE_EXTENT_ITEM) {
+        ERR("couldn't find (%llx,%x,%llx) in extent tree\n", address, TYPE_EXTENT_ITEM, size);
+        return 0;
+    } else if (tp.item->key.offset != size) {
+        ERR("extent %llx had size %llx, not %llx as expected\n", address, tp.item->key.offset, size);
+        return 0;
+    }
+    
+    if (tp.item->size == sizeof(EXTENT_ITEM_V0)) {
+        EXTENT_ITEM_V0* eiv0 = (EXTENT_ITEM_V0*)tp.item->data;
         
-        le = next_le;
+        return eiv0->refcount;
+    } else if (tp.item->size < sizeof(EXTENT_ITEM)) {
+        ERR("(%llx,%x,%llx) was %x bytes, expected at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type,
+                                                                       tp.item->key.offset, tp.item->size, sizeof(EXTENT_DATA));
+        return 0;
     }
     
-    ei = ExAllocatePoolWithTag(PagedPool, inline_len, ALLOC_TAG);
-    if (!ei) {
-        ERR("out of memory\n");
-        return STATUS_INSUFFICIENT_RESOURCES;
+    ei = (EXTENT_ITEM*)tp.item->data;
+    
+    return ei->refcount;
+}
+
+BOOL is_extent_unique(device_extension* Vcb, UINT64 address, UINT64 size, PIRP Irp) {
+    KEY searchkey;
+    traverse_ptr tp, next_tp;
+    NTSTATUS Status;
+    UINT64 rc, rcrun, root = 0, inode = 0;
+    UINT32 len;
+    EXTENT_ITEM* ei;
+    UINT8* ptr;
+    BOOL b;
+    
+    rc = get_extent_refcount(Vcb, address, size, Irp);
+
+    if (rc == 1)
+        return TRUE;
+    
+    if (rc == 0)
+        return FALSE;
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = TYPE_EXTENT_ITEM;
+    searchkey.offset = size;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+    if (!NT_SUCCESS(Status)) {
+        WARN("error - find_item returned %08x\n", Status);
+        return FALSE;
     }
     
-    ei->refcount = refcount;
-    ei->generation = Vcb->superblock.generation;
-    ei->flags = flags;
+    if (keycmp(tp.item->key, searchkey)) {
+        WARN("could not find (%llx,%x,%llx)\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
+        return FALSE;
+    }
     
-    // Do we need to sort the inline extent refs? The Linux driver doesn't seem to bother.
+    if (tp.item->size == sizeof(EXTENT_ITEM_V0))
+        return FALSE;
     
-    siptr = (UINT8*)&ei[1];
-    le = extent_refs->Flink;
-    while (le != extent_refs) {
-        extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
-        ULONG extlen = get_extent_data_len(er->type);
+    if (tp.item->size < sizeof(EXTENT_ITEM)) {
+        WARN("(%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(EXTENT_ITEM));
+        return FALSE;
+    }
+    
+    ei = (EXTENT_ITEM*)tp.item->data;
+    
+    len = tp.item->size - sizeof(EXTENT_ITEM);
+    ptr = (UINT8*)&ei[1];
+    
+    if (ei->flags & EXTENT_ITEM_TREE_BLOCK) {
+        if (tp.item->size < sizeof(EXTENT_ITEM) + sizeof(EXTENT_ITEM2)) {
+            WARN("(%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(EXTENT_ITEM) + sizeof(EXTENT_ITEM2));
+            return FALSE;
+        }
         
-        if (!all_inline && er == first_noninline)
-            break;
+        len -= sizeof(EXTENT_ITEM2);
+        ptr += sizeof(EXTENT_ITEM2);
+    }
+    
+    rcrun = 0;
+    
+    // Loop through inline extent entries
+    
+    while (len > 0) {
+        UINT8 secttype = *ptr;
+        ULONG sectlen = get_extent_data_len(secttype);
+        UINT64 sectcount = get_extent_data_refcount(secttype, ptr + sizeof(UINT8));
         
-        *siptr = er->type;
-        siptr++;
+        len--;
         
-        if (extlen > 0) {
-            RtlCopyMemory(siptr, er->data, extlen);
-            siptr += extlen;
+        if (sectlen > len) {
+            WARN("(%llx,%x,%llx): %x bytes left, expecting at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, len, sectlen);
+            return FALSE;
         }
-         
-        le = le->Flink;
+
+        if (sectlen == 0) {
+            WARN("(%llx,%x,%llx): unrecognized extent type %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, secttype);
+            return FALSE;
+        }
+        
+        if (secttype == TYPE_EXTENT_DATA_REF) {
+            EXTENT_DATA_REF* sectedr = (EXTENT_DATA_REF*)(ptr + sizeof(UINT8));
+            
+            if (root == 0 && inode == 0) {
+                root = sectedr->root;
+                inode = sectedr->objid;
+            } else if (root != sectedr->root || inode != sectedr->objid)
+                return FALSE;
+        } else
+            return FALSE;
+        
+        len -= sectlen;
+        ptr += sizeof(UINT8) + sectlen;
+        rcrun += sectcount;
     }
     
-    if (!insert_tree_item(Vcb, Vcb->extent_root, address, TYPE_EXTENT_ITEM, size, ei, inline_len, NULL, Irp, rollback)) {
-        ERR("error - failed to insert item\n");
-        ExFreePool(ei);
-        return STATUS_INTERNAL_ERROR;
-    }
+    if (rcrun == rc)
+        return TRUE;
+
+    // Loop through non-inlines if some refs still unaccounted for
     
-    if (!all_inline) {
-        le = &first_noninline->list_entry;
+    do {
+        b = find_next_item(Vcb, &tp, &next_tp, FALSE, Irp);
         
-        while (le != extent_refs) {
-            extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+        if (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == TYPE_EXTENT_DATA_REF) {
+            EXTENT_DATA_REF* edr = (EXTENT_DATA_REF*)tp.item->data;
             
-            if (!insert_tree_item(Vcb, Vcb->extent_root, address, er->type, er->hash, er->data, get_extent_data_len(er->type), NULL, Irp, rollback)) {
-                ERR("error - failed to insert item\n");
-                return STATUS_INTERNAL_ERROR;
+            if (tp.item->size < sizeof(EXTENT_DATA_REF)) {
+                WARN("(%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(EXTENT_ITEM) + sizeof(EXTENT_ITEM2));
+                return FALSE;
             }
             
-            er->allocated = FALSE;
+            if (root == 0 && inode == 0) {
+                root = edr->root;
+                inode = edr->objid;
+            } else if (root != edr->root || inode != edr->objid)
+                return FALSE;
             
-            le = le->Flink;
+            rcrun += edr->count;
         }
-    }
+        
+        if (rcrun == rc)
+            return TRUE;
+        
+        if (b) {
+            tp = next_tp;
+            
+            if (tp.item->key.obj_id > searchkey.obj_id)
+                break;
+        }
+    } while (b);
     
-    return STATUS_SUCCESS;
+    // If we reach this point, there's still some refs unaccounted for somewhere.
+    // Return FALSE in case we mess things up elsewhere.
+    
+    return FALSE;
 }
 
-static NTSTATUS populate_extent_refs_from_tree(device_extension* Vcb, UINT64 tree_address, UINT64 extent_address, LIST_ENTRY* extent_refs) {
-    UINT8* buf;
-    tree_header* th;
+UINT64 get_extent_flags(device_extension* Vcb, UINT64 address, PIRP Irp) {
+    KEY searchkey;
+    traverse_ptr tp;
     NTSTATUS Status;
+    EXTENT_ITEM* ei;
     
-    buf = ExAllocatePoolWithTag(PagedPool, Vcb->superblock.node_size, ALLOC_TAG);
-    if (!buf) {
-        ERR("out of memory\n");
-        return STATUS_INSUFFICIENT_RESOURCES;
-    }
-
-    Status = read_data(Vcb, tree_address, Vcb->superblock.node_size, NULL, TRUE, buf, NULL, NULL);
+    searchkey.obj_id = address;
+    searchkey.obj_type = Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA ? TYPE_METADATA_ITEM : TYPE_EXTENT_ITEM;
+    searchkey.offset = 0xffffffffffffffff;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
     if (!NT_SUCCESS(Status)) {
-        ERR("read_data returned %08x\n", Status);
-        ExFreePool(buf);
-        return Status;
+        ERR("error - find_item returned %08x\n", Status);
+        return 0;
     }
     
-    th = (tree_header*)buf;
-
-    if (th->level == 0) {
-        UINT32 i;
-        leaf_node* ln = (leaf_node*)&th[1];
-        
-        for (i = 0; i < th->num_items; i++) {
-            if (ln[i].key.obj_type == TYPE_EXTENT_DATA && ln[i].size >= sizeof(EXTENT_DATA) && ln[i].offset + ln[i].size <= Vcb->superblock.node_size - sizeof(tree_header)) {
-                EXTENT_DATA* ed = (EXTENT_DATA*)(((UINT8*)&th[1]) + ln[i].offset);
-                
-                if ((ed->type == EXTENT_TYPE_REGULAR || ed->type == EXTENT_TYPE_PREALLOC) && ln[i].size >= sizeof(EXTENT_DATA) - 1 + sizeof(EXTENT_DATA2)) {
-                    EXTENT_DATA2* ed2 = (EXTENT_DATA2*)&ed->data[0];
-                    
-                    if (ed2->address == extent_address) {
-                        Status = add_data_extent_ref(extent_refs, th->tree_id, ln[i].key.obj_id, ln[i].key.offset);
-                        if (!NT_SUCCESS(Status)) {
-                            ERR("add_data_extent_ref returned %08x\n", Status);
-                            ExFreePool(buf);
-                            return Status;
-                        }
-                    }
-                }
-            }
-        }
-    } else
-        WARN("shared data ref pointed to tree of level %x\n", th->level);
+    if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA && tp.item->key.obj_id == address &&
+        tp.item->key.obj_type == TYPE_METADATA_ITEM && tp.item->size >= sizeof(EXTENT_ITEM)) {
+        ei = (EXTENT_ITEM*)tp.item->data;
     
-    ExFreePool(buf);
+        return ei->flags;
+    }
     
-    return STATUS_SUCCESS;
+    if (tp.item->key.obj_id != address || tp.item->key.obj_type != TYPE_EXTENT_ITEM) {
+        ERR("couldn't find %llx in extent tree\n", address);
+        return 0;
+    }
+    
+    if (tp.item->size == sizeof(EXTENT_ITEM_V0))
+        return 0;
+    else if (tp.item->size < sizeof(EXTENT_ITEM)) {
+        ERR("(%llx,%x,%llx) was %x bytes, expected at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type,
+                                                                   tp.item->key.offset, tp.item->size, sizeof(EXTENT_DATA));
+        return 0;
+    }
+    
+    ei = (EXTENT_ITEM*)tp.item->data;
+    
+    return ei->flags;
 }
 
-NTSTATUS convert_old_data_extent(device_extension* Vcb, UINT64 address, UINT64 size, PIRP Irp, LIST_ENTRY* rollback) {
+void update_extent_flags(device_extension* Vcb, UINT64 address, UINT64 flags, PIRP Irp) {
     KEY searchkey;
-    traverse_ptr tp, next_tp;
-    BOOL b;
-    LIST_ENTRY extent_refs;
+    traverse_ptr tp;
     NTSTATUS Status;
+    EXTENT_ITEM* ei;
     
     searchkey.obj_id = address;
-    searchkey.obj_type = TYPE_EXTENT_ITEM;
-    searchkey.offset = size;
+    searchkey.obj_type = Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA ? TYPE_METADATA_ITEM : TYPE_EXTENT_ITEM;
+    searchkey.offset = 0xffffffffffffffff;
     
     Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
     if (!NT_SUCCESS(Status)) {
         ERR("error - find_item returned %08x\n", Status);
-        return Status;
+        return;
     }
     
-    if (keycmp(&tp.item->key, &searchkey)) {
-        WARN("extent item not found for address %llx, size %llx\n", address, size);
-        return STATUS_SUCCESS;
+    if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA && tp.item->key.obj_id == address &&
+        tp.item->key.obj_type == TYPE_METADATA_ITEM && tp.item->size >= sizeof(EXTENT_ITEM)) {
+        ei = (EXTENT_ITEM*)tp.item->data;
+        ei->flags = flags;
+        return;
     }
     
-    if (tp.item->size != sizeof(EXTENT_ITEM_V0)) {
-        TRACE("extent does not appear to be old - returning STATUS_SUCCESS\n");
-        return STATUS_SUCCESS;
+    if (tp.item->key.obj_id != address || tp.item->key.obj_type != TYPE_EXTENT_ITEM) {
+        ERR("couldn't find %llx in extent tree\n", address);
+        return;
     }
     
-    delete_tree_item(Vcb, &tp, rollback);
+    if (tp.item->size == sizeof(EXTENT_ITEM_V0))
+        return;
+    else if (tp.item->size < sizeof(EXTENT_ITEM)) {
+        ERR("(%llx,%x,%llx) was %x bytes, expected at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type,
+                                                                   tp.item->key.offset, tp.item->size, sizeof(EXTENT_DATA));
+        return;
+    }
     
-    searchkey.obj_id = address;
-    searchkey.obj_type = TYPE_EXTENT_REF_V0;
-    searchkey.offset = 0;
+    ei = (EXTENT_ITEM*)tp.item->data;
+    ei->flags = flags;
+}
+
+static changed_extent* get_changed_extent_item(chunk* c, UINT64 address, UINT64 size, BOOL no_csum) {
+    LIST_ENTRY* le;
+    changed_extent* ce;
     
-    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        return Status;
+    le = c->changed_extents.Flink;
+    while (le != &c->changed_extents) {
+        ce = CONTAINING_RECORD(le, changed_extent, list_entry);
+        
+        if (ce->address == address && ce->size == size)
+            return ce;
+        
+        le = le->Flink;
     }
     
-    InitializeListHead(&extent_refs);
+    ce = ExAllocatePoolWithTag(PagedPool, sizeof(changed_extent), ALLOC_TAG);
+    if (!ce) {
+        ERR("out of memory\n");
+        return NULL;
+    }
     
-    do {
-        b = find_next_item(Vcb, &tp, &next_tp, FALSE, Irp);
+    ce->address = address;
+    ce->size = size;
+    ce->old_size = size;
+    ce->count = 0;
+    ce->old_count = 0;
+    ce->no_csum = no_csum;
+    ce->superseded = FALSE;
+    InitializeListHead(&ce->refs);
+    InitializeListHead(&ce->old_refs);
+    
+    InsertTailList(&c->changed_extents, &ce->list_entry);
+    
+    return ce;
+}
+
+NTSTATUS update_changed_extent_ref(device_extension* Vcb, chunk* c, UINT64 address, UINT64 size, UINT64 root, UINT64 objid, UINT64 offset, signed long long count,
+                                   BOOL no_csum, BOOL superseded, PIRP Irp) {
+    LIST_ENTRY* le;
+    changed_extent* ce;
+    changed_extent_ref* cer;
+    NTSTATUS Status;
+    KEY searchkey;
+    traverse_ptr tp;
+    UINT64 old_count;
+    
+    ExAcquireResourceExclusiveLite(&c->changed_extents_lock, TRUE);
+    
+    ce = get_changed_extent_item(c, address, size, no_csum);
+    
+    if (!ce) {
+        ERR("get_changed_extent_item failed\n");
+        Status = STATUS_INTERNAL_ERROR;
+        goto end;
+    }
+    
+    if (IsListEmpty(&ce->refs) && IsListEmpty(&ce->old_refs)) { // new entry
+        searchkey.obj_id = address;
+        searchkey.obj_type = TYPE_EXTENT_ITEM;
+        searchkey.offset = 0xffffffffffffffff;
         
-        if (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
-            Status = populate_extent_refs_from_tree(Vcb, tp.item->key.offset, address, &extent_refs);
-            if (!NT_SUCCESS(Status)) {
-                ERR("populate_extent_refs_from_tree returned %08x\n", Status);
-                return Status;
-            }
+        Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+        if (!NT_SUCCESS(Status)) {
+            ERR("error - find_item returned %08x\n", Status);
+            goto end;
+        }
+        
+        if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
+            ERR("could not find address %llx in extent tree\n", address);
+            Status = STATUS_INTERNAL_ERROR;
+            goto end;
+        }
+        
+        if (tp.item->key.offset != size) {
+            ERR("extent %llx had size %llx, not %llx as expected\n", address, tp.item->key.offset, size);
+            Status = STATUS_INTERNAL_ERROR;
+            goto end;
+        }
+        
+        if (tp.item->size == sizeof(EXTENT_ITEM_V0)) {
+            EXTENT_ITEM_V0* eiv0 = (EXTENT_ITEM_V0*)tp.item->data;
             
-            delete_tree_item(Vcb, &tp, rollback);
+            ce->count = ce->old_count = eiv0->refcount;
+        } else if (tp.item->size >= sizeof(EXTENT_ITEM)) {
+            EXTENT_ITEM* ei = (EXTENT_ITEM*)tp.item->data;
+            
+            ce->count = ce->old_count = ei->refcount;
+        } else {
+            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(EXTENT_ITEM));
+            Status = STATUS_INTERNAL_ERROR;
+            goto end;
         }
+    }
+    
+    le = ce->refs.Flink;
+    while (le != &ce->refs) {
+        cer = CONTAINING_RECORD(le, changed_extent_ref, list_entry);
         
-        if (b) {
-            tp = next_tp;
+        if (cer->type == TYPE_EXTENT_DATA_REF && cer->edr.root == root && cer->edr.objid == objid && cer->edr.offset == offset) {
+            ce->count += count;
+            cer->edr.count += count;
+            Status = STATUS_SUCCESS;
             
-            if (tp.item->key.obj_id > searchkey.obj_id || tp.item->key.obj_type > searchkey.obj_type)
-                break;
+            if (superseded)
+                ce->superseded = TRUE;
+            
+            goto end;
         }
-    } while (b);
+        
+        le = le->Flink;
+    }
     
-    Status = construct_extent_item(Vcb, address, size, EXTENT_ITEM_DATA, &extent_refs, Irp, rollback);
-    if (!NT_SUCCESS(Status)) {
-        ERR("construct_extent_item returned %08x\n", Status);
-        free_extent_refs(&extent_refs);
-        return Status;
+    old_count = find_extent_data_refcount(Vcb, address, size, root, objid, offset, Irp);
+    
+    if (old_count > 0) {
+        cer = ExAllocatePoolWithTag(PagedPool, sizeof(changed_extent_ref), ALLOC_TAG);
+    
+        if (!cer) {
+            ERR("out of memory\n");
+            Status = STATUS_INSUFFICIENT_RESOURCES;
+            goto end;
+        }
+        
+        cer->type = TYPE_EXTENT_DATA_REF;
+        cer->edr.root = root;
+        cer->edr.objid = objid;
+        cer->edr.offset = offset;
+        cer->edr.count = old_count;
+        
+        InsertTailList(&ce->old_refs, &cer->list_entry);
     }
     
-    free_extent_refs(&extent_refs);
+    cer = ExAllocatePoolWithTag(PagedPool, sizeof(changed_extent_ref), ALLOC_TAG);
     
-    return STATUS_SUCCESS;
+    if (!cer) {
+        ERR("out of memory\n");
+        Status = STATUS_INSUFFICIENT_RESOURCES;
+        goto end;
+    }
+    
+    cer->type = TYPE_EXTENT_DATA_REF;
+    cer->edr.root = root;
+    cer->edr.objid = objid;
+    cer->edr.offset = offset;
+    cer->edr.count = old_count + count;
+    
+    InsertTailList(&ce->refs, &cer->list_entry);
+    
+    ce->count += count;
+    
+    if (superseded)
+        ce->superseded = TRUE;
+    
+    Status = STATUS_SUCCESS;
+    
+end:
+    ExReleaseResourceLite(&c->changed_extents_lock);
+    
+    return Status;
+}
+
+void add_changed_extent_ref(chunk* c, UINT64 address, UINT64 size, UINT64 root, UINT64 objid, UINT64 offset, UINT32 count, BOOL no_csum) {
+    changed_extent* ce;
+    changed_extent_ref* cer;
+    LIST_ENTRY* le;
+    
+    ce = get_changed_extent_item(c, address, size, no_csum);
+    
+    if (!ce) {
+        ERR("get_changed_extent_item failed\n");
+        return;
+    }
+    
+    le = ce->refs.Flink;
+    while (le != &ce->refs) {
+        cer = CONTAINING_RECORD(le, changed_extent_ref, list_entry);
+        
+        if (cer->type == TYPE_EXTENT_DATA_REF && cer->edr.root == root && cer->edr.objid == objid && cer->edr.offset == offset) {
+            ce->count += count;
+            cer->edr.count += count;
+            return;
+        }
+        
+        le = le->Flink;
+    }
+    
+    cer = ExAllocatePoolWithTag(PagedPool, sizeof(changed_extent_ref), ALLOC_TAG);
+    
+    if (!cer) {
+        ERR("out of memory\n");
+        return;
+    }
+    
+    cer->type = TYPE_EXTENT_DATA_REF;
+    cer->edr.root = root;
+    cer->edr.objid = objid;
+    cer->edr.offset = offset;
+    cer->edr.count = count;
+    
+    InsertTailList(&ce->refs, &cer->list_entry);
+    
+    ce->count += count;
 }
 
-UINT64 find_extent_data_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 root, UINT64 objid, UINT64 offset, PIRP Irp) {
+UINT64 find_extent_shared_tree_refcount(device_extension* Vcb, UINT64 address, UINT64 parent, PIRP Irp) {
     NTSTATUS Status;
     KEY searchkey;
     traverse_ptr tp;
-    EXTENT_DATA_REF* edr;
-    BOOL old = FALSE;
+    UINT64 inline_rc;
+    EXTENT_ITEM* ei;
+    UINT32 len;
+    UINT8* ptr;
     
     searchkey.obj_id = address;
-    searchkey.obj_type = TYPE_EXTENT_ITEM;
+    searchkey.obj_type = Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA ? TYPE_METADATA_ITEM : TYPE_EXTENT_ITEM;
     searchkey.offset = 0xffffffffffffffff;
     
     Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
@@ -1172,82 +1936,75 @@ UINT64 find_extent_data_refcount(device_extension* Vcb, UINT64 address, UINT64 s
         return 0;
     }
     
-    if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
+    if (tp.item->key.obj_id != searchkey.obj_id || (tp.item->key.obj_type != TYPE_EXTENT_ITEM && tp.item->key.obj_type != TYPE_METADATA_ITEM)) {
         TRACE("could not find address %llx in extent tree\n", address);
         return 0;
     }
     
-    if (tp.item->key.offset != size) {
-        ERR("extent %llx had size %llx, not %llx as expected\n", address, tp.item->key.offset, size);
+    if (tp.item->key.obj_type == TYPE_EXTENT_ITEM && tp.item->key.offset != Vcb->superblock.node_size) {
+        ERR("extent %llx had size %llx, not %llx as expected\n", address, tp.item->key.offset, Vcb->superblock.node_size);
         return 0;
     }
     
-    if (tp.item->size >= sizeof(EXTENT_ITEM)) {
-        EXTENT_ITEM* ei = (EXTENT_ITEM*)tp.item->data;
-        UINT32 len = tp.item->size - sizeof(EXTENT_ITEM);
-        UINT8* ptr = (UINT8*)&ei[1];
+    if (tp.item->size < sizeof(EXTENT_ITEM)) {
+        ERR("(%llx,%x,%llx): size was %u, expected at least %u\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(EXTENT_ITEM));
+        return 0;
+    }
+    
+    ei = (EXTENT_ITEM*)tp.item->data;
+    inline_rc = 0;
+    
+    len = tp.item->size - sizeof(EXTENT_ITEM);
+    ptr = (UINT8*)&ei[1];
+    
+    if (searchkey.obj_type == TYPE_EXTENT_ITEM && ei->flags & EXTENT_ITEM_TREE_BLOCK) {
+        if (tp.item->size < sizeof(EXTENT_ITEM) + sizeof(EXTENT_ITEM2)) {
+            ERR("(%llx,%x,%llx): size was %u, expected at least %u\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset,
+                                                                       tp.item->size, sizeof(EXTENT_ITEM) + sizeof(EXTENT_ITEM2));
+            return 0;
+        }
         
-        while (len > 0) {
-            UINT8 secttype = *ptr;
-            ULONG sectlen = get_extent_data_len(secttype);
-            UINT64 sectcount = get_extent_data_refcount(secttype, ptr + sizeof(UINT8));
-            
-            len--;
-            
-            if (sectlen > len) {
-                ERR("(%llx,%x,%llx): %x bytes left, expecting at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, len, sectlen);
-                return 0;
-            }
+        len -= sizeof(EXTENT_ITEM2);
+        ptr += sizeof(EXTENT_ITEM2);
+    }
+    
+    while (len > 0) {
+        UINT8 secttype = *ptr;
+        ULONG sectlen = get_extent_data_len(secttype);
+        UINT64 sectcount = get_extent_data_refcount(secttype, ptr + sizeof(UINT8));
+        
+        len--;
+        
+        if (sectlen > len) {
+            ERR("(%llx,%x,%llx): %x bytes left, expecting at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, len, sectlen);
+            return 0;
+        }
 
-            if (sectlen == 0) {
-                ERR("(%llx,%x,%llx): unrecognized extent type %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, secttype);
-                return 0;
-            }
-            
-            if (secttype == TYPE_EXTENT_DATA_REF) {
-                EXTENT_DATA_REF* sectedr = (EXTENT_DATA_REF*)(ptr + sizeof(UINT8));
-                
-                if (sectedr->root == root && sectedr->objid == objid && sectedr->offset == offset)
-                    return sectcount;
-            } else if (secttype == TYPE_SHARED_DATA_REF) {
-                SHARED_DATA_REF* sectsdr = (SHARED_DATA_REF*)(ptr + sizeof(UINT8));
-                BOOL found = FALSE;
-                LIST_ENTRY* le;
-                
-                le = Vcb->shared_extents.Flink;
-                while (le != &Vcb->shared_extents) {
-                    shared_data* sd = CONTAINING_RECORD(le, shared_data, list_entry);
-                    
-                    if (sd->address == sectsdr->offset) {
-                        LIST_ENTRY* le2 = sd->entries.Flink;
-                        while (le2 != &sd->entries) {
-                            shared_data_entry* sde = CONTAINING_RECORD(le2, shared_data_entry, list_entry);
-                            
-                            if (sde->edr.root == root && sde->edr.objid == objid && sde->edr.offset == offset)
-                                return sde->edr.count;
-                            
-                            le2 = le2->Flink;
-                        }
-                        found = TRUE;
-                        break;
-                    }
-                    
-                    le = le->Flink;
-                }
-                
-                if (!found)
-                    WARN("shared data extents not loaded for tree at %llx\n", sectsdr->offset);        
-            }
+        if (sectlen == 0) {
+            ERR("(%llx,%x,%llx): unrecognized extent type %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, secttype);
+            return 0;
+        }
+        
+        if (secttype == TYPE_SHARED_BLOCK_REF) {
+            SHARED_BLOCK_REF* sectsbr = (SHARED_BLOCK_REF*)(ptr + sizeof(UINT8));
             
-            len -= sectlen;
-            ptr += sizeof(UINT8) + sectlen;
+            if (sectsbr->offset == parent)
+                return 1;
         }
-    } else if (tp.item->size == sizeof(EXTENT_ITEM_V0))
-        old = TRUE;
+        
+        len -= sectlen;
+        ptr += sizeof(UINT8) + sectlen;
+        inline_rc += sectcount;
+    }
+    
+    // FIXME - what if old?
+    
+    if (inline_rc == ei->refcount)
+        return 0;
     
     searchkey.obj_id = address;
-    searchkey.obj_type = TYPE_EXTENT_DATA_REF;
-    searchkey.offset = get_extent_data_ref_hash2(root, objid, offset);
+    searchkey.obj_type = TYPE_SHARED_BLOCK_REF;
+    searchkey.offset = parent;
     
     Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
     if (!NT_SUCCESS(Status)) {
@@ -1255,139 +2012,102 @@ UINT64 find_extent_data_refcount(device_extension* Vcb, UINT64 address, UINT64 s
         return 0;
     }
     
-    if (!keycmp(&searchkey, &tp.item->key)) {    
-        if (tp.item->size < sizeof(EXTENT_DATA_REF))
-            ERR("(%llx,%x,%llx) has size %u, not %u as expected\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(EXTENT_DATA_REF));
-        else {    
-            edr = (EXTENT_DATA_REF*)tp.item->data;
-            
-            return edr->count;
-        }
+    if (!keycmp(searchkey, tp.item->key)) {    
+        if (tp.item->size < sizeof(SHARED_BLOCK_REF))
+            ERR("(%llx,%x,%llx) has size %u, not %u as expected\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(SHARED_BLOCK_REF));
+        else
+            return 1;
+    }
+    
+    return 0;
+}
+
+UINT64 find_extent_shared_data_refcount(device_extension* Vcb, UINT64 address, UINT64 parent, PIRP Irp) {
+    NTSTATUS Status;
+    KEY searchkey;
+    traverse_ptr tp;
+    UINT64 inline_rc;
+    EXTENT_ITEM* ei;
+    UINT32 len;
+    UINT8* ptr;
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA ? TYPE_METADATA_ITEM : TYPE_EXTENT_ITEM;
+    searchkey.offset = 0xffffffffffffffff;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return 0;
+    }
+    
+    if (tp.item->key.obj_id != searchkey.obj_id || (tp.item->key.obj_type != TYPE_EXTENT_ITEM && tp.item->key.obj_type != TYPE_METADATA_ITEM)) {
+        TRACE("could not find address %llx in extent tree\n", address);
+        return 0;
     }
-     
-    if (old) {
-        BOOL b;
+    
+    if (tp.item->size < sizeof(EXTENT_ITEM)) {
+        ERR("(%llx,%x,%llx): size was %u, expected at least %u\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(EXTENT_ITEM));
+        return 0;
+    }
+    
+    ei = (EXTENT_ITEM*)tp.item->data;
+    inline_rc = 0;
+    
+    len = tp.item->size - sizeof(EXTENT_ITEM);
+    ptr = (UINT8*)&ei[1];
+    
+    while (len > 0) {
+        UINT8 secttype = *ptr;
+        ULONG sectlen = get_extent_data_len(secttype);
+        UINT64 sectcount = get_extent_data_refcount(secttype, ptr + sizeof(UINT8));
         
-        searchkey.obj_id = address;
-        searchkey.obj_type = TYPE_EXTENT_REF_V0;
-        searchkey.offset = 0;
+        len--;
         
-        Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
-        if (!NT_SUCCESS(Status)) {
-            ERR("error - find_item returned %08x\n", Status);
+        if (sectlen > len) {
+            ERR("(%llx,%x,%llx): %x bytes left, expecting at least %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, len, sectlen);
             return 0;
         }
-        
-        do {
-            traverse_ptr next_tp;
-            
-            b = find_next_item(Vcb, &tp, &next_tp, FALSE, Irp);
-            
-            if (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
-                if (tp.item->size >= sizeof(EXTENT_REF_V0)) {
-                    EXTENT_REF_V0* erv0 = (EXTENT_REF_V0*)tp.item->data;
-                    
-                    if (erv0->root == root && erv0->objid == objid) {
-                        LIST_ENTRY* le;
-                        BOOL found = FALSE;
-                    
-                        le = Vcb->shared_extents.Flink;
-                        while (le != &Vcb->shared_extents) {
-                            shared_data* sd = CONTAINING_RECORD(le, shared_data, list_entry);
-                            
-                            if (sd->address == tp.item->key.offset) {
-                                LIST_ENTRY* le2 = sd->entries.Flink;
-                                while (le2 != &sd->entries) {
-                                    shared_data_entry* sde = CONTAINING_RECORD(le2, shared_data_entry, list_entry);
-                                    
-                                    if (sde->edr.root == root && sde->edr.objid == objid && sde->edr.offset == offset)
-                                        return sde->edr.count;
-                                    
-                                    le2 = le2->Flink;
-                                }
-                                found = TRUE;
-                                break;
-                            }
-                            
-                            le = le->Flink;
-                        }
-                        
-                        if (!found)
-                            WARN("shared data extents not loaded for tree at %llx\n", tp.item->key.offset);
-                    }
-                } else {
-                    ERR("(%llx,%x,%llx) was %x bytes, not %x as expected\n", tp.item->key.obj_id, tp.item->key.obj_type,
-                        tp.item->key.offset, tp.item->size, sizeof(EXTENT_REF_V0));
-                }
-            }
-            
-            if (b) {
-                tp = next_tp;
-                
-                if (tp.item->key.obj_id > searchkey.obj_id || (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type > searchkey.obj_type))
-                    break;
-            }
-        } while (b);
-    } else {
-        BOOL b;
-        
-        searchkey.obj_id = address;
-        searchkey.obj_type = TYPE_SHARED_DATA_REF;
-        searchkey.offset = 0;
-        
-        Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
-        if (!NT_SUCCESS(Status)) {
-            ERR("error - find_item returned %08x\n", Status);
+
+        if (sectlen == 0) {
+            ERR("(%llx,%x,%llx): unrecognized extent type %x\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, secttype);
             return 0;
         }
         
-        do {
-            traverse_ptr next_tp;
+        if (secttype == TYPE_SHARED_DATA_REF) {
+            SHARED_DATA_REF* sectsdr = (SHARED_DATA_REF*)(ptr + sizeof(UINT8));
             
-            b = find_next_item(Vcb, &tp, &next_tp, FALSE, Irp);
-            
-            if (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
-                if (tp.item->size >= sizeof(SHARED_DATA_REF)) {
-                    SHARED_DATA_REF* sdr = (SHARED_DATA_REF*)tp.item->data;
-                    LIST_ENTRY* le;
-                    BOOL found = FALSE;
-                    
-                    le = Vcb->shared_extents.Flink;
-                    while (le != &Vcb->shared_extents) {
-                        shared_data* sd = CONTAINING_RECORD(le, shared_data, list_entry);
-                        
-                        if (sd->address == sdr->offset) {
-                            LIST_ENTRY* le2 = sd->entries.Flink;
-                            while (le2 != &sd->entries) {
-                                shared_data_entry* sde = CONTAINING_RECORD(le2, shared_data_entry, list_entry);
-                                
-                                if (sde->edr.root == root && sde->edr.objid == objid && sde->edr.offset == offset)
-                                    return sde->edr.count;
-                                
-                                le2 = le2->Flink;
-                            }
-                            found = TRUE;
-                            break;
-                        }
-                        
-                        le = le->Flink;
-                    }
-
-                    if (!found)
-                        WARN("shared data extents not loaded for tree at %llx\n", sdr->offset);
-                } else {
-                    ERR("(%llx,%x,%llx) was %x bytes, not %x as expected\n", tp.item->key.obj_id, tp.item->key.obj_type,
-                        tp.item->key.offset, tp.item->size, sizeof(SHARED_DATA_REF));
-                }
-            }
-
-            if (b) {
-                tp = next_tp;
-
-                if (tp.item->key.obj_id > searchkey.obj_id || (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type > searchkey.obj_type))
-                    break;
-            }
-        } while (b);
+            if (sectsdr->offset == parent)
+                return sectsdr->count;
+        }
+        
+        len -= sectlen;
+        ptr += sizeof(UINT8) + sectlen;
+        inline_rc += sectcount;
+    }
+    
+    // FIXME - what if old?
+    
+    if (inline_rc == ei->refcount)
+        return 0;
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = TYPE_SHARED_DATA_REF;
+    searchkey.offset = parent;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE, Irp);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return 0;
+    }
+    
+    if (!keycmp(searchkey, tp.item->key)) {    
+        if (tp.item->size < sizeof(SHARED_DATA_REF))
+            ERR("(%llx,%x,%llx) has size %u, not %u as expected\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(SHARED_DATA_REF));
+        else {
+            SHARED_DATA_REF* sdr = (SHARED_DATA_REF*)tp.item->data;
+            return sdr->count;
+        }
     }
     
     return 0;
index 092ed28..e1e1272 100644 (file)
@@ -58,7 +58,46 @@ static NTSTATUS STDCALL set_basic_information(device_extension* Vcb, PIRP Irp, P
     // FIXME - what about subvol roots?
     
     // FIXME - link FILE_ATTRIBUTE_READONLY to st_mode
-    // FIXME - handle times == -1
+    
+    if (fbi->CreationTime.QuadPart == -1)
+        ccb->user_set_creation_time = TRUE;
+    else if (fbi->CreationTime.QuadPart != 0) {
+        win_time_to_unix(fbi->CreationTime, &fcb->inode_item.otime);
+        inode_item_changed = TRUE;
+        filter |= FILE_NOTIFY_CHANGE_CREATION;
+        
+        ccb->user_set_creation_time = TRUE;
+    }
+    
+    if (fbi->LastAccessTime.QuadPart == -1)
+        ccb->user_set_access_time = TRUE;
+    else if (fbi->LastAccessTime.QuadPart != 0) {
+        win_time_to_unix(fbi->LastAccessTime, &fcb->inode_item.st_atime);
+        inode_item_changed = TRUE;
+        filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
+        
+        ccb->user_set_access_time = TRUE;
+    }
+    
+    if (fbi->LastWriteTime.QuadPart == -1)
+        ccb->user_set_write_time = TRUE;
+    else if (fbi->LastWriteTime.QuadPart != 0) {
+        win_time_to_unix(fbi->LastWriteTime, &fcb->inode_item.st_mtime);
+        inode_item_changed = TRUE;
+        filter |= FILE_NOTIFY_CHANGE_LAST_WRITE;
+        
+        ccb->user_set_write_time = TRUE;
+    }
+    
+    if (fbi->ChangeTime.QuadPart == -1)
+        ccb->user_set_change_time = TRUE;
+    else if (fbi->ChangeTime.QuadPart != 0) {
+        win_time_to_unix(fbi->ChangeTime, &fcb->inode_item.st_ctime);
+        inode_item_changed = TRUE;
+        // no filter for this
+        
+        ccb->user_set_change_time = TRUE;
+    }
     
     // FileAttributes == 0 means don't set - undocumented, but seen in fastfat
     if (fbi->FileAttributes != 0) {
@@ -74,6 +113,9 @@ static NTSTATUS STDCALL set_basic_information(device_extension* Vcb, PIRP Irp, P
                 
         fcb->atts_changed = TRUE;
         
+        if (fcb->atts & FILE_ATTRIBUTE_REPARSE_POINT)
+            fbi->FileAttributes |= FILE_ATTRIBUTE_REPARSE_POINT;
+        
         if (defda == fbi->FileAttributes)
             fcb->atts_deleted = TRUE;
         
@@ -82,7 +124,9 @@ static NTSTATUS STDCALL set_basic_information(device_extension* Vcb, PIRP Irp, P
         KeQuerySystemTime(&time);
         win_time_to_unix(time, &now);
         
-        fcb->inode_item.st_ctime = now;
+        if (!ccb->user_set_change_time)
+            fcb->inode_item.st_ctime = now;
+        
         fcb->subvol->root_item.ctransid = Vcb->superblock.generation;
         fcb->subvol->root_item.ctime = now;
         
@@ -90,42 +134,11 @@ static NTSTATUS STDCALL set_basic_information(device_extension* Vcb, PIRP Irp, P
         
         filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
     }
-    
-    if (fbi->CreationTime.QuadPart == -1) {
-        FIXME("FIXME - support CreationTime == -1\n"); // FIXME - set ccb flag
-    } else if (fbi->CreationTime.QuadPart != 0) {
-        win_time_to_unix(fbi->CreationTime, &fcb->inode_item.otime);
-        inode_item_changed = TRUE;
-        filter |= FILE_NOTIFY_CHANGE_CREATION;
-    }
-    
-    if (fbi->LastAccessTime.QuadPart == -1) {
-        FIXME("FIXME - support LastAccessTime == -1\n"); // FIXME - set ccb flag
-    } else if (fbi->LastAccessTime.QuadPart != 0) {
-        win_time_to_unix(fbi->LastAccessTime, &fcb->inode_item.st_atime);
-        inode_item_changed = TRUE;
-        filter |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
-    }
-    
-    if (fbi->LastWriteTime.QuadPart == -1) {
-        FIXME("FIXME - support LastWriteTime == -1\n"); // FIXME - set ccb flag
-    } else if (fbi->LastWriteTime.QuadPart != 0) {
-        win_time_to_unix(fbi->LastWriteTime, &fcb->inode_item.st_mtime);
-        inode_item_changed = TRUE;
-        filter |= FILE_NOTIFY_CHANGE_LAST_WRITE;
-    }
-    
-    if (fbi->ChangeTime.QuadPart == -1) {
-        FIXME("FIXME - support ChangeTime == -1\n"); // FIXME - set ccb flag
-    } else if (fbi->ChangeTime.QuadPart != 0) {
-        win_time_to_unix(fbi->ChangeTime, &fcb->inode_item.st_ctime);
-        inode_item_changed = TRUE;
-        // no filter for this
-    }
 
     if (inode_item_changed) {
         fcb->inode_item.transid = Vcb->superblock.generation;
         fcb->inode_item.sequence++;
+        fcb->inode_item_changed = TRUE;
         
         mark_fcb_dirty(fcb);
     }
@@ -152,6 +165,8 @@ static NTSTATUS STDCALL set_disposition_information(device_extension* Vcb, PIRP
     if (!fileref)
         return STATUS_INVALID_PARAMETER;
     
+    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+    
     ExAcquireResourceExclusiveLite(fcb->Header.Resource, TRUE);
     
     TRACE("changing delete_on_close to %s for %S (fcb %p)\n", fdi->DeleteFile ? "TRUE" : "FALSE", file_desc(FileObject), fcb);
@@ -194,147 +209,10 @@ static NTSTATUS STDCALL set_disposition_information(device_extension* Vcb, PIRP
     
 end:
     ExReleaseResourceLite(fcb->Header.Resource);
-
-    return Status;
-}
-
-static NTSTATUS add_inode_extref(device_extension* Vcb, root* subvol, UINT64 inode, UINT64 parinode, UINT64 index, PANSI_STRING utf8, PIRP Irp, LIST_ENTRY* rollback) {
-    KEY searchkey;
-    traverse_ptr tp;
-    INODE_EXTREF* ier;
-    NTSTATUS Status;
-    
-    searchkey.obj_id = inode;
-    searchkey.obj_type = TYPE_INODE_EXTREF;
-    searchkey.offset = calc_crc32c((UINT32)parinode, (UINT8*)utf8->Buffer, utf8->Length);
-
-    Status = find_item(Vcb, subvol, &tp, &searchkey, FALSE, Irp);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        return Status;
-    }
-    
-    if (!keycmp(&searchkey, &tp.item->key)) {
-        ULONG iersize = tp.item->size + sizeof(INODE_EXTREF) - 1 + utf8->Length;
-        UINT8* ier2;
-        UINT32 maxlen = Vcb->superblock.node_size - sizeof(tree_header) - sizeof(leaf_node);
-        
-        if (iersize > maxlen) {
-            ERR("item would be too long (%u > %u)\n", iersize, maxlen);
-            return STATUS_INTERNAL_ERROR;
-        }
-        
-        ier2 = ExAllocatePoolWithTag(PagedPool, iersize, ALLOC_TAG);
-        if (!ier2) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
-        
-        if (tp.item->size > 0)
-            RtlCopyMemory(ier2, tp.item->data, tp.item->size);
-        
-        ier = (INODE_EXTREF*)&ier2[tp.item->size];
-        ier->dir = parinode;
-        ier->index = index;
-        ier->n = utf8->Length;
-        RtlCopyMemory(ier->name, utf8->Buffer, utf8->Length);
-        
-        delete_tree_item(Vcb, &tp, rollback);
-        
-        if (!insert_tree_item(Vcb, subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, ier2, iersize, NULL, Irp, rollback)) {
-            ERR("error - failed to insert item\n");
-            return STATUS_INTERNAL_ERROR;
-        }
-    } else {
-        ier = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_EXTREF) - 1 + utf8->Length, ALLOC_TAG);
-        if (!ier) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
-
-        ier->dir = parinode;
-        ier->index = index;
-        ier->n = utf8->Length;
-        RtlCopyMemory(ier->name, utf8->Buffer, utf8->Length);
-    
-        if (!insert_tree_item(Vcb, subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, ier, sizeof(INODE_EXTREF) - 1 + utf8->Length, NULL, Irp, rollback)) {
-            ERR("error - failed to insert item\n");
-            return STATUS_INTERNAL_ERROR;
-        }
-    }
-    
-    return STATUS_SUCCESS;
-}
-
-NTSTATUS add_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UINT64 parinode, UINT64 index, PANSI_STRING utf8, PIRP Irp, LIST_ENTRY* rollback) {
-    KEY searchkey;
-    traverse_ptr tp;
-    INODE_REF* ir;
-    NTSTATUS Status;
     
-    searchkey.obj_id = inode;
-    searchkey.obj_type = TYPE_INODE_REF;
-    searchkey.offset = parinode;
-    
-    Status = find_item(Vcb, subvol, &tp, &searchkey, FALSE, Irp);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        return Status;
-    }
-    
-    if (!keycmp(&searchkey, &tp.item->key)) {
-        ULONG irsize = tp.item->size + sizeof(INODE_REF) - 1 + utf8->Length;
-        UINT8* ir2;
-        UINT32 maxlen = Vcb->superblock.node_size - sizeof(tree_header) - sizeof(leaf_node);
-        
-        if (irsize > maxlen) {
-            if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_EXTENDED_IREF) {
-                TRACE("INODE_REF too long, creating INODE_EXTREF\n");
-                return add_inode_extref(Vcb, subvol, inode, parinode, index, utf8, Irp, rollback);
-            } else {
-                ERR("item would be too long (%u > %u)\n", irsize, maxlen);
-                return STATUS_INTERNAL_ERROR;
-            }
-        }
-        
-        ir2 = ExAllocatePoolWithTag(PagedPool, irsize, ALLOC_TAG);
-        if (!ir2) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
-        
-        if (tp.item->size > 0)
-            RtlCopyMemory(ir2, tp.item->data, tp.item->size);
-        
-        ir = (INODE_REF*)&ir2[tp.item->size];
-        ir->index = index;
-        ir->n = utf8->Length;
-        RtlCopyMemory(ir->name, utf8->Buffer, utf8->Length);
-        
-        delete_tree_item(Vcb, &tp, rollback);
-        
-        if (!insert_tree_item(Vcb, subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, ir2, irsize, NULL, Irp, rollback)) {
-            ERR("error - failed to insert item\n");
-            return STATUS_INTERNAL_ERROR;
-        }
-    } else {
-        ir = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_REF) - 1 + utf8->Length, ALLOC_TAG);
-        if (!ir) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
+    ExReleaseResourceLite(&Vcb->fcb_lock);
 
-        ir->index = index;
-        ir->n = utf8->Length;
-        RtlCopyMemory(ir->name, utf8->Buffer, utf8->Length);
-    
-        if (!insert_tree_item(Vcb, subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, ir, sizeof(INODE_REF) - 1 + ir->n, NULL, Irp, rollback)) {
-            ERR("error - failed to insert item\n");
-            return STATUS_INTERNAL_ERROR;
-        }
-    }
-    
-    return STATUS_SUCCESS;
+    return Status;
 }
 
 BOOL has_open_children(file_ref* fileref) {
@@ -346,7 +224,7 @@ BOOL has_open_children(file_ref* fileref) {
     while (le != &fileref->children) {
         file_ref* c = CONTAINING_RECORD(le, file_ref, list_entry);
         
-        if (c->fcb->open_count > 0)
+        if (c->open_count > 0)
             return TRUE;
         
         if (has_open_children(c))
@@ -425,6 +303,7 @@ static NTSTATUS duplicate_fcb(fcb* oldfcb, fcb** pfcb) {
     }
     
     RtlCopyMemory(&fcb->inode_item, &oldfcb->inode_item, sizeof(INODE_ITEM));
+    fcb->inode_item_changed = TRUE;
     
     if (oldfcb->sd && RtlLengthSecurityDescriptor(oldfcb->sd) > 0) {
         fcb->sd = ExAllocatePoolWithTag(PagedPool, RtlLengthSecurityDescriptor(oldfcb->sd), ALLOC_TAG);
@@ -564,6 +443,23 @@ static NTSTATUS duplicate_fcb(fcb* oldfcb, fcb** pfcb) {
         
         RtlCopyMemory(fcb->reparse_xattr.Buffer, oldfcb->reparse_xattr.Buffer, fcb->reparse_xattr.Length);
     }
+    
+    if (oldfcb->ea_xattr.Buffer && oldfcb->ea_xattr.Length > 0) {
+        fcb->ea_xattr.Length = fcb->ea_xattr.MaximumLength = oldfcb->ea_xattr.Length;
+        
+        fcb->ea_xattr.Buffer = ExAllocatePoolWithTag(PagedPool, fcb->ea_xattr.MaximumLength, ALLOC_TAG);
+        if (!fcb->ea_xattr.Buffer) {
+            ERR("out of memory\n");
+            
+            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+            free_fcb(fcb);
+            ExReleaseResourceLite(&Vcb->fcb_lock);
+            
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+        
+        RtlCopyMemory(fcb->ea_xattr.Buffer, oldfcb->ea_xattr.Buffer, fcb->ea_xattr.Length);
+    }
 
 end:
     *pfcb = fcb;
@@ -598,7 +494,7 @@ static NTSTATUS add_children_to_move_list(move_entry* me, PIRP Irp) {
         
         if (!fr->deleted) {
             me2 = ExAllocatePoolWithTag(PagedPool, sizeof(move_entry), ALLOC_TAG);
-            if (!me) {
+            if (!me2) {
                 ERR("out of memory\n");
                 Status = STATUS_INSUFFICIENT_RESOURCES;
                 goto end;
@@ -651,7 +547,8 @@ static NTSTATUS add_children_to_move_list(move_entry* me, PIRP Irp) {
                 }
                 
                 if (xa->n > xapreflen && RtlCompareMemory(xa->name, xapref, xapreflen) == xapreflen &&
-                    (tp.item->key.offset != EA_DOSATTRIB_HASH || xa->n != strlen(EA_DOSATTRIB) || RtlCompareMemory(xa->name, EA_DOSATTRIB, xa->n) != xa->n)
+                    (tp.item->key.offset != EA_DOSATTRIB_HASH || xa->n != strlen(EA_DOSATTRIB) || RtlCompareMemory(xa->name, EA_DOSATTRIB, xa->n) != xa->n) &&
+                    (tp.item->key.offset != EA_EA_HASH || xa->n != strlen(EA_EA) || RtlCompareMemory(xa->name, EA_EA, xa->n) != xa->n)
                 ) {
                     BOOL found = FALSE;
                 
@@ -766,7 +663,7 @@ static NTSTATUS add_children_to_move_list(move_entry* me, PIRP Irp) {
                         insert_fileref_child(me->fileref, fr, FALSE);
 
                         me2 = ExAllocatePoolWithTag(PagedPool, sizeof(move_entry), ALLOC_TAG);
-                        if (!me) {
+                        if (!me2) {
                             ERR("out of memory\n");
                             Status = STATUS_INSUFFICIENT_RESOURCES;
                             
@@ -906,7 +803,7 @@ static NTSTATUS add_children_to_move_list(move_entry* me, PIRP Irp) {
                         }
                         
                         ExAcquireResourceExclusiveLite(&me->fileref->fcb->Vcb->fcb_lock, TRUE);
-                        Status = open_fcb(me->fileref->fcb->Vcb, subvol, inode, di->type, &utf8, me->fileref->fcb, &fcb, Irp);
+                        Status = open_fcb(me->fileref->fcb->Vcb, subvol, inode, di->type, &utf8, me->fileref->fcb, &fcb, PagedPool, Irp);
                         ExReleaseResourceLite(&me->fileref->fcb->Vcb->fcb_lock);
                         
                         if (!NT_SUCCESS(Status)) {
@@ -991,7 +888,7 @@ static NTSTATUS add_children_to_move_list(move_entry* me, PIRP Irp) {
                             fr->fcb->fileref = fr;
                         
                         me2 = ExAllocatePoolWithTag(PagedPool, sizeof(move_entry), ALLOC_TAG);
-                        if (!me) {
+                        if (!me2) {
                             ERR("out of memory\n");
                             Status = STATUS_INSUFFICIENT_RESOURCES;
                             
@@ -1110,6 +1007,7 @@ static NTSTATUS move_across_subvols(file_ref* fileref, file_ref* destdir, PANSI_
                     me->dummyfcb->atts_deleted = me->fileref->fcb->atts_deleted;
                     me->dummyfcb->extents_changed = me->fileref->fcb->extents_changed;
                     me->dummyfcb->reparse_xattr_changed = me->fileref->fcb->reparse_xattr_changed;
+                    me->dummyfcb->ea_changed = me->fileref->fcb->ea_changed;
                 }
                 
                 me->dummyfcb->created = me->fileref->fcb->created;
@@ -1119,11 +1017,8 @@ static NTSTATUS move_across_subvols(file_ref* fileref, file_ref* destdir, PANSI_
                 if (!me->fileref->fcb->ads) {
                     LIST_ENTRY* le2;
                     
-                    if (destdir->fcb->subvol->lastinode == 0)
-                        get_last_inode(destdir->fcb->Vcb, destdir->fcb->subvol, Irp);
-
                     me->fileref->fcb->subvol = destdir->fcb->subvol;
-                    me->fileref->fcb->inode = ++destdir->fcb->subvol->lastinode; // FIXME - do proper function for this
+                    me->fileref->fcb->inode = InterlockedIncrement64(&destdir->fcb->subvol->lastinode);
                     me->fileref->fcb->inode_item.st_nlink = 1;
                     
                     defda = get_file_attributes(me->fileref->fcb->Vcb, &me->fileref->fcb->inode_item, me->fileref->fcb->subvol, me->fileref->fcb->inode,
@@ -1133,6 +1028,8 @@ static NTSTATUS move_across_subvols(file_ref* fileref, file_ref* destdir, PANSI_
                     me->fileref->fcb->atts_changed = defda != me->fileref->fcb->atts;
                     me->fileref->fcb->extents_changed = !IsListEmpty(&me->fileref->fcb->extents);
                     me->fileref->fcb->reparse_xattr_changed = !!me->fileref->fcb->reparse_xattr.Buffer;
+                    me->fileref->fcb->ea_changed = !!me->fileref->fcb->ea_xattr.Buffer;
+                    me->fileref->fcb->inode_item_changed = TRUE;
                     
                     le2 = me->fileref->fcb->extents.Flink;
                     while (le2 != &me->fileref->fcb->extents) {
@@ -1149,7 +1046,7 @@ static NTSTATUS move_across_subvols(file_ref* fileref, file_ref* destdir, PANSI_
                                     ERR("get_chunk_from_address(%llx) failed\n", ed2->address);
                                 } else {
                                     Status = update_changed_extent_ref(me->fileref->fcb->Vcb, c, ed2->address, ed2->size, me->fileref->fcb->subvol->id, me->fileref->fcb->inode,
-                                                                       ext->offset - ed2->offset, 1, me->fileref->fcb->inode_item.flags & BTRFS_INODE_NODATASUM, ed2->size, Irp);
+                                                                       ext->offset - ed2->offset, 1, me->fileref->fcb->inode_item.flags & BTRFS_INODE_NODATASUM, FALSE, Irp);
                                     
                                     if (!NT_SUCCESS(Status)) {
                                         ERR("update_changed_extent_ref returned %08x\n", Status);
@@ -1190,6 +1087,7 @@ static NTSTATUS move_across_subvols(file_ref* fileref, file_ref* destdir, PANSI_
                     ExFreePool(hl);
                 }
                 
+                me->fileref->fcb->inode_item_changed = TRUE;
                 mark_fcb_dirty(me->fileref->fcb);
                 
                 if ((!me->dummyfcb->ads && me->dummyfcb->inode_item.st_nlink > 1) || (me->dummyfcb->ads && me->parent->dummyfcb->inode_item.st_nlink > 1)) {
@@ -1211,6 +1109,7 @@ static NTSTATUS move_across_subvols(file_ref* fileref, file_ref* destdir, PANSI_
             } else {
                 ExAcquireResourceExclusiveLite(me->fileref->fcb->Header.Resource, TRUE);
                 me->fileref->fcb->inode_item.st_nlink++;
+                me->fileref->fcb->inode_item_changed = TRUE;
                 ExReleaseResourceLite(me->fileref->fcb->Header.Resource);
             }
         }
@@ -1333,6 +1232,7 @@ static NTSTATUS move_across_subvols(file_ref* fileref, file_ref* destdir, PANSI_
             me->fileref->parent->fcb->inode_item.sequence++;
             me->fileref->parent->fcb->inode_item.st_ctime = now;
             me->fileref->parent->fcb->inode_item.st_mtime = now;
+            me->fileref->parent->fcb->inode_item_changed = TRUE;
             mark_fcb_dirty(me->fileref->parent->fcb);
         }
 
@@ -1530,10 +1430,13 @@ static NTSTATUS STDCALL set_rename_information(device_extension* Vcb, PIRP Irp,
         
         related = relatedccb->fileref;
         increase_fileref_refcount(related);
+    } else if (fnus.Length >= sizeof(WCHAR) && fnus.Buffer[0] != '\\') {
+        related = fileref->parent;
+        increase_fileref_refcount(related);
     }
 
     ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-    Status = open_fileref(Vcb, &oldfileref, &fnus, related, FALSE, NULL, NULL, Irp);
+    Status = open_fileref(Vcb, &oldfileref, &fnus, related, FALSE, NULL, NULL, PagedPool, ccb->case_sensitive,  Irp);
     ExReleaseResourceLite(&Vcb->fcb_lock);
 
     if (NT_SUCCESS(Status)) {
@@ -1543,7 +1446,7 @@ static NTSTATUS STDCALL set_rename_information(device_extension* Vcb, PIRP Irp,
             if (!IrpSp->Parameters.SetFile.ReplaceIfExists) {
                 Status = STATUS_OBJECT_NAME_COLLISION;
                 goto end;
-            } else if ((oldfileref->fcb->open_count >= 1 || has_open_children(oldfileref)) && !oldfileref->deleted) {
+            } else if ((oldfileref->open_count >= 1 || has_open_children(oldfileref)) && !oldfileref->deleted) {
                 WARN("trying to overwrite open file\n");
                 Status = STATUS_ACCESS_DENIED;
                 goto end;
@@ -1566,7 +1469,7 @@ static NTSTATUS STDCALL set_rename_information(device_extension* Vcb, PIRP Irp,
     
     if (!related) {
         ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-        Status = open_fileref(Vcb, &related, &fnus, NULL, TRUE, NULL, NULL, Irp);
+        Status = open_fileref(Vcb, &related, &fnus, NULL, TRUE, NULL, NULL, PagedPool, ccb->case_sensitive, Irp);
         ExReleaseResourceLite(&Vcb->fcb_lock);
 
         if (!NT_SUCCESS(Status)) {
@@ -1669,8 +1572,11 @@ static NTSTATUS STDCALL set_rename_information(device_extension* Vcb, PIRP Irp,
         
         fcb->inode_item.transid = Vcb->superblock.generation;
         fcb->inode_item.sequence++;
-        fcb->inode_item.st_ctime = now;
         
+        if (!ccb->user_set_change_time)
+            fcb->inode_item.st_ctime = now;
+        
+        fcb->inode_item_changed = TRUE;
         mark_fcb_dirty(fcb);
         
         // update parent's INODE_ITEM
@@ -1683,6 +1589,7 @@ static NTSTATUS STDCALL set_rename_information(device_extension* Vcb, PIRP Irp,
         related->fcb->inode_item.st_ctime = now;
         related->fcb->inode_item.st_mtime = now;
         
+        related->fcb->inode_item_changed = TRUE;
         mark_fcb_dirty(related->fcb);
         send_notification_fileref(related, FILE_NOTIFY_CHANGE_LAST_WRITE, FILE_ACTION_MODIFIED);
         
@@ -1826,8 +1733,11 @@ static NTSTATUS STDCALL set_rename_information(device_extension* Vcb, PIRP Irp,
     
     fcb->inode_item.transid = Vcb->superblock.generation;
     fcb->inode_item.sequence++;
-    fcb->inode_item.st_ctime = now;
     
+    if (!ccb->user_set_change_time)
+        fcb->inode_item.st_ctime = now;
+    
+    fcb->inode_item_changed = TRUE;
     mark_fcb_dirty(fcb);
     
     // update new parent's INODE_ITEM
@@ -1840,6 +1750,7 @@ static NTSTATUS STDCALL set_rename_information(device_extension* Vcb, PIRP Irp,
     related->fcb->inode_item.st_ctime = now;
     related->fcb->inode_item.st_mtime = now;
     
+    related->fcb->inode_item_changed = TRUE;
     mark_fcb_dirty(related->fcb);
     
     // update old parent's INODE_ITEM
@@ -1856,6 +1767,7 @@ static NTSTATUS STDCALL set_rename_information(device_extension* Vcb, PIRP Irp,
     free_fileref(fr2);
     ExReleaseResourceLite(&Vcb->fcb_lock);
     
+    fr2->parent->fcb->inode_item_changed = TRUE;
     mark_fcb_dirty(fr2->parent->fcb);
     
     send_notification_fileref(fr2, fcb->type == BTRFS_TYPE_DIRECTORY ? FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME, FILE_ACTION_REMOVED);
@@ -1885,7 +1797,7 @@ end:
     }
     
     if (NT_SUCCESS(Status))
-        clear_rollback(&rollback);
+        clear_rollback(Vcb, &rollback);
     else
         do_rollback(Vcb, &rollback);
 
@@ -1965,6 +1877,7 @@ NTSTATUS STDCALL stream_set_end_of_file_information(device_extension* Vcb, UINT6
     fileref->parent->fcb->inode_item.sequence++;
     fileref->parent->fcb->inode_item.st_ctime = now;
     
+    fileref->parent->fcb->inode_item_changed = TRUE;
     mark_fcb_dirty(fileref->parent->fcb);
 
     fileref->parent->fcb->subvol->root_item.ctransid = Vcb->superblock.generation;
@@ -2051,10 +1964,12 @@ static NTSTATUS STDCALL set_end_of_file_information(device_extension* Vcb, PIRP
     CcSetFileSizes(FileObject, &ccfs);
     TRACE("setting FileSize for %S to %llx\n", file_desc(FileObject), ccfs.FileSize);
     
-    KeQuerySystemTime(&time);
-    
-    win_time_to_unix(time, &fcb->inode_item.st_mtime);
+    if (!ccb->user_set_write_time) {
+        KeQuerySystemTime(&time);
+        win_time_to_unix(time, &fcb->inode_item.st_mtime);
+    }
     
+    fcb->inode_item_changed = TRUE;
     mark_fcb_dirty(fcb);
     send_notification_fcb(fileref, FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE, FILE_ACTION_MODIFIED);
 
@@ -2062,7 +1977,7 @@ static NTSTATUS STDCALL set_end_of_file_information(device_extension* Vcb, PIRP
 
 end:
     if (NT_SUCCESS(Status))
-        clear_rollback(&rollback);
+        clear_rollback(Vcb, &rollback);
     else
         do_rollback(Vcb, &rollback);
 
@@ -2200,7 +2115,7 @@ static NTSTATUS STDCALL set_link_information(device_extension* Vcb, PIRP Irp, PF
     }
 
     ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-    Status = open_fileref(Vcb, &oldfileref, &fnus, related, FALSE, NULL, NULL, Irp);
+    Status = open_fileref(Vcb, &oldfileref, &fnus, related, FALSE, NULL, NULL, PagedPool, ccb->case_sensitive, Irp);
     ExReleaseResourceLite(&Vcb->fcb_lock);
 
     if (NT_SUCCESS(Status)) {
@@ -2210,7 +2125,7 @@ static NTSTATUS STDCALL set_link_information(device_extension* Vcb, PIRP Irp, PF
             if (!fli->ReplaceIfExists) {
                 Status = STATUS_OBJECT_NAME_COLLISION;
                 goto end;
-            } else if (oldfileref->fcb->open_count >= 1 && !oldfileref->deleted) {
+            } else if (oldfileref->open_count >= 1 && !oldfileref->deleted) {
                 WARN("trying to overwrite open file\n");
                 Status = STATUS_ACCESS_DENIED;
                 goto end;
@@ -2234,7 +2149,7 @@ static NTSTATUS STDCALL set_link_information(device_extension* Vcb, PIRP Irp, PF
     
     if (!related) {
         ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-        Status = open_fileref(Vcb, &related, &fnus, NULL, TRUE, NULL, NULL, Irp);
+        Status = open_fileref(Vcb, &related, &fnus, NULL, TRUE, NULL, NULL, PagedPool, ccb->case_sensitive, Irp);
         ExReleaseResourceLite(&Vcb->fcb_lock);
 
         if (!NT_SUCCESS(Status)) {
@@ -2361,8 +2276,11 @@ static NTSTATUS STDCALL set_link_information(device_extension* Vcb, PIRP Irp, PF
     fcb->inode_item.transid = Vcb->superblock.generation;
     fcb->inode_item.sequence++;
     fcb->inode_item.st_nlink++;
-    fcb->inode_item.st_ctime = now;
     
+    if (!ccb->user_set_change_time)
+        fcb->inode_item.st_ctime = now;
+    
+    fcb->inode_item_changed = TRUE;
     mark_fcb_dirty(fcb);
     
     // update parent's INODE_ITEM
@@ -2374,6 +2292,7 @@ static NTSTATUS STDCALL set_link_information(device_extension* Vcb, PIRP Irp, PF
     parfcb->inode_item.sequence++;
     parfcb->inode_item.st_ctime = now;
     
+    parfcb->inode_item_changed = TRUE;
     mark_fcb_dirty(parfcb);
     
     send_notification_fileref(fr2, FILE_NOTIFY_CHANGE_FILE_NAME, FILE_ACTION_ADDED);
@@ -2400,7 +2319,7 @@ end:
     }
     
     if (NT_SUCCESS(Status))
-        clear_rollback(&rollback);
+        clear_rollback(Vcb, &rollback);
     else
         do_rollback(Vcb, &rollback);
 
@@ -2529,13 +2448,6 @@ NTSTATUS STDCALL drv_set_information(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp
         {
             TRACE("FilePositionInformation\n");
             
-            if (Irp->RequestorMode == UserMode &&
-                (!(ccb->access & (FILE_READ_DATA | FILE_WRITE_DATA)) || !(ccb->options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)))) {
-         &nbs