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;
27 if (!capable(CAP_SYS_ADMIN))
30 if (!bdev_max_discard_sectors(sbi->sb->s_bdev))
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,
38 bdev_discard_granularity(sbi->sb->s_bdev));
40 err = ntfs_trim_fs(sbi, &range);
44 if (copy_to_user(user_range, &range, sizeof(range)))
50 static long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg)
52 struct inode *inode = file_inode(filp);
53 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
57 return ntfs_ioctl_fitrim(sbi, arg);
59 return -ENOTTY; /* Inappropriate ioctl for device. */
63 static long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg)
66 return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
71 * ntfs_getattr - inode_operations::getattr
73 int ntfs_getattr(struct user_namespace *mnt_userns, const struct path *path,
74 struct kstat *stat, u32 request_mask, u32 flags)
76 struct inode *inode = d_inode(path->dentry);
77 struct ntfs_inode *ni = ntfs_i(inode);
79 if (is_compressed(ni))
80 stat->attributes |= STATX_ATTR_COMPRESSED;
83 stat->attributes |= STATX_ATTR_ENCRYPTED;
85 stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED;
87 generic_fillattr(mnt_userns, inode, stat);
89 stat->result_mask |= STATX_BTIME;
90 stat->btime = ni->i_crtime;
91 stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */
96 static int ntfs_extend_initialized_size(struct file *file,
97 struct ntfs_inode *ni,
99 const loff_t new_valid)
101 struct inode *inode = &ni->vfs_inode;
102 struct address_space *mapping = inode->i_mapping;
103 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
107 if (is_resident(ni)) {
108 ni->i_valid = new_valid;
112 WARN_ON(is_compressed(ni));
113 WARN_ON(valid >= new_valid);
121 if (is_sparsed(ni)) {
122 bits = sbi->cluster_bits;
125 err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL,
130 if (lcn == SPARSE_LCN) {
131 pos = ((loff_t)clen + vcn) << bits;
137 zerofrom = pos & (PAGE_SIZE - 1);
138 len = PAGE_SIZE - zerofrom;
140 if (pos + len > new_valid)
141 len = new_valid - pos;
143 err = ntfs_write_begin(file, mapping, pos, len, &page, NULL);
147 zero_user_segment(page, zerofrom, PAGE_SIZE);
149 /* This function in any case puts page. */
150 err = ntfs_write_end(file, mapping, pos, len, len, page, NULL);
156 if (pos >= new_valid)
159 balance_dirty_pages_ratelimited(mapping);
167 ntfs_inode_warn(inode, "failed to extend initialized size to %llx.",
173 * ntfs_zero_range - Helper function for punch_hole.
175 * It zeroes a range [vbo, vbo_to).
177 static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to)
180 struct address_space *mapping = inode->i_mapping;
181 u32 blocksize = 1 << inode->i_blkbits;
182 pgoff_t idx = vbo >> PAGE_SHIFT;
183 u32 from = vbo & (PAGE_SIZE - 1);
184 pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT;
186 struct buffer_head *head, *bh;
187 u32 bh_next, bh_off, to;
191 for (; idx < idx_end; idx += 1, from = 0) {
192 page_off = (loff_t)idx << PAGE_SHIFT;
193 to = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off)
195 iblock = page_off >> inode->i_blkbits;
197 page = find_or_create_page(mapping, idx,
198 mapping_gfp_constraint(mapping,
203 if (!page_has_buffers(page))
204 create_empty_buffers(page, blocksize, 0);
206 bh = head = page_buffers(page);
209 bh_next = bh_off + blocksize;
211 if (bh_next <= from || bh_off >= to)
214 if (!buffer_mapped(bh)) {
215 ntfs_get_block(inode, iblock, bh, 0);
216 /* Unmapped? It's a hole - nothing to do. */
217 if (!buffer_mapped(bh))
221 /* Ok, it's mapped. Make sure it's up-to-date. */
222 if (PageUptodate(page))
223 set_buffer_uptodate(bh);
225 if (!buffer_uptodate(bh)) {
227 bh->b_end_io = end_buffer_read_sync;
229 submit_bh(REQ_OP_READ, bh);
232 if (!buffer_uptodate(bh)) {
240 mark_buffer_dirty(bh);
242 } while (bh_off = bh_next, iblock += 1,
243 head != (bh = bh->b_this_page));
245 zero_user_segment(page, from, to);
252 mark_inode_dirty(inode);
257 * ntfs_file_mmap - file_operations::mmap
259 static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma)
261 struct address_space *mapping = file->f_mapping;
262 struct inode *inode = mapping->host;
263 struct ntfs_inode *ni = ntfs_i(inode);
264 u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT);
265 bool rw = vma->vm_flags & VM_WRITE;
268 if (is_encrypted(ni)) {
269 ntfs_inode_warn(inode, "mmap encrypted not supported");
274 ntfs_inode_warn(inode, "mmap deduplicated not supported");
278 if (is_compressed(ni) && rw) {
279 ntfs_inode_warn(inode, "mmap(write) compressed not supported");
284 u64 to = min_t(loff_t, i_size_read(inode),
285 from + vma->vm_end - vma->vm_start);
287 if (is_sparsed(ni)) {
288 /* Allocate clusters for rw map. */
289 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
291 CLST vcn = from >> sbi->cluster_bits;
292 CLST end = bytes_to_cluster(sbi, to);
295 for (; vcn < end; vcn += len) {
296 err = attr_data_get_block(ni, vcn, 1, &lcn,
303 if (ni->i_valid < to) {
304 if (!inode_trylock(inode)) {
308 err = ntfs_extend_initialized_size(file, ni,
316 err = generic_file_mmap(file, vma);
321 static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
324 struct ntfs_inode *ni = ntfs_i(inode);
325 struct address_space *mapping = inode->i_mapping;
326 loff_t end = pos + count;
327 bool extend_init = file && pos > ni->i_valid;
330 if (end <= inode->i_size && !extend_init)
333 /* Mark rw ntfs as dirty. It will be cleared at umount. */
334 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
336 if (end > inode->i_size) {
337 err = ntfs_set_size(inode, end);
342 if (extend_init && !is_compressed(ni)) {
343 err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
350 inode->i_ctime = inode->i_mtime = current_time(inode);
351 mark_inode_dirty(inode);
353 if (IS_SYNC(inode)) {
356 err = filemap_fdatawrite_range(mapping, pos, end - 1);
357 err2 = sync_mapping_buffers(mapping);
360 err2 = write_inode_now(inode, 1);
364 err = filemap_fdatawait_range(mapping, pos, end - 1);
371 static int ntfs_truncate(struct inode *inode, loff_t new_size)
373 struct super_block *sb = inode->i_sb;
374 struct ntfs_inode *ni = ntfs_i(inode);
378 if (!S_ISREG(inode->i_mode))
381 if (is_compressed(ni)) {
382 if (ni->i_valid > new_size)
383 ni->i_valid = new_size;
385 err = block_truncate_page(inode->i_mapping, new_size,
391 new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
393 truncate_setsize(inode, new_size);
397 down_write(&ni->file.run_lock);
398 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
399 &new_valid, ni->mi.sbi->options->prealloc, NULL);
400 up_write(&ni->file.run_lock);
402 if (new_valid < ni->i_valid)
403 ni->i_valid = new_valid;
407 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
408 inode->i_ctime = inode->i_mtime = current_time(inode);
409 if (!IS_DIRSYNC(inode)) {
412 err = ntfs_sync_inode(inode);
418 mark_inode_dirty(inode);
420 /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
428 * Preallocate space for a file. This implements ntfs's fallocate file
429 * operation, which gets called from sys_fallocate system call. User
430 * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
431 * we just allocate clusters without zeroing them out. Otherwise we
432 * allocate and zero out clusters via an expanding truncate.
434 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
436 struct inode *inode = file->f_mapping->host;
437 struct address_space *mapping = inode->i_mapping;
438 struct super_block *sb = inode->i_sb;
439 struct ntfs_sb_info *sbi = sb->s_fs_info;
440 struct ntfs_inode *ni = ntfs_i(inode);
441 loff_t end = vbo + len;
442 loff_t vbo_down = round_down(vbo, max_t(unsigned long,
443 sbi->cluster_size, PAGE_SIZE));
444 bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);
445 loff_t i_size, new_size;
449 /* No support for dir. */
450 if (!S_ISREG(inode->i_mode))
454 * vfs_fallocate checks all possible combinations of mode.
455 * Do additional checks here before ntfs_set_state(dirty).
457 if (mode & FALLOC_FL_PUNCH_HOLE) {
458 if (!is_supported_holes)
460 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
461 } else if (mode & FALLOC_FL_INSERT_RANGE) {
462 if (!is_supported_holes)
465 ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
466 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) {
467 ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
472 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
475 i_size = inode->i_size;
476 new_size = max(end, i_size);
479 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
480 /* Should never be here, see ntfs_file_open. */
485 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
486 FALLOC_FL_INSERT_RANGE)) {
487 inode_dio_wait(inode);
488 filemap_invalidate_lock(mapping);
492 if (mode & FALLOC_FL_PUNCH_HOLE) {
494 loff_t mask, vbo_a, end_a, tmp;
496 err = filemap_write_and_wait_range(mapping, vbo_down,
501 truncate_pagecache(inode, vbo_down);
504 err = attr_punch_hole(ni, vbo, len, &frame_size);
506 if (err != E_NTFS_NOTALIGNED)
509 /* Process not aligned punch. */
510 mask = frame_size - 1;
511 vbo_a = (vbo + mask) & ~mask;
514 tmp = min(vbo_a, end);
516 err = ntfs_zero_range(inode, vbo, tmp);
521 if (vbo < end_a && end_a < end) {
522 err = ntfs_zero_range(inode, end_a, end);
527 /* Aligned punch_hole */
530 err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
533 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
535 * Write tail of the last page before removed range since
536 * it will get removed from the page cache below.
538 err = filemap_write_and_wait_range(mapping, vbo_down, vbo);
543 * Write data that will be shifted to preserve them
544 * when discarding page cache below.
546 err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
550 truncate_pagecache(inode, vbo_down);
553 err = attr_collapse_range(ni, vbo, len);
555 } else if (mode & FALLOC_FL_INSERT_RANGE) {
556 /* Check new size. */
557 err = inode_newsize_ok(inode, new_size);
561 /* Write out all dirty pages. */
562 err = filemap_write_and_wait_range(mapping, vbo_down,
566 truncate_pagecache(inode, vbo_down);
569 err = attr_insert_range(ni, vbo, len);
572 /* Check new size. */
574 /* generic/213: expected -ENOSPC instead of -EFBIG. */
575 if (!is_supported_holes) {
576 loff_t to_alloc = new_size - inode_get_bytes(inode);
579 (to_alloc >> sbi->cluster_bits) >
580 wnd_zeroes(&sbi->used.bitmap)) {
586 err = inode_newsize_ok(inode, new_size);
590 if (new_size > i_size) {
592 * Allocate clusters, do not change 'valid' size.
594 err = ntfs_set_size(inode, new_size);
599 if (is_supported_holes) {
600 CLST vcn = vbo >> sbi->cluster_bits;
601 CLST cend = bytes_to_cluster(sbi, end);
602 CLST cend_v = bytes_to_cluster(sbi, ni->i_valid);
610 * Allocate and zero new clusters.
611 * Zeroing these clusters may be too long.
613 for (; vcn < cend_v; vcn += clen) {
614 err = attr_data_get_block(ni, vcn, cend_v - vcn,
621 * Allocate but not zero new clusters.
623 for (; vcn < cend; vcn += clen) {
624 err = attr_data_get_block(ni, vcn, cend - vcn,
632 if (mode & FALLOC_FL_KEEP_SIZE) {
634 /* True - Keep preallocated. */
635 err = attr_set_size(ni, ATTR_DATA, NULL, 0,
636 &ni->file.run, i_size, &ni->i_valid,
639 } else if (new_size > i_size) {
640 inode->i_size = new_size;
646 filemap_invalidate_unlock(mapping);
649 inode->i_ctime = inode->i_mtime = current_time(inode);
650 mark_inode_dirty(inode);
658 * ntfs3_setattr - inode_operations::setattr
660 int ntfs3_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
663 struct super_block *sb = dentry->d_sb;
664 struct ntfs_sb_info *sbi = sb->s_fs_info;
665 struct inode *inode = d_inode(dentry);
666 struct ntfs_inode *ni = ntfs_i(inode);
667 u32 ia_valid = attr->ia_valid;
668 umode_t mode = inode->i_mode;
671 if (sbi->options->noacsrules) {
672 /* "No access rules" - Force any changes of time etc. */
673 attr->ia_valid |= ATTR_FORCE;
674 /* and disable for editing some attributes. */
675 attr->ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
676 ia_valid = attr->ia_valid;
679 err = setattr_prepare(mnt_userns, dentry, attr);
683 if (ia_valid & ATTR_SIZE) {
684 loff_t newsize, oldsize;
686 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
687 /* Should never be here, see ntfs_file_open(). */
691 inode_dio_wait(inode);
692 oldsize = inode->i_size;
693 newsize = attr->ia_size;
695 if (newsize <= oldsize)
696 err = ntfs_truncate(inode, newsize);
698 err = ntfs_extend(inode, newsize, 0, NULL);
703 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
704 inode->i_size = newsize;
707 setattr_copy(mnt_userns, inode, attr);
709 if (mode != inode->i_mode) {
710 err = ntfs_acl_chmod(mnt_userns, dentry);
714 /* Linux 'w' -> Windows 'ro'. */
715 if (0222 & inode->i_mode)
716 ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
718 ni->std_fa |= FILE_ATTRIBUTE_READONLY;
721 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
722 ntfs_save_wsl_perm(inode);
723 mark_inode_dirty(inode);
728 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
730 struct file *file = iocb->ki_filp;
731 struct inode *inode = file->f_mapping->host;
732 struct ntfs_inode *ni = ntfs_i(inode);
734 if (is_encrypted(ni)) {
735 ntfs_inode_warn(inode, "encrypted i/o not supported");
739 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
740 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
744 #ifndef CONFIG_NTFS3_LZX_XPRESS
745 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
748 "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
754 ntfs_inode_warn(inode, "read deduplicated not supported");
758 return generic_file_read_iter(iocb, iter);
762 * ntfs_get_frame_pages
764 * Return: Array of locked pages.
766 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
767 struct page **pages, u32 pages_per_frame,
768 bool *frame_uptodate)
770 gfp_t gfp_mask = mapping_gfp_mask(mapping);
773 *frame_uptodate = true;
775 for (npages = 0; npages < pages_per_frame; npages++, index++) {
778 page = find_or_create_page(mapping, index, gfp_mask);
781 page = pages[npages];
789 if (!PageUptodate(page))
790 *frame_uptodate = false;
792 pages[npages] = page;
799 * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
801 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
804 struct file *file = iocb->ki_filp;
805 size_t count = iov_iter_count(from);
806 loff_t pos = iocb->ki_pos;
807 struct inode *inode = file_inode(file);
808 loff_t i_size = inode->i_size;
809 struct address_space *mapping = inode->i_mapping;
810 struct ntfs_inode *ni = ntfs_i(inode);
811 u64 valid = ni->i_valid;
812 struct ntfs_sb_info *sbi = ni->mi.sbi;
813 struct page *page, **pages = NULL;
815 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
816 u32 frame_size = 1u << frame_bits;
817 u32 pages_per_frame = frame_size >> PAGE_SHIFT;
824 if (frame_size < PAGE_SIZE) {
826 * frame_size == 8K if cluster 512
827 * frame_size == 64K if cluster 4096
829 ntfs_inode_warn(inode, "page size is bigger than frame size");
833 pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
837 current->backing_dev_info = inode_to_bdi(inode);
838 err = file_remove_privs(file);
842 err = file_update_time(file);
846 /* Zero range [valid : pos). */
847 while (valid < pos) {
850 frame = valid >> frame_bits;
851 frame_vbo = valid & ~(frame_size - 1);
852 off = valid & (frame_size - 1);
854 err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn,
859 if (lcn == SPARSE_LCN) {
860 ni->i_valid = valid =
861 frame_vbo + ((u64)clen << sbi->cluster_bits);
865 /* Load full frame. */
866 err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
867 pages, pages_per_frame,
872 if (!frame_uptodate && off) {
873 err = ni_read_frame(ni, frame_vbo, pages,
876 for (ip = 0; ip < pages_per_frame; ip++) {
885 ip = off >> PAGE_SHIFT;
886 off = offset_in_page(valid);
887 for (; ip < pages_per_frame; ip++, off = 0) {
889 zero_user_segment(page, off, PAGE_SIZE);
890 flush_dcache_page(page);
891 SetPageUptodate(page);
895 err = ni_write_frame(ni, pages, pages_per_frame);
898 for (ip = 0; ip < pages_per_frame; ip++) {
900 SetPageUptodate(page);
908 ni->i_valid = valid = frame_vbo + frame_size;
911 /* Copy user data [pos : pos + count). */
913 size_t copied, bytes;
915 off = pos & (frame_size - 1);
916 bytes = frame_size - off;
920 frame_vbo = pos & ~(frame_size - 1);
921 index = frame_vbo >> PAGE_SHIFT;
923 if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
928 /* Load full frame. */
929 err = ntfs_get_frame_pages(mapping, index, pages,
930 pages_per_frame, &frame_uptodate);
934 if (!frame_uptodate) {
935 loff_t to = pos + bytes;
937 if (off || (to < i_size && (to & (frame_size - 1)))) {
938 err = ni_read_frame(ni, frame_vbo, pages,
941 for (ip = 0; ip < pages_per_frame;
954 ip = off >> PAGE_SHIFT;
955 off = offset_in_page(pos);
957 /* Copy user data to pages. */
959 size_t cp, tail = PAGE_SIZE - off;
962 cp = copy_page_from_iter_atomic(page, off,
963 min(tail, bytes), from);
964 flush_dcache_page(page);
980 err = ni_write_frame(ni, pages, pages_per_frame);
983 for (ip = 0; ip < pages_per_frame; ip++) {
985 ClearPageDirty(page);
986 SetPageUptodate(page);
995 * We can loop for a long time in here. Be nice and allow
996 * us to schedule out to avoid softlocking if preempt
1004 count = iov_iter_count(from);
1010 current->backing_dev_info = NULL;
1015 iocb->ki_pos += written;
1016 if (iocb->ki_pos > ni->i_valid)
1017 ni->i_valid = iocb->ki_pos;
1023 * ntfs_file_write_iter - file_operations::write_iter
1025 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1027 struct file *file = iocb->ki_filp;
1028 struct address_space *mapping = file->f_mapping;
1029 struct inode *inode = mapping->host;
1031 struct ntfs_inode *ni = ntfs_i(inode);
1033 if (is_encrypted(ni)) {
1034 ntfs_inode_warn(inode, "encrypted i/o not supported");
1038 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
1039 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
1044 ntfs_inode_warn(inode, "write into deduplicated not supported");
1048 if (!inode_trylock(inode)) {
1049 if (iocb->ki_flags & IOCB_NOWAIT)
1054 ret = generic_write_checks(iocb, from);
1058 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
1059 /* Should never be here, see ntfs_file_open(). */
1064 ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
1068 ret = is_compressed(ni) ? ntfs_compress_write(iocb, from)
1069 : __generic_file_write_iter(iocb, from);
1072 inode_unlock(inode);
1075 ret = generic_write_sync(iocb, ret);
1081 * ntfs_file_open - file_operations::open
1083 int ntfs_file_open(struct inode *inode, struct file *file)
1085 struct ntfs_inode *ni = ntfs_i(inode);
1087 if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
1088 (file->f_flags & O_DIRECT))) {
1092 /* Decompress "external compressed" file if opened for rw. */
1093 if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
1094 (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
1095 #ifdef CONFIG_NTFS3_LZX_XPRESS
1096 int err = ni_decompress_file(ni);
1103 "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
1108 return generic_file_open(inode, file);
1112 * ntfs_file_release - file_operations::release
1114 static int ntfs_file_release(struct inode *inode, struct file *file)
1116 struct ntfs_inode *ni = ntfs_i(inode);
1117 struct ntfs_sb_info *sbi = ni->mi.sbi;
1120 /* If we are last writer on the inode, drop the block reservation. */
1121 if (sbi->options->prealloc && ((file->f_mode & FMODE_WRITE) &&
1122 atomic_read(&inode->i_writecount) == 1)) {
1124 down_write(&ni->file.run_lock);
1126 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
1127 inode->i_size, &ni->i_valid, false, NULL);
1129 up_write(&ni->file.run_lock);
1136 * ntfs_fiemap - file_operations::fiemap
1138 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1139 __u64 start, __u64 len)
1142 struct ntfs_inode *ni = ntfs_i(inode);
1144 err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
1150 err = ni_fiemap(ni, fieinfo, start, len);
1158 const struct inode_operations ntfs_file_inode_operations = {
1159 .getattr = ntfs_getattr,
1160 .setattr = ntfs3_setattr,
1161 .listxattr = ntfs_listxattr,
1162 .permission = ntfs_permission,
1163 .get_inode_acl = ntfs_get_acl,
1164 .set_acl = ntfs_set_acl,
1165 .fiemap = ntfs_fiemap,
1168 const struct file_operations ntfs_file_operations = {
1169 .llseek = generic_file_llseek,
1170 .read_iter = ntfs_file_read_iter,
1171 .write_iter = ntfs_file_write_iter,
1172 .unlocked_ioctl = ntfs_ioctl,
1173 #ifdef CONFIG_COMPAT
1174 .compat_ioctl = ntfs_compat_ioctl,
1176 .splice_read = generic_file_splice_read,
1177 .mmap = ntfs_file_mmap,
1178 .open = ntfs_file_open,
1179 .fsync = generic_file_fsync,
1180 .splice_write = iter_file_splice_write,
1181 .fallocate = ntfs_fallocate,
1182 .release = ntfs_file_release,