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_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);
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 	if (unlikely(exfat_forced_shutdown(inode->i_sb)))
106 		return -EIO;
107 
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);
111 
112 	return ret;
113 }
114 
exfat_sync_inode(struct inode * inode)115 void exfat_sync_inode(struct inode *inode)
116 {
117 	lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
118 	__exfat_write_inode(inode, 1);
119 }
120 
121 /*
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
125  */
exfat_map_cluster(struct inode * inode,unsigned int clu_offset,unsigned int * clu,int create)126 static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
127 		unsigned int *clu, int create)
128 {
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;
137 
138 	num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
139 
140 	if (clu_offset >= num_clusters)
141 		num_to_be_allocated = clu_offset - num_clusters + 1;
142 
143 	if (!create && (num_to_be_allocated > 0)) {
144 		*clu = EXFAT_EOF_CLUSTER;
145 		return 0;
146 	}
147 
148 	*clu = last_clu = ei->start_clu;
149 
150 	if (ei->flags == ALLOC_NO_FAT_CHAIN) {
151 		if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
152 			last_clu += clu_offset - 1;
153 
154 			if (clu_offset == num_clusters)
155 				*clu = EXFAT_EOF_CLUSTER;
156 			else
157 				*clu += clu_offset;
158 		}
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;
165 
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;
175 		}
176 
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--;
182 		}
183 	}
184 
185 	if (*clu == EXFAT_EOF_CLUSTER) {
186 		exfat_set_volume_dirty(sb);
187 
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;
192 
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;
198 		}
199 
200 		ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
201 				inode_needs_sync(inode));
202 		if (ret)
203 			return ret;
204 
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;
211 		}
212 
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
223 				 */
224 				exfat_chain_cont_cluster(sb, ei->start_clu,
225 					num_clusters);
226 				ei->flags = ALLOC_FAT_CHAIN;
227 			}
228 			if (new_clu.flags == ALLOC_FAT_CHAIN)
229 				if (exfat_ent_set(sb, last_clu, new_clu.dir))
230 					return -EIO;
231 		}
232 
233 		num_clusters += num_to_be_allocated;
234 		*clu = new_clu.dir;
235 
236 		inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;
237 
238 		/*
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 ...)
244 		 */
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--;
252 			}
253 		}
254 
255 	}
256 
257 	/* hint information */
258 	ei->hint_bmap.off = local_clu_offset;
259 	ei->hint_bmap.clu = *clu;
260 
261 	return 0;
262 }
263 
exfat_get_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)264 static int exfat_get_block(struct inode *inode, sector_t iblock,
265 		struct buffer_head *bh_result, int create)
266 {
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;
277 
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;
282 
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;
292 	}
293 
294 	if (cluster == EXFAT_EOF_CLUSTER)
295 		goto done;
296 
297 	/* sector offset in cluster */
298 	sec_offset = iblock & (sbi->sect_per_clus - 1);
299 
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);
303 
304 	map_bh(bh_result, sb, phys);
305 	if (buffer_delay(bh_result))
306 		clear_buffer_delay(bh_result);
307 
308 	if (create) {
309 		valid_blks = EXFAT_B_TO_BLK_ROUND_UP(ei->valid_size, sb);
310 
311 		if (iblock + max_blocks < valid_blks) {
312 			/* The range has been written, map it */
313 			goto done;
314 		} else if (iblock < valid_blks) {
315 			/*
316 			 * The range has been partially written,
317 			 * map the written part.
318 			 */
319 			max_blocks = valid_blks - iblock;
320 			goto done;
321 		}
322 
323 		/* The area has not been written, map and mark as new. */
324 		set_buffer_new(bh_result);
325 
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);
330 
331 		if (iblock + max_blocks < valid_blks) {
332 			/* The range has been written, map it */
333 			goto done;
334 		} else if (iblock < valid_blks) {
335 			/*
336 			 * The area has been partially written,
337 			 * map the written part.
338 			 */
339 			max_blocks = valid_blks - iblock;
340 			goto done;
341 		} else if (iblock == valid_blks &&
342 			   (ei->valid_size & (sb->s_blocksize - 1))) {
343 			/*
344 			 * The block has been partially written,
345 			 * zero the unwritten part and map the block.
346 			 */
347 			loff_t size, pos;
348 			void *addr;
349 
350 			max_blocks = 1;
351 
352 			/*
353 			 * For direct read, the unwritten part will be zeroed in
354 			 * exfat_direct_IO()
355 			 */
356 			if (!bh_result->b_folio)
357 				goto done;
358 
359 			/*
360 			 * No buffer_head is allocated.
361 			 * (1) bmap: It's enough to fill bh_result without I/O.
362 			 * (2) read: The unwritten part should be filled with 0
363 			 *           If a folio does not have any buffers,
364 			 *           let's returns -EAGAIN to fallback to
365 			 *           per-bh IO like block_read_full_folio().
366 			 */
367 			if (!folio_buffers(bh_result->b_folio)) {
368 				err = -EAGAIN;
369 				goto done;
370 			}
371 
372 			pos = EXFAT_BLK_TO_B(iblock, sb);
373 			size = ei->valid_size - pos;
374 			addr = folio_address(bh_result->b_folio) +
375 			       offset_in_folio(bh_result->b_folio, pos);
376 
377 			/* Check if bh->b_data points to proper addr in folio */
378 			if (bh_result->b_data != addr) {
379 				exfat_fs_error_ratelimit(sb,
380 					"b_data(%p) != folio_addr(%p)",
381 					bh_result->b_data, addr);
382 				err = -EINVAL;
383 				goto done;
384 			}
385 
386 			/* Read a block */
387 			err = bh_read(bh_result, 0);
388 			if (err < 0)
389 				goto done;
390 
391 			/* Zero unwritten part of a block */
392 			memset(bh_result->b_data + size, 0,
393 			       bh_result->b_size - size);
394 
395 			err = 0;
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 	if (err < 0)
407 		clear_buffer_mapped(bh_result);
408 unlock_ret:
409 	mutex_unlock(&sbi->s_lock);
410 	return err;
411 }
412 
exfat_read_folio(struct file * file,struct folio * folio)413 static int exfat_read_folio(struct file *file, struct folio *folio)
414 {
415 	return mpage_read_folio(folio, exfat_get_block);
416 }
417 
exfat_readahead(struct readahead_control * rac)418 static void exfat_readahead(struct readahead_control *rac)
419 {
420 	struct address_space *mapping = rac->mapping;
421 	struct inode *inode = mapping->host;
422 	struct exfat_inode_info *ei = EXFAT_I(inode);
423 	loff_t pos = readahead_pos(rac);
424 
425 	/* Range cross valid_size, read it page by page. */
426 	if (ei->valid_size < i_size_read(inode) &&
427 	    pos <= ei->valid_size &&
428 	    ei->valid_size < pos + readahead_length(rac))
429 		return;
430 
431 	mpage_readahead(rac, exfat_get_block);
432 }
433 
exfat_writepages(struct address_space * mapping,struct writeback_control * wbc)434 static int exfat_writepages(struct address_space *mapping,
435 		struct writeback_control *wbc)
436 {
437 	if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
438 		return -EIO;
439 
440 	return mpage_writepages(mapping, wbc, exfat_get_block);
441 }
442 
exfat_write_failed(struct address_space * mapping,loff_t to)443 static void exfat_write_failed(struct address_space *mapping, loff_t to)
444 {
445 	struct inode *inode = mapping->host;
446 
447 	if (to > i_size_read(inode)) {
448 		truncate_pagecache(inode, i_size_read(inode));
449 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
450 		exfat_truncate(inode);
451 	}
452 }
453 
exfat_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned int len,struct folio ** foliop,void ** fsdata)454 static int exfat_write_begin(struct file *file, struct address_space *mapping,
455 		loff_t pos, unsigned int len,
456 		struct folio **foliop, void **fsdata)
457 {
458 	int ret;
459 
460 	if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
461 		return -EIO;
462 
463 	ret = block_write_begin(mapping, pos, len, foliop, exfat_get_block);
464 
465 	if (ret < 0)
466 		exfat_write_failed(mapping, pos+len);
467 
468 	return ret;
469 }
470 
exfat_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned int len,unsigned int copied,struct folio * folio,void * fsdata)471 static int exfat_write_end(struct file *file, struct address_space *mapping,
472 		loff_t pos, unsigned int len, unsigned int copied,
473 		struct folio *folio, void *fsdata)
474 {
475 	struct inode *inode = mapping->host;
476 	struct exfat_inode_info *ei = EXFAT_I(inode);
477 	int err;
478 
479 	err = generic_write_end(file, mapping, pos, len, copied, folio, fsdata);
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 	/*
508 	 * Need to use the DIO_LOCKING for avoiding the race
509 	 * condition of exfat_get_block() and ->truncate().
510 	 */
511 	ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
512 	if (ret < 0) {
513 		if (rw == WRITE && ret != -EIOCBQUEUED)
514 			exfat_write_failed(mapping, size);
515 
516 		return ret;
517 	} else
518 		size = pos + ret;
519 
520 	if (rw == WRITE) {
521 		/*
522 		 * If the block had been partially written before this write,
523 		 * ->valid_size will not be updated in exfat_get_block(),
524 		 * update it here.
525 		 */
526 		if (ei->valid_size < size) {
527 			ei->valid_size = size;
528 			mark_inode_dirty(inode);
529 		}
530 	} else if (pos < ei->valid_size && ei->valid_size < size) {
531 		/* zero the unwritten part in the partially written block */
532 		iov_iter_revert(iter, size - ei->valid_size);
533 		iov_iter_zero(size - ei->valid_size, iter);
534 	}
535 
536 	return ret;
537 }
538 
exfat_aop_bmap(struct address_space * mapping,sector_t block)539 static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
540 {
541 	sector_t blocknr;
542 
543 	/* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
544 	down_read(&EXFAT_I(mapping->host)->truncate_lock);
545 	blocknr = generic_block_bmap(mapping, block, exfat_get_block);
546 	up_read(&EXFAT_I(mapping->host)->truncate_lock);
547 	return blocknr;
548 }
549 
550 /*
551  * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
552  * up to the end of the block which corresponds to `from'.
553  * This is required during truncate to physically zeroout the tail end
554  * of that block so it doesn't yield old data if the file is later grown.
555  * Also, avoid causing failure from fsx for cases of "data past EOF"
556  */
exfat_block_truncate_page(struct inode * inode,loff_t from)557 int exfat_block_truncate_page(struct inode *inode, loff_t from)
558 {
559 	return block_truncate_page(inode->i_mapping, from, exfat_get_block);
560 }
561 
562 static const struct address_space_operations exfat_aops = {
563 	.dirty_folio	= block_dirty_folio,
564 	.invalidate_folio = block_invalidate_folio,
565 	.read_folio	= exfat_read_folio,
566 	.readahead	= exfat_readahead,
567 	.writepages	= exfat_writepages,
568 	.write_begin	= exfat_write_begin,
569 	.write_end	= exfat_write_end,
570 	.direct_IO	= exfat_direct_IO,
571 	.bmap		= exfat_aop_bmap,
572 	.migrate_folio	= buffer_migrate_folio,
573 };
574 
exfat_hash(loff_t i_pos)575 static inline unsigned long exfat_hash(loff_t i_pos)
576 {
577 	return hash_32(i_pos, EXFAT_HASH_BITS);
578 }
579 
exfat_hash_inode(struct inode * inode,loff_t i_pos)580 void exfat_hash_inode(struct inode *inode, loff_t i_pos)
581 {
582 	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
583 	struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
584 
585 	spin_lock(&sbi->inode_hash_lock);
586 	EXFAT_I(inode)->i_pos = i_pos;
587 	hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
588 	spin_unlock(&sbi->inode_hash_lock);
589 }
590 
exfat_unhash_inode(struct inode * inode)591 void exfat_unhash_inode(struct inode *inode)
592 {
593 	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
594 
595 	spin_lock(&sbi->inode_hash_lock);
596 	hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
597 	EXFAT_I(inode)->i_pos = 0;
598 	spin_unlock(&sbi->inode_hash_lock);
599 }
600 
exfat_iget(struct super_block * sb,loff_t i_pos)601 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
602 {
603 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
604 	struct exfat_inode_info *info;
605 	struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
606 	struct inode *inode = NULL;
607 
608 	spin_lock(&sbi->inode_hash_lock);
609 	hlist_for_each_entry(info, head, i_hash_fat) {
610 		WARN_ON(info->vfs_inode.i_sb != sb);
611 
612 		if (i_pos != info->i_pos)
613 			continue;
614 		inode = igrab(&info->vfs_inode);
615 		if (inode)
616 			break;
617 	}
618 	spin_unlock(&sbi->inode_hash_lock);
619 	return inode;
620 }
621 
622 /* doesn't deal with root inode */
exfat_fill_inode(struct inode * inode,struct exfat_dir_entry * info)623 static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
624 {
625 	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
626 	struct exfat_inode_info *ei = EXFAT_I(inode);
627 	loff_t size = info->size;
628 
629 	ei->dir = info->dir;
630 	ei->entry = info->entry;
631 	ei->attr = info->attr;
632 	ei->start_clu = info->start_clu;
633 	ei->flags = info->flags;
634 	ei->type = info->type;
635 	ei->valid_size = info->valid_size;
636 
637 	ei->version = 0;
638 	ei->hint_stat.eidx = 0;
639 	ei->hint_stat.clu = info->start_clu;
640 	ei->hint_femp.eidx = EXFAT_HINT_NONE;
641 	ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
642 	ei->i_pos = 0;
643 
644 	inode->i_uid = sbi->options.fs_uid;
645 	inode->i_gid = sbi->options.fs_gid;
646 	inode_inc_iversion(inode);
647 	inode->i_generation = get_random_u32();
648 
649 	if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
650 		inode->i_generation &= ~1;
651 		inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
652 		inode->i_op = &exfat_dir_inode_operations;
653 		inode->i_fop = &exfat_dir_operations;
654 		set_nlink(inode, info->num_subdirs);
655 	} else { /* regular file */
656 		inode->i_generation |= 1;
657 		inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
658 		inode->i_op = &exfat_file_inode_operations;
659 		inode->i_fop = &exfat_file_operations;
660 		inode->i_mapping->a_ops = &exfat_aops;
661 		inode->i_mapping->nrpages = 0;
662 	}
663 
664 	i_size_write(inode, size);
665 
666 	exfat_save_attr(inode, info->attr);
667 
668 	inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
669 	inode_set_mtime_to_ts(inode, info->mtime);
670 	inode_set_ctime_to_ts(inode, info->mtime);
671 	ei->i_crtime = info->crtime;
672 	inode_set_atime_to_ts(inode, info->atime);
673 
674 	return 0;
675 }
676 
exfat_build_inode(struct super_block * sb,struct exfat_dir_entry * info,loff_t i_pos)677 struct inode *exfat_build_inode(struct super_block *sb,
678 		struct exfat_dir_entry *info, loff_t i_pos)
679 {
680 	struct inode *inode;
681 	int err;
682 
683 	inode = exfat_iget(sb, i_pos);
684 	if (inode)
685 		goto out;
686 	inode = new_inode(sb);
687 	if (!inode) {
688 		inode = ERR_PTR(-ENOMEM);
689 		goto out;
690 	}
691 	inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
692 	inode_set_iversion(inode, 1);
693 	err = exfat_fill_inode(inode, info);
694 	if (err) {
695 		iput(inode);
696 		inode = ERR_PTR(err);
697 		goto out;
698 	}
699 	exfat_hash_inode(inode, i_pos);
700 	insert_inode_hash(inode);
701 out:
702 	return inode;
703 }
704 
exfat_evict_inode(struct inode * inode)705 void exfat_evict_inode(struct inode *inode)
706 {
707 	truncate_inode_pages(&inode->i_data, 0);
708 
709 	if (!inode->i_nlink) {
710 		i_size_write(inode, 0);
711 		mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
712 		__exfat_truncate(inode);
713 		mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
714 	}
715 
716 	invalidate_inode_buffers(inode);
717 	clear_inode(inode);
718 	exfat_cache_inval_inode(inode);
719 	exfat_unhash_inode(inode);
720 }
721