xfs_file.c 36 KB

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