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>
15 #include <linux/nls.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 request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
28 if (!capable(CAP_SYS_ADMIN))
31 if (!blk_queue_discard(q))
34 user_range = (struct fstrim_range __user *)arg;
35 if (copy_from_user(&range, user_range, sizeof(range)))
38 range.minlen = max_t(u32, range.minlen, q->limits.discard_granularity);
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);
122 if (is_sparsed(ni)) {
123 bits = sbi->cluster_bits;
126 err = attr_data_get_block(ni, vcn, 0, &lcn, &clen,
131 if (lcn == SPARSE_LCN) {
132 loff_t vbo = (loff_t)vcn << bits;
133 loff_t to = vbo + ((loff_t)clen << bits);
135 if (to <= new_valid) {
144 to = (new_valid >> bits) << bits;
154 zerofrom = pos & (PAGE_SIZE - 1);
155 len = PAGE_SIZE - zerofrom;
157 if (pos + len > new_valid)
158 len = new_valid - pos;
160 err = pagecache_write_begin(file, mapping, pos, len, 0, &page,
165 zero_user_segment(page, zerofrom, PAGE_SIZE);
167 /* This function in any case puts page. */
168 err = pagecache_write_end(file, mapping, pos, len, len, page,
175 if (pos >= new_valid)
178 balance_dirty_pages_ratelimited(mapping);
186 ntfs_inode_warn(inode, "failed to extend initialized size to %llx.",
192 * ntfs_zero_range - Helper function for punch_hole.
194 * It zeroes a range [vbo, vbo_to).
196 static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to)
199 struct address_space *mapping = inode->i_mapping;
200 u32 blocksize = 1 << inode->i_blkbits;
201 pgoff_t idx = vbo >> PAGE_SHIFT;
202 u32 z_start = vbo & (PAGE_SIZE - 1);
203 pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT;
205 struct buffer_head *head, *bh;
206 u32 bh_next, bh_off, z_end;
210 for (; idx < idx_end; idx += 1, z_start = 0) {
211 page_off = (loff_t)idx << PAGE_SHIFT;
212 z_end = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off)
214 iblock = page_off >> inode->i_blkbits;
216 page = find_or_create_page(mapping, idx,
217 mapping_gfp_constraint(mapping,
222 if (!page_has_buffers(page))
223 create_empty_buffers(page, blocksize, 0);
225 bh = head = page_buffers(page);
228 bh_next = bh_off + blocksize;
230 if (bh_next <= z_start || bh_off >= z_end)
233 if (!buffer_mapped(bh)) {
234 ntfs_get_block(inode, iblock, bh, 0);
235 /* Unmapped? It's a hole - nothing to do. */
236 if (!buffer_mapped(bh))
240 /* Ok, it's mapped. Make sure it's up-to-date. */
241 if (PageUptodate(page))
242 set_buffer_uptodate(bh);
244 if (!buffer_uptodate(bh)) {
246 bh->b_end_io = end_buffer_read_sync;
248 submit_bh(REQ_OP_READ, 0, bh);
251 if (!buffer_uptodate(bh)) {
259 mark_buffer_dirty(bh);
261 } while (bh_off = bh_next, iblock += 1,
262 head != (bh = bh->b_this_page));
264 zero_user_segment(page, z_start, z_end);
271 mark_inode_dirty(inode);
276 * ntfs_sparse_cluster - Helper function to zero a new allocated clusters.
278 * NOTE: 512 <= cluster size <= 2M
280 void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn,
283 struct address_space *mapping = inode->i_mapping;
284 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
285 u64 vbo = (u64)vcn << sbi->cluster_bits;
286 u64 bytes = (u64)len << sbi->cluster_bits;
287 u32 blocksize = 1 << inode->i_blkbits;
288 pgoff_t idx0 = page0 ? page0->index : -1;
289 loff_t vbo_clst = vbo & sbi->cluster_mask_inv;
290 loff_t end = ntfs_up_cluster(sbi, vbo + bytes);
291 pgoff_t idx = vbo_clst >> PAGE_SHIFT;
292 u32 from = vbo_clst & (PAGE_SIZE - 1);
293 pgoff_t idx_end = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
299 for (; idx < idx_end; idx += 1, from = 0) {
300 page = idx == idx0 ? page0 : grab_cache_page(mapping, idx);
305 page_off = (loff_t)idx << PAGE_SHIFT;
306 to = (page_off + PAGE_SIZE) > end ? (end - page_off)
310 if ((from || PAGE_SIZE != to) &&
311 likely(!page_has_buffers(page))) {
312 create_empty_buffers(page, blocksize, 0);
315 if (page_has_buffers(page)) {
316 struct buffer_head *head, *bh;
319 bh = head = page_buffers(page);
321 u32 bh_next = bh_off + blocksize;
323 if (from <= bh_off && bh_next <= to) {
324 set_buffer_uptodate(bh);
325 mark_buffer_dirty(bh);
326 } else if (!buffer_uptodate(bh)) {
330 } while (head != (bh = bh->b_this_page));
333 zero_user_segment(page, from, to);
336 if (!PageUptodate(page))
337 SetPageUptodate(page);
338 set_page_dirty(page);
347 mark_inode_dirty(inode);
351 * ntfs_file_mmap - file_operations::mmap
353 static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma)
355 struct address_space *mapping = file->f_mapping;
356 struct inode *inode = mapping->host;
357 struct ntfs_inode *ni = ntfs_i(inode);
358 u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT);
359 bool rw = vma->vm_flags & VM_WRITE;
362 if (is_encrypted(ni)) {
363 ntfs_inode_warn(inode, "mmap encrypted not supported");
368 ntfs_inode_warn(inode, "mmap deduplicated not supported");
372 if (is_compressed(ni) && rw) {
373 ntfs_inode_warn(inode, "mmap(write) compressed not supported");
378 u64 to = min_t(loff_t, i_size_read(inode),
379 from + vma->vm_end - vma->vm_start);
381 if (is_sparsed(ni)) {
382 /* Allocate clusters for rw map. */
383 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
385 CLST vcn = from >> sbi->cluster_bits;
386 CLST end = bytes_to_cluster(sbi, to);
389 for (; vcn < end; vcn += len) {
390 err = attr_data_get_block(ni, vcn, 1, &lcn,
397 ntfs_sparse_cluster(inode, NULL, vcn, 1);
401 if (ni->i_valid < to) {
402 if (!inode_trylock(inode)) {
406 err = ntfs_extend_initialized_size(file, ni,
414 err = generic_file_mmap(file, vma);
419 static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
422 struct ntfs_inode *ni = ntfs_i(inode);
423 struct address_space *mapping = inode->i_mapping;
424 loff_t end = pos + count;
425 bool extend_init = file && pos > ni->i_valid;
428 if (end <= inode->i_size && !extend_init)
431 /* Mark rw ntfs as dirty. It will be cleared at umount. */
432 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
434 if (end > inode->i_size) {
435 err = ntfs_set_size(inode, end);
441 if (extend_init && !is_compressed(ni)) {
442 err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
449 inode->i_ctime = inode->i_mtime = current_time(inode);
450 mark_inode_dirty(inode);
452 if (IS_SYNC(inode)) {
455 err = filemap_fdatawrite_range(mapping, pos, end - 1);
456 err2 = sync_mapping_buffers(mapping);
459 err2 = write_inode_now(inode, 1);
463 err = filemap_fdatawait_range(mapping, pos, end - 1);
470 static int ntfs_truncate(struct inode *inode, loff_t new_size)
472 struct super_block *sb = inode->i_sb;
473 struct ntfs_inode *ni = ntfs_i(inode);
477 if (!S_ISREG(inode->i_mode))
480 if (is_compressed(ni)) {
481 if (ni->i_valid > new_size)
482 ni->i_valid = new_size;
484 err = block_truncate_page(inode->i_mapping, new_size,
490 new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
494 truncate_setsize(inode, new_size);
496 down_write(&ni->file.run_lock);
497 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
498 &new_valid, true, NULL);
499 up_write(&ni->file.run_lock);
501 if (new_valid < ni->i_valid)
502 ni->i_valid = new_valid;
506 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
507 inode->i_ctime = inode->i_mtime = current_time(inode);
508 if (!IS_DIRSYNC(inode)) {
511 err = ntfs_sync_inode(inode);
517 mark_inode_dirty(inode);
519 /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
527 * Preallocate space for a file. This implements ntfs's fallocate file
528 * operation, which gets called from sys_fallocate system call. User
529 * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
530 * we just allocate clusters without zeroing them out. Otherwise we
531 * allocate and zero out clusters via an expanding truncate.
533 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
535 struct inode *inode = file->f_mapping->host;
536 struct super_block *sb = inode->i_sb;
537 struct ntfs_sb_info *sbi = sb->s_fs_info;
538 struct ntfs_inode *ni = ntfs_i(inode);
539 loff_t end = vbo + len;
540 loff_t vbo_down = round_down(vbo, PAGE_SIZE);
544 /* No support for dir. */
545 if (!S_ISREG(inode->i_mode))
548 /* Return error if mode is not supported. */
549 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
550 FALLOC_FL_COLLAPSE_RANGE)) {
551 ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
556 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
559 i_size = inode->i_size;
561 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
562 /* Should never be here, see ntfs_file_open. */
567 if (mode & FALLOC_FL_PUNCH_HOLE) {
569 loff_t mask, vbo_a, end_a, tmp;
571 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
576 err = filemap_write_and_wait_range(inode->i_mapping, vbo,
581 err = filemap_write_and_wait_range(inode->i_mapping, end,
586 inode_dio_wait(inode);
588 truncate_pagecache(inode, vbo_down);
590 if (!is_sparsed(ni) && !is_compressed(ni)) {
592 err = ntfs_zero_range(inode, vbo, end);
597 err = attr_punch_hole(ni, vbo, len, &frame_size);
599 if (err != E_NTFS_NOTALIGNED)
602 /* Process not aligned punch. */
603 mask = frame_size - 1;
604 vbo_a = (vbo + mask) & ~mask;
607 tmp = min(vbo_a, end);
609 err = ntfs_zero_range(inode, vbo, tmp);
614 if (vbo < end_a && end_a < end) {
615 err = ntfs_zero_range(inode, end_a, end);
620 /* Aligned punch_hole */
623 err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
626 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
627 if (mode & ~FALLOC_FL_COLLAPSE_RANGE) {
633 * Write tail of the last page before removed range since
634 * it will get removed from the page cache below.
636 err = filemap_write_and_wait_range(inode->i_mapping, vbo_down,
642 * Write data that will be shifted to preserve them
643 * when discarding page cache below.
645 err = filemap_write_and_wait_range(inode->i_mapping, end,
650 /* Wait for existing dio to complete. */
651 inode_dio_wait(inode);
653 truncate_pagecache(inode, vbo_down);
656 err = attr_collapse_range(ni, vbo, len);
660 * Normal file: Allocate clusters, do not change 'valid' size.
662 err = ntfs_set_size(inode, max(end, i_size));
666 if (is_sparsed(ni) || is_compressed(ni)) {
667 CLST vcn_v = ni->i_valid >> sbi->cluster_bits;
668 CLST vcn = vbo >> sbi->cluster_bits;
669 CLST cend = bytes_to_cluster(sbi, end);
674 * Allocate but do not zero new clusters. (see below comments)
675 * This breaks security: One can read unused on-disk areas.
676 * Zeroing these clusters may be too long.
677 * Maybe we should check here for root rights?
679 for (; vcn < cend; vcn += clen) {
680 err = attr_data_get_block(ni, vcn, cend - vcn,
684 if (!new || vcn >= vcn_v)
689 * NTFS is not able to store several unwritten areas.
690 * Activate 'ntfs_sparse_cluster' to zero new allocated clusters.
693 * 1G of sparsed clusters + 1 cluster of data =>
694 * valid_size == 1G + 1 cluster
695 * fallocate(1G) will zero 1G and this can be very long
696 * xfstest 016/086 will fail without 'ntfs_sparse_cluster'.
698 ntfs_sparse_cluster(inode, NULL, vcn,
699 min(vcn_v - vcn, clen));
703 if (mode & FALLOC_FL_KEEP_SIZE) {
705 /* True - Keep preallocated. */
706 err = attr_set_size(ni, ATTR_DATA, NULL, 0,
707 &ni->file.run, i_size, &ni->i_valid,
718 inode->i_ctime = inode->i_mtime = current_time(inode);
719 mark_inode_dirty(inode);
727 * ntfs3_setattr - inode_operations::setattr
729 int ntfs3_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
732 struct super_block *sb = dentry->d_sb;
733 struct ntfs_sb_info *sbi = sb->s_fs_info;
734 struct inode *inode = d_inode(dentry);
735 struct ntfs_inode *ni = ntfs_i(inode);
736 u32 ia_valid = attr->ia_valid;
737 umode_t mode = inode->i_mode;
740 if (sbi->options.no_acs_rules) {
741 /* "No access rules" - Force any changes of time etc. */
742 attr->ia_valid |= ATTR_FORCE;
743 /* and disable for editing some attributes. */
744 attr->ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
745 ia_valid = attr->ia_valid;
748 err = setattr_prepare(mnt_userns, dentry, attr);
752 if (ia_valid & ATTR_SIZE) {
753 loff_t oldsize = inode->i_size;
755 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
756 /* Should never be here, see ntfs_file_open(). */
760 inode_dio_wait(inode);
762 if (attr->ia_size < oldsize)
763 err = ntfs_truncate(inode, attr->ia_size);
764 else if (attr->ia_size > oldsize)
765 err = ntfs_extend(inode, attr->ia_size, 0, NULL);
770 ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
773 setattr_copy(mnt_userns, inode, attr);
775 if (mode != inode->i_mode) {
776 err = ntfs_acl_chmod(mnt_userns, inode);
780 /* Linux 'w' -> Windows 'ro'. */
781 if (0222 & inode->i_mode)
782 ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
784 ni->std_fa |= FILE_ATTRIBUTE_READONLY;
787 if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
788 ntfs_save_wsl_perm(inode);
789 mark_inode_dirty(inode);
794 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
796 struct file *file = iocb->ki_filp;
797 struct inode *inode = file->f_mapping->host;
798 struct ntfs_inode *ni = ntfs_i(inode);
800 if (is_encrypted(ni)) {
801 ntfs_inode_warn(inode, "encrypted i/o not supported");
805 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
806 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
810 #ifndef CONFIG_NTFS3_LZX_XPRESS
811 if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
814 "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
820 ntfs_inode_warn(inode, "read deduplicated not supported");
824 return generic_file_read_iter(iocb, iter);
828 * ntfs_get_frame_pages
830 * Return: Array of locked pages.
832 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
833 struct page **pages, u32 pages_per_frame,
834 bool *frame_uptodate)
836 gfp_t gfp_mask = mapping_gfp_mask(mapping);
839 *frame_uptodate = true;
841 for (npages = 0; npages < pages_per_frame; npages++, index++) {
844 page = find_or_create_page(mapping, index, gfp_mask);
847 page = pages[npages];
855 if (!PageUptodate(page))
856 *frame_uptodate = false;
858 pages[npages] = page;
865 * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
867 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
870 struct file *file = iocb->ki_filp;
871 size_t count = iov_iter_count(from);
872 loff_t pos = iocb->ki_pos;
873 struct inode *inode = file_inode(file);
874 loff_t i_size = inode->i_size;
875 struct address_space *mapping = inode->i_mapping;
876 struct ntfs_inode *ni = ntfs_i(inode);
877 u64 valid = ni->i_valid;
878 struct ntfs_sb_info *sbi = ni->mi.sbi;
879 struct page *page, **pages = NULL;
881 u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
882 u32 frame_size = 1u << frame_bits;
883 u32 pages_per_frame = frame_size >> PAGE_SHIFT;
890 if (frame_size < PAGE_SIZE) {
892 * frame_size == 8K if cluster 512
893 * frame_size == 64K if cluster 4096
895 ntfs_inode_warn(inode, "page size is bigger than frame size");
899 pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
903 current->backing_dev_info = inode_to_bdi(inode);
904 err = file_remove_privs(file);
908 err = file_update_time(file);
912 /* Zero range [valid : pos). */
913 while (valid < pos) {
916 frame = valid >> frame_bits;
917 frame_vbo = valid & ~(frame_size - 1);
918 off = valid & (frame_size - 1);
920 err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 0, &lcn,
925 if (lcn == SPARSE_LCN) {
926 ni->i_valid = valid =
927 frame_vbo + ((u64)clen << sbi->cluster_bits);
931 /* Load full frame. */
932 err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
933 pages, pages_per_frame,
938 if (!frame_uptodate && off) {
939 err = ni_read_frame(ni, frame_vbo, pages,
942 for (ip = 0; ip < pages_per_frame; ip++) {
951 ip = off >> PAGE_SHIFT;
952 off = offset_in_page(valid);
953 for (; ip < pages_per_frame; ip++, off = 0) {
955 zero_user_segment(page, off, PAGE_SIZE);
956 flush_dcache_page(page);
957 SetPageUptodate(page);
961 err = ni_write_frame(ni, pages, pages_per_frame);
964 for (ip = 0; ip < pages_per_frame; ip++) {
966 SetPageUptodate(page);
974 ni->i_valid = valid = frame_vbo + frame_size;
977 /* Copy user data [pos : pos + count). */
979 size_t copied, bytes;
981 off = pos & (frame_size - 1);
982 bytes = frame_size - off;
986 frame = pos >> frame_bits;
987 frame_vbo = pos & ~(frame_size - 1);
988 index = frame_vbo >> PAGE_SHIFT;
990 if (unlikely(iov_iter_fault_in_readable(from, bytes))) {
995 /* Load full frame. */
996 err = ntfs_get_frame_pages(mapping, index, pages,
997 pages_per_frame, &frame_uptodate);
1001 if (!frame_uptodate) {
1002 loff_t to = pos + bytes;
1004 if (off || (to < i_size && (to & (frame_size - 1)))) {
1005 err = ni_read_frame(ni, frame_vbo, pages,
1008 for (ip = 0; ip < pages_per_frame;
1021 ip = off >> PAGE_SHIFT;
1022 off = offset_in_page(pos);
1024 /* Copy user data to pages. */
1026 size_t cp, tail = PAGE_SIZE - off;
1029 cp = copy_page_from_iter_atomic(page, off,
1030 min(tail, bytes), from);
1031 flush_dcache_page(page);
1047 err = ni_write_frame(ni, pages, pages_per_frame);
1050 for (ip = 0; ip < pages_per_frame; ip++) {
1052 ClearPageDirty(page);
1053 SetPageUptodate(page);
1062 * We can loop for a long time in here. Be nice and allow
1063 * us to schedule out to avoid softlocking if preempt
1071 count = iov_iter_count(from);
1077 current->backing_dev_info = NULL;
1082 iocb->ki_pos += written;
1083 if (iocb->ki_pos > ni->i_valid)
1084 ni->i_valid = iocb->ki_pos;
1090 * ntfs_file_write_iter - file_operations::write_iter
1092 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1094 struct file *file = iocb->ki_filp;
1095 struct address_space *mapping = file->f_mapping;
1096 struct inode *inode = mapping->host;
1098 struct ntfs_inode *ni = ntfs_i(inode);
1100 if (is_encrypted(ni)) {
1101 ntfs_inode_warn(inode, "encrypted i/o not supported");
1105 if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
1106 ntfs_inode_warn(inode, "direct i/o + compressed not supported");
1111 ntfs_inode_warn(inode, "write into deduplicated not supported");
1115 if (!inode_trylock(inode)) {
1116 if (iocb->ki_flags & IOCB_NOWAIT)
1121 ret = generic_write_checks(iocb, from);
1125 if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
1126 /* Should never be here, see ntfs_file_open(). */
1131 ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
1135 ret = is_compressed(ni) ? ntfs_compress_write(iocb, from)
1136 : __generic_file_write_iter(iocb, from);
1139 inode_unlock(inode);
1142 ret = generic_write_sync(iocb, ret);
1148 * ntfs_file_open - file_operations::open
1150 int ntfs_file_open(struct inode *inode, struct file *file)
1152 struct ntfs_inode *ni = ntfs_i(inode);
1154 if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
1155 (file->f_flags & O_DIRECT))) {
1159 /* Decompress "external compressed" file if opened for rw. */
1160 if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
1161 (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
1162 #ifdef CONFIG_NTFS3_LZX_XPRESS
1163 int err = ni_decompress_file(ni);
1170 "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
1175 return generic_file_open(inode, file);
1179 * ntfs_file_release - file_operations::release
1181 static int ntfs_file_release(struct inode *inode, struct file *file)
1183 struct ntfs_inode *ni = ntfs_i(inode);
1184 struct ntfs_sb_info *sbi = ni->mi.sbi;
1187 /* If we are last writer on the inode, drop the block reservation. */
1188 if (sbi->options.prealloc && ((file->f_mode & FMODE_WRITE) &&
1189 atomic_read(&inode->i_writecount) == 1)) {
1191 down_write(&ni->file.run_lock);
1193 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
1194 inode->i_size, &ni->i_valid, false, NULL);
1196 up_write(&ni->file.run_lock);
1203 * ntfs_fiemap - file_operations::fiemap
1205 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1206 __u64 start, __u64 len)
1209 struct ntfs_inode *ni = ntfs_i(inode);
1211 err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
1217 err = ni_fiemap(ni, fieinfo, start, len);
1225 const struct inode_operations ntfs_file_inode_operations = {
1226 .getattr = ntfs_getattr,
1227 .setattr = ntfs3_setattr,
1228 .listxattr = ntfs_listxattr,
1229 .permission = ntfs_permission,
1230 .get_acl = ntfs_get_acl,
1231 .set_acl = ntfs_set_acl,
1232 .fiemap = ntfs_fiemap,
1235 const struct file_operations ntfs_file_operations = {
1236 .llseek = generic_file_llseek,
1237 .read_iter = ntfs_file_read_iter,
1238 .write_iter = ntfs_file_write_iter,
1239 .unlocked_ioctl = ntfs_ioctl,
1240 #ifdef CONFIG_COMPAT
1241 .compat_ioctl = ntfs_compat_ioctl,
1243 .splice_read = generic_file_splice_read,
1244 .mmap = ntfs_file_mmap,
1245 .open = ntfs_file_open,
1246 .fsync = generic_file_fsync,
1247 .splice_write = iter_file_splice_write,
1248 .fallocate = ntfs_fallocate,
1249 .release = ntfs_file_release,