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