xfs_aops.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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. * buffer_heads, and then submit them for I/O on the second pass.
  335. */
  336. STATIC void
  337. xfs_submit_ioend(
  338. xfs_ioend_t *ioend)
  339. {
  340. xfs_ioend_t *head = ioend;
  341. xfs_ioend_t *next;
  342. struct buffer_head *bh;
  343. struct bio *bio;
  344. sector_t lastblock = 0;
  345. /* Pass 1 - start writeback */
  346. do {
  347. next = ioend->io_list;
  348. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  349. xfs_start_buffer_writeback(bh);
  350. }
  351. } while ((ioend = next) != NULL);
  352. /* Pass 2 - submit I/O */
  353. ioend = head;
  354. do {
  355. next = ioend->io_list;
  356. bio = NULL;
  357. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  358. if (!bio) {
  359. retry:
  360. bio = xfs_alloc_ioend_bio(bh);
  361. } else if (bh->b_blocknr != lastblock + 1) {
  362. xfs_submit_ioend_bio(ioend, bio);
  363. goto retry;
  364. }
  365. if (bio_add_buffer(bio, bh) != bh->b_size) {
  366. xfs_submit_ioend_bio(ioend, bio);
  367. goto retry;
  368. }
  369. lastblock = bh->b_blocknr;
  370. }
  371. if (bio)
  372. xfs_submit_ioend_bio(ioend, bio);
  373. xfs_finish_ioend(ioend);
  374. } while ((ioend = next) != NULL);
  375. }
  376. /*
  377. * Cancel submission of all buffer_heads so far in this endio.
  378. * Toss the endio too. Only ever called for the initial page
  379. * in a writepage request, so only ever one page.
  380. */
  381. STATIC void
  382. xfs_cancel_ioend(
  383. xfs_ioend_t *ioend)
  384. {
  385. xfs_ioend_t *next;
  386. struct buffer_head *bh, *next_bh;
  387. do {
  388. next = ioend->io_list;
  389. bh = ioend->io_buffer_head;
  390. do {
  391. next_bh = bh->b_private;
  392. clear_buffer_async_write(bh);
  393. unlock_buffer(bh);
  394. } while ((bh = next_bh) != NULL);
  395. vn_iowake(ioend->io_vnode);
  396. mempool_free(ioend, xfs_ioend_pool);
  397. } while ((ioend = next) != NULL);
  398. }
  399. /*
  400. * Test to see if we've been building up a completion structure for
  401. * earlier buffers -- if so, we try to append to this ioend if we
  402. * can, otherwise we finish off any current ioend and start another.
  403. * Return true if we've finished the given ioend.
  404. */
  405. STATIC void
  406. xfs_add_to_ioend(
  407. struct inode *inode,
  408. struct buffer_head *bh,
  409. xfs_off_t offset,
  410. unsigned int type,
  411. xfs_ioend_t **result,
  412. int need_ioend)
  413. {
  414. xfs_ioend_t *ioend = *result;
  415. if (!ioend || need_ioend || type != ioend->io_type) {
  416. xfs_ioend_t *previous = *result;
  417. ioend = xfs_alloc_ioend(inode, type);
  418. ioend->io_offset = offset;
  419. ioend->io_buffer_head = bh;
  420. ioend->io_buffer_tail = bh;
  421. if (previous)
  422. previous->io_list = ioend;
  423. *result = ioend;
  424. } else {
  425. ioend->io_buffer_tail->b_private = bh;
  426. ioend->io_buffer_tail = bh;
  427. }
  428. bh->b_private = NULL;
  429. ioend->io_size += bh->b_size;
  430. }
  431. STATIC void
  432. xfs_map_buffer(
  433. struct buffer_head *bh,
  434. xfs_iomap_t *mp,
  435. xfs_off_t offset,
  436. uint block_bits)
  437. {
  438. sector_t bn;
  439. ASSERT(mp->iomap_bn != IOMAP_DADDR_NULL);
  440. bn = (mp->iomap_bn >> (block_bits - BBSHIFT)) +
  441. ((offset - mp->iomap_offset) >> block_bits);
  442. ASSERT(bn || (mp->iomap_flags & IOMAP_REALTIME));
  443. bh->b_blocknr = bn;
  444. set_buffer_mapped(bh);
  445. }
  446. STATIC void
  447. xfs_map_at_offset(
  448. struct buffer_head *bh,
  449. loff_t offset,
  450. int block_bits,
  451. xfs_iomap_t *iomapp)
  452. {
  453. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  454. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  455. lock_buffer(bh);
  456. xfs_map_buffer(bh, iomapp, offset, block_bits);
  457. bh->b_bdev = iomapp->iomap_target->bt_bdev;
  458. set_buffer_mapped(bh);
  459. clear_buffer_delay(bh);
  460. clear_buffer_unwritten(bh);
  461. }
  462. /*
  463. * Look for a page at index that is suitable for clustering.
  464. */
  465. STATIC unsigned int
  466. xfs_probe_page(
  467. struct page *page,
  468. unsigned int pg_offset,
  469. int mapped)
  470. {
  471. int ret = 0;
  472. if (PageWriteback(page))
  473. return 0;
  474. if (page->mapping && PageDirty(page)) {
  475. if (page_has_buffers(page)) {
  476. struct buffer_head *bh, *head;
  477. bh = head = page_buffers(page);
  478. do {
  479. if (!buffer_uptodate(bh))
  480. break;
  481. if (mapped != buffer_mapped(bh))
  482. break;
  483. ret += bh->b_size;
  484. if (ret >= pg_offset)
  485. break;
  486. } while ((bh = bh->b_this_page) != head);
  487. } else
  488. ret = mapped ? 0 : PAGE_CACHE_SIZE;
  489. }
  490. return ret;
  491. }
  492. STATIC size_t
  493. xfs_probe_cluster(
  494. struct inode *inode,
  495. struct page *startpage,
  496. struct buffer_head *bh,
  497. struct buffer_head *head,
  498. int mapped)
  499. {
  500. struct pagevec pvec;
  501. pgoff_t tindex, tlast, tloff;
  502. size_t total = 0;
  503. int done = 0, i;
  504. /* First sum forwards in this page */
  505. do {
  506. if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
  507. return total;
  508. total += bh->b_size;
  509. } while ((bh = bh->b_this_page) != head);
  510. /* if we reached the end of the page, sum forwards in following pages */
  511. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  512. tindex = startpage->index + 1;
  513. /* Prune this back to avoid pathological behavior */
  514. tloff = min(tlast, startpage->index + 64);
  515. pagevec_init(&pvec, 0);
  516. while (!done && tindex <= tloff) {
  517. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  518. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  519. break;
  520. for (i = 0; i < pagevec_count(&pvec); i++) {
  521. struct page *page = pvec.pages[i];
  522. size_t pg_offset, len = 0;
  523. if (tindex == tlast) {
  524. pg_offset =
  525. i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
  526. if (!pg_offset) {
  527. done = 1;
  528. break;
  529. }
  530. } else
  531. pg_offset = PAGE_CACHE_SIZE;
  532. if (page->index == tindex && !TestSetPageLocked(page)) {
  533. len = xfs_probe_page(page, pg_offset, mapped);
  534. unlock_page(page);
  535. }
  536. if (!len) {
  537. done = 1;
  538. break;
  539. }
  540. total += len;
  541. tindex++;
  542. }
  543. pagevec_release(&pvec);
  544. cond_resched();
  545. }
  546. return total;
  547. }
  548. /*
  549. * Test if a given page is suitable for writing as part of an unwritten
  550. * or delayed allocate extent.
  551. */
  552. STATIC int
  553. xfs_is_delayed_page(
  554. struct page *page,
  555. unsigned int type)
  556. {
  557. if (PageWriteback(page))
  558. return 0;
  559. if (page->mapping && page_has_buffers(page)) {
  560. struct buffer_head *bh, *head;
  561. int acceptable = 0;
  562. bh = head = page_buffers(page);
  563. do {
  564. if (buffer_unwritten(bh))
  565. acceptable = (type == IOMAP_UNWRITTEN);
  566. else if (buffer_delay(bh))
  567. acceptable = (type == IOMAP_DELAY);
  568. else if (buffer_dirty(bh) && buffer_mapped(bh))
  569. acceptable = (type == 0);
  570. else
  571. break;
  572. } while ((bh = bh->b_this_page) != head);
  573. if (acceptable)
  574. return 1;
  575. }
  576. return 0;
  577. }
  578. /*
  579. * Allocate & map buffers for page given the extent map. Write it out.
  580. * except for the original page of a writepage, this is called on
  581. * delalloc/unwritten pages only, for the original page it is possible
  582. * that the page has no mapping at all.
  583. */
  584. STATIC int
  585. xfs_convert_page(
  586. struct inode *inode,
  587. struct page *page,
  588. loff_t tindex,
  589. xfs_iomap_t *mp,
  590. xfs_ioend_t **ioendp,
  591. struct writeback_control *wbc,
  592. int startio,
  593. int all_bh)
  594. {
  595. struct buffer_head *bh, *head;
  596. xfs_off_t end_offset;
  597. unsigned long p_offset;
  598. unsigned int type;
  599. int bbits = inode->i_blkbits;
  600. int len, page_dirty;
  601. int count = 0, done = 0, uptodate = 1;
  602. xfs_off_t offset = page_offset(page);
  603. if (page->index != tindex)
  604. goto fail;
  605. if (TestSetPageLocked(page))
  606. goto fail;
  607. if (PageWriteback(page))
  608. goto fail_unlock_page;
  609. if (page->mapping != inode->i_mapping)
  610. goto fail_unlock_page;
  611. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  612. goto fail_unlock_page;
  613. /*
  614. * page_dirty is initially a count of buffers on the page before
  615. * EOF and is decremented as we move each into a cleanable state.
  616. *
  617. * Derivation:
  618. *
  619. * End offset is the highest offset that this page should represent.
  620. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  621. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  622. * hence give us the correct page_dirty count. On any other page,
  623. * it will be zero and in that case we need page_dirty to be the
  624. * count of buffers on the page.
  625. */
  626. end_offset = min_t(unsigned long long,
  627. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  628. i_size_read(inode));
  629. len = 1 << inode->i_blkbits;
  630. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  631. PAGE_CACHE_SIZE);
  632. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  633. page_dirty = p_offset / len;
  634. bh = head = page_buffers(page);
  635. do {
  636. if (offset >= end_offset)
  637. break;
  638. if (!buffer_uptodate(bh))
  639. uptodate = 0;
  640. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  641. done = 1;
  642. continue;
  643. }
  644. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  645. if (buffer_unwritten(bh))
  646. type = IOMAP_UNWRITTEN;
  647. else
  648. type = IOMAP_DELAY;
  649. if (!xfs_iomap_valid(mp, offset)) {
  650. done = 1;
  651. continue;
  652. }
  653. ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
  654. ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
  655. xfs_map_at_offset(bh, offset, bbits, mp);
  656. if (startio) {
  657. xfs_add_to_ioend(inode, bh, offset,
  658. type, ioendp, done);
  659. } else {
  660. set_buffer_dirty(bh);
  661. unlock_buffer(bh);
  662. mark_buffer_dirty(bh);
  663. }
  664. page_dirty--;
  665. count++;
  666. } else {
  667. type = 0;
  668. if (buffer_mapped(bh) && all_bh && startio) {
  669. lock_buffer(bh);
  670. xfs_add_to_ioend(inode, bh, offset,
  671. type, ioendp, done);
  672. count++;
  673. page_dirty--;
  674. } else {
  675. done = 1;
  676. }
  677. }
  678. } while (offset += len, (bh = bh->b_this_page) != head);
  679. if (uptodate && bh == head)
  680. SetPageUptodate(page);
  681. if (startio) {
  682. if (count) {
  683. struct backing_dev_info *bdi;
  684. bdi = inode->i_mapping->backing_dev_info;
  685. wbc->nr_to_write--;
  686. if (bdi_write_congested(bdi)) {
  687. wbc->encountered_congestion = 1;
  688. done = 1;
  689. } else if (wbc->nr_to_write <= 0) {
  690. done = 1;
  691. }
  692. }
  693. xfs_start_page_writeback(page, wbc, !page_dirty, count);
  694. }
  695. return done;
  696. fail_unlock_page:
  697. unlock_page(page);
  698. fail:
  699. return 1;
  700. }
  701. /*
  702. * Convert & write out a cluster of pages in the same extent as defined
  703. * by mp and following the start page.
  704. */
  705. STATIC void
  706. xfs_cluster_write(
  707. struct inode *inode,
  708. pgoff_t tindex,
  709. xfs_iomap_t *iomapp,
  710. xfs_ioend_t **ioendp,
  711. struct writeback_control *wbc,
  712. int startio,
  713. int all_bh,
  714. pgoff_t tlast)
  715. {
  716. struct pagevec pvec;
  717. int done = 0, i;
  718. pagevec_init(&pvec, 0);
  719. while (!done && tindex <= tlast) {
  720. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  721. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  722. break;
  723. for (i = 0; i < pagevec_count(&pvec); i++) {
  724. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  725. iomapp, ioendp, wbc, startio, all_bh);
  726. if (done)
  727. break;
  728. }
  729. pagevec_release(&pvec);
  730. cond_resched();
  731. }
  732. }
  733. /*
  734. * Calling this without startio set means we are being asked to make a dirty
  735. * page ready for freeing it's buffers. When called with startio set then
  736. * we are coming from writepage.
  737. *
  738. * When called with startio set it is important that we write the WHOLE
  739. * page if possible.
  740. * The bh->b_state's cannot know if any of the blocks or which block for
  741. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  742. * only valid if the page itself isn't completely uptodate. Some layers
  743. * may clear the page dirty flag prior to calling write page, under the
  744. * assumption the entire page will be written out; by not writing out the
  745. * whole page the page can be reused before all valid dirty data is
  746. * written out. Note: in the case of a page that has been dirty'd by
  747. * mapwrite and but partially setup by block_prepare_write the
  748. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  749. * valid state, thus the whole page must be written out thing.
  750. */
  751. STATIC int
  752. xfs_page_state_convert(
  753. struct inode *inode,
  754. struct page *page,
  755. struct writeback_control *wbc,
  756. int startio,
  757. int unmapped) /* also implies page uptodate */
  758. {
  759. struct buffer_head *bh, *head;
  760. xfs_iomap_t iomap;
  761. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  762. loff_t offset;
  763. unsigned long p_offset = 0;
  764. unsigned int type;
  765. __uint64_t end_offset;
  766. pgoff_t end_index, last_index, tlast;
  767. ssize_t size, len;
  768. int flags, err, iomap_valid = 0, uptodate = 1;
  769. int page_dirty, count = 0;
  770. int trylock = 0;
  771. int all_bh = unmapped;
  772. if (startio) {
  773. if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
  774. trylock |= BMAPI_TRYLOCK;
  775. }
  776. /* Is this page beyond the end of the file? */
  777. offset = i_size_read(inode);
  778. end_index = offset >> PAGE_CACHE_SHIFT;
  779. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  780. if (page->index >= end_index) {
  781. if ((page->index >= end_index + 1) ||
  782. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  783. if (startio)
  784. unlock_page(page);
  785. return 0;
  786. }
  787. }
  788. /*
  789. * page_dirty is initially a count of buffers on the page before
  790. * EOF and is decremented as we move each into a cleanable state.
  791. *
  792. * Derivation:
  793. *
  794. * End offset is the highest offset that this page should represent.
  795. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  796. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  797. * hence give us the correct page_dirty count. On any other page,
  798. * it will be zero and in that case we need page_dirty to be the
  799. * count of buffers on the page.
  800. */
  801. end_offset = min_t(unsigned long long,
  802. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  803. len = 1 << inode->i_blkbits;
  804. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  805. PAGE_CACHE_SIZE);
  806. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  807. page_dirty = p_offset / len;
  808. bh = head = page_buffers(page);
  809. offset = page_offset(page);
  810. flags = -1;
  811. type = 0;
  812. /* TODO: cleanup count and page_dirty */
  813. do {
  814. if (offset >= end_offset)
  815. break;
  816. if (!buffer_uptodate(bh))
  817. uptodate = 0;
  818. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  819. /*
  820. * the iomap is actually still valid, but the ioend
  821. * isn't. shouldn't happen too often.
  822. */
  823. iomap_valid = 0;
  824. continue;
  825. }
  826. if (iomap_valid)
  827. iomap_valid = xfs_iomap_valid(&iomap, offset);
  828. /*
  829. * First case, map an unwritten extent and prepare for
  830. * extent state conversion transaction on completion.
  831. *
  832. * Second case, allocate space for a delalloc buffer.
  833. * We can return EAGAIN here in the release page case.
  834. *
  835. * Third case, an unmapped buffer was found, and we are
  836. * in a path where we need to write the whole page out.
  837. */
  838. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  839. ((buffer_uptodate(bh) || PageUptodate(page)) &&
  840. !buffer_mapped(bh) && (unmapped || startio))) {
  841. /*
  842. * Make sure we don't use a read-only iomap
  843. */
  844. if (flags == BMAPI_READ)
  845. iomap_valid = 0;
  846. if (buffer_unwritten(bh)) {
  847. type = IOMAP_UNWRITTEN;
  848. flags = BMAPI_WRITE | BMAPI_IGNSTATE;
  849. } else if (buffer_delay(bh)) {
  850. type = IOMAP_DELAY;
  851. flags = BMAPI_ALLOCATE | trylock;
  852. } else {
  853. type = IOMAP_NEW;
  854. flags = BMAPI_WRITE | BMAPI_MMAP;
  855. }
  856. if (!iomap_valid) {
  857. if (type == IOMAP_NEW) {
  858. size = xfs_probe_cluster(inode,
  859. page, bh, head, 0);
  860. } else {
  861. size = len;
  862. }
  863. err = xfs_map_blocks(inode, offset, size,
  864. &iomap, flags);
  865. if (err)
  866. goto error;
  867. iomap_valid = xfs_iomap_valid(&iomap, offset);
  868. }
  869. if (iomap_valid) {
  870. xfs_map_at_offset(bh, offset,
  871. inode->i_blkbits, &iomap);
  872. if (startio) {
  873. xfs_add_to_ioend(inode, bh, offset,
  874. type, &ioend,
  875. !iomap_valid);
  876. } else {
  877. set_buffer_dirty(bh);
  878. unlock_buffer(bh);
  879. mark_buffer_dirty(bh);
  880. }
  881. page_dirty--;
  882. count++;
  883. }
  884. } else if (buffer_uptodate(bh) && startio) {
  885. /*
  886. * we got here because the buffer is already mapped.
  887. * That means it must already have extents allocated
  888. * underneath it. Map the extent by reading it.
  889. */
  890. if (!iomap_valid || type != 0) {
  891. flags = BMAPI_READ;
  892. size = xfs_probe_cluster(inode, page, bh,
  893. head, 1);
  894. err = xfs_map_blocks(inode, offset, size,
  895. &iomap, flags);
  896. if (err)
  897. goto error;
  898. iomap_valid = xfs_iomap_valid(&iomap, offset);
  899. }
  900. type = 0;
  901. if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
  902. ASSERT(buffer_mapped(bh));
  903. if (iomap_valid)
  904. all_bh = 1;
  905. xfs_add_to_ioend(inode, bh, offset, type,
  906. &ioend, !iomap_valid);
  907. page_dirty--;
  908. count++;
  909. } else {
  910. iomap_valid = 0;
  911. }
  912. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  913. (unmapped || startio)) {
  914. iomap_valid = 0;
  915. }
  916. if (!iohead)
  917. iohead = ioend;
  918. } while (offset += len, ((bh = bh->b_this_page) != head));
  919. if (uptodate && bh == head)
  920. SetPageUptodate(page);
  921. if (startio)
  922. xfs_start_page_writeback(page, wbc, 1, count);
  923. if (ioend && iomap_valid) {
  924. offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
  925. PAGE_CACHE_SHIFT;
  926. tlast = min_t(pgoff_t, offset, last_index);
  927. xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
  928. wbc, startio, all_bh, tlast);
  929. }
  930. if (iohead)
  931. xfs_submit_ioend(iohead);
  932. return page_dirty;
  933. error:
  934. if (iohead)
  935. xfs_cancel_ioend(iohead);
  936. /*
  937. * If it's delalloc and we have nowhere to put it,
  938. * throw it away, unless the lower layers told
  939. * us to try again.
  940. */
  941. if (err != -EAGAIN) {
  942. if (!unmapped)
  943. block_invalidatepage(page, 0);
  944. ClearPageUptodate(page);
  945. }
  946. return err;
  947. }
  948. /*
  949. * writepage: Called from one of two places:
  950. *
  951. * 1. we are flushing a delalloc buffer head.
  952. *
  953. * 2. we are writing out a dirty page. Typically the page dirty
  954. * state is cleared before we get here. In this case is it
  955. * conceivable we have no buffer heads.
  956. *
  957. * For delalloc space on the page we need to allocate space and
  958. * flush it. For unmapped buffer heads on the page we should
  959. * allocate space if the page is uptodate. For any other dirty
  960. * buffer heads on the page we should flush them.
  961. *
  962. * If we detect that a transaction would be required to flush
  963. * the page, we have to check the process flags first, if we
  964. * are already in a transaction or disk I/O during allocations
  965. * is off, we need to fail the writepage and redirty the page.
  966. */
  967. STATIC int
  968. xfs_vm_writepage(
  969. struct page *page,
  970. struct writeback_control *wbc)
  971. {
  972. int error;
  973. int need_trans;
  974. int delalloc, unmapped, unwritten;
  975. struct inode *inode = page->mapping->host;
  976. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  977. /*
  978. * We need a transaction if:
  979. * 1. There are delalloc buffers on the page
  980. * 2. The page is uptodate and we have unmapped buffers
  981. * 3. The page is uptodate and we have no buffers
  982. * 4. There are unwritten buffers on the page
  983. */
  984. if (!page_has_buffers(page)) {
  985. unmapped = 1;
  986. need_trans = 1;
  987. } else {
  988. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  989. if (!PageUptodate(page))
  990. unmapped = 0;
  991. need_trans = delalloc + unmapped + unwritten;
  992. }
  993. /*
  994. * If we need a transaction and the process flags say
  995. * we are already in a transaction, or no IO is allowed
  996. * then mark the page dirty again and leave the page
  997. * as is.
  998. */
  999. if (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_blocks(
  1083. struct inode *inode,
  1084. sector_t iblock,
  1085. struct buffer_head *bh_result,
  1086. int create,
  1087. int direct,
  1088. bmapi_flags_t flags)
  1089. {
  1090. vnode_t *vp = vn_from_inode(inode);
  1091. xfs_iomap_t iomap;
  1092. xfs_off_t offset;
  1093. ssize_t size;
  1094. int niomap = 1;
  1095. int error;
  1096. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1097. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  1098. size = bh_result->b_size;
  1099. VOP_BMAP(vp, offset, size,
  1100. create ? flags : BMAPI_READ, &iomap, &niomap, error);
  1101. if (error)
  1102. return -error;
  1103. if (niomap == 0)
  1104. return 0;
  1105. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  1106. /*
  1107. * For unwritten extents do not report a disk address on
  1108. * the read case (treat as if we're reading into a hole).
  1109. */
  1110. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  1111. xfs_map_buffer(bh_result, &iomap, offset,
  1112. inode->i_blkbits);
  1113. }
  1114. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  1115. if (direct)
  1116. bh_result->b_private = inode;
  1117. set_buffer_unwritten(bh_result);
  1118. set_buffer_delay(bh_result);
  1119. }
  1120. }
  1121. /*
  1122. * If this is a realtime file, data may be on a different device.
  1123. * to that pointed to from the buffer_head b_bdev currently.
  1124. */
  1125. bh_result->b_bdev = iomap.iomap_target->bt_bdev;
  1126. /*
  1127. * If we previously allocated a block out beyond eof and we are
  1128. * now coming back to use it then we will need to flag it as new
  1129. * 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 (direct || size > (1 << inode->i_blkbits)) {
  1144. ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
  1145. offset = min_t(xfs_off_t,
  1146. iomap.iomap_bsize - iomap.iomap_delta, size);
  1147. bh_result->b_size = (ssize_t)min_t(xfs_off_t, LONG_MAX, offset);
  1148. }
  1149. return 0;
  1150. }
  1151. int
  1152. xfs_get_blocks(
  1153. struct inode *inode,
  1154. sector_t iblock,
  1155. struct buffer_head *bh_result,
  1156. int create)
  1157. {
  1158. return __xfs_get_blocks(inode, iblock,
  1159. bh_result, create, 0, BMAPI_WRITE);
  1160. }
  1161. STATIC int
  1162. xfs_get_blocks_direct(
  1163. struct inode *inode,
  1164. sector_t iblock,
  1165. struct buffer_head *bh_result,
  1166. int create)
  1167. {
  1168. return __xfs_get_blocks(inode, iblock,
  1169. bh_result, create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  1170. }
  1171. STATIC void
  1172. xfs_end_io_direct(
  1173. struct kiocb *iocb,
  1174. loff_t offset,
  1175. ssize_t size,
  1176. void *private)
  1177. {
  1178. xfs_ioend_t *ioend = iocb->private;
  1179. /*
  1180. * Non-NULL private data means we need to issue a transaction to
  1181. * convert a range from unwritten to written extents. This needs
  1182. * to happen from process context but aio+dio I/O completion
  1183. * happens from irq context so we need to defer it to a workqueue.
  1184. * This is not necessary for synchronous direct I/O, but we do
  1185. * it anyway to keep the code uniform and simpler.
  1186. *
  1187. * The core direct I/O code might be changed to always call the
  1188. * completion handler in the future, in which case all this can
  1189. * go away.
  1190. */
  1191. if (private && size > 0) {
  1192. ioend->io_offset = offset;
  1193. ioend->io_size = size;
  1194. xfs_finish_ioend(ioend);
  1195. } else {
  1196. ASSERT(size >= 0);
  1197. xfs_destroy_ioend(ioend);
  1198. }
  1199. /*
  1200. * blockdev_direct_IO can return an error even after the I/O
  1201. * completion handler was called. Thus we need to protect
  1202. * against double-freeing.
  1203. */
  1204. iocb->private = NULL;
  1205. }
  1206. STATIC ssize_t
  1207. xfs_vm_direct_IO(
  1208. int rw,
  1209. struct kiocb *iocb,
  1210. const struct iovec *iov,
  1211. loff_t offset,
  1212. unsigned long nr_segs)
  1213. {
  1214. struct file *file = iocb->ki_filp;
  1215. struct inode *inode = file->f_mapping->host;
  1216. vnode_t *vp = vn_from_inode(inode);
  1217. xfs_iomap_t iomap;
  1218. int maps = 1;
  1219. int error;
  1220. ssize_t ret;
  1221. VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
  1222. if (error)
  1223. return -error;
  1224. iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
  1225. ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
  1226. iomap.iomap_target->bt_bdev,
  1227. iov, offset, nr_segs,
  1228. xfs_get_blocks_direct,
  1229. xfs_end_io_direct);
  1230. if (unlikely(ret <= 0 && iocb->private))
  1231. xfs_destroy_ioend(iocb->private);
  1232. return ret;
  1233. }
  1234. STATIC int
  1235. xfs_vm_prepare_write(
  1236. struct file *file,
  1237. struct page *page,
  1238. unsigned int from,
  1239. unsigned int to)
  1240. {
  1241. return block_prepare_write(page, from, to, xfs_get_blocks);
  1242. }
  1243. STATIC sector_t
  1244. xfs_vm_bmap(
  1245. struct address_space *mapping,
  1246. sector_t block)
  1247. {
  1248. struct inode *inode = (struct inode *)mapping->host;
  1249. vnode_t *vp = vn_from_inode(inode);
  1250. int error;
  1251. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  1252. VOP_RWLOCK(vp, VRWLOCK_READ);
  1253. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  1254. VOP_RWUNLOCK(vp, VRWLOCK_READ);
  1255. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1256. }
  1257. STATIC int
  1258. xfs_vm_readpage(
  1259. struct file *unused,
  1260. struct page *page)
  1261. {
  1262. return mpage_readpage(page, xfs_get_blocks);
  1263. }
  1264. STATIC int
  1265. xfs_vm_readpages(
  1266. struct file *unused,
  1267. struct address_space *mapping,
  1268. struct list_head *pages,
  1269. unsigned nr_pages)
  1270. {
  1271. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1272. }
  1273. STATIC void
  1274. xfs_vm_invalidatepage(
  1275. struct page *page,
  1276. unsigned long offset)
  1277. {
  1278. xfs_page_trace(XFS_INVALIDPAGE_ENTER,
  1279. page->mapping->host, page, offset);
  1280. block_invalidatepage(page, offset);
  1281. }
  1282. struct address_space_operations xfs_address_space_operations = {
  1283. .readpage = xfs_vm_readpage,
  1284. .readpages = xfs_vm_readpages,
  1285. .writepage = xfs_vm_writepage,
  1286. .sync_page = block_sync_page,
  1287. .releasepage = xfs_vm_releasepage,
  1288. .invalidatepage = xfs_vm_invalidatepage,
  1289. .prepare_write = xfs_vm_prepare_write,
  1290. .commit_write = generic_commit_write,
  1291. .bmap = xfs_vm_bmap,
  1292. .direct_IO = xfs_vm_direct_IO,
  1293. .migratepage = buffer_migrate_page,
  1294. };