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