1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 */ 5 6 #include <linux/slab.h> 7 #include <linux/unaligned.h> 8 #include <linux/buffer_head.h> 9 #include <linux/blkdev.h> 10 11 #include "exfat_raw.h" 12 #include "exfat_fs.h" 13 14 static int exfat_mirror_bh(struct super_block *sb, sector_t sec, 15 struct buffer_head *bh) 16 { 17 struct buffer_head *c_bh; 18 struct exfat_sb_info *sbi = EXFAT_SB(sb); 19 sector_t sec2; 20 int err = 0; 21 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); 25 if (!c_bh) 26 return -ENOMEM; 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); 32 brelse(c_bh); 33 } 34 35 return err; 36 } 37 38 static int __exfat_ent_get(struct super_block *sb, unsigned int loc, 39 unsigned int *content, struct buffer_head **last) 40 { 41 unsigned int off; 42 sector_t sec; 43 struct buffer_head *bh = last ? *last : NULL; 44 45 sec = FAT_ENT_OFFSET_SECTOR(sb, loc); 46 off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc); 47 48 if (!bh || bh->b_blocknr != sec || !buffer_uptodate(bh)) { 49 brelse(bh); 50 bh = sb_bread(sb, sec); 51 if (last) 52 *last = bh; 53 if (unlikely(!bh)) 54 return -EIO; 55 } 56 57 *content = le32_to_cpu(*(__le32 *)(&bh->b_data[off])); 58 59 /* remap reserved clusters to simplify code */ 60 if (*content > EXFAT_BAD_CLUSTER) 61 *content = EXFAT_EOF_CLUSTER; 62 63 if (!last) 64 brelse(bh); 65 return 0; 66 } 67 68 int exfat_ent_set(struct super_block *sb, unsigned int loc, 69 unsigned int content) 70 { 71 unsigned int off; 72 sector_t sec; 73 __le32 *fat_entry; 74 struct buffer_head *bh; 75 76 sec = FAT_ENT_OFFSET_SECTOR(sb, loc); 77 off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc); 78 79 bh = sb_bread(sb, sec); 80 if (!bh) 81 return -EIO; 82 83 fat_entry = (__le32 *)&(bh->b_data[off]); 84 *fat_entry = cpu_to_le32(content); 85 exfat_update_bh(bh, sb->s_flags & SB_SYNCHRONOUS); 86 exfat_mirror_bh(sb, sec, bh); 87 brelse(bh); 88 return 0; 89 } 90 91 /* 92 * Caller must release the buffer_head if no error return. 93 */ 94 int exfat_ent_get(struct super_block *sb, unsigned int loc, 95 unsigned int *content, struct buffer_head **last) 96 { 97 struct exfat_sb_info *sbi = EXFAT_SB(sb); 98 99 if (!is_valid_cluster(sbi, loc)) { 100 exfat_fs_error_ratelimit(sb, 101 "invalid access to FAT (entry 0x%08x)", 102 loc); 103 goto err; 104 } 105 106 if (unlikely(__exfat_ent_get(sb, loc, content, last))) { 107 exfat_fs_error_ratelimit(sb, 108 "failed to access to FAT (entry 0x%08x)", 109 loc); 110 goto err; 111 } 112 113 if (unlikely(*content == EXFAT_FREE_CLUSTER)) { 114 exfat_fs_error_ratelimit(sb, 115 "invalid access to FAT free cluster (entry 0x%08x)", 116 loc); 117 goto err; 118 } 119 120 if (unlikely(*content == EXFAT_BAD_CLUSTER)) { 121 exfat_fs_error_ratelimit(sb, 122 "invalid access to FAT bad cluster (entry 0x%08x)", 123 loc); 124 goto err; 125 } 126 127 if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) { 128 exfat_fs_error_ratelimit(sb, 129 "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)", 130 loc, *content); 131 goto err; 132 } 133 134 return 0; 135 err: 136 if (last) { 137 brelse(*last); 138 139 /* Avoid double release */ 140 *last = NULL; 141 } 142 return -EIO; 143 } 144 145 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain, 146 unsigned int len) 147 { 148 if (!len) 149 return 0; 150 151 while (len > 1) { 152 if (exfat_ent_set(sb, chain, chain + 1)) 153 return -EIO; 154 chain++; 155 len--; 156 } 157 158 if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER)) 159 return -EIO; 160 return 0; 161 } 162 163 static inline void exfat_discard_cluster(struct super_block *sb, 164 unsigned int clu, unsigned int num_clusters) 165 { 166 int ret; 167 struct exfat_sb_info *sbi = EXFAT_SB(sb); 168 169 ret = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, clu), 170 sbi->sect_per_clus * num_clusters, GFP_NOFS, 0); 171 if (ret == -EOPNOTSUPP) { 172 exfat_err(sb, "discard not supported by device, disabling"); 173 sbi->options.discard = 0; 174 } 175 } 176 177 /* This function must be called with bitmap_lock held */ 178 static int __exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain) 179 { 180 struct super_block *sb = inode->i_sb; 181 struct exfat_sb_info *sbi = EXFAT_SB(sb); 182 int cur_cmap_i, next_cmap_i; 183 unsigned int num_clusters = 0; 184 unsigned int clu; 185 186 /* invalid cluster number */ 187 if (p_chain->dir == EXFAT_FREE_CLUSTER || 188 p_chain->dir == EXFAT_EOF_CLUSTER || 189 p_chain->dir < EXFAT_FIRST_CLUSTER) 190 return 0; 191 192 /* no cluster to truncate */ 193 if (p_chain->size == 0) 194 return 0; 195 196 /* check cluster validation */ 197 if (!is_valid_cluster(sbi, p_chain->dir)) { 198 exfat_err(sb, "invalid start cluster (%u)", p_chain->dir); 199 return -EIO; 200 } 201 202 clu = p_chain->dir; 203 204 cur_cmap_i = next_cmap_i = 205 BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu)); 206 207 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 208 int err; 209 unsigned int last_cluster = p_chain->dir + p_chain->size - 1; 210 211 do { 212 bool sync = false; 213 214 if (clu < last_cluster) 215 next_cmap_i = 216 BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu+1)); 217 218 /* flush bitmap only if index would be changed or for last cluster */ 219 if (clu == last_cluster || cur_cmap_i != next_cmap_i) { 220 sync = true; 221 cur_cmap_i = next_cmap_i; 222 } 223 224 err = exfat_clear_bitmap(sb, clu, (sync && IS_DIRSYNC(inode))); 225 if (err) 226 break; 227 clu++; 228 num_clusters++; 229 } while (num_clusters < p_chain->size); 230 231 if (sbi->options.discard) 232 exfat_discard_cluster(sb, p_chain->dir, p_chain->size); 233 } else { 234 unsigned int nr_clu = 1; 235 236 do { 237 bool sync = false; 238 unsigned int n_clu = clu; 239 int err = exfat_get_next_cluster(sb, &n_clu); 240 241 if (err || n_clu == EXFAT_EOF_CLUSTER) 242 sync = true; 243 else 244 next_cmap_i = 245 BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(n_clu)); 246 247 if (cur_cmap_i != next_cmap_i) { 248 sync = true; 249 cur_cmap_i = next_cmap_i; 250 } 251 252 if (exfat_clear_bitmap(sb, clu, (sync && IS_DIRSYNC(inode)))) 253 break; 254 255 if (sbi->options.discard) { 256 if (n_clu == clu + 1) 257 nr_clu++; 258 else { 259 exfat_discard_cluster(sb, clu - nr_clu + 1, nr_clu); 260 nr_clu = 1; 261 } 262 } 263 264 clu = n_clu; 265 num_clusters++; 266 267 if (err) 268 break; 269 270 if (num_clusters >= sbi->num_clusters - EXFAT_FIRST_CLUSTER) { 271 /* 272 * The cluster chain includes a loop, scan the 273 * bitmap to get the number of used clusters. 274 */ 275 exfat_count_used_clusters(sb, &sbi->used_clusters); 276 277 return 0; 278 } 279 } while (clu != EXFAT_EOF_CLUSTER); 280 } 281 282 sbi->used_clusters -= num_clusters; 283 return 0; 284 } 285 286 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain) 287 { 288 int ret = 0; 289 290 mutex_lock(&EXFAT_SB(inode->i_sb)->bitmap_lock); 291 ret = __exfat_free_cluster(inode, p_chain); 292 mutex_unlock(&EXFAT_SB(inode->i_sb)->bitmap_lock); 293 294 return ret; 295 } 296 297 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain, 298 unsigned int *ret_clu) 299 { 300 struct buffer_head *bh = NULL; 301 unsigned int clu, next; 302 unsigned int count = 0; 303 304 next = p_chain->dir; 305 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 306 *ret_clu = next + p_chain->size - 1; 307 return 0; 308 } 309 310 do { 311 count++; 312 clu = next; 313 if (exfat_ent_get(sb, clu, &next, &bh)) 314 return -EIO; 315 } while (next != EXFAT_EOF_CLUSTER && count <= p_chain->size); 316 317 brelse(bh); 318 if (p_chain->size != count) { 319 exfat_fs_error(sb, 320 "bogus directory size (clus : ondisk(%d) != counted(%d))", 321 p_chain->size, count); 322 return -EIO; 323 } 324 325 *ret_clu = clu; 326 return 0; 327 } 328 329 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu) 330 { 331 struct super_block *sb = dir->i_sb; 332 struct exfat_sb_info *sbi = EXFAT_SB(sb); 333 struct buffer_head *bh; 334 sector_t blknr, last_blknr, i; 335 336 blknr = exfat_cluster_to_sector(sbi, clu); 337 last_blknr = blknr + sbi->sect_per_clus; 338 339 if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) { 340 exfat_fs_error_ratelimit(sb, 341 "%s: out of range(sect:%llu len:%u)", 342 __func__, (unsigned long long)blknr, 343 sbi->sect_per_clus); 344 return -EIO; 345 } 346 347 /* Zeroing the unused blocks on this cluster */ 348 for (i = blknr; i < last_blknr; i++) { 349 bh = sb_getblk(sb, i); 350 if (!bh) 351 return -ENOMEM; 352 353 memset(bh->b_data, 0, sb->s_blocksize); 354 set_buffer_uptodate(bh); 355 mark_buffer_dirty(bh); 356 brelse(bh); 357 } 358 359 if (IS_DIRSYNC(dir)) 360 return sync_blockdev_range(sb->s_bdev, 361 EXFAT_BLK_TO_B(blknr, sb), 362 EXFAT_BLK_TO_B(last_blknr, sb) - 1); 363 364 return 0; 365 } 366 367 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, 368 struct exfat_chain *p_chain, bool sync_bmap) 369 { 370 int ret = -ENOSPC; 371 unsigned int total_cnt; 372 unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER; 373 struct super_block *sb = inode->i_sb; 374 struct exfat_sb_info *sbi = EXFAT_SB(sb); 375 376 total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi); 377 378 if (unlikely(total_cnt < sbi->used_clusters)) { 379 exfat_fs_error_ratelimit(sb, 380 "%s: invalid used clusters(t:%u,u:%u)\n", 381 __func__, total_cnt, sbi->used_clusters); 382 return -EIO; 383 } 384 385 if (num_alloc > total_cnt - sbi->used_clusters) 386 return -ENOSPC; 387 388 mutex_lock(&sbi->bitmap_lock); 389 390 hint_clu = p_chain->dir; 391 /* find new cluster */ 392 if (hint_clu == EXFAT_EOF_CLUSTER) { 393 if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) { 394 exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)", 395 sbi->clu_srch_ptr); 396 sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER; 397 } 398 399 hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr); 400 if (hint_clu == EXFAT_EOF_CLUSTER) { 401 ret = -ENOSPC; 402 goto unlock; 403 } 404 } 405 406 /* check cluster validation */ 407 if (!is_valid_cluster(sbi, hint_clu)) { 408 if (hint_clu != sbi->num_clusters) 409 exfat_err(sb, "hint_cluster is invalid (%u), rewind to the first cluster", 410 hint_clu); 411 hint_clu = EXFAT_FIRST_CLUSTER; 412 p_chain->flags = ALLOC_FAT_CHAIN; 413 } 414 415 p_chain->dir = EXFAT_EOF_CLUSTER; 416 417 while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) != 418 EXFAT_EOF_CLUSTER) { 419 if (new_clu != hint_clu && 420 p_chain->flags == ALLOC_NO_FAT_CHAIN) { 421 if (exfat_chain_cont_cluster(sb, p_chain->dir, 422 p_chain->size)) { 423 ret = -EIO; 424 goto free_cluster; 425 } 426 p_chain->flags = ALLOC_FAT_CHAIN; 427 } 428 429 /* update allocation bitmap */ 430 if (exfat_set_bitmap(sb, new_clu, sync_bmap)) { 431 ret = -EIO; 432 goto free_cluster; 433 } 434 435 /* update FAT table */ 436 if (p_chain->flags == ALLOC_FAT_CHAIN) { 437 if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) { 438 ret = -EIO; 439 goto free_cluster; 440 } 441 } 442 443 if (p_chain->dir == EXFAT_EOF_CLUSTER) { 444 p_chain->dir = new_clu; 445 } else if (p_chain->flags == ALLOC_FAT_CHAIN) { 446 if (exfat_ent_set(sb, last_clu, new_clu)) { 447 ret = -EIO; 448 goto free_cluster; 449 } 450 } 451 p_chain->size++; 452 453 last_clu = new_clu; 454 455 if (p_chain->size == num_alloc) { 456 sbi->clu_srch_ptr = hint_clu; 457 sbi->used_clusters += num_alloc; 458 459 mutex_unlock(&sbi->bitmap_lock); 460 return 0; 461 } 462 463 hint_clu = new_clu + 1; 464 if (hint_clu >= sbi->num_clusters) { 465 hint_clu = EXFAT_FIRST_CLUSTER; 466 467 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 468 if (exfat_chain_cont_cluster(sb, p_chain->dir, 469 p_chain->size)) { 470 ret = -EIO; 471 goto free_cluster; 472 } 473 p_chain->flags = ALLOC_FAT_CHAIN; 474 } 475 } 476 } 477 free_cluster: 478 __exfat_free_cluster(inode, p_chain); 479 unlock: 480 mutex_unlock(&sbi->bitmap_lock); 481 return ret; 482 } 483 484 int exfat_count_num_clusters(struct super_block *sb, 485 struct exfat_chain *p_chain, unsigned int *ret_count) 486 { 487 unsigned int i, count; 488 unsigned int clu; 489 struct exfat_sb_info *sbi = EXFAT_SB(sb); 490 struct buffer_head *bh = NULL; 491 492 if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) { 493 *ret_count = 0; 494 return 0; 495 } 496 497 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) { 498 *ret_count = p_chain->size; 499 return 0; 500 } 501 502 clu = p_chain->dir; 503 count = 0; 504 for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) { 505 count++; 506 if (exfat_ent_get(sb, clu, &clu, &bh)) 507 return -EIO; 508 if (clu == EXFAT_EOF_CLUSTER) 509 break; 510 } 511 512 brelse(bh); 513 *ret_count = count; 514 515 /* 516 * since exfat_count_used_clusters() is not called, sbi->used_clusters 517 * cannot be used here. 518 */ 519 if (unlikely(i == sbi->num_clusters && clu != EXFAT_EOF_CLUSTER)) { 520 exfat_fs_error(sb, "The cluster chain has a loop"); 521 return -EIO; 522 } 523 524 return 0; 525 } 526