xfs_file.c 36 KB

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