^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) * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "exfat_raw.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "exfat_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) static int exfat_mirror_bh(struct super_block *sb, sector_t sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct buffer_head *c_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) sector_t sec2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (sbi->FAT2_start_sector != sbi->FAT1_start_sector) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) sec2 = sec - sbi->FAT1_start_sector + sbi->FAT2_start_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) c_bh = sb_getblk(sb, sec2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) if (!c_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) set_buffer_uptodate(c_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) mark_buffer_dirty(c_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (sb->s_flags & SB_SYNCHRONOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) err = sync_dirty_buffer(c_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) brelse(c_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int __exfat_ent_get(struct super_block *sb, unsigned int loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int *content)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) sector_t sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) bh = sb_bread(sb, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *content = le32_to_cpu(*(__le32 *)(&bh->b_data[off]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* remap reserved clusters to simplify code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (*content > EXFAT_BAD_CLUSTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *content = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int exfat_ent_set(struct super_block *sb, unsigned int loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned int content)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned int off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) sector_t sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) __le32 *fat_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) bh = sb_bread(sb, sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) fat_entry = (__le32 *)&(bh->b_data[off]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *fat_entry = cpu_to_le32(content);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) exfat_update_bh(bh, sb->s_flags & SB_SYNCHRONOUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) exfat_mirror_bh(sb, sec, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) unsigned int clus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (clus < EXFAT_FIRST_CLUSTER || sbi->num_clusters <= clus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int exfat_ent_get(struct super_block *sb, unsigned int loc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned int *content)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!is_valid_cluster(sbi, loc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return -EIO;
^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) err = __exfat_ent_get(sb, loc, content);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) "failed to access to FAT (entry 0x%08x, err:%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) loc, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (*content == EXFAT_FREE_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) "invalid access to FAT free cluster (entry 0x%08x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (*content == EXFAT_BAD_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) "invalid access to FAT bad cluster (entry 0x%08x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) loc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) loc, *content);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) while (len > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (exfat_ent_set(sb, chain, chain + 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) chain++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) unsigned int num_clusters = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned int clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* invalid cluster number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (p_chain->dir == EXFAT_FREE_CLUSTER ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) p_chain->dir == EXFAT_EOF_CLUSTER ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) p_chain->dir < EXFAT_FIRST_CLUSTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* no cluster to truncate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (p_chain->size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* check cluster validation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (!is_valid_cluster(sbi, p_chain->dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) clu = p_chain->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) exfat_clear_bitmap(inode, clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) clu++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) num_clusters++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) } while (num_clusters < p_chain->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) exfat_clear_bitmap(inode, clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (exfat_get_next_cluster(sb, &clu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) goto dec_used_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) num_clusters++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) } while (clu != EXFAT_EOF_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) dec_used_clus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) sbi->used_clusters -= num_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned int *ret_clu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) unsigned int clu, next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) unsigned int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) next = p_chain->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) *ret_clu = next + p_chain->size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) clu = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (exfat_ent_get(sb, clu, &next))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) } while (next != EXFAT_EOF_CLUSTER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (p_chain->size != count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) "bogus directory size (clus : ondisk(%d) != counted(%d))",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) p_chain->size, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) *ret_clu = clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct buffer_head *bhs[MAX_BUF_PER_PAGE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int nr_bhs = MAX_BUF_PER_PAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) sector_t blknr, last_blknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int err, i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) blknr = exfat_cluster_to_sector(sbi, clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) last_blknr = blknr + sbi->sect_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) exfat_fs_error_ratelimit(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) "%s: out of range(sect:%llu len:%u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) __func__, (unsigned long long)blknr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) sbi->sect_per_clus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* Zeroing the unused blocks on this cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) while (blknr < last_blknr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) for (n = 0; n < nr_bhs && blknr < last_blknr; n++, blknr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) bhs[n] = sb_getblk(sb, blknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (!bhs[n]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) goto release_bhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) memset(bhs[n]->b_data, 0, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) err = exfat_update_bhs(bhs, n, IS_DIRSYNC(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) goto release_bhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) for (i = 0; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) brelse(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) release_bhs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) exfat_err(sb, "failed zeroed sect %llu\n", (unsigned long long)blknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) for (i = 0; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) bforget(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct exfat_chain *p_chain, bool sync_bmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) int ret = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) unsigned int num_clusters = 0, total_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (unlikely(total_cnt < sbi->used_clusters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) exfat_fs_error_ratelimit(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) "%s: invalid used clusters(t:%u,u:%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) __func__, total_cnt, sbi->used_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (num_alloc > total_cnt - sbi->used_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) hint_clu = p_chain->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* find new cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (hint_clu == EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) sbi->clu_srch_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (hint_clu == EXFAT_EOF_CLUSTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* check cluster validation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (!is_valid_cluster(sbi, hint_clu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) exfat_err(sb, "hint_cluster is invalid (%u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) hint_clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) hint_clu = EXFAT_FIRST_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (exfat_chain_cont_cluster(sb, p_chain->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) num_clusters))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) p_chain->flags = ALLOC_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) p_chain->dir = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (new_clu != hint_clu &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) p_chain->flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (exfat_chain_cont_cluster(sb, p_chain->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) num_clusters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) goto free_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) p_chain->flags = ALLOC_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /* update allocation bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (exfat_set_bitmap(inode, new_clu, sync_bmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) goto free_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) num_clusters++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /* update FAT table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (p_chain->flags == ALLOC_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) goto free_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (p_chain->dir == EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) p_chain->dir = new_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) } else if (p_chain->flags == ALLOC_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (exfat_ent_set(sb, last_clu, new_clu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) goto free_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) last_clu = new_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (--num_alloc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) sbi->clu_srch_ptr = hint_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) sbi->used_clusters += num_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) p_chain->size += num_clusters;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) hint_clu = new_clu + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (hint_clu >= sbi->num_clusters) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) hint_clu = EXFAT_FIRST_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (exfat_chain_cont_cluster(sb, p_chain->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) num_clusters)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) goto free_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) p_chain->flags = ALLOC_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) free_cluster:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (num_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) exfat_free_cluster(inode, p_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int exfat_count_num_clusters(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct exfat_chain *p_chain, unsigned int *ret_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) unsigned int i, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) unsigned int clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) *ret_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) *ret_count = p_chain->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) clu = p_chain->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (exfat_ent_get(sb, clu, &clu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (clu == EXFAT_EOF_CLUSTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) *ret_count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }