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