xfs_aops.c 38 KB

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