xfs_aops.c 54 KB

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