cache.c 8.2 KB

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