selftests: cgroup: add memory controller self-tests
[linux/fpc-iii.git] / fs / fat / cache.c
blobe9bed49df6b71047b6658063e377fb59ef4c339e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/fat/cache.c
5 * Written 1992,1993 by Werner Almesberger
7 * Mar 1999. AV. Changed cache, so that it uses the starting cluster instead
8 * of inode number.
9 * May 1999. AV. Fixed the bogosity with FAT32 (read "FAT28"). Fscking lusers.
12 #include <linux/slab.h>
13 #include "fat.h"
15 /* this must be > 0. */
16 #define FAT_MAX_CACHE 8
18 struct fat_cache {
19 struct list_head cache_list;
20 int nr_contig; /* number of contiguous clusters */
21 int fcluster; /* cluster number in the file. */
22 int dcluster; /* cluster number on disk. */
25 struct fat_cache_id {
26 unsigned int id;
27 int nr_contig;
28 int fcluster;
29 int dcluster;
32 static inline int fat_max_cache(struct inode *inode)
34 return FAT_MAX_CACHE;
37 static struct kmem_cache *fat_cache_cachep;
39 static void init_once(void *foo)
41 struct fat_cache *cache = (struct fat_cache *)foo;
43 INIT_LIST_HEAD(&cache->cache_list);
46 int __init fat_cache_init(void)
48 fat_cache_cachep = kmem_cache_create("fat_cache",
49 sizeof(struct fat_cache),
50 0, SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
51 init_once);
52 if (fat_cache_cachep == NULL)
53 return -ENOMEM;
54 return 0;
57 void fat_cache_destroy(void)
59 kmem_cache_destroy(fat_cache_cachep);
62 static inline struct fat_cache *fat_cache_alloc(struct inode *inode)
64 return kmem_cache_alloc(fat_cache_cachep, GFP_NOFS);
67 static inline void fat_cache_free(struct fat_cache *cache)
69 BUG_ON(!list_empty(&cache->cache_list));
70 kmem_cache_free(fat_cache_cachep, cache);
73 static inline void fat_cache_update_lru(struct inode *inode,
74 struct fat_cache *cache)
76 if (MSDOS_I(inode)->cache_lru.next != &cache->cache_list)
77 list_move(&cache->cache_list, &MSDOS_I(inode)->cache_lru);
80 static int fat_cache_lookup(struct inode *inode, int fclus,
81 struct fat_cache_id *cid,
82 int *cached_fclus, int *cached_dclus)
84 static struct fat_cache nohit = { .fcluster = 0, };
86 struct fat_cache *hit = &nohit, *p;
87 int offset = -1;
89 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
90 list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) {
91 /* Find the cache of "fclus" or nearest cache. */
92 if (p->fcluster <= fclus && hit->fcluster < p->fcluster) {
93 hit = p;
94 if ((hit->fcluster + hit->nr_contig) < fclus) {
95 offset = hit->nr_contig;
96 } else {
97 offset = fclus - hit->fcluster;
98 break;
102 if (hit != &nohit) {
103 fat_cache_update_lru(inode, hit);
105 cid->id = MSDOS_I(inode)->cache_valid_id;
106 cid->nr_contig = hit->nr_contig;
107 cid->fcluster = hit->fcluster;
108 cid->dcluster = hit->dcluster;
109 *cached_fclus = cid->fcluster + offset;
110 *cached_dclus = cid->dcluster + offset;
112 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
114 return offset;
117 static struct fat_cache *fat_cache_merge(struct inode *inode,
118 struct fat_cache_id *new)
120 struct fat_cache *p;
122 list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) {
123 /* Find the same part as "new" in cluster-chain. */
124 if (p->fcluster == new->fcluster) {
125 BUG_ON(p->dcluster != new->dcluster);
126 if (new->nr_contig > p->nr_contig)
127 p->nr_contig = new->nr_contig;
128 return p;
131 return NULL;
134 static void fat_cache_add(struct inode *inode, struct fat_cache_id *new)
136 struct fat_cache *cache, *tmp;
138 if (new->fcluster == -1) /* dummy cache */
139 return;
141 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
142 if (new->id != FAT_CACHE_VALID &&
143 new->id != MSDOS_I(inode)->cache_valid_id)
144 goto out; /* this cache was invalidated */
146 cache = fat_cache_merge(inode, new);
147 if (cache == NULL) {
148 if (MSDOS_I(inode)->nr_caches < fat_max_cache(inode)) {
149 MSDOS_I(inode)->nr_caches++;
150 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
152 tmp = fat_cache_alloc(inode);
153 if (!tmp) {
154 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
155 MSDOS_I(inode)->nr_caches--;
156 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
157 return;
160 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
161 cache = fat_cache_merge(inode, new);
162 if (cache != NULL) {
163 MSDOS_I(inode)->nr_caches--;
164 fat_cache_free(tmp);
165 goto out_update_lru;
167 cache = tmp;
168 } else {
169 struct list_head *p = MSDOS_I(inode)->cache_lru.prev;
170 cache = list_entry(p, struct fat_cache, cache_list);
172 cache->fcluster = new->fcluster;
173 cache->dcluster = new->dcluster;
174 cache->nr_contig = new->nr_contig;
176 out_update_lru:
177 fat_cache_update_lru(inode, cache);
178 out:
179 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
183 * Cache invalidation occurs rarely, thus the LRU chain is not updated. It
184 * fixes itself after a while.
186 static void __fat_cache_inval_inode(struct inode *inode)
188 struct msdos_inode_info *i = MSDOS_I(inode);
189 struct fat_cache *cache;
191 while (!list_empty(&i->cache_lru)) {
192 cache = list_entry(i->cache_lru.next,
193 struct fat_cache, cache_list);
194 list_del_init(&cache->cache_list);
195 i->nr_caches--;
196 fat_cache_free(cache);
198 /* Update. The copy of caches before this id is discarded. */
199 i->cache_valid_id++;
200 if (i->cache_valid_id == FAT_CACHE_VALID)
201 i->cache_valid_id++;
204 void fat_cache_inval_inode(struct inode *inode)
206 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
207 __fat_cache_inval_inode(inode);
208 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
211 static inline int cache_contiguous(struct fat_cache_id *cid, int dclus)
213 cid->nr_contig++;
214 return ((cid->dcluster + cid->nr_contig) == dclus);
217 static inline void cache_init(struct fat_cache_id *cid, int fclus, int dclus)
219 cid->id = FAT_CACHE_VALID;
220 cid->fcluster = fclus;
221 cid->dcluster = dclus;
222 cid->nr_contig = 0;
225 int fat_get_cluster(struct inode *inode, int cluster, int *fclus, int *dclus)
227 struct super_block *sb = inode->i_sb;
228 const int limit = sb->s_maxbytes >> MSDOS_SB(sb)->cluster_bits;
229 struct fat_entry fatent;
230 struct fat_cache_id cid;
231 int nr;
233 BUG_ON(MSDOS_I(inode)->i_start == 0);
235 *fclus = 0;
236 *dclus = MSDOS_I(inode)->i_start;
237 if (cluster == 0)
238 return 0;
240 if (fat_cache_lookup(inode, cluster, &cid, fclus, dclus) < 0) {
242 * dummy, always not contiguous
243 * This is reinitialized by cache_init(), later.
245 cache_init(&cid, -1, -1);
248 fatent_init(&fatent);
249 while (*fclus < cluster) {
250 /* prevent the infinite loop of cluster chain */
251 if (*fclus > limit) {
252 fat_fs_error_ratelimit(sb,
253 "%s: detected the cluster chain loop"
254 " (i_pos %lld)", __func__,
255 MSDOS_I(inode)->i_pos);
256 nr = -EIO;
257 goto out;
260 nr = fat_ent_read(inode, &fatent, *dclus);
261 if (nr < 0)
262 goto out;
263 else if (nr == FAT_ENT_FREE) {
264 fat_fs_error_ratelimit(sb,
265 "%s: invalid cluster chain (i_pos %lld)",
266 __func__,
267 MSDOS_I(inode)->i_pos);
268 nr = -EIO;
269 goto out;
270 } else if (nr == FAT_ENT_EOF) {
271 fat_cache_add(inode, &cid);
272 goto out;
274 (*fclus)++;
275 *dclus = nr;
276 if (!cache_contiguous(&cid, *dclus))
277 cache_init(&cid, *fclus, *dclus);
279 nr = 0;
280 fat_cache_add(inode, &cid);
281 out:
282 fatent_brelse(&fatent);
283 return nr;
286 static int fat_bmap_cluster(struct inode *inode, int cluster)
288 struct super_block *sb = inode->i_sb;
289 int ret, fclus, dclus;
291 if (MSDOS_I(inode)->i_start == 0)
292 return 0;
294 ret = fat_get_cluster(inode, cluster, &fclus, &dclus);
295 if (ret < 0)
296 return ret;
297 else if (ret == FAT_ENT_EOF) {
298 fat_fs_error(sb, "%s: request beyond EOF (i_pos %lld)",
299 __func__, MSDOS_I(inode)->i_pos);
300 return -EIO;
302 return dclus;
305 int fat_get_mapped_cluster(struct inode *inode, sector_t sector,
306 sector_t last_block,
307 unsigned long *mapped_blocks, sector_t *bmap)
309 struct super_block *sb = inode->i_sb;
310 struct msdos_sb_info *sbi = MSDOS_SB(sb);
311 int cluster, offset;
313 cluster = sector >> (sbi->cluster_bits - sb->s_blocksize_bits);
314 offset = sector & (sbi->sec_per_clus - 1);
315 cluster = fat_bmap_cluster(inode, cluster);
316 if (cluster < 0)
317 return cluster;
318 else if (cluster) {
319 *bmap = fat_clus_to_blknr(sbi, cluster) + offset;
320 *mapped_blocks = sbi->sec_per_clus - offset;
321 if (*mapped_blocks > last_block - sector)
322 *mapped_blocks = last_block - sector;
325 return 0;
328 static int is_exceed_eof(struct inode *inode, sector_t sector,
329 sector_t *last_block, int create)
331 struct super_block *sb = inode->i_sb;
332 const unsigned long blocksize = sb->s_blocksize;
333 const unsigned char blocksize_bits = sb->s_blocksize_bits;
335 *last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
336 if (sector >= *last_block) {
337 if (!create)
338 return 1;
341 * ->mmu_private can access on only allocation path.
342 * (caller must hold ->i_mutex)
344 *last_block = (MSDOS_I(inode)->mmu_private + (blocksize - 1))
345 >> blocksize_bits;
346 if (sector >= *last_block)
347 return 1;
350 return 0;
353 int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
354 unsigned long *mapped_blocks, int create, bool from_bmap)
356 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
357 sector_t last_block;
359 *phys = 0;
360 *mapped_blocks = 0;
361 if ((sbi->fat_bits != 32) && (inode->i_ino == MSDOS_ROOT_INO)) {
362 if (sector < (sbi->dir_entries >> sbi->dir_per_block_bits)) {
363 *phys = sector + sbi->dir_start;
364 *mapped_blocks = 1;
366 return 0;
369 if (!from_bmap) {
370 if (is_exceed_eof(inode, sector, &last_block, create))
371 return 0;
372 } else {
373 last_block = inode->i_blocks >>
374 (inode->i_sb->s_blocksize_bits - 9);
375 if (sector >= last_block)
376 return 0;
379 return fat_get_mapped_cluster(inode, sector, last_block, mapped_blocks,
380 phys);