2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include "yaffs_cache.h"
15 /*------------------------ Short Operations Cache ------------------------------
16 * In many situations where there is no high level buffering a lot of
17 * reads might be short sequential reads, and a lot of writes may be short
18 * sequential writes. eg. scanning/writing a jpeg file.
19 * In these cases, a short read/write cache can provide a huge perfomance
20 * benefit with dumb-as-a-rock code.
21 * In Linux, the page cache provides read buffering and the short op cache
22 * provides write buffering.
24 * There are a small number (~10) of cache chunks per device so that we don't
25 * need a very intelligent search.
28 int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
30 struct yaffs_dev *dev = obj->my_dev;
32 struct yaffs_cache *cache;
33 int n_caches = obj->my_dev->param.n_caches;
35 for (i = 0; i < n_caches; i++) {
36 cache = &dev->cache[i];
37 if (cache->object == obj && cache->dirty)
44 void yaffs_flush_single_cache(struct yaffs_cache *cache, int discard)
47 if (!cache || cache->locked)
50 /* Write it out and free it up if need be.*/
52 yaffs_wr_data_obj(cache->object,
65 void yaffs_flush_file_cache(struct yaffs_obj *obj, int discard)
67 struct yaffs_dev *dev = obj->my_dev;
69 struct yaffs_cache *cache;
70 int n_caches = obj->my_dev->param.n_caches;
76 /* Find the chunks for this object and flush them. */
77 for (i = 0; i < n_caches; i++) {
78 cache = &dev->cache[i];
79 if (cache->object == obj)
80 yaffs_flush_single_cache(cache, discard);
86 void yaffs_flush_whole_cache(struct yaffs_dev *dev, int discard)
88 struct yaffs_obj *obj;
89 int n_caches = dev->param.n_caches;
92 /* Find a dirty object in the cache and flush it...
93 * until there are no further dirty objects.
97 for (i = 0; i < n_caches && !obj; i++) {
98 if (dev->cache[i].object && dev->cache[i].dirty)
99 obj = dev->cache[i].object;
102 yaffs_flush_file_cache(obj, discard);
107 /* Grab us an unused cache chunk for use.
108 * First look for an empty one.
109 * Then look for the least recently used non-dirty one.
110 * Then look for the least recently used dirty one...., flush and look again.
112 static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
116 if (dev->param.n_caches > 0) {
117 for (i = 0; i < dev->param.n_caches; i++) {
118 if (!dev->cache[i].object)
119 return &dev->cache[i];
126 struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
128 struct yaffs_cache *cache;
132 if (dev->param.n_caches < 1)
135 /* First look for an unused cache */
137 cache = yaffs_grab_chunk_worker(dev);
143 * Thery were all in use.
144 * Find the LRU cache and flush it if it is dirty.
150 for (i = 0; i < dev->param.n_caches; i++) {
151 if (dev->cache[i].object &&
152 !dev->cache[i].locked &&
153 (dev->cache[i].last_use < usage || !cache)) {
154 usage = dev->cache[i].last_use;
155 cache = &dev->cache[i];
160 yaffs_flush_single_cache(cache, 1);
162 yaffs_flush_file_cache(cache->object, 1);
163 cache = yaffs_grab_chunk_worker(dev);
169 /* Find a cached chunk */
170 struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
173 struct yaffs_dev *dev = obj->my_dev;
176 if (dev->param.n_caches < 1)
179 for (i = 0; i < dev->param.n_caches; i++) {
180 if (dev->cache[i].object == obj &&
181 dev->cache[i].chunk_id == chunk_id) {
184 return &dev->cache[i];
190 /* Mark the chunk for the least recently used algorithym */
191 void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
196 if (dev->param.n_caches < 1)
199 if (dev->cache_last_use < 0 ||
200 dev->cache_last_use > 100000000) {
201 /* Reset the cache usages */
202 for (i = 1; i < dev->param.n_caches; i++)
203 dev->cache[i].last_use = 0;
205 dev->cache_last_use = 0;
207 dev->cache_last_use++;
208 cache->last_use = dev->cache_last_use;
214 /* Invalidate a single cache page.
215 * Do this when a whole page gets written,
216 * ie the short cache for this page is no longer valid.
218 void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
220 struct yaffs_cache *cache;
222 if (object->my_dev->param.n_caches > 0) {
223 cache = yaffs_find_chunk_cache(object, chunk_id);
226 cache->object = NULL;
230 /* Invalidate all the cache pages associated with this object
231 * Do this whenever ther file is deleted or resized.
233 void yaffs_invalidate_file_cache(struct yaffs_obj *in)
236 struct yaffs_dev *dev = in->my_dev;
238 if (dev->param.n_caches > 0) {
240 for (i = 0; i < dev->param.n_caches; i++) {
241 if (dev->cache[i].object == in)
242 dev->cache[i].object = NULL;
248 int yaffs_cache_init(struct yaffs_dev *dev)
252 if (dev->param.n_caches > 0) {
256 dev->param.n_caches * sizeof(struct yaffs_cache);
258 if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
259 dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
261 dev->cache = kmalloc(cache_bytes, GFP_NOFS);
263 buf = (u8 *) dev->cache;
266 memset(dev->cache, 0, cache_bytes);
268 for (i = 0; i < dev->param.n_caches && buf; i++) {
269 dev->cache[i].object = NULL;
270 dev->cache[i].last_use = 0;
271 dev->cache[i].dirty = 0;
272 dev->cache[i].data = buf =
273 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
278 dev->cache_last_use = 0;
281 return init_failed ? -1 : 0;
284 void yaffs_cache_deinit(struct yaffs_dev *dev)
287 if (dev->param.n_caches > 0 && dev->cache) {
290 for (i = 0; i < dev->param.n_caches; i++) {
291 kfree(dev->cache[i].data);
292 dev->cache[i].data = NULL;