xfs_file.c 45 KB

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