2020-03-02 14:21:36 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
2021-03-04 08:20:35 +08:00
|
|
|
#include <linux/compat.h>
|
2020-03-02 14:21:36 +08:00
|
|
|
#include <linux/cred.h>
|
|
|
|
#include <linux/buffer_head.h>
|
2020-06-18 19:43:26 +08:00
|
|
|
#include <linux/blkdev.h>
|
2020-03-02 14:21:36 +08:00
|
|
|
|
|
|
|
#include "exfat_raw.h"
|
|
|
|
#include "exfat_fs.h"
|
|
|
|
|
|
|
|
static int exfat_cont_expand(struct inode *inode, loff_t size)
|
|
|
|
{
|
|
|
|
struct address_space *mapping = inode->i_mapping;
|
|
|
|
loff_t start = i_size_read(inode), count = size - i_size_read(inode);
|
|
|
|
int err, err2;
|
|
|
|
|
|
|
|
err = generic_cont_expand_simple(inode, size);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
inode->i_ctime = inode->i_mtime = current_time(inode);
|
|
|
|
mark_inode_dirty(inode);
|
|
|
|
|
|
|
|
if (!IS_SYNC(inode))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err = filemap_fdatawrite_range(mapping, start, start + count - 1);
|
|
|
|
err2 = sync_mapping_buffers(mapping);
|
|
|
|
if (!err)
|
|
|
|
err = err2;
|
|
|
|
err2 = write_inode_now(inode, 1);
|
|
|
|
if (!err)
|
|
|
|
err = err2;
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
return filemap_fdatawait_range(mapping, start, start + count - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
|
|
|
|
{
|
|
|
|
mode_t allow_utime = sbi->options.allow_utime;
|
|
|
|
|
|
|
|
if (!uid_eq(current_fsuid(), inode->i_uid)) {
|
|
|
|
if (in_group_p(inode->i_gid))
|
|
|
|
allow_utime >>= 3;
|
|
|
|
if (allow_utime & MAY_WRITE)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use a default check */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
|
|
|
|
struct inode *inode, umode_t *mode_ptr)
|
|
|
|
{
|
|
|
|
mode_t i_mode, mask, perm;
|
|
|
|
|
|
|
|
i_mode = inode->i_mode;
|
|
|
|
|
|
|
|
mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ?
|
|
|
|
sbi->options.fs_fmask : sbi->options.fs_dmask;
|
|
|
|
perm = *mode_ptr & ~(S_IFMT | mask);
|
|
|
|
|
|
|
|
/* Of the r and x bits, all (subject to umask) must be present.*/
|
|
|
|
if ((perm & 0555) != (i_mode & 0555))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (exfat_mode_can_hold_ro(inode)) {
|
|
|
|
/*
|
|
|
|
* Of the w bits, either all (subject to umask) or none must
|
|
|
|
* be present.
|
|
|
|
*/
|
|
|
|
if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
|
|
|
|
return -EPERM;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If exfat_mode_can_hold_ro(inode) is false, can't change
|
|
|
|
* w bits.
|
|
|
|
*/
|
|
|
|
if ((perm & 0222) != (0222 & ~mask))
|
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mode_ptr &= S_IFMT | perm;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* resize the file length */
|
2022-03-28 16:37:58 +08:00
|
|
|
int __exfat_truncate(struct inode *inode)
|
2020-03-02 14:21:36 +08:00
|
|
|
{
|
|
|
|
unsigned int num_clusters_new, num_clusters_phys;
|
|
|
|
unsigned int last_clu = EXFAT_FREE_CLUSTER;
|
|
|
|
struct exfat_chain clu;
|
|
|
|
struct super_block *sb = inode->i_sb;
|
|
|
|
struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
|
|
|
struct exfat_inode_info *ei = EXFAT_I(inode);
|
|
|
|
|
|
|
|
/* check if the given file ID is opened */
|
|
|
|
if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
|
|
|
|
return -EPERM;
|
|
|
|
|
2020-07-31 13:58:26 +08:00
|
|
|
exfat_set_volume_dirty(sb);
|
2020-03-02 14:21:36 +08:00
|
|
|
|
|
|
|
num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
|
2021-11-03 05:23:58 +08:00
|
|
|
num_clusters_phys = EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
|
2020-03-02 14:21:36 +08:00
|
|
|
|
|
|
|
exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
|
|
|
|
|
2022-03-28 16:37:58 +08:00
|
|
|
if (i_size_read(inode) > 0) {
|
2020-03-02 14:21:36 +08:00
|
|
|
/*
|
|
|
|
* Truncate FAT chain num_clusters after the first cluster
|
|
|
|
* num_clusters = min(new, phys);
|
|
|
|
*/
|
|
|
|
unsigned int num_clusters =
|
|
|
|
min(num_clusters_new, num_clusters_phys);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Follow FAT chain
|
|
|
|
* (defensive coding - works fine even with corrupted FAT table
|
|
|
|
*/
|
|
|
|
if (clu.flags == ALLOC_NO_FAT_CHAIN) {
|
|
|
|
clu.dir += num_clusters;
|
|
|
|
clu.size -= num_clusters;
|
|
|
|
} else {
|
|
|
|
while (num_clusters > 0) {
|
|
|
|
last_clu = clu.dir;
|
|
|
|
if (exfat_get_next_cluster(sb, &(clu.dir)))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
num_clusters--;
|
|
|
|
clu.size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ei->flags = ALLOC_NO_FAT_CHAIN;
|
|
|
|
ei->start_clu = EXFAT_EOF_CLUSTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ei->type == TYPE_FILE)
|
|
|
|
ei->attr |= ATTR_ARCHIVE;
|
|
|
|
|
2022-06-29 10:39:59 +08:00
|
|
|
/*
|
|
|
|
* update the directory entry
|
|
|
|
*
|
|
|
|
* If the directory entry is updated by mark_inode_dirty(), the
|
|
|
|
* directory entry will be written after a writeback cycle of
|
|
|
|
* updating the bitmap/FAT, which may result in clusters being
|
|
|
|
* freed but referenced by the directory entry in the event of a
|
|
|
|
* sudden power failure.
|
|
|
|
* __exfat_write_inode() is called for directory entry, bitmap
|
|
|
|
* and FAT to be written in a same writeback.
|
|
|
|
*/
|
2022-06-29 09:51:40 +08:00
|
|
|
if (__exfat_write_inode(inode, inode_needs_sync(inode)))
|
|
|
|
return -EIO;
|
2020-03-02 14:21:36 +08:00
|
|
|
|
|
|
|
/* cut off from the FAT chain */
|
|
|
|
if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
|
|
|
|
last_clu != EXFAT_EOF_CLUSTER) {
|
|
|
|
if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* invalidate cache and free the clusters */
|
|
|
|
/* clear exfat cache */
|
|
|
|
exfat_cache_inval_inode(inode);
|
|
|
|
|
|
|
|
/* hint information */
|
|
|
|
ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
|
|
|
|
ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
|
|
|
|
|
|
|
|
/* hint_stat will be used if this is directory. */
|
|
|
|
ei->hint_stat.eidx = 0;
|
|
|
|
ei->hint_stat.clu = ei->start_clu;
|
|
|
|
ei->hint_femp.eidx = EXFAT_HINT_NONE;
|
|
|
|
|
|
|
|
/* free the clusters */
|
|
|
|
if (exfat_free_cluster(inode, &clu))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-17 10:36:21 +08:00
|
|
|
void exfat_truncate(struct inode *inode)
|
2020-03-02 14:21:36 +08:00
|
|
|
{
|
|
|
|
struct super_block *sb = inode->i_sb;
|
|
|
|
struct exfat_sb_info *sbi = EXFAT_SB(sb);
|
2021-11-03 05:23:58 +08:00
|
|
|
struct exfat_inode_info *ei = EXFAT_I(inode);
|
2020-08-15 18:57:07 +08:00
|
|
|
unsigned int blocksize = i_blocksize(inode);
|
2020-03-02 14:21:36 +08:00
|
|
|
loff_t aligned_size;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
mutex_lock(&sbi->s_lock);
|
2021-11-03 05:23:58 +08:00
|
|
|
if (ei->start_clu == 0) {
|
2020-03-02 14:21:36 +08:00
|
|
|
/*
|
|
|
|
* Empty start_clu != ~0 (not allocated)
|
|
|
|
*/
|
|
|
|
exfat_fs_error(sb, "tried to truncate zeroed cluster.");
|
|
|
|
goto write_size;
|
|
|
|
}
|
|
|
|
|
2022-03-28 16:37:58 +08:00
|
|
|
err = __exfat_truncate(inode);
|
2020-03-02 14:21:36 +08:00
|
|
|
if (err)
|
|
|
|
goto write_size;
|
|
|
|
|
2021-11-22 21:02:37 +08:00
|
|
|
inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >>
|
|
|
|
inode->i_blkbits;
|
2020-03-02 14:21:36 +08:00
|
|
|
write_size:
|
|
|
|
aligned_size = i_size_read(inode);
|
|
|
|
if (aligned_size & (blocksize - 1)) {
|
|
|
|
aligned_size |= (blocksize - 1);
|
|
|
|
aligned_size++;
|
|
|
|
}
|
|
|
|
|
2021-11-03 05:23:58 +08:00
|
|
|
if (ei->i_size_ondisk > i_size_read(inode))
|
|
|
|
ei->i_size_ondisk = aligned_size;
|
2020-03-02 14:21:36 +08:00
|
|
|
|
2021-11-03 05:23:58 +08:00
|
|
|
if (ei->i_size_aligned > i_size_read(inode))
|
|
|
|
ei->i_size_aligned = aligned_size;
|
2020-03-02 14:21:36 +08:00
|
|
|
mutex_unlock(&sbi->s_lock);
|
|
|
|
}
|
|
|
|
|
2021-01-21 21:19:43 +08:00
|
|
|
int exfat_getattr(struct user_namespace *mnt_uerns, const struct path *path,
|
|
|
|
struct kstat *stat, unsigned int request_mask,
|
|
|
|
unsigned int query_flags)
|
2020-03-02 14:21:36 +08:00
|
|
|
{
|
|
|
|
struct inode *inode = d_backing_inode(path->dentry);
|
|
|
|
struct exfat_inode_info *ei = EXFAT_I(inode);
|
|
|
|
|
2021-01-21 21:19:30 +08:00
|
|
|
generic_fillattr(&init_user_ns, inode, stat);
|
2020-04-21 10:13:10 +08:00
|
|
|
exfat_truncate_atime(&stat->atime);
|
2020-03-02 14:21:36 +08:00
|
|
|
stat->result_mask |= STATX_BTIME;
|
|
|
|
stat->btime.tv_sec = ei->i_crtime.tv_sec;
|
|
|
|
stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
|
|
|
|
stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-21 21:19:43 +08:00
|
|
|
int exfat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
|
|
|
|
struct iattr *attr)
|
2020-03-02 14:21:36 +08:00
|
|
|
{
|
|
|
|
struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
|
|
|
|
struct inode *inode = dentry->d_inode;
|
|
|
|
unsigned int ia_valid;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if ((attr->ia_valid & ATTR_SIZE) &&
|
|
|
|
attr->ia_size > i_size_read(inode)) {
|
|
|
|
error = exfat_cont_expand(inode, attr->ia_size);
|
|
|
|
if (error || attr->ia_valid == ATTR_SIZE)
|
|
|
|
return error;
|
|
|
|
attr->ia_valid &= ~ATTR_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for setting the inode time. */
|
|
|
|
ia_valid = attr->ia_valid;
|
|
|
|
if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
|
|
|
|
exfat_allow_set_time(sbi, inode)) {
|
|
|
|
attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
|
|
|
|
ATTR_TIMES_SET);
|
|
|
|
}
|
|
|
|
|
2021-01-21 21:19:26 +08:00
|
|
|
error = setattr_prepare(&init_user_ns, dentry, attr);
|
2020-03-02 14:21:36 +08:00
|
|
|
attr->ia_valid = ia_valid;
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (((attr->ia_valid & ATTR_UID) &&
|
|
|
|
!uid_eq(attr->ia_uid, sbi->options.fs_uid)) ||
|
|
|
|
((attr->ia_valid & ATTR_GID) &&
|
|
|
|
!gid_eq(attr->ia_gid, sbi->options.fs_gid)) ||
|
|
|
|
((attr->ia_valid & ATTR_MODE) &&
|
|
|
|
(attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
|
|
|
|
error = -EPERM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't return -EPERM here. Yes, strange, but this is too
|
|
|
|
* old behavior.
|
|
|
|
*/
|
|
|
|
if (attr->ia_valid & ATTR_MODE) {
|
|
|
|
if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
|
|
|
|
attr->ia_valid &= ~ATTR_MODE;
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:39:59 +08:00
|
|
|
if (attr->ia_valid & ATTR_SIZE)
|
|
|
|
inode->i_mtime = inode->i_ctime = current_time(inode);
|
|
|
|
|
|
|
|
setattr_copy(&init_user_ns, inode, attr);
|
|
|
|
exfat_truncate_atime(&inode->i_atime);
|
|
|
|
|
2020-03-02 14:21:36 +08:00
|
|
|
if (attr->ia_valid & ATTR_SIZE) {
|
|
|
|
error = exfat_block_truncate_page(inode, attr->ia_size);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
down_write(&EXFAT_I(inode)->truncate_lock);
|
|
|
|
truncate_setsize(inode, attr->ia_size);
|
2022-06-29 10:39:59 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* __exfat_write_inode() is called from exfat_truncate(), inode
|
|
|
|
* is already written by it, so mark_inode_dirty() is unneeded.
|
|
|
|
*/
|
2022-11-17 10:36:21 +08:00
|
|
|
exfat_truncate(inode);
|
2020-03-02 14:21:36 +08:00
|
|
|
up_write(&EXFAT_I(inode)->truncate_lock);
|
2022-06-29 10:39:59 +08:00
|
|
|
} else
|
|
|
|
mark_inode_dirty(inode);
|
2020-03-02 14:21:36 +08:00
|
|
|
|
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:20:35 +08:00
|
|
|
static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct fstrim_range range;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2022-04-15 12:52:55 +08:00
|
|
|
if (!bdev_max_discard_sectors(inode->i_sb->s_bdev))
|
2021-03-04 08:20:35 +08:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
range.minlen = max_t(unsigned int, range.minlen,
|
2022-04-15 12:52:56 +08:00
|
|
|
bdev_discard_granularity(inode->i_sb->s_bdev));
|
2021-03-04 08:20:35 +08:00
|
|
|
|
|
|
|
ret = exfat_trim_fs(inode, &range);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct inode *inode = file_inode(filp);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FITRIM:
|
|
|
|
return exfat_ioctl_fitrim(inode, arg);
|
|
|
|
default:
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-18 19:43:26 +08:00
|
|
|
int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
|
|
|
|
{
|
|
|
|
struct inode *inode = filp->f_mapping->host;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = __generic_file_fsync(filp, start, end, datasync);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = sync_blockdev(inode->i_sb->s_bdev);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2021-01-26 22:52:35 +08:00
|
|
|
return blkdev_issue_flush(inode->i_sb->s_bdev);
|
2020-06-18 19:43:26 +08:00
|
|
|
}
|
|
|
|
|
2020-03-02 14:21:36 +08:00
|
|
|
const struct file_operations exfat_file_operations = {
|
2020-05-02 09:34:25 +08:00
|
|
|
.llseek = generic_file_llseek,
|
|
|
|
.read_iter = generic_file_read_iter,
|
|
|
|
.write_iter = generic_file_write_iter,
|
2021-03-04 08:20:35 +08:00
|
|
|
.unlocked_ioctl = exfat_ioctl,
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = exfat_compat_ioctl,
|
|
|
|
#endif
|
2020-05-02 09:34:25 +08:00
|
|
|
.mmap = generic_file_mmap,
|
2020-06-18 19:43:26 +08:00
|
|
|
.fsync = exfat_file_fsync,
|
2020-05-02 09:34:25 +08:00
|
|
|
.splice_read = generic_file_splice_read,
|
|
|
|
.splice_write = iter_file_splice_write,
|
2020-03-02 14:21:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct inode_operations exfat_file_inode_operations = {
|
|
|
|
.setattr = exfat_setattr,
|
|
|
|
.getattr = exfat_getattr,
|
|
|
|
};
|