inode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. 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;
  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);
  26. struct timespec64 ts;
  27. if (inode->i_ino == EXFAT_ROOT_INO)
  28. return 0;
  29. /*
  30. * If the inode is already unlinked, there is no need for updating it.
  31. */
  32. if (ei->dir.dir == DIR_DELETED)
  33. return 0;
  34. if (is_dir && ei->dir.dir == sbi->root_dir && ei->entry == -1)
  35. return 0;
  36. exfat_set_volume_dirty(sb);
  37. /* get the directory entry of given file or directory */
  38. if (exfat_get_dentry_set_by_ei(&es, sb, ei))
  39. return -EIO;
  40. ep = exfat_get_dentry_cached(&es, ES_IDX_FILE);
  41. ep2 = exfat_get_dentry_cached(&es, ES_IDX_STREAM);
  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. ts = inode_get_mtime(inode);
  50. exfat_set_entry_time(sbi, &ts,
  51. &ep->dentry.file.modify_tz,
  52. &ep->dentry.file.modify_time,
  53. &ep->dentry.file.modify_date,
  54. &ep->dentry.file.modify_time_cs);
  55. ts = inode_get_atime(inode);
  56. exfat_set_entry_time(sbi, &ts,
  57. &ep->dentry.file.access_tz,
  58. &ep->dentry.file.access_time,
  59. &ep->dentry.file.access_date,
  60. NULL);
  61. /* File size should be zero if there is no cluster allocated */
  62. on_disk_size = i_size_read(inode);
  63. if (ei->start_clu == EXFAT_EOF_CLUSTER)
  64. on_disk_size = 0;
  65. ep2->dentry.stream.size = cpu_to_le64(on_disk_size);
  66. /*
  67. * mmap write does not use exfat_write_end(), valid_size may be
  68. * extended to the sector-aligned length in exfat_get_block().
  69. * So we need to fixup valid_size to the writren length.
  70. */
  71. if (on_disk_size < ei->valid_size)
  72. ep2->dentry.stream.valid_size = ep2->dentry.stream.size;
  73. else
  74. ep2->dentry.stream.valid_size = cpu_to_le64(ei->valid_size);
  75. if (on_disk_size) {
  76. ep2->dentry.stream.flags = ei->flags;
  77. ep2->dentry.stream.start_clu = cpu_to_le32(ei->start_clu);
  78. } else {
  79. ep2->dentry.stream.flags = ALLOC_FAT_CHAIN;
  80. ep2->dentry.stream.start_clu = EXFAT_FREE_CLUSTER;
  81. }
  82. exfat_update_dir_chksum(&es);
  83. return exfat_put_dentry_set(&es, sync);
  84. }
  85. int exfat_write_inode(struct inode *inode, struct writeback_control *wbc)
  86. {
  87. int ret;
  88. if (unlikely(exfat_forced_shutdown(inode->i_sb)))
  89. return -EIO;
  90. mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
  91. ret = __exfat_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
  92. mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
  93. return ret;
  94. }
  95. void exfat_sync_inode(struct inode *inode)
  96. {
  97. lockdep_assert_held(&EXFAT_SB(inode->i_sb)->s_lock);
  98. __exfat_write_inode(inode, 1);
  99. }
  100. /*
  101. * Input: inode, (logical) clu_offset, target allocation area
  102. * Output: errcode, cluster number
  103. * *clu = (~0), if it's unable to allocate a new cluster
  104. */
  105. static int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
  106. unsigned int *clu, unsigned int *count, int create)
  107. {
  108. int ret;
  109. unsigned int last_clu;
  110. struct exfat_chain new_clu;
  111. struct super_block *sb = inode->i_sb;
  112. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  113. struct exfat_inode_info *ei = EXFAT_I(inode);
  114. unsigned int local_clu_offset = clu_offset;
  115. unsigned int num_to_be_allocated = 0, num_clusters;
  116. num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi);
  117. if (clu_offset >= num_clusters)
  118. num_to_be_allocated = clu_offset - num_clusters + 1;
  119. if (!create && (num_to_be_allocated > 0)) {
  120. *clu = EXFAT_EOF_CLUSTER;
  121. return 0;
  122. }
  123. *clu = last_clu = ei->start_clu;
  124. if (*clu == EXFAT_EOF_CLUSTER) {
  125. *count = 0;
  126. } else if (ei->flags == ALLOC_NO_FAT_CHAIN) {
  127. last_clu += num_clusters - 1;
  128. if (clu_offset < num_clusters) {
  129. *clu += clu_offset;
  130. *count = min(num_clusters - clu_offset, *count);
  131. } else {
  132. *clu = EXFAT_EOF_CLUSTER;
  133. *count = 0;
  134. }
  135. } else {
  136. int err = exfat_get_cluster(inode, clu_offset,
  137. clu, count, &last_clu);
  138. if (err)
  139. return -EIO;
  140. }
  141. if (*clu == EXFAT_EOF_CLUSTER) {
  142. exfat_set_volume_dirty(sb);
  143. new_clu.dir = (last_clu == EXFAT_EOF_CLUSTER) ?
  144. EXFAT_EOF_CLUSTER : last_clu + 1;
  145. new_clu.size = 0;
  146. new_clu.flags = ei->flags;
  147. /* allocate a cluster */
  148. if (num_to_be_allocated < 1) {
  149. /* Broken FAT (i_sze > allocated FAT) */
  150. exfat_fs_error(sb, "broken FAT chain.");
  151. return -EIO;
  152. }
  153. ret = exfat_alloc_cluster(inode, num_to_be_allocated, &new_clu,
  154. inode_needs_sync(inode));
  155. if (ret)
  156. return ret;
  157. if (new_clu.dir == EXFAT_EOF_CLUSTER ||
  158. new_clu.dir == EXFAT_FREE_CLUSTER) {
  159. exfat_fs_error(sb,
  160. "bogus cluster new allocated (last_clu : %u, new_clu : %u)",
  161. last_clu, new_clu.dir);
  162. return -EIO;
  163. }
  164. /* append to the FAT chain */
  165. if (last_clu == EXFAT_EOF_CLUSTER) {
  166. if (new_clu.flags == ALLOC_FAT_CHAIN)
  167. ei->flags = ALLOC_FAT_CHAIN;
  168. ei->start_clu = new_clu.dir;
  169. } else {
  170. if (new_clu.flags != ei->flags) {
  171. /* no-fat-chain bit is disabled,
  172. * so fat-chain should be synced with
  173. * alloc-bitmap
  174. */
  175. exfat_chain_cont_cluster(sb, ei->start_clu,
  176. num_clusters);
  177. ei->flags = ALLOC_FAT_CHAIN;
  178. }
  179. if (new_clu.flags == ALLOC_FAT_CHAIN)
  180. if (exfat_ent_set(sb, last_clu, new_clu.dir))
  181. return -EIO;
  182. }
  183. num_clusters += num_to_be_allocated;
  184. *clu = new_clu.dir;
  185. inode->i_blocks += EXFAT_CLU_TO_B(num_to_be_allocated, sbi) >> 9;
  186. /*
  187. * Move *clu pointer along FAT chains (hole care) because the
  188. * caller of this function expect *clu to be the last cluster.
  189. * This only works when num_to_be_allocated >= 2,
  190. * *clu = (the first cluster of the allocated chain) =>
  191. * (the last cluster of ...)
  192. */
  193. if (ei->flags == ALLOC_NO_FAT_CHAIN) {
  194. *clu += num_to_be_allocated - 1;
  195. } else {
  196. while (num_to_be_allocated > 1) {
  197. if (exfat_get_next_cluster(sb, clu))
  198. return -EIO;
  199. num_to_be_allocated--;
  200. }
  201. }
  202. *count = 1;
  203. }
  204. /* hint information */
  205. ei->hint_bmap.off = local_clu_offset;
  206. ei->hint_bmap.clu = *clu;
  207. return 0;
  208. }
  209. static int exfat_get_block(struct inode *inode, sector_t iblock,
  210. struct buffer_head *bh_result, int create)
  211. {
  212. struct exfat_inode_info *ei = EXFAT_I(inode);
  213. struct super_block *sb = inode->i_sb;
  214. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  215. unsigned long max_blocks = bh_result->b_size >> inode->i_blkbits;
  216. int err = 0;
  217. unsigned long mapped_blocks = 0;
  218. unsigned int cluster, sec_offset, count;
  219. sector_t last_block;
  220. sector_t phys = 0;
  221. sector_t valid_blks;
  222. loff_t i_size;
  223. mutex_lock(&sbi->s_lock);
  224. i_size = i_size_read(inode);
  225. last_block = EXFAT_B_TO_BLK_ROUND_UP(i_size, sb);
  226. if (iblock >= last_block && !create)
  227. goto done;
  228. /* Is this block already allocated? */
  229. count = EXFAT_B_TO_CLU_ROUND_UP(bh_result->b_size, sbi);
  230. err = exfat_map_cluster(inode, iblock >> sbi->sect_per_clus_bits,
  231. &cluster, &count, create);
  232. if (err) {
  233. if (err != -ENOSPC)
  234. exfat_fs_error_ratelimit(sb,
  235. "failed to bmap (inode : %p iblock : %llu, err : %d)",
  236. inode, (unsigned long long)iblock, err);
  237. goto unlock_ret;
  238. }
  239. if (cluster == EXFAT_EOF_CLUSTER)
  240. goto done;
  241. /* sector offset in cluster */
  242. sec_offset = iblock & (sbi->sect_per_clus - 1);
  243. phys = exfat_cluster_to_sector(sbi, cluster) + sec_offset;
  244. mapped_blocks = ((unsigned long)count << sbi->sect_per_clus_bits) - sec_offset;
  245. max_blocks = min(mapped_blocks, max_blocks);
  246. map_bh(bh_result, sb, phys);
  247. if (buffer_delay(bh_result))
  248. clear_buffer_delay(bh_result);
  249. /*
  250. * In most cases, we just need to set bh_result to mapped, unmapped
  251. * or new status as follows:
  252. * 1. i_size == valid_size
  253. * 2. write case (create == 1)
  254. * 3. direct_read (!bh_result->b_folio)
  255. * -> the unwritten part will be zeroed in exfat_direct_IO()
  256. *
  257. * Otherwise, in the case of buffered read, it is necessary to take
  258. * care the last nested block if valid_size is not equal to i_size.
  259. */
  260. if (i_size == ei->valid_size || create || !bh_result->b_folio)
  261. valid_blks = EXFAT_B_TO_BLK_ROUND_UP(ei->valid_size, sb);
  262. else
  263. valid_blks = EXFAT_B_TO_BLK(ei->valid_size, sb);
  264. /* The range has been fully written, map it */
  265. if (iblock + max_blocks < valid_blks)
  266. goto done;
  267. /* The range has been partially written, map the written part */
  268. if (iblock < valid_blks) {
  269. max_blocks = valid_blks - iblock;
  270. goto done;
  271. }
  272. /* The area has not been written, map and mark as new for create case */
  273. if (create) {
  274. set_buffer_new(bh_result);
  275. ei->valid_size = EXFAT_BLK_TO_B(iblock + max_blocks, sb);
  276. mark_inode_dirty(inode);
  277. goto done;
  278. }
  279. /*
  280. * The area has just one block partially written.
  281. * In that case, we should read and fill the unwritten part of
  282. * a block with zero.
  283. */
  284. if (bh_result->b_folio && iblock == valid_blks &&
  285. (ei->valid_size & (sb->s_blocksize - 1))) {
  286. loff_t size, pos;
  287. void *addr;
  288. max_blocks = 1;
  289. /*
  290. * No buffer_head is allocated.
  291. * (1) bmap: It's enough to set blocknr without I/O.
  292. * (2) read: The unwritten part should be filled with zero.
  293. * If a folio does not have any buffers,
  294. * let's returns -EAGAIN to fallback to
  295. * block_read_full_folio() for per-bh IO.
  296. */
  297. if (!folio_buffers(bh_result->b_folio)) {
  298. err = -EAGAIN;
  299. goto done;
  300. }
  301. pos = EXFAT_BLK_TO_B(iblock, sb);
  302. size = ei->valid_size - pos;
  303. addr = folio_address(bh_result->b_folio) +
  304. offset_in_folio(bh_result->b_folio, pos);
  305. /* Check if bh->b_data points to proper addr in folio */
  306. if (bh_result->b_data != addr) {
  307. exfat_fs_error_ratelimit(sb,
  308. "b_data(%p) != folio_addr(%p)",
  309. bh_result->b_data, addr);
  310. err = -EINVAL;
  311. goto done;
  312. }
  313. /* Read a block */
  314. err = bh_read(bh_result, 0);
  315. if (err < 0)
  316. goto done;
  317. /* Zero unwritten part of a block */
  318. memset(bh_result->b_data + size, 0, bh_result->b_size - size);
  319. err = 0;
  320. goto done;
  321. }
  322. /*
  323. * The area has not been written, clear mapped for read/bmap cases.
  324. * If so, it will be filled with zero without reading from disk.
  325. */
  326. clear_buffer_mapped(bh_result);
  327. done:
  328. bh_result->b_size = EXFAT_BLK_TO_B(max_blocks, sb);
  329. if (err < 0)
  330. clear_buffer_mapped(bh_result);
  331. unlock_ret:
  332. mutex_unlock(&sbi->s_lock);
  333. return err;
  334. }
  335. static int exfat_read_folio(struct file *file, struct folio *folio)
  336. {
  337. return mpage_read_folio(folio, exfat_get_block);
  338. }
  339. static void exfat_readahead(struct readahead_control *rac)
  340. {
  341. struct address_space *mapping = rac->mapping;
  342. struct inode *inode = mapping->host;
  343. struct exfat_inode_info *ei = EXFAT_I(inode);
  344. loff_t pos = readahead_pos(rac);
  345. /* Range cross valid_size, read it page by page. */
  346. if (ei->valid_size < i_size_read(inode) &&
  347. pos <= ei->valid_size &&
  348. ei->valid_size < pos + readahead_length(rac))
  349. return;
  350. mpage_readahead(rac, exfat_get_block);
  351. }
  352. static int exfat_writepages(struct address_space *mapping,
  353. struct writeback_control *wbc)
  354. {
  355. if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
  356. return -EIO;
  357. return mpage_writepages(mapping, wbc, exfat_get_block);
  358. }
  359. static void exfat_write_failed(struct address_space *mapping, loff_t to)
  360. {
  361. struct inode *inode = mapping->host;
  362. if (to > i_size_read(inode)) {
  363. truncate_pagecache(inode, i_size_read(inode));
  364. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  365. exfat_truncate(inode);
  366. }
  367. }
  368. static int exfat_write_begin(const struct kiocb *iocb,
  369. struct address_space *mapping,
  370. loff_t pos, unsigned int len,
  371. struct folio **foliop, void **fsdata)
  372. {
  373. int ret;
  374. if (unlikely(exfat_forced_shutdown(mapping->host->i_sb)))
  375. return -EIO;
  376. ret = block_write_begin(mapping, pos, len, foliop, exfat_get_block);
  377. if (ret < 0)
  378. exfat_write_failed(mapping, pos+len);
  379. return ret;
  380. }
  381. static int exfat_write_end(const struct kiocb *iocb,
  382. struct address_space *mapping,
  383. loff_t pos, unsigned int len, unsigned int copied,
  384. struct folio *folio, void *fsdata)
  385. {
  386. struct inode *inode = mapping->host;
  387. struct exfat_inode_info *ei = EXFAT_I(inode);
  388. int err;
  389. err = generic_write_end(iocb, mapping, pos, len, copied, folio, fsdata);
  390. if (err < len)
  391. exfat_write_failed(mapping, pos+len);
  392. if (!(err < 0) && pos + err > ei->valid_size) {
  393. ei->valid_size = pos + err;
  394. mark_inode_dirty(inode);
  395. }
  396. if (!(err < 0) && !(ei->attr & EXFAT_ATTR_ARCHIVE)) {
  397. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  398. ei->attr |= EXFAT_ATTR_ARCHIVE;
  399. mark_inode_dirty(inode);
  400. }
  401. return err;
  402. }
  403. static ssize_t exfat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  404. {
  405. struct address_space *mapping = iocb->ki_filp->f_mapping;
  406. struct inode *inode = mapping->host;
  407. struct exfat_inode_info *ei = EXFAT_I(inode);
  408. loff_t pos = iocb->ki_pos;
  409. loff_t size = pos + iov_iter_count(iter);
  410. int rw = iov_iter_rw(iter);
  411. ssize_t ret;
  412. /*
  413. * Need to use the DIO_LOCKING for avoiding the race
  414. * condition of exfat_get_block() and ->truncate().
  415. */
  416. ret = blockdev_direct_IO(iocb, inode, iter, exfat_get_block);
  417. if (ret < 0) {
  418. if (rw == WRITE && ret != -EIOCBQUEUED)
  419. exfat_write_failed(mapping, size);
  420. return ret;
  421. }
  422. size = pos + ret;
  423. if (rw == WRITE) {
  424. /*
  425. * If the block had been partially written before this write,
  426. * ->valid_size will not be updated in exfat_get_block(),
  427. * update it here.
  428. */
  429. if (ei->valid_size < size) {
  430. ei->valid_size = size;
  431. mark_inode_dirty(inode);
  432. }
  433. } else if (pos < ei->valid_size && ei->valid_size < size) {
  434. /* zero the unwritten part in the partially written block */
  435. iov_iter_revert(iter, size - ei->valid_size);
  436. iov_iter_zero(size - ei->valid_size, iter);
  437. }
  438. return ret;
  439. }
  440. static sector_t exfat_aop_bmap(struct address_space *mapping, sector_t block)
  441. {
  442. sector_t blocknr;
  443. /* exfat_get_cluster() assumes the requested blocknr isn't truncated. */
  444. down_read(&EXFAT_I(mapping->host)->truncate_lock);
  445. blocknr = generic_block_bmap(mapping, block, exfat_get_block);
  446. up_read(&EXFAT_I(mapping->host)->truncate_lock);
  447. return blocknr;
  448. }
  449. /*
  450. * exfat_block_truncate_page() zeroes out a mapping from file offset `from'
  451. * up to the end of the block which corresponds to `from'.
  452. * This is required during truncate to physically zeroout the tail end
  453. * of that block so it doesn't yield old data if the file is later grown.
  454. * Also, avoid causing failure from fsx for cases of "data past EOF"
  455. */
  456. int exfat_block_truncate_page(struct inode *inode, loff_t from)
  457. {
  458. return block_truncate_page(inode->i_mapping, from, exfat_get_block);
  459. }
  460. static const struct address_space_operations exfat_aops = {
  461. .dirty_folio = block_dirty_folio,
  462. .invalidate_folio = block_invalidate_folio,
  463. .read_folio = exfat_read_folio,
  464. .readahead = exfat_readahead,
  465. .writepages = exfat_writepages,
  466. .write_begin = exfat_write_begin,
  467. .write_end = exfat_write_end,
  468. .direct_IO = exfat_direct_IO,
  469. .bmap = exfat_aop_bmap,
  470. .migrate_folio = buffer_migrate_folio,
  471. };
  472. static inline unsigned long exfat_hash(loff_t i_pos)
  473. {
  474. return hash_32(i_pos, EXFAT_HASH_BITS);
  475. }
  476. void exfat_hash_inode(struct inode *inode, loff_t i_pos)
  477. {
  478. struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
  479. struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
  480. spin_lock(&sbi->inode_hash_lock);
  481. EXFAT_I(inode)->i_pos = i_pos;
  482. hlist_add_head(&EXFAT_I(inode)->i_hash_fat, head);
  483. spin_unlock(&sbi->inode_hash_lock);
  484. }
  485. void exfat_unhash_inode(struct inode *inode)
  486. {
  487. struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
  488. spin_lock(&sbi->inode_hash_lock);
  489. hlist_del_init(&EXFAT_I(inode)->i_hash_fat);
  490. EXFAT_I(inode)->i_pos = 0;
  491. spin_unlock(&sbi->inode_hash_lock);
  492. }
  493. struct inode *exfat_iget(struct super_block *sb, loff_t i_pos)
  494. {
  495. struct exfat_sb_info *sbi = EXFAT_SB(sb);
  496. struct exfat_inode_info *info;
  497. struct hlist_head *head = sbi->inode_hashtable + exfat_hash(i_pos);
  498. struct inode *inode = NULL;
  499. spin_lock(&sbi->inode_hash_lock);
  500. hlist_for_each_entry(info, head, i_hash_fat) {
  501. WARN_ON(info->vfs_inode.i_sb != sb);
  502. if (i_pos != info->i_pos)
  503. continue;
  504. inode = igrab(&info->vfs_inode);
  505. if (inode)
  506. break;
  507. }
  508. spin_unlock(&sbi->inode_hash_lock);
  509. return inode;
  510. }
  511. /* doesn't deal with root inode */
  512. static int exfat_fill_inode(struct inode *inode, struct exfat_dir_entry *info)
  513. {
  514. struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb);
  515. struct exfat_inode_info *ei = EXFAT_I(inode);
  516. loff_t size = info->size;
  517. ei->dir = info->dir;
  518. ei->entry = info->entry;
  519. ei->attr = info->attr;
  520. ei->start_clu = info->start_clu;
  521. ei->flags = info->flags;
  522. ei->type = info->type;
  523. ei->valid_size = info->valid_size;
  524. ei->version = 0;
  525. ei->hint_stat.eidx = 0;
  526. ei->hint_stat.clu = info->start_clu;
  527. ei->hint_femp.eidx = EXFAT_HINT_NONE;
  528. ei->hint_bmap.off = EXFAT_EOF_CLUSTER;
  529. ei->i_pos = 0;
  530. inode->i_uid = sbi->options.fs_uid;
  531. inode->i_gid = sbi->options.fs_gid;
  532. inode_inc_iversion(inode);
  533. inode->i_generation = get_random_u32();
  534. if (info->attr & EXFAT_ATTR_SUBDIR) { /* directory */
  535. inode->i_generation &= ~1;
  536. inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
  537. inode->i_op = &exfat_dir_inode_operations;
  538. inode->i_fop = &exfat_dir_operations;
  539. set_nlink(inode, info->num_subdirs);
  540. } else { /* regular file */
  541. inode->i_generation |= 1;
  542. inode->i_mode = exfat_make_mode(sbi, info->attr, 0777);
  543. inode->i_op = &exfat_file_inode_operations;
  544. inode->i_fop = &exfat_file_operations;
  545. inode->i_mapping->a_ops = &exfat_aops;
  546. inode->i_mapping->nrpages = 0;
  547. }
  548. i_size_write(inode, size);
  549. exfat_save_attr(inode, info->attr);
  550. inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9;
  551. inode_set_mtime_to_ts(inode, info->mtime);
  552. inode_set_ctime_to_ts(inode, info->mtime);
  553. ei->i_crtime = info->crtime;
  554. inode_set_atime_to_ts(inode, info->atime);
  555. return 0;
  556. }
  557. struct inode *exfat_build_inode(struct super_block *sb,
  558. struct exfat_dir_entry *info, loff_t i_pos)
  559. {
  560. struct inode *inode;
  561. int err;
  562. inode = exfat_iget(sb, i_pos);
  563. if (inode)
  564. goto out;
  565. inode = new_inode(sb);
  566. if (!inode) {
  567. inode = ERR_PTR(-ENOMEM);
  568. goto out;
  569. }
  570. inode->i_ino = iunique(sb, EXFAT_ROOT_INO);
  571. inode_set_iversion(inode, 1);
  572. err = exfat_fill_inode(inode, info);
  573. if (err) {
  574. iput(inode);
  575. inode = ERR_PTR(err);
  576. goto out;
  577. }
  578. exfat_hash_inode(inode, i_pos);
  579. insert_inode_hash(inode);
  580. out:
  581. return inode;
  582. }
  583. void exfat_evict_inode(struct inode *inode)
  584. {
  585. truncate_inode_pages(&inode->i_data, 0);
  586. if (!inode->i_nlink) {
  587. i_size_write(inode, 0);
  588. mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
  589. __exfat_truncate(inode);
  590. mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
  591. }
  592. invalidate_inode_buffers(inode);
  593. clear_inode(inode);
  594. exfat_cache_inval_inode(inode);
  595. exfat_unhash_inode(inode);
  596. }