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 17 #include "debug.h" 18 #include "ntfs.h" 19 #include "ntfs_fs.h" 20 21 static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg) 22 { 23 struct fstrim_range __user *user_range; 24 struct fstrim_range range; 25 struct block_device *dev; 26 int err; 27 28 if (!capable(CAP_SYS_ADMIN)) 29 return -EPERM; 30 31 dev = sbi->sb->s_bdev; 32 if (!bdev_max_discard_sectors(dev)) 33 return -EOPNOTSUPP; 34 35 user_range = (struct fstrim_range __user *)arg; 36 if (copy_from_user(&range, user_range, sizeof(range))) 37 return -EFAULT; 38 39 range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev)); 40 41 err = ntfs_trim_fs(sbi, &range); 42 if (err < 0) 43 return err; 44 45 if (copy_to_user(user_range, &range, sizeof(range))) 46 return -EFAULT; 47 48 return 0; 49 } 50 51 long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg) 52 { 53 struct inode *inode = file_inode(filp); 54 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; 55 56 switch (cmd) { 57 case FITRIM: 58 return ntfs_ioctl_fitrim(sbi, arg); 59 } 60 return -ENOTTY; /* Inappropriate ioctl for device. */ 61 } 62 63 #ifdef CONFIG_COMPAT 64 long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg) 65 66 { 67 return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); 68 } 69 #endif 70 71 /* 72 * ntfs_getattr - inode_operations::getattr 73 */ 74 int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path, 75 struct kstat *stat, u32 request_mask, u32 flags) 76 { 77 struct inode *inode = d_inode(path->dentry); 78 struct ntfs_inode *ni = ntfs_i(inode); 79 80 if (is_compressed(ni)) 81 stat->attributes |= STATX_ATTR_COMPRESSED; 82 83 if (is_encrypted(ni)) 84 stat->attributes |= STATX_ATTR_ENCRYPTED; 85 86 stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED; 87 88 generic_fillattr(idmap, request_mask, inode, stat); 89 90 stat->result_mask |= STATX_BTIME; 91 stat->btime = ni->i_crtime; 92 stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */ 93 94 return 0; 95 } 96 97 static int ntfs_extend_initialized_size(struct file *file, 98 struct ntfs_inode *ni, 99 const loff_t valid, 100 const loff_t new_valid) 101 { 102 struct inode *inode = &ni->vfs_inode; 103 struct address_space *mapping = inode->i_mapping; 104 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; 105 loff_t pos = valid; 106 int err; 107 108 if (is_resident(ni)) { 109 ni->i_valid = new_valid; 110 return 0; 111 } 112 113 WARN_ON(is_compressed(ni)); 114 WARN_ON(valid >= new_valid); 115 116 for (;;) { 117 u32 zerofrom, len; 118 struct page *page; 119 u8 bits; 120 CLST vcn, lcn, clen; 121 122 if (is_sparsed(ni)) { 123 bits = sbi->cluster_bits; 124 vcn = pos >> bits; 125 126 err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL, 127 false); 128 if (err) 129 goto out; 130 131 if (lcn == SPARSE_LCN) { 132 pos = ((loff_t)clen + vcn) << bits; 133 ni->i_valid = pos; 134 goto next; 135 } 136 } 137 138 zerofrom = pos & (PAGE_SIZE - 1); 139 len = PAGE_SIZE - zerofrom; 140 141 if (pos + len > new_valid) 142 len = new_valid - pos; 143 144 err = ntfs_write_begin(file, mapping, pos, len, &page, NULL); 145 if (err) 146 goto out; 147 148 zero_user_segment(page, zerofrom, PAGE_SIZE); 149 150 /* This function in any case puts page. */ 151 err = ntfs_write_end(file, mapping, pos, len, len, page, NULL); 152 if (err < 0) 153 goto out; 154 pos += len; 155 156 next: 157 if (pos >= new_valid) 158 break; 159 160 balance_dirty_pages_ratelimited(mapping); 161 cond_resched(); 162 } 163 164 return 0; 165 166 out: 167 ni->i_valid = valid; 168 ntfs_inode_warn(inode, "failed to extend initialized size to %llx.", 169 new_valid); 170 return err; 171 } 172 173 /* 174 * ntfs_zero_range - Helper function for punch_hole. 175 * 176 * It zeroes a range [vbo, vbo_to). 177 */ 178 static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to) 179 { 180 int err = 0; 181 struct address_space *mapping = inode->i_mapping; 182 u32 blocksize = i_blocksize(inode); 183 pgoff_t idx = vbo >> PAGE_SHIFT; 184 u32 from = vbo & (PAGE_SIZE - 1); 185 pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT; 186 loff_t page_off; 187 struct buffer_head *head, *bh; 188 u32 bh_next, bh_off, to; 189 sector_t iblock; 190 struct folio *folio; 191 bool dirty = false; 192 193 for (; idx < idx_end; idx += 1, from = 0) { 194 page_off = (loff_t)idx << PAGE_SHIFT; 195 to = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off) : 196 PAGE_SIZE; 197 iblock = page_off >> inode->i_blkbits; 198 199 folio = __filemap_get_folio(mapping, idx, 200 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, 201 mapping_gfp_constraint(mapping, ~__GFP_FS)); 202 if (IS_ERR(folio)) 203 return PTR_ERR(folio); 204 205 head = folio_buffers(folio); 206 if (!head) 207 head = create_empty_buffers(folio, blocksize, 0); 208 209 bh = head; 210 bh_off = 0; 211 do { 212 bh_next = bh_off + blocksize; 213 214 if (bh_next <= from || bh_off >= to) 215 continue; 216 217 if (!buffer_mapped(bh)) { 218 ntfs_get_block(inode, iblock, bh, 0); 219 /* Unmapped? It's a hole - nothing to do. */ 220 if (!buffer_mapped(bh)) 221 continue; 222 } 223 224 /* Ok, it's mapped. Make sure it's up-to-date. */ 225 if (folio_test_uptodate(folio)) 226 set_buffer_uptodate(bh); 227 else if (bh_read(bh, 0) < 0) { 228 err = -EIO; 229 folio_unlock(folio); 230 folio_put(folio); 231 goto out; 232 } 233 234 mark_buffer_dirty(bh); 235 } while (bh_off = bh_next, iblock += 1, 236 head != (bh = bh->b_this_page)); 237 238 folio_zero_segment(folio, from, to); 239 dirty = true; 240 241 folio_unlock(folio); 242 folio_put(folio); 243 cond_resched(); 244 } 245 out: 246 if (dirty) 247 mark_inode_dirty(inode); 248 return err; 249 } 250 251 /* 252 * ntfs_file_mmap - file_operations::mmap 253 */ 254 static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma) 255 { 256 struct address_space *mapping = file->f_mapping; 257 struct inode *inode = mapping->host; 258 struct ntfs_inode *ni = ntfs_i(inode); 259 u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT); 260 bool rw = vma->vm_flags & VM_WRITE; 261 int err; 262 263 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 264 return -EIO; 265 266 if (is_encrypted(ni)) { 267 ntfs_inode_warn(inode, "mmap encrypted not supported"); 268 return -EOPNOTSUPP; 269 } 270 271 if (is_dedup(ni)) { 272 ntfs_inode_warn(inode, "mmap deduplicated not supported"); 273 return -EOPNOTSUPP; 274 } 275 276 if (is_compressed(ni) && rw) { 277 ntfs_inode_warn(inode, "mmap(write) compressed not supported"); 278 return -EOPNOTSUPP; 279 } 280 281 if (rw) { 282 u64 to = min_t(loff_t, i_size_read(inode), 283 from + vma->vm_end - vma->vm_start); 284 285 if (is_sparsed(ni)) { 286 /* Allocate clusters for rw map. */ 287 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; 288 CLST lcn, len; 289 CLST vcn = from >> sbi->cluster_bits; 290 CLST end = bytes_to_cluster(sbi, to); 291 bool new; 292 293 for (; vcn < end; vcn += len) { 294 err = attr_data_get_block(ni, vcn, 1, &lcn, 295 &len, &new, true); 296 if (err) 297 goto out; 298 } 299 } 300 301 if (ni->i_valid < to) { 302 if (!inode_trylock(inode)) { 303 err = -EAGAIN; 304 goto out; 305 } 306 err = ntfs_extend_initialized_size(file, ni, 307 ni->i_valid, to); 308 inode_unlock(inode); 309 if (err) 310 goto out; 311 } 312 } 313 314 err = generic_file_mmap(file, vma); 315 out: 316 return err; 317 } 318 319 static int ntfs_extend(struct inode *inode, loff_t pos, size_t count, 320 struct file *file) 321 { 322 struct ntfs_inode *ni = ntfs_i(inode); 323 struct address_space *mapping = inode->i_mapping; 324 loff_t end = pos + count; 325 bool extend_init = file && pos > ni->i_valid; 326 int err; 327 328 if (end <= inode->i_size && !extend_init) 329 return 0; 330 331 /* Mark rw ntfs as dirty. It will be cleared at umount. */ 332 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY); 333 334 if (end > inode->i_size) { 335 err = ntfs_set_size(inode, end); 336 if (err) 337 goto out; 338 } 339 340 if (extend_init && !is_compressed(ni)) { 341 err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos); 342 if (err) 343 goto out; 344 } else { 345 err = 0; 346 } 347 348 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 349 mark_inode_dirty(inode); 350 351 if (IS_SYNC(inode)) { 352 int err2; 353 354 err = filemap_fdatawrite_range(mapping, pos, end - 1); 355 err2 = sync_mapping_buffers(mapping); 356 if (!err) 357 err = err2; 358 err2 = write_inode_now(inode, 1); 359 if (!err) 360 err = err2; 361 if (!err) 362 err = filemap_fdatawait_range(mapping, pos, end - 1); 363 } 364 365 out: 366 return err; 367 } 368 369 static int ntfs_truncate(struct inode *inode, loff_t new_size) 370 { 371 struct super_block *sb = inode->i_sb; 372 struct ntfs_inode *ni = ntfs_i(inode); 373 int err, dirty = 0; 374 u64 new_valid; 375 376 if (!S_ISREG(inode->i_mode)) 377 return 0; 378 379 if (is_compressed(ni)) { 380 if (ni->i_valid > new_size) 381 ni->i_valid = new_size; 382 } else { 383 err = block_truncate_page(inode->i_mapping, new_size, 384 ntfs_get_block); 385 if (err) 386 return err; 387 } 388 389 new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size)); 390 391 truncate_setsize(inode, new_size); 392 393 ni_lock(ni); 394 395 down_write(&ni->file.run_lock); 396 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size, 397 &new_valid, ni->mi.sbi->options->prealloc, NULL); 398 up_write(&ni->file.run_lock); 399 400 if (new_valid < ni->i_valid) 401 ni->i_valid = new_valid; 402 403 ni_unlock(ni); 404 405 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE; 406 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 407 if (!IS_DIRSYNC(inode)) { 408 dirty = 1; 409 } else { 410 err = ntfs_sync_inode(inode); 411 if (err) 412 return err; 413 } 414 415 if (dirty) 416 mark_inode_dirty(inode); 417 418 /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/ 419 420 return 0; 421 } 422 423 /* 424 * ntfs_fallocate 425 * 426 * Preallocate space for a file. This implements ntfs's fallocate file 427 * operation, which gets called from sys_fallocate system call. User 428 * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set 429 * we just allocate clusters without zeroing them out. Otherwise we 430 * allocate and zero out clusters via an expanding truncate. 431 */ 432 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len) 433 { 434 struct inode *inode = file->f_mapping->host; 435 struct address_space *mapping = inode->i_mapping; 436 struct super_block *sb = inode->i_sb; 437 struct ntfs_sb_info *sbi = sb->s_fs_info; 438 struct ntfs_inode *ni = ntfs_i(inode); 439 loff_t end = vbo + len; 440 loff_t vbo_down = round_down(vbo, max_t(unsigned long, 441 sbi->cluster_size, PAGE_SIZE)); 442 bool is_supported_holes = is_sparsed(ni) || is_compressed(ni); 443 loff_t i_size, new_size; 444 bool map_locked; 445 int err; 446 447 /* No support for dir. */ 448 if (!S_ISREG(inode->i_mode)) 449 return -EOPNOTSUPP; 450 451 /* 452 * vfs_fallocate checks all possible combinations of mode. 453 * Do additional checks here before ntfs_set_state(dirty). 454 */ 455 if (mode & FALLOC_FL_PUNCH_HOLE) { 456 if (!is_supported_holes) 457 return -EOPNOTSUPP; 458 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { 459 } else if (mode & FALLOC_FL_INSERT_RANGE) { 460 if (!is_supported_holes) 461 return -EOPNOTSUPP; 462 } else if (mode & 463 ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | 464 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) { 465 ntfs_inode_warn(inode, "fallocate(0x%x) is not supported", 466 mode); 467 return -EOPNOTSUPP; 468 } 469 470 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 471 472 inode_lock(inode); 473 i_size = inode->i_size; 474 new_size = max(end, i_size); 475 map_locked = false; 476 477 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { 478 /* Should never be here, see ntfs_file_open. */ 479 err = -EOPNOTSUPP; 480 goto out; 481 } 482 483 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE | 484 FALLOC_FL_INSERT_RANGE)) { 485 inode_dio_wait(inode); 486 filemap_invalidate_lock(mapping); 487 map_locked = true; 488 } 489 490 if (mode & FALLOC_FL_PUNCH_HOLE) { 491 u32 frame_size; 492 loff_t mask, vbo_a, end_a, tmp; 493 494 err = filemap_write_and_wait_range(mapping, vbo_down, 495 LLONG_MAX); 496 if (err) 497 goto out; 498 499 truncate_pagecache(inode, vbo_down); 500 501 ni_lock(ni); 502 err = attr_punch_hole(ni, vbo, len, &frame_size); 503 ni_unlock(ni); 504 if (!err) 505 goto ok; 506 507 if (err != E_NTFS_NOTALIGNED) 508 goto out; 509 510 /* Process not aligned punch. */ 511 err = 0; 512 mask = frame_size - 1; 513 vbo_a = (vbo + mask) & ~mask; 514 end_a = end & ~mask; 515 516 tmp = min(vbo_a, end); 517 if (tmp > vbo) { 518 err = ntfs_zero_range(inode, vbo, tmp); 519 if (err) 520 goto out; 521 } 522 523 if (vbo < end_a && end_a < end) { 524 err = ntfs_zero_range(inode, end_a, end); 525 if (err) 526 goto out; 527 } 528 529 /* Aligned punch_hole */ 530 if (end_a > vbo_a) { 531 ni_lock(ni); 532 err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL); 533 ni_unlock(ni); 534 if (err) 535 goto out; 536 } 537 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { 538 /* 539 * Write tail of the last page before removed range since 540 * it will get removed from the page cache below. 541 */ 542 err = filemap_write_and_wait_range(mapping, vbo_down, vbo); 543 if (err) 544 goto out; 545 546 /* 547 * Write data that will be shifted to preserve them 548 * when discarding page cache below. 549 */ 550 err = filemap_write_and_wait_range(mapping, end, LLONG_MAX); 551 if (err) 552 goto out; 553 554 truncate_pagecache(inode, vbo_down); 555 556 ni_lock(ni); 557 err = attr_collapse_range(ni, vbo, len); 558 ni_unlock(ni); 559 } else if (mode & FALLOC_FL_INSERT_RANGE) { 560 /* Check new size. */ 561 err = inode_newsize_ok(inode, new_size); 562 if (err) 563 goto out; 564 565 /* Write out all dirty pages. */ 566 err = filemap_write_and_wait_range(mapping, vbo_down, 567 LLONG_MAX); 568 if (err) 569 goto out; 570 truncate_pagecache(inode, vbo_down); 571 572 ni_lock(ni); 573 err = attr_insert_range(ni, vbo, len); 574 ni_unlock(ni); 575 if (err) 576 goto out; 577 } else { 578 /* Check new size. */ 579 u8 cluster_bits = sbi->cluster_bits; 580 581 /* Be sure file is non resident. */ 582 if (is_resident(ni)) { 583 ni_lock(ni); 584 err = attr_force_nonresident(ni); 585 ni_unlock(ni); 586 if (err) 587 goto out; 588 } 589 590 /* generic/213: expected -ENOSPC instead of -EFBIG. */ 591 if (!is_supported_holes) { 592 loff_t to_alloc = new_size - inode_get_bytes(inode); 593 594 if (to_alloc > 0 && 595 (to_alloc >> cluster_bits) > 596 wnd_zeroes(&sbi->used.bitmap)) { 597 err = -ENOSPC; 598 goto out; 599 } 600 } 601 602 err = inode_newsize_ok(inode, new_size); 603 if (err) 604 goto out; 605 606 if (new_size > i_size) { 607 /* 608 * Allocate clusters, do not change 'valid' size. 609 */ 610 err = ntfs_set_size(inode, new_size); 611 if (err) 612 goto out; 613 } 614 615 if (is_supported_holes) { 616 CLST vcn = vbo >> cluster_bits; 617 CLST cend = bytes_to_cluster(sbi, end); 618 CLST cend_v = bytes_to_cluster(sbi, ni->i_valid); 619 CLST lcn, clen; 620 bool new; 621 622 if (cend_v > cend) 623 cend_v = cend; 624 625 /* 626 * Allocate and zero new clusters. 627 * Zeroing these clusters may be too long. 628 */ 629 for (; vcn < cend_v; vcn += clen) { 630 err = attr_data_get_block(ni, vcn, cend_v - vcn, 631 &lcn, &clen, &new, 632 true); 633 if (err) 634 goto out; 635 } 636 /* 637 * Allocate but not zero new clusters. 638 */ 639 for (; vcn < cend; vcn += clen) { 640 err = attr_data_get_block(ni, vcn, cend - vcn, 641 &lcn, &clen, &new, 642 false); 643 if (err) 644 goto out; 645 } 646 } 647 648 if (mode & FALLOC_FL_KEEP_SIZE) { 649 ni_lock(ni); 650 /* True - Keep preallocated. */ 651 err = attr_set_size(ni, ATTR_DATA, NULL, 0, 652 &ni->file.run, i_size, &ni->i_valid, 653 true, NULL); 654 ni_unlock(ni); 655 if (err) 656 goto out; 657 } else if (new_size > i_size) { 658 i_size_write(inode, new_size); 659 } 660 } 661 662 ok: 663 err = file_modified(file); 664 if (err) 665 goto out; 666 667 out: 668 if (map_locked) 669 filemap_invalidate_unlock(mapping); 670 671 if (!err) { 672 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 673 mark_inode_dirty(inode); 674 } 675 676 inode_unlock(inode); 677 return err; 678 } 679 680 /* 681 * ntfs3_setattr - inode_operations::setattr 682 */ 683 int ntfs3_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 684 struct iattr *attr) 685 { 686 struct inode *inode = d_inode(dentry); 687 struct ntfs_inode *ni = ntfs_i(inode); 688 u32 ia_valid = attr->ia_valid; 689 umode_t mode = inode->i_mode; 690 int err; 691 692 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 693 return -EIO; 694 695 err = setattr_prepare(idmap, dentry, attr); 696 if (err) 697 goto out; 698 699 if (ia_valid & ATTR_SIZE) { 700 loff_t newsize, oldsize; 701 702 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { 703 /* Should never be here, see ntfs_file_open(). */ 704 err = -EOPNOTSUPP; 705 goto out; 706 } 707 inode_dio_wait(inode); 708 oldsize = i_size_read(inode); 709 newsize = attr->ia_size; 710 711 if (newsize <= oldsize) 712 err = ntfs_truncate(inode, newsize); 713 else 714 err = ntfs_extend(inode, newsize, 0, NULL); 715 716 if (err) 717 goto out; 718 719 ni->ni_flags |= NI_FLAG_UPDATE_PARENT; 720 i_size_write(inode, newsize); 721 } 722 723 setattr_copy(idmap, inode, attr); 724 725 if (mode != inode->i_mode) { 726 err = ntfs_acl_chmod(idmap, dentry); 727 if (err) 728 goto out; 729 730 /* Linux 'w' -> Windows 'ro'. */ 731 if (0222 & inode->i_mode) 732 ni->std_fa &= ~FILE_ATTRIBUTE_READONLY; 733 else 734 ni->std_fa |= FILE_ATTRIBUTE_READONLY; 735 } 736 737 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) 738 ntfs_save_wsl_perm(inode, NULL); 739 mark_inode_dirty(inode); 740 out: 741 return err; 742 } 743 744 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) 745 { 746 struct file *file = iocb->ki_filp; 747 struct inode *inode = file->f_mapping->host; 748 struct ntfs_inode *ni = ntfs_i(inode); 749 750 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 751 return -EIO; 752 753 if (is_encrypted(ni)) { 754 ntfs_inode_warn(inode, "encrypted i/o not supported"); 755 return -EOPNOTSUPP; 756 } 757 758 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) { 759 ntfs_inode_warn(inode, "direct i/o + compressed not supported"); 760 return -EOPNOTSUPP; 761 } 762 763 #ifndef CONFIG_NTFS3_LZX_XPRESS 764 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) { 765 ntfs_inode_warn( 766 inode, 767 "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files"); 768 return -EOPNOTSUPP; 769 } 770 #endif 771 772 if (is_dedup(ni)) { 773 ntfs_inode_warn(inode, "read deduplicated not supported"); 774 return -EOPNOTSUPP; 775 } 776 777 return generic_file_read_iter(iocb, iter); 778 } 779 780 static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos, 781 struct pipe_inode_info *pipe, size_t len, 782 unsigned int flags) 783 { 784 struct inode *inode = in->f_mapping->host; 785 struct ntfs_inode *ni = ntfs_i(inode); 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 filemap_splice_read(in, ppos, pipe, len, flags); 810 } 811 812 /* 813 * ntfs_get_frame_pages 814 * 815 * Return: Array of locked pages. 816 */ 817 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index, 818 struct page **pages, u32 pages_per_frame, 819 bool *frame_uptodate) 820 { 821 gfp_t gfp_mask = mapping_gfp_mask(mapping); 822 u32 npages; 823 824 *frame_uptodate = true; 825 826 for (npages = 0; npages < pages_per_frame; npages++, index++) { 827 struct page *page; 828 829 page = find_or_create_page(mapping, index, gfp_mask); 830 if (!page) { 831 while (npages--) { 832 page = pages[npages]; 833 unlock_page(page); 834 put_page(page); 835 } 836 837 return -ENOMEM; 838 } 839 840 if (!PageUptodate(page)) 841 *frame_uptodate = false; 842 843 pages[npages] = page; 844 } 845 846 return 0; 847 } 848 849 /* 850 * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files). 851 */ 852 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from) 853 { 854 int err; 855 struct file *file = iocb->ki_filp; 856 size_t count = iov_iter_count(from); 857 loff_t pos = iocb->ki_pos; 858 struct inode *inode = file_inode(file); 859 loff_t i_size = i_size_read(inode); 860 struct address_space *mapping = inode->i_mapping; 861 struct ntfs_inode *ni = ntfs_i(inode); 862 u64 valid = ni->i_valid; 863 struct ntfs_sb_info *sbi = ni->mi.sbi; 864 struct page *page, **pages = NULL; 865 size_t written = 0; 866 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits; 867 u32 frame_size = 1u << frame_bits; 868 u32 pages_per_frame = frame_size >> PAGE_SHIFT; 869 u32 ip, off; 870 CLST frame; 871 u64 frame_vbo; 872 pgoff_t index; 873 bool frame_uptodate; 874 875 if (frame_size < PAGE_SIZE) { 876 /* 877 * frame_size == 8K if cluster 512 878 * frame_size == 64K if cluster 4096 879 */ 880 ntfs_inode_warn(inode, "page size is bigger than frame size"); 881 return -EOPNOTSUPP; 882 } 883 884 pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS); 885 if (!pages) 886 return -ENOMEM; 887 888 err = file_remove_privs(file); 889 if (err) 890 goto out; 891 892 err = file_update_time(file); 893 if (err) 894 goto out; 895 896 /* Zero range [valid : pos). */ 897 while (valid < pos) { 898 CLST lcn, clen; 899 900 frame = valid >> frame_bits; 901 frame_vbo = valid & ~(frame_size - 1); 902 off = valid & (frame_size - 1); 903 904 err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn, 905 &clen, NULL, false); 906 if (err) 907 goto out; 908 909 if (lcn == SPARSE_LCN) { 910 ni->i_valid = valid = 911 frame_vbo + ((u64)clen << sbi->cluster_bits); 912 continue; 913 } 914 915 /* Load full frame. */ 916 err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT, 917 pages, pages_per_frame, 918 &frame_uptodate); 919 if (err) 920 goto out; 921 922 if (!frame_uptodate && off) { 923 err = ni_read_frame(ni, frame_vbo, pages, 924 pages_per_frame); 925 if (err) { 926 for (ip = 0; ip < pages_per_frame; ip++) { 927 page = pages[ip]; 928 unlock_page(page); 929 put_page(page); 930 } 931 goto out; 932 } 933 } 934 935 ip = off >> PAGE_SHIFT; 936 off = offset_in_page(valid); 937 for (; ip < pages_per_frame; ip++, off = 0) { 938 page = pages[ip]; 939 zero_user_segment(page, off, PAGE_SIZE); 940 flush_dcache_page(page); 941 SetPageUptodate(page); 942 } 943 944 ni_lock(ni); 945 err = ni_write_frame(ni, pages, pages_per_frame); 946 ni_unlock(ni); 947 948 for (ip = 0; ip < pages_per_frame; ip++) { 949 page = pages[ip]; 950 SetPageUptodate(page); 951 unlock_page(page); 952 put_page(page); 953 } 954 955 if (err) 956 goto out; 957 958 ni->i_valid = valid = frame_vbo + frame_size; 959 } 960 961 /* Copy user data [pos : pos + count). */ 962 while (count) { 963 size_t copied, bytes; 964 965 off = pos & (frame_size - 1); 966 bytes = frame_size - off; 967 if (bytes > count) 968 bytes = count; 969 970 frame_vbo = pos & ~(frame_size - 1); 971 index = frame_vbo >> PAGE_SHIFT; 972 973 if (unlikely(fault_in_iov_iter_readable(from, bytes))) { 974 err = -EFAULT; 975 goto out; 976 } 977 978 /* Load full frame. */ 979 err = ntfs_get_frame_pages(mapping, index, pages, 980 pages_per_frame, &frame_uptodate); 981 if (err) 982 goto out; 983 984 if (!frame_uptodate) { 985 loff_t to = pos + bytes; 986 987 if (off || (to < i_size && (to & (frame_size - 1)))) { 988 err = ni_read_frame(ni, frame_vbo, pages, 989 pages_per_frame); 990 if (err) { 991 for (ip = 0; ip < pages_per_frame; 992 ip++) { 993 page = pages[ip]; 994 unlock_page(page); 995 put_page(page); 996 } 997 goto out; 998 } 999 } 1000 } 1001 1002 WARN_ON(!bytes); 1003 copied = 0; 1004 ip = off >> PAGE_SHIFT; 1005 off = offset_in_page(pos); 1006 1007 /* Copy user data to pages. */ 1008 for (;;) { 1009 size_t cp, tail = PAGE_SIZE - off; 1010 1011 page = pages[ip]; 1012 cp = copy_page_from_iter_atomic(page, off, 1013 min(tail, bytes), from); 1014 flush_dcache_page(page); 1015 1016 copied += cp; 1017 bytes -= cp; 1018 if (!bytes || !cp) 1019 break; 1020 1021 if (cp < tail) { 1022 off += cp; 1023 } else { 1024 ip++; 1025 off = 0; 1026 } 1027 } 1028 1029 ni_lock(ni); 1030 err = ni_write_frame(ni, pages, pages_per_frame); 1031 ni_unlock(ni); 1032 1033 for (ip = 0; ip < pages_per_frame; ip++) { 1034 page = pages[ip]; 1035 ClearPageDirty(page); 1036 SetPageUptodate(page); 1037 unlock_page(page); 1038 put_page(page); 1039 } 1040 1041 if (err) 1042 goto out; 1043 1044 /* 1045 * We can loop for a long time in here. Be nice and allow 1046 * us to schedule out to avoid softlocking if preempt 1047 * is disabled. 1048 */ 1049 cond_resched(); 1050 1051 pos += copied; 1052 written += copied; 1053 1054 count = iov_iter_count(from); 1055 } 1056 1057 out: 1058 kfree(pages); 1059 1060 if (err < 0) 1061 return err; 1062 1063 iocb->ki_pos += written; 1064 if (iocb->ki_pos > ni->i_valid) 1065 ni->i_valid = iocb->ki_pos; 1066 if (iocb->ki_pos > i_size) 1067 i_size_write(inode, iocb->ki_pos); 1068 1069 return written; 1070 } 1071 1072 /* 1073 * ntfs_file_write_iter - file_operations::write_iter 1074 */ 1075 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 1076 { 1077 struct file *file = iocb->ki_filp; 1078 struct address_space *mapping = file->f_mapping; 1079 struct inode *inode = mapping->host; 1080 ssize_t ret; 1081 int err; 1082 struct ntfs_inode *ni = ntfs_i(inode); 1083 1084 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1085 return -EIO; 1086 1087 if (is_encrypted(ni)) { 1088 ntfs_inode_warn(inode, "encrypted i/o not supported"); 1089 return -EOPNOTSUPP; 1090 } 1091 1092 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) { 1093 ntfs_inode_warn(inode, "direct i/o + compressed not supported"); 1094 return -EOPNOTSUPP; 1095 } 1096 1097 if (is_dedup(ni)) { 1098 ntfs_inode_warn(inode, "write into deduplicated not supported"); 1099 return -EOPNOTSUPP; 1100 } 1101 1102 if (!inode_trylock(inode)) { 1103 if (iocb->ki_flags & IOCB_NOWAIT) 1104 return -EAGAIN; 1105 inode_lock(inode); 1106 } 1107 1108 ret = generic_write_checks(iocb, from); 1109 if (ret <= 0) 1110 goto out; 1111 1112 err = file_modified(iocb->ki_filp); 1113 if (err) { 1114 ret = err; 1115 goto out; 1116 } 1117 1118 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { 1119 /* Should never be here, see ntfs_file_open(). */ 1120 ret = -EOPNOTSUPP; 1121 goto out; 1122 } 1123 1124 ret = ntfs_extend(inode, iocb->ki_pos, ret, file); 1125 if (ret) 1126 goto out; 1127 1128 ret = is_compressed(ni) ? ntfs_compress_write(iocb, from) : 1129 __generic_file_write_iter(iocb, from); 1130 1131 out: 1132 inode_unlock(inode); 1133 1134 if (ret > 0) 1135 ret = generic_write_sync(iocb, ret); 1136 1137 return ret; 1138 } 1139 1140 /* 1141 * ntfs_file_open - file_operations::open 1142 */ 1143 int ntfs_file_open(struct inode *inode, struct file *file) 1144 { 1145 struct ntfs_inode *ni = ntfs_i(inode); 1146 1147 if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1148 return -EIO; 1149 1150 if (unlikely((is_compressed(ni) || is_encrypted(ni)) && 1151 (file->f_flags & O_DIRECT))) { 1152 return -EOPNOTSUPP; 1153 } 1154 1155 /* Decompress "external compressed" file if opened for rw. */ 1156 if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) && 1157 (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) { 1158 #ifdef CONFIG_NTFS3_LZX_XPRESS 1159 int err = ni_decompress_file(ni); 1160 1161 if (err) 1162 return err; 1163 #else 1164 ntfs_inode_warn( 1165 inode, 1166 "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files"); 1167 return -EOPNOTSUPP; 1168 #endif 1169 } 1170 1171 return generic_file_open(inode, file); 1172 } 1173 1174 /* 1175 * ntfs_file_release - file_operations::release 1176 */ 1177 static int ntfs_file_release(struct inode *inode, struct file *file) 1178 { 1179 struct ntfs_inode *ni = ntfs_i(inode); 1180 struct ntfs_sb_info *sbi = ni->mi.sbi; 1181 int err = 0; 1182 1183 /* If we are last writer on the inode, drop the block reservation. */ 1184 if (sbi->options->prealloc && 1185 ((file->f_mode & FMODE_WRITE) && 1186 atomic_read(&inode->i_writecount) == 1)) { 1187 ni_lock(ni); 1188 down_write(&ni->file.run_lock); 1189 1190 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, 1191 i_size_read(inode), &ni->i_valid, false, 1192 NULL); 1193 1194 up_write(&ni->file.run_lock); 1195 ni_unlock(ni); 1196 } 1197 return err; 1198 } 1199 1200 /* 1201 * ntfs_fiemap - file_operations::fiemap 1202 */ 1203 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 1204 __u64 start, __u64 len) 1205 { 1206 int err; 1207 struct ntfs_inode *ni = ntfs_i(inode); 1208 1209 err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR); 1210 if (err) 1211 return err; 1212 1213 ni_lock(ni); 1214 1215 err = ni_fiemap(ni, fieinfo, start, len); 1216 1217 ni_unlock(ni); 1218 1219 return err; 1220 } 1221 1222 // clang-format off 1223 const struct inode_operations ntfs_file_inode_operations = { 1224 .getattr = ntfs_getattr, 1225 .setattr = ntfs3_setattr, 1226 .listxattr = ntfs_listxattr, 1227 .get_acl = ntfs_get_acl, 1228 .set_acl = ntfs_set_acl, 1229 .fiemap = ntfs_fiemap, 1230 }; 1231 1232 const struct file_operations ntfs_file_operations = { 1233 .llseek = generic_file_llseek, 1234 .read_iter = ntfs_file_read_iter, 1235 .write_iter = ntfs_file_write_iter, 1236 .unlocked_ioctl = ntfs_ioctl, 1237 #ifdef CONFIG_COMPAT 1238 .compat_ioctl = ntfs_compat_ioctl, 1239 #endif 1240 .splice_read = ntfs_file_splice_read, 1241 .mmap = ntfs_file_mmap, 1242 .open = ntfs_file_open, 1243 .fsync = generic_file_fsync, 1244 .splice_write = iter_file_splice_write, 1245 .fallocate = ntfs_fallocate, 1246 .release = ntfs_file_release, 1247 }; 1248 1249 const struct file_operations ntfs_legacy_file_operations = { 1250 .llseek = generic_file_llseek, 1251 .read_iter = ntfs_file_read_iter, 1252 .splice_read = ntfs_file_splice_read, 1253 .open = ntfs_file_open, 1254 .release = ntfs_file_release, 1255 }; 1256 // clang-format on 1257