Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *
0004  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
0005  *
0006  *  Regular file handling primitives for NTFS-based filesystems.
0007  *
0008  */
0009 
0010 #include <linux/backing-dev.h>
0011 #include <linux/blkdev.h>
0012 #include <linux/buffer_head.h>
0013 #include <linux/compat.h>
0014 #include <linux/falloc.h>
0015 #include <linux/fiemap.h>
0016 
0017 #include "debug.h"
0018 #include "ntfs.h"
0019 #include "ntfs_fs.h"
0020 
0021 static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg)
0022 {
0023     struct fstrim_range __user *user_range;
0024     struct fstrim_range range;
0025     int err;
0026 
0027     if (!capable(CAP_SYS_ADMIN))
0028         return -EPERM;
0029 
0030     if (!bdev_max_discard_sectors(sbi->sb->s_bdev))
0031         return -EOPNOTSUPP;
0032 
0033     user_range = (struct fstrim_range __user *)arg;
0034     if (copy_from_user(&range, user_range, sizeof(range)))
0035         return -EFAULT;
0036 
0037     range.minlen = max_t(u32, range.minlen,
0038                  bdev_discard_granularity(sbi->sb->s_bdev));
0039 
0040     err = ntfs_trim_fs(sbi, &range);
0041     if (err < 0)
0042         return err;
0043 
0044     if (copy_to_user(user_range, &range, sizeof(range)))
0045         return -EFAULT;
0046 
0047     return 0;
0048 }
0049 
0050 static long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg)
0051 {
0052     struct inode *inode = file_inode(filp);
0053     struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
0054 
0055     switch (cmd) {
0056     case FITRIM:
0057         return ntfs_ioctl_fitrim(sbi, arg);
0058     }
0059     return -ENOTTY; /* Inappropriate ioctl for device. */
0060 }
0061 
0062 #ifdef CONFIG_COMPAT
0063 static long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg)
0064 
0065 {
0066     return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
0067 }
0068 #endif
0069 
0070 /*
0071  * ntfs_getattr - inode_operations::getattr
0072  */
0073 int ntfs_getattr(struct user_namespace *mnt_userns, const struct path *path,
0074          struct kstat *stat, u32 request_mask, u32 flags)
0075 {
0076     struct inode *inode = d_inode(path->dentry);
0077     struct ntfs_inode *ni = ntfs_i(inode);
0078 
0079     if (is_compressed(ni))
0080         stat->attributes |= STATX_ATTR_COMPRESSED;
0081 
0082     if (is_encrypted(ni))
0083         stat->attributes |= STATX_ATTR_ENCRYPTED;
0084 
0085     stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED;
0086 
0087     generic_fillattr(mnt_userns, inode, stat);
0088 
0089     stat->result_mask |= STATX_BTIME;
0090     stat->btime = ni->i_crtime;
0091     stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */
0092 
0093     return 0;
0094 }
0095 
0096 static int ntfs_extend_initialized_size(struct file *file,
0097                     struct ntfs_inode *ni,
0098                     const loff_t valid,
0099                     const loff_t new_valid)
0100 {
0101     struct inode *inode = &ni->vfs_inode;
0102     struct address_space *mapping = inode->i_mapping;
0103     struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
0104     loff_t pos = valid;
0105     int err;
0106 
0107     if (is_resident(ni)) {
0108         ni->i_valid = new_valid;
0109         return 0;
0110     }
0111 
0112     WARN_ON(is_compressed(ni));
0113     WARN_ON(valid >= new_valid);
0114 
0115     for (;;) {
0116         u32 zerofrom, len;
0117         struct page *page;
0118         u8 bits;
0119         CLST vcn, lcn, clen;
0120 
0121         if (is_sparsed(ni)) {
0122             bits = sbi->cluster_bits;
0123             vcn = pos >> bits;
0124 
0125             err = attr_data_get_block(ni, vcn, 0, &lcn, &clen,
0126                           NULL);
0127             if (err)
0128                 goto out;
0129 
0130             if (lcn == SPARSE_LCN) {
0131                 loff_t vbo = (loff_t)vcn << bits;
0132                 loff_t to = vbo + ((loff_t)clen << bits);
0133 
0134                 if (to <= new_valid) {
0135                     ni->i_valid = to;
0136                     pos = to;
0137                     goto next;
0138                 }
0139 
0140                 if (vbo < pos) {
0141                     pos = vbo;
0142                 } else {
0143                     to = (new_valid >> bits) << bits;
0144                     if (pos < to) {
0145                         ni->i_valid = to;
0146                         pos = to;
0147                         goto next;
0148                     }
0149                 }
0150             }
0151         }
0152 
0153         zerofrom = pos & (PAGE_SIZE - 1);
0154         len = PAGE_SIZE - zerofrom;
0155 
0156         if (pos + len > new_valid)
0157             len = new_valid - pos;
0158 
0159         err = ntfs_write_begin(file, mapping, pos, len, &page, NULL);
0160         if (err)
0161             goto out;
0162 
0163         zero_user_segment(page, zerofrom, PAGE_SIZE);
0164 
0165         /* This function in any case puts page. */
0166         err = ntfs_write_end(file, mapping, pos, len, len, page, NULL);
0167         if (err < 0)
0168             goto out;
0169         pos += len;
0170 
0171 next:
0172         if (pos >= new_valid)
0173             break;
0174 
0175         balance_dirty_pages_ratelimited(mapping);
0176         cond_resched();
0177     }
0178 
0179     return 0;
0180 
0181 out:
0182     ni->i_valid = valid;
0183     ntfs_inode_warn(inode, "failed to extend initialized size to %llx.",
0184             new_valid);
0185     return err;
0186 }
0187 
0188 /*
0189  * ntfs_zero_range - Helper function for punch_hole.
0190  *
0191  * It zeroes a range [vbo, vbo_to).
0192  */
0193 static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to)
0194 {
0195     int err = 0;
0196     struct address_space *mapping = inode->i_mapping;
0197     u32 blocksize = 1 << inode->i_blkbits;
0198     pgoff_t idx = vbo >> PAGE_SHIFT;
0199     u32 z_start = vbo & (PAGE_SIZE - 1);
0200     pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT;
0201     loff_t page_off;
0202     struct buffer_head *head, *bh;
0203     u32 bh_next, bh_off, z_end;
0204     sector_t iblock;
0205     struct page *page;
0206 
0207     for (; idx < idx_end; idx += 1, z_start = 0) {
0208         page_off = (loff_t)idx << PAGE_SHIFT;
0209         z_end = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off)
0210                             : PAGE_SIZE;
0211         iblock = page_off >> inode->i_blkbits;
0212 
0213         page = find_or_create_page(mapping, idx,
0214                        mapping_gfp_constraint(mapping,
0215                                   ~__GFP_FS));
0216         if (!page)
0217             return -ENOMEM;
0218 
0219         if (!page_has_buffers(page))
0220             create_empty_buffers(page, blocksize, 0);
0221 
0222         bh = head = page_buffers(page);
0223         bh_off = 0;
0224         do {
0225             bh_next = bh_off + blocksize;
0226 
0227             if (bh_next <= z_start || bh_off >= z_end)
0228                 continue;
0229 
0230             if (!buffer_mapped(bh)) {
0231                 ntfs_get_block(inode, iblock, bh, 0);
0232                 /* Unmapped? It's a hole - nothing to do. */
0233                 if (!buffer_mapped(bh))
0234                     continue;
0235             }
0236 
0237             /* Ok, it's mapped. Make sure it's up-to-date. */
0238             if (PageUptodate(page))
0239                 set_buffer_uptodate(bh);
0240 
0241             if (!buffer_uptodate(bh)) {
0242                 lock_buffer(bh);
0243                 bh->b_end_io = end_buffer_read_sync;
0244                 get_bh(bh);
0245                 submit_bh(REQ_OP_READ, bh);
0246 
0247                 wait_on_buffer(bh);
0248                 if (!buffer_uptodate(bh)) {
0249                     unlock_page(page);
0250                     put_page(page);
0251                     err = -EIO;
0252                     goto out;
0253                 }
0254             }
0255 
0256             mark_buffer_dirty(bh);
0257 
0258         } while (bh_off = bh_next, iblock += 1,
0259              head != (bh = bh->b_this_page));
0260 
0261         zero_user_segment(page, z_start, z_end);
0262 
0263         unlock_page(page);
0264         put_page(page);
0265         cond_resched();
0266     }
0267 out:
0268     mark_inode_dirty(inode);
0269     return err;
0270 }
0271 
0272 /*
0273  * ntfs_sparse_cluster - Helper function to zero a new allocated clusters.
0274  *
0275  * NOTE: 512 <= cluster size <= 2M
0276  */
0277 void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn,
0278              CLST len)
0279 {
0280     struct address_space *mapping = inode->i_mapping;
0281     struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
0282     u64 vbo = (u64)vcn << sbi->cluster_bits;
0283     u64 bytes = (u64)len << sbi->cluster_bits;
0284     u32 blocksize = 1 << inode->i_blkbits;
0285     pgoff_t idx0 = page0 ? page0->index : -1;
0286     loff_t vbo_clst = vbo & sbi->cluster_mask_inv;
0287     loff_t end = ntfs_up_cluster(sbi, vbo + bytes);
0288     pgoff_t idx = vbo_clst >> PAGE_SHIFT;
0289     u32 from = vbo_clst & (PAGE_SIZE - 1);
0290     pgoff_t idx_end = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
0291     loff_t page_off;
0292     u32 to;
0293     bool partial;
0294     struct page *page;
0295 
0296     for (; idx < idx_end; idx += 1, from = 0) {
0297         page = idx == idx0 ? page0 : grab_cache_page(mapping, idx);
0298 
0299         if (!page)
0300             continue;
0301 
0302         page_off = (loff_t)idx << PAGE_SHIFT;
0303         to = (page_off + PAGE_SIZE) > end ? (end - page_off)
0304                           : PAGE_SIZE;
0305         partial = false;
0306 
0307         if ((from || PAGE_SIZE != to) &&
0308             likely(!page_has_buffers(page))) {
0309             create_empty_buffers(page, blocksize, 0);
0310         }
0311 
0312         if (page_has_buffers(page)) {
0313             struct buffer_head *head, *bh;
0314             u32 bh_off = 0;
0315 
0316             bh = head = page_buffers(page);
0317             do {
0318                 u32 bh_next = bh_off + blocksize;
0319 
0320                 if (from <= bh_off && bh_next <= to) {
0321                     set_buffer_uptodate(bh);
0322                     mark_buffer_dirty(bh);
0323                 } else if (!buffer_uptodate(bh)) {
0324                     partial = true;
0325                 }
0326                 bh_off = bh_next;
0327             } while (head != (bh = bh->b_this_page));
0328         }
0329 
0330         zero_user_segment(page, from, to);
0331 
0332         if (!partial) {
0333             if (!PageUptodate(page))
0334                 SetPageUptodate(page);
0335             set_page_dirty(page);
0336         }
0337 
0338         if (idx != idx0) {
0339             unlock_page(page);
0340             put_page(page);
0341         }
0342         cond_resched();
0343     }
0344     mark_inode_dirty(inode);
0345 }
0346 
0347 /*
0348  * ntfs_file_mmap - file_operations::mmap
0349  */
0350 static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma)
0351 {
0352     struct address_space *mapping = file->f_mapping;
0353     struct inode *inode = mapping->host;
0354     struct ntfs_inode *ni = ntfs_i(inode);
0355     u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT);
0356     bool rw = vma->vm_flags & VM_WRITE;
0357     int err;
0358 
0359     if (is_encrypted(ni)) {
0360         ntfs_inode_warn(inode, "mmap encrypted not supported");
0361         return -EOPNOTSUPP;
0362     }
0363 
0364     if (is_dedup(ni)) {
0365         ntfs_inode_warn(inode, "mmap deduplicated not supported");
0366         return -EOPNOTSUPP;
0367     }
0368 
0369     if (is_compressed(ni) && rw) {
0370         ntfs_inode_warn(inode, "mmap(write) compressed not supported");
0371         return -EOPNOTSUPP;
0372     }
0373 
0374     if (rw) {
0375         u64 to = min_t(loff_t, i_size_read(inode),
0376                    from + vma->vm_end - vma->vm_start);
0377 
0378         if (is_sparsed(ni)) {
0379             /* Allocate clusters for rw map. */
0380             struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
0381             CLST lcn, len;
0382             CLST vcn = from >> sbi->cluster_bits;
0383             CLST end = bytes_to_cluster(sbi, to);
0384             bool new;
0385 
0386             for (; vcn < end; vcn += len) {
0387                 err = attr_data_get_block(ni, vcn, 1, &lcn,
0388                               &len, &new);
0389                 if (err)
0390                     goto out;
0391 
0392                 if (!new)
0393                     continue;
0394                 ntfs_sparse_cluster(inode, NULL, vcn, 1);
0395             }
0396         }
0397 
0398         if (ni->i_valid < to) {
0399             if (!inode_trylock(inode)) {
0400                 err = -EAGAIN;
0401                 goto out;
0402             }
0403             err = ntfs_extend_initialized_size(file, ni,
0404                                ni->i_valid, to);
0405             inode_unlock(inode);
0406             if (err)
0407                 goto out;
0408         }
0409     }
0410 
0411     err = generic_file_mmap(file, vma);
0412 out:
0413     return err;
0414 }
0415 
0416 static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
0417                struct file *file)
0418 {
0419     struct ntfs_inode *ni = ntfs_i(inode);
0420     struct address_space *mapping = inode->i_mapping;
0421     loff_t end = pos + count;
0422     bool extend_init = file && pos > ni->i_valid;
0423     int err;
0424 
0425     if (end <= inode->i_size && !extend_init)
0426         return 0;
0427 
0428     /* Mark rw ntfs as dirty. It will be cleared at umount. */
0429     ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
0430 
0431     if (end > inode->i_size) {
0432         err = ntfs_set_size(inode, end);
0433         if (err)
0434             goto out;
0435         inode->i_size = end;
0436     }
0437 
0438     if (extend_init && !is_compressed(ni)) {
0439         err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
0440         if (err)
0441             goto out;
0442     } else {
0443         err = 0;
0444     }
0445 
0446     inode->i_ctime = inode->i_mtime = current_time(inode);
0447     mark_inode_dirty(inode);
0448 
0449     if (IS_SYNC(inode)) {
0450         int err2;
0451 
0452         err = filemap_fdatawrite_range(mapping, pos, end - 1);
0453         err2 = sync_mapping_buffers(mapping);
0454         if (!err)
0455             err = err2;
0456         err2 = write_inode_now(inode, 1);
0457         if (!err)
0458             err = err2;
0459         if (!err)
0460             err = filemap_fdatawait_range(mapping, pos, end - 1);
0461     }
0462 
0463 out:
0464     return err;
0465 }
0466 
0467 static int ntfs_truncate(struct inode *inode, loff_t new_size)
0468 {
0469     struct super_block *sb = inode->i_sb;
0470     struct ntfs_inode *ni = ntfs_i(inode);
0471     int err, dirty = 0;
0472     u64 new_valid;
0473 
0474     if (!S_ISREG(inode->i_mode))
0475         return 0;
0476 
0477     if (is_compressed(ni)) {
0478         if (ni->i_valid > new_size)
0479             ni->i_valid = new_size;
0480     } else {
0481         err = block_truncate_page(inode->i_mapping, new_size,
0482                       ntfs_get_block);
0483         if (err)
0484             return err;
0485     }
0486 
0487     new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
0488 
0489     ni_lock(ni);
0490 
0491     truncate_setsize(inode, new_size);
0492 
0493     down_write(&ni->file.run_lock);
0494     err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
0495                 &new_valid, ni->mi.sbi->options->prealloc, NULL);
0496     up_write(&ni->file.run_lock);
0497 
0498     if (new_valid < ni->i_valid)
0499         ni->i_valid = new_valid;
0500 
0501     ni_unlock(ni);
0502 
0503     ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
0504     inode->i_ctime = inode->i_mtime = current_time(inode);
0505     if (!IS_DIRSYNC(inode)) {
0506         dirty = 1;
0507     } else {
0508         err = ntfs_sync_inode(inode);
0509         if (err)
0510             return err;
0511     }
0512 
0513     if (dirty)
0514         mark_inode_dirty(inode);
0515 
0516     /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
0517 
0518     return 0;
0519 }
0520 
0521 /*
0522  * ntfs_fallocate
0523  *
0524  * Preallocate space for a file. This implements ntfs's fallocate file
0525  * operation, which gets called from sys_fallocate system call. User
0526  * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
0527  * we just allocate clusters without zeroing them out. Otherwise we
0528  * allocate and zero out clusters via an expanding truncate.
0529  */
0530 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
0531 {
0532     struct inode *inode = file->f_mapping->host;
0533     struct address_space *mapping = inode->i_mapping;
0534     struct super_block *sb = inode->i_sb;
0535     struct ntfs_sb_info *sbi = sb->s_fs_info;
0536     struct ntfs_inode *ni = ntfs_i(inode);
0537     loff_t end = vbo + len;
0538     loff_t vbo_down = round_down(vbo, PAGE_SIZE);
0539     bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);
0540     loff_t i_size, new_size;
0541     bool map_locked;
0542     int err;
0543 
0544     /* No support for dir. */
0545     if (!S_ISREG(inode->i_mode))
0546         return -EOPNOTSUPP;
0547 
0548     /*
0549      * vfs_fallocate checks all possible combinations of mode.
0550      * Do additional checks here before ntfs_set_state(dirty).
0551      */
0552     if (mode & FALLOC_FL_PUNCH_HOLE) {
0553         if (!is_supported_holes)
0554             return -EOPNOTSUPP;
0555     } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
0556     } else if (mode & FALLOC_FL_INSERT_RANGE) {
0557         if (!is_supported_holes)
0558             return -EOPNOTSUPP;
0559     } else if (mode &
0560            ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
0561              FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) {
0562         ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
0563                 mode);
0564         return -EOPNOTSUPP;
0565     }
0566 
0567     ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
0568 
0569     inode_lock(inode);
0570     i_size = inode->i_size;
0571     new_size = max(end, i_size);
0572     map_locked = false;
0573 
0574     if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
0575         /* Should never be here, see ntfs_file_open. */
0576         err = -EOPNOTSUPP;
0577         goto out;
0578     }
0579 
0580     if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
0581             FALLOC_FL_INSERT_RANGE)) {
0582         inode_dio_wait(inode);
0583         filemap_invalidate_lock(mapping);
0584         map_locked = true;
0585     }
0586 
0587     if (mode & FALLOC_FL_PUNCH_HOLE) {
0588         u32 frame_size;
0589         loff_t mask, vbo_a, end_a, tmp;
0590 
0591         err = filemap_write_and_wait_range(mapping, vbo, end - 1);
0592         if (err)
0593             goto out;
0594 
0595         err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
0596         if (err)
0597             goto out;
0598 
0599         truncate_pagecache(inode, vbo_down);
0600 
0601         ni_lock(ni);
0602         err = attr_punch_hole(ni, vbo, len, &frame_size);
0603         ni_unlock(ni);
0604         if (err != E_NTFS_NOTALIGNED)
0605             goto out;
0606 
0607         /* Process not aligned punch. */
0608         mask = frame_size - 1;
0609         vbo_a = (vbo + mask) & ~mask;
0610         end_a = end & ~mask;
0611 
0612         tmp = min(vbo_a, end);
0613         if (tmp > vbo) {
0614             err = ntfs_zero_range(inode, vbo, tmp);
0615             if (err)
0616                 goto out;
0617         }
0618 
0619         if (vbo < end_a && end_a < end) {
0620             err = ntfs_zero_range(inode, end_a, end);
0621             if (err)
0622                 goto out;
0623         }
0624 
0625         /* Aligned punch_hole */
0626         if (end_a > vbo_a) {
0627             ni_lock(ni);
0628             err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
0629             ni_unlock(ni);
0630         }
0631     } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
0632         /*
0633          * Write tail of the last page before removed range since
0634          * it will get removed from the page cache below.
0635          */
0636         err = filemap_write_and_wait_range(mapping, vbo_down, vbo);
0637         if (err)
0638             goto out;
0639 
0640         /*
0641          * Write data that will be shifted to preserve them
0642          * when discarding page cache below.
0643          */
0644         err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
0645         if (err)
0646             goto out;
0647 
0648         truncate_pagecache(inode, vbo_down);
0649 
0650         ni_lock(ni);
0651         err = attr_collapse_range(ni, vbo, len);
0652         ni_unlock(ni);
0653     } else if (mode & FALLOC_FL_INSERT_RANGE) {
0654         /* Check new size. */
0655         err = inode_newsize_ok(inode, new_size);
0656         if (err)
0657             goto out;
0658 
0659         /* Write out all dirty pages. */
0660         err = filemap_write_and_wait_range(mapping, vbo_down,
0661                            LLONG_MAX);
0662         if (err)
0663             goto out;
0664         truncate_pagecache(inode, vbo_down);
0665 
0666         ni_lock(ni);
0667         err = attr_insert_range(ni, vbo, len);
0668         ni_unlock(ni);
0669     } else {
0670         /* Check new size. */
0671 
0672         /* generic/213: expected -ENOSPC instead of -EFBIG. */
0673         if (!is_supported_holes) {
0674             loff_t to_alloc = new_size - inode_get_bytes(inode);
0675 
0676             if (to_alloc > 0 &&
0677                 (to_alloc >> sbi->cluster_bits) >
0678                     wnd_zeroes(&sbi->used.bitmap)) {
0679                 err = -ENOSPC;
0680                 goto out;
0681             }
0682         }
0683 
0684         err = inode_newsize_ok(inode, new_size);
0685         if (err)
0686             goto out;
0687 
0688         /*
0689          * Allocate clusters, do not change 'valid' size.
0690          */
0691         err = ntfs_set_size(inode, new_size);
0692         if (err)
0693             goto out;
0694 
0695         if (is_supported_holes) {
0696             CLST vcn_v = ni->i_valid >> sbi->cluster_bits;
0697             CLST vcn = vbo >> sbi->cluster_bits;
0698             CLST cend = bytes_to_cluster(sbi, end);
0699             CLST lcn, clen;
0700             bool new;
0701 
0702             /*
0703              * Allocate but do not zero new clusters. (see below comments)
0704              * This breaks security: One can read unused on-disk areas.
0705              * Zeroing these clusters may be too long.
0706              * Maybe we should check here for root rights?
0707              */
0708             for (; vcn < cend; vcn += clen) {
0709                 err = attr_data_get_block(ni, vcn, cend - vcn,
0710                               &lcn, &clen, &new);
0711                 if (err)
0712                     goto out;
0713                 if (!new || vcn >= vcn_v)
0714                     continue;
0715 
0716                 /*
0717                  * Unwritten area.
0718                  * NTFS is not able to store several unwritten areas.
0719                  * Activate 'ntfs_sparse_cluster' to zero new allocated clusters.
0720                  *
0721                  * Dangerous in case:
0722                  * 1G of sparsed clusters + 1 cluster of data =>
0723                  * valid_size == 1G + 1 cluster
0724                  * fallocate(1G) will zero 1G and this can be very long
0725                  * xfstest 016/086 will fail without 'ntfs_sparse_cluster'.
0726                  */
0727                 ntfs_sparse_cluster(inode, NULL, vcn,
0728                             min(vcn_v - vcn, clen));
0729             }
0730         }
0731 
0732         if (mode & FALLOC_FL_KEEP_SIZE) {
0733             ni_lock(ni);
0734             /* True - Keep preallocated. */
0735             err = attr_set_size(ni, ATTR_DATA, NULL, 0,
0736                         &ni->file.run, i_size, &ni->i_valid,
0737                         true, NULL);
0738             ni_unlock(ni);
0739         }
0740     }
0741 
0742 out:
0743     if (map_locked)
0744         filemap_invalidate_unlock(mapping);
0745 
0746     if (!err) {
0747         inode->i_ctime = inode->i_mtime = current_time(inode);
0748         mark_inode_dirty(inode);
0749     }
0750 
0751     inode_unlock(inode);
0752     return err;
0753 }
0754 
0755 /*
0756  * ntfs3_setattr - inode_operations::setattr
0757  */
0758 int ntfs3_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0759           struct iattr *attr)
0760 {
0761     struct super_block *sb = dentry->d_sb;
0762     struct ntfs_sb_info *sbi = sb->s_fs_info;
0763     struct inode *inode = d_inode(dentry);
0764     struct ntfs_inode *ni = ntfs_i(inode);
0765     u32 ia_valid = attr->ia_valid;
0766     umode_t mode = inode->i_mode;
0767     int err;
0768 
0769     if (sbi->options->noacsrules) {
0770         /* "No access rules" - Force any changes of time etc. */
0771         attr->ia_valid |= ATTR_FORCE;
0772         /* and disable for editing some attributes. */
0773         attr->ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
0774         ia_valid = attr->ia_valid;
0775     }
0776 
0777     err = setattr_prepare(mnt_userns, dentry, attr);
0778     if (err)
0779         goto out;
0780 
0781     if (ia_valid & ATTR_SIZE) {
0782         loff_t oldsize = inode->i_size;
0783 
0784         if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
0785             /* Should never be here, see ntfs_file_open(). */
0786             err = -EOPNOTSUPP;
0787             goto out;
0788         }
0789         inode_dio_wait(inode);
0790 
0791         if (attr->ia_size <= oldsize)
0792             err = ntfs_truncate(inode, attr->ia_size);
0793         else if (attr->ia_size > oldsize)
0794             err = ntfs_extend(inode, attr->ia_size, 0, NULL);
0795 
0796         if (err)
0797             goto out;
0798 
0799         ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
0800     }
0801 
0802     setattr_copy(mnt_userns, inode, attr);
0803 
0804     if (mode != inode->i_mode) {
0805         err = ntfs_acl_chmod(mnt_userns, inode);
0806         if (err)
0807             goto out;
0808 
0809         /* Linux 'w' -> Windows 'ro'. */
0810         if (0222 & inode->i_mode)
0811             ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
0812         else
0813             ni->std_fa |= FILE_ATTRIBUTE_READONLY;
0814     }
0815 
0816     if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
0817         ntfs_save_wsl_perm(inode);
0818     mark_inode_dirty(inode);
0819 out:
0820     return err;
0821 }
0822 
0823 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
0824 {
0825     struct file *file = iocb->ki_filp;
0826     struct inode *inode = file->f_mapping->host;
0827     struct ntfs_inode *ni = ntfs_i(inode);
0828 
0829     if (is_encrypted(ni)) {
0830         ntfs_inode_warn(inode, "encrypted i/o not supported");
0831         return -EOPNOTSUPP;
0832     }
0833 
0834     if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
0835         ntfs_inode_warn(inode, "direct i/o + compressed not supported");
0836         return -EOPNOTSUPP;
0837     }
0838 
0839 #ifndef CONFIG_NTFS3_LZX_XPRESS
0840     if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
0841         ntfs_inode_warn(
0842             inode,
0843             "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
0844         return -EOPNOTSUPP;
0845     }
0846 #endif
0847 
0848     if (is_dedup(ni)) {
0849         ntfs_inode_warn(inode, "read deduplicated not supported");
0850         return -EOPNOTSUPP;
0851     }
0852 
0853     return generic_file_read_iter(iocb, iter);
0854 }
0855 
0856 /*
0857  * ntfs_get_frame_pages
0858  *
0859  * Return: Array of locked pages.
0860  */
0861 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
0862                 struct page **pages, u32 pages_per_frame,
0863                 bool *frame_uptodate)
0864 {
0865     gfp_t gfp_mask = mapping_gfp_mask(mapping);
0866     u32 npages;
0867 
0868     *frame_uptodate = true;
0869 
0870     for (npages = 0; npages < pages_per_frame; npages++, index++) {
0871         struct page *page;
0872 
0873         page = find_or_create_page(mapping, index, gfp_mask);
0874         if (!page) {
0875             while (npages--) {
0876                 page = pages[npages];
0877                 unlock_page(page);
0878                 put_page(page);
0879             }
0880 
0881             return -ENOMEM;
0882         }
0883 
0884         if (!PageUptodate(page))
0885             *frame_uptodate = false;
0886 
0887         pages[npages] = page;
0888     }
0889 
0890     return 0;
0891 }
0892 
0893 /*
0894  * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
0895  */
0896 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
0897 {
0898     int err;
0899     struct file *file = iocb->ki_filp;
0900     size_t count = iov_iter_count(from);
0901     loff_t pos = iocb->ki_pos;
0902     struct inode *inode = file_inode(file);
0903     loff_t i_size = inode->i_size;
0904     struct address_space *mapping = inode->i_mapping;
0905     struct ntfs_inode *ni = ntfs_i(inode);
0906     u64 valid = ni->i_valid;
0907     struct ntfs_sb_info *sbi = ni->mi.sbi;
0908     struct page *page, **pages = NULL;
0909     size_t written = 0;
0910     u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
0911     u32 frame_size = 1u << frame_bits;
0912     u32 pages_per_frame = frame_size >> PAGE_SHIFT;
0913     u32 ip, off;
0914     CLST frame;
0915     u64 frame_vbo;
0916     pgoff_t index;
0917     bool frame_uptodate;
0918 
0919     if (frame_size < PAGE_SIZE) {
0920         /*
0921          * frame_size == 8K if cluster 512
0922          * frame_size == 64K if cluster 4096
0923          */
0924         ntfs_inode_warn(inode, "page size is bigger than frame size");
0925         return -EOPNOTSUPP;
0926     }
0927 
0928     pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
0929     if (!pages)
0930         return -ENOMEM;
0931 
0932     current->backing_dev_info = inode_to_bdi(inode);
0933     err = file_remove_privs(file);
0934     if (err)
0935         goto out;
0936 
0937     err = file_update_time(file);
0938     if (err)
0939         goto out;
0940 
0941     /* Zero range [valid : pos). */
0942     while (valid < pos) {
0943         CLST lcn, clen;
0944 
0945         frame = valid >> frame_bits;
0946         frame_vbo = valid & ~(frame_size - 1);
0947         off = valid & (frame_size - 1);
0948 
0949         err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 0, &lcn,
0950                       &clen, NULL);
0951         if (err)
0952             goto out;
0953 
0954         if (lcn == SPARSE_LCN) {
0955             ni->i_valid = valid =
0956                 frame_vbo + ((u64)clen << sbi->cluster_bits);
0957             continue;
0958         }
0959 
0960         /* Load full frame. */
0961         err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
0962                        pages, pages_per_frame,
0963                        &frame_uptodate);
0964         if (err)
0965             goto out;
0966 
0967         if (!frame_uptodate && off) {
0968             err = ni_read_frame(ni, frame_vbo, pages,
0969                         pages_per_frame);
0970             if (err) {
0971                 for (ip = 0; ip < pages_per_frame; ip++) {
0972                     page = pages[ip];
0973                     unlock_page(page);
0974                     put_page(page);
0975                 }
0976                 goto out;
0977             }
0978         }
0979 
0980         ip = off >> PAGE_SHIFT;
0981         off = offset_in_page(valid);
0982         for (; ip < pages_per_frame; ip++, off = 0) {
0983             page = pages[ip];
0984             zero_user_segment(page, off, PAGE_SIZE);
0985             flush_dcache_page(page);
0986             SetPageUptodate(page);
0987         }
0988 
0989         ni_lock(ni);
0990         err = ni_write_frame(ni, pages, pages_per_frame);
0991         ni_unlock(ni);
0992 
0993         for (ip = 0; ip < pages_per_frame; ip++) {
0994             page = pages[ip];
0995             SetPageUptodate(page);
0996             unlock_page(page);
0997             put_page(page);
0998         }
0999 
1000         if (err)
1001             goto out;
1002 
1003         ni->i_valid = valid = frame_vbo + frame_size;
1004     }
1005 
1006     /* Copy user data [pos : pos + count). */
1007     while (count) {
1008         size_t copied, bytes;
1009 
1010         off = pos & (frame_size - 1);
1011         bytes = frame_size - off;
1012         if (bytes > count)
1013             bytes = count;
1014 
1015         frame_vbo = pos & ~(frame_size - 1);
1016         index = frame_vbo >> PAGE_SHIFT;
1017 
1018         if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
1019             err = -EFAULT;
1020             goto out;
1021         }
1022 
1023         /* Load full frame. */
1024         err = ntfs_get_frame_pages(mapping, index, pages,
1025                        pages_per_frame, &frame_uptodate);
1026         if (err)
1027             goto out;
1028 
1029         if (!frame_uptodate) {
1030             loff_t to = pos + bytes;
1031 
1032             if (off || (to < i_size && (to & (frame_size - 1)))) {
1033                 err = ni_read_frame(ni, frame_vbo, pages,
1034                             pages_per_frame);
1035                 if (err) {
1036                     for (ip = 0; ip < pages_per_frame;
1037                          ip++) {
1038                         page = pages[ip];
1039                         unlock_page(page);
1040                         put_page(page);
1041                     }
1042                     goto out;
1043                 }
1044             }
1045         }
1046 
1047         WARN_ON(!bytes);
1048         copied = 0;
1049         ip = off >> PAGE_SHIFT;
1050         off = offset_in_page(pos);
1051 
1052         /* Copy user data to pages. */
1053         for (;;) {
1054             size_t cp, tail = PAGE_SIZE - off;
1055 
1056             page = pages[ip];
1057             cp = copy_page_from_iter_atomic(page, off,
1058                             min(tail, bytes), from);
1059             flush_dcache_page(page);
1060 
1061             copied += cp;
1062             bytes -= cp;
1063             if (!bytes || !cp)
1064                 break;
1065 
1066             if (cp < tail) {
1067                 off += cp;
1068             } else {
1069                 ip++;
1070                 off = 0;
1071             }
1072         }
1073 
1074         ni_lock(ni);
1075         err = ni_write_frame(ni, pages, pages_per_frame);
1076         ni_unlock(ni);
1077 
1078         for (ip = 0; ip < pages_per_frame; ip++) {
1079             page = pages[ip];
1080             ClearPageDirty(page);
1081             SetPageUptodate(page);
1082             unlock_page(page);
1083             put_page(page);
1084         }
1085 
1086         if (err)
1087             goto out;
1088 
1089         /*
1090          * We can loop for a long time in here. Be nice and allow
1091          * us to schedule out to avoid softlocking if preempt
1092          * is disabled.
1093          */
1094         cond_resched();
1095 
1096         pos += copied;
1097         written += copied;
1098 
1099         count = iov_iter_count(from);
1100     }
1101 
1102 out:
1103     kfree(pages);
1104 
1105     current->backing_dev_info = NULL;
1106 
1107     if (err < 0)
1108         return err;
1109 
1110     iocb->ki_pos += written;
1111     if (iocb->ki_pos > ni->i_valid)
1112         ni->i_valid = iocb->ki_pos;
1113 
1114     return written;
1115 }
1116 
1117 /*
1118  * ntfs_file_write_iter - file_operations::write_iter
1119  */
1120 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1121 {
1122     struct file *file = iocb->ki_filp;
1123     struct address_space *mapping = file->f_mapping;
1124     struct inode *inode = mapping->host;
1125     ssize_t ret;
1126     struct ntfs_inode *ni = ntfs_i(inode);
1127 
1128     if (is_encrypted(ni)) {
1129         ntfs_inode_warn(inode, "encrypted i/o not supported");
1130         return -EOPNOTSUPP;
1131     }
1132 
1133     if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
1134         ntfs_inode_warn(inode, "direct i/o + compressed not supported");
1135         return -EOPNOTSUPP;
1136     }
1137 
1138     if (is_dedup(ni)) {
1139         ntfs_inode_warn(inode, "write into deduplicated not supported");
1140         return -EOPNOTSUPP;
1141     }
1142 
1143     if (!inode_trylock(inode)) {
1144         if (iocb->ki_flags & IOCB_NOWAIT)
1145             return -EAGAIN;
1146         inode_lock(inode);
1147     }
1148 
1149     ret = generic_write_checks(iocb, from);
1150     if (ret <= 0)
1151         goto out;
1152 
1153     if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
1154         /* Should never be here, see ntfs_file_open(). */
1155         ret = -EOPNOTSUPP;
1156         goto out;
1157     }
1158 
1159     ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
1160     if (ret)
1161         goto out;
1162 
1163     ret = is_compressed(ni) ? ntfs_compress_write(iocb, from)
1164                 : __generic_file_write_iter(iocb, from);
1165 
1166 out:
1167     inode_unlock(inode);
1168 
1169     if (ret > 0)
1170         ret = generic_write_sync(iocb, ret);
1171 
1172     return ret;
1173 }
1174 
1175 /*
1176  * ntfs_file_open - file_operations::open
1177  */
1178 int ntfs_file_open(struct inode *inode, struct file *file)
1179 {
1180     struct ntfs_inode *ni = ntfs_i(inode);
1181 
1182     if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
1183              (file->f_flags & O_DIRECT))) {
1184         return -EOPNOTSUPP;
1185     }
1186 
1187     /* Decompress "external compressed" file if opened for rw. */
1188     if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
1189         (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
1190 #ifdef CONFIG_NTFS3_LZX_XPRESS
1191         int err = ni_decompress_file(ni);
1192 
1193         if (err)
1194             return err;
1195 #else
1196         ntfs_inode_warn(
1197             inode,
1198             "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
1199         return -EOPNOTSUPP;
1200 #endif
1201     }
1202 
1203     return generic_file_open(inode, file);
1204 }
1205 
1206 /*
1207  * ntfs_file_release - file_operations::release
1208  */
1209 static int ntfs_file_release(struct inode *inode, struct file *file)
1210 {
1211     struct ntfs_inode *ni = ntfs_i(inode);
1212     struct ntfs_sb_info *sbi = ni->mi.sbi;
1213     int err = 0;
1214 
1215     /* If we are last writer on the inode, drop the block reservation. */
1216     if (sbi->options->prealloc && ((file->f_mode & FMODE_WRITE) &&
1217                       atomic_read(&inode->i_writecount) == 1)) {
1218         ni_lock(ni);
1219         down_write(&ni->file.run_lock);
1220 
1221         err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
1222                     inode->i_size, &ni->i_valid, false, NULL);
1223 
1224         up_write(&ni->file.run_lock);
1225         ni_unlock(ni);
1226     }
1227     return err;
1228 }
1229 
1230 /*
1231  * ntfs_fiemap - file_operations::fiemap
1232  */
1233 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1234         __u64 start, __u64 len)
1235 {
1236     int err;
1237     struct ntfs_inode *ni = ntfs_i(inode);
1238 
1239     err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
1240     if (err)
1241         return err;
1242 
1243     ni_lock(ni);
1244 
1245     err = ni_fiemap(ni, fieinfo, start, len);
1246 
1247     ni_unlock(ni);
1248 
1249     return err;
1250 }
1251 
1252 // clang-format off
1253 const struct inode_operations ntfs_file_inode_operations = {
1254     .getattr    = ntfs_getattr,
1255     .setattr    = ntfs3_setattr,
1256     .listxattr  = ntfs_listxattr,
1257     .permission = ntfs_permission,
1258     .get_acl    = ntfs_get_acl,
1259     .set_acl    = ntfs_set_acl,
1260     .fiemap     = ntfs_fiemap,
1261 };
1262 
1263 const struct file_operations ntfs_file_operations = {
1264     .llseek     = generic_file_llseek,
1265     .read_iter  = ntfs_file_read_iter,
1266     .write_iter = ntfs_file_write_iter,
1267     .unlocked_ioctl = ntfs_ioctl,
1268 #ifdef CONFIG_COMPAT
1269     .compat_ioctl   = ntfs_compat_ioctl,
1270 #endif
1271     .splice_read    = generic_file_splice_read,
1272     .mmap       = ntfs_file_mmap,
1273     .open       = ntfs_file_open,
1274     .fsync      = generic_file_fsync,
1275     .splice_write   = iter_file_splice_write,
1276     .fallocate  = ntfs_fallocate,
1277     .release    = ntfs_file_release,
1278 };
1279 // clang-format on