xfs_file.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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. if (flags & IOMAP_DIO_COW) {
  363. error = xfs_reflink_end_cow(ip, offset, size);
  364. if (error)
  365. return error;
  366. }
  367. /*
  368. * Unwritten conversion updates the in-core isize after extent
  369. * conversion but before updating the on-disk size. Updating isize any
  370. * earlier allows a racing dio read to find unwritten extents before
  371. * they are converted.
  372. */
  373. if (flags & IOMAP_DIO_UNWRITTEN)
  374. return xfs_iomap_write_unwritten(ip, offset, size, true);
  375. /*
  376. * We need to update the in-core inode size here so that we don't end up
  377. * with the on-disk inode size being outside the in-core inode size. We
  378. * have no other method of updating EOF for AIO, so always do it here
  379. * if necessary.
  380. *
  381. * We need to lock the test/set EOF update as we can be racing with
  382. * other IO completions here to update the EOF. Failing to serialise
  383. * here can result in EOF moving backwards and Bad Things Happen when
  384. * that occurs.
  385. */
  386. spin_lock(&ip->i_flags_lock);
  387. if (offset + size > i_size_read(inode)) {
  388. i_size_write(inode, offset + size);
  389. spin_unlock(&ip->i_flags_lock);
  390. error = xfs_setfilesize(ip, offset, size);
  391. } else {
  392. spin_unlock(&ip->i_flags_lock);
  393. }
  394. return error;
  395. }
  396. /*
  397. * xfs_file_dio_aio_write - handle direct IO writes
  398. *
  399. * Lock the inode appropriately to prepare for and issue a direct IO write.
  400. * By separating it from the buffered write path we remove all the tricky to
  401. * follow locking changes and looping.
  402. *
  403. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  404. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  405. * pages are flushed out.
  406. *
  407. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  408. * allowing them to be done in parallel with reads and other direct IO writes.
  409. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  410. * needs to do sub-block zeroing and that requires serialisation against other
  411. * direct IOs to the same block. In this case we need to serialise the
  412. * submission of the unaligned IOs so that we don't get racing block zeroing in
  413. * the dio layer. To avoid the problem with aio, we also need to wait for
  414. * outstanding IOs to complete so that unwritten extent conversion is completed
  415. * before we try to map the overlapping block. This is currently implemented by
  416. * hitting it with a big hammer (i.e. inode_dio_wait()).
  417. *
  418. * Returns with locks held indicated by @iolock and errors indicated by
  419. * negative return values.
  420. */
  421. STATIC ssize_t
  422. xfs_file_dio_aio_write(
  423. struct kiocb *iocb,
  424. struct iov_iter *from)
  425. {
  426. struct file *file = iocb->ki_filp;
  427. struct address_space *mapping = file->f_mapping;
  428. struct inode *inode = mapping->host;
  429. struct xfs_inode *ip = XFS_I(inode);
  430. struct xfs_mount *mp = ip->i_mount;
  431. ssize_t ret = 0;
  432. int unaligned_io = 0;
  433. int iolock;
  434. size_t count = iov_iter_count(from);
  435. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  436. mp->m_rtdev_targp : mp->m_ddev_targp;
  437. /* DIO must be aligned to device logical sector size */
  438. if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
  439. return -EINVAL;
  440. /*
  441. * Don't take the exclusive iolock here unless the I/O is unaligned to
  442. * the file system block size. We don't need to consider the EOF
  443. * extension case here because xfs_file_aio_write_checks() will relock
  444. * the inode as necessary for EOF zeroing cases and fill out the new
  445. * inode size as appropriate.
  446. */
  447. if ((iocb->ki_pos & mp->m_blockmask) ||
  448. ((iocb->ki_pos + count) & mp->m_blockmask)) {
  449. unaligned_io = 1;
  450. /*
  451. * We can't properly handle unaligned direct I/O to reflink
  452. * files yet, as we can't unshare a partial block.
  453. */
  454. if (xfs_is_reflink_inode(ip)) {
  455. trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
  456. return -EREMCHG;
  457. }
  458. iolock = XFS_IOLOCK_EXCL;
  459. } else {
  460. iolock = XFS_IOLOCK_SHARED;
  461. }
  462. if (iocb->ki_flags & IOCB_NOWAIT) {
  463. if (!xfs_ilock_nowait(ip, iolock))
  464. return -EAGAIN;
  465. } else {
  466. xfs_ilock(ip, iolock);
  467. }
  468. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  469. if (ret)
  470. goto out;
  471. count = iov_iter_count(from);
  472. /*
  473. * If we are doing unaligned IO, wait for all other IO to drain,
  474. * otherwise demote the lock if we had to take the exclusive lock
  475. * for other reasons in xfs_file_aio_write_checks.
  476. */
  477. if (unaligned_io) {
  478. /* If we are going to wait for other DIO to finish, bail */
  479. if (iocb->ki_flags & IOCB_NOWAIT) {
  480. if (atomic_read(&inode->i_dio_count))
  481. return -EAGAIN;
  482. } else {
  483. inode_dio_wait(inode);
  484. }
  485. } else if (iolock == XFS_IOLOCK_EXCL) {
  486. xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
  487. iolock = XFS_IOLOCK_SHARED;
  488. }
  489. trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
  490. ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, xfs_dio_write_end_io);
  491. out:
  492. xfs_iunlock(ip, iolock);
  493. /*
  494. * No fallback to buffered IO on errors for XFS, direct IO will either
  495. * complete fully or fail.
  496. */
  497. ASSERT(ret < 0 || ret == count);
  498. return ret;
  499. }
  500. static noinline ssize_t
  501. xfs_file_dax_write(
  502. struct kiocb *iocb,
  503. struct iov_iter *from)
  504. {
  505. struct inode *inode = iocb->ki_filp->f_mapping->host;
  506. struct xfs_inode *ip = XFS_I(inode);
  507. int iolock = XFS_IOLOCK_EXCL;
  508. ssize_t ret, error = 0;
  509. size_t count;
  510. loff_t pos;
  511. if (iocb->ki_flags & IOCB_NOWAIT) {
  512. if (!xfs_ilock_nowait(ip, iolock))
  513. return -EAGAIN;
  514. } else {
  515. xfs_ilock(ip, iolock);
  516. }
  517. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  518. if (ret)
  519. goto out;
  520. pos = iocb->ki_pos;
  521. count = iov_iter_count(from);
  522. trace_xfs_file_dax_write(ip, count, pos);
  523. ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops);
  524. if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
  525. i_size_write(inode, iocb->ki_pos);
  526. error = xfs_setfilesize(ip, pos, ret);
  527. }
  528. out:
  529. xfs_iunlock(ip, iolock);
  530. return error ? error : ret;
  531. }
  532. STATIC ssize_t
  533. xfs_file_buffered_aio_write(
  534. struct kiocb *iocb,
  535. struct iov_iter *from)
  536. {
  537. struct file *file = iocb->ki_filp;
  538. struct address_space *mapping = file->f_mapping;
  539. struct inode *inode = mapping->host;
  540. struct xfs_inode *ip = XFS_I(inode);
  541. ssize_t ret;
  542. int enospc = 0;
  543. int iolock;
  544. if (iocb->ki_flags & IOCB_NOWAIT)
  545. return -EOPNOTSUPP;
  546. write_retry:
  547. iolock = XFS_IOLOCK_EXCL;
  548. xfs_ilock(ip, iolock);
  549. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  550. if (ret)
  551. goto out;
  552. /* We can write back this queue in page reclaim */
  553. current->backing_dev_info = inode_to_bdi(inode);
  554. trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
  555. ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops);
  556. if (likely(ret >= 0))
  557. iocb->ki_pos += ret;
  558. /*
  559. * If we hit a space limit, try to free up some lingering preallocated
  560. * space before returning an error. In the case of ENOSPC, first try to
  561. * write back all dirty inodes to free up some of the excess reserved
  562. * metadata space. This reduces the chances that the eofblocks scan
  563. * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
  564. * also behaves as a filter to prevent too many eofblocks scans from
  565. * running at the same time.
  566. */
  567. if (ret == -EDQUOT && !enospc) {
  568. xfs_iunlock(ip, iolock);
  569. enospc = xfs_inode_free_quota_eofblocks(ip);
  570. if (enospc)
  571. goto write_retry;
  572. enospc = xfs_inode_free_quota_cowblocks(ip);
  573. if (enospc)
  574. goto write_retry;
  575. iolock = 0;
  576. } else if (ret == -ENOSPC && !enospc) {
  577. struct xfs_eofblocks eofb = {0};
  578. enospc = 1;
  579. xfs_flush_inodes(ip->i_mount);
  580. xfs_iunlock(ip, iolock);
  581. eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
  582. xfs_icache_free_eofblocks(ip->i_mount, &eofb);
  583. xfs_icache_free_cowblocks(ip->i_mount, &eofb);
  584. goto write_retry;
  585. }
  586. current->backing_dev_info = NULL;
  587. out:
  588. if (iolock)
  589. xfs_iunlock(ip, iolock);
  590. return ret;
  591. }
  592. STATIC ssize_t
  593. xfs_file_write_iter(
  594. struct kiocb *iocb,
  595. struct iov_iter *from)
  596. {
  597. struct file *file = iocb->ki_filp;
  598. struct address_space *mapping = file->f_mapping;
  599. struct inode *inode = mapping->host;
  600. struct xfs_inode *ip = XFS_I(inode);
  601. ssize_t ret;
  602. size_t ocount = iov_iter_count(from);
  603. XFS_STATS_INC(ip->i_mount, xs_write_calls);
  604. if (ocount == 0)
  605. return 0;
  606. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  607. return -EIO;
  608. if (IS_DAX(inode))
  609. ret = xfs_file_dax_write(iocb, from);
  610. else if (iocb->ki_flags & IOCB_DIRECT) {
  611. /*
  612. * Allow a directio write to fall back to a buffered
  613. * write *only* in the case that we're doing a reflink
  614. * CoW. In all other directio scenarios we do not
  615. * allow an operation to fall back to buffered mode.
  616. */
  617. ret = xfs_file_dio_aio_write(iocb, from);
  618. if (ret == -EREMCHG)
  619. goto buffered;
  620. } else {
  621. buffered:
  622. ret = xfs_file_buffered_aio_write(iocb, from);
  623. }
  624. if (ret > 0) {
  625. XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
  626. /* Handle various SYNC-type writes */
  627. ret = generic_write_sync(iocb, ret);
  628. }
  629. return ret;
  630. }
  631. #define XFS_FALLOC_FL_SUPPORTED \
  632. (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
  633. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
  634. FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
  635. STATIC long
  636. xfs_file_fallocate(
  637. struct file *file,
  638. int mode,
  639. loff_t offset,
  640. loff_t len)
  641. {
  642. struct inode *inode = file_inode(file);
  643. struct xfs_inode *ip = XFS_I(inode);
  644. long error;
  645. enum xfs_prealloc_flags flags = 0;
  646. uint iolock = XFS_IOLOCK_EXCL;
  647. loff_t new_size = 0;
  648. bool do_file_insert = false;
  649. if (!S_ISREG(inode->i_mode))
  650. return -EINVAL;
  651. if (mode & ~XFS_FALLOC_FL_SUPPORTED)
  652. return -EOPNOTSUPP;
  653. xfs_ilock(ip, iolock);
  654. error = xfs_break_layouts(inode, &iolock);
  655. if (error)
  656. goto out_unlock;
  657. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  658. iolock |= XFS_MMAPLOCK_EXCL;
  659. if (mode & FALLOC_FL_PUNCH_HOLE) {
  660. error = xfs_free_file_space(ip, offset, len);
  661. if (error)
  662. goto out_unlock;
  663. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  664. unsigned int blksize_mask = i_blocksize(inode) - 1;
  665. if (offset & blksize_mask || len & blksize_mask) {
  666. error = -EINVAL;
  667. goto out_unlock;
  668. }
  669. /*
  670. * There is no need to overlap collapse range with EOF,
  671. * in which case it is effectively a truncate operation
  672. */
  673. if (offset + len >= i_size_read(inode)) {
  674. error = -EINVAL;
  675. goto out_unlock;
  676. }
  677. new_size = i_size_read(inode) - len;
  678. error = xfs_collapse_file_space(ip, offset, len);
  679. if (error)
  680. goto out_unlock;
  681. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  682. unsigned int blksize_mask = i_blocksize(inode) - 1;
  683. loff_t isize = i_size_read(inode);
  684. if (offset & blksize_mask || len & blksize_mask) {
  685. error = -EINVAL;
  686. goto out_unlock;
  687. }
  688. /*
  689. * New inode size must not exceed ->s_maxbytes, accounting for
  690. * possible signed overflow.
  691. */
  692. if (inode->i_sb->s_maxbytes - isize < len) {
  693. error = -EFBIG;
  694. goto out_unlock;
  695. }
  696. new_size = isize + len;
  697. /* Offset should be less than i_size */
  698. if (offset >= isize) {
  699. error = -EINVAL;
  700. goto out_unlock;
  701. }
  702. do_file_insert = true;
  703. } else {
  704. flags |= XFS_PREALLOC_SET;
  705. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  706. offset + len > i_size_read(inode)) {
  707. new_size = offset + len;
  708. error = inode_newsize_ok(inode, new_size);
  709. if (error)
  710. goto out_unlock;
  711. }
  712. if (mode & FALLOC_FL_ZERO_RANGE)
  713. error = xfs_zero_file_space(ip, offset, len);
  714. else {
  715. if (mode & FALLOC_FL_UNSHARE_RANGE) {
  716. error = xfs_reflink_unshare(ip, offset, len);
  717. if (error)
  718. goto out_unlock;
  719. }
  720. error = xfs_alloc_file_space(ip, offset, len,
  721. XFS_BMAPI_PREALLOC);
  722. }
  723. if (error)
  724. goto out_unlock;
  725. }
  726. if (file->f_flags & O_DSYNC)
  727. flags |= XFS_PREALLOC_SYNC;
  728. error = xfs_update_prealloc_flags(ip, flags);
  729. if (error)
  730. goto out_unlock;
  731. /* Change file size if needed */
  732. if (new_size) {
  733. struct iattr iattr;
  734. iattr.ia_valid = ATTR_SIZE;
  735. iattr.ia_size = new_size;
  736. error = xfs_vn_setattr_size(file_dentry(file), &iattr);
  737. if (error)
  738. goto out_unlock;
  739. }
  740. /*
  741. * Perform hole insertion now that the file size has been
  742. * updated so that if we crash during the operation we don't
  743. * leave shifted extents past EOF and hence losing access to
  744. * the data that is contained within them.
  745. */
  746. if (do_file_insert)
  747. error = xfs_insert_file_space(ip, offset, len);
  748. out_unlock:
  749. xfs_iunlock(ip, iolock);
  750. return error;
  751. }
  752. STATIC int
  753. xfs_file_clone_range(
  754. struct file *file_in,
  755. loff_t pos_in,
  756. struct file *file_out,
  757. loff_t pos_out,
  758. u64 len)
  759. {
  760. return xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out,
  761. len, false);
  762. }
  763. STATIC ssize_t
  764. xfs_file_dedupe_range(
  765. struct file *src_file,
  766. u64 loff,
  767. u64 len,
  768. struct file *dst_file,
  769. u64 dst_loff)
  770. {
  771. struct inode *srci = file_inode(src_file);
  772. u64 max_dedupe;
  773. int error;
  774. /*
  775. * Since we have to read all these pages in to compare them, cut
  776. * it off at MAX_RW_COUNT/2 rounded down to the nearest block.
  777. * That means we won't do more than MAX_RW_COUNT IO per request.
  778. */
  779. max_dedupe = (MAX_RW_COUNT >> 1) & ~(i_blocksize(srci) - 1);
  780. if (len > max_dedupe)
  781. len = max_dedupe;
  782. error = xfs_reflink_remap_range(src_file, loff, dst_file, dst_loff,
  783. len, true);
  784. if (error)
  785. return error;
  786. return len;
  787. }
  788. STATIC int
  789. xfs_file_open(
  790. struct inode *inode,
  791. struct file *file)
  792. {
  793. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  794. return -EFBIG;
  795. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  796. return -EIO;
  797. file->f_mode |= FMODE_NOWAIT;
  798. return 0;
  799. }
  800. STATIC int
  801. xfs_dir_open(
  802. struct inode *inode,
  803. struct file *file)
  804. {
  805. struct xfs_inode *ip = XFS_I(inode);
  806. int mode;
  807. int error;
  808. error = xfs_file_open(inode, file);
  809. if (error)
  810. return error;
  811. /*
  812. * If there are any blocks, read-ahead block 0 as we're almost
  813. * certain to have the next operation be a read there.
  814. */
  815. mode = xfs_ilock_data_map_shared(ip);
  816. if (ip->i_d.di_nextents > 0)
  817. error = xfs_dir3_data_readahead(ip, 0, -1);
  818. xfs_iunlock(ip, mode);
  819. return error;
  820. }
  821. STATIC int
  822. xfs_file_release(
  823. struct inode *inode,
  824. struct file *filp)
  825. {
  826. return xfs_release(XFS_I(inode));
  827. }
  828. STATIC int
  829. xfs_file_readdir(
  830. struct file *file,
  831. struct dir_context *ctx)
  832. {
  833. struct inode *inode = file_inode(file);
  834. xfs_inode_t *ip = XFS_I(inode);
  835. size_t bufsize;
  836. /*
  837. * The Linux API doesn't pass down the total size of the buffer
  838. * we read into down to the filesystem. With the filldir concept
  839. * it's not needed for correct information, but the XFS dir2 leaf
  840. * code wants an estimate of the buffer size to calculate it's
  841. * readahead window and size the buffers used for mapping to
  842. * physical blocks.
  843. *
  844. * Try to give it an estimate that's good enough, maybe at some
  845. * point we can change the ->readdir prototype to include the
  846. * buffer size. For now we use the current glibc buffer size.
  847. */
  848. bufsize = (size_t)min_t(loff_t, XFS_READDIR_BUFSIZE, ip->i_d.di_size);
  849. return xfs_readdir(NULL, ip, ctx, bufsize);
  850. }
  851. STATIC loff_t
  852. xfs_file_llseek(
  853. struct file *file,
  854. loff_t offset,
  855. int whence)
  856. {
  857. struct inode *inode = file->f_mapping->host;
  858. if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
  859. return -EIO;
  860. switch (whence) {
  861. default:
  862. return generic_file_llseek(file, offset, whence);
  863. case SEEK_HOLE:
  864. offset = iomap_seek_hole(inode, offset, &xfs_iomap_ops);
  865. break;
  866. case SEEK_DATA:
  867. offset = iomap_seek_data(inode, offset, &xfs_iomap_ops);
  868. break;
  869. }
  870. if (offset < 0)
  871. return offset;
  872. return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  873. }
  874. /*
  875. * Locking for serialisation of IO during page faults. This results in a lock
  876. * ordering of:
  877. *
  878. * mmap_sem (MM)
  879. * sb_start_pagefault(vfs, freeze)
  880. * i_mmaplock (XFS - truncate serialisation)
  881. * page_lock (MM)
  882. * i_lock (XFS - extent map serialisation)
  883. */
  884. static int
  885. __xfs_filemap_fault(
  886. struct vm_fault *vmf,
  887. enum page_entry_size pe_size,
  888. bool write_fault)
  889. {
  890. struct inode *inode = file_inode(vmf->vma->vm_file);
  891. struct xfs_inode *ip = XFS_I(inode);
  892. int ret;
  893. trace_xfs_filemap_fault(ip, pe_size, write_fault);
  894. if (write_fault) {
  895. sb_start_pagefault(inode->i_sb);
  896. file_update_time(vmf->vma->vm_file);
  897. }
  898. xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  899. if (IS_DAX(inode)) {
  900. pfn_t pfn;
  901. ret = dax_iomap_fault(vmf, pe_size, &pfn, NULL, &xfs_iomap_ops);
  902. if (ret & VM_FAULT_NEEDDSYNC)
  903. ret = dax_finish_sync_fault(vmf, pe_size, pfn);
  904. } else {
  905. if (write_fault)
  906. ret = iomap_page_mkwrite(vmf, &xfs_iomap_ops);
  907. else
  908. ret = filemap_fault(vmf);
  909. }
  910. xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  911. if (write_fault)
  912. sb_end_pagefault(inode->i_sb);
  913. return ret;
  914. }
  915. static int
  916. xfs_filemap_fault(
  917. struct vm_fault *vmf)
  918. {
  919. /* DAX can shortcut the normal fault path on write faults! */
  920. return __xfs_filemap_fault(vmf, PE_SIZE_PTE,
  921. IS_DAX(file_inode(vmf->vma->vm_file)) &&
  922. (vmf->flags & FAULT_FLAG_WRITE));
  923. }
  924. static int
  925. xfs_filemap_huge_fault(
  926. struct vm_fault *vmf,
  927. enum page_entry_size pe_size)
  928. {
  929. if (!IS_DAX(file_inode(vmf->vma->vm_file)))
  930. return VM_FAULT_FALLBACK;
  931. /* DAX can shortcut the normal fault path on write faults! */
  932. return __xfs_filemap_fault(vmf, pe_size,
  933. (vmf->flags & FAULT_FLAG_WRITE));
  934. }
  935. static int
  936. xfs_filemap_page_mkwrite(
  937. struct vm_fault *vmf)
  938. {
  939. return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
  940. }
  941. /*
  942. * pfn_mkwrite was originally intended to ensure we capture time stamp updates
  943. * on write faults. In reality, it needs to serialise against truncate and
  944. * prepare memory for writing so handle is as standard write fault.
  945. */
  946. static int
  947. xfs_filemap_pfn_mkwrite(
  948. struct vm_fault *vmf)
  949. {
  950. return __xfs_filemap_fault(vmf, PE_SIZE_PTE, true);
  951. }
  952. static const struct vm_operations_struct xfs_file_vm_ops = {
  953. .fault = xfs_filemap_fault,
  954. .huge_fault = xfs_filemap_huge_fault,
  955. .map_pages = filemap_map_pages,
  956. .page_mkwrite = xfs_filemap_page_mkwrite,
  957. .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
  958. };
  959. STATIC int
  960. xfs_file_mmap(
  961. struct file *filp,
  962. struct vm_area_struct *vma)
  963. {
  964. /*
  965. * We don't support synchronous mappings for non-DAX files. At least
  966. * until someone comes with a sensible use case.
  967. */
  968. if (!IS_DAX(file_inode(filp)) && (vma->vm_flags & VM_SYNC))
  969. return -EOPNOTSUPP;
  970. file_accessed(filp);
  971. vma->vm_ops = &xfs_file_vm_ops;
  972. if (IS_DAX(file_inode(filp)))
  973. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  974. return 0;
  975. }
  976. const struct file_operations xfs_file_operations = {
  977. .llseek = xfs_file_llseek,
  978. .read_iter = xfs_file_read_iter,
  979. .write_iter = xfs_file_write_iter,
  980. .splice_read = generic_file_splice_read,
  981. .splice_write = iter_file_splice_write,
  982. .unlocked_ioctl = xfs_file_ioctl,
  983. #ifdef CONFIG_COMPAT
  984. .compat_ioctl = xfs_file_compat_ioctl,
  985. #endif
  986. .mmap = xfs_file_mmap,
  987. .mmap_supported_flags = MAP_SYNC,
  988. .open = xfs_file_open,
  989. .release = xfs_file_release,
  990. .fsync = xfs_file_fsync,
  991. .get_unmapped_area = thp_get_unmapped_area,
  992. .fallocate = xfs_file_fallocate,
  993. .clone_file_range = xfs_file_clone_range,
  994. .dedupe_file_range = xfs_file_dedupe_range,
  995. };
  996. const struct file_operations xfs_dir_file_operations = {
  997. .open = xfs_dir_open,
  998. .read = generic_read_dir,
  999. .iterate_shared = xfs_file_readdir,
  1000. .llseek = generic_file_llseek,
  1001. .unlocked_ioctl = xfs_file_ioctl,
  1002. #ifdef CONFIG_COMPAT
  1003. .compat_ioctl = xfs_file_compat_ioctl,
  1004. #endif
  1005. .fsync = xfs_dir_fsync,
  1006. };