xfs_aops.c 54 KB

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