inode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/buffer_head.h>
  7. #include <linux/mpage.h>
  8. #include <linux/bio.h>
  9. #include <linux/blkdev.h>
  10. #include <linux/time.h>
  11. #include <linux/writeback.h>
  12. #include <linux/uio.h>
  13. #include <linux/random.h>
  14. #include <linux/iversion.h>
  15. #include "exfat_raw.h"
  16. #include "exfat_fs.h"
  17. static int __exfat_write_inode(struct inode *inode, int sync)
  18. {
  19. unsigned long long on_disk_size;
  20. struct exfat_dentry *ep, *ep2;
  21. struct exfat_entry_set_cache *es = NULL;
  22. struct super_block *sb = inode->i_sb;
  23. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  24. struct exfat_inode_info *ei = EXFAT_I(inode);
  25. bool is_dir = (ei->type == TYPE_DIR) ? true : false;
  26. if (inode->i_ino == EXFAT_ROOT_INO)
  27. return 0;
  28. /*
  29. * If the indode is already unlinked, there is no need for updating it.
  30. */
  31. if (ei->dir.dir == DIR_DELETED)
  32. return 0;
  33. if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
  34. return 0;
  35. exfat_set_volume_dirty(sb);
  36. /* get the directory entry of given file or directory */
  37. es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry, ES_ALL_ENTRIES);
  38. if (!es)
  39. return -EIO;
  40. ep = exfat_get_dentry_cached(es, 0);
  41. ep2 = exfat_get_dentry_cached(es, 1);
  42. ep->dentry.file.attr = cpu_to_le16(exfat_make_attr(inode));
  43. /* set FILE_INFO structure using the acquired struct exfat_dentry */
  44. exfat_set_entry_time(sbi, &ei->i_crtime,
  45. &ep->dentry.file.create_tz,
  46. &ep->dentry.file.create_time,
  47. &ep->dentry.file.create_date,
  48. &ep->dentry.file.create_time_cs);
  49. exfat_set_entry_time(sbi, &inode->i_mtime,
  50. &ep->dentry.file.modify_tz,
  51. &ep->dentry.file.modify_time,
  52. &ep->dentry.file.modify_date,
  53. &ep->dentry.file.modify_time_cs);
  54. exfat_set_entry_time(sbi, &inode->i_atime,
  55. &ep->dentry.file.access_tz,
  56. &ep->dentry.file.access_time,
  57. &ep->dentry.file.access_date,
  58. NULL);
  59. /* File size should be zero if there is no cluster allocated */
  60. on_disk_size = i_size_read(inode);
  61. if (ei->start_clu == EXFAT_EOF_CLUSTER)
  62. on_disk_size = 0;
  63. ep2->dentry.stream.valid_size = cpu_to_le64(on_disk_size);
  64. ep2->dentry.stream.size = ep2->dentry.stream.valid_size;
  65. exfat_update_dir_chksum_with_entry_set(es);
  66. return exfat_free_dentry_set(es, sync);
  67. }
  68. int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
  69. {
  70. int ret;
  71. mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
  72. ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  73. mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
  74. return ret;
  75. }
  76. void exfat_sync_inode(struct inode *inode)
  77. {
  78. lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
  79. __exfat_write_inode(inode, 1);
  80. }
  81. /*
  82. * Input: inode, (logical) clu_offset, target allocation area
  83. * Output: errcode, cluster number
  84. * *clu = (~0), if it's unable to allocate a new cluster
  85. */
  86. static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
  87. unsigned int *clu, int create)
  88. {
  89. int ret, modified = false;
  90. unsigned int last_clu;
  91. struct exfat_chain new_clu;
  92. struct super_block *sb = inode->i_sb;
  93. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  94. struct exfat_inode_info *ei = EXFAT_I(inode);
  95. unsigned int local_clu_offset = clu_offset;
  96. unsigned int num_to_be_allocated = 0, num_clusters = 0;
  97. if (ei->i_size_ondisk > 0)
  98. num_clusters =
  99. EXFAT_B_TO_CLU_ROUND_UP(ei->i_size_ondisk, sbi);
  100. if (clu_offset >= num_clusters)
  101. num_to_be_allocated = clu_offset - num_clusters + 1;
  102. if (!create && (num_to_be_allocated > 0)) {
  103. *clu = EXFAT_EOF_CLUSTER;
  104. return 0;
  105. }
  106. *clu = last_clu = ei->start_clu;
  107. if (ei->flags == ALLOC_NO_FAT_CHAIN) {
  108. if (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
  109. last_clu += clu_offset - 1;
  110. if (clu_offset == num_clusters)
  111. *clu = EXFAT_EOF_CLUSTER;
  112. else
  113. *clu += clu_offset;
  114. }
  115. } else if (ei->type == TYPE_FILE) {
  116. unsigned int fclus = 0;
  117. int err = exfat_get_cluster(inode, clu_offset,
  118. &fclus, clu, &last_clu, 1);
  119. if (err)
  120. return -EIO;
  121. clu_offset -= fclus;
  122. } else {
  123. /* hint information */
  124. if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
  125. ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
  126. clu_offset -= ei->hint_bmap.off;
  127. /* hint_bmap.clu should be valid */
  128. WARN_ON(ei->hint_bmap.clu < 2);
  129. *clu = ei->hint_bmap.clu;
  130. }
  131. while (clu_offset > 0 && *clu != EXFAT_EOF_CLUSTER) {
  132. last_clu = *clu;
  133. if (exfat_get_next_cluster(sb, clu))
  134. return -EIO;
  135. clu_offset--;
  136. }
  137. }
  138. if (*clu == EXFAT_EOF_CLUSTER) {
  139. exfat_set_volume_dirty(sb);
  140. new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
  141. EXFAT_EOF_CLUSTER : last_clu + 1;
  142. new_clu.size = 0;
  143. new_clu.flags = ei->flags;
  144. /* allocate a cluster */
  145. if (num_to_be_allocated < 1) {
  146. /* Broken FAT (i_sze > allocated FAT) */
  147. exfat_fs_error(sb, "broken FAT chain.");
  148. return -EIO;
  149. }
  150. ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu);
  151. if (ret)
  152. return ret;
  153. if (new_clu.dir == EXFAT_EOF_CLUSTER ||
  154. new_clu.dir == EXFAT_FREE_CLUSTER) {
  155. exfat_fs_error(sb,
  156. "bogus cluster new allocated (last_clu : %u, new_clu : %u)",
  157. last_clu, new_clu.dir);
  158. return -EIO;
  159. }
  160. /* append to the FAT chain */
  161. if (last_clu == EXFAT_EOF_CLUSTER) {
  162. if (new_clu.flags == ALLOC_FAT_CHAIN)
  163. ei->flags = ALLOC_FAT_CHAIN;
  164. ei->start_clu = new_clu.dir;
  165. modified = true;
  166. } else {
  167. if (new_clu.flags != ei->flags) {
  168. /* no-fat-chain bit is disabled,
  169. * so fat-chain should be synced with
  170. * alloc-bitmap
  171. */
  172. exfat_chain_cont_cluster(sb, ei->start_clu,
  173. num_clusters);
  174. ei->flags = ALLOC_FAT_CHAIN;
  175. modified = true;
  176. }
  177. if (new_clu.flags == ALLOC_FAT_CHAIN)
  178. if (exfat_ent_set(sb, last_clu, new_clu.dir))
  179. return -EIO;
  180. }
  181. num_clusters += num_to_be_allocated;
  182. *clu = new_clu.dir;
  183. if (ei->dir.dir != DIR_DELETED && modified) {
  184. struct exfat_dentry *ep;
  185. struct exfat_entry_set_cache *es;
  186. int err;
  187. es = exfat_get_dentry_set(sb, &(ei->dir), ei->entry,
  188. ES_ALL_ENTRIES);
  189. if (!es)
  190. return -EIO;
  191. /* get stream entry */
  192. ep = exfat_get_dentry_cached(es, 1);
  193. /* update directory entry */
  194. ep->dentry.stream.flags = ei->flags;
  195. ep->dentry.stream.start_clu =
  196. cpu_to_le32(ei->start_clu);
  197. ep->dentry.stream.valid_size =
  198. cpu_to_le64(i_size_read(inode));
  199. ep->dentry.stream.size =
  200. ep->dentry.stream.valid_size;
  201. exfat_update_dir_chksum_with_entry_set(es);
  202. err = exfat_free_dentry_set(es, inode_needs_sync(inode));
  203. if (err)
  204. return err;
  205. } /* end of if != DIR_DELETED */
  206. inode->i_blocks +=
  207. num_to_be_allocated << sbi->sect_per_clus_bits;
  208. /*
  209. * Move *clu pointer along FAT chains (hole care) because the
  210. * caller of this function expect *clu to be the last cluster.
  211. * This only works when num_to_be_allocated >= 2,
  212. * *clu = (the first cluster of the allocated chain) =>
  213. * (the last cluster of ...)
  214. */
  215. if (ei->flags == ALLOC_NO_FAT_CHAIN) {
  216. *clu += num_to_be_allocated - 1;
  217. } else {
  218. while (num_to_be_allocated > 1) {
  219. if (exfat_get_next_cluster(sb, clu))
  220. return -EIO;
  221. num_to_be_allocated--;
  222. }
  223. }
  224. }
  225. /* hint information */
  226. ei->hint_bmap.off = local_clu_offset;
  227. ei->hint_bmap.clu = *clu;
  228. return 0;
  229. }
  230. static int exfat_map_new_buffer(struct exfat_inode_info *ei,
  231. struct buffer_head *bh, loff_t pos)
  232. {
  233. if (buffer_delay(bh) && pos > ei->i_size_aligned)
  234. return -EIO;
  235. set_buffer_new(bh);
  236. /*
  237. * Adjust i_size_aligned if i_size_ondisk is bigger than it.
  238. */
  239. if (ei->i_size_ondisk > ei->i_size_aligned)
  240. ei->i_size_aligned = ei->i_size_ondisk;
  241. return 0;
  242. }
  243. static int exfat_get_block(struct inode *inode, sector_t iblock,
  244. struct buffer_head *bh_result, int create)
  245. {
  246. struct exfat_inode_info *ei = EXFAT_I(inode);
  247. struct super_block *sb = inode->i_sb;
  248. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  249. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  250. int err = 0;
  251. unsigned long mapped_blocks = 0;
  252. unsigned int cluster, sec_offset;
  253. sector_t last_block;
  254. sector_t phys = 0;
  255. loff_t pos;
  256. mutex_lock(&sbi->s_lock);
  257. last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size_read(inode), sb);
  258. if (iblock >= last_block && !create)
  259. goto done;
  260. /* Is this block already allocated? */
  261. err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
  262. &cluster, create);
  263. if (err) {
  264. if (err != -ENOSPC)
  265. exfat_fs_error_ratelimit(sb,
  266. "failed to bmap (inode : %p iblock : %llu, err : %d)",
  267. inode, (unsigned long long)iblock, err);
  268. goto unlock_ret;
  269. }
  270. if (cluster == EXFAT_EOF_CLUSTER)
  271. goto done;
  272. /* sector offset in cluster */
  273. sec_offset = iblock & (sbi->sect_per_clus - 1);
  274. phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
  275. mapped_blocks = sbi->sect_per_clus - sec_offset;
  276. max_blocks = min(mapped_blocks, max_blocks);
  277. /* Treat newly added block / cluster */
  278. if (iblock < last_block)
  279. create = 0;
  280. if (create || buffer_delay(bh_result)) {
  281. pos = EXFAT_BLK_TO_B((iblock + 1), sb);
  282. if (ei->i_size_ondisk < pos)
  283. ei->i_size_ondisk = pos;
  284. }
  285. if (create) {
  286. err = exfat_map_new_buffer(ei, bh_result, pos);
  287. if (err) {
  288. exfat_fs_error(sb,
  289. "requested for bmap out of range(pos : (%llu) > i_size_aligned(%llu)\n",
  290. pos, ei->i_size_aligned);
  291. goto unlock_ret;
  292. }
  293. }
  294. if (buffer_delay(bh_result))
  295. clear_buffer_delay(bh_result);
  296. map_bh(bh_result, sb, phys);
  297. done:
  298. bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
  299. unlock_ret:
  300. mutex_unlock(&sbi->s_lock);
  301. return err;
  302. }
  303. static int exfat_readpage(struct file *file, struct page *page)
  304. {
  305. return mpage_readpage(page, exfat_get_block);
  306. }
  307. static void exfat_readahead(struct readahead_control *rac)
  308. {
  309. mpage_readahead(rac, exfat_get_block);
  310. }
  311. static int exfat_writepage(struct page *page, struct writeback_control *wbc)
  312. {
  313. return block_write_full_page(page, exfat_get_block, wbc);
  314. }
  315. static int exfat_writepages(struct address_space *mapping,
  316. struct writeback_control *wbc)
  317. {
  318. return mpage_writepages(mapping, wbc, exfat_get_block);
  319. }
  320. static void exfat_write_failed(struct address_space *mapping, loff_t to)
  321. {
  322. struct inode *inode = mapping->host;
  323. if (to > i_size_read(inode)) {
  324. truncate_pagecache(inode, i_size_read(inode));
  325. exfat_truncate(inode, EXFAT_I(inode)->i_size_aligned);
  326. }
  327. }
  328. static int exfat_write_begin(struct file *file, struct address_space *mapping,
  329. loff_t pos, unsigned int len, unsigned int flags,
  330. struct page **pagep, void **fsdata)
  331. {
  332. int ret;
  333. *pagep = NULL;
  334. ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  335. exfat_get_block,
  336. &EXFAT_I(mapping->host)->i_size_ondisk);
  337. if (ret < 0)
  338. exfat_write_failed(mapping, pos+len);
  339. return ret;
  340. }
  341. static int exfat_write_end(struct file *file, struct address_space *mapping,
  342. loff_t pos, unsigned int len, unsigned int copied,
  343. struct page *pagep, void *fsdata)
  344. {
  345. struct inode *inode = mapping->host;
  346. struct exfat_inode_info *ei = EXFAT_I(inode);
  347. int err;
  348. err = generic_write_end(file, mapping, pos, len, copied, pagep, fsdata);
  349. if (ei->i_size_aligned < i_size_read(inode)) {
  350. exfat_fs_error(inode->i_sb,
  351. "invalid size(size(%llu) > aligned(%llu)\n",
  352. i_size_read(inode), ei->i_size_aligned);
  353. return -EIO;
  354. }
  355. if (err < len)
  356. exfat_write_failed(mapping, pos+len);
  357. if (!(err < 0) && !(ei->attr & ATTR_ARCHIVE)) {
  358. inode->i_mtime = inode->i_ctime = current_time(inode);
  359. ei->attr |= ATTR_ARCHIVE;
  360. mark_inode_dirty(inode);
  361. }
  362. return err;
  363. }
  364. static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  365. {
  366. struct address_space *mapping = iocb->ki_filp->f_mapping;
  367. struct inode *inode = mapping->host;
  368. loff_t size = iocb->ki_pos + iov_iter_count(iter);
  369. int rw = iov_iter_rw(iter);
  370. ssize_t ret;
  371. if (rw == WRITE) {
  372. /*
  373. * FIXME: blockdev_direct_IO() doesn't use ->write_begin(),
  374. * so we need to update the ->i_size_aligned to block boundary.
  375. *
  376. * But we must fill the remaining area or hole by nul for
  377. * updating ->i_size_aligned
  378. *
  379. * Return 0, and fallback to normal buffered write.
  380. */
  381. if (EXFAT_I(inode)->i_size_aligned < size)
  382. return 0;
  383. }
  384. /*
  385. * Need to use the DIO_LOCKING for avoiding the race
  386. * condition of exfat_get_block() and ->truncate().
  387. */
  388. ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
  389. if (ret < 0 && (rw & WRITE))
  390. exfat_write_failed(mapping, size);
  391. return ret;
  392. }
  393. static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
  394. {
  395. sector_t blocknr;
  396. /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
  397. down_read(&EXFAT_I(mapping->host)->truncate_lock);
  398. blocknr = generic_block_bmap(mapping, block, exfat_get_block);
  399. up_read(&EXFAT_I(mapping->host)->truncate_lock);
  400. return blocknr;
  401. }
  402. /*
  403. * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
  404. * up to the end of the block which corresponds to `from'.
  405. * This is required during truncate to physically zeroout the tail end
  406. * of that block so it doesn't yield old data if the file is later grown.
  407. * Also, avoid causing failure from fsx for cases of "data past EOF"
  408. */
  409. int exfat_block_truncate_page(struct inode *inode, loff_t from)
  410. {
  411. return block_truncate_page(inode->i_mapping, from, exfat_get_block);
  412. }
  413. static const struct address_space_operations exfat_aops = {
  414. .readpage = exfat_readpage,
  415. .readahead = exfat_readahead,
  416. .writepage = exfat_writepage,
  417. .writepages = exfat_writepages,
  418. .write_begin = exfat_write_begin,
  419. .write_end = exfat_write_end,
  420. .direct_IO = exfat_direct_IO,
  421. .bmap = exfat_aop_bmap
  422. };
  423. static inline unsigned long exfat_hash(loff_t i_pos)
  424. {
  425. return hash_32(i_pos, EXFAT_HASH_BITS);
  426. }
  427. void exfat_hash_inode(struct inode *inode, loff_t i_pos)
  428. {
  429. struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
  430. struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
  431. spin_lock(&sbi->inode_hash_lock);
  432. EXFAT_I(inode)->i_pos = i_pos;
  433. hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
  434. spin_unlock(&sbi->inode_hash_lock);
  435. }
  436. void exfat_unhash_inode(struct inode *inode)
  437. {
  438. struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
  439. spin_lock(&sbi->inode_hash_lock);
  440. hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
  441. EXFAT_I(inode)->i_pos = 0;
  442. spin_unlock(&sbi->inode_hash_lock);
  443. }
  444. struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
  445. {
  446. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  447. struct exfat_inode_info *info;
  448. struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
  449. struct inode *inode = NULL;
  450. spin_lock(&sbi->inode_hash_lock);
  451. hlist_for_each_entry(info, head, i_hash_fat) {
  452. WARN_ON(info->vfs_inode.i_sb != sb);
  453. if (i_pos != info->i_pos)
  454. continue;
  455. inode = igrab(&info->vfs_inode);
  456. if (inode)
  457. break;
  458. }
  459. spin_unlock(&sbi->inode_hash_lock);
  460. return inode;
  461. }
  462. /* doesn't deal with root inode */
  463. static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
  464. {
  465. struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
  466. struct exfat_inode_info *ei = EXFAT_I(inode);
  467. loff_t size = info->size;
  468. ei->dir = info->dir;
  469. ei->entry = info->entry;
  470. ei->attr = info->attr;
  471. ei->start_clu = info->start_clu;
  472. ei->flags = info->flags;
  473. ei->type = info->type;
  474. ei->version = 0;
  475. ei->hint_stat.eidx = 0;
  476. ei->hint_stat.clu = info->start_clu;
  477. ei->hint_femp.eidx = EXFAT_HINT_NONE;
  478. ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
  479. ei->i_pos = 0;
  480. inode->i_uid = sbi->options.fs_uid;
  481. inode->i_gid = sbi->options.fs_gid;
  482. inode_inc_iversion(inode);
  483. inode->i_generation = prandom_u32();
  484. if (info->attr & ATTR_SUBDIR) { /* directory */
  485. inode->i_generation &= ~1;
  486. inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
  487. inode->i_op = &exfat_dir_inode_operations;
  488. inode->i_fop = &exfat_dir_operations;
  489. set_nlink(inode, info->num_subdirs);
  490. } else { /* regular file */
  491. inode->i_generation |= 1;
  492. inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
  493. inode->i_op = &exfat_file_inode_operations;
  494. inode->i_fop = &exfat_file_operations;
  495. inode->i_mapping->a_ops = &exfat_aops;
  496. inode->i_mapping->nrpages = 0;
  497. }
  498. i_size_write(inode, size);
  499. /* ondisk and aligned size should be aligned with block size */
  500. if (size & (inode->i_sb->s_blocksize - 1)) {
  501. size |= (inode->i_sb->s_blocksize - 1);
  502. size++;
  503. }
  504. ei->i_size_aligned = size;
  505. ei->i_size_ondisk = size;
  506. exfat_save_attr(inode, info->attr);
  507. inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >>
  508. inode->i_blkbits;
  509. inode->i_mtime = info->mtime;
  510. inode->i_ctime = info->mtime;
  511. ei->i_crtime = info->crtime;
  512. inode->i_atime = info->atime;
  513. return 0;
  514. }
  515. struct inode *exfat_build_inode(struct super_block *sb,
  516. struct exfat_dir_entry *info, loff_t i_pos)
  517. {
  518. struct inode *inode;
  519. int err;
  520. inode = exfat_iget(sb, i_pos);
  521. if (inode)
  522. goto out;
  523. inode = new_inode(sb);
  524. if (!inode) {
  525. inode = ERR_PTR(-ENOMEM);
  526. goto out;
  527. }
  528. inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
  529. inode_set_iversion(inode, 1);
  530. err = exfat_fill_inode(inode, info);
  531. if (err) {
  532. iput(inode);
  533. inode = ERR_PTR(err);
  534. goto out;
  535. }
  536. exfat_hash_inode(inode, i_pos);
  537. insert_inode_hash(inode);
  538. out:
  539. return inode;
  540. }
  541. void exfat_evict_inode(struct inode *inode)
  542. {
  543. truncate_inode_pages(&inode->i_data, 0);
  544. if (!inode->i_nlink) {
  545. i_size_write(inode, 0);
  546. mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
  547. __exfat_truncate(inode, 0);
  548. mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
  549. }
  550. invalidate_inode_buffers(inode);
  551. clear_inode(inode);
  552. exfat_cache_inval_inode(inode);
  553. exfat_unhash_inode(inode);
  554. }