xfs_file.c 41 KB

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