xfs_file.c 44 KB

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