xfs_file.c 40 KB

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