xfs_file.c 30 KB

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