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 <linux/unaligned.h>
8 #include <linux/buffer_head.h>
9 #include <linux/blkdev.h>
11 #include "exfat_raw.h"
14 static int exfat_mirror_bh(struct super_block
*sb
, sector_t sec
,
15 struct buffer_head
*bh
)
17 struct buffer_head
*c_bh
;
18 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
22 if (sbi
->FAT2_start_sector
!= sbi
->FAT1_start_sector
) {
23 sec2
= sec
- sbi
->FAT1_start_sector
+ sbi
->FAT2_start_sector
;
24 c_bh
= sb_getblk(sb
, sec2
);
27 memcpy(c_bh
->b_data
, bh
->b_data
, sb
->s_blocksize
);
28 set_buffer_uptodate(c_bh
);
29 mark_buffer_dirty(c_bh
);
30 if (sb
->s_flags
& SB_SYNCHRONOUS
)
31 err
= sync_dirty_buffer(c_bh
);
38 static int __exfat_ent_get(struct super_block
*sb
, unsigned int loc
,
39 unsigned int *content
)
43 struct buffer_head
*bh
;
45 sec
= FAT_ENT_OFFSET_SECTOR(sb
, loc
);
46 off
= FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb
, loc
);
48 bh
= sb_bread(sb
, sec
);
52 *content
= le32_to_cpu(*(__le32
*)(&bh
->b_data
[off
]));
54 /* remap reserved clusters to simplify code */
55 if (*content
> EXFAT_BAD_CLUSTER
)
56 *content
= EXFAT_EOF_CLUSTER
;
62 int exfat_ent_set(struct super_block
*sb
, unsigned int loc
,
68 struct buffer_head
*bh
;
70 sec
= FAT_ENT_OFFSET_SECTOR(sb
, loc
);
71 off
= FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb
, loc
);
73 bh
= sb_bread(sb
, sec
);
77 fat_entry
= (__le32
*)&(bh
->b_data
[off
]);
78 *fat_entry
= cpu_to_le32(content
);
79 exfat_update_bh(bh
, sb
->s_flags
& SB_SYNCHRONOUS
);
80 exfat_mirror_bh(sb
, sec
, bh
);
85 int exfat_ent_get(struct super_block
*sb
, unsigned int loc
,
86 unsigned int *content
)
88 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
91 if (!is_valid_cluster(sbi
, loc
)) {
92 exfat_fs_error(sb
, "invalid access to FAT (entry 0x%08x)",
97 err
= __exfat_ent_get(sb
, loc
, content
);
100 "failed to access to FAT (entry 0x%08x, err:%d)",
105 if (*content
== EXFAT_FREE_CLUSTER
) {
107 "invalid access to FAT free cluster (entry 0x%08x)",
112 if (*content
== EXFAT_BAD_CLUSTER
) {
114 "invalid access to FAT bad cluster (entry 0x%08x)",
119 if (*content
!= EXFAT_EOF_CLUSTER
&& !is_valid_cluster(sbi
, *content
)) {
121 "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
129 int exfat_chain_cont_cluster(struct super_block
*sb
, unsigned int chain
,
136 if (exfat_ent_set(sb
, chain
, chain
+ 1))
142 if (exfat_ent_set(sb
, chain
, EXFAT_EOF_CLUSTER
))
147 /* This function must be called with bitmap_lock held */
148 static int __exfat_free_cluster(struct inode
*inode
, struct exfat_chain
*p_chain
)
150 struct super_block
*sb
= inode
->i_sb
;
151 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
152 int cur_cmap_i
, next_cmap_i
;
153 unsigned int num_clusters
= 0;
156 /* invalid cluster number */
157 if (p_chain
->dir
== EXFAT_FREE_CLUSTER
||
158 p_chain
->dir
== EXFAT_EOF_CLUSTER
||
159 p_chain
->dir
< EXFAT_FIRST_CLUSTER
)
162 /* no cluster to truncate */
163 if (p_chain
->size
== 0)
166 /* check cluster validation */
167 if (!is_valid_cluster(sbi
, p_chain
->dir
)) {
168 exfat_err(sb
, "invalid start cluster (%u)", p_chain
->dir
);
174 cur_cmap_i
= next_cmap_i
=
175 BITMAP_OFFSET_SECTOR_INDEX(sb
, CLUSTER_TO_BITMAP_ENT(clu
));
177 if (p_chain
->flags
== ALLOC_NO_FAT_CHAIN
) {
178 unsigned int last_cluster
= p_chain
->dir
+ p_chain
->size
- 1;
182 if (clu
< last_cluster
)
184 BITMAP_OFFSET_SECTOR_INDEX(sb
, CLUSTER_TO_BITMAP_ENT(clu
+1));
186 /* flush bitmap only if index would be changed or for last cluster */
187 if (clu
== last_cluster
|| cur_cmap_i
!= next_cmap_i
) {
189 cur_cmap_i
= next_cmap_i
;
192 exfat_clear_bitmap(inode
, clu
, (sync
&& IS_DIRSYNC(inode
)));
195 } while (num_clusters
< p_chain
->size
);
199 unsigned int n_clu
= clu
;
200 int err
= exfat_get_next_cluster(sb
, &n_clu
);
202 if (err
|| n_clu
== EXFAT_EOF_CLUSTER
)
206 BITMAP_OFFSET_SECTOR_INDEX(sb
, CLUSTER_TO_BITMAP_ENT(n_clu
));
208 if (cur_cmap_i
!= next_cmap_i
) {
210 cur_cmap_i
= next_cmap_i
;
213 exfat_clear_bitmap(inode
, clu
, (sync
&& IS_DIRSYNC(inode
)));
219 } while (clu
!= EXFAT_EOF_CLUSTER
);
223 sbi
->used_clusters
-= num_clusters
;
227 int exfat_free_cluster(struct inode
*inode
, struct exfat_chain
*p_chain
)
231 mutex_lock(&EXFAT_SB(inode
->i_sb
)->bitmap_lock
);
232 ret
= __exfat_free_cluster(inode
, p_chain
);
233 mutex_unlock(&EXFAT_SB(inode
->i_sb
)->bitmap_lock
);
238 int exfat_find_last_cluster(struct super_block
*sb
, struct exfat_chain
*p_chain
,
239 unsigned int *ret_clu
)
241 unsigned int clu
, next
;
242 unsigned int count
= 0;
245 if (p_chain
->flags
== ALLOC_NO_FAT_CHAIN
) {
246 *ret_clu
= next
+ p_chain
->size
- 1;
253 if (exfat_ent_get(sb
, clu
, &next
))
255 } while (next
!= EXFAT_EOF_CLUSTER
);
257 if (p_chain
->size
!= count
) {
259 "bogus directory size (clus : ondisk(%d) != counted(%d))",
260 p_chain
->size
, count
);
268 int exfat_zeroed_cluster(struct inode
*dir
, unsigned int clu
)
270 struct super_block
*sb
= dir
->i_sb
;
271 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
272 struct buffer_head
*bh
;
273 sector_t blknr
, last_blknr
, i
;
275 blknr
= exfat_cluster_to_sector(sbi
, clu
);
276 last_blknr
= blknr
+ sbi
->sect_per_clus
;
278 if (last_blknr
> sbi
->num_sectors
&& sbi
->num_sectors
> 0) {
279 exfat_fs_error_ratelimit(sb
,
280 "%s: out of range(sect:%llu len:%u)",
281 __func__
, (unsigned long long)blknr
,
286 /* Zeroing the unused blocks on this cluster */
287 for (i
= blknr
; i
< last_blknr
; i
++) {
288 bh
= sb_getblk(sb
, i
);
292 memset(bh
->b_data
, 0, sb
->s_blocksize
);
293 set_buffer_uptodate(bh
);
294 mark_buffer_dirty(bh
);
299 return sync_blockdev_range(sb
->s_bdev
,
300 EXFAT_BLK_TO_B(blknr
, sb
),
301 EXFAT_BLK_TO_B(last_blknr
, sb
) - 1);
306 int exfat_alloc_cluster(struct inode
*inode
, unsigned int num_alloc
,
307 struct exfat_chain
*p_chain
, bool sync_bmap
)
310 unsigned int total_cnt
;
311 unsigned int hint_clu
, new_clu
, last_clu
= EXFAT_EOF_CLUSTER
;
312 struct super_block
*sb
= inode
->i_sb
;
313 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
315 total_cnt
= EXFAT_DATA_CLUSTER_COUNT(sbi
);
317 if (unlikely(total_cnt
< sbi
->used_clusters
)) {
318 exfat_fs_error_ratelimit(sb
,
319 "%s: invalid used clusters(t:%u,u:%u)\n",
320 __func__
, total_cnt
, sbi
->used_clusters
);
324 if (num_alloc
> total_cnt
- sbi
->used_clusters
)
327 mutex_lock(&sbi
->bitmap_lock
);
329 hint_clu
= p_chain
->dir
;
330 /* find new cluster */
331 if (hint_clu
== EXFAT_EOF_CLUSTER
) {
332 if (sbi
->clu_srch_ptr
< EXFAT_FIRST_CLUSTER
) {
333 exfat_err(sb
, "sbi->clu_srch_ptr is invalid (%u)",
335 sbi
->clu_srch_ptr
= EXFAT_FIRST_CLUSTER
;
338 hint_clu
= exfat_find_free_bitmap(sb
, sbi
->clu_srch_ptr
);
339 if (hint_clu
== EXFAT_EOF_CLUSTER
) {
345 /* check cluster validation */
346 if (!is_valid_cluster(sbi
, hint_clu
)) {
347 if (hint_clu
!= sbi
->num_clusters
)
348 exfat_err(sb
, "hint_cluster is invalid (%u), rewind to the first cluster",
350 hint_clu
= EXFAT_FIRST_CLUSTER
;
351 p_chain
->flags
= ALLOC_FAT_CHAIN
;
354 p_chain
->dir
= EXFAT_EOF_CLUSTER
;
356 while ((new_clu
= exfat_find_free_bitmap(sb
, hint_clu
)) !=
358 if (new_clu
!= hint_clu
&&
359 p_chain
->flags
== ALLOC_NO_FAT_CHAIN
) {
360 if (exfat_chain_cont_cluster(sb
, p_chain
->dir
,
365 p_chain
->flags
= ALLOC_FAT_CHAIN
;
368 /* update allocation bitmap */
369 if (exfat_set_bitmap(inode
, new_clu
, sync_bmap
)) {
374 /* update FAT table */
375 if (p_chain
->flags
== ALLOC_FAT_CHAIN
) {
376 if (exfat_ent_set(sb
, new_clu
, EXFAT_EOF_CLUSTER
)) {
382 if (p_chain
->dir
== EXFAT_EOF_CLUSTER
) {
383 p_chain
->dir
= new_clu
;
384 } else if (p_chain
->flags
== ALLOC_FAT_CHAIN
) {
385 if (exfat_ent_set(sb
, last_clu
, new_clu
)) {
394 if (p_chain
->size
== num_alloc
) {
395 sbi
->clu_srch_ptr
= hint_clu
;
396 sbi
->used_clusters
+= num_alloc
;
398 mutex_unlock(&sbi
->bitmap_lock
);
402 hint_clu
= new_clu
+ 1;
403 if (hint_clu
>= sbi
->num_clusters
) {
404 hint_clu
= EXFAT_FIRST_CLUSTER
;
406 if (p_chain
->flags
== ALLOC_NO_FAT_CHAIN
) {
407 if (exfat_chain_cont_cluster(sb
, p_chain
->dir
,
412 p_chain
->flags
= ALLOC_FAT_CHAIN
;
417 __exfat_free_cluster(inode
, p_chain
);
419 mutex_unlock(&sbi
->bitmap_lock
);
423 int exfat_count_num_clusters(struct super_block
*sb
,
424 struct exfat_chain
*p_chain
, unsigned int *ret_count
)
426 unsigned int i
, count
;
428 struct exfat_sb_info
*sbi
= EXFAT_SB(sb
);
430 if (!p_chain
->dir
|| p_chain
->dir
== EXFAT_EOF_CLUSTER
) {
435 if (p_chain
->flags
== ALLOC_NO_FAT_CHAIN
) {
436 *ret_count
= p_chain
->size
;
442 for (i
= EXFAT_FIRST_CLUSTER
; i
< sbi
->num_clusters
; i
++) {
444 if (exfat_ent_get(sb
, clu
, &clu
))
446 if (clu
== EXFAT_EOF_CLUSTER
)