Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/fs/fat/file.c
0004  *
0005  *  Written 1992,1993 by Werner Almesberger
0006  *
0007  *  regular file handling primitives for fat-based filesystems
0008  */
0009 
0010 #include <linux/capability.h>
0011 #include <linux/module.h>
0012 #include <linux/compat.h>
0013 #include <linux/mount.h>
0014 #include <linux/blkdev.h>
0015 #include <linux/backing-dev.h>
0016 #include <linux/fsnotify.h>
0017 #include <linux/security.h>
0018 #include <linux/falloc.h>
0019 #include "fat.h"
0020 
0021 static long fat_fallocate(struct file *file, int mode,
0022               loff_t offset, loff_t len);
0023 
0024 static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr)
0025 {
0026     u32 attr;
0027 
0028     inode_lock_shared(inode);
0029     attr = fat_make_attrs(inode);
0030     inode_unlock_shared(inode);
0031 
0032     return put_user(attr, user_attr);
0033 }
0034 
0035 static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)
0036 {
0037     struct inode *inode = file_inode(file);
0038     struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
0039     int is_dir = S_ISDIR(inode->i_mode);
0040     u32 attr, oldattr;
0041     struct iattr ia;
0042     int err;
0043 
0044     err = get_user(attr, user_attr);
0045     if (err)
0046         goto out;
0047 
0048     err = mnt_want_write_file(file);
0049     if (err)
0050         goto out;
0051     inode_lock(inode);
0052 
0053     /*
0054      * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
0055      * prevents the user from turning us into a VFAT
0056      * longname entry.  Also, we obviously can't set
0057      * any of the NTFS attributes in the high 24 bits.
0058      */
0059     attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR);
0060     /* Merge in ATTR_VOLUME and ATTR_DIR */
0061     attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) |
0062         (is_dir ? ATTR_DIR : 0);
0063     oldattr = fat_make_attrs(inode);
0064 
0065     /* Equivalent to a chmod() */
0066     ia.ia_valid = ATTR_MODE | ATTR_CTIME;
0067     ia.ia_ctime = current_time(inode);
0068     if (is_dir)
0069         ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO);
0070     else {
0071         ia.ia_mode = fat_make_mode(sbi, attr,
0072             S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO));
0073     }
0074 
0075     /* The root directory has no attributes */
0076     if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) {
0077         err = -EINVAL;
0078         goto out_unlock_inode;
0079     }
0080 
0081     if (sbi->options.sys_immutable &&
0082         ((attr | oldattr) & ATTR_SYS) &&
0083         !capable(CAP_LINUX_IMMUTABLE)) {
0084         err = -EPERM;
0085         goto out_unlock_inode;
0086     }
0087 
0088     /*
0089      * The security check is questionable...  We single
0090      * out the RO attribute for checking by the security
0091      * module, just because it maps to a file mode.
0092      */
0093     err = security_inode_setattr(file_mnt_user_ns(file),
0094                      file->f_path.dentry, &ia);
0095     if (err)
0096         goto out_unlock_inode;
0097 
0098     /* This MUST be done before doing anything irreversible... */
0099     err = fat_setattr(file_mnt_user_ns(file), file->f_path.dentry, &ia);
0100     if (err)
0101         goto out_unlock_inode;
0102 
0103     fsnotify_change(file->f_path.dentry, ia.ia_valid);
0104     if (sbi->options.sys_immutable) {
0105         if (attr & ATTR_SYS)
0106             inode->i_flags |= S_IMMUTABLE;
0107         else
0108             inode->i_flags &= ~S_IMMUTABLE;
0109     }
0110 
0111     fat_save_attrs(inode, attr);
0112     mark_inode_dirty(inode);
0113 out_unlock_inode:
0114     inode_unlock(inode);
0115     mnt_drop_write_file(file);
0116 out:
0117     return err;
0118 }
0119 
0120 static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
0121 {
0122     struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
0123     return put_user(sbi->vol_id, user_attr);
0124 }
0125 
0126 static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg)
0127 {
0128     struct super_block *sb = inode->i_sb;
0129     struct fstrim_range __user *user_range;
0130     struct fstrim_range range;
0131     int err;
0132 
0133     if (!capable(CAP_SYS_ADMIN))
0134         return -EPERM;
0135 
0136     if (!bdev_max_discard_sectors(sb->s_bdev))
0137         return -EOPNOTSUPP;
0138 
0139     user_range = (struct fstrim_range __user *)arg;
0140     if (copy_from_user(&range, user_range, sizeof(range)))
0141         return -EFAULT;
0142 
0143     range.minlen = max_t(unsigned int, range.minlen,
0144                  bdev_discard_granularity(sb->s_bdev));
0145 
0146     err = fat_trim_fs(inode, &range);
0147     if (err < 0)
0148         return err;
0149 
0150     if (copy_to_user(user_range, &range, sizeof(range)))
0151         return -EFAULT;
0152 
0153     return 0;
0154 }
0155 
0156 long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0157 {
0158     struct inode *inode = file_inode(filp);
0159     u32 __user *user_attr = (u32 __user *)arg;
0160 
0161     switch (cmd) {
0162     case FAT_IOCTL_GET_ATTRIBUTES:
0163         return fat_ioctl_get_attributes(inode, user_attr);
0164     case FAT_IOCTL_SET_ATTRIBUTES:
0165         return fat_ioctl_set_attributes(filp, user_attr);
0166     case FAT_IOCTL_GET_VOLUME_ID:
0167         return fat_ioctl_get_volume_id(inode, user_attr);
0168     case FITRIM:
0169         return fat_ioctl_fitrim(inode, arg);
0170     default:
0171         return -ENOTTY; /* Inappropriate ioctl for device */
0172     }
0173 }
0174 
0175 static int fat_file_release(struct inode *inode, struct file *filp)
0176 {
0177     if ((filp->f_mode & FMODE_WRITE) &&
0178         MSDOS_SB(inode->i_sb)->options.flush) {
0179         fat_flush_inodes(inode->i_sb, inode, NULL);
0180         set_current_state(TASK_UNINTERRUPTIBLE);
0181         io_schedule_timeout(HZ/10);
0182     }
0183     return 0;
0184 }
0185 
0186 int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
0187 {
0188     struct inode *inode = filp->f_mapping->host;
0189     int err;
0190 
0191     err = __generic_file_fsync(filp, start, end, datasync);
0192     if (err)
0193         return err;
0194 
0195     err = sync_mapping_buffers(MSDOS_SB(inode->i_sb)->fat_inode->i_mapping);
0196     if (err)
0197         return err;
0198 
0199     return blkdev_issue_flush(inode->i_sb->s_bdev);
0200 }
0201 
0202 
0203 const struct file_operations fat_file_operations = {
0204     .llseek     = generic_file_llseek,
0205     .read_iter  = generic_file_read_iter,
0206     .write_iter = generic_file_write_iter,
0207     .mmap       = generic_file_mmap,
0208     .release    = fat_file_release,
0209     .unlocked_ioctl = fat_generic_ioctl,
0210     .compat_ioctl   = compat_ptr_ioctl,
0211     .fsync      = fat_file_fsync,
0212     .splice_read    = generic_file_splice_read,
0213     .splice_write   = iter_file_splice_write,
0214     .fallocate  = fat_fallocate,
0215 };
0216 
0217 static int fat_cont_expand(struct inode *inode, loff_t size)
0218 {
0219     struct address_space *mapping = inode->i_mapping;
0220     loff_t start = inode->i_size, count = size - inode->i_size;
0221     int err;
0222 
0223     err = generic_cont_expand_simple(inode, size);
0224     if (err)
0225         goto out;
0226 
0227     fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
0228     mark_inode_dirty(inode);
0229     if (IS_SYNC(inode)) {
0230         int err2;
0231 
0232         /*
0233          * Opencode syncing since we don't have a file open to use
0234          * standard fsync path.
0235          */
0236         err = filemap_fdatawrite_range(mapping, start,
0237                            start + count - 1);
0238         err2 = sync_mapping_buffers(mapping);
0239         if (!err)
0240             err = err2;
0241         err2 = write_inode_now(inode, 1);
0242         if (!err)
0243             err = err2;
0244         if (!err) {
0245             err =  filemap_fdatawait_range(mapping, start,
0246                                start + count - 1);
0247         }
0248     }
0249 out:
0250     return err;
0251 }
0252 
0253 /*
0254  * Preallocate space for a file. This implements fat's fallocate file
0255  * operation, which gets called from sys_fallocate system call. User
0256  * space requests len bytes at offset. If FALLOC_FL_KEEP_SIZE is set
0257  * we just allocate clusters without zeroing them out. Otherwise we
0258  * allocate and zero out clusters via an expanding truncate.
0259  */
0260 static long fat_fallocate(struct file *file, int mode,
0261               loff_t offset, loff_t len)
0262 {
0263     int nr_cluster; /* Number of clusters to be allocated */
0264     loff_t mm_bytes; /* Number of bytes to be allocated for file */
0265     loff_t ondisksize; /* block aligned on-disk size in bytes*/
0266     struct inode *inode = file->f_mapping->host;
0267     struct super_block *sb = inode->i_sb;
0268     struct msdos_sb_info *sbi = MSDOS_SB(sb);
0269     int err = 0;
0270 
0271     /* No support for hole punch or other fallocate flags. */
0272     if (mode & ~FALLOC_FL_KEEP_SIZE)
0273         return -EOPNOTSUPP;
0274 
0275     /* No support for dir */
0276     if (!S_ISREG(inode->i_mode))
0277         return -EOPNOTSUPP;
0278 
0279     inode_lock(inode);
0280     if (mode & FALLOC_FL_KEEP_SIZE) {
0281         ondisksize = inode->i_blocks << 9;
0282         if ((offset + len) <= ondisksize)
0283             goto error;
0284 
0285         /* First compute the number of clusters to be allocated */
0286         mm_bytes = offset + len - ondisksize;
0287         nr_cluster = (mm_bytes + (sbi->cluster_size - 1)) >>
0288             sbi->cluster_bits;
0289 
0290         /* Start the allocation.We are not zeroing out the clusters */
0291         while (nr_cluster-- > 0) {
0292             err = fat_add_cluster(inode);
0293             if (err)
0294                 goto error;
0295         }
0296     } else {
0297         if ((offset + len) <= i_size_read(inode))
0298             goto error;
0299 
0300         /* This is just an expanding truncate */
0301         err = fat_cont_expand(inode, (offset + len));
0302     }
0303 
0304 error:
0305     inode_unlock(inode);
0306     return err;
0307 }
0308 
0309 /* Free all clusters after the skip'th cluster. */
0310 static int fat_free(struct inode *inode, int skip)
0311 {
0312     struct super_block *sb = inode->i_sb;
0313     int err, wait, free_start, i_start, i_logstart;
0314 
0315     if (MSDOS_I(inode)->i_start == 0)
0316         return 0;
0317 
0318     fat_cache_inval_inode(inode);
0319 
0320     wait = IS_DIRSYNC(inode);
0321     i_start = free_start = MSDOS_I(inode)->i_start;
0322     i_logstart = MSDOS_I(inode)->i_logstart;
0323 
0324     /* First, we write the new file size. */
0325     if (!skip) {
0326         MSDOS_I(inode)->i_start = 0;
0327         MSDOS_I(inode)->i_logstart = 0;
0328     }
0329     MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
0330     fat_truncate_time(inode, NULL, S_CTIME|S_MTIME);
0331     if (wait) {
0332         err = fat_sync_inode(inode);
0333         if (err) {
0334             MSDOS_I(inode)->i_start = i_start;
0335             MSDOS_I(inode)->i_logstart = i_logstart;
0336             return err;
0337         }
0338     } else
0339         mark_inode_dirty(inode);
0340 
0341     /* Write a new EOF, and get the remaining cluster chain for freeing. */
0342     if (skip) {
0343         struct fat_entry fatent;
0344         int ret, fclus, dclus;
0345 
0346         ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus);
0347         if (ret < 0)
0348             return ret;
0349         else if (ret == FAT_ENT_EOF)
0350             return 0;
0351 
0352         fatent_init(&fatent);
0353         ret = fat_ent_read(inode, &fatent, dclus);
0354         if (ret == FAT_ENT_EOF) {
0355             fatent_brelse(&fatent);
0356             return 0;
0357         } else if (ret == FAT_ENT_FREE) {
0358             fat_fs_error(sb,
0359                      "%s: invalid cluster chain (i_pos %lld)",
0360                      __func__, MSDOS_I(inode)->i_pos);
0361             ret = -EIO;
0362         } else if (ret > 0) {
0363             err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait);
0364             if (err)
0365                 ret = err;
0366         }
0367         fatent_brelse(&fatent);
0368         if (ret < 0)
0369             return ret;
0370 
0371         free_start = ret;
0372     }
0373     inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
0374 
0375     /* Freeing the remained cluster chain */
0376     return fat_free_clusters(inode, free_start);
0377 }
0378 
0379 void fat_truncate_blocks(struct inode *inode, loff_t offset)
0380 {
0381     struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
0382     const unsigned int cluster_size = sbi->cluster_size;
0383     int nr_clusters;
0384 
0385     /*
0386      * This protects against truncating a file bigger than it was then
0387      * trying to write into the hole.
0388      */
0389     if (MSDOS_I(inode)->mmu_private > offset)
0390         MSDOS_I(inode)->mmu_private = offset;
0391 
0392     nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits;
0393 
0394     fat_free(inode, nr_clusters);
0395     fat_flush_inodes(inode->i_sb, inode, NULL);
0396 }
0397 
0398 int fat_getattr(struct user_namespace *mnt_userns, const struct path *path,
0399         struct kstat *stat, u32 request_mask, unsigned int flags)
0400 {
0401     struct inode *inode = d_inode(path->dentry);
0402     struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
0403 
0404     generic_fillattr(mnt_userns, inode, stat);
0405     stat->blksize = sbi->cluster_size;
0406 
0407     if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) {
0408         /* Use i_pos for ino. This is used as fileid of nfs. */
0409         stat->ino = fat_i_pos_read(sbi, inode);
0410     }
0411 
0412     if (sbi->options.isvfat && request_mask & STATX_BTIME) {
0413         stat->result_mask |= STATX_BTIME;
0414         stat->btime = MSDOS_I(inode)->i_crtime;
0415     }
0416 
0417     return 0;
0418 }
0419 EXPORT_SYMBOL_GPL(fat_getattr);
0420 
0421 static int fat_sanitize_mode(const struct msdos_sb_info *sbi,
0422                  struct inode *inode, umode_t *mode_ptr)
0423 {
0424     umode_t mask, perm;
0425 
0426     /*
0427      * Note, the basic check is already done by a caller of
0428      * (attr->ia_mode & ~FAT_VALID_MODE)
0429      */
0430 
0431     if (S_ISREG(inode->i_mode))
0432         mask = sbi->options.fs_fmask;
0433     else
0434         mask = sbi->options.fs_dmask;
0435 
0436     perm = *mode_ptr & ~(S_IFMT | mask);
0437 
0438     /*
0439      * Of the r and x bits, all (subject to umask) must be present. Of the
0440      * w bits, either all (subject to umask) or none must be present.
0441      *
0442      * If fat_mode_can_hold_ro(inode) is false, can't change w bits.
0443      */
0444     if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO)))
0445         return -EPERM;
0446     if (fat_mode_can_hold_ro(inode)) {
0447         if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask)))
0448             return -EPERM;
0449     } else {
0450         if ((perm & S_IWUGO) != (S_IWUGO & ~mask))
0451             return -EPERM;
0452     }
0453 
0454     *mode_ptr &= S_IFMT | perm;
0455 
0456     return 0;
0457 }
0458 
0459 static int fat_allow_set_time(struct user_namespace *mnt_userns,
0460                   struct msdos_sb_info *sbi, struct inode *inode)
0461 {
0462     umode_t allow_utime = sbi->options.allow_utime;
0463 
0464     if (!uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode))) {
0465         if (in_group_p(i_gid_into_mnt(mnt_userns, inode)))
0466             allow_utime >>= 3;
0467         if (allow_utime & MAY_WRITE)
0468             return 1;
0469     }
0470 
0471     /* use a default check */
0472     return 0;
0473 }
0474 
0475 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)
0476 /* valid file mode bits */
0477 #define FAT_VALID_MODE  (S_IFREG | S_IFDIR | S_IRWXUGO)
0478 
0479 int fat_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
0480         struct iattr *attr)
0481 {
0482     struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb);
0483     struct inode *inode = d_inode(dentry);
0484     unsigned int ia_valid;
0485     int error;
0486 
0487     /* Check for setting the inode time. */
0488     ia_valid = attr->ia_valid;
0489     if (ia_valid & TIMES_SET_FLAGS) {
0490         if (fat_allow_set_time(mnt_userns, sbi, inode))
0491             attr->ia_valid &= ~TIMES_SET_FLAGS;
0492     }
0493 
0494     error = setattr_prepare(mnt_userns, dentry, attr);
0495     attr->ia_valid = ia_valid;
0496     if (error) {
0497         if (sbi->options.quiet)
0498             error = 0;
0499         goto out;
0500     }
0501 
0502     /*
0503      * Expand the file. Since inode_setattr() updates ->i_size
0504      * before calling the ->truncate(), but FAT needs to fill the
0505      * hole before it. XXX: this is no longer true with new truncate
0506      * sequence.
0507      */
0508     if (attr->ia_valid & ATTR_SIZE) {
0509         inode_dio_wait(inode);
0510 
0511         if (attr->ia_size > inode->i_size) {
0512             error = fat_cont_expand(inode, attr->ia_size);
0513             if (error || attr->ia_valid == ATTR_SIZE)
0514                 goto out;
0515             attr->ia_valid &= ~ATTR_SIZE;
0516         }
0517     }
0518 
0519     if (((attr->ia_valid & ATTR_UID) &&
0520          (!uid_eq(from_vfsuid(mnt_userns, i_user_ns(inode), attr->ia_vfsuid),
0521               sbi->options.fs_uid))) ||
0522         ((attr->ia_valid & ATTR_GID) &&
0523          (!gid_eq(from_vfsgid(mnt_userns, i_user_ns(inode), attr->ia_vfsgid),
0524               sbi->options.fs_gid))) ||
0525         ((attr->ia_valid & ATTR_MODE) &&
0526          (attr->ia_mode & ~FAT_VALID_MODE)))
0527         error = -EPERM;
0528 
0529     if (error) {
0530         if (sbi->options.quiet)
0531             error = 0;
0532         goto out;
0533     }
0534 
0535     /*
0536      * We don't return -EPERM here. Yes, strange, but this is too
0537      * old behavior.
0538      */
0539     if (attr->ia_valid & ATTR_MODE) {
0540         if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0)
0541             attr->ia_valid &= ~ATTR_MODE;
0542     }
0543 
0544     if (attr->ia_valid & ATTR_SIZE) {
0545         error = fat_block_truncate_page(inode, attr->ia_size);
0546         if (error)
0547             goto out;
0548         down_write(&MSDOS_I(inode)->truncate_lock);
0549         truncate_setsize(inode, attr->ia_size);
0550         fat_truncate_blocks(inode, attr->ia_size);
0551         up_write(&MSDOS_I(inode)->truncate_lock);
0552     }
0553 
0554     /*
0555      * setattr_copy can't truncate these appropriately, so we'll
0556      * copy them ourselves
0557      */
0558     if (attr->ia_valid & ATTR_ATIME)
0559         fat_truncate_time(inode, &attr->ia_atime, S_ATIME);
0560     if (attr->ia_valid & ATTR_CTIME)
0561         fat_truncate_time(inode, &attr->ia_ctime, S_CTIME);
0562     if (attr->ia_valid & ATTR_MTIME)
0563         fat_truncate_time(inode, &attr->ia_mtime, S_MTIME);
0564     attr->ia_valid &= ~(ATTR_ATIME|ATTR_CTIME|ATTR_MTIME);
0565 
0566     setattr_copy(mnt_userns, inode, attr);
0567     mark_inode_dirty(inode);
0568 out:
0569     return error;
0570 }
0571 EXPORT_SYMBOL_GPL(fat_setattr);
0572 
0573 const struct inode_operations fat_file_inode_operations = {
0574     .setattr    = fat_setattr,
0575     .getattr    = fat_getattr,
0576     .update_time    = fat_update_time,
0577 };