xfs_aops.c 35 KB

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