xfs_file.c 42 KB

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