1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. 5 * 6 * Regular file handling primitives for NTFS-based filesystems. 7 * 8 */ 9 10 #include <linux/backing-dev.h> 11 #include <linux/blkdev.h> 12 #include <linux/buffer_head.h> 13 #include <linux/compat.h> 14 #include <linux/falloc.h> 15 #include <linux/fiemap.h> 16 #include <linux/fileattr.h> 17 #include <linux/filelock.h> 18 #include <linux/iomap.h> 19 20 #include "debug.h" 21 #include "ntfs.h" 22 #include "ntfs_fs.h" 23 24 /* 25 * cifx, btrfs, exfat, ext4, f2fs use this constant. 26 * Hope this value will become common to all fs. 27 */ 28 #define NTFS3_IOC_SHUTDOWN _IOR('X', 125, __u32) 29 30 /* 31 * Helper for ntfs_should_use_dio. 32 */ 33 static u32 ntfs_dio_alignment(struct inode *inode) 34 { 35 struct ntfs_inode *ni = ntfs_i(inode); 36 37 if (is_resident(ni)) { 38 /* Check delalloc. */ 39 if (!ni->file.run_da.count) 40 return 0; 41 } 42 43 /* In most cases this is bdev_logical_block_size(bdev). */ 44 return ni->mi.sbi->bdev_blocksize; 45 } 46 47 /* 48 * Returns %true if the given DIO request should be attempted with DIO, or 49 * %false if it should fall back to buffered I/O. 50 */ 51 static bool ntfs_should_use_dio(struct kiocb *iocb, struct iov_iter *iter) 52 { 53 struct inode *inode = file_inode(iocb->ki_filp); 54 u32 dio_align = ntfs_dio_alignment(inode); 55 56 if (!dio_align) 57 return false; 58 59 return IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), dio_align); 60 } 61 62 static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg) 63 { 64 struct fstrim_range __user *user_range; 65 struct fstrim_range range; 66 struct block_device *dev; 67 int err; 68 69 if (!capable(CAP_SYS_ADMIN)) 70 return -EPERM; 71 72 dev = sbi->sb->s_bdev; 73 if (!bdev_max_discard_sectors(dev)) 74 return -EOPNOTSUPP; 75 76 user_range = (struct fstrim_range __user *)arg; 77 if (copy_from_user(&range, user_range, sizeof(range))) 78 return -EFAULT; 79 80 range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev)); 81 82 err = ntfs_trim_fs(sbi, &range); 83 if (err < 0) 84 return err; 85 86 if (copy_to_user(user_range, &range, sizeof(range))) 87 return -EFAULT; 88 89 return 0; 90 } 91 92 /* 93 * ntfs_fileattr_get - inode_operations::fileattr_get 94 */ 95 int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa) 96 { 97 struct inode *inode = d_inode(dentry); 98 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; 99 struct ntfs_inode *ni = ntfs_i(inode); 100 u32 flags = 0; 101 102 /* Avoid any operation if inode is bad. */ 103 if (unlikely(is_bad_ni(ni))) 104 return -EINVAL; 105 106 if (sbi->options->nocase) 107 flags |= FS_CASEFOLD_FL; 108 109 if (inode->i_flags & S_IMMUTABLE) 110 flags |= FS_IMMUTABLE_FL; 111 112 if (inode->i_flags & S_APPEND) 113 flags |= FS_APPEND_FL; 114 115 if (is_compressed(ni)) 116 flags |= FS_COMPR_FL; 117 118 if (is_encrypted(ni)) 119 flags |= FS_ENCRYPT_FL; 120 121 if (ni->nodump) 122 flags |= FS_NODUMP_FL; 123 124 fileattr_fill_flags(fa, flags); 125 126 return 0; 127 } 128 129 /* 130 * ntfs_fileattr_set - inode_operations::fileattr_set 131 */ 132 int ntfs_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry, 133 struct file_kattr *fa) 134 { 135 struct inode *inode = d_inode(dentry); 136 struct ntfs_inode *ni = ntfs_i(inode); 137 u32 flags = fa->flags; 138 unsigned int new_fl = 0; 139 140 /* Avoid any operation if inode is bad. */ 141 if (unlikely(is_bad_ni(ni))) 142 return -EINVAL; 143 144 if (fileattr_has_fsx(fa)) 145 return -EOPNOTSUPP; 146 147 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL)) 148 return -EOPNOTSUPP; 149 150 if (flags & FS_IMMUTABLE_FL) 151 new_fl |= S_IMMUTABLE; 152 153 if (flags & FS_APPEND_FL) 154 new_fl |= S_APPEND; 155 156 inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND); 157 158 /* Save nodump flag to return in ntfs_getattr. */ 159 if (flags & FS_NODUMP_FL) 160 ni->nodump = 1; 161 else 162 ni->nodump = 0; 163 164 inode_set_ctime_current(inode); 165 mark_inode_dirty(inode); 166 167 return 0; 168 } 169 170 static int ntfs_ioctl_get_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf) 171 { 172 if (copy_to_user(buf, sbi->volume.label, FSLABEL_MAX)) 173 return -EFAULT; 174 175 return 0; 176 } 177 178 static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf) 179 { 180 u8 user[FSLABEL_MAX] = { 0 }; 181 int len; 182 183 if (!capable(CAP_SYS_ADMIN)) 184 return -EPERM; 185 186 if (copy_from_user(user, buf, FSLABEL_MAX)) 187 return -EFAULT; 188 189 len = strnlen(user, FSLABEL_MAX); 190 191 return ntfs_set_label(sbi, user, len); 192 } 193 194 /* 195 * ntfs_force_shutdown - helper function. Called from ioctl 196 */ 197 static int ntfs_force_shutdown(struct super_block *sb, u32 flags) 198 { 199 int err; 200 struct ntfs_sb_info *sbi = sb->s_fs_info; 201 202 if (unlikely(ntfs3_forced_shutdown(sb))) 203 return 0; 204 205 /* No additional options yet (flags). */ 206 err = bdev_freeze(sb->s_bdev); 207 if (err) 208 return err; 209 set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &sbi->flags); 210 bdev_thaw(sb->s_bdev); 211 return 0; 212 } 213 214 static int ntfs_ioctl_shutdown(struct super_block *sb, unsigned long arg) 215 { 216 u32 flags; 217 218 if (!capable(CAP_SYS_ADMIN)) 219 return -EPERM; 220 221 if (get_user(flags, (__u32 __user *)arg)) 222 return -EFAULT; 223 224 return ntfs_force_shutdown(sb, flags); 225 } 226 227 /* 228 * ntfs_ioctl - file_operations::unlocked_ioctl 229 */ 230 long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg) 231 { 232 struct inode *inode = file_inode(filp); 233 struct super_block *sb = inode->i_sb; 234 struct ntfs_sb_info *sbi = sb->s_fs_info; 235 236 /* Avoid any operation if inode is bad. */ 237 if (unlikely(is_bad_ni(ntfs_i(inode)))) 238 return -EINVAL; 239 240 switch (cmd) { 241 case FITRIM: 242 return ntfs_ioctl_fitrim(sbi, arg); 243 case FS_IOC_GETFSLABEL: 244 return ntfs_ioctl_get_volume_label(sbi, (u8 __user *)arg); 245 case FS_IOC_SETFSLABEL: 246 return ntfs_ioctl_set_volume_label(sbi, (u8 __user *)arg); 247 case NTFS3_IOC_SHUTDOWN: 248 return ntfs_ioctl_shutdown(sb, arg); 249 } 250 return -ENOTTY; /* Inappropriate ioctl for device. */ 251 } 252 253 #ifdef CONFIG_COMPAT 254 long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg) 255 256 { 257 return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); 258 } 259 #endif 260 261 /* 262 * ntfs_getattr - inode_operations::getattr 263 */ 264 int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path, 265 struct kstat *stat, u32 request_mask, u32 flags) 266 { 267 struct inode *inode = d_inode(path->dentry); 268 struct ntfs_inode *ni = ntfs_i(inode); 269 270 /* Avoid any operation if inode is bad. */ 271 if (unlikely(is_bad_ni(ni))) 272 return -EINVAL; 273 274 stat->result_mask |= STATX_BTIME; 275 stat->btime = ni->i_crtime; 276 stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */ 277 278 if (inode->i_flags & S_IMMUTABLE) 279 stat->attributes |= STATX_ATTR_IMMUTABLE; 280 281 if (inode->i_flags & S_APPEND) 282 stat->attributes |= STATX_ATTR_APPEND; 283 284 if (ni->nodump) 285 stat->attributes |= STATX_ATTR_NODUMP; 286 287 if (is_compressed(ni)) 288 stat->attributes |= STATX_ATTR_COMPRESSED; 289 290 if (is_encrypted(ni)) 291 stat->attributes |= STATX_ATTR_ENCRYPTED; 292 293 stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED | 294 STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND; 295 296 generic_fillattr(idmap, request_mask, inode, stat); 297 298 return 0; 299 } 300 301 static int ntfs_extend_initialized_size(struct file *file, 302 struct ntfs_inode *ni, 303 const loff_t new_valid) 304 { 305 struct inode *inode = &ni->vfs_inode; 306 const loff_t valid = ni->i_valid; 307 int err; 308 309 if (valid >= new_valid) 310 return 0; 311 312 if (is_resident(ni)) { 313 ni->i_valid = new_valid; 314 return 0; 315 } 316 317 err = iomap_zero_range(inode, valid, new_valid - valid, NULL, 318 &ntfs_iomap_ops, &ntfs_iomap_folio_ops, NULL); 319 if (err) { 320 ni->i_valid = valid; 321 ntfs_inode_warn(inode, 322 "failed to extend initialized size to %llx.", 323 new_valid); 324 return err; 325 } 326 327 return 0; 328 } 329 330 /* Zero pagecache after 'from'. */ 331 static void ntfs_zero_tail(struct address_space *mapping, loff_t from) 332 { 333 struct folio_batch fbatch; 334 pgoff_t index = from >> PAGE_SHIFT; 335 unsigned nr, i; 336 337 folio_batch_init(&fbatch); 338 339 nr = filemap_get_folios(mapping, &index, -1, &fbatch); 340 341 for (i = 0; i < nr; i++) { 342 struct folio *folio = fbatch.folios[i]; 343 u32 st = folio_pos(folio) < from ? 344 offset_in_folio(folio, from) : 345 0; 346 347 folio_lock(folio); 348 folio_zero_segment(folio, st, folio_size(folio)); 349 350 folio_unlock(folio); 351 } 352 folio_batch_release(&fbatch); 353 } 354 355 static void ntfs_filemap_close(struct vm_area_struct *vma) 356 { 357 struct inode *inode = file_inode(vma->vm_file); 358 struct ntfs_inode *ni = ntfs_i(inode); 359 u64 i_size = i_size_read(inode); 360 u64 from = (u64)vma->vm_pgoff << PAGE_SHIFT; 361 u64 to = min(i_size, from + vma->vm_end - vma->vm_start); 362 363 if (ni->i_valid < to) { 364 ni->i_valid = to; 365 mark_inode_dirty(inode); 366 } 367 ntfs_zero_tail(inode->i_mapping, ni->i_valid); 368 } 369 370 /* Copy of generic_file_vm_ops. */ 371 static const struct vm_operations_struct ntfs_file_vm_ops = { 372 .close = ntfs_filemap_close, 373 .fault = filemap_fault, 374 .map_pages = filemap_map_pages, 375 .page_mkwrite = filemap_page_mkwrite, 376 }; 377 378 /* 379 * ntfs_file_mmap_prepare - file_operations::mmap_prepare 380 */ 381 static int ntfs_file_mmap_prepare(struct vm_area_desc *desc) 382 { 383 struct file *file = desc->file; 384 struct inode *inode = file_inode(file); 385 struct ntfs_inode *ni = ntfs_i(inode); 386 const bool rw = vma_desc_test(desc, VMA_WRITE_BIT); 387 int err; 388 389 /* Avoid any operation if inode is bad. */ 390 if (unlikely(is_bad_ni(ni))) 391 return -EINVAL; 392 393 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 394 return -EIO; 395 396 if (is_encrypted(ni)) { 397 ntfs_inode_warn(inode, "mmap encrypted not supported"); 398 return -EOPNOTSUPP; 399 } 400 401 if (is_dedup(ni)) { 402 ntfs_inode_warn(inode, "mmap deduplicated not supported"); 403 return -EOPNOTSUPP; 404 } 405 406 if (is_compressed(ni)) { 407 if (rw) { 408 ntfs_inode_warn(inode, 409 "mmap(write) compressed not supported"); 410 return -EOPNOTSUPP; 411 } 412 /* Turn off readahead for compressed files. */ 413 file->f_ra.ra_pages = 0; 414 } 415 416 if (rw) { 417 u64 from = (u64)desc->pgoff << PAGE_SHIFT; 418 u64 to = min_t(u64, i_size_read(inode), 419 from + vma_desc_size(desc)); 420 421 if (is_sparsed(ni)) { 422 /* Allocate clusters for rw map. */ 423 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; 424 CLST lcn, len; 425 CLST vcn = from >> sbi->cluster_bits; 426 CLST end = bytes_to_cluster(sbi, to); 427 bool new; 428 429 for (; vcn < end; vcn += len) { 430 err = attr_data_get_block(ni, vcn, 1, &lcn, 431 &len, &new, true, 432 NULL, false); 433 if (err) 434 goto out; 435 } 436 } 437 438 if (ni->i_valid < to) { 439 if (!inode_trylock(inode)) { 440 err = -EAGAIN; 441 goto out; 442 } 443 err = ntfs_extend_initialized_size(file, ni, to); 444 inode_unlock(inode); 445 if (err) 446 goto out; 447 } 448 } 449 450 err = generic_file_mmap_prepare(desc); 451 if (!err && rw) 452 desc->vm_ops = &ntfs_file_vm_ops; 453 out: 454 return err; 455 } 456 457 /* 458 * ntfs_fallocate - file_operations::ntfs_fallocate 459 * 460 * Preallocate space for a file. This implements ntfs's fallocate file 461 * operation, which gets called from sys_fallocate system call. User 462 * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set 463 * we just allocate clusters without zeroing them out. Otherwise we 464 * allocate and zero out clusters via an expanding truncate. 465 */ 466 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len) 467 { 468 struct inode *inode = file_inode(file); 469 struct address_space *mapping = inode->i_mapping; 470 struct super_block *sb = inode->i_sb; 471 struct ntfs_sb_info *sbi = sb->s_fs_info; 472 struct ntfs_inode *ni = ntfs_i(inode); 473 loff_t end = vbo + len; 474 loff_t vbo_down = round_down(vbo, max_t(unsigned long, 475 sbi->cluster_size, PAGE_SIZE)); 476 bool is_supported_holes = is_sparsed(ni) || is_compressed(ni); 477 loff_t i_size, new_size; 478 bool map_locked; 479 int err; 480 481 /* No support for dir. */ 482 if (!S_ISREG(inode->i_mode)) 483 return -EOPNOTSUPP; 484 485 /* 486 * vfs_fallocate checks all possible combinations of mode. 487 * Do additional checks here before ntfs_set_state(dirty). 488 */ 489 if (mode & FALLOC_FL_PUNCH_HOLE) { 490 if (!is_supported_holes) 491 return -EOPNOTSUPP; 492 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { 493 } else if (mode & FALLOC_FL_INSERT_RANGE) { 494 if (!is_supported_holes) 495 return -EOPNOTSUPP; 496 } else if (mode & 497 ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | 498 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) { 499 ntfs_inode_warn(inode, "fallocate(0x%x) is not supported", 500 mode); 501 return -EOPNOTSUPP; 502 } 503 504 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 505 506 inode_lock(inode); 507 i_size = inode->i_size; 508 new_size = max(end, i_size); 509 map_locked = false; 510 511 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { 512 /* Should never be here, see ntfs_file_open. */ 513 err = -EOPNOTSUPP; 514 goto out; 515 } 516 517 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE | 518 FALLOC_FL_INSERT_RANGE)) { 519 inode_dio_wait(inode); 520 filemap_invalidate_lock(mapping); 521 map_locked = true; 522 } 523 524 if (mode & FALLOC_FL_PUNCH_HOLE) { 525 u32 frame_size; 526 loff_t mask, vbo_a, end_a, tmp, from; 527 528 err = filemap_write_and_wait_range(mapping, vbo_down, 529 LLONG_MAX); 530 if (err) 531 goto out; 532 533 truncate_pagecache(inode, vbo_down); 534 535 ni_lock(ni); 536 err = attr_punch_hole(ni, vbo, len, &frame_size); 537 ni_unlock(ni); 538 if (!err) 539 goto ok; 540 541 if (err != E_NTFS_NOTALIGNED) 542 goto out; 543 544 /* Process not aligned punch. */ 545 err = 0; 546 if (end > i_size) 547 end = i_size; 548 mask = frame_size - 1; 549 vbo_a = (vbo + mask) & ~mask; 550 end_a = end & ~mask; 551 552 tmp = min(vbo_a, end); 553 from = min_t(loff_t, ni->i_valid, vbo); 554 /* Zero head of punch. */ 555 if (tmp > from) { 556 err = iomap_zero_range(inode, from, tmp - from, NULL, 557 &ntfs_iomap_ops, 558 &ntfs_iomap_folio_ops, NULL); 559 if (err) 560 goto out; 561 } 562 563 /* Aligned punch_hole. Deallocate clusters. */ 564 if (end_a > vbo_a) { 565 ni_lock(ni); 566 err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL); 567 ni_unlock(ni); 568 if (err) 569 goto out; 570 } 571 572 /* Zero tail of punch. */ 573 if (vbo < end_a && end_a < end) { 574 err = iomap_zero_range(inode, end_a, end - end_a, NULL, 575 &ntfs_iomap_ops, 576 &ntfs_iomap_folio_ops, NULL); 577 if (err) 578 goto out; 579 } 580 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { 581 /* 582 * Write tail of the last page before removed range since 583 * it will get removed from the page cache below. 584 */ 585 err = filemap_write_and_wait_range(mapping, vbo_down, vbo); 586 if (err) 587 goto out; 588 589 /* 590 * Write data that will be shifted to preserve them 591 * when discarding page cache below. 592 */ 593 err = filemap_write_and_wait_range(mapping, end, LLONG_MAX); 594 if (err) 595 goto out; 596 597 truncate_pagecache(inode, vbo_down); 598 599 ni_lock(ni); 600 err = attr_collapse_range(ni, vbo, len); 601 ni_unlock(ni); 602 if (err) 603 goto out; 604 } else if (mode & FALLOC_FL_INSERT_RANGE) { 605 /* Check new size. */ 606 err = inode_newsize_ok(inode, new_size); 607 if (err) 608 goto out; 609 610 /* Write out all dirty pages. */ 611 err = filemap_write_and_wait_range(mapping, vbo_down, 612 LLONG_MAX); 613 if (err) 614 goto out; 615 truncate_pagecache(inode, vbo_down); 616 617 ni_lock(ni); 618 err = attr_insert_range(ni, vbo, len); 619 ni_unlock(ni); 620 if (err) 621 goto out; 622 } else { 623 /* Check new size. */ 624 u8 cluster_bits = sbi->cluster_bits; 625 626 /* Be sure file is non resident. */ 627 if (is_resident(ni)) { 628 ni_lock(ni); 629 err = attr_force_nonresident(ni); 630 ni_unlock(ni); 631 if (err) 632 goto out; 633 } 634 635 /* generic/213: expected -ENOSPC instead of -EFBIG. */ 636 if (!is_supported_holes) { 637 loff_t to_alloc = new_size - inode_get_bytes(inode); 638 639 if (to_alloc > 0 && 640 (to_alloc >> cluster_bits) > 641 wnd_zeroes(&sbi->used.bitmap)) { 642 err = -ENOSPC; 643 goto out; 644 } 645 } 646 647 err = inode_newsize_ok(inode, new_size); 648 if (err) 649 goto out; 650 651 if (new_size > i_size) { 652 /* 653 * Allocate clusters, do not change 'valid' size. 654 */ 655 err = ntfs_set_size(inode, new_size); 656 if (err) 657 goto out; 658 } 659 660 if (is_supported_holes) { 661 CLST vcn = vbo >> cluster_bits; 662 CLST cend = bytes_to_cluster(sbi, end); 663 CLST lcn, clen; 664 bool new; 665 666 /* 667 * Allocate and zero new clusters. 668 */ 669 for (; vcn < cend; vcn += clen) { 670 err = attr_data_get_block(ni, vcn, cend - vcn, 671 &lcn, &clen, &new, 672 true, NULL, false); 673 if (err) 674 goto out; 675 } 676 } 677 678 if (mode & FALLOC_FL_KEEP_SIZE) { 679 err = ntfs_set_size(inode, i_size); 680 if (err) 681 goto out; 682 } 683 } 684 685 ok: 686 err = file_modified(file); 687 if (err) 688 goto out; 689 690 out: 691 if (map_locked) 692 filemap_invalidate_unlock(mapping); 693 694 if (!err) { 695 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 696 mark_inode_dirty(inode); 697 } 698 699 inode_unlock(inode); 700 return err; 701 } 702 703 /* 704 * ntfs_setattr - inode_operations::setattr 705 */ 706 int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 707 struct iattr *attr) 708 { 709 struct inode *inode = d_inode(dentry); 710 struct ntfs_inode *ni = ntfs_i(inode); 711 u32 ia_valid = attr->ia_valid; 712 umode_t mode = inode->i_mode; 713 int err; 714 715 /* Avoid any operation if inode is bad. */ 716 if (unlikely(is_bad_ni(ni))) 717 return -EINVAL; 718 719 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 720 return -EIO; 721 722 err = setattr_prepare(idmap, dentry, attr); 723 if (err) 724 goto out; 725 726 if (ia_valid & ATTR_SIZE) { 727 loff_t newsize, oldsize; 728 729 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { 730 /* Should never be here, see ntfs_file_open(). */ 731 err = -EOPNOTSUPP; 732 goto out; 733 } 734 inode_dio_wait(inode); 735 oldsize = i_size_read(inode); 736 newsize = attr->ia_size; 737 738 if (newsize != oldsize) { 739 truncate_setsize(inode, newsize); 740 741 err = ntfs_set_size(inode, newsize); 742 if (err) { 743 i_size_write(inode, oldsize); 744 goto out; 745 } 746 747 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE; 748 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 749 inode_set_mtime_to_ts(inode, 750 inode_set_ctime_current(inode)); 751 } 752 } 753 754 setattr_copy(idmap, inode, attr); 755 756 if (mode != inode->i_mode) { 757 err = ntfs_acl_chmod(idmap, dentry); 758 if (err) 759 goto out; 760 761 /* Linux 'w' -> Windows 'ro'. */ 762 if (0222 & inode->i_mode) 763 ni->std_fa &= ~FILE_ATTRIBUTE_READONLY; 764 else 765 ni->std_fa |= FILE_ATTRIBUTE_READONLY; 766 } 767 768 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) 769 ntfs_save_wsl_perm(inode, NULL); 770 mark_inode_dirty(inode); 771 out: 772 return err; 773 } 774 775 /* 776 * check_read_restriction: 777 * common code for ntfs_file_read_iter and ntfs_file_splice_read 778 */ 779 static int check_read_restriction(struct inode *inode) 780 { 781 struct ntfs_inode *ni = ntfs_i(inode); 782 783 /* Avoid any operation if inode is bad. */ 784 if (unlikely(is_bad_ni(ni))) 785 return -EINVAL; 786 787 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 788 return -EIO; 789 790 if (is_encrypted(ni)) { 791 ntfs_inode_warn(inode, "encrypted i/o not supported"); 792 return -EOPNOTSUPP; 793 } 794 795 #ifndef CONFIG_NTFS3_LZX_XPRESS 796 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) { 797 ntfs_inode_warn( 798 inode, 799 "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files"); 800 return -EOPNOTSUPP; 801 } 802 #endif 803 804 if (is_dedup(ni)) { 805 ntfs_inode_warn(inode, "read deduplicated not supported"); 806 return -EOPNOTSUPP; 807 } 808 809 return 0; 810 } 811 812 /* 813 * ntfs_file_read_iter - file_operations::read_iter 814 */ 815 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) 816 { 817 struct file *file = iocb->ki_filp; 818 struct inode *inode = file_inode(file); 819 struct ntfs_inode *ni = ntfs_i(inode); 820 size_t bytes = iov_iter_count(iter); 821 loff_t valid, i_size, vbo, end; 822 unsigned int dio_flags; 823 ssize_t err; 824 825 err = check_read_restriction(inode); 826 if (err) 827 return err; 828 829 if (!bytes) 830 return 0; /* skip atime */ 831 832 if (is_compressed(ni)) { 833 if (iocb->ki_flags & IOCB_DIRECT) { 834 ntfs_inode_warn( 835 inode, "direct i/o + compressed not supported"); 836 return -EOPNOTSUPP; 837 } 838 /* Turn off readahead for compressed files. */ 839 file->f_ra.ra_pages = 0; 840 } 841 842 /* Fallback to buffered I/O if the inode does not support direct I/O. */ 843 if (!(iocb->ki_flags & IOCB_DIRECT) || 844 !ntfs_should_use_dio(iocb, iter)) { 845 iocb->ki_flags &= ~IOCB_DIRECT; 846 return generic_file_read_iter(iocb, iter); 847 } 848 849 if (iocb->ki_flags & IOCB_NOWAIT) { 850 if (!inode_trylock_shared(inode)) 851 return -EAGAIN; 852 } else { 853 inode_lock_shared(inode); 854 } 855 856 vbo = iocb->ki_pos; 857 end = vbo + bytes; 858 dio_flags = 0; 859 valid = ni->i_valid; 860 i_size = inode->i_size; 861 862 if (vbo < valid) { 863 if (valid < end) { 864 /* read cross 'valid' size. */ 865 dio_flags |= IOMAP_DIO_FORCE_WAIT; 866 } 867 868 if (ni->file.run_da.count) { 869 /* Direct I/O is not compatible with delalloc. */ 870 err = ni_allocate_da_blocks(ni); 871 if (err) 872 goto out; 873 } 874 875 err = iomap_dio_rw(iocb, iter, &ntfs_iomap_ops, NULL, dio_flags, 876 NULL, 0); 877 878 if (err <= 0) 879 goto out; 880 end = vbo + err; 881 if (valid < end) { 882 size_t to_zero = end - valid; 883 /* Fix iter. */ 884 iov_iter_revert(iter, to_zero); 885 iov_iter_zero(to_zero, iter); 886 } 887 } else if (vbo < i_size) { 888 if (end > i_size) 889 bytes = i_size - vbo; 890 iov_iter_zero(bytes, iter); 891 iocb->ki_pos += bytes; 892 err = bytes; 893 } 894 895 out: 896 inode_unlock_shared(inode); 897 file_accessed(iocb->ki_filp); 898 return err; 899 } 900 901 /* 902 * ntfs_file_splice_read - file_operations::splice_read 903 */ 904 static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos, 905 struct pipe_inode_info *pipe, size_t len, 906 unsigned int flags) 907 { 908 struct inode *inode = file_inode(in); 909 ssize_t err; 910 911 err = check_read_restriction(inode); 912 if (err) 913 return err; 914 915 if (is_compressed(ntfs_i(inode))) { 916 /* Turn off readahead for compressed files. */ 917 in->f_ra.ra_pages = 0; 918 } 919 920 return filemap_splice_read(in, ppos, pipe, len, flags); 921 } 922 923 /* 924 * ntfs_get_frame_pages 925 * 926 * Return: Array of locked pages. 927 */ 928 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index, 929 struct page **pages, u32 pages_per_frame, 930 bool *frame_uptodate) 931 { 932 gfp_t gfp_mask = mapping_gfp_mask(mapping); 933 u32 npages; 934 935 *frame_uptodate = true; 936 937 for (npages = 0; npages < pages_per_frame; npages++, index++) { 938 struct folio *folio; 939 940 folio = __filemap_get_folio(mapping, index, 941 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, 942 gfp_mask | __GFP_ZERO); 943 if (IS_ERR(folio)) { 944 while (npages--) { 945 folio = page_folio(pages[npages]); 946 folio_unlock(folio); 947 folio_put(folio); 948 } 949 950 return -ENOMEM; 951 } 952 953 if (!folio_test_uptodate(folio)) 954 *frame_uptodate = false; 955 956 pages[npages] = &folio->page; 957 } 958 959 return 0; 960 } 961 962 /* 963 * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files). 964 */ 965 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from) 966 { 967 int err; 968 struct file *file = iocb->ki_filp; 969 size_t count = iov_iter_count(from); 970 loff_t pos = iocb->ki_pos; 971 struct inode *inode = file_inode(file); 972 loff_t i_size = i_size_read(inode); 973 struct address_space *mapping = inode->i_mapping; 974 struct ntfs_inode *ni = ntfs_i(inode); 975 u64 valid = ni->i_valid; 976 struct ntfs_sb_info *sbi = ni->mi.sbi; 977 struct page **pages = NULL; 978 struct folio *folio; 979 size_t written = 0; 980 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits; 981 u32 frame_size = 1u << frame_bits; 982 u32 pages_per_frame = frame_size >> PAGE_SHIFT; 983 u32 ip, off; 984 CLST frame; 985 u64 frame_vbo; 986 pgoff_t index; 987 bool frame_uptodate; 988 989 if (frame_size < PAGE_SIZE) { 990 /* 991 * frame_size == 8K if cluster 512 992 * frame_size == 64K if cluster 4096 993 */ 994 ntfs_inode_warn(inode, "page size is bigger than frame size"); 995 return -EOPNOTSUPP; 996 } 997 998 pages = kmalloc_objs(struct page *, pages_per_frame, GFP_NOFS); 999 if (!pages) 1000 return -ENOMEM; 1001 1002 err = file_remove_privs(file); 1003 if (err) 1004 goto out; 1005 1006 err = file_update_time(file); 1007 if (err) 1008 goto out; 1009 1010 /* Zero range [valid : pos). */ 1011 while (valid < pos) { 1012 CLST lcn, clen; 1013 1014 frame = valid >> frame_bits; 1015 frame_vbo = valid & ~(u64)(frame_size - 1); 1016 off = valid & (frame_size - 1); 1017 1018 err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn, 1019 &clen, NULL, false, NULL, false); 1020 if (err) 1021 goto out; 1022 1023 if (lcn == SPARSE_LCN) { 1024 ni->i_valid = valid = 1025 frame_vbo + ((u64)clen << sbi->cluster_bits); 1026 continue; 1027 } 1028 1029 /* Load full frame. */ 1030 err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT, 1031 pages, pages_per_frame, 1032 &frame_uptodate); 1033 if (err) 1034 goto out; 1035 1036 if (!frame_uptodate && off) { 1037 err = ni_read_frame(ni, frame_vbo, pages, 1038 pages_per_frame, 0); 1039 if (err) { 1040 for (ip = 0; ip < pages_per_frame; ip++) { 1041 folio = page_folio(pages[ip]); 1042 folio_unlock(folio); 1043 folio_put(folio); 1044 } 1045 goto out; 1046 } 1047 } 1048 1049 ip = off >> PAGE_SHIFT; 1050 off = offset_in_page(valid); 1051 for (; ip < pages_per_frame; ip++, off = 0) { 1052 folio = page_folio(pages[ip]); 1053 folio_zero_segment(folio, off, PAGE_SIZE); 1054 flush_dcache_folio(folio); 1055 folio_mark_uptodate(folio); 1056 } 1057 1058 ni_lock(ni); 1059 err = ni_write_frame(ni, pages, pages_per_frame); 1060 ni_unlock(ni); 1061 1062 for (ip = 0; ip < pages_per_frame; ip++) { 1063 folio = page_folio(pages[ip]); 1064 folio_mark_uptodate(folio); 1065 folio_unlock(folio); 1066 folio_put(folio); 1067 } 1068 1069 if (err) 1070 goto out; 1071 1072 ni->i_valid = valid = frame_vbo + frame_size; 1073 } 1074 1075 /* Copy user data [pos : pos + count). */ 1076 while (count) { 1077 size_t copied, bytes; 1078 1079 off = pos & (frame_size - 1); 1080 bytes = frame_size - off; 1081 if (bytes > count) 1082 bytes = count; 1083 1084 frame_vbo = pos & ~(u64)(frame_size - 1); 1085 index = frame_vbo >> PAGE_SHIFT; 1086 1087 if (unlikely(fault_in_iov_iter_readable(from, bytes))) { 1088 err = -EFAULT; 1089 goto out; 1090 } 1091 1092 /* Load full frame. */ 1093 err = ntfs_get_frame_pages(mapping, index, pages, 1094 pages_per_frame, &frame_uptodate); 1095 if (err) 1096 goto out; 1097 1098 if (!frame_uptodate) { 1099 loff_t to = pos + bytes; 1100 1101 if (off || (to < i_size && (to & (frame_size - 1)))) { 1102 err = ni_read_frame(ni, frame_vbo, pages, 1103 pages_per_frame, 0); 1104 if (err) { 1105 for (ip = 0; ip < pages_per_frame; 1106 ip++) { 1107 folio = page_folio(pages[ip]); 1108 folio_unlock(folio); 1109 folio_put(folio); 1110 } 1111 goto out; 1112 } 1113 } 1114 } 1115 1116 WARN_ON(!bytes); 1117 copied = 0; 1118 ip = off >> PAGE_SHIFT; 1119 off = offset_in_page(pos); 1120 1121 /* Copy user data to pages. */ 1122 for (;;) { 1123 size_t cp, tail = PAGE_SIZE - off; 1124 1125 folio = page_folio(pages[ip]); 1126 cp = copy_folio_from_iter_atomic( 1127 folio, off, min(tail, bytes), from); 1128 flush_dcache_folio(folio); 1129 1130 copied += cp; 1131 bytes -= cp; 1132 if (!bytes || !cp) 1133 break; 1134 1135 if (cp < tail) { 1136 off += cp; 1137 } else { 1138 ip++; 1139 off = 0; 1140 } 1141 } 1142 1143 ni_lock(ni); 1144 err = ni_write_frame(ni, pages, pages_per_frame); 1145 ni_unlock(ni); 1146 1147 for (ip = 0; ip < pages_per_frame; ip++) { 1148 folio = page_folio(pages[ip]); 1149 folio_clear_dirty(folio); 1150 folio_mark_uptodate(folio); 1151 folio_unlock(folio); 1152 folio_put(folio); 1153 } 1154 1155 if (err) 1156 goto out; 1157 1158 /* 1159 * We can loop for a long time in here. Be nice and allow 1160 * us to schedule out to avoid softlocking if preempt 1161 * is disabled. 1162 */ 1163 cond_resched(); 1164 1165 pos += copied; 1166 written += copied; 1167 1168 count = iov_iter_count(from); 1169 } 1170 1171 out: 1172 kfree(pages); 1173 1174 if (err < 0) 1175 return err; 1176 1177 iocb->ki_pos += written; 1178 if (iocb->ki_pos > ni->i_valid) 1179 ni->i_valid = iocb->ki_pos; 1180 if (iocb->ki_pos > i_size) 1181 i_size_write(inode, iocb->ki_pos); 1182 1183 return written; 1184 } 1185 1186 /* 1187 * check_write_restriction: 1188 * common code for ntfs_file_write_iter and ntfs_file_splice_write 1189 */ 1190 static int check_write_restriction(struct inode *inode) 1191 { 1192 struct ntfs_inode *ni = ntfs_i(inode); 1193 1194 /* Avoid any operation if inode is bad. */ 1195 if (unlikely(is_bad_ni(ni))) 1196 return -EINVAL; 1197 1198 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1199 return -EIO; 1200 1201 if (is_encrypted(ni)) { 1202 ntfs_inode_warn(inode, "encrypted i/o not supported"); 1203 return -EOPNOTSUPP; 1204 } 1205 1206 if (is_dedup(ni)) { 1207 ntfs_inode_warn(inode, "write into deduplicated not supported"); 1208 return -EOPNOTSUPP; 1209 } 1210 1211 if (unlikely(IS_IMMUTABLE(inode))) 1212 return -EPERM; 1213 1214 return 0; 1215 } 1216 1217 /* 1218 * ntfs_file_write_iter - file_operations::write_iter 1219 */ 1220 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 1221 { 1222 struct file *file = iocb->ki_filp; 1223 struct inode *inode = file_inode(file); 1224 struct ntfs_inode *ni = ntfs_i(inode); 1225 loff_t vbo, endbyte; 1226 ssize_t ret, err; 1227 1228 if (!inode_trylock(inode)) { 1229 if (iocb->ki_flags & IOCB_NOWAIT) 1230 return -EAGAIN; 1231 inode_lock(inode); 1232 } 1233 1234 ret = check_write_restriction(inode); 1235 if (ret) 1236 goto out; 1237 1238 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) { 1239 ntfs_inode_warn(inode, "direct i/o + compressed not supported"); 1240 ret = -EOPNOTSUPP; 1241 goto out; 1242 } 1243 1244 ret = generic_write_checks(iocb, from); 1245 if (ret <= 0) 1246 goto out; 1247 1248 err = file_modified(iocb->ki_filp); 1249 if (err) { 1250 ret = err; 1251 goto out; 1252 } 1253 1254 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { 1255 /* Should never be here, see ntfs_file_open(). */ 1256 ret = -EOPNOTSUPP; 1257 goto out; 1258 } 1259 1260 vbo = iocb->ki_pos; 1261 endbyte = vbo + ret; 1262 1263 if (endbyte > inode->i_size) { 1264 /* 1265 * Normal files: increase file size, allocate space. 1266 * Sparse/Compressed: increase file size. No space allocated. 1267 */ 1268 ret = ntfs_set_size(inode, endbyte); 1269 if (ret) 1270 goto out; 1271 } 1272 1273 if (is_compressed(ni)) { 1274 ret = ntfs_compress_write(iocb, from); 1275 goto out; 1276 } 1277 1278 if (vbo > ni->i_valid) { 1279 ret = ntfs_extend_initialized_size(file, ni, vbo); 1280 if (ret) 1281 goto out; 1282 } 1283 1284 /* Fallback to buffered I/O if the inode does not support direct I/O. */ 1285 if (!(iocb->ki_flags & IOCB_DIRECT) || 1286 !ntfs_should_use_dio(iocb, from)) { 1287 iocb->ki_flags &= ~IOCB_DIRECT; 1288 1289 ret = iomap_file_buffered_write(iocb, from, &ntfs_iomap_ops, 1290 &ntfs_iomap_folio_ops, NULL); 1291 inode_unlock(inode); 1292 1293 if (likely(ret > 0)) 1294 ret = generic_write_sync(iocb, ret); 1295 1296 return ret; 1297 } 1298 1299 if (ni->file.run_da.count) { 1300 /* Direct I/O is not compatible with delalloc. */ 1301 ret = ni_allocate_da_blocks(ni); 1302 if (ret) 1303 goto out; 1304 } 1305 1306 ret = iomap_dio_rw(iocb, from, &ntfs_iomap_ops, NULL, 1307 IOMAP_DIO_FORCE_WAIT, NULL, 0); 1308 1309 if (ret == -ENOTBLK) { 1310 /* Returns -ENOTBLK in case of a page invalidation failure for writes.*/ 1311 /* The callers needs to fall back to buffered I/O in this case. */ 1312 ret = 0; 1313 } 1314 1315 if (ret >= 0 && iov_iter_count(from)) { 1316 vbo = iocb->ki_pos; 1317 1318 iocb->ki_flags &= ~IOCB_DIRECT; 1319 err = iomap_file_buffered_write(iocb, from, &ntfs_iomap_ops, 1320 &ntfs_iomap_folio_ops, NULL); 1321 if (err < 0) { 1322 ret = err; 1323 goto out; 1324 } 1325 1326 /* 1327 * We need to ensure that the pages within the page cache for 1328 * the range covered by this I/O are written to disk and 1329 * invalidated. This is in attempt to preserve the expected 1330 * direct I/O semantics in the case we fallback to buffered I/O 1331 * to complete off the I/O request. 1332 */ 1333 ret += err; 1334 endbyte = vbo + err - 1; 1335 err = filemap_write_and_wait_range(inode->i_mapping, vbo, 1336 endbyte); 1337 if (err) { 1338 ret = err; 1339 goto out; 1340 } 1341 1342 invalidate_mapping_pages(inode->i_mapping, vbo >> PAGE_SHIFT, 1343 endbyte >> PAGE_SHIFT); 1344 } 1345 1346 out: 1347 inode_unlock(inode); 1348 1349 return ret; 1350 } 1351 1352 /* 1353 * ntfs_file_open - file_operations::open 1354 */ 1355 int ntfs_file_open(struct inode *inode, struct file *file) 1356 { 1357 struct ntfs_inode *ni = ntfs_i(inode); 1358 1359 /* Avoid any operation if inode is bad. */ 1360 if (unlikely(is_bad_ni(ni))) 1361 return -EINVAL; 1362 1363 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1364 return -EIO; 1365 1366 if (unlikely((is_compressed(ni) || is_encrypted(ni)) && 1367 (file->f_flags & O_DIRECT))) { 1368 return -EOPNOTSUPP; 1369 } 1370 1371 /* Decompress "external compressed" file if opened for rw. */ 1372 if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) && 1373 (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) { 1374 #ifdef CONFIG_NTFS3_LZX_XPRESS 1375 int err = ni_decompress_file(ni); 1376 1377 if (err) 1378 return err; 1379 #else 1380 ntfs_inode_warn( 1381 inode, 1382 "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files"); 1383 return -EOPNOTSUPP; 1384 #endif 1385 } 1386 1387 file->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT; 1388 1389 return generic_file_open(inode, file); 1390 } 1391 1392 /* 1393 * ntfs_file_release - file_operations::release 1394 * 1395 * Called when an inode is released. Note that this is different 1396 * from ntfs_file_open: open gets called at every open, but release 1397 * gets called only when /all/ the files are closed. 1398 */ 1399 static int ntfs_file_release(struct inode *inode, struct file *file) 1400 { 1401 int err; 1402 struct ntfs_inode *ni; 1403 1404 if (!(file->f_mode & FMODE_WRITE) || 1405 atomic_read(&inode->i_writecount) != 1 || 1406 inode->i_ino == MFT_REC_MFT) { 1407 return 0; 1408 } 1409 1410 /* Close the last writer on the inode. */ 1411 ni = ntfs_i(inode); 1412 1413 /* Allocate delayed blocks (clusters). */ 1414 err = ni_allocate_da_blocks(ni); 1415 if (err) 1416 goto out; 1417 1418 if (ni->mi.sbi->options->prealloc) { 1419 ni_lock(ni); 1420 down_write(&ni->file.run_lock); 1421 1422 /* Deallocate preallocated. */ 1423 err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, &ni->file.run, 1424 inode->i_size, &ni->i_valid, false, NULL, 1425 true); 1426 1427 up_write(&ni->file.run_lock); 1428 ni_unlock(ni); 1429 } 1430 out: 1431 return err; 1432 } 1433 1434 /* 1435 * ntfs_fiemap - inode_operations::fiemap 1436 */ 1437 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 1438 __u64 start, __u64 len) 1439 { 1440 int err; 1441 struct ntfs_inode *ni = ntfs_i(inode); 1442 1443 /* Avoid any operation if inode is bad. */ 1444 if (unlikely(is_bad_ni(ni))) 1445 return -EINVAL; 1446 1447 if (is_compressed(ni)) { 1448 /* Unfortunately cp -r incorrectly treats compressed clusters. */ 1449 ntfs_inode_warn(inode, 1450 "fiemap is not supported for compressed file"); 1451 return -EOPNOTSUPP; 1452 } 1453 1454 if (S_ISDIR(inode->i_mode)) { 1455 /* TODO: add support for dirs (ATTR_ALLOC). */ 1456 ntfs_inode_warn(inode, 1457 "fiemap is not supported for directories"); 1458 return -EOPNOTSUPP; 1459 } 1460 1461 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 1462 ntfs_inode_warn(inode, "fiemap(xattr) is not supported"); 1463 return -EOPNOTSUPP; 1464 } 1465 1466 inode_lock_shared(inode); 1467 1468 err = iomap_fiemap(inode, fieinfo, start, len, &ntfs_iomap_ops); 1469 1470 inode_unlock_shared(inode); 1471 return err; 1472 } 1473 1474 /* 1475 * ntfs_file_splice_write - file_operations::splice_write 1476 */ 1477 static ssize_t ntfs_file_splice_write(struct pipe_inode_info *pipe, 1478 struct file *file, loff_t *ppos, 1479 size_t len, unsigned int flags) 1480 { 1481 ssize_t err; 1482 struct inode *inode = file_inode(file); 1483 1484 err = check_write_restriction(inode); 1485 if (err) 1486 return err; 1487 1488 return iter_file_splice_write(pipe, file, ppos, len, flags); 1489 } 1490 1491 /* 1492 * ntfs_file_fsync - file_operations::fsync 1493 */ 1494 int ntfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 1495 { 1496 struct inode *inode = file_inode(file); 1497 struct super_block *sb = inode->i_sb; 1498 struct ntfs_sb_info *sbi = sb->s_fs_info; 1499 int err, ret; 1500 1501 if (unlikely(ntfs3_forced_shutdown(sb))) 1502 return -EIO; 1503 1504 ret = file_write_and_wait_range(file, start, end); 1505 if (ret) 1506 return ret; 1507 1508 ret = write_inode_now(inode, !datasync); 1509 1510 if (!ret) { 1511 ret = ni_write_parents(ntfs_i(inode), !datasync); 1512 } 1513 1514 if (!ret) { 1515 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); 1516 ntfs_update_mftmirr(sbi); 1517 } 1518 1519 err = sync_blockdev(sb->s_bdev); 1520 if (unlikely(err && !ret)) 1521 ret = err; 1522 if (!ret) 1523 blkdev_issue_flush(sb->s_bdev); 1524 return ret; 1525 } 1526 1527 /* 1528 * ntfs_llseek - file_operations::llseek 1529 */ 1530 static loff_t ntfs_llseek(struct file *file, loff_t offset, int whence) 1531 { 1532 struct inode *inode = file->f_mapping->host; 1533 struct ntfs_inode *ni = ntfs_i(inode); 1534 loff_t maxbytes = ntfs_get_maxbytes(ni); 1535 loff_t ret; 1536 1537 if (whence != SEEK_DATA && whence != SEEK_HOLE) { 1538 ret = generic_file_llseek_size(file, offset, whence, maxbytes, 1539 i_size_read(inode)); 1540 } else if ((unsigned long long)offset >= i_size_read(inode)) { 1541 ret = -ENXIO; 1542 } else { 1543 inode_lock_shared(inode); 1544 /* Scan file for hole or data. */ 1545 ret = ni_seek_data_or_hole(ni, offset, whence == SEEK_DATA); 1546 inode_unlock_shared(inode); 1547 1548 if (ret >= 0) 1549 ret = vfs_setpos(file, ret, maxbytes); 1550 } 1551 return ret; 1552 } 1553 1554 // clang-format off 1555 const struct inode_operations ntfs_file_inode_operations = { 1556 .getattr = ntfs_getattr, 1557 .setattr = ntfs_setattr, 1558 .listxattr = ntfs_listxattr, 1559 .get_acl = ntfs_get_acl, 1560 .set_acl = ntfs_set_acl, 1561 .fiemap = ntfs_fiemap, 1562 .fileattr_get = ntfs_fileattr_get, 1563 .fileattr_set = ntfs_fileattr_set, 1564 }; 1565 1566 const struct file_operations ntfs_file_operations = { 1567 .llseek = ntfs_llseek, 1568 .read_iter = ntfs_file_read_iter, 1569 .write_iter = ntfs_file_write_iter, 1570 .unlocked_ioctl = ntfs_ioctl, 1571 #ifdef CONFIG_COMPAT 1572 .compat_ioctl = ntfs_compat_ioctl, 1573 #endif 1574 .splice_read = ntfs_file_splice_read, 1575 .splice_write = ntfs_file_splice_write, 1576 .mmap_prepare = ntfs_file_mmap_prepare, 1577 .open = ntfs_file_open, 1578 .fsync = ntfs_file_fsync, 1579 .fallocate = ntfs_fallocate, 1580 .release = ntfs_file_release, 1581 .setlease = generic_setlease, 1582 }; 1583 // clang-format on 1584