^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/mpage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/bio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "exfat_raw.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "exfat_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int __exfat_write_inode(struct inode *inode, int sync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned long long on_disk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct exfat_dentry *ep, *ep2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct exfat_entry_set_cache *es = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) bool is_dir = (ei->type == TYPE_DIR) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (inode->i_ino == EXFAT_ROOT_INO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * If the indode is already unlinked, there is no need for updating it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (ei->dir.dir == DIR_DELETED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) exfat_set_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* get the directory entry of given file or directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (!es)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ep = exfat_get_dentry_cached(es, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ep2 = exfat_get_dentry_cached(es, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /* set FILE_INFO structure using the acquired struct exfat_dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) exfat_set_entry_time(sbi, &ei->i_crtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) &ep->dentry.file.create_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) &ep->dentry.file.create_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) &ep->dentry.file.create_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) &ep->dentry.file.create_time_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) exfat_set_entry_time(sbi, &inode->i_mtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) &ep->dentry.file.modify_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) &ep->dentry.file.modify_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) &ep->dentry.file.modify_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) &ep->dentry.file.modify_time_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) exfat_set_entry_time(sbi, &inode->i_atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) &ep->dentry.file.access_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) &ep->dentry.file.access_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) &ep->dentry.file.access_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* File size should be zero if there is no cluster allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) on_disk_size = i_size_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (ei->start_clu == EXFAT_EOF_CLUSTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) on_disk_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ep2->dentry.stream.size = ep2->dentry.stream.valid_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) exfat_update_dir_chksum_with_entry_set(es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return exfat_free_dentry_set(es, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) void exfat_sync_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) __exfat_write_inode(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * Input: inode, (logical) clu_offset, target allocation area
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Output: errcode, cluster number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * *clu = (~0), if it's unable to allocate a new cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) unsigned int *clu, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int ret, modified = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) unsigned int last_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct exfat_chain new_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) unsigned int local_clu_offset = clu_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) unsigned int num_to_be_allocated = 0, num_clusters = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (ei->i_size_ondisk > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) num_clusters =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (clu_offset >= num_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) num_to_be_allocated = clu_offset - num_clusters + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (!create && (num_to_be_allocated > 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) *clu = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *clu = last_clu = ei->start_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (ei->flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) last_clu += clu_offset - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (clu_offset == num_clusters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) *clu = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *clu += clu_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) } else if (ei->type == TYPE_FILE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) unsigned int fclus = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int err = exfat_get_cluster(inode, clu_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) &fclus, clu, &last_clu, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) clu_offset -= fclus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* hint information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) clu_offset -= ei->hint_bmap.off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* hint_bmap.clu should be valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) WARN_ON(ei->hint_bmap.clu < 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) *clu = ei->hint_bmap.clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) last_clu = *clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (exfat_get_next_cluster(sb, clu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) clu_offset--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (*clu == EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) exfat_set_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) EXFAT_EOF_CLUSTER : last_clu + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) new_clu.size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) new_clu.flags = ei->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /* allocate a cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (num_to_be_allocated < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* Broken FAT (i_sze > allocated FAT) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) exfat_fs_error(sb, "broken FAT chain.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) inode_needs_sync(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (new_clu.dir == EXFAT_EOF_CLUSTER ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) new_clu.dir == EXFAT_FREE_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) "bogus cluster new allocated (last_clu : %u, new_clu : %u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) last_clu, new_clu.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* append to the FAT chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (last_clu == EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (new_clu.flags == ALLOC_FAT_CHAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ei->flags = ALLOC_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) ei->start_clu = new_clu.dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) modified = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (new_clu.flags != ei->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /* no-fat-chain bit is disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * so fat-chain should be synced with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * alloc-bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) exfat_chain_cont_cluster(sb, ei->start_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) num_clusters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) ei->flags = ALLOC_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) modified = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (new_clu.flags == ALLOC_FAT_CHAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (exfat_ent_set(sb, last_clu, new_clu.dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) num_clusters += num_to_be_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) *clu = new_clu.dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (ei->dir.dir != DIR_DELETED && modified) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct exfat_entry_set_cache *es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ES_ALL_ENTRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!es)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* get stream entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ep = exfat_get_dentry_cached(es, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /* update directory entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ep->dentry.stream.flags = ei->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) ep->dentry.stream.start_clu =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) cpu_to_le32(ei->start_clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ep->dentry.stream.valid_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) cpu_to_le64(i_size_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ep->dentry.stream.size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ep->dentry.stream.valid_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) exfat_update_dir_chksum_with_entry_set(es);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) err = exfat_free_dentry_set(es, inode_needs_sync(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) } /* end of if != DIR_DELETED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) inode->i_blocks +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) num_to_be_allocated << sbi->sect_per_clus_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * Move *clu pointer along FAT chains (hole care) because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * caller of this function expect *clu to be the last cluster.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * This only works when num_to_be_allocated >= 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * *clu = (the first cluster of the allocated chain) =>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * (the last cluster of ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (ei->flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) *clu += num_to_be_allocated - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) while (num_to_be_allocated > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (exfat_get_next_cluster(sb, clu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) num_to_be_allocated--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /* hint information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) ei->hint_bmap.off = local_clu_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) ei->hint_bmap.clu = *clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int exfat_map_new_buffer(struct exfat_inode_info *ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct buffer_head *bh, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (buffer_delay(bh) && pos > ei->i_size_aligned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) set_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * Adjust i_size_aligned if i_size_ondisk is bigger than it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (ei->i_size_ondisk > ei->i_size_aligned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) ei->i_size_aligned = ei->i_size_ondisk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int exfat_get_block(struct inode *inode, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct buffer_head *bh_result, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) unsigned long mapped_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) unsigned int cluster, sec_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) sector_t last_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) sector_t phys = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) loff_t pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) mutex_lock(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (iblock >= last_block && !create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /* Is this block already allocated? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) &cluster, create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (err != -ENOSPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) exfat_fs_error_ratelimit(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) "failed to bmap (inode : %p iblock : %llu, err : %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) inode, (unsigned long long)iblock, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto unlock_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (cluster == EXFAT_EOF_CLUSTER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) /* sector offset in cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) sec_offset = iblock & (sbi->sect_per_clus - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) mapped_blocks = sbi->sect_per_clus - sec_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) max_blocks = min(mapped_blocks, max_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /* Treat newly added block / cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (iblock < last_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) create = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (create || buffer_delay(bh_result)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) pos = EXFAT_BLK_TO_B((iblock + 1), sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (ei->i_size_ondisk < pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) ei->i_size_ondisk = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (create) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) err = exfat_map_new_buffer(ei, bh_result, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) "requested for bmap out of range(pos : (%llu) > i_size_aligned(%llu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) pos, ei->i_size_aligned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) goto unlock_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (buffer_delay(bh_result))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) clear_buffer_delay(bh_result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) map_bh(bh_result, sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) unlock_ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) mutex_unlock(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static int exfat_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return mpage_readpage(page, exfat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static void exfat_readahead(struct readahead_control *rac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) mpage_readahead(rac, exfat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static int exfat_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return block_write_full_page(page, exfat_get_block, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static int exfat_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return mpage_writepages(mapping, wbc, exfat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static void exfat_write_failed(struct address_space *mapping, loff_t to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (to > i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) truncate_pagecache(inode, i_size_read(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) exfat_truncate(inode, EXFAT_I(inode)->i_size_aligned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static int exfat_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) loff_t pos, unsigned int len, unsigned int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) *pagep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) exfat_get_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) &EXFAT_I(mapping->host)->i_size_ondisk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) exfat_write_failed(mapping, pos+len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static int exfat_write_end(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) loff_t pos, unsigned int len, unsigned int copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct page *pagep, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (ei->i_size_aligned < i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) exfat_fs_error(inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) "invalid size(size(%llu) > aligned(%llu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) i_size_read(inode), ei->i_size_aligned);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (err < len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) exfat_write_failed(mapping, pos+len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (!(err < 0) && !(ei->attr & ATTR_ARCHIVE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) ei->attr |= ATTR_ARCHIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct address_space *mapping = iocb->ki_filp->f_mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) loff_t size = iocb->ki_pos + iov_iter_count(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) int rw = iov_iter_rw(iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (rw == WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) * so we need to update the ->i_size_aligned to block boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * But we must fill the remaining area or hole by nul for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * updating ->i_size_aligned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * Return 0, and fallback to normal buffered write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (EXFAT_I(inode)->i_size_aligned < size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * Need to use the DIO_LOCKING for avoiding the race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * condition of exfat_get_block() and ->truncate().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (ret < 0 && (rw & WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) exfat_write_failed(mapping, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) sector_t blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) down_read(&EXFAT_I(mapping->host)->truncate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) blocknr = generic_block_bmap(mapping, block, exfat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) up_read(&EXFAT_I(mapping->host)->truncate_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * up to the end of the block which corresponds to `from'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * This is required during truncate to physically zeroout the tail end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * of that block so it doesn't yield old data if the file is later grown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * Also, avoid causing failure from fsx for cases of "data past EOF"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) int exfat_block_truncate_page(struct inode *inode, loff_t from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return block_truncate_page(inode->i_mapping, from, exfat_get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static const struct address_space_operations exfat_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) .readpage = exfat_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) .readahead = exfat_readahead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) .writepage = exfat_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) .writepages = exfat_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) .write_begin = exfat_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) .write_end = exfat_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) .direct_IO = exfat_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) .bmap = exfat_aop_bmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static inline unsigned long exfat_hash(loff_t i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return hash_32(i_pos, EXFAT_HASH_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) void exfat_hash_inode(struct inode *inode, loff_t i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) spin_lock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) EXFAT_I(inode)->i_pos = i_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) spin_unlock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) void exfat_unhash_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) spin_lock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) EXFAT_I(inode)->i_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) spin_unlock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) struct exfat_inode_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) spin_lock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) hlist_for_each_entry(info, head, i_hash_fat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) WARN_ON(info->vfs_inode.i_sb != sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (i_pos != info->i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) inode = igrab(&info->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) spin_unlock(&sbi->inode_hash_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) /* doesn't deal with root inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) loff_t size = info->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) ei->dir = info->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) ei->entry = info->entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) ei->attr = info->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ei->start_clu = info->start_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) ei->flags = info->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) ei->type = info->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) ei->version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) ei->hint_stat.eidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) ei->hint_stat.clu = info->start_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) ei->hint_femp.eidx = EXFAT_HINT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) ei->i_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) inode->i_uid = sbi->options.fs_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) inode->i_gid = sbi->options.fs_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) inode->i_generation = prandom_u32();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (info->attr & ATTR_SUBDIR) { /* directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) inode->i_generation &= ~1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) inode->i_op = &exfat_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) inode->i_fop = &exfat_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) set_nlink(inode, info->num_subdirs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) } else { /* regular file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) inode->i_generation |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) inode->i_op = &exfat_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) inode->i_fop = &exfat_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) inode->i_mapping->a_ops = &exfat_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) inode->i_mapping->nrpages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) i_size_write(inode, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) /* ondisk and aligned size should be aligned with block size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) if (size & (inode->i_sb->s_blocksize - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) size |= (inode->i_sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) ei->i_size_aligned = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) ei->i_size_ondisk = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) exfat_save_attr(inode, info->attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) inode->i_blkbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) inode->i_mtime = info->mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) inode->i_ctime = info->mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) ei->i_crtime = info->crtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) inode->i_atime = info->atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) struct inode *exfat_build_inode(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) struct exfat_dir_entry *info, loff_t i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) inode = exfat_iget(sb, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) inode = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) inode_set_iversion(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) err = exfat_fill_inode(inode, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) inode = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) exfat_hash_inode(inode, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) void exfat_evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) truncate_inode_pages(&inode->i_data, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (!inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) i_size_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) __exfat_truncate(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) invalidate_inode_buffers(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) exfat_cache_inval_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) exfat_unhash_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }