xfs_file.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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, NULL, &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. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  226. ret = generic_file_read_iter(iocb, to);
  227. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  228. return ret;
  229. }
  230. STATIC ssize_t
  231. xfs_file_read_iter(
  232. struct kiocb *iocb,
  233. struct iov_iter *to)
  234. {
  235. struct inode *inode = file_inode(iocb->ki_filp);
  236. struct xfs_mount *mp = XFS_I(inode)->i_mount;
  237. ssize_t ret = 0;
  238. XFS_STATS_INC(mp, xs_read_calls);
  239. if (XFS_FORCED_SHUTDOWN(mp))
  240. return -EIO;
  241. if (IS_DAX(inode))
  242. ret = xfs_file_dax_read(iocb, to);
  243. else if (iocb->ki_flags & IOCB_DIRECT)
  244. ret = xfs_file_dio_aio_read(iocb, to);
  245. else
  246. ret = xfs_file_buffered_aio_read(iocb, to);
  247. if (ret > 0)
  248. XFS_STATS_ADD(mp, xs_read_bytes, ret);
  249. return ret;
  250. }
  251. /*
  252. * Zero any on disk space between the current EOF and the new, larger EOF.
  253. *
  254. * This handles the normal case of zeroing the remainder of the last block in
  255. * the file and the unusual case of zeroing blocks out beyond the size of the
  256. * file. This second case only happens with fixed size extents and when the
  257. * system crashes before the inode size was updated but after blocks were
  258. * allocated.
  259. *
  260. * Expects the iolock to be held exclusive, and will take the ilock internally.
  261. */
  262. int /* error (positive) */
  263. xfs_zero_eof(
  264. struct xfs_inode *ip,
  265. xfs_off_t offset, /* starting I/O offset */
  266. xfs_fsize_t isize, /* current inode size */
  267. bool *did_zeroing)
  268. {
  269. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  270. ASSERT(offset > isize);
  271. trace_xfs_zero_eof(ip, isize, offset - isize);
  272. return xfs_zero_range(ip, isize, offset - isize, did_zeroing);
  273. }
  274. /*
  275. * Common pre-write limit and setup checks.
  276. *
  277. * Called with the iolocked held either shared and exclusive according to
  278. * @iolock, and returns with it held. Might upgrade the iolock to exclusive
  279. * if called for a direct write beyond i_size.
  280. */
  281. STATIC ssize_t
  282. xfs_file_aio_write_checks(
  283. struct kiocb *iocb,
  284. struct iov_iter *from,
  285. int *iolock)
  286. {
  287. struct file *file = iocb->ki_filp;
  288. struct inode *inode = file->f_mapping->host;
  289. struct xfs_inode *ip = XFS_I(inode);
  290. ssize_t error = 0;
  291. size_t count = iov_iter_count(from);
  292. bool drained_dio = false;
  293. restart:
  294. error = generic_write_checks(iocb, from);
  295. if (error <= 0)
  296. return error;
  297. error = xfs_break_layouts(inode, iolock);
  298. if (error)
  299. return error;
  300. /*
  301. * For changing security info in file_remove_privs() we need i_rwsem
  302. * exclusively.
  303. */
  304. if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
  305. xfs_iunlock(ip, *iolock);
  306. *iolock = XFS_IOLOCK_EXCL;
  307. xfs_ilock(ip, *iolock);
  308. goto restart;
  309. }
  310. /*
  311. * If the offset is beyond the size of the file, we need to zero any
  312. * blocks that fall between the existing EOF and the start of this
  313. * write. If zeroing is needed and we are currently holding the
  314. * iolock shared, we need to update it to exclusive which implies
  315. * having to redo all checks before.
  316. *
  317. * We need to serialise against EOF updates that occur in IO
  318. * completions here. We want to make sure that nobody is changing the
  319. * size while we do this check until we have placed an IO barrier (i.e.
  320. * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
  321. * The spinlock effectively forms a memory barrier once we have the
  322. * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
  323. * and hence be able to correctly determine if we need to run zeroing.
  324. */
  325. spin_lock(&ip->i_flags_lock);
  326. if (iocb->ki_pos > i_size_read(inode)) {
  327. bool zero = false;
  328. spin_unlock(&ip->i_flags_lock);
  329. if (!drained_dio) {
  330. if (*iolock == XFS_IOLOCK_SHARED) {
  331. xfs_iunlock(ip, *iolock);
  332. *iolock = XFS_IOLOCK_EXCL;
  333. xfs_ilock(ip, *iolock);
  334. iov_iter_reexpand(from, count);
  335. }
  336. /*
  337. * We now have an IO submission barrier in place, but
  338. * AIO can do EOF updates during IO completion and hence
  339. * we now need to wait for all of them to drain. Non-AIO
  340. * DIO will have drained before we are given the
  341. * XFS_IOLOCK_EXCL, and so for most cases this wait is a
  342. * no-op.
  343. */
  344. inode_dio_wait(inode);
  345. drained_dio = true;
  346. goto restart;
  347. }
  348. error = xfs_zero_eof(ip, iocb->ki_pos, i_size_read(inode), &zero);
  349. if (error)
  350. return error;
  351. } else
  352. spin_unlock(&ip->i_flags_lock);
  353. /*
  354. * Updating the timestamps will grab the ilock again from
  355. * xfs_fs_dirty_inode, so we have to call it after dropping the
  356. * lock above. Eventually we should look into a way to avoid
  357. * the pointless lock roundtrip.
  358. */
  359. if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
  360. error = file_update_time(file);
  361. if (error)
  362. return error;
  363. }
  364. /*
  365. * If we're writing the file then make sure to clear the setuid and
  366. * setgid bits if the process is not being run by root. This keeps
  367. * people from modifying setuid and setgid binaries.
  368. */
  369. if (!IS_NOSEC(inode))
  370. return file_remove_privs(file);
  371. return 0;
  372. }
  373. static int
  374. xfs_dio_write_end_io(
  375. struct kiocb *iocb,
  376. ssize_t size,
  377. unsigned flags)
  378. {
  379. struct inode *inode = file_inode(iocb->ki_filp);
  380. struct xfs_inode *ip = XFS_I(inode);
  381. loff_t offset = iocb->ki_pos;
  382. bool update_size = false;
  383. int error = 0;
  384. trace_xfs_end_io_direct_write(ip, offset, size);
  385. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  386. return -EIO;
  387. if (size <= 0)
  388. return size;
  389. /*
  390. * We need to update the in-core inode size here so that we don't end up
  391. * with the on-disk inode size being outside the in-core inode size. We
  392. * have no other method of updating EOF for AIO, so always do it here
  393. * if necessary.
  394. *
  395. * We need to lock the test/set EOF update as we can be racing with
  396. * other IO completions here to update the EOF. Failing to serialise
  397. * here can result in EOF moving backwards and Bad Things Happen when
  398. * that occurs.
  399. */
  400. spin_lock(&ip->i_flags_lock);
  401. if (offset + size > i_size_read(inode)) {
  402. i_size_write(inode, offset + size);
  403. update_size = true;
  404. }
  405. spin_unlock(&ip->i_flags_lock);
  406. if (flags & IOMAP_DIO_COW) {
  407. error = xfs_reflink_end_cow(ip, offset, size);
  408. if (error)
  409. return error;
  410. }
  411. if (flags & IOMAP_DIO_UNWRITTEN)
  412. error = xfs_iomap_write_unwritten(ip, offset, size);
  413. else if (update_size)
  414. error = xfs_setfilesize(ip, offset, size);
  415. return error;
  416. }
  417. /*
  418. * xfs_file_dio_aio_write - handle direct IO writes
  419. *
  420. * Lock the inode appropriately to prepare for and issue a direct IO write.
  421. * By separating it from the buffered write path we remove all the tricky to
  422. * follow locking changes and looping.
  423. *
  424. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  425. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  426. * pages are flushed out.
  427. *
  428. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  429. * allowing them to be done in parallel with reads and other direct IO writes.
  430. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  431. * needs to do sub-block zeroing and that requires serialisation against other
  432. * direct IOs to the same block. In this case we need to serialise the
  433. * submission of the unaligned IOs so that we don't get racing block zeroing in
  434. * the dio layer. To avoid the problem with aio, we also need to wait for
  435. * outstanding IOs to complete so that unwritten extent conversion is completed
  436. * before we try to map the overlapping block. This is currently implemented by
  437. * hitting it with a big hammer (i.e. inode_dio_wait()).
  438. *
  439. * Returns with locks held indicated by @iolock and errors indicated by
  440. * negative return values.
  441. */
  442. STATIC ssize_t
  443. xfs_file_dio_aio_write(
  444. struct kiocb *iocb,
  445. struct iov_iter *from)
  446. {
  447. struct file *file = iocb->ki_filp;
  448. struct address_space *mapping = file->f_mapping;
  449. struct inode *inode = mapping->host;
  450. struct xfs_inode *ip = XFS_I(inode);
  451. struct xfs_mount *mp = ip->i_mount;
  452. ssize_t ret = 0;
  453. int unaligned_io = 0;
  454. int iolock;
  455. size_t count = iov_iter_count(from);
  456. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  457. mp->m_rtdev_targp : mp->m_ddev_targp;
  458. /* DIO must be aligned to device logical sector size */
  459. if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
  460. return -EINVAL;
  461. /*
  462. * Don't take the exclusive iolock here unless the I/O is unaligned to
  463. * the file system block size. We don't need to consider the EOF
  464. * extension case here because xfs_file_aio_write_checks() will relock
  465. * the inode as necessary for EOF zeroing cases and fill out the new
  466. * inode size as appropriate.
  467. */
  468. if ((iocb->ki_pos & mp->m_blockmask) ||
  469. ((iocb->ki_pos + count) & mp->m_blockmask)) {
  470. unaligned_io = 1;
  471. /*
  472. * We can't properly handle unaligned direct I/O to reflink
  473. * files yet, as we can't unshare a partial block.
  474. */
  475. if (xfs_is_reflink_inode(ip)) {
  476. trace_xfs_reflink_bounce_dio_write(ip, iocb->ki_pos, count);
  477. return -EREMCHG;
  478. }
  479. iolock = XFS_IOLOCK_EXCL;
  480. } else {
  481. iolock = XFS_IOLOCK_SHARED;
  482. }
  483. if (!xfs_ilock_nowait(ip, iolock)) {
  484. if (iocb->ki_flags & IOCB_NOWAIT)
  485. return -EAGAIN;
  486. xfs_ilock(ip, iolock);
  487. }
  488. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  489. if (ret)
  490. goto out;
  491. count = iov_iter_count(from);
  492. /*
  493. * If we are doing unaligned IO, wait for all other IO to drain,
  494. * otherwise demote the lock if we had to take the exclusive lock
  495. * for other reasons in xfs_file_aio_write_checks.
  496. */
  497. if (unaligned_io) {
  498. /* If we are going to wait for other DIO to finish, bail */
  499. if (iocb->ki_flags & IOCB_NOWAIT) {
  500. if (atomic_read(&inode->i_dio_count))
  501. return -EAGAIN;
  502. } else {
  503. inode_dio_wait(inode);
  504. }
  505. } else if (iolock == XFS_IOLOCK_EXCL) {
  506. xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
  507. iolock = XFS_IOLOCK_SHARED;
  508. }
  509. trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
  510. ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, xfs_dio_write_end_io);
  511. out:
  512. xfs_iunlock(ip, iolock);
  513. /*
  514. * No fallback to buffered IO on errors for XFS, direct IO will either
  515. * complete fully or fail.
  516. */
  517. ASSERT(ret < 0 || ret == count);
  518. return ret;
  519. }
  520. static noinline ssize_t
  521. xfs_file_dax_write(
  522. struct kiocb *iocb,
  523. struct iov_iter *from)
  524. {
  525. struct inode *inode = iocb->ki_filp->f_mapping->host;
  526. struct xfs_inode *ip = XFS_I(inode);
  527. int iolock = XFS_IOLOCK_EXCL;
  528. ssize_t ret, error = 0;
  529. size_t count;
  530. loff_t pos;
  531. if (!xfs_ilock_nowait(ip, iolock)) {
  532. if (iocb->ki_flags & IOCB_NOWAIT)
  533. return -EAGAIN;
  534. xfs_ilock(ip, iolock);
  535. }
  536. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  537. if (ret)
  538. goto out;
  539. pos = iocb->ki_pos;
  540. count = iov_iter_count(from);
  541. trace_xfs_file_dax_write(ip, count, pos);
  542. ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops);
  543. if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
  544. i_size_write(inode, iocb->ki_pos);
  545. error = xfs_setfilesize(ip, pos, ret);
  546. }
  547. out:
  548. xfs_iunlock(ip, iolock);
  549. return error ? error : ret;
  550. }
  551. STATIC ssize_t
  552. xfs_file_buffered_aio_write(
  553. struct kiocb *iocb,
  554. struct iov_iter *from)
  555. {
  556. struct file *file = iocb->ki_filp;
  557. struct address_space *mapping = file->f_mapping;
  558. struct inode *inode = mapping->host;
  559. struct xfs_inode *ip = XFS_I(inode);
  560. ssize_t ret;
  561. int enospc = 0;
  562. int iolock;
  563. write_retry:
  564. iolock = XFS_IOLOCK_EXCL;
  565. xfs_ilock(ip, iolock);
  566. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  567. if (ret)
  568. goto out;
  569. /* We can write back this queue in page reclaim */
  570. current->backing_dev_info = inode_to_bdi(inode);
  571. trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
  572. ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops);
  573. if (likely(ret >= 0))
  574. iocb->ki_pos += ret;
  575. /*
  576. * If we hit a space limit, try to free up some lingering preallocated
  577. * space before returning an error. In the case of ENOSPC, first try to
  578. * write back all dirty inodes to free up some of the excess reserved
  579. * metadata space. This reduces the chances that the eofblocks scan
  580. * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
  581. * also behaves as a filter to prevent too many eofblocks scans from
  582. * running at the same time.
  583. */
  584. if (ret == -EDQUOT && !enospc) {
  585. xfs_iunlock(ip, iolock);
  586. enospc = xfs_inode_free_quota_eofblocks(ip);
  587. if (enospc)
  588. goto write_retry;
  589. enospc = xfs_inode_free_quota_cowblocks(ip);
  590. if (enospc)
  591. goto write_retry;
  592. iolock = 0;
  593. } else if (ret == -ENOSPC && !enospc) {
  594. struct xfs_eofblocks eofb = {0};
  595. enospc = 1;
  596. xfs_flush_inodes(ip->i_mount);
  597. xfs_iunlock(ip, iolock);
  598. eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
  599. xfs_icache_free_eofblocks(ip->i_mount, &eofb);
  600. xfs_icache_free_cowblocks(ip->i_mount, &eofb);
  601. goto write_retry;
  602. }
  603. current->backing_dev_info = NULL;
  604. out:
  605. if (iolock)
  606. xfs_iunlock(ip, iolock);
  607. return ret;
  608. }
  609. STATIC ssize_t
  610. xfs_file_write_iter(
  611. struct kiocb *iocb,
  612. struct iov_iter *from)
  613. {
  614. struct file *file = iocb->ki_filp;
  615. struct address_space *mapping = file->f_mapping;
  616. struct inode *inode = mapping->host;
  617. struct xfs_inode *ip = XFS_I(inode);
  618. ssize_t ret;
  619. size_t ocount = iov_iter_count(from);
  620. XFS_STATS_INC(ip->i_mount, xs_write_calls);
  621. if (ocount == 0)
  622. return 0;
  623. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  624. return -EIO;
  625. if (IS_DAX(inode))
  626. ret = xfs_file_dax_write(iocb, from);
  627. else if (iocb->ki_flags & IOCB_DIRECT) {
  628. /*
  629. * Allow a directio write to fall back to a buffered
  630. * write *only* in the case that we're doing a reflink
  631. * CoW. In all other directio scenarios we do not
  632. * allow an operation to fall back to buffered mode.
  633. */
  634. ret = xfs_file_dio_aio_write(iocb, from);
  635. if (ret == -EREMCHG)
  636. goto buffered;
  637. } else {
  638. buffered:
  639. ret = xfs_file_buffered_aio_write(iocb, from);
  640. }
  641. if (ret > 0) {
  642. XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
  643. /* Handle various SYNC-type writes */
  644. ret = generic_write_sync(iocb, ret);
  645. }
  646. return ret;
  647. }
  648. #define XFS_FALLOC_FL_SUPPORTED \
  649. (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
  650. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
  651. FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
  652. STATIC long
  653. xfs_file_fallocate(
  654. struct file *file,
  655. int mode,
  656. loff_t offset,
  657. loff_t len)
  658. {
  659. struct inode *inode = file_inode(file);
  660. struct xfs_inode *ip = XFS_I(inode);
  661. long error;
  662. enum xfs_prealloc_flags flags = 0;
  663. uint iolock = XFS_IOLOCK_EXCL;
  664. loff_t new_size = 0;
  665. bool do_file_insert = 0;
  666. if (!S_ISREG(inode->i_mode))
  667. return -EINVAL;
  668. if (mode & ~XFS_FALLOC_FL_SUPPORTED)
  669. return -EOPNOTSUPP;
  670. xfs_ilock(ip, iolock);
  671. error = xfs_break_layouts(inode, &iolock);
  672. if (error)
  673. goto out_unlock;
  674. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  675. iolock |= XFS_MMAPLOCK_EXCL;
  676. if (mode & FALLOC_FL_PUNCH_HOLE) {
  677. error = xfs_free_file_space(ip, offset, len);
  678. if (error)
  679. goto out_unlock;
  680. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  681. unsigned int blksize_mask = i_blocksize(inode) - 1;
  682. if (offset & blksize_mask || len & blksize_mask) {
  683. error = -EINVAL;
  684. goto out_unlock;
  685. }
  686. /*
  687. * There is no need to overlap collapse range with EOF,
  688. * in which case it is effectively a truncate operation
  689. */
  690. if (offset + len >= i_size_read(inode)) {
  691. error = -EINVAL;
  692. goto out_unlock;
  693. }
  694. new_size = i_size_read(inode) - len;
  695. error = xfs_collapse_file_space(ip, offset, len);
  696. if (error)
  697. goto out_unlock;
  698. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  699. unsigned int blksize_mask = i_blocksize(inode) - 1;
  700. new_size = i_size_read(inode) + len;
  701. if (offset & blksize_mask || len & blksize_mask) {
  702. error = -EINVAL;
  703. goto out_unlock;
  704. }
  705. /* check the new inode size does not wrap through zero */
  706. if (new_size > inode->i_sb->s_maxbytes) {
  707. error = -EFBIG;
  708. goto out_unlock;
  709. }
  710. /* Offset should be less than i_size */
  711. if (offset >= i_size_read(inode)) {
  712. error = -EINVAL;
  713. goto out_unlock;
  714. }
  715. do_file_insert = 1;
  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. int error;
  785. error = xfs_reflink_remap_range(src_file, loff, dst_file, dst_loff,
  786. len, true);
  787. if (error)
  788. return error;
  789. return len;
  790. }
  791. STATIC int
  792. xfs_file_open(
  793. struct inode *inode,
  794. struct file *file)
  795. {
  796. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  797. return -EFBIG;
  798. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  799. return -EIO;
  800. file->f_mode |= FMODE_AIO_NOWAIT;
  801. return 0;
  802. }
  803. STATIC int
  804. xfs_dir_open(
  805. struct inode *inode,
  806. struct file *file)
  807. {
  808. struct xfs_inode *ip = XFS_I(inode);
  809. int mode;
  810. int error;
  811. error = xfs_file_open(inode, file);
  812. if (error)
  813. return error;
  814. /*
  815. * If there are any blocks, read-ahead block 0 as we're almost
  816. * certain to have the next operation be a read there.
  817. */
  818. mode = xfs_ilock_data_map_shared(ip);
  819. if (ip->i_d.di_nextents > 0)
  820. error = xfs_dir3_data_readahead(ip, 0, -1);
  821. xfs_iunlock(ip, mode);
  822. return error;
  823. }
  824. STATIC int
  825. xfs_file_release(
  826. struct inode *inode,
  827. struct file *filp)
  828. {
  829. return xfs_release(XFS_I(inode));
  830. }
  831. STATIC int
  832. xfs_file_readdir(
  833. struct file *file,
  834. struct dir_context *ctx)
  835. {
  836. struct inode *inode = file_inode(file);
  837. xfs_inode_t *ip = XFS_I(inode);
  838. size_t bufsize;
  839. /*
  840. * The Linux API doesn't pass down the total size of the buffer
  841. * we read into down to the filesystem. With the filldir concept
  842. * it's not needed for correct information, but the XFS dir2 leaf
  843. * code wants an estimate of the buffer size to calculate it's
  844. * readahead window and size the buffers used for mapping to
  845. * physical blocks.
  846. *
  847. * Try to give it an estimate that's good enough, maybe at some
  848. * point we can change the ->readdir prototype to include the
  849. * buffer size. For now we use the current glibc buffer size.
  850. */
  851. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  852. return xfs_readdir(NULL, ip, ctx, bufsize);
  853. }
  854. STATIC loff_t
  855. xfs_file_llseek(
  856. struct file *file,
  857. loff_t offset,
  858. int whence)
  859. {
  860. struct inode *inode = file->f_mapping->host;
  861. if (XFS_FORCED_SHUTDOWN(XFS_I(inode)->i_mount))
  862. return -EIO;
  863. switch (whence) {
  864. default:
  865. return generic_file_llseek(file, offset, whence);
  866. case SEEK_HOLE:
  867. offset = iomap_seek_hole(inode, offset, &xfs_iomap_ops);
  868. break;
  869. case SEEK_DATA:
  870. offset = iomap_seek_data(inode, offset, &xfs_iomap_ops);
  871. break;
  872. }
  873. if (offset < 0)
  874. return offset;
  875. return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  876. }
  877. /*
  878. * Locking for serialisation of IO during page faults. This results in a lock
  879. * ordering of:
  880. *
  881. * mmap_sem (MM)
  882. * sb_start_pagefault(vfs, freeze)
  883. * i_mmaplock (XFS - truncate serialisation)
  884. * page_lock (MM)
  885. * i_lock (XFS - extent map serialisation)
  886. */
  887. /*
  888. * mmap()d file has taken write protection fault and is being made writable. We
  889. * can set the page state up correctly for a writable page, which means we can
  890. * do correct delalloc accounting (ENOSPC checking!) and unwritten extent
  891. * mapping.
  892. */
  893. STATIC int
  894. xfs_filemap_page_mkwrite(
  895. struct vm_fault *vmf)
  896. {
  897. struct inode *inode = file_inode(vmf->vma->vm_file);
  898. int ret;
  899. trace_xfs_filemap_page_mkwrite(XFS_I(inode));
  900. sb_start_pagefault(inode->i_sb);
  901. file_update_time(vmf->vma->vm_file);
  902. xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  903. if (IS_DAX(inode)) {
  904. ret = dax_iomap_fault(vmf, PE_SIZE_PTE, &xfs_iomap_ops);
  905. } else {
  906. ret = iomap_page_mkwrite(vmf, &xfs_iomap_ops);
  907. ret = block_page_mkwrite_return(ret);
  908. }
  909. xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  910. sb_end_pagefault(inode->i_sb);
  911. return ret;
  912. }
  913. STATIC int
  914. xfs_filemap_fault(
  915. struct vm_fault *vmf)
  916. {
  917. struct inode *inode = file_inode(vmf->vma->vm_file);
  918. int ret;
  919. trace_xfs_filemap_fault(XFS_I(inode));
  920. /* DAX can shortcut the normal fault path on write faults! */
  921. if ((vmf->flags & FAULT_FLAG_WRITE) && IS_DAX(inode))
  922. return xfs_filemap_page_mkwrite(vmf);
  923. xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  924. if (IS_DAX(inode))
  925. ret = dax_iomap_fault(vmf, PE_SIZE_PTE, &xfs_iomap_ops);
  926. else
  927. ret = filemap_fault(vmf);
  928. xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  929. return ret;
  930. }
  931. /*
  932. * Similar to xfs_filemap_fault(), the DAX fault path can call into here on
  933. * both read and write faults. Hence we need to handle both cases. There is no
  934. * ->huge_mkwrite callout for huge pages, so we have a single function here to
  935. * handle both cases here. @flags carries the information on the type of fault
  936. * occuring.
  937. */
  938. STATIC int
  939. xfs_filemap_huge_fault(
  940. struct vm_fault *vmf,
  941. enum page_entry_size pe_size)
  942. {
  943. struct inode *inode = file_inode(vmf->vma->vm_file);
  944. struct xfs_inode *ip = XFS_I(inode);
  945. int ret;
  946. if (!IS_DAX(inode))
  947. return VM_FAULT_FALLBACK;
  948. trace_xfs_filemap_huge_fault(ip);
  949. if (vmf->flags & FAULT_FLAG_WRITE) {
  950. sb_start_pagefault(inode->i_sb);
  951. file_update_time(vmf->vma->vm_file);
  952. }
  953. xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  954. ret = dax_iomap_fault(vmf, pe_size, &xfs_iomap_ops);
  955. xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  956. if (vmf->flags & FAULT_FLAG_WRITE)
  957. sb_end_pagefault(inode->i_sb);
  958. return ret;
  959. }
  960. /*
  961. * pfn_mkwrite was originally inteneded to ensure we capture time stamp
  962. * updates on write faults. In reality, it's need to serialise against
  963. * truncate similar to page_mkwrite. Hence we cycle the XFS_MMAPLOCK_SHARED
  964. * to ensure we serialise the fault barrier in place.
  965. */
  966. static int
  967. xfs_filemap_pfn_mkwrite(
  968. struct vm_fault *vmf)
  969. {
  970. struct inode *inode = file_inode(vmf->vma->vm_file);
  971. struct xfs_inode *ip = XFS_I(inode);
  972. int ret = VM_FAULT_NOPAGE;
  973. loff_t size;
  974. trace_xfs_filemap_pfn_mkwrite(ip);
  975. sb_start_pagefault(inode->i_sb);
  976. file_update_time(vmf->vma->vm_file);
  977. /* check if the faulting page hasn't raced with truncate */
  978. xfs_ilock(ip, XFS_MMAPLOCK_SHARED);
  979. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  980. if (vmf->pgoff >= size)
  981. ret = VM_FAULT_SIGBUS;
  982. else if (IS_DAX(inode))
  983. ret = dax_pfn_mkwrite(vmf);
  984. xfs_iunlock(ip, XFS_MMAPLOCK_SHARED);
  985. sb_end_pagefault(inode->i_sb);
  986. return ret;
  987. }
  988. static const struct vm_operations_struct xfs_file_vm_ops = {
  989. .fault = xfs_filemap_fault,
  990. .huge_fault = xfs_filemap_huge_fault,
  991. .map_pages = filemap_map_pages,
  992. .page_mkwrite = xfs_filemap_page_mkwrite,
  993. .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
  994. };
  995. STATIC int
  996. xfs_file_mmap(
  997. struct file *filp,
  998. struct vm_area_struct *vma)
  999. {
  1000. file_accessed(filp);
  1001. vma->vm_ops = &xfs_file_vm_ops;
  1002. if (IS_DAX(file_inode(filp)))
  1003. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  1004. return 0;
  1005. }
  1006. const struct file_operations xfs_file_operations = {
  1007. .llseek = xfs_file_llseek,
  1008. .read_iter = xfs_file_read_iter,
  1009. .write_iter = xfs_file_write_iter,
  1010. .splice_read = generic_file_splice_read,
  1011. .splice_write = iter_file_splice_write,
  1012. .unlocked_ioctl = xfs_file_ioctl,
  1013. #ifdef CONFIG_COMPAT
  1014. .compat_ioctl = xfs_file_compat_ioctl,
  1015. #endif
  1016. .mmap = xfs_file_mmap,
  1017. .open = xfs_file_open,
  1018. .release = xfs_file_release,
  1019. .fsync = xfs_file_fsync,
  1020. .get_unmapped_area = thp_get_unmapped_area,
  1021. .fallocate = xfs_file_fallocate,
  1022. .clone_file_range = xfs_file_clone_range,
  1023. .dedupe_file_range = xfs_file_dedupe_range,
  1024. };
  1025. const struct file_operations xfs_dir_file_operations = {
  1026. .open = xfs_dir_open,
  1027. .read = generic_read_dir,
  1028. .iterate_shared = xfs_file_readdir,
  1029. .llseek = generic_file_llseek,
  1030. .unlocked_ioctl = xfs_file_ioctl,
  1031. #ifdef CONFIG_COMPAT
  1032. .compat_ioctl = xfs_file_compat_ioctl,
  1033. #endif
  1034. .fsync = xfs_dir_fsync,
  1035. };