xfs_file.c 37 KB

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