cache.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/fs/fat/cache.c
  4. *
  5. * Written 1992,1993 by Werner Almesberger
  6. *
  7. * Mar 1999. AV. Changed cache, so that it uses the starting cluster instead
  8. * of inode number.
  9. * May 1999. AV. Fixed the bogosity with FAT32 (read "FAT28"). Fscking lusers.
  10. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/unaligned.h>
  14. #include <linux/buffer_head.h>
  15. #include "exfat_raw.h"
  16. #include "exfat_fs.h"
  17. #define EXFAT_MAX_CACHE 16
  18. struct exfat_cache {
  19. struct list_head cache_list;
  20. unsigned int nr_contig; /* number of contiguous clusters */
  21. unsigned int fcluster; /* cluster number in the file. */
  22. unsigned int dcluster; /* cluster number on disk. */
  23. };
  24. struct exfat_cache_id {
  25. unsigned int id;
  26. unsigned int nr_contig;
  27. unsigned int fcluster;
  28. unsigned int dcluster;
  29. };
  30. static struct kmem_cache *exfat_cachep;
  31. static void exfat_cache_init_once(void *c)
  32. {
  33. struct exfat_cache *cache = (struct exfat_cache *)c;
  34. INIT_LIST_HEAD(&cache->cache_list);
  35. }
  36. int exfat_cache_init(void)
  37. {
  38. exfat_cachep = kmem_cache_create("exfat_cache",
  39. sizeof(struct exfat_cache),
  40. 0, SLAB_RECLAIM_ACCOUNT,
  41. exfat_cache_init_once);
  42. if (!exfat_cachep)
  43. return -ENOMEM;
  44. return 0;
  45. }
  46. void exfat_cache_shutdown(void)
  47. {
  48. if (!exfat_cachep)
  49. return;
  50. kmem_cache_destroy(exfat_cachep);
  51. }
  52. static inline struct exfat_cache *exfat_cache_alloc(void)
  53. {
  54. return kmem_cache_alloc(exfat_cachep, GFP_NOFS);
  55. }
  56. static inline void exfat_cache_free(struct exfat_cache *cache)
  57. {
  58. WARN_ON(!list_empty(&cache->cache_list));
  59. kmem_cache_free(exfat_cachep, cache);
  60. }
  61. static inline void exfat_cache_update_lru(struct inode *inode,
  62. struct exfat_cache *cache)
  63. {
  64. struct exfat_inode_info *ei = EXFAT_I(inode);
  65. if (ei->cache_lru.next != &cache->cache_list)
  66. list_move(&cache->cache_list, &ei->cache_lru);
  67. }
  68. /*
  69. * Find the cache that covers or precedes 'fclus' and return the last
  70. * cluster before the next cache range.
  71. */
  72. static inline unsigned int
  73. exfat_cache_lookup(struct inode *inode, struct exfat_cache_id *cid,
  74. unsigned int fclus, unsigned int end,
  75. unsigned int *cached_fclus, unsigned int *cached_dclus)
  76. {
  77. struct exfat_inode_info *ei = EXFAT_I(inode);
  78. static struct exfat_cache nohit = { .fcluster = 0, };
  79. struct exfat_cache *hit = &nohit, *p;
  80. unsigned int tail = 0; /* End boundary of hit cache */
  81. /*
  82. * Search range [fclus, end]. Stop early if:
  83. * 1. Cache covers entire range, or
  84. * 2. Next cache starts at current cache tail
  85. */
  86. spin_lock(&ei->cache_lru_lock);
  87. list_for_each_entry(p, &ei->cache_lru, cache_list) {
  88. /* Find the cache of "fclus" or nearest cache. */
  89. if (p->fcluster <= fclus) {
  90. if (p->fcluster < hit->fcluster)
  91. continue;
  92. hit = p;
  93. tail = hit->fcluster + hit->nr_contig;
  94. /* Current cache covers [fclus, end] completely */
  95. if (tail >= end)
  96. break;
  97. } else if (p->fcluster <= end) {
  98. end = p->fcluster - 1;
  99. /*
  100. * If we have a hit and next cache starts within/at
  101. * its tail, caches are contiguous, stop searching.
  102. */
  103. if (tail && tail >= end)
  104. break;
  105. }
  106. }
  107. if (hit != &nohit) {
  108. unsigned int offset;
  109. exfat_cache_update_lru(inode, hit);
  110. cid->id = ei->cache_valid_id;
  111. cid->nr_contig = hit->nr_contig;
  112. cid->fcluster = hit->fcluster;
  113. cid->dcluster = hit->dcluster;
  114. offset = min(cid->nr_contig, fclus - cid->fcluster);
  115. *cached_fclus = cid->fcluster + offset;
  116. *cached_dclus = cid->dcluster + offset;
  117. }
  118. spin_unlock(&ei->cache_lru_lock);
  119. /* Return next cache start or 'end' if no more caches */
  120. return end;
  121. }
  122. static struct exfat_cache *exfat_cache_merge(struct inode *inode,
  123. struct exfat_cache_id *new)
  124. {
  125. struct exfat_inode_info *ei = EXFAT_I(inode);
  126. struct exfat_cache *p;
  127. list_for_each_entry(p, &ei->cache_lru, cache_list) {
  128. /* Find the same part as "new" in cluster-chain. */
  129. if (p->fcluster == new->fcluster) {
  130. if (new->nr_contig > p->nr_contig)
  131. p->nr_contig = new->nr_contig;
  132. return p;
  133. }
  134. }
  135. return NULL;
  136. }
  137. static void exfat_cache_add(struct inode *inode,
  138. struct exfat_cache_id *new)
  139. {
  140. struct exfat_inode_info *ei = EXFAT_I(inode);
  141. struct exfat_cache *cache, *tmp;
  142. if (new->fcluster == EXFAT_EOF_CLUSTER) /* dummy cache */
  143. return;
  144. spin_lock(&ei->cache_lru_lock);
  145. if (new->id != EXFAT_CACHE_VALID &&
  146. new->id != ei->cache_valid_id)
  147. goto unlock; /* this cache was invalidated */
  148. cache = exfat_cache_merge(inode, new);
  149. if (cache == NULL) {
  150. if (ei->nr_caches < EXFAT_MAX_CACHE) {
  151. ei->nr_caches++;
  152. spin_unlock(&ei->cache_lru_lock);
  153. tmp = exfat_cache_alloc();
  154. if (!tmp) {
  155. spin_lock(&ei->cache_lru_lock);
  156. ei->nr_caches--;
  157. spin_unlock(&ei->cache_lru_lock);
  158. return;
  159. }
  160. spin_lock(&ei->cache_lru_lock);
  161. cache = exfat_cache_merge(inode, new);
  162. if (cache != NULL) {
  163. ei->nr_caches--;
  164. exfat_cache_free(tmp);
  165. goto out_update_lru;
  166. }
  167. cache = tmp;
  168. } else {
  169. struct list_head *p = ei->cache_lru.prev;
  170. cache = list_entry(p,
  171. struct exfat_cache, cache_list);
  172. }
  173. cache->fcluster = new->fcluster;
  174. cache->dcluster = new->dcluster;
  175. cache->nr_contig = new->nr_contig;
  176. }
  177. out_update_lru:
  178. exfat_cache_update_lru(inode, cache);
  179. unlock:
  180. spin_unlock(&ei->cache_lru_lock);
  181. }
  182. /*
  183. * Cache invalidation occurs rarely, thus the LRU chain is not updated. It
  184. * fixes itself after a while.
  185. */
  186. static void __exfat_cache_inval_inode(struct inode *inode)
  187. {
  188. struct exfat_inode_info *ei = EXFAT_I(inode);
  189. struct exfat_cache *cache;
  190. while (!list_empty(&ei->cache_lru)) {
  191. cache = list_entry(ei->cache_lru.next,
  192. struct exfat_cache, cache_list);
  193. list_del_init(&cache->cache_list);
  194. ei->nr_caches--;
  195. exfat_cache_free(cache);
  196. }
  197. /* Update. The copy of caches before this id is discarded. */
  198. ei->cache_valid_id++;
  199. if (ei->cache_valid_id == EXFAT_CACHE_VALID)
  200. ei->cache_valid_id++;
  201. }
  202. void exfat_cache_inval_inode(struct inode *inode)
  203. {
  204. struct exfat_inode_info *ei = EXFAT_I(inode);
  205. spin_lock(&ei->cache_lru_lock);
  206. __exfat_cache_inval_inode(inode);
  207. spin_unlock(&ei->cache_lru_lock);
  208. }
  209. static inline int cache_contiguous(struct exfat_cache_id *cid,
  210. unsigned int dclus)
  211. {
  212. cid->nr_contig++;
  213. return cid->dcluster + cid->nr_contig == dclus;
  214. }
  215. static inline void cache_init(struct exfat_cache_id *cid,
  216. unsigned int fclus, unsigned int dclus)
  217. {
  218. cid->id = EXFAT_CACHE_VALID;
  219. cid->fcluster = fclus;
  220. cid->dcluster = dclus;
  221. cid->nr_contig = 0;
  222. }
  223. int exfat_get_cluster(struct inode *inode, unsigned int cluster,
  224. unsigned int *dclus, unsigned int *count,
  225. unsigned int *last_dclus)
  226. {
  227. struct super_block *sb = inode->i_sb;
  228. struct exfat_inode_info *ei = EXFAT_I(inode);
  229. struct buffer_head *bh = NULL;
  230. struct exfat_cache_id cid;
  231. unsigned int content, fclus;
  232. unsigned int end = cluster + *count - 1;
  233. if (ei->start_clu == EXFAT_FREE_CLUSTER) {
  234. exfat_fs_error(sb,
  235. "invalid access to exfat cache (entry 0x%08x)",
  236. ei->start_clu);
  237. return -EIO;
  238. }
  239. fclus = 0;
  240. *dclus = ei->start_clu;
  241. *last_dclus = *dclus;
  242. /*
  243. * This case should not exist, as exfat_map_cluster function doesn't
  244. * call this routine when start_clu == EXFAT_EOF_CLUSTER.
  245. * This case is retained here for routine completeness.
  246. */
  247. if (*dclus == EXFAT_EOF_CLUSTER) {
  248. *count = 0;
  249. return 0;
  250. }
  251. /* If only the first cluster is needed, return now. */
  252. if (fclus == cluster && *count == 1)
  253. return 0;
  254. cache_init(&cid, fclus, *dclus);
  255. /*
  256. * Update the 'end' to exclude the next cache range, as clusters in
  257. * different cache are typically not contiguous.
  258. */
  259. end = exfat_cache_lookup(inode, &cid, cluster, end, &fclus, dclus);
  260. /* Return if the cache covers the entire range. */
  261. if (cid.fcluster + cid.nr_contig >= end) {
  262. *count = end - cluster + 1;
  263. return 0;
  264. }
  265. /* Find the first cluster we need. */
  266. while (fclus < cluster) {
  267. if (exfat_ent_get(sb, *dclus, &content, &bh))
  268. return -EIO;
  269. *last_dclus = *dclus;
  270. *dclus = content;
  271. fclus++;
  272. if (content == EXFAT_EOF_CLUSTER)
  273. break;
  274. if (!cache_contiguous(&cid, *dclus))
  275. cache_init(&cid, fclus, *dclus);
  276. }
  277. /*
  278. * Now the cid cache contains the first cluster requested, collect
  279. * the remaining clusters of this contiguous extent.
  280. */
  281. if (*dclus != EXFAT_EOF_CLUSTER) {
  282. unsigned int clu = *dclus;
  283. while (fclus < end) {
  284. if (exfat_ent_get(sb, clu, &content, &bh))
  285. return -EIO;
  286. if (++clu != content)
  287. break;
  288. fclus++;
  289. }
  290. cid.nr_contig = fclus - cid.fcluster;
  291. *count = fclus - cluster + 1;
  292. /*
  293. * Cache this discontiguous cluster, we'll definitely need
  294. * it later
  295. */
  296. if (fclus < end && content != EXFAT_EOF_CLUSTER) {
  297. exfat_cache_add(inode, &cid);
  298. cache_init(&cid, fclus + 1, content);
  299. }
  300. } else {
  301. *count = 0;
  302. }
  303. brelse(bh);
  304. exfat_cache_add(inode, &cid);
  305. return 0;
  306. }