0001
0002
0003
0004
0005
0006 #include <linux/init.h>
0007 #include <linux/buffer_head.h>
0008 #include <linux/mpage.h>
0009 #include <linux/bio.h>
0010 #include <linux/blkdev.h>
0011 #include <linux/time.h>
0012 #include <linux/writeback.h>
0013 #include <linux/uio.h>
0014 #include <linux/random.h>
0015 #include <linux/iversion.h>
0016
0017 #include "exfat_raw.h"
0018 #include "exfat_fs.h"
0019
0020 int __exfat_write_inode(struct inode *inode, int sync)
0021 {
0022 unsigned long long on_disk_size;
0023 struct exfat_dentry *ep, *ep2;
0024 struct exfat_entry_set_cache *es = NULL;
0025 struct super_block *sb = inode->i_sb;
0026 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0027 struct exfat_inode_info *ei = EXFAT_I(inode);
0028 bool is_dir = (ei->type == TYPE_DIR) ? true : false;
0029
0030 if (inode->i_ino == EXFAT_ROOT_INO)
0031 return 0;
0032
0033
0034
0035
0036 if (ei->dir.dir == DIR_DELETED)
0037 return 0;
0038
0039 if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
0040 return 0;
0041
0042 exfat_set_volume_dirty(sb);
0043
0044
0045 es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES);
0046 if (!es)
0047 return -EIO;
0048 ep = exfat_get_dentry_cached(es, 0);
0049 ep2 = exfat_get_dentry_cached(es, 1);
0050
0051 ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode));
0052
0053
0054 exfat_set_entry_time(sbi, &ei->i_crtime,
0055 &ep->dentry.file.create_tz,
0056 &ep->dentry.file.create_time,
0057 &ep->dentry.file.create_date,
0058 &ep->dentry.file.create_time_cs);
0059 exfat_set_entry_time(sbi, &inode->i_mtime,
0060 &ep->dentry.file.modify_tz,
0061 &ep->dentry.file.modify_time,
0062 &ep->dentry.file.modify_date,
0063 &ep->dentry.file.modify_time_cs);
0064 exfat_set_entry_time(sbi, &inode->i_atime,
0065 &ep->dentry.file.access_tz,
0066 &ep->dentry.file.access_time,
0067 &ep->dentry.file.access_date,
0068 NULL);
0069
0070
0071 on_disk_size = i_size_read(inode);
0072
0073 if (ei->start_clu == EXFAT_EOF_CLUSTER)
0074 on_disk_size = 0;
0075
0076 ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size);
0077 ep2->dentry.stream.size = ep2->dentry.stream.valid_size;
0078 if (on_disk_size) {
0079 ep2->dentry.stream.flags = ei->flags;
0080 ep2->dentry.stream.start_clu = cpu_to_le32(ei->start_clu);
0081 } else {
0082 ep2->dentry.stream.flags = ALLOC_FAT_CHAIN;
0083 ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER;
0084 }
0085
0086 exfat_update_dir_chksum_with_entry_set(es);
0087 return exfat_free_dentry_set(es, sync);
0088 }
0089
0090 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
0091 {
0092 int ret;
0093
0094 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
0095 ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
0096 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
0097
0098 return ret;
0099 }
0100
0101 void exfat_sync_inode(struct inode *inode)
0102 {
0103 lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
0104 __exfat_write_inode(inode, 1);
0105 }
0106
0107
0108
0109
0110
0111
0112 static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
0113 unsigned int *clu, int create)
0114 {
0115 int ret;
0116 unsigned int last_clu;
0117 struct exfat_chain new_clu;
0118 struct super_block *sb = inode->i_sb;
0119 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0120 struct exfat_inode_info *ei = EXFAT_I(inode);
0121 unsigned int local_clu_offset = clu_offset;
0122 unsigned int num_to_be_allocated = 0, num_clusters = 0;
0123
0124 if (ei->i_size_ondisk > 0)
0125 num_clusters =
0126 EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
0127
0128 if (clu_offset >= num_clusters)
0129 num_to_be_allocated = clu_offset - num_clusters + 1;
0130
0131 if (!create && (num_to_be_allocated > 0)) {
0132 *clu = EXFAT_EOF_CLUSTER;
0133 return 0;
0134 }
0135
0136 *clu = last_clu = ei->start_clu;
0137
0138 if (ei->flags == ALLOC_NO_FAT_CHAIN) {
0139 if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
0140 last_clu += clu_offset - 1;
0141
0142 if (clu_offset == num_clusters)
0143 *clu = EXFAT_EOF_CLUSTER;
0144 else
0145 *clu += clu_offset;
0146 }
0147 } else if (ei->type == TYPE_FILE) {
0148 unsigned int fclus = 0;
0149 int err = exfat_get_cluster(inode, clu_offset,
0150 &fclus, clu, &last_clu, 1);
0151 if (err)
0152 return -EIO;
0153
0154 clu_offset -= fclus;
0155 } else {
0156
0157 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
0158 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
0159 clu_offset -= ei->hint_bmap.off;
0160
0161 WARN_ON(ei->hint_bmap.clu < 2);
0162 *clu = ei->hint_bmap.clu;
0163 }
0164
0165 while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
0166 last_clu = *clu;
0167 if (exfat_get_next_cluster(sb, clu))
0168 return -EIO;
0169 clu_offset--;
0170 }
0171 }
0172
0173 if (*clu == EXFAT_EOF_CLUSTER) {
0174 exfat_set_volume_dirty(sb);
0175
0176 new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
0177 EXFAT_EOF_CLUSTER : last_clu + 1;
0178 new_clu.size = 0;
0179 new_clu.flags = ei->flags;
0180
0181
0182 if (num_to_be_allocated < 1) {
0183
0184 exfat_fs_error(sb, "broken FAT chain.");
0185 return -EIO;
0186 }
0187
0188 ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
0189 inode_needs_sync(inode));
0190 if (ret)
0191 return ret;
0192
0193 if (new_clu.dir == EXFAT_EOF_CLUSTER ||
0194 new_clu.dir == EXFAT_FREE_CLUSTER) {
0195 exfat_fs_error(sb,
0196 "bogus cluster new allocated (last_clu : %u, new_clu : %u)",
0197 last_clu, new_clu.dir);
0198 return -EIO;
0199 }
0200
0201
0202 if (last_clu == EXFAT_EOF_CLUSTER) {
0203 if (new_clu.flags == ALLOC_FAT_CHAIN)
0204 ei->flags = ALLOC_FAT_CHAIN;
0205 ei->start_clu = new_clu.dir;
0206 } else {
0207 if (new_clu.flags != ei->flags) {
0208
0209
0210
0211
0212 exfat_chain_cont_cluster(sb, ei->start_clu,
0213 num_clusters);
0214 ei->flags = ALLOC_FAT_CHAIN;
0215 }
0216 if (new_clu.flags == ALLOC_FAT_CHAIN)
0217 if (exfat_ent_set(sb, last_clu, new_clu.dir))
0218 return -EIO;
0219 }
0220
0221 num_clusters += num_to_be_allocated;
0222 *clu = new_clu.dir;
0223
0224 inode->i_blocks +=
0225 num_to_be_allocated << sbi->sect_per_clus_bits;
0226
0227
0228
0229
0230
0231
0232
0233
0234 if (ei->flags == ALLOC_NO_FAT_CHAIN) {
0235 *clu += num_to_be_allocated - 1;
0236 } else {
0237 while (num_to_be_allocated > 1) {
0238 if (exfat_get_next_cluster(sb, clu))
0239 return -EIO;
0240 num_to_be_allocated--;
0241 }
0242 }
0243
0244 }
0245
0246
0247 ei->hint_bmap.off = local_clu_offset;
0248 ei->hint_bmap.clu = *clu;
0249
0250 return 0;
0251 }
0252
0253 static int exfat_map_new_buffer(struct exfat_inode_info *ei,
0254 struct buffer_head *bh, loff_t pos)
0255 {
0256 if (buffer_delay(bh) && pos > ei->i_size_aligned)
0257 return -EIO;
0258 set_buffer_new(bh);
0259
0260
0261
0262
0263 if (ei->i_size_ondisk > ei->i_size_aligned)
0264 ei->i_size_aligned = ei->i_size_ondisk;
0265 return 0;
0266 }
0267
0268 static int exfat_get_block(struct inode *inode, sector_t iblock,
0269 struct buffer_head *bh_result, int create)
0270 {
0271 struct exfat_inode_info *ei = EXFAT_I(inode);
0272 struct super_block *sb = inode->i_sb;
0273 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0274 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
0275 int err = 0;
0276 unsigned long mapped_blocks = 0;
0277 unsigned int cluster, sec_offset;
0278 sector_t last_block;
0279 sector_t phys = 0;
0280 loff_t pos;
0281
0282 mutex_lock(&sbi->s_lock);
0283 last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb);
0284 if (iblock >= last_block && !create)
0285 goto done;
0286
0287
0288 err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
0289 &cluster, create);
0290 if (err) {
0291 if (err != -ENOSPC)
0292 exfat_fs_error_ratelimit(sb,
0293 "failed to bmap (inode : %p iblock : %llu, err : %d)",
0294 inode, (unsigned long long)iblock, err);
0295 goto unlock_ret;
0296 }
0297
0298 if (cluster == EXFAT_EOF_CLUSTER)
0299 goto done;
0300
0301
0302 sec_offset = iblock & (sbi->sect_per_clus - 1);
0303
0304 phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
0305 mapped_blocks = sbi->sect_per_clus - sec_offset;
0306 max_blocks = min(mapped_blocks, max_blocks);
0307
0308
0309 if (iblock < last_block)
0310 create = 0;
0311
0312 if (create || buffer_delay(bh_result)) {
0313 pos = EXFAT_BLK_TO_B((iblock + 1), sb);
0314 if (ei->i_size_ondisk < pos)
0315 ei->i_size_ondisk = pos;
0316 }
0317
0318 if (create) {
0319 err = exfat_map_new_buffer(ei, bh_result, pos);
0320 if (err) {
0321 exfat_fs_error(sb,
0322 "requested for bmap out of range(pos : (%llu) > i_size_aligned(%llu)\n",
0323 pos, ei->i_size_aligned);
0324 goto unlock_ret;
0325 }
0326 }
0327
0328 if (buffer_delay(bh_result))
0329 clear_buffer_delay(bh_result);
0330 map_bh(bh_result, sb, phys);
0331 done:
0332 bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
0333 unlock_ret:
0334 mutex_unlock(&sbi->s_lock);
0335 return err;
0336 }
0337
0338 static int exfat_read_folio(struct file *file, struct folio *folio)
0339 {
0340 return mpage_read_folio(folio, exfat_get_block);
0341 }
0342
0343 static void exfat_readahead(struct readahead_control *rac)
0344 {
0345 mpage_readahead(rac, exfat_get_block);
0346 }
0347
0348 static int exfat_writepage(struct page *page, struct writeback_control *wbc)
0349 {
0350 return block_write_full_page(page, exfat_get_block, wbc);
0351 }
0352
0353 static int exfat_writepages(struct address_space *mapping,
0354 struct writeback_control *wbc)
0355 {
0356 return mpage_writepages(mapping, wbc, exfat_get_block);
0357 }
0358
0359 static void exfat_write_failed(struct address_space *mapping, loff_t to)
0360 {
0361 struct inode *inode = mapping->host;
0362
0363 if (to > i_size_read(inode)) {
0364 truncate_pagecache(inode, i_size_read(inode));
0365 inode->i_mtime = inode->i_ctime = current_time(inode);
0366 exfat_truncate(inode, EXFAT_I(inode)->i_size_aligned);
0367 }
0368 }
0369
0370 static int exfat_write_begin(struct file *file, struct address_space *mapping,
0371 loff_t pos, unsigned int len,
0372 struct page **pagep, void **fsdata)
0373 {
0374 int ret;
0375
0376 *pagep = NULL;
0377 ret = cont_write_begin(file, mapping, pos, len, pagep, fsdata,
0378 exfat_get_block,
0379 &EXFAT_I(mapping->host)->i_size_ondisk);
0380
0381 if (ret < 0)
0382 exfat_write_failed(mapping, pos+len);
0383
0384 return ret;
0385 }
0386
0387 static int exfat_write_end(struct file *file, struct address_space *mapping,
0388 loff_t pos, unsigned int len, unsigned int copied,
0389 struct page *pagep, void *fsdata)
0390 {
0391 struct inode *inode = mapping->host;
0392 struct exfat_inode_info *ei = EXFAT_I(inode);
0393 int err;
0394
0395 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
0396
0397 if (ei->i_size_aligned < i_size_read(inode)) {
0398 exfat_fs_error(inode->i_sb,
0399 "invalid size(size(%llu) > aligned(%llu)\n",
0400 i_size_read(inode), ei->i_size_aligned);
0401 return -EIO;
0402 }
0403
0404 if (err < len)
0405 exfat_write_failed(mapping, pos+len);
0406
0407 if (!(err < 0) && !(ei->attr & ATTR_ARCHIVE)) {
0408 inode->i_mtime = inode->i_ctime = current_time(inode);
0409 ei->attr |= ATTR_ARCHIVE;
0410 mark_inode_dirty(inode);
0411 }
0412
0413 return err;
0414 }
0415
0416 static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
0417 {
0418 struct address_space *mapping = iocb->ki_filp->f_mapping;
0419 struct inode *inode = mapping->host;
0420 loff_t size = iocb->ki_pos + iov_iter_count(iter);
0421 int rw = iov_iter_rw(iter);
0422 ssize_t ret;
0423
0424 if (rw == WRITE) {
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434 if (EXFAT_I(inode)->i_size_aligned < size)
0435 return 0;
0436 }
0437
0438
0439
0440
0441
0442 ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
0443 if (ret < 0 && (rw & WRITE))
0444 exfat_write_failed(mapping, size);
0445 return ret;
0446 }
0447
0448 static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
0449 {
0450 sector_t blocknr;
0451
0452
0453 down_read(&EXFAT_I(mapping->host)->truncate_lock);
0454 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
0455 up_read(&EXFAT_I(mapping->host)->truncate_lock);
0456 return blocknr;
0457 }
0458
0459
0460
0461
0462
0463
0464
0465
0466 int exfat_block_truncate_page(struct inode *inode, loff_t from)
0467 {
0468 return block_truncate_page(inode->i_mapping, from, exfat_get_block);
0469 }
0470
0471 static const struct address_space_operations exfat_aops = {
0472 .dirty_folio = block_dirty_folio,
0473 .invalidate_folio = block_invalidate_folio,
0474 .read_folio = exfat_read_folio,
0475 .readahead = exfat_readahead,
0476 .writepage = exfat_writepage,
0477 .writepages = exfat_writepages,
0478 .write_begin = exfat_write_begin,
0479 .write_end = exfat_write_end,
0480 .direct_IO = exfat_direct_IO,
0481 .bmap = exfat_aop_bmap
0482 };
0483
0484 static inline unsigned long exfat_hash(loff_t i_pos)
0485 {
0486 return hash_32(i_pos, EXFAT_HASH_BITS);
0487 }
0488
0489 void exfat_hash_inode(struct inode *inode, loff_t i_pos)
0490 {
0491 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
0492 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
0493
0494 spin_lock(&sbi->inode_hash_lock);
0495 EXFAT_I(inode)->i_pos = i_pos;
0496 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
0497 spin_unlock(&sbi->inode_hash_lock);
0498 }
0499
0500 void exfat_unhash_inode(struct inode *inode)
0501 {
0502 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
0503
0504 spin_lock(&sbi->inode_hash_lock);
0505 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
0506 EXFAT_I(inode)->i_pos = 0;
0507 spin_unlock(&sbi->inode_hash_lock);
0508 }
0509
0510 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
0511 {
0512 struct exfat_sb_info *sbi = EXFAT_SB(sb);
0513 struct exfat_inode_info *info;
0514 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
0515 struct inode *inode = NULL;
0516
0517 spin_lock(&sbi->inode_hash_lock);
0518 hlist_for_each_entry(info, head, i_hash_fat) {
0519 WARN_ON(info->vfs_inode.i_sb != sb);
0520
0521 if (i_pos != info->i_pos)
0522 continue;
0523 inode = igrab(&info->vfs_inode);
0524 if (inode)
0525 break;
0526 }
0527 spin_unlock(&sbi->inode_hash_lock);
0528 return inode;
0529 }
0530
0531
0532 static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
0533 {
0534 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
0535 struct exfat_inode_info *ei = EXFAT_I(inode);
0536 loff_t size = info->size;
0537
0538 ei->dir = info->dir;
0539 ei->entry = info->entry;
0540 ei->attr = info->attr;
0541 ei->start_clu = info->start_clu;
0542 ei->flags = info->flags;
0543 ei->type = info->type;
0544
0545 ei->version = 0;
0546 ei->hint_stat.eidx = 0;
0547 ei->hint_stat.clu = info->start_clu;
0548 ei->hint_femp.eidx = EXFAT_HINT_NONE;
0549 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
0550 ei->i_pos = 0;
0551
0552 inode->i_uid = sbi->options.fs_uid;
0553 inode->i_gid = sbi->options.fs_gid;
0554 inode_inc_iversion(inode);
0555 inode->i_generation = prandom_u32();
0556
0557 if (info->attr & ATTR_SUBDIR) {
0558 inode->i_generation &= ~1;
0559 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
0560 inode->i_op = &exfat_dir_inode_operations;
0561 inode->i_fop = &exfat_dir_operations;
0562 set_nlink(inode, info->num_subdirs);
0563 } else {
0564 inode->i_generation |= 1;
0565 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
0566 inode->i_op = &exfat_file_inode_operations;
0567 inode->i_fop = &exfat_file_operations;
0568 inode->i_mapping->a_ops = &exfat_aops;
0569 inode->i_mapping->nrpages = 0;
0570 }
0571
0572 i_size_write(inode, size);
0573
0574
0575 if (size & (inode->i_sb->s_blocksize - 1)) {
0576 size |= (inode->i_sb->s_blocksize - 1);
0577 size++;
0578 }
0579
0580 ei->i_size_aligned = size;
0581 ei->i_size_ondisk = size;
0582
0583 exfat_save_attr(inode, info->attr);
0584
0585 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >>
0586 inode->i_blkbits;
0587 inode->i_mtime = info->mtime;
0588 inode->i_ctime = info->mtime;
0589 ei->i_crtime = info->crtime;
0590 inode->i_atime = info->atime;
0591
0592 return 0;
0593 }
0594
0595 struct inode *exfat_build_inode(struct super_block *sb,
0596 struct exfat_dir_entry *info, loff_t i_pos)
0597 {
0598 struct inode *inode;
0599 int err;
0600
0601 inode = exfat_iget(sb, i_pos);
0602 if (inode)
0603 goto out;
0604 inode = new_inode(sb);
0605 if (!inode) {
0606 inode = ERR_PTR(-ENOMEM);
0607 goto out;
0608 }
0609 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
0610 inode_set_iversion(inode, 1);
0611 err = exfat_fill_inode(inode, info);
0612 if (err) {
0613 iput(inode);
0614 inode = ERR_PTR(err);
0615 goto out;
0616 }
0617 exfat_hash_inode(inode, i_pos);
0618 insert_inode_hash(inode);
0619 out:
0620 return inode;
0621 }
0622
0623 void exfat_evict_inode(struct inode *inode)
0624 {
0625 truncate_inode_pages(&inode->i_data, 0);
0626
0627 if (!inode->i_nlink) {
0628 i_size_write(inode, 0);
0629 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
0630 __exfat_truncate(inode, 0);
0631 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
0632 }
0633
0634 invalidate_inode_buffers(inode);
0635 clear_inode(inode);
0636 exfat_cache_inval_inode(inode);
0637 exfat_unhash_inode(inode);
0638 }