1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
6 #include <linux/slab.h>
7 #include <linux/compat.h>
8 #include <linux/cred.h>
9 #include <linux/buffer_head.h>
10 #include <linux/blkdev.h>
12 #include "exfat_raw.h"
15 static int exfat_cont_expand(struct inode *inode, loff_t size)
17 struct address_space *mapping = inode->i_mapping;
18 loff_t start = i_size_read(inode), count = size - i_size_read(inode);
21 err = generic_cont_expand_simple(inode, size);
25 inode->i_mtime = inode_set_ctime_current(inode);
26 mark_inode_dirty(inode);
31 err = filemap_fdatawrite_range(mapping, start, start + count - 1);
32 err2 = sync_mapping_buffers(mapping);
35 err2 = write_inode_now(inode, 1);
41 return filemap_fdatawait_range(mapping, start, start + count - 1);
44 static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
46 mode_t allow_utime = sbi->options.allow_utime;
48 if (!uid_eq(current_fsuid(), inode->i_uid)) {
49 if (in_group_p(inode->i_gid))
51 if (allow_utime & MAY_WRITE)
55 /* use a default check */
59 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
60 struct inode *inode, umode_t *mode_ptr)
62 mode_t i_mode, mask, perm;
64 i_mode = inode->i_mode;
66 mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ?
67 sbi->options.fs_fmask : sbi->options.fs_dmask;
68 perm = *mode_ptr & ~(S_IFMT | mask);
70 /* Of the r and x bits, all (subject to umask) must be present.*/
71 if ((perm & 0555) != (i_mode & 0555))
74 if (exfat_mode_can_hold_ro(inode)) {
76 * Of the w bits, either all (subject to umask) or none must
79 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
83 * If exfat_mode_can_hold_ro(inode) is false, can't change
86 if ((perm & 0222) != (0222 & ~mask))
90 *mode_ptr &= S_IFMT | perm;
95 /* resize the file length */
96 int __exfat_truncate(struct inode *inode)
98 unsigned int num_clusters_new, num_clusters_phys;
99 unsigned int last_clu = EXFAT_FREE_CLUSTER;
100 struct exfat_chain clu;
101 struct super_block *sb = inode->i_sb;
102 struct exfat_sb_info *sbi = EXFAT_SB(sb);
103 struct exfat_inode_info *ei = EXFAT_I(inode);
105 /* check if the given file ID is opened */
106 if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
109 exfat_set_volume_dirty(sb);
111 num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
112 num_clusters_phys = EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
114 exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
116 if (i_size_read(inode) > 0) {
118 * Truncate FAT chain num_clusters after the first cluster
119 * num_clusters = min(new, phys);
121 unsigned int num_clusters =
122 min(num_clusters_new, num_clusters_phys);
126 * (defensive coding - works fine even with corrupted FAT table
128 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
129 clu.dir += num_clusters;
130 clu.size -= num_clusters;
132 while (num_clusters > 0) {
134 if (exfat_get_next_cluster(sb, &(clu.dir)))
142 ei->flags = ALLOC_NO_FAT_CHAIN;
143 ei->start_clu = EXFAT_EOF_CLUSTER;
146 if (ei->type == TYPE_FILE)
147 ei->attr |= ATTR_ARCHIVE;
150 * update the directory entry
152 * If the directory entry is updated by mark_inode_dirty(), the
153 * directory entry will be written after a writeback cycle of
154 * updating the bitmap/FAT, which may result in clusters being
155 * freed but referenced by the directory entry in the event of a
156 * sudden power failure.
157 * __exfat_write_inode() is called for directory entry, bitmap
158 * and FAT to be written in a same writeback.
160 if (__exfat_write_inode(inode, inode_needs_sync(inode)))
163 /* cut off from the FAT chain */
164 if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
165 last_clu != EXFAT_EOF_CLUSTER) {
166 if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
170 /* invalidate cache and free the clusters */
171 /* clear exfat cache */
172 exfat_cache_inval_inode(inode);
174 /* hint information */
175 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
176 ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
178 /* hint_stat will be used if this is directory. */
179 ei->hint_stat.eidx = 0;
180 ei->hint_stat.clu = ei->start_clu;
181 ei->hint_femp.eidx = EXFAT_HINT_NONE;
183 /* free the clusters */
184 if (exfat_free_cluster(inode, &clu))
190 void exfat_truncate(struct inode *inode)
192 struct super_block *sb = inode->i_sb;
193 struct exfat_sb_info *sbi = EXFAT_SB(sb);
194 struct exfat_inode_info *ei = EXFAT_I(inode);
195 unsigned int blocksize = i_blocksize(inode);
199 mutex_lock(&sbi->s_lock);
200 if (ei->start_clu == 0) {
202 * Empty start_clu != ~0 (not allocated)
204 exfat_fs_error(sb, "tried to truncate zeroed cluster.");
208 err = __exfat_truncate(inode);
212 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
214 aligned_size = i_size_read(inode);
215 if (aligned_size & (blocksize - 1)) {
216 aligned_size |= (blocksize - 1);
220 if (ei->i_size_ondisk > i_size_read(inode))
221 ei->i_size_ondisk = aligned_size;
223 if (ei->i_size_aligned > i_size_read(inode))
224 ei->i_size_aligned = aligned_size;
225 mutex_unlock(&sbi->s_lock);
228 int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,
229 struct kstat *stat, unsigned int request_mask,
230 unsigned int query_flags)
232 struct inode *inode = d_backing_inode(path->dentry);
233 struct exfat_inode_info *ei = EXFAT_I(inode);
235 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
236 exfat_truncate_atime(&stat->atime);
237 stat->result_mask |= STATX_BTIME;
238 stat->btime.tv_sec = ei->i_crtime.tv_sec;
239 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
240 stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
244 int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
247 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
248 struct inode *inode = dentry->d_inode;
249 unsigned int ia_valid;
252 if ((attr->ia_valid & ATTR_SIZE) &&
253 attr->ia_size > i_size_read(inode)) {
254 error = exfat_cont_expand(inode, attr->ia_size);
255 if (error || attr->ia_valid == ATTR_SIZE)
257 attr->ia_valid &= ~ATTR_SIZE;
260 /* Check for setting the inode time. */
261 ia_valid = attr->ia_valid;
262 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
263 exfat_allow_set_time(sbi, inode)) {
264 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
268 error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
269 attr->ia_valid = ia_valid;
273 if (((attr->ia_valid & ATTR_UID) &&
274 !uid_eq(attr->ia_uid, sbi->options.fs_uid)) ||
275 ((attr->ia_valid & ATTR_GID) &&
276 !gid_eq(attr->ia_gid, sbi->options.fs_gid)) ||
277 ((attr->ia_valid & ATTR_MODE) &&
278 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
284 * We don't return -EPERM here. Yes, strange, but this is too
287 if (attr->ia_valid & ATTR_MODE) {
288 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
289 attr->ia_valid &= ~ATTR_MODE;
292 if (attr->ia_valid & ATTR_SIZE)
293 inode->i_mtime = inode_set_ctime_current(inode);
295 setattr_copy(&nop_mnt_idmap, inode, attr);
296 exfat_truncate_atime(&inode->i_atime);
298 if (attr->ia_valid & ATTR_SIZE) {
299 error = exfat_block_truncate_page(inode, attr->ia_size);
303 down_write(&EXFAT_I(inode)->truncate_lock);
304 truncate_setsize(inode, attr->ia_size);
307 * __exfat_write_inode() is called from exfat_truncate(), inode
308 * is already written by it, so mark_inode_dirty() is unneeded.
310 exfat_truncate(inode);
311 up_write(&EXFAT_I(inode)->truncate_lock);
313 mark_inode_dirty(inode);
319 static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
321 struct fstrim_range range;
324 if (!capable(CAP_SYS_ADMIN))
327 if (!bdev_max_discard_sectors(inode->i_sb->s_bdev))
330 if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
333 range.minlen = max_t(unsigned int, range.minlen,
334 bdev_discard_granularity(inode->i_sb->s_bdev));
336 ret = exfat_trim_fs(inode, &range);
340 if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
346 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
348 struct inode *inode = file_inode(filp);
352 return exfat_ioctl_fitrim(inode, arg);
359 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
362 return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
366 int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
368 struct inode *inode = filp->f_mapping->host;
371 err = __generic_file_fsync(filp, start, end, datasync);
375 err = sync_blockdev(inode->i_sb->s_bdev);
379 return blkdev_issue_flush(inode->i_sb->s_bdev);
382 const struct file_operations exfat_file_operations = {
383 .llseek = generic_file_llseek,
384 .read_iter = generic_file_read_iter,
385 .write_iter = generic_file_write_iter,
386 .unlocked_ioctl = exfat_ioctl,
388 .compat_ioctl = exfat_compat_ioctl,
390 .mmap = generic_file_mmap,
391 .fsync = exfat_file_fsync,
392 .splice_read = filemap_splice_read,
393 .splice_write = iter_file_splice_write,
396 const struct inode_operations exfat_file_inode_operations = {
397 .setattr = exfat_setattr,
398 .getattr = exfat_getattr,