1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 */ 5 6 #include <linux/init.h> 7 #include <linux/buffer_head.h> 8 #include <linux/mpage.h> 9 #include <linux/bio.h> 10 #include <linux/blkdev.h> 11 #include <linux/time.h> 12 #include <linux/writeback.h> 13 #include <linux/uio.h> 14 #include <linux/random.h> 15 #include <linux/iversion.h> 16 17 #include "exfat_raw.h" 18 #include "exfat_fs.h" 19 20 static int __exfat_write_inode(struct inode *inode, int sync) 21 { 22 unsigned long long on_disk_size; 23 struct exfat_dentry *ep, *ep2; 24 struct exfat_entry_set_cache *es = NULL; 25 struct super_block *sb = inode->i_sb; 26 struct exfat_sb_info *sbi = EXFAT_SB(sb); 27 struct exfat_inode_info *ei = EXFAT_I(inode); 28 bool is_dir = (ei->type == TYPE_DIR) ? true : false; 29 30 if (inode->i_ino == EXFAT_ROOT_INO) 31 return 0; 32 33 /* 34 * If the indode is already unlinked, there is no need for updating it. 35 */ 36 if (ei->dir.dir == DIR_DELETED) 37 return 0; 38 39 if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1) 40 return 0; 41 42 exfat_set_vol_flags(sb, VOL_DIRTY); 43 44 /* get the directory entry of given file or directory */ 45 es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES); 46 if (!es) 47 return -EIO; 48 ep = exfat_get_dentry_cached(es, 0); 49 ep2 = exfat_get_dentry_cached(es, 1); 50 51 ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode)); 52 53 /* set FILE_INFO structure using the acquired struct exfat_dentry */ 54 exfat_set_entry_time(sbi, &ei->i_crtime, 55 &ep->dentry.file.create_tz, 56 &ep->dentry.file.create_time, 57 &ep->dentry.file.create_date, 58 &ep->dentry.file.create_time_cs); 59 exfat_set_entry_time(sbi, &inode->i_mtime, 60 &ep->dentry.file.modify_tz, 61 &ep->dentry.file.modify_time, 62 &ep->dentry.file.modify_date, 63 &ep->dentry.file.modify_time_cs); 64 exfat_set_entry_time(sbi, &inode->i_atime, 65 &ep->dentry.file.access_tz, 66 &ep->dentry.file.access_time, 67 &ep->dentry.file.access_date, 68 NULL); 69 70 /* File size should be zero if there is no cluster allocated */ 71 on_disk_size = i_size_read(inode); 72 73 if (ei->start_clu == EXFAT_EOF_CLUSTER) 74 on_disk_size = 0; 75 76 ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size); 77 ep2->dentry.stream.size = ep2->dentry.stream.valid_size; 78 79 exfat_update_dir_chksum_with_entry_set(es); 80 exfat_free_dentry_set(es, sync); 81 return 0; 82 } 83 84 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc) 85 { 86 int ret; 87 88 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 89 ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 90 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 91 92 return ret; 93 } 94 95 void exfat_sync_inode(struct inode *inode) 96 { 97 lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock); 98 __exfat_write_inode(inode, 1); 99 } 100 101 /* 102 * Input: inode, (logical) clu_offset, target allocation area 103 * Output: errcode, cluster number 104 * *clu = (~0), if it's unable to allocate a new cluster 105 */ 106 static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, 107 unsigned int *clu, int create) 108 { 109 int ret, modified = false; 110 unsigned int last_clu; 111 struct exfat_chain new_clu; 112 struct super_block *sb = inode->i_sb; 113 struct exfat_sb_info *sbi = EXFAT_SB(sb); 114 struct exfat_inode_info *ei = EXFAT_I(inode); 115 unsigned int local_clu_offset = clu_offset; 116 unsigned int num_to_be_allocated = 0, num_clusters = 0; 117 118 ei->rwoffset = EXFAT_CLU_TO_B(clu_offset, sbi); 119 120 if (EXFAT_I(inode)->i_size_ondisk > 0) 121 num_clusters = 122 EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk, 123 sbi); 124 125 if (clu_offset >= num_clusters) 126 num_to_be_allocated = clu_offset - num_clusters + 1; 127 128 if (!create && (num_to_be_allocated > 0)) { 129 *clu = EXFAT_EOF_CLUSTER; 130 return 0; 131 } 132 133 *clu = last_clu = ei->start_clu; 134 135 if (ei->flags == ALLOC_NO_FAT_CHAIN) { 136 if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) { 137 last_clu += clu_offset - 1; 138 139 if (clu_offset == num_clusters) 140 *clu = EXFAT_EOF_CLUSTER; 141 else 142 *clu += clu_offset; 143 } 144 } else if (ei->type == TYPE_FILE) { 145 unsigned int fclus = 0; 146 int err = exfat_get_cluster(inode, clu_offset, 147 &fclus, clu, &last_clu, 1); 148 if (err) 149 return -EIO; 150 151 clu_offset -= fclus; 152 } else { 153 /* hint information */ 154 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER && 155 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) { 156 clu_offset -= ei->hint_bmap.off; 157 /* hint_bmap.clu should be valid */ 158 WARN_ON(ei->hint_bmap.clu < 2); 159 *clu = ei->hint_bmap.clu; 160 } 161 162 while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) { 163 last_clu = *clu; 164 if (exfat_get_next_cluster(sb, clu)) 165 return -EIO; 166 clu_offset--; 167 } 168 } 169 170 if (*clu == EXFAT_EOF_CLUSTER) { 171 exfat_set_vol_flags(sb, VOL_DIRTY); 172 173 new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ? 174 EXFAT_EOF_CLUSTER : last_clu + 1; 175 new_clu.size = 0; 176 new_clu.flags = ei->flags; 177 178 /* allocate a cluster */ 179 if (num_to_be_allocated < 1) { 180 /* Broken FAT (i_sze > allocated FAT) */ 181 exfat_fs_error(sb, "broken FAT chain."); 182 return -EIO; 183 } 184 185 ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu); 186 if (ret) 187 return ret; 188 189 if (new_clu.dir == EXFAT_EOF_CLUSTER || 190 new_clu.dir == EXFAT_FREE_CLUSTER) { 191 exfat_fs_error(sb, 192 "bogus cluster new allocated (last_clu : %u, new_clu : %u)", 193 last_clu, new_clu.dir); 194 return -EIO; 195 } 196 197 /* append to the FAT chain */ 198 if (last_clu == EXFAT_EOF_CLUSTER) { 199 if (new_clu.flags == ALLOC_FAT_CHAIN) 200 ei->flags = ALLOC_FAT_CHAIN; 201 ei->start_clu = new_clu.dir; 202 modified = true; 203 } else { 204 if (new_clu.flags != ei->flags) { 205 /* no-fat-chain bit is disabled, 206 * so fat-chain should be synced with 207 * alloc-bitmap 208 */ 209 exfat_chain_cont_cluster(sb, ei->start_clu, 210 num_clusters); 211 ei->flags = ALLOC_FAT_CHAIN; 212 modified = true; 213 } 214 if (new_clu.flags == ALLOC_FAT_CHAIN) 215 if (exfat_ent_set(sb, last_clu, new_clu.dir)) 216 return -EIO; 217 } 218 219 num_clusters += num_to_be_allocated; 220 *clu = new_clu.dir; 221 222 if (ei->dir.dir != DIR_DELETED && modified) { 223 struct exfat_dentry *ep; 224 struct exfat_entry_set_cache *es; 225 226 es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, 227 ES_ALL_ENTRIES); 228 if (!es) 229 return -EIO; 230 /* get stream entry */ 231 ep = exfat_get_dentry_cached(es, 1); 232 233 /* update directory entry */ 234 ep->dentry.stream.flags = ei->flags; 235 ep->dentry.stream.start_clu = 236 cpu_to_le32(ei->start_clu); 237 ep->dentry.stream.valid_size = 238 cpu_to_le64(i_size_read(inode)); 239 ep->dentry.stream.size = 240 ep->dentry.stream.valid_size; 241 242 exfat_update_dir_chksum_with_entry_set(es); 243 exfat_free_dentry_set(es, inode_needs_sync(inode)); 244 245 } /* end of if != DIR_DELETED */ 246 247 inode->i_blocks += 248 num_to_be_allocated << sbi->sect_per_clus_bits; 249 250 /* 251 * Move *clu pointer along FAT chains (hole care) because the 252 * caller of this function expect *clu to be the last cluster. 253 * This only works when num_to_be_allocated >= 2, 254 * *clu = (the first cluster of the allocated chain) => 255 * (the last cluster of ...) 256 */ 257 if (ei->flags == ALLOC_NO_FAT_CHAIN) { 258 *clu += num_to_be_allocated - 1; 259 } else { 260 while (num_to_be_allocated > 1) { 261 if (exfat_get_next_cluster(sb, clu)) 262 return -EIO; 263 num_to_be_allocated--; 264 } 265 } 266 267 } 268 269 /* hint information */ 270 ei->hint_bmap.off = local_clu_offset; 271 ei->hint_bmap.clu = *clu; 272 273 return 0; 274 } 275 276 static int exfat_map_new_buffer(struct exfat_inode_info *ei, 277 struct buffer_head *bh, loff_t pos) 278 { 279 if (buffer_delay(bh) && pos > ei->i_size_aligned) 280 return -EIO; 281 set_buffer_new(bh); 282 283 /* 284 * Adjust i_size_aligned if i_size_ondisk is bigger than it. 285 */ 286 if (ei->i_size_ondisk > ei->i_size_aligned) 287 ei->i_size_aligned = ei->i_size_ondisk; 288 return 0; 289 } 290 291 static int exfat_get_block(struct inode *inode, sector_t iblock, 292 struct buffer_head *bh_result, int create) 293 { 294 struct exfat_inode_info *ei = EXFAT_I(inode); 295 struct super_block *sb = inode->i_sb; 296 struct exfat_sb_info *sbi = EXFAT_SB(sb); 297 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits; 298 int err = 0; 299 unsigned long mapped_blocks = 0; 300 unsigned int cluster, sec_offset; 301 sector_t last_block; 302 sector_t phys = 0; 303 loff_t pos; 304 305 mutex_lock(&sbi->s_lock); 306 last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb); 307 if (iblock >= last_block && !create) 308 goto done; 309 310 /* Is this block already allocated? */ 311 err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits, 312 &cluster, create); 313 if (err) { 314 if (err != -ENOSPC) 315 exfat_fs_error_ratelimit(sb, 316 "failed to bmap (inode : %p iblock : %llu, err : %d)", 317 inode, (unsigned long long)iblock, err); 318 goto unlock_ret; 319 } 320 321 if (cluster == EXFAT_EOF_CLUSTER) 322 goto done; 323 324 /* sector offset in cluster */ 325 sec_offset = iblock & (sbi->sect_per_clus - 1); 326 327 phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset; 328 mapped_blocks = sbi->sect_per_clus - sec_offset; 329 max_blocks = min(mapped_blocks, max_blocks); 330 331 /* Treat newly added block / cluster */ 332 if (iblock < last_block) 333 create = 0; 334 335 if (create || buffer_delay(bh_result)) { 336 pos = EXFAT_BLK_TO_B((iblock + 1), sb); 337 if (ei->i_size_ondisk < pos) 338 ei->i_size_ondisk = pos; 339 } 340 341 if (create) { 342 err = exfat_map_new_buffer(ei, bh_result, pos); 343 if (err) { 344 exfat_fs_error(sb, 345 "requested for bmap out of range(pos : (%llu) > i_size_aligned(%llu)\n", 346 pos, ei->i_size_aligned); 347 goto unlock_ret; 348 } 349 } 350 351 if (buffer_delay(bh_result)) 352 clear_buffer_delay(bh_result); 353 map_bh(bh_result, sb, phys); 354 done: 355 bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb); 356 unlock_ret: 357 mutex_unlock(&sbi->s_lock); 358 return err; 359 } 360 361 static int exfat_readpage(struct file *file, struct page *page) 362 { 363 return mpage_readpage(page, exfat_get_block); 364 } 365 366 static void exfat_readahead(struct readahead_control *rac) 367 { 368 mpage_readahead(rac, exfat_get_block); 369 } 370 371 static int exfat_writepage(struct page *page, struct writeback_control *wbc) 372 { 373 return block_write_full_page(page, exfat_get_block, wbc); 374 } 375 376 static int exfat_writepages(struct address_space *mapping, 377 struct writeback_control *wbc) 378 { 379 return mpage_writepages(mapping, wbc, exfat_get_block); 380 } 381 382 static void exfat_write_failed(struct address_space *mapping, loff_t to) 383 { 384 struct inode *inode = mapping->host; 385 386 if (to > i_size_read(inode)) { 387 truncate_pagecache(inode, i_size_read(inode)); 388 exfat_truncate(inode, EXFAT_I(inode)->i_size_aligned); 389 } 390 } 391 392 static int exfat_write_begin(struct file *file, struct address_space *mapping, 393 loff_t pos, unsigned int len, unsigned int flags, 394 struct page **pagep, void **fsdata) 395 { 396 int ret; 397 398 *pagep = NULL; 399 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 400 exfat_get_block, 401 &EXFAT_I(mapping->host)->i_size_ondisk); 402 403 if (ret < 0) 404 exfat_write_failed(mapping, pos+len); 405 406 return ret; 407 } 408 409 static int exfat_write_end(struct file *file, struct address_space *mapping, 410 loff_t pos, unsigned int len, unsigned int copied, 411 struct page *pagep, void *fsdata) 412 { 413 struct inode *inode = mapping->host; 414 struct exfat_inode_info *ei = EXFAT_I(inode); 415 int err; 416 417 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata); 418 419 if (EXFAT_I(inode)->i_size_aligned < i_size_read(inode)) { 420 exfat_fs_error(inode->i_sb, 421 "invalid size(size(%llu) > aligned(%llu)\n", 422 i_size_read(inode), EXFAT_I(inode)->i_size_aligned); 423 return -EIO; 424 } 425 426 if (err < len) 427 exfat_write_failed(mapping, pos+len); 428 429 if (!(err < 0) && !(ei->attr & ATTR_ARCHIVE)) { 430 inode->i_mtime = inode->i_ctime = current_time(inode); 431 ei->attr |= ATTR_ARCHIVE; 432 mark_inode_dirty(inode); 433 } 434 435 return err; 436 } 437 438 static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 439 { 440 struct address_space *mapping = iocb->ki_filp->f_mapping; 441 struct inode *inode = mapping->host; 442 loff_t size = iocb->ki_pos + iov_iter_count(iter); 443 int rw = iov_iter_rw(iter); 444 ssize_t ret; 445 446 if (rw == WRITE) { 447 /* 448 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), 449 * so we need to update the ->i_size_aligned to block boundary. 450 * 451 * But we must fill the remaining area or hole by nul for 452 * updating ->i_size_aligned 453 * 454 * Return 0, and fallback to normal buffered write. 455 */ 456 if (EXFAT_I(inode)->i_size_aligned < size) 457 return 0; 458 } 459 460 /* 461 * Need to use the DIO_LOCKING for avoiding the race 462 * condition of exfat_get_block() and ->truncate(). 463 */ 464 ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block); 465 if (ret < 0 && (rw & WRITE)) 466 exfat_write_failed(mapping, size); 467 return ret; 468 } 469 470 static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block) 471 { 472 sector_t blocknr; 473 474 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */ 475 down_read(&EXFAT_I(mapping->host)->truncate_lock); 476 blocknr = generic_block_bmap(mapping, block, exfat_get_block); 477 up_read(&EXFAT_I(mapping->host)->truncate_lock); 478 return blocknr; 479 } 480 481 /* 482 * exfat_block_truncate_page() zeroes out a mapping from file offset `from' 483 * up to the end of the block which corresponds to `from'. 484 * This is required during truncate to physically zeroout the tail end 485 * of that block so it doesn't yield old data if the file is later grown. 486 * Also, avoid causing failure from fsx for cases of "data past EOF" 487 */ 488 int exfat_block_truncate_page(struct inode *inode, loff_t from) 489 { 490 return block_truncate_page(inode->i_mapping, from, exfat_get_block); 491 } 492 493 static const struct address_space_operations exfat_aops = { 494 .readpage = exfat_readpage, 495 .readahead = exfat_readahead, 496 .writepage = exfat_writepage, 497 .writepages = exfat_writepages, 498 .write_begin = exfat_write_begin, 499 .write_end = exfat_write_end, 500 .direct_IO = exfat_direct_IO, 501 .bmap = exfat_aop_bmap 502 }; 503 504 static inline unsigned long exfat_hash(loff_t i_pos) 505 { 506 return hash_32(i_pos, EXFAT_HASH_BITS); 507 } 508 509 void exfat_hash_inode(struct inode *inode, loff_t i_pos) 510 { 511 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 512 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos); 513 514 spin_lock(&sbi->inode_hash_lock); 515 EXFAT_I(inode)->i_pos = i_pos; 516 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head); 517 spin_unlock(&sbi->inode_hash_lock); 518 } 519 520 void exfat_unhash_inode(struct inode *inode) 521 { 522 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 523 524 spin_lock(&sbi->inode_hash_lock); 525 hlist_del_init(&EXFAT_I(inode)->i_hash_fat); 526 EXFAT_I(inode)->i_pos = 0; 527 spin_unlock(&sbi->inode_hash_lock); 528 } 529 530 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos) 531 { 532 struct exfat_sb_info *sbi = EXFAT_SB(sb); 533 struct exfat_inode_info *info; 534 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos); 535 struct inode *inode = NULL; 536 537 spin_lock(&sbi->inode_hash_lock); 538 hlist_for_each_entry(info, head, i_hash_fat) { 539 WARN_ON(info->vfs_inode.i_sb != sb); 540 541 if (i_pos != info->i_pos) 542 continue; 543 inode = igrab(&info->vfs_inode); 544 if (inode) 545 break; 546 } 547 spin_unlock(&sbi->inode_hash_lock); 548 return inode; 549 } 550 551 /* doesn't deal with root inode */ 552 static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info) 553 { 554 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 555 struct exfat_inode_info *ei = EXFAT_I(inode); 556 loff_t size = info->size; 557 558 memcpy(&ei->dir, &info->dir, sizeof(struct exfat_chain)); 559 ei->entry = info->entry; 560 ei->attr = info->attr; 561 ei->start_clu = info->start_clu; 562 ei->flags = info->flags; 563 ei->type = info->type; 564 565 ei->version = 0; 566 ei->hint_stat.eidx = 0; 567 ei->hint_stat.clu = info->start_clu; 568 ei->hint_femp.eidx = EXFAT_HINT_NONE; 569 ei->rwoffset = 0; 570 ei->hint_bmap.off = EXFAT_EOF_CLUSTER; 571 ei->i_pos = 0; 572 573 inode->i_uid = sbi->options.fs_uid; 574 inode->i_gid = sbi->options.fs_gid; 575 inode_inc_iversion(inode); 576 inode->i_generation = prandom_u32(); 577 578 if (info->attr & ATTR_SUBDIR) { /* directory */ 579 inode->i_generation &= ~1; 580 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777); 581 inode->i_op = &exfat_dir_inode_operations; 582 inode->i_fop = &exfat_dir_operations; 583 set_nlink(inode, info->num_subdirs); 584 } else { /* regular file */ 585 inode->i_generation |= 1; 586 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777); 587 inode->i_op = &exfat_file_inode_operations; 588 inode->i_fop = &exfat_file_operations; 589 inode->i_mapping->a_ops = &exfat_aops; 590 inode->i_mapping->nrpages = 0; 591 } 592 593 i_size_write(inode, size); 594 595 /* ondisk and aligned size should be aligned with block size */ 596 if (size & (inode->i_sb->s_blocksize - 1)) { 597 size |= (inode->i_sb->s_blocksize - 1); 598 size++; 599 } 600 601 ei->i_size_aligned = size; 602 ei->i_size_ondisk = size; 603 604 exfat_save_attr(inode, info->attr); 605 606 inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) & 607 ~(sbi->cluster_size - 1)) >> inode->i_blkbits; 608 inode->i_mtime = info->mtime; 609 inode->i_ctime = info->mtime; 610 ei->i_crtime = info->crtime; 611 inode->i_atime = info->atime; 612 613 exfat_cache_init_inode(inode); 614 615 return 0; 616 } 617 618 struct inode *exfat_build_inode(struct super_block *sb, 619 struct exfat_dir_entry *info, loff_t i_pos) 620 { 621 struct inode *inode; 622 int err; 623 624 inode = exfat_iget(sb, i_pos); 625 if (inode) 626 goto out; 627 inode = new_inode(sb); 628 if (!inode) { 629 inode = ERR_PTR(-ENOMEM); 630 goto out; 631 } 632 inode->i_ino = iunique(sb, EXFAT_ROOT_INO); 633 inode_set_iversion(inode, 1); 634 err = exfat_fill_inode(inode, info); 635 if (err) { 636 iput(inode); 637 inode = ERR_PTR(err); 638 goto out; 639 } 640 exfat_hash_inode(inode, i_pos); 641 insert_inode_hash(inode); 642 out: 643 return inode; 644 } 645 646 void exfat_evict_inode(struct inode *inode) 647 { 648 truncate_inode_pages(&inode->i_data, 0); 649 650 if (!inode->i_nlink) { 651 i_size_write(inode, 0); 652 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 653 __exfat_truncate(inode, 0); 654 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 655 } 656 657 invalidate_inode_buffers(inode); 658 clear_inode(inode); 659 exfat_cache_inval_inode(inode); 660 exfat_unhash_inode(inode); 661 } 662