xfs_aops.c 54 KB

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