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 loff_t i_size;
278
279 mutex_lock(&sbi->s_lock);
280 i_size = i_size_read(inode);
281 last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size, sb);
282 if (iblock >= last_block && !create)
283 goto done;
284
285 /* Is this block already allocated? */
286 err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
287 &cluster, create);
288 if (err) {
289 if (err != -ENOSPC)
290 exfat_fs_error_ratelimit(sb,
291 "failed to bmap (inode : %p iblock : %llu, err : %d)",
292 inode, (unsigned long long)iblock, err);
293 goto unlock_ret;
294 }
295
296 if (cluster == EXFAT_EOF_CLUSTER)
297 goto done;
298
299 /* sector offset in cluster */
300 sec_offset = iblock & (sbi->sect_per_clus - 1);
301
302 phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
303 mapped_blocks = sbi->sect_per_clus - sec_offset;
304 max_blocks = min(mapped_blocks, max_blocks);
305
306 map_bh(bh_result, sb, phys);
307 if (buffer_delay(bh_result))
308 clear_buffer_delay(bh_result);
309
310 /*
311 * In most cases, we just need to set bh_result to mapped, unmapped
312 * or new status as follows:
313 * 1. i_size == valid_size
314 * 2. write case (create == 1)
315 * 3. direct_read (!bh_result->b_folio)
316 * -> the unwritten part will be zeroed in exfat_direct_IO()
317 *
318 * Otherwise, in the case of buffered read, it is necessary to take
319 * care the last nested block if valid_size is not equal to i_size.
320 */
321 if (i_size == ei->valid_size || create || !bh_result->b_folio)
322 valid_blks = EXFAT_B_TO_BLK_ROUND_UP(ei->valid_size, sb);
323 else
324 valid_blks = EXFAT_B_TO_BLK(ei->valid_size, sb);
325
326 /* The range has been fully written, map it */
327 if (iblock + max_blocks < valid_blks)
328 goto done;
329
330 /* The range has been partially written, map the written part */
331 if (iblock < valid_blks) {
332 max_blocks = valid_blks - iblock;
333 goto done;
334 }
335
336 /* The area has not been written, map and mark as new for create case */
337 if (create) {
338 set_buffer_new(bh_result);
339 ei->valid_size = EXFAT_BLK_TO_B(iblock + max_blocks, sb);
340 mark_inode_dirty(inode);
341 goto done;
342 }
343
344 /*
345 * The area has just one block partially written.
346 * In that case, we should read and fill the unwritten part of
347 * a block with zero.
348 */
349 if (bh_result->b_folio && iblock == valid_blks &&
350 (ei->valid_size & (sb->s_blocksize - 1))) {
351 loff_t size, pos;
352 void *addr;
353
354 max_blocks = 1;
355
356 /*
357 * No buffer_head is allocated.
358 * (1) bmap: It's enough to set blocknr without I/O.
359 * (2) read: The unwritten part should be filled with zero.
360 * If a folio does not have any buffers,
361 * let's returns -EAGAIN to fallback to
362 * block_read_full_folio() for per-bh IO.
363 */
364 if (!folio_buffers(bh_result->b_folio)) {
365 err = -EAGAIN;
366 goto done;
367 }
368
369 pos = EXFAT_BLK_TO_B(iblock, sb);
370 size = ei->valid_size - pos;
371 addr = folio_address(bh_result->b_folio) +
372 offset_in_folio(bh_result->b_folio, pos);
373
374 /* Check if bh->b_data points to proper addr in folio */
375 if (bh_result->b_data != addr) {
376 exfat_fs_error_ratelimit(sb,
377 "b_data(%p) != folio_addr(%p)",
378 bh_result->b_data, addr);
379 err = -EINVAL;
380 goto done;
381 }
382
383 /* Read a block */
384 err = bh_read(bh_result, 0);
385 if (err < 0)
386 goto done;
387
388 /* Zero unwritten part of a block */
389 memset(bh_result->b_data + size, 0, bh_result->b_size - size);
390 err = 0;
391 goto done;
392 }
393
394 /*
395 * The area has not been written, clear mapped for read/bmap cases.
396 * If so, it will be filled with zero without reading from disk.
397 */
398 clear_buffer_mapped(bh_result);
399 done:
400 bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
401 if (err < 0)
402 clear_buffer_mapped(bh_result);
403 unlock_ret:
404 mutex_unlock(&sbi->s_lock);
405 return err;
406 }
407
exfat_read_folio(struct file * file,struct folio * folio)408 static int exfat_read_folio(struct file *file, struct folio *folio)
409 {
410 return mpage_read_folio(folio, exfat_get_block);
411 }
412
exfat_readahead(struct readahead_control * rac)413 static void exfat_readahead(struct readahead_control *rac)
414 {
415 struct address_space *mapping = rac->mapping;
416 struct inode *inode = mapping->host;
417 struct exfat_inode_info *ei = EXFAT_I(inode);
418 loff_t pos = readahead_pos(rac);
419
420 /* Range cross valid_size, read it page by page. */
421 if (ei->valid_size < i_size_read(inode) &&
422 pos <= ei->valid_size &&
423 ei->valid_size < pos + readahead_length(rac))
424 return;
425
426 mpage_readahead(rac, exfat_get_block);
427 }
428
exfat_writepages(struct address_space * mapping,struct writeback_control * wbc)429 static int exfat_writepages(struct address_space *mapping,
430 struct writeback_control *wbc)
431 {
432 if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
433 return -EIO;
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(const struct kiocb * iocb,struct address_space * mapping,loff_t pos,unsigned int len,struct folio ** foliop,void ** fsdata)449 static int exfat_write_begin(const struct kiocb *iocb,
450 struct address_space *mapping,
451 loff_t pos, unsigned int len,
452 struct folio **foliop, void **fsdata)
453 {
454 int ret;
455
456 if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
457 return -EIO;
458
459 ret = block_write_begin(mapping, pos, len, foliop, exfat_get_block);
460
461 if (ret < 0)
462 exfat_write_failed(mapping, pos+len);
463
464 return ret;
465 }
466
exfat_write_end(const struct kiocb * iocb,struct address_space * mapping,loff_t pos,unsigned int len,unsigned int copied,struct folio * folio,void * fsdata)467 static int exfat_write_end(const struct kiocb *iocb,
468 struct address_space *mapping,
469 loff_t pos, unsigned int len, unsigned int copied,
470 struct folio *folio, void *fsdata)
471 {
472 struct inode *inode = mapping->host;
473 struct exfat_inode_info *ei = EXFAT_I(inode);
474 int err;
475
476 err = generic_write_end(iocb, mapping, pos, len, copied, folio, fsdata);
477 if (err < len)
478 exfat_write_failed(mapping, pos+len);
479
480 if (!(err < 0) && pos + err > ei->valid_size) {
481 ei->valid_size = pos + err;
482 mark_inode_dirty(inode);
483 }
484
485 if (!(err < 0) && !(ei->attr & EXFAT_ATTR_ARCHIVE)) {
486 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
487 ei->attr |= EXFAT_ATTR_ARCHIVE;
488 mark_inode_dirty(inode);
489 }
490
491 return err;
492 }
493
exfat_direct_IO(struct kiocb * iocb,struct iov_iter * iter)494 static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
495 {
496 struct address_space *mapping = iocb->ki_filp->f_mapping;
497 struct inode *inode = mapping->host;
498 struct exfat_inode_info *ei = EXFAT_I(inode);
499 loff_t pos = iocb->ki_pos;
500 loff_t size = pos + iov_iter_count(iter);
501 int rw = iov_iter_rw(iter);
502 ssize_t ret;
503
504 /*
505 * Need to use the DIO_LOCKING for avoiding the race
506 * condition of exfat_get_block() and ->truncate().
507 */
508 ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
509 if (ret < 0) {
510 if (rw == WRITE && ret != -EIOCBQUEUED)
511 exfat_write_failed(mapping, size);
512
513 return ret;
514 } else
515 size = pos + ret;
516
517 if (rw == WRITE) {
518 /*
519 * If the block had been partially written before this write,
520 * ->valid_size will not be updated in exfat_get_block(),
521 * update it here.
522 */
523 if (ei->valid_size < size) {
524 ei->valid_size = size;
525 mark_inode_dirty(inode);
526 }
527 } else if (pos < ei->valid_size && ei->valid_size < size) {
528 /* zero the unwritten part in the partially written block */
529 iov_iter_revert(iter, size - ei->valid_size);
530 iov_iter_zero(size - ei->valid_size, iter);
531 }
532
533 return ret;
534 }
535
exfat_aop_bmap(struct address_space * mapping,sector_t block)536 static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
537 {
538 sector_t blocknr;
539
540 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
541 down_read(&EXFAT_I(mapping->host)->truncate_lock);
542 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
543 up_read(&EXFAT_I(mapping->host)->truncate_lock);
544 return blocknr;
545 }
546
547 /*
548 * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
549 * up to the end of the block which corresponds to `from'.
550 * This is required during truncate to physically zeroout the tail end
551 * of that block so it doesn't yield old data if the file is later grown.
552 * Also, avoid causing failure from fsx for cases of "data past EOF"
553 */
exfat_block_truncate_page(struct inode * inode,loff_t from)554 int exfat_block_truncate_page(struct inode *inode, loff_t from)
555 {
556 return block_truncate_page(inode->i_mapping, from, exfat_get_block);
557 }
558
559 static const struct address_space_operations exfat_aops = {
560 .dirty_folio = block_dirty_folio,
561 .invalidate_folio = block_invalidate_folio,
562 .read_folio = exfat_read_folio,
563 .readahead = exfat_readahead,
564 .writepages = exfat_writepages,
565 .write_begin = exfat_write_begin,
566 .write_end = exfat_write_end,
567 .direct_IO = exfat_direct_IO,
568 .bmap = exfat_aop_bmap,
569 .migrate_folio = buffer_migrate_folio,
570 };
571
exfat_hash(loff_t i_pos)572 static inline unsigned long exfat_hash(loff_t i_pos)
573 {
574 return hash_32(i_pos, EXFAT_HASH_BITS);
575 }
576
exfat_hash_inode(struct inode * inode,loff_t i_pos)577 void exfat_hash_inode(struct inode *inode, loff_t i_pos)
578 {
579 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
580 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
581
582 spin_lock(&sbi->inode_hash_lock);
583 EXFAT_I(inode)->i_pos = i_pos;
584 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
585 spin_unlock(&sbi->inode_hash_lock);
586 }
587
exfat_unhash_inode(struct inode * inode)588 void exfat_unhash_inode(struct inode *inode)
589 {
590 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
591
592 spin_lock(&sbi->inode_hash_lock);
593 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
594 EXFAT_I(inode)->i_pos = 0;
595 spin_unlock(&sbi->inode_hash_lock);
596 }
597
exfat_iget(struct super_block * sb,loff_t i_pos)598 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
599 {
600 struct exfat_sb_info *sbi = EXFAT_SB(sb);
601 struct exfat_inode_info *info;
602 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
603 struct inode *inode = NULL;
604
605 spin_lock(&sbi->inode_hash_lock);
606 hlist_for_each_entry(info, head, i_hash_fat) {
607 WARN_ON(info->vfs_inode.i_sb != sb);
608
609 if (i_pos != info->i_pos)
610 continue;
611 inode = igrab(&info->vfs_inode);
612 if (inode)
613 break;
614 }
615 spin_unlock(&sbi->inode_hash_lock);
616 return inode;
617 }
618
619 /* doesn't deal with root inode */
exfat_fill_inode(struct inode * inode,struct exfat_dir_entry * info)620 static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
621 {
622 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
623 struct exfat_inode_info *ei = EXFAT_I(inode);
624 loff_t size = info->size;
625
626 ei->dir = info->dir;
627 ei->entry = info->entry;
628 ei->attr = info->attr;
629 ei->start_clu = info->start_clu;
630 ei->flags = info->flags;
631 ei->type = info->type;
632 ei->valid_size = info->valid_size;
633
634 ei->version = 0;
635 ei->hint_stat.eidx = 0;
636 ei->hint_stat.clu = info->start_clu;
637 ei->hint_femp.eidx = EXFAT_HINT_NONE;
638 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
639 ei->i_pos = 0;
640
641 inode->i_uid = sbi->options.fs_uid;
642 inode->i_gid = sbi->options.fs_gid;
643 inode_inc_iversion(inode);
644 inode->i_generation = get_random_u32();
645
646 if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
647 inode->i_generation &= ~1;
648 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
649 inode->i_op = &exfat_dir_inode_operations;
650 inode->i_fop = &exfat_dir_operations;
651 set_nlink(inode, info->num_subdirs);
652 } else { /* regular file */
653 inode->i_generation |= 1;
654 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
655 inode->i_op = &exfat_file_inode_operations;
656 inode->i_fop = &exfat_file_operations;
657 inode->i_mapping->a_ops = &exfat_aops;
658 inode->i_mapping->nrpages = 0;
659 }
660
661 i_size_write(inode, size);
662
663 exfat_save_attr(inode, info->attr);
664
665 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
666 inode_set_mtime_to_ts(inode, info->mtime);
667 inode_set_ctime_to_ts(inode, info->mtime);
668 ei->i_crtime = info->crtime;
669 inode_set_atime_to_ts(inode, info->atime);
670
671 return 0;
672 }
673
exfat_build_inode(struct super_block * sb,struct exfat_dir_entry * info,loff_t i_pos)674 struct inode *exfat_build_inode(struct super_block *sb,
675 struct exfat_dir_entry *info, loff_t i_pos)
676 {
677 struct inode *inode;
678 int err;
679
680 inode = exfat_iget(sb, i_pos);
681 if (inode)
682 goto out;
683 inode = new_inode(sb);
684 if (!inode) {
685 inode = ERR_PTR(-ENOMEM);
686 goto out;
687 }
688 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
689 inode_set_iversion(inode, 1);
690 err = exfat_fill_inode(inode, info);
691 if (err) {
692 iput(inode);
693 inode = ERR_PTR(err);
694 goto out;
695 }
696 exfat_hash_inode(inode, i_pos);
697 insert_inode_hash(inode);
698 out:
699 return inode;
700 }
701
exfat_evict_inode(struct inode * inode)702 void exfat_evict_inode(struct inode *inode)
703 {
704 truncate_inode_pages(&inode->i_data, 0);
705
706 if (!inode->i_nlink) {
707 i_size_write(inode, 0);
708 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
709 __exfat_truncate(inode);
710 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
711 }
712
713 invalidate_inode_buffers(inode);
714 clear_inode(inode);
715 exfat_cache_inval_inode(inode);
716 exfat_unhash_inode(inode);
717 }
718