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 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: &es, sb, p_dir: &(ei->dir), entry: ei->entry, ES_ALL_ENTRIES))
47 return -EIO;
48 ep = exfat_get_dentry_cached(es: &es, ES_IDX_FILE);
49 ep2 = exfat_get_dentry_cached(es: &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, ts: &ei->i_crtime,
55 tz: &ep->dentry.file.create_tz,
56 time: &ep->dentry.file.create_time,
57 date: &ep->dentry.file.create_date,
58 time_cs: &ep->dentry.file.create_time_cs);
59 ts = inode_get_mtime(inode);
60 exfat_set_entry_time(sbi, ts: &ts,
61 tz: &ep->dentry.file.modify_tz,
62 time: &ep->dentry.file.modify_time,
63 date: &ep->dentry.file.modify_date,
64 time_cs: &ep->dentry.file.modify_time_cs);
65 ts = inode_get_atime(inode);
66 exfat_set_entry_time(sbi, ts: &ts,
67 tz: &ep->dentry.file.access_tz,
68 time: &ep->dentry.file.access_time,
69 date: &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: &es);
98 return exfat_put_dentry_set(es: &es, sync);
99}
100
101int 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, sync: wbc->sync_mode == WB_SYNC_ALL);
107 mutex_unlock(lock: &EXFAT_SB(sb: inode->i_sb)->s_lock);
108
109 return ret;
110}
111
112void exfat_sync_inode(struct inode *inode)
113{
114 lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
115 __exfat_write_inode(inode, sync: 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 */
123static 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, cluster: clu_offset,
161 fclus: &fclus, dclus: clu, last_dclus: &last_clu, allow_eof: 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_alloc: num_to_be_allocated, p_chain: &new_clu,
200 sync_bmap: 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, chain: ei->start_clu,
224 len: num_clusters);
225 ei->flags = ALLOC_FAT_CHAIN;
226 }
227 if (new_clu.flags == ALLOC_FAT_CHAIN)
228 if (exfat_ent_set(sb, loc: last_clu, content: 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
263static 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
278static 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, clu_offset: iblock >> sbi->sect_per_clus_bits,
300 clu: &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, clus: 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: bh_result)) {
321 if (ei->i_size_ondisk < pos)
322 ei->i_size_ondisk = pos;
323 }
324
325 map_bh(bh: bh_result, sb, block: phys);
326 if (buffer_delay(bh: bh_result))
327 clear_buffer_delay(bh: 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: 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: bh_result, folio: bh_result->b_folio, offset: off);
390 err = bh_read(bh: bh_result, op_flags: 0);
391 if (err < 0)
392 goto unlock_ret;
393
394 folio_zero_segment(folio: bh_result->b_folio, start: off + size,
395 xend: 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: bh_result);
402 }
403 }
404done:
405 bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
406unlock_ret:
407 mutex_unlock(lock: &sbi->s_lock);
408 return err;
409}
410
411static int exfat_read_folio(struct file *file, struct folio *folio)
412{
413 return mpage_read_folio(folio, get_block: exfat_get_block);
414}
415
416static 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, get_block: exfat_get_block);
430}
431
432static int exfat_writepages(struct address_space *mapping,
433 struct writeback_control *wbc)
434{
435 return mpage_writepages(mapping, wbc, get_block: exfat_get_block);
436}
437
438static 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, new: i_size_read(inode));
444 inode_set_mtime_to_ts(inode, ts: inode_set_ctime_current(inode));
445 exfat_truncate(inode);
446 }
447}
448
449static int exfat_write_begin(struct file *file, struct address_space *mapping,
450 loff_t pos, unsigned int len,
451 struct page **pagep, void **fsdata)
452{
453 int ret;
454
455 *pagep = NULL;
456 ret = block_write_begin(mapping, pos, len, pagep, get_block: exfat_get_block);
457
458 if (ret < 0)
459 exfat_write_failed(mapping, to: pos+len);
460
461 return ret;
462}
463
464static int exfat_write_end(struct file *file, struct address_space *mapping,
465 loff_t pos, unsigned int len, unsigned int copied,
466 struct page *pagep, void *fsdata)
467{
468 struct inode *inode = mapping->host;
469 struct exfat_inode_info *ei = EXFAT_I(inode);
470 int err;
471
472 err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
473
474 if (ei->i_size_aligned < i_size_read(inode)) {
475 exfat_fs_error(inode->i_sb,
476 "invalid size(size(%llu) > aligned(%llu)\n",
477 i_size_read(inode), ei->i_size_aligned);
478 return -EIO;
479 }
480
481 if (err < len)
482 exfat_write_failed(mapping, to: pos+len);
483
484 if (!(err < 0) && pos + err > ei->valid_size) {
485 ei->valid_size = pos + err;
486 mark_inode_dirty(inode);
487 }
488
489 if (!(err < 0) && !(ei->attr & EXFAT_ATTR_ARCHIVE)) {
490 inode_set_mtime_to_ts(inode, ts: inode_set_ctime_current(inode));
491 ei->attr |= EXFAT_ATTR_ARCHIVE;
492 mark_inode_dirty(inode);
493 }
494
495 return err;
496}
497
498static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
499{
500 struct address_space *mapping = iocb->ki_filp->f_mapping;
501 struct inode *inode = mapping->host;
502 struct exfat_inode_info *ei = EXFAT_I(inode);
503 loff_t pos = iocb->ki_pos;
504 loff_t size = pos + iov_iter_count(i: iter);
505 int rw = iov_iter_rw(i: iter);
506 ssize_t ret;
507
508 if (rw == WRITE) {
509 /*
510 * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
511 * so we need to update the ->i_size_aligned to block boundary.
512 *
513 * But we must fill the remaining area or hole by nul for
514 * updating ->i_size_aligned
515 *
516 * Return 0, and fallback to normal buffered write.
517 */
518 if (EXFAT_I(inode)->i_size_aligned < size)
519 return 0;
520 }
521
522 /*
523 * Need to use the DIO_LOCKING for avoiding the race
524 * condition of exfat_get_block() and ->truncate().
525 */
526 ret = blockdev_direct_IO(iocb, inode, iter, get_block: exfat_get_block);
527 if (ret < 0) {
528 if (rw == WRITE && ret != -EIOCBQUEUED)
529 exfat_write_failed(mapping, to: size);
530
531 return ret;
532 } else
533 size = pos + ret;
534
535 /* zero the unwritten part in the partially written block */
536 if (rw == READ && pos < ei->valid_size && ei->valid_size < size) {
537 iov_iter_revert(i: iter, bytes: size - ei->valid_size);
538 iov_iter_zero(bytes: size - ei->valid_size, iter);
539 }
540
541 return ret;
542}
543
544static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
545{
546 sector_t blocknr;
547
548 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
549 down_read(sem: &EXFAT_I(inode: mapping->host)->truncate_lock);
550 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
551 up_read(sem: &EXFAT_I(inode: mapping->host)->truncate_lock);
552 return blocknr;
553}
554
555/*
556 * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
557 * up to the end of the block which corresponds to `from'.
558 * This is required during truncate to physically zeroout the tail end
559 * of that block so it doesn't yield old data if the file is later grown.
560 * Also, avoid causing failure from fsx for cases of "data past EOF"
561 */
562int exfat_block_truncate_page(struct inode *inode, loff_t from)
563{
564 return block_truncate_page(inode->i_mapping, from, exfat_get_block);
565}
566
567static const struct address_space_operations exfat_aops = {
568 .dirty_folio = block_dirty_folio,
569 .invalidate_folio = block_invalidate_folio,
570 .read_folio = exfat_read_folio,
571 .readahead = exfat_readahead,
572 .writepages = exfat_writepages,
573 .write_begin = exfat_write_begin,
574 .write_end = exfat_write_end,
575 .direct_IO = exfat_direct_IO,
576 .bmap = exfat_aop_bmap,
577 .migrate_folio = buffer_migrate_folio,
578};
579
580static inline unsigned long exfat_hash(loff_t i_pos)
581{
582 return hash_32(val: i_pos, EXFAT_HASH_BITS);
583}
584
585void exfat_hash_inode(struct inode *inode, loff_t i_pos)
586{
587 struct exfat_sb_info *sbi = EXFAT_SB(sb: inode->i_sb);
588 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
589
590 spin_lock(lock: &sbi->inode_hash_lock);
591 EXFAT_I(inode)->i_pos = i_pos;
592 hlist_add_head(n: &EXFAT_I(inode)->i_hash_fat, h: head);
593 spin_unlock(lock: &sbi->inode_hash_lock);
594}
595
596void exfat_unhash_inode(struct inode *inode)
597{
598 struct exfat_sb_info *sbi = EXFAT_SB(sb: inode->i_sb);
599
600 spin_lock(lock: &sbi->inode_hash_lock);
601 hlist_del_init(n: &EXFAT_I(inode)->i_hash_fat);
602 EXFAT_I(inode)->i_pos = 0;
603 spin_unlock(lock: &sbi->inode_hash_lock);
604}
605
606struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
607{
608 struct exfat_sb_info *sbi = EXFAT_SB(sb);
609 struct exfat_inode_info *info;
610 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
611 struct inode *inode = NULL;
612
613 spin_lock(lock: &sbi->inode_hash_lock);
614 hlist_for_each_entry(info, head, i_hash_fat) {
615 WARN_ON(info->vfs_inode.i_sb != sb);
616
617 if (i_pos != info->i_pos)
618 continue;
619 inode = igrab(&info->vfs_inode);
620 if (inode)
621 break;
622 }
623 spin_unlock(lock: &sbi->inode_hash_lock);
624 return inode;
625}
626
627/* doesn't deal with root inode */
628static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
629{
630 struct exfat_sb_info *sbi = EXFAT_SB(sb: inode->i_sb);
631 struct exfat_inode_info *ei = EXFAT_I(inode);
632 loff_t size = info->size;
633
634 ei->dir = info->dir;
635 ei->entry = info->entry;
636 ei->attr = info->attr;
637 ei->start_clu = info->start_clu;
638 ei->flags = info->flags;
639 ei->type = info->type;
640 ei->valid_size = info->valid_size;
641
642 ei->version = 0;
643 ei->hint_stat.eidx = 0;
644 ei->hint_stat.clu = info->start_clu;
645 ei->hint_femp.eidx = EXFAT_HINT_NONE;
646 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
647 ei->i_pos = 0;
648
649 inode->i_uid = sbi->options.fs_uid;
650 inode->i_gid = sbi->options.fs_gid;
651 inode_inc_iversion(inode);
652 inode->i_generation = get_random_u32();
653
654 if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
655 inode->i_generation &= ~1;
656 inode->i_mode = exfat_make_mode(sbi, attr: info->attr, mode: 0777);
657 inode->i_op = &exfat_dir_inode_operations;
658 inode->i_fop = &exfat_dir_operations;
659 set_nlink(inode, nlink: info->num_subdirs);
660 } else { /* regular file */
661 inode->i_generation |= 1;
662 inode->i_mode = exfat_make_mode(sbi, attr: info->attr, mode: 0777);
663 inode->i_op = &exfat_file_inode_operations;
664 inode->i_fop = &exfat_file_operations;
665 inode->i_mapping->a_ops = &exfat_aops;
666 inode->i_mapping->nrpages = 0;
667 }
668
669 i_size_write(inode, i_size: size);
670
671 /* ondisk and aligned size should be aligned with block size */
672 if (size & (inode->i_sb->s_blocksize - 1)) {
673 size |= (inode->i_sb->s_blocksize - 1);
674 size++;
675 }
676
677 ei->i_size_aligned = size;
678 ei->i_size_ondisk = size;
679
680 exfat_save_attr(inode, attr: info->attr);
681
682 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
683 inode_set_mtime_to_ts(inode, ts: info->mtime);
684 inode_set_ctime_to_ts(inode, ts: info->mtime);
685 ei->i_crtime = info->crtime;
686 inode_set_atime_to_ts(inode, ts: info->atime);
687
688 return 0;
689}
690
691struct inode *exfat_build_inode(struct super_block *sb,
692 struct exfat_dir_entry *info, loff_t i_pos)
693{
694 struct inode *inode;
695 int err;
696
697 inode = exfat_iget(sb, i_pos);
698 if (inode)
699 goto out;
700 inode = new_inode(sb);
701 if (!inode) {
702 inode = ERR_PTR(error: -ENOMEM);
703 goto out;
704 }
705 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
706 inode_set_iversion(inode, val: 1);
707 err = exfat_fill_inode(inode, info);
708 if (err) {
709 iput(inode);
710 inode = ERR_PTR(error: err);
711 goto out;
712 }
713 exfat_hash_inode(inode, i_pos);
714 insert_inode_hash(inode);
715out:
716 return inode;
717}
718
719void exfat_evict_inode(struct inode *inode)
720{
721 truncate_inode_pages(&inode->i_data, 0);
722
723 if (!inode->i_nlink) {
724 i_size_write(inode, i_size: 0);
725 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
726 __exfat_truncate(inode);
727 mutex_unlock(lock: &EXFAT_SB(sb: inode->i_sb)->s_lock);
728 }
729
730 invalidate_inode_buffers(inode);
731 clear_inode(inode);
732 exfat_cache_inval_inode(inode);
733 exfat_unhash_inode(inode);
734}
735

source code of linux/fs/exfat/inode.c