1f72376a8SAxel Dörfler/*
26102f078SAxel Dörfler * Copyright 2004-2009, Axel D��rfler, axeld@pinc-software.de.
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
4011a3346cSAxel Dörfler
41cfe386c2SAxel Dörfler#define BYPASS_IO_SIZE		65536
42c6573329SAxel Dörfler#define LAST_ACCESSES		3
43c6573329SAxel Dörfler
44f72376a8SAxel Dörflerstruct file_cache_ref {
45a477e3cfSIngo Weinhold	VMCache			*cache;
4680f54692SAxel Dörfler	struct vnode	*vnode;
47c6573329SAxel Dörfler	off_t			last_access[LAST_ACCESSES];
48c6573329SAxel Dörfler		// TODO: it would probably be enough to only store the least
49c6573329SAxel Dörfler		//	significant 31 bits, and make this uint32 (one bit for
50c6573329SAxel Dörfler		//	write vs. read)
51c6573329SAxel Dörfler	int32			last_access_index;
527491000fSIngo Weinhold	uint16			disabled_count;
538a26f35aSAxel Dörfler
548a26f35aSAxel Dörfler	inline void SetLastAccess(int32 index, off_t access, bool isWrite)
558a26f35aSAxel Dörfler	{
568a26f35aSAxel Dörfler		// we remember writes as negative offsets
578a26f35aSAxel Dörfler		last_access[index] = isWrite ? -access : access;
588a26f35aSAxel Dörfler	}
598a26f35aSAxel Dörfler
60cee04e80SArtur Wyszynski	inline off_t LastAccess(int32 index, bool isWrite) const
618a26f35aSAxel Dörfler	{
628a26f35aSAxel Dörfler		return isWrite ? -last_access[index] : last_access[index];
638a26f35aSAxel Dörfler	}
648a26f35aSAxel Dörfler
658a26f35aSAxel Dörfler	inline uint32 LastAccessPageOffset(int32 index, bool isWrite)
668a26f35aSAxel Dörfler	{
678a26f35aSAxel Dörfler		return LastAccess(index, isWrite) >> PAGE_SHIFT;
688a26f35aSAxel Dörfler	}
69f72376a8SAxel Dörfler};
70f72376a8SAxel Dörfler
71eb2bd0e8SStephan Aßmusclass PrecacheIO : public AsyncIOCallback {
72eb2bd0e8SStephan Aßmuspublic:
73eb2bd0e8SStephan Aßmus								PrecacheIO(file_cache_ref* ref, off_t offset,
74435c43f5SIngo Weinhold									generic_size_t size);
75eb2bd0e8SStephan Aßmus								~PrecacheIO();
76eb2bd0e8SStephan Aßmus
7740bb9481SIngo Weinhold			status_t			Prepare(vm_page_reservation* reservation);
78a5dbd78bSAxel Dörfler			void				ReadAsync();
79eb2bd0e8SStephan Aßmus
80eb2bd0e8SStephan Aßmus	virtual	void				IOFinished(status_t status,
81eb2bd0e8SStephan Aßmus									bool partialTransfer,
82435c43f5SIngo Weinhold									generic_size_t bytesTransferred);
83eb2bd0e8SStephan Aßmus
84eb2bd0e8SStephan Aßmusprivate:
85eb2bd0e8SStephan Aßmus			file_cache_ref*		fRef;
86eb2bd0e8SStephan Aßmus			VMCache*			fCache;
87eb2bd0e8SStephan Aßmus			vm_page**			fPages;
88eb2bd0e8SStephan Aßmus			size_t				fPageCount;
89eb2bd0e8SStephan Aßmus			ConditionVariable*	fBusyConditions;
90435c43f5SIngo Weinhold			generic_io_vec*		fVecs;
91eb2bd0e8SStephan Aßmus			off_t				fOffset;
92a5dbd78bSAxel Dörfler			uint32				fVecCount;
93435c43f5SIngo Weinhold			generic_size_t		fSize;
943cd20943SIngo Weinhold#if DEBUG_PAGE_ACCESS
953cd20943SIngo Weinhold			thread_id			fAllocatingThread;
963cd20943SIngo Weinhold#endif
97eb2bd0e8SStephan Aßmus};
98eb2bd0e8SStephan Aßmus
9961b6f38cSAxel Dörflertypedef status_t (*cache_func)(file_cache_ref* ref, void* cookie, off_t offset,
100a121b8c8SAxel Dörfler	int32 pageOffset, addr_t buffer, size_t bufferSize, bool useBuffer,
10140bb9481SIngo Weinhold	vm_page_reservation* reservation, size_t reservePages);
10209149281SAxel Dörfler
103435c43f5SIngo Weinholdstatic void add_to_iovec(generic_io_vec* vecs, uint32 &index, uint32 max,
104435c43f5SIngo Weinhold	generic_addr_t address, generic_size_t size);
105eb2bd0e8SStephan Aßmus
106f72376a8SAxel Dörfler
10761b6f38cSAxel Dörflerstatic struct cache_module_info* sCacheModule;
10864a9bd15SIngo Weinhold
10964a9bd15SIngo Weinhold
11064a9bd15SIngo Weinholdstatic const uint32 kZeroVecCount = 32;
11164a9bd15SIngo Weinholdstatic const size_t kZeroVecSize = kZeroVecCount * B_PAGE_SIZE;
112435c43f5SIngo Weinholdstatic phys_addr_t sZeroPage;	// physical address
113435c43f5SIngo Weinholdstatic generic_io_vec sZeroVecs[kZeroVecCount];
114324fc66bSAxel Dörfler
115324fc66bSAxel Dörfler
11611a3346cSAxel Dörfler//	#pragma mark -
11711a3346cSAxel Dörfler
11811a3346cSAxel Dörfler
119435c43f5SIngo WeinholdPrecacheIO::PrecacheIO(file_cache_ref* ref, off_t offset, generic_size_t size)
120eb2bd0e8SStephan Aßmus	:
121eb2bd0e8SStephan Aßmus	fRef(ref),
122eb2bd0e8SStephan Aßmus	fCache(ref->cache),
123eb2bd0e8SStephan Aßmus	fPages(NULL),
124eb2bd0e8SStephan Aßmus	fVecs(NULL),
125eb2bd0e8SStephan Aßmus	fOffset(offset),
126a5dbd78bSAxel Dörfler	fVecCount(0),
127eb2bd0e8SStephan Aßmus	fSize(size)
128eb2bd0e8SStephan Aßmus{
129eb2bd0e8SStephan Aßmus	fPageCount = (size + B_PAGE_SIZE - 1) / B_PAGE_SIZE;
130eb2bd0e8SStephan Aßmus	fCache->AcquireRefLocked();
131eb2bd0e8SStephan Aßmus}
132eb2bd0e8SStephan Aßmus
133eb2bd0e8SStephan Aßmus
134eb2bd0e8SStephan AßmusPrecacheIO::~PrecacheIO()
135eb2bd0e8SStephan Aßmus{
136eb2bd0e8SStephan Aßmus	delete[] fPages;
137eb2bd0e8SStephan Aßmus	delete[] fVecs;
138eb2bd0e8SStephan Aßmus	fCache->ReleaseRefLocked();
139eb2bd0e8SStephan Aßmus}
140eb2bd0e8SStephan Aßmus
141eb2bd0e8SStephan Aßmus
142eb2bd0e8SStephan Aßmusstatus_t
14340bb9481SIngo WeinholdPrecacheIO::Prepare(vm_page_reservation* reservation)
144eb2bd0e8SStephan Aßmus{
145eb2bd0e8SStephan Aßmus	if (fPageCount == 0)
146eb2bd0e8SStephan Aßmus		return B_BAD_VALUE;
147eb2bd0e8SStephan Aßmus
148eb2bd0e8SStephan Aßmus	fPages = new(std::nothrow) vm_page*[fPageCount];
149eb2bd0e8SStephan Aßmus	if (fPages == NULL)
150eb2bd0e8SStephan Aßmus		return B_NO_MEMORY;
151eb2bd0e8SStephan Aßmus
152435c43f5SIngo Weinhold	fVecs = new(std::nothrow) generic_io_vec[fPageCount];
153eb2bd0e8SStephan Aßmus	if (fVecs == NULL)
154eb2bd0e8SStephan Aßmus		return B_NO_MEMORY;
155eb2bd0e8SStephan Aßmus
156eb2bd0e8SStephan Aßmus	// allocate pages for the cache and mark them busy
157eb2bd0e8SStephan Aßmus	uint32 i = 0;
158435c43f5SIngo Weinhold	for (generic_size_t pos = 0; pos < fSize; pos += B_PAGE_SIZE) {
15940bb9481SIngo Weinhold		vm_page* page = vm_page_allocate_page(reservation,
16040bb9481SIngo Weinhold			PAGE_STATE_CACHED | VM_PAGE_ALLOC_BUSY);
161eb2bd0e8SStephan Aßmus
162eb2bd0e8SStephan Aßmus		fCache->InsertPage(page, fOffset + pos);
163eb2bd0e8SStephan Aßmus
164a5dbd78bSAxel Dörfler		add_to_iovec(fVecs, fVecCount, fPageCount,
165eb2bd0e8SStephan Aßmus			page->physical_page_number * B_PAGE_SIZE, B_PAGE_SIZE);
166d5ad7629SAxel Dörfler		fPages[i++] = page;
167eb2bd0e8SStephan Aßmus	}
168eb2bd0e8SStephan Aßmus
1693cd20943SIngo Weinhold#if DEBUG_PAGE_ACCESS
1703cd20943SIngo Weinhold	fAllocatingThread = find_thread(NULL);
1713cd20943SIngo Weinhold#endif
172eb2bd0e8SStephan Aßmus
173a5dbd78bSAxel Dörfler	return B_OK;
174a5dbd78bSAxel Dörfler}
175aff0fbbeSStephan Aßmus
176aff0fbbeSStephan Aßmus
177a5dbd78bSAxel Dörflervoid
178a5dbd78bSAxel DörflerPrecacheIO::ReadAsync()
179a5dbd78bSAxel Dörfler{
180a5dbd78bSAxel Dörfler	// This object is going to be deleted after the I/O request has been
181a5dbd78bSAxel Dörfler	// fulfilled
182a5dbd78bSAxel Dörfler	vfs_asynchronous_read_pages(fRef->vnode, NULL, fOffset, fVecs, fVecCount,
183a5dbd78bSAxel Dörfler		fSize, B_PHYSICAL_IO_REQUEST, this);
184eb2bd0e8SStephan Aßmus}
185eb2bd0e8SStephan Aßmus
186eb2bd0e8SStephan Aßmus
187eb2bd0e8SStephan Aßmusvoid
188eb2bd0e8SStephan AßmusPrecacheIO::IOFinished(status_t status, bool partialTransfer,
189435c43f5SIngo Weinhold	generic_size_t bytesTransferred)
190eb2bd0e8SStephan Aßmus{
191eb2bd0e8SStephan Aßmus	AutoLocker<VMCache> locker(fCache);
192eb2bd0e8SStephan Aßmus
193eb2bd0e8SStephan Aßmus	// Make successfully loaded pages accessible again (partially
194eb2bd0e8SStephan Aßmus	// transferred pages are considered failed)
195435c43f5SIngo Weinhold	phys_size_t pagesTransferred
196c32499b4SAxel Dörfler		= (bytesTransferred + B_PAGE_SIZE - 1) / B_PAGE_SIZE;
197c32499b4SAxel Dörfler
1981d578e15SIngo Weinhold	if (fOffset + (off_t)bytesTransferred > fCache->virtual_end)
199c32499b4SAxel Dörfler		bytesTransferred = fCache->virtual_end - fOffset;
200c32499b4SAxel Dörfler
201eb2bd0e8SStephan Aßmus	for (uint32 i = 0; i < pagesTransferred; i++) {
202c32499b4SAxel Dörfler		if (i == pagesTransferred - 1
203c32499b4SAxel Dörfler			&& (bytesTransferred % B_PAGE_SIZE) != 0) {
204c32499b4SAxel Dörfler			// clear partial page
205c32499b4SAxel Dörfler			size_t bytesTouched = bytesTransferred % B_PAGE_SIZE;
206435c43f5SIngo Weinhold			vm_memset_physical(
207435c43f5SIngo Weinhold				((phys_addr_t)fPages[i]->physical_page_number << PAGE_SHIFT)
208435c43f5SIngo Weinhold					+ bytesTouched,
209435c43f5SIngo Weinhold				0, B_PAGE_SIZE - bytesTouched);
210c32499b4SAxel Dörfler		}
211c32499b4SAxel Dörfler
2123cd20943SIngo Weinhold		DEBUG_PAGE_ACCESS_TRANSFER(fPages[i], fAllocatingThread);
2133cd20943SIngo Weinhold
21472382fa6SIngo Weinhold		fCache->MarkPageUnbusy(fPages[i]);
2153cd20943SIngo Weinhold
2163cd20943SIngo Weinhold		DEBUG_PAGE_ACCESS_END(fPages[i]);
217eb2bd0e8SStephan Aßmus	}
218eb2bd0e8SStephan Aßmus
219eb2bd0e8SStephan Aßmus	// Free pages after failed I/O
220eb2bd0e8SStephan Aßmus	for (uint32 i = pagesTransferred; i < fPageCount; i++) {
2213cd20943SIngo Weinhold		DEBUG_PAGE_ACCESS_TRANSFER(fPages[i], fAllocatingThread);
222522c2f19SIngo Weinhold		fCache->NotifyPageEvents(fPages[i], PAGE_EVENT_NOT_BUSY);
223eb2bd0e8SStephan Aßmus		fCache->RemovePage(fPages[i]);
224eb2bd0e8SStephan Aßmus		vm_page_set_state(fPages[i], PAGE_STATE_FREE);
225eb2bd0e8SStephan Aßmus	}
226eb2bd0e8SStephan Aßmus
227eb2bd0e8SStephan Aßmus	delete this;
228eb2bd0e8SStephan Aßmus}
229eb2bd0e8SStephan Aßmus
230eb2bd0e8SStephan Aßmus
231eb2bd0e8SStephan Aßmus//	#pragma mark -
232eb2bd0e8SStephan Aßmus
233eb2bd0e8SStephan Aßmus
234f72376a8SAxel Dörflerstatic void
235435c43f5SIngo Weinholdadd_to_iovec(generic_io_vec* vecs, uint32 &index, uint32 max,
236435c43f5SIngo Weinhold	generic_addr_t address, generic_size_t size)
237f72376a8SAxel Dörfler{
238435c43f5SIngo Weinhold	if (index > 0 && vecs[index - 1].base + vecs[index - 1].length == address) {
239f72376a8SAxel Dörfler		// the iovec can be combined with the previous one
240435c43f5SIngo Weinhold		vecs[index - 1].length += size;
241f72376a8SAxel Dörfler		return;
242f72376a8SAxel Dörfler	}
243f72376a8SAxel Dörfler
244139353cfSAxel Dörfler	if (index == max)
245139353cfSAxel Dörfler		panic("no more space for iovecs!");
246139353cfSAxel Dörfler
247f72376a8SAxel Dörfler	// we need to start a new iovec
248435c43f5SIngo Weinhold	vecs[index].base = address;
249435c43f5SIngo Weinhold	vecs[index].length = size;
250f72376a8SAxel Dörfler	index++;
251f72376a8SAxel Dörfler}
252f72376a8SAxel Dörfler
253f72376a8SAxel Dörfler
254c6573329SAxel Dörflerstatic inline bool
25561b6f38cSAxel Dörfleraccess_is_sequential(file_cache_ref* ref)
256c6573329SAxel Dörfler{
257c6573329SAxel Dörfler	return ref->last_access[ref->last_access_index] != 0;
258c6573329SAxel Dörfler}
259c6573329SAxel Dörfler
260c6573329SAxel Dörfler
261c6573329SAxel Dörflerstatic inline void
262435c43f5SIngo Weinholdpush_access(file_cache_ref* ref, off_t offset, generic_size_t bytes,
263435c43f5SIngo Weinhold	bool isWrite)
264c6573329SAxel Dörfler{
265c6573329SAxel Dörfler	TRACE(("%p: push %Ld, %ld, %s\n", ref, offset, bytes,
266c6573329SAxel Dörfler		isWrite ? "write" : "read"));
267c6573329SAxel Dörfler
268c6573329SAxel Dörfler	int32 index = ref->last_access_index;
269c6573329SAxel Dörfler	int32 previous = index - 1;
270c6573329SAxel Dörfler	if (previous < 0)
271c6573329SAxel Dörfler		previous = LAST_ACCESSES - 1;
272c6573329SAxel Dörfler
2738a26f35aSAxel Dörfler	if (offset != ref->LastAccess(previous, isWrite))
274c6573329SAxel Dörfler		ref->last_access[previous] = 0;
275c6573329SAxel Dörfler
2768a26f35aSAxel Dörfler	ref->SetLastAccess(index, offset + bytes, isWrite);
277c6573329SAxel Dörfler
278c6573329SAxel Dörfler	if (++index >= LAST_ACCESSES)
279c6573329SAxel Dörfler		index = 0;
280c6573329SAxel Dörfler	ref->last_access_index = index;
281c6573329SAxel Dörfler}
282c6573329SAxel Dörfler
283c6573329SAxel Dörfler
284c6573329SAxel Dörflerstatic void
28540bb9481SIngo Weinholdreserve_pages(file_cache_ref* ref, vm_page_reservation* reservation,
28640bb9481SIngo Weinhold	size_t reservePages, bool isWrite)
287c6573329SAxel Dörfler{
2885c99d639SIngo Weinhold	if (low_resource_state(B_KERNEL_RESOURCE_PAGES) != B_NO_LOW_RESOURCE) {
289a477e3cfSIngo Weinhold		VMCache* cache = ref->cache;
2905c99d639SIngo Weinhold		cache->Lock();
291c6573329SAxel Dörfler
292f8154d17SIngo Weinhold		if (cache->consumers.IsEmpty() && cache->areas == NULL
293c6573329SAxel Dörfler			&& access_is_sequential(ref)) {
294c6573329SAxel Dörfler			// we are not mapped, and we're accessed sequentially
295c6573329SAxel Dörfler
296c6573329SAxel Dörfler			if (isWrite) {
2974fe399e4SAxel Dörfler				// Just write some pages back, and actually wait until they
2984fe399e4SAxel Dörfler				// have been written back in order to relieve the page pressure
2994fe399e4SAxel Dörfler				// a bit.
3008a26f35aSAxel Dörfler				int32 index = ref->last_access_index;
3018a26f35aSAxel Dörfler				int32 previous = index - 1;
3028a26f35aSAxel Dörfler				if (previous < 0)
3038a26f35aSAxel Dörfler					previous = LAST_ACCESSES - 1;
3048a26f35aSAxel Dörfler
3054fe399e4SAxel Dörfler				vm_page_write_modified_page_range(cache,
3068a26f35aSAxel Dörfler					ref->LastAccessPageOffset(previous, true),
3078a26f35aSAxel Dörfler					ref->LastAccessPageOffset(index, true));
308c6573329SAxel Dörfler			} else {
309c6573329SAxel Dörfler				// free some pages from our cache
310e1b630c5SIngo Weinhold				// TODO: start with oldest
311c6573329SAxel Dörfler				uint32 left = reservePages;
31261b6f38cSAxel Dörfler				vm_page* page;
313e1b630c5SIngo Weinhold				for (VMCachePagesTree::Iterator it = cache->pages.GetIterator();
314e1b630c5SIngo Weinhold						(page = it.Next()) != NULL && left > 0;) {
315bd7645a1SIngo Weinhold					if (page->State() == PAGE_STATE_CACHED && !page->busy) {
3163cd20943SIngo Weinhold						DEBUG_PAGE_ACCESS_START(page);
317211c63dfSIngo Weinhold						ASSERT(!page->IsMapped());
318211c63dfSIngo Weinhold						ASSERT(!page->modified);
3195c99d639SIngo Weinhold						cache->RemovePage(page);
320c6573329SAxel Dörfler						vm_page_set_state(page, PAGE_STATE_FREE);
321c6573329SAxel Dörfler						left--;
322c6573329SAxel Dörfler					}
323c6573329SAxel Dörfler				}
324c6573329SAxel Dörfler			}
325c6573329SAxel Dörfler		}
3265c99d639SIngo Weinhold		cache->Unlock();
327c6573329SAxel Dörfler	}
328c6573329SAxel Dörfler
32940bb9481SIngo Weinhold	vm_page_reserve_pages(reservation, reservePages, VM_PRIORITY_USER);
330c6573329SAxel Dörfler}
331c6573329SAxel Dörfler
332c6573329SAxel Dörfler
333c32499b4SAxel Dörflerstatic inline status_t
334c32499b4SAxel Dörflerread_pages_and_clear_partial(file_cache_ref* ref, void* cookie, off_t offset,
335435c43f5SIngo Weinhold	const generic_io_vec* vecs, size_t count, uint32 flags,
336435c43f5SIngo Weinhold	generic_size_t* _numBytes)
337c32499b4SAxel Dörfler{
338435c43f5SIngo Weinhold	generic_size_t bytesUntouched = *_numBytes;
339c32499b4SAxel Dörfler
340c32499b4SAxel Dörfler	status_t status = vfs_read_pages(ref->vnode, cookie, offset, vecs, count,
341c32499b4SAxel Dörfler		flags, _numBytes);
342c32499b4SAxel Dörfler
343435c43f5SIngo Weinhold	generic_size_t bytesEnd = *_numBytes;
344c32499b4SAxel Dörfler
3451d578e15SIngo Weinhold	if (offset + (off_t)bytesEnd > ref->cache->virtual_end)
346c32499b4SAxel Dörfler		bytesEnd = ref->cache->virtual_end - offset;
347c32499b4SAxel Dörfler
348c32499b4SAxel Dörfler	if (status == B_OK && bytesEnd < bytesUntouched) {
349c32499b4SAxel Dörfler		// Clear out any leftovers that were not touched by the above read.
350c32499b4SAxel Dörfler		// We're doing this here so that not every file system/device has to
351c32499b4SAxel Dörfler		// implement this.
352c32499b4SAxel Dörfler		bytesUntouched -= bytesEnd;
353c32499b4SAxel Dörfler
354c32499b4SAxel Dörfler		for (int32 i = count; i-- > 0 && bytesUntouched != 0; ) {
355435c43f5SIngo Weinhold			generic_size_t length = min_c(bytesUntouched, vecs[i].length);
356435c43f5SIngo Weinhold			vm_memset_physical(vecs[i].base + vecs[i].length - length, 0,
357435c43f5SIngo Weinhold				length);
358c32499b4SAxel Dörfler
359c32499b4SAxel Dörfler			bytesUntouched -= length;
360c32499b4SAxel Dörfler		}
361c32499b4SAxel Dörfler	}
362c32499b4SAxel Dörfler
363c32499b4SAxel Dörfler	return status;
364c32499b4SAxel Dörfler}
365c32499b4SAxel Dörfler
366c32499b4SAxel Dörfler
3670633dcc2SAxel Dörfler/*!	Reads the requested amount of data into the cache, and allocates
3680633dcc2SAxel Dörfler	pages needed to fulfill that request. This function is called by cache_io().
3690633dcc2SAxel Dörfler	It can only handle a certain amount of bytes, and the caller must make
370061816eeSAxel Dörfler	sure that it matches that criterion.
3711af7d115SMichael Lotz	The cache_ref lock must be held when calling this function; during
3720633dcc2SAxel Dörfler	operation it will unlock the cache, though.
373061816eeSAxel Dörfler*/
3740710d59cSAxel Dörflerstatic status_t
37561b6f38cSAxel Dörflerread_into_cache(file_cache_ref* ref, void* cookie, off_t offset,
376a121b8c8SAxel Dörfler	int32 pageOffset, addr_t buffer, size_t bufferSize, bool useBuffer,
37740bb9481SIngo Weinhold	vm_page_reservation* reservation, size_t reservePages)
378f72376a8SAxel Dörfler{
379cfe386c2SAxel Dörfler	TRACE(("read_into_cache(offset = %Ld, pageOffset = %ld, buffer = %#lx, "
380cfe386c2SAxel Dörfler		"bufferSize = %lu\n", offset, pageOffset, buffer, bufferSize));
381f72376a8SAxel Dörfler
382a477e3cfSIngo Weinhold	VMCache* cache = ref->cache;
3830f6c560eSAxel Dörfler
384279c6b76SIngo Weinhold	// TODO: We're using way too much stack! Rather allocate a sufficiently
385279c6b76SIngo Weinhold	// large chunk on the heap.
386435c43f5SIngo Weinhold	generic_io_vec vecs[MAX_IO_VECS];
387eb2bd0e8SStephan Aßmus	uint32 vecCount = 0;
388f72376a8SAxel Dörfler
3891d578e15SIngo Weinhold	generic_size_t numBytes = PAGE_ALIGN(pageOffset + bufferSize);
39061b6f38cSAxel Dörfler	vm_page* pages[MAX_IO_VECS];
391f72376a8SAxel Dörfler	int32 pageIndex = 0;
392f72376a8SAxel Dörfler
393f72376a8SAxel Dörfler	// allocate pages for the cache and mark them busy
3941d578e15SIngo Weinhold	for (generic_size_t pos = 0; pos < numBytes; pos += B_PAGE_SIZE) {
39561b6f38cSAxel Dörfler		vm_page* page = pages[pageIndex++] = vm_page_allocate_page(
39640bb9481SIngo Weinhold			reservation, PAGE_STATE_CACHED | VM_PAGE_ALLOC_BUSY);
397139353cfSAxel Dörfler
3985c99d639SIngo Weinhold		cache->InsertPage(page, offset + pos);
399f72376a8SAxel Dörfler
4007f12cc54SIngo Weinhold		add_to_iovec(vecs, vecCount, MAX_IO_VECS,
4017f12cc54SIngo Weinhold			page->physical_page_number * B_PAGE_SIZE, B_PAGE_SIZE);
402061816eeSAxel Dörfler			// TODO: check if the array is large enough (currently panics)!
403f72376a8SAxel Dörfler	}
404f72376a8SAxel Dörfler
4053d268edaSAxel Dörfler	push_access(ref, offset, bufferSize, false);
4065c99d639SIngo Weinhold	cache->Unlock();
40740bb9481SIngo Weinhold	vm_page_unreserve_pages(reservation);
408a1d09631SAxel Dörfler
409f72376a8SAxel Dörfler	// read file into reserved pages
410c32499b4SAxel Dörfler	status_t status = read_pages_and_clear_partial(ref, cookie, offset, vecs,
4117f12cc54SIngo Weinhold		vecCount, B_PHYSICAL_IO_REQUEST, &numBytes);
412db8fb4fdSAxel Dörfler	if (status != B_OK) {
4132b028fcaSAxel Dörfler		// reading failed, free allocated pages
4142b028fcaSAxel Dörfler
4152b028fcaSAxel Dörfler		dprintf("file_cache: read pages failed: %s\n", strerror(status));
4162b028fcaSAxel Dörfler
4175c99d639SIngo Weinhold		cache->Lock();
418b2707997SStephan Aßmus
4192b028fcaSAxel Dörfler		for (int32 i = 0; i < pageIndex; i++) {
420522c2f19SIngo Weinhold			cache->NotifyPageEvents(pages[i], PAGE_EVENT_NOT_BUSY);
4215c99d639SIngo Weinhold			cache->RemovePage(pages[i]);
4222b028fcaSAxel Dörfler			vm_page_set_state(pages[i], PAGE_STATE_FREE);
4232b028fcaSAxel Dörfler		}
4242b028fcaSAxel Dörfler
425f72376a8SAxel Dörfler		return status;
426f72376a8SAxel Dörfler	}
427f72376a8SAxel Dörfler
428a121b8c8SAxel Dörfler	// copy the pages if needed and unmap them again
429f72376a8SAxel Dörfler
4307f12cc54SIngo Weinhold	for (int32 i = 0; i < pageIndex; i++) {
431a121b8c8SAxel Dörfler		if (useBuffer && bufferSize != 0) {
4327f12cc54SIngo Weinhold			size_t bytes = min_c(bufferSize, (size_t)B_PAGE_SIZE - pageOffset);
4337f12cc54SIngo Weinhold
43447c40a10SIngo Weinhold			vm_memcpy_from_physical((void*)buffer,
43547c40a10SIngo Weinhold				pages[i]->physical_page_number * B_PAGE_SIZE + pageOffset,
4369c5c5990SPaweł Dziepak				bytes, IS_USER_ADDRESS(buffer));
43747c40a10SIngo Weinhold
438f72376a8SAxel Dörfler			buffer += bytes;
439f72376a8SAxel Dörfler			bufferSize -= bytes;
4400f6c560eSAxel Dörfler			pageOffset = 0;
441b50494aaSAxel Dörfler		}
442f72376a8SAxel Dörfler	}
443f72376a8SAxel Dörfler
44440bb9481SIngo Weinhold	reserve_pages(ref, reservation, reservePages, false);
4455c99d639SIngo Weinhold	cache->Lock();
446a1d09631SAxel Dörfler
447f72376a8SAxel Dörfler	// make the pages accessible in the cache
448279c6b76SIngo Weinhold	for (int32 i = pageIndex; i-- > 0;) {
4493cd20943SIngo Weinhold		DEBUG_PAGE_ACCESS_END(pages[i]);