2017-09-08 08:02:43 +00:00
|
|
|
/* Copyright (c) Mark Harmstone 2016-17
|
|
|
|
*
|
2016-03-23 20:35:05 +00:00
|
|
|
* This file is part of WinBtrfs.
|
2017-09-08 08:02:43 +00:00
|
|
|
*
|
2016-03-23 20:35:05 +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-03-23 20:35:05 +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-03-23 20:35:05 +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"
|
|
|
|
|
2019-11-12 18:32:46 +00:00
|
|
|
CACHE_MANAGER_CALLBACKS cache_callbacks;
|
2016-03-23 20:35:05 +00:00
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static BOOLEAN __stdcall acquire_for_lazy_write(PVOID Context, BOOLEAN Wait) {
|
2016-03-23 20:35:05 +00:00
|
|
|
PFILE_OBJECT FileObject = Context;
|
|
|
|
fcb* fcb = FileObject->FsContext;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-03-23 20:35:05 +00:00
|
|
|
TRACE("(%p, %u)\n", Context, Wait);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!ExAcquireResourceSharedLite(&fcb->Vcb->tree_lock, Wait))
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2016-03-26 11:53:07 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
if (!ExAcquireResourceExclusiveLite(fcb->Header.Resource, Wait)) {
|
|
|
|
ExReleaseResourceLite(&fcb->Vcb->tree_lock);
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-01-01 17:12:12 +00:00
|
|
|
}
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-03-23 20:35:05 +00:00
|
|
|
fcb->lazy_writer_thread = KeGetCurrentThread();
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
return true;
|
2016-03-23 20:35:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static void __stdcall release_from_lazy_write(PVOID Context) {
|
2016-03-23 20:35:05 +00:00
|
|
|
PFILE_OBJECT FileObject = Context;
|
|
|
|
fcb* fcb = FileObject->FsContext;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-03-23 20:35:05 +00:00
|
|
|
TRACE("(%p)\n", Context);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2016-03-23 20:35:05 +00:00
|
|
|
fcb->lazy_writer_thread = NULL;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(fcb->Header.Resource);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
2017-01-01 17:12:12 +00:00
|
|
|
ExReleaseResourceLite(&fcb->Vcb->tree_lock);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (IoGetTopLevelIrp() == (PIRP)FSRTL_CACHE_TOP_LEVEL_IRP)
|
|
|
|
IoSetTopLevelIrp(NULL);
|
2016-03-23 20:35:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static BOOLEAN __stdcall acquire_for_read_ahead(PVOID Context, BOOLEAN Wait) {
|
2017-09-08 08:02:43 +00:00
|
|
|
PFILE_OBJECT FileObject = Context;
|
|
|
|
fcb* fcb = FileObject->FsContext;
|
|
|
|
|
2016-03-23 20:35:05 +00:00
|
|
|
TRACE("(%p, %u)\n", Context, Wait);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
if (!ExAcquireResourceSharedLite(fcb->Header.Resource, Wait))
|
2019-09-01 12:53:20 +00:00
|
|
|
return false;
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
IoSetTopLevelIrp((PIRP)FSRTL_CACHE_TOP_LEVEL_IRP);
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
return true;
|
2016-03-23 20:35:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-01 12:53:20 +00:00
|
|
|
static void __stdcall release_from_read_ahead(PVOID Context) {
|
2017-09-08 08:02:43 +00:00
|
|
|
PFILE_OBJECT FileObject = Context;
|
|
|
|
fcb* fcb = FileObject->FsContext;
|
|
|
|
|
2016-03-23 20:35:05 +00:00
|
|
|
TRACE("(%p)\n", Context);
|
2017-09-08 08:02:43 +00:00
|
|
|
|
|
|
|
ExReleaseResourceLite(fcb->Header.Resource);
|
|
|
|
|
|
|
|
if (IoGetTopLevelIrp() == (PIRP)FSRTL_CACHE_TOP_LEVEL_IRP)
|
|
|
|
IoSetTopLevelIrp(NULL);
|
2016-03-23 20:35:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 18:32:46 +00:00
|
|
|
void init_cache() {
|
|
|
|
cache_callbacks.AcquireForLazyWrite = acquire_for_lazy_write;
|
|
|
|
cache_callbacks.ReleaseFromLazyWrite = release_from_lazy_write;
|
|
|
|
cache_callbacks.AcquireForReadAhead = acquire_for_read_ahead;
|
|
|
|
cache_callbacks.ReleaseFromReadAhead = release_from_read_ahead;
|
2016-03-23 20:35:05 +00:00
|
|
|
}
|