Lines Matching full:cache

21  * cache.c
28 * This file implements a generic cache implementation used for both caches,
29 * plus functions layered ontop of the generic cache implementation to
32 * To avoid out of memory and fragmentation isssues with vmalloc the cache
35 * It should be noted that the cache is not used for file datablocks, these
36 * are decompressed and cached in the page-cache in the normal way. The
37 * cache is only used to temporarily cache fragment and metadata blocks
63 * Look-up block in cache, and increment usage count. If not in cache, read
67 struct squashfs_cache *cache, u64 block, int length) in squashfs_cache_get() argument
72 spin_lock(&cache->lock); in squashfs_cache_get()
75 for (i = 0; i < cache->entries; i++) in squashfs_cache_get()
76 if (cache->entry[i].block == block) in squashfs_cache_get()
79 if (i == cache->entries) { in squashfs_cache_get()
81 * Block not in cache, if all cache entries are used in squashfs_cache_get()
84 if (cache->unused == 0) { in squashfs_cache_get()
85 cache->num_waiters++; in squashfs_cache_get()
86 spin_unlock(&cache->lock); in squashfs_cache_get()
87 wait_event(cache->wait_queue, cache->unused); in squashfs_cache_get()
88 spin_lock(&cache->lock); in squashfs_cache_get()
89 cache->num_waiters--; in squashfs_cache_get()
94 * At least one unused cache entry. A simple in squashfs_cache_get()
96 * be evicted from the cache. in squashfs_cache_get()
98 i = cache->next_blk; in squashfs_cache_get()
99 for (n = 0; n < cache->entries; n++) { in squashfs_cache_get()
100 if (cache->entry[i].refcount == 0) in squashfs_cache_get()
102 i = (i + 1) % cache->entries; in squashfs_cache_get()
105 cache->next_blk = (i + 1) % cache->entries; in squashfs_cache_get()
106 entry = &cache->entry[i]; in squashfs_cache_get()
109 * Initialise choosen cache entry, and fill it in from in squashfs_cache_get()
112 cache->unused--; in squashfs_cache_get()
118 spin_unlock(&cache->lock); in squashfs_cache_get()
122 cache->block_size); in squashfs_cache_get()
124 spin_lock(&cache->lock); in squashfs_cache_get()
133 * have looked it up in the cache, and have slept in squashfs_cache_get()
137 spin_unlock(&cache->lock); in squashfs_cache_get()
140 spin_unlock(&cache->lock); in squashfs_cache_get()
146 * Block already in cache. Increment refcount so it doesn't in squashfs_cache_get()
148 * previously unused there's one less cache entry available in squashfs_cache_get()
151 entry = &cache->entry[i]; in squashfs_cache_get()
153 cache->unused--; in squashfs_cache_get()
162 spin_unlock(&cache->lock); in squashfs_cache_get()
165 spin_unlock(&cache->lock); in squashfs_cache_get()
172 cache->name, i, entry->block, entry->refcount, entry->error); in squashfs_cache_get()
175 ERROR("Unable to read %s cache entry [%llx]\n", cache->name, in squashfs_cache_get()
182 * Release cache entry, once usage count is zero it can be reused.
186 struct squashfs_cache *cache = entry->cache; in squashfs_cache_put() local
188 spin_lock(&cache->lock); in squashfs_cache_put()
191 cache->unused++; in squashfs_cache_put()
196 if (cache->num_waiters) { in squashfs_cache_put()
197 spin_unlock(&cache->lock); in squashfs_cache_put()
198 wake_up(&cache->wait_queue); in squashfs_cache_put()
202 spin_unlock(&cache->lock); in squashfs_cache_put()
206 * Delete cache reclaiming all kmalloced buffers.
208 void squashfs_cache_delete(struct squashfs_cache *cache) in squashfs_cache_delete() argument
212 if (cache == NULL) in squashfs_cache_delete()
215 for (i = 0; i < cache->entries; i++) { in squashfs_cache_delete()
216 if (cache->entry[i].data) { in squashfs_cache_delete()
217 for (j = 0; j < cache->pages; j++) in squashfs_cache_delete()
218 kfree(cache->entry[i].data[j]); in squashfs_cache_delete()
219 kfree(cache->entry[i].data); in squashfs_cache_delete()
223 kfree(cache->entry); in squashfs_cache_delete()
224 kfree(cache); in squashfs_cache_delete()
229 * Initialise cache allocating the specified number of entries, each of
237 struct squashfs_cache *cache = kzalloc(sizeof(*cache), GFP_KERNEL); in squashfs_cache_init() local
239 if (cache == NULL) { in squashfs_cache_init()
240 ERROR("Failed to allocate %s cache\n", name); in squashfs_cache_init()
244 cache->entry = kcalloc(entries, sizeof(*(cache->entry)), GFP_KERNEL); in squashfs_cache_init()
245 if (cache->entry == NULL) { in squashfs_cache_init()
246 ERROR("Failed to allocate %s cache\n", name); in squashfs_cache_init()
250 cache->next_blk = 0; in squashfs_cache_init()
251 cache->unused = entries; in squashfs_cache_init()
252 cache->entries = entries; in squashfs_cache_init()
253 cache->block_size = block_size; in squashfs_cache_init()
254 cache->pages = block_size >> PAGE_CACHE_SHIFT; in squashfs_cache_init()
255 cache->name = name; in squashfs_cache_init()
256 cache->num_waiters = 0; in squashfs_cache_init()
257 spin_lock_init(&cache->lock); in squashfs_cache_init()
258 init_waitqueue_head(&cache->wait_queue); in squashfs_cache_init()
261 struct squashfs_cache_entry *entry = &cache->entry[i]; in squashfs_cache_init()
263 init_waitqueue_head(&cache->entry[i].wait_queue); in squashfs_cache_init()
264 entry->cache = cache; in squashfs_cache_init()
266 entry->data = kcalloc(cache->pages, sizeof(void *), GFP_KERNEL); in squashfs_cache_init()
268 ERROR("Failed to allocate %s cache entry\n", name); in squashfs_cache_init()
272 for (j = 0; j < cache->pages; j++) { in squashfs_cache_init()
281 return cache; in squashfs_cache_init()
284 squashfs_cache_delete(cache); in squashfs_cache_init()
290 * Copy upto length bytes from cache entry to buffer starting at offset bytes
291 * into the cache entry. If there's not length bytes then copy the number of
367 * Look-up in the fragmment cache the fragment located at <start_block> in the
382 * filesystem. The cache is used here to avoid duplicating locking and