xref: /openbmc/linux/fs/exfat/file.c (revision 654762df2ec7d61b05acc788afbffaba52d658fe)
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>
7*654762dfSHyeongseok 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 
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 
2598d91704SNamjae Jeon 	inode->i_ctime = inode->i_mtime = current_time(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 
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 
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 */
9698d91704SNamjae Jeon int __exfat_truncate(struct inode *inode, loff_t new_size)
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 	int evict = (ei->dir.dir == DIR_DELETED) ? 1 : 0;
10598d91704SNamjae Jeon 
10698d91704SNamjae Jeon 	/* check if the given file ID is opened */
10798d91704SNamjae Jeon 	if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
10898d91704SNamjae Jeon 		return -EPERM;
10998d91704SNamjae Jeon 
1107018ec68STetsuhiro Kohada 	exfat_set_volume_dirty(sb);
11198d91704SNamjae Jeon 
11298d91704SNamjae Jeon 	num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
11398d91704SNamjae Jeon 	num_clusters_phys =
11498d91704SNamjae Jeon 		EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk, sbi);
11598d91704SNamjae Jeon 
11698d91704SNamjae Jeon 	exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
11798d91704SNamjae Jeon 
11898d91704SNamjae Jeon 	if (new_size > 0) {
11998d91704SNamjae Jeon 		/*
12098d91704SNamjae Jeon 		 * Truncate FAT chain num_clusters after the first cluster
12198d91704SNamjae Jeon 		 * num_clusters = min(new, phys);
12298d91704SNamjae Jeon 		 */
12398d91704SNamjae Jeon 		unsigned int num_clusters =
12498d91704SNamjae Jeon 			min(num_clusters_new, num_clusters_phys);
12598d91704SNamjae Jeon 
12698d91704SNamjae Jeon 		/*
12798d91704SNamjae Jeon 		 * Follow FAT chain
12898d91704SNamjae Jeon 		 * (defensive coding - works fine even with corrupted FAT table
12998d91704SNamjae Jeon 		 */
13098d91704SNamjae Jeon 		if (clu.flags == ALLOC_NO_FAT_CHAIN) {
13198d91704SNamjae Jeon 			clu.dir += num_clusters;
13298d91704SNamjae Jeon 			clu.size -= num_clusters;
13398d91704SNamjae Jeon 		} else {
13498d91704SNamjae Jeon 			while (num_clusters > 0) {
13598d91704SNamjae Jeon 				last_clu = clu.dir;
13698d91704SNamjae Jeon 				if (exfat_get_next_cluster(sb, &(clu.dir)))
13798d91704SNamjae Jeon 					return -EIO;
13898d91704SNamjae Jeon 
13998d91704SNamjae Jeon 				num_clusters--;
14098d91704SNamjae Jeon 				clu.size--;
14198d91704SNamjae Jeon 			}
14298d91704SNamjae Jeon 		}
14398d91704SNamjae Jeon 	} else {
14498d91704SNamjae Jeon 		ei->flags = ALLOC_NO_FAT_CHAIN;
14598d91704SNamjae Jeon 		ei->start_clu = EXFAT_EOF_CLUSTER;
14698d91704SNamjae Jeon 	}
14798d91704SNamjae Jeon 
14898d91704SNamjae Jeon 	i_size_write(inode, new_size);
14998d91704SNamjae Jeon 
15098d91704SNamjae Jeon 	if (ei->type == TYPE_FILE)
15198d91704SNamjae Jeon 		ei->attr |= ATTR_ARCHIVE;
15298d91704SNamjae Jeon 
15398d91704SNamjae Jeon 	/* update the directory entry */
15498d91704SNamjae Jeon 	if (!evict) {
15598d91704SNamjae Jeon 		struct timespec64 ts;
156943af1fdSTetsuhiro Kohada 		struct exfat_dentry *ep, *ep2;
157943af1fdSTetsuhiro Kohada 		struct exfat_entry_set_cache *es;
1588b0c4717STetsuhiro Kohada 		int err;
15998d91704SNamjae Jeon 
16098d91704SNamjae Jeon 		es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry,
161943af1fdSTetsuhiro Kohada 				ES_ALL_ENTRIES);
16298d91704SNamjae Jeon 		if (!es)
16398d91704SNamjae Jeon 			return -EIO;
164943af1fdSTetsuhiro Kohada 		ep = exfat_get_dentry_cached(es, 0);
165943af1fdSTetsuhiro Kohada 		ep2 = exfat_get_dentry_cached(es, 1);
16698d91704SNamjae Jeon 
16798d91704SNamjae Jeon 		ts = current_time(inode);
16898d91704SNamjae Jeon 		exfat_set_entry_time(sbi, &ts,
16998d91704SNamjae Jeon 				&ep->dentry.file.modify_tz,
17098d91704SNamjae Jeon 				&ep->dentry.file.modify_time,
17198d91704SNamjae Jeon 				&ep->dentry.file.modify_date,
172ed0f84d3STetsuhiro Kohada 				&ep->dentry.file.modify_time_cs);
17398d91704SNamjae Jeon 		ep->dentry.file.attr = cpu_to_le16(ei->attr);
17498d91704SNamjae Jeon 
17598d91704SNamjae Jeon 		/* File size should be zero if there is no cluster allocated */
17698d91704SNamjae Jeon 		if (ei->start_clu == EXFAT_EOF_CLUSTER) {
17729bbb14bSNamjae Jeon 			ep2->dentry.stream.valid_size = 0;
17829bbb14bSNamjae Jeon 			ep2->dentry.stream.size = 0;
17998d91704SNamjae Jeon 		} else {
18029bbb14bSNamjae Jeon 			ep2->dentry.stream.valid_size = cpu_to_le64(new_size);
18141e3928fSHyeongseok Kim 			ep2->dentry.stream.size = ep2->dentry.stream.valid_size;
18298d91704SNamjae Jeon 		}
18398d91704SNamjae Jeon 
18498d91704SNamjae Jeon 		if (new_size == 0) {
18598d91704SNamjae Jeon 			/* Any directory can not be truncated to zero */
18698d91704SNamjae Jeon 			WARN_ON(ei->type != TYPE_FILE);
18798d91704SNamjae Jeon 
18898d91704SNamjae Jeon 			ep2->dentry.stream.flags = ALLOC_FAT_CHAIN;
18998d91704SNamjae Jeon 			ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER;
19098d91704SNamjae Jeon 		}
19198d91704SNamjae Jeon 
192943af1fdSTetsuhiro Kohada 		exfat_update_dir_chksum_with_entry_set(es);
1938b0c4717STetsuhiro Kohada 		err = exfat_free_dentry_set(es, inode_needs_sync(inode));
1948b0c4717STetsuhiro Kohada 		if (err)
1958b0c4717STetsuhiro Kohada 			return err;
19698d91704SNamjae Jeon 	}
19798d91704SNamjae Jeon 
19898d91704SNamjae Jeon 	/* cut off from the FAT chain */
19998d91704SNamjae Jeon 	if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
20098d91704SNamjae Jeon 			last_clu != EXFAT_EOF_CLUSTER) {
20198d91704SNamjae Jeon 		if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
20298d91704SNamjae Jeon 			return -EIO;
20398d91704SNamjae Jeon 	}
20498d91704SNamjae Jeon 
20598d91704SNamjae Jeon 	/* invalidate cache and free the clusters */
20698d91704SNamjae Jeon 	/* clear exfat cache */
20798d91704SNamjae Jeon 	exfat_cache_inval_inode(inode);
20898d91704SNamjae Jeon 
20998d91704SNamjae Jeon 	/* hint information */
21098d91704SNamjae Jeon 	ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
21198d91704SNamjae Jeon 	ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
21298d91704SNamjae Jeon 
21398d91704SNamjae Jeon 	/* hint_stat will be used if this is directory. */
21498d91704SNamjae Jeon 	ei->hint_stat.eidx = 0;
21598d91704SNamjae Jeon 	ei->hint_stat.clu = ei->start_clu;
21698d91704SNamjae Jeon 	ei->hint_femp.eidx = EXFAT_HINT_NONE;
21798d91704SNamjae Jeon 
21898d91704SNamjae Jeon 	/* free the clusters */
21998d91704SNamjae Jeon 	if (exfat_free_cluster(inode, &clu))
22098d91704SNamjae Jeon 		return -EIO;
22198d91704SNamjae Jeon 
2227018ec68STetsuhiro Kohada 	exfat_clear_volume_dirty(sb);
22398d91704SNamjae Jeon 
22498d91704SNamjae Jeon 	return 0;
22598d91704SNamjae Jeon }
22698d91704SNamjae Jeon 
22798d91704SNamjae Jeon void exfat_truncate(struct inode *inode, loff_t size)
22898d91704SNamjae Jeon {
22998d91704SNamjae Jeon 	struct super_block *sb = inode->i_sb;
23098d91704SNamjae Jeon 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
23145882a6aSXianting Tian 	unsigned int blocksize = i_blocksize(inode);
23298d91704SNamjae Jeon 	loff_t aligned_size;
23398d91704SNamjae Jeon 	int err;
23498d91704SNamjae Jeon 
23598d91704SNamjae Jeon 	mutex_lock(&sbi->s_lock);
23698d91704SNamjae Jeon 	if (EXFAT_I(inode)->start_clu == 0) {
23798d91704SNamjae Jeon 		/*
23898d91704SNamjae Jeon 		 * Empty start_clu != ~0 (not allocated)
23998d91704SNamjae Jeon 		 */
24098d91704SNamjae Jeon 		exfat_fs_error(sb, "tried to truncate zeroed cluster.");
24198d91704SNamjae Jeon 		goto write_size;
24298d91704SNamjae Jeon 	}
24398d91704SNamjae Jeon 
24498d91704SNamjae Jeon 	err = __exfat_truncate(inode, i_size_read(inode));
24598d91704SNamjae Jeon 	if (err)
24698d91704SNamjae Jeon 		goto write_size;
24798d91704SNamjae Jeon 
24898d91704SNamjae Jeon 	inode->i_ctime = inode->i_mtime = current_time(inode);
24998d91704SNamjae Jeon 	if (IS_DIRSYNC(inode))
25098d91704SNamjae Jeon 		exfat_sync_inode(inode);
25198d91704SNamjae Jeon 	else
25298d91704SNamjae Jeon 		mark_inode_dirty(inode);
25398d91704SNamjae Jeon 
25498d91704SNamjae Jeon 	inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) &
25598d91704SNamjae Jeon 			~(sbi->cluster_size - 1)) >> inode->i_blkbits;
25698d91704SNamjae Jeon write_size:
25798d91704SNamjae Jeon 	aligned_size = i_size_read(inode);
25898d91704SNamjae Jeon 	if (aligned_size & (blocksize - 1)) {
25998d91704SNamjae Jeon 		aligned_size |= (blocksize - 1);
26098d91704SNamjae Jeon 		aligned_size++;
26198d91704SNamjae Jeon 	}
26298d91704SNamjae Jeon 
26398d91704SNamjae Jeon 	if (EXFAT_I(inode)->i_size_ondisk > i_size_read(inode))
26498d91704SNamjae Jeon 		EXFAT_I(inode)->i_size_ondisk = aligned_size;
26598d91704SNamjae Jeon 
26698d91704SNamjae Jeon 	if (EXFAT_I(inode)->i_size_aligned > i_size_read(inode))
26798d91704SNamjae Jeon 		EXFAT_I(inode)->i_size_aligned = aligned_size;
26898d91704SNamjae Jeon 	mutex_unlock(&sbi->s_lock);
26998d91704SNamjae Jeon }
27098d91704SNamjae Jeon 
271549c7297SChristian Brauner int exfat_getattr(struct user_namespace *mnt_uerns, const struct path *path,
272549c7297SChristian Brauner 		  struct kstat *stat, unsigned int request_mask,
273549c7297SChristian Brauner 		  unsigned int query_flags)
27498d91704SNamjae Jeon {
27598d91704SNamjae Jeon 	struct inode *inode = d_backing_inode(path->dentry);
27698d91704SNamjae Jeon 	struct exfat_inode_info *ei = EXFAT_I(inode);
27798d91704SNamjae Jeon 
2780d56a451SChristian Brauner 	generic_fillattr(&init_user_ns, inode, stat);
27981df1ad4SEric Sandeen 	exfat_truncate_atime(&stat->atime);
28098d91704SNamjae Jeon 	stat->result_mask |= STATX_BTIME;
28198d91704SNamjae Jeon 	stat->btime.tv_sec = ei->i_crtime.tv_sec;
28298d91704SNamjae Jeon 	stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
28398d91704SNamjae Jeon 	stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
28498d91704SNamjae Jeon 	return 0;
28598d91704SNamjae Jeon }
28698d91704SNamjae Jeon 
287549c7297SChristian Brauner int exfat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
288549c7297SChristian Brauner 		  struct iattr *attr)
28998d91704SNamjae Jeon {
29098d91704SNamjae Jeon 	struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
29198d91704SNamjae Jeon 	struct inode *inode = dentry->d_inode;
29298d91704SNamjae Jeon 	unsigned int ia_valid;
29398d91704SNamjae Jeon 	int error;
29498d91704SNamjae Jeon 
29598d91704SNamjae Jeon 	if ((attr->ia_valid & ATTR_SIZE) &&
29698d91704SNamjae Jeon 	    attr->ia_size > i_size_read(inode)) {
29798d91704SNamjae Jeon 		error = exfat_cont_expand(inode, attr->ia_size);
29898d91704SNamjae Jeon 		if (error || attr->ia_valid == ATTR_SIZE)
29998d91704SNamjae Jeon 			return error;
30098d91704SNamjae Jeon 		attr->ia_valid &= ~ATTR_SIZE;
30198d91704SNamjae Jeon 	}
30298d91704SNamjae Jeon 
30398d91704SNamjae Jeon 	/* Check for setting the inode time. */
30498d91704SNamjae Jeon 	ia_valid = attr->ia_valid;
30598d91704SNamjae Jeon 	if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
30698d91704SNamjae Jeon 	    exfat_allow_set_time(sbi, inode)) {
30798d91704SNamjae Jeon 		attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
30898d91704SNamjae Jeon 				ATTR_TIMES_SET);
30998d91704SNamjae Jeon 	}
31098d91704SNamjae Jeon 
3112f221d6fSChristian Brauner 	error = setattr_prepare(&init_user_ns, dentry, attr);
31298d91704SNamjae Jeon 	attr->ia_valid = ia_valid;
31398d91704SNamjae Jeon 	if (error)
31498d91704SNamjae Jeon 		goto out;
31598d91704SNamjae Jeon 
31698d91704SNamjae Jeon 	if (((attr->ia_valid & ATTR_UID) &&
31798d91704SNamjae Jeon 	     !uid_eq(attr->ia_uid, sbi->options.fs_uid)) ||
31898d91704SNamjae Jeon 	    ((attr->ia_valid & ATTR_GID) &&
31998d91704SNamjae Jeon 	     !gid_eq(attr->ia_gid, sbi->options.fs_gid)) ||
32098d91704SNamjae Jeon 	    ((attr->ia_valid & ATTR_MODE) &&
32198d91704SNamjae Jeon 	     (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
32298d91704SNamjae Jeon 		error = -EPERM;
32398d91704SNamjae Jeon 		goto out;
32498d91704SNamjae Jeon 	}
32598d91704SNamjae Jeon 
32698d91704SNamjae Jeon 	/*
32798d91704SNamjae Jeon 	 * We don't return -EPERM here. Yes, strange, but this is too
32898d91704SNamjae Jeon 	 * old behavior.
32998d91704SNamjae Jeon 	 */
33098d91704SNamjae Jeon 	if (attr->ia_valid & ATTR_MODE) {
33198d91704SNamjae Jeon 		if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
33298d91704SNamjae Jeon 			attr->ia_valid &= ~ATTR_MODE;
33398d91704SNamjae Jeon 	}
33498d91704SNamjae Jeon 
33598d91704SNamjae Jeon 	if (attr->ia_valid & ATTR_SIZE) {
33698d91704SNamjae Jeon 		error = exfat_block_truncate_page(inode, attr->ia_size);
33798d91704SNamjae Jeon 		if (error)
33898d91704SNamjae Jeon 			goto out;
33998d91704SNamjae Jeon 
34098d91704SNamjae Jeon 		down_write(&EXFAT_I(inode)->truncate_lock);
34198d91704SNamjae Jeon 		truncate_setsize(inode, attr->ia_size);
34298d91704SNamjae Jeon 		exfat_truncate(inode, attr->ia_size);
34398d91704SNamjae Jeon 		up_write(&EXFAT_I(inode)->truncate_lock);
34498d91704SNamjae Jeon 	}
34598d91704SNamjae Jeon 
3462f221d6fSChristian Brauner 	setattr_copy(&init_user_ns, inode, attr);
34781df1ad4SEric Sandeen 	exfat_truncate_atime(&inode->i_atime);
34898d91704SNamjae Jeon 	mark_inode_dirty(inode);
34998d91704SNamjae Jeon 
35098d91704SNamjae Jeon out:
35198d91704SNamjae Jeon 	return error;
35298d91704SNamjae Jeon }
35398d91704SNamjae Jeon 
354*654762dfSHyeongseok Kim static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
355*654762dfSHyeongseok Kim {
356*654762dfSHyeongseok Kim 	struct request_queue *q = bdev_get_queue(inode->i_sb->s_bdev);
357*654762dfSHyeongseok Kim 	struct fstrim_range range;
358*654762dfSHyeongseok Kim 	int ret = 0;
359*654762dfSHyeongseok Kim 
360*654762dfSHyeongseok Kim 	if (!capable(CAP_SYS_ADMIN))
361*654762dfSHyeongseok Kim 		return -EPERM;
362*654762dfSHyeongseok Kim 
363*654762dfSHyeongseok Kim 	if (!blk_queue_discard(q))
364*654762dfSHyeongseok Kim 		return -EOPNOTSUPP;
365*654762dfSHyeongseok Kim 
366*654762dfSHyeongseok Kim 	if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
367*654762dfSHyeongseok Kim 		return -EFAULT;
368*654762dfSHyeongseok Kim 
369*654762dfSHyeongseok Kim 	range.minlen = max_t(unsigned int, range.minlen,
370*654762dfSHyeongseok Kim 				q->limits.discard_granularity);
371*654762dfSHyeongseok Kim 
372*654762dfSHyeongseok Kim 	ret = exfat_trim_fs(inode, &range);
373*654762dfSHyeongseok Kim 	if (ret < 0)
374*654762dfSHyeongseok Kim 		return ret;
375*654762dfSHyeongseok Kim 
376*654762dfSHyeongseok Kim 	if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
377*654762dfSHyeongseok Kim 		return -EFAULT;
378*654762dfSHyeongseok Kim 
379*654762dfSHyeongseok Kim 	return 0;
380*654762dfSHyeongseok Kim }
381*654762dfSHyeongseok Kim 
382*654762dfSHyeongseok Kim long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
383*654762dfSHyeongseok Kim {
384*654762dfSHyeongseok Kim 	struct inode *inode = file_inode(filp);
385*654762dfSHyeongseok Kim 
386*654762dfSHyeongseok Kim 	switch (cmd) {
387*654762dfSHyeongseok Kim 	case FITRIM:
388*654762dfSHyeongseok Kim 		return exfat_ioctl_fitrim(inode, arg);
389*654762dfSHyeongseok Kim 	default:
390*654762dfSHyeongseok Kim 		return -ENOTTY;
391*654762dfSHyeongseok Kim 	}
392*654762dfSHyeongseok Kim }
393*654762dfSHyeongseok Kim 
394*654762dfSHyeongseok Kim #ifdef CONFIG_COMPAT
395*654762dfSHyeongseok Kim long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
396*654762dfSHyeongseok Kim 				unsigned long arg)
397*654762dfSHyeongseok Kim {
398*654762dfSHyeongseok Kim 	return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
399*654762dfSHyeongseok Kim }
400*654762dfSHyeongseok Kim #endif
401*654762dfSHyeongseok Kim 
4025267456eSSungjong Seo int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
4035267456eSSungjong Seo {
4045267456eSSungjong Seo 	struct inode *inode = filp->f_mapping->host;
4055267456eSSungjong Seo 	int err;
4065267456eSSungjong Seo 
4075267456eSSungjong Seo 	err = __generic_file_fsync(filp, start, end, datasync);
4085267456eSSungjong Seo 	if (err)
4095267456eSSungjong Seo 		return err;
4105267456eSSungjong Seo 
4115267456eSSungjong Seo 	err = sync_blockdev(inode->i_sb->s_bdev);
4125267456eSSungjong Seo 	if (err)
4135267456eSSungjong Seo 		return err;
4145267456eSSungjong Seo 
415c6bf3f0eSChristoph Hellwig 	return blkdev_issue_flush(inode->i_sb->s_bdev);
4165267456eSSungjong Seo }
4175267456eSSungjong Seo 
41898d91704SNamjae Jeon const struct file_operations exfat_file_operations = {
41998d91704SNamjae Jeon 	.llseek		= generic_file_llseek,
42098d91704SNamjae Jeon 	.read_iter	= generic_file_read_iter,
42198d91704SNamjae Jeon 	.write_iter	= generic_file_write_iter,
422*654762dfSHyeongseok Kim 	.unlocked_ioctl = exfat_ioctl,
423*654762dfSHyeongseok Kim #ifdef CONFIG_COMPAT
424*654762dfSHyeongseok Kim 	.compat_ioctl = exfat_compat_ioctl,
425*654762dfSHyeongseok Kim #endif
42698d91704SNamjae Jeon 	.mmap		= generic_file_mmap,
4275267456eSSungjong Seo 	.fsync		= exfat_file_fsync,
42898d91704SNamjae Jeon 	.splice_read	= generic_file_splice_read,
42903577948SEric Sandeen 	.splice_write	= iter_file_splice_write,
43098d91704SNamjae Jeon };
43198d91704SNamjae Jeon 
43298d91704SNamjae Jeon const struct inode_operations exfat_file_inode_operations = {
43398d91704SNamjae Jeon 	.setattr     = exfat_setattr,
43498d91704SNamjae Jeon 	.getattr     = exfat_getattr,
43598d91704SNamjae Jeon };
436