xfs_file.c 45 KB

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