xref: /openbmc/linux/fs/exfat/file.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
198d91704SNamjae Jeon // SPDX-License-Identifier: GPL-2.0-or-later
298d91704SNamjae Jeon /*
398d91704SNamjae Jeon  * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
498d91704SNamjae Jeon  */
598d91704SNamjae Jeon 
698d91704SNamjae Jeon #include <linux/slab.h>
7654762dfSHyeongseok Kim #include <linux/compat.h>
898d91704SNamjae Jeon #include <linux/cred.h>
998d91704SNamjae Jeon #include <linux/buffer_head.h>
105267456eSSungjong Seo #include <linux/blkdev.h>
1198d91704SNamjae Jeon 
1298d91704SNamjae Jeon #include "exfat_raw.h"
1398d91704SNamjae Jeon #include "exfat_fs.h"
1498d91704SNamjae Jeon 
exfat_cont_expand(struct inode * inode,loff_t size)1598d91704SNamjae Jeon static int exfat_cont_expand(struct inode *inode, loff_t size)
1698d91704SNamjae Jeon {
1798d91704SNamjae Jeon 	struct address_space *mapping = inode->i_mapping;
1898d91704SNamjae Jeon 	loff_t start = i_size_read(inode), count = size - i_size_read(inode);
1998d91704SNamjae Jeon 	int err, err2;
2098d91704SNamjae Jeon 
2198d91704SNamjae Jeon 	err = generic_cont_expand_simple(inode, size);
2298d91704SNamjae Jeon 	if (err)
2398d91704SNamjae Jeon 		return err;
2498d91704SNamjae Jeon 
25f29f1908SJeff Layton 	inode->i_mtime = inode_set_ctime_current(inode);
2698d91704SNamjae Jeon 	mark_inode_dirty(inode);
2798d91704SNamjae Jeon 
2898d91704SNamjae Jeon 	if (!IS_SYNC(inode))
2998d91704SNamjae Jeon 		return 0;
3098d91704SNamjae Jeon 
3198d91704SNamjae Jeon 	err = filemap_fdatawrite_range(mapping, start, start + count - 1);
3298d91704SNamjae Jeon 	err2 = sync_mapping_buffers(mapping);
3398d91704SNamjae Jeon 	if (!err)
3498d91704SNamjae Jeon 		err = err2;
3598d91704SNamjae Jeon 	err2 = write_inode_now(inode, 1);
3698d91704SNamjae Jeon 	if (!err)
3798d91704SNamjae Jeon 		err = err2;
3898d91704SNamjae Jeon 	if (err)
3998d91704SNamjae Jeon 		return err;
4098d91704SNamjae Jeon 
4198d91704SNamjae Jeon 	return filemap_fdatawait_range(mapping, start, start + count - 1);
4298d91704SNamjae Jeon }
4398d91704SNamjae Jeon 
exfat_allow_set_time(struct exfat_sb_info * sbi,struct inode * inode)4498d91704SNamjae Jeon static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
4598d91704SNamjae Jeon {
4698d91704SNamjae Jeon 	mode_t allow_utime = sbi->options.allow_utime;
4798d91704SNamjae Jeon 
4898d91704SNamjae Jeon 	if (!uid_eq(current_fsuid(), inode->i_uid)) {
4998d91704SNamjae Jeon 		if (in_group_p(inode->i_gid))
5098d91704SNamjae Jeon 			allow_utime >>= 3;
5198d91704SNamjae Jeon 		if (allow_utime & MAY_WRITE)
5298d91704SNamjae Jeon 			return true;
5398d91704SNamjae Jeon 	}
5498d91704SNamjae Jeon 
5598d91704SNamjae Jeon 	/* use a default check */
5698d91704SNamjae Jeon 	return false;
5798d91704SNamjae Jeon }
5898d91704SNamjae Jeon 
exfat_sanitize_mode(const struct exfat_sb_info * sbi,struct inode * inode,umode_t * mode_ptr)5998d91704SNamjae Jeon static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
6098d91704SNamjae Jeon 		struct inode *inode, umode_t *mode_ptr)
6198d91704SNamjae Jeon {
6298d91704SNamjae Jeon 	mode_t i_mode, mask, perm;
6398d91704SNamjae Jeon 
6498d91704SNamjae Jeon 	i_mode = inode->i_mode;
6598d91704SNamjae Jeon 
6698d91704SNamjae Jeon 	mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ?
6798d91704SNamjae Jeon 		sbi->options.fs_fmask : sbi->options.fs_dmask;
6898d91704SNamjae Jeon 	perm = *mode_ptr & ~(S_IFMT | mask);
6998d91704SNamjae Jeon 
7098d91704SNamjae Jeon 	/* Of the r and x bits, all (subject to umask) must be present.*/
7198d91704SNamjae Jeon 	if ((perm & 0555) != (i_mode & 0555))
7298d91704SNamjae Jeon 		return -EPERM;
7398d91704SNamjae Jeon 
7498d91704SNamjae Jeon 	if (exfat_mode_can_hold_ro(inode)) {
7598d91704SNamjae Jeon 		/*
7698d91704SNamjae Jeon 		 * Of the w bits, either all (subject to umask) or none must
7798d91704SNamjae Jeon 		 * be present.
7898d91704SNamjae Jeon 		 */
7998d91704SNamjae Jeon 		if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
8098d91704SNamjae Jeon 			return -EPERM;
8198d91704SNamjae Jeon 	} else {
8298d91704SNamjae Jeon 		/*
8398d91704SNamjae Jeon 		 * If exfat_mode_can_hold_ro(inode) is false, can't change
8498d91704SNamjae Jeon 		 * w bits.
8598d91704SNamjae Jeon 		 */
8698d91704SNamjae Jeon 		if ((perm & 0222) != (0222 & ~mask))
8798d91704SNamjae Jeon 			return -EPERM;
8898d91704SNamjae Jeon 	}
8998d91704SNamjae Jeon 
9098d91704SNamjae Jeon 	*mode_ptr &= S_IFMT | perm;
9198d91704SNamjae Jeon 
9298d91704SNamjae Jeon 	return 0;
9398d91704SNamjae Jeon }
9498d91704SNamjae Jeon 
9598d91704SNamjae Jeon /* resize the file length */
__exfat_truncate(struct inode * inode)96f7cde967SYuezhang Mo int __exfat_truncate(struct inode *inode)
9798d91704SNamjae Jeon {
9898d91704SNamjae Jeon 	unsigned int num_clusters_new, num_clusters_phys;
9998d91704SNamjae Jeon 	unsigned int last_clu = EXFAT_FREE_CLUSTER;
10098d91704SNamjae Jeon 	struct exfat_chain clu;
10198d91704SNamjae Jeon 	struct super_block *sb = inode->i_sb;
10298d91704SNamjae Jeon 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
10398d91704SNamjae Jeon 	struct exfat_inode_info *ei = EXFAT_I(inode);
10498d91704SNamjae Jeon 
10598d91704SNamjae Jeon 	/* check if the given file ID is opened */
10698d91704SNamjae Jeon 	if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
10798d91704SNamjae Jeon 		return -EPERM;
10898d91704SNamjae Jeon 
1097018ec68STetsuhiro Kohada 	exfat_set_volume_dirty(sb);
11098d91704SNamjae Jeon 
11198d91704SNamjae Jeon 	num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
1127dee6f57SChristophe Vu-Brugier 	num_clusters_phys = EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
11398d91704SNamjae Jeon 
11498d91704SNamjae Jeon 	exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
11598d91704SNamjae Jeon 
116f7cde967SYuezhang Mo 	if (i_size_read(inode) > 0) {
11798d91704SNamjae Jeon 		/*
11898d91704SNamjae Jeon 		 * Truncate FAT chain num_clusters after the first cluster
11998d91704SNamjae Jeon 		 * num_clusters = min(new, phys);
12098d91704SNamjae Jeon 		 */
12198d91704SNamjae Jeon 		unsigned int num_clusters =
12298d91704SNamjae Jeon 			min(num_clusters_new, num_clusters_phys);
12398d91704SNamjae Jeon 
12498d91704SNamjae Jeon 		/*
12598d91704SNamjae Jeon 		 * Follow FAT chain
12698d91704SNamjae Jeon 		 * (defensive coding - works fine even with corrupted FAT table
12798d91704SNamjae Jeon 		 */
12898d91704SNamjae Jeon 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
12998d91704SNamjae Jeon 			clu.dir += num_clusters;
13098d91704SNamjae Jeon 			clu.size -= num_clusters;
13198d91704SNamjae Jeon 		} else {
13298d91704SNamjae Jeon 			while (num_clusters > 0) {
13398d91704SNamjae Jeon 				last_clu = clu.dir;
13498d91704SNamjae Jeon 				if (exfat_get_next_cluster(sb, &(clu.dir)))
13598d91704SNamjae Jeon 					return -EIO;
13698d91704SNamjae Jeon 
13798d91704SNamjae Jeon 				num_clusters--;
13898d91704SNamjae Jeon 				clu.size--;
13998d91704SNamjae Jeon 			}
14098d91704SNamjae Jeon 		}
14198d91704SNamjae Jeon 	} else {
14298d91704SNamjae Jeon 		ei->flags = ALLOC_NO_FAT_CHAIN;
14398d91704SNamjae Jeon 		ei->start_clu = EXFAT_EOF_CLUSTER;
14498d91704SNamjae Jeon 	}
14598d91704SNamjae Jeon 
14698d91704SNamjae Jeon 	if (ei->type == TYPE_FILE)
14798d91704SNamjae Jeon 		ei->attr |= ATTR_ARCHIVE;
14898d91704SNamjae Jeon 
1494493895bSYuezhang Mo 	/*
1504493895bSYuezhang Mo 	 * update the directory entry
1514493895bSYuezhang Mo 	 *
1524493895bSYuezhang Mo 	 * If the directory entry is updated by mark_inode_dirty(), the
1534493895bSYuezhang Mo 	 * directory entry will be written after a writeback cycle of
1544493895bSYuezhang Mo 	 * updating the bitmap/FAT, which may result in clusters being
1554493895bSYuezhang Mo 	 * freed but referenced by the directory entry in the event of a
1564493895bSYuezhang Mo 	 * sudden power failure.
1574493895bSYuezhang Mo 	 * __exfat_write_inode() is called for directory entry, bitmap
1584493895bSYuezhang Mo 	 * and FAT to be written in a same writeback.
1594493895bSYuezhang Mo 	 */
16023e6e1c9SYuezhang Mo 	if (__exfat_write_inode(inode, inode_needs_sync(inode)))
16198d91704SNamjae Jeon 		return -EIO;
16298d91704SNamjae Jeon 
16398d91704SNamjae Jeon 	/* cut off from the FAT chain */
16498d91704SNamjae Jeon 	if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
16598d91704SNamjae Jeon 			last_clu != EXFAT_EOF_CLUSTER) {
16698d91704SNamjae Jeon 		if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
16798d91704SNamjae Jeon 			return -EIO;
16898d91704SNamjae Jeon 	}
16998d91704SNamjae Jeon 
17098d91704SNamjae Jeon 	/* invalidate cache and free the clusters */
17198d91704SNamjae Jeon 	/* clear exfat cache */
17298d91704SNamjae Jeon 	exfat_cache_inval_inode(inode);
17398d91704SNamjae Jeon 
17498d91704SNamjae Jeon 	/* hint information */
17598d91704SNamjae Jeon 	ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
17698d91704SNamjae Jeon 	ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
17798d91704SNamjae Jeon 
17898d91704SNamjae Jeon 	/* hint_stat will be used if this is directory. */
17998d91704SNamjae Jeon 	ei->hint_stat.eidx = 0;
18098d91704SNamjae Jeon 	ei->hint_stat.clu = ei->start_clu;
18198d91704SNamjae Jeon 	ei->hint_femp.eidx = EXFAT_HINT_NONE;
18298d91704SNamjae Jeon 
18398d91704SNamjae Jeon 	/* free the clusters */
18498d91704SNamjae Jeon 	if (exfat_free_cluster(inode, &clu))
18598d91704SNamjae Jeon 		return -EIO;
18698d91704SNamjae Jeon 
18798d91704SNamjae Jeon 	return 0;
18898d91704SNamjae Jeon }
18998d91704SNamjae Jeon 
exfat_truncate(struct inode * inode)190e981917bSYuezhang Mo void exfat_truncate(struct inode *inode)
19198d91704SNamjae Jeon {
19298d91704SNamjae Jeon 	struct super_block *sb = inode->i_sb;
19398d91704SNamjae Jeon 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
1947dee6f57SChristophe Vu-Brugier 	struct exfat_inode_info *ei = EXFAT_I(inode);
19545882a6aSXianting Tian 	unsigned int blocksize = i_blocksize(inode);
19698d91704SNamjae Jeon 	loff_t aligned_size;
19798d91704SNamjae Jeon 	int err;
19898d91704SNamjae Jeon 
19998d91704SNamjae Jeon 	mutex_lock(&sbi->s_lock);
2007dee6f57SChristophe Vu-Brugier 	if (ei->start_clu == 0) {
20198d91704SNamjae Jeon 		/*
20298d91704SNamjae Jeon 		 * Empty start_clu != ~0 (not allocated)
20398d91704SNamjae Jeon 		 */
20498d91704SNamjae Jeon 		exfat_fs_error(sb, "tried to truncate zeroed cluster.");
20598d91704SNamjae Jeon 		goto write_size;
20698d91704SNamjae Jeon 	}
20798d91704SNamjae Jeon 
208f7cde967SYuezhang Mo 	err = __exfat_truncate(inode);
20998d91704SNamjae Jeon 	if (err)
21098d91704SNamjae Jeon 		goto write_size;
21198d91704SNamjae Jeon 
21239c1ce8eSYuezhang Mo 	inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
21398d91704SNamjae Jeon write_size:
21498d91704SNamjae Jeon 	aligned_size = i_size_read(inode);
21598d91704SNamjae Jeon 	if (aligned_size & (blocksize - 1)) {
21698d91704SNamjae Jeon 		aligned_size |= (blocksize - 1);
21798d91704SNamjae Jeon 		aligned_size++;
21898d91704SNamjae Jeon 	}
21998d91704SNamjae Jeon 
2207dee6f57SChristophe Vu-Brugier 	if (ei->i_size_ondisk > i_size_read(inode))
2217dee6f57SChristophe Vu-Brugier 		ei->i_size_ondisk = aligned_size;
22298d91704SNamjae Jeon 
2237dee6f57SChristophe Vu-Brugier 	if (ei->i_size_aligned > i_size_read(inode))
2247dee6f57SChristophe Vu-Brugier 		ei->i_size_aligned = aligned_size;
22598d91704SNamjae Jeon 	mutex_unlock(&sbi->s_lock);
22698d91704SNamjae Jeon }
22798d91704SNamjae Jeon 
exfat_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,unsigned int request_mask,unsigned int query_flags)228b74d24f7SChristian Brauner int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,
229549c7297SChristian Brauner 		  struct kstat *stat, unsigned int request_mask,
230549c7297SChristian Brauner 		  unsigned int query_flags)
23198d91704SNamjae Jeon {
23298d91704SNamjae Jeon 	struct inode *inode = d_backing_inode(path->dentry);
23398d91704SNamjae Jeon 	struct exfat_inode_info *ei = EXFAT_I(inode);
23498d91704SNamjae Jeon 
235*0d72b928SJeff Layton 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
23681df1ad4SEric Sandeen 	exfat_truncate_atime(&stat->atime);
23798d91704SNamjae Jeon 	stat->result_mask |= STATX_BTIME;
23898d91704SNamjae Jeon 	stat->btime.tv_sec = ei->i_crtime.tv_sec;
23998d91704SNamjae Jeon 	stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
24098d91704SNamjae Jeon 	stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
24198d91704SNamjae Jeon 	return 0;
24298d91704SNamjae Jeon }
24398d91704SNamjae Jeon 
exfat_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)244c1632a0fSChristian Brauner int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
245549c7297SChristian Brauner 		  struct iattr *attr)
24698d91704SNamjae Jeon {
24798d91704SNamjae Jeon 	struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
24898d91704SNamjae Jeon 	struct inode *inode = dentry->d_inode;
24998d91704SNamjae Jeon 	unsigned int ia_valid;
25098d91704SNamjae Jeon 	int error;
25198d91704SNamjae Jeon 
25298d91704SNamjae Jeon 	if ((attr->ia_valid & ATTR_SIZE) &&
25398d91704SNamjae Jeon 	    attr->ia_size > i_size_read(inode)) {
25498d91704SNamjae Jeon 		error = exfat_cont_expand(inode, attr->ia_size);
25598d91704SNamjae Jeon 		if (error || attr->ia_valid == ATTR_SIZE)
25698d91704SNamjae Jeon 			return error;
25798d91704SNamjae Jeon 		attr->ia_valid &= ~ATTR_SIZE;
25898d91704SNamjae Jeon 	}
25998d91704SNamjae Jeon 
26098d91704SNamjae Jeon 	/* Check for setting the inode time. */
26198d91704SNamjae Jeon 	ia_valid = attr->ia_valid;
26298d91704SNamjae Jeon 	if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
26398d91704SNamjae Jeon 	    exfat_allow_set_time(sbi, inode)) {
26498d91704SNamjae Jeon 		attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
26598d91704SNamjae Jeon 				ATTR_TIMES_SET);
26698d91704SNamjae Jeon 	}
26798d91704SNamjae Jeon 
268c1632a0fSChristian Brauner 	error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
26998d91704SNamjae Jeon 	attr->ia_valid = ia_valid;
27098d91704SNamjae Jeon 	if (error)
27198d91704SNamjae Jeon 		goto out;
27298d91704SNamjae Jeon 
27398d91704SNamjae Jeon 	if (((attr->ia_valid & ATTR_UID) &&
27498d91704SNamjae Jeon 	     !uid_eq(attr->ia_uid, sbi->options.fs_uid)) ||
27598d91704SNamjae Jeon 	    ((attr->ia_valid & ATTR_GID) &&
27698d91704SNamjae Jeon 	     !gid_eq(attr->ia_gid, sbi->options.fs_gid)) ||
27798d91704SNamjae Jeon 	    ((attr->ia_valid & ATTR_MODE) &&
27898d91704SNamjae Jeon 	     (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
27998d91704SNamjae Jeon 		error = -EPERM;
28098d91704SNamjae Jeon 		goto out;
28198d91704SNamjae Jeon 	}
28298d91704SNamjae Jeon 
28398d91704SNamjae Jeon 	/*
28498d91704SNamjae Jeon 	 * We don't return -EPERM here. Yes, strange, but this is too
28598d91704SNamjae Jeon 	 * old behavior.
28698d91704SNamjae Jeon 	 */
28798d91704SNamjae Jeon 	if (attr->ia_valid & ATTR_MODE) {
28898d91704SNamjae Jeon 		if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
28998d91704SNamjae Jeon 			attr->ia_valid &= ~ATTR_MODE;
29098d91704SNamjae Jeon 	}
29198d91704SNamjae Jeon 
2924493895bSYuezhang Mo 	if (attr->ia_valid & ATTR_SIZE)
293f29f1908SJeff Layton 		inode->i_mtime = inode_set_ctime_current(inode);
2944493895bSYuezhang Mo 
295c1632a0fSChristian Brauner 	setattr_copy(&nop_mnt_idmap, inode, attr);
2964493895bSYuezhang Mo 	exfat_truncate_atime(&inode->i_atime);
2974493895bSYuezhang Mo 
29898d91704SNamjae Jeon 	if (attr->ia_valid & ATTR_SIZE) {
29998d91704SNamjae Jeon 		error = exfat_block_truncate_page(inode, attr->ia_size);
30098d91704SNamjae Jeon 		if (error)
30198d91704SNamjae Jeon 			goto out;
30298d91704SNamjae Jeon 
30398d91704SNamjae Jeon 		down_write(&EXFAT_I(inode)->truncate_lock);
30498d91704SNamjae Jeon 		truncate_setsize(inode, attr->ia_size);
3054493895bSYuezhang Mo 
3064493895bSYuezhang Mo 		/*
3074493895bSYuezhang Mo 		 * __exfat_write_inode() is called from exfat_truncate(), inode
3084493895bSYuezhang Mo 		 * is already written by it, so mark_inode_dirty() is unneeded.
3094493895bSYuezhang Mo 		 */
310e981917bSYuezhang Mo 		exfat_truncate(inode);
31198d91704SNamjae Jeon 		up_write(&EXFAT_I(inode)->truncate_lock);
3124493895bSYuezhang Mo 	} else
31398d91704SNamjae Jeon 		mark_inode_dirty(inode);
31498d91704SNamjae Jeon 
31598d91704SNamjae Jeon out:
31698d91704SNamjae Jeon 	return error;
31798d91704SNamjae Jeon }
31898d91704SNamjae Jeon 
exfat_ioctl_fitrim(struct inode * inode,unsigned long arg)319654762dfSHyeongseok Kim static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
320654762dfSHyeongseok Kim {
321654762dfSHyeongseok Kim 	struct fstrim_range range;
322654762dfSHyeongseok Kim 	int ret = 0;
323654762dfSHyeongseok Kim 
324654762dfSHyeongseok Kim 	if (!capable(CAP_SYS_ADMIN))
325654762dfSHyeongseok Kim 		return -EPERM;
326654762dfSHyeongseok Kim 
32770200574SChristoph Hellwig 	if (!bdev_max_discard_sectors(inode->i_sb->s_bdev))
328654762dfSHyeongseok Kim 		return -EOPNOTSUPP;
329654762dfSHyeongseok Kim 
330654762dfSHyeongseok Kim 	if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
331654762dfSHyeongseok Kim 		return -EFAULT;
332654762dfSHyeongseok Kim 
333654762dfSHyeongseok Kim 	range.minlen = max_t(unsigned int, range.minlen,
3347b47ef52SChristoph Hellwig 				bdev_discard_granularity(inode->i_sb->s_bdev));
335654762dfSHyeongseok Kim 
336654762dfSHyeongseok Kim 	ret = exfat_trim_fs(inode, &range);
337654762dfSHyeongseok Kim 	if (ret < 0)
338654762dfSHyeongseok Kim 		return ret;
339654762dfSHyeongseok Kim 
340654762dfSHyeongseok Kim 	if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
341654762dfSHyeongseok Kim 		return -EFAULT;
342654762dfSHyeongseok Kim 
343654762dfSHyeongseok Kim 	return 0;
344654762dfSHyeongseok Kim }
345654762dfSHyeongseok Kim 
exfat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)346654762dfSHyeongseok Kim long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
347654762dfSHyeongseok Kim {
348654762dfSHyeongseok Kim 	struct inode *inode = file_inode(filp);
349654762dfSHyeongseok Kim 
350654762dfSHyeongseok Kim 	switch (cmd) {
351654762dfSHyeongseok Kim 	case FITRIM:
352654762dfSHyeongseok Kim 		return exfat_ioctl_fitrim(inode, arg);
353654762dfSHyeongseok Kim 	default:
354654762dfSHyeongseok Kim 		return -ENOTTY;
355654762dfSHyeongseok Kim 	}
356654762dfSHyeongseok Kim }
357654762dfSHyeongseok Kim 
358654762dfSHyeongseok Kim #ifdef CONFIG_COMPAT
exfat_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)359654762dfSHyeongseok Kim long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
360654762dfSHyeongseok Kim 				unsigned long arg)
361654762dfSHyeongseok Kim {
362654762dfSHyeongseok Kim 	return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
363654762dfSHyeongseok Kim }
364654762dfSHyeongseok Kim #endif
365654762dfSHyeongseok Kim 
exfat_file_fsync(struct file * filp,loff_t start,loff_t end,int datasync)3665267456eSSungjong Seo int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
3675267456eSSungjong Seo {
3685267456eSSungjong Seo 	struct inode *inode = filp->f_mapping->host;
3695267456eSSungjong Seo 	int err;
3705267456eSSungjong Seo 
3715267456eSSungjong Seo 	err = __generic_file_fsync(filp, start, end, datasync);
3725267456eSSungjong Seo 	if (err)
3735267456eSSungjong Seo 		return err;
3745267456eSSungjong Seo 
3755267456eSSungjong Seo 	err = sync_blockdev(inode->i_sb->s_bdev);
3765267456eSSungjong Seo 	if (err)
3775267456eSSungjong Seo 		return err;
3785267456eSSungjong Seo 
379c6bf3f0eSChristoph Hellwig 	return blkdev_issue_flush(inode->i_sb->s_bdev);
3805267456eSSungjong Seo }
3815267456eSSungjong Seo 
38298d91704SNamjae Jeon const struct file_operations exfat_file_operations = {
38398d91704SNamjae Jeon 	.llseek		= generic_file_llseek,
38498d91704SNamjae Jeon 	.read_iter	= generic_file_read_iter,
38598d91704SNamjae Jeon 	.write_iter	= generic_file_write_iter,
386654762dfSHyeongseok Kim 	.unlocked_ioctl = exfat_ioctl,
387654762dfSHyeongseok Kim #ifdef CONFIG_COMPAT
388654762dfSHyeongseok Kim 	.compat_ioctl = exfat_compat_ioctl,
389654762dfSHyeongseok Kim #endif
39098d91704SNamjae Jeon 	.mmap		= generic_file_mmap,
3915267456eSSungjong Seo 	.fsync		= exfat_file_fsync,
3922cb1e089SDavid Howells 	.splice_read	= filemap_splice_read,
39303577948SEric Sandeen 	.splice_write	= iter_file_splice_write,
39498d91704SNamjae Jeon };
39598d91704SNamjae Jeon 
39698d91704SNamjae Jeon const struct inode_operations exfat_file_inode_operations = {
39798d91704SNamjae Jeon 	.setattr     = exfat_setattr,
39898d91704SNamjae Jeon 	.getattr     = exfat_getattr,
39998d91704SNamjae Jeon };
400