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 exfat_sb_info *sbi, struct inode *inode)
94 mode_t allow_utime = sbi->options.allow_utime;
96 if (!uid_eq(current_fsuid(), inode->i_uid)) {
97 if (in_group_p(inode->i_gid))
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_ROUND_UP(ei->i_size_ondisk, 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);
246 unsigned int blocksize = i_blocksize(inode);
250 mutex_lock(&sbi->s_lock);
251 if (ei->start_clu == 0) {
253 * Empty start_clu != ~0 (not allocated)
255 exfat_fs_error(sb, "tried to truncate zeroed cluster.");
259 err = __exfat_truncate(inode);
263 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
265 aligned_size = i_size_read(inode);
266 if (aligned_size & (blocksize - 1)) {
267 aligned_size |= (blocksize - 1);
271 if (ei->i_size_ondisk > i_size_read(inode))
272 ei->i_size_ondisk = aligned_size;
274 if (ei->i_size_aligned > i_size_read(inode))
275 ei->i_size_aligned = aligned_size;
276 mutex_unlock(&sbi->s_lock);
279 int exfat_getattr(struct mnt_idmap *idmap, const struct path *path,
280 struct kstat *stat, unsigned int request_mask,
281 unsigned int query_flags)
283 struct inode *inode = d_backing_inode(path->dentry);
284 struct exfat_inode_info *ei = EXFAT_I(inode);
286 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
287 exfat_truncate_atime(&stat->atime);
288 stat->result_mask |= STATX_BTIME;
289 stat->btime.tv_sec = ei->i_crtime.tv_sec;
290 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
291 stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
295 int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
298 struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
299 struct inode *inode = dentry->d_inode;
300 unsigned int ia_valid;
303 if ((attr->ia_valid & ATTR_SIZE) &&
304 attr->ia_size > i_size_read(inode)) {
305 error = exfat_cont_expand(inode, attr->ia_size);
306 if (error || attr->ia_valid == ATTR_SIZE)
308 attr->ia_valid &= ~ATTR_SIZE;
311 /* Check for setting the inode time. */
312 ia_valid = attr->ia_valid;
313 if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
314 exfat_allow_set_time(sbi, inode)) {
315 attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
319 error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
320 attr->ia_valid = ia_valid;
324 if (((attr->ia_valid & ATTR_UID) &&
325 !uid_eq(attr->ia_uid, sbi->options.fs_uid)) ||
326 ((attr->ia_valid & ATTR_GID) &&
327 !gid_eq(attr->ia_gid, sbi->options.fs_gid)) ||
328 ((attr->ia_valid & ATTR_MODE) &&
329 (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
335 * We don't return -EPERM here. Yes, strange, but this is too
338 if (attr->ia_valid & ATTR_MODE) {
339 if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
340 attr->ia_valid &= ~ATTR_MODE;
343 if (attr->ia_valid & ATTR_SIZE)
344 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
346 setattr_copy(&nop_mnt_idmap, inode, attr);
347 exfat_truncate_inode_atime(inode);
349 if (attr->ia_valid & ATTR_SIZE) {
350 error = exfat_block_truncate_page(inode, attr->ia_size);
354 down_write(&EXFAT_I(inode)->truncate_lock);
355 truncate_setsize(inode, attr->ia_size);
358 * __exfat_write_inode() is called from exfat_truncate(), inode
359 * is already written by it, so mark_inode_dirty() is unneeded.
361 exfat_truncate(inode);
362 up_write(&EXFAT_I(inode)->truncate_lock);
364 mark_inode_dirty(inode);
371 * modified ioctls from fat/file.c by Welmer Almesberger
373 static int exfat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
377 inode_lock_shared(inode);
378 attr = exfat_make_attr(inode);
379 inode_unlock_shared(inode);
381 return put_user(attr, user_attr);
384 static int exfat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
386 struct inode *inode = file_inode(file);
387 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
388 int is_dir = S_ISDIR(inode->i_mode);
393 err = get_user(attr, user_attr);
397 err = mnt_want_write_file(file);
402 oldattr = exfat_make_attr(inode);
405 * Mask attributes so we don't set reserved fields.
407 attr &= (EXFAT_ATTR_READONLY | EXFAT_ATTR_HIDDEN | EXFAT_ATTR_SYSTEM |
409 attr |= (is_dir ? EXFAT_ATTR_SUBDIR : 0);
411 /* Equivalent to a chmod() */
412 ia.ia_valid = ATTR_MODE | ATTR_CTIME;
413 ia.ia_ctime = current_time(inode);
415 ia.ia_mode = exfat_make_mode(sbi, attr, 0777);
417 ia.ia_mode = exfat_make_mode(sbi, attr, 0666 | (inode->i_mode & 0111));
419 /* The root directory has no attributes */
420 if (inode->i_ino == EXFAT_ROOT_INO && attr != EXFAT_ATTR_SUBDIR) {
422 goto out_unlock_inode;
425 if (((attr | oldattr) & EXFAT_ATTR_SYSTEM) &&
426 !capable(CAP_LINUX_IMMUTABLE)) {
428 goto out_unlock_inode;
432 * The security check is questionable... We single
433 * out the RO attribute for checking by the security
434 * module, just because it maps to a file mode.
436 err = security_inode_setattr(file_mnt_idmap(file),
437 file->f_path.dentry, &ia);
439 goto out_unlock_inode;
441 /* This MUST be done before doing anything irreversible... */
442 err = exfat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia);
444 goto out_unlock_inode;
446 fsnotify_change(file->f_path.dentry, ia.ia_valid);
448 exfat_save_attr(inode, attr);
449 mark_inode_dirty(inode);
452 mnt_drop_write_file(file);
457 static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
459 struct fstrim_range range;
462 if (!capable(CAP_SYS_ADMIN))
465 if (!bdev_max_discard_sectors(inode->i_sb->s_bdev))
468 if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
471 range.minlen = max_t(unsigned int, range.minlen,
472 bdev_discard_granularity(inode->i_sb->s_bdev));
474 ret = exfat_trim_fs(inode, &range);
478 if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
484 long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
486 struct inode *inode = file_inode(filp);
487 u32 __user *user_attr = (u32 __user *)arg;
490 case FAT_IOCTL_GET_ATTRIBUTES:
491 return exfat_ioctl_get_attributes(inode, user_attr);
492 case FAT_IOCTL_SET_ATTRIBUTES:
493 return exfat_ioctl_set_attributes(filp, user_attr);
495 return exfat_ioctl_fitrim(inode, arg);
502 long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
505 return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
509 int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
511 struct inode *inode = filp->f_mapping->host;
514 err = __generic_file_fsync(filp, start, end, datasync);
518 err = sync_blockdev(inode->i_sb->s_bdev);
522 return blkdev_issue_flush(inode->i_sb->s_bdev);
525 static int exfat_file_zeroed_range(struct file *file, loff_t start, loff_t end)
528 struct inode *inode = file_inode(file);
529 struct address_space *mapping = inode->i_mapping;
530 const struct address_space_operations *ops = mapping->a_ops;
532 while (start < end) {
534 struct page *page = NULL;
536 zerofrom = start & (PAGE_SIZE - 1);
537 len = PAGE_SIZE - zerofrom;
538 if (start + len > end)
541 err = ops->write_begin(file, mapping, start, len, &page, NULL);
545 zero_user_segment(page, zerofrom, zerofrom + len);
547 err = ops->write_end(file, mapping, start, len, len, page, NULL);
552 balance_dirty_pages_ratelimited(mapping);
560 static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter)
563 struct file *file = iocb->ki_filp;
564 struct inode *inode = file_inode(file);
565 struct exfat_inode_info *ei = EXFAT_I(inode);
566 loff_t pos = iocb->ki_pos;
571 valid_size = ei->valid_size;
573 ret = generic_write_checks(iocb, iter);
577 if (pos > valid_size) {
578 ret = exfat_file_zeroed_range(file, valid_size, pos);
579 if (ret < 0 && ret != -ENOSPC) {
580 exfat_err(inode->i_sb,
581 "write: fail to zero from %llu to %llu(%zd)",
582 valid_size, pos, ret);
588 ret = __generic_file_write_iter(iocb, iter);
594 if (pos > valid_size)
597 if (iocb_is_dsync(iocb) && iocb->ki_pos > pos) {
598 ssize_t err = vfs_fsync_range(file, pos, iocb->ki_pos - 1,
599 iocb->ki_flags & IOCB_SYNC);
612 static int exfat_file_mmap(struct file *file, struct vm_area_struct *vma)
615 struct inode *inode = file_inode(file);
616 struct exfat_inode_info *ei = EXFAT_I(inode);
617 loff_t start = ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
618 loff_t end = min_t(loff_t, i_size_read(inode),
619 start + vma->vm_end - vma->vm_start);
621 if ((vma->vm_flags & VM_WRITE) && ei->valid_size < end) {
622 ret = exfat_file_zeroed_range(file, ei->valid_size, end);
624 exfat_err(inode->i_sb,
625 "mmap: fail to zero from %llu to %llu(%d)",
631 return generic_file_mmap(file, vma);
634 const struct file_operations exfat_file_operations = {
635 .llseek = generic_file_llseek,
636 .read_iter = generic_file_read_iter,
637 .write_iter = exfat_file_write_iter,
638 .unlocked_ioctl = exfat_ioctl,
640 .compat_ioctl = exfat_compat_ioctl,
642 .mmap = exfat_file_mmap,
643 .fsync = exfat_file_fsync,
644 .splice_read = filemap_splice_read,
645 .splice_write = iter_file_splice_write,
648 const struct inode_operations exfat_file_inode_operations = {
649 .setattr = exfat_setattr,
650 .getattr = exfat_getattr,