xfs_file.c 36 KB

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