Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ntfs: update file operations

Rewrite the file operations to utilize the iomap infrastructure,
replacing the legacy buffer-head based implementation.

Implement ntfs_setattr() with size change handling, uid/gid/mode.

Add support for Direct I/O.

Add support for fallocate with the FALLOC_FL_KEEP_SIZE,
FALLOC_FL_PUNCH_HOLE, FALLOC_FL_COLLAPSE_RANGE, FALLOC_FL_INSERT_RANGE
and FALLOC_FL_ALLOCATE_RANGE modes.

Implement .llseek with SEEK_DATA / SEEK_HOLE support.

Implement ntfs_fiemap() using iomap_fiemap().

Add FS_IOC_SHUTDOWN, FS_IOC_[GS]ETFSLABEL, FITRIM ioctl support.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Hyunchul Lee <hyc.lee@gmail.com>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>

+1043 -1879
+1043 -1879
fs/ntfs/file.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 /* 3 - * file.c - NTFS kernel file operations. Part of the Linux-NTFS project. 3 + * NTFS kernel file operations. 4 4 * 5 5 * Copyright (c) 2001-2015 Anton Altaparmakov and Tuxera Inc. 6 + * Copyright (c) 2025 LG Electronics Co., Ltd. 6 7 */ 7 8 8 - #include <linux/blkdev.h> 9 - #include <linux/backing-dev.h> 10 - #include <linux/buffer_head.h> 11 - #include <linux/gfp.h> 12 - #include <linux/pagemap.h> 13 - #include <linux/pagevec.h> 14 - #include <linux/sched/signal.h> 15 - #include <linux/swap.h> 16 - #include <linux/uio.h> 17 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 18 19 - #include <asm/page.h> 20 - #include <linux/uaccess.h> 21 - 22 - #include "attrib.h" 23 - #include "bitmap.h" 24 - #include "inode.h" 25 - #include "debug.h" 26 19 #include "lcnalloc.h" 27 - #include "malloc.h" 28 - #include "mft.h" 29 20 #include "ntfs.h" 21 + #include "reparse.h" 22 + #include "ea.h" 23 + #include "iomap.h" 24 + #include "bitmap.h" 30 25 31 - /** 26 + #include <linux/filelock.h> 27 + 28 + /* 32 29 * ntfs_file_open - called when an inode is about to be opened 33 30 * @vi: inode to be opened 34 31 * @filp: file structure describing the inode ··· 45 48 */ 46 49 static int ntfs_file_open(struct inode *vi, struct file *filp) 47 50 { 51 + struct ntfs_inode *ni = NTFS_I(vi); 52 + 53 + if (NVolShutdown(ni->vol)) 54 + return -EIO; 55 + 48 56 if (sizeof(unsigned long) < 8) { 49 57 if (i_size_read(vi) > MAX_LFS_FILESIZE) 50 58 return -EOVERFLOW; 51 59 } 60 + 61 + filp->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT; 62 + 52 63 return generic_file_open(vi, filp); 53 64 } 54 65 55 - #ifdef NTFS_RW 56 - 57 - /** 58 - * ntfs_attr_extend_initialized - extend the initialized size of an attribute 59 - * @ni: ntfs inode of the attribute to extend 60 - * @new_init_size: requested new initialized size in bytes 66 + /* 67 + * Trim preallocated space on file release. 61 68 * 62 - * Extend the initialized size of an attribute described by the ntfs inode @ni 63 - * to @new_init_size bytes. This involves zeroing any non-sparse space between 64 - * the old initialized size and @new_init_size both in the page cache and on 65 - * disk (if relevant complete pages are already uptodate in the page cache then 66 - * these are simply marked dirty). 69 + * When the preallo_size mount option is set (default 64KB), writes extend 70 + * allocated_size and runlist in units of preallocated size to reduce 71 + * runlist merge overhead for small writes. This can leave 72 + * allocated_size > data_size if not all preallocated space is used. 67 73 * 68 - * As a side-effect, the file size (vfs inode->i_size) may be incremented as, 69 - * in the resident attribute case, it is tied to the initialized size and, in 70 - * the non-resident attribute case, it may not fall below the initialized size. 74 + * We perform the trim here because ->release() is called only when 75 + * the file is no longer open. At this point, no further writes can occur, 76 + * so it is safe to reclaim the unused preallocated space. 71 77 * 72 - * Note that if the attribute is resident, we do not need to touch the page 73 - * cache at all. This is because if the page cache page is not uptodate we 74 - * bring it uptodate later, when doing the write to the mft record since we 75 - * then already have the page mapped. And if the page is uptodate, the 76 - * non-initialized region will already have been zeroed when the page was 77 - * brought uptodate and the region may in fact already have been overwritten 78 - * with new data via mmap() based writes, so we cannot just zero it. And since 79 - * POSIX specifies that the behaviour of resizing a file whilst it is mmap()ped 80 - * is unspecified, we choose not to do zeroing and thus we do not need to touch 81 - * the page at all. For a more detailed explanation see ntfs_truncate() in 82 - * fs/ntfs/inode.c. 83 - * 84 - * Return 0 on success and -errno on error. In the case that an error is 85 - * encountered it is possible that the initialized size will already have been 86 - * incremented some way towards @new_init_size but it is guaranteed that if 87 - * this is the case, the necessary zeroing will also have happened and that all 88 - * metadata is self-consistent. 89 - * 90 - * Locking: i_mutex on the vfs inode corrseponsind to the ntfs inode @ni must be 91 - * held by the caller. 78 + * Returns 0 on success, or negative error on failure. 92 79 */ 93 - static int ntfs_attr_extend_initialized(ntfs_inode *ni, const s64 new_init_size) 80 + static int ntfs_trim_prealloc(struct inode *vi) 94 81 { 95 - s64 old_init_size; 96 - loff_t old_i_size; 97 - pgoff_t index, end_index; 98 - unsigned long flags; 99 - struct inode *vi = VFS_I(ni); 100 - ntfs_inode *base_ni; 101 - MFT_RECORD *m = NULL; 102 - ATTR_RECORD *a; 103 - ntfs_attr_search_ctx *ctx = NULL; 104 - struct address_space *mapping; 105 - struct page *page = NULL; 106 - u8 *kattr; 107 - int err; 108 - u32 attr_len; 82 + struct ntfs_inode *ni = NTFS_I(vi); 83 + struct ntfs_volume *vol = ni->vol; 84 + struct runlist_element *rl; 85 + s64 aligned_data_size; 86 + s64 vcn_ds, vcn_tr; 87 + ssize_t rc; 88 + int err = 0; 109 89 110 - read_lock_irqsave(&ni->size_lock, flags); 111 - old_init_size = ni->initialized_size; 112 - old_i_size = i_size_read(vi); 113 - BUG_ON(new_init_size > ni->allocated_size); 114 - read_unlock_irqrestore(&ni->size_lock, flags); 115 - ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, " 116 - "old_initialized_size 0x%llx, " 117 - "new_initialized_size 0x%llx, i_size 0x%llx.", 118 - vi->i_ino, (unsigned)le32_to_cpu(ni->type), 119 - (unsigned long long)old_init_size, 120 - (unsigned long long)new_init_size, old_i_size); 121 - if (!NInoAttr(ni)) 122 - base_ni = ni; 123 - else 124 - base_ni = ni->ext.base_ntfs_ino; 125 - /* Use goto to reduce indentation and we need the label below anyway. */ 126 - if (NInoNonResident(ni)) 127 - goto do_non_resident_extend; 128 - BUG_ON(old_init_size != old_i_size); 129 - m = map_mft_record(base_ni); 130 - if (IS_ERR(m)) { 131 - err = PTR_ERR(m); 132 - m = NULL; 133 - goto err_out; 134 - } 135 - ctx = ntfs_attr_get_search_ctx(base_ni, m); 136 - if (unlikely(!ctx)) { 137 - err = -ENOMEM; 138 - goto err_out; 139 - } 140 - err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 141 - CASE_SENSITIVE, 0, NULL, 0, ctx); 142 - if (unlikely(err)) { 143 - if (err == -ENOENT) 144 - err = -EIO; 145 - goto err_out; 146 - } 147 - m = ctx->mrec; 148 - a = ctx->attr; 149 - BUG_ON(a->non_resident); 150 - /* The total length of the attribute value. */ 151 - attr_len = le32_to_cpu(a->data.resident.value_length); 152 - BUG_ON(old_i_size != (loff_t)attr_len); 153 - /* 154 - * Do the zeroing in the mft record and update the attribute size in 155 - * the mft record. 156 - */ 157 - kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset); 158 - memset(kattr + attr_len, 0, new_init_size - attr_len); 159 - a->data.resident.value_length = cpu_to_le32((u32)new_init_size); 160 - /* Finally, update the sizes in the vfs and ntfs inodes. */ 161 - write_lock_irqsave(&ni->size_lock, flags); 162 - i_size_write(vi, new_init_size); 163 - ni->initialized_size = new_init_size; 164 - write_unlock_irqrestore(&ni->size_lock, flags); 165 - goto done; 166 - do_non_resident_extend: 167 - /* 168 - * If the new initialized size @new_init_size exceeds the current file 169 - * size (vfs inode->i_size), we need to extend the file size to the 170 - * new initialized size. 171 - */ 172 - if (new_init_size > old_i_size) { 173 - m = map_mft_record(base_ni); 174 - if (IS_ERR(m)) { 175 - err = PTR_ERR(m); 176 - m = NULL; 177 - goto err_out; 178 - } 179 - ctx = ntfs_attr_get_search_ctx(base_ni, m); 180 - if (unlikely(!ctx)) { 181 - err = -ENOMEM; 182 - goto err_out; 183 - } 184 - err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 185 - CASE_SENSITIVE, 0, NULL, 0, ctx); 186 - if (unlikely(err)) { 187 - if (err == -ENOENT) 188 - err = -EIO; 189 - goto err_out; 190 - } 191 - m = ctx->mrec; 192 - a = ctx->attr; 193 - BUG_ON(!a->non_resident); 194 - BUG_ON(old_i_size != (loff_t) 195 - sle64_to_cpu(a->data.non_resident.data_size)); 196 - a->data.non_resident.data_size = cpu_to_sle64(new_init_size); 197 - flush_dcache_mft_record_page(ctx->ntfs_ino); 198 - mark_mft_record_dirty(ctx->ntfs_ino); 199 - /* Update the file size in the vfs inode. */ 200 - i_size_write(vi, new_init_size); 201 - ntfs_attr_put_search_ctx(ctx); 202 - ctx = NULL; 203 - unmap_mft_record(base_ni); 204 - m = NULL; 205 - } 206 - mapping = vi->i_mapping; 207 - index = old_init_size >> PAGE_SHIFT; 208 - end_index = (new_init_size + PAGE_SIZE - 1) >> PAGE_SHIFT; 209 - do { 210 - /* 211 - * Read the page. If the page is not present, this will zero 212 - * the uninitialized regions for us. 213 - */ 214 - page = read_mapping_page(mapping, index, NULL); 215 - if (IS_ERR(page)) { 216 - err = PTR_ERR(page); 217 - goto init_err_out; 218 - } 219 - /* 220 - * Update the initialized size in the ntfs inode. This is 221 - * enough to make ntfs_writepage() work. 222 - */ 223 - write_lock_irqsave(&ni->size_lock, flags); 224 - ni->initialized_size = (s64)(index + 1) << PAGE_SHIFT; 225 - if (ni->initialized_size > new_init_size) 226 - ni->initialized_size = new_init_size; 227 - write_unlock_irqrestore(&ni->size_lock, flags); 228 - /* Set the page dirty so it gets written out. */ 229 - set_page_dirty(page); 230 - put_page(page); 231 - /* 232 - * Play nice with the vm and the rest of the system. This is 233 - * very much needed as we can potentially be modifying the 234 - * initialised size from a very small value to a really huge 235 - * value, e.g. 236 - * f = open(somefile, O_TRUNC); 237 - * truncate(f, 10GiB); 238 - * seek(f, 10GiB); 239 - * write(f, 1); 240 - * And this would mean we would be marking dirty hundreds of 241 - * thousands of pages or as in the above example more than 242 - * two and a half million pages! 243 - * 244 - * TODO: For sparse pages could optimize this workload by using 245 - * the FsMisc / MiscFs page bit as a "PageIsSparse" bit. This 246 - * would be set in read_folio for sparse pages and here we would 247 - * not need to mark dirty any pages which have this bit set. 248 - * The only caveat is that we have to clear the bit everywhere 249 - * where we allocate any clusters that lie in the page or that 250 - * contain the page. 251 - * 252 - * TODO: An even greater optimization would be for us to only 253 - * call read_folio() on pages which are not in sparse regions as 254 - * determined from the runlist. This would greatly reduce the 255 - * number of pages we read and make dirty in the case of sparse 256 - * files. 257 - */ 258 - balance_dirty_pages_ratelimited(mapping); 259 - cond_resched(); 260 - } while (++index < end_index); 261 - read_lock_irqsave(&ni->size_lock, flags); 262 - BUG_ON(ni->initialized_size != new_init_size); 263 - read_unlock_irqrestore(&ni->size_lock, flags); 264 - /* Now bring in sync the initialized_size in the mft record. */ 265 - m = map_mft_record(base_ni); 266 - if (IS_ERR(m)) { 267 - err = PTR_ERR(m); 268 - m = NULL; 269 - goto init_err_out; 270 - } 271 - ctx = ntfs_attr_get_search_ctx(base_ni, m); 272 - if (unlikely(!ctx)) { 273 - err = -ENOMEM; 274 - goto init_err_out; 275 - } 276 - err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 277 - CASE_SENSITIVE, 0, NULL, 0, ctx); 278 - if (unlikely(err)) { 279 - if (err == -ENOENT) 280 - err = -EIO; 281 - goto init_err_out; 282 - } 283 - m = ctx->mrec; 284 - a = ctx->attr; 285 - BUG_ON(!a->non_resident); 286 - a->data.non_resident.initialized_size = cpu_to_sle64(new_init_size); 287 - done: 288 - flush_dcache_mft_record_page(ctx->ntfs_ino); 289 - mark_mft_record_dirty(ctx->ntfs_ino); 290 - if (ctx) 291 - ntfs_attr_put_search_ctx(ctx); 292 - if (m) 293 - unmap_mft_record(base_ni); 294 - ntfs_debug("Done, initialized_size 0x%llx, i_size 0x%llx.", 295 - (unsigned long long)new_init_size, i_size_read(vi)); 296 - return 0; 297 - init_err_out: 298 - write_lock_irqsave(&ni->size_lock, flags); 299 - ni->initialized_size = old_init_size; 300 - write_unlock_irqrestore(&ni->size_lock, flags); 301 - err_out: 302 - if (ctx) 303 - ntfs_attr_put_search_ctx(ctx); 304 - if (m) 305 - unmap_mft_record(base_ni); 306 - ntfs_debug("Failed. Returning error code %i.", err); 307 - return err; 308 - } 90 + inode_lock(vi); 91 + mutex_lock(&ni->mrec_lock); 92 + down_write(&ni->runlist.lock); 309 93 310 - static ssize_t ntfs_prepare_file_for_write(struct kiocb *iocb, 311 - struct iov_iter *from) 312 - { 313 - loff_t pos; 314 - s64 end, ll; 315 - ssize_t err; 316 - unsigned long flags; 317 - struct file *file = iocb->ki_filp; 318 - struct inode *vi = file_inode(file); 319 - ntfs_inode *ni = NTFS_I(vi); 320 - ntfs_volume *vol = ni->vol; 94 + aligned_data_size = round_up(ni->data_size, vol->cluster_size); 95 + if (aligned_data_size >= ni->allocated_size) 96 + goto out_unlock; 321 97 322 - ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, pos " 323 - "0x%llx, count 0x%zx.", vi->i_ino, 324 - (unsigned)le32_to_cpu(ni->type), 325 - (unsigned long long)iocb->ki_pos, 326 - iov_iter_count(from)); 327 - err = generic_write_checks(iocb, from); 328 - if (unlikely(err <= 0)) 329 - goto out; 330 - /* 331 - * All checks have passed. Before we start doing any writing we want 332 - * to abort any totally illegal writes. 333 - */ 334 - BUG_ON(NInoMstProtected(ni)); 335 - BUG_ON(ni->type != AT_DATA); 336 - /* If file is encrypted, deny access, just like NT4. */ 337 - if (NInoEncrypted(ni)) { 338 - /* Only $DATA attributes can be encrypted. */ 339 - /* 340 - * Reminder for later: Encrypted files are _always_ 341 - * non-resident so that the content can always be encrypted. 342 - */ 343 - ntfs_debug("Denying write access to encrypted file."); 344 - err = -EACCES; 345 - goto out; 98 + vcn_ds = ntfs_bytes_to_cluster(vol, aligned_data_size); 99 + vcn_tr = -1; 100 + rc = ni->runlist.count - 2; 101 + rl = ni->runlist.rl; 102 + 103 + while (rc >= 0 && rl[rc].lcn == LCN_HOLE && vcn_ds <= rl[rc].vcn) { 104 + vcn_tr = rl[rc].vcn; 105 + rc--; 346 106 } 347 - if (NInoCompressed(ni)) { 348 - /* Only unnamed $DATA attribute can be compressed. */ 349 - BUG_ON(ni->name_len); 350 - /* 351 - * Reminder for later: If resident, the data is not actually 352 - * compressed. Only on the switch to non-resident does 353 - * compression kick in. This is in contrast to encrypted files 354 - * (see above). 355 - */ 356 - ntfs_error(vi->i_sb, "Writing to compressed files is not " 357 - "implemented yet. Sorry."); 358 - err = -EOPNOTSUPP; 359 - goto out; 360 - } 361 - err = file_remove_privs(file); 362 - if (unlikely(err)) 363 - goto out; 364 - /* 365 - * Our ->update_time method always succeeds thus file_update_time() 366 - * cannot fail either so there is no need to check the return code. 367 - */ 368 - file_update_time(file); 369 - pos = iocb->ki_pos; 370 - /* The first byte after the last cluster being written to. */ 371 - end = (pos + iov_iter_count(from) + vol->cluster_size_mask) & 372 - ~(u64)vol->cluster_size_mask; 373 - /* 374 - * If the write goes beyond the allocated size, extend the allocation 375 - * to cover the whole of the write, rounded up to the nearest cluster. 376 - */ 377 - read_lock_irqsave(&ni->size_lock, flags); 378 - ll = ni->allocated_size; 379 - read_unlock_irqrestore(&ni->size_lock, flags); 380 - if (end > ll) { 381 - /* 382 - * Extend the allocation without changing the data size. 383 - * 384 - * Note we ensure the allocation is big enough to at least 385 - * write some data but we do not require the allocation to be 386 - * complete, i.e. it may be partial. 387 - */ 388 - ll = ntfs_attr_extend_allocation(ni, end, -1, pos); 389 - if (likely(ll >= 0)) { 390 - BUG_ON(pos >= ll); 391 - /* If the extension was partial truncate the write. */ 392 - if (end > ll) { 393 - ntfs_debug("Truncating write to inode 0x%lx, " 394 - "attribute type 0x%x, because " 395 - "the allocation was only " 396 - "partially extended.", 397 - vi->i_ino, (unsigned) 398 - le32_to_cpu(ni->type)); 399 - iov_iter_truncate(from, ll - pos); 400 - } 107 + 108 + if (vcn_tr >= 0) { 109 + err = ntfs_rl_truncate_nolock(vol, &ni->runlist, vcn_tr); 110 + if (err) { 111 + kvfree(ni->runlist.rl); 112 + ni->runlist.rl = NULL; 113 + ntfs_error(vol->sb, "Preallocated block rollback failed"); 401 114 } else { 402 - err = ll; 403 - read_lock_irqsave(&ni->size_lock, flags); 404 - ll = ni->allocated_size; 405 - read_unlock_irqrestore(&ni->size_lock, flags); 406 - /* Perform a partial write if possible or fail. */ 407 - if (pos < ll) { 408 - ntfs_debug("Truncating write to inode 0x%lx " 409 - "attribute type 0x%x, because " 410 - "extending the allocation " 411 - "failed (error %d).", 412 - vi->i_ino, (unsigned) 413 - le32_to_cpu(ni->type), 414 - (int)-err); 415 - iov_iter_truncate(from, ll - pos); 416 - } else { 417 - if (err != -ENOSPC) 418 - ntfs_error(vi->i_sb, "Cannot perform " 419 - "write to inode " 420 - "0x%lx, attribute " 421 - "type 0x%x, because " 422 - "extending the " 423 - "allocation failed " 424 - "(error %ld).", 425 - vi->i_ino, (unsigned) 426 - le32_to_cpu(ni->type), 427 - (long)-err); 428 - else 429 - ntfs_debug("Cannot perform write to " 430 - "inode 0x%lx, " 431 - "attribute type 0x%x, " 432 - "because there is not " 433 - "space left.", 434 - vi->i_ino, (unsigned) 435 - le32_to_cpu(ni->type)); 436 - goto out; 437 - } 115 + ni->allocated_size = ntfs_cluster_to_bytes(vol, vcn_tr); 116 + err = ntfs_attr_update_mapping_pairs(ni, 0); 117 + if (err) 118 + ntfs_error(vol->sb, 119 + "Failed to rollback mapping pairs for prealloc"); 438 120 } 439 121 } 440 - /* 441 - * If the write starts beyond the initialized size, extend it up to the 442 - * beginning of the write and initialize all non-sparse space between 443 - * the old initialized size and the new one. This automatically also 444 - * increments the vfs inode->i_size to keep it above or equal to the 445 - * initialized_size. 446 - */ 447 - read_lock_irqsave(&ni->size_lock, flags); 448 - ll = ni->initialized_size; 449 - read_unlock_irqrestore(&ni->size_lock, flags); 450 - if (pos > ll) { 451 - /* 452 - * Wait for ongoing direct i/o to complete before proceeding. 453 - * New direct i/o cannot start as we hold i_mutex. 454 - */ 455 - inode_dio_wait(vi); 456 - err = ntfs_attr_extend_initialized(ni, pos); 457 - if (unlikely(err < 0)) 458 - ntfs_error(vi->i_sb, "Cannot perform write to inode " 459 - "0x%lx, attribute type 0x%x, because " 460 - "extending the initialized size " 461 - "failed (error %d).", vi->i_ino, 462 - (unsigned)le32_to_cpu(ni->type), 463 - (int)-err); 464 - } 465 - out: 122 + 123 + out_unlock: 124 + up_write(&ni->runlist.lock); 125 + mutex_unlock(&ni->mrec_lock); 126 + inode_unlock(vi); 127 + 466 128 return err; 467 129 } 468 130 469 - /** 470 - * __ntfs_grab_cache_pages - obtain a number of locked pages 471 - * @mapping: address space mapping from which to obtain page cache pages 472 - * @index: starting index in @mapping at which to begin obtaining pages 473 - * @nr_pages: number of page cache pages to obtain 474 - * @pages: array of pages in which to return the obtained page cache pages 475 - * @cached_page: allocated but as yet unused page 476 - * 477 - * Obtain @nr_pages locked page cache pages from the mapping @mapping and 478 - * starting at index @index. 479 - * 480 - * If a page is newly created, add it to lru list 481 - * 482 - * Note, the page locks are obtained in ascending page index order. 483 - */ 484 - static inline int __ntfs_grab_cache_pages(struct address_space *mapping, 485 - pgoff_t index, const unsigned nr_pages, struct page **pages, 486 - struct page **cached_page) 131 + static int ntfs_file_release(struct inode *vi, struct file *filp) 487 132 { 488 - int err, nr; 133 + if (!NInoCompressed(NTFS_I(vi))) 134 + return ntfs_trim_prealloc(vi); 489 135 490 - BUG_ON(!nr_pages); 491 - err = nr = 0; 492 - do { 493 - pages[nr] = find_get_page_flags(mapping, index, FGP_LOCK | 494 - FGP_ACCESSED); 495 - if (!pages[nr]) { 496 - if (!*cached_page) { 497 - *cached_page = page_cache_alloc(mapping); 498 - if (unlikely(!*cached_page)) { 499 - err = -ENOMEM; 500 - goto err_out; 501 - } 502 - } 503 - err = add_to_page_cache_lru(*cached_page, mapping, 504 - index, 505 - mapping_gfp_constraint(mapping, GFP_KERNEL)); 506 - if (unlikely(err)) { 507 - if (err == -EEXIST) 508 - continue; 509 - goto err_out; 510 - } 511 - pages[nr] = *cached_page; 512 - *cached_page = NULL; 513 - } 514 - index++; 515 - nr++; 516 - } while (nr < nr_pages); 517 - out: 518 - return err; 519 - err_out: 520 - while (nr > 0) { 521 - unlock_page(pages[--nr]); 522 - put_page(pages[nr]); 523 - } 524 - goto out; 525 - } 526 - 527 - static inline void ntfs_submit_bh_for_read(struct buffer_head *bh) 528 - { 529 - lock_buffer(bh); 530 - get_bh(bh); 531 - bh->b_end_io = end_buffer_read_sync; 532 - submit_bh(REQ_OP_READ, bh); 533 - } 534 - 535 - /** 536 - * ntfs_prepare_pages_for_non_resident_write - prepare pages for receiving data 537 - * @pages: array of destination pages 538 - * @nr_pages: number of pages in @pages 539 - * @pos: byte position in file at which the write begins 540 - * @bytes: number of bytes to be written 541 - * 542 - * This is called for non-resident attributes from ntfs_file_buffered_write() 543 - * with i_mutex held on the inode (@pages[0]->mapping->host). There are 544 - * @nr_pages pages in @pages which are locked but not kmap()ped. The source 545 - * data has not yet been copied into the @pages. 546 - * 547 - * Need to fill any holes with actual clusters, allocate buffers if necessary, 548 - * ensure all the buffers are mapped, and bring uptodate any buffers that are 549 - * only partially being written to. 550 - * 551 - * If @nr_pages is greater than one, we are guaranteed that the cluster size is 552 - * greater than PAGE_SIZE, that all pages in @pages are entirely inside 553 - * the same cluster and that they are the entirety of that cluster, and that 554 - * the cluster is sparse, i.e. we need to allocate a cluster to fill the hole. 555 - * 556 - * i_size is not to be modified yet. 557 - * 558 - * Return 0 on success or -errno on error. 559 - */ 560 - static int ntfs_prepare_pages_for_non_resident_write(struct page **pages, 561 - unsigned nr_pages, s64 pos, size_t bytes) 562 - { 563 - VCN vcn, highest_vcn = 0, cpos, cend, bh_cpos, bh_cend; 564 - LCN lcn; 565 - s64 bh_pos, vcn_len, end, initialized_size; 566 - sector_t lcn_block; 567 - struct folio *folio; 568 - struct inode *vi; 569 - ntfs_inode *ni, *base_ni = NULL; 570 - ntfs_volume *vol; 571 - runlist_element *rl, *rl2; 572 - struct buffer_head *bh, *head, *wait[2], **wait_bh = wait; 573 - ntfs_attr_search_ctx *ctx = NULL; 574 - MFT_RECORD *m = NULL; 575 - ATTR_RECORD *a = NULL; 576 - unsigned long flags; 577 - u32 attr_rec_len = 0; 578 - unsigned blocksize, u; 579 - int err, mp_size; 580 - bool rl_write_locked, was_hole, is_retry; 581 - unsigned char blocksize_bits; 582 - struct { 583 - u8 runlist_merged:1; 584 - u8 mft_attr_mapped:1; 585 - u8 mp_rebuilt:1; 586 - u8 attr_switched:1; 587 - } status = { 0, 0, 0, 0 }; 588 - 589 - BUG_ON(!nr_pages); 590 - BUG_ON(!pages); 591 - BUG_ON(!*pages); 592 - vi = pages[0]->mapping->host; 593 - ni = NTFS_I(vi); 594 - vol = ni->vol; 595 - ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, start page " 596 - "index 0x%lx, nr_pages 0x%x, pos 0x%llx, bytes 0x%zx.", 597 - vi->i_ino, ni->type, pages[0]->index, nr_pages, 598 - (long long)pos, bytes); 599 - blocksize = vol->sb->s_blocksize; 600 - blocksize_bits = vol->sb->s_blocksize_bits; 601 - rl_write_locked = false; 602 - rl = NULL; 603 - err = 0; 604 - vcn = lcn = -1; 605 - vcn_len = 0; 606 - lcn_block = -1; 607 - was_hole = false; 608 - cpos = pos >> vol->cluster_size_bits; 609 - end = pos + bytes; 610 - cend = (end + vol->cluster_size - 1) >> vol->cluster_size_bits; 611 - /* 612 - * Loop over each buffer in each folio. Use goto to 613 - * reduce indentation. 614 - */ 615 - u = 0; 616 - do_next_folio: 617 - folio = page_folio(pages[u]); 618 - bh_pos = folio_pos(folio); 619 - head = folio_buffers(folio); 620 - if (!head) 621 - /* 622 - * create_empty_buffers() will create uptodate/dirty 623 - * buffers if the folio is uptodate/dirty. 624 - */ 625 - head = create_empty_buffers(folio, blocksize, 0); 626 - bh = head; 627 - do { 628 - VCN cdelta; 629 - s64 bh_end; 630 - unsigned bh_cofs; 631 - 632 - /* Clear buffer_new on all buffers to reinitialise state. */ 633 - if (buffer_new(bh)) 634 - clear_buffer_new(bh); 635 - bh_end = bh_pos + blocksize; 636 - bh_cpos = bh_pos >> vol->cluster_size_bits; 637 - bh_cofs = bh_pos & vol->cluster_size_mask; 638 - if (buffer_mapped(bh)) { 639 - /* 640 - * The buffer is already mapped. If it is uptodate, 641 - * ignore it. 642 - */ 643 - if (buffer_uptodate(bh)) 644 - continue; 645 - /* 646 - * The buffer is not uptodate. If the folio is uptodate 647 - * set the buffer uptodate and otherwise ignore it. 648 - */ 649 - if (folio_test_uptodate(folio)) { 650 - set_buffer_uptodate(bh); 651 - continue; 652 - } 653 - /* 654 - * Neither the folio nor the buffer are uptodate. If 655 - * the buffer is only partially being written to, we 656 - * need to read it in before the write, i.e. now. 657 - */ 658 - if ((bh_pos < pos && bh_end > pos) || 659 - (bh_pos < end && bh_end > end)) { 660 - /* 661 - * If the buffer is fully or partially within 662 - * the initialized size, do an actual read. 663 - * Otherwise, simply zero the buffer. 664 - */ 665 - read_lock_irqsave(&ni->size_lock, flags); 666 - initialized_size = ni->initialized_size; 667 - read_unlock_irqrestore(&ni->size_lock, flags); 668 - if (bh_pos < initialized_size) { 669 - ntfs_submit_bh_for_read(bh); 670 - *wait_bh++ = bh; 671 - } else { 672 - folio_zero_range(folio, bh_offset(bh), 673 - blocksize); 674 - set_buffer_uptodate(bh); 675 - } 676 - } 677 - continue; 678 - } 679 - /* Unmapped buffer. Need to map it. */ 680 - bh->b_bdev = vol->sb->s_bdev; 681 - /* 682 - * If the current buffer is in the same clusters as the map 683 - * cache, there is no need to check the runlist again. The 684 - * map cache is made up of @vcn, which is the first cached file 685 - * cluster, @vcn_len which is the number of cached file 686 - * clusters, @lcn is the device cluster corresponding to @vcn, 687 - * and @lcn_block is the block number corresponding to @lcn. 688 - */ 689 - cdelta = bh_cpos - vcn; 690 - if (likely(!cdelta || (cdelta > 0 && cdelta < vcn_len))) { 691 - map_buffer_cached: 692 - BUG_ON(lcn < 0); 693 - bh->b_blocknr = lcn_block + 694 - (cdelta << (vol->cluster_size_bits - 695 - blocksize_bits)) + 696 - (bh_cofs >> blocksize_bits); 697 - set_buffer_mapped(bh); 698 - /* 699 - * If the folio is uptodate so is the buffer. If the 700 - * buffer is fully outside the write, we ignore it if 701 - * it was already allocated and we mark it dirty so it 702 - * gets written out if we allocated it. On the other 703 - * hand, if we allocated the buffer but we are not 704 - * marking it dirty we set buffer_new so we can do 705 - * error recovery. 706 - */ 707 - if (folio_test_uptodate(folio)) { 708 - if (!buffer_uptodate(bh)) 709 - set_buffer_uptodate(bh); 710 - if (unlikely(was_hole)) { 711 - /* We allocated the buffer. */ 712 - clean_bdev_bh_alias(bh); 713 - if (bh_end <= pos || bh_pos >= end) 714 - mark_buffer_dirty(bh); 715 - else 716 - set_buffer_new(bh); 717 - } 718 - continue; 719 - } 720 - /* Page is _not_ uptodate. */ 721 - if (likely(!was_hole)) { 722 - /* 723 - * Buffer was already allocated. If it is not 724 - * uptodate and is only partially being written 725 - * to, we need to read it in before the write, 726 - * i.e. now. 727 - */ 728 - if (!buffer_uptodate(bh) && bh_pos < end && 729 - bh_end > pos && 730 - (bh_pos < pos || 731 - bh_end > end)) { 732 - /* 733 - * If the buffer is fully or partially 734 - * within the initialized size, do an 735 - * actual read. Otherwise, simply zero 736 - * the buffer. 737 - */ 738 - read_lock_irqsave(&ni->size_lock, 739 - flags); 740 - initialized_size = ni->initialized_size; 741 - read_unlock_irqrestore(&ni->size_lock, 742 - flags); 743 - if (bh_pos < initialized_size) { 744 - ntfs_submit_bh_for_read(bh); 745 - *wait_bh++ = bh; 746 - } else { 747 - folio_zero_range(folio, 748 - bh_offset(bh), 749 - blocksize); 750 - set_buffer_uptodate(bh); 751 - } 752 - } 753 - continue; 754 - } 755 - /* We allocated the buffer. */ 756 - clean_bdev_bh_alias(bh); 757 - /* 758 - * If the buffer is fully outside the write, zero it, 759 - * set it uptodate, and mark it dirty so it gets 760 - * written out. If it is partially being written to, 761 - * zero region surrounding the write but leave it to 762 - * commit write to do anything else. Finally, if the 763 - * buffer is fully being overwritten, do nothing. 764 - */ 765 - if (bh_end <= pos || bh_pos >= end) { 766 - if (!buffer_uptodate(bh)) { 767 - folio_zero_range(folio, bh_offset(bh), 768 - blocksize); 769 - set_buffer_uptodate(bh); 770 - } 771 - mark_buffer_dirty(bh); 772 - continue; 773 - } 774 - set_buffer_new(bh); 775 - if (!buffer_uptodate(bh) && 776 - (bh_pos < pos || bh_end > end)) { 777 - u8 *kaddr; 778 - unsigned pofs; 779 - 780 - kaddr = kmap_local_folio(folio, 0); 781 - if (bh_pos < pos) { 782 - pofs = bh_pos & ~PAGE_MASK; 783 - memset(kaddr + pofs, 0, pos - bh_pos); 784 - } 785 - if (bh_end > end) { 786 - pofs = end & ~PAGE_MASK; 787 - memset(kaddr + pofs, 0, bh_end - end); 788 - } 789 - kunmap_local(kaddr); 790 - flush_dcache_folio(folio); 791 - } 792 - continue; 793 - } 794 - /* 795 - * Slow path: this is the first buffer in the cluster. If it 796 - * is outside allocated size and is not uptodate, zero it and 797 - * set it uptodate. 798 - */ 799 - read_lock_irqsave(&ni->size_lock, flags); 800 - initialized_size = ni->allocated_size; 801 - read_unlock_irqrestore(&ni->size_lock, flags); 802 - if (bh_pos > initialized_size) { 803 - if (folio_test_uptodate(folio)) { 804 - if (!buffer_uptodate(bh)) 805 - set_buffer_uptodate(bh); 806 - } else if (!buffer_uptodate(bh)) { 807 - folio_zero_range(folio, bh_offset(bh), 808 - blocksize); 809 - set_buffer_uptodate(bh); 810 - } 811 - continue; 812 - } 813 - is_retry = false; 814 - if (!rl) { 815 - down_read(&ni->runlist.lock); 816 - retry_remap: 817 - rl = ni->runlist.rl; 818 - } 819 - if (likely(rl != NULL)) { 820 - /* Seek to element containing target cluster. */ 821 - while (rl->length && rl[1].vcn <= bh_cpos) 822 - rl++; 823 - lcn = ntfs_rl_vcn_to_lcn(rl, bh_cpos); 824 - if (likely(lcn >= 0)) { 825 - /* 826 - * Successful remap, setup the map cache and 827 - * use that to deal with the buffer. 828 - */ 829 - was_hole = false; 830 - vcn = bh_cpos; 831 - vcn_len = rl[1].vcn - vcn; 832 - lcn_block = lcn << (vol->cluster_size_bits - 833 - blocksize_bits); 834 - cdelta = 0; 835 - /* 836 - * If the number of remaining clusters touched 837 - * by the write is smaller or equal to the 838 - * number of cached clusters, unlock the 839 - * runlist as the map cache will be used from 840 - * now on. 841 - */ 842 - if (likely(vcn + vcn_len >= cend)) { 843 - if (rl_write_locked) { 844 - up_write(&ni->runlist.lock); 845 - rl_write_locked = false; 846 - } else 847 - up_read(&ni->runlist.lock); 848 - rl = NULL; 849 - } 850 - goto map_buffer_cached; 851 - } 852 - } else 853 - lcn = LCN_RL_NOT_MAPPED; 854 - /* 855 - * If it is not a hole and not out of bounds, the runlist is 856 - * probably unmapped so try to map it now. 857 - */ 858 - if (unlikely(lcn != LCN_HOLE && lcn != LCN_ENOENT)) { 859 - if (likely(!is_retry && lcn == LCN_RL_NOT_MAPPED)) { 860 - /* Attempt to map runlist. */ 861 - if (!rl_write_locked) { 862 - /* 863 - * We need the runlist locked for 864 - * writing, so if it is locked for 865 - * reading relock it now and retry in 866 - * case it changed whilst we dropped 867 - * the lock. 868 - */ 869 - up_read(&ni->runlist.lock); 870 - down_write(&ni->runlist.lock); 871 - rl_write_locked = true; 872 - goto retry_remap; 873 - } 874 - err = ntfs_map_runlist_nolock(ni, bh_cpos, 875 - NULL); 876 - if (likely(!err)) { 877 - is_retry = true; 878 - goto retry_remap; 879 - } 880 - /* 881 - * If @vcn is out of bounds, pretend @lcn is 882 - * LCN_ENOENT. As long as the buffer is out 883 - * of bounds this will work fine. 884 - */ 885 - if (err == -ENOENT) { 886 - lcn = LCN_ENOENT; 887 - err = 0; 888 - goto rl_not_mapped_enoent; 889 - } 890 - } else 891 - err = -EIO; 892 - /* Failed to map the buffer, even after retrying. */ 893 - bh->b_blocknr = -1; 894 - ntfs_error(vol->sb, "Failed to write to inode 0x%lx, " 895 - "attribute type 0x%x, vcn 0x%llx, " 896 - "vcn offset 0x%x, because its " 897 - "location on disk could not be " 898 - "determined%s (error code %i).", 899 - ni->mft_no, ni->type, 900 - (unsigned long long)bh_cpos, 901 - (unsigned)bh_pos & 902 - vol->cluster_size_mask, 903 - is_retry ? " even after retrying" : "", 904 - err); 905 - break; 906 - } 907 - rl_not_mapped_enoent: 908 - /* 909 - * The buffer is in a hole or out of bounds. We need to fill 910 - * the hole, unless the buffer is in a cluster which is not 911 - * touched by the write, in which case we just leave the buffer 912 - * unmapped. This can only happen when the cluster size is 913 - * less than the page cache size. 914 - */ 915 - if (unlikely(vol->cluster_size < PAGE_SIZE)) { 916 - bh_cend = (bh_end + vol->cluster_size - 1) >> 917 - vol->cluster_size_bits; 918 - if ((bh_cend <= cpos || bh_cpos >= cend)) { 919 - bh->b_blocknr = -1; 920 - /* 921 - * If the buffer is uptodate we skip it. If it 922 - * is not but the folio is uptodate, we can set 923 - * the buffer uptodate. If the folio is not 924 - * uptodate, we can clear the buffer and set it 925 - * uptodate. Whether this is worthwhile is 926 - * debatable and this could be removed. 927 - */ 928 - if (folio_test_uptodate(folio)) { 929 - if (!buffer_uptodate(bh)) 930 - set_buffer_uptodate(bh); 931 - } else if (!buffer_uptodate(bh)) { 932 - folio_zero_range(folio, bh_offset(bh), 933 - blocksize); 934 - set_buffer_uptodate(bh); 935 - } 936 - continue; 937 - } 938 - } 939 - /* 940 - * Out of bounds buffer is invalid if it was not really out of 941 - * bounds. 942 - */ 943 - BUG_ON(lcn != LCN_HOLE); 944 - /* 945 - * We need the runlist locked for writing, so if it is locked 946 - * for reading relock it now and retry in case it changed 947 - * whilst we dropped the lock. 948 - */ 949 - BUG_ON(!rl); 950 - if (!rl_write_locked) { 951 - up_read(&ni->runlist.lock); 952 - down_write(&ni->runlist.lock); 953 - rl_write_locked = true; 954 - goto retry_remap; 955 - } 956 - /* Find the previous last allocated cluster. */ 957 - BUG_ON(rl->lcn != LCN_HOLE); 958 - lcn = -1; 959 - rl2 = rl; 960 - while (--rl2 >= ni->runlist.rl) { 961 - if (rl2->lcn >= 0) { 962 - lcn = rl2->lcn + rl2->length; 963 - break; 964 - } 965 - } 966 - rl2 = ntfs_cluster_alloc(vol, bh_cpos, 1, lcn, DATA_ZONE, 967 - false); 968 - if (IS_ERR(rl2)) { 969 - err = PTR_ERR(rl2); 970 - ntfs_debug("Failed to allocate cluster, error code %i.", 971 - err); 972 - break; 973 - } 974 - lcn = rl2->lcn; 975 - rl = ntfs_runlists_merge(ni->runlist.rl, rl2); 976 - if (IS_ERR(rl)) { 977 - err = PTR_ERR(rl); 978 - if (err != -ENOMEM) 979 - err = -EIO; 980 - if (ntfs_cluster_free_from_rl(vol, rl2)) { 981 - ntfs_error(vol->sb, "Failed to release " 982 - "allocated cluster in error " 983 - "code path. Run chkdsk to " 984 - "recover the lost cluster."); 985 - NVolSetErrors(vol); 986 - } 987 - ntfs_free(rl2); 988 - break; 989 - } 990 - ni->runlist.rl = rl; 991 - status.runlist_merged = 1; 992 - ntfs_debug("Allocated cluster, lcn 0x%llx.", 993 - (unsigned long long)lcn); 994 - /* Map and lock the mft record and get the attribute record. */ 995 - if (!NInoAttr(ni)) 996 - base_ni = ni; 997 - else 998 - base_ni = ni->ext.base_ntfs_ino; 999 - m = map_mft_record(base_ni); 1000 - if (IS_ERR(m)) { 1001 - err = PTR_ERR(m); 1002 - break; 1003 - } 1004 - ctx = ntfs_attr_get_search_ctx(base_ni, m); 1005 - if (unlikely(!ctx)) { 1006 - err = -ENOMEM; 1007 - unmap_mft_record(base_ni); 1008 - break; 1009 - } 1010 - status.mft_attr_mapped = 1; 1011 - err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 1012 - CASE_SENSITIVE, bh_cpos, NULL, 0, ctx); 1013 - if (unlikely(err)) { 1014 - if (err == -ENOENT) 1015 - err = -EIO; 1016 - break; 1017 - } 1018 - m = ctx->mrec; 1019 - a = ctx->attr; 1020 - /* 1021 - * Find the runlist element with which the attribute extent 1022 - * starts. Note, we cannot use the _attr_ version because we 1023 - * have mapped the mft record. That is ok because we know the 1024 - * runlist fragment must be mapped already to have ever gotten 1025 - * here, so we can just use the _rl_ version. 1026 - */ 1027 - vcn = sle64_to_cpu(a->data.non_resident.lowest_vcn); 1028 - rl2 = ntfs_rl_find_vcn_nolock(rl, vcn); 1029 - BUG_ON(!rl2); 1030 - BUG_ON(!rl2->length); 1031 - BUG_ON(rl2->lcn < LCN_HOLE); 1032 - highest_vcn = sle64_to_cpu(a->data.non_resident.highest_vcn); 1033 - /* 1034 - * If @highest_vcn is zero, calculate the real highest_vcn 1035 - * (which can really be zero). 1036 - */ 1037 - if (!highest_vcn) 1038 - highest_vcn = (sle64_to_cpu( 1039 - a->data.non_resident.allocated_size) >> 1040 - vol->cluster_size_bits) - 1; 1041 - /* 1042 - * Determine the size of the mapping pairs array for the new 1043 - * extent, i.e. the old extent with the hole filled. 1044 - */ 1045 - mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, vcn, 1046 - highest_vcn); 1047 - if (unlikely(mp_size <= 0)) { 1048 - if (!(err = mp_size)) 1049 - err = -EIO; 1050 - ntfs_debug("Failed to get size for mapping pairs " 1051 - "array, error code %i.", err); 1052 - break; 1053 - } 1054 - /* 1055 - * Resize the attribute record to fit the new mapping pairs 1056 - * array. 1057 - */ 1058 - attr_rec_len = le32_to_cpu(a->length); 1059 - err = ntfs_attr_record_resize(m, a, mp_size + le16_to_cpu( 1060 - a->data.non_resident.mapping_pairs_offset)); 1061 - if (unlikely(err)) { 1062 - BUG_ON(err != -ENOSPC); 1063 - // TODO: Deal with this by using the current attribute 1064 - // and fill it with as much of the mapping pairs 1065 - // array as possible. Then loop over each attribute 1066 - // extent rewriting the mapping pairs arrays as we go 1067 - // along and if when we reach the end we have not 1068 - // enough space, try to resize the last attribute 1069 - // extent and if even that fails, add a new attribute 1070 - // extent. 1071 - // We could also try to resize at each step in the hope 1072 - // that we will not need to rewrite every single extent. 1073 - // Note, we may need to decompress some extents to fill 1074 - // the runlist as we are walking the extents... 1075 - ntfs_error(vol->sb, "Not enough space in the mft " 1076 - "record for the extended attribute " 1077 - "record. This case is not " 1078 - "implemented yet."); 1079 - err = -EOPNOTSUPP; 1080 - break ; 1081 - } 1082 - status.mp_rebuilt = 1; 1083 - /* 1084 - * Generate the mapping pairs array directly into the attribute 1085 - * record. 1086 - */ 1087 - err = ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu( 1088 - a->data.non_resident.mapping_pairs_offset), 1089 - mp_size, rl2, vcn, highest_vcn, NULL); 1090 - if (unlikely(err)) { 1091 - ntfs_error(vol->sb, "Cannot fill hole in inode 0x%lx, " 1092 - "attribute type 0x%x, because building " 1093 - "the mapping pairs failed with error " 1094 - "code %i.", vi->i_ino, 1095 - (unsigned)le32_to_cpu(ni->type), err); 1096 - err = -EIO; 1097 - break; 1098 - } 1099 - /* Update the highest_vcn but only if it was not set. */ 1100 - if (unlikely(!a->data.non_resident.highest_vcn)) 1101 - a->data.non_resident.highest_vcn = 1102 - cpu_to_sle64(highest_vcn); 1103 - /* 1104 - * If the attribute is sparse/compressed, update the compressed 1105 - * size in the ntfs_inode structure and the attribute record. 1106 - */ 1107 - if (likely(NInoSparse(ni) || NInoCompressed(ni))) { 1108 - /* 1109 - * If we are not in the first attribute extent, switch 1110 - * to it, but first ensure the changes will make it to 1111 - * disk later. 1112 - */ 1113 - if (a->data.non_resident.lowest_vcn) { 1114 - flush_dcache_mft_record_page(ctx->ntfs_ino); 1115 - mark_mft_record_dirty(ctx->ntfs_ino); 1116 - ntfs_attr_reinit_search_ctx(ctx); 1117 - err = ntfs_attr_lookup(ni->type, ni->name, 1118 - ni->name_len, CASE_SENSITIVE, 1119 - 0, NULL, 0, ctx); 1120 - if (unlikely(err)) { 1121 - status.attr_switched = 1; 1122 - break; 1123 - } 1124 - /* @m is not used any more so do not set it. */ 1125 - a = ctx->attr; 1126 - } 1127 - write_lock_irqsave(&ni->size_lock, flags); 1128 - ni->itype.compressed.size += vol->cluster_size; 1129 - a->data.non_resident.compressed_size = 1130 - cpu_to_sle64(ni->itype.compressed.size); 1131 - write_unlock_irqrestore(&ni->size_lock, flags); 1132 - } 1133 - /* Ensure the changes make it to disk. */ 1134 - flush_dcache_mft_record_page(ctx->ntfs_ino); 1135 - mark_mft_record_dirty(ctx->ntfs_ino); 1136 - ntfs_attr_put_search_ctx(ctx); 1137 - unmap_mft_record(base_ni); 1138 - /* Successfully filled the hole. */ 1139 - status.runlist_merged = 0; 1140 - status.mft_attr_mapped = 0; 1141 - status.mp_rebuilt = 0; 1142 - /* Setup the map cache and use that to deal with the buffer. */ 1143 - was_hole = true; 1144 - vcn = bh_cpos; 1145 - vcn_len = 1; 1146 - lcn_block = lcn << (vol->cluster_size_bits - blocksize_bits); 1147 - cdelta = 0; 1148 - /* 1149 - * If the number of remaining clusters in the @pages is smaller 1150 - * or equal to the number of cached clusters, unlock the 1151 - * runlist as the map cache will be used from now on. 1152 - */ 1153 - if (likely(vcn + vcn_len >= cend)) { 1154 - up_write(&ni->runlist.lock); 1155 - rl_write_locked = false; 1156 - rl = NULL; 1157 - } 1158 - goto map_buffer_cached; 1159 - } while (bh_pos += blocksize, (bh = bh->b_this_page) != head); 1160 - /* If there are no errors, do the next page. */ 1161 - if (likely(!err && ++u < nr_pages)) 1162 - goto do_next_folio; 1163 - /* If there are no errors, release the runlist lock if we took it. */ 1164 - if (likely(!err)) { 1165 - if (unlikely(rl_write_locked)) { 1166 - up_write(&ni->runlist.lock); 1167 - rl_write_locked = false; 1168 - } else if (unlikely(rl)) 1169 - up_read(&ni->runlist.lock); 1170 - rl = NULL; 1171 - } 1172 - /* If we issued read requests, let them complete. */ 1173 - read_lock_irqsave(&ni->size_lock, flags); 1174 - initialized_size = ni->initialized_size; 1175 - read_unlock_irqrestore(&ni->size_lock, flags); 1176 - while (wait_bh > wait) { 1177 - bh = *--wait_bh; 1178 - wait_on_buffer(bh); 1179 - if (likely(buffer_uptodate(bh))) { 1180 - folio = bh->b_folio; 1181 - bh_pos = folio_pos(folio) + bh_offset(bh); 1182 - /* 1183 - * If the buffer overflows the initialized size, need 1184 - * to zero the overflowing region. 1185 - */ 1186 - if (unlikely(bh_pos + blocksize > initialized_size)) { 1187 - int ofs = 0; 1188 - 1189 - if (likely(bh_pos < initialized_size)) 1190 - ofs = initialized_size - bh_pos; 1191 - folio_zero_segment(folio, bh_offset(bh) + ofs, 1192 - blocksize); 1193 - } 1194 - } else /* if (unlikely(!buffer_uptodate(bh))) */ 1195 - err = -EIO; 1196 - } 1197 - if (likely(!err)) { 1198 - /* Clear buffer_new on all buffers. */ 1199 - u = 0; 1200 - do { 1201 - bh = head = page_buffers(pages[u]); 1202 - do { 1203 - if (buffer_new(bh)) 1204 - clear_buffer_new(bh); 1205 - } while ((bh = bh->b_this_page) != head); 1206 - } while (++u < nr_pages); 1207 - ntfs_debug("Done."); 1208 - return err; 1209 - } 1210 - if (status.attr_switched) { 1211 - /* Get back to the attribute extent we modified. */ 1212 - ntfs_attr_reinit_search_ctx(ctx); 1213 - if (ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 1214 - CASE_SENSITIVE, bh_cpos, NULL, 0, ctx)) { 1215 - ntfs_error(vol->sb, "Failed to find required " 1216 - "attribute extent of attribute in " 1217 - "error code path. Run chkdsk to " 1218 - "recover."); 1219 - write_lock_irqsave(&ni->size_lock, flags); 1220 - ni->itype.compressed.size += vol->cluster_size; 1221 - write_unlock_irqrestore(&ni->size_lock, flags); 1222 - flush_dcache_mft_record_page(ctx->ntfs_ino); 1223 - mark_mft_record_dirty(ctx->ntfs_ino); 1224 - /* 1225 - * The only thing that is now wrong is the compressed 1226 - * size of the base attribute extent which chkdsk 1227 - * should be able to fix. 1228 - */ 1229 - NVolSetErrors(vol); 1230 - } else { 1231 - m = ctx->mrec; 1232 - a = ctx->attr; 1233 - status.attr_switched = 0; 1234 - } 1235 - } 1236 - /* 1237 - * If the runlist has been modified, need to restore it by punching a 1238 - * hole into it and we then need to deallocate the on-disk cluster as 1239 - * well. Note, we only modify the runlist if we are able to generate a 1240 - * new mapping pairs array, i.e. only when the mapped attribute extent 1241 - * is not switched. 1242 - */ 1243 - if (status.runlist_merged && !status.attr_switched) { 1244 - BUG_ON(!rl_write_locked); 1245 - /* Make the file cluster we allocated sparse in the runlist. */ 1246 - if (ntfs_rl_punch_nolock(vol, &ni->runlist, bh_cpos, 1)) { 1247 - ntfs_error(vol->sb, "Failed to punch hole into " 1248 - "attribute runlist in error code " 1249 - "path. Run chkdsk to recover the " 1250 - "lost cluster."); 1251 - NVolSetErrors(vol); 1252 - } else /* if (success) */ { 1253 - status.runlist_merged = 0; 1254 - /* 1255 - * Deallocate the on-disk cluster we allocated but only 1256 - * if we succeeded in punching its vcn out of the 1257 - * runlist. 1258 - */ 1259 - down_write(&vol->lcnbmp_lock); 1260 - if (ntfs_bitmap_clear_bit(vol->lcnbmp_ino, lcn)) { 1261 - ntfs_error(vol->sb, "Failed to release " 1262 - "allocated cluster in error " 1263 - "code path. Run chkdsk to " 1264 - "recover the lost cluster."); 1265 - NVolSetErrors(vol); 1266 - } 1267 - up_write(&vol->lcnbmp_lock); 1268 - } 1269 - } 1270 - /* 1271 - * Resize the attribute record to its old size and rebuild the mapping 1272 - * pairs array. Note, we only can do this if the runlist has been 1273 - * restored to its old state which also implies that the mapped 1274 - * attribute extent is not switched. 1275 - */ 1276 - if (status.mp_rebuilt && !status.runlist_merged) { 1277 - if (ntfs_attr_record_resize(m, a, attr_rec_len)) { 1278 - ntfs_error(vol->sb, "Failed to restore attribute " 1279 - "record in error code path. Run " 1280 - "chkdsk to recover."); 1281 - NVolSetErrors(vol); 1282 - } else /* if (success) */ { 1283 - if (ntfs_mapping_pairs_build(vol, (u8*)a + 1284 - le16_to_cpu(a->data.non_resident. 1285 - mapping_pairs_offset), attr_rec_len - 1286 - le16_to_cpu(a->data.non_resident. 1287 - mapping_pairs_offset), ni->runlist.rl, 1288 - vcn, highest_vcn, NULL)) { 1289 - ntfs_error(vol->sb, "Failed to restore " 1290 - "mapping pairs array in error " 1291 - "code path. Run chkdsk to " 1292 - "recover."); 1293 - NVolSetErrors(vol); 1294 - } 1295 - flush_dcache_mft_record_page(ctx->ntfs_ino); 1296 - mark_mft_record_dirty(ctx->ntfs_ino); 1297 - } 1298 - } 1299 - /* Release the mft record and the attribute. */ 1300 - if (status.mft_attr_mapped) { 1301 - ntfs_attr_put_search_ctx(ctx); 1302 - unmap_mft_record(base_ni); 1303 - } 1304 - /* Release the runlist lock. */ 1305 - if (rl_write_locked) 1306 - up_write(&ni->runlist.lock); 1307 - else if (rl) 1308 - up_read(&ni->runlist.lock); 1309 - /* 1310 - * Zero out any newly allocated blocks to avoid exposing stale data. 1311 - * If BH_New is set, we know that the block was newly allocated above 1312 - * and that it has not been fully zeroed and marked dirty yet. 1313 - */ 1314 - nr_pages = u; 1315 - u = 0; 1316 - end = bh_cpos << vol->cluster_size_bits; 1317 - do { 1318 - folio = page_folio(pages[u]); 1319 - bh = head = folio_buffers(folio); 1320 - do { 1321 - if (u == nr_pages && 1322 - folio_pos(folio) + bh_offset(bh) >= end) 1323 - break; 1324 - if (!buffer_new(bh)) 1325 - continue; 1326 - clear_buffer_new(bh); 1327 - if (!buffer_uptodate(bh)) { 1328 - if (folio_test_uptodate(folio)) 1329 - set_buffer_uptodate(bh); 1330 - else { 1331 - folio_zero_range(folio, bh_offset(bh), 1332 - blocksize); 1333 - set_buffer_uptodate(bh); 1334 - } 1335 - } 1336 - mark_buffer_dirty(bh); 1337 - } while ((bh = bh->b_this_page) != head); 1338 - } while (++u <= nr_pages); 1339 - ntfs_error(vol->sb, "Failed. Returning error code %i.", err); 1340 - return err; 1341 - } 1342 - 1343 - static inline void ntfs_flush_dcache_pages(struct page **pages, 1344 - unsigned nr_pages) 1345 - { 1346 - BUG_ON(!nr_pages); 1347 - /* 1348 - * Warning: Do not do the decrement at the same time as the call to 1349 - * flush_dcache_page() because it is a NULL macro on i386 and hence the 1350 - * decrement never happens so the loop never terminates. 1351 - */ 1352 - do { 1353 - --nr_pages; 1354 - flush_dcache_page(pages[nr_pages]); 1355 - } while (nr_pages > 0); 1356 - } 1357 - 1358 - /** 1359 - * ntfs_commit_pages_after_non_resident_write - commit the received data 1360 - * @pages: array of destination pages 1361 - * @nr_pages: number of pages in @pages 1362 - * @pos: byte position in file at which the write begins 1363 - * @bytes: number of bytes to be written 1364 - * 1365 - * See description of ntfs_commit_pages_after_write(), below. 1366 - */ 1367 - static inline int ntfs_commit_pages_after_non_resident_write( 1368 - struct page **pages, const unsigned nr_pages, 1369 - s64 pos, size_t bytes) 1370 - { 1371 - s64 end, initialized_size; 1372 - struct inode *vi; 1373 - ntfs_inode *ni, *base_ni; 1374 - struct buffer_head *bh, *head; 1375 - ntfs_attr_search_ctx *ctx; 1376 - MFT_RECORD *m; 1377 - ATTR_RECORD *a; 1378 - unsigned long flags; 1379 - unsigned blocksize, u; 1380 - int err; 1381 - 1382 - vi = pages[0]->mapping->host; 1383 - ni = NTFS_I(vi); 1384 - blocksize = vi->i_sb->s_blocksize; 1385 - end = pos + bytes; 1386 - u = 0; 1387 - do { 1388 - s64 bh_pos; 1389 - struct page *page; 1390 - bool partial; 1391 - 1392 - page = pages[u]; 1393 - bh_pos = (s64)page->index << PAGE_SHIFT; 1394 - bh = head = page_buffers(page); 1395 - partial = false; 1396 - do { 1397 - s64 bh_end; 1398 - 1399 - bh_end = bh_pos + blocksize; 1400 - if (bh_end <= pos || bh_pos >= end) { 1401 - if (!buffer_uptodate(bh)) 1402 - partial = true; 1403 - } else { 1404 - set_buffer_uptodate(bh); 1405 - mark_buffer_dirty(bh); 1406 - } 1407 - } while (bh_pos += blocksize, (bh = bh->b_this_page) != head); 1408 - /* 1409 - * If all buffers are now uptodate but the page is not, set the 1410 - * page uptodate. 1411 - */ 1412 - if (!partial && !PageUptodate(page)) 1413 - SetPageUptodate(page); 1414 - } while (++u < nr_pages); 1415 - /* 1416 - * Finally, if we do not need to update initialized_size or i_size we 1417 - * are finished. 1418 - */ 1419 - read_lock_irqsave(&ni->size_lock, flags); 1420 - initialized_size = ni->initialized_size; 1421 - read_unlock_irqrestore(&ni->size_lock, flags); 1422 - if (end <= initialized_size) { 1423 - ntfs_debug("Done."); 1424 - return 0; 1425 - } 1426 - /* 1427 - * Update initialized_size/i_size as appropriate, both in the inode and 1428 - * the mft record. 1429 - */ 1430 - if (!NInoAttr(ni)) 1431 - base_ni = ni; 1432 - else 1433 - base_ni = ni->ext.base_ntfs_ino; 1434 - /* Map, pin, and lock the mft record. */ 1435 - m = map_mft_record(base_ni); 1436 - if (IS_ERR(m)) { 1437 - err = PTR_ERR(m); 1438 - m = NULL; 1439 - ctx = NULL; 1440 - goto err_out; 1441 - } 1442 - BUG_ON(!NInoNonResident(ni)); 1443 - ctx = ntfs_attr_get_search_ctx(base_ni, m); 1444 - if (unlikely(!ctx)) { 1445 - err = -ENOMEM; 1446 - goto err_out; 1447 - } 1448 - err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 1449 - CASE_SENSITIVE, 0, NULL, 0, ctx); 1450 - if (unlikely(err)) { 1451 - if (err == -ENOENT) 1452 - err = -EIO; 1453 - goto err_out; 1454 - } 1455 - a = ctx->attr; 1456 - BUG_ON(!a->non_resident); 1457 - write_lock_irqsave(&ni->size_lock, flags); 1458 - BUG_ON(end > ni->allocated_size); 1459 - ni->initialized_size = end; 1460 - a->data.non_resident.initialized_size = cpu_to_sle64(end); 1461 - if (end > i_size_read(vi)) { 1462 - i_size_write(vi, end); 1463 - a->data.non_resident.data_size = 1464 - a->data.non_resident.initialized_size; 1465 - } 1466 - write_unlock_irqrestore(&ni->size_lock, flags); 1467 - /* Mark the mft record dirty, so it gets written back. */ 1468 - flush_dcache_mft_record_page(ctx->ntfs_ino); 1469 - mark_mft_record_dirty(ctx->ntfs_ino); 1470 - ntfs_attr_put_search_ctx(ctx); 1471 - unmap_mft_record(base_ni); 1472 - ntfs_debug("Done."); 1473 136 return 0; 1474 - err_out: 1475 - if (ctx) 1476 - ntfs_attr_put_search_ctx(ctx); 1477 - if (m) 1478 - unmap_mft_record(base_ni); 1479 - ntfs_error(vi->i_sb, "Failed to update initialized_size/i_size (error " 1480 - "code %i).", err); 1481 - if (err != -ENOMEM) 1482 - NVolSetErrors(ni->vol); 1483 - return err; 1484 - } 1485 - 1486 - /** 1487 - * ntfs_commit_pages_after_write - commit the received data 1488 - * @pages: array of destination pages 1489 - * @nr_pages: number of pages in @pages 1490 - * @pos: byte position in file at which the write begins 1491 - * @bytes: number of bytes to be written 1492 - * 1493 - * This is called from ntfs_file_buffered_write() with i_mutex held on the inode 1494 - * (@pages[0]->mapping->host). There are @nr_pages pages in @pages which are 1495 - * locked but not kmap()ped. The source data has already been copied into the 1496 - * @page. ntfs_prepare_pages_for_non_resident_write() has been called before 1497 - * the data was copied (for non-resident attributes only) and it returned 1498 - * success. 1499 - * 1500 - * Need to set uptodate and mark dirty all buffers within the boundary of the 1501 - * write. If all buffers in a page are uptodate we set the page uptodate, too. 1502 - * 1503 - * Setting the buffers dirty ensures that they get written out later when 1504 - * ntfs_writepage() is invoked by the VM. 1505 - * 1506 - * Finally, we need to update i_size and initialized_size as appropriate both 1507 - * in the inode and the mft record. 1508 - * 1509 - * This is modelled after fs/buffer.c::generic_commit_write(), which marks 1510 - * buffers uptodate and dirty, sets the page uptodate if all buffers in the 1511 - * page are uptodate, and updates i_size if the end of io is beyond i_size. In 1512 - * that case, it also marks the inode dirty. 1513 - * 1514 - * If things have gone as outlined in 1515 - * ntfs_prepare_pages_for_non_resident_write(), we do not need to do any page 1516 - * content modifications here for non-resident attributes. For resident 1517 - * attributes we need to do the uptodate bringing here which we combine with 1518 - * the copying into the mft record which means we save one atomic kmap. 1519 - * 1520 - * Return 0 on success or -errno on error. 1521 - */ 1522 - static int ntfs_commit_pages_after_write(struct page **pages, 1523 - const unsigned nr_pages, s64 pos, size_t bytes) 1524 - { 1525 - s64 end, initialized_size; 1526 - loff_t i_size; 1527 - struct inode *vi; 1528 - ntfs_inode *ni, *base_ni; 1529 - struct page *page; 1530 - ntfs_attr_search_ctx *ctx; 1531 - MFT_RECORD *m; 1532 - ATTR_RECORD *a; 1533 - char *kattr, *kaddr; 1534 - unsigned long flags; 1535 - u32 attr_len; 1536 - int err; 1537 - 1538 - BUG_ON(!nr_pages); 1539 - BUG_ON(!pages); 1540 - page = pages[0]; 1541 - BUG_ON(!page); 1542 - vi = page->mapping->host; 1543 - ni = NTFS_I(vi); 1544 - ntfs_debug("Entering for inode 0x%lx, attribute type 0x%x, start page " 1545 - "index 0x%lx, nr_pages 0x%x, pos 0x%llx, bytes 0x%zx.", 1546 - vi->i_ino, ni->type, page->index, nr_pages, 1547 - (long long)pos, bytes); 1548 - if (NInoNonResident(ni)) 1549 - return ntfs_commit_pages_after_non_resident_write(pages, 1550 - nr_pages, pos, bytes); 1551 - BUG_ON(nr_pages > 1); 1552 - /* 1553 - * Attribute is resident, implying it is not compressed, encrypted, or 1554 - * sparse. 1555 - */ 1556 - if (!NInoAttr(ni)) 1557 - base_ni = ni; 1558 - else 1559 - base_ni = ni->ext.base_ntfs_ino; 1560 - BUG_ON(NInoNonResident(ni)); 1561 - /* Map, pin, and lock the mft record. */ 1562 - m = map_mft_record(base_ni); 1563 - if (IS_ERR(m)) { 1564 - err = PTR_ERR(m); 1565 - m = NULL; 1566 - ctx = NULL; 1567 - goto err_out; 1568 - } 1569 - ctx = ntfs_attr_get_search_ctx(base_ni, m); 1570 - if (unlikely(!ctx)) { 1571 - err = -ENOMEM; 1572 - goto err_out; 1573 - } 1574 - err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 1575 - CASE_SENSITIVE, 0, NULL, 0, ctx); 1576 - if (unlikely(err)) { 1577 - if (err == -ENOENT) 1578 - err = -EIO; 1579 - goto err_out; 1580 - } 1581 - a = ctx->attr; 1582 - BUG_ON(a->non_resident); 1583 - /* The total length of the attribute value. */ 1584 - attr_len = le32_to_cpu(a->data.resident.value_length); 1585 - i_size = i_size_read(vi); 1586 - BUG_ON(attr_len != i_size); 1587 - BUG_ON(pos > attr_len); 1588 - end = pos + bytes; 1589 - BUG_ON(end > le32_to_cpu(a->length) - 1590 - le16_to_cpu(a->data.resident.value_offset)); 1591 - kattr = (u8*)a + le16_to_cpu(a->data.resident.value_offset); 1592 - kaddr = kmap_atomic(page); 1593 - /* Copy the received data from the page to the mft record. */ 1594 - memcpy(kattr + pos, kaddr + pos, bytes); 1595 - /* Update the attribute length if necessary. */ 1596 - if (end > attr_len) { 1597 - attr_len = end; 1598 - a->data.resident.value_length = cpu_to_le32(attr_len); 1599 - } 1600 - /* 1601 - * If the page is not uptodate, bring the out of bounds area(s) 1602 - * uptodate by copying data from the mft record to the page. 1603 - */ 1604 - if (!PageUptodate(page)) { 1605 - if (pos > 0) 1606 - memcpy(kaddr, kattr, pos); 1607 - if (end < attr_len) 1608 - memcpy(kaddr + end, kattr + end, attr_len - end); 1609 - /* Zero the region outside the end of the attribute value. */ 1610 - memset(kaddr + attr_len, 0, PAGE_SIZE - attr_len); 1611 - flush_dcache_page(page); 1612 - SetPageUptodate(page); 1613 - } 1614 - kunmap_atomic(kaddr); 1615 - /* Update initialized_size/i_size if necessary. */ 1616 - read_lock_irqsave(&ni->size_lock, flags); 1617 - initialized_size = ni->initialized_size; 1618 - BUG_ON(end > ni->allocated_size); 1619 - read_unlock_irqrestore(&ni->size_lock, flags); 1620 - BUG_ON(initialized_size != i_size); 1621 - if (end > initialized_size) { 1622 - write_lock_irqsave(&ni->size_lock, flags); 1623 - ni->initialized_size = end; 1624 - i_size_write(vi, end); 1625 - write_unlock_irqrestore(&ni->size_lock, flags); 1626 - } 1627 - /* Mark the mft record dirty, so it gets written back. */ 1628 - flush_dcache_mft_record_page(ctx->ntfs_ino); 1629 - mark_mft_record_dirty(ctx->ntfs_ino); 1630 - ntfs_attr_put_search_ctx(ctx); 1631 - unmap_mft_record(base_ni); 1632 - ntfs_debug("Done."); 1633 - return 0; 1634 - err_out: 1635 - if (err == -ENOMEM) { 1636 - ntfs_warning(vi->i_sb, "Error allocating memory required to " 1637 - "commit the write."); 1638 - if (PageUptodate(page)) { 1639 - ntfs_warning(vi->i_sb, "Page is uptodate, setting " 1640 - "dirty so the write will be retried " 1641 - "later on by the VM."); 1642 - /* 1643 - * Put the page on mapping->dirty_pages, but leave its 1644 - * buffers' dirty state as-is. 1645 - */ 1646 - __set_page_dirty_nobuffers(page); 1647 - err = 0; 1648 - } else 1649 - ntfs_error(vi->i_sb, "Page is not uptodate. Written " 1650 - "data has been lost."); 1651 - } else { 1652 - ntfs_error(vi->i_sb, "Resident attribute commit write failed " 1653 - "with error %i.", err); 1654 - NVolSetErrors(ni->vol); 1655 - } 1656 - if (ctx) 1657 - ntfs_attr_put_search_ctx(ctx); 1658 - if (m) 1659 - unmap_mft_record(base_ni); 1660 - return err; 1661 137 } 1662 138 1663 139 /* 1664 - * Copy as much as we can into the pages and return the number of bytes which 1665 - * were successfully copied. If a fault is encountered then clear the pages 1666 - * out to (ofs + bytes) and return the number of bytes which were copied. 1667 - */ 1668 - static size_t ntfs_copy_from_user_iter(struct page **pages, unsigned nr_pages, 1669 - unsigned ofs, struct iov_iter *i, size_t bytes) 1670 - { 1671 - struct page **last_page = pages + nr_pages; 1672 - size_t total = 0; 1673 - unsigned len, copied; 1674 - 1675 - do { 1676 - len = PAGE_SIZE - ofs; 1677 - if (len > bytes) 1678 - len = bytes; 1679 - copied = copy_page_from_iter_atomic(*pages, ofs, len, i); 1680 - total += copied; 1681 - bytes -= copied; 1682 - if (!bytes) 1683 - break; 1684 - if (copied < len) 1685 - goto err; 1686 - ofs = 0; 1687 - } while (++pages < last_page); 1688 - out: 1689 - return total; 1690 - err: 1691 - /* Zero the rest of the target like __copy_from_user(). */ 1692 - len = PAGE_SIZE - copied; 1693 - do { 1694 - if (len > bytes) 1695 - len = bytes; 1696 - zero_user(*pages, copied, len); 1697 - bytes -= len; 1698 - copied = 0; 1699 - len = PAGE_SIZE; 1700 - } while (++pages < last_page); 1701 - goto out; 1702 - } 1703 - 1704 - /** 1705 - * ntfs_perform_write - perform buffered write to a file 1706 - * @file: file to write to 1707 - * @i: iov_iter with data to write 1708 - * @pos: byte offset in file at which to begin writing to 1709 - */ 1710 - static ssize_t ntfs_perform_write(struct file *file, struct iov_iter *i, 1711 - loff_t pos) 1712 - { 1713 - struct address_space *mapping = file->f_mapping; 1714 - struct inode *vi = mapping->host; 1715 - ntfs_inode *ni = NTFS_I(vi); 1716 - ntfs_volume *vol = ni->vol; 1717 - struct page *pages[NTFS_MAX_PAGES_PER_CLUSTER]; 1718 - struct page *cached_page = NULL; 1719 - VCN last_vcn; 1720 - LCN lcn; 1721 - size_t bytes; 1722 - ssize_t status, written = 0; 1723 - unsigned nr_pages; 1724 - 1725 - ntfs_debug("Entering for i_ino 0x%lx, attribute type 0x%x, pos " 1726 - "0x%llx, count 0x%lx.", vi->i_ino, 1727 - (unsigned)le32_to_cpu(ni->type), 1728 - (unsigned long long)pos, 1729 - (unsigned long)iov_iter_count(i)); 1730 - /* 1731 - * If a previous ntfs_truncate() failed, repeat it and abort if it 1732 - * fails again. 1733 - */ 1734 - if (unlikely(NInoTruncateFailed(ni))) { 1735 - int err; 1736 - 1737 - inode_dio_wait(vi); 1738 - err = ntfs_truncate(vi); 1739 - if (err || NInoTruncateFailed(ni)) { 1740 - if (!err) 1741 - err = -EIO; 1742 - ntfs_error(vol->sb, "Cannot perform write to inode " 1743 - "0x%lx, attribute type 0x%x, because " 1744 - "ntfs_truncate() failed (error code " 1745 - "%i).", vi->i_ino, 1746 - (unsigned)le32_to_cpu(ni->type), err); 1747 - return err; 1748 - } 1749 - } 1750 - /* 1751 - * Determine the number of pages per cluster for non-resident 1752 - * attributes. 1753 - */ 1754 - nr_pages = 1; 1755 - if (vol->cluster_size > PAGE_SIZE && NInoNonResident(ni)) 1756 - nr_pages = vol->cluster_size >> PAGE_SHIFT; 1757 - last_vcn = -1; 1758 - do { 1759 - VCN vcn; 1760 - pgoff_t start_idx; 1761 - unsigned ofs, do_pages, u; 1762 - size_t copied; 1763 - 1764 - start_idx = pos >> PAGE_SHIFT; 1765 - ofs = pos & ~PAGE_MASK; 1766 - bytes = PAGE_SIZE - ofs; 1767 - do_pages = 1; 1768 - if (nr_pages > 1) { 1769 - vcn = pos >> vol->cluster_size_bits; 1770 - if (vcn != last_vcn) { 1771 - last_vcn = vcn; 1772 - /* 1773 - * Get the lcn of the vcn the write is in. If 1774 - * it is a hole, need to lock down all pages in 1775 - * the cluster. 1776 - */ 1777 - down_read(&ni->runlist.lock); 1778 - lcn = ntfs_attr_vcn_to_lcn_nolock(ni, pos >> 1779 - vol->cluster_size_bits, false); 1780 - up_read(&ni->runlist.lock); 1781 - if (unlikely(lcn < LCN_HOLE)) { 1782 - if (lcn == LCN_ENOMEM) 1783 - status = -ENOMEM; 1784 - else { 1785 - status = -EIO; 1786 - ntfs_error(vol->sb, "Cannot " 1787 - "perform write to " 1788 - "inode 0x%lx, " 1789 - "attribute type 0x%x, " 1790 - "because the attribute " 1791 - "is corrupt.", 1792 - vi->i_ino, (unsigned) 1793 - le32_to_cpu(ni->type)); 1794 - } 1795 - break; 1796 - } 1797 - if (lcn == LCN_HOLE) { 1798 - start_idx = (pos & ~(s64) 1799 - vol->cluster_size_mask) 1800 - >> PAGE_SHIFT; 1801 - bytes = vol->cluster_size - (pos & 1802 - vol->cluster_size_mask); 1803 - do_pages = nr_pages; 1804 - } 1805 - } 1806 - } 1807 - if (bytes > iov_iter_count(i)) 1808 - bytes = iov_iter_count(i); 1809 - again: 1810 - /* 1811 - * Bring in the user page(s) that we will copy from _first_. 1812 - * Otherwise there is a nasty deadlock on copying from the same 1813 - * page(s) as we are writing to, without it/them being marked 1814 - * up-to-date. Note, at present there is nothing to stop the 1815 - * pages being swapped out between us bringing them into memory 1816 - * and doing the actual copying. 1817 - */ 1818 - if (unlikely(fault_in_iov_iter_readable(i, bytes))) { 1819 - status = -EFAULT; 1820 - break; 1821 - } 1822 - /* Get and lock @do_pages starting at index @start_idx. */ 1823 - status = __ntfs_grab_cache_pages(mapping, start_idx, do_pages, 1824 - pages, &cached_page); 1825 - if (unlikely(status)) 1826 - break; 1827 - /* 1828 - * For non-resident attributes, we need to fill any holes with 1829 - * actual clusters and ensure all bufferes are mapped. We also 1830 - * need to bring uptodate any buffers that are only partially 1831 - * being written to. 1832 - */ 1833 - if (NInoNonResident(ni)) { 1834 - status = ntfs_prepare_pages_for_non_resident_write( 1835 - pages, do_pages, pos, bytes); 1836 - if (unlikely(status)) { 1837 - do { 1838 - unlock_page(pages[--do_pages]); 1839 - put_page(pages[do_pages]); 1840 - } while (do_pages); 1841 - break; 1842 - } 1843 - } 1844 - u = (pos >> PAGE_SHIFT) - pages[0]->index; 1845 - copied = ntfs_copy_from_user_iter(pages + u, do_pages - u, ofs, 1846 - i, bytes); 1847 - ntfs_flush_dcache_pages(pages + u, do_pages - u); 1848 - status = 0; 1849 - if (likely(copied == bytes)) { 1850 - status = ntfs_commit_pages_after_write(pages, do_pages, 1851 - pos, bytes); 1852 - } 1853 - do { 1854 - unlock_page(pages[--do_pages]); 1855 - put_page(pages[do_pages]); 1856 - } while (do_pages); 1857 - if (unlikely(status < 0)) { 1858 - iov_iter_revert(i, copied); 1859 - break; 1860 - } 1861 - cond_resched(); 1862 - if (unlikely(copied < bytes)) { 1863 - iov_iter_revert(i, copied); 1864 - if (copied) 1865 - bytes = copied; 1866 - else if (bytes > PAGE_SIZE - ofs) 1867 - bytes = PAGE_SIZE - ofs; 1868 - goto again; 1869 - } 1870 - pos += copied; 1871 - written += copied; 1872 - balance_dirty_pages_ratelimited(mapping); 1873 - if (fatal_signal_pending(current)) { 1874 - status = -EINTR; 1875 - break; 1876 - } 1877 - } while (iov_iter_count(i)); 1878 - if (cached_page) 1879 - put_page(cached_page); 1880 - ntfs_debug("Done. Returning %s (written 0x%lx, status %li).", 1881 - written ? "written" : "status", (unsigned long)written, 1882 - (long)status); 1883 - return written ? written : status; 1884 - } 1885 - 1886 - /** 1887 - * ntfs_file_write_iter - simple wrapper for ntfs_file_write_iter_nolock() 1888 - * @iocb: IO state structure 1889 - * @from: iov_iter with data to write 1890 - * 1891 - * Basically the same as generic_file_write_iter() except that it ends up 1892 - * up calling ntfs_perform_write() instead of generic_perform_write() and that 1893 - * O_DIRECT is not implemented. 1894 - */ 1895 - static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 1896 - { 1897 - struct file *file = iocb->ki_filp; 1898 - struct inode *vi = file_inode(file); 1899 - ssize_t written = 0; 1900 - ssize_t err; 1901 - 1902 - inode_lock(vi); 1903 - /* We can write back this queue in page reclaim. */ 1904 - err = ntfs_prepare_file_for_write(iocb, from); 1905 - if (iov_iter_count(from) && !err) 1906 - written = ntfs_perform_write(file, from, iocb->ki_pos); 1907 - inode_unlock(vi); 1908 - iocb->ki_pos += written; 1909 - if (likely(written > 0)) 1910 - written = generic_write_sync(iocb, written); 1911 - return written ? written : err; 1912 - } 1913 - 1914 - /** 1915 140 * ntfs_file_fsync - sync a file to disk 1916 141 * @filp: file to be synced 142 + * @start: start offset to be synced 143 + * @end: end offset to be synced 1917 144 * @datasync: if non-zero only flush user data and not metadata 1918 145 * 1919 146 * Data integrity sync of a file to disk. Used for fsync, fdatasync, and msync ··· 152 1931 * 153 1932 * Also, if @datasync is true, we do not wait on the inode to be written out 154 1933 * but we always wait on the page cache pages to be written out. 155 - * 156 - * Locking: Caller must hold i_mutex on the inode. 157 - * 158 - * TODO: We should probably also write all attribute/index inodes associated 159 - * with this inode but since we have no simple way of getting to them we ignore 160 - * this problem for now. 161 1934 */ 162 1935 static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end, 163 1936 int datasync) 164 1937 { 165 1938 struct inode *vi = filp->f_mapping->host; 1939 + struct ntfs_inode *ni = NTFS_I(vi); 1940 + struct ntfs_volume *vol = ni->vol; 166 1941 int err, ret = 0; 1942 + struct inode *parent_vi, *ia_vi; 1943 + struct ntfs_attr_search_ctx *ctx; 167 1944 168 1945 ntfs_debug("Entering for inode 0x%lx.", vi->i_ino); 1946 + 1947 + if (NVolShutdown(vol)) 1948 + return -EIO; 169 1949 170 1950 err = file_write_and_wait_range(filp, start, end); 171 1951 if (err) 172 1952 return err; 173 - inode_lock(vi); 174 1953 175 - BUG_ON(S_ISDIR(vi->i_mode)); 176 1954 if (!datasync || !NInoNonResident(NTFS_I(vi))) 177 1955 ret = __ntfs_write_inode(vi, 1); 178 1956 write_inode_now(vi, !datasync); 1957 + 1958 + ctx = ntfs_attr_get_search_ctx(ni, NULL); 1959 + if (!ctx) 1960 + return -ENOMEM; 1961 + 1962 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_CHILD); 1963 + while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) { 1964 + if (ctx->attr->type == AT_FILE_NAME) { 1965 + struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr + 1966 + le16_to_cpu(ctx->attr->data.resident.value_offset)); 1967 + 1968 + parent_vi = ntfs_iget(vi->i_sb, MREF_LE(fn->parent_directory)); 1969 + if (IS_ERR(parent_vi)) 1970 + continue; 1971 + mutex_lock_nested(&NTFS_I(parent_vi)->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 1972 + ia_vi = ntfs_index_iget(parent_vi, I30, 4); 1973 + mutex_unlock(&NTFS_I(parent_vi)->mrec_lock); 1974 + if (IS_ERR(ia_vi)) { 1975 + iput(parent_vi); 1976 + continue; 1977 + } 1978 + write_inode_now(ia_vi, 1); 1979 + iput(ia_vi); 1980 + write_inode_now(parent_vi, 1); 1981 + iput(parent_vi); 1982 + } else if (ctx->attr->non_resident) { 1983 + struct inode *attr_vi; 1984 + __le16 *name; 1985 + 1986 + name = (__le16 *)((u8 *)ctx->attr + le16_to_cpu(ctx->attr->name_offset)); 1987 + if (ctx->attr->type == AT_DATA && ctx->attr->name_length == 0) 1988 + continue; 1989 + 1990 + attr_vi = ntfs_attr_iget(vi, ctx->attr->type, 1991 + name, ctx->attr->name_length); 1992 + if (IS_ERR(attr_vi)) 1993 + continue; 1994 + spin_lock(&attr_vi->i_lock); 1995 + if (inode_state_read_once(attr_vi) & I_DIRTY_PAGES) { 1996 + spin_unlock(&attr_vi->i_lock); 1997 + filemap_write_and_wait(attr_vi->i_mapping); 1998 + } else 1999 + spin_unlock(&attr_vi->i_lock); 2000 + iput(attr_vi); 2001 + } 2002 + } 2003 + mutex_unlock(&ni->mrec_lock); 2004 + ntfs_attr_put_search_ctx(ctx); 2005 + 2006 + write_inode_now(vol->mftbmp_ino, 1); 2007 + down_write(&vol->lcnbmp_lock); 2008 + write_inode_now(vol->lcnbmp_ino, 1); 2009 + up_write(&vol->lcnbmp_lock); 2010 + write_inode_now(vol->mft_ino, 1); 2011 + 179 2012 /* 180 2013 * NOTE: If we were to use mapping->private_list (see ext2 and 181 2014 * fs/buffer.c) for dirty blocks then we could optimize the below to be ··· 241 1966 if (likely(!ret)) 242 1967 ntfs_debug("Done."); 243 1968 else 244 - ntfs_warning(vi->i_sb, "Failed to f%ssync inode 0x%lx. Error " 245 - "%u.", datasync ? "data" : "", vi->i_ino, -ret); 246 - inode_unlock(vi); 1969 + ntfs_warning(vi->i_sb, 1970 + "Failed to f%ssync inode 0x%lx. Error %u.", 1971 + datasync ? "data" : "", vi->i_ino, -ret); 1972 + if (!ret) 1973 + blkdev_issue_flush(vi->i_sb->s_bdev); 247 1974 return ret; 248 1975 } 249 1976 250 - #endif /* NTFS_RW */ 1977 + static int ntfs_setattr_size(struct inode *vi, struct iattr *attr) 1978 + { 1979 + struct ntfs_inode *ni = NTFS_I(vi); 1980 + int err; 1981 + loff_t old_size = vi->i_size; 1982 + 1983 + if (NInoCompressed(ni) || NInoEncrypted(ni)) { 1984 + ntfs_warning(vi->i_sb, 1985 + "Changes in inode size are not supported yet for %s files, ignoring.", 1986 + NInoCompressed(ni) ? "compressed" : "encrypted"); 1987 + return -EOPNOTSUPP; 1988 + } 1989 + 1990 + err = inode_newsize_ok(vi, attr->ia_size); 1991 + if (err) 1992 + return err; 1993 + 1994 + inode_dio_wait(vi); 1995 + /* Serialize against page faults */ 1996 + if (NInoNonResident(NTFS_I(vi)) && attr->ia_size < old_size) { 1997 + err = iomap_truncate_page(vi, attr->ia_size, NULL, 1998 + &ntfs_read_iomap_ops, 1999 + &ntfs_iomap_folio_ops, NULL); 2000 + if (err) 2001 + return err; 2002 + } 2003 + 2004 + truncate_setsize(vi, attr->ia_size); 2005 + err = ntfs_truncate_vfs(vi, attr->ia_size, old_size); 2006 + if (err) { 2007 + i_size_write(vi, old_size); 2008 + return err; 2009 + } 2010 + 2011 + if (NInoNonResident(ni) && attr->ia_size > old_size && 2012 + old_size % PAGE_SIZE != 0) { 2013 + loff_t len = min_t(loff_t, 2014 + round_up(old_size, PAGE_SIZE) - old_size, 2015 + attr->ia_size - old_size); 2016 + err = iomap_zero_range(vi, old_size, len, 2017 + NULL, &ntfs_seek_iomap_ops, 2018 + &ntfs_iomap_folio_ops, NULL); 2019 + } 2020 + 2021 + return err; 2022 + } 2023 + 2024 + /* 2025 + * ntfs_setattr 2026 + * 2027 + * Called from notify_change() when an attribute is being changed. 2028 + * 2029 + * NOTE: Changes in inode size are not supported yet for compressed or 2030 + * encrypted files. 2031 + */ 2032 + int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 2033 + struct iattr *attr) 2034 + { 2035 + struct inode *vi = d_inode(dentry); 2036 + int err; 2037 + unsigned int ia_valid = attr->ia_valid; 2038 + struct ntfs_inode *ni = NTFS_I(vi); 2039 + struct ntfs_volume *vol = ni->vol; 2040 + 2041 + if (NVolShutdown(vol)) 2042 + return -EIO; 2043 + 2044 + err = setattr_prepare(idmap, dentry, attr); 2045 + if (err) 2046 + goto out; 2047 + 2048 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 2049 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 2050 + 2051 + if (ia_valid & ATTR_SIZE) { 2052 + err = ntfs_setattr_size(vi, attr); 2053 + if (err) 2054 + goto out; 2055 + 2056 + ia_valid |= ATTR_MTIME | ATTR_CTIME; 2057 + } 2058 + 2059 + setattr_copy(idmap, vi, attr); 2060 + 2061 + if (vol->sb->s_flags & SB_POSIXACL && !S_ISLNK(vi->i_mode)) { 2062 + err = posix_acl_chmod(idmap, dentry, vi->i_mode); 2063 + if (err) 2064 + goto out; 2065 + } 2066 + 2067 + if (0222 & vi->i_mode) 2068 + ni->flags &= ~FILE_ATTR_READONLY; 2069 + else 2070 + ni->flags |= FILE_ATTR_READONLY; 2071 + 2072 + if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) { 2073 + unsigned int flags = 0; 2074 + 2075 + if (ia_valid & ATTR_UID) 2076 + flags |= NTFS_EA_UID; 2077 + if (ia_valid & ATTR_GID) 2078 + flags |= NTFS_EA_GID; 2079 + if (ia_valid & ATTR_MODE) 2080 + flags |= NTFS_EA_MODE; 2081 + 2082 + if (S_ISDIR(vi->i_mode)) 2083 + vi->i_mode &= ~vol->dmask; 2084 + else 2085 + vi->i_mode &= ~vol->fmask; 2086 + 2087 + mutex_lock(&ni->mrec_lock); 2088 + ntfs_ea_set_wsl_inode(vi, 0, NULL, flags); 2089 + mutex_unlock(&ni->mrec_lock); 2090 + } 2091 + 2092 + mark_inode_dirty(vi); 2093 + out: 2094 + return err; 2095 + } 2096 + 2097 + int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path, 2098 + struct kstat *stat, unsigned int request_mask, 2099 + unsigned int query_flags) 2100 + { 2101 + struct inode *inode = d_backing_inode(path->dentry); 2102 + struct ntfs_inode *ni = NTFS_I(inode); 2103 + 2104 + generic_fillattr(idmap, request_mask, inode, stat); 2105 + 2106 + stat->blksize = NTFS_SB(inode->i_sb)->cluster_size; 2107 + stat->blocks = (((u64)NTFS_I(inode)->i_dealloc_clusters << 2108 + NTFS_SB(inode->i_sb)->cluster_size_bits) >> 9) + inode->i_blocks; 2109 + stat->result_mask |= STATX_BTIME; 2110 + stat->btime = NTFS_I(inode)->i_crtime; 2111 + 2112 + if (NInoCompressed(ni)) 2113 + stat->attributes |= STATX_ATTR_COMPRESSED; 2114 + 2115 + if (NInoEncrypted(ni)) 2116 + stat->attributes |= STATX_ATTR_ENCRYPTED; 2117 + 2118 + if (inode->i_flags & S_IMMUTABLE) 2119 + stat->attributes |= STATX_ATTR_IMMUTABLE; 2120 + 2121 + if (inode->i_flags & S_APPEND) 2122 + stat->attributes |= STATX_ATTR_APPEND; 2123 + 2124 + stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED | 2125 + STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND; 2126 + 2127 + /* 2128 + * If it's a compressed or encrypted file, NTFS currently 2129 + * does not support DIO. For normal files, we report the bdev 2130 + * logical block size. 2131 + */ 2132 + if (request_mask & STATX_DIOALIGN && S_ISREG(inode->i_mode)) { 2133 + unsigned int align = 2134 + bdev_logical_block_size(inode->i_sb->s_bdev); 2135 + 2136 + stat->result_mask |= STATX_DIOALIGN; 2137 + if (!NInoCompressed(ni) && !NInoEncrypted(ni)) { 2138 + stat->dio_mem_align = align; 2139 + stat->dio_offset_align = align; 2140 + } 2141 + } 2142 + 2143 + return 0; 2144 + } 2145 + 2146 + static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence) 2147 + { 2148 + struct inode *inode = file->f_mapping->host; 2149 + 2150 + switch (whence) { 2151 + case SEEK_HOLE: 2152 + inode_lock_shared(inode); 2153 + offset = iomap_seek_hole(inode, offset, &ntfs_seek_iomap_ops); 2154 + inode_unlock_shared(inode); 2155 + break; 2156 + case SEEK_DATA: 2157 + inode_lock_shared(inode); 2158 + offset = iomap_seek_data(inode, offset, &ntfs_seek_iomap_ops); 2159 + inode_unlock_shared(inode); 2160 + break; 2161 + default: 2162 + return generic_file_llseek_size(file, offset, whence, 2163 + inode->i_sb->s_maxbytes, 2164 + i_size_read(inode)); 2165 + } 2166 + if (offset < 0) 2167 + return offset; 2168 + return vfs_setpos(file, offset, inode->i_sb->s_maxbytes); 2169 + } 2170 + 2171 + static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 2172 + { 2173 + struct inode *vi = file_inode(iocb->ki_filp); 2174 + struct super_block *sb = vi->i_sb; 2175 + ssize_t ret; 2176 + 2177 + if (NVolShutdown(NTFS_SB(sb))) 2178 + return -EIO; 2179 + 2180 + if (NInoCompressed(NTFS_I(vi)) && iocb->ki_flags & IOCB_DIRECT) 2181 + return -EOPNOTSUPP; 2182 + 2183 + inode_lock_shared(vi); 2184 + 2185 + if (iocb->ki_flags & IOCB_DIRECT) { 2186 + size_t count = iov_iter_count(to); 2187 + 2188 + if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) { 2189 + ret = -EINVAL; 2190 + goto inode_unlock; 2191 + } 2192 + 2193 + file_accessed(iocb->ki_filp); 2194 + ret = iomap_dio_rw(iocb, to, &ntfs_read_iomap_ops, NULL, 0, 2195 + NULL, 0); 2196 + } else { 2197 + ret = generic_file_read_iter(iocb, to); 2198 + } 2199 + 2200 + inode_unlock: 2201 + inode_unlock_shared(vi); 2202 + 2203 + return ret; 2204 + } 2205 + 2206 + static int ntfs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size, 2207 + int error, unsigned int flags) 2208 + { 2209 + struct inode *inode = file_inode(iocb->ki_filp); 2210 + 2211 + if (error) 2212 + return error; 2213 + 2214 + if (size) { 2215 + if (i_size_read(inode) < iocb->ki_pos + size) { 2216 + i_size_write(inode, iocb->ki_pos + size); 2217 + mark_inode_dirty(inode); 2218 + } 2219 + } 2220 + 2221 + return 0; 2222 + } 2223 + 2224 + static const struct iomap_dio_ops ntfs_write_dio_ops = { 2225 + .end_io = ntfs_file_write_dio_end_io, 2226 + }; 2227 + 2228 + static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from) 2229 + { 2230 + ssize_t ret; 2231 + 2232 + ret = iomap_dio_rw(iocb, from, &ntfs_dio_iomap_ops, 2233 + &ntfs_write_dio_ops, 0, NULL, 0); 2234 + if (ret == -ENOTBLK) 2235 + ret = 0; 2236 + else if (ret < 0) 2237 + goto out; 2238 + 2239 + if (iov_iter_count(from)) { 2240 + loff_t offset, end; 2241 + ssize_t written; 2242 + int ret2; 2243 + 2244 + offset = iocb->ki_pos; 2245 + iocb->ki_flags &= ~IOCB_DIRECT; 2246 + written = iomap_file_buffered_write(iocb, from, 2247 + &ntfs_write_iomap_ops, &ntfs_iomap_folio_ops, 2248 + NULL); 2249 + if (written < 0) { 2250 + ret = written; 2251 + goto out; 2252 + } 2253 + 2254 + ret += written; 2255 + end = iocb->ki_pos + written - 1; 2256 + ret2 = filemap_write_and_wait_range(iocb->ki_filp->f_mapping, 2257 + offset, end); 2258 + if (ret2) { 2259 + ret = -EIO; 2260 + goto out; 2261 + } 2262 + if (!ret2) 2263 + invalidate_mapping_pages(iocb->ki_filp->f_mapping, 2264 + offset >> PAGE_SHIFT, 2265 + end >> PAGE_SHIFT); 2266 + } 2267 + 2268 + out: 2269 + return ret; 2270 + } 2271 + 2272 + static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 2273 + { 2274 + struct file *file = iocb->ki_filp; 2275 + struct inode *vi = file->f_mapping->host; 2276 + struct ntfs_inode *ni = NTFS_I(vi); 2277 + struct ntfs_volume *vol = ni->vol; 2278 + ssize_t ret; 2279 + ssize_t count; 2280 + loff_t pos; 2281 + int err; 2282 + loff_t old_data_size, old_init_size; 2283 + 2284 + if (NVolShutdown(vol)) 2285 + return -EIO; 2286 + 2287 + if (NInoEncrypted(ni)) { 2288 + ntfs_error(vi->i_sb, "Writing for %s files is not supported yet", 2289 + NInoCompressed(ni) ? "Compressed" : "Encrypted"); 2290 + return -EOPNOTSUPP; 2291 + } 2292 + 2293 + if (NInoCompressed(ni) && iocb->ki_flags & IOCB_DIRECT) 2294 + return -EOPNOTSUPP; 2295 + 2296 + if (iocb->ki_flags & IOCB_NOWAIT) { 2297 + if (!inode_trylock(vi)) 2298 + return -EAGAIN; 2299 + } else 2300 + inode_lock(vi); 2301 + 2302 + ret = generic_write_checks(iocb, from); 2303 + if (ret <= 0) 2304 + goto out_lock; 2305 + 2306 + err = file_modified(iocb->ki_filp); 2307 + if (err) { 2308 + ret = err; 2309 + goto out_lock; 2310 + } 2311 + 2312 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) 2313 + ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 2314 + 2315 + pos = iocb->ki_pos; 2316 + count = ret; 2317 + 2318 + old_data_size = ni->data_size; 2319 + old_init_size = ni->initialized_size; 2320 + 2321 + if (NInoNonResident(ni) && NInoCompressed(ni)) { 2322 + ret = ntfs_compress_write(ni, pos, count, from); 2323 + if (ret > 0) 2324 + iocb->ki_pos += ret; 2325 + goto out; 2326 + } 2327 + 2328 + if (NInoNonResident(ni) && iocb->ki_flags & IOCB_DIRECT) 2329 + ret = ntfs_dio_write_iter(iocb, from); 2330 + else 2331 + ret = iomap_file_buffered_write(iocb, from, &ntfs_write_iomap_ops, 2332 + &ntfs_iomap_folio_ops, NULL); 2333 + out: 2334 + if (ret < 0 && ret != -EIOCBQUEUED) { 2335 + if (ni->initialized_size != old_init_size) { 2336 + mutex_lock(&ni->mrec_lock); 2337 + ntfs_attr_set_initialized_size(ni, old_init_size); 2338 + mutex_unlock(&ni->mrec_lock); 2339 + } 2340 + if (ni->data_size != old_data_size) { 2341 + truncate_setsize(vi, old_data_size); 2342 + ntfs_attr_truncate(ni, old_data_size); 2343 + } 2344 + } 2345 + out_lock: 2346 + inode_unlock(vi); 2347 + if (ret > 0) 2348 + ret = generic_write_sync(iocb, ret); 2349 + return ret; 2350 + } 2351 + 2352 + static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf) 2353 + { 2354 + struct inode *inode = file_inode(vmf->vma->vm_file); 2355 + vm_fault_t ret; 2356 + 2357 + sb_start_pagefault(inode->i_sb); 2358 + file_update_time(vmf->vma->vm_file); 2359 + 2360 + ret = iomap_page_mkwrite(vmf, &ntfs_page_mkwrite_iomap_ops, NULL); 2361 + sb_end_pagefault(inode->i_sb); 2362 + return ret; 2363 + } 2364 + 2365 + static const struct vm_operations_struct ntfs_file_vm_ops = { 2366 + .fault = filemap_fault, 2367 + .map_pages = filemap_map_pages, 2368 + .page_mkwrite = ntfs_filemap_page_mkwrite, 2369 + }; 2370 + 2371 + static int ntfs_file_mmap_prepare(struct vm_area_desc *desc) 2372 + { 2373 + struct file *file = desc->file; 2374 + struct inode *inode = file_inode(file); 2375 + 2376 + if (NVolShutdown(NTFS_SB(file->f_mapping->host->i_sb))) 2377 + return -EIO; 2378 + 2379 + if (NInoCompressed(NTFS_I(inode))) 2380 + return -EOPNOTSUPP; 2381 + 2382 + if (vma_desc_test_flags(desc, VMA_WRITE_BIT)) { 2383 + struct inode *inode = file_inode(file); 2384 + loff_t from, to; 2385 + int err; 2386 + 2387 + from = ((loff_t)desc->pgoff << PAGE_SHIFT); 2388 + to = min_t(loff_t, i_size_read(inode), 2389 + from + desc->end - desc->start); 2390 + 2391 + if (NTFS_I(inode)->initialized_size < to) { 2392 + err = ntfs_extend_initialized_size(inode, to, to, false); 2393 + if (err) 2394 + return err; 2395 + } 2396 + } 2397 + 2398 + 2399 + file_accessed(file); 2400 + desc->vm_ops = &ntfs_file_vm_ops; 2401 + return 0; 2402 + } 2403 + 2404 + static int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 2405 + u64 start, u64 len) 2406 + { 2407 + return iomap_fiemap(inode, fieinfo, start, len, &ntfs_read_iomap_ops); 2408 + } 2409 + 2410 + static const char *ntfs_get_link(struct dentry *dentry, struct inode *inode, 2411 + struct delayed_call *done) 2412 + { 2413 + if (!NTFS_I(inode)->target) 2414 + return ERR_PTR(-EINVAL); 2415 + 2416 + return NTFS_I(inode)->target; 2417 + } 2418 + 2419 + static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos, 2420 + struct pipe_inode_info *pipe, size_t len, unsigned int flags) 2421 + { 2422 + if (NVolShutdown(NTFS_SB(in->f_mapping->host->i_sb))) 2423 + return -EIO; 2424 + 2425 + return filemap_splice_read(in, ppos, pipe, len, flags); 2426 + } 2427 + 2428 + static int ntfs_ioctl_shutdown(struct super_block *sb, unsigned long arg) 2429 + { 2430 + u32 flags; 2431 + 2432 + if (!capable(CAP_SYS_ADMIN)) 2433 + return -EPERM; 2434 + 2435 + if (get_user(flags, (__u32 __user *)arg)) 2436 + return -EFAULT; 2437 + 2438 + return ntfs_force_shutdown(sb, flags); 2439 + } 2440 + 2441 + static int ntfs_ioctl_get_volume_label(struct file *filp, unsigned long arg) 2442 + { 2443 + struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb); 2444 + char __user *buf = (char __user *)arg; 2445 + 2446 + if (!vol->volume_label) { 2447 + if (copy_to_user(buf, "", 1)) 2448 + return -EFAULT; 2449 + } else if (copy_to_user(buf, vol->volume_label, 2450 + MIN(FSLABEL_MAX, strlen(vol->volume_label) + 1))) 2451 + return -EFAULT; 2452 + return 0; 2453 + } 2454 + 2455 + static int ntfs_ioctl_set_volume_label(struct file *filp, unsigned long arg) 2456 + { 2457 + struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb); 2458 + char *label; 2459 + int ret; 2460 + 2461 + if (!capable(CAP_SYS_ADMIN)) 2462 + return -EPERM; 2463 + 2464 + label = strndup_user((const char __user *)arg, FSLABEL_MAX); 2465 + if (IS_ERR(label)) 2466 + return PTR_ERR(label); 2467 + 2468 + ret = mnt_want_write_file(filp); 2469 + if (ret) 2470 + goto out; 2471 + 2472 + ret = ntfs_write_volume_label(vol, label); 2473 + mnt_drop_write_file(filp); 2474 + out: 2475 + kfree(label); 2476 + return ret; 2477 + } 2478 + 2479 + static int ntfs_ioctl_fitrim(struct ntfs_volume *vol, unsigned long arg) 2480 + { 2481 + struct fstrim_range __user *user_range; 2482 + struct fstrim_range range; 2483 + struct block_device *dev; 2484 + int err; 2485 + 2486 + if (!capable(CAP_SYS_ADMIN)) 2487 + return -EPERM; 2488 + 2489 + dev = vol->sb->s_bdev; 2490 + if (!bdev_max_discard_sectors(dev)) 2491 + return -EOPNOTSUPP; 2492 + 2493 + user_range = (struct fstrim_range __user *)arg; 2494 + if (copy_from_user(&range, user_range, sizeof(range))) 2495 + return -EFAULT; 2496 + 2497 + if (range.len == 0) 2498 + return -EINVAL; 2499 + 2500 + if (range.len < vol->cluster_size) 2501 + return -EINVAL; 2502 + 2503 + range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev)); 2504 + 2505 + err = ntfs_trim_fs(vol, &range); 2506 + if (err < 0) 2507 + return err; 2508 + 2509 + if (copy_to_user(user_range, &range, sizeof(range))) 2510 + return -EFAULT; 2511 + 2512 + return 0; 2513 + } 2514 + 2515 + long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 2516 + { 2517 + switch (cmd) { 2518 + case FS_IOC_SHUTDOWN: 2519 + return ntfs_ioctl_shutdown(file_inode(filp)->i_sb, arg); 2520 + case FS_IOC_GETFSLABEL: 2521 + return ntfs_ioctl_get_volume_label(filp, arg); 2522 + case FS_IOC_SETFSLABEL: 2523 + return ntfs_ioctl_set_volume_label(filp, arg); 2524 + case FITRIM: 2525 + return ntfs_ioctl_fitrim(NTFS_SB(file_inode(filp)->i_sb), arg); 2526 + default: 2527 + return -ENOTTY; 2528 + } 2529 + } 2530 + 2531 + #ifdef CONFIG_COMPAT 2532 + long ntfs_compat_ioctl(struct file *filp, unsigned int cmd, 2533 + unsigned long arg) 2534 + { 2535 + return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); 2536 + } 2537 + #endif 2538 + 2539 + static int ntfs_allocate_range(struct ntfs_inode *ni, int mode, loff_t offset, 2540 + loff_t len) 2541 + { 2542 + struct inode *vi = VFS_I(ni); 2543 + struct ntfs_volume *vol = ni->vol; 2544 + s64 need_space; 2545 + loff_t old_size, new_size; 2546 + s64 start_vcn, end_vcn; 2547 + int err; 2548 + 2549 + old_size = i_size_read(vi); 2550 + new_size = max_t(loff_t, old_size, offset + len); 2551 + start_vcn = ntfs_bytes_to_cluster(vol, offset); 2552 + end_vcn = ntfs_bytes_to_cluster(vol, offset + len - 1) + 1; 2553 + 2554 + err = inode_newsize_ok(vi, new_size); 2555 + if (err) 2556 + goto out; 2557 + 2558 + need_space = ntfs_bytes_to_cluster(vol, ni->allocated_size); 2559 + if (need_space > start_vcn) 2560 + need_space = end_vcn - need_space; 2561 + else 2562 + need_space = end_vcn - start_vcn; 2563 + if (need_space > 0 && 2564 + need_space > (atomic64_read(&vol->free_clusters) - 2565 + atomic64_read(&vol->dirty_clusters))) { 2566 + err = -ENOSPC; 2567 + goto out; 2568 + } 2569 + 2570 + err = ntfs_attr_fallocate(ni, offset, len, 2571 + mode & FALLOC_FL_KEEP_SIZE ? true : false); 2572 + 2573 + if (!(mode & FALLOC_FL_KEEP_SIZE) && new_size != old_size) 2574 + i_size_write(vi, ni->data_size); 2575 + out: 2576 + return err; 2577 + } 2578 + 2579 + static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset, 2580 + loff_t len) 2581 + { 2582 + struct ntfs_volume *vol = ni->vol; 2583 + struct inode *vi = VFS_I(ni); 2584 + loff_t end_offset; 2585 + s64 start_vcn, end_vcn; 2586 + int err = 0; 2587 + 2588 + loff_t offset_down = round_down(offset, max_t(unsigned int, 2589 + vol->cluster_size, PAGE_SIZE)); 2590 + 2591 + if (NVolDisableSparse(vol)) { 2592 + err = -EOPNOTSUPP; 2593 + goto out; 2594 + } 2595 + 2596 + if (offset >= ni->data_size) 2597 + goto out; 2598 + 2599 + if (offset + len > ni->data_size) 2600 + end_offset = ni->data_size; 2601 + else 2602 + end_offset = offset + len; 2603 + 2604 + err = filemap_write_and_wait_range(vi->i_mapping, offset_down, LLONG_MAX); 2605 + if (err) 2606 + goto out; 2607 + truncate_pagecache(vi, offset_down); 2608 + 2609 + start_vcn = ntfs_bytes_to_cluster(vol, offset); 2610 + end_vcn = ntfs_bytes_to_cluster(vol, end_offset - 1) + 1; 2611 + 2612 + if (offset & vol->cluster_size_mask) { 2613 + loff_t to; 2614 + 2615 + to = min_t(loff_t, ntfs_cluster_to_bytes(vol, start_vcn + 1), 2616 + end_offset); 2617 + err = iomap_zero_range(vi, offset, to - offset, NULL, 2618 + &ntfs_seek_iomap_ops, 2619 + &ntfs_iomap_folio_ops, NULL); 2620 + if (err < 0 || (end_vcn - start_vcn) == 1) 2621 + goto out; 2622 + start_vcn++; 2623 + } 2624 + 2625 + if (end_offset & vol->cluster_size_mask) { 2626 + loff_t from; 2627 + 2628 + from = ntfs_cluster_to_bytes(vol, end_vcn - 1); 2629 + err = iomap_zero_range(vi, from, end_offset - from, NULL, 2630 + &ntfs_seek_iomap_ops, 2631 + &ntfs_iomap_folio_ops, NULL); 2632 + if (err < 0 || (end_vcn - start_vcn) == 1) 2633 + goto out; 2634 + end_vcn--; 2635 + } 2636 + 2637 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 2638 + err = ntfs_non_resident_attr_punch_hole(ni, start_vcn, 2639 + end_vcn - start_vcn); 2640 + mutex_unlock(&ni->mrec_lock); 2641 + out: 2642 + return err; 2643 + } 2644 + 2645 + static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len) 2646 + { 2647 + struct ntfs_volume *vol = ni->vol; 2648 + struct inode *vi = VFS_I(ni); 2649 + loff_t old_size, new_size; 2650 + s64 start_vcn, end_vcn; 2651 + int err; 2652 + 2653 + loff_t offset_down = round_down(offset, 2654 + max_t(unsigned long, vol->cluster_size, PAGE_SIZE)); 2655 + 2656 + if ((offset & vol->cluster_size_mask) || 2657 + (len & vol->cluster_size_mask) || 2658 + offset >= ni->allocated_size) { 2659 + err = -EINVAL; 2660 + goto out; 2661 + } 2662 + 2663 + old_size = i_size_read(vi); 2664 + start_vcn = ntfs_bytes_to_cluster(vol, offset); 2665 + end_vcn = ntfs_bytes_to_cluster(vol, offset + len - 1) + 1; 2666 + 2667 + if (ntfs_cluster_to_bytes(vol, end_vcn) > ni->allocated_size) 2668 + end_vcn = (round_up(ni->allocated_size - 1, 2669 + vol->cluster_size) >> vol->cluster_size_bits) + 1; 2670 + new_size = old_size - ntfs_cluster_to_bytes(vol, end_vcn - start_vcn); 2671 + if (new_size < 0) 2672 + new_size = 0; 2673 + err = filemap_write_and_wait_range(vi->i_mapping, 2674 + offset_down, LLONG_MAX); 2675 + if (err) 2676 + goto out; 2677 + 2678 + truncate_pagecache(vi, offset_down); 2679 + 2680 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 2681 + err = ntfs_non_resident_attr_collapse_range(ni, start_vcn, 2682 + end_vcn - start_vcn); 2683 + mutex_unlock(&ni->mrec_lock); 2684 + 2685 + if (new_size != old_size) 2686 + i_size_write(vi, ni->data_size); 2687 + out: 2688 + return err; 2689 + } 2690 + 2691 + static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len) 2692 + { 2693 + struct ntfs_volume *vol = ni->vol; 2694 + struct inode *vi = VFS_I(ni); 2695 + loff_t offset_down = round_down(offset, 2696 + max_t(unsigned long, vol->cluster_size, PAGE_SIZE)); 2697 + loff_t alloc_size, end_offset = offset + len; 2698 + loff_t old_size, new_size; 2699 + s64 start_vcn, end_vcn; 2700 + int err; 2701 + 2702 + if (NVolDisableSparse(vol)) { 2703 + err = -EOPNOTSUPP; 2704 + goto out; 2705 + } 2706 + 2707 + if ((offset & vol->cluster_size_mask) || 2708 + (len & vol->cluster_size_mask) || 2709 + offset >= ni->allocated_size) { 2710 + err = -EINVAL; 2711 + goto out; 2712 + } 2713 + 2714 + old_size = i_size_read(vi); 2715 + start_vcn = ntfs_bytes_to_cluster(vol, offset); 2716 + end_vcn = ntfs_bytes_to_cluster(vol, end_offset - 1) + 1; 2717 + 2718 + new_size = old_size + ntfs_cluster_to_bytes(vol, end_vcn - start_vcn); 2719 + alloc_size = ni->allocated_size + 2720 + ntfs_cluster_to_bytes(vol, end_vcn - start_vcn); 2721 + if (alloc_size < 0) { 2722 + err = -EFBIG; 2723 + goto out; 2724 + } 2725 + err = inode_newsize_ok(vi, alloc_size); 2726 + if (err) 2727 + goto out; 2728 + 2729 + err = filemap_write_and_wait_range(vi->i_mapping, 2730 + offset_down, LLONG_MAX); 2731 + if (err) 2732 + goto out; 2733 + 2734 + truncate_pagecache(vi, offset_down); 2735 + 2736 + mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); 2737 + err = ntfs_non_resident_attr_insert_range(ni, start_vcn, 2738 + end_vcn - start_vcn); 2739 + mutex_unlock(&ni->mrec_lock); 2740 + 2741 + if (new_size != old_size) 2742 + i_size_write(vi, ni->data_size); 2743 + out: 2744 + return err; 2745 + } 2746 + 2747 + #define NTFS_FALLOC_FL_SUPPORTED \ 2748 + (FALLOC_FL_ALLOCATE_RANGE | FALLOC_FL_KEEP_SIZE | \ 2749 + FALLOC_FL_INSERT_RANGE | FALLOC_FL_PUNCH_HOLE | \ 2750 + FALLOC_FL_COLLAPSE_RANGE) 2751 + 2752 + static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len) 2753 + { 2754 + struct inode *vi = file_inode(file); 2755 + struct ntfs_inode *ni = NTFS_I(vi); 2756 + struct ntfs_volume *vol = ni->vol; 2757 + int err = 0; 2758 + loff_t old_size; 2759 + bool map_locked = false; 2760 + 2761 + if (mode & ~(NTFS_FALLOC_FL_SUPPORTED)) 2762 + return -EOPNOTSUPP; 2763 + 2764 + if (!NVolFreeClusterKnown(vol)) 2765 + wait_event(vol->free_waitq, NVolFreeClusterKnown(vol)); 2766 + 2767 + if ((ni->vol->mft_zone_end - ni->vol->mft_zone_start) == 0) 2768 + return -ENOSPC; 2769 + 2770 + if (NInoNonResident(ni) && !NInoFullyMapped(ni)) { 2771 + down_write(&ni->runlist.lock); 2772 + err = ntfs_attr_map_whole_runlist(ni); 2773 + up_write(&ni->runlist.lock); 2774 + if (err) 2775 + return err; 2776 + } 2777 + 2778 + if (!(vol->vol_flags & VOLUME_IS_DIRTY)) { 2779 + err = ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY); 2780 + if (err) 2781 + return err; 2782 + } 2783 + 2784 + old_size = i_size_read(vi); 2785 + 2786 + inode_lock(vi); 2787 + if (NInoCompressed(ni) || NInoEncrypted(ni)) { 2788 + err = -EOPNOTSUPP; 2789 + goto out; 2790 + } 2791 + 2792 + inode_dio_wait(vi); 2793 + if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE | 2794 + FALLOC_FL_INSERT_RANGE)) { 2795 + filemap_invalidate_lock(vi->i_mapping); 2796 + map_locked = true; 2797 + } 2798 + 2799 + switch (mode & FALLOC_FL_MODE_MASK) { 2800 + case FALLOC_FL_ALLOCATE_RANGE: 2801 + case FALLOC_FL_KEEP_SIZE: 2802 + err = ntfs_allocate_range(ni, mode, offset, len); 2803 + break; 2804 + case FALLOC_FL_PUNCH_HOLE: 2805 + err = ntfs_punch_hole(ni, mode, offset, len); 2806 + break; 2807 + case FALLOC_FL_COLLAPSE_RANGE: 2808 + err = ntfs_collapse_range(ni, offset, len); 2809 + break; 2810 + case FALLOC_FL_INSERT_RANGE: 2811 + err = ntfs_insert_range(ni, offset, len); 2812 + break; 2813 + default: 2814 + err = -EOPNOTSUPP; 2815 + } 2816 + 2817 + if (err) 2818 + goto out; 2819 + 2820 + err = file_modified(file); 2821 + out: 2822 + if (map_locked) 2823 + filemap_invalidate_unlock(vi->i_mapping); 2824 + if (!err) { 2825 + if (mode == 0 && NInoNonResident(ni) && 2826 + offset > old_size && old_size % PAGE_SIZE != 0) { 2827 + loff_t len = min_t(loff_t, 2828 + round_up(old_size, PAGE_SIZE) - old_size, 2829 + offset - old_size); 2830 + err = iomap_zero_range(vi, old_size, len, NULL, 2831 + &ntfs_seek_iomap_ops, 2832 + &ntfs_iomap_folio_ops, NULL); 2833 + } 2834 + NInoSetFileNameDirty(ni); 2835 + inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi)); 2836 + mark_inode_dirty(vi); 2837 + } 2838 + 2839 + inode_unlock(vi); 2840 + return err; 2841 + } 251 2842 252 2843 const struct file_operations ntfs_file_ops = { 253 - .llseek = generic_file_llseek, 254 - .read_iter = generic_file_read_iter, 255 - #ifdef NTFS_RW 2844 + .llseek = ntfs_file_llseek, 2845 + .read_iter = ntfs_file_read_iter, 256 2846 .write_iter = ntfs_file_write_iter, 257 2847 .fsync = ntfs_file_fsync, 258 - #endif /* NTFS_RW */ 259 - .mmap = generic_file_mmap, 2848 + .mmap_prepare = ntfs_file_mmap_prepare, 260 2849 .open = ntfs_file_open, 261 - .splice_read = filemap_splice_read, 2850 + .release = ntfs_file_release, 2851 + .splice_read = ntfs_file_splice_read, 2852 + .splice_write = iter_file_splice_write, 2853 + .unlocked_ioctl = ntfs_ioctl, 2854 + #ifdef CONFIG_COMPAT 2855 + .compat_ioctl = ntfs_compat_ioctl, 2856 + #endif 2857 + .fallocate = ntfs_fallocate, 2858 + .setlease = generic_setlease, 262 2859 }; 263 2860 264 2861 const struct inode_operations ntfs_file_inode_ops = { 265 - #ifdef NTFS_RW 266 2862 .setattr = ntfs_setattr, 267 - #endif /* NTFS_RW */ 2863 + .getattr = ntfs_getattr, 2864 + .listxattr = ntfs_listxattr, 2865 + .get_acl = ntfs_get_acl, 2866 + .set_acl = ntfs_set_acl, 2867 + .fiemap = ntfs_fiemap, 2868 + }; 2869 + 2870 + const struct inode_operations ntfs_symlink_inode_operations = { 2871 + .get_link = ntfs_get_link, 2872 + .setattr = ntfs_setattr, 2873 + .listxattr = ntfs_listxattr, 2874 + }; 2875 + 2876 + const struct inode_operations ntfs_special_inode_operations = { 2877 + .setattr = ntfs_setattr, 2878 + .getattr = ntfs_getattr, 2879 + .listxattr = ntfs_listxattr, 2880 + .get_acl = ntfs_get_acl, 2881 + .set_acl = ntfs_set_acl, 268 2882 }; 269 2883 270 2884 const struct file_operations ntfs_empty_file_ops = {};