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