xfs_file.c 44 KB

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