xfs_file.c 36 KB

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