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