14342306fSKonstantin Komarov // SPDX-License-Identifier: GPL-2.0
24342306fSKonstantin Komarov /*
34342306fSKonstantin Komarov *
44342306fSKonstantin Komarov * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
54342306fSKonstantin Komarov *
6e8b8e97fSKari Argillander * Regular file handling primitives for NTFS-based filesystems.
7e8b8e97fSKari Argillander *
84342306fSKonstantin Komarov */
9e8b8e97fSKari Argillander
104342306fSKonstantin Komarov #include <linux/backing-dev.h>
11ccdf7741SChristoph Hellwig #include <linux/blkdev.h>
124342306fSKonstantin Komarov #include <linux/buffer_head.h>
134342306fSKonstantin Komarov #include <linux/compat.h>
144342306fSKonstantin Komarov #include <linux/falloc.h>
154342306fSKonstantin Komarov #include <linux/fiemap.h>
164342306fSKonstantin Komarov
174342306fSKonstantin Komarov #include "debug.h"
184342306fSKonstantin Komarov #include "ntfs.h"
194342306fSKonstantin Komarov #include "ntfs_fs.h"
204342306fSKonstantin Komarov
ntfs_ioctl_fitrim(struct ntfs_sb_info * sbi,unsigned long arg)214342306fSKonstantin Komarov static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg)
224342306fSKonstantin Komarov {
234342306fSKonstantin Komarov struct fstrim_range __user *user_range;
244342306fSKonstantin Komarov struct fstrim_range range;
2596de65a9SKonstantin Komarov struct block_device *dev;
264342306fSKonstantin Komarov int err;
274342306fSKonstantin Komarov
284342306fSKonstantin Komarov if (!capable(CAP_SYS_ADMIN))
294342306fSKonstantin Komarov return -EPERM;
304342306fSKonstantin Komarov
3196de65a9SKonstantin Komarov dev = sbi->sb->s_bdev;
3296de65a9SKonstantin Komarov if (!bdev_max_discard_sectors(dev))
334342306fSKonstantin Komarov return -EOPNOTSUPP;
344342306fSKonstantin Komarov
354342306fSKonstantin Komarov user_range = (struct fstrim_range __user *)arg;
364342306fSKonstantin Komarov if (copy_from_user(&range, user_range, sizeof(range)))
374342306fSKonstantin Komarov return -EFAULT;
384342306fSKonstantin Komarov
3996de65a9SKonstantin Komarov range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev));
404342306fSKonstantin Komarov
414342306fSKonstantin Komarov err = ntfs_trim_fs(sbi, &range);
424342306fSKonstantin Komarov if (err < 0)
434342306fSKonstantin Komarov return err;
444342306fSKonstantin Komarov
454342306fSKonstantin Komarov if (copy_to_user(user_range, &range, sizeof(range)))
464342306fSKonstantin Komarov return -EFAULT;
474342306fSKonstantin Komarov
484342306fSKonstantin Komarov return 0;
494342306fSKonstantin Komarov }
504342306fSKonstantin Komarov
ntfs_ioctl(struct file * filp,u32 cmd,unsigned long arg)514342306fSKonstantin Komarov static long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg)
524342306fSKonstantin Komarov {
534342306fSKonstantin Komarov struct inode *inode = file_inode(filp);
544342306fSKonstantin Komarov struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
554342306fSKonstantin Komarov
564342306fSKonstantin Komarov switch (cmd) {
574342306fSKonstantin Komarov case FITRIM:
584342306fSKonstantin Komarov return ntfs_ioctl_fitrim(sbi, arg);
594342306fSKonstantin Komarov }
60e8b8e97fSKari Argillander return -ENOTTY; /* Inappropriate ioctl for device. */
614342306fSKonstantin Komarov }
624342306fSKonstantin Komarov
634342306fSKonstantin Komarov #ifdef CONFIG_COMPAT
ntfs_compat_ioctl(struct file * filp,u32 cmd,unsigned long arg)644342306fSKonstantin Komarov static long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg)
654342306fSKonstantin Komarov
664342306fSKonstantin Komarov {
674342306fSKonstantin Komarov return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
684342306fSKonstantin Komarov }
694342306fSKonstantin Komarov #endif
704342306fSKonstantin Komarov
714342306fSKonstantin Komarov /*
72e8b8e97fSKari Argillander * ntfs_getattr - inode_operations::getattr
734342306fSKonstantin Komarov */
ntfs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,u32 flags)74b74d24f7SChristian Brauner int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
754342306fSKonstantin Komarov struct kstat *stat, u32 request_mask, u32 flags)
764342306fSKonstantin Komarov {
774342306fSKonstantin Komarov struct inode *inode = d_inode(path->dentry);
784342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
794342306fSKonstantin Komarov
804342306fSKonstantin Komarov if (is_compressed(ni))
814342306fSKonstantin Komarov stat->attributes |= STATX_ATTR_COMPRESSED;
824342306fSKonstantin Komarov
834342306fSKonstantin Komarov if (is_encrypted(ni))
844342306fSKonstantin Komarov stat->attributes |= STATX_ATTR_ENCRYPTED;
854342306fSKonstantin Komarov
864342306fSKonstantin Komarov stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED;
874342306fSKonstantin Komarov
880d72b928SJeff Layton generic_fillattr(idmap, request_mask, inode, stat);
894342306fSKonstantin Komarov
904342306fSKonstantin Komarov stat->result_mask |= STATX_BTIME;
914342306fSKonstantin Komarov stat->btime = ni->i_crtime;
924342306fSKonstantin Komarov stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */
934342306fSKonstantin Komarov
944342306fSKonstantin Komarov return 0;
954342306fSKonstantin Komarov }
964342306fSKonstantin Komarov
ntfs_extend_initialized_size(struct file * file,struct ntfs_inode * ni,const loff_t valid,const loff_t new_valid)974342306fSKonstantin Komarov static int ntfs_extend_initialized_size(struct file *file,
984342306fSKonstantin Komarov struct ntfs_inode *ni,
994342306fSKonstantin Komarov const loff_t valid,
1004342306fSKonstantin Komarov const loff_t new_valid)
1014342306fSKonstantin Komarov {
1024342306fSKonstantin Komarov struct inode *inode = &ni->vfs_inode;
1034342306fSKonstantin Komarov struct address_space *mapping = inode->i_mapping;
1044342306fSKonstantin Komarov struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1054342306fSKonstantin Komarov loff_t pos = valid;
1064342306fSKonstantin Komarov int err;
1074342306fSKonstantin Komarov
1084342306fSKonstantin Komarov if (is_resident(ni)) {
1094342306fSKonstantin Komarov ni->i_valid = new_valid;
1104342306fSKonstantin Komarov return 0;
1114342306fSKonstantin Komarov }
1124342306fSKonstantin Komarov
1134342306fSKonstantin Komarov WARN_ON(is_compressed(ni));
1144342306fSKonstantin Komarov WARN_ON(valid >= new_valid);
1154342306fSKonstantin Komarov
1164342306fSKonstantin Komarov for (;;) {
1174342306fSKonstantin Komarov u32 zerofrom, len;
1184342306fSKonstantin Komarov struct page *page;
1194342306fSKonstantin Komarov u8 bits;
1204342306fSKonstantin Komarov CLST vcn, lcn, clen;
1214342306fSKonstantin Komarov
1224342306fSKonstantin Komarov if (is_sparsed(ni)) {
1234342306fSKonstantin Komarov bits = sbi->cluster_bits;
1244342306fSKonstantin Komarov vcn = pos >> bits;
1254342306fSKonstantin Komarov
126c380b52fSKonstantin Komarov err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL,
127c380b52fSKonstantin Komarov false);
1284342306fSKonstantin Komarov if (err)
1294342306fSKonstantin Komarov goto out;
1304342306fSKonstantin Komarov
1314342306fSKonstantin Komarov if (lcn == SPARSE_LCN) {
1322f56a3f8SKonstantin Komarov pos = ((loff_t)clen + vcn) << bits;
1332f56a3f8SKonstantin Komarov ni->i_valid = pos;
1344342306fSKonstantin Komarov goto next;
1354342306fSKonstantin Komarov }
1364342306fSKonstantin Komarov }
1374342306fSKonstantin Komarov
1384342306fSKonstantin Komarov zerofrom = pos & (PAGE_SIZE - 1);
1394342306fSKonstantin Komarov len = PAGE_SIZE - zerofrom;
1404342306fSKonstantin Komarov
1414342306fSKonstantin Komarov if (pos + len > new_valid)
1424342306fSKonstantin Komarov len = new_valid - pos;
1434342306fSKonstantin Komarov
144652118b8SMatthew Wilcox (Oracle) err = ntfs_write_begin(file, mapping, pos, len, &page, NULL);
1454342306fSKonstantin Komarov if (err)
1464342306fSKonstantin Komarov goto out;
1474342306fSKonstantin Komarov
1484342306fSKonstantin Komarov zero_user_segment(page, zerofrom, PAGE_SIZE);
1494342306fSKonstantin Komarov
150e8b8e97fSKari Argillander /* This function in any case puts page. */
151652118b8SMatthew Wilcox (Oracle) err = ntfs_write_end(file, mapping, pos, len, len, page, NULL);
1524342306fSKonstantin Komarov if (err < 0)
1534342306fSKonstantin Komarov goto out;
1544342306fSKonstantin Komarov pos += len;
1554342306fSKonstantin Komarov
1564342306fSKonstantin Komarov next:
1574342306fSKonstantin Komarov if (pos >= new_valid)
1584342306fSKonstantin Komarov break;
1594342306fSKonstantin Komarov
1604342306fSKonstantin Komarov balance_dirty_pages_ratelimited(mapping);
1614342306fSKonstantin Komarov cond_resched();
1624342306fSKonstantin Komarov }
1634342306fSKonstantin Komarov
1644342306fSKonstantin Komarov return 0;
1654342306fSKonstantin Komarov
1664342306fSKonstantin Komarov out:
1674342306fSKonstantin Komarov ni->i_valid = valid;
1684342306fSKonstantin Komarov ntfs_inode_warn(inode, "failed to extend initialized size to %llx.",
1694342306fSKonstantin Komarov new_valid);
1704342306fSKonstantin Komarov return err;
1714342306fSKonstantin Komarov }
1724342306fSKonstantin Komarov
1734342306fSKonstantin Komarov /*
174e8b8e97fSKari Argillander * ntfs_zero_range - Helper function for punch_hole.
175d3624466SKonstantin Komarov *
176d3624466SKonstantin Komarov * It zeroes a range [vbo, vbo_to).
1774342306fSKonstantin Komarov */
ntfs_zero_range(struct inode * inode,u64 vbo,u64 vbo_to)1784342306fSKonstantin Komarov static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to)
1794342306fSKonstantin Komarov {
1804342306fSKonstantin Komarov int err = 0;
1814342306fSKonstantin Komarov struct address_space *mapping = inode->i_mapping;
182f39244e2SYangtao Li u32 blocksize = i_blocksize(inode);
1834342306fSKonstantin Komarov pgoff_t idx = vbo >> PAGE_SHIFT;
184c380b52fSKonstantin Komarov u32 from = vbo & (PAGE_SIZE - 1);
1854342306fSKonstantin Komarov pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT;
1864342306fSKonstantin Komarov loff_t page_off;
1874342306fSKonstantin Komarov struct buffer_head *head, *bh;
188c380b52fSKonstantin Komarov u32 bh_next, bh_off, to;
1894342306fSKonstantin Komarov sector_t iblock;
1904342306fSKonstantin Komarov struct page *page;
1914342306fSKonstantin Komarov
192c380b52fSKonstantin Komarov for (; idx < idx_end; idx += 1, from = 0) {
1934342306fSKonstantin Komarov page_off = (loff_t)idx << PAGE_SHIFT;
19496de65a9SKonstantin Komarov to = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off) :
19596de65a9SKonstantin Komarov PAGE_SIZE;
1964342306fSKonstantin Komarov iblock = page_off >> inode->i_blkbits;
1974342306fSKonstantin Komarov
1984342306fSKonstantin Komarov page = find_or_create_page(mapping, idx,
1994342306fSKonstantin Komarov mapping_gfp_constraint(mapping,
2004342306fSKonstantin Komarov ~__GFP_FS));
2014342306fSKonstantin Komarov if (!page)
2024342306fSKonstantin Komarov return -ENOMEM;
2034342306fSKonstantin Komarov
2044342306fSKonstantin Komarov if (!page_has_buffers(page))
2054342306fSKonstantin Komarov create_empty_buffers(page, blocksize, 0);
2064342306fSKonstantin Komarov
2074342306fSKonstantin Komarov bh = head = page_buffers(page);
2084342306fSKonstantin Komarov bh_off = 0;
2094342306fSKonstantin Komarov do {
2104342306fSKonstantin Komarov bh_next = bh_off + blocksize;
2114342306fSKonstantin Komarov
212c380b52fSKonstantin Komarov if (bh_next <= from || bh_off >= to)
2134342306fSKonstantin Komarov continue;
2144342306fSKonstantin Komarov
2154342306fSKonstantin Komarov if (!buffer_mapped(bh)) {
2164342306fSKonstantin Komarov ntfs_get_block(inode, iblock, bh, 0);
217d3624466SKonstantin Komarov /* Unmapped? It's a hole - nothing to do. */
2184342306fSKonstantin Komarov if (!buffer_mapped(bh))
2194342306fSKonstantin Komarov continue;
2204342306fSKonstantin Komarov }
2214342306fSKonstantin Komarov
222d3624466SKonstantin Komarov /* Ok, it's mapped. Make sure it's up-to-date. */
2234342306fSKonstantin Komarov if (PageUptodate(page))
2244342306fSKonstantin Komarov set_buffer_uptodate(bh);
2254342306fSKonstantin Komarov
2264342306fSKonstantin Komarov if (!buffer_uptodate(bh)) {
227c20bc9c6SKonstantin Komarov err = bh_read(bh, 0);
228c20bc9c6SKonstantin Komarov if (err < 0) {
2294342306fSKonstantin Komarov unlock_page(page);
2304342306fSKonstantin Komarov put_page(page);
2314342306fSKonstantin Komarov goto out;
2324342306fSKonstantin Komarov }
2334342306fSKonstantin Komarov }
2344342306fSKonstantin Komarov
2354342306fSKonstantin Komarov mark_buffer_dirty(bh);
2364342306fSKonstantin Komarov
2374342306fSKonstantin Komarov } while (bh_off = bh_next, iblock += 1,
2384342306fSKonstantin Komarov head != (bh = bh->b_this_page));
2394342306fSKonstantin Komarov
240c380b52fSKonstantin Komarov zero_user_segment(page, from, to);
2414342306fSKonstantin Komarov
2424342306fSKonstantin Komarov unlock_page(page);
2434342306fSKonstantin Komarov put_page(page);
2444342306fSKonstantin Komarov cond_resched();
2454342306fSKonstantin Komarov }
2464342306fSKonstantin Komarov out:
2474342306fSKonstantin Komarov mark_inode_dirty(inode);
2484342306fSKonstantin Komarov return err;
2494342306fSKonstantin Komarov }
2504342306fSKonstantin Komarov
2514342306fSKonstantin Komarov /*
252e8b8e97fSKari Argillander * ntfs_file_mmap - file_operations::mmap
2534342306fSKonstantin Komarov */
ntfs_file_mmap(struct file * file,struct vm_area_struct * vma)2544342306fSKonstantin Komarov static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma)
2554342306fSKonstantin Komarov {
2564342306fSKonstantin Komarov struct address_space *mapping = file->f_mapping;
2574342306fSKonstantin Komarov struct inode *inode = mapping->host;
2584342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
2594342306fSKonstantin Komarov u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT);
2604342306fSKonstantin Komarov bool rw = vma->vm_flags & VM_WRITE;
2614342306fSKonstantin Komarov int err;
2624342306fSKonstantin Komarov
263f73f9397SKonstantin Komarov if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
264f73f9397SKonstantin Komarov return -EIO;
265f73f9397SKonstantin Komarov
2664342306fSKonstantin Komarov if (is_encrypted(ni)) {
2674342306fSKonstantin Komarov ntfs_inode_warn(inode, "mmap encrypted not supported");
2684342306fSKonstantin Komarov return -EOPNOTSUPP;
2694342306fSKonstantin Komarov }
2704342306fSKonstantin Komarov
2714342306fSKonstantin Komarov if (is_dedup(ni)) {
2724342306fSKonstantin Komarov ntfs_inode_warn(inode, "mmap deduplicated not supported");
2734342306fSKonstantin Komarov return -EOPNOTSUPP;
2744342306fSKonstantin Komarov }
2754342306fSKonstantin Komarov
2764342306fSKonstantin Komarov if (is_compressed(ni) && rw) {
2774342306fSKonstantin Komarov ntfs_inode_warn(inode, "mmap(write) compressed not supported");
2784342306fSKonstantin Komarov return -EOPNOTSUPP;
2794342306fSKonstantin Komarov }
2804342306fSKonstantin Komarov
2814342306fSKonstantin Komarov if (rw) {
2824342306fSKonstantin Komarov u64 to = min_t(loff_t, i_size_read(inode),
2834342306fSKonstantin Komarov from + vma->vm_end - vma->vm_start);
2844342306fSKonstantin Komarov
2854342306fSKonstantin Komarov if (is_sparsed(ni)) {
286e8b8e97fSKari Argillander /* Allocate clusters for rw map. */
2874342306fSKonstantin Komarov struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
2884342306fSKonstantin Komarov CLST lcn, len;
2894342306fSKonstantin Komarov CLST vcn = from >> sbi->cluster_bits;
2904342306fSKonstantin Komarov CLST end = bytes_to_cluster(sbi, to);
2914342306fSKonstantin Komarov bool new;
2924342306fSKonstantin Komarov
2934342306fSKonstantin Komarov for (; vcn < end; vcn += len) {
2944342306fSKonstantin Komarov err = attr_data_get_block(ni, vcn, 1, &lcn,
295c380b52fSKonstantin Komarov &len, &new, true);
2964342306fSKonstantin Komarov if (err)
2974342306fSKonstantin Komarov goto out;
2984342306fSKonstantin Komarov }
2994342306fSKonstantin Komarov }
3004342306fSKonstantin Komarov
3014342306fSKonstantin Komarov if (ni->i_valid < to) {
30253173e38SKonstantin Komarov inode_lock(inode);
3034342306fSKonstantin Komarov err = ntfs_extend_initialized_size(file, ni,
3044342306fSKonstantin Komarov ni->i_valid, to);
3054342306fSKonstantin Komarov inode_unlock(inode);
3064342306fSKonstantin Komarov if (err)
3074342306fSKonstantin Komarov goto out;
3084342306fSKonstantin Komarov }
3094342306fSKonstantin Komarov }
3104342306fSKonstantin Komarov
3114342306fSKonstantin Komarov err = generic_file_mmap(file, vma);
3124342306fSKonstantin Komarov out:
3134342306fSKonstantin Komarov return err;
3144342306fSKonstantin Komarov }
3154342306fSKonstantin Komarov
ntfs_extend(struct inode * inode,loff_t pos,size_t count,struct file * file)3164342306fSKonstantin Komarov static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
3174342306fSKonstantin Komarov struct file *file)
3184342306fSKonstantin Komarov {
3194342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
3204342306fSKonstantin Komarov struct address_space *mapping = inode->i_mapping;
3214342306fSKonstantin Komarov loff_t end = pos + count;
3224342306fSKonstantin Komarov bool extend_init = file && pos > ni->i_valid;
3234342306fSKonstantin Komarov int err;
3244342306fSKonstantin Komarov
3254342306fSKonstantin Komarov if (end <= inode->i_size && !extend_init)
3264342306fSKonstantin Komarov return 0;
3274342306fSKonstantin Komarov
328e8b8e97fSKari Argillander /* Mark rw ntfs as dirty. It will be cleared at umount. */
3294342306fSKonstantin Komarov ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
3304342306fSKonstantin Komarov
3314342306fSKonstantin Komarov if (end > inode->i_size) {
3324342306fSKonstantin Komarov err = ntfs_set_size(inode, end);
3334342306fSKonstantin Komarov if (err)
3344342306fSKonstantin Komarov goto out;
3354342306fSKonstantin Komarov }
3364342306fSKonstantin Komarov
3374342306fSKonstantin Komarov if (extend_init && !is_compressed(ni)) {
3384342306fSKonstantin Komarov err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
3394342306fSKonstantin Komarov if (err)
3404342306fSKonstantin Komarov goto out;
3414342306fSKonstantin Komarov } else {
3424342306fSKonstantin Komarov err = 0;
3434342306fSKonstantin Komarov }
3444342306fSKonstantin Komarov
3453d65c46fSJeff Layton inode->i_mtime = inode_set_ctime_current(inode);
3464342306fSKonstantin Komarov mark_inode_dirty(inode);
3474342306fSKonstantin Komarov
3484342306fSKonstantin Komarov if (IS_SYNC(inode)) {
3494342306fSKonstantin Komarov int err2;
3504342306fSKonstantin Komarov
3514342306fSKonstantin Komarov err = filemap_fdatawrite_range(mapping, pos, end - 1);
3524342306fSKonstantin Komarov err2 = sync_mapping_buffers(mapping);
3534342306fSKonstantin Komarov if (!err)
3544342306fSKonstantin Komarov err = err2;
3554342306fSKonstantin Komarov err2 = write_inode_now(inode, 1);
3564342306fSKonstantin Komarov if (!err)
3574342306fSKonstantin Komarov err = err2;
3584342306fSKonstantin Komarov if (!err)
3594342306fSKonstantin Komarov err = filemap_fdatawait_range(mapping, pos, end - 1);
3604342306fSKonstantin Komarov }
3614342306fSKonstantin Komarov
3624342306fSKonstantin Komarov out:
3634342306fSKonstantin Komarov return err;
3644342306fSKonstantin Komarov }
3654342306fSKonstantin Komarov
ntfs_truncate(struct inode * inode,loff_t new_size)3664342306fSKonstantin Komarov static int ntfs_truncate(struct inode *inode, loff_t new_size)
3674342306fSKonstantin Komarov {
3684342306fSKonstantin Komarov struct super_block *sb = inode->i_sb;
3694342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
3704342306fSKonstantin Komarov int err, dirty = 0;
3714342306fSKonstantin Komarov u64 new_valid;
3724342306fSKonstantin Komarov
3734342306fSKonstantin Komarov if (!S_ISREG(inode->i_mode))
3744342306fSKonstantin Komarov return 0;
3754342306fSKonstantin Komarov
3764342306fSKonstantin Komarov if (is_compressed(ni)) {
3774342306fSKonstantin Komarov if (ni->i_valid > new_size)
3784342306fSKonstantin Komarov ni->i_valid = new_size;
3794342306fSKonstantin Komarov } else {
3804342306fSKonstantin Komarov err = block_truncate_page(inode->i_mapping, new_size,
3814342306fSKonstantin Komarov ntfs_get_block);
3824342306fSKonstantin Komarov if (err)
3834342306fSKonstantin Komarov return err;
3844342306fSKonstantin Komarov }
3854342306fSKonstantin Komarov
3864342306fSKonstantin Komarov new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
3874342306fSKonstantin Komarov
3884342306fSKonstantin Komarov truncate_setsize(inode, new_size);
3894342306fSKonstantin Komarov
3900226635cSTetsuo Handa ni_lock(ni);
3910226635cSTetsuo Handa
3924342306fSKonstantin Komarov down_write(&ni->file.run_lock);
3934342306fSKonstantin Komarov err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
394e95113edSKonstantin Komarov &new_valid, ni->mi.sbi->options->prealloc, NULL);
3954342306fSKonstantin Komarov up_write(&ni->file.run_lock);
3964342306fSKonstantin Komarov
3974342306fSKonstantin Komarov if (new_valid < ni->i_valid)
3984342306fSKonstantin Komarov ni->i_valid = new_valid;
3994342306fSKonstantin Komarov
4004342306fSKonstantin Komarov ni_unlock(ni);
4014342306fSKonstantin Komarov
4024342306fSKonstantin Komarov ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
4033d65c46fSJeff Layton inode->i_mtime = inode_set_ctime_current(inode);
4044342306fSKonstantin Komarov if (!IS_DIRSYNC(inode)) {
4054342306fSKonstantin Komarov dirty = 1;
4064342306fSKonstantin Komarov } else {
4074342306fSKonstantin Komarov err = ntfs_sync_inode(inode);
4084342306fSKonstantin Komarov if (err)
4094342306fSKonstantin Komarov return err;
4104342306fSKonstantin Komarov }
4114342306fSKonstantin Komarov
4124342306fSKonstantin Komarov if (dirty)
4134342306fSKonstantin Komarov mark_inode_dirty(inode);
4144342306fSKonstantin Komarov
4154342306fSKonstantin Komarov /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
4164342306fSKonstantin Komarov
4174342306fSKonstantin Komarov return 0;
4184342306fSKonstantin Komarov }
4194342306fSKonstantin Komarov
4204342306fSKonstantin Komarov /*
42116f93bddSKonstantin Komarov * ntfs_fallocate - file_operations::ntfs_fallocate
422e8b8e97fSKari Argillander *
4234342306fSKonstantin Komarov * Preallocate space for a file. This implements ntfs's fallocate file
4244342306fSKonstantin Komarov * operation, which gets called from sys_fallocate system call. User
4254342306fSKonstantin Komarov * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
4264342306fSKonstantin Komarov * we just allocate clusters without zeroing them out. Otherwise we
4274342306fSKonstantin Komarov * allocate and zero out clusters via an expanding truncate.
4284342306fSKonstantin Komarov */
ntfs_fallocate(struct file * file,int mode,loff_t vbo,loff_t len)4294342306fSKonstantin Komarov static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
4304342306fSKonstantin Komarov {
4314342306fSKonstantin Komarov struct inode *inode = file->f_mapping->host;
432e4d2f4fdSKonstantin Komarov struct address_space *mapping = inode->i_mapping;
4334342306fSKonstantin Komarov struct super_block *sb = inode->i_sb;
4344342306fSKonstantin Komarov struct ntfs_sb_info *sbi = sb->s_fs_info;
4354342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
4364342306fSKonstantin Komarov loff_t end = vbo + len;
437c380b52fSKonstantin Komarov loff_t vbo_down = round_down(vbo, max_t(unsigned long,
438c380b52fSKonstantin Komarov sbi->cluster_size, PAGE_SIZE));
439e4d2f4fdSKonstantin Komarov bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);
440e4d2f4fdSKonstantin Komarov loff_t i_size, new_size;
441e4d2f4fdSKonstantin Komarov bool map_locked;
4424342306fSKonstantin Komarov int err;
4434342306fSKonstantin Komarov
444e8b8e97fSKari Argillander /* No support for dir. */
4454342306fSKonstantin Komarov if (!S_ISREG(inode->i_mode))
4464342306fSKonstantin Komarov return -EOPNOTSUPP;
4474342306fSKonstantin Komarov
448e4d2f4fdSKonstantin Komarov /*
449e4d2f4fdSKonstantin Komarov * vfs_fallocate checks all possible combinations of mode.
450e4d2f4fdSKonstantin Komarov * Do additional checks here before ntfs_set_state(dirty).
451e4d2f4fdSKonstantin Komarov */
452e4d2f4fdSKonstantin Komarov if (mode & FALLOC_FL_PUNCH_HOLE) {
453e4d2f4fdSKonstantin Komarov if (!is_supported_holes)
454e4d2f4fdSKonstantin Komarov return -EOPNOTSUPP;
455e4d2f4fdSKonstantin Komarov } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
456e4d2f4fdSKonstantin Komarov } else if (mode & FALLOC_FL_INSERT_RANGE) {
457e4d2f4fdSKonstantin Komarov if (!is_supported_holes)
458e4d2f4fdSKonstantin Komarov return -EOPNOTSUPP;
459e4d2f4fdSKonstantin Komarov } else if (mode &
460e4d2f4fdSKonstantin Komarov ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
461e4d2f4fdSKonstantin Komarov FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) {
4624342306fSKonstantin Komarov ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
4634342306fSKonstantin Komarov mode);
4644342306fSKonstantin Komarov return -EOPNOTSUPP;
4654342306fSKonstantin Komarov }
4664342306fSKonstantin Komarov
4674342306fSKonstantin Komarov ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
4684342306fSKonstantin Komarov
4694342306fSKonstantin Komarov inode_lock(inode);
4704342306fSKonstantin Komarov i_size = inode->i_size;
471e4d2f4fdSKonstantin Komarov new_size = max(end, i_size);
472e4d2f4fdSKonstantin Komarov map_locked = false;
4734342306fSKonstantin Komarov
4744342306fSKonstantin Komarov if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
475e8b8e97fSKari Argillander /* Should never be here, see ntfs_file_open. */
4764342306fSKonstantin Komarov err = -EOPNOTSUPP;
4774342306fSKonstantin Komarov goto out;
4784342306fSKonstantin Komarov }
4794342306fSKonstantin Komarov
480e4d2f4fdSKonstantin Komarov if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
481e4d2f4fdSKonstantin Komarov FALLOC_FL_INSERT_RANGE)) {
482e4d2f4fdSKonstantin Komarov inode_dio_wait(inode);
483e4d2f4fdSKonstantin Komarov filemap_invalidate_lock(mapping);
484e4d2f4fdSKonstantin Komarov map_locked = true;
485e4d2f4fdSKonstantin Komarov }
486e4d2f4fdSKonstantin Komarov
4874342306fSKonstantin Komarov if (mode & FALLOC_FL_PUNCH_HOLE) {
4884342306fSKonstantin Komarov u32 frame_size;
4894342306fSKonstantin Komarov loff_t mask, vbo_a, end_a, tmp;
4904342306fSKonstantin Komarov
491c380b52fSKonstantin Komarov err = filemap_write_and_wait_range(mapping, vbo_down,
492c380b52fSKonstantin Komarov LLONG_MAX);
4934342306fSKonstantin Komarov if (err)
4944342306fSKonstantin Komarov goto out;
4954342306fSKonstantin Komarov
4964342306fSKonstantin Komarov truncate_pagecache(inode, vbo_down);
4974342306fSKonstantin Komarov
4984342306fSKonstantin Komarov ni_lock(ni);
4994342306fSKonstantin Komarov err = attr_punch_hole(ni, vbo, len, &frame_size);
5004342306fSKonstantin Komarov ni_unlock(ni);
5017e0aff0aSKonstantin Komarov if (!err)
5027e0aff0aSKonstantin Komarov goto ok;
5037e0aff0aSKonstantin Komarov
5044342306fSKonstantin Komarov if (err != E_NTFS_NOTALIGNED)
5054342306fSKonstantin Komarov goto out;
5064342306fSKonstantin Komarov
507d3624466SKonstantin Komarov /* Process not aligned punch. */
5087e0aff0aSKonstantin Komarov err = 0;
5094342306fSKonstantin Komarov mask = frame_size - 1;
5104342306fSKonstantin Komarov vbo_a = (vbo + mask) & ~mask;
5114342306fSKonstantin Komarov end_a = end & ~mask;
5124342306fSKonstantin Komarov
5134342306fSKonstantin Komarov tmp = min(vbo_a, end);
5144342306fSKonstantin Komarov if (tmp > vbo) {
5154342306fSKonstantin Komarov err = ntfs_zero_range(inode, vbo, tmp);
5164342306fSKonstantin Komarov if (err)
5174342306fSKonstantin Komarov goto out;
5184342306fSKonstantin Komarov }
5194342306fSKonstantin Komarov
5204342306fSKonstantin Komarov if (vbo < end_a && end_a < end) {
5214342306fSKonstantin Komarov err = ntfs_zero_range(inode, end_a, end);
5224342306fSKonstantin Komarov if (err)
5234342306fSKonstantin Komarov goto out;
5244342306fSKonstantin Komarov }
5254342306fSKonstantin Komarov
5264342306fSKonstantin Komarov /* Aligned punch_hole */
5274342306fSKonstantin Komarov if (end_a > vbo_a) {
5284342306fSKonstantin Komarov ni_lock(ni);
5294342306fSKonstantin Komarov err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
5304342306fSKonstantin Komarov ni_unlock(ni);
5317e0aff0aSKonstantin Komarov if (err)
5327e0aff0aSKonstantin Komarov goto out;
5334342306fSKonstantin Komarov }
5344342306fSKonstantin Komarov } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
5354342306fSKonstantin Komarov /*
5364342306fSKonstantin Komarov * Write tail of the last page before removed range since
5374342306fSKonstantin Komarov * it will get removed from the page cache below.
5384342306fSKonstantin Komarov */
539e4d2f4fdSKonstantin Komarov err = filemap_write_and_wait_range(mapping, vbo_down, vbo);
5404342306fSKonstantin Komarov if (err)
5414342306fSKonstantin Komarov goto out;
5424342306fSKonstantin Komarov
5434342306fSKonstantin Komarov /*
5444342306fSKonstantin Komarov * Write data that will be shifted to preserve them
545e8b8e97fSKari Argillander * when discarding page cache below.
5464342306fSKonstantin Komarov */
547e4d2f4fdSKonstantin Komarov err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
5484342306fSKonstantin Komarov if (err)
5494342306fSKonstantin Komarov goto out;
5504342306fSKonstantin Komarov
5514342306fSKonstantin Komarov truncate_pagecache(inode, vbo_down);
5524342306fSKonstantin Komarov
5534342306fSKonstantin Komarov ni_lock(ni);
5544342306fSKonstantin Komarov err = attr_collapse_range(ni, vbo, len);
5554342306fSKonstantin Komarov ni_unlock(ni);
55616f93bddSKonstantin Komarov if (err)
55716f93bddSKonstantin Komarov goto out;
558e4d2f4fdSKonstantin Komarov } else if (mode & FALLOC_FL_INSERT_RANGE) {
559e4d2f4fdSKonstantin Komarov /* Check new size. */
56011434697SKonstantin Komarov err = inode_newsize_ok(inode, new_size);
56111434697SKonstantin Komarov if (err)
56211434697SKonstantin Komarov goto out;
56311434697SKonstantin Komarov
564e4d2f4fdSKonstantin Komarov /* Write out all dirty pages. */
565e4d2f4fdSKonstantin Komarov err = filemap_write_and_wait_range(mapping, vbo_down,
566e4d2f4fdSKonstantin Komarov LLONG_MAX);
567e4d2f4fdSKonstantin Komarov if (err)
568e4d2f4fdSKonstantin Komarov goto out;
569e4d2f4fdSKonstantin Komarov truncate_pagecache(inode, vbo_down);
570e4d2f4fdSKonstantin Komarov
571e4d2f4fdSKonstantin Komarov ni_lock(ni);
572e4d2f4fdSKonstantin Komarov err = attr_insert_range(ni, vbo, len);
573e4d2f4fdSKonstantin Komarov ni_unlock(ni);
5747e0aff0aSKonstantin Komarov if (err)
5757e0aff0aSKonstantin Komarov goto out;
5764342306fSKonstantin Komarov } else {
577e4d2f4fdSKonstantin Komarov /* Check new size. */
57896de65a9SKonstantin Komarov u8 cluster_bits = sbi->cluster_bits;
579b3e04872SKonstantin Komarov
580b3e04872SKonstantin Komarov /* generic/213: expected -ENOSPC instead of -EFBIG. */
581b3e04872SKonstantin Komarov if (!is_supported_holes) {
582b3e04872SKonstantin Komarov loff_t to_alloc = new_size - inode_get_bytes(inode);
583b3e04872SKonstantin Komarov
584b3e04872SKonstantin Komarov if (to_alloc > 0 &&
58596de65a9SKonstantin Komarov (to_alloc >> cluster_bits) >
586b3e04872SKonstantin Komarov wnd_zeroes(&sbi->used.bitmap)) {
587b3e04872SKonstantin Komarov err = -ENOSPC;
588b3e04872SKonstantin Komarov goto out;
589b3e04872SKonstantin Komarov }
590b3e04872SKonstantin Komarov }
59111434697SKonstantin Komarov
59211434697SKonstantin Komarov err = inode_newsize_ok(inode, new_size);
59311434697SKonstantin Komarov if (err)
59411434697SKonstantin Komarov goto out;
59511434697SKonstantin Komarov
596ad26a9c8SKonstantin Komarov if (new_size > i_size) {
597e4d2f4fdSKonstantin Komarov /*
598e4d2f4fdSKonstantin Komarov * Allocate clusters, do not change 'valid' size.
599e4d2f4fdSKonstantin Komarov */
60011434697SKonstantin Komarov err = ntfs_set_size(inode, new_size);
6014342306fSKonstantin Komarov if (err)
6024342306fSKonstantin Komarov goto out;
603ad26a9c8SKonstantin Komarov }
6044342306fSKonstantin Komarov
605e4d2f4fdSKonstantin Komarov if (is_supported_holes) {
60696de65a9SKonstantin Komarov CLST vcn = vbo >> cluster_bits;
6074342306fSKonstantin Komarov CLST cend = bytes_to_cluster(sbi, end);
608c380b52fSKonstantin Komarov CLST cend_v = bytes_to_cluster(sbi, ni->i_valid);
6094342306fSKonstantin Komarov CLST lcn, clen;
6104342306fSKonstantin Komarov bool new;
6114342306fSKonstantin Komarov
612c380b52fSKonstantin Komarov if (cend_v > cend)
613c380b52fSKonstantin Komarov cend_v = cend;
614c380b52fSKonstantin Komarov
6154342306fSKonstantin Komarov /*
616c380b52fSKonstantin Komarov * Allocate and zero new clusters.
617e8b8e97fSKari Argillander * Zeroing these clusters may be too long.
618c380b52fSKonstantin Komarov */
619c380b52fSKonstantin Komarov for (; vcn < cend_v; vcn += clen) {
620c380b52fSKonstantin Komarov err = attr_data_get_block(ni, vcn, cend_v - vcn,
621c380b52fSKonstantin Komarov &lcn, &clen, &new,
622c380b52fSKonstantin Komarov true);
623c380b52fSKonstantin Komarov if (err)
624c380b52fSKonstantin Komarov goto out;
625c380b52fSKonstantin Komarov }
626c380b52fSKonstantin Komarov /*
627c380b52fSKonstantin Komarov * Allocate but not zero new clusters.
6284342306fSKonstantin Komarov */
6294342306fSKonstantin Komarov for (; vcn < cend; vcn += clen) {
6304342306fSKonstantin Komarov err = attr_data_get_block(ni, vcn, cend - vcn,
631c380b52fSKonstantin Komarov &lcn, &clen, &new,
632c380b52fSKonstantin Komarov false);
6334342306fSKonstantin Komarov if (err)
6344342306fSKonstantin Komarov goto out;
6354342306fSKonstantin Komarov }
6364342306fSKonstantin Komarov }
6374342306fSKonstantin Komarov
6384342306fSKonstantin Komarov if (mode & FALLOC_FL_KEEP_SIZE) {
6394342306fSKonstantin Komarov ni_lock(ni);
640e8b8e97fSKari Argillander /* True - Keep preallocated. */
6414342306fSKonstantin Komarov err = attr_set_size(ni, ATTR_DATA, NULL, 0,
6424342306fSKonstantin Komarov &ni->file.run, i_size, &ni->i_valid,
6434342306fSKonstantin Komarov true, NULL);
6444342306fSKonstantin Komarov ni_unlock(ni);
645c28efa87SKonstantin Komarov if (err)
646c28efa87SKonstantin Komarov goto out;
647ad26a9c8SKonstantin Komarov } else if (new_size > i_size) {
648f4cf29c6SKonstantin Komarov i_size_write(inode, new_size);
6494342306fSKonstantin Komarov }
6504342306fSKonstantin Komarov }
6514342306fSKonstantin Komarov
6527e0aff0aSKonstantin Komarov ok:
653c28efa87SKonstantin Komarov err = file_modified(file);
654c28efa87SKonstantin Komarov if (err)
655c28efa87SKonstantin Komarov goto out;
656c28efa87SKonstantin Komarov
6574342306fSKonstantin Komarov out:
658e4d2f4fdSKonstantin Komarov if (map_locked)
659e4d2f4fdSKonstantin Komarov filemap_invalidate_unlock(mapping);
6604342306fSKonstantin Komarov
6614342306fSKonstantin Komarov if (!err) {
6623d65c46fSJeff Layton inode->i_mtime = inode_set_ctime_current(inode);
6634342306fSKonstantin Komarov mark_inode_dirty(inode);
6644342306fSKonstantin Komarov }
6654342306fSKonstantin Komarov
6664342306fSKonstantin Komarov inode_unlock(inode);
6674342306fSKonstantin Komarov return err;
6684342306fSKonstantin Komarov }
6694342306fSKonstantin Komarov
6704342306fSKonstantin Komarov /*
671e8b8e97fSKari Argillander * ntfs3_setattr - inode_operations::setattr
6724342306fSKonstantin Komarov */
ntfs3_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)673c1632a0fSChristian Brauner int ntfs3_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
6744342306fSKonstantin Komarov struct iattr *attr)
6754342306fSKonstantin Komarov {
6764342306fSKonstantin Komarov struct inode *inode = d_inode(dentry);
6774342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
6784342306fSKonstantin Komarov u32 ia_valid = attr->ia_valid;
6794342306fSKonstantin Komarov umode_t mode = inode->i_mode;
6804342306fSKonstantin Komarov int err;
6814342306fSKonstantin Komarov
682f73f9397SKonstantin Komarov if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
683f73f9397SKonstantin Komarov return -EIO;
684f73f9397SKonstantin Komarov
685c1632a0fSChristian Brauner err = setattr_prepare(idmap, dentry, attr);
6864342306fSKonstantin Komarov if (err)
6874342306fSKonstantin Komarov goto out;
6884342306fSKonstantin Komarov
6894342306fSKonstantin Komarov if (ia_valid & ATTR_SIZE) {
690ad26a9c8SKonstantin Komarov loff_t newsize, oldsize;
6914342306fSKonstantin Komarov
6924342306fSKonstantin Komarov if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
693e8b8e97fSKari Argillander /* Should never be here, see ntfs_file_open(). */
6944342306fSKonstantin Komarov err = -EOPNOTSUPP;
6954342306fSKonstantin Komarov goto out;
6964342306fSKonstantin Komarov }
6974342306fSKonstantin Komarov inode_dio_wait(inode);
698f4cf29c6SKonstantin Komarov oldsize = i_size_read(inode);
699ad26a9c8SKonstantin Komarov newsize = attr->ia_size;
7004342306fSKonstantin Komarov
701ad26a9c8SKonstantin Komarov if (newsize <= oldsize)
702ad26a9c8SKonstantin Komarov err = ntfs_truncate(inode, newsize);
703ad26a9c8SKonstantin Komarov else
704ad26a9c8SKonstantin Komarov err = ntfs_extend(inode, newsize, 0, NULL);
7054342306fSKonstantin Komarov
7064342306fSKonstantin Komarov if (err)
7074342306fSKonstantin Komarov goto out;
7084342306fSKonstantin Komarov
7094342306fSKonstantin Komarov ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
710f4cf29c6SKonstantin Komarov i_size_write(inode, newsize);
7114342306fSKonstantin Komarov }
7124342306fSKonstantin Komarov
713c1632a0fSChristian Brauner setattr_copy(idmap, inode, attr);
7144342306fSKonstantin Komarov
7154342306fSKonstantin Komarov if (mode != inode->i_mode) {
71613e83a49SChristian Brauner err = ntfs_acl_chmod(idmap, dentry);
7174342306fSKonstantin Komarov if (err)
7184342306fSKonstantin Komarov goto out;
7194342306fSKonstantin Komarov
720e8b8e97fSKari Argillander /* Linux 'w' -> Windows 'ro'. */
7214342306fSKonstantin Komarov if (0222 & inode->i_mode)
7224342306fSKonstantin Komarov ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
7234342306fSKonstantin Komarov else
7244342306fSKonstantin Komarov ni->std_fa |= FILE_ATTRIBUTE_READONLY;
7254342306fSKonstantin Komarov }
7264342306fSKonstantin Komarov
7274342306fSKonstantin Komarov if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
7281842fbc8SKonstantin Komarov ntfs_save_wsl_perm(inode, NULL);
7294342306fSKonstantin Komarov mark_inode_dirty(inode);
7304342306fSKonstantin Komarov out:
7314342306fSKonstantin Komarov return err;
7324342306fSKonstantin Komarov }
7334342306fSKonstantin Komarov
ntfs_file_read_iter(struct kiocb * iocb,struct iov_iter * iter)7344342306fSKonstantin Komarov static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
7354342306fSKonstantin Komarov {
7364342306fSKonstantin Komarov struct file *file = iocb->ki_filp;
7374342306fSKonstantin Komarov struct inode *inode = file->f_mapping->host;
7384342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
7394342306fSKonstantin Komarov
740f73f9397SKonstantin Komarov if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
741f73f9397SKonstantin Komarov return -EIO;
742f73f9397SKonstantin Komarov
7434342306fSKonstantin Komarov if (is_encrypted(ni)) {
7444342306fSKonstantin Komarov ntfs_inode_warn(inode, "encrypted i/o not supported");
7454342306fSKonstantin Komarov return -EOPNOTSUPP;
7464342306fSKonstantin Komarov }
7474342306fSKonstantin Komarov
7484342306fSKonstantin Komarov if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
7494342306fSKonstantin Komarov ntfs_inode_warn(inode, "direct i/o + compressed not supported");
7504342306fSKonstantin Komarov return -EOPNOTSUPP;
7514342306fSKonstantin Komarov }
7524342306fSKonstantin Komarov
7534342306fSKonstantin Komarov #ifndef CONFIG_NTFS3_LZX_XPRESS
7544342306fSKonstantin Komarov if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
7554342306fSKonstantin Komarov ntfs_inode_warn(
7564342306fSKonstantin Komarov inode,
7574342306fSKonstantin Komarov "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
7584342306fSKonstantin Komarov return -EOPNOTSUPP;
7594342306fSKonstantin Komarov }
7604342306fSKonstantin Komarov #endif
7614342306fSKonstantin Komarov
7624342306fSKonstantin Komarov if (is_dedup(ni)) {
7634342306fSKonstantin Komarov ntfs_inode_warn(inode, "read deduplicated not supported");
7644342306fSKonstantin Komarov return -EOPNOTSUPP;
7654342306fSKonstantin Komarov }
7664342306fSKonstantin Komarov
767dd854e4bSKari Argillander return generic_file_read_iter(iocb, iter);
7684342306fSKonstantin Komarov }
7694342306fSKonstantin Komarov
ntfs_file_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)77051494398SDavid Howells static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
771f684073cSKonstantin Komarov struct pipe_inode_info *pipe, size_t len,
772f684073cSKonstantin Komarov unsigned int flags)
77351494398SDavid Howells {
77451494398SDavid Howells struct inode *inode = in->f_mapping->host;
77551494398SDavid Howells struct ntfs_inode *ni = ntfs_i(inode);
77651494398SDavid Howells
777f73f9397SKonstantin Komarov if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
778f73f9397SKonstantin Komarov return -EIO;
779f73f9397SKonstantin Komarov
78051494398SDavid Howells if (is_encrypted(ni)) {
78151494398SDavid Howells ntfs_inode_warn(inode, "encrypted i/o not supported");
78251494398SDavid Howells return -EOPNOTSUPP;
78351494398SDavid Howells }
78451494398SDavid Howells
78551494398SDavid Howells #ifndef CONFIG_NTFS3_LZX_XPRESS
78651494398SDavid Howells if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
78751494398SDavid Howells ntfs_inode_warn(
78851494398SDavid Howells inode,
78951494398SDavid Howells "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
79051494398SDavid Howells return -EOPNOTSUPP;
79151494398SDavid Howells }
79251494398SDavid Howells #endif
79351494398SDavid Howells
79451494398SDavid Howells if (is_dedup(ni)) {
79551494398SDavid Howells ntfs_inode_warn(inode, "read deduplicated not supported");
79651494398SDavid Howells return -EOPNOTSUPP;
79751494398SDavid Howells }
79851494398SDavid Howells
7992cb1e089SDavid Howells return filemap_splice_read(in, ppos, pipe, len, flags);
80051494398SDavid Howells }
80151494398SDavid Howells
802e8b8e97fSKari Argillander /*
803e8b8e97fSKari Argillander * ntfs_get_frame_pages
804e8b8e97fSKari Argillander *
805e8b8e97fSKari Argillander * Return: Array of locked pages.
806e8b8e97fSKari Argillander */
ntfs_get_frame_pages(struct address_space * mapping,pgoff_t index,struct page ** pages,u32 pages_per_frame,bool * frame_uptodate)8074342306fSKonstantin Komarov static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
8084342306fSKonstantin Komarov struct page **pages, u32 pages_per_frame,
8094342306fSKonstantin Komarov bool *frame_uptodate)
8104342306fSKonstantin Komarov {
8114342306fSKonstantin Komarov gfp_t gfp_mask = mapping_gfp_mask(mapping);
8124342306fSKonstantin Komarov u32 npages;
8134342306fSKonstantin Komarov
8144342306fSKonstantin Komarov *frame_uptodate = true;
8154342306fSKonstantin Komarov
8164342306fSKonstantin Komarov for (npages = 0; npages < pages_per_frame; npages++, index++) {
8174342306fSKonstantin Komarov struct page *page;
8184342306fSKonstantin Komarov
8194342306fSKonstantin Komarov page = find_or_create_page(mapping, index, gfp_mask);
8204342306fSKonstantin Komarov if (!page) {
8214342306fSKonstantin Komarov while (npages--) {
8224342306fSKonstantin Komarov page = pages[npages];
8234342306fSKonstantin Komarov unlock_page(page);
8244342306fSKonstantin Komarov put_page(page);
8254342306fSKonstantin Komarov }
8264342306fSKonstantin Komarov
8274342306fSKonstantin Komarov return -ENOMEM;
8284342306fSKonstantin Komarov }
8294342306fSKonstantin Komarov
8304342306fSKonstantin Komarov if (!PageUptodate(page))
8314342306fSKonstantin Komarov *frame_uptodate = false;
8324342306fSKonstantin Komarov
8334342306fSKonstantin Komarov pages[npages] = page;
8344342306fSKonstantin Komarov }
8354342306fSKonstantin Komarov
8364342306fSKonstantin Komarov return 0;
8374342306fSKonstantin Komarov }
8384342306fSKonstantin Komarov
839e8b8e97fSKari Argillander /*
840e8b8e97fSKari Argillander * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
841e8b8e97fSKari Argillander */
ntfs_compress_write(struct kiocb * iocb,struct iov_iter * from)8424342306fSKonstantin Komarov static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
8434342306fSKonstantin Komarov {
8444342306fSKonstantin Komarov int err;
8454342306fSKonstantin Komarov struct file *file = iocb->ki_filp;
8464342306fSKonstantin Komarov size_t count = iov_iter_count(from);
8474342306fSKonstantin Komarov loff_t pos = iocb->ki_pos;
8484342306fSKonstantin Komarov struct inode *inode = file_inode(file);
849f4cf29c6SKonstantin Komarov loff_t i_size = i_size_read(inode);
8504342306fSKonstantin Komarov struct address_space *mapping = inode->i_mapping;
8514342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
8524342306fSKonstantin Komarov u64 valid = ni->i_valid;
8534342306fSKonstantin Komarov struct ntfs_sb_info *sbi = ni->mi.sbi;
8544342306fSKonstantin Komarov struct page *page, **pages = NULL;
8554342306fSKonstantin Komarov size_t written = 0;
8564342306fSKonstantin Komarov u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
8574342306fSKonstantin Komarov u32 frame_size = 1u << frame_bits;
8584342306fSKonstantin Komarov u32 pages_per_frame = frame_size >> PAGE_SHIFT;
8594342306fSKonstantin Komarov u32 ip, off;
8604342306fSKonstantin Komarov CLST frame;
8614342306fSKonstantin Komarov u64 frame_vbo;
8624342306fSKonstantin Komarov pgoff_t index;
8634342306fSKonstantin Komarov bool frame_uptodate;
8644342306fSKonstantin Komarov
8654342306fSKonstantin Komarov if (frame_size < PAGE_SIZE) {
8664342306fSKonstantin Komarov /*
8674342306fSKonstantin Komarov * frame_size == 8K if cluster 512
8684342306fSKonstantin Komarov * frame_size == 64K if cluster 4096
8694342306fSKonstantin Komarov */
8704342306fSKonstantin Komarov ntfs_inode_warn(inode, "page size is bigger than frame size");
8714342306fSKonstantin Komarov return -EOPNOTSUPP;
8724342306fSKonstantin Komarov }
8734342306fSKonstantin Komarov
874345482bcSKari Argillander pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
8754342306fSKonstantin Komarov if (!pages)
8764342306fSKonstantin Komarov return -ENOMEM;
8774342306fSKonstantin Komarov
8784342306fSKonstantin Komarov err = file_remove_privs(file);
8794342306fSKonstantin Komarov if (err)
8804342306fSKonstantin Komarov goto out;
8814342306fSKonstantin Komarov
8824342306fSKonstantin Komarov err = file_update_time(file);
8834342306fSKonstantin Komarov if (err)
8844342306fSKonstantin Komarov goto out;
8854342306fSKonstantin Komarov
886e8b8e97fSKari Argillander /* Zero range [valid : pos). */
8874342306fSKonstantin Komarov while (valid < pos) {
8884342306fSKonstantin Komarov CLST lcn, clen;
8894342306fSKonstantin Komarov
8904342306fSKonstantin Komarov frame = valid >> frame_bits;
8914342306fSKonstantin Komarov frame_vbo = valid & ~(frame_size - 1);
8924342306fSKonstantin Komarov off = valid & (frame_size - 1);
8934342306fSKonstantin Komarov
894c380b52fSKonstantin Komarov err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn,
895c380b52fSKonstantin Komarov &clen, NULL, false);
8964342306fSKonstantin Komarov if (err)
8974342306fSKonstantin Komarov goto out;
8984342306fSKonstantin Komarov
8994342306fSKonstantin Komarov if (lcn == SPARSE_LCN) {
9004342306fSKonstantin Komarov ni->i_valid = valid =
9014342306fSKonstantin Komarov frame_vbo + ((u64)clen << sbi->cluster_bits);
9024342306fSKonstantin Komarov continue;
9034342306fSKonstantin Komarov }
9044342306fSKonstantin Komarov
905e8b8e97fSKari Argillander /* Load full frame. */
9064342306fSKonstantin Komarov err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
9074342306fSKonstantin Komarov pages, pages_per_frame,
9084342306fSKonstantin Komarov &frame_uptodate);
9094342306fSKonstantin Komarov if (err)
9104342306fSKonstantin Komarov goto out;
9114342306fSKonstantin Komarov
9124342306fSKonstantin Komarov if (!frame_uptodate && off) {
9134342306fSKonstantin Komarov err = ni_read_frame(ni, frame_vbo, pages,
9144342306fSKonstantin Komarov pages_per_frame);
9154342306fSKonstantin Komarov if (err) {
9164342306fSKonstantin Komarov for (ip = 0; ip < pages_per_frame; ip++) {
9174342306fSKonstantin Komarov page = pages[ip];
9184342306fSKonstantin Komarov unlock_page(page);
9194342306fSKonstantin Komarov put_page(page);
9204342306fSKonstantin Komarov }
9214342306fSKonstantin Komarov goto out;
9224342306fSKonstantin Komarov }
9234342306fSKonstantin Komarov }
9244342306fSKonstantin Komarov
9254342306fSKonstantin Komarov ip = off >> PAGE_SHIFT;
9264342306fSKonstantin Komarov off = offset_in_page(valid);
9274342306fSKonstantin Komarov for (; ip < pages_per_frame; ip++, off = 0) {
9284342306fSKonstantin Komarov page = pages[ip];
9294342306fSKonstantin Komarov zero_user_segment(page, off, PAGE_SIZE);
9304342306fSKonstantin Komarov flush_dcache_page(page);
9314342306fSKonstantin Komarov SetPageUptodate(page);
9324342306fSKonstantin Komarov }
9334342306fSKonstantin Komarov
9344342306fSKonstantin Komarov ni_lock(ni);
9354342306fSKonstantin Komarov err = ni_write_frame(ni, pages, pages_per_frame);
9364342306fSKonstantin Komarov ni_unlock(ni);
9374342306fSKonstantin Komarov
9384342306fSKonstantin Komarov for (ip = 0; ip < pages_per_frame; ip++) {
9394342306fSKonstantin Komarov page = pages[ip];
9404342306fSKonstantin Komarov SetPageUptodate(page);
9414342306fSKonstantin Komarov unlock_page(page);
9424342306fSKonstantin Komarov put_page(page);
9434342306fSKonstantin Komarov }
9444342306fSKonstantin Komarov
9454342306fSKonstantin Komarov if (err)
9464342306fSKonstantin Komarov goto out;
9474342306fSKonstantin Komarov
9484342306fSKonstantin Komarov ni->i_valid = valid = frame_vbo + frame_size;
9494342306fSKonstantin Komarov }
9504342306fSKonstantin Komarov
951e8b8e97fSKari Argillander /* Copy user data [pos : pos + count). */
9524342306fSKonstantin Komarov while (count) {
9534342306fSKonstantin Komarov size_t copied, bytes;
9544342306fSKonstantin Komarov
9554342306fSKonstantin Komarov off = pos & (frame_size - 1);
9564342306fSKonstantin Komarov bytes = frame_size - off;
9574342306fSKonstantin Komarov if (bytes > count)
9584342306fSKonstantin Komarov bytes = count;
9594342306fSKonstantin Komarov
9604342306fSKonstantin Komarov frame_vbo = pos & ~(frame_size - 1);
9614342306fSKonstantin Komarov index = frame_vbo >> PAGE_SHIFT;
9624342306fSKonstantin Komarov
963a6294593SAndreas Gruenbacher if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
9644342306fSKonstantin Komarov err = -EFAULT;
9654342306fSKonstantin Komarov goto out;
9664342306fSKonstantin Komarov }
9674342306fSKonstantin Komarov
968e8b8e97fSKari Argillander /* Load full frame. */
9694342306fSKonstantin Komarov err = ntfs_get_frame_pages(mapping, index, pages,
9704342306fSKonstantin Komarov pages_per_frame, &frame_uptodate);
9714342306fSKonstantin Komarov if (err)
9724342306fSKonstantin Komarov goto out;
9734342306fSKonstantin Komarov
9744342306fSKonstantin Komarov if (!frame_uptodate) {
9754342306fSKonstantin Komarov loff_t to = pos + bytes;
9764342306fSKonstantin Komarov
9774342306fSKonstantin Komarov if (off || (to < i_size && (to & (frame_size - 1)))) {
9784342306fSKonstantin Komarov err = ni_read_frame(ni, frame_vbo, pages,
9794342306fSKonstantin Komarov pages_per_frame);
9804342306fSKonstantin Komarov if (err) {
9814342306fSKonstantin Komarov for (ip = 0; ip < pages_per_frame;
9824342306fSKonstantin Komarov ip++) {
9834342306fSKonstantin Komarov page = pages[ip];
9844342306fSKonstantin Komarov unlock_page(page);
9854342306fSKonstantin Komarov put_page(page);
9864342306fSKonstantin Komarov }
9874342306fSKonstantin Komarov goto out;
9884342306fSKonstantin Komarov }
9894342306fSKonstantin Komarov }
9904342306fSKonstantin Komarov }
9914342306fSKonstantin Komarov
9924342306fSKonstantin Komarov WARN_ON(!bytes);
9934342306fSKonstantin Komarov copied = 0;
9944342306fSKonstantin Komarov ip = off >> PAGE_SHIFT;
9954342306fSKonstantin Komarov off = offset_in_page(pos);
9964342306fSKonstantin Komarov
997e8b8e97fSKari Argillander /* Copy user data to pages. */
9984342306fSKonstantin Komarov for (;;) {
9994342306fSKonstantin Komarov size_t cp, tail = PAGE_SIZE - off;
10004342306fSKonstantin Komarov
10014342306fSKonstantin Komarov page = pages[ip];
10024342306fSKonstantin Komarov cp = copy_page_from_iter_atomic(page, off,
10034342306fSKonstantin Komarov min(tail, bytes), from);
10044342306fSKonstantin Komarov flush_dcache_page(page);
10054342306fSKonstantin Komarov
10064342306fSKonstantin Komarov copied += cp;
10074342306fSKonstantin Komarov bytes -= cp;
10084342306fSKonstantin Komarov if (!bytes || !cp)
10094342306fSKonstantin Komarov break;
10104342306fSKonstantin Komarov
10114342306fSKonstantin Komarov if (cp < tail) {
10124342306fSKonstantin Komarov off += cp;
10134342306fSKonstantin Komarov } else {
10144342306fSKonstantin Komarov ip++;
10154342306fSKonstantin Komarov off = 0;
10164342306fSKonstantin Komarov }
10174342306fSKonstantin Komarov }
10184342306fSKonstantin Komarov
10194342306fSKonstantin Komarov ni_lock(ni);
10204342306fSKonstantin Komarov err = ni_write_frame(ni, pages, pages_per_frame);
10214342306fSKonstantin Komarov ni_unlock(ni);
10224342306fSKonstantin Komarov
10234342306fSKonstantin Komarov for (ip = 0; ip < pages_per_frame; ip++) {
10244342306fSKonstantin Komarov page = pages[ip];
10254342306fSKonstantin Komarov ClearPageDirty(page);
10264342306fSKonstantin Komarov SetPageUptodate(page);
10274342306fSKonstantin Komarov unlock_page(page);
10284342306fSKonstantin Komarov put_page(page);
10294342306fSKonstantin Komarov }
10304342306fSKonstantin Komarov
10314342306fSKonstantin Komarov if (err)
10324342306fSKonstantin Komarov goto out;
10334342306fSKonstantin Komarov
10344342306fSKonstantin Komarov /*
10354342306fSKonstantin Komarov * We can loop for a long time in here. Be nice and allow
10364342306fSKonstantin Komarov * us to schedule out to avoid softlocking if preempt
10374342306fSKonstantin Komarov * is disabled.
10384342306fSKonstantin Komarov */
10394342306fSKonstantin Komarov cond_resched();
10404342306fSKonstantin Komarov
10414342306fSKonstantin Komarov pos += copied;
10424342306fSKonstantin Komarov written += copied;
10434342306fSKonstantin Komarov
10444342306fSKonstantin Komarov count = iov_iter_count(from);
10454342306fSKonstantin Komarov }
10464342306fSKonstantin Komarov
10474342306fSKonstantin Komarov out:
1048195c52bdSKari Argillander kfree(pages);
10494342306fSKonstantin Komarov
10504342306fSKonstantin Komarov if (err < 0)
10514342306fSKonstantin Komarov return err;
10524342306fSKonstantin Komarov
10534342306fSKonstantin Komarov iocb->ki_pos += written;
10544342306fSKonstantin Komarov if (iocb->ki_pos > ni->i_valid)
10554342306fSKonstantin Komarov ni->i_valid = iocb->ki_pos;
1056ded8bf5bSKonstantin Komarov if (iocb->ki_pos > i_size)
1057ded8bf5bSKonstantin Komarov i_size_write(inode, iocb->ki_pos);
10584342306fSKonstantin Komarov
10594342306fSKonstantin Komarov return written;
10604342306fSKonstantin Komarov }
10614342306fSKonstantin Komarov
10624342306fSKonstantin Komarov /*
1063e8b8e97fSKari Argillander * ntfs_file_write_iter - file_operations::write_iter
10644342306fSKonstantin Komarov */
ntfs_file_write_iter(struct kiocb * iocb,struct iov_iter * from)10654342306fSKonstantin Komarov static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
10664342306fSKonstantin Komarov {
10674342306fSKonstantin Komarov struct file *file = iocb->ki_filp;
10684342306fSKonstantin Komarov struct address_space *mapping = file->f_mapping;
10694342306fSKonstantin Komarov struct inode *inode = mapping->host;
10704342306fSKonstantin Komarov ssize_t ret;
1071c28efa87SKonstantin Komarov int err;
10724342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
10734342306fSKonstantin Komarov
1074f73f9397SKonstantin Komarov if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
1075f73f9397SKonstantin Komarov return -EIO;
1076f73f9397SKonstantin Komarov
10774342306fSKonstantin Komarov if (is_encrypted(ni)) {
10784342306fSKonstantin Komarov ntfs_inode_warn(inode, "encrypted i/o not supported");
10794342306fSKonstantin Komarov return -EOPNOTSUPP;
10804342306fSKonstantin Komarov }
10814342306fSKonstantin Komarov
10824342306fSKonstantin Komarov if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
10834342306fSKonstantin Komarov ntfs_inode_warn(inode, "direct i/o + compressed not supported");
10844342306fSKonstantin Komarov return -EOPNOTSUPP;
10854342306fSKonstantin Komarov }
10864342306fSKonstantin Komarov
10874342306fSKonstantin Komarov if (is_dedup(ni)) {
10884342306fSKonstantin Komarov ntfs_inode_warn(inode, "write into deduplicated not supported");
10894342306fSKonstantin Komarov return -EOPNOTSUPP;
10904342306fSKonstantin Komarov }
10914342306fSKonstantin Komarov
10924342306fSKonstantin Komarov if (!inode_trylock(inode)) {
10934342306fSKonstantin Komarov if (iocb->ki_flags & IOCB_NOWAIT)
10944342306fSKonstantin Komarov return -EAGAIN;
10954342306fSKonstantin Komarov inode_lock(inode);
10964342306fSKonstantin Komarov }
10974342306fSKonstantin Komarov
10984342306fSKonstantin Komarov ret = generic_write_checks(iocb, from);
10994342306fSKonstantin Komarov if (ret <= 0)
11004342306fSKonstantin Komarov goto out;
11014342306fSKonstantin Komarov
1102c28efa87SKonstantin Komarov err = file_modified(iocb->ki_filp);
1103c28efa87SKonstantin Komarov if (err) {
1104c28efa87SKonstantin Komarov ret = err;
1105c28efa87SKonstantin Komarov goto out;
1106c28efa87SKonstantin Komarov }
1107c28efa87SKonstantin Komarov
11084342306fSKonstantin Komarov if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
1109d3624466SKonstantin Komarov /* Should never be here, see ntfs_file_open(). */
11104342306fSKonstantin Komarov ret = -EOPNOTSUPP;
11114342306fSKonstantin Komarov goto out;
11124342306fSKonstantin Komarov }
11134342306fSKonstantin Komarov
11144342306fSKonstantin Komarov ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
11154342306fSKonstantin Komarov if (ret)
11164342306fSKonstantin Komarov goto out;
11174342306fSKonstantin Komarov
111896de65a9SKonstantin Komarov ret = is_compressed(ni) ? ntfs_compress_write(iocb, from) :
111996de65a9SKonstantin Komarov __generic_file_write_iter(iocb, from);
11204342306fSKonstantin Komarov
11214342306fSKonstantin Komarov out:
11224342306fSKonstantin Komarov inode_unlock(inode);
11234342306fSKonstantin Komarov
11244342306fSKonstantin Komarov if (ret > 0)
11254342306fSKonstantin Komarov ret = generic_write_sync(iocb, ret);
11264342306fSKonstantin Komarov
11274342306fSKonstantin Komarov return ret;
11284342306fSKonstantin Komarov }
11294342306fSKonstantin Komarov
11304342306fSKonstantin Komarov /*
1131e8b8e97fSKari Argillander * ntfs_file_open - file_operations::open
11324342306fSKonstantin Komarov */
ntfs_file_open(struct inode * inode,struct file * file)11334342306fSKonstantin Komarov int ntfs_file_open(struct inode *inode, struct file *file)
11344342306fSKonstantin Komarov {
11354342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
11364342306fSKonstantin Komarov
1137f73f9397SKonstantin Komarov if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
1138f73f9397SKonstantin Komarov return -EIO;
1139f73f9397SKonstantin Komarov
11404342306fSKonstantin Komarov if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
11414342306fSKonstantin Komarov (file->f_flags & O_DIRECT))) {
11424342306fSKonstantin Komarov return -EOPNOTSUPP;
11434342306fSKonstantin Komarov }
11444342306fSKonstantin Komarov
1145e8b8e97fSKari Argillander /* Decompress "external compressed" file if opened for rw. */
11464342306fSKonstantin Komarov if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
11474342306fSKonstantin Komarov (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
11484342306fSKonstantin Komarov #ifdef CONFIG_NTFS3_LZX_XPRESS
11494342306fSKonstantin Komarov int err = ni_decompress_file(ni);
11504342306fSKonstantin Komarov
11514342306fSKonstantin Komarov if (err)
11524342306fSKonstantin Komarov return err;
11534342306fSKonstantin Komarov #else
11544342306fSKonstantin Komarov ntfs_inode_warn(
11554342306fSKonstantin Komarov inode,
11564342306fSKonstantin Komarov "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
11574342306fSKonstantin Komarov return -EOPNOTSUPP;
11584342306fSKonstantin Komarov #endif
11594342306fSKonstantin Komarov }
11604342306fSKonstantin Komarov
11614342306fSKonstantin Komarov return generic_file_open(inode, file);
11624342306fSKonstantin Komarov }
11634342306fSKonstantin Komarov
11644342306fSKonstantin Komarov /*
1165e8b8e97fSKari Argillander * ntfs_file_release - file_operations::release
11664342306fSKonstantin Komarov */
ntfs_file_release(struct inode * inode,struct file * file)11674342306fSKonstantin Komarov static int ntfs_file_release(struct inode *inode, struct file *file)
11684342306fSKonstantin Komarov {
11694342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
11704342306fSKonstantin Komarov struct ntfs_sb_info *sbi = ni->mi.sbi;
11714342306fSKonstantin Komarov int err = 0;
11724342306fSKonstantin Komarov
1173e8b8e97fSKari Argillander /* If we are last writer on the inode, drop the block reservation. */
117496de65a9SKonstantin Komarov if (sbi->options->prealloc &&
117596de65a9SKonstantin Komarov ((file->f_mode & FMODE_WRITE) &&
1176*550ef40fSKonstantin Komarov atomic_read(&inode->i_writecount) == 1)
1177*550ef40fSKonstantin Komarov /*
1178*550ef40fSKonstantin Komarov * The only file when inode->i_fop = &ntfs_file_operations and
1179*550ef40fSKonstantin Komarov * init_rwsem(&ni->file.run_lock) is not called explicitly is MFT.
1180*550ef40fSKonstantin Komarov *
1181*550ef40fSKonstantin Komarov * Add additional check here.
1182*550ef40fSKonstantin Komarov */
1183*550ef40fSKonstantin Komarov && inode->i_ino != MFT_REC_MFT) {
11844342306fSKonstantin Komarov ni_lock(ni);
11854342306fSKonstantin Komarov down_write(&ni->file.run_lock);
11864342306fSKonstantin Komarov
11874342306fSKonstantin Komarov err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
1188f4cf29c6SKonstantin Komarov i_size_read(inode), &ni->i_valid, false,
1189f4cf29c6SKonstantin Komarov NULL);
11904342306fSKonstantin Komarov
11914342306fSKonstantin Komarov up_write(&ni->file.run_lock);
11924342306fSKonstantin Komarov ni_unlock(ni);
11934342306fSKonstantin Komarov }
11944342306fSKonstantin Komarov return err;
11954342306fSKonstantin Komarov }
11964342306fSKonstantin Komarov
1197e8b8e97fSKari Argillander /*
1198e8b8e97fSKari Argillander * ntfs_fiemap - file_operations::fiemap
1199e8b8e97fSKari Argillander */
ntfs_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,__u64 start,__u64 len)12004342306fSKonstantin Komarov int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
12014342306fSKonstantin Komarov __u64 start, __u64 len)
12024342306fSKonstantin Komarov {
12034342306fSKonstantin Komarov int err;
12044342306fSKonstantin Komarov struct ntfs_inode *ni = ntfs_i(inode);
12054342306fSKonstantin Komarov
1206d4e8e135SKari Argillander err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
1207d4e8e135SKari Argillander if (err)
1208d4e8e135SKari Argillander return err;
12094342306fSKonstantin Komarov
12104342306fSKonstantin Komarov ni_lock(ni);
12114342306fSKonstantin Komarov
12124342306fSKonstantin Komarov err = ni_fiemap(ni, fieinfo, start, len);
12134342306fSKonstantin Komarov
12144342306fSKonstantin Komarov ni_unlock(ni);
12154342306fSKonstantin Komarov
12164342306fSKonstantin Komarov return err;
12174342306fSKonstantin Komarov }
12184342306fSKonstantin Komarov
12194342306fSKonstantin Komarov // clang-format off
12204342306fSKonstantin Komarov const struct inode_operations ntfs_file_inode_operations = {
12214342306fSKonstantin Komarov .getattr = ntfs_getattr,
12224342306fSKonstantin Komarov .setattr = ntfs3_setattr,
12234342306fSKonstantin Komarov .listxattr = ntfs_listxattr,
122475c5e0c9SKonstantin Komarov .get_acl = ntfs_get_acl,
12254342306fSKonstantin Komarov .set_acl = ntfs_set_acl,
12264342306fSKonstantin Komarov .fiemap = ntfs_fiemap,
12274342306fSKonstantin Komarov };
12284342306fSKonstantin Komarov
12294342306fSKonstantin Komarov const struct file_operations ntfs_file_operations = {
12304342306fSKonstantin Komarov .llseek = generic_file_llseek,
12314342306fSKonstantin Komarov .read_iter = ntfs_file_read_iter,
12324342306fSKonstantin Komarov .write_iter = ntfs_file_write_iter,
12334342306fSKonstantin Komarov .unlocked_ioctl = ntfs_ioctl,
12344342306fSKonstantin Komarov #ifdef CONFIG_COMPAT
12354342306fSKonstantin Komarov .compat_ioctl = ntfs_compat_ioctl,
12364342306fSKonstantin Komarov #endif
123751494398SDavid Howells .splice_read = ntfs_file_splice_read,
12384342306fSKonstantin Komarov .mmap = ntfs_file_mmap,
12394342306fSKonstantin Komarov .open = ntfs_file_open,
12404342306fSKonstantin Komarov .fsync = generic_file_fsync,
12414342306fSKonstantin Komarov .splice_write = iter_file_splice_write,
12424342306fSKonstantin Komarov .fallocate = ntfs_fallocate,
12434342306fSKonstantin Komarov .release = ntfs_file_release,
12444342306fSKonstantin Komarov };
12454342306fSKonstantin Komarov // clang-format on
1246