xfs_file.c 39 KB

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