xfs_aops.c 43 KB

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