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_ROUND_UP(ei->i_size_ondisk, 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 ei->i_size_aligned = round_up(size, sb->s_blocksize);
78 ei->i_size_ondisk = ei->i_size_aligned;
79 inode->i_blocks = round_up(size, sbi->cluster_size) >> 9;
80 mark_inode_dirty(inode);
83 return write_inode_now(inode, 1);
88 exfat_free_cluster(inode, &clu);
92 static bool exfat_allow_set_time(struct mnt_idmap *idmap,
93 struct exfat_sb_info *sbi, struct inode *inode)
95 mode_t allow_utime = sbi->options.allow_utime;
97 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode),
99 if (vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode)))
101 if (allow_utime & MAY_WRITE)
105 /* use a default check */
109 static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
110 struct inode *inode, umode_t *mode_ptr)
112 mode_t i_mode, mask, perm;
114 i_mode = inode->i_mode;
116 mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ?
117 sbi->options.fs_fmask : sbi->options.fs_dmask;
118 perm = *mode_ptr & ~(S_IFMT | mask);
120 /* Of the r and x bits, all (subject to umask) must be present.*/
121 if ((perm & 0555) != (i_mode & 0555))
124 if (exfat_mode_can_hold_ro(inode)) {
126 * Of the w bits, either all (subject to umask) or none must
129 if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
133 * If exfat_mode_can_hold_ro(inode) is false, can't change
136 if ((perm & 0222) != (0222 & ~mask))
140 *mode_ptr &= S_IFMT | perm;
145 /* resize the file length */
146 int __exfat_truncate(struct inode *inode)
148 unsigned int num_clusters_new, num_clusters_phys;
149 unsigned int last_clu = EXFAT_FREE_CLUSTER;
150 struct exfat_chain clu;
151 struct super_block *sb = inode->i_sb;
152 struct exfat_sb_info *sbi = EXFAT_SB(sb);
153 struct exfat_inode_info *ei = EXFAT_I(inode);
155 /* check if the given file ID is opened */
156 if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
159 exfat_set_volume_dirty(sb);
161 num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
162 num_clusters_phys = EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
164 exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
166 if (i_size_read(inode) > 0) {
168 * Truncate FAT chain num_clusters after the first cluster
169 * num_clusters = min(new, phys);
171 unsigned int num_clusters =
172 min(num_clusters_new, num_clusters_phys);
176 * (defensive coding - works fine even with corrupted FAT table
178 if (clu.flags == ALLOC_NO_FAT_CHAIN) {
179 clu.dir += num_clusters;
180 clu.size -= num_clusters;
182 while (num_clusters > 0) {
184 if (exfat_get_next_cluster(sb, &(clu.dir)))
192 ei->flags = ALLOC_NO_FAT_CHAIN;
193 ei->start_clu = EXFAT_EOF_CLUSTER;
196 if (i_size_read(inode) < ei->valid_size)
197 ei->valid_size = i_size_read(inode);
199 if (ei->type == TYPE_FILE)
200 ei->attr |= EXFAT_ATTR_ARCHIVE;
203 * update the directory entry
205 * If the directory entry is updated by mark_inode_dirty(), the
206 * directory entry will be written after a writeback cycle of
207 * updating the bitmap/FAT, which may result in clusters being
208 * freed but referenced by the directory entry in the event of a
209 * sudden power failure.
210 * __exfat_write_inode() is called for directory entry, bitmap
211 * and FAT to be written in a same writeback.
213 if (__exfat_write_inode(inode, inode_needs_sync(inode)))
216 /* cut off from the FAT chain */
217 if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
218 last_clu != EXFAT_EOF_CLUSTER) {
219 if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
223 /* invalidate cache and free the clusters */
224 /* clear exfat cache */
225 exfat_cache_inval_inode(inode);
227 /* hint information */
228 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
229 ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
231 /* hint_stat will be used if this is directory. */
232 ei->hint_stat.eidx = 0;
233 ei->hint_stat.clu = ei->start_clu;
234 ei->hint_femp.eidx = EXFAT_HINT_NONE;
236 /* free the clusters */
237 if (exfat_free_cluster(inode, &clu))
243 void exfat_truncate(struct inode *inode)
245 struct super_block *sb = inode->i_sb;
246 struct exfat_sb_info *sbi = EXFAT_SB(sb);
247 struct exfat_inode_info *ei = EXFAT_I(inode);
248 unsigned int blocksize = i_blocksize(inode);
252 mutex_lock(&sbi->s_lock);
253 if (ei->start_clu == 0) {
255 * Empty start_clu != ~0 (not allocated)
257 exfat_fs_error(sb, "tried to truncate zeroed cluster.");
261 err = __exfat_truncate(inode);
265 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
267 aligned_size = i_size_read(inode);
268 if (aligned_size & (blocksize - 1)) {
269 aligned_size |= (blocksize - 1);
273 if (ei->i_size_ondisk > i_size_read(inode))
274 ei->i_size_ondisk = aligned_size;
276 if (ei->i_size_aligned > i_size_read(inode))
277 ei->i_size_aligned = aligned_size;
278 mutex_unlock(&sbi->s_lock);
281 int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,
282 struct kstat *stat, unsigned int request_mask,
283 unsigned int query_flags)
285 struct inode *inode = d_backing_inode(path->dentry);
286 struct exfat_inode_info *ei = EXFAT_I(inode);
288 generic_fillattr(idmap, request_mask, inode, stat);
289 exfat_truncate_atime(&stat->atime);
290 stat->result_mask |= STATX_BTIME;
291 stat->btime.tv_sec = ei->i_crtime.tv_sec;
292 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
293 stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
297 int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
300 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
301 struct inode *inode = dentry->d_inode;
302 unsigned int ia_valid;
305 if ((attr->ia_valid & ATTR_SIZE) &&
306 attr->ia_size > i_size_read(inode)) {
307 error = exfat_cont_expand(inode, attr->ia_size);
308 if (error || attr->ia_valid == ATTR_SIZE)
310 attr->ia_valid &= ~ATTR_SIZE;
313 /* Check for setting the inode time. */
314 ia_valid = attr->ia_valid;
315 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
316 exfat_allow_set_time(idmap, sbi, inode)) {
317 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
321 error = setattr_prepare(idmap, dentry, attr);
322 attr->ia_valid = ia_valid;
326 if (((attr->ia_valid & ATTR_UID) &&
327 (!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid),
328 sbi->options.fs_uid))) ||
329 ((attr->ia_valid & ATTR_GID) &&
330 (!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid),
331 sbi->options.fs_gid))) ||
332 ((attr->ia_valid & ATTR_MODE) &&
333 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
339 * We don't return -EPERM here. Yes, strange, but this is too
342 if (attr->ia_valid & ATTR_MODE) {
343 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
344 attr->ia_valid &= ~ATTR_MODE;
347 if (attr->ia_valid & ATTR_SIZE)
348 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
350 setattr_copy(idmap, inode, attr);
351 exfat_truncate_inode_atime(inode);
353 if (attr->ia_valid & ATTR_SIZE) {
354 error = exfat_block_truncate_page(inode, attr->ia_size);
358 down_write(&EXFAT_I(inode)->truncate_lock);
359 truncate_setsize(inode, attr->ia_size);
362 * __exfat_write_inode() is called from exfat_truncate(), inode
363 * is already written by it, so mark_inode_dirty() is unneeded.
365 exfat_truncate(inode);
366 up_write(&EXFAT_I(inode)->truncate_lock);
368 mark_inode_dirty(inode);
375 * modified ioctls from fat/file.c by Welmer Almesberger
377 static int exfat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
381 inode_lock_shared(inode);
382 attr = exfat_make_attr(inode);
383 inode_unlock_shared(inode);
385 return put_user(attr, user_attr);
388 static int exfat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
390 struct inode *inode = file_inode(file);
391 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
392 int is_dir = S_ISDIR(inode->i_mode);
397 err = get_user(attr, user_attr);
401 err = mnt_want_write_file(file);
406 oldattr = exfat_make_attr(inode);
409 * Mask attributes so we don't set reserved fields.
411 attr &= (EXFAT_ATTR_READONLY | EXFAT_ATTR_HIDDEN | EXFAT_ATTR_SYSTEM |
413 attr |= (is_dir ? EXFAT_ATTR_SUBDIR : 0);
415 /* Equivalent to a chmod() */
416 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
417 ia.ia_ctime = current_time(inode);
419 ia.ia_mode = exfat_make_mode(sbi, attr, 0777);
421 ia.ia_mode = exfat_make_mode(sbi, attr, 0666 | (inode->i_mode & 0111));
423 /* The root directory has no attributes */
424 if (inode->i_ino == EXFAT_ROOT_INO && attr != EXFAT_ATTR_SUBDIR) {
426 goto out_unlock_inode;
429 if (((attr | oldattr) & EXFAT_ATTR_SYSTEM) &&
430 !capable(CAP_LINUX_IMMUTABLE)) {
432 goto out_unlock_inode;
436 * The security check is questionable... We single
437 * out the RO attribute for checking by the security
438 * module, just because it maps to a file mode.
440 err = security_inode_setattr(file_mnt_idmap(file),
441 file->f_path.dentry, &ia);
443 goto out_unlock_inode;
445 /* This MUST be done before doing anything irreversible... */
446 err = exfat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia);
448 goto out_unlock_inode;
450 fsnotify_change(file->f_path.dentry, ia.ia_valid);
452 exfat_save_attr(inode, attr);
453 mark_inode_dirty(inode);
456 mnt_drop_write_file(file);
461 static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
463 struct fstrim_range range;
466 if (!capable(CAP_SYS_ADMIN))
469 if (!bdev_max_discard_sectors(inode->i_sb->s_bdev))
472 if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
475 range.minlen = max_t(unsigned int, range.minlen,
476 bdev_discard_granularity(inode->i_sb->s_bdev));
478 ret = exfat_trim_fs(inode, &range);
482 if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
488 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
490 struct inode *inode = file_inode(filp);
491 u32 __user *user_attr = (u32 __user *)arg;
494 case FAT_IOCTL_GET_ATTRIBUTES:
495 return exfat_ioctl_get_attributes(inode, user_attr);
496 case FAT_IOCTL_SET_ATTRIBUTES:
497 return exfat_ioctl_set_attributes(filp, user_attr);
499 return exfat_ioctl_fitrim(inode, arg);
506 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
509 return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
513 int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
515 struct inode *inode = filp->f_mapping->host;
518 err = __generic_file_fsync(filp, start, end, datasync);
522 err = sync_blockdev(inode->i_sb->s_bdev);
526 return blkdev_issue_flush(inode->i_sb->s_bdev);
529 static int exfat_file_zeroed_range(struct file *file, loff_t start, loff_t end)
532 struct inode *inode = file_inode(file);
533 struct address_space *mapping = inode->i_mapping;
534 const struct address_space_operations *ops = mapping->a_ops;
536 while (start < end) {
538 struct page *page = NULL;
540 zerofrom = start & (PAGE_SIZE - 1);
541 len = PAGE_SIZE - zerofrom;
542 if (start + len > end)
545 err = ops->write_begin(file, mapping, start, len, &page, NULL);
549 zero_user_segment(page, zerofrom, zerofrom + len);
551 err = ops->write_end(file, mapping, start, len, len, page, NULL);
556 balance_dirty_pages_ratelimited(mapping);
564 static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
567 struct file *file = iocb->ki_filp;
568 struct inode *inode = file_inode(file);
569 struct exfat_inode_info *ei = EXFAT_I(inode);
570 loff_t pos = iocb->ki_pos;
575 valid_size = ei->valid_size;
577 ret = generic_write_checks(iocb, iter);
581 if (pos > valid_size) {
582 ret = exfat_file_zeroed_range(file, valid_size, pos);
583 if (ret < 0 && ret != -ENOSPC) {
584 exfat_err(inode->i_sb,
585 "write: fail to zero from %llu to %llu(%zd)",
586 valid_size, pos, ret);
592 ret = __generic_file_write_iter(iocb, iter);
598 if (pos > valid_size)
601 if (iocb_is_dsync(iocb) && iocb->ki_pos > pos) {
602 ssize_t err = vfs_fsync_range(file, pos, iocb->ki_pos - 1,
603 iocb->ki_flags & IOCB_SYNC);
616 static int exfat_file_mmap(struct file *file, struct vm_area_struct *vma)
619 struct inode *inode = file_inode(file);
620 struct exfat_inode_info *ei = EXFAT_I(inode);
621 loff_t start = ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
622 loff_t end = min_t(loff_t, i_size_read(inode),
623 start + vma->vm_end - vma->vm_start);
625 if ((vma->vm_flags & VM_WRITE) && ei->valid_size < end) {
626 ret = exfat_file_zeroed_range(file, ei->valid_size, end);
628 exfat_err(inode->i_sb,
629 "mmap: fail to zero from %llu to %llu(%d)",
635 return generic_file_mmap(file, vma);
638 const struct file_operations exfat_file_operations = {
639 .llseek = generic_file_llseek,
640 .read_iter = generic_file_read_iter,
641 .write_iter = exfat_file_write_iter,
642 .unlocked_ioctl = exfat_ioctl,
644 .compat_ioctl = exfat_compat_ioctl,
646 .mmap = exfat_file_mmap,
647 .fsync = exfat_file_fsync,
648 .splice_read = filemap_splice_read,
649 .splice_write = iter_file_splice_write,
652 const struct inode_operations exfat_file_inode_operations = {
653 .setattr = exfat_setattr,
654 .getattr = exfat_getattr,