2017-09-08 08:02:43 +00:00
|
|
|
/* Copyright (c) Mark Harmstone 2016-17
|
|
|
|
*
|
2016-05-05 17:26:47 +00:00
|
|
|
* This file is part of WinBtrfs.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2016-05-05 17:26:47 +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
|
|
|
*
|
2016-05-05 17:26:47 +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
|
|
|
*
|
2016-05-05 17:26:47 +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"
|
2020-04-23 02:38:57 +00:00
|
|
|
#include "crc32c.h"
|
2016-05-05 17:26:47 +00:00
|
|
|
|
|
|
|
// Number of increments in the size of each cache inode, in sectors. Should
|
|
|
|
// this be a constant number of sectors, a constant 256 KB, or what?
|
|
|
|
#define CACHE_INCREMENTS 64
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static NTSTATUS remove_free_space_inode(device_extension* Vcb, uint64_t inode, LIST_ENTRY* batchlist, PIRP Irp, LIST_ENTRY* rollback) {
|
2016-05-05 17:26:47 +00:00
|
|
|
NTSTATUS Status;
|
2016-07-27 19:24:26 +00:00
|
|
|
fcb* fcb;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = open_fcb(Vcb, Vcb->root_root, inode, BTRFS_TYPE_FILE, NULL, false, NULL, &fcb, PagedPool, Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("open_fcb returned %08lx\n", Status);
|
2016-05-05 17:26:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
mark_fcb_dirty(fcb);
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (fcb->inode_item.st_size > 0) {
|
2016-09-04 15:27:46 +00:00
|
|
|
Status = excise_extents(fcb->Vcb, fcb, 0, sector_align(fcb->inode_item.st_size, fcb->Vcb->superblock.sector_size), Irp, rollback);
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("excise_extents returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
fcb->deleted = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = flush_fcb(fcb, false, batchlist, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("flush_fcb returned %08lx\n", Status);
|
2019-05-11 09:20:02 +00:00
|
|
|
free_fcb(fcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2019-05-11 09:20:02 +00:00
|
|
|
free_fcb(fcb);
|
2016-07-27 19:24:26 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
NTSTATUS clear_free_space_cache(device_extension* Vcb, LIST_ENTRY* batchlist, PIRP Irp) {
|
2016-05-05 17:26:47 +00:00
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp, next_tp;
|
|
|
|
NTSTATUS Status;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool b;
|
2016-05-05 17:26:47 +00:00
|
|
|
LIST_ENTRY rollback;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
InitializeListHead(&rollback);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
searchkey.obj_id = FREE_SPACE_CACHE_ID;
|
|
|
|
searchkey.obj_type = 0;
|
|
|
|
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, Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("error - find_item returned %08lx\n", Status);
|
2016-05-05 17:26:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
do {
|
|
|
|
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;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (tp.item->key.obj_id == searchkey.obj_id && tp.item->key.obj_type == searchkey.obj_type) {
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (tp.item->size >= sizeof(FREE_SPACE_ITEM)) {
|
|
|
|
FREE_SPACE_ITEM* fsi = (FREE_SPACE_ITEM*)tp.item->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (fsi->key.obj_type != TYPE_INODE_ITEM)
|
2019-09-01 12:53:20 +00:00
|
|
|
WARN("key (%I64x,%x,%I64x) does not point to an INODE_ITEM\n", fsi->key.obj_id, fsi->key.obj_type, fsi->key.offset);
|
2016-05-05 17:26:47 +00:00
|
|
|
else {
|
2016-07-27 19:24:26 +00:00
|
|
|
LIST_ENTRY* le;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
Status = remove_free_space_inode(Vcb, fsi->key.obj_id, batchlist, Irp, &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("remove_free_space_inode for (%I64x,%x,%I64x) returned %08lx\n", fsi->key.obj_id, fsi->key.obj_type, fsi->key.offset, Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = Vcb->chunks.Flink;
|
|
|
|
while (le != &Vcb->chunks) {
|
|
|
|
chunk* c = CONTAINING_RECORD(le, chunk, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (c->offset == tp.item->key.offset && c->cache) {
|
2019-05-11 09:20:02 +00:00
|
|
|
reap_fcb(c->cache);
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache = NULL;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
} else
|
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, tp.item->size, sizeof(FREE_SPACE_ITEM));
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
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, Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (b)
|
|
|
|
tp = next_tp;
|
|
|
|
} while (b);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
2017-09-08 08:02:43 +00:00
|
|
|
clear_rollback(&rollback);
|
2016-05-05 17:26:47 +00:00
|
|
|
else
|
|
|
|
do_rollback(Vcb, &rollback);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (Vcb->space_root) {
|
|
|
|
searchkey.obj_id = 0;
|
|
|
|
searchkey.obj_type = 0;
|
|
|
|
searchkey.offset = 0;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->space_root, &tp, &searchkey, false, Irp);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
b = find_next_item(Vcb, &tp, &next_tp, false, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (b)
|
|
|
|
tp = next_tp;
|
|
|
|
} while (b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// regenerate free space tree
|
|
|
|
if (Vcb->superblock.compat_ro_flags & BTRFS_COMPAT_RO_FLAGS_FREE_SPACE_CACHE) {
|
|
|
|
LIST_ENTRY* le;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (!c->cache_loaded) {
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = load_cache_chunk(Vcb, c, NULL);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("load_cache_chunk(%I64x) returned %08lx\n", c->offset, 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);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
c->changed = true;
|
|
|
|
c->space_changed = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
NTSTATUS add_space_entry(LIST_ENTRY* list, LIST_ENTRY* list_size, uint64_t offset, uint64_t size) {
|
2016-05-05 17:26:47 +00:00
|
|
|
space* s;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s->address = offset;
|
2016-05-05 17:26:47 +00:00
|
|
|
s->size = size;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (IsListEmpty(list))
|
|
|
|
InsertTailList(list, &s->list_entry);
|
2016-05-05 17:26:47 +00:00
|
|
|
else {
|
2016-07-27 19:24:26 +00:00
|
|
|
space* s2 = CONTAINING_RECORD(list->Blink, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s2->address < offset)
|
|
|
|
InsertTailList(list, &s->list_entry);
|
2016-05-05 17:26:47 +00:00
|
|
|
else {
|
|
|
|
LIST_ENTRY* le;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = list->Flink;
|
|
|
|
while (le != list) {
|
2016-05-05 17:26:47 +00:00
|
|
|
s2 = CONTAINING_RECORD(le, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s2->address > offset) {
|
2016-05-05 17:26:47 +00:00
|
|
|
InsertTailList(le, &s->list_entry);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto size;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
size:
|
|
|
|
if (!list_size)
|
|
|
|
return STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (IsListEmpty(list_size))
|
|
|
|
InsertTailList(list_size, &s->list_entry_size);
|
|
|
|
else {
|
|
|
|
space* s2 = CONTAINING_RECORD(list_size->Blink, space, list_entry_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s2->size >= size)
|
|
|
|
InsertTailList(list_size, &s->list_entry_size);
|
|
|
|
else {
|
|
|
|
LIST_ENTRY* le;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = list_size->Flink;
|
|
|
|
while (le != list_size) {
|
|
|
|
s2 = CONTAINING_RECORD(le, space, list_entry_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s2->size <= size) {
|
|
|
|
InsertHeadList(le->Blink, &s->list_entry_size);
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static void load_free_space_bitmap(device_extension* Vcb, chunk* c, uint64_t offset, void* data, uint64_t* total_space) {
|
2016-05-05 17:26:47 +00:00
|
|
|
RTL_BITMAP bmph;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint32_t i, len, *dwords = data;
|
2016-05-05 17:26:47 +00:00
|
|
|
ULONG runlength, index;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// flip bits
|
2019-09-01 12:53:20 +00:00
|
|
|
for (i = 0; i < Vcb->superblock.sector_size / sizeof(uint32_t); i++) {
|
2016-05-05 17:26:47 +00:00
|
|
|
dwords[i] = ~dwords[i];
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
len = Vcb->superblock.sector_size * 8;
|
|
|
|
|
|
|
|
RtlInitializeBitMap(&bmph, data, len);
|
2016-05-05 17:26:47 +00:00
|
|
|
|
|
|
|
index = 0;
|
|
|
|
runlength = RtlFindFirstRunClear(&bmph, &index);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
while (runlength != 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t addr, length;
|
|
|
|
|
|
|
|
if (index >= len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (index + runlength >= len) {
|
|
|
|
runlength = len - index;
|
|
|
|
|
|
|
|
if (runlength == 0)
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
addr = offset + (index << Vcb->sector_shift);
|
|
|
|
length = runlength << Vcb->sector_shift;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
add_space_entry(&c->space, &c->space_size, addr, length);
|
2016-05-05 17:26:47 +00:00
|
|
|
index += runlength;
|
2016-10-29 17:05:10 +00:00
|
|
|
*total_space += length;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
runlength = RtlFindNextForwardRunClear(&bmph, index, &index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
static void order_space_entry(space* s, LIST_ENTRY* list_size) {
|
|
|
|
LIST_ENTRY* le;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (IsListEmpty(list_size)) {
|
|
|
|
InsertHeadList(list_size, &s->list_entry_size);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = list_size->Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
while (le != list_size) {
|
|
|
|
space* s2 = CONTAINING_RECORD(le, space, list_entry_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s2->size <= s->size) {
|
|
|
|
InsertHeadList(le->Blink, &s->list_entry_size);
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
InsertTailList(list_size, &s->list_entry_size);
|
|
|
|
}
|
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
typedef struct {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t stripe;
|
2016-10-29 17:05:10 +00:00
|
|
|
LIST_ENTRY list_entry;
|
|
|
|
} superblock_stripe;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static NTSTATUS add_superblock_stripe(LIST_ENTRY* stripes, uint64_t off, uint64_t len) {
|
|
|
|
uint64_t i;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
LIST_ENTRY* le;
|
|
|
|
superblock_stripe* ss;
|
2019-09-01 12:53:20 +00:00
|
|
|
bool ignore = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
le = stripes->Flink;
|
|
|
|
while (le != stripes) {
|
|
|
|
ss = CONTAINING_RECORD(le, superblock_stripe, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ss->stripe == off + i) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ignore = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ignore)
|
|
|
|
continue;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
ss = ExAllocatePoolWithTag(PagedPool, sizeof(superblock_stripe), ALLOC_TAG);
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!ss) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
ss->stripe = off + i;
|
|
|
|
InsertTailList(stripes, &ss->list_entry);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return STATUS_SUCCESS;
|
2016-10-29 17:05:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static NTSTATUS get_superblock_size(chunk* c, uint64_t* size) {
|
2017-01-01 17:12:12 +00:00
|
|
|
NTSTATUS Status;
|
2016-10-29 17:05:10 +00:00
|
|
|
CHUNK_ITEM* ci = c->chunk_item;
|
|
|
|
CHUNK_ITEM_STRIPE* cis = (CHUNK_ITEM_STRIPE*)&ci[1];
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t off_start, off_end, space = 0;
|
|
|
|
uint16_t i = 0, j;
|
2016-10-29 17:05:10 +00:00
|
|
|
LIST_ENTRY stripes;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
InitializeListHead(&stripes);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
while (superblock_addrs[i] != 0) {
|
|
|
|
if (ci->type & BLOCK_FLAG_RAID0 || ci->type & BLOCK_FLAG_RAID10) {
|
|
|
|
for (j = 0; j < ci->num_stripes; j++) {
|
|
|
|
ULONG sub_stripes = max(ci->sub_stripes, 1);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (cis[j].offset + (ci->size * ci->num_stripes / sub_stripes) > superblock_addrs[i] && cis[j].offset <= superblock_addrs[i] + sizeof(superblock)) {
|
|
|
|
off_start = superblock_addrs[i] - cis[j].offset;
|
|
|
|
off_start -= off_start % ci->stripe_length;
|
|
|
|
off_start *= ci->num_stripes / sub_stripes;
|
|
|
|
off_start += (j / sub_stripes) * ci->stripe_length;
|
|
|
|
|
|
|
|
off_end = off_start + ci->stripe_length;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_superblock_stripe(&stripes, off_start / ci->stripe_length, 1);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_superblock_stripe returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2016-10-29 17:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (ci->type & BLOCK_FLAG_RAID5) {
|
|
|
|
for (j = 0; j < ci->num_stripes; j++) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t stripe_size = ci->size / (ci->num_stripes - 1);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (cis[j].offset + stripe_size > superblock_addrs[i] && cis[j].offset <= superblock_addrs[i] + sizeof(superblock)) {
|
|
|
|
off_start = superblock_addrs[i] - cis[j].offset;
|
|
|
|
off_start -= off_start % (ci->stripe_length * (ci->num_stripes - 1));
|
|
|
|
off_start *= ci->num_stripes - 1;
|
|
|
|
|
|
|
|
off_end = off_start + (ci->stripe_length * (ci->num_stripes - 1));
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_superblock_stripe(&stripes, off_start / ci->stripe_length, (off_end - off_start) / ci->stripe_length);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_superblock_stripe returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2016-10-29 17:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (ci->type & BLOCK_FLAG_RAID6) {
|
|
|
|
for (j = 0; j < ci->num_stripes; j++) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t stripe_size = ci->size / (ci->num_stripes - 2);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (cis[j].offset + stripe_size > superblock_addrs[i] && cis[j].offset <= superblock_addrs[i] + sizeof(superblock)) {
|
|
|
|
off_start = superblock_addrs[i] - cis[j].offset;
|
|
|
|
off_start -= off_start % (ci->stripe_length * (ci->num_stripes - 2));
|
|
|
|
off_start *= ci->num_stripes - 2;
|
|
|
|
|
|
|
|
off_end = off_start + (ci->stripe_length * (ci->num_stripes - 2));
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_superblock_stripe(&stripes, off_start / ci->stripe_length, (off_end - off_start) / ci->stripe_length);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_superblock_stripe returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2016-10-29 17:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 02:38:57 +00:00
|
|
|
} else { // SINGLE, DUPLICATE, RAID1, RAID1C3, RAID1C4
|
2016-10-29 17:05:10 +00:00
|
|
|
for (j = 0; j < ci->num_stripes; j++) {
|
|
|
|
if (cis[j].offset + ci->size > superblock_addrs[i] && cis[j].offset <= superblock_addrs[i] + sizeof(superblock)) {
|
|
|
|
off_start = ((superblock_addrs[i] - cis[j].offset) / c->chunk_item->stripe_length) * c->chunk_item->stripe_length;
|
|
|
|
off_end = sector_align(superblock_addrs[i] - cis[j].offset + sizeof(superblock), c->chunk_item->stripe_length);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = add_superblock_stripe(&stripes, off_start / ci->stripe_length, (off_end - off_start) / ci->stripe_length);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_superblock_stripe returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2016-10-29 17:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
i++;
|
|
|
|
}
|
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:
|
2016-10-29 17:05:10 +00:00
|
|
|
while (!IsListEmpty(&stripes)) {
|
|
|
|
LIST_ENTRY* le = RemoveHeadList(&stripes);
|
|
|
|
superblock_stripe* ss = CONTAINING_RECORD(le, superblock_stripe, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
space++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
ExFreePool(ss);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
*size = space * ci->stripe_length;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
2016-10-29 17:05:10 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
NTSTATUS load_stored_free_space_cache(device_extension* Vcb, chunk* c, bool load_only, PIRP Irp) {
|
2016-05-05 17:26:47 +00:00
|
|
|
KEY searchkey;
|
2016-07-27 19:24:26 +00:00
|
|
|
traverse_ptr tp;
|
2016-05-05 17:26:47 +00:00
|
|
|
FREE_SPACE_ITEM* fsi;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t inode, *generation;
|
|
|
|
uint8_t* data;
|
2016-05-05 17:26:47 +00:00
|
|
|
NTSTATUS Status;
|
2022-04-28 19:31:44 +00:00
|
|
|
uint32_t *checksums, crc32, num_sectors, num_valid_sectors, size;
|
2016-05-05 17:26:47 +00:00
|
|
|
FREE_SPACE_ENTRY* fse;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t num_entries, num_bitmaps, extent_length, bmpnum, off, total_space = 0, superblock_size;
|
2016-07-27 19:24:26 +00:00
|
|
|
LIST_ENTRY *le, rollback;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// FIXME - does this break if Vcb->superblock.sector_size is not 4096?
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("(%p, %I64x)\n", Vcb, c->offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
searchkey.obj_id = FREE_SPACE_CACHE_ID;
|
|
|
|
searchkey.obj_type = 0;
|
|
|
|
searchkey.offset = c->offset;
|
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, Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("error - find_item returned %08lx\n", Status);
|
2016-05-05 17:26:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (keycmp(tp.item->key, searchkey)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("(%I64x,%x,%I64x) not found\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (tp.item->size < sizeof(FREE_SPACE_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, tp.item->size, sizeof(FREE_SPACE_ITEM));
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
fsi = (FREE_SPACE_ITEM*)tp.item->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (fsi->key.obj_type != TYPE_INODE_ITEM) {
|
|
|
|
WARN("cache pointed to something other than an INODE_ITEM\n");
|
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
inode = fsi->key.obj_id;
|
|
|
|
num_entries = fsi->num_entries;
|
|
|
|
num_bitmaps = fsi->num_bitmaps;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = open_fcb(Vcb, Vcb->root_root, inode, BTRFS_TYPE_FILE, NULL, false, NULL, &c->cache, PagedPool, Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("open_fcb returned %08lx\n", Status);
|
2016-09-04 15:27:46 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (load_only)
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (c->cache->inode_item.st_size == 0) {
|
|
|
|
WARN("cache had zero length\n");
|
2019-05-11 09:20:02 +00:00
|
|
|
free_fcb(c->cache);
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache = NULL;
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache->inode_item.flags |= BTRFS_INODE_NODATACOW;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (num_entries == 0 && num_bitmaps == 0)
|
|
|
|
return STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
size = (uint32_t)sector_align(c->cache->inode_item.st_size, Vcb->superblock.sector_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
data = ExAllocatePoolWithTag(PagedPool, size, ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!data) {
|
|
|
|
ERR("out of memory\n");
|
2019-05-11 09:20:02 +00:00
|
|
|
free_fcb(c->cache);
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache = NULL;
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-05-11 09:20:02 +00:00
|
|
|
if (c->chunk_item->size < 0x6400000) { // 100 MB
|
|
|
|
WARN("deleting free space cache for chunk smaller than 100MB\n");
|
|
|
|
goto clearcache;
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = read_file(c->cache, data, 0, c->cache->inode_item.st_size, NULL, NULL);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("read_file returned %08lx\n", Status);
|
2016-05-05 17:26:47 +00:00
|
|
|
ExFreePool(data);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
c->cache->deleted = true;
|
2016-07-27 19:24:26 +00:00
|
|
|
mark_fcb_dirty(c->cache);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-05-11 09:20:02 +00:00
|
|
|
free_fcb(c->cache);
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache = NULL;
|
2016-09-04 15:27:46 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (size > c->cache->inode_item.st_size)
|
2017-09-08 08:02:43 +00:00
|
|
|
RtlZeroMemory(&data[c->cache->inode_item.st_size], (ULONG)(size - c->cache->inode_item.st_size));
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
num_sectors = size >> Vcb->sector_shift;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
generation = (uint64_t*)(data + (num_sectors * sizeof(uint32_t)));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (*generation != fsi->generation) {
|
2019-09-01 12:53:20 +00:00
|
|
|
WARN("free space cache generation for %I64x was %I64x, expected %I64x\n", c->offset, *generation, fsi->generation);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto clearcache;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
extent_length = (num_sectors * sizeof(uint32_t)) + sizeof(uint64_t) + (num_entries * sizeof(FREE_SPACE_ENTRY));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
num_valid_sectors = (ULONG)((sector_align(extent_length, Vcb->superblock.sector_size) >> Vcb->sector_shift) + num_bitmaps);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (num_valid_sectors > num_sectors) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("free space cache for %I64x was %u sectors, expected at least %u\n", c->offset, num_sectors, num_valid_sectors);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto clearcache;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
checksums = (uint32_t*)data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
for (uint32_t i = 0; i < num_valid_sectors; i++) {
|
|
|
|
if (i << Vcb->sector_shift > sizeof(uint32_t) * num_sectors)
|
|
|
|
crc32 = ~calc_crc32c(0xffffffff, &data[i << Vcb->sector_shift], Vcb->superblock.sector_size);
|
|
|
|
else if ((i + 1) << Vcb->sector_shift < sizeof(uint32_t) * num_sectors)
|
2016-05-05 17:26:47 +00:00
|
|
|
crc32 = 0; // FIXME - test this
|
|
|
|
else
|
2022-04-28 19:31:44 +00:00
|
|
|
crc32 = ~calc_crc32c(0xffffffff, &data[sizeof(uint32_t) * num_sectors], ((i + 1) << Vcb->sector_shift) - (sizeof(uint32_t) * num_sectors));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (crc32 != checksums[i]) {
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("checksum %u was %08x, expected %08x\n", i, crc32, checksums[i]);
|
2016-07-27 19:24:26 +00:00
|
|
|
goto clearcache;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
off = (sizeof(uint32_t) * num_sectors) + sizeof(uint64_t);
|
2016-05-05 17:26:47 +00:00
|
|
|
|
|
|
|
bmpnum = 0;
|
2022-04-28 19:31:44 +00:00
|
|
|
for (uint32_t i = 0; i < num_entries; i++) {
|
|
|
|
if ((off + sizeof(FREE_SPACE_ENTRY)) >> Vcb->sector_shift != off >> Vcb->sector_shift)
|
2016-05-05 17:26:47 +00:00
|
|
|
off = sector_align(off, Vcb->superblock.sector_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
fse = (FREE_SPACE_ENTRY*)&data[off];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (fse->type == FREE_SPACE_EXTENT) {
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = add_space_entry(&c->space, &c->space_size, fse->offset, fse->size);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_space_entry returned %08lx\n", Status);
|
2016-05-05 17:26:47 +00:00
|
|
|
ExFreePool(data);
|
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
total_space += fse->size;
|
2016-05-05 17:26:47 +00:00
|
|
|
} else if (fse->type != FREE_SPACE_BITMAP) {
|
|
|
|
ERR("unknown free-space type %x\n", fse->type);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
off += sizeof(FREE_SPACE_ENTRY);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (num_bitmaps > 0) {
|
2022-04-28 19:31:44 +00:00
|
|
|
bmpnum = sector_align(off, Vcb->superblock.sector_size) >> Vcb->sector_shift;
|
2019-09-01 12:53:20 +00:00
|
|
|
off = (sizeof(uint32_t) * num_sectors) + sizeof(uint64_t);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
for (uint32_t i = 0; i < num_entries; i++) {
|
|
|
|
if ((off + sizeof(FREE_SPACE_ENTRY)) >> Vcb->sector_shift != off >> Vcb->sector_shift)
|
2016-05-05 17:26:47 +00:00
|
|
|
off = sector_align(off, Vcb->superblock.sector_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
fse = (FREE_SPACE_ENTRY*)&data[off];
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (fse->type == FREE_SPACE_BITMAP) {
|
|
|
|
// FIXME - make sure we don't overflow the buffer here
|
2022-04-28 19:31:44 +00:00
|
|
|
load_free_space_bitmap(Vcb, c, fse->offset, &data[bmpnum << Vcb->sector_shift], &total_space);
|
2016-05-05 17:26:47 +00:00
|
|
|
bmpnum++;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
off += sizeof(FREE_SPACE_ENTRY);
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
// do sanity check
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = get_superblock_size(c, &superblock_size);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("get_superblock_size returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
ExFreePool(data);
|
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (c->chunk_item->size - c->used != total_space + superblock_size) {
|
2019-09-01 12:53:20 +00:00
|
|
|
WARN("invalidating cache for chunk %I64x: space was %I64x, expected %I64x\n", c->offset, total_space + superblock_size, c->chunk_item->size - c->used);
|
2016-10-29 17:05:10 +00:00
|
|
|
goto clearcache;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
le = c->space.Flink;
|
|
|
|
while (le != &c->space) {
|
|
|
|
space* s = CONTAINING_RECORD(le, space, list_entry);
|
|
|
|
LIST_ENTRY* le2 = le->Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (le2 != &c->space) {
|
|
|
|
space* s2 = CONTAINING_RECORD(le2, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s2->address == s->address + s->size) {
|
2016-05-05 17:26:47 +00:00
|
|
|
s->size += s2->size;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
RemoveEntryList(&s2->list_entry);
|
2016-07-27 19:24:26 +00:00
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
2016-05-05 17:26:47 +00:00
|
|
|
ExFreePool(s2);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RemoveEntryList(&s->list_entry_size);
|
|
|
|
order_space_entry(s, &c->space_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
le2 = le;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
le = le2;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
ExFreePool(data);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_SUCCESS;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
clearcache:
|
|
|
|
ExFreePool(data);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
InitializeListHead(&rollback);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
Status = excise_extents(Vcb, c->cache, 0, c->cache->inode_item.st_size, Irp, &rollback);
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("excise_extents returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
do_rollback(Vcb, &rollback);
|
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
clear_rollback(&rollback);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
c->cache->deleted = true;
|
2016-07-27 19:24:26 +00:00
|
|
|
mark_fcb_dirty(c->cache);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
c->old_cache = c->cache;
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
le = c->space.Flink;
|
|
|
|
while (le != &c->space) {
|
|
|
|
space* s = CONTAINING_RECORD(le, space, list_entry);
|
|
|
|
LIST_ENTRY* le2 = le->Flink;
|
|
|
|
|
|
|
|
RemoveEntryList(&s->list_entry);
|
|
|
|
RemoveEntryList(&s->list_entry_size);
|
|
|
|
ExFreePool(s);
|
|
|
|
|
|
|
|
le = le2;
|
|
|
|
}
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
static NTSTATUS load_stored_free_space_tree(device_extension* Vcb, chunk* c, PIRP Irp) {
|
2016-05-05 17:26:47 +00:00
|
|
|
KEY searchkey;
|
2017-09-08 08:02:43 +00:00
|
|
|
traverse_ptr tp, next_tp;
|
2016-05-05 17:26:47 +00:00
|
|
|
NTSTATUS Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
ULONG* bmparr = NULL;
|
|
|
|
ULONG bmplen = 0;
|
|
|
|
LIST_ENTRY* le;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("(%p, %I64x)\n", Vcb, c->offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!Vcb->space_root)
|
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
|
|
|
|
searchkey.obj_id = c->offset;
|
|
|
|
searchkey.obj_type = TYPE_FREE_SPACE_INFO;
|
|
|
|
searchkey.offset = c->chunk_item->size;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->space_root, &tp, &searchkey, false, Irp);
|
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;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2016-07-27 19:24:26 +00:00
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
if (keycmp(tp.item->key, searchkey)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("(%I64x,%x,%I64x) not found\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
2016-05-05 17:26:47 +00:00
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
if (tp.item->size < sizeof(FREE_SPACE_INFO)) {
|
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, tp.item->size, sizeof(FREE_SPACE_INFO));
|
2017-09-08 08:02:43 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
while (find_next_item(Vcb, &tp, &next_tp, false, Irp)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
tp = next_tp;
|
|
|
|
|
|
|
|
if (tp.item->key.obj_id >= c->offset + c->chunk_item->size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (tp.item->key.obj_type == TYPE_FREE_SPACE_EXTENT) {
|
|
|
|
Status = add_space_entry(&c->space, &c->space_size, tp.item->key.obj_id, tp.item->key.offset);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_space_entry returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (bmparr) ExFreePool(bmparr);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
} else if (tp.item->key.obj_type == TYPE_FREE_SPACE_BITMAP) {
|
|
|
|
ULONG explen, index, runlength;
|
|
|
|
RTL_BITMAP bmp;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t lastoff;
|
|
|
|
ULONG bmpl;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
explen = (ULONG)(tp.item->key.offset >> Vcb->sector_shift) / 8;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (tp.item->size < explen) {
|
2020-04-23 02:38:57 +00:00
|
|
|
WARN("(%I64x,%x,%I64x) was %u bytes, expected %lu\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset, tp.item->size, explen);
|
2017-09-08 08:02:43 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
} else if (tp.item->size == 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
WARN("(%I64x,%x,%I64x) has size of 0\n", tp.item->key.obj_id, tp.item->key.obj_type, tp.item->key.offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
return STATUS_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bmplen < tp.item->size) {
|
|
|
|
if (bmparr)
|
|
|
|
ExFreePool(bmparr);
|
|
|
|
|
|
|
|
bmplen = (ULONG)sector_align(tp.item->size, sizeof(ULONG));
|
|
|
|
bmparr = ExAllocatePoolWithTag(PagedPool, bmplen, ALLOC_TAG);
|
|
|
|
if (!bmparr) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We copy the bitmap because it supposedly has to be ULONG-aligned
|
|
|
|
RtlCopyMemory(bmparr, tp.item->data, tp.item->size);
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
bmpl = (ULONG)tp.item->key.offset >> Vcb->sector_shift;
|
2019-09-01 12:53:20 +00:00
|
|
|
|
|
|
|
RtlInitializeBitMap(&bmp, bmparr, bmpl);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
lastoff = tp.item->key.obj_id;
|
|
|
|
|
|
|
|
runlength = RtlFindFirstRunClear(&bmp, &index);
|
|
|
|
|
|
|
|
while (runlength != 0) {
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t runstart, runend;
|
|
|
|
|
|
|
|
if (index >= bmpl)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (index + runlength >= bmpl) {
|
|
|
|
runlength = bmpl - index;
|
|
|
|
|
|
|
|
if (runlength == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
runstart = tp.item->key.obj_id + (index << Vcb->sector_shift);
|
|
|
|
runend = runstart + (runlength << Vcb->sector_shift);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (runstart > lastoff) {
|
|
|
|
Status = add_space_entry(&c->space, &c->space_size, lastoff, runstart - lastoff);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_space_entry returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (bmparr) ExFreePool(bmparr);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastoff = runend;
|
|
|
|
|
|
|
|
runlength = RtlFindNextForwardRunClear(&bmp, index + runlength, &index);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastoff < tp.item->key.obj_id + tp.item->key.offset) {
|
|
|
|
Status = add_space_entry(&c->space, &c->space_size, lastoff, tp.item->key.obj_id + tp.item->key.offset - lastoff);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("add_space_entry returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (bmparr) ExFreePool(bmparr);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bmparr)
|
|
|
|
ExFreePool(bmparr);
|
|
|
|
|
|
|
|
le = c->space.Flink;
|
|
|
|
while (le != &c->space) {
|
|
|
|
space* s = CONTAINING_RECORD(le, space, list_entry);
|
|
|
|
LIST_ENTRY* le2 = le->Flink;
|
|
|
|
|
|
|
|
if (le2 != &c->space) {
|
|
|
|
space* s2 = CONTAINING_RECORD(le2, space, list_entry);
|
|
|
|
|
|
|
|
if (s2->address == s->address + s->size) {
|
|
|
|
s->size += s2->size;
|
|
|
|
|
|
|
|
RemoveEntryList(&s2->list_entry);
|
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
|
|
|
ExFreePool(s2);
|
|
|
|
|
|
|
|
RemoveEntryList(&s->list_entry_size);
|
|
|
|
order_space_entry(s, &c->space_size);
|
|
|
|
|
|
|
|
le2 = le;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS load_free_space_cache(device_extension* Vcb, chunk* c, PIRP Irp) {
|
|
|
|
traverse_ptr tp, next_tp;
|
|
|
|
KEY searchkey;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t lastaddr;
|
|
|
|
bool b;
|
2017-09-08 08:02:43 +00:00
|
|
|
space* s;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
if (Vcb->superblock.compat_ro_flags & BTRFS_COMPAT_RO_FLAGS_FREE_SPACE_CACHE && Vcb->superblock.compat_ro_flags & BTRFS_COMPAT_RO_FLAGS_FREE_SPACE_CACHE_VALID) {
|
|
|
|
Status = load_stored_free_space_tree(Vcb, c, Irp);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status) && Status != STATUS_NOT_FOUND) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("load_stored_free_space_tree returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
} else if (Vcb->superblock.generation - 1 == Vcb->superblock.cache_generation) {
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = load_stored_free_space_cache(Vcb, c, false, Irp);
|
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("load_stored_free_space_cache returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
Status = STATUS_NOT_FOUND;
|
|
|
|
|
|
|
|
if (Status == STATUS_NOT_FOUND) {
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("generating free space cache for chunk %I64x\n", c->offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
searchkey.obj_id = c->offset;
|
|
|
|
searchkey.obj_type = TYPE_EXTENT_ITEM;
|
|
|
|
searchkey.offset = 0;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = find_item(Vcb, Vcb->extent_root, &tp, &searchkey, false, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("error - find_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastaddr = c->offset;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (tp.item->key.obj_id >= c->offset + c->chunk_item->size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (tp.item->key.obj_id >= c->offset && (tp.item->key.obj_type == TYPE_EXTENT_ITEM || tp.item->key.obj_type == TYPE_METADATA_ITEM)) {
|
|
|
|
if (tp.item->key.obj_id > lastaddr) {
|
|
|
|
s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->address = lastaddr;
|
|
|
|
s->size = tp.item->key.obj_id - lastaddr;
|
|
|
|
InsertTailList(&c->space, &s->list_entry);
|
|
|
|
|
|
|
|
order_space_entry(s, &c->space_size);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("(%I64x,%I64x)\n", s->address, s->size);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tp.item->key.obj_type == TYPE_METADATA_ITEM)
|
|
|
|
lastaddr = tp.item->key.obj_id + Vcb->superblock.node_size;
|
|
|
|
else
|
|
|
|
lastaddr = tp.item->key.obj_id + tp.item->key.offset;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
b = find_next_item(Vcb, &tp, &next_tp, false, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (b)
|
|
|
|
tp = next_tp;
|
|
|
|
} while (b);
|
|
|
|
|
|
|
|
if (lastaddr < c->offset + c->chunk_item->size) {
|
|
|
|
s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->address = lastaddr;
|
|
|
|
s->size = c->offset + c->chunk_item->size - lastaddr;
|
|
|
|
InsertTailList(&c->space, &s->list_entry);
|
|
|
|
|
|
|
|
order_space_entry(s, &c->space_size);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("(%I64x,%I64x)\n", s->address, s->size);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS load_cache_chunk(device_extension* Vcb, chunk* c, PIRP Irp) {
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
if (c->cache_loaded)
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
Status = load_free_space_cache(Vcb, c, Irp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("load_free_space_cache returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
protect_superblocks(c);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
c->cache_loaded = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static NTSTATUS insert_cache_extent(fcb* fcb, uint64_t start, uint64_t length, LIST_ENTRY* rollback) {
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
LIST_ENTRY* le = fcb->Vcb->chunks.Flink;
|
2016-05-05 17:26:47 +00:00
|
|
|
chunk* c;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t flags;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
flags = fcb->Vcb->data_flags;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
while (le != &fcb->Vcb->chunks) {
|
2016-05-05 17:26:47 +00:00
|
|
|
c = CONTAINING_RECORD(le, chunk, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!c->readonly && !c->reloc) {
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c, fcb->Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (c->chunk_item->type == flags && (c->chunk_item->size - c->used) >= length) {
|
2019-09-01 12:53:20 +00:00
|
|
|
if (insert_extent_chunk(fcb->Vcb, fcb, c, start, length, false, NULL, NULL, rollback, BTRFS_COMPRESSION_NONE, length, false, 0))
|
2016-10-29 17:05:10 +00:00
|
|
|
return STATUS_SUCCESS;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, fcb->Vcb);
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = alloc_chunk(fcb->Vcb, flags, &c, 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-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c, fcb->Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (c->chunk_item->type == flags && (c->chunk_item->size - c->used) >= length) {
|
2019-09-01 12:53:20 +00:00
|
|
|
if (insert_extent_chunk(fcb->Vcb, fcb, c, start, length, false, NULL, NULL, rollback, BTRFS_COMPRESSION_NONE, length, false, 0))
|
2017-09-08 08:02:43 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, fcb->Vcb);
|
2016-05-05 17:26:47 +00:00
|
|
|
|
|
|
|
return STATUS_DISK_FULL;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static NTSTATUS allocate_cache_chunk(device_extension* Vcb, chunk* c, bool* changed, LIST_ENTRY* batchlist, PIRP Irp, LIST_ENTRY* rollback) {
|
2016-05-05 17:26:47 +00:00
|
|
|
LIST_ENTRY* le;
|
|
|
|
NTSTATUS Status;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t num_entries, new_cache_size, i;
|
|
|
|
uint32_t num_sectors;
|
|
|
|
bool realloc_extents = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// FIXME - also do bitmaps
|
|
|
|
// FIXME - make sure this works when sector_size is not 4096
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
num_entries = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// num_entries is the number of entries in c->space and c->deleting - it might
|
|
|
|
// be slightly higher then what we end up writing, but doing it this way is much
|
|
|
|
// quicker and simpler.
|
|
|
|
if (!IsListEmpty(&c->space)) {
|
|
|
|
le = c->space.Flink;
|
|
|
|
while (le != &c->space) {
|
|
|
|
num_entries++;
|
2016-05-05 17:26:47 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = le->Flink;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!IsListEmpty(&c->deleting)) {
|
|
|
|
le = c->deleting.Flink;
|
|
|
|
while (le != &c->deleting) {
|
|
|
|
num_entries++;
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
new_cache_size = sizeof(uint64_t) + (num_entries * sizeof(FREE_SPACE_ENTRY));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
num_sectors = (uint32_t)sector_align(new_cache_size, Vcb->superblock.sector_size) >> Vcb->sector_shift;
|
2019-09-01 12:53:20 +00:00
|
|
|
num_sectors = (uint32_t)sector_align(num_sectors, CACHE_INCREMENTS);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// adjust for padding
|
|
|
|
// FIXME - there must be a more efficient way of doing this
|
2019-09-01 12:53:20 +00:00
|
|
|
new_cache_size = sizeof(uint64_t) + (sizeof(uint32_t) * num_sectors);
|
2016-05-05 17:26:47 +00:00
|
|
|
for (i = 0; i < num_entries; i++) {
|
2022-04-28 19:31:44 +00:00
|
|
|
if ((new_cache_size >> Vcb->sector_shift) != ((new_cache_size + sizeof(FREE_SPACE_ENTRY)) >> Vcb->sector_shift))
|
2016-05-05 17:26:47 +00:00
|
|
|
new_cache_size = sector_align(new_cache_size, Vcb->superblock.sector_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
new_cache_size += sizeof(FREE_SPACE_ENTRY);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
new_cache_size = sector_align(new_cache_size, CACHE_INCREMENTS << Vcb->sector_shift);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
TRACE("chunk %I64x: cache_size = %I64x, new_cache_size = %I64x\n", c->offset, c->cache ? c->cache->inode_item.st_size : 0, new_cache_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c->cache) {
|
|
|
|
if (new_cache_size > c->cache->inode_item.st_size)
|
2019-09-01 12:53:20 +00:00
|
|
|
realloc_extents = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
else {
|
|
|
|
le = c->cache->extents.Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le != &c->cache->extents) {
|
|
|
|
extent* ext = CONTAINING_RECORD(le, extent, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!ext->ignore && (ext->extent_data.type == EXTENT_TYPE_REGULAR || ext->extent_data.type == EXTENT_TYPE_PREALLOC)) {
|
|
|
|
EXTENT_DATA2* ed2 = (EXTENT_DATA2*)&ext->extent_data.data[0];
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ed2->size != 0) {
|
|
|
|
chunk* c2 = get_chunk_from_address(Vcb, ed2->address);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c2 && (c2->readonly || c2->reloc)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
realloc_extents = 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
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!c->cache) {
|
|
|
|
FREE_SPACE_ITEM* fsi;
|
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// create new inode
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
c->cache = create_fcb(Vcb, PagedPool);
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!c->cache) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache->Vcb = Vcb;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache->inode_item.st_size = new_cache_size;
|
|
|
|
c->cache->inode_item.st_blocks = new_cache_size;
|
|
|
|
c->cache->inode_item.st_nlink = 1;
|
|
|
|
c->cache->inode_item.st_mode = S_IRUSR | S_IWUSR | __S_IFREG;
|
|
|
|
c->cache->inode_item.flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW | BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache->Header.IsFastIoPossible = fast_io_possible(c->cache);
|
|
|
|
c->cache->Header.AllocationSize.QuadPart = 0;
|
|
|
|
c->cache->Header.FileSize.QuadPart = 0;
|
|
|
|
c->cache->Header.ValidDataLength.QuadPart = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache->subvol = Vcb->root_root;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
c->cache->inode = InterlockedIncrement64(&Vcb->root_root->lastinode);
|
2022-04-28 19:34:48 +00:00
|
|
|
c->cache->hash = calc_crc32c(0xffffffff, (uint8_t*)&c->cache->inode, sizeof(uint64_t));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache->type = BTRFS_TYPE_FILE;
|
2019-09-01 12:53:20 +00:00
|
|
|
c->cache->created = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// create new free space entry
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
fsi = ExAllocatePoolWithTag(PagedPool, sizeof(FREE_SPACE_ITEM), ALLOC_TAG);
|
|
|
|
if (!fsi) {
|
|
|
|
ERR("out of memory\n");
|
2019-05-11 09:20:02 +00:00
|
|
|
reap_fcb(c->cache);
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache = NULL;
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
searchkey.obj_id = FREE_SPACE_CACHE_ID;
|
|
|
|
searchkey.obj_type = 0;
|
|
|
|
searchkey.offset = c->offset;
|
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, Irp);
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("error - find_item returned %08lx\n", Status);
|
2016-09-04 15:27:46 +00:00
|
|
|
ExFreePool(fsi);
|
2019-05-11 09:20:02 +00:00
|
|
|
reap_fcb(c->cache);
|
2016-09-04 15:27:46 +00:00
|
|
|
c->cache = NULL;
|
2016-07-27 19:24:26 +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
|
|
|
ExFreePool(fsi);
|
2019-05-11 09:20:02 +00:00
|
|
|
reap_fcb(c->cache);
|
2017-09-08 08:02:43 +00:00
|
|
|
c->cache = NULL;
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
fsi->key.obj_id = c->cache->inode;
|
|
|
|
fsi->key.obj_type = TYPE_INODE_ITEM;
|
|
|
|
fsi->key.offset = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->root_root, FREE_SPACE_CACHE_ID, 0, c->offset, fsi, sizeof(FREE_SPACE_ITEM), NULL, Irp);
|
|
|
|
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(fsi);
|
2019-05-11 09:20:02 +00:00
|
|
|
reap_fcb(c->cache);
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// allocate space
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
Status = insert_cache_extent(c->cache, 0, new_cache_size, rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_cache_extent returned %08lx\n", Status);
|
2019-05-11 09:20:02 +00:00
|
|
|
reap_fcb(c->cache);
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache = NULL;
|
|
|
|
return Status;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
c->cache->extents_changed = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
InsertTailList(&Vcb->all_fcbs, &c->cache->list_entry_all);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:34:48 +00:00
|
|
|
add_fcb_to_subvol(c->cache);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = flush_fcb(c->cache, true, batchlist, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("flush_fcb returned %08lx\n", Status);
|
2019-05-11 09:20:02 +00:00
|
|
|
free_fcb(c->cache);
|
2017-09-08 08:02:43 +00:00
|
|
|
c->cache = NULL;
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = true;
|
2017-01-01 17:12:12 +00:00
|
|
|
} else if (realloc_extents) {
|
2016-05-05 17:26:47 +00:00
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
TRACE("reallocating extents\n");
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// add free_space entry to tree cache
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
searchkey.obj_id = FREE_SPACE_CACHE_ID;
|
|
|
|
searchkey.obj_type = 0;
|
|
|
|
searchkey.offset = c->offset;
|
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, Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("error - find_item returned %08lx\n", Status);
|
2016-05-05 17:26:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (keycmp(searchkey, tp.item->key)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("could not find (%I64x,%x,%I64x) in root_root\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (tp.item->size < sizeof(FREE_SPACE_ITEM)) {
|
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(FREE_SPACE_ITEM));
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
tp.tree->write = true;
|
2016-07-27 19:24:26 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
// remove existing extents
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (c->cache->inode_item.st_size > 0) {
|
|
|
|
le = c->cache->extents.Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
while (le != &c->cache->extents) {
|
|
|
|
extent* ext = CONTAINING_RECORD(le, extent, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!ext->ignore && (ext->extent_data.type == EXTENT_TYPE_REGULAR || ext->extent_data.type == EXTENT_TYPE_PREALLOC)) {
|
|
|
|
EXTENT_DATA2* ed2 = (EXTENT_DATA2*)&ext->extent_data.data[0];
|
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (ed2->size != 0) {
|
|
|
|
chunk* c2 = get_chunk_from_address(Vcb, ed2->address);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (c2) {
|
2019-09-01 12:53:20 +00:00
|
|
|
c2->changed = true;
|
|
|
|
c2->space_changed = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
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
|
|
|
Status = excise_extents(Vcb, c->cache, 0, c->cache->inode_item.st_size, Irp, rollback);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("excise_extents returned %08lx\n", Status);
|
2017-01-01 17:12:12 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// add new extent
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
Status = insert_cache_extent(c->cache, 0, new_cache_size, rollback);
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("insert_cache_extent returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// modify INODE_ITEM
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache->inode_item.st_size = new_cache_size;
|
|
|
|
c->cache->inode_item.st_blocks = new_cache_size;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = flush_fcb(c->cache, true, batchlist, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("flush_fcb returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = true;
|
2016-07-27 19:24:26 +00:00
|
|
|
} else {
|
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// add INODE_ITEM and free_space entry to tree cache, for writing later
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
searchkey.obj_id = c->cache->inode;
|
|
|
|
searchkey.obj_type = TYPE_INODE_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, Irp);
|
2016-07-27 19:24:26 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("error - find_item returned %08lx\n", Status);
|
2016-07-27 19:24:26 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (keycmp(searchkey, tp.item->key)) {
|
2016-07-27 19:24:26 +00:00
|
|
|
INODE_ITEM* ii;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ii = ExAllocatePoolWithTag(PagedPool, sizeof(INODE_ITEM), ALLOC_TAG);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!ii) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RtlCopyMemory(ii, &c->cache->inode_item, sizeof(INODE_ITEM));
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->root_root, c->cache->inode, TYPE_INODE_ITEM, 0, ii, sizeof(INODE_ITEM), NULL, Irp);
|
|
|
|
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(ii);
|
|
|
|
return Status;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
} else {
|
2016-07-27 19:24:26 +00:00
|
|
|
if (tp.item->size < sizeof(INODE_ITEM)) {
|
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(INODE_ITEM));
|
2016-07-27 19:24:26 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
tp.tree->write = true;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
searchkey.obj_id = FREE_SPACE_CACHE_ID;
|
|
|
|
searchkey.obj_type = 0;
|
|
|
|
searchkey.offset = c->offset;
|
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, Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("error - find_item returned %08lx\n", Status);
|
2016-05-05 17:26:47 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (keycmp(searchkey, tp.item->key)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("could not find (%I64x,%x,%I64x) in root_root\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (tp.item->size < sizeof(FREE_SPACE_ITEM)) {
|
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(FREE_SPACE_ITEM));
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_INTERNAL_ERROR;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
tp.tree->write = true;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// FIXME - reduce inode allocation if cache is shrinking. Make sure to avoid infinite write loops
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
NTSTATUS allocate_cache(device_extension* Vcb, bool* changed, PIRP Irp, LIST_ENTRY* rollback) {
|
2017-09-08 08:02:43 +00:00
|
|
|
LIST_ENTRY *le, batchlist;
|
2016-05-05 17:26:47 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
InitializeListHead(&batchlist);
|
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
|
|
|
|
|
|
|
le = Vcb->chunks.Flink;
|
|
|
|
while (le != &Vcb->chunks) {
|
|
|
|
chunk* c = CONTAINING_RECORD(le, chunk, list_entry);
|
|
|
|
|
2019-05-11 09:20:02 +00:00
|
|
|
if (c->space_changed && c->chunk_item->size >= 0x6400000) { // 100MB
|
2019-09-01 12:53:20 +00:00
|
|
|
bool b;
|
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
|
|
|
Status = allocate_cache_chunk(Vcb, c, &b, &batchlist, Irp, rollback);
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (b)
|
2019-09-01 12:53:20 +00:00
|
|
|
*changed = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("allocate_cache_chunk(%I64x) returned %08lx\n", c->offset, Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
clear_batch_list(Vcb, &batchlist);
|
|
|
|
return Status;
|
|
|
|
}
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
|
|
|
|
Status = commit_batch_list(Vcb, &batchlist, Irp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("commit_batch_list returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static void add_rollback_space(LIST_ENTRY* rollback, bool add, LIST_ENTRY* list, LIST_ENTRY* list_size, uint64_t address, uint64_t length, chunk* c) {
|
2016-09-04 15:27:46 +00:00
|
|
|
rollback_space* rs;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
rs = ExAllocatePoolWithTag(PagedPool, sizeof(rollback_space), ALLOC_TAG);
|
|
|
|
if (!rs) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
rs->list = list;
|
|
|
|
rs->list_size = list_size;
|
|
|
|
rs->address = address;
|
|
|
|
rs->length = length;
|
|
|
|
rs->chunk = c;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
add_rollback(rollback, add ? ROLLBACK_ADD_SPACE : ROLLBACK_SUBTRACT_SPACE, rs);
|
2016-09-04 15:27:46 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
void space_list_add2(LIST_ENTRY* list, LIST_ENTRY* list_size, uint64_t address, uint64_t length, chunk* c, LIST_ENTRY* rollback) {
|
2016-07-27 19:24:26 +00:00
|
|
|
LIST_ENTRY* le;
|
|
|
|
space *s, *s2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (IsListEmpty(list)) {
|
|
|
|
s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s->address = address;
|
|
|
|
s->size = length;
|
|
|
|
InsertTailList(list, &s->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size)
|
|
|
|
InsertTailList(list_size, &s->list_entry_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, true, list, list_size, address, length, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = list->Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
do {
|
2016-07-27 19:24:26 +00:00
|
|
|
s2 = CONTAINING_RECORD(le, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// old entry envelops new one completely
|
|
|
|
if (s2->address <= address && s2->address + s2->size >= address + length)
|
|
|
|
return;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// new entry envelops old one completely
|
|
|
|
if (address <= s2->address && address + length >= s2->address + s2->size) {
|
|
|
|
if (address < s2->address) {
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, true, list, list_size, address, s2->address - address, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s2->size += s2->address - address;
|
|
|
|
s2->address = address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
while (s2->list_entry.Blink != list) {
|
|
|
|
space* s3 = CONTAINING_RECORD(s2->list_entry.Blink, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s3->address + s3->size == s2->address) {
|
|
|
|
s2->address = s3->address;
|
|
|
|
s2->size += s3->size;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RemoveEntryList(&s3->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size)
|
|
|
|
RemoveEntryList(&s3->list_entry_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(s3);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (length > s2->size) {
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, true, list, list_size, s2->address + s2->size, address + length - s2->address - s2->size, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s2->size = length;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
while (s2->list_entry.Flink != list) {
|
|
|
|
space* s3 = CONTAINING_RECORD(s2->list_entry.Flink, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s3->address <= s2->address + s2->size) {
|
|
|
|
s2->size = max(s2->size, s3->address + s3->size - s2->address);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RemoveEntryList(&s3->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size)
|
|
|
|
RemoveEntryList(&s3->list_entry_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(s3);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size) {
|
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
|
|
|
order_space_entry(s2, list_size);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// new entry overlaps start of old one
|
|
|
|
if (address < s2->address && address + length >= s2->address) {
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, true, list, list_size, address, s2->address - address, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s2->size += s2->address - address;
|
|
|
|
s2->address = address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
while (s2->list_entry.Blink != list) {
|
|
|
|
space* s3 = CONTAINING_RECORD(s2->list_entry.Blink, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s3->address + s3->size == s2->address) {
|
|
|
|
s2->address = s3->address;
|
|
|
|
s2->size += s3->size;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RemoveEntryList(&s3->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size)
|
|
|
|
RemoveEntryList(&s3->list_entry_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(s3);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size) {
|
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
|
|
|
order_space_entry(s2, list_size);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// new entry overlaps end of old one
|
|
|
|
if (address <= s2->address + s2->size && address + length > s2->address + s2->size) {
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, true, list, list_size, address, s2->address + s2->size - address, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s2->size = address + length - s2->address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
while (s2->list_entry.Flink != list) {
|
|
|
|
space* s3 = CONTAINING_RECORD(s2->list_entry.Flink, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s3->address <= s2->address + s2->size) {
|
|
|
|
s2->size = max(s2->size, s3->address + s3->size - s2->address);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RemoveEntryList(&s3->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size)
|
|
|
|
RemoveEntryList(&s3->list_entry_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(s3);
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size) {
|
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
|
|
|
order_space_entry(s2, list_size);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// add completely separate entry
|
|
|
|
if (s2->address > address + length) {
|
|
|
|
s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, true, list, list_size, address, length, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s->address = address;
|
|
|
|
s->size = length;
|
|
|
|
InsertHeadList(s2->list_entry.Blink, &s->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size)
|
|
|
|
order_space_entry(s, list_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
le = le->Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
} while (le != list);
|
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// check if contiguous with last entry
|
|
|
|
if (s2->address + s2->size == address) {
|
|
|
|
s2->size += length;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size) {
|
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
|
|
|
order_space_entry(s2, list_size);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
// otherwise, insert at end
|
|
|
|
s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s->address = address;
|
|
|
|
s->size = length;
|
|
|
|
InsertTailList(list, &s->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size)
|
|
|
|
order_space_entry(s, list_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, true, list, list_size, address, length, c);
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
void space_list_merge(LIST_ENTRY* spacelist, LIST_ENTRY* spacelist_size, LIST_ENTRY* deleting) {
|
|
|
|
LIST_ENTRY* le = deleting->Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
while (le != deleting) {
|
|
|
|
space* s = CONTAINING_RECORD(le, space, list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
space_list_add2(spacelist, spacelist_size, s->address, s->size, NULL, NULL);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS copy_space_list(LIST_ENTRY* old_list, LIST_ENTRY* new_list) {
|
|
|
|
LIST_ENTRY* le;
|
|
|
|
|
|
|
|
le = old_list->Flink;
|
|
|
|
while (le != old_list) {
|
|
|
|
space* s = CONTAINING_RECORD(le, space, list_entry);
|
|
|
|
space* s2;
|
|
|
|
|
|
|
|
s2 = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
|
|
|
|
if (!s2) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2022-04-28 19:31:44 +00:00
|
|
|
|
|
|
|
s2->address = s->address;
|
|
|
|
s2->size = s->size;
|
|
|
|
|
|
|
|
InsertTailList(new_list, &s2->list_entry);
|
|
|
|
|
|
|
|
le = le->Flink;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
2022-04-28 19:31:44 +00:00
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
static NTSTATUS update_chunk_cache(device_extension* Vcb, chunk* c, BTRFS_TIME* now, LIST_ENTRY* batchlist, PIRP Irp, LIST_ENTRY* rollback) {
|
2016-05-05 17:26:47 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
FREE_SPACE_ITEM* fsi;
|
|
|
|
void* data;
|
2019-09-01 12:53:20 +00:00
|
|
|
uint64_t num_entries, *cachegen, off;
|
2022-04-28 19:31:44 +00:00
|
|
|
uint32_t *checksums, num_sectors;
|
|
|
|
LIST_ENTRY space_list, deleting;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
data = ExAllocatePoolWithTag(NonPagedPool, (ULONG)c->cache->inode_item.st_size, ALLOC_TAG);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!data) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
RtlZeroMemory(data, (ULONG)c->cache->inode_item.st_size);
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
InitializeListHead(&space_list);
|
|
|
|
InitializeListHead(&deleting);
|
|
|
|
|
|
|
|
Status = copy_space_list(&c->space, &space_list);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
ERR("copy_space_list returned %08lx\n", Status);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = copy_space_list(&c->deleting, &deleting);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
ERR("copy_space_list returned %08lx\n", Status);
|
|
|
|
|
|
|
|
while (!IsListEmpty(&space_list)) {
|
|
|
|
ExFreePool(CONTAINING_RECORD(RemoveHeadList(&space_list), space, list_entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
space_list_merge(&space_list, NULL, &deleting);
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
num_entries = 0;
|
2022-04-28 19:31:44 +00:00
|
|
|
num_sectors = (uint32_t)(c->cache->inode_item.st_size >> Vcb->sector_shift);
|
2019-09-01 12:53:20 +00:00
|
|
|
off = (sizeof(uint32_t) * num_sectors) + sizeof(uint64_t);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
while (!IsListEmpty(&space_list)) {
|
2017-09-08 08:02:43 +00:00
|
|
|
FREE_SPACE_ENTRY* fse;
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
space* s = CONTAINING_RECORD(RemoveHeadList(&space_list), space, list_entry);
|
2016-05-05 17:26:47 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
if ((off + sizeof(FREE_SPACE_ENTRY)) >> Vcb->sector_shift != off >> Vcb->sector_shift)
|
2016-05-05 17:26:47 +00:00
|
|
|
off = sector_align(off, Vcb->superblock.sector_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
fse = (FREE_SPACE_ENTRY*)((uint8_t*)data + off);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
fse->offset = s->address;
|
|
|
|
fse->size = s->size;
|
2016-05-05 17:26:47 +00:00
|
|
|
fse->type = FREE_SPACE_EXTENT;
|
|
|
|
num_entries++;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
off += sizeof(FREE_SPACE_ENTRY);
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update INODE_ITEM
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
c->cache->inode_item.generation = Vcb->superblock.generation;
|
|
|
|
c->cache->inode_item.transid = Vcb->superblock.generation;
|
|
|
|
c->cache->inode_item.sequence++;
|
|
|
|
c->cache->inode_item.st_ctime = *now;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = flush_fcb(c->cache, true, batchlist, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("flush_fcb returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// update free_space item
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
searchkey.obj_id = FREE_SPACE_CACHE_ID;
|
|
|
|
searchkey.obj_type = 0;
|
|
|
|
searchkey.offset = c->offset;
|
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, Irp);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("error - find_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
goto end;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
if (keycmp(searchkey, tp.item->key)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ERR("could not find (%I64x,%x,%I64x) in root_root\n", searchkey.obj_id, searchkey.obj_type, searchkey.offset);
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = STATUS_INTERNAL_ERROR;
|
|
|
|
goto end;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
if (tp.item->size < sizeof(FREE_SPACE_ITEM)) {
|
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(FREE_SPACE_ITEM));
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = STATUS_INTERNAL_ERROR;
|
|
|
|
goto end;
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
fsi = (FREE_SPACE_ITEM*)tp.item->data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
fsi->generation = Vcb->superblock.generation;
|
|
|
|
fsi->num_entries = num_entries;
|
|
|
|
fsi->num_bitmaps = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// set cache generation
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
cachegen = (uint64_t*)((uint8_t*)data + (sizeof(uint32_t) * num_sectors));
|
2016-05-05 17:26:47 +00:00
|
|
|
*cachegen = Vcb->superblock.generation;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// calculate cache checksums
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
checksums = (uint32_t*)data;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// FIXME - if we know sector is fully zeroed, use cached checksum
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
for (uint32_t i = 0; i < num_sectors; i++) {
|
|
|
|
if (i << Vcb->sector_shift > sizeof(uint32_t) * num_sectors)
|
|
|
|
checksums[i] = ~calc_crc32c(0xffffffff, (uint8_t*)data + (i << Vcb->sector_shift), Vcb->superblock.sector_size);
|
|
|
|
else if ((i + 1) << Vcb->sector_shift < sizeof(uint32_t) * num_sectors)
|
2016-05-05 17:26:47 +00:00
|
|
|
checksums[i] = 0; // FIXME - test this
|
|
|
|
else
|
2022-04-28 19:31:44 +00:00
|
|
|
checksums[i] = ~calc_crc32c(0xffffffff, (uint8_t*)data + (sizeof(uint32_t) * num_sectors), ((i + 1) << Vcb->sector_shift) - (sizeof(uint32_t) * num_sectors));
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
// write cache
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
Status = do_write_file(c->cache, 0, c->cache->inode_item.st_size, data, NULL, false, 0, rollback);
|
2016-05-05 17:26:47 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("do_write_file returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
// Writing the cache isn't critical, so we don't return an error if writing fails. This means
|
|
|
|
// we can still flush on a degraded mount if metadata is RAID1 but data is RAID0.
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
|
|
|
|
end:
|
2016-05-05 17:26:47 +00:00
|
|
|
ExFreePool(data);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
static NTSTATUS update_chunk_cache_tree(device_extension* Vcb, chunk* c, PIRP Irp) {
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS Status;
|
2022-04-28 19:31:44 +00:00
|
|
|
LIST_ENTRY space_list;
|
2017-09-08 08:02:43 +00:00
|
|
|
FREE_SPACE_INFO* fsi;
|
2022-04-28 19:31:44 +00:00
|
|
|
KEY searchkey;
|
|
|
|
traverse_ptr tp;
|
|
|
|
uint32_t fsi_count = 0;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
InitializeListHead(&space_list);
|
|
|
|
|
|
|
|
Status = copy_space_list(&c->space, &space_list);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
ERR("copy_space_list returned %08lx\n", Status);
|
|
|
|
return Status;
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
space_list_merge(&space_list, NULL, &c->deleting);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
searchkey.obj_id = c->offset;
|
|
|
|
searchkey.obj_type = TYPE_FREE_SPACE_EXTENT;
|
|
|
|
searchkey.offset = 0xffffffffffffffff;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
Status = find_item(Vcb, Vcb->space_root, &tp, &searchkey, false, Irp);
|
|
|
|
if (Status == STATUS_NOT_FOUND)
|
|
|
|
goto after_tree_walk;
|
|
|
|
else if (!NT_SUCCESS(Status)) {
|
|
|
|
ERR("find_item returned %08lx\n", Status);
|
|
|
|
|
|
|
|
while (!IsListEmpty(&space_list)) {
|
|
|
|
ExFreePool(CONTAINING_RECORD(RemoveHeadList(&space_list), space, list_entry));
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
return Status;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
while (!IsListEmpty(&space_list) && tp.item->key.obj_id < c->offset + c->chunk_item->size) {
|
|
|
|
traverse_ptr next_tp;
|
|
|
|
|
|
|
|
if (tp.item->key.obj_type == TYPE_FREE_SPACE_EXTENT) {
|
|
|
|
space* s = CONTAINING_RECORD(space_list.Flink, space, list_entry);
|
|
|
|
|
|
|
|
if (s->address < tp.item->key.obj_id || (s->address == tp.item->key.obj_id && s->size < tp.item->key.offset)) {
|
|
|
|
// add entry
|
|
|
|
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->space_root, s->address, TYPE_FREE_SPACE_EXTENT, s->size, NULL, 0,
|
|
|
|
NULL, Irp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
|
|
|
|
|
|
|
while (!IsListEmpty(&space_list)) {
|
|
|
|
ExFreePool(CONTAINING_RECORD(RemoveHeadList(&space_list), space, list_entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
fsi_count++;
|
|
|
|
|
|
|
|
RemoveHeadList(&space_list);
|
2022-09-28 16:08:10 +00:00
|
|
|
ExFreePool(s);
|
2022-04-28 19:31:44 +00:00
|
|
|
continue;
|
|
|
|
} else if (s->address == tp.item->key.obj_id && s->size == tp.item->key.offset) {
|
|
|
|
// unchanged entry
|
|
|
|
|
|
|
|
fsi_count++;
|
|
|
|
|
|
|
|
RemoveHeadList(&space_list);
|
2022-09-28 16:08:10 +00:00
|
|
|
ExFreePool(s);
|
2022-04-28 19:31:44 +00:00
|
|
|
} else {
|
|
|
|
// remove entry
|
|
|
|
|
|
|
|
Status = delete_tree_item(Vcb, &tp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
|
|
|
|
|
|
|
while (!IsListEmpty(&space_list)) {
|
|
|
|
ExFreePool(CONTAINING_RECORD(RemoveHeadList(&space_list), space, list_entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (tp.item->key.obj_type == TYPE_FREE_SPACE_BITMAP) {
|
|
|
|
Status = delete_tree_item(Vcb, &tp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
|
|
|
|
|
|
|
while (!IsListEmpty(&space_list)) {
|
|
|
|
ExFreePool(CONTAINING_RECORD(RemoveHeadList(&space_list), space, list_entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!find_next_item(Vcb, &tp, &next_tp, false, Irp))
|
|
|
|
goto after_tree_walk;
|
|
|
|
|
|
|
|
tp = next_tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// after loop, delete remaining tree items for this chunk
|
|
|
|
|
|
|
|
while (tp.item->key.obj_id < c->offset + c->chunk_item->size) {
|
|
|
|
traverse_ptr next_tp;
|
|
|
|
|
|
|
|
if (tp.item->key.obj_type == TYPE_FREE_SPACE_EXTENT || tp.item->key.obj_type == TYPE_FREE_SPACE_BITMAP) {
|
|
|
|
Status = delete_tree_item(Vcb, &tp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
|
|
|
ERR("delete_tree_item returned %08lx\n", Status);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!find_next_item(Vcb, &tp, &next_tp, false, NULL))
|
|
|
|
break;
|
|
|
|
|
|
|
|
tp = next_tp;
|
|
|
|
}
|
|
|
|
|
|
|
|
after_tree_walk:
|
|
|
|
// after loop, insert remaining space_list entries
|
|
|
|
|
|
|
|
while (!IsListEmpty(&space_list)) {
|
|
|
|
space* s = CONTAINING_RECORD(RemoveHeadList(&space_list), space, list_entry);
|
|
|
|
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->space_root, s->address, TYPE_FREE_SPACE_EXTENT, s->size, NULL, 0,
|
|
|
|
NULL, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2022-04-28 19:31:44 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
|
|
|
|
|
|
|
while (!IsListEmpty(&space_list)) {
|
|
|
|
ExFreePool(CONTAINING_RECORD(RemoveHeadList(&space_list), space, list_entry));
|
|
|
|
}
|
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
fsi_count++;
|
|
|
|
|
|
|
|
ExFreePool(s);
|
2017-09-08 08:02:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
// change TYPE_FREE_SPACE_INFO in place if present, and insert otherwise
|
|
|
|
|
|
|
|
searchkey.obj_id = c->offset;
|
|
|
|
searchkey.obj_type = TYPE_FREE_SPACE_INFO;
|
|
|
|
searchkey.offset = c->chunk_item->size;
|
|
|
|
|
|
|
|
Status = find_item(Vcb, Vcb->space_root, &tp, &searchkey, false, Irp);
|
|
|
|
if (!NT_SUCCESS(Status) && Status != STATUS_NOT_FOUND) {
|
|
|
|
ERR("find_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
if (NT_SUCCESS(Status) && !keycmp(tp.item->key, searchkey)) {
|
|
|
|
if (tp.item->size == sizeof(FREE_SPACE_INFO)) {
|
|
|
|
tree* t;
|
|
|
|
|
|
|
|
// change in place if possible
|
|
|
|
|
|
|
|
fsi = (FREE_SPACE_INFO*)tp.item->data;
|
|
|
|
|
|
|
|
fsi->count = fsi_count;
|
|
|
|
fsi->flags = 0;
|
|
|
|
|
|
|
|
tp.tree->write = true;
|
|
|
|
|
|
|
|
t = tp.tree;
|
|
|
|
while (t) {
|
|
|
|
if (t->paritem && t->paritem->ignore) {
|
|
|
|
t->paritem->ignore = false;
|
|
|
|
t->parent->header.num_items++;
|
|
|
|
t->parent->size += sizeof(internal_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
t->header.generation = Vcb->superblock.generation;
|
|
|
|
t = t->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
} else
|
|
|
|
delete_tree_item(Vcb, &tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert FREE_SPACE_INFO
|
|
|
|
|
|
|
|
fsi = ExAllocatePoolWithTag(PagedPool, sizeof(FREE_SPACE_INFO), ALLOC_TAG);
|
|
|
|
if (!fsi) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
fsi->count = fsi_count;
|
|
|
|
fsi->flags = 0;
|
|
|
|
|
|
|
|
Status = insert_tree_item(Vcb, Vcb->space_root, c->offset, TYPE_FREE_SPACE_INFO, c->chunk_item->size, fsi, sizeof(fsi),
|
|
|
|
NULL, Irp);
|
2017-09-08 08:02:43 +00:00
|
|
|
if (!NT_SUCCESS(Status)) {
|
2022-04-28 19:31:44 +00:00
|
|
|
ERR("insert_tree_item returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:27:46 +00:00
|
|
|
NTSTATUS update_chunk_caches(device_extension* Vcb, PIRP Irp, LIST_ENTRY* rollback) {
|
2017-09-08 08:02:43 +00:00
|
|
|
LIST_ENTRY *le, batchlist;
|
2016-05-05 17:26:47 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
chunk* c;
|
|
|
|
LARGE_INTEGER time;
|
|
|
|
BTRFS_TIME now;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
KeQuerySystemTime(&time);
|
|
|
|
win_time_to_unix(time, &now);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-10-29 17:05:10 +00:00
|
|
|
InitializeListHead(&batchlist);
|
2016-05-05 17:26:47 +00:00
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
le = Vcb->chunks.Flink;
|
|
|
|
while (le != &Vcb->chunks) {
|
|
|
|
c = CONTAINING_RECORD(le, chunk, list_entry);
|
|
|
|
|
2019-05-11 09:20:02 +00:00
|
|
|
if (c->space_changed && c->chunk_item->size >= 0x6400000) { // 100MB
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
Status = update_chunk_cache(Vcb, c, &now, &batchlist, Irp, rollback);
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("update_chunk_cache(%I64x) returned %08lx\n", c->offset, Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
clear_batch_list(Vcb, &batchlist);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = commit_batch_list(Vcb, &batchlist, Irp);
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("commit_batch_list returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
le = Vcb->chunks.Flink;
|
|
|
|
while (le != &Vcb->chunks) {
|
|
|
|
c = CONTAINING_RECORD(le, chunk, list_entry);
|
|
|
|
|
|
|
|
if (c->changed && (c->chunk_item->type & BLOCK_FLAG_RAID5 || c->chunk_item->type & BLOCK_FLAG_RAID6)) {
|
2019-09-01 12:53:20 +00:00
|
|
|
ExAcquireResourceExclusiveLite(&c->partial_stripes_lock, true);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
while (!IsListEmpty(&c->partial_stripes)) {
|
|
|
|
partial_stripe* ps = CONTAINING_RECORD(RemoveHeadList(&c->partial_stripes), partial_stripe, list_entry);
|
|
|
|
|
|
|
|
Status = flush_partial_stripe(Vcb, c, ps);
|
|
|
|
|
|
|
|
if (ps->bmparr)
|
|
|
|
ExFreePool(ps->bmparr);
|
|
|
|
|
|
|
|
ExFreePool(ps);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("flush_partial_stripe returned %08lx\n", Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&c->partial_stripes_lock);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&c->partial_stripes_lock);
|
2016-05-05 17:26:47 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
le = le->Flink;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-05-05 17:26:47 +00:00
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
2016-07-27 19:24:26 +00:00
|
|
|
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS update_chunk_caches_tree(device_extension* Vcb, PIRP Irp) {
|
2022-04-28 19:31:44 +00:00
|
|
|
LIST_ENTRY *le;
|
2017-09-08 08:02:43 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
chunk* c;
|
|
|
|
|
|
|
|
Vcb->superblock.compat_ro_flags |= BTRFS_COMPAT_RO_FLAGS_FREE_SPACE_CACHE_VALID;
|
|
|
|
|
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) {
|
|
|
|
c = CONTAINING_RECORD(le, chunk, list_entry);
|
|
|
|
|
|
|
|
if (c->space_changed) {
|
2018-12-16 11:03:16 +00:00
|
|
|
acquire_chunk_lock(c, Vcb);
|
2022-04-28 19:31:44 +00:00
|
|
|
Status = update_chunk_cache_tree(Vcb, c, Irp);
|
2018-12-16 11:03:16 +00:00
|
|
|
release_chunk_lock(c, Vcb);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status)) {
|
2020-04-23 02:38:57 +00:00
|
|
|
ERR("update_chunk_cache_tree(%I64x) returned %08lx\n", c->offset, Status);
|
2017-09-08 08:02:43 +00:00
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
le = le->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExReleaseResourceLite(&Vcb->chunk_lock);
|
|
|
|
|
|
|
|
return STATUS_SUCCESS;
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
void space_list_add(chunk* c, uint64_t address, uint64_t length, LIST_ENTRY* rollback) {
|
|
|
|
TRACE("(%p, %I64x, %I64x, %p)\n", c, address, length, rollback);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
c->changed = true;
|
|
|
|
c->space_changed = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
space_list_add2(&c->deleting, NULL, address, length, c, rollback);
|
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
void space_list_subtract2(LIST_ENTRY* list, LIST_ENTRY* list_size, uint64_t address, uint64_t length, chunk* c, LIST_ENTRY* rollback) {
|
2016-07-27 19:24:26 +00:00
|
|
|
LIST_ENTRY *le, *le2;
|
|
|
|
space *s, *s2;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (IsListEmpty(list))
|
|
|
|
return;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = list->Flink;
|
|
|
|
while (le != list) {
|
|
|
|
s2 = CONTAINING_RECORD(le, space, list_entry);
|
|
|
|
le2 = le->Flink;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s2->address >= address + length)
|
|
|
|
return;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (s2->address >= address && s2->address + s2->size <= address + length) { // remove entry entirely
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, false, list, list_size, s2->address, s2->size, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
RemoveEntryList(&s2->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size)
|
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
ExFreePool(s2);
|
|
|
|
} else if (address + length > s2->address && address + length < s2->address + s2->size) {
|
|
|
|
if (address > s2->address) { // cut out hole
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, false, list, list_size, address, length, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s = ExAllocatePoolWithTag(PagedPool, sizeof(space), ALLOC_TAG);
|
|
|
|
|
|
|
|
if (!s) {
|
|
|
|
ERR("out of memory\n");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s->address = s2->address;
|
|
|
|
s->size = address - s2->address;
|
|
|
|
InsertHeadList(s2->list_entry.Blink, &s->list_entry);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s2->size = s2->address + s2->size - address - length;
|
|
|
|
s2->address = address + length;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size) {
|
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
|
|
|
order_space_entry(s2, list_size);
|
|
|
|
order_space_entry(s, list_size);
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
return;
|
|
|
|
} else { // remove start of entry
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, false, list, list_size, s2->address, address + length - s2->address, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s2->size -= address + length - s2->address;
|
|
|
|
s2->address = address + length;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size) {
|
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
|
|
|
order_space_entry(s2, list_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (address > s2->address && address < s2->address + s2->size) { // remove end of entry
|
2016-09-04 15:27:46 +00:00
|
|
|
if (rollback)
|
2019-09-01 12:53:20 +00:00
|
|
|
add_rollback_space(rollback, false, list, list_size, address, s2->address + s2->size - address, c);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
s2->size = address - s2->address;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
if (list_size) {
|
|
|
|
RemoveEntryList(&s2->list_entry_size);
|
|
|
|
order_space_entry(s2, list_size);
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-07-27 19:24:26 +00:00
|
|
|
le = le2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
void space_list_subtract(chunk* c, uint64_t address, uint64_t length, LIST_ENTRY* rollback) {
|
2019-09-01 12:53:20 +00:00
|
|
|
c->changed = true;
|
|
|
|
c->space_changed = true;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2022-04-28 19:31:44 +00:00
|
|
|
space_list_subtract2(&c->space, &c->space_size, address, length, c, rollback);
|
|
|
|
|
|
|
|
space_list_subtract2(&c->deleting, NULL, address, length, c, rollback);
|
2016-07-27 19:24:26 +00:00
|
|
|
}
|