1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
6 * Regular file handling primitives for NTFS-based filesystems.
10 #include <linux/backing-dev.h>
11 #include <linux/buffer_head.h>
12 #include <linux/compat.h>
13 #include <linux/falloc.h>
14 #include <linux/fiemap.h>
20 static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg)
22 struct fstrim_range __user *user_range;
23 struct fstrim_range range;
24 struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
27 if (!capable(CAP_SYS_ADMIN))
30 if (!blk_queue_discard(q))
33 user_range = (struct fstrim_range __user *)arg;
34 if (copy_from_user(&range, user_range, sizeof(range)))
37 range.minlen = max_t(u32, range.minlen, q->limits.discard_granularity);
39 err = ntfs_trim_fs(sbi, &range);
43 if (copy_to_user(user_range, &range, sizeof(range)))
49 static long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg)
51 struct inode *inode = file_inode(filp);
52 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
56 return ntfs_ioctl_fitrim(sbi, arg);
58 return -ENOTTY; /* Inappropriate ioctl for device. */
62 static long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg)
65 return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
70 * ntfs_getattr - inode_operations::getattr
72 int ntfs_getattr(struct user_namespace *mnt_userns, const struct path *path,
73 struct kstat *stat, u32 request_mask, u32 flags)
75 struct inode *inode = d_inode(path->dentry);
76 struct ntfs_inode *ni = ntfs_i(inode);
78 if (is_compressed(ni))
79 stat->attributes |= STATX_ATTR_COMPRESSED;
82 stat->attributes |= STATX_ATTR_ENCRYPTED;
84 stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED;
86 generic_fillattr(mnt_userns, inode, stat);
88 stat->result_mask |= STATX_BTIME;
89 stat->btime = ni->i_crtime;
90 stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */
95 static int ntfs_extend_initialized_size(struct file *file,
96 struct ntfs_inode *ni,
98 const loff_t new_valid)
100 struct inode *inode = &ni->vfs_inode;
101 struct address_space *mapping = inode->i_mapping;
102 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
106 if (is_resident(ni)) {
107 ni->i_valid = new_valid;
111 WARN_ON(is_compressed(ni));
112 WARN_ON(valid >= new_valid);
121 if (is_sparsed(ni)) {
122 bits = sbi->cluster_bits;
125 err = attr_data_get_block(ni, vcn, 0, &lcn, &clen,
130 if (lcn == SPARSE_LCN) {
131 loff_t vbo = (loff_t)vcn << bits;
132 loff_t to = vbo + ((loff_t)clen << bits);
134 if (to <= new_valid) {
143 to = (new_valid >> bits) << bits;
153 zerofrom = pos & (PAGE_SIZE - 1);
154 len = PAGE_SIZE - zerofrom;
156 if (pos + len > new_valid)
157 len = new_valid - pos;
159 err = pagecache_write_begin(file, mapping, pos, len, 0, &page,
164 zero_user_segment(page, zerofrom, PAGE_SIZE);
166 /* This function in any case puts page. */
167 err = pagecache_write_end(file, mapping, pos, len, len, page,
174 if (pos >= new_valid)
177 balance_dirty_pages_ratelimited(mapping);
185 ntfs_inode_warn(inode, "failed to extend initialized size to %llx.",
191 * ntfs_zero_range - Helper function for punch_hole.
193 * It zeroes a range [vbo, vbo_to).
195 static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to)
198 struct address_space *mapping = inode->i_mapping;
199 u32 blocksize = 1 << inode->i_blkbits;
200 pgoff_t idx = vbo >> PAGE_SHIFT;
201 u32 z_start = vbo & (PAGE_SIZE - 1);
202 pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT;
204 struct buffer_head *head, *bh;
205 u32 bh_next, bh_off, z_end;
209 for (; idx < idx_end; idx += 1, z_start = 0) {
210 page_off = (loff_t)idx << PAGE_SHIFT;
211 z_end = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off)
213 iblock = page_off >> inode->i_blkbits;
215 page = find_or_create_page(mapping, idx,
216 mapping_gfp_constraint(mapping,
221 if (!page_has_buffers(page))
222 create_empty_buffers(page, blocksize, 0);
224 bh = head = page_buffers(page);
227 bh_next = bh_off + blocksize;
229 if (bh_next <= z_start || bh_off >= z_end)
232 if (!buffer_mapped(bh)) {
233 ntfs_get_block(inode, iblock, bh, 0);
234 /* Unmapped? It's a hole - nothing to do. */
235 if (!buffer_mapped(bh))
239 /* Ok, it's mapped. Make sure it's up-to-date. */
240 if (PageUptodate(page))
241 set_buffer_uptodate(bh);
243 if (!buffer_uptodate(bh)) {
245 bh->b_end_io = end_buffer_read_sync;
247 submit_bh(REQ_OP_READ, 0, bh);
250 if (!buffer_uptodate(bh)) {
258 mark_buffer_dirty(bh);
260 } while (bh_off = bh_next, iblock += 1,
261 head != (bh = bh->b_this_page));
263 zero_user_segment(page, z_start, z_end);
270 mark_inode_dirty(inode);
275 * ntfs_sparse_cluster - Helper function to zero a new allocated clusters.
277 * NOTE: 512 <= cluster size <= 2M
279 void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn,
282 struct address_space *mapping = inode->i_mapping;
283 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
284 u64 vbo = (u64)vcn << sbi->cluster_bits;
285 u64 bytes = (u64)len << sbi->cluster_bits;
286 u32 blocksize = 1 << inode->i_blkbits;
287 pgoff_t idx0 = page0 ? page0->index : -1;
288 loff_t vbo_clst = vbo & sbi->cluster_mask_inv;
289 loff_t end = ntfs_up_cluster(sbi, vbo + bytes);
290 pgoff_t idx = vbo_clst >> PAGE_SHIFT;
291 u32 from = vbo_clst & (PAGE_SIZE - 1);
292 pgoff_t idx_end = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
298 for (; idx < idx_end; idx += 1, from = 0) {
299 page = idx == idx0 ? page0 : grab_cache_page(mapping, idx);
304 page_off = (loff_t)idx << PAGE_SHIFT;
305 to = (page_off + PAGE_SIZE) > end ? (end - page_off)
309 if ((from || PAGE_SIZE != to) &&
310 likely(!page_has_buffers(page))) {
311 create_empty_buffers(page, blocksize, 0);
314 if (page_has_buffers(page)) {
315 struct buffer_head *head, *bh;
318 bh = head = page_buffers(page);
320 u32 bh_next = bh_off + blocksize;
322 if (from <= bh_off && bh_next <= to) {
323 set_buffer_uptodate(bh);
324 mark_buffer_dirty(bh);
325 } else if (!buffer_uptodate(bh)) {
329 } while (head != (bh = bh->b_this_page));
332 zero_user_segment(page, from, to);
335 if (!PageUptodate(page))
336 SetPageUptodate(page);
337 set_page_dirty(page);
346 mark_inode_dirty(inode);
350 * ntfs_file_mmap - file_operations::mmap
352 static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma)
354 struct address_space *mapping = file->f_mapping;
355 struct inode *inode = mapping->host;
356 struct ntfs_inode *ni = ntfs_i(inode);
357 u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT);
358 bool rw = vma->vm_flags & VM_WRITE;
361 if (is_encrypted(ni)) {
362 ntfs_inode_warn(inode, "mmap encrypted not supported");
367 ntfs_inode_warn(inode, "mmap deduplicated not supported");
371 if (is_compressed(ni) && rw) {
372 ntfs_inode_warn(inode, "mmap(write) compressed not supported");
377 u64 to = min_t(loff_t, i_size_read(inode),
378 from + vma->vm_end - vma->vm_start);
380 if (is_sparsed(ni)) {
381 /* Allocate clusters for rw map. */
382 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
384 CLST vcn = from >> sbi->cluster_bits;
385 CLST end = bytes_to_cluster(sbi, to);
388 for (; vcn < end; vcn += len) {
389 err = attr_data_get_block(ni, vcn, 1, &lcn,
396 ntfs_sparse_cluster(inode, NULL, vcn, 1);
400 if (ni->i_valid < to) {
401 if (!inode_trylock(inode)) {
405 err = ntfs_extend_initialized_size(file, ni,
413 err = generic_file_mmap(file, vma);
418 static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
421 struct ntfs_inode *ni = ntfs_i(inode);
422 struct address_space *mapping = inode->i_mapping;
423 loff_t end = pos + count;
424 bool extend_init = file && pos > ni->i_valid;
427 if (end <= inode->i_size && !extend_init)
430 /* Mark rw ntfs as dirty. It will be cleared at umount. */
431 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
433 if (end > inode->i_size) {
434 err = ntfs_set_size(inode, end);
440 if (extend_init && !is_compressed(ni)) {
441 err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
448 inode->i_ctime = inode->i_mtime = current_time(inode);
449 mark_inode_dirty(inode);
451 if (IS_SYNC(inode)) {
454 err = filemap_fdatawrite_range(mapping, pos, end - 1);
455 err2 = sync_mapping_buffers(mapping);
458 err2 = write_inode_now(inode, 1);
462 err = filemap_fdatawait_range(mapping, pos, end - 1);
469 static int ntfs_truncate(struct inode *inode, loff_t new_size)
471 struct super_block *sb = inode->i_sb;
472 struct ntfs_inode *ni = ntfs_i(inode);
476 if (!S_ISREG(inode->i_mode))
479 if (is_compressed(ni)) {
480 if (ni->i_valid > new_size)
481 ni->i_valid = new_size;
483 err = block_truncate_page(inode->i_mapping, new_size,
489 new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
493 truncate_setsize(inode, new_size);
495 down_write(&ni->file.run_lock);
496 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
497 &new_valid, true, NULL);
498 up_write(&ni->file.run_lock);
500 if (new_valid < ni->i_valid)
501 ni->i_valid = new_valid;
505 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
506 inode->i_ctime = inode->i_mtime = current_time(inode);
507 if (!IS_DIRSYNC(inode)) {
510 err = ntfs_sync_inode(inode);
516 mark_inode_dirty(inode);
518 /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
526 * Preallocate space for a file. This implements ntfs's fallocate file
527 * operation, which gets called from sys_fallocate system call. User
528 * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
529 * we just allocate clusters without zeroing them out. Otherwise we
530 * allocate and zero out clusters via an expanding truncate.
532 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
534 struct inode *inode = file->f_mapping->host;
535 struct super_block *sb = inode->i_sb;
536 struct ntfs_sb_info *sbi = sb->s_fs_info;
537 struct ntfs_inode *ni = ntfs_i(inode);
538 loff_t end = vbo + len;
539 loff_t vbo_down = round_down(vbo, PAGE_SIZE);
543 /* No support for dir. */
544 if (!S_ISREG(inode->i_mode))
547 /* Return error if mode is not supported. */
548 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
549 FALLOC_FL_COLLAPSE_RANGE)) {
550 ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
555 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
558 i_size = inode->i_size;
560 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
561 /* Should never be here, see ntfs_file_open. */
566 if (mode & FALLOC_FL_PUNCH_HOLE) {
568 loff_t mask, vbo_a, end_a, tmp;
570 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
575 err = filemap_write_and_wait_range(inode->i_mapping, vbo,
580 err = filemap_write_and_wait_range(inode->i_mapping, end,
585 inode_dio_wait(inode);
587 truncate_pagecache(inode, vbo_down);
589 if (!is_sparsed(ni) && !is_compressed(ni)) {
591 err = ntfs_zero_range(inode, vbo, end);
596 err = attr_punch_hole(ni, vbo, len, &frame_size);
598 if (err != E_NTFS_NOTALIGNED)
601 /* Process not aligned punch. */
602 mask = frame_size - 1;
603 vbo_a = (vbo + mask) & ~mask;
606 tmp = min(vbo_a, end);
608 err = ntfs_zero_range(inode, vbo, tmp);
613 if (vbo < end_a && end_a < end) {
614 err = ntfs_zero_range(inode, end_a, end);
619 /* Aligned punch_hole */
622 err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
625 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
626 if (mode & ~FALLOC_FL_COLLAPSE_RANGE) {
632 * Write tail of the last page before removed range since
633 * it will get removed from the page cache below.
635 err = filemap_write_and_wait_range(inode->i_mapping, vbo_down,
641 * Write data that will be shifted to preserve them
642 * when discarding page cache below.
644 err = filemap_write_and_wait_range(inode->i_mapping, end,
649 /* Wait for existing dio to complete. */
650 inode_dio_wait(inode);
652 truncate_pagecache(inode, vbo_down);
655 err = attr_collapse_range(ni, vbo, len);
659 * Normal file: Allocate clusters, do not change 'valid' size.
661 err = ntfs_set_size(inode, max(end, i_size));
665 if (is_sparsed(ni) || is_compressed(ni)) {
666 CLST vcn_v = ni->i_valid >> sbi->cluster_bits;
667 CLST vcn = vbo >> sbi->cluster_bits;
668 CLST cend = bytes_to_cluster(sbi, end);
673 * Allocate but do not zero new clusters. (see below comments)
674 * This breaks security: One can read unused on-disk areas.
675 * Zeroing these clusters may be too long.
676 * Maybe we should check here for root rights?
678 for (; vcn < cend; vcn += clen) {
679 err = attr_data_get_block(ni, vcn, cend - vcn,
683 if (!new || vcn >= vcn_v)
688 * NTFS is not able to store several unwritten areas.
689 * Activate 'ntfs_sparse_cluster' to zero new allocated clusters.
692 * 1G of sparsed clusters + 1 cluster of data =>
693 * valid_size == 1G + 1 cluster
694 * fallocate(1G) will zero 1G and this can be very long
695 * xfstest 016/086 will fail without 'ntfs_sparse_cluster'.
697 ntfs_sparse_cluster(inode, NULL, vcn,
698 min(vcn_v - vcn, clen));
702 if (mode & FALLOC_FL_KEEP_SIZE) {
704 /* True - Keep preallocated. */
705 err = attr_set_size(ni, ATTR_DATA, NULL, 0,
706 &ni->file.run, i_size, &ni->i_valid,
717 inode->i_ctime = inode->i_mtime = current_time(inode);
718 mark_inode_dirty(inode);
726 * ntfs3_setattr - inode_operations::setattr
728 int ntfs3_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
731 struct super_block *sb = dentry->d_sb;
732 struct ntfs_sb_info *sbi = sb->s_fs_info;
733 struct inode *inode = d_inode(dentry);
734 struct ntfs_inode *ni = ntfs_i(inode);
735 u32 ia_valid = attr->ia_valid;
736 umode_t mode = inode->i_mode;
739 if (sbi->options->noacsrules) {
740 /* "No access rules" - Force any changes of time etc. */
741 attr->ia_valid |= ATTR_FORCE;
742 /* and disable for editing some attributes. */
743 attr->ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
744 ia_valid = attr->ia_valid;
747 err = setattr_prepare(mnt_userns, dentry, attr);
751 if (ia_valid & ATTR_SIZE) {
752 loff_t oldsize = inode->i_size;
754 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
755 /* Should never be here, see ntfs_file_open(). */
759 inode_dio_wait(inode);
761 if (attr->ia_size < oldsize)
762 err = ntfs_truncate(inode, attr->ia_size);
763 else if (attr->ia_size > oldsize)
764 err = ntfs_extend(inode, attr->ia_size, 0, NULL);
769 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
772 setattr_copy(mnt_userns, inode, attr);
774 if (mode != inode->i_mode) {
775 err = ntfs_acl_chmod(mnt_userns, inode);
779 /* Linux 'w' -> Windows 'ro'. */
780 if (0222 & inode->i_mode)
781 ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
783 ni->std_fa |= FILE_ATTRIBUTE_READONLY;
786 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
787 ntfs_save_wsl_perm(inode);
788 mark_inode_dirty(inode);
793 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
795 struct file *file = iocb->ki_filp;
796 struct inode *inode = file->f_mapping->host;
797 struct ntfs_inode *ni = ntfs_i(inode);
799 if (is_encrypted(ni)) {
800 ntfs_inode_warn(inode, "encrypted i/o not supported");
804 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
805 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
809 #ifndef CONFIG_NTFS3_LZX_XPRESS
810 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
813 "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
819 ntfs_inode_warn(inode, "read deduplicated not supported");
823 return generic_file_read_iter(iocb, iter);
827 * ntfs_get_frame_pages
829 * Return: Array of locked pages.
831 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
832 struct page **pages, u32 pages_per_frame,
833 bool *frame_uptodate)
835 gfp_t gfp_mask = mapping_gfp_mask(mapping);
838 *frame_uptodate = true;
840 for (npages = 0; npages < pages_per_frame; npages++, index++) {
843 page = find_or_create_page(mapping, index, gfp_mask);
846 page = pages[npages];
854 if (!PageUptodate(page))
855 *frame_uptodate = false;
857 pages[npages] = page;
864 * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
866 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
869 struct file *file = iocb->ki_filp;
870 size_t count = iov_iter_count(from);
871 loff_t pos = iocb->ki_pos;
872 struct inode *inode = file_inode(file);
873 loff_t i_size = inode->i_size;
874 struct address_space *mapping = inode->i_mapping;
875 struct ntfs_inode *ni = ntfs_i(inode);
876 u64 valid = ni->i_valid;
877 struct ntfs_sb_info *sbi = ni->mi.sbi;
878 struct page *page, **pages = NULL;
880 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
881 u32 frame_size = 1u << frame_bits;
882 u32 pages_per_frame = frame_size >> PAGE_SHIFT;
889 if (frame_size < PAGE_SIZE) {
891 * frame_size == 8K if cluster 512
892 * frame_size == 64K if cluster 4096
894 ntfs_inode_warn(inode, "page size is bigger than frame size");
898 pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
902 current->backing_dev_info = inode_to_bdi(inode);
903 err = file_remove_privs(file);
907 err = file_update_time(file);
911 /* Zero range [valid : pos). */
912 while (valid < pos) {
915 frame = valid >> frame_bits;
916 frame_vbo = valid & ~(frame_size - 1);
917 off = valid & (frame_size - 1);
919 err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 0, &lcn,
924 if (lcn == SPARSE_LCN) {
925 ni->i_valid = valid =
926 frame_vbo + ((u64)clen << sbi->cluster_bits);
930 /* Load full frame. */
931 err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
932 pages, pages_per_frame,
937 if (!frame_uptodate && off) {
938 err = ni_read_frame(ni, frame_vbo, pages,
941 for (ip = 0; ip < pages_per_frame; ip++) {
950 ip = off >> PAGE_SHIFT;
951 off = offset_in_page(valid);
952 for (; ip < pages_per_frame; ip++, off = 0) {
954 zero_user_segment(page, off, PAGE_SIZE);
955 flush_dcache_page(page);
956 SetPageUptodate(page);
960 err = ni_write_frame(ni, pages, pages_per_frame);
963 for (ip = 0; ip < pages_per_frame; ip++) {
965 SetPageUptodate(page);
973 ni->i_valid = valid = frame_vbo + frame_size;
976 /* Copy user data [pos : pos + count). */
978 size_t copied, bytes;
980 off = pos & (frame_size - 1);
981 bytes = frame_size - off;
985 frame = pos >> frame_bits;
986 frame_vbo = pos & ~(frame_size - 1);
987 index = frame_vbo >> PAGE_SHIFT;
989 if (unlikely(iov_iter_fault_in_readable(from, bytes))) {
994 /* Load full frame. */
995 err = ntfs_get_frame_pages(mapping, index, pages,
996 pages_per_frame, &frame_uptodate);
1000 if (!frame_uptodate) {
1001 loff_t to = pos + bytes;
1003 if (off || (to < i_size && (to & (frame_size - 1)))) {
1004 err = ni_read_frame(ni, frame_vbo, pages,
1007 for (ip = 0; ip < pages_per_frame;
1020 ip = off >> PAGE_SHIFT;
1021 off = offset_in_page(pos);
1023 /* Copy user data to pages. */
1025 size_t cp, tail = PAGE_SIZE - off;
1028 cp = copy_page_from_iter_atomic(page, off,
1029 min(tail, bytes), from);
1030 flush_dcache_page(page);
1046 err = ni_write_frame(ni, pages, pages_per_frame);
1049 for (ip = 0; ip < pages_per_frame; ip++) {
1051 ClearPageDirty(page);
1052 SetPageUptodate(page);
1061 * We can loop for a long time in here. Be nice and allow
1062 * us to schedule out to avoid softlocking if preempt
1070 count = iov_iter_count(from);
1076 current->backing_dev_info = NULL;
1081 iocb->ki_pos += written;
1082 if (iocb->ki_pos > ni->i_valid)
1083 ni->i_valid = iocb->ki_pos;
1089 * ntfs_file_write_iter - file_operations::write_iter
1091 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1093 struct file *file = iocb->ki_filp;
1094 struct address_space *mapping = file->f_mapping;
1095 struct inode *inode = mapping->host;
1097 struct ntfs_inode *ni = ntfs_i(inode);
1099 if (is_encrypted(ni)) {
1100 ntfs_inode_warn(inode, "encrypted i/o not supported");
1104 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
1105 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
1110 ntfs_inode_warn(inode, "write into deduplicated not supported");
1114 if (!inode_trylock(inode)) {
1115 if (iocb->ki_flags & IOCB_NOWAIT)
1120 ret = generic_write_checks(iocb, from);
1124 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
1125 /* Should never be here, see ntfs_file_open(). */
1130 ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
1134 ret = is_compressed(ni) ? ntfs_compress_write(iocb, from)
1135 : __generic_file_write_iter(iocb, from);
1138 inode_unlock(inode);
1141 ret = generic_write_sync(iocb, ret);
1147 * ntfs_file_open - file_operations::open
1149 int ntfs_file_open(struct inode *inode, struct file *file)
1151 struct ntfs_inode *ni = ntfs_i(inode);
1153 if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
1154 (file->f_flags & O_DIRECT))) {
1158 /* Decompress "external compressed" file if opened for rw. */
1159 if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
1160 (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
1161 #ifdef CONFIG_NTFS3_LZX_XPRESS
1162 int err = ni_decompress_file(ni);
1169 "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
1174 return generic_file_open(inode, file);
1178 * ntfs_file_release - file_operations::release
1180 static int ntfs_file_release(struct inode *inode, struct file *file)
1182 struct ntfs_inode *ni = ntfs_i(inode);
1183 struct ntfs_sb_info *sbi = ni->mi.sbi;
1186 /* If we are last writer on the inode, drop the block reservation. */
1187 if (sbi->options->prealloc && ((file->f_mode & FMODE_WRITE) &&
1188 atomic_read(&inode->i_writecount) == 1)) {
1190 down_write(&ni->file.run_lock);
1192 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
1193 inode->i_size, &ni->i_valid, false, NULL);
1195 up_write(&ni->file.run_lock);
1202 * ntfs_fiemap - file_operations::fiemap
1204 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1205 __u64 start, __u64 len)
1208 struct ntfs_inode *ni = ntfs_i(inode);
1210 err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
1216 err = ni_fiemap(ni, fieinfo, start, len);
1224 const struct inode_operations ntfs_file_inode_operations = {
1225 .getattr = ntfs_getattr,
1226 .setattr = ntfs3_setattr,
1227 .listxattr = ntfs_listxattr,
1228 .permission = ntfs_permission,
1229 .get_acl = ntfs_get_acl,
1230 .set_acl = ntfs_set_acl,
1231 .fiemap = ntfs_fiemap,
1234 const struct file_operations ntfs_file_operations = {
1235 .llseek = generic_file_llseek,
1236 .read_iter = ntfs_file_read_iter,
1237 .write_iter = ntfs_file_write_iter,
1238 .unlocked_ioctl = ntfs_ioctl,
1239 #ifdef CONFIG_COMPAT
1240 .compat_ioctl = ntfs_compat_ioctl,
1242 .splice_read = generic_file_splice_read,
1243 .mmap = ntfs_file_mmap,
1244 .open = ntfs_file_open,
1245 .fsync = generic_file_fsync,
1246 .splice_write = iter_file_splice_write,
1247 .fallocate = ntfs_fallocate,
1248 .release = ntfs_file_release,