xfs_file.c 37 KB

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