xfs_aops.c 36 KB

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