Linux 6.13-rc4
[linux.git] / fs / exfat / inode.c
blob96952d4acb500f8e103571beeb43a9840858bd08
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
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>
17 #include "exfat_raw.h"
18 #include "exfat_fs.h"
20 int __exfat_write_inode(struct inode *inode, int sync)
22 unsigned long long on_disk_size;
23 struct exfat_dentry *ep, *ep2;
24 struct exfat_entry_set_cache es;
25 struct super_block *sb = inode->i_sb;
26 struct exfat_sb_info *sbi = EXFAT_SB(sb);
27 struct exfat_inode_info *ei = EXFAT_I(inode);
28 bool is_dir = (ei->type == TYPE_DIR) ? true : false;
29 struct timespec64 ts;
31 if (inode->i_ino == EXFAT_ROOT_INO)
32 return 0;
35 * If the inode is already unlinked, there is no need for updating it.
37 if (ei->dir.dir == DIR_DELETED)
38 return 0;
40 if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
41 return 0;
43 exfat_set_volume_dirty(sb);
45 /* get the directory entry of given file or directory */
46 if (exfat_get_dentry_set_by_ei(&es, sb, ei))
47 return -EIO;
48 ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
49 ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
51 ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode));
53 /* set FILE_INFO structure using the acquired struct exfat_dentry */
54 exfat_set_entry_time(sbi, &ei->i_crtime,
55 &ep->dentry.file.create_tz,
56 &ep->dentry.file.create_time,
57 &ep->dentry.file.create_date,
58 &ep->dentry.file.create_time_cs);
59 ts = inode_get_mtime(inode);
60 exfat_set_entry_time(sbi, &ts,
61 &ep->dentry.file.modify_tz,
62 &ep->dentry.file.modify_time,
63 &ep->dentry.file.modify_date,
64 &ep->dentry.file.modify_time_cs);
65 ts = inode_get_atime(inode);
66 exfat_set_entry_time(sbi, &ts,
67 &ep->dentry.file.access_tz,
68 &ep->dentry.file.access_time,
69 &ep->dentry.file.access_date,
70 NULL);
72 /* File size should be zero if there is no cluster allocated */
73 on_disk_size = i_size_read(inode);
75 if (ei->start_clu == EXFAT_EOF_CLUSTER)
76 on_disk_size = 0;
78 ep2->dentry.stream.size = cpu_to_le64(on_disk_size);
80 * mmap write does not use exfat_write_end(), valid_size may be
81 * extended to the sector-aligned length in exfat_get_block().
82 * So we need to fixup valid_size to the writren length.
84 if (on_disk_size < ei->valid_size)
85 ep2->dentry.stream.valid_size = ep2->dentry.stream.size;
86 else
87 ep2->dentry.stream.valid_size = cpu_to_le64(ei->valid_size);
89 if (on_disk_size) {
90 ep2->dentry.stream.flags = ei->flags;
91 ep2->dentry.stream.start_clu = cpu_to_le32(ei->start_clu);
92 } else {
93 ep2->dentry.stream.flags = ALLOC_FAT_CHAIN;
94 ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER;
97 exfat_update_dir_chksum(&es);
98 return exfat_put_dentry_set(&es, sync);
101 int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
103 int ret;
105 if (unlikely(exfat_forced_shutdown(inode->i_sb)))
106 return -EIO;
108 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
109 ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
110 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
112 return ret;
115 void exfat_sync_inode(struct inode *inode)
117 lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
118 __exfat_write_inode(inode, 1);
122 * Input: inode, (logical) clu_offset, target allocation area
123 * Output: errcode, cluster number
124 * *clu = (~0), if it's unable to allocate a new cluster
126 static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
127 unsigned int *clu, int create)
129 int ret;
130 unsigned int last_clu;
131 struct exfat_chain new_clu;
132 struct super_block *sb = inode->i_sb;
133 struct exfat_sb_info *sbi = EXFAT_SB(sb);
134 struct exfat_inode_info *ei = EXFAT_I(inode);
135 unsigned int local_clu_offset = clu_offset;
136 unsigned int num_to_be_allocated = 0, num_clusters;
138 num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
140 if (clu_offset >= num_clusters)
141 num_to_be_allocated = clu_offset - num_clusters + 1;
143 if (!create && (num_to_be_allocated > 0)) {
144 *clu = EXFAT_EOF_CLUSTER;
145 return 0;
148 *clu = last_clu = ei->start_clu;
150 if (ei->flags == ALLOC_NO_FAT_CHAIN) {
151 if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
152 last_clu += clu_offset - 1;
154 if (clu_offset == num_clusters)
155 *clu = EXFAT_EOF_CLUSTER;
156 else
157 *clu += clu_offset;
159 } else if (ei->type == TYPE_FILE) {
160 unsigned int fclus = 0;
161 int err = exfat_get_cluster(inode, clu_offset,
162 &fclus, clu, &last_clu, 1);
163 if (err)
164 return -EIO;
166 clu_offset -= fclus;
167 } else {
168 /* hint information */
169 if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
170 ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
171 clu_offset -= ei->hint_bmap.off;
172 /* hint_bmap.clu should be valid */
173 WARN_ON(ei->hint_bmap.clu < 2);
174 *clu = ei->hint_bmap.clu;
177 while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
178 last_clu = *clu;
179 if (exfat_get_next_cluster(sb, clu))
180 return -EIO;
181 clu_offset--;
185 if (*clu == EXFAT_EOF_CLUSTER) {
186 exfat_set_volume_dirty(sb);
188 new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
189 EXFAT_EOF_CLUSTER : last_clu + 1;
190 new_clu.size = 0;
191 new_clu.flags = ei->flags;
193 /* allocate a cluster */
194 if (num_to_be_allocated < 1) {
195 /* Broken FAT (i_sze > allocated FAT) */
196 exfat_fs_error(sb, "broken FAT chain.");
197 return -EIO;
200 ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
201 inode_needs_sync(inode));
202 if (ret)
203 return ret;
205 if (new_clu.dir == EXFAT_EOF_CLUSTER ||
206 new_clu.dir == EXFAT_FREE_CLUSTER) {
207 exfat_fs_error(sb,
208 "bogus cluster new allocated (last_clu : %u, new_clu : %u)",
209 last_clu, new_clu.dir);
210 return -EIO;
213 /* append to the FAT chain */
214 if (last_clu == EXFAT_EOF_CLUSTER) {
215 if (new_clu.flags == ALLOC_FAT_CHAIN)
216 ei->flags = ALLOC_FAT_CHAIN;
217 ei->start_clu = new_clu.dir;
218 } else {
219 if (new_clu.flags != ei->flags) {
220 /* no-fat-chain bit is disabled,
221 * so fat-chain should be synced with
222 * alloc-bitmap
224 exfat_chain_cont_cluster(sb, ei->start_clu,
225 num_clusters);
226 ei->flags = ALLOC_FAT_CHAIN;
228 if (new_clu.flags == ALLOC_FAT_CHAIN)
229 if (exfat_ent_set(sb, last_clu, new_clu.dir))
230 return -EIO;
233 num_clusters += num_to_be_allocated;
234 *clu = new_clu.dir;
236 inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;
239 * Move *clu pointer along FAT chains (hole care) because the
240 * caller of this function expect *clu to be the last cluster.
241 * This only works when num_to_be_allocated >= 2,
242 * *clu = (the first cluster of the allocated chain) =>
243 * (the last cluster of ...)
245 if (ei->flags == ALLOC_NO_FAT_CHAIN) {
246 *clu += num_to_be_allocated - 1;
247 } else {
248 while (num_to_be_allocated > 1) {
249 if (exfat_get_next_cluster(sb, clu))
250 return -EIO;
251 num_to_be_allocated--;
257 /* hint information */
258 ei->hint_bmap.off = local_clu_offset;
259 ei->hint_bmap.clu = *clu;
261 return 0;
264 static int exfat_get_block(struct inode *inode, sector_t iblock,
265 struct buffer_head *bh_result, int create)
267 struct exfat_inode_info *ei = EXFAT_I(inode);
268 struct super_block *sb = inode->i_sb;
269 struct exfat_sb_info *sbi = EXFAT_SB(sb);
270 unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
271 int err = 0;
272 unsigned long mapped_blocks = 0;
273 unsigned int cluster, sec_offset;
274 sector_t last_block;
275 sector_t phys = 0;
276 sector_t valid_blks;
278 mutex_lock(&sbi->s_lock);
279 last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb);
280 if (iblock >= last_block && !create)
281 goto done;
283 /* Is this block already allocated? */
284 err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
285 &cluster, create);
286 if (err) {
287 if (err != -ENOSPC)
288 exfat_fs_error_ratelimit(sb,
289 "failed to bmap (inode : %p iblock : %llu, err : %d)",
290 inode, (unsigned long long)iblock, err);
291 goto unlock_ret;
294 if (cluster == EXFAT_EOF_CLUSTER)
295 goto done;
297 /* sector offset in cluster */
298 sec_offset = iblock & (sbi->sect_per_clus - 1);
300 phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
301 mapped_blocks = sbi->sect_per_clus - sec_offset;
302 max_blocks = min(mapped_blocks, max_blocks);
304 map_bh(bh_result, sb, phys);
305 if (buffer_delay(bh_result))
306 clear_buffer_delay(bh_result);
308 if (create) {
309 valid_blks = EXFAT_B_TO_BLK_ROUND_UP(ei->valid_size, sb);
311 if (iblock + max_blocks < valid_blks) {
312 /* The range has been written, map it */
313 goto done;
314 } else if (iblock < valid_blks) {
316 * The range has been partially written,
317 * map the written part.
319 max_blocks = valid_blks - iblock;
320 goto done;
323 /* The area has not been written, map and mark as new. */
324 set_buffer_new(bh_result);
326 ei->valid_size = EXFAT_BLK_TO_B(iblock + max_blocks, sb);
327 mark_inode_dirty(inode);
328 } else {
329 valid_blks = EXFAT_B_TO_BLK(ei->valid_size, sb);
331 if (iblock + max_blocks < valid_blks) {
332 /* The range has been written, map it */
333 goto done;
334 } else if (iblock < valid_blks) {
336 * The area has been partially written,
337 * map the written part.
339 max_blocks = valid_blks - iblock;
340 goto done;
341 } else if (iblock == valid_blks &&
342 (ei->valid_size & (sb->s_blocksize - 1))) {
344 * The block has been partially written,
345 * zero the unwritten part and map the block.
347 loff_t size, off, pos;
349 max_blocks = 1;
352 * For direct read, the unwritten part will be zeroed in
353 * exfat_direct_IO()
355 if (!bh_result->b_folio)
356 goto done;
358 pos = EXFAT_BLK_TO_B(iblock, sb);
359 size = ei->valid_size - pos;
360 off = pos & (PAGE_SIZE - 1);
362 folio_set_bh(bh_result, bh_result->b_folio, off);
363 err = bh_read(bh_result, 0);
364 if (err < 0)
365 goto unlock_ret;
367 folio_zero_segment(bh_result->b_folio, off + size,
368 off + sb->s_blocksize);
369 } else {
371 * The range has not been written, clear the mapped flag
372 * to only zero the cache and do not read from disk.
374 clear_buffer_mapped(bh_result);
377 done:
378 bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
379 unlock_ret:
380 mutex_unlock(&sbi->s_lock);
381 return err;
384 static int exfat_read_folio(struct file *file, struct folio *folio)
386 return mpage_read_folio(folio, exfat_get_block);
389 static void exfat_readahead(struct readahead_control *rac)
391 struct address_space *mapping = rac->mapping;
392 struct inode *inode = mapping->host;
393 struct exfat_inode_info *ei = EXFAT_I(inode);
394 loff_t pos = readahead_pos(rac);
396 /* Range cross valid_size, read it page by page. */
397 if (ei->valid_size < i_size_read(inode) &&
398 pos <= ei->valid_size &&
399 ei->valid_size < pos + readahead_length(rac))
400 return;
402 mpage_readahead(rac, exfat_get_block);
405 static int exfat_writepages(struct address_space *mapping,
406 struct writeback_control *wbc)
408 if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
409 return -EIO;
411 return mpage_writepages(mapping, wbc, exfat_get_block);
414 static void exfat_write_failed(struct address_space *mapping, loff_t to)
416 struct inode *inode = mapping->host;
418 if (to > i_size_read(inode)) {
419 truncate_pagecache(inode, i_size_read(inode));
420 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
421 exfat_truncate(inode);
425 static int exfat_write_begin(struct file *file, struct address_space *mapping,
426 loff_t pos, unsigned int len,
427 struct folio **foliop, void **fsdata)
429 int ret;
431 if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
432 return -EIO;
434 ret = block_write_begin(mapping, pos, len, foliop, exfat_get_block);
436 if (ret < 0)
437 exfat_write_failed(mapping, pos+len);
439 return ret;
442 static int exfat_write_end(struct file *file, struct address_space *mapping,
443 loff_t pos, unsigned int len, unsigned int copied,
444 struct folio *folio, void *fsdata)
446 struct inode *inode = mapping->host;
447 struct exfat_inode_info *ei = EXFAT_I(inode);
448 int err;
450 err = generic_write_end(file, mapping, pos, len, copied, folio, fsdata);
451 if (err < len)
452 exfat_write_failed(mapping, pos+len);
454 if (!(err < 0) && pos + err > ei->valid_size) {
455 ei->valid_size = pos + err;
456 mark_inode_dirty(inode);
459 if (!(err < 0) && !(ei->attr & EXFAT_ATTR_ARCHIVE)) {
460 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
461 ei->attr |= EXFAT_ATTR_ARCHIVE;
462 mark_inode_dirty(inode);
465 return err;
468 static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
470 struct address_space *mapping = iocb->ki_filp->f_mapping;
471 struct inode *inode = mapping->host;
472 struct exfat_inode_info *ei = EXFAT_I(inode);
473 loff_t pos = iocb->ki_pos;
474 loff_t size = pos + iov_iter_count(iter);
475 int rw = iov_iter_rw(iter);
476 ssize_t ret;
479 * Need to use the DIO_LOCKING for avoiding the race
480 * condition of exfat_get_block() and ->truncate().
482 ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
483 if (ret < 0) {
484 if (rw == WRITE && ret != -EIOCBQUEUED)
485 exfat_write_failed(mapping, size);
487 return ret;
488 } else
489 size = pos + ret;
491 if (rw == WRITE) {
493 * If the block had been partially written before this write,
494 * ->valid_size will not be updated in exfat_get_block(),
495 * update it here.
497 if (ei->valid_size < size) {
498 ei->valid_size = size;
499 mark_inode_dirty(inode);
501 } else if (pos < ei->valid_size && ei->valid_size < size) {
502 /* zero the unwritten part in the partially written block */
503 iov_iter_revert(iter, size - ei->valid_size);
504 iov_iter_zero(size - ei->valid_size, iter);
507 return ret;
510 static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
512 sector_t blocknr;
514 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
515 down_read(&EXFAT_I(mapping->host)->truncate_lock);
516 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
517 up_read(&EXFAT_I(mapping->host)->truncate_lock);
518 return blocknr;
522 * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
523 * up to the end of the block which corresponds to `from'.
524 * This is required during truncate to physically zeroout the tail end
525 * of that block so it doesn't yield old data if the file is later grown.
526 * Also, avoid causing failure from fsx for cases of "data past EOF"
528 int exfat_block_truncate_page(struct inode *inode, loff_t from)
530 return block_truncate_page(inode->i_mapping, from, exfat_get_block);
533 static const struct address_space_operations exfat_aops = {
534 .dirty_folio = block_dirty_folio,
535 .invalidate_folio = block_invalidate_folio,
536 .read_folio = exfat_read_folio,
537 .readahead = exfat_readahead,
538 .writepages = exfat_writepages,
539 .write_begin = exfat_write_begin,
540 .write_end = exfat_write_end,
541 .direct_IO = exfat_direct_IO,
542 .bmap = exfat_aop_bmap,
543 .migrate_folio = buffer_migrate_folio,
546 static inline unsigned long exfat_hash(loff_t i_pos)
548 return hash_32(i_pos, EXFAT_HASH_BITS);
551 void exfat_hash_inode(struct inode *inode, loff_t i_pos)
553 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
554 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
556 spin_lock(&sbi->inode_hash_lock);
557 EXFAT_I(inode)->i_pos = i_pos;
558 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
559 spin_unlock(&sbi->inode_hash_lock);
562 void exfat_unhash_inode(struct inode *inode)
564 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
566 spin_lock(&sbi->inode_hash_lock);
567 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
568 EXFAT_I(inode)->i_pos = 0;
569 spin_unlock(&sbi->inode_hash_lock);
572 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
574 struct exfat_sb_info *sbi = EXFAT_SB(sb);
575 struct exfat_inode_info *info;
576 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
577 struct inode *inode = NULL;
579 spin_lock(&sbi->inode_hash_lock);
580 hlist_for_each_entry(info, head, i_hash_fat) {
581 WARN_ON(info->vfs_inode.i_sb != sb);
583 if (i_pos != info->i_pos)
584 continue;
585 inode = igrab(&info->vfs_inode);
586 if (inode)
587 break;
589 spin_unlock(&sbi->inode_hash_lock);
590 return inode;
593 /* doesn't deal with root inode */
594 static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
596 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
597 struct exfat_inode_info *ei = EXFAT_I(inode);
598 loff_t size = info->size;
600 ei->dir = info->dir;
601 ei->entry = info->entry;
602 ei->attr = info->attr;
603 ei->start_clu = info->start_clu;
604 ei->flags = info->flags;
605 ei->type = info->type;
606 ei->valid_size = info->valid_size;
608 ei->version = 0;
609 ei->hint_stat.eidx = 0;
610 ei->hint_stat.clu = info->start_clu;
611 ei->hint_femp.eidx = EXFAT_HINT_NONE;
612 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
613 ei->i_pos = 0;
615 inode->i_uid = sbi->options.fs_uid;
616 inode->i_gid = sbi->options.fs_gid;
617 inode_inc_iversion(inode);
618 inode->i_generation = get_random_u32();
620 if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
621 inode->i_generation &= ~1;
622 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
623 inode->i_op = &exfat_dir_inode_operations;
624 inode->i_fop = &exfat_dir_operations;
625 set_nlink(inode, info->num_subdirs);
626 } else { /* regular file */
627 inode->i_generation |= 1;
628 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
629 inode->i_op = &exfat_file_inode_operations;
630 inode->i_fop = &exfat_file_operations;
631 inode->i_mapping->a_ops = &exfat_aops;
632 inode->i_mapping->nrpages = 0;
635 i_size_write(inode, size);
637 exfat_save_attr(inode, info->attr);
639 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
640 inode_set_mtime_to_ts(inode, info->mtime);
641 inode_set_ctime_to_ts(inode, info->mtime);
642 ei->i_crtime = info->crtime;
643 inode_set_atime_to_ts(inode, info->atime);
645 return 0;
648 struct inode *exfat_build_inode(struct super_block *sb,
649 struct exfat_dir_entry *info, loff_t i_pos)
651 struct inode *inode;
652 int err;
654 inode = exfat_iget(sb, i_pos);
655 if (inode)
656 goto out;
657 inode = new_inode(sb);
658 if (!inode) {
659 inode = ERR_PTR(-ENOMEM);
660 goto out;
662 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
663 inode_set_iversion(inode, 1);
664 err = exfat_fill_inode(inode, info);
665 if (err) {
666 iput(inode);
667 inode = ERR_PTR(err);
668 goto out;
670 exfat_hash_inode(inode, i_pos);
671 insert_inode_hash(inode);
672 out:
673 return inode;
676 void exfat_evict_inode(struct inode *inode)
678 truncate_inode_pages(&inode->i_data, 0);
680 if (!inode->i_nlink) {
681 i_size_write(inode, 0);
682 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
683 __exfat_truncate(inode);
684 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
687 invalidate_inode_buffers(inode);
688 clear_inode(inode);
689 exfat_cache_inval_inode(inode);
690 exfat_unhash_inode(inode);