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