xfs_file.c 44 KB

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