cache.c 8.7 KB

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