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/compat.h> 8 #include <linux/cred.h> 9 #include <linux/buffer_head.h> 10 #include <linux/blkdev.h> 11 #include <linux/fsnotify.h> 12 #include <linux/security.h> 13 #include <linux/msdos_fs.h> 14 #include <linux/writeback.h> 15 16 #include "exfat_raw.h" 17 #include "exfat_fs.h" 18 19 static int exfat_cont_expand(struct inode *inode, loff_t size) 20 { 21 int ret; 22 unsigned int num_clusters, new_num_clusters, last_clu; 23 struct exfat_inode_info *ei = EXFAT_I(inode); 24 struct super_block *sb = inode->i_sb; 25 struct exfat_sb_info *sbi = EXFAT_SB(sb); 26 struct exfat_chain clu; 27 28 ret = inode_newsize_ok(inode, size); 29 if (ret) 30 return ret; 31 32 num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi); 33 new_num_clusters = EXFAT_B_TO_CLU_ROUND_UP(size, sbi); 34 35 if (new_num_clusters == num_clusters) 36 goto out; 37 38 if (num_clusters) { 39 exfat_chain_set(&clu, ei->start_clu, num_clusters, ei->flags); 40 ret = exfat_find_last_cluster(sb, &clu, &last_clu); 41 if (ret) 42 return ret; 43 44 clu.dir = last_clu + 1; 45 } else { 46 last_clu = EXFAT_EOF_CLUSTER; 47 clu.dir = EXFAT_EOF_CLUSTER; 48 } 49 50 clu.size = 0; 51 clu.flags = ei->flags; 52 53 ret = exfat_alloc_cluster(inode, new_num_clusters - num_clusters, 54 &clu, inode_needs_sync(inode)); 55 if (ret) 56 return ret; 57 58 /* Append new clusters to chain */ 59 if (num_clusters) { 60 if (clu.flags != ei->flags) 61 if (exfat_chain_cont_cluster(sb, ei->start_clu, num_clusters)) 62 goto free_clu; 63 64 if (clu.flags == ALLOC_FAT_CHAIN) 65 if (exfat_ent_set(sb, last_clu, clu.dir)) 66 goto free_clu; 67 } else 68 ei->start_clu = clu.dir; 69 70 ei->flags = clu.flags; 71 72 out: 73 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 74 /* Expanded range not zeroed, do not update valid_size */ 75 i_size_write(inode, size); 76 77 inode->i_blocks = round_up(size, sbi->cluster_size) >> 9; 78 mark_inode_dirty(inode); 79 80 if (IS_SYNC(inode)) 81 return write_inode_now(inode, 1); 82 83 return 0; 84 85 free_clu: 86 exfat_free_cluster(inode, &clu); 87 return -EIO; 88 } 89 90 static bool exfat_allow_set_time(struct mnt_idmap *idmap, 91 struct exfat_sb_info *sbi, struct inode *inode) 92 { 93 mode_t allow_utime = sbi->options.allow_utime; 94 95 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), 96 current_fsuid())) { 97 if (vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode))) 98 allow_utime >>= 3; 99 if (allow_utime & MAY_WRITE) 100 return true; 101 } 102 103 /* use a default check */ 104 return false; 105 } 106 107 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi, 108 struct inode *inode, umode_t *mode_ptr) 109 { 110 mode_t i_mode, mask, perm; 111 112 i_mode = inode->i_mode; 113 114 mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ? 115 sbi->options.fs_fmask : sbi->options.fs_dmask; 116 perm = *mode_ptr & ~(S_IFMT | mask); 117 118 /* Of the r and x bits, all (subject to umask) must be present.*/ 119 if ((perm & 0555) != (i_mode & 0555)) 120 return -EPERM; 121 122 if (exfat_mode_can_hold_ro(inode)) { 123 /* 124 * Of the w bits, either all (subject to umask) or none must 125 * be present. 126 */ 127 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask))) 128 return -EPERM; 129 } else { 130 /* 131 * If exfat_mode_can_hold_ro(inode) is false, can't change 132 * w bits. 133 */ 134 if ((perm & 0222) != (0222 & ~mask)) 135 return -EPERM; 136 } 137 138 *mode_ptr &= S_IFMT | perm; 139 140 return 0; 141 } 142 143 /* resize the file length */ 144 int __exfat_truncate(struct inode *inode) 145 { 146 unsigned int num_clusters_new, num_clusters_phys; 147 unsigned int last_clu = EXFAT_FREE_CLUSTER; 148 struct exfat_chain clu; 149 struct super_block *sb = inode->i_sb; 150 struct exfat_sb_info *sbi = EXFAT_SB(sb); 151 struct exfat_inode_info *ei = EXFAT_I(inode); 152 153 /* check if the given file ID is opened */ 154 if (ei->type != TYPE_FILE && ei->type != TYPE_DIR) 155 return -EPERM; 156 157 exfat_set_volume_dirty(sb); 158 159 num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi); 160 num_clusters_phys = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi); 161 162 exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags); 163 164 if (i_size_read(inode) > 0) { 165 /* 166 * Truncate FAT chain num_clusters after the first cluster 167 * num_clusters = min(new, phys); 168 */ 169 unsigned int num_clusters = 170 min(num_clusters_new, num_clusters_phys); 171 172 /* 173 * Follow FAT chain 174 * (defensive coding - works fine even with corrupted FAT table 175 */ 176 if (clu.flags == ALLOC_NO_FAT_CHAIN) { 177 clu.dir += num_clusters; 178 clu.size -= num_clusters; 179 } else { 180 while (num_clusters > 0) { 181 last_clu = clu.dir; 182 if (exfat_get_next_cluster(sb, &(clu.dir))) 183 return -EIO; 184 185 num_clusters--; 186 clu.size--; 187 } 188 } 189 } else { 190 ei->flags = ALLOC_NO_FAT_CHAIN; 191 ei->start_clu = EXFAT_EOF_CLUSTER; 192 } 193 194 if (i_size_read(inode) < ei->valid_size) 195 ei->valid_size = i_size_read(inode); 196 197 if (ei->type == TYPE_FILE) 198 ei->attr |= EXFAT_ATTR_ARCHIVE; 199 200 /* 201 * update the directory entry 202 * 203 * If the directory entry is updated by mark_inode_dirty(), the 204 * directory entry will be written after a writeback cycle of 205 * updating the bitmap/FAT, which may result in clusters being 206 * freed but referenced by the directory entry in the event of a 207 * sudden power failure. 208 * __exfat_write_inode() is called for directory entry, bitmap 209 * and FAT to be written in a same writeback. 210 */ 211 if (__exfat_write_inode(inode, inode_needs_sync(inode))) 212 return -EIO; 213 214 /* cut off from the FAT chain */ 215 if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER && 216 last_clu != EXFAT_EOF_CLUSTER) { 217 if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER)) 218 return -EIO; 219 } 220 221 /* invalidate cache and free the clusters */ 222 /* clear exfat cache */ 223 exfat_cache_inval_inode(inode); 224 225 /* hint information */ 226 ei->hint_bmap.off = EXFAT_EOF_CLUSTER; 227 ei->hint_bmap.clu = EXFAT_EOF_CLUSTER; 228 229 /* hint_stat will be used if this is directory. */ 230 ei->hint_stat.eidx = 0; 231 ei->hint_stat.clu = ei->start_clu; 232 ei->hint_femp.eidx = EXFAT_HINT_NONE; 233 234 /* free the clusters */ 235 if (exfat_free_cluster(inode, &clu)) 236 return -EIO; 237 238 return 0; 239 } 240 241 void exfat_truncate(struct inode *inode) 242 { 243 struct super_block *sb = inode->i_sb; 244 struct exfat_sb_info *sbi = EXFAT_SB(sb); 245 struct exfat_inode_info *ei = EXFAT_I(inode); 246 int err; 247 248 mutex_lock(&sbi->s_lock); 249 if (ei->start_clu == 0) { 250 /* 251 * Empty start_clu != ~0 (not allocated) 252 */ 253 exfat_fs_error(sb, "tried to truncate zeroed cluster."); 254 goto write_size; 255 } 256 257 err = __exfat_truncate(inode); 258 if (err) 259 goto write_size; 260 261 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9; 262 write_size: 263 mutex_unlock(&sbi->s_lock); 264 } 265 266 int exfat_getattr(struct mnt_idmap *idmap, const struct path *path, 267 struct kstat *stat, unsigned int request_mask, 268 unsigned int query_flags) 269 { 270 struct inode *inode = d_backing_inode(path->dentry); 271 struct exfat_inode_info *ei = EXFAT_I(inode); 272 273 generic_fillattr(idmap, request_mask, inode, stat); 274 exfat_truncate_atime(&stat->atime); 275 stat->result_mask |= STATX_BTIME; 276 stat->btime.tv_sec = ei->i_crtime.tv_sec; 277 stat->btime.tv_nsec = ei->i_crtime.tv_nsec; 278 stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size; 279 return 0; 280 } 281 282 int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 283 struct iattr *attr) 284 { 285 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb); 286 struct inode *inode = dentry->d_inode; 287 unsigned int ia_valid; 288 int error; 289 290 if (unlikely(exfat_forced_shutdown(inode->i_sb))) 291 return -EIO; 292 293 if ((attr->ia_valid & ATTR_SIZE) && 294 attr->ia_size > i_size_read(inode)) { 295 error = exfat_cont_expand(inode, attr->ia_size); 296 if (error || attr->ia_valid == ATTR_SIZE) 297 return error; 298 attr->ia_valid &= ~ATTR_SIZE; 299 } 300 301 /* Check for setting the inode time. */ 302 ia_valid = attr->ia_valid; 303 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) && 304 exfat_allow_set_time(idmap, sbi, inode)) { 305 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | 306 ATTR_TIMES_SET); 307 } 308 309 error = setattr_prepare(idmap, dentry, attr); 310 attr->ia_valid = ia_valid; 311 if (error) 312 goto out; 313 314 if (((attr->ia_valid & ATTR_UID) && 315 (!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid), 316 sbi->options.fs_uid))) || 317 ((attr->ia_valid & ATTR_GID) && 318 (!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid), 319 sbi->options.fs_gid))) || 320 ((attr->ia_valid & ATTR_MODE) && 321 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) { 322 error = -EPERM; 323 goto out; 324 } 325 326 /* 327 * We don't return -EPERM here. Yes, strange, but this is too 328 * old behavior. 329 */ 330 if (attr->ia_valid & ATTR_MODE) { 331 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) 332 attr->ia_valid &= ~ATTR_MODE; 333 } 334 335 if (attr->ia_valid & ATTR_SIZE) 336 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 337 338 setattr_copy(idmap, inode, attr); 339 exfat_truncate_inode_atime(inode); 340 341 if (attr->ia_valid & ATTR_SIZE) { 342 error = exfat_block_truncate_page(inode, attr->ia_size); 343 if (error) 344 goto out; 345 346 down_write(&EXFAT_I(inode)->truncate_lock); 347 truncate_setsize(inode, attr->ia_size); 348 349 /* 350 * __exfat_write_inode() is called from exfat_truncate(), inode 351 * is already written by it, so mark_inode_dirty() is unneeded. 352 */ 353 exfat_truncate(inode); 354 up_write(&EXFAT_I(inode)->truncate_lock); 355 } else 356 mark_inode_dirty(inode); 357 358 out: 359 return error; 360 } 361 362 /* 363 * modified ioctls from fat/file.c by Welmer Almesberger 364 */ 365 static int exfat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr) 366 { 367 u32 attr; 368 369 inode_lock_shared(inode); 370 attr = exfat_make_attr(inode); 371 inode_unlock_shared(inode); 372 373 return put_user(attr, user_attr); 374 } 375 376 static int exfat_ioctl_set_attributes(struct file *file, u32 __user *user_attr) 377 { 378 struct inode *inode = file_inode(file); 379 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 380 int is_dir = S_ISDIR(inode->i_mode); 381 u32 attr, oldattr; 382 struct iattr ia; 383 int err; 384 385 err = get_user(attr, user_attr); 386 if (err) 387 goto out; 388 389 err = mnt_want_write_file(file); 390 if (err) 391 goto out; 392 inode_lock(inode); 393 394 oldattr = exfat_make_attr(inode); 395 396 /* 397 * Mask attributes so we don't set reserved fields. 398 */ 399 attr &= (EXFAT_ATTR_READONLY | EXFAT_ATTR_HIDDEN | EXFAT_ATTR_SYSTEM | 400 EXFAT_ATTR_ARCHIVE); 401 attr |= (is_dir ? EXFAT_ATTR_SUBDIR : 0); 402 403 /* Equivalent to a chmod() */ 404 ia.ia_valid = ATTR_MODE | ATTR_CTIME; 405 ia.ia_ctime = current_time(inode); 406 if (is_dir) 407 ia.ia_mode = exfat_make_mode(sbi, attr, 0777); 408 else 409 ia.ia_mode = exfat_make_mode(sbi, attr, 0666 | (inode->i_mode & 0111)); 410 411 /* The root directory has no attributes */ 412 if (inode->i_ino == EXFAT_ROOT_INO && attr != EXFAT_ATTR_SUBDIR) { 413 err = -EINVAL; 414 goto out_unlock_inode; 415 } 416 417 if (((attr | oldattr) & EXFAT_ATTR_SYSTEM) && 418 !capable(CAP_LINUX_IMMUTABLE)) { 419 err = -EPERM; 420 goto out_unlock_inode; 421 } 422 423 /* 424 * The security check is questionable... We single 425 * out the RO attribute for checking by the security 426 * module, just because it maps to a file mode. 427 */ 428 err = security_inode_setattr(file_mnt_idmap(file), 429 file->f_path.dentry, &ia); 430 if (err) 431 goto out_unlock_inode; 432 433 /* This MUST be done before doing anything irreversible... */ 434 err = exfat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia); 435 if (err) 436 goto out_unlock_inode; 437 438 fsnotify_change(file->f_path.dentry, ia.ia_valid); 439 440 exfat_save_attr(inode, attr); 441 mark_inode_dirty(inode); 442 out_unlock_inode: 443 inode_unlock(inode); 444 mnt_drop_write_file(file); 445 out: 446 return err; 447 } 448 449 static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg) 450 { 451 struct fstrim_range range; 452 int ret = 0; 453 454 if (!capable(CAP_SYS_ADMIN)) 455 return -EPERM; 456 457 if (!bdev_max_discard_sectors(inode->i_sb->s_bdev)) 458 return -EOPNOTSUPP; 459 460 if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range))) 461 return -EFAULT; 462 463 range.minlen = max_t(unsigned int, range.minlen, 464 bdev_discard_granularity(inode->i_sb->s_bdev)); 465 466 ret = exfat_trim_fs(inode, &range); 467 if (ret < 0) 468 return ret; 469 470 if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range))) 471 return -EFAULT; 472 473 return 0; 474 } 475 476 static int exfat_ioctl_shutdown(struct super_block *sb, unsigned long arg) 477 { 478 u32 flags; 479 480 if (!capable(CAP_SYS_ADMIN)) 481 return -EPERM; 482 483 if (get_user(flags, (__u32 __user *)arg)) 484 return -EFAULT; 485 486 return exfat_force_shutdown(sb, flags); 487 } 488 489 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 490 { 491 struct inode *inode = file_inode(filp); 492 u32 __user *user_attr = (u32 __user *)arg; 493 494 switch (cmd) { 495 case FAT_IOCTL_GET_ATTRIBUTES: 496 return exfat_ioctl_get_attributes(inode, user_attr); 497 case FAT_IOCTL_SET_ATTRIBUTES: 498 return exfat_ioctl_set_attributes(filp, user_attr); 499 case EXFAT_IOC_SHUTDOWN: 500 return exfat_ioctl_shutdown(inode->i_sb, arg); 501 case FITRIM: 502 return exfat_ioctl_fitrim(inode, arg); 503 default: 504 return -ENOTTY; 505 } 506 } 507 508 #ifdef CONFIG_COMPAT 509 long exfat_compat_ioctl(struct file *filp, unsigned int cmd, 510 unsigned long arg) 511 { 512 return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); 513 } 514 #endif 515 516 int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) 517 { 518 struct inode *inode = filp->f_mapping->host; 519 int err; 520 521 if (unlikely(exfat_forced_shutdown(inode->i_sb))) 522 return -EIO; 523 524 err = __generic_file_fsync(filp, start, end, datasync); 525 if (err) 526 return err; 527 528 err = sync_blockdev(inode->i_sb->s_bdev); 529 if (err) 530 return err; 531 532 return blkdev_issue_flush(inode->i_sb->s_bdev); 533 } 534 535 static int exfat_extend_valid_size(struct file *file, loff_t new_valid_size) 536 { 537 int err; 538 loff_t pos; 539 struct inode *inode = file_inode(file); 540 struct exfat_inode_info *ei = EXFAT_I(inode); 541 struct address_space *mapping = inode->i_mapping; 542 const struct address_space_operations *ops = mapping->a_ops; 543 544 pos = ei->valid_size; 545 while (pos < new_valid_size) { 546 u32 len; 547 struct folio *folio; 548 549 len = PAGE_SIZE - (pos & (PAGE_SIZE - 1)); 550 if (pos + len > new_valid_size) 551 len = new_valid_size - pos; 552 553 err = ops->write_begin(file, mapping, pos, len, &folio, NULL); 554 if (err) 555 goto out; 556 557 err = ops->write_end(file, mapping, pos, len, len, folio, NULL); 558 if (err < 0) 559 goto out; 560 pos += len; 561 562 balance_dirty_pages_ratelimited(mapping); 563 cond_resched(); 564 } 565 566 out: 567 return err; 568 } 569 570 static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) 571 { 572 ssize_t ret; 573 struct file *file = iocb->ki_filp; 574 struct inode *inode = file_inode(file); 575 struct exfat_inode_info *ei = EXFAT_I(inode); 576 loff_t pos = iocb->ki_pos; 577 loff_t valid_size; 578 579 inode_lock(inode); 580 581 valid_size = ei->valid_size; 582 583 ret = generic_write_checks(iocb, iter); 584 if (ret < 0) 585 goto unlock; 586 587 if (pos > valid_size) { 588 ret = exfat_extend_valid_size(file, pos); 589 if (ret < 0 && ret != -ENOSPC) { 590 exfat_err(inode->i_sb, 591 "write: fail to zero from %llu to %llu(%zd)", 592 valid_size, pos, ret); 593 } 594 if (ret < 0) 595 goto unlock; 596 } 597 598 ret = __generic_file_write_iter(iocb, iter); 599 if (ret < 0) 600 goto unlock; 601 602 inode_unlock(inode); 603 604 if (pos > valid_size) 605 pos = valid_size; 606 607 if (iocb_is_dsync(iocb) && iocb->ki_pos > pos) { 608 ssize_t err = vfs_fsync_range(file, pos, iocb->ki_pos - 1, 609 iocb->ki_flags & IOCB_SYNC); 610 if (err < 0) 611 return err; 612 } 613 614 return ret; 615 616 unlock: 617 inode_unlock(inode); 618 619 return ret; 620 } 621 622 static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf) 623 { 624 int err; 625 struct vm_area_struct *vma = vmf->vma; 626 struct file *file = vma->vm_file; 627 struct inode *inode = file_inode(file); 628 struct exfat_inode_info *ei = EXFAT_I(inode); 629 loff_t start, end; 630 631 if (!inode_trylock(inode)) 632 return VM_FAULT_RETRY; 633 634 start = ((loff_t)vma->vm_pgoff << PAGE_SHIFT); 635 end = min_t(loff_t, i_size_read(inode), 636 start + vma->vm_end - vma->vm_start); 637 638 if (ei->valid_size < end) { 639 err = exfat_extend_valid_size(file, end); 640 if (err < 0) { 641 inode_unlock(inode); 642 return vmf_fs_error(err); 643 } 644 } 645 646 inode_unlock(inode); 647 648 return filemap_page_mkwrite(vmf); 649 } 650 651 static const struct vm_operations_struct exfat_file_vm_ops = { 652 .fault = filemap_fault, 653 .map_pages = filemap_map_pages, 654 .page_mkwrite = exfat_page_mkwrite, 655 }; 656 657 static int exfat_file_mmap(struct file *file, struct vm_area_struct *vma) 658 { 659 file_accessed(file); 660 vma->vm_ops = &exfat_file_vm_ops; 661 return 0; 662 } 663 664 const struct file_operations exfat_file_operations = { 665 .llseek = generic_file_llseek, 666 .read_iter = generic_file_read_iter, 667 .write_iter = exfat_file_write_iter, 668 .unlocked_ioctl = exfat_ioctl, 669 #ifdef CONFIG_COMPAT 670 .compat_ioctl = exfat_compat_ioctl, 671 #endif 672 .mmap = exfat_file_mmap, 673 .fsync = exfat_file_fsync, 674 .splice_read = filemap_splice_read, 675 .splice_write = iter_file_splice_write, 676 }; 677 678 const struct inode_operations exfat_file_inode_operations = { 679 .setattr = exfat_setattr, 680 .getattr = exfat_getattr, 681 }; 682