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 static 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 static 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, 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 = 1 << inode->i_blkbits;
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;
192 for (; idx < idx_end; idx += 1, from = 0) {
193 page_off = (loff_t)idx << PAGE_SHIFT;
194 to = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off) :
196 iblock = page_off >> inode->i_blkbits;
198 page = find_or_create_page(mapping, idx,
199 mapping_gfp_constraint(mapping,
204 if (!page_has_buffers(page))
205 create_empty_buffers(page, blocksize, 0);
207 bh = head = page_buffers(page);
210 bh_next = bh_off + blocksize;
212 if (bh_next <= from || bh_off >= to)
215 if (!buffer_mapped(bh)) {
216 ntfs_get_block(inode, iblock, bh, 0);
217 /* Unmapped? It's a hole - nothing to do. */
218 if (!buffer_mapped(bh))
222 /* Ok, it's mapped. Make sure it's up-to-date. */
223 if (PageUptodate(page))
224 set_buffer_uptodate(bh);
226 if (!buffer_uptodate(bh)) {
227 err = bh_read(bh, 0);
235 mark_buffer_dirty(bh);
237 } while (bh_off = bh_next, iblock += 1,
238 head != (bh = bh->b_this_page));
240 zero_user_segment(page, 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 (is_encrypted(ni)) {
264 ntfs_inode_warn(inode, "mmap encrypted not supported");
269 ntfs_inode_warn(inode, "mmap deduplicated not supported");
273 if (is_compressed(ni) && rw) {
274 ntfs_inode_warn(inode, "mmap(write) compressed not supported");
279 u64 to = min_t(loff_t, i_size_read(inode),
280 from + vma->vm_end - vma->vm_start);
282 if (is_sparsed(ni)) {
283 /* Allocate clusters for rw map. */
284 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
286 CLST vcn = from >> sbi->cluster_bits;
287 CLST end = bytes_to_cluster(sbi, to);
290 for (; vcn < end; vcn += len) {
291 err = attr_data_get_block(ni, vcn, 1, &lcn,
298 if (ni->i_valid < to) {
299 if (!inode_trylock(inode)) {
303 err = ntfs_extend_initialized_size(file, ni,
311 err = generic_file_mmap(file, vma);
316 static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
319 struct ntfs_inode *ni = ntfs_i(inode);
320 struct address_space *mapping = inode->i_mapping;
321 loff_t end = pos + count;
322 bool extend_init = file && pos > ni->i_valid;
325 if (end <= inode->i_size && !extend_init)
328 /* Mark rw ntfs as dirty. It will be cleared at umount. */
329 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
331 if (end > inode->i_size) {
332 err = ntfs_set_size(inode, end);
337 if (extend_init && !is_compressed(ni)) {
338 err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
345 inode->i_ctime = inode->i_mtime = current_time(inode);
346 mark_inode_dirty(inode);
348 if (IS_SYNC(inode)) {
351 err = filemap_fdatawrite_range(mapping, pos, end - 1);
352 err2 = sync_mapping_buffers(mapping);
355 err2 = write_inode_now(inode, 1);
359 err = filemap_fdatawait_range(mapping, pos, end - 1);
366 static int ntfs_truncate(struct inode *inode, loff_t new_size)
368 struct super_block *sb = inode->i_sb;
369 struct ntfs_inode *ni = ntfs_i(inode);
373 if (!S_ISREG(inode->i_mode))
376 if (is_compressed(ni)) {
377 if (ni->i_valid > new_size)
378 ni->i_valid = new_size;
380 err = block_truncate_page(inode->i_mapping, new_size,
386 new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
388 truncate_setsize(inode, new_size);
392 down_write(&ni->file.run_lock);
393 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
394 &new_valid, ni->mi.sbi->options->prealloc, NULL);
395 up_write(&ni->file.run_lock);
397 if (new_valid < ni->i_valid)
398 ni->i_valid = new_valid;
402 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
403 inode->i_ctime = inode->i_mtime = current_time(inode);
404 if (!IS_DIRSYNC(inode)) {
407 err = ntfs_sync_inode(inode);
413 mark_inode_dirty(inode);
415 /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
423 * Preallocate space for a file. This implements ntfs's fallocate file
424 * operation, which gets called from sys_fallocate system call. User
425 * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
426 * we just allocate clusters without zeroing them out. Otherwise we
427 * allocate and zero out clusters via an expanding truncate.
429 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
431 struct inode *inode = file->f_mapping->host;
432 struct address_space *mapping = inode->i_mapping;
433 struct super_block *sb = inode->i_sb;
434 struct ntfs_sb_info *sbi = sb->s_fs_info;
435 struct ntfs_inode *ni = ntfs_i(inode);
436 loff_t end = vbo + len;
437 loff_t vbo_down = round_down(vbo, max_t(unsigned long,
438 sbi->cluster_size, PAGE_SIZE));
439 bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);
440 loff_t i_size, new_size;
444 /* No support for dir. */
445 if (!S_ISREG(inode->i_mode))
449 * vfs_fallocate checks all possible combinations of mode.
450 * Do additional checks here before ntfs_set_state(dirty).
452 if (mode & FALLOC_FL_PUNCH_HOLE) {
453 if (!is_supported_holes)
455 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
456 } else if (mode & FALLOC_FL_INSERT_RANGE) {
457 if (!is_supported_holes)
460 ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
461 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) {
462 ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
467 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
470 i_size = inode->i_size;
471 new_size = max(end, i_size);
474 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
475 /* Should never be here, see ntfs_file_open. */
480 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
481 FALLOC_FL_INSERT_RANGE)) {
482 inode_dio_wait(inode);
483 filemap_invalidate_lock(mapping);
487 if (mode & FALLOC_FL_PUNCH_HOLE) {
489 loff_t mask, vbo_a, end_a, tmp;
491 err = filemap_write_and_wait_range(mapping, vbo_down,
496 truncate_pagecache(inode, vbo_down);
499 err = attr_punch_hole(ni, vbo, len, &frame_size);
501 if (err != E_NTFS_NOTALIGNED)
504 /* Process not aligned punch. */
505 mask = frame_size - 1;
506 vbo_a = (vbo + mask) & ~mask;
509 tmp = min(vbo_a, end);
511 err = ntfs_zero_range(inode, vbo, tmp);
516 if (vbo < end_a && end_a < end) {
517 err = ntfs_zero_range(inode, end_a, end);
522 /* Aligned punch_hole */
525 err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
528 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
530 * Write tail of the last page before removed range since
531 * it will get removed from the page cache below.
533 err = filemap_write_and_wait_range(mapping, vbo_down, vbo);
538 * Write data that will be shifted to preserve them
539 * when discarding page cache below.
541 err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
545 truncate_pagecache(inode, vbo_down);
548 err = attr_collapse_range(ni, vbo, len);
550 } else if (mode & FALLOC_FL_INSERT_RANGE) {
551 /* Check new size. */
552 err = inode_newsize_ok(inode, new_size);
556 /* Write out all dirty pages. */
557 err = filemap_write_and_wait_range(mapping, vbo_down,
561 truncate_pagecache(inode, vbo_down);
564 err = attr_insert_range(ni, vbo, len);
567 /* Check new size. */
568 u8 cluster_bits = sbi->cluster_bits;
570 /* generic/213: expected -ENOSPC instead of -EFBIG. */
571 if (!is_supported_holes) {
572 loff_t to_alloc = new_size - inode_get_bytes(inode);
575 (to_alloc >> cluster_bits) >
576 wnd_zeroes(&sbi->used.bitmap)) {
582 err = inode_newsize_ok(inode, new_size);
586 if (new_size > i_size) {
588 * Allocate clusters, do not change 'valid' size.
590 err = ntfs_set_size(inode, new_size);
595 if (is_supported_holes) {
596 CLST vcn = vbo >> cluster_bits;
597 CLST cend = bytes_to_cluster(sbi, end);
598 CLST cend_v = bytes_to_cluster(sbi, ni->i_valid);
606 * Allocate and zero new clusters.
607 * Zeroing these clusters may be too long.
609 for (; vcn < cend_v; vcn += clen) {
610 err = attr_data_get_block(ni, vcn, cend_v - vcn,
617 * Allocate but not zero new clusters.
619 for (; vcn < cend; vcn += clen) {
620 err = attr_data_get_block(ni, vcn, cend - vcn,
628 if (mode & FALLOC_FL_KEEP_SIZE) {
630 /* True - Keep preallocated. */
631 err = attr_set_size(ni, ATTR_DATA, NULL, 0,
632 &ni->file.run, i_size, &ni->i_valid,
635 } else if (new_size > i_size) {
636 inode->i_size = new_size;
642 filemap_invalidate_unlock(mapping);
645 inode->i_ctime = inode->i_mtime = current_time(inode);
646 mark_inode_dirty(inode);
654 * ntfs3_setattr - inode_operations::setattr
656 int ntfs3_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
659 struct inode *inode = d_inode(dentry);
660 struct ntfs_inode *ni = ntfs_i(inode);
661 u32 ia_valid = attr->ia_valid;
662 umode_t mode = inode->i_mode;
665 err = setattr_prepare(idmap, dentry, attr);
669 if (ia_valid & ATTR_SIZE) {
670 loff_t newsize, oldsize;
672 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
673 /* Should never be here, see ntfs_file_open(). */
677 inode_dio_wait(inode);
678 oldsize = inode->i_size;
679 newsize = attr->ia_size;
681 if (newsize <= oldsize)
682 err = ntfs_truncate(inode, newsize);
684 err = ntfs_extend(inode, newsize, 0, NULL);
689 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
690 inode->i_size = newsize;
693 setattr_copy(idmap, inode, attr);
695 if (mode != inode->i_mode) {
696 err = ntfs_acl_chmod(idmap, dentry);
700 /* Linux 'w' -> Windows 'ro'. */
701 if (0222 & inode->i_mode)
702 ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
704 ni->std_fa |= FILE_ATTRIBUTE_READONLY;
707 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
708 ntfs_save_wsl_perm(inode, NULL);
709 mark_inode_dirty(inode);
714 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
716 struct file *file = iocb->ki_filp;
717 struct inode *inode = file->f_mapping->host;
718 struct ntfs_inode *ni = ntfs_i(inode);
720 if (is_encrypted(ni)) {
721 ntfs_inode_warn(inode, "encrypted i/o not supported");
725 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
726 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
730 #ifndef CONFIG_NTFS3_LZX_XPRESS
731 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
734 "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
740 ntfs_inode_warn(inode, "read deduplicated not supported");
744 return generic_file_read_iter(iocb, iter);
747 static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
748 struct pipe_inode_info *pipe,
749 size_t len, unsigned int flags)
751 struct inode *inode = in->f_mapping->host;
752 struct ntfs_inode *ni = ntfs_i(inode);
754 if (is_encrypted(ni)) {
755 ntfs_inode_warn(inode, "encrypted i/o not supported");
759 #ifndef CONFIG_NTFS3_LZX_XPRESS
760 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
763 "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
769 ntfs_inode_warn(inode, "read deduplicated not supported");
773 return filemap_splice_read(in, ppos, pipe, len, flags);
777 * ntfs_get_frame_pages
779 * Return: Array of locked pages.
781 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
782 struct page **pages, u32 pages_per_frame,
783 bool *frame_uptodate)
785 gfp_t gfp_mask = mapping_gfp_mask(mapping);
788 *frame_uptodate = true;
790 for (npages = 0; npages < pages_per_frame; npages++, index++) {
793 page = find_or_create_page(mapping, index, gfp_mask);
796 page = pages[npages];
804 if (!PageUptodate(page))
805 *frame_uptodate = false;
807 pages[npages] = page;
814 * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
816 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
819 struct file *file = iocb->ki_filp;
820 size_t count = iov_iter_count(from);
821 loff_t pos = iocb->ki_pos;
822 struct inode *inode = file_inode(file);
823 loff_t i_size = inode->i_size;
824 struct address_space *mapping = inode->i_mapping;
825 struct ntfs_inode *ni = ntfs_i(inode);
826 u64 valid = ni->i_valid;
827 struct ntfs_sb_info *sbi = ni->mi.sbi;
828 struct page *page, **pages = NULL;
830 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
831 u32 frame_size = 1u << frame_bits;
832 u32 pages_per_frame = frame_size >> PAGE_SHIFT;
839 if (frame_size < PAGE_SIZE) {
841 * frame_size == 8K if cluster 512
842 * frame_size == 64K if cluster 4096
844 ntfs_inode_warn(inode, "page size is bigger than frame size");
848 pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
852 err = file_remove_privs(file);
856 err = file_update_time(file);
860 /* Zero range [valid : pos). */
861 while (valid < pos) {
864 frame = valid >> frame_bits;
865 frame_vbo = valid & ~(frame_size - 1);
866 off = valid & (frame_size - 1);
868 err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn,
873 if (lcn == SPARSE_LCN) {
874 ni->i_valid = valid =
875 frame_vbo + ((u64)clen << sbi->cluster_bits);
879 /* Load full frame. */
880 err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
881 pages, pages_per_frame,
886 if (!frame_uptodate && off) {
887 err = ni_read_frame(ni, frame_vbo, pages,
890 for (ip = 0; ip < pages_per_frame; ip++) {
899 ip = off >> PAGE_SHIFT;
900 off = offset_in_page(valid);
901 for (; ip < pages_per_frame; ip++, off = 0) {
903 zero_user_segment(page, off, PAGE_SIZE);
904 flush_dcache_page(page);
905 SetPageUptodate(page);
909 err = ni_write_frame(ni, pages, pages_per_frame);
912 for (ip = 0; ip < pages_per_frame; ip++) {
914 SetPageUptodate(page);
922 ni->i_valid = valid = frame_vbo + frame_size;
925 /* Copy user data [pos : pos + count). */
927 size_t copied, bytes;
929 off = pos & (frame_size - 1);
930 bytes = frame_size - off;
934 frame_vbo = pos & ~(frame_size - 1);
935 index = frame_vbo >> PAGE_SHIFT;
937 if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
942 /* Load full frame. */
943 err = ntfs_get_frame_pages(mapping, index, pages,
944 pages_per_frame, &frame_uptodate);
948 if (!frame_uptodate) {
949 loff_t to = pos + bytes;
951 if (off || (to < i_size && (to & (frame_size - 1)))) {
952 err = ni_read_frame(ni, frame_vbo, pages,
955 for (ip = 0; ip < pages_per_frame;
968 ip = off >> PAGE_SHIFT;
969 off = offset_in_page(pos);
971 /* Copy user data to pages. */
973 size_t cp, tail = PAGE_SIZE - off;
976 cp = copy_page_from_iter_atomic(page, off,
977 min(tail, bytes), from);
978 flush_dcache_page(page);
994 err = ni_write_frame(ni, pages, pages_per_frame);
997 for (ip = 0; ip < pages_per_frame; ip++) {
999 ClearPageDirty(page);
1000 SetPageUptodate(page);
1009 * We can loop for a long time in here. Be nice and allow
1010 * us to schedule out to avoid softlocking if preempt
1018 count = iov_iter_count(from);
1027 iocb->ki_pos += written;
1028 if (iocb->ki_pos > ni->i_valid)
1029 ni->i_valid = iocb->ki_pos;
1035 * ntfs_file_write_iter - file_operations::write_iter
1037 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1039 struct file *file = iocb->ki_filp;
1040 struct address_space *mapping = file->f_mapping;
1041 struct inode *inode = mapping->host;
1043 struct ntfs_inode *ni = ntfs_i(inode);
1045 if (is_encrypted(ni)) {
1046 ntfs_inode_warn(inode, "encrypted i/o not supported");
1050 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
1051 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
1056 ntfs_inode_warn(inode, "write into deduplicated not supported");
1060 if (!inode_trylock(inode)) {
1061 if (iocb->ki_flags & IOCB_NOWAIT)
1066 ret = generic_write_checks(iocb, from);
1070 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
1071 /* Should never be here, see ntfs_file_open(). */
1076 ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
1080 ret = is_compressed(ni) ? ntfs_compress_write(iocb, from) :
1081 __generic_file_write_iter(iocb, from);
1084 inode_unlock(inode);
1087 ret = generic_write_sync(iocb, ret);
1093 * ntfs_file_open - file_operations::open
1095 int ntfs_file_open(struct inode *inode, struct file *file)
1097 struct ntfs_inode *ni = ntfs_i(inode);
1099 if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
1100 (file->f_flags & O_DIRECT))) {
1104 /* Decompress "external compressed" file if opened for rw. */
1105 if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
1106 (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
1107 #ifdef CONFIG_NTFS3_LZX_XPRESS
1108 int err = ni_decompress_file(ni);
1115 "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
1120 return generic_file_open(inode, file);
1124 * ntfs_file_release - file_operations::release
1126 static int ntfs_file_release(struct inode *inode, struct file *file)
1128 struct ntfs_inode *ni = ntfs_i(inode);
1129 struct ntfs_sb_info *sbi = ni->mi.sbi;
1132 /* If we are last writer on the inode, drop the block reservation. */
1133 if (sbi->options->prealloc &&
1134 ((file->f_mode & FMODE_WRITE) &&
1135 atomic_read(&inode->i_writecount) == 1)) {
1137 down_write(&ni->file.run_lock);
1139 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
1140 inode->i_size, &ni->i_valid, false, NULL);
1142 up_write(&ni->file.run_lock);
1149 * ntfs_fiemap - file_operations::fiemap
1151 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1152 __u64 start, __u64 len)
1155 struct ntfs_inode *ni = ntfs_i(inode);
1157 err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
1163 err = ni_fiemap(ni, fieinfo, start, len);
1171 const struct inode_operations ntfs_file_inode_operations = {
1172 .getattr = ntfs_getattr,
1173 .setattr = ntfs3_setattr,
1174 .listxattr = ntfs_listxattr,
1175 .get_acl = ntfs_get_acl,
1176 .set_acl = ntfs_set_acl,
1177 .fiemap = ntfs_fiemap,
1180 const struct file_operations ntfs_file_operations = {
1181 .llseek = generic_file_llseek,
1182 .read_iter = ntfs_file_read_iter,
1183 .write_iter = ntfs_file_write_iter,
1184 .unlocked_ioctl = ntfs_ioctl,
1185 #ifdef CONFIG_COMPAT
1186 .compat_ioctl = ntfs_compat_ioctl,
1188 .splice_read = ntfs_file_splice_read,
1189 .mmap = ntfs_file_mmap,
1190 .open = ntfs_file_open,
1191 .fsync = generic_file_fsync,
1192 .splice_write = iter_file_splice_write,
1193 .fallocate = ntfs_fallocate,
1194 .release = ntfs_file_release,