inode.c 19 KB

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