xfs_aops.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  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_shared.h"
  20. #include "xfs_format.h"
  21. #include "xfs_log_format.h"
  22. #include "xfs_trans_resv.h"
  23. #include "xfs_mount.h"
  24. #include "xfs_inode.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_inode_item.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_error.h"
  29. #include "xfs_iomap.h"
  30. #include "xfs_trace.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_bmap_util.h"
  33. #include "xfs_bmap_btree.h"
  34. #include <linux/gfp.h>
  35. #include <linux/mpage.h>
  36. #include <linux/pagevec.h>
  37. #include <linux/writeback.h>
  38. void
  39. xfs_count_page_state(
  40. struct page *page,
  41. int *delalloc,
  42. int *unwritten)
  43. {
  44. struct buffer_head *bh, *head;
  45. *delalloc = *unwritten = 0;
  46. bh = head = page_buffers(page);
  47. do {
  48. if (buffer_unwritten(bh))
  49. (*unwritten) = 1;
  50. else if (buffer_delay(bh))
  51. (*delalloc) = 1;
  52. } while ((bh = bh->b_this_page) != head);
  53. }
  54. STATIC struct block_device *
  55. xfs_find_bdev_for_inode(
  56. struct inode *inode)
  57. {
  58. struct xfs_inode *ip = XFS_I(inode);
  59. struct xfs_mount *mp = ip->i_mount;
  60. if (XFS_IS_REALTIME_INODE(ip))
  61. return mp->m_rtdev_targp->bt_bdev;
  62. else
  63. return mp->m_ddev_targp->bt_bdev;
  64. }
  65. /*
  66. * We're now finished for good with this ioend structure.
  67. * Update the page state via the associated buffer_heads,
  68. * release holds on the inode and bio, and finally free
  69. * up memory. Do not use the ioend after this.
  70. */
  71. STATIC void
  72. xfs_destroy_ioend(
  73. xfs_ioend_t *ioend)
  74. {
  75. struct buffer_head *bh, *next;
  76. for (bh = ioend->io_buffer_head; bh; bh = next) {
  77. next = bh->b_private;
  78. bh->b_end_io(bh, !ioend->io_error);
  79. }
  80. mempool_free(ioend, xfs_ioend_pool);
  81. }
  82. /*
  83. * Fast and loose check if this write could update the on-disk inode size.
  84. */
  85. static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
  86. {
  87. return ioend->io_offset + ioend->io_size >
  88. XFS_I(ioend->io_inode)->i_d.di_size;
  89. }
  90. STATIC int
  91. xfs_setfilesize_trans_alloc(
  92. struct xfs_ioend *ioend)
  93. {
  94. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  95. struct xfs_trans *tp;
  96. int error;
  97. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  98. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
  99. if (error) {
  100. xfs_trans_cancel(tp);
  101. return error;
  102. }
  103. ioend->io_append_trans = tp;
  104. /*
  105. * We may pass freeze protection with a transaction. So tell lockdep
  106. * we released it.
  107. */
  108. __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
  109. /*
  110. * We hand off the transaction to the completion thread now, so
  111. * clear the flag here.
  112. */
  113. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  114. return 0;
  115. }
  116. /*
  117. * Update on-disk file size now that data has been written to disk.
  118. */
  119. STATIC int
  120. xfs_setfilesize(
  121. struct xfs_inode *ip,
  122. struct xfs_trans *tp,
  123. xfs_off_t offset,
  124. size_t size)
  125. {
  126. xfs_fsize_t isize;
  127. xfs_ilock(ip, XFS_ILOCK_EXCL);
  128. isize = xfs_new_eof(ip, offset + size);
  129. if (!isize) {
  130. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  131. xfs_trans_cancel(tp);
  132. return 0;
  133. }
  134. trace_xfs_setfilesize(ip, offset, size);
  135. ip->i_d.di_size = isize;
  136. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  137. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  138. return xfs_trans_commit(tp);
  139. }
  140. STATIC int
  141. xfs_setfilesize_ioend(
  142. struct xfs_ioend *ioend)
  143. {
  144. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  145. struct xfs_trans *tp = ioend->io_append_trans;
  146. /*
  147. * The transaction may have been allocated in the I/O submission thread,
  148. * thus we need to mark ourselves as being in a transaction manually.
  149. * Similarly for freeze protection.
  150. */
  151. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  152. __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
  153. return xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
  154. }
  155. /*
  156. * Schedule IO completion handling on the final put of an ioend.
  157. *
  158. * If there is no work to do we might as well call it a day and free the
  159. * ioend right now.
  160. */
  161. STATIC void
  162. xfs_finish_ioend(
  163. struct xfs_ioend *ioend)
  164. {
  165. if (atomic_dec_and_test(&ioend->io_remaining)) {
  166. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  167. if (ioend->io_type == XFS_IO_UNWRITTEN)
  168. queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
  169. else if (ioend->io_append_trans)
  170. queue_work(mp->m_data_workqueue, &ioend->io_work);
  171. else
  172. xfs_destroy_ioend(ioend);
  173. }
  174. }
  175. /*
  176. * IO write completion.
  177. */
  178. STATIC void
  179. xfs_end_io(
  180. struct work_struct *work)
  181. {
  182. xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
  183. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  184. int error = 0;
  185. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  186. ioend->io_error = -EIO;
  187. goto done;
  188. }
  189. if (ioend->io_error)
  190. goto done;
  191. /*
  192. * For unwritten extents we need to issue transactions to convert a
  193. * range to normal written extens after the data I/O has finished.
  194. */
  195. if (ioend->io_type == XFS_IO_UNWRITTEN) {
  196. error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
  197. ioend->io_size);
  198. } else if (ioend->io_append_trans) {
  199. error = xfs_setfilesize_ioend(ioend);
  200. } else {
  201. ASSERT(!xfs_ioend_is_append(ioend));
  202. }
  203. done:
  204. if (error)
  205. ioend->io_error = error;
  206. xfs_destroy_ioend(ioend);
  207. }
  208. /*
  209. * Allocate and initialise an IO completion structure.
  210. * We need to track unwritten extent write completion here initially.
  211. * We'll need to extend this for updating the ondisk inode size later
  212. * (vs. incore size).
  213. */
  214. STATIC xfs_ioend_t *
  215. xfs_alloc_ioend(
  216. struct inode *inode,
  217. unsigned int type)
  218. {
  219. xfs_ioend_t *ioend;
  220. ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
  221. /*
  222. * Set the count to 1 initially, which will prevent an I/O
  223. * completion callback from happening before we have started
  224. * all the I/O from calling the completion routine too early.
  225. */
  226. atomic_set(&ioend->io_remaining, 1);
  227. ioend->io_error = 0;
  228. ioend->io_list = NULL;
  229. ioend->io_type = type;
  230. ioend->io_inode = inode;
  231. ioend->io_buffer_head = NULL;
  232. ioend->io_buffer_tail = NULL;
  233. ioend->io_offset = 0;
  234. ioend->io_size = 0;
  235. ioend->io_append_trans = NULL;
  236. INIT_WORK(&ioend->io_work, xfs_end_io);
  237. return ioend;
  238. }
  239. STATIC int
  240. xfs_map_blocks(
  241. struct inode *inode,
  242. loff_t offset,
  243. struct xfs_bmbt_irec *imap,
  244. int type,
  245. int nonblocking)
  246. {
  247. struct xfs_inode *ip = XFS_I(inode);
  248. struct xfs_mount *mp = ip->i_mount;
  249. ssize_t count = 1 << inode->i_blkbits;
  250. xfs_fileoff_t offset_fsb, end_fsb;
  251. int error = 0;
  252. int bmapi_flags = XFS_BMAPI_ENTIRE;
  253. int nimaps = 1;
  254. if (XFS_FORCED_SHUTDOWN(mp))
  255. return -EIO;
  256. if (type == XFS_IO_UNWRITTEN)
  257. bmapi_flags |= XFS_BMAPI_IGSTATE;
  258. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  259. if (nonblocking)
  260. return -EAGAIN;
  261. xfs_ilock(ip, XFS_ILOCK_SHARED);
  262. }
  263. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  264. (ip->i_df.if_flags & XFS_IFEXTENTS));
  265. ASSERT(offset <= mp->m_super->s_maxbytes);
  266. if (offset + count > mp->m_super->s_maxbytes)
  267. count = mp->m_super->s_maxbytes - offset;
  268. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  269. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  270. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  271. imap, &nimaps, bmapi_flags);
  272. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  273. if (error)
  274. return error;
  275. if (type == XFS_IO_DELALLOC &&
  276. (!nimaps || isnullstartblock(imap->br_startblock))) {
  277. error = xfs_iomap_write_allocate(ip, offset, imap);
  278. if (!error)
  279. trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
  280. return error;
  281. }
  282. #ifdef DEBUG
  283. if (type == XFS_IO_UNWRITTEN) {
  284. ASSERT(nimaps);
  285. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  286. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  287. }
  288. #endif
  289. if (nimaps)
  290. trace_xfs_map_blocks_found(ip, offset, count, type, imap);
  291. return 0;
  292. }
  293. STATIC int
  294. xfs_imap_valid(
  295. struct inode *inode,
  296. struct xfs_bmbt_irec *imap,
  297. xfs_off_t offset)
  298. {
  299. offset >>= inode->i_blkbits;
  300. return offset >= imap->br_startoff &&
  301. offset < imap->br_startoff + imap->br_blockcount;
  302. }
  303. /*
  304. * BIO completion handler for buffered IO.
  305. */
  306. STATIC void
  307. xfs_end_bio(
  308. struct bio *bio,
  309. int error)
  310. {
  311. xfs_ioend_t *ioend = bio->bi_private;
  312. ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
  313. /* Toss bio and pass work off to an xfsdatad thread */
  314. bio->bi_private = NULL;
  315. bio->bi_end_io = NULL;
  316. bio_put(bio);
  317. xfs_finish_ioend(ioend);
  318. }
  319. STATIC void
  320. xfs_submit_ioend_bio(
  321. struct writeback_control *wbc,
  322. xfs_ioend_t *ioend,
  323. struct bio *bio)
  324. {
  325. atomic_inc(&ioend->io_remaining);
  326. bio->bi_private = ioend;
  327. bio->bi_end_io = xfs_end_bio;
  328. submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
  329. }
  330. STATIC struct bio *
  331. xfs_alloc_ioend_bio(
  332. struct buffer_head *bh)
  333. {
  334. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  335. struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
  336. ASSERT(bio->bi_private == NULL);
  337. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  338. bio->bi_bdev = bh->b_bdev;
  339. return bio;
  340. }
  341. STATIC void
  342. xfs_start_buffer_writeback(
  343. struct buffer_head *bh)
  344. {
  345. ASSERT(buffer_mapped(bh));
  346. ASSERT(buffer_locked(bh));
  347. ASSERT(!buffer_delay(bh));
  348. ASSERT(!buffer_unwritten(bh));
  349. mark_buffer_async_write(bh);
  350. set_buffer_uptodate(bh);
  351. clear_buffer_dirty(bh);
  352. }
  353. STATIC void
  354. xfs_start_page_writeback(
  355. struct page *page,
  356. int clear_dirty,
  357. int buffers)
  358. {
  359. ASSERT(PageLocked(page));
  360. ASSERT(!PageWriteback(page));
  361. /*
  362. * if the page was not fully cleaned, we need to ensure that the higher
  363. * layers come back to it correctly. That means we need to keep the page
  364. * dirty, and for WB_SYNC_ALL writeback we need to ensure the
  365. * PAGECACHE_TAG_TOWRITE index mark is not removed so another attempt to
  366. * write this page in this writeback sweep will be made.
  367. */
  368. if (clear_dirty) {
  369. clear_page_dirty_for_io(page);
  370. set_page_writeback(page);
  371. } else
  372. set_page_writeback_keepwrite(page);
  373. unlock_page(page);
  374. /* If no buffers on the page are to be written, finish it here */
  375. if (!buffers)
  376. end_page_writeback(page);
  377. }
  378. static inline int xfs_bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  379. {
  380. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  381. }
  382. /*
  383. * Submit all of the bios for all of the ioends we have saved up, covering the
  384. * initial writepage page and also any probed pages.
  385. *
  386. * Because we may have multiple ioends spanning a page, we need to start
  387. * writeback on all the buffers before we submit them for I/O. If we mark the
  388. * buffers as we got, then we can end up with a page that only has buffers
  389. * marked async write and I/O complete on can occur before we mark the other
  390. * buffers async write.
  391. *
  392. * The end result of this is that we trip a bug in end_page_writeback() because
  393. * we call it twice for the one page as the code in end_buffer_async_write()
  394. * assumes that all buffers on the page are started at the same time.
  395. *
  396. * The fix is two passes across the ioend list - one to start writeback on the
  397. * buffer_heads, and then submit them for I/O on the second pass.
  398. *
  399. * If @fail is non-zero, it means that we have a situation where some part of
  400. * the submission process has failed after we have marked paged for writeback
  401. * and unlocked them. In this situation, we need to fail the ioend chain rather
  402. * than submit it to IO. This typically only happens on a filesystem shutdown.
  403. */
  404. STATIC void
  405. xfs_submit_ioend(
  406. struct writeback_control *wbc,
  407. xfs_ioend_t *ioend,
  408. int fail)
  409. {
  410. xfs_ioend_t *head = ioend;
  411. xfs_ioend_t *next;
  412. struct buffer_head *bh;
  413. struct bio *bio;
  414. sector_t lastblock = 0;
  415. /* Pass 1 - start writeback */
  416. do {
  417. next = ioend->io_list;
  418. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
  419. xfs_start_buffer_writeback(bh);
  420. } while ((ioend = next) != NULL);
  421. /* Pass 2 - submit I/O */
  422. ioend = head;
  423. do {
  424. next = ioend->io_list;
  425. bio = NULL;
  426. /*
  427. * If we are failing the IO now, just mark the ioend with an
  428. * error and finish it. This will run IO completion immediately
  429. * as there is only one reference to the ioend at this point in
  430. * time.
  431. */
  432. if (fail) {
  433. ioend->io_error = fail;
  434. xfs_finish_ioend(ioend);
  435. continue;
  436. }
  437. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  438. if (!bio) {
  439. retry:
  440. bio = xfs_alloc_ioend_bio(bh);
  441. } else if (bh->b_blocknr != lastblock + 1) {
  442. xfs_submit_ioend_bio(wbc, ioend, bio);
  443. goto retry;
  444. }
  445. if (xfs_bio_add_buffer(bio, bh) != bh->b_size) {
  446. xfs_submit_ioend_bio(wbc, ioend, bio);
  447. goto retry;
  448. }
  449. lastblock = bh->b_blocknr;
  450. }
  451. if (bio)
  452. xfs_submit_ioend_bio(wbc, ioend, bio);
  453. xfs_finish_ioend(ioend);
  454. } while ((ioend = next) != NULL);
  455. }
  456. /*
  457. * Cancel submission of all buffer_heads so far in this endio.
  458. * Toss the endio too. Only ever called for the initial page
  459. * in a writepage request, so only ever one page.
  460. */
  461. STATIC void
  462. xfs_cancel_ioend(
  463. xfs_ioend_t *ioend)
  464. {
  465. xfs_ioend_t *next;
  466. struct buffer_head *bh, *next_bh;
  467. do {
  468. next = ioend->io_list;
  469. bh = ioend->io_buffer_head;
  470. do {
  471. next_bh = bh->b_private;
  472. clear_buffer_async_write(bh);
  473. /*
  474. * The unwritten flag is cleared when added to the
  475. * ioend. We're not submitting for I/O so mark the
  476. * buffer unwritten again for next time around.
  477. */
  478. if (ioend->io_type == XFS_IO_UNWRITTEN)
  479. set_buffer_unwritten(bh);
  480. unlock_buffer(bh);
  481. } while ((bh = next_bh) != NULL);
  482. mempool_free(ioend, xfs_ioend_pool);
  483. } while ((ioend = next) != NULL);
  484. }
  485. /*
  486. * Test to see if we've been building up a completion structure for
  487. * earlier buffers -- if so, we try to append to this ioend if we
  488. * can, otherwise we finish off any current ioend and start another.
  489. * Return true if we've finished the given ioend.
  490. */
  491. STATIC void
  492. xfs_add_to_ioend(
  493. struct inode *inode,
  494. struct buffer_head *bh,
  495. xfs_off_t offset,
  496. unsigned int type,
  497. xfs_ioend_t **result,
  498. int need_ioend)
  499. {
  500. xfs_ioend_t *ioend = *result;
  501. if (!ioend || need_ioend || type != ioend->io_type) {
  502. xfs_ioend_t *previous = *result;
  503. ioend = xfs_alloc_ioend(inode, type);
  504. ioend->io_offset = offset;
  505. ioend->io_buffer_head = bh;
  506. ioend->io_buffer_tail = bh;
  507. if (previous)
  508. previous->io_list = ioend;
  509. *result = ioend;
  510. } else {
  511. ioend->io_buffer_tail->b_private = bh;
  512. ioend->io_buffer_tail = bh;
  513. }
  514. bh->b_private = NULL;
  515. ioend->io_size += bh->b_size;
  516. }
  517. STATIC void
  518. xfs_map_buffer(
  519. struct inode *inode,
  520. struct buffer_head *bh,
  521. struct xfs_bmbt_irec *imap,
  522. xfs_off_t offset)
  523. {
  524. sector_t bn;
  525. struct xfs_mount *m = XFS_I(inode)->i_mount;
  526. xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
  527. xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
  528. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  529. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  530. bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
  531. ((offset - iomap_offset) >> inode->i_blkbits);
  532. ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
  533. bh->b_blocknr = bn;
  534. set_buffer_mapped(bh);
  535. }
  536. STATIC void
  537. xfs_map_at_offset(
  538. struct inode *inode,
  539. struct buffer_head *bh,
  540. struct xfs_bmbt_irec *imap,
  541. xfs_off_t offset)
  542. {
  543. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  544. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  545. xfs_map_buffer(inode, bh, imap, offset);
  546. set_buffer_mapped(bh);
  547. clear_buffer_delay(bh);
  548. clear_buffer_unwritten(bh);
  549. }
  550. /*
  551. * Test if a given page contains at least one buffer of a given @type.
  552. * If @check_all_buffers is true, then we walk all the buffers in the page to
  553. * try to find one of the type passed in. If it is not set, then the caller only
  554. * needs to check the first buffer on the page for a match.
  555. */
  556. STATIC bool
  557. xfs_check_page_type(
  558. struct page *page,
  559. unsigned int type,
  560. bool check_all_buffers)
  561. {
  562. struct buffer_head *bh;
  563. struct buffer_head *head;
  564. if (PageWriteback(page))
  565. return false;
  566. if (!page->mapping)
  567. return false;
  568. if (!page_has_buffers(page))
  569. return false;
  570. bh = head = page_buffers(page);
  571. do {
  572. if (buffer_unwritten(bh)) {
  573. if (type == XFS_IO_UNWRITTEN)
  574. return true;
  575. } else if (buffer_delay(bh)) {
  576. if (type == XFS_IO_DELALLOC)
  577. return true;
  578. } else if (buffer_dirty(bh) && buffer_mapped(bh)) {
  579. if (type == XFS_IO_OVERWRITE)
  580. return true;
  581. }
  582. /* If we are only checking the first buffer, we are done now. */
  583. if (!check_all_buffers)
  584. break;
  585. } while ((bh = bh->b_this_page) != head);
  586. return false;
  587. }
  588. /*
  589. * Allocate & map buffers for page given the extent map. Write it out.
  590. * except for the original page of a writepage, this is called on
  591. * delalloc/unwritten pages only, for the original page it is possible
  592. * that the page has no mapping at all.
  593. */
  594. STATIC int
  595. xfs_convert_page(
  596. struct inode *inode,
  597. struct page *page,
  598. loff_t tindex,
  599. struct xfs_bmbt_irec *imap,
  600. xfs_ioend_t **ioendp,
  601. struct writeback_control *wbc)
  602. {
  603. struct buffer_head *bh, *head;
  604. xfs_off_t end_offset;
  605. unsigned long p_offset;
  606. unsigned int type;
  607. int len, page_dirty;
  608. int count = 0, done = 0, uptodate = 1;
  609. xfs_off_t offset = page_offset(page);
  610. if (page->index != tindex)
  611. goto fail;
  612. if (!trylock_page(page))
  613. goto fail;
  614. if (PageWriteback(page))
  615. goto fail_unlock_page;
  616. if (page->mapping != inode->i_mapping)
  617. goto fail_unlock_page;
  618. if (!xfs_check_page_type(page, (*ioendp)->io_type, false))
  619. goto fail_unlock_page;
  620. /*
  621. * page_dirty is initially a count of buffers on the page before
  622. * EOF and is decremented as we move each into a cleanable state.
  623. *
  624. * Derivation:
  625. *
  626. * End offset is the highest offset that this page should represent.
  627. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  628. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  629. * hence give us the correct page_dirty count. On any other page,
  630. * it will be zero and in that case we need page_dirty to be the
  631. * count of buffers on the page.
  632. */
  633. end_offset = min_t(unsigned long long,
  634. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  635. i_size_read(inode));
  636. /*
  637. * If the current map does not span the entire page we are about to try
  638. * to write, then give up. The only way we can write a page that spans
  639. * multiple mappings in a single writeback iteration is via the
  640. * xfs_vm_writepage() function. Data integrity writeback requires the
  641. * entire page to be written in a single attempt, otherwise the part of
  642. * the page we don't write here doesn't get written as part of the data
  643. * integrity sync.
  644. *
  645. * For normal writeback, we also don't attempt to write partial pages
  646. * here as it simply means that write_cache_pages() will see it under
  647. * writeback and ignore the page until some point in the future, at
  648. * which time this will be the only page in the file that needs
  649. * writeback. Hence for more optimal IO patterns, we should always
  650. * avoid partial page writeback due to multiple mappings on a page here.
  651. */
  652. if (!xfs_imap_valid(inode, imap, end_offset))
  653. goto fail_unlock_page;
  654. len = 1 << inode->i_blkbits;
  655. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  656. PAGE_CACHE_SIZE);
  657. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  658. page_dirty = p_offset / len;
  659. /*
  660. * The moment we find a buffer that doesn't match our current type
  661. * specification or can't be written, abort the loop and start
  662. * writeback. As per the above xfs_imap_valid() check, only
  663. * xfs_vm_writepage() can handle partial page writeback fully - we are
  664. * limited here to the buffers that are contiguous with the current
  665. * ioend, and hence a buffer we can't write breaks that contiguity and
  666. * we have to defer the rest of the IO to xfs_vm_writepage().
  667. */
  668. bh = head = page_buffers(page);
  669. do {
  670. if (offset >= end_offset)
  671. break;
  672. if (!buffer_uptodate(bh))
  673. uptodate = 0;
  674. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  675. done = 1;
  676. break;
  677. }
  678. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  679. buffer_mapped(bh)) {
  680. if (buffer_unwritten(bh))
  681. type = XFS_IO_UNWRITTEN;
  682. else if (buffer_delay(bh))
  683. type = XFS_IO_DELALLOC;
  684. else
  685. type = XFS_IO_OVERWRITE;
  686. /*
  687. * imap should always be valid because of the above
  688. * partial page end_offset check on the imap.
  689. */
  690. ASSERT(xfs_imap_valid(inode, imap, offset));
  691. lock_buffer(bh);
  692. if (type != XFS_IO_OVERWRITE)
  693. xfs_map_at_offset(inode, bh, imap, offset);
  694. xfs_add_to_ioend(inode, bh, offset, type,
  695. ioendp, done);
  696. page_dirty--;
  697. count++;
  698. } else {
  699. done = 1;
  700. break;
  701. }
  702. } while (offset += len, (bh = bh->b_this_page) != head);
  703. if (uptodate && bh == head)
  704. SetPageUptodate(page);
  705. if (count) {
  706. if (--wbc->nr_to_write <= 0 &&
  707. wbc->sync_mode == WB_SYNC_NONE)
  708. done = 1;
  709. }
  710. xfs_start_page_writeback(page, !page_dirty, count);
  711. return done;
  712. fail_unlock_page:
  713. unlock_page(page);
  714. fail:
  715. return 1;
  716. }
  717. /*
  718. * Convert & write out a cluster of pages in the same extent as defined
  719. * by mp and following the start page.
  720. */
  721. STATIC void
  722. xfs_cluster_write(
  723. struct inode *inode,
  724. pgoff_t tindex,
  725. struct xfs_bmbt_irec *imap,
  726. xfs_ioend_t **ioendp,
  727. struct writeback_control *wbc,
  728. pgoff_t tlast)
  729. {
  730. struct pagevec pvec;
  731. int done = 0, i;
  732. pagevec_init(&pvec, 0);
  733. while (!done && tindex <= tlast) {
  734. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  735. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  736. break;
  737. for (i = 0; i < pagevec_count(&pvec); i++) {
  738. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  739. imap, ioendp, wbc);
  740. if (done)
  741. break;
  742. }
  743. pagevec_release(&pvec);
  744. cond_resched();
  745. }
  746. }
  747. STATIC void
  748. xfs_vm_invalidatepage(
  749. struct page *page,
  750. unsigned int offset,
  751. unsigned int length)
  752. {
  753. trace_xfs_invalidatepage(page->mapping->host, page, offset,
  754. length);
  755. block_invalidatepage(page, offset, length);
  756. }
  757. /*
  758. * If the page has delalloc buffers on it, we need to punch them out before we
  759. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  760. * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
  761. * is done on that same region - the delalloc extent is returned when none is
  762. * supposed to be there.
  763. *
  764. * We prevent this by truncating away the delalloc regions on the page before
  765. * invalidating it. Because they are delalloc, we can do this without needing a
  766. * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
  767. * truncation without a transaction as there is no space left for block
  768. * reservation (typically why we see a ENOSPC in writeback).
  769. *
  770. * This is not a performance critical path, so for now just do the punching a
  771. * buffer head at a time.
  772. */
  773. STATIC void
  774. xfs_aops_discard_page(
  775. struct page *page)
  776. {
  777. struct inode *inode = page->mapping->host;
  778. struct xfs_inode *ip = XFS_I(inode);
  779. struct buffer_head *bh, *head;
  780. loff_t offset = page_offset(page);
  781. if (!xfs_check_page_type(page, XFS_IO_DELALLOC, true))
  782. goto out_invalidate;
  783. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  784. goto out_invalidate;
  785. xfs_alert(ip->i_mount,
  786. "page discard on page %p, inode 0x%llx, offset %llu.",
  787. page, ip->i_ino, offset);
  788. xfs_ilock(ip, XFS_ILOCK_EXCL);
  789. bh = head = page_buffers(page);
  790. do {
  791. int error;
  792. xfs_fileoff_t start_fsb;
  793. if (!buffer_delay(bh))
  794. goto next_buffer;
  795. start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  796. error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
  797. if (error) {
  798. /* something screwed, just bail */
  799. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  800. xfs_alert(ip->i_mount,
  801. "page discard unable to remove delalloc mapping.");
  802. }
  803. break;
  804. }
  805. next_buffer:
  806. offset += 1 << inode->i_blkbits;
  807. } while ((bh = bh->b_this_page) != head);
  808. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  809. out_invalidate:
  810. xfs_vm_invalidatepage(page, 0, PAGE_CACHE_SIZE);
  811. return;
  812. }
  813. /*
  814. * Write out a dirty page.
  815. *
  816. * For delalloc space on the page we need to allocate space and flush it.
  817. * For unwritten space on the page we need to start the conversion to
  818. * regular allocated space.
  819. * For any other dirty buffer heads on the page we should flush them.
  820. */
  821. STATIC int
  822. xfs_vm_writepage(
  823. struct page *page,
  824. struct writeback_control *wbc)
  825. {
  826. struct inode *inode = page->mapping->host;
  827. struct buffer_head *bh, *head;
  828. struct xfs_bmbt_irec imap;
  829. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  830. loff_t offset;
  831. unsigned int type;
  832. __uint64_t end_offset;
  833. pgoff_t end_index, last_index;
  834. ssize_t len;
  835. int err, imap_valid = 0, uptodate = 1;
  836. int count = 0;
  837. int nonblocking = 0;
  838. trace_xfs_writepage(inode, page, 0, 0);
  839. ASSERT(page_has_buffers(page));
  840. /*
  841. * Refuse to write the page out if we are called from reclaim context.
  842. *
  843. * This avoids stack overflows when called from deeply used stacks in
  844. * random callers for direct reclaim or memcg reclaim. We explicitly
  845. * allow reclaim from kswapd as the stack usage there is relatively low.
  846. *
  847. * This should never happen except in the case of a VM regression so
  848. * warn about it.
  849. */
  850. if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
  851. PF_MEMALLOC))
  852. goto redirty;
  853. /*
  854. * Given that we do not allow direct reclaim to call us, we should
  855. * never be called while in a filesystem transaction.
  856. */
  857. if (WARN_ON_ONCE(current->flags & PF_FSTRANS))
  858. goto redirty;
  859. /* Is this page beyond the end of the file? */
  860. offset = i_size_read(inode);
  861. end_index = offset >> PAGE_CACHE_SHIFT;
  862. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  863. /*
  864. * The page index is less than the end_index, adjust the end_offset
  865. * to the highest offset that this page should represent.
  866. * -----------------------------------------------------
  867. * | file mapping | <EOF> |
  868. * -----------------------------------------------------
  869. * | Page ... | Page N-2 | Page N-1 | Page N | |
  870. * ^--------------------------------^----------|--------
  871. * | desired writeback range | see else |
  872. * ---------------------------------^------------------|
  873. */
  874. if (page->index < end_index)
  875. end_offset = (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT;
  876. else {
  877. /*
  878. * Check whether the page to write out is beyond or straddles
  879. * i_size or not.
  880. * -------------------------------------------------------
  881. * | file mapping | <EOF> |
  882. * -------------------------------------------------------
  883. * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
  884. * ^--------------------------------^-----------|---------
  885. * | | Straddles |
  886. * ---------------------------------^-----------|--------|
  887. */
  888. unsigned offset_into_page = offset & (PAGE_CACHE_SIZE - 1);
  889. /*
  890. * Skip the page if it is fully outside i_size, e.g. due to a
  891. * truncate operation that is in progress. We must redirty the
  892. * page so that reclaim stops reclaiming it. Otherwise
  893. * xfs_vm_releasepage() is called on it and gets confused.
  894. *
  895. * Note that the end_index is unsigned long, it would overflow
  896. * if the given offset is greater than 16TB on 32-bit system
  897. * and if we do check the page is fully outside i_size or not
  898. * via "if (page->index >= end_index + 1)" as "end_index + 1"
  899. * will be evaluated to 0. Hence this page will be redirtied
  900. * and be written out repeatedly which would result in an
  901. * infinite loop, the user program that perform this operation
  902. * will hang. Instead, we can verify this situation by checking
  903. * if the page to write is totally beyond the i_size or if it's
  904. * offset is just equal to the EOF.
  905. */
  906. if (page->index > end_index ||
  907. (page->index == end_index && offset_into_page == 0))
  908. goto redirty;
  909. /*
  910. * The page straddles i_size. It must be zeroed out on each
  911. * and every writepage invocation because it may be mmapped.
  912. * "A file is mapped in multiples of the page size. For a file
  913. * that is not a multiple of the page size, the remaining
  914. * memory is zeroed when mapped, and writes to that region are
  915. * not written out to the file."
  916. */
  917. zero_user_segment(page, offset_into_page, PAGE_CACHE_SIZE);
  918. /* Adjust the end_offset to the end of file */
  919. end_offset = offset;
  920. }
  921. len = 1 << inode->i_blkbits;
  922. bh = head = page_buffers(page);
  923. offset = page_offset(page);
  924. type = XFS_IO_OVERWRITE;
  925. if (wbc->sync_mode == WB_SYNC_NONE)
  926. nonblocking = 1;
  927. do {
  928. int new_ioend = 0;
  929. if (offset >= end_offset)
  930. break;
  931. if (!buffer_uptodate(bh))
  932. uptodate = 0;
  933. /*
  934. * set_page_dirty dirties all buffers in a page, independent
  935. * of their state. The dirty state however is entirely
  936. * meaningless for holes (!mapped && uptodate), so skip
  937. * buffers covering holes here.
  938. */
  939. if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
  940. imap_valid = 0;
  941. continue;
  942. }
  943. if (buffer_unwritten(bh)) {
  944. if (type != XFS_IO_UNWRITTEN) {
  945. type = XFS_IO_UNWRITTEN;
  946. imap_valid = 0;
  947. }
  948. } else if (buffer_delay(bh)) {
  949. if (type != XFS_IO_DELALLOC) {
  950. type = XFS_IO_DELALLOC;
  951. imap_valid = 0;
  952. }
  953. } else if (buffer_uptodate(bh)) {
  954. if (type != XFS_IO_OVERWRITE) {
  955. type = XFS_IO_OVERWRITE;
  956. imap_valid = 0;
  957. }
  958. } else {
  959. if (PageUptodate(page))
  960. ASSERT(buffer_mapped(bh));
  961. /*
  962. * This buffer is not uptodate and will not be
  963. * written to disk. Ensure that we will put any
  964. * subsequent writeable buffers into a new
  965. * ioend.
  966. */
  967. imap_valid = 0;
  968. continue;
  969. }
  970. if (imap_valid)
  971. imap_valid = xfs_imap_valid(inode, &imap, offset);
  972. if (!imap_valid) {
  973. /*
  974. * If we didn't have a valid mapping then we need to
  975. * put the new mapping into a separate ioend structure.
  976. * This ensures non-contiguous extents always have
  977. * separate ioends, which is particularly important
  978. * for unwritten extent conversion at I/O completion
  979. * time.
  980. */
  981. new_ioend = 1;
  982. err = xfs_map_blocks(inode, offset, &imap, type,
  983. nonblocking);
  984. if (err)
  985. goto error;
  986. imap_valid = xfs_imap_valid(inode, &imap, offset);
  987. }
  988. if (imap_valid) {
  989. lock_buffer(bh);
  990. if (type != XFS_IO_OVERWRITE)
  991. xfs_map_at_offset(inode, bh, &imap, offset);
  992. xfs_add_to_ioend(inode, bh, offset, type, &ioend,
  993. new_ioend);
  994. count++;
  995. }
  996. if (!iohead)
  997. iohead = ioend;
  998. } while (offset += len, ((bh = bh->b_this_page) != head));
  999. if (uptodate && bh == head)
  1000. SetPageUptodate(page);
  1001. xfs_start_page_writeback(page, 1, count);
  1002. /* if there is no IO to be submitted for this page, we are done */
  1003. if (!ioend)
  1004. return 0;
  1005. ASSERT(iohead);
  1006. /*
  1007. * Any errors from this point onwards need tobe reported through the IO
  1008. * completion path as we have marked the initial page as under writeback
  1009. * and unlocked it.
  1010. */
  1011. if (imap_valid) {
  1012. xfs_off_t end_index;
  1013. end_index = imap.br_startoff + imap.br_blockcount;
  1014. /* to bytes */
  1015. end_index <<= inode->i_blkbits;
  1016. /* to pages */
  1017. end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
  1018. /* check against file size */
  1019. if (end_index > last_index)
  1020. end_index = last_index;
  1021. xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
  1022. wbc, end_index);
  1023. }
  1024. /*
  1025. * Reserve log space if we might write beyond the on-disk inode size.
  1026. */
  1027. err = 0;
  1028. if (ioend->io_type != XFS_IO_UNWRITTEN && xfs_ioend_is_append(ioend))
  1029. err = xfs_setfilesize_trans_alloc(ioend);
  1030. xfs_submit_ioend(wbc, iohead, err);
  1031. return 0;
  1032. error:
  1033. if (iohead)
  1034. xfs_cancel_ioend(iohead);
  1035. if (err == -EAGAIN)
  1036. goto redirty;
  1037. xfs_aops_discard_page(page);
  1038. ClearPageUptodate(page);
  1039. unlock_page(page);
  1040. return err;
  1041. redirty:
  1042. redirty_page_for_writepage(wbc, page);
  1043. unlock_page(page);
  1044. return 0;
  1045. }
  1046. STATIC int
  1047. xfs_vm_writepages(
  1048. struct address_space *mapping,
  1049. struct writeback_control *wbc)
  1050. {
  1051. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  1052. return generic_writepages(mapping, wbc);
  1053. }
  1054. /*
  1055. * Called to move a page into cleanable state - and from there
  1056. * to be released. The page should already be clean. We always
  1057. * have buffer heads in this call.
  1058. *
  1059. * Returns 1 if the page is ok to release, 0 otherwise.
  1060. */
  1061. STATIC int
  1062. xfs_vm_releasepage(
  1063. struct page *page,
  1064. gfp_t gfp_mask)
  1065. {
  1066. int delalloc, unwritten;
  1067. trace_xfs_releasepage(page->mapping->host, page, 0, 0);
  1068. xfs_count_page_state(page, &delalloc, &unwritten);
  1069. if (WARN_ON_ONCE(delalloc))
  1070. return 0;
  1071. if (WARN_ON_ONCE(unwritten))
  1072. return 0;
  1073. return try_to_free_buffers(page);
  1074. }
  1075. /*
  1076. * When we map a DIO buffer, we may need to attach an ioend that describes the
  1077. * type of write IO we are doing. This passes to the completion function the
  1078. * operations it needs to perform. If the mapping is for an overwrite wholly
  1079. * within the EOF then we don't need an ioend and so we don't allocate one.
  1080. * This avoids the unnecessary overhead of allocating and freeing ioends for
  1081. * workloads that don't require transactions on IO completion.
  1082. *
  1083. * If we get multiple mappings in a single IO, we might be mapping different
  1084. * types. But because the direct IO can only have a single private pointer, we
  1085. * need to ensure that:
  1086. *
  1087. * a) i) the ioend spans the entire region of unwritten mappings; or
  1088. * ii) the ioend spans all the mappings that cross or are beyond EOF; and
  1089. * b) if it contains unwritten extents, it is *permanently* marked as such
  1090. *
  1091. * We could do this by chaining ioends like buffered IO does, but we only
  1092. * actually get one IO completion callback from the direct IO, and that spans
  1093. * the entire IO regardless of how many mappings and IOs are needed to complete
  1094. * the DIO. There is only going to be one reference to the ioend and its life
  1095. * cycle is constrained by the DIO completion code. hence we don't need
  1096. * reference counting here.
  1097. */
  1098. static void
  1099. xfs_map_direct(
  1100. struct inode *inode,
  1101. struct buffer_head *bh_result,
  1102. struct xfs_bmbt_irec *imap,
  1103. xfs_off_t offset)
  1104. {
  1105. struct xfs_ioend *ioend;
  1106. xfs_off_t size = bh_result->b_size;
  1107. int type;
  1108. if (ISUNWRITTEN(imap))
  1109. type = XFS_IO_UNWRITTEN;
  1110. else
  1111. type = XFS_IO_OVERWRITE;
  1112. trace_xfs_gbmap_direct(XFS_I(inode), offset, size, type, imap);
  1113. if (bh_result->b_private) {
  1114. ioend = bh_result->b_private;
  1115. ASSERT(ioend->io_size > 0);
  1116. ASSERT(offset >= ioend->io_offset);
  1117. if (offset + size > ioend->io_offset + ioend->io_size)
  1118. ioend->io_size = offset - ioend->io_offset + size;
  1119. if (type == XFS_IO_UNWRITTEN && type != ioend->io_type)
  1120. ioend->io_type = XFS_IO_UNWRITTEN;
  1121. trace_xfs_gbmap_direct_update(XFS_I(inode), ioend->io_offset,
  1122. ioend->io_size, ioend->io_type,
  1123. imap);
  1124. } else if (type == XFS_IO_UNWRITTEN ||
  1125. offset + size > i_size_read(inode)) {
  1126. ioend = xfs_alloc_ioend(inode, type);
  1127. ioend->io_offset = offset;
  1128. ioend->io_size = size;
  1129. bh_result->b_private = ioend;
  1130. set_buffer_defer_completion(bh_result);
  1131. trace_xfs_gbmap_direct_new(XFS_I(inode), offset, size, type,
  1132. imap);
  1133. } else {
  1134. trace_xfs_gbmap_direct_none(XFS_I(inode), offset, size, type,
  1135. imap);
  1136. }
  1137. }
  1138. /*
  1139. * If this is O_DIRECT or the mpage code calling tell them how large the mapping
  1140. * is, so that we can avoid repeated get_blocks calls.
  1141. *
  1142. * If the mapping spans EOF, then we have to break the mapping up as the mapping
  1143. * for blocks beyond EOF must be marked new so that sub block regions can be
  1144. * correctly zeroed. We can't do this for mappings within EOF unless the mapping
  1145. * was just allocated or is unwritten, otherwise the callers would overwrite
  1146. * existing data with zeros. Hence we have to split the mapping into a range up
  1147. * to and including EOF, and a second mapping for beyond EOF.
  1148. */
  1149. static void
  1150. xfs_map_trim_size(
  1151. struct inode *inode,
  1152. sector_t iblock,
  1153. struct buffer_head *bh_result,
  1154. struct xfs_bmbt_irec *imap,
  1155. xfs_off_t offset,
  1156. ssize_t size)
  1157. {
  1158. xfs_off_t mapping_size;
  1159. mapping_size = imap->br_startoff + imap->br_blockcount - iblock;
  1160. mapping_size <<= inode->i_blkbits;
  1161. ASSERT(mapping_size > 0);
  1162. if (mapping_size > size)
  1163. mapping_size = size;
  1164. if (offset < i_size_read(inode) &&
  1165. offset + mapping_size >= i_size_read(inode)) {
  1166. /* limit mapping to block that spans EOF */
  1167. mapping_size = roundup_64(i_size_read(inode) - offset,
  1168. 1 << inode->i_blkbits);
  1169. }
  1170. if (mapping_size > LONG_MAX)
  1171. mapping_size = LONG_MAX;
  1172. bh_result->b_size = mapping_size;
  1173. }
  1174. STATIC int
  1175. __xfs_get_blocks(
  1176. struct inode *inode,
  1177. sector_t iblock,
  1178. struct buffer_head *bh_result,
  1179. int create,
  1180. bool direct)
  1181. {
  1182. struct xfs_inode *ip = XFS_I(inode);
  1183. struct xfs_mount *mp = ip->i_mount;
  1184. xfs_fileoff_t offset_fsb, end_fsb;
  1185. int error = 0;
  1186. int lockmode = 0;
  1187. struct xfs_bmbt_irec imap;
  1188. int nimaps = 1;
  1189. xfs_off_t offset;
  1190. ssize_t size;
  1191. int new = 0;
  1192. if (XFS_FORCED_SHUTDOWN(mp))
  1193. return -EIO;
  1194. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1195. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  1196. size = bh_result->b_size;
  1197. if (!create && direct && offset >= i_size_read(inode))
  1198. return 0;
  1199. /*
  1200. * Direct I/O is usually done on preallocated files, so try getting
  1201. * a block mapping without an exclusive lock first. For buffered
  1202. * writes we already have the exclusive iolock anyway, so avoiding
  1203. * a lock roundtrip here by taking the ilock exclusive from the
  1204. * beginning is a useful micro optimization.
  1205. */
  1206. if (create && !direct) {
  1207. lockmode = XFS_ILOCK_EXCL;
  1208. xfs_ilock(ip, lockmode);
  1209. } else {
  1210. lockmode = xfs_ilock_data_map_shared(ip);
  1211. }
  1212. ASSERT(offset <= mp->m_super->s_maxbytes);
  1213. if (offset + size > mp->m_super->s_maxbytes)
  1214. size = mp->m_super->s_maxbytes - offset;
  1215. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
  1216. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1217. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  1218. &imap, &nimaps, XFS_BMAPI_ENTIRE);
  1219. if (error)
  1220. goto out_unlock;
  1221. if (create &&
  1222. (!nimaps ||
  1223. (imap.br_startblock == HOLESTARTBLOCK ||
  1224. imap.br_startblock == DELAYSTARTBLOCK))) {
  1225. if (direct || xfs_get_extsz_hint(ip)) {
  1226. /*
  1227. * Drop the ilock in preparation for starting the block
  1228. * allocation transaction. It will be retaken
  1229. * exclusively inside xfs_iomap_write_direct for the
  1230. * actual allocation.
  1231. */
  1232. xfs_iunlock(ip, lockmode);
  1233. error = xfs_iomap_write_direct(ip, offset, size,
  1234. &imap, nimaps);
  1235. if (error)
  1236. return error;
  1237. new = 1;
  1238. } else {
  1239. /*
  1240. * Delalloc reservations do not require a transaction,
  1241. * we can go on without dropping the lock here. If we
  1242. * are allocating a new delalloc block, make sure that
  1243. * we set the new flag so that we mark the buffer new so
  1244. * that we know that it is newly allocated if the write
  1245. * fails.
  1246. */
  1247. if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
  1248. new = 1;
  1249. error = xfs_iomap_write_delay(ip, offset, size, &imap);
  1250. if (error)
  1251. goto out_unlock;
  1252. xfs_iunlock(ip, lockmode);
  1253. }
  1254. trace_xfs_get_blocks_alloc(ip, offset, size,
  1255. ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
  1256. : XFS_IO_DELALLOC, &imap);
  1257. } else if (nimaps) {
  1258. trace_xfs_get_blocks_found(ip, offset, size,
  1259. ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
  1260. : XFS_IO_OVERWRITE, &imap);
  1261. xfs_iunlock(ip, lockmode);
  1262. } else {
  1263. trace_xfs_get_blocks_notfound(ip, offset, size);
  1264. goto out_unlock;
  1265. }
  1266. /* trim mapping down to size requested */
  1267. if (direct || size > (1 << inode->i_blkbits))
  1268. xfs_map_trim_size(inode, iblock, bh_result,
  1269. &imap, offset, size);
  1270. /*
  1271. * For unwritten extents do not report a disk address in the buffered
  1272. * read case (treat as if we're reading into a hole).
  1273. */
  1274. if (imap.br_startblock != HOLESTARTBLOCK &&
  1275. imap.br_startblock != DELAYSTARTBLOCK &&
  1276. (create || !ISUNWRITTEN(&imap))) {
  1277. xfs_map_buffer(inode, bh_result, &imap, offset);
  1278. if (ISUNWRITTEN(&imap))
  1279. set_buffer_unwritten(bh_result);
  1280. /* direct IO needs special help */
  1281. if (create && direct)
  1282. xfs_map_direct(inode, bh_result, &imap, offset);
  1283. }
  1284. /*
  1285. * If this is a realtime file, data may be on a different device.
  1286. * to that pointed to from the buffer_head b_bdev currently.
  1287. */
  1288. bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
  1289. /*
  1290. * If we previously allocated a block out beyond eof and we are now
  1291. * coming back to use it then we will need to flag it as new even if it
  1292. * has a disk address.
  1293. *
  1294. * With sub-block writes into unwritten extents we also need to mark
  1295. * the buffer as new so that the unwritten parts of the buffer gets
  1296. * correctly zeroed.
  1297. */
  1298. if (create &&
  1299. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1300. (offset >= i_size_read(inode)) ||
  1301. (new || ISUNWRITTEN(&imap))))
  1302. set_buffer_new(bh_result);
  1303. if (imap.br_startblock == DELAYSTARTBLOCK) {
  1304. BUG_ON(direct);
  1305. if (create) {
  1306. set_buffer_uptodate(bh_result);
  1307. set_buffer_mapped(bh_result);
  1308. set_buffer_delay(bh_result);
  1309. }
  1310. }
  1311. return 0;
  1312. out_unlock:
  1313. xfs_iunlock(ip, lockmode);
  1314. return error;
  1315. }
  1316. int
  1317. xfs_get_blocks(
  1318. struct inode *inode,
  1319. sector_t iblock,
  1320. struct buffer_head *bh_result,
  1321. int create)
  1322. {
  1323. return __xfs_get_blocks(inode, iblock, bh_result, create, false);
  1324. }
  1325. int
  1326. xfs_get_blocks_direct(
  1327. struct inode *inode,
  1328. sector_t iblock,
  1329. struct buffer_head *bh_result,
  1330. int create)
  1331. {
  1332. return __xfs_get_blocks(inode, iblock, bh_result, create, true);
  1333. }
  1334. static void
  1335. __xfs_end_io_direct_write(
  1336. struct inode *inode,
  1337. struct xfs_ioend *ioend,
  1338. loff_t offset,
  1339. ssize_t size)
  1340. {
  1341. struct xfs_mount *mp = XFS_I(inode)->i_mount;
  1342. if (XFS_FORCED_SHUTDOWN(mp) || ioend->io_error)
  1343. goto out_end_io;
  1344. /*
  1345. * dio completion end_io functions are only called on writes if more
  1346. * than 0 bytes was written.
  1347. */
  1348. ASSERT(size > 0);
  1349. /*
  1350. * The ioend only maps whole blocks, while the IO may be sector aligned.
  1351. * Hence the ioend offset/size may not match the IO offset/size exactly.
  1352. * Because we don't map overwrites within EOF into the ioend, the offset
  1353. * may not match, but only if the endio spans EOF. Either way, write
  1354. * the IO sizes into the ioend so that completion processing does the
  1355. * right thing.
  1356. */
  1357. ASSERT(offset + size <= ioend->io_offset + ioend->io_size);
  1358. ioend->io_size = size;
  1359. ioend->io_offset = offset;
  1360. /*
  1361. * The ioend tells us whether we are doing unwritten extent conversion
  1362. * or an append transaction that updates the on-disk file size. These
  1363. * cases are the only cases where we should *potentially* be needing
  1364. * to update the VFS inode size.
  1365. *
  1366. * We need to update the in-core inode size here so that we don't end up
  1367. * with the on-disk inode size being outside the in-core inode size. We
  1368. * have no other method of updating EOF for AIO, so always do it here
  1369. * if necessary.
  1370. *
  1371. * We need to lock the test/set EOF update as we can be racing with
  1372. * other IO completions here to update the EOF. Failing to serialise
  1373. * here can result in EOF moving backwards and Bad Things Happen when
  1374. * that occurs.
  1375. */
  1376. spin_lock(&XFS_I(inode)->i_flags_lock);
  1377. if (offset + size > i_size_read(inode))
  1378. i_size_write(inode, offset + size);
  1379. spin_unlock(&XFS_I(inode)->i_flags_lock);
  1380. /*
  1381. * If we are doing an append IO that needs to update the EOF on disk,
  1382. * do the transaction reserve now so we can use common end io
  1383. * processing. Stashing the error (if there is one) in the ioend will
  1384. * result in the ioend processing passing on the error if it is
  1385. * possible as we can't return it from here.
  1386. */
  1387. if (ioend->io_type == XFS_IO_OVERWRITE)
  1388. ioend->io_error = xfs_setfilesize_trans_alloc(ioend);
  1389. out_end_io:
  1390. xfs_end_io(&ioend->io_work);
  1391. return;
  1392. }
  1393. /*
  1394. * Complete a direct I/O write request.
  1395. *
  1396. * The ioend structure is passed from __xfs_get_blocks() to tell us what to do.
  1397. * If no ioend exists (i.e. @private == NULL) then the write IO is an overwrite
  1398. * wholly within the EOF and so there is nothing for us to do. Note that in this
  1399. * case the completion can be called in interrupt context, whereas if we have an
  1400. * ioend we will always be called in task context (i.e. from a workqueue).
  1401. */
  1402. STATIC void
  1403. xfs_end_io_direct_write(
  1404. struct kiocb *iocb,
  1405. loff_t offset,
  1406. ssize_t size,
  1407. void *private)
  1408. {
  1409. struct inode *inode = file_inode(iocb->ki_filp);
  1410. struct xfs_ioend *ioend = private;
  1411. trace_xfs_gbmap_direct_endio(XFS_I(inode), offset, size,
  1412. ioend ? ioend->io_type : 0, NULL);
  1413. if (!ioend) {
  1414. ASSERT(offset + size <= i_size_read(inode));
  1415. return;
  1416. }
  1417. __xfs_end_io_direct_write(inode, ioend, offset, size);
  1418. }
  1419. /*
  1420. * For DAX we need a mapping buffer callback for unwritten extent conversion
  1421. * when page faults allocate blocks and then zero them. Note that in this
  1422. * case the mapping indicated by the ioend may extend beyond EOF. We most
  1423. * definitely do not want to extend EOF here, so we trim back the ioend size to
  1424. * EOF.
  1425. */
  1426. #ifdef CONFIG_FS_DAX
  1427. void
  1428. xfs_end_io_dax_write(
  1429. struct buffer_head *bh,
  1430. int uptodate)
  1431. {
  1432. struct xfs_ioend *ioend = bh->b_private;
  1433. struct inode *inode = ioend->io_inode;
  1434. ssize_t size = ioend->io_size;
  1435. ASSERT(IS_DAX(ioend->io_inode));
  1436. /* if there was an error zeroing, then don't convert it */
  1437. if (!uptodate)
  1438. ioend->io_error = -EIO;
  1439. /*
  1440. * Trim update to EOF, so we don't extend EOF during unwritten extent
  1441. * conversion of partial EOF blocks.
  1442. */
  1443. spin_lock(&XFS_I(inode)->i_flags_lock);
  1444. if (ioend->io_offset + size > i_size_read(inode))
  1445. size = i_size_read(inode) - ioend->io_offset;
  1446. spin_unlock(&XFS_I(inode)->i_flags_lock);
  1447. __xfs_end_io_direct_write(inode, ioend, ioend->io_offset, size);
  1448. }
  1449. #else
  1450. void xfs_end_io_dax_write(struct buffer_head *bh, int uptodate) { }
  1451. #endif
  1452. static inline ssize_t
  1453. xfs_vm_do_dio(
  1454. struct inode *inode,
  1455. struct kiocb *iocb,
  1456. struct iov_iter *iter,
  1457. loff_t offset,
  1458. void (*endio)(struct kiocb *iocb,
  1459. loff_t offset,
  1460. ssize_t size,
  1461. void *private),
  1462. int flags)
  1463. {
  1464. struct block_device *bdev;
  1465. if (IS_DAX(inode))
  1466. return dax_do_io(iocb, inode, iter, offset,
  1467. xfs_get_blocks_direct, endio, 0);
  1468. bdev = xfs_find_bdev_for_inode(inode);
  1469. return __blockdev_direct_IO(iocb, inode, bdev, iter, offset,
  1470. xfs_get_blocks_direct, endio, NULL, flags);
  1471. }
  1472. STATIC ssize_t
  1473. xfs_vm_direct_IO(
  1474. struct kiocb *iocb,
  1475. struct iov_iter *iter,
  1476. loff_t offset)
  1477. {
  1478. struct inode *inode = iocb->ki_filp->f_mapping->host;
  1479. if (iov_iter_rw(iter) == WRITE)
  1480. return xfs_vm_do_dio(inode, iocb, iter, offset,
  1481. xfs_end_io_direct_write, DIO_ASYNC_EXTEND);
  1482. return xfs_vm_do_dio(inode, iocb, iter, offset, NULL, 0);
  1483. }
  1484. /*
  1485. * Punch out the delalloc blocks we have already allocated.
  1486. *
  1487. * Don't bother with xfs_setattr given that nothing can have made it to disk yet
  1488. * as the page is still locked at this point.
  1489. */
  1490. STATIC void
  1491. xfs_vm_kill_delalloc_range(
  1492. struct inode *inode,
  1493. loff_t start,
  1494. loff_t end)
  1495. {
  1496. struct xfs_inode *ip = XFS_I(inode);
  1497. xfs_fileoff_t start_fsb;
  1498. xfs_fileoff_t end_fsb;
  1499. int error;
  1500. start_fsb = XFS_B_TO_FSB(ip->i_mount, start);
  1501. end_fsb = XFS_B_TO_FSB(ip->i_mount, end);
  1502. if (end_fsb <= start_fsb)
  1503. return;
  1504. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1505. error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
  1506. end_fsb - start_fsb);
  1507. if (error) {
  1508. /* something screwed, just bail */
  1509. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1510. xfs_alert(ip->i_mount,
  1511. "xfs_vm_write_failed: unable to clean up ino %lld",
  1512. ip->i_ino);
  1513. }
  1514. }
  1515. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1516. }
  1517. STATIC void
  1518. xfs_vm_write_failed(
  1519. struct inode *inode,
  1520. struct page *page,
  1521. loff_t pos,
  1522. unsigned len)
  1523. {
  1524. loff_t block_offset;
  1525. loff_t block_start;
  1526. loff_t block_end;
  1527. loff_t from = pos & (PAGE_CACHE_SIZE - 1);
  1528. loff_t to = from + len;
  1529. struct buffer_head *bh, *head;
  1530. /*
  1531. * The request pos offset might be 32 or 64 bit, this is all fine
  1532. * on 64-bit platform. However, for 64-bit pos request on 32-bit
  1533. * platform, the high 32-bit will be masked off if we evaluate the
  1534. * block_offset via (pos & PAGE_MASK) because the PAGE_MASK is
  1535. * 0xfffff000 as an unsigned long, hence the result is incorrect
  1536. * which could cause the following ASSERT failed in most cases.
  1537. * In order to avoid this, we can evaluate the block_offset of the
  1538. * start of the page by using shifts rather than masks the mismatch
  1539. * problem.
  1540. */
  1541. block_offset = (pos >> PAGE_CACHE_SHIFT) << PAGE_CACHE_SHIFT;
  1542. ASSERT(block_offset + from == pos);
  1543. head = page_buffers(page);
  1544. block_start = 0;
  1545. for (bh = head; bh != head || !block_start;
  1546. bh = bh->b_this_page, block_start = block_end,
  1547. block_offset += bh->b_size) {
  1548. block_end = block_start + bh->b_size;
  1549. /* skip buffers before the write */
  1550. if (block_end <= from)
  1551. continue;
  1552. /* if the buffer is after the write, we're done */
  1553. if (block_start >= to)
  1554. break;
  1555. if (!buffer_delay(bh))
  1556. continue;
  1557. if (!buffer_new(bh) && block_offset < i_size_read(inode))
  1558. continue;
  1559. xfs_vm_kill_delalloc_range(inode, block_offset,
  1560. block_offset + bh->b_size);
  1561. /*
  1562. * This buffer does not contain data anymore. make sure anyone
  1563. * who finds it knows that for certain.
  1564. */
  1565. clear_buffer_delay(bh);
  1566. clear_buffer_uptodate(bh);
  1567. clear_buffer_mapped(bh);
  1568. clear_buffer_new(bh);
  1569. clear_buffer_dirty(bh);
  1570. }
  1571. }
  1572. /*
  1573. * This used to call block_write_begin(), but it unlocks and releases the page
  1574. * on error, and we need that page to be able to punch stale delalloc blocks out
  1575. * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at
  1576. * the appropriate point.
  1577. */
  1578. STATIC int
  1579. xfs_vm_write_begin(
  1580. struct file *file,
  1581. struct address_space *mapping,
  1582. loff_t pos,
  1583. unsigned len,
  1584. unsigned flags,
  1585. struct page **pagep,
  1586. void **fsdata)
  1587. {
  1588. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1589. struct page *page;
  1590. int status;
  1591. ASSERT(len <= PAGE_CACHE_SIZE);
  1592. page = grab_cache_page_write_begin(mapping, index, flags);
  1593. if (!page)
  1594. return -ENOMEM;
  1595. status = __block_write_begin(page, pos, len, xfs_get_blocks);
  1596. if (unlikely(status)) {
  1597. struct inode *inode = mapping->host;
  1598. size_t isize = i_size_read(inode);
  1599. xfs_vm_write_failed(inode, page, pos, len);
  1600. unlock_page(page);
  1601. /*
  1602. * If the write is beyond EOF, we only want to kill blocks
  1603. * allocated in this write, not blocks that were previously
  1604. * written successfully.
  1605. */
  1606. if (pos + len > isize) {
  1607. ssize_t start = max_t(ssize_t, pos, isize);
  1608. truncate_pagecache_range(inode, start, pos + len);
  1609. }
  1610. page_cache_release(page);
  1611. page = NULL;
  1612. }
  1613. *pagep = page;
  1614. return status;
  1615. }
  1616. /*
  1617. * On failure, we only need to kill delalloc blocks beyond EOF in the range of
  1618. * this specific write because they will never be written. Previous writes
  1619. * beyond EOF where block allocation succeeded do not need to be trashed, so
  1620. * only new blocks from this write should be trashed. For blocks within
  1621. * EOF, generic_write_end() zeros them so they are safe to leave alone and be
  1622. * written with all the other valid data.
  1623. */
  1624. STATIC int
  1625. xfs_vm_write_end(
  1626. struct file *file,
  1627. struct address_space *mapping,
  1628. loff_t pos,
  1629. unsigned len,
  1630. unsigned copied,
  1631. struct page *page,
  1632. void *fsdata)
  1633. {
  1634. int ret;
  1635. ASSERT(len <= PAGE_CACHE_SIZE);
  1636. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  1637. if (unlikely(ret < len)) {
  1638. struct inode *inode = mapping->host;
  1639. size_t isize = i_size_read(inode);
  1640. loff_t to = pos + len;
  1641. if (to > isize) {
  1642. /* only kill blocks in this write beyond EOF */
  1643. if (pos > isize)
  1644. isize = pos;
  1645. xfs_vm_kill_delalloc_range(inode, isize, to);
  1646. truncate_pagecache_range(inode, isize, to);
  1647. }
  1648. }
  1649. return ret;
  1650. }
  1651. STATIC sector_t
  1652. xfs_vm_bmap(
  1653. struct address_space *mapping,
  1654. sector_t block)
  1655. {
  1656. struct inode *inode = (struct inode *)mapping->host;
  1657. struct xfs_inode *ip = XFS_I(inode);
  1658. trace_xfs_vm_bmap(XFS_I(inode));
  1659. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1660. filemap_write_and_wait(mapping);
  1661. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1662. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1663. }
  1664. STATIC int
  1665. xfs_vm_readpage(
  1666. struct file *unused,
  1667. struct page *page)
  1668. {
  1669. return mpage_readpage(page, xfs_get_blocks);
  1670. }
  1671. STATIC int
  1672. xfs_vm_readpages(
  1673. struct file *unused,
  1674. struct address_space *mapping,
  1675. struct list_head *pages,
  1676. unsigned nr_pages)
  1677. {
  1678. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1679. }
  1680. /*
  1681. * This is basically a copy of __set_page_dirty_buffers() with one
  1682. * small tweak: buffers beyond EOF do not get marked dirty. If we mark them
  1683. * dirty, we'll never be able to clean them because we don't write buffers
  1684. * beyond EOF, and that means we can't invalidate pages that span EOF
  1685. * that have been marked dirty. Further, the dirty state can leak into
  1686. * the file interior if the file is extended, resulting in all sorts of
  1687. * bad things happening as the state does not match the underlying data.
  1688. *
  1689. * XXX: this really indicates that bufferheads in XFS need to die. Warts like
  1690. * this only exist because of bufferheads and how the generic code manages them.
  1691. */
  1692. STATIC int
  1693. xfs_vm_set_page_dirty(
  1694. struct page *page)
  1695. {
  1696. struct address_space *mapping = page->mapping;
  1697. struct inode *inode = mapping->host;
  1698. loff_t end_offset;
  1699. loff_t offset;
  1700. int newly_dirty;
  1701. struct mem_cgroup *memcg;
  1702. if (unlikely(!mapping))
  1703. return !TestSetPageDirty(page);
  1704. end_offset = i_size_read(inode);
  1705. offset = page_offset(page);
  1706. spin_lock(&mapping->private_lock);
  1707. if (page_has_buffers(page)) {
  1708. struct buffer_head *head = page_buffers(page);
  1709. struct buffer_head *bh = head;
  1710. do {
  1711. if (offset < end_offset)
  1712. set_buffer_dirty(bh);
  1713. bh = bh->b_this_page;
  1714. offset += 1 << inode->i_blkbits;
  1715. } while (bh != head);
  1716. }
  1717. /*
  1718. * Use mem_group_begin_page_stat() to keep PageDirty synchronized with
  1719. * per-memcg dirty page counters.
  1720. */
  1721. memcg = mem_cgroup_begin_page_stat(page);
  1722. newly_dirty = !TestSetPageDirty(page);
  1723. spin_unlock(&mapping->private_lock);
  1724. if (newly_dirty) {
  1725. /* sigh - __set_page_dirty() is static, so copy it here, too */
  1726. unsigned long flags;
  1727. spin_lock_irqsave(&mapping->tree_lock, flags);
  1728. if (page->mapping) { /* Race with truncate? */
  1729. WARN_ON_ONCE(!PageUptodate(page));
  1730. account_page_dirtied(page, mapping, memcg);
  1731. radix_tree_tag_set(&mapping->page_tree,
  1732. page_index(page), PAGECACHE_TAG_DIRTY);
  1733. }
  1734. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1735. }
  1736. mem_cgroup_end_page_stat(memcg);
  1737. if (newly_dirty)
  1738. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1739. return newly_dirty;
  1740. }
  1741. const struct address_space_operations xfs_address_space_operations = {
  1742. .readpage = xfs_vm_readpage,
  1743. .readpages = xfs_vm_readpages,
  1744. .writepage = xfs_vm_writepage,
  1745. .writepages = xfs_vm_writepages,
  1746. .set_page_dirty = xfs_vm_set_page_dirty,
  1747. .releasepage = xfs_vm_releasepage,
  1748. .invalidatepage = xfs_vm_invalidatepage,
  1749. .write_begin = xfs_vm_write_begin,
  1750. .write_end = xfs_vm_write_end,
  1751. .bmap = xfs_vm_bmap,
  1752. .direct_IO = xfs_vm_direct_IO,
  1753. .migratepage = buffer_migrate_page,
  1754. .is_partially_uptodate = block_is_partially_uptodate,
  1755. .error_remove_page = generic_error_remove_page,
  1756. };