file_cache.cpp revision 3cd20943
1f72376a8SAxel Dörfler/*
26102f078SAxel Dörfler * Copyright 2004-2009, Axel D��rfler,
3eb9f0103SAxel Dörfler * Distributed under the terms of the MIT License.
4eb9f0103SAxel Dörfler */
5f72376a8SAxel Dörfler
6f72376a8SAxel Dörfler
7f72376a8SAxel Dörfler#include "vnode_store.h"
8f72376a8SAxel Dörfler
90d871d3cSAxel Dörfler#include <unistd.h>
100d871d3cSAxel Dörfler#include <stdlib.h>
110d871d3cSAxel Dörfler#include <string.h>
120d871d3cSAxel Dörfler
13f72376a8SAxel Dörfler#include <KernelExport.h>
14f72376a8SAxel Dörfler#include <fs_cache.h>
15f72376a8SAxel Dörfler
16279c6b76SIngo Weinhold#include <condition_variable.h>
17f72376a8SAxel Dörfler#include <file_cache.h>
18279c6b76SIngo Weinhold#include <generic_syscall.h>
195c99d639SIngo Weinhold#include <low_resource_manager.h>
207b8683b2SIngo Weinhold#include <thread.h>
210d871d3cSAxel Dörfler#include <util/AutoLock.h>
22279c6b76SIngo Weinhold#include <util/kernel_cpp.h>
23f72376a8SAxel Dörfler#include <vfs.h>
24e50cf876SIngo Weinhold#include <vm/vm.h>
25e50cf876SIngo Weinhold#include <vm/vm_page.h>
26e50cf876SIngo Weinhold#include <vm/VMCache.h>
27f72376a8SAxel Dörfler
28aa4ba93eSIngo Weinhold#include "IORequest.h"
297f12cc54SIngo Weinhold
30f72376a8SAxel Dörfler
31f72376a8SAxel Dörfler//#define TRACE_FILE_CACHE
32f72376a8SAxel Dörfler#ifdef TRACE_FILE_CACHE
33f72376a8SAxel Dörfler#	define TRACE(x) dprintf x
34f72376a8SAxel Dörfler#else
35f72376a8SAxel Dörfler#	define TRACE(x) ;
36f72376a8SAxel Dörfler#endif
37f72376a8SAxel Dörfler
380f6c560eSAxel Dörfler// maximum number of iovecs per request
39279c6b76SIngo Weinhold#define MAX_IO_VECS			32	// 128 kB
400f6c560eSAxel Dörfler#define MAX_FILE_IO_VECS	32
4111a3346cSAxel Dörfler
42cfe386c2SAxel Dörfler#define BYPASS_IO_SIZE		65536
43c6573329SAxel Dörfler#define LAST_ACCESSES		3
44c6573329SAxel Dörfler
45f72376a8SAxel Dörflerstruct file_cache_ref {
46a477e3cfSIngo Weinhold	VMCache			*cache;
4780f54692SAxel Dörfler	struct vnode	*vnode;
48c6573329SAxel Dörfler	off_t			last_access[LAST_ACCESSES];
49c6573329SAxel Dörfler		// TODO: it would probably be enough to only store the least
50c6573329SAxel Dörfler		//	significant 31 bits, and make this uint32 (one bit for
51c6573329SAxel Dörfler		//	write vs. read)
52c6573329SAxel Dörfler	int32			last_access_index;
537491000fSIngo Weinhold	uint16			disabled_count;
548a26f35aSAxel Dörfler
558a26f35aSAxel Dörfler	inline void SetLastAccess(int32 index, off_t access, bool isWrite)
568a26f35aSAxel Dörfler	{
578a26f35aSAxel Dörfler		// we remember writes as negative offsets
588a26f35aSAxel Dörfler		last_access[index] = isWrite ? -access : access;
598a26f35aSAxel Dörfler	}
608a26f35aSAxel Dörfler
618a26f35aSAxel Dörfler	inline off_t LastAccess(int32 index, bool isWrite)
628a26f35aSAxel Dörfler	{
638a26f35aSAxel Dörfler		return isWrite ? -last_access[index] : last_access[index];
648a26f35aSAxel Dörfler	}
658a26f35aSAxel Dörfler
668a26f35aSAxel Dörfler	inline uint32 LastAccessPageOffset(int32 index, bool isWrite)
678a26f35aSAxel Dörfler	{
688a26f35aSAxel Dörfler		return LastAccess(index, isWrite) >> PAGE_SHIFT;
698a26f35aSAxel Dörfler	}
70f72376a8SAxel Dörfler};
71f72376a8SAxel Dörfler
72eb2bd0e8SStephan Aßmusclass PrecacheIO : public AsyncIOCallback {
73eb2bd0e8SStephan Aßmuspublic:
74eb2bd0e8SStephan Aßmus								PrecacheIO(file_cache_ref* ref, off_t offset,
75eb2bd0e8SStephan Aßmus									size_t size);
76eb2bd0e8SStephan Aßmus								~PrecacheIO();
77eb2bd0e8SStephan Aßmus
78a5dbd78bSAxel Dörfler			status_t			Prepare();
79a5dbd78bSAxel Dörfler			void				ReadAsync();
80eb2bd0e8SStephan Aßmus
81eb2bd0e8SStephan Aßmus	virtual	void				IOFinished(status_t status,
82eb2bd0e8SStephan Aßmus									bool partialTransfer,
83eb2bd0e8SStephan Aßmus									size_t bytesTransferred);
84eb2bd0e8SStephan Aßmus
85eb2bd0e8SStephan Aßmusprivate:
86eb2bd0e8SStephan Aßmus			file_cache_ref*		fRef;
87eb2bd0e8SStephan Aßmus			VMCache*			fCache;
88eb2bd0e8SStephan Aßmus			vm_page**			fPages;
89eb2bd0e8SStephan Aßmus			size_t				fPageCount;
90eb2bd0e8SStephan Aßmus			ConditionVariable*	fBusyConditions;
91eb2bd0e8SStephan Aßmus			iovec*				fVecs;
92eb2bd0e8SStephan Aßmus			off_t				fOffset;
93a5dbd78bSAxel Dörfler			uint32				fVecCount;
94eb2bd0e8SStephan Aßmus			size_t				fSize;
953cd20943SIngo Weinhold#if DEBUG_PAGE_ACCESS
963cd20943SIngo Weinhold			thread_id			fAllocatingThread;
973cd20943SIngo Weinhold#endif
98eb2bd0e8SStephan Aßmus};
99eb2bd0e8SStephan Aßmus
10061b6f38cSAxel Dörflertypedef status_t (*cache_func)(file_cache_ref* ref, void* cookie, off_t offset,
101a121b8c8SAxel Dörfler	int32 pageOffset, addr_t buffer, size_t bufferSize, bool useBuffer,
10209149281SAxel Dörfler	size_t lastReservedPages, size_t reservePages);
10309149281SAxel Dörfler
10461b6f38cSAxel Dörflerstatic void add_to_iovec(iovec* vecs, uint32 &index, uint32 max, addr_t address,
105eb2bd0e8SStephan Aßmus	size_t size);
106eb2bd0e8SStephan Aßmus
107f72376a8SAxel Dörfler
10861b6f38cSAxel Dörflerstatic struct cache_module_info* sCacheModule;
1097491000fSIngo Weinholdstatic const uint8 kZeroBuffer[4096] = {};
110324fc66bSAxel Dörfler
111324fc66bSAxel Dörfler
11211a3346cSAxel Dörfler//	#pragma mark -
11311a3346cSAxel Dörfler
11411a3346cSAxel Dörfler
115eb2bd0e8SStephan AßmusPrecacheIO::PrecacheIO(file_cache_ref* ref, off_t offset, size_t size)
116eb2bd0e8SStephan Aßmus	:
117eb2bd0e8SStephan Aßmus	fRef(ref),
118eb2bd0e8SStephan Aßmus	fCache(ref->cache),
119eb2bd0e8SStephan Aßmus	fPages(NULL),
120eb2bd0e8SStephan Aßmus	fVecs(NULL),
121eb2bd0e8SStephan Aßmus	fOffset(offset),
122a5dbd78bSAxel Dörfler	fVecCount(0),
123eb2bd0e8SStephan Aßmus	fSize(size)
124eb2bd0e8SStephan Aßmus{
125eb2bd0e8SStephan Aßmus	fPageCount = (size + B_PAGE_SIZE - 1) / B_PAGE_SIZE;
126eb2bd0e8SStephan Aßmus	fCache->AcquireRefLocked();
127eb2bd0e8SStephan Aßmus}
128eb2bd0e8SStephan Aßmus
129eb2bd0e8SStephan Aßmus
130eb2bd0e8SStephan AßmusPrecacheIO::~PrecacheIO()
131eb2bd0e8SStephan Aßmus{
132eb2bd0e8SStephan Aßmus	delete[] fPages;
133eb2bd0e8SStephan Aßmus	delete[] fVecs;
134eb2bd0e8SStephan Aßmus	fCache->ReleaseRefLocked();
135eb2bd0e8SStephan Aßmus}
136eb2bd0e8SStephan Aßmus
137eb2bd0e8SStephan Aßmus
138eb2bd0e8SStephan Aßmusstatus_t
139a5dbd78bSAxel DörflerPrecacheIO::Prepare()
140eb2bd0e8SStephan Aßmus{
141eb2bd0e8SStephan Aßmus	if (fPageCount == 0)
142eb2bd0e8SStephan Aßmus		return B_BAD_VALUE;
143eb2bd0e8SStephan Aßmus
144eb2bd0e8SStephan Aßmus	fPages = new(std::nothrow) vm_page*[fPageCount];
145eb2bd0e8SStephan Aßmus	if (fPages == NULL)
146eb2bd0e8SStephan Aßmus		return B_NO_MEMORY;
147eb2bd0e8SStephan Aßmus
148eb2bd0e8SStephan Aßmus	fVecs = new(std::nothrow) iovec[fPageCount];
149eb2bd0e8SStephan Aßmus	if (fVecs == NULL)
150eb2bd0e8SStephan Aßmus		return B_NO_MEMORY;
151eb2bd0e8SStephan Aßmus
152eb2bd0e8SStephan Aßmus	// allocate pages for the cache and mark them busy
153eb2bd0e8SStephan Aßmus	uint32 i = 0;
154eb2bd0e8SStephan Aßmus	for (size_t pos = 0; pos < fSize; pos += B_PAGE_SIZE) {
1551021fd28SIngo Weinhold		vm_page* page = vm_page_allocate_page(PAGE_STATE_FREE);
156eb2bd0e8SStephan Aßmus
157eb2bd0e8SStephan Aßmus		fCache->InsertPage(page, fOffset + pos);
158eb2bd0e8SStephan Aßmus
159a5dbd78bSAxel Dörfler		add_to_iovec(fVecs, fVecCount, fPageCount,
160eb2bd0e8SStephan Aßmus			page->physical_page_number * B_PAGE_SIZE, B_PAGE_SIZE);
161d5ad7629SAxel Dörfler		fPages[i++] = page;
162eb2bd0e8SStephan Aßmus	}
163eb2bd0e8SStephan Aßmus
1643cd20943SIngo Weinhold#if DEBUG_PAGE_ACCESS
1653cd20943SIngo Weinhold	fAllocatingThread = find_thread(NULL);
1663cd20943SIngo Weinhold#endif
167eb2bd0e8SStephan Aßmus
168a5dbd78bSAxel Dörfler	return B_OK;
169a5dbd78bSAxel Dörfler}
170aff0fbbeSStephan Aßmus
171aff0fbbeSStephan Aßmus
172a5dbd78bSAxel Dörflervoid
173a5dbd78bSAxel DörflerPrecacheIO::ReadAsync()
174a5dbd78bSAxel Dörfler{
175a5dbd78bSAxel Dörfler	// This object is going to be deleted after the I/O request has been
176a5dbd78bSAxel Dörfler	// fulfilled
177a5dbd78bSAxel Dörfler	vfs_asynchronous_read_pages(fRef->vnode, NULL, fOffset, fVecs, fVecCount,
178a5dbd78bSAxel Dörfler		fSize, B_PHYSICAL_IO_REQUEST, this);
179eb2bd0e8SStephan Aßmus}
180eb2bd0e8SStephan Aßmus
181eb2bd0e8SStephan Aßmus
182eb2bd0e8SStephan Aßmusvoid
183eb2bd0e8SStephan AßmusPrecacheIO::IOFinished(status_t status, bool partialTransfer,
184eb2bd0e8SStephan Aßmus	size_t bytesTransferred)
185eb2bd0e8SStephan Aßmus{
186eb2bd0e8SStephan Aßmus	AutoLocker<VMCache> locker(fCache);
187eb2bd0e8SStephan Aßmus
188eb2bd0e8SStephan Aßmus	// Make successfully loaded pages accessible again (partially
189eb2bd0e8SStephan Aßmus	// transferred pages are considered failed)
190c32499b4SAxel Dörfler	size_t pagesTransferred
191c32499b4SAxel Dörfler		= (bytesTransferred + B_PAGE_SIZE - 1) / B_PAGE_SIZE;
192c32499b4SAxel Dörfler
193c32499b4SAxel Dörfler	if (fOffset + bytesTransferred > fCache->virtual_end)
194c32499b4SAxel Dörfler		bytesTransferred = fCache->virtual_end - fOffset;
195c32499b4SAxel Dörfler
196eb2bd0e8SStephan Aßmus	for (uint32 i = 0; i < pagesTransferred; i++) {
197c32499b4SAxel Dörfler		if (i == pagesTransferred - 1
198c32499b4SAxel Dörfler			&& (bytesTransferred % B_PAGE_SIZE) != 0) {
199c32499b4SAxel Dörfler			// clear partial page
200c32499b4SAxel Dörfler			size_t bytesTouched = bytesTransferred % B_PAGE_SIZE;
201c32499b4SAxel Dörfler			vm_memset_physical((fPages[i]->physical_page_number << PAGE_SHIFT)
202c32499b4SAxel Dörfler				+ bytesTouched, 0, B_PAGE_SIZE - bytesTouched);
203c32499b4SAxel Dörfler		}
204c32499b4SAxel Dörfler
2053cd20943SIngo Weinhold		DEBUG_PAGE_ACCESS_TRANSFER(fPages[i], fAllocatingThread);
2063cd20943SIngo Weinhold
207eb2bd0e8SStephan Aßmus		fPages[i]->state = PAGE_STATE_ACTIVE;
208522c2f19SIngo Weinhold		fCache->NotifyPageEvents(fPages[i], PAGE_EVENT_NOT_BUSY);
2093cd20943SIngo Weinhold
2103cd20943SIngo Weinhold		DEBUG_PAGE_ACCESS_END(fPages[i]);
211eb2bd0e8SStephan Aßmus	}
212eb2bd0e8SStephan Aßmus
213eb2bd0e8SStephan Aßmus	// Free pages after failed I/O
214eb2bd0e8SStephan Aßmus	for (uint32 i = pagesTransferred; i < fPageCount; i++) {
2153cd20943SIngo Weinhold		DEBUG_PAGE_ACCESS_TRANSFER(fPages[i], fAllocatingThread);
216522c2f19SIngo Weinhold		fCache->NotifyPageEvents(fPages[i], PAGE_EVENT_NOT_BUSY);
217eb2bd0e8SStephan Aßmus		fCache->RemovePage(fPages[i]);
218eb2bd0e8SStephan Aßmus		vm_page_set_state(fPages[i], PAGE_STATE_FREE);
219eb2bd0e8SStephan Aßmus	}
220eb2bd0e8SStephan Aßmus
221eb2bd0e8SStephan Aßmus	delete this;
222eb2bd0e8SStephan Aßmus}
223eb2bd0e8SStephan Aßmus
224eb2bd0e8SStephan Aßmus
225eb2bd0e8SStephan Aßmus//	#pragma mark -
226eb2bd0e8SStephan Aßmus
227eb2bd0e8SStephan Aßmus
228f72376a8SAxel Dörflerstatic void
22961b6f38cSAxel Dörfleradd_to_iovec(iovec* vecs, uint32 &index, uint32 max, addr_t address,
230eb2bd0e8SStephan Aßmus	size_t size)
231f72376a8SAxel Dörfler{
232b50494aaSAxel Dörfler	if (index > 0 && (addr_t)vecs[index - 1].iov_base
233b50494aaSAxel Dörfler			+ vecs[index - 1].iov_len == address) {
234f72376a8SAxel Dörfler		// the iovec can be combined with the previous one
235f72376a8SAxel Dörfler		vecs[index - 1].iov_len += size;
236f72376a8SAxel Dörfler		return;
237f72376a8SAxel Dörfler	}
238f72376a8SAxel Dörfler
239139353cfSAxel Dörfler	if (index == max)
240139353cfSAxel Dörfler		panic("no more space for iovecs!");
241139353cfSAxel Dörfler
242f72376a8SAxel Dörfler	// we need to start a new iovec
24361b6f38cSAxel Dörfler	vecs[index].iov_base = (void*)address;
244f72376a8SAxel Dörfler	vecs[index].iov_len = size;
245f72376a8SAxel Dörfler	index++;
246f72376a8SAxel Dörfler}
247f72376a8SAxel Dörfler
248f72376a8SAxel Dörfler
249c6573329SAxel Dörflerstatic inline bool
25061b6f38cSAxel Dörfleraccess_is_sequential(file_cache_ref* ref)
251c6573329SAxel Dörfler{
252c6573329SAxel Dörfler	return ref->last_access[ref->last_access_index] != 0;
253c6573329SAxel Dörfler}
254c6573329SAxel Dörfler
255c6573329SAxel Dörfler
256c6573329SAxel Dörflerstatic inline void
25761b6f38cSAxel Dörflerpush_access(file_cache_ref* ref, off_t offset, size_t bytes, bool isWrite)
258c6573329SAxel Dörfler{
259c6573329SAxel Dörfler	TRACE(("%p: push %Ld, %ld, %s\n", ref, offset, bytes,
260c6573329SAxel Dörfler		isWrite ? "write" : "read"));
261c6573329SAxel Dörfler
262c6573329SAxel Dörfler	int32 index = ref->last_access_index;
263c6573329SAxel Dörfler	int32 previous = index - 1;
264c6573329SAxel Dörfler	if (previous < 0)
265c6573329SAxel Dörfler		previous = LAST_ACCESSES - 1;
266c6573329SAxel Dörfler
2678a26f35aSAxel Dörfler	if (offset != ref->LastAccess(previous, isWrite))
268c6573329SAxel Dörfler		ref->last_access[previous] = 0;
269c6573329SAxel Dörfler
2708a26f35aSAxel Dörfler	ref->SetLastAccess(index, offset + bytes, isWrite);
271c6573329SAxel Dörfler
272c6573329SAxel Dörfler	if (++index >= LAST_ACCESSES)
273c6573329SAxel Dörfler		index = 0;
274c6573329SAxel Dörfler	ref->last_access_index = index;
275c6573329SAxel Dörfler}
276c6573329SAxel Dörfler
277c6573329SAxel Dörfler
278c6573329SAxel Dörflerstatic void
27961b6f38cSAxel Dörflerreserve_pages(file_cache_ref* ref, size_t reservePages, bool isWrite)
280c6573329SAxel Dörfler{
2815c99d639SIngo Weinhold	if (low_resource_state(B_KERNEL_RESOURCE_PAGES) != B_NO_LOW_RESOURCE) {
282a477e3cfSIngo Weinhold		VMCache* cache = ref->cache;
2835c99d639SIngo Weinhold		cache->Lock();
284c6573329SAxel Dörfler
285c6573329SAxel Dörfler		if (list_is_empty(&cache->consumers) && cache->areas == NULL
286c6573329SAxel Dörfler			&& access_is_sequential(ref)) {
287c6573329SAxel Dörfler			// we are not mapped, and we're accessed sequentially
288c6573329SAxel Dörfler
289c6573329SAxel Dörfler			if (isWrite) {
290c6573329SAxel Dörfler				// just schedule some pages to be written back
2918a26f35aSAxel Dörfler				int32 index = ref->last_access_index;
2928a26f35aSAxel Dörfler				int32 previous = index - 1;
2938a26f35aSAxel Dörfler				if (previous < 0)
2948a26f35aSAxel Dörfler					previous = LAST_ACCESSES - 1;
2958a26f35aSAxel Dörfler
2968a26f35aSAxel Dörfler				vm_page_schedule_write_page_range(cache,
2978a26f35aSAxel Dörfler					ref->LastAccessPageOffset(previous, true),
2988a26f35aSAxel Dörfler					ref->LastAccessPageOffset(index, true));
299c6573329SAxel Dörfler			} else {
300c6573329SAxel Dörfler				// free some pages from our cache
301e1b630c5SIngo Weinhold				// TODO: start with oldest
302c6573329SAxel Dörfler				uint32 left = reservePages;
30361b6f38cSAxel Dörfler				vm_page* page;
304e1b630c5SIngo Weinhold				for (VMCachePagesTree::Iterator it = cache->pages.GetIterator();
305e1b630c5SIngo Weinhold						(page = it.Next()) != NULL && left > 0;) {
306c6573329SAxel Dörfler					if (page->state != PAGE_STATE_MODIFIED
307c6573329SAxel Dörfler						&& page->state != PAGE_STATE_BUSY) {
3083cd20943SIngo Weinhold						DEBUG_PAGE_ACCESS_START(page);
3095c99d639SIngo Weinhold						cache->RemovePage(page);
310c6573329SAxel Dörfler						vm_page_set_state(page, PAGE_STATE_FREE);
311c6573329SAxel Dörfler						left--;
312c6573329SAxel Dörfler					}
313c6573329SAxel Dörfler				}
314c6573329SAxel Dörfler			}
315c6573329SAxel Dörfler		}
3165c99d639SIngo Weinhold		cache->Unlock();
317c6573329SAxel Dörfler	}
318c6573329SAxel Dörfler
319c6573329SAxel Dörfler	vm_page_reserve_pages(reservePages);
320c6573329SAxel Dörfler}
321c6573329SAxel Dörfler
322c6573329SAxel Dörfler
323c32499b4SAxel Dörflerstatic inline status_t
324c32499b4SAxel Dörflerread_pages_and_clear_partial(file_cache_ref* ref, void* cookie, off_t offset,
325c32499b4SAxel Dörfler	const iovec* vecs, size_t count, uint32 flags, size_t* _numBytes)
326c32499b4SAxel Dörfler{
327c32499b4SAxel Dörfler	size_t bytesUntouched = *_numBytes;
328c32499b4SAxel Dörfler
329c32499b4SAxel Dörfler	status_t status = vfs_read_pages(ref->vnode, cookie, offset, vecs, count,
330c32499b4SAxel Dörfler		flags, _numBytes);
331c32499b4SAxel Dörfler
332c32499b4SAxel Dörfler	size_t bytesEnd = *_numBytes;
333c32499b4SAxel Dörfler
334c32499b4SAxel Dörfler	if (offset + bytesEnd > ref->cache->virtual_end)
335c32499b4SAxel Dörfler		bytesEnd = ref->cache->virtual_end - offset;
336c32499b4SAxel Dörfler
337c32499b4SAxel Dörfler	if (status == B_OK && bytesEnd < bytesUntouched) {
338c32499b4SAxel Dörfler		// Clear out any leftovers that were not touched by the above read.
339c32499b4SAxel Dörfler		// We're doing this here so that not every file system/device has to
340c32499b4SAxel Dörfler		// implement this.
341c32499b4SAxel Dörfler		bytesUntouched -= bytesEnd;
342c32499b4SAxel Dörfler
343c32499b4SAxel Dörfler		for (int32 i = count; i-- > 0 && bytesUntouched != 0; ) {
344c32499b4SAxel Dörfler			size_t length = min_c(bytesUntouched, vecs[i].iov_len);
345c32499b4SAxel Dörfler			vm_memset_physical((addr_t)vecs[i].iov_base + vecs[i].iov_len
346c32499b4SAxel Dörfler				- length, 0, length);
347c32499b4SAxel Dörfler
348c32499b4SAxel Dörfler			bytesUntouched -= length;
349c32499b4SAxel Dörfler		}
350c32499b4SAxel Dörfler	}
351c32499b4SAxel Dörfler
352c32499b4SAxel Dörfler	return status;
353c32499b4SAxel Dörfler}
354c32499b4SAxel Dörfler
355c32499b4SAxel Dörfler
3560633dcc2SAxel Dörfler/*!	Reads the requested amount of data into the cache, and allocates
3570633dcc2SAxel Dörfler	pages needed to fulfill that request. This function is called by cache_io().
3580633dcc2SAxel Dörfler	It can only handle a certain amount of bytes, and the caller must make
359061816eeSAxel Dörfler	sure that it matches that criterion.
3601af7d115SMichael Lotz	The cache_ref lock must be held when calling this function; during
3610633dcc2SAxel Dörfler	operation it will unlock the cache, though.
362061816eeSAxel Dörfler*/
3630710d59cSAxel Dörflerstatic status_t
36461b6f38cSAxel Dörflerread_into_cache(file_cache_ref* ref, void* cookie, off_t offset,
365a121b8c8SAxel Dörfler	int32 pageOffset, addr_t buffer, size_t bufferSize, bool useBuffer,
3663d268edaSAxel Dörfler	size_t lastReservedPages, size_t reservePages)
367f72376a8SAxel Dörfler{
368cfe386c2SAxel Dörfler	TRACE(("read_into_cache(offset = %Ld, pageOffset = %ld, buffer = %#lx, "
369cfe386c2SAxel Dörfler		"bufferSize = %lu\n", offset, pageOffset, buffer, bufferSize));
370f72376a8SAxel Dörfler
371a477e3cfSIngo Weinhold	VMCache* cache = ref->cache;
3720f6c560eSAxel Dörfler
373279c6b76SIngo Weinhold	// TODO: We're using way too much stack! Rather allocate a sufficiently
374279c6b76SIngo Weinhold	// large chunk on the heap.
375f72376a8SAxel Dörfler	iovec vecs[MAX_IO_VECS];
376eb2bd0e8SStephan Aßmus	uint32 vecCount = 0;
377f72376a8SAxel Dörfler
378cfe386c2SAxel Dörfler	size_t numBytes = PAGE_ALIGN(pageOffset + bufferSize);
37961b6f38cSAxel Dörfler	vm_page* pages[MAX_IO_VECS];
380f72376a8SAxel Dörfler	int32 pageIndex = 0;
381f72376a8SAxel Dörfler
382f72376a8SAxel Dörfler	// allocate pages for the cache and mark them busy
38370a11cecSAxel Dörfler	for (size_t pos = 0; pos < numBytes; pos += B_PAGE_SIZE) {
38461b6f38cSAxel Dörfler		vm_page* page = pages[pageIndex++] = vm_page_allocate_page(
3851021fd28SIngo Weinhold			PAGE_STATE_FREE);
386139353cfSAxel Dörfler
3875c99d639SIngo Weinhold		cache->InsertPage(page, offset + pos);
388f72376a8SAxel Dörfler
3897f12cc54SIngo Weinhold		add_to_iovec(vecs, vecCount, MAX_IO_VECS,
3907f12cc54SIngo Weinhold			page->physical_page_number * B_PAGE_SIZE, B_PAGE_SIZE);
391061816eeSAxel Dörfler			// TODO: check if the array is large enough (currently panics)!
392f72376a8SAxel Dörfler	}
393f72376a8SAxel Dörfler
3943d268edaSAxel Dörfler	push_access(ref, offset, bufferSize, false);
3955c99d639SIngo Weinhold	cache->Unlock();
39620b232e9SAxel Dörfler	vm_page_unreserve_pages(lastReservedPages);
397a1d09631SAxel Dörfler
398f72376a8SAxel Dörfler	// read file into reserved pages
399c32499b4SAxel Dörfler	status_t status = read_pages_and_clear_partial(ref, cookie, offset, vecs,
4007f12cc54SIngo Weinhold		vecCount, B_PHYSICAL_IO_REQUEST, &numBytes);
401db8fb4fdSAxel Dörfler	if (status != B_OK) {
4022b028fcaSAxel Dörfler		// reading failed, free allocated pages
4032b028fcaSAxel Dörfler
4042b028fcaSAxel Dörfler		dprintf("file_cache: read pages failed: %s\n", strerror(status));
4052b028fcaSAxel Dörfler
4065c99d639SIngo Weinhold		cache->Lock();
407b2707997SStephan Aßmus
4082b028fcaSAxel Dörfler		for (int32 i = 0; i < pageIndex; i++) {
409522c2f19SIngo Weinhold			cache->NotifyPageEvents(pages[i], PAGE_EVENT_NOT_BUSY);
4105c99d639SIngo Weinhold			cache->RemovePage(pages[i]);
4112b028fcaSAxel Dörfler			vm_page_set_state(pages[i], PAGE_STATE_FREE);
4122b028fcaSAxel Dörfler		}
4132b028fcaSAxel Dörfler
414f72376a8SAxel Dörfler		return status;
415f72376a8SAxel Dörfler	}
416f72376a8SAxel Dörfler
417a121b8c8SAxel Dörfler	// copy the pages if needed and unmap them again
418f72376a8SAxel Dörfler
4197f12cc54SIngo Weinhold	for (int32 i = 0; i < pageIndex; i++) {
420a121b8c8SAxel Dörfler		if (useBuffer && bufferSize != 0) {
4217f12cc54SIngo Weinhold			size_t bytes = min_c(bufferSize, (size_t)B_PAGE_SIZE - pageOffset);
4227f12cc54SIngo Weinhold
42347c40a10SIngo Weinhold			vm_memcpy_from_physical((void*)buffer,
42447c40a10SIngo Weinhold				pages[i]->physical_page_number * B_PAGE_SIZE + pageOffset,
42547c40a10SIngo Weinhold				bytes, true);
42647c40a10SIngo Weinhold
427f72376a8SAxel Dörfler			buffer += bytes;
428f72376a8SAxel Dörfler			bufferSize -= bytes;
4290f6c560eSAxel Dörfler			pageOffset = 0;
430b50494aaSAxel Dörfler		}
431f72376a8SAxel Dörfler	}
432f72376a8SAxel Dörfler
433c6573329SAxel Dörfler	reserve_pages(ref, reservePages, false);
4345c99d639SIngo Weinhold	cache->Lock();
435a1d09631SAxel Dörfler
436f72376a8SAxel Dörfler	// make the pages accessible in the cache
437279c6b76SIngo Weinhold	for (int32 i = pageIndex; i-- > 0;) {
4383cd20943SIngo Weinhold		DEBUG_PAGE_ACCESS_END(pages[i]);
4393cd20943SIngo Weinhold
440f72376a8SAxel Dörfler		pages[i]->state = PAGE_STATE_ACTIVE;
4416d4aea47SAxel Dörfler
442522c2f19SIngo Weinhold		cache->NotifyPageEvents(pages[i], PAGE_EVENT_NOT_BUSY);
443279c6b76SIngo Weinhold	}
444f72376a8SAxel Dörfler
445f72376a8SAxel Dörfler	return B_OK;
446f72376a8SAxel Dörfler}
447f72376a8SAxel Dörfler
448f72376a8SAxel Dörfler
449cfe386c2SAxel Dörflerstatic status_t
45061b6f38cSAxel Dörflerread_from_file(file_cache_ref* ref, void* cookie, off_t offset,
451a121b8c8SAxel Dörfler	int32 pageOffset, addr_t buffer, size_t bufferSize, bool useBuffer,
4523d268edaSAxel Dörfler	size_t lastReservedPages, size_t reservePages)
453cfe386c2SAxel Dörfler{
4549ff70e74SAxel Dörfler	TRACE(("read_from_file(offset = %Ld, pageOffset = %ld, buffer = %#lx, "
4559ff70e74SAxel Dörfler		"bufferSize = %lu\n", offset, pageOffset, buffer, bufferSize));
4569ff70e74SAxel Dörfler
457a121b8c8SAxel Dörfler	if (!useBuffer)
458a121b8c8SAxel Dörfler		return B_OK;
459a121b8c8SAxel Dörfler
460cfe386c2SAxel Dörfler	iovec vec;
46161b6f38cSAxel Dörfler	vec.iov_base = (void*)buffer;
462cfe386c2SAxel Dörfler	vec.iov_len = bufferSize;
463cfe386c2SAxel Dörfler
4643d268edaSAxel Dörfler	push_access(ref, offset, bufferSize, false);
4655c99d639SIngo Weinhold	ref->cache->Unlock();
466cfe386c2SAxel Dörfler	vm_page_unreserve_pages(lastReservedPages);