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>
11 #include <linux/fsnotify.h>
12 #include <linux/security.h>
13 #include <linux/msdos_fs.h>
14 #include <linux/writeback.h>
16 #include "exfat_raw.h"
19 static int exfat_cont_expand(struct inode *inode, loff_t size)
22 unsigned int num_clusters, new_num_clusters, last_clu;
23 struct exfat_inode_info *ei = EXFAT_I(inode);
24 struct super_block *sb = inode->i_sb;
25 struct exfat_sb_info *sbi = EXFAT_SB(sb);
26 struct exfat_chain clu;
28 ret = inode_newsize_ok(inode, size);
32 num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
33 new_num_clusters = EXFAT_B_TO_CLU_ROUND_UP(size, sbi);
35 if (new_num_clusters == num_clusters)
39 exfat_chain_set(&clu, ei->start_clu, num_clusters, ei->flags);
40 ret = exfat_find_last_cluster(sb, &clu, &last_clu);
44 clu.dir = last_clu + 1;
46 last_clu = EXFAT_EOF_CLUSTER;
47 clu.dir = EXFAT_EOF_CLUSTER;
51 clu.flags = ei->flags;
53 ret = exfat_alloc_cluster(inode, new_num_clusters - num_clusters,
54 &clu, inode_needs_sync(inode));
58 /* Append new clusters to chain */
60 if (clu.flags != ei->flags)
61 if (exfat_chain_cont_cluster(sb, ei->start_clu, num_clusters))
64 if (clu.flags == ALLOC_FAT_CHAIN)
65 if (exfat_ent_set(sb, last_clu, clu.dir))
68 ei->start_clu = clu.dir;
70 ei->flags = clu.flags;
73 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
74 /* Expanded range not zeroed, do not update valid_size */
75 i_size_write(inode, size);
77 inode->i_blocks = round_up(size, sbi->cluster_size) >> 9;
78 mark_inode_dirty(inode);
81 return write_inode_now(inode, 1);
86 exfat_free_cluster(inode, &clu);
90 static bool exfat_allow_set_time(struct mnt_idmap *idmap,
91 struct exfat_sb_info *sbi, struct inode *inode)
93 mode_t allow_utime = sbi->options.allow_utime;
95 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode),
97 if (vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode)))
99 if (allow_utime & MAY_WRITE)
103 /* use a default check */
107 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
108 struct inode *inode, umode_t *mode_ptr)
110 mode_t i_mode, mask, perm;
112 i_mode = inode->i_mode;
114 mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ?
115 sbi->options.fs_fmask : sbi->options.fs_dmask;
116 perm = *mode_ptr & ~(S_IFMT | mask);
118 /* Of the r and x bits, all (subject to umask) must be present.*/
119 if ((perm & 0555) != (i_mode & 0555))
122 if (exfat_mode_can_hold_ro(inode)) {
124 * Of the w bits, either all (subject to umask) or none must
127 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
131 * If exfat_mode_can_hold_ro(inode) is false, can't change
134 if ((perm & 0222) != (0222 & ~mask))
138 *mode_ptr &= S_IFMT | perm;
143 /* resize the file length */
144 int __exfat_truncate(struct inode *inode)
146 unsigned int num_clusters_new, num_clusters_phys;
147 unsigned int last_clu = EXFAT_FREE_CLUSTER;
148 struct exfat_chain clu;
149 struct super_block *sb = inode->i_sb;
150 struct exfat_sb_info *sbi = EXFAT_SB(sb);
151 struct exfat_inode_info *ei = EXFAT_I(inode);
153 /* check if the given file ID is opened */
154 if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
157 exfat_set_volume_dirty(sb);
159 num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
160 num_clusters_phys = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
162 exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
164 if (i_size_read(inode) > 0) {
166 * Truncate FAT chain num_clusters after the first cluster
167 * num_clusters = min(new, phys);
169 unsigned int num_clusters =
170 min(num_clusters_new, num_clusters_phys);
174 * (defensive coding - works fine even with corrupted FAT table
176 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
177 clu.dir += num_clusters;
178 clu.size -= num_clusters;
180 while (num_clusters > 0) {
182 if (exfat_get_next_cluster(sb, &(clu.dir)))
190 ei->flags = ALLOC_NO_FAT_CHAIN;
191 ei->start_clu = EXFAT_EOF_CLUSTER;
194 if (i_size_read(inode) < ei->valid_size)
195 ei->valid_size = i_size_read(inode);
197 if (ei->type == TYPE_FILE)
198 ei->attr |= EXFAT_ATTR_ARCHIVE;
201 * update the directory entry
203 * If the directory entry is updated by mark_inode_dirty(), the
204 * directory entry will be written after a writeback cycle of
205 * updating the bitmap/FAT, which may result in clusters being
206 * freed but referenced by the directory entry in the event of a
207 * sudden power failure.
208 * __exfat_write_inode() is called for directory entry, bitmap
209 * and FAT to be written in a same writeback.
211 if (__exfat_write_inode(inode, inode_needs_sync(inode)))
214 /* cut off from the FAT chain */
215 if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
216 last_clu != EXFAT_EOF_CLUSTER) {
217 if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
221 /* invalidate cache and free the clusters */
222 /* clear exfat cache */
223 exfat_cache_inval_inode(inode);
225 /* hint information */
226 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
227 ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
229 /* hint_stat will be used if this is directory. */
230 ei->hint_stat.eidx = 0;
231 ei->hint_stat.clu = ei->start_clu;
232 ei->hint_femp.eidx = EXFAT_HINT_NONE;
234 /* free the clusters */
235 if (exfat_free_cluster(inode, &clu))
241 void exfat_truncate(struct inode *inode)
243 struct super_block *sb = inode->i_sb;
244 struct exfat_sb_info *sbi = EXFAT_SB(sb);
245 struct exfat_inode_info *ei = EXFAT_I(inode);
248 mutex_lock(&sbi->s_lock);
249 if (ei->start_clu == 0) {
251 * Empty start_clu != ~0 (not allocated)
253 exfat_fs_error(sb, "tried to truncate zeroed cluster.");
257 err = __exfat_truncate(inode);
261 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
263 mutex_unlock(&sbi->s_lock);
266 int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,
267 struct kstat *stat, unsigned int request_mask,
268 unsigned int query_flags)
270 struct inode *inode = d_backing_inode(path->dentry);
271 struct exfat_inode_info *ei = EXFAT_I(inode);
273 generic_fillattr(idmap, request_mask, inode, stat);
274 exfat_truncate_atime(&stat->atime);
275 stat->result_mask |= STATX_BTIME;
276 stat->btime.tv_sec = ei->i_crtime.tv_sec;
277 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
278 stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
282 int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
285 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
286 struct inode *inode = dentry->d_inode;
287 unsigned int ia_valid;
290 if (unlikely(exfat_forced_shutdown(inode->i_sb)))
293 if ((attr->ia_valid & ATTR_SIZE) &&
294 attr->ia_size > i_size_read(inode)) {
295 error = exfat_cont_expand(inode, attr->ia_size);
296 if (error || attr->ia_valid == ATTR_SIZE)
298 attr->ia_valid &= ~ATTR_SIZE;
301 /* Check for setting the inode time. */
302 ia_valid = attr->ia_valid;
303 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
304 exfat_allow_set_time(idmap, sbi, inode)) {
305 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
309 error = setattr_prepare(idmap, dentry, attr);
310 attr->ia_valid = ia_valid;
314 if (((attr->ia_valid & ATTR_UID) &&
315 (!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid),
316 sbi->options.fs_uid))) ||
317 ((attr->ia_valid & ATTR_GID) &&
318 (!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid),
319 sbi->options.fs_gid))) ||
320 ((attr->ia_valid & ATTR_MODE) &&
321 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
327 * We don't return -EPERM here. Yes, strange, but this is too
330 if (attr->ia_valid & ATTR_MODE) {
331 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
332 attr->ia_valid &= ~ATTR_MODE;
335 if (attr->ia_valid & ATTR_SIZE)
336 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
338 setattr_copy(idmap, inode, attr);
339 exfat_truncate_inode_atime(inode);
341 if (attr->ia_valid & ATTR_SIZE) {
342 error = exfat_block_truncate_page(inode, attr->ia_size);
346 down_write(&EXFAT_I(inode)->truncate_lock);
347 truncate_setsize(inode, attr->ia_size);
350 * __exfat_write_inode() is called from exfat_truncate(), inode
351 * is already written by it, so mark_inode_dirty() is unneeded.
353 exfat_truncate(inode);
354 up_write(&EXFAT_I(inode)->truncate_lock);
356 mark_inode_dirty(inode);
363 * modified ioctls from fat/file.c by Welmer Almesberger
365 static int exfat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
369 inode_lock_shared(inode);
370 attr = exfat_make_attr(inode);
371 inode_unlock_shared(inode);
373 return put_user(attr, user_attr);
376 static int exfat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
378 struct inode *inode = file_inode(file);
379 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
380 int is_dir = S_ISDIR(inode->i_mode);
385 err = get_user(attr, user_attr);
389 err = mnt_want_write_file(file);
394 oldattr = exfat_make_attr(inode);
397 * Mask attributes so we don't set reserved fields.
399 attr &= (EXFAT_ATTR_READONLY | EXFAT_ATTR_HIDDEN | EXFAT_ATTR_SYSTEM |
401 attr |= (is_dir ? EXFAT_ATTR_SUBDIR : 0);
403 /* Equivalent to a chmod() */
404 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
405 ia.ia_ctime = current_time(inode);
407 ia.ia_mode = exfat_make_mode(sbi, attr, 0777);
409 ia.ia_mode = exfat_make_mode(sbi, attr, 0666 | (inode->i_mode & 0111));
411 /* The root directory has no attributes */
412 if (inode->i_ino == EXFAT_ROOT_INO && attr != EXFAT_ATTR_SUBDIR) {
414 goto out_unlock_inode;
417 if (((attr | oldattr) & EXFAT_ATTR_SYSTEM) &&
418 !capable(CAP_LINUX_IMMUTABLE)) {
420 goto out_unlock_inode;
424 * The security check is questionable... We single
425 * out the RO attribute for checking by the security
426 * module, just because it maps to a file mode.
428 err = security_inode_setattr(file_mnt_idmap(file),
429 file->f_path.dentry, &ia);
431 goto out_unlock_inode;
433 /* This MUST be done before doing anything irreversible... */
434 err = exfat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia);
436 goto out_unlock_inode;
438 fsnotify_change(file->f_path.dentry, ia.ia_valid);
440 exfat_save_attr(inode, attr);
441 mark_inode_dirty(inode);
444 mnt_drop_write_file(file);
449 static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
451 struct fstrim_range range;
454 if (!capable(CAP_SYS_ADMIN))
457 if (!bdev_max_discard_sectors(inode->i_sb->s_bdev))
460 if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
463 range.minlen = max_t(unsigned int, range.minlen,
464 bdev_discard_granularity(inode->i_sb->s_bdev));
466 ret = exfat_trim_fs(inode, &range);
470 if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
476 static int exfat_ioctl_shutdown(struct super_block *sb, unsigned long arg)
480 if (!capable(CAP_SYS_ADMIN))
483 if (get_user(flags, (__u32 __user *)arg))
486 return exfat_force_shutdown(sb, flags);
489 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
491 struct inode *inode = file_inode(filp);
492 u32 __user *user_attr = (u32 __user *)arg;
495 case FAT_IOCTL_GET_ATTRIBUTES:
496 return exfat_ioctl_get_attributes(inode, user_attr);
497 case FAT_IOCTL_SET_ATTRIBUTES:
498 return exfat_ioctl_set_attributes(filp, user_attr);
499 case EXFAT_IOC_SHUTDOWN:
500 return exfat_ioctl_shutdown(inode->i_sb, arg);
502 return exfat_ioctl_fitrim(inode, arg);
509 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
512 return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
516 int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
518 struct inode *inode = filp->f_mapping->host;
521 if (unlikely(exfat_forced_shutdown(inode->i_sb)))
524 err = __generic_file_fsync(filp, start, end, datasync);
528 err = sync_blockdev(inode->i_sb->s_bdev);
532 return blkdev_issue_flush(inode->i_sb->s_bdev);
535 static int exfat_extend_valid_size(struct file *file, loff_t new_valid_size)
539 struct inode *inode = file_inode(file);
540 struct exfat_inode_info *ei = EXFAT_I(inode);
541 struct address_space *mapping = inode->i_mapping;
542 const struct address_space_operations *ops = mapping->a_ops;
544 pos = ei->valid_size;
545 while (pos < new_valid_size) {
549 len = PAGE_SIZE - (pos & (PAGE_SIZE - 1));
550 if (pos + len > new_valid_size)
551 len = new_valid_size - pos;
553 err = ops->write_begin(file, mapping, pos, len, &folio, NULL);
557 err = ops->write_end(file, mapping, pos, len, len, folio, NULL);
562 balance_dirty_pages_ratelimited(mapping);
570 static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
573 struct file *file = iocb->ki_filp;
574 struct inode *inode = file_inode(file);
575 struct exfat_inode_info *ei = EXFAT_I(inode);
576 loff_t pos = iocb->ki_pos;
581 valid_size = ei->valid_size;
583 ret = generic_write_checks(iocb, iter);
587 if (pos > valid_size) {
588 ret = exfat_extend_valid_size(file, pos);
589 if (ret < 0 && ret != -ENOSPC) {
590 exfat_err(inode->i_sb,
591 "write: fail to zero from %llu to %llu(%zd)",
592 valid_size, pos, ret);
598 ret = __generic_file_write_iter(iocb, iter);
604 if (pos > valid_size)
607 if (iocb_is_dsync(iocb) && iocb->ki_pos > pos) {
608 ssize_t err = vfs_fsync_range(file, pos, iocb->ki_pos - 1,
609 iocb->ki_flags & IOCB_SYNC);
622 static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf)
625 struct vm_area_struct *vma = vmf->vma;
626 struct file *file = vma->vm_file;
627 struct inode *inode = file_inode(file);
628 struct exfat_inode_info *ei = EXFAT_I(inode);
631 if (!inode_trylock(inode))
632 return VM_FAULT_RETRY;
634 start = ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
635 end = min_t(loff_t, i_size_read(inode),
636 start + vma->vm_end - vma->vm_start);
638 if (ei->valid_size < end) {
639 err = exfat_extend_valid_size(file, end);
642 return vmf_fs_error(err);
648 return filemap_page_mkwrite(vmf);
651 static const struct vm_operations_struct exfat_file_vm_ops = {
652 .fault = filemap_fault,
653 .map_pages = filemap_map_pages,
654 .page_mkwrite = exfat_page_mkwrite,
657 static int exfat_file_mmap(struct file *file, struct vm_area_struct *vma)
660 vma->vm_ops = &exfat_file_vm_ops;
664 const struct file_operations exfat_file_operations = {
665 .llseek = generic_file_llseek,
666 .read_iter = generic_file_read_iter,
667 .write_iter = exfat_file_write_iter,
668 .unlocked_ioctl = exfat_ioctl,
670 .compat_ioctl = exfat_compat_ioctl,
672 .mmap = exfat_file_mmap,
673 .fsync = exfat_file_fsync,
674 .splice_read = filemap_splice_read,
675 .splice_write = iter_file_splice_write,
678 const struct inode_operations exfat_file_inode_operations = {
679 .setattr = exfat_setattr,
680 .getattr = exfat_getattr,