xfs_file.c 41 KB

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