xfs_file.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_da_format.h"
  14. #include "xfs_da_btree.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_trans.h"
  17. #include "xfs_inode_item.h"
  18. #include "xfs_bmap.h"
  19. #include "xfs_bmap_util.h"
  20. #include "xfs_error.h"
  21. #include "xfs_dir2.h"
  22. #include "xfs_dir2_priv.h"
  23. #include "xfs_ioctl.h"
  24. #include "xfs_trace.h"
  25. #include "xfs_log.h"
  26. #include "xfs_icache.h"
  27. #include "xfs_pnfs.h"
  28. #include "xfs_iomap.h"
  29. #include "xfs_reflink.h"
  30. #include <linux/dcache.h>
  31. #include <linux/falloc.h>
  32. #include <linux/pagevec.h>
  33. #include <linux/backing-dev.h>
  34. #include <linux/mman.h>
  35. static const struct vm_operations_struct xfs_file_vm_ops;
  36. int
  37. xfs_update_prealloc_flags(
  38. struct xfs_inode *ip,
  39. enum xfs_prealloc_flags flags)
  40. {
  41. struct xfs_trans *tp;
  42. int error;
  43. error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid,
  44. 0, 0, 0, &tp);
  45. if (error)
  46. return error;
  47. xfs_ilock(ip, XFS_ILOCK_EXCL);
  48. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  49. if (!(flags & XFS_PREALLOC_INVISIBLE)) {
  50. VFS_I(ip)->i_mode &= ~S_ISUID;
  51. if (VFS_I(ip)->i_mode & S_IXGRP)
  52. VFS_I(ip)->i_mode &= ~S_ISGID;
  53. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  54. }
  55. if (flags & XFS_PREALLOC_SET)
  56. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  57. if (flags & XFS_PREALLOC_CLEAR)
  58. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  59. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  60. if (flags & XFS_PREALLOC_SYNC)
  61. xfs_trans_set_sync(tp);
  62. return xfs_trans_commit(tp);
  63. }
  64. /*
  65. * Fsync operations on directories are much simpler than on regular files,
  66. * as there is no file data to flush, and thus also no need for explicit
  67. * cache flush operations, and there are no non-transaction metadata updates
  68. * on directories either.
  69. */
  70. STATIC int
  71. xfs_dir_fsync(
  72. struct file *file,
  73. loff_t start,
  74. loff_t end,
  75. int datasync)
  76. {
  77. struct xfs_inode *ip = XFS_I(file->f_mapping->host);
  78. struct xfs_mount *mp = ip->i_mount;
  79. xfs_lsn_t lsn = 0;
  80. trace_xfs_dir_fsync(ip);
  81. xfs_ilock(ip, XFS_ILOCK_SHARED);
  82. if (xfs_ipincount(ip))
  83. lsn = ip->i_itemp->ili_last_lsn;
  84. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  85. if (!lsn)
  86. return 0;
  87. return xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
  88. }
  89. STATIC int
  90. xfs_file_fsync(
  91. struct file *file,
  92. loff_t start,
  93. loff_t end,
  94. int datasync)
  95. {
  96. struct inode *inode = file->f_mapping->host;
  97. struct xfs_inode *ip = XFS_I(inode);
  98. struct xfs_mount *mp = ip->i_mount;
  99. int error = 0;
  100. int log_flushed = 0;
  101. xfs_lsn_t lsn = 0;
  102. trace_xfs_file_fsync(ip);
  103. error = file_write_and_wait_range(file, start, end);
  104. if (error)
  105. return error;
  106. if (XFS_FORCED_SHUTDOWN(mp))
  107. return -EIO;
  108. xfs_iflags_clear(ip, XFS_ITRUNCATED);
  109. /*
  110. * If we have an RT and/or log subvolume we need to make sure to flush
  111. * the write cache the device used for file data first. This is to
  112. * ensure newly written file data make it to disk before logging the new
  113. * inode size in case of an extending write.
  114. */
  115. if (XFS_IS_REALTIME_INODE(ip))
  116. xfs_blkdev_issue_flush(mp->m_rtdev_targp);
  117. else if (mp->m_logdev_targp != mp->m_ddev_targp)
  118. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  119. /*
  120. * All metadata updates are logged, which means that we just have to
  121. * flush the log up to the latest LSN that touched the inode. If we have
  122. * concurrent fsync/fdatasync() calls, we need them to all block on the
  123. * log force before we clear the ili_fsync_fields field. This ensures
  124. * that we don't get a racing sync operation that does not wait for the
  125. * metadata to hit the journal before returning. If we race with
  126. * clearing the ili_fsync_fields, then all that will happen is the log
  127. * force will do nothing as the lsn will already be on disk. We can't
  128. * race with setting ili_fsync_fields because that is done under
  129. * XFS_ILOCK_EXCL, and that can't happen because we hold the lock shared
  130. * until after the ili_fsync_fields is cleared.
  131. */
  132. xfs_ilock(ip, XFS_ILOCK_SHARED);
  133. if (xfs_ipincount(ip)) {
  134. if (!datasync ||
  135. (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP))
  136. lsn = ip->i_itemp->ili_last_lsn;
  137. }
  138. if (lsn) {
  139. error = xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
  140. ip->i_itemp->ili_fsync_fields = 0;
  141. }
  142. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  143. /*
  144. * If we only have a single device, and the log force about was
  145. * a no-op we might have to flush the data device cache here.
  146. * This can only happen for fdatasync/O_DSYNC if we were overwriting
  147. * an already allocated file and thus do not have any metadata to
  148. * commit.
  149. */
  150. if (!log_flushed && !XFS_IS_REALTIME_INODE(ip) &&
  151. mp->m_logdev_targp == mp->m_ddev_targp)
  152. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  153. return error;
  154. }
  155. STATIC ssize_t
  156. xfs_file_dio_aio_read(
  157. struct kiocb *iocb,
  158. struct iov_iter *to)
  159. {
  160. struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
  161. size_t count = iov_iter_count(to);
  162. ssize_t ret;
  163. trace_xfs_file_direct_read(ip, count, iocb->ki_pos);
  164. if (!count)
  165. return 0; /* skip atime */
  166. file_accessed(iocb->ki_filp);
  167. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  168. ret = iomap_dio_rw(iocb, to, &xfs_iomap_ops, NULL);
  169. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  170. return ret;
  171. }
  172. static noinline ssize_t
  173. xfs_file_dax_read(
  174. struct kiocb *iocb,
  175. struct iov_iter *to)
  176. {
  177. struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host);
  178. size_t count = iov_iter_count(to);
  179. ssize_t ret = 0;
  180. trace_xfs_file_dax_read(ip, count, iocb->ki_pos);
  181. if (!count)
  182. return 0; /* skip atime */
  183. if (iocb->ki_flags & IOCB_NOWAIT) {
  184. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
  185. return -EAGAIN;
  186. } else {
  187. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  188. }
  189. ret = dax_iomap_rw(iocb, to, &xfs_iomap_ops);
  190. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  191. file_accessed(iocb->ki_filp);
  192. return ret;
  193. }
  194. STATIC ssize_t
  195. xfs_file_buffered_aio_read(
  196. struct kiocb *iocb,
  197. struct iov_iter *to)
  198. {
  199. struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
  200. ssize_t ret;
  201. trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
  202. if (iocb->ki_flags & IOCB_NOWAIT) {
  203. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED))
  204. return -EAGAIN;
  205. } else {
  206. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  207. }
  208. ret = generic_file_read_iter(iocb, to);
  209. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  210. return ret;
  211. }
  212. STATIC ssize_t
  213. xfs_file_read_iter(
  214. struct kiocb *iocb,
  215. struct iov_iter *to)
  216. {
  217. struct inode *inode = file_inode(iocb->ki_filp);
  218. struct xfs_mount *mp = XFS_I(inode)->i_mount;
  219. ssize_t ret = 0;
  220. XFS_STATS_INC(mp, xs_read_calls);
  221. if (XFS_FORCED_SHUTDOWN(mp))
  222. return -EIO;
  223. if (IS_DAX(inode))
  224. ret = xfs_file_dax_read(iocb, to);
  225. else if (iocb->ki_flags & IOCB_DIRECT)
  226. ret = xfs_file_dio_aio_read(iocb, to);
  227. else
  228. ret = xfs_file_buffered_aio_read(iocb, to);
  229. if (ret > 0)
  230. XFS_STATS_ADD(mp, xs_read_bytes, ret);
  231. return ret;
  232. }
  233. /*
  234. * Common pre-write limit and setup checks.
  235. *
  236. * Called with the iolocked held either shared and exclusive according to
  237. * @iolock, and returns with it held. Might upgrade the iolock to exclusive
  238. * if called for a direct write beyond i_size.
  239. */
  240. STATIC ssize_t
  241. xfs_file_aio_write_checks(
  242. struct kiocb *iocb,
  243. struct iov_iter *from,
  244. int *iolock)
  245. {
  246. struct file *file = iocb->ki_filp;
  247. struct inode *inode = file->f_mapping->host;
  248. struct xfs_inode *ip = XFS_I(inode);
  249. ssize_t error = 0;
  250. size_t count = iov_iter_count(from);
  251. bool drained_dio = false;
  252. loff_t isize;
  253. restart:
  254. error = generic_write_checks(iocb, from);
  255. if (error <= 0)
  256. return error;
  257. error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
  258. if (error)
  259. return error;
  260. /*
  261. * For changing security info in file_remove_privs() we need i_rwsem
  262. * exclusively.
  263. */
  264. if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
  265. xfs_iunlock(ip, *iolock);
  266. *iolock = XFS_IOLOCK_EXCL;
  267. xfs_ilock(ip, *iolock);
  268. goto restart;
  269. }
  270. /*
  271. * If the offset is beyond the size of the file, we need to zero any
  272. * blocks that fall between the existing EOF and the start of this
  273. * write. If zeroing is needed and we are currently holding the
  274. * iolock shared, we need to update it to exclusive which implies
  275. * having to redo all checks before.
  276. *
  277. * We need to serialise against EOF updates that occur in IO
  278. * completions here. We want to make sure that nobody is changing the
  279. * size while we do this check until we have placed an IO barrier (i.e.
  280. * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
  281. * The spinlock effectively forms a memory barrier once we have the
  282. * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
  283. * and hence be able to correctly determine if we need to run zeroing.
  284. */
  285. spin_lock(&ip->i_flags_lock);
  286. isize = i_size_read(inode);
  287. if (iocb->ki_pos > isize) {
  288. spin_unlock(&ip->i_flags_lock);
  289. if (!drained_dio) {
  290. if (*iolock == XFS_IOLOCK_SHARED) {
  291. xfs_iunlock(ip, *iolock);
  292. *iolock = XFS_IOLOCK_EXCL;
  293. xfs_ilock(ip, *iolock);
  294. iov_iter_reexpand(from, count);
  295. }
  296. /*
  297. * We now have an IO submission barrier in place, but
  298. * AIO can do EOF updates during IO completion and hence
  299. * we now need to wait for all of them to drain. Non-AIO
  300. * DIO will have drained before we are given the
  301. * XFS_IOLOCK_EXCL, and so for most cases this wait is a
  302. * no-op.
  303. */
  304. inode_dio_wait(inode);
  305. drained_dio = true;
  306. goto restart;
  307. }
  308. trace_xfs_zero_eof(ip, isize, iocb->ki_pos - isize);
  309. error = iomap_zero_range(inode, isize, iocb->ki_pos - isize,
  310. NULL, &xfs_iomap_ops);
  311. if (error)
  312. return error;
  313. } else
  314. spin_unlock(&ip->i_flags_lock);
  315. /*
  316. * Updating the timestamps will grab the ilock again from
  317. * xfs_fs_dirty_inode, so we have to call it after dropping the
  318. * lock above. Eventually we should look into a way to avoid
  319. * the pointless lock roundtrip.
  320. */
  321. if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
  322. error = file_update_time(file);
  323. if (error)
  324. return error;
  325. }
  326. /*
  327. * If we're writing the file then make sure to clear the setuid and
  328. * setgid bits if the process is not being run by root. This keeps
  329. * people from modifying setuid and setgid binaries.
  330. */
  331. if (!IS_NOSEC(inode))
  332. return file_remove_privs(file);
  333. return 0;
  334. }
  335. static int
  336. xfs_dio_write_end_io(
  337. struct kiocb *iocb,
  338. ssize_t size,
  339. unsigned flags)
  340. {
  341. struct inode *inode = file_inode(iocb->ki_filp);
  342. struct xfs_inode *ip = XFS_I(inode);
  343. loff_t offset = iocb->ki_pos;
  344. int error = 0;
  345. trace_xfs_end_io_direct_write(ip, offset, size);
  346. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  347. return -EIO;
  348. if (size <= 0)
  349. return size;
  350. /*
  351. * Capture amount written on completion as we can't reliably account
  352. * for it on submission.
  353. */
  354. XFS_STATS_ADD(ip->i_mount, xs_write_bytes, size);
  355. if (flags & IOMAP_DIO_COW) {
  356. error = xfs_reflink_end_cow(ip, offset, size);
  357. if (error)
  358. return error;
  359. }
  360. /*
  361. * Unwritten conversion updates the in-core isize after extent
  362. * conversion but before updating the on-disk size. Updating isize any
  363. * earlier allows a racing dio read to find unwritten extents before
  364. * they are converted.
  365. */
  366. if (flags & IOMAP_DIO_UNWRITTEN)
  367. return xfs_iomap_write_unwritten(ip, offset, size, true);
  368. /*
  369. * We need to update the in-core inode size here so that we don't end up
  370. * with the on-disk inode size being outside the in-core inode size. We
  371. * have no other method of updating EOF for AIO, so always do it here
  372. * if necessary.
  373. *
  374. * We need to lock the test/set EOF update as we can be racing with
  375. * other IO completions here to update the EOF. Failing to serialise
  376. * here can result in EOF moving backwards and Bad Things Happen when
  377. * that occurs.
  378. */
  379. spin_lock(&ip->i_flags_lock);
  380. if (offset + size > i_size_read(inode)) {
  381. i_size_write(inode, offset + size);
  382. spin_unlock(&ip->i_flags_lock);
  383. error = xfs_setfilesize(ip, offset, size);
  384. } else {
  385. spin_unlock(&ip->i_flags_lock);
  386. }
  387. return error;
  388. }
  389. /*
  390. * xfs_file_dio_aio_write - handle direct IO writes
  391. *
  392. * Lock the inode appropriately to prepare for and issue a direct IO write.
  393. * By separating it from the buffered write path we remove all the tricky to
  394. * follow locking changes and looping.
  395. *
  396. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  397. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  398. * pages are flushed out.
  399. *
  400. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  401. * allowing them to be done in parallel with reads and other direct IO writes.
  402. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  403. * needs to do sub-block zeroing and that requires serialisation against other
  404. * direct IOs to the same block. In this case we need to serialise the
  405. * submission of the unaligned IOs so that we don't get racing block zeroing in
  406. * the dio layer. To avoid the problem with aio, we also need to wait for
  407. * outstanding IOs to complete so that unwritten extent conversion is completed
  408. * before we try to map the overlapping block. This is currently implemented by
  409. * hitting it with a big hammer (i.e. inode_dio_wait()).
  410. *
  411. * Returns with locks held indicated by @iolock and errors indicated by
  412. * negative return values.
  413. */
  414. STATIC ssize_t
  415. xfs_file_dio_aio_write(
  416. struct kiocb *iocb,
  417. struct iov_iter *from)
  418. {
  419. struct file *file = iocb->ki_filp;
  420. struct address_space *mapping = file->f_mapping;
  421. struct inode *inode = mapping->host;
  422. struct xfs_inode *ip = XFS_I(inode);
  423. struct xfs_mount *mp = ip->i_mount;
  424. ssize_t ret = 0;
  425. int unaligned_io = 0;
  426. int iolock;
  427. size_t count = iov_iter_count(from);
  428. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  429. mp->m_rtdev_targp : mp->m_ddev_targp;
  430. /* DIO must be aligned to device logical sector size */
  431. if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
  432. return -EINVAL;
  433. /*
  434. * Don't take the exclusive iolock here unless the I/O is unaligned to
  435. * the file system block size. We don't need to consider the EOF
  436. * extension case here because xfs_file_aio_write_checks() will relock
  437. * the inode as necessary for EOF zeroing cases and fill out the new
  438. * inode size as appropriate.
  439. */
  440. if ((iocb->ki_pos & mp->m_blockmask) ||
  441. ((iocb->ki_pos + count) & mp->m_blockmask)) {
  442. unaligned_io = 1;
  443. /*
  444. * We can't properly handle unaligned direct I/O to reflink
  445. * files yet, as we can't unshare a partial block.
  446. */
  447. if (xfs_is_reflink_inode(ip)) {
  448. trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
  449. return -EREMCHG;
  450. }
  451. iolock = XFS_IOLOCK_EXCL;
  452. } else {
  453. iolock = XFS_IOLOCK_SHARED;
  454. }
  455. if (iocb->ki_flags & IOCB_NOWAIT) {
  456. if (!xfs_ilock_nowait(ip, iolock))
  457. return -EAGAIN;
  458. } else {
  459. xfs_ilock(ip, iolock);
  460. }
  461. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  462. if (ret)
  463. goto out;
  464. count = iov_iter_count(from);
  465. /*
  466. * If we are doing unaligned IO, wait for all other IO to drain,
  467. * otherwise demote the lock if we had to take the exclusive lock
  468. * for other reasons in xfs_file_aio_write_checks.
  469. */
  470. if (unaligned_io) {
  471. /* If we are going to wait for other DIO to finish, bail */
  472. if (iocb->ki_flags & IOCB_NOWAIT) {
  473. if (atomic_read(&inode->i_dio_count))
  474. return -EAGAIN;
  475. } else {
  476. inode_dio_wait(inode);
  477. }
  478. } else if (iolock == XFS_IOLOCK_EXCL) {
  479. xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
  480. iolock = XFS_IOLOCK_SHARED;
  481. }
  482. trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
  483. ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, xfs_dio_write_end_io);
  484. out:
  485. xfs_iunlock(ip, iolock);
  486. /*
  487. * No fallback to buffered IO on errors for XFS, direct IO will either
  488. * complete fully or fail.
  489. */
  490. ASSERT(ret < 0 || ret == count);
  491. return ret;
  492. }
  493. static noinline ssize_t
  494. xfs_file_dax_write(
  495. struct kiocb *iocb,
  496. struct iov_iter *from)
  497. {
  498. struct inode *inode = iocb->ki_filp->f_mapping->host;
  499. struct xfs_inode *ip = XFS_I(inode);
  500. int iolock = XFS_IOLOCK_EXCL;
  501. ssize_t ret, error = 0;
  502. size_t count;
  503. loff_t pos;
  504. if (iocb->ki_flags & IOCB_NOWAIT) {
  505. if (!xfs_ilock_nowait(ip, iolock))
  506. return -EAGAIN;
  507. } else {
  508. xfs_ilock(ip, iolock);
  509. }
  510. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  511. if (ret)
  512. goto out;
  513. pos = iocb->ki_pos;
  514. count = iov_iter_count(from);
  515. trace_xfs_file_dax_write(ip, count, pos);
  516. ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops);
  517. if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
  518. i_size_write(inode, iocb->ki_pos);
  519. error = xfs_setfilesize(ip, pos, ret);
  520. }
  521. out:
  522. xfs_iunlock(ip, iolock);
  523. if (error)
  524. return error;
  525. if (ret > 0) {
  526. XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
  527. /* Handle various SYNC-type writes */
  528. ret = generic_write_sync(iocb, ret);
  529. }
  530. return ret;
  531. }
  532. STATIC ssize_t
  533. xfs_file_buffered_aio_write(
  534. struct kiocb *iocb,
  535. struct iov_iter *from)
  536. {
  537. struct file *file = iocb->ki_filp;
  538. struct address_space *mapping = file->f_mapping;
  539. struct inode *inode = mapping->host;
  540. struct xfs_inode *ip = XFS_I(inode);
  541. ssize_t ret;
  542. int enospc = 0;
  543. int iolock;
  544. if (iocb->ki_flags & IOCB_NOWAIT)
  545. return -EOPNOTSUPP;
  546. write_retry:
  547. iolock = XFS_IOLOCK_EXCL;
  548. xfs_ilock(ip, iolock);
  549. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  550. if (ret)
  551. goto out;
  552. /* We can write back this queue in page reclaim */
  553. current->backing_dev_info = inode_to_bdi(inode);
  554. trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
  555. ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops);
  556. if (likely(ret >= 0))
  557. iocb->ki_pos += ret;
  558. /*
  559. * If we hit a space limit, try to free up some lingering preallocated
  560. * space before returning an error. In the case of ENOSPC, first try to
  561. * write back all dirty inodes to free up some of the excess reserved
  562. * metadata space. This reduces the chances that the eofblocks scan
  563. * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
  564. * also behaves as a filter to prevent too many eofblocks scans from
  565. * running at the same time.
  566. */
  567. if (ret == -EDQUOT && !enospc) {
  568. xfs_iunlock(ip, iolock);
  569. enospc = xfs_inode_free_quota_eofblocks(ip);
  570. if (enospc)
  571. goto write_retry;
  572. enospc = xfs_inode_free_quota_cowblocks(ip);
  573. if (enospc)
  574. goto write_retry;
  575. iolock = 0;
  576. } else if (ret == -ENOSPC && !enospc) {
  577. struct xfs_eofblocks eofb = {0};
  578. enospc = 1;
  579. xfs_flush_inodes(ip->i_mount);
  580. xfs_iunlock(ip, iolock);
  581. eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
  582. xfs_icache_free_eofblocks(ip->i_mount, &eofb);
  583. xfs_icache_free_cowblocks(ip->i_mount, &eofb);
  584. goto write_retry;
  585. }
  586. current->backing_dev_info = NULL;
  587. out:
  588. if (iolock)
  589. xfs_iunlock(ip, iolock);
  590. if (ret > 0) {
  591. XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
  592. /* Handle various SYNC-type writes */
  593. ret = generic_write_sync(iocb, ret);
  594. }
  595. return ret;
  596. }
  597. STATIC ssize_t
  598. xfs_file_write_iter(
  599. struct kiocb *iocb,
  600. struct iov_iter *from)
  601. {
  602. struct file *file = iocb->ki_filp;
  603. struct address_space *mapping = file->f_mapping;
  604. struct inode *inode = mapping->host;
  605. struct xfs_inode *ip = XFS_I(inode);
  606. ssize_t ret;
  607. size_t ocount = iov_iter_count(from);
  608. XFS_STATS_INC(ip->i_mount, xs_write_calls);
  609. if (ocount == 0)
  610. return 0;
  611. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  612. return -EIO;
  613. if (IS_DAX(inode))
  614. return xfs_file_dax_write(iocb, from);
  615. if (iocb->ki_flags & IOCB_DIRECT) {
  616. /*
  617. * Allow a directio write to fall back to a buffered
  618. * write *only* in the case that we're doing a reflink
  619. * CoW. In all other directio scenarios we do not
  620. * allow an operation to fall back to buffered mode.
  621. */
  622. ret = xfs_file_dio_aio_write(iocb, from);
  623. if (ret != -EREMCHG)
  624. return ret;
  625. }
  626. return xfs_file_buffered_aio_write(iocb, from);
  627. }
  628. static void
  629. xfs_wait_dax_page(
  630. struct inode *inode,
  631. bool *did_unlock)
  632. {
  633. struct xfs_inode *ip = XFS_I(inode);
  634. *did_unlock = true;
  635. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  636. schedule();
  637. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  638. }
  639. static int
  640. xfs_break_dax_layouts(
  641. struct inode *inode,
  642. uint iolock,
  643. bool *did_unlock)
  644. {
  645. struct page *page;
  646. ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
  647. page = dax_layout_busy_page(inode->i_mapping);
  648. if (!page)
  649. return 0;
  650. return ___wait_var_event(&page->_refcount,
  651. atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
  652. 0, 0, xfs_wait_dax_page(inode, did_unlock));
  653. }
  654. int
  655. xfs_break_layouts(
  656. struct inode *inode,
  657. uint *iolock,
  658. enum layout_break_reason reason)
  659. {
  660. bool retry;
  661. int error;
  662. ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
  663. do {
  664. retry = false;
  665. switch (reason) {
  666. case BREAK_UNMAP:
  667. error = xfs_break_dax_layouts(inode, *iolock, &retry);
  668. if (error || retry)
  669. break;
  670. /* fall through */
  671. case BREAK_WRITE:
  672. error = xfs_break_leased_layouts(inode, iolock, &retry);
  673. break;
  674. default:
  675. WARN_ON_ONCE(1);
  676. error = -EINVAL;
  677. }
  678. } while (error == 0 && retry);
  679. return error;
  680. }
  681. #define XFS_FALLOC_FL_SUPPORTED \
  682. (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
  683. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
  684. FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
  685. STATIC long
  686. xfs_file_fallocate(
  687. struct file *file,
  688. int mode,
  689. loff_t offset,
  690. loff_t len)
  691. {
  692. struct inode *inode = file_inode(file);
  693. struct xfs_inode *ip = XFS_I(inode);
  694. long error;
  695. enum xfs_prealloc_flags flags = 0;
  696. uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
  697. loff_t new_size = 0;
  698. bool do_file_insert = false;
  699. if (!S_ISREG(inode->i_mode))
  700. return -EINVAL;
  701. if (mode & ~XFS_FALLOC_FL_SUPPORTED)
  702. return -EOPNOTSUPP;
  703. xfs_ilock(ip, iolock);
  704. error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
  705. if (error)
  706. goto out_unlock;
  707. if (mode & FALLOC_FL_PUNCH_HOLE) {
  708. error = xfs_free_file_space(ip, offset, len);
  709. if (error)
  710. goto out_unlock;
  711. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  712. unsigned int blksize_mask = i_blocksize(inode) - 1;
  713. if (offset & blksize_mask || len & blksize_mask) {
  714. error = -EINVAL;
  715. goto out_unlock;
  716. }
  717. /*
  718. * There is no need to overlap collapse range with EOF,
  719. * in which case it is effectively a truncate operation
  720. */
  721. if (offset + len >= i_size_read(inode)) {
  722. error = -EINVAL;
  723. goto out_unlock;
  724. }
  725. new_size = i_size_read(inode) - len;
  726. error = xfs_collapse_file_space(ip, offset, len);
  727. if (error)
  728. goto out_unlock;
  729. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  730. unsigned int blksize_mask = i_blocksize(inode) - 1;
  731. loff_t isize = i_size_read(inode);
  732. if (offset & blksize_mask || len & blksize_mask) {
  733. error = -EINVAL;
  734. goto out_unlock;
  735. }
  736. /*
  737. * New inode size must not exceed ->s_maxbytes, accounting for
  738. * possible signed overflow.
  739. */
  740. if (inode->i_sb->s_maxbytes - isize < len) {
  741. error = -EFBIG;
  742. goto out_unlock;
  743. }
  744. new_size = isize + len;
  745. /* Offset should be less than i_size */
  746. if (offset >= isize) {
  747. error = -EINVAL;
  748. goto out_unlock;
  749. }
  750. do_file_insert = true;
  751. } else {
  752. flags |= XFS_PREALLOC_SET;
  753. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  754. offset + len > i_size_read(inode)) {
  755. new_size = offset + len;
  756. error = inode_newsize_ok(inode, new_size);
  757. if (error)
  758. goto out_unlock;
  759. }
  760. if (mode & FALLOC_FL_ZERO_RANGE)
  761. error = xfs_zero_file_space(ip, offset, len);
  762. else {
  763. if (mode & FALLOC_FL_UNSHARE_RANGE) {
  764. error = xfs_reflink_unshare(ip, offset, len);
  765. if (error)
  766. goto out_unlock;
  767. }
  768. error = xfs_alloc_file_space(ip, offset, len,
  769. XFS_BMAPI_PREALLOC);
  770. }
  771. if (error)
  772. goto out_unlock;
  773. }
  774. if (file->f_flags & O_DSYNC)
  775. flags |= XFS_PREALLOC_SYNC;
  776. error = xfs_update_prealloc_flags(ip, flags);
  777. if (error)
  778. goto out_unlock;
  779. /* Change file size if needed */
  780. if (new_size) {
  781. struct iattr iattr;
  782. iattr.ia_valid = ATTR_SIZE;
  783. iattr.ia_size = new_size;
  784. error = xfs_vn_setattr_size(file_dentry(file), &iattr);
  785. if (error)
  786. goto out_unlock;
  787. }
  788. /*
  789. * Perform hole insertion now that the file size has been
  790. * updated so that if we crash during the operation we don't
  791. * leave shifted extents past EOF and hence losing access to
  792. * the data that is contained within them.
  793. */
  794. if (do_file_insert)
  795. error = xfs_insert_file_space(ip, offset, len);
  796. out_unlock:
  797. xfs_iunlock(ip, iolock);
  798. return error;
  799. }
  800. STATIC int
  801. xfs_file_clone_range(
  802. struct file *file_in,
  803. loff_t pos_in,
  804. struct file *file_out,
  805. loff_t pos_out,
  806. u64 len)
  807. {
  808. return xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out,
  809. len, false);
  810. }
  811. STATIC ssize_t
  812. xfs_file_dedupe_range(
  813. struct file *src_file,
  814. u64 loff,
  815. u64 len,
  816. struct file *dst_file,
  817. u64 dst_loff)
  818. {
  819. struct inode *srci = file_inode(src_file);
  820. u64 max_dedupe;
  821. int error;
  822. /*
  823. * Since we have to read all these pages in to compare them, cut
  824. * it off at MAX_RW_COUNT/2 rounded down to the nearest block.
  825. * That means we won't do more than MAX_RW_COUNT IO per request.
  826. */
  827. max_dedupe = (MAX_RW_COUNT >> 1) & ~(i_blocksize(srci) - 1);
  828. if (len > max_dedupe)
  829. len = max_dedupe;
  830. error = xfs_reflink_remap_range(src_file, loff, dst_file, dst_loff,
  831. len, true);
  832. if (error)
  833. return error;
  834. return len;
  835. }
  836. STATIC int
  837. xfs_file_open(
  838. struct inode *inode,
  839. struct file *file)
  840. {
  841. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  842. return -EFBIG;
  843. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  844. return -EIO;
  845. file->f_mode |= FMODE_NOWAIT;
  846. return 0;
  847. }
  848. STATIC int
  849. xfs_dir_open(
  850. struct inode *inode,
  851. struct file *file)
  852. {
  853. struct xfs_inode *ip = XFS_I(inode);
  854. int mode;
  855. int error;
  856. error = xfs_file_open(inode, file);
  857. if (error)
  858. return error;
  859. /*
  860. * If there are any blocks, read-ahead block 0 as we're almost
  861. * certain to have the next operation be a read there.
  862. */
  863. mode = xfs_ilock_data_map_shared(ip);
  864. if (ip->i_d.di_nextents > 0)
  865. error = xfs_dir3_data_readahead(ip, 0, -1);
  866. xfs_iunlock(ip, mode);
  867. return error;
  868. }
  869. STATIC int
  870. xfs_file_release(
  871. struct inode *inode,
  872. struct file *filp)
  873. {
  874. return xfs_release(XFS_I(inode));
  875. }
  876. STATIC int
  877. xfs_file_readdir(
  878. struct file *file,
  879. struct dir_context *ctx)
  880. {
  881. struct inode *inode = file_inode(file);
  882. xfs_inode_t *ip = XFS_I(inode);
  883. size_t bufsize;
  884. /*
  885. * The Linux API doesn't pass down the total size of the buffer
  886. * we read into down to the filesystem. With the filldir concept
  887. * it's not needed for correct information, but the XFS dir2 leaf
  888. * code wants an estimate of the buffer size to calculate it's
  889. * readahead window and size the buffers used for mapping to
  890. * physical blocks.
  891. *
  892. * Try to give it an estimate that's good enough, maybe at some
  893. * point we can change the ->readdir prototype to include the
  894. * buffer size. For now we use the current glibc buffer size.
  895. */
  896. bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
  897. return xfs_readdir(NULL, ip, ctx, bufsize);
  898. }
  899. STATIC loff_t
  900. xfs_file_llseek(
  901. struct file *file,
  902. loff_t offset,
  903. int whence)
  904. {
  905. struct inode *inode = file->f_mapping->host;
  906. if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
  907. return -EIO;
  908. switch (whence) {
  909. default:
  910. return generic_file_llseek(file, offset, whence);
  911. case SEEK_HOLE:
  912. offset = iomap_seek_hole(inode, offset, &xfs_iomap_ops);
  913. break;
  914. case SEEK_DATA:
  915. offset = iomap_seek_data(inode, offset, &xfs_iomap_ops);
  916. break;
  917. }
  918. if (offset < 0)
  919. return offset;
  920. return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  921. }
  922. /*
  923. * Locking for serialisation of IO during page faults. This results in a lock
  924. * ordering of:
  925. *
  926. * mmap_sem (MM)
  927. * sb_start_pagefault(vfs, freeze)
  928. * i_mmaplock (XFS - truncate serialisation)
  929. * page_lock (MM)
  930. * i_lock (XFS - extent map serialisation)
  931. */
  932. static vm_fault_t
  933. __xfs_filemap_fault(
  934. struct vm_fault *vmf,
  935. enum page_entry_size pe_size,
  936. bool write_fault)
  937. {
  938. struct inode *inode = file_inode(vmf->vma->vm_file);
  939. struct xfs_inode *ip = XFS_I(inode);
  940. vm_fault_t ret;
  941. trace_xfs_filemap_fault(ip, pe_size, write_fault);
  942. if (write_fault) {
  943. sb_start_pagefault(inode->i_sb);
  944. file_update_time(vmf->vma->vm_file);
  945. }
  946. xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  947. if (IS_DAX(inode)) {
  948. pfn_t pfn;
  949. ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL, &xfs_iomap_ops);
  950. if (ret & VM_FAULT_NEEDDSYNC)
  951. ret = dax_finish_sync_fault(vmf, pe_size, pfn);
  952. } else {
  953. if (write_fault)
  954. ret = iomap_page_mkwrite(vmf, &xfs_iomap_ops);
  955. else
  956. ret = filemap_fault(vmf);
  957. }
  958. xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  959. if (write_fault)
  960. sb_end_pagefault(inode->i_sb);
  961. return ret;
  962. }
  963. static vm_fault_t
  964. xfs_filemap_fault(
  965. struct vm_fault *vmf)
  966. {
  967. /* DAX can shortcut the normal fault path on write faults! */
  968. return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
  969. IS_DAX(file_inode(vmf->vma->vm_file)) &&
  970. (vmf->flags & FAULT_FLAG_WRITE));
  971. }
  972. static vm_fault_t
  973. xfs_filemap_huge_fault(
  974. struct vm_fault *vmf,
  975. enum page_entry_size pe_size)
  976. {
  977. if (!IS_DAX(file_inode(vmf->vma->vm_file)))
  978. return VM_FAULT_FALLBACK;
  979. /* DAX can shortcut the normal fault path on write faults! */
  980. return __xfs_filemap_fault(vmf, pe_size,
  981. (vmf->flags & FAULT_FLAG_WRITE));
  982. }
  983. static vm_fault_t
  984. xfs_filemap_page_mkwrite(
  985. struct vm_fault *vmf)
  986. {
  987. return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
  988. }
  989. /*
  990. * pfn_mkwrite was originally intended to ensure we capture time stamp updates
  991. * on write faults. In reality, it needs to serialise against truncate and
  992. * prepare memory for writing so handle is as standard write fault.
  993. */
  994. static vm_fault_t
  995. xfs_filemap_pfn_mkwrite(
  996. struct vm_fault *vmf)
  997. {
  998. return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
  999. }
  1000. static const struct vm_operations_struct xfs_file_vm_ops = {
  1001. .fault = xfs_filemap_fault,
  1002. .huge_fault = xfs_filemap_huge_fault,
  1003. .map_pages = filemap_map_pages,
  1004. .page_mkwrite = xfs_filemap_page_mkwrite,
  1005. .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
  1006. };
  1007. STATIC int
  1008. xfs_file_mmap(
  1009. struct file *filp,
  1010. struct vm_area_struct *vma)
  1011. {
  1012. /*
  1013. * We don't support synchronous mappings for non-DAX files. At least
  1014. * until someone comes with a sensible use case.
  1015. */
  1016. if (!IS_DAX(file_inode(filp)) && (vma->vm_flags & VM_SYNC))
  1017. return -EOPNOTSUPP;
  1018. file_accessed(filp);
  1019. vma->vm_ops = &xfs_file_vm_ops;
  1020. if (IS_DAX(file_inode(filp)))
  1021. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  1022. return 0;
  1023. }
  1024. const struct file_operations xfs_file_operations = {
  1025. .llseek = xfs_file_llseek,
  1026. .read_iter = xfs_file_read_iter,
  1027. .write_iter = xfs_file_write_iter,
  1028. .splice_read = generic_file_splice_read,
  1029. .splice_write = iter_file_splice_write,
  1030. .unlocked_ioctl = xfs_file_ioctl,
  1031. #ifdef CONFIG_COMPAT
  1032. .compat_ioctl = xfs_file_compat_ioctl,
  1033. #endif
  1034. .mmap = xfs_file_mmap,
  1035. .mmap_supported_flags = MAP_SYNC,
  1036. .open = xfs_file_open,
  1037. .release = xfs_file_release,
  1038. .fsync = xfs_file_fsync,
  1039. .get_unmapped_area = thp_get_unmapped_area,
  1040. .fallocate = xfs_file_fallocate,
  1041. .clone_file_range = xfs_file_clone_range,
  1042. .dedupe_file_range = xfs_file_dedupe_range,
  1043. };
  1044. const struct file_operations xfs_dir_file_operations = {
  1045. .open = xfs_dir_open,
  1046. .read = generic_read_dir,
  1047. .iterate_shared = xfs_file_readdir,
  1048. .llseek = generic_file_llseek,
  1049. .unlocked_ioctl = xfs_file_ioctl,
  1050. #ifdef CONFIG_COMPAT
  1051. .compat_ioctl = xfs_file_compat_ioctl,
  1052. #endif
  1053. .fsync = xfs_dir_fsync,
  1054. };