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