xfs_aops.c 35 KB

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