blob: 1cc7038e273de02bb404c91623bec8f65cd85288 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/fat/cache.c
3 *
4 * Written 1992,1993 by Werner Almesberger
5 *
6 * Mar 1999. AV. Changed cache, so that it uses the starting cluster instead
7 * of inode number.
8 * May 1999. AV. Fixed the bogosity with FAT32 (read "FAT28"). Fscking lusers.
9 */
10
11#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/buffer_head.h>
OGAWA Hirofumi9e975da2008-11-06 12:53:46 -080014#include "fat.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/* this must be > 0. */
17#define FAT_MAX_CACHE 8
18
19struct fat_cache {
20 struct list_head cache_list;
21 int nr_contig; /* number of contiguous clusters */
22 int fcluster; /* cluster number in the file. */
23 int dcluster; /* cluster number on disk. */
24};
25
26struct fat_cache_id {
27 unsigned int id;
28 int nr_contig;
29 int fcluster;
30 int dcluster;
31};
32
33static inline int fat_max_cache(struct inode *inode)
34{
35 return FAT_MAX_CACHE;
36}
37
Christoph Lametere18b8902006-12-06 20:33:20 -080038static struct kmem_cache *fat_cache_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070040static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 struct fat_cache *cache = (struct fat_cache *)foo;
43
Christoph Lametera35afb82007-05-16 22:10:57 -070044 INIT_LIST_HEAD(&cache->cache_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47int __init fat_cache_init(void)
48{
49 fat_cache_cachep = kmem_cache_create("fat_cache",
50 sizeof(struct fat_cache),
Paul Jackson4b6a9312006-03-24 03:16:05 -080051 0, SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
Paul Mundt20c2df82007-07-20 10:11:58 +090052 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (fat_cache_cachep == NULL)
54 return -ENOMEM;
55 return 0;
56}
57
Andrew Mortonef6689e2005-06-30 22:13:14 -070058void fat_cache_destroy(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -070060 kmem_cache_destroy(fat_cache_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63static inline struct fat_cache *fat_cache_alloc(struct inode *inode)
64{
Linus Torvalds8f593422008-05-19 19:53:01 -070065 return kmem_cache_alloc(fat_cache_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68static inline void fat_cache_free(struct fat_cache *cache)
69{
70 BUG_ON(!list_empty(&cache->cache_list));
71 kmem_cache_free(fat_cache_cachep, cache);
72}
73
74static inline void fat_cache_update_lru(struct inode *inode,
75 struct fat_cache *cache)
76{
77 if (MSDOS_I(inode)->cache_lru.next != &cache->cache_list)
78 list_move(&cache->cache_list, &MSDOS_I(inode)->cache_lru);
79}
80
81static int fat_cache_lookup(struct inode *inode, int fclus,
82 struct fat_cache_id *cid,
83 int *cached_fclus, int *cached_dclus)
84{
85 static struct fat_cache nohit = { .fcluster = 0, };
86
87 struct fat_cache *hit = &nohit, *p;
88 int offset = -1;
89
90 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
91 list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) {
92 /* Find the cache of "fclus" or nearest cache. */
93 if (p->fcluster <= fclus && hit->fcluster < p->fcluster) {
94 hit = p;
95 if ((hit->fcluster + hit->nr_contig) < fclus) {
96 offset = hit->nr_contig;
97 } else {
98 offset = fclus - hit->fcluster;
99 break;
100 }
101 }
102 }
103 if (hit != &nohit) {
104 fat_cache_update_lru(inode, hit);
105
106 cid->id = MSDOS_I(inode)->cache_valid_id;
107 cid->nr_contig = hit->nr_contig;
108 cid->fcluster = hit->fcluster;
109 cid->dcluster = hit->dcluster;
110 *cached_fclus = cid->fcluster + offset;
111 *cached_dclus = cid->dcluster + offset;
112 }
113 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
114
115 return offset;
116}
117
118static struct fat_cache *fat_cache_merge(struct inode *inode,
119 struct fat_cache_id *new)
120{
121 struct fat_cache *p;
122
123 list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) {
124 /* Find the same part as "new" in cluster-chain. */
125 if (p->fcluster == new->fcluster) {
126 BUG_ON(p->dcluster != new->dcluster);
127 if (new->nr_contig > p->nr_contig)
128 p->nr_contig = new->nr_contig;
129 return p;
130 }
131 }
132 return NULL;
133}
134
135static void fat_cache_add(struct inode *inode, struct fat_cache_id *new)
136{
137 struct fat_cache *cache, *tmp;
138
139 if (new->fcluster == -1) /* dummy cache */
140 return;
141
142 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
143 if (new->id != FAT_CACHE_VALID &&
144 new->id != MSDOS_I(inode)->cache_valid_id)
145 goto out; /* this cache was invalidated */
146
147 cache = fat_cache_merge(inode, new);
148 if (cache == NULL) {
149 if (MSDOS_I(inode)->nr_caches < fat_max_cache(inode)) {
150 MSDOS_I(inode)->nr_caches++;
151 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
152
153 tmp = fat_cache_alloc(inode);
OGAWA Hirofumi70030922011-04-12 21:08:38 +0900154 if (!tmp) {
155 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
156 MSDOS_I(inode)->nr_caches--;
157 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
158 return;
159 }
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
162 cache = fat_cache_merge(inode, new);
163 if (cache != NULL) {
164 MSDOS_I(inode)->nr_caches--;
165 fat_cache_free(tmp);
166 goto out_update_lru;
167 }
168 cache = tmp;
169 } else {
170 struct list_head *p = MSDOS_I(inode)->cache_lru.prev;
171 cache = list_entry(p, struct fat_cache, cache_list);
172 }
173 cache->fcluster = new->fcluster;
174 cache->dcluster = new->dcluster;
175 cache->nr_contig = new->nr_contig;
176 }
177out_update_lru:
178 fat_cache_update_lru(inode, cache);
179out:
180 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
181}
182
183/*
184 * Cache invalidation occurs rarely, thus the LRU chain is not updated. It
185 * fixes itself after a while.
186 */
187static void __fat_cache_inval_inode(struct inode *inode)
188{
189 struct msdos_inode_info *i = MSDOS_I(inode);
190 struct fat_cache *cache;
191
192 while (!list_empty(&i->cache_lru)) {
193 cache = list_entry(i->cache_lru.next, struct fat_cache, cache_list);
194 list_del_init(&cache->cache_list);
195 i->nr_caches--;
196 fat_cache_free(cache);
197 }
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++;
202}
203
204void fat_cache_inval_inode(struct inode *inode)
205{
206 spin_lock(&MSDOS_I(inode)->cache_lru_lock);
207 __fat_cache_inval_inode(inode);
208 spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
209}
210
211static inline int cache_contiguous(struct fat_cache_id *cid, int dclus)
212{
213 cid->nr_contig++;
214 return ((cid->dcluster + cid->nr_contig) == dclus);
215}
216
217static inline void cache_init(struct fat_cache_id *cid, int fclus, int dclus)
218{
219 cid->id = FAT_CACHE_VALID;
220 cid->fcluster = fclus;
221 cid->dcluster = dclus;
222 cid->nr_contig = 0;
223}
224
225int fat_get_cluster(struct inode *inode, int cluster, int *fclus, int *dclus)
226{
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;
232
233 BUG_ON(MSDOS_I(inode)->i_start == 0);
234
235 *fclus = 0;
236 *dclus = MSDOS_I(inode)->i_start;
237 if (cluster == 0)
238 return 0;
239
240 if (fat_cache_lookup(inode, cluster, &cid, fclus, dclus) < 0) {
241 /*
242 * dummy, always not contiguous
243 * This is reinitialized by cache_init(), later.
244 */
245 cache_init(&cid, -1, -1);
246 }
247
248 fatent_init(&fatent);
249 while (*fclus < cluster) {
250 /* prevent the infinite loop of cluster chain */
251 if (*fclus > limit) {
OGAWA Hirofumiaaa04b42010-05-24 14:33:12 -0700252 fat_fs_error_ratelimit(sb,
253 "%s: detected the cluster chain loop"
254 " (i_pos %lld)", __func__,
255 MSDOS_I(inode)->i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 nr = -EIO;
257 goto out;
258 }
259
260 nr = fat_ent_read(inode, &fatent, *dclus);
261 if (nr < 0)
262 goto out;
263 else if (nr == FAT_ENT_FREE) {
OGAWA Hirofumiaaa04b42010-05-24 14:33:12 -0700264 fat_fs_error_ratelimit(sb, "%s: invalid cluster chain"
265 " (i_pos %lld)", __func__,
266 MSDOS_I(inode)->i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 nr = -EIO;
268 goto out;
269 } else if (nr == FAT_ENT_EOF) {
270 fat_cache_add(inode, &cid);
271 goto out;
272 }
273 (*fclus)++;
274 *dclus = nr;
275 if (!cache_contiguous(&cid, *dclus))
276 cache_init(&cid, *fclus, *dclus);
277 }
278 nr = 0;
279 fat_cache_add(inode, &cid);
280out:
281 fatent_brelse(&fatent);
282 return nr;
283}
284
285static int fat_bmap_cluster(struct inode *inode, int cluster)
286{
287 struct super_block *sb = inode->i_sb;
288 int ret, fclus, dclus;
289
290 if (MSDOS_I(inode)->i_start == 0)
291 return 0;
292
293 ret = fat_get_cluster(inode, cluster, &fclus, &dclus);
294 if (ret < 0)
295 return ret;
296 else if (ret == FAT_ENT_EOF) {
Denis Karpov85c78592009-06-04 02:34:22 +0900297 fat_fs_error(sb, "%s: request beyond EOF (i_pos %lld)",
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700298 __func__, MSDOS_I(inode)->i_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return -EIO;
300 }
301 return dclus;
302}
303
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800304int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
OGAWA Hirofumi2bdf67e2008-11-06 12:53:57 -0800305 unsigned long *mapped_blocks, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct super_block *sb = inode->i_sb;
308 struct msdos_sb_info *sbi = MSDOS_SB(sb);
OGAWA Hirofumi2bdf67e2008-11-06 12:53:57 -0800309 const unsigned long blocksize = sb->s_blocksize;
310 const unsigned char blocksize_bits = sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 sector_t last_block;
312 int cluster, offset;
313
314 *phys = 0;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800315 *mapped_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if ((sbi->fat_bits != 32) && (inode->i_ino == MSDOS_ROOT_INO)) {
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800317 if (sector < (sbi->dir_entries >> sbi->dir_per_block_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 *phys = sector + sbi->dir_start;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800319 *mapped_blocks = 1;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
322 }
OGAWA Hirofumi2bdf67e2008-11-06 12:53:57 -0800323
324 last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
325 if (sector >= last_block) {
326 if (!create)
327 return 0;
328
329 /*
330 * ->mmu_private can access on only allocation path.
331 * (caller must hold ->i_mutex)
332 */
333 last_block = (MSDOS_I(inode)->mmu_private + (blocksize - 1))
334 >> blocksize_bits;
335 if (sector >= last_block)
336 return 0;
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 cluster = sector >> (sbi->cluster_bits - sb->s_blocksize_bits);
340 offset = sector & (sbi->sec_per_clus - 1);
341 cluster = fat_bmap_cluster(inode, cluster);
342 if (cluster < 0)
343 return cluster;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800344 else if (cluster) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 *phys = fat_clus_to_blknr(sbi, cluster) + offset;
OGAWA Hirofumie5174ba2006-01-08 01:02:11 -0800346 *mapped_blocks = sbi->sec_per_clus - offset;
347 if (*mapped_blocks > last_block - sector)
348 *mapped_blocks = last_block - sector;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
351}