xfs_aops.c 37 KB

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