xfs_file.c 37 KB

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