xfs_aops.c 41 KB

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