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