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 static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
87 return clus >= EXFAT_FIRST_CLUSTER && clus < sbi->num_clusters;
90 int exfat_ent_get(struct super_block *sb, unsigned int loc,
91 unsigned int *content)
93 struct exfat_sb_info *sbi = EXFAT_SB(sb);
96 if (!is_valid_cluster(sbi, loc)) {
97 exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)",
102 err = __exfat_ent_get(sb, loc, content);
105 "failed to access to FAT (entry 0x%08x, err:%d)",
110 if (*content == EXFAT_FREE_CLUSTER) {
112 "invalid access to FAT free cluster (entry 0x%08x)",
117 if (*content == EXFAT_BAD_CLUSTER) {
119 "invalid access to FAT bad cluster (entry 0x%08x)",
124 if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
126 "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
134 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
141 if (exfat_ent_set(sb, chain, chain + 1))
147 if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
152 /* This function must be called with bitmap_lock held */
153 static int __exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
155 struct super_block *sb = inode->i_sb;
156 struct exfat_sb_info *sbi = EXFAT_SB(sb);
157 int cur_cmap_i, next_cmap_i;
158 unsigned int num_clusters = 0;
161 /* invalid cluster number */
162 if (p_chain->dir == EXFAT_FREE_CLUSTER ||
163 p_chain->dir == EXFAT_EOF_CLUSTER ||
164 p_chain->dir < EXFAT_FIRST_CLUSTER)
167 /* no cluster to truncate */
168 if (p_chain->size == 0)
171 /* check cluster validation */
172 if (!is_valid_cluster(sbi, p_chain->dir)) {
173 exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
179 cur_cmap_i = next_cmap_i =
180 BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu));
182 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
183 unsigned int last_cluster = p_chain->dir + p_chain->size - 1;
187 if (clu < last_cluster)
189 BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu+1));
191 /* flush bitmap only if index would be changed or for last cluster */
192 if (clu == last_cluster || cur_cmap_i != next_cmap_i) {
194 cur_cmap_i = next_cmap_i;
197 exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode)));
200 } while (num_clusters < p_chain->size);
204 unsigned int n_clu = clu;
205 int err = exfat_get_next_cluster(sb, &n_clu);
207 if (err || n_clu == EXFAT_EOF_CLUSTER)
211 BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(n_clu));
213 if (cur_cmap_i != next_cmap_i) {
215 cur_cmap_i = next_cmap_i;
218 exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode)));
224 } while (clu != EXFAT_EOF_CLUSTER);
228 sbi->used_clusters -= num_clusters;
232 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
236 mutex_lock(&EXFAT_SB(inode->i_sb)->bitmap_lock);
237 ret = __exfat_free_cluster(inode, p_chain);
238 mutex_unlock(&EXFAT_SB(inode->i_sb)->bitmap_lock);
243 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
244 unsigned int *ret_clu)
246 unsigned int clu, next;
247 unsigned int count = 0;
250 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
251 *ret_clu = next + p_chain->size - 1;
258 if (exfat_ent_get(sb, clu, &next))
260 } while (next != EXFAT_EOF_CLUSTER);
262 if (p_chain->size != count) {
264 "bogus directory size (clus : ondisk(%d) != counted(%d))",
265 p_chain->size, count);
273 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
275 struct super_block *sb = dir->i_sb;
276 struct exfat_sb_info *sbi = EXFAT_SB(sb);
277 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
278 int nr_bhs = MAX_BUF_PER_PAGE;
279 sector_t blknr, last_blknr;
282 blknr = exfat_cluster_to_sector(sbi, clu);
283 last_blknr = blknr + sbi->sect_per_clus;
285 if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
286 exfat_fs_error_ratelimit(sb,
287 "%s: out of range(sect:%llu len:%u)",
288 __func__, (unsigned long long)blknr,
293 /* Zeroing the unused blocks on this cluster */
294 while (blknr < last_blknr) {
295 for (n = 0; n < nr_bhs && blknr < last_blknr; n++, blknr++) {
296 bhs[n] = sb_getblk(sb, blknr);
301 memset(bhs[n]->b_data, 0, sb->s_blocksize);
304 err = exfat_update_bhs(bhs, n, IS_DIRSYNC(dir));
308 for (i = 0; i < n; i++)
314 exfat_err(sb, "failed zeroed sect %llu\n", (unsigned long long)blknr);
315 for (i = 0; i < n; i++)
320 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
321 struct exfat_chain *p_chain, bool sync_bmap)
324 unsigned int num_clusters = 0, total_cnt;
325 unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
326 struct super_block *sb = inode->i_sb;
327 struct exfat_sb_info *sbi = EXFAT_SB(sb);
329 total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
331 if (unlikely(total_cnt < sbi->used_clusters)) {
332 exfat_fs_error_ratelimit(sb,
333 "%s: invalid used clusters(t:%u,u:%u)\n",
334 __func__, total_cnt, sbi->used_clusters);
338 if (num_alloc > total_cnt - sbi->used_clusters)
341 mutex_lock(&sbi->bitmap_lock);
343 hint_clu = p_chain->dir;
344 /* find new cluster */
345 if (hint_clu == EXFAT_EOF_CLUSTER) {
346 if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
347 exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n",
349 sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
352 hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
353 if (hint_clu == EXFAT_EOF_CLUSTER) {
359 /* check cluster validation */
360 if (!is_valid_cluster(sbi, hint_clu)) {
361 exfat_err(sb, "hint_cluster is invalid (%u)",
363 hint_clu = EXFAT_FIRST_CLUSTER;
364 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
365 if (exfat_chain_cont_cluster(sb, p_chain->dir,
370 p_chain->flags = ALLOC_FAT_CHAIN;
374 p_chain->dir = EXFAT_EOF_CLUSTER;
376 while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
378 if (new_clu != hint_clu &&
379 p_chain->flags == ALLOC_NO_FAT_CHAIN) {
380 if (exfat_chain_cont_cluster(sb, p_chain->dir,
385 p_chain->flags = ALLOC_FAT_CHAIN;
388 /* update allocation bitmap */
389 if (exfat_set_bitmap(inode, new_clu, sync_bmap)) {
396 /* update FAT table */
397 if (p_chain->flags == ALLOC_FAT_CHAIN) {
398 if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
404 if (p_chain->dir == EXFAT_EOF_CLUSTER) {
405 p_chain->dir = new_clu;
406 } else if (p_chain->flags == ALLOC_FAT_CHAIN) {
407 if (exfat_ent_set(sb, last_clu, new_clu)) {
414 if (--num_alloc == 0) {
415 sbi->clu_srch_ptr = hint_clu;
416 sbi->used_clusters += num_clusters;
418 p_chain->size += num_clusters;
419 mutex_unlock(&sbi->bitmap_lock);
423 hint_clu = new_clu + 1;
424 if (hint_clu >= sbi->num_clusters) {
425 hint_clu = EXFAT_FIRST_CLUSTER;
427 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
428 if (exfat_chain_cont_cluster(sb, p_chain->dir,
433 p_chain->flags = ALLOC_FAT_CHAIN;
439 __exfat_free_cluster(inode, p_chain);
441 mutex_unlock(&sbi->bitmap_lock);
445 int exfat_count_num_clusters(struct super_block *sb,
446 struct exfat_chain *p_chain, unsigned int *ret_count)
448 unsigned int i, count;
450 struct exfat_sb_info *sbi = EXFAT_SB(sb);
452 if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
457 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
458 *ret_count = p_chain->size;
464 for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
466 if (exfat_ent_get(sb, clu, &clu))
468 if (clu == EXFAT_EOF_CLUSTER)