xfs_file.c 30 KB

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