xfs_file.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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 = filemap_write_and_wait_range(inode->i_mapping, 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. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  208. ret = dax_iomap_rw(iocb, to, &xfs_iomap_ops);
  209. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  210. file_accessed(iocb->ki_filp);
  211. return ret;
  212. }
  213. STATIC ssize_t
  214. xfs_file_buffered_aio_read(
  215. struct kiocb *iocb,
  216. struct iov_iter *to)
  217. {
  218. struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp));
  219. ssize_t ret;
  220. trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos);
  221. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  222. ret = generic_file_read_iter(iocb, to);
  223. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  224. return ret;
  225. }
  226. STATIC ssize_t
  227. xfs_file_read_iter(
  228. struct kiocb *iocb,
  229. struct iov_iter *to)
  230. {
  231. struct inode *inode = file_inode(iocb->ki_filp);
  232. struct xfs_mount *mp = XFS_I(inode)->i_mount;
  233. ssize_t ret = 0;
  234. XFS_STATS_INC(mp, xs_read_calls);
  235. if (XFS_FORCED_SHUTDOWN(mp))
  236. return -EIO;
  237. if (IS_DAX(inode))
  238. ret = xfs_file_dax_read(iocb, to);
  239. else if (iocb->ki_flags & IOCB_DIRECT)
  240. ret = xfs_file_dio_aio_read(iocb, to);
  241. else
  242. ret = xfs_file_buffered_aio_read(iocb, to);
  243. if (ret > 0)
  244. XFS_STATS_ADD(mp, xs_read_bytes, ret);
  245. return ret;
  246. }
  247. /*
  248. * Zero any on disk space between the current EOF and the new, larger EOF.
  249. *
  250. * This handles the normal case of zeroing the remainder of the last block in
  251. * the file and the unusual case of zeroing blocks out beyond the size of the
  252. * file. This second case only happens with fixed size extents and when the
  253. * system crashes before the inode size was updated but after blocks were
  254. * allocated.
  255. *
  256. * Expects the iolock to be held exclusive, and will take the ilock internally.
  257. */
  258. int /* error (positive) */
  259. xfs_zero_eof(
  260. struct xfs_inode *ip,
  261. xfs_off_t offset, /* starting I/O offset */
  262. xfs_fsize_t isize, /* current inode size */
  263. bool *did_zeroing)
  264. {
  265. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  266. ASSERT(offset > isize);
  267. trace_xfs_zero_eof(ip, isize, offset - isize);
  268. return xfs_zero_range(ip, isize, offset - isize, did_zeroing);
  269. }
  270. /*
  271. * Common pre-write limit and setup checks.
  272. *
  273. * Called with the iolocked held either shared and exclusive according to
  274. * @iolock, and returns with it held. Might upgrade the iolock to exclusive
  275. * if called for a direct write beyond i_size.
  276. */
  277. STATIC ssize_t
  278. xfs_file_aio_write_checks(
  279. struct kiocb *iocb,
  280. struct iov_iter *from,
  281. int *iolock)
  282. {
  283. struct file *file = iocb->ki_filp;
  284. struct inode *inode = file->f_mapping->host;
  285. struct xfs_inode *ip = XFS_I(inode);
  286. ssize_t error = 0;
  287. size_t count = iov_iter_count(from);
  288. bool drained_dio = false;
  289. restart:
  290. error = generic_write_checks(iocb, from);
  291. if (error <= 0)
  292. return error;
  293. error = xfs_break_layouts(inode, iolock);
  294. if (error)
  295. return error;
  296. /*
  297. * For changing security info in file_remove_privs() we need i_rwsem
  298. * exclusively.
  299. */
  300. if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) {
  301. xfs_iunlock(ip, *iolock);
  302. *iolock = XFS_IOLOCK_EXCL;
  303. xfs_ilock(ip, *iolock);
  304. goto restart;
  305. }
  306. /*
  307. * If the offset is beyond the size of the file, we need to zero any
  308. * blocks that fall between the existing EOF and the start of this
  309. * write. If zeroing is needed and we are currently holding the
  310. * iolock shared, we need to update it to exclusive which implies
  311. * having to redo all checks before.
  312. *
  313. * We need to serialise against EOF updates that occur in IO
  314. * completions here. We want to make sure that nobody is changing the
  315. * size while we do this check until we have placed an IO barrier (i.e.
  316. * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched.
  317. * The spinlock effectively forms a memory barrier once we have the
  318. * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value
  319. * and hence be able to correctly determine if we need to run zeroing.
  320. */
  321. spin_lock(&ip->i_flags_lock);
  322. if (iocb->ki_pos > i_size_read(inode)) {
  323. bool zero = false;
  324. spin_unlock(&ip->i_flags_lock);
  325. if (!drained_dio) {
  326. if (*iolock == XFS_IOLOCK_SHARED) {
  327. xfs_iunlock(ip, *iolock);
  328. *iolock = XFS_IOLOCK_EXCL;
  329. xfs_ilock(ip, *iolock);
  330. iov_iter_reexpand(from, count);
  331. }
  332. /*
  333. * We now have an IO submission barrier in place, but
  334. * AIO can do EOF updates during IO completion and hence
  335. * we now need to wait for all of them to drain. Non-AIO
  336. * DIO will have drained before we are given the
  337. * XFS_IOLOCK_EXCL, and so for most cases this wait is a
  338. * no-op.
  339. */
  340. inode_dio_wait(inode);
  341. drained_dio = true;
  342. goto restart;
  343. }
  344. error = xfs_zero_eof(ip, iocb->ki_pos, i_size_read(inode), &zero);
  345. if (error)
  346. return error;
  347. } else
  348. spin_unlock(&ip->i_flags_lock);
  349. /*
  350. * Updating the timestamps will grab the ilock again from
  351. * xfs_fs_dirty_inode, so we have to call it after dropping the
  352. * lock above. Eventually we should look into a way to avoid
  353. * the pointless lock roundtrip.
  354. */
  355. if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
  356. error = file_update_time(file);
  357. if (error)
  358. return error;
  359. }
  360. /*
  361. * If we're writing the file then make sure to clear the setuid and
  362. * setgid bits if the process is not being run by root. This keeps
  363. * people from modifying setuid and setgid binaries.
  364. */
  365. if (!IS_NOSEC(inode))
  366. return file_remove_privs(file);
  367. return 0;
  368. }
  369. static int
  370. xfs_dio_write_end_io(
  371. struct kiocb *iocb,
  372. ssize_t size,
  373. unsigned flags)
  374. {
  375. struct inode *inode = file_inode(iocb->ki_filp);
  376. struct xfs_inode *ip = XFS_I(inode);
  377. loff_t offset = iocb->ki_pos;
  378. bool update_size = false;
  379. int error = 0;
  380. trace_xfs_end_io_direct_write(ip, offset, size);
  381. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  382. return -EIO;
  383. if (size <= 0)
  384. return size;
  385. /*
  386. * We need to update the in-core inode size here so that we don't end up
  387. * with the on-disk inode size being outside the in-core inode size. We
  388. * have no other method of updating EOF for AIO, so always do it here
  389. * if necessary.
  390. *
  391. * We need to lock the test/set EOF update as we can be racing with
  392. * other IO completions here to update the EOF. Failing to serialise
  393. * here can result in EOF moving backwards and Bad Things Happen when
  394. * that occurs.
  395. */
  396. spin_lock(&ip->i_flags_lock);
  397. if (offset + size > i_size_read(inode)) {
  398. i_size_write(inode, offset + size);
  399. update_size = true;
  400. }
  401. spin_unlock(&ip->i_flags_lock);
  402. if (flags & IOMAP_DIO_COW) {
  403. error = xfs_reflink_end_cow(ip, offset, size);
  404. if (error)
  405. return error;
  406. }
  407. if (flags & IOMAP_DIO_UNWRITTEN)
  408. error = xfs_iomap_write_unwritten(ip, offset, size);
  409. else if (update_size)
  410. error = xfs_setfilesize(ip, offset, size);
  411. return error;
  412. }
  413. /*
  414. * xfs_file_dio_aio_write - handle direct IO writes
  415. *
  416. * Lock the inode appropriately to prepare for and issue a direct IO write.
  417. * By separating it from the buffered write path we remove all the tricky to
  418. * follow locking changes and looping.
  419. *
  420. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  421. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  422. * pages are flushed out.
  423. *
  424. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  425. * allowing them to be done in parallel with reads and other direct IO writes.
  426. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  427. * needs to do sub-block zeroing and that requires serialisation against other
  428. * direct IOs to the same block. In this case we need to serialise the
  429. * submission of the unaligned IOs so that we don't get racing block zeroing in
  430. * the dio layer. To avoid the problem with aio, we also need to wait for
  431. * outstanding IOs to complete so that unwritten extent conversion is completed
  432. * before we try to map the overlapping block. This is currently implemented by
  433. * hitting it with a big hammer (i.e. inode_dio_wait()).
  434. *
  435. * Returns with locks held indicated by @iolock and errors indicated by
  436. * negative return values.
  437. */
  438. STATIC ssize_t
  439. xfs_file_dio_aio_write(
  440. struct kiocb *iocb,
  441. struct iov_iter *from)
  442. {
  443. struct file *file = iocb->ki_filp;
  444. struct address_space *mapping = file->f_mapping;
  445. struct inode *inode = mapping->host;
  446. struct xfs_inode *ip = XFS_I(inode);
  447. struct xfs_mount *mp = ip->i_mount;
  448. ssize_t ret = 0;
  449. int unaligned_io = 0;
  450. int iolock;
  451. size_t count = iov_iter_count(from);
  452. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  453. mp->m_rtdev_targp : mp->m_ddev_targp;
  454. /* DIO must be aligned to device logical sector size */
  455. if ((iocb->ki_pos | count) & target->bt_logical_sectormask)
  456. return -EINVAL;
  457. /*
  458. * Don't take the exclusive iolock here unless the I/O is unaligned to
  459. * the file system block size. We don't need to consider the EOF
  460. * extension case here because xfs_file_aio_write_checks() will relock
  461. * the inode as necessary for EOF zeroing cases and fill out the new
  462. * inode size as appropriate.
  463. */
  464. if ((iocb->ki_pos & mp->m_blockmask) ||
  465. ((iocb->ki_pos + count) & mp->m_blockmask)) {
  466. unaligned_io = 1;
  467. iolock = XFS_IOLOCK_EXCL;
  468. } else {
  469. iolock = XFS_IOLOCK_SHARED;
  470. }
  471. xfs_ilock(ip, iolock);
  472. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  473. if (ret)
  474. goto out;
  475. count = iov_iter_count(from);
  476. /*
  477. * If we are doing unaligned IO, wait for all other IO to drain,
  478. * otherwise demote the lock if we had to take the exclusive lock
  479. * for other reasons in xfs_file_aio_write_checks.
  480. */
  481. if (unaligned_io)
  482. inode_dio_wait(inode);
  483. else if (iolock == XFS_IOLOCK_EXCL) {
  484. xfs_ilock_demote(ip, XFS_IOLOCK_EXCL);
  485. iolock = XFS_IOLOCK_SHARED;
  486. }
  487. trace_xfs_file_direct_write(ip, count, iocb->ki_pos);
  488. /* If this is a block-aligned directio CoW, remap immediately. */
  489. if (xfs_is_reflink_inode(ip) && !unaligned_io) {
  490. ret = xfs_reflink_allocate_cow_range(ip, iocb->ki_pos, count);
  491. if (ret)
  492. goto out;
  493. }
  494. ret = iomap_dio_rw(iocb, from, &xfs_iomap_ops, xfs_dio_write_end_io);
  495. out:
  496. xfs_iunlock(ip, iolock);
  497. /*
  498. * No fallback to buffered IO on errors for XFS, direct IO will either
  499. * complete fully or fail.
  500. */
  501. ASSERT(ret < 0 || ret == count);
  502. return ret;
  503. }
  504. static noinline ssize_t
  505. xfs_file_dax_write(
  506. struct kiocb *iocb,
  507. struct iov_iter *from)
  508. {
  509. struct inode *inode = iocb->ki_filp->f_mapping->host;
  510. struct xfs_inode *ip = XFS_I(inode);
  511. int iolock = XFS_IOLOCK_EXCL;
  512. ssize_t ret, error = 0;
  513. size_t count;
  514. loff_t pos;
  515. xfs_ilock(ip, iolock);
  516. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  517. if (ret)
  518. goto out;
  519. pos = iocb->ki_pos;
  520. count = iov_iter_count(from);
  521. trace_xfs_file_dax_write(ip, count, pos);
  522. ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops);
  523. if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
  524. i_size_write(inode, iocb->ki_pos);
  525. error = xfs_setfilesize(ip, pos, ret);
  526. }
  527. out:
  528. xfs_iunlock(ip, iolock);
  529. return error ? error : ret;
  530. }
  531. STATIC ssize_t
  532. xfs_file_buffered_aio_write(
  533. struct kiocb *iocb,
  534. struct iov_iter *from)
  535. {
  536. struct file *file = iocb->ki_filp;
  537. struct address_space *mapping = file->f_mapping;
  538. struct inode *inode = mapping->host;
  539. struct xfs_inode *ip = XFS_I(inode);
  540. ssize_t ret;
  541. int enospc = 0;
  542. int iolock = XFS_IOLOCK_EXCL;
  543. xfs_ilock(ip, iolock);
  544. ret = xfs_file_aio_write_checks(iocb, from, &iolock);
  545. if (ret)
  546. goto out;
  547. /* We can write back this queue in page reclaim */
  548. current->backing_dev_info = inode_to_bdi(inode);
  549. write_retry:
  550. trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos);
  551. ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops);
  552. if (likely(ret >= 0))
  553. iocb->ki_pos += ret;
  554. /*
  555. * If we hit a space limit, try to free up some lingering preallocated
  556. * space before returning an error. In the case of ENOSPC, first try to
  557. * write back all dirty inodes to free up some of the excess reserved
  558. * metadata space. This reduces the chances that the eofblocks scan
  559. * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this
  560. * also behaves as a filter to prevent too many eofblocks scans from
  561. * running at the same time.
  562. */
  563. if (ret == -EDQUOT && !enospc) {
  564. enospc = xfs_inode_free_quota_eofblocks(ip);
  565. if (enospc)
  566. goto write_retry;
  567. enospc = xfs_inode_free_quota_cowblocks(ip);
  568. if (enospc)
  569. goto write_retry;
  570. } else if (ret == -ENOSPC && !enospc) {
  571. struct xfs_eofblocks eofb = {0};
  572. enospc = 1;
  573. xfs_flush_inodes(ip->i_mount);
  574. eofb.eof_scan_owner = ip->i_ino; /* for locking */
  575. eofb.eof_flags = XFS_EOF_FLAGS_SYNC;
  576. xfs_icache_free_eofblocks(ip->i_mount, &eofb);
  577. goto write_retry;
  578. }
  579. current->backing_dev_info = NULL;
  580. out:
  581. xfs_iunlock(ip, iolock);
  582. return ret;
  583. }
  584. STATIC ssize_t
  585. xfs_file_write_iter(
  586. struct kiocb *iocb,
  587. struct iov_iter *from)
  588. {
  589. struct file *file = iocb->ki_filp;
  590. struct address_space *mapping = file->f_mapping;
  591. struct inode *inode = mapping->host;
  592. struct xfs_inode *ip = XFS_I(inode);
  593. ssize_t ret;
  594. size_t ocount = iov_iter_count(from);
  595. XFS_STATS_INC(ip->i_mount, xs_write_calls);
  596. if (ocount == 0)
  597. return 0;
  598. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  599. return -EIO;
  600. if (IS_DAX(inode))
  601. ret = xfs_file_dax_write(iocb, from);
  602. else if (iocb->ki_flags & IOCB_DIRECT) {
  603. /*
  604. * Allow a directio write to fall back to a buffered
  605. * write *only* in the case that we're doing a reflink
  606. * CoW. In all other directio scenarios we do not
  607. * allow an operation to fall back to buffered mode.
  608. */
  609. ret = xfs_file_dio_aio_write(iocb, from);
  610. if (ret == -EREMCHG)
  611. goto buffered;
  612. } else {
  613. buffered:
  614. ret = xfs_file_buffered_aio_write(iocb, from);
  615. }
  616. if (ret > 0) {
  617. XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret);
  618. /* Handle various SYNC-type writes */
  619. ret = generic_write_sync(iocb, ret);
  620. }
  621. return ret;
  622. }
  623. #define XFS_FALLOC_FL_SUPPORTED \
  624. (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
  625. FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
  626. FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE)
  627. STATIC long
  628. xfs_file_fallocate(
  629. struct file *file,
  630. int mode,
  631. loff_t offset,
  632. loff_t len)
  633. {
  634. struct inode *inode = file_inode(file);
  635. struct xfs_inode *ip = XFS_I(inode);
  636. long error;
  637. enum xfs_prealloc_flags flags = 0;
  638. uint iolock = XFS_IOLOCK_EXCL;
  639. loff_t new_size = 0;
  640. bool do_file_insert = 0;
  641. if (!S_ISREG(inode->i_mode))
  642. return -EINVAL;
  643. if (mode & ~XFS_FALLOC_FL_SUPPORTED)
  644. return -EOPNOTSUPP;
  645. xfs_ilock(ip, iolock);
  646. error = xfs_break_layouts(inode, &iolock);
  647. if (error)
  648. goto out_unlock;
  649. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  650. iolock |= XFS_MMAPLOCK_EXCL;
  651. if (mode & FALLOC_FL_PUNCH_HOLE) {
  652. error = xfs_free_file_space(ip, offset, len);
  653. if (error)
  654. goto out_unlock;
  655. } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
  656. unsigned blksize_mask = (1 << inode->i_blkbits) - 1;
  657. if (offset & blksize_mask || len & blksize_mask) {
  658. error = -EINVAL;
  659. goto out_unlock;
  660. }
  661. /*
  662. * There is no need to overlap collapse range with EOF,
  663. * in which case it is effectively a truncate operation
  664. */
  665. if (offset + len >= i_size_read(inode)) {
  666. error = -EINVAL;
  667. goto out_unlock;
  668. }
  669. new_size = i_size_read(inode) - len;
  670. error = xfs_collapse_file_space(ip, offset, len);
  671. if (error)
  672. goto out_unlock;
  673. } else if (mode & FALLOC_FL_INSERT_RANGE) {
  674. unsigned blksize_mask = (1 << inode->i_blkbits) - 1;
  675. new_size = i_size_read(inode) + len;
  676. if (offset & blksize_mask || len & blksize_mask) {
  677. error = -EINVAL;
  678. goto out_unlock;
  679. }
  680. /* check the new inode size does not wrap through zero */
  681. if (new_size > inode->i_sb->s_maxbytes) {
  682. error = -EFBIG;
  683. goto out_unlock;
  684. }
  685. /* Offset should be less than i_size */
  686. if (offset >= i_size_read(inode)) {
  687. error = -EINVAL;
  688. goto out_unlock;
  689. }
  690. do_file_insert = 1;
  691. } else {
  692. flags |= XFS_PREALLOC_SET;
  693. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  694. offset + len > i_size_read(inode)) {
  695. new_size = offset + len;
  696. error = inode_newsize_ok(inode, new_size);
  697. if (error)
  698. goto out_unlock;
  699. }
  700. if (mode & FALLOC_FL_ZERO_RANGE)
  701. error = xfs_zero_file_space(ip, offset, len);
  702. else {
  703. if (mode & FALLOC_FL_UNSHARE_RANGE) {
  704. error = xfs_reflink_unshare(ip, offset, len);
  705. if (error)
  706. goto out_unlock;
  707. }
  708. error = xfs_alloc_file_space(ip, offset, len,
  709. XFS_BMAPI_PREALLOC);
  710. }
  711. if (error)
  712. goto out_unlock;
  713. }
  714. if (file->f_flags & O_DSYNC)
  715. flags |= XFS_PREALLOC_SYNC;
  716. error = xfs_update_prealloc_flags(ip, flags);
  717. if (error)
  718. goto out_unlock;
  719. /* Change file size if needed */
  720. if (new_size) {
  721. struct iattr iattr;
  722. iattr.ia_valid = ATTR_SIZE;
  723. iattr.ia_size = new_size;
  724. error = xfs_vn_setattr_size(file_dentry(file), &iattr);
  725. if (error)
  726. goto out_unlock;
  727. }
  728. /*
  729. * Perform hole insertion now that the file size has been
  730. * updated so that if we crash during the operation we don't
  731. * leave shifted extents past EOF and hence losing access to
  732. * the data that is contained within them.
  733. */
  734. if (do_file_insert)
  735. error = xfs_insert_file_space(ip, offset, len);
  736. out_unlock:
  737. xfs_iunlock(ip, iolock);
  738. return error;
  739. }
  740. STATIC int
  741. xfs_file_clone_range(
  742. struct file *file_in,
  743. loff_t pos_in,
  744. struct file *file_out,
  745. loff_t pos_out,
  746. u64 len)
  747. {
  748. return xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out,
  749. len, false);
  750. }
  751. STATIC ssize_t
  752. xfs_file_dedupe_range(
  753. struct file *src_file,
  754. u64 loff,
  755. u64 len,
  756. struct file *dst_file,
  757. u64 dst_loff)
  758. {
  759. int error;
  760. error = xfs_reflink_remap_range(src_file, loff, dst_file, dst_loff,
  761. len, true);
  762. if (error)
  763. return error;
  764. return len;
  765. }
  766. STATIC int
  767. xfs_file_open(
  768. struct inode *inode,
  769. struct file *file)
  770. {
  771. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  772. return -EFBIG;
  773. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  774. return -EIO;
  775. return 0;
  776. }
  777. STATIC int
  778. xfs_dir_open(
  779. struct inode *inode,
  780. struct file *file)
  781. {
  782. struct xfs_inode *ip = XFS_I(inode);
  783. int mode;
  784. int error;
  785. error = xfs_file_open(inode, file);
  786. if (error)
  787. return error;
  788. /*
  789. * If there are any blocks, read-ahead block 0 as we're almost
  790. * certain to have the next operation be a read there.
  791. */
  792. mode = xfs_ilock_data_map_shared(ip);
  793. if (ip->i_d.di_nextents > 0)
  794. xfs_dir3_data_readahead(ip, 0, -1);
  795. xfs_iunlock(ip, mode);
  796. return 0;
  797. }
  798. STATIC int
  799. xfs_file_release(
  800. struct inode *inode,
  801. struct file *filp)
  802. {
  803. return xfs_release(XFS_I(inode));
  804. }
  805. STATIC int
  806. xfs_file_readdir(
  807. struct file *file,
  808. struct dir_context *ctx)
  809. {
  810. struct inode *inode = file_inode(file);
  811. xfs_inode_t *ip = XFS_I(inode);
  812. size_t bufsize;
  813. /*
  814. * The Linux API doesn't pass down the total size of the buffer
  815. * we read into down to the filesystem. With the filldir concept
  816. * it's not needed for correct information, but the XFS dir2 leaf
  817. * code wants an estimate of the buffer size to calculate it's
  818. * readahead window and size the buffers used for mapping to
  819. * physical blocks.
  820. *
  821. * Try to give it an estimate that's good enough, maybe at some
  822. * point we can change the ->readdir prototype to include the
  823. * buffer size. For now we use the current glibc buffer size.
  824. */
  825. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  826. return xfs_readdir(ip, ctx, bufsize);
  827. }
  828. /*
  829. * This type is designed to indicate the type of offset we would like
  830. * to search from page cache for xfs_seek_hole_data().
  831. */
  832. enum {
  833. HOLE_OFF = 0,
  834. DATA_OFF,
  835. };
  836. /*
  837. * Lookup the desired type of offset from the given page.
  838. *
  839. * On success, return true and the offset argument will point to the
  840. * start of the region that was found. Otherwise this function will
  841. * return false and keep the offset argument unchanged.
  842. */
  843. STATIC bool
  844. xfs_lookup_buffer_offset(
  845. struct page *page,
  846. loff_t *offset,
  847. unsigned int type)
  848. {
  849. loff_t lastoff = page_offset(page);
  850. bool found = false;
  851. struct buffer_head *bh, *head;
  852. bh = head = page_buffers(page);
  853. do {
  854. /*
  855. * Unwritten extents that have data in the page
  856. * cache covering them can be identified by the
  857. * BH_Unwritten state flag. Pages with multiple
  858. * buffers might have a mix of holes, data and
  859. * unwritten extents - any buffer with valid
  860. * data in it should have BH_Uptodate flag set
  861. * on it.
  862. */
  863. if (buffer_unwritten(bh) ||
  864. buffer_uptodate(bh)) {
  865. if (type == DATA_OFF)
  866. found = true;
  867. } else {
  868. if (type == HOLE_OFF)
  869. found = true;
  870. }
  871. if (found) {
  872. *offset = lastoff;
  873. break;
  874. }
  875. lastoff += bh->b_size;
  876. } while ((bh = bh->b_this_page) != head);
  877. return found;
  878. }
  879. /*
  880. * This routine is called to find out and return a data or hole offset
  881. * from the page cache for unwritten extents according to the desired
  882. * type for xfs_seek_hole_data().
  883. *
  884. * The argument offset is used to tell where we start to search from the
  885. * page cache. Map is used to figure out the end points of the range to
  886. * lookup pages.
  887. *
  888. * Return true if the desired type of offset was found, and the argument
  889. * offset is filled with that address. Otherwise, return false and keep
  890. * offset unchanged.
  891. */
  892. STATIC bool
  893. xfs_find_get_desired_pgoff(
  894. struct inode *inode,
  895. struct xfs_bmbt_irec *map,
  896. unsigned int type,
  897. loff_t *offset)
  898. {
  899. struct xfs_inode *ip = XFS_I(inode);
  900. struct xfs_mount *mp = ip->i_mount;
  901. struct pagevec pvec;
  902. pgoff_t index;
  903. pgoff_t end;
  904. loff_t endoff;
  905. loff_t startoff = *offset;
  906. loff_t lastoff = startoff;
  907. bool found = false;
  908. pagevec_init(&pvec, 0);
  909. index = startoff >> PAGE_SHIFT;
  910. endoff = XFS_FSB_TO_B(mp, map->br_startoff + map->br_blockcount);
  911. end = endoff >> PAGE_SHIFT;
  912. do {
  913. int want;
  914. unsigned nr_pages;
  915. unsigned int i;
  916. want = min_t(pgoff_t, end - index, PAGEVEC_SIZE);
  917. nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
  918. want);
  919. /*
  920. * No page mapped into given range. If we are searching holes
  921. * and if this is the first time we got into the loop, it means
  922. * that the given offset is landed in a hole, return it.
  923. *
  924. * If we have already stepped through some block buffers to find
  925. * holes but they all contains data. In this case, the last
  926. * offset is already updated and pointed to the end of the last
  927. * mapped page, if it does not reach the endpoint to search,
  928. * that means there should be a hole between them.
  929. */
  930. if (nr_pages == 0) {
  931. /* Data search found nothing */
  932. if (type == DATA_OFF)
  933. break;
  934. ASSERT(type == HOLE_OFF);
  935. if (lastoff == startoff || lastoff < endoff) {
  936. found = true;
  937. *offset = lastoff;
  938. }
  939. break;
  940. }
  941. /*
  942. * At lease we found one page. If this is the first time we
  943. * step into the loop, and if the first page index offset is
  944. * greater than the given search offset, a hole was found.
  945. */
  946. if (type == HOLE_OFF && lastoff == startoff &&
  947. lastoff < page_offset(pvec.pages[0])) {
  948. found = true;
  949. break;
  950. }
  951. for (i = 0; i < nr_pages; i++) {
  952. struct page *page = pvec.pages[i];
  953. loff_t b_offset;
  954. /*
  955. * At this point, the page may be truncated or
  956. * invalidated (changing page->mapping to NULL),
  957. * or even swizzled back from swapper_space to tmpfs
  958. * file mapping. However, page->index will not change
  959. * because we have a reference on the page.
  960. *
  961. * Searching done if the page index is out of range.
  962. * If the current offset is not reaches the end of
  963. * the specified search range, there should be a hole
  964. * between them.
  965. */
  966. if (page->index > end) {
  967. if (type == HOLE_OFF && lastoff < endoff) {
  968. *offset = lastoff;
  969. found = true;
  970. }
  971. goto out;
  972. }
  973. lock_page(page);
  974. /*
  975. * Page truncated or invalidated(page->mapping == NULL).
  976. * We can freely skip it and proceed to check the next
  977. * page.
  978. */
  979. if (unlikely(page->mapping != inode->i_mapping)) {
  980. unlock_page(page);
  981. continue;
  982. }
  983. if (!page_has_buffers(page)) {
  984. unlock_page(page);
  985. continue;
  986. }
  987. found = xfs_lookup_buffer_offset(page, &b_offset, type);
  988. if (found) {
  989. /*
  990. * The found offset may be less than the start
  991. * point to search if this is the first time to
  992. * come here.
  993. */
  994. *offset = max_t(loff_t, startoff, b_offset);
  995. unlock_page(page);
  996. goto out;
  997. }
  998. /*
  999. * We either searching data but nothing was found, or
  1000. * searching hole but found a data buffer. In either
  1001. * case, probably the next page contains the desired
  1002. * things, update the last offset to it so.
  1003. */
  1004. lastoff = page_offset(page) + PAGE_SIZE;
  1005. unlock_page(page);
  1006. }
  1007. /*
  1008. * The number of returned pages less than our desired, search
  1009. * done. In this case, nothing was found for searching data,
  1010. * but we found a hole behind the last offset.
  1011. */
  1012. if (nr_pages < want) {
  1013. if (type == HOLE_OFF) {
  1014. *offset = lastoff;
  1015. found = true;
  1016. }
  1017. break;
  1018. }
  1019. index = pvec.pages[i - 1]->index + 1;
  1020. pagevec_release(&pvec);
  1021. } while (index <= end);
  1022. out:
  1023. pagevec_release(&pvec);
  1024. return found;
  1025. }
  1026. /*
  1027. * caller must lock inode with xfs_ilock_data_map_shared,
  1028. * can we craft an appropriate ASSERT?
  1029. *
  1030. * end is because the VFS-level lseek interface is defined such that any
  1031. * offset past i_size shall return -ENXIO, but we use this for quota code
  1032. * which does not maintain i_size, and we want to SEEK_DATA past i_size.
  1033. */
  1034. loff_t
  1035. __xfs_seek_hole_data(
  1036. struct inode *inode,
  1037. loff_t start,
  1038. loff_t end,
  1039. int whence)
  1040. {
  1041. struct xfs_inode *ip = XFS_I(inode);
  1042. struct xfs_mount *mp = ip->i_mount;
  1043. loff_t uninitialized_var(offset);
  1044. xfs_fileoff_t fsbno;
  1045. xfs_filblks_t lastbno;
  1046. int error;
  1047. if (start >= end) {
  1048. error = -ENXIO;
  1049. goto out_error;
  1050. }
  1051. /*
  1052. * Try to read extents from the first block indicated
  1053. * by fsbno to the end block of the file.
  1054. */
  1055. fsbno = XFS_B_TO_FSBT(mp, start);
  1056. lastbno = XFS_B_TO_FSB(mp, end);
  1057. for (;;) {
  1058. struct xfs_bmbt_irec map[2];
  1059. int nmap = 2;
  1060. unsigned int i;
  1061. error = xfs_bmapi_read(ip, fsbno, lastbno - fsbno, map, &nmap,
  1062. XFS_BMAPI_ENTIRE);
  1063. if (error)
  1064. goto out_error;
  1065. /* No extents at given offset, must be beyond EOF */
  1066. if (nmap == 0) {
  1067. error = -ENXIO;
  1068. goto out_error;
  1069. }
  1070. for (i = 0; i < nmap; i++) {
  1071. offset = max_t(loff_t, start,
  1072. XFS_FSB_TO_B(mp, map[i].br_startoff));
  1073. /* Landed in the hole we wanted? */
  1074. if (whence == SEEK_HOLE &&
  1075. map[i].br_startblock == HOLESTARTBLOCK)
  1076. goto out;
  1077. /* Landed in the data extent we wanted? */
  1078. if (whence == SEEK_DATA &&
  1079. (map[i].br_startblock == DELAYSTARTBLOCK ||
  1080. (map[i].br_state == XFS_EXT_NORM &&
  1081. !isnullstartblock(map[i].br_startblock))))
  1082. goto out;
  1083. /*
  1084. * Landed in an unwritten extent, try to search
  1085. * for hole or data from page cache.
  1086. */
  1087. if (map[i].br_state == XFS_EXT_UNWRITTEN) {
  1088. if (xfs_find_get_desired_pgoff(inode, &map[i],
  1089. whence == SEEK_HOLE ? HOLE_OFF : DATA_OFF,
  1090. &offset))
  1091. goto out;
  1092. }
  1093. }
  1094. /*
  1095. * We only received one extent out of the two requested. This
  1096. * means we've hit EOF and didn't find what we are looking for.
  1097. */
  1098. if (nmap == 1) {
  1099. /*
  1100. * If we were looking for a hole, set offset to
  1101. * the end of the file (i.e., there is an implicit
  1102. * hole at the end of any file).
  1103. */
  1104. if (whence == SEEK_HOLE) {
  1105. offset = end;
  1106. break;
  1107. }
  1108. /*
  1109. * If we were looking for data, it's nowhere to be found
  1110. */
  1111. ASSERT(whence == SEEK_DATA);
  1112. error = -ENXIO;
  1113. goto out_error;
  1114. }
  1115. ASSERT(i > 1);
  1116. /*
  1117. * Nothing was found, proceed to the next round of search
  1118. * if the next reading offset is not at or beyond EOF.
  1119. */
  1120. fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
  1121. start = XFS_FSB_TO_B(mp, fsbno);
  1122. if (start >= end) {
  1123. if (whence == SEEK_HOLE) {
  1124. offset = end;
  1125. break;
  1126. }
  1127. ASSERT(whence == SEEK_DATA);
  1128. error = -ENXIO;
  1129. goto out_error;
  1130. }
  1131. }
  1132. out:
  1133. /*
  1134. * If at this point we have found the hole we wanted, the returned
  1135. * offset may be bigger than the file size as it may be aligned to
  1136. * page boundary for unwritten extents. We need to deal with this
  1137. * situation in particular.
  1138. */
  1139. if (whence == SEEK_HOLE)
  1140. offset = min_t(loff_t, offset, end);
  1141. return offset;
  1142. out_error:
  1143. return error;
  1144. }
  1145. STATIC loff_t
  1146. xfs_seek_hole_data(
  1147. struct file *file,
  1148. loff_t start,
  1149. int whence)
  1150. {
  1151. struct inode *inode = file->f_mapping->host;
  1152. struct xfs_inode *ip = XFS_I(inode);
  1153. struct xfs_mount *mp = ip->i_mount;
  1154. uint lock;
  1155. loff_t offset, end;
  1156. int error = 0;
  1157. if (XFS_FORCED_SHUTDOWN(mp))
  1158. return -EIO;
  1159. lock = xfs_ilock_data_map_shared(ip);
  1160. end = i_size_read(inode);
  1161. offset = __xfs_seek_hole_data(inode, start, end, whence);
  1162. if (offset < 0) {
  1163. error = offset;
  1164. goto out_unlock;
  1165. }
  1166. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  1167. out_unlock:
  1168. xfs_iunlock(ip, lock);
  1169. if (error)
  1170. return error;
  1171. return offset;
  1172. }
  1173. STATIC loff_t
  1174. xfs_file_llseek(
  1175. struct file *file,
  1176. loff_t offset,
  1177. int whence)
  1178. {
  1179. switch (whence) {
  1180. case SEEK_END:
  1181. case SEEK_CUR:
  1182. case SEEK_SET:
  1183. return generic_file_llseek(file, offset, whence);
  1184. case SEEK_HOLE:
  1185. case SEEK_DATA:
  1186. return xfs_seek_hole_data(file, offset, whence);
  1187. default:
  1188. return -EINVAL;
  1189. }
  1190. }
  1191. /*
  1192. * Locking for serialisation of IO during page faults. This results in a lock
  1193. * ordering of:
  1194. *
  1195. * mmap_sem (MM)
  1196. * sb_start_pagefault(vfs, freeze)
  1197. * i_mmaplock (XFS - truncate serialisation)
  1198. * page_lock (MM)
  1199. * i_lock (XFS - extent map serialisation)
  1200. */
  1201. /*
  1202. * mmap()d file has taken write protection fault and is being made writable. We
  1203. * can set the page state up correctly for a writable page, which means we can
  1204. * do correct delalloc accounting (ENOSPC checking!) and unwritten extent
  1205. * mapping.
  1206. */
  1207. STATIC int
  1208. xfs_filemap_page_mkwrite(
  1209. struct vm_area_struct *vma,
  1210. struct vm_fault *vmf)
  1211. {
  1212. struct inode *inode = file_inode(vma->vm_file);
  1213. int ret;
  1214. trace_xfs_filemap_page_mkwrite(XFS_I(inode));
  1215. sb_start_pagefault(inode->i_sb);
  1216. file_update_time(vma->vm_file);
  1217. xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  1218. if (IS_DAX(inode)) {
  1219. ret = dax_iomap_fault(vma, vmf, &xfs_iomap_ops);
  1220. } else {
  1221. ret = iomap_page_mkwrite(vma, vmf, &xfs_iomap_ops);
  1222. ret = block_page_mkwrite_return(ret);
  1223. }
  1224. xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  1225. sb_end_pagefault(inode->i_sb);
  1226. return ret;
  1227. }
  1228. STATIC int
  1229. xfs_filemap_fault(
  1230. struct vm_area_struct *vma,
  1231. struct vm_fault *vmf)
  1232. {
  1233. struct inode *inode = file_inode(vma->vm_file);
  1234. int ret;
  1235. trace_xfs_filemap_fault(XFS_I(inode));
  1236. /* DAX can shortcut the normal fault path on write faults! */
  1237. if ((vmf->flags & FAULT_FLAG_WRITE) && IS_DAX(inode))
  1238. return xfs_filemap_page_mkwrite(vma, vmf);
  1239. xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  1240. if (IS_DAX(inode))
  1241. ret = dax_iomap_fault(vma, vmf, &xfs_iomap_ops);
  1242. else
  1243. ret = filemap_fault(vma, vmf);
  1244. xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  1245. return ret;
  1246. }
  1247. /*
  1248. * Similar to xfs_filemap_fault(), the DAX fault path can call into here on
  1249. * both read and write faults. Hence we need to handle both cases. There is no
  1250. * ->pmd_mkwrite callout for huge pages, so we have a single function here to
  1251. * handle both cases here. @flags carries the information on the type of fault
  1252. * occuring.
  1253. */
  1254. STATIC int
  1255. xfs_filemap_pmd_fault(
  1256. struct vm_area_struct *vma,
  1257. unsigned long addr,
  1258. pmd_t *pmd,
  1259. unsigned int flags)
  1260. {
  1261. struct inode *inode = file_inode(vma->vm_file);
  1262. struct xfs_inode *ip = XFS_I(inode);
  1263. int ret;
  1264. if (!IS_DAX(inode))
  1265. return VM_FAULT_FALLBACK;
  1266. trace_xfs_filemap_pmd_fault(ip);
  1267. if (flags & FAULT_FLAG_WRITE) {
  1268. sb_start_pagefault(inode->i_sb);
  1269. file_update_time(vma->vm_file);
  1270. }
  1271. xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  1272. ret = dax_iomap_pmd_fault(vma, addr, pmd, flags, &xfs_iomap_ops);
  1273. xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED);
  1274. if (flags & FAULT_FLAG_WRITE)
  1275. sb_end_pagefault(inode->i_sb);
  1276. return ret;
  1277. }
  1278. /*
  1279. * pfn_mkwrite was originally inteneded to ensure we capture time stamp
  1280. * updates on write faults. In reality, it's need to serialise against
  1281. * truncate similar to page_mkwrite. Hence we cycle the XFS_MMAPLOCK_SHARED
  1282. * to ensure we serialise the fault barrier in place.
  1283. */
  1284. static int
  1285. xfs_filemap_pfn_mkwrite(
  1286. struct vm_area_struct *vma,
  1287. struct vm_fault *vmf)
  1288. {
  1289. struct inode *inode = file_inode(vma->vm_file);
  1290. struct xfs_inode *ip = XFS_I(inode);
  1291. int ret = VM_FAULT_NOPAGE;
  1292. loff_t size;
  1293. trace_xfs_filemap_pfn_mkwrite(ip);
  1294. sb_start_pagefault(inode->i_sb);
  1295. file_update_time(vma->vm_file);
  1296. /* check if the faulting page hasn't raced with truncate */
  1297. xfs_ilock(ip, XFS_MMAPLOCK_SHARED);
  1298. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1299. if (vmf->pgoff >= size)
  1300. ret = VM_FAULT_SIGBUS;
  1301. else if (IS_DAX(inode))
  1302. ret = dax_pfn_mkwrite(vma, vmf);
  1303. xfs_iunlock(ip, XFS_MMAPLOCK_SHARED);
  1304. sb_end_pagefault(inode->i_sb);
  1305. return ret;
  1306. }
  1307. static const struct vm_operations_struct xfs_file_vm_ops = {
  1308. .fault = xfs_filemap_fault,
  1309. .pmd_fault = xfs_filemap_pmd_fault,
  1310. .map_pages = filemap_map_pages,
  1311. .page_mkwrite = xfs_filemap_page_mkwrite,
  1312. .pfn_mkwrite = xfs_filemap_pfn_mkwrite,
  1313. };
  1314. STATIC int
  1315. xfs_file_mmap(
  1316. struct file *filp,
  1317. struct vm_area_struct *vma)
  1318. {
  1319. file_accessed(filp);
  1320. vma->vm_ops = &xfs_file_vm_ops;
  1321. if (IS_DAX(file_inode(filp)))
  1322. vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
  1323. return 0;
  1324. }
  1325. const struct file_operations xfs_file_operations = {
  1326. .llseek = xfs_file_llseek,
  1327. .read_iter = xfs_file_read_iter,
  1328. .write_iter = xfs_file_write_iter,
  1329. .splice_read = generic_file_splice_read,
  1330. .splice_write = iter_file_splice_write,
  1331. .unlocked_ioctl = xfs_file_ioctl,
  1332. #ifdef CONFIG_COMPAT
  1333. .compat_ioctl = xfs_file_compat_ioctl,
  1334. #endif
  1335. .mmap = xfs_file_mmap,
  1336. .open = xfs_file_open,
  1337. .release = xfs_file_release,
  1338. .fsync = xfs_file_fsync,
  1339. .get_unmapped_area = thp_get_unmapped_area,
  1340. .fallocate = xfs_file_fallocate,
  1341. .clone_file_range = xfs_file_clone_range,
  1342. .dedupe_file_range = xfs_file_dedupe_range,
  1343. };
  1344. const struct file_operations xfs_dir_file_operations = {
  1345. .open = xfs_dir_open,
  1346. .read = generic_read_dir,
  1347. .iterate_shared = xfs_file_readdir,
  1348. .llseek = generic_file_llseek,
  1349. .unlocked_ioctl = xfs_file_ioctl,
  1350. #ifdef CONFIG_COMPAT
  1351. .compat_ioctl = xfs_file_compat_ioctl,
  1352. #endif
  1353. .fsync = xfs_dir_fsync,
  1354. };