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