1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. 4 */ 5 6 #include <linux/init.h> 7 #include <linux/buffer_head.h> 8 #include <linux/mpage.h> 9 #include <linux/bio.h> 10 #include <linux/blkdev.h> 11 #include <linux/time.h> 12 #include <linux/writeback.h> 13 #include <linux/uio.h> 14 #include <linux/random.h> 15 #include <linux/iversion.h> 16 #include <linux/iomap.h> 17 18 #include "exfat_raw.h" 19 #include "exfat_fs.h" 20 #include "iomap.h" 21 22 int __exfat_write_inode(struct inode *inode, int sync) 23 { 24 unsigned long long on_disk_size; 25 unsigned long long on_disk_valid_size; 26 struct exfat_dentry *ep, *ep2; 27 struct exfat_entry_set_cache es; 28 struct super_block *sb = inode->i_sb; 29 struct exfat_sb_info *sbi = EXFAT_SB(sb); 30 struct exfat_inode_info *ei = EXFAT_I(inode); 31 bool is_dir = (ei->type == TYPE_DIR); 32 loff_t valid_size = exfat_get_valid_size(ei); 33 struct timespec64 ts; 34 35 if (inode->i_ino == EXFAT_ROOT_INO) 36 return 0; 37 38 /* 39 * If the inode is already unlinked, there is no need for updating it. 40 */ 41 if (ei->dir.dir == DIR_DELETED) 42 return 0; 43 44 if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1) 45 return 0; 46 47 exfat_set_volume_dirty(sb); 48 49 /* get the directory entry of given file or directory */ 50 if (exfat_get_dentry_set_by_ei(&es, sb, ei)) 51 return -EIO; 52 ep = exfat_get_dentry_cached(&es, ES_IDX_FILE); 53 ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM); 54 55 ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode)); 56 57 /* set FILE_INFO structure using the acquired struct exfat_dentry */ 58 exfat_set_entry_time(sbi, &ei->i_crtime, 59 &ep->dentry.file.create_tz, 60 &ep->dentry.file.create_time, 61 &ep->dentry.file.create_date, 62 &ep->dentry.file.create_time_cs); 63 ts = inode_get_mtime(inode); 64 exfat_set_entry_time(sbi, &ts, 65 &ep->dentry.file.modify_tz, 66 &ep->dentry.file.modify_time, 67 &ep->dentry.file.modify_date, 68 &ep->dentry.file.modify_time_cs); 69 ts = inode_get_atime(inode); 70 exfat_set_entry_time(sbi, &ts, 71 &ep->dentry.file.access_tz, 72 &ep->dentry.file.access_time, 73 &ep->dentry.file.access_date, 74 NULL); 75 76 /* 77 * During a DIO write, valid_size is updated eagerly in iomap_end (so 78 * that concurrent buffered reads see IOMAP_MAPPED) while i_size is 79 * updated asynchronously in end_io. The FAT chain was already 80 * extended to cover ceil(valid_size/cluster_size) clusters. Use the 81 * maximum so the on-disk size field always covers the FAT chain, 82 * preventing fsck from reporting "more clusters are allocated". 83 */ 84 on_disk_size = max_t(unsigned long long, i_size_read(inode), 85 valid_size); 86 87 if (ei->start_clu == EXFAT_EOF_CLUSTER) 88 on_disk_size = 0; 89 /* 90 * valid_size on disk must reflect only confirmed data (up to i_size) 91 * and must not exceed on_disk_size. 92 */ 93 on_disk_valid_size = min_t(unsigned long long, valid_size, 94 i_size_read(inode)); 95 if (ei->start_clu == EXFAT_EOF_CLUSTER) 96 on_disk_valid_size = 0; 97 98 ep2->dentry.stream.size = cpu_to_le64(on_disk_size); 99 ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_valid_size); 100 101 if (on_disk_size) { 102 ep2->dentry.stream.flags = ei->flags; 103 ep2->dentry.stream.start_clu = cpu_to_le32(ei->start_clu); 104 } else { 105 ep2->dentry.stream.flags = ALLOC_FAT_CHAIN; 106 ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER; 107 } 108 109 exfat_update_dir_chksum(&es); 110 return exfat_put_dentry_set(&es, sync); 111 } 112 113 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc) 114 { 115 int ret; 116 117 if (unlikely(exfat_forced_shutdown(inode->i_sb))) 118 return -EIO; 119 120 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 121 ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL); 122 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 123 124 return ret; 125 } 126 127 void exfat_sync_inode(struct inode *inode) 128 { 129 lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock); 130 __exfat_write_inode(inode, 1); 131 } 132 133 /* 134 * Input: inode, (logical) clu_offset, target allocation area 135 * Output: errcode, cluster number 136 * *clu = (~0), if it's unable to allocate a new cluster 137 */ 138 int exfat_map_cluster(struct inode *inode, unsigned int clu_offset, 139 unsigned int *clu, unsigned int *count, int create, 140 bool *balloc) 141 { 142 int ret; 143 unsigned int last_clu; 144 struct exfat_chain new_clu; 145 struct super_block *sb = inode->i_sb; 146 struct exfat_sb_info *sbi = EXFAT_SB(sb); 147 struct exfat_inode_info *ei = EXFAT_I(inode); 148 unsigned int local_clu_offset = clu_offset; 149 unsigned int num_to_be_allocated = 0, num_clusters; 150 151 num_clusters = exfat_bytes_to_cluster(sbi, exfat_ondisk_size(inode)); 152 if (clu_offset > num_clusters || 153 *count > num_clusters - clu_offset) 154 num_to_be_allocated = clu_offset + *count - num_clusters; 155 156 if (!create && (num_to_be_allocated > 0)) { 157 *clu = EXFAT_EOF_CLUSTER; 158 return 0; 159 } 160 161 *clu = last_clu = ei->start_clu; 162 163 if (*clu == EXFAT_EOF_CLUSTER) { 164 *count = 0; 165 } else if (ei->flags == ALLOC_NO_FAT_CHAIN) { 166 last_clu += num_clusters - 1; 167 if (clu_offset < num_clusters) { 168 *clu += clu_offset; 169 *count = min(num_clusters - clu_offset, *count); 170 } else { 171 *clu = EXFAT_EOF_CLUSTER; 172 *count = 0; 173 } 174 } else { 175 int err = exfat_get_cluster(inode, clu_offset, 176 clu, count, &last_clu); 177 if (err) 178 return -EIO; 179 } 180 181 if (*clu == EXFAT_EOF_CLUSTER) { 182 exfat_set_volume_dirty(sb); 183 184 new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ? 185 EXFAT_EOF_CLUSTER : last_clu + 1; 186 new_clu.size = 0; 187 new_clu.flags = ei->flags; 188 189 /* allocate a cluster */ 190 if (num_to_be_allocated < 1) { 191 /* Broken FAT (i_sze > allocated FAT) */ 192 exfat_fs_error(sb, "broken FAT chain."); 193 return -EIO; 194 } 195 196 ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu, 197 inode_needs_sync(inode), true); 198 if (ret) 199 return ret; 200 201 if (new_clu.dir == EXFAT_EOF_CLUSTER || 202 new_clu.dir == EXFAT_FREE_CLUSTER) { 203 exfat_fs_error(sb, 204 "bogus cluster new allocated (last_clu : %u, new_clu : %u)", 205 last_clu, new_clu.dir); 206 return -EIO; 207 } 208 209 /* append to the FAT chain */ 210 if (last_clu == EXFAT_EOF_CLUSTER) { 211 if (new_clu.flags == ALLOC_FAT_CHAIN) 212 ei->flags = ALLOC_FAT_CHAIN; 213 ei->start_clu = new_clu.dir; 214 } else { 215 if (new_clu.flags != ei->flags) { 216 /* no-fat-chain bit is disabled, 217 * so fat-chain should be synced with 218 * alloc-bitmap 219 */ 220 if (exfat_chain_cont_cluster(sb, ei->start_clu, 221 num_clusters)) 222 return -EIO; 223 ei->flags = ALLOC_FAT_CHAIN; 224 } 225 if (new_clu.flags == ALLOC_FAT_CHAIN) 226 if (exfat_ent_set(sb, last_clu, new_clu.dir)) 227 return -EIO; 228 } 229 230 *clu = new_clu.dir; 231 *count = new_clu.size; 232 233 inode->i_blocks += exfat_cluster_to_sectors(sbi, new_clu.size); 234 if (balloc) 235 *balloc = true; 236 } 237 238 /* hint information */ 239 ei->hint_bmap.off = local_clu_offset; 240 ei->hint_bmap.clu = *clu; 241 242 return 0; 243 } 244 245 static int exfat_read_folio(struct file *file, struct folio *folio) 246 { 247 struct iomap_read_folio_ctx ctx = { 248 .cur_folio = folio, 249 .ops = &exfat_iomap_bio_read_ops, 250 }; 251 252 iomap_read_folio(exfat_iomap_next, &ctx, NULL); 253 return 0; 254 } 255 256 static void exfat_readahead(struct readahead_control *rac) 257 { 258 struct address_space *mapping = rac->mapping; 259 struct inode *inode = mapping->host; 260 struct exfat_inode_info *ei = EXFAT_I(inode); 261 loff_t pos = readahead_pos(rac); 262 loff_t valid_size = exfat_get_valid_size(ei); 263 struct iomap_read_folio_ctx ctx = { 264 .ops = &exfat_iomap_bio_read_ops, 265 .rac = rac, 266 }; 267 268 /* Range cross valid_size, read it page by page. */ 269 if (valid_size < i_size_read(inode) && 270 pos <= valid_size && 271 valid_size < pos + readahead_length(rac)) 272 return; 273 274 iomap_readahead(exfat_iomap_next, &ctx, NULL); 275 } 276 277 static int exfat_writepages(struct address_space *mapping, 278 struct writeback_control *wbc) 279 { 280 struct iomap_writepage_ctx wpc = { 281 .inode = mapping->host, 282 .wbc = wbc, 283 .ops = &exfat_writeback_ops, 284 }; 285 286 if (unlikely(exfat_forced_shutdown(mapping->host->i_sb))) 287 return -EIO; 288 289 return iomap_writepages(&wpc); 290 } 291 292 static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block) 293 { 294 sector_t blocknr; 295 296 inode_lock_shared(mapping->host); 297 blocknr = iomap_bmap(mapping, block, exfat_iomap_next); 298 inode_unlock_shared(mapping->host); 299 return blocknr; 300 } 301 302 static const struct address_space_operations exfat_aops = { 303 .read_folio = exfat_read_folio, 304 .readahead = exfat_readahead, 305 .writepages = exfat_writepages, 306 .dirty_folio = iomap_dirty_folio, 307 .bmap = exfat_aop_bmap, 308 .migrate_folio = filemap_migrate_folio, 309 .is_partially_uptodate = iomap_is_partially_uptodate, 310 .error_remove_folio = generic_error_remove_folio, 311 .release_folio = iomap_release_folio, 312 .invalidate_folio = iomap_invalidate_folio, 313 .swap_activate = exfat_iomap_swap_activate, 314 }; 315 316 static inline unsigned long exfat_hash(loff_t i_pos) 317 { 318 return hash_32(i_pos, EXFAT_HASH_BITS); 319 } 320 321 void exfat_hash_inode(struct inode *inode, loff_t i_pos) 322 { 323 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 324 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos); 325 326 spin_lock(&sbi->inode_hash_lock); 327 EXFAT_I(inode)->i_pos = i_pos; 328 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head); 329 spin_unlock(&sbi->inode_hash_lock); 330 } 331 332 void exfat_unhash_inode(struct inode *inode) 333 { 334 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 335 336 spin_lock(&sbi->inode_hash_lock); 337 hlist_del_init(&EXFAT_I(inode)->i_hash_fat); 338 EXFAT_I(inode)->i_pos = 0; 339 spin_unlock(&sbi->inode_hash_lock); 340 } 341 342 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos) 343 { 344 struct exfat_sb_info *sbi = EXFAT_SB(sb); 345 struct exfat_inode_info *info; 346 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos); 347 struct inode *inode = NULL; 348 349 spin_lock(&sbi->inode_hash_lock); 350 hlist_for_each_entry(info, head, i_hash_fat) { 351 WARN_ON(info->vfs_inode.i_sb != sb); 352 353 if (i_pos != info->i_pos) 354 continue; 355 inode = igrab(&info->vfs_inode); 356 if (inode) 357 break; 358 } 359 spin_unlock(&sbi->inode_hash_lock); 360 return inode; 361 } 362 363 /* doesn't deal with root inode */ 364 static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info) 365 { 366 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); 367 struct exfat_inode_info *ei = EXFAT_I(inode); 368 loff_t size = info->size; 369 370 ei->dir = info->dir; 371 ei->entry = info->entry; 372 ei->attr = info->attr; 373 ei->start_clu = info->start_clu; 374 ei->flags = info->flags; 375 ei->type = info->type; 376 exfat_set_valid_size(ei, info->valid_size); 377 exfat_set_zeroed_size(ei, info->valid_size); 378 379 ei->version = 0; 380 ei->hint_stat.eidx = 0; 381 ei->hint_stat.clu = info->start_clu; 382 ei->hint_femp.eidx = EXFAT_HINT_NONE; 383 ei->hint_bmap.off = EXFAT_EOF_CLUSTER; 384 ei->i_pos = 0; 385 386 inode->i_uid = sbi->options.fs_uid; 387 inode->i_gid = sbi->options.fs_gid; 388 inode_inc_iversion(inode); 389 inode->i_generation = get_random_u32(); 390 391 if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */ 392 inode->i_generation &= ~1; 393 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777); 394 inode->i_op = &exfat_dir_inode_operations; 395 inode->i_fop = &exfat_dir_operations; 396 set_nlink(inode, info->num_subdirs); 397 } else { /* regular file */ 398 inode->i_generation |= 1; 399 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777); 400 inode->i_op = &exfat_file_inode_operations; 401 inode->i_fop = &exfat_file_operations; 402 inode->i_mapping->a_ops = &exfat_aops; 403 inode->i_mapping->nrpages = 0; 404 } 405 406 i_size_write(inode, size); 407 408 exfat_save_attr(inode, info->attr); 409 410 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9; 411 inode_set_mtime_to_ts(inode, info->mtime); 412 inode_set_ctime_to_ts(inode, info->mtime); 413 ei->i_crtime = info->crtime; 414 inode_set_atime_to_ts(inode, info->atime); 415 416 return 0; 417 } 418 419 struct inode *exfat_build_inode(struct super_block *sb, 420 struct exfat_dir_entry *info, loff_t i_pos) 421 { 422 struct inode *inode; 423 int err; 424 425 inode = exfat_iget(sb, i_pos); 426 if (inode) 427 goto out; 428 inode = new_inode(sb); 429 if (!inode) { 430 inode = ERR_PTR(-ENOMEM); 431 goto out; 432 } 433 inode->i_ino = iunique(sb, EXFAT_ROOT_INO); 434 inode_set_iversion(inode, 1); 435 err = exfat_fill_inode(inode, info); 436 if (err) { 437 iput(inode); 438 inode = ERR_PTR(err); 439 goto out; 440 } 441 exfat_hash_inode(inode, i_pos); 442 insert_inode_hash(inode); 443 out: 444 return inode; 445 } 446 447 void exfat_evict_inode(struct inode *inode) 448 { 449 truncate_inode_pages_final(&inode->i_data); 450 451 if (!inode->i_nlink) { 452 i_size_write(inode, 0); 453 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock); 454 __exfat_truncate(inode); 455 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock); 456 } 457 458 clear_inode(inode); 459 exfat_cache_inval_inode(inode); 460 exfat_unhash_inode(inode); 461 } 462