1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/slab.h>
7 #include <asm/unaligned.h>
8 #include <linux/buffer_head.h>
10 #include "exfat_raw.h"
13 static int exfat_mirror_bh(struct super_block *sb, sector_t sec,
14 struct buffer_head *bh)
16 struct buffer_head *c_bh;
17 struct exfat_sb_info *sbi = EXFAT_SB(sb);
21 if (sbi->FAT2_start_sector != sbi->FAT1_start_sector) {
22 sec2 = sec - sbi->FAT1_start_sector + sbi->FAT2_start_sector;
23 c_bh = sb_getblk(sb, sec2);
26 memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
27 set_buffer_uptodate(c_bh);
28 mark_buffer_dirty(c_bh);
29 if (sb->s_flags & SB_SYNCHRONOUS)
30 err = sync_dirty_buffer(c_bh);
37 static int __exfat_ent_get(struct super_block *sb, unsigned int loc,
38 unsigned int *content)
42 struct buffer_head *bh;
44 sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
45 off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
47 bh = sb_bread(sb, sec);
51 *content = le32_to_cpu(*(__le32 *)(&bh->b_data[off]));
53 /* remap reserved clusters to simplify code */
54 if (*content > EXFAT_BAD_CLUSTER)
55 *content = EXFAT_EOF_CLUSTER;
61 int exfat_ent_set(struct super_block *sb, unsigned int loc,
67 struct buffer_head *bh;
69 sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
70 off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
72 bh = sb_bread(sb, sec);
76 fat_entry = (__le32 *)&(bh->b_data[off]);
77 *fat_entry = cpu_to_le32(content);
78 exfat_update_bh(bh, sb->s_flags & SB_SYNCHRONOUS);
79 exfat_mirror_bh(sb, sec, bh);
84 int exfat_ent_get(struct super_block *sb, unsigned int loc,
85 unsigned int *content)
87 struct exfat_sb_info *sbi = EXFAT_SB(sb);
90 if (!is_valid_cluster(sbi, loc)) {
91 exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)",
96 err = __exfat_ent_get(sb, loc, content);
99 "failed to access to FAT (entry 0x%08x, err:%d)",
104 if (*content == EXFAT_FREE_CLUSTER) {
106 "invalid access to FAT free cluster (entry 0x%08x)",
111 if (*content == EXFAT_BAD_CLUSTER) {
113 "invalid access to FAT bad cluster (entry 0x%08x)",
118 if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
120 "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
128 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
135 if (exfat_ent_set(sb, chain, chain + 1))
141 if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
146 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
148 unsigned int num_clusters = 0;
150 struct super_block *sb = inode->i_sb;
151 struct exfat_sb_info *sbi = EXFAT_SB(sb);
153 /* invalid cluster number */
154 if (p_chain->dir == EXFAT_FREE_CLUSTER ||
155 p_chain->dir == EXFAT_EOF_CLUSTER ||
156 p_chain->dir < EXFAT_FIRST_CLUSTER)
159 /* no cluster to truncate */
160 if (p_chain->size == 0)
163 /* check cluster validation */
164 if (!is_valid_cluster(sbi, p_chain->dir)) {
165 exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
171 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
173 exfat_clear_bitmap(inode, clu);
177 } while (num_clusters < p_chain->size);
180 exfat_clear_bitmap(inode, clu);
182 if (exfat_get_next_cluster(sb, &clu))
186 } while (clu != EXFAT_EOF_CLUSTER);
190 sbi->used_clusters -= num_clusters;
194 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
195 unsigned int *ret_clu)
197 unsigned int clu, next;
198 unsigned int count = 0;
201 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
202 *ret_clu = next + p_chain->size - 1;
209 if (exfat_ent_get(sb, clu, &next))
211 } while (next != EXFAT_EOF_CLUSTER);
213 if (p_chain->size != count) {
215 "bogus directory size (clus : ondisk(%d) != counted(%d))",
216 p_chain->size, count);
224 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
226 struct super_block *sb = dir->i_sb;
227 struct exfat_sb_info *sbi = EXFAT_SB(sb);
228 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
229 int nr_bhs = MAX_BUF_PER_PAGE;
230 sector_t blknr, last_blknr;
233 blknr = exfat_cluster_to_sector(sbi, clu);
234 last_blknr = blknr + sbi->sect_per_clus;
236 if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
237 exfat_fs_error_ratelimit(sb,
238 "%s: out of range(sect:%llu len:%u)",
239 __func__, (unsigned long long)blknr,
244 /* Zeroing the unused blocks on this cluster */
245 while (blknr < last_blknr) {
246 for (n = 0; n < nr_bhs && blknr < last_blknr; n++, blknr++) {
247 bhs[n] = sb_getblk(sb, blknr);
252 memset(bhs[n]->b_data, 0, sb->s_blocksize);
255 err = exfat_update_bhs(bhs, n, IS_DIRSYNC(dir));
259 for (i = 0; i < n; i++)
265 exfat_err(sb, "failed zeroed sect %llu\n", (unsigned long long)blknr);
266 for (i = 0; i < n; i++)
271 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
272 struct exfat_chain *p_chain)
275 unsigned int num_clusters = 0, total_cnt;
276 unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
277 struct super_block *sb = inode->i_sb;
278 struct exfat_sb_info *sbi = EXFAT_SB(sb);
280 total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
282 if (unlikely(total_cnt < sbi->used_clusters)) {
283 exfat_fs_error_ratelimit(sb,
284 "%s: invalid used clusters(t:%u,u:%u)\n",
285 __func__, total_cnt, sbi->used_clusters);
289 if (num_alloc > total_cnt - sbi->used_clusters)
292 hint_clu = p_chain->dir;
293 /* find new cluster */
294 if (hint_clu == EXFAT_EOF_CLUSTER) {
295 if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
296 exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n",
298 sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
301 hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
302 if (hint_clu == EXFAT_EOF_CLUSTER)
306 /* check cluster validation */
307 if (!is_valid_cluster(sbi, hint_clu)) {
308 exfat_err(sb, "hint_cluster is invalid (%u)",
310 hint_clu = EXFAT_FIRST_CLUSTER;
311 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
312 if (exfat_chain_cont_cluster(sb, p_chain->dir,
315 p_chain->flags = ALLOC_FAT_CHAIN;
319 p_chain->dir = EXFAT_EOF_CLUSTER;
321 while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
323 if (new_clu != hint_clu &&
324 p_chain->flags == ALLOC_NO_FAT_CHAIN) {
325 if (exfat_chain_cont_cluster(sb, p_chain->dir,
330 p_chain->flags = ALLOC_FAT_CHAIN;
333 /* update allocation bitmap */
334 if (exfat_set_bitmap(inode, new_clu)) {
341 /* update FAT table */
342 if (p_chain->flags == ALLOC_FAT_CHAIN) {
343 if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
349 if (p_chain->dir == EXFAT_EOF_CLUSTER) {
350 p_chain->dir = new_clu;
351 } else if (p_chain->flags == ALLOC_FAT_CHAIN) {
352 if (exfat_ent_set(sb, last_clu, new_clu)) {
359 if (--num_alloc == 0) {
360 sbi->clu_srch_ptr = hint_clu;
361 sbi->used_clusters += num_clusters;
363 p_chain->size += num_clusters;
367 hint_clu = new_clu + 1;
368 if (hint_clu >= sbi->num_clusters) {
369 hint_clu = EXFAT_FIRST_CLUSTER;
371 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
372 if (exfat_chain_cont_cluster(sb, p_chain->dir,
377 p_chain->flags = ALLOC_FAT_CHAIN;
383 exfat_free_cluster(inode, p_chain);
387 int exfat_count_num_clusters(struct super_block *sb,
388 struct exfat_chain *p_chain, unsigned int *ret_count)
390 unsigned int i, count;
392 struct exfat_sb_info *sbi = EXFAT_SB(sb);
394 if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
399 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
400 *ret_count = p_chain->size;
406 for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
408 if (exfat_ent_get(sb, clu, &clu))
410 if (clu == EXFAT_EOF_CLUSTER)