xfs_aops.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_sb.h"
  22. #include "xfs_ag.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_bmap_btree.h"
  26. #include "xfs_dinode.h"
  27. #include "xfs_inode.h"
  28. #include "xfs_inode_item.h"
  29. #include "xfs_alloc.h"
  30. #include "xfs_error.h"
  31. #include "xfs_rw.h"
  32. #include "xfs_iomap.h"
  33. #include "xfs_vnodeops.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_bmap.h"
  36. #include <linux/gfp.h>
  37. #include <linux/mpage.h>
  38. #include <linux/pagevec.h>
  39. #include <linux/writeback.h>
  40. void
  41. xfs_count_page_state(
  42. struct page *page,
  43. int *delalloc,
  44. int *unwritten)
  45. {
  46. struct buffer_head *bh, *head;
  47. *delalloc = *unwritten = 0;
  48. bh = head = page_buffers(page);
  49. do {
  50. if (buffer_unwritten(bh))
  51. (*unwritten) = 1;
  52. else if (buffer_delay(bh))
  53. (*delalloc) = 1;
  54. } while ((bh = bh->b_this_page) != head);
  55. }
  56. STATIC struct block_device *
  57. xfs_find_bdev_for_inode(
  58. struct inode *inode)
  59. {
  60. struct xfs_inode *ip = XFS_I(inode);
  61. struct xfs_mount *mp = ip->i_mount;
  62. if (XFS_IS_REALTIME_INODE(ip))
  63. return mp->m_rtdev_targp->bt_bdev;
  64. else
  65. return mp->m_ddev_targp->bt_bdev;
  66. }
  67. /*
  68. * We're now finished for good with this ioend structure.
  69. * Update the page state via the associated buffer_heads,
  70. * release holds on the inode and bio, and finally free
  71. * up memory. Do not use the ioend after this.
  72. */
  73. STATIC void
  74. xfs_destroy_ioend(
  75. xfs_ioend_t *ioend)
  76. {
  77. struct buffer_head *bh, *next;
  78. for (bh = ioend->io_buffer_head; bh; bh = next) {
  79. next = bh->b_private;
  80. bh->b_end_io(bh, !ioend->io_error);
  81. }
  82. if (ioend->io_iocb) {
  83. if (ioend->io_isasync) {
  84. aio_complete(ioend->io_iocb, ioend->io_error ?
  85. ioend->io_error : ioend->io_result, 0);
  86. }
  87. inode_dio_done(ioend->io_inode);
  88. }
  89. mempool_free(ioend, xfs_ioend_pool);
  90. }
  91. /*
  92. * Fast and loose check if this write could update the on-disk inode size.
  93. */
  94. static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
  95. {
  96. return ioend->io_offset + ioend->io_size >
  97. XFS_I(ioend->io_inode)->i_d.di_size;
  98. }
  99. STATIC int
  100. xfs_setfilesize_trans_alloc(
  101. struct xfs_ioend *ioend)
  102. {
  103. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  104. struct xfs_trans *tp;
  105. int error;
  106. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  107. error = xfs_trans_reserve(tp, 0, XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
  108. if (error) {
  109. xfs_trans_cancel(tp, 0);
  110. return error;
  111. }
  112. ioend->io_append_trans = tp;
  113. /*
  114. * We hand off the transaction to the completion thread now, so
  115. * clear the flag here.
  116. */
  117. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  118. return 0;
  119. }
  120. /*
  121. * Update on-disk file size now that data has been written to disk.
  122. */
  123. STATIC int
  124. xfs_setfilesize(
  125. struct xfs_ioend *ioend)
  126. {
  127. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  128. struct xfs_trans *tp = ioend->io_append_trans;
  129. xfs_fsize_t isize;
  130. /*
  131. * The transaction was allocated in the I/O submission thread,
  132. * thus we need to mark ourselves as beeing in a transaction
  133. * manually.
  134. */
  135. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  136. xfs_ilock(ip, XFS_ILOCK_EXCL);
  137. isize = xfs_new_eof(ip, ioend->io_offset + ioend->io_size);
  138. if (!isize) {
  139. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  140. xfs_trans_cancel(tp, 0);
  141. return 0;
  142. }
  143. trace_xfs_setfilesize(ip, ioend->io_offset, ioend->io_size);
  144. ip->i_d.di_size = isize;
  145. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  146. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  147. return xfs_trans_commit(tp, 0);
  148. }
  149. /*
  150. * Schedule IO completion handling on the final put of an ioend.
  151. *
  152. * If there is no work to do we might as well call it a day and free the
  153. * ioend right now.
  154. */
  155. STATIC void
  156. xfs_finish_ioend(
  157. struct xfs_ioend *ioend)
  158. {
  159. if (atomic_dec_and_test(&ioend->io_remaining)) {
  160. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  161. if (ioend->io_type == IO_UNWRITTEN)
  162. queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
  163. else if (ioend->io_append_trans)
  164. queue_work(mp->m_data_workqueue, &ioend->io_work);
  165. else
  166. xfs_destroy_ioend(ioend);
  167. }
  168. }
  169. /*
  170. * IO write completion.
  171. */
  172. STATIC void
  173. xfs_end_io(
  174. struct work_struct *work)
  175. {
  176. xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
  177. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  178. int error = 0;
  179. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  180. ioend->io_error = -EIO;
  181. goto done;
  182. }
  183. if (ioend->io_error)
  184. goto done;
  185. /*
  186. * For unwritten extents we need to issue transactions to convert a
  187. * range to normal written extens after the data I/O has finished.
  188. */
  189. if (ioend->io_type == IO_UNWRITTEN) {
  190. /*
  191. * For buffered I/O we never preallocate a transaction when
  192. * doing the unwritten extent conversion, but for direct I/O
  193. * we do not know if we are converting an unwritten extent
  194. * or not at the point where we preallocate the transaction.
  195. */
  196. if (ioend->io_append_trans) {
  197. ASSERT(ioend->io_isdirect);
  198. current_set_flags_nested(
  199. &ioend->io_append_trans->t_pflags, PF_FSTRANS);
  200. xfs_trans_cancel(ioend->io_append_trans, 0);
  201. }
  202. error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
  203. ioend->io_size);
  204. if (error) {
  205. ioend->io_error = -error;
  206. goto done;
  207. }
  208. } else if (ioend->io_append_trans) {
  209. error = xfs_setfilesize(ioend);
  210. if (error)
  211. ioend->io_error = -error;
  212. } else {
  213. ASSERT(!xfs_ioend_is_append(ioend));
  214. }
  215. done:
  216. xfs_destroy_ioend(ioend);
  217. }
  218. /*
  219. * Call IO completion handling in caller context on the final put of an ioend.
  220. */
  221. STATIC void
  222. xfs_finish_ioend_sync(
  223. struct xfs_ioend *ioend)
  224. {
  225. if (atomic_dec_and_test(&ioend->io_remaining))
  226. xfs_end_io(&ioend->io_work);
  227. }
  228. /*
  229. * Allocate and initialise an IO completion structure.
  230. * We need to track unwritten extent write completion here initially.
  231. * We'll need to extend this for updating the ondisk inode size later
  232. * (vs. incore size).
  233. */
  234. STATIC xfs_ioend_t *
  235. xfs_alloc_ioend(
  236. struct inode *inode,
  237. unsigned int type)
  238. {
  239. xfs_ioend_t *ioend;
  240. ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
  241. /*
  242. * Set the count to 1 initially, which will prevent an I/O
  243. * completion callback from happening before we have started
  244. * all the I/O from calling the completion routine too early.
  245. */
  246. atomic_set(&ioend->io_remaining, 1);
  247. ioend->io_isasync = 0;
  248. ioend->io_isdirect = 0;
  249. ioend->io_error = 0;
  250. ioend->io_list = NULL;
  251. ioend->io_type = type;
  252. ioend->io_inode = inode;
  253. ioend->io_buffer_head = NULL;
  254. ioend->io_buffer_tail = NULL;
  255. ioend->io_offset = 0;
  256. ioend->io_size = 0;
  257. ioend->io_iocb = NULL;
  258. ioend->io_result = 0;
  259. ioend->io_append_trans = NULL;
  260. INIT_WORK(&ioend->io_work, xfs_end_io);
  261. return ioend;
  262. }
  263. STATIC int
  264. xfs_map_blocks(
  265. struct inode *inode,
  266. loff_t offset,
  267. struct xfs_bmbt_irec *imap,
  268. int type,
  269. int nonblocking)
  270. {
  271. struct xfs_inode *ip = XFS_I(inode);
  272. struct xfs_mount *mp = ip->i_mount;
  273. ssize_t count = 1 << inode->i_blkbits;
  274. xfs_fileoff_t offset_fsb, end_fsb;
  275. int error = 0;
  276. int bmapi_flags = XFS_BMAPI_ENTIRE;
  277. int nimaps = 1;
  278. if (XFS_FORCED_SHUTDOWN(mp))
  279. return -XFS_ERROR(EIO);
  280. if (type == IO_UNWRITTEN)
  281. bmapi_flags |= XFS_BMAPI_IGSTATE;
  282. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  283. if (nonblocking)
  284. return -XFS_ERROR(EAGAIN);
  285. xfs_ilock(ip, XFS_ILOCK_SHARED);
  286. }
  287. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  288. (ip->i_df.if_flags & XFS_IFEXTENTS));
  289. ASSERT(offset <= mp->m_maxioffset);
  290. if (offset + count > mp->m_maxioffset)
  291. count = mp->m_maxioffset - offset;
  292. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  293. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  294. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  295. imap, &nimaps, bmapi_flags);
  296. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  297. if (error)
  298. return -XFS_ERROR(error);
  299. if (type == IO_DELALLOC &&
  300. (!nimaps || isnullstartblock(imap->br_startblock))) {
  301. error = xfs_iomap_write_allocate(ip, offset, count, imap);
  302. if (!error)
  303. trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
  304. return -XFS_ERROR(error);
  305. }
  306. #ifdef DEBUG
  307. if (type == IO_UNWRITTEN) {
  308. ASSERT(nimaps);
  309. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  310. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  311. }
  312. #endif
  313. if (nimaps)
  314. trace_xfs_map_blocks_found(ip, offset, count, type, imap);
  315. return 0;
  316. }
  317. STATIC int
  318. xfs_imap_valid(
  319. struct inode *inode,
  320. struct xfs_bmbt_irec *imap,
  321. xfs_off_t offset)
  322. {
  323. offset >>= inode->i_blkbits;
  324. return offset >= imap->br_startoff &&
  325. offset < imap->br_startoff + imap->br_blockcount;
  326. }
  327. /*
  328. * BIO completion handler for buffered IO.
  329. */
  330. STATIC void
  331. xfs_end_bio(
  332. struct bio *bio,
  333. int error)
  334. {
  335. xfs_ioend_t *ioend = bio->bi_private;
  336. ASSERT(atomic_read(&bio->bi_cnt) >= 1);
  337. ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
  338. /* Toss bio and pass work off to an xfsdatad thread */
  339. bio->bi_private = NULL;
  340. bio->bi_end_io = NULL;
  341. bio_put(bio);
  342. xfs_finish_ioend(ioend);
  343. }
  344. STATIC void
  345. xfs_submit_ioend_bio(
  346. struct writeback_control *wbc,
  347. xfs_ioend_t *ioend,
  348. struct bio *bio)
  349. {
  350. atomic_inc(&ioend->io_remaining);
  351. bio->bi_private = ioend;
  352. bio->bi_end_io = xfs_end_bio;
  353. submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
  354. }
  355. STATIC struct bio *
  356. xfs_alloc_ioend_bio(
  357. struct buffer_head *bh)
  358. {
  359. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  360. struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
  361. ASSERT(bio->bi_private == NULL);
  362. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  363. bio->bi_bdev = bh->b_bdev;
  364. return bio;
  365. }
  366. STATIC void
  367. xfs_start_buffer_writeback(
  368. struct buffer_head *bh)
  369. {
  370. ASSERT(buffer_mapped(bh));
  371. ASSERT(buffer_locked(bh));
  372. ASSERT(!buffer_delay(bh));
  373. ASSERT(!buffer_unwritten(bh));
  374. mark_buffer_async_write(bh);
  375. set_buffer_uptodate(bh);
  376. clear_buffer_dirty(bh);
  377. }
  378. STATIC void
  379. xfs_start_page_writeback(
  380. struct page *page,
  381. int clear_dirty,
  382. int buffers)
  383. {
  384. ASSERT(PageLocked(page));
  385. ASSERT(!PageWriteback(page));
  386. if (clear_dirty)
  387. clear_page_dirty_for_io(page);
  388. set_page_writeback(page);
  389. unlock_page(page);
  390. /* If no buffers on the page are to be written, finish it here */
  391. if (!buffers)
  392. end_page_writeback(page);
  393. }
  394. static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  395. {
  396. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  397. }
  398. /*
  399. * Submit all of the bios for all of the ioends we have saved up, covering the
  400. * initial writepage page and also any probed pages.
  401. *
  402. * Because we may have multiple ioends spanning a page, we need to start
  403. * writeback on all the buffers before we submit them for I/O. If we mark the
  404. * buffers as we got, then we can end up with a page that only has buffers
  405. * marked async write and I/O complete on can occur before we mark the other
  406. * buffers async write.
  407. *
  408. * The end result of this is that we trip a bug in end_page_writeback() because
  409. * we call it twice for the one page as the code in end_buffer_async_write()
  410. * assumes that all buffers on the page are started at the same time.
  411. *
  412. * The fix is two passes across the ioend list - one to start writeback on the
  413. * buffer_heads, and then submit them for I/O on the second pass.
  414. */
  415. STATIC void
  416. xfs_submit_ioend(
  417. struct writeback_control *wbc,
  418. xfs_ioend_t *ioend)
  419. {
  420. xfs_ioend_t *head = ioend;
  421. xfs_ioend_t *next;
  422. struct buffer_head *bh;
  423. struct bio *bio;
  424. sector_t lastblock = 0;
  425. /* Pass 1 - start writeback */
  426. do {
  427. next = ioend->io_list;
  428. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
  429. xfs_start_buffer_writeback(bh);
  430. } while ((ioend = next) != NULL);
  431. /* Pass 2 - submit I/O */
  432. ioend = head;
  433. do {
  434. next = ioend->io_list;
  435. bio = NULL;
  436. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  437. if (!bio) {
  438. retry:
  439. bio = xfs_alloc_ioend_bio(bh);
  440. } else if (bh->b_blocknr != lastblock + 1) {
  441. xfs_submit_ioend_bio(wbc, ioend, bio);
  442. goto retry;
  443. }
  444. if (bio_add_buffer(bio, bh) != bh->b_size) {
  445. xfs_submit_ioend_bio(wbc, ioend, bio);
  446. goto retry;
  447. }
  448. lastblock = bh->b_blocknr;
  449. }
  450. if (bio)
  451. xfs_submit_ioend_bio(wbc, ioend, bio);
  452. xfs_finish_ioend(ioend);
  453. } while ((ioend = next) != NULL);
  454. }
  455. /*
  456. * Cancel submission of all buffer_heads so far in this endio.
  457. * Toss the endio too. Only ever called for the initial page
  458. * in a writepage request, so only ever one page.
  459. */
  460. STATIC void
  461. xfs_cancel_ioend(
  462. xfs_ioend_t *ioend)
  463. {
  464. xfs_ioend_t *next;
  465. struct buffer_head *bh, *next_bh;
  466. do {
  467. next = ioend->io_list;
  468. bh = ioend->io_buffer_head;
  469. do {
  470. next_bh = bh->b_private;
  471. clear_buffer_async_write(bh);
  472. unlock_buffer(bh);
  473. } while ((bh = next_bh) != NULL);
  474. mempool_free(ioend, xfs_ioend_pool);
  475. } while ((ioend = next) != NULL);
  476. }
  477. /*
  478. * Test to see if we've been building up a completion structure for
  479. * earlier buffers -- if so, we try to append to this ioend if we
  480. * can, otherwise we finish off any current ioend and start another.
  481. * Return true if we've finished the given ioend.
  482. */
  483. STATIC void
  484. xfs_add_to_ioend(
  485. struct inode *inode,
  486. struct buffer_head *bh,
  487. xfs_off_t offset,
  488. unsigned int type,
  489. xfs_ioend_t **result,
  490. int need_ioend)
  491. {
  492. xfs_ioend_t *ioend = *result;
  493. if (!ioend || need_ioend || type != ioend->io_type) {
  494. xfs_ioend_t *previous = *result;
  495. ioend = xfs_alloc_ioend(inode, type);
  496. ioend->io_offset = offset;
  497. ioend->io_buffer_head = bh;
  498. ioend->io_buffer_tail = bh;
  499. if (previous)
  500. previous->io_list = ioend;
  501. *result = ioend;
  502. } else {
  503. ioend->io_buffer_tail->b_private = bh;
  504. ioend->io_buffer_tail = bh;
  505. }
  506. bh->b_private = NULL;
  507. ioend->io_size += bh->b_size;
  508. }
  509. STATIC void
  510. xfs_map_buffer(
  511. struct inode *inode,
  512. struct buffer_head *bh,
  513. struct xfs_bmbt_irec *imap,
  514. xfs_off_t offset)
  515. {
  516. sector_t bn;
  517. struct xfs_mount *m = XFS_I(inode)->i_mount;
  518. xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
  519. xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
  520. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  521. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  522. bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
  523. ((offset - iomap_offset) >> inode->i_blkbits);
  524. ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
  525. bh->b_blocknr = bn;
  526. set_buffer_mapped(bh);
  527. }
  528. STATIC void
  529. xfs_map_at_offset(
  530. struct inode *inode,
  531. struct buffer_head *bh,
  532. struct xfs_bmbt_irec *imap,
  533. xfs_off_t offset)
  534. {
  535. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  536. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  537. xfs_map_buffer(inode, bh, imap, offset);
  538. set_buffer_mapped(bh);
  539. clear_buffer_delay(bh);
  540. clear_buffer_unwritten(bh);
  541. }
  542. /*
  543. * Test if a given page is suitable for writing as part of an unwritten
  544. * or delayed allocate extent.
  545. */
  546. STATIC int
  547. xfs_check_page_type(
  548. struct page *page,
  549. unsigned int type)
  550. {
  551. if (PageWriteback(page))
  552. return 0;
  553. if (page->mapping && page_has_buffers(page)) {
  554. struct buffer_head *bh, *head;
  555. int acceptable = 0;
  556. bh = head = page_buffers(page);
  557. do {
  558. if (buffer_unwritten(bh))
  559. acceptable += (type == IO_UNWRITTEN);
  560. else if (buffer_delay(bh))
  561. acceptable += (type == IO_DELALLOC);
  562. else if (buffer_dirty(bh) && buffer_mapped(bh))
  563. acceptable += (type == IO_OVERWRITE);
  564. else
  565. break;
  566. } while ((bh = bh->b_this_page) != head);
  567. if (acceptable)
  568. return 1;
  569. }
  570. return 0;
  571. }
  572. /*
  573. * Allocate & map buffers for page given the extent map. Write it out.
  574. * except for the original page of a writepage, this is called on
  575. * delalloc/unwritten pages only, for the original page it is possible
  576. * that the page has no mapping at all.
  577. */
  578. STATIC int
  579. xfs_convert_page(
  580. struct inode *inode,
  581. struct page *page,
  582. loff_t tindex,
  583. struct xfs_bmbt_irec *imap,
  584. xfs_ioend_t **ioendp,
  585. struct writeback_control *wbc)
  586. {
  587. struct buffer_head *bh, *head;
  588. xfs_off_t end_offset;
  589. unsigned long p_offset;
  590. unsigned int type;
  591. int len, page_dirty;
  592. int count = 0, done = 0, uptodate = 1;
  593. xfs_off_t offset = page_offset(page);
  594. if (page->index != tindex)
  595. goto fail;
  596. if (!trylock_page(page))
  597. goto fail;
  598. if (PageWriteback(page))
  599. goto fail_unlock_page;
  600. if (page->mapping != inode->i_mapping)
  601. goto fail_unlock_page;
  602. if (!xfs_check_page_type(page, (*ioendp)->io_type))
  603. goto fail_unlock_page;
  604. /*
  605. * page_dirty is initially a count of buffers on the page before
  606. * EOF and is decremented as we move each into a cleanable state.
  607. *
  608. * Derivation:
  609. *
  610. * End offset is the highest offset that this page should represent.
  611. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  612. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  613. * hence give us the correct page_dirty count. On any other page,
  614. * it will be zero and in that case we need page_dirty to be the
  615. * count of buffers on the page.
  616. */
  617. end_offset = min_t(unsigned long long,
  618. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  619. i_size_read(inode));
  620. len = 1 << inode->i_blkbits;
  621. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  622. PAGE_CACHE_SIZE);
  623. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  624. page_dirty = p_offset / len;
  625. bh = head = page_buffers(page);
  626. do {
  627. if (offset >= end_offset)
  628. break;
  629. if (!buffer_uptodate(bh))
  630. uptodate = 0;
  631. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  632. done = 1;
  633. continue;
  634. }
  635. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  636. buffer_mapped(bh)) {
  637. if (buffer_unwritten(bh))
  638. type = IO_UNWRITTEN;
  639. else if (buffer_delay(bh))
  640. type = IO_DELALLOC;
  641. else
  642. type = IO_OVERWRITE;
  643. if (!xfs_imap_valid(inode, imap, offset)) {
  644. done = 1;
  645. continue;
  646. }
  647. lock_buffer(bh);
  648. if (type != IO_OVERWRITE)
  649. xfs_map_at_offset(inode, bh, imap, offset);
  650. xfs_add_to_ioend(inode, bh, offset, type,
  651. ioendp, done);
  652. page_dirty--;
  653. count++;
  654. } else {
  655. done = 1;
  656. }
  657. } while (offset += len, (bh = bh->b_this_page) != head);
  658. if (uptodate && bh == head)
  659. SetPageUptodate(page);
  660. if (count) {
  661. if (--wbc->nr_to_write <= 0 &&
  662. wbc->sync_mode == WB_SYNC_NONE)
  663. done = 1;
  664. }
  665. xfs_start_page_writeback(page, !page_dirty, count);
  666. return done;
  667. fail_unlock_page:
  668. unlock_page(page);
  669. fail:
  670. return 1;
  671. }
  672. /*
  673. * Convert & write out a cluster of pages in the same extent as defined
  674. * by mp and following the start page.
  675. */
  676. STATIC void
  677. xfs_cluster_write(
  678. struct inode *inode,
  679. pgoff_t tindex,
  680. struct xfs_bmbt_irec *imap,
  681. xfs_ioend_t **ioendp,
  682. struct writeback_control *wbc,
  683. pgoff_t tlast)
  684. {
  685. struct pagevec pvec;
  686. int done = 0, i;
  687. pagevec_init(&pvec, 0);
  688. while (!done && tindex <= tlast) {
  689. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  690. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  691. break;
  692. for (i = 0; i < pagevec_count(&pvec); i++) {
  693. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  694. imap, ioendp, wbc);
  695. if (done)
  696. break;
  697. }
  698. pagevec_release(&pvec);
  699. cond_resched();
  700. }
  701. }
  702. STATIC void
  703. xfs_vm_invalidatepage(
  704. struct page *page,
  705. unsigned long offset)
  706. {
  707. trace_xfs_invalidatepage(page->mapping->host, page, offset);
  708. block_invalidatepage(page, offset);
  709. }
  710. /*
  711. * If the page has delalloc buffers on it, we need to punch them out before we
  712. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  713. * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
  714. * is done on that same region - the delalloc extent is returned when none is
  715. * supposed to be there.
  716. *
  717. * We prevent this by truncating away the delalloc regions on the page before
  718. * invalidating it. Because they are delalloc, we can do this without needing a
  719. * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
  720. * truncation without a transaction as there is no space left for block
  721. * reservation (typically why we see a ENOSPC in writeback).
  722. *
  723. * This is not a performance critical path, so for now just do the punching a
  724. * buffer head at a time.
  725. */
  726. STATIC void
  727. xfs_aops_discard_page(
  728. struct page *page)
  729. {
  730. struct inode *inode = page->mapping->host;
  731. struct xfs_inode *ip = XFS_I(inode);
  732. struct buffer_head *bh, *head;
  733. loff_t offset = page_offset(page);
  734. if (!xfs_check_page_type(page, IO_DELALLOC))
  735. goto out_invalidate;
  736. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  737. goto out_invalidate;
  738. xfs_alert(ip->i_mount,
  739. "page discard on page %p, inode 0x%llx, offset %llu.",
  740. page, ip->i_ino, offset);
  741. xfs_ilock(ip, XFS_ILOCK_EXCL);
  742. bh = head = page_buffers(page);
  743. do {
  744. int error;
  745. xfs_fileoff_t start_fsb;
  746. if (!buffer_delay(bh))
  747. goto next_buffer;
  748. start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  749. error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
  750. if (error) {
  751. /* something screwed, just bail */
  752. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  753. xfs_alert(ip->i_mount,
  754. "page discard unable to remove delalloc mapping.");
  755. }
  756. break;
  757. }
  758. next_buffer:
  759. offset += 1 << inode->i_blkbits;
  760. } while ((bh = bh->b_this_page) != head);
  761. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  762. out_invalidate:
  763. xfs_vm_invalidatepage(page, 0);
  764. return;
  765. }
  766. /*
  767. * Write out a dirty page.
  768. *
  769. * For delalloc space on the page we need to allocate space and flush it.
  770. * For unwritten space on the page we need to start the conversion to
  771. * regular allocated space.
  772. * For any other dirty buffer heads on the page we should flush them.
  773. */
  774. STATIC int
  775. xfs_vm_writepage(
  776. struct page *page,
  777. struct writeback_control *wbc)
  778. {
  779. struct inode *inode = page->mapping->host;
  780. struct buffer_head *bh, *head;
  781. struct xfs_bmbt_irec imap;
  782. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  783. loff_t offset;
  784. unsigned int type;
  785. __uint64_t end_offset;
  786. pgoff_t end_index, last_index;
  787. ssize_t len;
  788. int err, imap_valid = 0, uptodate = 1;
  789. int count = 0;
  790. int nonblocking = 0;
  791. trace_xfs_writepage(inode, page, 0);
  792. ASSERT(page_has_buffers(page));
  793. /*
  794. * Refuse to write the page out if we are called from reclaim context.
  795. *
  796. * This avoids stack overflows when called from deeply used stacks in
  797. * random callers for direct reclaim or memcg reclaim. We explicitly
  798. * allow reclaim from kswapd as the stack usage there is relatively low.
  799. *
  800. * This should never happen except in the case of a VM regression so
  801. * warn about it.
  802. */
  803. if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
  804. PF_MEMALLOC))
  805. goto redirty;
  806. /*
  807. * Given that we do not allow direct reclaim to call us, we should
  808. * never be called while in a filesystem transaction.
  809. */
  810. if (WARN_ON(current->flags & PF_FSTRANS))
  811. goto redirty;
  812. /* Is this page beyond the end of the file? */
  813. offset = i_size_read(inode);
  814. end_index = offset >> PAGE_CACHE_SHIFT;
  815. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  816. if (page->index >= end_index) {
  817. if ((page->index >= end_index + 1) ||
  818. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  819. unlock_page(page);
  820. return 0;
  821. }
  822. }
  823. end_offset = min_t(unsigned long long,
  824. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  825. offset);
  826. len = 1 << inode->i_blkbits;
  827. bh = head = page_buffers(page);
  828. offset = page_offset(page);
  829. type = IO_OVERWRITE;
  830. if (wbc->sync_mode == WB_SYNC_NONE)
  831. nonblocking = 1;
  832. do {
  833. int new_ioend = 0;
  834. if (offset >= end_offset)
  835. break;
  836. if (!buffer_uptodate(bh))
  837. uptodate = 0;
  838. /*
  839. * set_page_dirty dirties all buffers in a page, independent
  840. * of their state. The dirty state however is entirely
  841. * meaningless for holes (!mapped && uptodate), so skip
  842. * buffers covering holes here.
  843. */
  844. if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
  845. imap_valid = 0;
  846. continue;
  847. }
  848. if (buffer_unwritten(bh)) {
  849. if (type != IO_UNWRITTEN) {
  850. type = IO_UNWRITTEN;
  851. imap_valid = 0;
  852. }
  853. } else if (buffer_delay(bh)) {
  854. if (type != IO_DELALLOC) {
  855. type = IO_DELALLOC;
  856. imap_valid = 0;
  857. }
  858. } else if (buffer_uptodate(bh)) {
  859. if (type != IO_OVERWRITE) {
  860. type = IO_OVERWRITE;
  861. imap_valid = 0;
  862. }
  863. } else {
  864. if (PageUptodate(page)) {
  865. ASSERT(buffer_mapped(bh));
  866. imap_valid = 0;
  867. }
  868. continue;
  869. }
  870. if (imap_valid)
  871. imap_valid = xfs_imap_valid(inode, &imap, offset);
  872. if (!imap_valid) {
  873. /*
  874. * If we didn't have a valid mapping then we need to
  875. * put the new mapping into a separate ioend structure.
  876. * This ensures non-contiguous extents always have
  877. * separate ioends, which is particularly important
  878. * for unwritten extent conversion at I/O completion
  879. * time.
  880. */
  881. new_ioend = 1;
  882. err = xfs_map_blocks(inode, offset, &imap, type,
  883. nonblocking);
  884. if (err)
  885. goto error;
  886. imap_valid = xfs_imap_valid(inode, &imap, offset);
  887. }
  888. if (imap_valid) {
  889. lock_buffer(bh);
  890. if (type != IO_OVERWRITE)
  891. xfs_map_at_offset(inode, bh, &imap, offset);
  892. xfs_add_to_ioend(inode, bh, offset, type, &ioend,
  893. new_ioend);
  894. count++;
  895. }
  896. if (!iohead)
  897. iohead = ioend;
  898. } while (offset += len, ((bh = bh->b_this_page) != head));
  899. if (uptodate && bh == head)
  900. SetPageUptodate(page);
  901. xfs_start_page_writeback(page, 1, count);
  902. if (ioend && imap_valid) {
  903. xfs_off_t end_index;
  904. end_index = imap.br_startoff + imap.br_blockcount;
  905. /* to bytes */
  906. end_index <<= inode->i_blkbits;
  907. /* to pages */
  908. end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
  909. /* check against file size */
  910. if (end_index > last_index)
  911. end_index = last_index;
  912. xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
  913. wbc, end_index);
  914. }
  915. if (iohead) {
  916. /*
  917. * Reserve log space if we might write beyond the on-disk
  918. * inode size.
  919. */
  920. if (ioend->io_type != IO_UNWRITTEN &&
  921. xfs_ioend_is_append(ioend)) {
  922. err = xfs_setfilesize_trans_alloc(ioend);
  923. if (err)
  924. goto error;
  925. }
  926. xfs_submit_ioend(wbc, iohead);
  927. }
  928. return 0;
  929. error:
  930. if (iohead)
  931. xfs_cancel_ioend(iohead);
  932. if (err == -EAGAIN)
  933. goto redirty;
  934. xfs_aops_discard_page(page);
  935. ClearPageUptodate(page);
  936. unlock_page(page);
  937. return err;
  938. redirty:
  939. redirty_page_for_writepage(wbc, page);
  940. unlock_page(page);
  941. return 0;
  942. }
  943. STATIC int
  944. xfs_vm_writepages(
  945. struct address_space *mapping,
  946. struct writeback_control *wbc)
  947. {
  948. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  949. return generic_writepages(mapping, wbc);
  950. }
  951. /*
  952. * Called to move a page into cleanable state - and from there
  953. * to be released. The page should already be clean. We always
  954. * have buffer heads in this call.
  955. *
  956. * Returns 1 if the page is ok to release, 0 otherwise.
  957. */
  958. STATIC int
  959. xfs_vm_releasepage(
  960. struct page *page,
  961. gfp_t gfp_mask)
  962. {
  963. int delalloc, unwritten;
  964. trace_xfs_releasepage(page->mapping->host, page, 0);
  965. xfs_count_page_state(page, &delalloc, &unwritten);
  966. if (WARN_ON(delalloc))
  967. return 0;
  968. if (WARN_ON(unwritten))
  969. return 0;
  970. return try_to_free_buffers(page);
  971. }
  972. STATIC int
  973. __xfs_get_blocks(
  974. struct inode *inode,
  975. sector_t iblock,
  976. struct buffer_head *bh_result,
  977. int create,
  978. int direct)
  979. {
  980. struct xfs_inode *ip = XFS_I(inode);
  981. struct xfs_mount *mp = ip->i_mount;
  982. xfs_fileoff_t offset_fsb, end_fsb;
  983. int error = 0;
  984. int lockmode = 0;
  985. struct xfs_bmbt_irec imap;
  986. int nimaps = 1;
  987. xfs_off_t offset;
  988. ssize_t size;
  989. int new = 0;
  990. if (XFS_FORCED_SHUTDOWN(mp))
  991. return -XFS_ERROR(EIO);
  992. offset = (xfs_off_t)iblock << inode->i_blkbits;
  993. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  994. size = bh_result->b_size;
  995. if (!create && direct && offset >= i_size_read(inode))
  996. return 0;
  997. /*
  998. * Direct I/O is usually done on preallocated files, so try getting
  999. * a block mapping without an exclusive lock first. For buffered
  1000. * writes we already have the exclusive iolock anyway, so avoiding
  1001. * a lock roundtrip here by taking the ilock exclusive from the
  1002. * beginning is a useful micro optimization.
  1003. */
  1004. if (create && !direct) {
  1005. lockmode = XFS_ILOCK_EXCL;
  1006. xfs_ilock(ip, lockmode);
  1007. } else {
  1008. lockmode = xfs_ilock_map_shared(ip);
  1009. }
  1010. ASSERT(offset <= mp->m_maxioffset);
  1011. if (offset + size > mp->m_maxioffset)
  1012. size = mp->m_maxioffset - offset;
  1013. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
  1014. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1015. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  1016. &imap, &nimaps, XFS_BMAPI_ENTIRE);
  1017. if (error)
  1018. goto out_unlock;
  1019. if (create &&
  1020. (!nimaps ||
  1021. (imap.br_startblock == HOLESTARTBLOCK ||
  1022. imap.br_startblock == DELAYSTARTBLOCK))) {
  1023. if (direct || xfs_get_extsz_hint(ip)) {
  1024. /*
  1025. * Drop the ilock in preparation for starting the block
  1026. * allocation transaction. It will be retaken
  1027. * exclusively inside xfs_iomap_write_direct for the
  1028. * actual allocation.
  1029. */
  1030. xfs_iunlock(ip, lockmode);
  1031. error = xfs_iomap_write_direct(ip, offset, size,
  1032. &imap, nimaps);
  1033. if (error)
  1034. return -error;
  1035. new = 1;
  1036. } else {
  1037. /*
  1038. * Delalloc reservations do not require a transaction,
  1039. * we can go on without dropping the lock here. If we
  1040. * are allocating a new delalloc block, make sure that
  1041. * we set the new flag so that we mark the buffer new so
  1042. * that we know that it is newly allocated if the write
  1043. * fails.
  1044. */
  1045. if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
  1046. new = 1;
  1047. error = xfs_iomap_write_delay(ip, offset, size, &imap);
  1048. if (error)
  1049. goto out_unlock;
  1050. xfs_iunlock(ip, lockmode);
  1051. }
  1052. trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
  1053. } else if (nimaps) {
  1054. trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
  1055. xfs_iunlock(ip, lockmode);
  1056. } else {
  1057. trace_xfs_get_blocks_notfound(ip, offset, size);
  1058. goto out_unlock;
  1059. }
  1060. if (imap.br_startblock != HOLESTARTBLOCK &&
  1061. imap.br_startblock != DELAYSTARTBLOCK) {
  1062. /*
  1063. * For unwritten extents do not report a disk address on
  1064. * the read case (treat as if we're reading into a hole).
  1065. */
  1066. if (create || !ISUNWRITTEN(&imap))
  1067. xfs_map_buffer(inode, bh_result, &imap, offset);
  1068. if (create && ISUNWRITTEN(&imap)) {
  1069. if (direct)
  1070. bh_result->b_private = inode;
  1071. set_buffer_unwritten(bh_result);
  1072. }
  1073. }
  1074. /*
  1075. * If this is a realtime file, data may be on a different device.
  1076. * to that pointed to from the buffer_head b_bdev currently.
  1077. */
  1078. bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
  1079. /*
  1080. * If we previously allocated a block out beyond eof and we are now
  1081. * coming back to use it then we will need to flag it as new even if it
  1082. * has a disk address.
  1083. *
  1084. * With sub-block writes into unwritten extents we also need to mark
  1085. * the buffer as new so that the unwritten parts of the buffer gets
  1086. * correctly zeroed.
  1087. */
  1088. if (create &&
  1089. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1090. (offset >= i_size_read(inode)) ||
  1091. (new || ISUNWRITTEN(&imap))))
  1092. set_buffer_new(bh_result);
  1093. if (imap.br_startblock == DELAYSTARTBLOCK) {
  1094. BUG_ON(direct);
  1095. if (create) {
  1096. set_buffer_uptodate(bh_result);
  1097. set_buffer_mapped(bh_result);
  1098. set_buffer_delay(bh_result);
  1099. }
  1100. }
  1101. /*
  1102. * If this is O_DIRECT or the mpage code calling tell them how large
  1103. * the mapping is, so that we can avoid repeated get_blocks calls.
  1104. */
  1105. if (direct || size > (1 << inode->i_blkbits)) {
  1106. xfs_off_t mapping_size;
  1107. mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
  1108. mapping_size <<= inode->i_blkbits;
  1109. ASSERT(mapping_size > 0);
  1110. if (mapping_size > size)
  1111. mapping_size = size;
  1112. if (mapping_size > LONG_MAX)
  1113. mapping_size = LONG_MAX;
  1114. bh_result->b_size = mapping_size;
  1115. }
  1116. return 0;
  1117. out_unlock:
  1118. xfs_iunlock(ip, lockmode);
  1119. return -error;
  1120. }
  1121. int
  1122. xfs_get_blocks(
  1123. struct inode *inode,
  1124. sector_t iblock,
  1125. struct buffer_head *bh_result,
  1126. int create)
  1127. {
  1128. return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
  1129. }
  1130. STATIC int
  1131. xfs_get_blocks_direct(
  1132. struct inode *inode,
  1133. sector_t iblock,
  1134. struct buffer_head *bh_result,
  1135. int create)
  1136. {
  1137. return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
  1138. }
  1139. /*
  1140. * Complete a direct I/O write request.
  1141. *
  1142. * If the private argument is non-NULL __xfs_get_blocks signals us that we
  1143. * need to issue a transaction to convert the range from unwritten to written
  1144. * extents. In case this is regular synchronous I/O we just call xfs_end_io
  1145. * to do this and we are done. But in case this was a successful AIO
  1146. * request this handler is called from interrupt context, from which we
  1147. * can't start transactions. In that case offload the I/O completion to
  1148. * the workqueues we also use for buffered I/O completion.
  1149. */
  1150. STATIC void
  1151. xfs_end_io_direct_write(
  1152. struct kiocb *iocb,
  1153. loff_t offset,
  1154. ssize_t size,
  1155. void *private,
  1156. int ret,
  1157. bool is_async)
  1158. {
  1159. struct xfs_ioend *ioend = iocb->private;
  1160. /*
  1161. * While the generic direct I/O code updates the inode size, it does
  1162. * so only after the end_io handler is called, which means our
  1163. * end_io handler thinks the on-disk size is outside the in-core
  1164. * size. To prevent this just update it a little bit earlier here.
  1165. */
  1166. if (offset + size > i_size_read(ioend->io_inode))
  1167. i_size_write(ioend->io_inode, offset + size);
  1168. /*
  1169. * blockdev_direct_IO can return an error even after the I/O
  1170. * completion handler was called. Thus we need to protect
  1171. * against double-freeing.
  1172. */
  1173. iocb->private = NULL;
  1174. ioend->io_offset = offset;
  1175. ioend->io_size = size;
  1176. ioend->io_iocb = iocb;
  1177. ioend->io_result = ret;
  1178. if (private && size > 0)
  1179. ioend->io_type = IO_UNWRITTEN;
  1180. if (is_async) {
  1181. ioend->io_isasync = 1;
  1182. xfs_finish_ioend(ioend);
  1183. } else {
  1184. xfs_finish_ioend_sync(ioend);
  1185. }
  1186. }
  1187. STATIC ssize_t
  1188. xfs_vm_direct_IO(
  1189. int rw,
  1190. struct kiocb *iocb,
  1191. const struct iovec *iov,
  1192. loff_t offset,
  1193. unsigned long nr_segs)
  1194. {
  1195. struct inode *inode = iocb->ki_filp->f_mapping->host;
  1196. struct block_device *bdev = xfs_find_bdev_for_inode(inode);
  1197. struct xfs_ioend *ioend = NULL;
  1198. ssize_t ret;
  1199. if (rw & WRITE) {
  1200. size_t size = iov_length(iov, nr_segs);
  1201. /*
  1202. * We need to preallocate a transaction for a size update
  1203. * here. In the case that this write both updates the size
  1204. * and converts at least on unwritten extent we will cancel
  1205. * the still clean transaction after the I/O has finished.
  1206. */
  1207. iocb->private = ioend = xfs_alloc_ioend(inode, IO_DIRECT);
  1208. if (offset + size > XFS_I(inode)->i_d.di_size) {
  1209. ret = xfs_setfilesize_trans_alloc(ioend);
  1210. if (ret)
  1211. goto out_destroy_ioend;
  1212. ioend->io_isdirect = 1;
  1213. }
  1214. ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
  1215. offset, nr_segs,
  1216. xfs_get_blocks_direct,
  1217. xfs_end_io_direct_write, NULL, 0);
  1218. if (ret != -EIOCBQUEUED && iocb->private)
  1219. goto out_trans_cancel;
  1220. } else {
  1221. ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
  1222. offset, nr_segs,
  1223. xfs_get_blocks_direct,
  1224. NULL, NULL, 0);
  1225. }
  1226. return ret;
  1227. out_trans_cancel:
  1228. if (ioend->io_append_trans) {
  1229. current_set_flags_nested(&ioend->io_append_trans->t_pflags,
  1230. PF_FSTRANS);
  1231. xfs_trans_cancel(ioend->io_append_trans, 0);
  1232. }
  1233. out_destroy_ioend:
  1234. xfs_destroy_ioend(ioend);
  1235. return ret;
  1236. }
  1237. /*
  1238. * Punch out the delalloc blocks we have already allocated.
  1239. *
  1240. * Don't bother with xfs_setattr given that nothing can have made it to disk yet
  1241. * as the page is still locked at this point.
  1242. */
  1243. STATIC void
  1244. xfs_vm_kill_delalloc_range(
  1245. struct inode *inode,
  1246. loff_t start,
  1247. loff_t end)
  1248. {
  1249. struct xfs_inode *ip = XFS_I(inode);
  1250. xfs_fileoff_t start_fsb;
  1251. xfs_fileoff_t end_fsb;
  1252. int error;
  1253. start_fsb = XFS_B_TO_FSB(ip->i_mount, start);
  1254. end_fsb = XFS_B_TO_FSB(ip->i_mount, end);
  1255. if (end_fsb <= start_fsb)
  1256. return;
  1257. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1258. error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
  1259. end_fsb - start_fsb);
  1260. if (error) {
  1261. /* something screwed, just bail */
  1262. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1263. xfs_alert(ip->i_mount,
  1264. "xfs_vm_write_failed: unable to clean up ino %lld",
  1265. ip->i_ino);
  1266. }
  1267. }
  1268. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1269. }
  1270. STATIC void
  1271. xfs_vm_write_failed(
  1272. struct inode *inode,
  1273. struct page *page,
  1274. loff_t pos,
  1275. unsigned len)
  1276. {
  1277. loff_t block_offset = pos & PAGE_MASK;
  1278. loff_t block_start;
  1279. loff_t block_end;
  1280. loff_t from = pos & (PAGE_CACHE_SIZE - 1);
  1281. loff_t to = from + len;
  1282. struct buffer_head *bh, *head;
  1283. ASSERT(block_offset + from == pos);
  1284. head = page_buffers(page);
  1285. block_start = 0;
  1286. for (bh = head; bh != head || !block_start;
  1287. bh = bh->b_this_page, block_start = block_end,
  1288. block_offset += bh->b_size) {
  1289. block_end = block_start + bh->b_size;
  1290. /* skip buffers before the write */
  1291. if (block_end <= from)
  1292. continue;
  1293. /* if the buffer is after the write, we're done */
  1294. if (block_start >= to)
  1295. break;
  1296. if (!buffer_delay(bh))
  1297. continue;
  1298. if (!buffer_new(bh) && block_offset < i_size_read(inode))
  1299. continue;
  1300. xfs_vm_kill_delalloc_range(inode, block_offset,
  1301. block_offset + bh->b_size);
  1302. }
  1303. }
  1304. /*
  1305. * This used to call block_write_begin(), but it unlocks and releases the page
  1306. * on error, and we need that page to be able to punch stale delalloc blocks out
  1307. * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at
  1308. * the appropriate point.
  1309. */
  1310. STATIC int
  1311. xfs_vm_write_begin(
  1312. struct file *file,
  1313. struct address_space *mapping,
  1314. loff_t pos,
  1315. unsigned len,
  1316. unsigned flags,
  1317. struct page **pagep,
  1318. void **fsdata)
  1319. {
  1320. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1321. struct page *page;
  1322. int status;
  1323. ASSERT(len <= PAGE_CACHE_SIZE);
  1324. page = grab_cache_page_write_begin(mapping, index,
  1325. flags | AOP_FLAG_NOFS);
  1326. if (!page)
  1327. return -ENOMEM;
  1328. status = __block_write_begin(page, pos, len, xfs_get_blocks);
  1329. if (unlikely(status)) {
  1330. struct inode *inode = mapping->host;
  1331. xfs_vm_write_failed(inode, page, pos, len);
  1332. unlock_page(page);
  1333. if (pos + len > i_size_read(inode))
  1334. truncate_pagecache(inode, pos + len, i_size_read(inode));
  1335. page_cache_release(page);
  1336. page = NULL;
  1337. }
  1338. *pagep = page;
  1339. return status;
  1340. }
  1341. /*
  1342. * On failure, we only need to kill delalloc blocks beyond EOF because they
  1343. * will never be written. For blocks within EOF, generic_write_end() zeros them
  1344. * so they are safe to leave alone and be written with all the other valid data.
  1345. */
  1346. STATIC int
  1347. xfs_vm_write_end(
  1348. struct file *file,
  1349. struct address_space *mapping,
  1350. loff_t pos,
  1351. unsigned len,
  1352. unsigned copied,
  1353. struct page *page,
  1354. void *fsdata)
  1355. {
  1356. int ret;
  1357. ASSERT(len <= PAGE_CACHE_SIZE);
  1358. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  1359. if (unlikely(ret < len)) {
  1360. struct inode *inode = mapping->host;
  1361. size_t isize = i_size_read(inode);
  1362. loff_t to = pos + len;
  1363. if (to > isize) {
  1364. truncate_pagecache(inode, to, isize);
  1365. xfs_vm_kill_delalloc_range(inode, isize, to);
  1366. }
  1367. }
  1368. return ret;
  1369. }
  1370. STATIC sector_t
  1371. xfs_vm_bmap(
  1372. struct address_space *mapping,
  1373. sector_t block)
  1374. {
  1375. struct inode *inode = (struct inode *)mapping->host;
  1376. struct xfs_inode *ip = XFS_I(inode);
  1377. trace_xfs_vm_bmap(XFS_I(inode));
  1378. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1379. xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
  1380. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1381. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1382. }
  1383. STATIC int
  1384. xfs_vm_readpage(
  1385. struct file *unused,
  1386. struct page *page)
  1387. {
  1388. return mpage_readpage(page, xfs_get_blocks);
  1389. }
  1390. STATIC int
  1391. xfs_vm_readpages(
  1392. struct file *unused,
  1393. struct address_space *mapping,
  1394. struct list_head *pages,
  1395. unsigned nr_pages)
  1396. {
  1397. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1398. }
  1399. const struct address_space_operations xfs_address_space_operations = {
  1400. .readpage = xfs_vm_readpage,
  1401. .readpages = xfs_vm_readpages,
  1402. .writepage = xfs_vm_writepage,
  1403. .writepages = xfs_vm_writepages,
  1404. .releasepage = xfs_vm_releasepage,
  1405. .invalidatepage = xfs_vm_invalidatepage,
  1406. .write_begin = xfs_vm_write_begin,
  1407. .write_end = xfs_vm_write_end,
  1408. .bmap = xfs_vm_bmap,
  1409. .direct_IO = xfs_vm_direct_IO,
  1410. .migratepage = buffer_migrate_page,
  1411. .is_partially_uptodate = block_is_partially_uptodate,
  1412. .error_remove_page = generic_error_remove_page,
  1413. };