cache.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  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. #include <linux/fs.h>
  11. #include <linux/slab.h>
  12. #include <linux/buffer_head.h>
  13. #include "fat.h"
  14. /* this must be > 0. */
  15. #define FAT_MAX_CACHE 8
  16. struct fat_cache {
  17. struct list_head cache_list;
  18. int nr_contig; /* number of contiguous clusters */
  19. int fcluster; /* cluster number in the file. */
  20. int dcluster; /* cluster number on disk. */
  21. };
  22. struct fat_cache_id {
  23. unsigned int id;
  24. int nr_contig;
  25. int fcluster;
  26. int dcluster;
  27. };
  28. static inline int fat_max_cache(struct inode *inode)
  29. {
  30. return FAT_MAX_CACHE;
  31. }
  32. static struct kmem_cache *fat_cache_cachep;
  33. static void init_once(void *foo)
  34. {
  35. struct fat_cache *cache = (struct fat_cache *)foo;
  36. INIT_LIST_HEAD(&cache->cache_list);
  37. }
  38. int __init fat_cache_init(void)
  39. {
  40. fat_cache_cachep = kmem_cache_create("fat_cache",
  41. sizeof(struct fat_cache),
  42. 0, SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD,
  43. init_once);
  44. if (fat_cache_cachep == NULL)
  45. return -ENOMEM;
  46. return 0;
  47. }
  48. void fat_cache_destroy(void)
  49. {
  50. kmem_cache_destroy(fat_cache_cachep);
  51. }
  52. static inline struct fat_cache *fat_cache_alloc(struct inode *inode)
  53. {
  54. return kmem_cache_alloc(fat_cache_cachep, GFP_NOFS);
  55. }
  56. static inline void fat_cache_free(struct fat_cache *cache)
  57. {
  58. BUG_ON(!list_empty(&cache->cache_list));
  59. kmem_cache_free(fat_cache_cachep, cache);
  60. }
  61. static inline void fat_cache_update_lru(struct inode *inode,
  62. struct fat_cache *cache)
  63. {
  64. if (MSDOS_I(inode)->cache_lru.next != &cache->cache_list)
  65. list_move(&cache->cache_list, &MSDOS_I(inode)->cache_lru);
  66. }
  67. static int fat_cache_lookup(struct inode *inode, int fclus,
  68. struct fat_cache_id *cid,
  69. int *cached_fclus, int *cached_dclus)
  70. {
  71. static struct fat_cache nohit = { .fcluster = 0, };
  72. struct fat_cache *hit = &nohit, *p;
  73. int offset = -1;
  74. spin_lock(&MSDOS_I(inode)->cache_lru_lock);
  75. list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) {
  76. /* Find the cache of "fclus" or nearest cache. */
  77. if (p->fcluster <= fclus && hit->fcluster < p->fcluster) {
  78. hit = p;
  79. if ((hit->fcluster + hit->nr_contig) < fclus) {
  80. offset = hit->nr_contig;
  81. } else {
  82. offset = fclus - hit->fcluster;
  83. break;
  84. }
  85. }
  86. }
  87. if (hit != &nohit) {
  88. fat_cache_update_lru(inode, hit);
  89. cid->id = MSDOS_I(inode)->cache_valid_id;
  90. cid->nr_contig = hit->nr_contig;
  91. cid->fcluster = hit->fcluster;
  92. cid->dcluster = hit->dcluster;
  93. *cached_fclus = cid->fcluster + offset;
  94. *cached_dclus = cid->dcluster + offset;
  95. }
  96. spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
  97. return offset;
  98. }
  99. static struct fat_cache *fat_cache_merge(struct inode *inode,
  100. struct fat_cache_id *new)
  101. {
  102. struct fat_cache *p;
  103. list_for_each_entry(p, &MSDOS_I(inode)->cache_lru, cache_list) {
  104. /* Find the same part as "new" in cluster-chain. */
  105. if (p->fcluster == new->fcluster) {
  106. BUG_ON(p->dcluster != new->dcluster);
  107. if (new->nr_contig > p->nr_contig)
  108. p->nr_contig = new->nr_contig;
  109. return p;
  110. }
  111. }
  112. return NULL;
  113. }
  114. static void fat_cache_add(struct inode *inode, struct fat_cache_id *new)
  115. {
  116. struct fat_cache *cache, *tmp;
  117. if (new->fcluster == -1) /* dummy cache */
  118. return;
  119. spin_lock(&MSDOS_I(inode)->cache_lru_lock);
  120. if (new->id != FAT_CACHE_VALID &&
  121. new->id != MSDOS_I(inode)->cache_valid_id)
  122. goto out; /* this cache was invalidated */
  123. cache = fat_cache_merge(inode, new);
  124. if (cache == NULL) {
  125. if (MSDOS_I(inode)->nr_caches < fat_max_cache(inode)) {
  126. MSDOS_I(inode)->nr_caches++;
  127. spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
  128. tmp = fat_cache_alloc(inode);
  129. if (!tmp) {
  130. spin_lock(&MSDOS_I(inode)->cache_lru_lock);
  131. MSDOS_I(inode)->nr_caches--;
  132. spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
  133. return;
  134. }
  135. spin_lock(&MSDOS_I(inode)->cache_lru_lock);
  136. cache = fat_cache_merge(inode, new);
  137. if (cache != NULL) {
  138. MSDOS_I(inode)->nr_caches--;
  139. fat_cache_free(tmp);
  140. goto out_update_lru;
  141. }
  142. cache = tmp;
  143. } else {
  144. struct list_head *p = MSDOS_I(inode)->cache_lru.prev;
  145. cache = list_entry(p, struct fat_cache, cache_list);
  146. }
  147. cache->fcluster = new->fcluster;
  148. cache->dcluster = new->dcluster;
  149. cache->nr_contig = new->nr_contig;
  150. }
  151. out_update_lru:
  152. fat_cache_update_lru(inode, cache);
  153. out:
  154. spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
  155. }
  156. /*
  157. * Cache invalidation occurs rarely, thus the LRU chain is not updated. It
  158. * fixes itself after a while.
  159. */
  160. static void __fat_cache_inval_inode(struct inode *inode)
  161. {
  162. struct msdos_inode_info *i = MSDOS_I(inode);
  163. struct fat_cache *cache;
  164. while (!list_empty(&i->cache_lru)) {
  165. cache = list_entry(i->cache_lru.next,
  166. struct fat_cache, cache_list);
  167. list_del_init(&cache->cache_list);
  168. i->nr_caches--;
  169. fat_cache_free(cache);
  170. }
  171. /* Update. The copy of caches before this id is discarded. */
  172. i->cache_valid_id++;
  173. if (i->cache_valid_id == FAT_CACHE_VALID)
  174. i->cache_valid_id++;
  175. }
  176. void fat_cache_inval_inode(struct inode *inode)
  177. {
  178. spin_lock(&MSDOS_I(inode)->cache_lru_lock);
  179. __fat_cache_inval_inode(inode);
  180. spin_unlock(&MSDOS_I(inode)->cache_lru_lock);
  181. }
  182. static inline int cache_contiguous(struct fat_cache_id *cid, int dclus)
  183. {
  184. cid->nr_contig++;
  185. return ((cid->dcluster + cid->nr_contig) == dclus);
  186. }
  187. static inline void cache_init(struct fat_cache_id *cid, int fclus, int dclus)
  188. {
  189. cid->id = FAT_CACHE_VALID;
  190. cid->fcluster = fclus;
  191. cid->dcluster = dclus;
  192. cid->nr_contig = 0;
  193. }
  194. int fat_get_cluster(struct inode *inode, int cluster, int *fclus, int *dclus)
  195. {
  196. struct super_block *sb = inode->i_sb;
  197. const int limit = sb->s_maxbytes >> MSDOS_SB(sb)->cluster_bits;
  198. struct fat_entry fatent;
  199. struct fat_cache_id cid;
  200. int nr;
  201. BUG_ON(MSDOS_I(inode)->i_start == 0);
  202. *fclus = 0;
  203. *dclus = MSDOS_I(inode)->i_start;
  204. if (cluster == 0)
  205. return 0;
  206. if (fat_cache_lookup(inode, cluster, &cid, fclus, dclus) < 0) {
  207. /*
  208. * dummy, always not contiguous
  209. * This is reinitialized by cache_init(), later.
  210. */
  211. cache_init(&cid, -1, -1);
  212. }
  213. fatent_init(&fatent);
  214. while (*fclus < cluster) {
  215. /* prevent the infinite loop of cluster chain */
  216. if (*fclus > limit) {
  217. fat_fs_error_ratelimit(sb,
  218. "%s: detected the cluster chain loop"
  219. " (i_pos %lld)", __func__,
  220. MSDOS_I(inode)->i_pos);
  221. nr = -EIO;
  222. goto out;
  223. }
  224. nr = fat_ent_read(inode, &fatent, *dclus);
  225. if (nr < 0)
  226. goto out;
  227. else if (nr == FAT_ENT_FREE) {
  228. fat_fs_error_ratelimit(sb,
  229. "%s: invalid cluster chain (i_pos %lld)",
  230. __func__,
  231. MSDOS_I(inode)->i_pos);
  232. nr = -EIO;
  233. goto out;
  234. } else if (nr == FAT_ENT_EOF) {
  235. fat_cache_add(inode, &cid);
  236. goto out;
  237. }
  238. (*fclus)++;
  239. *dclus = nr;
  240. if (!cache_contiguous(&cid, *dclus))
  241. cache_init(&cid, *fclus, *dclus);
  242. }
  243. nr = 0;
  244. fat_cache_add(inode, &cid);
  245. out:
  246. fatent_brelse(&fatent);
  247. return nr;
  248. }
  249. static int fat_bmap_cluster(struct inode *inode, int cluster)
  250. {
  251. struct super_block *sb = inode->i_sb;
  252. int ret, fclus, dclus;
  253. if (MSDOS_I(inode)->i_start == 0)
  254. return 0;
  255. ret = fat_get_cluster(inode, cluster, &fclus, &dclus);
  256. if (ret < 0)
  257. return ret;
  258. else if (ret == FAT_ENT_EOF) {
  259. fat_fs_error(sb, "%s: request beyond EOF (i_pos %lld)",
  260. __func__, MSDOS_I(inode)->i_pos);
  261. return -EIO;
  262. }
  263. return dclus;
  264. }
  265. int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,
  266. unsigned long *mapped_blocks, int create)
  267. {
  268. struct super_block *sb = inode->i_sb;
  269. struct msdos_sb_info *sbi = MSDOS_SB(sb);
  270. const unsigned long blocksize = sb->s_blocksize;
  271. const unsigned char blocksize_bits = sb->s_blocksize_bits;
  272. sector_t last_block;
  273. int cluster, offset;
  274. *phys = 0;
  275. *mapped_blocks = 0;
  276. if ((sbi->fat_bits != 32) && (inode->i_ino == MSDOS_ROOT_INO)) {
  277. if (sector < (sbi->dir_entries >> sbi->dir_per_block_bits)) {
  278. *phys = sector + sbi->dir_start;
  279. *mapped_blocks = 1;
  280. }
  281. return 0;
  282. }
  283. last_block = (i_size_read(inode) + (blocksize - 1)) >> blocksize_bits;
  284. if (sector >= last_block) {
  285. if (!create)
  286. return 0;
  287. /*
  288. * ->mmu_private can access on only allocation path.
  289. * (caller must hold ->i_mutex)
  290. */
  291. last_block = (MSDOS_I(inode)->mmu_private + (blocksize - 1))
  292. >> blocksize_bits;
  293. if (sector >= last_block)
  294. return 0;
  295. }
  296. cluster = sector >> (sbi->cluster_bits - sb->s_blocksize_bits);
  297. offset = sector & (sbi->sec_per_clus - 1);
  298. cluster = fat_bmap_cluster(inode, cluster);
  299. if (cluster < 0)
  300. return cluster;
  301. else if (cluster) {
  302. *phys = fat_clus_to_blknr(sbi, cluster) + offset;
  303. *mapped_blocks = sbi->sec_per_clus - offset;
  304. if (*mapped_blocks > last_block - sector)
  305. *mapped_blocks = last_block - sector;
  306. }
  307. return 0;
  308. }