[BTRFS]
authorPierre Schweitzer <pierre@reactos.org>
Thu, 5 May 2016 17:26:47 +0000 (17:26 +0000)
committerPierre Schweitzer <pierre@reactos.org>
Thu, 5 May 2016 17:26:47 +0000 (17:26 +0000)
Upgrade the WinBtrfs to release 0.4.

CORE-11172 #resolve #Committed in r71265

svn path=/trunk/; revision=71265

20 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/btrfsioctl.h
reactos/drivers/filesystems/btrfs/create.c
reactos/drivers/filesystems/btrfs/dirctrl.c
reactos/drivers/filesystems/btrfs/extent-tree.c [new file with mode: 0644]
reactos/drivers/filesystems/btrfs/fileinfo.c
reactos/drivers/filesystems/btrfs/flushthread.c
reactos/drivers/filesystems/btrfs/free-space.c [new file with mode: 0644]
reactos/drivers/filesystems/btrfs/fsctl.c
reactos/drivers/filesystems/btrfs/loader.c [deleted file]
reactos/drivers/filesystems/btrfs/pnp.c [new file with mode: 0644]
reactos/drivers/filesystems/btrfs/read.c
reactos/drivers/filesystems/btrfs/reparse.c
reactos/drivers/filesystems/btrfs/security.c
reactos/drivers/filesystems/btrfs/treefuncs.c
reactos/drivers/filesystems/btrfs/write.c

