xfs_file.c 42 KB

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