2017-09-08 08:02:43 +00:00
|
|
|
/* Copyright (c) Mark Harmstone 2016-17
|
|
|
|
*
|
2017-01-01 17:12:12 +00:00
|
|
|
* This file is part of WinBtrfs.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2017-01-01 17:12:12 +00:00
|
|
|
* 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.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2017-01-01 17:12:12 +00:00
|
|
|
* 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.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2017-01-01 17:12:12 +00:00
|
|
|
* 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"
|
|
|
|
#include "btrfsioctl.h"
|
2020-04-23 02:38:57 +00:00
|
|
|
#include "crc32c.h"
|
2017-09-08 08:02:43 +00:00
|
|
|
#include <ntddstor.h>
|
2017-01-01 17:12:12 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t address;
|
|
|
|
uint64_t new_address;
|
2017-01-01 17:12:12 +00:00
|
|
|
tree_header* data;
|
|
|
|
EXTENT_ITEM* ei;
|
|
|
|
tree* t;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool system;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY refs;
|
|
|
|
LIST_ENTRY list_entry;
|
|
|
|
} metadata_reloc;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t type;
|
|
|
|
uint64_t hash;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
union {
|
|
|
|
TREE_BLOCK_REF tbr;
|
|
|
|
SHARED_BLOCK_REF sbr;
|
|
|
|
};
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
metadata_reloc* parent;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool top;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY list_entry;
|
|
|
|
} metadata_reloc_ref;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t address;
|
|
|
|
uint64_t size;
|
|
|
|
uint64_t new_address;
|
2017-01-01 17:12:12 +00:00
|
|
|
chunk* newchunk;
|
|
|
|
EXTENT_ITEM* ei;
|
|
|
|
LIST_ENTRY refs;
|
|
|
|
LIST_ENTRY list_entry;
|
|
|
|
} data_reloc;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t type;
|
|
|
|
uint64_t hash;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
union {
|
|
|
|
EXTENT_DATA_REF edr;
|
|
|
|
SHARED_DATA_REF sdr;
|
|
|
|
};
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
metadata_reloc* parent;
|
|
|
|
LIST_ENTRY list_entry;
|
|
|
|
} data_reloc_ref;
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
#define BALANCE_UNIT 0x100000 // only read 1 MB at a time
|
2017-01-01 17:12:12 +00:00
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS add_metadata_reloc(_Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, LIST_ENTRY* items, traverse_ptr* tp,
|
2019-09-01 12:53:20 +00:00
|
|
|
bool skinny, metadata_reloc** mr2, chunk* c, LIST_ENTRY* rollback) {
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
metadata_reloc* mr;
|
|
|
|
EXTENT_ITEM* ei;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t len;
|
|
|
|
uint64_t inline_rc;
|
|
|
|
uint8_t* ptr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
mr = ExAllocatePoolWithTag(PagedPool, sizeof(metadata_reloc), ALLOC_TAG);
|
|
|
|
if (!mr) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
mr->address = tp->item->key.obj_id;
|
|
|
|
mr->data = NULL;
|
|
|
|
mr->ei = (EXTENT_ITEM*)tp->item->data;
|
2019-09-01 12:53:20 +00:00
|
|
|
mr->system = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
InitializeListHead(&mr->refs);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = delete_tree_item(Vcb, tp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(mr);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!c)
|
|
|
|
c = get_chunk_from_address(Vcb, tp->item->key.obj_id);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c) {
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
c->used -= Vcb->superblock.node_size;
|
|
|
|
|
|
|
|
space_list_add(c, tp->item->key.obj_id, Vcb->superblock.node_size, rollback);
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ei = (EXTENT_ITEM*)tp->item->data;
|
|
|
|
inline_rc = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
len = tp->item->size - sizeof(EXTENT_ITEM);
|
2019-09-01 12:53:20 +00:00
|
|
|
ptr = (uint8_t*)tp->item->data + sizeof(EXTENT_ITEM);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!skinny) {
|
|
|
|
len -= sizeof(EXTENT_ITEM2);
|
|
|
|
ptr += sizeof(EXTENT_ITEM2);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (len > 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t secttype = *ptr;
|
|
|
|
uint16_t sectlen = secttype == TYPE_TREE_BLOCK_REF ? sizeof(TREE_BLOCK_REF) : (secttype == TYPE_SHARED_BLOCK_REF ? sizeof(SHARED_BLOCK_REF) : 0);
|
2017-01-01 17:12:12 +00:00
|
|
|
metadata_reloc_ref* ref;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
len--;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (sectlen > len) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("(%I64x,%x,%I64x): %x bytes left, expecting at least %x\n", tp->item->key.obj_id, tp->item->key.obj_type, tp->item->key.offset, len, sectlen);
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sectlen == 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("(%I64x,%x,%I64x): unrecognized extent type %x\n", tp->item->key.obj_id, tp->item->key.obj_type, tp->item->key.offset, secttype);
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref = ExAllocatePoolWithTag(PagedPool, sizeof(metadata_reloc_ref), ALLOC_TAG);
|
|
|
|
if (!ref) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (secttype == TYPE_TREE_BLOCK_REF) {
|
|
|
|
ref->type = TYPE_TREE_BLOCK_REF;
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory(&ref->tbr, ptr + sizeof(uint8_t), sizeof(TREE_BLOCK_REF));
|
2017-01-01 17:12:12 +00:00
|
|
|
inline_rc++;
|
|
|
|
} else if (secttype == TYPE_SHARED_BLOCK_REF) {
|
|
|
|
ref->type = TYPE_SHARED_BLOCK_REF;
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory(&ref->sbr, ptr + sizeof(uint8_t), sizeof(SHARED_BLOCK_REF));
|
2017-01-01 17:12:12 +00:00
|
|
|
inline_rc++;
|
|
|
|
} else {
|
|
|
|
ERR("unexpected tree type %x\n", secttype);
|
|
|
|
ExFreePool(ref);
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->parent = NULL;
|
2019-09-01 12:53:20 +00:00
|
|
|
ref->top = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
InsertTailList(&mr->refs, &ref->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
len -= sectlen;
|
2019-09-01 12:53:20 +00:00
|
|
|
ptr += sizeof(uint8_t) + sectlen;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (inline_rc < ei->refcount) { // look for non-inline entries
|
|
|
|
traverse_ptr tp2 = *tp, next_tp;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
while (find_next_item(Vcb, &tp2, &next_tp, false, NULL)) {
|
2017-01-01 17:12:12 +00:00
|
|
|
tp2 = next_tp;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp2.item->key.obj_id == tp->item->key.obj_id) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if (tp2.item->key.obj_type == TYPE_TREE_BLOCK_REF) {
|
2017-01-01 17:12:12 +00:00
|
|
|
metadata_reloc_ref* ref = ExAllocatePoolWithTag(PagedPool, sizeof(metadata_reloc_ref), ALLOC_TAG);
|
|
|
|
if (!ref) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->type = TYPE_TREE_BLOCK_REF;
|
2017-09-08 08:02:43 +00:00
|
|
|
ref->tbr.offset = tp2.item->key.offset;
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->parent = NULL;
|
2019-09-01 12:53:20 +00:00
|
|
|
ref->top = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
InsertTailList(&mr->refs, &ref->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = delete_tree_item(Vcb, &tp2);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
} else if (tp2.item->key.obj_type == TYPE_SHARED_BLOCK_REF) {
|
2017-01-01 17:12:12 +00:00
|
|
|
metadata_reloc_ref* ref = ExAllocatePoolWithTag(PagedPool, sizeof(metadata_reloc_ref), ALLOC_TAG);
|
|
|
|
if (!ref) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->type = TYPE_SHARED_BLOCK_REF;
|
2017-09-08 08:02:43 +00:00
|
|
|
ref->sbr.offset = tp2.item->key.offset;
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->parent = NULL;
|
2019-09-01 12:53:20 +00:00
|
|
|
ref->top = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
InsertTailList(&mr->refs, &ref->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = delete_tree_item(Vcb, &tp2);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
InsertTailList(items, &mr->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (mr2)
|
|
|
|
*mr2 = mr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS add_metadata_reloc_parent(_Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, LIST_ENTRY* items,
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t address, metadata_reloc** mr2, LIST_ENTRY* rollback) {
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY* le;
|
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool skinny = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = items->Flink;
|
|
|
|
while (le != items) {
|
|
|
|
metadata_reloc* mr = CONTAINING_RECORD(le, metadata_reloc, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (mr->address == address) {
|
|
|
|
*mr2 = mr;
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
searchkey.obj_id = address;
|
|
|
|
searchkey.obj_type = TYPE_METADATA_ITEM;
|
|
|
|
searchkey.offset = 0xffffffffffffffff;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp.item->key.obj_id == address && tp.item->key.obj_type == TYPE_METADATA_ITEM && tp.item->size >= sizeof(EXTENT_ITEM))
|
2019-09-01 12:53:20 +00:00
|
|
|
skinny = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
else if (tp.item->key.obj_id == address && tp.item->key.obj_type == TYPE_EXTENT_ITEM && tp.item->key.offset == Vcb->superblock.node_size &&
|
|
|
|
tp.item->size >= sizeof(EXTENT_ITEM)) {
|
|
|
|
EXTENT_ITEM* ei = (EXTENT_ITEM*)tp.item->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!(ei->flags & EXTENT_ITEM_TREE_BLOCK)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("EXTENT_ITEM for %I64x found, but tree flag not set\n", address);
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("could not find valid EXTENT_ITEM for address %I64x\n", address);
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_metadata_reloc(Vcb, items, &tp, skinny, mr2, NULL, rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_metadata_reloc returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static void sort_metadata_reloc_refs(metadata_reloc* mr) {
|
|
|
|
LIST_ENTRY newlist, *le;
|
|
|
|
|
|
|
|
if (mr->refs.Flink == mr->refs.Blink) // 0 or 1 items
|
|
|
|
return;
|
|
|
|
|
|
|
|
// insertion sort
|
|
|
|
|
|
|
|
InitializeListHead(&newlist);
|
|
|
|
|
|
|
|
while (!IsListEmpty(&mr->refs)) {
|
|
|
|
metadata_reloc_ref* ref = CONTAINING_RECORD(RemoveHeadList(&mr->refs), metadata_reloc_ref, list_entry);
|
2019-09-01 12:53:20 +00:00
|
|
|
bool inserted = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (ref->type == TYPE_TREE_BLOCK_REF)
|
|
|
|
ref->hash = ref->tbr.offset;
|
|
|
|
else if (ref->type == TYPE_SHARED_BLOCK_REF)
|
|
|
|
ref->hash = ref->parent->new_address;
|
|
|
|
|
|
|
|
le = newlist.Flink;
|
|
|
|
while (le != &newlist) {
|
|
|
|
metadata_reloc_ref* ref2 = CONTAINING_RECORD(le, metadata_reloc_ref, list_entry);
|
|
|
|
|
|
|
|
if (ref->type < ref2->type || (ref->type == ref2->type && ref->hash > ref2->hash)) {
|
|
|
|
InsertHeadList(le->Blink, &ref->list_entry);
|
2019-09-01 12:53:20 +00:00
|
|
|
inserted = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inserted)
|
|
|
|
InsertTailList(&newlist, &ref->list_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
newlist.Flink->Blink = &mr->refs;
|
|
|
|
newlist.Blink->Flink = &mr->refs;
|
|
|
|
mr->refs.Flink = newlist.Flink;
|
|
|
|
mr->refs.Blink = newlist.Blink;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS add_metadata_reloc_extent_item(_Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, metadata_reloc* mr) {
|
|
|
|
NTSTATUS Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY* le;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t rc = 0;
|
|
|
|
uint16_t inline_len;
|
|
|
|
bool all_inline = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
metadata_reloc_ref* first_noninline = NULL;
|
|
|
|
EXTENT_ITEM* ei;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t* ptr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
inline_len = sizeof(EXTENT_ITEM);
|
|
|
|
if (!(Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA))
|
|
|
|
inline_len += sizeof(EXTENT_ITEM2);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
sort_metadata_reloc_refs(mr);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = mr->refs.Flink;
|
|
|
|
while (le != &mr->refs) {
|
|
|
|
metadata_reloc_ref* ref = CONTAINING_RECORD(le, metadata_reloc_ref, list_entry);
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t extlen = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
rc++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref->type == TYPE_TREE_BLOCK_REF)
|
|
|
|
extlen += sizeof(TREE_BLOCK_REF);
|
|
|
|
else if (ref->type == TYPE_SHARED_BLOCK_REF)
|
|
|
|
extlen += sizeof(SHARED_BLOCK_REF);
|
|
|
|
|
|
|
|
if (all_inline) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if ((ULONG)(inline_len + 1 + extlen) > (Vcb->superblock.node_size >> 2)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
all_inline = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
first_noninline = ref;
|
|
|
|
} else
|
|
|
|
inline_len += extlen + 1;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ei = ExAllocatePoolWithTag(PagedPool, inline_len, ALLOC_TAG);
|
|
|
|
if (!ei) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ei->refcount = rc;
|
|
|
|
ei->generation = mr->ei->generation;
|
|
|
|
ei->flags = mr->ei->flags;
|
2019-09-01 12:53:20 +00:00
|
|
|
ptr = (uint8_t*)&ei[1];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!(Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA)) {
|
|
|
|
EXTENT_ITEM2* ei2 = (EXTENT_ITEM2*)ptr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ei2->firstitem = *(KEY*)&mr->data[1];
|
|
|
|
ei2->level = mr->data->level;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ptr += sizeof(EXTENT_ITEM2);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = mr->refs.Flink;
|
|
|
|
while (le != &mr->refs) {
|
|
|
|
metadata_reloc_ref* ref = CONTAINING_RECORD(le, metadata_reloc_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref == first_noninline)
|
|
|
|
break;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
*ptr = ref->type;
|
|
|
|
ptr++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref->type == TYPE_TREE_BLOCK_REF) {
|
|
|
|
TREE_BLOCK_REF* tbr = (TREE_BLOCK_REF*)ptr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
tbr->offset = ref->tbr.offset;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ptr += sizeof(TREE_BLOCK_REF);
|
|
|
|
} else if (ref->type == TYPE_SHARED_BLOCK_REF) {
|
|
|
|
SHARED_BLOCK_REF* sbr = (SHARED_BLOCK_REF*)ptr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
sbr->offset = ref->parent->new_address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ptr += sizeof(SHARED_BLOCK_REF);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_SKINNY_METADATA)
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->extent_root, mr->new_address, TYPE_METADATA_ITEM, mr->data->level, ei, inline_len, NULL, NULL);
|
|
|
|
else
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->extent_root, mr->new_address, TYPE_EXTENT_ITEM, Vcb->superblock.node_size, ei, inline_len, NULL, NULL);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(ei);
|
|
|
|
return Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!all_inline) {
|
|
|
|
le = &first_noninline->list_entry;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le != &mr->refs) {
|
|
|
|
metadata_reloc_ref* ref = CONTAINING_RECORD(le, metadata_reloc_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref->type == TYPE_TREE_BLOCK_REF) {
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = insert_tree_item(Vcb, Vcb->extent_root, mr->new_address, TYPE_TREE_BLOCK_REF, ref->tbr.offset, NULL, 0, NULL, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
} else if (ref->type == TYPE_SHARED_BLOCK_REF) {
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = insert_tree_item(Vcb, Vcb->extent_root, mr->new_address, TYPE_SHARED_BLOCK_REF, ref->parent->new_address, NULL, 0, NULL, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ei->flags & EXTENT_ITEM_SHARED_BACKREFS || mr->data->flags & HEADER_FLAG_SHARED_BACKREF || !(mr->data->flags & HEADER_FLAG_MIXED_BACKREF)) {
|
|
|
|
if (mr->data->level > 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t i;
|
2017-01-01 17:12:12 +00:00
|
|
|
internal_node* in = (internal_node*)&mr->data[1];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (i = 0; i < mr->data->num_items; i++) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t sbrrc = find_extent_shared_tree_refcount(Vcb, in[i].address, mr->address, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
|
|
|
|
if (sbrrc > 0) {
|
|
|
|
SHARED_BLOCK_REF sbr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
sbr.offset = mr->new_address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = increase_extent_refcount(Vcb, in[i].address, Vcb->superblock.node_size, TYPE_SHARED_BLOCK_REF, &sbr, NULL, 0, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("increase_extent_refcount returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
sbr.offset = mr->address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = decrease_extent_refcount(Vcb, in[i].address, Vcb->superblock.node_size, TYPE_SHARED_BLOCK_REF, &sbr, NULL, 0,
|
2019-09-01 12:53:20 +00:00
|
|
|
sbr.offset, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("decrease_extent_refcount returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t i;
|
2017-01-01 17:12:12 +00:00
|
|
|
leaf_node* ln = (leaf_node*)&mr->data[1];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (i = 0; i < mr->data->num_items; i++) {
|
|
|
|
if (ln[i].key.obj_type == TYPE_EXTENT_DATA && ln[i].size >= sizeof(EXTENT_DATA) - 1 + sizeof(EXTENT_DATA2)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
EXTENT_DATA* ed = (EXTENT_DATA*)((uint8_t*)mr->data + sizeof(tree_header) + ln[i].offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ed->type == EXTENT_TYPE_REGULAR || ed->type == EXTENT_TYPE_PREALLOC) {
|
|
|
|
EXTENT_DATA2* ed2 = (EXTENT_DATA2*)ed->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ed2->size > 0) { // not sparse
|
2019-09-01 12:53:20 +00:00
|
|
|
uint32_t sdrrc = find_extent_shared_data_refcount(Vcb, ed2->address, mr->address, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (sdrrc > 0) {
|
|
|
|
SHARED_DATA_REF sdr;
|
|
|
|
chunk* c;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
sdr.offset = mr->new_address;
|
|
|
|
sdr.count = sdrrc;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = increase_extent_refcount(Vcb, ed2->address, ed2->size, TYPE_SHARED_DATA_REF, &sdr, NULL, 0, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("increase_extent_refcount returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
sdr.offset = mr->address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = decrease_extent_refcount(Vcb, ed2->address, ed2->size, TYPE_SHARED_DATA_REF, &sdr, NULL, 0,
|
2019-09-01 12:53:20 +00:00
|
|
|
sdr.offset, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("decrease_extent_refcount returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
c = get_chunk_from_address(Vcb, ed2->address);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c) {
|
|
|
|
// check changed_extents
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&c->changed_extents_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = c->changed_extents.Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le != &c->changed_extents) {
|
|
|
|
changed_extent* ce = CONTAINING_RECORD(le, changed_extent, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ce->address == ed2->address) {
|
|
|
|
LIST_ENTRY* le2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = ce->refs.Flink;
|
|
|
|
while (le2 != &ce->refs) {
|
|
|
|
changed_extent_ref* cer = CONTAINING_RECORD(le2, changed_extent_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (cer->type == TYPE_SHARED_DATA_REF && cer->sdr.offset == mr->address) {
|
|
|
|
cer->sdr.offset = mr->new_address;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = ce->old_refs.Flink;
|
|
|
|
while (le2 != &ce->old_refs) {
|
|
|
|
changed_extent_ref* cer = CONTAINING_RECORD(le2, changed_extent_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (cer->type == TYPE_SHARED_DATA_REF && cer->sdr.offset == mr->address) {
|
|
|
|
cer->sdr.offset = mr->new_address;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&c->changed_extents_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS write_metadata_items(_Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, LIST_ENTRY* items,
|
|
|
|
LIST_ENTRY* data_items, chunk* c, LIST_ENTRY* rollback) {
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY tree_writes, *le;
|
|
|
|
NTSTATUS Status;
|
|
|
|
traverse_ptr tp;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t level, max_level = 0;
|
2017-01-01 17:12:12 +00:00
|
|
|
chunk* newchunk = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
InitializeListHead(&tree_writes);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = items->Flink;
|
|
|
|
while (le != items) {
|
|
|
|
metadata_reloc* mr = CONTAINING_RECORD(le, metadata_reloc, list_entry);
|
|
|
|
LIST_ENTRY* le2;
|
|
|
|
chunk* pc;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
mr->data = ExAllocatePoolWithTag(PagedPool, Vcb->superblock.node_size, ALLOC_TAG);
|
|
|
|
if (!mr->data) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = read_data(Vcb, mr->address, Vcb->superblock.node_size, NULL, true, (uint8_t*)mr->data,
|
|
|
|
c && mr->address >= c->offset && mr->address < c->offset + c->chunk_item->size ? c : NULL, &pc, NULL, 0, false, NormalPagePriority);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("read_data returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (pc->chunk_item->type & BLOCK_FLAG_SYSTEM)
|
2019-09-01 12:53:20 +00:00
|
|
|
mr->system = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (data_items && mr->data->level == 0) {
|
2017-09-08 08:02:43 +00:00
|
|
|
le2 = data_items->Flink;
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le2 != data_items) {
|
|
|
|
data_reloc* dr = CONTAINING_RECORD(le2, data_reloc, list_entry);
|
|
|
|
leaf_node* ln = (leaf_node*)&mr->data[1];
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t i;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (i = 0; i < mr->data->num_items; i++) {
|
|
|
|
if (ln[i].key.obj_type == TYPE_EXTENT_DATA && ln[i].size >= sizeof(EXTENT_DATA) - 1 + sizeof(EXTENT_DATA2)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
EXTENT_DATA* ed = (EXTENT_DATA*)((uint8_t*)mr->data + sizeof(tree_header) + ln[i].offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ed->type == EXTENT_TYPE_REGULAR || ed->type == EXTENT_TYPE_PREALLOC) {
|
|
|
|
EXTENT_DATA2* ed2 = (EXTENT_DATA2*)ed->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ed2->address == dr->address)
|
|
|
|
ed2->address = dr->new_address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (mr->data->level > max_level)
|
|
|
|
max_level = mr->data->level;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = mr->refs.Flink;
|
|
|
|
while (le2 != &mr->refs) {
|
|
|
|
metadata_reloc_ref* ref = CONTAINING_RECORD(le2, metadata_reloc_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref->type == TYPE_TREE_BLOCK_REF) {
|
|
|
|
KEY* firstitem;
|
|
|
|
root* r = NULL;
|
|
|
|
LIST_ENTRY* le3;
|
|
|
|
tree* t;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
firstitem = (KEY*)&mr->data[1];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le3 = Vcb->roots.Flink;
|
|
|
|
while (le3 != &Vcb->roots) {
|
|
|
|
root* r2 = CONTAINING_RECORD(le3, root, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (r2->id == ref->tbr.offset) {
|
|
|
|
r = r2;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le3 = le3->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!r) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("could not find subvol with id %I64x\n", ref->tbr.offset);
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item_to_level(Vcb, r, &tp, firstitem, false, mr->data->level + 1, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status) && Status != STATUS_NOT_FOUND) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item_to_level returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
t = tp.tree;
|
|
|
|
while (t && t->header.level < mr->data->level + 1) {
|
|
|
|
t = t->parent;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!t)
|
2019-09-01 12:53:20 +00:00
|
|
|
ref->top = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
else {
|
|
|
|
metadata_reloc* mr2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_metadata_reloc_parent(Vcb, items, t->header.address, &mr2, rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_metadata_reloc_parent returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->parent = mr2;
|
|
|
|
}
|
|
|
|
} else if (ref->type == TYPE_SHARED_BLOCK_REF) {
|
|
|
|
metadata_reloc* mr2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_metadata_reloc_parent(Vcb, items, ref->sbr.offset, &mr2, rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_metadata_reloc_parent returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->parent = mr2;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = items->Flink;
|
|
|
|
while (le != items) {
|
|
|
|
metadata_reloc* mr = CONTAINING_RECORD(le, metadata_reloc, list_entry);
|
|
|
|
LIST_ENTRY* le2;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint32_t hash;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
mr->t = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
hash = calc_crc32c(0xffffffff, (uint8_t*)&mr->address, sizeof(uint64_t));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = Vcb->trees_ptrs[hash >> 24];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (le2) {
|
|
|
|
while (le2 != &Vcb->trees_hash) {
|
|
|
|
tree* t = CONTAINING_RECORD(le2, tree, list_entry_hash);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (t->header.address == mr->address) {
|
|
|
|
mr->t = t;
|
|
|
|
break;
|
|
|
|
} else if (t->hash > hash)
|
|
|
|
break;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (level = 0; level <= max_level; level++) {
|
|
|
|
le = items->Flink;
|
|
|
|
while (le != items) {
|
|
|
|
metadata_reloc* mr = CONTAINING_RECORD(le, metadata_reloc, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (mr->data->level == level) {
|
2019-09-01 12:53:20 +00:00
|
|
|
bool done = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY* le2;
|
|
|
|
tree_write* tw;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t flags;
|
2017-01-01 17:12:12 +00:00
|
|
|
tree* t3;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (mr->system)
|
|
|
|
flags = Vcb->system_flags;
|
|
|
|
else if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_MIXED_GROUPS)
|
|
|
|
flags = Vcb->data_flags;
|
|
|
|
else
|
|
|
|
flags = Vcb->metadata_flags;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (newchunk) {
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(newchunk, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (newchunk->chunk_item->type == flags && find_metadata_address_in_chunk(Vcb, newchunk, &mr->new_address)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
newchunk->used += Vcb->superblock.node_size;
|
2022-04-28 19:31:44 +00:00
|
|
|
space_list_subtract(newchunk, mr->new_address, Vcb->superblock.node_size, rollback);
|
2019-09-01 12:53:20 +00:00
|
|
|
done = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(newchunk, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!done) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->chunk_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = Vcb->chunks.Flink;
|
|
|
|
while (le2 != &Vcb->chunks) {
|
|
|
|
chunk* c2 = CONTAINING_RECORD(le2, chunk, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!c2->readonly && !c2->reloc && c2 != newchunk && c2->chunk_item->type == flags) {
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c2, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if ((c2->chunk_item->size - c2->used) >= Vcb->superblock.node_size) {
|
|
|
|
if (find_metadata_address_in_chunk(Vcb, c2, &mr->new_address)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
c2->used += Vcb->superblock.node_size;
|
2022-04-28 19:31:44 +00:00
|
|
|
space_list_subtract(c2, mr->new_address, Vcb->superblock.node_size, rollback);
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c2, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
newchunk = c2;
|
2019-09-01 12:53:20 +00:00
|
|
|
done = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c2, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// allocate new chunk if necessary
|
|
|
|
if (!done) {
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = alloc_chunk(Vcb, flags, &newchunk, false);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("alloc_chunk returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(newchunk, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
newchunk->balance_num = Vcb->balance.balance_num;
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!find_metadata_address_in_chunk(Vcb, newchunk, &mr->new_address)) {
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(newchunk, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
2017-01-01 17:12:12 +00:00
|
|
|
ERR("could not find address in new chunk\n");
|
|
|
|
Status = STATUS_DISK_FULL;
|
|
|
|
goto end;
|
|
|
|
} else {
|
2017-09-08 08:02:43 +00:00
|
|
|
newchunk->used += Vcb->superblock.node_size;
|
2022-04-28 19:31:44 +00:00
|
|
|
space_list_subtract(newchunk, mr->new_address, Vcb->superblock.node_size, rollback);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(newchunk, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// update parents
|
|
|
|
le2 = mr->refs.Flink;
|
|
|
|
while (le2 != &mr->refs) {
|
|
|
|
metadata_reloc_ref* ref = CONTAINING_RECORD(le2, metadata_reloc_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref->parent) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t i;
|
2017-01-01 17:12:12 +00:00
|
|
|
internal_node* in = (internal_node*)&ref->parent->data[1];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (i = 0; i < ref->parent->data->num_items; i++) {
|
|
|
|
if (in[i].address == mr->address) {
|
|
|
|
in[i].address = mr->new_address;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref->parent->t) {
|
|
|
|
LIST_ENTRY* le3;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le3 = ref->parent->t->itemlist.Flink;
|
|
|
|
while (le3 != &ref->parent->t->itemlist) {
|
|
|
|
tree_data* td = CONTAINING_RECORD(le3, tree_data, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!td->inserted && td->treeholder.address == mr->address)
|
|
|
|
td->treeholder.address = mr->new_address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le3 = le3->Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (ref->top && ref->type == TYPE_TREE_BLOCK_REF) {
|
|
|
|
LIST_ENTRY* le3;
|
|
|
|
root* r = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// alter ROOT_ITEM
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le3 = Vcb->roots.Flink;
|
|
|
|
while (le3 != &Vcb->roots) {
|
|
|
|
root* r2 = CONTAINING_RECORD(le3, root, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (r2->id == ref->tbr.offset) {
|
|
|
|
r = r2;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le3 = le3->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (r) {
|
|
|
|
r->treeholder.address = mr->new_address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (r == Vcb->root_root)
|
|
|
|
Vcb->superblock.root_tree_addr = mr->new_address;
|
|
|
|
else if (r == Vcb->chunk_root)
|
|
|
|
Vcb->superblock.chunk_tree_addr = mr->new_address;
|
|
|
|
else if (r->root_item.block_number == mr->address) {
|
|
|
|
KEY searchkey;
|
|
|
|
ROOT_ITEM* ri;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
r->root_item.block_number = mr->new_address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
searchkey.obj_id = r->id;
|
|
|
|
searchkey.obj_type = TYPE_ROOT_ITEM;
|
|
|
|
searchkey.offset = 0xffffffffffffffff;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->root_root, &tp, &searchkey, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp.item->key.obj_id != searchkey.obj_id || tp.item->key.obj_type != searchkey.obj_type) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("could not find ROOT_ITEM for tree %I64x\n", searchkey.obj_id);
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = STATUS_INTERNAL_ERROR;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ri = ExAllocatePoolWithTag(PagedPool, sizeof(ROOT_ITEM), ALLOC_TAG);
|
|
|
|
if (!ri) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RtlCopyMemory(ri, &r->root_item, sizeof(ROOT_ITEM));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = delete_tree_item(Vcb, &tp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->root_root, tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, ri, sizeof(ROOT_ITEM), NULL, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
mr->data->address = mr->new_address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
t3 = mr->t;
|
|
|
|
|
|
|
|
while (t3) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t h;
|
|
|
|
bool inserted;
|
2017-01-01 17:12:12 +00:00
|
|
|
tree* t4 = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// check if tree loaded more than once
|
|
|
|
if (t3->list_entry.Flink != &Vcb->trees_hash) {
|
|
|
|
tree* nt = CONTAINING_RECORD(t3->list_entry_hash.Flink, tree, list_entry_hash);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (nt->header.address == t3->header.address)
|
|
|
|
t4 = nt;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
t3->header.address = mr->new_address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
h = t3->hash >> 24;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->trees_ptrs[h] == &t3->list_entry_hash) {
|
|
|
|
if (t3->list_entry_hash.Flink == &Vcb->trees_hash)
|
|
|
|
Vcb->trees_ptrs[h] = NULL;
|
|
|
|
else {
|
|
|
|
tree* t2 = CONTAINING_RECORD(t3->list_entry_hash.Flink, tree, list_entry_hash);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (t2->hash >> 24 == h)
|
|
|
|
Vcb->trees_ptrs[h] = &t2->list_entry_hash;
|
|
|
|
else
|
|
|
|
Vcb->trees_ptrs[h] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RemoveEntryList(&t3->list_entry_hash);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
t3->hash = calc_crc32c(0xffffffff, (uint8_t*)&t3->header.address, sizeof(uint64_t));
|
2017-01-01 17:12:12 +00:00
|
|
|
h = t3->hash >> 24;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!Vcb->trees_ptrs[h]) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t h2 = h;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = Vcb->trees_hash.Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (h2 > 0) {
|
|
|
|
h2--;
|
|
|
|
do {
|
|
|
|
if (Vcb->trees_ptrs[h2]) {
|
|
|
|
le2 = Vcb->trees_ptrs[h2];
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
h2--;
|
|
|
|
} while (h2 > 0);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
le2 = Vcb->trees_ptrs[h];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
inserted = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le2 != &Vcb->trees_hash) {
|
|
|
|
tree* t2 = CONTAINING_RECORD(le2, tree, list_entry_hash);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (t2->hash >= t3->hash) {
|
|
|
|
InsertHeadList(le2->Blink, &t3->list_entry_hash);
|
2019-09-01 12:53:20 +00:00
|
|
|
inserted = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inserted)
|
|
|
|
InsertTailList(&Vcb->trees_hash, &t3->list_entry_hash);
|
|
|
|
|
|
|
|
if (!Vcb->trees_ptrs[h] || t3->list_entry_hash.Flink == Vcb->trees_ptrs[h])
|
|
|
|
Vcb->trees_ptrs[h] = &t3->list_entry_hash;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (data_items && level == 0) {
|
|
|
|
le2 = data_items->Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le2 != data_items) {
|
|
|
|
data_reloc* dr = CONTAINING_RECORD(le2, data_reloc, list_entry);
|
|
|
|
LIST_ENTRY* le3 = t3->itemlist.Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le3 != &t3->itemlist) {
|
|
|
|
tree_data* td = CONTAINING_RECORD(le3, tree_data, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!td->inserted && td->key.obj_type == TYPE_EXTENT_DATA && td->size >= sizeof(EXTENT_DATA) - 1 + sizeof(EXTENT_DATA2)) {
|
|
|
|
EXTENT_DATA* ed = (EXTENT_DATA*)td->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ed->type == EXTENT_TYPE_REGULAR || ed->type == EXTENT_TYPE_PREALLOC) {
|
|
|
|
EXTENT_DATA2* ed2 = (EXTENT_DATA2*)ed->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ed2->address == dr->address)
|
|
|
|
ed2->address = dr->new_address;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le3 = le3->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
t3 = t4;
|
|
|
|
}
|
|
|
|
|
2020-04-23 02:38:57 +00:00
|
|
|
calc_tree_checksum(Vcb, mr->data);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
tw = ExAllocatePoolWithTag(PagedPool, sizeof(tree_write), ALLOC_TAG);
|
|
|
|
if (!tw) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
tw->address = mr->new_address;
|
|
|
|
tw->length = Vcb->superblock.node_size;
|
2019-09-01 12:53:20 +00:00
|
|
|
tw->data = (uint8_t*)mr->data;
|
2019-11-12 18:32:46 +00:00
|
|
|
tw->allocated = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (IsListEmpty(&tree_writes))
|
|
|
|
InsertTailList(&tree_writes, &tw->list_entry);
|
|
|
|
else {
|
2019-09-01 12:53:20 +00:00
|
|
|
bool inserted = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = tree_writes.Flink;
|
|
|
|
while (le2 != &tree_writes) {
|
|
|
|
tree_write* tw2 = CONTAINING_RECORD(le2, tree_write, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tw2->address > tw->address) {
|
|
|
|
InsertHeadList(le2->Blink, &tw->list_entry);
|
2019-09-01 12:53:20 +00:00
|
|
|
inserted = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!inserted)
|
|
|
|
InsertTailList(&tree_writes, &tw->list_entry);
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = do_tree_writes(Vcb, &tree_writes, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_tree_writes returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = items->Flink;
|
|
|
|
while (le != items) {
|
|
|
|
metadata_reloc* mr = CONTAINING_RECORD(le, metadata_reloc, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = add_metadata_reloc_extent_item(Vcb, mr);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_metadata_reloc_extent_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
end:
|
|
|
|
while (!IsListEmpty(&tree_writes)) {
|
|
|
|
tree_write* tw = CONTAINING_RECORD(RemoveHeadList(&tree_writes), tree_write, list_entry);
|
2019-11-12 18:32:46 +00:00
|
|
|
|
|
|
|
if (tw->allocated)
|
|
|
|
ExFreePool(tw->data);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(tw);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static NTSTATUS balance_metadata_chunk(device_extension* Vcb, chunk* c, bool* changed) {
|
2017-01-01 17:12:12 +00:00
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
NTSTATUS Status;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool b;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY items, rollback;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint32_t loaded = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("chunk %I64x\n", c->offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
InitializeListHead(&rollback);
|
|
|
|
InitializeListHead(&items);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
searchkey.obj_id = c->offset;
|
|
|
|
searchkey.obj_type = TYPE_METADATA_ITEM;
|
|
|
|
searchkey.offset = 0xffffffffffffffff;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
do {
|
|
|
|
traverse_ptr next_tp;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp.item->key.obj_id >= c->offset + c->chunk_item->size)
|
|
|
|
break;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp.item->key.obj_id >= c->offset && (tp.item->key.obj_type == TYPE_EXTENT_ITEM || tp.item->key.obj_type == TYPE_METADATA_ITEM)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
bool tree = false, skinny = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp.item->key.obj_type == TYPE_METADATA_ITEM && tp.item->size >= sizeof(EXTENT_ITEM)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
tree = true;
|
|
|
|
skinny = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
} else if (tp.item->key.obj_type == TYPE_EXTENT_ITEM && tp.item->key.offset == Vcb->superblock.node_size &&
|
|
|
|
tp.item->size >= sizeof(EXTENT_ITEM)) {
|
|
|
|
EXTENT_ITEM* ei = (EXTENT_ITEM*)tp.item->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ei->flags & EXTENT_ITEM_TREE_BLOCK)
|
2019-09-01 12:53:20 +00:00
|
|
|
tree = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tree) {
|
|
|
|
Status = add_metadata_reloc(Vcb, &items, &tp, skinny, NULL, c, &rollback);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_metadata_reloc returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
loaded++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (loaded >= 64) // only do 64 at a time
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
b = find_next_item(Vcb, &tp, &next_tp, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (b)
|
|
|
|
tp = next_tp;
|
|
|
|
} while (b);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (IsListEmpty(&items)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
goto end;
|
|
|
|
} else
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = write_metadata_items(Vcb, &items, NULL, c, &rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("write_metadata_items returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->need_write = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
end:
|
2017-09-08 08:02:43 +00:00
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
Status = do_write(Vcb, NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
2017-09-08 08:02:43 +00:00
|
|
|
clear_rollback(&rollback);
|
2017-01-01 17:12:12 +00:00
|
|
|
else
|
|
|
|
do_rollback(Vcb, &rollback);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
free_trees(Vcb);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (!IsListEmpty(&items)) {
|
|
|
|
metadata_reloc* mr = CONTAINING_RECORD(RemoveHeadList(&items), metadata_reloc, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (!IsListEmpty(&mr->refs)) {
|
|
|
|
metadata_reloc_ref* ref = CONTAINING_RECORD(RemoveHeadList(&mr->refs), metadata_reloc_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(ref);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-11-12 18:32:46 +00:00
|
|
|
if (mr->data)
|
|
|
|
ExFreePool(mr->data);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(mr);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS data_reloc_add_tree_edr(_Requires_lock_held_(_Curr_->tree_lock) device_extension* Vcb, LIST_ENTRY* metadata_items,
|
|
|
|
data_reloc* dr, EXTENT_DATA_REF* edr, LIST_ENTRY* rollback) {
|
|
|
|
NTSTATUS Status;
|
|
|
|
LIST_ENTRY* le;
|
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
root* r = NULL;
|
|
|
|
metadata_reloc* mr;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t last_tree = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
data_reloc_ref* ref;
|
|
|
|
|
|
|
|
le = Vcb->roots.Flink;
|
|
|
|
while (le != &Vcb->roots) {
|
|
|
|
root* r2 = CONTAINING_RECORD(le, root, list_entry);
|
|
|
|
|
|
|
|
if (r2->id == edr->root) {
|
|
|
|
r = r2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!r) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("could not find subvol %I64x\n", edr->root);
|
2017-09-08 08:02:43 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
searchkey.obj_id = edr->objid;
|
|
|
|
searchkey.obj_type = TYPE_EXTENT_DATA;
|
|
|
|
searchkey.offset = 0;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, r, &tp, &searchkey, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
traverse_ptr tp2;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
if (find_next_item(Vcb, &tp, &tp2, false, NULL))
|
2017-09-08 08:02:43 +00:00
|
|
|
tp = tp2;
|
|
|
|
else {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("could not find EXTENT_DATA for inode %I64x in root %I64x\n", searchkey.obj_id, r->id);
|
2017-09-08 08:02:43 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ref = NULL;
|
|
|
|
|
|
|
|
while (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
|
|
|
|
traverse_ptr tp2;
|
|
|
|
|
|
|
|
if (tp.item->size >= sizeof(EXTENT_DATA)) {
|
|
|
|
EXTENT_DATA* ed = (EXTENT_DATA*)tp.item->data;
|
|
|
|
|
|
|
|
if ((ed->type == EXTENT_TYPE_PREALLOC || ed->type == EXTENT_TYPE_REGULAR) && tp.item->size >= offsetof(EXTENT_DATA, data[0]) + sizeof(EXTENT_DATA2)) {
|
|
|
|
EXTENT_DATA2* ed2 = (EXTENT_DATA2*)ed->data;
|
|
|
|
|
|
|
|
if (ed2->address == dr->address && ed2->size == dr->size && tp.item->key.offset - ed2->offset == edr->offset) {
|
|
|
|
if (ref && last_tree == tp.tree->header.address)
|
|
|
|
ref->edr.count++;
|
|
|
|
else {
|
|
|
|
ref = ExAllocatePoolWithTag(PagedPool, sizeof(data_reloc_ref), ALLOC_TAG);
|
|
|
|
if (!ref) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref->type = TYPE_EXTENT_DATA_REF;
|
|
|
|
RtlCopyMemory(&ref->edr, edr, sizeof(EXTENT_DATA_REF));
|
|
|
|
ref->edr.count = 1;
|
|
|
|
|
|
|
|
Status = add_metadata_reloc_parent(Vcb, metadata_items, tp.tree->header.address, &mr, rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_metadata_reloc_parent returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(ref);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
last_tree = tp.tree->header.address;
|
|
|
|
ref->parent = mr;
|
|
|
|
|
|
|
|
InsertTailList(&dr->refs, &ref->list_entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
if (find_next_item(Vcb, &tp, &tp2, false, NULL))
|
2017-09-08 08:02:43 +00:00
|
|
|
tp = tp2;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS add_data_reloc(_Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, LIST_ENTRY* items, LIST_ENTRY* metadata_items,
|
|
|
|
traverse_ptr* tp, chunk* c, LIST_ENTRY* rollback) {
|
|
|
|
NTSTATUS Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
data_reloc* dr;
|
|
|
|
EXTENT_ITEM* ei;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t len;
|
|
|
|
uint64_t inline_rc;
|
|
|
|
uint8_t* ptr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
dr = ExAllocatePoolWithTag(PagedPool, sizeof(data_reloc), ALLOC_TAG);
|
|
|
|
if (!dr) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
dr->address = tp->item->key.obj_id;
|
|
|
|
dr->size = tp->item->key.offset;
|
|
|
|
dr->ei = (EXTENT_ITEM*)tp->item->data;
|
|
|
|
InitializeListHead(&dr->refs);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = delete_tree_item(Vcb, tp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!c)
|
|
|
|
c = get_chunk_from_address(Vcb, tp->item->key.obj_id);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c) {
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
c->used -= tp->item->key.offset;
|
|
|
|
|
|
|
|
space_list_add(c, tp->item->key.obj_id, tp->item->key.offset, rollback);
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ei = (EXTENT_ITEM*)tp->item->data;
|
|
|
|
inline_rc = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
len = tp->item->size - sizeof(EXTENT_ITEM);
|
2019-09-01 12:53:20 +00:00
|
|
|
ptr = (uint8_t*)tp->item->data + sizeof(EXTENT_ITEM);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (len > 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t secttype = *ptr;
|
|
|
|
uint16_t sectlen = secttype == TYPE_EXTENT_DATA_REF ? sizeof(EXTENT_DATA_REF) : (secttype == TYPE_SHARED_DATA_REF ? sizeof(SHARED_DATA_REF) : 0);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
len--;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (sectlen > len) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("(%I64x,%x,%I64x): %x bytes left, expecting at least %x\n", tp->item->key.obj_id, tp->item->key.obj_type, tp->item->key.offset, len, sectlen);
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sectlen == 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("(%I64x,%x,%I64x): unrecognized extent type %x\n", tp->item->key.obj_id, tp->item->key.obj_type, tp->item->key.offset, secttype);
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (secttype == TYPE_EXTENT_DATA_REF) {
|
2019-09-01 12:53:20 +00:00
|
|
|
EXTENT_DATA_REF* edr = (EXTENT_DATA_REF*)(ptr + sizeof(uint8_t));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
inline_rc += edr->count;
|
|
|
|
|
|
|
|
Status = data_reloc_add_tree_edr(Vcb, metadata_items, dr, edr, rollback);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("data_reloc_add_tree_edr returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
} else if (secttype == TYPE_SHARED_DATA_REF) {
|
2017-09-08 08:02:43 +00:00
|
|
|
metadata_reloc* mr;
|
|
|
|
data_reloc_ref* ref;
|
|
|
|
|
|
|
|
ref = ExAllocatePoolWithTag(PagedPool, sizeof(data_reloc_ref), ALLOC_TAG);
|
|
|
|
if (!ref) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->type = TYPE_SHARED_DATA_REF;
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlCopyMemory(&ref->sdr, ptr + sizeof(uint8_t), sizeof(SHARED_DATA_REF));
|
2017-01-01 17:12:12 +00:00
|
|
|
inline_rc += ref->sdr.count;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_metadata_reloc_parent(Vcb, metadata_items, ref->sdr.offset, &mr, rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_metadata_reloc_parent returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(ref);
|
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->parent = mr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
InsertTailList(&dr->refs, &ref->list_entry);
|
2017-01-01 17:12:12 +00:00
|
|
|
} else {
|
|
|
|
ERR("unexpected tree type %x\n", secttype);
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
len -= sectlen;
|
2019-09-01 12:53:20 +00:00
|
|
|
ptr += sizeof(uint8_t) + sectlen;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (inline_rc < ei->refcount) { // look for non-inline entries
|
|
|
|
traverse_ptr tp2 = *tp, next_tp;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
while (find_next_item(Vcb, &tp2, &next_tp, false, NULL)) {
|
2017-01-01 17:12:12 +00:00
|
|
|
tp2 = next_tp;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp2.item->key.obj_id == tp->item->key.obj_id) {
|
|
|
|
if (tp2.item->key.obj_type == TYPE_EXTENT_DATA_REF && tp2.item->size >= sizeof(EXTENT_DATA_REF)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = data_reloc_add_tree_edr(Vcb, metadata_items, dr, (EXTENT_DATA_REF*)tp2.item->data, rollback);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("data_reloc_add_tree_edr returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = delete_tree_item(Vcb, &tp2);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2019-09-01 12:53:20 +00:00
|
|
|
} else if (tp2.item->key.obj_type == TYPE_SHARED_DATA_REF && tp2.item->size >= sizeof(uint32_t)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
metadata_reloc* mr;
|
|
|
|
data_reloc_ref* ref;
|
|
|
|
|
|
|
|
ref = ExAllocatePoolWithTag(PagedPool, sizeof(data_reloc_ref), ALLOC_TAG);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!ref) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->type = TYPE_SHARED_DATA_REF;
|
2017-09-08 08:02:43 +00:00
|
|
|
ref->sdr.offset = tp2.item->key.offset;
|
2019-09-01 12:53:20 +00:00
|
|
|
ref->sdr.count = *((uint32_t*)tp2.item->data);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_metadata_reloc_parent(Vcb, metadata_items, ref->sdr.offset, &mr, rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_metadata_reloc_parent returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(ref);
|
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ref->parent = mr;
|
|
|
|
InsertTailList(&dr->refs, &ref->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = delete_tree_item(Vcb, &tp2);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
InsertTailList(items, &dr->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static void sort_data_reloc_refs(data_reloc* dr) {
|
|
|
|
LIST_ENTRY newlist, *le;
|
|
|
|
|
|
|
|
if (IsListEmpty(&dr->refs))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// insertion sort
|
|
|
|
|
|
|
|
InitializeListHead(&newlist);
|
|
|
|
|
|
|
|
while (!IsListEmpty(&dr->refs)) {
|
|
|
|
data_reloc_ref* ref = CONTAINING_RECORD(RemoveHeadList(&dr->refs), data_reloc_ref, list_entry);
|
2019-09-01 12:53:20 +00:00
|
|
|
bool inserted = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (ref->type == TYPE_EXTENT_DATA_REF)
|
|
|
|
ref->hash = get_extent_data_ref_hash2(ref->edr.root, ref->edr.objid, ref->edr.offset);
|
|
|
|
else if (ref->type == TYPE_SHARED_DATA_REF)
|
|
|
|
ref->hash = ref->parent->new_address;
|
|
|
|
|
|
|
|
le = newlist.Flink;
|
|
|
|
while (le != &newlist) {
|
|
|
|
data_reloc_ref* ref2 = CONTAINING_RECORD(le, data_reloc_ref, list_entry);
|
|
|
|
|
|
|
|
if (ref->type < ref2->type || (ref->type == ref2->type && ref->hash > ref2->hash)) {
|
|
|
|
InsertHeadList(le->Blink, &ref->list_entry);
|
2019-09-01 12:53:20 +00:00
|
|
|
inserted = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inserted)
|
|
|
|
InsertTailList(&newlist, &ref->list_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
le = newlist.Flink;
|
|
|
|
while (le != &newlist) {
|
|
|
|
data_reloc_ref* ref = CONTAINING_RECORD(le, data_reloc_ref, list_entry);
|
|
|
|
|
|
|
|
if (le->Flink != &newlist) {
|
|
|
|
data_reloc_ref* ref2 = CONTAINING_RECORD(le->Flink, data_reloc_ref, list_entry);
|
|
|
|
|
|
|
|
if (ref->type == TYPE_EXTENT_DATA_REF && ref2->type == TYPE_EXTENT_DATA_REF && ref->edr.root == ref2->edr.root &&
|
|
|
|
ref->edr.objid == ref2->edr.objid && ref->edr.offset == ref2->edr.offset) {
|
|
|
|
RemoveEntryList(&ref2->list_entry);
|
|
|
|
ref->edr.count += ref2->edr.count;
|
|
|
|
ExFreePool(ref2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
newlist.Flink->Blink = &dr->refs;
|
|
|
|
newlist.Blink->Flink = &dr->refs;
|
|
|
|
dr->refs.Flink = newlist.Flink;
|
|
|
|
dr->refs.Blink = newlist.Blink;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS add_data_reloc_extent_item(_Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, data_reloc* dr) {
|
|
|
|
NTSTATUS Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY* le;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t rc = 0;
|
|
|
|
uint16_t inline_len;
|
|
|
|
bool all_inline = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
data_reloc_ref* first_noninline = NULL;
|
|
|
|
EXTENT_ITEM* ei;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t* ptr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
inline_len = sizeof(EXTENT_ITEM);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
sort_data_reloc_refs(dr);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = dr->refs.Flink;
|
|
|
|
while (le != &dr->refs) {
|
|
|
|
data_reloc_ref* ref = CONTAINING_RECORD(le, data_reloc_ref, list_entry);
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t extlen = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (ref->type == TYPE_EXTENT_DATA_REF) {
|
2017-01-01 17:12:12 +00:00
|
|
|
extlen += sizeof(EXTENT_DATA_REF);
|
2017-09-08 08:02:43 +00:00
|
|
|
rc += ref->edr.count;
|
|
|
|
} else if (ref->type == TYPE_SHARED_DATA_REF) {
|
2017-01-01 17:12:12 +00:00
|
|
|
extlen += sizeof(SHARED_DATA_REF);
|
2017-09-08 08:02:43 +00:00
|
|
|
rc++;
|
|
|
|
}
|
2017-01-01 17:12:12 +00:00
|
|
|
|
|
|
|
if (all_inline) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if ((ULONG)(inline_len + 1 + extlen) > (Vcb->superblock.node_size >> 2)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
all_inline = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
first_noninline = ref;
|
|
|
|
} else
|
|
|
|
inline_len += extlen + 1;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ei = ExAllocatePoolWithTag(PagedPool, inline_len, ALLOC_TAG);
|
|
|
|
if (!ei) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ei->refcount = rc;
|
|
|
|
ei->generation = dr->ei->generation;
|
|
|
|
ei->flags = dr->ei->flags;
|
2019-09-01 12:53:20 +00:00
|
|
|
ptr = (uint8_t*)&ei[1];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = dr->refs.Flink;
|
|
|
|
while (le != &dr->refs) {
|
|
|
|
data_reloc_ref* ref = CONTAINING_RECORD(le, data_reloc_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref == first_noninline)
|
|
|
|
break;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
*ptr = ref->type;
|
|
|
|
ptr++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref->type == TYPE_EXTENT_DATA_REF) {
|
|
|
|
EXTENT_DATA_REF* edr = (EXTENT_DATA_REF*)ptr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RtlCopyMemory(edr, &ref->edr, sizeof(EXTENT_DATA_REF));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ptr += sizeof(EXTENT_DATA_REF);
|
|
|
|
} else if (ref->type == TYPE_SHARED_DATA_REF) {
|
|
|
|
SHARED_DATA_REF* sdr = (SHARED_DATA_REF*)ptr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
sdr->offset = ref->parent->new_address;
|
|
|
|
sdr->count = ref->sdr.count;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ptr += sizeof(SHARED_DATA_REF);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->extent_root, dr->new_address, TYPE_EXTENT_ITEM, dr->size, ei, inline_len, NULL, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!all_inline) {
|
|
|
|
le = &first_noninline->list_entry;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le != &dr->refs) {
|
|
|
|
data_reloc_ref* ref = CONTAINING_RECORD(le, data_reloc_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ref->type == TYPE_EXTENT_DATA_REF) {
|
|
|
|
EXTENT_DATA_REF* edr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
edr = ExAllocatePoolWithTag(PagedPool, sizeof(EXTENT_DATA_REF), ALLOC_TAG);
|
|
|
|
if (!edr) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RtlCopyMemory(edr, &ref->edr, sizeof(EXTENT_DATA_REF));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->extent_root, dr->new_address, TYPE_EXTENT_DATA_REF, ref->hash, edr, sizeof(EXTENT_DATA_REF), NULL, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
} else if (ref->type == TYPE_SHARED_DATA_REF) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint32_t* sdr;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
sdr = ExAllocatePoolWithTag(PagedPool, sizeof(uint32_t), ALLOC_TAG);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!sdr) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
*sdr = ref->sdr.count;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = insert_tree_item(Vcb, Vcb->extent_root, dr->new_address, TYPE_SHARED_DATA_REF, ref->parent->new_address, sdr, sizeof(uint32_t), NULL, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static NTSTATUS balance_data_chunk(device_extension* Vcb, chunk* c, bool* changed) {
|
2017-01-01 17:12:12 +00:00
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
NTSTATUS Status;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool b;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY items, metadata_items, rollback, *le;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t loaded = 0, num_loaded = 0;
|
2017-01-01 17:12:12 +00:00
|
|
|
chunk* newchunk = NULL;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t* data = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("chunk %I64x\n", c->offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
InitializeListHead(&rollback);
|
|
|
|
InitializeListHead(&items);
|
|
|
|
InitializeListHead(&metadata_items);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
searchkey.obj_id = c->offset;
|
|
|
|
searchkey.obj_type = TYPE_EXTENT_ITEM;
|
|
|
|
searchkey.offset = 0xffffffffffffffff;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
do {
|
|
|
|
traverse_ptr next_tp;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp.item->key.obj_id >= c->offset + c->chunk_item->size)
|
|
|
|
break;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp.item->key.obj_id >= c->offset && tp.item->key.obj_type == TYPE_EXTENT_ITEM) {
|
2019-09-01 12:53:20 +00:00
|
|
|
bool tree = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp.item->key.obj_type == TYPE_EXTENT_ITEM && tp.item->size >= sizeof(EXTENT_ITEM)) {
|
|
|
|
EXTENT_ITEM* ei = (EXTENT_ITEM*)tp.item->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ei->flags & EXTENT_ITEM_TREE_BLOCK)
|
2019-09-01 12:53:20 +00:00
|
|
|
tree = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!tree) {
|
|
|
|
Status = add_data_reloc(Vcb, &items, &metadata_items, &tp, c, &rollback);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_data_reloc returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
loaded += tp.item->key.offset;
|
|
|
|
num_loaded++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (loaded >= 0x1000000 || num_loaded >= 100) // only do so much at a time, so we don't block too obnoxiously
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
b = find_next_item(Vcb, &tp, &next_tp, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (b)
|
|
|
|
tp = next_tp;
|
|
|
|
} while (b);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (IsListEmpty(&items)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
goto end;
|
|
|
|
} else
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
data = ExAllocatePoolWithTag(PagedPool, BALANCE_UNIT, ALLOC_TAG);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!data) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
le = items.Flink;
|
|
|
|
while (le != &items) {
|
|
|
|
data_reloc* dr = CONTAINING_RECORD(le, data_reloc, list_entry);
|
2019-09-01 12:53:20 +00:00
|
|
|
bool done = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY* le2;
|
2020-04-23 02:38:57 +00:00
|
|
|
void* csum;
|
2017-09-08 08:02:43 +00:00
|
|
|
RTL_BITMAP bmp;
|
|
|
|
ULONG* bmparr;
|
2019-09-01 12:53:20 +00:00
|
|
|
ULONG bmplen, runlength, index, lastoff;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (newchunk) {
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(newchunk, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (find_data_address_in_chunk(Vcb, newchunk, dr->size, &dr->new_address)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
newchunk->used += dr->size;
|
2022-04-28 19:31:44 +00:00
|
|
|
space_list_subtract(newchunk, dr->new_address, dr->size, &rollback);
|
2019-09-01 12:53:20 +00:00
|
|
|
done = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(newchunk, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!done) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->chunk_lock, true);
|
2017-01-01 17:12:12 +00:00
|
|
|
|
|
|
|
le2 = Vcb->chunks.Flink;
|
|
|
|
while (le2 != &Vcb->chunks) {
|
|
|
|
chunk* c2 = CONTAINING_RECORD(le2, chunk, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!c2->readonly && !c2->reloc && c2 != newchunk && c2->chunk_item->type == Vcb->data_flags) {
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c2, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if ((c2->chunk_item->size - c2->used) >= dr->size) {
|
|
|
|
if (find_data_address_in_chunk(Vcb, c2, dr->size, &dr->new_address)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
c2->used += dr->size;
|
2022-04-28 19:31:44 +00:00
|
|
|
space_list_subtract(c2, dr->new_address, dr->size, &rollback);
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c2, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
newchunk = c2;
|
2019-09-01 12:53:20 +00:00
|
|
|
done = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c2, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// allocate new chunk if necessary
|
|
|
|
if (!done) {
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = alloc_chunk(Vcb, Vcb->data_flags, &newchunk, false);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("alloc_chunk returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(newchunk, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
newchunk->balance_num = Vcb->balance.balance_num;
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!find_data_address_in_chunk(Vcb, newchunk, dr->size, &dr->new_address)) {
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(newchunk, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
2017-01-01 17:12:12 +00:00
|
|
|
ERR("could not find address in new chunk\n");
|
|
|
|
Status = STATUS_DISK_FULL;
|
|
|
|
goto end;
|
|
|
|
} else {
|
2017-09-08 08:02:43 +00:00
|
|
|
newchunk->used += dr->size;
|
2022-04-28 19:31:44 +00:00
|
|
|
space_list_subtract(newchunk, dr->new_address, dr->size, &rollback);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(newchunk, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
dr->newchunk = newchunk;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
bmplen = (ULONG)(dr->size >> Vcb->sector_shift);
|
2019-09-01 12:53:20 +00:00
|
|
|
|
|
|
|
bmparr = ExAllocatePoolWithTag(PagedPool, (ULONG)sector_align(bmplen + 1, sizeof(ULONG)), ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!bmparr) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
csum = ExAllocatePoolWithTag(PagedPool, (ULONG)((dr->size * Vcb->csum_size) >> Vcb->sector_shift), ALLOC_TAG);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!csum) {
|
|
|
|
ERR("out of memory\n");
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(bmparr);
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
RtlInitializeBitMap(&bmp, bmparr, bmplen);
|
2017-09-08 08:02:43 +00:00
|
|
|
RtlSetAllBits(&bmp); // 1 = no csum, 0 = csum
|
|
|
|
|
|
|
|
searchkey.obj_id = EXTENT_CSUM_ID;
|
|
|
|
searchkey.obj_type = TYPE_EXTENT_CSUM;
|
|
|
|
searchkey.offset = dr->address;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->checksum_root, &tp, &searchkey, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status) && Status != STATUS_NOT_FOUND) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(csum);
|
|
|
|
ExFreePool(bmparr);
|
|
|
|
goto end;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
if (Status != STATUS_NOT_FOUND) {
|
|
|
|
do {
|
|
|
|
traverse_ptr next_tp;
|
|
|
|
|
|
|
|
if (tp.item->key.obj_type == TYPE_EXTENT_CSUM) {
|
|
|
|
if (tp.item->key.offset >= dr->address + dr->size)
|
|
|
|
break;
|
2022-04-28 19:31:44 +00:00
|
|
|
else if (tp.item->size >= Vcb->csum_size && tp.item->key.offset + (((unsigned int)tp.item->size << Vcb->sector_shift) / Vcb->csum_size) >= dr->address) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t cs = max(dr->address, tp.item->key.offset);
|
2022-04-28 19:31:44 +00:00
|
|
|
uint64_t ce = min(dr->address + dr->size, tp.item->key.offset + (((unsigned int)tp.item->size << Vcb->sector_shift) / Vcb->csum_size));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
RtlCopyMemory((uint8_t*)csum + (((cs - dr->address) * Vcb->csum_size) >> Vcb->sector_shift),
|
|
|
|
tp.item->data + (((cs - tp.item->key.offset) * Vcb->csum_size) >> Vcb->sector_shift),
|
|
|
|
(ULONG)(((ce - cs) * Vcb->csum_size) >> Vcb->sector_shift));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
RtlClearBits(&bmp, (ULONG)((cs - dr->address) >> Vcb->sector_shift), (ULONG)((ce - cs) >> Vcb->sector_shift));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (ce == dr->address + dr->size)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
if (find_next_item(Vcb, &tp, &next_tp, false, NULL))
|
2017-09-08 08:02:43 +00:00
|
|
|
tp = next_tp;
|
|
|
|
else
|
|
|
|
break;
|
2019-09-01 12:53:20 +00:00
|
|
|
} while (true);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastoff = 0;
|
|
|
|
runlength = RtlFindFirstRunClear(&bmp, &index);
|
|
|
|
|
|
|
|
while (runlength != 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
if (index >= bmplen)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (index + runlength >= bmplen) {
|
|
|
|
runlength = bmplen - index;
|
|
|
|
|
|
|
|
if (runlength == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
if (index > lastoff) {
|
|
|
|
ULONG off = lastoff;
|
|
|
|
ULONG size = index - lastoff;
|
|
|
|
|
|
|
|
// handle no csum run
|
|
|
|
do {
|
|
|
|
ULONG rl;
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
if (size << Vcb->sector_shift > BALANCE_UNIT)
|
|
|
|
rl = BALANCE_UNIT >> Vcb->sector_shift;
|
2017-09-08 08:02:43 +00:00
|
|
|
else
|
|
|
|
rl = size;
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
Status = read_data(Vcb, dr->address + (off << Vcb->sector_shift), rl << Vcb->sector_shift, NULL, false, data,
|
2019-09-01 12:53:20 +00:00
|
|
|
c, NULL, NULL, 0, false, NormalPagePriority);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("read_data returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(csum);
|
|
|
|
ExFreePool(bmparr);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
Status = write_data_complete(Vcb, dr->new_address + (off << Vcb->sector_shift), data, rl << Vcb->sector_shift,
|
2019-09-01 12:53:20 +00:00
|
|
|
NULL, newchunk, false, 0, NormalPagePriority);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("write_data_complete returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(csum);
|
|
|
|
ExFreePool(bmparr);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
size -= rl;
|
|
|
|
off += rl;
|
|
|
|
} while (size > 0);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
add_checksum_entry(Vcb, dr->new_address + (index << Vcb->sector_shift), runlength, (uint8_t*)csum + (index * Vcb->csum_size), NULL);
|
|
|
|
add_checksum_entry(Vcb, dr->address + (index << Vcb->sector_shift), runlength, NULL, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
// handle csum run
|
|
|
|
do {
|
|
|
|
ULONG rl;
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
if (runlength << Vcb->sector_shift > BALANCE_UNIT)
|
|
|
|
rl = BALANCE_UNIT >> Vcb->sector_shift;
|
2017-09-08 08:02:43 +00:00
|
|
|
else
|
|
|
|
rl = runlength;
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
Status = read_data(Vcb, dr->address + (index << Vcb->sector_shift), rl << Vcb->sector_shift,
|
2020-04-23 02:38:57 +00:00
|
|
|
(uint8_t*)csum + (index * Vcb->csum_size), false, data, c, NULL, NULL, 0, false, NormalPagePriority);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("read_data returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(csum);
|
|
|
|
ExFreePool(bmparr);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
Status = write_data_complete(Vcb, dr->new_address + (index << Vcb->sector_shift), data, rl << Vcb->sector_shift,
|
2019-09-01 12:53:20 +00:00
|
|
|
NULL, newchunk, false, 0, NormalPagePriority);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("write_data_complete returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(csum);
|
|
|
|
ExFreePool(bmparr);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
runlength -= rl;
|
|
|
|
index += rl;
|
|
|
|
} while (runlength > 0);
|
|
|
|
|
|
|
|
lastoff = index;
|
|
|
|
runlength = RtlFindNextForwardRunClear(&bmp, index, &index);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExFreePool(csum);
|
|
|
|
ExFreePool(bmparr);
|
|
|
|
|
|
|
|
// handle final nocsum run
|
2022-04-28 19:31:44 +00:00
|
|
|
if (lastoff < dr->size >> Vcb->sector_shift) {
|
2017-09-08 08:02:43 +00:00
|
|
|
ULONG off = lastoff;
|
2022-04-28 19:31:44 +00:00
|
|
|
ULONG size = (ULONG)((dr->size >> Vcb->sector_shift) - lastoff);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
ULONG rl;
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
if (size << Vcb->sector_shift > BALANCE_UNIT)
|
|
|
|
rl = BALANCE_UNIT >> Vcb->sector_shift;
|
2017-09-08 08:02:43 +00:00
|
|
|
else
|
|
|
|
rl = size;
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
Status = read_data(Vcb, dr->address + (off << Vcb->sector_shift), rl << Vcb->sector_shift, NULL, false, data,
|
2019-09-01 12:53:20 +00:00
|
|
|
c, NULL, NULL, 0, false, NormalPagePriority);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("read_data returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
Status = write_data_complete(Vcb, dr->new_address + (off << Vcb->sector_shift), data, rl << Vcb->sector_shift,
|
2019-09-01 12:53:20 +00:00
|
|
|
NULL, newchunk, false, 0, NormalPagePriority);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("write_data_complete returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
size -= rl;
|
|
|
|
off += rl;
|
|
|
|
} while (size > 0);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(data);
|
|
|
|
data = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = write_metadata_items(Vcb, &metadata_items, &items, NULL, &rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("write_metadata_items returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = items.Flink;
|
|
|
|
while (le != &items) {
|
|
|
|
data_reloc* dr = CONTAINING_RECORD(le, data_reloc, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = add_data_reloc_extent_item(Vcb, dr);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_data_reloc_extent_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = c->changed_extents.Flink;
|
|
|
|
while (le != &c->changed_extents) {
|
|
|
|
LIST_ENTRY *le2, *le3;
|
|
|
|
changed_extent* ce = CONTAINING_RECORD(le, changed_extent, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le3 = le->Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = items.Flink;
|
|
|
|
while (le2 != &items) {
|
|
|
|
data_reloc* dr = CONTAINING_RECORD(le2, data_reloc, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ce->address == dr->address) {
|
|
|
|
ce->address = dr->new_address;
|
|
|
|
RemoveEntryList(&ce->list_entry);
|
|
|
|
InsertTailList(&dr->newchunk->changed_extents, &ce->list_entry);
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le3;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->need_write = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
end:
|
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
// update extents in cache inodes before we flush
|
|
|
|
le = Vcb->chunks.Flink;
|
|
|
|
while (le != &Vcb->chunks) {
|
|
|
|
chunk* c2 = CONTAINING_RECORD(le, chunk, list_entry);
|
|
|
|
|
|
|
|
if (c2->cache) {
|
|
|
|
LIST_ENTRY* le2;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(c2->cache->Header.Resource, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le2 = c2->cache->extents.Flink;
|
|
|
|
while (le2 != &c2->cache->extents) {
|
|
|
|
extent* ext = CONTAINING_RECORD(le2, extent, list_entry);
|
|
|
|
|
|
|
|
if (!ext->ignore) {
|
|
|
|
if (ext->extent_data.type == EXTENT_TYPE_REGULAR || ext->extent_data.type == EXTENT_TYPE_PREALLOC) {
|
|
|
|
EXTENT_DATA2* ed2 = (EXTENT_DATA2*)ext->extent_data.data;
|
|
|
|
|
|
|
|
if (ed2->size > 0 && ed2->address >= c->offset && ed2->address < c->offset + c->chunk_item->size) {
|
|
|
|
LIST_ENTRY* le3 = items.Flink;
|
|
|
|
while (le3 != &items) {
|
|
|
|
data_reloc* dr = CONTAINING_RECORD(le3, data_reloc, list_entry);
|
|
|
|
|
|
|
|
if (ed2->address == dr->address) {
|
|
|
|
ed2->address = dr->new_address;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
le3 = le3->Flink;
|
|
|
|
}
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le2 = le2->Flink;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExReleaseResourceLite(c2->cache->Header.Resource);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = le->Flink;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = do_write(Vcb, NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
clear_rollback(&rollback);
|
|
|
|
|
|
|
|
// update open FCBs
|
|
|
|
// FIXME - speed this up(?)
|
|
|
|
|
|
|
|
le = Vcb->all_fcbs.Flink;
|
|
|
|
while (le != &Vcb->all_fcbs) {
|
|
|
|
struct _fcb* fcb = CONTAINING_RECORD(le, struct _fcb, list_entry_all);
|
|
|
|
LIST_ENTRY* le2;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(fcb->Header.Resource, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le2 = fcb->extents.Flink;
|
|
|
|
while (le2 != &fcb->extents) {
|
|
|
|
extent* ext = CONTAINING_RECORD(le2, extent, list_entry);
|
|
|
|
|
|
|
|
if (!ext->ignore) {
|
|
|
|
if (ext->extent_data.type == EXTENT_TYPE_REGULAR || ext->extent_data.type == EXTENT_TYPE_PREALLOC) {
|
|
|
|
EXTENT_DATA2* ed2 = (EXTENT_DATA2*)ext->extent_data.data;
|
|
|
|
|
|
|
|
if (ed2->size > 0 && ed2->address >= c->offset && ed2->address < c->offset + c->chunk_item->size) {
|
|
|
|
LIST_ENTRY* le3 = items.Flink;
|
|
|
|
while (le3 != &items) {
|
|
|
|
data_reloc* dr = CONTAINING_RECORD(le3, data_reloc, list_entry);
|
|
|
|
|
|
|
|
if (ed2->address == dr->address) {
|
|
|
|
ed2->address = dr->new_address;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
le3 = le3->Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
le2 = le2->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(fcb->Header.Resource);
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
} else
|
2017-01-01 17:12:12 +00:00
|
|
|
do_rollback(Vcb, &rollback);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
free_trees(Vcb);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (data)
|
|
|
|
ExFreePool(data);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (!IsListEmpty(&items)) {
|
|
|
|
data_reloc* dr = CONTAINING_RECORD(RemoveHeadList(&items), data_reloc, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (!IsListEmpty(&dr->refs)) {
|
|
|
|
data_reloc_ref* ref = CONTAINING_RECORD(RemoveHeadList(&dr->refs), data_reloc_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(ref);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(dr);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (!IsListEmpty(&metadata_items)) {
|
|
|
|
metadata_reloc* mr = CONTAINING_RECORD(RemoveHeadList(&metadata_items), metadata_reloc, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (!IsListEmpty(&mr->refs)) {
|
|
|
|
metadata_reloc_ref* ref = CONTAINING_RECORD(RemoveHeadList(&mr->refs), metadata_reloc_ref, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(ref);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-11-12 18:32:46 +00:00
|
|
|
if (mr->data)
|
|
|
|
ExFreePool(mr->data);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(mr);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static __inline uint64_t get_chunk_dup_type(chunk* c) {
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c->chunk_item->type & BLOCK_FLAG_RAID0)
|
|
|
|
return BLOCK_FLAG_RAID0;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID1)
|
|
|
|
return BLOCK_FLAG_RAID1;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_DUPLICATE)
|
|
|
|
return BLOCK_FLAG_DUPLICATE;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID10)
|
|
|
|
return BLOCK_FLAG_RAID10;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID5)
|
|
|
|
return BLOCK_FLAG_RAID5;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID6)
|
|
|
|
return BLOCK_FLAG_RAID6;
|
2020-04-23 02:38:57 +00:00
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID1C3)
|
|
|
|
return BLOCK_FLAG_RAID1C3;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID1C4)
|
|
|
|
return BLOCK_FLAG_RAID1C4;
|
2017-01-01 17:12:12 +00:00
|
|
|
else
|
|
|
|
return BLOCK_FLAG_SINGLE;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static bool should_balance_chunk(device_extension* Vcb, uint8_t sort, chunk* c) {
|
2017-01-01 17:12:12 +00:00
|
|
|
btrfs_balance_opts* opts;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
opts = &Vcb->balance.opts[sort];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!(opts->flags & BTRFS_BALANCE_OPTS_ENABLED))
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_PROFILES) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t type = get_chunk_dup_type(c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!(type & opts->profiles))
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_DEVID) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t i;
|
2017-01-01 17:12:12 +00:00
|
|
|
CHUNK_ITEM_STRIPE* cis = (CHUNK_ITEM_STRIPE*)&c->chunk_item[1];
|
2019-09-01 12:53:20 +00:00
|
|
|
bool b = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (i = 0; i < c->chunk_item->num_stripes; i++) {
|
|
|
|
if (cis[i].dev_id == opts->devid) {
|
2019-09-01 12:53:20 +00:00
|
|
|
b = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!b)
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_DRANGE) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t i, factor;
|
|
|
|
uint64_t physsize;
|
2017-01-01 17:12:12 +00:00
|
|
|
CHUNK_ITEM_STRIPE* cis = (CHUNK_ITEM_STRIPE*)&c->chunk_item[1];
|
2019-09-01 12:53:20 +00:00
|
|
|
bool b = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c->chunk_item->type & BLOCK_FLAG_RAID0)
|
|
|
|
factor = c->chunk_item->num_stripes;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID10)
|
|
|
|
factor = c->chunk_item->num_stripes / c->chunk_item->sub_stripes;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID5)
|
|
|
|
factor = c->chunk_item->num_stripes - 1;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID6)
|
|
|
|
factor = c->chunk_item->num_stripes - 2;
|
2020-04-23 02:38:57 +00:00
|
|
|
else // SINGLE, DUPLICATE, RAID1, RAID1C3, RAID1C4
|
2017-01-01 17:12:12 +00:00
|
|
|
factor = 1;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
physsize = c->chunk_item->size / factor;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (i = 0; i < c->chunk_item->num_stripes; i++) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if (cis[i].offset < opts->drange_end && cis[i].offset + physsize >= opts->drange_start &&
|
|
|
|
(!(opts->flags & BTRFS_BALANCE_OPTS_DEVID) || cis[i].dev_id == opts->devid)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
b = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!b)
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_VRANGE) {
|
|
|
|
if (c->offset + c->chunk_item->size <= opts->vrange_start || c->offset > opts->vrange_end)
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_STRIPES) {
|
|
|
|
if (c->chunk_item->num_stripes < opts->stripes_start || c->chunk_item->num_stripes < opts->stripes_end)
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_USAGE) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t usage = c->used * 100 / c->chunk_item->size;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// usage == 0 should mean completely empty, not just that usage rounds to 0%
|
|
|
|
if (c->used > 0 && usage == 0)
|
|
|
|
usage = 1;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (usage < opts->usage_start || usage > opts->usage_end)
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_CONVERT && opts->flags & BTRFS_BALANCE_OPTS_SOFT) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t type = get_chunk_dup_type(c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (type == opts->convert)
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
return true;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_balance_args(btrfs_balance_opts* opts, BALANCE_ARGS* args) {
|
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_PROFILES) {
|
|
|
|
args->profiles = opts->profiles;
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_PROFILES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_USAGE) {
|
|
|
|
if (args->usage_start == 0) {
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_USAGE_RANGE;
|
|
|
|
args->usage_start = opts->usage_start;
|
|
|
|
args->usage_end = opts->usage_end;
|
|
|
|
} else {
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_USAGE;
|
|
|
|
args->usage = opts->usage_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_DEVID) {
|
|
|
|
args->devid = opts->devid;
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_DEVID;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_DRANGE) {
|
|
|
|
args->drange_start = opts->drange_start;
|
|
|
|
args->drange_end = opts->drange_end;
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_DRANGE;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_VRANGE) {
|
|
|
|
args->vrange_start = opts->vrange_start;
|
|
|
|
args->vrange_end = opts->vrange_end;
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_VRANGE;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_CONVERT) {
|
|
|
|
args->convert = opts->convert;
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_CONVERT;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_SOFT)
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_SOFT;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_LIMIT) {
|
|
|
|
if (args->limit_start == 0) {
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_LIMIT_RANGE;
|
2019-09-01 12:53:20 +00:00
|
|
|
args->limit_start = (uint32_t)opts->limit_start;
|
|
|
|
args->limit_end = (uint32_t)opts->limit_end;
|
2017-01-01 17:12:12 +00:00
|
|
|
} else {
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_LIMIT;
|
|
|
|
args->limit = opts->limit_end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts->flags & BTRFS_BALANCE_OPTS_STRIPES) {
|
|
|
|
args->stripes_start = opts->stripes_start;
|
|
|
|
args->stripes_end = opts->stripes_end;
|
|
|
|
args->flags |= BALANCE_ARGS_FLAGS_STRIPES_RANGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS add_balance_item(device_extension* Vcb) {
|
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
NTSTATUS Status;
|
|
|
|
BALANCE_ITEM* bi;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
searchkey.obj_id = BALANCE_ITEM_ID;
|
|
|
|
searchkey.obj_type = TYPE_TEMP_ITEM;
|
|
|
|
searchkey.offset = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->root_root, &tp, &searchkey, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!keycmp(tp.item->key, searchkey)) {
|
|
|
|
Status = delete_tree_item(Vcb, &tp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
bi = ExAllocatePoolWithTag(PagedPool, sizeof(BALANCE_ITEM), ALLOC_TAG);
|
|
|
|
if (!bi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RtlZeroMemory(bi, sizeof(BALANCE_ITEM));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.opts[BALANCE_OPTS_DATA].flags & BTRFS_BALANCE_OPTS_ENABLED) {
|
|
|
|
bi->flags |= BALANCE_FLAGS_DATA;
|
|
|
|
copy_balance_args(&Vcb->balance.opts[BALANCE_OPTS_DATA], &bi->data);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.opts[BALANCE_OPTS_METADATA].flags & BTRFS_BALANCE_OPTS_ENABLED) {
|
|
|
|
bi->flags |= BALANCE_FLAGS_METADATA;
|
|
|
|
copy_balance_args(&Vcb->balance.opts[BALANCE_OPTS_METADATA], &bi->metadata);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.opts[BALANCE_OPTS_SYSTEM].flags & BTRFS_BALANCE_OPTS_ENABLED) {
|
|
|
|
bi->flags |= BALANCE_FLAGS_SYSTEM;
|
|
|
|
copy_balance_args(&Vcb->balance.opts[BALANCE_OPTS_SYSTEM], &bi->system);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->root_root, BALANCE_ITEM_ID, TYPE_TEMP_ITEM, 0, bi, sizeof(BALANCE_ITEM), NULL, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(bi);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
end:
|
|
|
|
if (NT_SUCCESS(Status)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = do_write(Vcb, NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free_trees(Vcb);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS remove_balance_item(device_extension* Vcb) {
|
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
searchkey.obj_id = BALANCE_ITEM_ID;
|
|
|
|
searchkey.obj_type = TYPE_TEMP_ITEM;
|
|
|
|
searchkey.offset = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->root_root, &tp, &searchkey, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!keycmp(tp.item->key, searchkey)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = delete_tree_item(Vcb, &tp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = do_write(Vcb, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
free_trees(Vcb);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
end:
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void load_balance_args(btrfs_balance_opts* opts, BALANCE_ARGS* args) {
|
|
|
|
opts->flags = BTRFS_BALANCE_OPTS_ENABLED;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (args->flags & BALANCE_ARGS_FLAGS_PROFILES) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_PROFILES;
|
|
|
|
opts->profiles = args->profiles;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (args->flags & BALANCE_ARGS_FLAGS_USAGE) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_USAGE;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
opts->usage_start = 0;
|
2019-09-01 12:53:20 +00:00
|
|
|
opts->usage_end = (uint8_t)args->usage;
|
2017-01-01 17:12:12 +00:00
|
|
|
} else if (args->flags & BALANCE_ARGS_FLAGS_USAGE_RANGE) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_USAGE;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
opts->usage_start = (uint8_t)args->usage_start;
|
|
|
|
opts->usage_end = (uint8_t)args->usage_end;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (args->flags & BALANCE_ARGS_FLAGS_DEVID) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_DEVID;
|
|
|
|
opts->devid = args->devid;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (args->flags & BALANCE_ARGS_FLAGS_DRANGE) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_DRANGE;
|
|
|
|
opts->drange_start = args->drange_start;
|
|
|
|
opts->drange_end = args->drange_end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (args->flags & BALANCE_ARGS_FLAGS_VRANGE) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_VRANGE;
|
|
|
|
opts->vrange_start = args->vrange_start;
|
|
|
|
opts->vrange_end = args->vrange_end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (args->flags & BALANCE_ARGS_FLAGS_LIMIT) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_LIMIT;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
opts->limit_start = 0;
|
|
|
|
opts->limit_end = args->limit;
|
|
|
|
} else if (args->flags & BALANCE_ARGS_FLAGS_LIMIT_RANGE) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_LIMIT;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
opts->limit_start = args->limit_start;
|
|
|
|
opts->limit_end = args->limit_end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (args->flags & BALANCE_ARGS_FLAGS_STRIPES_RANGE) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_STRIPES;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
opts->stripes_start = (uint16_t)args->stripes_start;
|
|
|
|
opts->stripes_end = (uint16_t)args->stripes_end;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (args->flags & BALANCE_ARGS_FLAGS_CONVERT) {
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_CONVERT;
|
|
|
|
opts->convert = args->convert;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (args->flags & BALANCE_ARGS_FLAGS_SOFT)
|
|
|
|
opts->flags |= BTRFS_BALANCE_OPTS_SOFT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS remove_superblocks(device* dev) {
|
|
|
|
NTSTATUS Status;
|
|
|
|
superblock* sb;
|
|
|
|
int i = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
sb = ExAllocatePoolWithTag(PagedPool, sizeof(superblock), ALLOC_TAG);
|
|
|
|
if (!sb) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RtlZeroMemory(sb, sizeof(superblock));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
while (superblock_addrs[i] > 0 && dev->devitem.num_bytes >= superblock_addrs[i] + sizeof(superblock)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = write_data_phys(dev->devobj, dev->fileobj, superblock_addrs[i], sb, sizeof(superblock));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
ExFreePool(sb);
|
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
ExFreePool(sb);
|
2017-01-01 17:12:12 +00:00
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
return STATUS_SUCCESS;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS finish_removing_device(_Requires_exclusive_lock_held_(_Curr_->tree_lock) device_extension* Vcb, device* dev) {
|
2017-01-01 17:12:12 +00:00
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
LIST_ENTRY* le;
|
|
|
|
volume_device_extension* vde;
|
|
|
|
|
|
|
|
if (Vcb->need_write) {
|
|
|
|
Status = do_write(Vcb, NULL);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
} else
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
free_trees(Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// remove entry in chunk tree
|
|
|
|
|
|
|
|
searchkey.obj_id = 1;
|
|
|
|
searchkey.obj_type = TYPE_DEV_ITEM;
|
|
|
|
searchkey.offset = dev->devitem.dev_id;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->chunk_root, &tp, &searchkey, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!keycmp(searchkey, tp.item->key)) {
|
|
|
|
Status = delete_tree_item(Vcb, &tp);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// remove stats entry in device tree
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
searchkey.obj_id = 0;
|
|
|
|
searchkey.obj_type = TYPE_DEV_STATS;
|
|
|
|
searchkey.offset = dev->devitem.dev_id;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->dev_root, &tp, &searchkey, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!keycmp(searchkey, tp.item->key)) {
|
|
|
|
Status = delete_tree_item(Vcb, &tp);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// update superblock
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->superblock.num_devices--;
|
|
|
|
Vcb->superblock.total_bytes -= dev->devitem.num_bytes;
|
|
|
|
Vcb->devices_loaded--;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RemoveEntryList(&dev->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// flush
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = do_write(Vcb, NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
free_trees(Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return Status;
|
|
|
|
|
|
|
|
if (!dev->readonly && dev->devobj) {
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = remove_superblocks(dev);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("remove_superblocks returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// remove entry in volume list
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
vde = Vcb->vde;
|
|
|
|
|
|
|
|
if (dev->devobj) {
|
|
|
|
pdo_device_extension* pdode = vde->pdode;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&pdode->child_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = pdode->children.Flink;
|
|
|
|
while (le != &pdode->children) {
|
|
|
|
volume_child* vc = CONTAINING_RECORD(le, volume_child, list_entry);
|
|
|
|
|
|
|
|
if (RtlCompareMemory(&dev->devitem.device_uuid, &vc->uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
|
|
|
|
PFILE_OBJECT FileObject;
|
|
|
|
PDEVICE_OBJECT mountmgr;
|
|
|
|
UNICODE_STRING mmdevpath;
|
|
|
|
|
|
|
|
pdode->children_loaded--;
|
|
|
|
|
|
|
|
if (vc->had_drive_letter) { // re-add entry to mountmgr
|
|
|
|
RtlInitUnicodeString(&mmdevpath, MOUNTMGR_DEVICE_NAME);
|
|
|
|
Status = IoGetDeviceObjectPointer(&mmdevpath, FILE_READ_ATTRIBUTES, &FileObject, &mountmgr);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("IoGetDeviceObjectPointer returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
else {
|
|
|
|
MOUNTDEV_NAME mdn;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = dev_ioctl(dev->devobj, IOCTL_MOUNTDEV_QUERY_DEVICE_NAME, NULL, 0, &mdn, sizeof(MOUNTDEV_NAME), true, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_OVERFLOW)
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("IOCTL_MOUNTDEV_QUERY_DEVICE_NAME returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
else {
|
|
|
|
MOUNTDEV_NAME* mdn2;
|
|
|
|
ULONG mdnsize = (ULONG)offsetof(MOUNTDEV_NAME, Name[0]) + mdn.NameLength;
|
|
|
|
|
|
|
|
mdn2 = ExAllocatePoolWithTag(PagedPool, mdnsize, ALLOC_TAG);
|
|
|
|
if (!mdn2)
|
|
|
|
ERR("out of memory\n");
|
|
|
|
else {
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = dev_ioctl(dev->devobj, IOCTL_MOUNTDEV_QUERY_DEVICE_NAME, NULL, 0, mdn2, mdnsize, true, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("IOCTL_MOUNTDEV_QUERY_DEVICE_NAME returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
else {
|
|
|
|
UNICODE_STRING name;
|
|
|
|
|
|
|
|
name.Buffer = mdn2->Name;
|
|
|
|
name.Length = name.MaximumLength = mdn2->NameLength;
|
|
|
|
|
|
|
|
Status = mountmgr_add_drive_letter(mountmgr, &name);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("mountmgr_add_drive_letter returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExFreePool(mdn2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ObDereferenceObject(FileObject);
|
|
|
|
}
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExFreePool(vc->pnp_name.Buffer);
|
|
|
|
RemoveEntryList(&vc->list_entry);
|
|
|
|
ExFreePool(vc);
|
|
|
|
|
|
|
|
ObDereferenceObject(vc->fileobj);
|
|
|
|
|
|
|
|
break;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = le->Flink;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (pdode->children_loaded > 0 && vde->device->Characteristics & FILE_REMOVABLE_MEDIA) {
|
|
|
|
vde->device->Characteristics &= ~FILE_REMOVABLE_MEDIA;
|
|
|
|
|
|
|
|
le = pdode->children.Flink;
|
|
|
|
while (le != &pdode->children) {
|
|
|
|
volume_child* vc = CONTAINING_RECORD(le, volume_child, list_entry);
|
|
|
|
|
|
|
|
if (vc->devobj->Characteristics & FILE_REMOVABLE_MEDIA) {
|
|
|
|
vde->device->Characteristics |= FILE_REMOVABLE_MEDIA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pdode->num_children = Vcb->superblock.num_devices;
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&pdode->child_lock);
|
|
|
|
|
|
|
|
// free dev
|
|
|
|
|
|
|
|
if (dev->trim && !dev->readonly && !Vcb->options.no_trim)
|
|
|
|
trim_whole_device(dev);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (!IsListEmpty(&dev->space)) {
|
|
|
|
LIST_ENTRY* le2 = RemoveHeadList(&dev->space);
|
|
|
|
space* s = CONTAINING_RECORD(le2, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(s);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(dev);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->trim) {
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->trim = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = Vcb->devices.Flink;
|
|
|
|
while (le != &Vcb->devices) {
|
|
|
|
device* dev2 = CONTAINING_RECORD(le, device, list_entry);
|
|
|
|
|
|
|
|
if (dev2->trim) {
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->trim = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FsRtlNotifyVolumeEvent(Vcb->root_file, FSRTL_VOLUME_CHANGE_SIZE);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static void trim_unalloc_space(_Requires_lock_held_(_Curr_->tree_lock) device_extension* Vcb, device* dev) {
|
|
|
|
DEVICE_MANAGE_DATA_SET_ATTRIBUTES* dmdsa;
|
|
|
|
DEVICE_DATA_SET_RANGE* ranges;
|
|
|
|
ULONG datalen, i;
|
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
NTSTATUS Status;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool b;
|
|
|
|
uint64_t lastoff = 0x100000; // don't TRIM the first megabyte, in case someone has been daft enough to install GRUB there
|
2017-09-08 08:02:43 +00:00
|
|
|
LIST_ENTRY* le;
|
|
|
|
|
|
|
|
dev->num_trim_entries = 0;
|
|
|
|
|
|
|
|
searchkey.obj_id = dev->devitem.dev_id;
|
|
|
|
searchkey.obj_type = TYPE_DEV_EXTENT;
|
|
|
|
searchkey.offset = 0;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->dev_root, &tp, &searchkey, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
traverse_ptr next_tp;
|
|
|
|
|
|
|
|
if (tp.item->key.obj_id == dev->devitem.dev_id && tp.item->key.obj_type == TYPE_DEV_EXTENT) {
|
|
|
|
if (tp.item->size >= sizeof(DEV_EXTENT)) {
|
|
|
|
DEV_EXTENT* de = (DEV_EXTENT*)tp.item->data;
|
|
|
|
|
|
|
|
if (tp.item->key.offset > lastoff)
|
|
|
|
add_trim_entry_avoid_sb(Vcb, dev, lastoff, tp.item->key.offset - lastoff);
|
|
|
|
|
|
|
|
lastoff = tp.item->key.offset + de->length;
|
|
|
|
} else {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("(%I64x,%x,%I64x) was %u bytes, expected %Iu\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, sizeof(DEV_EXTENT));
|
2017-09-08 08:02:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
b = find_next_item(Vcb, &tp, &next_tp, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
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 (lastoff < dev->devitem.num_bytes)
|
|
|
|
add_trim_entry_avoid_sb(Vcb, dev, lastoff, dev->devitem.num_bytes - lastoff);
|
|
|
|
|
|
|
|
if (dev->num_trim_entries == 0)
|
|
|
|
return;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
datalen = (ULONG)sector_align(sizeof(DEVICE_MANAGE_DATA_SET_ATTRIBUTES), sizeof(uint64_t)) + (dev->num_trim_entries * sizeof(DEVICE_DATA_SET_RANGE));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
dmdsa = ExAllocatePoolWithTag(PagedPool, datalen, ALLOC_TAG);
|
|
|
|
if (!dmdsa) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
dmdsa->Size = sizeof(DEVICE_MANAGE_DATA_SET_ATTRIBUTES);
|
|
|
|
dmdsa->Action = DeviceDsmAction_Trim;
|
|
|
|
dmdsa->Flags = DEVICE_DSM_FLAG_TRIM_NOT_FS_ALLOCATED;
|
|
|
|
dmdsa->ParameterBlockOffset = 0;
|
|
|
|
dmdsa->ParameterBlockLength = 0;
|
2019-09-01 12:53:20 +00:00
|
|
|
dmdsa->DataSetRangesOffset = (ULONG)sector_align(sizeof(DEVICE_MANAGE_DATA_SET_ATTRIBUTES), sizeof(uint64_t));
|
2017-09-08 08:02:43 +00:00
|
|
|
dmdsa->DataSetRangesLength = dev->num_trim_entries * sizeof(DEVICE_DATA_SET_RANGE);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ranges = (DEVICE_DATA_SET_RANGE*)((uint8_t*)dmdsa + dmdsa->DataSetRangesOffset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
le = dev->trim_list.Flink;
|
|
|
|
while (le != &dev->trim_list) {
|
|
|
|
space* s = CONTAINING_RECORD(le, space, list_entry);
|
|
|
|
|
|
|
|
ranges[i].StartingOffset = s->address;
|
|
|
|
ranges[i].LengthInBytes = s->size;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = dev_ioctl(dev->devobj, IOCTL_STORAGE_MANAGE_DATA_SET_ATTRIBUTES, dmdsa, datalen, NULL, 0, true, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("IOCTL_STORAGE_MANAGE_DATA_SET_ATTRIBUTES returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExFreePool(dmdsa);
|
|
|
|
|
|
|
|
end:
|
|
|
|
while (!IsListEmpty(&dev->trim_list)) {
|
|
|
|
space* s = CONTAINING_RECORD(RemoveHeadList(&dev->trim_list), space, list_entry);
|
|
|
|
ExFreePool(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->num_trim_entries = 0;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static NTSTATUS try_consolidation(device_extension* Vcb, uint64_t flags, chunk** newchunk) {
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS Status;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool changed;
|
2017-09-08 08:02:43 +00:00
|
|
|
LIST_ENTRY* le;
|
|
|
|
chunk* rc;
|
|
|
|
|
|
|
|
// FIXME - allow with metadata chunks?
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
while (true) {
|
2017-09-08 08:02:43 +00:00
|
|
|
rc = NULL;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceSharedLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceSharedLite(&Vcb->chunk_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
// choose the least-used chunk we haven't looked at yet
|
|
|
|
le = Vcb->chunks.Flink;
|
|
|
|
while (le != &Vcb->chunks) {
|
|
|
|
chunk* c = CONTAINING_RECORD(le, chunk, list_entry);
|
|
|
|
|
|
|
|
// FIXME - skip full-size chunks over e.g. 90% full?
|
|
|
|
if (c->chunk_item->type & BLOCK_FLAG_DATA && !c->readonly && c->balance_num != Vcb->balance.balance_num && (!rc || c->used < rc->used))
|
|
|
|
rc = c;
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
|
|
|
|
if (!rc) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc->list_entry_balance.Flink) {
|
|
|
|
RemoveEntryList(&rc->list_entry_balance);
|
|
|
|
Vcb->balance.chunks_left--;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc->list_entry_balance.Flink = (LIST_ENTRY*)1; // so it doesn't get dropped
|
2019-09-01 12:53:20 +00:00
|
|
|
rc->reloc = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
|
|
|
|
do {
|
2019-09-01 12:53:20 +00:00
|
|
|
changed = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = balance_data_chunk(Vcb, rc, &changed);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("balance_data_chunk returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
Vcb->balance.status = Status;
|
|
|
|
rc->list_entry_balance.Flink = NULL;
|
2019-09-01 12:53:20 +00:00
|
|
|
rc->reloc = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
KeWaitForSingleObject(&Vcb->balance.event, Executive, KernelMode, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->readonly)
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.stopping = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->balance.stopping)
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
} while (changed);
|
|
|
|
|
|
|
|
rc->list_entry_balance.Flink = NULL;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
rc->changed = true;
|
|
|
|
rc->space_changed = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
rc->balance_num = Vcb->balance.balance_num;
|
|
|
|
|
|
|
|
Status = do_write(Vcb, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2018-12-16 11:03:16 +00:00
|
|
|
|
|
|
|
free_trees(Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->chunk_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = alloc_chunk(Vcb, flags, &rc, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
|
|
|
|
if (NT_SUCCESS(Status)) {
|
|
|
|
*newchunk = rc;
|
|
|
|
return Status;
|
|
|
|
} else {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("alloc_chunk returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS regenerate_space_list(device_extension* Vcb, device* dev) {
|
|
|
|
LIST_ENTRY* le;
|
|
|
|
|
|
|
|
while (!IsListEmpty(&dev->space)) {
|
|
|
|
space* s = CONTAINING_RECORD(RemoveHeadList(&dev->space), space, list_entry);
|
|
|
|
|
|
|
|
ExFreePool(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Linux driver doesn't like to allocate chunks within the first megabyte of a device.
|
|
|
|
|
|
|
|
space_list_add2(&dev->space, NULL, 0x100000, dev->devitem.num_bytes - 0x100000, NULL, NULL);
|
|
|
|
|
|
|
|
le = Vcb->chunks.Flink;
|
|
|
|
while (le != &Vcb->chunks) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t n;
|
2017-09-08 08:02:43 +00:00
|
|
|
chunk* c = CONTAINING_RECORD(le, chunk, list_entry);
|
|
|
|
CHUNK_ITEM_STRIPE* cis = (CHUNK_ITEM_STRIPE*)&c->chunk_item[1];
|
|
|
|
|
|
|
|
for (n = 0; n < c->chunk_item->num_stripes; n++) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t stripe_size = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (cis[n].dev_id == dev->devitem.dev_id) {
|
|
|
|
if (stripe_size == 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint16_t factor;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (c->chunk_item->type & BLOCK_FLAG_RAID0)
|
|
|
|
factor = c->chunk_item->num_stripes;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID10)
|
|
|
|
factor = c->chunk_item->num_stripes / c->chunk_item->sub_stripes;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID5)
|
|
|
|
factor = c->chunk_item->num_stripes - 1;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_RAID6)
|
|
|
|
factor = c->chunk_item->num_stripes - 2;
|
2020-04-23 02:38:57 +00:00
|
|
|
else // SINGLE, DUP, RAID1, RAID1C3, RAID1C4
|
2017-09-08 08:02:43 +00:00
|
|
|
factor = 1;
|
|
|
|
|
|
|
|
stripe_size = c->chunk_item->size / factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
space_list_subtract2(&dev->space, NULL, cis[n].offset, stripe_size, NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
_Function_class_(KSTART_ROUTINE)
|
2019-09-01 12:53:20 +00:00
|
|
|
void __stdcall balance_thread(void* context) {
|
2017-01-01 17:12:12 +00:00
|
|
|
device_extension* Vcb = (device_extension*)context;
|
|
|
|
LIST_ENTRY chunks;
|
|
|
|
LIST_ENTRY* le;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t num_chunks[3], okay_metadata_chunks = 0, okay_data_chunks = 0, okay_system_chunks = 0;
|
|
|
|
uint64_t old_data_flags = 0, old_metadata_flags = 0, old_system_flags = 0;
|
2017-01-01 17:12:12 +00:00
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Vcb->balance.balance_num++;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.stopping = false;
|
|
|
|
KeInitializeEvent(&Vcb->balance.finished, NotificationEvent, false);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->balance.opts[BALANCE_OPTS_DATA].flags & BTRFS_BALANCE_OPTS_ENABLED && Vcb->balance.opts[BALANCE_OPTS_DATA].flags & BTRFS_BALANCE_OPTS_CONVERT) {
|
|
|
|
old_data_flags = Vcb->data_flags;
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->data_flags = BLOCK_FLAG_DATA | (Vcb->balance.opts[BALANCE_OPTS_DATA].convert == BLOCK_FLAG_SINGLE ? 0 : Vcb->balance.opts[BALANCE_OPTS_DATA].convert);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
FsRtlNotifyVolumeEvent(Vcb->root_file, FSRTL_VOLUME_CHANGE_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Vcb->balance.opts[BALANCE_OPTS_METADATA].flags & BTRFS_BALANCE_OPTS_ENABLED && Vcb->balance.opts[BALANCE_OPTS_METADATA].flags & BTRFS_BALANCE_OPTS_CONVERT) {
|
|
|
|
old_metadata_flags = Vcb->metadata_flags;
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->metadata_flags = BLOCK_FLAG_METADATA | (Vcb->balance.opts[BALANCE_OPTS_METADATA].convert == BLOCK_FLAG_SINGLE ? 0 : Vcb->balance.opts[BALANCE_OPTS_METADATA].convert);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Vcb->balance.opts[BALANCE_OPTS_SYSTEM].flags & BTRFS_BALANCE_OPTS_ENABLED && Vcb->balance.opts[BALANCE_OPTS_SYSTEM].flags & BTRFS_BALANCE_OPTS_CONVERT) {
|
|
|
|
old_system_flags = Vcb->system_flags;
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->system_flags = BLOCK_FLAG_SYSTEM | (Vcb->balance.opts[BALANCE_OPTS_SYSTEM].convert == BLOCK_FLAG_SINGLE ? 0 : Vcb->balance.opts[BALANCE_OPTS_SYSTEM].convert);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->superblock.incompat_flags & BTRFS_INCOMPAT_FLAGS_MIXED_GROUPS) {
|
|
|
|
if (Vcb->balance.opts[BALANCE_OPTS_DATA].flags & BTRFS_BALANCE_OPTS_ENABLED)
|
|
|
|
RtlCopyMemory(&Vcb->balance.opts[BALANCE_OPTS_METADATA], &Vcb->balance.opts[BALANCE_OPTS_DATA], sizeof(btrfs_balance_opts));
|
|
|
|
else if (Vcb->balance.opts[BALANCE_OPTS_METADATA].flags & BTRFS_BALANCE_OPTS_ENABLED)
|
|
|
|
RtlCopyMemory(&Vcb->balance.opts[BALANCE_OPTS_DATA], &Vcb->balance.opts[BALANCE_OPTS_METADATA], sizeof(btrfs_balance_opts));
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
num_chunks[0] = num_chunks[1] = num_chunks[2] = 0;
|
|
|
|
Vcb->balance.total_chunks = Vcb->balance.chunks_left = 0;
|
|
|
|
|
|
|
|
InitializeListHead(&chunks);
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// FIXME - what are we supposed to do with limit_start?
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!Vcb->readonly) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!Vcb->balance.removing && !Vcb->balance.shrinking) {
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_balance_item(Vcb);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_balance_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
Vcb->balance.status = Status;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Vcb->need_write) {
|
|
|
|
Status = do_write(Vcb, NULL);
|
|
|
|
|
|
|
|
free_trees(Vcb);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
Vcb->balance.status = Status;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
KeWaitForSingleObject(&Vcb->balance.event, Executive, KernelMode, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->balance.stopping)
|
|
|
|
goto end;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceSharedLite(&Vcb->chunk_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = Vcb->chunks.Flink;
|
|
|
|
while (le != &Vcb->chunks) {
|
|
|
|
chunk* c = CONTAINING_RECORD(le, chunk, list_entry);
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t sort;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (c->chunk_item->type & BLOCK_FLAG_DATA)
|
|
|
|
sort = BALANCE_OPTS_DATA;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_METADATA)
|
|
|
|
sort = BALANCE_OPTS_METADATA;
|
|
|
|
else if (c->chunk_item->type & BLOCK_FLAG_SYSTEM)
|
|
|
|
sort = BALANCE_OPTS_SYSTEM;
|
|
|
|
else {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("unexpected chunk type %I64x\n", c->chunk_item->type);
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!(Vcb->balance.opts[sort].flags & BTRFS_BALANCE_OPTS_LIMIT) || num_chunks[sort] < Vcb->balance.opts[sort].limit_end) &&
|
|
|
|
should_balance_chunk(Vcb, sort, c)) {
|
|
|
|
InsertTailList(&chunks, &c->list_entry_balance);
|
|
|
|
|
|
|
|
num_chunks[sort]++;
|
|
|
|
Vcb->balance.total_chunks++;
|
|
|
|
Vcb->balance.chunks_left++;
|
|
|
|
} else if (sort == BALANCE_OPTS_METADATA)
|
|
|
|
okay_metadata_chunks++;
|
|
|
|
else if (sort == BALANCE_OPTS_DATA)
|
|
|
|
okay_data_chunks++;
|
|
|
|
else if (sort == BALANCE_OPTS_SYSTEM)
|
|
|
|
okay_system_chunks++;
|
|
|
|
|
|
|
|
if (!c->cache_loaded) {
|
|
|
|
Status = load_cache_chunk(Vcb, c, NULL);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("load_cache_chunk returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
Vcb->balance.status = Status;
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
|
|
|
|
// If we're doing a full balance, try and allocate a new chunk now, before we mess things up
|
|
|
|
if (okay_metadata_chunks == 0 || okay_data_chunks == 0 || okay_system_chunks == 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
bool consolidated = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
chunk* c;
|
|
|
|
|
|
|
|
if (okay_metadata_chunks == 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->chunk_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = alloc_chunk(Vcb, Vcb->metadata_flags, &c, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
c->balance_num = Vcb->balance.balance_num;
|
|
|
|
else if (Status != STATUS_DISK_FULL || consolidated) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("alloc_chunk returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
Vcb->balance.status = Status;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
|
|
|
|
if (Status == STATUS_DISK_FULL) {
|
|
|
|
Status = try_consolidation(Vcb, Vcb->metadata_flags, &c);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("try_consolidation returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
Vcb->balance.status = Status;
|
|
|
|
goto end;
|
|
|
|
} else
|
|
|
|
c->balance_num = Vcb->balance.balance_num;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
consolidated = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->balance.stopping)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (okay_data_chunks == 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->chunk_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = alloc_chunk(Vcb, Vcb->data_flags, &c, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
c->balance_num = Vcb->balance.balance_num;
|
|
|
|
else if (Status != STATUS_DISK_FULL || consolidated) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("alloc_chunk returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
Vcb->balance.status = Status;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
|
|
|
|
if (Status == STATUS_DISK_FULL) {
|
|
|
|
Status = try_consolidation(Vcb, Vcb->data_flags, &c);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("try_consolidation returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
Vcb->balance.status = Status;
|
|
|
|
goto end;
|
|
|
|
} else
|
|
|
|
c->balance_num = Vcb->balance.balance_num;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
consolidated = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->balance.stopping)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (okay_system_chunks == 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->chunk_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = alloc_chunk(Vcb, Vcb->system_flags, &c, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
c->balance_num = Vcb->balance.balance_num;
|
|
|
|
else if (Status != STATUS_DISK_FULL || consolidated) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("alloc_chunk returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
Vcb->balance.status = Status;
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
|
|
|
|
if (Status == STATUS_DISK_FULL) {
|
|
|
|
Status = try_consolidation(Vcb, Vcb->system_flags, &c);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("try_consolidation returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
Vcb->balance.status = Status;
|
|
|
|
goto end;
|
|
|
|
} else
|
|
|
|
c->balance_num = Vcb->balance.balance_num;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
consolidated = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->balance.stopping)
|
|
|
|
goto end;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceSharedLite(&Vcb->chunk_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = chunks.Flink;
|
|
|
|
while (le != &chunks) {
|
|
|
|
chunk* c = CONTAINING_RECORD(le, chunk, list_entry_balance);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
c->reloc = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// do data chunks before metadata
|
|
|
|
le = chunks.Flink;
|
|
|
|
while (le != &chunks) {
|
|
|
|
chunk* c = CONTAINING_RECORD(le, chunk, list_entry_balance);
|
|
|
|
LIST_ENTRY* le2 = le->Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c->chunk_item->type & BLOCK_FLAG_DATA) {
|
2019-09-01 12:53:20 +00:00
|
|
|
bool changed;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
do {
|
2019-09-01 12:53:20 +00:00
|
|
|
changed = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = balance_data_chunk(Vcb, c, &changed);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("balance_data_chunk returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->balance.status = Status;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
KeWaitForSingleObject(&Vcb->balance.event, Executive, KernelMode, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->readonly)
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.stopping = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.stopping)
|
|
|
|
break;
|
|
|
|
} while (changed);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
c->changed = true;
|
|
|
|
c->space_changed = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->balance.stopping)
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c->chunk_item->type & BLOCK_FLAG_DATA &&
|
|
|
|
(!(Vcb->balance.opts[BALANCE_OPTS_METADATA].flags & BTRFS_BALANCE_OPTS_ENABLED) || !(c->chunk_item->type & BLOCK_FLAG_METADATA))) {
|
|
|
|
RemoveEntryList(&c->list_entry_balance);
|
|
|
|
c->list_entry_balance.Flink = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->balance.chunks_left--;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le2;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// do metadata chunks
|
|
|
|
while (!IsListEmpty(&chunks)) {
|
|
|
|
chunk* c;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool changed;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = RemoveHeadList(&chunks);
|
|
|
|
c = CONTAINING_RECORD(le, chunk, list_entry_balance);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c->chunk_item->type & BLOCK_FLAG_METADATA || c->chunk_item->type & BLOCK_FLAG_SYSTEM) {
|
|
|
|
do {
|
|
|
|
Status = balance_metadata_chunk(Vcb, c, &changed);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("balance_metadata_chunk returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->balance.status = Status;
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
KeWaitForSingleObject(&Vcb->balance.event, Executive, KernelMode, false, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->readonly)
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.stopping = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.stopping)
|
|
|
|
break;
|
|
|
|
} while (changed);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
c->changed = true;
|
|
|
|
c->space_changed = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->balance.stopping)
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
c->list_entry_balance.Flink = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->balance.chunks_left--;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
end:
|
|
|
|
if (!Vcb->readonly) {
|
2017-09-08 08:02:43 +00:00
|
|
|
if (Vcb->balance.stopping || !NT_SUCCESS(Vcb->balance.status)) {
|
|
|
|
le = chunks.Flink;
|
|
|
|
while (le != &chunks) {
|
|
|
|
chunk* c = CONTAINING_RECORD(le, chunk, list_entry_balance);
|
2019-09-01 12:53:20 +00:00
|
|
|
c->reloc = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
c->list_entry_balance.Flink = NULL;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (old_data_flags != 0)
|
|
|
|
Vcb->data_flags = old_data_flags;
|
|
|
|
|
|
|
|
if (old_metadata_flags != 0)
|
|
|
|
Vcb->metadata_flags = old_metadata_flags;
|
|
|
|
|
|
|
|
if (old_system_flags != 0)
|
|
|
|
Vcb->system_flags = old_system_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Vcb->balance.removing) {
|
2017-01-01 17:12:12 +00:00
|
|
|
device* dev = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = Vcb->devices.Flink;
|
|
|
|
while (le != &Vcb->devices) {
|
|
|
|
device* dev2 = CONTAINING_RECORD(le, device, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (dev2->devitem.dev_id == Vcb->balance.opts[0].devid) {
|
|
|
|
dev = dev2;
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (dev) {
|
|
|
|
if (Vcb->balance.chunks_left == 0) {
|
|
|
|
Status = finish_removing_device(Vcb, dev);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("finish_removing_device returned %08lx\n", Status);
|
2019-09-01 12:53:20 +00:00
|
|
|
dev->reloc = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
} else
|
2019-09-01 12:53:20 +00:00
|
|
|
dev->reloc = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
} else if (Vcb->balance.shrinking) {
|
|
|
|
device* dev = NULL;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = Vcb->devices.Flink;
|
|
|
|
while (le != &Vcb->devices) {
|
|
|
|
device* dev2 = CONTAINING_RECORD(le, device, list_entry);
|
|
|
|
|
|
|
|
if (dev2->devitem.dev_id == Vcb->balance.opts[0].devid) {
|
|
|
|
dev = dev2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("could not find device %I64x\n", Vcb->balance.opts[0].devid);
|
2017-09-08 08:02:43 +00:00
|
|
|
Vcb->balance.status = STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Vcb->balance.stopping || !NT_SUCCESS(Vcb->balance.status)) {
|
|
|
|
if (dev) {
|
|
|
|
Status = regenerate_space_list(Vcb, dev);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("regenerate_space_list returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t old_size;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
old_size = dev->devitem.num_bytes;
|
|
|
|
dev->devitem.num_bytes = Vcb->balance.opts[0].drange_start;
|
|
|
|
|
|
|
|
Status = update_dev_item(Vcb, dev, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("update_dev_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
dev->devitem.num_bytes = old_size;
|
|
|
|
Vcb->balance.status = Status;
|
|
|
|
|
|
|
|
Status = regenerate_space_list(Vcb, dev);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("regenerate_space_list returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
} else {
|
|
|
|
Vcb->superblock.total_bytes -= old_size - dev->devitem.num_bytes;
|
|
|
|
|
|
|
|
Status = do_write(Vcb, NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
free_trees(Vcb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
|
|
|
|
if (!Vcb->balance.stopping && NT_SUCCESS(Vcb->balance.status))
|
|
|
|
FsRtlNotifyVolumeEvent(Vcb->root_file, FSRTL_VOLUME_CHANGE_SIZE);
|
|
|
|
} else {
|
|
|
|
Status = remove_balance_item(Vcb);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("remove_balance_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Vcb->trim && !Vcb->options.no_trim) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = Vcb->devices.Flink;
|
|
|
|
while (le != &Vcb->devices) {
|
|
|
|
device* dev2 = CONTAINING_RECORD(le, device, list_entry);
|
|
|
|
|
|
|
|
if (dev2->devobj && !dev2->readonly && dev2->trim)
|
|
|
|
trim_unalloc_space(Vcb, dev2);
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ZwClose(Vcb->balance.thread);
|
|
|
|
Vcb->balance.thread = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
KeSetEvent(&Vcb->balance.finished, 0, false);
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS start_balance(device_extension* Vcb, void* data, ULONG length, KPROCESSOR_MODE processor_mode) {
|
|
|
|
NTSTATUS Status;
|
|
|
|
btrfs_start_balance* bsb = (btrfs_start_balance*)data;
|
2019-11-12 18:32:46 +00:00
|
|
|
OBJECT_ATTRIBUTES oa;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint8_t i;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (length < sizeof(btrfs_start_balance) || !data)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!SeSinglePrivilegeCheck(RtlConvertLongToLuid(SE_MANAGE_VOLUME_PRIVILEGE), processor_mode))
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->locked) {
|
|
|
|
WARN("cannot start balance while locked\n");
|
|
|
|
return STATUS_DEVICE_NOT_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Vcb->scrub.thread) {
|
|
|
|
WARN("cannot start balance while scrub running\n");
|
|
|
|
return STATUS_DEVICE_NOT_READY;
|
|
|
|
}
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.thread) {
|
|
|
|
WARN("balance already running\n");
|
|
|
|
return STATUS_DEVICE_NOT_READY;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->readonly)
|
|
|
|
return STATUS_MEDIA_WRITE_PROTECTED;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!(bsb->opts[BALANCE_OPTS_DATA].flags & BTRFS_BALANCE_OPTS_ENABLED) &&
|
|
|
|
!(bsb->opts[BALANCE_OPTS_METADATA].flags & BTRFS_BALANCE_OPTS_ENABLED) &&
|
|
|
|
!(bsb->opts[BALANCE_OPTS_SYSTEM].flags & BTRFS_BALANCE_OPTS_ENABLED))
|
|
|
|
return STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (bsb->opts[i].flags & BTRFS_BALANCE_OPTS_ENABLED) {
|
|
|
|
if (bsb->opts[i].flags & BTRFS_BALANCE_OPTS_PROFILES) {
|
|
|
|
bsb->opts[i].profiles &= BLOCK_FLAG_RAID0 | BLOCK_FLAG_RAID1 | BLOCK_FLAG_DUPLICATE | BLOCK_FLAG_RAID10 |
|
2020-04-23 02:38:57 +00:00
|
|
|
BLOCK_FLAG_RAID5 | BLOCK_FLAG_RAID6 | BLOCK_FLAG_SINGLE | BLOCK_FLAG_RAID1C3 |
|
|
|
|
BLOCK_FLAG_RAID1C4;
|
2017-01-01 17:12:12 +00:00
|
|
|
|
|
|
|
if (bsb->opts[i].profiles == 0)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].flags & BTRFS_BALANCE_OPTS_DEVID) {
|
|
|
|
if (bsb->opts[i].devid == 0)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].flags & BTRFS_BALANCE_OPTS_DRANGE) {
|
|
|
|
if (bsb->opts[i].drange_start > bsb->opts[i].drange_end)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].flags & BTRFS_BALANCE_OPTS_VRANGE) {
|
|
|
|
if (bsb->opts[i].vrange_start > bsb->opts[i].vrange_end)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].flags & BTRFS_BALANCE_OPTS_LIMIT) {
|
|
|
|
bsb->opts[i].limit_start = max(1, bsb->opts[i].limit_start);
|
|
|
|
bsb->opts[i].limit_end = max(1, bsb->opts[i].limit_end);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].limit_start > bsb->opts[i].limit_end)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].flags & BTRFS_BALANCE_OPTS_STRIPES) {
|
|
|
|
bsb->opts[i].stripes_start = max(1, bsb->opts[i].stripes_start);
|
|
|
|
bsb->opts[i].stripes_end = max(1, bsb->opts[i].stripes_end);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].stripes_start > bsb->opts[i].stripes_end)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].flags & BTRFS_BALANCE_OPTS_USAGE) {
|
|
|
|
bsb->opts[i].usage_start = min(100, bsb->opts[i].stripes_start);
|
|
|
|
bsb->opts[i].usage_end = min(100, bsb->opts[i].stripes_end);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].stripes_start > bsb->opts[i].stripes_end)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bsb->opts[i].flags & BTRFS_BALANCE_OPTS_CONVERT) {
|
|
|
|
if (bsb->opts[i].convert != BLOCK_FLAG_RAID0 && bsb->opts[i].convert != BLOCK_FLAG_RAID1 &&
|
|
|
|
bsb->opts[i].convert != BLOCK_FLAG_DUPLICATE && bsb->opts[i].convert != BLOCK_FLAG_RAID10 &&
|
|
|
|
bsb->opts[i].convert != BLOCK_FLAG_RAID5 && bsb->opts[i].convert != BLOCK_FLAG_RAID6 &&
|
2020-04-23 02:38:57 +00:00
|
|
|
bsb->opts[i].convert != BLOCK_FLAG_SINGLE && bsb->opts[i].convert != BLOCK_FLAG_RAID1C3 &&
|
|
|
|
bsb->opts[i].convert != BLOCK_FLAG_RAID1C4)
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RtlCopyMemory(&Vcb->balance.opts[BALANCE_OPTS_DATA], &bsb->opts[BALANCE_OPTS_DATA], sizeof(btrfs_balance_opts));
|
|
|
|
RtlCopyMemory(&Vcb->balance.opts[BALANCE_OPTS_METADATA], &bsb->opts[BALANCE_OPTS_METADATA], sizeof(btrfs_balance_opts));
|
|
|
|
RtlCopyMemory(&Vcb->balance.opts[BALANCE_OPTS_SYSTEM], &bsb->opts[BALANCE_OPTS_SYSTEM], sizeof(btrfs_balance_opts));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.paused = false;
|
|
|
|
Vcb->balance.removing = false;
|
|
|
|
Vcb->balance.shrinking = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->balance.status = STATUS_SUCCESS;
|
|
|
|
KeInitializeEvent(&Vcb->balance.event, NotificationEvent, !Vcb->balance.paused);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-11-12 18:32:46 +00:00
|
|
|
InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
|
|
|
|
|
|
|
|
Status = PsCreateSystemThread(&Vcb->balance.thread, 0, &oa, NULL, NULL, balance_thread, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("PsCreateSystemThread returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS look_for_balance_item(_Requires_lock_held_(_Curr_->tree_lock) device_extension* Vcb) {
|
2017-01-01 17:12:12 +00:00
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
NTSTATUS Status;
|
|
|
|
BALANCE_ITEM* bi;
|
2019-11-12 18:32:46 +00:00
|
|
|
OBJECT_ATTRIBUTES oa;
|
2017-01-01 17:12:12 +00:00
|
|
|
int i;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
searchkey.obj_id = BALANCE_ITEM_ID;
|
|
|
|
searchkey.obj_type = TYPE_TEMP_ITEM;
|
|
|
|
searchkey.offset = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->root_root, &tp, &searchkey, false, NULL);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (keycmp(tp.item->key, searchkey)) {
|
|
|
|
TRACE("no balance item found\n");
|
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (tp.item->size < sizeof(BALANCE_ITEM)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("(%I64x,%x,%I64x) was %u bytes, expected %Iu\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset,
|
2017-01-01 17:12:12 +00:00
|
|
|
tp.item->size, sizeof(BALANCE_ITEM));
|
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
bi = (BALANCE_ITEM*)tp.item->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bi->flags & BALANCE_FLAGS_DATA)
|
|
|
|
load_balance_args(&Vcb->balance.opts[BALANCE_OPTS_DATA], &bi->data);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bi->flags & BALANCE_FLAGS_METADATA)
|
|
|
|
load_balance_args(&Vcb->balance.opts[BALANCE_OPTS_METADATA], &bi->metadata);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (bi->flags & BALANCE_FLAGS_SYSTEM)
|
|
|
|
load_balance_args(&Vcb->balance.opts[BALANCE_OPTS_SYSTEM], &bi->system);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// do the heuristics that Linux driver does
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (Vcb->balance.opts[i].flags & BTRFS_BALANCE_OPTS_ENABLED) {
|
|
|
|
// if converting, don't redo chunks already done
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.opts[i].flags & BTRFS_BALANCE_OPTS_CONVERT)
|
|
|
|
Vcb->balance.opts[i].flags |= BTRFS_BALANCE_OPTS_SOFT;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// don't balance chunks more than 90% filled - presumably these
|
|
|
|
// have already been done
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!(Vcb->balance.opts[i].flags & BTRFS_BALANCE_OPTS_USAGE) &&
|
|
|
|
!(Vcb->balance.opts[i].flags & BTRFS_BALANCE_OPTS_CONVERT)
|
|
|
|
) {
|
|
|
|
Vcb->balance.opts[i].flags |= BTRFS_BALANCE_OPTS_USAGE;
|
|
|
|
Vcb->balance.opts[i].usage_start = 0;
|
|
|
|
Vcb->balance.opts[i].usage_end = 90;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->readonly || Vcb->options.skip_balance)
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.paused = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
else
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.paused = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.removing = false;
|
|
|
|
Vcb->balance.shrinking = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->balance.status = STATUS_SUCCESS;
|
|
|
|
KeInitializeEvent(&Vcb->balance.event, NotificationEvent, !Vcb->balance.paused);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-11-12 18:32:46 +00:00
|
|
|
InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
|
|
|
|
|
|
|
|
Status = PsCreateSystemThread(&Vcb->balance.thread, 0, &oa, NULL, NULL, balance_thread, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("PsCreateSystemThread returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS query_balance(device_extension* Vcb, void* data, ULONG length) {
|
|
|
|
btrfs_query_balance* bqb = (btrfs_query_balance*)data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (length < sizeof(btrfs_query_balance) || !data)
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!Vcb->balance.thread) {
|
|
|
|
bqb->status = BTRFS_BALANCE_STOPPED;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Vcb->balance.status)) {
|
|
|
|
bqb->status |= BTRFS_BALANCE_ERROR;
|
|
|
|
bqb->error = Vcb->balance.status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
bqb->status = Vcb->balance.paused ? BTRFS_BALANCE_PAUSED : BTRFS_BALANCE_RUNNING;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.removing)
|
|
|
|
bqb->status |= BTRFS_BALANCE_REMOVAL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->balance.shrinking)
|
|
|
|
bqb->status |= BTRFS_BALANCE_SHRINKING;
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Vcb->balance.status))
|
|
|
|
bqb->status |= BTRFS_BALANCE_ERROR;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
bqb->chunks_left = Vcb->balance.chunks_left;
|
|
|
|
bqb->total_chunks = Vcb->balance.total_chunks;
|
|
|
|
bqb->error = Vcb->balance.status;
|
|
|
|
RtlCopyMemory(&bqb->data_opts, &Vcb->balance.opts[BALANCE_OPTS_DATA], sizeof(btrfs_balance_opts));
|
|
|
|
RtlCopyMemory(&bqb->metadata_opts, &Vcb->balance.opts[BALANCE_OPTS_METADATA], sizeof(btrfs_balance_opts));
|
|
|
|
RtlCopyMemory(&bqb->system_opts, &Vcb->balance.opts[BALANCE_OPTS_SYSTEM], sizeof(btrfs_balance_opts));
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS pause_balance(device_extension* Vcb, KPROCESSOR_MODE processor_mode) {
|
|
|
|
if (!SeSinglePrivilegeCheck(RtlConvertLongToLuid(SE_MANAGE_VOLUME_PRIVILEGE), processor_mode))
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!Vcb->balance.thread)
|
|
|
|
return STATUS_DEVICE_NOT_READY;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.paused)
|
|
|
|
return STATUS_DEVICE_NOT_READY;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.paused = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
KeClearEvent(&Vcb->balance.event);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS resume_balance(device_extension* Vcb, KPROCESSOR_MODE processor_mode) {
|
|
|
|
if (!SeSinglePrivilegeCheck(RtlConvertLongToLuid(SE_MANAGE_VOLUME_PRIVILEGE), processor_mode))
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!Vcb->balance.thread)
|
|
|
|
return STATUS_DEVICE_NOT_READY;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!Vcb->balance.paused)
|
|
|
|
return STATUS_DEVICE_NOT_READY;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->readonly)
|
|
|
|
return STATUS_MEDIA_WRITE_PROTECTED;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.paused = false;
|
|
|
|
KeSetEvent(&Vcb->balance.event, 0, false);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS stop_balance(device_extension* Vcb, KPROCESSOR_MODE processor_mode) {
|
|
|
|
if (!SeSinglePrivilegeCheck(RtlConvertLongToLuid(SE_MANAGE_VOLUME_PRIVILEGE), processor_mode))
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!Vcb->balance.thread)
|
|
|
|
return STATUS_DEVICE_NOT_READY;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.paused = false;
|
|
|
|
Vcb->balance.stopping = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
Vcb->balance.status = STATUS_SUCCESS;
|
2019-09-01 12:53:20 +00:00
|
|
|
KeSetEvent(&Vcb->balance.event, 0, false);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS remove_device(device_extension* Vcb, void* data, ULONG length, KPROCESSOR_MODE processor_mode) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t devid;
|
2017-01-01 17:12:12 +00:00
|
|
|
LIST_ENTRY* le;
|
|
|
|
device* dev = NULL;
|
|
|
|
NTSTATUS Status;
|
|
|
|
int i;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t num_rw_devices;
|
2019-11-12 18:32:46 +00:00
|
|
|
OBJECT_ATTRIBUTES oa;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2020-04-23 02:38:57 +00:00
|
|
|
TRACE("(%p, %p, %lx)\n", Vcb, data, length);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!SeSinglePrivilegeCheck(RtlConvertLongToLuid(SE_MANAGE_VOLUME_PRIVILEGE), processor_mode))
|
|
|
|
return STATUS_PRIVILEGE_NOT_HELD;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
if (length < sizeof(uint64_t))
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_INVALID_PARAMETER;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
devid = *(uint64_t*)data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceSharedLite(&Vcb->tree_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->readonly) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
return STATUS_MEDIA_WRITE_PROTECTED;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
num_rw_devices = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = Vcb->devices.Flink;
|
|
|
|
while (le != &Vcb->devices) {
|
|
|
|
device* dev2 = CONTAINING_RECORD(le, device, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (dev2->devitem.dev_id == devid)
|
|
|
|
dev = dev2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!dev2->readonly)
|
|
|
|
num_rw_devices++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!dev) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2019-09-01 12:53:20 +00:00
|
|
|
WARN("device %I64x not found\n", devid);
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!dev->readonly) {
|
|
|
|
if (num_rw_devices == 1) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
WARN("not removing last non-readonly device\n");
|
|
|
|
return STATUS_INVALID_PARAMETER;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (num_rw_devices == 4 &&
|
|
|
|
((Vcb->data_flags & BLOCK_FLAG_RAID10 || Vcb->metadata_flags & BLOCK_FLAG_RAID10 || Vcb->system_flags & BLOCK_FLAG_RAID10) ||
|
2020-04-23 02:38:57 +00:00
|
|
|
(Vcb->data_flags & BLOCK_FLAG_RAID6 || Vcb->metadata_flags & BLOCK_FLAG_RAID6 || Vcb->system_flags & BLOCK_FLAG_RAID6) ||
|
|
|
|
(Vcb->data_flags & BLOCK_FLAG_RAID1C4 || Vcb->metadata_flags & BLOCK_FLAG_RAID1C4 || Vcb->system_flags & BLOCK_FLAG_RAID1C4)
|
|
|
|
)
|
2017-01-01 17:12:12 +00:00
|
|
|
) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("would not be enough devices to satisfy RAID requirement (RAID6/10/1C4)\n");
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_CANNOT_DELETE;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2020-04-23 02:38:57 +00:00
|
|
|
if (num_rw_devices == 3 &&
|
|
|
|
((Vcb->data_flags & BLOCK_FLAG_RAID5 || Vcb->metadata_flags & BLOCK_FLAG_RAID5 || Vcb->system_flags & BLOCK_FLAG_RAID5) ||
|
|
|
|
(Vcb->data_flags & BLOCK_FLAG_RAID1C3 || Vcb->metadata_flags & BLOCK_FLAG_RAID1C3 || Vcb->system_flags & BLOCK_FLAG_RAID1C3))
|
|
|
|
) {
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("would not be enough devices to satisfy RAID requirement (RAID5/1C3)\n");
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_CANNOT_DELETE;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (num_rw_devices == 2 &&
|
|
|
|
((Vcb->data_flags & BLOCK_FLAG_RAID0 || Vcb->metadata_flags & BLOCK_FLAG_RAID0 || Vcb->system_flags & BLOCK_FLAG_RAID0) ||
|
|
|
|
(Vcb->data_flags & BLOCK_FLAG_RAID1 || Vcb->metadata_flags & BLOCK_FLAG_RAID1 || Vcb->system_flags & BLOCK_FLAG_RAID1))
|
|
|
|
) {
|
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
|
|
|
ERR("would not be enough devices to satisfy RAID requirement (RAID0/1)\n");
|
|
|
|
return STATUS_CANNOT_DELETE;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->tree_lock);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (Vcb->balance.thread) {
|
|
|
|
WARN("balance already running\n");
|
|
|
|
return STATUS_DEVICE_NOT_READY;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
dev->reloc = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
RtlZeroMemory(Vcb->balance.opts, sizeof(btrfs_balance_opts) * 3);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
Vcb->balance.opts[i].flags = BTRFS_BALANCE_OPTS_ENABLED | BTRFS_BALANCE_OPTS_DEVID;
|
|
|
|
Vcb->balance.opts[i].devid = devid;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Vcb->balance.paused = false;
|
|
|
|
Vcb->balance.removing = true;
|
|
|
|
Vcb->balance.shrinking = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
Vcb->balance.status = STATUS_SUCCESS;
|
2017-01-01 17:12:12 +00:00
|
|
|
KeInitializeEvent(&Vcb->balance.event, NotificationEvent, !Vcb->balance.paused);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-11-12 18:32:46 +00:00
|
|
|
InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
|
|
|
|
|
|
|
|
Status = PsCreateSystemThread(&Vcb->balance.thread, 0, &oa, NULL, NULL, balance_thread, Vcb);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("PsCreateSystemThread returned %08lx\n", Status);
|
2019-09-01 12:53:20 +00:00
|
|
|
dev->reloc = false;
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|