1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * NTFS kernel file operations. 4 * 5 * Copyright (c) 2001-2015 Anton Altaparmakov and Tuxera Inc. 6 * Copyright (c) 2025 LG Electronics Co., Ltd. 7 */ 8 9 #include <linux/writeback.h> 10 #include <linux/blkdev.h> 11 #include <linux/fs.h> 12 #include <linux/iomap.h> 13 #include <linux/uio.h> 14 #include <linux/posix_acl.h> 15 #include <linux/posix_acl_xattr.h> 16 #include <linux/compat.h> 17 #include <linux/falloc.h> 18 19 #include "lcnalloc.h" 20 #include "ntfs.h" 21 #include "reparse.h" 22 #include "ea.h" 23 #include "iomap.h" 24 #include "bitmap.h" 25 #include "volume.h" 26 27 #include <linux/filelock.h> 28 29 /* 30 * ntfs_file_open - called when an inode is about to be opened 31 * @vi: inode to be opened 32 * @filp: file structure describing the inode 33 * 34 * Limit file size to the page cache limit on architectures where unsigned long 35 * is 32-bits. This is the most we can do for now without overflowing the page 36 * cache page index. Doing it this way means we don't run into problems because 37 * of existing too large files. It would be better to allow the user to read 38 * the beginning of the file but I doubt very much anyone is going to hit this 39 * check on a 32-bit architecture, so there is no point in adding the extra 40 * complexity required to support this. 41 * 42 * On 64-bit architectures, the check is hopefully optimized away by the 43 * compiler. 44 * 45 * After the check passes, just call generic_file_open() to do its work. 46 */ 47 static int ntfs_file_open(struct inode *vi, struct file *filp) 48 { 49 struct ntfs_inode *ni = NTFS_I(vi); 50 51 if (NVolShutdown(ni->vol)) 52 return -EIO; 53 54 if (sizeof(unsigned long) < 8) { 55 if (i_size_read(vi) > MAX_LFS_FILESIZE) 56 return -EOVERFLOW; 57 } 58 59 filp->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT; 60 61 return generic_file_open(vi, filp); 62 } 63 64 /* 65 * Trim preallocated space on file release. 66 * 67 * When the preallo_size mount option is set (default 64KB), writes extend 68 * allocated_size and runlist in units of preallocated size to reduce 69 * runlist merge overhead for small writes. This can leave 70 * allocated_size > data_size if not all preallocated space is used. 71 * 72 * We perform the trim here because ->release() is called only when 73 * the file is no longer open. At this point, no further writes can occur, 74 * so it is safe to reclaim the unused preallocated space. 75 * 76 * Returns 0 on success, or negative error on failure. 77 */ 78 static int ntfs_trim_prealloc(struct inode *vi) 79 { 80 struct ntfs_inode *ni = NTFS_I(vi); 81 struct ntfs_volume *vol = ni->vol; 82 struct runlist_element *rl; 83 s64 aligned_data_size; 84 s64 vcn_ds, vcn_tr; 85 ssize_t rc; 86 int err = 0; 87 88 inode_lock(vi); 89 mutex_lock(&ni->mrec_lock); 90 down_write(&ni->runlist.lock); 91 92 aligned_data_size = round_up(ni->data_size, vol->cluster_size); 93 if (aligned_data_size >= ni->allocated_size) 94 goto out_unlock; 95 96 vcn_ds = ntfs_bytes_to_cluster(vol, aligned_data_size); 97 vcn_tr = -1; 98 rc = ni->runlist.count - 2; 99 rl = ni->runlist.rl; 100 101 while (rc >= 0 && rl[rc].lcn == LCN_HOLE && vcn_ds <= rl[rc].vcn) { 102 vcn_tr = rl[rc].vcn; 103 rc--; 104 } 105 106 if (vcn_tr >= 0) { 107 err = ntfs_rl_truncate_nolock(vol, &ni->runlist, vcn_tr); 108 if (err) { 109 kvfree(ni->runlist.rl); 110 ni->runlist.rl = NULL; 111 ntfs_error(vol->sb, "Preallocated block rollback failed"); 112 } else { 113 ni->allocated_size = ntfs_cluster_to_bytes(vol, vcn_tr); 114 err = ntfs_attr_update_mapping_pairs_locked( 115 ni, 0, ni); 116 if (err) 117 ntfs_error(vol->sb, 118 "Failed to rollback mapping pairs for prealloc"); 119 } 120 } 121 122 out_unlock: 123 up_write(&ni->runlist.lock); 124 mutex_unlock(&ni->mrec_lock); 125 inode_unlock(vi); 126 127 return err; 128 } 129 130 static int ntfs_file_release(struct inode *vi, struct file *filp) 131 { 132 if (!NInoCompressed(NTFS_I(vi)) && 133 !NInoWofCompressed(NTFS_I(vi))) 134 return ntfs_trim_prealloc(vi); 135 136 return 0; 137 } 138 139 /* 140 * ntfs_file_fsync - sync a file to disk 141 * @filp: file to be synced 142 * @start: start offset to be synced 143 * @end: end offset to be synced 144 * @datasync: if non-zero only flush user data and not metadata 145 * 146 * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync 147 * system calls. This function is inspired by fs/buffer.c::file_fsync(). 148 * 149 * If @datasync is false, write the mft record and all associated extent mft 150 * records as well as the $DATA attribute and then sync the block device. 151 * 152 * If @datasync is true and the attribute is non-resident, we skip the writing 153 * of the mft record and all associated extent mft records (this might still 154 * happen due to the write_inode_now() call). 155 * 156 * Also, if @datasync is true, we do not wait on the inode to be written out 157 * but we always wait on the page cache pages to be written out. 158 */ 159 static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end, 160 int datasync) 161 { 162 struct inode *vi = filp->f_mapping->host; 163 struct ntfs_inode *ni = NTFS_I(vi); 164 struct ntfs_volume *vol = ni->vol; 165 int err, ret = 0; 166 struct inode *parent_vi, *ia_vi; 167 struct ntfs_attr_search_ctx *ctx; 168 169 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no); 170 171 if (NVolShutdown(vol)) 172 return -EIO; 173 174 err = file_write_and_wait_range(filp, start, end); 175 if (err) 176 return err; 177 178 if (!datasync || !NInoNonResident(NTFS_I(vi))) 179 ret = __ntfs_write_inode(vi, 1); 180 write_inode_now(vi, !datasync); 181 182 ctx = ntfs_attr_get_search_ctx(ni, NULL); 183 if (!ctx) 184 return -ENOMEM; 185 186 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_CHILD); 187 while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) { 188 if (ctx->attr->type == AT_FILE_NAME) { 189 struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr + 190 le16_to_cpu(ctx->attr->data.resident.value_offset)); 191 192 parent_vi = ntfs_iget(vi->i_sb, MREF_LE(fn->parent_directory)); 193 if (IS_ERR(parent_vi)) 194 continue; 195 mutex_lock_nested(&NTFS_I(parent_vi)->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 196 ia_vi = ntfs_index_iget(parent_vi, I30, 4); 197 mutex_unlock(&NTFS_I(parent_vi)->mrec_lock); 198 if (IS_ERR(ia_vi)) { 199 iput(parent_vi); 200 continue; 201 } 202 write_inode_now(ia_vi, 1); 203 iput(ia_vi); 204 write_inode_now(parent_vi, 1); 205 iput(parent_vi); 206 } else if (ctx->attr->non_resident) { 207 struct inode *attr_vi; 208 __le16 *name; 209 210 name = (__le16 *)((u8 *)ctx->attr + le16_to_cpu(ctx->attr->name_offset)); 211 if (ctx->attr->type == AT_DATA && ctx->attr->name_length == 0) 212 continue; 213 214 attr_vi = ntfs_attr_iget(vi, ctx->attr->type, 215 name, ctx->attr->name_length); 216 if (IS_ERR(attr_vi)) 217 continue; 218 spin_lock(&attr_vi->i_lock); 219 if (inode_state_read_once(attr_vi) & I_DIRTY_PAGES) { 220 spin_unlock(&attr_vi->i_lock); 221 filemap_write_and_wait(attr_vi->i_mapping); 222 } else 223 spin_unlock(&attr_vi->i_lock); 224 iput(attr_vi); 225 } 226 } 227 mutex_unlock(&ni->mrec_lock); 228 ntfs_attr_put_search_ctx(ctx); 229 230 write_inode_now(vol->mftbmp_ino, 1); 231 down_write(&vol->lcnbmp_lock); 232 write_inode_now(vol->lcnbmp_ino, 1); 233 up_write(&vol->lcnbmp_lock); 234 write_inode_now(vol->mft_ino, 1); 235 236 /* 237 * NOTE: If we were to use mapping->private_list (see ext2 and 238 * fs/buffer.c) for dirty blocks then we could optimize the below to be 239 * sync_mapping_buffers(vi->i_mapping). 240 */ 241 err = sync_blockdev(vi->i_sb->s_bdev); 242 if (unlikely(err && !ret)) 243 ret = err; 244 if (likely(!ret)) 245 ntfs_debug("Done."); 246 else 247 ntfs_warning(vi->i_sb, 248 "Failed to f%ssync inode 0x%llx. Error %u.", 249 datasync ? "data" : "", ni->mft_no, -ret); 250 if (!ret) 251 blkdev_issue_flush(vi->i_sb->s_bdev); 252 return ret; 253 } 254 255 static int ntfs_setattr_size(struct inode *vi, struct iattr *attr) 256 { 257 struct ntfs_inode *ni = NTFS_I(vi); 258 int err; 259 loff_t old_size = vi->i_size; 260 261 if (NInoCompressed(ni) || NInoEncrypted(ni) || NInoWofCompressed(ni)) { 262 ntfs_warning( 263 vi->i_sb, 264 "Changes in inode size are not supported yet for %s files.", 265 NInoEncrypted(ni) ? "encrypted" : "compressed"); 266 return -EOPNOTSUPP; 267 } 268 269 err = inode_newsize_ok(vi, attr->ia_size); 270 if (err) 271 return err; 272 273 inode_dio_wait(vi); 274 275 /* 276 * Serialize with page faults and pagecache instantiation so that 277 * readers cannot observe the size change until the attribute 278 * updates below have completed. 279 */ 280 filemap_invalidate_lock(vi->i_mapping); 281 if (attr->ia_size > old_size) { 282 truncate_pagecache(vi, old_size); 283 i_size_write(vi, attr->ia_size); 284 pagecache_isize_extended(vi, old_size, attr->ia_size); 285 } else { 286 truncate_setsize(vi, attr->ia_size); 287 } 288 289 err = ntfs_truncate_vfs(vi, attr->ia_size, old_size); 290 if (err) 291 i_size_write(vi, old_size); 292 filemap_invalidate_unlock(vi->i_mapping); 293 294 return err; 295 } 296 297 /* 298 * ntfs_setattr 299 * 300 * Called from notify_change() when an attribute is being changed. 301 * 302 * NOTE: Changes in inode size are not supported yet for compressed or 303 * encrypted files. 304 */ 305 int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 306 struct iattr *attr) 307 { 308 struct inode *vi = d_inode(dentry); 309 int err; 310 unsigned int ia_valid = attr->ia_valid; 311 struct ntfs_inode *ni = NTFS_I(vi); 312 struct ntfs_volume *vol = ni->vol; 313 314 if (NVolShutdown(vol)) 315 return -EIO; 316 317 err = setattr_prepare(idmap, dentry, attr); 318 if (err) 319 goto out; 320 321 if ((ia_valid & ATTR_SIZE) && 322 (NInoCompressed(ni) || NInoEncrypted(ni) || 323 NInoWofCompressed(ni))) { 324 err = -EOPNOTSUPP; 325 goto out; 326 } 327 328 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 329 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 330 331 if (ia_valid & ATTR_SIZE) { 332 err = ntfs_setattr_size(vi, attr); 333 if (err) 334 goto out; 335 336 ia_valid |= ATTR_MTIME | ATTR_CTIME; 337 } 338 339 setattr_copy(idmap, vi, attr); 340 341 if (vol->sb->s_flags & SB_POSIXACL && !S_ISLNK(vi->i_mode)) { 342 err = posix_acl_chmod(idmap, dentry, vi->i_mode); 343 if (err) 344 goto out; 345 } 346 347 if (0222 & vi->i_mode) 348 ni->flags &= ~FILE_ATTR_READONLY; 349 else 350 ni->flags |= FILE_ATTR_READONLY; 351 352 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) { 353 unsigned int flags = 0; 354 355 if (ia_valid & ATTR_UID) 356 flags |= NTFS_EA_UID; 357 if (ia_valid & ATTR_GID) 358 flags |= NTFS_EA_GID; 359 if (ia_valid & ATTR_MODE) 360 flags |= NTFS_EA_MODE; 361 362 mutex_lock(&ni->mrec_lock); 363 err = ntfs_ea_set_wsl_inode(vi, 0, NULL, flags); 364 mutex_unlock(&ni->mrec_lock); 365 if (err) 366 goto out; 367 368 } 369 370 mark_inode_dirty(vi); 371 out: 372 return err; 373 } 374 375 int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path, 376 struct kstat *stat, unsigned int request_mask, 377 unsigned int query_flags) 378 { 379 struct inode *inode = d_backing_inode(path->dentry); 380 struct ntfs_inode *ni = NTFS_I(inode); 381 382 generic_fillattr(idmap, request_mask, inode, stat); 383 384 stat->blksize = NTFS_SB(inode->i_sb)->cluster_size; 385 stat->blocks = (((u64)NTFS_I(inode)->i_dealloc_clusters << 386 NTFS_SB(inode->i_sb)->cluster_size_bits) >> 9) + inode->i_blocks; 387 stat->result_mask |= STATX_BTIME; 388 stat->btime = NTFS_I(inode)->i_crtime; 389 390 if (NInoCompressed(ni) || NInoWofCompressed(ni)) 391 stat->attributes |= STATX_ATTR_COMPRESSED; 392 393 if (NInoEncrypted(ni)) 394 stat->attributes |= STATX_ATTR_ENCRYPTED; 395 396 if (inode->i_flags & S_IMMUTABLE) 397 stat->attributes |= STATX_ATTR_IMMUTABLE; 398 399 if (inode->i_flags & S_APPEND) 400 stat->attributes |= STATX_ATTR_APPEND; 401 402 stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED | 403 STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND; 404 405 /* 406 * If it's a compressed or encrypted file, NTFS currently 407 * does not support DIO. For normal files, we report the bdev 408 * logical block size. 409 */ 410 if (request_mask & STATX_DIOALIGN && S_ISREG(inode->i_mode)) { 411 unsigned int align = 412 bdev_logical_block_size(inode->i_sb->s_bdev); 413 414 stat->result_mask |= STATX_DIOALIGN; 415 if (!NInoCompressed(ni) && !NInoEncrypted(ni) && 416 !NInoWofCompressed(ni)) { 417 stat->dio_mem_align = align; 418 stat->dio_offset_align = align; 419 } 420 } 421 422 return 0; 423 } 424 425 static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence) 426 { 427 struct inode *inode = file->f_mapping->host; 428 429 if (NInoWofCompressed(NTFS_I(inode)) && 430 (whence == SEEK_HOLE || whence == SEEK_DATA)) 431 return -EOPNOTSUPP; 432 433 switch (whence) { 434 case SEEK_HOLE: 435 inode_lock_shared(inode); 436 offset = iomap_seek_hole(inode, offset, &ntfs_seek_iomap_ops); 437 inode_unlock_shared(inode); 438 break; 439 case SEEK_DATA: 440 inode_lock_shared(inode); 441 offset = iomap_seek_data(inode, offset, &ntfs_seek_iomap_ops); 442 inode_unlock_shared(inode); 443 break; 444 default: 445 return generic_file_llseek_size(file, offset, whence, 446 inode->i_sb->s_maxbytes, 447 i_size_read(inode)); 448 } 449 if (offset < 0) 450 return offset; 451 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes); 452 } 453 454 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 455 { 456 struct inode *vi = file_inode(iocb->ki_filp); 457 struct super_block *sb = vi->i_sb; 458 ssize_t ret; 459 460 if (NVolShutdown(NTFS_SB(sb))) 461 return -EIO; 462 463 if ((NInoCompressed(NTFS_I(vi)) || NInoWofCompressed(NTFS_I(vi))) && 464 iocb->ki_flags & IOCB_DIRECT) 465 return -EOPNOTSUPP; 466 467 inode_lock_shared(vi); 468 469 if (iocb->ki_flags & IOCB_DIRECT) { 470 size_t count = iov_iter_count(to); 471 472 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) { 473 ret = -EINVAL; 474 goto inode_unlock; 475 } 476 477 file_accessed(iocb->ki_filp); 478 ret = iomap_dio_rw(iocb, to, &ntfs_read_iomap_ops, NULL, 0, 479 NULL, 0); 480 } else { 481 ret = generic_file_read_iter(iocb, to); 482 } 483 484 inode_unlock: 485 inode_unlock_shared(vi); 486 487 return ret; 488 } 489 490 static int ntfs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size, 491 int error, unsigned int flags) 492 { 493 struct inode *inode = file_inode(iocb->ki_filp); 494 495 if (error) 496 return error; 497 498 if (size) { 499 if (i_size_read(inode) < iocb->ki_pos + size) { 500 i_size_write(inode, iocb->ki_pos + size); 501 mark_inode_dirty(inode); 502 } 503 } 504 505 return 0; 506 } 507 508 static const struct iomap_dio_ops ntfs_write_dio_ops = { 509 .end_io = ntfs_file_write_dio_end_io, 510 }; 511 512 static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from) 513 { 514 ssize_t ret; 515 516 ret = iomap_dio_rw(iocb, from, &ntfs_dio_iomap_ops, 517 &ntfs_write_dio_ops, 0, NULL, 0); 518 if (ret == -ENOTBLK) 519 ret = 0; 520 else if (ret < 0) 521 goto out; 522 523 if (iov_iter_count(from)) { 524 loff_t offset, end; 525 ssize_t written; 526 int ret2; 527 528 offset = iocb->ki_pos; 529 iocb->ki_flags &= ~IOCB_DIRECT; 530 written = iomap_file_buffered_write(iocb, from, 531 &ntfs_write_iomap_ops, &ntfs_iomap_folio_ops, 532 NULL); 533 if (written < 0) { 534 ret = written; 535 goto out; 536 } 537 538 ret += written; 539 end = iocb->ki_pos + written - 1; 540 ret2 = filemap_write_and_wait_range(iocb->ki_filp->f_mapping, 541 offset, end); 542 if (ret2) { 543 ret = -EIO; 544 goto out; 545 } 546 invalidate_mapping_pages(iocb->ki_filp->f_mapping, 547 offset >> PAGE_SHIFT, 548 end >> PAGE_SHIFT); 549 } 550 551 out: 552 return ret; 553 } 554 555 static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end) 556 { 557 struct ntfs_volume *vol = ni->vol; 558 loff_t prealloc_size = 0; 559 int err; 560 561 if (end <= ni->data_size) 562 return 0; 563 564 if (NInoCompressed(ni)) { 565 if (end > ni->allocated_size) 566 prealloc_size = round_up(end, 567 ni->itype.compressed.block_size); 568 } else if (end > ni->allocated_size && 569 end < ni->allocated_size + vol->preallocated_size) { 570 prealloc_size = ni->allocated_size + vol->preallocated_size; 571 } 572 573 mutex_lock(&ni->mrec_lock); 574 err = ntfs_attr_expand(ni, end, prealloc_size); 575 mutex_unlock(&ni->mrec_lock); 576 577 return err; 578 } 579 580 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 581 { 582 struct file *file = iocb->ki_filp; 583 struct inode *vi = file->f_mapping->host; 584 struct ntfs_inode *ni = NTFS_I(vi); 585 struct ntfs_volume *vol = ni->vol; 586 ssize_t ret; 587 ssize_t count; 588 loff_t pos, end; 589 int err; 590 loff_t old_data_size, old_init_size; 591 592 if (NVolShutdown(vol)) 593 return -EIO; 594 595 if (NInoWofCompressed(ni)) 596 return -EOPNOTSUPP; 597 598 if (NInoEncrypted(ni)) { 599 ntfs_error(vi->i_sb, "Writing for %s files is not supported yet", 600 NInoCompressed(ni) ? "Compressed" : "Encrypted"); 601 return -EOPNOTSUPP; 602 } 603 604 if (NInoCompressed(ni) && iocb->ki_flags & IOCB_DIRECT) 605 return -EOPNOTSUPP; 606 607 if (iocb->ki_flags & IOCB_NOWAIT) { 608 if (!inode_trylock(vi)) 609 return -EAGAIN; 610 } else 611 inode_lock(vi); 612 613 ret = generic_write_checks(iocb, from); 614 if (ret <= 0) 615 goto out_lock; 616 617 err = file_modified(iocb->ki_filp); 618 if (err) { 619 ret = err; 620 goto out_lock; 621 } 622 623 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 624 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 625 626 pos = iocb->ki_pos; 627 count = ret; 628 end = pos + count; 629 630 old_data_size = ni->data_size; 631 old_init_size = ni->initialized_size; 632 633 if (end > old_data_size) { 634 ret = ntfs_expand_for_write(ni, end); 635 if (ret < 0) 636 goto out; 637 } 638 639 if (NInoNonResident(ni) && !NInoCompressed(ni) && 640 end > old_init_size) { 641 ret = ntfs_extend_initialized_size(vi, pos, end); 642 if (ret < 0) 643 goto out; 644 } 645 646 if (NInoNonResident(ni) && NInoCompressed(ni)) { 647 ret = ntfs_compress_write(ni, pos, count, from); 648 if (ret > 0) 649 iocb->ki_pos += ret; 650 goto out; 651 } 652 653 if (NInoNonResident(ni) && iocb->ki_flags & IOCB_DIRECT) 654 ret = ntfs_dio_write_iter(iocb, from); 655 else 656 ret = iomap_file_buffered_write(iocb, from, &ntfs_write_iomap_ops, 657 &ntfs_iomap_folio_ops, NULL); 658 out: 659 if (ret < 0 && ret != -EIOCBQUEUED) { 660 if (ni->initialized_size != old_init_size) { 661 mutex_lock(&ni->mrec_lock); 662 ntfs_attr_set_initialized_size(ni, old_init_size); 663 mutex_unlock(&ni->mrec_lock); 664 } 665 if (ni->data_size != old_data_size) { 666 truncate_setsize(vi, old_data_size); 667 ntfs_attr_truncate(ni, old_data_size); 668 } 669 } 670 out_lock: 671 inode_unlock(vi); 672 if (ret > 0) 673 ret = generic_write_sync(iocb, ret); 674 return ret; 675 } 676 677 static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf) 678 { 679 struct inode *inode = file_inode(vmf->vma->vm_file); 680 struct address_space *mapping = inode->i_mapping; 681 vm_fault_t ret; 682 683 if (NInoWofCompressed(NTFS_I(inode))) 684 return VM_FAULT_SIGBUS; 685 686 sb_start_pagefault(inode->i_sb); 687 file_update_time(vmf->vma->vm_file); 688 689 /* 690 * Serialize against truncate/fallocate which hold the lock 691 * exclusively while invalidating pagecache and changing extents. 692 */ 693 filemap_invalidate_lock_shared(mapping); 694 ret = iomap_page_mkwrite(vmf, &ntfs_page_mkwrite_iomap_ops, NULL); 695 filemap_invalidate_unlock_shared(mapping); 696 697 sb_end_pagefault(inode->i_sb); 698 return ret; 699 } 700 701 static const struct vm_operations_struct ntfs_file_vm_ops = { 702 .fault = filemap_fault, 703 .map_pages = filemap_map_pages, 704 .page_mkwrite = ntfs_filemap_page_mkwrite, 705 }; 706 707 static int ntfs_file_mmap_prepare(struct vm_area_desc *desc) 708 { 709 struct file *file = desc->file; 710 struct inode *inode = file_inode(file); 711 712 if (NVolShutdown(NTFS_SB(file->f_mapping->host->i_sb))) 713 return -EIO; 714 715 if (NInoCompressed(NTFS_I(inode)) || NInoWofCompressed(NTFS_I(inode))) 716 return -EOPNOTSUPP; 717 718 if (vma_desc_test_all(desc, VMA_SHARED_BIT, VMA_MAYWRITE_BIT)) { 719 struct inode *inode = file_inode(file); 720 loff_t from, to; 721 int err; 722 723 from = ((loff_t)desc->pgoff << PAGE_SHIFT); 724 to = min_t(loff_t, i_size_read(inode), 725 from + desc->end - desc->start); 726 727 if (NTFS_I(inode)->initialized_size < to) { 728 err = ntfs_extend_initialized_size(inode, to, to); 729 if (err) 730 return err; 731 } 732 } 733 734 735 file_accessed(file); 736 desc->vm_ops = &ntfs_file_vm_ops; 737 return 0; 738 } 739 740 static int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 741 u64 start, u64 len) 742 { 743 if (NInoWofCompressed(NTFS_I(inode))) 744 return -EOPNOTSUPP; 745 746 return iomap_fiemap(inode, fieinfo, start, len, &ntfs_read_iomap_ops); 747 } 748 749 static const char *ntfs_get_link(struct dentry *dentry, struct inode *inode, 750 struct delayed_call *done) 751 { 752 struct ntfs_inode *ni = NTFS_I(inode); 753 char *target; 754 int err; 755 756 if (!dentry) 757 return ERR_PTR(-ECHILD); 758 759 if (!ni->target) 760 return ERR_PTR(-EINVAL); 761 762 if (ni->reparse_tag == IO_REPARSE_TAG_MOUNT_POINT || 763 (ni->reparse_tag == IO_REPARSE_TAG_SYMLINK && 764 !(ni->reparse_flags & cpu_to_le32(SYMLINK_FLAG_RELATIVE)))) { 765 if (NVolNativeSymlinkRel(ni->vol)) { 766 err = ntfs_translate_symlink_path(dentry, ni->target, &target); 767 if (err < 0) 768 return ERR_PTR(err); 769 set_delayed_call(done, kfree_link, target); 770 return target; 771 } 772 } 773 774 return ni->target; 775 } 776 777 static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos, 778 struct pipe_inode_info *pipe, size_t len, unsigned int flags) 779 { 780 if (NVolShutdown(NTFS_SB(in->f_mapping->host->i_sb))) 781 return -EIO; 782 783 return filemap_splice_read(in, ppos, pipe, len, flags); 784 } 785 786 static int ntfs_ioctl_shutdown(struct super_block *sb, unsigned long arg) 787 { 788 u32 flags; 789 790 if (!capable(CAP_SYS_ADMIN)) 791 return -EPERM; 792 793 if (get_user(flags, (__u32 __user *)arg)) 794 return -EFAULT; 795 796 return ntfs_force_shutdown(sb, flags); 797 } 798 799 static int ntfs_ioctl_get_volume_label(struct file *filp, unsigned long arg) 800 { 801 struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb); 802 char __user *buf = (char __user *)arg; 803 char label[FSLABEL_MAX]; 804 ssize_t len; 805 806 mutex_lock(&vol->volume_label_lock); 807 if (!vol->volume_label) { 808 label[0] = '\0'; 809 len = 0; 810 } else { 811 len = strscpy(label, vol->volume_label, sizeof(label)); 812 if (len == -E2BIG) 813 len = FSLABEL_MAX - 1; 814 } 815 mutex_unlock(&vol->volume_label_lock); 816 817 if (copy_to_user(buf, label, len + 1)) 818 return -EFAULT; 819 return 0; 820 } 821 822 static int ntfs_ioctl_set_volume_label(struct file *filp, unsigned long arg) 823 { 824 struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb); 825 char *label; 826 int ret; 827 828 if (!capable(CAP_SYS_ADMIN)) 829 return -EPERM; 830 831 label = strndup_user((const char __user *)arg, FSLABEL_MAX); 832 if (IS_ERR(label)) 833 return PTR_ERR(label); 834 835 ret = mnt_want_write_file(filp); 836 if (ret) 837 goto out; 838 839 ret = ntfs_write_volume_label(vol, label); 840 mnt_drop_write_file(filp); 841 out: 842 kfree(label); 843 return ret; 844 } 845 846 static int ntfs_ioctl_fitrim(struct ntfs_volume *vol, unsigned long arg) 847 { 848 struct fstrim_range __user *user_range; 849 struct fstrim_range range; 850 struct block_device *dev; 851 int err; 852 853 if (!capable(CAP_SYS_ADMIN)) 854 return -EPERM; 855 856 dev = vol->sb->s_bdev; 857 if (!bdev_max_discard_sectors(dev)) 858 return -EOPNOTSUPP; 859 860 user_range = (struct fstrim_range __user *)arg; 861 if (copy_from_user(&range, user_range, sizeof(range))) 862 return -EFAULT; 863 864 if (range.len == 0) 865 return -EINVAL; 866 867 if (range.len < vol->cluster_size) 868 return -EINVAL; 869 870 range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev)); 871 872 err = ntfs_trim_fs(vol, &range); 873 if (err < 0) 874 return err; 875 876 if (copy_to_user(user_range, &range, sizeof(range))) 877 return -EFAULT; 878 879 return 0; 880 } 881 882 long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 883 { 884 switch (cmd) { 885 case FS_IOC_SHUTDOWN: 886 return ntfs_ioctl_shutdown(file_inode(filp)->i_sb, arg); 887 case FS_IOC_GETFSLABEL: 888 return ntfs_ioctl_get_volume_label(filp, arg); 889 case FS_IOC_SETFSLABEL: 890 return ntfs_ioctl_set_volume_label(filp, arg); 891 case FITRIM: 892 return ntfs_ioctl_fitrim(NTFS_SB(file_inode(filp)->i_sb), arg); 893 default: 894 return -ENOTTY; 895 } 896 } 897 898 #ifdef CONFIG_COMPAT 899 long ntfs_compat_ioctl(struct file *filp, unsigned int cmd, 900 unsigned long arg) 901 { 902 return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); 903 } 904 #endif 905 906 static int ntfs_allocate_range(struct ntfs_inode *ni, int mode, loff_t offset, 907 loff_t len) 908 { 909 struct inode *vi = VFS_I(ni); 910 struct ntfs_volume *vol = ni->vol; 911 s64 need_space; 912 loff_t old_size, new_size; 913 s64 start_vcn, end_vcn; 914 int err; 915 916 old_size = i_size_read(vi); 917 new_size = max_t(loff_t, old_size, offset + len); 918 start_vcn = ntfs_bytes_to_cluster(vol, offset); 919 end_vcn = ntfs_bytes_to_cluster(vol, offset + len - 1) + 1; 920 921 err = inode_newsize_ok(vi, new_size); 922 if (err) 923 goto out; 924 925 need_space = ntfs_bytes_to_cluster(vol, ni->allocated_size); 926 if (need_space > start_vcn) 927 need_space = end_vcn - need_space; 928 else 929 need_space = end_vcn - start_vcn; 930 if (need_space > 0 && 931 need_space > (atomic64_read(&vol->free_clusters) - 932 atomic64_read(&vol->dirty_clusters))) { 933 err = -ENOSPC; 934 goto out; 935 } 936 937 err = ntfs_attr_fallocate(ni, offset, len, 938 mode & FALLOC_FL_KEEP_SIZE ? true : false); 939 940 if (!(mode & FALLOC_FL_KEEP_SIZE) && new_size != old_size) 941 i_size_write(vi, ni->data_size); 942 out: 943 return err; 944 } 945 946 static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset, 947 loff_t len) 948 { 949 struct ntfs_volume *vol = ni->vol; 950 struct inode *vi = VFS_I(ni); 951 loff_t end_offset; 952 s64 start_vcn, end_vcn; 953 int err = 0; 954 955 loff_t offset_down = round_down(offset, max_t(unsigned int, 956 vol->cluster_size, PAGE_SIZE)); 957 958 if (NVolDisableSparse(vol)) { 959 err = -EOPNOTSUPP; 960 goto out; 961 } 962 963 if (offset >= ni->data_size) 964 goto out; 965 966 if (offset + len > ni->data_size) 967 end_offset = ni->data_size; 968 else 969 end_offset = offset + len; 970 971 err = filemap_write_and_wait_range(vi->i_mapping, offset_down, LLONG_MAX); 972 if (err) 973 goto out; 974 truncate_pagecache(vi, offset_down); 975 976 start_vcn = ntfs_bytes_to_cluster(vol, offset); 977 end_vcn = ntfs_bytes_to_cluster(vol, end_offset - 1) + 1; 978 979 if (offset & vol->cluster_size_mask) { 980 if (offset < ni->initialized_size) { 981 loff_t to; 982 983 to = min_t(loff_t, 984 ntfs_cluster_to_bytes(vol, start_vcn + 1), 985 end_offset); 986 err = iomap_zero_range(vi, offset, to - offset, 987 NULL, &ntfs_seek_iomap_ops, 988 &ntfs_iomap_folio_ops, NULL); 989 if (err < 0) 990 goto out; 991 } 992 if (end_vcn - start_vcn == 1) 993 goto out; 994 start_vcn++; 995 } 996 997 if (end_offset & vol->cluster_size_mask) { 998 loff_t from; 999 1000 from = ntfs_cluster_to_bytes(vol, end_vcn - 1); 1001 if (from < ni->initialized_size) { 1002 err = iomap_zero_range(vi, from, end_offset - from, 1003 NULL, &ntfs_seek_iomap_ops, 1004 &ntfs_iomap_folio_ops, NULL); 1005 if (err < 0) 1006 goto out; 1007 } 1008 if (end_vcn - start_vcn == 1) 1009 goto out; 1010 end_vcn--; 1011 } 1012 1013 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1014 err = ntfs_non_resident_attr_punch_hole(ni, start_vcn, 1015 end_vcn - start_vcn); 1016 mutex_unlock(&ni->mrec_lock); 1017 out: 1018 return err; 1019 } 1020 1021 static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len) 1022 { 1023 struct ntfs_volume *vol = ni->vol; 1024 struct inode *vi = VFS_I(ni); 1025 loff_t old_size, new_size; 1026 s64 start_vcn, end_vcn; 1027 int err; 1028 1029 loff_t offset_down = round_down(offset, 1030 max_t(unsigned long, vol->cluster_size, PAGE_SIZE)); 1031 1032 if ((offset & vol->cluster_size_mask) || 1033 (len & vol->cluster_size_mask) || 1034 offset >= ni->allocated_size) { 1035 err = -EINVAL; 1036 goto out; 1037 } 1038 1039 old_size = i_size_read(vi); 1040 start_vcn = ntfs_bytes_to_cluster(vol, offset); 1041 end_vcn = ntfs_bytes_to_cluster(vol, offset + len - 1) + 1; 1042 1043 if (ntfs_cluster_to_bytes(vol, end_vcn) > ni->allocated_size) 1044 end_vcn = (round_up(ni->allocated_size - 1, 1045 vol->cluster_size) >> vol->cluster_size_bits) + 1; 1046 new_size = old_size - ntfs_cluster_to_bytes(vol, end_vcn - start_vcn); 1047 if (new_size < 0) 1048 new_size = 0; 1049 err = filemap_write_and_wait_range(vi->i_mapping, 1050 offset_down, LLONG_MAX); 1051 if (err) 1052 goto out; 1053 1054 truncate_pagecache(vi, offset_down); 1055 1056 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1057 err = ntfs_non_resident_attr_collapse_range(ni, start_vcn, 1058 end_vcn - start_vcn); 1059 mutex_unlock(&ni->mrec_lock); 1060 1061 if (new_size != old_size) 1062 i_size_write(vi, ni->data_size); 1063 out: 1064 return err; 1065 } 1066 1067 static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len) 1068 { 1069 struct ntfs_volume *vol = ni->vol; 1070 struct inode *vi = VFS_I(ni); 1071 loff_t offset_down = round_down(offset, 1072 max_t(unsigned long, vol->cluster_size, PAGE_SIZE)); 1073 loff_t alloc_size, end_offset = offset + len; 1074 loff_t old_size, new_size; 1075 s64 start_vcn, end_vcn; 1076 int err; 1077 1078 if (NVolDisableSparse(vol)) { 1079 err = -EOPNOTSUPP; 1080 goto out; 1081 } 1082 1083 if ((offset & vol->cluster_size_mask) || 1084 (len & vol->cluster_size_mask) || 1085 offset >= ni->allocated_size) { 1086 err = -EINVAL; 1087 goto out; 1088 } 1089 1090 old_size = i_size_read(vi); 1091 start_vcn = ntfs_bytes_to_cluster(vol, offset); 1092 end_vcn = ntfs_bytes_to_cluster(vol, end_offset - 1) + 1; 1093 1094 new_size = old_size + ntfs_cluster_to_bytes(vol, end_vcn - start_vcn); 1095 alloc_size = ni->allocated_size + 1096 ntfs_cluster_to_bytes(vol, end_vcn - start_vcn); 1097 if (alloc_size < 0) { 1098 err = -EFBIG; 1099 goto out; 1100 } 1101 err = inode_newsize_ok(vi, alloc_size); 1102 if (err) 1103 goto out; 1104 1105 err = filemap_write_and_wait_range(vi->i_mapping, 1106 offset_down, LLONG_MAX); 1107 if (err) 1108 goto out; 1109 1110 truncate_pagecache(vi, offset_down); 1111 1112 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1113 err = ntfs_non_resident_attr_insert_range(ni, start_vcn, 1114 end_vcn - start_vcn); 1115 mutex_unlock(&ni->mrec_lock); 1116 1117 if (new_size != old_size) 1118 i_size_write(vi, ni->data_size); 1119 out: 1120 return err; 1121 } 1122 1123 #define NTFS_FALLOC_FL_SUPPORTED \ 1124 (FALLOC_FL_ALLOCATE_RANGE | FALLOC_FL_KEEP_SIZE | \ 1125 FALLOC_FL_INSERT_RANGE | FALLOC_FL_PUNCH_HOLE | \ 1126 FALLOC_FL_COLLAPSE_RANGE) 1127 1128 static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len) 1129 { 1130 struct inode *vi = file_inode(file); 1131 struct ntfs_inode *ni = NTFS_I(vi); 1132 struct ntfs_volume *vol = ni->vol; 1133 int err = 0; 1134 loff_t old_size; 1135 1136 if (mode & ~(NTFS_FALLOC_FL_SUPPORTED)) 1137 return -EOPNOTSUPP; 1138 1139 if (NInoCompressed(ni) || NInoEncrypted(ni) || NInoWofCompressed(ni)) 1140 return -EOPNOTSUPP; 1141 1142 if (!NVolFreeClusterKnown(vol)) 1143 wait_event(vol->free_waitq, NVolFreeClusterKnown(vol)); 1144 1145 if ((ni->vol->mft_zone_end - ni->vol->mft_zone_start) == 0) 1146 return -ENOSPC; 1147 1148 if (NInoNonResident(ni) && !NInoFullyMapped(ni)) { 1149 down_write(&ni->runlist.lock); 1150 err = ntfs_attr_map_whole_runlist(ni); 1151 up_write(&ni->runlist.lock); 1152 if (err) 1153 return err; 1154 } 1155 1156 if (!(vol->vol_flags & VOLUME_IS_DIRTY)) { 1157 err = ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1158 if (err) 1159 return err; 1160 } 1161 1162 old_size = i_size_read(vi); 1163 1164 inode_lock(vi); 1165 if (NInoCompressed(ni) || NInoEncrypted(ni) || NInoWofCompressed(ni)) { 1166 inode_unlock(vi); 1167 return -EOPNOTSUPP; 1168 } 1169 1170 inode_dio_wait(vi); 1171 /* Take invalidate_lock for all fallocate operations to prevent races */ 1172 filemap_invalidate_lock(vi->i_mapping); 1173 1174 switch (mode & FALLOC_FL_MODE_MASK) { 1175 case FALLOC_FL_ALLOCATE_RANGE: 1176 case FALLOC_FL_KEEP_SIZE: 1177 err = ntfs_allocate_range(ni, mode, offset, len); 1178 break; 1179 case FALLOC_FL_PUNCH_HOLE: 1180 err = ntfs_punch_hole(ni, mode, offset, len); 1181 break; 1182 case FALLOC_FL_COLLAPSE_RANGE: 1183 err = ntfs_collapse_range(ni, offset, len); 1184 break; 1185 case FALLOC_FL_INSERT_RANGE: 1186 err = ntfs_insert_range(ni, offset, len); 1187 break; 1188 default: 1189 err = -EOPNOTSUPP; 1190 } 1191 1192 if (err) 1193 goto out; 1194 1195 err = file_modified(file); 1196 out: 1197 if (!err && mode == 0 && NInoNonResident(ni) && 1198 offset > old_size) { 1199 truncate_pagecache(vi, old_size); 1200 pagecache_isize_extended(vi, old_size, offset); 1201 } 1202 1203 filemap_invalidate_unlock(vi->i_mapping); 1204 1205 if (!err) { 1206 NInoSetFileNameDirty(ni); 1207 inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi)); 1208 mark_inode_dirty(vi); 1209 } 1210 1211 inode_unlock(vi); 1212 return err; 1213 } 1214 1215 const struct file_operations ntfs_file_ops = { 1216 .llseek = ntfs_file_llseek, 1217 .read_iter = ntfs_file_read_iter, 1218 .write_iter = ntfs_file_write_iter, 1219 .fsync = ntfs_file_fsync, 1220 .mmap_prepare = ntfs_file_mmap_prepare, 1221 .open = ntfs_file_open, 1222 .release = ntfs_file_release, 1223 .splice_read = ntfs_file_splice_read, 1224 .splice_write = iter_file_splice_write, 1225 .unlocked_ioctl = ntfs_ioctl, 1226 #ifdef CONFIG_COMPAT 1227 .compat_ioctl = ntfs_compat_ioctl, 1228 #endif 1229 .fallocate = ntfs_fallocate, 1230 .setlease = generic_setlease, 1231 }; 1232 1233 const struct inode_operations ntfs_file_inode_ops = { 1234 .setattr = ntfs_setattr, 1235 .getattr = ntfs_getattr, 1236 .listxattr = ntfs_listxattr, 1237 .get_acl = ntfs_get_acl, 1238 .set_acl = ntfs_set_acl, 1239 .fiemap = ntfs_fiemap, 1240 }; 1241 1242 const struct inode_operations ntfs_symlink_inode_operations = { 1243 .get_link = ntfs_get_link, 1244 .setattr = ntfs_setattr, 1245 .listxattr = ntfs_listxattr, 1246 }; 1247 1248 const struct inode_operations ntfs_special_inode_operations = { 1249 .setattr = ntfs_setattr, 1250 .getattr = ntfs_getattr, 1251 .listxattr = ntfs_listxattr, 1252 .get_acl = ntfs_get_acl, 1253 .set_acl = ntfs_set_acl, 1254 }; 1255 1256 const struct file_operations ntfs_empty_file_ops = {}; 1257 1258 const struct inode_operations ntfs_empty_inode_ops = {}; 1259