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_ioctl.h"
  35. #include "xfs_trace.h"
  36. #include <linux/dcache.h>
  37. #include <linux/falloc.h>
  38. #include <linux/pagevec.h>
  39. static const struct vm_operations_struct xfs_file_vm_ops;
  40. /*
  41. * Locking primitives for read and write IO paths to ensure we consistently use
  42. * and order the inode->i_mutex, ip->i_lock and ip->i_iolock.
  43. */
  44. static inline void
  45. xfs_rw_ilock(
  46. struct xfs_inode *ip,
  47. int type)
  48. {
  49. if (type & XFS_IOLOCK_EXCL)
  50. mutex_lock(&VFS_I(ip)->i_mutex);
  51. xfs_ilock(ip, type);
  52. }
  53. static inline void
  54. xfs_rw_iunlock(
  55. struct xfs_inode *ip,
  56. int type)
  57. {
  58. xfs_iunlock(ip, type);
  59. if (type & XFS_IOLOCK_EXCL)
  60. mutex_unlock(&VFS_I(ip)->i_mutex);
  61. }
  62. static inline void
  63. xfs_rw_ilock_demote(
  64. struct xfs_inode *ip,
  65. int type)
  66. {
  67. xfs_ilock_demote(ip, type);
  68. if (type & XFS_IOLOCK_EXCL)
  69. mutex_unlock(&VFS_I(ip)->i_mutex);
  70. }
  71. /*
  72. * xfs_iozero
  73. *
  74. * xfs_iozero clears the specified range of buffer supplied,
  75. * and marks all the affected blocks as valid and modified. If
  76. * an affected block is not allocated, it will be allocated. If
  77. * an affected block is not completely overwritten, and is not
  78. * valid before the operation, it will be read from disk before
  79. * being partially zeroed.
  80. */
  81. STATIC int
  82. xfs_iozero(
  83. struct xfs_inode *ip, /* inode */
  84. loff_t pos, /* offset in file */
  85. size_t count) /* size of data to zero */
  86. {
  87. struct page *page;
  88. struct address_space *mapping;
  89. int status;
  90. mapping = VFS_I(ip)->i_mapping;
  91. do {
  92. unsigned offset, bytes;
  93. void *fsdata;
  94. offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */
  95. bytes = PAGE_CACHE_SIZE - offset;
  96. if (bytes > count)
  97. bytes = count;
  98. status = pagecache_write_begin(NULL, mapping, pos, bytes,
  99. AOP_FLAG_UNINTERRUPTIBLE,
  100. &page, &fsdata);
  101. if (status)
  102. break;
  103. zero_user(page, offset, bytes);
  104. status = pagecache_write_end(NULL, mapping, pos, bytes, bytes,
  105. page, fsdata);
  106. WARN_ON(status <= 0); /* can't return less than zero! */
  107. pos += bytes;
  108. count -= bytes;
  109. status = 0;
  110. } while (count);
  111. return (-status);
  112. }
  113. /*
  114. * Fsync operations on directories are much simpler than on regular files,
  115. * as there is no file data to flush, and thus also no need for explicit
  116. * cache flush operations, and there are no non-transaction metadata updates
  117. * on directories either.
  118. */
  119. STATIC int
  120. xfs_dir_fsync(
  121. struct file *file,
  122. loff_t start,
  123. loff_t end,
  124. int datasync)
  125. {
  126. struct xfs_inode *ip = XFS_I(file->f_mapping->host);
  127. struct xfs_mount *mp = ip->i_mount;
  128. xfs_lsn_t lsn = 0;
  129. trace_xfs_dir_fsync(ip);
  130. xfs_ilock(ip, XFS_ILOCK_SHARED);
  131. if (xfs_ipincount(ip))
  132. lsn = ip->i_itemp->ili_last_lsn;
  133. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  134. if (!lsn)
  135. return 0;
  136. return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
  137. }
  138. STATIC int
  139. xfs_file_fsync(
  140. struct file *file,
  141. loff_t start,
  142. loff_t end,
  143. int datasync)
  144. {
  145. struct inode *inode = file->f_mapping->host;
  146. struct xfs_inode *ip = XFS_I(inode);
  147. struct xfs_mount *mp = ip->i_mount;
  148. int error = 0;
  149. int log_flushed = 0;
  150. xfs_lsn_t lsn = 0;
  151. trace_xfs_file_fsync(ip);
  152. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  153. if (error)
  154. return error;
  155. if (XFS_FORCED_SHUTDOWN(mp))
  156. return -XFS_ERROR(EIO);
  157. xfs_iflags_clear(ip, XFS_ITRUNCATED);
  158. if (mp->m_flags & XFS_MOUNT_BARRIER) {
  159. /*
  160. * If we have an RT and/or log subvolume we need to make sure
  161. * to flush the write cache the device used for file data
  162. * first. This is to ensure newly written file data make
  163. * it to disk before logging the new inode size in case of
  164. * an extending write.
  165. */
  166. if (XFS_IS_REALTIME_INODE(ip))
  167. xfs_blkdev_issue_flush(mp->m_rtdev_targp);
  168. else if (mp->m_logdev_targp != mp->m_ddev_targp)
  169. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  170. }
  171. /*
  172. * All metadata updates are logged, which means that we just have
  173. * to flush the log up to the latest LSN that touched the inode.
  174. */
  175. xfs_ilock(ip, XFS_ILOCK_SHARED);
  176. if (xfs_ipincount(ip)) {
  177. if (!datasync ||
  178. (ip->i_itemp->ili_fields & ~XFS_ILOG_TIMESTAMP))
  179. lsn = ip->i_itemp->ili_last_lsn;
  180. }
  181. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  182. if (lsn)
  183. error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed);
  184. /*
  185. * If we only have a single device, and the log force about was
  186. * a no-op we might have to flush the data device cache here.
  187. * This can only happen for fdatasync/O_DSYNC if we were overwriting
  188. * an already allocated file and thus do not have any metadata to
  189. * commit.
  190. */
  191. if ((mp->m_flags & XFS_MOUNT_BARRIER) &&
  192. mp->m_logdev_targp == mp->m_ddev_targp &&
  193. !XFS_IS_REALTIME_INODE(ip) &&
  194. !log_flushed)
  195. xfs_blkdev_issue_flush(mp->m_ddev_targp);
  196. return -error;
  197. }
  198. STATIC ssize_t
  199. xfs_file_aio_read(
  200. struct kiocb *iocb,
  201. const struct iovec *iovp,
  202. unsigned long nr_segs,
  203. loff_t pos)
  204. {
  205. struct file *file = iocb->ki_filp;
  206. struct inode *inode = file->f_mapping->host;
  207. struct xfs_inode *ip = XFS_I(inode);
  208. struct xfs_mount *mp = ip->i_mount;
  209. size_t size = 0;
  210. ssize_t ret = 0;
  211. int ioflags = 0;
  212. xfs_fsize_t n;
  213. XFS_STATS_INC(xs_read_calls);
  214. BUG_ON(iocb->ki_pos != pos);
  215. if (unlikely(file->f_flags & O_DIRECT))
  216. ioflags |= IO_ISDIRECT;
  217. if (file->f_mode & FMODE_NOCMTIME)
  218. ioflags |= IO_INVIS;
  219. ret = generic_segment_checks(iovp, &nr_segs, &size, VERIFY_WRITE);
  220. if (ret < 0)
  221. return ret;
  222. if (unlikely(ioflags & IO_ISDIRECT)) {
  223. xfs_buftarg_t *target =
  224. XFS_IS_REALTIME_INODE(ip) ?
  225. mp->m_rtdev_targp : mp->m_ddev_targp;
  226. if ((pos & target->bt_smask) || (size & target->bt_smask)) {
  227. if (pos == i_size_read(inode))
  228. return 0;
  229. return -XFS_ERROR(EINVAL);
  230. }
  231. }
  232. n = mp->m_super->s_maxbytes - pos;
  233. if (n <= 0 || size == 0)
  234. return 0;
  235. if (n < size)
  236. size = n;
  237. if (XFS_FORCED_SHUTDOWN(mp))
  238. return -EIO;
  239. /*
  240. * Locking is a bit tricky here. If we take an exclusive lock
  241. * for direct IO, we effectively serialise all new concurrent
  242. * read IO to this file and block it behind IO that is currently in
  243. * progress because IO in progress holds the IO lock shared. We only
  244. * need to hold the lock exclusive to blow away the page cache, so
  245. * only take lock exclusively if the page cache needs invalidation.
  246. * This allows the normal direct IO case of no page cache pages to
  247. * proceeed concurrently without serialisation.
  248. */
  249. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  250. if ((ioflags & IO_ISDIRECT) && inode->i_mapping->nrpages) {
  251. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  252. xfs_rw_ilock(ip, XFS_IOLOCK_EXCL);
  253. if (inode->i_mapping->nrpages) {
  254. ret = -filemap_write_and_wait_range(
  255. VFS_I(ip)->i_mapping,
  256. pos, -1);
  257. if (ret) {
  258. xfs_rw_iunlock(ip, XFS_IOLOCK_EXCL);
  259. return ret;
  260. }
  261. truncate_pagecache_range(VFS_I(ip), pos, -1);
  262. }
  263. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  264. }
  265. trace_xfs_file_read(ip, size, pos, ioflags);
  266. ret = generic_file_aio_read(iocb, iovp, nr_segs, pos);
  267. if (ret > 0)
  268. XFS_STATS_ADD(xs_read_bytes, ret);
  269. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  270. return ret;
  271. }
  272. STATIC ssize_t
  273. xfs_file_splice_read(
  274. struct file *infilp,
  275. loff_t *ppos,
  276. struct pipe_inode_info *pipe,
  277. size_t count,
  278. unsigned int flags)
  279. {
  280. struct xfs_inode *ip = XFS_I(infilp->f_mapping->host);
  281. int ioflags = 0;
  282. ssize_t ret;
  283. XFS_STATS_INC(xs_read_calls);
  284. if (infilp->f_mode & FMODE_NOCMTIME)
  285. ioflags |= IO_INVIS;
  286. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  287. return -EIO;
  288. xfs_rw_ilock(ip, XFS_IOLOCK_SHARED);
  289. trace_xfs_file_splice_read(ip, count, *ppos, ioflags);
  290. ret = generic_file_splice_read(infilp, ppos, pipe, count, flags);
  291. if (ret > 0)
  292. XFS_STATS_ADD(xs_read_bytes, ret);
  293. xfs_rw_iunlock(ip, XFS_IOLOCK_SHARED);
  294. return ret;
  295. }
  296. /*
  297. * xfs_file_splice_write() does not use xfs_rw_ilock() because
  298. * generic_file_splice_write() takes the i_mutex itself. This, in theory,
  299. * couuld cause lock inversions between the aio_write path and the splice path
  300. * if someone is doing concurrent splice(2) based writes and write(2) based
  301. * writes to the same inode. The only real way to fix this is to re-implement
  302. * the generic code here with correct locking orders.
  303. */
  304. STATIC ssize_t
  305. xfs_file_splice_write(
  306. struct pipe_inode_info *pipe,
  307. struct file *outfilp,
  308. loff_t *ppos,
  309. size_t count,
  310. unsigned int flags)
  311. {
  312. struct inode *inode = outfilp->f_mapping->host;
  313. struct xfs_inode *ip = XFS_I(inode);
  314. int ioflags = 0;
  315. ssize_t ret;
  316. XFS_STATS_INC(xs_write_calls);
  317. if (outfilp->f_mode & FMODE_NOCMTIME)
  318. ioflags |= IO_INVIS;
  319. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  320. return -EIO;
  321. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  322. trace_xfs_file_splice_write(ip, count, *ppos, ioflags);
  323. ret = generic_file_splice_write(pipe, outfilp, ppos, count, flags);
  324. if (ret > 0)
  325. XFS_STATS_ADD(xs_write_bytes, ret);
  326. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  327. return ret;
  328. }
  329. /*
  330. * This routine is called to handle zeroing any space in the last block of the
  331. * file that is beyond the EOF. We do this since the size is being increased
  332. * without writing anything to that block and we don't want to read the
  333. * garbage on the disk.
  334. */
  335. STATIC int /* error (positive) */
  336. xfs_zero_last_block(
  337. struct xfs_inode *ip,
  338. xfs_fsize_t offset,
  339. xfs_fsize_t isize)
  340. {
  341. struct xfs_mount *mp = ip->i_mount;
  342. xfs_fileoff_t last_fsb = XFS_B_TO_FSBT(mp, isize);
  343. int zero_offset = XFS_B_FSB_OFFSET(mp, isize);
  344. int zero_len;
  345. int nimaps = 1;
  346. int error = 0;
  347. struct xfs_bmbt_irec imap;
  348. xfs_ilock(ip, XFS_ILOCK_EXCL);
  349. error = xfs_bmapi_read(ip, last_fsb, 1, &imap, &nimaps, 0);
  350. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  351. if (error)
  352. return error;
  353. ASSERT(nimaps > 0);
  354. /*
  355. * If the block underlying isize is just a hole, then there
  356. * is nothing to zero.
  357. */
  358. if (imap.br_startblock == HOLESTARTBLOCK)
  359. return 0;
  360. zero_len = mp->m_sb.sb_blocksize - zero_offset;
  361. if (isize + zero_len > offset)
  362. zero_len = offset - isize;
  363. return xfs_iozero(ip, isize, zero_len);
  364. }
  365. /*
  366. * Zero any on disk space between the current EOF and the new, larger EOF.
  367. *
  368. * This handles the normal case of zeroing the remainder of the last block in
  369. * the file and the unusual case of zeroing blocks out beyond the size of the
  370. * file. This second case only happens with fixed size extents and when the
  371. * system crashes before the inode size was updated but after blocks were
  372. * allocated.
  373. *
  374. * Expects the iolock to be held exclusive, and will take the ilock internally.
  375. */
  376. int /* error (positive) */
  377. xfs_zero_eof(
  378. struct xfs_inode *ip,
  379. xfs_off_t offset, /* starting I/O offset */
  380. xfs_fsize_t isize) /* current inode size */
  381. {
  382. struct xfs_mount *mp = ip->i_mount;
  383. xfs_fileoff_t start_zero_fsb;
  384. xfs_fileoff_t end_zero_fsb;
  385. xfs_fileoff_t zero_count_fsb;
  386. xfs_fileoff_t last_fsb;
  387. xfs_fileoff_t zero_off;
  388. xfs_fsize_t zero_len;
  389. int nimaps;
  390. int error = 0;
  391. struct xfs_bmbt_irec imap;
  392. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  393. ASSERT(offset > isize);
  394. /*
  395. * First handle zeroing the block on which isize resides.
  396. *
  397. * We only zero a part of that block so it is handled specially.
  398. */
  399. if (XFS_B_FSB_OFFSET(mp, isize) != 0) {
  400. error = xfs_zero_last_block(ip, offset, isize);
  401. if (error)
  402. return error;
  403. }
  404. /*
  405. * Calculate the range between the new size and the old where blocks
  406. * needing to be zeroed may exist.
  407. *
  408. * To get the block where the last byte in the file currently resides,
  409. * we need to subtract one from the size and truncate back to a block
  410. * boundary. We subtract 1 in case the size is exactly on a block
  411. * boundary.
  412. */
  413. last_fsb = isize ? XFS_B_TO_FSBT(mp, isize - 1) : (xfs_fileoff_t)-1;
  414. start_zero_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  415. end_zero_fsb = XFS_B_TO_FSBT(mp, offset - 1);
  416. ASSERT((xfs_sfiloff_t)last_fsb < (xfs_sfiloff_t)start_zero_fsb);
  417. if (last_fsb == end_zero_fsb) {
  418. /*
  419. * The size was only incremented on its last block.
  420. * We took care of that above, so just return.
  421. */
  422. return 0;
  423. }
  424. ASSERT(start_zero_fsb <= end_zero_fsb);
  425. while (start_zero_fsb <= end_zero_fsb) {
  426. nimaps = 1;
  427. zero_count_fsb = end_zero_fsb - start_zero_fsb + 1;
  428. xfs_ilock(ip, XFS_ILOCK_EXCL);
  429. error = xfs_bmapi_read(ip, start_zero_fsb, zero_count_fsb,
  430. &imap, &nimaps, 0);
  431. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  432. if (error)
  433. return error;
  434. ASSERT(nimaps > 0);
  435. if (imap.br_state == XFS_EXT_UNWRITTEN ||
  436. imap.br_startblock == HOLESTARTBLOCK) {
  437. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  438. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  439. continue;
  440. }
  441. /*
  442. * There are blocks we need to zero.
  443. */
  444. zero_off = XFS_FSB_TO_B(mp, start_zero_fsb);
  445. zero_len = XFS_FSB_TO_B(mp, imap.br_blockcount);
  446. if ((zero_off + zero_len) > offset)
  447. zero_len = offset - zero_off;
  448. error = xfs_iozero(ip, zero_off, zero_len);
  449. if (error)
  450. return error;
  451. start_zero_fsb = imap.br_startoff + imap.br_blockcount;
  452. ASSERT(start_zero_fsb <= (end_zero_fsb + 1));
  453. }
  454. return 0;
  455. }
  456. /*
  457. * Common pre-write limit and setup checks.
  458. *
  459. * Called with the iolocked held either shared and exclusive according to
  460. * @iolock, and returns with it held. Might upgrade the iolock to exclusive
  461. * if called for a direct write beyond i_size.
  462. */
  463. STATIC ssize_t
  464. xfs_file_aio_write_checks(
  465. struct file *file,
  466. loff_t *pos,
  467. size_t *count,
  468. int *iolock)
  469. {
  470. struct inode *inode = file->f_mapping->host;
  471. struct xfs_inode *ip = XFS_I(inode);
  472. int error = 0;
  473. restart:
  474. error = generic_write_checks(file, pos, count, S_ISBLK(inode->i_mode));
  475. if (error)
  476. return error;
  477. /*
  478. * If the offset is beyond the size of the file, we need to zero any
  479. * blocks that fall between the existing EOF and the start of this
  480. * write. If zeroing is needed and we are currently holding the
  481. * iolock shared, we need to update it to exclusive which implies
  482. * having to redo all checks before.
  483. */
  484. if (*pos > i_size_read(inode)) {
  485. if (*iolock == XFS_IOLOCK_SHARED) {
  486. xfs_rw_iunlock(ip, *iolock);
  487. *iolock = XFS_IOLOCK_EXCL;
  488. xfs_rw_ilock(ip, *iolock);
  489. goto restart;
  490. }
  491. error = -xfs_zero_eof(ip, *pos, i_size_read(inode));
  492. if (error)
  493. return error;
  494. }
  495. /*
  496. * Updating the timestamps will grab the ilock again from
  497. * xfs_fs_dirty_inode, so we have to call it after dropping the
  498. * lock above. Eventually we should look into a way to avoid
  499. * the pointless lock roundtrip.
  500. */
  501. if (likely(!(file->f_mode & FMODE_NOCMTIME))) {
  502. error = file_update_time(file);
  503. if (error)
  504. return error;
  505. }
  506. /*
  507. * If we're writing the file then make sure to clear the setuid and
  508. * setgid bits if the process is not being run by root. This keeps
  509. * people from modifying setuid and setgid binaries.
  510. */
  511. return file_remove_suid(file);
  512. }
  513. /*
  514. * xfs_file_dio_aio_write - handle direct IO writes
  515. *
  516. * Lock the inode appropriately to prepare for and issue a direct IO write.
  517. * By separating it from the buffered write path we remove all the tricky to
  518. * follow locking changes and looping.
  519. *
  520. * If there are cached pages or we're extending the file, we need IOLOCK_EXCL
  521. * until we're sure the bytes at the new EOF have been zeroed and/or the cached
  522. * pages are flushed out.
  523. *
  524. * In most cases the direct IO writes will be done holding IOLOCK_SHARED
  525. * allowing them to be done in parallel with reads and other direct IO writes.
  526. * However, if the IO is not aligned to filesystem blocks, the direct IO layer
  527. * needs to do sub-block zeroing and that requires serialisation against other
  528. * direct IOs to the same block. In this case we need to serialise the
  529. * submission of the unaligned IOs so that we don't get racing block zeroing in
  530. * the dio layer. To avoid the problem with aio, we also need to wait for
  531. * outstanding IOs to complete so that unwritten extent conversion is completed
  532. * before we try to map the overlapping block. This is currently implemented by
  533. * hitting it with a big hammer (i.e. inode_dio_wait()).
  534. *
  535. * Returns with locks held indicated by @iolock and errors indicated by
  536. * negative return values.
  537. */
  538. STATIC ssize_t
  539. xfs_file_dio_aio_write(
  540. struct kiocb *iocb,
  541. const struct iovec *iovp,
  542. unsigned long nr_segs,
  543. loff_t pos,
  544. size_t ocount)
  545. {
  546. struct file *file = iocb->ki_filp;
  547. struct address_space *mapping = file->f_mapping;
  548. struct inode *inode = mapping->host;
  549. struct xfs_inode *ip = XFS_I(inode);
  550. struct xfs_mount *mp = ip->i_mount;
  551. ssize_t ret = 0;
  552. size_t count = ocount;
  553. int unaligned_io = 0;
  554. int iolock;
  555. struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ?
  556. mp->m_rtdev_targp : mp->m_ddev_targp;
  557. if ((pos & target->bt_smask) || (count & target->bt_smask))
  558. return -XFS_ERROR(EINVAL);
  559. if ((pos & mp->m_blockmask) || ((pos + count) & mp->m_blockmask))
  560. unaligned_io = 1;
  561. /*
  562. * We don't need to take an exclusive lock unless there page cache needs
  563. * to be invalidated or unaligned IO is being executed. We don't need to
  564. * consider the EOF extension case here because
  565. * xfs_file_aio_write_checks() will relock the inode as necessary for
  566. * EOF zeroing cases and fill out the new inode size as appropriate.
  567. */
  568. if (unaligned_io || mapping->nrpages)
  569. iolock = XFS_IOLOCK_EXCL;
  570. else
  571. iolock = XFS_IOLOCK_SHARED;
  572. xfs_rw_ilock(ip, iolock);
  573. /*
  574. * Recheck if there are cached pages that need invalidate after we got
  575. * the iolock to protect against other threads adding new pages while
  576. * we were waiting for the iolock.
  577. */
  578. if (mapping->nrpages && iolock == XFS_IOLOCK_SHARED) {
  579. xfs_rw_iunlock(ip, iolock);
  580. iolock = XFS_IOLOCK_EXCL;
  581. xfs_rw_ilock(ip, iolock);
  582. }
  583. ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
  584. if (ret)
  585. goto out;
  586. if (mapping->nrpages) {
  587. ret = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  588. pos, -1);
  589. if (ret)
  590. goto out;
  591. truncate_pagecache_range(VFS_I(ip), pos, -1);
  592. }
  593. /*
  594. * If we are doing unaligned IO, wait for all other IO to drain,
  595. * otherwise demote the lock if we had to flush cached pages
  596. */
  597. if (unaligned_io)
  598. inode_dio_wait(inode);
  599. else if (iolock == XFS_IOLOCK_EXCL) {
  600. xfs_rw_ilock_demote(ip, XFS_IOLOCK_EXCL);
  601. iolock = XFS_IOLOCK_SHARED;
  602. }
  603. trace_xfs_file_direct_write(ip, count, iocb->ki_pos, 0);
  604. ret = generic_file_direct_write(iocb, iovp,
  605. &nr_segs, pos, &iocb->ki_pos, count, ocount);
  606. out:
  607. xfs_rw_iunlock(ip, iolock);
  608. /* No fallback to buffered IO on errors for XFS. */
  609. ASSERT(ret < 0 || ret == count);
  610. return ret;
  611. }
  612. STATIC ssize_t
  613. xfs_file_buffered_aio_write(
  614. struct kiocb *iocb,
  615. const struct iovec *iovp,
  616. unsigned long nr_segs,
  617. loff_t pos,
  618. size_t ocount)
  619. {
  620. struct file *file = iocb->ki_filp;
  621. struct address_space *mapping = file->f_mapping;
  622. struct inode *inode = mapping->host;
  623. struct xfs_inode *ip = XFS_I(inode);
  624. ssize_t ret;
  625. int enospc = 0;
  626. int iolock = XFS_IOLOCK_EXCL;
  627. size_t count = ocount;
  628. xfs_rw_ilock(ip, iolock);
  629. ret = xfs_file_aio_write_checks(file, &pos, &count, &iolock);
  630. if (ret)
  631. goto out;
  632. /* We can write back this queue in page reclaim */
  633. current->backing_dev_info = mapping->backing_dev_info;
  634. write_retry:
  635. trace_xfs_file_buffered_write(ip, count, iocb->ki_pos, 0);
  636. ret = generic_file_buffered_write(iocb, iovp, nr_segs,
  637. pos, &iocb->ki_pos, count, 0);
  638. /*
  639. * If we just got an ENOSPC, try to write back all dirty inodes to
  640. * convert delalloc space to free up some of the excess reserved
  641. * metadata space.
  642. */
  643. if (ret == -ENOSPC && !enospc) {
  644. enospc = 1;
  645. xfs_flush_inodes(ip->i_mount);
  646. goto write_retry;
  647. }
  648. current->backing_dev_info = NULL;
  649. out:
  650. xfs_rw_iunlock(ip, iolock);
  651. return ret;
  652. }
  653. STATIC ssize_t
  654. xfs_file_aio_write(
  655. struct kiocb *iocb,
  656. const struct iovec *iovp,
  657. unsigned long nr_segs,
  658. loff_t pos)
  659. {
  660. struct file *file = iocb->ki_filp;
  661. struct address_space *mapping = file->f_mapping;
  662. struct inode *inode = mapping->host;
  663. struct xfs_inode *ip = XFS_I(inode);
  664. ssize_t ret;
  665. size_t ocount = 0;
  666. XFS_STATS_INC(xs_write_calls);
  667. BUG_ON(iocb->ki_pos != pos);
  668. ret = generic_segment_checks(iovp, &nr_segs, &ocount, VERIFY_READ);
  669. if (ret)
  670. return ret;
  671. if (ocount == 0)
  672. return 0;
  673. sb_start_write(inode->i_sb);
  674. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  675. ret = -EIO;
  676. goto out;
  677. }
  678. if (unlikely(file->f_flags & O_DIRECT))
  679. ret = xfs_file_dio_aio_write(iocb, iovp, nr_segs, pos, ocount);
  680. else
  681. ret = xfs_file_buffered_aio_write(iocb, iovp, nr_segs, pos,
  682. ocount);
  683. if (ret > 0) {
  684. ssize_t err;
  685. XFS_STATS_ADD(xs_write_bytes, ret);
  686. /* Handle various SYNC-type writes */
  687. err = generic_write_sync(file, pos, ret);
  688. if (err < 0)
  689. ret = err;
  690. }
  691. out:
  692. sb_end_write(inode->i_sb);
  693. return ret;
  694. }
  695. STATIC long
  696. xfs_file_fallocate(
  697. struct file *file,
  698. int mode,
  699. loff_t offset,
  700. loff_t len)
  701. {
  702. struct inode *inode = file->f_path.dentry->d_inode;
  703. long error;
  704. loff_t new_size = 0;
  705. xfs_flock64_t bf;
  706. xfs_inode_t *ip = XFS_I(inode);
  707. int cmd = XFS_IOC_RESVSP;
  708. int attr_flags = XFS_ATTR_NOLOCK;
  709. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  710. return -EOPNOTSUPP;
  711. bf.l_whence = 0;
  712. bf.l_start = offset;
  713. bf.l_len = len;
  714. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  715. if (mode & FALLOC_FL_PUNCH_HOLE)
  716. cmd = XFS_IOC_UNRESVSP;
  717. /* check the new inode size is valid before allocating */
  718. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  719. offset + len > i_size_read(inode)) {
  720. new_size = offset + len;
  721. error = inode_newsize_ok(inode, new_size);
  722. if (error)
  723. goto out_unlock;
  724. }
  725. if (file->f_flags & O_DSYNC)
  726. attr_flags |= XFS_ATTR_SYNC;
  727. error = -xfs_change_file_space(ip, cmd, &bf, 0, attr_flags);
  728. if (error)
  729. goto out_unlock;
  730. /* Change file size if needed */
  731. if (new_size) {
  732. struct iattr iattr;
  733. iattr.ia_valid = ATTR_SIZE;
  734. iattr.ia_size = new_size;
  735. error = -xfs_setattr_size(ip, &iattr, XFS_ATTR_NOLOCK);
  736. }
  737. out_unlock:
  738. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  739. return error;
  740. }
  741. STATIC int
  742. xfs_file_open(
  743. struct inode *inode,
  744. struct file *file)
  745. {
  746. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  747. return -EFBIG;
  748. if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb)))
  749. return -EIO;
  750. return 0;
  751. }
  752. STATIC int
  753. xfs_dir_open(
  754. struct inode *inode,
  755. struct file *file)
  756. {
  757. struct xfs_inode *ip = XFS_I(inode);
  758. int mode;
  759. int error;
  760. error = xfs_file_open(inode, file);
  761. if (error)
  762. return error;
  763. /*
  764. * If there are any blocks, read-ahead block 0 as we're almost
  765. * certain to have the next operation be a read there.
  766. */
  767. mode = xfs_ilock_map_shared(ip);
  768. if (ip->i_d.di_nextents > 0)
  769. xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK, NULL);
  770. xfs_iunlock(ip, mode);
  771. return 0;
  772. }
  773. STATIC int
  774. xfs_file_release(
  775. struct inode *inode,
  776. struct file *filp)
  777. {
  778. return -xfs_release(XFS_I(inode));
  779. }
  780. STATIC int
  781. xfs_file_readdir(
  782. struct file *filp,
  783. void *dirent,
  784. filldir_t filldir)
  785. {
  786. struct inode *inode = filp->f_path.dentry->d_inode;
  787. xfs_inode_t *ip = XFS_I(inode);
  788. int error;
  789. size_t bufsize;
  790. /*
  791. * The Linux API doesn't pass down the total size of the buffer
  792. * we read into down to the filesystem. With the filldir concept
  793. * it's not needed for correct information, but the XFS dir2 leaf
  794. * code wants an estimate of the buffer size to calculate it's
  795. * readahead window and size the buffers used for mapping to
  796. * physical blocks.
  797. *
  798. * Try to give it an estimate that's good enough, maybe at some
  799. * point we can change the ->readdir prototype to include the
  800. * buffer size. For now we use the current glibc buffer size.
  801. */
  802. bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size);
  803. error = xfs_readdir(ip, dirent, bufsize,
  804. (xfs_off_t *)&filp->f_pos, filldir);
  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. if (offset != file->f_pos)
  1109. file->f_pos = offset;
  1110. out_unlock:
  1111. xfs_iunlock_map_shared(ip, lock);
  1112. if (error)
  1113. return -error;
  1114. return offset;
  1115. }
  1116. STATIC loff_t
  1117. xfs_seek_hole(
  1118. struct file *file,
  1119. loff_t start)
  1120. {
  1121. struct inode *inode = file->f_mapping->host;
  1122. struct xfs_inode *ip = XFS_I(inode);
  1123. struct xfs_mount *mp = ip->i_mount;
  1124. loff_t uninitialized_var(offset);
  1125. xfs_fsize_t isize;
  1126. xfs_fileoff_t fsbno;
  1127. xfs_filblks_t end;
  1128. uint lock;
  1129. int error;
  1130. if (XFS_FORCED_SHUTDOWN(mp))
  1131. return -XFS_ERROR(EIO);
  1132. lock = xfs_ilock_map_shared(ip);
  1133. isize = i_size_read(inode);
  1134. if (start >= isize) {
  1135. error = ENXIO;
  1136. goto out_unlock;
  1137. }
  1138. fsbno = XFS_B_TO_FSBT(mp, start);
  1139. end = XFS_B_TO_FSB(mp, isize);
  1140. for (;;) {
  1141. struct xfs_bmbt_irec map[2];
  1142. int nmap = 2;
  1143. unsigned int i;
  1144. error = xfs_bmapi_read(ip, fsbno, end - fsbno, map, &nmap,
  1145. XFS_BMAPI_ENTIRE);
  1146. if (error)
  1147. goto out_unlock;
  1148. /* No extents at given offset, must be beyond EOF */
  1149. if (nmap == 0) {
  1150. error = ENXIO;
  1151. goto out_unlock;
  1152. }
  1153. for (i = 0; i < nmap; i++) {
  1154. offset = max_t(loff_t, start,
  1155. XFS_FSB_TO_B(mp, map[i].br_startoff));
  1156. /* Landed in a hole */
  1157. if (map[i].br_startblock == HOLESTARTBLOCK)
  1158. goto out;
  1159. /*
  1160. * Landed in an unwritten extent, try to search hole
  1161. * from page cache.
  1162. */
  1163. if (map[i].br_state == XFS_EXT_UNWRITTEN) {
  1164. if (xfs_find_get_desired_pgoff(inode, &map[i],
  1165. HOLE_OFF, &offset))
  1166. goto out;
  1167. }
  1168. }
  1169. /*
  1170. * map[0] contains data or its unwritten but contains
  1171. * data in page cache, probably means that we are
  1172. * reading after EOF. We should fix offset to point
  1173. * to the end of the file(i.e., there is an implicit
  1174. * hole at the end of any file).
  1175. */
  1176. if (nmap == 1) {
  1177. offset = isize;
  1178. break;
  1179. }
  1180. ASSERT(i > 1);
  1181. /*
  1182. * Both mappings contains data, proceed to the next round of
  1183. * search if the current reading offset not beyond or hit EOF.
  1184. */
  1185. fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount;
  1186. start = XFS_FSB_TO_B(mp, fsbno);
  1187. if (start >= isize) {
  1188. offset = isize;
  1189. break;
  1190. }
  1191. }
  1192. out:
  1193. /*
  1194. * At this point, we must have found a hole. However, the returned
  1195. * offset may be bigger than the file size as it may be aligned to
  1196. * page boundary for unwritten extents, we need to deal with this
  1197. * situation in particular.
  1198. */
  1199. offset = min_t(loff_t, offset, isize);
  1200. if (offset != file->f_pos)
  1201. file->f_pos = offset;
  1202. out_unlock:
  1203. xfs_iunlock_map_shared(ip, lock);
  1204. if (error)
  1205. return -error;
  1206. return offset;
  1207. }
  1208. STATIC loff_t
  1209. xfs_file_llseek(
  1210. struct file *file,
  1211. loff_t offset,
  1212. int origin)
  1213. {
  1214. switch (origin) {
  1215. case SEEK_END:
  1216. case SEEK_CUR:
  1217. case SEEK_SET:
  1218. return generic_file_llseek(file, offset, origin);
  1219. case SEEK_DATA:
  1220. return xfs_seek_data(file, offset);
  1221. case SEEK_HOLE:
  1222. return xfs_seek_hole(file, offset);
  1223. default:
  1224. return -EINVAL;
  1225. }
  1226. }
  1227. const struct file_operations xfs_file_operations = {
  1228. .llseek = xfs_file_llseek,
  1229. .read = do_sync_read,
  1230. .write = do_sync_write,
  1231. .aio_read = xfs_file_aio_read,
  1232. .aio_write = xfs_file_aio_write,
  1233. .splice_read = xfs_file_splice_read,
  1234. .splice_write = xfs_file_splice_write,
  1235. .unlocked_ioctl = xfs_file_ioctl,
  1236. #ifdef CONFIG_COMPAT
  1237. .compat_ioctl = xfs_file_compat_ioctl,
  1238. #endif
  1239. .mmap = xfs_file_mmap,
  1240. .open = xfs_file_open,
  1241. .release = xfs_file_release,
  1242. .fsync = xfs_file_fsync,
  1243. .fallocate = xfs_file_fallocate,
  1244. };
  1245. const struct file_operations xfs_dir_file_operations = {
  1246. .open = xfs_dir_open,
  1247. .read = generic_read_dir,
  1248. .readdir = xfs_file_readdir,
  1249. .llseek = generic_file_llseek,
  1250. .unlocked_ioctl = xfs_file_ioctl,
  1251. #ifdef CONFIG_COMPAT
  1252. .compat_ioctl = xfs_file_compat_ioctl,
  1253. #endif
  1254. .fsync = xfs_dir_fsync,
  1255. };
  1256. static const struct vm_operations_struct xfs_file_vm_ops = {
  1257. .fault = filemap_fault,
  1258. .page_mkwrite = xfs_vm_page_mkwrite,
  1259. .remap_pages = generic_file_remap_pages,
  1260. };