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 #include <linux/fileattr.h> 19 #include <linux/file.h> 20 #include <linux/filelock.h> 21 #include <linux/list.h> 22 #include <linux/overflow.h> 23 #include <linux/security.h> 24 #include <linux/slab.h> 25 #include <uapi/linux/ntfs.h> 26 27 #include "lcnalloc.h" 28 #include "ntfs.h" 29 #include "reparse.h" 30 #include "ea.h" 31 #include "iomap.h" 32 #include "bitmap.h" 33 #include "volume.h" 34 #include "mft.h" 35 #include "stream.h" 36 37 /* 38 * ntfs_file_open - called when an inode is about to be opened 39 * @vi: inode to be opened 40 * @filp: file structure describing the inode 41 * 42 * Limit file size to the page cache limit on architectures where unsigned long 43 * is 32-bits. This is the most we can do for now without overflowing the page 44 * cache page index. Doing it this way means we don't run into problems because 45 * of existing too large files. It would be better to allow the user to read 46 * the beginning of the file but I doubt very much anyone is going to hit this 47 * check on a 32-bit architecture, so there is no point in adding the extra 48 * complexity required to support this. 49 * 50 * On 64-bit architectures, the check is hopefully optimized away by the 51 * compiler. 52 * 53 * After the check passes, just call generic_file_open() to do its work. 54 */ 55 int ntfs_file_open(struct inode *vi, struct file *filp) 56 { 57 struct ntfs_inode *ni = NTFS_I(vi); 58 59 if (NVolShutdown(ni->vol)) 60 return -EIO; 61 62 if (sizeof(unsigned long) < 8) { 63 if (i_size_read(vi) > MAX_LFS_FILESIZE) 64 return -EOVERFLOW; 65 } 66 67 filp->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT; 68 69 return generic_file_open(vi, filp); 70 } 71 72 /* 73 * Trim preallocated space on file release. 74 * 75 * When the preallo_size mount option is set (default 64KB), writes extend 76 * allocated_size and runlist in units of preallocated size to reduce 77 * runlist merge overhead for small writes. This can leave 78 * allocated_size > data_size if not all preallocated space is used. 79 * 80 * We perform the trim here because ->release() is called only when 81 * the file is no longer open. At this point, no further writes can occur, 82 * so it is safe to reclaim the unused preallocated space. 83 * 84 * Returns 0 on success, or negative error on failure. 85 */ 86 static int ntfs_trim_prealloc(struct inode *vi) 87 { 88 struct ntfs_inode *ni = NTFS_I(vi); 89 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 90 struct ntfs_volume *vol = ni->vol; 91 struct runlist_element *rl; 92 s64 aligned_data_size; 93 s64 vcn_ds, vcn_tr; 94 ssize_t rc; 95 int err = 0; 96 97 inode_lock(vi); 98 mutex_lock(&mrec_ni->mrec_lock); 99 down_write(&ni->runlist.lock); 100 101 aligned_data_size = round_up(ni->data_size, vol->cluster_size); 102 if (aligned_data_size >= ni->allocated_size) 103 goto out_unlock; 104 105 vcn_ds = ntfs_bytes_to_cluster(vol, aligned_data_size); 106 vcn_tr = -1; 107 rc = ni->runlist.count - 2; 108 rl = ni->runlist.rl; 109 110 while (rc >= 0 && rl[rc].lcn == LCN_HOLE && vcn_ds <= rl[rc].vcn) { 111 vcn_tr = rl[rc].vcn; 112 rc--; 113 } 114 115 if (vcn_tr >= 0) { 116 err = ntfs_rl_truncate_nolock(vol, &ni->runlist, vcn_tr); 117 if (err) { 118 kvfree(ni->runlist.rl); 119 ni->runlist.rl = NULL; 120 ntfs_error(vol->sb, "Preallocated block rollback failed"); 121 } else { 122 ni->allocated_size = ntfs_cluster_to_bytes(vol, vcn_tr); 123 err = ntfs_attr_update_mapping_pairs_locked( 124 ni, 0, ni); 125 if (err) 126 ntfs_error(vol->sb, 127 "Failed to rollback mapping pairs for prealloc"); 128 } 129 } 130 131 out_unlock: 132 up_write(&ni->runlist.lock); 133 mutex_unlock(&mrec_ni->mrec_lock); 134 inode_unlock(vi); 135 136 return err; 137 } 138 139 int ntfs_file_release(struct inode *vi, struct file *filp) 140 { 141 if (!NInoCompressed(NTFS_I(vi)) && 142 !NInoWofCompressed(NTFS_I(vi))) 143 return ntfs_trim_prealloc(vi); 144 145 return 0; 146 } 147 148 /* 149 * ntfs_fileattr_get - inode_operations::fileattr_get 150 * @dentry: dentry to report the flags of 151 * @fa: filled in with the flags of @dentry 152 */ 153 int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa) 154 { 155 struct inode *vi = d_inode(dentry); 156 struct ntfs_inode *ni = NTFS_I(vi); 157 u32 flags = 0; 158 159 if (NVolShutdown(ni->vol)) 160 return -EIO; 161 162 if (NInoCompressed(ni) || NInoWofCompressed(ni)) 163 flags |= FS_COMPR_FL; 164 if (NInoEncrypted(ni)) 165 flags |= FS_ENCRYPT_FL; 166 if (vi->i_flags & S_IMMUTABLE) 167 flags |= FS_IMMUTABLE_FL; 168 if (vi->i_flags & S_APPEND) 169 flags |= FS_APPEND_FL; 170 if (vi->i_flags & S_NOATIME) 171 flags |= FS_NOATIME_FL; 172 if (ni->linuxflags & NTFS_LINUXFLAGS_NODUMP) 173 flags |= FS_NODUMP_FL; 174 if (!NVolCaseSensitive(ni->vol)) 175 flags |= FS_CASEFOLD_FL; 176 177 fileattr_fill_flags(fa, flags); 178 return 0; 179 } 180 181 static bool ntfs_is_forced_immutable(struct inode *vi) 182 { 183 struct ntfs_inode *ni = NTFS_I(vi); 184 185 if (ni->mft_no < FILE_first_user && S_ISREG(vi->i_mode)) 186 return true; 187 188 return NVolSysImmutable(ni->vol) && 189 (ni->flags & FILE_ATTR_SYSTEM) && 190 !S_ISFIFO(vi->i_mode) && !S_ISSOCK(vi->i_mode) && 191 !S_ISLNK(vi->i_mode); 192 } 193 194 /* 195 * ntfs_fileattr_set - inode_operations::fileattr_set 196 * @idmap: idmap of the mount @dentry was found from 197 * @dentry: dentry to set the flags of 198 * @fa: flags to set 199 */ 200 int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, 201 struct file_kattr *fa) 202 { 203 struct inode *vi = d_inode(dentry); 204 struct ntfs_inode *ni = NTFS_I(vi); 205 u32 allowed = FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL | 206 FS_NOATIME_FL; 207 u32 readonly = 0; 208 u32 linuxflags = ni->linuxflags & ~NTFS_LINUXFLAGS_MASK; 209 unsigned int new_fl = 0; 210 bool forced_immutable; 211 int err; 212 213 if (NVolShutdown(ni->vol)) 214 return -EIO; 215 216 if (fileattr_has_fsx(fa)) 217 return -EOPNOTSUPP; 218 219 /* chattr passes the unchanged read-only flags back to us too. */ 220 if (NInoCompressed(ni) || NInoWofCompressed(ni)) 221 readonly |= FS_COMPR_FL; 222 if (NInoEncrypted(ni)) 223 readonly |= FS_ENCRYPT_FL; 224 if ((fa->flags ^ readonly) & (FS_COMPR_FL | FS_ENCRYPT_FL)) 225 return -EOPNOTSUPP; 226 allowed |= readonly; 227 228 /* Case folding is a read-only, mount-wide property. */ 229 if (!NVolCaseSensitive(ni->vol)) 230 allowed |= FS_CASEFOLD_FL; 231 if (fa->flags & ~allowed) 232 return -EOPNOTSUPP; 233 234 forced_immutable = ntfs_is_forced_immutable(vi); 235 if (!(fa->flags & FS_IMMUTABLE_FL) && forced_immutable) 236 return -EPERM; 237 238 if (fa->flags & FS_IMMUTABLE_FL) { 239 new_fl |= S_IMMUTABLE; 240 /* Do not persist an immutable bit derived from the mount. */ 241 if (!forced_immutable || 242 (ni->linuxflags & NTFS_LINUXFLAGS_IMMUTABLE)) 243 linuxflags |= NTFS_LINUXFLAGS_IMMUTABLE; 244 } 245 if (fa->flags & FS_APPEND_FL) { 246 new_fl |= S_APPEND; 247 linuxflags |= NTFS_LINUXFLAGS_APPEND; 248 } 249 if (fa->flags & FS_NODUMP_FL) 250 linuxflags |= NTFS_LINUXFLAGS_NODUMP; 251 if (fa->flags & FS_NOATIME_FL) { 252 new_fl |= S_NOATIME; 253 linuxflags |= NTFS_LINUXFLAGS_NOATIME; 254 } 255 256 mutex_lock(&ni->mrec_lock); 257 err = ntfs_ea_set_linuxflags(vi, linuxflags); 258 mutex_unlock(&ni->mrec_lock); 259 if (err) 260 return err; 261 262 inode_set_flags(vi, new_fl, S_IMMUTABLE | S_APPEND | S_NOATIME); 263 inode_set_ctime_current(vi); 264 mark_inode_dirty(vi); 265 return 0; 266 } 267 268 struct ntfs_fsync_attr { 269 struct list_head list; 270 struct inode *inode; 271 }; 272 273 /* 274 * ntfs_file_fsync - sync a file to disk 275 * @filp: file to be synced 276 * @start: start offset to be synced 277 * @end: end offset to be synced 278 * @datasync: if non-zero only flush user data and not metadata 279 * 280 * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync 281 * system calls. This function is inspired by fs/buffer.c::file_fsync(). 282 * 283 * If @datasync is false, write the mft record and all associated extent mft 284 * records as well as the $DATA attribute and then sync the block device. 285 * 286 * If @datasync is true and the attribute is non-resident, we skip the writing 287 * of the mft record and all associated extent mft records (this might still 288 * happen due to the write_inode_now() call). 289 * 290 * Also, if @datasync is true, we do not wait on the inode to be written out 291 * but we always wait on the page cache pages to be written out. 292 */ 293 int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end, 294 int datasync) 295 { 296 struct inode *vi = filp->f_mapping->host; 297 struct ntfs_inode *ni = NTFS_I(vi); 298 struct ntfs_volume *vol = ni->vol; 299 int err, ret = 0; 300 struct inode *parent_vi, *ia_vi; 301 struct ntfs_attr_search_ctx *ctx; 302 struct ntfs_fsync_attr *attr, *next; 303 LIST_HEAD(attrs); 304 bool non_resident, stream; 305 306 ntfs_debug("Entering for inode 0x%llx.", ni->mft_no); 307 308 if (NVolShutdown(vol)) 309 return -EIO; 310 311 err = file_write_and_wait_range(filp, start, end); 312 if (err) 313 return err; 314 315 stream = ntfs_inode_is_named_stream(ni); 316 non_resident = NInoNonResident(ni); 317 if (stream) { 318 ni = ni->ext.base_ntfs_ino; 319 vi = VFS_I(ni); 320 } 321 322 if (!datasync || !non_resident) 323 ret = __ntfs_write_inode(vi, 1); 324 write_inode_now(vi, !datasync); 325 326 /* 327 * file_write_and_wait_range() already flushed this stream mapping. 328 * A stream fsync does not need to flush sibling attribute mappings. 329 */ 330 if (stream) 331 goto sync_volume; 332 333 ctx = ntfs_attr_get_search_ctx(ni, NULL); 334 if (!ctx) 335 return -ENOMEM; 336 337 mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_CHILD); 338 while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) { 339 if (ctx->attr->type == AT_FILE_NAME) { 340 struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr + 341 le16_to_cpu(ctx->attr->data.resident.value_offset)); 342 343 parent_vi = ntfs_iget(vi->i_sb, MREF_LE(fn->parent_directory)); 344 if (IS_ERR(parent_vi)) 345 continue; 346 mutex_lock_nested(&NTFS_I(parent_vi)->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 347 ia_vi = ntfs_index_iget(parent_vi, I30, 4); 348 mutex_unlock(&NTFS_I(parent_vi)->mrec_lock); 349 if (IS_ERR(ia_vi)) { 350 iput(parent_vi); 351 continue; 352 } 353 write_inode_now(ia_vi, 1); 354 iput(ia_vi); 355 write_inode_now(parent_vi, 1); 356 iput(parent_vi); 357 } else if (ctx->attr->non_resident) { 358 struct inode *attr_vi; 359 __le16 *name; 360 361 name = (__le16 *)((u8 *)ctx->attr + le16_to_cpu(ctx->attr->name_offset)); 362 if (ctx->attr->type == AT_DATA && ctx->attr->name_length == 0) 363 continue; 364 if (ctx->attr->type == AT_DATA && 365 ntfs_stream_unlinked(ni, name, ctx->attr->name_length)) 366 continue; 367 368 attr = kmalloc_obj(*attr, GFP_NOFS); 369 if (!attr) { 370 err = -ENOMEM; 371 break; 372 } 373 attr_vi = ntfs_attr_iget(vi, ctx->attr->type, 374 name, ctx->attr->name_length); 375 if (IS_ERR(attr_vi)) { 376 kfree(attr); 377 continue; 378 } 379 if (ntfs_inode_is_named_stream(NTFS_I(attr_vi))) 380 atomic_inc(&NTFS_I(attr_vi)->stream_open_count); 381 attr->inode = attr_vi; 382 list_add_tail(&attr->list, &attrs); 383 } 384 } 385 mutex_unlock(&ni->mrec_lock); 386 ntfs_attr_put_search_ctx(ctx); 387 if (err != -ENOENT && !ret) 388 ret = err; 389 390 /* Attribute writeback takes the base inode's MFT record lock. */ 391 list_for_each_entry_safe(attr, next, &attrs, list) { 392 struct inode *attr_vi = attr->inode; 393 394 err = filemap_write_and_wait(attr_vi->i_mapping); 395 if (err && !ret) 396 ret = err; 397 if (ntfs_inode_is_named_stream(NTFS_I(attr_vi))) { 398 err = ntfs_stream_put(attr_vi); 399 if (err && !ret) 400 ret = err; 401 } 402 iput(attr_vi); 403 list_del(&attr->list); 404 kfree(attr); 405 } 406 407 /* Attribute writeback may have updated the base mapping pairs. */ 408 err = write_inode_now(vi, 1); 409 if (err && !ret) 410 ret = err; 411 412 sync_volume: 413 write_inode_now(vol->mftbmp_ino, 1); 414 down_write(&vol->lcnbmp_lock); 415 write_inode_now(vol->lcnbmp_ino, 1); 416 up_write(&vol->lcnbmp_lock); 417 write_inode_now(vol->mft_ino, 1); 418 419 /* 420 * NOTE: If we were to use mapping->private_list (see ext2 and 421 * fs/buffer.c) for dirty blocks then we could optimize the below to be 422 * sync_mapping_buffers(vi->i_mapping). 423 */ 424 err = sync_blockdev(vi->i_sb->s_bdev); 425 if (unlikely(err && !ret)) 426 ret = err; 427 if (!ret) 428 ret = blkdev_issue_flush(vi->i_sb->s_bdev); 429 if (likely(!ret)) 430 ntfs_debug("Done."); 431 else 432 ntfs_warning(vi->i_sb, 433 "Failed to f%ssync inode 0x%llx. Error %u.", 434 datasync ? "data" : "", ni->mft_no, -ret); 435 return ret; 436 } 437 438 static void ntfs_pagecache_extend(struct inode *vi, loff_t from, loff_t to) 439 { 440 pagecache_isize_extended(vi, from, to); 441 442 /* 443 * NTFS initialized_size is byte-granular, so a partial old-EOF page 444 * must stay write-protected until page_mkwrite() updates it. The 445 * generic helper skips this when the block size is at least a page, 446 * the extension ends before the rounded block boundary, or that 447 * boundary is page-aligned. 448 */ 449 if (from < to && from & (PAGE_SIZE - 1)) 450 unmap_mapping_range(vi->i_mapping, round_down(from, PAGE_SIZE), 451 PAGE_SIZE, 0); 452 } 453 454 int ntfs_setattr_size(struct inode *vi, struct iattr *attr) 455 { 456 struct ntfs_inode *ni = NTFS_I(vi); 457 struct ntfs_inode *base_ni = ntfs_base_inode(ni); 458 bool stream = ntfs_inode_is_named_stream(ni); 459 int err; 460 loff_t old_size = vi->i_size; 461 462 if (stream && NVolShutdown(ni->vol)) 463 return -EIO; 464 465 if (NInoCompressed(ni) || NInoEncrypted(ni) || NInoWofCompressed(ni)) { 466 ntfs_warning( 467 vi->i_sb, 468 "Changes in inode size are not supported yet for %s files.", 469 NInoEncrypted(ni) ? "encrypted" : "compressed"); 470 return -EOPNOTSUPP; 471 } 472 473 err = inode_newsize_ok(vi, attr->ia_size); 474 if (err) 475 return err; 476 477 inode_dio_wait(vi); 478 479 /* 480 * Serialize with page faults and pagecache instantiation so that 481 * readers cannot observe the size change until the attribute 482 * updates below have completed. 483 */ 484 if (stream) { 485 filemap_invalidate_lock(vi->i_mapping); 486 err = filemap_write_and_wait(vi->i_mapping); 487 if (err) 488 goto out_unlock_mapping; 489 490 mutex_lock(&base_ni->mrec_lock); 491 err = ntfs_stream_inode_validate(vi); 492 mutex_unlock(&base_ni->mrec_lock); 493 if (err) 494 goto out_unlock_mapping; 495 } else { 496 filemap_invalidate_lock(vi->i_mapping); 497 } 498 499 if (attr->ia_size > old_size) { 500 truncate_pagecache(vi, old_size); 501 i_size_write(vi, attr->ia_size); 502 ntfs_pagecache_extend(vi, old_size, attr->ia_size); 503 } else { 504 truncate_setsize(vi, attr->ia_size); 505 } 506 507 if (stream) { 508 mutex_lock(&base_ni->mrec_lock); 509 err = ntfs_stream_inode_validate(vi); 510 if (!err) 511 err = __ntfs_attr_truncate_vfs(ni, attr->ia_size, 512 old_size); 513 mutex_unlock(&base_ni->mrec_lock); 514 } else { 515 err = ntfs_truncate_vfs(vi, attr->ia_size, old_size); 516 } 517 if (err) { 518 i_size_write(vi, old_size); 519 goto out_unlock_mapping; 520 } 521 522 out_unlock_mapping: 523 filemap_invalidate_unlock(vi->i_mapping); 524 if (!err && stream) 525 ntfs_stream_update_base_time(base_ni); 526 527 return err; 528 } 529 530 /* 531 * ntfs_setattr 532 * 533 * Called from notify_change() when an attribute is being changed. 534 * 535 * NOTE: Changes in inode size are not supported yet for compressed or 536 * encrypted files. 537 */ 538 int ntfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, 539 struct iattr *attr) 540 { 541 struct inode *vi = d_inode(dentry); 542 int err; 543 unsigned int ia_valid = attr->ia_valid; 544 struct ntfs_inode *ni = NTFS_I(vi); 545 struct ntfs_volume *vol = ni->vol; 546 547 if (NVolShutdown(vol)) 548 return -EIO; 549 550 err = setattr_prepare(idmap, dentry, attr); 551 if (err) 552 goto out; 553 554 if ((ia_valid & ATTR_SIZE) && 555 (NInoCompressed(ni) || NInoEncrypted(ni) || 556 NInoWofCompressed(ni))) { 557 err = -EOPNOTSUPP; 558 goto out; 559 } 560 561 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 562 563 if (ia_valid & ATTR_SIZE) { 564 err = ntfs_setattr_size(vi, attr); 565 if (err) 566 goto out; 567 568 ia_valid |= ATTR_MTIME | ATTR_CTIME; 569 } 570 571 setattr_copy(idmap, vi, attr); 572 573 if (vol->sb->s_flags & SB_POSIXACL && !S_ISLNK(vi->i_mode)) { 574 err = posix_acl_chmod(idmap, dentry, vi->i_mode); 575 if (err) 576 goto out; 577 } 578 579 if (0222 & vi->i_mode) 580 ni->flags &= ~FILE_ATTR_READONLY; 581 else 582 ni->flags |= FILE_ATTR_READONLY; 583 584 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) { 585 unsigned int flags = 0; 586 587 if (ia_valid & ATTR_UID) 588 flags |= NTFS_EA_UID; 589 if (ia_valid & ATTR_GID) 590 flags |= NTFS_EA_GID; 591 if (ia_valid & ATTR_MODE) 592 flags |= NTFS_EA_MODE; 593 594 mutex_lock(&ni->mrec_lock); 595 err = ntfs_ea_set_wsl_inode(vi, 0, NULL, flags); 596 mutex_unlock(&ni->mrec_lock); 597 if (err) 598 goto out; 599 600 } 601 602 mark_inode_dirty(vi); 603 out: 604 return err; 605 } 606 607 int ntfs_getattr(const struct mnt_idmap *idmap, const struct path *path, 608 struct kstat *stat, unsigned int request_mask, 609 unsigned int query_flags) 610 { 611 struct inode *inode = d_backing_inode(path->dentry); 612 struct ntfs_inode *ni = NTFS_I(inode); 613 614 generic_fillattr(idmap, request_mask, inode, stat); 615 616 stat->blksize = NTFS_SB(inode->i_sb)->cluster_size; 617 stat->blocks = (((u64)NTFS_I(inode)->i_dealloc_clusters << 618 NTFS_SB(inode->i_sb)->cluster_size_bits) >> 9) + inode->i_blocks; 619 stat->result_mask |= STATX_BTIME; 620 stat->btime = NTFS_I(inode)->i_crtime; 621 622 if (NInoCompressed(ni) || NInoWofCompressed(ni)) 623 stat->attributes |= STATX_ATTR_COMPRESSED; 624 625 if (NInoEncrypted(ni)) 626 stat->attributes |= STATX_ATTR_ENCRYPTED; 627 628 if (inode->i_flags & S_IMMUTABLE) 629 stat->attributes |= STATX_ATTR_IMMUTABLE; 630 631 if (inode->i_flags & S_APPEND) 632 stat->attributes |= STATX_ATTR_APPEND; 633 if (ni->linuxflags & NTFS_LINUXFLAGS_NODUMP) 634 stat->attributes |= STATX_ATTR_NODUMP; 635 636 stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED | 637 STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND | 638 STATX_ATTR_NODUMP; 639 640 /* 641 * If it's a compressed or encrypted file, NTFS currently 642 * does not support DIO. For normal files, we report the bdev 643 * logical block size. 644 */ 645 if (request_mask & STATX_DIOALIGN && S_ISREG(inode->i_mode)) { 646 unsigned int align = 647 bdev_logical_block_size(inode->i_sb->s_bdev); 648 649 stat->result_mask |= STATX_DIOALIGN; 650 if (!NInoCompressed(ni) && !NInoEncrypted(ni) && 651 !NInoWofCompressed(ni)) { 652 stat->dio_mem_align = align; 653 stat->dio_offset_align = align; 654 } 655 } 656 657 return 0; 658 } 659 660 /* 661 * Validate a stream before VFS write handling removes privileges or updates 662 * timestamps on its base inode. 663 */ 664 static int ntfs_file_modified(struct kiocb *iocb) 665 { 666 struct inode *inode = file_inode(iocb->ki_filp); 667 int err; 668 669 if (ntfs_inode_is_named_stream(NTFS_I(inode))) { 670 err = ntfs_stream_validate_for_mutation(inode, 671 iocb->ki_flags & IOCB_NOWAIT); 672 if (err) 673 return err; 674 } 675 return kiocb_modified(iocb); 676 } 677 678 loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence) 679 { 680 struct inode *inode = file->f_mapping->host; 681 682 if (NInoWofCompressed(NTFS_I(inode)) && 683 (whence == SEEK_HOLE || whence == SEEK_DATA)) 684 return -EOPNOTSUPP; 685 686 switch (whence) { 687 case SEEK_HOLE: 688 inode_lock_shared(inode); 689 offset = iomap_seek_hole(inode, offset, &ntfs_seek_iomap_ops); 690 inode_unlock_shared(inode); 691 break; 692 case SEEK_DATA: 693 inode_lock_shared(inode); 694 offset = iomap_seek_data(inode, offset, &ntfs_seek_iomap_ops); 695 inode_unlock_shared(inode); 696 break; 697 default: 698 return generic_file_llseek_size(file, offset, whence, 699 inode->i_sb->s_maxbytes, 700 i_size_read(inode)); 701 } 702 if (offset < 0) 703 return offset; 704 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes); 705 } 706 707 ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 708 { 709 struct inode *vi = file_inode(iocb->ki_filp); 710 struct super_block *sb = vi->i_sb; 711 ssize_t ret; 712 713 if (NVolShutdown(NTFS_SB(sb))) 714 return -EIO; 715 716 if ((NInoCompressed(NTFS_I(vi)) || NInoWofCompressed(NTFS_I(vi)) || 717 NInoEncrypted(NTFS_I(vi))) && iocb->ki_flags & IOCB_DIRECT) 718 return -EOPNOTSUPP; 719 720 inode_lock_shared(vi); 721 722 if (iocb->ki_flags & IOCB_DIRECT) { 723 size_t count = iov_iter_count(to); 724 725 if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) { 726 ret = -EINVAL; 727 goto inode_unlock; 728 } 729 730 file_accessed(iocb->ki_filp); 731 ret = iomap_dio_rw(iocb, to, &ntfs_read_iomap_ops, NULL, 0, 732 NULL, 0); 733 } else { 734 ret = generic_file_read_iter(iocb, to); 735 } 736 737 inode_unlock: 738 inode_unlock_shared(vi); 739 740 return ret; 741 } 742 743 static int ntfs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size, 744 int error, unsigned int flags) 745 { 746 struct inode *inode = file_inode(iocb->ki_filp); 747 748 if (error) 749 return error; 750 751 if (size) { 752 if (i_size_read(inode) < iocb->ki_pos + size) { 753 i_size_write(inode, iocb->ki_pos + size); 754 if (ntfs_inode_is_named_stream(NTFS_I(inode))) 755 mark_inode_dirty(VFS_I( 756 NTFS_I(inode)->ext.base_ntfs_ino)); 757 else 758 mark_inode_dirty(inode); 759 } 760 } 761 762 return 0; 763 } 764 765 static const struct iomap_dio_ops ntfs_write_dio_ops = { 766 .end_io = ntfs_file_write_dio_end_io, 767 }; 768 769 static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from) 770 { 771 ssize_t ret; 772 773 ret = iomap_dio_rw(iocb, from, &ntfs_dio_iomap_ops, 774 &ntfs_write_dio_ops, 0, NULL, 0); 775 if (ret == -ENOTBLK) 776 ret = 0; 777 else if (ret < 0) 778 goto out; 779 780 if (iov_iter_count(from)) { 781 loff_t offset, end; 782 ssize_t written; 783 int ret2; 784 785 if (iocb->ki_flags & IOCB_NOWAIT) { 786 if (!ret) 787 ret = -EAGAIN; 788 goto out; 789 } 790 791 offset = iocb->ki_pos; 792 iocb->ki_flags &= ~IOCB_DIRECT; 793 794 /* 795 * Prevent concurrent direct I/O and buffered I/O to the same file 796 * range. Wait for in-flight DIO to finish before dirtying pages. 797 */ 798 inode_dio_wait(file_inode(iocb->ki_filp)); 799 800 written = iomap_file_buffered_write(iocb, from, 801 &ntfs_write_iomap_ops, &ntfs_iomap_folio_ops, 802 NULL); 803 if (written < 0) { 804 ret = written; 805 goto out; 806 } 807 808 ret += written; 809 end = iocb->ki_pos + written - 1; 810 ret2 = filemap_write_and_wait_range(iocb->ki_filp->f_mapping, 811 offset, end); 812 if (ret2) { 813 ret = -EIO; 814 goto out; 815 } 816 invalidate_mapping_pages(iocb->ki_filp->f_mapping, 817 offset >> PAGE_SHIFT, 818 end >> PAGE_SHIFT); 819 } 820 821 out: 822 return ret; 823 } 824 825 static int ntfs_expand_for_write(struct ntfs_inode *ni, loff_t end) 826 { 827 struct ntfs_volume *vol = ni->vol; 828 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 829 loff_t prealloc_size = 0; 830 int err; 831 832 if (end <= ni->data_size) 833 return 0; 834 835 if (NInoCompressed(ni)) { 836 if (end > ni->allocated_size) 837 prealloc_size = round_up(end, 838 ni->itype.compressed.block_size); 839 } else if (end > ni->allocated_size && 840 end < ni->allocated_size + vol->preallocated_size) { 841 prealloc_size = ni->allocated_size + vol->preallocated_size; 842 } 843 844 mutex_lock(&mrec_ni->mrec_lock); 845 err = ntfs_attr_expand(ni, end, prealloc_size); 846 mutex_unlock(&mrec_ni->mrec_lock); 847 848 return err; 849 } 850 851 ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 852 { 853 struct file *file = iocb->ki_filp; 854 struct inode *vi = file->f_mapping->host; 855 struct ntfs_inode *ni = NTFS_I(vi); 856 struct ntfs_volume *vol = ni->vol; 857 ssize_t ret; 858 ssize_t count; 859 loff_t pos, end; 860 int err; 861 loff_t old_data_size, old_init_size; 862 863 if (NVolShutdown(vol)) 864 return -EIO; 865 866 if (NInoWofCompressed(ni)) 867 return -EOPNOTSUPP; 868 869 if (NInoEncrypted(ni)) { 870 ntfs_error(vi->i_sb, "Writing for %s files is not supported yet", 871 NInoCompressed(ni) ? "Compressed" : "Encrypted"); 872 return -EOPNOTSUPP; 873 } 874 875 if (NInoCompressed(ni) && iocb->ki_flags & IOCB_DIRECT) 876 return -EOPNOTSUPP; 877 878 if (iocb->ki_flags & IOCB_NOWAIT) { 879 if (!inode_trylock(vi)) 880 return -EAGAIN; 881 } else 882 inode_lock(vi); 883 884 ret = generic_write_checks(iocb, from); 885 if (ret <= 0) 886 goto out_lock; 887 888 err = ntfs_file_modified(iocb); 889 if (err) { 890 ret = err; 891 goto out_lock; 892 } 893 894 /* 895 * The volume must be marked dirty before the modification is made, 896 * without an unlocked check of the in-memory flag: the dirty bit 897 * is only cleared at the quiescent transitions, under the same 898 * $Volume mrec_lock this call takes, so an unlocked skip could 899 * lose the set to one of them. 900 */ 901 ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 902 903 pos = iocb->ki_pos; 904 count = ret; 905 end = pos + count; 906 907 old_data_size = ni->data_size; 908 old_init_size = ni->initialized_size; 909 910 if (end > old_data_size) { 911 ret = ntfs_expand_for_write(ni, end); 912 if (ret < 0) 913 goto out; 914 } 915 916 if (NInoNonResident(ni) && !NInoCompressed(ni) && 917 end > old_init_size) { 918 ret = ntfs_extend_initialized_size(vi, pos, end); 919 if (ret < 0) 920 goto out; 921 } 922 923 if (NInoNonResident(ni) && NInoCompressed(ni)) { 924 ret = ntfs_compress_write(ni, pos, count, from); 925 if (ret > 0) 926 iocb->ki_pos += ret; 927 goto out; 928 } 929 930 if (NInoNonResident(ni) && iocb->ki_flags & IOCB_DIRECT) 931 ret = ntfs_dio_write_iter(iocb, from); 932 else 933 ret = iomap_file_buffered_write(iocb, from, &ntfs_write_iomap_ops, 934 &ntfs_iomap_folio_ops, NULL); 935 out: 936 if (ret < 0 && ret != -EIOCBQUEUED) { 937 int init_err = 0; 938 int data_err = 0; 939 940 /* 941 * Like ntfs_setattr_size(): __ntfs_attr_truncate_vfs() 942 * expects the invalidate lock held across the size 943 * updates. 944 */ 945 filemap_invalidate_lock(vi->i_mapping); 946 947 /* ntfs_attr_set_initialized_size() rejects a resident one. */ 948 if (ni->initialized_size != old_init_size && 949 NInoNonResident(ni)) { 950 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 951 952 mutex_lock(&mrec_ni->mrec_lock); 953 init_err = ntfs_attr_set_initialized_size(ni, 954 old_init_size); 955 mutex_unlock(&mrec_ni->mrec_lock); 956 } 957 /* 958 * The shrink clamps the initialized size to the data size, 959 * which can sit above the old initialized one and expose 960 * the uninitialized tail, so skip it unless the rollback 961 * above restored that size. 962 */ 963 if (ni->data_size != old_data_size && !init_err) { 964 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 965 966 truncate_setsize(vi, old_data_size); 967 mutex_lock(&mrec_ni->mrec_lock); 968 data_err = __ntfs_attr_truncate_vfs(ni, old_data_size, 969 old_data_size); 970 mutex_unlock(&mrec_ni->mrec_lock); 971 /* Like ntfs_setattr_size(): stay on the durable size. */ 972 if (data_err) 973 i_size_write(vi, ni->data_size); 974 } 975 if (init_err || data_err) { 976 /* The failed sizes stay in the dirty MFT record. */ 977 NVolSetErrors(vol); 978 ntfs_error(vi->i_sb, 979 "Failed to roll back the size update: %d", 980 init_err ?: data_err); 981 } 982 983 filemap_invalidate_unlock(vi->i_mapping); 984 } 985 out_lock: 986 inode_unlock(vi); 987 if (ret > 0) 988 ret = generic_write_sync(iocb, ret); 989 return ret; 990 } 991 992 static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf) 993 { 994 struct inode *inode = file_inode(vmf->vma->vm_file); 995 struct address_space *mapping = inode->i_mapping; 996 bool stream = ntfs_inode_is_named_stream(NTFS_I(inode)); 997 int err; 998 vm_fault_t ret; 999 1000 if (NInoWofCompressed(NTFS_I(inode))) 1001 return VM_FAULT_SIGBUS; 1002 1003 sb_start_pagefault(inode->i_sb); 1004 if (stream) { 1005 err = ntfs_stream_validate_for_mutation(inode, false); 1006 if (err) 1007 goto out_error; 1008 err = file_update_time(vmf->vma->vm_file); 1009 if (err) 1010 goto out_error; 1011 } else { 1012 file_update_time(vmf->vma->vm_file); 1013 } 1014 1015 /* 1016 * Serialize against truncate/fallocate which hold the lock 1017 * exclusively while invalidating pagecache and changing extents. 1018 */ 1019 filemap_invalidate_lock_shared(mapping); 1020 ret = iomap_page_mkwrite(vmf, &ntfs_page_mkwrite_iomap_ops, NULL); 1021 filemap_invalidate_unlock_shared(mapping); 1022 sb_end_pagefault(inode->i_sb); 1023 return ret; 1024 1025 out_error: 1026 sb_end_pagefault(inode->i_sb); 1027 return vmf_error(err); 1028 } 1029 1030 static const struct vm_operations_struct ntfs_file_vm_ops = { 1031 .fault = filemap_fault, 1032 .map_pages = filemap_map_pages, 1033 .page_mkwrite = ntfs_filemap_page_mkwrite, 1034 }; 1035 1036 int ntfs_file_mmap_prepare(struct vm_area_desc *desc) 1037 { 1038 struct file *file = desc->file; 1039 struct inode *inode = file_inode(file); 1040 1041 if (NVolShutdown(NTFS_SB(file->f_mapping->host->i_sb))) 1042 return -EIO; 1043 1044 if (NInoCompressed(NTFS_I(inode)) || NInoWofCompressed(NTFS_I(inode))) 1045 return -EOPNOTSUPP; 1046 1047 if (vma_desc_test_all(desc, VMA_SHARED_BIT, VMA_MAYWRITE_BIT)) { 1048 struct inode *inode = file_inode(file); 1049 loff_t from, to; 1050 int err; 1051 1052 from = ((loff_t)desc->pgoff << PAGE_SHIFT); 1053 to = min_t(loff_t, i_size_read(inode), 1054 from + desc->end - desc->start); 1055 1056 if (NTFS_I(inode)->initialized_size < to) { 1057 err = ntfs_extend_initialized_size(inode, to, to); 1058 if (err) 1059 return err; 1060 } 1061 } 1062 1063 1064 file_accessed(file); 1065 desc->vm_ops = &ntfs_file_vm_ops; 1066 return 0; 1067 } 1068 1069 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 1070 u64 start, u64 len) 1071 { 1072 if (NInoWofCompressed(NTFS_I(inode))) 1073 return -EOPNOTSUPP; 1074 1075 return iomap_fiemap(inode, fieinfo, start, len, &ntfs_read_iomap_ops); 1076 } 1077 1078 static const char *ntfs_get_link(struct dentry *dentry, struct inode *inode, 1079 struct delayed_call *done) 1080 { 1081 struct ntfs_inode *ni = NTFS_I(inode); 1082 char *target; 1083 int err; 1084 1085 if (!dentry) 1086 return ERR_PTR(-ECHILD); 1087 1088 if (!ni->target) 1089 return ERR_PTR(-EINVAL); 1090 1091 if (ni->reparse_tag == IO_REPARSE_TAG_MOUNT_POINT || 1092 (ni->reparse_tag == IO_REPARSE_TAG_SYMLINK && 1093 !(ni->reparse_flags & cpu_to_le32(SYMLINK_FLAG_RELATIVE)))) { 1094 if (NVolNativeSymlinkRel(ni->vol)) { 1095 err = ntfs_translate_symlink_path(dentry, ni->target, &target); 1096 if (err < 0) 1097 return ERR_PTR(err); 1098 set_delayed_call(done, kfree_link, target); 1099 return target; 1100 } 1101 } 1102 1103 return ni->target; 1104 } 1105 1106 ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos, 1107 struct pipe_inode_info *pipe, size_t len, unsigned int flags) 1108 { 1109 if (NVolShutdown(NTFS_SB(in->f_mapping->host->i_sb))) 1110 return -EIO; 1111 1112 return filemap_splice_read(in, ppos, pipe, len, flags); 1113 } 1114 1115 static int ntfs_ioctl_shutdown(struct super_block *sb, unsigned long arg) 1116 { 1117 u32 flags; 1118 1119 if (!capable(CAP_SYS_ADMIN)) 1120 return -EPERM; 1121 1122 if (get_user(flags, (__u32 __user *)arg)) 1123 return -EFAULT; 1124 1125 return ntfs_force_shutdown(sb, flags); 1126 } 1127 1128 static int ntfs_ioctl_get_volume_label(struct file *filp, unsigned long arg) 1129 { 1130 struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb); 1131 char __user *buf = (char __user *)arg; 1132 char label[FSLABEL_MAX]; 1133 ssize_t len; 1134 1135 mutex_lock(&vol->volume_label_lock); 1136 if (!vol->volume_label) { 1137 label[0] = '\0'; 1138 len = 0; 1139 } else { 1140 len = strscpy(label, vol->volume_label, sizeof(label)); 1141 if (len == -E2BIG) 1142 len = FSLABEL_MAX - 1; 1143 } 1144 mutex_unlock(&vol->volume_label_lock); 1145 1146 if (copy_to_user(buf, label, len + 1)) 1147 return -EFAULT; 1148 return 0; 1149 } 1150 1151 static int ntfs_ioctl_set_volume_label(struct file *filp, unsigned long arg) 1152 { 1153 struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb); 1154 char *label; 1155 int ret; 1156 1157 if (!capable(CAP_SYS_ADMIN)) 1158 return -EPERM; 1159 1160 label = strndup_user((const char __user *)arg, FSLABEL_MAX); 1161 if (IS_ERR(label)) 1162 return PTR_ERR(label); 1163 1164 ret = mnt_want_write_file(filp); 1165 if (ret) 1166 goto out; 1167 1168 ret = ntfs_write_volume_label(vol, label); 1169 mnt_drop_write_file(filp); 1170 out: 1171 kfree(label); 1172 return ret; 1173 } 1174 1175 static int ntfs_ioctl_fitrim(struct ntfs_volume *vol, unsigned long arg) 1176 { 1177 struct fstrim_range __user *user_range; 1178 struct fstrim_range range; 1179 struct block_device *dev; 1180 int err; 1181 1182 if (!capable(CAP_SYS_ADMIN)) 1183 return -EPERM; 1184 1185 dev = vol->sb->s_bdev; 1186 if (!bdev_max_discard_sectors(dev)) 1187 return -EOPNOTSUPP; 1188 1189 user_range = (struct fstrim_range __user *)arg; 1190 if (copy_from_user(&range, user_range, sizeof(range))) 1191 return -EFAULT; 1192 1193 if (range.len == 0) 1194 return -EINVAL; 1195 1196 if (range.len < vol->cluster_size) 1197 return -EINVAL; 1198 1199 range.minlen = max_t(u64, range.minlen, bdev_discard_granularity(dev)); 1200 1201 err = ntfs_trim_fs(vol, &range); 1202 if (err < 0) 1203 return err; 1204 1205 if (copy_to_user(user_range, &range, sizeof(range))) 1206 return -EFAULT; 1207 1208 return 0; 1209 } 1210 1211 long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 1212 { 1213 switch (cmd) { 1214 case FS_IOC_SHUTDOWN: 1215 return ntfs_ioctl_shutdown(file_inode(filp)->i_sb, arg); 1216 case FS_IOC_GETFSLABEL: 1217 return ntfs_ioctl_get_volume_label(filp, arg); 1218 case FS_IOC_SETFSLABEL: 1219 return ntfs_ioctl_set_volume_label(filp, arg); 1220 case FITRIM: 1221 return ntfs_ioctl_fitrim(NTFS_SB(file_inode(filp)->i_sb), arg); 1222 case NTFS_IOC_STREAM_READ: 1223 return ntfs_ioctl_stream_read(filp, arg); 1224 case NTFS_IOC_STREAM_WRITE: 1225 return ntfs_ioctl_stream_write(filp, arg); 1226 case NTFS_IOC_STREAM_REMOVE: 1227 return ntfs_ioctl_stream_remove(filp, arg); 1228 case NTFS_IOC_LIST_STREAMS: 1229 return ntfs_ioctl_list_streams(filp, arg); 1230 default: 1231 return -ENOTTY; 1232 } 1233 } 1234 1235 #ifdef CONFIG_COMPAT 1236 long ntfs_compat_ioctl(struct file *filp, unsigned int cmd, 1237 unsigned long arg) 1238 { 1239 return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); 1240 } 1241 #endif 1242 1243 static int ntfs_allocate_range(struct ntfs_inode *ni, int mode, loff_t offset, 1244 loff_t len) 1245 { 1246 struct inode *vi = VFS_I(ni); 1247 struct ntfs_volume *vol = ni->vol; 1248 s64 need_space; 1249 loff_t old_size, new_size; 1250 s64 start_vcn, end_vcn; 1251 int err; 1252 1253 old_size = i_size_read(vi); 1254 new_size = max_t(loff_t, old_size, offset + len); 1255 start_vcn = ntfs_bytes_to_cluster(vol, offset); 1256 end_vcn = ntfs_bytes_to_cluster(vol, offset + len - 1) + 1; 1257 1258 err = inode_newsize_ok(vi, new_size); 1259 if (err) 1260 goto out; 1261 1262 need_space = ntfs_bytes_to_cluster(vol, ni->allocated_size); 1263 if (need_space > start_vcn) 1264 need_space = end_vcn - need_space; 1265 else 1266 need_space = end_vcn - start_vcn; 1267 if (need_space > 0 && 1268 need_space > (atomic64_read(&vol->free_clusters) - 1269 atomic64_read(&vol->dirty_clusters))) { 1270 err = -ENOSPC; 1271 goto out; 1272 } 1273 1274 err = ntfs_attr_fallocate(ni, offset, len, 1275 mode & FALLOC_FL_KEEP_SIZE ? true : false); 1276 1277 if (!(mode & FALLOC_FL_KEEP_SIZE) && new_size != old_size) 1278 i_size_write(vi, ni->data_size); 1279 out: 1280 return err; 1281 } 1282 1283 static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset, 1284 loff_t len) 1285 { 1286 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 1287 struct ntfs_volume *vol = ni->vol; 1288 struct inode *vi = VFS_I(ni); 1289 loff_t end_offset; 1290 s64 start_vcn, end_vcn; 1291 int err = 0; 1292 1293 loff_t offset_down = round_down(offset, max_t(unsigned int, 1294 vol->cluster_size, PAGE_SIZE)); 1295 1296 if (NVolDisableSparse(vol)) { 1297 err = -EOPNOTSUPP; 1298 goto out; 1299 } 1300 1301 if (offset >= ni->data_size) 1302 goto out; 1303 1304 if (offset + len > ni->data_size) 1305 end_offset = ni->data_size; 1306 else 1307 end_offset = offset + len; 1308 1309 err = filemap_write_and_wait_range(vi->i_mapping, offset_down, LLONG_MAX); 1310 if (err) 1311 goto out; 1312 truncate_pagecache(vi, offset_down); 1313 1314 start_vcn = ntfs_bytes_to_cluster(vol, offset); 1315 end_vcn = ntfs_bytes_to_cluster(vol, end_offset - 1) + 1; 1316 1317 if (offset & vol->cluster_size_mask) { 1318 if (offset < ni->initialized_size) { 1319 loff_t to; 1320 1321 to = min_t(loff_t, 1322 ntfs_cluster_to_bytes(vol, start_vcn + 1), 1323 end_offset); 1324 err = iomap_zero_range(vi, offset, to - offset, 1325 NULL, &ntfs_seek_iomap_ops, 1326 &ntfs_iomap_folio_ops, NULL); 1327 if (err < 0) 1328 goto out; 1329 } 1330 if (end_vcn - start_vcn == 1) 1331 goto out; 1332 start_vcn++; 1333 } 1334 1335 if (end_offset & vol->cluster_size_mask) { 1336 loff_t from; 1337 1338 from = ntfs_cluster_to_bytes(vol, end_vcn - 1); 1339 if (from < ni->initialized_size) { 1340 err = iomap_zero_range(vi, from, end_offset - from, 1341 NULL, &ntfs_seek_iomap_ops, 1342 &ntfs_iomap_folio_ops, NULL); 1343 if (err < 0) 1344 goto out; 1345 } 1346 if (end_vcn - start_vcn == 1) 1347 goto out; 1348 end_vcn--; 1349 } 1350 1351 mutex_lock_nested(&mrec_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1352 err = ntfs_non_resident_attr_punch_hole(ni, start_vcn, 1353 end_vcn - start_vcn); 1354 mutex_unlock(&mrec_ni->mrec_lock); 1355 out: 1356 return err; 1357 } 1358 1359 static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len) 1360 { 1361 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 1362 struct ntfs_volume *vol = ni->vol; 1363 struct inode *vi = VFS_I(ni); 1364 loff_t old_size, new_size; 1365 s64 start_vcn, end_vcn; 1366 int err; 1367 1368 loff_t offset_down = round_down(offset, 1369 max_t(unsigned long, vol->cluster_size, PAGE_SIZE)); 1370 1371 if ((offset & vol->cluster_size_mask) || 1372 (len & vol->cluster_size_mask) || 1373 offset >= ni->allocated_size) { 1374 err = -EINVAL; 1375 goto out; 1376 } 1377 1378 old_size = i_size_read(vi); 1379 start_vcn = ntfs_bytes_to_cluster(vol, offset); 1380 end_vcn = ntfs_bytes_to_cluster(vol, offset + len - 1) + 1; 1381 1382 if (ntfs_cluster_to_bytes(vol, end_vcn) > ni->allocated_size) 1383 end_vcn = (round_up(ni->allocated_size - 1, 1384 vol->cluster_size) >> vol->cluster_size_bits) + 1; 1385 new_size = old_size - ntfs_cluster_to_bytes(vol, end_vcn - start_vcn); 1386 if (new_size < 0) 1387 new_size = 0; 1388 err = filemap_write_and_wait_range(vi->i_mapping, 1389 offset_down, LLONG_MAX); 1390 if (err) 1391 goto out; 1392 1393 truncate_pagecache(vi, offset_down); 1394 1395 mutex_lock_nested(&mrec_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1396 err = ntfs_non_resident_attr_collapse_range(ni, start_vcn, 1397 end_vcn - start_vcn); 1398 mutex_unlock(&mrec_ni->mrec_lock); 1399 1400 if (new_size != old_size) 1401 i_size_write(vi, ni->data_size); 1402 out: 1403 return err; 1404 } 1405 1406 static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len) 1407 { 1408 struct ntfs_inode *mrec_ni = ntfs_base_inode(ni); 1409 struct ntfs_volume *vol = ni->vol; 1410 struct inode *vi = VFS_I(ni); 1411 loff_t offset_down = round_down(offset, 1412 max_t(unsigned long, vol->cluster_size, PAGE_SIZE)); 1413 loff_t alloc_size, end_offset = offset + len; 1414 loff_t old_size, new_size; 1415 s64 start_vcn, end_vcn; 1416 int err; 1417 1418 if (NVolDisableSparse(vol)) { 1419 err = -EOPNOTSUPP; 1420 goto out; 1421 } 1422 1423 if ((offset & vol->cluster_size_mask) || 1424 (len & vol->cluster_size_mask) || 1425 offset >= ni->allocated_size) { 1426 err = -EINVAL; 1427 goto out; 1428 } 1429 1430 old_size = i_size_read(vi); 1431 start_vcn = ntfs_bytes_to_cluster(vol, offset); 1432 end_vcn = ntfs_bytes_to_cluster(vol, end_offset - 1) + 1; 1433 1434 new_size = old_size + ntfs_cluster_to_bytes(vol, end_vcn - start_vcn); 1435 alloc_size = ni->allocated_size + 1436 ntfs_cluster_to_bytes(vol, end_vcn - start_vcn); 1437 if (alloc_size < 0) { 1438 err = -EFBIG; 1439 goto out; 1440 } 1441 err = inode_newsize_ok(vi, alloc_size); 1442 if (err) 1443 goto out; 1444 1445 err = filemap_write_and_wait_range(vi->i_mapping, 1446 offset_down, LLONG_MAX); 1447 if (err) 1448 goto out; 1449 1450 truncate_pagecache(vi, offset_down); 1451 1452 mutex_lock_nested(&mrec_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1453 err = ntfs_non_resident_attr_insert_range(ni, start_vcn, 1454 end_vcn - start_vcn); 1455 mutex_unlock(&mrec_ni->mrec_lock); 1456 1457 if (new_size != old_size) 1458 i_size_write(vi, ni->data_size); 1459 out: 1460 return err; 1461 } 1462 1463 #define NTFS_FALLOC_FL_SUPPORTED \ 1464 (FALLOC_FL_ALLOCATE_RANGE | FALLOC_FL_KEEP_SIZE | \ 1465 FALLOC_FL_INSERT_RANGE | FALLOC_FL_PUNCH_HOLE | \ 1466 FALLOC_FL_COLLAPSE_RANGE) 1467 1468 long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len) 1469 { 1470 struct inode *vi = file_inode(file); 1471 struct ntfs_inode *ni = NTFS_I(vi); 1472 struct ntfs_inode *base_ni = ntfs_base_inode(ni); 1473 struct ntfs_volume *vol = ni->vol; 1474 bool stream = ntfs_inode_is_named_stream(ni); 1475 int err = 0; 1476 loff_t old_size, new_size; 1477 1478 if (mode & ~(NTFS_FALLOC_FL_SUPPORTED)) 1479 return -EOPNOTSUPP; 1480 1481 if (NInoCompressed(ni) || NInoEncrypted(ni) || NInoWofCompressed(ni)) 1482 return -EOPNOTSUPP; 1483 1484 if (!NVolFreeClusterKnown(vol)) 1485 wait_event(vol->free_waitq, NVolFreeClusterKnown(vol)); 1486 1487 if (NInoNonResident(ni) && !NInoFullyMapped(ni)) { 1488 down_write(&ni->runlist.lock); 1489 err = ntfs_attr_map_whole_runlist(ni); 1490 up_write(&ni->runlist.lock); 1491 if (err) 1492 return err; 1493 } 1494 1495 err = ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 1496 if (err) 1497 return err; 1498 1499 inode_lock(vi); 1500 if (NInoCompressed(ni) || NInoEncrypted(ni) || NInoWofCompressed(ni)) { 1501 inode_unlock(vi); 1502 return -EOPNOTSUPP; 1503 } 1504 if (stream) { 1505 err = ntfs_stream_validate_for_mutation(vi, false); 1506 if (err) { 1507 inode_unlock(vi); 1508 return err; 1509 } 1510 } 1511 old_size = i_size_read(vi); 1512 1513 inode_dio_wait(vi); 1514 /* Take invalidate_lock for all fallocate operations to prevent races */ 1515 filemap_invalidate_lock(vi->i_mapping); 1516 1517 switch (mode & FALLOC_FL_MODE_MASK) { 1518 case FALLOC_FL_ALLOCATE_RANGE: 1519 case FALLOC_FL_KEEP_SIZE: 1520 err = ntfs_allocate_range(ni, mode, offset, len); 1521 break; 1522 case FALLOC_FL_PUNCH_HOLE: 1523 err = ntfs_punch_hole(ni, mode, offset, len); 1524 break; 1525 case FALLOC_FL_COLLAPSE_RANGE: 1526 err = ntfs_collapse_range(ni, offset, len); 1527 break; 1528 case FALLOC_FL_INSERT_RANGE: 1529 err = ntfs_insert_range(ni, offset, len); 1530 break; 1531 default: 1532 err = -EOPNOTSUPP; 1533 } 1534 1535 if (err) 1536 goto out; 1537 1538 err = file_modified(file); 1539 out: 1540 if (!err && mode == 0 && NInoNonResident(ni)) { 1541 new_size = i_size_read(vi); 1542 if (new_size > old_size) { 1543 truncate_pagecache(vi, old_size); 1544 ntfs_pagecache_extend(vi, old_size, new_size); 1545 } 1546 } 1547 1548 filemap_invalidate_unlock(vi->i_mapping); 1549 1550 if (!err) { 1551 if (stream) { 1552 ntfs_stream_update_base_time(base_ni); 1553 } else { 1554 NInoSetFileNameDirty(ni); 1555 inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi)); 1556 mark_inode_dirty(vi); 1557 } 1558 } 1559 1560 inode_unlock(vi); 1561 return err; 1562 } 1563 1564 const struct file_operations ntfs_file_ops = { 1565 .llseek = ntfs_file_llseek, 1566 .read_iter = ntfs_file_read_iter, 1567 .write_iter = ntfs_file_write_iter, 1568 .fsync = ntfs_file_fsync, 1569 .mmap_prepare = ntfs_file_mmap_prepare, 1570 .open = ntfs_file_open, 1571 .release = ntfs_file_release, 1572 .splice_read = ntfs_file_splice_read, 1573 .splice_write = iter_file_splice_write, 1574 .unlocked_ioctl = ntfs_ioctl, 1575 #ifdef CONFIG_COMPAT 1576 .compat_ioctl = ntfs_compat_ioctl, 1577 #endif 1578 .fallocate = ntfs_fallocate, 1579 .setlease = generic_setlease, 1580 }; 1581 1582 const struct inode_operations ntfs_file_inode_ops = { 1583 .setattr = ntfs_setattr, 1584 .getattr = ntfs_getattr, 1585 .fileattr_get = ntfs_fileattr_get, 1586 .fileattr_set = ntfs_fileattr_set, 1587 .listxattr = ntfs_listxattr, 1588 .get_acl = ntfs_get_acl, 1589 .set_acl = ntfs_set_acl, 1590 .fiemap = ntfs_fiemap, 1591 }; 1592 1593 const struct inode_operations ntfs_symlink_inode_operations = { 1594 .get_link = ntfs_get_link, 1595 .setattr = ntfs_setattr, 1596 .getattr = ntfs_getattr, 1597 .listxattr = ntfs_listxattr, 1598 .fileattr_get = ntfs_fileattr_get, 1599 .fileattr_set = ntfs_fileattr_set, 1600 }; 1601 1602 const struct inode_operations ntfs_special_inode_operations = { 1603 .setattr = ntfs_setattr, 1604 .getattr = ntfs_getattr, 1605 .listxattr = ntfs_listxattr, 1606 .fileattr_get = ntfs_fileattr_get, 1607 .fileattr_set = ntfs_fileattr_set, 1608 .get_acl = ntfs_get_acl, 1609 .set_acl = ntfs_set_acl, 1610 }; 1611 1612 const struct file_operations ntfs_empty_file_ops = {}; 1613 1614 const struct inode_operations ntfs_empty_inode_ops = {}; 1615