cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

file.c (28226B)


      1// SPDX-License-Identifier: GPL-2.0
      2/*
      3 *
      4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
      5 *
      6 *  Regular file handling primitives for NTFS-based filesystems.
      7 *
      8 */
      9
     10#include <linux/backing-dev.h>
     11#include <linux/blkdev.h>
     12#include <linux/buffer_head.h>
     13#include <linux/compat.h>
     14#include <linux/falloc.h>
     15#include <linux/fiemap.h>
     16
     17#include "debug.h"
     18#include "ntfs.h"
     19#include "ntfs_fs.h"
     20
     21static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg)
     22{
     23	struct fstrim_range __user *user_range;
     24	struct fstrim_range range;
     25	int err;
     26
     27	if (!capable(CAP_SYS_ADMIN))
     28		return -EPERM;
     29
     30	if (!bdev_max_discard_sectors(sbi->sb->s_bdev))
     31		return -EOPNOTSUPP;
     32
     33	user_range = (struct fstrim_range __user *)arg;
     34	if (copy_from_user(&range, user_range, sizeof(range)))
     35		return -EFAULT;
     36
     37	range.minlen = max_t(u32, range.minlen,
     38			     bdev_discard_granularity(sbi->sb->s_bdev));
     39
     40	err = ntfs_trim_fs(sbi, &range);
     41	if (err < 0)
     42		return err;
     43
     44	if (copy_to_user(user_range, &range, sizeof(range)))
     45		return -EFAULT;
     46
     47	return 0;
     48}
     49
     50static long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg)
     51{
     52	struct inode *inode = file_inode(filp);
     53	struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
     54
     55	switch (cmd) {
     56	case FITRIM:
     57		return ntfs_ioctl_fitrim(sbi, arg);
     58	}
     59	return -ENOTTY; /* Inappropriate ioctl for device. */
     60}
     61
     62#ifdef CONFIG_COMPAT
     63static long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg)
     64
     65{
     66	return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
     67}
     68#endif
     69
     70/*
     71 * ntfs_getattr - inode_operations::getattr
     72 */
     73int ntfs_getattr(struct user_namespace *mnt_userns, const struct path *path,
     74		 struct kstat *stat, u32 request_mask, u32 flags)
     75{
     76	struct inode *inode = d_inode(path->dentry);
     77	struct ntfs_inode *ni = ntfs_i(inode);
     78
     79	if (is_compressed(ni))
     80		stat->attributes |= STATX_ATTR_COMPRESSED;
     81
     82	if (is_encrypted(ni))
     83		stat->attributes |= STATX_ATTR_ENCRYPTED;
     84
     85	stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED;
     86
     87	generic_fillattr(mnt_userns, inode, stat);
     88
     89	stat->result_mask |= STATX_BTIME;
     90	stat->btime = ni->i_crtime;
     91	stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */
     92
     93	return 0;
     94}
     95
     96static int ntfs_extend_initialized_size(struct file *file,
     97					struct ntfs_inode *ni,
     98					const loff_t valid,
     99					const loff_t new_valid)
    100{
    101	struct inode *inode = &ni->vfs_inode;
    102	struct address_space *mapping = inode->i_mapping;
    103	struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
    104	loff_t pos = valid;
    105	int err;
    106
    107	if (is_resident(ni)) {
    108		ni->i_valid = new_valid;
    109		return 0;
    110	}
    111
    112	WARN_ON(is_compressed(ni));
    113	WARN_ON(valid >= new_valid);
    114
    115	for (;;) {
    116		u32 zerofrom, len;
    117		struct page *page;
    118		u8 bits;
    119		CLST vcn, lcn, clen;
    120
    121		if (is_sparsed(ni)) {
    122			bits = sbi->cluster_bits;
    123			vcn = pos >> bits;
    124
    125			err = attr_data_get_block(ni, vcn, 0, &lcn, &clen,
    126						  NULL);
    127			if (err)
    128				goto out;
    129
    130			if (lcn == SPARSE_LCN) {
    131				loff_t vbo = (loff_t)vcn << bits;
    132				loff_t to = vbo + ((loff_t)clen << bits);
    133
    134				if (to <= new_valid) {
    135					ni->i_valid = to;
    136					pos = to;
    137					goto next;
    138				}
    139
    140				if (vbo < pos) {
    141					pos = vbo;
    142				} else {
    143					to = (new_valid >> bits) << bits;
    144					if (pos < to) {
    145						ni->i_valid = to;
    146						pos = to;
    147						goto next;
    148					}
    149				}
    150			}
    151		}
    152
    153		zerofrom = pos & (PAGE_SIZE - 1);
    154		len = PAGE_SIZE - zerofrom;
    155
    156		if (pos + len > new_valid)
    157			len = new_valid - pos;
    158
    159		err = ntfs_write_begin(file, mapping, pos, len, &page, NULL);
    160		if (err)
    161			goto out;
    162
    163		zero_user_segment(page, zerofrom, PAGE_SIZE);
    164
    165		/* This function in any case puts page. */
    166		err = ntfs_write_end(file, mapping, pos, len, len, page, NULL);
    167		if (err < 0)
    168			goto out;
    169		pos += len;
    170
    171next:
    172		if (pos >= new_valid)
    173			break;
    174
    175		balance_dirty_pages_ratelimited(mapping);
    176		cond_resched();
    177	}
    178
    179	return 0;
    180
    181out:
    182	ni->i_valid = valid;
    183	ntfs_inode_warn(inode, "failed to extend initialized size to %llx.",
    184			new_valid);
    185	return err;
    186}
    187
    188/*
    189 * ntfs_zero_range - Helper function for punch_hole.
    190 *
    191 * It zeroes a range [vbo, vbo_to).
    192 */
    193static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to)
    194{
    195	int err = 0;
    196	struct address_space *mapping = inode->i_mapping;
    197	u32 blocksize = 1 << inode->i_blkbits;
    198	pgoff_t idx = vbo >> PAGE_SHIFT;
    199	u32 z_start = vbo & (PAGE_SIZE - 1);
    200	pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT;
    201	loff_t page_off;
    202	struct buffer_head *head, *bh;
    203	u32 bh_next, bh_off, z_end;
    204	sector_t iblock;
    205	struct page *page;
    206
    207	for (; idx < idx_end; idx += 1, z_start = 0) {
    208		page_off = (loff_t)idx << PAGE_SHIFT;
    209		z_end = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off)
    210							: PAGE_SIZE;
    211		iblock = page_off >> inode->i_blkbits;
    212
    213		page = find_or_create_page(mapping, idx,
    214					   mapping_gfp_constraint(mapping,
    215								  ~__GFP_FS));
    216		if (!page)
    217			return -ENOMEM;
    218
    219		if (!page_has_buffers(page))
    220			create_empty_buffers(page, blocksize, 0);
    221
    222		bh = head = page_buffers(page);
    223		bh_off = 0;
    224		do {
    225			bh_next = bh_off + blocksize;
    226
    227			if (bh_next <= z_start || bh_off >= z_end)
    228				continue;
    229
    230			if (!buffer_mapped(bh)) {
    231				ntfs_get_block(inode, iblock, bh, 0);
    232				/* Unmapped? It's a hole - nothing to do. */
    233				if (!buffer_mapped(bh))
    234					continue;
    235			}
    236
    237			/* Ok, it's mapped. Make sure it's up-to-date. */
    238			if (PageUptodate(page))
    239				set_buffer_uptodate(bh);
    240
    241			if (!buffer_uptodate(bh)) {
    242				lock_buffer(bh);
    243				bh->b_end_io = end_buffer_read_sync;
    244				get_bh(bh);
    245				submit_bh(REQ_OP_READ, 0, bh);
    246
    247				wait_on_buffer(bh);
    248				if (!buffer_uptodate(bh)) {
    249					unlock_page(page);
    250					put_page(page);
    251					err = -EIO;
    252					goto out;
    253				}
    254			}
    255
    256			mark_buffer_dirty(bh);
    257
    258		} while (bh_off = bh_next, iblock += 1,
    259			 head != (bh = bh->b_this_page));
    260
    261		zero_user_segment(page, z_start, z_end);
    262
    263		unlock_page(page);
    264		put_page(page);
    265		cond_resched();
    266	}
    267out:
    268	mark_inode_dirty(inode);
    269	return err;
    270}
    271
    272/*
    273 * ntfs_sparse_cluster - Helper function to zero a new allocated clusters.
    274 *
    275 * NOTE: 512 <= cluster size <= 2M
    276 */
    277void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn,
    278			 CLST len)
    279{
    280	struct address_space *mapping = inode->i_mapping;
    281	struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
    282	u64 vbo = (u64)vcn << sbi->cluster_bits;
    283	u64 bytes = (u64)len << sbi->cluster_bits;
    284	u32 blocksize = 1 << inode->i_blkbits;
    285	pgoff_t idx0 = page0 ? page0->index : -1;
    286	loff_t vbo_clst = vbo & sbi->cluster_mask_inv;
    287	loff_t end = ntfs_up_cluster(sbi, vbo + bytes);
    288	pgoff_t idx = vbo_clst >> PAGE_SHIFT;
    289	u32 from = vbo_clst & (PAGE_SIZE - 1);
    290	pgoff_t idx_end = (end + PAGE_SIZE - 1) >> PAGE_SHIFT;
    291	loff_t page_off;
    292	u32 to;
    293	bool partial;
    294	struct page *page;
    295
    296	for (; idx < idx_end; idx += 1, from = 0) {
    297		page = idx == idx0 ? page0 : grab_cache_page(mapping, idx);
    298
    299		if (!page)
    300			continue;
    301
    302		page_off = (loff_t)idx << PAGE_SHIFT;
    303		to = (page_off + PAGE_SIZE) > end ? (end - page_off)
    304						  : PAGE_SIZE;
    305		partial = false;
    306
    307		if ((from || PAGE_SIZE != to) &&
    308		    likely(!page_has_buffers(page))) {
    309			create_empty_buffers(page, blocksize, 0);
    310		}
    311
    312		if (page_has_buffers(page)) {
    313			struct buffer_head *head, *bh;
    314			u32 bh_off = 0;
    315
    316			bh = head = page_buffers(page);
    317			do {
    318				u32 bh_next = bh_off + blocksize;
    319
    320				if (from <= bh_off && bh_next <= to) {
    321					set_buffer_uptodate(bh);
    322					mark_buffer_dirty(bh);
    323				} else if (!buffer_uptodate(bh)) {
    324					partial = true;
    325				}
    326				bh_off = bh_next;
    327			} while (head != (bh = bh->b_this_page));
    328		}
    329
    330		zero_user_segment(page, from, to);
    331
    332		if (!partial) {
    333			if (!PageUptodate(page))
    334				SetPageUptodate(page);
    335			set_page_dirty(page);
    336		}
    337
    338		if (idx != idx0) {
    339			unlock_page(page);
    340			put_page(page);
    341		}
    342		cond_resched();
    343	}
    344	mark_inode_dirty(inode);
    345}
    346
    347/*
    348 * ntfs_file_mmap - file_operations::mmap
    349 */
    350static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma)
    351{
    352	struct address_space *mapping = file->f_mapping;
    353	struct inode *inode = mapping->host;
    354	struct ntfs_inode *ni = ntfs_i(inode);
    355	u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT);
    356	bool rw = vma->vm_flags & VM_WRITE;
    357	int err;
    358
    359	if (is_encrypted(ni)) {
    360		ntfs_inode_warn(inode, "mmap encrypted not supported");
    361		return -EOPNOTSUPP;
    362	}
    363
    364	if (is_dedup(ni)) {
    365		ntfs_inode_warn(inode, "mmap deduplicated not supported");
    366		return -EOPNOTSUPP;
    367	}
    368
    369	if (is_compressed(ni) && rw) {
    370		ntfs_inode_warn(inode, "mmap(write) compressed not supported");
    371		return -EOPNOTSUPP;
    372	}
    373
    374	if (rw) {
    375		u64 to = min_t(loff_t, i_size_read(inode),
    376			       from + vma->vm_end - vma->vm_start);
    377
    378		if (is_sparsed(ni)) {
    379			/* Allocate clusters for rw map. */
    380			struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
    381			CLST lcn, len;
    382			CLST vcn = from >> sbi->cluster_bits;
    383			CLST end = bytes_to_cluster(sbi, to);
    384			bool new;
    385
    386			for (; vcn < end; vcn += len) {
    387				err = attr_data_get_block(ni, vcn, 1, &lcn,
    388							  &len, &new);
    389				if (err)
    390					goto out;
    391
    392				if (!new)
    393					continue;
    394				ntfs_sparse_cluster(inode, NULL, vcn, 1);
    395			}
    396		}
    397
    398		if (ni->i_valid < to) {
    399			if (!inode_trylock(inode)) {
    400				err = -EAGAIN;
    401				goto out;
    402			}
    403			err = ntfs_extend_initialized_size(file, ni,
    404							   ni->i_valid, to);
    405			inode_unlock(inode);
    406			if (err)
    407				goto out;
    408		}
    409	}
    410
    411	err = generic_file_mmap(file, vma);
    412out:
    413	return err;
    414}
    415
    416static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
    417		       struct file *file)
    418{
    419	struct ntfs_inode *ni = ntfs_i(inode);
    420	struct address_space *mapping = inode->i_mapping;
    421	loff_t end = pos + count;
    422	bool extend_init = file && pos > ni->i_valid;
    423	int err;
    424
    425	if (end <= inode->i_size && !extend_init)
    426		return 0;
    427
    428	/* Mark rw ntfs as dirty. It will be cleared at umount. */
    429	ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
    430
    431	if (end > inode->i_size) {
    432		err = ntfs_set_size(inode, end);
    433		if (err)
    434			goto out;
    435		inode->i_size = end;
    436	}
    437
    438	if (extend_init && !is_compressed(ni)) {
    439		err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
    440		if (err)
    441			goto out;
    442	} else {
    443		err = 0;
    444	}
    445
    446	inode->i_ctime = inode->i_mtime = current_time(inode);
    447	mark_inode_dirty(inode);
    448
    449	if (IS_SYNC(inode)) {
    450		int err2;
    451
    452		err = filemap_fdatawrite_range(mapping, pos, end - 1);
    453		err2 = sync_mapping_buffers(mapping);
    454		if (!err)
    455			err = err2;
    456		err2 = write_inode_now(inode, 1);
    457		if (!err)
    458			err = err2;
    459		if (!err)
    460			err = filemap_fdatawait_range(mapping, pos, end - 1);
    461	}
    462
    463out:
    464	return err;
    465}
    466
    467static int ntfs_truncate(struct inode *inode, loff_t new_size)
    468{
    469	struct super_block *sb = inode->i_sb;
    470	struct ntfs_inode *ni = ntfs_i(inode);
    471	int err, dirty = 0;
    472	u64 new_valid;
    473
    474	if (!S_ISREG(inode->i_mode))
    475		return 0;
    476
    477	if (is_compressed(ni)) {
    478		if (ni->i_valid > new_size)
    479			ni->i_valid = new_size;
    480	} else {
    481		err = block_truncate_page(inode->i_mapping, new_size,
    482					  ntfs_get_block);
    483		if (err)
    484			return err;
    485	}
    486
    487	new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
    488
    489	ni_lock(ni);
    490
    491	truncate_setsize(inode, new_size);
    492
    493	down_write(&ni->file.run_lock);
    494	err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
    495			    &new_valid, ni->mi.sbi->options->prealloc, NULL);
    496	up_write(&ni->file.run_lock);
    497
    498	if (new_valid < ni->i_valid)
    499		ni->i_valid = new_valid;
    500
    501	ni_unlock(ni);
    502
    503	ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
    504	inode->i_ctime = inode->i_mtime = current_time(inode);
    505	if (!IS_DIRSYNC(inode)) {
    506		dirty = 1;
    507	} else {
    508		err = ntfs_sync_inode(inode);
    509		if (err)
    510			return err;
    511	}
    512
    513	if (dirty)
    514		mark_inode_dirty(inode);
    515
    516	/*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
    517
    518	return 0;
    519}
    520
    521/*
    522 * ntfs_fallocate
    523 *
    524 * Preallocate space for a file. This implements ntfs's fallocate file
    525 * operation, which gets called from sys_fallocate system call. User
    526 * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
    527 * we just allocate clusters without zeroing them out. Otherwise we
    528 * allocate and zero out clusters via an expanding truncate.
    529 */
    530static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
    531{
    532	struct inode *inode = file->f_mapping->host;
    533	struct super_block *sb = inode->i_sb;
    534	struct ntfs_sb_info *sbi = sb->s_fs_info;
    535	struct ntfs_inode *ni = ntfs_i(inode);
    536	loff_t end = vbo + len;
    537	loff_t vbo_down = round_down(vbo, PAGE_SIZE);
    538	loff_t i_size;
    539	int err;
    540
    541	/* No support for dir. */
    542	if (!S_ISREG(inode->i_mode))
    543		return -EOPNOTSUPP;
    544
    545	/* Return error if mode is not supported. */
    546	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
    547		     FALLOC_FL_COLLAPSE_RANGE)) {
    548		ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
    549				mode);
    550		return -EOPNOTSUPP;
    551	}
    552
    553	ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
    554
    555	inode_lock(inode);
    556	i_size = inode->i_size;
    557
    558	if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
    559		/* Should never be here, see ntfs_file_open. */
    560		err = -EOPNOTSUPP;
    561		goto out;
    562	}
    563
    564	if (mode & FALLOC_FL_PUNCH_HOLE) {
    565		u32 frame_size;
    566		loff_t mask, vbo_a, end_a, tmp;
    567
    568		if (!(mode & FALLOC_FL_KEEP_SIZE)) {
    569			err = -EINVAL;
    570			goto out;
    571		}
    572
    573		err = filemap_write_and_wait_range(inode->i_mapping, vbo,
    574						   end - 1);
    575		if (err)
    576			goto out;
    577
    578		err = filemap_write_and_wait_range(inode->i_mapping, end,
    579						   LLONG_MAX);
    580		if (err)
    581			goto out;
    582
    583		inode_dio_wait(inode);
    584
    585		truncate_pagecache(inode, vbo_down);
    586
    587		if (!is_sparsed(ni) && !is_compressed(ni)) {
    588			/*
    589			 * Normal file, can't make hole.
    590			 * TODO: Try to find way to save info about hole.
    591			 */
    592			err = -EOPNOTSUPP;
    593			goto out;
    594		}
    595
    596		ni_lock(ni);
    597		err = attr_punch_hole(ni, vbo, len, &frame_size);
    598		ni_unlock(ni);
    599		if (err != E_NTFS_NOTALIGNED)
    600			goto out;
    601
    602		/* Process not aligned punch. */
    603		mask = frame_size - 1;
    604		vbo_a = (vbo + mask) & ~mask;
    605		end_a = end & ~mask;
    606
    607		tmp = min(vbo_a, end);
    608		if (tmp > vbo) {
    609			err = ntfs_zero_range(inode, vbo, tmp);
    610			if (err)
    611				goto out;
    612		}
    613
    614		if (vbo < end_a && end_a < end) {
    615			err = ntfs_zero_range(inode, end_a, end);
    616			if (err)
    617				goto out;
    618		}
    619
    620		/* Aligned punch_hole */
    621		if (end_a > vbo_a) {
    622			ni_lock(ni);
    623			err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
    624			ni_unlock(ni);
    625		}
    626	} else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
    627		if (mode & ~FALLOC_FL_COLLAPSE_RANGE) {
    628			err = -EINVAL;
    629			goto out;
    630		}
    631
    632		/*
    633		 * Write tail of the last page before removed range since
    634		 * it will get removed from the page cache below.
    635		 */
    636		err = filemap_write_and_wait_range(inode->i_mapping, vbo_down,
    637						   vbo);
    638		if (err)
    639			goto out;
    640
    641		/*
    642		 * Write data that will be shifted to preserve them
    643		 * when discarding page cache below.
    644		 */
    645		err = filemap_write_and_wait_range(inode->i_mapping, end,
    646						   LLONG_MAX);
    647		if (err)
    648			goto out;
    649
    650		/* Wait for existing dio to complete. */
    651		inode_dio_wait(inode);
    652
    653		truncate_pagecache(inode, vbo_down);
    654
    655		ni_lock(ni);
    656		err = attr_collapse_range(ni, vbo, len);
    657		ni_unlock(ni);
    658	} else {
    659		/*
    660		 * Normal file: Allocate clusters, do not change 'valid' size.
    661		 */
    662		loff_t new_size = max(end, i_size);
    663
    664		err = inode_newsize_ok(inode, new_size);
    665		if (err)
    666			goto out;
    667
    668		err = ntfs_set_size(inode, new_size);
    669		if (err)
    670			goto out;
    671
    672		if (is_sparsed(ni) || is_compressed(ni)) {
    673			CLST vcn_v = ni->i_valid >> sbi->cluster_bits;
    674			CLST vcn = vbo >> sbi->cluster_bits;
    675			CLST cend = bytes_to_cluster(sbi, end);
    676			CLST lcn, clen;
    677			bool new;
    678
    679			/*
    680			 * Allocate but do not zero new clusters. (see below comments)
    681			 * This breaks security: One can read unused on-disk areas.
    682			 * Zeroing these clusters may be too long.
    683			 * Maybe we should check here for root rights?
    684			 */
    685			for (; vcn < cend; vcn += clen) {
    686				err = attr_data_get_block(ni, vcn, cend - vcn,
    687							  &lcn, &clen, &new);
    688				if (err)
    689					goto out;
    690				if (!new || vcn >= vcn_v)
    691					continue;
    692
    693				/*
    694				 * Unwritten area.
    695				 * NTFS is not able to store several unwritten areas.
    696				 * Activate 'ntfs_sparse_cluster' to zero new allocated clusters.
    697				 *
    698				 * Dangerous in case:
    699				 * 1G of sparsed clusters + 1 cluster of data =>
    700				 * valid_size == 1G + 1 cluster
    701				 * fallocate(1G) will zero 1G and this can be very long
    702				 * xfstest 016/086 will fail without 'ntfs_sparse_cluster'.
    703				 */
    704				ntfs_sparse_cluster(inode, NULL, vcn,
    705						    min(vcn_v - vcn, clen));
    706			}
    707		}
    708
    709		if (mode & FALLOC_FL_KEEP_SIZE) {
    710			ni_lock(ni);
    711			/* True - Keep preallocated. */
    712			err = attr_set_size(ni, ATTR_DATA, NULL, 0,
    713					    &ni->file.run, i_size, &ni->i_valid,
    714					    true, NULL);
    715			ni_unlock(ni);
    716		}
    717	}
    718
    719out:
    720	if (err == -EFBIG)
    721		err = -ENOSPC;
    722
    723	if (!err) {
    724		inode->i_ctime = inode->i_mtime = current_time(inode);
    725		mark_inode_dirty(inode);
    726	}
    727
    728	inode_unlock(inode);
    729	return err;
    730}
    731
    732/*
    733 * ntfs3_setattr - inode_operations::setattr
    734 */
    735int ntfs3_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
    736		  struct iattr *attr)
    737{
    738	struct super_block *sb = dentry->d_sb;
    739	struct ntfs_sb_info *sbi = sb->s_fs_info;
    740	struct inode *inode = d_inode(dentry);
    741	struct ntfs_inode *ni = ntfs_i(inode);
    742	u32 ia_valid = attr->ia_valid;
    743	umode_t mode = inode->i_mode;
    744	int err;
    745
    746	if (sbi->options->noacsrules) {
    747		/* "No access rules" - Force any changes of time etc. */
    748		attr->ia_valid |= ATTR_FORCE;
    749		/* and disable for editing some attributes. */
    750		attr->ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE);
    751		ia_valid = attr->ia_valid;
    752	}
    753
    754	err = setattr_prepare(mnt_userns, dentry, attr);
    755	if (err)
    756		goto out;
    757
    758	if (ia_valid & ATTR_SIZE) {
    759		loff_t oldsize = inode->i_size;
    760
    761		if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
    762			/* Should never be here, see ntfs_file_open(). */
    763			err = -EOPNOTSUPP;
    764			goto out;
    765		}
    766		inode_dio_wait(inode);
    767
    768		if (attr->ia_size <= oldsize)
    769			err = ntfs_truncate(inode, attr->ia_size);
    770		else if (attr->ia_size > oldsize)
    771			err = ntfs_extend(inode, attr->ia_size, 0, NULL);
    772
    773		if (err)
    774			goto out;
    775
    776		ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
    777	}
    778
    779	setattr_copy(mnt_userns, inode, attr);
    780
    781	if (mode != inode->i_mode) {
    782		err = ntfs_acl_chmod(mnt_userns, inode);
    783		if (err)
    784			goto out;
    785
    786		/* Linux 'w' -> Windows 'ro'. */
    787		if (0222 & inode->i_mode)
    788			ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
    789		else
    790			ni->std_fa |= FILE_ATTRIBUTE_READONLY;
    791	}
    792
    793	if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
    794		ntfs_save_wsl_perm(inode);
    795	mark_inode_dirty(inode);
    796out:
    797	return err;
    798}
    799
    800static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
    801{
    802	struct file *file = iocb->ki_filp;
    803	struct inode *inode = file->f_mapping->host;
    804	struct ntfs_inode *ni = ntfs_i(inode);
    805
    806	if (is_encrypted(ni)) {
    807		ntfs_inode_warn(inode, "encrypted i/o not supported");
    808		return -EOPNOTSUPP;
    809	}
    810
    811	if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
    812		ntfs_inode_warn(inode, "direct i/o + compressed not supported");
    813		return -EOPNOTSUPP;
    814	}
    815
    816#ifndef CONFIG_NTFS3_LZX_XPRESS
    817	if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
    818		ntfs_inode_warn(
    819			inode,
    820			"activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
    821		return -EOPNOTSUPP;
    822	}
    823#endif
    824
    825	if (is_dedup(ni)) {
    826		ntfs_inode_warn(inode, "read deduplicated not supported");
    827		return -EOPNOTSUPP;
    828	}
    829
    830	return generic_file_read_iter(iocb, iter);
    831}
    832
    833/*
    834 * ntfs_get_frame_pages
    835 *
    836 * Return: Array of locked pages.
    837 */
    838static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
    839				struct page **pages, u32 pages_per_frame,
    840				bool *frame_uptodate)
    841{
    842	gfp_t gfp_mask = mapping_gfp_mask(mapping);
    843	u32 npages;
    844
    845	*frame_uptodate = true;
    846
    847	for (npages = 0; npages < pages_per_frame; npages++, index++) {
    848		struct page *page;
    849
    850		page = find_or_create_page(mapping, index, gfp_mask);
    851		if (!page) {
    852			while (npages--) {
    853				page = pages[npages];
    854				unlock_page(page);
    855				put_page(page);
    856			}
    857
    858			return -ENOMEM;
    859		}
    860
    861		if (!PageUptodate(page))
    862			*frame_uptodate = false;
    863
    864		pages[npages] = page;
    865	}
    866
    867	return 0;
    868}
    869
    870/*
    871 * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
    872 */
    873static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
    874{
    875	int err;
    876	struct file *file = iocb->ki_filp;
    877	size_t count = iov_iter_count(from);
    878	loff_t pos = iocb->ki_pos;
    879	struct inode *inode = file_inode(file);
    880	loff_t i_size = inode->i_size;
    881	struct address_space *mapping = inode->i_mapping;
    882	struct ntfs_inode *ni = ntfs_i(inode);
    883	u64 valid = ni->i_valid;
    884	struct ntfs_sb_info *sbi = ni->mi.sbi;
    885	struct page *page, **pages = NULL;
    886	size_t written = 0;
    887	u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
    888	u32 frame_size = 1u << frame_bits;
    889	u32 pages_per_frame = frame_size >> PAGE_SHIFT;
    890	u32 ip, off;
    891	CLST frame;
    892	u64 frame_vbo;
    893	pgoff_t index;
    894	bool frame_uptodate;
    895
    896	if (frame_size < PAGE_SIZE) {
    897		/*
    898		 * frame_size == 8K if cluster 512
    899		 * frame_size == 64K if cluster 4096
    900		 */
    901		ntfs_inode_warn(inode, "page size is bigger than frame size");
    902		return -EOPNOTSUPP;
    903	}
    904
    905	pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
    906	if (!pages)
    907		return -ENOMEM;
    908
    909	current->backing_dev_info = inode_to_bdi(inode);
    910	err = file_remove_privs(file);
    911	if (err)
    912		goto out;
    913
    914	err = file_update_time(file);
    915	if (err)
    916		goto out;
    917
    918	/* Zero range [valid : pos). */
    919	while (valid < pos) {
    920		CLST lcn, clen;
    921
    922		frame = valid >> frame_bits;
    923		frame_vbo = valid & ~(frame_size - 1);
    924		off = valid & (frame_size - 1);
    925
    926		err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 0, &lcn,
    927					  &clen, NULL);
    928		if (err)
    929			goto out;
    930
    931		if (lcn == SPARSE_LCN) {
    932			ni->i_valid = valid =
    933				frame_vbo + ((u64)clen << sbi->cluster_bits);
    934			continue;
    935		}
    936
    937		/* Load full frame. */
    938		err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
    939					   pages, pages_per_frame,
    940					   &frame_uptodate);
    941		if (err)
    942			goto out;
    943
    944		if (!frame_uptodate && off) {
    945			err = ni_read_frame(ni, frame_vbo, pages,
    946					    pages_per_frame);
    947			if (err) {
    948				for (ip = 0; ip < pages_per_frame; ip++) {
    949					page = pages[ip];
    950					unlock_page(page);
    951					put_page(page);
    952				}
    953				goto out;
    954			}
    955		}
    956
    957		ip = off >> PAGE_SHIFT;
    958		off = offset_in_page(valid);
    959		for (; ip < pages_per_frame; ip++, off = 0) {
    960			page = pages[ip];
    961			zero_user_segment(page, off, PAGE_SIZE);
    962			flush_dcache_page(page);
    963			SetPageUptodate(page);
    964		}
    965
    966		ni_lock(ni);
    967		err = ni_write_frame(ni, pages, pages_per_frame);
    968		ni_unlock(ni);
    969
    970		for (ip = 0; ip < pages_per_frame; ip++) {
    971			page = pages[ip];
    972			SetPageUptodate(page);
    973			unlock_page(page);
    974			put_page(page);
    975		}
    976
    977		if (err)
    978			goto out;
    979
    980		ni->i_valid = valid = frame_vbo + frame_size;
    981	}
    982
    983	/* Copy user data [pos : pos + count). */
    984	while (count) {
    985		size_t copied, bytes;
    986
    987		off = pos & (frame_size - 1);
    988		bytes = frame_size - off;
    989		if (bytes > count)
    990			bytes = count;
    991
    992		frame = pos >> frame_bits;
    993		frame_vbo = pos & ~(frame_size - 1);
    994		index = frame_vbo >> PAGE_SHIFT;
    995
    996		if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
    997			err = -EFAULT;
    998			goto out;
    999		}
   1000
   1001		/* Load full frame. */
   1002		err = ntfs_get_frame_pages(mapping, index, pages,
   1003					   pages_per_frame, &frame_uptodate);
   1004		if (err)
   1005			goto out;
   1006
   1007		if (!frame_uptodate) {
   1008			loff_t to = pos + bytes;
   1009
   1010			if (off || (to < i_size && (to & (frame_size - 1)))) {
   1011				err = ni_read_frame(ni, frame_vbo, pages,
   1012						    pages_per_frame);
   1013				if (err) {
   1014					for (ip = 0; ip < pages_per_frame;
   1015					     ip++) {
   1016						page = pages[ip];
   1017						unlock_page(page);
   1018						put_page(page);
   1019					}
   1020					goto out;
   1021				}
   1022			}
   1023		}
   1024
   1025		WARN_ON(!bytes);
   1026		copied = 0;
   1027		ip = off >> PAGE_SHIFT;
   1028		off = offset_in_page(pos);
   1029
   1030		/* Copy user data to pages. */
   1031		for (;;) {
   1032			size_t cp, tail = PAGE_SIZE - off;
   1033
   1034			page = pages[ip];
   1035			cp = copy_page_from_iter_atomic(page, off,
   1036							min(tail, bytes), from);
   1037			flush_dcache_page(page);
   1038
   1039			copied += cp;
   1040			bytes -= cp;
   1041			if (!bytes || !cp)
   1042				break;
   1043
   1044			if (cp < tail) {
   1045				off += cp;
   1046			} else {
   1047				ip++;
   1048				off = 0;
   1049			}
   1050		}
   1051
   1052		ni_lock(ni);
   1053		err = ni_write_frame(ni, pages, pages_per_frame);
   1054		ni_unlock(ni);
   1055
   1056		for (ip = 0; ip < pages_per_frame; ip++) {
   1057			page = pages[ip];
   1058			ClearPageDirty(page);
   1059			SetPageUptodate(page);
   1060			unlock_page(page);
   1061			put_page(page);
   1062		}
   1063
   1064		if (err)
   1065			goto out;
   1066
   1067		/*
   1068		 * We can loop for a long time in here. Be nice and allow
   1069		 * us to schedule out to avoid softlocking if preempt
   1070		 * is disabled.
   1071		 */
   1072		cond_resched();
   1073
   1074		pos += copied;
   1075		written += copied;
   1076
   1077		count = iov_iter_count(from);
   1078	}
   1079
   1080out:
   1081	kfree(pages);
   1082
   1083	current->backing_dev_info = NULL;
   1084
   1085	if (err < 0)
   1086		return err;
   1087
   1088	iocb->ki_pos += written;
   1089	if (iocb->ki_pos > ni->i_valid)
   1090		ni->i_valid = iocb->ki_pos;
   1091
   1092	return written;
   1093}
   1094
   1095/*
   1096 * ntfs_file_write_iter - file_operations::write_iter
   1097 */
   1098static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
   1099{
   1100	struct file *file = iocb->ki_filp;
   1101	struct address_space *mapping = file->f_mapping;
   1102	struct inode *inode = mapping->host;
   1103	ssize_t ret;
   1104	struct ntfs_inode *ni = ntfs_i(inode);
   1105
   1106	if (is_encrypted(ni)) {
   1107		ntfs_inode_warn(inode, "encrypted i/o not supported");
   1108		return -EOPNOTSUPP;
   1109	}
   1110
   1111	if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
   1112		ntfs_inode_warn(inode, "direct i/o + compressed not supported");
   1113		return -EOPNOTSUPP;
   1114	}
   1115
   1116	if (is_dedup(ni)) {
   1117		ntfs_inode_warn(inode, "write into deduplicated not supported");
   1118		return -EOPNOTSUPP;
   1119	}
   1120
   1121	if (!inode_trylock(inode)) {
   1122		if (iocb->ki_flags & IOCB_NOWAIT)
   1123			return -EAGAIN;
   1124		inode_lock(inode);
   1125	}
   1126
   1127	ret = generic_write_checks(iocb, from);
   1128	if (ret <= 0)
   1129		goto out;
   1130
   1131	if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
   1132		/* Should never be here, see ntfs_file_open(). */
   1133		ret = -EOPNOTSUPP;
   1134		goto out;
   1135	}
   1136
   1137	ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
   1138	if (ret)
   1139		goto out;
   1140
   1141	ret = is_compressed(ni) ? ntfs_compress_write(iocb, from)
   1142				: __generic_file_write_iter(iocb, from);
   1143
   1144out:
   1145	inode_unlock(inode);
   1146
   1147	if (ret > 0)
   1148		ret = generic_write_sync(iocb, ret);
   1149
   1150	return ret;
   1151}
   1152
   1153/*
   1154 * ntfs_file_open - file_operations::open
   1155 */
   1156int ntfs_file_open(struct inode *inode, struct file *file)
   1157{
   1158	struct ntfs_inode *ni = ntfs_i(inode);
   1159
   1160	if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
   1161		     (file->f_flags & O_DIRECT))) {
   1162		return -EOPNOTSUPP;
   1163	}
   1164
   1165	/* Decompress "external compressed" file if opened for rw. */
   1166	if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
   1167	    (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
   1168#ifdef CONFIG_NTFS3_LZX_XPRESS
   1169		int err = ni_decompress_file(ni);
   1170
   1171		if (err)
   1172			return err;
   1173#else
   1174		ntfs_inode_warn(
   1175			inode,
   1176			"activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
   1177		return -EOPNOTSUPP;
   1178#endif
   1179	}
   1180
   1181	return generic_file_open(inode, file);
   1182}
   1183
   1184/*
   1185 * ntfs_file_release - file_operations::release
   1186 */
   1187static int ntfs_file_release(struct inode *inode, struct file *file)
   1188{
   1189	struct ntfs_inode *ni = ntfs_i(inode);
   1190	struct ntfs_sb_info *sbi = ni->mi.sbi;
   1191	int err = 0;
   1192
   1193	/* If we are last writer on the inode, drop the block reservation. */
   1194	if (sbi->options->prealloc && ((file->f_mode & FMODE_WRITE) &&
   1195				      atomic_read(&inode->i_writecount) == 1)) {
   1196		ni_lock(ni);
   1197		down_write(&ni->file.run_lock);
   1198
   1199		err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
   1200				    inode->i_size, &ni->i_valid, false, NULL);
   1201
   1202		up_write(&ni->file.run_lock);
   1203		ni_unlock(ni);
   1204	}
   1205	return err;
   1206}
   1207
   1208/*
   1209 * ntfs_fiemap - file_operations::fiemap
   1210 */
   1211int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
   1212		__u64 start, __u64 len)
   1213{
   1214	int err;
   1215	struct ntfs_inode *ni = ntfs_i(inode);
   1216
   1217	err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
   1218	if (err)
   1219		return err;
   1220
   1221	ni_lock(ni);
   1222
   1223	err = ni_fiemap(ni, fieinfo, start, len);
   1224
   1225	ni_unlock(ni);
   1226
   1227	return err;
   1228}
   1229
   1230// clang-format off
   1231const struct inode_operations ntfs_file_inode_operations = {
   1232	.getattr	= ntfs_getattr,
   1233	.setattr	= ntfs3_setattr,
   1234	.listxattr	= ntfs_listxattr,
   1235	.permission	= ntfs_permission,
   1236	.get_acl	= ntfs_get_acl,
   1237	.set_acl	= ntfs_set_acl,
   1238	.fiemap		= ntfs_fiemap,
   1239};
   1240
   1241const struct file_operations ntfs_file_operations = {
   1242	.llseek		= generic_file_llseek,
   1243	.read_iter	= ntfs_file_read_iter,
   1244	.write_iter	= ntfs_file_write_iter,
   1245	.unlocked_ioctl = ntfs_ioctl,
   1246#ifdef CONFIG_COMPAT
   1247	.compat_ioctl	= ntfs_compat_ioctl,
   1248#endif
   1249	.splice_read	= generic_file_splice_read,
   1250	.mmap		= ntfs_file_mmap,
   1251	.open		= ntfs_file_open,
   1252	.fsync		= generic_file_fsync,
   1253	.splice_write	= iter_file_splice_write,
   1254	.fallocate	= ntfs_fallocate,
   1255	.release	= ntfs_file_release,
   1256};
   1257// clang-format on