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(struct file * file,struct address_space * mapping,loff_t pos,unsigned int len,struct folio ** foliop,void ** fsdata)449 static int exfat_write_begin(struct file *file, struct address_space *mapping,
450 loff_t pos, unsigned int len,
451 struct folio **foliop, void **fsdata)
452 {
453 int ret;
454
455 if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
456 return -EIO;
457
458 ret = block_write_begin(mapping, pos, len, foliop, exfat_get_block);
459
460 if (ret < 0)
461 exfat_write_failed(mapping, pos+len);
462
463 return ret;
464 }
465
exfat_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned int len,unsigned int copied,struct folio * folio,void * fsdata)466 static int exfat_write_end(struct file *file, struct address_space *mapping,
467 loff_t pos, unsigned int len, unsigned int copied,
468 struct folio *folio, void *fsdata)
469 {
470 struct inode *inode = mapping->host;
471 struct exfat_inode_info *ei = EXFAT_I(inode);
472 int err;
473
474 err = generic_write_end(file, mapping, pos, len, copied, folio, fsdata);
475 if (err < len)
476 exfat_write_failed(mapping, pos+len);
477
478 if (!(err < 0) && pos + err > ei->valid_size) {
479 ei->valid_size = pos + err;
480 mark_inode_dirty(inode);
481 }
482
483 if (!(err < 0) && !(ei->attr & EXFAT_ATTR_ARCHIVE)) {
484 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
485 ei->attr |= EXFAT_ATTR_ARCHIVE;
486 mark_inode_dirty(inode);
487 }
488
489 return err;
490 }
491
exfat_direct_IO(struct kiocb * iocb,struct iov_iter * iter)492 static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
493 {
494 struct address_space *mapping = iocb->ki_filp->f_mapping;
495 struct inode *inode = mapping->host;
496 struct exfat_inode_info *ei = EXFAT_I(inode);
497 loff_t pos = iocb->ki_pos;
498 loff_t size = pos + iov_iter_count(iter);
499 int rw = iov_iter_rw(iter);
500 ssize_t ret;
501
502 /*
503 * Need to use the DIO_LOCKING for avoiding the race
504 * condition of exfat_get_block() and ->truncate().
505 */
506 ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
507 if (ret < 0) {
508 if (rw == WRITE && ret != -EIOCBQUEUED)
509 exfat_write_failed(mapping, size);
510
511 return ret;
512 } else
513 size = pos + ret;
514
515 if (rw == WRITE) {
516 /*
517 * If the block had been partially written before this write,
518 * ->valid_size will not be updated in exfat_get_block(),
519 * update it here.
520 */
521 if (ei->valid_size < size) {
522 ei->valid_size = size;
523 mark_inode_dirty(inode);
524 }
525 } else if (pos < ei->valid_size && ei->valid_size < size) {
526 /* zero the unwritten part in the partially written block */
527 iov_iter_revert(iter, size - ei->valid_size);
528 iov_iter_zero(size - ei->valid_size, iter);
529 }
530
531 return ret;
532 }
533
exfat_aop_bmap(struct address_space * mapping,sector_t block)534 static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
535 {
536 sector_t blocknr;
537
538 /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
539 down_read(&EXFAT_I(mapping->host)->truncate_lock);
540 blocknr = generic_block_bmap(mapping, block, exfat_get_block);
541 up_read(&EXFAT_I(mapping->host)->truncate_lock);
542 return blocknr;
543 }
544
545 /*
546 * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
547 * up to the end of the block which corresponds to `from'.
548 * This is required during truncate to physically zeroout the tail end
549 * of that block so it doesn't yield old data if the file is later grown.
550 * Also, avoid causing failure from fsx for cases of "data past EOF"
551 */
exfat_block_truncate_page(struct inode * inode,loff_t from)552 int exfat_block_truncate_page(struct inode *inode, loff_t from)
553 {
554 return block_truncate_page(inode->i_mapping, from, exfat_get_block);
555 }
556
557 static const struct address_space_operations exfat_aops = {
558 .dirty_folio = block_dirty_folio,
559 .invalidate_folio = block_invalidate_folio,
560 .read_folio = exfat_read_folio,
561 .readahead = exfat_readahead,
562 .writepages = exfat_writepages,
563 .write_begin = exfat_write_begin,
564 .write_end = exfat_write_end,
565 .direct_IO = exfat_direct_IO,
566 .bmap = exfat_aop_bmap,
567 .migrate_folio = buffer_migrate_folio,
568 };
569
exfat_hash(loff_t i_pos)570 static inline unsigned long exfat_hash(loff_t i_pos)
571 {
572 return hash_32(i_pos, EXFAT_HASH_BITS);
573 }
574
exfat_hash_inode(struct inode * inode,loff_t i_pos)575 void exfat_hash_inode(struct inode *inode, loff_t i_pos)
576 {
577 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
578 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
579
580 spin_lock(&sbi->inode_hash_lock);
581 EXFAT_I(inode)->i_pos = i_pos;
582 hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
583 spin_unlock(&sbi->inode_hash_lock);
584 }
585
exfat_unhash_inode(struct inode * inode)586 void exfat_unhash_inode(struct inode *inode)
587 {
588 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
589
590 spin_lock(&sbi->inode_hash_lock);
591 hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
592 EXFAT_I(inode)->i_pos = 0;
593 spin_unlock(&sbi->inode_hash_lock);
594 }
595
exfat_iget(struct super_block * sb,loff_t i_pos)596 struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
597 {
598 struct exfat_sb_info *sbi = EXFAT_SB(sb);
599 struct exfat_inode_info *info;
600 struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
601 struct inode *inode = NULL;
602
603 spin_lock(&sbi->inode_hash_lock);
604 hlist_for_each_entry(info, head, i_hash_fat) {
605 WARN_ON(info->vfs_inode.i_sb != sb);
606
607 if (i_pos != info->i_pos)
608 continue;
609 inode = igrab(&info->vfs_inode);
610 if (inode)
611 break;
612 }
613 spin_unlock(&sbi->inode_hash_lock);
614 return inode;
615 }
616
617 /* doesn't deal with root inode */
exfat_fill_inode(struct inode * inode,struct exfat_dir_entry * info)618 static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
619 {
620 struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
621 struct exfat_inode_info *ei = EXFAT_I(inode);
622 loff_t size = info->size;
623
624 ei->dir = info->dir;
625 ei->entry = info->entry;
626 ei->attr = info->attr;
627 ei->start_clu = info->start_clu;
628 ei->flags = info->flags;
629 ei->type = info->type;
630 ei->valid_size = info->valid_size;
631
632 ei->version = 0;
633 ei->hint_stat.eidx = 0;
634 ei->hint_stat.clu = info->start_clu;
635 ei->hint_femp.eidx = EXFAT_HINT_NONE;
636 ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
637 ei->i_pos = 0;
638
639 inode->i_uid = sbi->options.fs_uid;
640 inode->i_gid = sbi->options.fs_gid;
641 inode_inc_iversion(inode);
642 inode->i_generation = get_random_u32();
643
644 if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
645 inode->i_generation &= ~1;
646 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
647 inode->i_op = &exfat_dir_inode_operations;
648 inode->i_fop = &exfat_dir_operations;
649 set_nlink(inode, info->num_subdirs);
650 } else { /* regular file */
651 inode->i_generation |= 1;
652 inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
653 inode->i_op = &exfat_file_inode_operations;
654 inode->i_fop = &exfat_file_operations;
655 inode->i_mapping->a_ops = &exfat_aops;
656 inode->i_mapping->nrpages = 0;
657 }
658
659 i_size_write(inode, size);
660
661 exfat_save_attr(inode, info->attr);
662
663 inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
664 inode_set_mtime_to_ts(inode, info->mtime);
665 inode_set_ctime_to_ts(inode, info->mtime);
666 ei->i_crtime = info->crtime;
667 inode_set_atime_to_ts(inode, info->atime);
668
669 return 0;
670 }
671
exfat_build_inode(struct super_block * sb,struct exfat_dir_entry * info,loff_t i_pos)672 struct inode *exfat_build_inode(struct super_block *sb,
673 struct exfat_dir_entry *info, loff_t i_pos)
674 {
675 struct inode *inode;
676 int err;
677
678 inode = exfat_iget(sb, i_pos);
679 if (inode)
680 goto out;
681 inode = new_inode(sb);
682 if (!inode) {
683 inode = ERR_PTR(-ENOMEM);
684 goto out;
685 }
686 inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
687 inode_set_iversion(inode, 1);
688 err = exfat_fill_inode(inode, info);
689 if (err) {
690 iput(inode);
691 inode = ERR_PTR(err);
692 goto out;
693 }
694 exfat_hash_inode(inode, i_pos);
695 insert_inode_hash(inode);
696 out:
697 return inode;
698 }
699
exfat_evict_inode(struct inode * inode)700 void exfat_evict_inode(struct inode *inode)
701 {
702 truncate_inode_pages(&inode->i_data, 0);
703
704 if (!inode->i_nlink) {
705 i_size_write(inode, 0);
706 mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
707 __exfat_truncate(inode);
708 mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
709 }
710
711 invalidate_inode_buffers(inode);
712 clear_inode(inode);
713 exfat_cache_inval_inode(inode);
714 exfat_unhash_inode(inode);
715 }
716