file.c 26 KB

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