index 2e3d1ea..cd3d68d 100644 (file)
@@ -8,10 +8,13 @@ list(APPEND SOURCE
     crc32c.c
     create.c
     dirctrl.c
+    extent-tree.c
     fastio.c
     fileinfo.c
     flushthread.c
+    free-space.c
     fsctl.c
+    pnp.c
     read.c
     reparse.c
     search.c
@@ -30,7 +33,7 @@ endif()
 
 add_definitions(-D__KERNEL__)
 set_module_type(btrfs kernelmodedriver)
-target_link_libraries(btrfs ntoskrnl_vista)
+target_link_libraries(btrfs ntoskrnl_vista ${PSEH_LIB})
 add_importlibs(btrfs ntoskrnl hal)
 add_pch(btrfs btrfs_drv.h SOURCE)
 add_cd_file(TARGET btrfs DESTINATION reactos/system32/drivers NO_CAB FOR all)
index 6a1d256..a9fb3e5 100644 (file)
@@ -61,6 +61,8 @@ PDEVICE_OBJECT comdo = NULL;
 HANDLE log_handle = NULL;
 #endif
 
+int __security_cookie = __LINE__;
+
 static NTSTATUS STDCALL close_file(device_extension* Vcb, PFILE_OBJECT FileObject);
 
 typedef struct {
@@ -82,9 +84,9 @@ static NTSTATUS STDCALL dbg_completion(PDEVICE_OBJECT DeviceObject, PIRP Irp, PV
 }
 
 #ifdef DEBUG_LONG_MESSAGES
-void STDCALL _debug_message(const char* func, UINT8 priority, const char* file, unsigned int line, char* s, ...) {
+void STDCALL _debug_message(const char* func, const char* file, unsigned int line, char* s, ...) {
 #else
-void STDCALL _debug_message(const char* func, UINT8 priority, char* s, ...) {
+void STDCALL _debug_message(const char* func, char* s, ...) {
 #endif
     LARGE_INTEGER offset;
     PIO_STACK_LOCATION IrpSp;
@@ -95,9 +97,6 @@ void STDCALL _debug_message(const char* func, UINT8 priority, char* s, ...) {
     read_context* context = NULL;
     UINT32 length;
     
-    if (log_started && priority > debug_log_level)
-        return;
-    
     buf2 = ExAllocatePoolWithTag(NonPagedPool, 1024, ALLOC_TAG);
     
     if (!buf2) {
@@ -327,21 +326,17 @@ BOOL STDCALL get_last_inode(device_extension* Vcb, root* r) {
     }
     
     while (find_prev_item(Vcb, &tp, &prev_tp, FALSE)) {
-        free_traverse_ptr(&tp);
         tp = prev_tp;
         
         TRACE("moving on to %llx,%x,%llx\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
         
-        if (tp.item->key.obj_type == TYPE_INODE_ITEM) {
+        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;
-            free_traverse_ptr(&tp);
             TRACE("last inode for tree %llx is %llx\n", r->id, r->lastinode);
             return TRUE;
         }
     }
     
-    free_traverse_ptr(&tp);
-    
     r->lastinode = SUBVOL_ROOT_INODE;
     
     WARN("no INODE_ITEMs in tree %llx\n", r->id);
@@ -370,13 +365,11 @@ BOOL STDCALL get_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char*
     
     if (keycmp(&tp.item->key, &searchkey)) {
         TRACE("could not find item (%llx,%x,%llx)\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
-        free_traverse_ptr(&tp);
         return FALSE;
     }
     
     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));
-        free_traverse_ptr(&tp);
         return FALSE;
     }
     
@@ -386,7 +379,6 @@ BOOL STDCALL get_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char*
     while (TRUE) {
         if (size < sizeof(DIR_ITEM) || size < (sizeof(DIR_ITEM) - 1 + xa->m + xa->n)) {
             WARN("(%llx,%x,%llx) is truncated\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
-            free_traverse_ptr(&tp);
             return FALSE;
         }
         
@@ -399,7 +391,6 @@ BOOL STDCALL get_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char*
                 *data = ExAllocatePoolWithTag(PagedPool, xa->m, ALLOC_TAG);
                 if (!*data) {
                     ERR("out of memory\n");
-                    free_traverse_ptr(&tp);
                     return FALSE;
                 }
                 
@@ -407,7 +398,6 @@ BOOL STDCALL get_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char*
             } else
                 *data = NULL;
             
-            free_traverse_ptr(&tp);
             return TRUE;
         }
         
@@ -422,8 +412,6 @@ BOOL STDCALL get_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char*
     
     TRACE("xattr %s not found in (%llx,%x,%llx)\n", name, searchkey.obj_id, searchkey.obj_type, searchkey.offset);
     
-    free_traverse_ptr(&tp);
-    
     return FALSE;
 }
 
@@ -448,6 +436,8 @@ NTSTATUS STDCALL set_xattr(device_extension* Vcb, root* subvol, UINT64 inode, ch
     
     xasize = sizeof(DIR_ITEM) - 1 + (ULONG)strlen(name) + datalen;
     
+    // FIXME - make sure xasize not too big
+    
     if (!keycmp(&tp.item->key, &searchkey)) { // key exists
         UINT8* newdata;
         ULONG size = tp.item->size;
@@ -474,7 +464,6 @@ NTSTATUS STDCALL set_xattr(device_extension* Vcb, root* subvol, UINT64 inode, ch
                     newdata = ExAllocatePoolWithTag(PagedPool, tp.item->size + xasize - oldxasize, ALLOC_TAG);
                     if (!newdata) {
                         ERR("out of memory\n");
-                        free_traverse_ptr(&tp);
                         return STATUS_INSUFFICIENT_RESOURCES;
                     }
                     
@@ -510,7 +499,6 @@ NTSTATUS STDCALL set_xattr(device_extension* Vcb, root* subvol, UINT64 inode, ch
                     newdata = ExAllocatePoolWithTag(PagedPool, tp.item->size + xasize, ALLOC_TAG);
                     if (!newdata) {
                         ERR("out of memory\n");
-                        free_traverse_ptr(&tp);
                         return STATUS_INSUFFICIENT_RESOURCES;
                     }
                     
@@ -543,7 +531,6 @@ NTSTATUS STDCALL set_xattr(device_extension* Vcb, root* subvol, UINT64 inode, ch
         xa = ExAllocatePoolWithTag(PagedPool, xasize, ALLOC_TAG);
         if (!xa) {
             ERR("out of memory\n");
-            free_traverse_ptr(&tp);
             return STATUS_INSUFFICIENT_RESOURCES;
         }
         
@@ -560,8 +547,6 @@ NTSTATUS STDCALL set_xattr(device_extension* Vcb, root* subvol, UINT64 inode, ch
         insert_tree_item(Vcb, subvol, inode, TYPE_XATTR_ITEM, crc32, xa, xasize, NULL, rollback);
     }
     
-    free_traverse_ptr(&tp);
-    
     return STATUS_SUCCESS;
 }
 
@@ -589,7 +574,6 @@ BOOL STDCALL delete_xattr(device_extension* Vcb, root* subvol, UINT64 inode, cha
         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));
             
-            free_traverse_ptr(&tp);
             return FALSE;
         } else {
             xa = (DIR_ITEM*)tp.item->data;
@@ -599,7 +583,6 @@ BOOL STDCALL delete_xattr(device_extension* Vcb, root* subvol, UINT64 inode, cha
                 
                 if (size < sizeof(DIR_ITEM) || size < sizeof(DIR_ITEM) - 1 + xa->m + xa->n) {
                     ERR("(%llx,%x,%llx) was truncated\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
-                    free_traverse_ptr(&tp);
                         
                     return FALSE;
                 }
@@ -616,7 +599,6 @@ BOOL STDCALL delete_xattr(device_extension* Vcb, root* subvol, UINT64 inode, cha
                     
                     if (newsize == 0) {
                         TRACE("xattr %s deleted\n", name);
-                        free_traverse_ptr(&tp);
                         
                         return TRUE;
                     }
@@ -625,7 +607,6 @@ BOOL STDCALL delete_xattr(device_extension* Vcb, root* subvol, UINT64 inode, cha
                     newdata = ExAllocatePoolWithTag(PagedPool, newsize, ALLOC_TAG);
                     if (!newdata) {
                         ERR("out of memory\n");
-                        free_traverse_ptr(&tp);
                         return FALSE;
                     }
 
@@ -641,14 +622,12 @@ BOOL STDCALL delete_xattr(device_extension* Vcb, root* subvol, UINT64 inode, cha
                     
                     insert_tree_item(Vcb, subvol, inode, TYPE_XATTR_ITEM, crc32, newdata, newsize, NULL, rollback);
                     
-                    free_traverse_ptr(&tp);
                         
                     return TRUE;
                 }
                 
                 if (xa->m + xa->n >= size) { // FIXME - test this works
                     WARN("xattr %s not found\n", name);
-                    free_traverse_ptr(&tp);
 
                     return FALSE;
                 } else {
@@ -659,7 +638,6 @@ BOOL STDCALL delete_xattr(device_extension* Vcb, root* subvol, UINT64 inode, cha
         }
     } else {
         WARN("xattr %s not found\n", name);
-        free_traverse_ptr(&tp);
         
         return FALSE;
     }
@@ -686,14 +664,12 @@ NTSTATUS add_dir_item(device_extension* Vcb, root* subvol, UINT64 inode, UINT32
         
         if (tp.item->size + disize > maxlen) {
             WARN("DIR_ITEM was longer than maxlen (%u + %u > %u)\n", tp.item->size, disize, maxlen);
-            free_traverse_ptr(&tp);
             return STATUS_INTERNAL_ERROR;
         }
         
         di2 = ExAllocatePoolWithTag(PagedPool, tp.item->size + disize, ALLOC_TAG);
         if (!di2) {
             ERR("out of memory\n");
-            free_traverse_ptr(&tp);
             return STATUS_INSUFFICIENT_RESOURCES;
         }
         
@@ -710,8 +686,6 @@ NTSTATUS add_dir_item(device_extension* Vcb, root* subvol, UINT64 inode, UINT32
     } else {
         insert_tree_item(Vcb, subvol, inode, TYPE_DIR_ITEM, crc32, di, disize, NULL, rollback);
     }
-
-    free_traverse_ptr(&tp);
     
     return STATUS_SUCCESS;
 }
@@ -734,7 +708,6 @@ UINT64 find_next_dir_index(device_extension* Vcb, root* subvol, UINT64 inode) {
     
     if (!keycmp(&searchkey, &tp.item->key)) {
         if (!find_prev_item(Vcb, &tp, &prev_tp, FALSE)) {
-            free_traverse_ptr(&tp);
             tp = prev_tp;
             
             TRACE("moving back to %llx,%x,%llx\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
@@ -746,8 +719,6 @@ UINT64 find_next_dir_index(device_extension* Vcb, root* subvol, UINT64 inode) {
     } else
         dirpos = 2;
     
-    free_traverse_ptr(&tp);
-    
     return dirpos;
 }
 
@@ -794,6 +765,7 @@ exit:
 static NTSTATUS STDCALL drv_write(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
     NTSTATUS Status;
     BOOL top_level;
+    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
 
     FsRtlEnterFileSystem();
 
@@ -801,7 +773,18 @@ static NTSTATUS STDCALL drv_write(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
     
 //     ERR("recursive = %s\n", Irp != IoGetTopLevelIrp() ? "TRUE" : "FALSE");
     
-    Status = write_file(DeviceObject, Irp);
+    _SEH2_TRY {
+        if (IrpSp->MinorFunction & IRP_MN_COMPLETE) {
+            CcMdlWriteComplete(IrpSp->FileObject, &IrpSp->Parameters.Write.ByteOffset, Irp->MdlAddress);
+            
+            Irp->MdlAddress = NULL;
+            Irp->IoStatus.Status = STATUS_SUCCESS;
+        } else {
+            Status = write_file(DeviceObject, Irp);
+        }
+    } _SEH2_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
+        Status = _SEH2_GetExceptionCode();
+    } _SEH2_END;
     
     Irp->IoStatus.Status = Status;
 
@@ -1180,6 +1163,151 @@ static NTSTATUS STDCALL read_completion(PDEVICE_OBJECT DeviceObject, PIRP Irp, P
 //     }
 // }
 
+// static void test_dropping_tree(device_extension* Vcb) {
+//     LIST_ENTRY* le = Vcb->roots.Flink;
+//     
+//     while (le != &Vcb->roots) {
+//         root* r = CONTAINING_RECORD(le, root, list_entry);
+//         
+//         if (r->id == 0x101) {
+//             RemoveEntryList(&r->list_entry);
+//             InsertTailList(&Vcb->drop_roots, &r->list_entry);
+//             return;
+//         }
+//         
+//         le = le->Flink;
+//     }
+// }
+
+NTSTATUS create_root(device_extension* Vcb, UINT64 id, root** rootptr, BOOL no_tree, UINT64 offset, LIST_ENTRY* rollback) {
+    root* r;
+    tree* t;
+    ROOT_ITEM* ri;
+    traverse_ptr tp;
+    
+    r = ExAllocatePoolWithTag(PagedPool, sizeof(root), ALLOC_TAG);
+    if (!r) {
+        ERR("out of memory\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    r->nonpaged = ExAllocatePoolWithTag(NonPagedPool, sizeof(root_nonpaged), ALLOC_TAG);
+    if (!r->nonpaged) {
+        ERR("out of memory\n");
+        ExFreePool(r);
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    if (!no_tree) {
+        t = ExAllocatePoolWithTag(PagedPool, sizeof(tree), ALLOC_TAG);
+        if (!t) {
+            ERR("out of memory\n");
+            ExFreePool(r->nonpaged);
+            ExFreePool(r);
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+    }
+    
+    ri = ExAllocatePoolWithTag(PagedPool, sizeof(ROOT_ITEM), ALLOC_TAG);
+    if (!ri) {
+        ERR("out of memory\n");
+        
+        if (!no_tree)
+            ExFreePool(t);
+        
+        ExFreePool(r->nonpaged);
+        ExFreePool(r);
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    r->id = id;
+    r->treeholder.address = 0;
+    r->treeholder.generation = Vcb->superblock.generation;
+    r->treeholder.tree = no_tree ? NULL : t;
+    r->lastinode = 0;
+    r->path.Buffer = NULL;
+    RtlZeroMemory(&r->root_item, sizeof(ROOT_ITEM));
+    r->root_item.num_references = 1;
+    InitializeListHead(&r->fcbs);
+    
+    RtlCopyMemory(ri, &r->root_item, sizeof(ROOT_ITEM));
+    
+    // We ask here for a traverse_ptr to the item we're inserting, so we can
+    // copy some of the tree's variables
+    
+    if (!insert_tree_item(Vcb, Vcb->root_root, id, TYPE_ROOT_ITEM, offset, ri, sizeof(ROOT_ITEM), &tp, rollback)) {
+        ERR("insert_tree_item failed\n");
+        ExFreePool(ri);
+        
+        if (!no_tree)
+            ExFreePool(t);
+        
+        ExFreePool(r->nonpaged);
+        ExFreePool(r);
+        return STATUS_INTERNAL_ERROR;
+    }
+        
+    ExInitializeResourceLite(&r->nonpaged->load_tree_lock);
+    
+    InsertTailList(&Vcb->roots, &r->list_entry);
+    
+    if (!no_tree) {
+        t->header.fs_uuid = tp.tree->header.fs_uuid;
+        t->header.address = 0;
+        t->header.flags = HEADER_FLAG_MIXED_BACKREF | 1; // 1 == "written"? Why does the Linux driver record this?
+        t->header.chunk_tree_uuid = tp.tree->header.chunk_tree_uuid;
+        t->header.generation = Vcb->superblock.generation;
+        t->header.tree_id = id;
+        t->header.num_items = 0;
+        t->header.level = 0;
+
+        t->has_address = FALSE;
+        t->size = 0;
+        t->Vcb = Vcb;
+        t->parent = NULL;
+        t->paritem = NULL;
+        t->root = r;
+        
+        InitializeListHead(&t->itemlist);
+    
+        t->new_address = 0;
+        t->has_new_address = FALSE;
+        t->flags = tp.tree->flags;
+        
+        InsertTailList(&Vcb->trees, &t->list_entry);
+        
+        t->write = TRUE;
+        Vcb->write_trees++;
+    }
+    
+    *rootptr = r;
+
+    return STATUS_SUCCESS;
+}
+
+// static void test_creating_root(device_extension* Vcb) {
+//     NTSTATUS Status;
+//     LIST_ENTRY rollback;
+//     UINT64 id;
+//     root* r;
+//     
+//     InitializeListHead(&rollback);
+//     
+//     if (Vcb->root_root->lastinode == 0)
+//         get_last_inode(Vcb, Vcb->root_root);
+//     
+//     id = Vcb->root_root->lastinode > 0x100 ? (Vcb->root_root->lastinode + 1) : 0x101;
+//     Status = create_root(Vcb, id, &r, &rollback);
+//     
+//     if (!NT_SUCCESS(Status)) {
+//         ERR("create_root returned %08x\n", Status);
+//         do_rollback(Vcb, &rollback);
+//     } else {
+//         Vcb->root_root->lastinode = id;
+//         clear_rollback(&rollback);
+//     }
+// }
+
 static NTSTATUS STDCALL set_label(device_extension* Vcb, FILE_FS_LABEL_INFORMATION* ffli) {
     ULONG utf8len;
     NTSTATUS Status;
@@ -1210,6 +1338,8 @@ static NTSTATUS STDCALL set_label(device_extension* Vcb, FILE_FS_LABEL_INFORMATI
     
 //     test_tree_deletion(Vcb); // TESTING
 //     test_tree_splitting(Vcb);
+//     test_dropping_tree(Vcb);
+//     test_creating_root(Vcb);
     
     Status = consider_write(Vcb);
     
@@ -1241,6 +1371,11 @@ static NTSTATUS STDCALL drv_set_volume_information(IN PDEVICE_OBJECT DeviceObjec
         goto end;
     }
     
+    if (Vcb->removing) {
+        Status = STATUS_ACCESS_DENIED;
+        goto end;
+    }
+    
     switch (IrpSp->Parameters.SetVolume.FsInformationClass) {
         case FileFsControlInformation:
             FIXME("STUB: FileFsControlInformation\n");
@@ -1313,7 +1448,6 @@ NTSTATUS delete_dir_item(device_extension* Vcb, root* subvol, UINT64 parinode, U
                         
                         if (!newdi) {
                             ERR("out of memory\n");
-                            free_traverse_ptr(&tp);
                             return STATUS_INSUFFICIENT_RESOURCES;
                         }
                         
@@ -1343,8 +1477,6 @@ NTSTATUS delete_dir_item(device_extension* Vcb, root* subvol, UINT64 parinode, U
         WARN("could not find DIR_ITEM for crc32 %08x\n", crc32);
     }
     
-    free_traverse_ptr(&tp);
-    
     return STATUS_SUCCESS;
 }
 
@@ -1397,7 +1529,6 @@ NTSTATUS delete_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UIN
                         
                         if (!newir) {
                             ERR("out of memory\n");
-                            free_traverse_ptr(&tp);
                             return STATUS_INSUFFICIENT_RESOURCES;
                         }
                         
@@ -1437,8 +1568,6 @@ NTSTATUS delete_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UIN
         WARN("could not find INODE_REF entry for inode %llx in %llx\n", searchkey.obj_id, searchkey.offset);
     }
     
-    free_traverse_ptr(&tp);
-    
     if (changed)
         return STATUS_SUCCESS;
     
@@ -1488,7 +1617,6 @@ NTSTATUS delete_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UIN
                         
                         if (!newier) {
                             ERR("out of memory\n");
-                            free_traverse_ptr(&tp);
                             return STATUS_INSUFFICIENT_RESOURCES;
                         }
                         
@@ -1524,12 +1652,224 @@ NTSTATUS delete_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UIN
         WARN("couldn't find INODE_EXTREF entry either (offset = %08x)\n", (UINT32)searchkey.offset);
     }
     
-    free_traverse_ptr(&tp);
-    
     return changed ? STATUS_SUCCESS : STATUS_INTERNAL_ERROR;
 }
 
-NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
+static NTSTATUS delete_subvol(file_ref* fileref, LIST_ENTRY* rollback) {
+    NTSTATUS Status;
+    UINT64 index;
+    KEY searchkey;
+    traverse_ptr tp;
+    UINT32 crc32;
+    ROOT_ITEM* ri;
+    BOOL no_ref = FALSE;
+    fcb* fcb = fileref->fcb;
+    
+    // delete ROOT_REF in root tree
+    
+    Status = delete_root_ref(fcb->Vcb, fcb->subvol->id, fileref->parent->fcb->subvol->id, fileref->parent->fcb->inode, &fileref->utf8, &index, rollback);
+    
+    // A bug in Linux means that if you create a snapshot of a subvol containing another subvol,
+    // the ROOT_REF and ROOT_BACKREF items won't be created, nor will num_references of ROOT_ITEM
+    // be increased. In this case, we just unlink the subvol from its parent, and don't worry
+    // about anything else.
+    
+    if (Status == STATUS_NOT_FOUND)
+        no_ref = TRUE;
+    else if (!NT_SUCCESS(Status)) {
+        ERR("delete_root_ref returned %08x\n", Status);
+        return Status;
+    }
+    
+    if (!no_ref) {
+        // delete ROOT_BACKREF in root tree
+        
+        Status = update_root_backref(fcb->Vcb, fcb->subvol->id, fileref->parent->fcb->subvol->id, rollback);
+        if (!NT_SUCCESS(Status)) {
+            ERR("update_root_backref returned %08x\n", Status);
+            return Status;
+        }
+    }
+    
+    // delete DIR_ITEM in parent
+    
+    crc32 = calc_crc32c(0xfffffffe, (UINT8*)fileref->utf8.Buffer, fileref->utf8.Length);
+    Status = delete_dir_item(fcb->Vcb, fileref->parent->fcb->subvol, fileref->parent->fcb->inode, crc32, &fileref->utf8, rollback);
+    if (!NT_SUCCESS(Status)) {
+        ERR("delete_dir_item returned %08x\n", Status);
+        return Status;
+    }
+    
+    // delete DIR_INDEX in parent
+    
+    if (!no_ref) {
+        searchkey.obj_id = fileref->parent->fcb->inode;
+        searchkey.obj_type = TYPE_DIR_INDEX;
+        searchkey.offset = index;
+        
+        Status = find_item(fcb->Vcb, fileref->parent->fcb->subvol, &tp, &searchkey, FALSE);
+        if (!NT_SUCCESS(Status)) {
+            ERR("find_item 1 returned %08x\n", Status);
+            return Status;
+        }
+    
+        if (!keycmp(&searchkey, &tp.item->key)) {
+            delete_tree_item(fcb->Vcb, &tp, rollback);
+            TRACE("deleting (%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
+        }
+    } else {
+        BOOL b;
+        traverse_ptr next_tp;
+        
+        // If we have no ROOT_REF, we have to look through all the DIR_INDEX entries manually :-(
+        
+        searchkey.obj_id = fileref->parent->fcb->inode;
+        searchkey.obj_type = TYPE_DIR_INDEX;
+        searchkey.offset = 0;
+        
+        Status = find_item(fcb->Vcb, fileref->parent->fcb->subvol, &tp, &searchkey, FALSE);
+        if (!NT_SUCCESS(Status)) {
+            ERR("find_item 1 returned %08x\n", Status);
+            return Status;
+        }
+        
+        do {
+            if (tp.item->key.obj_type == TYPE_DIR_INDEX && tp.item->size >= sizeof(DIR_ITEM)) {
+                DIR_ITEM* di = (DIR_ITEM*)tp.item->data;
+                
+                if (di->key.obj_id == fcb->subvol->id && di->key.obj_type == TYPE_ROOT_ITEM && di->n == fileref->utf8.Length &&
+                    tp.item->size >= sizeof(DIR_ITEM) - 1 + di->m + di->n && RtlCompareMemory(fileref->utf8.Buffer, di->name, di->n) == di->n) {
+                    delete_tree_item(fcb->Vcb, &tp, rollback);
+                    break;
+                }
+            }
+        
+            b = find_next_item(fcb->Vcb, &tp, &next_tp, FALSE);
+            
+            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 (no_ref)
+        return STATUS_SUCCESS;
+    
+    if (fcb->subvol->root_item.num_references > 1) {
+        UINT64 offset;
+        
+        // change ROOT_ITEM num_references
+        
+        fcb->subvol->root_item.num_references--;
+        
+        searchkey.obj_id = fcb->subvol->id;
+        searchkey.obj_type = TYPE_ROOT_ITEM;
+        searchkey.offset = 0xffffffffffffffff;
+        
+        Status = find_item(fcb->Vcb, fcb->Vcb->root_root, &tp, &searchkey, FALSE);
+        if (!NT_SUCCESS(Status)) {
+            ERR("find_item 2 returned %08x\n", Status);
+            return Status;
+        }
+        
+        if (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
+            delete_tree_item(fcb->Vcb, &tp, rollback);
+            TRACE("deleting (%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
+            offset = tp.item->key.offset;
+        } else {
+            ERR("could not find ROOT_ITEM for subvol %llx\n", fcb->subvol->id);
+            offset = 0;
+        }
+        
+        ri = ExAllocatePoolWithTag(PagedPool, sizeof(ROOT_ITEM), ALLOC_TAG);
+        if (!ri) {
+            ERR("out of memory\n");
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+        
+        RtlCopyMemory(ri, &fcb->subvol->root_item, sizeof(ROOT_ITEM));
+        
+        if (!insert_tree_item(fcb->Vcb, fcb->Vcb->root_root, fcb->subvol->id, TYPE_ROOT_ITEM, offset, ri, sizeof(ROOT_ITEM), NULL, rollback)) {
+            ERR("insert_tree_item failed\n");
+            return STATUS_INTERNAL_ERROR;
+        }
+    } else {
+        RemoveEntryList(&fcb->subvol->list_entry);
+        
+        InsertTailList(&fcb->Vcb->drop_roots, &fcb->subvol->list_entry);
+    }
+    
+    return STATUS_SUCCESS;
+}
+
+static WCHAR* file_desc_fcb(fcb* fcb) {
+    char s[60];
+    UNICODE_STRING us;
+    ANSI_STRING as;
+    
+    if (fcb->debug_desc)
+        return fcb->debug_desc;
+    
+    fcb->debug_desc = ExAllocatePoolWithTag(PagedPool, 60 * sizeof(WCHAR), ALLOC_TAG);
+    if (!fcb->debug_desc)
+        return L"(memory error)";
+    
+    // I know this is pretty hackish...
+    // GCC doesn't like %llx in sprintf, and MSVC won't let us use swprintf
+    // without the CRT, which breaks drivers.
+    
+    sprintf(s, "subvol %x, inode %x", (UINT32)fcb->subvol->id, (UINT32)fcb->inode);
+    
+    as.Buffer = s;
+    as.Length = as.MaximumLength = strlen(s);
+    
+    us.Buffer = fcb->debug_desc;
+    us.MaximumLength = 60 * sizeof(WCHAR);
+    us.Length = 0;
+    
+    RtlAnsiStringToUnicodeString(&us, &as, FALSE);
+    
+    us.Buffer[us.Length / sizeof(WCHAR)] = 0;
+    
+    return fcb->debug_desc;
+}
+
+WCHAR* file_desc_fileref(file_ref* fileref) {
+    if (fileref->debug_desc)
+        return fileref->debug_desc;
+    
+    fileref->debug_desc = ExAllocatePoolWithTag(PagedPool, fileref->full_filename.Length + sizeof(WCHAR), ALLOC_TAG);
+    if (!fileref->debug_desc)
+        return L"(memory error)";
+    
+    RtlCopyMemory(fileref->debug_desc, fileref->full_filename.Buffer, fileref->full_filename.Length);
+    fileref->debug_desc[fileref->full_filename.Length / sizeof(WCHAR)] = 0;
+    
+    return fileref->debug_desc;
+}
+
+WCHAR* file_desc(PFILE_OBJECT FileObject) {
+    fcb* fcb = FileObject->FsContext;
+    ccb* ccb = FileObject->FsContext2;
+    file_ref* fileref = ccb ? ccb->fileref : NULL;
+    
+    if (fileref)
+        return file_desc_fileref(fileref);
+    else
+        return file_desc_fcb(fcb);
+}
+
+void send_notification_fileref(file_ref* fileref, ULONG filter_match, ULONG action) {
+    fcb* fcb = fileref->fcb;
+    
+    FsRtlNotifyFullReportChange(fcb->Vcb->NotifySync, &fcb->Vcb->DirNotifyList, (PSTRING)&fileref->full_filename, fileref->name_offset * sizeof(WCHAR),
+                                NULL, NULL, filter_match, action, NULL);
+}
+
+NTSTATUS delete_fileref(file_ref* fileref, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
     ULONG bytecount;
     NTSTATUS Status;
     char* utf8 = NULL;
@@ -1537,28 +1877,39 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
     KEY searchkey;
     traverse_ptr tp, tp2;
     UINT64 parinode, index;
+    root* parsubvol;
     INODE_ITEM *ii, *dirii;
     LARGE_INTEGER time;
     BTRFS_TIME now;
     LIST_ENTRY changed_sector_list;
+    fcb* fcb = fileref->fcb;
 #ifdef _DEBUG
     LARGE_INTEGER freq, time1, time2;
 #endif
     
-    // FIXME - throw error if try to delete subvol root(?)
-    
-    // FIXME - delete all children if deleting directory
-    
-    if (fcb->deleted) {
+    if (fileref->deleted || fcb->deleted) {
         WARN("trying to delete already-deleted file\n");
         return STATUS_SUCCESS;
     }
     
-    if (!fcb->par) {
+    if (fileref == fcb->Vcb->root_fileref) {
         ERR("error - trying to delete root FCB\n");
         return STATUS_INTERNAL_ERROR;
     }
     
+    if (fcb->inode == SUBVOL_ROOT_INODE) {
+        Status = delete_subvol(fileref, rollback);
+        
+        if (!NT_SUCCESS(Status))
+            goto exit;
+        else {
+            parinode = fileref->parent->fcb->inode;
+            parsubvol = fileref->parent->fcb->subvol;
+            bytecount = fileref->utf8.Length;
+            goto success2;
+        }
+    }
+    
 #ifdef _DEBUG
     time1 = KeQueryPerformanceCounter(&freq);
 #endif
@@ -1580,29 +1931,28 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
         RtlCopyMemory(s, fcb->adsxattr.Buffer, fcb->adsxattr.Length);
         s[fcb->adsxattr.Length] = 0;
         
-        if (!delete_xattr(fcb->Vcb, fcb->par->subvol, fcb->par->inode, s, fcb->adshash, rollback)) {
+        if (!delete_xattr(fcb->Vcb, fileref->parent->fcb->subvol, fileref->parent->fcb->inode, s, fcb->adshash, rollback)) {
             ERR("failed to delete xattr %s\n", s);
         }
         
         ExFreePool(s);
         
-        fcb->par->inode_item.transid = fcb->Vcb->superblock.generation;
-        fcb->par->inode_item.sequence++;
-        fcb->par->inode_item.st_ctime = now;
+        fileref->parent->fcb->inode_item.transid = fcb->Vcb->superblock.generation;
+        fileref->parent->fcb->inode_item.sequence++;
+        fileref->parent->fcb->inode_item.st_ctime = now;
         
-        searchkey.obj_id = fcb->par->inode;
+        searchkey.obj_id = fileref->parent->fcb->inode;
         searchkey.obj_type = TYPE_INODE_ITEM;
         searchkey.offset = 0xffffffffffffffff;
         
-        Status = find_item(fcb->Vcb, fcb->par->subvol, &tp, &searchkey, FALSE);
+        Status = find_item(fcb->Vcb, fileref->parent->fcb->subvol, &tp, &searchkey, FALSE);
         if (!NT_SUCCESS(Status)) {
             ERR("error - find_item returned %08x\n", Status);
             goto exit;
         }
         
         if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
-            ERR("error - could not find INODE_ITEM for inode %llx in subvol %llx\n", fcb->par->inode, fcb->par->subvol->id);
-            free_traverse_ptr(&tp);
+            ERR("error - could not find INODE_ITEM for inode %llx in subvol %llx\n", fileref->parent->fcb->inode, fileref->parent->fcb->subvol->id);
             Status = STATUS_INTERNAL_ERROR;
             goto exit;
         }
@@ -1610,25 +1960,22 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
         ii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
         if (!ii) {
             ERR("out of memory\n");
-            free_traverse_ptr(&tp);
             Status = STATUS_INSUFFICIENT_RESOURCES;
             goto exit;
         }
         
-        RtlCopyMemory(ii, &fcb->par->inode_item, sizeof(INODE_ITEM));
+        RtlCopyMemory(ii, &fileref->parent->fcb->inode_item, sizeof(INODE_ITEM));
         delete_tree_item(fcb->Vcb, &tp, rollback);
         
-        insert_tree_item(fcb->Vcb, fcb->par->subvol, searchkey.obj_id, searchkey.obj_type, 0, ii, sizeof(INODE_ITEM), NULL, rollback);
+        insert_tree_item(fcb->Vcb, fileref->parent->fcb->subvol, searchkey.obj_id, searchkey.obj_type, 0, ii, sizeof(INODE_ITEM), NULL, rollback);
         
-        free_traverse_ptr(&tp);
-        
-        fcb->par->subvol->root_item.ctransid = fcb->Vcb->superblock.generation;
-        fcb->par->subvol->root_item.ctime = now;
+        fileref->parent->fcb->subvol->root_item.ctransid = fcb->Vcb->superblock.generation;
+        fileref->parent->fcb->subvol->root_item.ctime = now;
         
         goto success;
     }
     
-    Status = RtlUnicodeToUTF8N(NULL, 0, &bytecount, fcb->filepart.Buffer, fcb->filepart.Length);
+    Status = RtlUnicodeToUTF8N(NULL, 0, &bytecount, fileref->filepart.Buffer, fileref->filepart.Length);
     if (!NT_SUCCESS(Status)) {
         ERR("RtlUnicodeToUTF8N failed with error %08x\n", Status);
         return Status;
@@ -1640,21 +1987,23 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
-    RtlUnicodeToUTF8N(utf8, bytecount, &bytecount, fcb->filepart.Buffer, fcb->filepart.Length);
+    RtlUnicodeToUTF8N(utf8, bytecount, &bytecount, fileref->filepart.Buffer, fileref->filepart.Length);
     utf8[bytecount] = 0;
     
     crc32 = calc_crc32c(0xfffffffe, (UINT8*)utf8, bytecount);
 
-    TRACE("deleting %.*S\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
+    TRACE("deleting %.*S\n", file_desc_fileref(fileref));
     
-    if (fcb->par->subvol == fcb->subvol)
-        parinode = fcb->par->inode;
+    if (fileref->parent->fcb->subvol == fcb->subvol)
+        parinode = fileref->parent->fcb->inode;
     else
         parinode = SUBVOL_ROOT_INODE;
     
+    parsubvol = fcb->subvol;
+    
     // delete DIR_ITEM (0x54)
     
-    Status = delete_dir_item(fcb->Vcb, fcb->subvol, parinode, crc32, &fcb->utf8, rollback);
+    Status = delete_dir_item(fcb->Vcb, fcb->subvol, parinode, crc32, &fileref->utf8, rollback);
     if (!NT_SUCCESS(Status)) {
         ERR("delete_dir_item returned %08x\n", Status);
         return Status;
@@ -1664,7 +2013,7 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
     
     index = 0;
     
-    Status = delete_inode_ref(fcb->Vcb, fcb->subvol, fcb->inode, parinode, &fcb->utf8, &index, rollback);
+    Status = delete_inode_ref(fcb->Vcb, fcb->subvol, fcb->inode, parinode, &fileref->utf8, &index, rollback);
     
     // delete DIR_INDEX (0x60)
     
@@ -1675,7 +2024,6 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
     Status = find_item(fcb->Vcb, fcb->subvol, &tp, &searchkey, FALSE);
     if (!NT_SUCCESS(Status)) {
         ERR("error - find_item returned %08x\n", Status);
-        free_traverse_ptr(&tp);
         Status = STATUS_INTERNAL_ERROR;
         goto exit;
     }
@@ -1694,23 +2042,19 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
     Status = find_item(fcb->Vcb, fcb->subvol, &tp2, &searchkey, FALSE);
     if (!NT_SUCCESS(Status)) {
         ERR("error - find_item returned %08x\n", Status);
-        free_traverse_ptr(&tp);
         goto exit;
     }
     
-    free_traverse_ptr(&tp);
     tp = tp2;
     
     if (keycmp(&searchkey, &tp.item->key)) {
         ERR("error - INODE_ITEM not found\n");
-        free_traverse_ptr(&tp);
         Status = STATUS_INTERNAL_ERROR;
         goto exit;
     }
     
     if (tp.item->size < sizeof(INODE_ITEM)) {
         ERR("(%llx,%x,%llx) was %u bytes, expected %u\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(INODE_ITEM));
-        free_traverse_ptr(&tp);
         Status = STATUS_INTERNAL_ERROR;
         goto exit;
     }
@@ -1724,7 +2068,6 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
         newii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
         if (!newii) {
             ERR("out of memory\n");
-            free_traverse_ptr(&tp);
             Status = STATUS_INSUFFICIENT_RESOURCES;
             goto exit;
         }
@@ -1742,18 +2085,17 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
         if (!insert_tree_item(fcb->Vcb, fcb->subvol, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, newii, sizeof(INODE_ITEM), NULL, rollback))
             ERR("error - failed to insert item\n");
         
-        free_traverse_ptr(&tp);
-        
         goto success2;
     }
     
     delete_tree_item(fcb->Vcb, &tp, rollback);
     TRACE("deleting (%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
     
+    fcb->deleted = TRUE;
+    
     // delete XATTR_ITEM (0x18)
     
     while (find_next_item(fcb->Vcb, &tp, &tp2, FALSE)) {
-        free_traverse_ptr(&tp);
         tp = tp2;
         
         if (tp.item->key.obj_id == fcb->inode) {
@@ -1766,8 +2108,6 @@ NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
             break;
     }
     
-    free_traverse_ptr(&tp);
-    
     // excise extents
     
     InitializeListHead(&changed_sector_list);
@@ -1790,49 +2130,45 @@ success2:
     searchkey.obj_type = TYPE_INODE_ITEM;
     searchkey.offset = 0;
     
-    Status = find_item(fcb->Vcb, fcb->subvol, &tp, &searchkey, FALSE);
+    Status = find_item(fcb->Vcb, parsubvol, &tp, &searchkey, FALSE);
     if (!NT_SUCCESS(Status)) {
         ERR("error - find_tree returned %08x\n", Status);
         goto exit;
     }
     
     if (keycmp(&searchkey, &tp.item->key)) {
-        ERR("error - could not find INODE_ITEM for parent directory %llx in subvol %llx\n", parinode, fcb->subvol->id);
-        free_traverse_ptr(&tp);
+        ERR("error - could not find INODE_ITEM for parent directory %llx in subvol %llx\n", parinode, parsubvol->id);
         Status = STATUS_INTERNAL_ERROR;
         goto exit;
     }
     
-    TRACE("fcb->par->inode_item.st_size was %llx\n", fcb->par->inode_item.st_size);
-    fcb->par->inode_item.st_size -= bytecount * 2;
-    TRACE("fcb->par->inode_item.st_size now %llx\n", fcb->par->inode_item.st_size);
-    fcb->par->inode_item.transid = fcb->Vcb->superblock.generation;
-    fcb->par->inode_item.sequence++;
-    fcb->par->inode_item.st_ctime = now;
-    fcb->par->inode_item.st_mtime = now;
+    TRACE("fileref->parent->fcb->inode_item.st_size was %llx\n", fileref->parent->fcb->inode_item.st_size);
+    fileref->parent->fcb->inode_item.st_size -= bytecount * 2;
+    TRACE("fileref->parent->fcb->inode_item.st_size now %llx\n", fileref->parent->fcb->inode_item.st_size);
+    fileref->parent->fcb->inode_item.transid = fcb->Vcb->superblock.generation;
+    fileref->parent->fcb->inode_item.sequence++;
+    fileref->parent->fcb->inode_item.st_ctime = now;
+    fileref->parent->fcb->inode_item.st_mtime = now;
 
     dirii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
     if (!dirii) {
         ERR("out of memory\n");
-        free_traverse_ptr(&tp);
         Status = STATUS_INSUFFICIENT_RESOURCES;
         goto exit;
     }
     
-    RtlCopyMemory(dirii, &fcb->par->inode_item, sizeof(INODE_ITEM));
+    RtlCopyMemory(dirii, &fileref->parent->fcb->inode_item, sizeof(INODE_ITEM));
     delete_tree_item(fcb->Vcb, &tp, rollback);
     
-    insert_tree_item(fcb->Vcb, fcb->subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, dirii, sizeof(INODE_ITEM), NULL, rollback);
-    
-    free_traverse_ptr(&tp);
+    insert_tree_item(fcb->Vcb, parsubvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, dirii, sizeof(INODE_ITEM), NULL, rollback);
     
-    fcb->subvol->root_item.ctransid = fcb->Vcb->superblock.generation;
-    fcb->subvol->root_item.ctime = now;
+    parsubvol->root_item.ctransid = fcb->Vcb->superblock.generation;
+    parsubvol->root_item.ctime = now;
     
 success:
     consider_write(fcb->Vcb);
     
-    fcb->deleted = TRUE;
+    fileref->deleted = TRUE;
     
     fcb->Header.AllocationSize.QuadPart = 0;
     fcb->Header.FileSize.QuadPart = 0;
@@ -1850,11 +2186,12 @@ success:
     
     // FIXME - set deleted flag of any open FCBs for ADS
     
-    TRACE("sending notification for deletion of %.*S\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
-    
-    FsRtlNotifyFullReportChange(fcb->Vcb->NotifySync, &fcb->Vcb->DirNotifyList, (PSTRING)&fcb->full_filename, fcb->name_offset * sizeof(WCHAR), NULL, NULL,
-                                fcb->type == BTRFS_TYPE_DIRECTORY ? FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME,
-                                FILE_ACTION_REMOVED, NULL);
+    if (FileObject && FileObject->FsContext2) {
+        ccb* ccb = FileObject->FsContext2;
+        
+        if (ccb->fileref)
+            send_notification_fileref(ccb->fileref, fcb->type == BTRFS_TYPE_DIRECTORY ? FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME, FILE_ACTION_REMOVED);
+    }
     
 #ifdef _DEBUG
     time2 = KeQueryPerformanceCounter(NULL);
@@ -1872,16 +2209,16 @@ exit:
 }
 
 void _free_fcb(fcb* fcb, const char* func, const char* file, unsigned int line) {
-    ULONG rc;
-    
+    LONG rc;
+
     rc = InterlockedDecrement(&fcb->refcount);
     
 #ifdef DEBUG_FCB_REFCOUNTS
 //     WARN("fcb %p: refcount now %i (%.*S)\n", fcb, rc, fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
 #ifdef DEBUG_LONG_MESSAGES
-    _debug_message(func, 1, file, line, "fcb %p: refcount now %i (%.*S)\n", fcb, rc, fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
+    _debug_message(func, file, line, "fcb %p: refcount now %i (subvol %llx, inode %llx)\n", fcb, rc, fcb->subvol ? fcb->subvol->id : 0, fcb->inode);
 #else
-    _debug_message(func, 1, "fcb %p: refcount now %i (%.*S)\n", fcb, rc, fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
+    _debug_message(func, "fcb %p: refcount now %i (subvol %llx, inode %llx)\n", fcb, rc, fcb->subvol ? fcb->subvol->id : 0, fcb->inode);
 #endif
 #endif
     
@@ -1889,30 +2226,13 @@ void _free_fcb(fcb* fcb, const char* func, const char* file, unsigned int line)
         return;
     
     ExAcquireResourceExclusiveLite(&fcb->Vcb->fcb_lock, TRUE);
-
-    if (fcb->filepart.Buffer)
-        RtlFreeUnicodeString(&fcb->filepart);
    
     ExDeleteResourceLite(&fcb->nonpaged->resource);
     ExDeleteResourceLite(&fcb->nonpaged->paging_resource);
     ExFreePool(fcb->nonpaged);
     
-    if (fcb->par/* && fcb->par != fcb->par->Vcb->root_fcb*/) {
+    if (fcb->list_entry.Flink)
         RemoveEntryList(&fcb->list_entry);
-        _free_fcb(fcb->par, func, file, line);
-    }
-    
-    if (fcb->prev)
-        fcb->prev->next = fcb->next;
-    
-    if (fcb->next)
-        fcb->next->prev = fcb->prev;
-    
-    if (fcb->Vcb->fcbs == fcb)
-        fcb->Vcb->fcbs = fcb->next;
-    
-    if (fcb->full_filename.Buffer)
-        ExFreePool(fcb->full_filename.Buffer);
     
     if (fcb->sd)
         ExFreePool(fcb->sd);
@@ -1920,8 +2240,8 @@ void _free_fcb(fcb* fcb, const char* func, const char* file, unsigned int line)
     if (fcb->adsxattr.Buffer)
         ExFreePool(fcb->adsxattr.Buffer);
     
-    if (fcb->utf8.Buffer)
-        ExFreePool(fcb->utf8.Buffer);
+    if (fcb->debug_desc)
+        ExFreePool(fcb->debug_desc);
     
     FsRtlUninitializeFileLock(&fcb->lock);
     
@@ -1930,16 +2250,69 @@ void _free_fcb(fcb* fcb, const char* func, const char* file, unsigned int line)
     ExFreePool(fcb);
 #ifdef DEBUG_FCB_REFCOUNTS
 #ifdef DEBUG_LONG_MESSAGES
-    _debug_message(func, 1, file, line, "freeing fcb %p\n", fcb);
+    _debug_message(func, file, line, "freeing fcb %p\n", fcb);
+#else
+    _debug_message(func, "freeing fcb %p\n", fcb);
+#endif
+#endif
+}
+
+void _free_fileref(file_ref* fr, const char* func, const char* file, unsigned int line) {
+    LONG rc;
+
+    rc = InterlockedDecrement(&fr->refcount);
+    
+#ifdef DEBUG_FCB_REFCOUNTS
+#ifdef DEBUG_LONG_MESSAGES
+    _debug_message(func, file, line, "fileref %p: refcount now %i\n", fr, rc);
 #else
-    _debug_message(func, 1, "freeing fcb %p\n", fcb);
+    _debug_message(func, "fileref %p: refcount now %i\n", fr, rc);
+#endif
 #endif
+    
+#ifdef _DEBUG
+    if (rc < 0) {
+        ERR("fileref %p: refcount now %i\n", fr, rc);
+        int3;
+    }
 #endif
+    
+    if (rc > 0)
+        return;
+    
+    // FIXME - do we need a file_ref lock?
+    
+    // FIXME - do delete if needed
+    
+    if (fr->filepart.Buffer)
+        ExFreePool(fr->filepart.Buffer);
+    
+    if (fr->utf8.Buffer)
+        ExFreePool(fr->utf8.Buffer);
+    
+    if (fr->full_filename.Buffer)
+        ExFreePool(fr->full_filename.Buffer);
+    
+    if (fr->debug_desc)
+        ExFreePool(fr->debug_desc);
+    
+    // FIXME - throw error if children not empty
+    
+    free_fcb(fr->fcb);
+    
+    if (fr->list_entry.Flink)
+        RemoveEntryList(&fr->list_entry);
+    
+    if (fr->parent)
+        free_fileref((file_ref*)fr->parent);
+    
+    ExFreePool(fr);
 }
 
 static NTSTATUS STDCALL close_file(device_extension* Vcb, PFILE_OBJECT FileObject) {
     fcb* fcb;
     ccb* ccb;
+    file_ref* fileref = NULL;
     
     TRACE("FileObject = %p\n", FileObject);
     
@@ -1951,7 +2324,7 @@ static NTSTATUS STDCALL close_file(device_extension* Vcb, PFILE_OBJECT FileObjec
     
     ccb = FileObject->FsContext2;
     
-    TRACE("close called for %.*S (fcb == %p)\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer, fcb);
+    TRACE("close called for %S (fcb == %p)\n", file_desc(FileObject), fcb);
     
     FsRtlNotifyCleanup(Vcb->NotifySync, &Vcb->DirNotifyList, ccb);
     
@@ -1961,43 +2334,54 @@ static NTSTATUS STDCALL close_file(device_extension* Vcb, PFILE_OBJECT FileObjec
         if (ccb->query_string.Buffer)
             RtlFreeUnicodeString(&ccb->query_string);
         
+        // FIXME - use refcounts for fileref
+        fileref = ccb->fileref;
+        
         ExFreePool(ccb);
     }
     
     CcUninitializeCacheMap(FileObject, NULL, NULL);
     
-    free_fcb(fcb);
+    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+    
+    if (fileref)
+        free_fileref(fileref);
+    else
+        free_fcb(fcb);
+    
+    ExReleaseResourceLite(&Vcb->fcb_lock);
     
     return STATUS_SUCCESS;
 }
 
-static void STDCALL uninit(device_extension* Vcb) {
+void STDCALL uninit(device_extension* Vcb, BOOL flush) {
     chunk* c;
     space* s;
     UINT64 i;
     LIST_ENTRY rollback;
     
-    InitializeListHead(&rollback);
-    
-    acquire_tree_lock(Vcb, TRUE);
+    if (flush) {
+        InitializeListHead(&rollback);
+        
+        acquire_tree_lock(Vcb, TRUE);
 
-    if (Vcb->write_trees > 0)
-        do_write(Vcb, &rollback);
-    
-    free_tree_cache(&Vcb->tree_cache);
-    
-    clear_rollback(&rollback);
+        if (Vcb->write_trees > 0)
+            do_write(Vcb, &rollback);
+        
+        free_trees(Vcb);
+        
+        clear_rollback(&rollback);
 
-    release_tree_lock(Vcb, TRUE);
+        release_tree_lock(Vcb, TRUE);
+    }
 
-    while (Vcb->roots) {
-        root* r = Vcb->roots->next;
+    while (!IsListEmpty(&Vcb->roots)) {
+        LIST_ENTRY* le = RemoveHeadList(&Vcb->roots);
+        root* r = CONTAINING_RECORD(le, root, list_entry);
 
-        ExDeleteResourceLite(&Vcb->roots->nonpaged->load_tree_lock);
-        ExFreePool(Vcb->roots->nonpaged);
-        ExFreePool(Vcb->roots);
-        
-        Vcb->roots = r;
+        ExDeleteResourceLite(&r->nonpaged->load_tree_lock);
+        ExFreePool(r->nonpaged);
+        ExFreePool(r);
     }
     
     while (!IsListEmpty(&Vcb->chunks)) {
@@ -2019,7 +2403,7 @@ static void STDCALL uninit(device_extension* Vcb) {
     }
     
     free_fcb(Vcb->volume_fcb);
-    free_fcb(Vcb->root_fcb);
+    free_fileref(Vcb->root_fileref);
     
     for (i = 0; i < Vcb->superblock.num_devices; i++) {
         while (!IsListEmpty(&Vcb->devices[i].disk_holes)) {
@@ -2058,13 +2442,17 @@ static NTSTATUS STDCALL drv_cleanup(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
         goto exit;
     }
     
-    if (FileObject) {
+    if (FileObject && FileObject->FsContext) {
         LONG oc;
+        ccb* ccb;
+        file_ref* fileref;
         
         fcb = FileObject->FsContext;
+        ccb = FileObject->FsContext2;
+        fileref = ccb ? ccb->fileref : NULL;
         
         TRACE("cleanup called for FileObject %p\n", FileObject);
-        TRACE("fcb %p (%.*S), refcount = %u, open_count = %u\n", fcb, fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer, fcb->refcount, fcb->open_count);
+        TRACE("fcb %p (%S), refcount = %u, open_count = %u\n", fcb, file_desc(FileObject), fcb->refcount, fcb->open_count);
         
         IoRemoveShareAccess(FileObject, &fcb->share_access);
         
@@ -2073,14 +2461,20 @@ static NTSTATUS STDCALL drv_cleanup(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
         ERR("fcb %p: open_count now %i\n", fcb, oc);
 #endif
         
+        if (ccb && ccb->options & FILE_DELETE_ON_CLOSE && fileref)
+            fileref->delete_on_close = TRUE;
+        
+        if (fileref && fileref->delete_on_close && fcb->type == BTRFS_TYPE_DIRECTORY && fcb->inode_item.st_size > 0)
+            fileref->delete_on_close = FALSE;
+        
         if (oc == 0) {
-            if (fcb->delete_on_close && fcb != fcb->Vcb->root_fcb && fcb != fcb->Vcb->volume_fcb) {
+            if (fileref && fileref->delete_on_close && fileref != fcb->Vcb->root_fileref && fcb != fcb->Vcb->volume_fcb) {
                 LIST_ENTRY rollback;
                 InitializeListHead(&rollback);
                 
                 acquire_tree_lock(fcb->Vcb, TRUE);
                 
-                Status = delete_fcb(fcb, FileObject, &rollback);
+                Status = delete_fileref(fileref, FileObject, &rollback);
                 
                 if (NT_SUCCESS(Status)) {
                     LARGE_INTEGER newlength;
@@ -2144,6 +2538,8 @@ ULONG STDCALL get_file_attributes(device_extension* Vcb, INODE_ITEM* ii, root* r
     char* eaval;
     UINT16 ealen;
     
+    // ii can be NULL
+    
     if (!ignore_xa && get_xattr(Vcb, r, inode, EA_DOSATTRIB, EA_DOSATTRIB_HASH, (UINT8**)&eaval, &ealen)) {
         if (ealen > 2) {
             if (eaval[0] == '0' && eaval[1] == 'x') {
@@ -2165,6 +2561,11 @@ ULONG STDCALL get_file_attributes(device_extension* Vcb, INODE_ITEM* ii, root* r
 
                 ExFreePool(eaval);
                 
+                if (type == BTRFS_TYPE_DIRECTORY)
+                    dosnum |= FILE_ATTRIBUTE_DIRECTORY;
+                else if (type == BTRFS_TYPE_SYMLINK)
+                    dosnum |= FILE_ATTRIBUTE_REPARSE_POINT;
+                
                 return dosnum;
             }
         }
@@ -2359,8 +2760,8 @@ static NTSTATUS STDCALL read_superblock(device_extension* Vcb, PDEVICE_OBJECT de
     return STATUS_SUCCESS;
 }
 
-static NTSTATUS STDCALL dev_ioctl(PDEVICE_OBJECT DeviceObject, ULONG ControlCode, PVOID InputBuffer,
-                    ULONG InputBufferSize, PVOID OutputBuffer, ULONG OutputBufferSize, BOOLEAN Override)
+NTSTATUS STDCALL dev_ioctl(PDEVICE_OBJECT DeviceObject, ULONG ControlCode, PVOID InputBuffer, ULONG InputBufferSize,
+                           PVOID OutputBuffer, ULONG OutputBufferSize, BOOLEAN Override, IO_STATUS_BLOCK* iosb)
 {
     PIRP Irp;
     KEVENT Event;
@@ -2393,6 +2794,9 @@ static NTSTATUS STDCALL dev_ioctl(PDEVICE_OBJECT DeviceObject, ULONG ControlCode
         KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
         Status = IoStatus.Status;
     }
+    
+    if (iosb)
+        *iosb = IoStatus;
 
     return Status;
 }
@@ -2430,11 +2834,11 @@ static NTSTATUS STDCALL add_root(device_extension* Vcb, UINT64 id, UINT64 addr,
     }
     
     r->id = id;
+    r->path.Buffer = NULL;
     r->treeholder.address = addr;
     r->treeholder.tree = NULL;
     init_tree_holder(&r->treeholder);
-    r->prev = NULL;
-    r->next = Vcb->roots;
+    InitializeListHead(&r->fcbs);
 
     r->nonpaged = ExAllocatePoolWithTag(NonPagedPool, sizeof(root_nonpaged), ALLOC_TAG);
     if (!r->nonpaged) {
@@ -2453,10 +2857,7 @@ 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->roots)
-        Vcb->roots->prev = r;
-    
-    Vcb->roots = r;
+    InsertTailList(&Vcb->roots, &r->list_entry);
     
     switch (r->id) {
         case BTRFS_ROOT_ROOT:
@@ -2478,6 +2879,10 @@ static NTSTATUS STDCALL add_root(device_extension* Vcb, UINT64 id, UINT64 addr,
         case BTRFS_ROOT_CHECKSUM:
             Vcb->checksum_root = r;
             break;
+            
+        case BTRFS_ROOT_UUID:
+            Vcb->uuid_root = r;
+            break;
     }
     
     return STATUS_SUCCESS;
@@ -2520,14 +2925,10 @@ static NTSTATUS STDCALL look_for_roots(device_extension* Vcb) {
     
         b = find_next_item(Vcb, &tp, &next_tp, FALSE);
         
-        if (b) {
-            free_traverse_ptr(&tp);
+        if (b)
             tp = next_tp;
-        }
     } while (b);
     
-    free_traverse_ptr(&tp);
-    
     return STATUS_SUCCESS;
 }
 
@@ -2591,15 +2992,12 @@ static NTSTATUS find_disk_holes(device_extension* Vcb, device* dev) {
         b = find_next_item(Vcb, &tp, &next_tp, FALSE);
         
         if (b) {
-            free_traverse_ptr(&tp);
             tp = next_tp;
             if (tp.item->key.obj_id > searchkey.obj_id || tp.item->key.obj_type > searchkey.obj_type)
                 break;
         }
     } while (b);
     
-    free_traverse_ptr(&tp);
-    
     if (lastaddr < dev->devitem.num_bytes) {
         Status = add_disk_hole(&dev->disk_holes, lastaddr, dev->devitem.num_bytes - lastaddr);
         if (!NT_SUCCESS(Status)) {
@@ -2674,6 +3072,8 @@ static NTSTATUS STDCALL load_chunk_root(device_extension* Vcb) {
                 c->offset = tp.item->key.offset;
                 c->used = c->oldused = 0;
                 c->space_changed = FALSE;
+                c->cache_inode = 0;
+                c->cache_size = 0;
                 
                 c->chunk_item = ExAllocatePoolWithTag(PagedPool, tp.item->size, ALLOC_TAG);
                 
@@ -2709,317 +3109,18 @@ static NTSTATUS STDCALL load_chunk_root(device_extension* Vcb) {
     
         b = find_next_item(Vcb, &tp, &next_tp, FALSE);
         
-        if (b) {
-            free_traverse_ptr(&tp);
+        if (b)
             tp = next_tp;
-        }
     } while (b);
     
-    free_traverse_ptr(&tp);
-    
     Vcb->log_to_phys_loaded = TRUE;
     
     return STATUS_SUCCESS;
 }
 
-static BOOL load_stored_free_space_cache(device_extension* Vcb, chunk* c) {
-    KEY searchkey;
-    traverse_ptr tp, tp2;
-    FREE_SPACE_ITEM* fsi;
-    UINT64 inode, num_sectors, i, generation;
-    INODE_ITEM* ii;
-    UINT8* data;
-    NTSTATUS Status;
-    UINT32 *checksums, crc32;
-#ifdef _DEBUG
-    FREE_SPACE_ENTRY* fse;
-    UINT64 num_entries;
-#endif
-    
-    TRACE("(%p, %llx)\n", Vcb, c->offset);
-    
-    if (Vcb->superblock.generation != Vcb->superblock.cache_generation)
-        return FALSE;
-    
-    searchkey.obj_id = FREE_SPACE_CACHE_ID;
-    searchkey.obj_type = 0;
-    searchkey.offset = c->offset;
-    
-    Status = find_item(Vcb, Vcb->root_root, &tp, &searchkey, FALSE);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        return FALSE;
-    }
-    
-    if (keycmp(&tp.item->key, &searchkey)) {
-        WARN("(%llx,%x,%llx) not found\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
-        free_traverse_ptr(&tp);
-        return FALSE;
-    }
-    
-    if (tp.item->size < sizeof(FREE_SPACE_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(FREE_SPACE_ITEM));
-        free_traverse_ptr(&tp);
-        return FALSE;
-    }
-    
-    fsi = (FREE_SPACE_ITEM*)tp.item->data;
-    
-    if (fsi->generation != Vcb->superblock.cache_generation) {
-        WARN("cache had generation %llx, expecting %llx\n", fsi->generation, Vcb->superblock.cache_generation);
-        free_traverse_ptr(&tp);
-        return FALSE;
-    }
-    
-    if (fsi->key.obj_type != TYPE_INODE_ITEM) {
-        WARN("cache pointed to something other than an INODE_ITEM\n");
-        free_traverse_ptr(&tp);
-        return FALSE;
-    }
-    
-    if (fsi->num_bitmaps > 0) {
-        WARN("cache had bitmaps, unsure of how to deal with these\n");
-        free_traverse_ptr(&tp);
-        return FALSE;
-    }
-    
-    inode = fsi->key.obj_id;
-    
-    searchkey = fsi->key;
-#ifdef _DEBUG
-    num_entries = fsi->num_entries;
-#endif
-    
-    Status = find_item(Vcb, Vcb->root_root, &tp2, &searchkey, FALSE);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        free_traverse_ptr(&tp);
-        return FALSE;
-    }
-    
-    free_traverse_ptr(&tp);
-    
-    if (keycmp(&tp2.item->key, &searchkey)) {
-        WARN("(%llx,%x,%llx) not found\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
-        free_traverse_ptr(&tp2);
-        return FALSE;
-    }
-    
-    if (tp2.item->size < sizeof(INODE_ITEM)) {
-        WARN("(%llx,%x,%llx) was %u bytes, expected %u\n", tp2.item->key.obj_id, tp2.item->key.obj_type, tp2.item->key.offset, tp2.item->size, sizeof(INODE_ITEM));
-        free_traverse_ptr(&tp2);
-        return FALSE;
-    }
-    
-    ii = (INODE_ITEM*)tp2.item->data;
-    
-    data = ExAllocatePoolWithTag(PagedPool, ii->st_size, ALLOC_TAG);
-    
-    if (!data) {
-        ERR("out of memory\n");
-        free_traverse_ptr(&tp2);
-        return FALSE;
-    }
-    
-    Status = read_file(Vcb, Vcb->root_root, inode, data, 0, ii->st_size, NULL);
-    if (!NT_SUCCESS(Status)) {
-        ERR("read_file returned %08x\n", Status);
-        ExFreePool(data);
-        free_traverse_ptr(&tp2);
-        return FALSE;
-    }
-    
-    num_sectors = ii->st_size / Vcb->superblock.sector_size;
-    
-    generation = *(data + (num_sectors * sizeof(UINT32)));
-    
-    if (generation != Vcb->superblock.cache_generation) {
-        ERR("generation was %llx, expected %llx\n", generation, Vcb->superblock.cache_generation);
-        ExFreePool(data);
-        free_traverse_ptr(&tp2);
-        return FALSE;
-    }
-    
-    checksums = ExAllocatePoolWithTag(PagedPool, sizeof(UINT32) * num_sectors, ALLOC_TAG); // FIXME - get rid of this
-    
-    if (!checksums) {
-        ERR("out of memory\n");
-        ExFreePool(data);
-        free_traverse_ptr(&tp2);
-        return FALSE;
-    }
-    
-    RtlCopyMemory(checksums, data, sizeof(UINT32) * num_sectors);
-    
-    for (i = 0; i < num_sectors; i++) {
-        if (i * Vcb->superblock.sector_size > sizeof(UINT32) * num_sectors)
-            crc32 = ~calc_crc32c(0xffffffff, &data[i * Vcb->superblock.sector_size], Vcb->superblock.sector_size);
-        else if ((i + 1) * Vcb->superblock.sector_size < sizeof(UINT32) * num_sectors)
-            crc32 = 0; // FIXME - test this
-        else
-            crc32 = ~calc_crc32c(0xffffffff, &data[sizeof(UINT32) * num_sectors], ((i + 1) * Vcb->superblock.sector_size) - (sizeof(UINT32) * num_sectors));
-        
-        if (crc32 != checksums[i]) {
-            WARN("checksum %llu was %08x, expected %08x\n", i, crc32, checksums[i]);
-            ExFreePool(checksums);
-            ExFreePool(data);
-            free_traverse_ptr(&tp2);
-            return FALSE;
-        }
-    }
-    
-    ExFreePool(checksums);
-    
-#ifdef _DEBUG
-    fse = (FREE_SPACE_ENTRY*)&data[(sizeof(UINT32) * num_sectors) + sizeof(UINT64)];
-
-    for (i = 0; i < num_entries; i++) {
-        TRACE("(%llx,%llx,%x)\n", fse[i].offset, fse[i].size, fse[i].type);
-    }
-#endif
-    
-    FIXME("FIXME - read cache\n");
-    
-    ExFreePool(data);
-    free_traverse_ptr(&tp2);
-    
-    return FALSE;
-}
-
-static NTSTATUS load_free_space_cache(device_extension* Vcb, chunk* c) {
-    traverse_ptr tp, next_tp;
-    KEY searchkey;
-    UINT64 lastaddr;
-    BOOL b;
-    space *s, *s2;
-    LIST_ENTRY* le;
-    NTSTATUS Status;
-    
-    load_stored_free_space_cache(Vcb, c);
-    
-    TRACE("generating free space cache for chunk %llx\n", c->offset);
-    
-    searchkey.obj_id = c->offset;
-    searchkey.obj_type = TYPE_EXTENT_ITEM;
-    searchkey.offset = 0;
-    
-    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE);
-    if (!NT_SUCCESS(Status)) {
-        ERR("error - find_item returned %08x\n", Status);
-        return Status;
-    }
-    
-    lastaddr = c->offset;
-    
-    do {
-        if (tp.item->key.obj_id >= c->offset + c->chunk_item->size)
-            break;
-        
-        if (tp.item->key.obj_id >= c->offset && (tp.item->key.obj_type == TYPE_EXTENT_ITEM || tp.item->key.obj_type == TYPE_METADATA_ITEM)) {
-            if (tp.item->key.obj_id > lastaddr) {
-                s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
-                
-                if (!s) {
-                    ERR("out of memory\n");
-                    return STATUS_INSUFFICIENT_RESOURCES;
-                }
-                
-                s->offset = lastaddr;
-                s->size = tp.item->key.obj_id - lastaddr;
-                s->type = SPACE_TYPE_FREE;
-                InsertTailList(&c->space, &s->list_entry);
-                
-                TRACE("(%llx,%llx)\n", s->offset, s->size);
-            }
-            
-            if (tp.item->key.obj_type == TYPE_METADATA_ITEM)
-                lastaddr = tp.item->key.obj_id + Vcb->superblock.node_size;
-            else
-                lastaddr = tp.item->key.obj_id + tp.item->key.offset;
-        }
-        
-        b = find_next_item(Vcb, &tp, &next_tp, FALSE);
-        if (b) {
-            free_traverse_ptr(&tp);
-            tp = next_tp;
-        }
-    } while (b);
-    
-    if (lastaddr < c->offset + c->chunk_item->size) {
-        s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
-        
-        if (!s) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
-        
-        s->offset = lastaddr;
-        s->size = c->offset + c->chunk_item->size - lastaddr;
-        s->type = SPACE_TYPE_FREE;
-        InsertTailList(&c->space, &s->list_entry);
-        
-        TRACE("(%llx,%llx)\n", s->offset, s->size);
-    }
-    
-    free_traverse_ptr(&tp);
-    
-    // add allocated space
-    
-    lastaddr = c->offset;
-    
-    le = c->space.Flink;
-    while (le != &c->space) {
-        s = CONTAINING_RECORD(le, space, list_entry);
-        
-        if (s->offset > lastaddr) {
-            s2 = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
-            
-            if (!s2) {
-                ERR("out of memory\n");
-                return STATUS_INSUFFICIENT_RESOURCES;
-            }
-            
-            s2->offset = lastaddr;
-            s2->size = s->offset - lastaddr;
-            s2->type = SPACE_TYPE_USED;
-            
-            InsertTailList(&s->list_entry, &s2->list_entry);
-        }
-        
-        lastaddr = s->offset + s->size;
-        
-        le = le->Flink;
-    }
-    
-    if (lastaddr < c->offset + c->chunk_item->size) {
-        s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
-        
-        if (!s) {
-            ERR("out of memory\n");
-            return STATUS_INSUFFICIENT_RESOURCES;
-        }
-        
-        s->offset = lastaddr;
-        s->size = c->offset + c->chunk_item->size - lastaddr;
-        s->type = SPACE_TYPE_USED;
-        InsertTailList(&c->space, &s->list_entry);
-    }
-    
-    le = c->space.Flink;
-    while (le != &c->space) {
-        s = CONTAINING_RECORD(le, space, list_entry);
-        
-        TRACE("%llx,%llx,%u\n", s->offset, s->size, s->type);
-        
-        le = le->Flink;
-    }
-    
-    return STATUS_SUCCESS;
-}
-
 void protect_superblocks(device_extension* Vcb, chunk* c) {
     int i = 0, j;
-    UINT64 addr;
+    UINT64 off_start, off_end;
     
     // FIXME - this will need modifying for RAID
     
@@ -3029,23 +3130,14 @@ void protect_superblocks(device_extension* Vcb, chunk* c) {
         
         for (j = 0; j < ci->num_stripes; j++) {
             if (cis[j].offset + ci->size > superblock_addrs[i] && cis[j].offset <= superblock_addrs[i] + sizeof(superblock)) {
-                UINT32 size;
-                
                 TRACE("cut out superblock in chunk %llx\n", c->offset);
                 
-                addr = (superblock_addrs[i] - cis[j].offset) + c->offset;
-                TRACE("addr %llx\n", addr);
+                // The Linux driver protects the whole stripe in which the superblock lives
                 
-                // This prevents trees from spanning a stripe boundary, which btrfs check complains
-                // about. It also prevents the chunk tree being placed at 0x11000, which for some
-                // reason makes the FS unmountable on Linux (it tries to read 0x10000, i.e. the 
-                // superblock, instead).
-                if (ci->type & BLOCK_FLAG_SYSTEM || ci->type & BLOCK_FLAG_METADATA)
-                    size = max(sizeof(superblock), Vcb->superblock.node_size);
-                else
-                    size = sizeof(superblock);
+                off_start = ((superblock_addrs[i] - cis[j].offset) / c->chunk_item->stripe_length) * c->chunk_item->stripe_length;
+                off_end = sector_align(superblock_addrs[i] - cis[j].offset + sizeof(superblock), c->chunk_item->stripe_length);
                 
-                add_to_space_list(c, addr, size, SPACE_TYPE_USED);
+                add_to_space_list(c, c->offset + off_start, off_end - off_start, SPACE_TYPE_USED);
             }
         }
         
@@ -3091,7 +3183,6 @@ static NTSTATUS STDCALL find_chunk_usage(device_extension* Vcb) {
             }
         }
         
-        free_traverse_ptr(&tp);
 //         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];
 // 
@@ -3218,6 +3309,7 @@ static root* find_default_subvol(device_extension* Vcb) {
     UINT64 inode;
     UINT8 type;
     UNICODE_STRING filename;
+    LIST_ENTRY* le;
     
     static WCHAR fn[] = L"default";
     static UINT32 crc32 = 0x8dbfc2d2;
@@ -3232,11 +3324,51 @@ static root* find_default_subvol(device_extension* Vcb) {
             return subvol;
     }
     
-    subvol = Vcb->roots;
-    while (subvol && subvol->id != BTRFS_ROOT_FSTREE)
-        subvol = subvol->next;
+    le = Vcb->roots.Flink;
+    while (le != &Vcb->roots) {
+        root* r = CONTAINING_RECORD(le, root, list_entry);
+        
+        if (r->id == BTRFS_ROOT_FSTREE)
+            return r;
+        
+        le = le->Flink;
+    }
     
-    return subvol;
+    return NULL;
+}
+
+static BOOL is_device_removable(PDEVICE_OBJECT devobj) {
+    NTSTATUS Status;
+    STORAGE_HOTPLUG_INFO shi;
+    
+    Status = dev_ioctl(devobj, IOCTL_STORAGE_GET_HOTPLUG_INFO, NULL, 0, &shi, sizeof(STORAGE_HOTPLUG_INFO), TRUE, NULL);
+    
+    if (!NT_SUCCESS(Status)) {
+        ERR("dev_ioctl returned %08x\n", Status);
+        return FALSE;
+    }
+    
+    return shi.MediaRemovable != 0 ? TRUE : FALSE;
+}
+
+static ULONG get_device_change_count(PDEVICE_OBJECT devobj) {
+    NTSTATUS Status;
+    ULONG cc;
+    IO_STATUS_BLOCK iosb;
+    
+    Status = dev_ioctl(devobj, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, &cc, sizeof(ULONG), TRUE, &iosb);
+    
+    if (!NT_SUCCESS(Status)) {
+        ERR("dev_ioctl returned %08x\n", Status);
+        return 0;
+    }
+    
+    if (iosb.Information < sizeof(ULONG)) {
+        ERR("iosb.Information was too short\n");
+        return 0;
+    }
+    
+    return cc;
 }
 
 static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
@@ -3250,6 +3382,7 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     LIST_ENTRY* le;
     KEY searchkey;
     traverse_ptr tp;
+    fcb* root_fcb = NULL;
     
     TRACE("mount_vol called\n");
     
@@ -3269,9 +3402,10 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
 //     }
 
     Status = dev_ioctl(DeviceToMount, IOCTL_DISK_GET_PARTITION_INFO_EX, NULL, 0,
-                       &piex, sizeof(piex), TRUE);
+                       &piex, sizeof(piex), TRUE, NULL);
     if (!NT_SUCCESS(Status)) {
         ERR("error reading partition information: %08x\n", Status);
+        Status = STATUS_UNRECOGNIZED_VOLUME;
         goto exit;
     }
 
@@ -3282,8 +3416,11 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
                             0,
                             FALSE,
                             &NewDeviceObject);
-    if (!NT_SUCCESS(Status))
+    if (!NT_SUCCESS(Status)) {
+        ERR("IoCreateDevice returned %08x\n", Status);
+        Status = STATUS_UNRECOGNIZED_VOLUME;
         goto exit;
+    }
     
 //     TRACE("DEV_ITEM = %x, superblock = %x\n", sizeof(DEV_ITEM), sizeof(superblock));
 
@@ -3291,8 +3428,6 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     Vcb = (PVOID)NewDeviceObject->DeviceExtension;
     RtlZeroMemory(Vcb, sizeof(device_extension));
     
-    InitializeListHead(&Vcb->tree_cache);
-    
     ExInitializeResourceLite(&Vcb->tree_lock);
     Vcb->tree_lock_counter = 0;
     Vcb->open_trees = 0;
@@ -3392,6 +3527,8 @@ 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].removable = is_device_removable(Vcb->devices[0].devobj);
+    Vcb->devices[0].change_count = Vcb->devices[0].removable ? get_device_change_count(Vcb->devices[0].devobj) : 0;
     
     if (Vcb->superblock.num_devices > 1)
         RtlZeroMemory(&Vcb->devices[1], sizeof(DEV_ITEM) * (Vcb->superblock.num_devices - 1));
@@ -3409,7 +3546,9 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     
 //     Vcb->root_tree_phys_addr = logical_to_physical(Vcb, Vcb->superblock.root_tree_addr);
     
-    Vcb->roots = NULL;
+    InitializeListHead(&Vcb->roots);
+    InitializeListHead(&Vcb->drop_roots);
+    
     Vcb->log_to_phys_loaded = FALSE;
     
     Vcb->max_inline = Vcb->superblock.node_size / 2;
@@ -3463,6 +3602,16 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         ERR("find_chunk_usage returned %08x\n", Status);
         goto exit;
     }
+        
+    // 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);
+        if (!NT_SUCCESS(Status)) {
+            ERR("clear_free_space_cache returned %08x\n", Status);
+            goto exit;
+        }
+    }
     
     Vcb->volume_fcb = create_fcb();
     if (!Vcb->volume_fcb) {
@@ -3474,48 +3623,35 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     Vcb->volume_fcb->Vcb = Vcb;
     Vcb->volume_fcb->sd = NULL;
     
-    Vcb->root_fcb = create_fcb();
-    if (!Vcb->root_fcb) {
-        ERR("out of memory\n");
-        Status = STATUS_INSUFFICIENT_RESOURCES;
-        goto exit;
-    }
-    
-    Vcb->root_fcb->Vcb = Vcb;
-    Vcb->root_fcb->inode = SUBVOL_ROOT_INODE;
-    Vcb->root_fcb->type = BTRFS_TYPE_DIRECTORY;
-    
-    Vcb->root_fcb->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, sizeof(WCHAR), ALLOC_TAG);
-    
-    if (!Vcb->root_fcb->full_filename.Buffer) {
+    root_fcb = create_fcb();
+    if (!root_fcb) {
         ERR("out of memory\n");
         Status = STATUS_INSUFFICIENT_RESOURCES;
         goto exit;
     }
     
-    Vcb->root_fcb->full_filename.Buffer[0] = '\\';
-    Vcb->root_fcb->full_filename.Length = Vcb->root_fcb->full_filename.MaximumLength = sizeof(WCHAR);
+    root_fcb->Vcb = Vcb;
+    root_fcb->inode = SUBVOL_ROOT_INODE;
+    root_fcb->type = BTRFS_TYPE_DIRECTORY;
     
 #ifdef DEBUG_FCB_REFCOUNTS
     WARN("volume FCB = %p\n", Vcb->volume_fcb);
-    WARN("root FCB = %p\n", Vcb->root_fcb);
+    WARN("root FCB = %p\n", root_fcb);
 #endif
     
-    Vcb->root_fcb->subvol = find_default_subvol(Vcb);
+    root_fcb->subvol = find_default_subvol(Vcb);
 
-    if (!Vcb->root_fcb->subvol) {
+    if (!root_fcb->subvol) {
         ERR("could not find top subvol\n");
         Status = STATUS_INTERNAL_ERROR;
         goto exit;
     }
     
-    Vcb->fcbs = Vcb->root_fcb;
-    
-    searchkey.obj_id = Vcb->root_fcb->inode;
+    searchkey.obj_id = root_fcb->inode;
     searchkey.obj_type = TYPE_INODE_ITEM;
     searchkey.offset = 0xffffffffffffffff;
     
-    Status = find_item(Vcb, Vcb->root_fcb->subvol, &tp, &searchkey, FALSE);
+    Status = find_item(Vcb, root_fcb->subvol, &tp, &searchkey, FALSE);
     if (!NT_SUCCESS(Status)) {
         ERR("error - find_item returned %08x\n", Status);
         goto exit;
@@ -3524,20 +3660,37 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
         ERR("couldn't find INODE_ITEM for root directory\n");
         Status = STATUS_INTERNAL_ERROR;
-        free_traverse_ptr(&tp);
         goto exit;
     }
     
     if (tp.item->size > 0)
-        RtlCopyMemory(&Vcb->root_fcb->inode_item, tp.item->data, min(sizeof(INODE_ITEM), tp.item->size));
+        RtlCopyMemory(&root_fcb->inode_item, tp.item->data, min(sizeof(INODE_ITEM), tp.item->size));
     
-    free_traverse_ptr(&tp);
+    fcb_get_sd(root_fcb, NULL);
     
-    fcb_get_sd(Vcb->root_fcb);
+    root_fcb->atts = get_file_attributes(Vcb, &root_fcb->inode_item, root_fcb->subvol, root_fcb->inode, root_fcb->type, FALSE, FALSE);
     
-    Vcb->root_fcb->atts = get_file_attributes(Vcb, &Vcb->root_fcb->inode_item, Vcb->root_fcb->subvol, Vcb->root_fcb->inode, Vcb->root_fcb->type,
-                                              FALSE, FALSE);
-      
+    Vcb->root_fileref = create_fileref();
+    if (!Vcb->root_fileref) {
+        ERR("out of memory\n");
+        Status = STATUS_INSUFFICIENT_RESOURCES;
+        goto exit;
+    }
+    
+    Vcb->root_fileref->fcb = root_fcb;
+    InsertTailList(&root_fcb->subvol->fcbs, &root_fcb->list_entry);
+    
+    Vcb->root_fileref->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, sizeof(WCHAR), ALLOC_TAG);
+    
+    if (!Vcb->root_fileref->full_filename.Buffer) {
+        ERR("out of memory\n");
+        Status = STATUS_INSUFFICIENT_RESOURCES;
+        goto exit;
+    }
+    
+    Vcb->root_fileref->full_filename.Buffer[0] = '\\';
+    Vcb->root_fileref->full_filename.Length = Vcb->root_fileref->full_filename.MaximumLength = sizeof(WCHAR);
+
     for (i = 0; i < Vcb->superblock.num_devices; i++) {
         Status = find_disk_holes(Vcb, &Vcb->devices[i]);
         if (!NT_SUCCESS(Status)) {
@@ -3607,12 +3760,6 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
 //     RtlCopyMemory(NewDeviceObject->Vpb->VolumeLabel,
 //                   Vcb->NtfsInfo.VolumeLabel,
 //                   Vcb->NtfsInfo.VolumeLabelLength);
-    
-    Status = PsCreateSystemThread(&Vcb->flush_thread_handle, 0, NULL, NULL, NULL, flush_thread, Vcb);
-    if (!NT_SUCCESS(Status)) {
-        ERR("PsCreateSystemThread returned %08x\n", Status);
-        goto exit;
-    }
 
     NewDeviceObject->Vpb = Stack->Parameters.MountVolume.Vpb;
     Stack->Parameters.MountVolume.Vpb->DeviceObject = NewDeviceObject;
@@ -3623,6 +3770,12 @@ static NTSTATUS STDCALL mount_vol(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     NewDeviceObject->Vpb->VolumeLabel[1] = 0;
     NewDeviceObject->Vpb->ReferenceCount++; // FIXME - should we deref this at any point?
     
+    Status = PsCreateSystemThread(&Vcb->flush_thread_handle, 0, NULL, NULL, NULL, flush_thread, NewDeviceObject);
+    if (!NT_SUCCESS(Status)) {
+        ERR("PsCreateSystemThread returned %08x\n", Status);
+        goto exit;
+    }
+    
     Status = STATUS_SUCCESS;
 
 exit:
@@ -3648,8 +3801,10 @@ exit:
 
     if (!NT_SUCCESS(Status)) {
         if (Vcb) {
-            if (Vcb->root_fcb)
-                free_fcb(Vcb->root_fcb);
+            if (Vcb->root_fileref)
+                free_fileref(Vcb->root_fileref);
+            else if (root_fcb)
+                free_fcb(root_fcb);
 
             if (Vcb->volume_fcb)
                 free_fcb(Vcb->volume_fcb);
@@ -3707,10 +3862,8 @@ static NTSTATUS STDCALL drv_file_system_control(IN PDEVICE_OBJECT DeviceObject,
             
         case IRP_MN_KERNEL_CALL:
             TRACE("IRP_MN_KERNEL_CALL\n");
-            break;
             
-        case IRP_MN_LOAD_FILE_SYSTEM:
-            TRACE("IRP_MN_LOAD_FILE_SYSTEM\n");
+            status = fsctl_request(DeviceObject, Irp, IrpSp->Parameters.FileSystemControl.FsControlCode, FALSE);
             break;
             
         case IRP_MN_USER_FS_REQUEST:
@@ -3720,13 +3873,11 @@ static NTSTATUS STDCALL drv_file_system_control(IN PDEVICE_OBJECT DeviceObject,
             break;
             
         case IRP_MN_VERIFY_VOLUME:
-            TRACE("IRP_MN_VERIFY_VOLUME\n");
+            FIXME("STUB: IRP_MN_VERIFY_VOLUME\n");
             break;
            
         default:
-            WARN("unknown minor %u\n", IrpSp->MinorFunction);
             break;
-            
     }
 
     Irp->IoStatus.Status = status;
@@ -3754,6 +3905,8 @@ static NTSTATUS STDCALL drv_lock_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP
     TRACE("lock control\n");
     
     Status = FsRtlProcessFileLock(&fcb->lock, Irp, NULL);
+
+    fcb->Header.IsFastIoPossible = fast_io_possible(fcb);
     
     if (top_level) 
         IoSetTopLevelIrp(NULL);
@@ -3800,7 +3953,7 @@ static NTSTATUS STDCALL drv_device_control(IN PDEVICE_OBJECT DeviceObject, IN PI
         FIXME("FIXME - pass through\n");
         Status = STATUS_NOT_IMPLEMENTED;
     } else {
-        TRACE("filename = %.*S\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
+        TRACE("filename = %S\n", file_desc(FileObject));
         
         switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
             case IOCTL_MOUNTDEV_QUERY_DEVICE_NAME:
@@ -3849,7 +4002,7 @@ static NTSTATUS STDCALL drv_shutdown(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp
         
         TRACE("shutting down Vcb %p\n", Vcb);
         
-        uninit(Vcb);
+        uninit(Vcb, TRUE);
     }
     
     Irp->IoStatus.Status = Status;
@@ -3865,65 +4018,25 @@ static NTSTATUS STDCALL drv_shutdown(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp
     return Status;
 }
 
-static NTSTATUS STDCALL drv_pnp(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
-    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
-    device_extension* Vcb = DeviceObject->DeviceExtension;
-    NTSTATUS Status;
-    BOOL top_level;
-
-    FIXME("STUB: pnp\n");
+BOOL is_file_name_valid(PUNICODE_STRING us) {
+    ULONG i;
     
-    FsRtlEnterFileSystem();
-
-    top_level = is_top_level(Irp);
+    if (us->Length < sizeof(WCHAR))
+        return FALSE;
     
-    Status = STATUS_NOT_IMPLEMENTED;
+    if (us->Length > 255 * sizeof(WCHAR))
+        return FALSE;
     
-    switch (IrpSp->MinorFunction) {
-        case IRP_MN_CANCEL_REMOVE_DEVICE:
-            TRACE("    IRP_MN_CANCEL_REMOVE_DEVICE\n");
-            break;
-
-        case IRP_MN_QUERY_REMOVE_DEVICE:
-            TRACE("    IRP_MN_QUERY_REMOVE_DEVICE\n");
-            break;
-
-        case IRP_MN_REMOVE_DEVICE:
-            TRACE("    IRP_MN_REMOVE_DEVICE\n");
-            break;
-
-        case IRP_MN_START_DEVICE:
-            TRACE("    IRP_MN_START_DEVICE\n");
-            break;
-
-        case IRP_MN_SURPRISE_REMOVAL:
-            TRACE("    IRP_MN_SURPRISE_REMOVAL\n");
-            break;
-            
-        case IRP_MN_QUERY_DEVICE_RELATIONS:
-            TRACE("    IRP_MN_QUERY_DEVICE_RELATIONS\n");
-            break;
-        
-        default:
-            WARN("Unrecognized minor function 0x%x\n", IrpSp->MinorFunction);
-            break;
+    for (i = 0; i < us->Length / sizeof(WCHAR); i++) {
+        if (us->Buffer[i] == '/' || us->Buffer[i] == '<' || us->Buffer[i] == '>' || us->Buffer[i] == ':' || us->Buffer[i] == '"' ||
+            us->Buffer[i] == '|' || us->Buffer[i] == '?' || us->Buffer[i] == '*' || (us->Buffer[i] >= 1 && us->Buffer[i] <= 31))
+            return FALSE;
     }
-
-//     Irp->IoStatus.Status = Status;
-//     Irp->IoStatus.Information = 0;
-
-    IoSkipCurrentIrpStackLocation(Irp);
     
-    Status = IoCallDriver(Vcb->devices[0].devobj, Irp);
-
-//     IoCompleteRequest(Irp, IO_NO_INCREMENT);
-    
-    if (top_level) 
-        IoSetTopLevelIrp(NULL);
+    if (us->Buffer[0] == '.' && (us->Length == sizeof(WCHAR) || (us->Length == 2 * sizeof(WCHAR) && us->Buffer[1] == '.')))
+        return FALSE;
     
-    FsRtlExitFileSystem();
-
-    return Status;
+    return TRUE;
 }
 
 #ifdef _DEBUG
index ad5ef71..b3a2398 100644 (file)
@@ -37,6 +37,7 @@ static const UINT64 superblock_addrs[] = { 0x10000, 0x4000000, 0x4000000000, 0x4
 #define TYPE_DEV_EXTENT        0xCC
 #define TYPE_DEV_ITEM          0xD8
 #define TYPE_CHUNK_ITEM        0xE4
+#define TYPE_SUBVOL_UUID       0xFB
 
 #define BTRFS_ROOT_ROOT         1
 #define BTRFS_ROOT_EXTENT       2
@@ -44,6 +45,7 @@ static const UINT64 superblock_addrs[] = { 0x10000, 0x4000000, 0x4000000000, 0x4
 #define BTRFS_ROOT_DEVTREE      4
 #define BTRFS_ROOT_FSTREE       5
 #define BTRFS_ROOT_CHECKSUM     7
+#define BTRFS_ROOT_UUID         9
 
 #define BTRFS_COMPRESSION_NONE  0
 #define BTRFS_COMPRESSION_ZLIB  1
@@ -155,6 +157,33 @@ typedef struct {
 } DEV_ITEM;
 
 #define SYS_CHUNK_ARRAY_SIZE 0x800
+#define BTRFS_NUM_BACKUP_ROOTS 4
+
+typedef struct {
+    UINT64 root_tree_addr;
+    UINT64 root_tree_generation;
+    UINT64 chunk_tree_addr;
+    UINT64 chunk_tree_generation;
+    UINT64 extent_tree_addr;
+    UINT64 extent_tree_generation;
+    UINT64 fs_tree_addr;
+    UINT64 fs_tree_generation;
+    UINT64 dev_root_addr;
+    UINT64 dev_root_generation;
+    UINT64 csum_root_addr;
+    UINT64 csum_root_generation;
+    UINT64 total_bytes;
+    UINT64 bytes_used;
+    UINT64 num_devices;
+    UINT64 reserved[4];
+    UINT8 root_level;
+    UINT8 chunk_root_level;
+    UINT8 extent_root_level;
+    UINT8 fs_root_level;
+    UINT8 dev_root_level;
+    UINT8 csum_root_level;
+    UINT8 reserved2[10];
+} superblock_backup;
 
 typedef struct {
     UINT8 checksum[32];
@@ -190,8 +219,8 @@ typedef struct {
     UINT64 uuid_tree_generation;
     UINT64 reserved[30];
     UINT8 sys_chunk_array[SYS_CHUNK_ARRAY_SIZE];
-//     struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
-    UINT8 reserved2[1237];
+    superblock_backup backup[BTRFS_NUM_BACKUP_ROOTS];
+    UINT8 reserved2[565];
 } superblock;
 
 #define BTRFS_TYPE_UNKNOWN   0
@@ -324,6 +353,11 @@ typedef struct {
     UINT64 flags;
 } EXTENT_ITEM;
 
+typedef struct {
+    KEY firstitem;
+    UINT8 level;
+} EXTENT_ITEM2;
+
 typedef struct {
     UINT32 refcount;
 } EXTENT_ITEM_V0;
@@ -355,7 +389,7 @@ typedef struct {
     UINT64 root;
     UINT64 gen;
     UINT64 objid;
-    UINT64 count;
+    UINT32 count;
 } EXTENT_REF_V0;
 
 typedef struct {
index b4b5116..b3ba777 100644 (file)
@@ -70,12 +70,12 @@ BEGIN
         BLOCK "080904b0"
         BEGIN
             VALUE "FileDescription", "WinBtrfs"
-            VALUE "FileVersion", "0.2"
+            VALUE "FileVersion", "0.4"
             VALUE "InternalName", "btrfs"
             VALUE "LegalCopyright", "Copyright (c) Mark Harmstone 2016"
             VALUE "OriginalFilename", "btrfs.sys"
             VALUE "ProductName", "WinBtrfs"
-            VALUE "ProductVersion", "0.2"
+            VALUE "ProductVersion", "0.4"
         END
     END
     BLOCK "VarFileInfo"
index df44b60..0aae320 100644 (file)
@@ -36,6 +36,7 @@
 #ifdef __REACTOS__
 #include <rtlfuncs.h>
 #include <iotypes.h>
+#include <pseh/pseh2.h>
 #endif /* __REACTOS__ */
 //#include <windows.h>
 #include <windef.h>
@@ -46,7 +47,6 @@
 #include "btrfs.h"
 
 #ifdef _DEBUG
-// #define DEBUG_TREE_REFCOUNTS
 // #define DEBUG_FCB_REFCOUNTS
 // #define DEBUG_LONG_MESSAGES
 #define DEBUG_PARANOID
@@ -68,6 +68,9 @@
 #define EA_DOSATTRIB "user.DOSATTRIB"
 #define EA_DOSATTRIB_HASH 0x914f9939
 
+#define EA_REPARSE "system.reparse"
+#define EA_REPARSE_HASH 0x786f6167
+
 #define READ_AHEAD_GRANULARITY 0x10000 // 64 KB
 
 #ifdef _MSC_VER
@@ -108,26 +111,18 @@ typedef struct _fcb {
     struct _fcb_nonpaged* nonpaged;
     LONG refcount;
     LONG open_count;
-    UNICODE_STRING filepart;
-    ANSI_STRING utf8;
     struct _device_extension* Vcb;
-    struct _fcb* par;
-    struct _fcb* prev;
-    struct _fcb* next;
     struct _root* subvol;
-    LIST_ENTRY children;
     UINT64 inode;
     UINT8 type;
-    BOOL delete_on_close;
     INODE_ITEM inode_item;
-    UNICODE_STRING full_filename;
-    ULONG name_offset;
     SECURITY_DESCRIPTOR* sd;
     FILE_LOCK lock;
     BOOL deleted;
     PKTHREAD lazy_writer_thread;
     ULONG atts;
     SHARE_ACCESS share_access;
+    WCHAR* debug_desc;
     
     BOOL ads;
     UINT32 adssize;
@@ -137,6 +132,24 @@ typedef struct _fcb {
     LIST_ENTRY list_entry;
 } fcb;
 
+struct _file_ref;
+
+typedef struct _file_ref {
+    fcb* fcb;
+    UNICODE_STRING filepart;
+    ANSI_STRING utf8;
+    UNICODE_STRING full_filename;
+    ULONG name_offset;
+    BOOL delete_on_close;
+    BOOL deleted;
+    LIST_ENTRY children;
+    LONG refcount;
+    struct _file_ref* parent;
+    WCHAR* debug_desc;
+    
+    LIST_ENTRY list_entry;
+} file_ref;
+
 typedef struct _ccb {
     USHORT NodeType;
     CSHORT NodeSize;
@@ -147,6 +160,8 @@ typedef struct _ccb {
     UNICODE_STRING query_string;
     BOOL has_wildcard;
     BOOL specific_file;
+    ACCESS_MASK access;
+    file_ref* fileref;
 } ccb;
 
 // typedef struct _log_to_phys {
@@ -203,7 +218,6 @@ typedef struct _tree {
 //     UINT64 address;
 //     UINT8 level;
     tree_header header;
-    LONG refcount;
     BOOL has_address;
     UINT32 size;
     struct _device_extension* Vcb;
@@ -216,6 +230,7 @@ typedef struct _tree {
     UINT64 new_address;
     BOOL has_new_address;
     UINT64 flags;
+    BOOL write;
 } tree;
 
 typedef struct {
@@ -229,9 +244,9 @@ typedef struct _root {
     root_nonpaged* nonpaged;
     UINT64 lastinode;
     ROOT_ITEM root_item;
-    
-    struct _root* prev;
-    struct _root* next;
+    UNICODE_STRING path;
+    LIST_ENTRY fcbs;
+    LIST_ENTRY list_entry;
 } root;
 
 typedef struct {
@@ -239,12 +254,6 @@ typedef struct {
     tree_data* item;
 } traverse_ptr;
 
-typedef struct _tree_cache {
-    tree* tree;
-    BOOL write;
-    LIST_ENTRY list_entry;
-} tree_cache;
-
 typedef struct _root_cache {
     root* root;
     struct _root_cache* next;
@@ -272,6 +281,8 @@ typedef struct {
 typedef struct {
     PDEVICE_OBJECT devobj;
     DEV_ITEM devitem;
+    BOOL removable;
+    ULONG change_count;
     LIST_ENTRY disk_holes;
 } device;
 
@@ -283,6 +294,8 @@ typedef struct {
     UINT32 oldused;
     BOOL space_changed;
     device** devices;
+    UINT64 cache_size;
+    UINT64 cache_inode;
     LIST_ENTRY space;
     LIST_ENTRY list_entry;
 } chunk;
@@ -301,9 +314,9 @@ typedef struct _device_extension {
     superblock superblock;
 //     WCHAR label[MAX_LABEL_SIZE];
     BOOL readonly;
-    fcb* fcbs;
+    BOOL removing;
     fcb* volume_fcb;
-    fcb* root_fcb;
+    file_ref* root_fileref;
     ERESOURCE DirResource;
     KSPIN_LOCK FcbListLock;
     ERESOURCE fcb_lock;
@@ -318,20 +331,20 @@ typedef struct _device_extension {
 //     UINT64 chunk_root_phys_addr;
     UINT64 root_tree_phys_addr;
 //     log_to_phys* log_to_phys;
-    root* roots;
+    LIST_ENTRY roots;
+    LIST_ENTRY drop_roots;
     root* chunk_root;
     root* root_root;
     root* extent_root;
     root* checksum_root;
     root* dev_root;
+    root* uuid_root;
     BOOL log_to_phys_loaded;
     UINT32 max_inline;
     LIST_ENTRY sys_chunks;
     LIST_ENTRY chunks;
     LIST_ENTRY trees;
-    LIST_ENTRY tree_cache;
     HANDLE flush_thread_handle;
-    KTIMER flush_thread_timer;
     LIST_ENTRY list_entry;
 } device_extension;
 
@@ -341,6 +354,43 @@ typedef struct {
     UINT32 uid;
 } uid_map;
 
+typedef struct {
+    LIST_ENTRY list_entry;
+    UINT64 key;
+} ordered_list;
+
+typedef struct {
+    ordered_list ol;
+    ULONG length;
+    UINT32* checksums;
+    BOOL deleted;
+} changed_sector;
+
+enum write_tree_status {
+    WriteTreeStatus_Pending,
+    WriteTreeStatus_Success,
+    WriteTreeStatus_Error,
+    WriteTreeStatus_Cancelling,
+    WriteTreeStatus_Cancelled
+};
+
+struct write_tree_context;
+
+typedef struct {
+    struct write_tree_context* context;
+    UINT8* buf;
+    device* device;
+    PIRP Irp;
+    IO_STATUS_BLOCK iosb;
+    enum write_tree_status status;
+    LIST_ENTRY list_entry;
+} write_tree_stripe;
+
+typedef struct {
+    KEVENT Event;
+    LIST_ENTRY stripes;
+} write_tree_context;
+
 // #pragma pack(pop)
 
 static __inline void init_tree_holder(tree_holder* th) {
@@ -368,6 +418,27 @@ static __inline void win_time_to_unix(LARGE_INTEGER t, BTRFS_TIME* out) {
     out->nanoseconds = (l % 10000000) * 100;
 }
 
+static __inline void insert_into_ordered_list(LIST_ENTRY* list, ordered_list* ins) {
+    LIST_ENTRY* le = list->Flink;
+    ordered_list* ol;
+    
+    while (le != list) {
+        ol = (ordered_list*)le;
+        
+        if (ol->key > ins->key) {
+            le->Blink->Flink = &ins->list_entry;
+            ins->list_entry.Blink = le->Blink;
+            le->Blink = &ins->list_entry;
+            ins->list_entry.Flink = le;
+            return;
+        }
+        
+        le = le->Flink;
+    }
+    
+    InsertTailList(list, &ins->list_entry);
+}
+
 // in btrfs.c
 device* find_device_from_uuid(device_extension* Vcb, BTRFS_UUID* uuid);
 ULONG sector_align( ULONG NumberToBeAligned, ULONG Alignment );
@@ -377,15 +448,25 @@ BOOL STDCALL get_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char*
 NTSTATUS STDCALL set_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char* name, UINT32 crc32, UINT8* data, UINT16 datalen, LIST_ENTRY* rollback);
 BOOL STDCALL delete_xattr(device_extension* Vcb, root* subvol, UINT64 inode, char* name, UINT32 crc32, LIST_ENTRY* rollback);
 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);
 NTSTATUS add_dir_item(device_extension* Vcb, root* subvol, UINT64 inode, UINT32 crc32, DIR_ITEM* di, ULONG disize, LIST_ENTRY* rollback);
 NTSTATUS delete_dir_item(device_extension* Vcb, root* subvol, UINT64 parinode, UINT32 crc32, PANSI_STRING utf8, LIST_ENTRY* rollback);
 UINT64 find_next_dir_index(device_extension* Vcb, root* subvol, UINT64 inode);
 NTSTATUS delete_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UINT64 parinode, PANSI_STRING utf8, UINT64* index, LIST_ENTRY* rollback);
-NTSTATUS delete_fcb(fcb* fcb, PFILE_OBJECT FileObject, LIST_ENTRY* rollback);
+NTSTATUS delete_fileref(file_ref* fileref, PFILE_OBJECT FileObject, LIST_ENTRY* rollback);
 fcb* create_fcb();
+file_ref* create_fileref();
 void protect_superblocks(device_extension* Vcb, chunk* c);
 BOOL is_top_level(PIRP Irp);
+NTSTATUS create_root(device_extension* Vcb, UINT64 id, root** rootptr, BOOL no_tree, UINT64 offset, LIST_ENTRY* rollback);
+void STDCALL uninit(device_extension* Vcb, BOOL flush);
+NTSTATUS STDCALL dev_ioctl(PDEVICE_OBJECT DeviceObject, ULONG ControlCode, PVOID InputBuffer,
+                           ULONG InputBufferSize, PVOID OutputBuffer, ULONG OutputBufferSize, BOOLEAN Override, IO_STATUS_BLOCK* iosb);
+BOOL is_file_name_valid(PUNICODE_STRING us);
+void send_notification_fileref(file_ref* fileref, ULONG filter_match, ULONG action);
+WCHAR* file_desc(PFILE_OBJECT FileObject);
+WCHAR* file_desc_fileref(file_ref* fileref);
 
 #ifdef _MSC_VER
 #define funcname __FUNCTION__
@@ -395,26 +476,34 @@ BOOL is_top_level(PIRP Irp);
 
 // FIXME - we probably shouldn't be moving funcname etc. around if we're not printing debug messages
 #define free_fcb(fcb) _free_fcb(fcb, funcname, __FILE__, __LINE__)
+#define free_fileref(fileref) _free_fileref(fileref, funcname, __FILE__, __LINE__)
 
 #ifdef _DEBUG
 
+extern BOOL log_started;
+extern UINT32 debug_log_level;
+
 #ifdef DEBUG_LONG_MESSAGES
 
-#define TRACE(s, ...) _debug_message(funcname, 3, __FILE__, __LINE__, s, ##__VA_ARGS__)
-#define WARN(s, ...) _debug_message(funcname, 2, __FILE__, __LINE__, s, ##__VA_ARGS__)
-#define FIXME(s, ...) _debug_message(funcname, 1, __FILE__, __LINE__, s, ##__VA_ARGS__)
-#define ERR(s, ...) _debug_message(funcname, 1, __FILE__, __LINE__, s, ##__VA_ARGS__)
+#define MSG(fn, file, line, s, level, ...) (!log_started || level <= debug_log_level) ? _debug_message(fn, file, line, s, ##__VA_ARGS__) : 0
 
-void STDCALL _debug_message(const char* func, UINT8 priority, const char* file, unsigned int line, char* s, ...);
+#define TRACE(s, ...) MSG(funcname, __FILE__, __LINE__, s, 3, ##__VA_ARGS__)
+#define WARN(s, ...) MSG(funcname, __FILE__, __LINE__, s, 2, ##__VA_ARGS__)
+#define FIXME(s, ...) MSG(funcname, __FILE__, __LINE__, s, 1, ##__VA_ARGS__)
+#define ERR(s, ...) MSG(funcname, __FILE__, __LINE__, s, 1, ##__VA_ARGS__)
+
+void STDCALL _debug_message(const char* func, const char* file, unsigned int line, char* s, ...);
 
 #else
 
-#define TRACE(s, ...) _debug_message(funcname, 3, s, ##__VA_ARGS__)
-#define WARN(s, ...) _debug_message(funcname, 2, s, ##__VA_ARGS__)
-#define FIXME(s, ...) _debug_message(funcname, 1, s, ##__VA_ARGS__)
-#define ERR(s, ...) _debug_message(funcname, 1, s, ##__VA_ARGS__)
+#define MSG(fn, s, level, ...) (!log_started || level <= debug_log_level) ? _debug_message(fn, s, ##__VA_ARGS__) : 0
+
+#define TRACE(s, ...) MSG(funcname, s, 3, ##__VA_ARGS__)
+#define WARN(s, ...) MSG(funcname, s, 2, ##__VA_ARGS__)
+#define FIXME(s, ...) MSG(funcname, s, 1, ##__VA_ARGS__)
+#define ERR(s, ...) MSG(funcname, s, 1, ##__VA_ARGS__)
 
-void STDCALL _debug_message(const char* func, UINT8 priority, char* s, ...);
+void STDCALL _debug_message(const char* func, char* s, ...);
 
 #endif
 
@@ -432,6 +521,12 @@ void STDCALL _debug_message(const char* func, UINT8 priority, char* s, ...);
 
 #endif
 
+static __inline void increase_chunk_usage(chunk* c, UINT64 delta) {
+    c->used += delta;
+    
+    TRACE("increasing size of chunk %llx by %llx\n", c->offset, delta);
+}
+
 // in fastio.c
 void STDCALL init_fast_io_dispatch(FAST_IO_DISPATCH** fiod);
 
@@ -442,23 +537,22 @@ UINT32 STDCALL calc_crc32c(UINT32 seed, UINT8* msg, ULONG msglen);
 NTSTATUS STDCALL _find_item(device_extension* Vcb, root* r, traverse_ptr* tp, const KEY* searchkey, BOOL ignore, 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, const char* func, const char* file, unsigned int line);
 BOOL STDCALL _find_prev_item(device_extension* Vcb, const traverse_ptr* tp, traverse_ptr* prev_tp, BOOL ignore, const char* func, const char* file, unsigned int line);
-void STDCALL _free_traverse_ptr(traverse_ptr* tp, const char* func, const char* file, unsigned int line);
-void STDCALL free_tree_cache(LIST_ENTRY* tc);
+void STDCALL free_trees(device_extension* Vcb);
 BOOL STDCALL insert_tree_item(device_extension* Vcb, root* r, UINT64 obj_id, UINT8 obj_type, UINT64 offset, void* data, UINT32 size, traverse_ptr* ptp, LIST_ENTRY* rollback);
 void STDCALL delete_tree_item(device_extension* Vcb, traverse_ptr* tp, LIST_ENTRY* rollback);
-void STDCALL add_to_tree_cache(device_extension* Vcb, tree* t, BOOL write);
 tree* STDCALL _free_tree(tree* t, const char* func, const char* file, unsigned int line);
 NTSTATUS STDCALL _load_tree(device_extension* Vcb, UINT64 addr, root* r, tree** pt, 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, const char* func, const char* file, unsigned int line);
 void clear_rollback(LIST_ENTRY* rollback);
 void do_rollback(device_extension* Vcb, LIST_ENTRY* rollback);
+void free_trees_root(device_extension* Vcb, root* r);
+NTSTATUS STDCALL read_tree(device_extension* Vcb, UINT64 addr, UINT8* buf);
 
 #define find_item(Vcb, r, tp, searchkey, ignore) _find_item(Vcb, r, tp, searchkey, ignore, funcname, __FILE__, __LINE__)
 #define find_next_item(Vcb, tp, next_tp, ignore) _find_next_item(Vcb, tp, next_tp, ignore, funcname, __FILE__, __LINE__)
 #define find_prev_item(Vcb, tp, prev_tp, ignore) _find_prev_item(Vcb, tp, prev_tp, ignore, funcname, __FILE__, __LINE__)
 #define free_tree(t) _free_tree(t, funcname, __FILE__, __LINE__)
 #define load_tree(t, addr, r, pt) _load_tree(t, addr, r, pt, funcname, __FILE__, __LINE__)
-#define free_traverse_ptr(tp) _free_traverse_ptr(tp, funcname, __FILE__, __LINE__)
 #define do_load_tree(Vcb, th, r, t, td, loaded) _do_load_tree(Vcb, th, r, t, td, loaded, funcname, __FILE__, __LINE__)  
 
 // in search.c
@@ -474,43 +568,55 @@ NTSTATUS STDCALL do_write(device_extension* Vcb, LIST_ENTRY* rollback);
 NTSTATUS write_file(PDEVICE_OBJECT DeviceObject, PIRP Irp);
 NTSTATUS write_file2(device_extension* Vcb, PIRP Irp, LARGE_INTEGER offset, void* buf, ULONG* length, BOOL paging_io, BOOL no_cache, LIST_ENTRY* rollback);
 NTSTATUS truncate_file(fcb* fcb, UINT64 end, LIST_ENTRY* rollback);
-NTSTATUS extend_file(fcb* fcb, UINT64 end, LIST_ENTRY* rollback);
+NTSTATUS extend_file(fcb* fcb, file_ref* fileref, UINT64 end, BOOL prealloc, LIST_ENTRY* rollback);
+NTSTATUS excise_extents_inode(device_extension* Vcb, root* subvol, UINT64 inode, INODE_ITEM* ii, UINT64 start_data, UINT64 end_data, LIST_ENTRY* changed_sector_list, LIST_ENTRY* rollback);
 NTSTATUS excise_extents(device_extension* Vcb, fcb* fcb, UINT64 start_data, UINT64 end_data, LIST_ENTRY* changed_sector_list, LIST_ENTRY* rollback);
 void update_checksum_tree(device_extension* Vcb, LIST_ENTRY* changed_sector_list, LIST_ENTRY* rollback);
 NTSTATUS insert_sparse_extent(device_extension* Vcb, root* r, UINT64 inode, UINT64 start, UINT64 length, LIST_ENTRY* rollback);
-NTSTATUS STDCALL add_extent_ref(device_extension* Vcb, UINT64 address, UINT64 size, root* subvol, UINT64 inode, UINT64 offset, LIST_ENTRY* rollback);
-NTSTATUS STDCALL remove_extent_ref(device_extension* Vcb, UINT64 address, UINT64 size, root* subvol, UINT64 inode, UINT64 offset, LIST_ENTRY* changed_sector_list, LIST_ENTRY* rollback);
-void print_trees(LIST_ENTRY* tc);
 chunk* get_chunk_from_address(device_extension* Vcb, UINT64 address);
 void add_to_space_list(chunk* c, UINT64 offset, UINT64 size, UINT8 type);
 NTSTATUS consider_write(device_extension* Vcb);
+BOOL insert_extent_chunk_inode(device_extension* Vcb, root* subvol, UINT64 inode, INODE_ITEM* inode_item, chunk* c, UINT64 start_data,
+                               UINT64 length, BOOL prealloc, void* data, LIST_ENTRY* changed_sector_list, LIST_ENTRY* rollback);
+chunk* alloc_chunk(device_extension* Vcb, UINT64 flags, LIST_ENTRY* rollback);
+NTSTATUS STDCALL write_data(device_extension* Vcb, UINT64 address, void* data, UINT32 length);
+NTSTATUS write_tree(device_extension* Vcb, UINT64 addr, UINT8* data, write_tree_context* wtc);
+void free_write_tree_stripes(write_tree_context* wtc);
+NTSTATUS get_tree_new_address(device_extension* Vcb, tree* t, LIST_ENTRY* rollback);
 
 // in dirctrl.c
 NTSTATUS STDCALL drv_directory_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
+ULONG STDCALL get_reparse_tag(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type);
 
 // in security.c
 NTSTATUS STDCALL drv_query_security(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
 NTSTATUS STDCALL drv_set_security(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
-void fcb_get_sd(fcb* fcb);
+void fcb_get_sd(fcb* fcb, struct _fcb* parent);
 // UINT32 STDCALL get_uid();
 void add_user_mapping(WCHAR* sidstring, ULONG sidstringlength, UINT32 uid);
-NTSTATUS fcb_get_new_sd(fcb* fcb, ACCESS_STATE* as);
+UINT32 sid_to_uid(PSID sid);
+NTSTATUS fcb_get_new_sd(fcb* fcb, file_ref* fileref, 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, LIST_ENTRY* rollback);
+NTSTATUS delete_root_ref(device_extension* Vcb, UINT64 subvolid, UINT64 parsubvolid, UINT64 parinode, PANSI_STRING utf8, UINT64* index, LIST_ENTRY* rollback);
+NTSTATUS STDCALL update_root_backref(device_extension* Vcb, UINT64 subvolid, UINT64 parsubvolid, 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);
 
 // in reparse.c
-BOOL follow_symlink(fcb* fcb, PFILE_OBJECT FileObject);
 NTSTATUS get_reparse_point(PDEVICE_OBJECT DeviceObject, PFILE_OBJECT FileObject, void* buffer, DWORD buflen, DWORD* 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 get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* relatedfcb, BOOL parent);
 BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING filename, UINT32 crc32, root* r, UINT64 parinode, root** subvol,
                                          UINT64* inode, UINT8* type, PANSI_STRING utf8);
+NTSTATUS update_inode_item(device_extension* Vcb, root* subvol, UINT64 inode, INODE_ITEM* ii, LIST_ENTRY* rollback);
+NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnus, file_ref* related, BOOL parent, USHORT* unparsed);
 
 // in fsctl.c
 NTSTATUS fsctl_request(PDEVICE_OBJECT DeviceObject, PIRP Irp, UINT32 type, BOOL user);
@@ -522,13 +628,31 @@ void STDCALL flush_thread(void* context);
 NTSTATUS STDCALL drv_read(PDEVICE_OBJECT DeviceObject, PIRP Irp);
 NTSTATUS STDCALL read_file(device_extension* Vcb, root* subvol, UINT64 inode, UINT8* data, UINT64 start, UINT64 length, ULONG* pbr);
 
+// in pnp.c
+NTSTATUS STDCALL drv_pnp(PDEVICE_OBJECT DeviceObject, PIRP Irp);
+
+// in free-space.c
+NTSTATUS load_free_space_cache(device_extension* Vcb, chunk* c);
+NTSTATUS clear_free_space_cache(device_extension* Vcb);
+NTSTATUS allocate_cache(device_extension* Vcb, BOOL* changed, LIST_ENTRY* rollback);
+NTSTATUS update_chunk_caches(device_extension* Vcb, LIST_ENTRY* rollback);
+
+// in extent-tree.c
+NTSTATUS increase_extent_refcount_data(device_extension* Vcb, UINT64 address, UINT64 size, root* subvol, UINT64 inode, UINT64 offset, UINT32 refcount, LIST_ENTRY* rollback);
+NTSTATUS decrease_extent_refcount_data(device_extension* Vcb, UINT64 address, UINT64 size, root* subvol, UINT64 inode, UINT64 offset, UINT32 refcount, LIST_ENTRY* changed_sector_list, LIST_ENTRY* rollback);
+void decrease_chunk_usage(chunk* c, UINT64 delta);
+NTSTATUS convert_shared_data_extent(device_extension* Vcb, UINT64 address, UINT64 size, LIST_ENTRY* rollback);
+NTSTATUS convert_old_data_extent(device_extension* Vcb, UINT64 address, UINT64 size, LIST_ENTRY* rollback);
+
+#define fast_io_possible(fcb) (!FsRtlAreThereCurrentFileLocks(&fcb->lock) && !fcb->Vcb->readonly ? FastIoIsPossible : FastIoIsQuestionable)
+
 static __inline void print_open_trees(device_extension* Vcb) {
     LIST_ENTRY* le = Vcb->trees.Flink;
     while (le != &Vcb->trees) {
         tree* t = CONTAINING_RECORD(le, tree, list_entry);
         tree_data* td = CONTAINING_RECORD(t->itemlist.Flink, tree_data, list_entry);
-        ERR("tree %p: root %llx, level %u, refcount %u, first key (%llx,%x,%llx)\n",
-                      t, t->root->id, t->header.level, t->refcount, td->key.obj_id, td->key.obj_type, td->key.offset);
+        ERR("tree %p: root %llx, level %u, first key (%llx,%x,%llx)\n",
+                      t, t->root->id, t->header.level, td->key.obj_id, td->key.obj_type, td->key.offset);
 
         le = le->Flink;
     }
@@ -580,24 +704,6 @@ static __inline void InsertAfter(LIST_ENTRY* head, LIST_ENTRY* item, LIST_ENTRY*
     ExReleaseResourceLite(&Vcb->tree_lock); \
 }
 
-#ifdef DEBUG_TREE_REFCOUNTS
-#ifdef DEBUG_LONG_MESSAGES
-#define _increase_tree_rc(t, func, file, line) { \
-    LONG rc = InterlockedIncrement(&t->refcount); \
-    _debug_message(func, file, line, "tree %p: refcount increased to %i (increase_tree_rc)\n", t, rc); \
-}
-#else
-#define _increase_tree_rc(t, func, file, line) { \
-    LONG rc = InterlockedIncrement(&t->refcount); \
-    _debug_message(func, "tree %p: refcount increased to %i (increase_tree_rc)\n", t, rc); \
-}
-#endif
-#define increase_tree_rc(t) _increase_tree_rc(t, funcname, __FILE__, __LINE__)
-#else
-#define increase_tree_rc(t) InterlockedIncrement(&t->refcount);
-#define _increase_tree_rc(t, func, file, line) increase_tree_rc(t)
-#endif
-
 // from sys/stat.h
 #define __S_IFMT        0170000 /* These bits determine file type.  */
 #define __S_IFDIR       0040000 /* Directory.  */
@@ -613,6 +719,14 @@ static __inline void InsertAfter(LIST_ENTRY* head, LIST_ENTRY* item, LIST_ENTRY*
 #define S_ISDIR(mode)    __S_ISTYPE((mode), __S_IFDIR)
 #endif
 
+#ifndef S_IRUSR
+#define S_IRUSR 0000400
+#endif
+
+#ifndef S_IWUSR
+#define S_IWUSR 0000200
+#endif
+
 #ifndef S_IXUSR
 #define S_IXUSR 0000100
 #endif
@@ -622,10 +736,26 @@ static __inline void InsertAfter(LIST_ENTRY* head, LIST_ENTRY* item, LIST_ENTRY*
 #define S_IFREG __S_IFREG
 #endif /* __REACTOS__ */
 
+#ifndef S_IRGRP
+#define S_IRGRP (S_IRUSR >> 3)
+#endif
+
+#ifndef S_IWGRP
+#define S_IWGRP (S_IWUSR >> 3)
+#endif
+
 #ifndef S_IXGRP
 #define S_IXGRP (S_IXUSR >> 3)
 #endif
 
+#ifndef S_IROTH
+#define S_IROTH (S_IRGRP >> 3)
+#endif
+
+#ifndef S_IWOTH
+#define S_IWOTH (S_IWGRP >> 3)
+#endif
+
 #ifndef S_IXOTH
 #define S_IXOTH (S_IXGRP >> 3)
 #endif
@@ -641,6 +771,8 @@ NTSTATUS WINAPI RtlUTF8ToUnicodeN(WCHAR *uni_dest, ULONG uni_bytes_max,
 #if defined(__REACTOS__) && (NTDDI_VERSION < NTDDI_VISTA)
 NTSTATUS NTAPI FsRtlRemoveDotsFromPath(PWSTR OriginalString,
                                        USHORT PathLength, USHORT *NewLength);
+NTSTATUS NTAPI FsRtlValidateReparsePointBuffer(ULONG BufferLength,
+                                               PREPARSE_DATA_BUFFER ReparseBuffer);
 #endif /* defined(__REACTOS__) && (NTDDI_VERSION < NTDDI_WIN7) */
 
 #endif
index c1171a3..3071431 100644 (file)
@@ -2,6 +2,8 @@
 #define BTRFSIOCTL_H_DEFINED
 
 #define FSCTL_BTRFS_GET_FILE_IDS CTL_CODE(FILE_DEVICE_UNKNOWN, 0x829, METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
+#define FSCTL_BTRFS_CREATE_SUBVOL CTL_CODE(FILE_DEVICE_UNKNOWN, 0x82a, METHOD_IN_DIRECT, FILE_ANY_ACCESS)
+#define FSCTL_BTRFS_CREATE_SNAPSHOT CTL_CODE(FILE_DEVICE_UNKNOWN, 0x82b, METHOD_IN_DIRECT, FILE_ANY_ACCESS)
 
 typedef struct {
     UINT64 subvol;
@@ -9,4 +11,10 @@ typedef struct {
     BOOL top;
 } btrfs_get_file_ids;
 
+typedef struct {
+    HANDLE subvol;
+    UINT32 namelen;
+    WCHAR name[1];
+} btrfs_create_snapshot;
+
 #endif
index 3756bdf..5a3c4b7 100644 (file)
@@ -72,7 +72,6 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
                     
                     if (!utf16) {
                         ERR("out of memory\n");
-                        free_traverse_ptr(&tp);
                         return FALSE;
                     }
                     
@@ -86,17 +85,37 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
                         
                         if (FsRtlAreNamesEqual(filename, &us, TRUE, NULL)) {
                             if (di->key.obj_type == TYPE_ROOT_ITEM) {
-                                root* fcbroot = Vcb->roots;
-                                while (fcbroot && fcbroot->id != di->key.obj_id)
-                                    fcbroot = fcbroot->next;
+                                LIST_ENTRY* le = Vcb->roots.Flink;
                                 
-                                *subvol = fcbroot;
-                                *inode = SUBVOL_ROOT_INODE;
-                                *type = BTRFS_TYPE_DIRECTORY;
+                                if (subvol) {
+                                    *subvol = NULL;
+                                    
+                                    while (le != &Vcb->roots) {
+                                        root* r2 = CONTAINING_RECORD(le, root, list_entry);
+                                        
+                                        if (r2->id == di->key.obj_id) {
+                                            *subvol = r2;
+                                            break;
+                                        }
+                                        
+                                        le = le->Flink;
+                                    }
+                                }
+
+                                if (inode)
+                                    *inode = SUBVOL_ROOT_INODE;
+                                
+                                if (type)
+                                    *type = BTRFS_TYPE_DIRECTORY;
                             } else {
-                                *subvol = r;
-                                *inode = di->key.obj_id;
-                                *type = di->type;
+                                if (subvol)
+                                    *subvol = r;
+                                
+                                if (inode)
+                                    *inode = di->key.obj_id;
+                                
+                                if (type)
+                                    *type = di->type;
                             }
                             
                             if (utf8) {
@@ -105,7 +124,6 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
                                 utf8->Buffer = ExAllocatePoolWithTag(PagedPool, utf8->MaximumLength, ALLOC_TAG);
                                 if (!utf8->Buffer) {
                                     ERR("out of memory\n");
-                                    free_traverse_ptr(&tp);
                                     ExFreePool(utf16);
                                     return FALSE;
                                 }
@@ -113,10 +131,9 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
                                 RtlCopyMemory(utf8->Buffer, di->name, di->n);
                             }
                             
-                            free_traverse_ptr(&tp);
                             ExFreePool(utf16);
                             
-                            TRACE("found %.*S by hash at (%llx,%llx)\n", filename->Length / sizeof(WCHAR), filename->Buffer, (*subvol)->id, *inode);
+//                             TRACE("found %.*S by hash at (%llx,%llx)\n", filename->Length / sizeof(WCHAR), filename->Buffer, (*subvol)->id, *inode);
                             
                             return TRUE;
                         }
@@ -137,28 +154,23 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
     Status = find_item(Vcb, r, &tp2, &searchkey, FALSE);
     if (!NT_SUCCESS(Status)) {
         ERR("error - find_item returned %08x\n", Status);
-        free_traverse_ptr(&tp);
         return FALSE;
     }
     
-    free_traverse_ptr(&tp);
     tp = tp2;
     
     TRACE("found item %llx,%x,%llx\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
     
     if (keycmp(&tp.item->key, &searchkey) == -1) {
         if (find_next_item(Vcb, &tp, &next_tp, FALSE)) {
-            free_traverse_ptr(&tp);
             tp = next_tp;
             
             TRACE("moving on to %llx,%x,%llx\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
         }
     }
     
-    if (tp.item->key.obj_id != parinode || tp.item->key.obj_type != TYPE_DIR_INDEX) {
-        free_traverse_ptr(&tp);
+    if (tp.item->key.obj_id != parinode || tp.item->key.obj_type != TYPE_DIR_INDEX)
         return FALSE;
-    }
     
     b = TRUE;
     do {
@@ -179,8 +191,6 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
                 
                 if (!utf16) {
                     ERR("out of memory\n");
-                    
-                    free_traverse_ptr(&tp);
                     return FALSE;
                 }
                 
@@ -194,19 +204,39 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
                     
                     if (FsRtlAreNamesEqual(filename, &us, TRUE, NULL)) {
                         if (di->key.obj_type == TYPE_ROOT_ITEM) {
-                            root* fcbroot = Vcb->roots;
-                            while (fcbroot && fcbroot->id != di->key.obj_id)
-                                fcbroot = fcbroot->next;
+                            LIST_ENTRY* le = Vcb->roots.Flink;
+
+                            if (subvol) {
+                                *subvol = NULL;
+                                
+                                while (le != &Vcb->roots) {
+                                    root* r2 = CONTAINING_RECORD(le, root, list_entry);
+                                    
+                                    if (r2->id == di->key.obj_id) {
+                                        *subvol = r2;
+                                        break;
+                                    }
+                                    
+                                    le = le->Flink;
+                                }
+                            }
+                            
+                            if (inode)
+                                *inode = SUBVOL_ROOT_INODE;
                             
-                            *subvol = fcbroot;
-                            *inode = SUBVOL_ROOT_INODE;
-                            *type = BTRFS_TYPE_DIRECTORY;
+                            if (type)
+                                *type = BTRFS_TYPE_DIRECTORY;
                         } else {
-                            *subvol = r;
-                            *inode = di->key.obj_id;
-                            *type = di->type;
+                            if (subvol)
+                                *subvol = r;
+                            
+                            if (inode)
+                                *inode = di->key.obj_id;
+                            
+                            if (type)
+                                *type = di->type;
                         }
-                        TRACE("found %.*S at (%llx,%llx)\n", filename->Length / sizeof(WCHAR), filename->Buffer, (*subvol)->id, *inode);
+//                         TRACE("found %.*S at (%llx,%llx)\n", filename->Length / sizeof(WCHAR), filename->Buffer, (*subvol)->id, *inode);
                         
                         if (utf8) {
                             utf8->MaximumLength = di->n;
@@ -214,7 +244,6 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
                             utf8->Buffer = ExAllocatePoolWithTag(PagedPool, utf8->MaximumLength, ALLOC_TAG);
                             if (!utf8->Buffer) {
                                 ERR("out of memory\n");
-                                free_traverse_ptr(&tp);
                                 ExFreePool(utf16);
                                 
                                 return FALSE;
@@ -223,7 +252,6 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
                             RtlCopyMemory(utf8->Buffer, di->name, di->n);
                         }
                         
-                        free_traverse_ptr(&tp);
                         ExFreePool(utf16);
                         
                         return TRUE;
@@ -237,15 +265,12 @@ BOOL STDCALL find_file_in_dir_with_crc32(device_extension* Vcb, PUNICODE_STRING
         b = find_next_item(Vcb, &tp, &next_tp, FALSE);
          
         if (b) {
-            free_traverse_ptr(&tp);
             tp = next_tp;
             
             b = tp.item->key.obj_id == parinode && tp.item->key.obj_type == TYPE_DIR_INDEX;
         }
     } while (b);
     
-    free_traverse_ptr(&tp);
-
     return FALSE;
 }
 
@@ -290,11 +315,31 @@ fcb* create_fcb() {
     
     FsRtlInitializeFileLock(&fcb->lock, NULL, NULL);
     
-    InitializeListHead(&fcb->children);
-    
     return fcb;
 }
 
+file_ref* create_fileref() {
+    file_ref* fr;
+    
+    fr = ExAllocatePoolWithTag(PagedPool, sizeof(file_ref), ALLOC_TAG);
+    if (!fr) {
+        ERR("out of memory\n");
+        return NULL;
+    }
+    
+    RtlZeroMemory(fr, sizeof(file_ref));
+    
+    fr->refcount = 1;
+    
+#ifdef DEBUG_FCB_REFCOUNTS
+    WARN("fileref %p: refcount now %i\n", fr, fr->refcount);
+#endif
+    
+    InitializeListHead(&fr->children);
+    
+    return fr;
+}
+
 static BOOL STDCALL find_file_in_dir(device_extension* Vcb, PUNICODE_STRING filename, root* r,
                                      UINT64 parinode, root** subvol, UINT64* inode, UINT8* type, PANSI_STRING utf8) {
     char* fn;
@@ -410,7 +455,6 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
                     xattr->Buffer = ExAllocatePoolWithTag(PagedPool, di->n + 1, ALLOC_TAG);
                     if (!xattr->Buffer) {
                         ERR("out of memory\n");
-                        free_traverse_ptr(&tp);
                         goto end;
                     }
                     
@@ -418,8 +462,6 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
                     RtlCopyMemory(xattr->Buffer, di->name, di->n);
                     xattr->Buffer[di->n] = 0;
                     
-                    free_traverse_ptr(&tp);
-                    
                     success = TRUE;
                     goto end;
                 }
@@ -435,8 +477,6 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
         }
     }
     
-    free_traverse_ptr(&tp);
-    
     searchkey.offset = 0;
     
     Status = find_item(Vcb, fcb->subvol, &tp, &searchkey, FALSE);
@@ -473,7 +513,6 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
                         WCHAR* utf16 = ExAllocatePoolWithTag(PagedPool, utf16len, ALLOC_TAG);
                         if (!utf16) {
                             ERR("out of memory\n");
-                            free_traverse_ptr(&tp);
                             goto end;
                         }
                         
@@ -497,7 +536,6 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
                                 xattr->Buffer = ExAllocatePoolWithTag(PagedPool, di->n + 1, ALLOC_TAG);
                                 if (!xattr->Buffer) {
                                     ERR("out of memory\n");
-                                    free_traverse_ptr(&tp);
                                     ExFreePool(utf16);
                                     goto end;
                                 }
@@ -506,8 +544,6 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
                                 RtlCopyMemory(xattr->Buffer, di->name, di->n);
                                 xattr->Buffer[di->n] = 0;
                                 
-                                free_traverse_ptr(&tp);
-                                
                                 success = TRUE;
                                 goto end;
                             }
@@ -529,7 +565,6 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
         
         b = find_next_item(Vcb, &tp, &next_tp, FALSE);
         if (b) {
-            free_traverse_ptr(&tp);
             tp = next_tp;
             
             if (next_tp.item->key.obj_id > fcb->inode || next_tp.item->key.obj_type > TYPE_XATTR_ITEM)
@@ -537,8 +572,6 @@ static BOOL find_stream(device_extension* Vcb, fcb* fcb, PUNICODE_STRING stream,
         }
     } while (b);
     
-    free_traverse_ptr(&tp);
-   
 end:
     ExFreePool(utf8);
     
@@ -645,22 +678,38 @@ static NTSTATUS split_path(PUNICODE_STRING path, UNICODE_STRING** parts, ULONG*
     return STATUS_SUCCESS;
 }
 
-static fcb* search_fcb_children(fcb* dir, PUNICODE_STRING name) {
+// #ifdef DEBUG_FCB_REFCOUNTS
+// static void print_fcbs(device_extension* Vcb) {
+//     fcb* fcb = Vcb->fcbs;
+//     
+//     while (fcb) {
+//         ERR("fcb %p (%.*S): refcount %u\n", fcb, fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer, fcb->refcount);
+//         
+//         fcb = fcb->next;
+//     }
+// }
+// #endif
+
+static file_ref* search_fileref_children(file_ref* dir, PUNICODE_STRING name) {
     LIST_ENTRY* le;
-    fcb *c, *deleted = NULL;
+    file_ref *c, *deleted = NULL;
+#ifdef DEBUG_FCB_REFCOUNTS
     ULONG rc;
+#endif
     
     le = dir->children.Flink;
     while (le != &dir->children) {
-        c = CONTAINING_RECORD(le, fcb, list_entry);
+        c = CONTAINING_RECORD(le, file_ref, list_entry);
         
         if (c->refcount > 0 && FsRtlAreNamesEqual(&c->filepart, name, TRUE, NULL)) {
             if (c->deleted) {
                 deleted = c;
             } else {
-                rc = InterlockedIncrement(&c->refcount);
 #ifdef DEBUG_FCB_REFCOUNTS
-                WARN("fcb %p: refcount now %i (%.*S)\n", c, rc, c->full_filename.Length / sizeof(WCHAR), c->full_filename.Buffer);
+                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;
             }
@@ -670,50 +719,226 @@ static fcb* search_fcb_children(fcb* dir, PUNICODE_STRING name) {
     }
     
     if (deleted) {
-        rc = InterlockedIncrement(&deleted->refcount);
 #ifdef DEBUG_FCB_REFCOUNTS
-        WARN("fcb %p: refcount now %i (%.*S)\n", deleted, rc, deleted->full_filename.Length / sizeof(WCHAR), deleted->full_filename.Buffer);
+        rc = InterlockedIncrement(&deleted->refcount);
+        WARN("fileref %p: refcount now %i (%S)\n", deleted, rc, file_desc_fileref(deleted));
+#else
+        InterlockedIncrement(&deleted->refcount);
 #endif
     }
     
     return deleted;
 }
 
-// #ifdef DEBUG_FCB_REFCOUNTS
-// static void print_fcbs(device_extension* Vcb) {
-//     fcb* fcb = Vcb->fcbs;
-//     
-//     while (fcb) {
-//         ERR("fcb %p (%.*S): refcount %u\n", fcb, fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer, fcb->refcount);
-//         
-//         fcb = fcb->next;
-//     }
-// }
-// #endif
+static NTSTATUS open_fcb(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type, PANSI_STRING utf8, fcb* parent, fcb** pfcb) {
+    KEY searchkey;
+    traverse_ptr tp;
+    NTSTATUS Status;
+    fcb* fcb;
+    
+    if (!IsListEmpty(&subvol->fcbs)) {
+        LIST_ENTRY* le = subvol->fcbs.Flink;
+                                
+        while (le != &subvol->fcbs) {
+            fcb = CONTAINING_RECORD(le, struct _fcb, list_entry);
+            
+            if (fcb->inode == inode && !fcb->ads) {
+#ifdef DEBUG_FCB_REFCOUNTS
+                LONG rc = InterlockedIncrement(&fcb->refcount);
+                
+                WARN("fcb %p: refcount now %i (subvol %llx, inode %llx)\n", fcb, rc, fcb->subvol->id, fcb->inode);
+#else
+                InterlockedIncrement(&fcb->refcount);
+#endif
 
-NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* relatedfcb, BOOL parent) {
-    fcb *dir, *sf, *sf2;
-    ULONG i, num_parts;
+                *pfcb = fcb;
+                return STATUS_SUCCESS;
+            }
+            
+            le = le->Flink;
+        }
+    }
+    
+    fcb = create_fcb();
+    if (!fcb) {
+        ERR("out of memory\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    fcb->Vcb = Vcb;
+    
+    fcb->subvol = subvol;
+    fcb->inode = inode;
+    fcb->type = type;
+    
+    searchkey.obj_id = inode;
+    searchkey.obj_type = TYPE_INODE_ITEM;
+    searchkey.offset = 0xffffffffffffffff;
+    
+    Status = find_item(Vcb, subvol, &tp, &searchkey, FALSE);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        free_fcb(fcb);
+        return Status;
+    }
+    
+    if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
+        ERR("couldn't find INODE_ITEM for inode %llx in subvol %llx\n", inode, subvol->id);
+        free_fcb(fcb);
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    if (tp.item->size > 0)
+        RtlCopyMemory(&fcb->inode_item, tp.item->data, min(sizeof(INODE_ITEM), tp.item->size));
+    
+    fcb->atts = get_file_attributes(Vcb, &fcb->inode_item, fcb->subvol, fcb->inode, fcb->type, utf8->Buffer[0] == '.', FALSE);
+    
+    fcb_get_sd(fcb, parent);
+    
+    InsertTailList(&subvol->fcbs, &fcb->list_entry);
+    
+    fcb->Header.IsFastIoPossible = fast_io_possible(fcb);
+    
+    if (fcb->inode_item.st_size == 0 || (fcb->type != BTRFS_TYPE_FILE && fcb->type != BTRFS_TYPE_SYMLINK)) {
+        fcb->Header.AllocationSize.QuadPart = 0;
+        fcb->Header.FileSize.QuadPart = 0;
+        fcb->Header.ValidDataLength.QuadPart = 0;
+    } else {
+        EXTENT_DATA* ed;
+        
+        searchkey.obj_id = fcb->inode;
+        searchkey.obj_type = TYPE_EXTENT_DATA;
+        searchkey.offset = 0xffffffffffffffff;
+        
+        Status = find_item(fcb->Vcb, fcb->subvol, &tp, &searchkey, FALSE);
+        if (!NT_SUCCESS(Status)) {
+            ERR("error - find_item returned %08x\n", Status);
+            free_fcb(fcb);
+            return Status;
+        }
+        
+        if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
+            ERR("error - could not find EXTENT_DATA items for inode %llx in subvol %llx\n", fcb->inode, fcb->subvol->id);
+            free_fcb(fcb);
+            return STATUS_INTERNAL_ERROR;
+        }
+        
+        if (tp.item->size < sizeof(EXTENT_DATA)) {
+            ERR("(%llx,%x,%llx) was %llx bytes, expected at least %llx\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset,
+                tp.item->size, sizeof(EXTENT_DATA));
+            
+            free_fcb(fcb);
+            return STATUS_INTERNAL_ERROR;
+        }
+        
+        ed = (EXTENT_DATA*)tp.item->data;
+        
+        if (ed->type == EXTENT_TYPE_INLINE)
+            fcb->Header.AllocationSize.QuadPart = fcb->inode_item.st_size;
+        else
+            fcb->Header.AllocationSize.QuadPart = sector_align(fcb->inode_item.st_size, fcb->Vcb->superblock.sector_size);
+        
+        fcb->Header.FileSize.QuadPart = fcb->inode_item.st_size;
+        fcb->Header.ValidDataLength.QuadPart = fcb->inode_item.st_size;
+    }
+    
+    *pfcb = fcb;
+    return STATUS_SUCCESS;
+}
+
+static NTSTATUS open_fcb_stream(device_extension* Vcb, root* subvol, UINT64 inode, ANSI_STRING* xattr,
+                                UINT32 streamsize, UINT32 streamhash, fcb* parent, fcb** pfcb) {
+    fcb* fcb;
+    
+    if (!IsListEmpty(&subvol->fcbs)) {
+        LIST_ENTRY* le = subvol->fcbs.Flink;
+                                
+        while (le != &subvol->fcbs) {
+            fcb = CONTAINING_RECORD(le, struct _fcb, list_entry);
+            
+            if (fcb->inode == inode && fcb->ads && fcb->adsxattr.Length == xattr->Length &&
+                RtlCompareMemory(fcb->adsxattr.Buffer, xattr->Buffer, fcb->adsxattr.Length) == fcb->adsxattr.Length) {
+#ifdef DEBUG_FCB_REFCOUNTS
+                LONG rc = InterlockedIncrement(&fcb->refcount);
+                
+                WARN("fcb %p: refcount now %i (subvol %llx, inode %llx)\n", fcb, rc, fcb->subvol->id, fcb->inode);
+#else
+                InterlockedIncrement(&fcb->refcount);
+#endif
+
+                *pfcb = fcb;
+                return STATUS_SUCCESS;
+            }
+                            
+            le = le->Flink;
+        }
+    }
+    
+    fcb = create_fcb();
+    if (!fcb) {
+        ERR("out of memory\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    fcb->Vcb = Vcb;
+    
+    fcb->subvol = parent->subvol;
+    fcb->inode = parent->inode;
+    fcb->type = parent->type;
+    fcb->ads = TRUE;
+    fcb->adssize = streamsize;
+    fcb->adshash = streamhash;
+    fcb->adsxattr = *xattr;
+    
+    fcb->Header.IsFastIoPossible = fast_io_possible(fcb);
+    fcb->Header.AllocationSize.QuadPart = fcb->adssize;
+    fcb->Header.FileSize.QuadPart = fcb->adssize;
+    fcb->Header.ValidDataLength.QuadPart = fcb->adssize;
+    
+    TRACE("stream found: size = %x, hash = %08x\n", fcb->adssize, fcb->adshash);
+    
+    InsertTailList(&fcb->subvol->fcbs, &fcb->list_entry);
+    
+    *pfcb = fcb;
+    
+    return STATUS_SUCCESS;
+}
+
+NTSTATUS open_fileref(device_extension* Vcb, file_ref** pfr, PUNICODE_STRING fnus, file_ref* related, BOOL parent, USHORT* unparsed) {
     UNICODE_STRING fnus2;
+    file_ref *dir, *sf, *sf2;
+    ULONG i, num_parts;
     UNICODE_STRING* parts = NULL;
     BOOL has_stream;
     NTSTATUS Status;
     
-    TRACE("(%p, %p, %.*S, %p, %s)\n", Vcb, pfcb, fnus->Length / sizeof(WCHAR), fnus->Buffer, relatedfcb, parent ? "TRUE" : "FALSE");
+    TRACE("(%p, %p, %p, %u, %p)\n", Vcb, pfr, related, parent, unparsed);
     
-// #ifdef DEBUG_FCB_REFCOUNTS
-//     print_fcbs(Vcb);
-// #endif
+    if (Vcb->removing)
+        return STATUS_ACCESS_DENIED;
     
     fnus2 = *fnus;
     
-    if (fnus2.Length < sizeof(WCHAR) && !relatedfcb) {
+    if (fnus2.Length < sizeof(WCHAR) && !related) {
         ERR("error - fnus was too short\n");
         return STATUS_INTERNAL_ERROR;
     }
     
-    if (relatedfcb) {
-        dir = relatedfcb;
+    if (related && fnus->Length == 0) {
+#ifdef DEBUG_FCB_REFCOUNTS
+        LONG rc = InterlockedIncrement(&related->refcount);
+        WARN("fileref %p: refcount now %i\n", related, rc);
+#else
+        InterlockedIncrement(&related->refcount);
+#endif
+        
+        
+        *pfr = related;
+        return STATUS_SUCCESS;
+    }
+    
+    if (related) {
+        dir = related;
     } else {
         if (fnus2.Buffer[0] != '\\') {
             ERR("error - filename %.*S did not begin with \\\n", fnus2.Length / sizeof(WCHAR), fnus2.Buffer);
@@ -721,26 +946,26 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
         }
         
         if (fnus2.Length == sizeof(WCHAR)) {
-            LONG rc;
-            
-            *pfcb = Vcb->root_fcb;
-            rc = InterlockedIncrement(&Vcb->root_fcb->refcount);
 #ifdef DEBUG_FCB_REFCOUNTS
-            WARN("fcb %p: refcount now %i (root)\n", Vcb->root_fcb, rc);
+            LONG rc = InterlockedIncrement(&Vcb->root_fileref->refcount);
+            WARN("fileref %p: refcount now %i (root)\n", Vcb->root_fileref, rc);
+#else
+            InterlockedIncrement(&Vcb->root_fileref->refcount);
 #endif
+            *pfr = Vcb->root_fileref;
             return STATUS_SUCCESS;
         }
         
-        dir = Vcb->root_fcb;
+        dir = Vcb->root_fileref;
         
         fnus2.Buffer++;
         fnus2.Length -= sizeof(WCHAR);
         fnus2.MaximumLength -= sizeof(WCHAR);
     }
     
-    if (dir->type != BTRFS_TYPE_DIRECTORY && (fnus->Length < sizeof(WCHAR) || fnus->Buffer[0] != ':')) {
-        WARN("passed relatedfcb which isn't a directory (%.*S) (fnus = %.*S)\n",
-             relatedfcb->full_filename.Length / sizeof(WCHAR), relatedfcb->full_filename.Buffer, fnus->Length / sizeof(WCHAR), fnus->Buffer);
+    if (dir->fcb->type != BTRFS_TYPE_DIRECTORY && (fnus->Length < sizeof(WCHAR) || fnus->Buffer[0] != ':')) {
+        WARN("passed related fileref which isn't a directory (%S) (fnus = %.*S)\n",
+             file_desc_fileref(related), fnus->Length / sizeof(WCHAR), fnus->Buffer);
         return STATUS_OBJECT_PATH_NOT_FOUND;
     }
     
@@ -754,17 +979,16 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
         }
     }
     
-    // FIXME - handle refcounts(?)
     sf = dir;
     dir->refcount++;
 #ifdef DEBUG_FCB_REFCOUNTS
-    WARN("fcb %p: refcount now %i (%.*S)\n", dir, dir->refcount, dir->full_filename.Length / sizeof(WCHAR), dir->full_filename.Buffer);
+    WARN("fileref %p: refcount now %i (%S)\n", dir, dir->refcount, file_desc_fileref(dir));
 #endif
     
     if (parent) {
         num_parts--;
         
-        if (has_stream) {
+        if (has_stream && num_parts > 0) {
             num_parts--;
             has_stream = FALSE;
         }
@@ -772,17 +996,18 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
     
     if (num_parts == 0) {
         Status = STATUS_SUCCESS;
-        *pfcb = dir;
+        *pfr = dir;
         goto end2;
     }
     
     for (i = 0; i < num_parts; i++) {
         BOOL lastpart = (i == num_parts-1) || (i == num_parts-2 && has_stream);
         
-        sf2 = search_fcb_children(sf, &parts[i]);
+        sf2 = search_fileref_children(sf, &parts[i]);
         
-        if (sf2 && sf2->type != BTRFS_TYPE_DIRECTORY && !lastpart) {
+        if (sf2 && sf2->fcb->type != BTRFS_TYPE_DIRECTORY && !lastpart) {
             WARN("passed path including file as subdirectory\n");
+            free_fileref(sf2);
             
             Status = STATUS_OBJECT_PATH_NOT_FOUND;
             goto end;
@@ -799,13 +1024,19 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
                 xattr.Buffer = NULL;
                 xattr.Length = xattr.MaximumLength = 0;
                 
-                if (!find_stream(Vcb, sf, &parts[i], &streamname, &streamsize, &streamhash, &xattr)) {
+                // FIXME - check if already opened
+                
+                if (!find_stream(Vcb, sf->fcb, &parts[i], &streamname, &streamsize, &streamhash, &xattr)) {
                     TRACE("could not find stream %.*S\n", parts[i].Length / sizeof(WCHAR), parts[i].Buffer);
                     
                     Status = STATUS_OBJECT_NAME_NOT_FOUND;
                     goto end;
                 } else {
                     ULONG fnlen;
+                    fcb* fcb;
+#ifdef DEBUG_FCB_REFCOUNTS
+                    LONG rc;
+#endif
                     
                     if (streamhash == EA_DOSATTRIB_HASH && xattr.Length == strlen(EA_DOSATTRIB) &&
                         RtlCompareMemory(xattr.Buffer, EA_DOSATTRIB, xattr.Length) == xattr.Length) {
@@ -815,14 +1046,21 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
                         goto end;
                     }
                     
-                    sf2 = create_fcb();
+                    Status = open_fcb_stream(Vcb, sf->fcb->subvol, sf->fcb->inode, &xattr, streamsize, streamhash, sf->fcb, &fcb);
+                    if (!NT_SUCCESS(Status)) {
+                        ERR("open_fcb_stream returned %08x\n", Status);
+                        goto end;
+                    }
+                    
+                    sf2 = create_fileref();
                     if (!sf2) {
                         ERR("out of memory\n");
+                        free_fcb(fcb);
                         Status = STATUS_INSUFFICIENT_RESOURCES;
                         goto end;
                     }
-        
-                    sf2->Vcb = Vcb;
+                    
+                    sf2->fcb = fcb;
         
                     if (streamname.Buffer) // case has changed
                         sf2->filepart = streamname;
@@ -831,7 +1069,7 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
                         sf2->filepart.Buffer = ExAllocatePoolWithTag(PagedPool, sf2->filepart.MaximumLength, ALLOC_TAG);
                         if (!sf2->filepart.Buffer) {
                             ERR("out of memory\n");
-                            free_fcb(sf2);
+                            free_fileref(sf2);
                             Status = STATUS_INSUFFICIENT_RESOURCES;
                             goto end;
                         }   
@@ -839,32 +1077,9 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
                         RtlCopyMemory(sf2->filepart.Buffer, parts[i].Buffer, parts[i].Length);
                     }
                     
-                    sf2->par = sf;
-                    
-                    sf->refcount++;
-#ifdef DEBUG_FCB_REFCOUNTS
-                    WARN("fcb %p: refcount now %i (%.*S)\n", sf, sf->refcount, sf->full_filename.Length / sizeof(WCHAR), sf->full_filename.Buffer);
-#endif
-                    
-                    sf2->subvol = sf->subvol;
-                    sf2->inode = sf->inode;
-                    sf2->type = sf->type;
-                    sf2->ads = TRUE;
-                    sf2->adssize = streamsize;
-                    sf2->adshash = streamhash;
-                    sf2->adsxattr = xattr;
-                    
-                    TRACE("stream found: size = %x, hash = %08x\n", sf2->adssize, sf2->adshash);
-                    
-                    if (Vcb->fcbs)
-                        Vcb->fcbs->prev = sf2;
-                    
-                    sf2->next = Vcb->fcbs;
-                    Vcb->fcbs = sf2;
-                    
                     sf2->name_offset = sf->full_filename.Length / sizeof(WCHAR);
                     
-                    if (sf != Vcb->root_fcb)
+                    if (sf != Vcb->root_fileref)
                         sf2->name_offset++;
                     
                     fnlen = (sf2->name_offset * sizeof(WCHAR)) + sf2->filepart.Length;
@@ -872,7 +1087,7 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
                     sf2->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, fnlen, ALLOC_TAG);
                     if (!sf2->full_filename.Buffer) {
                         ERR("out of memory\n");
-                        free_fcb(sf2);
+                        free_fileref(sf2);
                         Status = STATUS_INSUFFICIENT_RESOURCES;
                         goto end;
                     }
@@ -886,84 +1101,85 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
                     
                     // FIXME - make sure all functions know that ADS FCBs won't have a valid SD or INODE_ITEM
 
-                    TRACE("found stream %.*S (subvol = %p)\n", sf2->full_filename.Length / sizeof(WCHAR), sf2->full_filename.Buffer, sf->subvol);
-                    
+                    sf2->parent = (struct _file_ref*)sf;
                     InsertTailList(&sf->children, &sf2->list_entry);
+                    
+#ifdef DEBUG_FCB_REFCOUNTS
+                    rc = InterlockedIncrement(&sf->refcount);
+                    WARN("fileref %p: refcount now %i\n", sf, rc);
+#else
+                    InterlockedIncrement(&sf->refcount);
+#endif
                 }
             } else {
                 root* subvol;
                 UINT64 inode;
                 UINT8 type;
                 ANSI_STRING utf8;
-                KEY searchkey;
-                traverse_ptr tp;
+#ifdef DEBUG_FCB_REFCOUNTS
+                LONG rc;
+#endif
                 
-                if (!find_file_in_dir(Vcb, &parts[i], sf->subvol, sf->inode, &subvol, &inode, &type, &utf8)) {
+                if (!find_file_in_dir(Vcb, &parts[i], sf->fcb->subvol, sf->fcb->inode, &subvol, &inode, &type, &utf8)) {
                     TRACE("could not find %.*S\n", parts[i].Length / sizeof(WCHAR), parts[i].Buffer);
 
                     Status = lastpart ? STATUS_OBJECT_NAME_NOT_FOUND : STATUS_OBJECT_PATH_NOT_FOUND;
                     goto end;
-                } else if (type != BTRFS_TYPE_DIRECTORY && !lastpart) {
-                    WARN("passed path including file as subdirectory\n");
-                    
-                    Status = STATUS_OBJECT_PATH_NOT_FOUND;
-                    goto end;
                 } else {
-                    ULONG fnlen, strlen;
+                    fcb* fcb;
+                    ULONG strlen, fnlen;
                     
-                    sf2 = create_fcb();
+                    if (type != BTRFS_TYPE_DIRECTORY && !lastpart) {
+                        WARN("passed path including file as subdirectory\n");
+                        
+                        Status = STATUS_OBJECT_PATH_NOT_FOUND;
+                        goto end;
+                    }
+                    
+                    Status = open_fcb(Vcb, subvol, inode, type, &utf8, sf->fcb, &fcb);
+                    if (!NT_SUCCESS(Status)) {
+                        ERR("open_fcb returned %08x\n", Status);
+                        goto end;
+                    }
+                    
+                    sf2 = create_fileref();
                     if (!sf2) {
                         ERR("out of memory\n");
+                        free_fcb(fcb);
                         Status = STATUS_INSUFFICIENT_RESOURCES;
                         goto end;
                     }
                     
-                    sf2->Vcb = Vcb;
-
+                    sf2->fcb = fcb;
+                    
+                    sf2->utf8 = utf8;
+                    
                     Status = RtlUTF8ToUnicodeN(NULL, 0, &strlen, utf8.Buffer, utf8.Length);
                     if (!NT_SUCCESS(Status)) {
                         ERR("RtlUTF8ToUnicodeN 1 returned %08x\n", Status);
-                        free_fcb(sf2);
+                        free_fileref(sf2);
                         goto end;
-                    } else {
-                        sf2->filepart.MaximumLength = sf2->filepart.Length = strlen;
-                        sf2->filepart.Buffer = ExAllocatePoolWithTag(PagedPool, sf2->filepart.MaximumLength, ALLOC_TAG);
-                        if (!sf2->filepart.Buffer) {
-                            ERR("out of memory\n");
-                            free_fcb(sf2);
-                            Status = STATUS_INSUFFICIENT_RESOURCES;
-                            goto end;
-                        }
-                        
-                        Status = RtlUTF8ToUnicodeN(sf2->filepart.Buffer, strlen, &strlen, utf8.Buffer, utf8.Length);
-                        
-                        if (!NT_SUCCESS(Status)) {
-                            ERR("RtlUTF8ToUnicodeN 2 returned %08x\n", Status);
-                            free_fcb(sf2);
-                            goto end;
-                        }
                     }
                     
-                    sf2->par = sf;
-                    
-                    sf->refcount++;
-#ifdef DEBUG_FCB_REFCOUNTS
-                    WARN("fcb %p: refcount now %i (%.*S)\n", sf, sf->refcount, sf->full_filename.Length / sizeof(WCHAR), sf->full_filename.Buffer);
-#endif
-                    
-                    sf2->subvol = subvol;
-                    sf2->inode = inode;
-                    sf2->type = type;
-                    
-                    if (Vcb->fcbs)
-                        Vcb->fcbs->prev = sf2;
+                    sf2->filepart.MaximumLength = sf2->filepart.Length = strlen;
+                    sf2->filepart.Buffer = ExAllocatePoolWithTag(PagedPool, sf2->filepart.MaximumLength, ALLOC_TAG);
+                    if (!sf2->filepart.Buffer) {
+                        ERR("out of memory\n");
+                        free_fileref(sf2);
+                        Status = STATUS_INSUFFICIENT_RESOURCES;
+                        goto end;
+                    }
                     
-                    sf2->next = Vcb->fcbs;
-                    Vcb->fcbs = sf2;
+                    Status = RtlUTF8ToUnicodeN(sf2->filepart.Buffer, strlen, &strlen, utf8.Buffer, utf8.Length);
+                    if (!NT_SUCCESS(Status)) {
+                        ERR("RtlUTF8ToUnicodeN 2 returned %08x\n", Status);
+                        free_fileref(sf2);
+                        goto end;
+                    }
                     
                     sf2->name_offset = sf->full_filename.Length / sizeof(WCHAR);
-                    
-                    if (sf != Vcb->root_fcb)
+    
+                    if (sf != Vcb->root_fileref)
                         sf2->name_offset++;
                     
                     fnlen = (sf2->name_offset * sizeof(WCHAR)) + sf2->filepart.Length;
@@ -971,7 +1187,7 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
                     sf2->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, fnlen, ALLOC_TAG);
                     if (!sf2->full_filename.Buffer) {
                         ERR("out of memory\n");
-                        free_fcb(sf2);
+                        free_fileref(sf2);
                         Status = STATUS_INSUFFICIENT_RESOURCES;
                         goto end;
                     }
@@ -979,44 +1195,19 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
                     sf2->full_filename.Length = sf2->full_filename.MaximumLength = fnlen;
                     RtlCopyMemory(sf2->full_filename.Buffer, sf->full_filename.Buffer, sf->full_filename.Length);
                     
-                    if (sf != Vcb->root_fcb)
+                    if (sf != Vcb->root_fileref)
                         sf2->full_filename.Buffer[sf->full_filename.Length / sizeof(WCHAR)] = '\\';
                     
                     RtlCopyMemory(&sf2->full_filename.Buffer[sf2->name_offset], sf2->filepart.Buffer, sf2->filepart.Length);
                     
-                    sf2->utf8 = utf8;
-                    
-                    searchkey.obj_id = sf2->inode;
-                    searchkey.obj_type = TYPE_INODE_ITEM;
-                    searchkey.offset = 0xffffffffffffffff;
-                    
-                    Status = find_item(sf2->Vcb, sf2->subvol, &tp, &searchkey, FALSE);
-                    if (!NT_SUCCESS(Status)) {
-                        ERR("error - find_item returned %08x\n", Status);
-                        free_fcb(sf2);
-                        goto end;
-                    }
-                    
-                    if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
-                        ERR("couldn't find INODE_ITEM for inode %llx in subvol %llx\n", sf2->inode, sf2->subvol->id);
-                        Status = STATUS_INTERNAL_ERROR;
-                        free_fcb(sf2);
-                        free_traverse_ptr(&tp);
-                        goto end;
-                    }
-                    
-                    if (tp.item->size > 0)
-                        RtlCopyMemory(&sf2->inode_item, tp.item->data, min(sizeof(INODE_ITEM), tp.item->size));
-                    
-                    free_traverse_ptr(&tp);
-                    
-                    sf2->atts = get_file_attributes(Vcb, &sf2->inode_item, sf2->subvol, sf2->inode, sf2->type, sf2->filepart.Buffer[0] == '.', FALSE);
-                    
-                    fcb_get_sd(sf2);
-                    
-                    TRACE("found %.*S (subvol = %p)\n", sf2->full_filename.Length / sizeof(WCHAR), sf2->full_filename.Buffer, subvol);
-                    
+                    sf2->parent = (struct _file_ref*)sf;
                     InsertTailList(&sf->children, &sf2->list_entry);
+#ifdef DEBUG_FCB_REFCOUNTS
+                    rc = InterlockedIncrement(&sf->refcount);
+                    WARN("fileref %p: refcount now %i\n", sf, rc);
+#else
+                    InterlockedIncrement(&sf->refcount);
+#endif
                 }
             }
         }
@@ -1024,39 +1215,36 @@ NTSTATUS get_fcb(device_extension* Vcb, fcb** pfcb, PUNICODE_STRING fnus, fcb* r
         if (i == num_parts - 1)
             break;
         
-        free_fcb(sf);
+        if (sf2->fcb->atts & FILE_ATTRIBUTE_REPARSE_POINT) {
+            Status = STATUS_REPARSE;
+            
+            if (unparsed)
+                *unparsed = fnus->Length - ((parts[i+1].Buffer - fnus->Buffer - 1) * sizeof(WCHAR));
+            
+            break;
+        }
+        
+        free_fileref(sf);
         sf = sf2;
     }
     
-    Status = STATUS_SUCCESS;
-    *pfcb = sf2;
+    if (Status != STATUS_REPARSE)
+        Status = STATUS_SUCCESS;
+    *pfr = sf2;
     
 end:
-    free_fcb(sf);
+    free_fileref(sf);
     
 end2:
     if (parts)
         ExFreePool(parts);
     
-// #ifdef DEBUG_FCB_REFCOUNTS
-//     print_fcbs(Vcb);
-// #endif
-    
     TRACE("returning %08x\n", Status);
     
     return Status;
 }
 
-static NTSTATUS STDCALL attach_fcb_to_fileobject(device_extension* Vcb, fcb* fcb, PFILE_OBJECT FileObject) {
-    FileObject->FsContext = fcb;
-//     FileObject->FsContext2 = 0x0badc0de;//NULL;
-    
-    // FIXME - cache stuff
-    
-    return STATUS_SUCCESS;
-}
-
-static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_STRING fpus, fcb* parfcb, ULONG options, fcb** pfcb, LIST_ENTRY* rollback) {
+static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_STRING fpus, file_ref* parfileref, ULONG options, file_ref** pfr, LIST_ENTRY* rollback) {
     NTSTATUS Status;
     fcb* fcb;
     ULONG utf8len;
@@ -1074,7 +1262,10 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
     ANSI_STRING utf8as;
     ULONG defda;
+    file_ref* fileref;
+#ifdef DEBUG_FCB_REFCOUNTS
     LONG rc;
+#endif
     
     Status = RtlUnicodeToUTF8N(NULL, 0, &utf8len, fpus->Buffer, fpus->Length);
     if (!NT_SUCCESS(Status))
@@ -1096,7 +1287,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     
     crc32 = calc_crc32c(0xfffffffe, (UINT8*)utf8, utf8len);
     
-    dirpos = find_next_dir_index(Vcb, parfcb->subvol, parfcb->inode);
+    dirpos = find_next_dir_index(Vcb, parfileref->fcb->subvol, parfileref->fcb->inode);
     if (dirpos == 0) {
         Status = STATUS_INTERNAL_ERROR;
         ExFreePool(utf8);
@@ -1108,28 +1299,27 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     KeQuerySystemTime(&time);
     win_time_to_unix(time, &now);
     
-    TRACE("parfcb->inode_item.st_size was %llx\n", parfcb->inode_item.st_size);
-    parfcb->inode_item.st_size += utf8len * 2;
-    TRACE("parfcb->inode_item.st_size was %llx\n", parfcb->inode_item.st_size);
-    parfcb->inode_item.transid = Vcb->superblock.generation;
-    parfcb->inode_item.sequence++;
-    parfcb->inode_item.st_ctime = now;
-    parfcb->inode_item.st_mtime = now;
+//     TRACE("parfcb->inode_item.st_size was %llx\n", parfcb->inode_item.st_size);
+    parfileref->fcb->inode_item.st_size += utf8len * 2;
+//     TRACE("parfcb->inode_item.st_size was %llx\n", parfcb->inode_item.st_size);
+    parfileref->fcb->inode_item.transid = Vcb->superblock.generation;
+    parfileref->fcb->inode_item.sequence++;
+    parfileref->fcb->inode_item.st_ctime = now;
+    parfileref->fcb->inode_item.st_mtime = now;
     
-    searchkey.obj_id = parfcb->inode;
+    searchkey.obj_id = parfileref->fcb->inode;
     searchkey.obj_type = TYPE_INODE_ITEM;
-    searchkey.offset = 0;
+    searchkey.offset = 0xffffffffffffffff;
     
-    Status = find_item(Vcb, parfcb->subvol, &tp, &searchkey, FALSE);
+    Status = find_item(Vcb, parfileref->fcb->subvol, &tp, &searchkey, FALSE);
     if (!NT_SUCCESS(Status)) {
         ERR("error - find_item returned %08x\n", Status);
         ExFreePool(utf8);
         return Status;
     }
     
-    if (keycmp(&searchkey, &tp.item->key)) {
-        ERR("error - could not find INODE_ITEM for parent directory %llx in subvol %llx\n", parfcb->inode, parfcb->subvol->id);
-        free_traverse_ptr(&tp);
+    if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
+        ERR("error - could not find INODE_ITEM for parent directory %llx in subvol %llx\n", parfileref->fcb->inode, parfileref->fcb->subvol->id);
         ExFreePool(utf8);
         return STATUS_INTERNAL_ERROR;
     }
@@ -1137,22 +1327,19 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     dirii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
     if (!dirii) {
         ERR("out of memory\n");
-        free_traverse_ptr(&tp);
         ExFreePool(utf8);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
-    RtlCopyMemory(dirii, &parfcb->inode_item, sizeof(INODE_ITEM));
+    RtlCopyMemory(dirii, &parfileref->fcb->inode_item, sizeof(INODE_ITEM));
     delete_tree_item(Vcb, &tp, rollback);
     
-    insert_tree_item(Vcb, parfcb->subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, dirii, sizeof(INODE_ITEM), NULL, rollback);
+    insert_tree_item(Vcb, parfileref->fcb->subvol, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, dirii, sizeof(INODE_ITEM), NULL, rollback);
     
-    free_traverse_ptr(&tp);
+    if (parfileref->fcb->subvol->lastinode == 0)
+        get_last_inode(Vcb, parfileref->fcb->subvol);
     
-    if (parfcb->subvol->lastinode == 0)
-        get_last_inode(Vcb, parfcb->subvol);
-    
-    inode = parfcb->subvol->lastinode + 1;
+    inode = parfileref->fcb->subvol->lastinode + 1;
     
     type = options & FILE_DIRECTORY_FILE ? BTRFS_TYPE_DIRECTORY : BTRFS_TYPE_FILE;
     
@@ -1173,7 +1360,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     di->type = type;
     RtlCopyMemory(di->name, utf8, utf8len);
     
-    insert_tree_item(Vcb, parfcb->subvol, parfcb->inode, TYPE_DIR_INDEX, dirpos, di, disize, NULL, rollback);
+    insert_tree_item(Vcb, parfileref->fcb->subvol, parfileref->fcb->inode, TYPE_DIR_INDEX, dirpos, di, disize, NULL, rollback);
     
     di2 = ExAllocatePoolWithTag(PagedPool, disize, ALLOC_TAG);
     if (!di2) {
@@ -1184,19 +1371,17 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     
     RtlCopyMemory(di2, di, disize);
     
-    Status = add_dir_item(Vcb, parfcb->subvol, parfcb->inode, crc32, di2, disize, rollback);
+    Status = add_dir_item(Vcb, parfileref->fcb->subvol, parfileref->fcb->inode, crc32, di2, disize, rollback);
     if (!NT_SUCCESS(Status)) {
         ERR("add_dir_item returned %08x\n", Status);
         ExFreePool(utf8);
         return Status;
     }
     
-    // FIXME - handle Irp->Overlay.AllocationSize
-    
     utf8as.Buffer = utf8;
     utf8as.Length = utf8as.MaximumLength = utf8len;
     
-    Status = add_inode_ref(Vcb, parfcb->subvol, inode, parfcb->inode, dirpos, &utf8as, rollback);
+    Status = add_inode_ref(Vcb, parfileref->fcb->subvol, inode, parfileref->fcb->inode, dirpos, &utf8as, rollback);
     if (!NT_SUCCESS(Status)) {
         ERR("add_inode_ref returned %08x\n", Status);
         ExFreePool(utf8);
@@ -1229,7 +1414,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     
         sprintf(val, "0x%x", IrpSp->Parameters.Create.FileAttributes);
     
-        Status = set_xattr(Vcb, parfcb->subvol, inode, EA_DOSATTRIB, EA_DOSATTRIB_HASH, (UINT8*)val, strlen(val), rollback);
+        Status = set_xattr(Vcb, parfileref->fcb->subvol, inode, EA_DOSATTRIB, EA_DOSATTRIB_HASH, (UINT8*)val, strlen(val), rollback);
         if (!NT_SUCCESS(Status)) {
             ERR("set_xattr returned %08x\n", Status);
             ExFreePool(utf8);
@@ -1237,7 +1422,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
         }
     }
     
-    parfcb->subvol->lastinode++;
+    parfileref->fcb->subvol->lastinode++;
     
     fcb = create_fcb();
     if (!fcb) {
@@ -1248,7 +1433,6 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
         
     fcb->Vcb = Vcb;
     
-    RtlZeroMemory(&fcb->inode_item, sizeof(INODE_ITEM));
     fcb->inode_item.generation = Vcb->superblock.generation;
     fcb->inode_item.transid = Vcb->superblock.generation;
     fcb->inode_item.st_size = 0;
@@ -1257,7 +1441,7 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     fcb->inode_item.st_nlink = 1;
 //     fcb->inode_item.st_uid = UID_NOBODY; // FIXME?
     fcb->inode_item.st_gid = GID_NOBODY; // FIXME?
-    fcb->inode_item.st_mode = parfcb ? (parfcb->inode_item.st_mode & ~S_IFDIR) : 0755; // use parent's permissions by default
+    fcb->inode_item.st_mode = parfileref->fcb ? (parfileref->fcb->inode_item.st_mode & ~S_IFDIR) : 0755; // use parent's permissions by default
     fcb->inode_item.st_rdev = 0;
     fcb->inode_item.flags = 0;
     fcb->inode_item.sequence = 1;
@@ -1274,99 +1458,132 @@ static NTSTATUS STDCALL file_create2(PIRP Irp, device_extension* Vcb, PUNICODE_S
     }
     
     // inherit nodatacow flag from parent directory
-    if (parfcb->inode_item.flags & BTRFS_INODE_NODATACOW) {
+    if (parfileref->fcb->inode_item.flags & BTRFS_INODE_NODATACOW) {
         fcb->inode_item.flags |= BTRFS_INODE_NODATACOW;
         
         if (type != BTRFS_TYPE_DIRECTORY)
             fcb->inode_item.flags |= BTRFS_INODE_NODATASUM;
     }
     
-//     fcb->Header.IsFastIoPossible = TRUE;
+    fcb->Header.IsFastIoPossible = fast_io_possible(fcb);
     fcb->Header.AllocationSize.QuadPart = 0;
     fcb->Header.FileSize.QuadPart = 0;
     fcb->Header.ValidDataLength.QuadPart = 0;
     
     fcb->atts = IrpSp->Parameters.Create.FileAttributes;
     
-    if (options & FILE_DELETE_ON_CLOSE)
-        fcb->delete_on_close = TRUE;
-    
-    fcb->par = parfcb;
-    rc = InterlockedIncrement(&parfcb->refcount);
 #ifdef DEBUG_FCB_REFCOUNTS
-    WARN("fcb %p: refcount now %i (%.*S)\n", parfcb, rc, parfcb->full_filename.Length / sizeof(WCHAR), parfcb->full_filename.Buffer);
+    rc = InterlockedIncrement(&parfileref->fcb->refcount);
+    WARN("fcb %p: refcount now %i (%S)\n", parfileref->fcb, rc, file_desc_fileref(parfileref));
+#else
+    InterlockedIncrement(&parfileref->fcb->refcount);
 #endif
-    fcb->subvol = parfcb->subvol;
+    fcb->subvol = parfileref->fcb->subvol;
     fcb->inode = inode;
     fcb->type = type;
     
-    fcb->utf8.MaximumLength = fcb->utf8.Length = utf8len;
-    fcb->utf8.Buffer = utf8;
-    
-    Status = fcb_get_new_sd(fcb, IrpSp->Parameters.Create.SecurityContext->AccessState);
+    Status = fcb_get_new_sd(fcb, parfileref, IrpSp->Parameters.Create.SecurityContext->AccessState);
     
     if (!NT_SUCCESS(Status)) {
         ERR("fcb_get_new_sd returned %08x\n", Status);
-        ExFreePool(utf8);
+        free_fcb(fcb);
         return Status;
     }
+    
+    fileref = create_fileref();
+    if (!fileref) {
+        ERR("out of memory\n");
+        free_fcb(fcb);
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    fileref->fcb = fcb;
 
-    fcb->filepart = *fpus;
+    fileref->utf8.MaximumLength = fileref->utf8.Length = utf8len;
+    fileref->utf8.Buffer = utf8;
+    
+    fileref->filepart = *fpus;
         
-    Status = set_xattr(Vcb, parfcb->subvol, inode, EA_NTACL, EA_NTACL_HASH, (UINT8*)fcb->sd, RtlLengthSecurityDescriptor(fcb->sd), rollback);
+    Status = set_xattr(Vcb, parfileref->fcb->subvol, inode, EA_NTACL, EA_NTACL_HASH, (UINT8*)fcb->sd, RtlLengthSecurityDescriptor(fcb->sd), rollback);
     if (!NT_SUCCESS(Status)) {
         ERR("set_xattr returned %08x\n", Status);
-        ExFreePool(utf8);
+        free_fileref(fileref);
         return Status;
     }
     
-    fcb->full_filename.Length = parfcb->full_filename.Length + (parfcb->full_filename.Length == sizeof(WCHAR) ? 0 : sizeof(WCHAR)) + fcb->filepart.Length;
-    fcb->full_filename.MaximumLength = fcb->full_filename.Length;
-    fcb->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, fcb->full_filename.Length, ALLOC_TAG);
-    if (!fcb->full_filename.Buffer) {
+    fileref->full_filename.Length = parfileref->full_filename.Length + (parfileref->full_filename.Length == sizeof(WCHAR) ? 0 : sizeof(WCHAR)) + fileref->filepart.Length;
+    fileref->full_filename.MaximumLength = fileref->full_filename.Length;
+    fileref->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, fileref->full_filename.Length, ALLOC_TAG);
+    if (!fileref->full_filename.Buffer) {
         ERR("out of memory\n");
-        ExFreePool(utf8);
+        free_fileref(fileref);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
-    RtlCopyMemory(fcb->full_filename.Buffer, parfcb->full_filename.Buffer, parfcb->full_filename.Length);
+    RtlCopyMemory(fileref->full_filename.Buffer, parfileref->full_filename.Buffer, parfileref->full_filename.Length);
     
-    if (parfcb->full_filename.Length > sizeof(WCHAR))
-        fcb->full_filename.Buffer[parfcb->full_filename.Length / sizeof(WCHAR)] = '\\';
+    if (parfileref->full_filename.Length > sizeof(WCHAR))
+        fileref->full_filename.Buffer[parfileref->full_filename.Length / sizeof(WCHAR)] = '\\';
     
-    RtlCopyMemory(&fcb->full_filename.Buffer[(parfcb->full_filename.Length / sizeof(WCHAR)) + (parfcb->full_filename.Length == sizeof(WCHAR) ? 0 : 1)], fcb->filepart.Buffer, fcb->filepart.Length);
+    RtlCopyMemory(&fileref->full_filename.Buffer[(parfileref->full_filename.Length / sizeof(WCHAR)) + (parfileref->full_filename.Length == sizeof(WCHAR) ? 0 : 1)],
+                  fileref->filepart.Buffer, fileref->filepart.Length);
     
     ii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
     if (!ii) {
         ERR("out of memory\n");
-        ExFreePool(utf8);
+        free_fileref(fileref);
         return STATUS_INSUFFICIENT_RESOURCES;
     }
     
-    RtlCopyMemory(ii, &fcb->inode_item, sizeof(INODE_ITEM));
-    insert_tree_item(Vcb, parfcb->subvol, inode, TYPE_INODE_ITEM, 0, ii, sizeof(INODE_ITEM), NULL, rollback);
+    if (Irp->Overlay.AllocationSize.QuadPart > 0) {
+        Status = extend_file(fcb, fileref, Irp->Overlay.AllocationSize.QuadPart, TRUE, rollback);
+        
+        if (!NT_SUCCESS(Status)) {
+            ERR("extend_file returned %08x\n", Status);
+            free_fileref(fileref);
+            return Status;
+        }
+    }
     
-    *pfcb = fcb;
+    RtlCopyMemory(ii, &fcb->inode_item, sizeof(INODE_ITEM));
+    insert_tree_item(Vcb, fcb->subvol, inode, TYPE_INODE_ITEM, 0, ii, sizeof(INODE_ITEM), NULL, rollback);
     
     fcb->subvol->root_item.ctransid = Vcb->superblock.generation;
     fcb->subvol->root_item.ctime = now;
     
-    InsertTailList(&fcb->par->children, &fcb->list_entry);
+    fileref->parent = parfileref;
+    InsertTailList(&parfileref->children, &fileref->list_entry);
+#ifdef DEBUG_FCB_REFCOUNTS
+    rc = InterlockedIncrement(&parfileref->refcount);
+    WARN("fileref %p: refcount now %i\n", parfileref, rc);
+#else
+    InterlockedIncrement(&parfileref->refcount);
+#endif
+    InsertTailList(&fcb->subvol->fcbs, &fcb->list_entry);
+    
+    *pfr = fileref;
     
-    TRACE("created new file %.*S in subvol %llx, inode %llx\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer, fcb->subvol->id, fcb->inode);
+    TRACE("created new file %S in subvol %llx, inode %llx\n", file_desc_fileref(fileref), fcb->subvol->id, fcb->inode);
     
     return STATUS_SUCCESS;
 }
 
 static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJECT FileObject, PUNICODE_STRING fnus, ULONG disposition, ULONG options, LIST_ENTRY* rollback) {
     NTSTATUS Status;
-    fcb *fcb, *parfcb = NULL;
+//     fcb *fcb, *parfcb = NULL;
+    file_ref *fileref, *parfileref = NULL, *related;
     ULONG i, j;
-    ULONG utf8len;
+//     ULONG utf8len;
     ccb* ccb;
     static WCHAR datasuf[] = {':','$','D','A','T','A',0};
     UNICODE_STRING dsus, fpus, stream;
+    PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
+    ULONG access;
+    PACCESS_STATE access_state = IrpSp->Parameters.Create.SecurityContext->AccessState;
+#ifdef DEBUG_FCB_REFCOUNTS
     LONG oc;
+#endif
             
     TRACE("(%p, %p, %p, %.*S, %x, %x)\n", Irp, Vcb, FileObject, fnus->Length / sizeof(WCHAR), fnus->Buffer, disposition, options);
     
@@ -1377,20 +1594,25 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
     dsus.Length = dsus.MaximumLength = wcslen(datasuf) * sizeof(WCHAR);
     fpus.Buffer = NULL;
     
-    // FIXME - apparently you can open streams using RelatedFileObject. How can we test this?
+    if (FileObject->RelatedFileObject && FileObject->RelatedFileObject->FsContext2) {
+        struct _ccb* relatedccb = FileObject->RelatedFileObject->FsContext2;
+        
+        related = relatedccb->fileref;
+    } else
+        related = NULL;
     
     ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-    Status = get_fcb(Vcb, &parfcb, fnus, FileObject->RelatedFileObject ? FileObject->RelatedFileObject->FsContext : NULL, TRUE);
+    Status = open_fileref(Vcb, &parfileref, &FileObject->FileName, related, TRUE, NULL);
     ExReleaseResourceLite(&Vcb->fcb_lock);
     if (!NT_SUCCESS(Status))
         goto end;
     
-    if (parfcb->type != BTRFS_TYPE_DIRECTORY) {
+    if (parfileref->fcb->type != BTRFS_TYPE_DIRECTORY && (fnus->Length < sizeof(WCHAR) || fnus->Buffer[0] != ':')) {
         Status = STATUS_OBJECT_PATH_NOT_FOUND;
         goto end;
     }
     
-    if (parfcb->subvol->root_item.flags & BTRFS_SUBVOL_READONLY) {
+    if (parfileref->fcb->subvol->root_item.flags & BTRFS_SUBVOL_READONLY) {
         Status = STATUS_ACCESS_DENIED;
         goto end;
     }
@@ -1448,7 +1670,8 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
     }
     
     if (stream.Length > 0) {
-        struct _fcb* newpar;
+        file_ref* newpar;
+        fcb* fcb;
         static char xapref[] = "user.";
         ULONG xapreflen = strlen(xapref), fnlen;
         LARGE_INTEGER time;
@@ -1456,31 +1679,66 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
         KEY searchkey;
         traverse_ptr tp;
         INODE_ITEM* ii;
+        ULONG utf8len;
+        UINT64 offset;
+#ifdef DEBUG_FCB_REFCOUNTS
         LONG rc;
+#endif
         
         TRACE("fpus = %.*S\n", fpus.Length / sizeof(WCHAR), fpus.Buffer);
         TRACE("stream = %.*S\n", stream.Length / sizeof(WCHAR), stream.Buffer);
         
         ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-        Status = get_fcb(Vcb, &newpar, &fpus, parfcb, FALSE);
+        Status = open_fileref(Vcb, &newpar, &fpus, parfileref, FALSE, NULL);
         ExReleaseResourceLite(&Vcb->fcb_lock);
         
         if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {
-            Status = file_create2(Irp, Vcb, &fpus, parfcb, options, &newpar, rollback);
+            UNICODE_STRING fpus2;
+            
+            if (!SeAccessCheck(parfileref->fcb->sd, &access_state->SubjectSecurityContext, FALSE, options & FILE_DIRECTORY_FILE ? FILE_ADD_SUBDIRECTORY : FILE_ADD_FILE, 0, NULL,
+                               IoGetFileObjectGenericMapping(), IrpSp->Flags & SL_FORCE_ACCESS_CHECK ? UserMode : Irp->RequestorMode, &access, &Status)) {
+                WARN("SeAccessCheck failed, returning %08x\n", Status);
+                goto end;
+            }
+            
+            if (!is_file_name_valid(&fpus))
+                return STATUS_OBJECT_NAME_INVALID;
+            
+            fpus2.Length = fpus2.MaximumLength = fpus.Length;
+            fpus2.Buffer = ExAllocatePoolWithTag(PagedPool, fpus2.MaximumLength, ALLOC_TAG);
+            
+            if (!fpus2.Buffer) {
+                ERR("out of memory\n");
+                Status = STATUS_INSUFFICIENT_RESOURCES;
+                goto end;
+            }
+            
+            RtlCopyMemory(fpus2.Buffer, fpus.Buffer, fpus2.Length);
+            
+            Status = file_create2(Irp, Vcb, &fpus2, parfileref, options, &newpar, rollback);
         
             if (!NT_SUCCESS(Status)) {
                 ERR("file_create2 returned %08x\n", Status);
+                ExFreePool(fpus2.Buffer);
                 goto end;
             }
+            
+            // FIXME - send notification
         } else if (!NT_SUCCESS(Status)) {
-            ERR("get_fcb returned %08x\n", Status);
+            ERR("open_fileref returned %08x\n", Status);
             goto end;
         }
         
-        free_fcb(parfcb);
-        parfcb = newpar;
+        free_fileref(parfileref);
+        parfileref = newpar;
+        
+        if (!SeAccessCheck(parfileref->fcb->sd, &access_state->SubjectSecurityContext, FALSE, access_state->OriginalDesiredAccess, 0, NULL,
+                           IoGetFileObjectGenericMapping(), IrpSp->Flags & SL_FORCE_ACCESS_CHECK ? UserMode : Irp->RequestorMode, &access, &Status)) {
+            WARN("SeAccessCheck failed, returning %08x\n", Status);
+            goto end;
+        }
         
-        if (newpar->type != BTRFS_TYPE_FILE && newpar->type != BTRFS_TYPE_SYMLINK) {
+        if (parfileref->fcb->type != BTRFS_TYPE_FILE && parfileref->fcb->type != BTRFS_TYPE_SYMLINK) {
             WARN("parent not file or symlink\n");
             Status = STATUS_INVALID_PARAMETER;
             goto end;
@@ -1501,29 +1759,30 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
         
         fcb->Vcb = Vcb;
         
-//         fcb->Header.IsFastIoPossible = TRUE;
+        fcb->Header.IsFastIoPossible = fast_io_possible(fcb);
         fcb->Header.AllocationSize.QuadPart = 0;
         fcb->Header.FileSize.QuadPart = 0;
         fcb->Header.ValidDataLength.QuadPart = 0;
         
-        if (options & FILE_DELETE_ON_CLOSE)
-            fcb->delete_on_close = TRUE;
-        
-        fcb->par = parfcb;
-        rc = InterlockedIncrement(&parfcb->refcount);
 #ifdef DEBUG_FCB_REFCOUNTS
-        WARN("fcb %p: refcount now %i (%.*S)\n", parfcb, rc, parfcb->full_filename.Length / sizeof(WCHAR), parfcb->full_filename.Buffer);
+        rc = InterlockedIncrement(&parfileref->fcb->refcount);
+        WARN("fcb %p: refcount now %i (%S)\n", parfileref->fcb, rc, file_desc_fileref(parfileref));
+#else
+        InterlockedIncrement(&parfileref->fcb->refcount);
 #endif
-        fcb->subvol = parfcb->subvol;
-        fcb->inode = parfcb->inode;
-        fcb->type = parfcb->type;
+        fcb->subvol = parfileref->fcb->subvol;
+        fcb->inode = parfileref->fcb->inode;
+        fcb->type = parfileref->fcb->type;
         
         fcb->ads = TRUE;
         fcb->adssize = 0;
         
         Status = RtlUnicodeToUTF8N(NULL, 0, &utf8len, stream.Buffer, stream.Length);
-        if (!NT_SUCCESS(Status))
+        if (!NT_SUCCESS(Status)) {
+            ERR("RtlUnicodeToUTF8N 1 returned %08x\n", Status);
+            free_fcb(fcb);
             goto end;
+        }
         
         fcb->adsxattr.Length = utf8len + xapreflen;
         fcb->adsxattr.MaximumLength = fcb->adsxattr.Length + 1;
@@ -1539,6 +1798,7 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
         
         Status = RtlUnicodeToUTF8N(&fcb->adsxattr.Buffer[xapreflen], utf8len, &utf8len, stream.Buffer, stream.Length);
         if (!NT_SUCCESS(Status)) {
+            ERR("RtlUnicodeToUTF8N 2 returned %08x\n", Status);
             free_fcb(fcb);
             goto end;
         }
@@ -1550,91 +1810,123 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
         fcb->adshash = calc_crc32c(0xfffffffe, (UINT8*)fcb->adsxattr.Buffer, fcb->adsxattr.Length);
         TRACE("adshash = %08x\n", fcb->adshash);
 
-        fcb->name_offset = parfcb->full_filename.Length / sizeof(WCHAR);
-        if (parfcb != Vcb->root_fcb)
-            fcb->name_offset++;
-
-        fcb->filepart.MaximumLength = fcb->filepart.Length = stream.Length;
-        fcb->filepart.Buffer = ExAllocatePoolWithTag(PagedPool, fcb->filepart.MaximumLength, ALLOC_TAG);
-        if (!fcb->filepart.Buffer) {
+        fileref = create_fileref();
+        if (!fileref) {
             ERR("out of memory\n");
             free_fcb(fcb);
             Status = STATUS_INSUFFICIENT_RESOURCES;
             goto end;
         }
         
-        RtlCopyMemory(fcb->filepart.Buffer, stream.Buffer, stream.Length);
+        fileref->fcb = fcb;
         
-        fnlen = (fcb->name_offset * sizeof(WCHAR)) + fcb->filepart.Length;
+        fileref->name_offset = parfileref->full_filename.Length / sizeof(WCHAR);
+        if (parfileref != Vcb->root_fileref)
+            fileref->name_offset++;
 
-        fcb->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, fnlen, ALLOC_TAG);
-        if (!fcb->full_filename.Buffer) {
+        fileref->filepart.MaximumLength = fileref->filepart.Length = stream.Length;
+        fileref->filepart.Buffer = ExAllocatePoolWithTag(PagedPool, fileref->filepart.MaximumLength, ALLOC_TAG);
+        if (!fileref->filepart.Buffer) {
             ERR("out of memory\n");
-            free_fcb(fcb);
+            free_fileref(fileref);
             Status = STATUS_INSUFFICIENT_RESOURCES;
             goto end;
         }
         
-        fcb->full_filename.Length = fcb->full_filename.MaximumLength = fnlen;
-        RtlCopyMemory(fcb->full_filename.Buffer, parfcb->full_filename.Buffer, parfcb->full_filename.Length);
-
-        fcb->full_filename.Buffer[parfcb->full_filename.Length / sizeof(WCHAR)] = ':';
+        RtlCopyMemory(fileref->filepart.Buffer, stream.Buffer, stream.Length);
+        
+        fnlen = (fileref->name_offset * sizeof(WCHAR)) + fileref->filepart.Length;
 
-        RtlCopyMemory(&fcb->full_filename.Buffer[fcb->name_offset], fcb->filepart.Buffer, fcb->filepart.Length);
-        TRACE("full_filename = %.*S\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
+        fileref->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, fnlen, ALLOC_TAG);
+        if (!fileref->full_filename.Buffer) {
+            ERR("out of memory\n");
+            free_fileref(fileref);
+            Status = STATUS_INSUFFICIENT_RESOURCES;
+            goto end;
+        }
         
-        InsertTailList(&fcb->par->children, &fcb->list_entry);
+        fileref->full_filename.Length = fileref->full_filename.MaximumLength = fnlen;
+        RtlCopyMemory(fileref->full_filename.Buffer, parfileref->full_filename.Buffer, parfileref->full_filename.Length);
+
+        fileref->full_filename.Buffer[parfileref->full_filename.Length / sizeof(WCHAR)] = ':';
+
+        RtlCopyMemory(&fileref->full_filename.Buffer[fileref->name_offset], fileref->filepart.Buffer, fileref->filepart.Length);
+        TRACE("full_filename = %.*S\n", fileref->full_filename.Length / sizeof(WCHAR), fileref->full_filename.Buffer);
         
-        Status = set_xattr(Vcb, parfcb->subvol, parfcb->inode, fcb->adsxattr.Buffer, fcb->adshash, (UINT8*)"", 0, rollback);
+        Status = set_xattr(Vcb, parfileref->fcb->subvol, parfileref->fcb->inode, fcb->adsxattr.Buffer, fcb->adshash, (UINT8*)"", 0, rollback);
         if (!NT_SUCCESS(Status)) {
             ERR("set_xattr returned %08x\n", Status);
-            free_fcb(fcb);
+            free_fileref(fileref);
             goto end;
         }
         
+        InsertTailList(&fcb->subvol->fcbs, &fcb->list_entry);
+        
         KeQuerySystemTime(&time);
         win_time_to_unix(time, &now);
         
-        parfcb->inode_item.transid = Vcb->superblock.generation;
-        parfcb->inode_item.sequence++;
-        parfcb->inode_item.st_ctime = now;
+        parfileref->fcb->inode_item.transid = Vcb->superblock.generation;
+        parfileref->fcb->inode_item.sequence++;
+        parfileref->fcb->inode_item.st_ctime = now;
         
-        searchkey.obj_id = parfcb->inode;
+        searchkey.obj_id = parfileref->fcb->inode;
         searchkey.obj_type = TYPE_INODE_ITEM;
         searchkey.offset = 0xffffffffffffffff;
         
-        Status = find_item(Vcb, parfcb->subvol, &tp, &searchkey, FALSE);
+        Status = find_item(Vcb, parfileref->fcb->subvol, &tp, &searchkey, FALSE);
         if (!NT_SUCCESS(Status)) {
             ERR("error - find_item returned %08x\n", Status);
+            free_fileref(fileref);
             goto end;
         }
         
         if (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
             delete_tree_item(Vcb, &tp, rollback);
+            offset = tp.item->key.offset;
         } else {
-            WARN("could not find INODE_ITEM for inode %llx in subvol %llx\n", searchkey.obj_id, parfcb->subvol->id);
+            WARN("could not find INODE_ITEM for inode %llx in subvol %llx\n", searchkey.obj_id, parfileref->fcb->subvol->id);
+            offset = 0;
         }
         
-        free_traverse_ptr(&tp);
-        
         ii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
         if (!ii) {
             ERR("out of memory\n");
             Status = STATUS_INSUFFICIENT_RESOURCES;
+            free_fileref(fileref);
             goto end;
         }
     
-        RtlCopyMemory(ii, &parfcb->inode_item, sizeof(INODE_ITEM));
+        RtlCopyMemory(ii, &parfileref->fcb->inode_item, sizeof(INODE_ITEM));
+        
+        insert_tree_item(Vcb, parfileref->fcb->subvol, parfileref->fcb->inode, TYPE_INODE_ITEM, offset, ii, sizeof(INODE_ITEM), NULL, rollback);
         
-        insert_tree_item(Vcb, parfcb->subvol, parfcb->inode, TYPE_INODE_ITEM, 0, ii, sizeof(INODE_ITEM), NULL, rollback);
+        parfileref->fcb->subvol->root_item.ctransid = Vcb->superblock.generation;
+        parfileref->fcb->subvol->root_item.ctime = now;
         
-        parfcb->subvol->root_item.ctransid = Vcb->superblock.generation;
-        parfcb->subvol->root_item.ctime = now;
+        fileref->parent = (struct _file_ref*)parfileref;
+        InsertTailList(&parfileref->children, &fileref->list_entry);
+#ifdef DEBUG_FCB_REFCOUNTS
+        rc = InterlockedIncrement(&parfileref->refcount);
+        WARN("fileref %p: refcount now %i\n", parfileref, rc);
+#else
+        InterlockedIncrement(&parfileref->refcount);
+#endif
         
         ExFreePool(fpus.Buffer);
         fpus.Buffer = NULL;
     } else {
-        Status = file_create2(Irp, Vcb, &fpus, parfcb, options, &fcb, rollback);
+        if (!SeAccessCheck(parfileref->fcb->sd, &access_state->SubjectSecurityContext, FALSE, options & FILE_DIRECTORY_FILE ? FILE_ADD_SUBDIRECTORY : FILE_ADD_FILE, 0, NULL,
+                           IoGetFileObjectGenericMapping(), IrpSp->Flags & SL_FORCE_ACCESS_CHECK ? UserMode : Irp->RequestorMode, &access, &Status)) {
+            WARN("SeAccessCheck failed, returning %08x\n", Status);
+            goto end;
+        }
+        
+        if (!is_file_name_valid(&fpus)) {
+            Status = STATUS_OBJECT_NAME_INVALID;
+            goto end;
+        }
+        
+        Status = file_create2(Irp, Vcb, &fpus, parfileref, options, &fileref, rollback);
         
         if (!NT_SUCCESS(Status)) {
             ERR("file_create2 returned %08x\n", Status);
@@ -1642,26 +1934,20 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
         }
     }
     
-    ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-    
-    if (Vcb->fcbs)
-        Vcb->fcbs->prev = fcb;
-    
-    fcb->next = Vcb->fcbs;
-    Vcb->fcbs = fcb;
-    
-    ExReleaseResourceLite(&Vcb->fcb_lock);
-    
-    Status = attach_fcb_to_fileobject(Vcb, fcb, FileObject);
+    FileObject->FsContext = fileref->fcb;
     
     ccb = ExAllocatePoolWithTag(NonPagedPool, sizeof(*ccb), ALLOC_TAG);
     if (!ccb) {
         ERR("out of memory\n");
         Status = STATUS_INSUFFICIENT_RESOURCES;
+        free_fileref(fileref);
         goto end;
     }
     
     RtlZeroMemory(ccb, sizeof(*ccb));
+    
+    ccb->fileref = fileref;
+    
     ccb->NodeType = BTRFS_NODE_TYPE_CCB;
     ccb->NodeSize = sizeof(ccb);
     ccb->disposition = disposition;
@@ -1670,48 +1956,49 @@ static NTSTATUS STDCALL file_create(PIRP Irp, device_extension* Vcb, PFILE_OBJEC
     RtlInitUnicodeString(&ccb->query_string, NULL);
     ccb->has_wildcard = FALSE;
     ccb->specific_file = FALSE;
+    ccb->access = access;
     
-    oc = InterlockedIncrement(&fcb->open_count);
 #ifdef DEBUG_FCB_REFCOUNTS
-    ERR("fcb %p: open_count now %i\n", fcb, oc);
+    oc = InterlockedIncrement(&fileref->fcb->open_count);
+    ERR("fcb %p: open_count now %i\n", fileref->fcb, oc);
+#else
+    InterlockedIncrement(&fileref->fcb->open_count);
 #endif
     
     FileObject->FsContext2 = ccb;
 
-    FileObject->SectionObjectPointer = &fcb->nonpaged->segment_object;
+    FileObject->SectionObjectPointer = &fileref->fcb->nonpaged->segment_object;
     
-    TRACE("returning FCB %p with parent %p\n", fcb, parfcb);
+//     TRACE("returning FCB %p with parent %p\n", fcb, parfcb);
     
     Status = consider_write(Vcb);
     
     if (NT_SUCCESS(Status)) {
-        ULONG fnlen;
-
-        fcb->name_offset = fcb->par->full_filename.Length / sizeof(WCHAR);
-                
-        if (fcb->par != Vcb->root_fcb)
-            fcb->name_offset++;
-        
-        fnlen = (fcb->name_offset * sizeof(WCHAR)) + fcb->filepart.Length;
-        
-        fcb->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, fnlen, ALLOC_TAG);
-        if (!fcb->full_filename.Buffer) {
-            ERR("out of memory\n");
-            Status = STATUS_INSUFFICIENT_RESOURCES;
-            goto end;
-        }   
-        
-        fcb->full_filename.Length = fcb->full_filename.MaximumLength = fnlen;
-        RtlCopyMemory(fcb->full_filename.Buffer, fcb->par->full_filename.Buffer, fcb->par->full_filename.Length);
-        
-        if (fcb->par != Vcb->root_fcb)
-            fcb->full_filename.Buffer[fcb->par->full_filename.Length / sizeof(WCHAR)] = '\\';
-        
-        RtlCopyMemory(&fcb->full_filename.Buffer[fcb->name_offset], fcb->filepart.Buffer, fcb->filepart.Length);
+//         ULONG fnlen;
+// 
+//         fcb->name_offset = fcb->par->full_filename.Length / sizeof(WCHAR);
+//                 
+//         if (fcb->par != Vcb->root_fcb)
+//             fcb->name_offset++;
+//         
+//         fnlen = (fcb->name_offset * sizeof(WCHAR)) + fcb->filepart.Length;
+//         
+//         fcb->full_filename.Buffer = ExAllocatePoolWithTag(PagedPool, fnlen, ALLOC_TAG);
+//         if (!fcb->full_filename.Buffer) {
+//             ERR("out of memory\n");
+//             Status = STATUS_INSUFFICIENT_RESOURCES;
+//             goto end;
+//         }   
+//         
+//         fcb->full_filename.Length = fcb->full_filename.MaximumLength = fnlen;
+//         RtlCopyMemory(fcb->full_filename.Buffer, fcb->par->full_filename.Buffer, fcb->par->full_filename.Length);
+//         
+//         if (fcb->par != Vcb->root_fcb)
+//             fcb->full_filename.Buffer[fcb->par->full_filename.Length / sizeof(WCHAR)] = '\\';
+//         
+//         RtlCopyMemory(&fcb->full_filename.Buffer[fcb->name_offset], fcb->filepart.Buffer, fcb->filepart.Length);
         
-        FsRtlNotifyFullReportChange(Vcb->NotifySync, &Vcb->DirNotifyList, (PSTRING)&fcb->full_filename, fcb->name_offset * sizeof(WCHAR), NULL, NULL,
-                                    options & FILE_DIRECTORY_FILE ? FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME,
-                                    FILE_ACTION_ADDED, NULL);
+        send_notification_fileref(fileref, options & FILE_DIRECTORY_FILE ? FILE_NOTIFY_CHANGE_DIR_NAME : FILE_NOTIFY_CHANGE_FILE_NAME, FILE_ACTION_ADDED);
         
         goto end2;
     }
@@ -1721,8 +2008,8 @@ end:
         ExFreePool(fpus.Buffer);
     
 end2:
-    if (parfcb)
-        free_fcb(parfcb);
+    if (parfileref)
+        free_fileref(parfileref);
     
     return Status;
 }
@@ -1852,11 +2139,12 @@ static __inline void debug_create_options(ULONG RequestedOptions) {
     }
 }
 
-static NTSTATUS update_inode_item(device_extension* Vcb, root* subvol, UINT64 inode, INODE_ITEM* ii, LIST_ENTRY* rollback) {
+NTSTATUS update_inode_item(device_extension* Vcb, root* subvol, UINT64 inode, INODE_ITEM* ii, LIST_ENTRY* rollback) {
     KEY searchkey;
     traverse_ptr tp;
     INODE_ITEM* newii;
     NTSTATUS Status;
+    UINT64 offset = 0;
     
     searchkey.obj_id = inode;
     searchkey.obj_type = TYPE_INODE_ITEM;
@@ -1870,12 +2158,12 @@ static NTSTATUS update_inode_item(device_extension* Vcb, root* subvol, UINT64 in
     
     if (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
         delete_tree_item(Vcb, &tp, rollback);
+        
+        offset = tp.item->key.offset;
     } else {
         WARN("could not find INODE_ITEM for inode %llx in subvol %llx\n", searchkey.obj_id, subvol->id);
     }
     
-    free_traverse_ptr(&tp);
-    
     newii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
     if (!newii) {
         ERR("out of memory\n");
@@ -1884,23 +2172,143 @@ static NTSTATUS update_inode_item(device_extension* Vcb, root* subvol, UINT64 in
 
     RtlCopyMemory(newii, ii, sizeof(INODE_ITEM));
     
-    insert_tree_item(Vcb, subvol, inode, TYPE_INODE_ITEM, 0, newii, sizeof(INODE_ITEM), NULL, rollback);
+    insert_tree_item(Vcb, subvol, inode, TYPE_INODE_ITEM, offset, newii, sizeof(INODE_ITEM), NULL, rollback);
+    
+    return STATUS_SUCCESS;
+}
+
+static NTSTATUS get_reparse_block(fcb* fcb, UINT8** data) {
+    NTSTATUS Status;
+    
+    if (fcb->type == BTRFS_TYPE_FILE || fcb->type == BTRFS_TYPE_SYMLINK) {
+        ULONG size, bytes_read, i;
+        
+        if (fcb->inode_item.st_size < sizeof(ULONG)) {
+            WARN("file was too short to be a reparse point\n");
+            return STATUS_INVALID_PARAMETER;
+        }
+        
+        // 0x10007 = 0xffff (maximum length of data buffer) + 8 bytes header
+        size = min(0x10007, fcb->inode_item.st_size);
+        
+        *data = ExAllocatePoolWithTag(PagedPool, size, ALLOC_TAG);
+        if (!*data) {
+            ERR("out of memory\n");
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+        
+        Status = read_file(fcb->Vcb, fcb->subvol, fcb->inode, *data, 0, size, &bytes_read);
+        if (!NT_SUCCESS(Status)) {
+            ERR("read_file returned %08x\n", Status);
+            ExFreePool(*data);
+            return Status;
+        }
+        
+        if (fcb->type == BTRFS_TYPE_SYMLINK) {
+            ULONG stringlen, subnamelen, printnamelen, reqlen;
+            REPARSE_DATA_BUFFER* rdb;
+            
+            Status = RtlUTF8ToUnicodeN(NULL, 0, &stringlen, (char*)*data, bytes_read);
+            if (!NT_SUCCESS(Status)) {
+                ERR("RtlUTF8ToUnicodeN 1 returned %08x\n", Status);
+                ExFreePool(*data);
+                return Status;
+            }
+            
+            subnamelen = stringlen;
+            printnamelen = stringlen;
+            
+            reqlen = offsetof(REPARSE_DATA_BUFFER, SymbolicLinkReparseBuffer.PathBuffer) + subnamelen + printnamelen;
+            
+            rdb = ExAllocatePoolWithTag(PagedPool, reqlen, ALLOC_TAG);
+            
+            if (!rdb) {
+                ERR("out of memory\n");
+                ExFreePool(*data);
+                return STATUS_INSUFFICIENT_RESOURCES;
+            }
+            
+            rdb->ReparseTag = IO_REPARSE_TAG_SYMLINK;
+            rdb->ReparseDataLength = reqlen - offsetof(REPARSE_DATA_BUFFER, SymbolicLinkReparseBuffer);
+            rdb->Reserved = 0;
+            
+            rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset = 0;
+            rdb->SymbolicLinkReparseBuffer.SubstituteNameLength = subnamelen;
+            rdb->SymbolicLinkReparseBuffer.PrintNameOffset = subnamelen;
+            rdb->SymbolicLinkReparseBuffer.PrintNameLength = printnamelen;
+            rdb->SymbolicLinkReparseBuffer.Flags = SYMLINK_FLAG_RELATIVE;
+            
+            Status = RtlUTF8ToUnicodeN(&rdb->SymbolicLinkReparseBuffer.PathBuffer[rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(WCHAR)],
+                                    stringlen, &stringlen, (char*)*data, size);
+
+            if (!NT_SUCCESS(Status)) {
+                ERR("RtlUTF8ToUnicodeN 2 returned %08x\n", Status);
+                ExFreePool(rdb);
+                ExFreePool(*data);
+                return Status;
+            }
+            
+            for (i = 0; i < stringlen / sizeof(WCHAR); i++) {
+                if (rdb->SymbolicLinkReparseBuffer.PathBuffer[(rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(WCHAR)) + i] == '/')
+                    rdb->SymbolicLinkReparseBuffer.PathBuffer[(rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(WCHAR)) + i] = '\\';
+            }
+            
+            RtlCopyMemory(&rdb->SymbolicLinkReparseBuffer.PathBuffer[rdb->SymbolicLinkReparseBuffer.PrintNameOffset / sizeof(WCHAR)],
+                        &rdb->SymbolicLinkReparseBuffer.PathBuffer[rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(WCHAR)],
+                        rdb->SymbolicLinkReparseBuffer.SubstituteNameLength);
+            
+            ExFreePool(*data);
+            
+            *data = (UINT8*)rdb;
+        } else {
+            Status = FsRtlValidateReparsePointBuffer(bytes_read, (REPARSE_DATA_BUFFER*)*data);
+            if (!NT_SUCCESS(Status)) {
+                ERR("FsRtlValidateReparsePointBuffer returned %08x\n", Status);
+                ExFreePool(*data);
+                return Status;
+            }
+        }
+    } else {
+        UINT16 datalen;
+        
+        if (!get_xattr(fcb->Vcb, fcb->subvol, fcb->inode, EA_REPARSE, EA_REPARSE_HASH, data, &datalen))
+            return STATUS_INTERNAL_ERROR;
+        
+        if (!*data)
+            return STATUS_INTERNAL_ERROR;
+        
+        if (datalen < sizeof(ULONG)) {
+            WARN("xattr was too short to be a reparse point\n");
+            ExFreePool(*data);
+            return STATUS_INTERNAL_ERROR;
+        }
+        
+        Status = FsRtlValidateReparsePointBuffer(datalen, (REPARSE_DATA_BUFFER*)*data);
+        if (!NT_SUCCESS(Status)) {
+            ERR("FsRtlValidateReparsePointBuffer returned %08x\n", Status);
+            ExFreePool(*data);
+            return Status;
+        }
+    }
     
     return STATUS_SUCCESS;
 }
 
-static NTSTATUS STDCALL create_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_ENTRY* rollback) {
+static NTSTATUS STDCALL open_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_ENTRY* rollback) {
     PFILE_OBJECT FileObject;
     ULONG RequestedDisposition;
     ULONG options;
     NTSTATUS Status;
-    fcb* fcb;
     ccb* ccb;
     device_extension* Vcb = DeviceObject->DeviceExtension;
     PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation(Irp);
     ULONG access;
     PACCESS_STATE access_state = Stack->Parameters.Create.SecurityContext->AccessState;
+    USHORT unparsed;
+    file_ref *related, *fileref;
+#ifdef DEBUG_FCB_REFCOUNTS
     LONG oc;
+#endif
     
     Irp->IoStatus.Information = 0;
     
@@ -1912,6 +2320,12 @@ static NTSTATUS STDCALL create_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_
         Status = STATUS_INVALID_PARAMETER;
         goto exit;
     }
+    
+    if (options & FILE_OPEN_BY_FILE_ID) {
+        WARN("FILE_OPEN_BY_FILE_ID not supported\n");
+        Status = STATUS_NOT_IMPLEMENTED;
+        goto exit;
+    }
 
     FileObject = Stack->FileObject;
 
@@ -1957,22 +2371,58 @@ static NTSTATUS STDCALL create_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_
     }
     
     // FIXME - if Vcb->readonly or subvol readonly, don't allow the write ACCESS_MASK flags
+    
+    if (FileObject->RelatedFileObject && FileObject->RelatedFileObject->FsContext2) {
+        struct _ccb* relatedccb = FileObject->RelatedFileObject->FsContext2;
+        
+        related = relatedccb->fileref;
+    } else
+        related = NULL;
 
     ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
-    Status = get_fcb(Vcb, &fcb, &FileObject->FileName, FileObject->RelatedFileObject ? FileObject->RelatedFileObject->FsContext : NULL, Stack->Flags & SL_OPEN_TARGET_DIRECTORY);
+    Status = open_fileref(Vcb, &fileref, &FileObject->FileName, related, Stack->Flags & SL_OPEN_TARGET_DIRECTORY, &unparsed);
     ExReleaseResourceLite(&Vcb->fcb_lock);
     
-    if (NT_SUCCESS(Status) && fcb->deleted) {
-        free_fcb(fcb);
-        Status = STATUS_OBJECT_NAME_NOT_FOUND;
+    if (Status == STATUS_REPARSE) {
+        REPARSE_DATA_BUFFER* data;
+        
+        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;
+        }
+        
+        Status = STATUS_REPARSE;
+        RtlCopyMemory(&Irp->IoStatus.Information, data, sizeof(ULONG));
+        
+        data->Reserved = unparsed;
+        
+        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;
+        goto exit; // FIXME?
+    }
+    
     if (NT_SUCCESS(Status)) {
         if (RequestedDisposition == FILE_CREATE) {
-            TRACE("file %.*S already exists, returning STATUS_OBJECT_NAME_COLLISION\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
+            TRACE("file %S already exists, returning STATUS_OBJECT_NAME_COLLISION\n", file_desc_fileref(fileref));
             Status = STATUS_OBJECT_NAME_COLLISION;
-            free_fcb(fcb);
+            free_fileref(fileref);
             goto exit;
         }
     } else if (Status == STATUS_OBJECT_NAME_NOT_FOUND) {
@@ -1981,88 +2431,122 @@ static NTSTATUS STDCALL create_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_
             goto exit;
         }
     } else {
-        TRACE("get_fcb returned %08x\n", Status);
+        TRACE("open_fileref returned %08x\n", Status);
         goto exit;
     }
     
     if (NT_SUCCESS(Status)) { // file already exists
-        struct _fcb* sf;
+        file_ref* sf;
         
         if (Vcb->readonly && RequestedDisposition == FILE_OVERWRITE_IF) {
             Status = STATUS_MEDIA_WRITE_PROTECTED;
-            free_fcb(fcb);
+            free_fileref(fileref);
             goto exit;
         }
         
-        if (fcb->subvol->root_item.flags & BTRFS_SUBVOL_READONLY && (RequestedDisposition == FILE_SUPERSEDE ||
+        if (fileref->fcb->subvol->root_item.flags & BTRFS_SUBVOL_READONLY && (RequestedDisposition == FILE_SUPERSEDE ||
             RequestedDisposition == FILE_OVERWRITE || RequestedDisposition == FILE_OVERWRITE_IF)) {
             Status = STATUS_ACCESS_DENIED;
-            free_fcb(fcb);
+            free_fileref(fileref);
             goto exit;
         }
         
-        TRACE("deleted = %s\n", fcb->deleted ? "TRUE" : "FALSE");
+        TRACE("deleted = %s\n", fileref->deleted ? "TRUE" : "FALSE");
         
-        sf = fcb;
+        sf = fileref;
         while (sf) {
             if (sf->delete_on_close) {
                 WARN("could not open as deletion pending\n");
                 Status = STATUS_DELETE_PENDING;
-                free_fcb(fcb);
+                
+                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+                free_fileref(fileref);
+                ExReleaseResourceLite(&Vcb->fcb_lock);
                 goto exit;
             }
-            sf = sf->par;
+            sf = sf->parent;
+        }
+        
+        if (fileref->fcb->type == BTRFS_TYPE_DIRECTORY && (RequestedDisposition == FILE_SUPERSEDE || RequestedDisposition == FILE_OVERWRITE || RequestedDisposition == FILE_OVERWRITE_IF)) {
+            Status = STATUS_ACCESS_DENIED;
+            free_fileref(fileref);
+            goto exit;
         }
         
-        if (fcb->type == BTRFS_TYPE_SYMLINK && !(options & FILE_OPEN_REPARSE_POINT))  {
-            if (!follow_symlink(fcb, FileObject)) {
-                ERR("follow_symlink failed\n");
-                Status = STATUS_INTERNAL_ERROR;
-                free_fcb(fcb);
+        if ((fileref->fcb->type == BTRFS_TYPE_SYMLINK || fileref->fcb->atts & FILE_ATTRIBUTE_REPARSE_POINT) && !(options & FILE_OPEN_REPARSE_POINT))  {
+            UINT8* data;
+            
+            /* How reparse points work from the point of view of the filesystem appears to
+             * undocumented. When returning STATUS_REPARSE, MSDN encourages us to return
+             * IO_REPARSE in Irp->IoStatus.Information, but that means we have to do our own
+             * translation. If we instead return the reparse tag in Information, and store
+             * a pointer to the reparse data buffer in Irp->Tail.Overlay.AuxiliaryBuffer,
+             * IopSymlinkProcessReparse will do the translation for us. */
+            
+            Status = get_reparse_block(fileref->fcb, &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;
             }
             
             Status = STATUS_REPARSE;
-            Irp->IoStatus.Information = IO_REPARSE;
-            free_fcb(fcb);
+            RtlCopyMemory(&Irp->IoStatus.Information, data, sizeof(ULONG));
+            
+            Irp->Tail.Overlay.AuxiliaryBuffer = (void*)data;
+            
+            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+            free_fileref(fileref);
+            ExReleaseResourceLite(&Vcb->fcb_lock);
             goto exit;
         }
         
-        if (!SeAccessCheck(fcb->sd, &access_state->SubjectSecurityContext, FALSE, access_state->OriginalDesiredAccess, 0, NULL,
+        if (!SeAccessCheck(fileref->fcb->ads ? fileref->parent->fcb->sd : fileref->fcb->sd, &access_state->SubjectSecurityContext, FALSE, access_state->OriginalDesiredAccess, 0, NULL,
             IoGetFileObjectGenericMapping(), Stack->Flags & SL_FORCE_ACCESS_CHECK ? UserMode : Irp->RequestorMode, &access, &Status)) {
             WARN("SeAccessCheck failed, returning %08x\n", Status);
-            free_fcb(fcb);
+        
+            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+            free_fileref(fileref);
+            ExReleaseResourceLite(&Vcb->fcb_lock);
             goto exit;
         }
         
-        if (fcb->open_count > 0) {
-            Status = IoCheckShareAccess(access, Stack->Parameters.Create.ShareAccess, FileObject, &fcb->share_access, TRUE);
+        if (fileref->fcb->open_count > 0) {
+            Status = IoCheckShareAccess(access, Stack->Parameters.Create.ShareAccess, FileObject, &fileref->fcb->share_access, TRUE);
             
             if (!NT_SUCCESS(Status)) {
                 WARN("IoCheckShareAccess failed, returning %08x\n", Status);
-                free_fcb(fcb);
+                
+                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+                free_fileref(fileref);
+                ExReleaseResourceLite(&Vcb->fcb_lock);
                 goto exit;
             }
         } else {
-            IoSetShareAccess(access, Stack->Parameters.Create.ShareAccess, FileObject, &fcb->share_access);
+            IoSetShareAccess(access, Stack->Parameters.Create.ShareAccess, FileObject, &fileref->fcb->share_access);
         }
 
         if (access & FILE_WRITE_DATA || options & FILE_DELETE_ON_CLOSE) {
-            if (!MmFlushImageSection(&fcb->nonpaged->segment_object, MmFlushForWrite)) {
+            if (!MmFlushImageSection(&fileref->fcb->nonpaged->segment_object, MmFlushForWrite)) {
                 Status = (options & FILE_DELETE_ON_CLOSE) ? STATUS_CANNOT_DELETE : STATUS_SHARING_VIOLATION;
-                free_fcb(fcb);
+                
+                ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+                free_fileref(fileref);
+                ExReleaseResourceLite(&Vcb->fcb_lock);
                 goto exit;
             }
         }
         
         if (RequestedDisposition == FILE_OVERWRITE || RequestedDisposition == FILE_OVERWRITE_IF || RequestedDisposition == FILE_SUPERSEDE) {
             ULONG defda;
-            LIST_ENTRY changed_sector_list;
             
-            if ((RequestedDisposition == FILE_OVERWRITE || RequestedDisposition == FILE_OVERWRITE_IF) && fcb->atts & FILE_ATTRIBUTE_READONLY) {
+            if ((RequestedDisposition == FILE_OVERWRITE || RequestedDisposition == FILE_OVERWRITE_IF) && fileref->fcb->atts & FILE_ATTRIBUTE_READONLY) {
                 WARN("cannot overwrite readonly file\n");
                 Status = STATUS_ACCESS_DENIED;
-                free_fcb(fcb);
+                free_fileref(fileref);
                 goto exit;
             }
     
@@ -2073,137 +2557,100 @@ static NTSTATUS STDCALL create_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_
 //                 free_fcb(fcb);
 //                 goto exit;
 //             }
-            InitializeListHead(&changed_sector_list);
             
             // FIXME - make sure not ADS!
-            Status = truncate_file(fcb, fcb->inode_item.st_size, rollback);
+            Status = truncate_file(fileref->fcb, fileref->fcb->inode_item.st_size, rollback);
             if (!NT_SUCCESS(Status)) {
                 ERR("truncate_file returned %08x\n", Status);
-                free_fcb(fcb);
+                free_fileref(fileref);
                 goto exit;
             }
             
-            Status = update_inode_item(Vcb, fcb->subvol, fcb->inode, &fcb->inode_item, rollback);
+            if (Irp->Overlay.AllocationSize.QuadPart > 0) {
+                Status = extend_file(fileref->fcb, fileref, Irp->Overlay.AllocationSize.QuadPart, TRUE, rollback);
+                
+                if (!NT_SUCCESS(Status)) {
+                    ERR("extend_file returned %08x\n", Status);
+                    free_fileref(fileref);
+                    goto exit;
+                }
+            }
+            
+            Status = update_inode_item(Vcb, fileref->fcb->subvol, fileref->fcb->inode, &fileref->fcb->inode_item, rollback);
             if (!NT_SUCCESS(Status)) {
                 ERR("update_inode_item returned %08x\n", Status);
-                free_fcb(fcb);
+                free_fileref(fileref);
                 goto exit;
             }
             
-            defda = get_file_attributes(Vcb, &fcb->inode_item, fcb->subvol, fcb->inode, fcb->type, fcb->filepart.Length > 0 && fcb->filepart.Buffer[0] == '.', TRUE);
+            defda = get_file_attributes(Vcb, &fileref->fcb->inode_item, fileref->fcb->subvol, fileref->fcb->inode, fileref->fcb->type,
+                                        fileref->filepart.Length > 0 && fileref->filepart.Buffer[0] == '.', TRUE);
             
             if (RequestedDisposition == FILE_SUPERSEDE)
-                fcb->atts = Stack->Parameters.Create.FileAttributes | FILE_ATTRIBUTE_ARCHIVE;
+                fileref->fcb->atts = Stack->Parameters.Create.FileAttributes | FILE_ATTRIBUTE_ARCHIVE;
             else
-                fcb->atts |= Stack->Parameters.Create.FileAttributes | FILE_ATTRIBUTE_ARCHIVE;
+                fileref->fcb->atts |= Stack->Parameters.Create.FileAttributes | FILE_ATTRIBUTE_ARCHIVE;
             
             if (Stack->Parameters.Create.FileAttributes != defda) {
                 char val[64];
             
                 sprintf(val, "0x%x", Stack->Parameters.Create.FileAttributes);
             
-                Status = set_xattr(Vcb, fcb->subvol, fcb->inode, EA_DOSATTRIB, EA_DOSATTRIB_HASH, (UINT8*)val, strlen(val), rollback);
+                Status = set_xattr(Vcb, fileref->fcb->subvol, fileref->fcb->inode, EA_DOSATTRIB, EA_DOSATTRIB_HASH, (UINT8*)val, strlen(val), rollback);
                 if (!NT_SUCCESS(Status)) {
                     ERR("set_xattr returned %08x\n", Status);
-                    free_fcb(fcb);
+                    free_fileref(fileref);
                     goto exit;
                 }
             } else
-                delete_xattr(Vcb, fcb->subvol, fcb->inode, EA_DOSATTRIB, EA_DOSATTRIB_HASH, rollback);
+                delete_xattr(Vcb, fileref->fcb->subvol, fileref->fcb->inode, EA_DOSATTRIB, EA_DOSATTRIB_HASH, rollback);
             
             // FIXME - truncate streams
             // FIXME - do we need to alter parent directory's times?
             // FIXME - send notifications
             
-            Status = consider_write(fcb->Vcb);
+            Status = consider_write(Vcb);
             if (!NT_SUCCESS(Status)) {
                 ERR("consider_write returned %08x\n", Status);
-                free_fcb(fcb);
+                free_fileref(fileref);
                 goto exit;
             }
         }
     
-//         fcb->Header.IsFastIoPossible = TRUE;
-        
-        if (fcb->ads) {
-            fcb->Header.AllocationSize.QuadPart = fcb->adssize;
-            fcb->Header.FileSize.QuadPart = fcb->adssize;
-            fcb->Header.ValidDataLength.QuadPart = fcb->adssize;
-        } else if (fcb->inode_item.st_size == 0 || (fcb->type != BTRFS_TYPE_FILE && fcb->type != BTRFS_TYPE_SYMLINK)) {
-            fcb->Header.AllocationSize.QuadPart = 0;
-            fcb->Header.FileSize.QuadPart = 0;
-            fcb->Header.ValidDataLength.QuadPart = 0;
-        } else {
-            KEY searchkey;
-            traverse_ptr tp;
-            EXTENT_DATA* ed;
-            
-            searchkey.obj_id = fcb->inode;
-            searchkey.obj_type = TYPE_EXTENT_DATA;
-            searchkey.offset = 0xffffffffffffffff;
-            
-            Status = find_item(fcb->Vcb, fcb->subvol, &tp, &searchkey, FALSE);
-            if (!NT_SUCCESS(Status)) {
-                ERR("error - find_item returned %08x\n", Status);
-                free_fcb(fcb);
-                goto exit;
-            }
-            
-            if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
-                ERR("error - could not find EXTENT_DATA items for inode %llx in subvol %llx\n", fcb->inode, fcb->subvol->id);
-                free_traverse_ptr(&tp);
-                free_fcb(fcb);
-                Status = STATUS_INTERNAL_ERROR;
-                goto exit;
-            }
-            
-            if (tp.item->size < sizeof(EXTENT_DATA)) {
-                ERR("(%llx,%x,%llx) was %llx bytes, expected at least %llx\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset,
-                    tp.item->size, sizeof(EXTENT_DATA));
-                free_traverse_ptr(&tp);
-                free_fcb(fcb);
-                Status = STATUS_INTERNAL_ERROR;
-                goto exit;
-            }
+        if (options & FILE_NON_DIRECTORY_FILE && fileref->fcb->type == BTRFS_TYPE_DIRECTORY) {
+            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+            free_fileref(fileref);
+            ExReleaseResourceLite(&Vcb->fcb_lock);
             
-            ed = (EXTENT_DATA*)tp.item->data;
-            
-            if (ed->type == EXTENT_TYPE_INLINE)
-                fcb->Header.AllocationSize.QuadPart = fcb->inode_item.st_size;
-            else
-                fcb->Header.AllocationSize.QuadPart = sector_align(fcb->inode_item.st_size, fcb->Vcb->superblock.sector_size);
-            
-            fcb->Header.FileSize.QuadPart = fcb->inode_item.st_size;
-            fcb->Header.ValidDataLength.QuadPart = fcb->inode_item.st_size;
-            
-            free_traverse_ptr(&tp);
-        }
-    
-        if (options & FILE_NON_DIRECTORY_FILE && fcb->type == BTRFS_TYPE_DIRECTORY) {
-            free_fcb(fcb);
             Status = STATUS_FILE_IS_A_DIRECTORY;
             goto exit;
-        } else if (options & FILE_DIRECTORY_FILE && fcb->type != BTRFS_TYPE_DIRECTORY) {
-            TRACE("returning STATUS_NOT_A_DIRECTORY (type = %u, path = %.*S)\n", fcb->type, fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
-            free_fcb(fcb);
+        } else if (options & FILE_DIRECTORY_FILE && fileref->fcb->type != BTRFS_TYPE_DIRECTORY) {
+            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;
         }
     
-        Status = attach_fcb_to_fileobject(Vcb, fcb, FileObject);
-        
-        if (options & FILE_DELETE_ON_CLOSE)
-            fcb->delete_on_close = TRUE;
+        FileObject->FsContext = fileref->fcb;
         
         ccb = ExAllocatePoolWithTag(NonPagedPool, sizeof(*ccb), ALLOC_TAG);
         if (!ccb) {
             ERR("out of memory\n");
-            free_fcb(fcb);
+            
+            ExAcquireResourceExclusiveLite(&Vcb->fcb_lock, TRUE);
+            free_fileref(fileref);
+            ExReleaseResourceLite(&Vcb->fcb_lock);
+
             Status = STATUS_INSUFFICIENT_RESOURCES;
             goto exit;
         }
         
         RtlZeroMemory(ccb, sizeof(*ccb));
+        
         ccb->NodeType = BTRFS_NODE_TYPE_CCB;
         ccb->NodeSize = sizeof(ccb);
         ccb->disposition = RequestedDisposition;
@@ -2212,10 +2659,13 @@ static NTSTATUS STDCALL create_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_
         RtlInitUnicodeString(&ccb->query_string, NULL);
         ccb->has_wildcard = FALSE;
         ccb->specific_file = FALSE;
+        ccb->access = access;
+        
+        ccb->fileref = fileref;
         
         FileObject->FsContext2 = ccb;
         
-        FileObject->SectionObjectPointer = &fcb->nonpaged->segment_object;
+        FileObject->SectionObjectPointer = &fileref->fcb->nonpaged->segment_object;
         
         if (NT_SUCCESS(Status)) {
             switch (RequestedDisposition) {
@@ -2235,16 +2685,15 @@ static NTSTATUS STDCALL create_file(PDEVICE_OBJECT DeviceObject, PIRP Irp, LIST_
             }
         }
         
-        oc = InterlockedIncrement(&fcb->open_count);
 #ifdef DEBUG_FCB_REFCOUNTS
-        ERR("fcb %p: open_count now %i\n", fcb, oc);
+        oc = InterlockedIncrement(&fileref->fcb->open_count);
+        ERR("fcb %p: open_count now %i\n", fileref->fcb, oc);
+#else
+        InterlockedIncrement(&fileref->fcb->open_count);
 #endif
     } else {
         Status = file_create(Irp, DeviceObject->DeviceExtension, FileObject, &FileObject->FileName, RequestedDisposition, options, rollback);
         Irp->IoStatus.Information = NT_SUCCESS(Status) ? FILE_CREATED : 0;
-        
-//         if (!NT_SUCCESS(Status))
-//             free_fcb(fcb);
     }
     
     if (NT_SUCCESS(Status) && !(options & FILE_NO_INTERMEDIATE_BUFFERING))
@@ -2337,7 +2786,9 @@ NTSTATUS STDCALL drv_create(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
     if (IrpSp->FileObject->FileName.Length == 0 && !IrpSp->FileObject->RelatedFileObject) {
         ULONG RequestedDisposition = ((IrpSp->Parameters.Create.Options >> 24) & 0xff);
         ULONG RequestedOptions = IrpSp->Parameters.Create.Options & FILE_VALID_OPTION_FLAGS;
+#ifdef DEBUG_FCB_REFCOUNTS
         LONG rc, oc;
+#endif
         
         TRACE("open operation for volume\n");
 
@@ -2354,44 +2805,45 @@ NTSTATUS STDCALL drv_create(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
             goto exit;
         }
 
+#ifdef DEBUG_FCB_REFCOUNTS
         rc = InterlockedIncrement(&Vcb->volume_fcb->refcount);
         oc = InterlockedIncrement(&Vcb->volume_fcb->open_count);
-#ifdef DEBUG_FCB_REFCOUNTS
         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
-        attach_fcb_to_fileobject(Vcb, Vcb->volume_fcb, IrpSp->FileObject);
-// //         NtfsAttachFCBToFileObject(DeviceExt, DeviceExt->VolumeFcb, FileObject);
-// //         DeviceExt->VolumeFcb->RefCount++;
+        IrpSp->FileObject->FsContext = Vcb->volume_fcb;
         
         IrpSp->FileObject->SectionObjectPointer = &Vcb->volume_fcb->nonpaged->segment_object;
 
         Irp->IoStatus.Information = FILE_OPENED;
         Status = STATUS_SUCCESS;
     } else {
-        BOOL exclusive;
+        BOOL exclusive, skip_lock;
         ULONG disposition;
         
         TRACE("file name: %.*S\n", IrpSp->FileObject->FileName.Length / sizeof(WCHAR), IrpSp->FileObject->FileName.Buffer);
         
-        if (IrpSp->FileObject->RelatedFileObject) {
-            fcb* relfcb = IrpSp->FileObject->RelatedFileObject->FsContext;
-            
-            if (relfcb)
-                TRACE("related file name = %.*S\n", relfcb->full_filename.Length / sizeof(WCHAR), relfcb->full_filename.Buffer);
-        }
+        if (IrpSp->FileObject->RelatedFileObject)
+            TRACE("related file = %S\n", file_desc(IrpSp->FileObject->RelatedFileObject));
         
         disposition = ((IrpSp->Parameters.Create.Options >> 24) & 0xff);
         
         // We acquire the lock exclusively if there's the possibility we might be writing
         exclusive = disposition != FILE_OPEN;
+        
+        // Don't lock again if we're being called from within CcCopyRead etc.
+        skip_lock = ExIsResourceAcquiredExclusiveLite(&Vcb->tree_lock);
 
-        acquire_tree_lock(Vcb, exclusive); 
+        if (!skip_lock)
+            acquire_tree_lock(Vcb, exclusive); 
         
 //         ExAcquireResourceExclusiveLite(&Vpb->DirResource, TRUE);
     //     Status = NtfsCreateFile(DeviceObject,
     //                             Irp);
-        Status = create_file(DeviceObject, Irp, &rollback);
+        Status = open_file(DeviceObject, Irp, &rollback);
 //         ExReleaseResourceLite(&Vpb->DirResource);
         
         if (exclusive && !NT_SUCCESS(Status))
@@ -2399,7 +2851,8 @@ NTSTATUS STDCALL drv_create(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
         else
             clear_rollback(&rollback);
         
-        release_tree_lock(Vcb, exclusive);
+        if (!skip_lock)
+            release_tree_lock(Vcb, exclusive);
         
 //         Status = STATUS_ACCESS_DENIED;
     }
index 9f062a0..30bca36 100644 (file)
@@ -31,7 +31,59 @@ typedef struct {
     enum DirEntryType dir_entry_type;
 } dir_entry;
 
-static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp, dir_entry* de, root* r) {
+ULONG STDCALL get_reparse_tag(device_extension* Vcb, root* subvol, UINT64 inode, UINT8 type) {
+    ULONG att, tag, br;
+    NTSTATUS Status;
+    
+    // FIXME - will this slow things down?
+    
+    if (type == BTRFS_TYPE_SYMLINK)
+        return IO_REPARSE_TAG_SYMLINK;
+    
+    if (type != BTRFS_TYPE_FILE && type != BTRFS_TYPE_DIRECTORY)
+        return 0;
+    
+    att = get_file_attributes(Vcb, NULL, subvol, inode, type, FALSE, FALSE);
+    
+    if (!(att & FILE_ATTRIBUTE_REPARSE_POINT))
+        return 0;
+    
+    if (type == BTRFS_TYPE_DIRECTORY) {
+        UINT8* data;
+        UINT16 datalen;
+        
+        if (!get_xattr(Vcb, subvol, inode, EA_REPARSE, EA_REPARSE_HASH, &data, &datalen))
+            return 0;
+        
+        if (!data)
+            return 0;
+        
+        if (datalen < sizeof(ULONG)) {
+            ExFreePool(data);
+            return 0;
+        }
+        
+        RtlCopyMemory(&tag, data, sizeof(ULONG));
+        
+        ExFreePool(data);
+    } else {
+        // FIXME - see if file loaded and cached, and do CcCopyRead if it is
+
+        Status = read_file(Vcb, subvol, inode, (UINT8*)&tag, 0, sizeof(ULONG), &br);
+        
+        if (!NT_SUCCESS(Status)) {
+            ERR("read_file returned %08x\n", Status);
+            return 0;
+        }
+        
+        if (br < sizeof(ULONG))
+            return 0;
+    }
+    
+    return tag;
+}
+
+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;
     UINT64 inode;
@@ -43,9 +95,21 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp,
     IrpSp = IoGetCurrentIrpStackLocation(Irp);
     
     if (de->key.obj_type == TYPE_ROOT_ITEM) { // subvol
-        r = fcb->Vcb->roots;
-        while (r && r->id != de->key.obj_id)
-            r = r->next;
+        LIST_ENTRY* le;
+        
+        r = NULL;
+        
+        le = fcb->Vcb->roots.Flink;
+        while (le != &fcb->Vcb->roots) {
+            root* subvol = CONTAINING_RECORD(le, root, list_entry);
+            
+            if (subvol->id == de->key.obj_id) {
+                r = subvol;
+                break;
+            }
+            
+            le = le->Flink;
+        }
         
         if (!r) {
             ERR("could not find root %llx\n", de->key.obj_id);
@@ -64,17 +128,23 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp,
                 LIST_ENTRY* le;
                 BOOL found = FALSE;
                 
-                le = fcb->children.Flink;
-                while (le != &fcb->children) {
-                    struct _fcb* c = CONTAINING_RECORD(le, struct _fcb, list_entry);
+                if (fileref) {
+                    ExAcquireResourceSharedLite(&fcb->Vcb->fcb_lock, TRUE);
                     
-                    if (c->subvol == r && c->inode == inode) {
-                        ii = c->inode_item;
-                        found = TRUE;
-                        break;
+                    le = fileref->children.Flink;
+                    while (le != &fileref->children) {
+                        file_ref* c = CONTAINING_RECORD(le, file_ref, list_entry);
+                        
+                        if (c->fcb->subvol == r && c->fcb->inode == inode && !c->fcb->ads) {
+                            ii = c->fcb->inode_item;
+                            found = TRUE;
+                            break;
+                        }
+                        
+                        le = le->Flink;
                     }
                     
-                    le = le->Flink;
+                    ExReleaseResourceLite(&fcb->Vcb->fcb_lock);
                 }
                 
                 if (!found) {
@@ -93,7 +163,6 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp,
                     
                     if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
                         ERR("could not find inode item for inode %llx in root %llx\n", inode, r->id);
-                        free_traverse_ptr(&tp);
                         return STATUS_INTERNAL_ERROR;
                     }
                     
@@ -101,8 +170,6 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp,
                     
                     if (tp.item->size > 0)
                         RtlCopyMemory(&ii, tp.item->data, min(sizeof(INODE_ITEM), tp.item->size));
-                    
-                    free_traverse_ptr(&tp);
                 }
                 
                 break;
@@ -115,9 +182,14 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp,
                 break;
                 
             case DirEntryType_Parent:
-                ii = fcb->par->inode_item;
-                r = fcb->par->subvol;
-                inode = fcb->par->inode;
+                if (fileref && fileref->parent) {
+                    ii = fileref->parent->fcb->inode_item;
+                    r = fileref->parent->fcb->subvol;
+                    inode = fileref->parent->fcb->inode;
+                } else {
+                    ERR("no fileref\n");
+                    return STATUS_INTERNAL_ERROR;
+                }
                 break;
         }
     }
@@ -162,7 +234,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp,
             fbdi->AllocationSize.QuadPart = de->type == BTRFS_TYPE_SYMLINK ? 0 : ii.st_blocks;
             fbdi->FileAttributes = get_file_attributes(fcb->Vcb, &ii, r, inode, de->type, dotfile, FALSE);
             fbdi->FileNameLength = stringlen;
-            fbdi->EaSize = de->type == BTRFS_TYPE_SYMLINK ? IO_REPARSE_TAG_SYMLINK : 0;
+            fbdi->EaSize = get_reparse_tag(fcb->Vcb, r, inode, de->type);
             fbdi->ShortNameLength = 0;
 //             fibdi->ShortName[12];
             
@@ -237,7 +309,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp,
             ffdi->AllocationSize.QuadPart = de->type == BTRFS_TYPE_SYMLINK ? 0 : ii.st_blocks;
             ffdi->FileAttributes = get_file_attributes(fcb->Vcb, &ii, r, inode, de->type, dotfile, FALSE);
             ffdi->FileNameLength = stringlen;
-            ffdi->EaSize = de->type == BTRFS_TYPE_SYMLINK ? IO_REPARSE_TAG_SYMLINK : 0;
+            ffdi->EaSize = get_reparse_tag(fcb->Vcb, r, inode, de->type);
             
             Status = RtlUTF8ToUnicodeN(ffdi->FileName, stringlen, &stringlen, de->name, de->namelen);
 
@@ -277,7 +349,7 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp,
             fibdi->AllocationSize.QuadPart = de->type == BTRFS_TYPE_SYMLINK ? 0 : ii.st_blocks;
             fibdi->FileAttributes = get_file_attributes(fcb->Vcb, &ii, r, inode, de->type, dotfile, FALSE);
             fibdi->FileNameLength = stringlen;
-            fibdi->EaSize = de->type == BTRFS_TYPE_SYMLINK ? IO_REPARSE_TAG_SYMLINK : 0;
+            fibdi->EaSize = get_reparse_tag(fcb->Vcb, r, inode, de->type);
             fibdi->ShortNameLength = 0;
 //             fibdi->ShortName[12];
             fibdi->FileId.QuadPart = inode;
@@ -347,13 +419,13 @@ static NTSTATUS STDCALL query_dir_item(fcb* fcb, void* buf, LONG* len, PIRP Irp,
     return STATUS_NO_MORE_FILES;
 }
 
-static NTSTATUS STDCALL next_dir_entry(fcb* fcb, UINT64* offset, dir_entry* de, traverse_ptr* tp) {
+static NTSTATUS STDCALL next_dir_entry(fcb* fcb, file_ref* fileref, UINT64* offset, dir_entry* de, traverse_ptr* tp) {
     KEY searchkey;
     traverse_ptr next_tp;
     DIR_ITEM* di;
     NTSTATUS Status;
     
-    if (fcb->par) { // don't return . and .. if root directory
+    if (fileref && fileref->parent) { // don't return . and .. if root directory
         if (*offset == 0) {
             de->key.obj_id = fcb->inode;
             de->key.obj_type = TYPE_INODE_ITEM;
@@ -367,7 +439,7 @@ static NTSTATUS STDCALL next_dir_entry(fcb* fcb, UINT64* offset, dir_entry* de,
             
             return STATUS_SUCCESS;
         } else if (*offset == 1) {
-            de->key.obj_id = fcb->par->inode;
+            de->key.obj_id = fileref->parent->fcb->inode;
             de->key.obj_type = TYPE_INODE_ITEM;
             de->key.offset = 0;
             de->dir_entry_type = DirEntryType_Parent;
@@ -389,7 +461,6 @@ static NTSTATUS STDCALL next_dir_entry(fcb* fcb, UINT64* offset, dir_entry* de,
         Status = find_item(fcb->Vcb, fcb->subvol, tp, &searchkey, FALSE);
         if (!NT_SUCCESS(Status)) {
             ERR("error - find_item returned %08x\n", Status);
-            free_traverse_ptr(tp);
             tp->tree = NULL;
             return Status;
         }
@@ -398,7 +469,6 @@ static NTSTATUS STDCALL next_dir_entry(fcb* fcb, UINT64* offset, dir_entry* de,
         
         if (keycmp(&tp->item->key, &searchkey) == -1) {
             if (find_next_item(fcb->Vcb, tp, &next_tp, FALSE)) {
-                free_traverse_ptr(tp);
                 *tp = next_tp;
                 
                 TRACE("moving on to %llx,%x,%llx\n", tp->item->key.obj_id, tp->item->key.obj_type, tp->item->key.offset);
@@ -406,7 +476,6 @@ static NTSTATUS STDCALL next_dir_entry(fcb* fcb, UINT64* offset, dir_entry* de,
         }
         
         if (tp->item->key.obj_id != searchkey.obj_id || tp->item->key.obj_type != searchkey.obj_type || tp->item->key.offset < *offset) {
-            free_traverse_ptr(tp);
             tp->tree = NULL;
             return STATUS_NO_MORE_FILES;
         }
@@ -418,7 +487,6 @@ static NTSTATUS STDCALL next_dir_entry(fcb* fcb, UINT64* offset, dir_entry* de,
         if (tp->item->size < sizeof(DIR_ITEM) || tp->item->size < sizeof(DIR_ITEM) - 1 + di->m + di->n) {
             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));
             
-            free_traverse_ptr(tp);
             tp->tree = NULL;
             return STATUS_INTERNAL_ERROR;
         }
@@ -433,7 +501,6 @@ static NTSTATUS STDCALL next_dir_entry(fcb* fcb, UINT64* offset, dir_entry* de,
     } else {
         if (find_next_item(fcb->Vcb, tp, &next_tp, FALSE)) {
             if (next_tp.item->key.obj_type == TYPE_DIR_INDEX && next_tp.item->key.obj_id == tp->item->key.obj_id) {
-                free_traverse_ptr(tp);
                 *tp = next_tp;
                 
                 *offset = tp->item->key.offset + 1;
@@ -442,8 +509,6 @@ static NTSTATUS STDCALL next_dir_entry(fcb* fcb, UINT64* offset, dir_entry* de,
                 
                 if (tp->item->size < sizeof(DIR_ITEM) || tp->item->size < sizeof(DIR_ITEM) - 1 + di->m + di->n) {
                     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));
-                    
-                    free_traverse_ptr(&next_tp);
                     return STATUS_INTERNAL_ERROR;
                 }
         
@@ -454,10 +519,8 @@ static NTSTATUS STDCALL next_dir_entry(fcb* fcb, UINT64* offset, dir_entry* de,
                 de->dir_entry_type = DirEntryType_File;
                 
                 return STATUS_SUCCESS;
-            } else {
-                free_traverse_ptr(&next_tp);
+            } else
                 return STATUS_NO_MORE_FILES;
-            }
         } else
             return STATUS_NO_MORE_FILES;
     }
@@ -468,11 +531,12 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     NTSTATUS Status, status2;
     fcb* fcb;
     ccb* ccb;
+    file_ref* fileref;
     void* buf;
     UINT8 *curitem, *lastitem;
     LONG length;
     ULONG count;
-    BOOL has_wildcard = FALSE, specific_file = FALSE;
+    BOOL has_wildcard = FALSE, specific_file = FALSE, initial;
 //     UINT64 num_reads_orig;
     traverse_ptr tp;
     dir_entry de;
@@ -486,10 +550,11 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     IrpSp = IoGetCurrentIrpStackLocation(Irp);
     fcb = IrpSp->FileObject->FsContext;
     ccb = IrpSp->FileObject->FsContext2;
+    fileref = ccb ? ccb->fileref : NULL;
     
     acquire_tree_lock(fcb->Vcb, FALSE);
     
-    TRACE("%.*S\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
+    TRACE("%S\n", file_desc(IrpSp->FileObject));
     
     if (IrpSp->Flags == 0) {
         TRACE("QD flags: (none)\n");
@@ -517,6 +582,8 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
             TRACE("    unknown flags: %u\n", flags);
     }
     
+    initial = !ccb->query_string.Buffer;
+    
     if (IrpSp->Flags & SL_RESTART_SCAN) {
         ccb->query_dir_offset = 0;
         
@@ -567,6 +634,9 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     } else {
         has_wildcard = ccb->has_wildcard;
         specific_file = ccb->specific_file;
+        
+        if (!(IrpSp->Flags & SL_RESTART_SCAN))
+            initial = FALSE;
     }
     
     if (ccb->query_string.Buffer) {
@@ -574,10 +644,10 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     }
     
     tp.tree = NULL;
-    Status = next_dir_entry(fcb, &ccb->query_dir_offset, &de, &tp);
+    Status = next_dir_entry(fcb, fileref, &ccb->query_dir_offset, &de, &tp);
     
     if (!NT_SUCCESS(Status)) {
-        if (Status == STATUS_NO_MORE_FILES && IrpSp->Flags & SL_RETURN_SINGLE_ENTRY)
+        if (Status == STATUS_NO_MORE_FILES && initial)
             Status = STATUS_NO_SUCH_FILE;
         goto end;
     }
@@ -604,7 +674,6 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     if (Irp->MdlAddress && !buf) {
         ERR("MmGetSystemAddressForMdlSafe returned NULL\n");
         Status = STATUS_INSUFFICIENT_RESOURCES;
-        if (tp.tree) free_traverse_ptr(&tp);
         goto end;
     }
     
@@ -619,14 +688,12 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         Status = RtlUTF8ToUnicodeN(NULL, 0, &stringlen, de.name, de.namelen);
         if (!NT_SUCCESS(Status)) {
             ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
-            if (tp.tree) free_traverse_ptr(&tp);
             goto end;
         }
         
         uni_fn = ExAllocatePoolWithTag(PagedPool, stringlen, ALLOC_TAG);
         if (!uni_fn) {
             ERR("out of memory\n");
-            if (tp.tree) free_traverse_ptr(&tp);
             Status = STATUS_INSUFFICIENT_RESOURCES;
             goto end;
         }
@@ -635,7 +702,6 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         
         if (!NT_SUCCESS(Status)) {
             ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
-            if (tp.tree) free_traverse_ptr(&tp);
             goto end;
         }
         
@@ -643,21 +709,19 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
         di_uni_fn.Buffer = uni_fn;
         
         while (!FsRtlIsNameInExpression(&ccb->query_string, &di_uni_fn, TRUE, NULL)) {
-            Status = next_dir_entry(fcb, &ccb->query_dir_offset, &de, &tp);
+            Status = next_dir_entry(fcb, fileref, &ccb->query_dir_offset, &de, &tp);
             
             ExFreePool(uni_fn);
             if (NT_SUCCESS(Status)) {
                 Status = RtlUTF8ToUnicodeN(NULL, 0, &stringlen, de.name, de.namelen);
                 if (!NT_SUCCESS(Status)) {
                     ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
-                    if (tp.tree) free_traverse_ptr(&tp);
                     goto end;
                 }
                 
                 uni_fn = ExAllocatePoolWithTag(PagedPool, stringlen, ALLOC_TAG);
                 if (!uni_fn) {
                     ERR("out of memory\n");
-                    if (tp.tree) free_traverse_ptr(&tp);
                     Status = STATUS_INSUFFICIENT_RESOURCES;
                     goto end;
                 }
@@ -667,16 +731,13 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
                 if (!NT_SUCCESS(Status)) {
                     ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
                     ExFreePool(uni_fn);
-                    if (tp.tree) free_traverse_ptr(&tp);
                     goto end;
                 }
                 
                 di_uni_fn.Length = di_uni_fn.MaximumLength = stringlen;
                 di_uni_fn.Buffer = uni_fn;
             } else {
-                if (tp.tree) free_traverse_ptr(&tp);
-
-                if (Status == STATUS_NO_MORE_FILES && IrpSp->Flags & SL_RETURN_SINGLE_ENTRY)
+                if (Status == STATUS_NO_MORE_FILES && initial)
                     Status = STATUS_NO_SUCH_FILE;
                 
                 goto end;
@@ -689,7 +750,7 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     TRACE("file(0) = %.*s\n", de.namelen, de.name);
     TRACE("offset = %u\n", ccb->query_dir_offset - 1);
 
-    Status = query_dir_item(fcb, buf, &length, Irp, &de, fcb->subvol);
+    Status = query_dir_item(fcb, fileref, buf, &length, Irp, &de, fcb->subvol);
     
     count = 0;
     if (NT_SUCCESS(Status) && !(IrpSp->Flags & SL_RETURN_SINGLE_ENTRY) && !specific_file) {
@@ -717,7 +778,7 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
                 WCHAR* uni_fn = NULL;
                 UNICODE_STRING di_uni_fn;
                 
-                Status = next_dir_entry(fcb, &ccb->query_dir_offset, &de, &tp);
+                Status = next_dir_entry(fcb, fileref, &ccb->query_dir_offset, &de, &tp);
                 if (NT_SUCCESS(Status)) {
                     if (has_wildcard) {
                         ULONG stringlen;
@@ -725,14 +786,12 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
                         Status = RtlUTF8ToUnicodeN(NULL, 0, &stringlen, de.name, de.namelen);
                         if (!NT_SUCCESS(Status)) {
                             ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
-                            if (tp.tree) free_traverse_ptr(&tp);
                             goto end;
                         }
                         
                         uni_fn = ExAllocatePoolWithTag(PagedPool, stringlen, ALLOC_TAG);
                         if (!uni_fn) {
                             ERR("out of memory\n");
-                            if (tp.tree) free_traverse_ptr(&tp);
                             Status = STATUS_INSUFFICIENT_RESOURCES;
                             goto end;
                         }
@@ -742,7 +801,6 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
                         if (!NT_SUCCESS(Status)) {
                             ERR("RtlUTF8ToUnicodeN returned %08x\n", Status);
                             ExFreePool(uni_fn);
-                            if (tp.tree) free_traverse_ptr(&tp);
                             goto end;
                         }
                         
@@ -757,7 +815,7 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
                         TRACE("file(%u) %u = %.*s\n", count, curitem - (UINT8*)buf, de.namelen, de.name);
                         TRACE("offset = %u\n", ccb->query_dir_offset - 1);
                         
-                        status2 = query_dir_item(fcb, curitem, &length, Irp, &de, fcb->subvol);
+                        status2 = query_dir_item(fcb, fileref, curitem, &length, Irp, &de, fcb->subvol);
                         
                         if (NT_SUCCESS(status2)) {
                             ULONG* lastoffset = (ULONG*)lastitem;
@@ -789,8 +847,6 @@ static NTSTATUS STDCALL query_directory(PDEVICE_OBJECT DeviceObject, PIRP Irp) {
     
     Irp->IoStatus.Information = IrpSp->Parameters.QueryDirectory.Length - length;
     
-    if (tp.tree) free_traverse_ptr(&tp);
-    
 end:
     release_tree_lock(fcb->Vcb, FALSE);
     
@@ -804,11 +860,18 @@ static NTSTATUS STDCALL notify_change_directory(device_extension* Vcb, PIRP Irp)
     PIO_STACK_LOCATION IrpSp = IoGetCurrentIrpStackLocation(Irp);
     PFILE_OBJECT FileObject = IrpSp->FileObject;
     fcb* fcb = FileObject->FsContext;
+    ccb* ccb = FileObject->FsContext2;
+    file_ref* fileref = ccb->fileref;
     NTSTATUS Status;
 //     WCHAR fn[MAX_PATH];
     
     TRACE("IRP_MN_NOTIFY_CHANGE_DIRECTORY\n");
     
+    if (!fileref) {
+        ERR("no fileref\n");
+        return STATUS_INVALID_PARAMETER;
+    }
+    
     acquire_tree_lock(fcb->Vcb, FALSE);
     
     if (fcb->type != BTRFS_TYPE_DIRECTORY) {
@@ -818,9 +881,9 @@ static NTSTATUS STDCALL notify_change_directory(device_extension* Vcb, PIRP Irp)
     
     // FIXME - raise exception if FCB marked for deletion?
     
-    TRACE("%.*S\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer);
+    TRACE("%S\n", file_desc(FileObject));
     
-    FsRtlNotifyFullChangeDirectory(Vcb->NotifySync, &Vcb->DirNotifyList, FileObject->FsContext2, (PSTRING)&fcb->full_filename,
+    FsRtlNotifyFullChangeDirectory(Vcb->NotifySync, &Vcb->DirNotifyList, FileObject->FsContext2, (PSTRING)&fileref->full_filename,
         IrpSp->Flags & SL_WATCH_TREE, FALSE, IrpSp->Parameters.NotifyDirectory.CompletionFilter, Irp, NULL, NULL);
     
     Status = STATUS_PENDING;
@@ -867,6 +930,10 @@ NTSTATUS STDCALL drv_directory_control(IN PDEVICE_OBJECT DeviceObject, IN PIRP I
 
     if (func != IRP_MN_NOTIFY_CHANGE_DIRECTORY || Status != STATUS_PENDING) {
         Irp->IoStatus.Status = Status;
+        
+        if (Irp->UserIosb)
+            *Irp->UserIosb = Irp->IoStatus;
+        
         IoCompleteRequest( Irp, IO_DISK_INCREMENT );
     }
     
diff --git a/reactos/drivers/filesystems/btrfs/extent-tree.c b/reactos/drivers/filesystems/btrfs/extent-tree.c
new file mode 100644 (file)
index 0000000..2699301
--- /dev/null
@@ -0,0 +1,1239 @@
+/* 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"
+
+static __inline ULONG get_extent_data_len(UINT8 type) {
+    switch (type) {
+        case TYPE_TREE_BLOCK_REF:
+            return sizeof(TREE_BLOCK_REF);
+            
+        case TYPE_EXTENT_DATA_REF:
+            return sizeof(EXTENT_DATA_REF);
+            
+        // FIXME - TYPE_EXTENT_REF_V0
+        // FIXME - TYPE_SHARED_BLOCK_REF
+            
+        case TYPE_SHARED_DATA_REF:
+            return sizeof(SHARED_DATA_REF);
+            
+        default:
+            return 0;
+    }
+}
+
+static __inline UINT64 get_extent_data_refcount(UINT8 type, void* data) {
+    switch (type) {
+        case TYPE_TREE_BLOCK_REF:
+            return 1;
+            
+        case TYPE_EXTENT_DATA_REF:
+        {
+            EXTENT_DATA_REF* edr = (EXTENT_DATA_REF*)data;
+            return edr->count;
+        }
+        
+        // FIXME - TYPE_EXTENT_REF_V0
+        // FIXME - TYPE_SHARED_BLOCK_REF
+        
+        case TYPE_SHARED_DATA_REF:
+        {
+            SHARED_DATA_REF* sdr = (SHARED_DATA_REF*)data;
+            return sdr->count;
+        }
+            
+        default:
+            return 0;
+    }
+}
+
+static UINT64 get_extent_data_ref_hash(EXTENT_DATA_REF* edr) {
+    UINT32 high_crc = 0xffffffff, low_crc = 0xffffffff;
+
+    high_crc = calc_crc32c(high_crc, (UINT8*)&edr->root, sizeof(UINT64));
+    low_crc = calc_crc32c(low_crc, (UINT8*)&edr->objid, sizeof(UINT64));
+    low_crc = calc_crc32c(low_crc, (UINT8*)&edr->offset, sizeof(UINT64));
+    
+    return ((UINT64)high_crc << 31) ^ (UINT64)low_crc;
+}
+
+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 {
+        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, 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;
+    
+    // FIXME - handle A9s
+    
+    if (datalen == 0) {
+        ERR("unrecognized extent type %x\n", type);
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = TYPE_EXTENT_ITEM;
+    searchkey.offset = 0xffffffffffffffff;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE);
+    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 != 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 (is_tree) {
+            EXTENT_ITEM2* ei2 = (EXTENT_ITEM2*)ptr;
+            ei2->firstitem = *firstitem;
+            ei2->level = level;
+            ptr = (UINT8*)&ei2[1];
+        }
+        
+        *ptr = type;
+        RtlCopyMemory(ptr + 1, data, datalen);
+        
+        if (!insert_tree_item(Vcb, Vcb->extent_root, address, TYPE_EXTENT_ITEM, size, ei, eisize, NULL, rollback)) {
+            ERR("insert_tree_item failed\n");
+            return STATUS_INTERNAL_ERROR;
+        }
+        
+        // FIXME - add to space list?
+
+        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;
+    } else if (tp.item->size == sizeof(EXTENT_ITEM_V0)) {
+        TRACE("converting old-style extent at (%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
+        
+        Status = convert_old_data_extent(Vcb, address, size, rollback);
+        if (!NT_SUCCESS(Status)) {
+            ERR("convert_old_data_extent returned %08x\n", Status);
+            return Status;
+        }
+        
+        return increase_extent_refcount(Vcb, address, size, type, data, firstitem, level, rollback);
+    } else 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) {
+        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 (secttype == TYPE_SHARED_DATA_REF) {
+            TRACE("found shared data extent at %llx, converting\n", tp.item->key.obj_id);
+            
+            Status = convert_shared_data_extent(Vcb, address, size, rollback);
+            if (!NT_SUCCESS(Status)) {
+                ERR("convert_shared_data_extent returned %08x\n", Status);
+                return Status;
+            }
+            
+            return increase_extent_refcount(Vcb, address, size, type, data, firstitem, level, rollback);
+        }
+        
+        // If inline extent already present, increase refcount and return
+        
+        if (secttype == type) {
+            if (type == TYPE_EXTENT_DATA_REF) {
+                EXTENT_DATA_REF* sectedr = (EXTENT_DATA_REF*)(ptr + sizeof(UINT8));
+                EXTENT_DATA_REF* edr = (EXTENT_DATA_REF*)data;
+                
+                if (sectedr->root == edr->root && sectedr->objid == edr->objid && sectedr->offset == edr->offset) {
+                    UINT32 rc = get_extent_data_refcount(type, data);
+                    EXTENT_DATA_REF* sectedr2;
+                    
+                    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;
+                    
+                    sectedr2 = (EXTENT_DATA_REF*)((UINT8*)newei + ((UINT8*)sectedr - tp.item->data));
+                    sectedr2->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, rollback)) {
+                        ERR("insert_tree_item failed\n");
+                        return STATUS_INTERNAL_ERROR;
+                    }
+                    
+                    return STATUS_SUCCESS;
+                }
+            } else if (type == TYPE_TREE_BLOCK_REF) {
+                ERR("trying to increase refcount of tree extent\n");
+                return STATUS_INTERNAL_ERROR;
+            } else {
+                ERR("unhandled extent type %x\n", type);
+                return STATUS_INTERNAL_ERROR;
+            }
+        }
+        
+        len -= sectlen;
+        ptr += sizeof(UINT8) + sectlen;
+        inline_rc += sectcount;
+    }
+    
+    offset = get_extent_hash(type, data);
+    
+    max_extent_item_size = (Vcb->superblock.node_size >> 4) - sizeof(leaf_node);
+    
+    // If we can, add entry as inline extent item
+    
+    if (inline_rc == ei->refcount && tp.item->size + sizeof(UINT8) + datalen < max_extent_item_size) {
+        len = tp.item->size - sizeof(EXTENT_ITEM);
+        ptr = (UINT8*)&ei[1];
+        
+        if (ei->flags & EXTENT_ITEM_TREE_BLOCK) {
+            len -= sizeof(EXTENT_ITEM2);
+            ptr += sizeof(EXTENT_ITEM2);
+        }
+
+        while (len > 0) {
+            UINT8 secttype = *ptr;
+            ULONG sectlen = get_extent_data_len(secttype);
+            
+            if (secttype > type)
+                break;
+            
+            len--;
+            
+            if (secttype == type) {
+                UINT64 sectoff = get_extent_hash(secttype, ptr + 1);
+                
+                if (sectoff > offset)
+                    break;
+            }
+            
+            len -= sectlen;
+            ptr += sizeof(UINT8) + sectlen;
+        }
+        
+        newei = ExAllocatePoolWithTag(PagedPool, tp.item->size + sizeof(UINT8) + datalen, ALLOC_TAG);
+        RtlCopyMemory(newei, tp.item->data, ptr - tp.item->data);
+        
+        newei->generation = Vcb->superblock.generation;
+        newei->refcount += get_extent_data_refcount(type, data);
+        
+        if (len > 0)
+            RtlCopyMemory((UINT8*)newei + (ptr - tp.item->data) + sizeof(UINT8) + datalen, ptr, len + 1);
+        
+        ptr = (ptr - tp.item->data) + (UINT8*)newei;
+        
+        *ptr = type;
+        RtlCopyMemory(ptr + 1, data, datalen);
+        
+        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 + sizeof(UINT8) + datalen, NULL, rollback)) {
+            ERR("insert_tree_item failed\n");
+            return STATUS_INTERNAL_ERROR;
+        }
+        
+        return STATUS_SUCCESS;
+    }
+    
+    // Look for existing non-inline entry, and increase refcount if found
+    
+    if (inline_rc != ei->refcount) {
+        traverse_ptr tp2;
+        
+        searchkey.obj_id = address;
+        searchkey.obj_type = type;
+        searchkey.offset = offset;
+        
+        Status = find_item(Vcb, Vcb->extent_root, &tp2, &searchkey, FALSE);
+        if (!NT_SUCCESS(Status)) {
+            ERR("error - find_item returned %08x\n", Status);
+            return Status;
+        }
+        
+        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;
+            }
+            
+            data2 = ExAllocatePoolWithTag(PagedPool, tp2.item->size, ALLOC_TAG);
+            RtlCopyMemory(data2, tp2.item->data, tp2.item->size);
+            
+            if (type == TYPE_EXTENT_DATA_REF) {
+                EXTENT_DATA_REF* edr = (EXTENT_DATA_REF*)data2;
+                
+                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;
+            } else {
+                ERR("unhandled extent type %x\n", type);
+                return STATUS_INTERNAL_ERROR;
+            }
+            
+            delete_tree_item(Vcb, &tp2, rollback);
+            
+            if (!insert_tree_item(Vcb, Vcb->extent_root, tp2.item->key.obj_id, tp2.item->key.obj_type, tp2.item->key.offset, data2, tp2.item->size, NULL, rollback)) {
+                ERR("insert_tree_item failed\n");
+                return STATUS_INTERNAL_ERROR;
+            }
+            
+            newei = ExAllocatePoolWithTag(PagedPool, tp.item->size, ALLOC_TAG);
+            RtlCopyMemory(newei, tp.item->data, tp.item->size);
+            
+            newei->generation = Vcb->superblock.generation;
+            newei->refcount += get_extent_data_refcount(type, data);
+            
+            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, rollback)) {
+                ERR("insert_tree_item failed\n");
+                return STATUS_INTERNAL_ERROR;
+            }
+            
+            return STATUS_SUCCESS;
+        }
+    }
+    
+    // Otherwise, add new non-inline entry
+    
+    data2 = ExAllocatePoolWithTag(PagedPool, datalen, ALLOC_TAG);
+    RtlCopyMemory(data2, data, datalen);
+    
+    if (!insert_tree_item(Vcb, Vcb->extent_root, address, type, offset, data2, datalen, NULL, rollback)) {
+        ERR("insert_tree_item failed\n");
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    newei = ExAllocatePoolWithTag(PagedPool, tp.item->size, ALLOC_TAG);
+    RtlCopyMemory(newei, tp.item->data, tp.item->size);
+    
+    newei->generation = Vcb->superblock.generation;
+    newei->refcount += get_extent_data_refcount(type, data);
+    
+    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, rollback)) {
+        ERR("insert_tree_item failed\n");
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    return STATUS_SUCCESS;
+}
+
+NTSTATUS increase_extent_refcount_data(device_extension* Vcb, UINT64 address, UINT64 size, root* subvol, UINT64 inode, UINT64 offset, UINT32 refcount, LIST_ENTRY* rollback) {
+    EXTENT_DATA_REF edr;
+    
+    edr.root = subvol->id;
+    edr.objid = inode;
+    edr.offset = offset;
+    edr.count = refcount;
+    
+    return increase_extent_refcount(Vcb, address, size, TYPE_EXTENT_DATA_REF, &edr, NULL, 0, rollback);
+}
+
+void decrease_chunk_usage(chunk* c, UINT64 delta) {
+    c->used -= delta;
+    
+    TRACE("decreasing size of chunk %llx by %llx\n", c->offset, delta);
+}
+
+static NTSTATUS remove_extent(device_extension* Vcb, UINT64 address, UINT64 size, LIST_ENTRY* changed_sector_list) {
+    chunk* c;
+    LIST_ENTRY* le;
+    
+    if (changed_sector_list) {
+        changed_sector* sc = ExAllocatePoolWithTag(PagedPool, sizeof(changed_sector), ALLOC_TAG);
+        if (!sc) {
+            ERR("out of memory\n");
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+        
+        sc->ol.key = address;
+        sc->checksums = NULL;
+        sc->length = size / Vcb->superblock.sector_size;
+
+        sc->deleted = TRUE;
+        
+        insert_into_ordered_list(changed_sector_list, &sc->ol);
+    }
+    
+    c = NULL;
+    le = Vcb->chunks.Flink;
+    while (le != &Vcb->chunks) {
+        c = CONTAINING_RECORD(le, chunk, list_entry);
+        
+        if (address >= c->offset && address + size < c->offset + c->chunk_item->size)
+            break;
+        
+        le = le->Flink;
+    }
+    if (le == &Vcb->chunks) c = NULL;
+    
+    if (c) {
+        decrease_chunk_usage(c, size);
+        
+        add_to_space_list(c, address, size, SPACE_TYPE_DELETING);
+    }
+    
+    return STATUS_SUCCESS;
+}
+
+static NTSTATUS decrease_extent_refcount(device_extension* Vcb, UINT64 address, UINT64 size, UINT8 type, void* data, KEY* firstitem,
+                                         UINT8 level, LIST_ENTRY* changed_sector_list, LIST_ENTRY* rollback) {
+    KEY searchkey;
+    NTSTATUS Status;
+    traverse_ptr tp, tp2;
+    EXTENT_ITEM* ei;
+    ULONG len;
+    UINT64 inline_rc, offset;
+    UINT8* ptr;
+    UINT32 rc = get_extent_data_refcount(type, data);
+    ULONG datalen = get_extent_data_len(type);
+    
+    // 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);
+    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) {
+        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)) {
+        TRACE("converting old-style extent at (%llx,%x,%llx)\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
+        
+        Status = convert_old_data_extent(Vcb, address, size, rollback);
+        if (!NT_SUCCESS(Status)) {
+            ERR("convert_old_data_extent returned %08x\n", Status);
+            return Status;
+        }
+        
+        return decrease_extent_refcount(Vcb, address, size, type, data, firstitem, level, changed_sector_list, 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) {
+        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);
+    }
+    
+    if (ei->refcount < rc) {
+        ERR("error - extent has refcount %llx, trying to reduce by %x\n", ei->refcount, rc);
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    inline_rc = 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));
+        
+        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 (secttype == TYPE_SHARED_DATA_REF) {
+            TRACE("found shared data extent at %llx, converting\n", tp.item->key.obj_id);
+            
+            Status = convert_shared_data_extent(Vcb, address, size, rollback);
+            if (!NT_SUCCESS(Status)) {
+                ERR("convert_shared_data_extent returned %08x\n", Status);
+                return Status;
+            }
+            
+            return decrease_extent_refcount(Vcb, address, size, type, data, firstitem, level, changed_sector_list, rollback);
+        }
+        
+        if (secttype == type) {
+            if (type == TYPE_EXTENT_DATA_REF) {
+                EXTENT_DATA_REF* sectedr = (EXTENT_DATA_REF*)(ptr + sizeof(UINT8));
+                EXTENT_DATA_REF* edr = (EXTENT_DATA_REF*)data;
+                ULONG neweilen;
+                EXTENT_ITEM* newei;
+                
+                if (sectedr->root == edr->root && sectedr->objid == edr->objid && sectedr->offset == edr->offset) {
+                    if (ei->refcount == edr->count) {
+                        Status = remove_extent(Vcb, address, size, changed_sector_list);
+                        if (!NT_SUCCESS(Status)) {
+                            ERR("remove_extent returned %08x\n", Status);
+                            return Status;
+                        }
+                        
+                        delete_tree_item(Vcb, &tp, rollback);
+                        return STATUS_SUCCESS;
+                    }
+                    
+                    if (sectedr->count < edr->count) {
+                        ERR("error - extent section has refcount %x, trying to reduce by %x\n", sectedr->count, edr->count);
+                        return STATUS_INTERNAL_ERROR;
+                    }
+                    
+                    if (sectedr->count > edr->count)    // reduce section refcount
+                        neweilen = tp.item->size;
+                    else                                // remove section entirely
+                        neweilen = tp.item->size - sizeof(UINT8) - sectlen;
+                    
+                    newei = ExAllocatePoolWithTag(PagedPool, neweilen, ALLOC_TAG);
+                    if (!newei) {
+                        ERR("out of memory\n");
+                        return STATUS_INSUFFICIENT_RESOURCES;
+                    }
+                    
+                    if (sectedr->count > edr->count) {
+                        EXTENT_DATA_REF* newedr = (EXTENT_DATA_REF*)((UINT8*)newei + ((UINT8*)sectedr - tp.item->data));
+                        
+                        RtlCopyMemory(newei, ei, neweilen);
+                        
+                        newedr->count -= rc;
+                    } else {
+                        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 -= 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, neweilen, NULL, rollback)) {
+                        ERR("insert_tree_item failed\n");
+                        return STATUS_INTERNAL_ERROR;
+                    }
+                    
+                    return STATUS_SUCCESS;
+                }
+//             } else if (type == TYPE_TREE_BLOCK_REF) {
+//                 ERR("trying to increase refcount of tree extent\n");
+//                 return STATUS_INTERNAL_ERROR;
+            } else {
+                ERR("unhandled extent type %x\n", type);
+                return STATUS_INTERNAL_ERROR;
+            }
+        }
+        
+        len -= sectlen;
+        ptr += sizeof(UINT8) + sectlen;
+        inline_rc += sectcount;
+    }
+    
+    if (inline_rc == ei->refcount) {
+        ERR("entry not found in inline extent item for address %llx\n", address);
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    offset = get_extent_hash(type, data);
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = type;
+    searchkey.offset = offset;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp2, &searchkey, FALSE);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return Status;
+    }
+    
+    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;
+    }
+    
+    if (tp2.item->size < datalen) {
+        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, datalen);
+        return STATUS_INTERNAL_ERROR;
+    }
+    
+    if (type == TYPE_EXTENT_DATA_REF) {
+        EXTENT_DATA_REF* sectedr = (EXTENT_DATA_REF*)tp2.item->data;
+        EXTENT_DATA_REF* edr = (EXTENT_DATA_REF*)data;
+        EXTENT_ITEM* newei;
+        
+        if (sectedr->root == edr->root && sectedr->objid == edr->objid && sectedr->offset == edr->offset) {
+            if (ei->refcount == edr->count) {
+                Status = remove_extent(Vcb, address, size, changed_sector_list);
+                if (!NT_SUCCESS(Status)) {
+                    ERR("remove_extent returned %08x\n", Status);
+                    return Status;
+                }
+                
+                delete_tree_item(Vcb, &tp, rollback);
+                delete_tree_item(Vcb, &tp2, rollback);
+                return STATUS_SUCCESS;
+            }
+            
+            if (sectedr->count < edr->count) {
+                ERR("error - extent section has refcount %x, trying to reduce by %x\n", sectedr->count, edr->count);
+                return STATUS_INTERNAL_ERROR;
+            }
+            
+            delete_tree_item(Vcb, &tp2, rollback);
+            
+            if (sectedr->count > edr->count) {
+                EXTENT_DATA_REF* newedr = ExAllocatePoolWithTag(PagedPool, tp2.item->size, ALLOC_TAG);
+                
+                if (!newedr) {
+                    ERR("out of memory\n");
+                    return STATUS_INSUFFICIENT_RESOURCES;
+                }
+                
+                RtlCopyMemory(newedr, sectedr, tp2.item->size);
+                
+                newedr->count -= edr->count;
+                
+                if (!insert_tree_item(Vcb, Vcb->extent_root, tp2.item->key.obj_id, tp2.item->key.obj_type, tp2.item->key.offset, newedr, tp2.item->size, NULL, rollback)) {
+                    ERR("insert_tree_item failed\n");
+                    return STATUS_INTERNAL_ERROR;
+                }
+            }
+            
+            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, rollback)) {
+                ERR("insert_tree_item failed\n");
+                return STATUS_INTERNAL_ERROR;
+            }
+            
+            return STATUS_SUCCESS;
+        } else {
+            ERR("error - hash collision?\n");
+            return STATUS_INTERNAL_ERROR;
+        }
+//     } else if (type == TYPE_TREE_BLOCK_REF) {
+//         ERR("trying to increase refcount of tree extent\n");
+//         return STATUS_INTERNAL_ERROR;
+    } else {
+        ERR("unhandled extent type %x\n", type);
+        return STATUS_INTERNAL_ERROR;
+    }
+}
+
+NTSTATUS decrease_extent_refcount_data(device_extension* Vcb, UINT64 address, UINT64 size, root* subvol, UINT64 inode,
+                                       UINT64 offset, UINT32 refcount, LIST_ENTRY* changed_sector_list, LIST_ENTRY* rollback) {
+    EXTENT_DATA_REF edr;
+    
+    edr.root = subvol->id;
+    edr.objid = inode;
+    edr.offset = offset;
+    edr.count = refcount;
+    
+    return decrease_extent_refcount(Vcb, address, size, TYPE_EXTENT_DATA_REF, &edr, NULL, 0, changed_sector_list, 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);
+    }
+}
+
+static NTSTATUS add_data_extent_ref(LIST_ENTRY* extent_refs, UINT64 tree_id, UINT64 obj_id, UINT64 offset) {
+    extent_ref* er2;
+    EXTENT_DATA_REF* edr;
+    LIST_ENTRY* le;
+    
+    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_EXTENT_DATA_REF) {
+                edr = (EXTENT_DATA_REF*)er->data;
+                
+                if (edr->root == tree_id && edr->objid == obj_id && edr->offset == offset) {
+                    edr->count++;
+                    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;
+    }
+    
+    edr = ExAllocatePoolWithTag(PagedPool, sizeof(EXTENT_DATA_REF), ALLOC_TAG);
+    if (!edr) {
+        ERR("out of memory\n");
+        ExFreePool(er2);
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    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);
+    
+    return STATUS_SUCCESS;
+}
+
+static NTSTATUS construct_extent_item(device_extension* Vcb, UINT64 address, UINT64 size, UINT64 flags, LIST_ENTRY* extent_refs, 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;
+    
+    if (IsListEmpty(extent_refs)) {
+        WARN("no extent refs found\n");
+        return STATUS_SUCCESS;
+    }
+    
+    refcount = 0;
+    inline_len = sizeof(EXTENT_ITEM);
+    
+    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;
+            }
+        }
+        
+        le = next_le;
+    }
+    
+    ei = ExAllocatePoolWithTag(PagedPool, inline_len, ALLOC_TAG);
+    if (!ei) {
+        ERR("out of memory\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+    
+    ei->refcount = refcount;
+    ei->generation = Vcb->superblock.generation;
+    ei->flags = flags;
+    
+    // Do we need to sort the inline extent refs? The Linux driver doesn't seem to bother.
+    
+    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 (!all_inline && er == first_noninline)
+            break;
+        
+        *siptr = er->type;
+        siptr++;
+        
+        if (extlen > 0) {
+            RtlCopyMemory(siptr, er->data, extlen);
+            siptr += extlen;
+        }
+         
+        le = le->Flink;
+    }
+    
+    if (!insert_tree_item(Vcb, Vcb->extent_root, address, TYPE_EXTENT_ITEM, size, ei, inline_len, NULL, 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);
+            
+            if (!insert_tree_item(Vcb, Vcb->extent_root, address, er->type, er->hash, er->data, get_extent_data_len(er->type), NULL, rollback)) {
+                ERR("error - failed to insert item\n");
+                return STATUS_INTERNAL_ERROR;
+            }
+            
+            er->allocated = FALSE;
+            
+            le = le->Flink;
+        }
+    }
+    
+    return STATUS_SUCCESS;
+}
+
+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;
+    NTSTATUS Status;
+    
+    buf = ExAllocatePoolWithTag(PagedPool, Vcb->superblock.node_size, ALLOC_TAG);
+    if (!buf) {
+        ERR("out of memory\n");
+        return STATUS_INSUFFICIENT_RESOURCES;
+    }
+
+    Status = read_tree(Vcb, tree_address, buf);
+    if (!NT_SUCCESS(Status)) {
+        ERR("read_tree returned %08x\n", Status);
+        ExFreePool(buf);
+        return Status;
+    }
+    
+    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);
+    
+    ExFreePool(buf);
+    
+    return STATUS_SUCCESS;
+}
+
+NTSTATUS convert_shared_data_extent(device_extension* Vcb, UINT64 address, UINT64 size, LIST_ENTRY* rollback) {
+    KEY searchkey;
+    traverse_ptr tp;
+    LIST_ENTRY extent_refs;
+    LIST_ENTRY *le, *next_le;
+    EXTENT_ITEM* ei;
+    UINT64 eiflags, inline_rc;
+    UINT8* siptr;
+    ULONG len;
+    NTSTATUS Status;
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = TYPE_EXTENT_ITEM;
+    searchkey.offset = size;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return Status;
+    }
+    
+    if (keycmp(&tp.item->key, &searchkey)) {
+        WARN("extent item not found for address %llx, size %llx\n", address, size);
+        return STATUS_SUCCESS;
+    }
+    
+    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);
+    eiflags = ei->flags;
+    
+    InitializeListHead(&extent_refs);
+    
+    inline_rc = 0;
+    siptr = (UINT8*)&ei[1];
+    
+    do {
+        extent_ref* er;
+        ULONG extlen;
+        
+        extlen = get_extent_data_len(*siptr);
+        
+        if (extlen == 0) {
+            ERR("unrecognized extent subitem %x\n", *siptr);
+            free_extent_refs(&extent_refs);
+            return STATUS_INTERNAL_ERROR;
+        }
+        
+        if (extlen > len - 1) {
+            ERR("extent %llx was truncated\n", address);
+            free_extent_refs(&extent_refs);
+            return STATUS_INTERNAL_ERROR;
+        }
+        
+        er = ExAllocatePoolWithTag(PagedPool, sizeof(extent_ref), ALLOC_TAG);
+        if (!er) {
+            ERR("out of memory\n");
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+        
+        er->type = *siptr;
+        
+        er->data = ExAllocatePoolWithTag(PagedPool, extlen, ALLOC_TAG);
+        if (!er->data) {
+            ERR("out of memory\n");
+            ExFreePool(er);
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+
+        RtlCopyMemory(er->data, siptr+1, extlen);
+        er->allocated = TRUE;
+        
+        InsertTailList(&extent_refs, &er->list_entry);
+        
+        siptr += extlen;
+        len -= extlen + 1;
+        
+        inline_rc += get_extent_data_refcount(er->type, er->data);
+    } while (len > 0);
+    
+    delete_tree_item(Vcb, &tp, rollback);
+    
+    if (inline_rc < ei->refcount) {
+        BOOL b;
+        traverse_ptr next_tp;
+        
+        do {
+            b = find_next_item(Vcb, &tp, &next_tp, FALSE);
+            
+            if (tp.item->key.obj_id == address) {
+                ULONG extlen;
+                
+                extlen = get_extent_data_len(tp.item->key.obj_type);
+                
+                if (extlen != 0 && tp.item->size >= extlen) {
+                    extent_ref* er = ExAllocatePoolWithTag(PagedPool, sizeof(extent_ref), ALLOC_TAG);
+                    if (!er) {
+                        ERR("out of memory\n");
+                        return STATUS_INSUFFICIENT_RESOURCES;
+                    }
+                    
+                    er->type = tp.item->key.obj_type;
+                    
+                    er->data = ExAllocatePoolWithTag(PagedPool, extlen, ALLOC_TAG);
+                    if (!er->data) {
+                        ERR("out of memory\n");
+                        ExFreePool(er);
+                        return STATUS_INSUFFICIENT_RESOURCES;
+                    }
+
+                    RtlCopyMemory(er->data, siptr+1, extlen);
+                    er->allocated = TRUE;
+                    
+                    InsertTailList(&extent_refs, &er->list_entry);
+                    
+                    delete_tree_item(Vcb, &tp, rollback);
+                }
+            }
+            
+            if (b) {
+                tp = next_tp;
+                
+                if (tp.item->key.obj_id > address)
+                    break;
+            }
+        } while (b);
+    }
+    
+    le = extent_refs.Flink;
+    while (le != &extent_refs) {
+        extent_ref* er = CONTAINING_RECORD(le, extent_ref, list_entry);
+        next_le = le->Flink;
+        
+        if (er->type == TYPE_SHARED_DATA_REF) {
+            SHARED_DATA_REF* sdr = er->data;
+            
+            Status = populate_extent_refs_from_tree(Vcb, sdr->offset, address, &extent_refs);
+            if (!NT_SUCCESS(Status)) {
+                ERR("populate_extent_refs_from_tree returned %08x\n", Status);
+                free_extent_refs(&extent_refs);
+                return Status;
+            }
+
+            RemoveEntryList(&er->list_entry);
+            
+            if (er->allocated)
+                ExFreePool(er->data);
+            
+            ExFreePool(er);
+        }
+        // FIXME - also do for SHARED_BLOCK_REF?
+
+        le = next_le;
+    }
+    
+    Status = construct_extent_item(Vcb, address, size, eiflags, &extent_refs, rollback);
+    if (!NT_SUCCESS(Status)) {
+        ERR("construct_extent_item returned %08x\n", Status);
+        free_extent_refs(&extent_refs);
+        return Status;
+    }
+    
+    free_extent_refs(&extent_refs);
+    
+    return STATUS_SUCCESS;
+}
+
+NTSTATUS convert_old_data_extent(device_extension* Vcb, UINT64 address, UINT64 size, LIST_ENTRY* rollback) {
+    KEY searchkey;
+    traverse_ptr tp, next_tp;
+    BOOL b;
+    LIST_ENTRY extent_refs;
+    NTSTATUS Status;
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = TYPE_EXTENT_ITEM;
+    searchkey.offset = size;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return Status;
+    }
+    
+    if (keycmp(&tp.item->key, &searchkey)) {
+        WARN("extent item not found for address %llx, size %llx\n", address, size);
+        return STATUS_SUCCESS;
+    }
+    
+    if (tp.item->size != sizeof(EXTENT_ITEM_V0)) {
+        TRACE("extent does not appear to be old - returning STATUS_SUCCESS\n");
+        return STATUS_SUCCESS;
+    }
+    
+    delete_tree_item(Vcb, &tp, rollback);
+    
+    searchkey.obj_id = address;
+    searchkey.obj_type = TYPE_EXTENT_REF_V0;
+    searchkey.offset = 0;
+    
+    Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, FALSE);
+    if (!NT_SUCCESS(Status)) {
+        ERR("error - find_item returned %08x\n", Status);
+        return Status;
+    }
+    
+    InitializeListHead(&extent_refs);
+    
+    do {
+        b = find_next_item(Vcb, &tp, &next_tp, FALSE);
+        
+        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;
+            }
+            
+            delete_tree_item(Vcb, &tp, rollback);
+        }
+        
+        if (b) {
+            tp = next_tp;
+            
+            if (tp.item->key.obj_id > searchkey.obj_id || tp.item->key.obj_type > searchkey.obj_type)
+                break;
+        }
+    } while (b);
+    
+    Status = construct_extent_item(Vcb, address, size, EXTENT_ITEM_DATA, &extent_refs, rollback);
+    if (!NT_SUCCESS(Status)) {
+        ERR("construct_extent_item returned %08x\n", Status);
+        free_extent_refs(&extent_refs);
+        return Status;
+    }
+    
+    free_extent_refs(&extent_refs);
+    
+    return STATUS_SUCCESS;
+}
index e21c88b..5f6ee5e 100644 (file)
 
 #include "btrfs_drv.h"
 
-static NTSTATUS STDCALL move_subvol(device_extension* Vcb, fcb* fcb, root* destsubvol, UINT64 destinode, PANSI_STRING utf8, UINT32 crc32, UINT32 oldcrc32, BTRFS_TIME* now, BOOL ReplaceIfExists, LIST_ENTRY* rollback);
+static NTSTATUS STDCALL move_subvol(device_extension* Vcb, file_ref* fileref, root* destsubvol, UINT64 destinode, PANSI_STRING utf8, UINT32 crc32,
+                                    UINT32 oldcrc32, BTRFS_TIME* now, BOOL ReplaceIfExists, LIST_ENTRY* rollback);
 
 static NTSTATUS STDCALL set_basic_information(device_extension* Vcb, PIRP Irp, PFILE_OBJECT FileObject, LIST_ENTRY* rollback) {
     FILE_BASIC_INFORMATION* fbi = Irp->AssociatedIrp.SystemBuffer;
     fcb* fcb = FileObject->FsContext;
+    ccb* ccb = FileObject->FsContext2;
+    file_ref* fileref = ccb ? ccb->fileref : NULL;
     ULONG defda;
     BOOL inode_item_changed = FALSE;
     NTSTATUS Status;
     
-    if (fcb->ads)
-        fcb = fcb->par;
+    if (fcb->ads) {
+        if (fileref && fileref->parent)
+            fcb = fileref->parent->fcb;
+        else {
+            ERR("stream did not have fileref\n");
+            return STATUS_INSUFFICIENT_RESOURCES;
+        }
+    }
     
-    TRACE("file = %.*S, attributes = %x\n", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer, fbi->FileAttributes);
+    TRACE("file = %S, attributes = %x\n", file_desc(FileObject), fbi->FileAttributes);
     
     if (fbi->FileAttributes & FILE_ATTRIBUTE_DIRECTORY && fcb->type != BTRFS_TYPE_DIRECTORY) {
         WARN("attempted to set FILE_ATTRIBUTE_DIRECTORY on non-directory\n");
@@ -47,7 +56,7 @@ static NTSTATUS STDCALL set_basic_information(device_extension* Vcb, PIRP Irp, P
         LARGE_INTEGER time;
         BTRFS_TIME now;
         
-        defda = get_file_attributes(Vcb, &fcb->inode_item, fcb->subvol, fcb->inode, fcb->type, fcb->filepart.Length > 0 && fcb->filepart.Buffer[0] == '.', TRUE);
+        defda = get_file_attributes(Vcb, &fcb->inode_item, fcb->subvol, fcb->inode, fcb->type, fileref->filepart.Length > 0 && fileref->filepart.Buffer[0] == '.', TRUE);
         
         if (fcb->type == BTRFS_TYPE_DIRECTORY)
             fbi->FileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
@@ -109,8 +118,6 @@ static NTSTATUS STDCALL set_basic_information(device_extension* Vcb, PIRP Irp, P
         else
             WARN("couldn't find old INODE_ITEM\n");
         
-        free_traverse_ptr(&tp);
-        
         ii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
         if (!ii) {
             ERR("out of memory\n");
@@ -132,16 +139,31 @@ static NTSTATUS STDCALL set_basic_information(device_extension* Vcb, PIRP Irp, P
 static NTSTATUS STDCALL set_disposition_information(device_extension* Vcb, PIRP Irp, PFILE_OBJECT FileObject) {
     FILE_DISPOSITION_INFORMATION* fdi = Irp->AssociatedIrp.SystemBuffer;
     fcb* fcb = FileObject->FsContext;
+    ccb* ccb = FileObject->FsContext2;
+    file_ref* fileref = ccb ? ccb->fileref : NULL;
     ULONG atts;
     
-    TRACE("changing delete_on_close to %s for %.*S (fcb %p)\n", fdi->DeleteFile ? "TRUE" : "FALSE", fcb->full_filename.Length / sizeof(WCHAR), fcb->full_filename.Buffer, fcb);
+    if (!fileref)
+        return STATUS_INVALID_PARAMETER;
+    
+    TRACE("changing delete_on_close to %s for %S (fcb %p)\n", fdi->DeleteFile ? "TRUE" : "FALSE", file_desc(FileObject), fcb);
+    
+    if (fcb->ads) {
+        if (fileref->parent)
+            atts = fileref->parent->fcb->atts;
+        else {
+            ERR("no fileref for stream\n");
+            return STATUS_INTERNAL_ERROR;
+        }
+    } else
+        atts = fcb->atts;
     
-    atts = fcb->ads ? fcb->par->atts : fcb->atts;
     TRACE("atts = %x\n", atts);
     
     if (atts & FILE_ATTRIBUTE_READONLY)
         return STATUS_CANNOT_DELETE;
     
+    // FIXME - can we skip this bit for subvols?
     if (fcb->type == BTRFS_TYPE_DIRECTORY && fcb->inode_item.st_size > 0)
         return STATUS_DIRECTORY_NOT_EMPTY;
     
@@ -150,13 +172,8 @@ static NTSTATUS STDCALL set_disposition_information(device_extension* Vcb, PIRP
         return STATUS_CANNOT_DELETE;
     }
     
-    if (fcb->inode == SUBVOL_ROOT_INODE) {
-        FIXME("FIXME - subvol deletion not yet supported\n");
-        return STATUS_INTERNAL_ERROR;
-    }
+    ccb->fileref->delete_on_close = fdi->DeleteFile;
     
-    fcb->delete_on_close = fdi->DeleteFile;
-    // FIXME - should this fail if file opened with FILE_DELETE_ON_CLOSE?
     FileObject->DeletePending = fdi->DeleteFile;
     
     return STATUS_SUCCESS;
@@ -185,14 +202,12 @@ static NTSTATUS add_inode_extref(device_extension* Vcb, root* subvol, UINT64 ino
         
         if (iersize > maxlen) {
             ERR("item would be too long (%u > %u)\n", iersize, maxlen);
-            free_traverse_ptr(&tp);
             return STATUS_INTERNAL_ERROR;
         }
         
         ier2 = ExAllocatePoolWithTag(PagedPool, iersize, ALLOC_TAG);
         if (!ier2) {
             ERR("out of memory\n");
-            free_traverse_ptr(&tp);
             return STATUS_INSUFFICIENT_RESOURCES;
         }
         
@@ -209,14 +224,12 @@ static NTSTATUS add_inode_extref(device_extension* Vcb, root* subvol, UINT64 ino
         
         if (!insert_tree_item(Vcb, subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, ier2, iersize, NULL, rollback)) {
             ERR("error - failed to insert item\n");
-            free_traverse_ptr(&tp);
             return STATUS_INTERNAL_ERROR;
         }
     } else {
         ier = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_EXTREF) - 1 + utf8->Length, ALLOC_TAG);
         if (!ier) {
             ERR("out of memory\n");
-            free_traverse_ptr(&tp);
             return STATUS_INSUFFICIENT_RESOURCES;
         }
 
@@ -227,13 +240,10 @@ static NTSTATUS add_inode_extref(device_extension* Vcb, root* subvol, UINT64 ino
     
         if (!insert_tree_item(Vcb, subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, ier, sizeof(INODE_EXTREF) - 1 + utf8->Length, NULL, rollback)) {
             ERR("error - failed to insert item\n");
-            free_traverse_ptr(&tp);
             return STATUS_INTERNAL_ERROR;
         }
     }
     
-    free_traverse_ptr(&tp);
-    
     return STATUS_SUCCESS;
 }
 
@@ -261,11 +271,9 @@ NTSTATUS add_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UINT64
         if (irsize > maxlen) {
             if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_EXTENDED_IREF) {
                 TRACE("INODE_REF too long, creating INODE_EXTREF\n");
-                free_traverse_ptr(&tp);
                 return add_inode_extref(Vcb, subvol, inode, parinode, index, utf8, rollback);
             } else {
                 ERR("item would be too long (%u > %u)\n", irsize, maxlen);
-                free_traverse_ptr(&tp);
                 return STATUS_INTERNAL_ERROR;
             }
         }
@@ -273,7 +281,6 @@ NTSTATUS add_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UINT64
         ir2 = ExAllocatePoolWithTag(PagedPool, irsize, ALLOC_TAG);
         if (!ir2) {
             ERR("out of memory\n");
-            free_traverse_ptr(&tp);
             return STATUS_INSUFFICIENT_RESOURCES;
         }
         
@@ -289,14 +296,12 @@ NTSTATUS add_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UINT64
         
         if (!insert_tree_item(Vcb, subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, ir2, irsize, NULL, rollback)) {
             ERR("error - failed to insert item\n");
-            free_traverse_ptr(&tp);
             return STATUS_INTERNAL_ERROR;
         }
     } else {
         ir = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_REF) - 1 + utf8->Length, ALLOC_TAG);
         if (!ir) {
             ERR("out of memory\n");
-            free_traverse_ptr(&tp);
             return STATUS_INSUFFICIENT_RESOURCES;
         }
 
@@ -306,20 +311,17 @@ NTSTATUS add_inode_ref(device_extension* Vcb, root* subvol, UINT64 inode, UINT64
     
         if (!insert_tree_item(Vcb, subvol, searchkey.obj_id, searchkey.obj_type, searchkey.offset, ir, sizeof(INODE_REF) - 1 + ir->n, NULL, rollback)) {
             ERR("error - failed to insert item\n");
-            free_traverse_ptr(&tp);
             return STATUS_INTERNAL_ERROR;
         }
     }
     
-    free_traverse_ptr(&tp);
-    
     return STATUS_SUCCESS;
 }
 
-static NTSTATUS get_fcb_from_dir_item(device_extension* Vcb, fcb** pfcb, fcb* parent, root* subvol, DIR_ITEM* di) {
+static NTSTATUS get_fileref_from_dir_item(device_extension* Vcb, file_ref** pfr, file_ref* parent, root* subvol, DIR_ITEM* di) {
     LIST_ENTRY* le;
+    file_ref* fileref;
     fcb* sf2;
-    struct _fcb* c;
     KEY searchkey;
     traverse_ptr tp;
     NTSTATUS Status;
@@ -327,48 +329,67 @@ static NTSTATUS get_fcb_from_dir_item(device_extension* Vcb, fcb** pfcb, fcb* pa
     le = parent->children.Flink;
     
     while (le != &parent->children) {
-        c = CONTAINING_RECORD(le, struct _fcb, list_entry);
+        file_ref* c = CONTAINING_RECORD(le, file_ref, list_entry);
         
-        if (c->refcount > 0 && c->inode == di->key.obj_id && c->subvol == subvol) {
-            c->refcount++;
+        if (c->fcb->inode == di->key.obj_id && c->fcb->subvol == subvol) {
 #ifdef DEBUG_FCB_REFCOUNTS
-            WARN("fcb %p: refcount now %i (%.*S)\n", c, c->refcount, c->full_filename.Length / sizeof(WCHAR), c->full_filename.Buffer);
+            LONG rc = InterlockedIncrement(&c->refcount);
+            WARN("fileref %p: refcount now %i (%S)\n", c, rc, file_desc_fileref(c));
+#else
+            InterlockedIncrement(&c->refcount);
 #endif
-            *pfcb = c;
+            *pfr = c;
           &nbs