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/blkdev.h>
12 #include <linux/buffer_head.h>
13 #include <linux/compat.h>
14 #include <linux/falloc.h>
15 #include <linux/fiemap.h>
21 static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg)
23 struct fstrim_range __user *user_range;
24 struct fstrim_range range;
25 struct block_device *dev;
28 if (!capable(CAP_SYS_ADMIN))
31 dev = sbi->sb->s_bdev;
32 if (!bdev_max_discard_sectors(dev))
35 user_range = (struct fstrim_range __user *)arg;
36 if (copy_from_user(&range, user_range, sizeof(range)))
39 range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev));
41 err = ntfs_trim_fs(sbi, &range);
45 if (copy_to_user(user_range, &range, sizeof(range)))
51 long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg)
53 struct inode *inode = file_inode(filp);
54 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
58 return ntfs_ioctl_fitrim(sbi, arg);
60 return -ENOTTY; /* Inappropriate ioctl for device. */
64 long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg)
67 return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
72 * ntfs_getattr - inode_operations::getattr
74 int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
75 struct kstat *stat, u32 request_mask, u32 flags)
77 struct inode *inode = d_inode(path->dentry);
78 struct ntfs_inode *ni = ntfs_i(inode);
80 if (is_compressed(ni))
81 stat->attributes |= STATX_ATTR_COMPRESSED;
84 stat->attributes |= STATX_ATTR_ENCRYPTED;
86 stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED;
88 generic_fillattr(idmap, request_mask, inode, stat);
90 stat->result_mask |= STATX_BTIME;
91 stat->btime = ni->i_crtime;
92 stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */
97 static int ntfs_extend_initialized_size(struct file *file,
98 struct ntfs_inode *ni,
100 const loff_t new_valid)
102 struct inode *inode = &ni->vfs_inode;
103 struct address_space *mapping = inode->i_mapping;
104 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
108 if (is_resident(ni)) {
109 ni->i_valid = new_valid;
113 WARN_ON(is_compressed(ni));
114 WARN_ON(valid >= new_valid);
122 if (is_sparsed(ni)) {
123 bits = sbi->cluster_bits;
126 err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL,
131 if (lcn == SPARSE_LCN) {
132 pos = ((loff_t)clen + vcn) << bits;
138 zerofrom = pos & (PAGE_SIZE - 1);
139 len = PAGE_SIZE - zerofrom;
141 if (pos + len > new_valid)
142 len = new_valid - pos;
144 err = ntfs_write_begin(file, mapping, pos, len, &page, NULL);
148 zero_user_segment(page, zerofrom, PAGE_SIZE);
150 /* This function in any case puts page. */
151 err = ntfs_write_end(file, mapping, pos, len, len, page, NULL);
157 if (pos >= new_valid)
160 balance_dirty_pages_ratelimited(mapping);
168 ntfs_inode_warn(inode, "failed to extend initialized size to %llx.",
174 * ntfs_zero_range - Helper function for punch_hole.
176 * It zeroes a range [vbo, vbo_to).
178 static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to)
181 struct address_space *mapping = inode->i_mapping;
182 u32 blocksize = i_blocksize(inode);
183 pgoff_t idx = vbo >> PAGE_SHIFT;
184 u32 from = vbo & (PAGE_SIZE - 1);
185 pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT;
187 struct buffer_head *head, *bh;
188 u32 bh_next, bh_off, to;
193 for (; idx < idx_end; idx += 1, from = 0) {
194 page_off = (loff_t)idx << PAGE_SHIFT;
195 to = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off) :
197 iblock = page_off >> inode->i_blkbits;
199 folio = __filemap_get_folio(mapping, idx,
200 FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
201 mapping_gfp_constraint(mapping, ~__GFP_FS));
203 return PTR_ERR(folio);
205 head = folio_buffers(folio);
207 head = create_empty_buffers(folio, blocksize, 0);
212 bh_next = bh_off + blocksize;
214 if (bh_next <= from || bh_off >= to)
217 if (!buffer_mapped(bh)) {
218 ntfs_get_block(inode, iblock, bh, 0);
219 /* Unmapped? It's a hole - nothing to do. */
220 if (!buffer_mapped(bh))
224 /* Ok, it's mapped. Make sure it's up-to-date. */
225 if (folio_test_uptodate(folio))
226 set_buffer_uptodate(bh);
227 else if (bh_read(bh, 0) < 0) {
234 mark_buffer_dirty(bh);
235 } while (bh_off = bh_next, iblock += 1,
236 head != (bh = bh->b_this_page));
238 folio_zero_segment(folio, from, to);
247 mark_inode_dirty(inode);
252 * ntfs_file_mmap - file_operations::mmap
254 static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma)
256 struct address_space *mapping = file->f_mapping;
257 struct inode *inode = mapping->host;
258 struct ntfs_inode *ni = ntfs_i(inode);
259 u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT);
260 bool rw = vma->vm_flags & VM_WRITE;
263 if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
266 if (is_encrypted(ni)) {
267 ntfs_inode_warn(inode, "mmap encrypted not supported");
272 ntfs_inode_warn(inode, "mmap deduplicated not supported");
276 if (is_compressed(ni) && rw) {
277 ntfs_inode_warn(inode, "mmap(write) compressed not supported");
282 u64 to = min_t(loff_t, i_size_read(inode),
283 from + vma->vm_end - vma->vm_start);
285 if (is_sparsed(ni)) {
286 /* Allocate clusters for rw map. */
287 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
289 CLST vcn = from >> sbi->cluster_bits;
290 CLST end = bytes_to_cluster(sbi, to);
293 for (; vcn < end; vcn += len) {
294 err = attr_data_get_block(ni, vcn, 1, &lcn,
301 if (ni->i_valid < to) {
302 if (!inode_trylock(inode)) {
306 err = ntfs_extend_initialized_size(file, ni,
314 err = generic_file_mmap(file, vma);
319 static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
322 struct ntfs_inode *ni = ntfs_i(inode);
323 struct address_space *mapping = inode->i_mapping;
324 loff_t end = pos + count;
325 bool extend_init = file && pos > ni->i_valid;
328 if (end <= inode->i_size && !extend_init)
331 /* Mark rw ntfs as dirty. It will be cleared at umount. */
332 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
334 if (end > inode->i_size) {
335 err = ntfs_set_size(inode, end);
340 if (extend_init && !is_compressed(ni)) {
341 err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
348 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
349 mark_inode_dirty(inode);
351 if (IS_SYNC(inode)) {
354 err = filemap_fdatawrite_range(mapping, pos, end - 1);
355 err2 = sync_mapping_buffers(mapping);
358 err2 = write_inode_now(inode, 1);
362 err = filemap_fdatawait_range(mapping, pos, end - 1);
369 static int ntfs_truncate(struct inode *inode, loff_t new_size)
371 struct super_block *sb = inode->i_sb;
372 struct ntfs_inode *ni = ntfs_i(inode);
376 if (!S_ISREG(inode->i_mode))
379 if (is_compressed(ni)) {
380 if (ni->i_valid > new_size)
381 ni->i_valid = new_size;
383 err = block_truncate_page(inode->i_mapping, new_size,
389 new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
391 truncate_setsize(inode, new_size);
395 down_write(&ni->file.run_lock);
396 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
397 &new_valid, ni->mi.sbi->options->prealloc, NULL);
398 up_write(&ni->file.run_lock);
400 if (new_valid < ni->i_valid)
401 ni->i_valid = new_valid;
405 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
406 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
407 if (!IS_DIRSYNC(inode)) {
410 err = ntfs_sync_inode(inode);
416 mark_inode_dirty(inode);
418 /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
426 * Preallocate space for a file. This implements ntfs's fallocate file
427 * operation, which gets called from sys_fallocate system call. User
428 * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
429 * we just allocate clusters without zeroing them out. Otherwise we
430 * allocate and zero out clusters via an expanding truncate.
432 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
434 struct inode *inode = file->f_mapping->host;
435 struct address_space *mapping = inode->i_mapping;
436 struct super_block *sb = inode->i_sb;
437 struct ntfs_sb_info *sbi = sb->s_fs_info;
438 struct ntfs_inode *ni = ntfs_i(inode);
439 loff_t end = vbo + len;
440 loff_t vbo_down = round_down(vbo, max_t(unsigned long,
441 sbi->cluster_size, PAGE_SIZE));
442 bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);
443 loff_t i_size, new_size;
447 /* No support for dir. */
448 if (!S_ISREG(inode->i_mode))
452 * vfs_fallocate checks all possible combinations of mode.
453 * Do additional checks here before ntfs_set_state(dirty).
455 if (mode & FALLOC_FL_PUNCH_HOLE) {
456 if (!is_supported_holes)
458 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
459 } else if (mode & FALLOC_FL_INSERT_RANGE) {
460 if (!is_supported_holes)
463 ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
464 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) {
465 ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
470 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
473 i_size = inode->i_size;
474 new_size = max(end, i_size);
477 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
478 /* Should never be here, see ntfs_file_open. */
483 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
484 FALLOC_FL_INSERT_RANGE)) {
485 inode_dio_wait(inode);
486 filemap_invalidate_lock(mapping);
490 if (mode & FALLOC_FL_PUNCH_HOLE) {
492 loff_t mask, vbo_a, end_a, tmp;
494 err = filemap_write_and_wait_range(mapping, vbo_down,
499 truncate_pagecache(inode, vbo_down);
502 err = attr_punch_hole(ni, vbo, len, &frame_size);
507 if (err != E_NTFS_NOTALIGNED)
510 /* Process not aligned punch. */
512 mask = frame_size - 1;
513 vbo_a = (vbo + mask) & ~mask;
516 tmp = min(vbo_a, end);
518 err = ntfs_zero_range(inode, vbo, tmp);
523 if (vbo < end_a && end_a < end) {
524 err = ntfs_zero_range(inode, end_a, end);
529 /* Aligned punch_hole */
532 err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
537 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
539 * Write tail of the last page before removed range since
540 * it will get removed from the page cache below.
542 err = filemap_write_and_wait_range(mapping, vbo_down, vbo);
547 * Write data that will be shifted to preserve them
548 * when discarding page cache below.
550 err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
554 truncate_pagecache(inode, vbo_down);
557 err = attr_collapse_range(ni, vbo, len);
559 } else if (mode & FALLOC_FL_INSERT_RANGE) {
560 /* Check new size. */
561 err = inode_newsize_ok(inode, new_size);
565 /* Write out all dirty pages. */
566 err = filemap_write_and_wait_range(mapping, vbo_down,
570 truncate_pagecache(inode, vbo_down);
573 err = attr_insert_range(ni, vbo, len);
578 /* Check new size. */
579 u8 cluster_bits = sbi->cluster_bits;
581 /* Be sure file is non resident. */
582 if (is_resident(ni)) {
584 err = attr_force_nonresident(ni);
590 /* generic/213: expected -ENOSPC instead of -EFBIG. */
591 if (!is_supported_holes) {
592 loff_t to_alloc = new_size - inode_get_bytes(inode);
595 (to_alloc >> cluster_bits) >
596 wnd_zeroes(&sbi->used.bitmap)) {
602 err = inode_newsize_ok(inode, new_size);
606 if (new_size > i_size) {
608 * Allocate clusters, do not change 'valid' size.
610 err = ntfs_set_size(inode, new_size);
615 if (is_supported_holes) {
616 CLST vcn = vbo >> cluster_bits;
617 CLST cend = bytes_to_cluster(sbi, end);
618 CLST cend_v = bytes_to_cluster(sbi, ni->i_valid);
626 * Allocate and zero new clusters.
627 * Zeroing these clusters may be too long.
629 for (; vcn < cend_v; vcn += clen) {
630 err = attr_data_get_block(ni, vcn, cend_v - vcn,
637 * Allocate but not zero new clusters.
639 for (; vcn < cend; vcn += clen) {
640 err = attr_data_get_block(ni, vcn, cend - vcn,
648 if (mode & FALLOC_FL_KEEP_SIZE) {
650 /* True - Keep preallocated. */
651 err = attr_set_size(ni, ATTR_DATA, NULL, 0,
652 &ni->file.run, i_size, &ni->i_valid,
657 } else if (new_size > i_size) {
658 i_size_write(inode, new_size);
663 err = file_modified(file);
669 filemap_invalidate_unlock(mapping);
672 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
673 mark_inode_dirty(inode);
681 * ntfs3_setattr - inode_operations::setattr
683 int ntfs3_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
686 struct inode *inode = d_inode(dentry);
687 struct ntfs_inode *ni = ntfs_i(inode);
688 u32 ia_valid = attr->ia_valid;
689 umode_t mode = inode->i_mode;
692 if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
695 err = setattr_prepare(idmap, dentry, attr);
699 if (ia_valid & ATTR_SIZE) {
700 loff_t newsize, oldsize;
702 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
703 /* Should never be here, see ntfs_file_open(). */
707 inode_dio_wait(inode);
708 oldsize = i_size_read(inode);
709 newsize = attr->ia_size;
711 if (newsize <= oldsize)
712 err = ntfs_truncate(inode, newsize);
714 err = ntfs_extend(inode, newsize, 0, NULL);
719 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
720 i_size_write(inode, newsize);
723 setattr_copy(idmap, inode, attr);
725 if (mode != inode->i_mode) {
726 err = ntfs_acl_chmod(idmap, dentry);
730 /* Linux 'w' -> Windows 'ro'. */
731 if (0222 & inode->i_mode)
732 ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
734 ni->std_fa |= FILE_ATTRIBUTE_READONLY;
737 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
738 ntfs_save_wsl_perm(inode, NULL);
739 mark_inode_dirty(inode);
744 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
746 struct file *file = iocb->ki_filp;
747 struct inode *inode = file->f_mapping->host;
748 struct ntfs_inode *ni = ntfs_i(inode);
750 if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
753 if (is_encrypted(ni)) {
754 ntfs_inode_warn(inode, "encrypted i/o not supported");
758 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
759 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
763 #ifndef CONFIG_NTFS3_LZX_XPRESS
764 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
767 "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
773 ntfs_inode_warn(inode, "read deduplicated not supported");
777 return generic_file_read_iter(iocb, iter);
780 static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
781 struct pipe_inode_info *pipe, size_t len,
784 struct inode *inode = in->f_mapping->host;
785 struct ntfs_inode *ni = ntfs_i(inode);
787 if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
790 if (is_encrypted(ni)) {
791 ntfs_inode_warn(inode, "encrypted i/o not supported");
795 #ifndef CONFIG_NTFS3_LZX_XPRESS
796 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
799 "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
805 ntfs_inode_warn(inode, "read deduplicated not supported");
809 return filemap_splice_read(in, ppos, pipe, len, flags);
813 * ntfs_get_frame_pages
815 * Return: Array of locked pages.
817 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
818 struct page **pages, u32 pages_per_frame,
819 bool *frame_uptodate)
821 gfp_t gfp_mask = mapping_gfp_mask(mapping);
824 *frame_uptodate = true;
826 for (npages = 0; npages < pages_per_frame; npages++, index++) {
829 page = find_or_create_page(mapping, index, gfp_mask);
832 page = pages[npages];
840 if (!PageUptodate(page))
841 *frame_uptodate = false;
843 pages[npages] = page;
850 * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
852 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
855 struct file *file = iocb->ki_filp;
856 size_t count = iov_iter_count(from);
857 loff_t pos = iocb->ki_pos;
858 struct inode *inode = file_inode(file);
859 loff_t i_size = i_size_read(inode);
860 struct address_space *mapping = inode->i_mapping;
861 struct ntfs_inode *ni = ntfs_i(inode);
862 u64 valid = ni->i_valid;
863 struct ntfs_sb_info *sbi = ni->mi.sbi;
864 struct page *page, **pages = NULL;
866 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
867 u32 frame_size = 1u << frame_bits;
868 u32 pages_per_frame = frame_size >> PAGE_SHIFT;
875 if (frame_size < PAGE_SIZE) {
877 * frame_size == 8K if cluster 512
878 * frame_size == 64K if cluster 4096
880 ntfs_inode_warn(inode, "page size is bigger than frame size");
884 pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
888 err = file_remove_privs(file);
892 err = file_update_time(file);
896 /* Zero range [valid : pos). */
897 while (valid < pos) {
900 frame = valid >> frame_bits;
901 frame_vbo = valid & ~(frame_size - 1);
902 off = valid & (frame_size - 1);
904 err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn,
909 if (lcn == SPARSE_LCN) {
910 ni->i_valid = valid =
911 frame_vbo + ((u64)clen << sbi->cluster_bits);
915 /* Load full frame. */
916 err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
917 pages, pages_per_frame,
922 if (!frame_uptodate && off) {
923 err = ni_read_frame(ni, frame_vbo, pages,
926 for (ip = 0; ip < pages_per_frame; ip++) {
935 ip = off >> PAGE_SHIFT;
936 off = offset_in_page(valid);
937 for (; ip < pages_per_frame; ip++, off = 0) {
939 zero_user_segment(page, off, PAGE_SIZE);
940 flush_dcache_page(page);
941 SetPageUptodate(page);
945 err = ni_write_frame(ni, pages, pages_per_frame);
948 for (ip = 0; ip < pages_per_frame; ip++) {
950 SetPageUptodate(page);
958 ni->i_valid = valid = frame_vbo + frame_size;
961 /* Copy user data [pos : pos + count). */
963 size_t copied, bytes;
965 off = pos & (frame_size - 1);
966 bytes = frame_size - off;
970 frame_vbo = pos & ~(frame_size - 1);
971 index = frame_vbo >> PAGE_SHIFT;
973 if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
978 /* Load full frame. */
979 err = ntfs_get_frame_pages(mapping, index, pages,
980 pages_per_frame, &frame_uptodate);
984 if (!frame_uptodate) {
985 loff_t to = pos + bytes;
987 if (off || (to < i_size && (to & (frame_size - 1)))) {
988 err = ni_read_frame(ni, frame_vbo, pages,
991 for (ip = 0; ip < pages_per_frame;
1004 ip = off >> PAGE_SHIFT;
1005 off = offset_in_page(pos);
1007 /* Copy user data to pages. */
1009 size_t cp, tail = PAGE_SIZE - off;
1012 cp = copy_page_from_iter_atomic(page, off,
1013 min(tail, bytes), from);
1014 flush_dcache_page(page);
1030 err = ni_write_frame(ni, pages, pages_per_frame);
1033 for (ip = 0; ip < pages_per_frame; ip++) {
1035 ClearPageDirty(page);
1036 SetPageUptodate(page);
1045 * We can loop for a long time in here. Be nice and allow
1046 * us to schedule out to avoid softlocking if preempt
1054 count = iov_iter_count(from);
1063 iocb->ki_pos += written;
1064 if (iocb->ki_pos > ni->i_valid)
1065 ni->i_valid = iocb->ki_pos;
1066 if (iocb->ki_pos > i_size)
1067 i_size_write(inode, iocb->ki_pos);
1073 * ntfs_file_write_iter - file_operations::write_iter
1075 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1077 struct file *file = iocb->ki_filp;
1078 struct address_space *mapping = file->f_mapping;
1079 struct inode *inode = mapping->host;
1082 struct ntfs_inode *ni = ntfs_i(inode);
1084 if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
1087 if (is_encrypted(ni)) {
1088 ntfs_inode_warn(inode, "encrypted i/o not supported");
1092 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
1093 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
1098 ntfs_inode_warn(inode, "write into deduplicated not supported");
1102 if (!inode_trylock(inode)) {
1103 if (iocb->ki_flags & IOCB_NOWAIT)
1108 ret = generic_write_checks(iocb, from);
1112 err = file_modified(iocb->ki_filp);
1118 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
1119 /* Should never be here, see ntfs_file_open(). */
1124 ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
1128 ret = is_compressed(ni) ? ntfs_compress_write(iocb, from) :
1129 __generic_file_write_iter(iocb, from);
1132 inode_unlock(inode);
1135 ret = generic_write_sync(iocb, ret);
1141 * ntfs_file_open - file_operations::open
1143 int ntfs_file_open(struct inode *inode, struct file *file)
1145 struct ntfs_inode *ni = ntfs_i(inode);
1147 if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
1150 if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
1151 (file->f_flags & O_DIRECT))) {
1155 /* Decompress "external compressed" file if opened for rw. */
1156 if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
1157 (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
1158 #ifdef CONFIG_NTFS3_LZX_XPRESS
1159 int err = ni_decompress_file(ni);
1166 "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
1171 return generic_file_open(inode, file);
1175 * ntfs_file_release - file_operations::release
1177 static int ntfs_file_release(struct inode *inode, struct file *file)
1179 struct ntfs_inode *ni = ntfs_i(inode);
1180 struct ntfs_sb_info *sbi = ni->mi.sbi;
1183 /* If we are last writer on the inode, drop the block reservation. */
1184 if (sbi->options->prealloc &&
1185 ((file->f_mode & FMODE_WRITE) &&
1186 atomic_read(&inode->i_writecount) == 1)) {
1188 down_write(&ni->file.run_lock);
1190 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
1191 i_size_read(inode), &ni->i_valid, false,
1194 up_write(&ni->file.run_lock);
1201 * ntfs_fiemap - file_operations::fiemap
1203 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1204 __u64 start, __u64 len)
1207 struct ntfs_inode *ni = ntfs_i(inode);
1209 err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
1215 err = ni_fiemap(ni, fieinfo, start, len);
1223 const struct inode_operations ntfs_file_inode_operations = {
1224 .getattr = ntfs_getattr,
1225 .setattr = ntfs3_setattr,
1226 .listxattr = ntfs_listxattr,
1227 .get_acl = ntfs_get_acl,
1228 .set_acl = ntfs_set_acl,
1229 .fiemap = ntfs_fiemap,
1232 const struct file_operations ntfs_file_operations = {
1233 .llseek = generic_file_llseek,
1234 .read_iter = ntfs_file_read_iter,
1235 .write_iter = ntfs_file_write_iter,
1236 .unlocked_ioctl = ntfs_ioctl,
1237 #ifdef CONFIG_COMPAT
1238 .compat_ioctl = ntfs_compat_ioctl,
1240 .splice_read = ntfs_file_splice_read,
1241 .mmap = ntfs_file_mmap,
1242 .open = ntfs_file_open,
1243 .fsync = generic_file_fsync,
1244 .splice_write = iter_file_splice_write,
1245 .fallocate = ntfs_fallocate,
1246 .release = ntfs_file_release,
1249 const struct file_operations ntfs_legacy_file_operations = {
1250 .llseek = generic_file_llseek,
1251 .read_iter = ntfs_file_read_iter,
1252 .splice_read = ntfs_file_splice_read,
1253 .open = ntfs_file_open,
1254 .release = ntfs_file_release,