xfs_file.c 36 KB

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