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