cache.c 8.3 KB

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