xref: /linux/fs/exfat/inode.c (revision aec2f682d47c54ef434b2d440992626d80b1ebdc)
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 
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);
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 
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 
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  */
126 static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
127 		unsigned int *clu, unsigned int *count, 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 (*clu == EXFAT_EOF_CLUSTER) {
151 		*count = 0;
152 	} else if (ei->flags == ALLOC_NO_FAT_CHAIN) {
153 		last_clu += num_clusters - 1;
154 		if (clu_offset < num_clusters) {
155 			*clu += clu_offset;
156 			*count = min(num_clusters - clu_offset, *count);
157 		} else {
158 			*clu = EXFAT_EOF_CLUSTER;
159 			*count = 0;
160 		}
161 	} else {
162 		int err = exfat_get_cluster(inode, clu_offset,
163 				clu, count, &last_clu);
164 		if (err)
165 			return -EIO;
166 	}
167 
168 	if (*clu == EXFAT_EOF_CLUSTER) {
169 		exfat_set_volume_dirty(sb);
170 
171 		new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
172 				EXFAT_EOF_CLUSTER : last_clu + 1;
173 		new_clu.size = 0;
174 		new_clu.flags = ei->flags;
175 
176 		/* allocate a cluster */
177 		if (num_to_be_allocated < 1) {
178 			/* Broken FAT (i_sze > allocated FAT) */
179 			exfat_fs_error(sb, "broken FAT chain.");
180 			return -EIO;
181 		}
182 
183 		ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
184 				inode_needs_sync(inode));
185 		if (ret)
186 			return ret;
187 
188 		if (new_clu.dir == EXFAT_EOF_CLUSTER ||
189 		    new_clu.dir == EXFAT_FREE_CLUSTER) {
190 			exfat_fs_error(sb,
191 				"bogus cluster new allocated (last_clu : %u, new_clu : %u)",
192 				last_clu, new_clu.dir);
193 			return -EIO;
194 		}
195 
196 		/* append to the FAT chain */
197 		if (last_clu == EXFAT_EOF_CLUSTER) {
198 			if (new_clu.flags == ALLOC_FAT_CHAIN)
199 				ei->flags = ALLOC_FAT_CHAIN;
200 			ei->start_clu = new_clu.dir;
201 		} else {
202 			if (new_clu.flags != ei->flags) {
203 				/* no-fat-chain bit is disabled,
204 				 * so fat-chain should be synced with
205 				 * alloc-bitmap
206 				 */
207 				if (exfat_chain_cont_cluster(sb, ei->start_clu,
208 						num_clusters))
209 					return -EIO;
210 				ei->flags = ALLOC_FAT_CHAIN;
211 			}
212 			if (new_clu.flags == ALLOC_FAT_CHAIN)
213 				if (exfat_ent_set(sb, last_clu, new_clu.dir))
214 					return -EIO;
215 		}
216 
217 		*clu = new_clu.dir;
218 
219 		inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;
220 
221 		/*
222 		 * Move *clu pointer along FAT chains (hole care) because the
223 		 * caller of this function expect *clu to be the last cluster.
224 		 * This only works when num_to_be_allocated >= 2,
225 		 * *clu = (the first cluster of the allocated chain) =>
226 		 * (the last cluster of ...)
227 		 */
228 		if (exfat_cluster_walk(sb, clu, num_to_be_allocated - 1, ei->flags))
229 			return -EIO;
230 		*count = 1;
231 	}
232 
233 	/* hint information */
234 	ei->hint_bmap.off = local_clu_offset;
235 	ei->hint_bmap.clu = *clu;
236 
237 	return 0;
238 }
239 
240 static int exfat_get_block(struct inode *inode, sector_t iblock,
241 		struct buffer_head *bh_result, int create)
242 {
243 	struct exfat_inode_info *ei = EXFAT_I(inode);
244 	struct super_block *sb = inode->i_sb;
245 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
246 	unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
247 	int err = 0;
248 	unsigned long mapped_blocks = 0;
249 	unsigned int cluster, sec_offset, count;
250 	sector_t last_block;
251 	sector_t phys = 0;
252 	sector_t valid_blks;
253 	loff_t i_size;
254 
255 	mutex_lock(&sbi->s_lock);
256 	i_size = i_size_read(inode);
257 	last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size, sb);
258 	if (iblock >= last_block && !create)
259 		goto done;
260 
261 	/* Is this block already allocated? */
262 	count = EXFAT_B_TO_CLU_ROUND_UP(bh_result->b_size, sbi);
263 	err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
264 			&cluster, &count, create);
265 	if (err) {
266 		if (err != -ENOSPC)
267 			exfat_fs_error_ratelimit(sb,
268 				"failed to bmap (inode : %p iblock : %llu, err : %d)",
269 				inode, (unsigned long long)iblock, err);
270 		goto unlock_ret;
271 	}
272 
273 	if (cluster == EXFAT_EOF_CLUSTER)
274 		goto done;
275 
276 	/* sector offset in cluster */
277 	sec_offset = iblock & (sbi->sect_per_clus - 1);
278 
279 	phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
280 	mapped_blocks = ((unsigned long)count << sbi->sect_per_clus_bits) - sec_offset;
281 	max_blocks = min(mapped_blocks, max_blocks);
282 
283 	map_bh(bh_result, sb, phys);
284 	if (buffer_delay(bh_result))
285 		clear_buffer_delay(bh_result);
286 
287 	/*
288 	 * In most cases, we just need to set bh_result to mapped, unmapped
289 	 * or new status as follows:
290 	 *  1. i_size == valid_size
291 	 *  2. write case (create == 1)
292 	 *  3. direct_read (!bh_result->b_folio)
293 	 *     -> the unwritten part will be zeroed in exfat_direct_IO()
294 	 *
295 	 * Otherwise, in the case of buffered read, it is necessary to take
296 	 * care the last nested block if valid_size is not equal to i_size.
297 	 */
298 	if (i_size == ei->valid_size || create || !bh_result->b_folio)
299 		valid_blks = EXFAT_B_TO_BLK_ROUND_UP(ei->valid_size, sb);
300 	else
301 		valid_blks = EXFAT_B_TO_BLK(ei->valid_size, sb);
302 
303 	/* The range has been fully written, map it */
304 	if (iblock + max_blocks < valid_blks)
305 		goto done;
306 
307 	/* The range has been partially written, map the written part */
308 	if (iblock < valid_blks) {
309 		max_blocks = valid_blks - iblock;
310 		goto done;
311 	}
312 
313 	/* The area has not been written, map and mark as new for create case */
314 	if (create) {
315 		set_buffer_new(bh_result);
316 		ei->valid_size = EXFAT_BLK_TO_B(iblock + max_blocks, sb);
317 		mark_inode_dirty(inode);
318 		goto done;
319 	}
320 
321 	/*
322 	 * The area has just one block partially written.
323 	 * In that case, we should read and fill the unwritten part of
324 	 * a block with zero.
325 	 */
326 	if (bh_result->b_folio && iblock == valid_blks &&
327 	    (ei->valid_size & (sb->s_blocksize - 1))) {
328 		loff_t size, pos;
329 		void *addr;
330 
331 		max_blocks = 1;
332 
333 		/*
334 		 * No buffer_head is allocated.
335 		 * (1) bmap: It's enough to set blocknr without I/O.
336 		 * (2) read: The unwritten part should be filled with zero.
337 		 *           If a folio does not have any buffers,
338 		 *           let's returns -EAGAIN to fallback to
339 		 *           block_read_full_folio() for per-bh IO.
340 		 */
341 		if (!folio_buffers(bh_result->b_folio)) {
342 			err = -EAGAIN;
343 			goto done;
344 		}
345 
346 		pos = EXFAT_BLK_TO_B(iblock, sb);
347 		size = ei->valid_size - pos;
348 		addr = folio_address(bh_result->b_folio) +
349 			offset_in_folio(bh_result->b_folio, pos);
350 
351 		/* Check if bh->b_data points to proper addr in folio */
352 		if (bh_result->b_data != addr) {
353 			exfat_fs_error_ratelimit(sb,
354 					"b_data(%p) != folio_addr(%p)",
355 					bh_result->b_data, addr);
356 			err = -EINVAL;
357 			goto done;
358 		}
359 
360 		/* Read a block */
361 		err = bh_read(bh_result, 0);
362 		if (err < 0)
363 			goto done;
364 
365 		/* Zero unwritten part of a block */
366 		memset(bh_result->b_data + size, 0, bh_result->b_size - size);
367 		err = 0;
368 		goto done;
369 	}
370 
371 	/*
372 	 * The area has not been written, clear mapped for read/bmap cases.
373 	 * If so, it will be filled with zero without reading from disk.
374 	 */
375 	clear_buffer_mapped(bh_result);
376 done:
377 	bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
378 	if (err < 0)
379 		clear_buffer_mapped(bh_result);
380 unlock_ret:
381 	mutex_unlock(&sbi->s_lock);
382 	return err;
383 }
384 
385 static int exfat_read_folio(struct file *file, struct folio *folio)
386 {
387 	return mpage_read_folio(folio, exfat_get_block);
388 }
389 
390 static void exfat_readahead(struct readahead_control *rac)
391 {
392 	struct address_space *mapping = rac->mapping;
393 	struct inode *inode = mapping->host;
394 	struct exfat_inode_info *ei = EXFAT_I(inode);
395 	loff_t pos = readahead_pos(rac);
396 
397 	/* Range cross valid_size, read it page by page. */
398 	if (ei->valid_size < i_size_read(inode) &&
399 	    pos <= ei->valid_size &&
400 	    ei->valid_size < pos + readahead_length(rac))
401 		return;
402 
403 	mpage_readahead(rac, exfat_get_block);
404 }
405 
406 static int exfat_writepages(struct address_space *mapping,
407 		struct writeback_control *wbc)
408 {
409 	if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
410 		return -EIO;
411 
412 	return mpage_writepages(mapping, wbc, exfat_get_block);
413 }
414 
415 static void exfat_write_failed(struct address_space *mapping, loff_t to)
416 {
417 	struct inode *inode = mapping->host;
418 
419 	if (to > i_size_read(inode)) {
420 		truncate_pagecache(inode, i_size_read(inode));
421 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
422 		exfat_truncate(inode);
423 	}
424 }
425 
426 static int exfat_write_begin(const struct kiocb *iocb,
427 			     struct address_space *mapping,
428 			     loff_t pos, unsigned int len,
429 			     struct folio **foliop, void **fsdata)
430 {
431 	int ret;
432 
433 	if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
434 		return -EIO;
435 
436 	ret = block_write_begin(mapping, pos, len, foliop, exfat_get_block);
437 
438 	if (ret < 0)
439 		exfat_write_failed(mapping, pos+len);
440 
441 	return ret;
442 }
443 
444 static int exfat_write_end(const struct kiocb *iocb,
445 			   struct address_space *mapping,
446 			   loff_t pos, unsigned int len, unsigned int copied,
447 			   struct folio *folio, void *fsdata)
448 {
449 	struct inode *inode = mapping->host;
450 	struct exfat_inode_info *ei = EXFAT_I(inode);
451 	int err;
452 
453 	err = generic_write_end(iocb, mapping, pos, len, copied, folio, fsdata);
454 	if (err < len)
455 		exfat_write_failed(mapping, pos+len);
456 
457 	if (!(err < 0) && pos + err > ei->valid_size) {
458 		ei->valid_size = pos + err;
459 		mark_inode_dirty(inode);
460 	}
461 
462 	if (!(err < 0) && !(ei->attr & EXFAT_ATTR_ARCHIVE)) {
463 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
464 		ei->attr |= EXFAT_ATTR_ARCHIVE;
465 		mark_inode_dirty(inode);
466 	}
467 
468 	return err;
469 }
470 
471 static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
472 {
473 	struct address_space *mapping = iocb->ki_filp->f_mapping;
474 	struct inode *inode = mapping->host;
475 	struct exfat_inode_info *ei = EXFAT_I(inode);
476 	loff_t pos = iocb->ki_pos;
477 	loff_t size = pos + iov_iter_count(iter);
478 	int rw = iov_iter_rw(iter);
479 	ssize_t ret;
480 
481 	/*
482 	 * Need to use the DIO_LOCKING for avoiding the race
483 	 * condition of exfat_get_block() and ->truncate().
484 	 */
485 	ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
486 	if (ret < 0) {
487 		if (rw == WRITE && ret != -EIOCBQUEUED)
488 			exfat_write_failed(mapping, size);
489 
490 		return ret;
491 	}
492 
493 	size = pos + ret;
494 
495 	if (rw == WRITE) {
496 		/*
497 		 * If the block had been partially written before this write,
498 		 * ->valid_size will not be updated in exfat_get_block(),
499 		 * update it here.
500 		 */
501 		if (ei->valid_size < size) {
502 			ei->valid_size = size;
503 			mark_inode_dirty(inode);
504 		}
505 	} else if (pos < ei->valid_size && ei->valid_size < size) {
506 		/* zero the unwritten part in the partially written block */
507 		iov_iter_revert(iter, size - ei->valid_size);
508 		iov_iter_zero(size - ei->valid_size, iter);
509 	}
510 
511 	return ret;
512 }
513 
514 static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
515 {
516 	sector_t blocknr;
517 
518 	/* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
519 	down_read(&EXFAT_I(mapping->host)->truncate_lock);
520 	blocknr = generic_block_bmap(mapping, block, exfat_get_block);
521 	up_read(&EXFAT_I(mapping->host)->truncate_lock);
522 	return blocknr;
523 }
524 
525 /*
526  * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
527  * up to the end of the block which corresponds to `from'.
528  * This is required during truncate to physically zeroout the tail end
529  * of that block so it doesn't yield old data if the file is later grown.
530  * Also, avoid causing failure from fsx for cases of "data past EOF"
531  */
532 int exfat_block_truncate_page(struct inode *inode, loff_t from)
533 {
534 	return block_truncate_page(inode->i_mapping, from, exfat_get_block);
535 }
536 
537 static const struct address_space_operations exfat_aops = {
538 	.dirty_folio	= block_dirty_folio,
539 	.invalidate_folio = block_invalidate_folio,
540 	.read_folio	= exfat_read_folio,
541 	.readahead	= exfat_readahead,
542 	.writepages	= exfat_writepages,
543 	.write_begin	= exfat_write_begin,
544 	.write_end	= exfat_write_end,
545 	.direct_IO	= exfat_direct_IO,
546 	.bmap		= exfat_aop_bmap,
547 	.migrate_folio	= buffer_migrate_folio,
548 };
549 
550 static inline unsigned long exfat_hash(loff_t i_pos)
551 {
552 	return hash_32(i_pos, EXFAT_HASH_BITS);
553 }
554 
555 void exfat_hash_inode(struct inode *inode, loff_t i_pos)
556 {
557 	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
558 	struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
559 
560 	spin_lock(&sbi->inode_hash_lock);
561 	EXFAT_I(inode)->i_pos = i_pos;
562 	hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
563 	spin_unlock(&sbi->inode_hash_lock);
564 }
565 
566 void exfat_unhash_inode(struct inode *inode)
567 {
568 	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
569 
570 	spin_lock(&sbi->inode_hash_lock);
571 	hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
572 	EXFAT_I(inode)->i_pos = 0;
573 	spin_unlock(&sbi->inode_hash_lock);
574 }
575 
576 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
577 {
578 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
579 	struct exfat_inode_info *info;
580 	struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
581 	struct inode *inode = NULL;
582 
583 	spin_lock(&sbi->inode_hash_lock);
584 	hlist_for_each_entry(info, head, i_hash_fat) {
585 		WARN_ON(info->vfs_inode.i_sb != sb);
586 
587 		if (i_pos != info->i_pos)
588 			continue;
589 		inode = igrab(&info->vfs_inode);
590 		if (inode)
591 			break;
592 	}
593 	spin_unlock(&sbi->inode_hash_lock);
594 	return inode;
595 }
596 
597 /* doesn't deal with root inode */
598 static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
599 {
600 	struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
601 	struct exfat_inode_info *ei = EXFAT_I(inode);
602 	loff_t size = info->size;
603 
604 	ei->dir = info->dir;
605 	ei->entry = info->entry;
606 	ei->attr = info->attr;
607 	ei->start_clu = info->start_clu;
608 	ei->flags = info->flags;
609 	ei->type = info->type;
610 	ei->valid_size = info->valid_size;
611 
612 	ei->version = 0;
613 	ei->hint_stat.eidx = 0;
614 	ei->hint_stat.clu = info->start_clu;
615 	ei->hint_femp.eidx = EXFAT_HINT_NONE;
616 	ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
617 	ei->i_pos = 0;
618 
619 	inode->i_uid = sbi->options.fs_uid;
620 	inode->i_gid = sbi->options.fs_gid;
621 	inode_inc_iversion(inode);
622 	inode->i_generation = get_random_u32();
623 
624 	if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
625 		inode->i_generation &= ~1;
626 		inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
627 		inode->i_op = &exfat_dir_inode_operations;
628 		inode->i_fop = &exfat_dir_operations;
629 		set_nlink(inode, info->num_subdirs);
630 	} else { /* regular file */
631 		inode->i_generation |= 1;
632 		inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
633 		inode->i_op = &exfat_file_inode_operations;
634 		inode->i_fop = &exfat_file_operations;
635 		inode->i_mapping->a_ops = &exfat_aops;
636 		inode->i_mapping->nrpages = 0;
637 	}
638 
639 	i_size_write(inode, size);
640 
641 	exfat_save_attr(inode, info->attr);
642 
643 	inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
644 	inode_set_mtime_to_ts(inode, info->mtime);
645 	inode_set_ctime_to_ts(inode, info->mtime);
646 	ei->i_crtime = info->crtime;
647 	inode_set_atime_to_ts(inode, info->atime);
648 
649 	return 0;
650 }
651 
652 struct inode *exfat_build_inode(struct super_block *sb,
653 		struct exfat_dir_entry *info, loff_t i_pos)
654 {
655 	struct inode *inode;
656 	int err;
657 
658 	inode = exfat_iget(sb, i_pos);
659 	if (inode)
660 		goto out;
661 	inode = new_inode(sb);
662 	if (!inode) {
663 		inode = ERR_PTR(-ENOMEM);
664 		goto out;
665 	}
666 	inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
667 	inode_set_iversion(inode, 1);
668 	err = exfat_fill_inode(inode, info);
669 	if (err) {
670 		iput(inode);
671 		inode = ERR_PTR(err);
672 		goto out;
673 	}
674 	exfat_hash_inode(inode, i_pos);
675 	insert_inode_hash(inode);
676 out:
677 	return inode;
678 }
679 
680 void exfat_evict_inode(struct inode *inode)
681 {
682 	truncate_inode_pages_final(&inode->i_data);
683 
684 	if (!inode->i_nlink) {
685 		i_size_write(inode, 0);
686 		mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
687 		__exfat_truncate(inode);
688 		mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
689 	}
690 
691 	clear_inode(inode);
692 	exfat_cache_inval_inode(inode);
693 	exfat_unhash_inode(inode);
694 }
695