xfs_aops.c 36 KB

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