xfs_aops.c 38 KB

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