file.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/file.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * from
  11. *
  12. * linux/fs/minix/file.c
  13. *
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * ext4 fs regular file handling primitives
  17. *
  18. * 64-bit file support on 64-bit platforms by Jakub Jelinek
  19. * (jj@sunsite.ms.mff.cuni.cz)
  20. */
  21. #include <linux/time.h>
  22. #include <linux/fs.h>
  23. #include <linux/iomap.h>
  24. #include <linux/mount.h>
  25. #include <linux/path.h>
  26. #include <linux/dax.h>
  27. #include <linux/quotaops.h>
  28. #include <linux/pagevec.h>
  29. #include <linux/uio.h>
  30. #include <linux/mman.h>
  31. #include <linux/backing-dev.h>
  32. #include "ext4.h"
  33. #include "ext4_jbd2.h"
  34. #include "xattr.h"
  35. #include "acl.h"
  36. #include "truncate.h"
  37. /*
  38. * Returns %true if the given DIO request should be attempted with DIO, or
  39. * %false if it should fall back to buffered I/O.
  40. *
  41. * DIO isn't well specified; when it's unsupported (either due to the request
  42. * being misaligned, or due to the file not supporting DIO at all), filesystems
  43. * either fall back to buffered I/O or return EINVAL. For files that don't use
  44. * any special features like encryption or verity, ext4 has traditionally
  45. * returned EINVAL for misaligned DIO. iomap_dio_rw() uses this convention too.
  46. * In this case, we should attempt the DIO, *not* fall back to buffered I/O.
  47. *
  48. * In contrast, in cases where DIO is unsupported due to ext4 features, ext4
  49. * traditionally falls back to buffered I/O.
  50. *
  51. * This function implements the traditional ext4 behavior in all these cases.
  52. */
  53. static bool ext4_should_use_dio(struct kiocb *iocb, struct iov_iter *iter)
  54. {
  55. struct inode *inode = file_inode(iocb->ki_filp);
  56. u32 dio_align = ext4_dio_alignment(inode);
  57. if (dio_align == 0)
  58. return false;
  59. if (dio_align == 1)
  60. return true;
  61. return IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), dio_align);
  62. }
  63. static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
  64. {
  65. ssize_t ret;
  66. struct inode *inode = file_inode(iocb->ki_filp);
  67. if (iocb->ki_flags & IOCB_NOWAIT) {
  68. if (!inode_trylock_shared(inode))
  69. return -EAGAIN;
  70. } else {
  71. inode_lock_shared(inode);
  72. }
  73. if (!ext4_should_use_dio(iocb, to)) {
  74. inode_unlock_shared(inode);
  75. /*
  76. * Fallback to buffered I/O if the operation being performed on
  77. * the inode is not supported by direct I/O. The IOCB_DIRECT
  78. * flag needs to be cleared here in order to ensure that the
  79. * direct I/O path within generic_file_read_iter() is not
  80. * taken.
  81. */
  82. iocb->ki_flags &= ~IOCB_DIRECT;
  83. return generic_file_read_iter(iocb, to);
  84. }
  85. ret = iomap_dio_rw(iocb, to, &ext4_iomap_ops, NULL, 0, NULL, 0);
  86. inode_unlock_shared(inode);
  87. file_accessed(iocb->ki_filp);
  88. return ret;
  89. }
  90. #ifdef CONFIG_FS_DAX
  91. static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
  92. {
  93. struct inode *inode = file_inode(iocb->ki_filp);
  94. ssize_t ret;
  95. if (iocb->ki_flags & IOCB_NOWAIT) {
  96. if (!inode_trylock_shared(inode))
  97. return -EAGAIN;
  98. } else {
  99. inode_lock_shared(inode);
  100. }
  101. /*
  102. * Recheck under inode lock - at this point we are sure it cannot
  103. * change anymore
  104. */
  105. if (!IS_DAX(inode)) {
  106. inode_unlock_shared(inode);
  107. /* Fallback to buffered IO in case we cannot support DAX */
  108. return generic_file_read_iter(iocb, to);
  109. }
  110. ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
  111. inode_unlock_shared(inode);
  112. file_accessed(iocb->ki_filp);
  113. return ret;
  114. }
  115. #endif
  116. static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  117. {
  118. struct inode *inode = file_inode(iocb->ki_filp);
  119. if (unlikely(ext4_forced_shutdown(inode->i_sb)))
  120. return -EIO;
  121. if (!iov_iter_count(to))
  122. return 0; /* skip atime */
  123. #ifdef CONFIG_FS_DAX
  124. if (IS_DAX(inode))
  125. return ext4_dax_read_iter(iocb, to);
  126. #endif
  127. if (iocb->ki_flags & IOCB_DIRECT)
  128. return ext4_dio_read_iter(iocb, to);
  129. return generic_file_read_iter(iocb, to);
  130. }
  131. static ssize_t ext4_file_splice_read(struct file *in, loff_t *ppos,
  132. struct pipe_inode_info *pipe,
  133. size_t len, unsigned int flags)
  134. {
  135. struct inode *inode = file_inode(in);
  136. if (unlikely(ext4_forced_shutdown(inode->i_sb)))
  137. return -EIO;
  138. return filemap_splice_read(in, ppos, pipe, len, flags);
  139. }
  140. /*
  141. * Called when an inode is released. Note that this is different
  142. * from ext4_file_open: open gets called at every open, but release
  143. * gets called only when /all/ the files are closed.
  144. */
  145. static int ext4_release_file(struct inode *inode, struct file *filp)
  146. {
  147. if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
  148. ext4_alloc_da_blocks(inode);
  149. ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
  150. }
  151. /* if we are the last writer on the inode, drop the block reservation */
  152. if ((filp->f_mode & FMODE_WRITE) &&
  153. (atomic_read(&inode->i_writecount) == 1) &&
  154. !EXT4_I(inode)->i_reserved_data_blocks) {
  155. down_write(&EXT4_I(inode)->i_data_sem);
  156. ext4_discard_preallocations(inode);
  157. up_write(&EXT4_I(inode)->i_data_sem);
  158. }
  159. if (is_dx(inode) && filp->private_data)
  160. ext4_htree_free_dir_info(filp->private_data);
  161. return 0;
  162. }
  163. /*
  164. * This tests whether the IO in question is block-aligned or not.
  165. * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
  166. * are converted to written only after the IO is complete. Until they are
  167. * mapped, these blocks appear as holes, so dio_zero_block() will assume that
  168. * it needs to zero out portions of the start and/or end block. If 2 AIO
  169. * threads are at work on the same unwritten block, they must be synchronized
  170. * or one thread will zero the other's data, causing corruption.
  171. */
  172. static bool
  173. ext4_unaligned_io(struct inode *inode, struct iov_iter *from, loff_t pos)
  174. {
  175. struct super_block *sb = inode->i_sb;
  176. unsigned long blockmask = sb->s_blocksize - 1;
  177. if ((pos | iov_iter_alignment(from)) & blockmask)
  178. return true;
  179. return false;
  180. }
  181. static bool
  182. ext4_extending_io(struct inode *inode, loff_t offset, size_t len)
  183. {
  184. if (offset + len > i_size_read(inode) ||
  185. offset + len > EXT4_I(inode)->i_disksize)
  186. return true;
  187. return false;
  188. }
  189. /* Is IO overwriting allocated or initialized blocks? */
  190. static bool ext4_overwrite_io(struct inode *inode,
  191. loff_t pos, loff_t len, bool *unwritten)
  192. {
  193. struct ext4_map_blocks map;
  194. unsigned int blkbits = inode->i_blkbits;
  195. int err, blklen;
  196. if (pos + len > i_size_read(inode))
  197. return false;
  198. map.m_lblk = pos >> blkbits;
  199. map.m_len = EXT4_MAX_BLOCKS(len, pos, blkbits);
  200. blklen = map.m_len;
  201. err = ext4_map_blocks(NULL, inode, &map, 0);
  202. if (err != blklen)
  203. return false;
  204. /*
  205. * 'err==len' means that all of the blocks have been preallocated,
  206. * regardless of whether they have been initialized or not. We need to
  207. * check m_flags to distinguish the unwritten extents.
  208. */
  209. *unwritten = !(map.m_flags & EXT4_MAP_MAPPED);
  210. return true;
  211. }
  212. static ssize_t ext4_generic_write_checks(struct kiocb *iocb,
  213. struct iov_iter *from)
  214. {
  215. struct inode *inode = file_inode(iocb->ki_filp);
  216. ssize_t ret;
  217. if (unlikely(IS_IMMUTABLE(inode)))
  218. return -EPERM;
  219. ret = generic_write_checks(iocb, from);
  220. if (ret <= 0)
  221. return ret;
  222. /*
  223. * If we have encountered a bitmap-format file, the size limit
  224. * is smaller than s_maxbytes, which is for extent-mapped files.
  225. */
  226. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
  227. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  228. if (iocb->ki_pos >= sbi->s_bitmap_maxbytes)
  229. return -EFBIG;
  230. iov_iter_truncate(from, sbi->s_bitmap_maxbytes - iocb->ki_pos);
  231. }
  232. return iov_iter_count(from);
  233. }
  234. static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
  235. {
  236. ssize_t ret, count;
  237. count = ext4_generic_write_checks(iocb, from);
  238. if (count <= 0)
  239. return count;
  240. ret = file_modified(iocb->ki_filp);
  241. if (ret)
  242. return ret;
  243. return count;
  244. }
  245. static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
  246. struct iov_iter *from)
  247. {
  248. ssize_t ret;
  249. struct inode *inode = file_inode(iocb->ki_filp);
  250. if (iocb->ki_flags & IOCB_NOWAIT)
  251. return -EOPNOTSUPP;
  252. inode_lock(inode);
  253. ret = ext4_write_checks(iocb, from);
  254. if (ret <= 0)
  255. goto out;
  256. ret = generic_perform_write(iocb, from);
  257. out:
  258. inode_unlock(inode);
  259. if (unlikely(ret <= 0))
  260. return ret;
  261. return generic_write_sync(iocb, ret);
  262. }
  263. static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset,
  264. ssize_t written, ssize_t count)
  265. {
  266. handle_t *handle;
  267. lockdep_assert_held_write(&inode->i_rwsem);
  268. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  269. if (IS_ERR(handle))
  270. return PTR_ERR(handle);
  271. if (ext4_update_inode_size(inode, offset + written)) {
  272. int ret = ext4_mark_inode_dirty(handle, inode);
  273. if (unlikely(ret)) {
  274. ext4_journal_stop(handle);
  275. return ret;
  276. }
  277. }
  278. if ((written == count) && inode->i_nlink)
  279. ext4_orphan_del(handle, inode);
  280. ext4_journal_stop(handle);
  281. return written;
  282. }
  283. /*
  284. * Clean up the inode after DIO or DAX extending write has completed and the
  285. * inode size has been updated using ext4_handle_inode_extension().
  286. */
  287. static void ext4_inode_extension_cleanup(struct inode *inode, bool need_trunc)
  288. {
  289. lockdep_assert_held_write(&inode->i_rwsem);
  290. if (need_trunc) {
  291. ext4_truncate_failed_write(inode);
  292. /*
  293. * If the truncate operation failed early, then the inode may
  294. * still be on the orphan list. In that case, we need to try
  295. * remove the inode from the in-memory linked list.
  296. */
  297. if (inode->i_nlink)
  298. ext4_orphan_del(NULL, inode);
  299. return;
  300. }
  301. /*
  302. * If i_disksize got extended either due to writeback of delalloc
  303. * blocks or extending truncate while the DIO was running we could fail
  304. * to cleanup the orphan list in ext4_handle_inode_extension(). Do it
  305. * now.
  306. */
  307. if (ext4_inode_orphan_tracked(inode) && inode->i_nlink) {
  308. handle_t *handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  309. if (IS_ERR(handle)) {
  310. /*
  311. * The write has successfully completed. Not much to
  312. * do with the error here so just cleanup the orphan
  313. * list and hope for the best.
  314. */
  315. ext4_orphan_del(NULL, inode);
  316. return;
  317. }
  318. ext4_orphan_del(handle, inode);
  319. ext4_journal_stop(handle);
  320. }
  321. }
  322. static int ext4_dio_write_end_io(struct kiocb *iocb, ssize_t size,
  323. int error, unsigned int flags)
  324. {
  325. loff_t pos = iocb->ki_pos;
  326. struct inode *inode = file_inode(iocb->ki_filp);
  327. if (!error && size && flags & IOMAP_DIO_UNWRITTEN)
  328. error = ext4_convert_unwritten_extents(NULL, inode, pos, size);
  329. if (error)
  330. return error;
  331. /*
  332. * Note that EXT4_I(inode)->i_disksize can get extended up to
  333. * inode->i_size while the I/O was running due to writeback of delalloc
  334. * blocks. But the code in ext4_iomap_alloc() is careful to use
  335. * zeroed/unwritten extents if this is possible; thus we won't leave
  336. * uninitialized blocks in a file even if we didn't succeed in writing
  337. * as much as we intended. Also we can race with truncate or write
  338. * expanding the file so we have to be a bit careful here.
  339. */
  340. if (pos + size <= READ_ONCE(EXT4_I(inode)->i_disksize) &&
  341. pos + size <= i_size_read(inode))
  342. return size;
  343. return ext4_handle_inode_extension(inode, pos, size, size);
  344. }
  345. static const struct iomap_dio_ops ext4_dio_write_ops = {
  346. .end_io = ext4_dio_write_end_io,
  347. };
  348. /*
  349. * The intention here is to start with shared lock acquired then see if any
  350. * condition requires an exclusive inode lock. If yes, then we restart the
  351. * whole operation by releasing the shared lock and acquiring exclusive lock.
  352. *
  353. * - For unaligned_io we never take shared lock as it may cause data corruption
  354. * when two unaligned IO tries to modify the same block e.g. while zeroing.
  355. *
  356. * - For extending writes case we don't take the shared lock, since it requires
  357. * updating inode i_disksize and/or orphan handling with exclusive lock.
  358. *
  359. * - shared locking will only be true mostly with overwrites, including
  360. * initialized blocks and unwritten blocks. For overwrite unwritten blocks
  361. * we protect splitting extents by i_data_sem in ext4_inode_info, so we can
  362. * also release exclusive i_rwsem lock.
  363. *
  364. * - Otherwise we will switch to exclusive i_rwsem lock.
  365. */
  366. static ssize_t ext4_dio_write_checks(struct kiocb *iocb, struct iov_iter *from,
  367. bool *ilock_shared, bool *extend,
  368. bool *unwritten, int *dio_flags)
  369. {
  370. struct file *file = iocb->ki_filp;
  371. struct inode *inode = file_inode(file);
  372. loff_t offset;
  373. size_t count;
  374. ssize_t ret;
  375. bool overwrite, unaligned_io;
  376. restart:
  377. ret = ext4_generic_write_checks(iocb, from);
  378. if (ret <= 0)
  379. goto out;
  380. offset = iocb->ki_pos;
  381. count = ret;
  382. unaligned_io = ext4_unaligned_io(inode, from, offset);
  383. *extend = ext4_extending_io(inode, offset, count);
  384. overwrite = ext4_overwrite_io(inode, offset, count, unwritten);
  385. /*
  386. * Determine whether we need to upgrade to an exclusive lock. This is
  387. * required to change security info in file_modified(), for extending
  388. * I/O, any form of non-overwrite I/O, and unaligned I/O to unwritten
  389. * extents (as partial block zeroing may be required).
  390. *
  391. * Note that unaligned writes are allowed under shared lock so long as
  392. * they are pure overwrites. Otherwise, concurrent unaligned writes risk
  393. * data corruption due to partial block zeroing in the dio layer, and so
  394. * the I/O must occur exclusively.
  395. */
  396. if (*ilock_shared &&
  397. ((!IS_NOSEC(inode) || *extend || !overwrite ||
  398. (unaligned_io && *unwritten)))) {
  399. if (iocb->ki_flags & IOCB_NOWAIT) {
  400. ret = -EAGAIN;
  401. goto out;
  402. }
  403. inode_unlock_shared(inode);
  404. *ilock_shared = false;
  405. inode_lock(inode);
  406. goto restart;
  407. }
  408. /*
  409. * Now that locking is settled, determine dio flags and exclusivity
  410. * requirements. We don't use DIO_OVERWRITE_ONLY because we enforce
  411. * behavior already. The inode lock is already held exclusive if the
  412. * write is non-overwrite or extending, so drain all outstanding dio and
  413. * set the force wait dio flag.
  414. */
  415. if (!*ilock_shared && (unaligned_io || *extend)) {
  416. if (iocb->ki_flags & IOCB_NOWAIT) {
  417. ret = -EAGAIN;
  418. goto out;
  419. }
  420. if (unaligned_io && (!overwrite || *unwritten))
  421. inode_dio_wait(inode);
  422. *dio_flags = IOMAP_DIO_FORCE_WAIT;
  423. }
  424. ret = file_modified(file);
  425. if (ret < 0)
  426. goto out;
  427. return count;
  428. out:
  429. if (*ilock_shared)
  430. inode_unlock_shared(inode);
  431. else
  432. inode_unlock(inode);
  433. return ret;
  434. }
  435. static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
  436. {
  437. ssize_t ret;
  438. handle_t *handle;
  439. struct inode *inode = file_inode(iocb->ki_filp);
  440. loff_t offset = iocb->ki_pos;
  441. size_t count = iov_iter_count(from);
  442. const struct iomap_ops *iomap_ops = &ext4_iomap_ops;
  443. bool extend = false, unwritten = false;
  444. bool ilock_shared = true;
  445. int dio_flags = 0;
  446. /*
  447. * Quick check here without any i_rwsem lock to see if it is extending
  448. * IO. A more reliable check is done in ext4_dio_write_checks() with
  449. * proper locking in place.
  450. */
  451. if (offset + count > i_size_read(inode))
  452. ilock_shared = false;
  453. if (iocb->ki_flags & IOCB_NOWAIT) {
  454. if (ilock_shared) {
  455. if (!inode_trylock_shared(inode))
  456. return -EAGAIN;
  457. } else {
  458. if (!inode_trylock(inode))
  459. return -EAGAIN;
  460. }
  461. } else {
  462. if (ilock_shared)
  463. inode_lock_shared(inode);
  464. else
  465. inode_lock(inode);
  466. }
  467. /* Fallback to buffered I/O if the inode does not support direct I/O. */
  468. if (!ext4_should_use_dio(iocb, from)) {
  469. if (ilock_shared)
  470. inode_unlock_shared(inode);
  471. else
  472. inode_unlock(inode);
  473. return ext4_buffered_write_iter(iocb, from);
  474. }
  475. /*
  476. * Prevent inline data from being created since we are going to allocate
  477. * blocks for DIO. We know the inode does not currently have inline data
  478. * because ext4_should_use_dio() checked for it, but we have to clear
  479. * the state flag before the write checks because a lock cycle could
  480. * introduce races with other writers.
  481. */
  482. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  483. ret = ext4_dio_write_checks(iocb, from, &ilock_shared, &extend,
  484. &unwritten, &dio_flags);
  485. if (ret <= 0)
  486. return ret;
  487. offset = iocb->ki_pos;
  488. count = ret;
  489. if (extend) {
  490. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  491. if (IS_ERR(handle)) {
  492. ret = PTR_ERR(handle);
  493. goto out;
  494. }
  495. ret = ext4_orphan_add(handle, inode);
  496. if (ret) {
  497. ext4_journal_stop(handle);
  498. goto out;
  499. }
  500. ext4_journal_stop(handle);
  501. }
  502. if (ilock_shared && !unwritten)
  503. iomap_ops = &ext4_iomap_overwrite_ops;
  504. ret = iomap_dio_rw(iocb, from, iomap_ops, &ext4_dio_write_ops,
  505. dio_flags, NULL, 0);
  506. if (ret == -ENOTBLK)
  507. ret = 0;
  508. if (extend) {
  509. /*
  510. * We always perform extending DIO write synchronously so by
  511. * now the IO is completed and ext4_handle_inode_extension()
  512. * was called. Cleanup the inode in case of error or race with
  513. * writeback of delalloc blocks.
  514. */
  515. WARN_ON_ONCE(ret == -EIOCBQUEUED);
  516. ext4_inode_extension_cleanup(inode, ret < 0);
  517. }
  518. out:
  519. if (ilock_shared)
  520. inode_unlock_shared(inode);
  521. else
  522. inode_unlock(inode);
  523. if (ret >= 0 && iov_iter_count(from)) {
  524. ssize_t err;
  525. loff_t endbyte;
  526. offset = iocb->ki_pos;
  527. err = ext4_buffered_write_iter(iocb, from);
  528. if (err < 0)
  529. return err;
  530. /*
  531. * We need to ensure that the pages within the page cache for
  532. * the range covered by this I/O are written to disk and
  533. * invalidated. This is in attempt to preserve the expected
  534. * direct I/O semantics in the case we fallback to buffered I/O
  535. * to complete off the I/O request.
  536. */
  537. ret += err;
  538. endbyte = offset + err - 1;
  539. err = filemap_write_and_wait_range(iocb->ki_filp->f_mapping,
  540. offset, endbyte);
  541. if (!err)
  542. invalidate_mapping_pages(iocb->ki_filp->f_mapping,
  543. offset >> PAGE_SHIFT,
  544. endbyte >> PAGE_SHIFT);
  545. }
  546. return ret;
  547. }
  548. #ifdef CONFIG_FS_DAX
  549. static ssize_t
  550. ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
  551. {
  552. ssize_t ret;
  553. size_t count;
  554. loff_t offset;
  555. handle_t *handle;
  556. bool extend = false;
  557. struct inode *inode = file_inode(iocb->ki_filp);
  558. if (iocb->ki_flags & IOCB_NOWAIT) {
  559. if (!inode_trylock(inode))
  560. return -EAGAIN;
  561. } else {
  562. inode_lock(inode);
  563. }
  564. ret = ext4_write_checks(iocb, from);
  565. if (ret <= 0)
  566. goto out;
  567. offset = iocb->ki_pos;
  568. count = iov_iter_count(from);
  569. if (offset + count > EXT4_I(inode)->i_disksize) {
  570. handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
  571. if (IS_ERR(handle)) {
  572. ret = PTR_ERR(handle);
  573. goto out;
  574. }
  575. ret = ext4_orphan_add(handle, inode);
  576. if (ret) {
  577. ext4_journal_stop(handle);
  578. goto out;
  579. }
  580. extend = true;
  581. ext4_journal_stop(handle);
  582. }
  583. ret = dax_iomap_rw(iocb, from, &ext4_iomap_ops);
  584. if (extend) {
  585. ret = ext4_handle_inode_extension(inode, offset, ret, count);
  586. ext4_inode_extension_cleanup(inode, ret < (ssize_t)count);
  587. }
  588. out:
  589. inode_unlock(inode);
  590. if (ret > 0)
  591. ret = generic_write_sync(iocb, ret);
  592. return ret;
  593. }
  594. #endif
  595. static ssize_t
  596. ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  597. {
  598. struct inode *inode = file_inode(iocb->ki_filp);
  599. if (unlikely(ext4_forced_shutdown(inode->i_sb)))
  600. return -EIO;
  601. #ifdef CONFIG_FS_DAX
  602. if (IS_DAX(inode))
  603. return ext4_dax_write_iter(iocb, from);
  604. #endif
  605. if (iocb->ki_flags & IOCB_DIRECT)
  606. return ext4_dio_write_iter(iocb, from);
  607. else
  608. return ext4_buffered_write_iter(iocb, from);
  609. }
  610. #ifdef CONFIG_FS_DAX
  611. static vm_fault_t ext4_dax_huge_fault(struct vm_fault *vmf, unsigned int order)
  612. {
  613. int error = 0;
  614. vm_fault_t result;
  615. int retries = 0;
  616. handle_t *handle = NULL;
  617. struct inode *inode = file_inode(vmf->vma->vm_file);
  618. struct super_block *sb = inode->i_sb;
  619. /*
  620. * We have to distinguish real writes from writes which will result in a
  621. * COW page; COW writes should *not* poke the journal (the file will not
  622. * be changed). Doing so would cause unintended failures when mounted
  623. * read-only.
  624. *
  625. * We check for VM_SHARED rather than vmf->cow_page since the latter is
  626. * unset for order != 0 (i.e. only in do_cow_fault); for
  627. * other sizes, dax_iomap_fault will handle splitting / fallback so that
  628. * we eventually come back with a COW page.
  629. */
  630. bool write = (vmf->flags & FAULT_FLAG_WRITE) &&
  631. (vmf->vma->vm_flags & VM_SHARED);
  632. struct address_space *mapping = vmf->vma->vm_file->f_mapping;
  633. pfn_t pfn;
  634. if (write) {
  635. sb_start_pagefault(sb);
  636. file_update_time(vmf->vma->vm_file);
  637. filemap_invalidate_lock_shared(mapping);
  638. retry:
  639. handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
  640. EXT4_DATA_TRANS_BLOCKS(sb));
  641. if (IS_ERR(handle)) {
  642. filemap_invalidate_unlock_shared(mapping);
  643. sb_end_pagefault(sb);
  644. return VM_FAULT_SIGBUS;
  645. }
  646. } else {
  647. filemap_invalidate_lock_shared(mapping);
  648. }
  649. result = dax_iomap_fault(vmf, order, &pfn, &error, &ext4_iomap_ops);
  650. if (write) {
  651. ext4_journal_stop(handle);
  652. if ((result & VM_FAULT_ERROR) && error == -ENOSPC &&
  653. ext4_should_retry_alloc(sb, &retries))
  654. goto retry;
  655. /* Handling synchronous page fault? */
  656. if (result & VM_FAULT_NEEDDSYNC)
  657. result = dax_finish_sync_fault(vmf, order, pfn);
  658. filemap_invalidate_unlock_shared(mapping);
  659. sb_end_pagefault(sb);
  660. } else {
  661. filemap_invalidate_unlock_shared(mapping);
  662. }
  663. return result;
  664. }
  665. static vm_fault_t ext4_dax_fault(struct vm_fault *vmf)
  666. {
  667. return ext4_dax_huge_fault(vmf, 0);
  668. }
  669. static const struct vm_operations_struct ext4_dax_vm_ops = {
  670. .fault = ext4_dax_fault,
  671. .huge_fault = ext4_dax_huge_fault,
  672. .page_mkwrite = ext4_dax_fault,
  673. .pfn_mkwrite = ext4_dax_fault,
  674. };
  675. #else
  676. #define ext4_dax_vm_ops ext4_file_vm_ops
  677. #endif
  678. static const struct vm_operations_struct ext4_file_vm_ops = {
  679. .fault = filemap_fault,
  680. .map_pages = filemap_map_pages,
  681. .page_mkwrite = ext4_page_mkwrite,
  682. };
  683. static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
  684. {
  685. struct inode *inode = file->f_mapping->host;
  686. struct dax_device *dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
  687. if (unlikely(ext4_forced_shutdown(inode->i_sb)))
  688. return -EIO;
  689. /*
  690. * We don't support synchronous mappings for non-DAX files and
  691. * for DAX files if underneath dax_device is not synchronous.
  692. */
  693. if (!daxdev_mapping_supported(vma, dax_dev))
  694. return -EOPNOTSUPP;
  695. file_accessed(file);
  696. if (IS_DAX(file_inode(file))) {
  697. vma->vm_ops = &ext4_dax_vm_ops;
  698. vm_flags_set(vma, VM_HUGEPAGE);
  699. } else {
  700. vma->vm_ops = &ext4_file_vm_ops;
  701. }
  702. return 0;
  703. }
  704. static int ext4_sample_last_mounted(struct super_block *sb,
  705. struct vfsmount *mnt)
  706. {
  707. struct ext4_sb_info *sbi = EXT4_SB(sb);
  708. struct path path;
  709. char buf[64], *cp;
  710. handle_t *handle;
  711. int err;
  712. if (likely(ext4_test_mount_flag(sb, EXT4_MF_MNTDIR_SAMPLED)))
  713. return 0;
  714. if (sb_rdonly(sb) || !sb_start_intwrite_trylock(sb))
  715. return 0;
  716. ext4_set_mount_flag(sb, EXT4_MF_MNTDIR_SAMPLED);
  717. /*
  718. * Sample where the filesystem has been mounted and
  719. * store it in the superblock for sysadmin convenience
  720. * when trying to sort through large numbers of block
  721. * devices or filesystem images.
  722. */
  723. memset(buf, 0, sizeof(buf));
  724. path.mnt = mnt;
  725. path.dentry = mnt->mnt_root;
  726. cp = d_path(&path, buf, sizeof(buf));
  727. err = 0;
  728. if (IS_ERR(cp))
  729. goto out;
  730. handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
  731. err = PTR_ERR(handle);
  732. if (IS_ERR(handle))
  733. goto out;
  734. BUFFER_TRACE(sbi->s_sbh, "get_write_access");
  735. err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
  736. EXT4_JTR_NONE);
  737. if (err)
  738. goto out_journal;
  739. lock_buffer(sbi->s_sbh);
  740. strtomem_pad(sbi->s_es->s_last_mounted, cp, 0);
  741. ext4_superblock_csum_set(sb);
  742. unlock_buffer(sbi->s_sbh);
  743. ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
  744. out_journal:
  745. ext4_journal_stop(handle);
  746. out:
  747. sb_end_intwrite(sb);
  748. return err;
  749. }
  750. static int ext4_file_open(struct inode *inode, struct file *filp)
  751. {
  752. int ret;
  753. if (unlikely(ext4_forced_shutdown(inode->i_sb)))
  754. return -EIO;
  755. ret = ext4_sample_last_mounted(inode->i_sb, filp->f_path.mnt);
  756. if (ret)
  757. return ret;
  758. ret = fscrypt_file_open(inode, filp);
  759. if (ret)
  760. return ret;
  761. ret = fsverity_file_open(inode, filp);
  762. if (ret)
  763. return ret;
  764. /*
  765. * Set up the jbd2_inode if we are opening the inode for
  766. * writing and the journal is present
  767. */
  768. if (filp->f_mode & FMODE_WRITE) {
  769. ret = ext4_inode_attach_jinode(inode);
  770. if (ret < 0)
  771. return ret;
  772. }
  773. filp->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
  774. return dquot_file_open(inode, filp);
  775. }
  776. /*
  777. * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
  778. * by calling generic_file_llseek_size() with the appropriate maxbytes
  779. * value for each.
  780. */
  781. loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
  782. {
  783. struct inode *inode = file->f_mapping->host;
  784. loff_t maxbytes = ext4_get_maxbytes(inode);
  785. switch (whence) {
  786. default:
  787. return generic_file_llseek_size(file, offset, whence,
  788. maxbytes, i_size_read(inode));
  789. case SEEK_HOLE:
  790. inode_lock_shared(inode);
  791. offset = iomap_seek_hole(inode, offset,
  792. &ext4_iomap_report_ops);
  793. inode_unlock_shared(inode);
  794. break;
  795. case SEEK_DATA:
  796. inode_lock_shared(inode);
  797. offset = iomap_seek_data(inode, offset,
  798. &ext4_iomap_report_ops);
  799. inode_unlock_shared(inode);
  800. break;
  801. }
  802. if (offset < 0)
  803. return offset;
  804. return vfs_setpos(file, offset, maxbytes);
  805. }
  806. const struct file_operations ext4_file_operations = {
  807. .llseek = ext4_llseek,
  808. .read_iter = ext4_file_read_iter,
  809. .write_iter = ext4_file_write_iter,
  810. .iopoll = iocb_bio_iopoll,
  811. .unlocked_ioctl = ext4_ioctl,
  812. #ifdef CONFIG_COMPAT
  813. .compat_ioctl = ext4_compat_ioctl,
  814. #endif
  815. .mmap = ext4_file_mmap,
  816. .open = ext4_file_open,
  817. .release = ext4_release_file,
  818. .fsync = ext4_sync_file,
  819. .get_unmapped_area = thp_get_unmapped_area,
  820. .splice_read = ext4_file_splice_read,
  821. .splice_write = iter_file_splice_write,
  822. .fallocate = ext4_fallocate,
  823. .fop_flags = FOP_MMAP_SYNC | FOP_BUFFER_RASYNC |
  824. FOP_DIO_PARALLEL_WRITE,
  825. };
  826. const struct inode_operations ext4_file_inode_operations = {
  827. .setattr = ext4_setattr,
  828. .getattr = ext4_file_getattr,
  829. .listxattr = ext4_listxattr,
  830. .get_inode_acl = ext4_get_acl,
  831. .set_acl = ext4_set_acl,
  832. .fiemap = ext4_fiemap,
  833. .fileattr_get = ext4_fileattr_get,
  834. .fileattr_set = ext4_fileattr_set,
  835. };