xfs_file.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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. /* unaligned dio always waits, bail */
  457. if (unaligned_io)
  458. return -EAGAIN;
  459. if (!xfs_ilock_nowait(ip, iolock))
  460. return -EAGAIN;
  461. } else {
  462. xfs_ilock(ip, iolock);
  463. }
  464. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  465. if (ret)
  466. goto out;
  467. count = iov_iter_count(from);
  468. /*
  469. * If we are doing unaligned IO, we can't allow any other overlapping IO
  470. * in-flight at the same time or we risk data corruption. Wait for all
  471. * other IO to drain before we submit. If the IO is aligned, demote the
  472. * iolock if we had to take the exclusive lock in
  473. * xfs_file_aio_write_checks() for other reasons.
  474. */
  475. if (unaligned_io) {
  476. inode_dio_wait(inode);
  477. } else if (iolock == XFS_IOLOCK_EXCL) {
  478. xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
  479. iolock = XFS_IOLOCK_SHARED;
  480. }
  481. trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
  482. ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, xfs_dio_write_end_io);
  483. /*
  484. * If unaligned, this is the only IO in-flight. If it has not yet
  485. * completed, wait on it before we release the iolock to prevent
  486. * subsequent overlapping IO.
  487. */
  488. if (ret == -EIOCBQUEUED && unaligned_io)
  489. inode_dio_wait(inode);
  490. out:
  491. xfs_iunlock(ip, iolock);
  492. /*
  493. * No fallback to buffered IO on errors for XFS, direct IO will either
  494. * complete fully or fail.
  495. */
  496. ASSERT(ret < 0 || ret == count);
  497. return ret;
  498. }
  499. static noinline ssize_t
  500. xfs_file_dax_write(
  501. struct kiocb *iocb,
  502. struct iov_iter *from)
  503. {
  504. struct inode *inode = iocb->ki_filp->f_mapping->host;
  505. struct xfs_inode *ip = XFS_I(inode);
  506. int iolock = XFS_IOLOCK_EXCL;
  507. ssize_t ret, error = 0;
  508. size_t count;
  509. loff_t pos;
  510. if (iocb->ki_flags & IOCB_NOWAIT) {
  511. if (!xfs_ilock_nowait(ip, iolock))
  512. return -EAGAIN;
  513. } else {
  514. xfs_ilock(ip, iolock);
  515. }
  516. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  517. if (ret)
  518. goto out;
  519. pos = iocb->ki_pos;
  520. count = iov_iter_count(from);
  521. trace_xfs_file_dax_write(ip, count, pos);
  522. ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops);
  523. if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
  524. i_size_write(inode, iocb->ki_pos);
  525. error = xfs_setfilesize(ip, pos, ret);
  526. }
  527. out:
  528. xfs_iunlock(ip, iolock);
  529. if (error)
  530. return error;
  531. if (ret > 0) {
  532. XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
  533. /* Handle various SYNC-type writes */
  534. ret = generic_write_sync(iocb, ret);
  535. }
  536. return ret;
  537. }
  538. STATIC ssize_t
  539. xfs_file_buffered_aio_write(
  540. struct kiocb *iocb,
  541. struct iov_iter *from)
  542. {
  543. struct file *file = iocb->ki_filp;
  544. struct address_space *mapping = file->f_mapping;
  545. struct inode *inode = mapping->host;
  546. struct xfs_inode *ip = XFS_I(inode);
  547. ssize_t ret;
  548. int enospc = 0;
  549. int iolock;
  550. if (iocb->ki_flags & IOCB_NOWAIT)
  551. return -EOPNOTSUPP;
  552. write_retry:
  553. iolock = XFS_IOLOCK_EXCL;
  554. xfs_ilock(ip, iolock);
  555. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  556. if (ret)
  557. goto out;
  558. /* We can write back this queue in page reclaim */
  559. current->backing_dev_info = inode_to_bdi(inode);
  560. trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
  561. ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops);
  562. if (likely(ret >= 0))
  563. iocb->ki_pos += ret;
  564. /*
  565. * If we hit a space limit, try to free up some lingering preallocated
  566. * space before returning an error. In the case of ENOSPC, first try to
  567. * write back all dirty inodes to free up some of the excess reserved
  568. * metadata space. This reduces the chances that the eofblocks scan
  569. * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
  570. * also behaves as a filter to prevent too many eofblocks scans from
  571. * running at the same time.
  572. */
  573. if (ret == -EDQUOT && !enospc) {
  574. xfs_iunlock(ip, iolock);
  575. enospc = xfs_inode_free_quota_eofblocks(ip);
  576. if (enospc)
  577. goto write_retry;
  578. enospc = xfs_inode_free_quota_cowblocks(ip);
  579. if (enospc)
  580. goto write_retry;
  581. iolock = 0;
  582. } else if (ret == -ENOSPC && !enospc) {
  583. struct xfs_eofblocks eofb = {0};
  584. enospc = 1;
  585. xfs_flush_inodes(ip->i_mount);
  586. xfs_iunlock(ip, iolock);
  587. eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
  588. xfs_icache_free_eofblocks(ip->i_mount, &eofb);
  589. xfs_icache_free_cowblocks(ip->i_mount, &eofb);
  590. goto write_retry;
  591. }
  592. current->backing_dev_info = NULL;
  593. out:
  594. if (iolock)
  595. xfs_iunlock(ip, iolock);
  596. if (ret > 0) {
  597. XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
  598. /* Handle various SYNC-type writes */
  599. ret = generic_write_sync(iocb, ret);
  600. }
  601. return ret;
  602. }
  603. STATIC ssize_t
  604. xfs_file_write_iter(
  605. struct kiocb *iocb,
  606. struct iov_iter *from)
  607. {
  608. struct file *file = iocb->ki_filp;
  609. struct address_space *mapping = file->f_mapping;
  610. struct inode *inode = mapping->host;
  611. struct xfs_inode *ip = XFS_I(inode);
  612. ssize_t ret;
  613. size_t ocount = iov_iter_count(from);
  614. XFS_STATS_INC(ip->i_mount, xs_write_calls);
  615. if (ocount == 0)
  616. return 0;
  617. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  618. return -EIO;
  619. if (IS_DAX(inode))
  620. return xfs_file_dax_write(iocb, from);
  621. if (iocb->ki_flags & IOCB_DIRECT) {
  622. /*
  623. * Allow a directio write to fall back to a buffered
  624. * write *only* in the case that we're doing a reflink
  625. * CoW. In all other directio scenarios we do not
  626. * allow an operation to fall back to buffered mode.
  627. */
  628. ret = xfs_file_dio_aio_write(iocb, from);
  629. if (ret != -EREMCHG)
  630. return ret;
  631. }
  632. return xfs_file_buffered_aio_write(iocb, from);
  633. }
  634. static void
  635. xfs_wait_dax_page(
  636. struct inode *inode)
  637. {
  638. struct xfs_inode *ip = XFS_I(inode);
  639. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
  640. schedule();
  641. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  642. }
  643. static int
  644. xfs_break_dax_layouts(
  645. struct inode *inode,
  646. bool *retry)
  647. {
  648. struct page *page;
  649. ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
  650. page = dax_layout_busy_page(inode->i_mapping);
  651. if (!page)
  652. return 0;
  653. *retry = true;
  654. return ___wait_var_event(&page->_refcount,
  655. atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
  656. 0, 0, xfs_wait_dax_page(inode));
  657. }
  658. int
  659. xfs_break_layouts(
  660. struct inode *inode,
  661. uint *iolock,
  662. enum layout_break_reason reason)
  663. {
  664. bool retry;
  665. int error;
  666. ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
  667. do {
  668. retry = false;
  669. switch (reason) {
  670. case BREAK_UNMAP:
  671. error = xfs_break_dax_layouts(inode, &retry);
  672. if (error || retry)
  673. break;
  674. /* fall through */
  675. case BREAK_WRITE:
  676. error = xfs_break_leased_layouts(inode, iolock, &retry);
  677. break;
  678. default:
  679. WARN_ON_ONCE(1);
  680. error = -EINVAL;
  681. }
  682. } while (error == 0 && retry);
  683. return error;
  684. }
  685. #define XFS_FALLOC_FL_SUPPORTED \
  686. (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
  687. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
  688. FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
  689. STATIC long
  690. xfs_file_fallocate(
  691. struct file *file,
  692. int mode,
  693. loff_t offset,
  694. loff_t len)
  695. {
  696. struct inode *inode = file_inode(file);
  697. struct xfs_inode *ip = XFS_I(inode);
  698. long error;
  699. enum xfs_prealloc_flags flags = 0;
  700. uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
  701. loff_t new_size = 0;
  702. bool do_file_insert = false;
  703. if (!S_ISREG(inode->i_mode))
  704. return -EINVAL;
  705. if (mode & ~XFS_FALLOC_FL_SUPPORTED)
  706. return -EOPNOTSUPP;
  707. xfs_ilock(ip, iolock);
  708. error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
  709. if (error)
  710. goto out_unlock;
  711. if (mode & FALLOC_FL_PUNCH_HOLE) {
  712. error = xfs_free_file_space(ip, offset, len);
  713. if (error)
  714. goto out_unlock;
  715. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  716. unsigned int blksize_mask = i_blocksize(inode) - 1;
  717. if (offset & blksize_mask || len & blksize_mask) {
  718. error = -EINVAL;
  719. goto out_unlock;
  720. }
  721. /*
  722. * There is no need to overlap collapse range with EOF,
  723. * in which case it is effectively a truncate operation
  724. */
  725. if (offset + len >= i_size_read(inode)) {
  726. error = -EINVAL;
  727. goto out_unlock;
  728. }
  729. new_size = i_size_read(inode) - len;
  730. error = xfs_collapse_file_space(ip, offset, len);
  731. if (error)
  732. goto out_unlock;
  733. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  734. unsigned int blksize_mask = i_blocksize(inode) - 1;
  735. loff_t isize = i_size_read(inode);
  736. if (offset & blksize_mask || len & blksize_mask) {
  737. error = -EINVAL;
  738. goto out_unlock;
  739. }
  740. /*
  741. * New inode size must not exceed ->s_maxbytes, accounting for
  742. * possible signed overflow.
  743. */
  744. if (inode->i_sb->s_maxbytes - isize < len) {
  745. error = -EFBIG;
  746. goto out_unlock;
  747. }
  748. new_size = isize + len;
  749. /* Offset should be less than i_size */
  750. if (offset >= isize) {
  751. error = -EINVAL;
  752. goto out_unlock;
  753. }
  754. do_file_insert = true;
  755. } else {
  756. flags |= XFS_PREALLOC_SET;
  757. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  758. offset + len > i_size_read(inode)) {
  759. new_size = offset + len;
  760. error = inode_newsize_ok(inode, new_size);
  761. if (error)
  762. goto out_unlock;
  763. }
  764. if (mode & FALLOC_FL_ZERO_RANGE)
  765. error = xfs_zero_file_space(ip, offset, len);
  766. else {
  767. if (mode & FALLOC_FL_UNSHARE_RANGE) {
  768. error = xfs_reflink_unshare(ip, offset, len);
  769. if (error)
  770. goto out_unlock;
  771. }
  772. error = xfs_alloc_file_space(ip, offset, len,
  773. XFS_BMAPI_PREALLOC);
  774. }
  775. if (error)
  776. goto out_unlock;
  777. }
  778. if (file->f_flags & O_DSYNC)
  779. flags |= XFS_PREALLOC_SYNC;
  780. error = xfs_update_prealloc_flags(ip, flags);
  781. if (error)
  782. goto out_unlock;
  783. /* Change file size if needed */
  784. if (new_size) {
  785. struct iattr iattr;
  786. iattr.ia_valid = ATTR_SIZE;
  787. iattr.ia_size = new_size;
  788. error = xfs_vn_setattr_size(file_dentry(file), &iattr);
  789. if (error)
  790. goto out_unlock;
  791. }
  792. /*
  793. * Perform hole insertion now that the file size has been
  794. * updated so that if we crash during the operation we don't
  795. * leave shifted extents past EOF and hence losing access to
  796. * the data that is contained within them.
  797. */
  798. if (do_file_insert)
  799. error = xfs_insert_file_space(ip, offset, len);
  800. out_unlock:
  801. xfs_iunlock(ip, iolock);
  802. return error;
  803. }
  804. STATIC int
  805. xfs_file_clone_range(
  806. struct file *file_in,
  807. loff_t pos_in,
  808. struct file *file_out,
  809. loff_t pos_out,
  810. u64 len)
  811. {
  812. return xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out,
  813. len, false);
  814. }
  815. STATIC int
  816. xfs_file_dedupe_range(
  817. struct file *file_in,
  818. loff_t pos_in,
  819. struct file *file_out,
  820. loff_t pos_out,
  821. u64 len)
  822. {
  823. return xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out,
  824. len, true);
  825. }
  826. STATIC int
  827. xfs_file_open(
  828. struct inode *inode,
  829. struct file *file)
  830. {
  831. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  832. return -EFBIG;
  833. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  834. return -EIO;
  835. file->f_mode |= FMODE_NOWAIT;
  836. return 0;
  837. }
  838. STATIC int
  839. xfs_dir_open(
  840. struct inode *inode,
  841. struct file *file)
  842. {
  843. struct xfs_inode *ip = XFS_I(inode);
  844. int mode;
  845. int error;
  846. error = xfs_file_open(inode, file);
  847. if (error)
  848. return error;
  849. /*
  850. * If there are any blocks, read-ahead block 0 as we're almost
  851. * certain to have the next operation be a read there.
  852. */
  853. mode = xfs_ilock_data_map_shared(ip);
  854. if (ip->i_d.di_nextents > 0)
  855. error = xfs_dir3_data_readahead(ip, 0, -1);
  856. xfs_iunlock(ip, mode);
  857. return error;
  858. }
  859. STATIC int
  860. xfs_file_release(
  861. struct inode *inode,
  862. struct file *filp)
  863. {
  864. return xfs_release(XFS_I(inode));
  865. }
  866. STATIC int
  867. xfs_file_readdir(
  868. struct file *file,
  869. struct dir_context *ctx)
  870. {
  871. struct inode *inode = file_inode(file);
  872. xfs_inode_t *ip = XFS_I(inode);
  873. size_t bufsize;
  874. /*
  875. * The Linux API doesn't pass down the total size of the buffer
  876. * we read into down to the filesystem. With the filldir concept
  877. * it's not needed for correct information, but the XFS dir2 leaf
  878. * code wants an estimate of the buffer size to calculate it's
  879. * readahead window and size the buffers used for mapping to
  880. * physical blocks.
  881. *
  882. * Try to give it an estimate that's good enough, maybe at some
  883. * point we can change the ->readdir prototype to include the
  884. * buffer size. For now we use the current glibc buffer size.
  885. */
  886. bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
  887. return xfs_readdir(NULL, ip, ctx, bufsize);
  888. }
  889. STATIC loff_t
  890. xfs_file_llseek(
  891. struct file *file,
  892. loff_t offset,
  893. int whence)
  894. {
  895. struct inode *inode = file->f_mapping->host;
  896. if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
  897. return -EIO;
  898. switch (whence) {
  899. default:
  900. return generic_file_llseek(file, offset, whence);
  901. case SEEK_HOLE:
  902. offset = iomap_seek_hole(inode, offset, &xfs_iomap_ops);
  903. break;
  904. case SEEK_DATA:
  905. offset = iomap_seek_data(inode, offset, &xfs_iomap_ops);
  906. break;
  907. }
  908. if (offset < 0)
  909. return offset;
  910. return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  911. }
  912. /*
  913. * Locking for serialisation of IO during page faults. This results in a lock
  914. * ordering of:
  915. *
  916. * mmap_sem (MM)
  917. * sb_start_pagefault(vfs, freeze)
  918. * i_mmaplock (XFS - truncate serialisation)
  919. * page_lock (MM)
  920. * i_lock (XFS - extent map serialisation)
  921. */
  922. static vm_fault_t
  923. __xfs_filemap_fault(
  924. struct vm_fault *vmf,
  925. enum page_entry_size pe_size,
  926. bool write_fault)
  927. {
  928. struct inode *inode = file_inode(vmf->vma->vm_file);
  929. struct xfs_inode *ip = XFS_I(inode);
  930. vm_fault_t ret;
  931. trace_xfs_filemap_fault(ip, pe_size, write_fault);
  932. if (write_fault) {
  933. sb_start_pagefault(inode->i_sb);
  934. file_update_time(vmf->vma->vm_file);
  935. }
  936. xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  937. if (IS_DAX(inode)) {
  938. pfn_t pfn;
  939. ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL, &xfs_iomap_ops);
  940. if (ret & VM_FAULT_NEEDDSYNC)
  941. ret = dax_finish_sync_fault(vmf, pe_size, pfn);
  942. } else {
  943. if (write_fault)
  944. ret = iomap_page_mkwrite(vmf, &xfs_iomap_ops);
  945. else
  946. ret = filemap_fault(vmf);
  947. }
  948. xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  949. if (write_fault)
  950. sb_end_pagefault(inode->i_sb);
  951. return ret;
  952. }
  953. static inline bool
  954. xfs_is_write_fault(
  955. struct vm_fault *vmf)
  956. {
  957. return (vmf->flags & FAULT_FLAG_WRITE) &&
  958. (vmf->vma->vm_flags & VM_SHARED);
  959. }
  960. static vm_fault_t
  961. xfs_filemap_fault(
  962. struct vm_fault *vmf)
  963. {
  964. /* DAX can shortcut the normal fault path on write faults! */
  965. return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
  966. IS_DAX(file_inode(vmf->vma->vm_file)) &&
  967. xfs_is_write_fault(vmf));
  968. }
  969. static vm_fault_t
  970. xfs_filemap_huge_fault(
  971. struct vm_fault *vmf,
  972. enum page_entry_size pe_size)
  973. {
  974. if (!IS_DAX(file_inode(vmf->vma->vm_file)))
  975. return VM_FAULT_FALLBACK;
  976. /* DAX can shortcut the normal fault path on write faults! */
  977. return __xfs_filemap_fault(vmf, pe_size,
  978. xfs_is_write_fault(vmf));
  979. }
  980. static vm_fault_t
  981. xfs_filemap_page_mkwrite(
  982. struct vm_fault *vmf)
  983. {
  984. return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
  985. }
  986. /*
  987. * pfn_mkwrite was originally intended to ensure we capture time stamp updates
  988. * on write faults. In reality, it needs to serialise against truncate and
  989. * prepare memory for writing so handle is as standard write fault.
  990. */
  991. static vm_fault_t
  992. xfs_filemap_pfn_mkwrite(
  993. struct vm_fault *vmf)
  994. {
  995. return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
  996. }
  997. static const struct vm_operations_struct xfs_file_vm_ops = {
  998. .fault = xfs_filemap_fault,
  999. .huge_fault = xfs_filemap_huge_fault,
  1000. .map_pages = filemap_map_pages,
  1001. .page_mkwrite = xfs_filemap_page_mkwrite,
  1002. .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
  1003. };
  1004. STATIC int
  1005. xfs_file_mmap(
  1006. struct file *filp,
  1007. struct vm_area_struct *vma)
  1008. {
  1009. /*
  1010. * We don't support synchronous mappings for non-DAX files. At least
  1011. * until someone comes with a sensible use case.
  1012. */
  1013. if (!IS_DAX(file_inode(filp)) && (vma->vm_flags & VM_SYNC))
  1014. return -EOPNOTSUPP;
  1015. file_accessed(filp);
  1016. vma->vm_ops = &xfs_file_vm_ops;
  1017. if (IS_DAX(file_inode(filp)))
  1018. vma->vm_flags |= VM_HUGEPAGE;
  1019. return 0;
  1020. }
  1021. const struct file_operations xfs_file_operations = {
  1022. .llseek = xfs_file_llseek,
  1023. .read_iter = xfs_file_read_iter,
  1024. .write_iter = xfs_file_write_iter,
  1025. .splice_read = generic_file_splice_read,
  1026. .splice_write = iter_file_splice_write,
  1027. .unlocked_ioctl = xfs_file_ioctl,
  1028. #ifdef CONFIG_COMPAT
  1029. .compat_ioctl = xfs_file_compat_ioctl,
  1030. #endif
  1031. .mmap = xfs_file_mmap,
  1032. .mmap_supported_flags = MAP_SYNC,
  1033. .open = xfs_file_open,
  1034. .release = xfs_file_release,
  1035. .fsync = xfs_file_fsync,
  1036. .get_unmapped_area = thp_get_unmapped_area,
  1037. .fallocate = xfs_file_fallocate,
  1038. .clone_file_range = xfs_file_clone_range,
  1039. .dedupe_file_range = xfs_file_dedupe_range,
  1040. };
  1041. const struct file_operations xfs_dir_file_operations = {
  1042. .open = xfs_dir_open,
  1043. .read = generic_read_dir,
  1044. .iterate_shared = xfs_file_readdir,
  1045. .llseek = generic_file_llseek,
  1046. .unlocked_ioctl = xfs_file_ioctl,
  1047. #ifdef CONFIG_COMPAT
  1048. .compat_ioctl = xfs_file_compat_ioctl,
  1049. #endif
  1050. .fsync = xfs_dir_fsync,
  1051. };