xfs_aops.c 42 KB

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