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