xfs_aops.c 39 KB

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