linux/fs/exfat/file.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
   4 */
   5
   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
  12#include "exfat_raw.h"
  13#include "exfat_fs.h"
  14
  15static int exfat_cont_expand(struct inode *inode, loff_t size)
  16{
  17        struct address_space *mapping = inode->i_mapping;
  18        loff_t start = i_size_read(inode), count = size - i_size_read(inode);
  19        int err, err2;
  20
  21        err = generic_cont_expand_simple(inode, size);
  22        if (err)
  23                return err;
  24
  25        inode->i_ctime = inode->i_mtime = current_time(inode);
  26        mark_inode_dirty(inode);
  27
  28        if (!IS_SYNC(inode))
  29                return 0;
  30
  31        err = filemap_fdatawrite_range(mapping, start, start + count - 1);
  32        err2 = sync_mapping_buffers(mapping);
  33        if (!err)
  34                err = err2;
  35        err2 = write_inode_now(inode, 1);
  36        if (!err)
  37                err = err2;
  38        if (err)
  39                return err;
  40
  41        return filemap_fdatawait_range(mapping, start, start + count - 1);
  42}
  43
  44static bool exfat_allow_set_time(struct exfat_sb_info *sbi, struct inode *inode)
  45{
  46        mode_t allow_utime = sbi->options.allow_utime;
  47
  48        if (!uid_eq(current_fsuid(), inode->i_uid)) {
  49                if (in_group_p(inode->i_gid))
  50                        allow_utime >>= 3;
  51                if (allow_utime & MAY_WRITE)
  52                        return true;
  53        }
  54
  55        /* use a default check */
  56        return false;
  57}
  58
  59static int exfat_sanitize_mode(const struct exfat_sb_info *sbi,
  60                struct inode *inode, umode_t *mode_ptr)
  61{
  62        mode_t i_mode, mask, perm;
  63
  64        i_mode = inode->i_mode;
  65
  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);
  69
  70        /* Of the r and x bits, all (subject to umask) must be present.*/
  71        if ((perm & 0555) != (i_mode & 0555))
  72                return -EPERM;
  73
  74        if (exfat_mode_can_hold_ro(inode)) {
  75                /*
  76                 * Of the w bits, either all (subject to umask) or none must
  77                 * be present.
  78                 */
  79                if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask)))
  80                        return -EPERM;
  81        } else {
  82                /*
  83                 * If exfat_mode_can_hold_ro(inode) is false, can't change
  84                 * w bits.
  85                 */
  86                if ((perm & 0222) != (0222 & ~mask))
  87                        return -EPERM;
  88        }
  89
  90        *mode_ptr &= S_IFMT | perm;
  91
  92        return 0;
  93}
  94
  95/* resize the file length */
  96int __exfat_truncate(struct inode *inode, loff_t new_size)
  97{
  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);
 104        int evict = (ei->dir.dir == DIR_DELETED) ? 1 : 0;
 105
 106        /* check if the given file ID is opened */
 107        if (ei->type != TYPE_FILE && ei->type != TYPE_DIR)
 108                return -EPERM;
 109
 110        exfat_set_volume_dirty(sb);
 111
 112        num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi);
 113        num_clusters_phys =
 114                EXFAT_B_TO_CLU_ROUND_UP(EXFAT_I(inode)->i_size_ondisk, sbi);
 115
 116        exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags);
 117
 118        if (new_size > 0) {
 119                /*
 120                 * Truncate FAT chain num_clusters after the first cluster
 121                 * num_clusters = min(new, phys);
 122                 */
 123                unsigned int num_clusters =
 124                        min(num_clusters_new, num_clusters_phys);
 125
 126                /*
 127                 * Follow FAT chain
 128                 * (defensive coding - works fine even with corrupted FAT table
 129                 */
 130                if (clu.flags == ALLOC_NO_FAT_CHAIN) {
 131                        clu.dir += num_clusters;
 132                        clu.size -= num_clusters;
 133                } else {
 134                        while (num_clusters > 0) {
 135                                last_clu = clu.dir;
 136                                if (exfat_get_next_cluster(sb, &(clu.dir)))
 137                                        return -EIO;
 138
 139                                num_clusters--;
 140                                clu.size--;
 141                        }
 142                }
 143        } else {
 144                ei->flags = ALLOC_NO_FAT_CHAIN;
 145                ei->start_clu = EXFAT_EOF_CLUSTER;
 146        }
 147
 148        i_size_write(inode, new_size);
 149
 150        if (ei->type == TYPE_FILE)
 151                ei->attr |= ATTR_ARCHIVE;
 152
 153        /* update the directory entry */
 154        if (!evict) {
 155                struct timespec64 ts;
 156                struct exfat_dentry *ep, *ep2;
 157                struct exfat_entry_set_cache *es;
 158                int err;
 159
 160                es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry,
 161                                ES_ALL_ENTRIES);
 162                if (!es)
 163                        return -EIO;
 164                ep = exfat_get_dentry_cached(es, 0);
 165                ep2 = exfat_get_dentry_cached(es, 1);
 166
 167                ts = current_time(inode);
 168                exfat_set_entry_time(sbi, &ts,
 169                                &ep->dentry.file.modify_tz,
 170                                &ep->dentry.file.modify_time,
 171                                &ep->dentry.file.modify_date,
 172                                &ep->dentry.file.modify_time_cs);
 173                ep->dentry.file.attr = cpu_to_le16(ei->attr);
 174
 175                /* File size should be zero if there is no cluster allocated */
 176                if (ei->start_clu == EXFAT_EOF_CLUSTER) {
 177                        ep2->dentry.stream.valid_size = 0;
 178                        ep2->dentry.stream.size = 0;
 179                } else {
 180                        ep2->dentry.stream.valid_size = cpu_to_le64(new_size);
 181                        ep2->dentry.stream.size = ep2->dentry.stream.valid_size;
 182                }
 183
 184                if (new_size == 0) {
 185                        /* Any directory can not be truncated to zero */
 186                        WARN_ON(ei->type != TYPE_FILE);
 187
 188                        ep2->dentry.stream.flags = ALLOC_FAT_CHAIN;
 189                        ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER;
 190                }
 191
 192                exfat_update_dir_chksum_with_entry_set(es);
 193                err = exfat_free_dentry_set(es, inode_needs_sync(inode));
 194                if (err)
 195                        return err;
 196        }
 197
 198        /* cut off from the FAT chain */
 199        if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER &&
 200                        last_clu != EXFAT_EOF_CLUSTER) {
 201                if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER))
 202                        return -EIO;
 203        }
 204
 205        /* invalidate cache and free the clusters */
 206        /* clear exfat cache */
 207        exfat_cache_inval_inode(inode);
 208
 209        /* hint information */
 210        ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
 211        ei->hint_bmap.clu = EXFAT_EOF_CLUSTER;
 212
 213        /* hint_stat will be used if this is directory. */
 214        ei->hint_stat.eidx = 0;
 215        ei->hint_stat.clu = ei->start_clu;
 216        ei->hint_femp.eidx = EXFAT_HINT_NONE;
 217
 218        /* free the clusters */
 219        if (exfat_free_cluster(inode, &clu))
 220                return -EIO;
 221
 222        exfat_clear_volume_dirty(sb);
 223
 224        return 0;
 225}
 226
 227void exfat_truncate(struct inode *inode, loff_t size)
 228{
 229        struct super_block *sb = inode->i_sb;
 230        struct exfat_sb_info *sbi = EXFAT_SB(sb);
 231        unsigned int blocksize = i_blocksize(inode);
 232        loff_t aligned_size;
 233        int err;
 234
 235        mutex_lock(&sbi->s_lock);
 236        if (EXFAT_I(inode)->start_clu == 0) {
 237                /*
 238                 * Empty start_clu != ~0 (not allocated)
 239                 */
 240                exfat_fs_error(sb, "tried to truncate zeroed cluster.");
 241                goto write_size;
 242        }
 243
 244        err = __exfat_truncate(inode, i_size_read(inode));
 245        if (err)
 246                goto write_size;
 247
 248        inode->i_ctime = inode->i_mtime = current_time(inode);
 249        if (IS_DIRSYNC(inode))
 250                exfat_sync_inode(inode);
 251        else
 252                mark_inode_dirty(inode);
 253
 254        inode->i_blocks = ((i_size_read(inode) + (sbi->cluster_size - 1)) &
 255                        ~(sbi->cluster_size - 1)) >> inode->i_blkbits;
 256write_size:
 257        aligned_size = i_size_read(inode);
 258        if (aligned_size & (blocksize - 1)) {
 259                aligned_size |= (blocksize - 1);
 260                aligned_size++;
 261        }
 262
 263        if (EXFAT_I(inode)->i_size_ondisk > i_size_read(inode))
 264                EXFAT_I(inode)->i_size_ondisk = aligned_size;
 265
 266        if (EXFAT_I(inode)->i_size_aligned > i_size_read(inode))
 267                EXFAT_I(inode)->i_size_aligned = aligned_size;
 268        mutex_unlock(&sbi->s_lock);
 269}
 270
 271int exfat_getattr(struct user_namespace *mnt_uerns, const struct path *path,
 272                  struct kstat *stat, unsigned int request_mask,
 273                  unsigned int query_flags)
 274{
 275        struct inode *inode = d_backing_inode(path->dentry);
 276        struct exfat_inode_info *ei = EXFAT_I(inode);
 277
 278        generic_fillattr(&init_user_ns, inode, stat);
 279        exfat_truncate_atime(&stat->atime);
 280        stat->result_mask |= STATX_BTIME;
 281        stat->btime.tv_sec = ei->i_crtime.tv_sec;
 282        stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
 283        stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size;
 284        return 0;
 285}
 286
 287int exfat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
 288                  struct iattr *attr)
 289{
 290        struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb);
 291        struct inode *inode = dentry->d_inode;
 292        unsigned int ia_valid;
 293        int error;
 294
 295        if ((attr->ia_valid & ATTR_SIZE) &&
 296            attr->ia_size > i_size_read(inode)) {
 297                error = exfat_cont_expand(inode, attr->ia_size);
 298                if (error || attr->ia_valid == ATTR_SIZE)
 299                        return error;
 300                attr->ia_valid &= ~ATTR_SIZE;
 301        }
 302
 303        /* Check for setting the inode time. */
 304        ia_valid = attr->ia_valid;
 305        if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) &&
 306            exfat_allow_set_time(sbi, inode)) {
 307                attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET |
 308                                ATTR_TIMES_SET);
 309        }
 310
 311        error = setattr_prepare(&init_user_ns, dentry, attr);
 312        attr->ia_valid = ia_valid;
 313        if (error)
 314                goto out;
 315
 316        if (((attr->ia_valid & ATTR_UID) &&
 317             !uid_eq(attr->ia_uid, sbi->options.fs_uid)) ||
 318            ((attr->ia_valid & ATTR_GID) &&
 319             !gid_eq(attr->ia_gid, sbi->options.fs_gid)) ||
 320            ((attr->ia_valid & ATTR_MODE) &&
 321             (attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) {
 322                error = -EPERM;
 323                goto out;
 324        }
 325
 326        /*
 327         * We don't return -EPERM here. Yes, strange, but this is too
 328         * old behavior.
 329         */
 330        if (attr->ia_valid & ATTR_MODE) {
 331                if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
 332                        attr->ia_valid &= ~ATTR_MODE;
 333        }
 334
 335        if (attr->ia_valid & ATTR_SIZE) {
 336                error = exfat_block_truncate_page(inode, attr->ia_size);
 337                if (error)
 338                        goto out;
 339
 340                down_write(&EXFAT_I(inode)->truncate_lock);
 341                truncate_setsize(inode, attr->ia_size);
 342                exfat_truncate(inode, attr->ia_size);
 343                up_write(&EXFAT_I(inode)->truncate_lock);
 344        }
 345
 346        setattr_copy(&init_user_ns, inode, attr);
 347        exfat_truncate_atime(&inode->i_atime);
 348        mark_inode_dirty(inode);
 349
 350out:
 351        return error;
 352}
 353
 354static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
 355{
 356        struct request_queue *q = bdev_get_queue(inode->i_sb->s_bdev);
 357        struct fstrim_range range;
 358        int ret = 0;
 359
 360        if (!capable(CAP_SYS_ADMIN))
 361                return -EPERM;
 362
 363        if (!blk_queue_discard(q))
 364                return -EOPNOTSUPP;
 365
 366        if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range)))
 367                return -EFAULT;
 368
 369        range.minlen = max_t(unsigned int, range.minlen,
 370                                q->limits.discard_granularity);
 371
 372        ret = exfat_trim_fs(inode, &range);
 373        if (ret < 0)
 374                return ret;
 375
 376        if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range)))
 377                return -EFAULT;
 378
 379        return 0;
 380}
 381
 382long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 383{
 384        struct inode *inode = file_inode(filp);
 385
 386        switch (cmd) {
 387        case FITRIM:
 388                return exfat_ioctl_fitrim(inode, arg);
 389        default:
 390                return -ENOTTY;
 391        }
 392}
 393
 394#ifdef CONFIG_COMPAT
 395long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
 396                                unsigned long arg)
 397{
 398        return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
 399}
 400#endif
 401
 402int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
 403{
 404        struct inode *inode = filp->f_mapping->host;
 405        int err;
 406
 407        err = __generic_file_fsync(filp, start, end, datasync);
 408        if (err)
 409                return err;
 410
 411        err = sync_blockdev(inode->i_sb->s_bdev);
 412        if (err)
 413                return err;
 414
 415        return blkdev_issue_flush(inode->i_sb->s_bdev);
 416}
 417
 418const struct file_operations exfat_file_operations = {
 419        .llseek         = generic_file_llseek,
 420        .read_iter      = generic_file_read_iter,
 421        .write_iter     = generic_file_write_iter,
 422        .unlocked_ioctl = exfat_ioctl,
 423#ifdef CONFIG_COMPAT
 424        .compat_ioctl = exfat_compat_ioctl,
 425#endif
 426        .mmap           = generic_file_mmap,
 427        .fsync          = exfat_file_fsync,
 428        .splice_read    = generic_file_splice_read,
 429        .splice_write   = iter_file_splice_write,
 430};
 431
 432const struct inode_operations exfat_file_inode_operations = {
 433        .setattr     = exfat_setattr,
 434        .getattr     = exfat_getattr,
 435};
 436