xfs_file.c 36 KB

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