xfs_aops.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_inum.h"
  22. #include "xfs_sb.h"
  23. #include "xfs_ag.h"
  24. #include "xfs_dir2.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_dmapi.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dir2_sf.h"
  32. #include "xfs_attr_sf.h"
  33. #include "xfs_dinode.h"
  34. #include "xfs_inode.h"
  35. #include "xfs_alloc.h"
  36. #include "xfs_btree.h"
  37. #include "xfs_error.h"
  38. #include "xfs_rw.h"
  39. #include "xfs_iomap.h"
  40. #include "xfs_vnodeops.h"
  41. #include "xfs_trace.h"
  42. #include "xfs_bmap.h"
  43. #include <linux/gfp.h>
  44. #include <linux/mpage.h>
  45. #include <linux/pagevec.h>
  46. #include <linux/writeback.h>
  47. /*
  48. * Prime number of hash buckets since address is used as the key.
  49. */
  50. #define NVSYNC 37
  51. #define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC])
  52. static wait_queue_head_t xfs_ioend_wq[NVSYNC];
  53. void __init
  54. xfs_ioend_init(void)
  55. {
  56. int i;
  57. for (i = 0; i < NVSYNC; i++)
  58. init_waitqueue_head(&xfs_ioend_wq[i]);
  59. }
  60. void
  61. xfs_ioend_wait(
  62. xfs_inode_t *ip)
  63. {
  64. wait_queue_head_t *wq = to_ioend_wq(ip);
  65. wait_event(*wq, (atomic_read(&ip->i_iocount) == 0));
  66. }
  67. STATIC void
  68. xfs_ioend_wake(
  69. xfs_inode_t *ip)
  70. {
  71. if (atomic_dec_and_test(&ip->i_iocount))
  72. wake_up(to_ioend_wq(ip));
  73. }
  74. void
  75. xfs_count_page_state(
  76. struct page *page,
  77. int *delalloc,
  78. int *unmapped,
  79. int *unwritten)
  80. {
  81. struct buffer_head *bh, *head;
  82. *delalloc = *unmapped = *unwritten = 0;
  83. bh = head = page_buffers(page);
  84. do {
  85. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  86. (*unmapped) = 1;
  87. else if (buffer_unwritten(bh))
  88. (*unwritten) = 1;
  89. else if (buffer_delay(bh))
  90. (*delalloc) = 1;
  91. } while ((bh = bh->b_this_page) != head);
  92. }
  93. STATIC struct block_device *
  94. xfs_find_bdev_for_inode(
  95. struct inode *inode)
  96. {
  97. struct xfs_inode *ip = XFS_I(inode);
  98. struct xfs_mount *mp = ip->i_mount;
  99. if (XFS_IS_REALTIME_INODE(ip))
  100. return mp->m_rtdev_targp->bt_bdev;
  101. else
  102. return mp->m_ddev_targp->bt_bdev;
  103. }
  104. /*
  105. * We're now finished for good with this ioend structure.
  106. * Update the page state via the associated buffer_heads,
  107. * release holds on the inode and bio, and finally free
  108. * up memory. Do not use the ioend after this.
  109. */
  110. STATIC void
  111. xfs_destroy_ioend(
  112. xfs_ioend_t *ioend)
  113. {
  114. struct buffer_head *bh, *next;
  115. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  116. for (bh = ioend->io_buffer_head; bh; bh = next) {
  117. next = bh->b_private;
  118. bh->b_end_io(bh, !ioend->io_error);
  119. }
  120. /*
  121. * Volume managers supporting multiple paths can send back ENODEV
  122. * when the final path disappears. In this case continuing to fill
  123. * the page cache with dirty data which cannot be written out is
  124. * evil, so prevent that.
  125. */
  126. if (unlikely(ioend->io_error == -ENODEV)) {
  127. xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ,
  128. __FILE__, __LINE__);
  129. }
  130. xfs_ioend_wake(ip);
  131. mempool_free(ioend, xfs_ioend_pool);
  132. }
  133. /*
  134. * If the end of the current ioend is beyond the current EOF,
  135. * return the new EOF value, otherwise zero.
  136. */
  137. STATIC xfs_fsize_t
  138. xfs_ioend_new_eof(
  139. xfs_ioend_t *ioend)
  140. {
  141. xfs_inode_t *ip = XFS_I(ioend->io_inode);
  142. xfs_fsize_t isize;
  143. xfs_fsize_t bsize;
  144. bsize = ioend->io_offset + ioend->io_size;
  145. isize = MAX(ip->i_size, ip->i_new_size);
  146. isize = MIN(isize, bsize);
  147. return isize > ip->i_d.di_size ? isize : 0;
  148. }
  149. /*
  150. * Update on-disk file size now that data has been written to disk. The
  151. * current in-memory file size is i_size. If a write is beyond eof i_new_size
  152. * will be the intended file size until i_size is updated. If this write does
  153. * not extend all the way to the valid file size then restrict this update to
  154. * the end of the write.
  155. *
  156. * This function does not block as blocking on the inode lock in IO completion
  157. * can lead to IO completion order dependency deadlocks.. If it can't get the
  158. * inode ilock it will return EAGAIN. Callers must handle this.
  159. */
  160. STATIC int
  161. xfs_setfilesize(
  162. xfs_ioend_t *ioend)
  163. {
  164. xfs_inode_t *ip = XFS_I(ioend->io_inode);
  165. xfs_fsize_t isize;
  166. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
  167. ASSERT(ioend->io_type != IOMAP_READ);
  168. if (unlikely(ioend->io_error))
  169. return 0;
  170. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
  171. return EAGAIN;
  172. isize = xfs_ioend_new_eof(ioend);
  173. if (isize) {
  174. ip->i_d.di_size = isize;
  175. xfs_mark_inode_dirty(ip);
  176. }
  177. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  178. return 0;
  179. }
  180. /*
  181. * Schedule IO completion handling on a xfsdatad if this was
  182. * the final hold on this ioend. If we are asked to wait,
  183. * flush the workqueue.
  184. */
  185. STATIC void
  186. xfs_finish_ioend(
  187. xfs_ioend_t *ioend,
  188. int wait)
  189. {
  190. if (atomic_dec_and_test(&ioend->io_remaining)) {
  191. struct workqueue_struct *wq;
  192. wq = (ioend->io_type == IOMAP_UNWRITTEN) ?
  193. xfsconvertd_workqueue : xfsdatad_workqueue;
  194. queue_work(wq, &ioend->io_work);
  195. if (wait)
  196. flush_workqueue(wq);
  197. }
  198. }
  199. /*
  200. * IO write completion.
  201. */
  202. STATIC void
  203. xfs_end_io(
  204. struct work_struct *work)
  205. {
  206. xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
  207. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  208. int error = 0;
  209. /*
  210. * For unwritten extents we need to issue transactions to convert a
  211. * range to normal written extens after the data I/O has finished.
  212. */
  213. if (ioend->io_type == IOMAP_UNWRITTEN &&
  214. likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) {
  215. error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
  216. ioend->io_size);
  217. if (error)
  218. ioend->io_error = error;
  219. }
  220. /*
  221. * We might have to update the on-disk file size after extending
  222. * writes.
  223. */
  224. if (ioend->io_type != IOMAP_READ) {
  225. error = xfs_setfilesize(ioend);
  226. ASSERT(!error || error == EAGAIN);
  227. }
  228. /*
  229. * If we didn't complete processing of the ioend, requeue it to the
  230. * tail of the workqueue for another attempt later. Otherwise destroy
  231. * it.
  232. */
  233. if (error == EAGAIN) {
  234. atomic_inc(&ioend->io_remaining);
  235. xfs_finish_ioend(ioend, 0);
  236. /* ensure we don't spin on blocked ioends */
  237. delay(1);
  238. } else
  239. xfs_destroy_ioend(ioend);
  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_error = 0;
  261. ioend->io_list = NULL;
  262. ioend->io_type = type;
  263. ioend->io_inode = inode;
  264. ioend->io_buffer_head = NULL;
  265. ioend->io_buffer_tail = NULL;
  266. atomic_inc(&XFS_I(ioend->io_inode)->i_iocount);
  267. ioend->io_offset = 0;
  268. ioend->io_size = 0;
  269. INIT_WORK(&ioend->io_work, xfs_end_io);
  270. return ioend;
  271. }
  272. STATIC int
  273. xfs_map_blocks(
  274. struct inode *inode,
  275. loff_t offset,
  276. ssize_t count,
  277. struct xfs_bmbt_irec *imap,
  278. int flags)
  279. {
  280. int nmaps = 1;
  281. int new = 0;
  282. return -xfs_iomap(XFS_I(inode), offset, count, flags, imap, &nmaps, &new);
  283. }
  284. STATIC int
  285. xfs_iomap_valid(
  286. struct inode *inode,
  287. struct xfs_bmbt_irec *imap,
  288. loff_t offset)
  289. {
  290. struct xfs_mount *mp = XFS_I(inode)->i_mount;
  291. xfs_off_t iomap_offset = XFS_FSB_TO_B(mp, imap->br_startoff);
  292. xfs_off_t iomap_bsize = XFS_FSB_TO_B(mp, imap->br_blockcount);
  293. return offset >= iomap_offset &&
  294. offset < iomap_offset + iomap_bsize;
  295. }
  296. /*
  297. * BIO completion handler for buffered IO.
  298. */
  299. STATIC void
  300. xfs_end_bio(
  301. struct bio *bio,
  302. int error)
  303. {
  304. xfs_ioend_t *ioend = bio->bi_private;
  305. ASSERT(atomic_read(&bio->bi_cnt) >= 1);
  306. ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
  307. /* Toss bio and pass work off to an xfsdatad thread */
  308. bio->bi_private = NULL;
  309. bio->bi_end_io = NULL;
  310. bio_put(bio);
  311. xfs_finish_ioend(ioend, 0);
  312. }
  313. STATIC void
  314. xfs_submit_ioend_bio(
  315. struct writeback_control *wbc,
  316. xfs_ioend_t *ioend,
  317. struct bio *bio)
  318. {
  319. atomic_inc(&ioend->io_remaining);
  320. bio->bi_private = ioend;
  321. bio->bi_end_io = xfs_end_bio;
  322. /*
  323. * If the I/O is beyond EOF we mark the inode dirty immediately
  324. * but don't update the inode size until I/O completion.
  325. */
  326. if (xfs_ioend_new_eof(ioend))
  327. xfs_mark_inode_dirty(XFS_I(ioend->io_inode));
  328. submit_bio(wbc->sync_mode == WB_SYNC_ALL ?
  329. WRITE_SYNC_PLUG : WRITE, bio);
  330. ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
  331. bio_put(bio);
  332. }
  333. STATIC struct bio *
  334. xfs_alloc_ioend_bio(
  335. struct buffer_head *bh)
  336. {
  337. struct bio *bio;
  338. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  339. do {
  340. bio = bio_alloc(GFP_NOIO, nvecs);
  341. nvecs >>= 1;
  342. } while (!bio);
  343. ASSERT(bio->bi_private == NULL);
  344. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  345. bio->bi_bdev = bh->b_bdev;
  346. bio_get(bio);
  347. return bio;
  348. }
  349. STATIC void
  350. xfs_start_buffer_writeback(
  351. struct buffer_head *bh)
  352. {
  353. ASSERT(buffer_mapped(bh));
  354. ASSERT(buffer_locked(bh));
  355. ASSERT(!buffer_delay(bh));
  356. ASSERT(!buffer_unwritten(bh));
  357. mark_buffer_async_write(bh);
  358. set_buffer_uptodate(bh);
  359. clear_buffer_dirty(bh);
  360. }
  361. STATIC void
  362. xfs_start_page_writeback(
  363. struct page *page,
  364. int clear_dirty,
  365. int buffers)
  366. {
  367. ASSERT(PageLocked(page));
  368. ASSERT(!PageWriteback(page));
  369. if (clear_dirty)
  370. clear_page_dirty_for_io(page);
  371. set_page_writeback(page);
  372. unlock_page(page);
  373. /* If no buffers on the page are to be written, finish it here */
  374. if (!buffers)
  375. end_page_writeback(page);
  376. }
  377. static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  378. {
  379. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  380. }
  381. /*
  382. * Submit all of the bios for all of the ioends we have saved up, covering the
  383. * initial writepage page and also any probed pages.
  384. *
  385. * Because we may have multiple ioends spanning a page, we need to start
  386. * writeback on all the buffers before we submit them for I/O. If we mark the
  387. * buffers as we got, then we can end up with a page that only has buffers
  388. * marked async write and I/O complete on can occur before we mark the other
  389. * buffers async write.
  390. *
  391. * The end result of this is that we trip a bug in end_page_writeback() because
  392. * we call it twice for the one page as the code in end_buffer_async_write()
  393. * assumes that all buffers on the page are started at the same time.
  394. *
  395. * The fix is two passes across the ioend list - one to start writeback on the
  396. * buffer_heads, and then submit them for I/O on the second pass.
  397. */
  398. STATIC void
  399. xfs_submit_ioend(
  400. struct writeback_control *wbc,
  401. xfs_ioend_t *ioend)
  402. {
  403. xfs_ioend_t *head = ioend;
  404. xfs_ioend_t *next;
  405. struct buffer_head *bh;
  406. struct bio *bio;
  407. sector_t lastblock = 0;
  408. /* Pass 1 - start writeback */
  409. do {
  410. next = ioend->io_list;
  411. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  412. xfs_start_buffer_writeback(bh);
  413. }
  414. } while ((ioend = next) != NULL);
  415. /* Pass 2 - submit I/O */
  416. ioend = head;
  417. do {
  418. next = ioend->io_list;
  419. bio = NULL;
  420. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  421. if (!bio) {
  422. retry:
  423. bio = xfs_alloc_ioend_bio(bh);
  424. } else if (bh->b_blocknr != lastblock + 1) {
  425. xfs_submit_ioend_bio(wbc, ioend, bio);
  426. goto retry;
  427. }
  428. if (bio_add_buffer(bio, bh) != bh->b_size) {
  429. xfs_submit_ioend_bio(wbc, ioend, bio);
  430. goto retry;
  431. }
  432. lastblock = bh->b_blocknr;
  433. }
  434. if (bio)
  435. xfs_submit_ioend_bio(wbc, ioend, bio);
  436. xfs_finish_ioend(ioend, 0);
  437. } while ((ioend = next) != NULL);
  438. }
  439. /*
  440. * Cancel submission of all buffer_heads so far in this endio.
  441. * Toss the endio too. Only ever called for the initial page
  442. * in a writepage request, so only ever one page.
  443. */
  444. STATIC void
  445. xfs_cancel_ioend(
  446. xfs_ioend_t *ioend)
  447. {
  448. xfs_ioend_t *next;
  449. struct buffer_head *bh, *next_bh;
  450. do {
  451. next = ioend->io_list;
  452. bh = ioend->io_buffer_head;
  453. do {
  454. next_bh = bh->b_private;
  455. clear_buffer_async_write(bh);
  456. unlock_buffer(bh);
  457. } while ((bh = next_bh) != NULL);
  458. xfs_ioend_wake(XFS_I(ioend->io_inode));
  459. mempool_free(ioend, xfs_ioend_pool);
  460. } while ((ioend = next) != NULL);
  461. }
  462. /*
  463. * Test to see if we've been building up a completion structure for
  464. * earlier buffers -- if so, we try to append to this ioend if we
  465. * can, otherwise we finish off any current ioend and start another.
  466. * Return true if we've finished the given ioend.
  467. */
  468. STATIC void
  469. xfs_add_to_ioend(
  470. struct inode *inode,
  471. struct buffer_head *bh,
  472. xfs_off_t offset,
  473. unsigned int type,
  474. xfs_ioend_t **result,
  475. int need_ioend)
  476. {
  477. xfs_ioend_t *ioend = *result;
  478. if (!ioend || need_ioend || type != ioend->io_type) {
  479. xfs_ioend_t *previous = *result;
  480. ioend = xfs_alloc_ioend(inode, type);
  481. ioend->io_offset = offset;
  482. ioend->io_buffer_head = bh;
  483. ioend->io_buffer_tail = bh;
  484. if (previous)
  485. previous->io_list = ioend;
  486. *result = ioend;
  487. } else {
  488. ioend->io_buffer_tail->b_private = bh;
  489. ioend->io_buffer_tail = bh;
  490. }
  491. bh->b_private = NULL;
  492. ioend->io_size += bh->b_size;
  493. }
  494. STATIC void
  495. xfs_map_buffer(
  496. struct inode *inode,
  497. struct buffer_head *bh,
  498. struct xfs_bmbt_irec *imap,
  499. xfs_off_t offset)
  500. {
  501. sector_t bn;
  502. struct xfs_mount *m = XFS_I(inode)->i_mount;
  503. xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
  504. xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
  505. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  506. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  507. bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
  508. ((offset - iomap_offset) >> inode->i_blkbits);
  509. ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
  510. bh->b_blocknr = bn;
  511. set_buffer_mapped(bh);
  512. }
  513. STATIC void
  514. xfs_map_at_offset(
  515. struct inode *inode,
  516. struct buffer_head *bh,
  517. struct xfs_bmbt_irec *imap,
  518. xfs_off_t offset)
  519. {
  520. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  521. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  522. lock_buffer(bh);
  523. xfs_map_buffer(inode, bh, imap, offset);
  524. bh->b_bdev = xfs_find_bdev_for_inode(inode);
  525. set_buffer_mapped(bh);
  526. clear_buffer_delay(bh);
  527. clear_buffer_unwritten(bh);
  528. }
  529. /*
  530. * Look for a page at index that is suitable for clustering.
  531. */
  532. STATIC unsigned int
  533. xfs_probe_page(
  534. struct page *page,
  535. unsigned int pg_offset,
  536. int mapped)
  537. {
  538. int ret = 0;
  539. if (PageWriteback(page))
  540. return 0;
  541. if (page->mapping && PageDirty(page)) {
  542. if (page_has_buffers(page)) {
  543. struct buffer_head *bh, *head;
  544. bh = head = page_buffers(page);
  545. do {
  546. if (!buffer_uptodate(bh))
  547. break;
  548. if (mapped != buffer_mapped(bh))
  549. break;
  550. ret += bh->b_size;
  551. if (ret >= pg_offset)
  552. break;
  553. } while ((bh = bh->b_this_page) != head);
  554. } else
  555. ret = mapped ? 0 : PAGE_CACHE_SIZE;
  556. }
  557. return ret;
  558. }
  559. STATIC size_t
  560. xfs_probe_cluster(
  561. struct inode *inode,
  562. struct page *startpage,
  563. struct buffer_head *bh,
  564. struct buffer_head *head,
  565. int mapped)
  566. {
  567. struct pagevec pvec;
  568. pgoff_t tindex, tlast, tloff;
  569. size_t total = 0;
  570. int done = 0, i;
  571. /* First sum forwards in this page */
  572. do {
  573. if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
  574. return total;
  575. total += bh->b_size;
  576. } while ((bh = bh->b_this_page) != head);
  577. /* if we reached the end of the page, sum forwards in following pages */
  578. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  579. tindex = startpage->index + 1;
  580. /* Prune this back to avoid pathological behavior */
  581. tloff = min(tlast, startpage->index + 64);
  582. pagevec_init(&pvec, 0);
  583. while (!done && tindex <= tloff) {
  584. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  585. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  586. break;
  587. for (i = 0; i < pagevec_count(&pvec); i++) {
  588. struct page *page = pvec.pages[i];
  589. size_t pg_offset, pg_len = 0;
  590. if (tindex == tlast) {
  591. pg_offset =
  592. i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
  593. if (!pg_offset) {
  594. done = 1;
  595. break;
  596. }
  597. } else
  598. pg_offset = PAGE_CACHE_SIZE;
  599. if (page->index == tindex && trylock_page(page)) {
  600. pg_len = xfs_probe_page(page, pg_offset, mapped);
  601. unlock_page(page);
  602. }
  603. if (!pg_len) {
  604. done = 1;
  605. break;
  606. }
  607. total += pg_len;
  608. tindex++;
  609. }
  610. pagevec_release(&pvec);
  611. cond_resched();
  612. }
  613. return total;
  614. }
  615. /*
  616. * Test if a given page is suitable for writing as part of an unwritten
  617. * or delayed allocate extent.
  618. */
  619. STATIC int
  620. xfs_is_delayed_page(
  621. struct page *page,
  622. unsigned int type)
  623. {
  624. if (PageWriteback(page))
  625. return 0;
  626. if (page->mapping && page_has_buffers(page)) {
  627. struct buffer_head *bh, *head;
  628. int acceptable = 0;
  629. bh = head = page_buffers(page);
  630. do {
  631. if (buffer_unwritten(bh))
  632. acceptable = (type == IOMAP_UNWRITTEN);
  633. else if (buffer_delay(bh))
  634. acceptable = (type == IOMAP_DELAY);
  635. else if (buffer_dirty(bh) && buffer_mapped(bh))
  636. acceptable = (type == IOMAP_NEW);
  637. else
  638. break;
  639. } while ((bh = bh->b_this_page) != head);
  640. if (acceptable)
  641. return 1;
  642. }
  643. return 0;
  644. }
  645. /*
  646. * Allocate & map buffers for page given the extent map. Write it out.
  647. * except for the original page of a writepage, this is called on
  648. * delalloc/unwritten pages only, for the original page it is possible
  649. * that the page has no mapping at all.
  650. */
  651. STATIC int
  652. xfs_convert_page(
  653. struct inode *inode,
  654. struct page *page,
  655. loff_t tindex,
  656. struct xfs_bmbt_irec *imap,
  657. xfs_ioend_t **ioendp,
  658. struct writeback_control *wbc,
  659. int startio,
  660. int all_bh)
  661. {
  662. struct buffer_head *bh, *head;
  663. xfs_off_t end_offset;
  664. unsigned long p_offset;
  665. unsigned int type;
  666. int len, page_dirty;
  667. int count = 0, done = 0, uptodate = 1;
  668. xfs_off_t offset = page_offset(page);
  669. if (page->index != tindex)
  670. goto fail;
  671. if (!trylock_page(page))
  672. goto fail;
  673. if (PageWriteback(page))
  674. goto fail_unlock_page;
  675. if (page->mapping != inode->i_mapping)
  676. goto fail_unlock_page;
  677. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  678. goto fail_unlock_page;
  679. /*
  680. * page_dirty is initially a count of buffers on the page before
  681. * EOF and is decremented as we move each into a cleanable state.
  682. *
  683. * Derivation:
  684. *
  685. * End offset is the highest offset that this page should represent.
  686. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  687. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  688. * hence give us the correct page_dirty count. On any other page,
  689. * it will be zero and in that case we need page_dirty to be the
  690. * count of buffers on the page.
  691. */
  692. end_offset = min_t(unsigned long long,
  693. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  694. i_size_read(inode));
  695. len = 1 << inode->i_blkbits;
  696. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  697. PAGE_CACHE_SIZE);
  698. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  699. page_dirty = p_offset / len;
  700. bh = head = page_buffers(page);
  701. do {
  702. if (offset >= end_offset)
  703. break;
  704. if (!buffer_uptodate(bh))
  705. uptodate = 0;
  706. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  707. done = 1;
  708. continue;
  709. }
  710. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  711. if (buffer_unwritten(bh))
  712. type = IOMAP_UNWRITTEN;
  713. else
  714. type = IOMAP_DELAY;
  715. if (!xfs_iomap_valid(inode, imap, offset)) {
  716. done = 1;
  717. continue;
  718. }
  719. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  720. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  721. xfs_map_at_offset(inode, bh, imap, offset);
  722. if (startio) {
  723. xfs_add_to_ioend(inode, bh, offset,
  724. type, ioendp, done);
  725. } else {
  726. set_buffer_dirty(bh);
  727. unlock_buffer(bh);
  728. mark_buffer_dirty(bh);
  729. }
  730. page_dirty--;
  731. count++;
  732. } else {
  733. type = IOMAP_NEW;
  734. if (buffer_mapped(bh) && all_bh && startio) {
  735. lock_buffer(bh);
  736. xfs_add_to_ioend(inode, bh, offset,
  737. type, ioendp, done);
  738. count++;
  739. page_dirty--;
  740. } else {
  741. done = 1;
  742. }
  743. }
  744. } while (offset += len, (bh = bh->b_this_page) != head);
  745. if (uptodate && bh == head)
  746. SetPageUptodate(page);
  747. if (startio) {
  748. if (count) {
  749. wbc->nr_to_write--;
  750. if (wbc->nr_to_write <= 0)
  751. done = 1;
  752. }
  753. xfs_start_page_writeback(page, !page_dirty, count);
  754. }
  755. return done;
  756. fail_unlock_page:
  757. unlock_page(page);
  758. fail:
  759. return 1;
  760. }
  761. /*
  762. * Convert & write out a cluster of pages in the same extent as defined
  763. * by mp and following the start page.
  764. */
  765. STATIC void
  766. xfs_cluster_write(
  767. struct inode *inode,
  768. pgoff_t tindex,
  769. struct xfs_bmbt_irec *imap,
  770. xfs_ioend_t **ioendp,
  771. struct writeback_control *wbc,
  772. int startio,
  773. int all_bh,
  774. pgoff_t tlast)
  775. {
  776. struct pagevec pvec;
  777. int done = 0, i;
  778. pagevec_init(&pvec, 0);
  779. while (!done && tindex <= tlast) {
  780. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  781. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  782. break;
  783. for (i = 0; i < pagevec_count(&pvec); i++) {
  784. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  785. imap, ioendp, wbc, startio, all_bh);
  786. if (done)
  787. break;
  788. }
  789. pagevec_release(&pvec);
  790. cond_resched();
  791. }
  792. }
  793. STATIC void
  794. xfs_vm_invalidatepage(
  795. struct page *page,
  796. unsigned long offset)
  797. {
  798. trace_xfs_invalidatepage(page->mapping->host, page, offset);
  799. block_invalidatepage(page, offset);
  800. }
  801. /*
  802. * If the page has delalloc buffers on it, we need to punch them out before we
  803. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  804. * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
  805. * is done on that same region - the delalloc extent is returned when none is
  806. * supposed to be there.
  807. *
  808. * We prevent this by truncating away the delalloc regions on the page before
  809. * invalidating it. Because they are delalloc, we can do this without needing a
  810. * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
  811. * truncation without a transaction as there is no space left for block
  812. * reservation (typically why we see a ENOSPC in writeback).
  813. *
  814. * This is not a performance critical path, so for now just do the punching a
  815. * buffer head at a time.
  816. */
  817. STATIC void
  818. xfs_aops_discard_page(
  819. struct page *page)
  820. {
  821. struct inode *inode = page->mapping->host;
  822. struct xfs_inode *ip = XFS_I(inode);
  823. struct buffer_head *bh, *head;
  824. loff_t offset = page_offset(page);
  825. ssize_t len = 1 << inode->i_blkbits;
  826. if (!xfs_is_delayed_page(page, IOMAP_DELAY))
  827. goto out_invalidate;
  828. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  829. goto out_invalidate;
  830. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  831. "page discard on page %p, inode 0x%llx, offset %llu.",
  832. page, ip->i_ino, offset);
  833. xfs_ilock(ip, XFS_ILOCK_EXCL);
  834. bh = head = page_buffers(page);
  835. do {
  836. int done;
  837. xfs_fileoff_t offset_fsb;
  838. xfs_bmbt_irec_t imap;
  839. int nimaps = 1;
  840. int error;
  841. xfs_fsblock_t firstblock;
  842. xfs_bmap_free_t flist;
  843. if (!buffer_delay(bh))
  844. goto next_buffer;
  845. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  846. /*
  847. * Map the range first and check that it is a delalloc extent
  848. * before trying to unmap the range. Otherwise we will be
  849. * trying to remove a real extent (which requires a
  850. * transaction) or a hole, which is probably a bad idea...
  851. */
  852. error = xfs_bmapi(NULL, ip, offset_fsb, 1,
  853. XFS_BMAPI_ENTIRE, NULL, 0, &imap,
  854. &nimaps, NULL, NULL);
  855. if (error) {
  856. /* something screwed, just bail */
  857. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  858. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  859. "page discard failed delalloc mapping lookup.");
  860. }
  861. break;
  862. }
  863. if (!nimaps) {
  864. /* nothing there */
  865. goto next_buffer;
  866. }
  867. if (imap.br_startblock != DELAYSTARTBLOCK) {
  868. /* been converted, ignore */
  869. goto next_buffer;
  870. }
  871. WARN_ON(imap.br_blockcount == 0);
  872. /*
  873. * Note: while we initialise the firstblock/flist pair, they
  874. * should never be used because blocks should never be
  875. * allocated or freed for a delalloc extent and hence we need
  876. * don't cancel or finish them after the xfs_bunmapi() call.
  877. */
  878. xfs_bmap_init(&flist, &firstblock);
  879. error = xfs_bunmapi(NULL, ip, offset_fsb, 1, 0, 1, &firstblock,
  880. &flist, NULL, &done);
  881. ASSERT(!flist.xbf_count && !flist.xbf_first);
  882. if (error) {
  883. /* something screwed, just bail */
  884. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  885. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  886. "page discard unable to remove delalloc mapping.");
  887. }
  888. break;
  889. }
  890. next_buffer:
  891. offset += len;
  892. } while ((bh = bh->b_this_page) != head);
  893. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  894. out_invalidate:
  895. xfs_vm_invalidatepage(page, 0);
  896. return;
  897. }
  898. /*
  899. * Calling this without startio set means we are being asked to make a dirty
  900. * page ready for freeing it's buffers. When called with startio set then
  901. * we are coming from writepage.
  902. *
  903. * When called with startio set it is important that we write the WHOLE
  904. * page if possible.
  905. * The bh->b_state's cannot know if any of the blocks or which block for
  906. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  907. * only valid if the page itself isn't completely uptodate. Some layers
  908. * may clear the page dirty flag prior to calling write page, under the
  909. * assumption the entire page will be written out; by not writing out the
  910. * whole page the page can be reused before all valid dirty data is
  911. * written out. Note: in the case of a page that has been dirty'd by
  912. * mapwrite and but partially setup by block_prepare_write the
  913. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  914. * valid state, thus the whole page must be written out thing.
  915. */
  916. STATIC int
  917. xfs_page_state_convert(
  918. struct inode *inode,
  919. struct page *page,
  920. struct writeback_control *wbc,
  921. int startio,
  922. int unmapped) /* also implies page uptodate */
  923. {
  924. struct buffer_head *bh, *head;
  925. struct xfs_bmbt_irec imap;
  926. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  927. loff_t offset;
  928. unsigned long p_offset = 0;
  929. unsigned int type;
  930. __uint64_t end_offset;
  931. pgoff_t end_index, last_index, tlast;
  932. ssize_t size, len;
  933. int flags, err, iomap_valid = 0, uptodate = 1;
  934. int page_dirty, count = 0;
  935. int trylock = 0;
  936. int all_bh = unmapped;
  937. if (startio) {
  938. if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
  939. trylock |= BMAPI_TRYLOCK;
  940. }
  941. /* Is this page beyond the end of the file? */
  942. offset = i_size_read(inode);
  943. end_index = offset >> PAGE_CACHE_SHIFT;
  944. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  945. if (page->index >= end_index) {
  946. if ((page->index >= end_index + 1) ||
  947. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  948. if (startio)
  949. unlock_page(page);
  950. return 0;
  951. }
  952. }
  953. /*
  954. * page_dirty is initially a count of buffers on the page before
  955. * EOF and is decremented as we move each into a cleanable state.
  956. *
  957. * Derivation:
  958. *
  959. * End offset is the highest offset that this page should represent.
  960. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  961. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  962. * hence give us the correct page_dirty count. On any other page,
  963. * it will be zero and in that case we need page_dirty to be the
  964. * count of buffers on the page.
  965. */
  966. end_offset = min_t(unsigned long long,
  967. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  968. len = 1 << inode->i_blkbits;
  969. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  970. PAGE_CACHE_SIZE);
  971. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  972. page_dirty = p_offset / len;
  973. bh = head = page_buffers(page);
  974. offset = page_offset(page);
  975. flags = BMAPI_READ;
  976. type = IOMAP_NEW;
  977. /* TODO: cleanup count and page_dirty */
  978. do {
  979. if (offset >= end_offset)
  980. break;
  981. if (!buffer_uptodate(bh))
  982. uptodate = 0;
  983. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  984. /*
  985. * the iomap is actually still valid, but the ioend
  986. * isn't. shouldn't happen too often.
  987. */
  988. iomap_valid = 0;
  989. continue;
  990. }
  991. if (iomap_valid)
  992. iomap_valid = xfs_iomap_valid(inode, &imap, offset);
  993. /*
  994. * First case, map an unwritten extent and prepare for
  995. * extent state conversion transaction on completion.
  996. *
  997. * Second case, allocate space for a delalloc buffer.
  998. * We can return EAGAIN here in the release page case.
  999. *
  1000. * Third case, an unmapped buffer was found, and we are
  1001. * in a path where we need to write the whole page out.
  1002. */
  1003. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  1004. ((buffer_uptodate(bh) || PageUptodate(page)) &&
  1005. !buffer_mapped(bh) && (unmapped || startio))) {
  1006. int new_ioend = 0;
  1007. /*
  1008. * Make sure we don't use a read-only iomap
  1009. */
  1010. if (flags == BMAPI_READ)
  1011. iomap_valid = 0;
  1012. if (buffer_unwritten(bh)) {
  1013. type = IOMAP_UNWRITTEN;
  1014. flags = BMAPI_WRITE | BMAPI_IGNSTATE;
  1015. } else if (buffer_delay(bh)) {
  1016. type = IOMAP_DELAY;
  1017. flags = BMAPI_ALLOCATE | trylock;
  1018. } else {
  1019. type = IOMAP_NEW;
  1020. flags = BMAPI_WRITE | BMAPI_MMAP;
  1021. }
  1022. if (!iomap_valid) {
  1023. /*
  1024. * if we didn't have a valid mapping then we
  1025. * need to ensure that we put the new mapping
  1026. * in a new ioend structure. This needs to be
  1027. * done to ensure that the ioends correctly
  1028. * reflect the block mappings at io completion
  1029. * for unwritten extent conversion.
  1030. */
  1031. new_ioend = 1;
  1032. if (type == IOMAP_NEW) {
  1033. size = xfs_probe_cluster(inode,
  1034. page, bh, head, 0);
  1035. } else {
  1036. size = len;
  1037. }
  1038. err = xfs_map_blocks(inode, offset, size,
  1039. &imap, flags);
  1040. if (err)
  1041. goto error;
  1042. iomap_valid = xfs_iomap_valid(inode, &imap, offset);
  1043. }
  1044. if (iomap_valid) {
  1045. xfs_map_at_offset(inode, bh, &imap, offset);
  1046. if (startio) {
  1047. xfs_add_to_ioend(inode, bh, offset,
  1048. type, &ioend,
  1049. new_ioend);
  1050. } else {
  1051. set_buffer_dirty(bh);
  1052. unlock_buffer(bh);
  1053. mark_buffer_dirty(bh);
  1054. }
  1055. page_dirty--;
  1056. count++;
  1057. }
  1058. } else if (buffer_uptodate(bh) && startio) {
  1059. /*
  1060. * we got here because the buffer is already mapped.
  1061. * That means it must already have extents allocated
  1062. * underneath it. Map the extent by reading it.
  1063. */
  1064. if (!iomap_valid || flags != BMAPI_READ) {
  1065. flags = BMAPI_READ;
  1066. size = xfs_probe_cluster(inode, page, bh,
  1067. head, 1);
  1068. err = xfs_map_blocks(inode, offset, size,
  1069. &imap, flags);
  1070. if (err)
  1071. goto error;
  1072. iomap_valid = xfs_iomap_valid(inode, &imap, offset);
  1073. }
  1074. /*
  1075. * We set the type to IOMAP_NEW in case we are doing a
  1076. * small write at EOF that is extending the file but
  1077. * without needing an allocation. We need to update the
  1078. * file size on I/O completion in this case so it is
  1079. * the same case as having just allocated a new extent
  1080. * that we are writing into for the first time.
  1081. */
  1082. type = IOMAP_NEW;
  1083. if (trylock_buffer(bh)) {
  1084. ASSERT(buffer_mapped(bh));
  1085. if (iomap_valid)
  1086. all_bh = 1;
  1087. xfs_add_to_ioend(inode, bh, offset, type,
  1088. &ioend, !iomap_valid);
  1089. page_dirty--;
  1090. count++;
  1091. } else {
  1092. iomap_valid = 0;
  1093. }
  1094. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  1095. (unmapped || startio)) {
  1096. iomap_valid = 0;
  1097. }
  1098. if (!iohead)
  1099. iohead = ioend;
  1100. } while (offset += len, ((bh = bh->b_this_page) != head));
  1101. if (uptodate && bh == head)
  1102. SetPageUptodate(page);
  1103. if (startio)
  1104. xfs_start_page_writeback(page, 1, count);
  1105. if (ioend && iomap_valid) {
  1106. struct xfs_mount *m = XFS_I(inode)->i_mount;
  1107. xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap.br_startoff);
  1108. xfs_off_t iomap_bsize = XFS_FSB_TO_B(m, imap.br_blockcount);
  1109. offset = (iomap_offset + iomap_bsize - 1) >>
  1110. PAGE_CACHE_SHIFT;
  1111. tlast = min_t(pgoff_t, offset, last_index);
  1112. xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
  1113. wbc, startio, all_bh, tlast);
  1114. }
  1115. if (iohead)
  1116. xfs_submit_ioend(wbc, iohead);
  1117. return page_dirty;
  1118. error:
  1119. if (iohead)
  1120. xfs_cancel_ioend(iohead);
  1121. /*
  1122. * If it's delalloc and we have nowhere to put it,
  1123. * throw it away, unless the lower layers told
  1124. * us to try again.
  1125. */
  1126. if (err != -EAGAIN) {
  1127. if (!unmapped)
  1128. xfs_aops_discard_page(page);
  1129. ClearPageUptodate(page);
  1130. }
  1131. return err;
  1132. }
  1133. /*
  1134. * writepage: Called from one of two places:
  1135. *
  1136. * 1. we are flushing a delalloc buffer head.
  1137. *
  1138. * 2. we are writing out a dirty page. Typically the page dirty
  1139. * state is cleared before we get here. In this case is it
  1140. * conceivable we have no buffer heads.
  1141. *
  1142. * For delalloc space on the page we need to allocate space and
  1143. * flush it. For unmapped buffer heads on the page we should
  1144. * allocate space if the page is uptodate. For any other dirty
  1145. * buffer heads on the page we should flush them.
  1146. *
  1147. * If we detect that a transaction would be required to flush
  1148. * the page, we have to check the process flags first, if we
  1149. * are already in a transaction or disk I/O during allocations
  1150. * is off, we need to fail the writepage and redirty the page.
  1151. */
  1152. STATIC int
  1153. xfs_vm_writepage(
  1154. struct page *page,
  1155. struct writeback_control *wbc)
  1156. {
  1157. int error;
  1158. int need_trans;
  1159. int delalloc, unmapped, unwritten;
  1160. struct inode *inode = page->mapping->host;
  1161. trace_xfs_writepage(inode, page, 0);
  1162. /*
  1163. * We need a transaction if:
  1164. * 1. There are delalloc buffers on the page
  1165. * 2. The page is uptodate and we have unmapped buffers
  1166. * 3. The page is uptodate and we have no buffers
  1167. * 4. There are unwritten buffers on the page
  1168. */
  1169. if (!page_has_buffers(page)) {
  1170. unmapped = 1;
  1171. need_trans = 1;
  1172. } else {
  1173. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1174. if (!PageUptodate(page))
  1175. unmapped = 0;
  1176. need_trans = delalloc + unmapped + unwritten;
  1177. }
  1178. /*
  1179. * If we need a transaction and the process flags say
  1180. * we are already in a transaction, or no IO is allowed
  1181. * then mark the page dirty again and leave the page
  1182. * as is.
  1183. */
  1184. if (current_test_flags(PF_FSTRANS) && need_trans)
  1185. goto out_fail;
  1186. /*
  1187. * Delay hooking up buffer heads until we have
  1188. * made our go/no-go decision.
  1189. */
  1190. if (!page_has_buffers(page))
  1191. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1192. /*
  1193. * VM calculation for nr_to_write seems off. Bump it way
  1194. * up, this gets simple streaming writes zippy again.
  1195. * To be reviewed again after Jens' writeback changes.
  1196. */
  1197. wbc->nr_to_write *= 4;
  1198. /*
  1199. * Convert delayed allocate, unwritten or unmapped space
  1200. * to real space and flush out to disk.
  1201. */
  1202. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1203. if (error == -EAGAIN)
  1204. goto out_fail;
  1205. if (unlikely(error < 0))
  1206. goto out_unlock;
  1207. return 0;
  1208. out_fail:
  1209. redirty_page_for_writepage(wbc, page);
  1210. unlock_page(page);
  1211. return 0;
  1212. out_unlock:
  1213. unlock_page(page);
  1214. return error;
  1215. }
  1216. STATIC int
  1217. xfs_vm_writepages(
  1218. struct address_space *mapping,
  1219. struct writeback_control *wbc)
  1220. {
  1221. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  1222. return generic_writepages(mapping, wbc);
  1223. }
  1224. /*
  1225. * Called to move a page into cleanable state - and from there
  1226. * to be released. Possibly the page is already clean. We always
  1227. * have buffer heads in this call.
  1228. *
  1229. * Returns 0 if the page is ok to release, 1 otherwise.
  1230. *
  1231. * Possible scenarios are:
  1232. *
  1233. * 1. We are being called to release a page which has been written
  1234. * to via regular I/O. buffer heads will be dirty and possibly
  1235. * delalloc. If no delalloc buffer heads in this case then we
  1236. * can just return zero.
  1237. *
  1238. * 2. We are called to release a page which has been written via
  1239. * mmap, all we need to do is ensure there is no delalloc
  1240. * state in the buffer heads, if not we can let the caller
  1241. * free them and we should come back later via writepage.
  1242. */
  1243. STATIC int
  1244. xfs_vm_releasepage(
  1245. struct page *page,
  1246. gfp_t gfp_mask)
  1247. {
  1248. struct inode *inode = page->mapping->host;
  1249. int dirty, delalloc, unmapped, unwritten;
  1250. struct writeback_control wbc = {
  1251. .sync_mode = WB_SYNC_ALL,
  1252. .nr_to_write = 1,
  1253. };
  1254. trace_xfs_releasepage(inode, page, 0);
  1255. if (!page_has_buffers(page))
  1256. return 0;
  1257. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1258. if (!delalloc && !unwritten)
  1259. goto free_buffers;
  1260. if (!(gfp_mask & __GFP_FS))
  1261. return 0;
  1262. /* If we are already inside a transaction or the thread cannot
  1263. * do I/O, we cannot release this page.
  1264. */
  1265. if (current_test_flags(PF_FSTRANS))
  1266. return 0;
  1267. /*
  1268. * Convert delalloc space to real space, do not flush the
  1269. * data out to disk, that will be done by the caller.
  1270. * Never need to allocate space here - we will always
  1271. * come back to writepage in that case.
  1272. */
  1273. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1274. if (dirty == 0 && !unwritten)
  1275. goto free_buffers;
  1276. return 0;
  1277. free_buffers:
  1278. return try_to_free_buffers(page);
  1279. }
  1280. STATIC int
  1281. __xfs_get_blocks(
  1282. struct inode *inode,
  1283. sector_t iblock,
  1284. struct buffer_head *bh_result,
  1285. int create,
  1286. int direct,
  1287. bmapi_flags_t flags)
  1288. {
  1289. struct xfs_bmbt_irec imap;
  1290. xfs_off_t offset;
  1291. ssize_t size;
  1292. int nimap = 1;
  1293. int new = 0;
  1294. int error;
  1295. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1296. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  1297. size = bh_result->b_size;
  1298. if (!create && direct && offset >= i_size_read(inode))
  1299. return 0;
  1300. error = xfs_iomap(XFS_I(inode), offset, size,
  1301. create ? flags : BMAPI_READ, &imap, &nimap, &new);
  1302. if (error)
  1303. return -error;
  1304. if (nimap == 0)
  1305. return 0;
  1306. if (imap.br_startblock != HOLESTARTBLOCK &&
  1307. imap.br_startblock != DELAYSTARTBLOCK) {
  1308. /*
  1309. * For unwritten extents do not report a disk address on
  1310. * the read case (treat as if we're reading into a hole).
  1311. */
  1312. if (create || !ISUNWRITTEN(&imap))
  1313. xfs_map_buffer(inode, bh_result, &imap, offset);
  1314. if (create && ISUNWRITTEN(&imap)) {
  1315. if (direct)
  1316. bh_result->b_private = inode;
  1317. set_buffer_unwritten(bh_result);
  1318. }
  1319. }
  1320. /*
  1321. * If this is a realtime file, data may be on a different device.
  1322. * to that pointed to from the buffer_head b_bdev currently.
  1323. */
  1324. bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
  1325. /*
  1326. * If we previously allocated a block out beyond eof and we are now
  1327. * coming back to use it then we will need to flag it as new even if it
  1328. * has a disk address.
  1329. *
  1330. * With sub-block writes into unwritten extents we also need to mark
  1331. * the buffer as new so that the unwritten parts of the buffer gets
  1332. * correctly zeroed.
  1333. */
  1334. if (create &&
  1335. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1336. (offset >= i_size_read(inode)) ||
  1337. (new || ISUNWRITTEN(&imap))))
  1338. set_buffer_new(bh_result);
  1339. if (imap.br_startblock == DELAYSTARTBLOCK) {
  1340. BUG_ON(direct);
  1341. if (create) {
  1342. set_buffer_uptodate(bh_result);
  1343. set_buffer_mapped(bh_result);
  1344. set_buffer_delay(bh_result);
  1345. }
  1346. }
  1347. if (direct || size > (1 << inode->i_blkbits)) {
  1348. struct xfs_mount *mp = XFS_I(inode)->i_mount;
  1349. xfs_off_t iomap_offset = XFS_FSB_TO_B(mp, imap.br_startoff);
  1350. xfs_off_t iomap_delta = offset - iomap_offset;
  1351. xfs_off_t iomap_bsize = XFS_FSB_TO_B(mp, imap.br_blockcount);
  1352. ASSERT(iomap_bsize - iomap_delta > 0);
  1353. offset = min_t(xfs_off_t,
  1354. iomap_bsize - iomap_delta, size);
  1355. bh_result->b_size = (ssize_t)min_t(xfs_off_t, LONG_MAX, offset);
  1356. }
  1357. return 0;
  1358. }
  1359. int
  1360. xfs_get_blocks(
  1361. struct inode *inode,
  1362. sector_t iblock,
  1363. struct buffer_head *bh_result,
  1364. int create)
  1365. {
  1366. return __xfs_get_blocks(inode, iblock,
  1367. bh_result, create, 0, BMAPI_WRITE);
  1368. }
  1369. STATIC int
  1370. xfs_get_blocks_direct(
  1371. struct inode *inode,
  1372. sector_t iblock,
  1373. struct buffer_head *bh_result,
  1374. int create)
  1375. {
  1376. return __xfs_get_blocks(inode, iblock,
  1377. bh_result, create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  1378. }
  1379. STATIC void
  1380. xfs_end_io_direct(
  1381. struct kiocb *iocb,
  1382. loff_t offset,
  1383. ssize_t size,
  1384. void *private)
  1385. {
  1386. xfs_ioend_t *ioend = iocb->private;
  1387. /*
  1388. * Non-NULL private data means we need to issue a transaction to
  1389. * convert a range from unwritten to written extents. This needs
  1390. * to happen from process context but aio+dio I/O completion
  1391. * happens from irq context so we need to defer it to a workqueue.
  1392. * This is not necessary for synchronous direct I/O, but we do
  1393. * it anyway to keep the code uniform and simpler.
  1394. *
  1395. * Well, if only it were that simple. Because synchronous direct I/O
  1396. * requires extent conversion to occur *before* we return to userspace,
  1397. * we have to wait for extent conversion to complete. Look at the
  1398. * iocb that has been passed to us to determine if this is AIO or
  1399. * not. If it is synchronous, tell xfs_finish_ioend() to kick the
  1400. * workqueue and wait for it to complete.
  1401. *
  1402. * The core direct I/O code might be changed to always call the
  1403. * completion handler in the future, in which case all this can
  1404. * go away.
  1405. */
  1406. ioend->io_offset = offset;
  1407. ioend->io_size = size;
  1408. if (ioend->io_type == IOMAP_READ) {
  1409. xfs_finish_ioend(ioend, 0);
  1410. } else if (private && size > 0) {
  1411. xfs_finish_ioend(ioend, is_sync_kiocb(iocb));
  1412. } else {
  1413. /*
  1414. * A direct I/O write ioend starts it's life in unwritten
  1415. * state in case they map an unwritten extent. This write
  1416. * didn't map an unwritten extent so switch it's completion
  1417. * handler.
  1418. */
  1419. ioend->io_type = IOMAP_NEW;
  1420. xfs_finish_ioend(ioend, 0);
  1421. }
  1422. /*
  1423. * blockdev_direct_IO can return an error even after the I/O
  1424. * completion handler was called. Thus we need to protect
  1425. * against double-freeing.
  1426. */
  1427. iocb->private = NULL;
  1428. }
  1429. STATIC ssize_t
  1430. xfs_vm_direct_IO(
  1431. int rw,
  1432. struct kiocb *iocb,
  1433. const struct iovec *iov,
  1434. loff_t offset,
  1435. unsigned long nr_segs)
  1436. {
  1437. struct file *file = iocb->ki_filp;
  1438. struct inode *inode = file->f_mapping->host;
  1439. struct block_device *bdev;
  1440. ssize_t ret;
  1441. bdev = xfs_find_bdev_for_inode(inode);
  1442. iocb->private = xfs_alloc_ioend(inode, rw == WRITE ?
  1443. IOMAP_UNWRITTEN : IOMAP_READ);
  1444. ret = blockdev_direct_IO_no_locking(rw, iocb, inode, bdev, iov,
  1445. offset, nr_segs,
  1446. xfs_get_blocks_direct,
  1447. xfs_end_io_direct);
  1448. if (unlikely(ret != -EIOCBQUEUED && iocb->private))
  1449. xfs_destroy_ioend(iocb->private);
  1450. return ret;
  1451. }
  1452. STATIC int
  1453. xfs_vm_write_begin(
  1454. struct file *file,
  1455. struct address_space *mapping,
  1456. loff_t pos,
  1457. unsigned len,
  1458. unsigned flags,
  1459. struct page **pagep,
  1460. void **fsdata)
  1461. {
  1462. *pagep = NULL;
  1463. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  1464. xfs_get_blocks);
  1465. }
  1466. STATIC sector_t
  1467. xfs_vm_bmap(
  1468. struct address_space *mapping,
  1469. sector_t block)
  1470. {
  1471. struct inode *inode = (struct inode *)mapping->host;
  1472. struct xfs_inode *ip = XFS_I(inode);
  1473. xfs_itrace_entry(XFS_I(inode));
  1474. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1475. xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
  1476. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1477. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1478. }
  1479. STATIC int
  1480. xfs_vm_readpage(
  1481. struct file *unused,
  1482. struct page *page)
  1483. {
  1484. return mpage_readpage(page, xfs_get_blocks);
  1485. }
  1486. STATIC int
  1487. xfs_vm_readpages(
  1488. struct file *unused,
  1489. struct address_space *mapping,
  1490. struct list_head *pages,
  1491. unsigned nr_pages)
  1492. {
  1493. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1494. }
  1495. const struct address_space_operations xfs_address_space_operations = {
  1496. .readpage = xfs_vm_readpage,
  1497. .readpages = xfs_vm_readpages,
  1498. .writepage = xfs_vm_writepage,
  1499. .writepages = xfs_vm_writepages,
  1500. .sync_page = block_sync_page,
  1501. .releasepage = xfs_vm_releasepage,
  1502. .invalidatepage = xfs_vm_invalidatepage,
  1503. .write_begin = xfs_vm_write_begin,
  1504. .write_end = generic_write_end,
  1505. .bmap = xfs_vm_bmap,
  1506. .direct_IO = xfs_vm_direct_IO,
  1507. .migratepage = buffer_migrate_page,
  1508. .is_partially_uptodate = block_is_partially_uptodate,
  1509. .error_remove_page = generic_error_remove_page,
  1510. };