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