xfs_aops.c 42 KB

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